Statistics
| Branch: | Revision:

root / vl.c @ 640f42e4

History | View | Annotate | Download (141.1 kB)

1 0824d6fc bellard
/*
2 80cabfad bellard
 * QEMU System Emulator
3 5fafdf24 ths
 *
4 68d0f70e bellard
 * Copyright (c) 2003-2008 Fabrice Bellard
5 5fafdf24 ths
 *
6 1df912cf bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 1df912cf bellard
 * of this software and associated documentation files (the "Software"), to deal
8 1df912cf bellard
 * in the Software without restriction, including without limitation the rights
9 1df912cf bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 1df912cf bellard
 * copies of the Software, and to permit persons to whom the Software is
11 1df912cf bellard
 * furnished to do so, subject to the following conditions:
12 1df912cf bellard
 *
13 1df912cf bellard
 * The above copyright notice and this permission notice shall be included in
14 1df912cf bellard
 * all copies or substantial portions of the Software.
15 1df912cf bellard
 *
16 1df912cf bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 1df912cf bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 1df912cf bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 1df912cf bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 1df912cf bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 1df912cf bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 1df912cf bellard
 * THE SOFTWARE.
23 0824d6fc bellard
 */
24 0824d6fc bellard
#include <unistd.h>
25 0824d6fc bellard
#include <fcntl.h>
26 0824d6fc bellard
#include <signal.h>
27 0824d6fc bellard
#include <time.h>
28 0824d6fc bellard
#include <errno.h>
29 67b915a5 bellard
#include <sys/time.h>
30 c88676f8 bellard
#include <zlib.h>
31 67b915a5 bellard
32 179a2c19 blueswir1
/* Needed early for HOST_BSD etc. */
33 d40cdb10 blueswir1
#include "config-host.h"
34 d40cdb10 blueswir1
35 67b915a5 bellard
#ifndef _WIN32
36 0858532e aliguori
#include <pwd.h>
37 67b915a5 bellard
#include <sys/times.h>
38 f1510b2c bellard
#include <sys/wait.h>
39 67b915a5 bellard
#include <termios.h>
40 67b915a5 bellard
#include <sys/mman.h>
41 f1510b2c bellard
#include <sys/ioctl.h>
42 24646c7e blueswir1
#include <sys/resource.h>
43 f1510b2c bellard
#include <sys/socket.h>
44 c94c8d64 bellard
#include <netinet/in.h>
45 24646c7e blueswir1
#include <net/if.h>
46 24646c7e blueswir1
#if defined(__NetBSD__)
47 24646c7e blueswir1
#include <net/if_tap.h>
48 24646c7e blueswir1
#endif
49 24646c7e blueswir1
#ifdef __linux__
50 24646c7e blueswir1
#include <linux/if_tun.h>
51 24646c7e blueswir1
#endif
52 24646c7e blueswir1
#include <arpa/inet.h>
53 9d728e8c bellard
#include <dirent.h>
54 7c9d8e07 bellard
#include <netdb.h>
55 cb4b976b ths
#include <sys/select.h>
56 179a2c19 blueswir1
#ifdef HOST_BSD
57 7d3505c5 bellard
#include <sys/stat.h>
58 c5e97233 blueswir1
#if defined(__FreeBSD__) || defined(__DragonFly__)
59 7d3505c5 bellard
#include <libutil.h>
60 24646c7e blueswir1
#else
61 24646c7e blueswir1
#include <util.h>
62 128ab2ff blueswir1
#endif
63 5c40d2bd ths
#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
64 5c40d2bd ths
#include <freebsd/stdlib.h>
65 7d3505c5 bellard
#else
66 223f0d72 blueswir1
#ifdef __linux__
67 7d3505c5 bellard
#include <pty.h>
68 7d3505c5 bellard
#include <malloc.h>
69 fd872598 bellard
#include <linux/rtc.h>
70 bd494f4c ths
71 bd494f4c ths
/* For the benefit of older linux systems which don't supply it,
72 bd494f4c ths
   we use a local copy of hpet.h. */
73 bd494f4c ths
/* #include <linux/hpet.h> */
74 bd494f4c ths
#include "hpet.h"
75 bd494f4c ths
76 e57a8c0e bellard
#include <linux/ppdev.h>
77 5867c88a ths
#include <linux/parport.h>
78 223f0d72 blueswir1
#endif
79 223f0d72 blueswir1
#ifdef __sun__
80 d5d10bc3 ths
#include <sys/stat.h>
81 d5d10bc3 ths
#include <sys/ethernet.h>
82 d5d10bc3 ths
#include <sys/sockio.h>
83 d5d10bc3 ths
#include <netinet/arp.h>
84 d5d10bc3 ths
#include <netinet/in.h>
85 d5d10bc3 ths
#include <netinet/in_systm.h>
86 d5d10bc3 ths
#include <netinet/ip.h>
87 d5d10bc3 ths
#include <netinet/ip_icmp.h> // must come after ip.h
88 d5d10bc3 ths
#include <netinet/udp.h>
89 d5d10bc3 ths
#include <netinet/tcp.h>
90 d5d10bc3 ths
#include <net/if.h>
91 d5d10bc3 ths
#include <syslog.h>
92 d5d10bc3 ths
#include <stropts.h>
93 67b915a5 bellard
#endif
94 7d3505c5 bellard
#endif
95 ec530c81 bellard
#endif
96 67b915a5 bellard
97 9892fbfb blueswir1
#if defined(__OpenBSD__)
98 9892fbfb blueswir1
#include <util.h>
99 9892fbfb blueswir1
#endif
100 9892fbfb blueswir1
101 8a16d273 ths
#if defined(CONFIG_VDE)
102 8a16d273 ths
#include <libvdeplug.h>
103 8a16d273 ths
#endif
104 8a16d273 ths
105 67b915a5 bellard
#ifdef _WIN32
106 49dc768d aliguori
#include <windows.h>
107 7d3505c5 bellard
#include <malloc.h>
108 67b915a5 bellard
#include <sys/timeb.h>
109 4fddf62a ths
#include <mmsystem.h>
110 67b915a5 bellard
#define getopt_long_only getopt_long
111 67b915a5 bellard
#define memalign(align, size) malloc(size)
112 67b915a5 bellard
#endif
113 67b915a5 bellard
114 73332e5c bellard
#ifdef CONFIG_SDL
115 96bcd4f8 bellard
#ifdef __APPLE__
116 83fb7adf bellard
#include <SDL/SDL.h>
117 880fec5d malc
int qemu_main(int argc, char **argv, char **envp);
118 880fec5d malc
int main(int argc, char **argv)
119 880fec5d malc
{
120 880fec5d malc
    qemu_main(argc, argv, NULL);
121 880fec5d malc
}
122 880fec5d malc
#undef main
123 880fec5d malc
#define main qemu_main
124 96bcd4f8 bellard
#endif
125 73332e5c bellard
#endif /* CONFIG_SDL */
126 0824d6fc bellard
127 5b0753e0 bellard
#ifdef CONFIG_COCOA
128 5b0753e0 bellard
#undef main
129 5b0753e0 bellard
#define main qemu_main
130 5b0753e0 bellard
#endif /* CONFIG_COCOA */
131 5b0753e0 bellard
132 511d2b14 blueswir1
#include "hw/hw.h"
133 511d2b14 blueswir1
#include "hw/boards.h"
134 511d2b14 blueswir1
#include "hw/usb.h"
135 511d2b14 blueswir1
#include "hw/pcmcia.h"
136 511d2b14 blueswir1
#include "hw/pc.h"
137 511d2b14 blueswir1
#include "hw/audiodev.h"
138 511d2b14 blueswir1
#include "hw/isa.h"
139 511d2b14 blueswir1
#include "hw/baum.h"
140 511d2b14 blueswir1
#include "hw/bt.h"
141 b6f6e3d3 aliguori
#include "hw/smbios.h"
142 5ef4efa4 aurel32
#include "bt-host.h"
143 511d2b14 blueswir1
#include "net.h"
144 511d2b14 blueswir1
#include "monitor.h"
145 511d2b14 blueswir1
#include "console.h"
146 511d2b14 blueswir1
#include "sysemu.h"
147 511d2b14 blueswir1
#include "gdbstub.h"
148 511d2b14 blueswir1
#include "qemu-timer.h"
149 511d2b14 blueswir1
#include "qemu-char.h"
150 511d2b14 blueswir1
#include "cache-utils.h"
151 511d2b14 blueswir1
#include "block.h"
152 a718acec blueswir1
#include "dma.h"
153 511d2b14 blueswir1
#include "audio/audio.h"
154 511d2b14 blueswir1
#include "migration.h"
155 511d2b14 blueswir1
#include "kvm.h"
156 511d2b14 blueswir1
#include "balloon.h"
157 511d2b14 blueswir1
158 0824d6fc bellard
#include "disas.h"
159 fc01f7e7 bellard
160 8a7ddc38 bellard
#include "exec-all.h"
161 0824d6fc bellard
162 511d2b14 blueswir1
#include "qemu_socket.h"
163 511d2b14 blueswir1
164 511d2b14 blueswir1
#if defined(CONFIG_SLIRP)
165 511d2b14 blueswir1
#include "libslirp.h"
166 511d2b14 blueswir1
#endif
167 511d2b14 blueswir1
168 0824d6fc bellard
//#define DEBUG_UNUSED_IOPORT
169 fd872598 bellard
//#define DEBUG_IOPORT
170 9dc63a1e blueswir1
//#define DEBUG_NET
171 9dc63a1e blueswir1
//#define DEBUG_SLIRP
172 330d0414 bellard
173 d12d51d5 aliguori
174 d12d51d5 aliguori
#ifdef DEBUG_IOPORT
175 93fcfe39 aliguori
#  define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
176 d12d51d5 aliguori
#else
177 d12d51d5 aliguori
#  define LOG_IOPORT(...) do { } while (0)
178 d12d51d5 aliguori
#endif
179 d12d51d5 aliguori
180 1bfe856e bellard
#define DEFAULT_RAM_SIZE 128
181 313aa567 bellard
182 0d92ed30 pbrook
/* Max number of USB devices that can be specified on the commandline.  */
183 0d92ed30 pbrook
#define MAX_USB_CMDLINE 8
184 0d92ed30 pbrook
185 dc72ac14 balrog
/* Max number of bluetooth switches on the commandline.  */
186 dc72ac14 balrog
#define MAX_BT_CMDLINE 10
187 dc72ac14 balrog
188 7dea1da4 bellard
/* XXX: use a two level table to limit memory usage */
189 7dea1da4 bellard
#define MAX_IOPORTS 65536
190 0824d6fc bellard
191 80cabfad bellard
const char *bios_dir = CONFIG_QEMU_SHAREDIR;
192 1192dad8 j_mayer
const char *bios_name = NULL;
193 dbed7e40 blueswir1
static void *ioport_opaque[MAX_IOPORTS];
194 dbed7e40 blueswir1
static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
195 dbed7e40 blueswir1
static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
196 e4bcb14c ths
/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
197 faea38e7 bellard
   to store the VM snapshots */
198 e4bcb14c ths
DriveInfo drives_table[MAX_DRIVES+1];
199 e4bcb14c ths
int nb_drives;
200 dbed7e40 blueswir1
static int vga_ram_size;
201 cb5a7aa8 malc
enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
202 3023f332 aliguori
static DisplayState *display_state;
203 a20dd508 bellard
int nographic;
204 dbed7e40 blueswir1
static int curses;
205 7d957bd8 aliguori
static int sdl;
206 3d11d0eb bellard
const char* keyboard_layout = NULL;
207 313aa567 bellard
int64_t ticks_per_sec;
208 00f82b8a aurel32
ram_addr_t ram_size;
209 c4b1fcc0 bellard
int nb_nics;
210 7c9d8e07 bellard
NICInfo nd_table[MAX_NICS];
211 8a7ddc38 bellard
int vm_running;
212 c0f4ce77 aliguori
static int autostart;
213 f6503059 balrog
static int rtc_utc = 1;
214 f6503059 balrog
static int rtc_date_offset = -1; /* -1 means no change */
215 1bfe856e bellard
int cirrus_vga_enabled = 1;
216 c2b3b41a aliguori
int std_vga_enabled = 0;
217 d34cab9f ths
int vmsvga_enabled = 0;
218 d827220b bellard
#ifdef TARGET_SPARC
219 d827220b bellard
int graphic_width = 1024;
220 d827220b bellard
int graphic_height = 768;
221 eee0b836 blueswir1
int graphic_depth = 8;
222 d827220b bellard
#else
223 1bfe856e bellard
int graphic_width = 800;
224 1bfe856e bellard
int graphic_height = 600;
225 e9b137c2 bellard
int graphic_depth = 15;
226 eee0b836 blueswir1
#endif
227 dbed7e40 blueswir1
static int full_screen = 0;
228 634a21f6 blueswir1
#ifdef CONFIG_SDL
229 dbed7e40 blueswir1
static int no_frame = 0;
230 634a21f6 blueswir1
#endif
231 667accab ths
int no_quit = 0;
232 8d11df9e bellard
CharDriverState *serial_hds[MAX_SERIAL_PORTS];
233 6508fe59 bellard
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
234 9ede2fde aliguori
CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
235 a09db21f bellard
#ifdef TARGET_I386
236 a09db21f bellard
int win2k_install_hack = 0;
237 73822ec8 aliguori
int rtc_td_hack = 0;
238 a09db21f bellard
#endif
239 bb36d470 bellard
int usb_enabled = 0;
240 1b530a6d aurel32
int singlestep = 0;
241 6a00d601 bellard
int smp_cpus = 1;
242 73fc9742 ths
const char *vnc_display;
243 6515b203 bellard
int acpi_enabled = 1;
244 16b29ae1 aliguori
int no_hpet = 0;
245 52ca8d6a bellard
int fd_bootchk = 1;
246 d1beab82 bellard
int no_reboot = 0;
247 b2f76161 aurel32
int no_shutdown = 0;
248 9467cd46 balrog
int cursor_hide = 1;
249 a171fe39 balrog
int graphic_rotate = 0;
250 b9e82a59 blueswir1
#ifndef _WIN32
251 71e3ceb8 ths
int daemonize = 0;
252 b9e82a59 blueswir1
#endif
253 9ae02555 ths
const char *option_rom[MAX_OPTION_ROMS];
254 9ae02555 ths
int nb_option_roms;
255 8e71621f pbrook
int semihosting_enabled = 0;
256 2b8f2d41 balrog
#ifdef TARGET_ARM
257 2b8f2d41 balrog
int old_param = 0;
258 2b8f2d41 balrog
#endif
259 c35734b2 ths
const char *qemu_name;
260 3780e197 ths
int alt_grab = 0;
261 95efd11c blueswir1
#if defined(TARGET_SPARC) || defined(TARGET_PPC)
262 66508601 blueswir1
unsigned int nb_prom_envs = 0;
263 66508601 blueswir1
const char *prom_envs[MAX_PROM_ENVS];
264 66508601 blueswir1
#endif
265 ec691c80 aliguori
int nb_drives_opt;
266 ec691c80 aliguori
struct drive_opt drives_opt[MAX_DRIVES];
267 0824d6fc bellard
268 ee5605e5 balrog
static CPUState *cur_cpu;
269 ee5605e5 balrog
static CPUState *next_cpu;
270 76ea08f9 balrog
static int event_pending = 1;
271 bf20dc07 ths
/* Conversion factor from emulated instructions to virtual clock ticks.  */
272 2e70f6ef pbrook
static int icount_time_shift;
273 bf20dc07 ths
/* Arbitrarily pick 1MIPS as the minimum allowable speed.  */
274 2e70f6ef pbrook
#define MAX_ICOUNT_SHIFT 10
275 2e70f6ef pbrook
/* Compensate for varying guest execution speed.  */
276 2e70f6ef pbrook
static int64_t qemu_icount_bias;
277 dbed7e40 blueswir1
static QEMUTimer *icount_rt_timer;
278 dbed7e40 blueswir1
static QEMUTimer *icount_vm_timer;
279 9043b62d blueswir1
static QEMUTimer *nographic_timer;
280 ee5605e5 balrog
281 8fcb1b90 blueswir1
uint8_t qemu_uuid[16];
282 8fcb1b90 blueswir1
283 0824d6fc bellard
/***********************************************************/
284 26aa7d72 bellard
/* x86 ISA bus support */
285 26aa7d72 bellard
286 26aa7d72 bellard
target_phys_addr_t isa_mem_base = 0;
287 3de388f6 bellard
PicState2 *isa_pic;
288 0824d6fc bellard
289 477e3edf aliguori
static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
290 477e3edf aliguori
static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
291 477e3edf aliguori
292 477e3edf aliguori
static uint32_t ioport_read(int index, uint32_t address)
293 477e3edf aliguori
{
294 477e3edf aliguori
    static IOPortReadFunc *default_func[3] = {
295 477e3edf aliguori
        default_ioport_readb,
296 477e3edf aliguori
        default_ioport_readw,
297 477e3edf aliguori
        default_ioport_readl
298 477e3edf aliguori
    };
299 477e3edf aliguori
    IOPortReadFunc *func = ioport_read_table[index][address];
300 477e3edf aliguori
    if (!func)
301 477e3edf aliguori
        func = default_func[index];
302 477e3edf aliguori
    return func(ioport_opaque[address], address);
303 477e3edf aliguori
}
304 477e3edf aliguori
305 477e3edf aliguori
static void ioport_write(int index, uint32_t address, uint32_t data)
306 477e3edf aliguori
{
307 477e3edf aliguori
    static IOPortWriteFunc *default_func[3] = {
308 477e3edf aliguori
        default_ioport_writeb,
309 477e3edf aliguori
        default_ioport_writew,
310 477e3edf aliguori
        default_ioport_writel
311 477e3edf aliguori
    };
312 477e3edf aliguori
    IOPortWriteFunc *func = ioport_write_table[index][address];
313 477e3edf aliguori
    if (!func)
314 477e3edf aliguori
        func = default_func[index];
315 477e3edf aliguori
    func(ioport_opaque[address], address, data);
316 477e3edf aliguori
}
317 477e3edf aliguori
318 9596ebb7 pbrook
static uint32_t default_ioport_readb(void *opaque, uint32_t address)
319 0824d6fc bellard
{
320 0824d6fc bellard
#ifdef DEBUG_UNUSED_IOPORT
321 1196be37 ths
    fprintf(stderr, "unused inb: port=0x%04x\n", address);
322 0824d6fc bellard
#endif
323 fc01f7e7 bellard
    return 0xff;
324 0824d6fc bellard
}
325 0824d6fc bellard
326 9596ebb7 pbrook
static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
327 0824d6fc bellard
{
328 0824d6fc bellard
#ifdef DEBUG_UNUSED_IOPORT
329 1196be37 ths
    fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
330 0824d6fc bellard
#endif
331 0824d6fc bellard
}
332 0824d6fc bellard
333 0824d6fc bellard
/* default is to make two byte accesses */
334 9596ebb7 pbrook
static uint32_t default_ioport_readw(void *opaque, uint32_t address)
335 0824d6fc bellard
{
336 0824d6fc bellard
    uint32_t data;
337 477e3edf aliguori
    data = ioport_read(0, address);
338 db45c29a bellard
    address = (address + 1) & (MAX_IOPORTS - 1);
339 477e3edf aliguori
    data |= ioport_read(0, address) << 8;
340 0824d6fc bellard
    return data;
341 0824d6fc bellard
}
342 0824d6fc bellard
343 9596ebb7 pbrook
static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
344 0824d6fc bellard
{
345 477e3edf aliguori
    ioport_write(0, address, data & 0xff);
346 db45c29a bellard
    address = (address + 1) & (MAX_IOPORTS - 1);
347 477e3edf aliguori
    ioport_write(0, address, (data >> 8) & 0xff);
348 0824d6fc bellard
}
349 0824d6fc bellard
350 9596ebb7 pbrook
static uint32_t default_ioport_readl(void *opaque, uint32_t address)
351 0824d6fc bellard
{
352 fc01f7e7 bellard
#ifdef DEBUG_UNUSED_IOPORT
353 1196be37 ths
    fprintf(stderr, "unused inl: port=0x%04x\n", address);
354 fc01f7e7 bellard
#endif
355 fc01f7e7 bellard
    return 0xffffffff;
356 0824d6fc bellard
}
357 0824d6fc bellard
358 9596ebb7 pbrook
static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
359 0824d6fc bellard
{
360 fc01f7e7 bellard
#ifdef DEBUG_UNUSED_IOPORT
361 1196be37 ths
    fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
362 fc01f7e7 bellard
#endif
363 0824d6fc bellard
}
364 0824d6fc bellard
365 fc01f7e7 bellard
/* size is the word size in byte */
366 5fafdf24 ths
int register_ioport_read(int start, int length, int size,
367 c4b1fcc0 bellard
                         IOPortReadFunc *func, void *opaque)
368 f1510b2c bellard
{
369 fc01f7e7 bellard
    int i, bsize;
370 f1510b2c bellard
371 c4b1fcc0 bellard
    if (size == 1) {
372 fc01f7e7 bellard
        bsize = 0;
373 c4b1fcc0 bellard
    } else if (size == 2) {
374 fc01f7e7 bellard
        bsize = 1;
375 c4b1fcc0 bellard
    } else if (size == 4) {
376 fc01f7e7 bellard
        bsize = 2;
377 c4b1fcc0 bellard
    } else {
378 88fdf56f balrog
        hw_error("register_ioport_read: invalid size");
379 fc01f7e7 bellard
        return -1;
380 c4b1fcc0 bellard
    }
381 c4b1fcc0 bellard
    for(i = start; i < start + length; i += size) {
382 fc01f7e7 bellard
        ioport_read_table[bsize][i] = func;
383 c4b1fcc0 bellard
        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
384 88fdf56f balrog
            hw_error("register_ioport_read: invalid opaque");
385 c4b1fcc0 bellard
        ioport_opaque[i] = opaque;
386 c4b1fcc0 bellard
    }
387 f1510b2c bellard
    return 0;
388 f1510b2c bellard
}
389 f1510b2c bellard
390 fc01f7e7 bellard
/* size is the word size in byte */
391 5fafdf24 ths
int register_ioport_write(int start, int length, int size,
392 c4b1fcc0 bellard
                          IOPortWriteFunc *func, void *opaque)
393 f1510b2c bellard
{
394 fc01f7e7 bellard
    int i, bsize;
395 f1510b2c bellard
396 c4b1fcc0 bellard
    if (size == 1) {
397 fc01f7e7 bellard
        bsize = 0;
398 c4b1fcc0 bellard
    } else if (size == 2) {
399 fc01f7e7 bellard
        bsize = 1;
400 c4b1fcc0 bellard
    } else if (size == 4) {
401 fc01f7e7 bellard
        bsize = 2;
402 c4b1fcc0 bellard
    } else {
403 88fdf56f balrog
        hw_error("register_ioport_write: invalid size");
404 fc01f7e7 bellard
        return -1;
405 c4b1fcc0 bellard
    }
406 c4b1fcc0 bellard
    for(i = start; i < start + length; i += size) {
407 fc01f7e7 bellard
        ioport_write_table[bsize][i] = func;
408 88fdf56f balrog
        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
409 88fdf56f balrog
            hw_error("register_ioport_write: invalid opaque");
410 c4b1fcc0 bellard
        ioport_opaque[i] = opaque;
411 c4b1fcc0 bellard
    }
412 f1510b2c bellard
    return 0;
413 f1510b2c bellard
}
414 f1510b2c bellard
415 69b91039 bellard
void isa_unassign_ioport(int start, int length)
416 69b91039 bellard
{
417 69b91039 bellard
    int i;
418 69b91039 bellard
419 69b91039 bellard
    for(i = start; i < start + length; i++) {
420 69b91039 bellard
        ioport_read_table[0][i] = default_ioport_readb;
421 69b91039 bellard
        ioport_read_table[1][i] = default_ioport_readw;
422 69b91039 bellard
        ioport_read_table[2][i] = default_ioport_readl;
423 69b91039 bellard
424 69b91039 bellard
        ioport_write_table[0][i] = default_ioport_writeb;
425 69b91039 bellard
        ioport_write_table[1][i] = default_ioport_writew;
426 69b91039 bellard
        ioport_write_table[2][i] = default_ioport_writel;
427 a7607f7e aliguori
428 a7607f7e aliguori
        ioport_opaque[i] = NULL;
429 69b91039 bellard
    }
430 69b91039 bellard
}
431 69b91039 bellard
432 20f32282 bellard
/***********************************************************/
433 20f32282 bellard
434 c45886db bellard
void cpu_outb(CPUState *env, int addr, int val)
435 0824d6fc bellard
{
436 d12d51d5 aliguori
    LOG_IOPORT("outb: %04x %02x\n", addr, val);
437 477e3edf aliguori
    ioport_write(0, addr, val);
438 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
439 89bfc105 bellard
    if (env)
440 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
441 89bfc105 bellard
#endif
442 0824d6fc bellard
}
443 0824d6fc bellard
444 c45886db bellard
void cpu_outw(CPUState *env, int addr, int val)
445 0824d6fc bellard
{
446 d12d51d5 aliguori
    LOG_IOPORT("outw: %04x %04x\n", addr, val);
447 477e3edf aliguori
    ioport_write(1, addr, val);
448 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
449 89bfc105 bellard
    if (env)
450 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
451 89bfc105 bellard
#endif
452 0824d6fc bellard
}
453 0824d6fc bellard
454 c45886db bellard
void cpu_outl(CPUState *env, int addr, int val)
455 0824d6fc bellard
{
456 d12d51d5 aliguori
    LOG_IOPORT("outl: %04x %08x\n", addr, val);
457 477e3edf aliguori
    ioport_write(2, addr, val);
458 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
459 89bfc105 bellard
    if (env)
460 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
461 89bfc105 bellard
#endif
462 0824d6fc bellard
}
463 0824d6fc bellard
464 c45886db bellard
int cpu_inb(CPUState *env, int addr)
465 0824d6fc bellard
{
466 fd872598 bellard
    int val;
467 477e3edf aliguori
    val = ioport_read(0, addr);
468 d12d51d5 aliguori
    LOG_IOPORT("inb : %04x %02x\n", addr, val);
469 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
470 89bfc105 bellard
    if (env)
471 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
472 89bfc105 bellard
#endif
473 fd872598 bellard
    return val;
474 0824d6fc bellard
}
475 0824d6fc bellard
476 c45886db bellard
int cpu_inw(CPUState *env, int addr)
477 0824d6fc bellard
{
478 fd872598 bellard
    int val;
479 477e3edf aliguori
    val = ioport_read(1, addr);
480 d12d51d5 aliguori
    LOG_IOPORT("inw : %04x %04x\n", addr, val);
481 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
482 89bfc105 bellard
    if (env)
483 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
484 89bfc105 bellard
#endif
485 fd872598 bellard
    return val;
486 0824d6fc bellard
}
487 0824d6fc bellard
488 c45886db bellard
int cpu_inl(CPUState *env, int addr)
489 0824d6fc bellard
{
490 fd872598 bellard
    int val;
491 477e3edf aliguori
    val = ioport_read(2, addr);
492 d12d51d5 aliguori
    LOG_IOPORT("inl : %04x %08x\n", addr, val);
493 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
494 89bfc105 bellard
    if (env)
495 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
496 89bfc105 bellard
#endif
497 fd872598 bellard
    return val;
498 0824d6fc bellard
}
499 0824d6fc bellard
500 0824d6fc bellard
/***********************************************************/
501 0824d6fc bellard
void hw_error(const char *fmt, ...)
502 0824d6fc bellard
{
503 0824d6fc bellard
    va_list ap;
504 6a00d601 bellard
    CPUState *env;
505 0824d6fc bellard
506 0824d6fc bellard
    va_start(ap, fmt);
507 0824d6fc bellard
    fprintf(stderr, "qemu: hardware error: ");
508 0824d6fc bellard
    vfprintf(stderr, fmt, ap);
509 0824d6fc bellard
    fprintf(stderr, "\n");
510 6a00d601 bellard
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
511 6a00d601 bellard
        fprintf(stderr, "CPU #%d:\n", env->cpu_index);
512 0824d6fc bellard
#ifdef TARGET_I386
513 6a00d601 bellard
        cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
514 c45886db bellard
#else
515 6a00d601 bellard
        cpu_dump_state(env, stderr, fprintf, 0);
516 0824d6fc bellard
#endif
517 6a00d601 bellard
    }
518 0824d6fc bellard
    va_end(ap);
519 0824d6fc bellard
    abort();
520 0824d6fc bellard
}
521 df751fa8 aliguori
 
522 df751fa8 aliguori
/***************/
523 df751fa8 aliguori
/* ballooning */
524 df751fa8 aliguori
525 df751fa8 aliguori
static QEMUBalloonEvent *qemu_balloon_event;
526 df751fa8 aliguori
void *qemu_balloon_event_opaque;
527 df751fa8 aliguori
528 df751fa8 aliguori
void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
529 df751fa8 aliguori
{
530 df751fa8 aliguori
    qemu_balloon_event = func;
531 df751fa8 aliguori
    qemu_balloon_event_opaque = opaque;
532 df751fa8 aliguori
}
533 df751fa8 aliguori
534 df751fa8 aliguori
void qemu_balloon(ram_addr_t target)
535 df751fa8 aliguori
{
536 df751fa8 aliguori
    if (qemu_balloon_event)
537 df751fa8 aliguori
        qemu_balloon_event(qemu_balloon_event_opaque, target);
538 df751fa8 aliguori
}
539 df751fa8 aliguori
540 df751fa8 aliguori
ram_addr_t qemu_balloon_status(void)
541 df751fa8 aliguori
{
542 df751fa8 aliguori
    if (qemu_balloon_event)
543 df751fa8 aliguori
        return qemu_balloon_event(qemu_balloon_event_opaque, 0);
544 df751fa8 aliguori
    return 0;
545 df751fa8 aliguori
}
546 0824d6fc bellard
547 8a7ddc38 bellard
/***********************************************************/
548 63066f4f bellard
/* keyboard/mouse */
549 63066f4f bellard
550 63066f4f bellard
static QEMUPutKBDEvent *qemu_put_kbd_event;
551 63066f4f bellard
static void *qemu_put_kbd_event_opaque;
552 455204eb ths
static QEMUPutMouseEntry *qemu_put_mouse_event_head;
553 455204eb ths
static QEMUPutMouseEntry *qemu_put_mouse_event_current;
554 63066f4f bellard
555 63066f4f bellard
void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
556 63066f4f bellard
{
557 63066f4f bellard
    qemu_put_kbd_event_opaque = opaque;
558 63066f4f bellard
    qemu_put_kbd_event = func;
559 63066f4f bellard
}
560 63066f4f bellard
561 455204eb ths
QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
562 455204eb ths
                                                void *opaque, int absolute,
563 455204eb ths
                                                const char *name)
564 63066f4f bellard
{
565 455204eb ths
    QEMUPutMouseEntry *s, *cursor;
566 455204eb ths
567 455204eb ths
    s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
568 455204eb ths
569 455204eb ths
    s->qemu_put_mouse_event = func;
570 455204eb ths
    s->qemu_put_mouse_event_opaque = opaque;
571 455204eb ths
    s->qemu_put_mouse_event_absolute = absolute;
572 455204eb ths
    s->qemu_put_mouse_event_name = qemu_strdup(name);
573 455204eb ths
    s->next = NULL;
574 455204eb ths
575 455204eb ths
    if (!qemu_put_mouse_event_head) {
576 455204eb ths
        qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
577 455204eb ths
        return s;
578 455204eb ths
    }
579 455204eb ths
580 455204eb ths
    cursor = qemu_put_mouse_event_head;
581 455204eb ths
    while (cursor->next != NULL)
582 455204eb ths
        cursor = cursor->next;
583 455204eb ths
584 455204eb ths
    cursor->next = s;
585 455204eb ths
    qemu_put_mouse_event_current = s;
586 455204eb ths
587 455204eb ths
    return s;
588 455204eb ths
}
589 455204eb ths
590 455204eb ths
void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
591 455204eb ths
{
592 455204eb ths
    QEMUPutMouseEntry *prev = NULL, *cursor;
593 455204eb ths
594 455204eb ths
    if (!qemu_put_mouse_event_head || entry == NULL)
595 455204eb ths
        return;
596 455204eb ths
597 455204eb ths
    cursor = qemu_put_mouse_event_head;
598 455204eb ths
    while (cursor != NULL && cursor != entry) {
599 455204eb ths
        prev = cursor;
600 455204eb ths
        cursor = cursor->next;
601 455204eb ths
    }
602 455204eb ths
603 455204eb ths
    if (cursor == NULL) // does not exist or list empty
604 455204eb ths
        return;
605 455204eb ths
    else if (prev == NULL) { // entry is head
606 455204eb ths
        qemu_put_mouse_event_head = cursor->next;
607 455204eb ths
        if (qemu_put_mouse_event_current == entry)
608 455204eb ths
            qemu_put_mouse_event_current = cursor->next;
609 455204eb ths
        qemu_free(entry->qemu_put_mouse_event_name);
610 455204eb ths
        qemu_free(entry);
611 455204eb ths
        return;
612 455204eb ths
    }
613 455204eb ths
614 455204eb ths
    prev->next = entry->next;
615 455204eb ths
616 455204eb ths
    if (qemu_put_mouse_event_current == entry)
617 455204eb ths
        qemu_put_mouse_event_current = prev;
618 455204eb ths
619 455204eb ths
    qemu_free(entry->qemu_put_mouse_event_name);
620 455204eb ths
    qemu_free(entry);
621 63066f4f bellard
}
622 63066f4f bellard
623 63066f4f bellard
void kbd_put_keycode(int keycode)
624 63066f4f bellard
{
625 63066f4f bellard
    if (qemu_put_kbd_event) {
626 63066f4f bellard
        qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
627 63066f4f bellard
    }
628 63066f4f bellard
}
629 63066f4f bellard
630 63066f4f bellard
void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
631 63066f4f bellard
{
632 455204eb ths
    QEMUPutMouseEvent *mouse_event;
633 455204eb ths
    void *mouse_event_opaque;
634 a171fe39 balrog
    int width;
635 455204eb ths
636 455204eb ths
    if (!qemu_put_mouse_event_current) {
637 455204eb ths
        return;
638 455204eb ths
    }
639 455204eb ths
640 455204eb ths
    mouse_event =
641 455204eb ths
        qemu_put_mouse_event_current->qemu_put_mouse_event;
642 455204eb ths
    mouse_event_opaque =
643 455204eb ths
        qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
644 455204eb ths
645 455204eb ths
    if (mouse_event) {
646 a171fe39 balrog
        if (graphic_rotate) {
647 a171fe39 balrog
            if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
648 a171fe39 balrog
                width = 0x7fff;
649 a171fe39 balrog
            else
650 b94ed577 aurel32
                width = graphic_width - 1;
651 a171fe39 balrog
            mouse_event(mouse_event_opaque,
652 a171fe39 balrog
                                 width - dy, dx, dz, buttons_state);
653 a171fe39 balrog
        } else
654 a171fe39 balrog
            mouse_event(mouse_event_opaque,
655 a171fe39 balrog
                                 dx, dy, dz, buttons_state);
656 63066f4f bellard
    }
657 63066f4f bellard
}
658 63066f4f bellard
659 09b26c5e bellard
int kbd_mouse_is_absolute(void)
660 09b26c5e bellard
{
661 455204eb ths
    if (!qemu_put_mouse_event_current)
662 455204eb ths
        return 0;
663 455204eb ths
664 455204eb ths
    return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
665 455204eb ths
}
666 455204eb ths
667 376253ec aliguori
void do_info_mice(Monitor *mon)
668 455204eb ths
{
669 455204eb ths
    QEMUPutMouseEntry *cursor;
670 455204eb ths
    int index = 0;
671 455204eb ths
672 455204eb ths
    if (!qemu_put_mouse_event_head) {
673 376253ec aliguori
        monitor_printf(mon, "No mouse devices connected\n");
674 455204eb ths
        return;
675 455204eb ths
    }
676 455204eb ths
677 376253ec aliguori
    monitor_printf(mon, "Mouse devices available:\n");
678 455204eb ths
    cursor = qemu_put_mouse_event_head;
679 455204eb ths
    while (cursor != NULL) {
680 376253ec aliguori
        monitor_printf(mon, "%c Mouse #%d: %s\n",
681 376253ec aliguori
                       (cursor == qemu_put_mouse_event_current ? '*' : ' '),
682 376253ec aliguori
                       index, cursor->qemu_put_mouse_event_name);
683 455204eb ths
        index++;
684 455204eb ths
        cursor = cursor->next;
685 455204eb ths
    }
686 455204eb ths
}
687 455204eb ths
688 376253ec aliguori
void do_mouse_set(Monitor *mon, int index)
689 455204eb ths
{
690 455204eb ths
    QEMUPutMouseEntry *cursor;
691 455204eb ths
    int i = 0;
692 455204eb ths
693 455204eb ths
    if (!qemu_put_mouse_event_head) {
694 376253ec aliguori
        monitor_printf(mon, "No mouse devices connected\n");
695 455204eb ths
        return;
696 455204eb ths
    }
697 455204eb ths
698 455204eb ths
    cursor = qemu_put_mouse_event_head;
699 455204eb ths
    while (cursor != NULL && index != i) {
700 455204eb ths
        i++;
701 455204eb ths
        cursor = cursor->next;
702 455204eb ths
    }
703 455204eb ths
704 455204eb ths
    if (cursor != NULL)
705 455204eb ths
        qemu_put_mouse_event_current = cursor;
706 455204eb ths
    else
707 376253ec aliguori
        monitor_printf(mon, "Mouse at given index not found\n");
708 09b26c5e bellard
}
709 09b26c5e bellard
710 1dce7c3c bellard
/* compute with 96 bit intermediate result: (a*b)/c */
711 1dce7c3c bellard
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
712 0824d6fc bellard
{
713 1dce7c3c bellard
    union {
714 1dce7c3c bellard
        uint64_t ll;
715 1dce7c3c bellard
        struct {
716 1dce7c3c bellard
#ifdef WORDS_BIGENDIAN
717 1dce7c3c bellard
            uint32_t high, low;
718 1dce7c3c bellard
#else
719 1dce7c3c bellard
            uint32_t low, high;
720 3b46e624 ths
#endif
721 1dce7c3c bellard
        } l;
722 1dce7c3c bellard
    } u, res;
723 1dce7c3c bellard
    uint64_t rl, rh;
724 0824d6fc bellard
725 1dce7c3c bellard
    u.ll = a;
726 1dce7c3c bellard
    rl = (uint64_t)u.l.low * (uint64_t)b;
727 1dce7c3c bellard
    rh = (uint64_t)u.l.high * (uint64_t)b;
728 1dce7c3c bellard
    rh += (rl >> 32);
729 1dce7c3c bellard
    res.l.high = rh / c;
730 1dce7c3c bellard
    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
731 1dce7c3c bellard
    return res.ll;
732 34865134 bellard
}
733 34865134 bellard
734 1dce7c3c bellard
/***********************************************************/
735 1dce7c3c bellard
/* real time host monotonic timer */
736 34865134 bellard
737 1dce7c3c bellard
#define QEMU_TIMER_BASE 1000000000LL
738 34865134 bellard
739 1dce7c3c bellard
#ifdef WIN32
740 0824d6fc bellard
741 1dce7c3c bellard
static int64_t clock_freq;
742 1115dde7 bellard
743 1dce7c3c bellard
static void init_get_clock(void)
744 1115dde7 bellard
{
745 a8e5ac33 bellard
    LARGE_INTEGER freq;
746 a8e5ac33 bellard
    int ret;
747 1dce7c3c bellard
    ret = QueryPerformanceFrequency(&freq);
748 1dce7c3c bellard
    if (ret == 0) {
749 1dce7c3c bellard
        fprintf(stderr, "Could not calibrate ticks\n");
750 1dce7c3c bellard
        exit(1);
751 1dce7c3c bellard
    }
752 1dce7c3c bellard
    clock_freq = freq.QuadPart;
753 1115dde7 bellard
}
754 1115dde7 bellard
755 1dce7c3c bellard
static int64_t get_clock(void)
756 b8076a74 bellard
{
757 1dce7c3c bellard
    LARGE_INTEGER ti;
758 1dce7c3c bellard
    QueryPerformanceCounter(&ti);
759 1dce7c3c bellard
    return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
760 b8076a74 bellard
}
761 b8076a74 bellard
762 1dce7c3c bellard
#else
763 90cb9493 bellard
764 1dce7c3c bellard
static int use_rt_clock;
765 1dce7c3c bellard
766 1dce7c3c bellard
static void init_get_clock(void)
767 90cb9493 bellard
{
768 1dce7c3c bellard
    use_rt_clock = 0;
769 c5e97233 blueswir1
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
770 c5e97233 blueswir1
    || defined(__DragonFly__)
771 1dce7c3c bellard
    {
772 1dce7c3c bellard
        struct timespec ts;
773 1dce7c3c bellard
        if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
774 1dce7c3c bellard
            use_rt_clock = 1;
775 1dce7c3c bellard
        }
776 1dce7c3c bellard
    }
