Statistics
| Branch: | Revision:

root / vl.c @ 3b0ba927

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