Statistics
| Branch: | Revision:

root / vl.c @ 6b99dadc

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