777 1dce7c3c bellard
#endif
778 90cb9493 bellard
}
779 90cb9493 bellard
780 1dce7c3c bellard
static int64_t get_clock(void)
781 fdbb4691 bellard
{
782 c5e97233 blueswir1
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
783 c5e97233 blueswir1
        || defined(__DragonFly__)
784 1dce7c3c bellard
    if (use_rt_clock) {
785 1dce7c3c bellard
        struct timespec ts;
786 1dce7c3c bellard
        clock_gettime(CLOCK_MONOTONIC, &ts);
787 1dce7c3c bellard
        return ts.tv_sec * 1000000000LL + ts.tv_nsec;
788 5fafdf24 ths
    } else
789 fdbb4691 bellard
#endif
790 1dce7c3c bellard
    {
791 1dce7c3c bellard
        /* XXX: using gettimeofday leads to problems if the date
792 1dce7c3c bellard
           changes, so it should be avoided. */
793 1dce7c3c bellard
        struct timeval tv;
794 1dce7c3c bellard
        gettimeofday(&tv, NULL);
795 1dce7c3c bellard
        return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
796 1dce7c3c bellard
    }
797 fdbb4691 bellard
}
798 34865134 bellard
#endif
799 34865134 bellard
800 2e70f6ef pbrook
/* Return the virtual CPU time, based on the instruction counter.  */
801 2e70f6ef pbrook
static int64_t cpu_get_icount(void)
802 2e70f6ef pbrook
{
803 2e70f6ef pbrook
    int64_t icount;
804 2e70f6ef pbrook
    CPUState *env = cpu_single_env;;
805 2e70f6ef pbrook
    icount = qemu_icount;
806 2e70f6ef pbrook
    if (env) {
807 2e70f6ef pbrook
        if (!can_do_io(env))
808 2e70f6ef pbrook
            fprintf(stderr, "Bad clock read\n");
809 2e70f6ef pbrook
        icount -= (env->icount_decr.u16.low + env->icount_extra);
810 2e70f6ef pbrook
    }
811 2e70f6ef pbrook
    return qemu_icount_bias + (icount << icount_time_shift);
812 2e70f6ef pbrook
}
813 2e70f6ef pbrook
814 1dce7c3c bellard
/***********************************************************/
815 1dce7c3c bellard
/* guest cycle counter */
816 1dce7c3c bellard
817 eade0f19 bellard
static int64_t cpu_ticks_prev;
818 34865134 bellard
static int64_t cpu_ticks_offset;
819 1dce7c3c bellard
static int64_t cpu_clock_offset;
820 8a7ddc38 bellard
static int cpu_ticks_enabled;
821 34865134 bellard
822 1dce7c3c bellard
/* return the host CPU cycle counter and handle stop/restart */
823 1dce7c3c bellard
int64_t cpu_get_ticks(void)
824 34865134 bellard
{
825 2e70f6ef pbrook
    if (use_icount) {
826 2e70f6ef pbrook
        return cpu_get_icount();
827 2e70f6ef pbrook
    }
828 8a7ddc38 bellard
    if (!cpu_ticks_enabled) {
829 8a7ddc38 bellard
        return cpu_ticks_offset;
830 8a7ddc38 bellard
    } else {
831 eade0f19 bellard
        int64_t ticks;
832 eade0f19 bellard
        ticks = cpu_get_real_ticks();
833 eade0f19 bellard
        if (cpu_ticks_prev > ticks) {
834 eade0f19 bellard
            /* Note: non increasing ticks may happen if the host uses
835 eade0f19 bellard
               software suspend */
836 eade0f19 bellard
            cpu_ticks_offset += cpu_ticks_prev - ticks;
837 eade0f19 bellard
        }
838 eade0f19 bellard
        cpu_ticks_prev = ticks;
839 eade0f19 bellard
        return ticks + cpu_ticks_offset;
840 8a7ddc38 bellard
    }
841 34865134 bellard
}
842 34865134 bellard
843 1dce7c3c bellard
/* return the host CPU monotonic timer and handle stop/restart */
844 1dce7c3c bellard
static int64_t cpu_get_clock(void)
845 1dce7c3c bellard
{
846 1dce7c3c bellard
    int64_t ti;
847 1dce7c3c bellard
    if (!cpu_ticks_enabled) {
848 1dce7c3c bellard
        return cpu_clock_offset;
849 1dce7c3c bellard
    } else {
850 1dce7c3c bellard
        ti = get_clock();
851 1dce7c3c bellard
        return ti + cpu_clock_offset;
852 1dce7c3c bellard
    }
853 1dce7c3c bellard
}
854 1dce7c3c bellard
855 34865134 bellard
/* enable cpu_get_ticks() */
856 34865134 bellard
void cpu_enable_ticks(void)
857 34865134 bellard
{
858 8a7ddc38 bellard
    if (!cpu_ticks_enabled) {
859 8a7ddc38 bellard
        cpu_ticks_offset -= cpu_get_real_ticks();
860 1dce7c3c bellard
        cpu_clock_offset -= get_clock();
861 8a7ddc38 bellard
        cpu_ticks_enabled = 1;
862 8a7ddc38 bellard
    }
863 34865134 bellard
}
864 34865134 bellard
865 34865134 bellard
/* disable cpu_get_ticks() : the clock is stopped. You must not call
866 34865134 bellard
   cpu_get_ticks() after that.  */
867 34865134 bellard
void cpu_disable_ticks(void)
868 34865134 bellard
{
869 8a7ddc38 bellard
    if (cpu_ticks_enabled) {
870 8a7ddc38 bellard
        cpu_ticks_offset = cpu_get_ticks();
871 1dce7c3c bellard
        cpu_clock_offset = cpu_get_clock();
872 8a7ddc38 bellard
        cpu_ticks_enabled = 0;
873 8a7ddc38 bellard
    }
874 34865134 bellard
}
875 34865134 bellard
876 1dce7c3c bellard
/***********************************************************/
877 1dce7c3c bellard
/* timers */
878 5fafdf24 ths
879 8a7ddc38 bellard
#define QEMU_TIMER_REALTIME 0
880 8a7ddc38 bellard
#define QEMU_TIMER_VIRTUAL  1
881 8a7ddc38 bellard
882 8a7ddc38 bellard
struct QEMUClock {
883 8a7ddc38 bellard
    int type;
884 8a7ddc38 bellard
    /* XXX: add frequency */
885 8a7ddc38 bellard
};
886 8a7ddc38 bellard
887 8a7ddc38 bellard
struct QEMUTimer {
888 8a7ddc38 bellard
    QEMUClock *clock;
889 8a7ddc38 bellard
    int64_t expire_time;
890 8a7ddc38 bellard
    QEMUTimerCB *cb;
891 8a7ddc38 bellard
    void *opaque;
892 8a7ddc38 bellard
    struct QEMUTimer *next;
893 8a7ddc38 bellard
};
894 8a7ddc38 bellard
895 c8994013 ths
struct qemu_alarm_timer {
896 c8994013 ths
    char const *name;
897 efe75411 ths
    unsigned int flags;
898 c8994013 ths
899 c8994013 ths
    int (*start)(struct qemu_alarm_timer *t);
900 c8994013 ths
    void (*stop)(struct qemu_alarm_timer *t);
901 efe75411 ths
    void (*rearm)(struct qemu_alarm_timer *t);
902 c8994013 ths
    void *priv;
903 c8994013 ths
};
904 c8994013 ths
905 efe75411 ths
#define ALARM_FLAG_DYNTICKS  0x1
906 d5d08334 balrog
#define ALARM_FLAG_EXPIRED   0x2
907 efe75411 ths
908 efe75411 ths
static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
909 efe75411 ths
{
910 efe75411 ths
    return t->flags & ALARM_FLAG_DYNTICKS;
911 efe75411 ths
}
912 efe75411 ths
913 efe75411 ths
static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
914 efe75411 ths
{
915 efe75411 ths
    if (!alarm_has_dynticks(t))
916 efe75411 ths
        return;
917 efe75411 ths
918 efe75411 ths
    t->rearm(t);
919 efe75411 ths
}
920 efe75411 ths
921 efe75411 ths
/* TODO: MIN_TIMER_REARM_US should be optimized */
922 efe75411 ths
#define MIN_TIMER_REARM_US 250
923 efe75411 ths
924 c8994013 ths
static struct qemu_alarm_timer *alarm_timer;
925 f49e58dc aliguori
#ifndef _WIN32
926 c96f1a48 aliguori
static int alarm_timer_rfd, alarm_timer_wfd;
927 f49e58dc aliguori
#endif
928 8a7ddc38 bellard
929 40c3bac3 bellard
#ifdef _WIN32
930 c8994013 ths
931 c8994013 ths
struct qemu_alarm_win32 {
932 c8994013 ths
    MMRESULT timerId;
933 c8994013 ths
    HANDLE host_alarm;
934 c8994013 ths
    unsigned int period;
935 c8994013 ths
} alarm_win32_data = {0, NULL, -1};
936 c8994013 ths
937 c8994013 ths
static int win32_start_timer(struct qemu_alarm_timer *t);
938 c8994013 ths
static void win32_stop_timer(struct qemu_alarm_timer *t);
939 efe75411 ths
static void win32_rearm_timer(struct qemu_alarm_timer *t);
940 c8994013 ths
941 40c3bac3 bellard
#else
942 c8994013 ths
943 c8994013 ths
static int unix_start_timer(struct qemu_alarm_timer *t);
944 c8994013 ths
static void unix_stop_timer(struct qemu_alarm_timer *t);
945 c8994013 ths
946 231c6586 ths
#ifdef __linux__
947 231c6586 ths
948 efe75411 ths
static int dynticks_start_timer(struct qemu_alarm_timer *t);
949 efe75411 ths
static void dynticks_stop_timer(struct qemu_alarm_timer *t);
950 efe75411 ths
static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
951 efe75411 ths
952 c40ec5a9 ths
static int hpet_start_timer(struct qemu_alarm_timer *t);
953 c40ec5a9 ths
static void hpet_stop_timer(struct qemu_alarm_timer *t);
954 c40ec5a9 ths
955 c8994013 ths
static int rtc_start_timer(struct qemu_alarm_timer *t);
956 c8994013 ths
static void rtc_stop_timer(struct qemu_alarm_timer *t);
957 c8994013 ths
958 efe75411 ths
#endif /* __linux__ */
959 8a7ddc38 bellard
960 c8994013 ths
#endif /* _WIN32 */
961 c8994013 ths
962 2e70f6ef pbrook
/* Correlation between real and virtual time is always going to be
963 bf20dc07 ths
   fairly approximate, so ignore small variation.
964 2e70f6ef pbrook
   When the guest is idle real and virtual time will be aligned in
965 2e70f6ef pbrook
   the IO wait loop.  */
966 2e70f6ef pbrook
#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
967 2e70f6ef pbrook
968 2e70f6ef pbrook
static void icount_adjust(void)
969 2e70f6ef pbrook
{
970 2e70f6ef pbrook
    int64_t cur_time;
971 2e70f6ef pbrook
    int64_t cur_icount;
972 2e70f6ef pbrook
    int64_t delta;
973 2e70f6ef pbrook
    static int64_t last_delta;
974 2e70f6ef pbrook
    /* If the VM is not running, then do nothing.  */
975 2e70f6ef pbrook
    if (!vm_running)
976 2e70f6ef pbrook
        return;
977 2e70f6ef pbrook
978 2e70f6ef pbrook
    cur_time = cpu_get_clock();
979 2e70f6ef pbrook
    cur_icount = qemu_get_clock(vm_clock);
980 2e70f6ef pbrook
    delta = cur_icount - cur_time;
981 2e70f6ef pbrook
    /* FIXME: This is a very crude algorithm, somewhat prone to oscillation.  */
982 2e70f6ef pbrook
    if (delta > 0
983 2e70f6ef pbrook
        && last_delta + ICOUNT_WOBBLE < delta * 2
984 2e70f6ef pbrook
        && icount_time_shift > 0) {
985 2e70f6ef pbrook
        /* The guest is getting too far ahead.  Slow time down.  */
986 2e70f6ef pbrook
        icount_time_shift--;
987 2e70f6ef pbrook
    }
988 2e70f6ef pbrook
    if (delta < 0
989 2e70f6ef pbrook
        && last_delta - ICOUNT_WOBBLE > delta * 2
990 2e70f6ef pbrook
        && icount_time_shift < MAX_ICOUNT_SHIFT) {
991 2e70f6ef pbrook
        /* The guest is getting too far behind.  Speed time up.  */
992 2e70f6ef pbrook
        icount_time_shift++;
993 2e70f6ef pbrook
    }
994 2e70f6ef pbrook
    last_delta = delta;
995 2e70f6ef pbrook
    qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
996 2e70f6ef pbrook
}
997 2e70f6ef pbrook
998 2e70f6ef pbrook
static void icount_adjust_rt(void * opaque)
999 2e70f6ef pbrook
{
1000 2e70f6ef pbrook
    qemu_mod_timer(icount_rt_timer,
1001 2e70f6ef pbrook
                   qemu_get_clock(rt_clock) + 1000);
1002 2e70f6ef pbrook
    icount_adjust();
1003 2e70f6ef pbrook
}
1004 2e70f6ef pbrook
1005 2e70f6ef pbrook
static void icount_adjust_vm(void * opaque)
1006 2e70f6ef pbrook
{
1007 2e70f6ef pbrook
    qemu_mod_timer(icount_vm_timer,
1008 2e70f6ef pbrook
                   qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1009 2e70f6ef pbrook
    icount_adjust();
1010 2e70f6ef pbrook
}
1011 2e70f6ef pbrook
1012 2e70f6ef pbrook
static void init_icount_adjust(void)
1013 2e70f6ef pbrook
{
1014 2e70f6ef pbrook
    /* Have both realtime and virtual time triggers for speed adjustment.
1015 2e70f6ef pbrook
       The realtime trigger catches emulated time passing too slowly,
1016 2e70f6ef pbrook
       the virtual time trigger catches emulated time passing too fast.
1017 2e70f6ef pbrook
       Realtime triggers occur even when idle, so use them less frequently
1018 2e70f6ef pbrook
       than VM triggers.  */
1019 2e70f6ef pbrook
    icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
1020 2e70f6ef pbrook
    qemu_mod_timer(icount_rt_timer,
1021 2e70f6ef pbrook
                   qemu_get_clock(rt_clock) + 1000);
1022 2e70f6ef pbrook
    icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
1023 2e70f6ef pbrook
    qemu_mod_timer(icount_vm_timer,
1024 2e70f6ef pbrook
                   qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1025 2e70f6ef pbrook
}
1026 2e70f6ef pbrook
1027 c8994013 ths
static struct qemu_alarm_timer alarm_timers[] = {
1028 efe75411 ths
#ifndef _WIN32
1029 231c6586 ths
#ifdef __linux__
1030 efe75411 ths
    {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
1031 efe75411 ths
     dynticks_stop_timer, dynticks_rearm_timer, NULL},
1032 c40ec5a9 ths
    /* HPET - if available - is preferred */
1033 efe75411 ths
    {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
1034 c40ec5a9 ths
    /* ...otherwise try RTC */
1035 efe75411 ths
    {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
1036 c8994013 ths
#endif
1037 efe75411 ths
    {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
1038 c8994013 ths
#else
1039 efe75411 ths
    {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1040 efe75411 ths
     win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1041 efe75411 ths
    {"win32", 0, win32_start_timer,
1042 efe75411 ths
     win32_stop_timer, NULL, &alarm_win32_data},
1043 c8994013 ths
#endif
1044 c8994013 ths
    {NULL, }
1045 c8994013 ths
};
1046 c8994013 ths
1047 3f47aa8c blueswir1
static void show_available_alarms(void)
1048 f3dcfada ths
{
1049 f3dcfada ths
    int i;
1050 f3dcfada ths
1051 f3dcfada ths
    printf("Available alarm timers, in order of precedence:\n");
1052 f3dcfada ths
    for (i = 0; alarm_timers[i].name; i++)
1053 f3dcfada ths
        printf("%s\n", alarm_timers[i].name);
1054 f3dcfada ths
}
1055 f3dcfada ths
1056 f3dcfada ths
static void configure_alarms(char const *opt)
1057 f3dcfada ths
{
1058 f3dcfada ths
    int i;
1059 f3dcfada ths
    int cur = 0;
1060 b1503cda malc
    int count = ARRAY_SIZE(alarm_timers) - 1;
1061 f3dcfada ths
    char *arg;
1062 f3dcfada ths
    char *name;
1063 2e70f6ef pbrook
    struct qemu_alarm_timer tmp;
1064 f3dcfada ths
1065 3adda04c aurel32
    if (!strcmp(opt, "?")) {
1066 f3dcfada ths
        show_available_alarms();
1067 f3dcfada ths
        exit(0);
1068 f3dcfada ths
    }
1069 f3dcfada ths
1070 f3dcfada ths
    arg = strdup(opt);
1071 f3dcfada ths
1072 f3dcfada ths
    /* Reorder the array */
1073 f3dcfada ths
    name = strtok(arg, ",");
1074 f3dcfada ths
    while (name) {
1075 e2b577e5 balrog
        for (i = 0; i < count && alarm_timers[i].name; i++) {
1076 f3dcfada ths
            if (!strcmp(alarm_timers[i].name, name))
1077 f3dcfada ths
                break;
1078 f3dcfada ths
        }
1079 f3dcfada ths
1080 f3dcfada ths
        if (i == count) {
1081 f3dcfada ths
            fprintf(stderr, "Unknown clock %s\n", name);
1082 f3dcfada ths
            goto next;
1083 f3dcfada ths
        }
1084 f3dcfada ths
1085 f3dcfada ths
        if (i < cur)
1086 f3dcfada ths
            /* Ignore */
1087 f3dcfada ths
            goto next;
1088 f3dcfada ths
1089 f3dcfada ths
        /* Swap */
1090 f3dcfada ths
        tmp = alarm_timers[i];
1091 f3dcfada ths
        alarm_timers[i] = alarm_timers[cur];
1092 f3dcfada ths
        alarm_timers[cur] = tmp;
1093 f3dcfada ths
1094 f3dcfada ths
        cur++;
1095 f3dcfada ths
next:
1096 f3dcfada ths
        name = strtok(NULL, ",");
1097 f3dcfada ths
    }
1098 f3dcfada ths
1099 f3dcfada ths
    free(arg);
1100 f3dcfada ths
1101 f3dcfada ths
    if (cur) {
1102 2e70f6ef pbrook
        /* Disable remaining timers */
1103 f3dcfada ths
        for (i = cur; i < count; i++)
1104 f3dcfada ths
            alarm_timers[i].name = NULL;
1105 3adda04c aurel32
    } else {
1106 3adda04c aurel32
        show_available_alarms();
1107 3adda04c aurel32
        exit(1);
1108 f3dcfada ths
    }
1109 f3dcfada ths
}
1110 f3dcfada ths
1111 c8994013 ths
QEMUClock *rt_clock;
1112 c8994013 ths
QEMUClock *vm_clock;
1113 c8994013 ths
1114 c8994013 ths
static QEMUTimer *active_timers[2];
1115 c8994013 ths
1116 9596ebb7 pbrook
static QEMUClock *qemu_new_clock(int type)
1117 8a7ddc38 bellard
{
1118 8a7ddc38 bellard
    QEMUClock *clock;
1119 8a7ddc38 bellard
    clock = qemu_mallocz(sizeof(QEMUClock));
1120 8a7ddc38 bellard
    clock->type = type;
1121 8a7ddc38 bellard
    return clock;
1122 8a7ddc38 bellard
}
1123 8a7ddc38 bellard
1124 8a7ddc38 bellard
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1125 8a7ddc38 bellard
{
1126 8a7ddc38 bellard
    QEMUTimer *ts;
1127 8a7ddc38 bellard
1128 8a7ddc38 bellard
    ts = qemu_mallocz(sizeof(QEMUTimer));
1129 8a7ddc38 bellard
    ts->clock = clock;
1130 8a7ddc38 bellard
    ts->cb = cb;
1131 8a7ddc38 bellard
    ts->opaque = opaque;
1132 8a7ddc38 bellard
    return ts;
1133 8a7ddc38 bellard
}
1134 8a7ddc38 bellard
1135 8a7ddc38 bellard
void qemu_free_timer(QEMUTimer *ts)
1136 8a7ddc38 bellard
{
1137 8a7ddc38 bellard
    qemu_free(ts);
1138 8a7ddc38 bellard
}
1139 8a7ddc38 bellard
1140 8a7ddc38 bellard
/* stop a timer, but do not dealloc it */
1141 8a7ddc38 bellard
void qemu_del_timer(QEMUTimer *ts)
1142 8a7ddc38 bellard
{
1143 8a7ddc38 bellard
    QEMUTimer **pt, *t;
1144 8a7ddc38 bellard
1145 8a7ddc38 bellard
    /* NOTE: this code must be signal safe because
1146 8a7ddc38 bellard
       qemu_timer_expired() can be called from a signal. */
1147 8a7ddc38 bellard
    pt = &active_timers[ts->clock->type];
1148 8a7ddc38 bellard
    for(;;) {
1149 8a7ddc38 bellard
        t = *pt;
1150 8a7ddc38 bellard
        if (!t)
1151 8a7ddc38 bellard
            break;
1152 8a7ddc38 bellard
        if (t == ts) {
1153 8a7ddc38 bellard
            *pt = t->next;
1154 8a7ddc38 bellard
            break;
1155 8a7ddc38 bellard
        }
1156 8a7ddc38 bellard
        pt = &t->next;
1157 8a7ddc38 bellard
    }
1158 8a7ddc38 bellard
}
1159 8a7ddc38 bellard
1160 8a7ddc38 bellard
/* modify the current timer so that it will be fired when current_time
1161 8a7ddc38 bellard
   >= expire_time. The corresponding callback will be called. */
1162 8a7ddc38 bellard
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1163 8a7ddc38 bellard
{
1164 8a7ddc38 bellard
    QEMUTimer **pt, *t;
1165 8a7ddc38 bellard
1166 8a7ddc38 bellard
    qemu_del_timer(ts);
1167 8a7ddc38 bellard
1168 8a7ddc38 bellard
    /* add the timer in the sorted list */
1169 8a7ddc38 bellard
    /* NOTE: this code must be signal safe because
1170 8a7ddc38 bellard
       qemu_timer_expired() can be called from a signal. */
1171 8a7ddc38 bellard
    pt = &active_timers[ts->clock->type];
1172 8a7ddc38 bellard
    for(;;) {
1173 8a7ddc38 bellard
        t = *pt;
1174 8a7ddc38 bellard
        if (!t)
1175 8a7ddc38 bellard
            break;
1176 5fafdf24 ths
        if (t->expire_time > expire_time)
1177 8a7ddc38 bellard
            break;
1178 8a7ddc38 bellard
        pt = &t->next;
1179 8a7ddc38 bellard
    }
1180 8a7ddc38 bellard
    ts->expire_time = expire_time;
1181 8a7ddc38 bellard
    ts->next = *pt;
1182 8a7ddc38 bellard
    *pt = ts;
1183 d5d08334 balrog
1184 d5d08334 balrog
    /* Rearm if necessary  */
1185 2e70f6ef pbrook
    if (pt == &active_timers[ts->clock->type]) {
1186 2e70f6ef pbrook
        if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1187 2e70f6ef pbrook
            qemu_rearm_alarm_timer(alarm_timer);
1188 2e70f6ef pbrook
        }
1189 2e70f6ef pbrook
        /* Interrupt execution to force deadline recalculation.  */
1190 2e70f6ef pbrook
        if (use_icount && cpu_single_env) {
1191 3098dba0 aurel32
            cpu_exit(cpu_single_env);
1192 2e70f6ef pbrook
        }
1193 2e70f6ef pbrook
    }
1194 8a7ddc38 bellard
}
1195 8a7ddc38 bellard
1196 8a7ddc38 bellard
int qemu_timer_pending(QEMUTimer *ts)
1197 8a7ddc38 bellard
{
1198 8a7ddc38 bellard
    QEMUTimer *t;
1199 8a7ddc38 bellard
    for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1200 8a7ddc38 bellard
        if (t == ts)
1201 8a7ddc38 bellard
            return 1;
1202 8a7ddc38 bellard
    }
1203 8a7ddc38 bellard
    return 0;
1204 8a7ddc38 bellard
}
1205 8a7ddc38 bellard
1206 8a7ddc38 bellard
static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1207 8a7ddc38 bellard
{
1208 8a7ddc38 bellard
    if (!timer_head)
1209 8a7ddc38 bellard
        return 0;
1210 8a7ddc38 bellard
    return (timer_head->expire_time <= current_time);
1211 8a7ddc38 bellard
}
1212 8a7ddc38 bellard
1213 8a7ddc38 bellard
static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1214 8a7ddc38 bellard
{
1215 8a7ddc38 bellard
    QEMUTimer *ts;
1216 3b46e624 ths
1217 8a7ddc38 bellard
    for(;;) {
1218 8a7ddc38 bellard
        ts = *ptimer_head;
1219 e95c8d51 bellard
        if (!ts || ts->expire_time > current_time)
1220 8a7ddc38 bellard
            break;
1221 8a7ddc38 bellard
        /* remove timer from the list before calling the callback */
1222 8a7ddc38 bellard
        *ptimer_head = ts->next;
1223 8a7ddc38 bellard
        ts->next = NULL;
1224 3b46e624 ths
1225 8a7ddc38 bellard
        /* run the callback (the timer list can be modified) */
1226 8a7ddc38 bellard
        ts->cb(ts->opaque);
1227 8a7ddc38 bellard
    }
1228 8a7ddc38 bellard
}
1229 8a7ddc38 bellard
1230 8a7ddc38 bellard
int64_t qemu_get_clock(QEMUClock *clock)
1231 8a7ddc38 bellard
{
1232 8a7ddc38 bellard
    switch(clock->type) {
1233 8a7ddc38 bellard
    case QEMU_TIMER_REALTIME:
1234 1dce7c3c bellard
        return get_clock() / 1000000;
1235 8a7ddc38 bellard
    default:
1236 8a7ddc38 bellard
    case QEMU_TIMER_VIRTUAL:
1237 2e70f6ef pbrook
        if (use_icount) {
1238 2e70f6ef pbrook
            return cpu_get_icount();
1239 2e70f6ef pbrook
        } else {
1240 2e70f6ef pbrook
            return cpu_get_clock();
1241 2e70f6ef pbrook
        }
1242 8a7ddc38 bellard
    }
1243 8a7ddc38 bellard
}
1244 8a7ddc38 bellard
1245 1dce7c3c bellard
static void init_timers(void)
1246 1dce7c3c bellard
{
1247 1dce7c3c bellard
    init_get_clock();
1248 1dce7c3c bellard
    ticks_per_sec = QEMU_TIMER_BASE;
1249 1dce7c3c bellard
    rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1250 1dce7c3c bellard
    vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1251 1dce7c3c bellard
}
1252 1dce7c3c bellard
1253 8a7ddc38 bellard
/* save a timer */
1254 8a7ddc38 bellard
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1255 8a7ddc38 bellard
{
1256 8a7ddc38 bellard
    uint64_t expire_time;
1257 8a7ddc38 bellard
1258 8a7ddc38 bellard
    if (qemu_timer_pending(ts)) {
1259 8a7ddc38 bellard
        expire_time = ts->expire_time;
1260 8a7ddc38 bellard
    } else {
1261 8a7ddc38 bellard
        expire_time = -1;
1262 8a7ddc38 bellard
    }
1263 8a7ddc38 bellard
    qemu_put_be64(f, expire_time);
1264 8a7ddc38 bellard
}
1265 8a7ddc38 bellard
1266 8a7ddc38 bellard
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1267 8a7ddc38 bellard
{
1268 8a7ddc38 bellard
    uint64_t expire_time;
1269 8a7ddc38 bellard
1270 8a7ddc38 bellard
    expire_time = qemu_get_be64(f);
1271 8a7ddc38 bellard
    if (expire_time != -1) {
1272 8a7ddc38 bellard
        qemu_mod_timer(ts, expire_time);
1273 8a7ddc38 bellard
    } else {
1274 8a7ddc38 bellard
        qemu_del_timer(ts);
1275 8a7ddc38 bellard
    }
1276 8a7ddc38 bellard
}
1277 8a7ddc38 bellard
1278 8a7ddc38 bellard
static void timer_save(QEMUFile *f, void *opaque)
1279 8a7ddc38 bellard
{
1280 8a7ddc38 bellard
    if (cpu_ticks_enabled) {
1281 8a7ddc38 bellard
        hw_error("cannot save state if virtual timers are running");
1282 8a7ddc38 bellard
    }
1283 bee8d684 ths
    qemu_put_be64(f, cpu_ticks_offset);
1284 bee8d684 ths
    qemu_put_be64(f, ticks_per_sec);
1285 bee8d684 ths
    qemu_put_be64(f, cpu_clock_offset);
1286 8a7ddc38 bellard
}
1287 8a7ddc38 bellard
1288 8a7ddc38 bellard
static int timer_load(QEMUFile *f, void *opaque, int version_id)
1289 8a7ddc38 bellard
{
1290 c88676f8 bellard
    if (version_id != 1 && version_id != 2)
1291 8a7ddc38 bellard
        return -EINVAL;
1292 8a7ddc38 bellard
    if (cpu_ticks_enabled) {
1293 8a7ddc38 bellard
        return -EINVAL;
1294 8a7ddc38 bellard
    }
1295 bee8d684 ths
    cpu_ticks_offset=qemu_get_be64(f);
1296 bee8d684 ths
    ticks_per_sec=qemu_get_be64(f);
1297 c88676f8 bellard
    if (version_id == 2) {
1298 bee8d684 ths
        cpu_clock_offset=qemu_get_be64(f);
1299 c88676f8 bellard
    }
1300 8a7ddc38 bellard
    return 0;
1301 8a7ddc38 bellard
}
1302 8a7ddc38 bellard
1303 67b915a5 bellard
#ifdef _WIN32
1304 b9e82a59 blueswir1
static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1305 b9e82a59 blueswir1
                                        DWORD_PTR dwUser, DWORD_PTR dw1,
1306 b9e82a59 blueswir1
                                        DWORD_PTR dw2)
1307 67b915a5 bellard
#else
1308 8a7ddc38 bellard
static void host_alarm_handler(int host_signum)
1309 67b915a5 bellard
#endif
1310 8a7ddc38 bellard
{
1311 02ba45c5 bellard
#if 0
1312 02ba45c5 bellard
#define DISP_FREQ 1000
1313 02ba45c5 bellard
    {
1314 02ba45c5 bellard
        static int64_t delta_min = INT64_MAX;
1315 02ba45c5 bellard
        static int64_t delta_max, delta_cum, last_clock, delta, ti;
1316 02ba45c5 bellard
        static int count;
1317 02ba45c5 bellard
        ti = qemu_get_clock(vm_clock);
1318 02ba45c5 bellard
        if (last_clock != 0) {
1319 02ba45c5 bellard
            delta = ti - last_clock;
1320 02ba45c5 bellard
            if (delta < delta_min)
1321 02ba45c5 bellard
                delta_min = delta;
1322 02ba45c5 bellard
            if (delta > delta_max)
1323 02ba45c5 bellard
                delta_max = delta;
1324 02ba45c5 bellard
            delta_cum += delta;
1325 02ba45c5 bellard
            if (++count == DISP_FREQ) {
1326 26a76461 bellard
                printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
1327 02ba45c5 bellard
                       muldiv64(delta_min, 1000000, ticks_per_sec),
1328 02ba45c5 bellard
                       muldiv64(delta_max, 1000000, ticks_per_sec),
1329 02ba45c5 bellard
                       muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1330 02ba45c5 bellard
                       (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1331 02ba45c5 bellard
                count = 0;
1332 02ba45c5 bellard
                delta_min = INT64_MAX;
1333 02ba45c5 bellard
                delta_max = 0;
1334 02ba45c5 bellard
                delta_cum = 0;
1335 02ba45c5 bellard
            }
1336 02ba45c5 bellard
        }
1337 02ba45c5 bellard
        last_clock = ti;
1338 02ba45c5 bellard
    }
1339 02ba45c5 bellard
#endif
1340 efe75411 ths
    if (alarm_has_dynticks(alarm_timer) ||
1341 2e70f6ef pbrook
        (!use_icount &&
1342 2e70f6ef pbrook
            qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1343 2e70f6ef pbrook
                               qemu_get_clock(vm_clock))) ||
1344 8a7ddc38 bellard
        qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1345 8a7ddc38 bellard
                           qemu_get_clock(rt_clock))) {
1346 c96f1a48 aliguori
        CPUState *env = next_cpu;
1347 c96f1a48 aliguori
1348 06d9f2f7 bellard
#ifdef _WIN32
1349 c8994013 ths
        struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1350 c8994013 ths
        SetEvent(data->host_alarm);
1351 f49e58dc aliguori
#else
1352 f49e58dc aliguori
        static const char byte = 0;
1353 c96f1a48 aliguori
        write(alarm_timer_wfd, &byte, sizeof(byte));
1354 f49e58dc aliguori
#endif
1355 d5d08334 balrog
        alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1356 d5d08334 balrog
1357 4f8eb8da balrog
        if (env) {
1358 4f8eb8da balrog
            /* stop the currently executing cpu because a timer occured */
1359 3098dba0 aurel32
            cpu_exit(env);
1360 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
1361 4f8eb8da balrog
            if (env->kqemu_enabled) {
1362 4f8eb8da balrog
                kqemu_cpu_interrupt(env);
1363 4f8eb8da balrog
            }
1364 ee5605e5 balrog
#endif
1365 4f8eb8da balrog
        }
1366 ee5605e5 balrog
        event_pending = 1;
1367 8a7ddc38 bellard
    }
1368 8a7ddc38 bellard
}
1369 8a7ddc38 bellard
1370 2e70f6ef pbrook
static int64_t qemu_next_deadline(void)
1371 efe75411 ths
{
1372 2e70f6ef pbrook
    int64_t delta;
1373 efe75411 ths
1374 efe75411 ths
    if (active_timers[QEMU_TIMER_VIRTUAL]) {
1375 2e70f6ef pbrook
        delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1376 2e70f6ef pbrook
                     qemu_get_clock(vm_clock);
1377 2e70f6ef pbrook
    } else {
1378 2e70f6ef pbrook
        /* To avoid problems with overflow limit this to 2^32.  */
1379 2e70f6ef pbrook
        delta = INT32_MAX;
1380 efe75411 ths
    }
1381 efe75411 ths
1382 2e70f6ef pbrook
    if (delta < 0)
1383 2e70f6ef pbrook
        delta = 0;
1384 efe75411 ths
1385 2e70f6ef pbrook
    return delta;
1386 2e70f6ef pbrook
}
1387 2e70f6ef pbrook
1388 8632fb9a blueswir1
#if defined(__linux__) || defined(_WIN32)
1389 2e70f6ef pbrook
static uint64_t qemu_next_deadline_dyntick(void)
1390 2e70f6ef pbrook
{
1391 2e70f6ef pbrook
    int64_t delta;
1392 2e70f6ef pbrook
    int64_t rtdelta;
1393 2e70f6ef pbrook
1394 2e70f6ef pbrook
    if (use_icount)
1395 2e70f6ef pbrook
        delta = INT32_MAX;
1396 2e70f6ef pbrook
    else
1397 2e70f6ef pbrook
        delta = (qemu_next_deadline() + 999) / 1000;
1398 2e70f6ef pbrook
1399 2e70f6ef pbrook
    if (active_timers[QEMU_TIMER_REALTIME]) {
1400 2e70f6ef pbrook
        rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1401 2e70f6ef pbrook
                 qemu_get_clock(rt_clock))*1000;
1402 2e70f6ef pbrook
        if (rtdelta < delta)
1403 2e70f6ef pbrook
            delta = rtdelta;
1404 2e70f6ef pbrook
    }
1405 2e70f6ef pbrook
1406 2e70f6ef pbrook
    if (delta < MIN_TIMER_REARM_US)
1407 2e70f6ef pbrook
        delta = MIN_TIMER_REARM_US;
1408 2e70f6ef pbrook
1409 2e70f6ef pbrook
    return delta;
1410 efe75411 ths
}
1411 8632fb9a blueswir1
#endif
1412 efe75411 ths
1413 fd872598 bellard
#ifndef _WIN32
1414 fd872598 bellard
1415 7183b4b4 aliguori
/* Sets a specific flag */
1416 7183b4b4 aliguori
static int fcntl_setfl(int fd, int flag)
1417 7183b4b4 aliguori
{
1418 7183b4b4 aliguori
    int flags;
1419 7183b4b4 aliguori
1420 7183b4b4 aliguori
    flags = fcntl(fd, F_GETFL);
1421 7183b4b4 aliguori
    if (flags == -1)
1422 7183b4b4 aliguori
        return -errno;
1423 7183b4b4 aliguori
1424 7183b4b4 aliguori
    if (fcntl(fd, F_SETFL, flags | flag) == -1)
1425 7183b4b4 aliguori
        return -errno;
1426 7183b4b4 aliguori
1427 7183b4b4 aliguori
    return 0;
1428 7183b4b4 aliguori
}
1429 7183b4b4 aliguori
1430 829309c7 bellard
#if defined(__linux__)
1431 829309c7 bellard
1432 fd872598 bellard
#define RTC_FREQ 1024
1433 fd872598 bellard
1434 de9a95f0 aurel32
static void enable_sigio_timer(int fd)
1435 c8994013 ths
{
1436 c8994013 ths
    struct sigaction act;
1437 c8994013 ths
1438 c8994013 ths
    /* timer signal */
1439 c8994013 ths
    sigfillset(&act.sa_mask);
1440 c8994013 ths
    act.sa_flags = 0;
1441 c8994013 ths
    act.sa_handler = host_alarm_handler;
1442 c8994013 ths
1443 c8994013 ths
    sigaction(SIGIO, &act, NULL);
1444 7183b4b4 aliguori
    fcntl_setfl(fd, O_ASYNC);
1445 c8994013 ths
    fcntl(fd, F_SETOWN, getpid());
1446 c8994013 ths
}
1447 829309c7 bellard
1448 c40ec5a9 ths
static int hpet_start_timer(struct qemu_alarm_timer *t)
1449 c40ec5a9 ths
{
1450 c40ec5a9 ths
    struct hpet_info info;
1451 c40ec5a9 ths
    int r, fd;
1452 c40ec5a9 ths
1453 c40ec5a9 ths
    fd = open("/dev/hpet", O_RDONLY);
1454 c40ec5a9 ths
    if (fd < 0)
1455 c40ec5a9 ths
        return -1;
1456 c40ec5a9 ths
1457 c40ec5a9 ths
    /* Set frequency */
1458 c40ec5a9 ths
    r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1459 c40ec5a9 ths
    if (r < 0) {
1460 c40ec5a9 ths
        fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1461 c40ec5a9 ths
                "error, but for better emulation accuracy type:\n"
1462 c40ec5a9 ths
                "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1463 c40ec5a9 ths
        goto fail;
1464 c40ec5a9 ths
    }
1465 c40ec5a9 ths
1466 c40ec5a9 ths
    /* Check capabilities */
1467 c40ec5a9 ths
    r = ioctl(fd, HPET_INFO, &info);
1468 c40ec5a9 ths
    if (r < 0)
1469 c40ec5a9 ths
        goto fail;
1470 c40ec5a9 ths
1471 c40ec5a9 ths
    /* Enable periodic mode */
1472 c40ec5a9 ths
    r = ioctl(fd, HPET_EPI, 0);
1473 c40ec5a9 ths
    if (info.hi_flags && (r < 0))
1474 c40ec5a9 ths
        goto fail;
1475 c40ec5a9 ths
1476 c40ec5a9 ths
    /* Enable interrupt */
1477 c40ec5a9 ths
    r = ioctl(fd, HPET_IE_ON, 0);
1478 c40ec5a9 ths
    if (r < 0)
1479 c40ec5a9 ths
        goto fail;
1480 c40ec5a9 ths
1481 c40ec5a9 ths
    enable_sigio_timer(fd);
1482 fcdc2129 pbrook
    t->priv = (void *)(long)fd;
1483 c40ec5a9 ths
1484 c40ec5a9 ths
    return 0;
1485 c40ec5a9 ths
fail:
1486 c40ec5a9 ths
    close(fd);
1487 c40ec5a9 ths
    return -1;
1488 c40ec5a9 ths
}
1489 c40ec5a9 ths
1490 c40ec5a9 ths
static void hpet_stop_timer(struct qemu_alarm_timer *t)
1491 c40ec5a9 ths
{
1492 fcdc2129 pbrook
    int fd = (long)t->priv;
1493 c40ec5a9 ths
1494 c40ec5a9 ths
    close(fd);
1495 c40ec5a9 ths
}
1496 c40ec5a9 ths
1497 c8994013 ths
static int rtc_start_timer(struct qemu_alarm_timer *t)
1498 fd872598 bellard
{
1499 c8994013 ths
    int rtc_fd;
1500 b5a23ad4 balrog
    unsigned long current_rtc_freq = 0;
1501 c8994013 ths
1502 aeb30be6 balrog
    TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
1503 fd872598 bellard
    if (rtc_fd < 0)
1504 fd872598 bellard
        return -1;
1505 b5a23ad4 balrog
    ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1506 b5a23ad4 balrog
    if (current_rtc_freq != RTC_FREQ &&
1507 b5a23ad4 balrog
        ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
1508 fd872598 bellard
        fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1509 fd872598 bellard
                "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1510 fd872598 bellard
                "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1511 fd872598 bellard
        goto fail;
1512 fd872598 bellard
    }
1513 fd872598 bellard
    if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1514 fd872598 bellard
    fail:
1515 fd872598 bellard
        close(rtc_fd);
1516 fd872598 bellard
        return -1;
1517 fd872598 bellard
    }
1518 c8994013 ths
1519 c8994013 ths
    enable_sigio_timer(rtc_fd);
1520 c8994013 ths
1521 fcdc2129 pbrook
    t->priv = (void *)(long)rtc_fd;
1522 c8994013 ths
1523 fd872598 bellard
    return 0;
1524 fd872598 bellard
}
1525 fd872598 bellard
1526 c8994013 ths
static void rtc_stop_timer(struct qemu_alarm_timer *t)
1527 829309c7 bellard
{
1528 fcdc2129 pbrook
    int rtc_fd = (long)t->priv;
1529 c8994013 ths
1530 c8994013 ths
    close(rtc_fd);
1531 829309c7 bellard
}
1532 829309c7 bellard
1533 efe75411 ths
static int dynticks_start_timer(struct qemu_alarm_timer *t)
1534 efe75411 ths
{
1535 efe75411 ths
    struct sigevent ev;
1536 efe75411 ths
    timer_t host_timer;
1537 efe75411 ths
    struct sigaction act;
1538 efe75411 ths
1539 efe75411 ths
    sigfillset(&act.sa_mask);
1540 efe75411 ths
    act.sa_flags = 0;
1541 efe75411 ths
    act.sa_handler = host_alarm_handler;
1542 efe75411 ths
1543 efe75411 ths
    sigaction(SIGALRM, &act, NULL);
1544 efe75411 ths
1545 efe75411 ths
    ev.sigev_value.sival_int = 0;
1546 efe75411 ths
    ev.sigev_notify = SIGEV_SIGNAL;
1547 efe75411 ths
    ev.sigev_signo = SIGALRM;
1548 efe75411 ths
1549 efe75411 ths
    if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1550 efe75411 ths
        perror("timer_create");
1551 efe75411 ths
1552 efe75411 ths
        /* disable dynticks */
1553 efe75411 ths
        fprintf(stderr, "Dynamic Ticks disabled\n");
1554 efe75411 ths
1555 efe75411 ths
        return -1;
1556 efe75411 ths
    }
