4 * Copyright (c) 2003-2006 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>
62 #if defined(CONFIG_SLIRP)
68 #include <sys/timeb.h>
70 #define getopt_long_only getopt_long
71 #define memalign(align, size) malloc(size)
74 #include "qemu_socket.h"
80 #endif /* CONFIG_SDL */
84 #define main qemu_main
85 #endif /* CONFIG_COCOA */
91 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
93 //#define DEBUG_UNUSED_IOPORT
94 //#define DEBUG_IOPORT
96 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
99 #define DEFAULT_RAM_SIZE 144
101 #define DEFAULT_RAM_SIZE 128
104 #define GUI_REFRESH_INTERVAL 30
106 /* Max number of USB devices that can be specified on the commandline. */
107 #define MAX_USB_CMDLINE 8
109 /* XXX: use a two level table to limit memory usage */
110 #define MAX_IOPORTS 65536
112 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
113 char phys_ram_file
[1024];
114 void *ioport_opaque
[MAX_IOPORTS
];
115 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
116 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
117 /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available
118 to store the VM snapshots */
119 BlockDriverState
*bs_table
[MAX_DISKS
+ 1], *fd_table
[MAX_FD
];
120 /* point to the block driver where the snapshots are managed */
121 BlockDriverState
*bs_snapshots
;
124 static DisplayState display_state
;
126 const char* keyboard_layout
= NULL
;
127 int64_t ticks_per_sec
;
128 int boot_device
= 'c';
130 int pit_min_timer_count
= 0;
132 NICInfo nd_table
[MAX_NICS
];
133 QEMUTimer
*gui_timer
;
136 int cirrus_vga_enabled
= 1;
138 int graphic_width
= 1024;
139 int graphic_height
= 768;
141 int graphic_width
= 800;
142 int graphic_height
= 600;
144 int graphic_depth
= 15;
147 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
148 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
150 int win2k_install_hack
= 0;
153 static VLANState
*first_vlan
;
155 int vnc_display
= -1;
156 #if defined(TARGET_SPARC)
158 #elif defined(TARGET_I386)
163 int acpi_enabled
= 1;
167 /***********************************************************/
168 /* x86 ISA bus support */
170 target_phys_addr_t isa_mem_base
= 0;
173 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
175 #ifdef DEBUG_UNUSED_IOPORT
176 fprintf(stderr
, "inb: port=0x%04x\n", address
);
181 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
183 #ifdef DEBUG_UNUSED_IOPORT
184 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
188 /* default is to make two byte accesses */
189 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
192 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
193 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
194 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
198 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
200 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
201 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
202 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
205 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
207 #ifdef DEBUG_UNUSED_IOPORT
208 fprintf(stderr
, "inl: port=0x%04x\n", address
);
213 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
215 #ifdef DEBUG_UNUSED_IOPORT
216 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
220 void init_ioports(void)
224 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
225 ioport_read_table
[0][i
] = default_ioport_readb
;
226 ioport_write_table
[0][i
] = default_ioport_writeb
;
227 ioport_read_table
[1][i
] = default_ioport_readw
;
228 ioport_write_table
[1][i
] = default_ioport_writew
;
229 ioport_read_table
[2][i
] = default_ioport_readl
;
230 ioport_write_table
[2][i
] = default_ioport_writel
;
234 /* size is the word size in byte */
235 int register_ioport_read(int start
, int length
, int size
,
236 IOPortReadFunc
*func
, void *opaque
)
242 } else if (size
== 2) {
244 } else if (size
== 4) {
247 hw_error("register_ioport_read: invalid size");
250 for(i
= start
; i
< start
+ length
; i
+= size
) {
251 ioport_read_table
[bsize
][i
] = func
;
252 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
253 hw_error("register_ioport_read: invalid opaque");
254 ioport_opaque
[i
] = opaque
;
259 /* size is the word size in byte */
260 int register_ioport_write(int start
, int length
, int size
,
261 IOPortWriteFunc
*func
, void *opaque
)
267 } else if (size
== 2) {
269 } else if (size
== 4) {
272 hw_error("register_ioport_write: invalid size");
275 for(i
= start
; i
< start
+ length
; i
+= size
) {
276 ioport_write_table
[bsize
][i
] = func
;
277 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
278 hw_error("register_ioport_read: invalid opaque");
279 ioport_opaque
[i
] = opaque
;
284 void isa_unassign_ioport(int start
, int length
)
288 for(i
= start
; i
< start
+ length
; i
++) {
289 ioport_read_table
[0][i
] = default_ioport_readb
;
290 ioport_read_table
[1][i
] = default_ioport_readw
;
291 ioport_read_table
[2][i
] = default_ioport_readl
;
293 ioport_write_table
[0][i
] = default_ioport_writeb
;
294 ioport_write_table
[1][i
] = default_ioport_writew
;
295 ioport_write_table
[2][i
] = default_ioport_writel
;
299 /***********************************************************/
301 void pstrcpy(char *buf
, int buf_size
, const char *str
)
311 if (c
== 0 || q
>= buf
+ buf_size
- 1)
318 /* strcat and truncate. */
319 char *pstrcat(char *buf
, int buf_size
, const char *s
)
324 pstrcpy(buf
+ len
, buf_size
- len
, s
);
328 int strstart(const char *str
, const char *val
, const char **ptr
)
344 void cpu_outb(CPUState
*env
, int addr
, int val
)
347 if (loglevel
& CPU_LOG_IOPORT
)
348 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
350 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
353 env
->last_io_time
= cpu_get_time_fast();
357 void cpu_outw(CPUState
*env
, int addr
, int val
)
360 if (loglevel
& CPU_LOG_IOPORT
)
361 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
363 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
366 env
->last_io_time
= cpu_get_time_fast();
370 void cpu_outl(CPUState
*env
, int addr
, int val
)
373 if (loglevel
& CPU_LOG_IOPORT
)
374 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
376 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
379 env
->last_io_time
= cpu_get_time_fast();
383 int cpu_inb(CPUState
*env
, int addr
)
386 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
388 if (loglevel
& CPU_LOG_IOPORT
)
389 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
393 env
->last_io_time
= cpu_get_time_fast();
398 int cpu_inw(CPUState
*env
, int addr
)
401 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
403 if (loglevel
& CPU_LOG_IOPORT
)
404 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
408 env
->last_io_time
= cpu_get_time_fast();
413 int cpu_inl(CPUState
*env
, int addr
)
416 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
418 if (loglevel
& CPU_LOG_IOPORT
)
419 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
423 env
->last_io_time
= cpu_get_time_fast();
428 /***********************************************************/
429 void hw_error(const char *fmt
, ...)
435 fprintf(stderr
, "qemu: hardware error: ");
436 vfprintf(stderr
, fmt
, ap
);
437 fprintf(stderr
, "\n");
438 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
439 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
441 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
443 cpu_dump_state(env
, stderr
, fprintf
, 0);
450 /***********************************************************/
453 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
454 static void *qemu_put_kbd_event_opaque
;
455 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
456 static void *qemu_put_mouse_event_opaque
;
457 static int qemu_put_mouse_event_absolute
;
459 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
461 qemu_put_kbd_event_opaque
= opaque
;
462 qemu_put_kbd_event
= func
;
465 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
, int absolute
)
467 qemu_put_mouse_event_opaque
= opaque
;
468 qemu_put_mouse_event
= func
;
469 qemu_put_mouse_event_absolute
= absolute
;
472 void kbd_put_keycode(int keycode
)
474 if (qemu_put_kbd_event
) {
475 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
479 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
481 if (qemu_put_mouse_event
) {
482 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
483 dx
, dy
, dz
, buttons_state
);
487 int kbd_mouse_is_absolute(void)
489 return qemu_put_mouse_event_absolute
;
492 /* compute with 96 bit intermediate result: (a*b)/c */
493 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
498 #ifdef WORDS_BIGENDIAN
508 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
509 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
512 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
516 /***********************************************************/
517 /* real time host monotonic timer */
519 #define QEMU_TIMER_BASE 1000000000LL
523 static int64_t clock_freq
;
525 static void init_get_clock(void)
529 ret
= QueryPerformanceFrequency(&freq
);
531 fprintf(stderr
, "Could not calibrate ticks\n");
534 clock_freq
= freq
.QuadPart
;
537 static int64_t get_clock(void)
540 QueryPerformanceCounter(&ti
);
541 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
546 static int use_rt_clock
;
548 static void init_get_clock(void)
551 #if defined(__linux__)
554 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
561 static int64_t get_clock(void)
563 #if defined(__linux__)
566 clock_gettime(CLOCK_MONOTONIC
, &ts
);
567 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
571 /* XXX: using gettimeofday leads to problems if the date
572 changes, so it should be avoided. */
574 gettimeofday(&tv
, NULL
);
575 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
581 /***********************************************************/
582 /* guest cycle counter */
584 static int64_t cpu_ticks_prev
;
585 static int64_t cpu_ticks_offset
;
586 static int64_t cpu_clock_offset
;
587 static int cpu_ticks_enabled
;
589 /* return the host CPU cycle counter and handle stop/restart */
590 int64_t cpu_get_ticks(void)
592 if (!cpu_ticks_enabled
) {
593 return cpu_ticks_offset
;
596 ticks
= cpu_get_real_ticks();
597 if (cpu_ticks_prev
> ticks
) {
598 /* Note: non increasing ticks may happen if the host uses
600 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
602 cpu_ticks_prev
= ticks
;
603 return ticks
+ cpu_ticks_offset
;
607 /* return the host CPU monotonic timer and handle stop/restart */
608 static int64_t cpu_get_clock(void)
611 if (!cpu_ticks_enabled
) {
612 return cpu_clock_offset
;
615 return ti
+ cpu_clock_offset
;
619 /* enable cpu_get_ticks() */
620 void cpu_enable_ticks(void)
622 if (!cpu_ticks_enabled
) {
623 cpu_ticks_offset
-= cpu_get_real_ticks();
624 cpu_clock_offset
-= get_clock();
625 cpu_ticks_enabled
= 1;
629 /* disable cpu_get_ticks() : the clock is stopped. You must not call
630 cpu_get_ticks() after that. */
631 void cpu_disable_ticks(void)
633 if (cpu_ticks_enabled
) {
634 cpu_ticks_offset
= cpu_get_ticks();
635 cpu_clock_offset
= cpu_get_clock();
636 cpu_ticks_enabled
= 0;
640 /***********************************************************/
643 #define QEMU_TIMER_REALTIME 0
644 #define QEMU_TIMER_VIRTUAL 1
648 /* XXX: add frequency */
656 struct QEMUTimer
*next
;
662 static QEMUTimer
*active_timers
[2];
664 static MMRESULT timerID
;
665 static HANDLE host_alarm
= NULL
;
666 static unsigned int period
= 1;
668 /* frequency of the times() clock tick */
669 static int timer_freq
;
672 QEMUClock
*qemu_new_clock(int type
)
675 clock
= qemu_mallocz(sizeof(QEMUClock
));
682 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
686 ts
= qemu_mallocz(sizeof(QEMUTimer
));
693 void qemu_free_timer(QEMUTimer
*ts
)
698 /* stop a timer, but do not dealloc it */
699 void qemu_del_timer(QEMUTimer
*ts
)
703 /* NOTE: this code must be signal safe because
704 qemu_timer_expired() can be called from a signal. */
705 pt
= &active_timers
[ts
->clock
->type
];
718 /* modify the current timer so that it will be fired when current_time
719 >= expire_time. The corresponding callback will be called. */
720 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
726 /* add the timer in the sorted list */
727 /* NOTE: this code must be signal safe because
728 qemu_timer_expired() can be called from a signal. */
729 pt
= &active_timers
[ts
->clock
->type
];
734 if (t
->expire_time
> expire_time
)
738 ts
->expire_time
= expire_time
;
743 int qemu_timer_pending(QEMUTimer
*ts
)
746 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
753 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
757 return (timer_head
->expire_time
<= current_time
);
760 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
766 if (!ts
|| ts
->expire_time
> current_time
)
768 /* remove timer from the list before calling the callback */
769 *ptimer_head
= ts
->next
;
772 /* run the callback (the timer list can be modified) */
777 int64_t qemu_get_clock(QEMUClock
*clock
)
779 switch(clock
->type
) {
780 case QEMU_TIMER_REALTIME
:
781 return get_clock() / 1000000;
783 case QEMU_TIMER_VIRTUAL
:
784 return cpu_get_clock();
788 static void init_timers(void)
791 ticks_per_sec
= QEMU_TIMER_BASE
;
792 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
793 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
797 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
799 uint64_t expire_time
;
801 if (qemu_timer_pending(ts
)) {
802 expire_time
= ts
->expire_time
;
806 qemu_put_be64(f
, expire_time
);
809 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
811 uint64_t expire_time
;
813 expire_time
= qemu_get_be64(f
);
814 if (expire_time
!= -1) {
815 qemu_mod_timer(ts
, expire_time
);
821 static void timer_save(QEMUFile
*f
, void *opaque
)
823 if (cpu_ticks_enabled
) {
824 hw_error("cannot save state if virtual timers are running");
826 qemu_put_be64s(f
, &cpu_ticks_offset
);
827 qemu_put_be64s(f
, &ticks_per_sec
);
828 qemu_put_be64s(f
, &cpu_clock_offset
);
831 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
833 if (version_id
!= 1 && version_id
!= 2)
835 if (cpu_ticks_enabled
) {
838 qemu_get_be64s(f
, &cpu_ticks_offset
);
839 qemu_get_be64s(f
, &ticks_per_sec
);
840 if (version_id
== 2) {
841 qemu_get_be64s(f
, &cpu_clock_offset
);
847 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
848 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
850 static void host_alarm_handler(int host_signum
)
854 #define DISP_FREQ 1000
856 static int64_t delta_min
= INT64_MAX
;
857 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
859 ti
= qemu_get_clock(vm_clock
);
860 if (last_clock
!= 0) {
861 delta
= ti
- last_clock
;
862 if (delta
< delta_min
)
864 if (delta
> delta_max
)
867 if (++count
== DISP_FREQ
) {
868 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
869 muldiv64(delta_min
, 1000000, ticks_per_sec
),
870 muldiv64(delta_max
, 1000000, ticks_per_sec
),
871 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
872 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
874 delta_min
= INT64_MAX
;
882 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
883 qemu_get_clock(vm_clock
)) ||
884 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
885 qemu_get_clock(rt_clock
))) {
887 SetEvent(host_alarm
);
889 CPUState
*env
= cpu_single_env
;
891 /* stop the currently executing cpu because a timer occured */
892 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
894 if (env
->kqemu_enabled
) {
895 kqemu_cpu_interrupt(env
);
904 #if defined(__linux__)
906 #define RTC_FREQ 1024
910 static int start_rtc_timer(void)
912 rtc_fd
= open("/dev/rtc", O_RDONLY
);
915 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
916 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
917 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
918 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
921 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
926 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
932 static int start_rtc_timer(void)
937 #endif /* !defined(__linux__) */
939 #endif /* !defined(_WIN32) */
941 static void init_timer_alarm(void)
948 ZeroMemory(&tc
, sizeof(TIMECAPS
));
949 timeGetDevCaps(&tc
, sizeof(TIMECAPS
));
950 if (period
< tc
.wPeriodMin
)
951 period
= tc
.wPeriodMin
;
952 timeBeginPeriod(period
);
953 timerID
= timeSetEvent(1, // interval (ms)
954 period
, // resolution
955 host_alarm_handler
, // function
956 (DWORD
)&count
, // user parameter
957 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
959 perror("failed timer alarm");
962 host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
964 perror("failed CreateEvent");
967 qemu_add_wait_object(host_alarm
, NULL
, NULL
);
969 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
972 struct sigaction act
;
973 struct itimerval itv
;
975 /* get times() syscall frequency */
976 timer_freq
= sysconf(_SC_CLK_TCK
);
979 sigfillset(&act
.sa_mask
);
981 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
982 act
.sa_flags
|= SA_ONSTACK
;
984 act
.sa_handler
= host_alarm_handler
;
985 sigaction(SIGALRM
, &act
, NULL
);
987 itv
.it_interval
.tv_sec
= 0;
988 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
989 itv
.it_value
.tv_sec
= 0;
990 itv
.it_value
.tv_usec
= 10 * 1000;
991 setitimer(ITIMER_REAL
, &itv
, NULL
);
992 /* we probe the tick duration of the kernel to inform the user if
993 the emulated kernel requested a too high timer frequency */
994 getitimer(ITIMER_REAL
, &itv
);
996 #if defined(__linux__)
997 /* XXX: force /dev/rtc usage because even 2.6 kernels may not
998 have timers with 1 ms resolution. The correct solution will
999 be to use the POSIX real time timers available in recent
1001 if (itv
.it_interval
.tv_usec
> 1000 || 1) {
1002 /* try to use /dev/rtc to have a faster timer */
1003 if (start_rtc_timer() < 0)
1005 /* disable itimer */
1006 itv
.it_interval
.tv_sec
= 0;
1007 itv
.it_interval
.tv_usec
= 0;
1008 itv
.it_value
.tv_sec
= 0;
1009 itv
.it_value
.tv_usec
= 0;
1010 setitimer(ITIMER_REAL
, &itv
, NULL
);
1013 sigaction(SIGIO
, &act
, NULL
);
1014 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1015 fcntl(rtc_fd
, F_SETOWN
, getpid());
1017 #endif /* defined(__linux__) */
1020 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1021 PIT_FREQ
) / 1000000;
1027 void quit_timers(void)
1030 timeKillEvent(timerID
);
1031 timeEndPeriod(period
);
1033 CloseHandle(host_alarm
);
1039 /***********************************************************/
1040 /* character device */
1042 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1044 return s
->chr_write(s
, buf
, len
);
1047 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1051 return s
->chr_ioctl(s
, cmd
, arg
);
1054 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1059 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1060 qemu_chr_write(s
, buf
, strlen(buf
));
1064 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1066 if (s
->chr_send_event
)
1067 s
->chr_send_event(s
, event
);
1070 void qemu_chr_add_read_handler(CharDriverState
*s
,
1071 IOCanRWHandler
*fd_can_read
,
1072 IOReadHandler
*fd_read
, void *opaque
)
1074 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
1077 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
1079 s
->chr_event
= chr_event
;
1082 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1087 static void null_chr_add_read_handler(CharDriverState
*chr
,
1088 IOCanRWHandler
*fd_can_read
,
1089 IOReadHandler
*fd_read
, void *opaque
)
1093 CharDriverState
*qemu_chr_open_null(void)
1095 CharDriverState
*chr
;
1097 chr
= qemu_mallocz(sizeof(CharDriverState
));
1100 chr
->chr_write
= null_chr_write
;
1101 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1107 static void socket_cleanup(void)
1112 static int socket_init(void)
1117 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1119 err
= WSAGetLastError();
1120 fprintf(stderr
, "WSAStartup: %d\n", err
);
1123 atexit(socket_cleanup
);
1127 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1133 ret
= send(fd
, buf
, len
, 0);
1136 errno
= WSAGetLastError();
1137 if (errno
!= WSAEWOULDBLOCK
) {
1140 } else if (ret
== 0) {
1150 void socket_set_nonblock(int fd
)
1152 unsigned long opt
= 1;
1153 ioctlsocket(fd
, FIONBIO
, &opt
);
1158 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1164 ret
= write(fd
, buf
, len
);
1166 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1168 } else if (ret
== 0) {
1178 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1180 return unix_write(fd
, buf
, len1
);
1183 void socket_set_nonblock(int fd
)
1185 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1187 #endif /* !_WIN32 */
1193 IOCanRWHandler
*fd_can_read
;
1194 IOReadHandler
*fd_read
;
1199 #define STDIO_MAX_CLIENTS 2
1201 static int stdio_nb_clients
;
1202 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1204 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1206 FDCharDriver
*s
= chr
->opaque
;
1207 return unix_write(s
->fd_out
, buf
, len
);
1210 static int fd_chr_read_poll(void *opaque
)
1212 CharDriverState
*chr
= opaque
;
1213 FDCharDriver
*s
= chr
->opaque
;
1215 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
1219 static void fd_chr_read(void *opaque
)
1221 CharDriverState
*chr
= opaque
;
1222 FDCharDriver
*s
= chr
->opaque
;
1227 if (len
> s
->max_size
)
1231 size
= read(s
->fd_in
, buf
, len
);
1233 /* FD has been closed. Remove it from the active list. */
1234 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1238 s
->fd_read(s
->fd_opaque
, buf
, size
);
1242 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1243 IOCanRWHandler
*fd_can_read
,
1244 IOReadHandler
*fd_read
, void *opaque
)
1246 FDCharDriver
*s
= chr
->opaque
;
1248 if (s
->fd_in
>= 0) {
1249 s
->fd_can_read
= fd_can_read
;
1250 s
->fd_read
= fd_read
;
1251 s
->fd_opaque
= opaque
;
1252 if (nographic
&& s
->fd_in
== 0) {
1254 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1255 fd_chr_read
, NULL
, chr
);
1260 /* open a character device to a unix fd */
1261 CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1263 CharDriverState
*chr
;
1266 chr
= qemu_mallocz(sizeof(CharDriverState
));
1269 s
= qemu_mallocz(sizeof(FDCharDriver
));
1277 chr
->chr_write
= fd_chr_write
;
1278 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1282 CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1286 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1289 return qemu_chr_open_fd(-1, fd_out
);
1292 CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1296 fd
= open(filename
, O_RDWR
| O_BINARY
);
1299 return qemu_chr_open_fd(fd
, fd
);
1303 /* for STDIO, we handle the case where several clients use it
1306 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1308 #define TERM_FIFO_MAX_SIZE 1
1310 static int term_got_escape
, client_index
;
1311 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1312 static int term_fifo_size
;
1313 static int term_timestamps
;
1314 static int64_t term_timestamps_start
;
1316 void term_print_help(void)
1319 "C-a h print this help\n"
1320 "C-a x exit emulator\n"
1321 "C-a s save disk data back to file (if -snapshot)\n"
1322 "C-a b send break (magic sysrq)\n"
1323 "C-a t toggle console timestamps\n"
1324 "C-a c switch between console and monitor\n"
1325 "C-a C-a send C-a\n"
1329 /* called when a char is received */
1330 static void stdio_received_byte(int ch
)
1332 if (term_got_escape
) {
1333 term_got_escape
= 0;
1344 for (i
= 0; i
< MAX_DISKS
; i
++) {
1346 bdrv_commit(bs_table
[i
]);
1351 if (client_index
< stdio_nb_clients
) {
1352 CharDriverState
*chr
;
1355 chr
= stdio_clients
[client_index
];
1357 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1362 if (client_index
>= stdio_nb_clients
)
1364 if (client_index
== 0) {
1365 /* send a new line in the monitor to get the prompt */
1371 term_timestamps
= !term_timestamps
;
1372 term_timestamps_start
= -1;
1377 } else if (ch
== TERM_ESCAPE
) {
1378 term_got_escape
= 1;
1381 if (client_index
< stdio_nb_clients
) {
1383 CharDriverState
*chr
;
1386 chr
= stdio_clients
[client_index
];
1388 if (s
->fd_can_read(s
->fd_opaque
) > 0) {
1390 s
->fd_read(s
->fd_opaque
, buf
, 1);
1391 } else if (term_fifo_size
== 0) {
1392 term_fifo
[term_fifo_size
++] = ch
;
1398 static int stdio_read_poll(void *opaque
)
1400 CharDriverState
*chr
;
1403 if (client_index
< stdio_nb_clients
) {
1404 chr
= stdio_clients
[client_index
];
1406 /* try to flush the queue if needed */
1407 if (term_fifo_size
!= 0 && s
->fd_can_read(s
->fd_opaque
) > 0) {
1408 s
->fd_read(s
->fd_opaque
, term_fifo
, 1);
1411 /* see if we can absorb more chars */
1412 if (term_fifo_size
== 0)
1421 static void stdio_read(void *opaque
)
1426 size
= read(0, buf
, 1);
1428 /* stdin has been closed. Remove it from the active list. */
1429 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
1433 stdio_received_byte(buf
[0]);
1436 static int stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1438 FDCharDriver
*s
= chr
->opaque
;
1439 if (!term_timestamps
) {
1440 return unix_write(s
->fd_out
, buf
, len
);
1445 for(i
= 0; i
< len
; i
++) {
1446 unix_write(s
->fd_out
, buf
+ i
, 1);
1447 if (buf
[i
] == '\n') {
1452 if (term_timestamps_start
== -1)
1453 term_timestamps_start
= ti
;
1454 ti
-= term_timestamps_start
;
1455 secs
= ti
/ 1000000000;
1456 snprintf(buf1
, sizeof(buf1
),
1457 "[%02d:%02d:%02d.%03d] ",
1461 (int)((ti
/ 1000000) % 1000));
1462 unix_write(s
->fd_out
, buf1
, strlen(buf1
));
1469 /* init terminal so that we can grab keys */
1470 static struct termios oldtty
;
1471 static int old_fd0_flags
;
1473 static void term_exit(void)
1475 tcsetattr (0, TCSANOW
, &oldtty
);
1476 fcntl(0, F_SETFL
, old_fd0_flags
);
1479 static void term_init(void)
1483 tcgetattr (0, &tty
);
1485 old_fd0_flags
= fcntl(0, F_GETFL
);
1487 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1488 |INLCR
|IGNCR
|ICRNL
|IXON
);
1489 tty
.c_oflag
|= OPOST
;
1490 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1491 /* if graphical mode, we allow Ctrl-C handling */
1493 tty
.c_lflag
&= ~ISIG
;
1494 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1497 tty
.c_cc
[VTIME
] = 0;
1499 tcsetattr (0, TCSANOW
, &tty
);
1503 fcntl(0, F_SETFL
, O_NONBLOCK
);
1506 CharDriverState
*qemu_chr_open_stdio(void)
1508 CharDriverState
*chr
;
1511 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1513 chr
= qemu_chr_open_fd(0, 1);
1514 chr
->chr_write
= stdio_write
;
1515 if (stdio_nb_clients
== 0)
1516 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, NULL
);
1517 client_index
= stdio_nb_clients
;
1519 if (stdio_nb_clients
!= 0)
1521 chr
= qemu_chr_open_fd(0, 1);
1523 stdio_clients
[stdio_nb_clients
++] = chr
;
1524 if (stdio_nb_clients
== 1) {
1525 /* set the terminal in raw mode */
1531 #if defined(__linux__)
1532 CharDriverState
*qemu_chr_open_pty(void)
1535 char slave_name
[1024];
1536 int master_fd
, slave_fd
;
1538 /* Not satisfying */
1539 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1543 /* Disabling local echo and line-buffered output */
1544 tcgetattr (master_fd
, &tty
);
1545 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1547 tty
.c_cc
[VTIME
] = 0;
1548 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1550 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1551 return qemu_chr_open_fd(master_fd
, master_fd
);
1554 static void tty_serial_init(int fd
, int speed
,
1555 int parity
, int data_bits
, int stop_bits
)
1561 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1562 speed
, parity
, data_bits
, stop_bits
);
1564 tcgetattr (fd
, &tty
);
1606 cfsetispeed(&tty
, spd
);
1607 cfsetospeed(&tty
, spd
);
1609 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1610 |INLCR
|IGNCR
|ICRNL
|IXON
);
1611 tty
.c_oflag
|= OPOST
;
1612 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1613 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1634 tty
.c_cflag
|= PARENB
;
1637 tty
.c_cflag
|= PARENB
| PARODD
;
1641 tty
.c_cflag
|= CSTOPB
;
1643 tcsetattr (fd
, TCSANOW
, &tty
);
1646 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1648 FDCharDriver
*s
= chr
->opaque
;
1651 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1653 QEMUSerialSetParams
*ssp
= arg
;
1654 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1655 ssp
->data_bits
, ssp
->stop_bits
);
1658 case CHR_IOCTL_SERIAL_SET_BREAK
:
1660 int enable
= *(int *)arg
;
1662 tcsendbreak(s
->fd_in
, 1);
1671 CharDriverState
*qemu_chr_open_tty(const char *filename
)
1673 CharDriverState
*chr
;
1676 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1679 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1680 tty_serial_init(fd
, 115200, 'N', 8, 1);
1681 chr
= qemu_chr_open_fd(fd
, fd
);
1684 chr
->chr_ioctl
= tty_serial_ioctl
;
1688 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1690 int fd
= (int)chr
->opaque
;
1694 case CHR_IOCTL_PP_READ_DATA
:
1695 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1697 *(uint8_t *)arg
= b
;
1699 case CHR_IOCTL_PP_WRITE_DATA
:
1700 b
= *(uint8_t *)arg
;
1701 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1704 case CHR_IOCTL_PP_READ_CONTROL
:
1705 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1707 *(uint8_t *)arg
= b
;
1709 case CHR_IOCTL_PP_WRITE_CONTROL
:
1710 b
= *(uint8_t *)arg
;
1711 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1714 case CHR_IOCTL_PP_READ_STATUS
:
1715 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1717 *(uint8_t *)arg
= b
;
1725 CharDriverState
*qemu_chr_open_pp(const char *filename
)
1727 CharDriverState
*chr
;
1730 fd
= open(filename
, O_RDWR
);
1734 if (ioctl(fd
, PPCLAIM
) < 0) {
1739 chr
= qemu_mallocz(sizeof(CharDriverState
));
1744 chr
->opaque
= (void *)fd
;
1745 chr
->chr_write
= null_chr_write
;
1746 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1747 chr
->chr_ioctl
= pp_ioctl
;
1752 CharDriverState
*qemu_chr_open_pty(void)
1758 #endif /* !defined(_WIN32) */
1762 IOCanRWHandler
*fd_can_read
;
1763 IOReadHandler
*fd_read
;
1766 HANDLE hcom
, hrecv
, hsend
;
1767 OVERLAPPED orecv
, osend
;
1772 #define NSENDBUF 2048
1773 #define NRECVBUF 2048
1774 #define MAXCONNECT 1
1775 #define NTIMEOUT 5000
1777 static int win_chr_poll(void *opaque
);
1778 static int win_chr_pipe_poll(void *opaque
);
1780 static void win_chr_close2(WinCharState
*s
)
1783 CloseHandle(s
->hsend
);
1787 CloseHandle(s
->hrecv
);
1791 CloseHandle(s
->hcom
);
1795 qemu_del_polling_cb(win_chr_pipe_poll
, s
);
1797 qemu_del_polling_cb(win_chr_poll
, s
);
1800 static void win_chr_close(CharDriverState
*chr
)
1802 WinCharState
*s
= chr
->opaque
;
1806 static int win_chr_init(WinCharState
*s
, const char *filename
)
1809 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1814 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1816 fprintf(stderr
, "Failed CreateEvent\n");
1819 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1821 fprintf(stderr
, "Failed CreateEvent\n");
1825 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1826 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1827 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1828 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1833 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1834 fprintf(stderr
, "Failed SetupComm\n");
1838 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1839 size
= sizeof(COMMCONFIG
);
1840 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1841 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1842 CommConfigDialog(filename
, NULL
, &comcfg
);
1844 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1845 fprintf(stderr
, "Failed SetCommState\n");
1849 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1850 fprintf(stderr
, "Failed SetCommMask\n");
1854 cto
.ReadIntervalTimeout
= MAXDWORD
;
1855 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1856 fprintf(stderr
, "Failed SetCommTimeouts\n");
1860 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1861 fprintf(stderr
, "Failed ClearCommError\n");
1864 qemu_add_polling_cb(win_chr_poll
, s
);
1872 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1874 WinCharState
*s
= chr
->opaque
;
1875 DWORD len
, ret
, size
, err
;
1878 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1879 s
->osend
.hEvent
= s
->hsend
;
1882 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1884 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1886 err
= GetLastError();
1887 if (err
== ERROR_IO_PENDING
) {
1888 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1906 static int win_chr_read_poll(WinCharState
*s
)
1908 s
->max_size
= s
->fd_can_read(s
->win_opaque
);
1912 static void win_chr_readfile(WinCharState
*s
)
1918 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1919 s
->orecv
.hEvent
= s
->hrecv
;
1920 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1922 err
= GetLastError();
1923 if (err
== ERROR_IO_PENDING
) {
1924 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1929 s
->fd_read(s
->win_opaque
, buf
, size
);
1933 static void win_chr_read(WinCharState
*s
)
1935 if (s
->len
> s
->max_size
)
1936 s
->len
= s
->max_size
;
1940 win_chr_readfile(s
);
1943 static int win_chr_poll(void *opaque
)
1945 WinCharState
*s
= opaque
;
1949 ClearCommError(s
->hcom
, &comerr
, &status
);
1950 if (status
.cbInQue
> 0) {
1951 s
->len
= status
.cbInQue
;
1952 win_chr_read_poll(s
);
1959 static void win_chr_add_read_handler(CharDriverState
*chr
,
1960 IOCanRWHandler
*fd_can_read
,
1961 IOReadHandler
*fd_read
, void *opaque
)
1963 WinCharState
*s
= chr
->opaque
;
1965 s
->fd_can_read
= fd_can_read
;
1966 s
->fd_read
= fd_read
;
1967 s
->win_opaque
= opaque
;
1970 CharDriverState
*qemu_chr_open_win(const char *filename
)
1972 CharDriverState
*chr
;
1975 chr
= qemu_mallocz(sizeof(CharDriverState
));
1978 s
= qemu_mallocz(sizeof(WinCharState
));
1984 chr
->chr_write
= win_chr_write
;
1985 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
1986 chr
->chr_close
= win_chr_close
;
1988 if (win_chr_init(s
, filename
) < 0) {
1996 static int win_chr_pipe_poll(void *opaque
)
1998 WinCharState
*s
= opaque
;
2001 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2004 win_chr_read_poll(s
);
2011 static int win_chr_pipe_init(WinCharState
*s
, const char *filename
)
2020 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2022 fprintf(stderr
, "Failed CreateEvent\n");
2025 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2027 fprintf(stderr
, "Failed CreateEvent\n");
2031 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2032 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2033 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2035 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2036 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2037 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2042 ZeroMemory(&ov
, sizeof(ov
));
2043 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2044 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2046 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2050 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2052 fprintf(stderr
, "Failed GetOverlappedResult\n");
2054 CloseHandle(ov
.hEvent
);
2061 CloseHandle(ov
.hEvent
);
2064 qemu_add_polling_cb(win_chr_pipe_poll
, s
);
2073 CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2075 CharDriverState
*chr
;
2078 chr
= qemu_mallocz(sizeof(CharDriverState
));
2081 s
= qemu_mallocz(sizeof(WinCharState
));
2087 chr
->chr_write
= win_chr_write
;
2088 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2089 chr
->chr_close
= win_chr_close
;
2091 if (win_chr_pipe_init(s
, filename
) < 0) {
2099 CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2101 CharDriverState
*chr
;
2104 chr
= qemu_mallocz(sizeof(CharDriverState
));
2107 s
= qemu_mallocz(sizeof(WinCharState
));
2114 chr
->chr_write
= win_chr_write
;
2115 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2119 CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2123 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2124 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2125 if (fd_out
== INVALID_HANDLE_VALUE
)
2128 return qemu_chr_open_win_file(fd_out
);
2132 /***********************************************************/
2133 /* UDP Net console */
2136 IOCanRWHandler
*fd_can_read
;
2137 IOReadHandler
*fd_read
;
2140 struct sockaddr_in daddr
;
2147 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2149 NetCharDriver
*s
= chr
->opaque
;
2151 return sendto(s
->fd
, buf
, len
, 0,
2152 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2155 static int udp_chr_read_poll(void *opaque
)
2157 CharDriverState
*chr
= opaque
;
2158 NetCharDriver
*s
= chr
->opaque
;
2160 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2162 /* If there were any stray characters in the queue process them
2165 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2166 s
->fd_read(s
->fd_opaque
, &s
->buf
[s
->bufptr
], 1);
2168 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2173 static void udp_chr_read(void *opaque
)
2175 CharDriverState
*chr
= opaque
;
2176 NetCharDriver
*s
= chr
->opaque
;
2178 if (s
->max_size
== 0)
2180 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2181 s
->bufptr
= s
->bufcnt
;
2186 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2187 s
->fd_read(s
->fd_opaque
, &s
->buf
[s
->bufptr
], 1);
2189 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2193 static void udp_chr_add_read_handler(CharDriverState
*chr
,
2194 IOCanRWHandler
*fd_can_read
,
2195 IOReadHandler
*fd_read
, void *opaque
)
2197 NetCharDriver
*s
= chr
->opaque
;
2200 s
->fd_can_read
= fd_can_read
;
2201 s
->fd_read
= fd_read
;
2202 s
->fd_opaque
= opaque
;
2203 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2204 udp_chr_read
, NULL
, chr
);
2208 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2209 int parse_host_src_port(struct sockaddr_in
*haddr
,
2210 struct sockaddr_in
*saddr
,
2213 CharDriverState
*qemu_chr_open_udp(const char *def
)
2215 CharDriverState
*chr
= NULL
;
2216 NetCharDriver
*s
= NULL
;
2218 struct sockaddr_in saddr
;
2220 chr
= qemu_mallocz(sizeof(CharDriverState
));
2223 s
= qemu_mallocz(sizeof(NetCharDriver
));
2227 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2229 perror("socket(PF_INET, SOCK_DGRAM)");
2233 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2234 printf("Could not parse: %s\n", def
);
2238 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2248 chr
->chr_write
= udp_chr_write
;
2249 chr
->chr_add_read_handler
= udp_chr_add_read_handler
;
2262 /***********************************************************/
2263 /* TCP Net console */
2266 IOCanRWHandler
*fd_can_read
;
2267 IOReadHandler
*fd_read
;
2275 static void tcp_chr_accept(void *opaque
);
2277 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2279 TCPCharDriver
*s
= chr
->opaque
;
2281 return send_all(s
->fd
, buf
, len
);
2283 /* XXX: indicate an error ? */
2288 static int tcp_chr_read_poll(void *opaque
)
2290 CharDriverState
*chr
= opaque
;
2291 TCPCharDriver
*s
= chr
->opaque
;
2294 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2299 #define IAC_BREAK 243
2300 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2302 char *buf
, int *size
)
2304 /* Handle any telnet client's basic IAC options to satisfy char by
2305 * char mode with no echo. All IAC options will be removed from
2306 * the buf and the do_telnetopt variable will be used to track the
2307 * state of the width of the IAC information.
2309 * IAC commands come in sets of 3 bytes with the exception of the
2310 * "IAC BREAK" command and the double IAC.
2316 for (i
= 0; i
< *size
; i
++) {
2317 if (s
->do_telnetopt
> 1) {
2318 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2319 /* Double IAC means send an IAC */
2323 s
->do_telnetopt
= 1;
2325 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2326 /* Handle IAC break commands by sending a serial break */
2327 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
2332 if (s
->do_telnetopt
>= 4) {
2333 s
->do_telnetopt
= 1;
2336 if ((unsigned char)buf
[i
] == IAC
) {
2337 s
->do_telnetopt
= 2;
2348 static void tcp_chr_read(void *opaque
)
2350 CharDriverState
*chr
= opaque
;
2351 TCPCharDriver
*s
= chr
->opaque
;
2355 if (!s
->connected
|| s
->max_size
<= 0)
2358 if (len
> s
->max_size
)
2360 size
= recv(s
->fd
, buf
, len
, 0);
2362 /* connection closed */
2364 if (s
->listen_fd
>= 0) {
2365 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2367 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2370 } else if (size
> 0) {
2371 if (s
->do_telnetopt
)
2372 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2374 s
->fd_read(s
->fd_opaque
, buf
, size
);
2378 static void tcp_chr_add_read_handler(CharDriverState
*chr
,
2379 IOCanRWHandler
*fd_can_read
,
2380 IOReadHandler
*fd_read
, void *opaque
)
2382 TCPCharDriver
*s
= chr
->opaque
;
2384 s
->fd_can_read
= fd_can_read
;
2385 s
->fd_read
= fd_read
;
2386 s
->fd_opaque
= opaque
;
2389 static void tcp_chr_connect(void *opaque
)
2391 CharDriverState
*chr
= opaque
;
2392 TCPCharDriver
*s
= chr
->opaque
;
2395 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2396 tcp_chr_read
, NULL
, chr
);
2399 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2400 static void tcp_chr_telnet_init(int fd
)
2403 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2404 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2405 send(fd
, (char *)buf
, 3, 0);
2406 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2407 send(fd
, (char *)buf
, 3, 0);
2408 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2409 send(fd
, (char *)buf
, 3, 0);
2410 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2411 send(fd
, (char *)buf
, 3, 0);
2414 static void tcp_chr_accept(void *opaque
)
2416 CharDriverState
*chr
= opaque
;
2417 TCPCharDriver
*s
= chr
->opaque
;
2418 struct sockaddr_in saddr
;
2423 len
= sizeof(saddr
);
2424 fd
= accept(s
->listen_fd
, (struct sockaddr
*)&saddr
, &len
);
2425 if (fd
< 0 && errno
!= EINTR
) {
2427 } else if (fd
>= 0) {
2428 if (s
->do_telnetopt
)
2429 tcp_chr_telnet_init(fd
);
2433 socket_set_nonblock(fd
);
2435 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2436 tcp_chr_connect(chr
);
2439 static void tcp_chr_close(CharDriverState
*chr
)
2441 TCPCharDriver
*s
= chr
->opaque
;
2444 if (s
->listen_fd
>= 0)
2445 closesocket(s
->listen_fd
);
2449 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2452 CharDriverState
*chr
= NULL
;
2453 TCPCharDriver
*s
= NULL
;
2454 int fd
= -1, ret
, err
, val
;
2456 int is_waitconnect
= 1;
2458 struct sockaddr_in saddr
;
2460 if (parse_host_port(&saddr
, host_str
) < 0)
2464 while((ptr
= strchr(ptr
,','))) {
2466 if (!strncmp(ptr
,"server",6)) {
2468 } else if (!strncmp(ptr
,"nowait",6)) {
2471 printf("Unknown option: %s\n", ptr
);
2478 chr
= qemu_mallocz(sizeof(CharDriverState
));
2481 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2485 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2489 if (!is_waitconnect
)
2490 socket_set_nonblock(fd
);
2496 /* allow fast reuse */
2498 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2500 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2503 ret
= listen(fd
, 0);
2507 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2509 s
->do_telnetopt
= 1;
2512 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2514 err
= socket_error();
2515 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2516 } else if (err
== EINPROGRESS
) {
2528 tcp_chr_connect(chr
);
2530 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2534 chr
->chr_write
= tcp_chr_write
;
2535 chr
->chr_add_read_handler
= tcp_chr_add_read_handler
;
2536 chr
->chr_close
= tcp_chr_close
;
2537 if (is_listen
&& is_waitconnect
) {
2538 printf("QEMU waiting for connection on: %s\n", host_str
);
2539 tcp_chr_accept(chr
);
2540 socket_set_nonblock(s
->listen_fd
);
2552 CharDriverState
*qemu_chr_open(const char *filename
)
2556 if (!strcmp(filename
, "vc")) {
2557 return text_console_init(&display_state
);
2558 } else if (!strcmp(filename
, "null")) {
2559 return qemu_chr_open_null();
2561 if (strstart(filename
, "tcp:", &p
)) {
2562 return qemu_chr_open_tcp(p
, 0);
2564 if (strstart(filename
, "telnet:", &p
)) {
2565 return qemu_chr_open_tcp(p
, 1);
2567 if (strstart(filename
, "udp:", &p
)) {
2568 return qemu_chr_open_udp(p
);
2571 if (strstart(filename
, "file:", &p
)) {
2572 return qemu_chr_open_file_out(p
);
2573 } else if (strstart(filename
, "pipe:", &p
)) {
2574 return qemu_chr_open_pipe(p
);
2575 } else if (!strcmp(filename
, "pty")) {
2576 return qemu_chr_open_pty();
2577 } else if (!strcmp(filename
, "stdio")) {
2578 return qemu_chr_open_stdio();
2581 #if defined(__linux__)
2582 if (strstart(filename
, "/dev/parport", NULL
)) {
2583 return qemu_chr_open_pp(filename
);
2585 if (strstart(filename
, "/dev/", NULL
)) {
2586 return qemu_chr_open_tty(filename
);
2590 if (strstart(filename
, "COM", NULL
)) {
2591 return qemu_chr_open_win(filename
);
2593 if (strstart(filename
, "pipe:", &p
)) {
2594 return qemu_chr_open_win_pipe(p
);
2596 if (strstart(filename
, "file:", &p
)) {
2597 return qemu_chr_open_win_file_out(p
);
2605 void qemu_chr_close(CharDriverState
*chr
)
2608 chr
->chr_close(chr
);
2611 /***********************************************************/
2612 /* network device redirectors */
2614 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2618 for(i
=0;i
<size
;i
+=16) {
2622 fprintf(f
, "%08x ", i
);
2625 fprintf(f
, " %02x", buf
[i
+j
]);
2630 for(j
=0;j
<len
;j
++) {
2632 if (c
< ' ' || c
> '~')
2634 fprintf(f
, "%c", c
);
2640 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
2643 for(i
= 0; i
< 6; i
++) {
2644 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2657 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
2662 p1
= strchr(p
, sep
);
2668 if (len
> buf_size
- 1)
2670 memcpy(buf
, p
, len
);
2677 int parse_host_src_port(struct sockaddr_in
*haddr
,
2678 struct sockaddr_in
*saddr
,
2679 const char *input_str
)
2681 char *str
= strdup(input_str
);
2682 char *host_str
= str
;
2687 * Chop off any extra arguments at the end of the string which
2688 * would start with a comma, then fill in the src port information
2689 * if it was provided else use the "any address" and "any port".
2691 if ((ptr
= strchr(str
,',')))
2694 if ((src_str
= strchr(input_str
,'@'))) {
2699 if (parse_host_port(haddr
, host_str
) < 0)
2702 if (!src_str
|| *src_str
== '\0')
2705 if (parse_host_port(saddr
, src_str
) < 0)
2716 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
2724 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2726 saddr
->sin_family
= AF_INET
;
2727 if (buf
[0] == '\0') {
2728 saddr
->sin_addr
.s_addr
= 0;
2730 if (isdigit(buf
[0])) {
2731 if (!inet_aton(buf
, &saddr
->sin_addr
))
2734 if ((he
= gethostbyname(buf
)) == NULL
)
2736 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
2739 port
= strtol(p
, (char **)&r
, 0);
2742 saddr
->sin_port
= htons(port
);
2746 /* find or alloc a new VLAN */
2747 VLANState
*qemu_find_vlan(int id
)
2749 VLANState
**pvlan
, *vlan
;
2750 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2754 vlan
= qemu_mallocz(sizeof(VLANState
));
2759 pvlan
= &first_vlan
;
2760 while (*pvlan
!= NULL
)
2761 pvlan
= &(*pvlan
)->next
;
2766 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
2767 IOReadHandler
*fd_read
,
2768 IOCanRWHandler
*fd_can_read
,
2771 VLANClientState
*vc
, **pvc
;
2772 vc
= qemu_mallocz(sizeof(VLANClientState
));
2775 vc
->fd_read
= fd_read
;
2776 vc
->fd_can_read
= fd_can_read
;
2777 vc
->opaque
= opaque
;
2781 pvc
= &vlan
->first_client
;
2782 while (*pvc
!= NULL
)
2783 pvc
= &(*pvc
)->next
;
2788 int qemu_can_send_packet(VLANClientState
*vc1
)
2790 VLANState
*vlan
= vc1
->vlan
;
2791 VLANClientState
*vc
;
2793 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2795 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
2802 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
2804 VLANState
*vlan
= vc1
->vlan
;
2805 VLANClientState
*vc
;
2808 printf("vlan %d send:\n", vlan
->id
);
2809 hex_dump(stdout
, buf
, size
);
2811 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2813 vc
->fd_read(vc
->opaque
, buf
, size
);
2818 #if defined(CONFIG_SLIRP)
2820 /* slirp network adapter */
2822 static int slirp_inited
;
2823 static VLANClientState
*slirp_vc
;
2825 int slirp_can_output(void)
2827 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
2830 void slirp_output(const uint8_t *pkt
, int pkt_len
)
2833 printf("slirp output:\n");
2834 hex_dump(stdout
, pkt
, pkt_len
);
2838 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
2841 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
2844 printf("slirp input:\n");
2845 hex_dump(stdout
, buf
, size
);
2847 slirp_input(buf
, size
);
2850 static int net_slirp_init(VLANState
*vlan
)
2852 if (!slirp_inited
) {
2856 slirp_vc
= qemu_new_vlan_client(vlan
,
2857 slirp_receive
, NULL
, NULL
);
2858 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
2862 static void net_slirp_redir(const char *redir_str
)
2867 struct in_addr guest_addr
;
2868 int host_port
, guest_port
;
2870 if (!slirp_inited
) {
2876 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2878 if (!strcmp(buf
, "tcp")) {
2880 } else if (!strcmp(buf
, "udp")) {
2886 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2888 host_port
= strtol(buf
, &r
, 0);
2892 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2894 if (buf
[0] == '\0') {
2895 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
2897 if (!inet_aton(buf
, &guest_addr
))
2900 guest_port
= strtol(p
, &r
, 0);
2904 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
2905 fprintf(stderr
, "qemu: could not set up redirection\n");
2910 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
2918 static void smb_exit(void)
2922 char filename
[1024];
2924 /* erase all the files in the directory */
2925 d
= opendir(smb_dir
);
2930 if (strcmp(de
->d_name
, ".") != 0 &&
2931 strcmp(de
->d_name
, "..") != 0) {
2932 snprintf(filename
, sizeof(filename
), "%s/%s",
2933 smb_dir
, de
->d_name
);
2941 /* automatic user mode samba server configuration */
2942 void net_slirp_smb(const char *exported_dir
)
2944 char smb_conf
[1024];
2945 char smb_cmdline
[1024];
2948 if (!slirp_inited
) {
2953 /* XXX: better tmp dir construction */
2954 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
2955 if (mkdir(smb_dir
, 0700) < 0) {
2956 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
2959 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
2961 f
= fopen(smb_conf
, "w");
2963 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
2970 "socket address=127.0.0.1\n"
2971 "pid directory=%s\n"
2972 "lock directory=%s\n"
2973 "log file=%s/log.smbd\n"
2974 "smb passwd file=%s/smbpasswd\n"
2975 "security = share\n"
2990 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "/usr/sbin/smbd -s %s",
2993 slirp_add_exec(0, smb_cmdline
, 4, 139);
2996 #endif /* !defined(_WIN32) */
2998 #endif /* CONFIG_SLIRP */
3000 #if !defined(_WIN32)
3002 typedef struct TAPState
{
3003 VLANClientState
*vc
;
3007 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3009 TAPState
*s
= opaque
;
3012 ret
= write(s
->fd
, buf
, size
);
3013 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3020 static void tap_send(void *opaque
)
3022 TAPState
*s
= opaque
;
3026 size
= read(s
->fd
, buf
, sizeof(buf
));
3028 qemu_send_packet(s
->vc
, buf
, size
);
3034 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3038 s
= qemu_mallocz(sizeof(TAPState
));
3042 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3043 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3044 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3049 static int tap_open(char *ifname
, int ifname_size
)
3055 fd
= open("/dev/tap", O_RDWR
);
3057 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3062 dev
= devname(s
.st_rdev
, S_IFCHR
);
3063 pstrcpy(ifname
, ifname_size
, dev
);
3065 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3068 #elif defined(__sun__)
3069 static int tap_open(char *ifname
, int ifname_size
)
3071 fprintf(stderr
, "warning: tap_open not yet implemented\n");
3075 static int tap_open(char *ifname
, int ifname_size
)
3080 fd
= open("/dev/net/tun", O_RDWR
);
3082 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
3085 memset(&ifr
, 0, sizeof(ifr
));
3086 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
3087 if (ifname
[0] != '\0')
3088 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
3090 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
3091 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
3093 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
3097 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
3098 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3103 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
3104 const char *setup_script
)
3107 int pid
, status
, fd
;
3112 if (ifname1
!= NULL
)
3113 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
3116 fd
= tap_open(ifname
, sizeof(ifname
));
3122 if (setup_script
[0] != '\0') {
3123 /* try to launch network init script */
3128 *parg
++ = (char *)setup_script
;
3131 execv(setup_script
, args
);
3134 while (waitpid(pid
, &status
, 0) != pid
);
3135 if (!WIFEXITED(status
) ||
3136 WEXITSTATUS(status
) != 0) {
3137 fprintf(stderr
, "%s: could not launch network script\n",
3143 s
= net_tap_fd_init(vlan
, fd
);
3146 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3147 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
3151 #endif /* !_WIN32 */
3153 /* network connection */
3154 typedef struct NetSocketState
{
3155 VLANClientState
*vc
;
3157 int state
; /* 0 = getting length, 1 = getting data */
3161 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
3164 typedef struct NetSocketListenState
{
3167 } NetSocketListenState
;
3169 /* XXX: we consider we can send the whole packet without blocking */
3170 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
3172 NetSocketState
*s
= opaque
;
3176 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
3177 send_all(s
->fd
, buf
, size
);
3180 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
3182 NetSocketState
*s
= opaque
;
3183 sendto(s
->fd
, buf
, size
, 0,
3184 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
3187 static void net_socket_send(void *opaque
)
3189 NetSocketState
*s
= opaque
;
3194 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
3196 err
= socket_error();
3197 if (err
!= EWOULDBLOCK
)
3199 } else if (size
== 0) {
3200 /* end of connection */
3202 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3208 /* reassemble a packet from the network */
3214 memcpy(s
->buf
+ s
->index
, buf
, l
);
3218 if (s
->index
== 4) {
3220 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
3226 l
= s
->packet_len
- s
->index
;
3229 memcpy(s
->buf
+ s
->index
, buf
, l
);
3233 if (s
->index
>= s
->packet_len
) {
3234 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
3243 static void net_socket_send_dgram(void *opaque
)
3245 NetSocketState
*s
= opaque
;
3248 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
3252 /* end of connection */
3253 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3256 qemu_send_packet(s
->vc
, s
->buf
, size
);
3259 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
3264 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
3265 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
3266 inet_ntoa(mcastaddr
->sin_addr
),
3267 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
3271 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
3273 perror("socket(PF_INET, SOCK_DGRAM)");
3278 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
3279 (const char *)&val
, sizeof(val
));
3281 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
3285 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
3291 /* Add host to multicast group */
3292 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
3293 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
3295 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
3296 (const char *)&imr
, sizeof(struct ip_mreq
));
3298 perror("setsockopt(IP_ADD_MEMBERSHIP)");
3302 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
3304 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
3305 (const char *)&val
, sizeof(val
));
3307 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
3311 socket_set_nonblock(fd
);
3319 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
3322 struct sockaddr_in saddr
;
3324 socklen_t saddr_len
;
3327 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
3328 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
3329 * by ONLY ONE process: we must "clone" this dgram socket --jjo
3333 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
3335 if (saddr
.sin_addr
.s_addr
==0) {
3336 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
3340 /* clone dgram socket */
3341 newfd
= net_socket_mcast_create(&saddr
);
3343 /* error already reported by net_socket_mcast_create() */
3347 /* clone newfd to fd, close newfd */
3352 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
3353 fd
, strerror(errno
));
3358 s
= qemu_mallocz(sizeof(NetSocketState
));
3363 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
3364 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
3366 /* mcast: save bound address as dst */
3367 if (is_connected
) s
->dgram_dst
=saddr
;
3369 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3370 "socket: fd=%d (%s mcast=%s:%d)",
3371 fd
, is_connected
? "cloned" : "",
3372 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3376 static void net_socket_connect(void *opaque
)
3378 NetSocketState
*s
= opaque
;
3379 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
3382 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
3386 s
= qemu_mallocz(sizeof(NetSocketState
));
3390 s
->vc
= qemu_new_vlan_client(vlan
,
3391 net_socket_receive
, NULL
, s
);
3392 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3393 "socket: fd=%d", fd
);
3395 net_socket_connect(s
);
3397 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
3402 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
3405 int so_type
=-1, optlen
=sizeof(so_type
);
3407 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
3408 fprintf(stderr
, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd
);
3413 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
3415 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3417 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
3418 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
3419 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3424 static void net_socket_accept(void *opaque
)
3426 NetSocketListenState
*s
= opaque
;
3428 struct sockaddr_in saddr
;
3433 len
= sizeof(saddr
);
3434 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
3435 if (fd
< 0 && errno
!= EINTR
) {
3437 } else if (fd
>= 0) {
3441 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
3445 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
3446 "socket: connection from %s:%d",
3447 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3451 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
3453 NetSocketListenState
*s
;
3455 struct sockaddr_in saddr
;
3457 if (parse_host_port(&saddr
, host_str
) < 0)
3460 s
= qemu_mallocz(sizeof(NetSocketListenState
));
3464 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3469 socket_set_nonblock(fd
);
3471 /* allow fast reuse */
3473 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3475 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3480 ret
= listen(fd
, 0);
3487 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
3491 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
3494 int fd
, connected
, ret
, err
;
3495 struct sockaddr_in saddr
;
3497 if (parse_host_port(&saddr
, host_str
) < 0)
3500 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3505 socket_set_nonblock(fd
);
3509 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3511 err
= socket_error();
3512 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3513 } else if (err
== EINPROGRESS
) {
3525 s
= net_socket_fd_init(vlan
, fd
, connected
);
3528 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3529 "socket: connect to %s:%d",
3530 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3534 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
3538 struct sockaddr_in saddr
;
3540 if (parse_host_port(&saddr
, host_str
) < 0)
3544 fd
= net_socket_mcast_create(&saddr
);
3548 s
= net_socket_fd_init(vlan
, fd
, 0);
3552 s
->dgram_dst
= saddr
;
3554 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3555 "socket: mcast=%s:%d",
3556 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3561 static int get_param_value(char *buf
, int buf_size
,
3562 const char *tag
, const char *str
)
3571 while (*p
!= '\0' && *p
!= '=') {
3572 if ((q
- option
) < sizeof(option
) - 1)
3580 if (!strcmp(tag
, option
)) {
3582 while (*p
!= '\0' && *p
!= ',') {
3583 if ((q
- buf
) < buf_size
- 1)
3590 while (*p
!= '\0' && *p
!= ',') {
3601 int net_client_init(const char *str
)
3612 while (*p
!= '\0' && *p
!= ',') {
3613 if ((q
- device
) < sizeof(device
) - 1)
3621 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
3622 vlan_id
= strtol(buf
, NULL
, 0);
3624 vlan
= qemu_find_vlan(vlan_id
);
3626 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
3629 if (!strcmp(device
, "nic")) {
3633 if (nb_nics
>= MAX_NICS
) {
3634 fprintf(stderr
, "Too Many NICs\n");
3637 nd
= &nd_table
[nb_nics
];
3638 macaddr
= nd
->macaddr
;
3644 macaddr
[5] = 0x56 + nb_nics
;
3646 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
3647 if (parse_macaddr(macaddr
, buf
) < 0) {
3648 fprintf(stderr
, "invalid syntax for ethernet address\n");
3652 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
3653 nd
->model
= strdup(buf
);
3659 if (!strcmp(device
, "none")) {
3660 /* does nothing. It is needed to signal that no network cards
3665 if (!strcmp(device
, "user")) {
3666 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
3667 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
3669 ret
= net_slirp_init(vlan
);
3673 if (!strcmp(device
, "tap")) {
3675 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
3676 fprintf(stderr
, "tap: no interface name\n");
3679 ret
= tap_win32_init(vlan
, ifname
);
3682 if (!strcmp(device
, "tap")) {
3684 char setup_script
[1024];
3686 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3687 fd
= strtol(buf
, NULL
, 0);
3689 if (net_tap_fd_init(vlan
, fd
))
3692 get_param_value(ifname
, sizeof(ifname
), "ifname", p
);
3693 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
3694 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
3696 ret
= net_tap_init(vlan
, ifname
, setup_script
);
3700 if (!strcmp(device
, "socket")) {
3701 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3703 fd
= strtol(buf
, NULL
, 0);
3705 if (net_socket_fd_init(vlan
, fd
, 1))
3707 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
3708 ret
= net_socket_listen_init(vlan
, buf
);
3709 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
3710 ret
= net_socket_connect_init(vlan
, buf
);
3711 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
3712 ret
= net_socket_mcast_init(vlan
, buf
);
3714 fprintf(stderr
, "Unknown socket options: %s\n", p
);
3719 fprintf(stderr
, "Unknown network device: %s\n", device
);
3723 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
3729 void do_info_network(void)
3732 VLANClientState
*vc
;
3734 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3735 term_printf("VLAN %d devices:\n", vlan
->id
);
3736 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3737 term_printf(" %s\n", vc
->info_str
);
3741 /***********************************************************/
3744 static USBPort
*used_usb_ports
;
3745 static USBPort
*free_usb_ports
;
3747 /* ??? Maybe change this to register a hub to keep track of the topology. */
3748 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
3749 usb_attachfn attach
)
3751 port
->opaque
= opaque
;
3752 port
->index
= index
;
3753 port
->attach
= attach
;
3754 port
->next
= free_usb_ports
;
3755 free_usb_ports
= port
;
3758 static int usb_device_add(const char *devname
)
3764 if (!free_usb_ports
)
3767 if (strstart(devname
, "host:", &p
)) {
3768 dev
= usb_host_device_open(p
);
3769 } else if (!strcmp(devname
, "mouse")) {
3770 dev
= usb_mouse_init();
3771 } else if (!strcmp(devname
, "tablet")) {
3772 dev
= usb_tablet_init();
3773 } else if (strstart(devname
, "disk:", &p
)) {
3774 dev
= usb_msd_init(p
);
3781 /* Find a USB port to add the device to. */
3782 port
= free_usb_ports
;
3786 /* Create a new hub and chain it on. */
3787 free_usb_ports
= NULL
;
3788 port
->next
= used_usb_ports
;
3789 used_usb_ports
= port
;
3791 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
3792 usb_attach(port
, hub
);
3793 port
= free_usb_ports
;
3796 free_usb_ports
= port
->next
;
3797 port
->next
= used_usb_ports
;
3798 used_usb_ports
= port
;
3799 usb_attach(port
, dev
);
3803 static int usb_device_del(const char *devname
)
3811 if (!used_usb_ports
)
3814 p
= strchr(devname
, '.');
3817 bus_num
= strtoul(devname
, NULL
, 0);
3818 addr
= strtoul(p
+ 1, NULL
, 0);
3822 lastp
= &used_usb_ports
;
3823 port
= used_usb_ports
;
3824 while (port
&& port
->dev
->addr
!= addr
) {
3825 lastp
= &port
->next
;
3833 *lastp
= port
->next
;
3834 usb_attach(port
, NULL
);
3835 dev
->handle_destroy(dev
);
3836 port
->next
= free_usb_ports
;
3837 free_usb_ports
= port
;
3841 void do_usb_add(const char *devname
)
3844 ret
= usb_device_add(devname
);
3846 term_printf("Could not add USB device '%s'\n", devname
);
3849 void do_usb_del(const char *devname
)
3852 ret
= usb_device_del(devname
);
3854 term_printf("Could not remove USB device '%s'\n", devname
);
3861 const char *speed_str
;
3864 term_printf("USB support not enabled\n");
3868 for (port
= used_usb_ports
; port
; port
= port
->next
) {
3872 switch(dev
->speed
) {
3876 case USB_SPEED_FULL
:
3879 case USB_SPEED_HIGH
:
3886 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
3887 0, dev
->addr
, speed_str
, dev
->devname
);
3891 /***********************************************************/
3894 static char *pid_filename
;
3896 /* Remove PID file. Called on normal exit */
3898 static void remove_pidfile(void)
3900 unlink (pid_filename
);
3903 static void create_pidfile(const char *filename
)
3905 struct stat pidstat
;
3908 /* Try to write our PID to the named file */
3909 if (stat(filename
, &pidstat
) < 0) {
3910 if (errno
== ENOENT
) {
3911 if ((f
= fopen (filename
, "w")) == NULL
) {
3912 perror("Opening pidfile");
3915 fprintf(f
, "%d\n", getpid());
3917 pid_filename
= qemu_strdup(filename
);
3918 if (!pid_filename
) {
3919 fprintf(stderr
, "Could not save PID filename");
3922 atexit(remove_pidfile
);
3925 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
3931 /***********************************************************/
3934 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
3938 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
3942 static void dumb_refresh(DisplayState
*ds
)
3947 void dumb_display_init(DisplayState
*ds
)
3952 ds
->dpy_update
= dumb_update
;
3953 ds
->dpy_resize
= dumb_resize
;
3954 ds
->dpy_refresh
= dumb_refresh
;
3957 /***********************************************************/
3960 #define MAX_IO_HANDLERS 64
3962 typedef struct IOHandlerRecord
{
3964 IOCanRWHandler
*fd_read_poll
;
3966 IOHandler
*fd_write
;
3968 /* temporary data */
3970 struct IOHandlerRecord
*next
;
3973 static IOHandlerRecord
*first_io_handler
;
3975 /* XXX: fd_read_poll should be suppressed, but an API change is
3976 necessary in the character devices to suppress fd_can_read(). */
3977 int qemu_set_fd_handler2(int fd
,
3978 IOCanRWHandler
*fd_read_poll
,
3980 IOHandler
*fd_write
,
3983 IOHandlerRecord
**pioh
, *ioh
;
3985 if (!fd_read
&& !fd_write
) {
3986 pioh
= &first_io_handler
;
3991 if (ioh
->fd
== fd
) {
3999 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4003 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4006 ioh
->next
= first_io_handler
;
4007 first_io_handler
= ioh
;
4010 ioh
->fd_read_poll
= fd_read_poll
;
4011 ioh
->fd_read
= fd_read
;
4012 ioh
->fd_write
= fd_write
;
4013 ioh
->opaque
= opaque
;
4018 int qemu_set_fd_handler(int fd
,
4020 IOHandler
*fd_write
,
4023 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4026 /***********************************************************/
4027 /* Polling handling */
4029 typedef struct PollingEntry
{
4032 struct PollingEntry
*next
;
4035 static PollingEntry
*first_polling_entry
;
4037 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4039 PollingEntry
**ppe
, *pe
;
4040 pe
= qemu_mallocz(sizeof(PollingEntry
));
4044 pe
->opaque
= opaque
;
4045 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
4050 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
4052 PollingEntry
**ppe
, *pe
;
4053 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
4055 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
4064 /***********************************************************/
4065 /* Wait objects support */
4066 typedef struct WaitObjects
{
4068 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
4069 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
4070 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
4073 static WaitObjects wait_objects
= {0};
4075 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4077 WaitObjects
*w
= &wait_objects
;
4079 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
4081 w
->events
[w
->num
] = handle
;
4082 w
->func
[w
->num
] = func
;
4083 w
->opaque
[w
->num
] = opaque
;
4088 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4091 WaitObjects
*w
= &wait_objects
;
4094 for (i
= 0; i
< w
->num
; i
++) {
4095 if (w
->events
[i
] == handle
)
4098 w
->events
[i
] = w
->events
[i
+ 1];
4099 w
->func
[i
] = w
->func
[i
+ 1];
4100 w
->opaque
[i
] = w
->opaque
[i
+ 1];
4108 /***********************************************************/
4109 /* savevm/loadvm support */
4111 #define IO_BUF_SIZE 32768
4115 BlockDriverState
*bs
;
4118 int64_t base_offset
;
4119 int64_t buf_offset
; /* start of buffer when writing, end of buffer
4122 int buf_size
; /* 0 when writing */
4123 uint8_t buf
[IO_BUF_SIZE
];
4126 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
4130 f
= qemu_mallocz(sizeof(QEMUFile
));
4133 if (!strcmp(mode
, "wb")) {
4135 } else if (!strcmp(mode
, "rb")) {
4140 f
->outfile
= fopen(filename
, mode
);
4152 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
4156 f
= qemu_mallocz(sizeof(QEMUFile
));
4161 f
->is_writable
= is_writable
;
4162 f
->base_offset
= offset
;
4166 void qemu_fflush(QEMUFile
*f
)
4168 if (!f
->is_writable
)
4170 if (f
->buf_index
> 0) {
4172 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4173 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
4175 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4176 f
->buf
, f
->buf_index
);
4178 f
->buf_offset
+= f
->buf_index
;
4183 static void qemu_fill_buffer(QEMUFile
*f
)
4190 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4191 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
4195 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4196 f
->buf
, IO_BUF_SIZE
);
4202 f
->buf_offset
+= len
;
4205 void qemu_fclose(QEMUFile
*f
)
4215 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
4219 l
= IO_BUF_SIZE
- f
->buf_index
;
4222 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
4226 if (f
->buf_index
>= IO_BUF_SIZE
)
4231 void qemu_put_byte(QEMUFile
*f
, int v
)
4233 f
->buf
[f
->buf_index
++] = v
;
4234 if (f
->buf_index
>= IO_BUF_SIZE
)
4238 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
4244 l
= f
->buf_size
- f
->buf_index
;
4246 qemu_fill_buffer(f
);
4247 l
= f
->buf_size
- f
->buf_index
;
4253 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
4258 return size1
- size
;
4261 int qemu_get_byte(QEMUFile
*f
)
4263 if (f
->buf_index
>= f
->buf_size
) {
4264 qemu_fill_buffer(f
);
4265 if (f
->buf_index
>= f
->buf_size
)
4268 return f
->buf
[f
->buf_index
++];
4271 int64_t qemu_ftell(QEMUFile
*f
)
4273 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
4276 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
4278 if (whence
== SEEK_SET
) {
4280 } else if (whence
== SEEK_CUR
) {
4281 pos
+= qemu_ftell(f
);
4283 /* SEEK_END not supported */
4286 if (f
->is_writable
) {
4288 f
->buf_offset
= pos
;
4290 f
->buf_offset
= pos
;
4297 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
4299 qemu_put_byte(f
, v
>> 8);
4300 qemu_put_byte(f
, v
);
4303 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
4305 qemu_put_byte(f
, v
>> 24);
4306 qemu_put_byte(f
, v
>> 16);
4307 qemu_put_byte(f
, v
>> 8);
4308 qemu_put_byte(f
, v
);
4311 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
4313 qemu_put_be32(f
, v
>> 32);
4314 qemu_put_be32(f
, v
);
4317 unsigned int qemu_get_be16(QEMUFile
*f
)
4320 v
= qemu_get_byte(f
) << 8;
4321 v
|= qemu_get_byte(f
);
4325 unsigned int qemu_get_be32(QEMUFile
*f
)
4328 v
= qemu_get_byte(f
) << 24;
4329 v
|= qemu_get_byte(f
) << 16;
4330 v
|= qemu_get_byte(f
) << 8;
4331 v
|= qemu_get_byte(f
);
4335 uint64_t qemu_get_be64(QEMUFile
*f
)
4338 v
= (uint64_t)qemu_get_be32(f
) << 32;
4339 v
|= qemu_get_be32(f
);
4343 typedef struct SaveStateEntry
{
4347 SaveStateHandler
*save_state
;
4348 LoadStateHandler
*load_state
;
4350 struct SaveStateEntry
*next
;
4353 static SaveStateEntry
*first_se
;
4355 int register_savevm(const char *idstr
,
4358 SaveStateHandler
*save_state
,
4359 LoadStateHandler
*load_state
,
4362 SaveStateEntry
*se
, **pse
;
4364 se
= qemu_malloc(sizeof(SaveStateEntry
));
4367 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
4368 se
->instance_id
= instance_id
;
4369 se
->version_id
= version_id
;
4370 se
->save_state
= save_state
;
4371 se
->load_state
= load_state
;
4372 se
->opaque
= opaque
;
4375 /* add at the end of list */
4377 while (*pse
!= NULL
)
4378 pse
= &(*pse
)->next
;
4383 #define QEMU_VM_FILE_MAGIC 0x5145564d
4384 #define QEMU_VM_FILE_VERSION 0x00000002
4386 int qemu_savevm_state(QEMUFile
*f
)
4390 int64_t cur_pos
, len_pos
, total_len_pos
;
4392 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
4393 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
4394 total_len_pos
= qemu_ftell(f
);
4395 qemu_put_be64(f
, 0); /* total size */
4397 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4399 len
= strlen(se
->idstr
);
4400 qemu_put_byte(f
, len
);
4401 qemu_put_buffer(f
, se
->idstr
, len
);
4403 qemu_put_be32(f
, se
->instance_id
);
4404 qemu_put_be32(f
, se
->version_id
);
4406 /* record size: filled later */
4407 len_pos
= qemu_ftell(f
);
4408 qemu_put_be32(f
, 0);
4410 se
->save_state(f
, se
->opaque
);
4412 /* fill record size */
4413 cur_pos
= qemu_ftell(f
);
4414 len
= cur_pos
- len_pos
- 4;
4415 qemu_fseek(f
, len_pos
, SEEK_SET
);
4416 qemu_put_be32(f
, len
);
4417 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4419 cur_pos
= qemu_ftell(f
);
4420 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
4421 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
4422 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4428 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
4432 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4433 if (!strcmp(se
->idstr
, idstr
) &&
4434 instance_id
== se
->instance_id
)
4440 int qemu_loadvm_state(QEMUFile
*f
)
4443 int len
, ret
, instance_id
, record_len
, version_id
;
4444 int64_t total_len
, end_pos
, cur_pos
;
4448 v
= qemu_get_be32(f
);
4449 if (v
!= QEMU_VM_FILE_MAGIC
)
4451 v
= qemu_get_be32(f
);
4452 if (v
!= QEMU_VM_FILE_VERSION
) {
4457 total_len
= qemu_get_be64(f
);
4458 end_pos
= total_len
+ qemu_ftell(f
);
4460 if (qemu_ftell(f
) >= end_pos
)
4462 len
= qemu_get_byte(f
);
4463 qemu_get_buffer(f
, idstr
, len
);
4465 instance_id
= qemu_get_be32(f
);
4466 version_id
= qemu_get_be32(f
);
4467 record_len
= qemu_get_be32(f
);
4469 printf("idstr=%s instance=0x%x version=%d len=%d\n",
4470 idstr
, instance_id
, version_id
, record_len
);
4472 cur_pos
= qemu_ftell(f
);
4473 se
= find_se(idstr
, instance_id
);
4475 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
4476 instance_id
, idstr
);
4478 ret
= se
->load_state(f
, se
->opaque
, version_id
);
4480 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
4481 instance_id
, idstr
);
4484 /* always seek to exact end of record */
4485 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
4492 /* device can contain snapshots */
4493 static int bdrv_can_snapshot(BlockDriverState
*bs
)
4496 !bdrv_is_removable(bs
) &&
4497 !bdrv_is_read_only(bs
));
4500 /* device must be snapshots in order to have a reliable snapshot */
4501 static int bdrv_has_snapshot(BlockDriverState
*bs
)
4504 !bdrv_is_removable(bs
) &&
4505 !bdrv_is_read_only(bs
));
4508 static BlockDriverState
*get_bs_snapshots(void)
4510 BlockDriverState
*bs
;
4514 return bs_snapshots
;
4515 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4517 if (bdrv_can_snapshot(bs
))
4526 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
4529 QEMUSnapshotInfo
*sn_tab
, *sn
;
4533 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
4536 for(i
= 0; i
< nb_sns
; i
++) {
4538 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
4548 void do_savevm(const char *name
)
4550 BlockDriverState
*bs
, *bs1
;
4551 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
4552 int must_delete
, ret
, i
;
4553 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
4555 int saved_vm_running
;
4562 bs
= get_bs_snapshots();
4564 term_printf("No block device can accept snapshots\n");
4568 /* ??? Should this occur after vm_stop? */
4571 saved_vm_running
= vm_running
;
4576 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
4581 memset(sn
, 0, sizeof(*sn
));
4583 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
4584 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
4587 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
4590 /* fill auxiliary fields */
4593 sn
->date_sec
= tb
.time
;
4594 sn
->date_nsec
= tb
.millitm
* 1000000;
4596 gettimeofday(&tv
, NULL
);
4597 sn
->date_sec
= tv
.tv_sec
;
4598 sn
->date_nsec
= tv
.tv_usec
* 1000;
4600 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
4602 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
4603 term_printf("Device %s does not support VM state snapshots\n",
4604 bdrv_get_device_name(bs
));
4608 /* save the VM state */
4609 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
4611 term_printf("Could not open VM state file\n");
4614 ret
= qemu_savevm_state(f
);
4615 sn
->vm_state_size
= qemu_ftell(f
);
4618 term_printf("Error %d while writing VM\n", ret
);
4622 /* create the snapshots */
4624 for(i
= 0; i
< MAX_DISKS
; i
++) {
4626 if (bdrv_has_snapshot(bs1
)) {
4628 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
4630 term_printf("Error while deleting snapshot on '%s'\n",
4631 bdrv_get_device_name(bs1
));
4634 ret
= bdrv_snapshot_create(bs1
, sn
);
4636 term_printf("Error while creating snapshot on '%s'\n",
4637 bdrv_get_device_name(bs1
));
4643 if (saved_vm_running
)
4647 void do_loadvm(const char *name
)
4649 BlockDriverState
*bs
, *bs1
;
4650 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
4653 int saved_vm_running
;
4655 bs
= get_bs_snapshots();
4657 term_printf("No block device supports snapshots\n");
4661 /* Flush all IO requests so they don't interfere with the new state. */
4664 saved_vm_running
= vm_running
;
4667 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4669 if (bdrv_has_snapshot(bs1
)) {
4670 ret
= bdrv_snapshot_goto(bs1
, name
);
4673 term_printf("Warning: ");
4676 term_printf("Snapshots not supported on device '%s'\n",
4677 bdrv_get_device_name(bs1
));
4680 term_printf("Could not find snapshot '%s' on device '%s'\n",
4681 name
, bdrv_get_device_name(bs1
));
4684 term_printf("Error %d while activating snapshot on '%s'\n",
4685 ret
, bdrv_get_device_name(bs1
));
4688 /* fatal on snapshot block device */
4695 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
4696 term_printf("Device %s does not support VM state snapshots\n",
4697 bdrv_get_device_name(bs
));
4701 /* restore the VM state */
4702 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
4704 term_printf("Could not open VM state file\n");
4707 ret
= qemu_loadvm_state(f
);
4710 term_printf("Error %d while loading VM state\n", ret
);
4713 if (saved_vm_running
)
4717 void do_delvm(const char *name
)
4719 BlockDriverState
*bs
, *bs1
;
4722 bs
= get_bs_snapshots();
4724 term_printf("No block device supports snapshots\n");
4728 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4730 if (bdrv_has_snapshot(bs1
)) {
4731 ret
= bdrv_snapshot_delete(bs1
, name
);
4733 if (ret
== -ENOTSUP
)
4734 term_printf("Snapshots not supported on device '%s'\n",
4735 bdrv_get_device_name(bs1
));
4737 term_printf("Error %d while deleting snapshot on '%s'\n",
4738 ret
, bdrv_get_device_name(bs1
));
4744 void do_info_snapshots(void)
4746 BlockDriverState
*bs
, *bs1
;
4747 QEMUSnapshotInfo
*sn_tab
, *sn
;
4751 bs
= get_bs_snapshots();
4753 term_printf("No available block device supports snapshots\n");
4756 term_printf("Snapshot devices:");
4757 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4759 if (bdrv_has_snapshot(bs1
)) {
4761 term_printf(" %s", bdrv_get_device_name(bs1
));
4766 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
4768 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
4771 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
4772 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
4773 for(i
= 0; i
< nb_sns
; i
++) {
4775 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
4780 /***********************************************************/
4781 /* cpu save/restore */
4783 #if defined(TARGET_I386)
4785 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
4787 qemu_put_be32(f
, dt
->selector
);
4788 qemu_put_betl(f
, dt
->base
);
4789 qemu_put_be32(f
, dt
->limit
);
4790 qemu_put_be32(f
, dt
->flags
);
4793 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
4795 dt
->selector
= qemu_get_be32(f
);
4796 dt
->base
= qemu_get_betl(f
);
4797 dt
->limit
= qemu_get_be32(f
);
4798 dt
->flags
= qemu_get_be32(f
);
4801 void cpu_save(QEMUFile
*f
, void *opaque
)
4803 CPUState
*env
= opaque
;
4804 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
4808 for(i
= 0; i
< CPU_NB_REGS
; i
++)
4809 qemu_put_betls(f
, &env
->regs
[i
]);
4810 qemu_put_betls(f
, &env
->eip
);
4811 qemu_put_betls(f
, &env
->eflags
);
4812 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
4813 qemu_put_be32s(f
, &hflags
);
4817 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
4819 for(i
= 0; i
< 8; i
++) {
4820 fptag
|= ((!env
->fptags
[i
]) << i
);
4823 qemu_put_be16s(f
, &fpuc
);
4824 qemu_put_be16s(f
, &fpus
);
4825 qemu_put_be16s(f
, &fptag
);
4827 #ifdef USE_X86LDOUBLE
4832 qemu_put_be16s(f
, &fpregs_format
);
4834 for(i
= 0; i
< 8; i
++) {
4835 #ifdef USE_X86LDOUBLE
4839 /* we save the real CPU data (in case of MMX usage only 'mant'
4840 contains the MMX register */
4841 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
4842 qemu_put_be64(f
, mant
);
4843 qemu_put_be16(f
, exp
);
4846 /* if we use doubles for float emulation, we save the doubles to
4847 avoid losing information in case of MMX usage. It can give
4848 problems if the image is restored on a CPU where long
4849 doubles are used instead. */
4850 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
4854 for(i
= 0; i
< 6; i
++)
4855 cpu_put_seg(f
, &env
->segs
[i
]);
4856 cpu_put_seg(f
, &env
->ldt
);
4857 cpu_put_seg(f
, &env
->tr
);
4858 cpu_put_seg(f
, &env
->gdt
);
4859 cpu_put_seg(f
, &env
->idt
);
4861 qemu_put_be32s(f
, &env
->sysenter_cs
);
4862 qemu_put_be32s(f
, &env
->sysenter_esp
);
4863 qemu_put_be32s(f
, &env
->sysenter_eip
);
4865 qemu_put_betls(f
, &env
->cr
[0]);
4866 qemu_put_betls(f
, &env
->cr
[2]);
4867 qemu_put_betls(f
, &env
->cr
[3]);
4868 qemu_put_betls(f
, &env
->cr
[4]);
4870 for(i
= 0; i
< 8; i
++)
4871 qemu_put_betls(f
, &env
->dr
[i
]);
4874 qemu_put_be32s(f
, &env
->a20_mask
);
4877 qemu_put_be32s(f
, &env
->mxcsr
);
4878 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
4879 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
4880 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
4883 #ifdef TARGET_X86_64
4884 qemu_put_be64s(f
, &env
->efer
);
4885 qemu_put_be64s(f
, &env
->star
);
4886 qemu_put_be64s(f
, &env
->lstar
);
4887 qemu_put_be64s(f
, &env
->cstar
);
4888 qemu_put_be64s(f
, &env
->fmask
);
4889 qemu_put_be64s(f
, &env
->kernelgsbase
);
4891 qemu_put_be32s(f
, &env
->smbase
);
4894 #ifdef USE_X86LDOUBLE
4895 /* XXX: add that in a FPU generic layer */
4896 union x86_longdouble
{
4901 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
4902 #define EXPBIAS1 1023
4903 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
4904 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
4906 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
4910 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
4911 /* exponent + sign */
4912 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
4913 e
|= SIGND1(temp
) >> 16;
4918 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4920 CPUState
*env
= opaque
;
4923 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
4925 if (version_id
!= 3 && version_id
!= 4)
4927 for(i
= 0; i
< CPU_NB_REGS
; i
++)
4928 qemu_get_betls(f
, &env
->regs
[i
]);
4929 qemu_get_betls(f
, &env
->eip
);
4930 qemu_get_betls(f
, &env
->eflags
);
4931 qemu_get_be32s(f
, &hflags
);
4933 qemu_get_be16s(f
, &fpuc
);
4934 qemu_get_be16s(f
, &fpus
);
4935 qemu_get_be16s(f
, &fptag
);
4936 qemu_get_be16s(f
, &fpregs_format
);
4938 /* NOTE: we cannot always restore the FPU state if the image come
4939 from a host with a different 'USE_X86LDOUBLE' define. We guess
4940 if we are in an MMX state to restore correctly in that case. */
4941 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
4942 for(i
= 0; i
< 8; i
++) {
4946 switch(fpregs_format
) {
4948 mant
= qemu_get_be64(f
);
4949 exp
= qemu_get_be16(f
);
4950 #ifdef USE_X86LDOUBLE
4951 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
4953 /* difficult case */
4955 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
4957 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
4961 mant
= qemu_get_be64(f
);
4962 #ifdef USE_X86LDOUBLE
4964 union x86_longdouble
*p
;
4965 /* difficult case */
4966 p
= (void *)&env
->fpregs
[i
];
4971 fp64_to_fp80(p
, mant
);
4975 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
4984 /* XXX: restore FPU round state */
4985 env
->fpstt
= (fpus
>> 11) & 7;
4986 env
->fpus
= fpus
& ~0x3800;
4988 for(i
= 0; i
< 8; i
++) {
4989 env
->fptags
[i
] = (fptag
>> i
) & 1;
4992 for(i
= 0; i
< 6; i
++)
4993 cpu_get_seg(f
, &env
->segs
[i
]);
4994 cpu_get_seg(f
, &env
->ldt
);
4995 cpu_get_seg(f
, &env
->tr
);
4996 cpu_get_seg(f
, &env
->gdt
);
4997 cpu_get_seg(f
, &env
->idt
);
4999 qemu_get_be32s(f
, &env
->sysenter_cs
);
5000 qemu_get_be32s(f
, &env
->sysenter_esp
);
5001 qemu_get_be32s(f
, &env
->sysenter_eip
);
5003 qemu_get_betls(f
, &env
->cr
[0]);
5004 qemu_get_betls(f
, &env
->cr
[2]);
5005 qemu_get_betls(f
, &env
->cr
[3]);
5006 qemu_get_betls(f
, &env
->cr
[4]);
5008 for(i
= 0; i
< 8; i
++)
5009 qemu_get_betls(f
, &env
->dr
[i
]);
5012 qemu_get_be32s(f
, &env
->a20_mask
);
5014 qemu_get_be32s(f
, &env
->mxcsr
);
5015 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5016 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5017 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5020 #ifdef TARGET_X86_64
5021 qemu_get_be64s(f
, &env
->efer
);
5022 qemu_get_be64s(f
, &env
->star
);
5023 qemu_get_be64s(f
, &env
->lstar
);
5024 qemu_get_be64s(f
, &env
->cstar
);
5025 qemu_get_be64s(f
, &env
->fmask
);
5026 qemu_get_be64s(f
, &env
->kernelgsbase
);
5028 if (version_id
>= 4)
5029 qemu_get_be32s(f
, &env
->smbase
);
5031 /* XXX: compute hflags from scratch, except for CPL and IIF */
5032 env
->hflags
= hflags
;
5037 #elif defined(TARGET_PPC)
5038 void cpu_save(QEMUFile
*f
, void *opaque
)
5042 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5047 #elif defined(TARGET_MIPS)
5048 void cpu_save(QEMUFile
*f
, void *opaque
)
5052 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5057 #elif defined(TARGET_SPARC)
5058 void cpu_save(QEMUFile
*f
, void *opaque
)
5060 CPUState
*env
= opaque
;
5064 for(i
= 0; i
< 8; i
++)
5065 qemu_put_betls(f
, &env
->gregs
[i
]);
5066 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5067 qemu_put_betls(f
, &env
->regbase
[i
]);
5070 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5076 qemu_put_be32(f
, u
.i
);
5079 qemu_put_betls(f
, &env
->pc
);
5080 qemu_put_betls(f
, &env
->npc
);
5081 qemu_put_betls(f
, &env
->y
);
5083 qemu_put_be32(f
, tmp
);
5084 qemu_put_betls(f
, &env
->fsr
);
5085 qemu_put_betls(f
, &env
->tbr
);
5086 #ifndef TARGET_SPARC64
5087 qemu_put_be32s(f
, &env
->wim
);
5089 for(i
= 0; i
< 16; i
++)
5090 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
5094 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5096 CPUState
*env
= opaque
;
5100 for(i
= 0; i
< 8; i
++)
5101 qemu_get_betls(f
, &env
->gregs
[i
]);
5102 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5103 qemu_get_betls(f
, &env
->regbase
[i
]);
5106 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5111 u
.i
= qemu_get_be32(f
);
5115 qemu_get_betls(f
, &env
->pc
);
5116 qemu_get_betls(f
, &env
->npc
);
5117 qemu_get_betls(f
, &env
->y
);
5118 tmp
= qemu_get_be32(f
);
5119 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
5120 correctly updated */
5122 qemu_get_betls(f
, &env
->fsr
);
5123 qemu_get_betls(f
, &env
->tbr
);
5124 #ifndef TARGET_SPARC64
5125 qemu_get_be32s(f
, &env
->wim
);
5127 for(i
= 0; i
< 16; i
++)
5128 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
5134 #elif defined(TARGET_ARM)
5136 /* ??? Need to implement these. */
5137 void cpu_save(QEMUFile
*f
, void *opaque
)
5141 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5148 #warning No CPU save/restore functions
5152 /***********************************************************/
5153 /* ram save/restore */
5155 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
5159 v
= qemu_get_byte(f
);
5162 if (qemu_get_buffer(f
, buf
, len
) != len
)
5166 v
= qemu_get_byte(f
);
5167 memset(buf
, v
, len
);
5175 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
5179 if (qemu_get_be32(f
) != phys_ram_size
)
5181 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
5182 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
5189 #define BDRV_HASH_BLOCK_SIZE 1024
5190 #define IOBUF_SIZE 4096
5191 #define RAM_CBLOCK_MAGIC 0xfabe
5193 typedef struct RamCompressState
{
5196 uint8_t buf
[IOBUF_SIZE
];
5199 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
5202 memset(s
, 0, sizeof(*s
));
5204 ret
= deflateInit2(&s
->zstream
, 1,
5206 9, Z_DEFAULT_STRATEGY
);
5209 s
->zstream
.avail_out
= IOBUF_SIZE
;
5210 s
->zstream
.next_out
= s
->buf
;
5214 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
5216 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
5217 qemu_put_be16(s
->f
, len
);
5218 qemu_put_buffer(s
->f
, buf
, len
);
5221 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
5225 s
->zstream
.avail_in
= len
;
5226 s
->zstream
.next_in
= (uint8_t *)buf
;
5227 while (s
->zstream
.avail_in
> 0) {
5228 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
5231 if (s
->zstream
.avail_out
== 0) {
5232 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
5233 s
->zstream
.avail_out
= IOBUF_SIZE
;
5234 s
->zstream
.next_out
= s
->buf
;
5240 static void ram_compress_close(RamCompressState
*s
)
5244 /* compress last bytes */
5246 ret
= deflate(&s
->zstream
, Z_FINISH
);
5247 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
5248 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
5250 ram_put_cblock(s
, s
->buf
, len
);
5252 s
->zstream
.avail_out
= IOBUF_SIZE
;
5253 s
->zstream
.next_out
= s
->buf
;
5254 if (ret
== Z_STREAM_END
)
5261 deflateEnd(&s
->zstream
);
5264 typedef struct RamDecompressState
{
5267 uint8_t buf
[IOBUF_SIZE
];
5268 } RamDecompressState
;
5270 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
5273 memset(s
, 0, sizeof(*s
));
5275 ret
= inflateInit(&s
->zstream
);
5281 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
5285 s
->zstream
.avail_out
= len
;
5286 s
->zstream
.next_out
= buf
;
5287 while (s
->zstream
.avail_out
> 0) {
5288 if (s
->zstream
.avail_in
== 0) {
5289 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
5291 clen
= qemu_get_be16(s
->f
);
5292 if (clen
> IOBUF_SIZE
)
5294 qemu_get_buffer(s
->f
, s
->buf
, clen
);
5295 s
->zstream
.avail_in
= clen
;
5296 s
->zstream
.next_in
= s
->buf
;
5298 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
5299 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
5306 static void ram_decompress_close(RamDecompressState
*s
)
5308 inflateEnd(&s
->zstream
);
5311 static void ram_save(QEMUFile
*f
, void *opaque
)
5314 RamCompressState s1
, *s
= &s1
;
5317 qemu_put_be32(f
, phys_ram_size
);
5318 if (ram_compress_open(s
, f
) < 0)
5320 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5322 if (tight_savevm_enabled
) {
5326 /* find if the memory block is available on a virtual
5329 for(j
= 0; j
< MAX_DISKS
; j
++) {
5331 sector_num
= bdrv_hash_find(bs_table
[j
],
5332 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5333 if (sector_num
>= 0)
5338 goto normal_compress
;
5341 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
5342 ram_compress_buf(s
, buf
, 10);
5348 ram_compress_buf(s
, buf
, 1);
5349 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5352 ram_compress_close(s
);
5355 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
5357 RamDecompressState s1
, *s
= &s1
;
5361 if (version_id
== 1)
5362 return ram_load_v1(f
, opaque
);
5363 if (version_id
!= 2)
5365 if (qemu_get_be32(f
) != phys_ram_size
)
5367 if (ram_decompress_open(s
, f
) < 0)
5369 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5370 if (ram_decompress_buf(s
, buf
, 1) < 0) {
5371 fprintf(stderr
, "Error while reading ram block header\n");
5375 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
5376 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
5385 ram_decompress_buf(s
, buf
+ 1, 9);
5387 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
5388 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
5389 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
5392 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
5393 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
5394 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
5395 bs_index
, sector_num
);
5402 printf("Error block header\n");
5406 ram_decompress_close(s
);
5410 /***********************************************************/
5411 /* bottom halves (can be seen as timers which expire ASAP) */
5420 static QEMUBH
*first_bh
= NULL
;
5422 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
5425 bh
= qemu_mallocz(sizeof(QEMUBH
));
5429 bh
->opaque
= opaque
;
5433 int qemu_bh_poll(void)
5452 void qemu_bh_schedule(QEMUBH
*bh
)
5454 CPUState
*env
= cpu_single_env
;
5458 bh
->next
= first_bh
;
5461 /* stop the currently executing CPU to execute the BH ASAP */
5463 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
5467 void qemu_bh_cancel(QEMUBH
*bh
)
5470 if (bh
->scheduled
) {
5473 pbh
= &(*pbh
)->next
;
5479 void qemu_bh_delete(QEMUBH
*bh
)
5485 /***********************************************************/
5486 /* machine registration */
5488 QEMUMachine
*first_machine
= NULL
;
5490 int qemu_register_machine(QEMUMachine
*m
)
5493 pm
= &first_machine
;
5501 QEMUMachine
*find_machine(const char *name
)
5505 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5506 if (!strcmp(m
->name
, name
))
5512 /***********************************************************/
5513 /* main execution loop */
5515 void gui_update(void *opaque
)
5517 display_state
.dpy_refresh(&display_state
);
5518 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
5521 struct vm_change_state_entry
{
5522 VMChangeStateHandler
*cb
;
5524 LIST_ENTRY (vm_change_state_entry
) entries
;
5527 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
5529 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
5532 VMChangeStateEntry
*e
;
5534 e
= qemu_mallocz(sizeof (*e
));
5540 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
5544 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
5546 LIST_REMOVE (e
, entries
);
5550 static void vm_state_notify(int running
)
5552 VMChangeStateEntry
*e
;
5554 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
5555 e
->cb(e
->opaque
, running
);
5559 /* XXX: support several handlers */
5560 static VMStopHandler
*vm_stop_cb
;
5561 static void *vm_stop_opaque
;
5563 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
5566 vm_stop_opaque
= opaque
;
5570 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
5584 void vm_stop(int reason
)
5587 cpu_disable_ticks();
5591 vm_stop_cb(vm_stop_opaque
, reason
);
5598 /* reset/shutdown handler */
5600 typedef struct QEMUResetEntry
{
5601 QEMUResetHandler
*func
;
5603 struct QEMUResetEntry
*next
;
5606 static QEMUResetEntry
*first_reset_entry
;
5607 static int reset_requested
;
5608 static int shutdown_requested
;
5609 static int powerdown_requested
;
5611 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
5613 QEMUResetEntry
**pre
, *re
;
5615 pre
= &first_reset_entry
;
5616 while (*pre
!= NULL
)
5617 pre
= &(*pre
)->next
;
5618 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
5620 re
->opaque
= opaque
;
5625 void qemu_system_reset(void)
5629 /* reset all devices */
5630 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
5631 re
->func(re
->opaque
);
5635 void qemu_system_reset_request(void)
5638 shutdown_requested
= 1;
5640 reset_requested
= 1;
5643 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5646 void qemu_system_shutdown_request(void)
5648 shutdown_requested
= 1;
5650 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5653 void qemu_system_powerdown_request(void)
5655 powerdown_requested
= 1;
5657 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5660 void main_loop_wait(int timeout
)
5662 IOHandlerRecord
*ioh
, *ioh_next
;
5663 fd_set rfds
, wfds
, xfds
;
5669 /* XXX: need to suppress polling by better using win32 events */
5671 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
5672 ret
|= pe
->func(pe
->opaque
);
5675 if (ret
== 0 && timeout
> 0) {
5677 WaitObjects
*w
= &wait_objects
;
5679 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
5680 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
5681 if (w
->func
[ret
- WAIT_OBJECT_0
])
5682 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
5683 } else if (ret
== WAIT_TIMEOUT
) {
5685 err
= GetLastError();
5686 fprintf(stderr
, "Wait error %d %d\n", ret
, err
);
5690 /* poll any events */
5691 /* XXX: separate device handlers from system ones */
5696 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
5698 (!ioh
->fd_read_poll
||
5699 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
5700 FD_SET(ioh
->fd
, &rfds
);
5704 if (ioh
->fd_write
) {
5705 FD_SET(ioh
->fd
, &wfds
);
5715 tv
.tv_usec
= timeout
* 1000;
5717 #if defined(CONFIG_SLIRP)
5719 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
5722 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
5724 /* XXX: better handling of removal */
5725 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
5726 ioh_next
= ioh
->next
;
5727 if (FD_ISSET(ioh
->fd
, &rfds
)) {
5728 ioh
->fd_read(ioh
->opaque
);
5730 if (FD_ISSET(ioh
->fd
, &wfds
)) {
5731 ioh
->fd_write(ioh
->opaque
);
5735 #if defined(CONFIG_SLIRP)
5742 slirp_select_poll(&rfds
, &wfds
, &xfds
);
5749 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
5750 qemu_get_clock(vm_clock
));
5751 /* run dma transfers, if any */
5755 /* real time timers */
5756 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
5757 qemu_get_clock(rt_clock
));
5760 static CPUState
*cur_cpu
;
5765 #ifdef CONFIG_PROFILER
5770 cur_cpu
= first_cpu
;
5777 env
= env
->next_cpu
;
5780 #ifdef CONFIG_PROFILER
5781 ti
= profile_getclock();
5783 ret
= cpu_exec(env
);
5784 #ifdef CONFIG_PROFILER
5785 qemu_time
+= profile_getclock() - ti
;
5787 if (ret
!= EXCP_HALTED
)
5789 /* all CPUs are halted ? */
5790 if (env
== cur_cpu
) {
5797 if (shutdown_requested
) {
5798 ret
= EXCP_INTERRUPT
;
5801 if (reset_requested
) {
5802 reset_requested
= 0;
5803 qemu_system_reset();
5804 ret
= EXCP_INTERRUPT
;
5806 if (powerdown_requested
) {
5807 powerdown_requested
= 0;
5808 qemu_system_powerdown();
5809 ret
= EXCP_INTERRUPT
;
5811 if (ret
== EXCP_DEBUG
) {
5812 vm_stop(EXCP_DEBUG
);
5814 /* if hlt instruction, we wait until the next IRQ */
5815 /* XXX: use timeout computed from timers */
5816 if (ret
== EXCP_HLT
)
5823 #ifdef CONFIG_PROFILER
5824 ti
= profile_getclock();
5826 main_loop_wait(timeout
);
5827 #ifdef CONFIG_PROFILER
5828 dev_time
+= profile_getclock() - ti
;
5831 cpu_disable_ticks();
5837 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2006 Fabrice Bellard\n"
5838 "usage: %s [options] [disk_image]\n"
5840 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
5842 "Standard options:\n"
5843 "-M machine select emulated machine (-M ? for list)\n"
5844 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
5845 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
5846 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
5847 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
5848 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
5849 "-snapshot write to temporary files instead of disk image files\n"
5851 "-no-quit disable SDL window close capability\n"
5854 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
5856 "-m megs set virtual RAM size to megs MB [default=%d]\n"
5857 "-smp n set the number of CPUs to 'n' [default=1]\n"
5858 "-nographic disable graphical output and redirect serial I/Os to console\n"
5860 "-k language use keyboard layout (for example \"fr\" for French)\n"
5863 "-audio-help print list of audio drivers and their options\n"
5864 "-soundhw c1,... enable audio support\n"
5865 " and only specified sound cards (comma separated list)\n"
5866 " use -soundhw ? to get the list of supported cards\n"
5867 " use -soundhw all to enable all of them\n"
5869 "-localtime set the real time clock to local time [default=utc]\n"
5870 "-full-screen start in full screen\n"
5872 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
5874 "-usb enable the USB driver (will be the default soon)\n"
5875 "-usbdevice name add the host or guest USB device 'name'\n"
5876 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5877 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
5880 "Network options:\n"
5881 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
5882 " create a new Network Interface Card and connect it to VLAN 'n'\n"
5884 "-net user[,vlan=n][,hostname=host]\n"
5885 " connect the user mode network stack to VLAN 'n' and send\n"
5886 " hostname 'host' to DHCP clients\n"
5889 "-net tap[,vlan=n],ifname=name\n"
5890 " connect the host TAP network interface to VLAN 'n'\n"
5892 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
5893 " connect the host TAP network interface to VLAN 'n' and use\n"
5894 " the network script 'file' (default=%s);\n"
5895 " use 'fd=h' to connect to an already opened TAP interface\n"
5897 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
5898 " connect the vlan 'n' to another VLAN using a socket connection\n"
5899 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
5900 " connect the vlan 'n' to multicast maddr and port\n"
5901 "-net none use it alone to have zero network devices; if no -net option\n"
5902 " is provided, the default is '-net nic -net user'\n"
5905 "-tftp prefix allow tftp access to files starting with prefix [-net user]\n"
5907 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
5909 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
5910 " redirect TCP or UDP connections from host to guest [-net user]\n"
5913 "Linux boot specific:\n"
5914 "-kernel bzImage use 'bzImage' as kernel image\n"
5915 "-append cmdline use 'cmdline' as kernel command line\n"
5916 "-initrd file use 'file' as initial ram disk\n"
5918 "Debug/Expert options:\n"
5919 "-monitor dev redirect the monitor to char device 'dev'\n"
5920 "-serial dev redirect the serial port to char device 'dev'\n"
5921 "-parallel dev redirect the parallel port to char device 'dev'\n"
5922 "-pidfile file Write PID to 'file'\n"
5923 "-S freeze CPU at startup (use 'c' to start execution)\n"
5924 "-s wait gdb connection to port %d\n"
5925 "-p port change gdb connection port\n"
5926 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
5927 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
5928 " translation (t=none or lba) (usually qemu can guess them)\n"
5929 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
5931 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
5932 "-no-kqemu disable KQEMU kernel module usage\n"
5934 #ifdef USE_CODE_COPY
5935 "-no-code-copy disable code copy acceleration\n"
5938 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
5939 " (default is CL-GD5446 PCI VGA)\n"
5940 "-no-acpi disable ACPI\n"
5942 "-no-reboot exit instead of rebooting\n"
5943 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
5944 "-vnc display start a VNC server on display\n"
5946 "During emulation, the following keys are useful:\n"
5947 "ctrl-alt-f toggle full screen\n"
5948 "ctrl-alt-n switch to virtual console 'n'\n"
5949 "ctrl-alt toggle mouse and keyboard grab\n"
5951 "When using -nographic, press 'ctrl-a h' to get some help.\n"
5956 DEFAULT_NETWORK_SCRIPT
,
5958 DEFAULT_GDBSTUB_PORT
,
5963 #define HAS_ARG 0x0001
5977 QEMU_OPTION_snapshot
,
5979 QEMU_OPTION_no_fd_bootchk
,
5982 QEMU_OPTION_nographic
,
5984 QEMU_OPTION_audio_help
,
5985 QEMU_OPTION_soundhw
,
6003 QEMU_OPTION_no_code_copy
,
6005 QEMU_OPTION_localtime
,
6006 QEMU_OPTION_cirrusvga
,
6008 QEMU_OPTION_std_vga
,
6009 QEMU_OPTION_monitor
,
6011 QEMU_OPTION_parallel
,
6013 QEMU_OPTION_full_screen
,
6014 QEMU_OPTION_no_quit
,
6015 QEMU_OPTION_pidfile
,
6016 QEMU_OPTION_no_kqemu
,
6017 QEMU_OPTION_kernel_kqemu
,
6018 QEMU_OPTION_win2k_hack
,
6020 QEMU_OPTION_usbdevice
,
6023 QEMU_OPTION_no_acpi
,
6024 QEMU_OPTION_no_reboot
,
6027 typedef struct QEMUOption
{
6033 const QEMUOption qemu_options
[] = {
6034 { "h", 0, QEMU_OPTION_h
},
6036 { "M", HAS_ARG
, QEMU_OPTION_M
},
6037 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
6038 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
6039 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
6040 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
6041 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
6042 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
6043 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
6044 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
6045 { "snapshot", 0, QEMU_OPTION_snapshot
},
6047 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
6049 { "m", HAS_ARG
, QEMU_OPTION_m
},
6050 { "nographic", 0, QEMU_OPTION_nographic
},
6051 { "k", HAS_ARG
, QEMU_OPTION_k
},
6053 { "audio-help", 0, QEMU_OPTION_audio_help
},
6054 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
6057 { "net", HAS_ARG
, QEMU_OPTION_net
},
6059 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
6061 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
6063 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
6066 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
6067 { "append", HAS_ARG
, QEMU_OPTION_append
},
6068 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
6070 { "S", 0, QEMU_OPTION_S
},
6071 { "s", 0, QEMU_OPTION_s
},
6072 { "p", HAS_ARG
, QEMU_OPTION_p
},
6073 { "d", HAS_ARG
, QEMU_OPTION_d
},
6074 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
6075 { "L", HAS_ARG
, QEMU_OPTION_L
},
6076 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
6078 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
6079 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
6081 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6082 { "g", 1, QEMU_OPTION_g
},
6084 { "localtime", 0, QEMU_OPTION_localtime
},
6085 { "std-vga", 0, QEMU_OPTION_std_vga
},
6086 { "monitor", 1, QEMU_OPTION_monitor
},
6087 { "serial", 1, QEMU_OPTION_serial
},
6088 { "parallel", 1, QEMU_OPTION_parallel
},
6089 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
6090 { "full-screen", 0, QEMU_OPTION_full_screen
},
6092 { "no-quit", 0, QEMU_OPTION_no_quit
},
6094 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
6095 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
6096 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
6097 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
6098 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
6100 /* temporary options */
6101 { "usb", 0, QEMU_OPTION_usb
},
6102 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
6103 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
6104 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
6108 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
6110 /* this stack is only used during signal handling */
6111 #define SIGNAL_STACK_SIZE 32768
6113 static uint8_t *signal_stack
;
6117 /* password input */
6119 static BlockDriverState
*get_bdrv(int index
)
6121 BlockDriverState
*bs
;
6124 bs
= bs_table
[index
];
6125 } else if (index
< 6) {
6126 bs
= fd_table
[index
- 4];
6133 static void read_passwords(void)
6135 BlockDriverState
*bs
;
6139 for(i
= 0; i
< 6; i
++) {
6141 if (bs
&& bdrv_is_encrypted(bs
)) {
6142 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
6143 for(j
= 0; j
< 3; j
++) {
6144 monitor_readline("Password: ",
6145 1, password
, sizeof(password
));
6146 if (bdrv_set_key(bs
, password
) == 0)
6148 term_printf("invalid password\n");
6154 /* XXX: currently we cannot use simultaneously different CPUs */
6155 void register_machines(void)
6157 #if defined(TARGET_I386)
6158 qemu_register_machine(&pc_machine
);
6159 qemu_register_machine(&isapc_machine
);
6160 #elif defined(TARGET_PPC)
6161 qemu_register_machine(&heathrow_machine
);
6162 qemu_register_machine(&core99_machine
);
6163 qemu_register_machine(&prep_machine
);
6164 #elif defined(TARGET_MIPS)
6165 qemu_register_machine(&mips_machine
);
6166 #elif defined(TARGET_SPARC)
6167 #ifdef TARGET_SPARC64
6168 qemu_register_machine(&sun4u_machine
);
6170 qemu_register_machine(&sun4m_machine
);
6172 #elif defined(TARGET_ARM)
6173 qemu_register_machine(&integratorcp926_machine
);
6174 qemu_register_machine(&integratorcp1026_machine
);
6175 qemu_register_machine(&versatilepb_machine
);
6176 qemu_register_machine(&versatileab_machine
);
6177 qemu_register_machine(&realview_machine
);
6178 #elif defined(TARGET_SH4)
6179 qemu_register_machine(&shix_machine
);
6181 #error unsupported CPU
6186 struct soundhw soundhw
[] = {
6193 { .init_isa
= pcspk_audio_init
}
6198 "Creative Sound Blaster 16",
6201 { .init_isa
= SB16_init
}
6208 "Yamaha YMF262 (OPL3)",
6210 "Yamaha YM3812 (OPL2)",
6214 { .init_isa
= Adlib_init
}
6221 "Gravis Ultrasound GF1",
6224 { .init_isa
= GUS_init
}
6230 "ENSONIQ AudioPCI ES1370",
6233 { .init_pci
= es1370_init
}
6236 { NULL
, NULL
, 0, 0, { NULL
} }
6239 static void select_soundhw (const char *optarg
)
6243 if (*optarg
== '?') {
6246 printf ("Valid sound card names (comma separated):\n");
6247 for (c
= soundhw
; c
->name
; ++c
) {
6248 printf ("%-11s %s\n", c
->name
, c
->descr
);
6250 printf ("\n-soundhw all will enable all of the above\n");
6251 exit (*optarg
!= '?');
6259 if (!strcmp (optarg
, "all")) {
6260 for (c
= soundhw
; c
->name
; ++c
) {
6268 e
= strchr (p
, ',');
6269 l
= !e
? strlen (p
) : (size_t) (e
- p
);
6271 for (c
= soundhw
; c
->name
; ++c
) {
6272 if (!strncmp (c
->name
, p
, l
)) {
6281 "Unknown sound card name (too big to show)\n");
6284 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
6289 p
+= l
+ (e
!= NULL
);
6293 goto show_valid_cards
;
6299 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
6301 exit(STATUS_CONTROL_C_EXIT
);
6306 #define MAX_NET_CLIENTS 32
6308 int main(int argc
, char **argv
)
6310 #ifdef CONFIG_GDBSTUB
6311 int use_gdbstub
, gdbstub_port
;
6314 int snapshot
, linux_boot
;
6315 const char *initrd_filename
;
6316 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
6317 const char *kernel_filename
, *kernel_cmdline
;
6318 DisplayState
*ds
= &display_state
;
6319 int cyls
, heads
, secs
, translation
;
6320 int start_emulation
= 1;
6321 char net_clients
[MAX_NET_CLIENTS
][256];
6324 const char *r
, *optarg
;
6325 CharDriverState
*monitor_hd
;
6326 char monitor_device
[128];
6327 char serial_devices
[MAX_SERIAL_PORTS
][128];
6328 int serial_device_index
;
6329 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
6330 int parallel_device_index
;
6331 const char *loadvm
= NULL
;
6332 QEMUMachine
*machine
;
6333 char usb_devices
[MAX_USB_CMDLINE
][128];
6334 int usb_devices_index
;
6336 LIST_INIT (&vm_change_state_head
);
6339 struct sigaction act
;
6340 sigfillset(&act
.sa_mask
);
6342 act
.sa_handler
= SIG_IGN
;
6343 sigaction(SIGPIPE
, &act
, NULL
);
6346 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
6347 /* Note: cpu_interrupt() is currently not SMP safe, so we force
6348 QEMU to run on a single CPU */
6353 h
= GetCurrentProcess();
6354 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
6355 for(i
= 0; i
< 32; i
++) {
6356 if (mask
& (1 << i
))
6361 SetProcessAffinityMask(h
, mask
);
6367 register_machines();
6368 machine
= first_machine
;
6369 initrd_filename
= NULL
;
6370 for(i
= 0; i
< MAX_FD
; i
++)
6371 fd_filename
[i
] = NULL
;
6372 for(i
= 0; i
< MAX_DISKS
; i
++)
6373 hd_filename
[i
] = NULL
;
6374 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
6375 vga_ram_size
= VGA_RAM_SIZE
;
6376 bios_size
= BIOS_SIZE
;
6377 #ifdef CONFIG_GDBSTUB
6379 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
6383 kernel_filename
= NULL
;
6384 kernel_cmdline
= "";
6390 cyls
= heads
= secs
= 0;
6391 translation
= BIOS_ATA_TRANSLATION_AUTO
;
6392 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
6394 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
6395 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
6396 serial_devices
[i
][0] = '\0';
6397 serial_device_index
= 0;
6399 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
6400 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
6401 parallel_devices
[i
][0] = '\0';
6402 parallel_device_index
= 0;
6404 usb_devices_index
= 0;
6409 /* default mac address of the first network interface */
6417 hd_filename
[0] = argv
[optind
++];
6419 const QEMUOption
*popt
;
6422 popt
= qemu_options
;
6425 fprintf(stderr
, "%s: invalid option -- '%s'\n",
6429 if (!strcmp(popt
->name
, r
+ 1))
6433 if (popt
->flags
& HAS_ARG
) {
6434 if (optind
>= argc
) {
6435 fprintf(stderr
, "%s: option '%s' requires an argument\n",
6439 optarg
= argv
[optind
++];
6444 switch(popt
->index
) {
6446 machine
= find_machine(optarg
);
6449 printf("Supported machines are:\n");
6450 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6451 printf("%-10s %s%s\n",
6453 m
== first_machine
? " (default)" : "");
6458 case QEMU_OPTION_initrd
:
6459 initrd_filename
= optarg
;
6461 case QEMU_OPTION_hda
:
6462 case QEMU_OPTION_hdb
:
6463 case QEMU_OPTION_hdc
:
6464 case QEMU_OPTION_hdd
:
6467 hd_index
= popt
->index
- QEMU_OPTION_hda
;
6468 hd_filename
[hd_index
] = optarg
;
6469 if (hd_index
== cdrom_index
)
6473 case QEMU_OPTION_snapshot
:
6476 case QEMU_OPTION_hdachs
:
6480 cyls
= strtol(p
, (char **)&p
, 0);
6481 if (cyls
< 1 || cyls
> 16383)
6486 heads
= strtol(p
, (char **)&p
, 0);
6487 if (heads
< 1 || heads
> 16)
6492 secs
= strtol(p
, (char **)&p
, 0);
6493 if (secs
< 1 || secs
> 63)
6497 if (!strcmp(p
, "none"))
6498 translation
= BIOS_ATA_TRANSLATION_NONE
;
6499 else if (!strcmp(p
, "lba"))
6500 translation
= BIOS_ATA_TRANSLATION_LBA
;
6501 else if (!strcmp(p
, "auto"))
6502 translation
= BIOS_ATA_TRANSLATION_AUTO
;
6505 } else if (*p
!= '\0') {
6507 fprintf(stderr
, "qemu: invalid physical CHS format\n");
6512 case QEMU_OPTION_nographic
:
6513 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
6514 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
6517 case QEMU_OPTION_kernel
:
6518 kernel_filename
= optarg
;
6520 case QEMU_OPTION_append
:
6521 kernel_cmdline
= optarg
;
6523 case QEMU_OPTION_cdrom
:
6524 if (cdrom_index
>= 0) {
6525 hd_filename
[cdrom_index
] = optarg
;
6528 case QEMU_OPTION_boot
:
6529 boot_device
= optarg
[0];
6530 if (boot_device
!= 'a' &&
6533 boot_device
!= 'n' &&
6535 boot_device
!= 'c' && boot_device
!= 'd') {
6536 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
6540 case QEMU_OPTION_fda
:
6541 fd_filename
[0] = optarg
;
6543 case QEMU_OPTION_fdb
:
6544 fd_filename
[1] = optarg
;
6547 case QEMU_OPTION_no_fd_bootchk
:
6551 case QEMU_OPTION_no_code_copy
:
6552 code_copy_enabled
= 0;
6554 case QEMU_OPTION_net
:
6555 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
6556 fprintf(stderr
, "qemu: too many network clients\n");
6559 pstrcpy(net_clients
[nb_net_clients
],
6560 sizeof(net_clients
[0]),
6565 case QEMU_OPTION_tftp
:
6566 tftp_prefix
= optarg
;
6569 case QEMU_OPTION_smb
:
6570 net_slirp_smb(optarg
);
6573 case QEMU_OPTION_redir
:
6574 net_slirp_redir(optarg
);
6578 case QEMU_OPTION_audio_help
:
6582 case QEMU_OPTION_soundhw
:
6583 select_soundhw (optarg
);
6590 ram_size
= atoi(optarg
) * 1024 * 1024;
6593 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
6594 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
6595 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
6604 mask
= cpu_str_to_log_mask(optarg
);
6606 printf("Log items (comma separated):\n");
6607 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
6608 printf("%-10s %s\n", item
->name
, item
->help
);
6615 #ifdef CONFIG_GDBSTUB
6620 gdbstub_port
= atoi(optarg
);
6627 start_emulation
= 0;
6630 keyboard_layout
= optarg
;
6632 case QEMU_OPTION_localtime
:
6635 case QEMU_OPTION_cirrusvga
:
6636 cirrus_vga_enabled
= 1;
6638 case QEMU_OPTION_std_vga
:
6639 cirrus_vga_enabled
= 0;
6646 w
= strtol(p
, (char **)&p
, 10);
6649 fprintf(stderr
, "qemu: invalid resolution or depth\n");
6655 h
= strtol(p
, (char **)&p
, 10);
6660 depth
= strtol(p
, (char **)&p
, 10);
6661 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
6662 depth
!= 24 && depth
!= 32)
6664 } else if (*p
== '\0') {
6665 depth
= graphic_depth
;
6672 graphic_depth
= depth
;
6675 case QEMU_OPTION_monitor
:
6676 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
6678 case QEMU_OPTION_serial
:
6679 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
6680 fprintf(stderr
, "qemu: too many serial ports\n");
6683 pstrcpy(serial_devices
[serial_device_index
],
6684 sizeof(serial_devices
[0]), optarg
);
6685 serial_device_index
++;
6687 case QEMU_OPTION_parallel
:
6688 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
6689 fprintf(stderr
, "qemu: too many parallel ports\n");
6692 pstrcpy(parallel_devices
[parallel_device_index
],
6693 sizeof(parallel_devices
[0]), optarg
);
6694 parallel_device_index
++;
6696 case QEMU_OPTION_loadvm
:
6699 case QEMU_OPTION_full_screen
:
6703 case QEMU_OPTION_no_quit
:
6707 case QEMU_OPTION_pidfile
:
6708 create_pidfile(optarg
);
6711 case QEMU_OPTION_win2k_hack
:
6712 win2k_install_hack
= 1;
6716 case QEMU_OPTION_no_kqemu
:
6719 case QEMU_OPTION_kernel_kqemu
:
6723 case QEMU_OPTION_usb
:
6726 case QEMU_OPTION_usbdevice
:
6728 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
6729 fprintf(stderr
, "Too many USB devices\n");
6732 pstrcpy(usb_devices
[usb_devices_index
],
6733 sizeof(usb_devices
[usb_devices_index
]),
6735 usb_devices_index
++;
6737 case QEMU_OPTION_smp
:
6738 smp_cpus
= atoi(optarg
);
6739 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
6740 fprintf(stderr
, "Invalid number of CPUs\n");
6744 case QEMU_OPTION_vnc
:
6745 vnc_display
= atoi(optarg
);
6746 if (vnc_display
< 0) {
6747 fprintf(stderr
, "Invalid VNC display\n");
6751 case QEMU_OPTION_no_acpi
:
6754 case QEMU_OPTION_no_reboot
:
6765 linux_boot
= (kernel_filename
!= NULL
);
6768 hd_filename
[0] == '\0' &&
6769 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
6770 fd_filename
[0] == '\0')
6773 /* boot to cd by default if no hard disk */
6774 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
6775 if (fd_filename
[0] != '\0')
6781 setvbuf(stdout
, NULL
, _IOLBF
, 0);
6791 /* init network clients */
6792 if (nb_net_clients
== 0) {
6793 /* if no clients, we use a default config */
6794 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
6796 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
6801 for(i
= 0;i
< nb_net_clients
; i
++) {
6802 if (net_client_init(net_clients
[i
]) < 0)
6806 /* init the memory */
6807 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
6809 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
6810 if (!phys_ram_base
) {
6811 fprintf(stderr
, "Could not allocate physical memory\n");
6815 /* we always create the cdrom drive, even if no disk is there */
6817 if (cdrom_index
>= 0) {
6818 bs_table
[cdrom_index
] = bdrv_new("cdrom");
6819 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
6822 /* open the virtual block devices */
6823 for(i
= 0; i
< MAX_DISKS
; i
++) {
6824 if (hd_filename
[i
]) {
6827 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
6828 bs_table
[i
] = bdrv_new(buf
);
6830 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
6831 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
6835 if (i
== 0 && cyls
!= 0) {
6836 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
6837 bdrv_set_translation_hint(bs_table
[i
], translation
);
6842 /* we always create at least one floppy disk */
6843 fd_table
[0] = bdrv_new("fda");
6844 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
6846 for(i
= 0; i
< MAX_FD
; i
++) {
6847 if (fd_filename
[i
]) {
6850 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
6851 fd_table
[i
] = bdrv_new(buf
);
6852 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
6854 if (fd_filename
[i
] != '\0') {
6855 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
6856 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
6857 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
6865 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
6866 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
6872 dumb_display_init(ds
);
6873 } else if (vnc_display
!= -1) {
6874 vnc_display_init(ds
, vnc_display
);
6876 #if defined(CONFIG_SDL)
6877 sdl_display_init(ds
, full_screen
);
6878 #elif defined(CONFIG_COCOA)
6879 cocoa_display_init(ds
, full_screen
);
6881 dumb_display_init(ds
);
6885 monitor_hd
= qemu_chr_open(monitor_device
);
6887 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
6890 monitor_init(monitor_hd
, !nographic
);
6892 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6893 const char *devname
= serial_devices
[i
];
6894 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
6895 serial_hds
[i
] = qemu_chr_open(devname
);
6896 if (!serial_hds
[i
]) {
6897 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
6901 if (!strcmp(devname
, "vc"))
6902 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6906 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6907 const char *devname
= parallel_devices
[i
];
6908 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
6909 parallel_hds
[i
] = qemu_chr_open(devname
);
6910 if (!parallel_hds
[i
]) {
6911 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
6915 if (!strcmp(devname
, "vc"))
6916 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6920 machine
->init(ram_size
, vga_ram_size
, boot_device
,
6921 ds
, fd_filename
, snapshot
,
6922 kernel_filename
, kernel_cmdline
, initrd_filename
);
6924 /* init USB devices */
6926 for(i
= 0; i
< usb_devices_index
; i
++) {
6927 if (usb_device_add(usb_devices
[i
]) < 0) {
6928 fprintf(stderr
, "Warning: could not add USB device %s\n",
6934 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
6935 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
6937 #ifdef CONFIG_GDBSTUB
6939 if (gdbserver_start(gdbstub_port
) < 0) {
6940 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
6944 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
6952 /* XXX: simplify init */
6954 if (start_emulation
) {