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