1557 efe75411 ths
1558 0399bfe0 blueswir1
    t->priv = (void *)(long)host_timer;
1559 efe75411 ths
1560 efe75411 ths
    return 0;
1561 efe75411 ths
}
1562 efe75411 ths
1563 efe75411 ths
static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1564 efe75411 ths
{
1565 0399bfe0 blueswir1
    timer_t host_timer = (timer_t)(long)t->priv;
1566 efe75411 ths
1567 efe75411 ths
    timer_delete(host_timer);
1568 efe75411 ths
}
1569 efe75411 ths
1570 efe75411 ths
static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1571 efe75411 ths
{
1572 0399bfe0 blueswir1
    timer_t host_timer = (timer_t)(long)t->priv;
1573 efe75411 ths
    struct itimerspec timeout;
1574 efe75411 ths
    int64_t nearest_delta_us = INT64_MAX;
1575 efe75411 ths
    int64_t current_us;
1576 efe75411 ths
1577 efe75411 ths
    if (!active_timers[QEMU_TIMER_REALTIME] &&
1578 efe75411 ths
                !active_timers[QEMU_TIMER_VIRTUAL])
1579 d5d08334 balrog
        return;
1580 efe75411 ths
1581 2e70f6ef pbrook
    nearest_delta_us = qemu_next_deadline_dyntick();
1582 efe75411 ths
1583 efe75411 ths
    /* check whether a timer is already running */
1584 efe75411 ths
    if (timer_gettime(host_timer, &timeout)) {
1585 efe75411 ths
        perror("gettime");
1586 efe75411 ths
        fprintf(stderr, "Internal timer error: aborting\n");
1587 efe75411 ths
        exit(1);
1588 efe75411 ths
    }
1589 efe75411 ths
    current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1590 efe75411 ths
    if (current_us && current_us <= nearest_delta_us)
1591 efe75411 ths
        return;
1592 efe75411 ths
1593 efe75411 ths
    timeout.it_interval.tv_sec = 0;
1594 efe75411 ths
    timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1595 efe75411 ths
    timeout.it_value.tv_sec =  nearest_delta_us / 1000000;
1596 efe75411 ths
    timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1597 efe75411 ths
    if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1598 efe75411 ths
        perror("settime");
1599 efe75411 ths
        fprintf(stderr, "Internal timer error: aborting\n");
1600 efe75411 ths
        exit(1);
1601 efe75411 ths
    }
1602 efe75411 ths
}
1603 efe75411 ths
1604 70744b3a ths
#endif /* defined(__linux__) */
1605 231c6586 ths
1606 c8994013 ths
static int unix_start_timer(struct qemu_alarm_timer *t)
1607 c8994013 ths
{
1608 c8994013 ths
    struct sigaction act;
1609 c8994013 ths
    struct itimerval itv;
1610 c8994013 ths
    int err;
1611 c8994013 ths
1612 c8994013 ths
    /* timer signal */
1613 c8994013 ths
    sigfillset(&act.sa_mask);
1614 c8994013 ths
    act.sa_flags = 0;
1615 c8994013 ths
    act.sa_handler = host_alarm_handler;
1616 c8994013 ths
1617 c8994013 ths
    sigaction(SIGALRM, &act, NULL);
1618 c8994013 ths
1619 c8994013 ths
    itv.it_interval.tv_sec = 0;
1620 c8994013 ths
    /* for i386 kernel 2.6 to get 1 ms */
1621 c8994013 ths
    itv.it_interval.tv_usec = 999;
1622 c8994013 ths
    itv.it_value.tv_sec = 0;
1623 c8994013 ths
    itv.it_value.tv_usec = 10 * 1000;
1624 c8994013 ths
1625 c8994013 ths
    err = setitimer(ITIMER_REAL, &itv, NULL);
1626 c8994013 ths
    if (err)
1627 c8994013 ths
        return -1;
1628 c8994013 ths
1629 c8994013 ths
    return 0;
1630 c8994013 ths
}
1631 c8994013 ths
1632 c8994013 ths
static void unix_stop_timer(struct qemu_alarm_timer *t)
1633 c8994013 ths
{
1634 c8994013 ths
    struct itimerval itv;
1635 c8994013 ths
1636 c8994013 ths
    memset(&itv, 0, sizeof(itv));
1637 c8994013 ths
    setitimer(ITIMER_REAL, &itv, NULL);
1638 c8994013 ths
}
1639 c8994013 ths
1640 829309c7 bellard
#endif /* !defined(_WIN32) */
1641 fd872598 bellard
1642 f49e58dc aliguori
static void try_to_rearm_timer(void *opaque)
1643 f49e58dc aliguori
{
1644 f49e58dc aliguori
    struct qemu_alarm_timer *t = opaque;
1645 f49e58dc aliguori
#ifndef _WIN32
1646 f49e58dc aliguori
    ssize_t len;
1647 f49e58dc aliguori
1648 f49e58dc aliguori
    /* Drain the notify pipe */
1649 f49e58dc aliguori
    do {
1650 f49e58dc aliguori
        char buffer[512];
1651 f49e58dc aliguori
        len = read(alarm_timer_rfd, buffer, sizeof(buffer));
1652 f49e58dc aliguori
    } while ((len == -1 && errno == EINTR) || len > 0);
1653 f49e58dc aliguori
#endif
1654 f49e58dc aliguori
1655 f49e58dc aliguori
    if (t->flags & ALARM_FLAG_EXPIRED) {
1656 f49e58dc aliguori
        alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
1657 f49e58dc aliguori
        qemu_rearm_alarm_timer(alarm_timer);
1658 f49e58dc aliguori
    }
1659 f49e58dc aliguori
}
1660 f49e58dc aliguori
1661 c8994013 ths
#ifdef _WIN32
1662 c8994013 ths
1663 c8994013 ths
static int win32_start_timer(struct qemu_alarm_timer *t)
1664 c8994013 ths
{
1665 c8994013 ths
    TIMECAPS tc;
1666 c8994013 ths
    struct qemu_alarm_win32 *data = t->priv;
1667 efe75411 ths
    UINT flags;
1668 c8994013 ths
1669 c8994013 ths
    data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1670 c8994013 ths
    if (!data->host_alarm) {
1671 c8994013 ths
        perror("Failed CreateEvent");
1672 c396a7f0 ths
        return -1;
1673 c8994013 ths
    }
1674 c8994013 ths
1675 c8994013 ths
    memset(&tc, 0, sizeof(tc));
1676 c8994013 ths
    timeGetDevCaps(&tc, sizeof(tc));
1677 c8994013 ths
1678 c8994013 ths
    if (data->period < tc.wPeriodMin)
1679 c8994013 ths
        data->period = tc.wPeriodMin;
1680 c8994013 ths
1681 c8994013 ths
    timeBeginPeriod(data->period);
1682 c8994013 ths
1683 efe75411 ths
    flags = TIME_CALLBACK_FUNCTION;
1684 efe75411 ths
    if (alarm_has_dynticks(t))
1685 efe75411 ths
        flags |= TIME_ONESHOT;
1686 efe75411 ths
    else
1687 efe75411 ths
        flags |= TIME_PERIODIC;
1688 efe75411 ths
1689 c8994013 ths
    data->timerId = timeSetEvent(1,         // interval (ms)
1690 c8994013 ths
                        data->period,       // resolution
1691 c8994013 ths
                        host_alarm_handler, // function
1692 c8994013 ths
                        (DWORD)t,           // parameter
1693 efe75411 ths
                        flags);
1694 c8994013 ths
1695 c8994013 ths
    if (!data->timerId) {
1696 c8994013 ths
        perror("Failed to initialize win32 alarm timer");
1697 c8994013 ths
1698 c8994013 ths
        timeEndPeriod(data->period);
1699 c8994013 ths
        CloseHandle(data->host_alarm);
1700 c8994013 ths
        return -1;
1701 c8994013 ths
    }
1702 c8994013 ths
1703 f49e58dc aliguori
    qemu_add_wait_object(data->host_alarm, try_to_rearm_timer, t);
1704 c8994013 ths
1705 c8994013 ths
    return 0;
1706 c8994013 ths
}
1707 c8994013 ths
1708 c8994013 ths
static void win32_stop_timer(struct qemu_alarm_timer *t)
1709 c8994013 ths
{
1710 c8994013 ths
    struct qemu_alarm_win32 *data = t->priv;
1711 c8994013 ths
1712 c8994013 ths
    timeKillEvent(data->timerId);
1713 c8994013 ths
    timeEndPeriod(data->period);
1714 c8994013 ths
1715 c8994013 ths
    CloseHandle(data->host_alarm);
1716 c8994013 ths
}
1717 c8994013 ths
1718 efe75411 ths
static void win32_rearm_timer(struct qemu_alarm_timer *t)
1719 efe75411 ths
{
1720 efe75411 ths
    struct qemu_alarm_win32 *data = t->priv;
1721 efe75411 ths
    uint64_t nearest_delta_us;
1722 efe75411 ths
1723 efe75411 ths
    if (!active_timers[QEMU_TIMER_REALTIME] &&
1724 efe75411 ths
                !active_timers[QEMU_TIMER_VIRTUAL])
1725 d5d08334 balrog
        return;
1726 efe75411 ths
1727 2e70f6ef pbrook
    nearest_delta_us = qemu_next_deadline_dyntick();
1728 efe75411 ths
    nearest_delta_us /= 1000;
1729 efe75411 ths
1730 efe75411 ths
    timeKillEvent(data->timerId);
1731 efe75411 ths
1732 efe75411 ths
    data->timerId = timeSetEvent(1,
1733 efe75411 ths
                        data->period,
1734 efe75411 ths
                        host_alarm_handler,
1735 efe75411 ths
                        (DWORD)t,
1736 efe75411 ths
                        TIME_ONESHOT | TIME_PERIODIC);
1737 efe75411 ths
1738 efe75411 ths
    if (!data->timerId) {
1739 efe75411 ths
        perror("Failed to re-arm win32 alarm timer");
1740 efe75411 ths
1741 efe75411 ths
        timeEndPeriod(data->period);
1742 efe75411 ths
        CloseHandle(data->host_alarm);
1743 efe75411 ths
        exit(1);
1744 efe75411 ths
    }
1745 efe75411 ths
}
1746 efe75411 ths
1747 c8994013 ths
#endif /* _WIN32 */
1748 c8994013 ths
1749 7183b4b4 aliguori
static int init_timer_alarm(void)
1750 8a7ddc38 bellard
{
1751 223f0d72 blueswir1
    struct qemu_alarm_timer *t = NULL;
1752 c8994013 ths
    int i, err = -1;
1753 f49e58dc aliguori
1754 f49e58dc aliguori
#ifndef _WIN32
1755 c96f1a48 aliguori
    int fds[2];
1756 c96f1a48 aliguori
1757 7183b4b4 aliguori
    err = pipe(fds);
1758 7183b4b4 aliguori
    if (err == -1)
1759 7183b4b4 aliguori
        return -errno;
1760 7183b4b4 aliguori
1761 7183b4b4 aliguori
    err = fcntl_setfl(fds[0], O_NONBLOCK);
1762 7183b4b4 aliguori
    if (err < 0)
1763 7183b4b4 aliguori
        goto fail;
1764 7183b4b4 aliguori
1765 7183b4b4 aliguori
    err = fcntl_setfl(fds[1], O_NONBLOCK);
1766 7183b4b4 aliguori
    if (err < 0)
1767 7183b4b4 aliguori
        goto fail;
1768 7183b4b4 aliguori
1769 c96f1a48 aliguori
    alarm_timer_rfd = fds[0];
1770 c96f1a48 aliguori
    alarm_timer_wfd = fds[1];
1771 f49e58dc aliguori
#endif
1772 c8994013 ths
1773 c8994013 ths
    for (i = 0; alarm_timers[i].name; i++) {
1774 c8994013 ths
        t = &alarm_timers[i];
1775 c8994013 ths
1776 c8994013 ths
        err = t->start(t);
1777 c8994013 ths
        if (!err)
1778 c8994013 ths
            break;
1779 67b915a5 bellard
    }
1780 fd872598 bellard
1781 c8994013 ths
    if (err) {
1782 7183b4b4 aliguori
        err = -ENOENT;
1783 7183b4b4 aliguori
        goto fail;
1784 67b915a5 bellard
    }
1785 c8994013 ths
1786 f49e58dc aliguori
#ifndef _WIN32
1787 6abfbd79 aliguori
    qemu_set_fd_handler2(alarm_timer_rfd, NULL,
1788 6abfbd79 aliguori
                         try_to_rearm_timer, NULL, t);
1789 f49e58dc aliguori
#endif
1790 6abfbd79 aliguori
1791 c8994013 ths
    alarm_timer = t;
1792 7183b4b4 aliguori
1793 6abfbd79 aliguori
    return 0;
1794 7183b4b4 aliguori
1795 7183b4b4 aliguori
fail:
1796 f49e58dc aliguori
#ifndef _WIN32
1797 7183b4b4 aliguori
    close(fds[0]);
1798 7183b4b4 aliguori
    close(fds[1]);
1799 f49e58dc aliguori
#endif
1800 7183b4b4 aliguori
    return err;
1801 8a7ddc38 bellard
}
1802 8a7ddc38 bellard
1803 9596ebb7 pbrook
static void quit_timers(void)
1804 40c3bac3 bellard
{
1805 c8994013 ths
    alarm_timer->stop(alarm_timer);
1806 c8994013 ths
    alarm_timer = NULL;
1807 40c3bac3 bellard
}
1808 40c3bac3 bellard
1809 c4b1fcc0 bellard
/***********************************************************/
1810 f6503059 balrog
/* host time/date access */
1811 f6503059 balrog
void qemu_get_timedate(struct tm *tm, int offset)
1812 f6503059 balrog
{
1813 f6503059 balrog
    time_t ti;
1814 f6503059 balrog
    struct tm *ret;
1815 f6503059 balrog
1816 f6503059 balrog
    time(&ti);
1817 f6503059 balrog
    ti += offset;
1818 f6503059 balrog
    if (rtc_date_offset == -1) {
1819 f6503059 balrog
        if (rtc_utc)
1820 f6503059 balrog
            ret = gmtime(&ti);
1821 f6503059 balrog
        else
1822 f6503059 balrog
            ret = localtime(&ti);
1823 f6503059 balrog
    } else {
1824 f6503059 balrog
        ti -= rtc_date_offset;
1825 f6503059 balrog
        ret = gmtime(&ti);
1826 f6503059 balrog
    }
1827 f6503059 balrog
1828 f6503059 balrog
    memcpy(tm, ret, sizeof(struct tm));
1829 f6503059 balrog
}
1830 f6503059 balrog
1831 f6503059 balrog
int qemu_timedate_diff(struct tm *tm)
1832 f6503059 balrog
{
1833 f6503059 balrog
    time_t seconds;
1834 f6503059 balrog
1835 f6503059 balrog
    if (rtc_date_offset == -1)
1836 f6503059 balrog
        if (rtc_utc)
1837 f6503059 balrog
            seconds = mktimegm(tm);
1838 f6503059 balrog
        else
1839 f6503059 balrog
            seconds = mktime(tm);
1840 f6503059 balrog
    else
1841 f6503059 balrog
        seconds = mktimegm(tm) + rtc_date_offset;
1842 f6503059 balrog
1843 f6503059 balrog
    return seconds - time(NULL);
1844 f6503059 balrog
}
1845 f6503059 balrog
1846 fd1dff4b bellard
#ifdef _WIN32
1847 fd1dff4b bellard
static void socket_cleanup(void)
1848 fd1dff4b bellard
{
1849 fd1dff4b bellard
    WSACleanup();
1850 fd1dff4b bellard
}
1851 82c643ff bellard
1852 fd1dff4b bellard
static int socket_init(void)
1853 fd1dff4b bellard
{
1854 fd1dff4b bellard
    WSADATA Data;
1855 fd1dff4b bellard
    int ret, err;
1856 fd1dff4b bellard
1857 fd1dff4b bellard
    ret = WSAStartup(MAKEWORD(2,2), &Data);
1858 fd1dff4b bellard
    if (ret != 0) {
1859 fd1dff4b bellard
        err = WSAGetLastError();
1860 fd1dff4b bellard
        fprintf(stderr, "WSAStartup: %d\n", err);
1861 fd1dff4b bellard
        return -1;
1862 fd1dff4b bellard
    }
1863 fd1dff4b bellard
    atexit(socket_cleanup);
1864 fd1dff4b bellard
    return 0;
1865 fd1dff4b bellard
}
1866 64b7b733 aurel32
#endif
1867 64b7b733 aurel32
1868 63a01ef8 aliguori
const char *get_opt_name(char *buf, int buf_size, const char *p)
1869 609497ab balrog
{
1870 609497ab balrog
    char *q;
1871 609497ab balrog
1872 609497ab balrog
    q = buf;
1873 609497ab balrog
    while (*p != '\0' && *p != '=') {
1874 609497ab balrog
        if (q && (q - buf) < buf_size - 1)
1875 609497ab balrog
            *q++ = *p;
1876 609497ab balrog
        p++;
1877 609497ab balrog
    }
1878 609497ab balrog
    if (q)
1879 609497ab balrog
        *q = '\0';
1880 609497ab balrog
1881 609497ab balrog
    return p;
1882 609497ab balrog
}
1883 609497ab balrog
1884 63a01ef8 aliguori
const char *get_opt_value(char *buf, int buf_size, const char *p)
1885 e4bcb14c ths
{
1886 e4bcb14c ths
    char *q;
1887 e4bcb14c ths
1888 e4bcb14c ths
    q = buf;
1889 e4bcb14c ths
    while (*p != '\0') {
1890 609497ab balrog
        if (*p == ',') {
1891 609497ab balrog
            if (*(p + 1) != ',')
1892 e4bcb14c ths
                break;
1893 e4bcb14c ths
            p++;
1894 609497ab balrog
        }
1895 e4bcb14c ths
        if (q && (q - buf) < buf_size - 1)
1896 e4bcb14c ths
            *q++ = *p;
1897 e4bcb14c ths
        p++;
1898 e4bcb14c ths
    }
1899 e4bcb14c ths
    if (q)
1900 e4bcb14c ths
        *q = '\0';
1901 e4bcb14c ths
1902 e4bcb14c ths
    return p;
1903 e4bcb14c ths
}
1904 e4bcb14c ths
1905 63a01ef8 aliguori
int get_param_value(char *buf, int buf_size,
1906 63a01ef8 aliguori
                    const char *tag, const char *str)
1907 7c9d8e07 bellard
{
1908 7c9d8e07 bellard
    const char *p;
1909 7c9d8e07 bellard
    char option[128];
1910 7c9d8e07 bellard
1911 7c9d8e07 bellard
    p = str;
1912 7c9d8e07 bellard
    for(;;) {
1913 609497ab balrog
        p = get_opt_name(option, sizeof(option), p);
1914 7c9d8e07 bellard
        if (*p != '=')
1915 7c9d8e07 bellard
            break;
1916 7c9d8e07 bellard
        p++;
1917 7c9d8e07 bellard
        if (!strcmp(tag, option)) {
1918 609497ab balrog
            (void)get_opt_value(buf, buf_size, p);
1919 e4bcb14c ths
            return strlen(buf);
1920 7c9d8e07 bellard
        } else {
1921 609497ab balrog
            p = get_opt_value(NULL, 0, p);
1922 7c9d8e07 bellard
        }
1923 7c9d8e07 bellard
        if (*p != ',')
1924 7c9d8e07 bellard
            break;
1925 7c9d8e07 bellard
        p++;
1926 7c9d8e07 bellard
    }
1927 7c9d8e07 bellard
    return 0;
1928 7c9d8e07 bellard
}
1929 7c9d8e07 bellard
1930 63a01ef8 aliguori
int check_params(char *buf, int buf_size,
1931 63a01ef8 aliguori
                 const char * const *params, const char *str)
1932 e4bcb14c ths
{
1933 e4bcb14c ths
    const char *p;
1934 e4bcb14c ths
    int i;
1935 e4bcb14c ths
1936 e4bcb14c ths
    p = str;
1937 e4bcb14c ths
    for(;;) {
1938 609497ab balrog
        p = get_opt_name(buf, buf_size, p);
1939 e4bcb14c ths
        if (*p != '=')
1940 e4bcb14c ths
            return -1;
1941 e4bcb14c ths
        p++;
1942 e4bcb14c ths
        for(i = 0; params[i] != NULL; i++)
1943 e4bcb14c ths
            if (!strcmp(params[i], buf))
1944 e4bcb14c ths
                break;
1945 e4bcb14c ths
        if (params[i] == NULL)
1946 e4bcb14c ths
            return -1;
1947 609497ab balrog
        p = get_opt_value(NULL, 0, p);
1948 e4bcb14c ths
        if (*p != ',')
1949 e4bcb14c ths
            break;
1950 e4bcb14c ths
        p++;
1951 e4bcb14c ths
    }
1952 e4bcb14c ths
    return 0;
1953 e4bcb14c ths
}
1954 e4bcb14c ths
1955 1ae26a18 balrog
/***********************************************************/
1956 1ae26a18 balrog
/* Bluetooth support */
1957 1ae26a18 balrog
static int nb_hcis;
1958 1ae26a18 balrog
static int cur_hci;
1959 1ae26a18 balrog
static struct HCIInfo *hci_table[MAX_NICS];
1960 dc72ac14 balrog
1961 1ae26a18 balrog
static struct bt_vlan_s {
1962 1ae26a18 balrog
    struct bt_scatternet_s net;
1963 1ae26a18 balrog
    int id;
1964 1ae26a18 balrog
    struct bt_vlan_s *next;
1965 1ae26a18 balrog
} *first_bt_vlan;
1966 1ae26a18 balrog
1967 1ae26a18 balrog
/* find or alloc a new bluetooth "VLAN" */
1968 674bb261 blueswir1
static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
1969 1ae26a18 balrog
{
1970 1ae26a18 balrog
    struct bt_vlan_s **pvlan, *vlan;
1971 1ae26a18 balrog
    for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1972 1ae26a18 balrog
        if (vlan->id == id)
1973 1ae26a18 balrog
            return &vlan->net;
1974 1ae26a18 balrog
    }
1975 1ae26a18 balrog
    vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1976 1ae26a18 balrog
    vlan->id = id;
1977 1ae26a18 balrog
    pvlan = &first_bt_vlan;
1978 1ae26a18 balrog
    while (*pvlan != NULL)
1979 1ae26a18 balrog
        pvlan = &(*pvlan)->next;
1980 1ae26a18 balrog
    *pvlan = vlan;
1981 1ae26a18 balrog
    return &vlan->net;
