Statistics
| Branch: | Revision:

root / vl.c @ 2ac71179

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