4 * Copyright (c) 2003-2007 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
35 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
45 #include <sys/select.h>
46 #include <arpa/inet.h>
52 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
53 #include <freebsd/stdlib.h>
57 #include <linux/if_tun.h>
60 #include <linux/rtc.h>
62 /* For the benefit of older linux systems which don't supply it,
63 we use a local copy of hpet.h. */
64 /* #include <linux/hpet.h> */
67 #include <linux/ppdev.h>
68 #include <linux/parport.h>
71 #include <sys/ethernet.h>
72 #include <sys/sockio.h>
73 #include <netinet/arp.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_icmp.h> // must come after ip.h
78 #include <netinet/udp.h>
79 #include <netinet/tcp.h>
87 int inet_aton(const char *cp
, struct in_addr
*ia
);
90 #if defined(CONFIG_SLIRP)
96 #include <sys/timeb.h>
98 #define getopt_long_only getopt_long
99 #define memalign(align, size) malloc(size)
102 #include "qemu_socket.h"
108 #endif /* CONFIG_SDL */
112 #define main qemu_main
113 #endif /* CONFIG_COCOA */
117 #include "exec-all.h"
119 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
120 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
122 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
124 #define SMBD_COMMAND "/usr/sbin/smbd"
127 //#define DEBUG_UNUSED_IOPORT
128 //#define DEBUG_IOPORT
130 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
133 #define DEFAULT_RAM_SIZE 144
135 #define DEFAULT_RAM_SIZE 128
138 #define GUI_REFRESH_INTERVAL 30
140 /* Max number of USB devices that can be specified on the commandline. */
141 #define MAX_USB_CMDLINE 8
143 /* XXX: use a two level table to limit memory usage */
144 #define MAX_IOPORTS 65536
146 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
147 const char *bios_name
= NULL
;
148 char phys_ram_file
[1024];
149 void *ioport_opaque
[MAX_IOPORTS
];
150 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
151 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
152 /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available
153 to store the VM snapshots */
154 BlockDriverState
*bs_table
[MAX_DISKS
+ 1], *fd_table
[MAX_FD
];
155 BlockDriverState
*pflash_table
[MAX_PFLASH
];
156 BlockDriverState
*sd_bdrv
;
157 BlockDriverState
*mtd_bdrv
;
158 /* point to the block driver where the snapshots are managed */
159 BlockDriverState
*bs_snapshots
;
161 static DisplayState display_state
;
163 const char* keyboard_layout
= NULL
;
164 int64_t ticks_per_sec
;
165 #if defined(TARGET_I386)
166 #define MAX_BOOT_DEVICES 3
168 #define MAX_BOOT_DEVICES 1
170 static char boot_device
[MAX_BOOT_DEVICES
+ 1];
172 int pit_min_timer_count
= 0;
174 NICInfo nd_table
[MAX_NICS
];
177 int cirrus_vga_enabled
= 1;
178 int vmsvga_enabled
= 0;
180 int graphic_width
= 1024;
181 int graphic_height
= 768;
182 int graphic_depth
= 8;
184 int graphic_width
= 800;
185 int graphic_height
= 600;
186 int graphic_depth
= 15;
191 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
192 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
194 int win2k_install_hack
= 0;
197 static VLANState
*first_vlan
;
199 const char *vnc_display
;
200 #if defined(TARGET_SPARC)
202 #elif defined(TARGET_I386)
207 int acpi_enabled
= 1;
211 int graphic_rotate
= 0;
213 const char *option_rom
[MAX_OPTION_ROMS
];
215 int semihosting_enabled
= 0;
220 const char *qemu_name
;
223 unsigned int nb_prom_envs
= 0;
224 const char *prom_envs
[MAX_PROM_ENVS
];
227 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
229 /***********************************************************/
230 /* x86 ISA bus support */
232 target_phys_addr_t isa_mem_base
= 0;
235 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
237 #ifdef DEBUG_UNUSED_IOPORT
238 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
243 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
245 #ifdef DEBUG_UNUSED_IOPORT
246 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
250 /* default is to make two byte accesses */
251 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
254 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
255 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
256 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
260 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
262 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
263 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
264 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
267 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
269 #ifdef DEBUG_UNUSED_IOPORT
270 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
275 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
277 #ifdef DEBUG_UNUSED_IOPORT
278 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
282 void init_ioports(void)
286 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
287 ioport_read_table
[0][i
] = default_ioport_readb
;
288 ioport_write_table
[0][i
] = default_ioport_writeb
;
289 ioport_read_table
[1][i
] = default_ioport_readw
;
290 ioport_write_table
[1][i
] = default_ioport_writew
;
291 ioport_read_table
[2][i
] = default_ioport_readl
;
292 ioport_write_table
[2][i
] = default_ioport_writel
;
296 /* size is the word size in byte */
297 int register_ioport_read(int start
, int length
, int size
,
298 IOPortReadFunc
*func
, void *opaque
)
304 } else if (size
== 2) {
306 } else if (size
== 4) {
309 hw_error("register_ioport_read: invalid size");
312 for(i
= start
; i
< start
+ length
; i
+= size
) {
313 ioport_read_table
[bsize
][i
] = func
;
314 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
315 hw_error("register_ioport_read: invalid opaque");
316 ioport_opaque
[i
] = opaque
;
321 /* size is the word size in byte */
322 int register_ioport_write(int start
, int length
, int size
,
323 IOPortWriteFunc
*func
, void *opaque
)
329 } else if (size
== 2) {
331 } else if (size
== 4) {
334 hw_error("register_ioport_write: invalid size");
337 for(i
= start
; i
< start
+ length
; i
+= size
) {
338 ioport_write_table
[bsize
][i
] = func
;
339 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
340 hw_error("register_ioport_write: invalid opaque");
341 ioport_opaque
[i
] = opaque
;
346 void isa_unassign_ioport(int start
, int length
)
350 for(i
= start
; i
< start
+ length
; i
++) {
351 ioport_read_table
[0][i
] = default_ioport_readb
;
352 ioport_read_table
[1][i
] = default_ioport_readw
;
353 ioport_read_table
[2][i
] = default_ioport_readl
;
355 ioport_write_table
[0][i
] = default_ioport_writeb
;
356 ioport_write_table
[1][i
] = default_ioport_writew
;
357 ioport_write_table
[2][i
] = default_ioport_writel
;
361 /***********************************************************/
363 void cpu_outb(CPUState
*env
, int addr
, int val
)
366 if (loglevel
& CPU_LOG_IOPORT
)
367 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
369 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
372 env
->last_io_time
= cpu_get_time_fast();
376 void cpu_outw(CPUState
*env
, int addr
, int val
)
379 if (loglevel
& CPU_LOG_IOPORT
)
380 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
382 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
385 env
->last_io_time
= cpu_get_time_fast();
389 void cpu_outl(CPUState
*env
, int addr
, int val
)
392 if (loglevel
& CPU_LOG_IOPORT
)
393 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
395 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
398 env
->last_io_time
= cpu_get_time_fast();
402 int cpu_inb(CPUState
*env
, int addr
)
405 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
407 if (loglevel
& CPU_LOG_IOPORT
)
408 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
412 env
->last_io_time
= cpu_get_time_fast();
417 int cpu_inw(CPUState
*env
, int addr
)
420 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
422 if (loglevel
& CPU_LOG_IOPORT
)
423 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
427 env
->last_io_time
= cpu_get_time_fast();
432 int cpu_inl(CPUState
*env
, int addr
)
435 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
437 if (loglevel
& CPU_LOG_IOPORT
)
438 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
442 env
->last_io_time
= cpu_get_time_fast();
447 /***********************************************************/
448 void hw_error(const char *fmt
, ...)
454 fprintf(stderr
, "qemu: hardware error: ");
455 vfprintf(stderr
, fmt
, ap
);
456 fprintf(stderr
, "\n");
457 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
458 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
460 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
462 cpu_dump_state(env
, stderr
, fprintf
, 0);
469 /***********************************************************/
472 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
473 static void *qemu_put_kbd_event_opaque
;
474 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
475 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
477 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
479 qemu_put_kbd_event_opaque
= opaque
;
480 qemu_put_kbd_event
= func
;
483 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
484 void *opaque
, int absolute
,
487 QEMUPutMouseEntry
*s
, *cursor
;
489 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
493 s
->qemu_put_mouse_event
= func
;
494 s
->qemu_put_mouse_event_opaque
= opaque
;
495 s
->qemu_put_mouse_event_absolute
= absolute
;
496 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
499 if (!qemu_put_mouse_event_head
) {
500 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
504 cursor
= qemu_put_mouse_event_head
;
505 while (cursor
->next
!= NULL
)
506 cursor
= cursor
->next
;
509 qemu_put_mouse_event_current
= s
;
514 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
516 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
518 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
521 cursor
= qemu_put_mouse_event_head
;
522 while (cursor
!= NULL
&& cursor
!= entry
) {
524 cursor
= cursor
->next
;
527 if (cursor
== NULL
) // does not exist or list empty
529 else if (prev
== NULL
) { // entry is head
530 qemu_put_mouse_event_head
= cursor
->next
;
531 if (qemu_put_mouse_event_current
== entry
)
532 qemu_put_mouse_event_current
= cursor
->next
;
533 qemu_free(entry
->qemu_put_mouse_event_name
);
538 prev
->next
= entry
->next
;
540 if (qemu_put_mouse_event_current
== entry
)
541 qemu_put_mouse_event_current
= prev
;
543 qemu_free(entry
->qemu_put_mouse_event_name
);
547 void kbd_put_keycode(int keycode
)
549 if (qemu_put_kbd_event
) {
550 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
554 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
556 QEMUPutMouseEvent
*mouse_event
;
557 void *mouse_event_opaque
;
560 if (!qemu_put_mouse_event_current
) {
565 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
567 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
570 if (graphic_rotate
) {
571 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
574 width
= graphic_width
;
575 mouse_event(mouse_event_opaque
,
576 width
- dy
, dx
, dz
, buttons_state
);
578 mouse_event(mouse_event_opaque
,
579 dx
, dy
, dz
, buttons_state
);
583 int kbd_mouse_is_absolute(void)
585 if (!qemu_put_mouse_event_current
)
588 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
591 void do_info_mice(void)
593 QEMUPutMouseEntry
*cursor
;
596 if (!qemu_put_mouse_event_head
) {
597 term_printf("No mouse devices connected\n");
601 term_printf("Mouse devices available:\n");
602 cursor
= qemu_put_mouse_event_head
;
603 while (cursor
!= NULL
) {
604 term_printf("%c Mouse #%d: %s\n",
605 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
606 index
, cursor
->qemu_put_mouse_event_name
);
608 cursor
= cursor
->next
;
612 void do_mouse_set(int index
)
614 QEMUPutMouseEntry
*cursor
;
617 if (!qemu_put_mouse_event_head
) {
618 term_printf("No mouse devices connected\n");
622 cursor
= qemu_put_mouse_event_head
;
623 while (cursor
!= NULL
&& index
!= i
) {
625 cursor
= cursor
->next
;
629 qemu_put_mouse_event_current
= cursor
;
631 term_printf("Mouse at given index not found\n");
634 /* compute with 96 bit intermediate result: (a*b)/c */
635 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
640 #ifdef WORDS_BIGENDIAN
650 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
651 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
654 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
658 /***********************************************************/
659 /* real time host monotonic timer */
661 #define QEMU_TIMER_BASE 1000000000LL
665 static int64_t clock_freq
;
667 static void init_get_clock(void)
671 ret
= QueryPerformanceFrequency(&freq
);
673 fprintf(stderr
, "Could not calibrate ticks\n");
676 clock_freq
= freq
.QuadPart
;
679 static int64_t get_clock(void)
682 QueryPerformanceCounter(&ti
);
683 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
688 static int use_rt_clock
;
690 static void init_get_clock(void)
693 #if defined(__linux__)
696 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
703 static int64_t get_clock(void)
705 #if defined(__linux__)
708 clock_gettime(CLOCK_MONOTONIC
, &ts
);
709 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
713 /* XXX: using gettimeofday leads to problems if the date
714 changes, so it should be avoided. */
716 gettimeofday(&tv
, NULL
);
717 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
723 /***********************************************************/
724 /* guest cycle counter */
726 static int64_t cpu_ticks_prev
;
727 static int64_t cpu_ticks_offset
;
728 static int64_t cpu_clock_offset
;
729 static int cpu_ticks_enabled
;
731 /* return the host CPU cycle counter and handle stop/restart */
732 int64_t cpu_get_ticks(void)
734 if (!cpu_ticks_enabled
) {
735 return cpu_ticks_offset
;
738 ticks
= cpu_get_real_ticks();
739 if (cpu_ticks_prev
> ticks
) {
740 /* Note: non increasing ticks may happen if the host uses
742 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
744 cpu_ticks_prev
= ticks
;
745 return ticks
+ cpu_ticks_offset
;
749 /* return the host CPU monotonic timer and handle stop/restart */
750 static int64_t cpu_get_clock(void)
753 if (!cpu_ticks_enabled
) {
754 return cpu_clock_offset
;
757 return ti
+ cpu_clock_offset
;
761 /* enable cpu_get_ticks() */
762 void cpu_enable_ticks(void)
764 if (!cpu_ticks_enabled
) {
765 cpu_ticks_offset
-= cpu_get_real_ticks();
766 cpu_clock_offset
-= get_clock();
767 cpu_ticks_enabled
= 1;
771 /* disable cpu_get_ticks() : the clock is stopped. You must not call
772 cpu_get_ticks() after that. */
773 void cpu_disable_ticks(void)
775 if (cpu_ticks_enabled
) {
776 cpu_ticks_offset
= cpu_get_ticks();
777 cpu_clock_offset
= cpu_get_clock();
778 cpu_ticks_enabled
= 0;
782 /***********************************************************/
785 #define QEMU_TIMER_REALTIME 0
786 #define QEMU_TIMER_VIRTUAL 1
790 /* XXX: add frequency */
798 struct QEMUTimer
*next
;
801 struct qemu_alarm_timer
{
805 int (*start
)(struct qemu_alarm_timer
*t
);
806 void (*stop
)(struct qemu_alarm_timer
*t
);
807 void (*rearm
)(struct qemu_alarm_timer
*t
);
811 #define ALARM_FLAG_DYNTICKS 0x1
813 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
815 return t
->flags
& ALARM_FLAG_DYNTICKS
;
818 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
820 if (!alarm_has_dynticks(t
))
826 /* TODO: MIN_TIMER_REARM_US should be optimized */
827 #define MIN_TIMER_REARM_US 250
829 static struct qemu_alarm_timer
*alarm_timer
;
833 struct qemu_alarm_win32
{
837 } alarm_win32_data
= {0, NULL
, -1};
839 static int win32_start_timer(struct qemu_alarm_timer
*t
);
840 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
841 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
845 static int unix_start_timer(struct qemu_alarm_timer
*t
);
846 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
850 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
851 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
852 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
854 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
855 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
857 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
858 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
860 #endif /* __linux__ */
864 static struct qemu_alarm_timer alarm_timers
[] = {
867 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
868 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
869 /* HPET - if available - is preferred */
870 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
871 /* ...otherwise try RTC */
872 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
874 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
876 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
877 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
878 {"win32", 0, win32_start_timer
,
879 win32_stop_timer
, NULL
, &alarm_win32_data
},
884 static void show_available_alarms()
888 printf("Available alarm timers, in order of precedence:\n");
889 for (i
= 0; alarm_timers
[i
].name
; i
++)
890 printf("%s\n", alarm_timers
[i
].name
);
893 static void configure_alarms(char const *opt
)
897 int count
= (sizeof(alarm_timers
) / sizeof(*alarm_timers
)) - 1;
901 if (!strcmp(opt
, "help")) {
902 show_available_alarms();
908 /* Reorder the array */
909 name
= strtok(arg
, ",");
911 struct qemu_alarm_timer tmp
;
913 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
914 if (!strcmp(alarm_timers
[i
].name
, name
))
919 fprintf(stderr
, "Unknown clock %s\n", name
);
928 tmp
= alarm_timers
[i
];
929 alarm_timers
[i
] = alarm_timers
[cur
];
930 alarm_timers
[cur
] = tmp
;
934 name
= strtok(NULL
, ",");
940 /* Disable remaining timers */
941 for (i
= cur
; i
< count
; i
++)
942 alarm_timers
[i
].name
= NULL
;
946 show_available_alarms();
952 static QEMUTimer
*active_timers
[2];
954 QEMUClock
*qemu_new_clock(int type
)
957 clock
= qemu_mallocz(sizeof(QEMUClock
));
964 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
968 ts
= qemu_mallocz(sizeof(QEMUTimer
));
975 void qemu_free_timer(QEMUTimer
*ts
)
980 /* stop a timer, but do not dealloc it */
981 void qemu_del_timer(QEMUTimer
*ts
)
985 /* NOTE: this code must be signal safe because
986 qemu_timer_expired() can be called from a signal. */
987 pt
= &active_timers
[ts
->clock
->type
];
1000 /* modify the current timer so that it will be fired when current_time
1001 >= expire_time. The corresponding callback will be called. */
1002 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1008 /* add the timer in the sorted list */
1009 /* NOTE: this code must be signal safe because
1010 qemu_timer_expired() can be called from a signal. */
1011 pt
= &active_timers
[ts
->clock
->type
];
1016 if (t
->expire_time
> expire_time
)
1020 ts
->expire_time
= expire_time
;
1025 int qemu_timer_pending(QEMUTimer
*ts
)
1028 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1035 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1039 return (timer_head
->expire_time
<= current_time
);
1042 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1048 if (!ts
|| ts
->expire_time
> current_time
)
1050 /* remove timer from the list before calling the callback */
1051 *ptimer_head
= ts
->next
;
1054 /* run the callback (the timer list can be modified) */
1057 qemu_rearm_alarm_timer(alarm_timer
);
1060 int64_t qemu_get_clock(QEMUClock
*clock
)
1062 switch(clock
->type
) {
1063 case QEMU_TIMER_REALTIME
:
1064 return get_clock() / 1000000;
1066 case QEMU_TIMER_VIRTUAL
:
1067 return cpu_get_clock();
1071 static void init_timers(void)
1074 ticks_per_sec
= QEMU_TIMER_BASE
;
1075 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1076 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1080 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1082 uint64_t expire_time
;
1084 if (qemu_timer_pending(ts
)) {
1085 expire_time
= ts
->expire_time
;
1089 qemu_put_be64(f
, expire_time
);
1092 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1094 uint64_t expire_time
;
1096 expire_time
= qemu_get_be64(f
);
1097 if (expire_time
!= -1) {
1098 qemu_mod_timer(ts
, expire_time
);
1104 static void timer_save(QEMUFile
*f
, void *opaque
)
1106 if (cpu_ticks_enabled
) {
1107 hw_error("cannot save state if virtual timers are running");
1109 qemu_put_be64s(f
, &cpu_ticks_offset
);
1110 qemu_put_be64s(f
, &ticks_per_sec
);
1111 qemu_put_be64s(f
, &cpu_clock_offset
);
1114 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1116 if (version_id
!= 1 && version_id
!= 2)
1118 if (cpu_ticks_enabled
) {
1121 qemu_get_be64s(f
, &cpu_ticks_offset
);
1122 qemu_get_be64s(f
, &ticks_per_sec
);
1123 if (version_id
== 2) {
1124 qemu_get_be64s(f
, &cpu_clock_offset
);
1130 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1131 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1133 static void host_alarm_handler(int host_signum
)
1137 #define DISP_FREQ 1000
1139 static int64_t delta_min
= INT64_MAX
;
1140 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1142 ti
= qemu_get_clock(vm_clock
);
1143 if (last_clock
!= 0) {
1144 delta
= ti
- last_clock
;
1145 if (delta
< delta_min
)
1147 if (delta
> delta_max
)
1150 if (++count
== DISP_FREQ
) {
1151 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1152 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1153 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1154 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1155 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1157 delta_min
= INT64_MAX
;
1165 if (alarm_has_dynticks(alarm_timer
) ||
1166 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1167 qemu_get_clock(vm_clock
)) ||
1168 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1169 qemu_get_clock(rt_clock
))) {
1171 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1172 SetEvent(data
->host_alarm
);
1174 CPUState
*env
= cpu_single_env
;
1176 /* stop the currently executing cpu because a timer occured */
1177 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1179 if (env
->kqemu_enabled
) {
1180 kqemu_cpu_interrupt(env
);
1187 static uint64_t qemu_next_deadline(void)
1189 int64_t nearest_delta_us
= INT64_MAX
;
1192 if (active_timers
[QEMU_TIMER_REALTIME
])
1193 nearest_delta_us
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1194 qemu_get_clock(rt_clock
))*1000;
1196 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1198 vmdelta_us
= (active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1199 qemu_get_clock(vm_clock
)+999)/1000;
1200 if (vmdelta_us
< nearest_delta_us
)
1201 nearest_delta_us
= vmdelta_us
;
1204 /* Avoid arming the timer to negative, zero, or too low values */
1205 if (nearest_delta_us
<= MIN_TIMER_REARM_US
)
1206 nearest_delta_us
= MIN_TIMER_REARM_US
;
1208 return nearest_delta_us
;
1213 #if defined(__linux__)
1215 #define RTC_FREQ 1024
1217 static void enable_sigio_timer(int fd
)
1219 struct sigaction act
;
1222 sigfillset(&act
.sa_mask
);
1224 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1225 act
.sa_flags
|= SA_ONSTACK
;
1227 act
.sa_handler
= host_alarm_handler
;
1229 sigaction(SIGIO
, &act
, NULL
);
1230 fcntl(fd
, F_SETFL
, O_ASYNC
);
1231 fcntl(fd
, F_SETOWN
, getpid());
1234 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1236 struct hpet_info info
;
1239 fd
= open("/dev/hpet", O_RDONLY
);
1244 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1246 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1247 "error, but for better emulation accuracy type:\n"
1248 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1252 /* Check capabilities */
1253 r
= ioctl(fd
, HPET_INFO
, &info
);
1257 /* Enable periodic mode */
1258 r
= ioctl(fd
, HPET_EPI
, 0);
1259 if (info
.hi_flags
&& (r
< 0))
1262 /* Enable interrupt */
1263 r
= ioctl(fd
, HPET_IE_ON
, 0);
1267 enable_sigio_timer(fd
);
1268 t
->priv
= (void *)(long)fd
;
1276 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1278 int fd
= (long)t
->priv
;
1283 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1287 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1290 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1291 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1292 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1293 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1296 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1302 enable_sigio_timer(rtc_fd
);
1304 t
->priv
= (void *)(long)rtc_fd
;
1309 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1311 int rtc_fd
= (long)t
->priv
;
1316 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1320 struct sigaction act
;
1322 sigfillset(&act
.sa_mask
);
1324 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
1325 act
.sa_flags
|= SA_ONSTACK
;
1327 act
.sa_handler
= host_alarm_handler
;
1329 sigaction(SIGALRM
, &act
, NULL
);
1331 ev
.sigev_value
.sival_int
= 0;
1332 ev
.sigev_notify
= SIGEV_SIGNAL
;
1333 ev
.sigev_signo
= SIGALRM
;
1335 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1336 perror("timer_create");
1338 /* disable dynticks */
1339 fprintf(stderr
, "Dynamic Ticks disabled\n");
1344 t
->priv
= (void *)host_timer
;
1349 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1351 timer_t host_timer
= (timer_t
)t
->priv
;
1353 timer_delete(host_timer
);
1356 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1358 timer_t host_timer
= (timer_t
)t
->priv
;
1359 struct itimerspec timeout
;
1360 int64_t nearest_delta_us
= INT64_MAX
;
1363 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1364 !active_timers
[QEMU_TIMER_VIRTUAL
])
1367 nearest_delta_us
= qemu_next_deadline();
1369 /* check whether a timer is already running */
1370 if (timer_gettime(host_timer
, &timeout
)) {
1372 fprintf(stderr
, "Internal timer error: aborting\n");
1375 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1376 if (current_us
&& current_us
<= nearest_delta_us
)
1379 timeout
.it_interval
.tv_sec
= 0;
1380 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1381 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1382 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1383 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1385 fprintf(stderr
, "Internal timer error: aborting\n");
1390 #endif /* defined(__linux__) */
1392 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1394 struct sigaction act
;
1395 struct itimerval itv
;
1399 sigfillset(&act
.sa_mask
);
1401 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
1402 act
.sa_flags
|= SA_ONSTACK
;
1404 act
.sa_handler
= host_alarm_handler
;
1406 sigaction(SIGALRM
, &act
, NULL
);
1408 itv
.it_interval
.tv_sec
= 0;
1409 /* for i386 kernel 2.6 to get 1 ms */
1410 itv
.it_interval
.tv_usec
= 999;
1411 itv
.it_value
.tv_sec
= 0;
1412 itv
.it_value
.tv_usec
= 10 * 1000;
1414 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1421 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1423 struct itimerval itv
;
1425 memset(&itv
, 0, sizeof(itv
));
1426 setitimer(ITIMER_REAL
, &itv
, NULL
);
1429 #endif /* !defined(_WIN32) */
1433 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1436 struct qemu_alarm_win32
*data
= t
->priv
;
1439 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1440 if (!data
->host_alarm
) {
1441 perror("Failed CreateEvent");
1445 memset(&tc
, 0, sizeof(tc
));
1446 timeGetDevCaps(&tc
, sizeof(tc
));
1448 if (data
->period
< tc
.wPeriodMin
)
1449 data
->period
= tc
.wPeriodMin
;
1451 timeBeginPeriod(data
->period
);
1453 flags
= TIME_CALLBACK_FUNCTION
;
1454 if (alarm_has_dynticks(t
))
1455 flags
|= TIME_ONESHOT
;
1457 flags
|= TIME_PERIODIC
;
1459 data
->timerId
= timeSetEvent(1, // interval (ms)
1460 data
->period
, // resolution
1461 host_alarm_handler
, // function
1462 (DWORD
)t
, // parameter
1465 if (!data
->timerId
) {
1466 perror("Failed to initialize win32 alarm timer");
1468 timeEndPeriod(data
->period
);
1469 CloseHandle(data
->host_alarm
);
1473 qemu_add_wait_object(data
->host_alarm
, NULL
, NULL
);
1478 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1480 struct qemu_alarm_win32
*data
= t
->priv
;
1482 timeKillEvent(data
->timerId
);
1483 timeEndPeriod(data
->period
);
1485 CloseHandle(data
->host_alarm
);
1488 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1490 struct qemu_alarm_win32
*data
= t
->priv
;
1491 uint64_t nearest_delta_us
;
1493 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1494 !active_timers
[QEMU_TIMER_VIRTUAL
])
1497 nearest_delta_us
= qemu_next_deadline();
1498 nearest_delta_us
/= 1000;
1500 timeKillEvent(data
->timerId
);
1502 data
->timerId
= timeSetEvent(1,
1506 TIME_ONESHOT
| TIME_PERIODIC
);
1508 if (!data
->timerId
) {
1509 perror("Failed to re-arm win32 alarm timer");
1511 timeEndPeriod(data
->period
);
1512 CloseHandle(data
->host_alarm
);
1519 static void init_timer_alarm(void)
1521 struct qemu_alarm_timer
*t
;
1524 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1525 t
= &alarm_timers
[i
];
1533 fprintf(stderr
, "Unable to find any suitable alarm timer.\n");
1534 fprintf(stderr
, "Terminating\n");
1541 void quit_timers(void)
1543 alarm_timer
->stop(alarm_timer
);
1547 /***********************************************************/
1548 /* character device */
1550 static void qemu_chr_event(CharDriverState
*s
, int event
)
1554 s
->chr_event(s
->handler_opaque
, event
);
1557 static void qemu_chr_reset_bh(void *opaque
)
1559 CharDriverState
*s
= opaque
;
1560 qemu_chr_event(s
, CHR_EVENT_RESET
);
1561 qemu_bh_delete(s
->bh
);
1565 void qemu_chr_reset(CharDriverState
*s
)
1567 if (s
->bh
== NULL
) {
1568 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
1569 qemu_bh_schedule(s
->bh
);
1573 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1575 return s
->chr_write(s
, buf
, len
);
1578 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1582 return s
->chr_ioctl(s
, cmd
, arg
);
1585 int qemu_chr_can_read(CharDriverState
*s
)
1587 if (!s
->chr_can_read
)
1589 return s
->chr_can_read(s
->handler_opaque
);
1592 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
1594 s
->chr_read(s
->handler_opaque
, buf
, len
);
1598 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1603 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1604 qemu_chr_write(s
, buf
, strlen(buf
));
1608 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1610 if (s
->chr_send_event
)
1611 s
->chr_send_event(s
, event
);
1614 void qemu_chr_add_handlers(CharDriverState
*s
,
1615 IOCanRWHandler
*fd_can_read
,
1616 IOReadHandler
*fd_read
,
1617 IOEventHandler
*fd_event
,
1620 s
->chr_can_read
= fd_can_read
;
1621 s
->chr_read
= fd_read
;
1622 s
->chr_event
= fd_event
;
1623 s
->handler_opaque
= opaque
;
1624 if (s
->chr_update_read_handler
)
1625 s
->chr_update_read_handler(s
);
1628 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1633 static CharDriverState
*qemu_chr_open_null(void)
1635 CharDriverState
*chr
;
1637 chr
= qemu_mallocz(sizeof(CharDriverState
));
1640 chr
->chr_write
= null_chr_write
;
1644 /* MUX driver for serial I/O splitting */
1645 static int term_timestamps
;
1646 static int64_t term_timestamps_start
;
1649 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
1650 IOReadHandler
*chr_read
[MAX_MUX
];
1651 IOEventHandler
*chr_event
[MAX_MUX
];
1652 void *ext_opaque
[MAX_MUX
];
1653 CharDriverState
*drv
;
1655 int term_got_escape
;
1660 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1662 MuxDriver
*d
= chr
->opaque
;
1664 if (!term_timestamps
) {
1665 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
1670 for(i
= 0; i
< len
; i
++) {
1671 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
1672 if (buf
[i
] == '\n') {
1678 if (term_timestamps_start
== -1)
1679 term_timestamps_start
= ti
;
1680 ti
-= term_timestamps_start
;
1681 secs
= ti
/ 1000000000;
1682 snprintf(buf1
, sizeof(buf1
),
1683 "[%02d:%02d:%02d.%03d] ",
1687 (int)((ti
/ 1000000) % 1000));
1688 d
->drv
->chr_write(d
->drv
, buf1
, strlen(buf1
));
1695 static char *mux_help
[] = {
1696 "% h print this help\n\r",
1697 "% x exit emulator\n\r",
1698 "% s save disk data back to file (if -snapshot)\n\r",
1699 "% t toggle console timestamps\n\r"
1700 "% b send break (magic sysrq)\n\r",
1701 "% c switch between console and monitor\n\r",
1706 static int term_escape_char
= 0x01; /* ctrl-a is used for escape */
1707 static void mux_print_help(CharDriverState
*chr
)
1710 char ebuf
[15] = "Escape-Char";
1711 char cbuf
[50] = "\n\r";
1713 if (term_escape_char
> 0 && term_escape_char
< 26) {
1714 sprintf(cbuf
,"\n\r");
1715 sprintf(ebuf
,"C-%c", term_escape_char
- 1 + 'a');
1717 sprintf(cbuf
,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char
);
1719 chr
->chr_write(chr
, cbuf
, strlen(cbuf
));
1720 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
1721 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
1722 if (mux_help
[i
][j
] == '%')
1723 chr
->chr_write(chr
, ebuf
, strlen(ebuf
));
1725 chr
->chr_write(chr
, &mux_help
[i
][j
], 1);
1730 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
1732 if (d
->term_got_escape
) {
1733 d
->term_got_escape
= 0;
1734 if (ch
== term_escape_char
)
1739 mux_print_help(chr
);
1743 char *term
= "QEMU: Terminated\n\r";
1744 chr
->chr_write(chr
,term
,strlen(term
));
1751 for (i
= 0; i
< MAX_DISKS
; i
++) {
1753 bdrv_commit(bs_table
[i
]);
1756 bdrv_commit(mtd_bdrv
);
1760 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1763 /* Switch to the next registered device */
1765 if (chr
->focus
>= d
->mux_cnt
)
1769 term_timestamps
= !term_timestamps
;
1770 term_timestamps_start
= -1;
1773 } else if (ch
== term_escape_char
) {
1774 d
->term_got_escape
= 1;
1782 static int mux_chr_can_read(void *opaque
)
1784 CharDriverState
*chr
= opaque
;
1785 MuxDriver
*d
= chr
->opaque
;
1786 if (d
->chr_can_read
[chr
->focus
])
1787 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
1791 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
1793 CharDriverState
*chr
= opaque
;
1794 MuxDriver
*d
= chr
->opaque
;
1796 for(i
= 0; i
< size
; i
++)
1797 if (mux_proc_byte(chr
, d
, buf
[i
]))
1798 d
->chr_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
], &buf
[i
], 1);
1801 static void mux_chr_event(void *opaque
, int event
)
1803 CharDriverState
*chr
= opaque
;
1804 MuxDriver
*d
= chr
->opaque
;
1807 /* Send the event to all registered listeners */
1808 for (i
= 0; i
< d
->mux_cnt
; i
++)
1809 if (d
->chr_event
[i
])
1810 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
1813 static void mux_chr_update_read_handler(CharDriverState
*chr
)
1815 MuxDriver
*d
= chr
->opaque
;
1817 if (d
->mux_cnt
>= MAX_MUX
) {
1818 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
1821 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
1822 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
1823 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
1824 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
1825 /* Fix up the real driver with mux routines */
1826 if (d
->mux_cnt
== 0) {
1827 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
1828 mux_chr_event
, chr
);
1830 chr
->focus
= d
->mux_cnt
;
1834 CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
1836 CharDriverState
*chr
;
1839 chr
= qemu_mallocz(sizeof(CharDriverState
));
1842 d
= qemu_mallocz(sizeof(MuxDriver
));
1851 chr
->chr_write
= mux_chr_write
;
1852 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
1859 static void socket_cleanup(void)
1864 static int socket_init(void)
1869 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1871 err
= WSAGetLastError();
1872 fprintf(stderr
, "WSAStartup: %d\n", err
);
1875 atexit(socket_cleanup
);
1879 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1885 ret
= send(fd
, buf
, len
, 0);
1888 errno
= WSAGetLastError();
1889 if (errno
!= WSAEWOULDBLOCK
) {
1892 } else if (ret
== 0) {
1902 void socket_set_nonblock(int fd
)
1904 unsigned long opt
= 1;
1905 ioctlsocket(fd
, FIONBIO
, &opt
);
1910 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1916 ret
= write(fd
, buf
, len
);
1918 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1920 } else if (ret
== 0) {
1930 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1932 return unix_write(fd
, buf
, len1
);
1935 void socket_set_nonblock(int fd
)
1937 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1939 #endif /* !_WIN32 */
1948 #define STDIO_MAX_CLIENTS 1
1949 static int stdio_nb_clients
= 0;
1951 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1953 FDCharDriver
*s
= chr
->opaque
;
1954 return unix_write(s
->fd_out
, buf
, len
);
1957 static int fd_chr_read_poll(void *opaque
)
1959 CharDriverState
*chr
= opaque
;
1960 FDCharDriver
*s
= chr
->opaque
;
1962 s
->max_size
= qemu_chr_can_read(chr
);
1966 static void fd_chr_read(void *opaque
)
1968 CharDriverState
*chr
= opaque
;
1969 FDCharDriver
*s
= chr
->opaque
;
1974 if (len
> s
->max_size
)
1978 size
= read(s
->fd_in
, buf
, len
);
1980 /* FD has been closed. Remove it from the active list. */
1981 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1985 qemu_chr_read(chr
, buf
, size
);
1989 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1991 FDCharDriver
*s
= chr
->opaque
;
1993 if (s
->fd_in
>= 0) {
1994 if (nographic
&& s
->fd_in
== 0) {
1996 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1997 fd_chr_read
, NULL
, chr
);
2002 /* open a character device to a unix fd */
2003 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
2005 CharDriverState
*chr
;
2008 chr
= qemu_mallocz(sizeof(CharDriverState
));
2011 s
= qemu_mallocz(sizeof(FDCharDriver
));
2019 chr
->chr_write
= fd_chr_write
;
2020 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
2022 qemu_chr_reset(chr
);
2027 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
2031 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
2034 return qemu_chr_open_fd(-1, fd_out
);
2037 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
2040 char filename_in
[256], filename_out
[256];
2042 snprintf(filename_in
, 256, "%s.in", filename
);
2043 snprintf(filename_out
, 256, "%s.out", filename
);
2044 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
2045 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
2046 if (fd_in
< 0 || fd_out
< 0) {
2051 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
2055 return qemu_chr_open_fd(fd_in
, fd_out
);
2059 /* for STDIO, we handle the case where several clients use it
2062 #define TERM_FIFO_MAX_SIZE 1
2064 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
2065 static int term_fifo_size
;
2067 static int stdio_read_poll(void *opaque
)
2069 CharDriverState
*chr
= opaque
;
2071 /* try to flush the queue if needed */
2072 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
2073 qemu_chr_read(chr
, term_fifo
, 1);
2076 /* see if we can absorb more chars */
2077 if (term_fifo_size
== 0)
2083 static void stdio_read(void *opaque
)
2087 CharDriverState
*chr
= opaque
;
2089 size
= read(0, buf
, 1);
2091 /* stdin has been closed. Remove it from the active list. */
2092 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
2096 if (qemu_chr_can_read(chr
) > 0) {
2097 qemu_chr_read(chr
, buf
, 1);
2098 } else if (term_fifo_size
== 0) {
2099 term_fifo
[term_fifo_size
++] = buf
[0];
2104 /* init terminal so that we can grab keys */
2105 static struct termios oldtty
;
2106 static int old_fd0_flags
;
2108 static void term_exit(void)
2110 tcsetattr (0, TCSANOW
, &oldtty
);
2111 fcntl(0, F_SETFL
, old_fd0_flags
);
2114 static void term_init(void)
2118 tcgetattr (0, &tty
);
2120 old_fd0_flags
= fcntl(0, F_GETFL
);
2122 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
2123 |INLCR
|IGNCR
|ICRNL
|IXON
);
2124 tty
.c_oflag
|= OPOST
;
2125 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
2126 /* if graphical mode, we allow Ctrl-C handling */
2128 tty
.c_lflag
&= ~ISIG
;
2129 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
2132 tty
.c_cc
[VTIME
] = 0;
2134 tcsetattr (0, TCSANOW
, &tty
);
2138 fcntl(0, F_SETFL
, O_NONBLOCK
);
2141 static CharDriverState
*qemu_chr_open_stdio(void)
2143 CharDriverState
*chr
;
2145 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
2147 chr
= qemu_chr_open_fd(0, 1);
2148 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
2155 #if defined(__linux__) || defined(__sun__)
2156 static CharDriverState
*qemu_chr_open_pty(void)
2159 char slave_name
[1024];
2160 int master_fd
, slave_fd
;
2162 #if defined(__linux__)
2163 /* Not satisfying */
2164 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
2169 /* Disabling local echo and line-buffered output */
2170 tcgetattr (master_fd
, &tty
);
2171 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
2173 tty
.c_cc
[VTIME
] = 0;
2174 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
2176 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
2177 return qemu_chr_open_fd(master_fd
, master_fd
);
2180 static void tty_serial_init(int fd
, int speed
,
2181 int parity
, int data_bits
, int stop_bits
)
2187 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
2188 speed
, parity
, data_bits
, stop_bits
);
2190 tcgetattr (fd
, &tty
);
2232 cfsetispeed(&tty
, spd
);
2233 cfsetospeed(&tty
, spd
);
2235 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
2236 |INLCR
|IGNCR
|ICRNL
|IXON
);
2237 tty
.c_oflag
|= OPOST
;
2238 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
2239 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
2260 tty
.c_cflag
|= PARENB
;
2263 tty
.c_cflag
|= PARENB
| PARODD
;
2267 tty
.c_cflag
|= CSTOPB
;
2269 tcsetattr (fd
, TCSANOW
, &tty
);
2272 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2274 FDCharDriver
*s
= chr
->opaque
;
2277 case CHR_IOCTL_SERIAL_SET_PARAMS
:
2279 QEMUSerialSetParams
*ssp
= arg
;
2280 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
2281 ssp
->data_bits
, ssp
->stop_bits
);
2284 case CHR_IOCTL_SERIAL_SET_BREAK
:
2286 int enable
= *(int *)arg
;
2288 tcsendbreak(s
->fd_in
, 1);
2297 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
2299 CharDriverState
*chr
;
2302 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
2303 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2304 tty_serial_init(fd
, 115200, 'N', 8, 1);
2305 chr
= qemu_chr_open_fd(fd
, fd
);
2310 chr
->chr_ioctl
= tty_serial_ioctl
;
2311 qemu_chr_reset(chr
);
2314 #else /* ! __linux__ && ! __sun__ */
2315 static CharDriverState
*qemu_chr_open_pty(void)
2319 #endif /* __linux__ || __sun__ */
2321 #if defined(__linux__)
2325 } ParallelCharDriver
;
2327 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
2329 if (s
->mode
!= mode
) {
2331 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
2338 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2340 ParallelCharDriver
*drv
= chr
->opaque
;
2345 case CHR_IOCTL_PP_READ_DATA
:
2346 if (ioctl(fd
, PPRDATA
, &b
) < 0)
2348 *(uint8_t *)arg
= b
;
2350 case CHR_IOCTL_PP_WRITE_DATA
:
2351 b
= *(uint8_t *)arg
;
2352 if (ioctl(fd
, PPWDATA
, &b
) < 0)
2355 case CHR_IOCTL_PP_READ_CONTROL
:
2356 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
2358 /* Linux gives only the lowest bits, and no way to know data
2359 direction! For better compatibility set the fixed upper
2361 *(uint8_t *)arg
= b
| 0xc0;
2363 case CHR_IOCTL_PP_WRITE_CONTROL
:
2364 b
= *(uint8_t *)arg
;
2365 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
2368 case CHR_IOCTL_PP_READ_STATUS
:
2369 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
2371 *(uint8_t *)arg
= b
;
2373 case CHR_IOCTL_PP_EPP_READ_ADDR
:
2374 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2375 struct ParallelIOArg
*parg
= arg
;
2376 int n
= read(fd
, parg
->buffer
, parg
->count
);
2377 if (n
!= parg
->count
) {
2382 case CHR_IOCTL_PP_EPP_READ
:
2383 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2384 struct ParallelIOArg
*parg
= arg
;
2385 int n
= read(fd
, parg
->buffer
, parg
->count
);
2386 if (n
!= parg
->count
) {
2391 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
2392 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2393 struct ParallelIOArg
*parg
= arg
;
2394 int n
= write(fd
, parg
->buffer
, parg
->count
);
2395 if (n
!= parg
->count
) {
2400 case CHR_IOCTL_PP_EPP_WRITE
:
2401 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2402 struct ParallelIOArg
*parg
= arg
;
2403 int n
= write(fd
, parg
->buffer
, parg
->count
);
2404 if (n
!= parg
->count
) {
2415 static void pp_close(CharDriverState
*chr
)
2417 ParallelCharDriver
*drv
= chr
->opaque
;
2420 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
2421 ioctl(fd
, PPRELEASE
);
2426 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
2428 CharDriverState
*chr
;
2429 ParallelCharDriver
*drv
;
2432 TFR(fd
= open(filename
, O_RDWR
));
2436 if (ioctl(fd
, PPCLAIM
) < 0) {
2441 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
2447 drv
->mode
= IEEE1284_MODE_COMPAT
;
2449 chr
= qemu_mallocz(sizeof(CharDriverState
));
2455 chr
->chr_write
= null_chr_write
;
2456 chr
->chr_ioctl
= pp_ioctl
;
2457 chr
->chr_close
= pp_close
;
2460 qemu_chr_reset(chr
);
2464 #endif /* __linux__ */
2470 HANDLE hcom
, hrecv
, hsend
;
2471 OVERLAPPED orecv
, osend
;
2476 #define NSENDBUF 2048
2477 #define NRECVBUF 2048
2478 #define MAXCONNECT 1
2479 #define NTIMEOUT 5000
2481 static int win_chr_poll(void *opaque
);
2482 static int win_chr_pipe_poll(void *opaque
);
2484 static void win_chr_close(CharDriverState
*chr
)
2486 WinCharState
*s
= chr
->opaque
;
2489 CloseHandle(s
->hsend
);
2493 CloseHandle(s
->hrecv
);
2497 CloseHandle(s
->hcom
);
2501 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
2503 qemu_del_polling_cb(win_chr_poll
, chr
);
2506 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
2508 WinCharState
*s
= chr
->opaque
;
2510 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2515 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2517 fprintf(stderr
, "Failed CreateEvent\n");
2520 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2522 fprintf(stderr
, "Failed CreateEvent\n");
2526 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2527 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2528 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2529 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
2534 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2535 fprintf(stderr
, "Failed SetupComm\n");
2539 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2540 size
= sizeof(COMMCONFIG
);
2541 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2542 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2543 CommConfigDialog(filename
, NULL
, &comcfg
);
2545 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2546 fprintf(stderr
, "Failed SetCommState\n");
2550 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2551 fprintf(stderr
, "Failed SetCommMask\n");
2555 cto
.ReadIntervalTimeout
= MAXDWORD
;
2556 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2557 fprintf(stderr
, "Failed SetCommTimeouts\n");
2561 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2562 fprintf(stderr
, "Failed ClearCommError\n");
2565 qemu_add_polling_cb(win_chr_poll
, chr
);
2573 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2575 WinCharState
*s
= chr
->opaque
;
2576 DWORD len
, ret
, size
, err
;
2579 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2580 s
->osend
.hEvent
= s
->hsend
;
2583 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2585 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2587 err
= GetLastError();
2588 if (err
== ERROR_IO_PENDING
) {
2589 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2607 static int win_chr_read_poll(CharDriverState
*chr
)
2609 WinCharState
*s
= chr
->opaque
;
2611 s
->max_size
= qemu_chr_can_read(chr
);
2615 static void win_chr_readfile(CharDriverState
*chr
)
2617 WinCharState
*s
= chr
->opaque
;
2622 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2623 s
->orecv
.hEvent
= s
->hrecv
;
2624 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2626 err
= GetLastError();
2627 if (err
== ERROR_IO_PENDING
) {
2628 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2633 qemu_chr_read(chr
, buf
, size
);
2637 static void win_chr_read(CharDriverState
*chr
)
2639 WinCharState
*s
= chr
->opaque
;
2641 if (s
->len
> s
->max_size
)
2642 s
->len
= s
->max_size
;
2646 win_chr_readfile(chr
);
2649 static int win_chr_poll(void *opaque
)
2651 CharDriverState
*chr
= opaque
;
2652 WinCharState
*s
= chr
->opaque
;
2656 ClearCommError(s
->hcom
, &comerr
, &status
);
2657 if (status
.cbInQue
> 0) {
2658 s
->len
= status
.cbInQue
;
2659 win_chr_read_poll(chr
);
2666 static CharDriverState
*qemu_chr_open_win(const char *filename
)
2668 CharDriverState
*chr
;
2671 chr
= qemu_mallocz(sizeof(CharDriverState
));
2674 s
= qemu_mallocz(sizeof(WinCharState
));
2680 chr
->chr_write
= win_chr_write
;
2681 chr
->chr_close
= win_chr_close
;
2683 if (win_chr_init(chr
, filename
) < 0) {
2688 qemu_chr_reset(chr
);
2692 static int win_chr_pipe_poll(void *opaque
)
2694 CharDriverState
*chr
= opaque
;
2695 WinCharState
*s
= chr
->opaque
;
2698 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2701 win_chr_read_poll(chr
);
2708 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
2710 WinCharState
*s
= chr
->opaque
;
2718 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2720 fprintf(stderr
, "Failed CreateEvent\n");
2723 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2725 fprintf(stderr
, "Failed CreateEvent\n");
2729 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2730 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2731 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2733 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2734 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2735 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2740 ZeroMemory(&ov
, sizeof(ov
));
2741 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2742 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2744 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2748 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2750 fprintf(stderr
, "Failed GetOverlappedResult\n");
2752 CloseHandle(ov
.hEvent
);
2759 CloseHandle(ov
.hEvent
);
2762 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2771 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2773 CharDriverState
*chr
;
2776 chr
= qemu_mallocz(sizeof(CharDriverState
));
2779 s
= qemu_mallocz(sizeof(WinCharState
));
2785 chr
->chr_write
= win_chr_write
;
2786 chr
->chr_close
= win_chr_close
;
2788 if (win_chr_pipe_init(chr
, filename
) < 0) {
2793 qemu_chr_reset(chr
);
2797 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2799 CharDriverState
*chr
;
2802 chr
= qemu_mallocz(sizeof(CharDriverState
));
2805 s
= qemu_mallocz(sizeof(WinCharState
));
2812 chr
->chr_write
= win_chr_write
;
2813 qemu_chr_reset(chr
);
2817 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
2819 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2822 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2826 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2827 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2828 if (fd_out
== INVALID_HANDLE_VALUE
)
2831 return qemu_chr_open_win_file(fd_out
);
2833 #endif /* !_WIN32 */
2835 /***********************************************************/
2836 /* UDP Net console */
2840 struct sockaddr_in daddr
;
2847 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2849 NetCharDriver
*s
= chr
->opaque
;
2851 return sendto(s
->fd
, buf
, len
, 0,
2852 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2855 static int udp_chr_read_poll(void *opaque
)
2857 CharDriverState
*chr
= opaque
;
2858 NetCharDriver
*s
= chr
->opaque
;
2860 s
->max_size
= qemu_chr_can_read(chr
);
2862 /* If there were any stray characters in the queue process them
2865 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2866 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2868 s
->max_size
= qemu_chr_can_read(chr
);
2873 static void udp_chr_read(void *opaque
)
2875 CharDriverState
*chr
= opaque
;
2876 NetCharDriver
*s
= chr
->opaque
;
2878 if (s
->max_size
== 0)
2880 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2881 s
->bufptr
= s
->bufcnt
;
2886 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2887 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2889 s
->max_size
= qemu_chr_can_read(chr
);
2893 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2895 NetCharDriver
*s
= chr
->opaque
;
2898 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2899 udp_chr_read
, NULL
, chr
);
2903 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2905 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2907 int parse_host_src_port(struct sockaddr_in
*haddr
,
2908 struct sockaddr_in
*saddr
,
2911 static CharDriverState
*qemu_chr_open_udp(const char *def
)
2913 CharDriverState
*chr
= NULL
;
2914 NetCharDriver
*s
= NULL
;
2916 struct sockaddr_in saddr
;
2918 chr
= qemu_mallocz(sizeof(CharDriverState
));
2921 s
= qemu_mallocz(sizeof(NetCharDriver
));
2925 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2927 perror("socket(PF_INET, SOCK_DGRAM)");
2931 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2932 printf("Could not parse: %s\n", def
);
2936 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2946 chr
->chr_write
= udp_chr_write
;
2947 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2960 /***********************************************************/
2961 /* TCP Net console */
2972 static void tcp_chr_accept(void *opaque
);
2974 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2976 TCPCharDriver
*s
= chr
->opaque
;
2978 return send_all(s
->fd
, buf
, len
);
2980 /* XXX: indicate an error ? */
2985 static int tcp_chr_read_poll(void *opaque
)
2987 CharDriverState
*chr
= opaque
;
2988 TCPCharDriver
*s
= chr
->opaque
;
2991 s
->max_size
= qemu_chr_can_read(chr
);
2996 #define IAC_BREAK 243
2997 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2999 char *buf
, int *size
)
3001 /* Handle any telnet client's basic IAC options to satisfy char by
3002 * char mode with no echo. All IAC options will be removed from
3003 * the buf and the do_telnetopt variable will be used to track the
3004 * state of the width of the IAC information.
3006 * IAC commands come in sets of 3 bytes with the exception of the
3007 * "IAC BREAK" command and the double IAC.
3013 for (i
= 0; i
< *size
; i
++) {
3014 if (s
->do_telnetopt
> 1) {
3015 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
3016 /* Double IAC means send an IAC */
3020 s
->do_telnetopt
= 1;
3022 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
3023 /* Handle IAC break commands by sending a serial break */
3024 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
3029 if (s
->do_telnetopt
>= 4) {
3030 s
->do_telnetopt
= 1;
3033 if ((unsigned char)buf
[i
] == IAC
) {
3034 s
->do_telnetopt
= 2;
3045 static void tcp_chr_read(void *opaque
)
3047 CharDriverState
*chr
= opaque
;
3048 TCPCharDriver
*s
= chr
->opaque
;
3052 if (!s
->connected
|| s
->max_size
<= 0)
3055 if (len
> s
->max_size
)
3057 size
= recv(s
->fd
, buf
, len
, 0);
3059 /* connection closed */
3061 if (s
->listen_fd
>= 0) {
3062 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
3064 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3067 } else if (size
> 0) {
3068 if (s
->do_telnetopt
)
3069 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
3071 qemu_chr_read(chr
, buf
, size
);
3075 static void tcp_chr_connect(void *opaque
)
3077 CharDriverState
*chr
= opaque
;
3078 TCPCharDriver
*s
= chr
->opaque
;
3081 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
3082 tcp_chr_read
, NULL
, chr
);
3083 qemu_chr_reset(chr
);
3086 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3087 static void tcp_chr_telnet_init(int fd
)
3090 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3091 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3092 send(fd
, (char *)buf
, 3, 0);
3093 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3094 send(fd
, (char *)buf
, 3, 0);
3095 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3096 send(fd
, (char *)buf
, 3, 0);
3097 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3098 send(fd
, (char *)buf
, 3, 0);
3101 static void socket_set_nodelay(int fd
)
3104 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
3107 static void tcp_chr_accept(void *opaque
)
3109 CharDriverState
*chr
= opaque
;
3110 TCPCharDriver
*s
= chr
->opaque
;
3111 struct sockaddr_in saddr
;
3113 struct sockaddr_un uaddr
;
3115 struct sockaddr
*addr
;
3122 len
= sizeof(uaddr
);
3123 addr
= (struct sockaddr
*)&uaddr
;
3127 len
= sizeof(saddr
);
3128 addr
= (struct sockaddr
*)&saddr
;
3130 fd
= accept(s
->listen_fd
, addr
, &len
);
3131 if (fd
< 0 && errno
!= EINTR
) {
3133 } else if (fd
>= 0) {
3134 if (s
->do_telnetopt
)
3135 tcp_chr_telnet_init(fd
);
3139 socket_set_nonblock(fd
);
3141 socket_set_nodelay(fd
);
3143 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
3144 tcp_chr_connect(chr
);
3147 static void tcp_chr_close(CharDriverState
*chr
)
3149 TCPCharDriver
*s
= chr
->opaque
;
3152 if (s
->listen_fd
>= 0)
3153 closesocket(s
->listen_fd
);
3157 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
3161 CharDriverState
*chr
= NULL
;
3162 TCPCharDriver
*s
= NULL
;
3163 int fd
= -1, ret
, err
, val
;
3165 int is_waitconnect
= 1;
3168 struct sockaddr_in saddr
;
3170 struct sockaddr_un uaddr
;
3172 struct sockaddr
*addr
;
3177 addr
= (struct sockaddr
*)&uaddr
;
3178 addrlen
= sizeof(uaddr
);
3179 if (parse_unix_path(&uaddr
, host_str
) < 0)
3184 addr
= (struct sockaddr
*)&saddr
;
3185 addrlen
= sizeof(saddr
);
3186 if (parse_host_port(&saddr
, host_str
) < 0)
3191 while((ptr
= strchr(ptr
,','))) {
3193 if (!strncmp(ptr
,"server",6)) {
3195 } else if (!strncmp(ptr
,"nowait",6)) {
3197 } else if (!strncmp(ptr
,"nodelay",6)) {
3200 printf("Unknown option: %s\n", ptr
);
3207 chr
= qemu_mallocz(sizeof(CharDriverState
));
3210 s
= qemu_mallocz(sizeof(TCPCharDriver
));
3216 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
3219 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3224 if (!is_waitconnect
)
3225 socket_set_nonblock(fd
);
3230 s
->is_unix
= is_unix
;
3231 s
->do_nodelay
= do_nodelay
&& !is_unix
;
3234 chr
->chr_write
= tcp_chr_write
;
3235 chr
->chr_close
= tcp_chr_close
;
3238 /* allow fast reuse */
3242 strncpy(path
, uaddr
.sun_path
, 108);
3249 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3252 ret
= bind(fd
, addr
, addrlen
);
3256 ret
= listen(fd
, 0);
3261 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
3263 s
->do_telnetopt
= 1;
3266 ret
= connect(fd
, addr
, addrlen
);
3268 err
= socket_error();
3269 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3270 } else if (err
== EINPROGRESS
) {
3273 } else if (err
== WSAEALREADY
) {
3285 socket_set_nodelay(fd
);
3287 tcp_chr_connect(chr
);
3289 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
3292 if (is_listen
&& is_waitconnect
) {
3293 printf("QEMU waiting for connection on: %s\n", host_str
);
3294 tcp_chr_accept(chr
);
3295 socket_set_nonblock(s
->listen_fd
);
3307 CharDriverState
*qemu_chr_open(const char *filename
)
3311 if (!strcmp(filename
, "vc")) {
3312 return text_console_init(&display_state
, 0);
3313 } else if (strstart(filename
, "vc:", &p
)) {
3314 return text_console_init(&display_state
, p
);
3315 } else if (!strcmp(filename
, "null")) {
3316 return qemu_chr_open_null();
3318 if (strstart(filename
, "tcp:", &p
)) {
3319 return qemu_chr_open_tcp(p
, 0, 0);
3321 if (strstart(filename
, "telnet:", &p
)) {
3322 return qemu_chr_open_tcp(p
, 1, 0);
3324 if (strstart(filename
, "udp:", &p
)) {
3325 return qemu_chr_open_udp(p
);
3327 if (strstart(filename
, "mon:", &p
)) {
3328 CharDriverState
*drv
= qemu_chr_open(p
);
3330 drv
= qemu_chr_open_mux(drv
);
3331 monitor_init(drv
, !nographic
);
3334 printf("Unable to open driver: %s\n", p
);
3338 if (strstart(filename
, "unix:", &p
)) {
3339 return qemu_chr_open_tcp(p
, 0, 1);
3340 } else if (strstart(filename
, "file:", &p
)) {
3341 return qemu_chr_open_file_out(p
);
3342 } else if (strstart(filename
, "pipe:", &p
)) {
3343 return qemu_chr_open_pipe(p
);
3344 } else if (!strcmp(filename
, "pty")) {
3345 return qemu_chr_open_pty();
3346 } else if (!strcmp(filename
, "stdio")) {
3347 return qemu_chr_open_stdio();
3349 #if defined(__linux__)
3350 if (strstart(filename
, "/dev/parport", NULL
)) {
3351 return qemu_chr_open_pp(filename
);
3354 #if defined(__linux__) || defined(__sun__)
3355 if (strstart(filename
, "/dev/", NULL
)) {
3356 return qemu_chr_open_tty(filename
);
3360 if (strstart(filename
, "COM", NULL
)) {
3361 return qemu_chr_open_win(filename
);
3363 if (strstart(filename
, "pipe:", &p
)) {
3364 return qemu_chr_open_win_pipe(p
);
3366 if (strstart(filename
, "con:", NULL
)) {
3367 return qemu_chr_open_win_con(filename
);
3369 if (strstart(filename
, "file:", &p
)) {
3370 return qemu_chr_open_win_file_out(p
);
3378 void qemu_chr_close(CharDriverState
*chr
)
3381 chr
->chr_close(chr
);
3384 /***********************************************************/
3385 /* network device redirectors */
3387 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
3391 for(i
=0;i
<size
;i
+=16) {
3395 fprintf(f
, "%08x ", i
);
3398 fprintf(f
, " %02x", buf
[i
+j
]);
3403 for(j
=0;j
<len
;j
++) {
3405 if (c
< ' ' || c
> '~')
3407 fprintf(f
, "%c", c
);
3413 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
3416 for(i
= 0; i
< 6; i
++) {
3417 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3430 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
3435 p1
= strchr(p
, sep
);
3441 if (len
> buf_size
- 1)
3443 memcpy(buf
, p
, len
);
3450 int parse_host_src_port(struct sockaddr_in
*haddr
,
3451 struct sockaddr_in
*saddr
,
3452 const char *input_str
)
3454 char *str
= strdup(input_str
);
3455 char *host_str
= str
;
3460 * Chop off any extra arguments at the end of the string which
3461 * would start with a comma, then fill in the src port information
3462 * if it was provided else use the "any address" and "any port".
3464 if ((ptr
= strchr(str
,',')))
3467 if ((src_str
= strchr(input_str
,'@'))) {
3472 if (parse_host_port(haddr
, host_str
) < 0)
3475 if (!src_str
|| *src_str
== '\0')
3478 if (parse_host_port(saddr
, src_str
) < 0)
3489 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
3497 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3499 saddr
->sin_family
= AF_INET
;
3500 if (buf
[0] == '\0') {
3501 saddr
->sin_addr
.s_addr
= 0;
3503 if (isdigit(buf
[0])) {
3504 if (!inet_aton(buf
, &saddr
->sin_addr
))
3507 if ((he
= gethostbyname(buf
)) == NULL
)
3509 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
3512 port
= strtol(p
, (char **)&r
, 0);
3515 saddr
->sin_port
= htons(port
);
3520 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
3525 len
= MIN(108, strlen(str
));
3526 p
= strchr(str
, ',');
3528 len
= MIN(len
, p
- str
);
3530 memset(uaddr
, 0, sizeof(*uaddr
));
3532 uaddr
->sun_family
= AF_UNIX
;
3533 memcpy(uaddr
->sun_path
, str
, len
);
3539 /* find or alloc a new VLAN */
3540 VLANState
*qemu_find_vlan(int id
)
3542 VLANState
**pvlan
, *vlan
;
3543 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3547 vlan
= qemu_mallocz(sizeof(VLANState
));
3552 pvlan
= &first_vlan
;
3553 while (*pvlan
!= NULL
)
3554 pvlan
= &(*pvlan
)->next
;
3559 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
3560 IOReadHandler
*fd_read
,
3561 IOCanRWHandler
*fd_can_read
,
3564 VLANClientState
*vc
, **pvc
;
3565 vc
= qemu_mallocz(sizeof(VLANClientState
));
3568 vc
->fd_read
= fd_read
;
3569 vc
->fd_can_read
= fd_can_read
;
3570 vc
->opaque
= opaque
;
3574 pvc
= &vlan
->first_client
;
3575 while (*pvc
!= NULL
)
3576 pvc
= &(*pvc
)->next
;
3581 int qemu_can_send_packet(VLANClientState
*vc1
)
3583 VLANState
*vlan
= vc1
->vlan
;
3584 VLANClientState
*vc
;
3586 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3588 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
3595 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
3597 VLANState
*vlan
= vc1
->vlan
;
3598 VLANClientState
*vc
;
3601 printf("vlan %d send:\n", vlan
->id
);
3602 hex_dump(stdout
, buf
, size
);
3604 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3606 vc
->fd_read(vc
->opaque
, buf
, size
);
3611 #if defined(CONFIG_SLIRP)
3613 /* slirp network adapter */
3615 static int slirp_inited
;
3616 static VLANClientState
*slirp_vc
;
3618 int slirp_can_output(void)
3620 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
3623 void slirp_output(const uint8_t *pkt
, int pkt_len
)
3626 printf("slirp output:\n");
3627 hex_dump(stdout
, pkt
, pkt_len
);
3631 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
3634 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
3637 printf("slirp input:\n");
3638 hex_dump(stdout
, buf
, size
);
3640 slirp_input(buf
, size
);
3643 static int net_slirp_init(VLANState
*vlan
)
3645 if (!slirp_inited
) {
3649 slirp_vc
= qemu_new_vlan_client(vlan
,
3650 slirp_receive
, NULL
, NULL
);
3651 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
3655 static void net_slirp_redir(const char *redir_str
)
3660 struct in_addr guest_addr
;
3661 int host_port
, guest_port
;
3663 if (!slirp_inited
) {
3669 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3671 if (!strcmp(buf
, "tcp")) {
3673 } else if (!strcmp(buf
, "udp")) {
3679 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3681 host_port
= strtol(buf
, &r
, 0);
3685 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3687 if (buf
[0] == '\0') {
3688 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
3690 if (!inet_aton(buf
, &guest_addr
))
3693 guest_port
= strtol(p
, &r
, 0);
3697 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3698 fprintf(stderr
, "qemu: could not set up redirection\n");
3703 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3711 static void smb_exit(void)
3715 char filename
[1024];
3717 /* erase all the files in the directory */
3718 d
= opendir(smb_dir
);
3723 if (strcmp(de
->d_name
, ".") != 0 &&
3724 strcmp(de
->d_name
, "..") != 0) {
3725 snprintf(filename
, sizeof(filename
), "%s/%s",
3726 smb_dir
, de
->d_name
);
3734 /* automatic user mode samba server configuration */
3735 void net_slirp_smb(const char *exported_dir
)
3737 char smb_conf
[1024];
3738 char smb_cmdline
[1024];
3741 if (!slirp_inited
) {
3746 /* XXX: better tmp dir construction */
3747 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3748 if (mkdir(smb_dir
, 0700) < 0) {
3749 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3752 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3754 f
= fopen(smb_conf
, "w");
3756 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3763 "socket address=127.0.0.1\n"
3764 "pid directory=%s\n"
3765 "lock directory=%s\n"
3766 "log file=%s/log.smbd\n"
3767 "smb passwd file=%s/smbpasswd\n"
3768 "security = share\n"
3783 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3784 SMBD_COMMAND
, smb_conf
);
3786 slirp_add_exec(0, smb_cmdline
, 4, 139);
3789 #endif /* !defined(_WIN32) */
3790 void do_info_slirp(void)
3795 #endif /* CONFIG_SLIRP */
3797 #if !defined(_WIN32)
3799 typedef struct TAPState
{
3800 VLANClientState
*vc
;
3802 char down_script
[1024];
3805 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3807 TAPState
*s
= opaque
;
3810 ret
= write(s
->fd
, buf
, size
);
3811 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3818 static void tap_send(void *opaque
)
3820 TAPState
*s
= opaque
;
3827 sbuf
.maxlen
= sizeof(buf
);
3829 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
3831 size
= read(s
->fd
, buf
, sizeof(buf
));
3834 qemu_send_packet(s
->vc
, buf
, size
);
3840 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3844 s
= qemu_mallocz(sizeof(TAPState
));
3848 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3849 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3850 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3854 #if defined (_BSD) || defined (__FreeBSD_kernel__)
3855 static int tap_open(char *ifname
, int ifname_size
)
3861 TFR(fd
= open("/dev/tap", O_RDWR
));
3863 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3868 dev
= devname(s
.st_rdev
, S_IFCHR
);
3869 pstrcpy(ifname
, ifname_size
, dev
);
3871 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3874 #elif defined(__sun__)
3875 #define TUNNEWPPA (('T'<<16) | 0x0001)
3877 * Allocate TAP device, returns opened fd.
3878 * Stores dev name in the first arg(must be large enough).
3880 int tap_alloc(char *dev
)
3882 int tap_fd
, if_fd
, ppa
= -1;
3883 static int ip_fd
= 0;
3886 static int arp_fd
= 0;
3887 int ip_muxid
, arp_muxid
;
3888 struct strioctl strioc_if
, strioc_ppa
;
3889 int link_type
= I_PLINK
;;
3891 char actual_name
[32] = "";
3893 memset(&ifr
, 0x0, sizeof(ifr
));
3897 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
3901 /* Check if IP device was opened */
3905 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
3907 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
3911 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
3913 syslog(LOG_ERR
, "Can't open /dev/tap");
3917 /* Assign a new PPA and get its unit number. */
3918 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
3919 strioc_ppa
.ic_timout
= 0;
3920 strioc_ppa
.ic_len
= sizeof(ppa
);
3921 strioc_ppa
.ic_dp
= (char *)&ppa
;
3922 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
3923 syslog (LOG_ERR
, "Can't assign new interface");
3925 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
3927 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
3930 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
3931 syslog(LOG_ERR
, "Can't push IP module");
3935 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
3936 syslog(LOG_ERR
, "Can't get flags\n");
3938 snprintf (actual_name
, 32, "tap%d", ppa
);
3939 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3942 /* Assign ppa according to the unit number returned by tun device */
3944 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
3945 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
3946 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
3947 syslog (LOG_ERR
, "Can't get flags\n");
3948 /* Push arp module to if_fd */
3949 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
3950 syslog (LOG_ERR
, "Can't push ARP module (2)");
3952 /* Push arp module to ip_fd */
3953 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
3954 syslog (LOG_ERR
, "I_POP failed\n");
3955 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
3956 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
3958 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
3960 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
3962 /* Set ifname to arp */
3963 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
3964 strioc_if
.ic_timout
= 0;
3965 strioc_if
.ic_len
= sizeof(ifr
);
3966 strioc_if
.ic_dp
= (char *)&ifr
;
3967 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
3968 syslog (LOG_ERR
, "Can't set ifname to arp\n");
3971 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
3972 syslog(LOG_ERR
, "Can't link TAP device to IP");
3976 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
3977 syslog (LOG_ERR
, "Can't link TAP device to ARP");
3981 memset(&ifr
, 0x0, sizeof(ifr
));
3982 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3983 ifr
.lifr_ip_muxid
= ip_muxid
;
3984 ifr
.lifr_arp_muxid
= arp_muxid
;
3986 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
3988 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
3989 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
3990 syslog (LOG_ERR
, "Can't set multiplexor id");
3993 sprintf(dev
, "tap%d", ppa
);
3997 static int tap_open(char *ifname
, int ifname_size
)
4001 if( (fd
= tap_alloc(dev
)) < 0 ){
4002 fprintf(stderr
, "Cannot allocate TAP device\n");
4005 pstrcpy(ifname
, ifname_size
, dev
);
4006 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
4010 static int tap_open(char *ifname
, int ifname_size
)
4015 TFR(fd
= open("/dev/net/tun", O_RDWR
));
4017 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
4020 memset(&ifr
, 0, sizeof(ifr
));
4021 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
4022 if (ifname
[0] != '\0')
4023 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
4025 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
4026 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
4028 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
4032 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
4033 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
4038 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
4044 /* try to launch network script */
4048 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
4049 for (i
= 0; i
< open_max
; i
++)
4050 if (i
!= STDIN_FILENO
&&
4051 i
!= STDOUT_FILENO
&&
4052 i
!= STDERR_FILENO
&&
4057 *parg
++ = (char *)setup_script
;
4058 *parg
++ = (char *)ifname
;
4060 execv(setup_script
, args
);
4063 while (waitpid(pid
, &status
, 0) != pid
);
4064 if (!WIFEXITED(status
) ||
4065 WEXITSTATUS(status
) != 0) {
4066 fprintf(stderr
, "%s: could not launch network script\n",
4074 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
4075 const char *setup_script
, const char *down_script
)
4081 if (ifname1
!= NULL
)
4082 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
4085 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
4089 if (!setup_script
|| !strcmp(setup_script
, "no"))
4091 if (setup_script
[0] != '\0') {
4092 if (launch_script(setup_script
, ifname
, fd
))
4095 s
= net_tap_fd_init(vlan
, fd
);
4098 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4099 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
4100 if (down_script
&& strcmp(down_script
, "no"))
4101 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
4105 #endif /* !_WIN32 */
4107 /* network connection */
4108 typedef struct NetSocketState
{
4109 VLANClientState
*vc
;
4111 int state
; /* 0 = getting length, 1 = getting data */
4115 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
4118 typedef struct NetSocketListenState
{
4121 } NetSocketListenState
;
4123 /* XXX: we consider we can send the whole packet without blocking */
4124 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
4126 NetSocketState
*s
= opaque
;
4130 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
4131 send_all(s
->fd
, buf
, size
);
4134 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
4136 NetSocketState
*s
= opaque
;
4137 sendto(s
->fd
, buf
, size
, 0,
4138 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
4141 static void net_socket_send(void *opaque
)
4143 NetSocketState
*s
= opaque
;
4148 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
4150 err
= socket_error();
4151 if (err
!= EWOULDBLOCK
)
4153 } else if (size
== 0) {
4154 /* end of connection */
4156 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
4162 /* reassemble a packet from the network */
4168 memcpy(s
->buf
+ s
->index
, buf
, l
);
4172 if (s
->index
== 4) {
4174 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
4180 l
= s
->packet_len
- s
->index
;
4183 memcpy(s
->buf
+ s
->index
, buf
, l
);
4187 if (s
->index
>= s
->packet_len
) {
4188 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
4197 static void net_socket_send_dgram(void *opaque
)
4199 NetSocketState
*s
= opaque
;
4202 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
4206 /* end of connection */
4207 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
4210 qemu_send_packet(s
->vc
, s
->buf
, size
);
4213 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
4218 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
4219 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
4220 inet_ntoa(mcastaddr
->sin_addr
),
4221 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
4225 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
4227 perror("socket(PF_INET, SOCK_DGRAM)");
4232 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
4233 (const char *)&val
, sizeof(val
));
4235 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
4239 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
4245 /* Add host to multicast group */
4246 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
4247 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
4249 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
4250 (const char *)&imr
, sizeof(struct ip_mreq
));
4252 perror("setsockopt(IP_ADD_MEMBERSHIP)");
4256 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
4258 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
4259 (const char *)&val
, sizeof(val
));
4261 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
4265 socket_set_nonblock(fd
);
4273 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
4276 struct sockaddr_in saddr
;
4278 socklen_t saddr_len
;
4281 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
4282 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
4283 * by ONLY ONE process: we must "clone" this dgram socket --jjo
4287 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
4289 if (saddr
.sin_addr
.s_addr
==0) {
4290 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
4294 /* clone dgram socket */
4295 newfd
= net_socket_mcast_create(&saddr
);
4297 /* error already reported by net_socket_mcast_create() */
4301 /* clone newfd to fd, close newfd */
4306 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
4307 fd
, strerror(errno
));
4312 s
= qemu_mallocz(sizeof(NetSocketState
));
4317 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
4318 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
4320 /* mcast: save bound address as dst */
4321 if (is_connected
) s
->dgram_dst
=saddr
;
4323 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4324 "socket: fd=%d (%s mcast=%s:%d)",
4325 fd
, is_connected
? "cloned" : "",
4326 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4330 static void net_socket_connect(void *opaque
)
4332 NetSocketState
*s
= opaque
;
4333 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
4336 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
4340 s
= qemu_mallocz(sizeof(NetSocketState
));
4344 s
->vc
= qemu_new_vlan_client(vlan
,
4345 net_socket_receive
, NULL
, s
);
4346 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4347 "socket: fd=%d", fd
);
4349 net_socket_connect(s
);
4351 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
4356 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
4359 int so_type
=-1, optlen
=sizeof(so_type
);
4361 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
4362 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
4367 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
4369 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
4371 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
4372 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
4373 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
4378 static void net_socket_accept(void *opaque
)
4380 NetSocketListenState
*s
= opaque
;
4382 struct sockaddr_in saddr
;
4387 len
= sizeof(saddr
);
4388 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
4389 if (fd
< 0 && errno
!= EINTR
) {
4391 } else if (fd
>= 0) {
4395 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
4399 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
4400 "socket: connection from %s:%d",
4401 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4405 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
4407 NetSocketListenState
*s
;
4409 struct sockaddr_in saddr
;
4411 if (parse_host_port(&saddr
, host_str
) < 0)
4414 s
= qemu_mallocz(sizeof(NetSocketListenState
));
4418 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4423 socket_set_nonblock(fd
);
4425 /* allow fast reuse */
4427 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
4429 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4434 ret
= listen(fd
, 0);
4441 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
4445 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
4448 int fd
, connected
, ret
, err
;
4449 struct sockaddr_in saddr
;
4451 if (parse_host_port(&saddr
, host_str
) < 0)
4454 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4459 socket_set_nonblock(fd
);
4463 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4465 err
= socket_error();
4466 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
4467 } else if (err
== EINPROGRESS
) {
4470 } else if (err
== WSAEALREADY
) {
4483 s
= net_socket_fd_init(vlan
, fd
, connected
);
4486 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4487 "socket: connect to %s:%d",
4488 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4492 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
4496 struct sockaddr_in saddr
;
4498 if (parse_host_port(&saddr
, host_str
) < 0)
4502 fd
= net_socket_mcast_create(&saddr
);
4506 s
= net_socket_fd_init(vlan
, fd
, 0);
4510 s
->dgram_dst
= saddr
;
4512 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4513 "socket: mcast=%s:%d",
4514 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4519 static int get_param_value(char *buf
, int buf_size
,
4520 const char *tag
, const char *str
)
4529 while (*p
!= '\0' && *p
!= '=') {
4530 if ((q
- option
) < sizeof(option
) - 1)
4538 if (!strcmp(tag
, option
)) {
4540 while (*p
!= '\0' && *p
!= ',') {
4541 if ((q
- buf
) < buf_size
- 1)
4548 while (*p
!= '\0' && *p
!= ',') {
4559 static int net_client_init(const char *str
)
4570 while (*p
!= '\0' && *p
!= ',') {
4571 if ((q
- device
) < sizeof(device
) - 1)
4579 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
4580 vlan_id
= strtol(buf
, NULL
, 0);
4582 vlan
= qemu_find_vlan(vlan_id
);
4584 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
4587 if (!strcmp(device
, "nic")) {
4591 if (nb_nics
>= MAX_NICS
) {
4592 fprintf(stderr
, "Too Many NICs\n");
4595 nd
= &nd_table
[nb_nics
];
4596 macaddr
= nd
->macaddr
;
4602 macaddr
[5] = 0x56 + nb_nics
;
4604 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
4605 if (parse_macaddr(macaddr
, buf
) < 0) {
4606 fprintf(stderr
, "invalid syntax for ethernet address\n");
4610 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
4611 nd
->model
= strdup(buf
);
4615 vlan
->nb_guest_devs
++;
4618 if (!strcmp(device
, "none")) {
4619 /* does nothing. It is needed to signal that no network cards
4624 if (!strcmp(device
, "user")) {
4625 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
4626 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
4628 vlan
->nb_host_devs
++;
4629 ret
= net_slirp_init(vlan
);
4633 if (!strcmp(device
, "tap")) {
4635 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4636 fprintf(stderr
, "tap: no interface name\n");
4639 vlan
->nb_host_devs
++;
4640 ret
= tap_win32_init(vlan
, ifname
);
4643 if (!strcmp(device
, "tap")) {
4645 char setup_script
[1024], down_script
[1024];
4647 vlan
->nb_host_devs
++;
4648 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4649 fd
= strtol(buf
, NULL
, 0);
4651 if (net_tap_fd_init(vlan
, fd
))
4654 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4657 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
4658 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
4660 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
4661 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
4663 ret
= net_tap_init(vlan
, ifname
, setup_script
, down_script
);
4667 if (!strcmp(device
, "socket")) {
4668 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4670 fd
= strtol(buf
, NULL
, 0);
4672 if (net_socket_fd_init(vlan
, fd
, 1))
4674 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
4675 ret
= net_socket_listen_init(vlan
, buf
);
4676 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
4677 ret
= net_socket_connect_init(vlan
, buf
);
4678 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
4679 ret
= net_socket_mcast_init(vlan
, buf
);
4681 fprintf(stderr
, "Unknown socket options: %s\n", p
);
4684 vlan
->nb_host_devs
++;
4687 fprintf(stderr
, "Unknown network device: %s\n", device
);
4691 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
4697 void do_info_network(void)
4700 VLANClientState
*vc
;
4702 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
4703 term_printf("VLAN %d devices:\n", vlan
->id
);
4704 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
4705 term_printf(" %s\n", vc
->info_str
);
4709 /***********************************************************/
4712 static USBPort
*used_usb_ports
;
4713 static USBPort
*free_usb_ports
;
4715 /* ??? Maybe change this to register a hub to keep track of the topology. */
4716 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
4717 usb_attachfn attach
)
4719 port
->opaque
= opaque
;
4720 port
->index
= index
;
4721 port
->attach
= attach
;
4722 port
->next
= free_usb_ports
;
4723 free_usb_ports
= port
;
4726 static int usb_device_add(const char *devname
)
4732 if (!free_usb_ports
)
4735 if (strstart(devname
, "host:", &p
)) {
4736 dev
= usb_host_device_open(p
);
4737 } else if (!strcmp(devname
, "mouse")) {
4738 dev
= usb_mouse_init();
4739 } else if (!strcmp(devname
, "tablet")) {
4740 dev
= usb_tablet_init();
4741 } else if (!strcmp(devname
, "keyboard")) {
4742 dev
= usb_keyboard_init();
4743 } else if (strstart(devname
, "disk:", &p
)) {
4744 dev
= usb_msd_init(p
);
4745 } else if (!strcmp(devname
, "wacom-tablet")) {
4746 dev
= usb_wacom_init();
4753 /* Find a USB port to add the device to. */
4754 port
= free_usb_ports
;
4758 /* Create a new hub and chain it on. */
4759 free_usb_ports
= NULL
;
4760 port
->next
= used_usb_ports
;
4761 used_usb_ports
= port
;
4763 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
4764 usb_attach(port
, hub
);
4765 port
= free_usb_ports
;
4768 free_usb_ports
= port
->next
;
4769 port
->next
= used_usb_ports
;
4770 used_usb_ports
= port
;
4771 usb_attach(port
, dev
);
4775 static int usb_device_del(const char *devname
)
4783 if (!used_usb_ports
)
4786 p
= strchr(devname
, '.');
4789 bus_num
= strtoul(devname
, NULL
, 0);
4790 addr
= strtoul(p
+ 1, NULL
, 0);
4794 lastp
= &used_usb_ports
;
4795 port
= used_usb_ports
;
4796 while (port
&& port
->dev
->addr
!= addr
) {
4797 lastp
= &port
->next
;
4805 *lastp
= port
->next
;
4806 usb_attach(port
, NULL
);
4807 dev
->handle_destroy(dev
);
4808 port
->next
= free_usb_ports
;
4809 free_usb_ports
= port
;
4813 void do_usb_add(const char *devname
)
4816 ret
= usb_device_add(devname
);
4818 term_printf("Could not add USB device '%s'\n", devname
);
4821 void do_usb_del(const char *devname
)
4824 ret
= usb_device_del(devname
);
4826 term_printf("Could not remove USB device '%s'\n", devname
);
4833 const char *speed_str
;
4836 term_printf("USB support not enabled\n");
4840 for (port
= used_usb_ports
; port
; port
= port
->next
) {
4844 switch(dev
->speed
) {
4848 case USB_SPEED_FULL
:
4851 case USB_SPEED_HIGH
:
4858 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
4859 0, dev
->addr
, speed_str
, dev
->devname
);
4863 /***********************************************************/
4864 /* PCMCIA/Cardbus */
4866 static struct pcmcia_socket_entry_s
{
4867 struct pcmcia_socket_s
*socket
;
4868 struct pcmcia_socket_entry_s
*next
;
4869 } *pcmcia_sockets
= 0;
4871 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
4873 struct pcmcia_socket_entry_s
*entry
;
4875 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
4876 entry
->socket
= socket
;
4877 entry
->next
= pcmcia_sockets
;
4878 pcmcia_sockets
= entry
;
4881 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
4883 struct pcmcia_socket_entry_s
*entry
, **ptr
;
4885 ptr
= &pcmcia_sockets
;
4886 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
4887 if (entry
->socket
== socket
) {
4893 void pcmcia_info(void)
4895 struct pcmcia_socket_entry_s
*iter
;
4896 if (!pcmcia_sockets
)
4897 term_printf("No PCMCIA sockets\n");
4899 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
4900 term_printf("%s: %s\n", iter
->socket
->slot_string
,
4901 iter
->socket
->attached
? iter
->socket
->card_string
:
4905 /***********************************************************/
4908 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4912 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4916 static void dumb_refresh(DisplayState
*ds
)
4918 #if defined(CONFIG_SDL)
4923 static void dumb_display_init(DisplayState
*ds
)
4928 ds
->dpy_update
= dumb_update
;
4929 ds
->dpy_resize
= dumb_resize
;
4930 ds
->dpy_refresh
= dumb_refresh
;
4933 /***********************************************************/
4936 #define MAX_IO_HANDLERS 64
4938 typedef struct IOHandlerRecord
{
4940 IOCanRWHandler
*fd_read_poll
;
4942 IOHandler
*fd_write
;
4945 /* temporary data */
4947 struct IOHandlerRecord
*next
;
4950 static IOHandlerRecord
*first_io_handler
;
4952 /* XXX: fd_read_poll should be suppressed, but an API change is
4953 necessary in the character devices to suppress fd_can_read(). */
4954 int qemu_set_fd_handler2(int fd
,
4955 IOCanRWHandler
*fd_read_poll
,
4957 IOHandler
*fd_write
,
4960 IOHandlerRecord
**pioh
, *ioh
;
4962 if (!fd_read
&& !fd_write
) {
4963 pioh
= &first_io_handler
;
4968 if (ioh
->fd
== fd
) {
4975 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4979 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4982 ioh
->next
= first_io_handler
;
4983 first_io_handler
= ioh
;
4986 ioh
->fd_read_poll
= fd_read_poll
;
4987 ioh
->fd_read
= fd_read
;
4988 ioh
->fd_write
= fd_write
;
4989 ioh
->opaque
= opaque
;
4995 int qemu_set_fd_handler(int fd
,
4997 IOHandler
*fd_write
,
5000 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
5003 /***********************************************************/
5004 /* Polling handling */
5006 typedef struct PollingEntry
{
5009 struct PollingEntry
*next
;
5012 static PollingEntry
*first_polling_entry
;
5014 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
5016 PollingEntry
**ppe
, *pe
;
5017 pe
= qemu_mallocz(sizeof(PollingEntry
));
5021 pe
->opaque
= opaque
;
5022 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
5027 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
5029 PollingEntry
**ppe
, *pe
;
5030 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
5032 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
5041 /***********************************************************/
5042 /* Wait objects support */
5043 typedef struct WaitObjects
{
5045 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
5046 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
5047 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
5050 static WaitObjects wait_objects
= {0};
5052 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
5054 WaitObjects
*w
= &wait_objects
;
5056 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
5058 w
->events
[w
->num
] = handle
;
5059 w
->func
[w
->num
] = func
;
5060 w
->opaque
[w
->num
] = opaque
;
5065 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
5068 WaitObjects
*w
= &wait_objects
;
5071 for (i
= 0; i
< w
->num
; i
++) {
5072 if (w
->events
[i
] == handle
)
5075 w
->events
[i
] = w
->events
[i
+ 1];
5076 w
->func
[i
] = w
->func
[i
+ 1];
5077 w
->opaque
[i
] = w
->opaque
[i
+ 1];
5085 /***********************************************************/
5086 /* savevm/loadvm support */
5088 #define IO_BUF_SIZE 32768
5092 BlockDriverState
*bs
;
5095 int64_t base_offset
;
5096 int64_t buf_offset
; /* start of buffer when writing, end of buffer
5099 int buf_size
; /* 0 when writing */
5100 uint8_t buf
[IO_BUF_SIZE
];
5103 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
5107 f
= qemu_mallocz(sizeof(QEMUFile
));
5110 if (!strcmp(mode
, "wb")) {
5112 } else if (!strcmp(mode
, "rb")) {
5117 f
->outfile
= fopen(filename
, mode
);
5129 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
5133 f
= qemu_mallocz(sizeof(QEMUFile
));
5138 f
->is_writable
= is_writable
;
5139 f
->base_offset
= offset
;
5143 void qemu_fflush(QEMUFile
*f
)
5145 if (!f
->is_writable
)
5147 if (f
->buf_index
> 0) {
5149 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
5150 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
5152 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
5153 f
->buf
, f
->buf_index
);
5155 f
->buf_offset
+= f
->buf_index
;
5160 static void qemu_fill_buffer(QEMUFile
*f
)
5167 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
5168 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
5172 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
5173 f
->buf
, IO_BUF_SIZE
);
5179 f
->buf_offset
+= len
;
5182 void qemu_fclose(QEMUFile
*f
)
5192 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
5196 l
= IO_BUF_SIZE
- f
->buf_index
;
5199 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
5203 if (f
->buf_index
>= IO_BUF_SIZE
)
5208 void qemu_put_byte(QEMUFile
*f
, int v
)
5210 f
->buf
[f
->buf_index
++] = v
;
5211 if (f
->buf_index
>= IO_BUF_SIZE
)
5215 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
5221 l
= f
->buf_size
- f
->buf_index
;
5223 qemu_fill_buffer(f
);
5224 l
= f
->buf_size
- f
->buf_index
;
5230 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
5235 return size1
- size
;
5238 int qemu_get_byte(QEMUFile
*f
)
5240 if (f
->buf_index
>= f
->buf_size
) {
5241 qemu_fill_buffer(f
);
5242 if (f
->buf_index
>= f
->buf_size
)
5245 return f
->buf
[f
->buf_index
++];
5248 int64_t qemu_ftell(QEMUFile
*f
)
5250 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
5253 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
5255 if (whence
== SEEK_SET
) {
5257 } else if (whence
== SEEK_CUR
) {
5258 pos
+= qemu_ftell(f
);
5260 /* SEEK_END not supported */
5263 if (f
->is_writable
) {
5265 f
->buf_offset
= pos
;
5267 f
->buf_offset
= pos
;
5274 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
5276 qemu_put_byte(f
, v
>> 8);
5277 qemu_put_byte(f
, v
);
5280 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
5282 qemu_put_byte(f
, v
>> 24);
5283 qemu_put_byte(f
, v
>> 16);
5284 qemu_put_byte(f
, v
>> 8);
5285 qemu_put_byte(f
, v
);
5288 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
5290 qemu_put_be32(f
, v
>> 32);
5291 qemu_put_be32(f
, v
);
5294 unsigned int qemu_get_be16(QEMUFile
*f
)
5297 v
= qemu_get_byte(f
) << 8;
5298 v
|= qemu_get_byte(f
);
5302 unsigned int qemu_get_be32(QEMUFile
*f
)
5305 v
= qemu_get_byte(f
) << 24;
5306 v
|= qemu_get_byte(f
) << 16;
5307 v
|= qemu_get_byte(f
) << 8;
5308 v
|= qemu_get_byte(f
);
5312 uint64_t qemu_get_be64(QEMUFile
*f
)
5315 v
= (uint64_t)qemu_get_be32(f
) << 32;
5316 v
|= qemu_get_be32(f
);
5320 typedef struct SaveStateEntry
{
5324 SaveStateHandler
*save_state
;
5325 LoadStateHandler
*load_state
;
5327 struct SaveStateEntry
*next
;
5330 static SaveStateEntry
*first_se
;
5332 int register_savevm(const char *idstr
,
5335 SaveStateHandler
*save_state
,
5336 LoadStateHandler
*load_state
,
5339 SaveStateEntry
*se
, **pse
;
5341 se
= qemu_malloc(sizeof(SaveStateEntry
));
5344 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
5345 se
->instance_id
= instance_id
;
5346 se
->version_id
= version_id
;
5347 se
->save_state
= save_state
;
5348 se
->load_state
= load_state
;
5349 se
->opaque
= opaque
;
5352 /* add at the end of list */
5354 while (*pse
!= NULL
)
5355 pse
= &(*pse
)->next
;
5360 #define QEMU_VM_FILE_MAGIC 0x5145564d
5361 #define QEMU_VM_FILE_VERSION 0x00000002
5363 int qemu_savevm_state(QEMUFile
*f
)
5367 int64_t cur_pos
, len_pos
, total_len_pos
;
5369 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
5370 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
5371 total_len_pos
= qemu_ftell(f
);
5372 qemu_put_be64(f
, 0); /* total size */
5374 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5376 len
= strlen(se
->idstr
);
5377 qemu_put_byte(f
, len
);
5378 qemu_put_buffer(f
, se
->idstr
, len
);
5380 qemu_put_be32(f
, se
->instance_id
);
5381 qemu_put_be32(f
, se
->version_id
);
5383 /* record size: filled later */
5384 len_pos
= qemu_ftell(f
);
5385 qemu_put_be32(f
, 0);
5387 se
->save_state(f
, se
->opaque
);
5389 /* fill record size */
5390 cur_pos
= qemu_ftell(f
);
5391 len
= cur_pos
- len_pos
- 4;
5392 qemu_fseek(f
, len_pos
, SEEK_SET
);
5393 qemu_put_be32(f
, len
);
5394 qemu_fseek(f
, cur_pos
, SEEK_SET
);
5396 cur_pos
= qemu_ftell(f
);
5397 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
5398 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
5399 qemu_fseek(f
, cur_pos
, SEEK_SET
);
5405 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
5409 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5410 if (!strcmp(se
->idstr
, idstr
) &&
5411 instance_id
== se
->instance_id
)
5417 int qemu_loadvm_state(QEMUFile
*f
)
5420 int len
, ret
, instance_id
, record_len
, version_id
;
5421 int64_t total_len
, end_pos
, cur_pos
;
5425 v
= qemu_get_be32(f
);
5426 if (v
!= QEMU_VM_FILE_MAGIC
)
5428 v
= qemu_get_be32(f
);
5429 if (v
!= QEMU_VM_FILE_VERSION
) {
5434 total_len
= qemu_get_be64(f
);
5435 end_pos
= total_len
+ qemu_ftell(f
);
5437 if (qemu_ftell(f
) >= end_pos
)
5439 len
= qemu_get_byte(f
);
5440 qemu_get_buffer(f
, idstr
, len
);
5442 instance_id
= qemu_get_be32(f
);
5443 version_id
= qemu_get_be32(f
);
5444 record_len
= qemu_get_be32(f
);
5446 printf("idstr=%s instance=0x%x version=%d len=%d\n",
5447 idstr
, instance_id
, version_id
, record_len
);
5449 cur_pos
= qemu_ftell(f
);
5450 se
= find_se(idstr
, instance_id
);
5452 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
5453 instance_id
, idstr
);
5455 ret
= se
->load_state(f
, se
->opaque
, version_id
);
5457 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
5458 instance_id
, idstr
);
5461 /* always seek to exact end of record */
5462 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
5469 /* device can contain snapshots */
5470 static int bdrv_can_snapshot(BlockDriverState
*bs
)
5473 !bdrv_is_removable(bs
) &&
5474 !bdrv_is_read_only(bs
));
5477 /* device must be snapshots in order to have a reliable snapshot */
5478 static int bdrv_has_snapshot(BlockDriverState
*bs
)
5481 !bdrv_is_removable(bs
) &&
5482 !bdrv_is_read_only(bs
));
5485 static BlockDriverState
*get_bs_snapshots(void)
5487 BlockDriverState
*bs
;
5491 return bs_snapshots
;
5492 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5494 if (bdrv_can_snapshot(bs
))
5503 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
5506 QEMUSnapshotInfo
*sn_tab
, *sn
;
5510 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5513 for(i
= 0; i
< nb_sns
; i
++) {
5515 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
5525 void do_savevm(const char *name
)
5527 BlockDriverState
*bs
, *bs1
;
5528 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
5529 int must_delete
, ret
, i
;
5530 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5532 int saved_vm_running
;
5539 bs
= get_bs_snapshots();
5541 term_printf("No block device can accept snapshots\n");
5545 /* ??? Should this occur after vm_stop? */
5548 saved_vm_running
= vm_running
;
5553 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
5558 memset(sn
, 0, sizeof(*sn
));
5560 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
5561 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
5564 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
5567 /* fill auxiliary fields */
5570 sn
->date_sec
= tb
.time
;
5571 sn
->date_nsec
= tb
.millitm
* 1000000;
5573 gettimeofday(&tv
, NULL
);
5574 sn
->date_sec
= tv
.tv_sec
;
5575 sn
->date_nsec
= tv
.tv_usec
* 1000;
5577 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
5579 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5580 term_printf("Device %s does not support VM state snapshots\n",
5581 bdrv_get_device_name(bs
));
5585 /* save the VM state */
5586 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
5588 term_printf("Could not open VM state file\n");
5591 ret
= qemu_savevm_state(f
);
5592 sn
->vm_state_size
= qemu_ftell(f
);
5595 term_printf("Error %d while writing VM\n", ret
);
5599 /* create the snapshots */
5601 for(i
= 0; i
< MAX_DISKS
; i
++) {
5603 if (bdrv_has_snapshot(bs1
)) {
5605 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
5607 term_printf("Error while deleting snapshot on '%s'\n",
5608 bdrv_get_device_name(bs1
));
5611 ret
= bdrv_snapshot_create(bs1
, sn
);
5613 term_printf("Error while creating snapshot on '%s'\n",
5614 bdrv_get_device_name(bs1
));
5620 if (saved_vm_running
)
5624 void do_loadvm(const char *name
)
5626 BlockDriverState
*bs
, *bs1
;
5627 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5630 int saved_vm_running
;
5632 bs
= get_bs_snapshots();
5634 term_printf("No block device supports snapshots\n");
5638 /* Flush all IO requests so they don't interfere with the new state. */
5641 saved_vm_running
= vm_running
;
5644 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5646 if (bdrv_has_snapshot(bs1
)) {
5647 ret
= bdrv_snapshot_goto(bs1
, name
);
5650 term_printf("Warning: ");
5653 term_printf("Snapshots not supported on device '%s'\n",
5654 bdrv_get_device_name(bs1
));
5657 term_printf("Could not find snapshot '%s' on device '%s'\n",
5658 name
, bdrv_get_device_name(bs1
));
5661 term_printf("Error %d while activating snapshot on '%s'\n",
5662 ret
, bdrv_get_device_name(bs1
));
5665 /* fatal on snapshot block device */
5672 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5673 term_printf("Device %s does not support VM state snapshots\n",
5674 bdrv_get_device_name(bs
));
5678 /* restore the VM state */
5679 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
5681 term_printf("Could not open VM state file\n");
5684 ret
= qemu_loadvm_state(f
);
5687 term_printf("Error %d while loading VM state\n", ret
);
5690 if (saved_vm_running
)
5694 void do_delvm(const char *name
)
5696 BlockDriverState
*bs
, *bs1
;
5699 bs
= get_bs_snapshots();
5701 term_printf("No block device supports snapshots\n");
5705 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5707 if (bdrv_has_snapshot(bs1
)) {
5708 ret
= bdrv_snapshot_delete(bs1
, name
);
5710 if (ret
== -ENOTSUP
)
5711 term_printf("Snapshots not supported on device '%s'\n",
5712 bdrv_get_device_name(bs1
));
5714 term_printf("Error %d while deleting snapshot on '%s'\n",
5715 ret
, bdrv_get_device_name(bs1
));
5721 void do_info_snapshots(void)
5723 BlockDriverState
*bs
, *bs1
;
5724 QEMUSnapshotInfo
*sn_tab
, *sn
;
5728 bs
= get_bs_snapshots();
5730 term_printf("No available block device supports snapshots\n");
5733 term_printf("Snapshot devices:");
5734 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5736 if (bdrv_has_snapshot(bs1
)) {
5738 term_printf(" %s", bdrv_get_device_name(bs1
));
5743 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5745 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
5748 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
5749 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
5750 for(i
= 0; i
< nb_sns
; i
++) {
5752 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
5757 /***********************************************************/
5758 /* cpu save/restore */
5760 #if defined(TARGET_I386)
5762 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
5764 qemu_put_be32(f
, dt
->selector
);
5765 qemu_put_betl(f
, dt
->base
);
5766 qemu_put_be32(f
, dt
->limit
);
5767 qemu_put_be32(f
, dt
->flags
);
5770 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
5772 dt
->selector
= qemu_get_be32(f
);
5773 dt
->base
= qemu_get_betl(f
);
5774 dt
->limit
= qemu_get_be32(f
);
5775 dt
->flags
= qemu_get_be32(f
);
5778 void cpu_save(QEMUFile
*f
, void *opaque
)
5780 CPUState
*env
= opaque
;
5781 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
5785 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5786 qemu_put_betls(f
, &env
->regs
[i
]);
5787 qemu_put_betls(f
, &env
->eip
);
5788 qemu_put_betls(f
, &env
->eflags
);
5789 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
5790 qemu_put_be32s(f
, &hflags
);
5794 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
5796 for(i
= 0; i
< 8; i
++) {
5797 fptag
|= ((!env
->fptags
[i
]) << i
);
5800 qemu_put_be16s(f
, &fpuc
);
5801 qemu_put_be16s(f
, &fpus
);
5802 qemu_put_be16s(f
, &fptag
);
5804 #ifdef USE_X86LDOUBLE
5809 qemu_put_be16s(f
, &fpregs_format
);
5811 for(i
= 0; i
< 8; i
++) {
5812 #ifdef USE_X86LDOUBLE
5816 /* we save the real CPU data (in case of MMX usage only 'mant'
5817 contains the MMX register */
5818 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
5819 qemu_put_be64(f
, mant
);
5820 qemu_put_be16(f
, exp
);
5823 /* if we use doubles for float emulation, we save the doubles to
5824 avoid losing information in case of MMX usage. It can give
5825 problems if the image is restored on a CPU where long
5826 doubles are used instead. */
5827 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
5831 for(i
= 0; i
< 6; i
++)
5832 cpu_put_seg(f
, &env
->segs
[i
]);
5833 cpu_put_seg(f
, &env
->ldt
);
5834 cpu_put_seg(f
, &env
->tr
);
5835 cpu_put_seg(f
, &env
->gdt
);
5836 cpu_put_seg(f
, &env
->idt
);
5838 qemu_put_be32s(f
, &env
->sysenter_cs
);
5839 qemu_put_be32s(f
, &env
->sysenter_esp
);
5840 qemu_put_be32s(f
, &env
->sysenter_eip
);
5842 qemu_put_betls(f
, &env
->cr
[0]);
5843 qemu_put_betls(f
, &env
->cr
[2]);
5844 qemu_put_betls(f
, &env
->cr
[3]);
5845 qemu_put_betls(f
, &env
->cr
[4]);
5847 for(i
= 0; i
< 8; i
++)
5848 qemu_put_betls(f
, &env
->dr
[i
]);
5851 qemu_put_be32s(f
, &env
->a20_mask
);
5854 qemu_put_be32s(f
, &env
->mxcsr
);
5855 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5856 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5857 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5860 #ifdef TARGET_X86_64
5861 qemu_put_be64s(f
, &env
->efer
);
5862 qemu_put_be64s(f
, &env
->star
);
5863 qemu_put_be64s(f
, &env
->lstar
);
5864 qemu_put_be64s(f
, &env
->cstar
);
5865 qemu_put_be64s(f
, &env
->fmask
);
5866 qemu_put_be64s(f
, &env
->kernelgsbase
);
5868 qemu_put_be32s(f
, &env
->smbase
);
5871 #ifdef USE_X86LDOUBLE
5872 /* XXX: add that in a FPU generic layer */
5873 union x86_longdouble
{
5878 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
5879 #define EXPBIAS1 1023
5880 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
5881 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
5883 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
5887 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
5888 /* exponent + sign */
5889 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
5890 e
|= SIGND1(temp
) >> 16;
5895 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5897 CPUState
*env
= opaque
;
5900 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
5902 if (version_id
!= 3 && version_id
!= 4)
5904 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5905 qemu_get_betls(f
, &env
->regs
[i
]);
5906 qemu_get_betls(f
, &env
->eip
);
5907 qemu_get_betls(f
, &env
->eflags
);
5908 qemu_get_be32s(f
, &hflags
);
5910 qemu_get_be16s(f
, &fpuc
);
5911 qemu_get_be16s(f
, &fpus
);
5912 qemu_get_be16s(f
, &fptag
);
5913 qemu_get_be16s(f
, &fpregs_format
);
5915 /* NOTE: we cannot always restore the FPU state if the image come
5916 from a host with a different 'USE_X86LDOUBLE' define. We guess
5917 if we are in an MMX state to restore correctly in that case. */
5918 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
5919 for(i
= 0; i
< 8; i
++) {
5923 switch(fpregs_format
) {
5925 mant
= qemu_get_be64(f
);
5926 exp
= qemu_get_be16(f
);
5927 #ifdef USE_X86LDOUBLE
5928 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5930 /* difficult case */
5932 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5934 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5938 mant
= qemu_get_be64(f
);
5939 #ifdef USE_X86LDOUBLE
5941 union x86_longdouble
*p
;
5942 /* difficult case */
5943 p
= (void *)&env
->fpregs
[i
];
5948 fp64_to_fp80(p
, mant
);
5952 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5961 /* XXX: restore FPU round state */
5962 env
->fpstt
= (fpus
>> 11) & 7;
5963 env
->fpus
= fpus
& ~0x3800;
5965 for(i
= 0; i
< 8; i
++) {
5966 env
->fptags
[i
] = (fptag
>> i
) & 1;
5969 for(i
= 0; i
< 6; i
++)
5970 cpu_get_seg(f
, &env
->segs
[i
]);
5971 cpu_get_seg(f
, &env
->ldt
);
5972 cpu_get_seg(f
, &env
->tr
);
5973 cpu_get_seg(f
, &env
->gdt
);
5974 cpu_get_seg(f
, &env
->idt
);
5976 qemu_get_be32s(f
, &env
->sysenter_cs
);
5977 qemu_get_be32s(f
, &env
->sysenter_esp
);
5978 qemu_get_be32s(f
, &env
->sysenter_eip
);
5980 qemu_get_betls(f
, &env
->cr
[0]);
5981 qemu_get_betls(f
, &env
->cr
[2]);
5982 qemu_get_betls(f
, &env
->cr
[3]);
5983 qemu_get_betls(f
, &env
->cr
[4]);
5985 for(i
= 0; i
< 8; i
++)
5986 qemu_get_betls(f
, &env
->dr
[i
]);
5989 qemu_get_be32s(f
, &env
->a20_mask
);
5991 qemu_get_be32s(f
, &env
->mxcsr
);
5992 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5993 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5994 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5997 #ifdef TARGET_X86_64
5998 qemu_get_be64s(f
, &env
->efer
);
5999 qemu_get_be64s(f
, &env
->star
);
6000 qemu_get_be64s(f
, &env
->lstar
);
6001 qemu_get_be64s(f
, &env
->cstar
);
6002 qemu_get_be64s(f
, &env
->fmask
);
6003 qemu_get_be64s(f
, &env
->kernelgsbase
);
6005 if (version_id
>= 4)
6006 qemu_get_be32s(f
, &env
->smbase
);
6008 /* XXX: compute hflags from scratch, except for CPL and IIF */
6009 env
->hflags
= hflags
;
6014 #elif defined(TARGET_PPC)
6015 void cpu_save(QEMUFile
*f
, void *opaque
)
6019 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6024 #elif defined(TARGET_MIPS)
6025 void cpu_save(QEMUFile
*f
, void *opaque
)
6029 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6034 #elif defined(TARGET_SPARC)
6035 void cpu_save(QEMUFile
*f
, void *opaque
)
6037 CPUState
*env
= opaque
;
6041 for(i
= 0; i
< 8; i
++)
6042 qemu_put_betls(f
, &env
->gregs
[i
]);
6043 for(i
= 0; i
< NWINDOWS
* 16; i
++)
6044 qemu_put_betls(f
, &env
->regbase
[i
]);
6047 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
6053 qemu_put_be32(f
, u
.i
);
6056 qemu_put_betls(f
, &env
->pc
);
6057 qemu_put_betls(f
, &env
->npc
);
6058 qemu_put_betls(f
, &env
->y
);
6060 qemu_put_be32(f
, tmp
);
6061 qemu_put_betls(f
, &env
->fsr
);
6062 qemu_put_betls(f
, &env
->tbr
);
6063 #ifndef TARGET_SPARC64
6064 qemu_put_be32s(f
, &env
->wim
);
6066 for(i
= 0; i
< 16; i
++)
6067 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
6071 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6073 CPUState
*env
= opaque
;
6077 for(i
= 0; i
< 8; i
++)
6078 qemu_get_betls(f
, &env
->gregs
[i
]);
6079 for(i
= 0; i
< NWINDOWS
* 16; i
++)
6080 qemu_get_betls(f
, &env
->regbase
[i
]);
6083 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
6088 u
.i
= qemu_get_be32(f
);
6092 qemu_get_betls(f
, &env
->pc
);
6093 qemu_get_betls(f
, &env
->npc
);
6094 qemu_get_betls(f
, &env
->y
);
6095 tmp
= qemu_get_be32(f
);
6096 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
6097 correctly updated */
6099 qemu_get_betls(f
, &env
->fsr
);
6100 qemu_get_betls(f
, &env
->tbr
);
6101 #ifndef TARGET_SPARC64
6102 qemu_get_be32s(f
, &env
->wim
);
6104 for(i
= 0; i
< 16; i
++)
6105 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
6111 #elif defined(TARGET_ARM)
6113 void cpu_save(QEMUFile
*f
, void *opaque
)
6116 CPUARMState
*env
= (CPUARMState
*)opaque
;
6118 for (i
= 0; i
< 16; i
++) {
6119 qemu_put_be32(f
, env
->regs
[i
]);
6121 qemu_put_be32(f
, cpsr_read(env
));
6122 qemu_put_be32(f
, env
->spsr
);
6123 for (i
= 0; i
< 6; i
++) {
6124 qemu_put_be32(f
, env
->banked_spsr
[i
]);
6125 qemu_put_be32(f
, env
->banked_r13
[i
]);
6126 qemu_put_be32(f
, env
->banked_r14
[i
]);
6128 for (i
= 0; i
< 5; i
++) {
6129 qemu_put_be32(f
, env
->usr_regs
[i
]);
6130 qemu_put_be32(f
, env
->fiq_regs
[i
]);
6132 qemu_put_be32(f
, env
->cp15
.c0_cpuid
);
6133 qemu_put_be32(f
, env
->cp15
.c0_cachetype
);
6134 qemu_put_be32(f
, env
->cp15
.c1_sys
);
6135 qemu_put_be32(f
, env
->cp15
.c1_coproc
);
6136 qemu_put_be32(f
, env
->cp15
.c1_xscaleauxcr
);
6137 qemu_put_be32(f
, env
->cp15
.c2_base
);
6138 qemu_put_be32(f
, env
->cp15
.c2_data
);
6139 qemu_put_be32(f
, env
->cp15
.c2_insn
);
6140 qemu_put_be32(f
, env
->cp15
.c3
);
6141 qemu_put_be32(f
, env
->cp15
.c5_insn
);
6142 qemu_put_be32(f
, env
->cp15
.c5_data
);
6143 for (i
= 0; i
< 8; i
++) {
6144 qemu_put_be32(f
, env
->cp15
.c6_region
[i
]);
6146 qemu_put_be32(f
, env
->cp15
.c6_insn
);
6147 qemu_put_be32(f
, env
->cp15
.c6_data
);
6148 qemu_put_be32(f
, env
->cp15
.c9_insn
);
6149 qemu_put_be32(f
, env
->cp15
.c9_data
);
6150 qemu_put_be32(f
, env
->cp15
.c13_fcse
);
6151 qemu_put_be32(f
, env
->cp15
.c13_context
);
6152 qemu_put_be32(f
, env
->cp15
.c15_cpar
);
6154 qemu_put_be32(f
, env
->features
);
6156 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
6157 for (i
= 0; i
< 16; i
++) {
6159 u
.d
= env
->vfp
.regs
[i
];
6160 qemu_put_be32(f
, u
.l
.upper
);
6161 qemu_put_be32(f
, u
.l
.lower
);
6163 for (i
= 0; i
< 16; i
++) {
6164 qemu_put_be32(f
, env
->vfp
.xregs
[i
]);
6167 /* TODO: Should use proper FPSCR access functions. */
6168 qemu_put_be32(f
, env
->vfp
.vec_len
);
6169 qemu_put_be32(f
, env
->vfp
.vec_stride
);
6172 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
6173 for (i
= 0; i
< 16; i
++) {
6174 qemu_put_be64(f
, env
->iwmmxt
.regs
[i
]);
6176 for (i
= 0; i
< 16; i
++) {
6177 qemu_put_be32(f
, env
->iwmmxt
.cregs
[i
]);
6182 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6184 CPUARMState
*env
= (CPUARMState
*)opaque
;
6187 if (version_id
!= 0)
6190 for (i
= 0; i
< 16; i
++) {
6191 env
->regs
[i
] = qemu_get_be32(f
);
6193 cpsr_write(env
, qemu_get_be32(f
), 0xffffffff);
6194 env
->spsr
= qemu_get_be32(f
);
6195 for (i
= 0; i
< 6; i
++) {
6196 env
->banked_spsr
[i
] = qemu_get_be32(f
);
6197 env
->banked_r13
[i
] = qemu_get_be32(f
);
6198 env
->banked_r14
[i
] = qemu_get_be32(f
);
6200 for (i
= 0; i
< 5; i
++) {
6201 env
->usr_regs
[i
] = qemu_get_be32(f
);
6202 env
->fiq_regs
[i
] = qemu_get_be32(f
);
6204 env
->cp15
.c0_cpuid
= qemu_get_be32(f
);
6205 env
->cp15
.c0_cachetype
= qemu_get_be32(f
);
6206 env
->cp15
.c1_sys
= qemu_get_be32(f
);
6207 env
->cp15
.c1_coproc
= qemu_get_be32(f
);
6208 env
->cp15
.c1_xscaleauxcr
= qemu_get_be32(f
);
6209 env
->cp15
.c2_base
= qemu_get_be32(f
);
6210 env
->cp15
.c2_data
= qemu_get_be32(f
);
6211 env
->cp15
.c2_insn
= qemu_get_be32(f
);
6212 env
->cp15
.c3
= qemu_get_be32(f
);
6213 env
->cp15
.c5_insn
= qemu_get_be32(f
);
6214 env
->cp15
.c5_data
= qemu_get_be32(f
);
6215 for (i
= 0; i
< 8; i
++) {
6216 env
->cp15
.c6_region
[i
] = qemu_get_be32(f
);
6218 env
->cp15
.c6_insn
= qemu_get_be32(f
);
6219 env
->cp15
.c6_data
= qemu_get_be32(f
);
6220 env
->cp15
.c9_insn
= qemu_get_be32(f
);
6221 env
->cp15
.c9_data
= qemu_get_be32(f
);
6222 env
->cp15
.c13_fcse
= qemu_get_be32(f
);
6223 env
->cp15
.c13_context
= qemu_get_be32(f
);
6224 env
->cp15
.c15_cpar
= qemu_get_be32(f
);
6226 env
->features
= qemu_get_be32(f
);
6228 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
6229 for (i
= 0; i
< 16; i
++) {
6231 u
.l
.upper
= qemu_get_be32(f
);
6232 u
.l
.lower
= qemu_get_be32(f
);
6233 env
->vfp
.regs
[i
] = u
.d
;
6235 for (i
= 0; i
< 16; i
++) {
6236 env
->vfp
.xregs
[i
] = qemu_get_be32(f
);
6239 /* TODO: Should use proper FPSCR access functions. */
6240 env
->vfp
.vec_len
= qemu_get_be32(f
);
6241 env
->vfp
.vec_stride
= qemu_get_be32(f
);
6244 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
6245 for (i
= 0; i
< 16; i
++) {
6246 env
->iwmmxt
.regs
[i
] = qemu_get_be64(f
);
6248 for (i
= 0; i
< 16; i
++) {
6249 env
->iwmmxt
.cregs
[i
] = qemu_get_be32(f
);
6258 #warning No CPU save/restore functions
6262 /***********************************************************/
6263 /* ram save/restore */
6265 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
6269 v
= qemu_get_byte(f
);
6272 if (qemu_get_buffer(f
, buf
, len
) != len
)
6276 v
= qemu_get_byte(f
);
6277 memset(buf
, v
, len
);
6285 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
6289 if (qemu_get_be32(f
) != phys_ram_size
)
6291 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
6292 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
6299 #define BDRV_HASH_BLOCK_SIZE 1024
6300 #define IOBUF_SIZE 4096
6301 #define RAM_CBLOCK_MAGIC 0xfabe
6303 typedef struct RamCompressState
{
6306 uint8_t buf
[IOBUF_SIZE
];
6309 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
6312 memset(s
, 0, sizeof(*s
));
6314 ret
= deflateInit2(&s
->zstream
, 1,
6316 9, Z_DEFAULT_STRATEGY
);
6319 s
->zstream
.avail_out
= IOBUF_SIZE
;
6320 s
->zstream
.next_out
= s
->buf
;
6324 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
6326 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
6327 qemu_put_be16(s
->f
, len
);
6328 qemu_put_buffer(s
->f
, buf
, len
);
6331 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
6335 s
->zstream
.avail_in
= len
;
6336 s
->zstream
.next_in
= (uint8_t *)buf
;
6337 while (s
->zstream
.avail_in
> 0) {
6338 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
6341 if (s
->zstream
.avail_out
== 0) {
6342 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
6343 s
->zstream
.avail_out
= IOBUF_SIZE
;
6344 s
->zstream
.next_out
= s
->buf
;
6350 static void ram_compress_close(RamCompressState
*s
)
6354 /* compress last bytes */
6356 ret
= deflate(&s
->zstream
, Z_FINISH
);
6357 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
6358 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
6360 ram_put_cblock(s
, s
->buf
, len
);
6362 s
->zstream
.avail_out
= IOBUF_SIZE
;
6363 s
->zstream
.next_out
= s
->buf
;
6364 if (ret
== Z_STREAM_END
)
6371 deflateEnd(&s
->zstream
);
6374 typedef struct RamDecompressState
{
6377 uint8_t buf
[IOBUF_SIZE
];
6378 } RamDecompressState
;
6380 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
6383 memset(s
, 0, sizeof(*s
));
6385 ret
= inflateInit(&s
->zstream
);
6391 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
6395 s
->zstream
.avail_out
= len
;
6396 s
->zstream
.next_out
= buf
;
6397 while (s
->zstream
.avail_out
> 0) {
6398 if (s
->zstream
.avail_in
== 0) {
6399 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
6401 clen
= qemu_get_be16(s
->f
);
6402 if (clen
> IOBUF_SIZE
)
6404 qemu_get_buffer(s
->f
, s
->buf
, clen
);
6405 s
->zstream
.avail_in
= clen
;
6406 s
->zstream
.next_in
= s
->buf
;
6408 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
6409 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
6416 static void ram_decompress_close(RamDecompressState
*s
)
6418 inflateEnd(&s
->zstream
);
6421 static void ram_save(QEMUFile
*f
, void *opaque
)
6424 RamCompressState s1
, *s
= &s1
;
6427 qemu_put_be32(f
, phys_ram_size
);
6428 if (ram_compress_open(s
, f
) < 0)
6430 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6432 if (tight_savevm_enabled
) {
6436 /* find if the memory block is available on a virtual
6439 for(j
= 0; j
< MAX_DISKS
; j
++) {
6441 sector_num
= bdrv_hash_find(bs_table
[j
],
6442 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6443 if (sector_num
>= 0)
6448 goto normal_compress
;
6451 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
6452 ram_compress_buf(s
, buf
, 10);
6458 ram_compress_buf(s
, buf
, 1);
6459 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6462 ram_compress_close(s
);
6465 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
6467 RamDecompressState s1
, *s
= &s1
;
6471 if (version_id
== 1)
6472 return ram_load_v1(f
, opaque
);
6473 if (version_id
!= 2)
6475 if (qemu_get_be32(f
) != phys_ram_size
)
6477 if (ram_decompress_open(s
, f
) < 0)
6479 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6480 if (ram_decompress_buf(s
, buf
, 1) < 0) {
6481 fprintf(stderr
, "Error while reading ram block header\n");
6485 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
6486 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
6495 ram_decompress_buf(s
, buf
+ 1, 9);
6497 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
6498 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
6499 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
6502 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
6503 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
6504 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
6505 bs_index
, sector_num
);
6512 printf("Error block header\n");
6516 ram_decompress_close(s
);
6520 /***********************************************************/
6521 /* bottom halves (can be seen as timers which expire ASAP) */
6530 static QEMUBH
*first_bh
= NULL
;
6532 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
6535 bh
= qemu_mallocz(sizeof(QEMUBH
));
6539 bh
->opaque
= opaque
;
6543 int qemu_bh_poll(void)
6562 void qemu_bh_schedule(QEMUBH
*bh
)
6564 CPUState
*env
= cpu_single_env
;
6568 bh
->next
= first_bh
;
6571 /* stop the currently executing CPU to execute the BH ASAP */
6573 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
6577 void qemu_bh_cancel(QEMUBH
*bh
)
6580 if (bh
->scheduled
) {
6583 pbh
= &(*pbh
)->next
;
6589 void qemu_bh_delete(QEMUBH
*bh
)
6595 /***********************************************************/
6596 /* machine registration */
6598 QEMUMachine
*first_machine
= NULL
;
6600 int qemu_register_machine(QEMUMachine
*m
)
6603 pm
= &first_machine
;
6611 QEMUMachine
*find_machine(const char *name
)
6615 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6616 if (!strcmp(m
->name
, name
))
6622 /***********************************************************/
6623 /* main execution loop */
6625 void gui_update(void *opaque
)
6627 DisplayState
*ds
= opaque
;
6628 ds
->dpy_refresh(ds
);
6629 qemu_mod_timer(ds
->gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
6632 struct vm_change_state_entry
{
6633 VMChangeStateHandler
*cb
;
6635 LIST_ENTRY (vm_change_state_entry
) entries
;
6638 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
6640 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
6643 VMChangeStateEntry
*e
;
6645 e
= qemu_mallocz(sizeof (*e
));
6651 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
6655 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
6657 LIST_REMOVE (e
, entries
);
6661 static void vm_state_notify(int running
)
6663 VMChangeStateEntry
*e
;
6665 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
6666 e
->cb(e
->opaque
, running
);
6670 /* XXX: support several handlers */
6671 static VMStopHandler
*vm_stop_cb
;
6672 static void *vm_stop_opaque
;
6674 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6677 vm_stop_opaque
= opaque
;
6681 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6692 qemu_rearm_alarm_timer(alarm_timer
);
6696 void vm_stop(int reason
)
6699 cpu_disable_ticks();
6703 vm_stop_cb(vm_stop_opaque
, reason
);
6710 /* reset/shutdown handler */
6712 typedef struct QEMUResetEntry
{
6713 QEMUResetHandler
*func
;
6715 struct QEMUResetEntry
*next
;
6718 static QEMUResetEntry
*first_reset_entry
;
6719 static int reset_requested
;
6720 static int shutdown_requested
;
6721 static int powerdown_requested
;
6723 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
6725 QEMUResetEntry
**pre
, *re
;
6727 pre
= &first_reset_entry
;
6728 while (*pre
!= NULL
)
6729 pre
= &(*pre
)->next
;
6730 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
6732 re
->opaque
= opaque
;
6737 static void qemu_system_reset(void)
6741 /* reset all devices */
6742 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
6743 re
->func(re
->opaque
);
6747 void qemu_system_reset_request(void)
6750 shutdown_requested
= 1;
6752 reset_requested
= 1;
6755 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6758 void qemu_system_shutdown_request(void)
6760 shutdown_requested
= 1;
6762 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6765 void qemu_system_powerdown_request(void)
6767 powerdown_requested
= 1;
6769 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6772 void main_loop_wait(int timeout
)
6774 IOHandlerRecord
*ioh
;
6775 fd_set rfds
, wfds
, xfds
;
6784 /* XXX: need to suppress polling by better using win32 events */
6786 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
6787 ret
|= pe
->func(pe
->opaque
);
6792 WaitObjects
*w
= &wait_objects
;
6794 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
6795 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
6796 if (w
->func
[ret
- WAIT_OBJECT_0
])
6797 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
6799 /* Check for additional signaled events */
6800 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
6802 /* Check if event is signaled */
6803 ret2
= WaitForSingleObject(w
->events
[i
], 0);
6804 if(ret2
== WAIT_OBJECT_0
) {
6806 w
->func
[i
](w
->opaque
[i
]);
6807 } else if (ret2
== WAIT_TIMEOUT
) {
6809 err
= GetLastError();
6810 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
6813 } else if (ret
== WAIT_TIMEOUT
) {
6815 err
= GetLastError();
6816 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
6820 /* poll any events */
6821 /* XXX: separate device handlers from system ones */
6826 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6830 (!ioh
->fd_read_poll
||
6831 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
6832 FD_SET(ioh
->fd
, &rfds
);
6836 if (ioh
->fd_write
) {
6837 FD_SET(ioh
->fd
, &wfds
);
6847 tv
.tv_usec
= timeout
* 1000;
6849 #if defined(CONFIG_SLIRP)
6851 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
6854 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
6856 IOHandlerRecord
**pioh
;
6858 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6859 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
6860 ioh
->fd_read(ioh
->opaque
);
6862 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
6863 ioh
->fd_write(ioh
->opaque
);
6867 /* remove deleted IO handlers */
6868 pioh
= &first_io_handler
;
6878 #if defined(CONFIG_SLIRP)
6885 slirp_select_poll(&rfds
, &wfds
, &xfds
);
6891 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
6892 qemu_get_clock(vm_clock
));
6893 /* run dma transfers, if any */
6897 /* real time timers */
6898 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
6899 qemu_get_clock(rt_clock
));
6901 /* Check bottom-halves last in case any of the earlier events triggered
6907 static CPUState
*cur_cpu
;
6912 #ifdef CONFIG_PROFILER
6917 cur_cpu
= first_cpu
;
6924 env
= env
->next_cpu
;
6927 #ifdef CONFIG_PROFILER
6928 ti
= profile_getclock();
6930 ret
= cpu_exec(env
);
6931 #ifdef CONFIG_PROFILER
6932 qemu_time
+= profile_getclock() - ti
;
6934 if (ret
== EXCP_HLT
) {
6935 /* Give the next CPU a chance to run. */
6939 if (ret
!= EXCP_HALTED
)
6941 /* all CPUs are halted ? */
6947 if (shutdown_requested
) {
6948 ret
= EXCP_INTERRUPT
;
6951 if (reset_requested
) {
6952 reset_requested
= 0;
6953 qemu_system_reset();
6954 ret
= EXCP_INTERRUPT
;
6956 if (powerdown_requested
) {
6957 powerdown_requested
= 0;
6958 qemu_system_powerdown();
6959 ret
= EXCP_INTERRUPT
;
6961 if (ret
== EXCP_DEBUG
) {
6962 vm_stop(EXCP_DEBUG
);
6964 /* If all cpus are halted then wait until the next IRQ */
6965 /* XXX: use timeout computed from timers */
6966 if (ret
== EXCP_HALTED
)
6973 #ifdef CONFIG_PROFILER
6974 ti
= profile_getclock();
6976 main_loop_wait(timeout
);
6977 #ifdef CONFIG_PROFILER
6978 dev_time
+= profile_getclock() - ti
;
6981 cpu_disable_ticks();
6985 static void help(int exitcode
)
6987 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2007 Fabrice Bellard\n"
6988 "usage: %s [options] [disk_image]\n"
6990 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
6992 "Standard options:\n"
6993 "-M machine select emulated machine (-M ? for list)\n"
6994 "-cpu cpu select CPU (-cpu ? for list)\n"
6995 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
6996 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
6997 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
6998 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
6999 "-mtdblock file use 'file' as on-board Flash memory image\n"
7000 "-sd file use 'file' as SecureDigital card image\n"
7001 "-pflash file use 'file' as a parallel flash image\n"
7002 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
7003 "-snapshot write to temporary files instead of disk image files\n"
7005 "-no-frame open SDL window without a frame and window decorations\n"
7006 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
7007 "-no-quit disable SDL window close capability\n"
7010 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
7012 "-m megs set virtual RAM size to megs MB [default=%d]\n"
7013 "-smp n set the number of CPUs to 'n' [default=1]\n"
7014 "-nographic disable graphical output and redirect serial I/Os to console\n"
7015 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
7017 "-k language use keyboard layout (for example \"fr\" for French)\n"
7020 "-audio-help print list of audio drivers and their options\n"
7021 "-soundhw c1,... enable audio support\n"
7022 " and only specified sound cards (comma separated list)\n"
7023 " use -soundhw ? to get the list of supported cards\n"
7024 " use -soundhw all to enable all of them\n"
7026 "-localtime set the real time clock to local time [default=utc]\n"
7027 "-full-screen start in full screen\n"
7029 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
7031 "-usb enable the USB driver (will be the default soon)\n"
7032 "-usbdevice name add the host or guest USB device 'name'\n"
7033 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7034 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
7036 "-name string set the name of the guest\n"
7038 "Network options:\n"
7039 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
7040 " create a new Network Interface Card and connect it to VLAN 'n'\n"
7042 "-net user[,vlan=n][,hostname=host]\n"
7043 " connect the user mode network stack to VLAN 'n' and send\n"
7044 " hostname 'host' to DHCP clients\n"
7047 "-net tap[,vlan=n],ifname=name\n"
7048 " connect the host TAP network interface to VLAN 'n'\n"
7050 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
7051 " connect the host TAP network interface to VLAN 'n' and use the\n"
7052 " network scripts 'file' (default=%s)\n"
7053 " and 'dfile' (default=%s);\n"
7054 " use '[down]script=no' to disable script execution;\n"
7055 " use 'fd=h' to connect to an already opened TAP interface\n"
7057 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
7058 " connect the vlan 'n' to another VLAN using a socket connection\n"
7059 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
7060 " connect the vlan 'n' to multicast maddr and port\n"
7061 "-net none use it alone to have zero network devices; if no -net option\n"
7062 " is provided, the default is '-net nic -net user'\n"
7065 "-tftp dir allow tftp access to files in dir [-net user]\n"
7066 "-bootp file advertise file in BOOTP replies\n"
7068 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
7070 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
7071 " redirect TCP or UDP connections from host to guest [-net user]\n"
7074 "Linux boot specific:\n"
7075 "-kernel bzImage use 'bzImage' as kernel image\n"
7076 "-append cmdline use 'cmdline' as kernel command line\n"
7077 "-initrd file use 'file' as initial ram disk\n"
7079 "Debug/Expert options:\n"
7080 "-monitor dev redirect the monitor to char device 'dev'\n"
7081 "-serial dev redirect the serial port to char device 'dev'\n"
7082 "-parallel dev redirect the parallel port to char device 'dev'\n"
7083 "-pidfile file Write PID to 'file'\n"
7084 "-S freeze CPU at startup (use 'c' to start execution)\n"
7085 "-s wait gdb connection to port\n"
7086 "-p port set gdb connection port [default=%s]\n"
7087 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
7088 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
7089 " translation (t=none or lba) (usually qemu can guess them)\n"
7090 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
7092 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
7093 "-no-kqemu disable KQEMU kernel module usage\n"
7095 #ifdef USE_CODE_COPY
7096 "-no-code-copy disable code copy acceleration\n"
7099 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
7100 " (default is CL-GD5446 PCI VGA)\n"
7101 "-no-acpi disable ACPI\n"
7103 "-no-reboot exit instead of rebooting\n"
7104 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
7105 "-vnc display start a VNC server on display\n"
7107 "-daemonize daemonize QEMU after initializing\n"
7109 "-option-rom rom load a file, rom, into the option ROM space\n"
7111 "-prom-env variable=value set OpenBIOS nvram variables\n"
7113 "-clock force the use of the given methods for timer alarm.\n"
7114 " To see what timers are available use -clock help\n"
7116 "During emulation, the following keys are useful:\n"
7117 "ctrl-alt-f toggle full screen\n"
7118 "ctrl-alt-n switch to virtual console 'n'\n"
7119 "ctrl-alt toggle mouse and keyboard grab\n"
7121 "When using -nographic, press 'ctrl-a h' to get some help.\n"
7126 DEFAULT_NETWORK_SCRIPT
,
7127 DEFAULT_NETWORK_DOWN_SCRIPT
,
7129 DEFAULT_GDBSTUB_PORT
,
7134 #define HAS_ARG 0x0001
7148 QEMU_OPTION_mtdblock
,
7152 QEMU_OPTION_snapshot
,
7154 QEMU_OPTION_no_fd_bootchk
,
7157 QEMU_OPTION_nographic
,
7158 QEMU_OPTION_portrait
,
7160 QEMU_OPTION_audio_help
,
7161 QEMU_OPTION_soundhw
,
7181 QEMU_OPTION_no_code_copy
,
7183 QEMU_OPTION_localtime
,
7184 QEMU_OPTION_cirrusvga
,
7187 QEMU_OPTION_std_vga
,
7189 QEMU_OPTION_monitor
,
7191 QEMU_OPTION_parallel
,
7193 QEMU_OPTION_full_screen
,
7194 QEMU_OPTION_no_frame
,
7195 QEMU_OPTION_alt_grab
,
7196 QEMU_OPTION_no_quit
,
7197 QEMU_OPTION_pidfile
,
7198 QEMU_OPTION_no_kqemu
,
7199 QEMU_OPTION_kernel_kqemu
,
7200 QEMU_OPTION_win2k_hack
,
7202 QEMU_OPTION_usbdevice
,
7205 QEMU_OPTION_no_acpi
,
7206 QEMU_OPTION_no_reboot
,
7207 QEMU_OPTION_show_cursor
,
7208 QEMU_OPTION_daemonize
,
7209 QEMU_OPTION_option_rom
,
7210 QEMU_OPTION_semihosting
,
7212 QEMU_OPTION_prom_env
,
7213 QEMU_OPTION_old_param
,
7217 typedef struct QEMUOption
{
7223 const QEMUOption qemu_options
[] = {
7224 { "h", 0, QEMU_OPTION_h
},
7225 { "help", 0, QEMU_OPTION_h
},
7227 { "M", HAS_ARG
, QEMU_OPTION_M
},
7228 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
7229 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
7230 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
7231 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
7232 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
7233 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
7234 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
7235 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
7236 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
7237 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
7238 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
7239 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
7240 { "snapshot", 0, QEMU_OPTION_snapshot
},
7242 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
7244 { "m", HAS_ARG
, QEMU_OPTION_m
},
7245 { "nographic", 0, QEMU_OPTION_nographic
},
7246 { "portrait", 0, QEMU_OPTION_portrait
},
7247 { "k", HAS_ARG
, QEMU_OPTION_k
},
7249 { "audio-help", 0, QEMU_OPTION_audio_help
},
7250 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
7253 { "net", HAS_ARG
, QEMU_OPTION_net
},
7255 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
7256 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
7258 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
7260 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
7263 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
7264 { "append", HAS_ARG
, QEMU_OPTION_append
},
7265 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
7267 { "S", 0, QEMU_OPTION_S
},
7268 { "s", 0, QEMU_OPTION_s
},
7269 { "p", HAS_ARG
, QEMU_OPTION_p
},
7270 { "d", HAS_ARG
, QEMU_OPTION_d
},
7271 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
7272 { "L", HAS_ARG
, QEMU_OPTION_L
},
7273 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
7274 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
7276 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
7277 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
7279 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7280 { "g", 1, QEMU_OPTION_g
},
7282 { "localtime", 0, QEMU_OPTION_localtime
},
7283 { "std-vga", 0, QEMU_OPTION_std_vga
},
7284 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
7285 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
7286 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
7287 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
7288 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
7289 { "full-screen", 0, QEMU_OPTION_full_screen
},
7291 { "no-frame", 0, QEMU_OPTION_no_frame
},
7292 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
7293 { "no-quit", 0, QEMU_OPTION_no_quit
},
7295 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
7296 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
7297 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
7298 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
7299 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
7301 /* temporary options */
7302 { "usb", 0, QEMU_OPTION_usb
},
7303 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
7304 { "vmwarevga", 0, QEMU_OPTION_vmsvga
},
7305 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
7306 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
7307 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
7308 { "daemonize", 0, QEMU_OPTION_daemonize
},
7309 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
7310 #if defined(TARGET_ARM) || defined(TARGET_M68K)
7311 { "semihosting", 0, QEMU_OPTION_semihosting
},
7313 { "name", HAS_ARG
, QEMU_OPTION_name
},
7314 #if defined(TARGET_SPARC)
7315 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
7317 #if defined(TARGET_ARM)
7318 { "old-param", 0, QEMU_OPTION_old_param
},
7320 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
7324 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
7326 /* this stack is only used during signal handling */
7327 #define SIGNAL_STACK_SIZE 32768
7329 static uint8_t *signal_stack
;
7333 /* password input */
7335 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
7340 if (!bdrv_is_encrypted(bs
))
7343 term_printf("%s is encrypted.\n", name
);
7344 for(i
= 0; i
< 3; i
++) {
7345 monitor_readline("Password: ", 1, password
, sizeof(password
));
7346 if (bdrv_set_key(bs
, password
) == 0)
7348 term_printf("invalid password\n");
7353 static BlockDriverState
*get_bdrv(int index
)
7355 BlockDriverState
*bs
;
7358 bs
= bs_table
[index
];
7359 } else if (index
< 6) {
7360 bs
= fd_table
[index
- 4];
7367 static void read_passwords(void)
7369 BlockDriverState
*bs
;
7372 for(i
= 0; i
< 6; i
++) {
7375 qemu_key_check(bs
, bdrv_get_device_name(bs
));
7379 /* XXX: currently we cannot use simultaneously different CPUs */
7380 void register_machines(void)
7382 #if defined(TARGET_I386)
7383 qemu_register_machine(&pc_machine
);
7384 qemu_register_machine(&isapc_machine
);
7385 #elif defined(TARGET_PPC)
7386 qemu_register_machine(&heathrow_machine
);
7387 qemu_register_machine(&core99_machine
);
7388 qemu_register_machine(&prep_machine
);
7389 qemu_register_machine(&ref405ep_machine
);
7390 qemu_register_machine(&taihu_machine
);
7391 #elif defined(TARGET_MIPS)
7392 qemu_register_machine(&mips_machine
);
7393 qemu_register_machine(&mips_malta_machine
);
7394 qemu_register_machine(&mips_pica61_machine
);
7395 qemu_register_machine(&mips_mipssim_machine
);
7396 #elif defined(TARGET_SPARC)
7397 #ifdef TARGET_SPARC64
7398 qemu_register_machine(&sun4u_machine
);
7400 qemu_register_machine(&ss5_machine
);
7401 qemu_register_machine(&ss10_machine
);
7403 #elif defined(TARGET_ARM)
7404 qemu_register_machine(&integratorcp_machine
);
7405 qemu_register_machine(&versatilepb_machine
);
7406 qemu_register_machine(&versatileab_machine
);
7407 qemu_register_machine(&realview_machine
);
7408 qemu_register_machine(&akitapda_machine
);
7409 qemu_register_machine(&spitzpda_machine
);
7410 qemu_register_machine(&borzoipda_machine
);
7411 qemu_register_machine(&terrierpda_machine
);
7412 qemu_register_machine(&palmte_machine
);
7413 #elif defined(TARGET_SH4)
7414 qemu_register_machine(&shix_machine
);
7415 qemu_register_machine(&r2d_machine
);
7416 #elif defined(TARGET_ALPHA)
7418 #elif defined(TARGET_M68K)
7419 qemu_register_machine(&mcf5208evb_machine
);
7420 qemu_register_machine(&an5206_machine
);
7421 #elif defined(TARGET_CRIS)
7422 qemu_register_machine(&bareetraxfs_machine
);
7424 #error unsupported CPU
7429 struct soundhw soundhw
[] = {
7430 #ifdef HAS_AUDIO_CHOICE
7437 { .init_isa
= pcspk_audio_init
}
7442 "Creative Sound Blaster 16",
7445 { .init_isa
= SB16_init
}
7452 "Yamaha YMF262 (OPL3)",
7454 "Yamaha YM3812 (OPL2)",
7458 { .init_isa
= Adlib_init
}
7465 "Gravis Ultrasound GF1",
7468 { .init_isa
= GUS_init
}
7474 "ENSONIQ AudioPCI ES1370",
7477 { .init_pci
= es1370_init
}
7481 { NULL
, NULL
, 0, 0, { NULL
} }
7484 static void select_soundhw (const char *optarg
)
7488 if (*optarg
== '?') {
7491 printf ("Valid sound card names (comma separated):\n");
7492 for (c
= soundhw
; c
->name
; ++c
) {
7493 printf ("%-11s %s\n", c
->name
, c
->descr
);
7495 printf ("\n-soundhw all will enable all of the above\n");
7496 exit (*optarg
!= '?');
7504 if (!strcmp (optarg
, "all")) {
7505 for (c
= soundhw
; c
->name
; ++c
) {
7513 e
= strchr (p
, ',');
7514 l
= !e
? strlen (p
) : (size_t) (e
- p
);
7516 for (c
= soundhw
; c
->name
; ++c
) {
7517 if (!strncmp (c
->name
, p
, l
)) {
7526 "Unknown sound card name (too big to show)\n");
7529 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
7534 p
+= l
+ (e
!= NULL
);
7538 goto show_valid_cards
;
7544 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
7546 exit(STATUS_CONTROL_C_EXIT
);
7551 #define MAX_NET_CLIENTS 32
7553 int main(int argc
, char **argv
)
7555 #ifdef CONFIG_GDBSTUB
7557 const char *gdbstub_port
;
7559 int i
, cdrom_index
, pflash_index
;
7560 int snapshot
, linux_boot
;
7561 const char *initrd_filename
;
7562 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
7563 const char *pflash_filename
[MAX_PFLASH
];
7564 const char *sd_filename
;
7565 const char *mtd_filename
;
7566 const char *kernel_filename
, *kernel_cmdline
;
7567 DisplayState
*ds
= &display_state
;
7568 int cyls
, heads
, secs
, translation
;
7569 char net_clients
[MAX_NET_CLIENTS
][256];
7572 const char *r
, *optarg
;
7573 CharDriverState
*monitor_hd
;
7574 char monitor_device
[128];
7575 char serial_devices
[MAX_SERIAL_PORTS
][128];
7576 int serial_device_index
;
7577 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
7578 int parallel_device_index
;
7579 const char *loadvm
= NULL
;
7580 QEMUMachine
*machine
;
7581 const char *cpu_model
;
7582 char usb_devices
[MAX_USB_CMDLINE
][128];
7583 int usb_devices_index
;
7585 const char *pid_file
= NULL
;
7588 LIST_INIT (&vm_change_state_head
);
7591 struct sigaction act
;
7592 sigfillset(&act
.sa_mask
);
7594 act
.sa_handler
= SIG_IGN
;
7595 sigaction(SIGPIPE
, &act
, NULL
);
7598 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
7599 /* Note: cpu_interrupt() is currently not SMP safe, so we force
7600 QEMU to run on a single CPU */
7605 h
= GetCurrentProcess();
7606 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
7607 for(i
= 0; i
< 32; i
++) {
7608 if (mask
& (1 << i
))
7613 SetProcessAffinityMask(h
, mask
);
7619 register_machines();
7620 machine
= first_machine
;
7622 initrd_filename
= NULL
;
7623 for(i
= 0; i
< MAX_FD
; i
++)
7624 fd_filename
[i
] = NULL
;
7625 for(i
= 0; i
< MAX_DISKS
; i
++)
7626 hd_filename
[i
] = NULL
;
7627 for(i
= 0; i
< MAX_PFLASH
; i
++)
7628 pflash_filename
[i
] = NULL
;
7631 mtd_filename
= NULL
;
7632 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
7633 vga_ram_size
= VGA_RAM_SIZE
;
7634 #ifdef CONFIG_GDBSTUB
7636 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
7640 kernel_filename
= NULL
;
7641 kernel_cmdline
= "";
7647 cyls
= heads
= secs
= 0;
7648 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7649 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
7651 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
7652 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
7653 serial_devices
[i
][0] = '\0';
7654 serial_device_index
= 0;
7656 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
7657 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
7658 parallel_devices
[i
][0] = '\0';
7659 parallel_device_index
= 0;
7661 usb_devices_index
= 0;
7666 /* default mac address of the first network interface */
7674 hd_filename
[0] = argv
[optind
++];
7676 const QEMUOption
*popt
;
7679 /* Treat --foo the same as -foo. */
7682 popt
= qemu_options
;
7685 fprintf(stderr
, "%s: invalid option -- '%s'\n",
7689 if (!strcmp(popt
->name
, r
+ 1))
7693 if (popt
->flags
& HAS_ARG
) {
7694 if (optind
>= argc
) {
7695 fprintf(stderr
, "%s: option '%s' requires an argument\n",
7699 optarg
= argv
[optind
++];
7704 switch(popt
->index
) {
7706 machine
= find_machine(optarg
);
7709 printf("Supported machines are:\n");
7710 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
7711 printf("%-10s %s%s\n",
7713 m
== first_machine
? " (default)" : "");
7715 exit(*optarg
!= '?');
7718 case QEMU_OPTION_cpu
:
7719 /* hw initialization will check this */
7720 if (*optarg
== '?') {
7721 /* XXX: implement xxx_cpu_list for targets that still miss it */
7722 #if defined(cpu_list)
7723 cpu_list(stdout
, &fprintf
);
7730 case QEMU_OPTION_initrd
:
7731 initrd_filename
= optarg
;
7733 case QEMU_OPTION_hda
:
7734 case QEMU_OPTION_hdb
:
7735 case QEMU_OPTION_hdc
:
7736 case QEMU_OPTION_hdd
:
7739 hd_index
= popt
->index
- QEMU_OPTION_hda
;
7740 hd_filename
[hd_index
] = optarg
;
7741 if (hd_index
== cdrom_index
)
7745 case QEMU_OPTION_mtdblock
:
7746 mtd_filename
= optarg
;
7748 case QEMU_OPTION_sd
:
7749 sd_filename
= optarg
;
7751 case QEMU_OPTION_pflash
:
7752 if (pflash_index
>= MAX_PFLASH
) {
7753 fprintf(stderr
, "qemu: too many parallel flash images\n");
7756 pflash_filename
[pflash_index
++] = optarg
;
7758 case QEMU_OPTION_snapshot
:
7761 case QEMU_OPTION_hdachs
:
7765 cyls
= strtol(p
, (char **)&p
, 0);
7766 if (cyls
< 1 || cyls
> 16383)
7771 heads
= strtol(p
, (char **)&p
, 0);
7772 if (heads
< 1 || heads
> 16)
7777 secs
= strtol(p
, (char **)&p
, 0);
7778 if (secs
< 1 || secs
> 63)
7782 if (!strcmp(p
, "none"))
7783 translation
= BIOS_ATA_TRANSLATION_NONE
;
7784 else if (!strcmp(p
, "lba"))
7785 translation
= BIOS_ATA_TRANSLATION_LBA
;
7786 else if (!strcmp(p
, "auto"))
7787 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7790 } else if (*p
!= '\0') {
7792 fprintf(stderr
, "qemu: invalid physical CHS format\n");
7797 case QEMU_OPTION_nographic
:
7798 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
7799 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "null");
7800 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
7803 case QEMU_OPTION_portrait
:
7806 case QEMU_OPTION_kernel
:
7807 kernel_filename
= optarg
;
7809 case QEMU_OPTION_append
:
7810 kernel_cmdline
= optarg
;
7812 case QEMU_OPTION_cdrom
:
7813 if (cdrom_index
>= 0) {
7814 hd_filename
[cdrom_index
] = optarg
;
7817 case QEMU_OPTION_boot
:
7818 if (strlen(optarg
) > MAX_BOOT_DEVICES
) {
7819 fprintf(stderr
, "qemu: too many boot devices\n");
7822 strncpy(boot_device
, optarg
, MAX_BOOT_DEVICES
);
7823 #if defined(TARGET_SPARC) || defined(TARGET_I386)
7824 #define BOOTCHARS "acdn"
7826 #define BOOTCHARS "acd"
7828 if (strlen(boot_device
) != strspn(boot_device
, BOOTCHARS
)) {
7829 fprintf(stderr
, "qemu: invalid boot device "
7830 "sequence '%s'\n", boot_device
);
7834 case QEMU_OPTION_fda
:
7835 fd_filename
[0] = optarg
;
7837 case QEMU_OPTION_fdb
:
7838 fd_filename
[1] = optarg
;
7841 case QEMU_OPTION_no_fd_bootchk
:
7845 case QEMU_OPTION_no_code_copy
:
7846 code_copy_enabled
= 0;
7848 case QEMU_OPTION_net
:
7849 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
7850 fprintf(stderr
, "qemu: too many network clients\n");
7853 pstrcpy(net_clients
[nb_net_clients
],
7854 sizeof(net_clients
[0]),
7859 case QEMU_OPTION_tftp
:
7860 tftp_prefix
= optarg
;
7862 case QEMU_OPTION_bootp
:
7863 bootp_filename
= optarg
;
7866 case QEMU_OPTION_smb
:
7867 net_slirp_smb(optarg
);
7870 case QEMU_OPTION_redir
:
7871 net_slirp_redir(optarg
);
7875 case QEMU_OPTION_audio_help
:
7879 case QEMU_OPTION_soundhw
:
7880 select_soundhw (optarg
);
7887 ram_size
= atoi(optarg
) * 1024 * 1024;
7890 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
7891 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
7892 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
7901 mask
= cpu_str_to_log_mask(optarg
);
7903 printf("Log items (comma separated):\n");
7904 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
7905 printf("%-10s %s\n", item
->name
, item
->help
);
7912 #ifdef CONFIG_GDBSTUB
7917 gdbstub_port
= optarg
;
7923 case QEMU_OPTION_bios
:
7930 keyboard_layout
= optarg
;
7932 case QEMU_OPTION_localtime
:
7935 case QEMU_OPTION_cirrusvga
:
7936 cirrus_vga_enabled
= 1;
7939 case QEMU_OPTION_vmsvga
:
7940 cirrus_vga_enabled
= 0;
7943 case QEMU_OPTION_std_vga
:
7944 cirrus_vga_enabled
= 0;
7952 w
= strtol(p
, (char **)&p
, 10);
7955 fprintf(stderr
, "qemu: invalid resolution or depth\n");
7961 h
= strtol(p
, (char **)&p
, 10);
7966 depth
= strtol(p
, (char **)&p
, 10);
7967 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
7968 depth
!= 24 && depth
!= 32)
7970 } else if (*p
== '\0') {
7971 depth
= graphic_depth
;
7978 graphic_depth
= depth
;
7981 case QEMU_OPTION_echr
:
7984 term_escape_char
= strtol(optarg
, &r
, 0);
7986 printf("Bad argument to echr\n");
7989 case QEMU_OPTION_monitor
:
7990 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
7992 case QEMU_OPTION_serial
:
7993 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
7994 fprintf(stderr
, "qemu: too many serial ports\n");
7997 pstrcpy(serial_devices
[serial_device_index
],
7998 sizeof(serial_devices
[0]), optarg
);
7999 serial_device_index
++;
8001 case QEMU_OPTION_parallel
:
8002 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
8003 fprintf(stderr
, "qemu: too many parallel ports\n");
8006 pstrcpy(parallel_devices
[parallel_device_index
],
8007 sizeof(parallel_devices
[0]), optarg
);
8008 parallel_device_index
++;
8010 case QEMU_OPTION_loadvm
:
8013 case QEMU_OPTION_full_screen
:
8017 case QEMU_OPTION_no_frame
:
8020 case QEMU_OPTION_alt_grab
:
8023 case QEMU_OPTION_no_quit
:
8027 case QEMU_OPTION_pidfile
:
8031 case QEMU_OPTION_win2k_hack
:
8032 win2k_install_hack
= 1;
8036 case QEMU_OPTION_no_kqemu
:
8039 case QEMU_OPTION_kernel_kqemu
:
8043 case QEMU_OPTION_usb
:
8046 case QEMU_OPTION_usbdevice
:
8048 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
8049 fprintf(stderr
, "Too many USB devices\n");
8052 pstrcpy(usb_devices
[usb_devices_index
],
8053 sizeof(usb_devices
[usb_devices_index
]),
8055 usb_devices_index
++;
8057 case QEMU_OPTION_smp
:
8058 smp_cpus
= atoi(optarg
);
8059 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
8060 fprintf(stderr
, "Invalid number of CPUs\n");
8064 case QEMU_OPTION_vnc
:
8065 vnc_display
= optarg
;
8067 case QEMU_OPTION_no_acpi
:
8070 case QEMU_OPTION_no_reboot
:
8073 case QEMU_OPTION_show_cursor
:
8076 case QEMU_OPTION_daemonize
:
8079 case QEMU_OPTION_option_rom
:
8080 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
8081 fprintf(stderr
, "Too many option ROMs\n");
8084 option_rom
[nb_option_roms
] = optarg
;
8087 case QEMU_OPTION_semihosting
:
8088 semihosting_enabled
= 1;
8090 case QEMU_OPTION_name
:
8094 case QEMU_OPTION_prom_env
:
8095 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
8096 fprintf(stderr
, "Too many prom variables\n");
8099 prom_envs
[nb_prom_envs
] = optarg
;
8104 case QEMU_OPTION_old_param
:
8107 case QEMU_OPTION_clock
:
8108 configure_alarms(optarg
);
8115 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
8116 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
8123 if (pipe(fds
) == -1)
8134 len
= read(fds
[0], &status
, 1);
8135 if (len
== -1 && (errno
== EINTR
))
8140 else if (status
== 1) {
8141 fprintf(stderr
, "Could not acquire pidfile\n");
8159 signal(SIGTSTP
, SIG_IGN
);
8160 signal(SIGTTOU
, SIG_IGN
);
8161 signal(SIGTTIN
, SIG_IGN
);
8165 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
8168 write(fds
[1], &status
, 1);
8170 fprintf(stderr
, "Could not acquire pid file\n");
8178 linux_boot
= (kernel_filename
!= NULL
);
8181 (!strchr(boot_device
, 'n')) &&
8182 hd_filename
[0] == '\0' &&
8183 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
8184 fd_filename
[0] == '\0')
8187 /* boot to floppy or the default cd if no hard disk defined yet */
8188 if (!boot_device
[0]) {
8189 if (hd_filename
[0] != '\0')
8190 boot_device
[0] = 'c';
8191 else if (fd_filename
[0] != '\0')
8192 boot_device
[0] = 'a';
8194 boot_device
[0] = 'd';
8197 setvbuf(stdout
, NULL
, _IOLBF
, 0);
8207 /* init network clients */
8208 if (nb_net_clients
== 0) {
8209 /* if no clients, we use a default config */
8210 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
8212 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
8217 for(i
= 0;i
< nb_net_clients
; i
++) {
8218 if (net_client_init(net_clients
[i
]) < 0)
8221 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
8222 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
8224 if (vlan
->nb_guest_devs
== 0) {
8225 fprintf(stderr
, "Invalid vlan (%d) with no nics\n", vlan
->id
);
8228 if (vlan
->nb_host_devs
== 0)
8230 "Warning: vlan %d is not connected to host network\n",
8235 if (strchr(boot_device
, 'n')) {
8236 for (i
= 0; i
< nb_nics
; i
++) {
8237 const char *model
= nd_table
[i
].model
;
8241 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
8242 if (get_image_size(buf
) > 0) {
8243 option_rom
[nb_option_roms
] = strdup(buf
);
8249 fprintf(stderr
, "No valid PXE rom found for network device\n");
8255 /* init the memory */
8256 phys_ram_size
= ram_size
+ vga_ram_size
+ MAX_BIOS_SIZE
;
8258 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
8259 if (!phys_ram_base
) {
8260 fprintf(stderr
, "Could not allocate physical memory\n");
8264 /* we always create the cdrom drive, even if no disk is there */
8266 if (cdrom_index
>= 0) {
8267 bs_table
[cdrom_index
] = bdrv_new("cdrom");
8268 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
8271 /* open the virtual block devices */
8272 for(i
= 0; i
< MAX_DISKS
; i
++) {
8273 if (hd_filename
[i
]) {
8276 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
8277 bs_table
[i
] = bdrv_new(buf
);
8279 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8280 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
8284 if (i
== 0 && cyls
!= 0) {
8285 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
8286 bdrv_set_translation_hint(bs_table
[i
], translation
);
8291 /* we always create at least one floppy disk */
8292 fd_table
[0] = bdrv_new("fda");
8293 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
8295 for(i
= 0; i
< MAX_FD
; i
++) {
8296 if (fd_filename
[i
]) {
8299 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
8300 fd_table
[i
] = bdrv_new(buf
);
8301 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
8303 if (fd_filename
[i
][0] != '\0') {
8304 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
8305 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8306 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
8314 /* Open the virtual parallel flash block devices */
8315 for(i
= 0; i
< MAX_PFLASH
; i
++) {
8316 if (pflash_filename
[i
]) {
8317 if (!pflash_table
[i
]) {
8319 snprintf(buf
, sizeof(buf
), "fl%c", i
+ 'a');
8320 pflash_table
[i
] = bdrv_new(buf
);
8322 if (bdrv_open(pflash_table
[i
], pflash_filename
[i
],
8323 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8324 fprintf(stderr
, "qemu: could not open flash image '%s'\n",
8325 pflash_filename
[i
]);
8331 sd_bdrv
= bdrv_new ("sd");
8332 /* FIXME: This isn't really a floppy, but it's a reasonable
8334 bdrv_set_type_hint(sd_bdrv
, BDRV_TYPE_FLOPPY
);
8336 if (bdrv_open(sd_bdrv
, sd_filename
,
8337 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8338 fprintf(stderr
, "qemu: could not open SD card image %s\n",
8341 qemu_key_check(sd_bdrv
, sd_filename
);
8345 mtd_bdrv
= bdrv_new ("mtd");
8346 if (bdrv_open(mtd_bdrv
, mtd_filename
,
8347 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0 ||
8348 qemu_key_check(mtd_bdrv
, mtd_filename
)) {
8349 fprintf(stderr
, "qemu: could not open Flash image %s\n",
8351 bdrv_delete(mtd_bdrv
);
8356 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
8357 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
8362 memset(&display_state
, 0, sizeof(display_state
));
8364 /* nearly nothing to do */
8365 dumb_display_init(ds
);
8366 } else if (vnc_display
!= NULL
) {
8367 vnc_display_init(ds
);
8368 if (vnc_display_open(ds
, vnc_display
) < 0)
8371 #if defined(CONFIG_SDL)
8372 sdl_display_init(ds
, full_screen
, no_frame
);
8373 #elif defined(CONFIG_COCOA)
8374 cocoa_display_init(ds
, full_screen
);
8378 /* Maintain compatibility with multiple stdio monitors */
8379 if (!strcmp(monitor_device
,"stdio")) {
8380 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
8381 if (!strcmp(serial_devices
[i
],"mon:stdio")) {
8382 monitor_device
[0] = '\0';
8384 } else if (!strcmp(serial_devices
[i
],"stdio")) {
8385 monitor_device
[0] = '\0';
8386 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "mon:stdio");
8391 if (monitor_device
[0] != '\0') {
8392 monitor_hd
= qemu_chr_open(monitor_device
);
8394 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
8397 monitor_init(monitor_hd
, !nographic
);
8400 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
8401 const char *devname
= serial_devices
[i
];
8402 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
8403 serial_hds
[i
] = qemu_chr_open(devname
);
8404 if (!serial_hds
[i
]) {
8405 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
8409 if (strstart(devname
, "vc", 0))
8410 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
8414 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
8415 const char *devname
= parallel_devices
[i
];
8416 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
8417 parallel_hds
[i
] = qemu_chr_open(devname
);
8418 if (!parallel_hds
[i
]) {
8419 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
8423 if (strstart(devname
, "vc", 0))
8424 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
8428 machine
->init(ram_size
, vga_ram_size
, boot_device
,
8429 ds
, fd_filename
, snapshot
,
8430 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
8432 /* init USB devices */
8434 for(i
= 0; i
< usb_devices_index
; i
++) {
8435 if (usb_device_add(usb_devices
[i
]) < 0) {
8436 fprintf(stderr
, "Warning: could not add USB device %s\n",
8442 if (display_state
.dpy_refresh
) {
8443 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
8444 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
8447 #ifdef CONFIG_GDBSTUB
8449 /* XXX: use standard host:port notation and modify options
8451 if (gdbserver_start(gdbstub_port
) < 0) {
8452 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
8463 /* XXX: simplify init */
8476 len
= write(fds
[1], &status
, 1);
8477 if (len
== -1 && (errno
== EINTR
))
8483 TFR(fd
= open("/dev/null", O_RDWR
));
8497 #if !defined(_WIN32)
8498 /* close network clients */
8499 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
8500 VLANClientState
*vc
;
8502 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
8503 if (vc
->fd_read
== tap_receive
) {
8505 TAPState
*s
= vc
->opaque
;
8507 if (sscanf(vc
->info_str
, "tap: ifname=%63s ", ifname
) == 1 &&
8509 launch_script(s
->down_script
, ifname
, s
->fd
);