1982 1ae26a18 balrog
}
1983 1ae26a18 balrog
1984 1ae26a18 balrog
static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1985 1ae26a18 balrog
{
1986 1ae26a18 balrog
}
1987 1ae26a18 balrog
1988 1ae26a18 balrog
static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1989 1ae26a18 balrog
{
1990 1ae26a18 balrog
    return -ENOTSUP;
1991 1ae26a18 balrog
}
1992 1ae26a18 balrog
1993 1ae26a18 balrog
static struct HCIInfo null_hci = {
1994 1ae26a18 balrog
    .cmd_send = null_hci_send,
1995 1ae26a18 balrog
    .sco_send = null_hci_send,
1996 1ae26a18 balrog
    .acl_send = null_hci_send,
1997 1ae26a18 balrog
    .bdaddr_set = null_hci_addr_set,
1998 1ae26a18 balrog
};
1999 1ae26a18 balrog
2000 1ae26a18 balrog
struct HCIInfo *qemu_next_hci(void)
2001 1ae26a18 balrog
{
2002 1ae26a18 balrog
    if (cur_hci == nb_hcis)
2003 1ae26a18 balrog
        return &null_hci;
2004 1ae26a18 balrog
2005 1ae26a18 balrog
    return hci_table[cur_hci++];
2006 1ae26a18 balrog
}
2007 1ae26a18 balrog
2008 dc72ac14 balrog
static struct HCIInfo *hci_init(const char *str)
2009 dc72ac14 balrog
{
2010 dc72ac14 balrog
    char *endp;
2011 dc72ac14 balrog
    struct bt_scatternet_s *vlan = 0;
2012 dc72ac14 balrog
2013 dc72ac14 balrog
    if (!strcmp(str, "null"))
2014 dc72ac14 balrog
        /* null */
2015 dc72ac14 balrog
        return &null_hci;
2016 dc72ac14 balrog
    else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
2017 dc72ac14 balrog
        /* host[:hciN] */
2018 dc72ac14 balrog
        return bt_host_hci(str[4] ? str + 5 : "hci0");
2019 dc72ac14 balrog
    else if (!strncmp(str, "hci", 3)) {
2020 dc72ac14 balrog
        /* hci[,vlan=n] */
2021 dc72ac14 balrog
        if (str[3]) {
2022 dc72ac14 balrog
            if (!strncmp(str + 3, ",vlan=", 6)) {
2023 dc72ac14 balrog
                vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
2024 dc72ac14 balrog
                if (*endp)
2025 dc72ac14 balrog
                    vlan = 0;
2026 dc72ac14 balrog
            }
2027 dc72ac14 balrog
        } else
2028 dc72ac14 balrog
            vlan = qemu_find_bt_vlan(0);
2029 dc72ac14 balrog
        if (vlan)
2030 dc72ac14 balrog
           return bt_new_hci(vlan);
2031 dc72ac14 balrog
    }
2032 dc72ac14 balrog
2033 dc72ac14 balrog
    fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
2034 dc72ac14 balrog
2035 dc72ac14 balrog
    return 0;
2036 dc72ac14 balrog
}
2037 dc72ac14 balrog
2038 dc72ac14 balrog
static int bt_hci_parse(const char *str)
2039 dc72ac14 balrog
{
2040 dc72ac14 balrog
    struct HCIInfo *hci;
2041 dc72ac14 balrog
    bdaddr_t bdaddr;
2042 dc72ac14 balrog
2043 dc72ac14 balrog
    if (nb_hcis >= MAX_NICS) {
2044 dc72ac14 balrog
        fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
2045 dc72ac14 balrog
        return -1;
2046 dc72ac14 balrog
    }
2047 dc72ac14 balrog
2048 dc72ac14 balrog
    hci = hci_init(str);
2049 dc72ac14 balrog
    if (!hci)
2050 dc72ac14 balrog
        return -1;
2051 dc72ac14 balrog
2052 dc72ac14 balrog
    bdaddr.b[0] = 0x52;
2053 dc72ac14 balrog
    bdaddr.b[1] = 0x54;
2054 dc72ac14 balrog
    bdaddr.b[2] = 0x00;
2055 dc72ac14 balrog
    bdaddr.b[3] = 0x12;
2056 dc72ac14 balrog
    bdaddr.b[4] = 0x34;
2057 dc72ac14 balrog
    bdaddr.b[5] = 0x56 + nb_hcis;
2058 dc72ac14 balrog
    hci->bdaddr_set(hci, bdaddr.b);
2059 dc72ac14 balrog
2060 dc72ac14 balrog
    hci_table[nb_hcis++] = hci;
2061 dc72ac14 balrog
2062 dc72ac14 balrog
    return 0;
2063 dc72ac14 balrog
}
2064 dc72ac14 balrog
2065 dc72ac14 balrog
static void bt_vhci_add(int vlan_id)
2066 dc72ac14 balrog
{
2067 dc72ac14 balrog
    struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
2068 dc72ac14 balrog
2069 dc72ac14 balrog
    if (!vlan->slave)
2070 dc72ac14 balrog
        fprintf(stderr, "qemu: warning: adding a VHCI to "
2071 dc72ac14 balrog
                        "an empty scatternet %i\n", vlan_id);
2072 dc72ac14 balrog
2073 dc72ac14 balrog
    bt_vhci_init(bt_new_hci(vlan));
2074 dc72ac14 balrog
}
2075 dc72ac14 balrog
2076 dc72ac14 balrog
static struct bt_device_s *bt_device_add(const char *opt)
2077 dc72ac14 balrog
{
2078 dc72ac14 balrog
    struct bt_scatternet_s *vlan;
2079 dc72ac14 balrog
    int vlan_id = 0;
2080 dc72ac14 balrog
    char *endp = strstr(opt, ",vlan=");
2081 dc72ac14 balrog
    int len = (endp ? endp - opt : strlen(opt)) + 1;
2082 dc72ac14 balrog
    char devname[10];
2083 dc72ac14 balrog
2084 dc72ac14 balrog
    pstrcpy(devname, MIN(sizeof(devname), len), opt);
2085 dc72ac14 balrog
2086 dc72ac14 balrog
    if (endp) {
2087 dc72ac14 balrog
        vlan_id = strtol(endp + 6, &endp, 0);
2088 dc72ac14 balrog
        if (*endp) {
2089 dc72ac14 balrog
            fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
2090 dc72ac14 balrog
            return 0;
2091 dc72ac14 balrog
        }
2092 dc72ac14 balrog
    }
2093 dc72ac14 balrog
2094 dc72ac14 balrog
    vlan = qemu_find_bt_vlan(vlan_id);
2095 dc72ac14 balrog
2096 dc72ac14 balrog
    if (!vlan->slave)
2097 dc72ac14 balrog
        fprintf(stderr, "qemu: warning: adding a slave device to "
2098 dc72ac14 balrog
                        "an empty scatternet %i\n", vlan_id);
2099 dc72ac14 balrog
2100 dc72ac14 balrog
    if (!strcmp(devname, "keyboard"))
2101 dc72ac14 balrog
        return bt_keyboard_init(vlan);
2102 dc72ac14 balrog
2103 dc72ac14 balrog
    fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
2104 dc72ac14 balrog
    return 0;
2105 dc72ac14 balrog
}
2106 dc72ac14 balrog
2107 dc72ac14 balrog
static int bt_parse(const char *opt)
2108 dc72ac14 balrog
{
2109 dc72ac14 balrog
    const char *endp, *p;
2110 dc72ac14 balrog
    int vlan;
2111 dc72ac14 balrog
2112 dc72ac14 balrog
    if (strstart(opt, "hci", &endp)) {
2113 dc72ac14 balrog
        if (!*endp || *endp == ',') {
2114 dc72ac14 balrog
            if (*endp)
2115 dc72ac14 balrog
                if (!strstart(endp, ",vlan=", 0))
2116 dc72ac14 balrog
                    opt = endp + 1;
2117 dc72ac14 balrog
2118 dc72ac14 balrog
            return bt_hci_parse(opt);
2119 dc72ac14 balrog
       }
2120 dc72ac14 balrog
    } else if (strstart(opt, "vhci", &endp)) {
2121 dc72ac14 balrog
        if (!*endp || *endp == ',') {
2122 dc72ac14 balrog
            if (*endp) {
2123 dc72ac14 balrog
                if (strstart(endp, ",vlan=", &p)) {
2124 dc72ac14 balrog
                    vlan = strtol(p, (char **) &endp, 0);
2125 dc72ac14 balrog
                    if (*endp) {
2126 dc72ac14 balrog
                        fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
2127 dc72ac14 balrog
                        return 1;
2128 dc72ac14 balrog
                    }
2129 dc72ac14 balrog
                } else {
2130 dc72ac14 balrog
                    fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
2131 dc72ac14 balrog
                    return 1;
2132 dc72ac14 balrog
                }
2133 dc72ac14 balrog
            } else
2134 dc72ac14 balrog
                vlan = 0;
2135 dc72ac14 balrog
2136 dc72ac14 balrog
            bt_vhci_add(vlan);
2137 dc72ac14 balrog
            return 0;
2138 dc72ac14 balrog
        }
2139 dc72ac14 balrog
    } else if (strstart(opt, "device:", &endp))
2140 dc72ac14 balrog
        return !bt_device_add(endp);
2141 dc72ac14 balrog
2142 dc72ac14 balrog
    fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
2143 dc72ac14 balrog
    return 1;
2144 dc72ac14 balrog
}
2145 dc72ac14 balrog
2146 1ae26a18 balrog
/***********************************************************/
2147 1ae26a18 balrog
/* QEMU Block devices */
2148 1ae26a18 balrog
2149 609497ab balrog
#define HD_ALIAS "index=%d,media=disk"
2150 e4bcb14c ths
#define CDROM_ALIAS "index=2,media=cdrom"
2151 e4bcb14c ths
#define FD_ALIAS "index=%d,if=floppy"
2152 609497ab balrog
#define PFLASH_ALIAS "if=pflash"
2153 609497ab balrog
#define MTD_ALIAS "if=mtd"
2154 9d413d1d balrog
#define SD_ALIAS "index=0,if=sd"
2155 e4bcb14c ths
2156 7d5aca9e aliguori
static int drive_opt_get_free_idx(void)
2157 7d5aca9e aliguori
{
2158 7d5aca9e aliguori
    int index;
2159 7d5aca9e aliguori
2160 7d5aca9e aliguori
    for (index = 0; index < MAX_DRIVES; index++)
2161 7d5aca9e aliguori
        if (!drives_opt[index].used) {
2162 7d5aca9e aliguori
            drives_opt[index].used = 1;
2163 7d5aca9e aliguori
            return index;
2164 7d5aca9e aliguori
        }
2165 7d5aca9e aliguori
2166 7d5aca9e aliguori
    return -1;
2167 7d5aca9e aliguori
}
2168 7d5aca9e aliguori
2169 7d5aca9e aliguori
static int drive_get_free_idx(void)
2170 7d5aca9e aliguori
{
2171 7d5aca9e aliguori
    int index;
2172 7d5aca9e aliguori
2173 7d5aca9e aliguori
    for (index = 0; index < MAX_DRIVES; index++)
2174 7d5aca9e aliguori
        if (!drives_table[index].used) {
2175 7d5aca9e aliguori
            drives_table[index].used = 1;
2176 7d5aca9e aliguori
            return index;
2177 7d5aca9e aliguori
        }
2178 7d5aca9e aliguori
2179 7d5aca9e aliguori
    return -1;
2180 7d5aca9e aliguori
}
2181 7d5aca9e aliguori
2182 4d73cd3b aliguori
int drive_add(const char *file, const char *fmt, ...)
2183 e4bcb14c ths
{
2184 e4bcb14c ths
    va_list ap;
2185 7d5aca9e aliguori
    int index = drive_opt_get_free_idx();
2186 e4bcb14c ths
2187 7d5aca9e aliguori
    if (nb_drives_opt >= MAX_DRIVES || index == -1) {
2188 e4bcb14c ths
        fprintf(stderr, "qemu: too many drives\n");
2189 4d73cd3b aliguori
        return -1;
2190 e4bcb14c ths
    }
2191 e4bcb14c ths
2192 7d5aca9e aliguori
    drives_opt[index].file = file;
2193 e4bcb14c ths
    va_start(ap, fmt);
2194 7d5aca9e aliguori
    vsnprintf(drives_opt[index].opt,
2195 609497ab balrog
              sizeof(drives_opt[0].opt), fmt, ap);
2196 e4bcb14c ths
    va_end(ap);
2197 e4bcb14c ths
2198 7d5aca9e aliguori
    nb_drives_opt++;
2199 7d5aca9e aliguori
    return index;
2200 e4bcb14c ths
}
2201 e4bcb14c ths
2202 b01b1111 aliguori
void drive_remove(int index)
2203 b01b1111 aliguori
{
2204 b01b1111 aliguori
    drives_opt[index].used = 0;
2205 b01b1111 aliguori
    nb_drives_opt--;
2206 b01b1111 aliguori
}
2207 b01b1111 aliguori
2208 f60d39bc ths
int drive_get_index(BlockInterfaceType type, int bus, int unit)
2209 e4bcb14c ths
{
2210 e4bcb14c ths
    int index;
2211 e4bcb14c ths
2212 e4bcb14c ths
    /* seek interface, bus and unit */
2213 e4bcb14c ths
2214 7d5aca9e aliguori
    for (index = 0; index < MAX_DRIVES; index++)
2215 f60d39bc ths
        if (drives_table[index].type == type &&
2216 e4bcb14c ths
            drives_table[index].bus == bus &&
2217 7d5aca9e aliguori
            drives_table[index].unit == unit &&
2218 7d5aca9e aliguori
            drives_table[index].used)
2219 e4bcb14c ths
        return index;
2220 e4bcb14c ths
2221 e4bcb14c ths
    return -1;
2222 e4bcb14c ths
}
2223 e4bcb14c ths
2224 f60d39bc ths
int drive_get_max_bus(BlockInterfaceType type)
2225 e4bcb14c ths
{
2226 e4bcb14c ths
    int max_bus;
2227 e4bcb14c ths
    int index;
2228 e4bcb14c ths
2229 e4bcb14c ths
    max_bus = -1;
2230 e4bcb14c ths
    for (index = 0; index < nb_drives; index++) {
2231 f60d39bc ths
        if(drives_table[index].type == type &&
2232 e4bcb14c ths
           drives_table[index].bus > max_bus)
2233 e4bcb14c ths
            max_bus = drives_table[index].bus;
2234 e4bcb14c ths
    }
2235 e4bcb14c ths
    return max_bus;
2236 e4bcb14c ths
}
2237 e4bcb14c ths
2238 fa879c64 aliguori
const char *drive_get_serial(BlockDriverState *bdrv)
2239 fa879c64 aliguori
{
2240 fa879c64 aliguori
    int index;
2241 fa879c64 aliguori
2242 fa879c64 aliguori
    for (index = 0; index < nb_drives; index++)
2243 fa879c64 aliguori
        if (drives_table[index].bdrv == bdrv)
2244 fa879c64 aliguori
            return drives_table[index].serial;
2245 fa879c64 aliguori
2246 fa879c64 aliguori
    return "\0";
2247 fa879c64 aliguori
}
2248 fa879c64 aliguori
2249 428c5705 aliguori
BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
2250 428c5705 aliguori
{
2251 428c5705 aliguori
    int index;
2252 428c5705 aliguori
2253 428c5705 aliguori
    for (index = 0; index < nb_drives; index++)
2254 428c5705 aliguori
        if (drives_table[index].bdrv == bdrv)
2255 428c5705 aliguori
            return drives_table[index].onerror;
2256 428c5705 aliguori
2257 cdad4bd8 aliguori
    return BLOCK_ERR_STOP_ENOSPC;
2258 428c5705 aliguori
}
2259 428c5705 aliguori
2260 a1620fac aurel32
static void bdrv_format_print(void *opaque, const char *name)
2261 a1620fac aurel32
{
2262 a1620fac aurel32
    fprintf(stderr, " %s", name);
2263 a1620fac aurel32
}
2264 a1620fac aurel32
2265 b01b1111 aliguori
void drive_uninit(BlockDriverState *bdrv)
2266 b01b1111 aliguori
{
2267 b01b1111 aliguori
    int i;
2268 b01b1111 aliguori
2269 b01b1111 aliguori
    for (i = 0; i < MAX_DRIVES; i++)
2270 b01b1111 aliguori
        if (drives_table[i].bdrv == bdrv) {
2271 b01b1111 aliguori
            drives_table[i].bdrv = NULL;
2272 b01b1111 aliguori
            drives_table[i].used = 0;
2273 b01b1111 aliguori
            drive_remove(drives_table[i].drive_opt_idx);
2274 b01b1111 aliguori
            nb_drives--;
2275 b01b1111 aliguori
            break;
2276 b01b1111 aliguori
        }
2277 b01b1111 aliguori
}
2278 b01b1111 aliguori
2279 4d73cd3b aliguori
int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
2280 e4bcb14c ths
{
2281 e4bcb14c ths
    char buf[128];
2282 e4bcb14c ths
    char file[1024];
2283 c8522bdf balrog
    char devname[128];
2284 fa879c64 aliguori
    char serial[21];
2285 c8522bdf balrog
    const char *mediastr = "";
2286 f60d39bc ths
    BlockInterfaceType type;
2287 e4bcb14c ths
    enum { MEDIA_DISK, MEDIA_CDROM } media;
2288 e4bcb14c ths
    int bus_id, unit_id;
2289 e4bcb14c ths
    int cyls, heads, secs, translation;
2290 e4bcb14c ths
    BlockDriverState *bdrv;
2291 1e72d3b7 aurel32
    BlockDriver *drv = NULL;
2292 4d73cd3b aliguori
    QEMUMachine *machine = opaque;
2293 e4bcb14c ths
    int max_devs;
2294 e4bcb14c ths
    int index;
2295 33f00271 balrog
    int cache;
2296 428c5705 aliguori
    int bdrv_flags, onerror;
2297 7d5aca9e aliguori
    int drives_table_idx;
2298 609497ab balrog
    char *str = arg->opt;
2299 7ccfb2eb blueswir1
    static const char * const params[] = { "bus", "unit", "if", "index",
2300 7ccfb2eb blueswir1
                                           "cyls", "heads", "secs", "trans",
2301 7ccfb2eb blueswir1
                                           "media", "snapshot", "file",
2302 428c5705 aliguori
                                           "cache", "format", "serial", "werror",
2303 428c5705 aliguori
                                           NULL };
2304 e4bcb14c ths
2305 e4bcb14c ths
    if (check_params(buf, sizeof(buf), params, str) < 0) {
2306 ff993638 balrog
         fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
2307 e4bcb14c ths
                         buf, str);
2308 e4bcb14c ths
         return -1;
2309 e4bcb14c ths
    }
2310 e4bcb14c ths
2311 e4bcb14c ths
    file[0] = 0;
2312 e4bcb14c ths
    cyls = heads = secs = 0;
2313 e4bcb14c ths
    bus_id = 0;
2314 e4bcb14c ths
    unit_id = -1;
2315 e4bcb14c ths
    translation = BIOS_ATA_TRANSLATION_AUTO;
2316 e4bcb14c ths
    index = -1;
2317 4dc822d7 aliguori
    cache = 3;
2318 e4bcb14c ths
2319 c9b1ae2c blueswir1
    if (machine->use_scsi) {
2320 f60d39bc ths
        type = IF_SCSI;
2321 e4bcb14c ths
        max_devs = MAX_SCSI_DEVS;
2322 363a37d5 blueswir1
        pstrcpy(devname, sizeof(devname), "scsi");
2323 e4bcb14c ths
    } else {
2324 f60d39bc ths
        type = IF_IDE;
2325 e4bcb14c ths
        max_devs = MAX_IDE_DEVS;
2326 363a37d5 blueswir1
        pstrcpy(devname, sizeof(devname), "ide");
2327 e4bcb14c ths
    }
2328 e4bcb14c ths
    media = MEDIA_DISK;
2329 e4bcb14c ths
2330 e4bcb14c ths
    /* extract parameters */
2331 e4bcb14c ths
2332 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "bus", str)) {
2333 e4bcb14c ths
        bus_id = strtol(buf, NULL, 0);
2334 e4bcb14c ths
        if (bus_id < 0) {
2335 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2336 e4bcb14c ths
            return -1;
2337 e4bcb14c ths
        }
2338 e4bcb14c ths
    }
2339 e4bcb14c ths
2340 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "unit", str)) {
2341 e4bcb14c ths
        unit_id = strtol(buf, NULL, 0);
2342 e4bcb14c ths
        if (unit_id < 0) {
2343 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2344 e4bcb14c ths
            return -1;
2345 e4bcb14c ths
        }
2346 e4bcb14c ths
    }
2347 e4bcb14c ths
2348 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "if", str)) {
2349 ae45d369 bellard
        pstrcpy(devname, sizeof(devname), buf);
2350 e4bcb14c ths
        if (!strcmp(buf, "ide")) {
2351 f60d39bc ths
            type = IF_IDE;
2352 e4bcb14c ths
            max_devs = MAX_IDE_DEVS;
2353 e4bcb14c ths
        } else if (!strcmp(buf, "scsi")) {
2354 f60d39bc ths
            type = IF_SCSI;
2355 e4bcb14c ths
            max_devs = MAX_SCSI_DEVS;
2356 e4bcb14c ths
        } else if (!strcmp(buf, "floppy")) {
2357 f60d39bc ths
            type = IF_FLOPPY;
2358 e4bcb14c ths
            max_devs = 0;
2359 e4bcb14c ths
        } else if (!strcmp(buf, "pflash")) {
2360 f60d39bc ths
            type = IF_PFLASH;
2361 e4bcb14c ths
            max_devs = 0;
2362 e4bcb14c ths
        } else if (!strcmp(buf, "mtd")) {
2363 f60d39bc ths
            type = IF_MTD;
2364 e4bcb14c ths
            max_devs = 0;
2365 e4bcb14c ths
        } else if (!strcmp(buf, "sd")) {
2366 f60d39bc ths
            type = IF_SD;
2367 e4bcb14c ths
            max_devs = 0;
2368 6e02c38d aliguori
        } else if (!strcmp(buf, "virtio")) {
2369 6e02c38d aliguori
            type = IF_VIRTIO;
2370 6e02c38d aliguori
            max_devs = 0;
2371 6e02c38d aliguori
        } else {
2372 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2373 e4bcb14c ths
            return -1;
2374 e4bcb14c ths
        }
2375 e4bcb14c ths
    }
2376 e4bcb14c ths
2377 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "index", str)) {
2378 e4bcb14c ths
        index = strtol(buf, NULL, 0);
2379 e4bcb14c ths
        if (index < 0) {
2380 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid index\n", str);
2381 e4bcb14c ths
            return -1;
2382 e4bcb14c ths
        }
2383 e4bcb14c ths
    }
2384 e4bcb14c ths
2385 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2386 e4bcb14c ths
        cyls = strtol(buf, NULL, 0);
2387 e4bcb14c ths
    }
2388 e4bcb14c ths
2389 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "heads", str)) {
2390 e4bcb14c ths
        heads = strtol(buf, NULL, 0);
2391 e4bcb14c ths
    }
2392 e4bcb14c ths
2393 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "secs", str)) {
2394 e4bcb14c ths
        secs = strtol(buf, NULL, 0);
2395 e4bcb14c ths
    }
2396 e4bcb14c ths
2397 e4bcb14c ths
    if (cyls || heads || secs) {
2398 e4bcb14c ths
        if (cyls < 1 || cyls > 16383) {
2399 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2400 e4bcb14c ths
            return -1;
2401 e4bcb14c ths
        }
2402 e4bcb14c ths
        if (heads < 1 || heads > 16) {
2403 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2404 e4bcb14c ths
            return -1;
2405 e4bcb14c ths
        }
2406 e4bcb14c ths
        if (secs < 1 || secs > 63) {
2407 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2408 e4bcb14c ths
            return -1;
2409 e4bcb14c ths
        }
2410 e4bcb14c ths
    }
2411 e4bcb14c ths
2412 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "trans", str)) {
2413 e4bcb14c ths
        if (!cyls) {
2414 e4bcb14c ths
            fprintf(stderr,
2415 e4bcb14c ths
                    "qemu: '%s' trans must be used with cyls,heads and secs\n",
2416 e4bcb14c ths
                    str);
2417 e4bcb14c ths
            return -1;
2418 e4bcb14c ths
        }
2419 e4bcb14c ths
        if (!strcmp(buf, "none"))
2420 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_NONE;
2421 e4bcb14c ths
        else if (!strcmp(buf, "lba"))
2422 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_LBA;
2423 e4bcb14c ths
        else if (!strcmp(buf, "auto"))
2424 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_AUTO;
2425 e4bcb14c ths
        else {
2426 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2427 e4bcb14c ths
            return -1;
2428 e4bcb14c ths
        }
2429 e4bcb14c ths
    }
2430 e4bcb14c ths
2431 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "media", str)) {
2432 e4bcb14c ths
        if (!strcmp(buf, "disk")) {
2433 e4bcb14c ths
            media = MEDIA_DISK;
2434 e4bcb14c ths
        } else if (!strcmp(buf, "cdrom")) {
2435 e4bcb14c ths
            if (cyls || secs || heads) {
2436 e4bcb14c ths
                fprintf(stderr,
2437 e4bcb14c ths
                        "qemu: '%s' invalid physical CHS format\n", str);
2438 e4bcb14c ths
                return -1;
2439 e4bcb14c ths
            }
2440 e4bcb14c ths
            media = MEDIA_CDROM;
2441 e4bcb14c ths
        } else {
2442 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid media\n", str);
2443 e4bcb14c ths
            return -1;
2444 e4bcb14c ths
        }
2445 e4bcb14c ths
    }
2446 e4bcb14c ths
2447 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2448 e4bcb14c ths
        if (!strcmp(buf, "on"))
2449 e4bcb14c ths
            snapshot = 1;
2450 e4bcb14c ths
        else if (!strcmp(buf, "off"))
2451 e4bcb14c ths
            snapshot = 0;
2452 e4bcb14c ths
        else {
2453 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2454 e4bcb14c ths
            return -1;
2455 e4bcb14c ths
        }
2456 e4bcb14c ths
    }
2457 e4bcb14c ths
2458 33f00271 balrog
    if (get_param_value(buf, sizeof(buf), "cache", str)) {
2459 9f7965c7 aliguori
        if (!strcmp(buf, "off") || !strcmp(buf, "none"))
2460 33f00271 balrog
            cache = 0;
2461 9f7965c7 aliguori
        else if (!strcmp(buf, "writethrough"))
2462 33f00271 balrog
            cache = 1;
2463 9f7965c7 aliguori
        else if (!strcmp(buf, "writeback"))
2464 9f7965c7 aliguori
            cache = 2;
2465 33f00271 balrog
        else {
2466 33f00271 balrog
           fprintf(stderr, "qemu: invalid cache option\n");
2467 33f00271 balrog
           return -1;
2468 33f00271 balrog
        }
2469 33f00271 balrog
    }
2470 33f00271 balrog
2471 1e72d3b7 aurel32
    if (get_param_value(buf, sizeof(buf), "format", str)) {
2472 a1620fac aurel32
       if (strcmp(buf, "?") == 0) {
2473 a1620fac aurel32
            fprintf(stderr, "qemu: Supported formats:");
2474 a1620fac aurel32
            bdrv_iterate_format(bdrv_format_print, NULL);
2475 a1620fac aurel32
            fprintf(stderr, "\n");
2476 a1620fac aurel32
            return -1;
2477 a1620fac aurel32
        }
2478 1e72d3b7 aurel32
        drv = bdrv_find_format(buf);
2479 1e72d3b7 aurel32
        if (!drv) {
2480 1e72d3b7 aurel32
            fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2481 1e72d3b7 aurel32
            return -1;
2482 1e72d3b7 aurel32
        }
2483 1e72d3b7 aurel32
    }
2484 1e72d3b7 aurel32
2485 609497ab balrog
    if (arg->file == NULL)
2486 609497ab balrog
        get_param_value(file, sizeof(file), "file", str);
2487 609497ab balrog
    else
2488 609497ab balrog
        pstrcpy(file, sizeof(file), arg->file);
2489 e4bcb14c ths
2490 fa879c64 aliguori
    if (!get_param_value(serial, sizeof(serial), "serial", str))
2491 fa879c64 aliguori
            memset(serial, 0,  sizeof(serial));
2492 fa879c64 aliguori
2493 cdad4bd8 aliguori
    onerror = BLOCK_ERR_STOP_ENOSPC;
2494 428c5705 aliguori
    if (get_param_value(buf, sizeof(serial), "werror", str)) {
2495 869a5c6d aliguori
        if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
2496 ea8a5d7f aliguori
            fprintf(stderr, "werror is no supported by this format\n");
2497 428c5705 aliguori
            return -1;
2498 428c5705 aliguori
        }
2499 428c5705 aliguori
        if (!strcmp(buf, "ignore"))
2500 428c5705 aliguori
            onerror = BLOCK_ERR_IGNORE;
2501 428c5705 aliguori
        else if (!strcmp(buf, "enospc"))
2502 428c5705 aliguori
            onerror = BLOCK_ERR_STOP_ENOSPC;
2503 428c5705 aliguori
        else if (!strcmp(buf, "stop"))
2504 428c5705 aliguori
            onerror = BLOCK_ERR_STOP_ANY;
2505 428c5705 aliguori
        else if (!strcmp(buf, "report"))
2506 428c5705 aliguori
            onerror = BLOCK_ERR_REPORT;
2507 428c5705 aliguori
        else {
2508 428c5705 aliguori
            fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
2509 428c5705 aliguori
            return -1;
2510 428c5705 aliguori
        }
2511 428c5705 aliguori
    }
2512 428c5705 aliguori
2513 e4bcb14c ths
    /* compute bus and unit according index */
2514 e4bcb14c ths
2515 e4bcb14c ths
    if (index != -1) {
2516 e4bcb14c ths
        if (bus_id != 0 || unit_id != -1) {
2517 e4bcb14c ths
            fprintf(stderr,
2518 e4bcb14c ths
                    "qemu: '%s' index cannot be used with bus and unit\n", str);
2519 e4bcb14c ths
            return -1;
2520 e4bcb14c ths
        }
2521 e4bcb14c ths
        if (max_devs == 0)
2522 e4bcb14c ths
        {
2523 e4bcb14c ths
            unit_id = index;
2524 e4bcb14c ths
            bus_id = 0;
2525 e4bcb14c ths
        } else {
2526 e4bcb14c ths
            unit_id = index % max_devs;
2527 e4bcb14c ths
            bus_id = index / max_devs;
2528 e4bcb14c ths
        }
2529 e4bcb14c ths
    }
2530 e4bcb14c ths
2531 e4bcb14c ths
    /* if user doesn't specify a unit_id,
2532 e4bcb14c ths
     * try to find the first free
2533 e4bcb14c ths
     */
2534 e4bcb14c ths
2535 e4bcb14c ths
    if (unit_id == -1) {
2536 e4bcb14c ths
       unit_id = 0;
2537 f60d39bc ths
       while (drive_get_index(type, bus_id, unit_id) != -1) {
2538 e4bcb14c ths
           unit_id++;
2539 e4bcb14c ths
           if (max_devs && unit_id >= max_devs) {
2540 e4bcb14c ths
               unit_id -= max_devs;
2541 e4bcb14c ths
               bus_id++;
2542 e4bcb14c ths
           }
2543 e4bcb14c ths
       }
2544 e4bcb14c ths
    }
2545 e4bcb14c ths
2546 e4bcb14c ths
    /* check unit id */
2547 e4bcb14c ths
2548 e4bcb14c ths
    if (max_devs && unit_id >= max_devs) {
2549 e4bcb14c ths
        fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2550 e4bcb14c ths
                        str, unit_id, max_devs - 1);
2551 e4bcb14c ths
        return -1;
2552 e4bcb14c ths
    }
2553 e4bcb14c ths
2554 e4bcb14c ths
    /*
2555 e4bcb14c ths
     * ignore multiple definitions
2556 e4bcb14c ths
     */
2557 e4bcb14c ths
2558 f60d39bc ths
    if (drive_get_index(type, bus_id, unit_id) != -1)
2559 4d73cd3b aliguori
        return -2;
2560 e4bcb14c ths
2561 e4bcb14c ths
    /* init */
2562 e4bcb14c ths
2563 f60d39bc ths
    if (type == IF_IDE || type == IF_SCSI)
2564 c8522bdf balrog
        mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
2565 e6198a70 balrog
    if (max_devs)
2566 e6198a70 balrog
        snprintf(buf, sizeof(buf), "%s%i%s%i",
2567 e6198a70 balrog
                 devname, bus_id, mediastr, unit_id);
2568 e6198a70 balrog
    else
2569 e6198a70 balrog
        snprintf(buf, sizeof(buf), "%s%s%i",
2570 e6198a70 balrog
                 devname, mediastr, unit_id);
2571 e4bcb14c ths
    bdrv = bdrv_new(buf);
2572 7d5aca9e aliguori
    drives_table_idx = drive_get_free_idx();
2573 7d5aca9e aliguori
    drives_table[drives_table_idx].bdrv = bdrv;
2574 7d5aca9e aliguori
    drives_table[drives_table_idx].type = type;
2575 7d5aca9e aliguori
    drives_table[drives_table_idx].bus = bus_id;
2576 7d5aca9e aliguori
    drives_table[drives_table_idx].unit = unit_id;
2577 7d5aca9e aliguori
    drives_table[drives_table_idx].onerror = onerror;
2578 b01b1111 aliguori
    drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
2579 fa879c64 aliguori
    strncpy(drives_table[nb_drives].serial, serial, sizeof(serial));
2580 e4bcb14c ths
    nb_drives++;
2581 e4bcb14c ths
2582 f60d39bc ths
    switch(type) {
2583 e4bcb14c ths
    case IF_IDE:
2584 e4bcb14c ths
    case IF_SCSI:
2585 e4bcb14c ths
        switch(media) {
2586 e4bcb14c ths
        case MEDIA_DISK:
2587 e4bcb14c ths
            if (cyls != 0) {
2588 e4bcb14c ths
                bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2589 e4bcb14c ths
                bdrv_set_translation_hint(bdrv, translation);
2590 e4bcb14c ths
            }
2591 e4bcb14c ths
            break;
2592 e4bcb14c ths
        case MEDIA_CDROM:
2593 e4bcb14c ths
            bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2594 e4bcb14c ths
            break;
2595 e4bcb14c ths
        }
2596 e4bcb14c ths
        break;
2597 e4bcb14c ths
    case IF_SD:
2598 e4bcb14c ths
        /* FIXME: This isn't really a floppy, but it's a reasonable
2599 e4bcb14c ths
           approximation.  */
2600 e4bcb14c ths
    case IF_FLOPPY:
2601 e4bcb14c ths
        bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2602 e4bcb14c ths
        break;
2603 e4bcb14c ths
    case IF_PFLASH:
2604 e4bcb14c ths
    case IF_MTD:
2605 6e02c38d aliguori
    case IF_VIRTIO:
2606 e4bcb14c ths
        break;
2607 e4bcb14c ths
    }
2608 e4bcb14c ths
    if (!file[0])
2609 4d73cd3b aliguori
        return -2;
2610 33f00271 balrog
    bdrv_flags = 0;
2611 9f7965c7 aliguori
    if (snapshot) {
2612 33f00271 balrog
        bdrv_flags |= BDRV_O_SNAPSHOT;
2613 9f7965c7 aliguori
        cache = 2; /* always use write-back with snapshot */
2614 9f7965c7 aliguori
    }
2615 9f7965c7 aliguori
    if (cache == 0) /* no caching */
2616 9f7965c7 aliguori
        bdrv_flags |= BDRV_O_NOCACHE;
2617 9f7965c7 aliguori
    else if (cache == 2) /* write-back */
2618 9f7965c7 aliguori
        bdrv_flags |= BDRV_O_CACHE_WB;
2619 4dc822d7 aliguori
    else if (cache == 3) /* not specified */
2620 4dc822d7 aliguori
        bdrv_flags |= BDRV_O_CACHE_DEF;
2621 c0f4ce77 aliguori
    if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
2622 e4bcb14c ths
        fprintf(stderr, "qemu: could not open disk image %s\n",
2623 e4bcb14c ths
                        file);
2624 e4bcb14c ths
        return -1;
2625 e4bcb14c ths
    }
2626 c0f4ce77 aliguori
    if (bdrv_key_required(bdrv))
2627 c0f4ce77 aliguori
        autostart = 0;
2628 4d73cd3b aliguori
    return drives_table_idx;
2629 e4bcb14c ths
}
2630 e4bcb14c ths
2631 330d0414 bellard
/***********************************************************/
2632 a594cfbf bellard
/* USB devices */
2633 a594cfbf bellard
2634 0d92ed30 pbrook
static USBPort *used_usb_ports;
2635 0d92ed30 pbrook
static USBPort *free_usb_ports;
2636 0d92ed30 pbrook
2637 0d92ed30 pbrook
/* ??? Maybe change this to register a hub to keep track of the topology.  */
2638 0d92ed30 pbrook
void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2639 0d92ed30 pbrook
                            usb_attachfn attach)
2640 0d92ed30 pbrook
{
2641 0d92ed30 pbrook
    port->opaque = opaque;
2642 0d92ed30 pbrook
    port->index = index;
2643 0d92ed30 pbrook
    port->attach = attach;
2644 0d92ed30 pbrook
    port->next = free_usb_ports;
2645 0d92ed30 pbrook
    free_usb_ports = port;
2646 0d92ed30 pbrook
}
2647 0d92ed30 pbrook
2648 4b096fc9 aliguori
int usb_device_add_dev(USBDevice *dev)
2649 4b096fc9 aliguori
{
2650 4b096fc9 aliguori
    USBPort *port;
2651 4b096fc9 aliguori
2652 4b096fc9 aliguori
    /* Find a USB port to add the device to.  */
2653 4b096fc9 aliguori
    port = free_usb_ports;
2654 4b096fc9 aliguori
    if (!port->next) {
2655 4b096fc9 aliguori
        USBDevice *hub;
2656 4b096fc9 aliguori
2657 4b096fc9 aliguori
        /* Create a new hub and chain it on.  */
2658 4b096fc9 aliguori
        free_usb_ports = NULL;
2659 4b096fc9 aliguori
        port->next = used_usb_ports;
2660 4b096fc9 aliguori
        used_usb_ports = port;
2661 4b096fc9 aliguori
2662 4b096fc9 aliguori
        hub = usb_hub_init(VM_USB_HUB_SIZE);
2663 4b096fc9 aliguori
        usb_attach(port, hub);
2664 4b096fc9 aliguori
        port = free_usb_ports;
2665 4b096fc9 aliguori
    }
2666 4b096fc9 aliguori
2667 4b096fc9 aliguori
    free_usb_ports = port->next;
2668 4b096fc9 aliguori
    port->next = used_usb_ports;
2669 4b096fc9 aliguori
    used_usb_ports = port;
2670 4b096fc9 aliguori
    usb_attach(port, dev);
2671 4b096fc9 aliguori
    return 0;
2672 4b096fc9 aliguori
}
2673 4b096fc9 aliguori
2674 bb5fc20f aliguori
static void usb_msd_password_cb(void *opaque, int err)
2675 bb5fc20f aliguori
{
2676 bb5fc20f aliguori
    USBDevice *dev = opaque;
2677 bb5fc20f aliguori
2678 bb5fc20f aliguori
    if (!err)
2679 bb5fc20f aliguori
        usb_device_add_dev(dev);
2680 bb5fc20f aliguori
    else
2681 bb5fc20f aliguori
        dev->handle_destroy(dev);
2682 bb5fc20f aliguori
}
2683 bb5fc20f aliguori
2684 c0f4ce77 aliguori
static int usb_device_add(const char *devname, int is_hotplug)
2685 a594cfbf bellard
{
2686 a594cfbf bellard
    const char *p;
2687 a594cfbf bellard
    USBDevice *dev;
2688 a594cfbf bellard
2689 0d92ed30 pbrook
    if (!free_usb_ports)
2690 a594cfbf bellard
        return -1;
2691 a594cfbf bellard
2692 a594cfbf bellard
    if (strstart(devname, "host:", &p)) {
2693 a594cfbf bellard
        dev = usb_host_device_open(p);
2694 a594cfbf bellard
    } else if (!strcmp(devname, "mouse")) {
2695 a594cfbf bellard
        dev = usb_mouse_init();
2696 09b26c5e bellard
    } else if (!strcmp(devname, "tablet")) {
2697 47b2d338 balrog
        dev = usb_tablet_init();
2698 47b2d338 balrog
    } else if (!strcmp(devname, "keyboard")) {
2699 47b2d338 balrog
        dev = usb_keyboard_init();
2700 2e5d83bb pbrook
    } else if (strstart(devname, "disk:", &p)) {
2701 c0f4ce77 aliguori
        BlockDriverState *bs;
2702 c0f4ce77 aliguori
2703 bb5fc20f aliguori
        dev = usb_msd_init(p);
2704 c0f4ce77 aliguori
        if (!dev)
2705 c0f4ce77 aliguori
            return -1;
2706 bb5fc20f aliguori
        bs = usb_msd_get_bdrv(dev);
2707 c0f4ce77 aliguori
        if (bdrv_key_required(bs)) {
2708 c0f4ce77 aliguori
            autostart = 0;
2709 bb5fc20f aliguori
            if (is_hotplug) {
2710 376253ec aliguori
                monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2711 376253ec aliguori
                                            dev);
2712 bb5fc20f aliguori
                return 0;
2713 c0f4ce77 aliguori
            }
2714 c0f4ce77 aliguori
        }
2715 f6d2a316 balrog
    } else if (!strcmp(devname, "wacom-tablet")) {
2716 f6d2a316 balrog
        dev = usb_wacom_init();
2717 a7954218 balrog
    } else if (strstart(devname, "serial:", &p)) {
2718 a7954218 balrog
        dev = usb_serial_init(p);
2719 2e4d9fb1 aurel32
#ifdef CONFIG_BRLAPI
2720 2e4d9fb1 aurel32
    } else if (!strcmp(devname, "braille")) {
2721 2e4d9fb1 aurel32
        dev = usb_baum_init();
2722 2e4d9fb1 aurel32
#endif
2723 6c9f886c balrog
    } else if (strstart(devname, "net:", &p)) {
2724 9ad97e65 balrog
        int nic = nb_nics;
2725 6c9f886c balrog
2726 9ad97e65 balrog
        if (net_client_init("nic", p) < 0)
2727 6c9f886c balrog
            return -1;
2728 9ad97e65 balrog
        nd_table[nic].model = "usb";
2729 9ad97e65 balrog
        dev = usb_net_init(&nd_table[nic]);
2730 dc72ac14 balrog
    } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2731 dc72ac14 balrog
        dev = usb_bt_init(devname[2] ? hci_init(p) :
2732 dc72ac14 balrog
                        bt_new_hci(qemu_find_bt_vlan(0)));
2733 a594cfbf bellard
    } else {
2734 a594cfbf bellard
        return -1;
2735 a594cfbf bellard
    }
2736 0d92ed30 pbrook
    if (!dev)
2737 0d92ed30 pbrook
        return -1;
2738 0d92ed30 pbrook
2739 4b096fc9 aliguori
    return usb_device_add_dev(dev);
