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 int boot_device
= 'c';
167 int pit_min_timer_count
= 0;
169 NICInfo nd_table
[MAX_NICS
];
172 int cirrus_vga_enabled
= 1;
173 int vmsvga_enabled
= 0;
175 int graphic_width
= 1024;
176 int graphic_height
= 768;
177 int graphic_depth
= 8;
179 int graphic_width
= 800;
180 int graphic_height
= 600;
181 int graphic_depth
= 15;
186 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
187 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
189 int win2k_install_hack
= 0;
192 static VLANState
*first_vlan
;
194 const char *vnc_display
;
195 #if defined(TARGET_SPARC)
197 #elif defined(TARGET_I386)
202 int acpi_enabled
= 1;
206 int graphic_rotate
= 0;
208 const char *option_rom
[MAX_OPTION_ROMS
];
210 int semihosting_enabled
= 0;
215 const char *qemu_name
;
218 unsigned int nb_prom_envs
= 0;
219 const char *prom_envs
[MAX_PROM_ENVS
];
222 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
224 /***********************************************************/
225 /* x86 ISA bus support */
227 target_phys_addr_t isa_mem_base
= 0;
230 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
232 #ifdef DEBUG_UNUSED_IOPORT
233 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
238 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
240 #ifdef DEBUG_UNUSED_IOPORT
241 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
245 /* default is to make two byte accesses */
246 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
249 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
250 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
251 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
255 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
257 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
258 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
259 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
262 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
264 #ifdef DEBUG_UNUSED_IOPORT
265 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
270 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
272 #ifdef DEBUG_UNUSED_IOPORT
273 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
277 void init_ioports(void)
281 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
282 ioport_read_table
[0][i
] = default_ioport_readb
;
283 ioport_write_table
[0][i
] = default_ioport_writeb
;
284 ioport_read_table
[1][i
] = default_ioport_readw
;
285 ioport_write_table
[1][i
] = default_ioport_writew
;
286 ioport_read_table
[2][i
] = default_ioport_readl
;
287 ioport_write_table
[2][i
] = default_ioport_writel
;
291 /* size is the word size in byte */
292 int register_ioport_read(int start
, int length
, int size
,
293 IOPortReadFunc
*func
, void *opaque
)
299 } else if (size
== 2) {
301 } else if (size
== 4) {
304 hw_error("register_ioport_read: invalid size");
307 for(i
= start
; i
< start
+ length
; i
+= size
) {
308 ioport_read_table
[bsize
][i
] = func
;
309 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
310 hw_error("register_ioport_read: invalid opaque");
311 ioport_opaque
[i
] = opaque
;
316 /* size is the word size in byte */
317 int register_ioport_write(int start
, int length
, int size
,
318 IOPortWriteFunc
*func
, void *opaque
)
324 } else if (size
== 2) {
326 } else if (size
== 4) {
329 hw_error("register_ioport_write: invalid size");
332 for(i
= start
; i
< start
+ length
; i
+= size
) {
333 ioport_write_table
[bsize
][i
] = func
;
334 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
335 hw_error("register_ioport_write: invalid opaque");
336 ioport_opaque
[i
] = opaque
;
341 void isa_unassign_ioport(int start
, int length
)
345 for(i
= start
; i
< start
+ length
; i
++) {
346 ioport_read_table
[0][i
] = default_ioport_readb
;
347 ioport_read_table
[1][i
] = default_ioport_readw
;
348 ioport_read_table
[2][i
] = default_ioport_readl
;
350 ioport_write_table
[0][i
] = default_ioport_writeb
;
351 ioport_write_table
[1][i
] = default_ioport_writew
;
352 ioport_write_table
[2][i
] = default_ioport_writel
;
356 /***********************************************************/
358 void cpu_outb(CPUState
*env
, int addr
, int val
)
361 if (loglevel
& CPU_LOG_IOPORT
)
362 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
364 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
367 env
->last_io_time
= cpu_get_time_fast();
371 void cpu_outw(CPUState
*env
, int addr
, int val
)
374 if (loglevel
& CPU_LOG_IOPORT
)
375 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
377 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
380 env
->last_io_time
= cpu_get_time_fast();
384 void cpu_outl(CPUState
*env
, int addr
, int val
)
387 if (loglevel
& CPU_LOG_IOPORT
)
388 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
390 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
393 env
->last_io_time
= cpu_get_time_fast();
397 int cpu_inb(CPUState
*env
, int addr
)
400 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
402 if (loglevel
& CPU_LOG_IOPORT
)
403 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
407 env
->last_io_time
= cpu_get_time_fast();
412 int cpu_inw(CPUState
*env
, int addr
)
415 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
417 if (loglevel
& CPU_LOG_IOPORT
)
418 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
422 env
->last_io_time
= cpu_get_time_fast();
427 int cpu_inl(CPUState
*env
, int addr
)
430 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
432 if (loglevel
& CPU_LOG_IOPORT
)
433 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
437 env
->last_io_time
= cpu_get_time_fast();
442 /***********************************************************/
443 void hw_error(const char *fmt
, ...)
449 fprintf(stderr
, "qemu: hardware error: ");
450 vfprintf(stderr
, fmt
, ap
);
451 fprintf(stderr
, "\n");
452 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
453 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
455 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
457 cpu_dump_state(env
, stderr
, fprintf
, 0);
464 /***********************************************************/
467 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
468 static void *qemu_put_kbd_event_opaque
;
469 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
470 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
472 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
474 qemu_put_kbd_event_opaque
= opaque
;
475 qemu_put_kbd_event
= func
;
478 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
479 void *opaque
, int absolute
,
482 QEMUPutMouseEntry
*s
, *cursor
;
484 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
488 s
->qemu_put_mouse_event
= func
;
489 s
->qemu_put_mouse_event_opaque
= opaque
;
490 s
->qemu_put_mouse_event_absolute
= absolute
;
491 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
494 if (!qemu_put_mouse_event_head
) {
495 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
499 cursor
= qemu_put_mouse_event_head
;
500 while (cursor
->next
!= NULL
)
501 cursor
= cursor
->next
;
504 qemu_put_mouse_event_current
= s
;
509 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
511 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
513 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
516 cursor
= qemu_put_mouse_event_head
;
517 while (cursor
!= NULL
&& cursor
!= entry
) {
519 cursor
= cursor
->next
;
522 if (cursor
== NULL
) // does not exist or list empty
524 else if (prev
== NULL
) { // entry is head
525 qemu_put_mouse_event_head
= cursor
->next
;
526 if (qemu_put_mouse_event_current
== entry
)
527 qemu_put_mouse_event_current
= cursor
->next
;
528 qemu_free(entry
->qemu_put_mouse_event_name
);
533 prev
->next
= entry
->next
;
535 if (qemu_put_mouse_event_current
== entry
)
536 qemu_put_mouse_event_current
= prev
;
538 qemu_free(entry
->qemu_put_mouse_event_name
);
542 void kbd_put_keycode(int keycode
)
544 if (qemu_put_kbd_event
) {
545 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
549 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
551 QEMUPutMouseEvent
*mouse_event
;
552 void *mouse_event_opaque
;
555 if (!qemu_put_mouse_event_current
) {
560 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
562 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
565 if (graphic_rotate
) {
566 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
569 width
= graphic_width
;
570 mouse_event(mouse_event_opaque
,
571 width
- dy
, dx
, dz
, buttons_state
);
573 mouse_event(mouse_event_opaque
,
574 dx
, dy
, dz
, buttons_state
);
578 int kbd_mouse_is_absolute(void)
580 if (!qemu_put_mouse_event_current
)
583 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
586 void do_info_mice(void)
588 QEMUPutMouseEntry
*cursor
;
591 if (!qemu_put_mouse_event_head
) {
592 term_printf("No mouse devices connected\n");
596 term_printf("Mouse devices available:\n");
597 cursor
= qemu_put_mouse_event_head
;
598 while (cursor
!= NULL
) {
599 term_printf("%c Mouse #%d: %s\n",
600 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
601 index
, cursor
->qemu_put_mouse_event_name
);
603 cursor
= cursor
->next
;
607 void do_mouse_set(int index
)
609 QEMUPutMouseEntry
*cursor
;
612 if (!qemu_put_mouse_event_head
) {
613 term_printf("No mouse devices connected\n");
617 cursor
= qemu_put_mouse_event_head
;
618 while (cursor
!= NULL
&& index
!= i
) {
620 cursor
= cursor
->next
;
624 qemu_put_mouse_event_current
= cursor
;
626 term_printf("Mouse at given index not found\n");
629 /* compute with 96 bit intermediate result: (a*b)/c */
630 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
635 #ifdef WORDS_BIGENDIAN
645 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
646 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
649 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
653 /***********************************************************/
654 /* real time host monotonic timer */
656 #define QEMU_TIMER_BASE 1000000000LL
660 static int64_t clock_freq
;
662 static void init_get_clock(void)
666 ret
= QueryPerformanceFrequency(&freq
);
668 fprintf(stderr
, "Could not calibrate ticks\n");
671 clock_freq
= freq
.QuadPart
;
674 static int64_t get_clock(void)
677 QueryPerformanceCounter(&ti
);
678 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
683 static int use_rt_clock
;
685 static void init_get_clock(void)
688 #if defined(__linux__)
691 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
698 static int64_t get_clock(void)
700 #if defined(__linux__)
703 clock_gettime(CLOCK_MONOTONIC
, &ts
);
704 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
708 /* XXX: using gettimeofday leads to problems if the date
709 changes, so it should be avoided. */
711 gettimeofday(&tv
, NULL
);
712 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
718 /***********************************************************/
719 /* guest cycle counter */
721 static int64_t cpu_ticks_prev
;
722 static int64_t cpu_ticks_offset
;
723 static int64_t cpu_clock_offset
;
724 static int cpu_ticks_enabled
;
726 /* return the host CPU cycle counter and handle stop/restart */
727 int64_t cpu_get_ticks(void)
729 if (!cpu_ticks_enabled
) {
730 return cpu_ticks_offset
;
733 ticks
= cpu_get_real_ticks();
734 if (cpu_ticks_prev
> ticks
) {
735 /* Note: non increasing ticks may happen if the host uses
737 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
739 cpu_ticks_prev
= ticks
;
740 return ticks
+ cpu_ticks_offset
;
744 /* return the host CPU monotonic timer and handle stop/restart */
745 static int64_t cpu_get_clock(void)
748 if (!cpu_ticks_enabled
) {
749 return cpu_clock_offset
;
752 return ti
+ cpu_clock_offset
;
756 /* enable cpu_get_ticks() */
757 void cpu_enable_ticks(void)
759 if (!cpu_ticks_enabled
) {
760 cpu_ticks_offset
-= cpu_get_real_ticks();
761 cpu_clock_offset
-= get_clock();
762 cpu_ticks_enabled
= 1;
766 /* disable cpu_get_ticks() : the clock is stopped. You must not call
767 cpu_get_ticks() after that. */
768 void cpu_disable_ticks(void)
770 if (cpu_ticks_enabled
) {
771 cpu_ticks_offset
= cpu_get_ticks();
772 cpu_clock_offset
= cpu_get_clock();
773 cpu_ticks_enabled
= 0;
777 /***********************************************************/
780 #define QEMU_TIMER_REALTIME 0
781 #define QEMU_TIMER_VIRTUAL 1
785 /* XXX: add frequency */
793 struct QEMUTimer
*next
;
796 struct qemu_alarm_timer
{
800 int (*start
)(struct qemu_alarm_timer
*t
);
801 void (*stop
)(struct qemu_alarm_timer
*t
);
802 void (*rearm
)(struct qemu_alarm_timer
*t
);
806 #define ALARM_FLAG_DYNTICKS 0x1
808 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
810 return t
->flags
& ALARM_FLAG_DYNTICKS
;
813 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
815 if (!alarm_has_dynticks(t
))
821 /* TODO: MIN_TIMER_REARM_US should be optimized */
822 #define MIN_TIMER_REARM_US 250
824 static struct qemu_alarm_timer
*alarm_timer
;
828 struct qemu_alarm_win32
{
832 } alarm_win32_data
= {0, NULL
, -1};
834 static int win32_start_timer(struct qemu_alarm_timer
*t
);
835 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
836 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
840 static int unix_start_timer(struct qemu_alarm_timer
*t
);
841 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
845 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
846 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
847 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
849 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
850 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
852 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
853 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
855 #endif /* __linux__ */
859 static struct qemu_alarm_timer alarm_timers
[] = {
862 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
863 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
864 /* HPET - if available - is preferred */
865 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
866 /* ...otherwise try RTC */
867 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
869 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
871 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
872 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
873 {"win32", 0, win32_start_timer
,
874 win32_stop_timer
, NULL
, &alarm_win32_data
},
879 static void show_available_alarms()
883 printf("Available alarm timers, in order of precedence:\n");
884 for (i
= 0; alarm_timers
[i
].name
; i
++)
885 printf("%s\n", alarm_timers
[i
].name
);
888 static void configure_alarms(char const *opt
)
892 int count
= (sizeof(alarm_timers
) / sizeof(*alarm_timers
)) - 1;
896 if (!strcmp(opt
, "help")) {
897 show_available_alarms();
903 /* Reorder the array */
904 name
= strtok(arg
, ",");
906 struct qemu_alarm_timer tmp
;
908 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
909 if (!strcmp(alarm_timers
[i
].name
, name
))
914 fprintf(stderr
, "Unknown clock %s\n", name
);
923 tmp
= alarm_timers
[i
];
924 alarm_timers
[i
] = alarm_timers
[cur
];
925 alarm_timers
[cur
] = tmp
;
929 name
= strtok(NULL
, ",");
935 /* Disable remaining timers */
936 for (i
= cur
; i
< count
; i
++)
937 alarm_timers
[i
].name
= NULL
;
941 show_available_alarms();
947 static QEMUTimer
*active_timers
[2];
949 QEMUClock
*qemu_new_clock(int type
)
952 clock
= qemu_mallocz(sizeof(QEMUClock
));
959 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
963 ts
= qemu_mallocz(sizeof(QEMUTimer
));
970 void qemu_free_timer(QEMUTimer
*ts
)
975 /* stop a timer, but do not dealloc it */
976 void qemu_del_timer(QEMUTimer
*ts
)
980 /* NOTE: this code must be signal safe because
981 qemu_timer_expired() can be called from a signal. */
982 pt
= &active_timers
[ts
->clock
->type
];
995 /* modify the current timer so that it will be fired when current_time
996 >= expire_time. The corresponding callback will be called. */
997 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1003 /* add the timer in the sorted list */
1004 /* NOTE: this code must be signal safe because
1005 qemu_timer_expired() can be called from a signal. */
1006 pt
= &active_timers
[ts
->clock
->type
];
1011 if (t
->expire_time
> expire_time
)
1015 ts
->expire_time
= expire_time
;
1020 int qemu_timer_pending(QEMUTimer
*ts
)
1023 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1030 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1034 return (timer_head
->expire_time
<= current_time
);
1037 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1043 if (!ts
|| ts
->expire_time
> current_time
)
1045 /* remove timer from the list before calling the callback */
1046 *ptimer_head
= ts
->next
;
1049 /* run the callback (the timer list can be modified) */
1052 qemu_rearm_alarm_timer(alarm_timer
);
1055 int64_t qemu_get_clock(QEMUClock
*clock
)
1057 switch(clock
->type
) {
1058 case QEMU_TIMER_REALTIME
:
1059 return get_clock() / 1000000;
1061 case QEMU_TIMER_VIRTUAL
:
1062 return cpu_get_clock();
1066 static void init_timers(void)
1069 ticks_per_sec
= QEMU_TIMER_BASE
;
1070 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1071 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1075 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1077 uint64_t expire_time
;
1079 if (qemu_timer_pending(ts
)) {
1080 expire_time
= ts
->expire_time
;
1084 qemu_put_be64(f
, expire_time
);
1087 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1089 uint64_t expire_time
;
1091 expire_time
= qemu_get_be64(f
);
1092 if (expire_time
!= -1) {
1093 qemu_mod_timer(ts
, expire_time
);
1099 static void timer_save(QEMUFile
*f
, void *opaque
)
1101 if (cpu_ticks_enabled
) {
1102 hw_error("cannot save state if virtual timers are running");
1104 qemu_put_be64s(f
, &cpu_ticks_offset
);
1105 qemu_put_be64s(f
, &ticks_per_sec
);
1106 qemu_put_be64s(f
, &cpu_clock_offset
);
1109 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1111 if (version_id
!= 1 && version_id
!= 2)
1113 if (cpu_ticks_enabled
) {
1116 qemu_get_be64s(f
, &cpu_ticks_offset
);
1117 qemu_get_be64s(f
, &ticks_per_sec
);
1118 if (version_id
== 2) {
1119 qemu_get_be64s(f
, &cpu_clock_offset
);
1125 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1126 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1128 static void host_alarm_handler(int host_signum
)
1132 #define DISP_FREQ 1000
1134 static int64_t delta_min
= INT64_MAX
;
1135 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1137 ti
= qemu_get_clock(vm_clock
);
1138 if (last_clock
!= 0) {
1139 delta
= ti
- last_clock
;
1140 if (delta
< delta_min
)
1142 if (delta
> delta_max
)
1145 if (++count
== DISP_FREQ
) {
1146 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1147 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1148 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1149 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1150 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1152 delta_min
= INT64_MAX
;
1160 if (alarm_has_dynticks(alarm_timer
) ||
1161 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1162 qemu_get_clock(vm_clock
)) ||
1163 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1164 qemu_get_clock(rt_clock
))) {
1166 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1167 SetEvent(data
->host_alarm
);
1169 CPUState
*env
= cpu_single_env
;
1171 /* stop the currently executing cpu because a timer occured */
1172 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1174 if (env
->kqemu_enabled
) {
1175 kqemu_cpu_interrupt(env
);
1182 static uint64_t qemu_next_deadline(void)
1184 int64_t nearest_delta_us
= INT64_MAX
;
1187 if (active_timers
[QEMU_TIMER_REALTIME
])
1188 nearest_delta_us
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1189 qemu_get_clock(rt_clock
))*1000;
1191 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1193 vmdelta_us
= (active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1194 qemu_get_clock(vm_clock
)+999)/1000;
1195 if (vmdelta_us
< nearest_delta_us
)
1196 nearest_delta_us
= vmdelta_us
;
1199 /* Avoid arming the timer to negative, zero, or too low values */
1200 if (nearest_delta_us
<= MIN_TIMER_REARM_US
)
1201 nearest_delta_us
= MIN_TIMER_REARM_US
;
1203 return nearest_delta_us
;
1208 #if defined(__linux__)
1210 #define RTC_FREQ 1024
1212 static void enable_sigio_timer(int fd
)
1214 struct sigaction act
;
1217 sigfillset(&act
.sa_mask
);
1219 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1220 act
.sa_flags
|= SA_ONSTACK
;
1222 act
.sa_handler
= host_alarm_handler
;
1224 sigaction(SIGIO
, &act
, NULL
);
1225 fcntl(fd
, F_SETFL
, O_ASYNC
);
1226 fcntl(fd
, F_SETOWN
, getpid());
1229 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1231 struct hpet_info info
;
1234 fd
= open("/dev/hpet", O_RDONLY
);
1239 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1241 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1242 "error, but for better emulation accuracy type:\n"
1243 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1247 /* Check capabilities */
1248 r
= ioctl(fd
, HPET_INFO
, &info
);
1252 /* Enable periodic mode */
1253 r
= ioctl(fd
, HPET_EPI
, 0);
1254 if (info
.hi_flags
&& (r
< 0))
1257 /* Enable interrupt */
1258 r
= ioctl(fd
, HPET_IE_ON
, 0);
1262 enable_sigio_timer(fd
);
1263 t
->priv
= (void *)(long)fd
;
1271 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1273 int fd
= (long)t
->priv
;
1278 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1282 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1285 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1286 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1287 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1288 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1291 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1297 enable_sigio_timer(rtc_fd
);
1299 t
->priv
= (void *)(long)rtc_fd
;
1304 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1306 int rtc_fd
= (long)t
->priv
;
1311 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1315 struct sigaction act
;
1317 sigfillset(&act
.sa_mask
);
1319 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
1320 act
.sa_flags
|= SA_ONSTACK
;
1322 act
.sa_handler
= host_alarm_handler
;
1324 sigaction(SIGALRM
, &act
, NULL
);
1326 ev
.sigev_value
.sival_int
= 0;
1327 ev
.sigev_notify
= SIGEV_SIGNAL
;
1328 ev
.sigev_signo
= SIGALRM
;
1330 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1331 perror("timer_create");
1333 /* disable dynticks */
1334 fprintf(stderr
, "Dynamic Ticks disabled\n");
1339 t
->priv
= (void *)host_timer
;
1344 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1346 timer_t host_timer
= (timer_t
)t
->priv
;
1348 timer_delete(host_timer
);
1351 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1353 timer_t host_timer
= (timer_t
)t
->priv
;
1354 struct itimerspec timeout
;
1355 int64_t nearest_delta_us
= INT64_MAX
;
1358 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1359 !active_timers
[QEMU_TIMER_VIRTUAL
])
1362 nearest_delta_us
= qemu_next_deadline();
1364 /* check whether a timer is already running */
1365 if (timer_gettime(host_timer
, &timeout
)) {
1367 fprintf(stderr
, "Internal timer error: aborting\n");
1370 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1371 if (current_us
&& current_us
<= nearest_delta_us
)
1374 timeout
.it_interval
.tv_sec
= 0;
1375 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1376 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1377 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1378 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1380 fprintf(stderr
, "Internal timer error: aborting\n");
1385 #endif /* defined(__linux__) */
1387 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1389 struct sigaction act
;
1390 struct itimerval itv
;
1394 sigfillset(&act
.sa_mask
);
1396 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
1397 act
.sa_flags
|= SA_ONSTACK
;
1399 act
.sa_handler
= host_alarm_handler
;
1401 sigaction(SIGALRM
, &act
, NULL
);
1403 itv
.it_interval
.tv_sec
= 0;
1404 /* for i386 kernel 2.6 to get 1 ms */
1405 itv
.it_interval
.tv_usec
= 999;
1406 itv
.it_value
.tv_sec
= 0;
1407 itv
.it_value
.tv_usec
= 10 * 1000;
1409 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1416 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1418 struct itimerval itv
;
1420 memset(&itv
, 0, sizeof(itv
));
1421 setitimer(ITIMER_REAL
, &itv
, NULL
);
1424 #endif /* !defined(_WIN32) */
1428 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1431 struct qemu_alarm_win32
*data
= t
->priv
;
1434 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1435 if (!data
->host_alarm
) {
1436 perror("Failed CreateEvent");
1440 memset(&tc
, 0, sizeof(tc
));
1441 timeGetDevCaps(&tc
, sizeof(tc
));
1443 if (data
->period
< tc
.wPeriodMin
)
1444 data
->period
= tc
.wPeriodMin
;
1446 timeBeginPeriod(data
->period
);
1448 flags
= TIME_CALLBACK_FUNCTION
;
1449 if (alarm_has_dynticks(t
))
1450 flags
|= TIME_ONESHOT
;
1452 flags
|= TIME_PERIODIC
;
1454 data
->timerId
= timeSetEvent(1, // interval (ms)
1455 data
->period
, // resolution
1456 host_alarm_handler
, // function
1457 (DWORD
)t
, // parameter
1460 if (!data
->timerId
) {
1461 perror("Failed to initialize win32 alarm timer");
1463 timeEndPeriod(data
->period
);
1464 CloseHandle(data
->host_alarm
);
1468 qemu_add_wait_object(data
->host_alarm
, NULL
, NULL
);
1473 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1475 struct qemu_alarm_win32
*data
= t
->priv
;
1477 timeKillEvent(data
->timerId
);
1478 timeEndPeriod(data
->period
);
1480 CloseHandle(data
->host_alarm
);
1483 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1485 struct qemu_alarm_win32
*data
= t
->priv
;
1486 uint64_t nearest_delta_us
;
1488 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1489 !active_timers
[QEMU_TIMER_VIRTUAL
])
1492 nearest_delta_us
= qemu_next_deadline();
1493 nearest_delta_us
/= 1000;
1495 timeKillEvent(data
->timerId
);
1497 data
->timerId
= timeSetEvent(1,
1501 TIME_ONESHOT
| TIME_PERIODIC
);
1503 if (!data
->timerId
) {
1504 perror("Failed to re-arm win32 alarm timer");
1506 timeEndPeriod(data
->period
);
1507 CloseHandle(data
->host_alarm
);
1514 static void init_timer_alarm(void)
1516 struct qemu_alarm_timer
*t
;
1519 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1520 t
= &alarm_timers
[i
];
1528 fprintf(stderr
, "Unable to find any suitable alarm timer.\n");
1529 fprintf(stderr
, "Terminating\n");
1536 void quit_timers(void)
1538 alarm_timer
->stop(alarm_timer
);
1542 /***********************************************************/
1543 /* character device */
1545 static void qemu_chr_event(CharDriverState
*s
, int event
)
1549 s
->chr_event(s
->handler_opaque
, event
);
1552 static void qemu_chr_reset_bh(void *opaque
)
1554 CharDriverState
*s
= opaque
;
1555 qemu_chr_event(s
, CHR_EVENT_RESET
);
1556 qemu_bh_delete(s
->bh
);
1560 void qemu_chr_reset(CharDriverState
*s
)
1562 if (s
->bh
== NULL
) {
1563 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
1564 qemu_bh_schedule(s
->bh
);
1568 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1570 return s
->chr_write(s
, buf
, len
);
1573 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1577 return s
->chr_ioctl(s
, cmd
, arg
);
1580 int qemu_chr_can_read(CharDriverState
*s
)
1582 if (!s
->chr_can_read
)
1584 return s
->chr_can_read(s
->handler_opaque
);
1587 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
1589 s
->chr_read(s
->handler_opaque
, buf
, len
);
1593 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1598 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1599 qemu_chr_write(s
, buf
, strlen(buf
));
1603 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1605 if (s
->chr_send_event
)
1606 s
->chr_send_event(s
, event
);
1609 void qemu_chr_add_handlers(CharDriverState
*s
,
1610 IOCanRWHandler
*fd_can_read
,
1611 IOReadHandler
*fd_read
,
1612 IOEventHandler
*fd_event
,
1615 s
->chr_can_read
= fd_can_read
;
1616 s
->chr_read
= fd_read
;
1617 s
->chr_event
= fd_event
;
1618 s
->handler_opaque
= opaque
;
1619 if (s
->chr_update_read_handler
)
1620 s
->chr_update_read_handler(s
);
1623 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1628 static CharDriverState
*qemu_chr_open_null(void)
1630 CharDriverState
*chr
;
1632 chr
= qemu_mallocz(sizeof(CharDriverState
));
1635 chr
->chr_write
= null_chr_write
;
1639 /* MUX driver for serial I/O splitting */
1640 static int term_timestamps
;
1641 static int64_t term_timestamps_start
;
1644 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
1645 IOReadHandler
*chr_read
[MAX_MUX
];
1646 IOEventHandler
*chr_event
[MAX_MUX
];
1647 void *ext_opaque
[MAX_MUX
];
1648 CharDriverState
*drv
;
1650 int term_got_escape
;
1655 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1657 MuxDriver
*d
= chr
->opaque
;
1659 if (!term_timestamps
) {
1660 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
1665 for(i
= 0; i
< len
; i
++) {
1666 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
1667 if (buf
[i
] == '\n') {
1673 if (term_timestamps_start
== -1)
1674 term_timestamps_start
= ti
;
1675 ti
-= term_timestamps_start
;
1676 secs
= ti
/ 1000000000;
1677 snprintf(buf1
, sizeof(buf1
),
1678 "[%02d:%02d:%02d.%03d] ",
1682 (int)((ti
/ 1000000) % 1000));
1683 d
->drv
->chr_write(d
->drv
, buf1
, strlen(buf1
));
1690 static char *mux_help
[] = {
1691 "% h print this help\n\r",
1692 "% x exit emulator\n\r",
1693 "% s save disk data back to file (if -snapshot)\n\r",
1694 "% t toggle console timestamps\n\r"
1695 "% b send break (magic sysrq)\n\r",
1696 "% c switch between console and monitor\n\r",
1701 static int term_escape_char
= 0x01; /* ctrl-a is used for escape */
1702 static void mux_print_help(CharDriverState
*chr
)
1705 char ebuf
[15] = "Escape-Char";
1706 char cbuf
[50] = "\n\r";
1708 if (term_escape_char
> 0 && term_escape_char
< 26) {
1709 sprintf(cbuf
,"\n\r");
1710 sprintf(ebuf
,"C-%c", term_escape_char
- 1 + 'a');
1712 sprintf(cbuf
,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char
);
1714 chr
->chr_write(chr
, cbuf
, strlen(cbuf
));
1715 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
1716 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
1717 if (mux_help
[i
][j
] == '%')
1718 chr
->chr_write(chr
, ebuf
, strlen(ebuf
));
1720 chr
->chr_write(chr
, &mux_help
[i
][j
], 1);
1725 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
1727 if (d
->term_got_escape
) {
1728 d
->term_got_escape
= 0;
1729 if (ch
== term_escape_char
)
1734 mux_print_help(chr
);
1738 char *term
= "QEMU: Terminated\n\r";
1739 chr
->chr_write(chr
,term
,strlen(term
));
1746 for (i
= 0; i
< MAX_DISKS
; i
++) {
1748 bdrv_commit(bs_table
[i
]);
1751 bdrv_commit(mtd_bdrv
);
1755 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1758 /* Switch to the next registered device */
1760 if (chr
->focus
>= d
->mux_cnt
)
1764 term_timestamps
= !term_timestamps
;
1765 term_timestamps_start
= -1;
1768 } else if (ch
== term_escape_char
) {
1769 d
->term_got_escape
= 1;
1777 static int mux_chr_can_read(void *opaque
)
1779 CharDriverState
*chr
= opaque
;
1780 MuxDriver
*d
= chr
->opaque
;
1781 if (d
->chr_can_read
[chr
->focus
])
1782 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
1786 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
1788 CharDriverState
*chr
= opaque
;
1789 MuxDriver
*d
= chr
->opaque
;
1791 for(i
= 0; i
< size
; i
++)
1792 if (mux_proc_byte(chr
, d
, buf
[i
]))
1793 d
->chr_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
], &buf
[i
], 1);
1796 static void mux_chr_event(void *opaque
, int event
)
1798 CharDriverState
*chr
= opaque
;
1799 MuxDriver
*d
= chr
->opaque
;
1802 /* Send the event to all registered listeners */
1803 for (i
= 0; i
< d
->mux_cnt
; i
++)
1804 if (d
->chr_event
[i
])
1805 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
1808 static void mux_chr_update_read_handler(CharDriverState
*chr
)
1810 MuxDriver
*d
= chr
->opaque
;
1812 if (d
->mux_cnt
>= MAX_MUX
) {
1813 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
1816 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
1817 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
1818 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
1819 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
1820 /* Fix up the real driver with mux routines */
1821 if (d
->mux_cnt
== 0) {
1822 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
1823 mux_chr_event
, chr
);
1825 chr
->focus
= d
->mux_cnt
;
1829 CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
1831 CharDriverState
*chr
;
1834 chr
= qemu_mallocz(sizeof(CharDriverState
));
1837 d
= qemu_mallocz(sizeof(MuxDriver
));
1846 chr
->chr_write
= mux_chr_write
;
1847 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
1854 static void socket_cleanup(void)
1859 static int socket_init(void)
1864 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1866 err
= WSAGetLastError();
1867 fprintf(stderr
, "WSAStartup: %d\n", err
);
1870 atexit(socket_cleanup
);
1874 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1880 ret
= send(fd
, buf
, len
, 0);
1883 errno
= WSAGetLastError();
1884 if (errno
!= WSAEWOULDBLOCK
) {
1887 } else if (ret
== 0) {
1897 void socket_set_nonblock(int fd
)
1899 unsigned long opt
= 1;
1900 ioctlsocket(fd
, FIONBIO
, &opt
);
1905 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1911 ret
= write(fd
, buf
, len
);
1913 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1915 } else if (ret
== 0) {
1925 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1927 return unix_write(fd
, buf
, len1
);
1930 void socket_set_nonblock(int fd
)
1932 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1934 #endif /* !_WIN32 */
1943 #define STDIO_MAX_CLIENTS 1
1944 static int stdio_nb_clients
= 0;
1946 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1948 FDCharDriver
*s
= chr
->opaque
;
1949 return unix_write(s
->fd_out
, buf
, len
);
1952 static int fd_chr_read_poll(void *opaque
)
1954 CharDriverState
*chr
= opaque
;
1955 FDCharDriver
*s
= chr
->opaque
;
1957 s
->max_size
= qemu_chr_can_read(chr
);
1961 static void fd_chr_read(void *opaque
)
1963 CharDriverState
*chr
= opaque
;
1964 FDCharDriver
*s
= chr
->opaque
;
1969 if (len
> s
->max_size
)
1973 size
= read(s
->fd_in
, buf
, len
);
1975 /* FD has been closed. Remove it from the active list. */
1976 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1980 qemu_chr_read(chr
, buf
, size
);
1984 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1986 FDCharDriver
*s
= chr
->opaque
;
1988 if (s
->fd_in
>= 0) {
1989 if (nographic
&& s
->fd_in
== 0) {
1991 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1992 fd_chr_read
, NULL
, chr
);
1997 /* open a character device to a unix fd */
1998 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
2000 CharDriverState
*chr
;
2003 chr
= qemu_mallocz(sizeof(CharDriverState
));
2006 s
= qemu_mallocz(sizeof(FDCharDriver
));
2014 chr
->chr_write
= fd_chr_write
;
2015 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
2017 qemu_chr_reset(chr
);
2022 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
2026 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
2029 return qemu_chr_open_fd(-1, fd_out
);
2032 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
2035 char filename_in
[256], filename_out
[256];
2037 snprintf(filename_in
, 256, "%s.in", filename
);
2038 snprintf(filename_out
, 256, "%s.out", filename
);
2039 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
2040 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
2041 if (fd_in
< 0 || fd_out
< 0) {
2046 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
2050 return qemu_chr_open_fd(fd_in
, fd_out
);
2054 /* for STDIO, we handle the case where several clients use it
2057 #define TERM_FIFO_MAX_SIZE 1
2059 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
2060 static int term_fifo_size
;
2062 static int stdio_read_poll(void *opaque
)
2064 CharDriverState
*chr
= opaque
;
2066 /* try to flush the queue if needed */
2067 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
2068 qemu_chr_read(chr
, term_fifo
, 1);
2071 /* see if we can absorb more chars */
2072 if (term_fifo_size
== 0)
2078 static void stdio_read(void *opaque
)
2082 CharDriverState
*chr
= opaque
;
2084 size
= read(0, buf
, 1);
2086 /* stdin has been closed. Remove it from the active list. */
2087 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
2091 if (qemu_chr_can_read(chr
) > 0) {
2092 qemu_chr_read(chr
, buf
, 1);
2093 } else if (term_fifo_size
== 0) {
2094 term_fifo
[term_fifo_size
++] = buf
[0];
2099 /* init terminal so that we can grab keys */
2100 static struct termios oldtty
;
2101 static int old_fd0_flags
;
2103 static void term_exit(void)
2105 tcsetattr (0, TCSANOW
, &oldtty
);
2106 fcntl(0, F_SETFL
, old_fd0_flags
);
2109 static void term_init(void)
2113 tcgetattr (0, &tty
);
2115 old_fd0_flags
= fcntl(0, F_GETFL
);
2117 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
2118 |INLCR
|IGNCR
|ICRNL
|IXON
);
2119 tty
.c_oflag
|= OPOST
;
2120 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
2121 /* if graphical mode, we allow Ctrl-C handling */
2123 tty
.c_lflag
&= ~ISIG
;
2124 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
2127 tty
.c_cc
[VTIME
] = 0;
2129 tcsetattr (0, TCSANOW
, &tty
);
2133 fcntl(0, F_SETFL
, O_NONBLOCK
);
2136 static CharDriverState
*qemu_chr_open_stdio(void)
2138 CharDriverState
*chr
;
2140 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
2142 chr
= qemu_chr_open_fd(0, 1);
2143 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
2150 #if defined(__linux__) || defined(__sun__)
2151 static CharDriverState
*qemu_chr_open_pty(void)
2154 char slave_name
[1024];
2155 int master_fd
, slave_fd
;
2157 #if defined(__linux__)
2158 /* Not satisfying */
2159 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
2164 /* Disabling local echo and line-buffered output */
2165 tcgetattr (master_fd
, &tty
);
2166 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
2168 tty
.c_cc
[VTIME
] = 0;
2169 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
2171 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
2172 return qemu_chr_open_fd(master_fd
, master_fd
);
2175 static void tty_serial_init(int fd
, int speed
,
2176 int parity
, int data_bits
, int stop_bits
)
2182 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
2183 speed
, parity
, data_bits
, stop_bits
);
2185 tcgetattr (fd
, &tty
);
2227 cfsetispeed(&tty
, spd
);
2228 cfsetospeed(&tty
, spd
);
2230 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
2231 |INLCR
|IGNCR
|ICRNL
|IXON
);
2232 tty
.c_oflag
|= OPOST
;
2233 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
2234 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
2255 tty
.c_cflag
|= PARENB
;
2258 tty
.c_cflag
|= PARENB
| PARODD
;
2262 tty
.c_cflag
|= CSTOPB
;
2264 tcsetattr (fd
, TCSANOW
, &tty
);
2267 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2269 FDCharDriver
*s
= chr
->opaque
;
2272 case CHR_IOCTL_SERIAL_SET_PARAMS
:
2274 QEMUSerialSetParams
*ssp
= arg
;
2275 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
2276 ssp
->data_bits
, ssp
->stop_bits
);
2279 case CHR_IOCTL_SERIAL_SET_BREAK
:
2281 int enable
= *(int *)arg
;
2283 tcsendbreak(s
->fd_in
, 1);
2292 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
2294 CharDriverState
*chr
;
2297 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
2298 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2299 tty_serial_init(fd
, 115200, 'N', 8, 1);
2300 chr
= qemu_chr_open_fd(fd
, fd
);
2305 chr
->chr_ioctl
= tty_serial_ioctl
;
2306 qemu_chr_reset(chr
);
2309 #else /* ! __linux__ && ! __sun__ */
2310 static CharDriverState
*qemu_chr_open_pty(void)
2314 #endif /* __linux__ || __sun__ */
2316 #if defined(__linux__)
2320 } ParallelCharDriver
;
2322 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
2324 if (s
->mode
!= mode
) {
2326 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
2333 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2335 ParallelCharDriver
*drv
= chr
->opaque
;
2340 case CHR_IOCTL_PP_READ_DATA
:
2341 if (ioctl(fd
, PPRDATA
, &b
) < 0)
2343 *(uint8_t *)arg
= b
;
2345 case CHR_IOCTL_PP_WRITE_DATA
:
2346 b
= *(uint8_t *)arg
;
2347 if (ioctl(fd
, PPWDATA
, &b
) < 0)
2350 case CHR_IOCTL_PP_READ_CONTROL
:
2351 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
2353 /* Linux gives only the lowest bits, and no way to know data
2354 direction! For better compatibility set the fixed upper
2356 *(uint8_t *)arg
= b
| 0xc0;
2358 case CHR_IOCTL_PP_WRITE_CONTROL
:
2359 b
= *(uint8_t *)arg
;
2360 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
2363 case CHR_IOCTL_PP_READ_STATUS
:
2364 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
2366 *(uint8_t *)arg
= b
;
2368 case CHR_IOCTL_PP_EPP_READ_ADDR
:
2369 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2370 struct ParallelIOArg
*parg
= arg
;
2371 int n
= read(fd
, parg
->buffer
, parg
->count
);
2372 if (n
!= parg
->count
) {
2377 case CHR_IOCTL_PP_EPP_READ
:
2378 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2379 struct ParallelIOArg
*parg
= arg
;
2380 int n
= read(fd
, parg
->buffer
, parg
->count
);
2381 if (n
!= parg
->count
) {
2386 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
2387 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2388 struct ParallelIOArg
*parg
= arg
;
2389 int n
= write(fd
, parg
->buffer
, parg
->count
);
2390 if (n
!= parg
->count
) {
2395 case CHR_IOCTL_PP_EPP_WRITE
:
2396 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2397 struct ParallelIOArg
*parg
= arg
;
2398 int n
= write(fd
, parg
->buffer
, parg
->count
);
2399 if (n
!= parg
->count
) {
2410 static void pp_close(CharDriverState
*chr
)
2412 ParallelCharDriver
*drv
= chr
->opaque
;
2415 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
2416 ioctl(fd
, PPRELEASE
);
2421 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
2423 CharDriverState
*chr
;
2424 ParallelCharDriver
*drv
;
2427 TFR(fd
= open(filename
, O_RDWR
));
2431 if (ioctl(fd
, PPCLAIM
) < 0) {
2436 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
2442 drv
->mode
= IEEE1284_MODE_COMPAT
;
2444 chr
= qemu_mallocz(sizeof(CharDriverState
));
2450 chr
->chr_write
= null_chr_write
;
2451 chr
->chr_ioctl
= pp_ioctl
;
2452 chr
->chr_close
= pp_close
;
2455 qemu_chr_reset(chr
);
2459 #endif /* __linux__ */
2465 HANDLE hcom
, hrecv
, hsend
;
2466 OVERLAPPED orecv
, osend
;
2471 #define NSENDBUF 2048
2472 #define NRECVBUF 2048
2473 #define MAXCONNECT 1
2474 #define NTIMEOUT 5000
2476 static int win_chr_poll(void *opaque
);
2477 static int win_chr_pipe_poll(void *opaque
);
2479 static void win_chr_close(CharDriverState
*chr
)
2481 WinCharState
*s
= chr
->opaque
;
2484 CloseHandle(s
->hsend
);
2488 CloseHandle(s
->hrecv
);
2492 CloseHandle(s
->hcom
);
2496 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
2498 qemu_del_polling_cb(win_chr_poll
, chr
);
2501 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
2503 WinCharState
*s
= chr
->opaque
;
2505 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2510 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2512 fprintf(stderr
, "Failed CreateEvent\n");
2515 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2517 fprintf(stderr
, "Failed CreateEvent\n");
2521 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2522 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2523 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2524 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
2529 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2530 fprintf(stderr
, "Failed SetupComm\n");
2534 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2535 size
= sizeof(COMMCONFIG
);
2536 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2537 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2538 CommConfigDialog(filename
, NULL
, &comcfg
);
2540 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2541 fprintf(stderr
, "Failed SetCommState\n");
2545 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2546 fprintf(stderr
, "Failed SetCommMask\n");
2550 cto
.ReadIntervalTimeout
= MAXDWORD
;
2551 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2552 fprintf(stderr
, "Failed SetCommTimeouts\n");
2556 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2557 fprintf(stderr
, "Failed ClearCommError\n");
2560 qemu_add_polling_cb(win_chr_poll
, chr
);
2568 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2570 WinCharState
*s
= chr
->opaque
;
2571 DWORD len
, ret
, size
, err
;
2574 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2575 s
->osend
.hEvent
= s
->hsend
;
2578 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2580 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2582 err
= GetLastError();
2583 if (err
== ERROR_IO_PENDING
) {
2584 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2602 static int win_chr_read_poll(CharDriverState
*chr
)
2604 WinCharState
*s
= chr
->opaque
;
2606 s
->max_size
= qemu_chr_can_read(chr
);
2610 static void win_chr_readfile(CharDriverState
*chr
)
2612 WinCharState
*s
= chr
->opaque
;
2617 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2618 s
->orecv
.hEvent
= s
->hrecv
;
2619 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2621 err
= GetLastError();
2622 if (err
== ERROR_IO_PENDING
) {
2623 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2628 qemu_chr_read(chr
, buf
, size
);
2632 static void win_chr_read(CharDriverState
*chr
)
2634 WinCharState
*s
= chr
->opaque
;
2636 if (s
->len
> s
->max_size
)
2637 s
->len
= s
->max_size
;
2641 win_chr_readfile(chr
);
2644 static int win_chr_poll(void *opaque
)
2646 CharDriverState
*chr
= opaque
;
2647 WinCharState
*s
= chr
->opaque
;
2651 ClearCommError(s
->hcom
, &comerr
, &status
);
2652 if (status
.cbInQue
> 0) {
2653 s
->len
= status
.cbInQue
;
2654 win_chr_read_poll(chr
);
2661 static CharDriverState
*qemu_chr_open_win(const char *filename
)
2663 CharDriverState
*chr
;
2666 chr
= qemu_mallocz(sizeof(CharDriverState
));
2669 s
= qemu_mallocz(sizeof(WinCharState
));
2675 chr
->chr_write
= win_chr_write
;
2676 chr
->chr_close
= win_chr_close
;
2678 if (win_chr_init(chr
, filename
) < 0) {
2683 qemu_chr_reset(chr
);
2687 static int win_chr_pipe_poll(void *opaque
)
2689 CharDriverState
*chr
= opaque
;
2690 WinCharState
*s
= chr
->opaque
;
2693 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2696 win_chr_read_poll(chr
);
2703 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
2705 WinCharState
*s
= chr
->opaque
;
2713 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2715 fprintf(stderr
, "Failed CreateEvent\n");
2718 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2720 fprintf(stderr
, "Failed CreateEvent\n");
2724 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2725 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2726 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2728 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2729 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2730 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2735 ZeroMemory(&ov
, sizeof(ov
));
2736 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2737 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2739 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2743 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2745 fprintf(stderr
, "Failed GetOverlappedResult\n");
2747 CloseHandle(ov
.hEvent
);
2754 CloseHandle(ov
.hEvent
);
2757 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2766 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2768 CharDriverState
*chr
;
2771 chr
= qemu_mallocz(sizeof(CharDriverState
));
2774 s
= qemu_mallocz(sizeof(WinCharState
));
2780 chr
->chr_write
= win_chr_write
;
2781 chr
->chr_close
= win_chr_close
;
2783 if (win_chr_pipe_init(chr
, filename
) < 0) {
2788 qemu_chr_reset(chr
);
2792 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2794 CharDriverState
*chr
;
2797 chr
= qemu_mallocz(sizeof(CharDriverState
));
2800 s
= qemu_mallocz(sizeof(WinCharState
));
2807 chr
->chr_write
= win_chr_write
;
2808 qemu_chr_reset(chr
);
2812 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
2814 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2817 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2821 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2822 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2823 if (fd_out
== INVALID_HANDLE_VALUE
)
2826 return qemu_chr_open_win_file(fd_out
);
2828 #endif /* !_WIN32 */
2830 /***********************************************************/
2831 /* UDP Net console */
2835 struct sockaddr_in daddr
;
2842 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2844 NetCharDriver
*s
= chr
->opaque
;
2846 return sendto(s
->fd
, buf
, len
, 0,
2847 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2850 static int udp_chr_read_poll(void *opaque
)
2852 CharDriverState
*chr
= opaque
;
2853 NetCharDriver
*s
= chr
->opaque
;
2855 s
->max_size
= qemu_chr_can_read(chr
);
2857 /* If there were any stray characters in the queue process them
2860 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2861 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2863 s
->max_size
= qemu_chr_can_read(chr
);
2868 static void udp_chr_read(void *opaque
)
2870 CharDriverState
*chr
= opaque
;
2871 NetCharDriver
*s
= chr
->opaque
;
2873 if (s
->max_size
== 0)
2875 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2876 s
->bufptr
= s
->bufcnt
;
2881 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2882 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2884 s
->max_size
= qemu_chr_can_read(chr
);
2888 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2890 NetCharDriver
*s
= chr
->opaque
;
2893 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2894 udp_chr_read
, NULL
, chr
);
2898 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2900 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2902 int parse_host_src_port(struct sockaddr_in
*haddr
,
2903 struct sockaddr_in
*saddr
,
2906 static CharDriverState
*qemu_chr_open_udp(const char *def
)
2908 CharDriverState
*chr
= NULL
;
2909 NetCharDriver
*s
= NULL
;
2911 struct sockaddr_in saddr
;
2913 chr
= qemu_mallocz(sizeof(CharDriverState
));
2916 s
= qemu_mallocz(sizeof(NetCharDriver
));
2920 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2922 perror("socket(PF_INET, SOCK_DGRAM)");
2926 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2927 printf("Could not parse: %s\n", def
);
2931 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2941 chr
->chr_write
= udp_chr_write
;
2942 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2955 /***********************************************************/
2956 /* TCP Net console */
2967 static void tcp_chr_accept(void *opaque
);
2969 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2971 TCPCharDriver
*s
= chr
->opaque
;
2973 return send_all(s
->fd
, buf
, len
);
2975 /* XXX: indicate an error ? */
2980 static int tcp_chr_read_poll(void *opaque
)
2982 CharDriverState
*chr
= opaque
;
2983 TCPCharDriver
*s
= chr
->opaque
;
2986 s
->max_size
= qemu_chr_can_read(chr
);
2991 #define IAC_BREAK 243
2992 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2994 char *buf
, int *size
)
2996 /* Handle any telnet client's basic IAC options to satisfy char by
2997 * char mode with no echo. All IAC options will be removed from
2998 * the buf and the do_telnetopt variable will be used to track the
2999 * state of the width of the IAC information.
3001 * IAC commands come in sets of 3 bytes with the exception of the
3002 * "IAC BREAK" command and the double IAC.
3008 for (i
= 0; i
< *size
; i
++) {
3009 if (s
->do_telnetopt
> 1) {
3010 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
3011 /* Double IAC means send an IAC */
3015 s
->do_telnetopt
= 1;
3017 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
3018 /* Handle IAC break commands by sending a serial break */
3019 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
3024 if (s
->do_telnetopt
>= 4) {
3025 s
->do_telnetopt
= 1;
3028 if ((unsigned char)buf
[i
] == IAC
) {
3029 s
->do_telnetopt
= 2;
3040 static void tcp_chr_read(void *opaque
)
3042 CharDriverState
*chr
= opaque
;
3043 TCPCharDriver
*s
= chr
->opaque
;
3047 if (!s
->connected
|| s
->max_size
<= 0)
3050 if (len
> s
->max_size
)
3052 size
= recv(s
->fd
, buf
, len
, 0);
3054 /* connection closed */
3056 if (s
->listen_fd
>= 0) {
3057 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
3059 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3062 } else if (size
> 0) {
3063 if (s
->do_telnetopt
)
3064 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
3066 qemu_chr_read(chr
, buf
, size
);
3070 static void tcp_chr_connect(void *opaque
)
3072 CharDriverState
*chr
= opaque
;
3073 TCPCharDriver
*s
= chr
->opaque
;
3076 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
3077 tcp_chr_read
, NULL
, chr
);
3078 qemu_chr_reset(chr
);
3081 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3082 static void tcp_chr_telnet_init(int fd
)
3085 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3086 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3087 send(fd
, (char *)buf
, 3, 0);
3088 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3089 send(fd
, (char *)buf
, 3, 0);
3090 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3091 send(fd
, (char *)buf
, 3, 0);
3092 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3093 send(fd
, (char *)buf
, 3, 0);
3096 static void socket_set_nodelay(int fd
)
3099 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
3102 static void tcp_chr_accept(void *opaque
)
3104 CharDriverState
*chr
= opaque
;
3105 TCPCharDriver
*s
= chr
->opaque
;
3106 struct sockaddr_in saddr
;
3108 struct sockaddr_un uaddr
;
3110 struct sockaddr
*addr
;
3117 len
= sizeof(uaddr
);
3118 addr
= (struct sockaddr
*)&uaddr
;
3122 len
= sizeof(saddr
);
3123 addr
= (struct sockaddr
*)&saddr
;
3125 fd
= accept(s
->listen_fd
, addr
, &len
);
3126 if (fd
< 0 && errno
!= EINTR
) {
3128 } else if (fd
>= 0) {
3129 if (s
->do_telnetopt
)
3130 tcp_chr_telnet_init(fd
);
3134 socket_set_nonblock(fd
);
3136 socket_set_nodelay(fd
);
3138 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
3139 tcp_chr_connect(chr
);
3142 static void tcp_chr_close(CharDriverState
*chr
)
3144 TCPCharDriver
*s
= chr
->opaque
;
3147 if (s
->listen_fd
>= 0)
3148 closesocket(s
->listen_fd
);
3152 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
3156 CharDriverState
*chr
= NULL
;
3157 TCPCharDriver
*s
= NULL
;
3158 int fd
= -1, ret
, err
, val
;
3160 int is_waitconnect
= 1;
3163 struct sockaddr_in saddr
;
3165 struct sockaddr_un uaddr
;
3167 struct sockaddr
*addr
;
3172 addr
= (struct sockaddr
*)&uaddr
;
3173 addrlen
= sizeof(uaddr
);
3174 if (parse_unix_path(&uaddr
, host_str
) < 0)
3179 addr
= (struct sockaddr
*)&saddr
;
3180 addrlen
= sizeof(saddr
);
3181 if (parse_host_port(&saddr
, host_str
) < 0)
3186 while((ptr
= strchr(ptr
,','))) {
3188 if (!strncmp(ptr
,"server",6)) {
3190 } else if (!strncmp(ptr
,"nowait",6)) {
3192 } else if (!strncmp(ptr
,"nodelay",6)) {
3195 printf("Unknown option: %s\n", ptr
);
3202 chr
= qemu_mallocz(sizeof(CharDriverState
));
3205 s
= qemu_mallocz(sizeof(TCPCharDriver
));
3211 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
3214 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3219 if (!is_waitconnect
)
3220 socket_set_nonblock(fd
);
3225 s
->is_unix
= is_unix
;
3226 s
->do_nodelay
= do_nodelay
&& !is_unix
;
3229 chr
->chr_write
= tcp_chr_write
;
3230 chr
->chr_close
= tcp_chr_close
;
3233 /* allow fast reuse */
3237 strncpy(path
, uaddr
.sun_path
, 108);
3244 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3247 ret
= bind(fd
, addr
, addrlen
);
3251 ret
= listen(fd
, 0);
3256 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
3258 s
->do_telnetopt
= 1;
3261 ret
= connect(fd
, addr
, addrlen
);
3263 err
= socket_error();
3264 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3265 } else if (err
== EINPROGRESS
) {
3268 } else if (err
== WSAEALREADY
) {
3280 socket_set_nodelay(fd
);
3282 tcp_chr_connect(chr
);
3284 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
3287 if (is_listen
&& is_waitconnect
) {
3288 printf("QEMU waiting for connection on: %s\n", host_str
);
3289 tcp_chr_accept(chr
);
3290 socket_set_nonblock(s
->listen_fd
);
3302 CharDriverState
*qemu_chr_open(const char *filename
)
3306 if (!strcmp(filename
, "vc")) {
3307 return text_console_init(&display_state
, 0);
3308 } else if (strstart(filename
, "vc:", &p
)) {
3309 return text_console_init(&display_state
, p
);
3310 } else if (!strcmp(filename
, "null")) {
3311 return qemu_chr_open_null();
3313 if (strstart(filename
, "tcp:", &p
)) {
3314 return qemu_chr_open_tcp(p
, 0, 0);
3316 if (strstart(filename
, "telnet:", &p
)) {
3317 return qemu_chr_open_tcp(p
, 1, 0);
3319 if (strstart(filename
, "udp:", &p
)) {
3320 return qemu_chr_open_udp(p
);
3322 if (strstart(filename
, "mon:", &p
)) {
3323 CharDriverState
*drv
= qemu_chr_open(p
);
3325 drv
= qemu_chr_open_mux(drv
);
3326 monitor_init(drv
, !nographic
);
3329 printf("Unable to open driver: %s\n", p
);
3333 if (strstart(filename
, "unix:", &p
)) {
3334 return qemu_chr_open_tcp(p
, 0, 1);
3335 } else if (strstart(filename
, "file:", &p
)) {
3336 return qemu_chr_open_file_out(p
);
3337 } else if (strstart(filename
, "pipe:", &p
)) {
3338 return qemu_chr_open_pipe(p
);
3339 } else if (!strcmp(filename
, "pty")) {
3340 return qemu_chr_open_pty();
3341 } else if (!strcmp(filename
, "stdio")) {
3342 return qemu_chr_open_stdio();
3344 #if defined(__linux__)
3345 if (strstart(filename
, "/dev/parport", NULL
)) {
3346 return qemu_chr_open_pp(filename
);
3349 #if defined(__linux__) || defined(__sun__)
3350 if (strstart(filename
, "/dev/", NULL
)) {
3351 return qemu_chr_open_tty(filename
);
3355 if (strstart(filename
, "COM", NULL
)) {
3356 return qemu_chr_open_win(filename
);
3358 if (strstart(filename
, "pipe:", &p
)) {
3359 return qemu_chr_open_win_pipe(p
);
3361 if (strstart(filename
, "con:", NULL
)) {
3362 return qemu_chr_open_win_con(filename
);
3364 if (strstart(filename
, "file:", &p
)) {
3365 return qemu_chr_open_win_file_out(p
);
3373 void qemu_chr_close(CharDriverState
*chr
)
3376 chr
->chr_close(chr
);
3379 /***********************************************************/
3380 /* network device redirectors */
3382 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
3386 for(i
=0;i
<size
;i
+=16) {
3390 fprintf(f
, "%08x ", i
);
3393 fprintf(f
, " %02x", buf
[i
+j
]);
3398 for(j
=0;j
<len
;j
++) {
3400 if (c
< ' ' || c
> '~')
3402 fprintf(f
, "%c", c
);
3408 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
3411 for(i
= 0; i
< 6; i
++) {
3412 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3425 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
3430 p1
= strchr(p
, sep
);
3436 if (len
> buf_size
- 1)
3438 memcpy(buf
, p
, len
);
3445 int parse_host_src_port(struct sockaddr_in
*haddr
,
3446 struct sockaddr_in
*saddr
,
3447 const char *input_str
)
3449 char *str
= strdup(input_str
);
3450 char *host_str
= str
;
3455 * Chop off any extra arguments at the end of the string which
3456 * would start with a comma, then fill in the src port information
3457 * if it was provided else use the "any address" and "any port".
3459 if ((ptr
= strchr(str
,',')))
3462 if ((src_str
= strchr(input_str
,'@'))) {
3467 if (parse_host_port(haddr
, host_str
) < 0)
3470 if (!src_str
|| *src_str
== '\0')
3473 if (parse_host_port(saddr
, src_str
) < 0)
3484 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
3492 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3494 saddr
->sin_family
= AF_INET
;
3495 if (buf
[0] == '\0') {
3496 saddr
->sin_addr
.s_addr
= 0;
3498 if (isdigit(buf
[0])) {
3499 if (!inet_aton(buf
, &saddr
->sin_addr
))
3502 if ((he
= gethostbyname(buf
)) == NULL
)
3504 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
3507 port
= strtol(p
, (char **)&r
, 0);
3510 saddr
->sin_port
= htons(port
);
3515 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
3520 len
= MIN(108, strlen(str
));
3521 p
= strchr(str
, ',');
3523 len
= MIN(len
, p
- str
);
3525 memset(uaddr
, 0, sizeof(*uaddr
));
3527 uaddr
->sun_family
= AF_UNIX
;
3528 memcpy(uaddr
->sun_path
, str
, len
);
3534 /* find or alloc a new VLAN */
3535 VLANState
*qemu_find_vlan(int id
)
3537 VLANState
**pvlan
, *vlan
;
3538 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3542 vlan
= qemu_mallocz(sizeof(VLANState
));
3547 pvlan
= &first_vlan
;
3548 while (*pvlan
!= NULL
)
3549 pvlan
= &(*pvlan
)->next
;
3554 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
3555 IOReadHandler
*fd_read
,
3556 IOCanRWHandler
*fd_can_read
,
3559 VLANClientState
*vc
, **pvc
;
3560 vc
= qemu_mallocz(sizeof(VLANClientState
));
3563 vc
->fd_read
= fd_read
;
3564 vc
->fd_can_read
= fd_can_read
;
3565 vc
->opaque
= opaque
;
3569 pvc
= &vlan
->first_client
;
3570 while (*pvc
!= NULL
)
3571 pvc
= &(*pvc
)->next
;
3576 int qemu_can_send_packet(VLANClientState
*vc1
)
3578 VLANState
*vlan
= vc1
->vlan
;
3579 VLANClientState
*vc
;
3581 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3583 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
3590 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
3592 VLANState
*vlan
= vc1
->vlan
;
3593 VLANClientState
*vc
;
3596 printf("vlan %d send:\n", vlan
->id
);
3597 hex_dump(stdout
, buf
, size
);
3599 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3601 vc
->fd_read(vc
->opaque
, buf
, size
);
3606 #if defined(CONFIG_SLIRP)
3608 /* slirp network adapter */
3610 static int slirp_inited
;
3611 static VLANClientState
*slirp_vc
;
3613 int slirp_can_output(void)
3615 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
3618 void slirp_output(const uint8_t *pkt
, int pkt_len
)
3621 printf("slirp output:\n");
3622 hex_dump(stdout
, pkt
, pkt_len
);
3626 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
3629 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
3632 printf("slirp input:\n");
3633 hex_dump(stdout
, buf
, size
);
3635 slirp_input(buf
, size
);
3638 static int net_slirp_init(VLANState
*vlan
)
3640 if (!slirp_inited
) {
3644 slirp_vc
= qemu_new_vlan_client(vlan
,
3645 slirp_receive
, NULL
, NULL
);
3646 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
3650 static void net_slirp_redir(const char *redir_str
)
3655 struct in_addr guest_addr
;
3656 int host_port
, guest_port
;
3658 if (!slirp_inited
) {
3664 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3666 if (!strcmp(buf
, "tcp")) {
3668 } else if (!strcmp(buf
, "udp")) {
3674 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3676 host_port
= strtol(buf
, &r
, 0);
3680 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3682 if (buf
[0] == '\0') {
3683 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
3685 if (!inet_aton(buf
, &guest_addr
))
3688 guest_port
= strtol(p
, &r
, 0);
3692 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3693 fprintf(stderr
, "qemu: could not set up redirection\n");
3698 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3706 static void smb_exit(void)
3710 char filename
[1024];
3712 /* erase all the files in the directory */
3713 d
= opendir(smb_dir
);
3718 if (strcmp(de
->d_name
, ".") != 0 &&
3719 strcmp(de
->d_name
, "..") != 0) {
3720 snprintf(filename
, sizeof(filename
), "%s/%s",
3721 smb_dir
, de
->d_name
);
3729 /* automatic user mode samba server configuration */
3730 void net_slirp_smb(const char *exported_dir
)
3732 char smb_conf
[1024];
3733 char smb_cmdline
[1024];
3736 if (!slirp_inited
) {
3741 /* XXX: better tmp dir construction */
3742 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3743 if (mkdir(smb_dir
, 0700) < 0) {
3744 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3747 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3749 f
= fopen(smb_conf
, "w");
3751 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3758 "socket address=127.0.0.1\n"
3759 "pid directory=%s\n"
3760 "lock directory=%s\n"
3761 "log file=%s/log.smbd\n"
3762 "smb passwd file=%s/smbpasswd\n"
3763 "security = share\n"
3778 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3779 SMBD_COMMAND
, smb_conf
);
3781 slirp_add_exec(0, smb_cmdline
, 4, 139);
3784 #endif /* !defined(_WIN32) */
3786 #endif /* CONFIG_SLIRP */
3788 #if !defined(_WIN32)
3790 typedef struct TAPState
{
3791 VLANClientState
*vc
;
3793 char down_script
[1024];
3796 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3798 TAPState
*s
= opaque
;
3801 ret
= write(s
->fd
, buf
, size
);
3802 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3809 static void tap_send(void *opaque
)
3811 TAPState
*s
= opaque
;
3818 sbuf
.maxlen
= sizeof(buf
);
3820 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
3822 size
= read(s
->fd
, buf
, sizeof(buf
));
3825 qemu_send_packet(s
->vc
, buf
, size
);
3831 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3835 s
= qemu_mallocz(sizeof(TAPState
));
3839 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3840 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3841 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3845 #if defined (_BSD) || defined (__FreeBSD_kernel__)
3846 static int tap_open(char *ifname
, int ifname_size
)
3852 TFR(fd
= open("/dev/tap", O_RDWR
));
3854 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3859 dev
= devname(s
.st_rdev
, S_IFCHR
);
3860 pstrcpy(ifname
, ifname_size
, dev
);
3862 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3865 #elif defined(__sun__)
3866 #define TUNNEWPPA (('T'<<16) | 0x0001)
3868 * Allocate TAP device, returns opened fd.
3869 * Stores dev name in the first arg(must be large enough).
3871 int tap_alloc(char *dev
)
3873 int tap_fd
, if_fd
, ppa
= -1;
3874 static int ip_fd
= 0;
3877 static int arp_fd
= 0;
3878 int ip_muxid
, arp_muxid
;
3879 struct strioctl strioc_if
, strioc_ppa
;
3880 int link_type
= I_PLINK
;;
3882 char actual_name
[32] = "";
3884 memset(&ifr
, 0x0, sizeof(ifr
));
3888 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
3892 /* Check if IP device was opened */
3896 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
3898 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
3902 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
3904 syslog(LOG_ERR
, "Can't open /dev/tap");
3908 /* Assign a new PPA and get its unit number. */
3909 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
3910 strioc_ppa
.ic_timout
= 0;
3911 strioc_ppa
.ic_len
= sizeof(ppa
);
3912 strioc_ppa
.ic_dp
= (char *)&ppa
;
3913 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
3914 syslog (LOG_ERR
, "Can't assign new interface");
3916 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
3918 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
3921 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
3922 syslog(LOG_ERR
, "Can't push IP module");
3926 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
3927 syslog(LOG_ERR
, "Can't get flags\n");
3929 snprintf (actual_name
, 32, "tap%d", ppa
);
3930 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3933 /* Assign ppa according to the unit number returned by tun device */
3935 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
3936 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
3937 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
3938 syslog (LOG_ERR
, "Can't get flags\n");
3939 /* Push arp module to if_fd */
3940 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
3941 syslog (LOG_ERR
, "Can't push ARP module (2)");
3943 /* Push arp module to ip_fd */
3944 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
3945 syslog (LOG_ERR
, "I_POP failed\n");
3946 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
3947 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
3949 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
3951 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
3953 /* Set ifname to arp */
3954 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
3955 strioc_if
.ic_timout
= 0;
3956 strioc_if
.ic_len
= sizeof(ifr
);
3957 strioc_if
.ic_dp
= (char *)&ifr
;
3958 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
3959 syslog (LOG_ERR
, "Can't set ifname to arp\n");
3962 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
3963 syslog(LOG_ERR
, "Can't link TAP device to IP");
3967 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
3968 syslog (LOG_ERR
, "Can't link TAP device to ARP");
3972 memset(&ifr
, 0x0, sizeof(ifr
));
3973 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3974 ifr
.lifr_ip_muxid
= ip_muxid
;
3975 ifr
.lifr_arp_muxid
= arp_muxid
;
3977 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
3979 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
3980 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
3981 syslog (LOG_ERR
, "Can't set multiplexor id");
3984 sprintf(dev
, "tap%d", ppa
);
3988 static int tap_open(char *ifname
, int ifname_size
)
3992 if( (fd
= tap_alloc(dev
)) < 0 ){
3993 fprintf(stderr
, "Cannot allocate TAP device\n");
3996 pstrcpy(ifname
, ifname_size
, dev
);
3997 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
4001 static int tap_open(char *ifname
, int ifname_size
)
4006 TFR(fd
= open("/dev/net/tun", O_RDWR
));
4008 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
4011 memset(&ifr
, 0, sizeof(ifr
));
4012 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
4013 if (ifname
[0] != '\0')
4014 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
4016 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
4017 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
4019 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
4023 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
4024 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
4029 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
4035 /* try to launch network script */
4039 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
4040 for (i
= 0; i
< open_max
; i
++)
4041 if (i
!= STDIN_FILENO
&&
4042 i
!= STDOUT_FILENO
&&
4043 i
!= STDERR_FILENO
&&
4048 *parg
++ = (char *)setup_script
;
4049 *parg
++ = (char *)ifname
;
4051 execv(setup_script
, args
);
4054 while (waitpid(pid
, &status
, 0) != pid
);
4055 if (!WIFEXITED(status
) ||
4056 WEXITSTATUS(status
) != 0) {
4057 fprintf(stderr
, "%s: could not launch network script\n",
4065 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
4066 const char *setup_script
, const char *down_script
)
4072 if (ifname1
!= NULL
)
4073 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
4076 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
4080 if (!setup_script
|| !strcmp(setup_script
, "no"))
4082 if (setup_script
[0] != '\0') {
4083 if (launch_script(setup_script
, ifname
, fd
))
4086 s
= net_tap_fd_init(vlan
, fd
);
4089 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4090 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
4091 if (down_script
&& strcmp(down_script
, "no"))
4092 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
4096 #endif /* !_WIN32 */
4098 /* network connection */
4099 typedef struct NetSocketState
{
4100 VLANClientState
*vc
;
4102 int state
; /* 0 = getting length, 1 = getting data */
4106 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
4109 typedef struct NetSocketListenState
{
4112 } NetSocketListenState
;
4114 /* XXX: we consider we can send the whole packet without blocking */
4115 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
4117 NetSocketState
*s
= opaque
;
4121 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
4122 send_all(s
->fd
, buf
, size
);
4125 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
4127 NetSocketState
*s
= opaque
;
4128 sendto(s
->fd
, buf
, size
, 0,
4129 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
4132 static void net_socket_send(void *opaque
)
4134 NetSocketState
*s
= opaque
;
4139 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
4141 err
= socket_error();
4142 if (err
!= EWOULDBLOCK
)
4144 } else if (size
== 0) {
4145 /* end of connection */
4147 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
4153 /* reassemble a packet from the network */
4159 memcpy(s
->buf
+ s
->index
, buf
, l
);
4163 if (s
->index
== 4) {
4165 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
4171 l
= s
->packet_len
- s
->index
;
4174 memcpy(s
->buf
+ s
->index
, buf
, l
);
4178 if (s
->index
>= s
->packet_len
) {
4179 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
4188 static void net_socket_send_dgram(void *opaque
)
4190 NetSocketState
*s
= opaque
;
4193 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
4197 /* end of connection */
4198 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
4201 qemu_send_packet(s
->vc
, s
->buf
, size
);
4204 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
4209 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
4210 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
4211 inet_ntoa(mcastaddr
->sin_addr
),
4212 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
4216 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
4218 perror("socket(PF_INET, SOCK_DGRAM)");
4223 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
4224 (const char *)&val
, sizeof(val
));
4226 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
4230 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
4236 /* Add host to multicast group */
4237 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
4238 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
4240 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
4241 (const char *)&imr
, sizeof(struct ip_mreq
));
4243 perror("setsockopt(IP_ADD_MEMBERSHIP)");
4247 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
4249 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
4250 (const char *)&val
, sizeof(val
));
4252 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
4256 socket_set_nonblock(fd
);
4264 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
4267 struct sockaddr_in saddr
;
4269 socklen_t saddr_len
;
4272 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
4273 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
4274 * by ONLY ONE process: we must "clone" this dgram socket --jjo
4278 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
4280 if (saddr
.sin_addr
.s_addr
==0) {
4281 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
4285 /* clone dgram socket */
4286 newfd
= net_socket_mcast_create(&saddr
);
4288 /* error already reported by net_socket_mcast_create() */
4292 /* clone newfd to fd, close newfd */
4297 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
4298 fd
, strerror(errno
));
4303 s
= qemu_mallocz(sizeof(NetSocketState
));
4308 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
4309 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
4311 /* mcast: save bound address as dst */
4312 if (is_connected
) s
->dgram_dst
=saddr
;
4314 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4315 "socket: fd=%d (%s mcast=%s:%d)",
4316 fd
, is_connected
? "cloned" : "",
4317 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4321 static void net_socket_connect(void *opaque
)
4323 NetSocketState
*s
= opaque
;
4324 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
4327 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
4331 s
= qemu_mallocz(sizeof(NetSocketState
));
4335 s
->vc
= qemu_new_vlan_client(vlan
,
4336 net_socket_receive
, NULL
, s
);
4337 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4338 "socket: fd=%d", fd
);
4340 net_socket_connect(s
);
4342 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
4347 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
4350 int so_type
=-1, optlen
=sizeof(so_type
);
4352 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
4353 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
4358 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
4360 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
4362 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
4363 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
4364 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
4369 static void net_socket_accept(void *opaque
)
4371 NetSocketListenState
*s
= opaque
;
4373 struct sockaddr_in saddr
;
4378 len
= sizeof(saddr
);
4379 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
4380 if (fd
< 0 && errno
!= EINTR
) {
4382 } else if (fd
>= 0) {
4386 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
4390 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
4391 "socket: connection from %s:%d",
4392 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4396 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
4398 NetSocketListenState
*s
;
4400 struct sockaddr_in saddr
;
4402 if (parse_host_port(&saddr
, host_str
) < 0)
4405 s
= qemu_mallocz(sizeof(NetSocketListenState
));
4409 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4414 socket_set_nonblock(fd
);
4416 /* allow fast reuse */
4418 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
4420 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4425 ret
= listen(fd
, 0);
4432 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
4436 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
4439 int fd
, connected
, ret
, err
;
4440 struct sockaddr_in saddr
;
4442 if (parse_host_port(&saddr
, host_str
) < 0)
4445 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4450 socket_set_nonblock(fd
);
4454 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4456 err
= socket_error();
4457 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
4458 } else if (err
== EINPROGRESS
) {
4461 } else if (err
== WSAEALREADY
) {
4474 s
= net_socket_fd_init(vlan
, fd
, connected
);
4477 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4478 "socket: connect to %s:%d",
4479 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4483 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
4487 struct sockaddr_in saddr
;
4489 if (parse_host_port(&saddr
, host_str
) < 0)
4493 fd
= net_socket_mcast_create(&saddr
);
4497 s
= net_socket_fd_init(vlan
, fd
, 0);
4501 s
->dgram_dst
= saddr
;
4503 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4504 "socket: mcast=%s:%d",
4505 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4510 static int get_param_value(char *buf
, int buf_size
,
4511 const char *tag
, const char *str
)
4520 while (*p
!= '\0' && *p
!= '=') {
4521 if ((q
- option
) < sizeof(option
) - 1)
4529 if (!strcmp(tag
, option
)) {
4531 while (*p
!= '\0' && *p
!= ',') {
4532 if ((q
- buf
) < buf_size
- 1)
4539 while (*p
!= '\0' && *p
!= ',') {
4550 static int net_client_init(const char *str
)
4561 while (*p
!= '\0' && *p
!= ',') {
4562 if ((q
- device
) < sizeof(device
) - 1)
4570 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
4571 vlan_id
= strtol(buf
, NULL
, 0);
4573 vlan
= qemu_find_vlan(vlan_id
);
4575 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
4578 if (!strcmp(device
, "nic")) {
4582 if (nb_nics
>= MAX_NICS
) {
4583 fprintf(stderr
, "Too Many NICs\n");
4586 nd
= &nd_table
[nb_nics
];
4587 macaddr
= nd
->macaddr
;
4593 macaddr
[5] = 0x56 + nb_nics
;
4595 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
4596 if (parse_macaddr(macaddr
, buf
) < 0) {
4597 fprintf(stderr
, "invalid syntax for ethernet address\n");
4601 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
4602 nd
->model
= strdup(buf
);
4606 vlan
->nb_guest_devs
++;
4609 if (!strcmp(device
, "none")) {
4610 /* does nothing. It is needed to signal that no network cards
4615 if (!strcmp(device
, "user")) {
4616 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
4617 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
4619 vlan
->nb_host_devs
++;
4620 ret
= net_slirp_init(vlan
);
4624 if (!strcmp(device
, "tap")) {
4626 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4627 fprintf(stderr
, "tap: no interface name\n");
4630 vlan
->nb_host_devs
++;
4631 ret
= tap_win32_init(vlan
, ifname
);
4634 if (!strcmp(device
, "tap")) {
4636 char setup_script
[1024], down_script
[1024];
4638 vlan
->nb_host_devs
++;
4639 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4640 fd
= strtol(buf
, NULL
, 0);
4642 if (net_tap_fd_init(vlan
, fd
))
4645 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4648 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
4649 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
4651 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
4652 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
4654 ret
= net_tap_init(vlan
, ifname
, setup_script
, down_script
);
4658 if (!strcmp(device
, "socket")) {
4659 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4661 fd
= strtol(buf
, NULL
, 0);
4663 if (net_socket_fd_init(vlan
, fd
, 1))
4665 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
4666 ret
= net_socket_listen_init(vlan
, buf
);
4667 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
4668 ret
= net_socket_connect_init(vlan
, buf
);
4669 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
4670 ret
= net_socket_mcast_init(vlan
, buf
);
4672 fprintf(stderr
, "Unknown socket options: %s\n", p
);
4675 vlan
->nb_host_devs
++;
4678 fprintf(stderr
, "Unknown network device: %s\n", device
);
4682 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
4688 void do_info_network(void)
4691 VLANClientState
*vc
;
4693 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
4694 term_printf("VLAN %d devices:\n", vlan
->id
);
4695 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
4696 term_printf(" %s\n", vc
->info_str
);
4700 /***********************************************************/
4703 static USBPort
*used_usb_ports
;
4704 static USBPort
*free_usb_ports
;
4706 /* ??? Maybe change this to register a hub to keep track of the topology. */
4707 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
4708 usb_attachfn attach
)
4710 port
->opaque
= opaque
;
4711 port
->index
= index
;
4712 port
->attach
= attach
;
4713 port
->next
= free_usb_ports
;
4714 free_usb_ports
= port
;
4717 static int usb_device_add(const char *devname
)
4723 if (!free_usb_ports
)
4726 if (strstart(devname
, "host:", &p
)) {
4727 dev
= usb_host_device_open(p
);
4728 } else if (!strcmp(devname
, "mouse")) {
4729 dev
= usb_mouse_init();
4730 } else if (!strcmp(devname
, "tablet")) {
4731 dev
= usb_tablet_init();
4732 } else if (!strcmp(devname
, "keyboard")) {
4733 dev
= usb_keyboard_init();
4734 } else if (strstart(devname
, "disk:", &p
)) {
4735 dev
= usb_msd_init(p
);
4736 } else if (!strcmp(devname
, "wacom-tablet")) {
4737 dev
= usb_wacom_init();
4744 /* Find a USB port to add the device to. */
4745 port
= free_usb_ports
;
4749 /* Create a new hub and chain it on. */
4750 free_usb_ports
= NULL
;
4751 port
->next
= used_usb_ports
;
4752 used_usb_ports
= port
;
4754 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
4755 usb_attach(port
, hub
);
4756 port
= free_usb_ports
;
4759 free_usb_ports
= port
->next
;
4760 port
->next
= used_usb_ports
;
4761 used_usb_ports
= port
;
4762 usb_attach(port
, dev
);
4766 static int usb_device_del(const char *devname
)
4774 if (!used_usb_ports
)
4777 p
= strchr(devname
, '.');
4780 bus_num
= strtoul(devname
, NULL
, 0);
4781 addr
= strtoul(p
+ 1, NULL
, 0);
4785 lastp
= &used_usb_ports
;
4786 port
= used_usb_ports
;
4787 while (port
&& port
->dev
->addr
!= addr
) {
4788 lastp
= &port
->next
;
4796 *lastp
= port
->next
;
4797 usb_attach(port
, NULL
);
4798 dev
->handle_destroy(dev
);
4799 port
->next
= free_usb_ports
;
4800 free_usb_ports
= port
;
4804 void do_usb_add(const char *devname
)
4807 ret
= usb_device_add(devname
);
4809 term_printf("Could not add USB device '%s'\n", devname
);
4812 void do_usb_del(const char *devname
)
4815 ret
= usb_device_del(devname
);
4817 term_printf("Could not remove USB device '%s'\n", devname
);
4824 const char *speed_str
;
4827 term_printf("USB support not enabled\n");
4831 for (port
= used_usb_ports
; port
; port
= port
->next
) {
4835 switch(dev
->speed
) {
4839 case USB_SPEED_FULL
:
4842 case USB_SPEED_HIGH
:
4849 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
4850 0, dev
->addr
, speed_str
, dev
->devname
);
4854 /***********************************************************/
4855 /* PCMCIA/Cardbus */
4857 static struct pcmcia_socket_entry_s
{
4858 struct pcmcia_socket_s
*socket
;
4859 struct pcmcia_socket_entry_s
*next
;
4860 } *pcmcia_sockets
= 0;
4862 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
4864 struct pcmcia_socket_entry_s
*entry
;
4866 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
4867 entry
->socket
= socket
;
4868 entry
->next
= pcmcia_sockets
;
4869 pcmcia_sockets
= entry
;
4872 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
4874 struct pcmcia_socket_entry_s
*entry
, **ptr
;
4876 ptr
= &pcmcia_sockets
;
4877 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
4878 if (entry
->socket
== socket
) {
4884 void pcmcia_info(void)
4886 struct pcmcia_socket_entry_s
*iter
;
4887 if (!pcmcia_sockets
)
4888 term_printf("No PCMCIA sockets\n");
4890 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
4891 term_printf("%s: %s\n", iter
->socket
->slot_string
,
4892 iter
->socket
->attached
? iter
->socket
->card_string
:
4896 /***********************************************************/
4899 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4903 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4907 static void dumb_refresh(DisplayState
*ds
)
4909 #if defined(CONFIG_SDL)
4914 static void dumb_display_init(DisplayState
*ds
)
4919 ds
->dpy_update
= dumb_update
;
4920 ds
->dpy_resize
= dumb_resize
;
4921 ds
->dpy_refresh
= dumb_refresh
;
4924 /***********************************************************/
4927 #define MAX_IO_HANDLERS 64
4929 typedef struct IOHandlerRecord
{
4931 IOCanRWHandler
*fd_read_poll
;
4933 IOHandler
*fd_write
;
4936 /* temporary data */
4938 struct IOHandlerRecord
*next
;
4941 static IOHandlerRecord
*first_io_handler
;
4943 /* XXX: fd_read_poll should be suppressed, but an API change is
4944 necessary in the character devices to suppress fd_can_read(). */
4945 int qemu_set_fd_handler2(int fd
,
4946 IOCanRWHandler
*fd_read_poll
,
4948 IOHandler
*fd_write
,
4951 IOHandlerRecord
**pioh
, *ioh
;
4953 if (!fd_read
&& !fd_write
) {
4954 pioh
= &first_io_handler
;
4959 if (ioh
->fd
== fd
) {
4966 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4970 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4973 ioh
->next
= first_io_handler
;
4974 first_io_handler
= ioh
;
4977 ioh
->fd_read_poll
= fd_read_poll
;
4978 ioh
->fd_read
= fd_read
;
4979 ioh
->fd_write
= fd_write
;
4980 ioh
->opaque
= opaque
;
4986 int qemu_set_fd_handler(int fd
,
4988 IOHandler
*fd_write
,
4991 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4994 /***********************************************************/
4995 /* Polling handling */
4997 typedef struct PollingEntry
{
5000 struct PollingEntry
*next
;
5003 static PollingEntry
*first_polling_entry
;
5005 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
5007 PollingEntry
**ppe
, *pe
;
5008 pe
= qemu_mallocz(sizeof(PollingEntry
));
5012 pe
->opaque
= opaque
;
5013 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
5018 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
5020 PollingEntry
**ppe
, *pe
;
5021 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
5023 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
5032 /***********************************************************/
5033 /* Wait objects support */
5034 typedef struct WaitObjects
{
5036 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
5037 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
5038 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
5041 static WaitObjects wait_objects
= {0};
5043 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
5045 WaitObjects
*w
= &wait_objects
;
5047 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
5049 w
->events
[w
->num
] = handle
;
5050 w
->func
[w
->num
] = func
;
5051 w
->opaque
[w
->num
] = opaque
;
5056 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
5059 WaitObjects
*w
= &wait_objects
;
5062 for (i
= 0; i
< w
->num
; i
++) {
5063 if (w
->events
[i
] == handle
)
5066 w
->events
[i
] = w
->events
[i
+ 1];
5067 w
->func
[i
] = w
->func
[i
+ 1];
5068 w
->opaque
[i
] = w
->opaque
[i
+ 1];
5076 /***********************************************************/
5077 /* savevm/loadvm support */
5079 #define IO_BUF_SIZE 32768
5083 BlockDriverState
*bs
;
5086 int64_t base_offset
;
5087 int64_t buf_offset
; /* start of buffer when writing, end of buffer
5090 int buf_size
; /* 0 when writing */
5091 uint8_t buf
[IO_BUF_SIZE
];
5094 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
5098 f
= qemu_mallocz(sizeof(QEMUFile
));
5101 if (!strcmp(mode
, "wb")) {
5103 } else if (!strcmp(mode
, "rb")) {
5108 f
->outfile
= fopen(filename
, mode
);
5120 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
5124 f
= qemu_mallocz(sizeof(QEMUFile
));
5129 f
->is_writable
= is_writable
;
5130 f
->base_offset
= offset
;
5134 void qemu_fflush(QEMUFile
*f
)
5136 if (!f
->is_writable
)
5138 if (f
->buf_index
> 0) {
5140 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
5141 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
5143 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
5144 f
->buf
, f
->buf_index
);
5146 f
->buf_offset
+= f
->buf_index
;
5151 static void qemu_fill_buffer(QEMUFile
*f
)
5158 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
5159 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
5163 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
5164 f
->buf
, IO_BUF_SIZE
);
5170 f
->buf_offset
+= len
;
5173 void qemu_fclose(QEMUFile
*f
)
5183 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
5187 l
= IO_BUF_SIZE
- f
->buf_index
;
5190 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
5194 if (f
->buf_index
>= IO_BUF_SIZE
)
5199 void qemu_put_byte(QEMUFile
*f
, int v
)
5201 f
->buf
[f
->buf_index
++] = v
;
5202 if (f
->buf_index
>= IO_BUF_SIZE
)
5206 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
5212 l
= f
->buf_size
- f
->buf_index
;
5214 qemu_fill_buffer(f
);
5215 l
= f
->buf_size
- f
->buf_index
;
5221 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
5226 return size1
- size
;
5229 int qemu_get_byte(QEMUFile
*f
)
5231 if (f
->buf_index
>= f
->buf_size
) {
5232 qemu_fill_buffer(f
);
5233 if (f
->buf_index
>= f
->buf_size
)
5236 return f
->buf
[f
->buf_index
++];
5239 int64_t qemu_ftell(QEMUFile
*f
)
5241 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
5244 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
5246 if (whence
== SEEK_SET
) {
5248 } else if (whence
== SEEK_CUR
) {
5249 pos
+= qemu_ftell(f
);
5251 /* SEEK_END not supported */
5254 if (f
->is_writable
) {
5256 f
->buf_offset
= pos
;
5258 f
->buf_offset
= pos
;
5265 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
5267 qemu_put_byte(f
, v
>> 8);
5268 qemu_put_byte(f
, v
);
5271 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
5273 qemu_put_byte(f
, v
>> 24);
5274 qemu_put_byte(f
, v
>> 16);
5275 qemu_put_byte(f
, v
>> 8);
5276 qemu_put_byte(f
, v
);
5279 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
5281 qemu_put_be32(f
, v
>> 32);
5282 qemu_put_be32(f
, v
);
5285 unsigned int qemu_get_be16(QEMUFile
*f
)
5288 v
= qemu_get_byte(f
) << 8;
5289 v
|= qemu_get_byte(f
);
5293 unsigned int qemu_get_be32(QEMUFile
*f
)
5296 v
= qemu_get_byte(f
) << 24;
5297 v
|= qemu_get_byte(f
) << 16;
5298 v
|= qemu_get_byte(f
) << 8;
5299 v
|= qemu_get_byte(f
);
5303 uint64_t qemu_get_be64(QEMUFile
*f
)
5306 v
= (uint64_t)qemu_get_be32(f
) << 32;
5307 v
|= qemu_get_be32(f
);
5311 typedef struct SaveStateEntry
{
5315 SaveStateHandler
*save_state
;
5316 LoadStateHandler
*load_state
;
5318 struct SaveStateEntry
*next
;
5321 static SaveStateEntry
*first_se
;
5323 int register_savevm(const char *idstr
,
5326 SaveStateHandler
*save_state
,
5327 LoadStateHandler
*load_state
,
5330 SaveStateEntry
*se
, **pse
;
5332 se
= qemu_malloc(sizeof(SaveStateEntry
));
5335 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
5336 se
->instance_id
= instance_id
;
5337 se
->version_id
= version_id
;
5338 se
->save_state
= save_state
;
5339 se
->load_state
= load_state
;
5340 se
->opaque
= opaque
;
5343 /* add at the end of list */
5345 while (*pse
!= NULL
)
5346 pse
= &(*pse
)->next
;
5351 #define QEMU_VM_FILE_MAGIC 0x5145564d
5352 #define QEMU_VM_FILE_VERSION 0x00000002
5354 int qemu_savevm_state(QEMUFile
*f
)
5358 int64_t cur_pos
, len_pos
, total_len_pos
;
5360 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
5361 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
5362 total_len_pos
= qemu_ftell(f
);
5363 qemu_put_be64(f
, 0); /* total size */
5365 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5367 len
= strlen(se
->idstr
);
5368 qemu_put_byte(f
, len
);
5369 qemu_put_buffer(f
, se
->idstr
, len
);
5371 qemu_put_be32(f
, se
->instance_id
);
5372 qemu_put_be32(f
, se
->version_id
);
5374 /* record size: filled later */
5375 len_pos
= qemu_ftell(f
);
5376 qemu_put_be32(f
, 0);
5378 se
->save_state(f
, se
->opaque
);
5380 /* fill record size */
5381 cur_pos
= qemu_ftell(f
);
5382 len
= cur_pos
- len_pos
- 4;
5383 qemu_fseek(f
, len_pos
, SEEK_SET
);
5384 qemu_put_be32(f
, len
);
5385 qemu_fseek(f
, cur_pos
, SEEK_SET
);
5387 cur_pos
= qemu_ftell(f
);
5388 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
5389 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
5390 qemu_fseek(f
, cur_pos
, SEEK_SET
);
5396 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
5400 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5401 if (!strcmp(se
->idstr
, idstr
) &&
5402 instance_id
== se
->instance_id
)
5408 int qemu_loadvm_state(QEMUFile
*f
)
5411 int len
, ret
, instance_id
, record_len
, version_id
;
5412 int64_t total_len
, end_pos
, cur_pos
;
5416 v
= qemu_get_be32(f
);
5417 if (v
!= QEMU_VM_FILE_MAGIC
)
5419 v
= qemu_get_be32(f
);
5420 if (v
!= QEMU_VM_FILE_VERSION
) {
5425 total_len
= qemu_get_be64(f
);
5426 end_pos
= total_len
+ qemu_ftell(f
);
5428 if (qemu_ftell(f
) >= end_pos
)
5430 len
= qemu_get_byte(f
);
5431 qemu_get_buffer(f
, idstr
, len
);
5433 instance_id
= qemu_get_be32(f
);
5434 version_id
= qemu_get_be32(f
);
5435 record_len
= qemu_get_be32(f
);
5437 printf("idstr=%s instance=0x%x version=%d len=%d\n",
5438 idstr
, instance_id
, version_id
, record_len
);
5440 cur_pos
= qemu_ftell(f
);
5441 se
= find_se(idstr
, instance_id
);
5443 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
5444 instance_id
, idstr
);
5446 ret
= se
->load_state(f
, se
->opaque
, version_id
);
5448 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
5449 instance_id
, idstr
);
5452 /* always seek to exact end of record */
5453 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
5460 /* device can contain snapshots */
5461 static int bdrv_can_snapshot(BlockDriverState
*bs
)
5464 !bdrv_is_removable(bs
) &&
5465 !bdrv_is_read_only(bs
));
5468 /* device must be snapshots in order to have a reliable snapshot */
5469 static int bdrv_has_snapshot(BlockDriverState
*bs
)
5472 !bdrv_is_removable(bs
) &&
5473 !bdrv_is_read_only(bs
));
5476 static BlockDriverState
*get_bs_snapshots(void)
5478 BlockDriverState
*bs
;
5482 return bs_snapshots
;
5483 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5485 if (bdrv_can_snapshot(bs
))
5494 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
5497 QEMUSnapshotInfo
*sn_tab
, *sn
;
5501 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5504 for(i
= 0; i
< nb_sns
; i
++) {
5506 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
5516 void do_savevm(const char *name
)
5518 BlockDriverState
*bs
, *bs1
;
5519 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
5520 int must_delete
, ret
, i
;
5521 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5523 int saved_vm_running
;
5530 bs
= get_bs_snapshots();
5532 term_printf("No block device can accept snapshots\n");
5536 /* ??? Should this occur after vm_stop? */
5539 saved_vm_running
= vm_running
;
5544 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
5549 memset(sn
, 0, sizeof(*sn
));
5551 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
5552 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
5555 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
5558 /* fill auxiliary fields */
5561 sn
->date_sec
= tb
.time
;
5562 sn
->date_nsec
= tb
.millitm
* 1000000;
5564 gettimeofday(&tv
, NULL
);
5565 sn
->date_sec
= tv
.tv_sec
;
5566 sn
->date_nsec
= tv
.tv_usec
* 1000;
5568 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
5570 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5571 term_printf("Device %s does not support VM state snapshots\n",
5572 bdrv_get_device_name(bs
));
5576 /* save the VM state */
5577 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
5579 term_printf("Could not open VM state file\n");
5582 ret
= qemu_savevm_state(f
);
5583 sn
->vm_state_size
= qemu_ftell(f
);
5586 term_printf("Error %d while writing VM\n", ret
);
5590 /* create the snapshots */
5592 for(i
= 0; i
< MAX_DISKS
; i
++) {
5594 if (bdrv_has_snapshot(bs1
)) {
5596 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
5598 term_printf("Error while deleting snapshot on '%s'\n",
5599 bdrv_get_device_name(bs1
));
5602 ret
= bdrv_snapshot_create(bs1
, sn
);
5604 term_printf("Error while creating snapshot on '%s'\n",
5605 bdrv_get_device_name(bs1
));
5611 if (saved_vm_running
)
5615 void do_loadvm(const char *name
)
5617 BlockDriverState
*bs
, *bs1
;
5618 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5621 int saved_vm_running
;
5623 bs
= get_bs_snapshots();
5625 term_printf("No block device supports snapshots\n");
5629 /* Flush all IO requests so they don't interfere with the new state. */
5632 saved_vm_running
= vm_running
;
5635 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5637 if (bdrv_has_snapshot(bs1
)) {
5638 ret
= bdrv_snapshot_goto(bs1
, name
);
5641 term_printf("Warning: ");
5644 term_printf("Snapshots not supported on device '%s'\n",
5645 bdrv_get_device_name(bs1
));
5648 term_printf("Could not find snapshot '%s' on device '%s'\n",
5649 name
, bdrv_get_device_name(bs1
));
5652 term_printf("Error %d while activating snapshot on '%s'\n",
5653 ret
, bdrv_get_device_name(bs1
));
5656 /* fatal on snapshot block device */
5663 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5664 term_printf("Device %s does not support VM state snapshots\n",
5665 bdrv_get_device_name(bs
));
5669 /* restore the VM state */
5670 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
5672 term_printf("Could not open VM state file\n");
5675 ret
= qemu_loadvm_state(f
);
5678 term_printf("Error %d while loading VM state\n", ret
);
5681 if (saved_vm_running
)
5685 void do_delvm(const char *name
)
5687 BlockDriverState
*bs
, *bs1
;
5690 bs
= get_bs_snapshots();
5692 term_printf("No block device supports snapshots\n");
5696 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5698 if (bdrv_has_snapshot(bs1
)) {
5699 ret
= bdrv_snapshot_delete(bs1
, name
);
5701 if (ret
== -ENOTSUP
)
5702 term_printf("Snapshots not supported on device '%s'\n",
5703 bdrv_get_device_name(bs1
));
5705 term_printf("Error %d while deleting snapshot on '%s'\n",
5706 ret
, bdrv_get_device_name(bs1
));
5712 void do_info_snapshots(void)
5714 BlockDriverState
*bs
, *bs1
;
5715 QEMUSnapshotInfo
*sn_tab
, *sn
;
5719 bs
= get_bs_snapshots();
5721 term_printf("No available block device supports snapshots\n");
5724 term_printf("Snapshot devices:");
5725 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5727 if (bdrv_has_snapshot(bs1
)) {
5729 term_printf(" %s", bdrv_get_device_name(bs1
));
5734 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5736 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
5739 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
5740 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
5741 for(i
= 0; i
< nb_sns
; i
++) {
5743 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
5748 /***********************************************************/
5749 /* cpu save/restore */
5751 #if defined(TARGET_I386)
5753 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
5755 qemu_put_be32(f
, dt
->selector
);
5756 qemu_put_betl(f
, dt
->base
);
5757 qemu_put_be32(f
, dt
->limit
);
5758 qemu_put_be32(f
, dt
->flags
);
5761 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
5763 dt
->selector
= qemu_get_be32(f
);
5764 dt
->base
= qemu_get_betl(f
);
5765 dt
->limit
= qemu_get_be32(f
);
5766 dt
->flags
= qemu_get_be32(f
);
5769 void cpu_save(QEMUFile
*f
, void *opaque
)
5771 CPUState
*env
= opaque
;
5772 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
5776 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5777 qemu_put_betls(f
, &env
->regs
[i
]);
5778 qemu_put_betls(f
, &env
->eip
);
5779 qemu_put_betls(f
, &env
->eflags
);
5780 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
5781 qemu_put_be32s(f
, &hflags
);
5785 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
5787 for(i
= 0; i
< 8; i
++) {
5788 fptag
|= ((!env
->fptags
[i
]) << i
);
5791 qemu_put_be16s(f
, &fpuc
);
5792 qemu_put_be16s(f
, &fpus
);
5793 qemu_put_be16s(f
, &fptag
);
5795 #ifdef USE_X86LDOUBLE
5800 qemu_put_be16s(f
, &fpregs_format
);
5802 for(i
= 0; i
< 8; i
++) {
5803 #ifdef USE_X86LDOUBLE
5807 /* we save the real CPU data (in case of MMX usage only 'mant'
5808 contains the MMX register */
5809 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
5810 qemu_put_be64(f
, mant
);
5811 qemu_put_be16(f
, exp
);
5814 /* if we use doubles for float emulation, we save the doubles to
5815 avoid losing information in case of MMX usage. It can give
5816 problems if the image is restored on a CPU where long
5817 doubles are used instead. */
5818 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
5822 for(i
= 0; i
< 6; i
++)
5823 cpu_put_seg(f
, &env
->segs
[i
]);
5824 cpu_put_seg(f
, &env
->ldt
);
5825 cpu_put_seg(f
, &env
->tr
);
5826 cpu_put_seg(f
, &env
->gdt
);
5827 cpu_put_seg(f
, &env
->idt
);
5829 qemu_put_be32s(f
, &env
->sysenter_cs
);
5830 qemu_put_be32s(f
, &env
->sysenter_esp
);
5831 qemu_put_be32s(f
, &env
->sysenter_eip
);
5833 qemu_put_betls(f
, &env
->cr
[0]);
5834 qemu_put_betls(f
, &env
->cr
[2]);
5835 qemu_put_betls(f
, &env
->cr
[3]);
5836 qemu_put_betls(f
, &env
->cr
[4]);
5838 for(i
= 0; i
< 8; i
++)
5839 qemu_put_betls(f
, &env
->dr
[i
]);
5842 qemu_put_be32s(f
, &env
->a20_mask
);
5845 qemu_put_be32s(f
, &env
->mxcsr
);
5846 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5847 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5848 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5851 #ifdef TARGET_X86_64
5852 qemu_put_be64s(f
, &env
->efer
);
5853 qemu_put_be64s(f
, &env
->star
);
5854 qemu_put_be64s(f
, &env
->lstar
);
5855 qemu_put_be64s(f
, &env
->cstar
);
5856 qemu_put_be64s(f
, &env
->fmask
);
5857 qemu_put_be64s(f
, &env
->kernelgsbase
);
5859 qemu_put_be32s(f
, &env
->smbase
);
5862 #ifdef USE_X86LDOUBLE
5863 /* XXX: add that in a FPU generic layer */
5864 union x86_longdouble
{
5869 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
5870 #define EXPBIAS1 1023
5871 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
5872 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
5874 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
5878 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
5879 /* exponent + sign */
5880 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
5881 e
|= SIGND1(temp
) >> 16;
5886 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5888 CPUState
*env
= opaque
;
5891 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
5893 if (version_id
!= 3 && version_id
!= 4)
5895 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5896 qemu_get_betls(f
, &env
->regs
[i
]);
5897 qemu_get_betls(f
, &env
->eip
);
5898 qemu_get_betls(f
, &env
->eflags
);
5899 qemu_get_be32s(f
, &hflags
);
5901 qemu_get_be16s(f
, &fpuc
);
5902 qemu_get_be16s(f
, &fpus
);
5903 qemu_get_be16s(f
, &fptag
);
5904 qemu_get_be16s(f
, &fpregs_format
);
5906 /* NOTE: we cannot always restore the FPU state if the image come
5907 from a host with a different 'USE_X86LDOUBLE' define. We guess
5908 if we are in an MMX state to restore correctly in that case. */
5909 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
5910 for(i
= 0; i
< 8; i
++) {
5914 switch(fpregs_format
) {
5916 mant
= qemu_get_be64(f
);
5917 exp
= qemu_get_be16(f
);
5918 #ifdef USE_X86LDOUBLE
5919 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5921 /* difficult case */
5923 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5925 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5929 mant
= qemu_get_be64(f
);
5930 #ifdef USE_X86LDOUBLE
5932 union x86_longdouble
*p
;
5933 /* difficult case */
5934 p
= (void *)&env
->fpregs
[i
];
5939 fp64_to_fp80(p
, mant
);
5943 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5952 /* XXX: restore FPU round state */
5953 env
->fpstt
= (fpus
>> 11) & 7;
5954 env
->fpus
= fpus
& ~0x3800;
5956 for(i
= 0; i
< 8; i
++) {
5957 env
->fptags
[i
] = (fptag
>> i
) & 1;
5960 for(i
= 0; i
< 6; i
++)
5961 cpu_get_seg(f
, &env
->segs
[i
]);
5962 cpu_get_seg(f
, &env
->ldt
);
5963 cpu_get_seg(f
, &env
->tr
);
5964 cpu_get_seg(f
, &env
->gdt
);
5965 cpu_get_seg(f
, &env
->idt
);
5967 qemu_get_be32s(f
, &env
->sysenter_cs
);
5968 qemu_get_be32s(f
, &env
->sysenter_esp
);
5969 qemu_get_be32s(f
, &env
->sysenter_eip
);
5971 qemu_get_betls(f
, &env
->cr
[0]);
5972 qemu_get_betls(f
, &env
->cr
[2]);
5973 qemu_get_betls(f
, &env
->cr
[3]);
5974 qemu_get_betls(f
, &env
->cr
[4]);
5976 for(i
= 0; i
< 8; i
++)
5977 qemu_get_betls(f
, &env
->dr
[i
]);
5980 qemu_get_be32s(f
, &env
->a20_mask
);
5982 qemu_get_be32s(f
, &env
->mxcsr
);
5983 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5984 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5985 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5988 #ifdef TARGET_X86_64
5989 qemu_get_be64s(f
, &env
->efer
);
5990 qemu_get_be64s(f
, &env
->star
);
5991 qemu_get_be64s(f
, &env
->lstar
);
5992 qemu_get_be64s(f
, &env
->cstar
);
5993 qemu_get_be64s(f
, &env
->fmask
);
5994 qemu_get_be64s(f
, &env
->kernelgsbase
);
5996 if (version_id
>= 4)
5997 qemu_get_be32s(f
, &env
->smbase
);
5999 /* XXX: compute hflags from scratch, except for CPL and IIF */
6000 env
->hflags
= hflags
;
6005 #elif defined(TARGET_PPC)
6006 void cpu_save(QEMUFile
*f
, void *opaque
)
6010 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6015 #elif defined(TARGET_MIPS)
6016 void cpu_save(QEMUFile
*f
, void *opaque
)
6020 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6025 #elif defined(TARGET_SPARC)
6026 void cpu_save(QEMUFile
*f
, void *opaque
)
6028 CPUState
*env
= opaque
;
6032 for(i
= 0; i
< 8; i
++)
6033 qemu_put_betls(f
, &env
->gregs
[i
]);
6034 for(i
= 0; i
< NWINDOWS
* 16; i
++)
6035 qemu_put_betls(f
, &env
->regbase
[i
]);
6038 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
6044 qemu_put_be32(f
, u
.i
);
6047 qemu_put_betls(f
, &env
->pc
);
6048 qemu_put_betls(f
, &env
->npc
);
6049 qemu_put_betls(f
, &env
->y
);
6051 qemu_put_be32(f
, tmp
);
6052 qemu_put_betls(f
, &env
->fsr
);
6053 qemu_put_betls(f
, &env
->tbr
);
6054 #ifndef TARGET_SPARC64
6055 qemu_put_be32s(f
, &env
->wim
);
6057 for(i
= 0; i
< 16; i
++)
6058 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
6062 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6064 CPUState
*env
= opaque
;
6068 for(i
= 0; i
< 8; i
++)
6069 qemu_get_betls(f
, &env
->gregs
[i
]);
6070 for(i
= 0; i
< NWINDOWS
* 16; i
++)
6071 qemu_get_betls(f
, &env
->regbase
[i
]);
6074 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
6079 u
.i
= qemu_get_be32(f
);
6083 qemu_get_betls(f
, &env
->pc
);
6084 qemu_get_betls(f
, &env
->npc
);
6085 qemu_get_betls(f
, &env
->y
);
6086 tmp
= qemu_get_be32(f
);
6087 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
6088 correctly updated */
6090 qemu_get_betls(f
, &env
->fsr
);
6091 qemu_get_betls(f
, &env
->tbr
);
6092 #ifndef TARGET_SPARC64
6093 qemu_get_be32s(f
, &env
->wim
);
6095 for(i
= 0; i
< 16; i
++)
6096 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
6102 #elif defined(TARGET_ARM)
6104 void cpu_save(QEMUFile
*f
, void *opaque
)
6107 CPUARMState
*env
= (CPUARMState
*)opaque
;
6109 for (i
= 0; i
< 16; i
++) {
6110 qemu_put_be32(f
, env
->regs
[i
]);
6112 qemu_put_be32(f
, cpsr_read(env
));
6113 qemu_put_be32(f
, env
->spsr
);
6114 for (i
= 0; i
< 6; i
++) {
6115 qemu_put_be32(f
, env
->banked_spsr
[i
]);
6116 qemu_put_be32(f
, env
->banked_r13
[i
]);
6117 qemu_put_be32(f
, env
->banked_r14
[i
]);
6119 for (i
= 0; i
< 5; i
++) {
6120 qemu_put_be32(f
, env
->usr_regs
[i
]);
6121 qemu_put_be32(f
, env
->fiq_regs
[i
]);
6123 qemu_put_be32(f
, env
->cp15
.c0_cpuid
);
6124 qemu_put_be32(f
, env
->cp15
.c0_cachetype
);
6125 qemu_put_be32(f
, env
->cp15
.c1_sys
);
6126 qemu_put_be32(f
, env
->cp15
.c1_coproc
);
6127 qemu_put_be32(f
, env
->cp15
.c1_xscaleauxcr
);
6128 qemu_put_be32(f
, env
->cp15
.c2_base
);
6129 qemu_put_be32(f
, env
->cp15
.c2_data
);
6130 qemu_put_be32(f
, env
->cp15
.c2_insn
);
6131 qemu_put_be32(f
, env
->cp15
.c3
);
6132 qemu_put_be32(f
, env
->cp15
.c5_insn
);
6133 qemu_put_be32(f
, env
->cp15
.c5_data
);
6134 for (i
= 0; i
< 8; i
++) {
6135 qemu_put_be32(f
, env
->cp15
.c6_region
[i
]);
6137 qemu_put_be32(f
, env
->cp15
.c6_insn
);
6138 qemu_put_be32(f
, env
->cp15
.c6_data
);
6139 qemu_put_be32(f
, env
->cp15
.c9_insn
);
6140 qemu_put_be32(f
, env
->cp15
.c9_data
);
6141 qemu_put_be32(f
, env
->cp15
.c13_fcse
);
6142 qemu_put_be32(f
, env
->cp15
.c13_context
);
6143 qemu_put_be32(f
, env
->cp15
.c15_cpar
);
6145 qemu_put_be32(f
, env
->features
);
6147 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
6148 for (i
= 0; i
< 16; i
++) {
6150 u
.d
= env
->vfp
.regs
[i
];
6151 qemu_put_be32(f
, u
.l
.upper
);
6152 qemu_put_be32(f
, u
.l
.lower
);
6154 for (i
= 0; i
< 16; i
++) {
6155 qemu_put_be32(f
, env
->vfp
.xregs
[i
]);
6158 /* TODO: Should use proper FPSCR access functions. */
6159 qemu_put_be32(f
, env
->vfp
.vec_len
);
6160 qemu_put_be32(f
, env
->vfp
.vec_stride
);
6163 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
6164 for (i
= 0; i
< 16; i
++) {
6165 qemu_put_be64(f
, env
->iwmmxt
.regs
[i
]);
6167 for (i
= 0; i
< 16; i
++) {
6168 qemu_put_be32(f
, env
->iwmmxt
.cregs
[i
]);
6173 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6175 CPUARMState
*env
= (CPUARMState
*)opaque
;
6178 if (version_id
!= 0)
6181 for (i
= 0; i
< 16; i
++) {
6182 env
->regs
[i
] = qemu_get_be32(f
);
6184 cpsr_write(env
, qemu_get_be32(f
), 0xffffffff);
6185 env
->spsr
= qemu_get_be32(f
);
6186 for (i
= 0; i
< 6; i
++) {
6187 env
->banked_spsr
[i
] = qemu_get_be32(f
);
6188 env
->banked_r13
[i
] = qemu_get_be32(f
);
6189 env
->banked_r14
[i
] = qemu_get_be32(f
);
6191 for (i
= 0; i
< 5; i
++) {
6192 env
->usr_regs
[i
] = qemu_get_be32(f
);
6193 env
->fiq_regs
[i
] = qemu_get_be32(f
);
6195 env
->cp15
.c0_cpuid
= qemu_get_be32(f
);
6196 env
->cp15
.c0_cachetype
= qemu_get_be32(f
);
6197 env
->cp15
.c1_sys
= qemu_get_be32(f
);
6198 env
->cp15
.c1_coproc
= qemu_get_be32(f
);
6199 env
->cp15
.c1_xscaleauxcr
= qemu_get_be32(f
);
6200 env
->cp15
.c2_base
= qemu_get_be32(f
);
6201 env
->cp15
.c2_data
= qemu_get_be32(f
);
6202 env
->cp15
.c2_insn
= qemu_get_be32(f
);
6203 env
->cp15
.c3
= qemu_get_be32(f
);
6204 env
->cp15
.c5_insn
= qemu_get_be32(f
);
6205 env
->cp15
.c5_data
= qemu_get_be32(f
);
6206 for (i
= 0; i
< 8; i
++) {
6207 env
->cp15
.c6_region
[i
] = qemu_get_be32(f
);
6209 env
->cp15
.c6_insn
= qemu_get_be32(f
);
6210 env
->cp15
.c6_data
= qemu_get_be32(f
);
6211 env
->cp15
.c9_insn
= qemu_get_be32(f
);
6212 env
->cp15
.c9_data
= qemu_get_be32(f
);
6213 env
->cp15
.c13_fcse
= qemu_get_be32(f
);
6214 env
->cp15
.c13_context
= qemu_get_be32(f
);
6215 env
->cp15
.c15_cpar
= qemu_get_be32(f
);
6217 env
->features
= qemu_get_be32(f
);
6219 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
6220 for (i
= 0; i
< 16; i
++) {
6222 u
.l
.upper
= qemu_get_be32(f
);
6223 u
.l
.lower
= qemu_get_be32(f
);
6224 env
->vfp
.regs
[i
] = u
.d
;
6226 for (i
= 0; i
< 16; i
++) {
6227 env
->vfp
.xregs
[i
] = qemu_get_be32(f
);
6230 /* TODO: Should use proper FPSCR access functions. */
6231 env
->vfp
.vec_len
= qemu_get_be32(f
);
6232 env
->vfp
.vec_stride
= qemu_get_be32(f
);
6235 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
6236 for (i
= 0; i
< 16; i
++) {
6237 env
->iwmmxt
.regs
[i
] = qemu_get_be64(f
);
6239 for (i
= 0; i
< 16; i
++) {
6240 env
->iwmmxt
.cregs
[i
] = qemu_get_be32(f
);
6249 #warning No CPU save/restore functions
6253 /***********************************************************/
6254 /* ram save/restore */
6256 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
6260 v
= qemu_get_byte(f
);
6263 if (qemu_get_buffer(f
, buf
, len
) != len
)
6267 v
= qemu_get_byte(f
);
6268 memset(buf
, v
, len
);
6276 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
6280 if (qemu_get_be32(f
) != phys_ram_size
)
6282 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
6283 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
6290 #define BDRV_HASH_BLOCK_SIZE 1024
6291 #define IOBUF_SIZE 4096
6292 #define RAM_CBLOCK_MAGIC 0xfabe
6294 typedef struct RamCompressState
{
6297 uint8_t buf
[IOBUF_SIZE
];
6300 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
6303 memset(s
, 0, sizeof(*s
));
6305 ret
= deflateInit2(&s
->zstream
, 1,
6307 9, Z_DEFAULT_STRATEGY
);
6310 s
->zstream
.avail_out
= IOBUF_SIZE
;
6311 s
->zstream
.next_out
= s
->buf
;
6315 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
6317 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
6318 qemu_put_be16(s
->f
, len
);
6319 qemu_put_buffer(s
->f
, buf
, len
);
6322 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
6326 s
->zstream
.avail_in
= len
;
6327 s
->zstream
.next_in
= (uint8_t *)buf
;
6328 while (s
->zstream
.avail_in
> 0) {
6329 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
6332 if (s
->zstream
.avail_out
== 0) {
6333 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
6334 s
->zstream
.avail_out
= IOBUF_SIZE
;
6335 s
->zstream
.next_out
= s
->buf
;
6341 static void ram_compress_close(RamCompressState
*s
)
6345 /* compress last bytes */
6347 ret
= deflate(&s
->zstream
, Z_FINISH
);
6348 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
6349 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
6351 ram_put_cblock(s
, s
->buf
, len
);
6353 s
->zstream
.avail_out
= IOBUF_SIZE
;
6354 s
->zstream
.next_out
= s
->buf
;
6355 if (ret
== Z_STREAM_END
)
6362 deflateEnd(&s
->zstream
);
6365 typedef struct RamDecompressState
{
6368 uint8_t buf
[IOBUF_SIZE
];
6369 } RamDecompressState
;
6371 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
6374 memset(s
, 0, sizeof(*s
));
6376 ret
= inflateInit(&s
->zstream
);
6382 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
6386 s
->zstream
.avail_out
= len
;
6387 s
->zstream
.next_out
= buf
;
6388 while (s
->zstream
.avail_out
> 0) {
6389 if (s
->zstream
.avail_in
== 0) {
6390 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
6392 clen
= qemu_get_be16(s
->f
);
6393 if (clen
> IOBUF_SIZE
)
6395 qemu_get_buffer(s
->f
, s
->buf
, clen
);
6396 s
->zstream
.avail_in
= clen
;
6397 s
->zstream
.next_in
= s
->buf
;
6399 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
6400 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
6407 static void ram_decompress_close(RamDecompressState
*s
)
6409 inflateEnd(&s
->zstream
);
6412 static void ram_save(QEMUFile
*f
, void *opaque
)
6415 RamCompressState s1
, *s
= &s1
;
6418 qemu_put_be32(f
, phys_ram_size
);
6419 if (ram_compress_open(s
, f
) < 0)
6421 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6423 if (tight_savevm_enabled
) {
6427 /* find if the memory block is available on a virtual
6430 for(j
= 0; j
< MAX_DISKS
; j
++) {
6432 sector_num
= bdrv_hash_find(bs_table
[j
],
6433 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6434 if (sector_num
>= 0)
6439 goto normal_compress
;
6442 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
6443 ram_compress_buf(s
, buf
, 10);
6449 ram_compress_buf(s
, buf
, 1);
6450 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6453 ram_compress_close(s
);
6456 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
6458 RamDecompressState s1
, *s
= &s1
;
6462 if (version_id
== 1)
6463 return ram_load_v1(f
, opaque
);
6464 if (version_id
!= 2)
6466 if (qemu_get_be32(f
) != phys_ram_size
)
6468 if (ram_decompress_open(s
, f
) < 0)
6470 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6471 if (ram_decompress_buf(s
, buf
, 1) < 0) {
6472 fprintf(stderr
, "Error while reading ram block header\n");
6476 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
6477 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
6486 ram_decompress_buf(s
, buf
+ 1, 9);
6488 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
6489 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
6490 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
6493 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
6494 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
6495 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
6496 bs_index
, sector_num
);
6503 printf("Error block header\n");
6507 ram_decompress_close(s
);
6511 /***********************************************************/
6512 /* bottom halves (can be seen as timers which expire ASAP) */
6521 static QEMUBH
*first_bh
= NULL
;
6523 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
6526 bh
= qemu_mallocz(sizeof(QEMUBH
));
6530 bh
->opaque
= opaque
;
6534 int qemu_bh_poll(void)
6553 void qemu_bh_schedule(QEMUBH
*bh
)
6555 CPUState
*env
= cpu_single_env
;
6559 bh
->next
= first_bh
;
6562 /* stop the currently executing CPU to execute the BH ASAP */
6564 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
6568 void qemu_bh_cancel(QEMUBH
*bh
)
6571 if (bh
->scheduled
) {
6574 pbh
= &(*pbh
)->next
;
6580 void qemu_bh_delete(QEMUBH
*bh
)
6586 /***********************************************************/
6587 /* machine registration */
6589 QEMUMachine
*first_machine
= NULL
;
6591 int qemu_register_machine(QEMUMachine
*m
)
6594 pm
= &first_machine
;
6602 QEMUMachine
*find_machine(const char *name
)
6606 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6607 if (!strcmp(m
->name
, name
))
6613 /***********************************************************/
6614 /* main execution loop */
6616 void gui_update(void *opaque
)
6618 DisplayState
*ds
= opaque
;
6619 ds
->dpy_refresh(ds
);
6620 qemu_mod_timer(ds
->gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
6623 struct vm_change_state_entry
{
6624 VMChangeStateHandler
*cb
;
6626 LIST_ENTRY (vm_change_state_entry
) entries
;
6629 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
6631 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
6634 VMChangeStateEntry
*e
;
6636 e
= qemu_mallocz(sizeof (*e
));
6642 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
6646 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
6648 LIST_REMOVE (e
, entries
);
6652 static void vm_state_notify(int running
)
6654 VMChangeStateEntry
*e
;
6656 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
6657 e
->cb(e
->opaque
, running
);
6661 /* XXX: support several handlers */
6662 static VMStopHandler
*vm_stop_cb
;
6663 static void *vm_stop_opaque
;
6665 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6668 vm_stop_opaque
= opaque
;
6672 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6683 qemu_rearm_alarm_timer(alarm_timer
);
6687 void vm_stop(int reason
)
6690 cpu_disable_ticks();
6694 vm_stop_cb(vm_stop_opaque
, reason
);
6701 /* reset/shutdown handler */
6703 typedef struct QEMUResetEntry
{
6704 QEMUResetHandler
*func
;
6706 struct QEMUResetEntry
*next
;
6709 static QEMUResetEntry
*first_reset_entry
;
6710 static int reset_requested
;
6711 static int shutdown_requested
;
6712 static int powerdown_requested
;
6714 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
6716 QEMUResetEntry
**pre
, *re
;
6718 pre
= &first_reset_entry
;
6719 while (*pre
!= NULL
)
6720 pre
= &(*pre
)->next
;
6721 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
6723 re
->opaque
= opaque
;
6728 static void qemu_system_reset(void)
6732 /* reset all devices */
6733 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
6734 re
->func(re
->opaque
);
6738 void qemu_system_reset_request(void)
6741 shutdown_requested
= 1;
6743 reset_requested
= 1;
6746 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6749 void qemu_system_shutdown_request(void)
6751 shutdown_requested
= 1;
6753 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6756 void qemu_system_powerdown_request(void)
6758 powerdown_requested
= 1;
6760 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6763 void main_loop_wait(int timeout
)
6765 IOHandlerRecord
*ioh
;
6766 fd_set rfds
, wfds
, xfds
;
6775 /* XXX: need to suppress polling by better using win32 events */
6777 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
6778 ret
|= pe
->func(pe
->opaque
);
6783 WaitObjects
*w
= &wait_objects
;
6785 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
6786 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
6787 if (w
->func
[ret
- WAIT_OBJECT_0
])
6788 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
6790 /* Check for additional signaled events */
6791 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
6793 /* Check if event is signaled */
6794 ret2
= WaitForSingleObject(w
->events
[i
], 0);
6795 if(ret2
== WAIT_OBJECT_0
) {
6797 w
->func
[i
](w
->opaque
[i
]);
6798 } else if (ret2
== WAIT_TIMEOUT
) {
6800 err
= GetLastError();
6801 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
6804 } else if (ret
== WAIT_TIMEOUT
) {
6806 err
= GetLastError();
6807 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
6811 /* poll any events */
6812 /* XXX: separate device handlers from system ones */
6817 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6821 (!ioh
->fd_read_poll
||
6822 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
6823 FD_SET(ioh
->fd
, &rfds
);
6827 if (ioh
->fd_write
) {
6828 FD_SET(ioh
->fd
, &wfds
);
6838 tv
.tv_usec
= timeout
* 1000;
6840 #if defined(CONFIG_SLIRP)
6842 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
6845 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
6847 IOHandlerRecord
**pioh
;
6849 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6850 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
6851 ioh
->fd_read(ioh
->opaque
);
6853 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
6854 ioh
->fd_write(ioh
->opaque
);
6858 /* remove deleted IO handlers */
6859 pioh
= &first_io_handler
;
6869 #if defined(CONFIG_SLIRP)
6876 slirp_select_poll(&rfds
, &wfds
, &xfds
);
6882 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
6883 qemu_get_clock(vm_clock
));
6884 /* run dma transfers, if any */
6888 /* real time timers */
6889 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
6890 qemu_get_clock(rt_clock
));
6892 /* Check bottom-halves last in case any of the earlier events triggered
6898 static CPUState
*cur_cpu
;
6903 #ifdef CONFIG_PROFILER
6908 cur_cpu
= first_cpu
;
6915 env
= env
->next_cpu
;
6918 #ifdef CONFIG_PROFILER
6919 ti
= profile_getclock();
6921 ret
= cpu_exec(env
);
6922 #ifdef CONFIG_PROFILER
6923 qemu_time
+= profile_getclock() - ti
;
6925 if (ret
== EXCP_HLT
) {
6926 /* Give the next CPU a chance to run. */
6930 if (ret
!= EXCP_HALTED
)
6932 /* all CPUs are halted ? */
6938 if (shutdown_requested
) {
6939 ret
= EXCP_INTERRUPT
;
6942 if (reset_requested
) {
6943 reset_requested
= 0;
6944 qemu_system_reset();
6945 ret
= EXCP_INTERRUPT
;
6947 if (powerdown_requested
) {
6948 powerdown_requested
= 0;
6949 qemu_system_powerdown();
6950 ret
= EXCP_INTERRUPT
;
6952 if (ret
== EXCP_DEBUG
) {
6953 vm_stop(EXCP_DEBUG
);
6955 /* If all cpus are halted then wait until the next IRQ */
6956 /* XXX: use timeout computed from timers */
6957 if (ret
== EXCP_HALTED
)
6964 #ifdef CONFIG_PROFILER
6965 ti
= profile_getclock();
6967 main_loop_wait(timeout
);
6968 #ifdef CONFIG_PROFILER
6969 dev_time
+= profile_getclock() - ti
;
6972 cpu_disable_ticks();
6976 static void help(int exitcode
)
6978 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2007 Fabrice Bellard\n"
6979 "usage: %s [options] [disk_image]\n"
6981 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
6983 "Standard options:\n"
6984 "-M machine select emulated machine (-M ? for list)\n"
6985 "-cpu cpu select CPU (-cpu ? for list)\n"
6986 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
6987 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
6988 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
6989 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
6990 "-mtdblock file use 'file' as on-board Flash memory image\n"
6991 "-sd file use 'file' as SecureDigital card image\n"
6992 "-pflash file use 'file' as a parallel flash image\n"
6993 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
6994 "-snapshot write to temporary files instead of disk image files\n"
6996 "-no-frame open SDL window without a frame and window decorations\n"
6997 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
6998 "-no-quit disable SDL window close capability\n"
7001 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
7003 "-m megs set virtual RAM size to megs MB [default=%d]\n"
7004 "-smp n set the number of CPUs to 'n' [default=1]\n"
7005 "-nographic disable graphical output and redirect serial I/Os to console\n"
7006 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
7008 "-k language use keyboard layout (for example \"fr\" for French)\n"
7011 "-audio-help print list of audio drivers and their options\n"
7012 "-soundhw c1,... enable audio support\n"
7013 " and only specified sound cards (comma separated list)\n"
7014 " use -soundhw ? to get the list of supported cards\n"
7015 " use -soundhw all to enable all of them\n"
7017 "-localtime set the real time clock to local time [default=utc]\n"
7018 "-full-screen start in full screen\n"
7020 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
7022 "-usb enable the USB driver (will be the default soon)\n"
7023 "-usbdevice name add the host or guest USB device 'name'\n"
7024 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7025 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
7027 "-name string set the name of the guest\n"
7029 "Network options:\n"
7030 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
7031 " create a new Network Interface Card and connect it to VLAN 'n'\n"
7033 "-net user[,vlan=n][,hostname=host]\n"
7034 " connect the user mode network stack to VLAN 'n' and send\n"
7035 " hostname 'host' to DHCP clients\n"
7038 "-net tap[,vlan=n],ifname=name\n"
7039 " connect the host TAP network interface to VLAN 'n'\n"
7041 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
7042 " connect the host TAP network interface to VLAN 'n' and use the\n"
7043 " network scripts 'file' (default=%s)\n"
7044 " and 'dfile' (default=%s);\n"
7045 " use '[down]script=no' to disable script execution;\n"
7046 " use 'fd=h' to connect to an already opened TAP interface\n"
7048 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
7049 " connect the vlan 'n' to another VLAN using a socket connection\n"
7050 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
7051 " connect the vlan 'n' to multicast maddr and port\n"
7052 "-net none use it alone to have zero network devices; if no -net option\n"
7053 " is provided, the default is '-net nic -net user'\n"
7056 "-tftp dir allow tftp access to files in dir [-net user]\n"
7057 "-bootp file advertise file in BOOTP replies\n"
7059 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
7061 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
7062 " redirect TCP or UDP connections from host to guest [-net user]\n"
7065 "Linux boot specific:\n"
7066 "-kernel bzImage use 'bzImage' as kernel image\n"
7067 "-append cmdline use 'cmdline' as kernel command line\n"
7068 "-initrd file use 'file' as initial ram disk\n"
7070 "Debug/Expert options:\n"
7071 "-monitor dev redirect the monitor to char device 'dev'\n"
7072 "-serial dev redirect the serial port to char device 'dev'\n"
7073 "-parallel dev redirect the parallel port to char device 'dev'\n"
7074 "-pidfile file Write PID to 'file'\n"
7075 "-S freeze CPU at startup (use 'c' to start execution)\n"
7076 "-s wait gdb connection to port\n"
7077 "-p port set gdb connection port [default=%s]\n"
7078 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
7079 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
7080 " translation (t=none or lba) (usually qemu can guess them)\n"
7081 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
7083 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
7084 "-no-kqemu disable KQEMU kernel module usage\n"
7086 #ifdef USE_CODE_COPY
7087 "-no-code-copy disable code copy acceleration\n"
7090 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
7091 " (default is CL-GD5446 PCI VGA)\n"
7092 "-no-acpi disable ACPI\n"
7094 "-no-reboot exit instead of rebooting\n"
7095 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
7096 "-vnc display start a VNC server on display\n"
7098 "-daemonize daemonize QEMU after initializing\n"
7100 "-option-rom rom load a file, rom, into the option ROM space\n"
7102 "-prom-env variable=value set OpenBIOS nvram variables\n"
7104 "-clock force the use of the given methods for timer alarm.\n"
7105 " To see what timers are available use -clock help\n"
7107 "During emulation, the following keys are useful:\n"
7108 "ctrl-alt-f toggle full screen\n"
7109 "ctrl-alt-n switch to virtual console 'n'\n"
7110 "ctrl-alt toggle mouse and keyboard grab\n"
7112 "When using -nographic, press 'ctrl-a h' to get some help.\n"
7117 DEFAULT_NETWORK_SCRIPT
,
7118 DEFAULT_NETWORK_DOWN_SCRIPT
,
7120 DEFAULT_GDBSTUB_PORT
,
7125 #define HAS_ARG 0x0001
7139 QEMU_OPTION_mtdblock
,
7143 QEMU_OPTION_snapshot
,
7145 QEMU_OPTION_no_fd_bootchk
,
7148 QEMU_OPTION_nographic
,
7149 QEMU_OPTION_portrait
,
7151 QEMU_OPTION_audio_help
,
7152 QEMU_OPTION_soundhw
,
7172 QEMU_OPTION_no_code_copy
,
7174 QEMU_OPTION_localtime
,
7175 QEMU_OPTION_cirrusvga
,
7178 QEMU_OPTION_std_vga
,
7180 QEMU_OPTION_monitor
,
7182 QEMU_OPTION_parallel
,
7184 QEMU_OPTION_full_screen
,
7185 QEMU_OPTION_no_frame
,
7186 QEMU_OPTION_alt_grab
,
7187 QEMU_OPTION_no_quit
,
7188 QEMU_OPTION_pidfile
,
7189 QEMU_OPTION_no_kqemu
,
7190 QEMU_OPTION_kernel_kqemu
,
7191 QEMU_OPTION_win2k_hack
,
7193 QEMU_OPTION_usbdevice
,
7196 QEMU_OPTION_no_acpi
,
7197 QEMU_OPTION_no_reboot
,
7198 QEMU_OPTION_show_cursor
,
7199 QEMU_OPTION_daemonize
,
7200 QEMU_OPTION_option_rom
,
7201 QEMU_OPTION_semihosting
,
7203 QEMU_OPTION_prom_env
,
7204 QEMU_OPTION_old_param
,
7208 typedef struct QEMUOption
{
7214 const QEMUOption qemu_options
[] = {
7215 { "h", 0, QEMU_OPTION_h
},
7216 { "help", 0, QEMU_OPTION_h
},
7218 { "M", HAS_ARG
, QEMU_OPTION_M
},
7219 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
7220 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
7221 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
7222 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
7223 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
7224 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
7225 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
7226 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
7227 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
7228 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
7229 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
7230 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
7231 { "snapshot", 0, QEMU_OPTION_snapshot
},
7233 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
7235 { "m", HAS_ARG
, QEMU_OPTION_m
},
7236 { "nographic", 0, QEMU_OPTION_nographic
},
7237 { "portrait", 0, QEMU_OPTION_portrait
},
7238 { "k", HAS_ARG
, QEMU_OPTION_k
},
7240 { "audio-help", 0, QEMU_OPTION_audio_help
},
7241 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
7244 { "net", HAS_ARG
, QEMU_OPTION_net
},
7246 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
7247 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
7249 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
7251 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
7254 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
7255 { "append", HAS_ARG
, QEMU_OPTION_append
},
7256 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
7258 { "S", 0, QEMU_OPTION_S
},
7259 { "s", 0, QEMU_OPTION_s
},
7260 { "p", HAS_ARG
, QEMU_OPTION_p
},
7261 { "d", HAS_ARG
, QEMU_OPTION_d
},
7262 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
7263 { "L", HAS_ARG
, QEMU_OPTION_L
},
7264 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
7265 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
7267 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
7268 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
7270 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7271 { "g", 1, QEMU_OPTION_g
},
7273 { "localtime", 0, QEMU_OPTION_localtime
},
7274 { "std-vga", 0, QEMU_OPTION_std_vga
},
7275 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
7276 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
7277 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
7278 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
7279 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
7280 { "full-screen", 0, QEMU_OPTION_full_screen
},
7282 { "no-frame", 0, QEMU_OPTION_no_frame
},
7283 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
7284 { "no-quit", 0, QEMU_OPTION_no_quit
},
7286 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
7287 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
7288 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
7289 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
7290 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
7292 /* temporary options */
7293 { "usb", 0, QEMU_OPTION_usb
},
7294 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
7295 { "vmwarevga", 0, QEMU_OPTION_vmsvga
},
7296 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
7297 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
7298 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
7299 { "daemonize", 0, QEMU_OPTION_daemonize
},
7300 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
7301 #if defined(TARGET_ARM) || defined(TARGET_M68K)
7302 { "semihosting", 0, QEMU_OPTION_semihosting
},
7304 { "name", HAS_ARG
, QEMU_OPTION_name
},
7305 #if defined(TARGET_SPARC)
7306 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
7308 #if defined(TARGET_ARM)
7309 { "old-param", 0, QEMU_OPTION_old_param
},
7311 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
7315 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
7317 /* this stack is only used during signal handling */
7318 #define SIGNAL_STACK_SIZE 32768
7320 static uint8_t *signal_stack
;
7324 /* password input */
7326 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
7331 if (!bdrv_is_encrypted(bs
))
7334 term_printf("%s is encrypted.\n", name
);
7335 for(i
= 0; i
< 3; i
++) {
7336 monitor_readline("Password: ", 1, password
, sizeof(password
));
7337 if (bdrv_set_key(bs
, password
) == 0)
7339 term_printf("invalid password\n");
7344 static BlockDriverState
*get_bdrv(int index
)
7346 BlockDriverState
*bs
;
7349 bs
= bs_table
[index
];
7350 } else if (index
< 6) {
7351 bs
= fd_table
[index
- 4];
7358 static void read_passwords(void)
7360 BlockDriverState
*bs
;
7363 for(i
= 0; i
< 6; i
++) {
7366 qemu_key_check(bs
, bdrv_get_device_name(bs
));
7370 /* XXX: currently we cannot use simultaneously different CPUs */
7371 void register_machines(void)
7373 #if defined(TARGET_I386)
7374 qemu_register_machine(&pc_machine
);
7375 qemu_register_machine(&isapc_machine
);
7376 #elif defined(TARGET_PPC)
7377 qemu_register_machine(&heathrow_machine
);
7378 qemu_register_machine(&core99_machine
);
7379 qemu_register_machine(&prep_machine
);
7380 qemu_register_machine(&ref405ep_machine
);
7381 qemu_register_machine(&taihu_machine
);
7382 #elif defined(TARGET_MIPS)
7383 qemu_register_machine(&mips_machine
);
7384 qemu_register_machine(&mips_malta_machine
);
7385 qemu_register_machine(&mips_pica61_machine
);
7386 qemu_register_machine(&mips_mipssim_machine
);
7387 #elif defined(TARGET_SPARC)
7388 #ifdef TARGET_SPARC64
7389 qemu_register_machine(&sun4u_machine
);
7391 qemu_register_machine(&ss5_machine
);
7392 qemu_register_machine(&ss10_machine
);
7394 #elif defined(TARGET_ARM)
7395 qemu_register_machine(&integratorcp_machine
);
7396 qemu_register_machine(&versatilepb_machine
);
7397 qemu_register_machine(&versatileab_machine
);
7398 qemu_register_machine(&realview_machine
);
7399 qemu_register_machine(&akitapda_machine
);
7400 qemu_register_machine(&spitzpda_machine
);
7401 qemu_register_machine(&borzoipda_machine
);
7402 qemu_register_machine(&terrierpda_machine
);
7403 qemu_register_machine(&palmte_machine
);
7404 #elif defined(TARGET_SH4)
7405 qemu_register_machine(&shix_machine
);
7406 qemu_register_machine(&r2d_machine
);
7407 #elif defined(TARGET_ALPHA)
7409 #elif defined(TARGET_M68K)
7410 qemu_register_machine(&mcf5208evb_machine
);
7411 qemu_register_machine(&an5206_machine
);
7412 #elif defined(TARGET_CRIS)
7413 qemu_register_machine(&bareetraxfs_machine
);
7415 #error unsupported CPU
7420 struct soundhw soundhw
[] = {
7421 #ifdef HAS_AUDIO_CHOICE
7428 { .init_isa
= pcspk_audio_init
}
7433 "Creative Sound Blaster 16",
7436 { .init_isa
= SB16_init
}
7443 "Yamaha YMF262 (OPL3)",
7445 "Yamaha YM3812 (OPL2)",
7449 { .init_isa
= Adlib_init
}
7456 "Gravis Ultrasound GF1",
7459 { .init_isa
= GUS_init
}
7465 "ENSONIQ AudioPCI ES1370",
7468 { .init_pci
= es1370_init
}
7472 { NULL
, NULL
, 0, 0, { NULL
} }
7475 static void select_soundhw (const char *optarg
)
7479 if (*optarg
== '?') {
7482 printf ("Valid sound card names (comma separated):\n");
7483 for (c
= soundhw
; c
->name
; ++c
) {
7484 printf ("%-11s %s\n", c
->name
, c
->descr
);
7486 printf ("\n-soundhw all will enable all of the above\n");
7487 exit (*optarg
!= '?');
7495 if (!strcmp (optarg
, "all")) {
7496 for (c
= soundhw
; c
->name
; ++c
) {
7504 e
= strchr (p
, ',');
7505 l
= !e
? strlen (p
) : (size_t) (e
- p
);
7507 for (c
= soundhw
; c
->name
; ++c
) {
7508 if (!strncmp (c
->name
, p
, l
)) {
7517 "Unknown sound card name (too big to show)\n");
7520 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
7525 p
+= l
+ (e
!= NULL
);
7529 goto show_valid_cards
;
7535 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
7537 exit(STATUS_CONTROL_C_EXIT
);
7542 #define MAX_NET_CLIENTS 32
7544 int main(int argc
, char **argv
)
7546 #ifdef CONFIG_GDBSTUB
7548 const char *gdbstub_port
;
7550 int i
, cdrom_index
, pflash_index
;
7551 int snapshot
, linux_boot
;
7552 const char *initrd_filename
;
7553 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
7554 const char *pflash_filename
[MAX_PFLASH
];
7555 const char *sd_filename
;
7556 const char *mtd_filename
;
7557 const char *kernel_filename
, *kernel_cmdline
;
7558 DisplayState
*ds
= &display_state
;
7559 int cyls
, heads
, secs
, translation
;
7560 char net_clients
[MAX_NET_CLIENTS
][256];
7563 const char *r
, *optarg
;
7564 CharDriverState
*monitor_hd
;
7565 char monitor_device
[128];
7566 char serial_devices
[MAX_SERIAL_PORTS
][128];
7567 int serial_device_index
;
7568 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
7569 int parallel_device_index
;
7570 const char *loadvm
= NULL
;
7571 QEMUMachine
*machine
;
7572 const char *cpu_model
;
7573 char usb_devices
[MAX_USB_CMDLINE
][128];
7574 int usb_devices_index
;
7576 const char *pid_file
= NULL
;
7579 LIST_INIT (&vm_change_state_head
);
7582 struct sigaction act
;
7583 sigfillset(&act
.sa_mask
);
7585 act
.sa_handler
= SIG_IGN
;
7586 sigaction(SIGPIPE
, &act
, NULL
);
7589 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
7590 /* Note: cpu_interrupt() is currently not SMP safe, so we force
7591 QEMU to run on a single CPU */
7596 h
= GetCurrentProcess();
7597 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
7598 for(i
= 0; i
< 32; i
++) {
7599 if (mask
& (1 << i
))
7604 SetProcessAffinityMask(h
, mask
);
7610 register_machines();
7611 machine
= first_machine
;
7613 initrd_filename
= NULL
;
7614 for(i
= 0; i
< MAX_FD
; i
++)
7615 fd_filename
[i
] = NULL
;
7616 for(i
= 0; i
< MAX_DISKS
; i
++)
7617 hd_filename
[i
] = NULL
;
7618 for(i
= 0; i
< MAX_PFLASH
; i
++)
7619 pflash_filename
[i
] = NULL
;
7622 mtd_filename
= NULL
;
7623 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
7624 vga_ram_size
= VGA_RAM_SIZE
;
7625 #ifdef CONFIG_GDBSTUB
7627 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
7631 kernel_filename
= NULL
;
7632 kernel_cmdline
= "";
7638 cyls
= heads
= secs
= 0;
7639 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7640 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
7642 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
7643 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
7644 serial_devices
[i
][0] = '\0';
7645 serial_device_index
= 0;
7647 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
7648 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
7649 parallel_devices
[i
][0] = '\0';
7650 parallel_device_index
= 0;
7652 usb_devices_index
= 0;
7657 /* default mac address of the first network interface */
7665 hd_filename
[0] = argv
[optind
++];
7667 const QEMUOption
*popt
;
7670 /* Treat --foo the same as -foo. */
7673 popt
= qemu_options
;
7676 fprintf(stderr
, "%s: invalid option -- '%s'\n",
7680 if (!strcmp(popt
->name
, r
+ 1))
7684 if (popt
->flags
& HAS_ARG
) {
7685 if (optind
>= argc
) {
7686 fprintf(stderr
, "%s: option '%s' requires an argument\n",
7690 optarg
= argv
[optind
++];
7695 switch(popt
->index
) {
7697 machine
= find_machine(optarg
);
7700 printf("Supported machines are:\n");
7701 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
7702 printf("%-10s %s%s\n",
7704 m
== first_machine
? " (default)" : "");
7706 exit(*optarg
!= '?');
7709 case QEMU_OPTION_cpu
:
7710 /* hw initialization will check this */
7711 if (*optarg
== '?') {
7712 /* XXX: implement xxx_cpu_list for targets that still miss it */
7713 #if defined(cpu_list)
7714 cpu_list(stdout
, &fprintf
);
7721 case QEMU_OPTION_initrd
:
7722 initrd_filename
= optarg
;
7724 case QEMU_OPTION_hda
:
7725 case QEMU_OPTION_hdb
:
7726 case QEMU_OPTION_hdc
:
7727 case QEMU_OPTION_hdd
:
7730 hd_index
= popt
->index
- QEMU_OPTION_hda
;
7731 hd_filename
[hd_index
] = optarg
;
7732 if (hd_index
== cdrom_index
)
7736 case QEMU_OPTION_mtdblock
:
7737 mtd_filename
= optarg
;
7739 case QEMU_OPTION_sd
:
7740 sd_filename
= optarg
;
7742 case QEMU_OPTION_pflash
:
7743 if (pflash_index
>= MAX_PFLASH
) {
7744 fprintf(stderr
, "qemu: too many parallel flash images\n");
7747 pflash_filename
[pflash_index
++] = optarg
;
7749 case QEMU_OPTION_snapshot
:
7752 case QEMU_OPTION_hdachs
:
7756 cyls
= strtol(p
, (char **)&p
, 0);
7757 if (cyls
< 1 || cyls
> 16383)
7762 heads
= strtol(p
, (char **)&p
, 0);
7763 if (heads
< 1 || heads
> 16)
7768 secs
= strtol(p
, (char **)&p
, 0);
7769 if (secs
< 1 || secs
> 63)
7773 if (!strcmp(p
, "none"))
7774 translation
= BIOS_ATA_TRANSLATION_NONE
;
7775 else if (!strcmp(p
, "lba"))
7776 translation
= BIOS_ATA_TRANSLATION_LBA
;
7777 else if (!strcmp(p
, "auto"))
7778 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7781 } else if (*p
!= '\0') {
7783 fprintf(stderr
, "qemu: invalid physical CHS format\n");
7788 case QEMU_OPTION_nographic
:
7789 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
7790 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "null");
7791 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
7794 case QEMU_OPTION_portrait
:
7797 case QEMU_OPTION_kernel
:
7798 kernel_filename
= optarg
;
7800 case QEMU_OPTION_append
:
7801 kernel_cmdline
= optarg
;
7803 case QEMU_OPTION_cdrom
:
7804 if (cdrom_index
>= 0) {
7805 hd_filename
[cdrom_index
] = optarg
;
7808 case QEMU_OPTION_boot
:
7809 boot_device
= optarg
[0];
7810 if (boot_device
!= 'a' &&
7811 #if defined(TARGET_SPARC) || defined(TARGET_I386)
7813 boot_device
!= 'n' &&
7815 boot_device
!= 'c' && boot_device
!= 'd') {
7816 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
7820 case QEMU_OPTION_fda
:
7821 fd_filename
[0] = optarg
;
7823 case QEMU_OPTION_fdb
:
7824 fd_filename
[1] = optarg
;
7827 case QEMU_OPTION_no_fd_bootchk
:
7831 case QEMU_OPTION_no_code_copy
:
7832 code_copy_enabled
= 0;
7834 case QEMU_OPTION_net
:
7835 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
7836 fprintf(stderr
, "qemu: too many network clients\n");
7839 pstrcpy(net_clients
[nb_net_clients
],
7840 sizeof(net_clients
[0]),
7845 case QEMU_OPTION_tftp
:
7846 tftp_prefix
= optarg
;
7848 case QEMU_OPTION_bootp
:
7849 bootp_filename
= optarg
;
7852 case QEMU_OPTION_smb
:
7853 net_slirp_smb(optarg
);
7856 case QEMU_OPTION_redir
:
7857 net_slirp_redir(optarg
);
7861 case QEMU_OPTION_audio_help
:
7865 case QEMU_OPTION_soundhw
:
7866 select_soundhw (optarg
);
7873 ram_size
= atoi(optarg
) * 1024 * 1024;
7876 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
7877 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
7878 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
7887 mask
= cpu_str_to_log_mask(optarg
);
7889 printf("Log items (comma separated):\n");
7890 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
7891 printf("%-10s %s\n", item
->name
, item
->help
);
7898 #ifdef CONFIG_GDBSTUB
7903 gdbstub_port
= optarg
;
7909 case QEMU_OPTION_bios
:
7916 keyboard_layout
= optarg
;
7918 case QEMU_OPTION_localtime
:
7921 case QEMU_OPTION_cirrusvga
:
7922 cirrus_vga_enabled
= 1;
7925 case QEMU_OPTION_vmsvga
:
7926 cirrus_vga_enabled
= 0;
7929 case QEMU_OPTION_std_vga
:
7930 cirrus_vga_enabled
= 0;
7938 w
= strtol(p
, (char **)&p
, 10);
7941 fprintf(stderr
, "qemu: invalid resolution or depth\n");
7947 h
= strtol(p
, (char **)&p
, 10);
7952 depth
= strtol(p
, (char **)&p
, 10);
7953 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
7954 depth
!= 24 && depth
!= 32)
7956 } else if (*p
== '\0') {
7957 depth
= graphic_depth
;
7964 graphic_depth
= depth
;
7967 case QEMU_OPTION_echr
:
7970 term_escape_char
= strtol(optarg
, &r
, 0);
7972 printf("Bad argument to echr\n");
7975 case QEMU_OPTION_monitor
:
7976 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
7978 case QEMU_OPTION_serial
:
7979 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
7980 fprintf(stderr
, "qemu: too many serial ports\n");
7983 pstrcpy(serial_devices
[serial_device_index
],
7984 sizeof(serial_devices
[0]), optarg
);
7985 serial_device_index
++;
7987 case QEMU_OPTION_parallel
:
7988 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
7989 fprintf(stderr
, "qemu: too many parallel ports\n");
7992 pstrcpy(parallel_devices
[parallel_device_index
],
7993 sizeof(parallel_devices
[0]), optarg
);
7994 parallel_device_index
++;
7996 case QEMU_OPTION_loadvm
:
7999 case QEMU_OPTION_full_screen
:
8003 case QEMU_OPTION_no_frame
:
8006 case QEMU_OPTION_alt_grab
:
8009 case QEMU_OPTION_no_quit
:
8013 case QEMU_OPTION_pidfile
:
8017 case QEMU_OPTION_win2k_hack
:
8018 win2k_install_hack
= 1;
8022 case QEMU_OPTION_no_kqemu
:
8025 case QEMU_OPTION_kernel_kqemu
:
8029 case QEMU_OPTION_usb
:
8032 case QEMU_OPTION_usbdevice
:
8034 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
8035 fprintf(stderr
, "Too many USB devices\n");
8038 pstrcpy(usb_devices
[usb_devices_index
],
8039 sizeof(usb_devices
[usb_devices_index
]),
8041 usb_devices_index
++;
8043 case QEMU_OPTION_smp
:
8044 smp_cpus
= atoi(optarg
);
8045 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
8046 fprintf(stderr
, "Invalid number of CPUs\n");
8050 case QEMU_OPTION_vnc
:
8051 vnc_display
= optarg
;
8053 case QEMU_OPTION_no_acpi
:
8056 case QEMU_OPTION_no_reboot
:
8059 case QEMU_OPTION_show_cursor
:
8062 case QEMU_OPTION_daemonize
:
8065 case QEMU_OPTION_option_rom
:
8066 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
8067 fprintf(stderr
, "Too many option ROMs\n");
8070 option_rom
[nb_option_roms
] = optarg
;
8073 case QEMU_OPTION_semihosting
:
8074 semihosting_enabled
= 1;
8076 case QEMU_OPTION_name
:
8080 case QEMU_OPTION_prom_env
:
8081 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
8082 fprintf(stderr
, "Too many prom variables\n");
8085 prom_envs
[nb_prom_envs
] = optarg
;
8090 case QEMU_OPTION_old_param
:
8093 case QEMU_OPTION_clock
:
8094 configure_alarms(optarg
);
8101 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
8102 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
8109 if (pipe(fds
) == -1)
8120 len
= read(fds
[0], &status
, 1);
8121 if (len
== -1 && (errno
== EINTR
))
8126 else if (status
== 1) {
8127 fprintf(stderr
, "Could not acquire pidfile\n");
8145 signal(SIGTSTP
, SIG_IGN
);
8146 signal(SIGTTOU
, SIG_IGN
);
8147 signal(SIGTTIN
, SIG_IGN
);
8151 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
8154 write(fds
[1], &status
, 1);
8156 fprintf(stderr
, "Could not acquire pid file\n");
8164 linux_boot
= (kernel_filename
!= NULL
);
8167 boot_device
!= 'n' &&
8168 hd_filename
[0] == '\0' &&
8169 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
8170 fd_filename
[0] == '\0')
8173 /* boot to floppy or the default cd if no hard disk defined yet */
8174 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
8175 if (fd_filename
[0] != '\0')
8181 setvbuf(stdout
, NULL
, _IOLBF
, 0);
8191 /* init network clients */
8192 if (nb_net_clients
== 0) {
8193 /* if no clients, we use a default config */
8194 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
8196 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
8201 for(i
= 0;i
< nb_net_clients
; i
++) {
8202 if (net_client_init(net_clients
[i
]) < 0)
8205 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
8206 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
8208 if (vlan
->nb_guest_devs
== 0) {
8209 fprintf(stderr
, "Invalid vlan (%d) with no nics\n", vlan
->id
);
8212 if (vlan
->nb_host_devs
== 0)
8214 "Warning: vlan %d is not connected to host network\n",
8219 if (boot_device
== 'n') {
8220 for (i
= 0; i
< nb_nics
; i
++) {
8221 const char *model
= nd_table
[i
].model
;
8225 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
8226 if (get_image_size(buf
) > 0) {
8227 option_rom
[nb_option_roms
] = strdup(buf
);
8233 fprintf(stderr
, "No valid PXE rom found for network device\n");
8239 /* init the memory */
8240 phys_ram_size
= ram_size
+ vga_ram_size
+ MAX_BIOS_SIZE
;
8242 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
8243 if (!phys_ram_base
) {
8244 fprintf(stderr
, "Could not allocate physical memory\n");
8248 /* we always create the cdrom drive, even if no disk is there */
8250 if (cdrom_index
>= 0) {
8251 bs_table
[cdrom_index
] = bdrv_new("cdrom");
8252 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
8255 /* open the virtual block devices */
8256 for(i
= 0; i
< MAX_DISKS
; i
++) {
8257 if (hd_filename
[i
]) {
8260 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
8261 bs_table
[i
] = bdrv_new(buf
);
8263 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8264 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
8268 if (i
== 0 && cyls
!= 0) {
8269 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
8270 bdrv_set_translation_hint(bs_table
[i
], translation
);
8275 /* we always create at least one floppy disk */
8276 fd_table
[0] = bdrv_new("fda");
8277 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
8279 for(i
= 0; i
< MAX_FD
; i
++) {
8280 if (fd_filename
[i
]) {
8283 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
8284 fd_table
[i
] = bdrv_new(buf
);
8285 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
8287 if (fd_filename
[i
][0] != '\0') {
8288 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
8289 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8290 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
8298 /* Open the virtual parallel flash block devices */
8299 for(i
= 0; i
< MAX_PFLASH
; i
++) {
8300 if (pflash_filename
[i
]) {
8301 if (!pflash_table
[i
]) {
8303 snprintf(buf
, sizeof(buf
), "fl%c", i
+ 'a');
8304 pflash_table
[i
] = bdrv_new(buf
);
8306 if (bdrv_open(pflash_table
[i
], pflash_filename
[i
],
8307 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8308 fprintf(stderr
, "qemu: could not open flash image '%s'\n",
8309 pflash_filename
[i
]);
8315 sd_bdrv
= bdrv_new ("sd");
8316 /* FIXME: This isn't really a floppy, but it's a reasonable
8318 bdrv_set_type_hint(sd_bdrv
, BDRV_TYPE_FLOPPY
);
8320 if (bdrv_open(sd_bdrv
, sd_filename
,
8321 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8322 fprintf(stderr
, "qemu: could not open SD card image %s\n",
8325 qemu_key_check(sd_bdrv
, sd_filename
);
8329 mtd_bdrv
= bdrv_new ("mtd");
8330 if (bdrv_open(mtd_bdrv
, mtd_filename
,
8331 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0 ||
8332 qemu_key_check(mtd_bdrv
, mtd_filename
)) {
8333 fprintf(stderr
, "qemu: could not open Flash image %s\n",
8335 bdrv_delete(mtd_bdrv
);
8340 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
8341 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
8346 memset(&display_state
, 0, sizeof(display_state
));
8348 /* nearly nothing to do */
8349 dumb_display_init(ds
);
8350 } else if (vnc_display
!= NULL
) {
8351 vnc_display_init(ds
);
8352 if (vnc_display_open(ds
, vnc_display
) < 0)
8355 #if defined(CONFIG_SDL)
8356 sdl_display_init(ds
, full_screen
, no_frame
);
8357 #elif defined(CONFIG_COCOA)
8358 cocoa_display_init(ds
, full_screen
);
8362 /* Maintain compatibility with multiple stdio monitors */
8363 if (!strcmp(monitor_device
,"stdio")) {
8364 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
8365 if (!strcmp(serial_devices
[i
],"mon:stdio")) {
8366 monitor_device
[0] = '\0';
8368 } else if (!strcmp(serial_devices
[i
],"stdio")) {
8369 monitor_device
[0] = '\0';
8370 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "mon:stdio");
8375 if (monitor_device
[0] != '\0') {
8376 monitor_hd
= qemu_chr_open(monitor_device
);
8378 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
8381 monitor_init(monitor_hd
, !nographic
);
8384 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
8385 const char *devname
= serial_devices
[i
];
8386 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
8387 serial_hds
[i
] = qemu_chr_open(devname
);
8388 if (!serial_hds
[i
]) {
8389 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
8393 if (strstart(devname
, "vc", 0))
8394 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
8398 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
8399 const char *devname
= parallel_devices
[i
];
8400 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
8401 parallel_hds
[i
] = qemu_chr_open(devname
);
8402 if (!parallel_hds
[i
]) {
8403 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
8407 if (strstart(devname
, "vc", 0))
8408 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
8412 machine
->init(ram_size
, vga_ram_size
, boot_device
,
8413 ds
, fd_filename
, snapshot
,
8414 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
8416 /* init USB devices */
8418 for(i
= 0; i
< usb_devices_index
; i
++) {
8419 if (usb_device_add(usb_devices
[i
]) < 0) {
8420 fprintf(stderr
, "Warning: could not add USB device %s\n",
8426 if (display_state
.dpy_refresh
) {
8427 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
8428 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
8431 #ifdef CONFIG_GDBSTUB
8433 /* XXX: use standard host:port notation and modify options
8435 if (gdbserver_start(gdbstub_port
) < 0) {
8436 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
8447 /* XXX: simplify init */
8460 len
= write(fds
[1], &status
, 1);
8461 if (len
== -1 && (errno
== EINTR
))
8467 TFR(fd
= open("/dev/null", O_RDWR
));
8481 #if !defined(_WIN32)
8482 /* close network clients */
8483 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
8484 VLANClientState
*vc
;
8486 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
8487 if (vc
->fd_read
== tap_receive
) {
8489 TAPState
*s
= vc
->opaque
;
8491 if (sscanf(vc
->info_str
, "tap: ifname=%63s ", ifname
) == 1 &&
8493 launch_script(s
->down_script
, ifname
, s
->fd
);