84f1b3c97f170868a61c25e9adc2f7bfc370c079
[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 "hw/hw.h"
25 #include "hw/boards.h"
26 #include "hw/usb.h"
27 #include "hw/pcmcia.h"
28 #include "hw/pc.h"
29 #include "hw/audiodev.h"
30 #include "hw/isa.h"
31 #include "hw/baum.h"
32 #include "net.h"
33 #include "console.h"
34 #include "sysemu.h"
35 #include "gdbstub.h"
36 #include "qemu-timer.h"
37 #include "qemu-char.h"
38 #include "block.h"
39 #include "audio/audio.h"
40
41 #include <unistd.h>
42 #include <fcntl.h>
43 #include <signal.h>
44 #include <time.h>
45 #include <errno.h>
46 #include <sys/time.h>
47 #include <zlib.h>
48
49 #ifndef _WIN32
50 #include <sys/times.h>
51 #include <sys/wait.h>
52 #include <termios.h>
53 #include <sys/poll.h>
54 #include <sys/mman.h>
55 #include <sys/ioctl.h>
56 #include <sys/socket.h>
57 #include <netinet/in.h>
58 #include <dirent.h>
59 #include <netdb.h>
60 #include <sys/select.h>
61 #include <arpa/inet.h>
62 #ifdef _BSD
63 #include <sys/stat.h>
64 #if !defined(__APPLE__) && !defined(__OpenBSD__)
65 #include <libutil.h>
66 #endif
67 #ifdef __OpenBSD__
68 #include <net/if.h>
69 #endif
70 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
71 #include <freebsd/stdlib.h>
72 #else
73 #ifndef __sun__
74 #include <linux/if.h>
75 #include <linux/if_tun.h>
76 #include <pty.h>
77 #include <malloc.h>
78 #include <linux/rtc.h>
79
80 /* For the benefit of older linux systems which don't supply it,
81    we use a local copy of hpet.h. */
82 /* #include <linux/hpet.h> */
83 #include "hpet.h"
84
85 #include <linux/ppdev.h>
86 #include <linux/parport.h>
87 #else
88 #include <sys/stat.h>
89 #include <sys/ethernet.h>
90 #include <sys/sockio.h>
91 #include <netinet/arp.h>
92 #include <netinet/in.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/ip.h>
95 #include <netinet/ip_icmp.h> // must come after ip.h
96 #include <netinet/udp.h>
97 #include <netinet/tcp.h>
98 #include <net/if.h>
99 #include <syslog.h>
100 #include <stropts.h>
101 #endif
102 #endif
103 #endif
104
105 #include "qemu_socket.h"
106
107 #if defined(CONFIG_SLIRP)
108 #include "libslirp.h"
109 #endif
110
111 #if defined(__OpenBSD__)
112 #include <util.h>
113 #endif
114
115 #if defined(CONFIG_VDE)
116 #include <libvdeplug.h>
117 #endif
118
119 #ifdef _WIN32
120 #include <malloc.h>
121 #include <sys/timeb.h>
122 #include <mmsystem.h>
123 #define getopt_long_only getopt_long
124 #define memalign(align, size) malloc(size)
125 #endif
126
127 #ifdef CONFIG_SDL
128 #ifdef __APPLE__
129 #include <SDL/SDL.h>
130 #endif
131 #endif /* CONFIG_SDL */
132
133 #ifdef CONFIG_COCOA
134 #undef main
135 #define main qemu_main
136 #endif /* CONFIG_COCOA */
137
138 #include "disas.h"
139
140 #include "exec-all.h"
141
142 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
143 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
144 #ifdef __sun__
145 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
146 #else
147 #define SMBD_COMMAND "/usr/sbin/smbd"
148 #endif
149
150 //#define DEBUG_UNUSED_IOPORT
151 //#define DEBUG_IOPORT
152
153 #ifdef TARGET_PPC
154 #define DEFAULT_RAM_SIZE 144
155 #else
156 #define DEFAULT_RAM_SIZE 128
157 #endif
158
159 /* Max number of USB devices that can be specified on the commandline.  */
160 #define MAX_USB_CMDLINE 8
161
162 /* XXX: use a two level table to limit memory usage */
163 #define MAX_IOPORTS 65536
164
165 const char *bios_dir = CONFIG_QEMU_SHAREDIR;
166 const char *bios_name = NULL;
167 void *ioport_opaque[MAX_IOPORTS];
168 IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
169 IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
170 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
171    to store the VM snapshots */
172 DriveInfo drives_table[MAX_DRIVES+1];
173 int nb_drives;
174 /* point to the block driver where the snapshots are managed */
175 BlockDriverState *bs_snapshots;
176 int vga_ram_size;
177 static DisplayState display_state;
178 int nographic;
179 int curses;
180 const char* keyboard_layout = NULL;
181 int64_t ticks_per_sec;
182 ram_addr_t ram_size;
183 int pit_min_timer_count = 0;
184 int nb_nics;
185 NICInfo nd_table[MAX_NICS];
186 int vm_running;
187 static int rtc_utc = 1;
188 static int rtc_date_offset = -1; /* -1 means no change */
189 int cirrus_vga_enabled = 1;
190 int vmsvga_enabled = 0;
191 #ifdef TARGET_SPARC
192 int graphic_width = 1024;
193 int graphic_height = 768;
194 int graphic_depth = 8;
195 #else
196 int graphic_width = 800;
197 int graphic_height = 600;
198 int graphic_depth = 15;
199 #endif
200 int full_screen = 0;
201 int no_frame = 0;
202 int no_quit = 0;
203 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
204 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
205 #ifdef TARGET_I386
206 int win2k_install_hack = 0;
207 #endif
208 int usb_enabled = 0;
209 static VLANState *first_vlan;
210 int smp_cpus = 1;
211 const char *vnc_display;
212 #if defined(TARGET_SPARC)
213 #define MAX_CPUS 16
214 #elif defined(TARGET_I386)
215 #define MAX_CPUS 255
216 #else
217 #define MAX_CPUS 1
218 #endif
219 int acpi_enabled = 1;
220 int fd_bootchk = 1;
221 int no_reboot = 0;
222 int no_shutdown = 0;
223 int cursor_hide = 1;
224 int graphic_rotate = 0;
225 int daemonize = 0;
226 const char *option_rom[MAX_OPTION_ROMS];
227 int nb_option_roms;
228 int semihosting_enabled = 0;
229 int autostart = 1;
230 #ifdef TARGET_ARM
231 int old_param = 0;
232 #endif
233 const char *qemu_name;
234 int alt_grab = 0;
235 #ifdef TARGET_SPARC
236 unsigned int nb_prom_envs = 0;
237 const char *prom_envs[MAX_PROM_ENVS];
238 #endif
239 int nb_drives_opt;
240 struct drive_opt {
241     const char *file;
242     char opt[1024];
243 } drives_opt[MAX_DRIVES];
244
245 static CPUState *cur_cpu;
246 static CPUState *next_cpu;
247 static int event_pending = 1;
248 /* Conversion factor from emulated instructions to virtual clock ticks.  */
249 static int icount_time_shift;
250 /* Arbitrarily pick 1MIPS as the minimum allowable speed.  */
251 #define MAX_ICOUNT_SHIFT 10
252 /* Compensate for varying guest execution speed.  */
253 static int64_t qemu_icount_bias;
254 QEMUTimer *icount_rt_timer;
255 QEMUTimer *icount_vm_timer;
256
257 uint8_t qemu_uuid[16];
258
259 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
260
261 /***********************************************************/
262 /* x86 ISA bus support */
263
264 target_phys_addr_t isa_mem_base = 0;
265 PicState2 *isa_pic;
266
267 static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
268 static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
269
270 static uint32_t ioport_read(int index, uint32_t address)
271 {
272     static IOPortReadFunc *default_func[3] = {
273         default_ioport_readb,
274         default_ioport_readw,
275         default_ioport_readl
276     };
277     IOPortReadFunc *func = ioport_read_table[index][address];
278     if (!func)
279         func = default_func[index];
280     return func(ioport_opaque[address], address);
281 }
282
283 static void ioport_write(int index, uint32_t address, uint32_t data)
284 {
285     static IOPortWriteFunc *default_func[3] = {
286         default_ioport_writeb,
287         default_ioport_writew,
288         default_ioport_writel
289     };
290     IOPortWriteFunc *func = ioport_write_table[index][address];
291     if (!func)
292         func = default_func[index];
293     func(ioport_opaque[address], address, data);
294 }
295
296 static uint32_t default_ioport_readb(void *opaque, uint32_t address)
297 {
298 #ifdef DEBUG_UNUSED_IOPORT
299     fprintf(stderr, "unused inb: port=0x%04x\n", address);
300 #endif
301     return 0xff;
302 }
303
304 static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
305 {
306 #ifdef DEBUG_UNUSED_IOPORT
307     fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
308 #endif
309 }
310
311 /* default is to make two byte accesses */
312 static uint32_t default_ioport_readw(void *opaque, uint32_t address)
313 {
314     uint32_t data;
315     data = ioport_read(0, address);
316     address = (address + 1) & (MAX_IOPORTS - 1);
317     data |= ioport_read(0, address) << 8;
318     return data;
319 }
320
321 static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
322 {
323     ioport_write(0, address, data & 0xff);
324     address = (address + 1) & (MAX_IOPORTS - 1);
325     ioport_write(0, address, (data >> 8) & 0xff);
326 }
327
328 static uint32_t default_ioport_readl(void *opaque, uint32_t address)
329 {
330 #ifdef DEBUG_UNUSED_IOPORT
331     fprintf(stderr, "unused inl: port=0x%04x\n", address);
332 #endif
333     return 0xffffffff;
334 }
335
336 static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
337 {
338 #ifdef DEBUG_UNUSED_IOPORT
339     fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
340 #endif
341 }
342
343 /* size is the word size in byte */
344 int register_ioport_read(int start, int length, int size,
345                          IOPortReadFunc *func, void *opaque)
346 {
347     int i, bsize;
348
349     if (size == 1) {
350         bsize = 0;
351     } else if (size == 2) {
352         bsize = 1;
353     } else if (size == 4) {
354         bsize = 2;
355     } else {
356         hw_error("register_ioport_read: invalid size");
357         return -1;
358     }
359     for(i = start; i < start + length; i += size) {
360         ioport_read_table[bsize][i] = func;
361         if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
362             hw_error("register_ioport_read: invalid opaque");
363         ioport_opaque[i] = opaque;
364     }
365     return 0;
366 }
367
368 /* size is the word size in byte */
369 int register_ioport_write(int start, int length, int size,
370                           IOPortWriteFunc *func, void *opaque)
371 {
372     int i, bsize;
373
374     if (size == 1) {
375         bsize = 0;
376     } else if (size == 2) {
377         bsize = 1;
378     } else if (size == 4) {
379         bsize = 2;
380     } else {
381         hw_error("register_ioport_write: invalid size");
382         return -1;
383     }
384     for(i = start; i < start + length; i += size) {
385         ioport_write_table[bsize][i] = func;
386         if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
387             hw_error("register_ioport_write: invalid opaque");
388         ioport_opaque[i] = opaque;
389     }
390     return 0;
391 }
392
393 void isa_unassign_ioport(int start, int length)
394 {
395     int i;
396
397     for(i = start; i < start + length; i++) {
398         ioport_read_table[0][i] = default_ioport_readb;
399         ioport_read_table[1][i] = default_ioport_readw;
400         ioport_read_table[2][i] = default_ioport_readl;
401
402         ioport_write_table[0][i] = default_ioport_writeb;
403         ioport_write_table[1][i] = default_ioport_writew;
404         ioport_write_table[2][i] = default_ioport_writel;
405     }
406 }
407
408 /***********************************************************/
409
410 void cpu_outb(CPUState *env, int addr, int val)
411 {
412 #ifdef DEBUG_IOPORT
413     if (loglevel & CPU_LOG_IOPORT)
414         fprintf(logfile, "outb: %04x %02x\n", addr, val);
415 #endif
416     ioport_write(0, addr, val);
417 #ifdef USE_KQEMU
418     if (env)
419         env->last_io_time = cpu_get_time_fast();
420 #endif
421 }
422
423 void cpu_outw(CPUState *env, int addr, int val)
424 {
425 #ifdef DEBUG_IOPORT
426     if (loglevel & CPU_LOG_IOPORT)
427         fprintf(logfile, "outw: %04x %04x\n", addr, val);
428 #endif
429     ioport_write(1, addr, val);
430 #ifdef USE_KQEMU
431     if (env)
432         env->last_io_time = cpu_get_time_fast();
433 #endif
434 }
435
436 void cpu_outl(CPUState *env, int addr, int val)
437 {
438 #ifdef DEBUG_IOPORT
439     if (loglevel & CPU_LOG_IOPORT)
440         fprintf(logfile, "outl: %04x %08x\n", addr, val);
441 #endif
442     ioport_write(2, addr, val);
443 #ifdef USE_KQEMU
444     if (env)
445         env->last_io_time = cpu_get_time_fast();
446 #endif
447 }
448
449 int cpu_inb(CPUState *env, int addr)
450 {
451     int val;
452     val = ioport_read(0, addr);
453 #ifdef DEBUG_IOPORT
454     if (loglevel & CPU_LOG_IOPORT)
455         fprintf(logfile, "inb : %04x %02x\n", addr, val);
456 #endif
457 #ifdef USE_KQEMU
458     if (env)
459         env->last_io_time = cpu_get_time_fast();
460 #endif
461     return val;
462 }
463
464 int cpu_inw(CPUState *env, int addr)
465 {
466     int val;
467     val = ioport_read(1, addr);
468 #ifdef DEBUG_IOPORT
469     if (loglevel & CPU_LOG_IOPORT)
470         fprintf(logfile, "inw : %04x %04x\n", addr, val);
471 #endif
472 #ifdef USE_KQEMU
473     if (env)
474         env->last_io_time = cpu_get_time_fast();
475 #endif
476     return val;
477 }
478
479 int cpu_inl(CPUState *env, int addr)
480 {
481     int val;
482     val = ioport_read(2, addr);
483 #ifdef DEBUG_IOPORT
484     if (loglevel & CPU_LOG_IOPORT)
485         fprintf(logfile, "inl : %04x %08x\n", addr, val);
486 #endif
487 #ifdef USE_KQEMU
488     if (env)
489         env->last_io_time = cpu_get_time_fast();
490 #endif
491     return val;
492 }
493
494 /***********************************************************/
495 void hw_error(const char *fmt, ...)
496 {
497     va_list ap;
498     CPUState *env;
499
500     va_start(ap, fmt);
501     fprintf(stderr, "qemu: hardware error: ");
502     vfprintf(stderr, fmt, ap);
503     fprintf(stderr, "\n");
504     for(env = first_cpu; env != NULL; env = env->next_cpu) {
505         fprintf(stderr, "CPU #%d:\n", env->cpu_index);
506 #ifdef TARGET_I386
507         cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
508 #else
509         cpu_dump_state(env, stderr, fprintf, 0);
510 #endif
511     }
512     va_end(ap);
513     abort();
514 }
515
516 /***********************************************************/
517 /* keyboard/mouse */
518
519 static QEMUPutKBDEvent *qemu_put_kbd_event;
520 static void *qemu_put_kbd_event_opaque;
521 static QEMUPutMouseEntry *qemu_put_mouse_event_head;
522 static QEMUPutMouseEntry *qemu_put_mouse_event_current;
523
524 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
525 {
526     qemu_put_kbd_event_opaque = opaque;
527     qemu_put_kbd_event = func;
528 }
529
530 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
531                                                 void *opaque, int absolute,
532                                                 const char *name)
533 {
534     QEMUPutMouseEntry *s, *cursor;
535
536     s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
537     if (!s)
538         return NULL;
539
540     s->qemu_put_mouse_event = func;
541     s->qemu_put_mouse_event_opaque = opaque;
542     s->qemu_put_mouse_event_absolute = absolute;
543     s->qemu_put_mouse_event_name = qemu_strdup(name);
544     s->next = NULL;
545
546     if (!qemu_put_mouse_event_head) {
547         qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
548         return s;
549     }
550
551     cursor = qemu_put_mouse_event_head;
552     while (cursor->next != NULL)
553         cursor = cursor->next;
554
555     cursor->next = s;
556     qemu_put_mouse_event_current = s;
557
558     return s;
559 }
560
561 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
562 {
563     QEMUPutMouseEntry *prev = NULL, *cursor;
564
565     if (!qemu_put_mouse_event_head || entry == NULL)
566         return;
567
568     cursor = qemu_put_mouse_event_head;
569     while (cursor != NULL && cursor != entry) {
570         prev = cursor;
571         cursor = cursor->next;
572     }
573
574     if (cursor == NULL) // does not exist or list empty
575         return;
576     else if (prev == NULL) { // entry is head
577         qemu_put_mouse_event_head = cursor->next;
578         if (qemu_put_mouse_event_current == entry)
579             qemu_put_mouse_event_current = cursor->next;
580         qemu_free(entry->qemu_put_mouse_event_name);
581         qemu_free(entry);
582         return;
583     }
584
585     prev->next = entry->next;
586
587     if (qemu_put_mouse_event_current == entry)
588         qemu_put_mouse_event_current = prev;
589
590     qemu_free(entry->qemu_put_mouse_event_name);
591     qemu_free(entry);
592 }
593
594 void kbd_put_keycode(int keycode)
595 {
596     if (qemu_put_kbd_event) {
597         qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
598     }
599 }
600
601 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
602 {
603     QEMUPutMouseEvent *mouse_event;
604     void *mouse_event_opaque;
605     int width;
606
607     if (!qemu_put_mouse_event_current) {
608         return;
609     }
610
611     mouse_event =
612         qemu_put_mouse_event_current->qemu_put_mouse_event;
613     mouse_event_opaque =
614         qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
615
616     if (mouse_event) {
617         if (graphic_rotate) {
618             if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
619                 width = 0x7fff;
620             else
621                 width = graphic_width - 1;
622             mouse_event(mouse_event_opaque,
623                                  width - dy, dx, dz, buttons_state);
624         } else
625             mouse_event(mouse_event_opaque,
626                                  dx, dy, dz, buttons_state);
627     }
628 }
629
630 int kbd_mouse_is_absolute(void)
631 {
632     if (!qemu_put_mouse_event_current)
633         return 0;
634
635     return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
636 }
637
638 void do_info_mice(void)
639 {
640     QEMUPutMouseEntry *cursor;
641     int index = 0;
642
643     if (!qemu_put_mouse_event_head) {
644         term_printf("No mouse devices connected\n");
645         return;
646     }
647
648     term_printf("Mouse devices available:\n");
649     cursor = qemu_put_mouse_event_head;
650     while (cursor != NULL) {
651         term_printf("%c Mouse #%d: %s\n",
652                     (cursor == qemu_put_mouse_event_current ? '*' : ' '),
653                     index, cursor->qemu_put_mouse_event_name);
654         index++;
655         cursor = cursor->next;
656     }
657 }
658
659 void do_mouse_set(int index)
660 {
661     QEMUPutMouseEntry *cursor;
662     int i = 0;
663
664     if (!qemu_put_mouse_event_head) {
665         term_printf("No mouse devices connected\n");
666         return;
667     }
668
669     cursor = qemu_put_mouse_event_head;
670     while (cursor != NULL && index != i) {
671         i++;
672         cursor = cursor->next;
673     }
674
675     if (cursor != NULL)
676         qemu_put_mouse_event_current = cursor;
677     else
678         term_printf("Mouse at given index not found\n");
679 }
680
681 /* compute with 96 bit intermediate result: (a*b)/c */
682 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
683 {
684     union {
685         uint64_t ll;
686         struct {
687 #ifdef WORDS_BIGENDIAN
688             uint32_t high, low;
689 #else
690             uint32_t low, high;
691 #endif
692         } l;
693     } u, res;
694     uint64_t rl, rh;
695
696     u.ll = a;
697     rl = (uint64_t)u.l.low * (uint64_t)b;
698     rh = (uint64_t)u.l.high * (uint64_t)b;
699     rh += (rl >> 32);
700     res.l.high = rh / c;
701     res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
702     return res.ll;
703 }
704
705 /***********************************************************/
706 /* real time host monotonic timer */
707
708 #define QEMU_TIMER_BASE 1000000000LL
709
710 #ifdef WIN32
711
712 static int64_t clock_freq;
713
714 static void init_get_clock(void)
715 {
716     LARGE_INTEGER freq;
717     int ret;
718     ret = QueryPerformanceFrequency(&freq);
719     if (ret == 0) {
720         fprintf(stderr, "Could not calibrate ticks\n");
721         exit(1);
722     }
723     clock_freq = freq.QuadPart;
724 }
725
726 static int64_t get_clock(void)
727 {
728     LARGE_INTEGER ti;
729     QueryPerformanceCounter(&ti);
730     return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
731 }
732
733 #else
734
735 static int use_rt_clock;
736
737 static void init_get_clock(void)
738 {
739     use_rt_clock = 0;
740 #if defined(__linux__)
741     {
742         struct timespec ts;
743         if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
744             use_rt_clock = 1;
745         }
746     }
747 #endif
748 }
749
750 static int64_t get_clock(void)
751 {
752 #if defined(__linux__)
753     if (use_rt_clock) {
754         struct timespec ts;
755         clock_gettime(CLOCK_MONOTONIC, &ts);
756         return ts.tv_sec * 1000000000LL + ts.tv_nsec;
757     } else
758 #endif
759     {
760         /* XXX: using gettimeofday leads to problems if the date
761            changes, so it should be avoided. */
762         struct timeval tv;
763         gettimeofday(&tv, NULL);
764         return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
765     }
766 }
767 #endif
768
769 /* Return the virtual CPU time, based on the instruction counter.  */
770 static int64_t cpu_get_icount(void)
771 {
772     int64_t icount;
773     CPUState *env = cpu_single_env;;
774     icount = qemu_icount;
775     if (env) {
776         if (!can_do_io(env))
777             fprintf(stderr, "Bad clock read\n");
778         icount -= (env->icount_decr.u16.low + env->icount_extra);
779     }
780     return qemu_icount_bias + (icount << icount_time_shift);
781 }
782
783 /***********************************************************/
784 /* guest cycle counter */
785
786 static int64_t cpu_ticks_prev;
787 static int64_t cpu_ticks_offset;
788 static int64_t cpu_clock_offset;
789 static int cpu_ticks_enabled;
790
791 /* return the host CPU cycle counter and handle stop/restart */
792 int64_t cpu_get_ticks(void)
793 {
794     if (use_icount) {
795         return cpu_get_icount();
796     }
797     if (!cpu_ticks_enabled) {
798         return cpu_ticks_offset;
799     } else {
800         int64_t ticks;
801         ticks = cpu_get_real_ticks();
802         if (cpu_ticks_prev > ticks) {
803             /* Note: non increasing ticks may happen if the host uses
804                software suspend */
805             cpu_ticks_offset += cpu_ticks_prev - ticks;
806         }
807         cpu_ticks_prev = ticks;
808         return ticks + cpu_ticks_offset;
809     }
810 }
811
812 /* return the host CPU monotonic timer and handle stop/restart */
813 static int64_t cpu_get_clock(void)
814 {
815     int64_t ti;
816     if (!cpu_ticks_enabled) {
817         return cpu_clock_offset;
818     } else {
819         ti = get_clock();
820         return ti + cpu_clock_offset;
821     }
822 }
823
824 /* enable cpu_get_ticks() */
825 void cpu_enable_ticks(void)
826 {
827     if (!cpu_ticks_enabled) {
828         cpu_ticks_offset -= cpu_get_real_ticks();
829         cpu_clock_offset -= get_clock();
830         cpu_ticks_enabled = 1;
831     }
832 }
833
834 /* disable cpu_get_ticks() : the clock is stopped. You must not call
835    cpu_get_ticks() after that.  */
836 void cpu_disable_ticks(void)
837 {
838     if (cpu_ticks_enabled) {
839         cpu_ticks_offset = cpu_get_ticks();
840         cpu_clock_offset = cpu_get_clock();
841         cpu_ticks_enabled = 0;
842     }
843 }
844
845 /***********************************************************/
846 /* timers */
847
848 #define QEMU_TIMER_REALTIME 0
849 #define QEMU_TIMER_VIRTUAL  1
850
851 struct QEMUClock {
852     int type;
853     /* XXX: add frequency */
854 };
855
856 struct QEMUTimer {
857     QEMUClock *clock;
858     int64_t expire_time;
859     QEMUTimerCB *cb;
860     void *opaque;
861     struct QEMUTimer *next;
862 };
863
864 struct qemu_alarm_timer {
865     char const *name;
866     unsigned int flags;
867
868     int (*start)(struct qemu_alarm_timer *t);
869     void (*stop)(struct qemu_alarm_timer *t);
870     void (*rearm)(struct qemu_alarm_timer *t);
871     void *priv;
872 };
873
874 #define ALARM_FLAG_DYNTICKS  0x1
875 #define ALARM_FLAG_EXPIRED   0x2
876
877 static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
878 {
879     return t->flags & ALARM_FLAG_DYNTICKS;
880 }
881
882 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
883 {
884     if (!alarm_has_dynticks(t))
885         return;
886
887     t->rearm(t);
888 }
889
890 /* TODO: MIN_TIMER_REARM_US should be optimized */
891 #define MIN_TIMER_REARM_US 250
892
893 static struct qemu_alarm_timer *alarm_timer;
894
895 #ifdef _WIN32
896
897 struct qemu_alarm_win32 {
898     MMRESULT timerId;
899     HANDLE host_alarm;
900     unsigned int period;
901 } alarm_win32_data = {0, NULL, -1};
902
903 static int win32_start_timer(struct qemu_alarm_timer *t);
904 static void win32_stop_timer(struct qemu_alarm_timer *t);
905 static void win32_rearm_timer(struct qemu_alarm_timer *t);
906
907 #else
908
909 static int unix_start_timer(struct qemu_alarm_timer *t);
910 static void unix_stop_timer(struct qemu_alarm_timer *t);
911
912 #ifdef __linux__
913
914 static int dynticks_start_timer(struct qemu_alarm_timer *t);
915 static void dynticks_stop_timer(struct qemu_alarm_timer *t);
916 static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
917
918 static int hpet_start_timer(struct qemu_alarm_timer *t);
919 static void hpet_stop_timer(struct qemu_alarm_timer *t);
920
921 static int rtc_start_timer(struct qemu_alarm_timer *t);
922 static void rtc_stop_timer(struct qemu_alarm_timer *t);
923
924 #endif /* __linux__ */
925
926 #endif /* _WIN32 */
927
928 /* Correlation between real and virtual time is always going to be
929    fairly approximate, so ignore small variation.
930    When the guest is idle real and virtual time will be aligned in
931    the IO wait loop.  */
932 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
933
934 static void icount_adjust(void)
935 {
936     int64_t cur_time;
937     int64_t cur_icount;
938     int64_t delta;
939     static int64_t last_delta;
940     /* If the VM is not running, then do nothing.  */
941     if (!vm_running)
942         return;
943
944     cur_time = cpu_get_clock();
945     cur_icount = qemu_get_clock(vm_clock);
946     delta = cur_icount - cur_time;
947     /* FIXME: This is a very crude algorithm, somewhat prone to oscillation.  */
948     if (delta > 0
949         && last_delta + ICOUNT_WOBBLE < delta * 2
950         && icount_time_shift > 0) {
951         /* The guest is getting too far ahead.  Slow time down.  */
952         icount_time_shift--;
953     }
954     if (delta < 0
955         && last_delta - ICOUNT_WOBBLE > delta * 2
956         && icount_time_shift < MAX_ICOUNT_SHIFT) {
957         /* The guest is getting too far behind.  Speed time up.  */
958         icount_time_shift++;
959     }
960     last_delta = delta;
961     qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
962 }
963
964 static void icount_adjust_rt(void * opaque)
965 {
966     qemu_mod_timer(icount_rt_timer,
967                    qemu_get_clock(rt_clock) + 1000);
968     icount_adjust();
969 }
970
971 static void icount_adjust_vm(void * opaque)
972 {
973     qemu_mod_timer(icount_vm_timer,
974                    qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
975     icount_adjust();
976 }
977
978 static void init_icount_adjust(void)
979 {
980     /* Have both realtime and virtual time triggers for speed adjustment.
981        The realtime trigger catches emulated time passing too slowly,
982        the virtual time trigger catches emulated time passing too fast.
983        Realtime triggers occur even when idle, so use them less frequently
984        than VM triggers.  */
985     icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
986     qemu_mod_timer(icount_rt_timer,
987                    qemu_get_clock(rt_clock) + 1000);
988     icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
989     qemu_mod_timer(icount_vm_timer,
990                    qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
991 }
992
993 static struct qemu_alarm_timer alarm_timers[] = {
994 #ifndef _WIN32
995 #ifdef __linux__
996     {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
997      dynticks_stop_timer, dynticks_rearm_timer, NULL},
998     /* HPET - if available - is preferred */
999     {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
1000     /* ...otherwise try RTC */
1001     {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
1002 #endif
1003     {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
1004 #else
1005     {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1006      win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1007     {"win32", 0, win32_start_timer,
1008      win32_stop_timer, NULL, &alarm_win32_data},
1009 #endif
1010     {NULL, }
1011 };
1012
1013 static void show_available_alarms(void)
1014 {
1015     int i;
1016
1017     printf("Available alarm timers, in order of precedence:\n");
1018     for (i = 0; alarm_timers[i].name; i++)
1019         printf("%s\n", alarm_timers[i].name);
1020 }
1021
1022 static void configure_alarms(char const *opt)
1023 {
1024     int i;
1025     int cur = 0;
1026     int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1;
1027     char *arg;
1028     char *name;
1029     struct qemu_alarm_timer tmp;
1030
1031     if (!strcmp(opt, "?")) {
1032         show_available_alarms();
1033         exit(0);
1034     }
1035
1036     arg = strdup(opt);
1037
1038     /* Reorder the array */
1039     name = strtok(arg, ",");
1040     while (name) {
1041         for (i = 0; i < count && alarm_timers[i].name; i++) {
1042             if (!strcmp(alarm_timers[i].name, name))
1043                 break;
1044         }
1045
1046         if (i == count) {
1047             fprintf(stderr, "Unknown clock %s\n", name);
1048             goto next;
1049         }
1050
1051         if (i < cur)
1052             /* Ignore */
1053             goto next;
1054
1055         /* Swap */
1056         tmp = alarm_timers[i];
1057         alarm_timers[i] = alarm_timers[cur];
1058         alarm_timers[cur] = tmp;
1059
1060         cur++;
1061 next:
1062         name = strtok(NULL, ",");
1063     }
1064
1065     free(arg);
1066
1067     if (cur) {
1068         /* Disable remaining timers */
1069         for (i = cur; i < count; i++)
1070             alarm_timers[i].name = NULL;
1071     } else {
1072         show_available_alarms();
1073         exit(1);
1074     }
1075 }
1076
1077 QEMUClock *rt_clock;
1078 QEMUClock *vm_clock;
1079
1080 static QEMUTimer *active_timers[2];
1081
1082 static QEMUClock *qemu_new_clock(int type)
1083 {
1084     QEMUClock *clock;
1085     clock = qemu_mallocz(sizeof(QEMUClock));
1086     if (!clock)
1087         return NULL;
1088     clock->type = type;
1089     return clock;
1090 }
1091
1092 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1093 {
1094     QEMUTimer *ts;
1095
1096     ts = qemu_mallocz(sizeof(QEMUTimer));
1097     ts->clock = clock;
1098     ts->cb = cb;
1099     ts->opaque = opaque;
1100     return ts;
1101 }
1102
1103 void qemu_free_timer(QEMUTimer *ts)
1104 {
1105     qemu_free(ts);
1106 }
1107
1108 /* stop a timer, but do not dealloc it */
1109 void qemu_del_timer(QEMUTimer *ts)
1110 {
1111     QEMUTimer **pt, *t;
1112
1113     /* NOTE: this code must be signal safe because
1114        qemu_timer_expired() can be called from a signal. */
1115     pt = &active_timers[ts->clock->type];
1116     for(;;) {
1117         t = *pt;
1118         if (!t)
1119             break;
1120         if (t == ts) {
1121             *pt = t->next;
1122             break;
1123         }
1124         pt = &t->next;
1125     }
1126 }
1127
1128 /* modify the current timer so that it will be fired when current_time
1129    >= expire_time. The corresponding callback will be called. */
1130 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1131 {
1132     QEMUTimer **pt, *t;
1133
1134     qemu_del_timer(ts);
1135
1136     /* add the timer in the sorted list */
1137     /* NOTE: this code must be signal safe because
1138        qemu_timer_expired() can be called from a signal. */
1139     pt = &active_timers[ts->clock->type];
1140     for(;;) {
1141         t = *pt;
1142         if (!t)
1143             break;
1144         if (t->expire_time > expire_time)
1145             break;
1146         pt = &t->next;
1147     }
1148     ts->expire_time = expire_time;
1149     ts->next = *pt;
1150     *pt = ts;
1151
1152     /* Rearm if necessary  */
1153     if (pt == &active_timers[ts->clock->type]) {
1154         if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1155             qemu_rearm_alarm_timer(alarm_timer);
1156         }
1157         /* Interrupt execution to force deadline recalculation.  */
1158         if (use_icount && cpu_single_env) {
1159             cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
1160         }
1161     }
1162 }
1163
1164 int qemu_timer_pending(QEMUTimer *ts)
1165 {
1166     QEMUTimer *t;
1167     for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1168         if (t == ts)
1169             return 1;
1170     }
1171     return 0;
1172 }
1173
1174 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1175 {
1176     if (!timer_head)
1177         return 0;
1178     return (timer_head->expire_time <= current_time);
1179 }
1180
1181 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1182 {
1183     QEMUTimer *ts;
1184
1185     for(;;) {
1186         ts = *ptimer_head;
1187         if (!ts || ts->expire_time > current_time)
1188             break;
1189         /* remove timer from the list before calling the callback */
1190         *ptimer_head = ts->next;
1191         ts->next = NULL;
1192
1193         /* run the callback (the timer list can be modified) */
1194         ts->cb(ts->opaque);
1195     }
1196 }
1197
1198 int64_t qemu_get_clock(QEMUClock *clock)
1199 {
1200     switch(clock->type) {
1201     case QEMU_TIMER_REALTIME:
1202         return get_clock() / 1000000;
1203     default:
1204     case QEMU_TIMER_VIRTUAL:
1205         if (use_icount) {
1206             return cpu_get_icount();
1207         } else {
1208             return cpu_get_clock();
1209         }
1210     }
1211 }
1212
1213 static void init_timers(void)
1214 {
1215     init_get_clock();
1216     ticks_per_sec = QEMU_TIMER_BASE;
1217     rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1218     vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1219 }
1220
1221 /* save a timer */
1222 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1223 {
1224     uint64_t expire_time;
1225
1226     if (qemu_timer_pending(ts)) {
1227         expire_time = ts->expire_time;
1228     } else {
1229         expire_time = -1;
1230     }
1231     qemu_put_be64(f, expire_time);
1232 }
1233
1234 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1235 {
1236     uint64_t expire_time;
1237
1238     expire_time = qemu_get_be64(f);
1239     if (expire_time != -1) {
1240         qemu_mod_timer(ts, expire_time);
1241     } else {
1242         qemu_del_timer(ts);
1243     }
1244 }
1245
1246 static void timer_save(QEMUFile *f, void *opaque)
1247 {
1248     if (cpu_ticks_enabled) {
1249         hw_error("cannot save state if virtual timers are running");
1250     }
1251     qemu_put_be64(f, cpu_ticks_offset);
1252     qemu_put_be64(f, ticks_per_sec);
1253     qemu_put_be64(f, cpu_clock_offset);
1254 }
1255
1256 static int timer_load(QEMUFile *f, void *opaque, int version_id)
1257 {
1258     if (version_id != 1 && version_id != 2)
1259         return -EINVAL;
1260     if (cpu_ticks_enabled) {
1261         return -EINVAL;
1262     }
1263     cpu_ticks_offset=qemu_get_be64(f);
1264     ticks_per_sec=qemu_get_be64(f);
1265     if (version_id == 2) {
1266         cpu_clock_offset=qemu_get_be64(f);
1267     }
1268     return 0;
1269 }
1270
1271 #ifdef _WIN32
1272 void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1273                                  DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1274 #else
1275 static void host_alarm_handler(int host_signum)
1276 #endif
1277 {
1278 #if 0
1279 #define DISP_FREQ 1000
1280     {
1281         static int64_t delta_min = INT64_MAX;
1282         static int64_t delta_max, delta_cum, last_clock, delta, ti;
1283         static int count;
1284         ti = qemu_get_clock(vm_clock);
1285         if (last_clock != 0) {
1286             delta = ti - last_clock;
1287             if (delta < delta_min)
1288                 delta_min = delta;
1289             if (delta > delta_max)
1290                 delta_max = delta;
1291             delta_cum += delta;
1292             if (++count == DISP_FREQ) {
1293                 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
1294                        muldiv64(delta_min, 1000000, ticks_per_sec),
1295                        muldiv64(delta_max, 1000000, ticks_per_sec),
1296                        muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1297                        (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1298                 count = 0;
1299                 delta_min = INT64_MAX;
1300                 delta_max = 0;
1301                 delta_cum = 0;
1302             }
1303         }
1304         last_clock = ti;
1305     }
1306 #endif
1307     if (alarm_has_dynticks(alarm_timer) ||
1308         (!use_icount &&
1309             qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1310                                qemu_get_clock(vm_clock))) ||
1311         qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1312                            qemu_get_clock(rt_clock))) {
1313 #ifdef _WIN32
1314         struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1315         SetEvent(data->host_alarm);
1316 #endif
1317         CPUState *env = next_cpu;
1318
1319         alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1320
1321         if (env) {
1322             /* stop the currently executing cpu because a timer occured */
1323             cpu_interrupt(env, CPU_INTERRUPT_EXIT);
1324 #ifdef USE_KQEMU
1325             if (env->kqemu_enabled) {
1326                 kqemu_cpu_interrupt(env);
1327             }
1328 #endif
1329         }
1330         event_pending = 1;
1331     }
1332 }
1333
1334 static int64_t qemu_next_deadline(void)
1335 {
1336     int64_t delta;
1337
1338     if (active_timers[QEMU_TIMER_VIRTUAL]) {
1339         delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1340                      qemu_get_clock(vm_clock);
1341     } else {
1342         /* To avoid problems with overflow limit this to 2^32.  */
1343         delta = INT32_MAX;
1344     }
1345
1346     if (delta < 0)
1347         delta = 0;
1348
1349     return delta;
1350 }
1351
1352 #if defined(__linux__) || defined(_WIN32)
1353 static uint64_t qemu_next_deadline_dyntick(void)
1354 {
1355     int64_t delta;
1356     int64_t rtdelta;
1357
1358     if (use_icount)
1359         delta = INT32_MAX;
1360     else
1361         delta = (qemu_next_deadline() + 999) / 1000;
1362
1363     if (active_timers[QEMU_TIMER_REALTIME]) {
1364         rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1365                  qemu_get_clock(rt_clock))*1000;
1366         if (rtdelta < delta)
1367             delta = rtdelta;
1368     }
1369
1370     if (delta < MIN_TIMER_REARM_US)
1371         delta = MIN_TIMER_REARM_US;
1372
1373     return delta;
1374 }
1375 #endif
1376
1377 #ifndef _WIN32
1378
1379 #if defined(__linux__)
1380
1381 #define RTC_FREQ 1024
1382
1383 static void enable_sigio_timer(int fd)
1384 {
1385     struct sigaction act;
1386
1387     /* timer signal */
1388     sigfillset(&act.sa_mask);
1389     act.sa_flags = 0;
1390     act.sa_handler = host_alarm_handler;
1391
1392     sigaction(SIGIO, &act, NULL);
1393     fcntl(fd, F_SETFL, O_ASYNC);
1394     fcntl(fd, F_SETOWN, getpid());
1395 }
1396
1397 static int hpet_start_timer(struct qemu_alarm_timer *t)
1398 {
1399     struct hpet_info info;
1400     int r, fd;
1401
1402     fd = open("/dev/hpet", O_RDONLY);
1403     if (fd < 0)
1404         return -1;
1405
1406     /* Set frequency */
1407     r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1408     if (r < 0) {
1409         fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1410                 "error, but for better emulation accuracy type:\n"
1411                 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1412         goto fail;
1413     }
1414
1415     /* Check capabilities */
1416     r = ioctl(fd, HPET_INFO, &info);
1417     if (r < 0)
1418         goto fail;
1419
1420     /* Enable periodic mode */
1421     r = ioctl(fd, HPET_EPI, 0);
1422     if (info.hi_flags && (r < 0))
1423         goto fail;
1424
1425     /* Enable interrupt */
1426     r = ioctl(fd, HPET_IE_ON, 0);
1427     if (r < 0)
1428         goto fail;
1429
1430     enable_sigio_timer(fd);
1431     t->priv = (void *)(long)fd;
1432
1433     return 0;
1434 fail:
1435     close(fd);
1436     return -1;
1437 }
1438
1439 static void hpet_stop_timer(struct qemu_alarm_timer *t)
1440 {
1441     int fd = (long)t->priv;
1442
1443     close(fd);
1444 }
1445
1446 static int rtc_start_timer(struct qemu_alarm_timer *t)
1447 {
1448     int rtc_fd;
1449     unsigned long current_rtc_freq = 0;
1450
1451     TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
1452     if (rtc_fd < 0)
1453         return -1;
1454     ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1455     if (current_rtc_freq != RTC_FREQ &&
1456         ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
1457         fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1458                 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1459                 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1460         goto fail;
1461     }
1462     if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1463     fail:
1464         close(rtc_fd);
1465         return -1;
1466     }
1467
1468     enable_sigio_timer(rtc_fd);
1469
1470     t->priv = (void *)(long)rtc_fd;
1471
1472     return 0;
1473 }
1474
1475 static void rtc_stop_timer(struct qemu_alarm_timer *t)
1476 {
1477     int rtc_fd = (long)t->priv;
1478
1479     close(rtc_fd);
1480 }
1481
1482 static int dynticks_start_timer(struct qemu_alarm_timer *t)
1483 {
1484     struct sigevent ev;
1485     timer_t host_timer;
1486     struct sigaction act;
1487
1488     sigfillset(&act.sa_mask);
1489     act.sa_flags = 0;
1490     act.sa_handler = host_alarm_handler;
1491
1492     sigaction(SIGALRM, &act, NULL);
1493
1494     ev.sigev_value.sival_int = 0;
1495     ev.sigev_notify = SIGEV_SIGNAL;
1496     ev.sigev_signo = SIGALRM;
1497
1498     if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1499         perror("timer_create");
1500
1501         /* disable dynticks */
1502         fprintf(stderr, "Dynamic Ticks disabled\n");
1503
1504         return -1;
1505     }
1506
1507     t->priv = (void *)host_timer;
1508
1509     return 0;
1510 }
1511
1512 static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1513 {
1514     timer_t host_timer = (timer_t)t->priv;
1515
1516     timer_delete(host_timer);
1517 }
1518
1519 static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1520 {
1521     timer_t host_timer = (timer_t)t->priv;
1522     struct itimerspec timeout;
1523     int64_t nearest_delta_us = INT64_MAX;
1524     int64_t current_us;
1525
1526     if (!active_timers[QEMU_TIMER_REALTIME] &&
1527                 !active_timers[QEMU_TIMER_VIRTUAL])
1528         return;
1529
1530     nearest_delta_us = qemu_next_deadline_dyntick();
1531
1532     /* check whether a timer is already running */
1533     if (timer_gettime(host_timer, &timeout)) {
1534         perror("gettime");
1535         fprintf(stderr, "Internal timer error: aborting\n");
1536         exit(1);
1537     }
1538     current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1539     if (current_us && current_us <= nearest_delta_us)
1540         return;
1541
1542     timeout.it_interval.tv_sec = 0;
1543     timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1544     timeout.it_value.tv_sec =  nearest_delta_us / 1000000;
1545     timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1546     if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1547         perror("settime");
1548         fprintf(stderr, "Internal timer error: aborting\n");
1549         exit(1);
1550     }
1551 }
1552
1553 #endif /* defined(__linux__) */
1554
1555 static int unix_start_timer(struct qemu_alarm_timer *t)
1556 {
1557     struct sigaction act;
1558     struct itimerval itv;
1559     int err;
1560
1561     /* timer signal */
1562     sigfillset(&act.sa_mask);
1563     act.sa_flags = 0;
1564     act.sa_handler = host_alarm_handler;
1565
1566     sigaction(SIGALRM, &act, NULL);
1567
1568     itv.it_interval.tv_sec = 0;
1569     /* for i386 kernel 2.6 to get 1 ms */
1570     itv.it_interval.tv_usec = 999;
1571     itv.it_value.tv_sec = 0;
1572     itv.it_value.tv_usec = 10 * 1000;
1573
1574     err = setitimer(ITIMER_REAL, &itv, NULL);
1575     if (err)
1576         return -1;
1577
1578     return 0;
1579 }
1580
1581 static void unix_stop_timer(struct qemu_alarm_timer *t)
1582 {
1583     struct itimerval itv;
1584
1585     memset(&itv, 0, sizeof(itv));
1586     setitimer(ITIMER_REAL, &itv, NULL);
1587 }
1588
1589 #endif /* !defined(_WIN32) */
1590
1591 #ifdef _WIN32
1592
1593 static int win32_start_timer(struct qemu_alarm_timer *t)
1594 {
1595     TIMECAPS tc;
1596     struct qemu_alarm_win32 *data = t->priv;
1597     UINT flags;
1598
1599     data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1600     if (!data->host_alarm) {
1601         perror("Failed CreateEvent");
1602         return -1;
1603     }
1604
1605     memset(&tc, 0, sizeof(tc));
1606     timeGetDevCaps(&tc, sizeof(tc));
1607
1608     if (data->period < tc.wPeriodMin)
1609         data->period = tc.wPeriodMin;
1610
1611     timeBeginPeriod(data->period);
1612
1613     flags = TIME_CALLBACK_FUNCTION;
1614     if (alarm_has_dynticks(t))
1615         flags |= TIME_ONESHOT;
1616     else
1617         flags |= TIME_PERIODIC;
1618
1619     data->timerId = timeSetEvent(1,         // interval (ms)
1620                         data->period,       // resolution
1621                         host_alarm_handler, // function
1622                         (DWORD)t,           // parameter
1623                         flags);
1624
1625     if (!data->timerId) {
1626         perror("Failed to initialize win32 alarm timer");
1627
1628         timeEndPeriod(data->period);
1629         CloseHandle(data->host_alarm);
1630         return -1;
1631     }
1632
1633     qemu_add_wait_object(data->host_alarm, NULL, NULL);
1634
1635     return 0;
1636 }
1637
1638 static void win32_stop_timer(struct qemu_alarm_timer *t)
1639 {
1640     struct qemu_alarm_win32 *data = t->priv;
1641
1642     timeKillEvent(data->timerId);
1643     timeEndPeriod(data->period);
1644
1645     CloseHandle(data->host_alarm);
1646 }
1647
1648 static void win32_rearm_timer(struct qemu_alarm_timer *t)
1649 {
1650     struct qemu_alarm_win32 *data = t->priv;
1651     uint64_t nearest_delta_us;
1652
1653     if (!active_timers[QEMU_TIMER_REALTIME] &&
1654                 !active_timers[QEMU_TIMER_VIRTUAL])
1655         return;
1656
1657     nearest_delta_us = qemu_next_deadline_dyntick();
1658     nearest_delta_us /= 1000;
1659
1660     timeKillEvent(data->timerId);
1661
1662     data->timerId = timeSetEvent(1,
1663                         data->period,
1664                         host_alarm_handler,
1665                         (DWORD)t,
1666                         TIME_ONESHOT | TIME_PERIODIC);
1667
1668     if (!data->timerId) {
1669         perror("Failed to re-arm win32 alarm timer");
1670
1671         timeEndPeriod(data->period);
1672         CloseHandle(data->host_alarm);
1673         exit(1);
1674     }
1675 }
1676
1677 #endif /* _WIN32 */
1678
1679 static void init_timer_alarm(void)
1680 {
1681     struct qemu_alarm_timer *t;
1682     int i, err = -1;
1683
1684     for (i = 0; alarm_timers[i].name; i++) {
1685         t = &alarm_timers[i];
1686
1687         err = t->start(t);
1688         if (!err)
1689             break;
1690     }
1691
1692     if (err) {
1693         fprintf(stderr, "Unable to find any suitable alarm timer.\n");
1694         fprintf(stderr, "Terminating\n");
1695         exit(1);
1696     }
1697
1698     alarm_timer = t;
1699 }
1700
1701 static void quit_timers(void)
1702 {
1703     alarm_timer->stop(alarm_timer);
1704     alarm_timer = NULL;
1705 }
1706
1707 /***********************************************************/
1708 /* host time/date access */
1709 void qemu_get_timedate(struct tm *tm, int offset)
1710 {
1711     time_t ti;
1712     struct tm *ret;
1713
1714     time(&ti);
1715     ti += offset;
1716     if (rtc_date_offset == -1) {
1717         if (rtc_utc)
1718             ret = gmtime(&ti);
1719         else
1720             ret = localtime(&ti);
1721     } else {
1722         ti -= rtc_date_offset;
1723         ret = gmtime(&ti);
1724     }
1725
1726     memcpy(tm, ret, sizeof(struct tm));
1727 }
1728
1729 int qemu_timedate_diff(struct tm *tm)
1730 {
1731     time_t seconds;
1732
1733     if (rtc_date_offset == -1)
1734         if (rtc_utc)
1735             seconds = mktimegm(tm);
1736         else
1737             seconds = mktime(tm);
1738     else
1739         seconds = mktimegm(tm) + rtc_date_offset;
1740
1741     return seconds - time(NULL);
1742 }
1743
1744 /***********************************************************/
1745 /* character device */
1746
1747 static void qemu_chr_event(CharDriverState *s, int event)
1748 {
1749     if (!s->chr_event)
1750         return;
1751     s->chr_event(s->handler_opaque, event);
1752 }
1753
1754 static void qemu_chr_reset_bh(void *opaque)
1755 {
1756     CharDriverState *s = opaque;
1757     qemu_chr_event(s, CHR_EVENT_RESET);
1758     qemu_bh_delete(s->bh);
1759     s->bh = NULL;
1760 }
1761
1762 void qemu_chr_reset(CharDriverState *s)
1763 {
1764     if (s->bh == NULL) {
1765         s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
1766         qemu_bh_schedule(s->bh);
1767     }
1768 }
1769
1770 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
1771 {
1772     return s->chr_write(s, buf, len);
1773 }
1774
1775 int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
1776 {
1777     if (!s->chr_ioctl)
1778         return -ENOTSUP;
1779     return s->chr_ioctl(s, cmd, arg);
1780 }
1781
1782 int qemu_chr_can_read(CharDriverState *s)
1783 {
1784     if (!s->chr_can_read)
1785         return 0;
1786     return s->chr_can_read(s->handler_opaque);
1787 }
1788
1789 void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
1790 {
1791     s->chr_read(s->handler_opaque, buf, len);
1792 }
1793
1794 void qemu_chr_accept_input(CharDriverState *s)
1795 {
1796     if (s->chr_accept_input)
1797         s->chr_accept_input(s);
1798 }
1799
1800 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
1801 {
1802     char buf[4096];
1803     va_list ap;
1804     va_start(ap, fmt);
1805     vsnprintf(buf, sizeof(buf), fmt, ap);
1806     qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
1807     va_end(ap);
1808 }
1809
1810 void qemu_chr_send_event(CharDriverState *s, int event)
1811 {
1812     if (s->chr_send_event)
1813         s->chr_send_event(s, event);
1814 }
1815
1816 void qemu_chr_add_handlers(CharDriverState *s,
1817                            IOCanRWHandler *fd_can_read,
1818                            IOReadHandler *fd_read,
1819                            IOEventHandler *fd_event,
1820                            void *opaque)
1821 {
1822     s->chr_can_read = fd_can_read;
1823     s->chr_read = fd_read;
1824     s->chr_event = fd_event;
1825     s->handler_opaque = opaque;
1826     if (s->chr_update_read_handler)
1827         s->chr_update_read_handler(s);
1828 }
1829
1830 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1831 {
1832     return len;
1833 }
1834
1835 static CharDriverState *qemu_chr_open_null(void)
1836 {
1837     CharDriverState *chr;
1838
1839     chr = qemu_mallocz(sizeof(CharDriverState));
1840     if (!chr)
1841         return NULL;
1842     chr->chr_write = null_chr_write;
1843     return chr;
1844 }
1845
1846 /* MUX driver for serial I/O splitting */
1847 static int term_timestamps;
1848 static int64_t term_timestamps_start;
1849 #define MAX_MUX 4
1850 #define MUX_BUFFER_SIZE 32      /* Must be a power of 2.  */
1851 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
1852 typedef struct {
1853     IOCanRWHandler *chr_can_read[MAX_MUX];
1854     IOReadHandler *chr_read[MAX_MUX];
1855     IOEventHandler *chr_event[MAX_MUX];
1856     void *ext_opaque[MAX_MUX];
1857     CharDriverState *drv;
1858     unsigned char buffer[MUX_BUFFER_SIZE];
1859     int prod;
1860     int cons;
1861     int mux_cnt;
1862     int term_got_escape;
1863     int max_size;
1864 } MuxDriver;
1865
1866
1867 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1868 {
1869     MuxDriver *d = chr->opaque;
1870     int ret;
1871     if (!term_timestamps) {
1872         ret = d->drv->chr_write(d->drv, buf, len);
1873     } else {
1874         int i;
1875
1876         ret = 0;
1877         for(i = 0; i < len; i++) {
1878             ret += d->drv->chr_write(d->drv, buf+i, 1);
1879             if (buf[i] == '\n') {
1880                 char buf1[64];
1881                 int64_t ti;
1882                 int secs;
1883
1884                 ti = get_clock();
1885                 if (term_timestamps_start == -1)
1886                     term_timestamps_start = ti;
1887                 ti -= term_timestamps_start;
1888                 secs = ti / 1000000000;
1889                 snprintf(buf1, sizeof(buf1),
1890                          "[%02d:%02d:%02d.%03d] ",
1891                          secs / 3600,
1892                          (secs / 60) % 60,
1893                          secs % 60,
1894                          (int)((ti / 1000000) % 1000));
1895                 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
1896             }
1897         }
1898     }
1899     return ret;
1900 }
1901
1902 static const char * const mux_help[] = {
1903     "% h    print this help\n\r",
1904     "% x    exit emulator\n\r",
1905     "% s    save disk data back to file (if -snapshot)\n\r",
1906     "% t    toggle console timestamps\n\r"
1907     "% b    send break (magic sysrq)\n\r",
1908     "% c    switch between console and monitor\n\r",
1909     "% %  sends %\n\r",
1910     NULL
1911 };
1912
1913 static int term_escape_char = 0x01; /* ctrl-a is used for escape */
1914 static void mux_print_help(CharDriverState *chr)
1915 {
1916     int i, j;
1917     char ebuf[15] = "Escape-Char";
1918     char cbuf[50] = "\n\r";
1919
1920     if (term_escape_char > 0 && term_escape_char < 26) {
1921         snprintf(cbuf, sizeof(cbuf), "\n\r");
1922         snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
1923     } else {
1924         snprintf(cbuf, sizeof(cbuf),
1925                  "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
1926                  term_escape_char);
1927     }
1928     chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
1929     for (i = 0; mux_help[i] != NULL; i++) {
1930         for (j=0; mux_help[i][j] != '\0'; j++) {
1931             if (mux_help[i][j] == '%')
1932                 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
1933             else
1934                 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
1935         }
1936     }
1937 }
1938
1939 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
1940 {
1941     if (d->term_got_escape) {
1942         d->term_got_escape = 0;
1943         if (ch == term_escape_char)
1944             goto send_char;
1945         switch(ch) {
1946         case '?':
1947         case 'h':
1948             mux_print_help(chr);
1949             break;
1950         case 'x':
1951             {
1952                  const char *term =  "QEMU: Terminated\n\r";
1953                  chr->chr_write(chr,(uint8_t *)term,strlen(term));
1954                  exit(0);
1955                  break;
1956             }
1957         case 's':
1958             {
1959                 int i;
1960                 for (i = 0; i < nb_drives; i++) {
1961                         bdrv_commit(drives_table[i].bdrv);
1962                 }
1963             }
1964             break;
1965         case 'b':
1966             qemu_chr_event(chr, CHR_EVENT_BREAK);
1967             break;
1968         case 'c':
1969             /* Switch to the next registered device */
1970             chr->focus++;
1971             if (chr->focus >= d->mux_cnt)
1972                 chr->focus = 0;
1973             break;
1974        case 't':
1975            term_timestamps = !term_timestamps;
1976            term_timestamps_start = -1;
1977            break;
1978         }
1979     } else if (ch == term_escape_char) {
1980         d->term_got_escape = 1;
1981     } else {
1982     send_char:
1983         return 1;
1984     }
1985     return 0;
1986 }
1987
1988 static void mux_chr_accept_input(CharDriverState *chr)
1989 {
1990     int m = chr->focus;
1991     MuxDriver *d = chr->opaque;
1992
1993     while (d->prod != d->cons &&
1994            d->chr_can_read[m] &&
1995            d->chr_can_read[m](d->ext_opaque[m])) {
1996         d->chr_read[m](d->ext_opaque[m],
1997                        &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
1998     }
1999 }
2000
2001 static int mux_chr_can_read(void *opaque)
2002 {
2003     CharDriverState *chr = opaque;
2004     MuxDriver *d = chr->opaque;
2005
2006     if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
2007         return 1;
2008     if (d->chr_can_read[chr->focus])
2009         return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
2010     return 0;
2011 }
2012
2013 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
2014 {
2015     CharDriverState *chr = opaque;
2016     MuxDriver *d = chr->opaque;
2017     int m = chr->focus;
2018     int i;
2019
2020     mux_chr_accept_input (opaque);
2021
2022     for(i = 0; i < size; i++)
2023         if (mux_proc_byte(chr, d, buf[i])) {
2024             if (d->prod == d->cons &&
2025                 d->chr_can_read[m] &&
2026                 d->chr_can_read[m](d->ext_opaque[m]))
2027                 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
2028             else
2029                 d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i];
2030         }
2031 }
2032
2033 static void mux_chr_event(void *opaque, int event)
2034 {
2035     CharDriverState *chr = opaque;
2036     MuxDriver *d = chr->opaque;
2037     int i;
2038
2039     /* Send the event to all registered listeners */
2040     for (i = 0; i < d->mux_cnt; i++)
2041         if (d->chr_event[i])
2042             d->chr_event[i](d->ext_opaque[i], event);
2043 }
2044
2045 static void mux_chr_update_read_handler(CharDriverState *chr)
2046 {
2047     MuxDriver *d = chr->opaque;
2048
2049     if (d->mux_cnt >= MAX_MUX) {
2050         fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
2051         return;
2052     }
2053     d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
2054     d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
2055     d->chr_read[d->mux_cnt] = chr->chr_read;
2056     d->chr_event[d->mux_cnt] = chr->chr_event;
2057     /* Fix up the real driver with mux routines */
2058     if (d->mux_cnt == 0) {
2059         qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
2060                               mux_chr_event, chr);
2061     }
2062     chr->focus = d->mux_cnt;
2063     d->mux_cnt++;
2064 }
2065
2066 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
2067 {
2068     CharDriverState *chr;
2069     MuxDriver *d;
2070
2071     chr = qemu_mallocz(sizeof(CharDriverState));
2072     if (!chr)
2073         return NULL;
2074     d = qemu_mallocz(sizeof(MuxDriver));
2075     if (!d) {
2076         free(chr);
2077         return NULL;
2078     }
2079
2080     chr->opaque = d;
2081     d->drv = drv;
2082     chr->focus = -1;
2083     chr->chr_write = mux_chr_write;
2084     chr->chr_update_read_handler = mux_chr_update_read_handler;
2085     chr->chr_accept_input = mux_chr_accept_input;
2086     return chr;
2087 }
2088
2089
2090 #ifdef _WIN32
2091
2092 static void socket_cleanup(void)
2093 {
2094     WSACleanup();
2095 }
2096
2097 static int socket_init(void)
2098 {
2099     WSADATA Data;
2100     int ret, err;
2101
2102     ret = WSAStartup(MAKEWORD(2,2), &Data);
2103     if (ret != 0) {
2104         err = WSAGetLastError();
2105         fprintf(stderr, "WSAStartup: %d\n", err);
2106         return -1;
2107     }
2108     atexit(socket_cleanup);
2109     return 0;
2110 }
2111
2112 static int send_all(int fd, const uint8_t *buf, int len1)
2113 {
2114     int ret, len;
2115
2116     len = len1;
2117     while (len > 0) {
2118         ret = send(fd, buf, len, 0);
2119         if (ret < 0) {
2120             int errno;
2121             errno = WSAGetLastError();
2122             if (errno != WSAEWOULDBLOCK) {
2123                 return -1;
2124             }
2125         } else if (ret == 0) {
2126             break;
2127         } else {
2128             buf += ret;
2129             len -= ret;
2130         }
2131     }
2132     return len1 - len;
2133 }
2134
2135 #else
2136
2137 static int unix_write(int fd, const uint8_t *buf, int len1)
2138 {
2139     int ret, len;
2140
2141     len = len1;
2142     while (len > 0) {
2143         ret = write(fd, buf, len);
2144         if (ret < 0) {
2145             if (errno != EINTR && errno != EAGAIN)
2146                 return -1;
2147         } else if (ret == 0) {
2148             break;
2149         } else {
2150             buf += ret;
2151             len -= ret;
2152         }
2153     }
2154     return len1 - len;
2155 }
2156
2157 static inline int send_all(int fd, const uint8_t *buf, int len1)
2158 {
2159     return unix_write(fd, buf, len1);
2160 }
2161 #endif /* !_WIN32 */
2162
2163 #ifndef _WIN32
2164
2165 typedef struct {
2166     int fd_in, fd_out;
2167     int max_size;
2168 } FDCharDriver;
2169
2170 #define STDIO_MAX_CLIENTS 1
2171 static int stdio_nb_clients = 0;
2172
2173 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2174 {
2175     FDCharDriver *s = chr->opaque;
2176     return unix_write(s->fd_out, buf, len);
2177 }
2178
2179 static int fd_chr_read_poll(void *opaque)
2180 {
2181     CharDriverState *chr = opaque;
2182     FDCharDriver *s = chr->opaque;
2183
2184     s->max_size = qemu_chr_can_read(chr);
2185     return s->max_size;
2186 }
2187
2188 static void fd_chr_read(void *opaque)
2189 {
2190     CharDriverState *chr = opaque;
2191     FDCharDriver *s = chr->opaque;
2192     int size, len;
2193     uint8_t buf[1024];
2194
2195     len = sizeof(buf);
2196     if (len > s->max_size)
2197         len = s->max_size;
2198     if (len == 0)
2199         return;
2200     size = read(s->fd_in, buf, len);
2201     if (size == 0) {
2202         /* FD has been closed. Remove it from the active list.  */
2203         qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
2204         return;
2205     }
2206     if (size > 0) {
2207         qemu_chr_read(chr, buf, size);
2208     }
2209 }
2210
2211 static void fd_chr_update_read_handler(CharDriverState *chr)
2212 {
2213     FDCharDriver *s = chr->opaque;
2214
2215     if (s->fd_in >= 0) {
2216         if (nographic && s->fd_in == 0) {
2217         } else {
2218             qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
2219                                  fd_chr_read, NULL, chr);
2220         }
2221     }
2222 }
2223
2224 static void fd_chr_close(struct CharDriverState *chr)
2225 {
2226     FDCharDriver *s = chr->opaque;
2227
2228     if (s->fd_in >= 0) {
2229         if (nographic && s->fd_in == 0) {
2230         } else {
2231             qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
2232         }
2233     }
2234
2235     qemu_free(s);
2236 }
2237
2238 /* open a character device to a unix fd */
2239 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
2240 {
2241     CharDriverState *chr;
2242     FDCharDriver *s;
2243
2244     chr = qemu_mallocz(sizeof(CharDriverState));
2245     if (!chr)
2246         return NULL;
2247     s = qemu_mallocz(sizeof(FDCharDriver));
2248     if (!s) {
2249         free(chr);
2250         return NULL;
2251     }
2252     s->fd_in = fd_in;
2253     s->fd_out = fd_out;
2254     chr->opaque = s;
2255     chr->chr_write = fd_chr_write;
2256     chr->chr_update_read_handler = fd_chr_update_read_handler;
2257     chr->chr_close = fd_chr_close;
2258
2259     qemu_chr_reset(chr);
2260
2261     return chr;
2262 }
2263
2264 static CharDriverState *qemu_chr_open_file_out(const char *file_out)
2265 {
2266     int fd_out;
2267
2268     TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
2269     if (fd_out < 0)
2270         return NULL;
2271     return qemu_chr_open_fd(-1, fd_out);
2272 }
2273
2274 static CharDriverState *qemu_chr_open_pipe(const char *filename)
2275 {
2276     int fd_in, fd_out;
2277     char filename_in[256], filename_out[256];
2278
2279     snprintf(filename_in, 256, "%s.in", filename);
2280     snprintf(filename_out, 256, "%s.out", filename);
2281     TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
2282     TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
2283     if (fd_in < 0 || fd_out < 0) {
2284         if (fd_in >= 0)
2285             close(fd_in);
2286         if (fd_out >= 0)
2287             close(fd_out);
2288         TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
2289         if (fd_in < 0)
2290             return NULL;
2291     }
2292     return qemu_chr_open_fd(fd_in, fd_out);
2293 }
2294
2295
2296 /* for STDIO, we handle the case where several clients use it
2297    (nographic mode) */
2298
2299 #define TERM_FIFO_MAX_SIZE 1
2300
2301 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
2302 static int term_fifo_size;
2303
2304 static int stdio_read_poll(void *opaque)
2305 {
2306     CharDriverState *chr = opaque;
2307
2308     /* try to flush the queue if needed */
2309     if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
2310         qemu_chr_read(chr, term_fifo, 1);
2311         term_fifo_size = 0;
2312     }
2313     /* see if we can absorb more chars */
2314     if (term_fifo_size == 0)
2315         return 1;
2316     else
2317         return 0;
2318 }
2319
2320 static void stdio_read(void *opaque)
2321 {
2322     int size;
2323     uint8_t buf[1];
2324     CharDriverState *chr = opaque;
2325
2326     size = read(0, buf, 1);
2327     if (size == 0) {
2328         /* stdin has been closed. Remove it from the active list.  */
2329         qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
2330         return;
2331     }
2332     if (size > 0) {
2333         if (qemu_chr_can_read(chr) > 0) {
2334             qemu_chr_read(chr, buf, 1);
2335         } else if (term_fifo_size == 0) {
2336             term_fifo[term_fifo_size++] = buf[0];
2337         }
2338     }
2339 }
2340
2341 /* init terminal so that we can grab keys */
2342 static struct termios oldtty;
2343 static int old_fd0_flags;
2344 static int term_atexit_done;
2345
2346 static void term_exit(void)
2347 {
2348     tcsetattr (0, TCSANOW, &oldtty);
2349     fcntl(0, F_SETFL, old_fd0_flags);
2350 }
2351
2352 static void term_init(void)
2353 {
2354     struct termios tty;
2355
2356     tcgetattr (0, &tty);
2357     oldtty = tty;
2358     old_fd0_flags = fcntl(0, F_GETFL);
2359
2360     tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2361                           |INLCR|IGNCR|ICRNL|IXON);
2362     tty.c_oflag |= OPOST;
2363     tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
2364     /* if graphical mode, we allow Ctrl-C handling */
2365     if (nographic)
2366         tty.c_lflag &= ~ISIG;
2367     tty.c_cflag &= ~(CSIZE|PARENB);
2368     tty.c_cflag |= CS8;
2369     tty.c_cc[VMIN] = 1;
2370     tty.c_cc[VTIME] = 0;
2371
2372     tcsetattr (0, TCSANOW, &tty);
2373
2374     if (!term_atexit_done++)
2375         atexit(term_exit);
2376
2377     fcntl(0, F_SETFL, O_NONBLOCK);
2378 }
2379
2380 static void qemu_chr_close_stdio(struct CharDriverState *chr)
2381 {
2382     term_exit();
2383     stdio_nb_clients--;
2384     qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
2385     fd_chr_close(chr);
2386 }
2387
2388 static CharDriverState *qemu_chr_open_stdio(void)
2389 {
2390     CharDriverState *chr;
2391
2392     if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
2393         return NULL;
2394     chr = qemu_chr_open_fd(0, 1);
2395     chr->chr_close = qemu_chr_close_stdio;
2396     qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
2397     stdio_nb_clients++;
2398     term_init();
2399
2400     return chr;
2401 }
2402
2403 #ifdef __sun__
2404 /* Once Solaris has openpty(), this is going to be removed. */
2405 int openpty(int *amaster, int *aslave, char *name,
2406             struct termios *termp, struct winsize *winp)
2407 {
2408         const char *slave;
2409         int mfd = -1, sfd = -1;
2410
2411         *amaster = *aslave = -1;
2412
2413         mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
2414         if (mfd < 0)
2415                 goto err;
2416
2417         if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
2418                 goto err;
2419
2420         if ((slave = ptsname(mfd)) == NULL)
2421                 goto err;
2422
2423         if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
2424                 goto err;
2425
2426         if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
2427             (termp != NULL && tcgetattr(sfd, termp) < 0))
2428                 goto err;
2429
2430         if (amaster)
2431                 *amaster = mfd;
2432         if (aslave)
2433                 *aslave = sfd;
2434         if (winp)
2435                 ioctl(sfd, TIOCSWINSZ, winp);
2436
2437         return 0;
2438
2439 err:
2440         if (sfd != -1)
2441                 close(sfd);
2442         close(mfd);
2443         return -1;
2444 }
2445
2446 void cfmakeraw (struct termios *termios_p)
2447 {
2448         termios_p->c_iflag &=
2449                 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
2450         termios_p->c_oflag &= ~OPOST;
2451         termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
2452         termios_p->c_cflag &= ~(CSIZE|PARENB);
2453         termios_p->c_cflag |= CS8;
2454
2455         termios_p->c_cc[VMIN] = 0;
2456         termios_p->c_cc[VTIME] = 0;
2457 }
2458 #endif
2459
2460 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2461     || defined(__NetBSD__) || defined(__OpenBSD__)
2462
2463 typedef struct {
2464     int fd;
2465     int connected;
2466     int polling;
2467     int read_bytes;
2468     QEMUTimer *timer;
2469 } PtyCharDriver;
2470
2471 static void pty_chr_update_read_handler(CharDriverState *chr);
2472 static void pty_chr_state(CharDriverState *chr, int connected);
2473
2474 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2475 {
2476     PtyCharDriver *s = chr->opaque;
2477
2478     if (!s->connected) {
2479         /* guest sends data, check for (re-)connect */
2480         pty_chr_update_read_handler(chr);
2481         return 0;
2482     }
2483     return unix_write(s->fd, buf, len);
2484 }
2485
2486 static int pty_chr_read_poll(void *opaque)
2487 {
2488     CharDriverState *chr = opaque;
2489     PtyCharDriver *s = chr->opaque;
2490
2491     s->read_bytes = qemu_chr_can_read(chr);
2492     return s->read_bytes;
2493 }
2494
2495 static void pty_chr_read(void *opaque)
2496 {
2497     CharDriverState *chr = opaque;
2498     PtyCharDriver *s = chr->opaque;
2499     int size, len;
2500     uint8_t buf[1024];
2501
2502     len = sizeof(buf);
2503     if (len > s->read_bytes)
2504         len = s->read_bytes;
2505     if (len == 0)
2506         return;
2507     size = read(s->fd, buf, len);
2508     if ((size == -1 && errno == EIO) ||
2509         (size == 0)) {
2510         pty_chr_state(chr, 0);
2511         return;
2512     }
2513     if (size > 0) {
2514         pty_chr_state(chr, 1);
2515         qemu_chr_read(chr, buf, size);
2516     }
2517 }
2518
2519 static void pty_chr_update_read_handler(CharDriverState *chr)
2520 {
2521     PtyCharDriver *s = chr->opaque;
2522
2523     qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
2524                          pty_chr_read, NULL, chr);
2525     s->polling = 1;
2526     /*
2527      * Short timeout here: just need wait long enougth that qemu makes
2528      * it through the poll loop once.  When reconnected we want a
2529      * short timeout so we notice it almost instantly.  Otherwise
2530      * read() gives us -EIO instantly, making pty_chr_state() reset the
2531      * timeout to the normal (much longer) poll interval before the
2532      * timer triggers.
2533      */
2534     qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
2535 }
2536
2537 static void pty_chr_state(CharDriverState *chr, int connected)
2538 {
2539     PtyCharDriver *s = chr->opaque;
2540
2541     if (!connected) {
2542         qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
2543         s->connected = 0;
2544         s->polling = 0;
2545         /* (re-)connect poll interval for idle guests: once per second.
2546          * We check more frequently in case the guests sends data to
2547          * the virtual device linked to our pty. */
2548         qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
2549     } else {
2550         if (!s->connected)
2551             qemu_chr_reset(chr);
2552         s->connected = 1;
2553     }
2554 }
2555
2556 static void pty_chr_timer(void *opaque)
2557 {
2558     struct CharDriverState *chr = opaque;
2559     PtyCharDriver *s = chr->opaque;
2560
2561     if (s->connected)
2562         return;
2563     if (s->polling) {
2564         /* If we arrive here without polling being cleared due
2565          * read returning -EIO, then we are (re-)connected */
2566         pty_chr_state(chr, 1);
2567         return;
2568     }
2569
2570     /* Next poll ... */
2571     pty_chr_update_read_handler(chr);
2572 }
2573
2574 static void pty_chr_close(struct CharDriverState *chr)
2575 {
2576     PtyCharDriver *s = chr->opaque;
2577
2578     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
2579     close(s->fd);
2580     qemu_free(s);
2581 }
2582
2583 static CharDriverState *qemu_chr_open_pty(void)
2584 {
2585     CharDriverState *chr;
2586     PtyCharDriver *s;
2587     struct termios tty;
2588     int slave_fd;
2589 #if defined(__OpenBSD__)
2590     char pty_name[PATH_MAX];
2591 #define q_ptsname(x) pty_name
2592 #else
2593     char *pty_name = NULL;
2594 #define q_ptsname(x) ptsname(x)
2595 #endif
2596
2597     chr = qemu_mallocz(sizeof(CharDriverState));
2598     if (!chr)
2599         return NULL;
2600     s = qemu_mallocz(sizeof(PtyCharDriver));
2601     if (!s) {
2602         qemu_free(chr);
2603         return NULL;
2604     }
2605
2606     if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
2607         return NULL;
2608     }
2609
2610     /* Set raw attributes on the pty. */
2611     cfmakeraw(&tty);
2612     tcsetattr(slave_fd, TCSAFLUSH, &tty);
2613     close(slave_fd);
2614
2615     fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
2616
2617     chr->opaque = s;
2618     chr->chr_write = pty_chr_write;
2619     chr->chr_update_read_handler = pty_chr_update_read_handler;
2620     chr->chr_close = pty_chr_close;
2621
2622     s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
2623
2624     return chr;
2625 }
2626
2627 static void tty_serial_init(int fd, int speed,
2628                             int parity, int data_bits, int stop_bits)
2629 {
2630     struct termios tty;
2631     speed_t spd;
2632
2633 #if 0
2634     printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
2635            speed, parity, data_bits, stop_bits);
2636 #endif
2637     tcgetattr (fd, &tty);
2638
2639 #define MARGIN 1.1
2640     if (speed <= 50 * MARGIN)
2641         spd = B50;
2642     else if (speed <= 75 * MARGIN)
2643         spd = B75;
2644     else if (speed <= 300 * MARGIN)
2645         spd = B300;
2646     else if (speed <= 600 * MARGIN)
2647         spd = B600;
2648     else if (speed <= 1200 * MARGIN)
2649         spd = B1200;
2650     else if (speed <= 2400 * MARGIN)
2651         spd = B2400;
2652     else if (speed <= 4800 * MARGIN)
2653         spd = B4800;
2654     else if (speed <= 9600 * MARGIN)
2655         spd = B9600;
2656     else if (speed <= 19200 * MARGIN)
2657         spd = B19200;
2658     else if (speed <= 38400 * MARGIN)
2659         spd = B38400;
2660     else if (speed <= 57600 * MARGIN)
2661         spd = B57600;
2662     else if (speed <= 115200 * MARGIN)
2663         spd = B115200;
2664     else
2665         spd = B115200;
2666
2667     cfsetispeed(&tty, spd);
2668     cfsetospeed(&tty, spd);
2669
2670     tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2671                           |INLCR|IGNCR|ICRNL|IXON);
2672     tty.c_oflag |= OPOST;
2673     tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
2674     tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
2675     switch(data_bits) {
2676     default:
2677     case 8:
2678         tty.c_cflag |= CS8;
2679         break;
2680     case 7:
2681         tty.c_cflag |= CS7;
2682         break;
2683     case 6:
2684         tty.c_cflag |= CS6;
2685         break;
2686     case 5:
2687         tty.c_cflag |= CS5;
2688         break;
2689     }
2690     switch(parity) {
2691     default:
2692     case 'N':
2693         break;
2694     case 'E':
2695         tty.c_cflag |= PARENB;
2696         break;
2697     case 'O':
2698         tty.c_cflag |= PARENB | PARODD;
2699         break;
2700     }
2701     if (stop_bits == 2)
2702         tty.c_cflag |= CSTOPB;
2703
2704     tcsetattr (fd, TCSANOW, &tty);
2705 }
2706
2707 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
2708 {
2709     FDCharDriver *s = chr->opaque;
2710
2711     switch(cmd) {
2712     case CHR_IOCTL_SERIAL_SET_PARAMS:
2713         {
2714             QEMUSerialSetParams *ssp = arg;
2715             tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
2716                             ssp->data_bits, ssp->stop_bits);
2717         }
2718         break;
2719     case CHR_IOCTL_SERIAL_SET_BREAK:
2720         {
2721             int enable = *(int *)arg;
2722             if (enable)
2723                 tcsendbreak(s->fd_in, 1);
2724         }
2725         break;
2726     case CHR_IOCTL_SERIAL_GET_TIOCM:
2727         {
2728             int sarg = 0;
2729             int *targ = (int *)arg;
2730             ioctl(s->fd_in, TIOCMGET, &sarg);
2731             *targ = 0;
2732             if (sarg | TIOCM_CTS)
2733                 *targ |= CHR_TIOCM_CTS;
2734             if (sarg | TIOCM_CAR)
2735                 *targ |= CHR_TIOCM_CAR;
2736             if (sarg | TIOCM_DSR)
2737                 *targ |= CHR_TIOCM_DSR;
2738             if (sarg | TIOCM_RI)
2739                 *targ |= CHR_TIOCM_RI;
2740             if (sarg | TIOCM_DTR)
2741                 *targ |= CHR_TIOCM_DTR;
2742             if (sarg | TIOCM_RTS)
2743                 *targ |= CHR_TIOCM_RTS;
2744         }
2745         break;
2746     case CHR_IOCTL_SERIAL_SET_TIOCM:
2747         {
2748             int sarg = *(int *)arg;
2749             int targ = 0;
2750             if (sarg | CHR_TIOCM_DTR)
2751                 targ |= TIOCM_DTR;
2752             if (sarg | CHR_TIOCM_RTS)
2753                 targ |= TIOCM_RTS;
2754             ioctl(s->fd_in, TIOCMSET, &targ);
2755         }
2756         break;
2757     default:
2758         return -ENOTSUP;
2759     }
2760     return 0;
2761 }
2762
2763 static CharDriverState *qemu_chr_open_tty(const char *filename)
2764 {
2765     CharDriverState *chr;
2766     int fd;
2767
2768     TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
2769     tty_serial_init(fd, 115200, 'N', 8, 1);
2770     chr = qemu_chr_open_fd(fd, fd);
2771     if (!chr) {
2772         close(fd);
2773         return NULL;
2774     }
2775     chr->chr_ioctl = tty_serial_ioctl;
2776     qemu_chr_reset(chr);
2777     return chr;
2778 }
2779 #else  /* ! __linux__ && ! __sun__ */
2780 static CharDriverState *qemu_chr_open_pty(void)
2781 {
2782     return NULL;
2783 }
2784 #endif /* __linux__ || __sun__ */
2785
2786 #if defined(__linux__)
2787 typedef struct {
2788     int fd;
2789     int mode;
2790 } ParallelCharDriver;
2791
2792 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
2793 {
2794     if (s->mode != mode) {
2795         int m = mode;
2796         if (ioctl(s->fd, PPSETMODE, &m) < 0)
2797             return 0;
2798         s->mode = mode;
2799     }
2800     return 1;
2801 }
2802
2803 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
2804 {
2805     ParallelCharDriver *drv = chr->opaque;
2806     int fd = drv->fd;
2807     uint8_t b;
2808
2809     switch(cmd) {
2810     case CHR_IOCTL_PP_READ_DATA:
2811         if (ioctl(fd, PPRDATA, &b) < 0)
2812             return -ENOTSUP;
2813         *(uint8_t *)arg = b;
2814         break;
2815     case CHR_IOCTL_PP_WRITE_DATA:
2816         b = *(uint8_t *)arg;
2817         if (ioctl(fd, PPWDATA, &b) < 0)
2818             return -ENOTSUP;
2819         break;
2820     case CHR_IOCTL_PP_READ_CONTROL:
2821         if (ioctl(fd, PPRCONTROL, &b) < 0)
2822             return -ENOTSUP;
2823         /* Linux gives only the lowest bits, and no way to know data
2824            direction! For better compatibility set the fixed upper
2825            bits. */
2826         *(uint8_t *)arg = b | 0xc0;
2827         break;
2828     case CHR_IOCTL_PP_WRITE_CONTROL:
2829         b = *(uint8_t *)arg;
2830         if (ioctl(fd, PPWCONTROL, &b) < 0)
2831             return -ENOTSUP;
2832         break;
2833     case CHR_IOCTL_PP_READ_STATUS:
2834         if (ioctl(fd, PPRSTATUS, &b) < 0)
2835             return -ENOTSUP;
2836         *(uint8_t *)arg = b;
2837         break;
2838     case CHR_IOCTL_PP_DATA_DIR:
2839         if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
2840             return -ENOTSUP;
2841         break;
2842     case CHR_IOCTL_PP_EPP_READ_ADDR:
2843         if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2844             struct ParallelIOArg *parg = arg;
2845             int n = read(fd, parg->buffer, parg->count);
2846             if (n != parg->count) {
2847                 return -EIO;
2848             }
2849         }
2850         break;
2851     case CHR_IOCTL_PP_EPP_READ:
2852         if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2853             struct ParallelIOArg *parg = arg;
2854             int n = read(fd, parg->buffer, parg->count);
2855             if (n != parg->count) {
2856                 return -EIO;
2857             }
2858         }
2859         break;
2860     case CHR_IOCTL_PP_EPP_WRITE_ADDR:
2861         if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2862             struct ParallelIOArg *parg = arg;
2863             int n = write(fd, parg->buffer, parg->count);
2864             if (n != parg->count) {
2865                 return -EIO;
2866             }
2867         }
2868         break;
2869     case CHR_IOCTL_PP_EPP_WRITE:
2870         if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2871             struct ParallelIOArg *parg = arg;
2872             int n = write(fd, parg->buffer, parg->count);
2873             if (n != parg->count) {
2874                 return -EIO;
2875             }
2876         }
2877         break;
2878     default:
2879         return -ENOTSUP;
2880     }
2881     return 0;
2882 }
2883
2884 static void pp_close(CharDriverState *chr)
2885 {
2886     ParallelCharDriver *drv = chr->opaque;
2887     int fd = drv->fd;
2888
2889     pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
2890     ioctl(fd, PPRELEASE);
2891     close(fd);
2892     qemu_free(drv);
2893 }
2894
2895 static CharDriverState *qemu_chr_open_pp(const char *filename)
2896 {
2897     CharDriverState *chr;
2898     ParallelCharDriver *drv;
2899     int fd;
2900
2901     TFR(fd = open(filename, O_RDWR));
2902     if (fd < 0)
2903         return NULL;
2904
2905     if (ioctl(fd, PPCLAIM) < 0) {
2906         close(fd);
2907         return NULL;
2908     }
2909
2910     drv = qemu_mallocz(sizeof(ParallelCharDriver));
2911     if (!drv) {
2912         close(fd);
2913         return NULL;
2914     }
2915     drv->fd = fd;
2916     drv->mode = IEEE1284_MODE_COMPAT;
2917
2918     chr = qemu_mallocz(sizeof(CharDriverState));
2919     if (!chr) {
2920         qemu_free(drv);
2921         close(fd);
2922         return NULL;
2923     }
2924     chr->chr_write = null_chr_write;
2925     chr->chr_ioctl = pp_ioctl;
2926     chr->chr_close = pp_close;
2927     chr->opaque = drv;
2928
2929     qemu_chr_reset(chr);
2930
2931     return chr;
2932 }
2933 #endif /* __linux__ */
2934
2935 #else /* _WIN32 */
2936
2937 typedef struct {
2938     int max_size;
2939     HANDLE hcom, hrecv, hsend;
2940     OVERLAPPED orecv, osend;
2941     BOOL fpipe;
2942     DWORD len;
2943 } WinCharState;
2944
2945 #define NSENDBUF 2048
2946 #define NRECVBUF 2048
2947 #define MAXCONNECT 1
2948 #define NTIMEOUT 5000
2949
2950 static int win_chr_poll(void *opaque);
2951 static int win_chr_pipe_poll(void *opaque);
2952
2953 static void win_chr_close(CharDriverState *chr)
2954 {
2955     WinCharState *s = chr->opaque;
2956
2957     if (s->hsend) {
2958         CloseHandle(s->hsend);
2959         s->hsend = NULL;
2960     }
2961     if (s->hrecv) {
2962         CloseHandle(s->hrecv);
2963         s->hrecv = NULL;
2964     }
2965     if (s->hcom) {
2966         CloseHandle(s->hcom);
2967         s->hcom = NULL;
2968     }
2969     if (s->fpipe)
2970         qemu_del_polling_cb(win_chr_pipe_poll, chr);
2971     else
2972         qemu_del_polling_cb(win_chr_poll, chr);
2973 }
2974
2975 static int win_chr_init(CharDriverState *chr, const char *filename)
2976 {
2977     WinCharState *s = chr->opaque;
2978     COMMCONFIG comcfg;
2979     COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
2980     COMSTAT comstat;
2981     DWORD size;
2982     DWORD err;
2983
2984     s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2985     if (!s->hsend) {
2986         fprintf(stderr, "Failed CreateEvent\n");
2987         goto fail;
2988     }
2989     s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2990     if (!s->hrecv) {
2991         fprintf(stderr, "Failed CreateEvent\n");
2992         goto fail;
2993     }
2994
2995     s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
2996                       OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
2997     if (s->hcom == INVALID_HANDLE_VALUE) {
2998         fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
2999         s->hcom = NULL;
3000         goto fail;
3001     }
3002
3003     if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
3004         fprintf(stderr, "Failed SetupComm\n");
3005         goto fail;
3006     }
3007
3008     ZeroMemory(&comcfg, sizeof(COMMCONFIG));
3009     size = sizeof(COMMCONFIG);
3010     GetDefaultCommConfig(filename, &comcfg, &size);
3011     comcfg.dcb.DCBlength = sizeof(DCB);
3012     CommConfigDialog(filename, NULL, &comcfg);
3013
3014     if (!SetCommState(s->hcom, &comcfg.dcb)) {
3015         fprintf(stderr, "Failed SetCommState\n");
3016         goto fail;
3017     }
3018
3019     if (!SetCommMask(s->hcom, EV_ERR)) {
3020         fprintf(stderr, "Failed SetCommMask\n");
3021         goto fail;
3022     }
3023
3024     cto.ReadIntervalTimeout = MAXDWORD;
3025     if (!SetCommTimeouts(s->hcom, &cto)) {
3026         fprintf(stderr, "Failed SetCommTimeouts\n");
3027         goto fail;
3028     }
3029
3030     if (!ClearCommError(s->hcom, &err, &comstat)) {
3031         fprintf(stderr, "Failed ClearCommError\n");
3032         goto fail;
3033     }
3034     qemu_add_polling_cb(win_chr_poll, chr);
3035     return 0;
3036
3037  fail:
3038     win_chr_close(chr);
3039     return -1;
3040 }
3041
3042 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
3043 {
3044     WinCharState *s = chr->opaque;
3045     DWORD len, ret, size, err;
3046
3047     len = len1;
3048     ZeroMemory(&s->osend, sizeof(s->osend));
3049     s->osend.hEvent = s->hsend;
3050     while (len > 0) {
3051         if (s->hsend)
3052             ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
3053         else
3054             ret = WriteFile(s->hcom, buf, len, &size, NULL);
3055         if (!ret) {
3056             err = GetLastError();
3057             if (err == ERROR_IO_PENDING) {
3058                 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
3059                 if (ret) {
3060                     buf += size;
3061                     len -= size;
3062                 } else {
3063                     break;
3064                 }
3065             } else {
3066                 break;
3067             }
3068         } else {
3069             buf += size;
3070             len -= size;
3071         }
3072     }
3073     return len1 - len;
3074 }
3075
3076 static int win_chr_read_poll(CharDriverState *chr)
3077 {
3078     WinCharState *s = chr->opaque;
3079
3080     s->max_size = qemu_chr_can_read(chr);
3081     return s->max_size;
3082 }
3083
3084 static void win_chr_readfile(CharDriverState *chr)
3085 {
3086     WinCharState *s = chr->opaque;
3087     int ret, err;
3088     uint8_t buf[1024];
3089     DWORD size;
3090
3091     ZeroMemory(&s->orecv, sizeof(s->orecv));
3092     s->orecv.hEvent = s->hrecv;
3093     ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
3094     if (!ret) {
3095         err = GetLastError();
3096         if (err == ERROR_IO_PENDING) {
3097             ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
3098         }
3099     }
3100
3101     if (size > 0) {
3102         qemu_chr_read(chr, buf, size);
3103     }
3104 }
3105
3106 static void win_chr_read(CharDriverState *chr)
3107 {
3108     WinCharState *s = chr->opaque;
3109
3110     if (s->len > s->max_size)
3111         s->len = s->max_size;
3112     if (s->len == 0)
3113         return;
3114
3115     win_chr_readfile(chr);
3116 }
3117
3118 static int win_chr_poll(void *opaque)
3119 {
3120     CharDriverState *chr = opaque;
3121     WinCharState *s = chr->opaque;
3122     COMSTAT status;
3123     DWORD comerr;
3124
3125     ClearCommError(s->hcom, &comerr, &status);
3126     if (status.cbInQue > 0) {
3127         s->len = status.cbInQue;
3128         win_chr_read_poll(chr);
3129         win_chr_read(chr);
3130         return 1;
3131     }
3132     return 0;
3133 }
3134
3135 static CharDriverState *qemu_chr_open_win(const char *filename)
3136 {
3137     CharDriverState *chr;
3138     WinCharState *s;
3139
3140     chr = qemu_mallocz(sizeof(CharDriverState));
3141     if (!chr)
3142         return NULL;
3143     s = qemu_mallocz(sizeof(WinCharState));
3144     if (!s) {
3145         free(chr);
3146         return NULL;
3147     }
3148     chr->opaque = s;
3149     chr->chr_write = win_chr_write;
3150     chr->chr_close = win_chr_close;
3151
3152     if (win_chr_init(chr, filename) < 0) {
3153         free(s);
3154         free(chr);
3155         return NULL;
3156     }
3157     qemu_chr_reset(chr);
3158     return chr;
3159 }
3160
3161 static int win_chr_pipe_poll(void *opaque)
3162 {
3163     CharDriverState *chr = opaque;
3164     WinCharState *s = chr->opaque;
3165     DWORD size;
3166
3167     PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
3168     if (size > 0) {
3169         s->len = size;
3170         win_chr_read_poll(chr);
3171         win_chr_read(chr);
3172         return 1;
3173     }
3174     return 0;
3175 }
3176
3177 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
3178 {
3179     WinCharState *s = chr->opaque;
3180     OVERLAPPED ov;
3181     int ret;
3182     DWORD size;
3183     char openname[256];
3184
3185     s->fpipe = TRUE;
3186
3187     s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
3188     if (!s->hsend) {
3189         fprintf(stderr, "Failed CreateEvent\n");
3190         goto fail;
3191     }
3192     s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
3193     if (!s->hrecv) {
3194         fprintf(stderr, "Failed CreateEvent\n");
3195         goto fail;
3196     }
3197
3198     snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
3199     s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
3200                               PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
3201                               PIPE_WAIT,
3202                               MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
3203     if (s->hcom == INVALID_HANDLE_VALUE) {
3204         fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
3205         s->hcom = NULL;
3206         goto fail;
3207     }
3208
3209     ZeroMemory(&ov, sizeof(ov));
3210     ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
3211     ret = ConnectNamedPipe(s->hcom, &ov);
3212     if (ret) {
3213         fprintf(stderr, "Failed ConnectNamedPipe\n");
3214         goto fail;
3215     }
3216
3217     ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
3218     if (!ret) {
3219         fprintf(stderr, "Failed GetOverlappedResult\n");
3220         if (ov.hEvent) {
3221             CloseHandle(ov.hEvent);
3222             ov.hEvent = NULL;
3223         }
3224         goto fail;
3225     }
3226
3227     if (ov.hEvent) {
3228         CloseHandle(ov.hEvent);
3229         ov.hEvent = NULL;
3230     }
3231     qemu_add_polling_cb(win_chr_pipe_poll, chr);
3232     return 0;
3233
3234  fail:
3235     win_chr_close(chr);
3236     return -1;
3237 }
3238
3239
3240 static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
3241 {
3242     CharDriverState *chr;
3243     WinCharState *s;
3244
3245     chr = qemu_mallocz(sizeof(CharDriverState));
3246     if (!chr)
3247         return NULL;
3248     s = qemu_mallocz(sizeof(WinCharState));
3249     if (!s) {
3250         free(chr);
3251         return NULL;
3252     }
3253     chr->opaque = s;
3254     chr->chr_write = win_chr_write;
3255     chr->chr_close = win_chr_close;
3256
3257     if (win_chr_pipe_init(chr, filename) < 0) {
3258         free(s);
3259         free(chr);
3260         return NULL;
3261     }
3262     qemu_chr_reset(chr);
3263     return chr;
3264 }
3265
3266 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
3267 {
3268     CharDriverState *chr;
3269     WinCharState *s;
3270
3271     chr = qemu_mallocz(sizeof(CharDriverState));
3272     if (!chr)
3273         return NULL;
3274     s = qemu_mallocz(sizeof(WinCharState));
3275     if (!s) {
3276         free(chr);
3277         return NULL;
3278     }
3279     s->hcom = fd_out;
3280     chr->opaque = s;
3281     chr->chr_write = win_chr_write;
3282     qemu_chr_reset(chr);
3283     return chr;
3284 }
3285
3286 static CharDriverState *qemu_chr_open_win_con(const char *filename)
3287 {
3288     return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
3289 }
3290
3291 static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
3292 {
3293     HANDLE fd_out;
3294
3295     fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3296                         OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3297     if (fd_out == INVALID_HANDLE_VALUE)
3298         return NULL;
3299
3300     return qemu_chr_open_win_file(fd_out);
3301 }
3302 #endif /* !_WIN32 */
3303
3304 /***********************************************************/
3305 /* UDP Net console */
3306
3307 typedef struct {
3308     int fd;
3309     struct sockaddr_in daddr;
3310     uint8_t buf[1024];
3311     int bufcnt;
3312     int bufptr;
3313     int max_size;
3314 } NetCharDriver;
3315
3316 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
3317 {
3318     NetCharDriver *s = chr->opaque;
3319
3320     return sendto(s->fd, buf, len, 0,
3321                   (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
3322 }
3323
3324 static int udp_chr_read_poll(void *opaque)
3325 {
3326     CharDriverState *chr = opaque;
3327     NetCharDriver *s = chr->opaque;
3328
3329     s->max_size = qemu_chr_can_read(chr);
3330
3331     /* If there were any stray characters in the queue process them
3332      * first
3333      */
3334     while (s->max_size > 0 && s->bufptr < s->bufcnt) {
3335         qemu_chr_read(chr, &s->buf[s->bufptr], 1);
3336         s->bufptr++;
3337         s->max_size = qemu_chr_can_read(chr);
3338     }
3339     return s->max_size;
3340 }
3341
3342 static void udp_chr_read(void *opaque)
3343 {
3344     CharDriverState *chr = opaque;
3345     NetCharDriver *s = chr->opaque;
3346
3347     if (s->max_size == 0)
3348         return;
3349     s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
3350     s->bufptr = s->bufcnt;
3351     if (s->bufcnt <= 0)
3352         return;
3353
3354     s->bufptr = 0;
3355     while (s->max_size > 0 && s->bufptr < s->bufcnt) {
3356         qemu_chr_read(chr, &s->buf[s->bufptr], 1);
3357         s->bufptr++;
3358         s->max_size = qemu_chr_can_read(chr);
3359     }
3360 }
3361
3362 static void udp_chr_update_read_handler(CharDriverState *chr)
3363 {
3364     NetCharDriver *s = chr->opaque;
3365
3366     if (s->fd >= 0) {
3367         qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
3368                              udp_chr_read, NULL, chr);
3369     }
3370 }
3371
3372 int parse_host_port(struct sockaddr_in *saddr, const char *str);
3373 #ifndef _WIN32
3374 static int parse_unix_path(struct sockaddr_un *uaddr, const char *str);
3375 #endif
3376 int parse_host_src_port(struct sockaddr_in *haddr,
3377                         struct sockaddr_in *saddr,
3378                         const char *str);
3379
3380 static CharDriverState *qemu_chr_open_udp(const char *def)
3381 {
3382     CharDriverState *chr = NULL;
3383     NetCharDriver *s = NULL;
3384     int fd = -1;
3385     struct sockaddr_in saddr;
3386
3387     chr = qemu_mallocz(sizeof(CharDriverState));
3388     if (!chr)
3389         goto return_err;
3390     s = qemu_mallocz(sizeof(NetCharDriver));
3391     if (!s)
3392         goto return_err;
3393
3394     fd = socket(PF_INET, SOCK_DGRAM, 0);
3395     if (fd < 0) {
3396         perror("socket(PF_INET, SOCK_DGRAM)");
3397         goto return_err;
3398     }
3399
3400     if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
3401         printf("Could not parse: %s\n", def);
3402         goto return_err;
3403     }
3404
3405     if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
3406     {
3407         perror("bind");
3408         goto return_err;
3409     }
3410
3411     s->fd = fd;
3412     s->bufcnt = 0;
3413     s->bufptr = 0;
3414     chr->opaque = s;
3415     chr->chr_write = udp_chr_write;
3416     chr->chr_update_read_handler = udp_chr_update_read_handler;
3417     return chr;
3418
3419 return_err:
3420     if (chr)
3421         free(chr);
3422     if (s)
3423         free(s);
3424     if (fd >= 0)
3425         closesocket(fd);
3426     return NULL;
3427 }
3428
3429 /***********************************************************/
3430 /* TCP Net console */
3431
3432 typedef struct {
3433     int fd, listen_fd;
3434     int connected;
3435     int max_size;
3436     int do_telnetopt;
3437     int do_nodelay;
3438     int is_unix;
3439 } TCPCharDriver;
3440
3441 static void tcp_chr_accept(void *opaque);
3442
3443 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
3444 {
3445     TCPCharDriver *s = chr->opaque;
3446     if (s->connected) {
3447         return send_all(s->fd, buf, len);
3448     } else {
3449         /* XXX: indicate an error ? */
3450         return len;
3451     }
3452 }
3453
3454 static int tcp_chr_read_poll(void *opaque)
3455 {
3456     CharDriverState *chr = opaque;
3457     TCPCharDriver *s = chr->opaque;
3458     if (!s->connected)
3459         return 0;
3460     s->max_size = qemu_chr_can_read(chr);
3461     return s->max_size;
3462 }
3463
3464 #define IAC 255
3465 #define IAC_BREAK 243
3466 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
3467                                       TCPCharDriver *s,
3468                                       uint8_t *buf, int *size)
3469 {
3470     /* Handle any telnet client's basic IAC options to satisfy char by
3471      * char mode with no echo.  All IAC options will be removed from
3472      * the buf and the do_telnetopt variable will be used to track the
3473      * state of the width of the IAC information.
3474      *
3475      * IAC commands come in sets of 3 bytes with the exception of the
3476      * "IAC BREAK" command and the double IAC.
3477      */
3478
3479     int i;
3480     int j = 0;
3481
3482     for (i = 0; i < *size; i++) {
3483         if (s->do_telnetopt > 1) {
3484             if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
3485                 /* Double IAC means send an IAC */
3486                 if (j != i)
3487                     buf[j] = buf[i];
3488                 j++;
3489                 s->do_telnetopt = 1;
3490             } else {
3491                 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
3492                     /* Handle IAC break commands by sending a serial break */
3493                     qemu_chr_event(chr, CHR_EVENT_BREAK);
3494                     s->do_telnetopt++;
3495                 }
3496                 s->do_telnetopt++;
3497             }
3498             if (s->do_telnetopt >= 4) {
3499                 s->do_telnetopt = 1;
3500             }
3501         } else {
3502             if ((unsigned char)buf[i] == IAC) {
3503                 s->do_telnetopt = 2;
3504             } else {
3505                 if (j != i)
3506                     buf[j] = buf[i];
3507                 j++;
3508             }
3509         }
3510     }
3511     *size = j;
3512 }
3513
3514 static void tcp_chr_read(void *opaque)
3515 {
3516     CharDriverState *chr = opaque;
3517     TCPCharDriver *s = chr->opaque;
3518     uint8_t buf[1024];
3519     int len, size;
3520
3521     if (!s->connected || s->max_size <= 0)
3522         return;
3523     len = sizeof(buf);
3524     if (len > s->max_size)
3525         len = s->max_size;
3526     size = recv(s->fd, buf, len, 0);
3527     if (size == 0) {
3528         /* connection closed */
3529         s->connected = 0;
3530         if (s->listen_fd >= 0) {
3531             qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
3532         }
3533         qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
3534         closesocket(s->fd);
3535         s->fd = -1;
3536     } else if (size > 0) {
3537         if (s->do_telnetopt)
3538             tcp_chr_process_IAC_bytes(chr, s, buf, &size);
3539         if (size > 0)
3540             qemu_chr_read(chr, buf, size);
3541     }
3542 }
3543
3544 static void tcp_chr_connect(void *opaque)
3545 {
3546     CharDriverState *chr = opaque;
3547     TCPCharDriver *s = chr->opaque;
3548
3549     s->connected = 1;
3550     qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
3551                          tcp_chr_read, NULL, chr);
3552     qemu_chr_reset(chr);
3553 }
3554
3555 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3556 static void tcp_chr_telnet_init(int fd)
3557 {
3558     char buf[3];
3559     /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3560     IACSET(buf, 0xff, 0xfb, 0x01);  /* IAC WILL ECHO */
3561     send(fd, (char *)buf, 3, 0);
3562     IACSET(buf, 0xff, 0xfb, 0x03);  /* IAC WILL Suppress go ahead */
3563     send(fd, (char *)buf, 3, 0);
3564     IACSET(buf, 0xff, 0xfb, 0x00);  /* IAC WILL Binary */
3565     send(fd, (char *)buf, 3, 0);
3566     IACSET(buf, 0xff, 0xfd, 0x00);  /* IAC DO Binary */
3567     send(fd, (char *)buf, 3, 0);
3568 }
3569
3570 static void socket_set_nodelay(int fd)
3571 {
3572     int val = 1;
3573     setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
3574 }
3575
3576 static void tcp_chr_accept(void *opaque)
3577 {
3578     CharDriverState *chr = opaque;
3579     TCPCharDriver *s = chr->opaque;
3580     struct sockaddr_in saddr;
3581 #ifndef _WIN32
3582     struct sockaddr_un uaddr;
3583 #endif
3584     struct sockaddr *addr;
3585     socklen_t len;
3586     int fd;
3587
3588     for(;;) {
3589 #ifndef _WIN32
3590         if (s->is_unix) {
3591             len = sizeof(uaddr);
3592             addr = (struct sockaddr *)&uaddr;
3593         } else
3594 #endif
3595         {
3596             len = sizeof(saddr);
3597             addr = (struct sockaddr *)&saddr;
3598         }
3599         fd = accept(s->listen_fd, addr, &len);
3600         if (fd < 0 && errno != EINTR) {
3601             return;
3602         } else if (fd >= 0) {
3603             if (s->do_telnetopt)
3604                 tcp_chr_telnet_init(fd);
3605             break;
3606         }
3607     }
3608     socket_set_nonblock(fd);
3609     if (s->do_nodelay)
3610         socket_set_nodelay(fd);
3611     s->fd = fd;
3612     qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
3613     tcp_chr_connect(chr);
3614 }
3615
3616 static void tcp_chr_close(CharDriverState *chr)
3617 {
3618     TCPCharDriver *s = chr->opaque;
3619     if (s->fd >= 0)
3620         closesocket(s->fd);
3621     if (s->listen_fd >= 0)
3622         closesocket(s->listen_fd);
3623     qemu_free(s);
3624 }
3625
3626 static CharDriverState *qemu_chr_open_tcp(const char *host_str,
3627                                           int is_telnet,
3628                                           int is_unix)
3629 {
3630     CharDriverState *chr = NULL;
3631     TCPCharDriver *s = NULL;
3632     int fd = -1, ret, err, val;
3633     int is_listen = 0;
3634     int is_waitconnect = 1;
3635     int do_nodelay = 0;
3636     const char *ptr;
3637     struct sockaddr_in saddr;
3638 #ifndef _WIN32
3639     struct sockaddr_un uaddr;
3640 #endif
3641     struct sockaddr *addr;
3642     socklen_t addrlen;
3643
3644 #ifndef _WIN32
3645     if (is_unix) {
3646         addr = (struct sockaddr *)&uaddr;
3647         addrlen = sizeof(uaddr);
3648         if (parse_unix_path(&uaddr, host_str) < 0)
3649             goto fail;
3650     } else
3651 #endif
3652     {
3653         addr = (struct sockaddr *)&saddr;
3654         addrlen = sizeof(saddr);
3655         if (parse_host_port(&saddr, host_str) < 0)
3656             goto fail;
3657     }
3658
3659     ptr = host_str;
3660     while((ptr = strchr(ptr,','))) {
3661         ptr++;
3662         if (!strncmp(ptr,"server",6)) {
3663             is_listen = 1;
3664         } else if (!strncmp(ptr,"nowait",6)) {
3665             is_waitconnect = 0;
3666         } else if (!strncmp(ptr,"nodelay",6)) {
3667             do_nodelay = 1;
3668         } else {
3669             printf("Unknown option: %s\n", ptr);
3670             goto fail;
3671         }
3672     }
3673     if (!is_listen)
3674         is_waitconnect = 0;
3675
3676     chr = qemu_mallocz(sizeof(CharDriverState));
3677     if (!chr)
3678         goto fail;
3679     s = qemu_mallocz(sizeof(TCPCharDriver));
3680     if (!s)
3681         goto fail;
3682
3683 #ifndef _WIN32
3684     if (is_unix)
3685         fd = socket(PF_UNIX, SOCK_STREAM, 0);
3686     else
3687 #endif
3688         fd = socket(PF_INET, SOCK_STREAM, 0);
3689
3690     if (fd < 0)
3691         goto fail;
3692
3693     if (!is_waitconnect)
3694         socket_set_nonblock(fd);
3695
3696     s->connected = 0;
3697     s->fd = -1;
3698     s->listen_fd = -1;
3699     s->is_unix = is_unix;
3700     s->do_nodelay = do_nodelay && !is_unix;
3701
3702     chr->opaque = s;
3703     chr->chr_write = tcp_chr_write;
3704     chr->chr_close = tcp_chr_close;
3705
3706     if (is_listen) {
3707         /* allow fast reuse */
3708 #ifndef _WIN32
3709         if (is_unix) {
3710             char path[109];
3711             pstrcpy(path, sizeof(path), uaddr.sun_path);
3712             unlink(path);
3713         } else
3714 #endif
3715         {
3716             val = 1;
3717             setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
3718         }
3719
3720         ret = bind(fd, addr, addrlen);
3721         if (ret < 0)
3722             goto fail;
3723
3724         ret = listen(fd, 0);
3725         if (ret < 0)
3726             goto fail;
3727
3728         s->listen_fd = fd;
3729         qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
3730         if (is_telnet)
3731             s->do_telnetopt = 1;
3732     } else {
3733         for(;;) {
3734             ret = connect(fd, addr, addrlen);
3735             if (ret < 0) {
3736                 err = socket_error();
3737                 if (err == EINTR || err == EWOULDBLOCK) {
3738                 } else if (err == EINPROGRESS) {
3739                     break;
3740 #ifdef _WIN32
3741                 } else if (err == WSAEALREADY) {
3742                     break;
3743 #endif
3744                 } else {
3745                     goto fail;
3746                 }
3747             } else {
3748                 s->connected = 1;
3749                 break;
3750             }
3751         }
3752         s->fd = fd;
3753         socket_set_nodelay(fd);
3754         if (s->connected)
3755             tcp_chr_connect(chr);
3756         else
3757             qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
3758     }
3759
3760     if (is_listen && is_waitconnect) {
3761         printf("QEMU waiting for connection on: %s\n", host_str);
3762         tcp_chr_accept(chr);
3763         socket_set_nonblock(s->listen_fd);
3764     }
3765
3766     return chr;
3767  fail:
3768     if (fd >= 0)
3769         closesocket(fd);
3770     qemu_free(s);
3771     qemu_free(chr);
3772     return NULL;
3773 }
3774
3775 CharDriverState *qemu_chr_open(const char *filename)
3776 {
3777     const char *p;
3778
3779     if (!strcmp(filename, "vc")) {
3780         return text_console_init(&display_state, 0);
3781     } else if (strstart(filename, "vc:", &p)) {
3782         return text_console_init(&display_state, p);
3783     } else if (!strcmp(filename, "null")) {
3784         return qemu_chr_open_null();
3785     } else
3786     if (strstart(filename, "tcp:", &p)) {
3787         return qemu_chr_open_tcp(p, 0, 0);
3788     } else
3789     if (strstart(filename, "telnet:", &p)) {
3790         return qemu_chr_open_tcp(p, 1, 0);
3791     } else
3792     if (strstart(filename, "udp:", &p)) {
3793         return qemu_chr_open_udp(p);
3794     } else
3795     if (strstart(filename, "mon:", &p)) {
3796         CharDriverState *drv = qemu_chr_open(p);
3797         if (drv) {
3798             drv = qemu_chr_open_mux(drv);
3799             monitor_init(drv, !nographic);
3800             return drv;
3801         }
3802         printf("Unable to open driver: %s\n", p);
3803         return 0;
3804     } else
3805 #ifndef _WIN32
3806     if (strstart(filename, "unix:", &p)) {
3807         return qemu_chr_open_tcp(p, 0, 1);
3808     } else if (strstart(filename, "file:", &p)) {
3809         return qemu_chr_open_file_out(p);
3810     } else if (strstart(filename, "pipe:", &p)) {
3811         return qemu_chr_open_pipe(p);
3812     } else if (!strcmp(filename, "pty")) {
3813         return qemu_chr_open_pty();
3814     } else if (!strcmp(filename, "stdio")) {
3815         return qemu_chr_open_stdio();
3816     } else
3817 #if defined(__linux__)
3818     if (strstart(filename, "/dev/parport", NULL)) {
3819         return qemu_chr_open_pp(filename);
3820     } else
3821 #endif
3822 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
3823     || defined(__NetBSD__) || defined(__OpenBSD__)
3824     if (strstart(filename, "/dev/", NULL)) {
3825         return qemu_chr_open_tty(filename);
3826     } else
3827 #endif
3828 #else /* !_WIN32 */
3829     if (strstart(filename, "COM", NULL)) {
3830         return qemu_chr_open_win(filename);
3831     } else
3832     if (strstart(filename, "pipe:", &p)) {
3833         return qemu_chr_open_win_pipe(p);
3834     } else
3835     if (strstart(filename, "con:", NULL)) {
3836         return qemu_chr_open_win_con(filename);
3837     } else
3838     if (strstart(filename, "file:", &p)) {
3839         return qemu_chr_open_win_file_out(p);
3840     } else
3841 #endif
3842 #ifdef CONFIG_BRLAPI
3843     if (!strcmp(filename, "braille")) {
3844         return chr_baum_init();
3845     } else
3846 #endif
3847     {
3848         return NULL;
3849     }
3850 }
3851
3852 void qemu_chr_close(CharDriverState *chr)
3853 {
3854     if (chr->chr_close)
3855         chr->chr_close(chr);
3856     qemu_free(chr);
3857 }
3858
3859 /***********************************************************/
3860 /* network device redirectors */
3861
3862 __attribute__ (( unused ))
3863 static void hex_dump(FILE *f, const uint8_t *buf, int size)
3864 {
3865     int len, i, j, c;
3866
3867     for(i=0;i<size;i+=16) {
3868         len = size - i;
3869         if (len > 16)
3870             len = 16;
3871         fprintf(f, "%08x ", i);
3872         for(j=0;j<16;j++) {
3873             if (j < len)
3874                 fprintf(f, " %02x", buf[i+j]);
3875             else
3876                 fprintf(f, "   ");
3877         }
3878         fprintf(f, " ");
3879         for(j=0;j<len;j++) {
3880             c = buf[i+j];
3881             if (c < ' ' || c > '~')
3882                 c = '.';
3883             fprintf(f, "%c", c);
3884         }
3885         fprintf(f, "\n");
3886     }
3887 }
3888
3889 static int parse_macaddr(uint8_t *macaddr, const char *p)
3890 {
3891     int i;
3892     char *last_char;
3893     long int offset;
3894
3895     errno = 0;
3896     offset = strtol(p, &last_char, 0);    
3897     if (0 == errno && '\0' == *last_char &&
3898             offset >= 0 && offset <= 0xFFFFFF) {
3899         macaddr[3] = (offset & 0xFF0000) >> 16;
3900         macaddr[4] = (offset & 0xFF00) >> 8;
3901         macaddr[5] = offset & 0xFF;
3902         return 0;
3903     } else {
3904         for(i = 0; i < 6; i++) {
3905             macaddr[i] = strtol(p, (char **)&p, 16);
3906             if (i == 5) {
3907                 if (*p != '\0')
3908                     return -1;
3909             } else {
3910                 if (*p != ':' && *p != '-')
3911                     return -1;
3912                 p++;
3913             }
3914         }
3915         return 0;    
3916     }
3917
3918     return -1;
3919 }
3920
3921 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
3922 {
3923     const char *p, *p1;
3924     int len;
3925     p = *pp;
3926     p1 = strchr(p, sep);
3927     if (!p1)
3928         return -1;
3929     len = p1 - p;
3930     p1++;
3931     if (buf_size > 0) {
3932         if (len > buf_size - 1)
3933             len = buf_size - 1;
3934         memcpy(buf, p, len);
3935         buf[len] = '\0';
3936     }
3937     *pp = p1;
3938     return 0;
3939 }
3940
3941 int parse_host_src_port(struct sockaddr_in *haddr,
3942                         struct sockaddr_in *saddr,
3943                         const char *input_str)
3944 {
3945     char *str = strdup(input_str);
3946     char *host_str = str;
3947     char *src_str;
3948     const char *src_str2;
3949     char *ptr;
3950
3951     /*
3952      * Chop off any extra arguments at the end of the string which
3953      * would start with a comma, then fill in the src port information
3954      * if it was provided else use the "any address" and "any port".
3955      */
3956     if ((ptr = strchr(str,',')))
3957         *ptr = '\0';
3958
3959     if ((src_str = strchr(input_str,'@'))) {
3960         *src_str = '\0';
3961         src_str++;
3962     }
3963
3964     if (parse_host_port(haddr, host_str) < 0)
3965         goto fail;
3966
3967     src_str2 = src_str;
3968     if (!src_str || *src_str == '\0')
3969         src_str2 = ":0";
3970
3971     if (parse_host_port(saddr, src_str2) < 0)
3972         goto fail;
3973
3974     free(str);
3975     return(0);
3976
3977 fail:
3978     free(str);
3979     return -1;
3980 }
3981
3982 int parse_host_port(struct sockaddr_in *saddr, const char *str)
3983 {
3984     char buf[512];
3985     struct hostent *he;
3986     const char *p, *r;
3987     int port;
3988
3989     p = str;
3990     if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3991         return -1;
3992     saddr->sin_family = AF_INET;
3993     if (buf[0] == '\0') {
3994         saddr->sin_addr.s_addr = 0;
3995     } else {
3996         if (isdigit(buf[0])) {
3997             if (!inet_aton(buf, &saddr->sin_addr))
3998                 return -1;
3999         } else {
4000             if ((he = gethostbyname(buf)) == NULL)
4001                 return - 1;
4002             saddr->sin_addr = *(struct in_addr *)he->h_addr;
4003         }
4004     }
4005     port = strtol(p, (char **)&r, 0);
4006     if (r == p)
4007         return -1;
4008     saddr->sin_port = htons(port);
4009     return 0;
4010 }
4011
4012 #ifndef _WIN32
4013 static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
4014 {
4015     const char *p;
4016     int len;
4017
4018     len = MIN(108, strlen(str));
4019     p = strchr(str, ',');
4020     if (p)
4021         len = MIN(len, p - str);
4022
4023     memset(uaddr, 0, sizeof(*uaddr));
4024
4025     uaddr->sun_family = AF_UNIX;
4026     memcpy(uaddr->sun_path, str, len);
4027
4028     return 0;
4029 }
4030 #endif
4031
4032 /* find or alloc a new VLAN */
4033 VLANState *qemu_find_vlan(int id)
4034 {
4035     VLANState **pvlan, *vlan;
4036     for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
4037         if (vlan->id == id)
4038             return vlan;
4039     }
4040     vlan = qemu_mallocz(sizeof(VLANState));
4041     if (!vlan)
4042         return NULL;
4043     vlan->id = id;
4044     vlan->next = NULL;
4045     pvlan = &first_vlan;
4046     while (*pvlan != NULL)
4047         pvlan = &(*pvlan)->next;
4048     *pvlan = vlan;
4049     return vlan;
4050 }
4051
4052 VLANClientState *qemu_new_vlan_client(VLANState *vlan,
4053                                       IOReadHandler *fd_read,
4054                                       IOCanRWHandler *fd_can_read,
4055                                       void *opaque)
4056 {
4057     VLANClientState *vc, **pvc;
4058     vc = qemu_mallocz(sizeof(VLANClientState));
4059     if (!vc)
4060         return NULL;
4061     vc->fd_read = fd_read;
4062     vc->fd_can_read = fd_can_read;
4063     vc->opaque = opaque;
4064     vc->vlan = vlan;
4065
4066     vc->next = NULL;
4067     pvc = &vlan->first_client;
4068     while (*pvc != NULL)
4069         pvc = &(*pvc)->next;
4070     *pvc = vc;
4071     return vc;
4072 }
4073
4074 void qemu_del_vlan_client(VLANClientState *vc)
4075 {
4076     VLANClientState **pvc = &vc->vlan->first_client;
4077
4078     while (*pvc != NULL)
4079         if (*pvc == vc) {
4080             *pvc = vc->next;
4081             free(vc);
4082             break;
4083         } else
4084             pvc = &(*pvc)->next;
4085 }
4086
4087 int qemu_can_send_packet(VLANClientState *vc1)
4088 {
4089     VLANState *vlan = vc1->vlan;
4090     VLANClientState *vc;
4091
4092     for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
4093         if (vc != vc1) {
4094             if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
4095                 return 1;
4096         }
4097     }
4098     return 0;
4099 }
4100
4101 void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
4102 {
4103     VLANState *vlan = vc1->vlan;
4104     VLANClientState *vc;
4105
4106 #if 0
4107     printf("vlan %d send:\n", vlan->id);
4108     hex_dump(stdout, buf, size);
4109 #endif
4110     for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
4111         if (vc != vc1) {
4112             vc->fd_read(vc->opaque, buf, size);
4113         }
4114     }
4115 }
4116
4117 #if defined(CONFIG_SLIRP)
4118
4119 /* slirp network adapter */
4120
4121 static int slirp_inited;
4122 static VLANClientState *slirp_vc;
4123
4124 int slirp_can_output(void)
4125 {
4126     return !slirp_vc || qemu_can_send_packet(slirp_vc);
4127 }
4128
4129 void slirp_output(const uint8_t *pkt, int pkt_len)
4130 {
4131 #if 0
4132     printf("slirp output:\n");
4133     hex_dump(stdout, pkt, pkt_len);
4134 #endif
4135     if (!slirp_vc)
4136         return;
4137     qemu_send_packet(slirp_vc, pkt, pkt_len);
4138 }
4139
4140 static void slirp_receive(void *opaque, const uint8_t *buf, int size)
4141 {
4142 #if 0
4143     printf("slirp input:\n");
4144     hex_dump(stdout, buf, size);
4145 #endif
4146     slirp_input(buf, size);
4147 }
4148
4149 static int net_slirp_init(VLANState *vlan)
4150 {
4151     if (!slirp_inited) {
4152         slirp_inited = 1;
4153         slirp_init();
4154     }
4155     slirp_vc = qemu_new_vlan_client(vlan,
4156                                     slirp_receive, NULL, NULL);
4157     snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector");
4158     return 0;
4159 }
4160
4161 static void net_slirp_redir(const char *redir_str)
4162 {
4163     int is_udp;
4164     char buf[256], *r;
4165     const char *p;
4166     struct in_addr guest_addr;
4167     int host_port, guest_port;
4168
4169     if (!slirp_inited) {
4170         slirp_inited = 1;
4171         slirp_init();
4172     }
4173
4174     p = redir_str;
4175     if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
4176         goto fail;
4177     if (!strcmp(buf, "tcp")) {
4178         is_udp = 0;
4179     } else if (!strcmp(buf, "udp")) {
4180         is_udp = 1;
4181     } else {
4182         goto fail;
4183     }
4184
4185     if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
4186         goto fail;
4187     host_port = strtol(buf, &r, 0);
4188     if (r == buf)
4189         goto fail;
4190
4191     if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
4192         goto fail;
4193     if (buf[0] == '\0') {
4194         pstrcpy(buf, sizeof(buf), "10.0.2.15");
4195     }
4196     if (!inet_aton(buf, &guest_addr))
4197         goto fail;
4198
4199     guest_port = strtol(p, &r, 0);
4200     if (r == p)
4201         goto fail;
4202
4203     if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
4204         fprintf(stderr, "qemu: could not set up redirection\n");
4205         exit(1);
4206     }
4207     return;
4208  fail:
4209     fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
4210     exit(1);
4211 }
4212
4213 #ifndef _WIN32
4214
4215 char smb_dir[1024];
4216
4217 static void erase_dir(char *dir_name)
4218 {
4219     DIR *d;
4220     struct dirent *de;
4221     char filename[1024];
4222
4223     /* erase all the files in the directory */
4224     if ((d = opendir(dir_name)) != 0) {
4225         for(;;) {
4226             de = readdir(d);
4227             if (!de)
4228                 break;
4229             if (strcmp(de->d_name, ".") != 0 &&
4230                 strcmp(de->d_name, "..") != 0) {
4231                 snprintf(filename, sizeof(filename), "%s/%s",
4232                          smb_dir, de->d_name);
4233                 if (unlink(filename) != 0)  /* is it a directory? */
4234                     erase_dir(filename);
4235             }
4236         }
4237         closedir(d);
4238         rmdir(dir_name);
4239     }
4240 }
4241
4242 /* automatic user mode samba server configuration */
4243 static void smb_exit(void)
4244 {
4245     erase_dir(smb_dir);
4246 }
4247
4248 /* automatic user mode samba server configuration */
4249 static void net_slirp_smb(const char *exported_dir)
4250 {
4251     char smb_conf[1024];
4252     char smb_cmdline[1024];
4253     FILE *f;
4254
4255     if (!slirp_inited) {
4256         slirp_inited = 1;
4257         slirp_init();
4258     }
4259
4260     /* XXX: better tmp dir construction */
4261     snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
4262     if (mkdir(smb_dir, 0700) < 0) {
4263         fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
4264         exit(1);
4265     }
4266     snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
4267
4268     f = fopen(smb_conf, "w");
4269     if (!f) {
4270         fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
4271         exit(1);
4272     }
4273     fprintf(f,
4274             "[global]\n"
4275             "private dir=%s\n"
4276             "smb ports=0\n"
4277             "socket address=127.0.0.1\n"
4278             "pid directory=%s\n"
4279             "lock directory=%s\n"
4280             "log file=%s/log.smbd\n"
4281             "smb passwd file=%s/smbpasswd\n"
4282             "security = share\n"
4283             "[qemu]\n"
4284             "path=%s\n"
4285             "read only=no\n"
4286             "guest ok=yes\n",
4287             smb_dir,
4288             smb_dir,
4289             smb_dir,
4290             smb_dir,
4291             smb_dir,
4292             exported_dir
4293             );
4294     fclose(f);
4295     atexit(smb_exit);
4296
4297     snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
4298              SMBD_COMMAND, smb_conf);
4299
4300     slirp_add_exec(0, smb_cmdline, 4, 139);
4301 }
4302
4303 #endif /* !defined(_WIN32) */
4304 void do_info_slirp(void)
4305 {
4306     slirp_stats();
4307 }
4308
4309 #endif /* CONFIG_SLIRP */
4310
4311 #if !defined(_WIN32)
4312
4313 typedef struct TAPState {
4314     VLANClientState *vc;
4315     int fd;
4316     char down_script[1024];
4317 } TAPState;
4318
4319 static void tap_receive(void *opaque, const uint8_t *buf, int size)
4320 {
4321     TAPState *s = opaque;
4322     int ret;
4323     for(;;) {
4324         ret = write(s->fd, buf, size);
4325         if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
4326         } else {
4327             break;
4328         }
4329     }
4330 }
4331
4332 static void tap_send(void *opaque)
4333 {
4334     TAPState *s = opaque;
4335     uint8_t buf[4096];
4336     int size;
4337
4338 #ifdef __sun__
4339     struct strbuf sbuf;
4340     int f = 0;
4341     sbuf.maxlen = sizeof(buf);
4342     sbuf.buf = buf;
4343     size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
4344 #else
4345     size = read(s->fd, buf, sizeof(buf));
4346 #endif
4347     if (size > 0) {
4348         qemu_send_packet(s->vc, buf, size);
4349     }
4350 }
4351
4352 /* fd support */
4353
4354 static TAPState *net_tap_fd_init(VLANState *vlan, int fd)
4355 {
4356     TAPState *s;
4357
4358     s = qemu_mallocz(sizeof(TAPState));
4359     if (!s)
4360         return NULL;
4361     s->fd = fd;
4362     s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
4363     qemu_set_fd_handler(s->fd, tap_send, NULL, s);
4364     snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd);
4365     return s;
4366 }
4367
4368 #if defined (_BSD) || defined (__FreeBSD_kernel__)
4369 static int tap_open(char *ifname, int ifname_size)
4370 {
4371     int fd;
4372     char *dev;
4373     struct stat s;
4374
4375     TFR(fd = open("/dev/tap", O_RDWR));
4376     if (fd < 0) {
4377         fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
4378         return -1;
4379     }
4380
4381     fstat(fd, &s);
4382     dev = devname(s.st_rdev, S_IFCHR);
4383     pstrcpy(ifname, ifname_size, dev);
4384
4385     fcntl(fd, F_SETFL, O_NONBLOCK);
4386     return fd;
4387 }
4388 #elif defined(__sun__)
4389 #define TUNNEWPPA       (('T'<<16) | 0x0001)
4390 /*
4391  * Allocate TAP device, returns opened fd.
4392  * Stores dev name in the first arg(must be large enough).
4393  */
4394 int tap_alloc(char *dev, size_t dev_size)
4395 {
4396     int tap_fd, if_fd, ppa = -1;
4397     static int ip_fd = 0;
4398     char *ptr;
4399
4400     static int arp_fd = 0;
4401     int ip_muxid, arp_muxid;
4402     struct strioctl  strioc_if, strioc_ppa;
4403     int link_type = I_PLINK;;
4404     struct lifreq ifr;
4405     char actual_name[32] = "";
4406
4407     memset(&ifr, 0x0, sizeof(ifr));
4408
4409     if( *dev ){
4410        ptr = dev;
4411        while( *ptr && !isdigit((int)*ptr) ) ptr++;
4412        ppa = atoi(ptr);
4413     }
4414
4415     /* Check if IP device was opened */
4416     if( ip_fd )
4417        close(ip_fd);
4418
4419     TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
4420     if (ip_fd < 0) {
4421        syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
4422        return -1;
4423     }
4424
4425     TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
4426     if (tap_fd < 0) {
4427        syslog(LOG_ERR, "Can't open /dev/tap");
4428        return -1;
4429     }
4430
4431     /* Assign a new PPA and get its unit number. */
4432     strioc_ppa.ic_cmd = TUNNEWPPA;
4433     strioc_ppa.ic_timout = 0;
4434     strioc_ppa.ic_len = sizeof(ppa);
4435     strioc_ppa.ic_dp = (char *)&ppa;
4436     if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
4437        syslog (LOG_ERR, "Can't assign new interface");
4438
4439     TFR(if_fd = open("/dev/tap", O_RDWR, 0));
4440     if (if_fd < 0) {
4441        syslog(LOG_ERR, "Can't open /dev/tap (2)");
4442        return -1;
4443     }
4444     if(ioctl(if_fd, I_PUSH, "ip") < 0){
4445        syslog(LOG_ERR, "Can't push IP module");
4446        return -1;
4447     }
4448
4449     if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
4450         syslog(LOG_ERR, "Can't get flags\n");
4451
4452     snprintf (actual_name, 32, "tap%d", ppa);
4453     strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
4454
4455     ifr.lifr_ppa = ppa;
4456     /* Assign ppa according to the unit number returned by tun device */
4457
4458     if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
4459         syslog (LOG_ERR, "Can't set PPA %d", ppa);
4460     if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
4461         syslog (LOG_ERR, "Can't get flags\n");
4462     /* Push arp module to if_fd */
4463     if (ioctl (if_fd, I_PUSH, "arp") < 0)
4464         syslog (LOG_ERR, "Can't push ARP module (2)");
4465
4466     /* Push arp module to ip_fd */
4467     if (ioctl (ip_fd, I_POP, NULL) < 0)
4468         syslog (LOG_ERR, "I_POP failed\n");
4469     if (ioctl (ip_fd, I_PUSH, "arp") < 0)
4470         syslog (LOG_ERR, "Can't push ARP module (3)\n");
4471     /* Open arp_fd */
4472     TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
4473     if (arp_fd < 0)
4474        syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
4475
4476     /* Set ifname to arp */
4477     strioc_if.ic_cmd = SIOCSLIFNAME;
4478     strioc_if.ic_timout = 0;
4479     strioc_if.ic_len = sizeof(ifr);
4480     strioc_if.ic_dp = (char *)&ifr;
4481     if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
4482         syslog (LOG_ERR, "Can't set ifname to arp\n");
4483     }
4484
4485     if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
4486        syslog(LOG_ERR, "Can't link TAP device to IP");
4487        return -1;
4488     }
4489
4490     if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
4491         syslog (LOG_ERR, "Can't link TAP device to ARP");
4492
4493     close (if_fd);
4494
4495     memset(&ifr, 0x0, sizeof(ifr));
4496     strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
4497     ifr.lifr_ip_muxid  = ip_muxid;
4498     ifr.lifr_arp_muxid = arp_muxid;
4499
4500     if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
4501     {
4502       ioctl (ip_fd, I_PUNLINK , arp_muxid);
4503       ioctl (ip_fd, I_PUNLINK, ip_muxid);
4504       syslog (LOG_ERR, "Can't set multiplexor id");
4505     }
4506
4507     snprintf(dev, dev_size, "tap%d", ppa);
4508     return tap_fd;
4509 }
4510
4511 static int tap_open(char *ifname, int ifname_size)
4512 {
4513     char  dev[10]="";
4514     int fd;
4515     if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
4516        fprintf(stderr, "Cannot allocate TAP device\n");
4517        return -1;
4518     }
4519     pstrcpy(ifname, ifname_size, dev);
4520     fcntl(fd, F_SETFL, O_NONBLOCK);
4521     return fd;
4522 }
4523 #else
4524 static int tap_open(char *ifname, int ifname_size)
4525 {
4526     struct ifreq ifr;
4527     int fd, ret;
4528
4529     TFR(fd = open("/dev/net/tun", O_RDWR));
4530     if (fd < 0) {
4531         fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
4532         return -1;
4533     }
4534     memset(&ifr, 0, sizeof(ifr));
4535     ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
4536     if (ifname[0] != '\0')
4537         pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
4538     else
4539         pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
4540     ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
4541     if (ret != 0) {
4542         fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
4543         close(fd);
4544         return -1;
4545     }
4546     pstrcpy(ifname, ifname_size, ifr.ifr_name);
4547     fcntl(fd, F_SETFL, O_NONBLOCK);
4548     return fd;
4549 }
4550 #endif
4551
4552 static int launch_script(const char *setup_script, const char *ifname, int fd)
4553 {
4554     int pid, status;
4555     char *args[3];
4556     char **parg;
4557
4558         /* try to launch network script */
4559         pid = fork();
4560         if (pid >= 0) {
4561             if (pid == 0) {
4562                 int open_max = sysconf (_SC_OPEN_MAX), i;
4563                 for (i = 0; i < open_max; i++)
4564                     if (i != STDIN_FILENO &&
4565                         i != STDOUT_FILENO &&
4566                         i != STDERR_FILENO &&
4567                         i != fd)
4568                         close(i);
4569
4570                 parg = args;
4571                 *parg++ = (char *)setup_script;
4572                 *parg++ = (char *)ifname;
4573                 *parg++ = NULL;
4574                 execv(setup_script, args);
4575                 _exit(1);
4576             }
4577             while (waitpid(pid, &status, 0) != pid);
4578             if (!WIFEXITED(status) ||
4579                 WEXITSTATUS(status) != 0) {
4580                 fprintf(stderr, "%s: could not launch network script\n",
4581                         setup_script);
4582                 return -1;
4583             }
4584         }
4585     return 0;
4586 }
4587
4588 static int net_tap_init(VLANState *vlan, const char *ifname1,
4589                         const char *setup_script, const char *down_script)
4590 {
4591     TAPState *s;
4592     int fd;
4593     char ifname[128];
4594
4595     if (ifname1 != NULL)
4596         pstrcpy(ifname, sizeof(ifname), ifname1);
4597     else
4598         ifname[0] = '\0';
4599     TFR(fd = tap_open(ifname, sizeof(ifname)));
4600     if (fd < 0)
4601         return -1;
4602
4603     if (!setup_script || !strcmp(setup_script, "no"))
4604         setup_script = "";
4605     if (setup_script[0] != '\0') {
4606         if (launch_script(setup_script, ifname, fd))
4607             return -1;
4608     }
4609     s = net_tap_fd_init(vlan, fd);
4610     if (!s)
4611         return -1;
4612     snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4613              "tap: ifname=%s setup_script=%s", ifname, setup_script);
4614     if (down_script && strcmp(down_script, "no"))
4615         snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
4616     return 0;
4617 }
4618
4619 #endif /* !_WIN32 */
4620
4621 #if defined(CONFIG_VDE)
4622 typedef struct VDEState {
4623     VLANClientState *vc;
4624     VDECONN *vde;
4625 } VDEState;
4626
4627 static void vde_to_qemu(void *opaque)
4628 {
4629     VDEState *s = opaque;
4630     uint8_t buf[4096];
4631     int size;
4632
4633     size = vde_recv(s->vde, buf, sizeof(buf), 0);
4634     if (size > 0) {
4635         qemu_send_packet(s->vc, buf, size);
4636     }
4637 }
4638
4639 static void vde_from_qemu(void *opaque, const uint8_t *buf, int size)
4640 {
4641     VDEState *s = opaque;
4642     int ret;
4643     for(;;) {
4644         ret = vde_send(s->vde, buf, size, 0);
4645         if (ret < 0 && errno == EINTR) {
4646         } else {
4647             break;
4648         }
4649     }
4650 }
4651
4652 static int net_vde_init(VLANState *vlan, const char *sock, int port,
4653                         const char *group, int mode)
4654 {
4655     VDEState *s;
4656     char *init_group = strlen(group) ? (char *)group : NULL;
4657     char *init_sock = strlen(sock) ? (char *)sock : NULL;
4658
4659     struct vde_open_args args = {
4660         .port = port,
4661         .group = init_group,
4662         .mode = mode,
4663     };
4664
4665     s = qemu_mallocz(sizeof(VDEState));
4666     if (!s)
4667         return -1;
4668     s->vde = vde_open(init_sock, "QEMU", &args);
4669     if (!s->vde){
4670         free(s);
4671         return -1;
4672     }
4673     s->vc = qemu_new_vlan_client(vlan, vde_from_qemu, NULL, s);
4674     qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
4675     snprintf(s->vc->info_str, sizeof(s->vc->info_str), "vde: sock=%s fd=%d",
4676              sock, vde_datafd(s->vde));
4677     return 0;
4678 }
4679 #endif
4680
4681 /* network connection */
4682 typedef struct NetSocketState {
4683     VLANClientState *vc;
4684     int fd;
4685     int state; /* 0 = getting length, 1 = getting data */
4686     int index;
4687     int packet_len;
4688     uint8_t buf[4096];
4689     struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
4690 } NetSocketState;
4691
4692 typedef struct NetSocketListenState {
4693     VLANState *vlan;
4694     int fd;
4695 } NetSocketListenState;
4696
4697 /* XXX: we consider we can send the whole packet without blocking */
4698 static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
4699 {
4700     NetSocketState *s = opaque;
4701     uint32_t len;
4702     len = htonl(size);
4703
4704     send_all(s->fd, (const uint8_t *)&len, sizeof(len));
4705     send_all(s->fd, buf, size);
4706 }
4707
4708 static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
4709 {
4710     NetSocketState *s = opaque;
4711     sendto(s->fd, buf, size, 0,
4712            (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
4713 }
4714
4715 static void net_socket_send(void *opaque)
4716 {
4717     NetSocketState *s = opaque;
4718     int l, size, err;
4719     uint8_t buf1[4096];
4720     const uint8_t *buf;
4721
4722     size = recv(s->fd, buf1, sizeof(buf1), 0);
4723     if (size < 0) {
4724         err = socket_error();
4725         if (err != EWOULDBLOCK)
4726             goto eoc;
4727     } else if (size == 0) {
4728         /* end of connection */
4729     eoc:
4730         qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
4731         closesocket(s->fd);
4732         return;
4733     }
4734     buf = buf1;
4735     while (size > 0) {
4736         /* reassemble a packet from the network */
4737         switch(s->state) {
4738         case 0:
4739             l = 4 - s->index;
4740             if (l > size)
4741                 l = size;
4742             memcpy(s->buf + s->index, buf, l);
4743             buf += l;
4744             size -= l;
4745             s->index += l;
4746             if (s->index == 4) {
4747                 /* got length */
4748                 s->packet_len = ntohl(*(uint32_t *)s->buf);
4749                 s->index = 0;
4750                 s->state = 1;
4751             }
4752             break;
4753         case 1:
4754             l = s->packet_len - s->index;
4755             if (l > size)
4756                 l = size;
4757             memcpy(s->buf + s->index, buf, l);
4758             s->index += l;
4759             buf += l;
4760             size -= l;
4761             if (s->index >= s->packet_len) {
4762                 qemu_send_packet(s->vc, s->buf, s->packet_len);
4763                 s->index = 0;
4764                 s->state = 0;
4765             }
4766             break;
4767         }
4768     }
4769 }
4770
4771 static void net_socket_send_dgram(void *opaque)
4772 {
4773     NetSocketState *s = opaque;
4774     int size;
4775
4776     size = recv(s->fd, s->buf, sizeof(s->buf), 0);
4777     if (size < 0)
4778         return;
4779     if (size == 0) {
4780         /* end of connection */
4781         qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
4782         return;
4783     }
4784     qemu_send_packet(s->vc, s->buf, size);
4785 }
4786
4787 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
4788 {
4789     struct ip_mreq imr;
4790     int fd;
4791     int val, ret;
4792     if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
4793         fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
4794                 inet_ntoa(mcastaddr->sin_addr),
4795                 (int)ntohl(mcastaddr->sin_addr.s_addr));
4796         return -1;
4797
4798     }
4799     fd = socket(PF_INET, SOCK_DGRAM, 0);
4800     if (fd < 0) {
4801         perror("socket(PF_INET, SOCK_DGRAM)");
4802         return -1;
4803     }
4804
4805     val = 1;
4806     ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
4807                    (const char *)&val, sizeof(val));
4808     if (ret < 0) {
4809         perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
4810         goto fail;
4811     }
4812
4813     ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
4814     if (ret < 0) {
4815         perror("bind");
4816         goto fail;
4817     }
4818
4819     /* Add host to multicast group */
4820     imr.imr_multiaddr = mcastaddr->sin_addr;
4821     imr.imr_interface.s_addr = htonl(INADDR_ANY);
4822
4823     ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
4824                      (const char *)&imr, sizeof(struct ip_mreq));
4825     if (ret < 0) {
4826         perror("setsockopt(IP_ADD_MEMBERSHIP)");
4827         goto fail;
4828     }
4829
4830     /* Force mcast msgs to loopback (eg. several QEMUs in same host */
4831     val = 1;
4832     ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
4833                    (const char *)&val, sizeof(val));
4834     if (ret < 0) {
4835         perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
4836         goto fail;
4837     }
4838
4839     socket_set_nonblock(fd);
4840     return fd;
4841 fail:
4842     if (fd >= 0)
4843         closesocket(fd);
4844     return -1;
4845 }
4846
4847 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd,
4848                                           int is_connected)
4849 {
4850     struct sockaddr_in saddr;
4851     int newfd;
4852     socklen_t saddr_len;
4853     NetSocketState *s;
4854
4855     /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
4856      * Because this may be "shared" socket from a "master" process, datagrams would be recv()
4857      * by ONLY ONE process: we must "clone" this dgram socket --jjo
4858      */
4859
4860     if (is_connected) {
4861         if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
4862             /* must be bound */
4863             if (saddr.sin_addr.s_addr==0) {
4864                 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
4865                         fd);
4866                 return NULL;
4867             }
4868             /* clone dgram socket */
4869             newfd = net_socket_mcast_create(&saddr);
4870             if (newfd < 0) {
4871                 /* error already reported by net_socket_mcast_create() */
4872                 close(fd);
4873                 return NULL;
4874             }
4875             /* clone newfd to fd, close newfd */
4876             dup2(newfd, fd);
4877             close(newfd);
4878
4879         } else {
4880             fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
4881                     fd, strerror(errno));
4882             return NULL;
4883         }
4884     }
4885
4886     s = qemu_mallocz(sizeof(NetSocketState));
4887     if (!s)
4888         return NULL;
4889     s->fd = fd;
4890
4891     s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
4892     qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
4893
4894     /* mcast: save bound address as dst */
4895     if (is_connected) s->dgram_dst=saddr;
4896
4897     snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4898             "socket: fd=%d (%s mcast=%s:%d)",
4899             fd, is_connected? "cloned" : "",
4900             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4901     return s;
4902 }
4903
4904 static void net_socket_connect(void *opaque)
4905 {
4906     NetSocketState *s = opaque;
4907     qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
4908 }
4909
4910 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd,
4911                                           int is_connected)
4912 {
4913     NetSocketState *s;
4914     s = qemu_mallocz(sizeof(NetSocketState));
4915     if (!s)
4916         return NULL;
4917     s->fd = fd;
4918     s->vc = qemu_new_vlan_client(vlan,
4919                                  net_socket_receive, NULL, s);
4920     snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4921              "socket: fd=%d", fd);
4922     if (is_connected) {
4923         net_socket_connect(s);
4924     } else {
4925         qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
4926     }
4927     return s;
4928 }
4929
4930 static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd,
4931                                           int is_connected)
4932 {
4933     int so_type=-1, optlen=sizeof(so_type);
4934
4935     if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
4936         (socklen_t *)&optlen)< 0) {
4937         fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
4938         return NULL;
4939     }
4940     switch(so_type) {
4941     case SOCK_DGRAM:
4942         return net_socket_fd_init_dgram(vlan, fd, is_connected);
4943     case SOCK_STREAM:
4944         return net_socket_fd_init_stream(vlan, fd, is_connected);
4945     default:
4946         /* who knows ... this could be a eg. a pty, do warn and continue as stream */
4947         fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
4948         return net_socket_fd_init_stream(vlan, fd, is_connected);
4949     }
4950     return NULL;
4951 }
4952
4953 static void net_socket_accept(void *opaque)
4954 {
4955     NetSocketListenState *s = opaque;
4956     NetSocketState *s1;
4957     struct sockaddr_in saddr;
4958     socklen_t len;
4959     int fd;
4960
4961     for(;;) {
4962         len = sizeof(saddr);
4963         fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
4964         if (fd < 0 && errno != EINTR) {
4965             return;
4966         } else if (fd >= 0) {
4967             break;
4968         }
4969     }
4970     s1 = net_socket_fd_init(s->vlan, fd, 1);
4971     if (!s1) {
4972         closesocket(fd);
4973     } else {
4974         snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
4975                  "socket: connection from %s:%d",
4976                  inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4977     }
4978 }
4979
4980 static int net_socket_listen_init(VLANState *vlan, const char *host_str)
4981 {
4982     NetSocketListenState *s;
4983     int fd, val, ret;
4984     struct sockaddr_in saddr;
4985
4986     if (parse_host_port(&saddr, host_str) < 0)
4987         return -1;
4988
4989     s = qemu_mallocz(sizeof(NetSocketListenState));
4990     if (!s)
4991         return -1;
4992
4993     fd = socket(PF_INET, SOCK_STREAM, 0);
4994     if (fd < 0) {
4995         perror("socket");
4996         return -1;
4997     }
4998     socket_set_nonblock(fd);
4999
5000     /* allow fast reuse */
5001     val = 1;
5002     setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
5003
5004     ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
5005     if (ret < 0) {
5006         perror("bind");
5007         return -1;
5008     }
5009     ret = listen(fd, 0);
5010     if (ret < 0) {
5011         perror("listen");
5012         return -1;
5013     }
5014     s->vlan = vlan;
5015     s->fd = fd;
5016     qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
5017     return 0;
5018 }
5019
5020 static int net_socket_connect_init(VLANState *vlan, const char *host_str)
5021 {
5022     NetSocketState *s;
5023     int fd, connected, ret, err;
5024     struct sockaddr_in saddr;
5025
5026     if (parse_host_port(&saddr, host_str) < 0)
5027         return -1;
5028
5029     fd = socket(PF_INET, SOCK_STREAM, 0);
5030     if (fd < 0) {
5031         perror("socket");
5032         return -1;
5033     }
5034     socket_set_nonblock(fd);
5035
5036     connected = 0;
5037     for(;;) {
5038         ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
5039         if (ret < 0) {
5040             err = socket_error();
5041             if (err == EINTR || err == EWOULDBLOCK) {
5042             } else if (err == EINPROGRESS) {
5043                 break;
5044 #ifdef _WIN32
5045             } else if (err == WSAEALREADY) {
5046                 break;
5047 #endif
5048             } else {
5049                 perror("connect");
5050                 closesocket(fd);
5051                 return -1;
5052             }
5053         } else {
5054             connected = 1;
5055             break;
5056         }
5057     }
5058     s = net_socket_fd_init(vlan, fd, connected);
5059     if (!s)
5060         return -1;
5061     snprintf(s->vc->info_str, sizeof(s->vc->info_str),
5062              "socket: connect to %s:%d",
5063              inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
5064     return 0;
5065 }
5066
5067 static int net_socket_mcast_init(VLANState *vlan, const char *host_str)
5068 {
5069     NetSocketState *s;
5070     int fd;
5071     struct sockaddr_in saddr;
5072
5073     if (parse_host_port(&saddr, host_str) < 0)
5074         return -1;
5075
5076
5077     fd = net_socket_mcast_create(&saddr);
5078     if (fd < 0)
5079         return -1;
5080
5081     s = net_socket_fd_init(vlan, fd, 0);
5082     if (!s)
5083         return -1;
5084
5085     s->dgram_dst = saddr;
5086
5087     snprintf(s->vc->info_str, sizeof(s->vc->info_str),
5088              "socket: mcast=%s:%d",
5089              inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
5090     return 0;
5091
5092 }
5093
5094 static const char *get_opt_name(char *buf, int buf_size, const char *p)
5095 {
5096     char *q;
5097
5098     q = buf;
5099     while (*p != '\0' && *p != '=') {
5100         if (q && (q - buf) < buf_size - 1)
5101             *q++ = *p;
5102         p++;
5103     }
5104     if (q)
5105         *q = '\0';
5106
5107     return p;
5108 }
5109
5110 static const char *get_opt_value(char *buf, int buf_size, const char *p)
5111 {
5112     char *q;
5113
5114     q = buf;
5115     while (*p != '\0') {
5116         if (*p == ',') {
5117             if (*(p + 1) != ',')
5118                 break;
5119             p++;
5120         }
5121         if (q && (q - buf) < buf_size - 1)
5122             *q++ = *p;
5123         p++;
5124     }
5125     if (q)
5126         *q = '\0';
5127
5128     return p;
5129 }
5130
5131 static int get_param_value(char *buf, int buf_size,
5132                            const char *tag, const char *str)
5133 {
5134     const char *p;
5135     char option[128];
5136
5137     p = str;
5138     for(;;) {
5139         p = get_opt_name(option, sizeof(option), p);
5140         if (*p != '=')
5141             break;
5142         p++;
5143         if (!strcmp(tag, option)) {
5144             (void)get_opt_value(buf, buf_size, p);
5145             return strlen(buf);
5146         } else {
5147             p = get_opt_value(NULL, 0, p);
5148         }
5149         if (*p != ',')
5150             break;
5151         p++;
5152     }
5153     return 0;
5154 }
5155
5156 static int check_params(char *buf, int buf_size,
5157                         const char * const *params, const char *str)
5158 {
5159     const char *p;
5160     int i;
5161
5162     p = str;
5163     for(;;) {
5164         p = get_opt_name(buf, buf_size, p);
5165         if (*p != '=')
5166             return -1;
5167         p++;
5168         for(i = 0; params[i] != NULL; i++)
5169             if (!strcmp(params[i], buf))
5170                 break;
5171         if (params[i] == NULL)
5172             return -1;
5173         p = get_opt_value(NULL, 0, p);
5174         if (*p != ',')
5175             break;
5176         p++;
5177     }
5178     return 0;
5179 }
5180
5181 static int net_client_init(const char *device, const char *p)
5182 {
5183     char buf[1024];
5184     int vlan_id, ret;
5185     VLANState *vlan;
5186
5187     vlan_id = 0;
5188     if (get_param_value(buf, sizeof(buf), "vlan", p)) {
5189         vlan_id = strtol(buf, NULL, 0);
5190     }
5191     vlan = qemu_find_vlan(vlan_id);
5192     if (!vlan) {
5193         fprintf(stderr, "Could not create vlan %d\n", vlan_id);
5194         return -1;
5195     }
5196     if (!strcmp(device, "nic")) {
5197         NICInfo *nd;
5198         uint8_t *macaddr;
5199
5200         if (nb_nics >= MAX_NICS) {
5201             fprintf(stderr, "Too Many NICs\n");
5202             return -1;
5203         }
5204         nd = &nd_table[nb_nics];
5205         macaddr = nd->macaddr;
5206         macaddr[0] = 0x52;
5207         macaddr[1] = 0x54;
5208         macaddr[2] = 0x00;
5209         macaddr[3] = 0x12;
5210         macaddr[4] = 0x34;
5211         macaddr[5] = 0x56 + nb_nics;
5212
5213         if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
5214             if (parse_macaddr(macaddr, buf) < 0) {
5215                 fprintf(stderr, "invalid syntax for ethernet address\n");
5216                 return -1;
5217             }
5218         }
5219         if (get_param_value(buf, sizeof(buf), "model", p)) {
5220             nd->model = strdup(buf);
5221         }
5222         nd->vlan = vlan;
5223         nb_nics++;
5224         vlan->nb_guest_devs++;
5225         ret = 0;
5226     } else
5227     if (!strcmp(device, "none")) {
5228         /* does nothing. It is needed to signal that no network cards
5229            are wanted */
5230         ret = 0;
5231     } else
5232 #ifdef CONFIG_SLIRP
5233     if (!strcmp(device, "user")) {
5234         if (get_param_value(buf, sizeof(buf), "hostname", p)) {
5235             pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
5236         }
5237         vlan->nb_host_devs++;
5238         ret = net_slirp_init(vlan);
5239     } else
5240 #endif
5241 #ifdef _WIN32
5242     if (!strcmp(device, "tap")) {
5243         char ifname[64];
5244         if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
5245             fprintf(stderr, "tap: no interface name\n");
5246             return -1;
5247         }
5248         vlan->nb_host_devs++;
5249         ret = tap_win32_init(vlan, ifname);
5250     } else
5251 #else
5252     if (!strcmp(device, "tap")) {
5253         char ifname[64];
5254         char setup_script[1024], down_script[1024];
5255         int fd;
5256         vlan->nb_host_devs++;
5257         if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
5258             fd = strtol(buf, NULL, 0);
5259             fcntl(fd, F_SETFL, O_NONBLOCK);
5260             ret = -1;
5261             if (net_tap_fd_init(vlan, fd))
5262                 ret = 0;
5263         } else {
5264             if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
5265                 ifname[0] = '\0';
5266             }
5267             if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
5268                 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
5269             }
5270             if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
5271                 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
5272             }
5273             ret = net_tap_init(vlan, ifname, setup_script, down_script);
5274         }
5275     } else
5276 #endif
5277     if (!strcmp(device, "socket")) {
5278         if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
5279             int fd;
5280             fd = strtol(buf, NULL, 0);
5281             ret = -1;
5282             if (net_socket_fd_init(vlan, fd, 1))
5283                 ret = 0;
5284         } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
5285             ret = net_socket_listen_init(vlan, buf);
5286         } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
5287             ret = net_socket_connect_init(vlan, buf);
5288         } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
5289             ret = net_socket_mcast_init(vlan, buf);
5290         } else {
5291             fprintf(stderr, "Unknown socket options: %s\n", p);
5292             return -1;
5293         }
5294         vlan->nb_host_devs++;
5295     } else
5296 #ifdef CONFIG_VDE
5297     if (!strcmp(device, "vde")) {
5298         char vde_sock[1024], vde_group[512];
5299         int vde_port, vde_mode;
5300         vlan->nb_host_devs++;
5301         if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
5302             vde_sock[0] = '\0';
5303         }
5304         if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
5305             vde_port = strtol(buf, NULL, 10);
5306         } else {
5307             vde_port = 0;
5308         }
5309         if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
5310             vde_group[0] = '\0';
5311         }
5312         if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
5313             vde_mode = strtol(buf, NULL, 8);
5314         } else {
5315             vde_mode = 0700;
5316         }
5317         ret = net_vde_init(vlan, vde_sock, vde_port, vde_group, vde_mode);
5318     } else
5319 #endif
5320     {
5321         fprintf(stderr, "Unknown network device: %s\n", device);
5322         return -1;
5323     }
5324     if (ret < 0) {
5325         fprintf(stderr, "Could not initialize device '%s'\n", device);
5326     }
5327
5328     return ret;
5329 }
5330
5331 static int net_client_parse(const char *str)
5332 {
5333     const char *p;
5334     char *q;
5335     char device[64];
5336
5337     p = str;
5338     q = device;
5339     while (*p != '\0' && *p != ',') {
5340         if ((q - device) < sizeof(device) - 1)
5341             *q++ = *p;
5342         p++;
5343     }
5344     *q = '\0';
5345     if (*p == ',')
5346         p++;
5347
5348     return net_client_init(device, p);
5349 }
5350
5351 void do_info_network(void)
5352 {
5353     VLANState *vlan;
5354     VLANClientState *vc;
5355
5356     for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
5357         term_printf("VLAN %d devices:\n", vlan->id);
5358         for(vc = vlan->first_client; vc != NULL; vc = vc->next)
5359             term_printf("  %s\n", vc->info_str);
5360     }
5361 }
5362
5363 #define HD_ALIAS "index=%d,media=disk"
5364 #ifdef TARGET_PPC
5365 #define CDROM_ALIAS "index=1,media=cdrom"
5366 #else
5367 #define CDROM_ALIAS "index=2,media=cdrom"
5368 #endif
5369 #define FD_ALIAS "index=%d,if=floppy"
5370 #define PFLASH_ALIAS "if=pflash"
5371 #define MTD_ALIAS "if=mtd"
5372 #define SD_ALIAS "index=0,if=sd"
5373
5374 static int drive_add(const char *file, const char *fmt, ...)
5375 {
5376     va_list ap;
5377
5378     if (nb_drives_opt >= MAX_DRIVES) {
5379         fprintf(stderr, "qemu: too many drives\n");
5380         exit(1);
5381     }
5382
5383     drives_opt[nb_drives_opt].file = file;
5384     va_start(ap, fmt);
5385     vsnprintf(drives_opt[nb_drives_opt].opt,
5386               sizeof(drives_opt[0].opt), fmt, ap);
5387     va_end(ap);
5388
5389     return nb_drives_opt++;
5390 }
5391
5392 int drive_get_index(BlockInterfaceType type, int bus, int unit)
5393 {
5394     int index;
5395
5396     /* seek interface, bus and unit */
5397
5398     for (index = 0; index < nb_drives; index++)
5399         if (drives_table[index].type == type &&
5400             drives_table[index].bus == bus &&
5401             drives_table[index].unit == unit)
5402         return index;
5403
5404     return -1;
5405 }
5406
5407 int drive_get_max_bus(BlockInterfaceType type)
5408 {
5409     int max_bus;
5410     int index;
5411
5412     max_bus = -1;
5413     for (index = 0; index < nb_drives; index++) {
5414         if(drives_table[index].type == type &&
5415            drives_table[index].bus > max_bus)
5416             max_bus = drives_table[index].bus;
5417     }
5418     return max_bus;
5419 }
5420
5421 static void bdrv_format_print(void *opaque, const char *name)
5422 {
5423     fprintf(stderr, " %s", name);
5424 }
5425
5426 static int drive_init(struct drive_opt *arg, int snapshot,
5427                       QEMUMachine *machine)
5428 {
5429     char buf[128];
5430     char file[1024];
5431     char devname[128];
5432     const char *mediastr = "";
5433     BlockInterfaceType type;
5434     enum { MEDIA_DISK, MEDIA_CDROM } media;
5435     int bus_id, unit_id;
5436     int cyls, heads, secs, translation;
5437     BlockDriverState *bdrv;
5438     BlockDriver *drv = NULL;
5439     int max_devs;
5440     int index;
5441     int cache;
5442     int bdrv_flags;
5443     char *str = arg->opt;
5444     static const char * const params[] = { "bus", "unit", "if", "index",
5445                                            "cyls", "heads", "secs", "trans",
5446                                            "media", "snapshot", "file",
5447                                            "cache", "format", NULL };
5448
5449     if (check_params(buf, sizeof(buf), params, str) < 0) {
5450          fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
5451                          buf, str);
5452          return -1;
5453     }
5454
5455     file[0] = 0;
5456     cyls = heads = secs = 0;
5457     bus_id = 0;
5458     unit_id = -1;
5459     translation = BIOS_ATA_TRANSLATION_AUTO;
5460     index = -1;
5461     cache = 1;
5462
5463     if (!strcmp(machine->name, "realview") ||
5464         !strcmp(machine->name, "SS-5") ||
5465         !strcmp(machine->name, "SS-10") ||
5466         !strcmp(machine->name, "SS-600MP") ||
5467         !strcmp(machine->name, "versatilepb") ||
5468         !strcmp(machine->name, "versatileab")) {
5469         type = IF_SCSI;
5470         max_devs = MAX_SCSI_DEVS;
5471         pstrcpy(devname, sizeof(devname), "scsi");
5472     } else {
5473         type = IF_IDE;
5474         max_devs = MAX_IDE_DEVS;
5475         pstrcpy(devname, sizeof(devname), "ide");
5476     }
5477     media = MEDIA_DISK;
5478
5479     /* extract parameters */
5480
5481     if (get_param_value(buf, sizeof(buf), "bus", str)) {
5482         bus_id = strtol(buf, NULL, 0);
5483         if (bus_id < 0) {
5484             fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
5485             return -1;
5486         }
5487     }
5488
5489     if (get_param_value(buf, sizeof(buf), "unit", str)) {
5490         unit_id = strtol(buf, NULL, 0);
5491         if (unit_id < 0) {
5492             fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
5493             return -1;
5494         }
5495     }
5496
5497     if (get_param_value(buf, sizeof(buf), "if", str)) {
5498         pstrcpy(devname, sizeof(devname), buf);
5499         if (!strcmp(buf, "ide")) {
5500             type = IF_IDE;
5501             max_devs = MAX_IDE_DEVS;
5502         } else if (!strcmp(buf, "scsi")) {
5503             type = IF_SCSI;
5504             max_devs = MAX_SCSI_DEVS;
5505         } else if (!strcmp(buf, "floppy")) {
5506             type = IF_FLOPPY;
5507             max_devs = 0;
5508         } else if (!strcmp(buf, "pflash")) {
5509             type = IF_PFLASH;
5510             max_devs = 0;
5511         } else if (!strcmp(buf, "mtd")) {
5512             type = IF_MTD;
5513             max_devs = 0;
5514         } else if (!strcmp(buf, "sd")) {
5515             type = IF_SD;
5516             max_devs = 0;
5517         } else {
5518             fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
5519             return -1;
5520         }
5521     }
5522
5523     if (get_param_value(buf, sizeof(buf), "index", str)) {
5524         index = strtol(buf, NULL, 0);
5525         if (index < 0) {
5526             fprintf(stderr, "qemu: '%s' invalid index\n", str);
5527             return -1;
5528         }
5529     }
5530
5531     if (get_param_value(buf, sizeof(buf), "cyls", str)) {
5532         cyls = strtol(buf, NULL, 0);
5533     }
5534
5535     if (get_param_value(buf, sizeof(buf), "heads", str)) {
5536         heads = strtol(buf, NULL, 0);
5537     }
5538
5539     if (get_param_value(buf, sizeof(buf), "secs", str)) {
5540         secs = strtol(buf, NULL, 0);
5541     }
5542
5543     if (cyls || heads || secs) {
5544         if (cyls < 1 || cyls > 16383) {
5545             fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
5546             return -1;
5547         }
5548         if (heads < 1 || heads > 16) {
5549             fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
5550             return -1;
5551         }
5552         if (secs < 1 || secs > 63) {
5553             fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
5554             return -1;
5555         }
5556     }
5557
5558     if (get_param_value(buf, sizeof(buf), "trans", str)) {
5559         if (!cyls) {
5560             fprintf(stderr,
5561                     "qemu: '%s' trans must be used with cyls,heads and secs\n",
5562                     str);
5563             return -1;
5564         }
5565         if (!strcmp(buf, "none"))
5566             translation = BIOS_ATA_TRANSLATION_NONE;
5567         else if (!strcmp(buf, "lba"))
5568             translation = BIOS_ATA_TRANSLATION_LBA;
5569         else if (!strcmp(buf, "auto"))
5570             translation = BIOS_ATA_TRANSLATION_AUTO;
5571         else {
5572             fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
5573             return -1;
5574         }
5575     }
5576
5577     if (get_param_value(buf, sizeof(buf), "media", str)) {
5578         if (!strcmp(buf, "disk")) {
5579             media = MEDIA_DISK;
5580         } else if (!strcmp(buf, "cdrom")) {
5581             if (cyls || secs || heads) {
5582                 fprintf(stderr,
5583                         "qemu: '%s' invalid physical CHS format\n", str);
5584                 return -1;
5585             }
5586             media = MEDIA_CDROM;
5587         } else {
5588             fprintf(stderr, "qemu: '%s' invalid media\n", str);
5589             return -1;
5590         }
5591     }
5592
5593     if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
5594         if (!strcmp(buf, "on"))
5595             snapshot = 1;
5596         else if (!strcmp(buf, "off"))
5597             snapshot = 0;
5598         else {
5599             fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
5600             return -1;
5601         }
5602     }
5603
5604     if (get_param_value(buf, sizeof(buf), "cache", str)) {
5605         if (!strcmp(buf, "off"))
5606             cache = 0;
5607         else if (!strcmp(buf, "on"))
5608             cache = 1;
5609         else {
5610            fprintf(stderr, "qemu: invalid cache option\n");
5611            return -1;
5612         }
5613     }
5614
5615     if (get_param_value(buf, sizeof(buf), "format", str)) {
5616        if (strcmp(buf, "?") == 0) {
5617             fprintf(stderr, "qemu: Supported formats:");
5618             bdrv_iterate_format(bdrv_format_print, NULL);
5619             fprintf(stderr, "\n");
5620             return -1;
5621         }
5622         drv = bdrv_find_format(buf);
5623         if (!drv) {
5624             fprintf(stderr, "qemu: '%s' invalid format\n", buf);
5625             return -1;
5626         }
5627     }
5628
5629     if (arg->file == NULL)
5630         get_param_value(file, sizeof(file), "file", str);
5631     else
5632         pstrcpy(file, sizeof(file), arg->file);
5633
5634     /* compute bus and unit according index */
5635
5636     if (index != -1) {
5637         if (bus_id != 0 || unit_id != -1) {
5638             fprintf(stderr,
5639                     "qemu: '%s' index cannot be used with bus and unit\n", str);
5640             return -1;
5641         }
5642         if (max_devs == 0)
5643         {
5644             unit_id = index;
5645             bus_id = 0;
5646         } else {
5647             unit_id = index % max_devs;
5648             bus_id = index / max_devs;
5649         }
5650     }
5651
5652     /* if user doesn't specify a unit_id,
5653      * try to find the first free
5654      */
5655
5656     if (unit_id == -1) {
5657        unit_id = 0;
5658        while (drive_get_index(type, bus_id, unit_id) != -1) {
5659            unit_id++;
5660            if (max_devs && unit_id >= max_devs) {
5661                unit_id -= max_devs;
5662                bus_id++;
5663            }
5664        }
5665     }
5666
5667     /* check unit id */
5668
5669     if (max_devs && unit_id >= max_devs) {
5670         fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
5671                         str, unit_id, max_devs - 1);
5672         return -1;
5673     }
5674
5675     /*
5676      * ignore multiple definitions
5677      */
5678
5679     if (drive_get_index(type, bus_id, unit_id) != -1)
5680         return 0;
5681
5682     /* init */
5683
5684     if (type == IF_IDE || type == IF_SCSI)
5685         mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
5686     if (max_devs)
5687         snprintf(buf, sizeof(buf), "%s%i%s%i",
5688                  devname, bus_id, mediastr, unit_id);
5689     else
5690         snprintf(buf, sizeof(buf), "%s%s%i",
5691                  devname, mediastr, unit_id);
5692     bdrv = bdrv_new(buf);
5693     drives_table[nb_drives].bdrv = bdrv;
5694     drives_table[nb_drives].type = type;
5695     drives_table[nb_drives].bus = bus_id;
5696     drives_table[nb_drives].unit = unit_id;
5697     nb_drives++;
5698
5699     switch(type) {
5700     case IF_IDE:
5701     case IF_SCSI:
5702         switch(media) {
5703         case MEDIA_DISK:
5704             if (cyls != 0) {
5705                 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
5706                 bdrv_set_translation_hint(bdrv, translation);
5707             }
5708             break;
5709         case MEDIA_CDROM:
5710             bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
5711             break;
5712         }
5713         break;
5714     case IF_SD:
5715         /* FIXME: This isn't really a floppy, but it's a reasonable
5716            approximation.  */
5717     case IF_FLOPPY:
5718         bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
5719         break;
5720     case IF_PFLASH:
5721     case IF_MTD:
5722         break;
5723     }
5724     if (!file[0])
5725         return 0;
5726     bdrv_flags = 0;
5727     if (snapshot)
5728         bdrv_flags |= BDRV_O_SNAPSHOT;
5729     if (!cache)
5730         bdrv_flags |= BDRV_O_DIRECT;
5731     if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) {
5732         fprintf(stderr, "qemu: could not open disk image %s\n",
5733                         file);
5734         return -1;
5735     }
5736     return 0;
5737 }
5738
5739 /***********************************************************/
5740 /* USB devices */
5741
5742 static USBPort *used_usb_ports;
5743 static USBPort *free_usb_ports;
5744
5745 /* ??? Maybe change this to register a hub to keep track of the topology.  */
5746 void qemu_register_usb_port(USBPort *port, void *opaque, int index,
5747                             usb_attachfn attach)
5748 {
5749     port->opaque = opaque;
5750     port->index = index;
5751     port->attach = attach;
5752     port->next = free_usb_ports;
5753     free_usb_ports = port;
5754 }
5755
5756 int usb_device_add_dev(USBDevice *dev)
5757 {
5758     USBPort *port;
5759
5760     /* Find a USB port to add the device to.  */
5761     port = free_usb_ports;
5762     if (!port->next) {
5763         USBDevice *hub;
5764
5765         /* Create a new hub and chain it on.  */
5766         free_usb_ports = NULL;
5767         port->next = used_usb_ports;
5768         used_usb_ports = port;
5769
5770         hub = usb_hub_init(VM_USB_HUB_SIZE);
5771         usb_attach(port, hub);
5772         port = free_usb_ports;
5773     }
5774
5775     free_usb_ports = port->next;
5776     port->next = used_usb_ports;
5777     used_usb_ports = port;
5778     usb_attach(port, dev);
5779     return 0;
5780 }
5781
5782 static int usb_device_add(const char *devname)
5783 {
5784     const char *p;
5785     USBDevice *dev;
5786
5787     if (!free_usb_ports)
5788         return -1;
5789
5790     if (strstart(devname, "host:", &p)) {
5791         dev = usb_host_device_open(p);
5792     } else if (!strcmp(devname, "mouse")) {
5793         dev = usb_mouse_init();
5794     } else if (!strcmp(devname, "tablet")) {
5795         dev = usb_tablet_init();
5796     } else if (!strcmp(devname, "keyboard")) {
5797         dev = usb_keyboard_init();
5798     } else if (strstart(devname, "disk:", &p)) {
5799         dev = usb_msd_init(p);
5800     } else if (!strcmp(devname, "wacom-tablet")) {
5801         dev = usb_wacom_init();
5802     } else if (strstart(devname, "serial:", &p)) {
5803         dev = usb_serial_init(p);
5804 #ifdef CONFIG_BRLAPI
5805     } else if (!strcmp(devname, "braille")) {
5806         dev = usb_baum_init();
5807 #endif
5808     } else if (strstart(devname, "net:", &p)) {
5809         int nic = nb_nics;
5810
5811         if (net_client_init("nic", p) < 0)
5812             return -1;
5813         nd_table[nic].model = "usb";
5814         dev = usb_net_init(&nd_table[nic]);
5815     } else {
5816         return -1;
5817     }
5818     if (!dev)
5819         return -1;
5820
5821     return usb_device_add_dev(dev);
5822 }
5823
5824 int usb_device_del_addr(int bus_num, int addr)
5825 {
5826     USBPort *port;
5827     USBPort **lastp;
5828     USBDevice *dev;
5829
5830     if (!used_usb_ports)
5831         return -1;
5832
5833     if (bus_num != 0)
5834         return -1;
5835
5836     lastp = &used_usb_ports;
5837     port = used_usb_ports;
5838     while (port && port->dev->addr != addr) {
5839         lastp = &port->next;
5840         port = port->next;
5841     }
5842
5843     if (!port)
5844         return -1;
5845
5846     dev = port->dev;
5847     *lastp = port->next;
5848     usb_attach(port, NULL);
5849     dev->handle_destroy(dev);
5850     port->next = free_usb_ports;
5851     free_usb_ports = port;
5852     return 0;
5853 }
5854
5855 static int usb_device_del(const char *devname)
5856 {
5857     int bus_num, addr;
5858     const char *p;
5859
5860     if (strstart(devname, "host:", &p))
5861         return usb_host_device_close(p);
5862
5863     if (!used_usb_ports)
5864         return -1;
5865
5866     p = strchr(devname, '.');
5867     if (!p)
5868         return -1;
5869     bus_num = strtoul(devname, NULL, 0);
5870     addr = strtoul(p + 1, NULL, 0);
5871
5872     return usb_device_del_addr(bus_num, addr);
5873 }
5874
5875 void do_usb_add(const char *devname)
5876 {
5877     usb_device_add(devname);
5878 }
5879
5880 void do_usb_del(const char *devname)
5881 {
5882     usb_device_del(devname);
5883 }
5884
5885 void usb_info(void)
5886 {
5887     USBDevice *dev;
5888     USBPort *port;
5889     const char *speed_str;
5890
5891     if (!usb_enabled) {
5892         term_printf("USB support not enabled\n");
5893         return;
5894     }
5895
5896     for (port = used_usb_ports; port; port = port->next) {
5897         dev = port->dev;
5898         if (!dev)
5899             continue;
5900         switch(dev->speed) {
5901         case USB_SPEED_LOW:
5902             speed_str = "1.5";
5903             break;
5904         case USB_SPEED_FULL:
5905             speed_str = "12";
5906             break;
5907         case USB_SPEED_HIGH:
5908             speed_str = "480";
5909             break;
5910         default:
5911             speed_str = "?";
5912             break;
5913         }
5914         term_printf("  Device %d.%d, Speed %s Mb/s, Product %s\n",
5915                     0, dev->addr, speed_str, dev->devname);
5916     }
5917 }
5918
5919 /***********************************************************/
5920 /* PCMCIA/Cardbus */
5921
5922 static struct pcmcia_socket_entry_s {
5923     struct pcmcia_socket_s *socket;
5924     struct pcmcia_socket_entry_s *next;
5925 } *pcmcia_sockets = 0;
5926
5927 void pcmcia_socket_register(struct pcmcia_socket_s *socket)
5928 {
5929     struct pcmcia_socket_entry_s *entry;
5930
5931     entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
5932     entry->socket = socket;
5933     entry->next = pcmcia_sockets;
5934     pcmcia_sockets = entry;
5935 }
5936
5937 void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
5938 {
5939     struct pcmcia_socket_entry_s *entry, **ptr;
5940
5941     ptr = &pcmcia_sockets;
5942     for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
5943         if (entry->socket == socket) {
5944             *ptr = entry->next;
5945             qemu_free(entry);
5946         }
5947 }
5948
5949 void pcmcia_info(void)
5950 {
5951     struct pcmcia_socket_entry_s *iter;
5952     if (!pcmcia_sockets)
5953         term_printf("No PCMCIA sockets\n");
5954
5955     for (iter = pcmcia_sockets; iter; iter = iter->next)
5956         term_printf("%s: %s\n", iter->socket->slot_string,
5957                     iter->socket->attached ? iter->socket->card_string :
5958                     "Empty");
5959 }
5960
5961 /***********************************************************/
5962 /* dumb display */
5963
5964 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
5965 {
5966 }
5967
5968 static void dumb_resize(DisplayState *ds, int w, int h)
5969 {
5970 }
5971
5972 static void dumb_refresh(DisplayState *ds)
5973 {
5974 #if defined(CONFIG_SDL)
5975     vga_hw_update();
5976 #endif
5977 }
5978
5979 static void dumb_display_init(DisplayState *ds)
5980 {
5981     ds->data = NULL;
5982     ds->linesize = 0;
5983     ds->depth = 0;
5984     ds->dpy_update = dumb_update;
5985     ds->dpy_resize = dumb_resize;
5986     ds->dpy_refresh = dumb_refresh;
5987     ds->gui_timer_interval = 500;
5988     ds->idle = 1;
5989 }
5990
5991 /***********************************************************/
5992 /* I/O handling */
5993
5994 #define MAX_IO_HANDLERS 64
5995
5996 typedef struct IOHandlerRecord {
5997     int fd;
5998     IOCanRWHandler *fd_read_poll;
5999     IOHandler *fd_read;
6000     IOHandler *fd_write;
6001     int deleted;
6002     void *opaque;
6003     /* temporary data */
6004     struct pollfd *ufd;
6005     struct IOHandlerRecord *next;
6006 } IOHandlerRecord;
6007
6008 static IOHandlerRecord *first_io_handler;
6009
6010 /* XXX: fd_read_poll should be suppressed, but an API change is
6011    necessary in the character devices to suppress fd_can_read(). */
6012 int qemu_set_fd_handler2(int fd,
6013                          IOCanRWHandler *fd_read_poll,
6014                          IOHandler *fd_read,
6015                          IOHandler *fd_write,
6016                          void *opaque)
6017 {
6018     IOHandlerRecord **pioh, *ioh;
6019
6020     if (!fd_read && !fd_write) {
6021         pioh = &first_io_handler;
6022         for(;;) {
6023             ioh = *pioh;
6024             if (ioh == NULL)
6025                 break;
6026             if (ioh->fd == fd) {
6027                 ioh->deleted = 1;
6028                 break;
6029             }
6030             pioh = &ioh->next;
6031         }
6032     } else {
6033         for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
6034             if (ioh->fd == fd)
6035                 goto found;
6036         }
6037         ioh = qemu_mallocz(sizeof(IOHandlerRecord));
6038         if (!ioh)
6039             return -1;
6040         ioh->next = first_io_handler;
6041         first_io_handler = ioh;
6042     found:
6043         ioh->fd = fd;
6044         ioh->fd_read_poll = fd_read_poll;
6045         ioh->fd_read = fd_read;
6046         ioh->fd_write = fd_write;
6047         ioh->opaque = opaque;
6048         ioh->deleted = 0;
6049     }
6050     return 0;
6051 }
6052
6053 int qemu_set_fd_handler(int fd,
6054                         IOHandler *fd_read,
6055                         IOHandler *fd_write,
6056                         void *opaque)
6057 {
6058     return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
6059 }
6060
6061 /***********************************************************/
6062 /* Polling handling */
6063
6064 typedef struct PollingEntry {
6065     PollingFunc *func;
6066     void *opaque;
6067     struct PollingEntry *next;
6068 } PollingEntry;
6069
6070 static PollingEntry *first_polling_entry;
6071
6072 int qemu_add_polling_cb(PollingFunc *func, void *opaque)
6073 {
6074     PollingEntry **ppe, *pe;
6075     pe = qemu_mallocz(sizeof(PollingEntry));
6076     if (!pe)
6077         return -1;
6078     pe->func = func;
6079     pe->opaque = opaque;
6080     for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
6081     *ppe = pe;
6082     return 0;
6083 }
6084
6085 void qemu_del_polling_cb(PollingFunc *func, void *opaque)
6086 {
6087     PollingEntry **ppe, *pe;
6088     for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
6089         pe = *ppe;
6090         if (pe->func == func && pe->opaque == opaque) {
6091             *ppe = pe->next;
6092             qemu_free(pe);
6093             break;
6094         }
6095     }
6096 }
6097
6098 #ifdef _WIN32
6099 /***********************************************************/
6100 /* Wait objects support */
6101 typedef struct WaitObjects {
6102     int num;
6103     HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
6104     WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
6105     void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
6106 } WaitObjects;
6107
6108 static WaitObjects wait_objects = {0};
6109
6110 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
6111 {
6112     WaitObjects *w = &wait_objects;
6113
6114     if (w->num >= MAXIMUM_WAIT_OBJECTS)
6115         return -1;
6116     w->events[w->num] = handle;
6117     w->func[w->num] = func;
6118     w->opaque[w->num] = opaque;
6119     w->num++;
6120     return 0;
6121 }
6122
6123 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
6124 {
6125     int i, found;
6126     WaitObjects *w = &wait_objects;
6127
6128     found = 0;
6129     for (i = 0; i < w->num; i++) {
6130         if (w->events[i] == handle)
6131             found = 1;
6132         if (found) {
6133             w->events[i] = w->events[i + 1];
6134             w->func[i] = w->func[i + 1];
6135             w->opaque[i] = w->opaque[i + 1];
6136         }
6137     }
6138     if (found)
6139         w->num--;
6140 }
6141 #endif
6142
6143 /***********************************************************/
6144 /* savevm/loadvm support */
6145
6146 #define IO_BUF_SIZE 32768
6147
6148 struct QEMUFile {
6149     FILE *outfile;
6150     BlockDriverState *bs;
6151     int is_file;
6152     int is_writable;
6153     int64_t base_offset;
6154     int64_t buf_offset; /* start of buffer when writing, end of buffer
6155                            when reading */
6156     int buf_index;
6157     int buf_size; /* 0 when writing */
6158     uint8_t buf[IO_BUF_SIZE];
6159 };
6160
6161 QEMUFile *qemu_fopen(const char *filename, const char *mode)
6162 {
6163     QEMUFile *f;
6164
6165     f = qemu_mallocz(sizeof(QEMUFile));
6166     if (!f)
6167         return NULL;
6168     if (!strcmp(mode, "wb")) {
6169         f->is_writable = 1;
6170     } else if (!strcmp(mode, "rb")) {
6171         f->is_writable = 0;
6172     } else {
6173         goto fail;
6174     }
6175     f->outfile = fopen(filename, mode);
6176     if (!f->outfile)
6177         goto fail;
6178     f->is_file = 1;
6179     return f;
6180  fail:
6181     if (f->outfile)
6182         fclose(f->outfile);
6183     qemu_free(f);
6184     return NULL;
6185 }
6186
6187 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
6188 {
6189     QEMUFile *f;
6190
6191     f = qemu_mallocz(sizeof(QEMUFile));
6192     if (!f)
6193         return NULL;
6194     f->is_file = 0;
6195     f->bs = bs;
6196     f->is_writable = is_writable;
6197     f->base_offset = offset;
6198     return f;
6199 }
6200
6201 void qemu_fflush(QEMUFile *f)
6202 {
6203     if (!f->is_writable)
6204         return;
6205     if (f->buf_index > 0) {
6206         if (f->is_file) {
6207             fseek(f->outfile, f->buf_offset, SEEK_SET);
6208             fwrite(f->buf, 1, f->buf_index, f->outfile);
6209         } else {
6210             bdrv_pwrite(f->bs, f->base_offset + f->buf_offset,
6211                         f->buf, f->buf_index);
6212         }
6213         f->buf_offset += f->buf_index;
6214         f->buf_index = 0;
6215     }
6216 }
6217
6218 static void qemu_fill_buffer(QEMUFile *f)
6219 {
6220     int len;
6221
6222     if (f->is_writable)
6223         return;
6224     if (f->is_file) {
6225         fseek(f->outfile, f->buf_offset, SEEK_SET);
6226         len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile);
6227         if (len < 0)
6228             len = 0;
6229     } else {
6230         len = bdrv_pread(f->bs, f->base_offset + f->buf_offset,
6231                          f->buf, IO_BUF_SIZE);
6232         if (len < 0)
6233             len = 0;
6234     }
6235     f->buf_index = 0;
6236     f->buf_size = len;
6237     f->buf_offset += len;
6238 }
6239
6240 void qemu_fclose(QEMUFile *f)
6241 {
6242     if (f->is_writable)
6243         qemu_fflush(f);
6244     if (f->is_file) {
6245         fclose(f->outfile);
6246     }
6247     qemu_free(f);
6248 }
6249
6250 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
6251 {
6252     size_t l;
6253     while (size > 0) {
6254         l = IO_BUF_SIZE - f->buf_index;
6255         if (l > size)
6256             l = size;
6257         memcpy(f->buf + f->buf_index, buf, l);
6258         f->buf_index += l;
6259         buf += l;
6260         size -= l;
6261         if (f->buf_index >= IO_BUF_SIZE)
6262             qemu_fflush(f);
6263     }
6264 }
6265
6266 void qemu_put_byte(QEMUFile *f, int8_t v)
6267 {
6268     f->buf[f->buf_index++] = v;
6269     if (f->buf_index >= IO_BUF_SIZE)
6270         qemu_fflush(f);
6271 }
6272
6273 size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size1)
6274 {
6275     size_t size, l;
6276
6277     size = size1;
6278     while (size > 0) {
6279         l = f->buf_size - f->buf_index;
6280         if (l == 0) {
6281             qemu_fill_buffer(f);
6282             l = f->buf_size - f->buf_index;
6283             if (l == 0)
6284                 break;
6285         }
6286         if (l > size)
6287             l = size;
6288         memcpy(buf, f->buf + f->buf_index, l);
6289         f->buf_index += l;
6290         buf += l;
6291         size -= l;
6292     }
6293     return size1 - size;
6294 }
6295
6296 int8_t qemu_get_byte(QEMUFile *f)
6297 {
6298     if (f->buf_index >= f->buf_size) {
6299         qemu_fill_buffer(f);
6300         if (f->buf_index >= f->buf_size)
6301             return 0;
6302     }
6303     return f->buf[f->buf_index++];
6304 }
6305
6306 int64_t qemu_ftell(QEMUFile *f)
6307 {
6308     return f->buf_offset - f->buf_size + f->buf_index;
6309 }
6310
6311 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
6312 {
6313     if (whence == SEEK_SET) {
6314         /* nothing to do */
6315     } else if (whence == SEEK_CUR) {
6316         pos += qemu_ftell(f);
6317     } else {
6318         /* SEEK_END not supported */
6319         return -1;
6320     }
6321     if (f->is_writable) {
6322         qemu_fflush(f);
6323         f->buf_offset = pos;
6324     } else {
6325         f->buf_offset = pos;
6326         f->buf_index = 0;
6327         f->buf_size = 0;
6328     }
6329     return pos;
6330 }
6331
6332 void qemu_put_be16(QEMUFile *f, uint16_t v)
6333 {
6334     qemu_put_byte(f, v >> 8);
6335     qemu_put_byte(f, v);
6336 }
6337
6338 void qemu_put_be32(QEMUFile *f, uint32_t v)
6339 {
6340     qemu_put_byte(f, v >> 24);
6341     qemu_put_byte(f, v >> 16);
6342     qemu_put_byte(f, v >> 8);
6343     qemu_put_byte(f, v);
6344 }
6345
6346 void qemu_put_be64(QEMUFile *f, uint64_t v)
6347 {
6348     qemu_put_be32(f, v >> 32);
6349     qemu_put_be32(f, v);
6350 }
6351
6352 uint16_t qemu_get_be16(QEMUFile *f)
6353 {
6354     uint16_t v;
6355     v = qemu_get_byte(f) << 8;
6356     v |= qemu_get_byte(f);
6357     return v;
6358 }
6359
6360 uint32_t qemu_get_be32(QEMUFile *f)
6361 {
6362     uint32_t v;
6363     v = qemu_get_byte(f) << 24;
6364     v |= qemu_get_byte(f) << 16;
6365     v |= qemu_get_byte(f) << 8;
6366     v |= qemu_get_byte(f);
6367     return v;
6368 }
6369
6370 uint64_t qemu_get_be64(QEMUFile *f)
6371 {
6372     uint64_t v;
6373     v = (uint64_t)qemu_get_be32(f) << 32;
6374     v |= qemu_get_be32(f);
6375     return v;
6376 }
6377
6378 typedef struct SaveStateEntry {
6379     char idstr[256];
6380     int instance_id;
6381     int version_id;
6382     SaveStateHandler *save_state;
6383     LoadStateHandler *load_state;
6384     void *opaque;
6385     struct SaveStateEntry *next;
6386 } SaveStateEntry;
6387
6388 static SaveStateEntry *first_se;
6389
6390 /* TODO: Individual devices generally have very little idea about the rest
6391    of the system, so instance_id should be removed/replaced.
6392    Meanwhile pass -1 as instance_id if you do not already have a clearly
6393    distinguishing id for all instances of your device class. */
6394 int register_savevm(const char *idstr,
6395                     int instance_id,
6396                     int version_id,
6397                     SaveStateHandler *save_state,
6398                     LoadStateHandler *load_state,
6399                     void *opaque)
6400 {
6401     SaveStateEntry *se, **pse;
6402
6403     se = qemu_malloc(sizeof(SaveStateEntry));
6404     if (!se)
6405         return -1;
6406     pstrcpy(se->idstr, sizeof(se->idstr), idstr);
6407     se->instance_id = (instance_id == -1) ? 0 : instance_id;
6408     se->version_id = version_id;
6409     se->save_state = save_state;
6410     se->load_state = load_state;
6411     se->opaque = opaque;
6412     se->next = NULL;
6413
6414     /* add at the end of list */
6415     pse = &first_se;
6416     while (*pse != NULL) {
6417         if (instance_id == -1
6418                 && strcmp(se->idstr, (*pse)->idstr) == 0
6419                 && se->instance_id <= (*pse)->instance_id)
6420             se->instance_id = (*pse)->instance_id + 1;
6421         pse = &(*pse)->next;
6422     }
6423     *pse = se;
6424     return 0;
6425 }
6426
6427 #define QEMU_VM_FILE_MAGIC   0x5145564d
6428 #define QEMU_VM_FILE_VERSION 0x00000002
6429
6430 static int qemu_savevm_state(QEMUFile *f)
6431 {
6432     SaveStateEntry *se;
6433     int len, ret;
6434     int64_t cur_pos, len_pos, total_len_pos;
6435
6436     qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
6437     qemu_put_be32(f, QEMU_VM_FILE_VERSION);
6438     total_len_pos = qemu_ftell(f);
6439     qemu_put_be64(f, 0); /* total size */
6440
6441     for(se = first_se; se != NULL; se = se->next) {
6442         if (se->save_state == NULL)
6443             /* this one has a loader only, for backwards compatibility */
6444             continue;
6445
6446         /* ID string */
6447         len = strlen(se->idstr);
6448         qemu_put_byte(f, len);
6449         qemu_put_buffer(f, (uint8_t *)se->idstr, len);
6450
6451         qemu_put_be32(f, se->instance_id);
6452         qemu_put_be32(f, se->version_id);
6453
6454         /* record size: filled later */
6455         len_pos = qemu_ftell(f);
6456         qemu_put_be32(f, 0);
6457         se->save_state(f, se->opaque);
6458
6459         /* fill record size */
6460         cur_pos = qemu_ftell(f);
6461         len = cur_pos - len_pos - 4;
6462         qemu_fseek(f, len_pos, SEEK_SET);
6463         qemu_put_be32(f, len);
6464         qemu_fseek(f, cur_pos, SEEK_SET);
6465     }
6466     cur_pos = qemu_ftell(f);
6467     qemu_fseek(f, total_len_pos, SEEK_SET);
6468     qemu_put_be64(f, cur_pos - total_len_pos - 8);
6469     qemu_fseek(f, cur_pos, SEEK_SET);
6470
6471     ret = 0;
6472     return ret;
6473 }
6474
6475 static SaveStateEntry *find_se(const char *idstr, int instance_id)
6476 {
6477     SaveStateEntry *se;
6478
6479     for(se = first_se; se != NULL; se = se->next) {
6480         if (!strcmp(se->idstr, idstr) &&
6481             instance_id == se->instance_id)
6482             return se;
6483     }
6484     return NULL;
6485 }
6486
6487 static int qemu_loadvm_state(QEMUFile *f)
6488 {
6489     SaveStateEntry *se;
6490     int len, ret, instance_id, record_len, version_id;
6491     int64_t total_len, end_pos, cur_pos;
6492     unsigned int v;
6493     char idstr[256];
6494
6495     v = qemu_get_be32(f);
6496     if (v != QEMU_VM_FILE_MAGIC)
6497         goto fail;
6498     v = qemu_get_be32(f);
6499     if (v != QEMU_VM_FILE_VERSION) {
6500     fail:
6501         ret = -1;
6502         goto the_end;
6503     }
6504     total_len = qemu_get_be64(f);
6505     end_pos = total_len + qemu_ftell(f);
6506     for(;;) {
6507         if (qemu_ftell(f) >= end_pos)
6508             break;
6509         len = qemu_get_byte(f);
6510         qemu_get_buffer(f, (uint8_t *)idstr, len);
6511         idstr[len] = '\0';
6512         instance_id = qemu_get_be32(f);
6513         version_id = qemu_get_be32(f);
6514         record_len = qemu_get_be32(f);
6515 #if 0
6516         printf("idstr=%s instance=0x%x version=%d len=%d\n",
6517                idstr, instance_id, version_id, record_len);
6518 #endif
6519         cur_pos = qemu_ftell(f);
6520         se = find_se(idstr, instance_id);
6521         if (!se) {
6522             fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
6523                     instance_id, idstr);
6524         } else {
6525             ret = se->load_state(f, se->opaque, version_id);
6526             if (ret < 0) {
6527                 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
6528                         instance_id, idstr);
6529             }
6530         }
6531         /* always seek to exact end of record */
6532         qemu_fseek(f, cur_pos + record_len, SEEK_SET);
6533     }
6534     ret = 0;
6535  the_end:
6536     return ret;
6537 }
6538
6539 /* device can contain snapshots */
6540 static int bdrv_can_snapshot(BlockDriverState *bs)
6541 {
6542     return (bs &&
6543             !bdrv_is_removable(bs) &&
6544             !bdrv_is_read_only(bs));
6545 }
6546
6547 /* device must be snapshots in order to have a reliable snapshot */
6548 static int bdrv_has_snapshot(BlockDriverState *bs)
6549 {
6550     return (bs &&
6551             !bdrv_is_removable(bs) &&
6552             !bdrv_is_read_only(bs));
6553 }
6554
6555 static BlockDriverState *get_bs_snapshots(void)
6556 {
6557     BlockDriverState *bs;
6558     int i;
6559
6560     if (bs_snapshots)
6561         return bs_snapshots;
6562     for(i = 0; i <= nb_drives; i++) {
6563         bs = drives_table[i].bdrv;
6564         if (bdrv_can_snapshot(bs))
6565             goto ok;
6566     }
6567     return NULL;
6568  ok:
6569     bs_snapshots = bs;
6570     return bs;
6571 }
6572
6573 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
6574                               const char *name)
6575 {
6576     QEMUSnapshotInfo *sn_tab, *sn;
6577     int nb_sns, i, ret;
6578
6579     ret = -ENOENT;
6580     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
6581     if (nb_sns < 0)
6582         return ret;
6583     for(i = 0; i < nb_sns; i++) {
6584         sn = &sn_tab[i];
6585         if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
6586             *sn_info = *sn;
6587             ret = 0;
6588             break;
6589         }
6590     }
6591     qemu_free(sn_tab);
6592     return ret;
6593 }
6594
6595 void do_savevm(const char *name)
6596 {
6597     BlockDriverState *bs, *bs1;
6598     QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
6599     int must_delete, ret, i;
6600     BlockDriverInfo bdi1, *bdi = &bdi1;
6601     QEMUFile *f;
6602     int saved_vm_running;
6603 #ifdef _WIN32
6604     struct _timeb tb;
6605 #else
6606     struct timeval tv;
6607 #endif
6608
6609     bs = get_bs_snapshots();
6610     if (!bs) {
6611         term_printf("No block device can accept snapshots\n");
6612         return;
6613     }
6614
6615     /* ??? Should this occur after vm_stop?  */
6616     qemu_aio_flush();
6617
6618     saved_vm_running = vm_running;
6619     vm_stop(0);
6620
6621     must_delete = 0;
6622     if (name) {
6623         ret = bdrv_snapshot_find(bs, old_sn, name);
6624         if (ret >= 0) {
6625             must_delete = 1;
6626         }
6627     }
6628     memset(sn, 0, sizeof(*sn));
6629     if (must_delete) {
6630         pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
6631         pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
6632     } else {
6633         if (name)
6634             pstrcpy(sn->name, sizeof(sn->name), name);
6635     }
6636
6637     /* fill auxiliary fields */
6638 #ifdef _WIN32
6639     _ftime(&tb);
6640     sn->date_sec = tb.time;
6641     sn->date_nsec = tb.millitm * 1000000;
6642 #else
6643     gettimeofday(&tv, NULL);
6644     sn->date_sec = tv.tv_sec;
6645     sn->date_nsec = tv.tv_usec * 1000;
6646 #endif
6647     sn->vm_clock_nsec = qemu_get_clock(vm_clock);
6648
6649     if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
6650         term_printf("Device %s does not support VM state snapshots\n",
6651                     bdrv_get_device_name(bs));
6652         goto the_end;
6653     }
6654
6655     /* save the VM state */
6656     f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
6657     if (!f) {
6658         term_printf("Could not open VM state file\n");
6659         goto the_end;
6660     }
6661     ret = qemu_savevm_state(f);
6662     sn->vm_state_size = qemu_ftell(f);
6663     qemu_fclose(f);
6664     if (ret < 0) {
6665         term_printf("Error %d while writing VM\n", ret);
6666         goto the_end;
6667     }
6668
6669     /* create the snapshots */
6670
6671     for(i = 0; i < nb_drives; i++) {
6672         bs1 = drives_table[i].bdrv;
6673         if (bdrv_has_snapshot(bs1)) {
6674             if (must_delete) {
6675                 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
6676                 if (ret < 0) {
6677                     term_printf("Error while deleting snapshot on '%s'\n",
6678                                 bdrv_get_device_name(bs1));
6679                 }
6680             }
6681             ret = bdrv_snapshot_create(bs1, sn);
6682             if (ret < 0) {
6683                 term_printf("Error while creating snapshot on '%s'\n",
6684                             bdrv_get_device_name(bs1));
6685             }
6686         }
6687     }
6688
6689  the_end:
6690     if (saved_vm_running)
6691         vm_start();
6692 }
6693
6694 void do_loadvm(const char *name)
6695 {
6696     BlockDriverState *bs, *bs1;
6697     BlockDriverInfo bdi1, *bdi = &bdi1;
6698     QEMUFile *f;
6699     int i, ret;
6700     int saved_vm_running;
6701
6702     bs = get_bs_snapshots();
6703     if (!bs) {
6704         term_printf("No block device supports snapshots\n");
6705         return;
6706     }
6707
6708     /* Flush all IO requests so they don't interfere with the new state.  */
6709     qemu_aio_flush();
6710
6711     saved_vm_running = vm_running;
6712     vm_stop(0);
6713
6714     for(i = 0; i <= nb_drives; i++) {
6715         bs1 = drives_table[i].bdrv;
6716         if (bdrv_has_snapshot(bs1)) {
6717             ret = bdrv_snapshot_goto(bs1, name);
6718             if (ret < 0) {
6719                 if (bs != bs1)
6720                     term_printf("Warning: ");
6721                 switch(ret) {
6722                 case -ENOTSUP:
6723                     term_printf("Snapshots not supported on device '%s'\n",
6724                                 bdrv_get_device_name(bs1));
6725                     break;
6726                 case -ENOENT:
6727                     term_printf("Could not find snapshot '%s' on device '%s'\n",
6728                                 name, bdrv_get_device_name(bs1));
6729                     break;
6730                 default:
6731                     term_printf("Error %d while activating snapshot on '%s'\n",
6732                                 ret, bdrv_get_device_name(bs1));
6733                     break;
6734                 }
6735                 /* fatal on snapshot block device */
6736                 if (bs == bs1)
6737                     goto the_end;
6738             }
6739         }
6740     }
6741
6742     if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
6743         term_printf("Device %s does not support VM state snapshots\n",
6744                     bdrv_get_device_name(bs));
6745         return;
6746     }
6747
6748     /* restore the VM state */
6749     f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
6750     if (!f) {
6751         term_printf("Could not open VM state file\n");
6752         goto the_end;
6753     }
6754     ret = qemu_loadvm_state(f);
6755     qemu_fclose(f);
6756     if (ret < 0) {
6757         term_printf("Error %d while loading VM state\n", ret);
6758     }
6759  the_end:
6760     if (saved_vm_running)
6761         vm_start();
6762 }
6763
6764 void do_delvm(const char *name)
6765 {
6766     BlockDriverState *bs, *bs1;
6767     int i, ret;
6768
6769     bs = get_bs_snapshots();
6770     if (!bs) {
6771         term_printf("No block device supports snapshots\n");
6772         return;
6773     }
6774
6775     for(i = 0; i <= nb_drives; i++) {
6776         bs1 = drives_table[i].bdrv;
6777         if (bdrv_has_snapshot(bs1)) {
6778             ret = bdrv_snapshot_delete(bs1, name);
6779             if (ret < 0) {
6780                 if (ret == -ENOTSUP)
6781                     term_printf("Snapshots not supported on device '%s'\n",
6782                                 bdrv_get_device_name(bs1));
6783                 else
6784                     term_printf("Error %d while deleting snapshot on '%s'\n",
6785                                 ret, bdrv_get_device_name(bs1));
6786             }
6787         }
6788     }
6789 }
6790
6791 void do_info_snapshots(void)
6792 {
6793     BlockDriverState *bs, *bs1;
6794     QEMUSnapshotInfo *sn_tab, *sn;
6795     int nb_sns, i;
6796     char buf[256];
6797
6798     bs = get_bs_snapshots();
6799     if (!bs) {
6800         term_printf("No available block device supports snapshots\n");
6801         return;
6802     }
6803     term_printf("Snapshot devices:");
6804     for(i = 0; i <= nb_drives; i++) {
6805         bs1 = drives_table[i].bdrv;
6806         if (bdrv_has_snapshot(bs1)) {
6807             if (bs == bs1)
6808                 term_printf(" %s", bdrv_get_device_name(bs1));
6809         }
6810     }
6811     term_printf("\n");
6812
6813     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
6814     if (nb_sns < 0) {
6815         term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
6816         return;
6817     }
6818     term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
6819     term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
6820     for(i = 0; i < nb_sns; i++) {
6821         sn = &sn_tab[i];
6822         term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
6823     }
6824     qemu_free(sn_tab);
6825 }
6826
6827 /***********************************************************/
6828 /* ram save/restore */
6829
6830 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
6831 {
6832     int v;
6833
6834     v = qemu_get_byte(f);
6835     switch(v) {
6836     case 0:
6837         if (qemu_get_buffer(f, buf, len) != len)
6838             return -EIO;
6839         break;
6840     case 1:
6841         v = qemu_get_byte(f);
6842         memset(buf, v, len);
6843         break;
6844     default:
6845         return -EINVAL;
6846     }
6847     return 0;
6848 }
6849
6850 static int ram_load_v1(QEMUFile *f, void *opaque)
6851 {
6852     int ret;
6853     ram_addr_t i;
6854
6855     if (qemu_get_be32(f) != phys_ram_size)
6856         return -EINVAL;
6857     for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
6858         ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
6859         if (ret)
6860             return ret;
6861     }
6862     return 0;
6863 }
6864
6865 #define BDRV_HASH_BLOCK_SIZE 1024
6866 #define IOBUF_SIZE 4096
6867 #define RAM_CBLOCK_MAGIC 0xfabe
6868
6869 typedef struct RamCompressState {
6870     z_stream zstream;
6871     QEMUFile *f;
6872     uint8_t buf[IOBUF_SIZE];
6873 } RamCompressState;
6874
6875 static int ram_compress_open(RamCompressState *s, QEMUFile *f)
6876 {
6877     int ret;
6878     memset(s, 0, sizeof(*s));
6879     s->f = f;
6880     ret = deflateInit2(&s->zstream, 1,
6881                        Z_DEFLATED, 15,
6882                        9, Z_DEFAULT_STRATEGY);
6883     if (ret != Z_OK)
6884         return -1;
6885     s->zstream.avail_out = IOBUF_SIZE;
6886     s->zstream.next_out = s->buf;
6887     return 0;
6888 }
6889
6890 static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len)
6891 {
6892     qemu_put_be16(s->f, RAM_CBLOCK_MAGIC);
6893     qemu_put_be16(s->f, len);
6894     qemu_put_buffer(s->f, buf, len);
6895 }
6896
6897 static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len)
6898 {
6899     int ret;
6900
6901     s->zstream.avail_in = len;
6902     s->zstream.next_in = (uint8_t *)buf;
6903     while (s->zstream.avail_in > 0) {
6904         ret = deflate(&s->zstream, Z_NO_FLUSH);
6905         if (ret != Z_OK)
6906             return -1;
6907         if (s->zstream.avail_out == 0) {
6908             ram_put_cblock(s, s->buf, IOBUF_SIZE);
6909             s->zstream.avail_out = IOBUF_SIZE;
6910             s->zstream.next_out = s->buf;
6911         }
6912     }
6913     return 0;
6914 }
6915
6916 static void ram_compress_close(RamCompressState *s)
6917 {
6918     int len, ret;
6919
6920     /* compress last bytes */
6921     for(;;) {
6922         ret = deflate(&s->zstream, Z_FINISH);
6923         if (ret == Z_OK || ret == Z_STREAM_END) {
6924             len = IOBUF_SIZE - s->zstream.avail_out;
6925             if (len > 0) {
6926                 ram_put_cblock(s, s->buf, len);
6927             }
6928             s->zstream.avail_out = IOBUF_SIZE;
6929             s->zstream.next_out = s->buf;
6930             if (ret == Z_STREAM_END)
6931                 break;
6932         } else {
6933             goto fail;
6934         }
6935     }
6936 fail:
6937     deflateEnd(&s->zstream);
6938 }
6939
6940 typedef struct RamDecompressState {
6941     z_stream zstream;
6942     QEMUFile *f;
6943     uint8_t buf[IOBUF_SIZE];
6944 } RamDecompressState;
6945
6946 static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
6947 {
6948     int ret;
6949     memset(s, 0, sizeof(*s));
6950     s->f = f;
6951     ret = inflateInit(&s->zstream);
6952     if (ret != Z_OK)
6953         return -1;
6954     return 0;
6955 }
6956
6957 static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
6958 {
6959     int ret, clen;
6960
6961     s->zstream.avail_out = len;
6962     s->zstream.next_out = buf;
6963     while (s->zstream.avail_out > 0) {
6964         if (s->zstream.avail_in == 0) {
6965             if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
6966                 return -1;
6967             clen = qemu_get_be16(s->f);
6968             if (clen > IOBUF_SIZE)
6969                 return -1;
6970             qemu_get_buffer(s->f, s->buf, clen);
6971             s->zstream.avail_in = clen;
6972             s->zstream.next_in = s->buf;
6973         }
6974         ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
6975         if (ret != Z_OK && ret != Z_STREAM_END) {
6976             return -1;
6977         }
6978     }
6979     return 0;
6980 }
6981
6982 static void ram_decompress_close(RamDecompressState *s)
6983 {
6984     inflateEnd(&s->zstream);
6985 }
6986
6987 static void ram_save(QEMUFile *f, void *opaque)
6988 {
6989     ram_addr_t i;
6990     RamCompressState s1, *s = &s1;
6991     uint8_t buf[10];
6992
6993     qemu_put_be32(f, phys_ram_size);
6994     if (ram_compress_open(s, f) < 0)
6995         return;
6996     for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
6997 #if 0
6998         if (tight_savevm_enabled) {
6999             int64_t sector_num;
7000             int j;
7001
7002             /* find if the memory block is available on a virtual
7003                block device */
7004             sector_num = -1;
7005             for(j = 0; j < nb_drives; j++) {
7006                 sector_num = bdrv_hash_find(drives_table[j].bdrv,
7007                                             phys_ram_base + i,
7008                                             BDRV_HASH_BLOCK_SIZE);
7009                 if (sector_num >= 0)
7010                     break;
7011             }
7012             if (j == nb_drives)
7013                 goto normal_compress;
7014             buf[0] = 1;
7015             buf[1] = j;
7016             cpu_to_be64wu((uint64_t *)(buf + 2), sector_num);
7017             ram_compress_buf(s, buf, 10);
7018         } else
7019 #endif
7020         {
7021             //        normal_compress:
7022             buf[0] = 0;
7023             ram_compress_buf(s, buf, 1);
7024             ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE);
7025         }
7026     }
7027     ram_compress_close(s);
7028 }
7029
7030 static int ram_load(QEMUFile *f, void *opaque, int version_id)
7031 {
7032     RamDecompressState s1, *s = &s1;
7033     uint8_t buf[10];
7034     ram_addr_t i;
7035
7036     if (version_id == 1)
7037         return ram_load_v1(f, opaque);
7038     if (version_id != 2)
7039         return -EINVAL;
7040     if (qemu_get_be32(f) != phys_ram_size)
7041         return -EINVAL;
7042     if (ram_decompress_open(s, f) < 0)
7043         return -EINVAL;
7044     for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
7045         if (ram_decompress_buf(s, buf, 1) < 0) {
7046             fprintf(stderr, "Error while reading ram block header\n");
7047             goto error;
7048         }
7049         if (buf[0] == 0) {
7050             if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
7051                 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
7052                 goto error;
7053             }
7054         } else
7055 #if 0
7056         if (buf[0] == 1) {
7057             int bs_index;
7058             int64_t sector_num;
7059
7060             ram_decompress_buf(s, buf + 1, 9);
7061             bs_index = buf[1];
7062             sector_num = be64_to_cpupu((const uint64_t *)(buf + 2));
7063             if (bs_index >= nb_drives) {
7064                 fprintf(stderr, "Invalid block device index %d\n", bs_index);
7065                 goto error;
7066             }
7067             if (bdrv_read(drives_table[bs_index].bdrv, sector_num,
7068                           phys_ram_base + i,
7069                           BDRV_HASH_BLOCK_SIZE / 512) < 0) {
7070                 fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n",
7071                         bs_index, sector_num);
7072                 goto error;
7073             }
7074         } else
7075 #endif
7076         {
7077         error:
7078             printf("Error block header\n");
7079             return -EINVAL;
7080         }
7081     }
7082     ram_decompress_close(s);
7083     return 0;
7084 }
7085
7086 /***********************************************************/
7087 /* bottom halves (can be seen as timers which expire ASAP) */
7088
7089 struct QEMUBH {
7090     QEMUBHFunc *cb;
7091     void *opaque;
7092     int scheduled;
7093     QEMUBH *next;
7094 };
7095
7096 static QEMUBH *first_bh = NULL;
7097
7098 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
7099 {
7100     QEMUBH *bh;
7101     bh = qemu_mallocz(sizeof(QEMUBH));
7102     if (!bh)
7103         return NULL;
7104     bh->cb = cb;
7105     bh->opaque = opaque;
7106     return bh;
7107 }
7108
7109 int qemu_bh_poll(void)
7110 {
7111     QEMUBH *bh, **pbh;
7112     int ret;
7113
7114     ret = 0;
7115     for(;;) {
7116         pbh = &first_bh;
7117         bh = *pbh;
7118         if (!bh)
7119             break;
7120         ret = 1;
7121         *pbh = bh->next;
7122         bh->scheduled = 0;
7123         bh->cb(bh->opaque);
7124     }
7125     return ret;
7126 }
7127
7128 void qemu_bh_schedule(QEMUBH *bh)
7129 {
7130     CPUState *env = cpu_single_env;
7131     if (bh->scheduled)
7132         return;
7133     bh->scheduled = 1;
7134     bh->next = first_bh;
7135     first_bh = bh;
7136
7137     /* stop the currently executing CPU to execute the BH ASAP */
7138     if (env) {
7139         cpu_interrupt(env, CPU_INTERRUPT_EXIT);
7140     }
7141 }
7142
7143 void qemu_bh_cancel(QEMUBH *bh)
7144 {
7145     QEMUBH **pbh;
7146     if (bh->scheduled) {
7147         pbh = &first_bh;
7148         while (*pbh != bh)
7149             pbh = &(*pbh)->next;
7150         *pbh = bh->next;
7151         bh->scheduled = 0;
7152     }
7153 }
7154
7155 void qemu_bh_delete(QEMUBH *bh)
7156 {
7157     qemu_bh_cancel(bh);
7158     qemu_free(bh);
7159 }
7160
7161 /***********************************************************/
7162 /* machine registration */
7163
7164 QEMUMachine *first_machine = NULL;
7165
7166 int qemu_register_machine(QEMUMachine *m)
7167 {
7168     QEMUMachine **pm;
7169     pm = &first_machine;
7170     while (*pm != NULL)
7171         pm = &(*pm)->next;
7172     m->next = NULL;
7173     *pm = m;
7174     return 0;
7175 }
7176
7177 static QEMUMachine *find_machine(const char *name)
7178 {
7179     QEMUMachine *m;
7180
7181     for(m = first_machine; m != NULL; m = m->next) {
7182         if (!strcmp(m->name, name))
7183             return m;
7184     }
7185     return NULL;
7186 }
7187
7188 /***********************************************************/
7189 /* main execution loop */
7190
7191 static void gui_update(void *opaque)
7192 {
7193     DisplayState *ds = opaque;
7194     ds->dpy_refresh(ds);
7195     qemu_mod_timer(ds->gui_timer,
7196         (ds->gui_timer_interval ?
7197             ds->gui_timer_interval :
7198             GUI_REFRESH_INTERVAL)
7199         + qemu_get_clock(rt_clock));
7200 }
7201
7202 struct vm_change_state_entry {
7203     VMChangeStateHandler *cb;
7204     void *opaque;
7205     LIST_ENTRY (vm_change_state_entry) entries;
7206 };
7207
7208 static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
7209
7210 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
7211                                                      void *opaque)
7212 {
7213     VMChangeStateEntry *e;
7214
7215     e = qemu_mallocz(sizeof (*e));
7216     if (!e)
7217         return NULL;
7218
7219     e->cb = cb;
7220     e->opaque = opaque;
7221     LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
7222     return e;
7223 }
7224
7225 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
7226 {
7227     LIST_REMOVE (e, entries);
7228     qemu_free (e);
7229 }
7230
7231 static void vm_state_notify(int running)
7232 {
7233     VMChangeStateEntry *e;
7234
7235     for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
7236         e->cb(e->opaque, running);
7237     }
7238 }
7239
7240 /* XXX: support several handlers */
7241 static VMStopHandler *vm_stop_cb;
7242 static void *vm_stop_opaque;
7243
7244 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
7245 {
7246     vm_stop_cb = cb;
7247     vm_stop_opaque = opaque;
7248     return 0;
7249 }
7250
7251 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
7252 {
7253     vm_stop_cb = NULL;
7254 }
7255
7256 void vm_start(void)
7257 {
7258     if (!vm_running) {
7259         cpu_enable_ticks();
7260         vm_running = 1;
7261         vm_state_notify(1);
7262         qemu_rearm_alarm_timer(alarm_timer);
7263     }
7264 }
7265
7266 void vm_stop(int reason)
7267 {
7268     if (vm_running) {
7269         cpu_disable_ticks();
7270         vm_running = 0;
7271         if (reason != 0) {
7272             if (vm_stop_cb) {
7273                 vm_stop_cb(vm_stop_opaque, reason);
7274             }
7275         }
7276         vm_state_notify(0);
7277     }
7278 }
7279
7280 /* reset/shutdown handler */
7281
7282 typedef struct QEMUResetEntry {
7283     QEMUResetHandler *func;
7284     void *opaque;
7285     struct QEMUResetEntry *next;
7286 } QEMUResetEntry;
7287
7288 static QEMUResetEntry *first_reset_entry;
7289 static int reset_requested;
7290 static int shutdown_requested;
7291 static int powerdown_requested;
7292
7293 int qemu_shutdown_requested(void)
7294 {
7295     int r = shutdown_requested;
7296     shutdown_requested = 0;
7297     return r;
7298 }
7299
7300 int qemu_reset_requested(void)
7301 {
7302     int r = reset_requested;
7303     reset_requested = 0;
7304     return r;
7305 }
7306
7307 int qemu_powerdown_requested(void)
7308 {
7309     int r = powerdown_requested;
7310     powerdown_requested = 0;
7311     return r;
7312 }
7313
7314 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
7315 {
7316     QEMUResetEntry **pre, *re;
7317
7318     pre = &first_reset_entry;
7319     while (*pre != NULL)
7320         pre = &(*pre)->next;
7321     re = qemu_mallocz(sizeof(QEMUResetEntry));
7322     re->func = func;
7323     re->opaque = opaque;
7324     re->next = NULL;
7325     *pre = re;
7326 }
7327
7328 void qemu_system_reset(void)
7329 {
7330     QEMUResetEntry *re;
7331
7332     /* reset all devices */
7333     for(re = first_reset_entry; re != NULL; re = re->next) {
7334         re->func(re->opaque);
7335     }
7336 }
7337
7338 void qemu_system_reset_request(void)
7339 {
7340     if (no_reboot) {
7341         shutdown_requested = 1;
7342     } else {
7343         reset_requested = 1;
7344     }
7345     if (cpu_single_env)
7346         cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
7347 }
7348
7349 void qemu_system_shutdown_request(void)
7350 {
7351     shutdown_requested = 1;
7352     if (cpu_single_env)
7353         cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
7354 }
7355
7356 void qemu_system_powerdown_request(void)
7357 {
7358     powerdown_requested = 1;
7359     if (cpu_single_env)
7360         cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
7361 }
7362
7363 void main_loop_wait(int timeout)
7364 {
7365     IOHandlerRecord *ioh;
7366     fd_set rfds, wfds, xfds;
7367     int ret, nfds;
7368 #ifdef _WIN32
7369     int ret2, i;
7370 #endif
7371     struct timeval tv;
7372     PollingEntry *pe;
7373
7374
7375     /* XXX: need to suppress polling by better using win32 events */
7376     ret = 0;
7377     for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
7378         ret |= pe->func(pe->opaque);
7379     }
7380 #ifdef _WIN32
7381     if (ret == 0) {
7382         int err;
7383         WaitObjects *w = &wait_objects;
7384
7385         ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout);
7386         if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
7387             if (w->func[ret - WAIT_OBJECT_0])
7388                 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
7389
7390             /* Check for additional signaled events */
7391             for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
7392
7393                 /* Check if event is signaled */
7394                 ret2 = WaitForSingleObject(w->events[i], 0);
7395                 if(ret2 == WAIT_OBJECT_0) {
7396                     if (w->func[i])
7397                         w->func[i](w->opaque[i]);
7398                 } else if (ret2 == WAIT_TIMEOUT) {
7399                 } else {
7400                     err = GetLastError();
7401                     fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
7402                 }
7403             }
7404         } else if (ret == WAIT_TIMEOUT) {
7405         } else {
7406             err = GetLastError();
7407             fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
7408         }
7409     }
7410 #endif
7411     /* poll any events */
7412     /* XXX: separate device handlers from system ones */
7413     nfds = -1;
7414     FD_ZERO(&rfds);
7415     FD_ZERO(&wfds);
7416     FD_ZERO(&xfds);
7417     for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
7418         if (ioh->deleted)
7419             continue;
7420         if (ioh->fd_read &&
7421             (!ioh->fd_read_poll ||
7422              ioh->fd_read_poll(ioh->opaque) != 0)) {
7423             FD_SET(ioh->fd, &rfds);
7424             if (ioh->fd > nfds)
7425                 nfds = ioh->fd;
7426         }
7427         if (ioh->fd_write) {
7428             FD_SET(ioh->fd, &wfds);
7429             if (ioh->fd > nfds)
7430                 nfds = ioh->fd;
7431         }
7432     }
7433
7434     tv.tv_sec = 0;
7435 #ifdef _WIN32
7436     tv.tv_usec = 0;
7437 #else
7438     tv.tv_usec = timeout * 1000;
7439 #endif
7440 #if defined(CONFIG_SLIRP)
7441     if (slirp_inited) {
7442         slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
7443     }
7444 #endif
7445     ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
7446     if (ret > 0) {
7447         IOHandlerRecord **pioh;
7448
7449         for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
7450             if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
7451                 ioh->fd_read(ioh->opaque);
7452             }
7453             if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
7454                 ioh->fd_write(ioh->opaque);
7455             }
7456         }
7457
7458         /* remove deleted IO handlers */
7459         pioh = &first_io_handler;
7460         while (*pioh) {
7461             ioh = *pioh;
7462             if (ioh->deleted) {
7463                 *pioh = ioh->next;
7464                 qemu_free(ioh);
7465             } else
7466                 pioh = &ioh->next;
7467         }
7468     }
7469 #if defined(CONFIG_SLIRP)
7470     if (slirp_inited) {
7471         if (ret < 0) {
7472             FD_ZERO(&rfds);
7473             FD_ZERO(&wfds);
7474             FD_ZERO(&xfds);
7475         }
7476         slirp_select_poll(&rfds, &wfds, &xfds);
7477     }
7478 #endif
7479
7480     if (vm_running) {
7481         if (likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
7482         qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
7483                         qemu_get_clock(vm_clock));
7484         /* run dma transfers, if any */
7485         DMA_run();
7486     }
7487
7488     /* real time timers */
7489     qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
7490                     qemu_get_clock(rt_clock));
7491
7492     if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
7493         alarm_timer->flags &= ~(ALARM_FLAG_EXPIRED);
7494         qemu_rearm_alarm_timer(alarm_timer);
7495     }
7496
7497     /* Check bottom-halves last in case any of the earlier events triggered
7498        them.  */
7499     qemu_bh_poll();
7500
7501 }
7502
7503 static int main_loop(void)
7504 {
7505     int ret, timeout;
7506 #ifdef CONFIG_PROFILER
7507     int64_t ti;
7508 #endif
7509     CPUState *env;
7510
7511     cur_cpu = first_cpu;
7512     next_cpu = cur_cpu->next_cpu ?: first_cpu;
7513     for(;;) {
7514         if (vm_running) {
7515
7516             for(;;) {
7517                 /* get next cpu */
7518                 env = next_cpu;
7519 #ifdef CONFIG_PROFILER
7520                 ti = profile_getclock();
7521 #endif
7522                 if (use_icount) {
7523                     int64_t count;
7524                     int decr;
7525                     qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
7526                     env->icount_decr.u16.low = 0;
7527                     env->icount_extra = 0;
7528                     count = qemu_next_deadline();
7529                     count = (count + (1 << icount_time_shift) - 1)
7530                             >> icount_time_shift;
7531                     qemu_icount += count;
7532                     decr = (count > 0xffff) ? 0xffff : count;
7533                     count -= decr;
7534                     env->icount_decr.u16.low = decr;
7535                     env->icount_extra = count;
7536                 }
7537                 ret = cpu_exec(env);
7538 #ifdef CONFIG_PROFILER
7539                 qemu_time += profile_getclock() - ti;
7540 #endif
7541                 if (use_icount) {
7542                     /* Fold pending instructions back into the
7543                        instruction counter, and clear the interrupt flag.  */
7544                     qemu_icount -= (env->icount_decr.u16.low
7545                                     + env->icount_extra);
7546                     env->icount_decr.u32 = 0;
7547                     env->icount_extra = 0;
7548                 }
7549                 next_cpu = env->next_cpu ?: first_cpu;
7550                 if (event_pending && likely(ret != EXCP_DEBUG)) {
7551                     ret = EXCP_INTERRUPT;
7552                     event_pending = 0;
7553                     break;
7554                 }
7555                 if (ret == EXCP_HLT) {
7556                     /* Give the next CPU a chance to run.  */
7557                     cur_cpu = env;
7558                     continue;
7559                 }
7560                 if (ret != EXCP_HALTED)
7561                     break;
7562                 /* all CPUs are halted ? */
7563                 if (env == cur_cpu)
7564                     break;
7565             }
7566             cur_cpu = env;
7567
7568             if (shutdown_requested) {
7569                 ret = EXCP_INTERRUPT;
7570                 if (no_shutdown) {
7571                     vm_stop(0);
7572                     no_shutdown = 0;
7573                 }
7574                 else
7575                     break;
7576             }
7577             if (reset_requested) {
7578                 reset_requested = 0;
7579                 qemu_system_reset();
7580                 ret = EXCP_INTERRUPT;
7581             }
7582             if (powerdown_requested) {
7583                 powerdown_requested = 0;
7584                 qemu_system_powerdown();
7585                 ret = EXCP_INTERRUPT;
7586             }
7587             if (unlikely(ret == EXCP_DEBUG)) {
7588                 vm_stop(EXCP_DEBUG);
7589             }
7590             /* If all cpus are halted then wait until the next IRQ */
7591             /* XXX: use timeout computed from timers */
7592             if (ret == EXCP_HALTED) {
7593                 if (use_icount) {
7594                     int64_t add;
7595                     int64_t delta;
7596                     /* Advance virtual time to the next event.  */
7597                     if (use_icount == 1) {
7598                         /* When not using an adaptive execution frequency
7599                            we tend to get badly out of sync with real time,
7600                            so just delay for a reasonable amount of time.  */
7601                         delta = 0;
7602                     } else {
7603                         delta = cpu_get_icount() - cpu_get_clock();
7604                     }
7605                     if (delta > 0) {
7606                         /* If virtual time is ahead of real time then just
7607                            wait for IO.  */
7608                         timeout = (delta / 1000000) + 1;
7609                     } else {
7610                         /* Wait for either IO to occur or the next
7611                            timer event.  */
7612                         add = qemu_next_deadline();
7613                         /* We advance the timer before checking for IO.
7614                            Limit the amount we advance so that early IO
7615                            activity won't get the guest too far ahead.  */
7616                         if (add > 10000000)
7617                             add = 10000000;
7618                         delta += add;
7619                         add = (add + (1 << icount_time_shift) - 1)
7620                               >> icount_time_shift;
7621                         qemu_icount += add;
7622                         timeout = delta / 1000000;
7623                         if (timeout < 0)
7624                             timeout = 0;
7625                     }
7626                 } else {
7627                     timeout = 10;
7628                 }
7629             } else {
7630                 timeout = 0;
7631             }
7632         } else {
7633             if (shutdown_requested)
7634                 break;
7635             timeout = 10;
7636         }
7637 #ifdef CONFIG_PROFILER
7638         ti = profile_getclock();
7639 #endif
7640         main_loop_wait(timeout);
7641 #ifdef CONFIG_PROFILER
7642         dev_time += profile_getclock() - ti;
7643 #endif
7644     }
7645     cpu_disable_ticks();
7646     return ret;
7647 }
7648
7649 static void help(int exitcode)
7650 {
7651     printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
7652            "usage: %s [options] [disk_image]\n"
7653            "\n"
7654            "'disk_image' is a raw hard image image for IDE hard disk 0\n"
7655            "\n"
7656            "Standard options:\n"
7657            "-M machine      select emulated machine (-M ? for list)\n"
7658            "-cpu cpu        select CPU (-cpu ? for list)\n"
7659            "-fda/-fdb file  use 'file' as floppy disk 0/1 image\n"
7660            "-hda/-hdb file  use 'file' as IDE hard disk 0/1 image\n"
7661            "-hdc/-hdd file  use 'file' as IDE hard disk 2/3 image\n"
7662            "-cdrom file     use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
7663            "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
7664            "       [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
7665            "       [,cache=on|off][,format=f]\n"
7666            "                use 'file' as a drive image\n"
7667            "-mtdblock file  use 'file' as on-board Flash memory image\n"
7668            "-sd file        use 'file' as SecureDigital card image\n"
7669            "-pflash file    use 'file' as a parallel flash image\n"
7670            "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
7671            "-snapshot       write to temporary files instead of disk image files\n"
7672 #ifdef CONFIG_SDL
7673            "-no-frame       open SDL window without a frame and window decorations\n"
7674            "-alt-grab       use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
7675            "-no-quit        disable SDL window close capability\n"
7676 #endif
7677 #ifdef TARGET_I386
7678            "-no-fd-bootchk  disable boot signature checking for floppy disks\n"
7679 #endif
7680            "-m megs         set virtual RAM size to megs MB [default=%d]\n"
7681            "-smp n          set the number of CPUs to 'n' [default=1]\n"
7682            "-nographic      disable graphical output and redirect serial I/Os to console\n"
7683            "-portrait       rotate graphical output 90 deg left (only PXA LCD)\n"
7684 #ifndef _WIN32
7685            "-k language     use keyboard layout (for example \"fr\" for French)\n"
7686 #endif
7687 #ifdef HAS_AUDIO
7688            "-audio-help     print list of audio drivers and their options\n"
7689            "-soundhw c1,... enable audio support\n"
7690            "                and only specified sound cards (comma separated list)\n"
7691            "                use -soundhw ? to get the list of supported cards\n"
7692            "                use -soundhw all to enable all of them\n"
7693 #endif
7694            "-localtime      set the real time clock to local time [default=utc]\n"
7695            "-full-screen    start in full screen\n"
7696 #ifdef TARGET_I386
7697            "-win2k-hack     use it when installing Windows 2000 to avoid a disk full bug\n"
7698 #endif
7699            "-usb            enable the USB driver (will be the default soon)\n"
7700            "-usbdevice name add the host or guest USB device 'name'\n"
7701 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7702            "-g WxH[xDEPTH]  Set the initial graphical resolution and depth\n"
7703 #endif
7704            "-name string    set the name of the guest\n"
7705            "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
7706            "\n"
7707            "Network options:\n"
7708            "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
7709            "                create a new Network Interface Card and connect it to VLAN 'n'\n"
7710 #ifdef CONFIG_SLIRP
7711            "-net user[,vlan=n][,hostname=host]\n"
7712            "                connect the user mode network stack to VLAN 'n' and send\n"
7713            "                hostname 'host' to DHCP clients\n"
7714 #endif
7715 #ifdef _WIN32
7716            "-net tap[,vlan=n],ifname=name\n"
7717            "                connect the host TAP network interface to VLAN 'n'\n"
7718 #else
7719            "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
7720            "                connect the host TAP network interface to VLAN 'n' and use the\n"
7721            "                network scripts 'file' (default=%s)\n"
7722            "                and 'dfile' (default=%s);\n"
7723            "                use '[down]script=no' to disable script execution;\n"
7724            "                use 'fd=h' to connect to an already opened TAP interface\n"
7725 #endif
7726            "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
7727            "                connect the vlan 'n' to another VLAN using a socket connection\n"
7728            "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
7729            "                connect the vlan 'n' to multicast maddr and port\n"
7730 #ifdef CONFIG_VDE
7731            "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
7732            "                connect the vlan 'n' to port 'n' of a vde switch running\n"
7733            "                on host and listening for incoming connections on 'socketpath'.\n"
7734            "                Use group 'groupname' and mode 'octalmode' to change default\n"
7735            "                ownership and permissions for communication port.\n"
7736 #endif
7737            "-net none       use it alone to have zero network devices; if no -net option\n"
7738            "                is provided, the default is '-net nic -net user'\n"
7739            "\n"
7740 #ifdef CONFIG_SLIRP
7741            "-tftp dir       allow tftp access to files in dir [-net user]\n"
7742            "-bootp file     advertise file in BOOTP replies\n"
7743 #ifndef _WIN32
7744            "-smb dir        allow SMB access to files in 'dir' [-net user]\n"
7745 #endif
7746            "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
7747            "                redirect TCP or UDP connections from host to guest [-net user]\n"
7748 #endif
7749            "\n"
7750            "Linux boot specific:\n"
7751            "-kernel bzImage use 'bzImage' as kernel image\n"
7752            "-append cmdline use 'cmdline' as kernel command line\n"
7753            "-initrd file    use 'file' as initial ram disk\n"
7754            "\n"
7755            "Debug/Expert options:\n"
7756            "-monitor dev    redirect the monitor to char device 'dev'\n"
7757            "-serial dev     redirect the serial port to char device 'dev'\n"
7758            "-parallel dev   redirect the parallel port to char device 'dev'\n"
7759            "-pidfile file   Write PID to 'file'\n"
7760            "-S              freeze CPU at startup (use 'c' to start execution)\n"
7761            "-s              wait gdb connection to port\n"
7762            "-p port         set gdb connection port [default=%s]\n"
7763            "-d item1,...    output log to %s (use -d ? for a list of log items)\n"
7764            "-hdachs c,h,s[,t]  force hard disk 0 physical geometry and the optional BIOS\n"
7765            "                translation (t=none or lba) (usually qemu can guess them)\n"
7766            "-L path         set the directory for the BIOS, VGA BIOS and keymaps\n"
7767 #ifdef USE_KQEMU
7768            "-kernel-kqemu   enable KQEMU full virtualization (default is user mode only)\n"
7769            "-no-kqemu       disable KQEMU kernel module usage\n"
7770 #endif
7771 #ifdef TARGET_I386
7772            "-std-vga        simulate a standard VGA card with VESA Bochs Extensions\n"
7773            "                (default is CL-GD5446 PCI VGA)\n"
7774            "-no-acpi        disable ACPI\n"
7775 #endif
7776 #ifdef CONFIG_CURSES
7777            "-curses         use a curses/ncurses interface instead of SDL\n"
7778 #endif
7779            "-no-reboot      exit instead of rebooting\n"
7780            "-no-shutdown    stop before shutdown\n"
7781            "-loadvm [tag|id]  start right away with a saved state (loadvm in monitor)\n"
7782            "-vnc display    start a VNC server on display\n"
7783 #ifndef _WIN32
7784            "-daemonize      daemonize QEMU after initializing\n"
7785 #endif
7786            "-option-rom rom load a file, rom, into the option ROM space\n"
7787 #ifdef TARGET_SPARC
7788            "-prom-env variable=value  set OpenBIOS nvram variables\n"
7789 #endif
7790            "-clock          force the use of the given methods for timer alarm.\n"
7791            "                To see what timers are available use -clock ?\n"
7792            "-startdate      select initial date of the clock\n"
7793            "-icount [N|auto]\n"
7794            "                Enable virtual instruction counter with 2^N clock ticks per instruction\n"
7795            "\n"
7796            "During emulation, the following keys are useful:\n"
7797            "ctrl-alt-f      toggle full screen\n"
7798            "ctrl-alt-n      switch to virtual console 'n'\n"
7799            "ctrl-alt        toggle mouse and keyboard grab\n"
7800            "\n"
7801            "When using -nographic, press 'ctrl-a h' to get some help.\n"
7802            ,
7803            "qemu",
7804            DEFAULT_RAM_SIZE,
7805 #ifndef _WIN32
7806            DEFAULT_NETWORK_SCRIPT,
7807            DEFAULT_NETWORK_DOWN_SCRIPT,
7808 #endif
7809            DEFAULT_GDBSTUB_PORT,
7810            "/tmp/qemu.log");
7811     exit(exitcode);
7812 }
7813
7814 #define HAS_ARG 0x0001
7815
7816 enum {
7817     QEMU_OPTION_h,
7818
7819     QEMU_OPTION_M,
7820     QEMU_OPTION_cpu,
7821     QEMU_OPTION_fda,
7822     QEMU_OPTION_fdb,
7823     QEMU_OPTION_hda,
7824     QEMU_OPTION_hdb,
7825     QEMU_OPTION_hdc,
7826     QEMU_OPTION_hdd,
7827     QEMU_OPTION_drive,
7828     QEMU_OPTION_cdrom,
7829     QEMU_OPTION_mtdblock,
7830     QEMU_OPTION_sd,
7831     QEMU_OPTION_pflash,
7832     QEMU_OPTION_boot,
7833     QEMU_OPTION_snapshot,
7834 #ifdef TARGET_I386
7835     QEMU_OPTION_no_fd_bootchk,
7836 #endif
7837     QEMU_OPTION_m,
7838     QEMU_OPTION_nographic,
7839     QEMU_OPTION_portrait,
7840 #ifdef HAS_AUDIO
7841     QEMU_OPTION_audio_help,
7842     QEMU_OPTION_soundhw,
7843 #endif
7844
7845     QEMU_OPTION_net,
7846     QEMU_OPTION_tftp,
7847     QEMU_OPTION_bootp,
7848     QEMU_OPTION_smb,
7849     QEMU_OPTION_redir,
7850
7851     QEMU_OPTION_kernel,
7852     QEMU_OPTION_append,
7853     QEMU_OPTION_initrd,
7854
7855     QEMU_OPTION_S,
7856     QEMU_OPTION_s,
7857     QEMU_OPTION_p,
7858     QEMU_OPTION_d,
7859     QEMU_OPTION_hdachs,
7860     QEMU_OPTION_L,
7861     QEMU_OPTION_bios,
7862     QEMU_OPTION_k,
7863     QEMU_OPTION_localtime,
7864     QEMU_OPTION_cirrusvga,
7865     QEMU_OPTION_vmsvga,
7866     QEMU_OPTION_g,
7867     QEMU_OPTION_std_vga,
7868     QEMU_OPTION_echr,
7869     QEMU_OPTION_monitor,
7870     QEMU_OPTION_serial,
7871     QEMU_OPTION_parallel,
7872     QEMU_OPTION_loadvm,
7873     QEMU_OPTION_full_screen,
7874     QEMU_OPTION_no_frame,
7875     QEMU_OPTION_alt_grab,
7876     QEMU_OPTION_no_quit,
7877     QEMU_OPTION_pidfile,
7878     QEMU_OPTION_no_kqemu,
7879     QEMU_OPTION_kernel_kqemu,
7880     QEMU_OPTION_win2k_hack,
7881     QEMU_OPTION_usb,
7882     QEMU_OPTION_usbdevice,
7883     QEMU_OPTION_smp,
7884     QEMU_OPTION_vnc,
7885     QEMU_OPTION_no_acpi,
7886     QEMU_OPTION_curses,
7887     QEMU_OPTION_no_reboot,
7888     QEMU_OPTION_no_shutdown,
7889     QEMU_OPTION_show_cursor,
7890     QEMU_OPTION_daemonize,
7891     QEMU_OPTION_option_rom,
7892     QEMU_OPTION_semihosting,
7893     QEMU_OPTION_name,
7894     QEMU_OPTION_prom_env,
7895     QEMU_OPTION_old_param,
7896     QEMU_OPTION_clock,
7897     QEMU_OPTION_startdate,
7898     QEMU_OPTION_tb_size,
7899     QEMU_OPTION_icount,
7900     QEMU_OPTION_uuid,
7901 };
7902
7903 typedef struct QEMUOption {
7904     const char *name;
7905     int flags;
7906     int index;
7907 } QEMUOption;
7908
7909 const QEMUOption qemu_options[] = {
7910     { "h", 0, QEMU_OPTION_h },
7911     { "help", 0, QEMU_OPTION_h },
7912
7913     { "M", HAS_ARG, QEMU_OPTION_M },
7914     { "cpu", HAS_ARG, QEMU_OPTION_cpu },
7915     { "fda", HAS_ARG, QEMU_OPTION_fda },
7916     { "fdb", HAS_ARG, QEMU_OPTION_fdb },
7917     { "hda", HAS_ARG, QEMU_OPTION_hda },
7918     { "hdb", HAS_ARG, QEMU_OPTION_hdb },
7919     { "hdc", HAS_ARG, QEMU_OPTION_hdc },
7920     { "hdd", HAS_ARG, QEMU_OPTION_hdd },
7921     { "drive", HAS_ARG, QEMU_OPTION_drive },
7922     { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
7923     { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
7924     { "sd", HAS_ARG, QEMU_OPTION_sd },
7925     { "pflash", HAS_ARG, QEMU_OPTION_pflash },
7926     { "boot", HAS_ARG, QEMU_OPTION_boot },
7927     { "snapshot", 0, QEMU_OPTION_snapshot },
7928 #ifdef TARGET_I386
7929     { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
7930 #endif
7931     { "m", HAS_ARG, QEMU_OPTION_m },
7932     { "nographic", 0, QEMU_OPTION_nographic },
7933     { "portrait", 0, QEMU_OPTION_portrait },
7934     { "k", HAS_ARG, QEMU_OPTION_k },
7935 #ifdef HAS_AUDIO
7936     { "audio-help", 0, QEMU_OPTION_audio_help },
7937     { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
7938 #endif
7939
7940     { "net", HAS_ARG, QEMU_OPTION_net},
7941 #ifdef CONFIG_SLIRP
7942     { "tftp", HAS_ARG, QEMU_OPTION_tftp },
7943     { "bootp", HAS_ARG, QEMU_OPTION_bootp },
7944 #ifndef _WIN32
7945     { "smb", HAS_ARG, QEMU_OPTION_smb },
7946 #endif
7947     { "redir", HAS_ARG, QEMU_OPTION_redir },
7948 #endif
7949
7950     { "kernel", HAS_ARG, QEMU_OPTION_kernel },
7951     { "append", HAS_ARG, QEMU_OPTION_append },
7952     { "initrd", HAS_ARG, QEMU_OPTION_initrd },
7953
7954     { "S", 0, QEMU_OPTION_S },
7955     { "s", 0, QEMU_OPTION_s },
7956     { "p", HAS_ARG, QEMU_OPTION_p },
7957     { "d", HAS_ARG, QEMU_OPTION_d },
7958     { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
7959     { "L", HAS_ARG, QEMU_OPTION_L },
7960     { "bios", HAS_ARG, QEMU_OPTION_bios },
7961 #ifdef USE_KQEMU
7962     { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
7963     { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
7964 #endif
7965 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7966     { "g", 1, QEMU_OPTION_g },
7967 #endif
7968     { "localtime", 0, QEMU_OPTION_localtime },
7969     { "std-vga", 0, QEMU_OPTION_std_vga },
7970     { "echr", HAS_ARG, QEMU_OPTION_echr },
7971     { "monitor", HAS_ARG, QEMU_OPTION_monitor },
7972     { "serial", HAS_ARG, QEMU_OPTION_serial },
7973     { "parallel", HAS_ARG, QEMU_OPTION_parallel },
7974     { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
7975     { "full-screen", 0, QEMU_OPTION_full_screen },
7976 #ifdef CONFIG_SDL
7977     { "no-frame", 0, QEMU_OPTION_no_frame },
7978     { "alt-grab", 0, QEMU_OPTION_alt_grab },
7979     { "no-quit", 0, QEMU_OPTION_no_quit },
7980 #endif
7981     { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
7982     { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
7983     { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
7984     { "smp", HAS_ARG, QEMU_OPTION_smp },
7985     { "vnc", HAS_ARG, QEMU_OPTION_vnc },
7986 #ifdef CONFIG_CURSES
7987     { "curses", 0, QEMU_OPTION_curses },
7988 #endif
7989     { "uuid", HAS_ARG, QEMU_OPTION_uuid },
7990
7991     /* temporary options */
7992     { "usb", 0, QEMU_OPTION_usb },
7993     { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
7994     { "vmwarevga", 0, QEMU_OPTION_vmsvga },
7995     { "no-acpi", 0, QEMU_OPTION_no_acpi },
7996     { "no-reboot", 0, QEMU_OPTION_no_reboot },
7997     { "no-shutdown", 0, QEMU_OPTION_no_shutdown },
7998     { "show-cursor", 0, QEMU_OPTION_show_cursor },
7999     { "daemonize", 0, QEMU_OPTION_daemonize },
8000     { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
8001 #if defined(TARGET_ARM) || defined(TARGET_M68K)
8002     { "semihosting", 0, QEMU_OPTION_semihosting },
8003 #endif
8004     { "name", HAS_ARG, QEMU_OPTION_name },
8005 #if defined(TARGET_SPARC)
8006     { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
8007 #endif
8008 #if defined(TARGET_ARM)
8009     { "old-param", 0, QEMU_OPTION_old_param },
8010 #endif
8011     { "clock", HAS_ARG, QEMU_OPTION_clock },
8012     { "startdate", HAS_ARG, QEMU_OPTION_startdate },
8013     { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
8014     { "icount", HAS_ARG, QEMU_OPTION_icount },
8015     { NULL },
8016 };
8017
8018 /* password input */
8019
8020 int qemu_key_check(BlockDriverState *bs, const char *name)
8021 {
8022     char password[256];
8023     int i;
8024
8025     if (!bdrv_is_encrypted(bs))
8026         return 0;
8027
8028     term_printf("%s is encrypted.\n", name);
8029     for(i = 0; i < 3; i++) {
8030         monitor_readline("Password: ", 1, password, sizeof(password));
8031         if (bdrv_set_key(bs, password) == 0)
8032             return 0;
8033         term_printf("invalid password\n");
8034     }
8035     return -EPERM;
8036 }
8037
8038 static BlockDriverState *get_bdrv(int index)
8039 {
8040     if (index > nb_drives)
8041         return NULL;
8042     return drives_table[index].bdrv;
8043 }
8044
8045 static void read_passwords(void)
8046 {
8047     BlockDriverState *bs;
8048     int i;
8049
8050     for(i = 0; i < 6; i++) {
8051         bs = get_bdrv(i);
8052         if (bs)
8053             qemu_key_check(bs, bdrv_get_device_name(bs));
8054     }
8055 }
8056
8057 #ifdef HAS_AUDIO
8058 struct soundhw soundhw[] = {
8059 #ifdef HAS_AUDIO_CHOICE
8060 #if defined(TARGET_I386) || defined(TARGET_MIPS)
8061     {
8062         "pcspk",
8063         "PC speaker",
8064         0,
8065         1,
8066         { .init_isa = pcspk_audio_init }
8067     },
8068 #endif
8069     {
8070         "sb16",
8071         "Creative Sound Blaster 16",
8072         0,
8073         1,
8074         { .init_isa = SB16_init }
8075     },
8076
8077 #ifdef CONFIG_CS4231A
8078     {
8079         "cs4231a",
8080         "CS4231A",
8081         0,
8082         1,
8083         { .init_isa = cs4231a_init }
8084     },
8085 #endif
8086
8087 #ifdef CONFIG_ADLIB
8088     {
8089         "adlib",
8090 #ifdef HAS_YMF262
8091         "Yamaha YMF262 (OPL3)",
8092 #else
8093         "Yamaha YM3812 (OPL2)",
8094 #endif
8095         0,
8096         1,
8097         { .init_isa = Adlib_init }
8098     },
8099 #endif
8100
8101 #ifdef CONFIG_GUS
8102     {
8103         "gus",
8104         "Gravis Ultrasound GF1",
8105         0,
8106         1,
8107         { .init_isa = GUS_init }
8108     },
8109 #endif
8110
8111 #ifdef CONFIG_AC97
8112     {
8113         "ac97",
8114         "Intel 82801AA AC97 Audio",
8115         0,
8116         0,
8117         { .init_pci = ac97_init }
8118     },
8119 #endif
8120
8121     {
8122         "es1370",
8123         "ENSONIQ AudioPCI ES1370",
8124         0,
8125         0,
8126         { .init_pci = es1370_init }
8127     },
8128 #endif
8129
8130     { NULL, NULL, 0, 0, { NULL } }
8131 };
8132
8133 static void select_soundhw (const char *optarg)
8134 {
8135     struct soundhw *c;
8136
8137     if (*optarg == '?') {
8138     show_valid_cards:
8139
8140         printf ("Valid sound card names (comma separated):\n");
8141         for (c = soundhw; c->name; ++c) {
8142             printf ("%-11s %s\n", c->name, c->descr);
8143         }
8144         printf ("\n-soundhw all will enable all of the above\n");
8145         exit (*optarg != '?');
8146     }
8147     else {
8148         size_t l;
8149         const char *p;
8150         char *e;
8151         int bad_card = 0;
8152
8153         if (!strcmp (optarg, "all")) {
8154             for (c = soundhw; c->name; ++c) {
8155                 c->enabled = 1;
8156             }
8157             return;
8158         }
8159
8160         p = optarg;
8161         while (*p) {
8162             e = strchr (p, ',');
8163             l = !e ? strlen (p) : (size_t) (e - p);
8164
8165             for (c = soundhw; c->name; ++c) {
8166                 if (!strncmp (c->name, p, l)) {
8167                     c->enabled = 1;
8168                     break;
8169                 }
8170             }
8171
8172             if (!c->name) {
8173                 if (l > 80) {
8174                     fprintf (stderr,
8175                              "Unknown sound card name (too big to show)\n");
8176                 }
8177                 else {
8178                     fprintf (stderr, "Unknown sound card name `%.*s'\n",
8179                              (int) l, p);
8180                 }
8181                 bad_card = 1;
8182             }
8183             p += l + (e != NULL);
8184         }
8185
8186         if (bad_card)
8187             goto show_valid_cards;
8188     }
8189 }
8190 #endif
8191
8192 #ifdef _WIN32
8193 static BOOL WINAPI qemu_ctrl_handler(DWORD type)
8194 {
8195     exit(STATUS_CONTROL_C_EXIT);
8196     return TRUE;
8197 }
8198 #endif
8199
8200 static int qemu_uuid_parse(const char *str, uint8_t *uuid)
8201 {
8202     int ret;
8203
8204     if(strlen(str) != 36)
8205         return -1;
8206
8207     ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
8208             &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
8209             &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
8210
8211     if(ret != 16)
8212         return -1;
8213
8214     return 0;
8215 }
8216
8217 #define MAX_NET_CLIENTS 32
8218
8219 #ifndef _WIN32
8220
8221 static void termsig_handler(int signal)
8222 {
8223     qemu_system_shutdown_request();
8224 }
8225
8226 static void termsig_setup(void)
8227 {
8228     struct sigaction act;
8229
8230     memset(&act, 0, sizeof(act));
8231     act.sa_handler = termsig_handler;
8232     sigaction(SIGINT,  &act, NULL);
8233     sigaction(SIGHUP,  &act, NULL);
8234     sigaction(SIGTERM, &act, NULL);
8235 }
8236
8237 #endif
8238
8239 int main(int argc, char **argv)
8240 {
8241 #ifdef CONFIG_GDBSTUB
8242     int use_gdbstub;
8243     const char *gdbstub_port;
8244 #endif
8245     uint32_t boot_devices_bitmap = 0;
8246     int i;
8247     int snapshot, linux_boot, net_boot;
8248     const char *initrd_filename;
8249     const char *kernel_filename, *kernel_cmdline;
8250     const char *boot_devices = "";
8251     DisplayState *ds = &display_state;
8252     int cyls, heads, secs, translation;
8253     const char *net_clients[MAX_NET_CLIENTS];
8254     int nb_net_clients;
8255     int hda_index;
8256     int optind;
8257     const char *r, *optarg;
8258     CharDriverState *monitor_hd;
8259     const char *monitor_device;
8260     const char *serial_devices[MAX_SERIAL_PORTS];
8261     int serial_device_index;
8262     const char *parallel_devices[MAX_PARALLEL_PORTS];
8263     int parallel_device_index;
8264     const char *loadvm = NULL;
8265     QEMUMachine *machine;
8266     const char *cpu_model;
8267     const char *usb_devices[MAX_USB_CMDLINE];
8268     int usb_devices_index;
8269     int fds[2];
8270     int tb_size;
8271     const char *pid_file = NULL;
8272     VLANState *vlan;
8273
8274     LIST_INIT (&vm_change_state_head);
8275 #ifndef _WIN32
8276     {
8277         struct sigaction act;
8278         sigfillset(&act.sa_mask);
8279         act.sa_flags = 0;
8280         act.sa_handler = SIG_IGN;
8281         sigaction(SIGPIPE, &act, NULL);
8282     }
8283 #else
8284     SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
8285     /* Note: cpu_interrupt() is currently not SMP safe, so we force
8286        QEMU to run on a single CPU */
8287     {
8288         HANDLE h;
8289         DWORD mask, smask;
8290         int i;
8291         h = GetCurrentProcess();
8292         if (GetProcessAffinityMask(h, &mask, &smask)) {
8293             for(i = 0; i < 32; i++) {
8294                 if (mask & (1 << i))
8295                     break;
8296             }
8297             if (i != 32) {
8298                 mask = 1 << i;
8299                 SetProcessAffinityMask(h, mask);
8300             }
8301         }
8302     }
8303 #endif
8304
8305     register_machines();
8306     machine = first_machine;
8307     cpu_model = NULL;
8308     initrd_filename = NULL;
8309     ram_size = 0;
8310     vga_ram_size = VGA_RAM_SIZE;
8311 #ifdef CONFIG_GDBSTUB
8312     use_gdbstub = 0;
8313     gdbstub_port = DEFAULT_GDBSTUB_PORT;
8314 #endif
8315     snapshot = 0;
8316     nographic = 0;
8317     curses = 0;
8318     kernel_filename = NULL;
8319     kernel_cmdline = "";
8320     cyls = heads = secs = 0;
8321     translation = BIOS_ATA_TRANSLATION_AUTO;
8322     monitor_device = "vc";
8323
8324     serial_devices[0] = "vc:80Cx24C";
8325     for(i = 1; i < MAX_SERIAL_PORTS; i++)
8326         serial_devices[i] = NULL;
8327     serial_device_index = 0;
8328
8329     parallel_devices[0] = "vc:640x480";
8330     for(i = 1; i < MAX_PARALLEL_PORTS; i++)
8331         parallel_devices[i] = NULL;
8332     parallel_device_index = 0;
8333
8334     usb_devices_index = 0;
8335
8336     nb_net_clients = 0;
8337     nb_drives = 0;
8338     nb_drives_opt = 0;
8339     hda_index = -1;
8340
8341     nb_nics = 0;
8342
8343     tb_size = 0;
8344     
8345     optind = 1;
8346     for(;;) {
8347         if (optind >= argc)
8348             break;
8349         r = argv[optind];
8350         if (r[0] != '-') {
8351             hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
8352         } else {
8353             const QEMUOption *popt;
8354
8355             optind++;
8356             /* Treat --foo the same as -foo.  */
8357             if (r[1] == '-')
8358                 r++;
8359             popt = qemu_options;
8360             for(;;) {
8361                 if (!popt->name) {
8362                     fprintf(stderr, "%s: invalid option -- '%s'\n",
8363                             argv[0], r);
8364                     exit(1);
8365                 }
8366                 if (!strcmp(popt->name, r + 1))
8367                     break;
8368                 popt++;
8369             }
8370             if (popt->flags & HAS_ARG) {
8371                 if (optind >= argc) {
8372                     fprintf(stderr, "%s: option '%s' requires an argument\n",
8373                             argv[0], r);
8374                     exit(1);
8375                 }
8376                 optarg = argv[optind++];
8377             } else {
8378                 optarg = NULL;
8379             }
8380
8381             switch(popt->index) {
8382             case QEMU_OPTION_M:
8383                 machine = find_machine(optarg);
8384                 if (!machine) {
8385                     QEMUMachine *m;
8386                     printf("Supported machines are:\n");
8387                     for(m = first_machine; m != NULL; m = m->next) {
8388                         printf("%-10s %s%s\n",
8389                                m->name, m->desc,
8390                                m == first_machine ? " (default)" : "");
8391                     }
8392                     exit(*optarg != '?');
8393                 }
8394                 break;
8395             case QEMU_OPTION_cpu:
8396                 /* hw initialization will check this */
8397                 if (*optarg == '?') {
8398 /* XXX: implement xxx_cpu_list for targets that still miss it */
8399 #if defined(cpu_list)
8400                     cpu_list(stdout, &fprintf);
8401 #endif
8402                     exit(0);
8403                 } else {
8404                     cpu_model = optarg;
8405                 }
8406                 break;
8407             case QEMU_OPTION_initrd:
8408                 initrd_filename = optarg;
8409                 break;
8410             case QEMU_OPTION_hda:
8411                 if (cyls == 0)
8412                     hda_index = drive_add(optarg, HD_ALIAS, 0);
8413                 else
8414                     hda_index = drive_add(optarg, HD_ALIAS
8415                              ",cyls=%d,heads=%d,secs=%d%s",
8416                              0, cyls, heads, secs,
8417                              translation == BIOS_ATA_TRANSLATION_LBA ?
8418                                  ",trans=lba" :
8419                              translation == BIOS_ATA_TRANSLATION_NONE ?
8420                                  ",trans=none" : "");
8421                  break;
8422             case QEMU_OPTION_hdb:
8423             case QEMU_OPTION_hdc:
8424             case QEMU_OPTION_hdd:
8425                 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
8426                 break;
8427             case QEMU_OPTION_drive:
8428                 drive_add(NULL, "%s", optarg);
8429                 break;
8430             case QEMU_OPTION_mtdblock:
8431                 drive_add(optarg, MTD_ALIAS);
8432                 break;
8433             case QEMU_OPTION_sd:
8434                 drive_add(optarg, SD_ALIAS);
8435                 break;
8436             case QEMU_OPTION_pflash:
8437                 drive_add(optarg, PFLASH_ALIAS);
8438                 break;
8439             case QEMU_OPTION_snapshot:
8440                 snapshot = 1;
8441                 break;
8442             case QEMU_OPTION_hdachs:
8443                 {
8444                     const char *p;
8445                     p = optarg;
8446                     cyls = strtol(p, (char **)&p, 0);
8447                     if (cyls < 1 || cyls > 16383)
8448                         goto chs_fail;
8449                     if (*p != ',')
8450                         goto chs_fail;
8451                     p++;
8452                     heads = strtol(p, (char **)&p, 0);
8453                     if (heads < 1 || heads > 16)
8454                         goto chs_fail;
8455                     if (*p != ',')
8456                         goto chs_fail;
8457                     p++;
8458                     secs = strtol(p, (char **)&p, 0);
8459                     if (secs < 1 || secs > 63)
8460                         goto chs_fail;
8461                     if (*p == ',') {
8462                         p++;
8463                         if (!strcmp(p, "none"))
8464                             translation = BIOS_ATA_TRANSLATION_NONE;
8465                         else if (!strcmp(p, "lba"))
8466                             translation = BIOS_ATA_TRANSLATION_LBA;
8467                         else if (!strcmp(p, "auto"))
8468                             translation = BIOS_ATA_TRANSLATION_AUTO;
8469                         else
8470                             goto chs_fail;
8471                     } else if (*p != '\0') {
8472                     chs_fail:
8473                         fprintf(stderr, "qemu: invalid physical CHS format\n");
8474                         exit(1);
8475                     }
8476                     if (hda_index != -1)
8477                         snprintf(drives_opt[hda_index].opt,
8478                                  sizeof(drives_opt[hda_index].opt),
8479                                  HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
8480                                  0, cyls, heads, secs,
8481                                  translation == BIOS_ATA_TRANSLATION_LBA ?
8482                                     ",trans=lba" :
8483                                  translation == BIOS_ATA_TRANSLATION_NONE ?
8484                                      ",trans=none" : "");
8485                 }
8486                 break;
8487             case QEMU_OPTION_nographic:
8488                 nographic = 1;
8489                 break;
8490 #ifdef CONFIG_CURSES
8491             case QEMU_OPTION_curses:
8492                 curses = 1;
8493                 break;
8494 #endif
8495             case QEMU_OPTION_portrait:
8496                 graphic_rotate = 1;
8497                 break;
8498             case QEMU_OPTION_kernel:
8499                 kernel_filename = optarg;
8500                 break;
8501             case QEMU_OPTION_append:
8502                 kernel_cmdline = optarg;
8503                 break;
8504             case QEMU_OPTION_cdrom:
8505                 drive_add(optarg, CDROM_ALIAS);
8506                 break;
8507             case QEMU_OPTION_boot:
8508                 boot_devices = optarg;
8509                 /* We just do some generic consistency checks */
8510                 {
8511                     /* Could easily be extended to 64 devices if needed */
8512                     const char *p;
8513                     
8514                     boot_devices_bitmap = 0;
8515                     for (p = boot_devices; *p != '\0'; p++) {
8516                         /* Allowed boot devices are:
8517                          * a b     : floppy disk drives
8518                          * c ... f : IDE disk drives
8519                          * g ... m : machine implementation dependant drives
8520                          * n ... p : network devices
8521                          * It's up to each machine implementation to check
8522                          * if the given boot devices match the actual hardware
8523                          * implementation and firmware features.
8524                          */
8525                         if (*p < 'a' || *p > 'q') {
8526                             fprintf(stderr, "Invalid boot device '%c'\n", *p);
8527                             exit(1);
8528                         }
8529                         if (boot_devices_bitmap & (1 << (*p - 'a'))) {
8530                             fprintf(stderr,
8531                                     "Boot device '%c' was given twice\n",*p);
8532                             exit(1);
8533                         }
8534                         boot_devices_bitmap |= 1 << (*p - 'a');
8535                     }
8536                 }
8537                 break;
8538             case QEMU_OPTION_fda:
8539             case QEMU_OPTION_fdb:
8540                 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
8541                 break;
8542 #ifdef TARGET_I386
8543             case QEMU_OPTION_no_fd_bootchk:
8544                 fd_bootchk = 0;
8545                 break;
8546 #endif
8547             case QEMU_OPTION_net:
8548                 if (nb_net_clients >= MAX_NET_CLIENTS) {
8549                     fprintf(stderr, "qemu: too many network clients\n");
8550                     exit(1);
8551                 }
8552                 net_clients[nb_net_clients] = optarg;
8553                 nb_net_clients++;
8554                 break;
8555 #ifdef CONFIG_SLIRP
8556             case QEMU_OPTION_tftp:
8557                 tftp_prefix = optarg;
8558                 break;
8559             case QEMU_OPTION_bootp:
8560                 bootp_filename = optarg;
8561                 break;
8562 #ifndef _WIN32
8563             case QEMU_OPTION_smb:
8564                 net_slirp_smb(optarg);
8565                 break;
8566 #endif
8567             case QEMU_OPTION_redir:
8568                 net_slirp_redir(optarg);
8569                 break;
8570 #endif
8571 #ifdef HAS_AUDIO
8572             case QEMU_OPTION_audio_help:
8573                 AUD_help ();
8574                 exit (0);
8575                 break;
8576             case QEMU_OPTION_soundhw:
8577                 select_soundhw (optarg);
8578                 break;
8579 #endif
8580             case QEMU_OPTION_h:
8581                 help(0);
8582                 break;
8583             case QEMU_OPTION_m: {
8584                 uint64_t value;
8585                 char *ptr;
8586
8587                 value = strtoul(optarg, &ptr, 10);
8588                 switch (*ptr) {
8589                 case 0: case 'M': case 'm':
8590                     value <<= 20;
8591                     break;
8592                 case 'G': case 'g':
8593                     value <<= 30;
8594                     break;
8595                 default:
8596                     fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
8597                     exit(1);
8598                 }
8599
8600                 /* On 32-bit hosts, QEMU is limited by virtual address space */
8601                 if (value > (2047 << 20)
8602 #ifndef USE_KQEMU
8603                     && HOST_LONG_BITS == 32
8604 #endif
8605                     ) {
8606                     fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
8607                     exit(1);
8608                 }
8609                 if (value != (uint64_t)(ram_addr_t)value) {
8610                     fprintf(stderr, "qemu: ram size too large\n");
8611                     exit(1);
8612                 }
8613                 ram_size = value;
8614                 break;
8615             }
8616             case QEMU_OPTION_d:
8617                 {
8618                     int mask;
8619                     CPULogItem *item;
8620
8621                     mask = cpu_str_to_log_mask(optarg);
8622                     if (!mask) {
8623                         printf("Log items (comma separated):\n");
8624                     for(item = cpu_log_items; item->mask != 0; item++) {
8625                         printf("%-10s %s\n", item->name, item->help);
8626                     }
8627                     exit(1);
8628                     }
8629                     cpu_set_log(mask);
8630                 }
8631                 break;
8632 #ifdef CONFIG_GDBSTUB
8633             case QEMU_OPTION_s:
8634                 use_gdbstub = 1;
8635                 break;
8636             case QEMU_OPTION_p:
8637                 gdbstub_port = optarg;
8638                 break;
8639 #endif
8640             case QEMU_OPTION_L:
8641                 bios_dir = optarg;
8642                 break;
8643             case QEMU_OPTION_bios:
8644                 bios_name = optarg;
8645                 break;
8646             case QEMU_OPTION_S:
8647                 autostart = 0;
8648                 break;
8649             case QEMU_OPTION_k:
8650                 keyboard_layout = optarg;
8651                 break;
8652             case QEMU_OPTION_localtime:
8653                 rtc_utc = 0;
8654                 break;
8655             case QEMU_OPTION_cirrusvga:
8656                 cirrus_vga_enabled = 1;
8657                 vmsvga_enabled = 0;
8658                 break;
8659             case QEMU_OPTION_vmsvga:
8660                 cirrus_vga_enabled = 0;
8661                 vmsvga_enabled = 1;
8662                 break;
8663             case QEMU_OPTION_std_vga:
8664                 cirrus_vga_enabled = 0;
8665                 vmsvga_enabled = 0;
8666                 break;
8667             case QEMU_OPTION_g:
8668                 {
8669                     const char *p;
8670                     int w, h, depth;
8671                     p = optarg;
8672                     w = strtol(p, (char **)&p, 10);
8673                     if (w <= 0) {
8674                     graphic_error:
8675                         fprintf(stderr, "qemu: invalid resolution or depth\n");
8676                         exit(1);
8677                     }
8678                     if (*p != 'x')
8679                         goto graphic_error;
8680                     p++;
8681                     h = strtol(p, (char **)&p, 10);
8682                     if (h <= 0)
8683                         goto graphic_error;
8684                     if (*p == 'x') {
8685                         p++;
8686                         depth = strtol(p, (char **)&p, 10);
8687                         if (depth != 8 && depth != 15 && depth != 16 &&
8688                             depth != 24 && depth != 32)
8689                             goto graphic_error;
8690                     } else if (*p == '\0') {
8691                         depth = graphic_depth;
8692                     } else {
8693                         goto graphic_error;
8694                     }
8695
8696                     graphic_width = w;
8697                     graphic_height = h;
8698                     graphic_depth = depth;
8699                 }
8700                 break;
8701             case QEMU_OPTION_echr:
8702                 {
8703                     char *r;
8704                     term_escape_char = strtol(optarg, &r, 0);
8705                     if (r == optarg)
8706                         printf("Bad argument to echr\n");
8707                     break;
8708                 }
8709             case QEMU_OPTION_monitor:
8710                 monitor_device = optarg;
8711                 break;
8712             case QEMU_OPTION_serial:
8713                 if (serial_device_index >= MAX_SERIAL_PORTS) {
8714                     fprintf(stderr, "qemu: too many serial ports\n");
8715                     exit(1);
8716                 }
8717                 serial_devices[serial_device_index] = optarg;
8718                 serial_device_index++;
8719                 break;
8720             case QEMU_OPTION_parallel:
8721                 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
8722                     fprintf(stderr, "qemu: too many parallel ports\n");
8723                     exit(1);
8724                 }
8725                 parallel_devices[parallel_device_index] = optarg;
8726                 parallel_device_index++;
8727                 break;
8728             case QEMU_OPTION_loadvm:
8729                 loadvm = optarg;
8730                 break;
8731             case QEMU_OPTION_full_screen:
8732                 full_screen = 1;
8733                 break;
8734 #ifdef CONFIG_SDL
8735             case QEMU_OPTION_no_frame:
8736                 no_frame = 1;
8737                 break;
8738             case QEMU_OPTION_alt_grab:
8739                 alt_grab = 1;
8740                 break;
8741             case QEMU_OPTION_no_quit:
8742                 no_quit = 1;
8743                 break;
8744 #endif
8745             case QEMU_OPTION_pidfile:
8746                 pid_file = optarg;
8747                 break;
8748 #ifdef TARGET_I386
8749             case QEMU_OPTION_win2k_hack:
8750                 win2k_install_hack = 1;
8751                 break;
8752 #endif
8753 #ifdef USE_KQEMU
8754             case QEMU_OPTION_no_kqemu:
8755                 kqemu_allowed = 0;
8756                 break;
8757             case QEMU_OPTION_kernel_kqemu:
8758                 kqemu_allowed = 2;
8759                 break;
8760 #endif
8761             case QEMU_OPTION_usb:
8762                 usb_enabled = 1;
8763                 break;
8764             case QEMU_OPTION_usbdevice:
8765                 usb_enabled = 1;
8766                 if (usb_devices_index >= MAX_USB_CMDLINE) {
8767                     fprintf(stderr, "Too many USB devices\n");
8768                     exit(1);
8769                 }
8770                 usb_devices[usb_devices_index] = optarg;
8771                 usb_devices_index++;
8772                 break;
8773             case QEMU_OPTION_smp:
8774                 smp_cpus = atoi(optarg);
8775                 if (smp_cpus < 1 || smp_cpus > MAX_CPUS) {
8776                     fprintf(stderr, "Invalid number of CPUs\n");
8777                     exit(1);
8778                 }
8779                 break;
8780             case QEMU_OPTION_vnc:
8781                 vnc_display = optarg;
8782                 break;
8783             case QEMU_OPTION_no_acpi:
8784                 acpi_enabled = 0;
8785                 break;
8786             case QEMU_OPTION_no_reboot:
8787                 no_reboot = 1;
8788                 break;
8789             case QEMU_OPTION_no_shutdown:
8790                 no_shutdown = 1;
8791                 break;
8792             case QEMU_OPTION_show_cursor:
8793                 cursor_hide = 0;
8794                 break;
8795             case QEMU_OPTION_uuid:
8796                 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
8797                     fprintf(stderr, "Fail to parse UUID string."
8798                             " Wrong format.\n");
8799                     exit(1);
8800                 }
8801                 break;
8802             case QEMU_OPTION_daemonize:
8803                 daemonize = 1;
8804                 break;
8805             case QEMU_OPTION_option_rom:
8806                 if (nb_option_roms >= MAX_OPTION_ROMS) {
8807                     fprintf(stderr, "Too many option ROMs\n");
8808                     exit(1);
8809                 }
8810                 option_rom[nb_option_roms] = optarg;
8811                 nb_option_roms++;
8812                 break;
8813             case QEMU_OPTION_semihosting:
8814                 semihosting_enabled = 1;
8815                 break;
8816             case QEMU_OPTION_name:
8817                 qemu_name = optarg;
8818                 break;
8819 #ifdef TARGET_SPARC
8820             case QEMU_OPTION_prom_env:
8821                 if (nb_prom_envs >= MAX_PROM_ENVS) {
8822                     fprintf(stderr, "Too many prom variables\n");
8823                     exit(1);
8824                 }
8825                 prom_envs[nb_prom_envs] = optarg;
8826                 nb_prom_envs++;
8827                 break;
8828 #endif
8829 #ifdef TARGET_ARM
8830             case QEMU_OPTION_old_param:
8831                 old_param = 1;
8832                 break;
8833 #endif
8834             case QEMU_OPTION_clock:
8835                 configure_alarms(optarg);
8836                 break;
8837             case QEMU_OPTION_startdate:
8838                 {
8839                     struct tm tm;
8840                     time_t rtc_start_date;
8841                     if (!strcmp(optarg, "now")) {
8842                         rtc_date_offset = -1;
8843                     } else {
8844                         if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
8845                                &tm.tm_year,
8846                                &tm.tm_mon,
8847                                &tm.tm_mday,
8848                                &tm.tm_hour,
8849                                &tm.tm_min,
8850                                &tm.tm_sec) == 6) {
8851                             /* OK */
8852                         } else if (sscanf(optarg, "%d-%d-%d",
8853                                           &tm.tm_year,
8854                                           &tm.tm_mon,
8855                                           &tm.tm_mday) == 3) {
8856                             tm.tm_hour = 0;
8857                             tm.tm_min = 0;
8858                             tm.tm_sec = 0;
8859                         } else {
8860                             goto date_fail;
8861                         }
8862                         tm.tm_year -= 1900;
8863                         tm.tm_mon--;
8864                         rtc_start_date = mktimegm(&tm);
8865                         if (rtc_start_date == -1) {
8866                         date_fail:
8867                             fprintf(stderr, "Invalid date format. Valid format are:\n"
8868                                     "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
8869                             exit(1);
8870                         }
8871                         rtc_date_offset = time(NULL) - rtc_start_date;
8872                     }
8873                 }
8874                 break;
8875             case QEMU_OPTION_tb_size:
8876                 tb_size = strtol(optarg, NULL, 0);
8877                 if (tb_size < 0)
8878                     tb_size = 0;
8879                 break;
8880             case QEMU_OPTION_icount:
8881                 use_icount = 1;
8882                 if (strcmp(optarg, "auto") == 0) {
8883                     icount_time_shift = -1;
8884                 } else {
8885                     icount_time_shift = strtol(optarg, NULL, 0);
8886                 }
8887                 break;
8888             }
8889         }
8890     }
8891
8892     if (nographic) {
8893        if (serial_device_index == 0)
8894            serial_devices[0] = "stdio";
8895        if (parallel_device_index == 0)
8896            parallel_devices[0] = "null";
8897        if (strncmp(monitor_device, "vc", 2) == 0)
8898            monitor_device = "stdio";
8899     }
8900
8901 #ifndef _WIN32
8902     if (daemonize) {
8903         pid_t pid;
8904
8905         if (pipe(fds) == -1)
8906             exit(1);
8907
8908         pid = fork();
8909         if (pid > 0) {
8910             uint8_t status;
8911             ssize_t len;
8912
8913             close(fds[1]);
8914
8915         again:
8916             len = read(fds[0], &status, 1);
8917             if (len == -1 && (errno == EINTR))
8918                 goto again;
8919
8920             if (len != 1)
8921                 exit(1);
8922             else if (status == 1) {
8923                 fprintf(stderr, "Could not acquire pidfile\n");
8924                 exit(1);
8925             } else
8926                 exit(0);
8927         } else if (pid < 0)
8928             exit(1);
8929
8930         setsid();
8931
8932         pid = fork();
8933         if (pid > 0)
8934             exit(0);
8935         else if (pid < 0)
8936             exit(1);
8937
8938         umask(027);
8939
8940         signal(SIGTSTP, SIG_IGN);
8941         signal(SIGTTOU, SIG_IGN);
8942         signal(SIGTTIN, SIG_IGN);
8943     }
8944 #endif
8945
8946     if (pid_file && qemu_create_pidfile(pid_file) != 0) {
8947         if (daemonize) {
8948             uint8_t status = 1;
8949             write(fds[1], &status, 1);
8950         } else
8951             fprintf(stderr, "Could not acquire pid file\n");
8952         exit(1);
8953     }
8954
8955 #ifdef USE_KQEMU
8956     if (smp_cpus > 1)
8957         kqemu_allowed = 0;
8958 #endif
8959     linux_boot = (kernel_filename != NULL);
8960     net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
8961
8962     if (!linux_boot && net_boot == 0 &&
8963         !machine->nodisk_ok && nb_drives_opt == 0)
8964         help(1);
8965
8966     if (!linux_boot && *kernel_cmdline != '\0') {
8967         fprintf(stderr, "-append only allowed with -kernel option\n");
8968         exit(1);
8969     }
8970
8971     if (!linux_boot && initrd_filename != NULL) {
8972         fprintf(stderr, "-initrd only allowed with -kernel option\n");
8973         exit(1);
8974     }
8975
8976     /* boot to floppy or the default cd if no hard disk defined yet */
8977     if (!boot_devices[0]) {
8978         boot_devices = "cad";
8979     }
8980     setvbuf(stdout, NULL, _IOLBF, 0);
8981
8982     init_timers();
8983     init_timer_alarm();
8984     if (use_icount && icount_time_shift < 0) {
8985         use_icount = 2;
8986         /* 125MIPS seems a reasonable initial guess at the guest speed.
8987            It will be corrected fairly quickly anyway.  */
8988         icount_time_shift = 3;
8989         init_icount_adjust();
8990     }
8991
8992 #ifdef _WIN32
8993     socket_init();
8994 #endif
8995
8996     /* init network clients */
8997     if (nb_net_clients == 0) {
8998         /* if no clients, we use a default config */
8999         net_clients[nb_net_clients++] = "nic";
9000 #ifdef CONFIG_SLIRP
9001         net_clients[nb_net_clients++] = "user";
9002 #endif
9003     }
9004
9005     for(i = 0;i < nb_net_clients; i++) {
9006         if (net_client_parse(net_clients[i]) < 0)
9007             exit(1);
9008     }
9009     for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
9010         if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
9011             continue;
9012         if (vlan->nb_guest_devs == 0)
9013             fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
9014         if (vlan->nb_host_devs == 0)
9015             fprintf(stderr,
9016                     "Warning: vlan %d is not connected to host network\n",
9017                     vlan->id);
9018     }
9019
9020 #ifdef TARGET_I386
9021     /* XXX: this should be moved in the PC machine instantiation code */
9022     if (net_boot != 0) {
9023         int netroms = 0;
9024         for (i = 0; i < nb_nics && i < 4; i++) {
9025             const char *model = nd_table[i].model;
9026             char buf[1024];
9027             if (net_boot & (1 << i)) {
9028                 if (model == NULL)
9029                     model = "ne2k_pci";
9030                 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
9031                 if (get_image_size(buf) > 0) {
9032                     if (nb_option_roms >= MAX_OPTION_ROMS) {
9033                         fprintf(stderr, "Too many option ROMs\n");
9034                         exit(1);
9035                     }
9036                     option_rom[nb_option_roms] = strdup(buf);
9037                     nb_option_roms++;
9038                     netroms++;
9039                 }
9040             }
9041         }
9042         if (netroms == 0) {
9043             fprintf(stderr, "No valid PXE rom found for network device\n");
9044             exit(1);
9045         }
9046     }
9047 #endif
9048
9049     /* init the memory */
9050     phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED;
9051
9052     if (machine->ram_require & RAMSIZE_FIXED) {
9053         if (ram_size > 0) {
9054             if (ram_size < phys_ram_size) {
9055                 fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
9056                                 machine->name, (unsigned long long) phys_ram_size);
9057                 exit(-1);
9058             }
9059
9060             phys_ram_size = ram_size;
9061         } else
9062             ram_size = phys_ram_size;
9063     } else {
9064         if (ram_size == 0)
9065             ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
9066
9067         phys_ram_size += ram_size;
9068     }
9069
9070     phys_ram_base = qemu_vmalloc(phys_ram_size);
9071     if (!phys_ram_base) {
9072         fprintf(stderr, "Could not allocate physical memory\n");
9073         exit(1);
9074     }
9075
9076     /* init the dynamic translator */
9077     cpu_exec_init_all(tb_size * 1024 * 1024);
9078
9079     bdrv_init();
9080
9081     /* we always create the cdrom drive, even if no disk is there */
9082
9083     if (nb_drives_opt < MAX_DRIVES)
9084         drive_add(NULL, CDROM_ALIAS);
9085
9086     /* we always create at least one floppy */
9087
9088     if (nb_drives_opt < MAX_DRIVES)
9089         drive_add(NULL, FD_ALIAS, 0);
9090
9091     /* we always create one sd slot, even if no card is in it */
9092
9093     if (nb_drives_opt < MAX_DRIVES)
9094         drive_add(NULL, SD_ALIAS);
9095
9096     /* open the virtual block devices */
9097
9098     for(i = 0; i < nb_drives_opt; i++)
9099         if (drive_init(&drives_opt[i], snapshot, machine) == -1)
9100             exit(1);
9101
9102     register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
9103     register_savevm("ram", 0, 2, ram_save, ram_load, NULL);
9104
9105     /* terminal init */
9106     memset(&display_state, 0, sizeof(display_state));
9107     if (nographic) {
9108         if (curses) {
9109             fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
9110             exit(1);
9111         }
9112         /* nearly nothing to do */
9113         dumb_display_init(ds);
9114     } else if (vnc_display != NULL) {
9115         vnc_display_init(ds);
9116         if (vnc_display_open(ds, vnc_display) < 0)
9117             exit(1);
9118     } else
9119 #if defined(CONFIG_CURSES)
9120     if (curses) {
9121         curses_display_init(ds, full_screen);
9122     } else
9123 #endif
9124     {
9125 #if defined(CONFIG_SDL)
9126         sdl_display_init(ds, full_screen, no_frame);
9127 #elif defined(CONFIG_COCOA)
9128         cocoa_display_init(ds, full_screen);
9129 #else
9130         dumb_display_init(ds);
9131 #endif
9132     }
9133
9134 #ifndef _WIN32
9135     /* must be after terminal init, SDL library changes signal handlers */
9136     termsig_setup();
9137 #endif
9138
9139     /* Maintain compatibility with multiple stdio monitors */
9140     if (!strcmp(monitor_device,"stdio")) {
9141         for (i = 0; i < MAX_SERIAL_PORTS; i++) {
9142             const char *devname = serial_devices[i];
9143             if (devname && !strcmp(devname,"mon:stdio")) {
9144                 monitor_device = NULL;
9145                 break;
9146             } else if (devname && !strcmp(devname,"stdio")) {
9147                 monitor_device = NULL;
9148                 serial_devices[i] = "mon:stdio";
9149                 break;
9150             }
9151         }
9152     }
9153     if (monitor_device) {
9154         monitor_hd = qemu_chr_open(monitor_device);
9155         if (!monitor_hd) {
9156             fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
9157             exit(1);
9158         }
9159         monitor_init(monitor_hd, !nographic);
9160     }
9161
9162     for(i = 0; i < MAX_SERIAL_PORTS; i++) {
9163         const char *devname = serial_devices[i];
9164         if (devname && strcmp(devname, "none")) {
9165             serial_hds[i] = qemu_chr_open(devname);
9166             if (!serial_hds[i]) {
9167                 fprintf(stderr, "qemu: could not open serial device '%s'\n",
9168                         devname);
9169                 exit(1);
9170             }
9171             if (strstart(devname, "vc", 0))
9172                 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
9173         }
9174     }
9175
9176     for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
9177         const char *devname = parallel_devices[i];
9178         if (devname && strcmp(devname, "none")) {
9179             parallel_hds[i] = qemu_chr_open(devname);
9180             if (!parallel_hds[i]) {
9181                 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
9182                         devname);
9183                 exit(1);
9184             }
9185             if (strstart(devname, "vc", 0))
9186                 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
9187         }
9188     }
9189
9190     machine->init(ram_size, vga_ram_size, boot_devices, ds,
9191                   kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
9192
9193     /* init USB devices */
9194     if (usb_enabled) {
9195         for(i = 0; i < usb_devices_index; i++) {
9196             if (usb_device_add(usb_devices[i]) < 0) {
9197                 fprintf(stderr, "Warning: could not add USB device %s\n",
9198                         usb_devices[i]);
9199             }
9200         }
9201     }
9202
9203     if (display_state.dpy_refresh) {
9204         display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state);
9205         qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock));
9206     }
9207
9208 #ifdef CONFIG_GDBSTUB
9209     if (use_gdbstub) {
9210         /* XXX: use standard host:port notation and modify options
9211            accordingly. */
9212         if (gdbserver_start(gdbstub_port) < 0) {
9213             fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
9214                     gdbstub_port);
9215             exit(1);
9216         }
9217     }
9218 #endif
9219
9220     if (loadvm)
9221         do_loadvm(loadvm);
9222
9223     {
9224         /* XXX: simplify init */
9225         read_passwords();
9226         if (autostart) {
9227             vm_start();
9228         }
9229     }
9230
9231     if (daemonize) {
9232         uint8_t status = 0;
9233         ssize_t len;
9234         int fd;
9235
9236     again1:
9237         len = write(fds[1], &status, 1);
9238         if (len == -1 && (errno == EINTR))
9239             goto again1;
9240
9241         if (len != 1)
9242             exit(1);
9243
9244         chdir("/");
9245         TFR(fd = open("/dev/null", O_RDWR));
9246         if (fd == -1)
9247             exit(1);
9248
9249         dup2(fd, 0);
9250         dup2(fd, 1);
9251         dup2(fd, 2);
9252
9253         close(fd);
9254     }
9255
9256     main_loop();
9257     quit_timers();
9258
9259 #if !defined(_WIN32)
9260     /* close network clients */
9261     for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
9262         VLANClientState *vc;
9263
9264         for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
9265             if (vc->fd_read == tap_receive) {
9266                 char ifname[64];
9267                 TAPState *s = vc->opaque;
9268
9269                 if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 &&
9270                     s->down_script[0])
9271                     launch_script(s->down_script, ifname, s->fd);
9272             }
9273 #if defined(CONFIG_VDE)
9274             if (vc->fd_read == vde_from_qemu) {
9275                 VDEState *s = vc->opaque;
9276                 vde_close(s->vde);
9277             }
9278 #endif
9279         }
9280     }
9281 #endif
9282     return 0;
9283 }