2740 a594cfbf bellard
}
2741 a594cfbf bellard
2742 1f3870ab aliguori
int usb_device_del_addr(int bus_num, int addr)
2743 a594cfbf bellard
{
2744 0d92ed30 pbrook
    USBPort *port;
2745 0d92ed30 pbrook
    USBPort **lastp;
2746 059809e4 bellard
    USBDevice *dev;
2747 a594cfbf bellard
2748 0d92ed30 pbrook
    if (!used_usb_ports)
2749 a594cfbf bellard
        return -1;
2750 a594cfbf bellard
2751 a594cfbf bellard
    if (bus_num != 0)
2752 a594cfbf bellard
        return -1;
2753 0d92ed30 pbrook
2754 0d92ed30 pbrook
    lastp = &used_usb_ports;
2755 0d92ed30 pbrook
    port = used_usb_ports;
2756 0d92ed30 pbrook
    while (port && port->dev->addr != addr) {
2757 0d92ed30 pbrook
        lastp = &port->next;
2758 0d92ed30 pbrook
        port = port->next;
2759 a594cfbf bellard
    }
2760 0d92ed30 pbrook
2761 0d92ed30 pbrook
    if (!port)
2762 a594cfbf bellard
        return -1;
2763 0d92ed30 pbrook
2764 059809e4 bellard
    dev = port->dev;
2765 0d92ed30 pbrook
    *lastp = port->next;
2766 0d92ed30 pbrook
    usb_attach(port, NULL);
2767 059809e4 bellard
    dev->handle_destroy(dev);
2768 0d92ed30 pbrook
    port->next = free_usb_ports;
2769 0d92ed30 pbrook
    free_usb_ports = port;
2770 a594cfbf bellard
    return 0;
2771 a594cfbf bellard
}
2772 a594cfbf bellard
2773 1f3870ab aliguori
static int usb_device_del(const char *devname)
2774 1f3870ab aliguori
{
2775 1f3870ab aliguori
    int bus_num, addr;
2776 1f3870ab aliguori
    const char *p;
2777 1f3870ab aliguori
2778 5d0c5750 aliguori
    if (strstart(devname, "host:", &p))
2779 5d0c5750 aliguori
        return usb_host_device_close(p);
2780 5d0c5750 aliguori
2781 1f3870ab aliguori
    if (!used_usb_ports)
2782 1f3870ab aliguori
        return -1;
2783 1f3870ab aliguori
2784 1f3870ab aliguori
    p = strchr(devname, '.');
2785 1f3870ab aliguori
    if (!p)
2786 1f3870ab aliguori
        return -1;
2787 1f3870ab aliguori
    bus_num = strtoul(devname, NULL, 0);
2788 1f3870ab aliguori
    addr = strtoul(p + 1, NULL, 0);
2789 1f3870ab aliguori
2790 1f3870ab aliguori
    return usb_device_del_addr(bus_num, addr);
2791 1f3870ab aliguori
}
2792 1f3870ab aliguori
2793 376253ec aliguori
void do_usb_add(Monitor *mon, const char *devname)
2794 a594cfbf bellard
{
2795 c0f4ce77 aliguori
    usb_device_add(devname, 1);
2796 a594cfbf bellard
}
2797 a594cfbf bellard
2798 376253ec aliguori
void do_usb_del(Monitor *mon, const char *devname)
2799 a594cfbf bellard
{
2800 4b096fc9 aliguori
    usb_device_del(devname);
2801 a594cfbf bellard
}
2802 a594cfbf bellard
2803 376253ec aliguori
void usb_info(Monitor *mon)
2804 a594cfbf bellard
{
2805 a594cfbf bellard
    USBDevice *dev;
2806 0d92ed30 pbrook
    USBPort *port;
2807 a594cfbf bellard
    const char *speed_str;
2808 a594cfbf bellard
2809 0d92ed30 pbrook
    if (!usb_enabled) {
2810 376253ec aliguori
        monitor_printf(mon, "USB support not enabled\n");
2811 a594cfbf bellard
        return;
2812 a594cfbf bellard
    }
2813 a594cfbf bellard
2814 0d92ed30 pbrook
    for (port = used_usb_ports; port; port = port->next) {
2815 0d92ed30 pbrook
        dev = port->dev;
2816 0d92ed30 pbrook
        if (!dev)
2817 0d92ed30 pbrook
            continue;
2818 0d92ed30 pbrook
        switch(dev->speed) {
2819 5fafdf24 ths
        case USB_SPEED_LOW:
2820 5fafdf24 ths
            speed_str = "1.5";
2821 0d92ed30 pbrook
            break;
2822 5fafdf24 ths
        case USB_SPEED_FULL:
2823 5fafdf24 ths
            speed_str = "12";
2824 0d92ed30 pbrook
            break;
2825 5fafdf24 ths
        case USB_SPEED_HIGH:
2826 5fafdf24 ths
            speed_str = "480";
2827 0d92ed30 pbrook
            break;
2828 0d92ed30 pbrook
        default:
2829 5fafdf24 ths
            speed_str = "?";
2830 0d92ed30 pbrook
            break;
2831 a594cfbf bellard
        }
2832 376253ec aliguori
        monitor_printf(mon, "  Device %d.%d, Speed %s Mb/s, Product %s\n",
2833 376253ec aliguori
                       0, dev->addr, speed_str, dev->devname);
2834 a594cfbf bellard
    }
2835 a594cfbf bellard
}
2836 a594cfbf bellard
2837 f7cce898 bellard
/***********************************************************/
2838 201a51fc balrog
/* PCMCIA/Cardbus */
2839 201a51fc balrog
2840 201a51fc balrog
static struct pcmcia_socket_entry_s {
2841 201a51fc balrog
    struct pcmcia_socket_s *socket;
2842 201a51fc balrog
    struct pcmcia_socket_entry_s *next;
2843 201a51fc balrog
} *pcmcia_sockets = 0;
2844 201a51fc balrog
2845 201a51fc balrog
void pcmcia_socket_register(struct pcmcia_socket_s *socket)
2846 201a51fc balrog
{
2847 201a51fc balrog
    struct pcmcia_socket_entry_s *entry;
2848 201a51fc balrog
2849 201a51fc balrog
    entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2850 201a51fc balrog
    entry->socket = socket;
2851 201a51fc balrog
    entry->next = pcmcia_sockets;
2852 201a51fc balrog
    pcmcia_sockets = entry;
2853 201a51fc balrog
}
2854 201a51fc balrog
2855 201a51fc balrog
void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
2856 201a51fc balrog
{
2857 201a51fc balrog
    struct pcmcia_socket_entry_s *entry, **ptr;
2858 201a51fc balrog
2859 201a51fc balrog
    ptr = &pcmcia_sockets;
2860 201a51fc balrog
    for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2861 201a51fc balrog
        if (entry->socket == socket) {
2862 201a51fc balrog
            *ptr = entry->next;
2863 201a51fc balrog
            qemu_free(entry);
2864 201a51fc balrog
        }
2865 201a51fc balrog
}
2866 201a51fc balrog
2867 376253ec aliguori
void pcmcia_info(Monitor *mon)
2868 201a51fc balrog
{
2869 201a51fc balrog
    struct pcmcia_socket_entry_s *iter;
2870 376253ec aliguori
2871 201a51fc balrog
    if (!pcmcia_sockets)
2872 376253ec aliguori
        monitor_printf(mon, "No PCMCIA sockets\n");
2873 201a51fc balrog
2874 201a51fc balrog
    for (iter = pcmcia_sockets; iter; iter = iter->next)
2875 376253ec aliguori
        monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2876 376253ec aliguori
                       iter->socket->attached ? iter->socket->card_string :
2877 376253ec aliguori
                       "Empty");
2878 201a51fc balrog
}
2879 201a51fc balrog
2880 201a51fc balrog
/***********************************************************/
2881 3023f332 aliguori
/* register display */
2882 3023f332 aliguori
2883 7b5d76da aliguori
struct DisplayAllocator default_allocator = {
2884 7b5d76da aliguori
    defaultallocator_create_displaysurface,
2885 7b5d76da aliguori
    defaultallocator_resize_displaysurface,
2886 7b5d76da aliguori
    defaultallocator_free_displaysurface
2887 7b5d76da aliguori
};
2888 7b5d76da aliguori
2889 3023f332 aliguori
void register_displaystate(DisplayState *ds)
2890 3023f332 aliguori
{
2891 3023f332 aliguori
    DisplayState **s;
2892 3023f332 aliguori
    s = &display_state;
2893 3023f332 aliguori
    while (*s != NULL)
2894 3023f332 aliguori
        s = &(*s)->next;
2895 3023f332 aliguori
    ds->next = NULL;
2896 3023f332 aliguori
    *s = ds;
2897 3023f332 aliguori
}
2898 3023f332 aliguori
2899 3023f332 aliguori
DisplayState *get_displaystate(void)
2900 3023f332 aliguori
{
2901 3023f332 aliguori
    return display_state;
2902 3023f332 aliguori
}
2903 3023f332 aliguori
2904 7b5d76da aliguori
DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2905 7b5d76da aliguori
{
2906 7b5d76da aliguori
    if(ds->allocator ==  &default_allocator) ds->allocator = da;
2907 7b5d76da aliguori
    return ds->allocator;
2908 7b5d76da aliguori
}
2909 7b5d76da aliguori
2910 2ff89790 ths
/* dumb display */
2911 2ff89790 ths
2912 8f391ab4 aliguori
static void dumb_display_init(void)
2913 2ff89790 ths
{
2914 8f391ab4 aliguori
    DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
2915 7b5d76da aliguori
    ds->allocator = &default_allocator;
2916 7b5d76da aliguori
    ds->surface = qemu_create_displaysurface(ds, 640, 480);
2917 8f391ab4 aliguori
    register_displaystate(ds);
2918 2ff89790 ths
}
2919 2ff89790 ths
2920 2ff89790 ths
/***********************************************************/
2921 8a7ddc38 bellard
/* I/O handling */
2922 0824d6fc bellard
2923 c4b1fcc0 bellard
typedef struct IOHandlerRecord {
2924 c4b1fcc0 bellard
    int fd;
2925 7c9d8e07 bellard
    IOCanRWHandler *fd_read_poll;
2926 7c9d8e07 bellard
    IOHandler *fd_read;
2927 7c9d8e07 bellard
    IOHandler *fd_write;
2928 cafffd40 ths
    int deleted;
2929 c4b1fcc0 bellard
    void *opaque;
2930 c4b1fcc0 bellard
    /* temporary data */
2931 c4b1fcc0 bellard
    struct pollfd *ufd;
2932 8a7ddc38 bellard
    struct IOHandlerRecord *next;
2933 c4b1fcc0 bellard
} IOHandlerRecord;
2934 c4b1fcc0 bellard
2935 8a7ddc38 bellard
static IOHandlerRecord *first_io_handler;
2936 c4b1fcc0 bellard
2937 7c9d8e07 bellard
/* XXX: fd_read_poll should be suppressed, but an API change is
2938 7c9d8e07 bellard
   necessary in the character devices to suppress fd_can_read(). */
2939 5fafdf24 ths
int qemu_set_fd_handler2(int fd,
2940 5fafdf24 ths
                         IOCanRWHandler *fd_read_poll,
2941 5fafdf24 ths
                         IOHandler *fd_read,
2942 5fafdf24 ths
                         IOHandler *fd_write,
2943 7c9d8e07 bellard
                         void *opaque)
2944 c4b1fcc0 bellard
{
2945 7c9d8e07 bellard
    IOHandlerRecord **pioh, *ioh;
2946 c4b1fcc0 bellard
2947 7c9d8e07 bellard
    if (!fd_read && !fd_write) {
2948 7c9d8e07 bellard
        pioh = &first_io_handler;
2949 7c9d8e07 bellard
        for(;;) {
2950 7c9d8e07 bellard
            ioh = *pioh;
2951 7c9d8e07 bellard
            if (ioh == NULL)
2952 7c9d8e07 bellard
                break;
2953 7c9d8e07 bellard
            if (ioh->fd == fd) {
2954 cafffd40 ths
                ioh->deleted = 1;
2955 7c9d8e07 bellard
                break;
2956 7c9d8e07 bellard
            }
2957 7c9d8e07 bellard
            pioh = &ioh->next;
2958 7c9d8e07 bellard
        }
2959 7c9d8e07 bellard
    } else {
2960 7c9d8e07 bellard
        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2961 7c9d8e07 bellard
            if (ioh->fd == fd)
2962 7c9d8e07 bellard
                goto found;
2963 7c9d8e07 bellard
        }
2964 7c9d8e07 bellard
        ioh = qemu_mallocz(sizeof(IOHandlerRecord));
2965 7c9d8e07 bellard
        ioh->next = first_io_handler;
2966 7c9d8e07 bellard
        first_io_handler = ioh;
2967 7c9d8e07 bellard
    found:
2968 7c9d8e07 bellard
        ioh->fd = fd;
2969 7c9d8e07 bellard
        ioh->fd_read_poll = fd_read_poll;
2970 7c9d8e07 bellard
        ioh->fd_read = fd_read;
2971 7c9d8e07 bellard
        ioh->fd_write = fd_write;
2972 7c9d8e07 bellard
        ioh->opaque = opaque;
2973 cafffd40 ths
        ioh->deleted = 0;
2974 7c9d8e07 bellard
    }
2975 c4b1fcc0 bellard
    return 0;
2976 c4b1fcc0 bellard
}
2977 c4b1fcc0 bellard
2978 5fafdf24 ths
int qemu_set_fd_handler(int fd,
2979 5fafdf24 ths
                        IOHandler *fd_read,
2980 5fafdf24 ths
                        IOHandler *fd_write,
2981 7c9d8e07 bellard
                        void *opaque)
2982 8a7ddc38 bellard
{
2983 7c9d8e07 bellard
    return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
2984 8a7ddc38 bellard
}
2985 8a7ddc38 bellard
2986 56f3a5d0 aliguori
#ifdef _WIN32
2987 8a7ddc38 bellard
/***********************************************************/
2988 f331110f bellard
/* Polling handling */
2989 f331110f bellard
2990 f331110f bellard
typedef struct PollingEntry {
2991 f331110f bellard
    PollingFunc *func;
2992 f331110f bellard
    void *opaque;
2993 f331110f bellard
    struct PollingEntry *next;
2994 f331110f bellard
} PollingEntry;
2995 f331110f bellard
2996 f331110f bellard
static PollingEntry *first_polling_entry;
2997 f331110f bellard
2998 f331110f bellard
int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2999 f331110f bellard
{
3000 f331110f bellard
    PollingEntry **ppe, *pe;
3001 f331110f bellard
    pe = qemu_mallocz(sizeof(PollingEntry));
3002 f331110f bellard
    pe->func = func;
3003 f331110f bellard
    pe->opaque = opaque;
3004 f331110f bellard
    for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
3005 f331110f bellard
    *ppe = pe;
3006 f331110f bellard
    return 0;
3007 f331110f bellard
}
3008 f331110f bellard
3009 f331110f bellard
void qemu_del_polling_cb(PollingFunc *func, void *opaque)
3010 f331110f bellard
{
3011 f331110f bellard
    PollingEntry **ppe, *pe;
3012 f331110f bellard
    for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
3013 f331110f bellard
        pe = *ppe;
3014 f331110f bellard
        if (pe->func == func && pe->opaque == opaque) {
3015 f331110f bellard
            *ppe = pe->next;
3016 f331110f bellard
            qemu_free(pe);
3017 f331110f bellard
            break;
3018 f331110f bellard
        }
3019 f331110f bellard
    }
3020 f331110f bellard
}
3021 f331110f bellard
3022 a18e524a bellard
/***********************************************************/
3023 a18e524a bellard
/* Wait objects support */
3024 a18e524a bellard
typedef struct WaitObjects {
3025 a18e524a bellard
    int num;
3026 a18e524a bellard
    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
3027 a18e524a bellard
    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
3028 a18e524a bellard
    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
3029 a18e524a bellard
} WaitObjects;
3030 a18e524a bellard
3031 a18e524a bellard
static WaitObjects wait_objects = {0};
3032 3b46e624 ths
3033 a18e524a bellard
int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3034 a18e524a bellard
{
3035 a18e524a bellard
    WaitObjects *w = &wait_objects;
3036 a18e524a bellard
3037 a18e524a bellard
    if (w->num >= MAXIMUM_WAIT_OBJECTS)
3038 a18e524a bellard
        return -1;
3039 a18e524a bellard
    w->events[w->num] = handle;
3040 a18e524a bellard
    w->func[w->num] = func;
3041 a18e524a bellard
    w->opaque[w->num] = opaque;
3042 a18e524a bellard
    w->num++;
3043 a18e524a bellard
    return 0;
3044 a18e524a bellard
}
3045 a18e524a bellard
3046 a18e524a bellard
void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3047 a18e524a bellard
{
3048 a18e524a bellard
    int i, found;
3049 a18e524a bellard
    WaitObjects *w = &wait_objects;
3050 a18e524a bellard
3051 a18e524a bellard
    found = 0;
3052 a18e524a bellard
    for (i = 0; i < w->num; i++) {
3053 a18e524a bellard
        if (w->events[i] == handle)
3054 a18e524a bellard
            found = 1;
3055 a18e524a bellard
        if (found) {
3056 a18e524a bellard
            w->events[i] = w->events[i + 1];
3057 a18e524a bellard
            w->func[i] = w->func[i + 1];
3058 a18e524a bellard
            w->opaque[i] = w->opaque[i + 1];
3059 3b46e624 ths
        }
3060 a18e524a bellard
    }
3061 a18e524a bellard
    if (found)
3062 a18e524a bellard
        w->num--;
3063 a18e524a bellard
}
3064 a18e524a bellard
#endif
3065 a18e524a bellard
3066 8a7ddc38 bellard
/***********************************************************/
3067 8a7ddc38 bellard
/* ram save/restore */
3068 8a7ddc38 bellard
3069 8a7ddc38 bellard
static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
3070 8a7ddc38 bellard
{
3071 8a7ddc38 bellard
    int v;
3072 8a7ddc38 bellard
3073 8a7ddc38 bellard
    v = qemu_get_byte(f);
3074 8a7ddc38 bellard
    switch(v) {
3075 8a7ddc38 bellard
    case 0:
3076 8a7ddc38 bellard
        if (qemu_get_buffer(f, buf, len) != len)
3077 8a7ddc38 bellard
            return -EIO;
3078 8a7ddc38 bellard
        break;
3079 8a7ddc38 bellard
    case 1:
3080 8a7ddc38 bellard
        v = qemu_get_byte(f);
3081 8a7ddc38 bellard
        memset(buf, v, len);
3082 8a7ddc38 bellard
        break;
3083 8a7ddc38 bellard
    default:
3084 8a7ddc38 bellard
        return -EINVAL;
3085 8a7ddc38 bellard
    }
3086 871d2f07 aliguori
3087 871d2f07 aliguori
    if (qemu_file_has_error(f))
3088 871d2f07 aliguori
        return -EIO;
3089 871d2f07 aliguori
3090 8a7ddc38 bellard
    return 0;
3091 8a7ddc38 bellard
}
3092 8a7ddc38 bellard
3093 c88676f8 bellard
static int ram_load_v1(QEMUFile *f, void *opaque)
3094 c88676f8 bellard
{
3095 00f82b8a aurel32
    int ret;
3096 00f82b8a aurel32
    ram_addr_t i;
3097 c88676f8 bellard
3098 94a6b54f pbrook
    if (qemu_get_be32(f) != last_ram_offset)
3099 c88676f8 bellard
        return -EINVAL;
3100 94a6b54f pbrook
    for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
3101 5579c7f3 pbrook
        ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
3102 c88676f8 bellard
        if (ret)
3103 c88676f8 bellard
            return ret;
3104 c88676f8 bellard
    }
3105 c88676f8 bellard
    return 0;
3106 c88676f8 bellard
}
3107 c88676f8 bellard
3108 c88676f8 bellard
#define BDRV_HASH_BLOCK_SIZE 1024
3109 c88676f8 bellard
#define IOBUF_SIZE 4096
3110 c88676f8 bellard
#define RAM_CBLOCK_MAGIC 0xfabe
3111 c88676f8 bellard
3112 c88676f8 bellard
typedef struct RamDecompressState {
3113 c88676f8 bellard
    z_stream zstream;
3114 c88676f8 bellard
    QEMUFile *f;
3115 c88676f8 bellard
    uint8_t buf[IOBUF_SIZE];
3116 c88676f8 bellard
} RamDecompressState;
3117 c88676f8 bellard
3118 c88676f8 bellard
static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
3119 c88676f8 bellard
{
3120 c88676f8 bellard
    int ret;
3121 c88676f8 bellard
    memset(s, 0, sizeof(*s));
3122 c88676f8 bellard
    s->f = f;
3123 c88676f8 bellard
    ret = inflateInit(&s->zstream);
3124 c88676f8 bellard
    if (ret != Z_OK)
3125 c88676f8 bellard
        return -1;
3126 c88676f8 bellard
    return 0;
3127 c88676f8 bellard
}
3128 c88676f8 bellard
3129 c88676f8 bellard
static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
3130 c88676f8 bellard
{
3131 c88676f8 bellard
    int ret, clen;
3132 c88676f8 bellard
3133 c88676f8 bellard
    s->zstream.avail_out = len;
3134 c88676f8 bellard
    s->zstream.next_out = buf;
3135 c88676f8 bellard
    while (s->zstream.avail_out > 0) {
3136 c88676f8 bellard
        if (s->zstream.avail_in == 0) {
3137 c88676f8 bellard
            if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3138 c88676f8 bellard
                return -1;
3139 c88676f8 bellard
            clen = qemu_get_be16(s->f);
3140 c88676f8 bellard
            if (clen > IOBUF_SIZE)
3141 c88676f8 bellard
                return -1;
3142 c88676f8 bellard
            qemu_get_buffer(s->f, s->buf, clen);
3143 c88676f8 bellard
            s->zstream.avail_in = clen;
3144 c88676f8 bellard
            s->zstream.next_in = s->buf;
3145 c88676f8 bellard
        }
3146 c88676f8 bellard
        ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3147 c88676f8 bellard
        if (ret != Z_OK && ret != Z_STREAM_END) {
3148 c88676f8 bellard
            return -1;
3149 c88676f8 bellard
        }
3150 c88676f8 bellard
    }
3151 c88676f8 bellard
    return 0;
3152 c88676f8 bellard
}
3153 c88676f8 bellard
3154 c88676f8 bellard
static void ram_decompress_close(RamDecompressState *s)
3155 c88676f8 bellard
{
3156 c88676f8 bellard
    inflateEnd(&s->zstream);
3157 c88676f8 bellard
}
3158 c88676f8 bellard
3159 475e4277 aliguori
#define RAM_SAVE_FLAG_FULL        0x01
3160 475e4277 aliguori
#define RAM_SAVE_FLAG_COMPRESS        0x02
3161 475e4277 aliguori
#define RAM_SAVE_FLAG_MEM_SIZE        0x04
3162 475e4277 aliguori
#define RAM_SAVE_FLAG_PAGE        0x08
3163 475e4277 aliguori
#define RAM_SAVE_FLAG_EOS        0x10
3164 475e4277 aliguori
3165 475e4277 aliguori
static int is_dup_page(uint8_t *page, uint8_t ch)
3166 8a7ddc38 bellard
{
3167 475e4277 aliguori
    uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3168 475e4277 aliguori
    uint32_t *array = (uint32_t *)page;
3169 475e4277 aliguori
    int i;
3170 3b46e624 ths
3171 475e4277 aliguori
    for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3172 475e4277 aliguori
        if (array[i] != val)
3173 475e4277 aliguori
            return 0;
3174 475e4277 aliguori
    }
3175 475e4277 aliguori
3176 475e4277 aliguori
    return 1;
3177 475e4277 aliguori
}
3178 475e4277 aliguori
3179 475e4277 aliguori
static int ram_save_block(QEMUFile *f)
3180 475e4277 aliguori
{
3181 475e4277 aliguori
    static ram_addr_t current_addr = 0;
3182 475e4277 aliguori
    ram_addr_t saved_addr = current_addr;
3183 475e4277 aliguori
    ram_addr_t addr = 0;
3184 475e4277 aliguori
    int found = 0;
3185 475e4277 aliguori
3186 94a6b54f pbrook
    while (addr < last_ram_offset) {
3187 475e4277 aliguori
        if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
3188 5579c7f3 pbrook
            uint8_t *p;
3189 475e4277 aliguori
3190 475e4277 aliguori
            cpu_physical_memory_reset_dirty(current_addr,
3191 475e4277 aliguori
                                            current_addr + TARGET_PAGE_SIZE,
3192 475e4277 aliguori
                                            MIGRATION_DIRTY_FLAG);
3193 475e4277 aliguori
3194 5579c7f3 pbrook
            p = qemu_get_ram_ptr(current_addr);
3195 475e4277 aliguori
3196 5579c7f3 pbrook
            if (is_dup_page(p, *p)) {
3197 475e4277 aliguori
                qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
3198 5579c7f3 pbrook
                qemu_put_byte(f, *p);
3199 475e4277 aliguori
            } else {
3200 475e4277 aliguori
                qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
3201 5579c7f3 pbrook
                qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
3202 c88676f8 bellard
            }
3203 475e4277 aliguori
3204 475e4277 aliguori
            found = 1;
3205 475e4277 aliguori
            break;
3206 c88676f8 bellard
        }
3207 475e4277 aliguori
        addr += TARGET_PAGE_SIZE;
3208 94a6b54f pbrook
        current_addr = (saved_addr + addr) % last_ram_offset;
3209 8a7ddc38 bellard
    }
3210 475e4277 aliguori
3211 475e4277 aliguori
    return found;
3212 8a7ddc38 bellard
}
3213 8a7ddc38 bellard
3214 475e4277 aliguori
static ram_addr_t ram_save_threshold = 10;
3215 475e4277 aliguori
3216 475e4277 aliguori
static ram_addr_t ram_save_remaining(void)
3217 475e4277 aliguori
{
3218 475e4277 aliguori
    ram_addr_t addr;
3219 475e4277 aliguori
    ram_addr_t count = 0;
3220 475e4277 aliguori
3221 94a6b54f pbrook
    for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
3222 475e4277 aliguori
        if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3223 475e4277 aliguori
            count++;
3224 475e4277 aliguori
    }
3225 475e4277 aliguori
3226 475e4277 aliguori
    return count;
3227 475e4277 aliguori
}
3228 475e4277 aliguori
3229 475e4277 aliguori
static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3230 475e4277 aliguori
{
3231 475e4277 aliguori
    ram_addr_t addr;
3232 475e4277 aliguori
3233 475e4277 aliguori
    if (stage == 1) {
3234 475e4277 aliguori
        /* Make sure all dirty bits are set */
3235 94a6b54f pbrook
        for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
3236 475e4277 aliguori
            if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3237 475e4277 aliguori
                cpu_physical_memory_set_dirty(addr);
3238 475e4277 aliguori
        }
3239 475e4277 aliguori
        
3240 475e4277 aliguori
        /* Enable dirty memory tracking */
3241 475e4277 aliguori
        cpu_physical_memory_set_dirty_tracking(1);
3242 475e4277 aliguori
3243 94a6b54f pbrook
        qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
3244 475e4277 aliguori
    }
3245 475e4277 aliguori
3246 475e4277 aliguori
    while (!qemu_file_rate_limit(f)) {
3247 475e4277 aliguori
        int ret;
3248 475e4277 aliguori
3249 475e4277 aliguori
        ret = ram_save_block(f);
3250 475e4277 aliguori
        if (ret == 0) /* no more blocks */
3251 475e4277 aliguori
            break;
3252 475e4277 aliguori
    }
3253 475e4277 aliguori
3254 475e4277 aliguori
    /* try transferring iterative blocks of memory */
3255 475e4277 aliguori
3256 475e4277 aliguori
    if (stage == 3) {
3257 475e4277 aliguori
3258 475e4277 aliguori
        /* flush all remaining blocks regardless of rate limiting */
3259 475e4277 aliguori
        while (ram_save_block(f) != 0);
3260 8215e914 aliguori
        cpu_physical_memory_set_dirty_tracking(0);
3261 475e4277 aliguori
    }
3262 475e4277 aliguori
3263 475e4277 aliguori
    qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3264 475e4277 aliguori
3265 475e4277 aliguori
    return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
3266 475e4277 aliguori
}
3267 475e4277 aliguori
3268 475e4277 aliguori
static int ram_load_dead(QEMUFile *f, void *opaque)
3269 8a7ddc38 bellard
{
3270 c88676f8 bellard
    RamDecompressState s1, *s = &s1;
3271 c88676f8 bellard
    uint8_t buf[10];
3272 00f82b8a aurel32
    ram_addr_t i;
3273 8a7ddc38 bellard
3274 c88676f8 bellard
    if (ram_decompress_open(s, f) < 0)
3275 c88676f8 bellard
        return -EINVAL;
3276 94a6b54f pbrook
    for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
3277 c88676f8 bellard
        if (ram_decompress_buf(s, buf, 1) < 0) {
3278 c88676f8 bellard
            fprintf(stderr, "Error while reading ram block header\n");
3279 c88676f8 bellard
            goto error;
3280 c88676f8 bellard
        }
3281 c88676f8 bellard
        if (buf[0] == 0) {
3282 5579c7f3 pbrook
            if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
3283 5579c7f3 pbrook
                                   BDRV_HASH_BLOCK_SIZE) < 0) {
3284 00f82b8a aurel32
                fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
3285 c88676f8 bellard
                goto error;
3286 c88676f8 bellard
            }
3287 475e4277 aliguori
        } else {
3288 c88676f8 bellard
        error:
3289 c88676f8 bellard
            printf("Error block header\n");
3290 c88676f8 bellard
            return -EINVAL;
3291 c88676f8 bellard
        }
3292 8a7ddc38 bellard
    }
3293 c88676f8 bellard
    ram_decompress_close(s);
3294 475e4277 aliguori
3295 475e4277 aliguori
    return 0;
3296 475e4277 aliguori
}
3297 475e4277 aliguori
3298 475e4277 aliguori
static int ram_load(QEMUFile *f, void *opaque, int version_id)
3299 475e4277 aliguori
{
3300 475e4277 aliguori
    ram_addr_t addr;
3301 475e4277 aliguori
    int flags;
3302 475e4277 aliguori
3303 475e4277 aliguori
    if (version_id == 1)
3304 475e4277 aliguori
        return ram_load_v1(f, opaque);
3305 475e4277 aliguori
3306 475e4277 aliguori
    if (version_id == 2) {
3307 94a6b54f pbrook
        if (qemu_get_be32(f) != last_ram_offset)
3308 475e4277 aliguori
            return -EINVAL;
3309 475e4277 aliguori
        return ram_load_dead(f, opaque);
3310 475e4277 aliguori
    }
3311 475e4277 aliguori
3312 475e4277 aliguori
    if (version_id != 3)
3313 475e4277 aliguori
        return -EINVAL;
3314 475e4277 aliguori
3315 475e4277 aliguori
    do {
3316 475e4277 aliguori
        addr = qemu_get_be64(f);
3317 475e4277 aliguori
3318 475e4277 aliguori
        flags = addr & ~TARGET_PAGE_MASK;
3319 475e4277 aliguori
        addr &= TARGET_PAGE_MASK;
3320 475e4277 aliguori
3321 475e4277 aliguori
        if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
3322 94a6b54f pbrook
            if (addr != last_ram_offset)
3323 475e4277 aliguori
                return -EINVAL;
3324 475e4277 aliguori
        }
3325 475e4277 aliguori
3326 475e4277 aliguori
        if (flags & RAM_SAVE_FLAG_FULL) {
3327 475e4277 aliguori
            if (ram_load_dead(f, opaque) < 0)
3328 475e4277 aliguori
                return -EINVAL;
3329 475e4277 aliguori
        }
3330 475e4277 aliguori
        
3331 475e4277 aliguori
        if (flags & RAM_SAVE_FLAG_COMPRESS) {
3332 475e4277 aliguori
            uint8_t ch = qemu_get_byte(f);
3333 5579c7f3 pbrook
            memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
3334 475e4277 aliguori
        } else if (flags & RAM_SAVE_FLAG_PAGE)
3335 5579c7f3 pbrook
            qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
3336 475e4277 aliguori
    } while (!(flags & RAM_SAVE_FLAG_EOS));
3337 475e4277 aliguori
3338 8a7ddc38 bellard
    return 0;
3339 8a7ddc38 bellard
}
3340 8a7ddc38 bellard
3341 9e472e10 aliguori
void qemu_service_io(void)
3342 9e472e10 aliguori
{
3343 9e472e10 aliguori
    CPUState *env = cpu_single_env;
3344 9e472e10 aliguori
    if (env) {
3345 3098dba0 aurel32
        cpu_exit(env);
3346 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
3347 9e472e10 aliguori
        if (env->kqemu_enabled) {
3348 9e472e10 aliguori
            kqemu_cpu_interrupt(env);
3349 9e472e10 aliguori
        }
3350 9e472e10 aliguori
#endif
3351 9e472e10 aliguori
    }
3352 9e472e10 aliguori
}
3353 9e472e10 aliguori
3354 8a7ddc38 bellard
/***********************************************************/
3355 83f64091 bellard
/* bottom halves (can be seen as timers which expire ASAP) */
3356 83f64091 bellard
3357 83f64091 bellard
struct QEMUBH {
3358 83f64091 bellard
    QEMUBHFunc *cb;
3359 83f64091 bellard
    void *opaque;
3360 83f64091 bellard
    int scheduled;
3361 1b435b10 aliguori
    int idle;
3362 1b435b10 aliguori
    int deleted;
3363 83f64091 bellard
    QEMUBH *next;
3364 83f64091 bellard
};
3365 83f64091 bellard
3366 83f64091 bellard
static QEMUBH *first_bh = NULL;
3367 83f64091 bellard
3368 83f64091 bellard
QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3369 83f64091 bellard
{
3370 83f64091 bellard
    QEMUBH *bh;
3371 83f64091 bellard
    bh = qemu_mallocz(sizeof(QEMUBH));
3372 83f64091 bellard
    bh->cb = cb;
3373 83f64091 bellard
    bh->opaque = opaque;
3374 1b435b10 aliguori
    bh->next = first_bh;
3375 1b435b10 aliguori
    first_bh = bh;
3376 83f64091 bellard
    return bh;
3377 83f64091 bellard
}
3378 83f64091 bellard
3379 6eb5733a bellard
int qemu_bh_poll(void)
3380 83f64091 bellard
{
3381 1b435b10 aliguori
    QEMUBH *bh, **bhp;
3382 6eb5733a bellard
    int ret;
3383 83f64091 bellard
3384 6eb5733a bellard
    ret = 0;
3385 1b435b10 aliguori
    for (bh = first_bh; bh; bh = bh->next) {
3386 1b435b10 aliguori
        if (!bh->deleted && bh->scheduled) {
3387 1b435b10 aliguori
            bh->scheduled = 0;
3388 1b435b10 aliguori
            if (!bh->idle)
3389 1b435b10 aliguori
                ret = 1;
3390 1b435b10 aliguori
            bh->idle = 0;
3391 1b435b10 aliguori
            bh->cb(bh->opaque);
3392 1b435b10 aliguori
        }
3393 83f64091 bellard
    }
3394 1b435b10 aliguori
3395 1b435b10 aliguori
    /* remove deleted bhs */
3396 1b435b10 aliguori
    bhp = &first_bh;
3397 1b435b10 aliguori
    while (*bhp) {
3398 1b435b10 aliguori
        bh = *bhp;
3399 1b435b10 aliguori
        if (bh->deleted) {
3400 1b435b10 aliguori
            *bhp = bh->next;
3401 1b435b10 aliguori
            qemu_free(bh);
3402 1b435b10 aliguori
        } else
3403 1b435b10 aliguori
            bhp = &bh->next;
3404 1b435b10 aliguori
    }
3405 1b435b10 aliguori
3406 6eb5733a bellard
    return ret;
3407 83f64091 bellard
}
3408 83f64091 bellard
3409 1b435b10 aliguori
void qemu_bh_schedule_idle(QEMUBH *bh)
3410 1b435b10 aliguori
{
3411 1b435b10 aliguori
    if (bh->scheduled)
3412 1b435b10 aliguori
        return;
3413 1b435b10 aliguori
    bh->scheduled = 1;
3414 1b435b10 aliguori
    bh->idle = 1;
3415 1b435b10 aliguori
}
3416 1b435b10 aliguori
3417 83f64091 bellard
void qemu_bh_schedule(QEMUBH *bh)
3418 83f64091 bellard
{
3419 83f64091 bellard
    CPUState *env = cpu_single_env;
3420 83f64091 bellard
    if (bh->scheduled)
3421 83f64091 bellard
        return;
3422 83f64091 bellard
    bh->scheduled = 1;
3423 1b435b10 aliguori
    bh->idle = 0;
3424 83f64091 bellard
    /* stop the currently executing CPU to execute the BH ASAP */
3425 83f64091 bellard
    if (env) {
3426 3098dba0 aurel32
        cpu_exit(env);
3427 83f64091 bellard
    }
3428 83f64091 bellard
}
3429 83f64091 bellard
3430 83f64091 bellard
void qemu_bh_cancel(QEMUBH *bh)
3431 83f64091 bellard
{
3432 1b435b10 aliguori
    bh->scheduled = 0;
3433 83f64091 bellard
}
3434 83f64091 bellard
3435 83f64091 bellard
void qemu_bh_delete(QEMUBH *bh)
3436 83f64091 bellard
{
3437 1b435b10 aliguori
    bh->scheduled = 0;
3438 1b435b10 aliguori
    bh->deleted = 1;
3439 83f64091 bellard
}
3440 83f64091 bellard
3441 56f3a5d0 aliguori
static void qemu_bh_update_timeout(int *timeout)
3442 56f3a5d0 aliguori
{
3443 56f3a5d0 aliguori
    QEMUBH *bh;
3444 56f3a5d0 aliguori
3445 56f3a5d0 aliguori
    for (bh = first_bh; bh; bh = bh->next) {
3446 56f3a5d0 aliguori
        if (!bh->deleted && bh->scheduled) {
3447 56f3a5d0 aliguori
            if (bh->idle) {
3448 56f3a5d0 aliguori
                /* idle bottom halves will be polled at least
3449 56f3a5d0 aliguori
                 * every 10ms */
3450 56f3a5d0 aliguori
                *timeout = MIN(10, *timeout);
3451 56f3a5d0 aliguori
            } else {
3452 56f3a5d0 aliguori
                /* non-idle bottom halves will be executed
3453 56f3a5d0 aliguori
                 * immediately */
3454 56f3a5d0 aliguori
                *timeout = 0;
3455 56f3a5d0 aliguori
                break;
3456 56f3a5d0 aliguori
            }
3457 56f3a5d0 aliguori
        }
3458 56f3a5d0 aliguori
    }
3459 56f3a5d0 aliguori
}
3460 56f3a5d0 aliguori
3461 83f64091 bellard
/***********************************************************/
3462 cc1daa40 bellard
/* machine registration */
3463 cc1daa40 bellard
3464 bdaf78e0 blueswir1
static QEMUMachine *first_machine = NULL;
3465 6f338c34 aliguori
QEMUMachine *current_machine = NULL;
3466 cc1daa40 bellard
3467 cc1daa40 bellard
int qemu_register_machine(QEMUMachine *m)
3468 cc1daa40 bellard
{
3469 cc1daa40 bellard
    QEMUMachine **pm;
3470 cc1daa40 bellard
    pm = &first_machine;
3471 cc1daa40 bellard
    while (*pm != NULL)
3472 cc1daa40 bellard
        pm = &(*pm)->next;
3473 cc1daa40 bellard
    m->next = NULL;
3474 cc1daa40 bellard
    *pm = m;
3475 cc1daa40 bellard
    return 0;
3476 cc1daa40 bellard
}
3477 cc1daa40 bellard
3478 9596ebb7 pbrook
static QEMUMachine *find_machine(const char *name)
3479 cc1daa40 bellard
{
3480 cc1daa40 bellard
    QEMUMachine *m;
3481 cc1daa40 bellard
3482 cc1daa40 bellard
    for(m = first_machine; m != NULL; m = m->next) {
3483 cc1daa40 bellard
        if (!strcmp(m->name, name))
3484 cc1daa40 bellard
            return m;
3485 cc1daa40 bellard
    }
3486 cc1daa40 bellard
    return NULL;
3487 cc1daa40 bellard
}
3488 cc1daa40 bellard
3489 cc1daa40 bellard
/***********************************************************/
3490 8a7ddc38 bellard
/* main execution loop */
3491 8a7ddc38 bellard
3492 9596ebb7 pbrook
static void gui_update(void *opaque)
3493 8a7ddc38 bellard
{
3494 7d957bd8 aliguori
    uint64_t interval = GUI_REFRESH_INTERVAL;
3495 740733bb ths
    DisplayState *ds = opaque;
3496 7d957bd8 aliguori
    DisplayChangeListener *dcl = ds->listeners;
3497 7d957bd8 aliguori
3498 7d957bd8 aliguori
    dpy_refresh(ds);
3499 7d957bd8 aliguori
3500 7d957bd8 aliguori
    while (dcl != NULL) {
3501 7d957bd8 aliguori
        if (dcl->gui_timer_interval &&
3502 7d957bd8 aliguori
            dcl->gui_timer_interval < interval)
3503 7d957bd8 aliguori
            interval = dcl->gui_timer_interval;
3504 7d957bd8 aliguori
        dcl = dcl->next;
3505 7d957bd8 aliguori
    }
3506 7d957bd8 aliguori
    qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
3507 8a7ddc38 bellard
}
3508 8a7ddc38 bellard
3509 9043b62d blueswir1
static void nographic_update(void *opaque)
3510 9043b62d blueswir1
{
3511 9043b62d blueswir1
    uint64_t interval = GUI_REFRESH_INTERVAL;
3512 9043b62d blueswir1
3513 9043b62d blueswir1
    qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3514 9043b62d blueswir1
}
3515 9043b62d blueswir1
3516 0bd48850 bellard
struct vm_change_state_entry {
3517 0bd48850 bellard
    VMChangeStateHandler *cb;
3518 0bd48850 bellard
    void *opaque;
3519 0bd48850 bellard
    LIST_ENTRY (vm_change_state_entry) entries;
3520 0bd48850 bellard
};
3521 0bd48850 bellard
3522 0bd48850 bellard
static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3523 0bd48850 bellard
3524 0bd48850 bellard
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3525 0bd48850 bellard
                                                     void *opaque)
