Statistics
| Branch: | Revision:

root / vl.c @ e0f084bf

History | View | Annotate | Download (154.5 kB)

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