Statistics
| Branch: | Revision:

root / vl.c @ 4870852c

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