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>
53 #include <linux/if_tun.h>
56 #include <linux/rtc.h>
57 #include <linux/ppdev.h>
58 #include <linux/parport.h>
61 #include <sys/ethernet.h>
62 #include <sys/sockio.h>
63 #include <arpa/inet.h>
64 #include <netinet/arp.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/ip.h>
68 #include <netinet/ip_icmp.h> // must come after ip.h
69 #include <netinet/udp.h>
70 #include <netinet/tcp.h>
78 #if defined(CONFIG_SLIRP)
84 #include <sys/timeb.h>
86 #define getopt_long_only getopt_long
87 #define memalign(align, size) malloc(size)
90 #include "qemu_socket.h"
96 #endif /* CONFIG_SDL */
100 #define main qemu_main
101 #endif /* CONFIG_COCOA */
105 #include "exec-all.h"
107 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
109 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
111 #define SMBD_COMMAND "/usr/sbin/smbd"
114 //#define DEBUG_UNUSED_IOPORT
115 //#define DEBUG_IOPORT
117 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
120 #define DEFAULT_RAM_SIZE 144
122 #define DEFAULT_RAM_SIZE 128
125 #define GUI_REFRESH_INTERVAL 30
127 /* Max number of USB devices that can be specified on the commandline. */
128 #define MAX_USB_CMDLINE 8
130 /* XXX: use a two level table to limit memory usage */
131 #define MAX_IOPORTS 65536
133 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
134 char phys_ram_file
[1024];
135 void *ioport_opaque
[MAX_IOPORTS
];
136 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
137 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
138 /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available
139 to store the VM snapshots */
140 BlockDriverState
*bs_table
[MAX_DISKS
+ 1], *fd_table
[MAX_FD
];
141 BlockDriverState
*sd_bdrv
;
142 /* point to the block driver where the snapshots are managed */
143 BlockDriverState
*bs_snapshots
;
145 static DisplayState display_state
;
147 const char* keyboard_layout
= NULL
;
148 int64_t ticks_per_sec
;
149 int boot_device
= 'c';
151 int pit_min_timer_count
= 0;
153 NICInfo nd_table
[MAX_NICS
];
154 QEMUTimer
*gui_timer
;
157 int cirrus_vga_enabled
= 1;
158 int vmsvga_enabled
= 0;
160 int graphic_width
= 1024;
161 int graphic_height
= 768;
163 int graphic_width
= 800;
164 int graphic_height
= 600;
166 int graphic_depth
= 15;
170 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
171 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
173 int win2k_install_hack
= 0;
176 static VLANState
*first_vlan
;
178 const char *vnc_display
;
179 #if defined(TARGET_SPARC)
181 #elif defined(TARGET_I386)
186 int acpi_enabled
= 1;
190 const char *option_rom
[MAX_OPTION_ROMS
];
192 int semihosting_enabled
= 0;
194 const char *qemu_name
;
196 /***********************************************************/
197 /* x86 ISA bus support */
199 target_phys_addr_t isa_mem_base
= 0;
202 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
204 #ifdef DEBUG_UNUSED_IOPORT
205 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
210 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
212 #ifdef DEBUG_UNUSED_IOPORT
213 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
217 /* default is to make two byte accesses */
218 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
221 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
222 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
223 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
227 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
229 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
230 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
231 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
234 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
236 #ifdef DEBUG_UNUSED_IOPORT
237 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
242 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
244 #ifdef DEBUG_UNUSED_IOPORT
245 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
249 void init_ioports(void)
253 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
254 ioport_read_table
[0][i
] = default_ioport_readb
;
255 ioport_write_table
[0][i
] = default_ioport_writeb
;
256 ioport_read_table
[1][i
] = default_ioport_readw
;
257 ioport_write_table
[1][i
] = default_ioport_writew
;
258 ioport_read_table
[2][i
] = default_ioport_readl
;
259 ioport_write_table
[2][i
] = default_ioport_writel
;
263 /* size is the word size in byte */
264 int register_ioport_read(int start
, int length
, int size
,
265 IOPortReadFunc
*func
, void *opaque
)
271 } else if (size
== 2) {
273 } else if (size
== 4) {
276 hw_error("register_ioport_read: invalid size");
279 for(i
= start
; i
< start
+ length
; i
+= size
) {
280 ioport_read_table
[bsize
][i
] = func
;
281 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
282 hw_error("register_ioport_read: invalid opaque");
283 ioport_opaque
[i
] = opaque
;
288 /* size is the word size in byte */
289 int register_ioport_write(int start
, int length
, int size
,
290 IOPortWriteFunc
*func
, void *opaque
)
296 } else if (size
== 2) {
298 } else if (size
== 4) {
301 hw_error("register_ioport_write: invalid size");
304 for(i
= start
; i
< start
+ length
; i
+= size
) {
305 ioport_write_table
[bsize
][i
] = func
;
306 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
307 hw_error("register_ioport_write: invalid opaque");
308 ioport_opaque
[i
] = opaque
;
313 void isa_unassign_ioport(int start
, int length
)
317 for(i
= start
; i
< start
+ length
; i
++) {
318 ioport_read_table
[0][i
] = default_ioport_readb
;
319 ioport_read_table
[1][i
] = default_ioport_readw
;
320 ioport_read_table
[2][i
] = default_ioport_readl
;
322 ioport_write_table
[0][i
] = default_ioport_writeb
;
323 ioport_write_table
[1][i
] = default_ioport_writew
;
324 ioport_write_table
[2][i
] = default_ioport_writel
;
328 /***********************************************************/
330 void cpu_outb(CPUState
*env
, int addr
, int val
)
333 if (loglevel
& CPU_LOG_IOPORT
)
334 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
336 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
339 env
->last_io_time
= cpu_get_time_fast();
343 void cpu_outw(CPUState
*env
, int addr
, int val
)
346 if (loglevel
& CPU_LOG_IOPORT
)
347 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
349 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
352 env
->last_io_time
= cpu_get_time_fast();
356 void cpu_outl(CPUState
*env
, int addr
, int val
)
359 if (loglevel
& CPU_LOG_IOPORT
)
360 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
362 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
365 env
->last_io_time
= cpu_get_time_fast();
369 int cpu_inb(CPUState
*env
, int addr
)
372 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
374 if (loglevel
& CPU_LOG_IOPORT
)
375 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
379 env
->last_io_time
= cpu_get_time_fast();
384 int cpu_inw(CPUState
*env
, int addr
)
387 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
389 if (loglevel
& CPU_LOG_IOPORT
)
390 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
394 env
->last_io_time
= cpu_get_time_fast();
399 int cpu_inl(CPUState
*env
, int addr
)
402 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
404 if (loglevel
& CPU_LOG_IOPORT
)
405 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
409 env
->last_io_time
= cpu_get_time_fast();
414 /***********************************************************/
415 void hw_error(const char *fmt
, ...)
421 fprintf(stderr
, "qemu: hardware error: ");
422 vfprintf(stderr
, fmt
, ap
);
423 fprintf(stderr
, "\n");
424 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
425 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
427 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
429 cpu_dump_state(env
, stderr
, fprintf
, 0);
436 /***********************************************************/
439 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
440 static void *qemu_put_kbd_event_opaque
;
441 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
442 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
444 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
446 qemu_put_kbd_event_opaque
= opaque
;
447 qemu_put_kbd_event
= func
;
450 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
451 void *opaque
, int absolute
,
454 QEMUPutMouseEntry
*s
, *cursor
;
456 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
460 s
->qemu_put_mouse_event
= func
;
461 s
->qemu_put_mouse_event_opaque
= opaque
;
462 s
->qemu_put_mouse_event_absolute
= absolute
;
463 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
466 if (!qemu_put_mouse_event_head
) {
467 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
471 cursor
= qemu_put_mouse_event_head
;
472 while (cursor
->next
!= NULL
)
473 cursor
= cursor
->next
;
476 qemu_put_mouse_event_current
= s
;
481 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
483 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
485 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
488 cursor
= qemu_put_mouse_event_head
;
489 while (cursor
!= NULL
&& cursor
!= entry
) {
491 cursor
= cursor
->next
;
494 if (cursor
== NULL
) // does not exist or list empty
496 else if (prev
== NULL
) { // entry is head
497 qemu_put_mouse_event_head
= cursor
->next
;
498 if (qemu_put_mouse_event_current
== entry
)
499 qemu_put_mouse_event_current
= cursor
->next
;
500 qemu_free(entry
->qemu_put_mouse_event_name
);
505 prev
->next
= entry
->next
;
507 if (qemu_put_mouse_event_current
== entry
)
508 qemu_put_mouse_event_current
= prev
;
510 qemu_free(entry
->qemu_put_mouse_event_name
);
514 void kbd_put_keycode(int keycode
)
516 if (qemu_put_kbd_event
) {
517 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
521 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
523 QEMUPutMouseEvent
*mouse_event
;
524 void *mouse_event_opaque
;
526 if (!qemu_put_mouse_event_current
) {
531 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
533 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
536 mouse_event(mouse_event_opaque
, dx
, dy
, dz
, buttons_state
);
540 int kbd_mouse_is_absolute(void)
542 if (!qemu_put_mouse_event_current
)
545 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
548 void (*kbd_mouse_set
)(int x
, int y
, int on
) = NULL
;
549 void (*kbd_cursor_define
)(int width
, int height
, int bpp
, int hot_x
, int hot_y
,
550 uint8_t *image
, uint8_t *mask
) = NULL
;
552 void do_info_mice(void)
554 QEMUPutMouseEntry
*cursor
;
557 if (!qemu_put_mouse_event_head
) {
558 term_printf("No mouse devices connected\n");
562 term_printf("Mouse devices available:\n");
563 cursor
= qemu_put_mouse_event_head
;
564 while (cursor
!= NULL
) {
565 term_printf("%c Mouse #%d: %s\n",
566 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
567 index
, cursor
->qemu_put_mouse_event_name
);
569 cursor
= cursor
->next
;
573 void do_mouse_set(int index
)
575 QEMUPutMouseEntry
*cursor
;
578 if (!qemu_put_mouse_event_head
) {
579 term_printf("No mouse devices connected\n");
583 cursor
= qemu_put_mouse_event_head
;
584 while (cursor
!= NULL
&& index
!= i
) {
586 cursor
= cursor
->next
;
590 qemu_put_mouse_event_current
= cursor
;
592 term_printf("Mouse at given index not found\n");
595 /* compute with 96 bit intermediate result: (a*b)/c */
596 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
601 #ifdef WORDS_BIGENDIAN
611 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
612 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
615 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
619 /***********************************************************/
620 /* real time host monotonic timer */
622 #define QEMU_TIMER_BASE 1000000000LL
626 static int64_t clock_freq
;
628 static void init_get_clock(void)
632 ret
= QueryPerformanceFrequency(&freq
);
634 fprintf(stderr
, "Could not calibrate ticks\n");
637 clock_freq
= freq
.QuadPart
;
640 static int64_t get_clock(void)
643 QueryPerformanceCounter(&ti
);
644 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
649 static int use_rt_clock
;
651 static void init_get_clock(void)
654 #if defined(__linux__)
657 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
664 static int64_t get_clock(void)
666 #if defined(__linux__)
669 clock_gettime(CLOCK_MONOTONIC
, &ts
);
670 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
674 /* XXX: using gettimeofday leads to problems if the date
675 changes, so it should be avoided. */
677 gettimeofday(&tv
, NULL
);
678 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
684 /***********************************************************/
685 /* guest cycle counter */
687 static int64_t cpu_ticks_prev
;
688 static int64_t cpu_ticks_offset
;
689 static int64_t cpu_clock_offset
;
690 static int cpu_ticks_enabled
;
692 /* return the host CPU cycle counter and handle stop/restart */
693 int64_t cpu_get_ticks(void)
695 if (!cpu_ticks_enabled
) {
696 return cpu_ticks_offset
;
699 ticks
= cpu_get_real_ticks();
700 if (cpu_ticks_prev
> ticks
) {
701 /* Note: non increasing ticks may happen if the host uses
703 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
705 cpu_ticks_prev
= ticks
;
706 return ticks
+ cpu_ticks_offset
;
710 /* return the host CPU monotonic timer and handle stop/restart */
711 static int64_t cpu_get_clock(void)
714 if (!cpu_ticks_enabled
) {
715 return cpu_clock_offset
;
718 return ti
+ cpu_clock_offset
;
722 /* enable cpu_get_ticks() */
723 void cpu_enable_ticks(void)
725 if (!cpu_ticks_enabled
) {
726 cpu_ticks_offset
-= cpu_get_real_ticks();
727 cpu_clock_offset
-= get_clock();
728 cpu_ticks_enabled
= 1;
732 /* disable cpu_get_ticks() : the clock is stopped. You must not call
733 cpu_get_ticks() after that. */
734 void cpu_disable_ticks(void)
736 if (cpu_ticks_enabled
) {
737 cpu_ticks_offset
= cpu_get_ticks();
738 cpu_clock_offset
= cpu_get_clock();
739 cpu_ticks_enabled
= 0;
743 /***********************************************************/
746 #define QEMU_TIMER_REALTIME 0
747 #define QEMU_TIMER_VIRTUAL 1
751 /* XXX: add frequency */
759 struct QEMUTimer
*next
;
765 static QEMUTimer
*active_timers
[2];
767 static MMRESULT timerID
;
768 static HANDLE host_alarm
= NULL
;
769 static unsigned int period
= 1;
771 /* frequency of the times() clock tick */
772 static int timer_freq
;
775 QEMUClock
*qemu_new_clock(int type
)
778 clock
= qemu_mallocz(sizeof(QEMUClock
));
785 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
789 ts
= qemu_mallocz(sizeof(QEMUTimer
));
796 void qemu_free_timer(QEMUTimer
*ts
)
801 /* stop a timer, but do not dealloc it */
802 void qemu_del_timer(QEMUTimer
*ts
)
806 /* NOTE: this code must be signal safe because
807 qemu_timer_expired() can be called from a signal. */
808 pt
= &active_timers
[ts
->clock
->type
];
821 /* modify the current timer so that it will be fired when current_time
822 >= expire_time. The corresponding callback will be called. */
823 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
829 /* add the timer in the sorted list */
830 /* NOTE: this code must be signal safe because
831 qemu_timer_expired() can be called from a signal. */
832 pt
= &active_timers
[ts
->clock
->type
];
837 if (t
->expire_time
> expire_time
)
841 ts
->expire_time
= expire_time
;
846 int qemu_timer_pending(QEMUTimer
*ts
)
849 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
856 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
860 return (timer_head
->expire_time
<= current_time
);
863 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
869 if (!ts
|| ts
->expire_time
> current_time
)
871 /* remove timer from the list before calling the callback */
872 *ptimer_head
= ts
->next
;
875 /* run the callback (the timer list can be modified) */
880 int64_t qemu_get_clock(QEMUClock
*clock
)
882 switch(clock
->type
) {
883 case QEMU_TIMER_REALTIME
:
884 return get_clock() / 1000000;
886 case QEMU_TIMER_VIRTUAL
:
887 return cpu_get_clock();
891 static void init_timers(void)
894 ticks_per_sec
= QEMU_TIMER_BASE
;
895 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
896 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
900 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
902 uint64_t expire_time
;
904 if (qemu_timer_pending(ts
)) {
905 expire_time
= ts
->expire_time
;
909 qemu_put_be64(f
, expire_time
);
912 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
914 uint64_t expire_time
;
916 expire_time
= qemu_get_be64(f
);
917 if (expire_time
!= -1) {
918 qemu_mod_timer(ts
, expire_time
);
924 static void timer_save(QEMUFile
*f
, void *opaque
)
926 if (cpu_ticks_enabled
) {
927 hw_error("cannot save state if virtual timers are running");
929 qemu_put_be64s(f
, &cpu_ticks_offset
);
930 qemu_put_be64s(f
, &ticks_per_sec
);
931 qemu_put_be64s(f
, &cpu_clock_offset
);
934 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
936 if (version_id
!= 1 && version_id
!= 2)
938 if (cpu_ticks_enabled
) {
941 qemu_get_be64s(f
, &cpu_ticks_offset
);
942 qemu_get_be64s(f
, &ticks_per_sec
);
943 if (version_id
== 2) {
944 qemu_get_be64s(f
, &cpu_clock_offset
);
950 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
951 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
953 static void host_alarm_handler(int host_signum
)
957 #define DISP_FREQ 1000
959 static int64_t delta_min
= INT64_MAX
;
960 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
962 ti
= qemu_get_clock(vm_clock
);
963 if (last_clock
!= 0) {
964 delta
= ti
- last_clock
;
965 if (delta
< delta_min
)
967 if (delta
> delta_max
)
970 if (++count
== DISP_FREQ
) {
971 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
972 muldiv64(delta_min
, 1000000, ticks_per_sec
),
973 muldiv64(delta_max
, 1000000, ticks_per_sec
),
974 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
975 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
977 delta_min
= INT64_MAX
;
985 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
986 qemu_get_clock(vm_clock
)) ||
987 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
988 qemu_get_clock(rt_clock
))) {
990 SetEvent(host_alarm
);
992 CPUState
*env
= cpu_single_env
;
994 /* stop the currently executing cpu because a timer occured */
995 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
997 if (env
->kqemu_enabled
) {
998 kqemu_cpu_interrupt(env
);
1007 #if defined(__linux__)
1009 #define RTC_FREQ 1024
1013 static int start_rtc_timer(void)
1015 rtc_fd
= open("/dev/rtc", O_RDONLY
);
1018 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1019 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1020 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1021 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1024 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1029 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
1035 static int start_rtc_timer(void)
1040 #endif /* !defined(__linux__) */
1042 #endif /* !defined(_WIN32) */
1044 static void init_timer_alarm(void)
1051 ZeroMemory(&tc
, sizeof(TIMECAPS
));
1052 timeGetDevCaps(&tc
, sizeof(TIMECAPS
));
1053 if (period
< tc
.wPeriodMin
)
1054 period
= tc
.wPeriodMin
;
1055 timeBeginPeriod(period
);
1056 timerID
= timeSetEvent(1, // interval (ms)
1057 period
, // resolution
1058 host_alarm_handler
, // function
1059 (DWORD
)&count
, // user parameter
1060 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
1062 perror("failed timer alarm");
1065 host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1067 perror("failed CreateEvent");
1070 qemu_add_wait_object(host_alarm
, NULL
, NULL
);
1072 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
1075 struct sigaction act
;
1076 struct itimerval itv
;
1078 /* get times() syscall frequency */
1079 timer_freq
= sysconf(_SC_CLK_TCK
);
1082 sigfillset(&act
.sa_mask
);
1084 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1085 act
.sa_flags
|= SA_ONSTACK
;
1087 act
.sa_handler
= host_alarm_handler
;
1088 sigaction(SIGALRM
, &act
, NULL
);
1090 itv
.it_interval
.tv_sec
= 0;
1091 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
1092 itv
.it_value
.tv_sec
= 0;
1093 itv
.it_value
.tv_usec
= 10 * 1000;
1094 setitimer(ITIMER_REAL
, &itv
, NULL
);
1095 /* we probe the tick duration of the kernel to inform the user if
1096 the emulated kernel requested a too high timer frequency */
1097 getitimer(ITIMER_REAL
, &itv
);
1099 #if defined(__linux__)
1100 /* XXX: force /dev/rtc usage because even 2.6 kernels may not
1101 have timers with 1 ms resolution. The correct solution will
1102 be to use the POSIX real time timers available in recent
1104 if (itv
.it_interval
.tv_usec
> 1000 || 1) {
1105 /* try to use /dev/rtc to have a faster timer */
1106 if (start_rtc_timer() < 0)
1108 /* disable itimer */
1109 itv
.it_interval
.tv_sec
= 0;
1110 itv
.it_interval
.tv_usec
= 0;
1111 itv
.it_value
.tv_sec
= 0;
1112 itv
.it_value
.tv_usec
= 0;
1113 setitimer(ITIMER_REAL
, &itv
, NULL
);
1116 sigaction(SIGIO
, &act
, NULL
);
1117 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1118 fcntl(rtc_fd
, F_SETOWN
, getpid());
1120 #endif /* defined(__linux__) */
1123 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1124 PIT_FREQ
) / 1000000;
1130 void quit_timers(void)
1133 timeKillEvent(timerID
);
1134 timeEndPeriod(period
);
1136 CloseHandle(host_alarm
);
1142 /***********************************************************/
1143 /* character device */
1145 static void qemu_chr_event(CharDriverState
*s
, int event
)
1149 s
->chr_event(s
->handler_opaque
, event
);
1152 static void qemu_chr_reset_bh(void *opaque
)
1154 CharDriverState
*s
= opaque
;
1155 qemu_chr_event(s
, CHR_EVENT_RESET
);
1156 qemu_bh_delete(s
->bh
);
1160 void qemu_chr_reset(CharDriverState
*s
)
1162 if (s
->bh
== NULL
) {
1163 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
1164 qemu_bh_schedule(s
->bh
);
1168 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1170 return s
->chr_write(s
, buf
, len
);
1173 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1177 return s
->chr_ioctl(s
, cmd
, arg
);
1180 int qemu_chr_can_read(CharDriverState
*s
)
1182 if (!s
->chr_can_read
)
1184 return s
->chr_can_read(s
->handler_opaque
);
1187 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
1189 s
->chr_read(s
->handler_opaque
, buf
, len
);
1193 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1198 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1199 qemu_chr_write(s
, buf
, strlen(buf
));
1203 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1205 if (s
->chr_send_event
)
1206 s
->chr_send_event(s
, event
);
1209 void qemu_chr_add_handlers(CharDriverState
*s
,
1210 IOCanRWHandler
*fd_can_read
,
1211 IOReadHandler
*fd_read
,
1212 IOEventHandler
*fd_event
,
1215 s
->chr_can_read
= fd_can_read
;
1216 s
->chr_read
= fd_read
;
1217 s
->chr_event
= fd_event
;
1218 s
->handler_opaque
= opaque
;
1219 if (s
->chr_update_read_handler
)
1220 s
->chr_update_read_handler(s
);
1223 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1228 static CharDriverState
*qemu_chr_open_null(void)
1230 CharDriverState
*chr
;
1232 chr
= qemu_mallocz(sizeof(CharDriverState
));
1235 chr
->chr_write
= null_chr_write
;
1239 /* MUX driver for serial I/O splitting */
1240 static int term_timestamps
;
1241 static int64_t term_timestamps_start
;
1244 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
1245 IOReadHandler
*chr_read
[MAX_MUX
];
1246 IOEventHandler
*chr_event
[MAX_MUX
];
1247 void *ext_opaque
[MAX_MUX
];
1248 CharDriverState
*drv
;
1250 int term_got_escape
;
1255 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1257 MuxDriver
*d
= chr
->opaque
;
1259 if (!term_timestamps
) {
1260 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
1265 for(i
= 0; i
< len
; i
++) {
1266 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
1267 if (buf
[i
] == '\n') {
1273 if (term_timestamps_start
== -1)
1274 term_timestamps_start
= ti
;
1275 ti
-= term_timestamps_start
;
1276 secs
= ti
/ 1000000000;
1277 snprintf(buf1
, sizeof(buf1
),
1278 "[%02d:%02d:%02d.%03d] ",
1282 (int)((ti
/ 1000000) % 1000));
1283 d
->drv
->chr_write(d
->drv
, buf1
, strlen(buf1
));
1290 static char *mux_help
[] = {
1291 "% h print this help\n\r",
1292 "% x exit emulator\n\r",
1293 "% s save disk data back to file (if -snapshot)\n\r",
1294 "% t toggle console timestamps\n\r"
1295 "% b send break (magic sysrq)\n\r",
1296 "% c switch between console and monitor\n\r",
1301 static int term_escape_char
= 0x01; /* ctrl-a is used for escape */
1302 static void mux_print_help(CharDriverState
*chr
)
1305 char ebuf
[15] = "Escape-Char";
1306 char cbuf
[50] = "\n\r";
1308 if (term_escape_char
> 0 && term_escape_char
< 26) {
1309 sprintf(cbuf
,"\n\r");
1310 sprintf(ebuf
,"C-%c", term_escape_char
- 1 + 'a');
1312 sprintf(cbuf
,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char
);
1314 chr
->chr_write(chr
, cbuf
, strlen(cbuf
));
1315 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
1316 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
1317 if (mux_help
[i
][j
] == '%')
1318 chr
->chr_write(chr
, ebuf
, strlen(ebuf
));
1320 chr
->chr_write(chr
, &mux_help
[i
][j
], 1);
1325 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
1327 if (d
->term_got_escape
) {
1328 d
->term_got_escape
= 0;
1329 if (ch
== term_escape_char
)
1334 mux_print_help(chr
);
1338 char *term
= "QEMU: Terminated\n\r";
1339 chr
->chr_write(chr
,term
,strlen(term
));
1346 for (i
= 0; i
< MAX_DISKS
; i
++) {
1348 bdrv_commit(bs_table
[i
]);
1354 chr
->chr_event(chr
->opaque
, CHR_EVENT_BREAK
);
1357 /* Switch to the next registered device */
1359 if (chr
->focus
>= d
->mux_cnt
)
1363 term_timestamps
= !term_timestamps
;
1364 term_timestamps_start
= -1;
1367 } else if (ch
== term_escape_char
) {
1368 d
->term_got_escape
= 1;
1376 static int mux_chr_can_read(void *opaque
)
1378 CharDriverState
*chr
= opaque
;
1379 MuxDriver
*d
= chr
->opaque
;
1380 if (d
->chr_can_read
[chr
->focus
])
1381 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
1385 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
1387 CharDriverState
*chr
= opaque
;
1388 MuxDriver
*d
= chr
->opaque
;
1390 for(i
= 0; i
< size
; i
++)
1391 if (mux_proc_byte(chr
, d
, buf
[i
]))
1392 d
->chr_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
], &buf
[i
], 1);
1395 static void mux_chr_event(void *opaque
, int event
)
1397 CharDriverState
*chr
= opaque
;
1398 MuxDriver
*d
= chr
->opaque
;
1401 /* Send the event to all registered listeners */
1402 for (i
= 0; i
< d
->mux_cnt
; i
++)
1403 if (d
->chr_event
[i
])
1404 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
1407 static void mux_chr_update_read_handler(CharDriverState
*chr
)
1409 MuxDriver
*d
= chr
->opaque
;
1411 if (d
->mux_cnt
>= MAX_MUX
) {
1412 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
1415 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
1416 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
1417 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
1418 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
1419 /* Fix up the real driver with mux routines */
1420 if (d
->mux_cnt
== 0) {
1421 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
1422 mux_chr_event
, chr
);
1424 chr
->focus
= d
->mux_cnt
;
1428 CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
1430 CharDriverState
*chr
;
1433 chr
= qemu_mallocz(sizeof(CharDriverState
));
1436 d
= qemu_mallocz(sizeof(MuxDriver
));
1445 chr
->chr_write
= mux_chr_write
;
1446 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
1453 static void socket_cleanup(void)
1458 static int socket_init(void)
1463 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1465 err
= WSAGetLastError();
1466 fprintf(stderr
, "WSAStartup: %d\n", err
);
1469 atexit(socket_cleanup
);
1473 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1479 ret
= send(fd
, buf
, len
, 0);
1482 errno
= WSAGetLastError();
1483 if (errno
!= WSAEWOULDBLOCK
) {
1486 } else if (ret
== 0) {
1496 void socket_set_nonblock(int fd
)
1498 unsigned long opt
= 1;
1499 ioctlsocket(fd
, FIONBIO
, &opt
);
1504 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1510 ret
= write(fd
, buf
, len
);
1512 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1514 } else if (ret
== 0) {
1524 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1526 return unix_write(fd
, buf
, len1
);
1529 void socket_set_nonblock(int fd
)
1531 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1533 #endif /* !_WIN32 */
1542 #define STDIO_MAX_CLIENTS 1
1543 static int stdio_nb_clients
= 0;
1545 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1547 FDCharDriver
*s
= chr
->opaque
;
1548 return unix_write(s
->fd_out
, buf
, len
);
1551 static int fd_chr_read_poll(void *opaque
)
1553 CharDriverState
*chr
= opaque
;
1554 FDCharDriver
*s
= chr
->opaque
;
1556 s
->max_size
= qemu_chr_can_read(chr
);
1560 static void fd_chr_read(void *opaque
)
1562 CharDriverState
*chr
= opaque
;
1563 FDCharDriver
*s
= chr
->opaque
;
1568 if (len
> s
->max_size
)
1572 size
= read(s
->fd_in
, buf
, len
);
1574 /* FD has been closed. Remove it from the active list. */
1575 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1579 qemu_chr_read(chr
, buf
, size
);
1583 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1585 FDCharDriver
*s
= chr
->opaque
;
1587 if (s
->fd_in
>= 0) {
1588 if (nographic
&& s
->fd_in
== 0) {
1590 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1591 fd_chr_read
, NULL
, chr
);
1596 /* open a character device to a unix fd */
1597 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1599 CharDriverState
*chr
;
1602 chr
= qemu_mallocz(sizeof(CharDriverState
));
1605 s
= qemu_mallocz(sizeof(FDCharDriver
));
1613 chr
->chr_write
= fd_chr_write
;
1614 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
1616 qemu_chr_reset(chr
);
1621 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1625 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1628 return qemu_chr_open_fd(-1, fd_out
);
1631 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1634 char filename_in
[256], filename_out
[256];
1636 snprintf(filename_in
, 256, "%s.in", filename
);
1637 snprintf(filename_out
, 256, "%s.out", filename
);
1638 fd_in
= open(filename_in
, O_RDWR
| O_BINARY
);
1639 fd_out
= open(filename_out
, O_RDWR
| O_BINARY
);
1640 if (fd_in
< 0 || fd_out
< 0) {
1645 fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
);
1649 return qemu_chr_open_fd(fd_in
, fd_out
);
1653 /* for STDIO, we handle the case where several clients use it
1656 #define TERM_FIFO_MAX_SIZE 1
1658 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1659 static int term_fifo_size
;
1661 static int stdio_read_poll(void *opaque
)
1663 CharDriverState
*chr
= opaque
;
1665 /* try to flush the queue if needed */
1666 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
1667 qemu_chr_read(chr
, term_fifo
, 1);
1670 /* see if we can absorb more chars */
1671 if (term_fifo_size
== 0)
1677 static void stdio_read(void *opaque
)
1681 CharDriverState
*chr
= opaque
;
1683 size
= read(0, buf
, 1);
1685 /* stdin has been closed. Remove it from the active list. */
1686 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
1690 if (qemu_chr_can_read(chr
) > 0) {
1691 qemu_chr_read(chr
, buf
, 1);
1692 } else if (term_fifo_size
== 0) {
1693 term_fifo
[term_fifo_size
++] = buf
[0];
1698 /* init terminal so that we can grab keys */
1699 static struct termios oldtty
;
1700 static int old_fd0_flags
;
1702 static void term_exit(void)
1704 tcsetattr (0, TCSANOW
, &oldtty
);
1705 fcntl(0, F_SETFL
, old_fd0_flags
);
1708 static void term_init(void)
1712 tcgetattr (0, &tty
);
1714 old_fd0_flags
= fcntl(0, F_GETFL
);
1716 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1717 |INLCR
|IGNCR
|ICRNL
|IXON
);
1718 tty
.c_oflag
|= OPOST
;
1719 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1720 /* if graphical mode, we allow Ctrl-C handling */
1722 tty
.c_lflag
&= ~ISIG
;
1723 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1726 tty
.c_cc
[VTIME
] = 0;
1728 tcsetattr (0, TCSANOW
, &tty
);
1732 fcntl(0, F_SETFL
, O_NONBLOCK
);
1735 static CharDriverState
*qemu_chr_open_stdio(void)
1737 CharDriverState
*chr
;
1739 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1741 chr
= qemu_chr_open_fd(0, 1);
1742 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
1749 #if defined(__linux__)
1750 static CharDriverState
*qemu_chr_open_pty(void)
1753 char slave_name
[1024];
1754 int master_fd
, slave_fd
;
1756 /* Not satisfying */
1757 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1761 /* Disabling local echo and line-buffered output */
1762 tcgetattr (master_fd
, &tty
);
1763 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1765 tty
.c_cc
[VTIME
] = 0;
1766 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1768 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1769 return qemu_chr_open_fd(master_fd
, master_fd
);
1772 static void tty_serial_init(int fd
, int speed
,
1773 int parity
, int data_bits
, int stop_bits
)
1779 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1780 speed
, parity
, data_bits
, stop_bits
);
1782 tcgetattr (fd
, &tty
);
1824 cfsetispeed(&tty
, spd
);
1825 cfsetospeed(&tty
, spd
);
1827 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1828 |INLCR
|IGNCR
|ICRNL
|IXON
);
1829 tty
.c_oflag
|= OPOST
;
1830 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1831 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1852 tty
.c_cflag
|= PARENB
;
1855 tty
.c_cflag
|= PARENB
| PARODD
;
1859 tty
.c_cflag
|= CSTOPB
;
1861 tcsetattr (fd
, TCSANOW
, &tty
);
1864 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1866 FDCharDriver
*s
= chr
->opaque
;
1869 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1871 QEMUSerialSetParams
*ssp
= arg
;
1872 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1873 ssp
->data_bits
, ssp
->stop_bits
);
1876 case CHR_IOCTL_SERIAL_SET_BREAK
:
1878 int enable
= *(int *)arg
;
1880 tcsendbreak(s
->fd_in
, 1);
1889 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1891 CharDriverState
*chr
;
1894 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1897 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1898 tty_serial_init(fd
, 115200, 'N', 8, 1);
1899 chr
= qemu_chr_open_fd(fd
, fd
);
1902 chr
->chr_ioctl
= tty_serial_ioctl
;
1903 qemu_chr_reset(chr
);
1910 } ParallelCharDriver
;
1912 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1914 if (s
->mode
!= mode
) {
1916 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1923 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1925 ParallelCharDriver
*drv
= chr
->opaque
;
1930 case CHR_IOCTL_PP_READ_DATA
:
1931 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1933 *(uint8_t *)arg
= b
;
1935 case CHR_IOCTL_PP_WRITE_DATA
:
1936 b
= *(uint8_t *)arg
;
1937 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1940 case CHR_IOCTL_PP_READ_CONTROL
:
1941 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1943 /* Linux gives only the lowest bits, and no way to know data
1944 direction! For better compatibility set the fixed upper
1946 *(uint8_t *)arg
= b
| 0xc0;
1948 case CHR_IOCTL_PP_WRITE_CONTROL
:
1949 b
= *(uint8_t *)arg
;
1950 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1953 case CHR_IOCTL_PP_READ_STATUS
:
1954 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1956 *(uint8_t *)arg
= b
;
1958 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1959 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1960 struct ParallelIOArg
*parg
= arg
;
1961 int n
= read(fd
, parg
->buffer
, parg
->count
);
1962 if (n
!= parg
->count
) {
1967 case CHR_IOCTL_PP_EPP_READ
:
1968 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1969 struct ParallelIOArg
*parg
= arg
;
1970 int n
= read(fd
, parg
->buffer
, parg
->count
);
1971 if (n
!= parg
->count
) {
1976 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1977 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1978 struct ParallelIOArg
*parg
= arg
;
1979 int n
= write(fd
, parg
->buffer
, parg
->count
);
1980 if (n
!= parg
->count
) {
1985 case CHR_IOCTL_PP_EPP_WRITE
:
1986 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1987 struct ParallelIOArg
*parg
= arg
;
1988 int n
= write(fd
, parg
->buffer
, parg
->count
);
1989 if (n
!= parg
->count
) {
2000 static void pp_close(CharDriverState
*chr
)
2002 ParallelCharDriver
*drv
= chr
->opaque
;
2005 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
2006 ioctl(fd
, PPRELEASE
);
2011 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
2013 CharDriverState
*chr
;
2014 ParallelCharDriver
*drv
;
2017 fd
= open(filename
, O_RDWR
);
2021 if (ioctl(fd
, PPCLAIM
) < 0) {
2026 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
2032 drv
->mode
= IEEE1284_MODE_COMPAT
;
2034 chr
= qemu_mallocz(sizeof(CharDriverState
));
2040 chr
->chr_write
= null_chr_write
;
2041 chr
->chr_ioctl
= pp_ioctl
;
2042 chr
->chr_close
= pp_close
;
2045 qemu_chr_reset(chr
);
2051 static CharDriverState
*qemu_chr_open_pty(void)
2057 #endif /* !defined(_WIN32) */
2062 HANDLE hcom
, hrecv
, hsend
;
2063 OVERLAPPED orecv
, osend
;
2068 #define NSENDBUF 2048
2069 #define NRECVBUF 2048
2070 #define MAXCONNECT 1
2071 #define NTIMEOUT 5000
2073 static int win_chr_poll(void *opaque
);
2074 static int win_chr_pipe_poll(void *opaque
);
2076 static void win_chr_close(CharDriverState
*chr
)
2078 WinCharState
*s
= chr
->opaque
;
2081 CloseHandle(s
->hsend
);
2085 CloseHandle(s
->hrecv
);
2089 CloseHandle(s
->hcom
);
2093 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
2095 qemu_del_polling_cb(win_chr_poll
, chr
);
2098 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
2100 WinCharState
*s
= chr
->opaque
;
2102 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2107 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2109 fprintf(stderr
, "Failed CreateEvent\n");
2112 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2114 fprintf(stderr
, "Failed CreateEvent\n");
2118 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2119 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2120 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2121 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
2126 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2127 fprintf(stderr
, "Failed SetupComm\n");
2131 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2132 size
= sizeof(COMMCONFIG
);
2133 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2134 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2135 CommConfigDialog(filename
, NULL
, &comcfg
);
2137 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2138 fprintf(stderr
, "Failed SetCommState\n");
2142 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2143 fprintf(stderr
, "Failed SetCommMask\n");
2147 cto
.ReadIntervalTimeout
= MAXDWORD
;
2148 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2149 fprintf(stderr
, "Failed SetCommTimeouts\n");
2153 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2154 fprintf(stderr
, "Failed ClearCommError\n");
2157 qemu_add_polling_cb(win_chr_poll
, chr
);
2165 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2167 WinCharState
*s
= chr
->opaque
;
2168 DWORD len
, ret
, size
, err
;
2171 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2172 s
->osend
.hEvent
= s
->hsend
;
2175 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2177 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2179 err
= GetLastError();
2180 if (err
== ERROR_IO_PENDING
) {
2181 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2199 static int win_chr_read_poll(CharDriverState
*chr
)
2201 WinCharState
*s
= chr
->opaque
;
2203 s
->max_size
= qemu_chr_can_read(chr
);
2207 static void win_chr_readfile(CharDriverState
*chr
)
2209 WinCharState
*s
= chr
->opaque
;
2214 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2215 s
->orecv
.hEvent
= s
->hrecv
;
2216 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2218 err
= GetLastError();
2219 if (err
== ERROR_IO_PENDING
) {
2220 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2225 qemu_chr_read(chr
, buf
, size
);
2229 static void win_chr_read(CharDriverState
*chr
)
2231 WinCharState
*s
= chr
->opaque
;
2233 if (s
->len
> s
->max_size
)
2234 s
->len
= s
->max_size
;
2238 win_chr_readfile(chr
);
2241 static int win_chr_poll(void *opaque
)
2243 CharDriverState
*chr
= opaque
;
2244 WinCharState
*s
= chr
->opaque
;
2248 ClearCommError(s
->hcom
, &comerr
, &status
);
2249 if (status
.cbInQue
> 0) {
2250 s
->len
= status
.cbInQue
;
2251 win_chr_read_poll(chr
);
2258 static CharDriverState
*qemu_chr_open_win(const char *filename
)
2260 CharDriverState
*chr
;
2263 chr
= qemu_mallocz(sizeof(CharDriverState
));
2266 s
= qemu_mallocz(sizeof(WinCharState
));
2272 chr
->chr_write
= win_chr_write
;
2273 chr
->chr_close
= win_chr_close
;
2275 if (win_chr_init(chr
, filename
) < 0) {
2280 qemu_chr_reset(chr
);
2284 static int win_chr_pipe_poll(void *opaque
)
2286 CharDriverState
*chr
= opaque
;
2287 WinCharState
*s
= chr
->opaque
;
2290 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2293 win_chr_read_poll(chr
);
2300 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
2302 WinCharState
*s
= chr
->opaque
;
2310 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2312 fprintf(stderr
, "Failed CreateEvent\n");
2315 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2317 fprintf(stderr
, "Failed CreateEvent\n");
2321 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2322 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2323 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2325 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2326 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2327 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2332 ZeroMemory(&ov
, sizeof(ov
));
2333 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2334 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2336 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2340 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2342 fprintf(stderr
, "Failed GetOverlappedResult\n");
2344 CloseHandle(ov
.hEvent
);
2351 CloseHandle(ov
.hEvent
);
2354 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2363 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2365 CharDriverState
*chr
;
2368 chr
= qemu_mallocz(sizeof(CharDriverState
));
2371 s
= qemu_mallocz(sizeof(WinCharState
));
2377 chr
->chr_write
= win_chr_write
;
2378 chr
->chr_close
= win_chr_close
;
2380 if (win_chr_pipe_init(chr
, filename
) < 0) {
2385 qemu_chr_reset(chr
);
2389 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2391 CharDriverState
*chr
;
2394 chr
= qemu_mallocz(sizeof(CharDriverState
));
2397 s
= qemu_mallocz(sizeof(WinCharState
));
2404 chr
->chr_write
= win_chr_write
;
2405 qemu_chr_reset(chr
);
2409 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2413 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2414 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2415 if (fd_out
== INVALID_HANDLE_VALUE
)
2418 return qemu_chr_open_win_file(fd_out
);
2422 /***********************************************************/
2423 /* UDP Net console */
2427 struct sockaddr_in daddr
;
2434 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2436 NetCharDriver
*s
= chr
->opaque
;
2438 return sendto(s
->fd
, buf
, len
, 0,
2439 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2442 static int udp_chr_read_poll(void *opaque
)
2444 CharDriverState
*chr
= opaque
;
2445 NetCharDriver
*s
= chr
->opaque
;
2447 s
->max_size
= qemu_chr_can_read(chr
);
2449 /* If there were any stray characters in the queue process them
2452 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2453 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2455 s
->max_size
= qemu_chr_can_read(chr
);
2460 static void udp_chr_read(void *opaque
)
2462 CharDriverState
*chr
= opaque
;
2463 NetCharDriver
*s
= chr
->opaque
;
2465 if (s
->max_size
== 0)
2467 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2468 s
->bufptr
= s
->bufcnt
;
2473 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2474 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2476 s
->max_size
= qemu_chr_can_read(chr
);
2480 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2482 NetCharDriver
*s
= chr
->opaque
;
2485 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2486 udp_chr_read
, NULL
, chr
);
2490 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2492 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2494 int parse_host_src_port(struct sockaddr_in
*haddr
,
2495 struct sockaddr_in
*saddr
,
2498 static CharDriverState
*qemu_chr_open_udp(const char *def
)
2500 CharDriverState
*chr
= NULL
;
2501 NetCharDriver
*s
= NULL
;
2503 struct sockaddr_in saddr
;
2505 chr
= qemu_mallocz(sizeof(CharDriverState
));
2508 s
= qemu_mallocz(sizeof(NetCharDriver
));
2512 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2514 perror("socket(PF_INET, SOCK_DGRAM)");
2518 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2519 printf("Could not parse: %s\n", def
);
2523 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2533 chr
->chr_write
= udp_chr_write
;
2534 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2547 /***********************************************************/
2548 /* TCP Net console */
2559 static void tcp_chr_accept(void *opaque
);
2561 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2563 TCPCharDriver
*s
= chr
->opaque
;
2565 return send_all(s
->fd
, buf
, len
);
2567 /* XXX: indicate an error ? */
2572 static int tcp_chr_read_poll(void *opaque
)
2574 CharDriverState
*chr
= opaque
;
2575 TCPCharDriver
*s
= chr
->opaque
;
2578 s
->max_size
= qemu_chr_can_read(chr
);
2583 #define IAC_BREAK 243
2584 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2586 char *buf
, int *size
)
2588 /* Handle any telnet client's basic IAC options to satisfy char by
2589 * char mode with no echo. All IAC options will be removed from
2590 * the buf and the do_telnetopt variable will be used to track the
2591 * state of the width of the IAC information.
2593 * IAC commands come in sets of 3 bytes with the exception of the
2594 * "IAC BREAK" command and the double IAC.
2600 for (i
= 0; i
< *size
; i
++) {
2601 if (s
->do_telnetopt
> 1) {
2602 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2603 /* Double IAC means send an IAC */
2607 s
->do_telnetopt
= 1;
2609 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2610 /* Handle IAC break commands by sending a serial break */
2611 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
2616 if (s
->do_telnetopt
>= 4) {
2617 s
->do_telnetopt
= 1;
2620 if ((unsigned char)buf
[i
] == IAC
) {
2621 s
->do_telnetopt
= 2;
2632 static void tcp_chr_read(void *opaque
)
2634 CharDriverState
*chr
= opaque
;
2635 TCPCharDriver
*s
= chr
->opaque
;
2639 if (!s
->connected
|| s
->max_size
<= 0)
2642 if (len
> s
->max_size
)
2644 size
= recv(s
->fd
, buf
, len
, 0);
2646 /* connection closed */
2648 if (s
->listen_fd
>= 0) {
2649 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2651 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2654 } else if (size
> 0) {
2655 if (s
->do_telnetopt
)
2656 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2658 qemu_chr_read(chr
, buf
, size
);
2662 static void tcp_chr_connect(void *opaque
)
2664 CharDriverState
*chr
= opaque
;
2665 TCPCharDriver
*s
= chr
->opaque
;
2668 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2669 tcp_chr_read
, NULL
, chr
);
2670 qemu_chr_reset(chr
);
2673 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2674 static void tcp_chr_telnet_init(int fd
)
2677 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2678 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2679 send(fd
, (char *)buf
, 3, 0);
2680 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2681 send(fd
, (char *)buf
, 3, 0);
2682 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2683 send(fd
, (char *)buf
, 3, 0);
2684 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2685 send(fd
, (char *)buf
, 3, 0);
2688 static void socket_set_nodelay(int fd
)
2691 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2694 static void tcp_chr_accept(void *opaque
)
2696 CharDriverState
*chr
= opaque
;
2697 TCPCharDriver
*s
= chr
->opaque
;
2698 struct sockaddr_in saddr
;
2700 struct sockaddr_un uaddr
;
2702 struct sockaddr
*addr
;
2709 len
= sizeof(uaddr
);
2710 addr
= (struct sockaddr
*)&uaddr
;
2714 len
= sizeof(saddr
);
2715 addr
= (struct sockaddr
*)&saddr
;
2717 fd
= accept(s
->listen_fd
, addr
, &len
);
2718 if (fd
< 0 && errno
!= EINTR
) {
2720 } else if (fd
>= 0) {
2721 if (s
->do_telnetopt
)
2722 tcp_chr_telnet_init(fd
);
2726 socket_set_nonblock(fd
);
2728 socket_set_nodelay(fd
);
2730 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2731 tcp_chr_connect(chr
);
2734 static void tcp_chr_close(CharDriverState
*chr
)
2736 TCPCharDriver
*s
= chr
->opaque
;
2739 if (s
->listen_fd
>= 0)
2740 closesocket(s
->listen_fd
);
2744 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2748 CharDriverState
*chr
= NULL
;
2749 TCPCharDriver
*s
= NULL
;
2750 int fd
= -1, ret
, err
, val
;
2752 int is_waitconnect
= 1;
2755 struct sockaddr_in saddr
;
2757 struct sockaddr_un uaddr
;
2759 struct sockaddr
*addr
;
2764 addr
= (struct sockaddr
*)&uaddr
;
2765 addrlen
= sizeof(uaddr
);
2766 if (parse_unix_path(&uaddr
, host_str
) < 0)
2771 addr
= (struct sockaddr
*)&saddr
;
2772 addrlen
= sizeof(saddr
);
2773 if (parse_host_port(&saddr
, host_str
) < 0)
2778 while((ptr
= strchr(ptr
,','))) {
2780 if (!strncmp(ptr
,"server",6)) {
2782 } else if (!strncmp(ptr
,"nowait",6)) {
2784 } else if (!strncmp(ptr
,"nodelay",6)) {
2787 printf("Unknown option: %s\n", ptr
);
2794 chr
= qemu_mallocz(sizeof(CharDriverState
));
2797 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2803 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2806 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2811 if (!is_waitconnect
)
2812 socket_set_nonblock(fd
);
2817 s
->is_unix
= is_unix
;
2818 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2821 chr
->chr_write
= tcp_chr_write
;
2822 chr
->chr_close
= tcp_chr_close
;
2825 /* allow fast reuse */
2829 strncpy(path
, uaddr
.sun_path
, 108);
2836 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2839 ret
= bind(fd
, addr
, addrlen
);
2843 ret
= listen(fd
, 0);
2848 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2850 s
->do_telnetopt
= 1;
2853 ret
= connect(fd
, addr
, addrlen
);
2855 err
= socket_error();
2856 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2857 } else if (err
== EINPROGRESS
) {
2860 } else if (err
== WSAEALREADY
) {
2872 socket_set_nodelay(fd
);
2874 tcp_chr_connect(chr
);
2876 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2879 if (is_listen
&& is_waitconnect
) {
2880 printf("QEMU waiting for connection on: %s\n", host_str
);
2881 tcp_chr_accept(chr
);
2882 socket_set_nonblock(s
->listen_fd
);
2894 CharDriverState
*qemu_chr_open(const char *filename
)
2898 if (!strcmp(filename
, "vc")) {
2899 return text_console_init(&display_state
);
2900 } else if (!strcmp(filename
, "null")) {
2901 return qemu_chr_open_null();
2903 if (strstart(filename
, "tcp:", &p
)) {
2904 return qemu_chr_open_tcp(p
, 0, 0);
2906 if (strstart(filename
, "telnet:", &p
)) {
2907 return qemu_chr_open_tcp(p
, 1, 0);
2909 if (strstart(filename
, "udp:", &p
)) {
2910 return qemu_chr_open_udp(p
);
2912 if (strstart(filename
, "mon:", &p
)) {
2913 CharDriverState
*drv
= qemu_chr_open(p
);
2915 drv
= qemu_chr_open_mux(drv
);
2916 monitor_init(drv
, !nographic
);
2919 printf("Unable to open driver: %s\n", p
);
2923 if (strstart(filename
, "unix:", &p
)) {
2924 return qemu_chr_open_tcp(p
, 0, 1);
2925 } else if (strstart(filename
, "file:", &p
)) {
2926 return qemu_chr_open_file_out(p
);
2927 } else if (strstart(filename
, "pipe:", &p
)) {
2928 return qemu_chr_open_pipe(p
);
2929 } else if (!strcmp(filename
, "pty")) {
2930 return qemu_chr_open_pty();
2931 } else if (!strcmp(filename
, "stdio")) {
2932 return qemu_chr_open_stdio();
2935 #if defined(__linux__)
2936 if (strstart(filename
, "/dev/parport", NULL
)) {
2937 return qemu_chr_open_pp(filename
);
2939 if (strstart(filename
, "/dev/", NULL
)) {
2940 return qemu_chr_open_tty(filename
);
2944 if (strstart(filename
, "COM", NULL
)) {
2945 return qemu_chr_open_win(filename
);
2947 if (strstart(filename
, "pipe:", &p
)) {
2948 return qemu_chr_open_win_pipe(p
);
2950 if (strstart(filename
, "file:", &p
)) {
2951 return qemu_chr_open_win_file_out(p
);
2959 void qemu_chr_close(CharDriverState
*chr
)
2962 chr
->chr_close(chr
);
2965 /***********************************************************/
2966 /* network device redirectors */
2968 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2972 for(i
=0;i
<size
;i
+=16) {
2976 fprintf(f
, "%08x ", i
);
2979 fprintf(f
, " %02x", buf
[i
+j
]);
2984 for(j
=0;j
<len
;j
++) {
2986 if (c
< ' ' || c
> '~')
2988 fprintf(f
, "%c", c
);
2994 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
2997 for(i
= 0; i
< 6; i
++) {
2998 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3011 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
3016 p1
= strchr(p
, sep
);
3022 if (len
> buf_size
- 1)
3024 memcpy(buf
, p
, len
);
3031 int parse_host_src_port(struct sockaddr_in
*haddr
,
3032 struct sockaddr_in
*saddr
,
3033 const char *input_str
)
3035 char *str
= strdup(input_str
);
3036 char *host_str
= str
;
3041 * Chop off any extra arguments at the end of the string which
3042 * would start with a comma, then fill in the src port information
3043 * if it was provided else use the "any address" and "any port".
3045 if ((ptr
= strchr(str
,',')))
3048 if ((src_str
= strchr(input_str
,'@'))) {
3053 if (parse_host_port(haddr
, host_str
) < 0)
3056 if (!src_str
|| *src_str
== '\0')
3059 if (parse_host_port(saddr
, src_str
) < 0)
3070 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
3078 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3080 saddr
->sin_family
= AF_INET
;
3081 if (buf
[0] == '\0') {
3082 saddr
->sin_addr
.s_addr
= 0;
3084 if (isdigit(buf
[0])) {
3085 if (!inet_aton(buf
, &saddr
->sin_addr
))
3088 if ((he
= gethostbyname(buf
)) == NULL
)
3090 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
3093 port
= strtol(p
, (char **)&r
, 0);
3096 saddr
->sin_port
= htons(port
);
3101 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
3106 len
= MIN(108, strlen(str
));
3107 p
= strchr(str
, ',');
3109 len
= MIN(len
, p
- str
);
3111 memset(uaddr
, 0, sizeof(*uaddr
));
3113 uaddr
->sun_family
= AF_UNIX
;
3114 memcpy(uaddr
->sun_path
, str
, len
);
3120 /* find or alloc a new VLAN */
3121 VLANState
*qemu_find_vlan(int id
)
3123 VLANState
**pvlan
, *vlan
;
3124 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3128 vlan
= qemu_mallocz(sizeof(VLANState
));
3133 pvlan
= &first_vlan
;
3134 while (*pvlan
!= NULL
)
3135 pvlan
= &(*pvlan
)->next
;
3140 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
3141 IOReadHandler
*fd_read
,
3142 IOCanRWHandler
*fd_can_read
,
3145 VLANClientState
*vc
, **pvc
;
3146 vc
= qemu_mallocz(sizeof(VLANClientState
));
3149 vc
->fd_read
= fd_read
;
3150 vc
->fd_can_read
= fd_can_read
;
3151 vc
->opaque
= opaque
;
3155 pvc
= &vlan
->first_client
;
3156 while (*pvc
!= NULL
)
3157 pvc
= &(*pvc
)->next
;
3162 int qemu_can_send_packet(VLANClientState
*vc1
)
3164 VLANState
*vlan
= vc1
->vlan
;
3165 VLANClientState
*vc
;
3167 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3169 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
3176 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
3178 VLANState
*vlan
= vc1
->vlan
;
3179 VLANClientState
*vc
;
3182 printf("vlan %d send:\n", vlan
->id
);
3183 hex_dump(stdout
, buf
, size
);
3185 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3187 vc
->fd_read(vc
->opaque
, buf
, size
);
3192 #if defined(CONFIG_SLIRP)
3194 /* slirp network adapter */
3196 static int slirp_inited
;
3197 static VLANClientState
*slirp_vc
;
3199 int slirp_can_output(void)
3201 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
3204 void slirp_output(const uint8_t *pkt
, int pkt_len
)
3207 printf("slirp output:\n");
3208 hex_dump(stdout
, pkt
, pkt_len
);
3212 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
3215 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
3218 printf("slirp input:\n");
3219 hex_dump(stdout
, buf
, size
);
3221 slirp_input(buf
, size
);
3224 static int net_slirp_init(VLANState
*vlan
)
3226 if (!slirp_inited
) {
3230 slirp_vc
= qemu_new_vlan_client(vlan
,
3231 slirp_receive
, NULL
, NULL
);
3232 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
3236 static void net_slirp_redir(const char *redir_str
)
3241 struct in_addr guest_addr
;
3242 int host_port
, guest_port
;
3244 if (!slirp_inited
) {
3250 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3252 if (!strcmp(buf
, "tcp")) {
3254 } else if (!strcmp(buf
, "udp")) {
3260 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3262 host_port
= strtol(buf
, &r
, 0);
3266 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3268 if (buf
[0] == '\0') {
3269 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
3271 if (!inet_aton(buf
, &guest_addr
))
3274 guest_port
= strtol(p
, &r
, 0);
3278 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3279 fprintf(stderr
, "qemu: could not set up redirection\n");
3284 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3292 static void smb_exit(void)
3296 char filename
[1024];
3298 /* erase all the files in the directory */
3299 d
= opendir(smb_dir
);
3304 if (strcmp(de
->d_name
, ".") != 0 &&
3305 strcmp(de
->d_name
, "..") != 0) {
3306 snprintf(filename
, sizeof(filename
), "%s/%s",
3307 smb_dir
, de
->d_name
);
3315 /* automatic user mode samba server configuration */
3316 void net_slirp_smb(const char *exported_dir
)
3318 char smb_conf
[1024];
3319 char smb_cmdline
[1024];
3322 if (!slirp_inited
) {
3327 /* XXX: better tmp dir construction */
3328 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3329 if (mkdir(smb_dir
, 0700) < 0) {
3330 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3333 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3335 f
= fopen(smb_conf
, "w");
3337 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3344 "socket address=127.0.0.1\n"
3345 "pid directory=%s\n"
3346 "lock directory=%s\n"
3347 "log file=%s/log.smbd\n"
3348 "smb passwd file=%s/smbpasswd\n"
3349 "security = share\n"
3364 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3365 SMBD_COMMAND
, smb_conf
);
3367 slirp_add_exec(0, smb_cmdline
, 4, 139);
3370 #endif /* !defined(_WIN32) */
3372 #endif /* CONFIG_SLIRP */
3374 #if !defined(_WIN32)
3376 typedef struct TAPState
{
3377 VLANClientState
*vc
;
3381 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3383 TAPState
*s
= opaque
;
3386 ret
= write(s
->fd
, buf
, size
);
3387 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3394 static void tap_send(void *opaque
)
3396 TAPState
*s
= opaque
;
3403 sbuf
.maxlen
= sizeof(buf
);
3405 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
3407 size
= read(s
->fd
, buf
, sizeof(buf
));
3410 qemu_send_packet(s
->vc
, buf
, size
);
3416 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3420 s
= qemu_mallocz(sizeof(TAPState
));
3424 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3425 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3426 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3431 static int tap_open(char *ifname
, int ifname_size
)
3437 fd
= open("/dev/tap", O_RDWR
);
3439 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3444 dev
= devname(s
.st_rdev
, S_IFCHR
);
3445 pstrcpy(ifname
, ifname_size
, dev
);
3447 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3450 #elif defined(__sun__)
3451 #define TUNNEWPPA (('T'<<16) | 0x0001)
3453 * Allocate TAP device, returns opened fd.
3454 * Stores dev name in the first arg(must be large enough).
3456 int tap_alloc(char *dev
)
3458 int tap_fd
, if_fd
, ppa
= -1;
3459 static int ip_fd
= 0;
3462 static int arp_fd
= 0;
3463 int ip_muxid
, arp_muxid
;
3464 struct strioctl strioc_if
, strioc_ppa
;
3465 int link_type
= I_PLINK
;;
3467 char actual_name
[32] = "";
3469 memset(&ifr
, 0x0, sizeof(ifr
));
3473 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
3477 /* Check if IP device was opened */
3481 if( (ip_fd
= open("/dev/udp", O_RDWR
, 0)) < 0){
3482 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
3486 if( (tap_fd
= open("/dev/tap", O_RDWR
, 0)) < 0){
3487 syslog(LOG_ERR
, "Can't open /dev/tap");
3491 /* Assign a new PPA and get its unit number. */
3492 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
3493 strioc_ppa
.ic_timout
= 0;
3494 strioc_ppa
.ic_len
= sizeof(ppa
);
3495 strioc_ppa
.ic_dp
= (char *)&ppa
;
3496 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
3497 syslog (LOG_ERR
, "Can't assign new interface");
3499 if( (if_fd
= open("/dev/tap", O_RDWR
, 0)) < 0){
3500 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
3503 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
3504 syslog(LOG_ERR
, "Can't push IP module");
3508 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
3509 syslog(LOG_ERR
, "Can't get flags\n");
3511 snprintf (actual_name
, 32, "tap%d", ppa
);
3512 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3515 /* Assign ppa according to the unit number returned by tun device */
3517 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
3518 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
3519 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
3520 syslog (LOG_ERR
, "Can't get flags\n");
3521 /* Push arp module to if_fd */
3522 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
3523 syslog (LOG_ERR
, "Can't push ARP module (2)");
3525 /* Push arp module to ip_fd */
3526 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
3527 syslog (LOG_ERR
, "I_POP failed\n");
3528 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
3529 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
3531 if ((arp_fd
= open ("/dev/tap", O_RDWR
, 0)) < 0)
3532 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
3534 /* Set ifname to arp */
3535 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
3536 strioc_if
.ic_timout
= 0;
3537 strioc_if
.ic_len
= sizeof(ifr
);
3538 strioc_if
.ic_dp
= (char *)&ifr
;
3539 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
3540 syslog (LOG_ERR
, "Can't set ifname to arp\n");
3543 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
3544 syslog(LOG_ERR
, "Can't link TAP device to IP");
3548 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
3549 syslog (LOG_ERR
, "Can't link TAP device to ARP");
3553 memset(&ifr
, 0x0, sizeof(ifr
));
3554 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3555 ifr
.lifr_ip_muxid
= ip_muxid
;
3556 ifr
.lifr_arp_muxid
= arp_muxid
;
3558 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
3560 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
3561 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
3562 syslog (LOG_ERR
, "Can't set multiplexor id");
3565 sprintf(dev
, "tap%d", ppa
);
3569 static int tap_open(char *ifname
, int ifname_size
)
3573 if( (fd
= tap_alloc(dev
)) < 0 ){
3574 fprintf(stderr
, "Cannot allocate TAP device\n");
3577 pstrcpy(ifname
, ifname_size
, dev
);
3578 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3582 static int tap_open(char *ifname
, int ifname_size
)
3587 fd
= open("/dev/net/tun", O_RDWR
);
3589 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
3592 memset(&ifr
, 0, sizeof(ifr
));
3593 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
3594 if (ifname
[0] != '\0')
3595 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
3597 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
3598 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
3600 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
3604 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
3605 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3610 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
3611 const char *setup_script
)
3614 int pid
, status
, fd
;
3619 if (ifname1
!= NULL
)
3620 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
3623 fd
= tap_open(ifname
, sizeof(ifname
));
3627 if (!setup_script
|| !strcmp(setup_script
, "no"))
3629 if (setup_script
[0] != '\0') {
3630 /* try to launch network init script */
3634 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
3635 for (i
= 0; i
< open_max
; i
++)
3636 if (i
!= STDIN_FILENO
&&
3637 i
!= STDOUT_FILENO
&&
3638 i
!= STDERR_FILENO
&&
3643 *parg
++ = (char *)setup_script
;
3646 execv(setup_script
, args
);
3649 while (waitpid(pid
, &status
, 0) != pid
);
3650 if (!WIFEXITED(status
) ||
3651 WEXITSTATUS(status
) != 0) {
3652 fprintf(stderr
, "%s: could not launch network script\n",
3658 s
= net_tap_fd_init(vlan
, fd
);
3661 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3662 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
3666 #endif /* !_WIN32 */
3668 /* network connection */
3669 typedef struct NetSocketState
{
3670 VLANClientState
*vc
;
3672 int state
; /* 0 = getting length, 1 = getting data */
3676 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
3679 typedef struct NetSocketListenState
{
3682 } NetSocketListenState
;
3684 /* XXX: we consider we can send the whole packet without blocking */
3685 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
3687 NetSocketState
*s
= opaque
;
3691 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
3692 send_all(s
->fd
, buf
, size
);
3695 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
3697 NetSocketState
*s
= opaque
;
3698 sendto(s
->fd
, buf
, size
, 0,
3699 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
3702 static void net_socket_send(void *opaque
)
3704 NetSocketState
*s
= opaque
;
3709 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
3711 err
= socket_error();
3712 if (err
!= EWOULDBLOCK
)
3714 } else if (size
== 0) {
3715 /* end of connection */
3717 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3723 /* reassemble a packet from the network */
3729 memcpy(s
->buf
+ s
->index
, buf
, l
);
3733 if (s
->index
== 4) {
3735 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
3741 l
= s
->packet_len
- s
->index
;
3744 memcpy(s
->buf
+ s
->index
, buf
, l
);
3748 if (s
->index
>= s
->packet_len
) {
3749 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
3758 static void net_socket_send_dgram(void *opaque
)
3760 NetSocketState
*s
= opaque
;
3763 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
3767 /* end of connection */
3768 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3771 qemu_send_packet(s
->vc
, s
->buf
, size
);
3774 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
3779 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
3780 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
3781 inet_ntoa(mcastaddr
->sin_addr
),
3782 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
3786 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
3788 perror("socket(PF_INET, SOCK_DGRAM)");
3793 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
3794 (const char *)&val
, sizeof(val
));
3796 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
3800 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
3806 /* Add host to multicast group */
3807 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
3808 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
3810 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
3811 (const char *)&imr
, sizeof(struct ip_mreq
));
3813 perror("setsockopt(IP_ADD_MEMBERSHIP)");
3817 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
3819 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
3820 (const char *)&val
, sizeof(val
));
3822 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
3826 socket_set_nonblock(fd
);
3834 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
3837 struct sockaddr_in saddr
;
3839 socklen_t saddr_len
;
3842 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
3843 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
3844 * by ONLY ONE process: we must "clone" this dgram socket --jjo
3848 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
3850 if (saddr
.sin_addr
.s_addr
==0) {
3851 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
3855 /* clone dgram socket */
3856 newfd
= net_socket_mcast_create(&saddr
);
3858 /* error already reported by net_socket_mcast_create() */
3862 /* clone newfd to fd, close newfd */
3867 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
3868 fd
, strerror(errno
));
3873 s
= qemu_mallocz(sizeof(NetSocketState
));
3878 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
3879 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
3881 /* mcast: save bound address as dst */
3882 if (is_connected
) s
->dgram_dst
=saddr
;
3884 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3885 "socket: fd=%d (%s mcast=%s:%d)",
3886 fd
, is_connected
? "cloned" : "",
3887 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3891 static void net_socket_connect(void *opaque
)
3893 NetSocketState
*s
= opaque
;
3894 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
3897 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
3901 s
= qemu_mallocz(sizeof(NetSocketState
));
3905 s
->vc
= qemu_new_vlan_client(vlan
,
3906 net_socket_receive
, NULL
, s
);
3907 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3908 "socket: fd=%d", fd
);
3910 net_socket_connect(s
);
3912 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
3917 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
3920 int so_type
=-1, optlen
=sizeof(so_type
);
3922 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
3923 fprintf(stderr
, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd
);
3928 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
3930 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3932 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
3933 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
3934 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3939 static void net_socket_accept(void *opaque
)
3941 NetSocketListenState
*s
= opaque
;
3943 struct sockaddr_in saddr
;
3948 len
= sizeof(saddr
);
3949 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
3950 if (fd
< 0 && errno
!= EINTR
) {
3952 } else if (fd
>= 0) {
3956 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
3960 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
3961 "socket: connection from %s:%d",
3962 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3966 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
3968 NetSocketListenState
*s
;
3970 struct sockaddr_in saddr
;
3972 if (parse_host_port(&saddr
, host_str
) < 0)
3975 s
= qemu_mallocz(sizeof(NetSocketListenState
));
3979 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3984 socket_set_nonblock(fd
);
3986 /* allow fast reuse */
3988 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3990 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3995 ret
= listen(fd
, 0);
4002 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
4006 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
4009 int fd
, connected
, ret
, err
;
4010 struct sockaddr_in saddr
;
4012 if (parse_host_port(&saddr
, host_str
) < 0)
4015 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4020 socket_set_nonblock(fd
);
4024 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4026 err
= socket_error();
4027 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
4028 } else if (err
== EINPROGRESS
) {
4031 } else if (err
== WSAEALREADY
) {
4044 s
= net_socket_fd_init(vlan
, fd
, connected
);
4047 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4048 "socket: connect to %s:%d",
4049 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4053 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
4057 struct sockaddr_in saddr
;
4059 if (parse_host_port(&saddr
, host_str
) < 0)
4063 fd
= net_socket_mcast_create(&saddr
);
4067 s
= net_socket_fd_init(vlan
, fd
, 0);
4071 s
->dgram_dst
= saddr
;
4073 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4074 "socket: mcast=%s:%d",
4075 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4080 static int get_param_value(char *buf
, int buf_size
,
4081 const char *tag
, const char *str
)
4090 while (*p
!= '\0' && *p
!= '=') {
4091 if ((q
- option
) < sizeof(option
) - 1)
4099 if (!strcmp(tag
, option
)) {
4101 while (*p
!= '\0' && *p
!= ',') {
4102 if ((q
- buf
) < buf_size
- 1)
4109 while (*p
!= '\0' && *p
!= ',') {
4120 static int net_client_init(const char *str
)
4131 while (*p
!= '\0' && *p
!= ',') {
4132 if ((q
- device
) < sizeof(device
) - 1)
4140 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
4141 vlan_id
= strtol(buf
, NULL
, 0);
4143 vlan
= qemu_find_vlan(vlan_id
);
4145 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
4148 if (!strcmp(device
, "nic")) {
4152 if (nb_nics
>= MAX_NICS
) {
4153 fprintf(stderr
, "Too Many NICs\n");
4156 nd
= &nd_table
[nb_nics
];
4157 macaddr
= nd
->macaddr
;
4163 macaddr
[5] = 0x56 + nb_nics
;
4165 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
4166 if (parse_macaddr(macaddr
, buf
) < 0) {
4167 fprintf(stderr
, "invalid syntax for ethernet address\n");
4171 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
4172 nd
->model
= strdup(buf
);
4178 if (!strcmp(device
, "none")) {
4179 /* does nothing. It is needed to signal that no network cards
4184 if (!strcmp(device
, "user")) {
4185 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
4186 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
4188 ret
= net_slirp_init(vlan
);
4192 if (!strcmp(device
, "tap")) {
4194 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4195 fprintf(stderr
, "tap: no interface name\n");
4198 ret
= tap_win32_init(vlan
, ifname
);
4201 if (!strcmp(device
, "tap")) {
4203 char setup_script
[1024];
4205 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4206 fd
= strtol(buf
, NULL
, 0);
4208 if (net_tap_fd_init(vlan
, fd
))
4211 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4214 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
4215 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
4217 ret
= net_tap_init(vlan
, ifname
, setup_script
);
4221 if (!strcmp(device
, "socket")) {
4222 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4224 fd
= strtol(buf
, NULL
, 0);
4226 if (net_socket_fd_init(vlan
, fd
, 1))
4228 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
4229 ret
= net_socket_listen_init(vlan
, buf
);
4230 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
4231 ret
= net_socket_connect_init(vlan
, buf
);
4232 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
4233 ret
= net_socket_mcast_init(vlan
, buf
);
4235 fprintf(stderr
, "Unknown socket options: %s\n", p
);
4240 fprintf(stderr
, "Unknown network device: %s\n", device
);
4244 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
4250 void do_info_network(void)
4253 VLANClientState
*vc
;
4255 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
4256 term_printf("VLAN %d devices:\n", vlan
->id
);
4257 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
4258 term_printf(" %s\n", vc
->info_str
);
4262 /***********************************************************/
4265 static USBPort
*used_usb_ports
;
4266 static USBPort
*free_usb_ports
;
4268 /* ??? Maybe change this to register a hub to keep track of the topology. */
4269 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
4270 usb_attachfn attach
)
4272 port
->opaque
= opaque
;
4273 port
->index
= index
;
4274 port
->attach
= attach
;
4275 port
->next
= free_usb_ports
;
4276 free_usb_ports
= port
;
4279 static int usb_device_add(const char *devname
)
4285 if (!free_usb_ports
)
4288 if (strstart(devname
, "host:", &p
)) {
4289 dev
= usb_host_device_open(p
);
4290 } else if (!strcmp(devname
, "mouse")) {
4291 dev
= usb_mouse_init();
4292 } else if (!strcmp(devname
, "tablet")) {
4293 dev
= usb_tablet_init();
4294 } else if (strstart(devname
, "disk:", &p
)) {
4295 dev
= usb_msd_init(p
);
4302 /* Find a USB port to add the device to. */
4303 port
= free_usb_ports
;
4307 /* Create a new hub and chain it on. */
4308 free_usb_ports
= NULL
;
4309 port
->next
= used_usb_ports
;
4310 used_usb_ports
= port
;
4312 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
4313 usb_attach(port
, hub
);
4314 port
= free_usb_ports
;
4317 free_usb_ports
= port
->next
;
4318 port
->next
= used_usb_ports
;
4319 used_usb_ports
= port
;
4320 usb_attach(port
, dev
);
4324 static int usb_device_del(const char *devname
)
4332 if (!used_usb_ports
)
4335 p
= strchr(devname
, '.');
4338 bus_num
= strtoul(devname
, NULL
, 0);
4339 addr
= strtoul(p
+ 1, NULL
, 0);
4343 lastp
= &used_usb_ports
;
4344 port
= used_usb_ports
;
4345 while (port
&& port
->dev
->addr
!= addr
) {
4346 lastp
= &port
->next
;
4354 *lastp
= port
->next
;
4355 usb_attach(port
, NULL
);
4356 dev
->handle_destroy(dev
);
4357 port
->next
= free_usb_ports
;
4358 free_usb_ports
= port
;
4362 void do_usb_add(const char *devname
)
4365 ret
= usb_device_add(devname
);
4367 term_printf("Could not add USB device '%s'\n", devname
);
4370 void do_usb_del(const char *devname
)
4373 ret
= usb_device_del(devname
);
4375 term_printf("Could not remove USB device '%s'\n", devname
);
4382 const char *speed_str
;
4385 term_printf("USB support not enabled\n");
4389 for (port
= used_usb_ports
; port
; port
= port
->next
) {
4393 switch(dev
->speed
) {
4397 case USB_SPEED_FULL
:
4400 case USB_SPEED_HIGH
:
4407 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
4408 0, dev
->addr
, speed_str
, dev
->devname
);
4412 /***********************************************************/
4415 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4419 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4423 static void dumb_refresh(DisplayState
*ds
)
4428 void dumb_display_init(DisplayState
*ds
)
4433 ds
->dpy_update
= dumb_update
;
4434 ds
->dpy_resize
= dumb_resize
;
4435 ds
->dpy_refresh
= dumb_refresh
;
4438 /***********************************************************/
4441 #define MAX_IO_HANDLERS 64
4443 typedef struct IOHandlerRecord
{
4445 IOCanRWHandler
*fd_read_poll
;
4447 IOHandler
*fd_write
;
4450 /* temporary data */
4452 struct IOHandlerRecord
*next
;
4455 static IOHandlerRecord
*first_io_handler
;
4457 /* XXX: fd_read_poll should be suppressed, but an API change is
4458 necessary in the character devices to suppress fd_can_read(). */
4459 int qemu_set_fd_handler2(int fd
,
4460 IOCanRWHandler
*fd_read_poll
,
4462 IOHandler
*fd_write
,
4465 IOHandlerRecord
**pioh
, *ioh
;
4467 if (!fd_read
&& !fd_write
) {
4468 pioh
= &first_io_handler
;
4473 if (ioh
->fd
== fd
) {
4480 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4484 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4487 ioh
->next
= first_io_handler
;
4488 first_io_handler
= ioh
;
4491 ioh
->fd_read_poll
= fd_read_poll
;
4492 ioh
->fd_read
= fd_read
;
4493 ioh
->fd_write
= fd_write
;
4494 ioh
->opaque
= opaque
;
4500 int qemu_set_fd_handler(int fd
,
4502 IOHandler
*fd_write
,
4505 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4508 /***********************************************************/
4509 /* Polling handling */
4511 typedef struct PollingEntry
{
4514 struct PollingEntry
*next
;
4517 static PollingEntry
*first_polling_entry
;
4519 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4521 PollingEntry
**ppe
, *pe
;
4522 pe
= qemu_mallocz(sizeof(PollingEntry
));
4526 pe
->opaque
= opaque
;
4527 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
4532 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
4534 PollingEntry
**ppe
, *pe
;
4535 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
4537 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
4546 /***********************************************************/
4547 /* Wait objects support */
4548 typedef struct WaitObjects
{
4550 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
4551 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
4552 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
4555 static WaitObjects wait_objects
= {0};
4557 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4559 WaitObjects
*w
= &wait_objects
;
4561 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
4563 w
->events
[w
->num
] = handle
;
4564 w
->func
[w
->num
] = func
;
4565 w
->opaque
[w
->num
] = opaque
;
4570 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4573 WaitObjects
*w
= &wait_objects
;
4576 for (i
= 0; i
< w
->num
; i
++) {
4577 if (w
->events
[i
] == handle
)
4580 w
->events
[i
] = w
->events
[i
+ 1];
4581 w
->func
[i
] = w
->func
[i
+ 1];
4582 w
->opaque
[i
] = w
->opaque
[i
+ 1];
4590 /***********************************************************/
4591 /* savevm/loadvm support */
4593 #define IO_BUF_SIZE 32768
4597 BlockDriverState
*bs
;
4600 int64_t base_offset
;
4601 int64_t buf_offset
; /* start of buffer when writing, end of buffer
4604 int buf_size
; /* 0 when writing */
4605 uint8_t buf
[IO_BUF_SIZE
];
4608 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
4612 f
= qemu_mallocz(sizeof(QEMUFile
));
4615 if (!strcmp(mode
, "wb")) {
4617 } else if (!strcmp(mode
, "rb")) {
4622 f
->outfile
= fopen(filename
, mode
);
4634 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
4638 f
= qemu_mallocz(sizeof(QEMUFile
));
4643 f
->is_writable
= is_writable
;
4644 f
->base_offset
= offset
;
4648 void qemu_fflush(QEMUFile
*f
)
4650 if (!f
->is_writable
)
4652 if (f
->buf_index
> 0) {
4654 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4655 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
4657 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4658 f
->buf
, f
->buf_index
);
4660 f
->buf_offset
+= f
->buf_index
;
4665 static void qemu_fill_buffer(QEMUFile
*f
)
4672 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4673 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
4677 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4678 f
->buf
, IO_BUF_SIZE
);
4684 f
->buf_offset
+= len
;
4687 void qemu_fclose(QEMUFile
*f
)
4697 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
4701 l
= IO_BUF_SIZE
- f
->buf_index
;
4704 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
4708 if (f
->buf_index
>= IO_BUF_SIZE
)
4713 void qemu_put_byte(QEMUFile
*f
, int v
)
4715 f
->buf
[f
->buf_index
++] = v
;
4716 if (f
->buf_index
>= IO_BUF_SIZE
)
4720 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
4726 l
= f
->buf_size
- f
->buf_index
;
4728 qemu_fill_buffer(f
);
4729 l
= f
->buf_size
- f
->buf_index
;
4735 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
4740 return size1
- size
;
4743 int qemu_get_byte(QEMUFile
*f
)
4745 if (f
->buf_index
>= f
->buf_size
) {
4746 qemu_fill_buffer(f
);
4747 if (f
->buf_index
>= f
->buf_size
)
4750 return f
->buf
[f
->buf_index
++];
4753 int64_t qemu_ftell(QEMUFile
*f
)
4755 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
4758 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
4760 if (whence
== SEEK_SET
) {
4762 } else if (whence
== SEEK_CUR
) {
4763 pos
+= qemu_ftell(f
);
4765 /* SEEK_END not supported */
4768 if (f
->is_writable
) {
4770 f
->buf_offset
= pos
;
4772 f
->buf_offset
= pos
;
4779 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
4781 qemu_put_byte(f
, v
>> 8);
4782 qemu_put_byte(f
, v
);
4785 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
4787 qemu_put_byte(f
, v
>> 24);
4788 qemu_put_byte(f
, v
>> 16);
4789 qemu_put_byte(f
, v
>> 8);
4790 qemu_put_byte(f
, v
);
4793 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
4795 qemu_put_be32(f
, v
>> 32);
4796 qemu_put_be32(f
, v
);
4799 unsigned int qemu_get_be16(QEMUFile
*f
)
4802 v
= qemu_get_byte(f
) << 8;
4803 v
|= qemu_get_byte(f
);
4807 unsigned int qemu_get_be32(QEMUFile
*f
)
4810 v
= qemu_get_byte(f
) << 24;
4811 v
|= qemu_get_byte(f
) << 16;
4812 v
|= qemu_get_byte(f
) << 8;
4813 v
|= qemu_get_byte(f
);
4817 uint64_t qemu_get_be64(QEMUFile
*f
)
4820 v
= (uint64_t)qemu_get_be32(f
) << 32;
4821 v
|= qemu_get_be32(f
);
4825 typedef struct SaveStateEntry
{
4829 SaveStateHandler
*save_state
;
4830 LoadStateHandler
*load_state
;
4832 struct SaveStateEntry
*next
;
4835 static SaveStateEntry
*first_se
;
4837 int register_savevm(const char *idstr
,
4840 SaveStateHandler
*save_state
,
4841 LoadStateHandler
*load_state
,
4844 SaveStateEntry
*se
, **pse
;
4846 se
= qemu_malloc(sizeof(SaveStateEntry
));
4849 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
4850 se
->instance_id
= instance_id
;
4851 se
->version_id
= version_id
;
4852 se
->save_state
= save_state
;
4853 se
->load_state
= load_state
;
4854 se
->opaque
= opaque
;
4857 /* add at the end of list */
4859 while (*pse
!= NULL
)
4860 pse
= &(*pse
)->next
;
4865 #define QEMU_VM_FILE_MAGIC 0x5145564d
4866 #define QEMU_VM_FILE_VERSION 0x00000002
4868 int qemu_savevm_state(QEMUFile
*f
)
4872 int64_t cur_pos
, len_pos
, total_len_pos
;
4874 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
4875 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
4876 total_len_pos
= qemu_ftell(f
);
4877 qemu_put_be64(f
, 0); /* total size */
4879 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4881 len
= strlen(se
->idstr
);
4882 qemu_put_byte(f
, len
);
4883 qemu_put_buffer(f
, se
->idstr
, len
);
4885 qemu_put_be32(f
, se
->instance_id
);
4886 qemu_put_be32(f
, se
->version_id
);
4888 /* record size: filled later */
4889 len_pos
= qemu_ftell(f
);
4890 qemu_put_be32(f
, 0);
4892 se
->save_state(f
, se
->opaque
);
4894 /* fill record size */
4895 cur_pos
= qemu_ftell(f
);
4896 len
= cur_pos
- len_pos
- 4;
4897 qemu_fseek(f
, len_pos
, SEEK_SET
);
4898 qemu_put_be32(f
, len
);
4899 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4901 cur_pos
= qemu_ftell(f
);
4902 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
4903 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
4904 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4910 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
4914 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4915 if (!strcmp(se
->idstr
, idstr
) &&
4916 instance_id
== se
->instance_id
)
4922 int qemu_loadvm_state(QEMUFile
*f
)
4925 int len
, ret
, instance_id
, record_len
, version_id
;
4926 int64_t total_len
, end_pos
, cur_pos
;
4930 v
= qemu_get_be32(f
);
4931 if (v
!= QEMU_VM_FILE_MAGIC
)
4933 v
= qemu_get_be32(f
);
4934 if (v
!= QEMU_VM_FILE_VERSION
) {
4939 total_len
= qemu_get_be64(f
);
4940 end_pos
= total_len
+ qemu_ftell(f
);
4942 if (qemu_ftell(f
) >= end_pos
)
4944 len
= qemu_get_byte(f
);
4945 qemu_get_buffer(f
, idstr
, len
);
4947 instance_id
= qemu_get_be32(f
);
4948 version_id
= qemu_get_be32(f
);
4949 record_len
= qemu_get_be32(f
);
4951 printf("idstr=%s instance=0x%x version=%d len=%d\n",
4952 idstr
, instance_id
, version_id
, record_len
);
4954 cur_pos
= qemu_ftell(f
);
4955 se
= find_se(idstr
, instance_id
);
4957 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
4958 instance_id
, idstr
);
4960 ret
= se
->load_state(f
, se
->opaque
, version_id
);
4962 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
4963 instance_id
, idstr
);
4966 /* always seek to exact end of record */
4967 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
4974 /* device can contain snapshots */
4975 static int bdrv_can_snapshot(BlockDriverState
*bs
)
4978 !bdrv_is_removable(bs
) &&
4979 !bdrv_is_read_only(bs
));
4982 /* device must be snapshots in order to have a reliable snapshot */
4983 static int bdrv_has_snapshot(BlockDriverState
*bs
)
4986 !bdrv_is_removable(bs
) &&
4987 !bdrv_is_read_only(bs
));
4990 static BlockDriverState
*get_bs_snapshots(void)
4992 BlockDriverState
*bs
;
4996 return bs_snapshots
;
4997 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4999 if (bdrv_can_snapshot(bs
))
5008 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
5011 QEMUSnapshotInfo
*sn_tab
, *sn
;
5015 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5018 for(i
= 0; i
< nb_sns
; i
++) {
5020 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
5030 void do_savevm(const char *name
)
5032 BlockDriverState
*bs
, *bs1
;
5033 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
5034 int must_delete
, ret
, i
;
5035 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5037 int saved_vm_running
;
5044 bs
= get_bs_snapshots();
5046 term_printf("No block device can accept snapshots\n");
5050 /* ??? Should this occur after vm_stop? */
5053 saved_vm_running
= vm_running
;
5058 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
5063 memset(sn
, 0, sizeof(*sn
));
5065 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
5066 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
5069 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
5072 /* fill auxiliary fields */
5075 sn
->date_sec
= tb
.time
;
5076 sn
->date_nsec
= tb
.millitm
* 1000000;
5078 gettimeofday(&tv
, NULL
);
5079 sn
->date_sec
= tv
.tv_sec
;
5080 sn
->date_nsec
= tv
.tv_usec
* 1000;
5082 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
5084 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5085 term_printf("Device %s does not support VM state snapshots\n",
5086 bdrv_get_device_name(bs
));
5090 /* save the VM state */
5091 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
5093 term_printf("Could not open VM state file\n");
5096 ret
= qemu_savevm_state(f
);
5097 sn
->vm_state_size
= qemu_ftell(f
);
5100 term_printf("Error %d while writing VM\n", ret
);
5104 /* create the snapshots */
5106 for(i
= 0; i
< MAX_DISKS
; i
++) {
5108 if (bdrv_has_snapshot(bs1
)) {
5110 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
5112 term_printf("Error while deleting snapshot on '%s'\n",
5113 bdrv_get_device_name(bs1
));
5116 ret
= bdrv_snapshot_create(bs1
, sn
);
5118 term_printf("Error while creating snapshot on '%s'\n",
5119 bdrv_get_device_name(bs1
));
5125 if (saved_vm_running
)
5129 void do_loadvm(const char *name
)
5131 BlockDriverState
*bs
, *bs1
;
5132 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5135 int saved_vm_running
;
5137 bs
= get_bs_snapshots();
5139 term_printf("No block device supports snapshots\n");
5143 /* Flush all IO requests so they don't interfere with the new state. */
5146 saved_vm_running
= vm_running
;
5149 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5151 if (bdrv_has_snapshot(bs1
)) {
5152 ret
= bdrv_snapshot_goto(bs1
, name
);
5155 term_printf("Warning: ");
5158 term_printf("Snapshots not supported on device '%s'\n",
5159 bdrv_get_device_name(bs1
));
5162 term_printf("Could not find snapshot '%s' on device '%s'\n",
5163 name
, bdrv_get_device_name(bs1
));
5166 term_printf("Error %d while activating snapshot on '%s'\n",
5167 ret
, bdrv_get_device_name(bs1
));
5170 /* fatal on snapshot block device */
5177 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5178 term_printf("Device %s does not support VM state snapshots\n",
5179 bdrv_get_device_name(bs
));
5183 /* restore the VM state */
5184 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
5186 term_printf("Could not open VM state file\n");
5189 ret
= qemu_loadvm_state(f
);
5192 term_printf("Error %d while loading VM state\n", ret
);
5195 if (saved_vm_running
)
5199 void do_delvm(const char *name
)
5201 BlockDriverState
*bs
, *bs1
;
5204 bs
= get_bs_snapshots();
5206 term_printf("No block device supports snapshots\n");
5210 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5212 if (bdrv_has_snapshot(bs1
)) {
5213 ret
= bdrv_snapshot_delete(bs1
, name
);
5215 if (ret
== -ENOTSUP
)
5216 term_printf("Snapshots not supported on device '%s'\n",
5217 bdrv_get_device_name(bs1
));
5219 term_printf("Error %d while deleting snapshot on '%s'\n",
5220 ret
, bdrv_get_device_name(bs1
));
5226 void do_info_snapshots(void)
5228 BlockDriverState
*bs
, *bs1
;
5229 QEMUSnapshotInfo
*sn_tab
, *sn
;
5233 bs
= get_bs_snapshots();
5235 term_printf("No available block device supports snapshots\n");
5238 term_printf("Snapshot devices:");
5239 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5241 if (bdrv_has_snapshot(bs1
)) {
5243 term_printf(" %s", bdrv_get_device_name(bs1
));
5248 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5250 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
5253 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
5254 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
5255 for(i
= 0; i
< nb_sns
; i
++) {
5257 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
5262 /***********************************************************/
5263 /* cpu save/restore */
5265 #if defined(TARGET_I386)
5267 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
5269 qemu_put_be32(f
, dt
->selector
);
5270 qemu_put_betl(f
, dt
->base
);
5271 qemu_put_be32(f
, dt
->limit
);
5272 qemu_put_be32(f
, dt
->flags
);
5275 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
5277 dt
->selector
= qemu_get_be32(f
);
5278 dt
->base
= qemu_get_betl(f
);
5279 dt
->limit
= qemu_get_be32(f
);
5280 dt
->flags
= qemu_get_be32(f
);
5283 void cpu_save(QEMUFile
*f
, void *opaque
)
5285 CPUState
*env
= opaque
;
5286 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
5290 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5291 qemu_put_betls(f
, &env
->regs
[i
]);
5292 qemu_put_betls(f
, &env
->eip
);
5293 qemu_put_betls(f
, &env
->eflags
);
5294 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
5295 qemu_put_be32s(f
, &hflags
);
5299 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
5301 for(i
= 0; i
< 8; i
++) {
5302 fptag
|= ((!env
->fptags
[i
]) << i
);
5305 qemu_put_be16s(f
, &fpuc
);
5306 qemu_put_be16s(f
, &fpus
);
5307 qemu_put_be16s(f
, &fptag
);
5309 #ifdef USE_X86LDOUBLE
5314 qemu_put_be16s(f
, &fpregs_format
);
5316 for(i
= 0; i
< 8; i
++) {
5317 #ifdef USE_X86LDOUBLE
5321 /* we save the real CPU data (in case of MMX usage only 'mant'
5322 contains the MMX register */
5323 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
5324 qemu_put_be64(f
, mant
);
5325 qemu_put_be16(f
, exp
);
5328 /* if we use doubles for float emulation, we save the doubles to
5329 avoid losing information in case of MMX usage. It can give
5330 problems if the image is restored on a CPU where long
5331 doubles are used instead. */
5332 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
5336 for(i
= 0; i
< 6; i
++)
5337 cpu_put_seg(f
, &env
->segs
[i
]);
5338 cpu_put_seg(f
, &env
->ldt
);
5339 cpu_put_seg(f
, &env
->tr
);
5340 cpu_put_seg(f
, &env
->gdt
);
5341 cpu_put_seg(f
, &env
->idt
);
5343 qemu_put_be32s(f
, &env
->sysenter_cs
);
5344 qemu_put_be32s(f
, &env
->sysenter_esp
);
5345 qemu_put_be32s(f
, &env
->sysenter_eip
);
5347 qemu_put_betls(f
, &env
->cr
[0]);
5348 qemu_put_betls(f
, &env
->cr
[2]);
5349 qemu_put_betls(f
, &env
->cr
[3]);
5350 qemu_put_betls(f
, &env
->cr
[4]);
5352 for(i
= 0; i
< 8; i
++)
5353 qemu_put_betls(f
, &env
->dr
[i
]);
5356 qemu_put_be32s(f
, &env
->a20_mask
);
5359 qemu_put_be32s(f
, &env
->mxcsr
);
5360 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5361 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5362 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5365 #ifdef TARGET_X86_64
5366 qemu_put_be64s(f
, &env
->efer
);
5367 qemu_put_be64s(f
, &env
->star
);
5368 qemu_put_be64s(f
, &env
->lstar
);
5369 qemu_put_be64s(f
, &env
->cstar
);
5370 qemu_put_be64s(f
, &env
->fmask
);
5371 qemu_put_be64s(f
, &env
->kernelgsbase
);
5373 qemu_put_be32s(f
, &env
->smbase
);
5376 #ifdef USE_X86LDOUBLE
5377 /* XXX: add that in a FPU generic layer */
5378 union x86_longdouble
{
5383 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
5384 #define EXPBIAS1 1023
5385 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
5386 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
5388 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
5392 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
5393 /* exponent + sign */
5394 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
5395 e
|= SIGND1(temp
) >> 16;
5400 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5402 CPUState
*env
= opaque
;
5405 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
5407 if (version_id
!= 3 && version_id
!= 4)
5409 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5410 qemu_get_betls(f
, &env
->regs
[i
]);
5411 qemu_get_betls(f
, &env
->eip
);
5412 qemu_get_betls(f
, &env
->eflags
);
5413 qemu_get_be32s(f
, &hflags
);
5415 qemu_get_be16s(f
, &fpuc
);
5416 qemu_get_be16s(f
, &fpus
);
5417 qemu_get_be16s(f
, &fptag
);
5418 qemu_get_be16s(f
, &fpregs_format
);
5420 /* NOTE: we cannot always restore the FPU state if the image come
5421 from a host with a different 'USE_X86LDOUBLE' define. We guess
5422 if we are in an MMX state to restore correctly in that case. */
5423 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
5424 for(i
= 0; i
< 8; i
++) {
5428 switch(fpregs_format
) {
5430 mant
= qemu_get_be64(f
);
5431 exp
= qemu_get_be16(f
);
5432 #ifdef USE_X86LDOUBLE
5433 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5435 /* difficult case */
5437 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5439 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5443 mant
= qemu_get_be64(f
);
5444 #ifdef USE_X86LDOUBLE
5446 union x86_longdouble
*p
;
5447 /* difficult case */
5448 p
= (void *)&env
->fpregs
[i
];
5453 fp64_to_fp80(p
, mant
);
5457 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5466 /* XXX: restore FPU round state */
5467 env
->fpstt
= (fpus
>> 11) & 7;
5468 env
->fpus
= fpus
& ~0x3800;
5470 for(i
= 0; i
< 8; i
++) {
5471 env
->fptags
[i
] = (fptag
>> i
) & 1;
5474 for(i
= 0; i
< 6; i
++)
5475 cpu_get_seg(f
, &env
->segs
[i
]);
5476 cpu_get_seg(f
, &env
->ldt
);
5477 cpu_get_seg(f
, &env
->tr
);
5478 cpu_get_seg(f
, &env
->gdt
);
5479 cpu_get_seg(f
, &env
->idt
);
5481 qemu_get_be32s(f
, &env
->sysenter_cs
);
5482 qemu_get_be32s(f
, &env
->sysenter_esp
);
5483 qemu_get_be32s(f
, &env
->sysenter_eip
);
5485 qemu_get_betls(f
, &env
->cr
[0]);
5486 qemu_get_betls(f
, &env
->cr
[2]);
5487 qemu_get_betls(f
, &env
->cr
[3]);
5488 qemu_get_betls(f
, &env
->cr
[4]);
5490 for(i
= 0; i
< 8; i
++)
5491 qemu_get_betls(f
, &env
->dr
[i
]);
5494 qemu_get_be32s(f
, &env
->a20_mask
);
5496 qemu_get_be32s(f
, &env
->mxcsr
);
5497 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5498 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5499 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5502 #ifdef TARGET_X86_64
5503 qemu_get_be64s(f
, &env
->efer
);
5504 qemu_get_be64s(f
, &env
->star
);
5505 qemu_get_be64s(f
, &env
->lstar
);
5506 qemu_get_be64s(f
, &env
->cstar
);
5507 qemu_get_be64s(f
, &env
->fmask
);
5508 qemu_get_be64s(f
, &env
->kernelgsbase
);
5510 if (version_id
>= 4)
5511 qemu_get_be32s(f
, &env
->smbase
);
5513 /* XXX: compute hflags from scratch, except for CPL and IIF */
5514 env
->hflags
= hflags
;
5519 #elif defined(TARGET_PPC)
5520 void cpu_save(QEMUFile
*f
, void *opaque
)
5524 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5529 #elif defined(TARGET_MIPS)
5530 void cpu_save(QEMUFile
*f
, void *opaque
)
5534 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5539 #elif defined(TARGET_SPARC)
5540 void cpu_save(QEMUFile
*f
, void *opaque
)
5542 CPUState
*env
= opaque
;
5546 for(i
= 0; i
< 8; i
++)
5547 qemu_put_betls(f
, &env
->gregs
[i
]);
5548 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5549 qemu_put_betls(f
, &env
->regbase
[i
]);
5552 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5558 qemu_put_be32(f
, u
.i
);
5561 qemu_put_betls(f
, &env
->pc
);
5562 qemu_put_betls(f
, &env
->npc
);
5563 qemu_put_betls(f
, &env
->y
);
5565 qemu_put_be32(f
, tmp
);
5566 qemu_put_betls(f
, &env
->fsr
);
5567 qemu_put_betls(f
, &env
->tbr
);
5568 #ifndef TARGET_SPARC64
5569 qemu_put_be32s(f
, &env
->wim
);
5571 for(i
= 0; i
< 16; i
++)
5572 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
5576 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5578 CPUState
*env
= opaque
;
5582 for(i
= 0; i
< 8; i
++)
5583 qemu_get_betls(f
, &env
->gregs
[i
]);
5584 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5585 qemu_get_betls(f
, &env
->regbase
[i
]);
5588 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5593 u
.i
= qemu_get_be32(f
);
5597 qemu_get_betls(f
, &env
->pc
);
5598 qemu_get_betls(f
, &env
->npc
);
5599 qemu_get_betls(f
, &env
->y
);
5600 tmp
= qemu_get_be32(f
);
5601 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
5602 correctly updated */
5604 qemu_get_betls(f
, &env
->fsr
);
5605 qemu_get_betls(f
, &env
->tbr
);
5606 #ifndef TARGET_SPARC64
5607 qemu_get_be32s(f
, &env
->wim
);
5609 for(i
= 0; i
< 16; i
++)
5610 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
5616 #elif defined(TARGET_ARM)
5618 /* ??? Need to implement these. */
5619 void cpu_save(QEMUFile
*f
, void *opaque
)
5623 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5630 #warning No CPU save/restore functions
5634 /***********************************************************/
5635 /* ram save/restore */
5637 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
5641 v
= qemu_get_byte(f
);
5644 if (qemu_get_buffer(f
, buf
, len
) != len
)
5648 v
= qemu_get_byte(f
);
5649 memset(buf
, v
, len
);
5657 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
5661 if (qemu_get_be32(f
) != phys_ram_size
)
5663 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
5664 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
5671 #define BDRV_HASH_BLOCK_SIZE 1024
5672 #define IOBUF_SIZE 4096
5673 #define RAM_CBLOCK_MAGIC 0xfabe
5675 typedef struct RamCompressState
{
5678 uint8_t buf
[IOBUF_SIZE
];
5681 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
5684 memset(s
, 0, sizeof(*s
));
5686 ret
= deflateInit2(&s
->zstream
, 1,
5688 9, Z_DEFAULT_STRATEGY
);
5691 s
->zstream
.avail_out
= IOBUF_SIZE
;
5692 s
->zstream
.next_out
= s
->buf
;
5696 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
5698 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
5699 qemu_put_be16(s
->f
, len
);
5700 qemu_put_buffer(s
->f
, buf
, len
);
5703 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
5707 s
->zstream
.avail_in
= len
;
5708 s
->zstream
.next_in
= (uint8_t *)buf
;
5709 while (s
->zstream
.avail_in
> 0) {
5710 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
5713 if (s
->zstream
.avail_out
== 0) {
5714 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
5715 s
->zstream
.avail_out
= IOBUF_SIZE
;
5716 s
->zstream
.next_out
= s
->buf
;
5722 static void ram_compress_close(RamCompressState
*s
)
5726 /* compress last bytes */
5728 ret
= deflate(&s
->zstream
, Z_FINISH
);
5729 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
5730 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
5732 ram_put_cblock(s
, s
->buf
, len
);
5734 s
->zstream
.avail_out
= IOBUF_SIZE
;
5735 s
->zstream
.next_out
= s
->buf
;
5736 if (ret
== Z_STREAM_END
)
5743 deflateEnd(&s
->zstream
);
5746 typedef struct RamDecompressState
{
5749 uint8_t buf
[IOBUF_SIZE
];
5750 } RamDecompressState
;
5752 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
5755 memset(s
, 0, sizeof(*s
));
5757 ret
= inflateInit(&s
->zstream
);
5763 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
5767 s
->zstream
.avail_out
= len
;
5768 s
->zstream
.next_out
= buf
;
5769 while (s
->zstream
.avail_out
> 0) {
5770 if (s
->zstream
.avail_in
== 0) {
5771 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
5773 clen
= qemu_get_be16(s
->f
);
5774 if (clen
> IOBUF_SIZE
)
5776 qemu_get_buffer(s
->f
, s
->buf
, clen
);
5777 s
->zstream
.avail_in
= clen
;
5778 s
->zstream
.next_in
= s
->buf
;
5780 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
5781 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
5788 static void ram_decompress_close(RamDecompressState
*s
)
5790 inflateEnd(&s
->zstream
);
5793 static void ram_save(QEMUFile
*f
, void *opaque
)
5796 RamCompressState s1
, *s
= &s1
;
5799 qemu_put_be32(f
, phys_ram_size
);
5800 if (ram_compress_open(s
, f
) < 0)
5802 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5804 if (tight_savevm_enabled
) {
5808 /* find if the memory block is available on a virtual
5811 for(j
= 0; j
< MAX_DISKS
; j
++) {
5813 sector_num
= bdrv_hash_find(bs_table
[j
],
5814 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5815 if (sector_num
>= 0)
5820 goto normal_compress
;
5823 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
5824 ram_compress_buf(s
, buf
, 10);
5830 ram_compress_buf(s
, buf
, 1);
5831 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5834 ram_compress_close(s
);
5837 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
5839 RamDecompressState s1
, *s
= &s1
;
5843 if (version_id
== 1)
5844 return ram_load_v1(f
, opaque
);
5845 if (version_id
!= 2)
5847 if (qemu_get_be32(f
) != phys_ram_size
)
5849 if (ram_decompress_open(s
, f
) < 0)
5851 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5852 if (ram_decompress_buf(s
, buf
, 1) < 0) {
5853 fprintf(stderr
, "Error while reading ram block header\n");
5857 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
5858 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
5867 ram_decompress_buf(s
, buf
+ 1, 9);
5869 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
5870 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
5871 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
5874 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
5875 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
5876 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
5877 bs_index
, sector_num
);
5884 printf("Error block header\n");
5888 ram_decompress_close(s
);
5892 /***********************************************************/
5893 /* bottom halves (can be seen as timers which expire ASAP) */
5902 static QEMUBH
*first_bh
= NULL
;
5904 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
5907 bh
= qemu_mallocz(sizeof(QEMUBH
));
5911 bh
->opaque
= opaque
;
5915 int qemu_bh_poll(void)
5934 void qemu_bh_schedule(QEMUBH
*bh
)
5936 CPUState
*env
= cpu_single_env
;
5940 bh
->next
= first_bh
;
5943 /* stop the currently executing CPU to execute the BH ASAP */
5945 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
5949 void qemu_bh_cancel(QEMUBH
*bh
)
5952 if (bh
->scheduled
) {
5955 pbh
= &(*pbh
)->next
;
5961 void qemu_bh_delete(QEMUBH
*bh
)
5967 /***********************************************************/
5968 /* machine registration */
5970 QEMUMachine
*first_machine
= NULL
;
5972 int qemu_register_machine(QEMUMachine
*m
)
5975 pm
= &first_machine
;
5983 QEMUMachine
*find_machine(const char *name
)
5987 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5988 if (!strcmp(m
->name
, name
))
5994 /***********************************************************/
5995 /* main execution loop */
5997 void gui_update(void *opaque
)
5999 display_state
.dpy_refresh(&display_state
);
6000 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
6003 struct vm_change_state_entry
{
6004 VMChangeStateHandler
*cb
;
6006 LIST_ENTRY (vm_change_state_entry
) entries
;
6009 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
6011 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
6014 VMChangeStateEntry
*e
;
6016 e
= qemu_mallocz(sizeof (*e
));
6022 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
6026 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
6028 LIST_REMOVE (e
, entries
);
6032 static void vm_state_notify(int running
)
6034 VMChangeStateEntry
*e
;
6036 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
6037 e
->cb(e
->opaque
, running
);
6041 /* XXX: support several handlers */
6042 static VMStopHandler
*vm_stop_cb
;
6043 static void *vm_stop_opaque
;
6045 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6048 vm_stop_opaque
= opaque
;
6052 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6066 void vm_stop(int reason
)
6069 cpu_disable_ticks();
6073 vm_stop_cb(vm_stop_opaque
, reason
);
6080 /* reset/shutdown handler */
6082 typedef struct QEMUResetEntry
{
6083 QEMUResetHandler
*func
;
6085 struct QEMUResetEntry
*next
;
6088 static QEMUResetEntry
*first_reset_entry
;
6089 static int reset_requested
;
6090 static int shutdown_requested
;
6091 static int powerdown_requested
;
6093 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
6095 QEMUResetEntry
**pre
, *re
;
6097 pre
= &first_reset_entry
;
6098 while (*pre
!= NULL
)
6099 pre
= &(*pre
)->next
;
6100 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
6102 re
->opaque
= opaque
;
6107 static void qemu_system_reset(void)
6111 /* reset all devices */
6112 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
6113 re
->func(re
->opaque
);
6117 void qemu_system_reset_request(void)
6120 shutdown_requested
= 1;
6122 reset_requested
= 1;
6125 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6128 void qemu_system_shutdown_request(void)
6130 shutdown_requested
= 1;
6132 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6135 void qemu_system_powerdown_request(void)
6137 powerdown_requested
= 1;
6139 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6142 void main_loop_wait(int timeout
)
6144 IOHandlerRecord
*ioh
;
6145 fd_set rfds
, wfds
, xfds
;
6151 /* XXX: need to suppress polling by better using win32 events */
6153 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
6154 ret
|= pe
->func(pe
->opaque
);
6157 if (ret
== 0 && timeout
> 0) {
6159 WaitObjects
*w
= &wait_objects
;
6161 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
6162 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
6163 if (w
->func
[ret
- WAIT_OBJECT_0
])
6164 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
6165 } else if (ret
== WAIT_TIMEOUT
) {
6167 err
= GetLastError();
6168 fprintf(stderr
, "Wait error %d %d\n", ret
, err
);
6172 /* poll any events */
6173 /* XXX: separate device handlers from system ones */
6178 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6182 (!ioh
->fd_read_poll
||
6183 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
6184 FD_SET(ioh
->fd
, &rfds
);
6188 if (ioh
->fd_write
) {
6189 FD_SET(ioh
->fd
, &wfds
);
6199 tv
.tv_usec
= timeout
* 1000;
6201 #if defined(CONFIG_SLIRP)
6203 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
6206 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
6208 IOHandlerRecord
**pioh
;
6210 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6213 if (FD_ISSET(ioh
->fd
, &rfds
)) {
6214 ioh
->fd_read(ioh
->opaque
);
6216 if (FD_ISSET(ioh
->fd
, &wfds
)) {
6217 ioh
->fd_write(ioh
->opaque
);
6221 /* remove deleted IO handlers */
6222 pioh
= &first_io_handler
;
6232 #if defined(CONFIG_SLIRP)
6239 slirp_select_poll(&rfds
, &wfds
, &xfds
);
6246 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
6247 qemu_get_clock(vm_clock
));
6248 /* run dma transfers, if any */
6252 /* real time timers */
6253 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
6254 qemu_get_clock(rt_clock
));
6257 static CPUState
*cur_cpu
;
6262 #ifdef CONFIG_PROFILER
6267 cur_cpu
= first_cpu
;
6274 env
= env
->next_cpu
;
6277 #ifdef CONFIG_PROFILER
6278 ti
= profile_getclock();
6280 ret
= cpu_exec(env
);
6281 #ifdef CONFIG_PROFILER
6282 qemu_time
+= profile_getclock() - ti
;
6284 if (ret
== EXCP_HLT
) {
6285 /* Give the next CPU a chance to run. */
6289 if (ret
!= EXCP_HALTED
)
6291 /* all CPUs are halted ? */
6297 if (shutdown_requested
) {
6298 ret
= EXCP_INTERRUPT
;
6301 if (reset_requested
) {
6302 reset_requested
= 0;
6303 qemu_system_reset();
6304 ret
= EXCP_INTERRUPT
;
6306 if (powerdown_requested
) {
6307 powerdown_requested
= 0;
6308 qemu_system_powerdown();
6309 ret
= EXCP_INTERRUPT
;
6311 if (ret
== EXCP_DEBUG
) {
6312 vm_stop(EXCP_DEBUG
);
6314 /* If all cpus are halted then wait until the next IRQ */
6315 /* XXX: use timeout computed from timers */
6316 if (ret
== EXCP_HALTED
)
6323 #ifdef CONFIG_PROFILER
6324 ti
= profile_getclock();
6326 main_loop_wait(timeout
);
6327 #ifdef CONFIG_PROFILER
6328 dev_time
+= profile_getclock() - ti
;
6331 cpu_disable_ticks();
6337 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2007 Fabrice Bellard\n"
6338 "usage: %s [options] [disk_image]\n"
6340 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
6342 "Standard options:\n"
6343 "-M machine select emulated machine (-M ? for list)\n"
6344 "-cpu cpu select CPU (-cpu ? for list)\n"
6345 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
6346 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
6347 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
6348 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
6349 "-sd file use 'file' as SecureDigital card image\n"
6350 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
6351 "-snapshot write to temporary files instead of disk image files\n"
6353 "-no-frame open SDL window without a frame and window decorations\n"
6354 "-no-quit disable SDL window close capability\n"
6357 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
6359 "-m megs set virtual RAM size to megs MB [default=%d]\n"
6360 "-smp n set the number of CPUs to 'n' [default=1]\n"
6361 "-nographic disable graphical output and redirect serial I/Os to console\n"
6363 "-k language use keyboard layout (for example \"fr\" for French)\n"
6366 "-audio-help print list of audio drivers and their options\n"
6367 "-soundhw c1,... enable audio support\n"
6368 " and only specified sound cards (comma separated list)\n"
6369 " use -soundhw ? to get the list of supported cards\n"
6370 " use -soundhw all to enable all of them\n"
6372 "-localtime set the real time clock to local time [default=utc]\n"
6373 "-full-screen start in full screen\n"
6375 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
6377 "-usb enable the USB driver (will be the default soon)\n"
6378 "-usbdevice name add the host or guest USB device 'name'\n"
6379 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6380 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
6382 "-name string set the name of the guest\n"
6384 "Network options:\n"
6385 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
6386 " create a new Network Interface Card and connect it to VLAN 'n'\n"
6388 "-net user[,vlan=n][,hostname=host]\n"
6389 " connect the user mode network stack to VLAN 'n' and send\n"
6390 " hostname 'host' to DHCP clients\n"
6393 "-net tap[,vlan=n],ifname=name\n"
6394 " connect the host TAP network interface to VLAN 'n'\n"
6396 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
6397 " connect the host TAP network interface to VLAN 'n' and use\n"
6398 " the network script 'file' (default=%s);\n"
6399 " use 'script=no' to disable script execution;\n"
6400 " use 'fd=h' to connect to an already opened TAP interface\n"
6402 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
6403 " connect the vlan 'n' to another VLAN using a socket connection\n"
6404 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
6405 " connect the vlan 'n' to multicast maddr and port\n"
6406 "-net none use it alone to have zero network devices; if no -net option\n"
6407 " is provided, the default is '-net nic -net user'\n"
6410 "-tftp dir allow tftp access to files in dir [-net user]\n"
6411 "-bootp file advertise file in BOOTP replies\n"
6413 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
6415 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
6416 " redirect TCP or UDP connections from host to guest [-net user]\n"
6419 "Linux boot specific:\n"
6420 "-kernel bzImage use 'bzImage' as kernel image\n"
6421 "-append cmdline use 'cmdline' as kernel command line\n"
6422 "-initrd file use 'file' as initial ram disk\n"
6424 "Debug/Expert options:\n"
6425 "-monitor dev redirect the monitor to char device 'dev'\n"
6426 "-serial dev redirect the serial port to char device 'dev'\n"
6427 "-parallel dev redirect the parallel port to char device 'dev'\n"
6428 "-pidfile file Write PID to 'file'\n"
6429 "-S freeze CPU at startup (use 'c' to start execution)\n"
6430 "-s wait gdb connection to port\n"
6431 "-p port set gdb connection port [default=%s]\n"
6432 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
6433 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
6434 " translation (t=none or lba) (usually qemu can guess them)\n"
6435 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
6437 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
6438 "-no-kqemu disable KQEMU kernel module usage\n"
6440 #ifdef USE_CODE_COPY
6441 "-no-code-copy disable code copy acceleration\n"
6444 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
6445 " (default is CL-GD5446 PCI VGA)\n"
6446 "-no-acpi disable ACPI\n"
6448 "-no-reboot exit instead of rebooting\n"
6449 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
6450 "-vnc display start a VNC server on display\n"
6452 "-daemonize daemonize QEMU after initializing\n"
6454 "-option-rom rom load a file, rom, into the option ROM space\n"
6456 "During emulation, the following keys are useful:\n"
6457 "ctrl-alt-f toggle full screen\n"
6458 "ctrl-alt-n switch to virtual console 'n'\n"
6459 "ctrl-alt toggle mouse and keyboard grab\n"
6461 "When using -nographic, press 'ctrl-a h' to get some help.\n"
6466 DEFAULT_NETWORK_SCRIPT
,
6468 DEFAULT_GDBSTUB_PORT
,
6473 #define HAS_ARG 0x0001
6489 QEMU_OPTION_snapshot
,
6491 QEMU_OPTION_no_fd_bootchk
,
6494 QEMU_OPTION_nographic
,
6496 QEMU_OPTION_audio_help
,
6497 QEMU_OPTION_soundhw
,
6516 QEMU_OPTION_no_code_copy
,
6518 QEMU_OPTION_localtime
,
6519 QEMU_OPTION_cirrusvga
,
6522 QEMU_OPTION_std_vga
,
6524 QEMU_OPTION_monitor
,
6526 QEMU_OPTION_parallel
,
6528 QEMU_OPTION_full_screen
,
6529 QEMU_OPTION_no_frame
,
6530 QEMU_OPTION_no_quit
,
6531 QEMU_OPTION_pidfile
,
6532 QEMU_OPTION_no_kqemu
,
6533 QEMU_OPTION_kernel_kqemu
,
6534 QEMU_OPTION_win2k_hack
,
6536 QEMU_OPTION_usbdevice
,
6539 QEMU_OPTION_no_acpi
,
6540 QEMU_OPTION_no_reboot
,
6541 QEMU_OPTION_daemonize
,
6542 QEMU_OPTION_option_rom
,
6543 QEMU_OPTION_semihosting
,
6547 typedef struct QEMUOption
{
6553 const QEMUOption qemu_options
[] = {
6554 { "h", 0, QEMU_OPTION_h
},
6555 { "help", 0, QEMU_OPTION_h
},
6557 { "M", HAS_ARG
, QEMU_OPTION_M
},
6558 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
6559 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
6560 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
6561 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
6562 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
6563 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
6564 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
6565 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
6566 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
6567 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
6568 { "snapshot", 0, QEMU_OPTION_snapshot
},
6570 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
6572 { "m", HAS_ARG
, QEMU_OPTION_m
},
6573 { "nographic", 0, QEMU_OPTION_nographic
},
6574 { "k", HAS_ARG
, QEMU_OPTION_k
},
6576 { "audio-help", 0, QEMU_OPTION_audio_help
},
6577 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
6580 { "net", HAS_ARG
, QEMU_OPTION_net
},
6582 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
6583 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
6585 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
6587 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
6590 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
6591 { "append", HAS_ARG
, QEMU_OPTION_append
},
6592 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
6594 { "S", 0, QEMU_OPTION_S
},
6595 { "s", 0, QEMU_OPTION_s
},
6596 { "p", HAS_ARG
, QEMU_OPTION_p
},
6597 { "d", HAS_ARG
, QEMU_OPTION_d
},
6598 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
6599 { "L", HAS_ARG
, QEMU_OPTION_L
},
6600 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
6602 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
6603 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
6605 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6606 { "g", 1, QEMU_OPTION_g
},
6608 { "localtime", 0, QEMU_OPTION_localtime
},
6609 { "std-vga", 0, QEMU_OPTION_std_vga
},
6610 { "echr", 1, QEMU_OPTION_echr
},
6611 { "monitor", 1, QEMU_OPTION_monitor
},
6612 { "serial", 1, QEMU_OPTION_serial
},
6613 { "parallel", 1, QEMU_OPTION_parallel
},
6614 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
6615 { "full-screen", 0, QEMU_OPTION_full_screen
},
6617 { "no-frame", 0, QEMU_OPTION_no_frame
},
6618 { "no-quit", 0, QEMU_OPTION_no_quit
},
6620 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
6621 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
6622 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
6623 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
6624 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
6626 /* temporary options */
6627 { "usb", 0, QEMU_OPTION_usb
},
6628 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
6629 { "vmwarevga", 0, QEMU_OPTION_vmsvga
},
6630 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
6631 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
6632 { "daemonize", 0, QEMU_OPTION_daemonize
},
6633 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
6634 #if defined(TARGET_ARM)
6635 { "semihosting", 0, QEMU_OPTION_semihosting
},
6637 { "name", HAS_ARG
, QEMU_OPTION_name
},
6641 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
6643 /* this stack is only used during signal handling */
6644 #define SIGNAL_STACK_SIZE 32768
6646 static uint8_t *signal_stack
;
6650 /* password input */
6652 static BlockDriverState
*get_bdrv(int index
)
6654 BlockDriverState
*bs
;
6657 bs
= bs_table
[index
];
6658 } else if (index
< 6) {
6659 bs
= fd_table
[index
- 4];
6666 static void read_passwords(void)
6668 BlockDriverState
*bs
;
6672 for(i
= 0; i
< 6; i
++) {
6674 if (bs
&& bdrv_is_encrypted(bs
)) {
6675 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
6676 for(j
= 0; j
< 3; j
++) {
6677 monitor_readline("Password: ",
6678 1, password
, sizeof(password
));
6679 if (bdrv_set_key(bs
, password
) == 0)
6681 term_printf("invalid password\n");
6687 /* XXX: currently we cannot use simultaneously different CPUs */
6688 void register_machines(void)
6690 #if defined(TARGET_I386)
6691 qemu_register_machine(&pc_machine
);
6692 qemu_register_machine(&isapc_machine
);
6693 #elif defined(TARGET_PPC)
6694 qemu_register_machine(&heathrow_machine
);
6695 qemu_register_machine(&core99_machine
);
6696 qemu_register_machine(&prep_machine
);
6697 #elif defined(TARGET_MIPS)
6698 qemu_register_machine(&mips_machine
);
6699 qemu_register_machine(&mips_malta_machine
);
6700 qemu_register_machine(&mips_pica61_machine
);
6701 #elif defined(TARGET_SPARC)
6702 #ifdef TARGET_SPARC64
6703 qemu_register_machine(&sun4u_machine
);
6705 qemu_register_machine(&ss5_machine
);
6706 qemu_register_machine(&ss10_machine
);
6708 #elif defined(TARGET_ARM)
6709 qemu_register_machine(&integratorcp_machine
);
6710 qemu_register_machine(&versatilepb_machine
);
6711 qemu_register_machine(&versatileab_machine
);
6712 qemu_register_machine(&realview_machine
);
6713 #elif defined(TARGET_SH4)
6714 qemu_register_machine(&shix_machine
);
6715 #elif defined(TARGET_ALPHA)
6718 #error unsupported CPU
6723 struct soundhw soundhw
[] = {
6730 { .init_isa
= pcspk_audio_init
}
6735 "Creative Sound Blaster 16",
6738 { .init_isa
= SB16_init
}
6745 "Yamaha YMF262 (OPL3)",
6747 "Yamaha YM3812 (OPL2)",
6751 { .init_isa
= Adlib_init
}
6758 "Gravis Ultrasound GF1",
6761 { .init_isa
= GUS_init
}
6767 "ENSONIQ AudioPCI ES1370",
6770 { .init_pci
= es1370_init
}
6773 { NULL
, NULL
, 0, 0, { NULL
} }
6776 static void select_soundhw (const char *optarg
)
6780 if (*optarg
== '?') {
6783 printf ("Valid sound card names (comma separated):\n");
6784 for (c
= soundhw
; c
->name
; ++c
) {
6785 printf ("%-11s %s\n", c
->name
, c
->descr
);
6787 printf ("\n-soundhw all will enable all of the above\n");
6788 exit (*optarg
!= '?');
6796 if (!strcmp (optarg
, "all")) {
6797 for (c
= soundhw
; c
->name
; ++c
) {
6805 e
= strchr (p
, ',');
6806 l
= !e
? strlen (p
) : (size_t) (e
- p
);
6808 for (c
= soundhw
; c
->name
; ++c
) {
6809 if (!strncmp (c
->name
, p
, l
)) {
6818 "Unknown sound card name (too big to show)\n");
6821 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
6826 p
+= l
+ (e
!= NULL
);
6830 goto show_valid_cards
;
6836 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
6838 exit(STATUS_CONTROL_C_EXIT
);
6843 #define MAX_NET_CLIENTS 32
6845 int main(int argc
, char **argv
)
6847 #ifdef CONFIG_GDBSTUB
6849 const char *gdbstub_port
;
6852 int snapshot
, linux_boot
;
6853 const char *initrd_filename
;
6854 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
6855 const char *sd_filename
;
6856 const char *kernel_filename
, *kernel_cmdline
;
6857 DisplayState
*ds
= &display_state
;
6858 int cyls
, heads
, secs
, translation
;
6859 char net_clients
[MAX_NET_CLIENTS
][256];
6862 const char *r
, *optarg
;
6863 CharDriverState
*monitor_hd
;
6864 char monitor_device
[128];
6865 char serial_devices
[MAX_SERIAL_PORTS
][128];
6866 int serial_device_index
;
6867 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
6868 int parallel_device_index
;
6869 const char *loadvm
= NULL
;
6870 QEMUMachine
*machine
;
6871 const char *cpu_model
;
6872 char usb_devices
[MAX_USB_CMDLINE
][128];
6873 int usb_devices_index
;
6875 const char *pid_file
= NULL
;
6877 LIST_INIT (&vm_change_state_head
);
6880 struct sigaction act
;
6881 sigfillset(&act
.sa_mask
);
6883 act
.sa_handler
= SIG_IGN
;
6884 sigaction(SIGPIPE
, &act
, NULL
);
6887 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
6888 /* Note: cpu_interrupt() is currently not SMP safe, so we force
6889 QEMU to run on a single CPU */
6894 h
= GetCurrentProcess();
6895 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
6896 for(i
= 0; i
< 32; i
++) {
6897 if (mask
& (1 << i
))
6902 SetProcessAffinityMask(h
, mask
);
6908 register_machines();
6909 machine
= first_machine
;
6911 initrd_filename
= NULL
;
6912 for(i
= 0; i
< MAX_FD
; i
++)
6913 fd_filename
[i
] = NULL
;
6914 for(i
= 0; i
< MAX_DISKS
; i
++)
6915 hd_filename
[i
] = NULL
;
6917 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
6918 vga_ram_size
= VGA_RAM_SIZE
;
6919 #ifdef CONFIG_GDBSTUB
6921 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
6925 kernel_filename
= NULL
;
6926 kernel_cmdline
= "";
6932 cyls
= heads
= secs
= 0;
6933 translation
= BIOS_ATA_TRANSLATION_AUTO
;
6934 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
6936 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
6937 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
6938 serial_devices
[i
][0] = '\0';
6939 serial_device_index
= 0;
6941 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
6942 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
6943 parallel_devices
[i
][0] = '\0';
6944 parallel_device_index
= 0;
6946 usb_devices_index
= 0;
6951 /* default mac address of the first network interface */
6959 hd_filename
[0] = argv
[optind
++];
6961 const QEMUOption
*popt
;
6964 /* Treat --foo the same as -foo. */
6967 popt
= qemu_options
;
6970 fprintf(stderr
, "%s: invalid option -- '%s'\n",
6974 if (!strcmp(popt
->name
, r
+ 1))
6978 if (popt
->flags
& HAS_ARG
) {
6979 if (optind
>= argc
) {
6980 fprintf(stderr
, "%s: option '%s' requires an argument\n",
6984 optarg
= argv
[optind
++];
6989 switch(popt
->index
) {
6991 machine
= find_machine(optarg
);
6994 printf("Supported machines are:\n");
6995 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6996 printf("%-10s %s%s\n",
6998 m
== first_machine
? " (default)" : "");
7003 case QEMU_OPTION_cpu
:
7004 /* hw initialization will check this */
7005 if (optarg
[0] == '?') {
7006 #if defined(TARGET_PPC)
7007 ppc_cpu_list(stdout
, &fprintf
);
7008 #elif defined(TARGET_ARM)
7010 #elif defined(TARGET_MIPS)
7011 mips_cpu_list(stdout
, &fprintf
);
7012 #elif defined(TARGET_SPARC)
7013 sparc_cpu_list(stdout
, &fprintf
);
7020 case QEMU_OPTION_initrd
:
7021 initrd_filename
= optarg
;
7023 case QEMU_OPTION_hda
:
7024 case QEMU_OPTION_hdb
:
7025 case QEMU_OPTION_hdc
:
7026 case QEMU_OPTION_hdd
:
7029 hd_index
= popt
->index
- QEMU_OPTION_hda
;
7030 hd_filename
[hd_index
] = optarg
;
7031 if (hd_index
== cdrom_index
)
7035 case QEMU_OPTION_sd
:
7036 sd_filename
= optarg
;
7038 case QEMU_OPTION_snapshot
:
7041 case QEMU_OPTION_hdachs
:
7045 cyls
= strtol(p
, (char **)&p
, 0);
7046 if (cyls
< 1 || cyls
> 16383)
7051 heads
= strtol(p
, (char **)&p
, 0);
7052 if (heads
< 1 || heads
> 16)
7057 secs
= strtol(p
, (char **)&p
, 0);
7058 if (secs
< 1 || secs
> 63)
7062 if (!strcmp(p
, "none"))
7063 translation
= BIOS_ATA_TRANSLATION_NONE
;
7064 else if (!strcmp(p
, "lba"))
7065 translation
= BIOS_ATA_TRANSLATION_LBA
;
7066 else if (!strcmp(p
, "auto"))
7067 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7070 } else if (*p
!= '\0') {
7072 fprintf(stderr
, "qemu: invalid physical CHS format\n");
7077 case QEMU_OPTION_nographic
:
7078 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
7079 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "null");
7080 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
7083 case QEMU_OPTION_kernel
:
7084 kernel_filename
= optarg
;
7086 case QEMU_OPTION_append
:
7087 kernel_cmdline
= optarg
;
7089 case QEMU_OPTION_cdrom
:
7090 if (cdrom_index
>= 0) {
7091 hd_filename
[cdrom_index
] = optarg
;
7094 case QEMU_OPTION_boot
:
7095 boot_device
= optarg
[0];
7096 if (boot_device
!= 'a' &&
7097 #if defined(TARGET_SPARC) || defined(TARGET_I386)
7099 boot_device
!= 'n' &&
7101 boot_device
!= 'c' && boot_device
!= 'd') {
7102 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
7106 case QEMU_OPTION_fda
:
7107 fd_filename
[0] = optarg
;
7109 case QEMU_OPTION_fdb
:
7110 fd_filename
[1] = optarg
;
7113 case QEMU_OPTION_no_fd_bootchk
:
7117 case QEMU_OPTION_no_code_copy
:
7118 code_copy_enabled
= 0;
7120 case QEMU_OPTION_net
:
7121 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
7122 fprintf(stderr
, "qemu: too many network clients\n");
7125 pstrcpy(net_clients
[nb_net_clients
],
7126 sizeof(net_clients
[0]),
7131 case QEMU_OPTION_tftp
:
7132 tftp_prefix
= optarg
;
7134 case QEMU_OPTION_bootp
:
7135 bootp_filename
= optarg
;
7138 case QEMU_OPTION_smb
:
7139 net_slirp_smb(optarg
);
7142 case QEMU_OPTION_redir
:
7143 net_slirp_redir(optarg
);
7147 case QEMU_OPTION_audio_help
:
7151 case QEMU_OPTION_soundhw
:
7152 select_soundhw (optarg
);
7159 ram_size
= atoi(optarg
) * 1024 * 1024;
7162 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
7163 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
7164 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
7173 mask
= cpu_str_to_log_mask(optarg
);
7175 printf("Log items (comma separated):\n");
7176 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
7177 printf("%-10s %s\n", item
->name
, item
->help
);
7184 #ifdef CONFIG_GDBSTUB
7189 gdbstub_port
= optarg
;
7199 keyboard_layout
= optarg
;
7201 case QEMU_OPTION_localtime
:
7204 case QEMU_OPTION_cirrusvga
:
7205 cirrus_vga_enabled
= 1;
7208 case QEMU_OPTION_vmsvga
:
7209 cirrus_vga_enabled
= 0;
7212 case QEMU_OPTION_std_vga
:
7213 cirrus_vga_enabled
= 0;
7221 w
= strtol(p
, (char **)&p
, 10);
7224 fprintf(stderr
, "qemu: invalid resolution or depth\n");
7230 h
= strtol(p
, (char **)&p
, 10);
7235 depth
= strtol(p
, (char **)&p
, 10);
7236 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
7237 depth
!= 24 && depth
!= 32)
7239 } else if (*p
== '\0') {
7240 depth
= graphic_depth
;
7247 graphic_depth
= depth
;
7250 case QEMU_OPTION_echr
:
7253 term_escape_char
= strtol(optarg
, &r
, 0);
7255 printf("Bad argument to echr\n");
7258 case QEMU_OPTION_monitor
:
7259 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
7261 case QEMU_OPTION_serial
:
7262 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
7263 fprintf(stderr
, "qemu: too many serial ports\n");
7266 pstrcpy(serial_devices
[serial_device_index
],
7267 sizeof(serial_devices
[0]), optarg
);
7268 serial_device_index
++;
7270 case QEMU_OPTION_parallel
:
7271 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
7272 fprintf(stderr
, "qemu: too many parallel ports\n");
7275 pstrcpy(parallel_devices
[parallel_device_index
],
7276 sizeof(parallel_devices
[0]), optarg
);
7277 parallel_device_index
++;
7279 case QEMU_OPTION_loadvm
:
7282 case QEMU_OPTION_full_screen
:
7286 case QEMU_OPTION_no_frame
:
7289 case QEMU_OPTION_no_quit
:
7293 case QEMU_OPTION_pidfile
:
7297 case QEMU_OPTION_win2k_hack
:
7298 win2k_install_hack
= 1;
7302 case QEMU_OPTION_no_kqemu
:
7305 case QEMU_OPTION_kernel_kqemu
:
7309 case QEMU_OPTION_usb
:
7312 case QEMU_OPTION_usbdevice
:
7314 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
7315 fprintf(stderr
, "Too many USB devices\n");
7318 pstrcpy(usb_devices
[usb_devices_index
],
7319 sizeof(usb_devices
[usb_devices_index
]),
7321 usb_devices_index
++;
7323 case QEMU_OPTION_smp
:
7324 smp_cpus
= atoi(optarg
);
7325 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
7326 fprintf(stderr
, "Invalid number of CPUs\n");
7330 case QEMU_OPTION_vnc
:
7331 vnc_display
= optarg
;
7333 case QEMU_OPTION_no_acpi
:
7336 case QEMU_OPTION_no_reboot
:
7339 case QEMU_OPTION_daemonize
:
7342 case QEMU_OPTION_option_rom
:
7343 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
7344 fprintf(stderr
, "Too many option ROMs\n");
7347 option_rom
[nb_option_roms
] = optarg
;
7350 case QEMU_OPTION_semihosting
:
7351 semihosting_enabled
= 1;
7353 case QEMU_OPTION_name
:
7361 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
7362 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
7369 if (pipe(fds
) == -1)
7380 len
= read(fds
[0], &status
, 1);
7381 if (len
== -1 && (errno
== EINTR
))
7386 else if (status
== 1) {
7387 fprintf(stderr
, "Could not acquire pidfile\n");
7405 signal(SIGTSTP
, SIG_IGN
);
7406 signal(SIGTTOU
, SIG_IGN
);
7407 signal(SIGTTIN
, SIG_IGN
);
7411 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
7414 write(fds
[1], &status
, 1);
7416 fprintf(stderr
, "Could not acquire pid file\n");
7424 linux_boot
= (kernel_filename
!= NULL
);
7427 boot_device
!= 'n' &&
7428 hd_filename
[0] == '\0' &&
7429 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
7430 fd_filename
[0] == '\0')
7433 /* boot to floppy or the default cd if no hard disk defined yet */
7434 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
7435 if (fd_filename
[0] != '\0')
7441 setvbuf(stdout
, NULL
, _IOLBF
, 0);
7451 /* init network clients */
7452 if (nb_net_clients
== 0) {
7453 /* if no clients, we use a default config */
7454 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
7456 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
7461 for(i
= 0;i
< nb_net_clients
; i
++) {
7462 if (net_client_init(net_clients
[i
]) < 0)
7467 if (boot_device
== 'n') {
7468 for (i
= 0; i
< nb_nics
; i
++) {
7469 const char *model
= nd_table
[i
].model
;
7473 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
7474 if (get_image_size(buf
) > 0) {
7475 option_rom
[nb_option_roms
] = strdup(buf
);
7481 fprintf(stderr
, "No valid PXE rom found for network device\n");
7484 boot_device
= 'c'; /* to prevent confusion by the BIOS */
7488 /* init the memory */
7489 phys_ram_size
= ram_size
+ vga_ram_size
+ MAX_BIOS_SIZE
;
7491 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
7492 if (!phys_ram_base
) {
7493 fprintf(stderr
, "Could not allocate physical memory\n");
7497 /* we always create the cdrom drive, even if no disk is there */
7499 if (cdrom_index
>= 0) {
7500 bs_table
[cdrom_index
] = bdrv_new("cdrom");
7501 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
7504 /* open the virtual block devices */
7505 for(i
= 0; i
< MAX_DISKS
; i
++) {
7506 if (hd_filename
[i
]) {
7509 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
7510 bs_table
[i
] = bdrv_new(buf
);
7512 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7513 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
7517 if (i
== 0 && cyls
!= 0) {
7518 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
7519 bdrv_set_translation_hint(bs_table
[i
], translation
);
7524 /* we always create at least one floppy disk */
7525 fd_table
[0] = bdrv_new("fda");
7526 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
7528 for(i
= 0; i
< MAX_FD
; i
++) {
7529 if (fd_filename
[i
]) {
7532 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
7533 fd_table
[i
] = bdrv_new(buf
);
7534 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
7536 if (fd_filename
[i
][0] != '\0') {
7537 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
7538 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7539 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
7547 sd_bdrv
= bdrv_new ("sd");
7548 /* FIXME: This isn't really a floppy, but it's a reasonable
7550 bdrv_set_type_hint(sd_bdrv
, BDRV_TYPE_FLOPPY
);
7552 if (bdrv_open(sd_bdrv
, sd_filename
,
7553 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7554 fprintf(stderr
, "qemu: could not open SD card image %s\n",
7559 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
7560 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
7566 dumb_display_init(ds
);
7567 } else if (vnc_display
!= NULL
) {
7568 vnc_display_init(ds
, vnc_display
);
7570 #if defined(CONFIG_SDL)
7571 sdl_display_init(ds
, full_screen
, no_frame
);
7572 #elif defined(CONFIG_COCOA)
7573 cocoa_display_init(ds
, full_screen
);
7575 dumb_display_init(ds
);
7579 /* Maintain compatibility with multiple stdio monitors */
7580 if (!strcmp(monitor_device
,"stdio")) {
7581 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7582 if (!strcmp(serial_devices
[i
],"mon:stdio")) {
7583 monitor_device
[0] = '\0';
7585 } else if (!strcmp(serial_devices
[i
],"stdio")) {
7586 monitor_device
[0] = '\0';
7587 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "mon:stdio");
7592 if (monitor_device
[0] != '\0') {
7593 monitor_hd
= qemu_chr_open(monitor_device
);
7595 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
7598 monitor_init(monitor_hd
, !nographic
);
7601 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7602 const char *devname
= serial_devices
[i
];
7603 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7604 serial_hds
[i
] = qemu_chr_open(devname
);
7605 if (!serial_hds
[i
]) {
7606 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
7610 if (!strcmp(devname
, "vc"))
7611 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
7615 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
7616 const char *devname
= parallel_devices
[i
];
7617 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7618 parallel_hds
[i
] = qemu_chr_open(devname
);
7619 if (!parallel_hds
[i
]) {
7620 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
7624 if (!strcmp(devname
, "vc"))
7625 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
7629 machine
->init(ram_size
, vga_ram_size
, boot_device
,
7630 ds
, fd_filename
, snapshot
,
7631 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
7633 /* init USB devices */
7635 for(i
= 0; i
< usb_devices_index
; i
++) {
7636 if (usb_device_add(usb_devices
[i
]) < 0) {
7637 fprintf(stderr
, "Warning: could not add USB device %s\n",
7643 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
7644 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
7646 #ifdef CONFIG_GDBSTUB
7648 /* XXX: use standard host:port notation and modify options
7650 if (gdbserver_start(gdbstub_port
) < 0) {
7651 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
7661 /* XXX: simplify init */
7674 len
= write(fds
[1], &status
, 1);
7675 if (len
== -1 && (errno
== EINTR
))
7681 fd
= open("/dev/null", O_RDWR
);