3526 0bd48850 bellard
{
3527 0bd48850 bellard
    VMChangeStateEntry *e;
3528 0bd48850 bellard
3529 0bd48850 bellard
    e = qemu_mallocz(sizeof (*e));
3530 0bd48850 bellard
3531 0bd48850 bellard
    e->cb = cb;
3532 0bd48850 bellard
    e->opaque = opaque;
3533 0bd48850 bellard
    LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3534 0bd48850 bellard
    return e;
3535 0bd48850 bellard
}
3536 0bd48850 bellard
3537 0bd48850 bellard
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3538 0bd48850 bellard
{
3539 0bd48850 bellard
    LIST_REMOVE (e, entries);
3540 0bd48850 bellard
    qemu_free (e);
3541 0bd48850 bellard
}
3542 0bd48850 bellard
3543 9781e040 aliguori
static void vm_state_notify(int running, int reason)
3544 0bd48850 bellard
{
3545 0bd48850 bellard
    VMChangeStateEntry *e;
3546 0bd48850 bellard
3547 0bd48850 bellard
    for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
3548 9781e040 aliguori
        e->cb(e->opaque, running, reason);
3549 0bd48850 bellard
    }
3550 0bd48850 bellard
}
3551 0bd48850 bellard
3552 8a7ddc38 bellard
void vm_start(void)
3553 8a7ddc38 bellard
{
3554 8a7ddc38 bellard
    if (!vm_running) {
3555 8a7ddc38 bellard
        cpu_enable_ticks();
3556 8a7ddc38 bellard
        vm_running = 1;
3557 9781e040 aliguori
        vm_state_notify(1, 0);
3558 efe75411 ths
        qemu_rearm_alarm_timer(alarm_timer);
3559 8a7ddc38 bellard
    }
3560 8a7ddc38 bellard
}
3561 8a7ddc38 bellard
3562 5fafdf24 ths
void vm_stop(int reason)
3563 8a7ddc38 bellard
{
3564 8a7ddc38 bellard
    if (vm_running) {
3565 8a7ddc38 bellard
        cpu_disable_ticks();
3566 8a7ddc38 bellard
        vm_running = 0;
3567 9781e040 aliguori
        vm_state_notify(0, reason);
3568 8a7ddc38 bellard
    }
3569 8a7ddc38 bellard
}
3570 8a7ddc38 bellard
3571 bb0c6722 bellard
/* reset/shutdown handler */
3572 bb0c6722 bellard
3573 bb0c6722 bellard
typedef struct QEMUResetEntry {
3574 bb0c6722 bellard
    QEMUResetHandler *func;
3575 bb0c6722 bellard
    void *opaque;
3576 bb0c6722 bellard
    struct QEMUResetEntry *next;
3577 bb0c6722 bellard
} QEMUResetEntry;
3578 bb0c6722 bellard
3579 bb0c6722 bellard
static QEMUResetEntry *first_reset_entry;
3580 bb0c6722 bellard
static int reset_requested;
3581 bb0c6722 bellard
static int shutdown_requested;
3582 3475187d bellard
static int powerdown_requested;
3583 bb0c6722 bellard
3584 cf7a2fe2 aurel32
int qemu_shutdown_requested(void)
3585 cf7a2fe2 aurel32
{
3586 cf7a2fe2 aurel32
    int r = shutdown_requested;
3587 cf7a2fe2 aurel32
    shutdown_requested = 0;
3588 cf7a2fe2 aurel32
    return r;
3589 cf7a2fe2 aurel32
}
3590 cf7a2fe2 aurel32
3591 cf7a2fe2 aurel32
int qemu_reset_requested(void)
3592 cf7a2fe2 aurel32
{
3593 cf7a2fe2 aurel32
    int r = reset_requested;
3594 cf7a2fe2 aurel32
    reset_requested = 0;
3595 cf7a2fe2 aurel32
    return r;
3596 cf7a2fe2 aurel32
}
3597 cf7a2fe2 aurel32
3598 cf7a2fe2 aurel32
int qemu_powerdown_requested(void)
3599 cf7a2fe2 aurel32
{
3600 cf7a2fe2 aurel32
    int r = powerdown_requested;
3601 cf7a2fe2 aurel32
    powerdown_requested = 0;
3602 cf7a2fe2 aurel32
    return r;
3603 cf7a2fe2 aurel32
}
3604 cf7a2fe2 aurel32
3605 bb0c6722 bellard
void qemu_register_reset(QEMUResetHandler *func, void *opaque)
3606 bb0c6722 bellard
{
3607 bb0c6722 bellard
    QEMUResetEntry **pre, *re;
3608 bb0c6722 bellard
3609 bb0c6722 bellard
    pre = &first_reset_entry;
3610 bb0c6722 bellard
    while (*pre != NULL)
3611 bb0c6722 bellard
        pre = &(*pre)->next;
3612 bb0c6722 bellard
    re = qemu_mallocz(sizeof(QEMUResetEntry));
3613 bb0c6722 bellard
    re->func = func;
3614 bb0c6722 bellard
    re->opaque = opaque;
3615 bb0c6722 bellard
    re->next = NULL;
3616 bb0c6722 bellard
    *pre = re;
3617 bb0c6722 bellard
}
3618 bb0c6722 bellard
3619 cf7a2fe2 aurel32
void qemu_system_reset(void)
3620 bb0c6722 bellard
{
3621 bb0c6722 bellard
    QEMUResetEntry *re;
3622 bb0c6722 bellard
3623 bb0c6722 bellard
    /* reset all devices */
3624 bb0c6722 bellard
    for(re = first_reset_entry; re != NULL; re = re->next) {
3625 bb0c6722 bellard
        re->func(re->opaque);
3626 bb0c6722 bellard
    }
3627 29203dcf aliguori
    if (kvm_enabled())
3628 29203dcf aliguori
        kvm_sync_vcpus();
3629 bb0c6722 bellard
}
3630 bb0c6722 bellard
3631 bb0c6722 bellard
void qemu_system_reset_request(void)
3632 bb0c6722 bellard
{
3633 d1beab82 bellard
    if (no_reboot) {
3634 d1beab82 bellard
        shutdown_requested = 1;
3635 d1beab82 bellard
    } else {
3636 d1beab82 bellard
        reset_requested = 1;
3637 d1beab82 bellard
    }
3638 6a00d601 bellard
    if (cpu_single_env)
3639 3098dba0 aurel32
        cpu_exit(cpu_single_env);
3640 bb0c6722 bellard
}
3641 bb0c6722 bellard
3642 bb0c6722 bellard
void qemu_system_shutdown_request(void)
3643 bb0c6722 bellard
{
3644 bb0c6722 bellard
    shutdown_requested = 1;
3645 6a00d601 bellard
    if (cpu_single_env)
3646 3098dba0 aurel32
        cpu_exit(cpu_single_env);
3647 bb0c6722 bellard
}
3648 bb0c6722 bellard
3649 3475187d bellard
void qemu_system_powerdown_request(void)
3650 3475187d bellard
{
3651 3475187d bellard
    powerdown_requested = 1;
3652 6a00d601 bellard
    if (cpu_single_env)
3653 3098dba0 aurel32
        cpu_exit(cpu_single_env);
3654 bb0c6722 bellard
}
3655 bb0c6722 bellard
3656 877cf882 ths
#ifdef _WIN32
3657 69d6451c blueswir1
static void host_main_loop_wait(int *timeout)
3658 56f3a5d0 aliguori
{
3659 56f3a5d0 aliguori
    int ret, ret2, i;
3660 f331110f bellard
    PollingEntry *pe;
3661 f331110f bellard
3662 c4b1fcc0 bellard
3663 f331110f bellard
    /* XXX: need to suppress polling by better using win32 events */
3664 f331110f bellard
    ret = 0;
3665 f331110f bellard
    for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3666 f331110f bellard
        ret |= pe->func(pe->opaque);
3667 f331110f bellard
    }
3668 e6b1e558 ths
    if (ret == 0) {
3669 a18e524a bellard
        int err;
3670 a18e524a bellard
        WaitObjects *w = &wait_objects;
3671 3b46e624 ths
3672 56f3a5d0 aliguori
        ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
3673 a18e524a bellard
        if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3674 a18e524a bellard
            if (w->func[ret - WAIT_OBJECT_0])
3675 a18e524a bellard
                w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
3676 3b46e624 ths
3677 5fafdf24 ths
            /* Check for additional signaled events */
3678 e6b1e558 ths
            for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
3679 3b46e624 ths
3680 e6b1e558 ths
                /* Check if event is signaled */
3681 e6b1e558 ths
                ret2 = WaitForSingleObject(w->events[i], 0);
3682 e6b1e558 ths
                if(ret2 == WAIT_OBJECT_0) {
3683 e6b1e558 ths
                    if (w->func[i])
3684 e6b1e558 ths
                        w->func[i](w->opaque[i]);
3685 e6b1e558 ths
                } else if (ret2 == WAIT_TIMEOUT) {
3686 e6b1e558 ths
                } else {
3687 e6b1e558 ths
                    err = GetLastError();
3688 e6b1e558 ths
                    fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
3689 3b46e624 ths
                }
3690 3b46e624 ths
            }
3691 a18e524a bellard
        } else if (ret == WAIT_TIMEOUT) {
3692 a18e524a bellard
        } else {
3693 a18e524a bellard
            err = GetLastError();
3694 e6b1e558 ths
            fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
3695 a18e524a bellard
        }
3696 f331110f bellard
    }
3697 56f3a5d0 aliguori
3698 56f3a5d0 aliguori
    *timeout = 0;
3699 56f3a5d0 aliguori
}
3700 56f3a5d0 aliguori
#else
3701 69d6451c blueswir1
static void host_main_loop_wait(int *timeout)
3702 56f3a5d0 aliguori
{
3703 56f3a5d0 aliguori
}
3704 fd1dff4b bellard
#endif
3705 56f3a5d0 aliguori
3706 56f3a5d0 aliguori
void main_loop_wait(int timeout)
3707 56f3a5d0 aliguori
{
3708 56f3a5d0 aliguori
    IOHandlerRecord *ioh;
3709 56f3a5d0 aliguori
    fd_set rfds, wfds, xfds;
3710 56f3a5d0 aliguori
    int ret, nfds;
3711 56f3a5d0 aliguori
    struct timeval tv;
3712 56f3a5d0 aliguori
3713 56f3a5d0 aliguori
    qemu_bh_update_timeout(&timeout);
3714 56f3a5d0 aliguori
3715 56f3a5d0 aliguori
    host_main_loop_wait(&timeout);
3716 56f3a5d0 aliguori
3717 fd1dff4b bellard
    /* poll any events */
3718 fd1dff4b bellard
    /* XXX: separate device handlers from system ones */
3719 6abfbd79 aliguori
    nfds = -1;
3720 fd1dff4b bellard
    FD_ZERO(&rfds);
3721 fd1dff4b bellard
    FD_ZERO(&wfds);
3722 e035649e bellard
    FD_ZERO(&xfds);
3723 fd1dff4b bellard
    for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
3724 cafffd40 ths
        if (ioh->deleted)
3725 cafffd40 ths
            continue;
3726 fd1dff4b bellard
        if (ioh->fd_read &&
3727 fd1dff4b bellard
            (!ioh->fd_read_poll ||
3728 fd1dff4b bellard
             ioh->fd_read_poll(ioh->opaque) != 0)) {
3729 fd1dff4b bellard
            FD_SET(ioh->fd, &rfds);
3730 fd1dff4b bellard
            if (ioh->fd > nfds)
3731 fd1dff4b bellard
                nfds = ioh->fd;
3732 fd1dff4b bellard
        }
3733 fd1dff4b bellard
        if (ioh->fd_write) {
3734 fd1dff4b bellard
            FD_SET(ioh->fd, &wfds);
3735 fd1dff4b bellard
            if (ioh->fd > nfds)
3736 fd1dff4b bellard
                nfds = ioh->fd;
3737 fd1dff4b bellard
        }
3738 fd1dff4b bellard
    }
3739 3b46e624 ths
3740 56f3a5d0 aliguori
    tv.tv_sec = timeout / 1000;
3741 56f3a5d0 aliguori
    tv.tv_usec = (timeout % 1000) * 1000;
3742 56f3a5d0 aliguori
3743 e035649e bellard
#if defined(CONFIG_SLIRP)
3744 63a01ef8 aliguori
    if (slirp_is_inited()) {
3745 e035649e bellard
        slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3746 e035649e bellard
    }
3747 e035649e bellard
#endif
3748 e035649e bellard
    ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
3749 fd1dff4b bellard
    if (ret > 0) {
3750 cafffd40 ths
        IOHandlerRecord **pioh;
3751 cafffd40 ths
3752 cafffd40 ths
        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
3753 6ab43fdc ths
            if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
3754 fd1dff4b bellard
                ioh->fd_read(ioh->opaque);
3755 7c9d8e07 bellard
            }
3756 6ab43fdc ths
            if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
3757 fd1dff4b bellard
                ioh->fd_write(ioh->opaque);
3758 c4b1fcc0 bellard
            }
3759 b4608c04 bellard
        }
3760 cafffd40 ths
3761 cafffd40 ths
        /* remove deleted IO handlers */
3762 cafffd40 ths
        pioh = &first_io_handler;
3763 cafffd40 ths
        while (*pioh) {
3764 cafffd40 ths
            ioh = *pioh;
3765 cafffd40 ths
            if (ioh->deleted) {
3766 cafffd40 ths
                *pioh = ioh->next;
3767 cafffd40 ths
                qemu_free(ioh);
3768 5fafdf24 ths
            } else
3769 cafffd40 ths
                pioh = &ioh->next;
3770 cafffd40 ths
        }
3771 fd1dff4b bellard
    }
3772 c20709aa bellard
#if defined(CONFIG_SLIRP)
3773 63a01ef8 aliguori
    if (slirp_is_inited()) {
3774 e035649e bellard
        if (ret < 0) {
3775 e035649e bellard
            FD_ZERO(&rfds);
3776 e035649e bellard
            FD_ZERO(&wfds);
3777 e035649e bellard
            FD_ZERO(&xfds);
3778 c20709aa bellard
        }
3779 e035649e bellard
        slirp_select_poll(&rfds, &wfds, &xfds);
3780 fd1dff4b bellard
    }
3781 c20709aa bellard
#endif
3782 c20709aa bellard
3783 357c692c aliguori
    /* vm time timers */
3784 357c692c aliguori
    if (vm_running && likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
3785 357c692c aliguori
        qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
3786 357c692c aliguori
                        qemu_get_clock(vm_clock));
3787 357c692c aliguori
3788 357c692c aliguori
    /* real time timers */
3789 357c692c aliguori
    qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
3790 357c692c aliguori
                    qemu_get_clock(rt_clock));
3791 357c692c aliguori
3792 423f0742 pbrook
    /* Check bottom-halves last in case any of the earlier events triggered
3793 423f0742 pbrook
       them.  */
3794 423f0742 pbrook
    qemu_bh_poll();
