4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "hw/boards.h"
27 #include "hw/pcmcia.h"
29 #include "hw/audiodev.h"
36 #include "qemu-timer.h"
37 #include "qemu-char.h"
39 #include "audio/audio.h"
50 #include <sys/times.h>
55 #include <sys/ioctl.h>
56 #include <sys/socket.h>
57 #include <netinet/in.h>
60 #include <sys/select.h>
61 #include <arpa/inet.h>
67 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
68 #include <freebsd/stdlib.h>
72 #include <linux/if_tun.h>
75 #include <linux/rtc.h>
77 /* For the benefit of older linux systems which don't supply it,
78 we use a local copy of hpet.h. */
79 /* #include <linux/hpet.h> */
82 #include <linux/ppdev.h>
83 #include <linux/parport.h>
86 #include <sys/ethernet.h>
87 #include <sys/sockio.h>
88 #include <netinet/arp.h>
89 #include <netinet/in.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/ip.h>
92 #include <netinet/ip_icmp.h> // must come after ip.h
93 #include <netinet/udp.h>
94 #include <netinet/tcp.h>
101 #include <winsock2.h>
102 int inet_aton(const char *cp
, struct in_addr
*ia
);
105 #if defined(CONFIG_SLIRP)
106 #include "libslirp.h"
111 #include <sys/timeb.h>
112 #include <mmsystem.h>
113 #define getopt_long_only getopt_long
114 #define memalign(align, size) malloc(size)
117 #include "qemu_socket.h"
123 #endif /* CONFIG_SDL */
127 #define main qemu_main
128 #endif /* CONFIG_COCOA */
132 #include "exec-all.h"
134 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
135 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
137 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
139 #define SMBD_COMMAND "/usr/sbin/smbd"
142 //#define DEBUG_UNUSED_IOPORT
143 //#define DEBUG_IOPORT
146 #define DEFAULT_RAM_SIZE 144
148 #define DEFAULT_RAM_SIZE 128
151 #define GUI_REFRESH_INTERVAL 30
153 /* Max number of USB devices that can be specified on the commandline. */
154 #define MAX_USB_CMDLINE 8
156 /* XXX: use a two level table to limit memory usage */
157 #define MAX_IOPORTS 65536
159 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
160 const char *bios_name
= NULL
;
161 void *ioport_opaque
[MAX_IOPORTS
];
162 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
163 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
164 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
165 to store the VM snapshots */
166 DriveInfo drives_table
[MAX_DRIVES
+1];
168 /* point to the block driver where the snapshots are managed */
169 BlockDriverState
*bs_snapshots
;
171 static DisplayState display_state
;
174 const char* keyboard_layout
= NULL
;
175 int64_t ticks_per_sec
;
177 int pit_min_timer_count
= 0;
179 NICInfo nd_table
[MAX_NICS
];
181 static int rtc_utc
= 1;
182 static int rtc_date_offset
= -1; /* -1 means no change */
183 int cirrus_vga_enabled
= 1;
184 int vmsvga_enabled
= 0;
186 int graphic_width
= 1024;
187 int graphic_height
= 768;
188 int graphic_depth
= 8;
190 int graphic_width
= 800;
191 int graphic_height
= 600;
192 int graphic_depth
= 15;
197 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
198 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
200 int win2k_install_hack
= 0;
203 static VLANState
*first_vlan
;
205 const char *vnc_display
;
206 #if defined(TARGET_SPARC)
208 #elif defined(TARGET_I386)
213 int acpi_enabled
= 1;
218 int graphic_rotate
= 0;
220 const char *option_rom
[MAX_OPTION_ROMS
];
222 int semihosting_enabled
= 0;
227 const char *qemu_name
;
230 unsigned int nb_prom_envs
= 0;
231 const char *prom_envs
[MAX_PROM_ENVS
];
237 } drives_opt
[MAX_DRIVES
];
239 static CPUState
*cur_cpu
;
240 static CPUState
*next_cpu
;
241 static int event_pending
= 1;
243 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
245 /***********************************************************/
246 /* x86 ISA bus support */
248 target_phys_addr_t isa_mem_base
= 0;
251 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
253 #ifdef DEBUG_UNUSED_IOPORT
254 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
259 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
261 #ifdef DEBUG_UNUSED_IOPORT
262 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
266 /* default is to make two byte accesses */
267 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
270 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
271 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
272 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
276 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
278 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
279 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
280 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
283 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
285 #ifdef DEBUG_UNUSED_IOPORT
286 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
291 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
293 #ifdef DEBUG_UNUSED_IOPORT
294 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
298 static void init_ioports(void)
302 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
303 ioport_read_table
[0][i
] = default_ioport_readb
;
304 ioport_write_table
[0][i
] = default_ioport_writeb
;
305 ioport_read_table
[1][i
] = default_ioport_readw
;
306 ioport_write_table
[1][i
] = default_ioport_writew
;
307 ioport_read_table
[2][i
] = default_ioport_readl
;
308 ioport_write_table
[2][i
] = default_ioport_writel
;
312 /* size is the word size in byte */
313 int register_ioport_read(int start
, int length
, int size
,
314 IOPortReadFunc
*func
, void *opaque
)
320 } else if (size
== 2) {
322 } else if (size
== 4) {
325 hw_error("register_ioport_read: invalid size");
328 for(i
= start
; i
< start
+ length
; i
+= size
) {
329 ioport_read_table
[bsize
][i
] = func
;
330 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
331 hw_error("register_ioport_read: invalid opaque");
332 ioport_opaque
[i
] = opaque
;
337 /* size is the word size in byte */
338 int register_ioport_write(int start
, int length
, int size
,
339 IOPortWriteFunc
*func
, void *opaque
)
345 } else if (size
== 2) {
347 } else if (size
== 4) {
350 hw_error("register_ioport_write: invalid size");
353 for(i
= start
; i
< start
+ length
; i
+= size
) {
354 ioport_write_table
[bsize
][i
] = func
;
355 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
356 hw_error("register_ioport_write: invalid opaque");
357 ioport_opaque
[i
] = opaque
;
362 void isa_unassign_ioport(int start
, int length
)
366 for(i
= start
; i
< start
+ length
; i
++) {
367 ioport_read_table
[0][i
] = default_ioport_readb
;
368 ioport_read_table
[1][i
] = default_ioport_readw
;
369 ioport_read_table
[2][i
] = default_ioport_readl
;
371 ioport_write_table
[0][i
] = default_ioport_writeb
;
372 ioport_write_table
[1][i
] = default_ioport_writew
;
373 ioport_write_table
[2][i
] = default_ioport_writel
;
377 /***********************************************************/
379 void cpu_outb(CPUState
*env
, int addr
, int val
)
382 if (loglevel
& CPU_LOG_IOPORT
)
383 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
385 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
388 env
->last_io_time
= cpu_get_time_fast();
392 void cpu_outw(CPUState
*env
, int addr
, int val
)
395 if (loglevel
& CPU_LOG_IOPORT
)
396 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
398 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
401 env
->last_io_time
= cpu_get_time_fast();
405 void cpu_outl(CPUState
*env
, int addr
, int val
)
408 if (loglevel
& CPU_LOG_IOPORT
)
409 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
411 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
414 env
->last_io_time
= cpu_get_time_fast();
418 int cpu_inb(CPUState
*env
, int addr
)
421 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
423 if (loglevel
& CPU_LOG_IOPORT
)
424 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
428 env
->last_io_time
= cpu_get_time_fast();
433 int cpu_inw(CPUState
*env
, int addr
)
436 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
438 if (loglevel
& CPU_LOG_IOPORT
)
439 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
443 env
->last_io_time
= cpu_get_time_fast();
448 int cpu_inl(CPUState
*env
, int addr
)
451 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
453 if (loglevel
& CPU_LOG_IOPORT
)
454 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
458 env
->last_io_time
= cpu_get_time_fast();
463 /***********************************************************/
464 void hw_error(const char *fmt
, ...)
470 fprintf(stderr
, "qemu: hardware error: ");
471 vfprintf(stderr
, fmt
, ap
);
472 fprintf(stderr
, "\n");
473 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
474 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
476 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
478 cpu_dump_state(env
, stderr
, fprintf
, 0);
485 /***********************************************************/
488 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
489 static void *qemu_put_kbd_event_opaque
;
490 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
491 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
493 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
495 qemu_put_kbd_event_opaque
= opaque
;
496 qemu_put_kbd_event
= func
;
499 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
500 void *opaque
, int absolute
,
503 QEMUPutMouseEntry
*s
, *cursor
;
505 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
509 s
->qemu_put_mouse_event
= func
;
510 s
->qemu_put_mouse_event_opaque
= opaque
;
511 s
->qemu_put_mouse_event_absolute
= absolute
;
512 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
515 if (!qemu_put_mouse_event_head
) {
516 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
520 cursor
= qemu_put_mouse_event_head
;
521 while (cursor
->next
!= NULL
)
522 cursor
= cursor
->next
;
525 qemu_put_mouse_event_current
= s
;
530 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
532 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
534 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
537 cursor
= qemu_put_mouse_event_head
;
538 while (cursor
!= NULL
&& cursor
!= entry
) {
540 cursor
= cursor
->next
;
543 if (cursor
== NULL
) // does not exist or list empty
545 else if (prev
== NULL
) { // entry is head
546 qemu_put_mouse_event_head
= cursor
->next
;
547 if (qemu_put_mouse_event_current
== entry
)
548 qemu_put_mouse_event_current
= cursor
->next
;
549 qemu_free(entry
->qemu_put_mouse_event_name
);
554 prev
->next
= entry
->next
;
556 if (qemu_put_mouse_event_current
== entry
)
557 qemu_put_mouse_event_current
= prev
;
559 qemu_free(entry
->qemu_put_mouse_event_name
);
563 void kbd_put_keycode(int keycode
)
565 if (qemu_put_kbd_event
) {
566 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
570 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
572 QEMUPutMouseEvent
*mouse_event
;
573 void *mouse_event_opaque
;
576 if (!qemu_put_mouse_event_current
) {
581 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
583 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
586 if (graphic_rotate
) {
587 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
590 width
= graphic_width
- 1;
591 mouse_event(mouse_event_opaque
,
592 width
- dy
, dx
, dz
, buttons_state
);
594 mouse_event(mouse_event_opaque
,
595 dx
, dy
, dz
, buttons_state
);
599 int kbd_mouse_is_absolute(void)
601 if (!qemu_put_mouse_event_current
)
604 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
607 void do_info_mice(void)
609 QEMUPutMouseEntry
*cursor
;
612 if (!qemu_put_mouse_event_head
) {
613 term_printf("No mouse devices connected\n");
617 term_printf("Mouse devices available:\n");
618 cursor
= qemu_put_mouse_event_head
;
619 while (cursor
!= NULL
) {
620 term_printf("%c Mouse #%d: %s\n",
621 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
622 index
, cursor
->qemu_put_mouse_event_name
);
624 cursor
= cursor
->next
;
628 void do_mouse_set(int index
)
630 QEMUPutMouseEntry
*cursor
;
633 if (!qemu_put_mouse_event_head
) {
634 term_printf("No mouse devices connected\n");
638 cursor
= qemu_put_mouse_event_head
;
639 while (cursor
!= NULL
&& index
!= i
) {
641 cursor
= cursor
->next
;
645 qemu_put_mouse_event_current
= cursor
;
647 term_printf("Mouse at given index not found\n");
650 /* compute with 96 bit intermediate result: (a*b)/c */
651 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
656 #ifdef WORDS_BIGENDIAN
666 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
667 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
670 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
674 /***********************************************************/
675 /* real time host monotonic timer */
677 #define QEMU_TIMER_BASE 1000000000LL
681 static int64_t clock_freq
;
683 static void init_get_clock(void)
687 ret
= QueryPerformanceFrequency(&freq
);
689 fprintf(stderr
, "Could not calibrate ticks\n");
692 clock_freq
= freq
.QuadPart
;
695 static int64_t get_clock(void)
698 QueryPerformanceCounter(&ti
);
699 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
704 static int use_rt_clock
;
706 static void init_get_clock(void)
709 #if defined(__linux__)
712 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
719 static int64_t get_clock(void)
721 #if defined(__linux__)
724 clock_gettime(CLOCK_MONOTONIC
, &ts
);
725 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
729 /* XXX: using gettimeofday leads to problems if the date
730 changes, so it should be avoided. */
732 gettimeofday(&tv
, NULL
);
733 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
739 /***********************************************************/
740 /* guest cycle counter */
742 static int64_t cpu_ticks_prev
;
743 static int64_t cpu_ticks_offset
;
744 static int64_t cpu_clock_offset
;
745 static int cpu_ticks_enabled
;
747 /* return the host CPU cycle counter and handle stop/restart */
748 int64_t cpu_get_ticks(void)
750 if (!cpu_ticks_enabled
) {
751 return cpu_ticks_offset
;
754 ticks
= cpu_get_real_ticks();
755 if (cpu_ticks_prev
> ticks
) {
756 /* Note: non increasing ticks may happen if the host uses
758 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
760 cpu_ticks_prev
= ticks
;
761 return ticks
+ cpu_ticks_offset
;
765 /* return the host CPU monotonic timer and handle stop/restart */
766 static int64_t cpu_get_clock(void)
769 if (!cpu_ticks_enabled
) {
770 return cpu_clock_offset
;
773 return ti
+ cpu_clock_offset
;
777 /* enable cpu_get_ticks() */
778 void cpu_enable_ticks(void)
780 if (!cpu_ticks_enabled
) {
781 cpu_ticks_offset
-= cpu_get_real_ticks();
782 cpu_clock_offset
-= get_clock();
783 cpu_ticks_enabled
= 1;
787 /* disable cpu_get_ticks() : the clock is stopped. You must not call
788 cpu_get_ticks() after that. */
789 void cpu_disable_ticks(void)
791 if (cpu_ticks_enabled
) {
792 cpu_ticks_offset
= cpu_get_ticks();
793 cpu_clock_offset
= cpu_get_clock();
794 cpu_ticks_enabled
= 0;
798 /***********************************************************/
801 #define QEMU_TIMER_REALTIME 0
802 #define QEMU_TIMER_VIRTUAL 1
806 /* XXX: add frequency */
814 struct QEMUTimer
*next
;
817 struct qemu_alarm_timer
{
821 int (*start
)(struct qemu_alarm_timer
*t
);
822 void (*stop
)(struct qemu_alarm_timer
*t
);
823 void (*rearm
)(struct qemu_alarm_timer
*t
);
827 #define ALARM_FLAG_DYNTICKS 0x1
828 #define ALARM_FLAG_EXPIRED 0x2
830 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
832 return t
->flags
& ALARM_FLAG_DYNTICKS
;
835 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
837 if (!alarm_has_dynticks(t
))
843 /* TODO: MIN_TIMER_REARM_US should be optimized */
844 #define MIN_TIMER_REARM_US 250
846 static struct qemu_alarm_timer
*alarm_timer
;
850 struct qemu_alarm_win32
{
854 } alarm_win32_data
= {0, NULL
, -1};
856 static int win32_start_timer(struct qemu_alarm_timer
*t
);
857 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
858 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
862 static int unix_start_timer(struct qemu_alarm_timer
*t
);
863 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
867 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
868 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
869 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
871 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
872 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
874 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
875 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
877 #endif /* __linux__ */
881 static struct qemu_alarm_timer alarm_timers
[] = {
884 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
885 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
886 /* HPET - if available - is preferred */
887 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
888 /* ...otherwise try RTC */
889 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
891 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
893 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
894 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
895 {"win32", 0, win32_start_timer
,
896 win32_stop_timer
, NULL
, &alarm_win32_data
},
901 static void show_available_alarms(void)
905 printf("Available alarm timers, in order of precedence:\n");
906 for (i
= 0; alarm_timers
[i
].name
; i
++)
907 printf("%s\n", alarm_timers
[i
].name
);
910 static void configure_alarms(char const *opt
)
914 int count
= (sizeof(alarm_timers
) / sizeof(*alarm_timers
)) - 1;
918 if (!strcmp(opt
, "?")) {
919 show_available_alarms();
925 /* Reorder the array */
926 name
= strtok(arg
, ",");
928 struct qemu_alarm_timer tmp
;
930 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
931 if (!strcmp(alarm_timers
[i
].name
, name
))
936 fprintf(stderr
, "Unknown clock %s\n", name
);
945 tmp
= alarm_timers
[i
];
946 alarm_timers
[i
] = alarm_timers
[cur
];
947 alarm_timers
[cur
] = tmp
;
951 name
= strtok(NULL
, ",");
957 /* Disable remaining timers */
958 for (i
= cur
; i
< count
; i
++)
959 alarm_timers
[i
].name
= NULL
;
961 show_available_alarms();
969 static QEMUTimer
*active_timers
[2];
971 static QEMUClock
*qemu_new_clock(int type
)
974 clock
= qemu_mallocz(sizeof(QEMUClock
));
981 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
985 ts
= qemu_mallocz(sizeof(QEMUTimer
));
992 void qemu_free_timer(QEMUTimer
*ts
)
997 /* stop a timer, but do not dealloc it */
998 void qemu_del_timer(QEMUTimer
*ts
)
1002 /* NOTE: this code must be signal safe because
1003 qemu_timer_expired() can be called from a signal. */
1004 pt
= &active_timers
[ts
->clock
->type
];
1017 /* modify the current timer so that it will be fired when current_time
1018 >= expire_time. The corresponding callback will be called. */
1019 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1025 /* add the timer in the sorted list */
1026 /* NOTE: this code must be signal safe because
1027 qemu_timer_expired() can be called from a signal. */
1028 pt
= &active_timers
[ts
->clock
->type
];
1033 if (t
->expire_time
> expire_time
)
1037 ts
->expire_time
= expire_time
;
1041 /* Rearm if necessary */
1042 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0 &&
1043 pt
== &active_timers
[ts
->clock
->type
])
1044 qemu_rearm_alarm_timer(alarm_timer
);
1047 int qemu_timer_pending(QEMUTimer
*ts
)
1050 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1057 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1061 return (timer_head
->expire_time
<= current_time
);
1064 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1070 if (!ts
|| ts
->expire_time
> current_time
)
1072 /* remove timer from the list before calling the callback */
1073 *ptimer_head
= ts
->next
;
1076 /* run the callback (the timer list can be modified) */
1081 int64_t qemu_get_clock(QEMUClock
*clock
)
1083 switch(clock
->type
) {
1084 case QEMU_TIMER_REALTIME
:
1085 return get_clock() / 1000000;
1087 case QEMU_TIMER_VIRTUAL
:
1088 return cpu_get_clock();
1092 static void init_timers(void)
1095 ticks_per_sec
= QEMU_TIMER_BASE
;
1096 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1097 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1101 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1103 uint64_t expire_time
;
1105 if (qemu_timer_pending(ts
)) {
1106 expire_time
= ts
->expire_time
;
1110 qemu_put_be64(f
, expire_time
);
1113 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1115 uint64_t expire_time
;
1117 expire_time
= qemu_get_be64(f
);
1118 if (expire_time
!= -1) {
1119 qemu_mod_timer(ts
, expire_time
);
1125 static void timer_save(QEMUFile
*f
, void *opaque
)
1127 if (cpu_ticks_enabled
) {
1128 hw_error("cannot save state if virtual timers are running");
1130 qemu_put_be64(f
, cpu_ticks_offset
);
1131 qemu_put_be64(f
, ticks_per_sec
);
1132 qemu_put_be64(f
, cpu_clock_offset
);
1135 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1137 if (version_id
!= 1 && version_id
!= 2)
1139 if (cpu_ticks_enabled
) {
1142 cpu_ticks_offset
=qemu_get_be64(f
);
1143 ticks_per_sec
=qemu_get_be64(f
);
1144 if (version_id
== 2) {
1145 cpu_clock_offset
=qemu_get_be64(f
);
1151 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1152 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1154 static void host_alarm_handler(int host_signum
)
1158 #define DISP_FREQ 1000
1160 static int64_t delta_min
= INT64_MAX
;
1161 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1163 ti
= qemu_get_clock(vm_clock
);
1164 if (last_clock
!= 0) {
1165 delta
= ti
- last_clock
;
1166 if (delta
< delta_min
)
1168 if (delta
> delta_max
)
1171 if (++count
== DISP_FREQ
) {
1172 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1173 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1174 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1175 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1176 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1178 delta_min
= INT64_MAX
;
1186 if (alarm_has_dynticks(alarm_timer
) ||
1187 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1188 qemu_get_clock(vm_clock
)) ||
1189 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1190 qemu_get_clock(rt_clock
))) {
1192 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1193 SetEvent(data
->host_alarm
);
1195 CPUState
*env
= next_cpu
;
1197 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1200 /* stop the currently executing cpu because a timer occured */
1201 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1203 if (env
->kqemu_enabled
) {
1204 kqemu_cpu_interrupt(env
);
1212 static uint64_t qemu_next_deadline(void)
1214 int64_t nearest_delta_us
= INT64_MAX
;
1217 if (active_timers
[QEMU_TIMER_REALTIME
])
1218 nearest_delta_us
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1219 qemu_get_clock(rt_clock
))*1000;
1221 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1223 vmdelta_us
= (active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1224 qemu_get_clock(vm_clock
)+999)/1000;
1225 if (vmdelta_us
< nearest_delta_us
)
1226 nearest_delta_us
= vmdelta_us
;
1229 /* Avoid arming the timer to negative, zero, or too low values */
1230 if (nearest_delta_us
<= MIN_TIMER_REARM_US
)
1231 nearest_delta_us
= MIN_TIMER_REARM_US
;
1233 return nearest_delta_us
;
1238 #if defined(__linux__)
1240 #define RTC_FREQ 1024
1242 static void enable_sigio_timer(int fd
)
1244 struct sigaction act
;
1247 sigfillset(&act
.sa_mask
);
1249 act
.sa_handler
= host_alarm_handler
;
1251 sigaction(SIGIO
, &act
, NULL
);
1252 fcntl(fd
, F_SETFL
, O_ASYNC
);
1253 fcntl(fd
, F_SETOWN
, getpid());
1256 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1258 struct hpet_info info
;
1261 fd
= open("/dev/hpet", O_RDONLY
);
1266 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1268 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1269 "error, but for better emulation accuracy type:\n"
1270 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1274 /* Check capabilities */
1275 r
= ioctl(fd
, HPET_INFO
, &info
);
1279 /* Enable periodic mode */
1280 r
= ioctl(fd
, HPET_EPI
, 0);
1281 if (info
.hi_flags
&& (r
< 0))
1284 /* Enable interrupt */
1285 r
= ioctl(fd
, HPET_IE_ON
, 0);
1289 enable_sigio_timer(fd
);
1290 t
->priv
= (void *)(long)fd
;
1298 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1300 int fd
= (long)t
->priv
;
1305 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1308 unsigned long current_rtc_freq
= 0;
1310 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1313 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1314 if (current_rtc_freq
!= RTC_FREQ
&&
1315 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1316 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1317 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1318 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1321 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1327 enable_sigio_timer(rtc_fd
);
1329 t
->priv
= (void *)(long)rtc_fd
;
1334 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1336 int rtc_fd
= (long)t
->priv
;
1341 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1345 struct sigaction act
;
1347 sigfillset(&act
.sa_mask
);
1349 act
.sa_handler
= host_alarm_handler
;
1351 sigaction(SIGALRM
, &act
, NULL
);
1353 ev
.sigev_value
.sival_int
= 0;
1354 ev
.sigev_notify
= SIGEV_SIGNAL
;
1355 ev
.sigev_signo
= SIGALRM
;
1357 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1358 perror("timer_create");
1360 /* disable dynticks */
1361 fprintf(stderr
, "Dynamic Ticks disabled\n");
1366 t
->priv
= (void *)host_timer
;
1371 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1373 timer_t host_timer
= (timer_t
)t
->priv
;
1375 timer_delete(host_timer
);
1378 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1380 timer_t host_timer
= (timer_t
)t
->priv
;
1381 struct itimerspec timeout
;
1382 int64_t nearest_delta_us
= INT64_MAX
;
1385 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1386 !active_timers
[QEMU_TIMER_VIRTUAL
])
1389 nearest_delta_us
= qemu_next_deadline();
1391 /* check whether a timer is already running */
1392 if (timer_gettime(host_timer
, &timeout
)) {
1394 fprintf(stderr
, "Internal timer error: aborting\n");
1397 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1398 if (current_us
&& current_us
<= nearest_delta_us
)
1401 timeout
.it_interval
.tv_sec
= 0;
1402 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1403 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1404 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1405 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1407 fprintf(stderr
, "Internal timer error: aborting\n");
1412 #endif /* defined(__linux__) */
1414 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1416 struct sigaction act
;
1417 struct itimerval itv
;
1421 sigfillset(&act
.sa_mask
);
1423 act
.sa_handler
= host_alarm_handler
;
1425 sigaction(SIGALRM
, &act
, NULL
);
1427 itv
.it_interval
.tv_sec
= 0;
1428 /* for i386 kernel 2.6 to get 1 ms */
1429 itv
.it_interval
.tv_usec
= 999;
1430 itv
.it_value
.tv_sec
= 0;
1431 itv
.it_value
.tv_usec
= 10 * 1000;
1433 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1440 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1442 struct itimerval itv
;
1444 memset(&itv
, 0, sizeof(itv
));
1445 setitimer(ITIMER_REAL
, &itv
, NULL
);
1448 #endif /* !defined(_WIN32) */
1452 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1455 struct qemu_alarm_win32
*data
= t
->priv
;
1458 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1459 if (!data
->host_alarm
) {
1460 perror("Failed CreateEvent");
1464 memset(&tc
, 0, sizeof(tc
));
1465 timeGetDevCaps(&tc
, sizeof(tc
));
1467 if (data
->period
< tc
.wPeriodMin
)
1468 data
->period
= tc
.wPeriodMin
;
1470 timeBeginPeriod(data
->period
);
1472 flags
= TIME_CALLBACK_FUNCTION
;
1473 if (alarm_has_dynticks(t
))
1474 flags
|= TIME_ONESHOT
;
1476 flags
|= TIME_PERIODIC
;
1478 data
->timerId
= timeSetEvent(1, // interval (ms)
1479 data
->period
, // resolution
1480 host_alarm_handler
, // function
1481 (DWORD
)t
, // parameter
1484 if (!data
->timerId
) {
1485 perror("Failed to initialize win32 alarm timer");
1487 timeEndPeriod(data
->period
);
1488 CloseHandle(data
->host_alarm
);
1492 qemu_add_wait_object(data
->host_alarm
, NULL
, NULL
);
1497 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1499 struct qemu_alarm_win32
*data
= t
->priv
;
1501 timeKillEvent(data
->timerId
);
1502 timeEndPeriod(data
->period
);
1504 CloseHandle(data
->host_alarm
);
1507 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1509 struct qemu_alarm_win32
*data
= t
->priv
;
1510 uint64_t nearest_delta_us
;
1512 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1513 !active_timers
[QEMU_TIMER_VIRTUAL
])
1516 nearest_delta_us
= qemu_next_deadline();
1517 nearest_delta_us
/= 1000;
1519 timeKillEvent(data
->timerId
);
1521 data
->timerId
= timeSetEvent(1,
1525 TIME_ONESHOT
| TIME_PERIODIC
);
1527 if (!data
->timerId
) {
1528 perror("Failed to re-arm win32 alarm timer");
1530 timeEndPeriod(data
->period
);
1531 CloseHandle(data
->host_alarm
);
1538 static void init_timer_alarm(void)
1540 struct qemu_alarm_timer
*t
;
1543 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1544 t
= &alarm_timers
[i
];
1552 fprintf(stderr
, "Unable to find any suitable alarm timer.\n");
1553 fprintf(stderr
, "Terminating\n");
1560 static void quit_timers(void)
1562 alarm_timer
->stop(alarm_timer
);
1566 /***********************************************************/
1567 /* host time/date access */
1568 void qemu_get_timedate(struct tm
*tm
, int offset
)
1575 if (rtc_date_offset
== -1) {
1579 ret
= localtime(&ti
);
1581 ti
-= rtc_date_offset
;
1585 memcpy(tm
, ret
, sizeof(struct tm
));
1588 int qemu_timedate_diff(struct tm
*tm
)
1592 if (rtc_date_offset
== -1)
1594 seconds
= mktimegm(tm
);
1596 seconds
= mktime(tm
);
1598 seconds
= mktimegm(tm
) + rtc_date_offset
;
1600 return seconds
- time(NULL
);
1603 /***********************************************************/
1604 /* character device */
1606 static void qemu_chr_event(CharDriverState
*s
, int event
)
1610 s
->chr_event(s
->handler_opaque
, event
);
1613 static void qemu_chr_reset_bh(void *opaque
)
1615 CharDriverState
*s
= opaque
;
1616 qemu_chr_event(s
, CHR_EVENT_RESET
);
1617 qemu_bh_delete(s
->bh
);
1621 void qemu_chr_reset(CharDriverState
*s
)
1623 if (s
->bh
== NULL
) {
1624 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
1625 qemu_bh_schedule(s
->bh
);
1629 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1631 return s
->chr_write(s
, buf
, len
);
1634 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1638 return s
->chr_ioctl(s
, cmd
, arg
);
1641 int qemu_chr_can_read(CharDriverState
*s
)
1643 if (!s
->chr_can_read
)
1645 return s
->chr_can_read(s
->handler_opaque
);
1648 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
1650 s
->chr_read(s
->handler_opaque
, buf
, len
);
1653 void qemu_chr_accept_input(CharDriverState
*s
)
1655 if (s
->chr_accept_input
)
1656 s
->chr_accept_input(s
);
1659 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1664 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1665 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
1669 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1671 if (s
->chr_send_event
)
1672 s
->chr_send_event(s
, event
);
1675 void qemu_chr_add_handlers(CharDriverState
*s
,
1676 IOCanRWHandler
*fd_can_read
,
1677 IOReadHandler
*fd_read
,
1678 IOEventHandler
*fd_event
,
1681 s
->chr_can_read
= fd_can_read
;
1682 s
->chr_read
= fd_read
;
1683 s
->chr_event
= fd_event
;
1684 s
->handler_opaque
= opaque
;
1685 if (s
->chr_update_read_handler
)
1686 s
->chr_update_read_handler(s
);
1689 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1694 static CharDriverState
*qemu_chr_open_null(void)
1696 CharDriverState
*chr
;
1698 chr
= qemu_mallocz(sizeof(CharDriverState
));
1701 chr
->chr_write
= null_chr_write
;
1705 /* MUX driver for serial I/O splitting */
1706 static int term_timestamps
;
1707 static int64_t term_timestamps_start
;
1709 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
1710 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
1712 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
1713 IOReadHandler
*chr_read
[MAX_MUX
];
1714 IOEventHandler
*chr_event
[MAX_MUX
];
1715 void *ext_opaque
[MAX_MUX
];
1716 CharDriverState
*drv
;
1717 unsigned char buffer
[MUX_BUFFER_SIZE
];
1721 int term_got_escape
;
1726 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1728 MuxDriver
*d
= chr
->opaque
;
1730 if (!term_timestamps
) {
1731 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
1736 for(i
= 0; i
< len
; i
++) {
1737 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
1738 if (buf
[i
] == '\n') {
1744 if (term_timestamps_start
== -1)
1745 term_timestamps_start
= ti
;
1746 ti
-= term_timestamps_start
;
1747 secs
= ti
/ 1000000000;
1748 snprintf(buf1
, sizeof(buf1
),
1749 "[%02d:%02d:%02d.%03d] ",
1753 (int)((ti
/ 1000000) % 1000));
1754 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
1761 static char *mux_help
[] = {
1762 "% h print this help\n\r",
1763 "% x exit emulator\n\r",
1764 "% s save disk data back to file (if -snapshot)\n\r",
1765 "% t toggle console timestamps\n\r"
1766 "% b send break (magic sysrq)\n\r",
1767 "% c switch between console and monitor\n\r",
1772 static int term_escape_char
= 0x01; /* ctrl-a is used for escape */
1773 static void mux_print_help(CharDriverState
*chr
)
1776 char ebuf
[15] = "Escape-Char";
1777 char cbuf
[50] = "\n\r";
1779 if (term_escape_char
> 0 && term_escape_char
< 26) {
1780 sprintf(cbuf
,"\n\r");
1781 sprintf(ebuf
,"C-%c", term_escape_char
- 1 + 'a');
1783 sprintf(cbuf
,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
1786 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
1787 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
1788 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
1789 if (mux_help
[i
][j
] == '%')
1790 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
1792 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
1797 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
1799 if (d
->term_got_escape
) {
1800 d
->term_got_escape
= 0;
1801 if (ch
== term_escape_char
)
1806 mux_print_help(chr
);
1810 char *term
= "QEMU: Terminated\n\r";
1811 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
1818 for (i
= 0; i
< nb_drives
; i
++) {
1819 bdrv_commit(drives_table
[i
].bdrv
);
1824 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1827 /* Switch to the next registered device */
1829 if (chr
->focus
>= d
->mux_cnt
)
1833 term_timestamps
= !term_timestamps
;
1834 term_timestamps_start
= -1;
1837 } else if (ch
== term_escape_char
) {
1838 d
->term_got_escape
= 1;
1846 static void mux_chr_accept_input(CharDriverState
*chr
)
1849 MuxDriver
*d
= chr
->opaque
;
1851 while (d
->prod
!= d
->cons
&&
1852 d
->chr_can_read
[m
] &&
1853 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
1854 d
->chr_read
[m
](d
->ext_opaque
[m
],
1855 &d
->buffer
[d
->cons
++ & MUX_BUFFER_MASK
], 1);
1859 static int mux_chr_can_read(void *opaque
)
1861 CharDriverState
*chr
= opaque
;
1862 MuxDriver
*d
= chr
->opaque
;
1864 if ((d
->prod
- d
->cons
) < MUX_BUFFER_SIZE
)
1866 if (d
->chr_can_read
[chr
->focus
])
1867 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
1871 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
1873 CharDriverState
*chr
= opaque
;
1874 MuxDriver
*d
= chr
->opaque
;
1878 mux_chr_accept_input (opaque
);
1880 for(i
= 0; i
< size
; i
++)
1881 if (mux_proc_byte(chr
, d
, buf
[i
])) {
1882 if (d
->prod
== d
->cons
&&
1883 d
->chr_can_read
[m
] &&
1884 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
1885 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
1887 d
->buffer
[d
->prod
++ & MUX_BUFFER_MASK
] = buf
[i
];
1891 static void mux_chr_event(void *opaque
, int event
)
1893 CharDriverState
*chr
= opaque
;
1894 MuxDriver
*d
= chr
->opaque
;
1897 /* Send the event to all registered listeners */
1898 for (i
= 0; i
< d
->mux_cnt
; i
++)
1899 if (d
->chr_event
[i
])
1900 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
1903 static void mux_chr_update_read_handler(CharDriverState
*chr
)
1905 MuxDriver
*d
= chr
->opaque
;
1907 if (d
->mux_cnt
>= MAX_MUX
) {
1908 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
1911 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
1912 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
1913 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
1914 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
1915 /* Fix up the real driver with mux routines */
1916 if (d
->mux_cnt
== 0) {
1917 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
1918 mux_chr_event
, chr
);
1920 chr
->focus
= d
->mux_cnt
;
1924 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
1926 CharDriverState
*chr
;
1929 chr
= qemu_mallocz(sizeof(CharDriverState
));
1932 d
= qemu_mallocz(sizeof(MuxDriver
));
1941 chr
->chr_write
= mux_chr_write
;
1942 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
1943 chr
->chr_accept_input
= mux_chr_accept_input
;
1950 static void socket_cleanup(void)
1955 static int socket_init(void)
1960 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1962 err
= WSAGetLastError();
1963 fprintf(stderr
, "WSAStartup: %d\n", err
);
1966 atexit(socket_cleanup
);
1970 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1976 ret
= send(fd
, buf
, len
, 0);
1979 errno
= WSAGetLastError();
1980 if (errno
!= WSAEWOULDBLOCK
) {
1983 } else if (ret
== 0) {
1993 void socket_set_nonblock(int fd
)
1995 unsigned long opt
= 1;
1996 ioctlsocket(fd
, FIONBIO
, &opt
);
2001 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
2007 ret
= write(fd
, buf
, len
);
2009 if (errno
!= EINTR
&& errno
!= EAGAIN
)
2011 } else if (ret
== 0) {
2021 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
2023 return unix_write(fd
, buf
, len1
);
2026 void socket_set_nonblock(int fd
)
2028 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2030 #endif /* !_WIN32 */
2039 #define STDIO_MAX_CLIENTS 1
2040 static int stdio_nb_clients
= 0;
2042 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2044 FDCharDriver
*s
= chr
->opaque
;
2045 return unix_write(s
->fd_out
, buf
, len
);
2048 static int fd_chr_read_poll(void *opaque
)
2050 CharDriverState
*chr
= opaque
;
2051 FDCharDriver
*s
= chr
->opaque
;
2053 s
->max_size
= qemu_chr_can_read(chr
);
2057 static void fd_chr_read(void *opaque
)
2059 CharDriverState
*chr
= opaque
;
2060 FDCharDriver
*s
= chr
->opaque
;
2065 if (len
> s
->max_size
)
2069 size
= read(s
->fd_in
, buf
, len
);
2071 /* FD has been closed. Remove it from the active list. */
2072 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
2076 qemu_chr_read(chr
, buf
, size
);
2080 static void fd_chr_update_read_handler(CharDriverState
*chr
)
2082 FDCharDriver
*s
= chr
->opaque
;
2084 if (s
->fd_in
>= 0) {
2085 if (nographic
&& s
->fd_in
== 0) {
2087 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
2088 fd_chr_read
, NULL
, chr
);
2093 static void fd_chr_close(struct CharDriverState
*chr
)
2095 FDCharDriver
*s
= chr
->opaque
;
2097 if (s
->fd_in
>= 0) {
2098 if (nographic
&& s
->fd_in
== 0) {
2100 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
2107 /* open a character device to a unix fd */
2108 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
2110 CharDriverState
*chr
;
2113 chr
= qemu_mallocz(sizeof(CharDriverState
));
2116 s
= qemu_mallocz(sizeof(FDCharDriver
));
2124 chr
->chr_write
= fd_chr_write
;
2125 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
2126 chr
->chr_close
= fd_chr_close
;
2128 qemu_chr_reset(chr
);
2133 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
2137 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
2140 return qemu_chr_open_fd(-1, fd_out
);
2143 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
2146 char filename_in
[256], filename_out
[256];
2148 snprintf(filename_in
, 256, "%s.in", filename
);
2149 snprintf(filename_out
, 256, "%s.out", filename
);
2150 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
2151 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
2152 if (fd_in
< 0 || fd_out
< 0) {
2157 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
2161 return qemu_chr_open_fd(fd_in
, fd_out
);
2165 /* for STDIO, we handle the case where several clients use it
2168 #define TERM_FIFO_MAX_SIZE 1
2170 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
2171 static int term_fifo_size
;
2173 static int stdio_read_poll(void *opaque
)
2175 CharDriverState
*chr
= opaque
;
2177 /* try to flush the queue if needed */
2178 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
2179 qemu_chr_read(chr
, term_fifo
, 1);
2182 /* see if we can absorb more chars */
2183 if (term_fifo_size
== 0)
2189 static void stdio_read(void *opaque
)
2193 CharDriverState
*chr
= opaque
;
2195 size
= read(0, buf
, 1);
2197 /* stdin has been closed. Remove it from the active list. */
2198 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
2202 if (qemu_chr_can_read(chr
) > 0) {
2203 qemu_chr_read(chr
, buf
, 1);
2204 } else if (term_fifo_size
== 0) {
2205 term_fifo
[term_fifo_size
++] = buf
[0];
2210 /* init terminal so that we can grab keys */
2211 static struct termios oldtty
;
2212 static int old_fd0_flags
;
2213 static int term_atexit_done
;
2215 static void term_exit(void)
2217 tcsetattr (0, TCSANOW
, &oldtty
);
2218 fcntl(0, F_SETFL
, old_fd0_flags
);
2221 static void term_init(void)
2225 tcgetattr (0, &tty
);
2227 old_fd0_flags
= fcntl(0, F_GETFL
);
2229 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
2230 |INLCR
|IGNCR
|ICRNL
|IXON
);
2231 tty
.c_oflag
|= OPOST
;
2232 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
2233 /* if graphical mode, we allow Ctrl-C handling */
2235 tty
.c_lflag
&= ~ISIG
;
2236 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
2239 tty
.c_cc
[VTIME
] = 0;
2241 tcsetattr (0, TCSANOW
, &tty
);
2243 if (!term_atexit_done
++)
2246 fcntl(0, F_SETFL
, O_NONBLOCK
);
2249 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
2253 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
2257 static CharDriverState
*qemu_chr_open_stdio(void)
2259 CharDriverState
*chr
;
2261 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
2263 chr
= qemu_chr_open_fd(0, 1);
2264 chr
->chr_close
= qemu_chr_close_stdio
;
2265 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
2273 /* Once Solaris has openpty(), this is going to be removed. */
2274 int openpty(int *amaster
, int *aslave
, char *name
,
2275 struct termios
*termp
, struct winsize
*winp
)
2278 int mfd
= -1, sfd
= -1;
2280 *amaster
= *aslave
= -1;
2282 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
2286 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
2289 if ((slave
= ptsname(mfd
)) == NULL
)
2292 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
2295 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
2296 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
2304 ioctl(sfd
, TIOCSWINSZ
, winp
);
2315 void cfmakeraw (struct termios
*termios_p
)
2317 termios_p
->c_iflag
&=
2318 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
2319 termios_p
->c_oflag
&= ~OPOST
;
2320 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
2321 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
2322 termios_p
->c_cflag
|= CS8
;
2324 termios_p
->c_cc
[VMIN
] = 0;
2325 termios_p
->c_cc
[VTIME
] = 0;
2329 #if defined(__linux__) || defined(__sun__)
2330 static CharDriverState
*qemu_chr_open_pty(void)
2333 int master_fd
, slave_fd
;
2335 if (openpty(&master_fd
, &slave_fd
, NULL
, NULL
, NULL
) < 0) {
2339 /* Set raw attributes on the pty. */
2341 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
2343 fprintf(stderr
, "char device redirected to %s\n", ptsname(master_fd
));
2344 return qemu_chr_open_fd(master_fd
, master_fd
);
2347 static void tty_serial_init(int fd
, int speed
,
2348 int parity
, int data_bits
, int stop_bits
)
2354 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
2355 speed
, parity
, data_bits
, stop_bits
);
2357 tcgetattr (fd
, &tty
);
2360 if (speed
<= 50 * MARGIN
)
2362 else if (speed
<= 75 * MARGIN
)
2364 else if (speed
<= 300 * MARGIN
)
2366 else if (speed
<= 600 * MARGIN
)
2368 else if (speed
<= 1200 * MARGIN
)
2370 else if (speed
<= 2400 * MARGIN
)
2372 else if (speed
<= 4800 * MARGIN
)
2374 else if (speed
<= 9600 * MARGIN
)
2376 else if (speed
<= 19200 * MARGIN
)
2378 else if (speed
<= 38400 * MARGIN
)
2380 else if (speed
<= 57600 * MARGIN
)
2382 else if (speed
<= 115200 * MARGIN
)
2387 cfsetispeed(&tty
, spd
);
2388 cfsetospeed(&tty
, spd
);
2390 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
2391 |INLCR
|IGNCR
|ICRNL
|IXON
);
2392 tty
.c_oflag
|= OPOST
;
2393 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
2394 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
2415 tty
.c_cflag
|= PARENB
;
2418 tty
.c_cflag
|= PARENB
| PARODD
;
2422 tty
.c_cflag
|= CSTOPB
;
2424 tcsetattr (fd
, TCSANOW
, &tty
);
2427 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2429 FDCharDriver
*s
= chr
->opaque
;
2432 case CHR_IOCTL_SERIAL_SET_PARAMS
:
2434 QEMUSerialSetParams
*ssp
= arg
;
2435 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
2436 ssp
->data_bits
, ssp
->stop_bits
);
2439 case CHR_IOCTL_SERIAL_SET_BREAK
:
2441 int enable
= *(int *)arg
;
2443 tcsendbreak(s
->fd_in
, 1);
2452 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
2454 CharDriverState
*chr
;
2457 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
2458 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2459 tty_serial_init(fd
, 115200, 'N', 8, 1);
2460 chr
= qemu_chr_open_fd(fd
, fd
);
2465 chr
->chr_ioctl
= tty_serial_ioctl
;
2466 qemu_chr_reset(chr
);
2469 #else /* ! __linux__ && ! __sun__ */
2470 static CharDriverState
*qemu_chr_open_pty(void)
2474 #endif /* __linux__ || __sun__ */
2476 #if defined(__linux__)
2480 } ParallelCharDriver
;
2482 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
2484 if (s
->mode
!= mode
) {
2486 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
2493 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2495 ParallelCharDriver
*drv
= chr
->opaque
;
2500 case CHR_IOCTL_PP_READ_DATA
:
2501 if (ioctl(fd
, PPRDATA
, &b
) < 0)
2503 *(uint8_t *)arg
= b
;
2505 case CHR_IOCTL_PP_WRITE_DATA
:
2506 b
= *(uint8_t *)arg
;
2507 if (ioctl(fd
, PPWDATA
, &b
) < 0)
2510 case CHR_IOCTL_PP_READ_CONTROL
:
2511 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
2513 /* Linux gives only the lowest bits, and no way to know data
2514 direction! For better compatibility set the fixed upper
2516 *(uint8_t *)arg
= b
| 0xc0;
2518 case CHR_IOCTL_PP_WRITE_CONTROL
:
2519 b
= *(uint8_t *)arg
;
2520 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
2523 case CHR_IOCTL_PP_READ_STATUS
:
2524 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
2526 *(uint8_t *)arg
= b
;
2528 case CHR_IOCTL_PP_EPP_READ_ADDR
:
2529 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2530 struct ParallelIOArg
*parg
= arg
;
2531 int n
= read(fd
, parg
->buffer
, parg
->count
);
2532 if (n
!= parg
->count
) {
2537 case CHR_IOCTL_PP_EPP_READ
:
2538 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2539 struct ParallelIOArg
*parg
= arg
;
2540 int n
= read(fd
, parg
->buffer
, parg
->count
);
2541 if (n
!= parg
->count
) {
2546 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
2547 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2548 struct ParallelIOArg
*parg
= arg
;
2549 int n
= write(fd
, parg
->buffer
, parg
->count
);
2550 if (n
!= parg
->count
) {
2555 case CHR_IOCTL_PP_EPP_WRITE
:
2556 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2557 struct ParallelIOArg
*parg
= arg
;
2558 int n
= write(fd
, parg
->buffer
, parg
->count
);
2559 if (n
!= parg
->count
) {
2570 static void pp_close(CharDriverState
*chr
)
2572 ParallelCharDriver
*drv
= chr
->opaque
;
2575 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
2576 ioctl(fd
, PPRELEASE
);
2581 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
2583 CharDriverState
*chr
;
2584 ParallelCharDriver
*drv
;
2587 TFR(fd
= open(filename
, O_RDWR
));
2591 if (ioctl(fd
, PPCLAIM
) < 0) {
2596 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
2602 drv
->mode
= IEEE1284_MODE_COMPAT
;
2604 chr
= qemu_mallocz(sizeof(CharDriverState
));
2610 chr
->chr_write
= null_chr_write
;
2611 chr
->chr_ioctl
= pp_ioctl
;
2612 chr
->chr_close
= pp_close
;
2615 qemu_chr_reset(chr
);
2619 #endif /* __linux__ */
2625 HANDLE hcom
, hrecv
, hsend
;
2626 OVERLAPPED orecv
, osend
;
2631 #define NSENDBUF 2048
2632 #define NRECVBUF 2048
2633 #define MAXCONNECT 1
2634 #define NTIMEOUT 5000
2636 static int win_chr_poll(void *opaque
);
2637 static int win_chr_pipe_poll(void *opaque
);
2639 static void win_chr_close(CharDriverState
*chr
)
2641 WinCharState
*s
= chr
->opaque
;
2644 CloseHandle(s
->hsend
);
2648 CloseHandle(s
->hrecv
);
2652 CloseHandle(s
->hcom
);
2656 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
2658 qemu_del_polling_cb(win_chr_poll
, chr
);
2661 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
2663 WinCharState
*s
= chr
->opaque
;
2665 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2670 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2672 fprintf(stderr
, "Failed CreateEvent\n");
2675 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2677 fprintf(stderr
, "Failed CreateEvent\n");
2681 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2682 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2683 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2684 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
2689 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2690 fprintf(stderr
, "Failed SetupComm\n");
2694 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2695 size
= sizeof(COMMCONFIG
);
2696 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2697 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2698 CommConfigDialog(filename
, NULL
, &comcfg
);
2700 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2701 fprintf(stderr
, "Failed SetCommState\n");
2705 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2706 fprintf(stderr
, "Failed SetCommMask\n");
2710 cto
.ReadIntervalTimeout
= MAXDWORD
;
2711 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2712 fprintf(stderr
, "Failed SetCommTimeouts\n");
2716 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2717 fprintf(stderr
, "Failed ClearCommError\n");
2720 qemu_add_polling_cb(win_chr_poll
, chr
);
2728 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2730 WinCharState
*s
= chr
->opaque
;
2731 DWORD len
, ret
, size
, err
;
2734 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2735 s
->osend
.hEvent
= s
->hsend
;
2738 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2740 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2742 err
= GetLastError();
2743 if (err
== ERROR_IO_PENDING
) {
2744 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2762 static int win_chr_read_poll(CharDriverState
*chr
)
2764 WinCharState
*s
= chr
->opaque
;
2766 s
->max_size
= qemu_chr_can_read(chr
);
2770 static void win_chr_readfile(CharDriverState
*chr
)
2772 WinCharState
*s
= chr
->opaque
;
2777 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2778 s
->orecv
.hEvent
= s
->hrecv
;
2779 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2781 err
= GetLastError();
2782 if (err
== ERROR_IO_PENDING
) {
2783 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2788 qemu_chr_read(chr
, buf
, size
);
2792 static void win_chr_read(CharDriverState
*chr
)
2794 WinCharState
*s
= chr
->opaque
;
2796 if (s
->len
> s
->max_size
)
2797 s
->len
= s
->max_size
;
2801 win_chr_readfile(chr
);
2804 static int win_chr_poll(void *opaque
)
2806 CharDriverState
*chr
= opaque
;
2807 WinCharState
*s
= chr
->opaque
;
2811 ClearCommError(s
->hcom
, &comerr
, &status
);
2812 if (status
.cbInQue
> 0) {
2813 s
->len
= status
.cbInQue
;
2814 win_chr_read_poll(chr
);
2821 static CharDriverState
*qemu_chr_open_win(const char *filename
)
2823 CharDriverState
*chr
;
2826 chr
= qemu_mallocz(sizeof(CharDriverState
));
2829 s
= qemu_mallocz(sizeof(WinCharState
));
2835 chr
->chr_write
= win_chr_write
;
2836 chr
->chr_close
= win_chr_close
;
2838 if (win_chr_init(chr
, filename
) < 0) {
2843 qemu_chr_reset(chr
);
2847 static int win_chr_pipe_poll(void *opaque
)
2849 CharDriverState
*chr
= opaque
;
2850 WinCharState
*s
= chr
->opaque
;
2853 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2856 win_chr_read_poll(chr
);
2863 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
2865 WinCharState
*s
= chr
->opaque
;
2873 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2875 fprintf(stderr
, "Failed CreateEvent\n");
2878 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2880 fprintf(stderr
, "Failed CreateEvent\n");
2884 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2885 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2886 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2888 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2889 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2890 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2895 ZeroMemory(&ov
, sizeof(ov
));
2896 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2897 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2899 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2903 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2905 fprintf(stderr
, "Failed GetOverlappedResult\n");
2907 CloseHandle(ov
.hEvent
);
2914 CloseHandle(ov
.hEvent
);
2917 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2926 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2928 CharDriverState
*chr
;
2931 chr
= qemu_mallocz(sizeof(CharDriverState
));
2934 s
= qemu_mallocz(sizeof(WinCharState
));
2940 chr
->chr_write
= win_chr_write
;
2941 chr
->chr_close
= win_chr_close
;
2943 if (win_chr_pipe_init(chr
, filename
) < 0) {
2948 qemu_chr_reset(chr
);
2952 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2954 CharDriverState
*chr
;
2957 chr
= qemu_mallocz(sizeof(CharDriverState
));
2960 s
= qemu_mallocz(sizeof(WinCharState
));
2967 chr
->chr_write
= win_chr_write
;
2968 qemu_chr_reset(chr
);
2972 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
2974 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2977 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2981 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2982 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2983 if (fd_out
== INVALID_HANDLE_VALUE
)
2986 return qemu_chr_open_win_file(fd_out
);
2988 #endif /* !_WIN32 */
2990 /***********************************************************/
2991 /* UDP Net console */
2995 struct sockaddr_in daddr
;
3002 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
3004 NetCharDriver
*s
= chr
->opaque
;
3006 return sendto(s
->fd
, buf
, len
, 0,
3007 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
3010 static int udp_chr_read_poll(void *opaque
)
3012 CharDriverState
*chr
= opaque
;
3013 NetCharDriver
*s
= chr
->opaque
;
3015 s
->max_size
= qemu_chr_can_read(chr
);
3017 /* If there were any stray characters in the queue process them
3020 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
3021 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
3023 s
->max_size
= qemu_chr_can_read(chr
);
3028 static void udp_chr_read(void *opaque
)
3030 CharDriverState
*chr
= opaque
;
3031 NetCharDriver
*s
= chr
->opaque
;
3033 if (s
->max_size
== 0)
3035 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
3036 s
->bufptr
= s
->bufcnt
;
3041 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
3042 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
3044 s
->max_size
= qemu_chr_can_read(chr
);
3048 static void udp_chr_update_read_handler(CharDriverState
*chr
)
3050 NetCharDriver
*s
= chr
->opaque
;
3053 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
3054 udp_chr_read
, NULL
, chr
);
3058 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
3060 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
3062 int parse_host_src_port(struct sockaddr_in
*haddr
,
3063 struct sockaddr_in
*saddr
,
3066 static CharDriverState
*qemu_chr_open_udp(const char *def
)
3068 CharDriverState
*chr
= NULL
;
3069 NetCharDriver
*s
= NULL
;
3071 struct sockaddr_in saddr
;
3073 chr
= qemu_mallocz(sizeof(CharDriverState
));
3076 s
= qemu_mallocz(sizeof(NetCharDriver
));
3080 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
3082 perror("socket(PF_INET, SOCK_DGRAM)");
3086 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
3087 printf("Could not parse: %s\n", def
);
3091 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
3101 chr
->chr_write
= udp_chr_write
;
3102 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
3115 /***********************************************************/
3116 /* TCP Net console */
3127 static void tcp_chr_accept(void *opaque
);
3129 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
3131 TCPCharDriver
*s
= chr
->opaque
;
3133 return send_all(s
->fd
, buf
, len
);
3135 /* XXX: indicate an error ? */
3140 static int tcp_chr_read_poll(void *opaque
)
3142 CharDriverState
*chr
= opaque
;
3143 TCPCharDriver
*s
= chr
->opaque
;
3146 s
->max_size
= qemu_chr_can_read(chr
);
3151 #define IAC_BREAK 243
3152 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
3154 uint8_t *buf
, int *size
)
3156 /* Handle any telnet client's basic IAC options to satisfy char by
3157 * char mode with no echo. All IAC options will be removed from
3158 * the buf and the do_telnetopt variable will be used to track the
3159 * state of the width of the IAC information.
3161 * IAC commands come in sets of 3 bytes with the exception of the
3162 * "IAC BREAK" command and the double IAC.
3168 for (i
= 0; i
< *size
; i
++) {
3169 if (s
->do_telnetopt
> 1) {
3170 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
3171 /* Double IAC means send an IAC */
3175 s
->do_telnetopt
= 1;
3177 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
3178 /* Handle IAC break commands by sending a serial break */
3179 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
3184 if (s
->do_telnetopt
>= 4) {
3185 s
->do_telnetopt
= 1;
3188 if ((unsigned char)buf
[i
] == IAC
) {
3189 s
->do_telnetopt
= 2;
3200 static void tcp_chr_read(void *opaque
)
3202 CharDriverState
*chr
= opaque
;
3203 TCPCharDriver
*s
= chr
->opaque
;
3207 if (!s
->connected
|| s
->max_size
<= 0)
3210 if (len
> s
->max_size
)
3212 size
= recv(s
->fd
, buf
, len
, 0);
3214 /* connection closed */
3216 if (s
->listen_fd
>= 0) {
3217 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
3219 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3222 } else if (size
> 0) {
3223 if (s
->do_telnetopt
)
3224 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
3226 qemu_chr_read(chr
, buf
, size
);
3230 static void tcp_chr_connect(void *opaque
)
3232 CharDriverState
*chr
= opaque
;
3233 TCPCharDriver
*s
= chr
->opaque
;
3236 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
3237 tcp_chr_read
, NULL
, chr
);
3238 qemu_chr_reset(chr
);
3241 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3242 static void tcp_chr_telnet_init(int fd
)
3245 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3246 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3247 send(fd
, (char *)buf
, 3, 0);
3248 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3249 send(fd
, (char *)buf
, 3, 0);
3250 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3251 send(fd
, (char *)buf
, 3, 0);
3252 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3253 send(fd
, (char *)buf
, 3, 0);
3256 static void socket_set_nodelay(int fd
)
3259 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
3262 static void tcp_chr_accept(void *opaque
)
3264 CharDriverState
*chr
= opaque
;
3265 TCPCharDriver
*s
= chr
->opaque
;
3266 struct sockaddr_in saddr
;
3268 struct sockaddr_un uaddr
;
3270 struct sockaddr
*addr
;
3277 len
= sizeof(uaddr
);
3278 addr
= (struct sockaddr
*)&uaddr
;
3282 len
= sizeof(saddr
);
3283 addr
= (struct sockaddr
*)&saddr
;
3285 fd
= accept(s
->listen_fd
, addr
, &len
);
3286 if (fd
< 0 && errno
!= EINTR
) {
3288 } else if (fd
>= 0) {
3289 if (s
->do_telnetopt
)
3290 tcp_chr_telnet_init(fd
);
3294 socket_set_nonblock(fd
);
3296 socket_set_nodelay(fd
);
3298 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
3299 tcp_chr_connect(chr
);
3302 static void tcp_chr_close(CharDriverState
*chr
)
3304 TCPCharDriver
*s
= chr
->opaque
;
3307 if (s
->listen_fd
>= 0)
3308 closesocket(s
->listen_fd
);
3312 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
3316 CharDriverState
*chr
= NULL
;
3317 TCPCharDriver
*s
= NULL
;
3318 int fd
= -1, ret
, err
, val
;
3320 int is_waitconnect
= 1;
3323 struct sockaddr_in saddr
;
3325 struct sockaddr_un uaddr
;
3327 struct sockaddr
*addr
;
3332 addr
= (struct sockaddr
*)&uaddr
;
3333 addrlen
= sizeof(uaddr
);
3334 if (parse_unix_path(&uaddr
, host_str
) < 0)
3339 addr
= (struct sockaddr
*)&saddr
;
3340 addrlen
= sizeof(saddr
);
3341 if (parse_host_port(&saddr
, host_str
) < 0)
3346 while((ptr
= strchr(ptr
,','))) {
3348 if (!strncmp(ptr
,"server",6)) {
3350 } else if (!strncmp(ptr
,"nowait",6)) {
3352 } else if (!strncmp(ptr
,"nodelay",6)) {
3355 printf("Unknown option: %s\n", ptr
);
3362 chr
= qemu_mallocz(sizeof(CharDriverState
));
3365 s
= qemu_mallocz(sizeof(TCPCharDriver
));
3371 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
3374 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3379 if (!is_waitconnect
)
3380 socket_set_nonblock(fd
);
3385 s
->is_unix
= is_unix
;
3386 s
->do_nodelay
= do_nodelay
&& !is_unix
;
3389 chr
->chr_write
= tcp_chr_write
;
3390 chr
->chr_close
= tcp_chr_close
;
3393 /* allow fast reuse */
3397 pstrcpy(path
, sizeof(path
), uaddr
.sun_path
);
3403 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3406 ret
= bind(fd
, addr
, addrlen
);
3410 ret
= listen(fd
, 0);
3415 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
3417 s
->do_telnetopt
= 1;
3420 ret
= connect(fd
, addr
, addrlen
);
3422 err
= socket_error();
3423 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3424 } else if (err
== EINPROGRESS
) {
3427 } else if (err
== WSAEALREADY
) {
3439 socket_set_nodelay(fd
);
3441 tcp_chr_connect(chr
);
3443 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
3446 if (is_listen
&& is_waitconnect
) {
3447 printf("QEMU waiting for connection on: %s\n", host_str
);
3448 tcp_chr_accept(chr
);
3449 socket_set_nonblock(s
->listen_fd
);
3461 CharDriverState
*qemu_chr_open(const char *filename
)
3465 if (!strcmp(filename
, "vc")) {
3466 return text_console_init(&display_state
, 0);
3467 } else if (strstart(filename
, "vc:", &p
)) {
3468 return text_console_init(&display_state
, p
);
3469 } else if (!strcmp(filename
, "null")) {
3470 return qemu_chr_open_null();
3472 if (strstart(filename
, "tcp:", &p
)) {
3473 return qemu_chr_open_tcp(p
, 0, 0);
3475 if (strstart(filename
, "telnet:", &p
)) {
3476 return qemu_chr_open_tcp(p
, 1, 0);
3478 if (strstart(filename
, "udp:", &p
)) {
3479 return qemu_chr_open_udp(p
);
3481 if (strstart(filename
, "mon:", &p
)) {
3482 CharDriverState
*drv
= qemu_chr_open(p
);
3484 drv
= qemu_chr_open_mux(drv
);
3485 monitor_init(drv
, !nographic
);
3488 printf("Unable to open driver: %s\n", p
);
3492 if (strstart(filename
, "unix:", &p
)) {
3493 return qemu_chr_open_tcp(p
, 0, 1);
3494 } else if (strstart(filename
, "file:", &p
)) {
3495 return qemu_chr_open_file_out(p
);
3496 } else if (strstart(filename
, "pipe:", &p
)) {
3497 return qemu_chr_open_pipe(p
);
3498 } else if (!strcmp(filename
, "pty")) {
3499 return qemu_chr_open_pty();
3500 } else if (!strcmp(filename
, "stdio")) {
3501 return qemu_chr_open_stdio();
3503 #if defined(__linux__)
3504 if (strstart(filename
, "/dev/parport", NULL
)) {
3505 return qemu_chr_open_pp(filename
);
3508 #if defined(__linux__) || defined(__sun__)
3509 if (strstart(filename
, "/dev/", NULL
)) {
3510 return qemu_chr_open_tty(filename
);
3514 if (strstart(filename
, "COM", NULL
)) {
3515 return qemu_chr_open_win(filename
);
3517 if (strstart(filename
, "pipe:", &p
)) {
3518 return qemu_chr_open_win_pipe(p
);
3520 if (strstart(filename
, "con:", NULL
)) {
3521 return qemu_chr_open_win_con(filename
);
3523 if (strstart(filename
, "file:", &p
)) {
3524 return qemu_chr_open_win_file_out(p
);
3527 #ifdef CONFIG_BRLAPI
3528 if (!strcmp(filename
, "braille")) {
3529 return chr_baum_init();
3537 void qemu_chr_close(CharDriverState
*chr
)
3540 chr
->chr_close(chr
);
3544 /***********************************************************/
3545 /* network device redirectors */
3547 __attribute__ (( unused
))
3548 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
3552 for(i
=0;i
<size
;i
+=16) {
3556 fprintf(f
, "%08x ", i
);
3559 fprintf(f
, " %02x", buf
[i
+j
]);
3564 for(j
=0;j
<len
;j
++) {
3566 if (c
< ' ' || c
> '~')
3568 fprintf(f
, "%c", c
);
3574 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
3581 offset
= strtol(p
, &last_char
, 0);
3582 if (0 == errno
&& '\0' == *last_char
&&
3583 offset
>= 0 && offset
<= 0xFFFFFF) {
3584 macaddr
[3] = (offset
& 0xFF0000) >> 16;
3585 macaddr
[4] = (offset
& 0xFF00) >> 8;
3586 macaddr
[5] = offset
& 0xFF;
3589 for(i
= 0; i
< 6; i
++) {
3590 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3595 if (*p
!= ':' && *p
!= '-')
3606 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
3611 p1
= strchr(p
, sep
);
3617 if (len
> buf_size
- 1)
3619 memcpy(buf
, p
, len
);
3626 int parse_host_src_port(struct sockaddr_in
*haddr
,
3627 struct sockaddr_in
*saddr
,
3628 const char *input_str
)
3630 char *str
= strdup(input_str
);
3631 char *host_str
= str
;
3636 * Chop off any extra arguments at the end of the string which
3637 * would start with a comma, then fill in the src port information
3638 * if it was provided else use the "any address" and "any port".
3640 if ((ptr
= strchr(str
,',')))
3643 if ((src_str
= strchr(input_str
,'@'))) {
3648 if (parse_host_port(haddr
, host_str
) < 0)
3651 if (!src_str
|| *src_str
== '\0')
3654 if (parse_host_port(saddr
, src_str
) < 0)
3665 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
3673 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3675 saddr
->sin_family
= AF_INET
;
3676 if (buf
[0] == '\0') {
3677 saddr
->sin_addr
.s_addr
= 0;
3679 if (isdigit(buf
[0])) {
3680 if (!inet_aton(buf
, &saddr
->sin_addr
))
3683 if ((he
= gethostbyname(buf
)) == NULL
)
3685 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
3688 port
= strtol(p
, (char **)&r
, 0);
3691 saddr
->sin_port
= htons(port
);
3696 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
3701 len
= MIN(108, strlen(str
));
3702 p
= strchr(str
, ',');
3704 len
= MIN(len
, p
- str
);
3706 memset(uaddr
, 0, sizeof(*uaddr
));
3708 uaddr
->sun_family
= AF_UNIX
;
3709 memcpy(uaddr
->sun_path
, str
, len
);
3715 /* find or alloc a new VLAN */
3716 VLANState
*qemu_find_vlan(int id
)
3718 VLANState
**pvlan
, *vlan
;
3719 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3723 vlan
= qemu_mallocz(sizeof(VLANState
));
3728 pvlan
= &first_vlan
;
3729 while (*pvlan
!= NULL
)
3730 pvlan
= &(*pvlan
)->next
;
3735 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
3736 IOReadHandler
*fd_read
,
3737 IOCanRWHandler
*fd_can_read
,
3740 VLANClientState
*vc
, **pvc
;
3741 vc
= qemu_mallocz(sizeof(VLANClientState
));
3744 vc
->fd_read
= fd_read
;
3745 vc
->fd_can_read
= fd_can_read
;
3746 vc
->opaque
= opaque
;
3750 pvc
= &vlan
->first_client
;
3751 while (*pvc
!= NULL
)
3752 pvc
= &(*pvc
)->next
;
3757 int qemu_can_send_packet(VLANClientState
*vc1
)
3759 VLANState
*vlan
= vc1
->vlan
;
3760 VLANClientState
*vc
;
3762 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3764 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
3771 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
3773 VLANState
*vlan
= vc1
->vlan
;
3774 VLANClientState
*vc
;
3777 printf("vlan %d send:\n", vlan
->id
);
3778 hex_dump(stdout
, buf
, size
);
3780 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3782 vc
->fd_read(vc
->opaque
, buf
, size
);
3787 #if defined(CONFIG_SLIRP)
3789 /* slirp network adapter */
3791 static int slirp_inited
;
3792 static VLANClientState
*slirp_vc
;
3794 int slirp_can_output(void)
3796 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
3799 void slirp_output(const uint8_t *pkt
, int pkt_len
)
3802 printf("slirp output:\n");
3803 hex_dump(stdout
, pkt
, pkt_len
);
3807 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
3810 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
3813 printf("slirp input:\n");
3814 hex_dump(stdout
, buf
, size
);
3816 slirp_input(buf
, size
);
3819 static int net_slirp_init(VLANState
*vlan
)
3821 if (!slirp_inited
) {
3825 slirp_vc
= qemu_new_vlan_client(vlan
,
3826 slirp_receive
, NULL
, NULL
);
3827 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
3831 static void net_slirp_redir(const char *redir_str
)
3836 struct in_addr guest_addr
;
3837 int host_port
, guest_port
;
3839 if (!slirp_inited
) {
3845 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3847 if (!strcmp(buf
, "tcp")) {
3849 } else if (!strcmp(buf
, "udp")) {
3855 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3857 host_port
= strtol(buf
, &r
, 0);
3861 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3863 if (buf
[0] == '\0') {
3864 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
3866 if (!inet_aton(buf
, &guest_addr
))
3869 guest_port
= strtol(p
, &r
, 0);
3873 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3874 fprintf(stderr
, "qemu: could not set up redirection\n");
3879 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3887 static void erase_dir(char *dir_name
)
3891 char filename
[1024];
3893 /* erase all the files in the directory */
3894 if ((d
= opendir(dir_name
)) != 0) {
3899 if (strcmp(de
->d_name
, ".") != 0 &&
3900 strcmp(de
->d_name
, "..") != 0) {
3901 snprintf(filename
, sizeof(filename
), "%s/%s",
3902 smb_dir
, de
->d_name
);
3903 if (unlink(filename
) != 0) /* is it a directory? */
3904 erase_dir(filename
);
3912 /* automatic user mode samba server configuration */
3913 static void smb_exit(void)
3918 /* automatic user mode samba server configuration */
3919 static void net_slirp_smb(const char *exported_dir
)
3921 char smb_conf
[1024];
3922 char smb_cmdline
[1024];
3925 if (!slirp_inited
) {
3930 /* XXX: better tmp dir construction */
3931 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3932 if (mkdir(smb_dir
, 0700) < 0) {
3933 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3936 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3938 f
= fopen(smb_conf
, "w");
3940 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3947 "socket address=127.0.0.1\n"
3948 "pid directory=%s\n"
3949 "lock directory=%s\n"
3950 "log file=%s/log.smbd\n"
3951 "smb passwd file=%s/smbpasswd\n"
3952 "security = share\n"
3967 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3968 SMBD_COMMAND
, smb_conf
);
3970 slirp_add_exec(0, smb_cmdline
, 4, 139);
3973 #endif /* !defined(_WIN32) */
3974 void do_info_slirp(void)
3979 #endif /* CONFIG_SLIRP */
3981 #if !defined(_WIN32)
3983 typedef struct TAPState
{
3984 VLANClientState
*vc
;
3986 char down_script
[1024];
3989 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3991 TAPState
*s
= opaque
;
3994 ret
= write(s
->fd
, buf
, size
);
3995 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
4002 static void tap_send(void *opaque
)
4004 TAPState
*s
= opaque
;
4011 sbuf
.maxlen
= sizeof(buf
);
4013 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
4015 size
= read(s
->fd
, buf
, sizeof(buf
));
4018 qemu_send_packet(s
->vc
, buf
, size
);
4024 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
4028 s
= qemu_mallocz(sizeof(TAPState
));
4032 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
4033 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
4034 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
4038 #if defined (_BSD) || defined (__FreeBSD_kernel__)
4039 static int tap_open(char *ifname
, int ifname_size
)
4045 TFR(fd
= open("/dev/tap", O_RDWR
));
4047 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
4052 dev
= devname(s
.st_rdev
, S_IFCHR
);
4053 pstrcpy(ifname
, ifname_size
, dev
);
4055 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
4058 #elif defined(__sun__)
4059 #define TUNNEWPPA (('T'<<16) | 0x0001)
4061 * Allocate TAP device, returns opened fd.
4062 * Stores dev name in the first arg(must be large enough).
4064 int tap_alloc(char *dev
)
4066 int tap_fd
, if_fd
, ppa
= -1;
4067 static int ip_fd
= 0;
4070 static int arp_fd
= 0;
4071 int ip_muxid
, arp_muxid
;
4072 struct strioctl strioc_if
, strioc_ppa
;
4073 int link_type
= I_PLINK
;;
4075 char actual_name
[32] = "";
4077 memset(&ifr
, 0x0, sizeof(ifr
));
4081 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
4085 /* Check if IP device was opened */
4089 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
4091 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
4095 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
4097 syslog(LOG_ERR
, "Can't open /dev/tap");
4101 /* Assign a new PPA and get its unit number. */
4102 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
4103 strioc_ppa
.ic_timout
= 0;
4104 strioc_ppa
.ic_len
= sizeof(ppa
);
4105 strioc_ppa
.ic_dp
= (char *)&ppa
;
4106 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
4107 syslog (LOG_ERR
, "Can't assign new interface");
4109 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
4111 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
4114 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
4115 syslog(LOG_ERR
, "Can't push IP module");
4119 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
4120 syslog(LOG_ERR
, "Can't get flags\n");
4122 snprintf (actual_name
, 32, "tap%d", ppa
);
4123 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
4126 /* Assign ppa according to the unit number returned by tun device */
4128 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
4129 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
4130 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
4131 syslog (LOG_ERR
, "Can't get flags\n");
4132 /* Push arp module to if_fd */
4133 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
4134 syslog (LOG_ERR
, "Can't push ARP module (2)");
4136 /* Push arp module to ip_fd */
4137 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
4138 syslog (LOG_ERR
, "I_POP failed\n");
4139 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
4140 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
4142 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
4144 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
4146 /* Set ifname to arp */
4147 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
4148 strioc_if
.ic_timout
= 0;
4149 strioc_if
.ic_len
= sizeof(ifr
);
4150 strioc_if
.ic_dp
= (char *)&ifr
;
4151 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
4152 syslog (LOG_ERR
, "Can't set ifname to arp\n");
4155 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
4156 syslog(LOG_ERR
, "Can't link TAP device to IP");
4160 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
4161 syslog (LOG_ERR
, "Can't link TAP device to ARP");
4165 memset(&ifr
, 0x0, sizeof(ifr
));
4166 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
4167 ifr
.lifr_ip_muxid
= ip_muxid
;
4168 ifr
.lifr_arp_muxid
= arp_muxid
;
4170 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
4172 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
4173 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
4174 syslog (LOG_ERR
, "Can't set multiplexor id");
4177 sprintf(dev
, "tap%d", ppa
);
4181 static int tap_open(char *ifname
, int ifname_size
)
4185 if( (fd
= tap_alloc(dev
)) < 0 ){
4186 fprintf(stderr
, "Cannot allocate TAP device\n");
4189 pstrcpy(ifname
, ifname_size
, dev
);
4190 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
4194 static int tap_open(char *ifname
, int ifname_size
)
4199 TFR(fd
= open("/dev/net/tun", O_RDWR
));
4201 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
4204 memset(&ifr
, 0, sizeof(ifr
));
4205 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
4206 if (ifname
[0] != '\0')
4207 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
4209 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
4210 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
4212 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
4216 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
4217 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
4222 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
4228 /* try to launch network script */
4232 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
4233 for (i
= 0; i
< open_max
; i
++)
4234 if (i
!= STDIN_FILENO
&&
4235 i
!= STDOUT_FILENO
&&
4236 i
!= STDERR_FILENO
&&
4241 *parg
++ = (char *)setup_script
;
4242 *parg
++ = (char *)ifname
;
4244 execv(setup_script
, args
);
4247 while (waitpid(pid
, &status
, 0) != pid
);
4248 if (!WIFEXITED(status
) ||
4249 WEXITSTATUS(status
) != 0) {
4250 fprintf(stderr
, "%s: could not launch network script\n",
4258 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
4259 const char *setup_script
, const char *down_script
)
4265 if (ifname1
!= NULL
)
4266 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
4269 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
4273 if (!setup_script
|| !strcmp(setup_script
, "no"))
4275 if (setup_script
[0] != '\0') {
4276 if (launch_script(setup_script
, ifname
, fd
))
4279 s
= net_tap_fd_init(vlan
, fd
);
4282 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4283 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
4284 if (down_script
&& strcmp(down_script
, "no"))
4285 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
4289 #endif /* !_WIN32 */
4291 /* network connection */
4292 typedef struct NetSocketState
{
4293 VLANClientState
*vc
;
4295 int state
; /* 0 = getting length, 1 = getting data */
4299 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
4302 typedef struct NetSocketListenState
{
4305 } NetSocketListenState
;
4307 /* XXX: we consider we can send the whole packet without blocking */
4308 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
4310 NetSocketState
*s
= opaque
;
4314 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
4315 send_all(s
->fd
, buf
, size
);
4318 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
4320 NetSocketState
*s
= opaque
;
4321 sendto(s
->fd
, buf
, size
, 0,
4322 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
4325 static void net_socket_send(void *opaque
)
4327 NetSocketState
*s
= opaque
;
4332 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
4334 err
= socket_error();
4335 if (err
!= EWOULDBLOCK
)
4337 } else if (size
== 0) {
4338 /* end of connection */
4340 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
4346 /* reassemble a packet from the network */
4352 memcpy(s
->buf
+ s
->index
, buf
, l
);
4356 if (s
->index
== 4) {
4358 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
4364 l
= s
->packet_len
- s
->index
;
4367 memcpy(s
->buf
+ s
->index
, buf
, l
);
4371 if (s
->index
>= s
->packet_len
) {
4372 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
4381 static void net_socket_send_dgram(void *opaque
)
4383 NetSocketState
*s
= opaque
;
4386 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
4390 /* end of connection */
4391 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
4394 qemu_send_packet(s
->vc
, s
->buf
, size
);
4397 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
4402 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
4403 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
4404 inet_ntoa(mcastaddr
->sin_addr
),
4405 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
4409 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
4411 perror("socket(PF_INET, SOCK_DGRAM)");
4416 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
4417 (const char *)&val
, sizeof(val
));
4419 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
4423 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
4429 /* Add host to multicast group */
4430 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
4431 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
4433 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
4434 (const char *)&imr
, sizeof(struct ip_mreq
));
4436 perror("setsockopt(IP_ADD_MEMBERSHIP)");
4440 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
4442 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
4443 (const char *)&val
, sizeof(val
));
4445 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
4449 socket_set_nonblock(fd
);
4457 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
4460 struct sockaddr_in saddr
;
4462 socklen_t saddr_len
;
4465 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
4466 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
4467 * by ONLY ONE process: we must "clone" this dgram socket --jjo
4471 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
4473 if (saddr
.sin_addr
.s_addr
==0) {
4474 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
4478 /* clone dgram socket */
4479 newfd
= net_socket_mcast_create(&saddr
);
4481 /* error already reported by net_socket_mcast_create() */
4485 /* clone newfd to fd, close newfd */
4490 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
4491 fd
, strerror(errno
));
4496 s
= qemu_mallocz(sizeof(NetSocketState
));
4501 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
4502 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
4504 /* mcast: save bound address as dst */
4505 if (is_connected
) s
->dgram_dst
=saddr
;
4507 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4508 "socket: fd=%d (%s mcast=%s:%d)",
4509 fd
, is_connected
? "cloned" : "",
4510 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4514 static void net_socket_connect(void *opaque
)
4516 NetSocketState
*s
= opaque
;
4517 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
4520 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
4524 s
= qemu_mallocz(sizeof(NetSocketState
));
4528 s
->vc
= qemu_new_vlan_client(vlan
,
4529 net_socket_receive
, NULL
, s
);
4530 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4531 "socket: fd=%d", fd
);
4533 net_socket_connect(s
);
4535 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
4540 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
4543 int so_type
=-1, optlen
=sizeof(so_type
);
4545 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
4546 (socklen_t
*)&optlen
)< 0) {
4547 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
4552 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
4554 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
4556 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
4557 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
4558 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
4563 static void net_socket_accept(void *opaque
)
4565 NetSocketListenState
*s
= opaque
;
4567 struct sockaddr_in saddr
;
4572 len
= sizeof(saddr
);
4573 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
4574 if (fd
< 0 && errno
!= EINTR
) {
4576 } else if (fd
>= 0) {
4580 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
4584 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
4585 "socket: connection from %s:%d",
4586 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4590 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
4592 NetSocketListenState
*s
;
4594 struct sockaddr_in saddr
;
4596 if (parse_host_port(&saddr
, host_str
) < 0)
4599 s
= qemu_mallocz(sizeof(NetSocketListenState
));
4603 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4608 socket_set_nonblock(fd
);
4610 /* allow fast reuse */
4612 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
4614 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4619 ret
= listen(fd
, 0);
4626 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
4630 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
4633 int fd
, connected
, ret
, err
;
4634 struct sockaddr_in saddr
;
4636 if (parse_host_port(&saddr
, host_str
) < 0)
4639 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4644 socket_set_nonblock(fd
);
4648 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4650 err
= socket_error();
4651 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
4652 } else if (err
== EINPROGRESS
) {
4655 } else if (err
== WSAEALREADY
) {
4668 s
= net_socket_fd_init(vlan
, fd
, connected
);
4671 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4672 "socket: connect to %s:%d",
4673 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4677 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
4681 struct sockaddr_in saddr
;
4683 if (parse_host_port(&saddr
, host_str
) < 0)
4687 fd
= net_socket_mcast_create(&saddr
);
4691 s
= net_socket_fd_init(vlan
, fd
, 0);
4695 s
->dgram_dst
= saddr
;
4697 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4698 "socket: mcast=%s:%d",
4699 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4704 static const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
4709 while (*p
!= '\0' && *p
!= '=') {
4710 if (q
&& (q
- buf
) < buf_size
- 1)
4720 static const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
4725 while (*p
!= '\0') {
4727 if (*(p
+ 1) != ',')
4731 if (q
&& (q
- buf
) < buf_size
- 1)
4741 static int get_param_value(char *buf
, int buf_size
,
4742 const char *tag
, const char *str
)
4749 p
= get_opt_name(option
, sizeof(option
), p
);
4753 if (!strcmp(tag
, option
)) {
4754 (void)get_opt_value(buf
, buf_size
, p
);
4757 p
= get_opt_value(NULL
, 0, p
);
4766 static int check_params(char *buf
, int buf_size
,
4767 char **params
, const char *str
)
4774 p
= get_opt_name(buf
, buf_size
, p
);
4778 for(i
= 0; params
[i
] != NULL
; i
++)
4779 if (!strcmp(params
[i
], buf
))
4781 if (params
[i
] == NULL
)
4783 p
= get_opt_value(NULL
, 0, p
);
4792 static int net_client_init(const char *str
)
4803 while (*p
!= '\0' && *p
!= ',') {
4804 if ((q
- device
) < sizeof(device
) - 1)
4812 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
4813 vlan_id
= strtol(buf
, NULL
, 0);
4815 vlan
= qemu_find_vlan(vlan_id
);
4817 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
4820 if (!strcmp(device
, "nic")) {
4824 if (nb_nics
>= MAX_NICS
) {
4825 fprintf(stderr
, "Too Many NICs\n");
4828 nd
= &nd_table
[nb_nics
];
4829 macaddr
= nd
->macaddr
;
4835 macaddr
[5] = 0x56 + nb_nics
;
4837 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
4838 if (parse_macaddr(macaddr
, buf
) < 0) {
4839 fprintf(stderr
, "invalid syntax for ethernet address\n");
4843 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
4844 nd
->model
= strdup(buf
);
4848 vlan
->nb_guest_devs
++;
4851 if (!strcmp(device
, "none")) {
4852 /* does nothing. It is needed to signal that no network cards
4857 if (!strcmp(device
, "user")) {
4858 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
4859 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
4861 vlan
->nb_host_devs
++;
4862 ret
= net_slirp_init(vlan
);
4866 if (!strcmp(device
, "tap")) {
4868 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4869 fprintf(stderr
, "tap: no interface name\n");
4872 vlan
->nb_host_devs
++;
4873 ret
= tap_win32_init(vlan
, ifname
);
4876 if (!strcmp(device
, "tap")) {
4878 char setup_script
[1024], down_script
[1024];
4880 vlan
->nb_host_devs
++;
4881 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4882 fd
= strtol(buf
, NULL
, 0);
4883 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
4885 if (net_tap_fd_init(vlan
, fd
))
4888 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4891 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
4892 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
4894 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
4895 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
4897 ret
= net_tap_init(vlan
, ifname
, setup_script
, down_script
);
4901 if (!strcmp(device
, "socket")) {
4902 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4904 fd
= strtol(buf
, NULL
, 0);
4906 if (net_socket_fd_init(vlan
, fd
, 1))
4908 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
4909 ret
= net_socket_listen_init(vlan
, buf
);
4910 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
4911 ret
= net_socket_connect_init(vlan
, buf
);
4912 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
4913 ret
= net_socket_mcast_init(vlan
, buf
);
4915 fprintf(stderr
, "Unknown socket options: %s\n", p
);
4918 vlan
->nb_host_devs
++;
4921 fprintf(stderr
, "Unknown network device: %s\n", device
);
4925 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
4931 void do_info_network(void)
4934 VLANClientState
*vc
;
4936 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
4937 term_printf("VLAN %d devices:\n", vlan
->id
);
4938 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
4939 term_printf(" %s\n", vc
->info_str
);
4943 #define HD_ALIAS "index=%d,media=disk"
4945 #define CDROM_ALIAS "index=1,media=cdrom"
4947 #define CDROM_ALIAS "index=2,media=cdrom"
4949 #define FD_ALIAS "index=%d,if=floppy"
4950 #define PFLASH_ALIAS "if=pflash"
4951 #define MTD_ALIAS "if=mtd"
4952 #define SD_ALIAS "index=0,if=sd"
4954 static int drive_add(const char *file
, const char *fmt
, ...)
4958 if (nb_drives_opt
>= MAX_DRIVES
) {
4959 fprintf(stderr
, "qemu: too many drives\n");
4963 drives_opt
[nb_drives_opt
].file
= file
;
4965 vsnprintf(drives_opt
[nb_drives_opt
].opt
,
4966 sizeof(drives_opt
[0].opt
), fmt
, ap
);
4969 return nb_drives_opt
++;
4972 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
4976 /* seek interface, bus and unit */
4978 for (index
= 0; index
< nb_drives
; index
++)
4979 if (drives_table
[index
].type
== type
&&
4980 drives_table
[index
].bus
== bus
&&
4981 drives_table
[index
].unit
== unit
)
4987 int drive_get_max_bus(BlockInterfaceType type
)
4993 for (index
= 0; index
< nb_drives
; index
++) {
4994 if(drives_table
[index
].type
== type
&&
4995 drives_table
[index
].bus
> max_bus
)
4996 max_bus
= drives_table
[index
].bus
;
5001 static void bdrv_format_print(void *opaque
, const char *name
)
5003 fprintf(stderr
, " %s", name
);
5006 static int drive_init(struct drive_opt
*arg
, int snapshot
,
5007 QEMUMachine
*machine
)
5012 const char *mediastr
= "";
5013 BlockInterfaceType type
;
5014 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
5015 int bus_id
, unit_id
;
5016 int cyls
, heads
, secs
, translation
;
5017 BlockDriverState
*bdrv
;
5018 BlockDriver
*drv
= NULL
;
5023 char *str
= arg
->opt
;
5024 char *params
[] = { "bus", "unit", "if", "index", "cyls", "heads",
5025 "secs", "trans", "media", "snapshot", "file",
5026 "cache", "format", NULL
};
5028 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
5029 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
5035 cyls
= heads
= secs
= 0;
5038 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5042 if (!strcmp(machine
->name
, "realview") ||
5043 !strcmp(machine
->name
, "SS-5") ||
5044 !strcmp(machine
->name
, "SS-10") ||
5045 !strcmp(machine
->name
, "SS-600MP") ||
5046 !strcmp(machine
->name
, "versatilepb") ||
5047 !strcmp(machine
->name
, "versatileab")) {
5049 max_devs
= MAX_SCSI_DEVS
;
5050 strcpy(devname
, "scsi");
5053 max_devs
= MAX_IDE_DEVS
;
5054 strcpy(devname
, "ide");
5058 /* extract parameters */
5060 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
5061 bus_id
= strtol(buf
, NULL
, 0);
5063 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
5068 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
5069 unit_id
= strtol(buf
, NULL
, 0);
5071 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
5076 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
5077 pstrcpy(devname
, sizeof(devname
), buf
);
5078 if (!strcmp(buf
, "ide")) {
5080 max_devs
= MAX_IDE_DEVS
;
5081 } else if (!strcmp(buf
, "scsi")) {
5083 max_devs
= MAX_SCSI_DEVS
;
5084 } else if (!strcmp(buf
, "floppy")) {
5087 } else if (!strcmp(buf
, "pflash")) {
5090 } else if (!strcmp(buf
, "mtd")) {
5093 } else if (!strcmp(buf
, "sd")) {
5097 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
5102 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
5103 index
= strtol(buf
, NULL
, 0);
5105 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
5110 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
5111 cyls
= strtol(buf
, NULL
, 0);
5114 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
5115 heads
= strtol(buf
, NULL
, 0);
5118 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
5119 secs
= strtol(buf
, NULL
, 0);
5122 if (cyls
|| heads
|| secs
) {
5123 if (cyls
< 1 || cyls
> 16383) {
5124 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
5127 if (heads
< 1 || heads
> 16) {
5128 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
5131 if (secs
< 1 || secs
> 63) {
5132 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
5137 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
5140 "qemu: '%s' trans must be used with cyls,heads and secs\n",
5144 if (!strcmp(buf
, "none"))
5145 translation
= BIOS_ATA_TRANSLATION_NONE
;
5146 else if (!strcmp(buf
, "lba"))
5147 translation
= BIOS_ATA_TRANSLATION_LBA
;
5148 else if (!strcmp(buf
, "auto"))
5149 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5151 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
5156 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
5157 if (!strcmp(buf
, "disk")) {
5159 } else if (!strcmp(buf
, "cdrom")) {
5160 if (cyls
|| secs
|| heads
) {
5162 "qemu: '%s' invalid physical CHS format\n", str
);
5165 media
= MEDIA_CDROM
;
5167 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
5172 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
5173 if (!strcmp(buf
, "on"))
5175 else if (!strcmp(buf
, "off"))
5178 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
5183 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
5184 if (!strcmp(buf
, "off"))
5186 else if (!strcmp(buf
, "on"))
5189 fprintf(stderr
, "qemu: invalid cache option\n");
5194 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
5195 if (strcmp(buf
, "?") == 0) {
5196 fprintf(stderr
, "qemu: Supported formats:");
5197 bdrv_iterate_format(bdrv_format_print
, NULL
);
5198 fprintf(stderr
, "\n");
5201 drv
= bdrv_find_format(buf
);
5203 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
5208 if (arg
->file
== NULL
)
5209 get_param_value(file
, sizeof(file
), "file", str
);
5211 pstrcpy(file
, sizeof(file
), arg
->file
);
5213 /* compute bus and unit according index */
5216 if (bus_id
!= 0 || unit_id
!= -1) {
5218 "qemu: '%s' index cannot be used with bus and unit\n", str
);
5226 unit_id
= index
% max_devs
;
5227 bus_id
= index
/ max_devs
;
5231 /* if user doesn't specify a unit_id,
5232 * try to find the first free
5235 if (unit_id
== -1) {
5237 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
5239 if (max_devs
&& unit_id
>= max_devs
) {
5240 unit_id
-= max_devs
;
5248 if (max_devs
&& unit_id
>= max_devs
) {
5249 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
5250 str
, unit_id
, max_devs
- 1);
5255 * ignore multiple definitions
5258 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
5263 if (type
== IF_IDE
|| type
== IF_SCSI
)
5264 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
5266 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
5267 devname
, bus_id
, mediastr
, unit_id
);
5269 snprintf(buf
, sizeof(buf
), "%s%s%i",
5270 devname
, mediastr
, unit_id
);
5271 bdrv
= bdrv_new(buf
);
5272 drives_table
[nb_drives
].bdrv
= bdrv
;
5273 drives_table
[nb_drives
].type
= type
;
5274 drives_table
[nb_drives
].bus
= bus_id
;
5275 drives_table
[nb_drives
].unit
= unit_id
;
5284 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
5285 bdrv_set_translation_hint(bdrv
, translation
);
5289 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
5294 /* FIXME: This isn't really a floppy, but it's a reasonable
5297 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
5307 bdrv_flags
|= BDRV_O_SNAPSHOT
;
5309 bdrv_flags
|= BDRV_O_DIRECT
;
5310 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
5311 fprintf(stderr
, "qemu: could not open disk image %s\n",
5318 /***********************************************************/
5321 static USBPort
*used_usb_ports
;
5322 static USBPort
*free_usb_ports
;
5324 /* ??? Maybe change this to register a hub to keep track of the topology. */
5325 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
5326 usb_attachfn attach
)
5328 port
->opaque
= opaque
;
5329 port
->index
= index
;
5330 port
->attach
= attach
;
5331 port
->next
= free_usb_ports
;
5332 free_usb_ports
= port
;
5335 static int usb_device_add(const char *devname
)
5341 if (!free_usb_ports
)
5344 if (strstart(devname
, "host:", &p
)) {
5345 dev
= usb_host_device_open(p
);
5346 } else if (!strcmp(devname
, "mouse")) {
5347 dev
= usb_mouse_init();
5348 } else if (!strcmp(devname
, "tablet")) {
5349 dev
= usb_tablet_init();
5350 } else if (!strcmp(devname
, "keyboard")) {
5351 dev
= usb_keyboard_init();
5352 } else if (strstart(devname
, "disk:", &p
)) {
5353 dev
= usb_msd_init(p
);
5354 } else if (!strcmp(devname
, "wacom-tablet")) {
5355 dev
= usb_wacom_init();
5356 } else if (strstart(devname
, "serial:", &p
)) {
5357 dev
= usb_serial_init(p
);
5358 #ifdef CONFIG_BRLAPI
5359 } else if (!strcmp(devname
, "braille")) {
5360 dev
= usb_baum_init();
5368 /* Find a USB port to add the device to. */
5369 port
= free_usb_ports
;
5373 /* Create a new hub and chain it on. */
5374 free_usb_ports
= NULL
;
5375 port
->next
= used_usb_ports
;
5376 used_usb_ports
= port
;
5378 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
5379 usb_attach(port
, hub
);
5380 port
= free_usb_ports
;
5383 free_usb_ports
= port
->next
;
5384 port
->next
= used_usb_ports
;
5385 used_usb_ports
= port
;
5386 usb_attach(port
, dev
);
5390 static int usb_device_del(const char *devname
)
5398 if (!used_usb_ports
)
5401 p
= strchr(devname
, '.');
5404 bus_num
= strtoul(devname
, NULL
, 0);
5405 addr
= strtoul(p
+ 1, NULL
, 0);
5409 lastp
= &used_usb_ports
;
5410 port
= used_usb_ports
;
5411 while (port
&& port
->dev
->addr
!= addr
) {
5412 lastp
= &port
->next
;
5420 *lastp
= port
->next
;
5421 usb_attach(port
, NULL
);
5422 dev
->handle_destroy(dev
);
5423 port
->next
= free_usb_ports
;
5424 free_usb_ports
= port
;
5428 void do_usb_add(const char *devname
)
5431 ret
= usb_device_add(devname
);
5433 term_printf("Could not add USB device '%s'\n", devname
);
5436 void do_usb_del(const char *devname
)
5439 ret
= usb_device_del(devname
);
5441 term_printf("Could not remove USB device '%s'\n", devname
);
5448 const char *speed_str
;
5451 term_printf("USB support not enabled\n");
5455 for (port
= used_usb_ports
; port
; port
= port
->next
) {
5459 switch(dev
->speed
) {
5463 case USB_SPEED_FULL
:
5466 case USB_SPEED_HIGH
:
5473 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
5474 0, dev
->addr
, speed_str
, dev
->devname
);
5478 /***********************************************************/
5479 /* PCMCIA/Cardbus */
5481 static struct pcmcia_socket_entry_s
{
5482 struct pcmcia_socket_s
*socket
;
5483 struct pcmcia_socket_entry_s
*next
;
5484 } *pcmcia_sockets
= 0;
5486 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
5488 struct pcmcia_socket_entry_s
*entry
;
5490 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
5491 entry
->socket
= socket
;
5492 entry
->next
= pcmcia_sockets
;
5493 pcmcia_sockets
= entry
;
5496 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
5498 struct pcmcia_socket_entry_s
*entry
, **ptr
;
5500 ptr
= &pcmcia_sockets
;
5501 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
5502 if (entry
->socket
== socket
) {
5508 void pcmcia_info(void)
5510 struct pcmcia_socket_entry_s
*iter
;
5511 if (!pcmcia_sockets
)
5512 term_printf("No PCMCIA sockets\n");
5514 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
5515 term_printf("%s: %s\n", iter
->socket
->slot_string
,
5516 iter
->socket
->attached
? iter
->socket
->card_string
:
5520 /***********************************************************/
5523 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
5527 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
5531 static void dumb_refresh(DisplayState
*ds
)
5533 #if defined(CONFIG_SDL)
5538 static void dumb_display_init(DisplayState
*ds
)
5543 ds
->dpy_update
= dumb_update
;
5544 ds
->dpy_resize
= dumb_resize
;
5545 ds
->dpy_refresh
= dumb_refresh
;
5548 /***********************************************************/
5551 #define MAX_IO_HANDLERS 64
5553 typedef struct IOHandlerRecord
{
5555 IOCanRWHandler
*fd_read_poll
;
5557 IOHandler
*fd_write
;
5560 /* temporary data */
5562 struct IOHandlerRecord
*next
;
5565 static IOHandlerRecord
*first_io_handler
;
5567 /* XXX: fd_read_poll should be suppressed, but an API change is
5568 necessary in the character devices to suppress fd_can_read(). */
5569 int qemu_set_fd_handler2(int fd
,
5570 IOCanRWHandler
*fd_read_poll
,
5572 IOHandler
*fd_write
,
5575 IOHandlerRecord
**pioh
, *ioh
;
5577 if (!fd_read
&& !fd_write
) {
5578 pioh
= &first_io_handler
;
5583 if (ioh
->fd
== fd
) {
5590 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
5594 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
5597 ioh
->next
= first_io_handler
;
5598 first_io_handler
= ioh
;
5601 ioh
->fd_read_poll
= fd_read_poll
;
5602 ioh
->fd_read
= fd_read
;
5603 ioh
->fd_write
= fd_write
;
5604 ioh
->opaque
= opaque
;
5610 int qemu_set_fd_handler(int fd
,
5612 IOHandler
*fd_write
,
5615 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
5618 /***********************************************************/
5619 /* Polling handling */
5621 typedef struct PollingEntry
{
5624 struct PollingEntry
*next
;
5627 static PollingEntry
*first_polling_entry
;
5629 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
5631 PollingEntry
**ppe
, *pe
;
5632 pe
= qemu_mallocz(sizeof(PollingEntry
));
5636 pe
->opaque
= opaque
;
5637 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
5642 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
5644 PollingEntry
**ppe
, *pe
;
5645 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
5647 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
5656 /***********************************************************/
5657 /* Wait objects support */
5658 typedef struct WaitObjects
{
5660 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
5661 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
5662 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
5665 static WaitObjects wait_objects
= {0};
5667 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
5669 WaitObjects
*w
= &wait_objects
;
5671 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
5673 w
->events
[w
->num
] = handle
;
5674 w
->func
[w
->num
] = func
;
5675 w
->opaque
[w
->num
] = opaque
;
5680 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
5683 WaitObjects
*w
= &wait_objects
;
5686 for (i
= 0; i
< w
->num
; i
++) {
5687 if (w
->events
[i
] == handle
)
5690 w
->events
[i
] = w
->events
[i
+ 1];
5691 w
->func
[i
] = w
->func
[i
+ 1];
5692 w
->opaque
[i
] = w
->opaque
[i
+ 1];
5700 /***********************************************************/
5701 /* savevm/loadvm support */
5703 #define IO_BUF_SIZE 32768
5707 BlockDriverState
*bs
;
5710 int64_t base_offset
;
5711 int64_t buf_offset
; /* start of buffer when writing, end of buffer
5714 int buf_size
; /* 0 when writing */
5715 uint8_t buf
[IO_BUF_SIZE
];
5718 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
5722 f
= qemu_mallocz(sizeof(QEMUFile
));
5725 if (!strcmp(mode
, "wb")) {
5727 } else if (!strcmp(mode
, "rb")) {
5732 f
->outfile
= fopen(filename
, mode
);
5744 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
5748 f
= qemu_mallocz(sizeof(QEMUFile
));
5753 f
->is_writable
= is_writable
;
5754 f
->base_offset
= offset
;
5758 void qemu_fflush(QEMUFile
*f
)
5760 if (!f
->is_writable
)
5762 if (f
->buf_index
> 0) {
5764 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
5765 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
5767 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
5768 f
->buf
, f
->buf_index
);
5770 f
->buf_offset
+= f
->buf_index
;
5775 static void qemu_fill_buffer(QEMUFile
*f
)
5782 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
5783 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
5787 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
5788 f
->buf
, IO_BUF_SIZE
);
5794 f
->buf_offset
+= len
;
5797 void qemu_fclose(QEMUFile
*f
)
5807 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
5811 l
= IO_BUF_SIZE
- f
->buf_index
;
5814 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
5818 if (f
->buf_index
>= IO_BUF_SIZE
)
5823 void qemu_put_byte(QEMUFile
*f
, int v
)
5825 f
->buf
[f
->buf_index
++] = v
;
5826 if (f
->buf_index
>= IO_BUF_SIZE
)
5830 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
5836 l
= f
->buf_size
- f
->buf_index
;
5838 qemu_fill_buffer(f
);
5839 l
= f
->buf_size
- f
->buf_index
;
5845 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
5850 return size1
- size
;
5853 int qemu_get_byte(QEMUFile
*f
)
5855 if (f
->buf_index
>= f
->buf_size
) {
5856 qemu_fill_buffer(f
);
5857 if (f
->buf_index
>= f
->buf_size
)
5860 return f
->buf
[f
->buf_index
++];
5863 int64_t qemu_ftell(QEMUFile
*f
)
5865 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
5868 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
5870 if (whence
== SEEK_SET
) {
5872 } else if (whence
== SEEK_CUR
) {
5873 pos
+= qemu_ftell(f
);
5875 /* SEEK_END not supported */
5878 if (f
->is_writable
) {
5880 f
->buf_offset
= pos
;
5882 f
->buf_offset
= pos
;
5889 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
5891 qemu_put_byte(f
, v
>> 8);
5892 qemu_put_byte(f
, v
);
5895 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
5897 qemu_put_byte(f
, v
>> 24);
5898 qemu_put_byte(f
, v
>> 16);
5899 qemu_put_byte(f
, v
>> 8);
5900 qemu_put_byte(f
, v
);
5903 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
5905 qemu_put_be32(f
, v
>> 32);
5906 qemu_put_be32(f
, v
);
5909 unsigned int qemu_get_be16(QEMUFile
*f
)
5912 v
= qemu_get_byte(f
) << 8;
5913 v
|= qemu_get_byte(f
);
5917 unsigned int qemu_get_be32(QEMUFile
*f
)
5920 v
= qemu_get_byte(f
) << 24;
5921 v
|= qemu_get_byte(f
) << 16;
5922 v
|= qemu_get_byte(f
) << 8;
5923 v
|= qemu_get_byte(f
);
5927 uint64_t qemu_get_be64(QEMUFile
*f
)
5930 v
= (uint64_t)qemu_get_be32(f
) << 32;
5931 v
|= qemu_get_be32(f
);
5935 typedef struct SaveStateEntry
{
5939 SaveStateHandler
*save_state
;
5940 LoadStateHandler
*load_state
;
5942 struct SaveStateEntry
*next
;
5945 static SaveStateEntry
*first_se
;
5947 int register_savevm(const char *idstr
,
5950 SaveStateHandler
*save_state
,
5951 LoadStateHandler
*load_state
,
5954 SaveStateEntry
*se
, **pse
;
5956 se
= qemu_malloc(sizeof(SaveStateEntry
));
5959 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
5960 se
->instance_id
= instance_id
;
5961 se
->version_id
= version_id
;
5962 se
->save_state
= save_state
;
5963 se
->load_state
= load_state
;
5964 se
->opaque
= opaque
;
5967 /* add at the end of list */
5969 while (*pse
!= NULL
)
5970 pse
= &(*pse
)->next
;
5975 #define QEMU_VM_FILE_MAGIC 0x5145564d
5976 #define QEMU_VM_FILE_VERSION 0x00000002
5978 static int qemu_savevm_state(QEMUFile
*f
)
5982 int64_t cur_pos
, len_pos
, total_len_pos
;
5984 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
5985 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
5986 total_len_pos
= qemu_ftell(f
);
5987 qemu_put_be64(f
, 0); /* total size */
5989 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5991 len
= strlen(se
->idstr
);
5992 qemu_put_byte(f
, len
);
5993 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
5995 qemu_put_be32(f
, se
->instance_id
);
5996 qemu_put_be32(f
, se
->version_id
);
5998 /* record size: filled later */
5999 len_pos
= qemu_ftell(f
);
6000 qemu_put_be32(f
, 0);
6001 se
->save_state(f
, se
->opaque
);
6003 /* fill record size */
6004 cur_pos
= qemu_ftell(f
);
6005 len
= cur_pos
- len_pos
- 4;
6006 qemu_fseek(f
, len_pos
, SEEK_SET
);
6007 qemu_put_be32(f
, len
);
6008 qemu_fseek(f
, cur_pos
, SEEK_SET
);
6010 cur_pos
= qemu_ftell(f
);
6011 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
6012 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
6013 qemu_fseek(f
, cur_pos
, SEEK_SET
);
6019 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
6023 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
6024 if (!strcmp(se
->idstr
, idstr
) &&
6025 instance_id
== se
->instance_id
)
6031 static int qemu_loadvm_state(QEMUFile
*f
)
6034 int len
, ret
, instance_id
, record_len
, version_id
;
6035 int64_t total_len
, end_pos
, cur_pos
;
6039 v
= qemu_get_be32(f
);
6040 if (v
!= QEMU_VM_FILE_MAGIC
)
6042 v
= qemu_get_be32(f
);
6043 if (v
!= QEMU_VM_FILE_VERSION
) {
6048 total_len
= qemu_get_be64(f
);
6049 end_pos
= total_len
+ qemu_ftell(f
);
6051 if (qemu_ftell(f
) >= end_pos
)
6053 len
= qemu_get_byte(f
);
6054 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
6056 instance_id
= qemu_get_be32(f
);
6057 version_id
= qemu_get_be32(f
);
6058 record_len
= qemu_get_be32(f
);
6060 printf("idstr=%s instance=0x%x version=%d len=%d\n",
6061 idstr
, instance_id
, version_id
, record_len
);
6063 cur_pos
= qemu_ftell(f
);
6064 se
= find_se(idstr
, instance_id
);
6066 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
6067 instance_id
, idstr
);
6069 ret
= se
->load_state(f
, se
->opaque
, version_id
);
6071 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
6072 instance_id
, idstr
);
6075 /* always seek to exact end of record */
6076 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
6083 /* device can contain snapshots */
6084 static int bdrv_can_snapshot(BlockDriverState
*bs
)
6087 !bdrv_is_removable(bs
) &&
6088 !bdrv_is_read_only(bs
));
6091 /* device must be snapshots in order to have a reliable snapshot */
6092 static int bdrv_has_snapshot(BlockDriverState
*bs
)
6095 !bdrv_is_removable(bs
) &&
6096 !bdrv_is_read_only(bs
));
6099 static BlockDriverState
*get_bs_snapshots(void)
6101 BlockDriverState
*bs
;
6105 return bs_snapshots
;
6106 for(i
= 0; i
<= nb_drives
; i
++) {
6107 bs
= drives_table
[i
].bdrv
;
6108 if (bdrv_can_snapshot(bs
))
6117 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
6120 QEMUSnapshotInfo
*sn_tab
, *sn
;
6124 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
6127 for(i
= 0; i
< nb_sns
; i
++) {
6129 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
6139 void do_savevm(const char *name
)
6141 BlockDriverState
*bs
, *bs1
;
6142 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
6143 int must_delete
, ret
, i
;
6144 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
6146 int saved_vm_running
;
6153 bs
= get_bs_snapshots();
6155 term_printf("No block device can accept snapshots\n");
6159 /* ??? Should this occur after vm_stop? */
6162 saved_vm_running
= vm_running
;
6167 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
6172 memset(sn
, 0, sizeof(*sn
));
6174 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
6175 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
6178 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
6181 /* fill auxiliary fields */
6184 sn
->date_sec
= tb
.time
;
6185 sn
->date_nsec
= tb
.millitm
* 1000000;
6187 gettimeofday(&tv
, NULL
);
6188 sn
->date_sec
= tv
.tv_sec
;
6189 sn
->date_nsec
= tv
.tv_usec
* 1000;
6191 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
6193 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
6194 term_printf("Device %s does not support VM state snapshots\n",
6195 bdrv_get_device_name(bs
));
6199 /* save the VM state */
6200 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
6202 term_printf("Could not open VM state file\n");
6205 ret
= qemu_savevm_state(f
);
6206 sn
->vm_state_size
= qemu_ftell(f
);
6209 term_printf("Error %d while writing VM\n", ret
);
6213 /* create the snapshots */
6215 for(i
= 0; i
< nb_drives
; i
++) {
6216 bs1
= drives_table
[i
].bdrv
;
6217 if (bdrv_has_snapshot(bs1
)) {
6219 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
6221 term_printf("Error while deleting snapshot on '%s'\n",
6222 bdrv_get_device_name(bs1
));
6225 ret
= bdrv_snapshot_create(bs1
, sn
);
6227 term_printf("Error while creating snapshot on '%s'\n",
6228 bdrv_get_device_name(bs1
));
6234 if (saved_vm_running
)
6238 void do_loadvm(const char *name
)
6240 BlockDriverState
*bs
, *bs1
;
6241 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
6244 int saved_vm_running
;
6246 bs
= get_bs_snapshots();
6248 term_printf("No block device supports snapshots\n");
6252 /* Flush all IO requests so they don't interfere with the new state. */
6255 saved_vm_running
= vm_running
;
6258 for(i
= 0; i
<= nb_drives
; i
++) {
6259 bs1
= drives_table
[i
].bdrv
;
6260 if (bdrv_has_snapshot(bs1
)) {
6261 ret
= bdrv_snapshot_goto(bs1
, name
);
6264 term_printf("Warning: ");
6267 term_printf("Snapshots not supported on device '%s'\n",
6268 bdrv_get_device_name(bs1
));
6271 term_printf("Could not find snapshot '%s' on device '%s'\n",
6272 name
, bdrv_get_device_name(bs1
));
6275 term_printf("Error %d while activating snapshot on '%s'\n",
6276 ret
, bdrv_get_device_name(bs1
));
6279 /* fatal on snapshot block device */
6286 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
6287 term_printf("Device %s does not support VM state snapshots\n",
6288 bdrv_get_device_name(bs
));
6292 /* restore the VM state */
6293 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
6295 term_printf("Could not open VM state file\n");
6298 ret
= qemu_loadvm_state(f
);
6301 term_printf("Error %d while loading VM state\n", ret
);
6304 if (saved_vm_running
)
6308 void do_delvm(const char *name
)
6310 BlockDriverState
*bs
, *bs1
;
6313 bs
= get_bs_snapshots();
6315 term_printf("No block device supports snapshots\n");
6319 for(i
= 0; i
<= nb_drives
; i
++) {
6320 bs1
= drives_table
[i
].bdrv
;
6321 if (bdrv_has_snapshot(bs1
)) {
6322 ret
= bdrv_snapshot_delete(bs1
, name
);
6324 if (ret
== -ENOTSUP
)
6325 term_printf("Snapshots not supported on device '%s'\n",
6326 bdrv_get_device_name(bs1
));
6328 term_printf("Error %d while deleting snapshot on '%s'\n",
6329 ret
, bdrv_get_device_name(bs1
));
6335 void do_info_snapshots(void)
6337 BlockDriverState
*bs
, *bs1
;
6338 QEMUSnapshotInfo
*sn_tab
, *sn
;
6342 bs
= get_bs_snapshots();
6344 term_printf("No available block device supports snapshots\n");
6347 term_printf("Snapshot devices:");
6348 for(i
= 0; i
<= nb_drives
; i
++) {
6349 bs1
= drives_table
[i
].bdrv
;
6350 if (bdrv_has_snapshot(bs1
)) {
6352 term_printf(" %s", bdrv_get_device_name(bs1
));
6357 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
6359 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
6362 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
6363 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
6364 for(i
= 0; i
< nb_sns
; i
++) {
6366 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
6371 /***********************************************************/
6372 /* ram save/restore */
6374 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
6378 v
= qemu_get_byte(f
);
6381 if (qemu_get_buffer(f
, buf
, len
) != len
)
6385 v
= qemu_get_byte(f
);
6386 memset(buf
, v
, len
);
6394 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
6399 if (qemu_get_be32(f
) != phys_ram_size
)
6401 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
6402 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
6409 #define BDRV_HASH_BLOCK_SIZE 1024
6410 #define IOBUF_SIZE 4096
6411 #define RAM_CBLOCK_MAGIC 0xfabe
6413 typedef struct RamCompressState
{
6416 uint8_t buf
[IOBUF_SIZE
];
6419 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
6422 memset(s
, 0, sizeof(*s
));
6424 ret
= deflateInit2(&s
->zstream
, 1,
6426 9, Z_DEFAULT_STRATEGY
);
6429 s
->zstream
.avail_out
= IOBUF_SIZE
;
6430 s
->zstream
.next_out
= s
->buf
;
6434 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
6436 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
6437 qemu_put_be16(s
->f
, len
);
6438 qemu_put_buffer(s
->f
, buf
, len
);
6441 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
6445 s
->zstream
.avail_in
= len
;
6446 s
->zstream
.next_in
= (uint8_t *)buf
;
6447 while (s
->zstream
.avail_in
> 0) {
6448 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
6451 if (s
->zstream
.avail_out
== 0) {
6452 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
6453 s
->zstream
.avail_out
= IOBUF_SIZE
;
6454 s
->zstream
.next_out
= s
->buf
;
6460 static void ram_compress_close(RamCompressState
*s
)
6464 /* compress last bytes */
6466 ret
= deflate(&s
->zstream
, Z_FINISH
);
6467 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
6468 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
6470 ram_put_cblock(s
, s
->buf
, len
);
6472 s
->zstream
.avail_out
= IOBUF_SIZE
;
6473 s
->zstream
.next_out
= s
->buf
;
6474 if (ret
== Z_STREAM_END
)
6481 deflateEnd(&s
->zstream
);
6484 typedef struct RamDecompressState
{
6487 uint8_t buf
[IOBUF_SIZE
];
6488 } RamDecompressState
;
6490 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
6493 memset(s
, 0, sizeof(*s
));
6495 ret
= inflateInit(&s
->zstream
);
6501 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
6505 s
->zstream
.avail_out
= len
;
6506 s
->zstream
.next_out
= buf
;
6507 while (s
->zstream
.avail_out
> 0) {
6508 if (s
->zstream
.avail_in
== 0) {
6509 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
6511 clen
= qemu_get_be16(s
->f
);
6512 if (clen
> IOBUF_SIZE
)
6514 qemu_get_buffer(s
->f
, s
->buf
, clen
);
6515 s
->zstream
.avail_in
= clen
;
6516 s
->zstream
.next_in
= s
->buf
;
6518 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
6519 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
6526 static void ram_decompress_close(RamDecompressState
*s
)
6528 inflateEnd(&s
->zstream
);
6531 static void ram_save(QEMUFile
*f
, void *opaque
)
6534 RamCompressState s1
, *s
= &s1
;
6537 qemu_put_be32(f
, phys_ram_size
);
6538 if (ram_compress_open(s
, f
) < 0)
6540 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6542 if (tight_savevm_enabled
) {
6546 /* find if the memory block is available on a virtual
6549 for(j
= 0; j
< nb_drives
; j
++) {
6550 sector_num
= bdrv_hash_find(drives_table
[j
].bdrv
,
6552 BDRV_HASH_BLOCK_SIZE
);
6553 if (sector_num
>= 0)
6557 goto normal_compress
;
6560 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
6561 ram_compress_buf(s
, buf
, 10);
6567 ram_compress_buf(s
, buf
, 1);
6568 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6571 ram_compress_close(s
);
6574 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
6576 RamDecompressState s1
, *s
= &s1
;
6580 if (version_id
== 1)
6581 return ram_load_v1(f
, opaque
);
6582 if (version_id
!= 2)
6584 if (qemu_get_be32(f
) != phys_ram_size
)
6586 if (ram_decompress_open(s
, f
) < 0)
6588 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6589 if (ram_decompress_buf(s
, buf
, 1) < 0) {
6590 fprintf(stderr
, "Error while reading ram block header\n");
6594 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
6595 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
6604 ram_decompress_buf(s
, buf
+ 1, 9);
6606 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
6607 if (bs_index
>= nb_drives
) {
6608 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
6611 if (bdrv_read(drives_table
[bs_index
].bdrv
, sector_num
,
6613 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
6614 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
6615 bs_index
, sector_num
);
6622 printf("Error block header\n");
6626 ram_decompress_close(s
);
6630 /***********************************************************/
6631 /* bottom halves (can be seen as timers which expire ASAP) */
6640 static QEMUBH
*first_bh
= NULL
;
6642 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
6645 bh
= qemu_mallocz(sizeof(QEMUBH
));
6649 bh
->opaque
= opaque
;
6653 int qemu_bh_poll(void)
6672 void qemu_bh_schedule(QEMUBH
*bh
)
6674 CPUState
*env
= cpu_single_env
;
6678 bh
->next
= first_bh
;
6681 /* stop the currently executing CPU to execute the BH ASAP */
6683 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
6687 void qemu_bh_cancel(QEMUBH
*bh
)
6690 if (bh
->scheduled
) {
6693 pbh
= &(*pbh
)->next
;
6699 void qemu_bh_delete(QEMUBH
*bh
)
6705 /***********************************************************/
6706 /* machine registration */
6708 QEMUMachine
*first_machine
= NULL
;
6710 int qemu_register_machine(QEMUMachine
*m
)
6713 pm
= &first_machine
;
6721 static QEMUMachine
*find_machine(const char *name
)
6725 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6726 if (!strcmp(m
->name
, name
))
6732 /***********************************************************/
6733 /* main execution loop */
6735 static void gui_update(void *opaque
)
6737 DisplayState
*ds
= opaque
;
6738 ds
->dpy_refresh(ds
);
6739 qemu_mod_timer(ds
->gui_timer
,
6740 (ds
->gui_timer_interval
?
6741 ds
->gui_timer_interval
:
6742 GUI_REFRESH_INTERVAL
)
6743 + qemu_get_clock(rt_clock
));
6746 struct vm_change_state_entry
{
6747 VMChangeStateHandler
*cb
;
6749 LIST_ENTRY (vm_change_state_entry
) entries
;
6752 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
6754 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
6757 VMChangeStateEntry
*e
;
6759 e
= qemu_mallocz(sizeof (*e
));
6765 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
6769 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
6771 LIST_REMOVE (e
, entries
);
6775 static void vm_state_notify(int running
)
6777 VMChangeStateEntry
*e
;
6779 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
6780 e
->cb(e
->opaque
, running
);
6784 /* XXX: support several handlers */
6785 static VMStopHandler
*vm_stop_cb
;
6786 static void *vm_stop_opaque
;
6788 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6791 vm_stop_opaque
= opaque
;
6795 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6806 qemu_rearm_alarm_timer(alarm_timer
);
6810 void vm_stop(int reason
)
6813 cpu_disable_ticks();
6817 vm_stop_cb(vm_stop_opaque
, reason
);
6824 /* reset/shutdown handler */
6826 typedef struct QEMUResetEntry
{
6827 QEMUResetHandler
*func
;
6829 struct QEMUResetEntry
*next
;
6832 static QEMUResetEntry
*first_reset_entry
;
6833 static int reset_requested
;
6834 static int shutdown_requested
;
6835 static int powerdown_requested
;
6837 int qemu_shutdown_requested(void)
6839 int r
= shutdown_requested
;
6840 shutdown_requested
= 0;
6844 int qemu_reset_requested(void)
6846 int r
= reset_requested
;
6847 reset_requested
= 0;
6851 int qemu_powerdown_requested(void)
6853 int r
= powerdown_requested
;
6854 powerdown_requested
= 0;
6858 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
6860 QEMUResetEntry
**pre
, *re
;
6862 pre
= &first_reset_entry
;
6863 while (*pre
!= NULL
)
6864 pre
= &(*pre
)->next
;
6865 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
6867 re
->opaque
= opaque
;
6872 void qemu_system_reset(void)
6876 /* reset all devices */
6877 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
6878 re
->func(re
->opaque
);
6882 void qemu_system_reset_request(void)
6885 shutdown_requested
= 1;
6887 reset_requested
= 1;
6890 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6893 void qemu_system_shutdown_request(void)
6895 shutdown_requested
= 1;
6897 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6900 void qemu_system_powerdown_request(void)
6902 powerdown_requested
= 1;
6904 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6907 /* boot_set handler */
6908 QEMUBootSetHandler
*qemu_boot_set_handler
= NULL
;
6910 void qemu_register_boot_set(QEMUBootSetHandler
*func
)
6912 qemu_boot_set_handler
= func
;
6915 void main_loop_wait(int timeout
)
6917 IOHandlerRecord
*ioh
;
6918 fd_set rfds
, wfds
, xfds
;
6927 /* XXX: need to suppress polling by better using win32 events */
6929 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
6930 ret
|= pe
->func(pe
->opaque
);
6935 WaitObjects
*w
= &wait_objects
;
6937 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
6938 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
6939 if (w
->func
[ret
- WAIT_OBJECT_0
])
6940 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
6942 /* Check for additional signaled events */
6943 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
6945 /* Check if event is signaled */
6946 ret2
= WaitForSingleObject(w
->events
[i
], 0);
6947 if(ret2
== WAIT_OBJECT_0
) {
6949 w
->func
[i
](w
->opaque
[i
]);
6950 } else if (ret2
== WAIT_TIMEOUT
) {
6952 err
= GetLastError();
6953 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
6956 } else if (ret
== WAIT_TIMEOUT
) {
6958 err
= GetLastError();
6959 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
6963 /* poll any events */
6964 /* XXX: separate device handlers from system ones */
6969 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6973 (!ioh
->fd_read_poll
||
6974 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
6975 FD_SET(ioh
->fd
, &rfds
);
6979 if (ioh
->fd_write
) {
6980 FD_SET(ioh
->fd
, &wfds
);
6990 tv
.tv_usec
= timeout
* 1000;
6992 #if defined(CONFIG_SLIRP)
6994 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
6997 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
6999 IOHandlerRecord
**pioh
;
7001 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
7002 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
7003 ioh
->fd_read(ioh
->opaque
);
7005 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
7006 ioh
->fd_write(ioh
->opaque
);
7010 /* remove deleted IO handlers */
7011 pioh
= &first_io_handler
;
7021 #if defined(CONFIG_SLIRP)
7028 slirp_select_poll(&rfds
, &wfds
, &xfds
);
7034 if (likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
7035 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
7036 qemu_get_clock(vm_clock
));
7037 /* run dma transfers, if any */
7041 /* real time timers */
7042 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
7043 qemu_get_clock(rt_clock
));
7045 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
7046 alarm_timer
->flags
&= ~(ALARM_FLAG_EXPIRED
);
7047 qemu_rearm_alarm_timer(alarm_timer
);
7050 /* Check bottom-halves last in case any of the earlier events triggered
7056 static int main_loop(void)
7059 #ifdef CONFIG_PROFILER
7064 cur_cpu
= first_cpu
;
7065 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
7072 #ifdef CONFIG_PROFILER
7073 ti
= profile_getclock();
7075 ret
= cpu_exec(env
);
7076 #ifdef CONFIG_PROFILER
7077 qemu_time
+= profile_getclock() - ti
;
7079 next_cpu
= env
->next_cpu
?: first_cpu
;
7080 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
7081 ret
= EXCP_INTERRUPT
;
7085 if (ret
== EXCP_HLT
) {
7086 /* Give the next CPU a chance to run. */
7090 if (ret
!= EXCP_HALTED
)
7092 /* all CPUs are halted ? */
7098 if (shutdown_requested
) {
7099 ret
= EXCP_INTERRUPT
;
7107 if (reset_requested
) {
7108 reset_requested
= 0;
7109 qemu_system_reset();
7110 ret
= EXCP_INTERRUPT
;
7112 if (powerdown_requested
) {
7113 powerdown_requested
= 0;
7114 qemu_system_powerdown();
7115 ret
= EXCP_INTERRUPT
;
7117 if (unlikely(ret
== EXCP_DEBUG
)) {
7118 vm_stop(EXCP_DEBUG
);
7120 /* If all cpus are halted then wait until the next IRQ */
7121 /* XXX: use timeout computed from timers */
7122 if (ret
== EXCP_HALTED
)
7129 #ifdef CONFIG_PROFILER
7130 ti
= profile_getclock();
7132 main_loop_wait(timeout
);
7133 #ifdef CONFIG_PROFILER
7134 dev_time
+= profile_getclock() - ti
;
7137 cpu_disable_ticks();
7141 static void help(int exitcode
)
7143 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
7144 "usage: %s [options] [disk_image]\n"
7146 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
7148 "Standard options:\n"
7149 "-M machine select emulated machine (-M ? for list)\n"
7150 "-cpu cpu select CPU (-cpu ? for list)\n"
7151 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
7152 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
7153 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
7154 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
7155 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
7156 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
7157 " [,cache=on|off][,format=f]\n"
7158 " use 'file' as a drive image\n"
7159 "-mtdblock file use 'file' as on-board Flash memory image\n"
7160 "-sd file use 'file' as SecureDigital card image\n"
7161 "-pflash file use 'file' as a parallel flash image\n"
7162 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
7163 "-snapshot write to temporary files instead of disk image files\n"
7165 "-no-frame open SDL window without a frame and window decorations\n"
7166 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
7167 "-no-quit disable SDL window close capability\n"
7170 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
7172 "-m megs set virtual RAM size to megs MB [default=%d]\n"
7173 "-smp n set the number of CPUs to 'n' [default=1]\n"
7174 "-nographic disable graphical output and redirect serial I/Os to console\n"
7175 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
7177 "-k language use keyboard layout (for example \"fr\" for French)\n"
7180 "-audio-help print list of audio drivers and their options\n"
7181 "-soundhw c1,... enable audio support\n"
7182 " and only specified sound cards (comma separated list)\n"
7183 " use -soundhw ? to get the list of supported cards\n"
7184 " use -soundhw all to enable all of them\n"
7186 "-localtime set the real time clock to local time [default=utc]\n"
7187 "-full-screen start in full screen\n"
7189 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
7191 "-usb enable the USB driver (will be the default soon)\n"
7192 "-usbdevice name add the host or guest USB device 'name'\n"
7193 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7194 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
7196 "-name string set the name of the guest\n"
7198 "Network options:\n"
7199 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
7200 " create a new Network Interface Card and connect it to VLAN 'n'\n"
7202 "-net user[,vlan=n][,hostname=host]\n"
7203 " connect the user mode network stack to VLAN 'n' and send\n"
7204 " hostname 'host' to DHCP clients\n"
7207 "-net tap[,vlan=n],ifname=name\n"
7208 " connect the host TAP network interface to VLAN 'n'\n"
7210 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
7211 " connect the host TAP network interface to VLAN 'n' and use the\n"
7212 " network scripts 'file' (default=%s)\n"
7213 " and 'dfile' (default=%s);\n"
7214 " use '[down]script=no' to disable script execution;\n"
7215 " use 'fd=h' to connect to an already opened TAP interface\n"
7217 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
7218 " connect the vlan 'n' to another VLAN using a socket connection\n"
7219 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
7220 " connect the vlan 'n' to multicast maddr and port\n"
7221 "-net none use it alone to have zero network devices; if no -net option\n"
7222 " is provided, the default is '-net nic -net user'\n"
7225 "-tftp dir allow tftp access to files in dir [-net user]\n"
7226 "-bootp file advertise file in BOOTP replies\n"
7228 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
7230 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
7231 " redirect TCP or UDP connections from host to guest [-net user]\n"
7234 "Linux boot specific:\n"
7235 "-kernel bzImage use 'bzImage' as kernel image\n"
7236 "-append cmdline use 'cmdline' as kernel command line\n"
7237 "-initrd file use 'file' as initial ram disk\n"
7239 "Debug/Expert options:\n"
7240 "-monitor dev redirect the monitor to char device 'dev'\n"
7241 "-serial dev redirect the serial port to char device 'dev'\n"
7242 "-parallel dev redirect the parallel port to char device 'dev'\n"
7243 "-pidfile file Write PID to 'file'\n"
7244 "-S freeze CPU at startup (use 'c' to start execution)\n"
7245 "-s wait gdb connection to port\n"
7246 "-p port set gdb connection port [default=%s]\n"
7247 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
7248 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
7249 " translation (t=none or lba) (usually qemu can guess them)\n"
7250 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
7252 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
7253 "-no-kqemu disable KQEMU kernel module usage\n"
7256 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
7257 " (default is CL-GD5446 PCI VGA)\n"
7258 "-no-acpi disable ACPI\n"
7260 #ifdef CONFIG_CURSES
7261 "-curses use a curses/ncurses interface instead of SDL\n"
7263 "-no-reboot exit instead of rebooting\n"
7264 "-no-shutdown stop before shutdown\n"
7265 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
7266 "-vnc display start a VNC server on display\n"
7268 "-daemonize daemonize QEMU after initializing\n"
7270 "-option-rom rom load a file, rom, into the option ROM space\n"
7272 "-prom-env variable=value set OpenBIOS nvram variables\n"
7274 "-clock force the use of the given methods for timer alarm.\n"
7275 " To see what timers are available use -clock ?\n"
7276 "-startdate select initial date of the clock\n"
7278 "During emulation, the following keys are useful:\n"
7279 "ctrl-alt-f toggle full screen\n"
7280 "ctrl-alt-n switch to virtual console 'n'\n"
7281 "ctrl-alt toggle mouse and keyboard grab\n"
7283 "When using -nographic, press 'ctrl-a h' to get some help.\n"
7288 DEFAULT_NETWORK_SCRIPT
,
7289 DEFAULT_NETWORK_DOWN_SCRIPT
,
7291 DEFAULT_GDBSTUB_PORT
,
7296 #define HAS_ARG 0x0001
7311 QEMU_OPTION_mtdblock
,
7315 QEMU_OPTION_snapshot
,
7317 QEMU_OPTION_no_fd_bootchk
,
7320 QEMU_OPTION_nographic
,
7321 QEMU_OPTION_portrait
,
7323 QEMU_OPTION_audio_help
,
7324 QEMU_OPTION_soundhw
,
7345 QEMU_OPTION_localtime
,
7346 QEMU_OPTION_cirrusvga
,
7349 QEMU_OPTION_std_vga
,
7351 QEMU_OPTION_monitor
,
7353 QEMU_OPTION_parallel
,
7355 QEMU_OPTION_full_screen
,
7356 QEMU_OPTION_no_frame
,
7357 QEMU_OPTION_alt_grab
,
7358 QEMU_OPTION_no_quit
,
7359 QEMU_OPTION_pidfile
,
7360 QEMU_OPTION_no_kqemu
,
7361 QEMU_OPTION_kernel_kqemu
,
7362 QEMU_OPTION_win2k_hack
,
7364 QEMU_OPTION_usbdevice
,
7367 QEMU_OPTION_no_acpi
,
7369 QEMU_OPTION_no_reboot
,
7370 QEMU_OPTION_no_shutdown
,
7371 QEMU_OPTION_show_cursor
,
7372 QEMU_OPTION_daemonize
,
7373 QEMU_OPTION_option_rom
,
7374 QEMU_OPTION_semihosting
,
7376 QEMU_OPTION_prom_env
,
7377 QEMU_OPTION_old_param
,
7379 QEMU_OPTION_startdate
,
7380 QEMU_OPTION_tb_size
,
7383 typedef struct QEMUOption
{
7389 const QEMUOption qemu_options
[] = {
7390 { "h", 0, QEMU_OPTION_h
},
7391 { "help", 0, QEMU_OPTION_h
},
7393 { "M", HAS_ARG
, QEMU_OPTION_M
},
7394 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
7395 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
7396 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
7397 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
7398 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
7399 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
7400 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
7401 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
7402 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
7403 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
7404 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
7405 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
7406 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
7407 { "snapshot", 0, QEMU_OPTION_snapshot
},
7409 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
7411 { "m", HAS_ARG
, QEMU_OPTION_m
},
7412 { "nographic", 0, QEMU_OPTION_nographic
},
7413 { "portrait", 0, QEMU_OPTION_portrait
},
7414 { "k", HAS_ARG
, QEMU_OPTION_k
},
7416 { "audio-help", 0, QEMU_OPTION_audio_help
},
7417 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
7420 { "net", HAS_ARG
, QEMU_OPTION_net
},
7422 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
7423 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
7425 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
7427 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
7430 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
7431 { "append", HAS_ARG
, QEMU_OPTION_append
},
7432 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
7434 { "S", 0, QEMU_OPTION_S
},
7435 { "s", 0, QEMU_OPTION_s
},
7436 { "p", HAS_ARG
, QEMU_OPTION_p
},
7437 { "d", HAS_ARG
, QEMU_OPTION_d
},
7438 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
7439 { "L", HAS_ARG
, QEMU_OPTION_L
},
7440 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
7442 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
7443 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
7445 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7446 { "g", 1, QEMU_OPTION_g
},
7448 { "localtime", 0, QEMU_OPTION_localtime
},
7449 { "std-vga", 0, QEMU_OPTION_std_vga
},
7450 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
7451 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
7452 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
7453 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
7454 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
7455 { "full-screen", 0, QEMU_OPTION_full_screen
},
7457 { "no-frame", 0, QEMU_OPTION_no_frame
},
7458 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
7459 { "no-quit", 0, QEMU_OPTION_no_quit
},
7461 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
7462 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
7463 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
7464 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
7465 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
7466 #ifdef CONFIG_CURSES
7467 { "curses", 0, QEMU_OPTION_curses
},
7470 /* temporary options */
7471 { "usb", 0, QEMU_OPTION_usb
},
7472 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
7473 { "vmwarevga", 0, QEMU_OPTION_vmsvga
},
7474 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
7475 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
7476 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
7477 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
7478 { "daemonize", 0, QEMU_OPTION_daemonize
},
7479 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
7480 #if defined(TARGET_ARM) || defined(TARGET_M68K)
7481 { "semihosting", 0, QEMU_OPTION_semihosting
},
7483 { "name", HAS_ARG
, QEMU_OPTION_name
},
7484 #if defined(TARGET_SPARC)
7485 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
7487 #if defined(TARGET_ARM)
7488 { "old-param", 0, QEMU_OPTION_old_param
},
7490 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
7491 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
7492 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
7496 /* password input */
7498 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
7503 if (!bdrv_is_encrypted(bs
))
7506 term_printf("%s is encrypted.\n", name
);
7507 for(i
= 0; i
< 3; i
++) {
7508 monitor_readline("Password: ", 1, password
, sizeof(password
));
7509 if (bdrv_set_key(bs
, password
) == 0)
7511 term_printf("invalid password\n");
7516 static BlockDriverState
*get_bdrv(int index
)
7518 if (index
> nb_drives
)
7520 return drives_table
[index
].bdrv
;
7523 static void read_passwords(void)
7525 BlockDriverState
*bs
;
7528 for(i
= 0; i
< 6; i
++) {
7531 qemu_key_check(bs
, bdrv_get_device_name(bs
));
7536 struct soundhw soundhw
[] = {
7537 #ifdef HAS_AUDIO_CHOICE
7538 #if defined(TARGET_I386) || defined(TARGET_MIPS)
7544 { .init_isa
= pcspk_audio_init
}
7549 "Creative Sound Blaster 16",
7552 { .init_isa
= SB16_init
}
7559 "Yamaha YMF262 (OPL3)",
7561 "Yamaha YM3812 (OPL2)",
7565 { .init_isa
= Adlib_init
}
7572 "Gravis Ultrasound GF1",
7575 { .init_isa
= GUS_init
}
7582 "Intel 82801AA AC97 Audio",
7585 { .init_pci
= ac97_init
}
7591 "ENSONIQ AudioPCI ES1370",
7594 { .init_pci
= es1370_init
}
7598 { NULL
, NULL
, 0, 0, { NULL
} }
7601 static void select_soundhw (const char *optarg
)
7605 if (*optarg
== '?') {
7608 printf ("Valid sound card names (comma separated):\n");
7609 for (c
= soundhw
; c
->name
; ++c
) {
7610 printf ("%-11s %s\n", c
->name
, c
->descr
);
7612 printf ("\n-soundhw all will enable all of the above\n");
7613 exit (*optarg
!= '?');
7621 if (!strcmp (optarg
, "all")) {
7622 for (c
= soundhw
; c
->name
; ++c
) {
7630 e
= strchr (p
, ',');
7631 l
= !e
? strlen (p
) : (size_t) (e
- p
);
7633 for (c
= soundhw
; c
->name
; ++c
) {
7634 if (!strncmp (c
->name
, p
, l
)) {
7643 "Unknown sound card name (too big to show)\n");
7646 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
7651 p
+= l
+ (e
!= NULL
);
7655 goto show_valid_cards
;
7661 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
7663 exit(STATUS_CONTROL_C_EXIT
);
7668 #define MAX_NET_CLIENTS 32
7670 int main(int argc
, char **argv
)
7672 #ifdef CONFIG_GDBSTUB
7674 const char *gdbstub_port
;
7676 uint32_t boot_devices_bitmap
= 0;
7678 int snapshot
, linux_boot
, net_boot
;
7679 const char *initrd_filename
;
7680 const char *kernel_filename
, *kernel_cmdline
;
7681 const char *boot_devices
= "";
7682 DisplayState
*ds
= &display_state
;
7683 int cyls
, heads
, secs
, translation
;
7684 const char *net_clients
[MAX_NET_CLIENTS
];
7688 const char *r
, *optarg
;
7689 CharDriverState
*monitor_hd
;
7690 const char *monitor_device
;
7691 const char *serial_devices
[MAX_SERIAL_PORTS
];
7692 int serial_device_index
;
7693 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
7694 int parallel_device_index
;
7695 const char *loadvm
= NULL
;
7696 QEMUMachine
*machine
;
7697 const char *cpu_model
;
7698 const char *usb_devices
[MAX_USB_CMDLINE
];
7699 int usb_devices_index
;
7702 const char *pid_file
= NULL
;
7705 LIST_INIT (&vm_change_state_head
);
7708 struct sigaction act
;
7709 sigfillset(&act
.sa_mask
);
7711 act
.sa_handler
= SIG_IGN
;
7712 sigaction(SIGPIPE
, &act
, NULL
);
7715 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
7716 /* Note: cpu_interrupt() is currently not SMP safe, so we force
7717 QEMU to run on a single CPU */
7722 h
= GetCurrentProcess();
7723 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
7724 for(i
= 0; i
< 32; i
++) {
7725 if (mask
& (1 << i
))
7730 SetProcessAffinityMask(h
, mask
);
7736 register_machines();
7737 machine
= first_machine
;
7739 initrd_filename
= NULL
;
7741 vga_ram_size
= VGA_RAM_SIZE
;
7742 #ifdef CONFIG_GDBSTUB
7744 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
7749 kernel_filename
= NULL
;
7750 kernel_cmdline
= "";
7751 cyls
= heads
= secs
= 0;
7752 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7753 monitor_device
= "vc:800x600";
7755 serial_devices
[0] = "vc:80Cx24C";
7756 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
7757 serial_devices
[i
] = NULL
;
7758 serial_device_index
= 0;
7760 parallel_devices
[0] = "vc:640x480";
7761 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
7762 parallel_devices
[i
] = NULL
;
7763 parallel_device_index
= 0;
7765 usb_devices_index
= 0;
7782 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
7784 const QEMUOption
*popt
;
7787 /* Treat --foo the same as -foo. */
7790 popt
= qemu_options
;
7793 fprintf(stderr
, "%s: invalid option -- '%s'\n",
7797 if (!strcmp(popt
->name
, r
+ 1))
7801 if (popt
->flags
& HAS_ARG
) {
7802 if (optind
>= argc
) {
7803 fprintf(stderr
, "%s: option '%s' requires an argument\n",
7807 optarg
= argv
[optind
++];
7812 switch(popt
->index
) {
7814 machine
= find_machine(optarg
);
7817 printf("Supported machines are:\n");
7818 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
7819 printf("%-10s %s%s\n",
7821 m
== first_machine
? " (default)" : "");
7823 exit(*optarg
!= '?');
7826 case QEMU_OPTION_cpu
:
7827 /* hw initialization will check this */
7828 if (*optarg
== '?') {
7829 /* XXX: implement xxx_cpu_list for targets that still miss it */
7830 #if defined(cpu_list)
7831 cpu_list(stdout
, &fprintf
);
7838 case QEMU_OPTION_initrd
:
7839 initrd_filename
= optarg
;
7841 case QEMU_OPTION_hda
:
7843 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
7845 hda_index
= drive_add(optarg
, HD_ALIAS
7846 ",cyls=%d,heads=%d,secs=%d%s",
7847 0, cyls
, heads
, secs
,
7848 translation
== BIOS_ATA_TRANSLATION_LBA
?
7850 translation
== BIOS_ATA_TRANSLATION_NONE
?
7851 ",trans=none" : "");
7853 case QEMU_OPTION_hdb
:
7854 case QEMU_OPTION_hdc
:
7855 case QEMU_OPTION_hdd
:
7856 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
7858 case QEMU_OPTION_drive
:
7859 drive_add(NULL
, "%s", optarg
);
7861 case QEMU_OPTION_mtdblock
:
7862 drive_add(optarg
, MTD_ALIAS
);
7864 case QEMU_OPTION_sd
:
7865 drive_add(optarg
, SD_ALIAS
);
7867 case QEMU_OPTION_pflash
:
7868 drive_add(optarg
, PFLASH_ALIAS
);
7870 case QEMU_OPTION_snapshot
:
7873 case QEMU_OPTION_hdachs
:
7877 cyls
= strtol(p
, (char **)&p
, 0);
7878 if (cyls
< 1 || cyls
> 16383)
7883 heads
= strtol(p
, (char **)&p
, 0);
7884 if (heads
< 1 || heads
> 16)
7889 secs
= strtol(p
, (char **)&p
, 0);
7890 if (secs
< 1 || secs
> 63)
7894 if (!strcmp(p
, "none"))
7895 translation
= BIOS_ATA_TRANSLATION_NONE
;
7896 else if (!strcmp(p
, "lba"))
7897 translation
= BIOS_ATA_TRANSLATION_LBA
;
7898 else if (!strcmp(p
, "auto"))
7899 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7902 } else if (*p
!= '\0') {
7904 fprintf(stderr
, "qemu: invalid physical CHS format\n");
7907 if (hda_index
!= -1)
7908 snprintf(drives_opt
[hda_index
].opt
,
7909 sizeof(drives_opt
[hda_index
].opt
),
7910 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
7911 0, cyls
, heads
, secs
,
7912 translation
== BIOS_ATA_TRANSLATION_LBA
?
7914 translation
== BIOS_ATA_TRANSLATION_NONE
?
7915 ",trans=none" : "");
7918 case QEMU_OPTION_nographic
:
7919 serial_devices
[0] = "stdio";
7920 parallel_devices
[0] = "null";
7921 monitor_device
= "stdio";
7924 #ifdef CONFIG_CURSES
7925 case QEMU_OPTION_curses
:
7929 case QEMU_OPTION_portrait
:
7932 case QEMU_OPTION_kernel
:
7933 kernel_filename
= optarg
;
7935 case QEMU_OPTION_append
:
7936 kernel_cmdline
= optarg
;
7938 case QEMU_OPTION_cdrom
:
7939 drive_add(optarg
, CDROM_ALIAS
);
7941 case QEMU_OPTION_boot
:
7942 boot_devices
= optarg
;
7943 /* We just do some generic consistency checks */
7945 /* Could easily be extended to 64 devices if needed */
7948 boot_devices_bitmap
= 0;
7949 for (p
= boot_devices
; *p
!= '\0'; p
++) {
7950 /* Allowed boot devices are:
7951 * a b : floppy disk drives
7952 * c ... f : IDE disk drives
7953 * g ... m : machine implementation dependant drives
7954 * n ... p : network devices
7955 * It's up to each machine implementation to check
7956 * if the given boot devices match the actual hardware
7957 * implementation and firmware features.
7959 if (*p
< 'a' || *p
> 'q') {
7960 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
7963 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
7965 "Boot device '%c' was given twice\n",*p
);
7968 boot_devices_bitmap
|= 1 << (*p
- 'a');
7972 case QEMU_OPTION_fda
:
7973 case QEMU_OPTION_fdb
:
7974 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
7977 case QEMU_OPTION_no_fd_bootchk
:
7981 case QEMU_OPTION_net
:
7982 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
7983 fprintf(stderr
, "qemu: too many network clients\n");
7986 net_clients
[nb_net_clients
] = optarg
;
7990 case QEMU_OPTION_tftp
:
7991 tftp_prefix
= optarg
;
7993 case QEMU_OPTION_bootp
:
7994 bootp_filename
= optarg
;
7997 case QEMU_OPTION_smb
:
7998 net_slirp_smb(optarg
);
8001 case QEMU_OPTION_redir
:
8002 net_slirp_redir(optarg
);
8006 case QEMU_OPTION_audio_help
:
8010 case QEMU_OPTION_soundhw
:
8011 select_soundhw (optarg
);
8017 case QEMU_OPTION_m
: {
8021 value
= strtoul(optarg
, &ptr
, 10);
8023 case 0: case 'M': case 'm':
8030 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
8034 /* On 32-bit hosts, QEMU is limited by virtual address space */
8035 if (value
> (2047 << 20)
8037 && HOST_LONG_BITS
== 32
8040 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
8043 if (value
!= (uint64_t)(ram_addr_t
)value
) {
8044 fprintf(stderr
, "qemu: ram size too large\n");
8055 mask
= cpu_str_to_log_mask(optarg
);
8057 printf("Log items (comma separated):\n");
8058 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
8059 printf("%-10s %s\n", item
->name
, item
->help
);
8066 #ifdef CONFIG_GDBSTUB
8071 gdbstub_port
= optarg
;
8077 case QEMU_OPTION_bios
:
8084 keyboard_layout
= optarg
;
8086 case QEMU_OPTION_localtime
:
8089 case QEMU_OPTION_cirrusvga
:
8090 cirrus_vga_enabled
= 1;
8093 case QEMU_OPTION_vmsvga
:
8094 cirrus_vga_enabled
= 0;
8097 case QEMU_OPTION_std_vga
:
8098 cirrus_vga_enabled
= 0;
8106 w
= strtol(p
, (char **)&p
, 10);
8109 fprintf(stderr
, "qemu: invalid resolution or depth\n");
8115 h
= strtol(p
, (char **)&p
, 10);
8120 depth
= strtol(p
, (char **)&p
, 10);
8121 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
8122 depth
!= 24 && depth
!= 32)
8124 } else if (*p
== '\0') {
8125 depth
= graphic_depth
;
8132 graphic_depth
= depth
;
8135 case QEMU_OPTION_echr
:
8138 term_escape_char
= strtol(optarg
, &r
, 0);
8140 printf("Bad argument to echr\n");
8143 case QEMU_OPTION_monitor
:
8144 monitor_device
= optarg
;
8146 case QEMU_OPTION_serial
:
8147 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
8148 fprintf(stderr
, "qemu: too many serial ports\n");
8151 serial_devices
[serial_device_index
] = optarg
;
8152 serial_device_index
++;
8154 case QEMU_OPTION_parallel
:
8155 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
8156 fprintf(stderr
, "qemu: too many parallel ports\n");
8159 parallel_devices
[parallel_device_index
] = optarg
;
8160 parallel_device_index
++;
8162 case QEMU_OPTION_loadvm
:
8165 case QEMU_OPTION_full_screen
:
8169 case QEMU_OPTION_no_frame
:
8172 case QEMU_OPTION_alt_grab
:
8175 case QEMU_OPTION_no_quit
:
8179 case QEMU_OPTION_pidfile
:
8183 case QEMU_OPTION_win2k_hack
:
8184 win2k_install_hack
= 1;
8188 case QEMU_OPTION_no_kqemu
:
8191 case QEMU_OPTION_kernel_kqemu
:
8195 case QEMU_OPTION_usb
:
8198 case QEMU_OPTION_usbdevice
:
8200 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
8201 fprintf(stderr
, "Too many USB devices\n");
8204 usb_devices
[usb_devices_index
] = optarg
;
8205 usb_devices_index
++;
8207 case QEMU_OPTION_smp
:
8208 smp_cpus
= atoi(optarg
);
8209 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
8210 fprintf(stderr
, "Invalid number of CPUs\n");
8214 case QEMU_OPTION_vnc
:
8215 vnc_display
= optarg
;
8217 case QEMU_OPTION_no_acpi
:
8220 case QEMU_OPTION_no_reboot
:
8223 case QEMU_OPTION_no_shutdown
:
8226 case QEMU_OPTION_show_cursor
:
8229 case QEMU_OPTION_daemonize
:
8232 case QEMU_OPTION_option_rom
:
8233 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
8234 fprintf(stderr
, "Too many option ROMs\n");
8237 option_rom
[nb_option_roms
] = optarg
;
8240 case QEMU_OPTION_semihosting
:
8241 semihosting_enabled
= 1;
8243 case QEMU_OPTION_name
:
8247 case QEMU_OPTION_prom_env
:
8248 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
8249 fprintf(stderr
, "Too many prom variables\n");
8252 prom_envs
[nb_prom_envs
] = optarg
;
8257 case QEMU_OPTION_old_param
:
8261 case QEMU_OPTION_clock
:
8262 configure_alarms(optarg
);
8264 case QEMU_OPTION_startdate
:
8267 time_t rtc_start_date
;
8268 if (!strcmp(optarg
, "now")) {
8269 rtc_date_offset
= -1;
8271 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
8279 } else if (sscanf(optarg
, "%d-%d-%d",
8282 &tm
.tm_mday
) == 3) {
8291 rtc_start_date
= mktimegm(&tm
);
8292 if (rtc_start_date
== -1) {
8294 fprintf(stderr
, "Invalid date format. Valid format are:\n"
8295 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
8298 rtc_date_offset
= time(NULL
) - rtc_start_date
;
8302 case QEMU_OPTION_tb_size
:
8303 tb_size
= strtol(optarg
, NULL
, 0);
8312 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
8313 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
8320 if (pipe(fds
) == -1)
8331 len
= read(fds
[0], &status
, 1);
8332 if (len
== -1 && (errno
== EINTR
))
8337 else if (status
== 1) {
8338 fprintf(stderr
, "Could not acquire pidfile\n");
8356 signal(SIGTSTP
, SIG_IGN
);
8357 signal(SIGTTOU
, SIG_IGN
);
8358 signal(SIGTTIN
, SIG_IGN
);
8362 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
8365 write(fds
[1], &status
, 1);
8367 fprintf(stderr
, "Could not acquire pid file\n");
8375 linux_boot
= (kernel_filename
!= NULL
);
8376 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
8378 /* XXX: this should not be: some embedded targets just have flash */
8379 if (!linux_boot
&& net_boot
== 0 &&
8383 /* boot to floppy or the default cd if no hard disk defined yet */
8384 if (!boot_devices
[0]) {
8385 boot_devices
= "cad";
8387 setvbuf(stdout
, NULL
, _IOLBF
, 0);
8397 /* init network clients */
8398 if (nb_net_clients
== 0) {
8399 /* if no clients, we use a default config */
8400 net_clients
[0] = "nic";
8401 net_clients
[1] = "user";
8405 for(i
= 0;i
< nb_net_clients
; i
++) {
8406 if (net_client_init(net_clients
[i
]) < 0)
8409 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
8410 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
8412 if (vlan
->nb_guest_devs
== 0) {
8413 fprintf(stderr
, "Invalid vlan (%d) with no nics\n", vlan
->id
);
8416 if (vlan
->nb_host_devs
== 0)
8418 "Warning: vlan %d is not connected to host network\n",
8423 /* XXX: this should be moved in the PC machine instantiation code */
8424 if (net_boot
!= 0) {
8426 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
8427 const char *model
= nd_table
[i
].model
;
8429 if (net_boot
& (1 << i
)) {
8432 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
8433 if (get_image_size(buf
) > 0) {
8434 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
8435 fprintf(stderr
, "Too many option ROMs\n");
8438 option_rom
[nb_option_roms
] = strdup(buf
);
8445 fprintf(stderr
, "No valid PXE rom found for network device\n");
8451 /* init the memory */
8452 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
8454 if (machine
->ram_require
& RAMSIZE_FIXED
) {
8456 if (ram_size
< phys_ram_size
) {
8457 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
8458 machine
->name
, (unsigned long long) phys_ram_size
);
8462 phys_ram_size
= ram_size
;
8464 ram_size
= phys_ram_size
;
8467 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
8469 phys_ram_size
+= ram_size
;
8472 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
8473 if (!phys_ram_base
) {
8474 fprintf(stderr
, "Could not allocate physical memory\n");
8478 /* init the dynamic translator */
8479 cpu_exec_init_all(tb_size
* 1024 * 1024);
8483 /* we always create the cdrom drive, even if no disk is there */
8485 if (nb_drives_opt
< MAX_DRIVES
)
8486 drive_add(NULL
, CDROM_ALIAS
);
8488 /* we always create at least one floppy */
8490 if (nb_drives_opt
< MAX_DRIVES
)
8491 drive_add(NULL
, FD_ALIAS
, 0);
8493 /* we always create one sd slot, even if no card is in it */
8495 if (nb_drives_opt
< MAX_DRIVES
)
8496 drive_add(NULL
, SD_ALIAS
);
8498 /* open the virtual block devices */
8500 for(i
= 0; i
< nb_drives_opt
; i
++)
8501 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
8504 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
8505 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
8510 memset(&display_state
, 0, sizeof(display_state
));
8513 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
8516 /* nearly nothing to do */
8517 dumb_display_init(ds
);
8518 } else if (vnc_display
!= NULL
) {
8519 vnc_display_init(ds
);
8520 if (vnc_display_open(ds
, vnc_display
) < 0)
8523 #if defined(CONFIG_CURSES)
8525 curses_display_init(ds
, full_screen
);
8529 #if defined(CONFIG_SDL)
8530 sdl_display_init(ds
, full_screen
, no_frame
);
8531 #elif defined(CONFIG_COCOA)
8532 cocoa_display_init(ds
, full_screen
);
8534 dumb_display_init(ds
);
8538 /* Maintain compatibility with multiple stdio monitors */
8539 if (!strcmp(monitor_device
,"stdio")) {
8540 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
8541 const char *devname
= serial_devices
[i
];
8542 if (devname
&& !strcmp(devname
,"mon:stdio")) {
8543 monitor_device
= NULL
;
8545 } else if (devname
&& !strcmp(devname
,"stdio")) {
8546 monitor_device
= NULL
;
8547 serial_devices
[i
] = "mon:stdio";
8552 if (monitor_device
) {
8553 monitor_hd
= qemu_chr_open(monitor_device
);
8555 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
8558 monitor_init(monitor_hd
, !nographic
);
8561 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
8562 const char *devname
= serial_devices
[i
];
8563 if (devname
&& strcmp(devname
, "none")) {
8564 serial_hds
[i
] = qemu_chr_open(devname
);
8565 if (!serial_hds
[i
]) {
8566 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
8570 if (strstart(devname
, "vc", 0))
8571 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
8575 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
8576 const char *devname
= parallel_devices
[i
];
8577 if (devname
&& strcmp(devname
, "none")) {
8578 parallel_hds
[i
] = qemu_chr_open(devname
);
8579 if (!parallel_hds
[i
]) {
8580 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
8584 if (strstart(devname
, "vc", 0))
8585 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
8589 machine
->init(ram_size
, vga_ram_size
, boot_devices
, ds
,
8590 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
8592 /* init USB devices */
8594 for(i
= 0; i
< usb_devices_index
; i
++) {
8595 if (usb_device_add(usb_devices
[i
]) < 0) {
8596 fprintf(stderr
, "Warning: could not add USB device %s\n",
8602 if (display_state
.dpy_refresh
) {
8603 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
8604 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
8607 #ifdef CONFIG_GDBSTUB
8609 /* XXX: use standard host:port notation and modify options
8611 if (gdbserver_start(gdbstub_port
) < 0) {
8612 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
8623 /* XXX: simplify init */
8636 len
= write(fds
[1], &status
, 1);
8637 if (len
== -1 && (errno
== EINTR
))
8643 TFR(fd
= open("/dev/null", O_RDWR
));
8657 #if !defined(_WIN32)
8658 /* close network clients */
8659 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
8660 VLANClientState
*vc
;
8662 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
8663 if (vc
->fd_read
== tap_receive
) {
8665 TAPState
*s
= vc
->opaque
;
8667 if (sscanf(vc
->info_str
, "tap: ifname=%63s ", ifname
) == 1 &&
8669 launch_script(s
->down_script
, ifname
, s
->fd
);