Statistics
| Branch: | Revision:

root / vl.c @ d9f75a4e

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