3795 3b46e624 ths
3796 5905b2e5 bellard
}
3797 5905b2e5 bellard
3798 9596ebb7 pbrook
static int main_loop(void)
3799 5905b2e5 bellard
{
3800 5905b2e5 bellard
    int ret, timeout;
3801 89bfc105 bellard
#ifdef CONFIG_PROFILER
3802 89bfc105 bellard
    int64_t ti;
3803 89bfc105 bellard
#endif
3804 6a00d601 bellard
    CPUState *env;
3805 5905b2e5 bellard
3806 6a00d601 bellard
    cur_cpu = first_cpu;
3807 ee5605e5 balrog
    next_cpu = cur_cpu->next_cpu ?: first_cpu;
3808 5905b2e5 bellard
    for(;;) {
3809 5905b2e5 bellard
        if (vm_running) {
3810 15a76449 bellard
3811 15a76449 bellard
            for(;;) {
3812 15a76449 bellard
                /* get next cpu */
3813 ee5605e5 balrog
                env = next_cpu;
3814 89bfc105 bellard
#ifdef CONFIG_PROFILER
3815 89bfc105 bellard
                ti = profile_getclock();
3816 89bfc105 bellard
#endif
3817 2e70f6ef pbrook
                if (use_icount) {
3818 2e70f6ef pbrook
                    int64_t count;
3819 2e70f6ef pbrook
                    int decr;
3820 2e70f6ef pbrook
                    qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3821 2e70f6ef pbrook
                    env->icount_decr.u16.low = 0;
3822 2e70f6ef pbrook
                    env->icount_extra = 0;
3823 2e70f6ef pbrook
                    count = qemu_next_deadline();
3824 2e70f6ef pbrook
                    count = (count + (1 << icount_time_shift) - 1)
3825 2e70f6ef pbrook
                            >> icount_time_shift;
3826 2e70f6ef pbrook
                    qemu_icount += count;
3827 2e70f6ef pbrook
                    decr = (count > 0xffff) ? 0xffff : count;
3828 2e70f6ef pbrook
                    count -= decr;
3829 2e70f6ef pbrook
                    env->icount_decr.u16.low = decr;
3830 2e70f6ef pbrook
                    env->icount_extra = count;
3831 2e70f6ef pbrook
                }
3832 6a00d601 bellard
                ret = cpu_exec(env);
3833 89bfc105 bellard
#ifdef CONFIG_PROFILER
3834 89bfc105 bellard
                qemu_time += profile_getclock() - ti;
3835 89bfc105 bellard
#endif
3836 2e70f6ef pbrook
                if (use_icount) {
3837 2e70f6ef pbrook
                    /* Fold pending instructions back into the
3838 2e70f6ef pbrook
                       instruction counter, and clear the interrupt flag.  */
3839 2e70f6ef pbrook
                    qemu_icount -= (env->icount_decr.u16.low
3840 2e70f6ef pbrook
                                    + env->icount_extra);
3841 2e70f6ef pbrook
                    env->icount_decr.u32 = 0;
3842 2e70f6ef pbrook
                    env->icount_extra = 0;
3843 2e70f6ef pbrook
                }
3844 ee5605e5 balrog
                next_cpu = env->next_cpu ?: first_cpu;
3845 95b01009 aurel32
                if (event_pending && likely(ret != EXCP_DEBUG)) {
3846 ee5605e5 balrog
                    ret = EXCP_INTERRUPT;
3847 ee5605e5 balrog
                    event_pending = 0;
3848 ee5605e5 balrog
                    break;
3849 ee5605e5 balrog
                }
3850 bd967e05 pbrook
                if (ret == EXCP_HLT) {
3851 bd967e05 pbrook
                    /* Give the next CPU a chance to run.  */
3852 bd967e05 pbrook
                    cur_cpu = env;
3853 bd967e05 pbrook
                    continue;
3854 bd967e05 pbrook
                }
3855 15a76449 bellard
                if (ret != EXCP_HALTED)
3856 15a76449 bellard
                    break;
3857 15a76449 bellard
                /* all CPUs are halted ? */
3858 bd967e05 pbrook
                if (env == cur_cpu)
3859 15a76449 bellard
                    break;
3860 15a76449 bellard
            }
3861 15a76449 bellard
            cur_cpu = env;
3862 15a76449 bellard
3863 5905b2e5 bellard
            if (shutdown_requested) {
3864 3475187d bellard
                ret = EXCP_INTERRUPT;
3865 b2f76161 aurel32
                if (no_shutdown) {
3866 b2f76161 aurel32
                    vm_stop(0);
3867 b2f76161 aurel32
                    no_shutdown = 0;
3868 b2f76161 aurel32
                }
3869 b2f76161 aurel32
                else
3870 b2f76161 aurel32
                    break;
3871 5905b2e5 bellard
            }
3872 5905b2e5 bellard
            if (reset_requested) {
3873 5905b2e5 bellard
                reset_requested = 0;
3874 5905b2e5 bellard
                qemu_system_reset();
3875 3475187d bellard
                ret = EXCP_INTERRUPT;
3876 3475187d bellard
            }
3877 3475187d bellard
            if (powerdown_requested) {
3878 3475187d bellard
                powerdown_requested = 0;
3879 3475187d bellard
                qemu_system_powerdown();
3880 3475187d bellard
                ret = EXCP_INTERRUPT;
3881 5905b2e5 bellard
            }
3882 95b01009 aurel32
            if (unlikely(ret == EXCP_DEBUG)) {
3883 880a7578 aliguori
                gdb_set_stop_cpu(cur_cpu);
3884 5905b2e5 bellard
                vm_stop(EXCP_DEBUG);
3885 5905b2e5 bellard
            }
3886 bd967e05 pbrook
            /* If all cpus are halted then wait until the next IRQ */
3887 5905b2e5 bellard
            /* XXX: use timeout computed from timers */
3888 2e70f6ef pbrook
            if (ret == EXCP_HALTED) {
3889 2e70f6ef pbrook
                if (use_icount) {
3890 2e70f6ef pbrook
                    int64_t add;
3891 2e70f6ef pbrook
                    int64_t delta;
3892 2e70f6ef pbrook
                    /* Advance virtual time to the next event.  */
3893 2e70f6ef pbrook
                    if (use_icount == 1) {
3894 2e70f6ef pbrook
                        /* When not using an adaptive execution frequency
3895 2e70f6ef pbrook
                           we tend to get badly out of sync with real time,
3896 bf20dc07 ths
                           so just delay for a reasonable amount of time.  */
3897 2e70f6ef pbrook
                        delta = 0;
3898 2e70f6ef pbrook
                    } else {
3899 2e70f6ef pbrook
                        delta = cpu_get_icount() - cpu_get_clock();
3900 2e70f6ef pbrook
                    }
3901 2e70f6ef pbrook
                    if (delta > 0) {
3902 2e70f6ef pbrook
                        /* If virtual time is ahead of real time then just
3903 2e70f6ef pbrook
                           wait for IO.  */
3904 2e70f6ef pbrook
                        timeout = (delta / 1000000) + 1;
3905 2e70f6ef pbrook
                    } else {
3906 2e70f6ef pbrook
                        /* Wait for either IO to occur or the next
3907 2e70f6ef pbrook
                           timer event.  */
3908 2e70f6ef pbrook
                        add = qemu_next_deadline();
3909 2e70f6ef pbrook
                        /* We advance the timer before checking for IO.
3910 2e70f6ef pbrook
                           Limit the amount we advance so that early IO
3911 2e70f6ef pbrook
                           activity won't get the guest too far ahead.  */
3912 2e70f6ef pbrook
                        if (add > 10000000)
3913 2e70f6ef pbrook
                            add = 10000000;
3914 2e70f6ef pbrook
                        delta += add;
3915 2e70f6ef pbrook
                        add = (add + (1 << icount_time_shift) - 1)
3916 2e70f6ef pbrook
                              >> icount_time_shift;
3917 2e70f6ef pbrook
                        qemu_icount += add;
3918 2e70f6ef pbrook
                        timeout = delta / 1000000;
3919 2e70f6ef pbrook
                        if (timeout < 0)
3920 2e70f6ef pbrook
                            timeout = 0;
3921 2e70f6ef pbrook
                    }
3922 2e70f6ef pbrook
                } else {
3923 0a1af395 aliguori
                    timeout = 5000;
3924 2e70f6ef pbrook
                }
3925 2e70f6ef pbrook
            } else {
3926 5905b2e5 bellard
                timeout = 0;
3927 2e70f6ef pbrook
            }
3928 5905b2e5 bellard
        } else {
3929 98448f58 blueswir1
            if (shutdown_requested) {
3930 98448f58 blueswir1
                ret = EXCP_INTERRUPT;
3931 5b08fc10 aliguori
                break;
3932 98448f58 blueswir1
            }
3933 0a1af395 aliguori
            timeout = 5000;
3934 5905b2e5 bellard
        }
3935 89bfc105 bellard
#ifdef CONFIG_PROFILER
3936 89bfc105 bellard
        ti = profile_getclock();
3937 89bfc105 bellard
#endif
3938 5905b2e5 bellard
        main_loop_wait(timeout);
3939 89bfc105 bellard
#ifdef CONFIG_PROFILER
3940 89bfc105 bellard
        dev_time += profile_getclock() - ti;
3941 89bfc105 bellard
#endif
3942 b4608c04 bellard
    }
3943 34865134 bellard
    cpu_disable_ticks();
3944 34865134 bellard
    return ret;
3945 b4608c04 bellard
}
3946 b4608c04 bellard
3947 9bd7e6d9 pbrook
static void version(void)
3948 9bd7e6d9 pbrook
{
3949 4a19f1ec pbrook
    printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
3950 9bd7e6d9 pbrook
}
3951 9bd7e6d9 pbrook
3952 15f82208 ths
static void help(int exitcode)
3953 0824d6fc bellard
{
3954 9bd7e6d9 pbrook
    version();
3955 9bd7e6d9 pbrook
    printf("usage: %s [options] [disk_image]\n"
3956 0824d6fc bellard
           "\n"
3957 a20dd508 bellard
           "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3958 fc01f7e7 bellard
           "\n"
3959 5824d651 blueswir1
#define DEF(option, opt_arg, opt_enum, opt_help)        \
3960 5824d651 blueswir1
           opt_help
3961 5824d651 blueswir1
#define DEFHEADING(text) stringify(text) "\n"
3962 5824d651 blueswir1
#include "qemu-options.h"
3963 5824d651 blueswir1
#undef DEF
3964 5824d651 blueswir1
#undef DEFHEADING
3965 5824d651 blueswir1
#undef GEN_DOCS
3966 0824d6fc bellard
           "\n"
3967 82c643ff bellard
           "During emulation, the following keys are useful:\n"
3968 032a8c9e bellard
           "ctrl-alt-f      toggle full screen\n"
3969 032a8c9e bellard
           "ctrl-alt-n      switch to virtual console 'n'\n"
3970 032a8c9e bellard
           "ctrl-alt        toggle mouse and keyboard grab\n"
3971 82c643ff bellard
           "\n"
3972 82c643ff bellard
           "When using -nographic, press 'ctrl-a h' to get some help.\n"
3973 82c643ff bellard
           ,
3974 0db63474 bellard
           "qemu",
3975 a00bad7e bellard
           DEFAULT_RAM_SIZE,
3976 7c9d8e07 bellard
#ifndef _WIN32
3977 a00bad7e bellard
           DEFAULT_NETWORK_SCRIPT,
3978 b46a8906 ths
           DEFAULT_NETWORK_DOWN_SCRIPT,
3979 7c9d8e07 bellard
#endif
3980 6e44ba7f bellard
           DEFAULT_GDBSTUB_PORT,
3981 bce61846 bellard
           "/tmp/qemu.log");
3982 15f82208 ths
    exit(exitcode);
3983 0824d6fc bellard
}
3984 0824d6fc bellard
3985 cd6f1169 bellard
#define HAS_ARG 0x0001
3986 cd6f1169 bellard
3987 cd6f1169 bellard
enum {
3988 5824d651 blueswir1
#define DEF(option, opt_arg, opt_enum, opt_help)        \
3989 5824d651 blueswir1
    opt_enum,
3990 5824d651 blueswir1
#define DEFHEADING(text)
3991 5824d651 blueswir1
#include "qemu-options.h"
3992 5824d651 blueswir1
#undef DEF
3993 5824d651 blueswir1
#undef DEFHEADING
3994 5824d651 blueswir1
#undef GEN_DOCS
3995 cd6f1169 bellard
};
3996 cd6f1169 bellard
3997 cd6f1169 bellard
typedef struct QEMUOption {
3998 cd6f1169 bellard
    const char *name;
3999 cd6f1169 bellard
    int flags;
4000 cd6f1169 bellard
    int index;
4001 cd6f1169 bellard
} QEMUOption;
4002 cd6f1169 bellard
4003 dbed7e40 blueswir1
static const QEMUOption qemu_options[] = {
4004 cd6f1169 bellard
    { "h", 0, QEMU_OPTION_h },
4005 5824d651 blueswir1
#define DEF(option, opt_arg, opt_enum, opt_help)        \
4006 5824d651 blueswir1
    { option, opt_arg, opt_enum },
4007 5824d651 blueswir1
#define DEFHEADING(text)
4008 5824d651 blueswir1
#include "qemu-options.h"
4009 5824d651 blueswir1
#undef DEF
4010 5824d651 blueswir1
#undef DEFHEADING
4011 5824d651 blueswir1
#undef GEN_DOCS
4012 cd6f1169 bellard
    { NULL },
4013 fc01f7e7 bellard
};
4014 fc01f7e7 bellard
4015 1d14ffa9 bellard
#ifdef HAS_AUDIO
4016 6a36d84e bellard
struct soundhw soundhw[] = {
4017 b00052e4 balrog
#ifdef HAS_AUDIO_CHOICE
4018 4ce7ff6e aurel32
#if defined(TARGET_I386) || defined(TARGET_MIPS)
4019 fd06c375 bellard
    {
4020 fd06c375 bellard
        "pcspk",
4021 fd06c375 bellard
        "PC speaker",
4022 fd06c375 bellard
        0,
4023 fd06c375 bellard
        1,
4024 fd06c375 bellard
        { .init_isa = pcspk_audio_init }
4025 fd06c375 bellard
    },
4026 fd06c375 bellard
#endif
4027 4c9b53e3 malc
4028 4c9b53e3 malc
#ifdef CONFIG_SB16
4029 6a36d84e bellard
    {
4030 6a36d84e bellard
        "sb16",
4031 6a36d84e bellard
        "Creative Sound Blaster 16",
4032 6a36d84e bellard
        0,
4033 6a36d84e bellard
        1,
4034 6a36d84e bellard
        { .init_isa = SB16_init }
4035 6a36d84e bellard
    },
4036 4c9b53e3 malc
#endif
4037 6a36d84e bellard
4038 cc53d26d malc
#ifdef CONFIG_CS4231A
4039 cc53d26d malc
    {
4040 cc53d26d malc
        "cs4231a",
4041 cc53d26d malc
        "CS4231A",
4042 cc53d26d malc
        0,
4043 cc53d26d malc
        1,
4044 cc53d26d malc
        { .init_isa = cs4231a_init }
4045 cc53d26d malc
    },
4046 cc53d26d malc
#endif
4047 cc53d26d malc
4048 1d14ffa9 bellard
#ifdef CONFIG_ADLIB
4049 6a36d84e bellard
    {
4050 6a36d84e bellard
        "adlib",
4051 1d14ffa9 bellard
#ifdef HAS_YMF262
4052 6a36d84e bellard
        "Yamaha YMF262 (OPL3)",
4053 1d14ffa9 bellard
#else
4054 6a36d84e bellard
        "Yamaha YM3812 (OPL2)",
4055 1d14ffa9 bellard
#endif
4056 6a36d84e bellard
        0,
4057 6a36d84e bellard
        1,
4058 6a36d84e bellard
        { .init_isa = Adlib_init }
4059 6a36d84e bellard
    },
4060 1d14ffa9 bellard
#endif
4061 6a36d84e bellard
4062 1d14ffa9 bellard
#ifdef CONFIG_GUS
4063 6a36d84e bellard
    {
4064 6a36d84e bellard
        "gus",
4065 6a36d84e bellard
        "Gravis Ultrasound GF1",
4066 6a36d84e bellard
        0,
4067 6a36d84e bellard
        1,
4068 6a36d84e bellard
        { .init_isa = GUS_init }
4069 6a36d84e bellard
    },
4070 1d14ffa9 bellard
#endif
4071 6a36d84e bellard
4072 4c9b53e3 malc
#ifdef CONFIG_AC97
4073 e5c9a13e balrog
    {
4074 e5c9a13e balrog
        "ac97",
4075 e5c9a13e balrog
        "Intel 82801AA AC97 Audio",
4076 e5c9a13e balrog
        0,
4077 e5c9a13e balrog
        0,
4078 e5c9a13e balrog
        { .init_pci = ac97_init }
4079 e5c9a13e balrog
    },
4080 4c9b53e3 malc
#endif
4081 e5c9a13e balrog
4082 4c9b53e3 malc
#ifdef CONFIG_ES1370
4083 6a36d84e bellard
    {
4084 6a36d84e bellard
        "es1370",
4085 6a36d84e bellard
        "ENSONIQ AudioPCI ES1370",
4086 6a36d84e bellard
        0,
4087 6a36d84e bellard
        0,
4088 6a36d84e bellard
        { .init_pci = es1370_init }
4089 6a36d84e bellard
    },
4090 b00052e4 balrog
#endif
4091 6a36d84e bellard
4092 4c9b53e3 malc
#endif /* HAS_AUDIO_CHOICE */
4093 4c9b53e3 malc
4094 6a36d84e bellard
    { NULL, NULL, 0, 0, { NULL } }
4095 6a36d84e bellard
};
4096 6a36d84e bellard
4097 6a36d84e bellard
static void select_soundhw (const char *optarg)
4098 6a36d84e bellard
{
4099 6a36d84e bellard
    struct soundhw *c;
4100 6a36d84e bellard
4101 6a36d84e bellard
    if (*optarg == '?') {
4102 6a36d84e bellard
    show_valid_cards:
4103 6a36d84e bellard
4104 6a36d84e bellard
        printf ("Valid sound card names (comma separated):\n");
4105 6a36d84e bellard
        for (c = soundhw; c->name; ++c) {
4106 6a36d84e bellard
            printf ("%-11s %s\n", c->name, c->descr);
4107 6a36d84e bellard
        }
4108 6a36d84e bellard
        printf ("\n-soundhw all will enable all of the above\n");
4109 1d14ffa9 bellard
        exit (*optarg != '?');
4110 1d14ffa9 bellard
    }
4111 1d14ffa9 bellard
    else {
4112 6a36d84e bellard
        size_t l;
4113 1d14ffa9 bellard
        const char *p;
4114 1d14ffa9 bellard
        char *e;
4115 1d14ffa9 bellard
        int bad_card = 0;
4116 1d14ffa9 bellard
4117 6a36d84e bellard
        if (!strcmp (optarg, "all")) {
4118 6a36d84e bellard
            for (c = soundhw; c->name; ++c) {
4119 6a36d84e bellard
                c->enabled = 1;
4120 6a36d84e bellard
            }
4121 6a36d84e bellard
            return;
4122 6a36d84e bellard
        }
4123 1d14ffa9 bellard
4124 6a36d84e bellard
        p = optarg;
4125 1d14ffa9 bellard
        while (*p) {
4126 1d14ffa9 bellard
            e = strchr (p, ',');
4127 1d14ffa9 bellard
            l = !e ? strlen (p) : (size_t) (e - p);
4128 6a36d84e bellard
4129 6a36d84e bellard
            for (c = soundhw; c->name; ++c) {
4130 6a36d84e bellard
                if (!strncmp (c->name, p, l)) {
4131 6a36d84e bellard
                    c->enabled = 1;
4132 1d14ffa9 bellard
                    break;
4133 1d14ffa9 bellard
                }
4134 1d14ffa9 bellard
            }
4135 6a36d84e bellard
4136 6a36d84e bellard
            if (!c->name) {
4137 1d14ffa9 bellard
                if (l > 80) {
4138 1d14ffa9 bellard
                    fprintf (stderr,
4139 1d14ffa9 bellard
                             "Unknown sound card name (too big to show)\n");
4140 1d14ffa9 bellard
                }
4141 1d14ffa9 bellard
                else {
4142 1d14ffa9 bellard
                    fprintf (stderr, "Unknown sound card name `%.*s'\n",
4143 1d14ffa9 bellard
                             (int) l, p);
4144 1d14ffa9 bellard
                }
4145 1d14ffa9 bellard
                bad_card = 1;
4146 1d14ffa9 bellard
            }
4147 1d14ffa9 bellard
            p += l + (e != NULL);
4148 1d14ffa9 bellard
        }
4149 1d14ffa9 bellard
4150 1d14ffa9 bellard
        if (bad_card)
4151 1d14ffa9 bellard
            goto show_valid_cards;
4152 1d14ffa9 bellard
    }
4153 1d14ffa9 bellard
}
4154 1d14ffa9 bellard
#endif
4155 1d14ffa9 bellard
4156 3893c124 malc
static void select_vgahw (const char *p)
4157 3893c124 malc
{
4158 3893c124 malc
    const char *opts;
4159 3893c124 malc
4160 3893c124 malc
    if (strstart(p, "std", &opts)) {
4161 c2b3b41a aliguori
        std_vga_enabled = 1;
4162 3893c124 malc
        cirrus_vga_enabled = 0;
4163 3893c124 malc
        vmsvga_enabled = 0;
4164 3893c124 malc
    } else if (strstart(p, "cirrus", &opts)) {
4165 3893c124 malc
        cirrus_vga_enabled = 1;
4166 c2b3b41a aliguori
        std_vga_enabled = 0;
4167 3893c124 malc
        vmsvga_enabled = 0;
4168 3893c124 malc
    } else if (strstart(p, "vmware", &opts)) {
4169 3893c124 malc
        cirrus_vga_enabled = 0;
4170 c2b3b41a aliguori
        std_vga_enabled = 0;
4171 3893c124 malc
        vmsvga_enabled = 1;
4172 c2b3b41a aliguori
    } else if (strstart(p, "none", &opts)) {
4173 c2b3b41a aliguori
        cirrus_vga_enabled = 0;
4174 c2b3b41a aliguori
        std_vga_enabled = 0;
4175 c2b3b41a aliguori
        vmsvga_enabled = 0;
4176 3893c124 malc
    } else {
4177 3893c124 malc
    invalid_vga:
4178 3893c124 malc
        fprintf(stderr, "Unknown vga type: %s\n", p);
4179 3893c124 malc
        exit(1);
4180 3893c124 malc
    }
4181 cb5a7aa8 malc
    while (*opts) {
4182 cb5a7aa8 malc
        const char *nextopt;
4183 cb5a7aa8 malc
4184 cb5a7aa8 malc
        if (strstart(opts, ",retrace=", &nextopt)) {
4185 cb5a7aa8 malc
            opts = nextopt;
4186 cb5a7aa8 malc
            if (strstart(opts, "dumb", &nextopt))
4187 cb5a7aa8 malc
                vga_retrace_method = VGA_RETRACE_DUMB;
4188 cb5a7aa8 malc
            else if (strstart(opts, "precise", &nextopt))
4189 cb5a7aa8 malc
                vga_retrace_method = VGA_RETRACE_PRECISE;
4190 cb5a7aa8 malc
            else goto invalid_vga;
4191 cb5a7aa8 malc
        } else goto invalid_vga;
4192 cb5a7aa8 malc
        opts = nextopt;
4193 cb5a7aa8 malc
    }
4194 3893c124 malc
}
4195 3893c124 malc
4196 3587d7e6 bellard
#ifdef _WIN32
4197 3587d7e6 bellard
static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4198 3587d7e6 bellard
{
4199 3587d7e6 bellard
    exit(STATUS_CONTROL_C_EXIT);
4200 3587d7e6 bellard
    return TRUE;
4201 3587d7e6 bellard
}
4202 3587d7e6 bellard
#endif
4203 3587d7e6 bellard
4204 c4be29ff aliguori
int qemu_uuid_parse(const char *str, uint8_t *uuid)
4205 8fcb1b90 blueswir1
{
4206 8fcb1b90 blueswir1
    int ret;
4207 8fcb1b90 blueswir1
4208 8fcb1b90 blueswir1
    if(strlen(str) != 36)
4209 8fcb1b90 blueswir1
        return -1;
4210 8fcb1b90 blueswir1
4211 8fcb1b90 blueswir1
    ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4212 8fcb1b90 blueswir1
            &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4213 8fcb1b90 blueswir1
            &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4214 8fcb1b90 blueswir1
4215 8fcb1b90 blueswir1
    if(ret != 16)
4216 8fcb1b90 blueswir1
        return -1;
4217 8fcb1b90 blueswir1
4218 b6f6e3d3 aliguori
#ifdef TARGET_I386
4219 b6f6e3d3 aliguori
    smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4220 b6f6e3d3 aliguori
#endif
4221 b6f6e3d3 aliguori
4222 8fcb1b90 blueswir1
    return 0;
4223 8fcb1b90 blueswir1
}
4224 8fcb1b90 blueswir1
4225 7c9d8e07 bellard
#define MAX_NET_CLIENTS 32
4226 c20709aa bellard
4227 5b08fc10 aliguori
#ifndef _WIN32
4228 5b08fc10 aliguori
4229 5b08fc10 aliguori
static void termsig_handler(int signal)
4230 5b08fc10 aliguori
{
4231 5b08fc10 aliguori
    qemu_system_shutdown_request();
4232 5b08fc10 aliguori
}
4233 5b08fc10 aliguori
4234 6f9e3801 blueswir1
static void termsig_setup(void)
4235 5b08fc10 aliguori
{
4236 5b08fc10 aliguori
    struct sigaction act;
4237 5b08fc10 aliguori
4238 5b08fc10 aliguori
    memset(&act, 0, sizeof(act));
4239 5b08fc10 aliguori
    act.sa_handler = termsig_handler;
4240 5b08fc10 aliguori
    sigaction(SIGINT,  &act, NULL);
4241 5b08fc10 aliguori
    sigaction(SIGHUP,  &act, NULL);
4242 5b08fc10 aliguori
    sigaction(SIGTERM, &act, NULL);
4243 5b08fc10 aliguori
}
4244 5b08fc10 aliguori
4245 5b08fc10 aliguori
#endif
4246 5b08fc10 aliguori
4247 902b3d5c malc
int main(int argc, char **argv, char **envp)
4248 0824d6fc bellard
{
4249 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
4250 59030a8c aliguori
    const char *gdbstub_dev = NULL;
4251 67b915a5 bellard
#endif
4252 28c5af54 j_mayer
    uint32_t boot_devices_bitmap = 0;
4253 e4bcb14c ths
    int i;
4254 28c5af54 j_mayer
    int snapshot, linux_boot, net_boot;
4255 7f7f9873 bellard
    const char *initrd_filename;
4256 a20dd508 bellard
    const char *kernel_filename, *kernel_cmdline;
4257 28c5af54 j_mayer
    const char *boot_devices = "";
4258 3023f332 aliguori
    DisplayState *ds;
4259 7d957bd8 aliguori
    DisplayChangeListener *dcl;
4260 46d4767d bellard
    int cyls, heads, secs, translation;
4261 fd5f393a pbrook
    const char *net_clients[MAX_NET_CLIENTS];
4262 7c9d8e07 bellard
    int nb_net_clients;
4263 dc72ac14 balrog
    const char *bt_opts[MAX_BT_CMDLINE];
4264 dc72ac14 balrog
    int nb_bt_opts;
4265 e4bcb14c ths
    int hda_index;
4266 cd6f1169 bellard
    int optind;
4267 cd6f1169 bellard
    const char *r, *optarg;
4268 4c621805 aliguori
    CharDriverState *monitor_hd = NULL;
4269 fd5f393a pbrook
    const char *monitor_device;
4270 fd5f393a pbrook
    const char *serial_devices[MAX_SERIAL_PORTS];
4271 8d11df9e bellard
    int serial_device_index;
4272 fd5f393a pbrook
    const char *parallel_devices[MAX_PARALLEL_PORTS];
4273 6508fe59 bellard
    int parallel_device_index;
4274 9ede2fde aliguori
    const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4275 9ede2fde aliguori
    int virtio_console_index;
4276 d63d307f bellard
    const char *loadvm = NULL;
4277 cc1daa40 bellard
    QEMUMachine *machine;
4278 94fc95cd j_mayer
    const char *cpu_model;
4279 fd5f393a pbrook
    const char *usb_devices[MAX_USB_CMDLINE];
4280 a594cfbf bellard
    int usb_devices_index;
4281 b9e82a59 blueswir1
#ifndef _WIN32
4282 71e3ceb8 ths
    int fds[2];
4283 b9e82a59 blueswir1
#endif
4284 26a5f13b bellard
    int tb_size;
4285 93815bc2 ths
    const char *pid_file = NULL;
4286 5bb7910a aliguori
    const char *incoming = NULL;
4287 b9e82a59 blueswir1
#ifndef _WIN32
4288 54042bcf aliguori
    int fd = 0;
4289 54042bcf aliguori
    struct passwd *pwd = NULL;
4290 0858532e aliguori
    const char *chroot_dir = NULL;
4291 0858532e aliguori
    const char *run_as = NULL;
4292 b9e82a59 blueswir1
#endif
4293 0bd48850 bellard
4294 902b3d5c malc
    qemu_cache_utils_init(envp);
4295 902b3d5c malc
4296 0bd48850 bellard
    LIST_INIT (&vm_change_state_head);
4297 be995c27 bellard
#ifndef _WIN32
4298 be995c27 bellard
    {
4299 be995c27 bellard
        struct sigaction act;
4300 be995c27 bellard
        sigfillset(&act.sa_mask);
4301 be995c27 bellard
        act.sa_flags = 0;
4302 be995c27 bellard
        act.sa_handler = SIG_IGN;
4303 be995c27 bellard
        sigaction(SIGPIPE, &act, NULL);
4304 be995c27 bellard
    }
4305 3587d7e6 bellard
#else
4306 3587d7e6 bellard
    SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
4307 a8e5ac33 bellard
    /* Note: cpu_interrupt() is currently not SMP safe, so we force
4308 a8e5ac33 bellard
       QEMU to run on a single CPU */
4309 a8e5ac33 bellard
    {
4310 a8e5ac33 bellard
        HANDLE h;
4311 a8e5ac33 bellard
        DWORD mask, smask;
4312 a8e5ac33 bellard
        int i;
4313 a8e5ac33 bellard
        h = GetCurrentProcess();
4314 a8e5ac33 bellard
        if (GetProcessAffinityMask(h, &mask, &smask)) {
4315 a8e5ac33 bellard
            for(i = 0; i < 32; i++) {
4316 a8e5ac33 bellard
                if (mask & (1 << i))
4317 a8e5ac33 bellard
                    break;
4318 a8e5ac33 bellard
            }
4319 a8e5ac33 bellard
            if (i != 32) {
4320 a8e5ac33 bellard
                mask = 1 << i;
4321 a8e5ac33 bellard
                SetProcessAffinityMask(h, mask);
4322 a8e5ac33 bellard
            }
4323 a8e5ac33 bellard
        }
4324 a8e5ac33 bellard
    }
4325 67b915a5 bellard
#endif
4326 be995c27 bellard
4327 cc1daa40 bellard
    register_machines();
4328 cc1daa40 bellard
    machine = first_machine;
4329 94fc95cd j_mayer
    cpu_model = NULL;
4330 fc01f7e7 bellard
    initrd_filename = NULL;
4331 4fc5d071 aurel32
    ram_size = 0;
4332 313aa567 bellard
    vga_ram_size = VGA_RAM_SIZE;
4333 33e3963e bellard
    snapshot = 0;
4334 a20dd508 bellard
    nographic = 0;
4335 4d3b6f6e balrog
    curses = 0;
4336 a20dd508 bellard
    kernel_filename = NULL;
4337 a20dd508 bellard
    kernel_cmdline = "";
4338 c4b1fcc0 bellard
    cyls = heads = secs = 0;
4339 46d4767d bellard
    translation = BIOS_ATA_TRANSLATION_AUTO;
4340 d47d13b9 aliguori
    monitor_device = "vc:80Cx24C";
4341 c4b1fcc0 bellard
4342 c75a823c aurel32
    serial_devices[0] = "vc:80Cx24C";
4343 8d11df9e bellard
    for(i = 1; i < MAX_SERIAL_PORTS; i++)
4344 fd5f393a pbrook
        serial_devices[i] = NULL;
4345 8d11df9e bellard
    serial_device_index = 0;
4346 3b46e624 ths
4347 8290edda aliguori
    parallel_devices[0] = "vc:80Cx24C";
4348 6508fe59 bellard
    for(i = 1; i < MAX_PARALLEL_PORTS; i++)
4349 fd5f393a pbrook
        parallel_devices[i] = NULL;
4350 6508fe59 bellard
    parallel_device_index = 0;
4351 3b46e624 ths
4352 1b8fc811 aliguori
    for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
4353 9ede2fde aliguori
        virtio_consoles[i] = NULL;
4354 9ede2fde aliguori
    virtio_console_index = 0;
4355 9ede2fde aliguori
4356 a594cfbf bellard
    usb_devices_index = 0;
4357 3b46e624 ths
4358 7c9d8e07 bellard
    nb_net_clients = 0;
4359 dc72ac14 balrog
    nb_bt_opts = 0;
4360 e4bcb14c ths
    nb_drives = 0;
4361 e4bcb14c ths
    nb_drives_opt = 0;
4362 e4bcb14c ths
    hda_index = -1;
4363 7c9d8e07 bellard
4364 7c9d8e07 bellard
    nb_nics = 0;
4365 3b46e624 ths
4366 26a5f13b bellard
    tb_size = 0;
4367 41bd639b blueswir1
    autostart= 1;
4368 41bd639b blueswir1
4369 cd6f1169 bellard
    optind = 1;
4370 0824d6fc bellard
    for(;;) {
4371 cd6f1169 bellard
        if (optind >= argc)
4372 0824d6fc bellard
            break;
4373 cd6f1169 bellard
        r = argv[optind];
4374 cd6f1169 bellard
        if (r[0] != '-') {
4375 609497ab balrog
            hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
4376 cd6f1169 bellard
        } else {
4377 cd6f1169 bellard
            const QEMUOption *popt;
4378 cd6f1169 bellard
4379 cd6f1169 bellard
            optind++;
4380 dff5efc8 pbrook
            /* Treat --foo the same as -foo.  */
4381 dff5efc8 pbrook
            if (r[1] == '-')
4382 dff5efc8 pbrook
                r++;
4383 cd6f1169 bellard
            popt = qemu_options;
4384 cd6f1169 bellard
            for(;;) {
4385 cd6f1169 bellard
                if (!popt->name) {
4386 5fafdf24 ths
                    fprintf(stderr, "%s: invalid option -- '%s'\n",
4387 cd6f1169 bellard
                            argv[0], r);
4388 cd6f1169 bellard
                    exit(1);
4389 cd6f1169 bellard
                }
4390 cd6f1169 bellard
                if (!strcmp(popt->name, r + 1))
4391 cd6f1169 bellard
                    break;
4392 cd6f1169 bellard
                popt++;
4393 cd6f1169 bellard
            }
4394 cd6f1169 bellard
            if (popt->flags & HAS_ARG) {
4395 cd6f1169 bellard
                if (optind >= argc) {
4396 cd6f1169 bellard
                    fprintf(stderr, "%s: option '%s' requires an argument\n",
4397 cd6f1169 bellard
                            argv[0], r);
4398 cd6f1169 bellard
                    exit(1);
4399 cd6f1169 bellard
                }
4400 cd6f1169 bellard
                optarg = argv[optind++];
4401 cd6f1169 bellard
            } else {
4402 cd6f1169 bellard
                optarg = NULL;
4403 cd6f1169 bellard
            }
4404 cd6f1169 bellard
4405 cd6f1169 bellard
            switch(popt->index) {
4406 cc1daa40 bellard
            case QEMU_OPTION_M:
4407 cc1daa40 bellard
                machine = find_machine(optarg);
4408 cc1daa40 bellard
                if (!machine) {
4409 cc1daa40 bellard
                    QEMUMachine *m;
4410 cc1daa40 bellard
                    printf("Supported machines are:\n");
4411 cc1daa40 bellard
                    for(m = first_machine; m != NULL; m = m->next) {
4412 cc1daa40 bellard
                        printf("%-10s %s%s\n",
4413 5fafdf24 ths
                               m->name, m->desc,
4414 cc1daa40 bellard
                               m == first_machine ? " (default)" : "");
4415 cc1daa40 bellard
                    }
4416 15f82208 ths
                    exit(*optarg != '?');
4417 cc1daa40 bellard
                }
4418 cc1daa40 bellard
                break;
4419 94fc95cd j_mayer
            case QEMU_OPTION_cpu:
4420 94fc95cd j_mayer
                /* hw initialization will check this */
4421 15f82208 ths
                if (*optarg == '?') {
4422 c732abe2 j_mayer
/* XXX: implement xxx_cpu_list for targets that still miss it */
4423 c732abe2 j_mayer
#if defined(cpu_list)
4424 c732abe2 j_mayer
                    cpu_list(stdout, &fprintf);
4425 94fc95cd j_mayer
#endif
4426 15f82208 ths
                    exit(0);
4427 94fc95cd j_mayer
                } else {
4428 94fc95cd j_mayer
                    cpu_model = optarg;
4429 94fc95cd j_mayer
                }
4430 94fc95cd j_mayer
                break;
4431 cd6f1169 bellard
            case QEMU_OPTION_initrd:
4432 fc01f7e7 bellard
                initrd_filename = optarg;
4433 fc01f7e7 bellard
                break;
4434 cd6f1169 bellard
            case QEMU_OPTION_hda:
4435 e4bcb14c ths
                if (cyls == 0)
4436 609497ab balrog
                    hda_index = drive_add(optarg, HD_ALIAS, 0);
4437 e4bcb14c ths
                else
4438 609497ab balrog
                    hda_index = drive_add(optarg, HD_ALIAS
4439 e4bcb14c ths
                             ",cyls=%d,heads=%d,secs=%d%s",
4440 609497ab balrog
                             0, cyls, heads, secs,
4441 e4bcb14c ths
                             translation == BIOS_ATA_TRANSLATION_LBA ?
4442 e4bcb14c ths
                                 ",trans=lba" :
4443 e4bcb14c ths
                             translation == BIOS_ATA_TRANSLATION_NONE ?
4444 e4bcb14c ths
                                 ",trans=none" : "");
4445 e4bcb14c ths
                 break;
4446 cd6f1169 bellard
            case QEMU_OPTION_hdb:
4447 cc1daa40 bellard
            case QEMU_OPTION_hdc:
4448 cc1daa40 bellard
            case QEMU_OPTION_hdd:
4449 609497ab balrog
                drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
4450 fc01f7e7 bellard
                break;
4451 e4bcb14c ths
            case QEMU_OPTION_drive:
4452 609497ab balrog
                drive_add(NULL, "%s", optarg);
4453 e4bcb14c ths
                break;
4454 3e3d5815 balrog
            case QEMU_OPTION_mtdblock:
4455 609497ab balrog
                drive_add(optarg, MTD_ALIAS);
4456 3e3d5815 balrog
                break;
4457 a1bb27b1 pbrook
            case QEMU_OPTION_sd:
4458 609497ab balrog
                drive_add(optarg, SD_ALIAS);
4459 a1bb27b1 pbrook
                break;
4460 86f55663 j_mayer
            case QEMU_OPTION_pflash:
4461 609497ab balrog
                drive_add(optarg, PFLASH_ALIAS);
4462 86f55663 j_mayer
                break;
4463 cd6f1169 bellard
            case QEMU_OPTION_snapshot:
4464 33e3963e bellard
                snapshot = 1;
4465 33e3963e bellard
                break;
4466 cd6f1169 bellard
            case QEMU_OPTION_hdachs:
4467 330d0414 bellard
                {
4468 330d0414 bellard
                    const char *p;
4469 330d0414 bellard
                    p = optarg;
4470 330d0414 bellard
                    cyls = strtol(p, (char **)&p, 0);
4471 46d4767d bellard
                    if (cyls < 1 || cyls > 16383)
4472 46d4767d bellard
                        goto chs_fail;
4473 330d0414 bellard
                    if (*p != ',')
4474 330d0414 bellard
                        goto chs_fail;
4475 330d0414 bellard
                    p++;
4476 330d0414 bellard
                    heads = strtol(p, (char **)&p, 0);
4477 46d4767d bellard
                    if (heads < 1 || heads > 16)
4478 46d4767d bellard
                        goto chs_fail;
4479 330d0414 bellard
                    if (*p != ',')
4480 330d0414 bellard
                        goto chs_fail;
4481 330d0414 bellard
                    p++;
4482 330d0414 bellard
                    secs = strtol(p, (char **)&p, 0);
4483 46d4767d bellard
                    if (secs < 1 || secs > 63)
4484 46d4767d bellard
                        goto chs_fail;
4485 46d4767d bellard
                    if (*p == ',') {
4486 46d4767d bellard
                        p++;
4487 46d4767d bellard
                        if (!strcmp(p, "none"))
4488 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_NONE;
4489 46d4767d bellard
                        else if (!strcmp(p, "lba"))
4490 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_LBA;
4491 46d4767d bellard
                        else if (!strcmp(p, "auto"))
4492 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_AUTO;
4493 46d4767d bellard
                        else
4494 46d4767d bellard
                            goto chs_fail;
4495 46d4767d bellard
                    } else if (*p != '\0') {
4496 c4b1fcc0 bellard
                    chs_fail:
4497 46d4767d bellard
                        fprintf(stderr, "qemu: invalid physical CHS format\n");
4498 46d4767d bellard
                        exit(1);
4499 c4b1fcc0 bellard
                    }
4500 e4bcb14c ths
                    if (hda_index != -1)
4501 609497ab balrog
                        snprintf(drives_opt[hda_index].opt,
4502 609497ab balrog
                                 sizeof(drives_opt[hda_index].opt),
4503 609497ab balrog
                                 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
4504 609497ab balrog
                                 0, cyls, heads, secs,
4505 e4bcb14c ths
                                 translation == BIOS_ATA_TRANSLATION_LBA ?
4506 e4bcb14c ths
                                         ",trans=lba" :
4507 e4bcb14c ths
                                 translation == BIOS_ATA_TRANSLATION_NONE ?
4508 e4bcb14c ths
                                     ",trans=none" : "");
4509 330d0414 bellard
                }
4510 330d0414 bellard
                break;
4511 cd6f1169 bellard
            case QEMU_OPTION_nographic:
4512 a20dd508 bellard
                nographic = 1;
4513 a20dd508 bellard
                break;
4514 4d3b6f6e balrog
#ifdef CONFIG_CURSES
4515 4d3b6f6e balrog
            case QEMU_OPTION_curses:
4516 4d3b6f6e balrog
                curses = 1;
4517 4d3b6f6e balrog
                break;
4518 4d3b6f6e balrog
#endif
4519 a171fe39 balrog
            case QEMU_OPTION_portrait:
4520 a171fe39 balrog
                graphic_rotate = 1;
4521 a171fe39 balrog
                break;
4522 cd6f1169 bellard
            case QEMU_OPTION_kernel:
4523 a20dd508 bellard
                kernel_filename = optarg;
4524 a20dd508 bellard
                break;
4525 cd6f1169 bellard
            case QEMU_OPTION_append:
4526 a20dd508 bellard
                kernel_cmdline = optarg;
4527 313aa567 bellard
                break;
4528 cd6f1169 bellard
            case QEMU_OPTION_cdrom:
4529 609497ab balrog
                drive_add(optarg, CDROM_ALIAS);
4530 36b486bb bellard
                break;
4531 cd6f1169 bellard
            case QEMU_OPTION_boot:
4532 28c5af54 j_mayer
                boot_devices = optarg;
4533 28c5af54 j_mayer
                /* We just do some generic consistency checks */
4534 28c5af54 j_mayer
                {
4535 28c5af54 j_mayer
                    /* Could easily be extended to 64 devices if needed */
4536 60fe76f3 ths
                    const char *p;
4537 28c5af54 j_mayer
                    
4538 28c5af54 j_mayer
                    boot_devices_bitmap = 0;
4539 28c5af54 j_mayer
                    for (p = boot_devices; *p != '\0'; p++) {
4540 28c5af54 j_mayer
                        /* Allowed boot devices are:
4541 28c5af54 j_mayer
                         * a b     : floppy disk drives
4542 28c5af54 j_mayer
                         * c ... f : IDE disk drives
4543 28c5af54 j_mayer
                         * g ... m : machine implementation dependant drives
4544 28c5af54 j_mayer
                         * n ... p : network devices
4545 28c5af54 j_mayer
                         * It's up to each machine implementation to check
4546 28c5af54 j_mayer
                         * if the given boot devices match the actual hardware
4547 28c5af54 j_mayer
                         * implementation and firmware features.
4548 28c5af54 j_mayer
                         */
4549 28c5af54 j_mayer
                        if (*p < 'a' || *p > 'q') {
4550 28c5af54 j_mayer
                            fprintf(stderr, "Invalid boot device '%c'\n", *p);
4551 28c5af54 j_mayer
                            exit(1);
4552 28c5af54 j_mayer
                        }
4553 28c5af54 j_mayer
                        if (boot_devices_bitmap & (1 << (*p - 'a'))) {
4554 28c5af54 j_mayer
                            fprintf(stderr,
4555 28c5af54 j_mayer
                                    "Boot device '%c' was given twice\n",*p);
4556 28c5af54 j_mayer
                            exit(1);
4557 28c5af54 j_mayer
                        }
4558 28c5af54 j_mayer
                        boot_devices_bitmap |= 1 << (*p - 'a');
4559 28c5af54 j_mayer
                    }
4560 36b486bb bellard
                }
4561 36b486bb bellard
                break;
4562 cd6f1169 bellard
            case QEMU_OPTION_fda:
4563 cd6f1169 bellard
            case QEMU_OPTION_fdb:
4564 609497ab balrog
                drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
4565 c45886db bellard
                break;
4566 52ca8d6a bellard
#ifdef TARGET_I386
4567 52ca8d6a bellard
            case QEMU_OPTION_no_fd_bootchk:
4568 52ca8d6a bellard
                fd_bootchk = 0;
4569 52ca8d6a bellard
                break;
4570 52ca8d6a bellard
#endif
4571 7c9d8e07 bellard
            case QEMU_OPTION_net:
4572 7c9d8e07 bellard
                if (nb_net_clients >= MAX_NET_CLIENTS) {
4573 7c9d8e07 bellard
                    fprintf(stderr, "qemu: too many network clients\n");
4574 c4b1fcc0 bellard
                    exit(1);
4575 c4b1fcc0 bellard
                }
4576 fd5f393a pbrook
                net_clients[nb_net_clients] = optarg;
4577 7c9d8e07 bellard
                nb_net_clients++;
4578 702c651c bellard
                break;
4579 c7f74643 bellard
#ifdef CONFIG_SLIRP
4580 c7f74643 bellard
            case QEMU_OPTION_tftp:
4581 c7f74643 bellard
                tftp_prefix = optarg;
4582 9bf05444 bellard
                break;
4583 47d5d01a ths
            case QEMU_OPTION_bootp:
4584 47d5d01a ths
                bootp_filename = optarg;
4585 47d5d01a ths
                break;
4586 c94c8d64 bellard
#ifndef _WIN32
4587 9d728e8c bellard
            case QEMU_OPTION_smb:
4588 9d728e8c bellard
                net_slirp_smb(optarg);
4589 9d728e8c bellard
                break;
4590 c94c8d64 bellard
#endif
4591 9bf05444 bellard
            case QEMU_OPTION_redir:
4592 3b46e624 ths
                net_slirp_redir(optarg);
4593 9bf05444 bellard
                break;
4594 c7f74643 bellard
#endif
4595 dc72ac14 balrog
            case QEMU_OPTION_bt:
4596 dc72ac14 balrog
                if (nb_bt_opts >= MAX_BT_CMDLINE) {
4597 dc72ac14 balrog
                    fprintf(stderr, "qemu: too many bluetooth options\n");
4598 dc72ac14 balrog
                    exit(1);
4599 dc72ac14 balrog
                }
4600 dc72ac14 balrog
                bt_opts[nb_bt_opts++] = optarg;
4601 dc72ac14 balrog
                break;
4602 1d14ffa9 bellard
#ifdef HAS_AUDIO
4603 1d14ffa9 bellard
            case QEMU_OPTION_audio_help:
4604 1d14ffa9 bellard
                AUD_help ();
4605 1d14ffa9 bellard
                exit (0);
4606 1d14ffa9 bellard
                break;
4607 1d14ffa9 bellard
            case QEMU_OPTION_soundhw:
4608 1d14ffa9 bellard
                select_soundhw (optarg);
4609 1d14ffa9 bellard
                break;
4610 1d14ffa9 bellard
#endif
4611 cd6f1169 bellard
            case QEMU_OPTION_h:
4612 15f82208 ths
                help(0);
4613 cd6f1169 bellard
                break;
4614 9bd7e6d9 pbrook
            case QEMU_OPTION_version:
4615 9bd7e6d9 pbrook
                version();
4616 9bd7e6d9 pbrook
                exit(0);
4617 9bd7e6d9 pbrook
                break;
4618 00f82b8a aurel32
            case QEMU_OPTION_m: {
4619 00f82b8a aurel32
                uint64_t value;
4620 00f82b8a aurel32
                char *ptr;
4621 00f82b8a aurel32
4622 00f82b8a aurel32
                value = strtoul(optarg, &ptr, 10);
4623 00f82b8a aurel32
                switch (*ptr) {
4624 00f82b8a aurel32
                case 0: case 'M': case 'm':
4625 00f82b8a aurel32
                    value <<= 20;
4626 00f82b8a aurel32
                    break;
4627 00f82b8a aurel32
                case 'G': case 'g':
4628 00f82b8a aurel32
                    value <<= 30;
4629 00f82b8a aurel32
                    break;
4630 00f82b8a aurel32
                default:
4631 00f82b8a aurel32
                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
4632 cd6f1169 bellard
                    exit(1);
4633 cd6f1169 bellard
                }
4634 00f82b8a aurel32
4635 00f82b8a aurel32
                /* On 32-bit hosts, QEMU is limited by virtual address space */
4636 00f82b8a aurel32
                if (value > (2047 << 20)
4637 640f42e4 blueswir1
#ifndef CONFIG_KQEMU
4638 00f82b8a aurel32
                    && HOST_LONG_BITS == 32
4639 00f82b8a aurel32
#endif
4640 00f82b8a aurel32
                    ) {
4641 00f82b8a aurel32
                    fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4642 00f82b8a aurel32
                    exit(1);
4643 00f82b8a aurel32
                }
4644 00f82b8a aurel32
                if (value != (uint64_t)(ram_addr_t)value) {
4645 00f82b8a aurel32
                    fprintf(stderr, "qemu: ram size too large\n");
4646 00f82b8a aurel32
                    exit(1);
4647 00f82b8a aurel32
                }
4648 00f82b8a aurel32
                ram_size = value;
4649 cd6f1169 bellard
                break;
4650 00f82b8a aurel32
            }
4651 cd6f1169 bellard
            case QEMU_OPTION_d:
4652 cd6f1169 bellard
                {
4653 cd6f1169 bellard
                    int mask;
4654 c7cd6a37 blueswir1
                    const CPULogItem *item;
4655 3b46e624 ths
4656 cd6f1169 bellard
                    mask = cpu_str_to_log_mask(optarg);
4657 cd6f1169 bellard
                    if (!mask) {
4658 cd6f1169 bellard
                        printf("Log items (comma separated):\n");
4659 f193c797 bellard
                    for(item = cpu_log_items; item->mask != 0; item++) {
4660 f193c797 bellard
                        printf("%-10s %s\n", item->name, item->help);
4661 f193c797 bellard
                    }
4662 f193c797 bellard
                    exit(1);
4663 cd6f1169 bellard
                    }
4664 cd6f1169 bellard
                    cpu_set_log(mask);
4665 f193c797 bellard
                }
4666 cd6f1169 bellard
                break;
4667 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
4668 cd6f1169 bellard
            case QEMU_OPTION_s:
4669 59030a8c aliguori
                gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
4670 cd6f1169 bellard
                break;
4671 59030a8c aliguori
            case QEMU_OPTION_gdb:
4672 59030a8c aliguori
                gdbstub_dev = optarg;
4673 cd6f1169 bellard
                break;
4674 67b915a5 bellard
#endif
4675 cd6f1169 bellard
            case QEMU_OPTION_L:
4676 cd6f1169 bellard
                bios_dir = optarg;
4677 cd6f1169 bellard
                break;
4678 1192dad8 j_mayer
            case QEMU_OPTION_bios:
4679 1192dad8 j_mayer
                bios_name = optarg;
4680 1192dad8 j_mayer
                break;
4681 1b530a6d aurel32
            case QEMU_OPTION_singlestep:
4682 1b530a6d aurel32
                singlestep = 1;
4683 1b530a6d aurel32
                break;
4684 cd6f1169 bellard
            case QEMU_OPTION_S:
4685 3c07f8e8 pbrook
                autostart = 0;
4686 cd6f1169 bellard
                break;
4687 5824d651 blueswir1
#ifndef _WIN32
4688 3d11d0eb bellard
            case QEMU_OPTION_k:
4689 3d11d0eb bellard
                keyboard_layout = optarg;
4690 3d11d0eb bellard
                break;
4691 5824d651 blueswir1
#endif
4692 ee22c2f7 bellard
            case QEMU_OPTION_localtime:
4693 ee22c2f7 bellard
                rtc_utc = 0;
4694 ee22c2f7 bellard
                break;
4695 3893c124 malc
            case QEMU_OPTION_vga:
4696 3893c124 malc
                select_vgahw (optarg);
4697 1bfe856e bellard
                break;
4698 5824d651 blueswir1
#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4699 e9b137c2 bellard
            case QEMU_OPTION_g:
4700 e9b137c2 bellard
                {
4701 e9b137c2 bellard
                    const char *p;
4702 e9b137c2 bellard
                    int w, h, depth;
4703 e9b137c2 bellard
                    p = optarg;
4704 e9b137c2 bellard
                    w = strtol(p, (char **)&p, 10);
4705 e9b137c2 bellard
                    if (w <= 0) {
4706 e9b137c2 bellard
                    graphic_error:
4707 e9b137c2 bellard
                        fprintf(stderr, "qemu: invalid resolution or depth\n");
4708 e9b137c2 bellard
                        exit(1);
4709 e9b137c2 bellard
                    }
4710 e9b137c2 bellard
                    if (*p != 'x')
4711 e9b137c2 bellard
                        goto graphic_error;
4712 e9b137c2 bellard
                    p++;
4713 e9b137c2 bellard
                    h = strtol(p, (char **)&p, 10);
4714 e9b137c2 bellard
                    if (h <= 0)
4715 e9b137c2 bellard
                        goto graphic_error;
4716 e9b137c2 bellard
                    if (*p == 'x') {
4717 e9b137c2 bellard
                        p++;
4718 e9b137c2 bellard
                        depth = strtol(p, (char **)&p, 10);
4719 5fafdf24 ths
                        if (depth != 8 && depth != 15 && depth != 16 &&
4720 e9b137c2 bellard
                            depth != 24 && depth != 32)
4721 e9b137c2 bellard
                            goto graphic_error;
4722 e9b137c2 bellard
                    } else if (*p == '\0') {
4723 e9b137c2 bellard
                        depth = graphic_depth;
4724 e9b137c2 bellard
                    } else {
4725 e9b137c2 bellard
                        goto graphic_error;
4726 e9b137c2 bellard
                    }
4727 3b46e624 ths
4728 e9b137c2 bellard
                    graphic_width = w;
4729 e9b137c2 bellard
                    graphic_height = h;
4730 e9b137c2 bellard
                    graphic_depth = depth;
4731 e9b137c2 bellard
                }
4732 e9b137c2 bellard
                break;
4733 5824d651 blueswir1
#endif
4734 20d8a3ed ths
            case QEMU_OPTION_echr:
4735 20d8a3ed ths
                {
4736 20d8a3ed ths
                    char *r;
4737 20d8a3ed ths
                    term_escape_char = strtol(optarg, &r, 0);
4738 20d8a3ed ths
                    if (r == optarg)
4739 20d8a3ed ths
                        printf("Bad argument to echr\n");
4740 20d8a3ed ths
                    break;
4741 20d8a3ed ths
                }
4742 82c643ff bellard
            case QEMU_OPTION_monitor:
4743 fd5f393a pbrook
                monitor_device = optarg;
4744 82c643ff bellard
                break;
4745 82c643ff bellard
            case QEMU_OPTION_serial:
4746 8d11df9e bellard
                if (serial_device_index >= MAX_SERIAL_PORTS) {
4747 8d11df9e bellard
                    fprintf(stderr, "qemu: too many serial ports\n");
4748 8d11df9e bellard
                    exit(1);
4749 8d11df9e bellard
                }
4750 fd5f393a pbrook
                serial_devices[serial_device_index] = optarg;
4751 8d11df9e bellard
                serial_device_index++;
4752 82c643ff bellard
                break;
4753 51ecf136 aliguori
            case QEMU_OPTION_virtiocon:
4754 51ecf136 aliguori
                if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
4755 51ecf136 aliguori
                    fprintf(stderr, "qemu: too many virtio consoles\n");
4756 51ecf136 aliguori
                    exit(1);
4757 51ecf136 aliguori
                }
4758 51ecf136 aliguori
                virtio_consoles[virtio_console_index] = optarg;
4759 51ecf136 aliguori
                virtio_console_index++;
4760 51ecf136 aliguori
                break;
4761 6508fe59 bellard
            case QEMU_OPTION_parallel:
4762 6508fe59 bellard
                if (parallel_device_index >= MAX_PARALLEL_PORTS) {
4763 6508fe59 bellard
                    fprintf(stderr, "qemu: too many parallel ports\n");
4764 6508fe59 bellard
                    exit(1);
4765 6508fe59 bellard
                }
4766 fd5f393a pbrook
                parallel_devices[parallel_device_index] = optarg;
4767 6508fe59 bellard
                parallel_device_index++;
4768 6508fe59 bellard
                break;
4769 d63d307f bellard
            case QEMU_OPTION_loadvm:
4770 d63d307f bellard
                loadvm = optarg;
4771 d63d307f bellard
                break;
4772 d63d307f bellard
            case QEMU_OPTION_full_screen:
4773 d63d307f bellard
                full_screen = 1;
4774 d63d307f bellard
                break;
4775 667accab ths
#ifdef CONFIG_SDL
4776 43523e93 ths
            case QEMU_OPTION_no_frame:
4777 43523e93 ths
                no_frame = 1;
4778 43523e93 ths
                break;
4779 3780e197 ths
            case QEMU_OPTION_alt_grab:
4780 3780e197 ths
                alt_grab = 1;
4781 3780e197 ths
                break;
4782 667accab ths
            case QEMU_OPTION_no_quit:
4783 667accab ths
                no_quit = 1;
4784 667accab ths
                break;
4785 7d957bd8 aliguori
            case QEMU_OPTION_sdl:
4786 7d957bd8 aliguori
                sdl = 1;
4787 7d957bd8 aliguori
                break;
4788 667accab ths
#endif
4789 f7cce898 bellard
            case QEMU_OPTION_pidfile:
4790 93815bc2 ths
                pid_file = optarg;
4791 f7cce898 bellard
                break;
4792 a09db21f bellard
#ifdef TARGET_I386
4793 a09db21f bellard
            case QEMU_OPTION_win2k_hack:
4794 a09db21f bellard
                win2k_install_hack = 1;
4795 a09db21f bellard
                break;
4796 73822ec8 aliguori
            case QEMU_OPTION_rtc_td_hack:
4797 73822ec8 aliguori
                rtc_td_hack = 1;
4798 73822ec8 aliguori
                break;
4799 8a92ea2f aliguori
            case QEMU_OPTION_acpitable:
4800 8a92ea2f aliguori
                if(acpi_table_add(optarg) < 0) {
4801 8a92ea2f aliguori
                    fprintf(stderr, "Wrong acpi table provided\n");
4802 8a92ea2f aliguori
                    exit(1);
4803 8a92ea2f aliguori
                }
4804 8a92ea2f aliguori
                break;
4805 b6f6e3d3 aliguori
            case QEMU_OPTION_smbios:
4806 b6f6e3d3 aliguori
                if(smbios_entry_add(optarg) < 0) {
4807 b6f6e3d3 aliguori
                    fprintf(stderr, "Wrong smbios provided\n");
4808 b6f6e3d3 aliguori
                    exit(1);
4809 b6f6e3d3 aliguori
                }
4810 b6f6e3d3 aliguori
                break;
4811 a09db21f bellard
#endif
4812 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
4813 d993e026 bellard
            case QEMU_OPTION_no_kqemu:
4814 d993e026 bellard
                kqemu_allowed = 0;
4815 d993e026 bellard
                break;
4816 89bfc105 bellard
            case QEMU_OPTION_kernel_kqemu:
4817 89bfc105 bellard
                kqemu_allowed = 2;
4818 89bfc105 bellard
                break;
4819 d993e026 bellard
#endif
4820 7ba1e619 aliguori
#ifdef CONFIG_KVM
4821 7ba1e619 aliguori
            case QEMU_OPTION_enable_kvm:
4822 7ba1e619 aliguori
                kvm_allowed = 1;
4823 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
4824 7ba1e619 aliguori
                kqemu_allowed = 0;
4825 7ba1e619 aliguori
#endif
4826 7ba1e619 aliguori
                break;
4827 7ba1e619 aliguori
#endif
4828 bb36d470 bellard
            case QEMU_OPTION_usb:
4829 bb36d470 bellard
                usb_enabled = 1;
4830 bb36d470 bellard
                break;
4831 a594cfbf bellard
            case QEMU_OPTION_usbdevice:
4832 a594cfbf bellard
                usb_enabled = 1;
4833 0d92ed30 pbrook
                if (usb_devices_index >= MAX_USB_CMDLINE) {
4834 a594cfbf bellard
                    fprintf(stderr, "Too many USB devices\n");
4835 a594cfbf bellard
                    exit(1);
4836 a594cfbf bellard
                }
4837 fd5f393a pbrook
                usb_devices[usb_devices_index] = optarg;
4838 a594cfbf bellard
                usb_devices_index++;
4839 a594cfbf bellard
                break;
4840 6a00d601 bellard
            case QEMU_OPTION_smp:
4841 6a00d601 bellard
                smp_cpus = atoi(optarg);
4842 b2097003 aliguori
                if (smp_cpus < 1) {
4843 6a00d601 bellard
                    fprintf(stderr, "Invalid number of CPUs\n");
4844 6a00d601 bellard
                    exit(1);
4845 6a00d601 bellard
                }
4846 6a00d601 bellard
                break;
4847 24236869 bellard
            case QEMU_OPTION_vnc:
4848 73fc9742 ths
                vnc_display = optarg;
4849 24236869 bellard
                break;
4850 5824d651 blueswir1
#ifdef TARGET_I386
4851 6515b203 bellard
            case QEMU_OPTION_no_acpi:
4852 6515b203 bellard
                acpi_enabled = 0;
4853 6515b203 bellard
                break;
4854 16b29ae1 aliguori
            case QEMU_OPTION_no_hpet:
4855 16b29ae1 aliguori
                no_hpet = 1;
4856 16b29ae1 aliguori
                break;
4857 5824d651 blueswir1
#endif
4858 d1beab82 bellard
            case QEMU_OPTION_no_reboot:
4859 d1beab82 bellard
                no_reboot = 1;
4860 d1beab82 bellard
                break;
4861 b2f76161 aurel32
            case QEMU_OPTION_no_shutdown:
4862 b2f76161 aurel32
                no_shutdown = 1;
4863 b2f76161 aurel32
                break;
4864 9467cd46 balrog
            case QEMU_OPTION_show_cursor:
4865 9467cd46 balrog
                cursor_hide = 0;
4866 9467cd46 balrog
                break;
4867 8fcb1b90 blueswir1
            case QEMU_OPTION_uuid:
4868 8fcb1b90 blueswir1
                if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
4869 8fcb1b90 blueswir1
                    fprintf(stderr, "Fail to parse UUID string."
4870 8fcb1b90 blueswir1
                            " Wrong format.\n");
4871 8fcb1b90 blueswir1
                    exit(1);
4872 8fcb1b90 blueswir1
                }
4873 8fcb1b90 blueswir1
                break;
4874 5824d651 blueswir1
#ifndef _WIN32
4875 71e3ceb8 ths
            case QEMU_OPTION_daemonize:
4876 71e3ceb8 ths
                daemonize = 1;
4877 71e3ceb8 ths
                break;
4878 5824d651 blueswir1
#endif
4879 9ae02555 ths
            case QEMU_OPTION_option_rom:
4880 9ae02555 ths
                if (nb_option_roms >= MAX_OPTION_ROMS) {
4881 9ae02555 ths
                    fprintf(stderr, "Too many option ROMs\n");
4882 9ae02555 ths
                    exit(1);
4883 9ae02555 ths
                }
4884 9ae02555 ths
                option_rom[nb_option_roms] = optarg;
4885 9ae02555 ths
                nb_option_roms++;
4886 9ae02555 ths
                break;
4887 5824d651 blueswir1
#if defined(TARGET_ARM) || defined(TARGET_M68K)
4888 8e71621f pbrook
            case QEMU_OPTION_semihosting:
4889 8e71621f pbrook
                semihosting_enabled = 1;
4890 8e71621f pbrook
                break;
4891 5824d651 blueswir1
#endif
4892 c35734b2 ths
            case QEMU_OPTION_name:
4893 c35734b2 ths
                qemu_name = optarg;
4894 c35734b2 ths
                break;
4895 95efd11c blueswir1
#if defined(TARGET_SPARC) || defined(TARGET_PPC)
4896 66508601 blueswir1
            case QEMU_OPTION_prom_env:
4897 66508601 blueswir1
                if (nb_prom_envs >= MAX_PROM_ENVS) {
4898 66508601 blueswir1
                    fprintf(stderr, "Too many prom variables\n");
4899 66508601 blueswir1
                    exit(1);
4900 66508601 blueswir1
                }
4901 66508601 blueswir1
                prom_envs[nb_prom_envs] = optarg;
4902 66508601 blueswir1
                nb_prom_envs++;
4903 66508601 blueswir1
                break;
4904 66508601 blueswir1
#endif
4905 2b8f2d41 balrog
#ifdef TARGET_ARM
4906 2b8f2d41 balrog
            case QEMU_OPTION_old_param:
4907 2b8f2d41 balrog
                old_param = 1;
4908 05ebd537 ths
                break;
4909 2b8f2d41 balrog
#endif
4910 f3dcfada ths
            case QEMU_OPTION_clock:
4911 f3dcfada ths
                configure_alarms(optarg);
4912 f3dcfada ths
                break;
4913 7e0af5d0 bellard
            case QEMU_OPTION_startdate:
4914 7e0af5d0 bellard
                {
4915 7e0af5d0 bellard
                    struct tm tm;
4916 f6503059 balrog
                    time_t rtc_start_date;
4917 7e0af5d0 bellard
                    if (!strcmp(optarg, "now")) {
4918 f6503059 balrog
                        rtc_date_offset = -1;
4919 7e0af5d0 bellard
                    } else {
4920 7e0af5d0 bellard
                        if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
4921 7e0af5d0 bellard
                               &tm.tm_year,
4922 7e0af5d0 bellard
                               &tm.tm_mon,
4923 7e0af5d0 bellard
                               &tm.tm_mday,
4924 7e0af5d0 bellard
                               &tm.tm_hour,
4925 7e0af5d0 bellard
                               &tm.tm_min,
4926 7e0af5d0 bellard
                               &tm.tm_sec) == 6) {
4927 7e0af5d0 bellard
                            /* OK */
4928 7e0af5d0 bellard
                        } else if (sscanf(optarg, "%d-%d-%d",
4929 7e0af5d0 bellard
                                          &tm.tm_year,
4930 7e0af5d0 bellard
                                          &tm.tm_mon,
4931 7e0af5d0 bellard
                                          &tm.tm_mday) == 3) {
4932 7e0af5d0 bellard
                            tm.tm_hour = 0;
4933 7e0af5d0 bellard
                            tm.tm_min = 0;
4934 7e0af5d0 bellard
                            tm.tm_sec = 0;
4935 7e0af5d0 bellard
                        } else {
4936 7e0af5d0 bellard
                            goto date_fail;
4937 7e0af5d0 bellard
                        }
4938 7e0af5d0 bellard
                        tm.tm_year -= 1900;
4939 7e0af5d0 bellard
                        tm.tm_mon--;
4940 3c6b2088 bellard
                        rtc_start_date = mktimegm(&tm);
4941 7e0af5d0 bellard
                        if (rtc_start_date == -1) {
4942 7e0af5d0 bellard
                        date_fail:
4943 7e0af5d0 bellard
                            fprintf(stderr, "Invalid date format. Valid format are:\n"
4944 7e0af5d0 bellard
                                    "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
4945 7e0af5d0 bellard
                            exit(1);
4946 7e0af5d0 bellard
                        }
4947 f6503059 balrog
                        rtc_date_offset = time(NULL) - rtc_start_date;
4948 7e0af5d0 bellard
                    }
4949 7e0af5d0 bellard
                }
4950 7e0af5d0 bellard
                break;
4951 26a5f13b bellard
            case QEMU_OPTION_tb_size:
4952 26a5f13b bellard
                tb_size = strtol(optarg, NULL, 0);
4953 26a5f13b bellard
                if (tb_size < 0)
4954 26a5f13b bellard
                    tb_size = 0;
4955 26a5f13b bellard
                break;
4956 2e70f6ef pbrook
            case QEMU_OPTION_icount:
4957 2e70f6ef pbrook
                use_icount = 1;
4958 2e70f6ef pbrook
                if (strcmp(optarg, "auto") == 0) {
4959 2e70f6ef pbrook
                    icount_time_shift = -1;
4960 2e70f6ef pbrook
                } else {
4961 2e70f6ef pbrook
                    icount_time_shift = strtol(optarg, NULL, 0);
4962 2e70f6ef pbrook
                }
4963 2e70f6ef pbrook
                break;
4964 5bb7910a aliguori
            case QEMU_OPTION_incoming:
4965 5bb7910a aliguori
                incoming = optarg;
4966 5bb7910a aliguori
                break;
4967 5824d651 blueswir1
#ifndef _WIN32
4968 0858532e aliguori
            case QEMU_OPTION_chroot:
4969 0858532e aliguori
                chroot_dir = optarg;
4970 0858532e aliguori
                break;
4971 0858532e aliguori
            case QEMU_OPTION_runas:
4972 0858532e aliguori
                run_as = optarg;
4973 0858532e aliguori
                break;
4974 5824d651 blueswir1
#endif
4975 cd6f1169 bellard
            }
4976 0824d6fc bellard
        }
4977 0824d6fc bellard
    }
4978 330d0414 bellard
4979 640f42e4 blueswir1
#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
4980 7ba1e619 aliguori
    if (kvm_allowed && kqemu_allowed) {
4981 7ba1e619 aliguori
        fprintf(stderr,
4982 7ba1e619 aliguori
                "You can not enable both KVM and kqemu at the same time\n");
4983 7ba1e619 aliguori
        exit(1);
4984 7ba1e619 aliguori
    }
4985 7ba1e619 aliguori
#endif
4986 7ba1e619 aliguori
4987 3d878caa balrog
    machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
4988 b2097003 aliguori
    if (smp_cpus > machine->max_cpus) {
4989 b2097003 aliguori
        fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
4990 b2097003 aliguori
                "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
4991 b2097003 aliguori
                machine->max_cpus);
4992 b2097003 aliguori
        exit(1);
4993 b2097003 aliguori
    }
4994 b2097003 aliguori
4995 bc0129d9 aliguori
    if (nographic) {
4996 bc0129d9 aliguori
       if (serial_device_index == 0)
4997 bc0129d9 aliguori
           serial_devices[0] = "stdio";
4998 bc0129d9 aliguori
       if (parallel_device_index == 0)
4999 bc0129d9 aliguori
           parallel_devices[0] = "null";
5000 bc0129d9 aliguori
       if (strncmp(monitor_device, "vc", 2) == 0)
5001 bc0129d9 aliguori
           monitor_device = "stdio";
5002 bc0129d9 aliguori
    }
5003 bc0129d9 aliguori
5004 71e3ceb8 ths
#ifndef _WIN32
5005 71e3ceb8 ths
    if (daemonize) {
5006 71e3ceb8 ths
        pid_t pid;
5007 71e3ceb8 ths
5008 71e3ceb8 ths
        if (pipe(fds) == -1)
5009 71e3ceb8 ths
            exit(1);
5010 71e3ceb8 ths
5011 71e3ceb8 ths
        pid = fork();
5012 71e3ceb8 ths
        if (pid > 0) {
5013 71e3ceb8 ths
            uint8_t status;
5014 71e3ceb8 ths
            ssize_t len;
5015 71e3ceb8 ths
5016 71e3ceb8 ths
            close(fds[1]);
5017 71e3ceb8 ths
5018 71e3ceb8 ths
        again:
5019 93815bc2 ths
            len = read(fds[0], &status, 1);
5020 93815bc2 ths
            if (len == -1 && (errno == EINTR))
5021 93815bc2 ths
                goto again;
5022 93815bc2 ths
5023 93815bc2 ths
            if (len != 1)
5024 93815bc2 ths
                exit(1);
5025 93815bc2 ths
            else if (status == 1) {
5026 93815bc2 ths
                fprintf(stderr, "Could not acquire pidfile\n");
5027 93815bc2 ths
                exit(1);
5028 93815bc2 ths
            } else
5029 93815bc2 ths
                exit(0);
5030 71e3ceb8 ths
        } else if (pid < 0)
5031 93815bc2 ths
            exit(1);
5032 71e3ceb8 ths
5033 71e3ceb8 ths
        setsid();
5034 71e3ceb8 ths
5035 71e3ceb8 ths
        pid = fork();
5036 71e3ceb8 ths
        if (pid > 0)
5037 71e3ceb8 ths
            exit(0);
5038 71e3ceb8 ths
        else if (pid < 0)
5039 71e3ceb8 ths
            exit(1);
5040 71e3ceb8 ths
5041 71e3ceb8 ths
        umask(027);
5042 71e3ceb8 ths
5043 71e3ceb8 ths
        signal(SIGTSTP, SIG_IGN);
5044 71e3ceb8 ths
        signal(SIGTTOU, SIG_IGN);
5045 71e3ceb8 ths
        signal(SIGTTIN, SIG_IGN);
5046 71e3ceb8 ths
    }
5047 71e3ceb8 ths
5048 aa26bb2d ths
    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
5049 93815bc2 ths
        if (daemonize) {
5050 93815bc2 ths
            uint8_t status = 1;
5051 93815bc2 ths
            write(fds[1], &status, 1);
5052 93815bc2 ths
        } else
5053 93815bc2 ths
            fprintf(stderr, "Could not acquire pid file\n");
5054 93815bc2 ths
        exit(1);
5055 93815bc2 ths
    }
5056 b9e82a59 blueswir1
#endif
5057 93815bc2 ths
5058 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
5059 ff3fbb30 bellard
    if (smp_cpus > 1)
5060 ff3fbb30 bellard
        kqemu_allowed = 0;
5061 ff3fbb30 bellard
#endif
5062 a20dd508 bellard
    linux_boot = (kernel_filename != NULL);
5063 7317b8ca balrog
    net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5064 6c41b272 balrog
5065 f8d39c01 ths
    if (!linux_boot && *kernel_cmdline != '\0') {
5066 f8d39c01 ths
        fprintf(stderr, "-append only allowed with -kernel option\n");
5067 f8d39c01 ths
        exit(1);
5068 f8d39c01 ths
    }
5069 f8d39c01 ths
5070 f8d39c01 ths
    if (!linux_boot && initrd_filename != NULL) {
5071 f8d39c01 ths
        fprintf(stderr, "-initrd only allowed with -kernel option\n");
5072 f8d39c01 ths
        exit(1);
5073 f8d39c01 ths
    }
5074 f8d39c01 ths
5075 96d30e48 ths
    /* boot to floppy or the default cd if no hard disk defined yet */
5076 28c5af54 j_mayer
    if (!boot_devices[0]) {
5077 e4bcb14c ths
        boot_devices = "cad";
5078 96d30e48 ths
    }
5079 b118d61e bellard
    setvbuf(stdout, NULL, _IOLBF, 0);
5080 3b46e624 ths
5081 634fce96 pbrook
    init_timers();
5082 7183b4b4 aliguori
    if (init_timer_alarm() < 0) {
5083 7183b4b4 aliguori
        fprintf(stderr, "could not initialize alarm timer\n");
5084 7183b4b4 aliguori
        exit(1);
5085 7183b4b4 aliguori
    }
5086 2e70f6ef pbrook
    if (use_icount && icount_time_shift < 0) {
5087 2e70f6ef pbrook
        use_icount = 2;
5088 2e70f6ef pbrook
        /* 125MIPS seems a reasonable initial guess at the guest speed.
5089 2e70f6ef pbrook
           It will be corrected fairly quickly anyway.  */
5090 2e70f6ef pbrook
        icount_time_shift = 3;
5091 2e70f6ef pbrook
        init_icount_adjust();
5092 2e70f6ef pbrook
    }
5093 634fce96 pbrook
5094 fd1dff4b bellard
#ifdef _WIN32
5095 fd1dff4b bellard
    socket_init();
5096 fd1dff4b bellard
#endif
5097 fd1dff4b bellard
5098 7c9d8e07 bellard
    /* init network clients */
5099 7c9d8e07 bellard
    if (nb_net_clients == 0) {
5100 7c9d8e07 bellard
        /* if no clients, we use a default config */
5101 f441b28b aliguori
        net_clients[nb_net_clients++] = "nic";
5102 f441b28b aliguori
#ifdef CONFIG_SLIRP
5103 f441b28b aliguori
        net_clients[nb_net_clients++] = "user";
5104 f441b28b aliguori
#endif
5105 c20709aa bellard
    }
5106 c20709aa bellard
5107 7c9d8e07 bellard
    for(i = 0;i < nb_net_clients; i++) {
5108 9ad97e65 balrog
        if (net_client_parse(net_clients[i]) < 0)
5109 7c9d8e07 bellard
            exit(1);
5110 702c651c bellard
    }
5111 63a01ef8 aliguori
    net_client_check();
5112 f1510b2c bellard
5113 eec85c2a ths
#ifdef TARGET_I386
5114 ed494d87 balrog
    /* XXX: this should be moved in the PC machine instantiation code */
5115 28c5af54 j_mayer
    if (net_boot != 0) {
5116 28c5af54 j_mayer
        int netroms = 0;
5117 28c5af54 j_mayer
        for (i = 0; i < nb_nics && i < 4; i++) {
5118 eec85c2a ths
            const char *model = nd_table[i].model;
5119 eec85c2a ths
            char buf[1024];
5120 28c5af54 j_mayer
            if (net_boot & (1 << i)) {
5121 28c5af54 j_mayer
                if (model == NULL)
5122 28c5af54 j_mayer
                    model = "ne2k_pci";
5123 28c5af54 j_mayer
                snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
5124 28c5af54 j_mayer
                if (get_image_size(buf) > 0) {
5125 28c5af54 j_mayer
                    if (nb_option_roms >= MAX_OPTION_ROMS) {
5126 28c5af54 j_mayer
                        fprintf(stderr, "Too many option ROMs\n");
5127 28c5af54 j_mayer
                        exit(1);
5128 28c5af54 j_mayer
                    }
5129 28c5af54 j_mayer
                    option_rom[nb_option_roms] = strdup(buf);
5130 28c5af54 j_mayer
                    nb_option_roms++;
5131 28c5af54 j_mayer
                    netroms++;
5132 28c5af54 j_mayer
                }
5133 28c5af54 j_mayer
            }
5134 eec85c2a ths
        }
5135 28c5af54 j_mayer
        if (netroms == 0) {
5136 eec85c2a ths
            fprintf(stderr, "No valid PXE rom found for network device\n");
5137 eec85c2a ths
            exit(1);
5138 eec85c2a ths
        }
5139 eec85c2a ths
    }
5140 eec85c2a ths
#endif
5141 eec85c2a ths
5142 dc72ac14 balrog
    /* init the bluetooth world */
5143 dc72ac14 balrog
    for (i = 0; i < nb_bt_opts; i++)
5144 dc72ac14 balrog
        if (bt_parse(bt_opts[i]))
5145 dc72ac14 balrog
            exit(1);
5146 dc72ac14 balrog
5147 0824d6fc bellard
    /* init the memory */
5148 94a6b54f pbrook
    if (ram_size == 0)
5149 94a6b54f pbrook
        ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5150 9ae02555 ths
5151 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
5152 94a6b54f pbrook
    /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5153 94a6b54f pbrook
       guest ram allocation.  It needs to go away.  */
5154 94a6b54f pbrook
    if (kqemu_allowed) {
5155 94a6b54f pbrook
        kqemu_phys_ram_size = ram_size + VGA_RAM_SIZE + 4 * 1024 * 1024;
5156 94a6b54f pbrook
        kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5157 94a6b54f pbrook
        if (!kqemu_phys_ram_base) {
5158 94a6b54f pbrook
            fprintf(stderr, "Could not allocate physical memory\n");
5159 94a6b54f pbrook
            exit(1);
5160 94a6b54f pbrook
        }
5161 0824d6fc bellard
    }
5162 94a6b54f pbrook
#endif
5163 0824d6fc bellard
5164 26a5f13b bellard
    /* init the dynamic translator */
5165 26a5f13b bellard
    cpu_exec_init_all(tb_size * 1024 * 1024);
5166 26a5f13b bellard
5167 5905b2e5 bellard
    bdrv_init();
5168 6512a2a7 aliguori
    dma_helper_init();
5169 c4b1fcc0 bellard
5170 e4bcb14c ths
    /* we always create the cdrom drive, even if no disk is there */
5171 c4b1fcc0 bellard
5172 e4bcb14c ths
    if (nb_drives_opt < MAX_DRIVES)
5173 609497ab balrog
        drive_add(NULL, CDROM_ALIAS);
5174 c4b1fcc0 bellard
5175 9d413d1d balrog
    /* we always create at least one floppy */
5176 33e3963e bellard
5177 e4bcb14c ths
    if (nb_drives_opt < MAX_DRIVES)
5178 609497ab balrog
        drive_add(NULL, FD_ALIAS, 0);
5179 86f55663 j_mayer
5180 9d413d1d balrog
    /* we always create one sd slot, even if no card is in it */
5181 9d413d1d balrog
5182 9d413d1d balrog
    if (nb_drives_opt < MAX_DRIVES)
5183 609497ab balrog
        drive_add(NULL, SD_ALIAS);
5184 9d413d1d balrog
5185 e4bcb14c ths
    /* open the virtual block devices */
5186 e4bcb14c ths
5187 e4bcb14c ths
    for(i = 0; i < nb_drives_opt; i++)
5188 609497ab balrog
        if (drive_init(&drives_opt[i], snapshot, machine) == -1)
5189 e4bcb14c ths
            exit(1);
5190 3e3d5815 balrog
5191 c88676f8 bellard
    register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
5192 475e4277 aliguori
    register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
5193 8a7ddc38 bellard
5194 3023f332 aliguori
#ifndef _WIN32
5195 3023f332 aliguori
    /* must be after terminal init, SDL library changes signal handlers */
5196 3023f332 aliguori
    termsig_setup();
5197 3023f332 aliguori
#endif
5198 3023f332 aliguori
5199 3023f332 aliguori
    /* Maintain compatibility with multiple stdio monitors */
5200 3023f332 aliguori
    if (!strcmp(monitor_device,"stdio")) {
5201 3023f332 aliguori
        for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5202 3023f332 aliguori
            const char *devname = serial_devices[i];
5203 3023f332 aliguori
            if (devname && !strcmp(devname,"mon:stdio")) {
5204 3023f332 aliguori
                monitor_device = NULL;
5205 3023f332 aliguori
                break;
5206 3023f332 aliguori
            } else if (devname && !strcmp(devname,"stdio")) {
5207 3023f332 aliguori
                monitor_device = NULL;
5208 3023f332 aliguori
                serial_devices[i] = "mon:stdio";
5209 3023f332 aliguori
                break;
5210 3023f332 aliguori
            }
5211 3023f332 aliguori
        }
5212 3023f332 aliguori
    }
5213 3023f332 aliguori
5214 3023f332 aliguori
    if (kvm_enabled()) {
5215 3023f332 aliguori
        int ret;
5216 3023f332 aliguori
5217 3023f332 aliguori
        ret = kvm_init(smp_cpus);
5218 3023f332 aliguori
        if (ret < 0) {
5219 3023f332 aliguori
            fprintf(stderr, "failed to initialize KVM\n");
5220 3023f332 aliguori
            exit(1);
5221 3023f332 aliguori
        }
5222 3023f332 aliguori
    }
5223 3023f332 aliguori
5224 4c621805 aliguori
    if (monitor_device) {
5225 ceecf1d1 aurel32
        monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
5226 4c621805 aliguori
        if (!monitor_hd) {
5227 4c621805 aliguori
            fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5228 4c621805 aliguori
            exit(1);
5229 4c621805 aliguori
        }
5230 4c621805 aliguori
    }
5231 4c621805 aliguori
5232 2796dae0 aliguori
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5233 2796dae0 aliguori
        const char *devname = serial_devices[i];
5234 2796dae0 aliguori
        if (devname && strcmp(devname, "none")) {
5235 2796dae0 aliguori
            char label[32];
5236 2796dae0 aliguori
            snprintf(label, sizeof(label), "serial%d", i);
5237 ceecf1d1 aurel32
            serial_hds[i] = qemu_chr_open(label, devname, NULL);
5238 2796dae0 aliguori
            if (!serial_hds[i]) {
5239 2796dae0 aliguori
                fprintf(stderr, "qemu: could not open serial device '%s'\n",
5240 2796dae0 aliguori
                        devname);
5241 2796dae0 aliguori
                exit(1);
5242 2796dae0 aliguori
            }
5243 2796dae0 aliguori
        }
5244 2796dae0 aliguori
    }
5245 2796dae0 aliguori
5246 2796dae0 aliguori
    for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5247 2796dae0 aliguori
        const char *devname = parallel_devices[i];
5248 2796dae0 aliguori
        if (devname && strcmp(devname, "none")) {
5249 2796dae0 aliguori
            char label[32];
5250 2796dae0 aliguori
            snprintf(label, sizeof(label), "parallel%d", i);
5251 ceecf1d1 aurel32
            parallel_hds[i] = qemu_chr_open(label, devname, NULL);
5252 2796dae0 aliguori
            if (!parallel_hds[i]) {
5253 2796dae0 aliguori
                fprintf(stderr, "qemu: could not open parallel device '%s'\n",
5254 2796dae0 aliguori
                        devname);
5255 2796dae0 aliguori
                exit(1);
5256 2796dae0 aliguori
            }
5257 2796dae0 aliguori
        }
5258 2796dae0 aliguori
    }
5259 2796dae0 aliguori
5260 2796dae0 aliguori
    for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5261 2796dae0 aliguori
        const char *devname = virtio_consoles[i];
5262 2796dae0 aliguori
        if (devname && strcmp(devname, "none")) {
5263 2796dae0 aliguori
            char label[32];
5264 2796dae0 aliguori
            snprintf(label, sizeof(label), "virtcon%d", i);
5265 ceecf1d1 aurel32
            virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
5266 2796dae0 aliguori
            if (!virtcon_hds[i]) {
5267 2796dae0 aliguori
                fprintf(stderr, "qemu: could not open virtio console '%s'\n",
5268 2796dae0 aliguori
                        devname);
5269 2796dae0 aliguori
                exit(1);
5270 2796dae0 aliguori
            }
5271 2796dae0 aliguori
        }
5272 2796dae0 aliguori
    }
5273 2796dae0 aliguori
5274 3023f332 aliguori
    machine->init(ram_size, vga_ram_size, boot_devices,
5275 3023f332 aliguori
                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5276 3023f332 aliguori
5277 6f338c34 aliguori
    current_machine = machine;
5278 6f338c34 aliguori
5279 3023f332 aliguori
    /* Set KVM's vcpu state to qemu's initial CPUState. */
5280 3023f332 aliguori
    if (kvm_enabled()) {
5281 3023f332 aliguori
        int ret;
5282 3023f332 aliguori
5283 3023f332 aliguori
        ret = kvm_sync_vcpus();
5284 3023f332 aliguori
        if (ret < 0) {
5285 3023f332 aliguori
            fprintf(stderr, "failed to initialize vcpus\n");
5286 3023f332 aliguori
            exit(1);
5287 3023f332 aliguori
        }
5288 3023f332 aliguori
    }
5289 3023f332 aliguori
5290 3023f332 aliguori
    /* init USB devices */
5291 3023f332 aliguori
    if (usb_enabled) {
5292 3023f332 aliguori
        for(i = 0; i < usb_devices_index; i++) {
5293 c0f4ce77 aliguori
            if (usb_device_add(usb_devices[i], 0) < 0) {
5294 3023f332 aliguori
                fprintf(stderr, "Warning: could not add USB device %s\n",
5295 3023f332 aliguori
                        usb_devices[i]);
5296 3023f332 aliguori
            }
5297 3023f332 aliguori
        }
5298 3023f332 aliguori
    }
5299 3023f332 aliguori
5300 8f391ab4 aliguori
    if (!display_state)
5301 8f391ab4 aliguori
        dumb_display_init();
5302 3023f332 aliguori
    /* just use the first displaystate for the moment */
5303 3023f332 aliguori
    ds = display_state;
5304 313aa567 bellard
    /* terminal init */
5305 a20dd508 bellard
    if (nographic) {
5306 4d3b6f6e balrog
        if (curses) {
5307 4d3b6f6e balrog
            fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
5308 4d3b6f6e balrog
            exit(1);
5309 4d3b6f6e balrog
        }
5310 7d957bd8 aliguori
    } else { 
5311 4d3b6f6e balrog
#if defined(CONFIG_CURSES)
5312 7d957bd8 aliguori
            if (curses) {
5313 7d957bd8 aliguori
                /* At the moment curses cannot be used with other displays */
5314 7d957bd8 aliguori
                curses_display_init(ds, full_screen);
5315 7d957bd8 aliguori
            } else
5316 4d3b6f6e balrog
#endif
5317 7d957bd8 aliguori
            {
5318 7d957bd8 aliguori
                if (vnc_display != NULL) {
5319 7d957bd8 aliguori
                    vnc_display_init(ds);
5320 7d957bd8 aliguori
                    if (vnc_display_open(ds, vnc_display) < 0)
5321 7d957bd8 aliguori
                        exit(1);
5322 7d957bd8 aliguori
                }
5323 5b0753e0 bellard
#if defined(CONFIG_SDL)
5324 d268de04 aliguori
                if (sdl || !vnc_display)
5325 7d957bd8 aliguori
                    sdl_display_init(ds, full_screen, no_frame);
5326 5b0753e0 bellard
#elif defined(CONFIG_COCOA)
5327 d268de04 aliguori
                if (sdl || !vnc_display)
5328 7d957bd8 aliguori
                    cocoa_display_init(ds, full_screen);
5329 313aa567 bellard
#endif
5330 7d957bd8 aliguori
            }
5331 313aa567 bellard
    }
5332 7d957bd8 aliguori
    dpy_resize(ds);
5333 5b08fc10 aliguori
5334 3023f332 aliguori
    dcl = ds->listeners;
5335 3023f332 aliguori
    while (dcl != NULL) {
5336 3023f332 aliguori
        if (dcl->dpy_refresh != NULL) {
5337 3023f332 aliguori
            ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5338 3023f332 aliguori
            qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
5339 20d8a3ed ths
        }
5340 3023f332 aliguori
        dcl = dcl->next;
5341 20d8a3ed ths
    }
5342 3023f332 aliguori
5343 9043b62d blueswir1
    if (nographic || (vnc_display && !sdl)) {
5344 9043b62d blueswir1
        nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5345 9043b62d blueswir1
        qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5346 9043b62d blueswir1
    }
5347 9043b62d blueswir1
5348 2796dae0 aliguori
    text_consoles_set_display(display_state);
5349 2970a6c9 aliguori
    qemu_chr_initial_reset();
5350 2796dae0 aliguori
5351 4c621805 aliguori
    if (monitor_device && monitor_hd)
5352 cde76ee1 aliguori
        monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
5353 82c643ff bellard
5354 8d11df9e bellard
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5355 c03b0f0f bellard
        const char *devname = serial_devices[i];
5356 fd5f393a pbrook
        if (devname && strcmp(devname, "none")) {
5357 5ccfae10 aliguori
            char label[32];
5358 5ccfae10 aliguori
            snprintf(label, sizeof(label), "serial%d", i);
5359 af3a9031 ths
            if (strstart(devname, "vc", 0))
5360 7ba1260a bellard
                qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
5361 8d11df9e bellard
        }
5362 82c643ff bellard
    }
5363 82c643ff bellard
5364 6508fe59 bellard
    for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5365 c03b0f0f bellard
        const char *devname = parallel_devices[i];
5366 fd5f393a pbrook
        if (devname && strcmp(devname, "none")) {
5367 5ccfae10 aliguori
            char label[32];
5368 5ccfae10 aliguori
            snprintf(label, sizeof(label), "parallel%d", i);
5369 af3a9031 ths
            if (strstart(devname, "vc", 0))
5370 7ba1260a bellard
                qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
5371 6508fe59 bellard
        }
5372 6508fe59 bellard
    }
5373 6508fe59 bellard
5374 9ede2fde aliguori
    for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5375 9ede2fde aliguori
        const char *devname = virtio_consoles[i];
5376 2796dae0 aliguori
        if (virtcon_hds[i] && devname) {
5377 9ede2fde aliguori
            char label[32];
5378 9ede2fde aliguori
            snprintf(label, sizeof(label), "virtcon%d", i);
5379 9ede2fde aliguori
            if (strstart(devname, "vc", 0))
5380 9ede2fde aliguori
                qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5381 9ede2fde aliguori
        }
5382 9ede2fde aliguori
    }
5383 9ede2fde aliguori
5384 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
5385 59030a8c aliguori
    if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
5386 59030a8c aliguori
        fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
5387 59030a8c aliguori
                gdbstub_dev);
5388 59030a8c aliguori
        exit(1);
5389 45669e00 balrog
    }
5390 67b915a5 bellard
#endif
5391 45669e00 balrog
5392 d63d307f bellard
    if (loadvm)
5393 376253ec aliguori
        do_loadvm(cur_mon, loadvm);
5394 d63d307f bellard
5395 5bb7910a aliguori
    if (incoming) {
5396 5bb7910a aliguori
        autostart = 0; /* fixme how to deal with -daemonize */
5397 5bb7910a aliguori
        qemu_start_incoming_migration(incoming);
5398 5bb7910a aliguori
    }
5399 5bb7910a aliguori
5400 c0f4ce77 aliguori
    if (autostart)
5401 c0f4ce77 aliguori
        vm_start();
5402 ffd843bc ths
5403 b9e82a59 blueswir1
#ifndef _WIN32
5404 71e3ceb8 ths
    if (daemonize) {
5405 71e3ceb8 ths
        uint8_t status = 0;
5406 71e3ceb8 ths
        ssize_t len;
5407 71e3ceb8 ths
5408 71e3ceb8 ths
    again1:
5409 71e3ceb8 ths
        len = write(fds[1], &status, 1);
5410 71e3ceb8 ths
        if (len == -1 && (errno == EINTR))
5411 71e3ceb8 ths
            goto again1;
5412 71e3ceb8 ths
5413 71e3ceb8 ths
        if (len != 1)
5414 71e3ceb8 ths
            exit(1);
5415 71e3ceb8 ths
5416 bd54b863 aliguori
        chdir("/");
5417 aeb30be6 balrog
        TFR(fd = open("/dev/null", O_RDWR));
5418 71e3ceb8 ths
        if (fd == -1)
5419 71e3ceb8 ths
            exit(1);
5420 0858532e aliguori
    }
5421 71e3ceb8 ths
5422 0858532e aliguori
    if (run_as) {
5423 0858532e aliguori
        pwd = getpwnam(run_as);
5424 0858532e aliguori
        if (!pwd) {
5425 0858532e aliguori
            fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
5426 0858532e aliguori
            exit(1);
5427 0858532e aliguori
        }
5428 0858532e aliguori
    }
5429 0858532e aliguori
5430 0858532e aliguori
    if (chroot_dir) {
5431 0858532e aliguori
        if (chroot(chroot_dir) < 0) {
5432 0858532e aliguori
            fprintf(stderr, "chroot failed\n");
5433 0858532e aliguori
            exit(1);
5434 0858532e aliguori
        }
5435 0858532e aliguori
        chdir("/");
5436 0858532e aliguori
    }
5437 0858532e aliguori
5438 0858532e aliguori
    if (run_as) {
5439 0858532e aliguori
        if (setgid(pwd->pw_gid) < 0) {
5440 0858532e aliguori
            fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
5441 0858532e aliguori
            exit(1);
5442 0858532e aliguori
        }
5443 0858532e aliguori
        if (setuid(pwd->pw_uid) < 0) {
5444 0858532e aliguori
            fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
5445 0858532e aliguori
            exit(1);
5446 0858532e aliguori
        }
5447 0858532e aliguori
        if (setuid(0) != -1) {
5448 0858532e aliguori
            fprintf(stderr, "Dropping privileges failed\n");
5449 0858532e aliguori
            exit(1);
5450 0858532e aliguori
        }
5451 0858532e aliguori
    }
5452 0858532e aliguori
5453 0858532e aliguori
    if (daemonize) {
5454 0858532e aliguori
        dup2(fd, 0);
5455 0858532e aliguori
        dup2(fd, 1);
5456 0858532e aliguori
        dup2(fd, 2);
5457 71e3ceb8 ths
5458 0858532e aliguori
        close(fd);
5459 71e3ceb8 ths
    }
5460 b9e82a59 blueswir1
#endif
5461 71e3ceb8 ths
5462 8a7ddc38 bellard
    main_loop();
5463 40c3bac3 bellard
    quit_timers();
5464 63a01ef8 aliguori
    net_cleanup();
5465 b46a8906 ths
5466 0824d6fc bellard
    return 0;
5467 0824d6fc bellard
}