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;
146 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
147 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
149 int win2k_install_hack
= 0;
152 static VLANState
*first_vlan
;
154 int vnc_display
= -1;
155 #if defined(TARGET_SPARC)
157 #elif defined(TARGET_I386)
162 int acpi_enabled
= 1;
166 /***********************************************************/
167 /* x86 ISA bus support */
169 target_phys_addr_t isa_mem_base
= 0;
172 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
174 #ifdef DEBUG_UNUSED_IOPORT
175 fprintf(stderr
, "inb: port=0x%04x\n", address
);
180 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
182 #ifdef DEBUG_UNUSED_IOPORT
183 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
187 /* default is to make two byte accesses */
188 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
191 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
192 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
193 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
197 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
199 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
200 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
201 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
204 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
206 #ifdef DEBUG_UNUSED_IOPORT
207 fprintf(stderr
, "inl: port=0x%04x\n", address
);
212 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
214 #ifdef DEBUG_UNUSED_IOPORT
215 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
219 void init_ioports(void)
223 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
224 ioport_read_table
[0][i
] = default_ioport_readb
;
225 ioport_write_table
[0][i
] = default_ioport_writeb
;
226 ioport_read_table
[1][i
] = default_ioport_readw
;
227 ioport_write_table
[1][i
] = default_ioport_writew
;
228 ioport_read_table
[2][i
] = default_ioport_readl
;
229 ioport_write_table
[2][i
] = default_ioport_writel
;
233 /* size is the word size in byte */
234 int register_ioport_read(int start
, int length
, int size
,
235 IOPortReadFunc
*func
, void *opaque
)
241 } else if (size
== 2) {
243 } else if (size
== 4) {
246 hw_error("register_ioport_read: invalid size");
249 for(i
= start
; i
< start
+ length
; i
+= size
) {
250 ioport_read_table
[bsize
][i
] = func
;
251 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
252 hw_error("register_ioport_read: invalid opaque");
253 ioport_opaque
[i
] = opaque
;
258 /* size is the word size in byte */
259 int register_ioport_write(int start
, int length
, int size
,
260 IOPortWriteFunc
*func
, void *opaque
)
266 } else if (size
== 2) {
268 } else if (size
== 4) {
271 hw_error("register_ioport_write: invalid size");
274 for(i
= start
; i
< start
+ length
; i
+= size
) {
275 ioport_write_table
[bsize
][i
] = func
;
276 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
277 hw_error("register_ioport_read: invalid opaque");
278 ioport_opaque
[i
] = opaque
;
283 void isa_unassign_ioport(int start
, int length
)
287 for(i
= start
; i
< start
+ length
; i
++) {
288 ioport_read_table
[0][i
] = default_ioport_readb
;
289 ioport_read_table
[1][i
] = default_ioport_readw
;
290 ioport_read_table
[2][i
] = default_ioport_readl
;
292 ioport_write_table
[0][i
] = default_ioport_writeb
;
293 ioport_write_table
[1][i
] = default_ioport_writew
;
294 ioport_write_table
[2][i
] = default_ioport_writel
;
298 /***********************************************************/
300 void pstrcpy(char *buf
, int buf_size
, const char *str
)
310 if (c
== 0 || q
>= buf
+ buf_size
- 1)
317 /* strcat and truncate. */
318 char *pstrcat(char *buf
, int buf_size
, const char *s
)
323 pstrcpy(buf
+ len
, buf_size
- len
, s
);
327 int strstart(const char *str
, const char *val
, const char **ptr
)
343 void cpu_outb(CPUState
*env
, int addr
, int val
)
346 if (loglevel
& CPU_LOG_IOPORT
)
347 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
349 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
352 env
->last_io_time
= cpu_get_time_fast();
356 void cpu_outw(CPUState
*env
, int addr
, int val
)
359 if (loglevel
& CPU_LOG_IOPORT
)
360 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
362 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
365 env
->last_io_time
= cpu_get_time_fast();
369 void cpu_outl(CPUState
*env
, int addr
, int val
)
372 if (loglevel
& CPU_LOG_IOPORT
)
373 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
375 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
378 env
->last_io_time
= cpu_get_time_fast();
382 int cpu_inb(CPUState
*env
, int addr
)
385 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
387 if (loglevel
& CPU_LOG_IOPORT
)
388 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
392 env
->last_io_time
= cpu_get_time_fast();
397 int cpu_inw(CPUState
*env
, int addr
)
400 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
402 if (loglevel
& CPU_LOG_IOPORT
)
403 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
407 env
->last_io_time
= cpu_get_time_fast();
412 int cpu_inl(CPUState
*env
, int addr
)
415 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
417 if (loglevel
& CPU_LOG_IOPORT
)
418 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
422 env
->last_io_time
= cpu_get_time_fast();
427 /***********************************************************/
428 void hw_error(const char *fmt
, ...)
434 fprintf(stderr
, "qemu: hardware error: ");
435 vfprintf(stderr
, fmt
, ap
);
436 fprintf(stderr
, "\n");
437 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
438 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
440 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
442 cpu_dump_state(env
, stderr
, fprintf
, 0);
449 /***********************************************************/
452 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
453 static void *qemu_put_kbd_event_opaque
;
454 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
455 static void *qemu_put_mouse_event_opaque
;
456 static int qemu_put_mouse_event_absolute
;
458 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
460 qemu_put_kbd_event_opaque
= opaque
;
461 qemu_put_kbd_event
= func
;
464 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
, int absolute
)
466 qemu_put_mouse_event_opaque
= opaque
;
467 qemu_put_mouse_event
= func
;
468 qemu_put_mouse_event_absolute
= absolute
;
471 void kbd_put_keycode(int keycode
)
473 if (qemu_put_kbd_event
) {
474 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
478 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
480 if (qemu_put_mouse_event
) {
481 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
482 dx
, dy
, dz
, buttons_state
);
486 int kbd_mouse_is_absolute(void)
488 return qemu_put_mouse_event_absolute
;
491 /* compute with 96 bit intermediate result: (a*b)/c */
492 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
497 #ifdef WORDS_BIGENDIAN
507 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
508 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
511 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
515 /***********************************************************/
516 /* real time host monotonic timer */
518 #define QEMU_TIMER_BASE 1000000000LL
522 static int64_t clock_freq
;
524 static void init_get_clock(void)
528 ret
= QueryPerformanceFrequency(&freq
);
530 fprintf(stderr
, "Could not calibrate ticks\n");
533 clock_freq
= freq
.QuadPart
;
536 static int64_t get_clock(void)
539 QueryPerformanceCounter(&ti
);
540 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
545 static int use_rt_clock
;
547 static void init_get_clock(void)
550 #if defined(__linux__)
553 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
560 static int64_t get_clock(void)
562 #if defined(__linux__)
565 clock_gettime(CLOCK_MONOTONIC
, &ts
);
566 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
570 /* XXX: using gettimeofday leads to problems if the date
571 changes, so it should be avoided. */
573 gettimeofday(&tv
, NULL
);
574 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
580 /***********************************************************/
581 /* guest cycle counter */
583 static int64_t cpu_ticks_prev
;
584 static int64_t cpu_ticks_offset
;
585 static int64_t cpu_clock_offset
;
586 static int cpu_ticks_enabled
;
588 /* return the host CPU cycle counter and handle stop/restart */
589 int64_t cpu_get_ticks(void)
591 if (!cpu_ticks_enabled
) {
592 return cpu_ticks_offset
;
595 ticks
= cpu_get_real_ticks();
596 if (cpu_ticks_prev
> ticks
) {
597 /* Note: non increasing ticks may happen if the host uses
599 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
601 cpu_ticks_prev
= ticks
;
602 return ticks
+ cpu_ticks_offset
;
606 /* return the host CPU monotonic timer and handle stop/restart */
607 static int64_t cpu_get_clock(void)
610 if (!cpu_ticks_enabled
) {
611 return cpu_clock_offset
;
614 return ti
+ cpu_clock_offset
;
618 /* enable cpu_get_ticks() */
619 void cpu_enable_ticks(void)
621 if (!cpu_ticks_enabled
) {
622 cpu_ticks_offset
-= cpu_get_real_ticks();
623 cpu_clock_offset
-= get_clock();
624 cpu_ticks_enabled
= 1;
628 /* disable cpu_get_ticks() : the clock is stopped. You must not call
629 cpu_get_ticks() after that. */
630 void cpu_disable_ticks(void)
632 if (cpu_ticks_enabled
) {
633 cpu_ticks_offset
= cpu_get_ticks();
634 cpu_clock_offset
= cpu_get_clock();
635 cpu_ticks_enabled
= 0;
639 /***********************************************************/
642 #define QEMU_TIMER_REALTIME 0
643 #define QEMU_TIMER_VIRTUAL 1
647 /* XXX: add frequency */
655 struct QEMUTimer
*next
;
661 static QEMUTimer
*active_timers
[2];
663 static MMRESULT timerID
;
664 static HANDLE host_alarm
= NULL
;
665 static unsigned int period
= 1;
667 /* frequency of the times() clock tick */
668 static int timer_freq
;
671 QEMUClock
*qemu_new_clock(int type
)
674 clock
= qemu_mallocz(sizeof(QEMUClock
));
681 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
685 ts
= qemu_mallocz(sizeof(QEMUTimer
));
692 void qemu_free_timer(QEMUTimer
*ts
)
697 /* stop a timer, but do not dealloc it */
698 void qemu_del_timer(QEMUTimer
*ts
)
702 /* NOTE: this code must be signal safe because
703 qemu_timer_expired() can be called from a signal. */
704 pt
= &active_timers
[ts
->clock
->type
];
717 /* modify the current timer so that it will be fired when current_time
718 >= expire_time. The corresponding callback will be called. */
719 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
725 /* add the timer in the sorted list */
726 /* NOTE: this code must be signal safe because
727 qemu_timer_expired() can be called from a signal. */
728 pt
= &active_timers
[ts
->clock
->type
];
733 if (t
->expire_time
> expire_time
)
737 ts
->expire_time
= expire_time
;
742 int qemu_timer_pending(QEMUTimer
*ts
)
745 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
752 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
756 return (timer_head
->expire_time
<= current_time
);
759 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
765 if (!ts
|| ts
->expire_time
> current_time
)
767 /* remove timer from the list before calling the callback */
768 *ptimer_head
= ts
->next
;
771 /* run the callback (the timer list can be modified) */
776 int64_t qemu_get_clock(QEMUClock
*clock
)
778 switch(clock
->type
) {
779 case QEMU_TIMER_REALTIME
:
780 return get_clock() / 1000000;
782 case QEMU_TIMER_VIRTUAL
:
783 return cpu_get_clock();
787 static void init_timers(void)
790 ticks_per_sec
= QEMU_TIMER_BASE
;
791 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
792 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
796 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
798 uint64_t expire_time
;
800 if (qemu_timer_pending(ts
)) {
801 expire_time
= ts
->expire_time
;
805 qemu_put_be64(f
, expire_time
);
808 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
810 uint64_t expire_time
;
812 expire_time
= qemu_get_be64(f
);
813 if (expire_time
!= -1) {
814 qemu_mod_timer(ts
, expire_time
);
820 static void timer_save(QEMUFile
*f
, void *opaque
)
822 if (cpu_ticks_enabled
) {
823 hw_error("cannot save state if virtual timers are running");
825 qemu_put_be64s(f
, &cpu_ticks_offset
);
826 qemu_put_be64s(f
, &ticks_per_sec
);
827 qemu_put_be64s(f
, &cpu_clock_offset
);
830 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
832 if (version_id
!= 1 && version_id
!= 2)
834 if (cpu_ticks_enabled
) {
837 qemu_get_be64s(f
, &cpu_ticks_offset
);
838 qemu_get_be64s(f
, &ticks_per_sec
);
839 if (version_id
== 2) {
840 qemu_get_be64s(f
, &cpu_clock_offset
);
846 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
847 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
849 static void host_alarm_handler(int host_signum
)
853 #define DISP_FREQ 1000
855 static int64_t delta_min
= INT64_MAX
;
856 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
858 ti
= qemu_get_clock(vm_clock
);
859 if (last_clock
!= 0) {
860 delta
= ti
- last_clock
;
861 if (delta
< delta_min
)
863 if (delta
> delta_max
)
866 if (++count
== DISP_FREQ
) {
867 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
868 muldiv64(delta_min
, 1000000, ticks_per_sec
),
869 muldiv64(delta_max
, 1000000, ticks_per_sec
),
870 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
871 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
873 delta_min
= INT64_MAX
;
881 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
882 qemu_get_clock(vm_clock
)) ||
883 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
884 qemu_get_clock(rt_clock
))) {
886 SetEvent(host_alarm
);
888 CPUState
*env
= cpu_single_env
;
890 /* stop the currently executing cpu because a timer occured */
891 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
893 if (env
->kqemu_enabled
) {
894 kqemu_cpu_interrupt(env
);
903 #if defined(__linux__)
905 #define RTC_FREQ 1024
909 static int start_rtc_timer(void)
911 rtc_fd
= open("/dev/rtc", O_RDONLY
);
914 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
915 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
916 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
917 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
920 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
925 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
931 static int start_rtc_timer(void)
936 #endif /* !defined(__linux__) */
938 #endif /* !defined(_WIN32) */
940 static void init_timer_alarm(void)
947 ZeroMemory(&tc
, sizeof(TIMECAPS
));
948 timeGetDevCaps(&tc
, sizeof(TIMECAPS
));
949 if (period
< tc
.wPeriodMin
)
950 period
= tc
.wPeriodMin
;
951 timeBeginPeriod(period
);
952 timerID
= timeSetEvent(1, // interval (ms)
953 period
, // resolution
954 host_alarm_handler
, // function
955 (DWORD
)&count
, // user parameter
956 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
958 perror("failed timer alarm");
961 host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
963 perror("failed CreateEvent");
966 qemu_add_wait_object(host_alarm
, NULL
, NULL
);
968 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
971 struct sigaction act
;
972 struct itimerval itv
;
974 /* get times() syscall frequency */
975 timer_freq
= sysconf(_SC_CLK_TCK
);
978 sigfillset(&act
.sa_mask
);
980 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
981 act
.sa_flags
|= SA_ONSTACK
;
983 act
.sa_handler
= host_alarm_handler
;
984 sigaction(SIGALRM
, &act
, NULL
);
986 itv
.it_interval
.tv_sec
= 0;
987 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
988 itv
.it_value
.tv_sec
= 0;
989 itv
.it_value
.tv_usec
= 10 * 1000;
990 setitimer(ITIMER_REAL
, &itv
, NULL
);
991 /* we probe the tick duration of the kernel to inform the user if
992 the emulated kernel requested a too high timer frequency */
993 getitimer(ITIMER_REAL
, &itv
);
995 #if defined(__linux__)
996 /* XXX: force /dev/rtc usage because even 2.6 kernels may not
997 have timers with 1 ms resolution. The correct solution will
998 be to use the POSIX real time timers available in recent
1000 if (itv
.it_interval
.tv_usec
> 1000 || 1) {
1001 /* try to use /dev/rtc to have a faster timer */
1002 if (start_rtc_timer() < 0)
1004 /* disable itimer */
1005 itv
.it_interval
.tv_sec
= 0;
1006 itv
.it_interval
.tv_usec
= 0;
1007 itv
.it_value
.tv_sec
= 0;
1008 itv
.it_value
.tv_usec
= 0;
1009 setitimer(ITIMER_REAL
, &itv
, NULL
);
1012 sigaction(SIGIO
, &act
, NULL
);
1013 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1014 fcntl(rtc_fd
, F_SETOWN
, getpid());
1016 #endif /* defined(__linux__) */
1019 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1020 PIT_FREQ
) / 1000000;
1026 void quit_timers(void)
1029 timeKillEvent(timerID
);
1030 timeEndPeriod(period
);
1032 CloseHandle(host_alarm
);
1038 /***********************************************************/
1039 /* character device */
1041 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1043 return s
->chr_write(s
, buf
, len
);
1046 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1050 return s
->chr_ioctl(s
, cmd
, arg
);
1053 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1058 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1059 qemu_chr_write(s
, buf
, strlen(buf
));
1063 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1065 if (s
->chr_send_event
)
1066 s
->chr_send_event(s
, event
);
1069 void qemu_chr_add_read_handler(CharDriverState
*s
,
1070 IOCanRWHandler
*fd_can_read
,
1071 IOReadHandler
*fd_read
, void *opaque
)
1073 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
1076 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
1078 s
->chr_event
= chr_event
;
1081 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1086 static void null_chr_add_read_handler(CharDriverState
*chr
,
1087 IOCanRWHandler
*fd_can_read
,
1088 IOReadHandler
*fd_read
, void *opaque
)
1092 CharDriverState
*qemu_chr_open_null(void)
1094 CharDriverState
*chr
;
1096 chr
= qemu_mallocz(sizeof(CharDriverState
));
1099 chr
->chr_write
= null_chr_write
;
1100 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1106 static void socket_cleanup(void)
1111 static int socket_init(void)
1116 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1118 err
= WSAGetLastError();
1119 fprintf(stderr
, "WSAStartup: %d\n", err
);
1122 atexit(socket_cleanup
);
1126 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1132 ret
= send(fd
, buf
, len
, 0);
1135 errno
= WSAGetLastError();
1136 if (errno
!= WSAEWOULDBLOCK
) {
1139 } else if (ret
== 0) {
1149 void socket_set_nonblock(int fd
)
1151 unsigned long opt
= 1;
1152 ioctlsocket(fd
, FIONBIO
, &opt
);
1157 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1163 ret
= write(fd
, buf
, len
);
1165 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1167 } else if (ret
== 0) {
1177 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1179 return unix_write(fd
, buf
, len1
);
1182 void socket_set_nonblock(int fd
)
1184 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1186 #endif /* !_WIN32 */
1192 IOCanRWHandler
*fd_can_read
;
1193 IOReadHandler
*fd_read
;
1198 #define STDIO_MAX_CLIENTS 2
1200 static int stdio_nb_clients
;
1201 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1203 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1205 FDCharDriver
*s
= chr
->opaque
;
1206 return unix_write(s
->fd_out
, buf
, len
);
1209 static int fd_chr_read_poll(void *opaque
)
1211 CharDriverState
*chr
= opaque
;
1212 FDCharDriver
*s
= chr
->opaque
;
1214 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
1218 static void fd_chr_read(void *opaque
)
1220 CharDriverState
*chr
= opaque
;
1221 FDCharDriver
*s
= chr
->opaque
;
1226 if (len
> s
->max_size
)
1230 size
= read(s
->fd_in
, buf
, len
);
1232 /* FD has been closed. Remove it from the active list. */
1233 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1237 s
->fd_read(s
->fd_opaque
, buf
, size
);
1241 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1242 IOCanRWHandler
*fd_can_read
,
1243 IOReadHandler
*fd_read
, void *opaque
)
1245 FDCharDriver
*s
= chr
->opaque
;
1247 if (s
->fd_in
>= 0) {
1248 s
->fd_can_read
= fd_can_read
;
1249 s
->fd_read
= fd_read
;
1250 s
->fd_opaque
= opaque
;
1251 if (nographic
&& s
->fd_in
== 0) {
1253 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1254 fd_chr_read
, NULL
, chr
);
1259 /* open a character device to a unix fd */
1260 CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1262 CharDriverState
*chr
;
1265 chr
= qemu_mallocz(sizeof(CharDriverState
));
1268 s
= qemu_mallocz(sizeof(FDCharDriver
));
1276 chr
->chr_write
= fd_chr_write
;
1277 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1281 CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1285 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1288 return qemu_chr_open_fd(-1, fd_out
);
1291 CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1295 fd
= open(filename
, O_RDWR
| O_BINARY
);
1298 return qemu_chr_open_fd(fd
, fd
);
1302 /* for STDIO, we handle the case where several clients use it
1305 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1307 #define TERM_FIFO_MAX_SIZE 1
1309 static int term_got_escape
, client_index
;
1310 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1311 static int term_fifo_size
;
1312 static int term_timestamps
;
1313 static int64_t term_timestamps_start
;
1315 void term_print_help(void)
1318 "C-a h print this help\n"
1319 "C-a x exit emulator\n"
1320 "C-a s save disk data back to file (if -snapshot)\n"
1321 "C-a b send break (magic sysrq)\n"
1322 "C-a t toggle console timestamps\n"
1323 "C-a c switch between console and monitor\n"
1324 "C-a C-a send C-a\n"
1328 /* called when a char is received */
1329 static void stdio_received_byte(int ch
)
1331 if (term_got_escape
) {
1332 term_got_escape
= 0;
1343 for (i
= 0; i
< MAX_DISKS
; i
++) {
1345 bdrv_commit(bs_table
[i
]);
1350 if (client_index
< stdio_nb_clients
) {
1351 CharDriverState
*chr
;
1354 chr
= stdio_clients
[client_index
];
1356 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1361 if (client_index
>= stdio_nb_clients
)
1363 if (client_index
== 0) {
1364 /* send a new line in the monitor to get the prompt */
1370 term_timestamps
= !term_timestamps
;
1371 term_timestamps_start
= -1;
1376 } else if (ch
== TERM_ESCAPE
) {
1377 term_got_escape
= 1;
1380 if (client_index
< stdio_nb_clients
) {
1382 CharDriverState
*chr
;
1385 chr
= stdio_clients
[client_index
];
1387 if (s
->fd_can_read(s
->fd_opaque
) > 0) {
1389 s
->fd_read(s
->fd_opaque
, buf
, 1);
1390 } else if (term_fifo_size
== 0) {
1391 term_fifo
[term_fifo_size
++] = ch
;
1397 static int stdio_read_poll(void *opaque
)
1399 CharDriverState
*chr
;
1402 if (client_index
< stdio_nb_clients
) {
1403 chr
= stdio_clients
[client_index
];
1405 /* try to flush the queue if needed */
1406 if (term_fifo_size
!= 0 && s
->fd_can_read(s
->fd_opaque
) > 0) {
1407 s
->fd_read(s
->fd_opaque
, term_fifo
, 1);
1410 /* see if we can absorb more chars */
1411 if (term_fifo_size
== 0)
1420 static void stdio_read(void *opaque
)
1425 size
= read(0, buf
, 1);
1427 /* stdin has been closed. Remove it from the active list. */
1428 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
1432 stdio_received_byte(buf
[0]);
1435 static int stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1437 FDCharDriver
*s
= chr
->opaque
;
1438 if (!term_timestamps
) {
1439 return unix_write(s
->fd_out
, buf
, len
);
1444 for(i
= 0; i
< len
; i
++) {
1445 unix_write(s
->fd_out
, buf
+ i
, 1);
1446 if (buf
[i
] == '\n') {
1451 if (term_timestamps_start
== -1)
1452 term_timestamps_start
= ti
;
1453 ti
-= term_timestamps_start
;
1454 secs
= ti
/ 1000000000;
1455 snprintf(buf1
, sizeof(buf1
),
1456 "[%02d:%02d:%02d.%03d] ",
1460 (int)((ti
/ 1000000) % 1000));
1461 unix_write(s
->fd_out
, buf1
, strlen(buf1
));
1468 /* init terminal so that we can grab keys */
1469 static struct termios oldtty
;
1470 static int old_fd0_flags
;
1472 static void term_exit(void)
1474 tcsetattr (0, TCSANOW
, &oldtty
);
1475 fcntl(0, F_SETFL
, old_fd0_flags
);
1478 static void term_init(void)
1482 tcgetattr (0, &tty
);
1484 old_fd0_flags
= fcntl(0, F_GETFL
);
1486 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1487 |INLCR
|IGNCR
|ICRNL
|IXON
);
1488 tty
.c_oflag
|= OPOST
;
1489 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1490 /* if graphical mode, we allow Ctrl-C handling */
1492 tty
.c_lflag
&= ~ISIG
;
1493 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1496 tty
.c_cc
[VTIME
] = 0;
1498 tcsetattr (0, TCSANOW
, &tty
);
1502 fcntl(0, F_SETFL
, O_NONBLOCK
);
1505 CharDriverState
*qemu_chr_open_stdio(void)
1507 CharDriverState
*chr
;
1510 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1512 chr
= qemu_chr_open_fd(0, 1);
1513 chr
->chr_write
= stdio_write
;
1514 if (stdio_nb_clients
== 0)
1515 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, NULL
);
1516 client_index
= stdio_nb_clients
;
1518 if (stdio_nb_clients
!= 0)
1520 chr
= qemu_chr_open_fd(0, 1);
1522 stdio_clients
[stdio_nb_clients
++] = chr
;
1523 if (stdio_nb_clients
== 1) {
1524 /* set the terminal in raw mode */
1530 #if defined(__linux__)
1531 CharDriverState
*qemu_chr_open_pty(void)
1534 char slave_name
[1024];
1535 int master_fd
, slave_fd
;
1537 /* Not satisfying */
1538 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1542 /* Disabling local echo and line-buffered output */
1543 tcgetattr (master_fd
, &tty
);
1544 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1546 tty
.c_cc
[VTIME
] = 0;
1547 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1549 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1550 return qemu_chr_open_fd(master_fd
, master_fd
);
1553 static void tty_serial_init(int fd
, int speed
,
1554 int parity
, int data_bits
, int stop_bits
)
1560 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1561 speed
, parity
, data_bits
, stop_bits
);
1563 tcgetattr (fd
, &tty
);
1605 cfsetispeed(&tty
, spd
);
1606 cfsetospeed(&tty
, spd
);
1608 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1609 |INLCR
|IGNCR
|ICRNL
|IXON
);
1610 tty
.c_oflag
|= OPOST
;
1611 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1612 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1633 tty
.c_cflag
|= PARENB
;
1636 tty
.c_cflag
|= PARENB
| PARODD
;
1640 tty
.c_cflag
|= CSTOPB
;
1642 tcsetattr (fd
, TCSANOW
, &tty
);
1645 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1647 FDCharDriver
*s
= chr
->opaque
;
1650 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1652 QEMUSerialSetParams
*ssp
= arg
;
1653 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1654 ssp
->data_bits
, ssp
->stop_bits
);
1657 case CHR_IOCTL_SERIAL_SET_BREAK
:
1659 int enable
= *(int *)arg
;
1661 tcsendbreak(s
->fd_in
, 1);
1670 CharDriverState
*qemu_chr_open_tty(const char *filename
)
1672 CharDriverState
*chr
;
1675 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1678 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1679 tty_serial_init(fd
, 115200, 'N', 8, 1);
1680 chr
= qemu_chr_open_fd(fd
, fd
);
1683 chr
->chr_ioctl
= tty_serial_ioctl
;
1687 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1689 int fd
= (int)chr
->opaque
;
1693 case CHR_IOCTL_PP_READ_DATA
:
1694 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1696 *(uint8_t *)arg
= b
;
1698 case CHR_IOCTL_PP_WRITE_DATA
:
1699 b
= *(uint8_t *)arg
;
1700 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1703 case CHR_IOCTL_PP_READ_CONTROL
:
1704 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1706 *(uint8_t *)arg
= b
;
1708 case CHR_IOCTL_PP_WRITE_CONTROL
:
1709 b
= *(uint8_t *)arg
;
1710 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1713 case CHR_IOCTL_PP_READ_STATUS
:
1714 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1716 *(uint8_t *)arg
= b
;
1724 CharDriverState
*qemu_chr_open_pp(const char *filename
)
1726 CharDriverState
*chr
;
1729 fd
= open(filename
, O_RDWR
);
1733 if (ioctl(fd
, PPCLAIM
) < 0) {
1738 chr
= qemu_mallocz(sizeof(CharDriverState
));
1743 chr
->opaque
= (void *)fd
;
1744 chr
->chr_write
= null_chr_write
;
1745 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1746 chr
->chr_ioctl
= pp_ioctl
;
1751 CharDriverState
*qemu_chr_open_pty(void)
1757 #endif /* !defined(_WIN32) */
1761 IOCanRWHandler
*fd_can_read
;
1762 IOReadHandler
*fd_read
;
1765 HANDLE hcom
, hrecv
, hsend
;
1766 OVERLAPPED orecv
, osend
;
1771 #define NSENDBUF 2048
1772 #define NRECVBUF 2048
1773 #define MAXCONNECT 1
1774 #define NTIMEOUT 5000
1776 static int win_chr_poll(void *opaque
);
1777 static int win_chr_pipe_poll(void *opaque
);
1779 static void win_chr_close2(WinCharState
*s
)
1782 CloseHandle(s
->hsend
);
1786 CloseHandle(s
->hrecv
);
1790 CloseHandle(s
->hcom
);
1794 qemu_del_polling_cb(win_chr_pipe_poll
, s
);
1796 qemu_del_polling_cb(win_chr_poll
, s
);
1799 static void win_chr_close(CharDriverState
*chr
)
1801 WinCharState
*s
= chr
->opaque
;
1805 static int win_chr_init(WinCharState
*s
, const char *filename
)
1808 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1813 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1815 fprintf(stderr
, "Failed CreateEvent\n");
1818 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1820 fprintf(stderr
, "Failed CreateEvent\n");
1824 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1825 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1826 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1827 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1832 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1833 fprintf(stderr
, "Failed SetupComm\n");
1837 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1838 size
= sizeof(COMMCONFIG
);
1839 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1840 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1841 CommConfigDialog(filename
, NULL
, &comcfg
);
1843 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1844 fprintf(stderr
, "Failed SetCommState\n");
1848 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1849 fprintf(stderr
, "Failed SetCommMask\n");
1853 cto
.ReadIntervalTimeout
= MAXDWORD
;
1854 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1855 fprintf(stderr
, "Failed SetCommTimeouts\n");
1859 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1860 fprintf(stderr
, "Failed ClearCommError\n");
1863 qemu_add_polling_cb(win_chr_poll
, s
);
1871 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1873 WinCharState
*s
= chr
->opaque
;
1874 DWORD len
, ret
, size
, err
;
1877 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1878 s
->osend
.hEvent
= s
->hsend
;
1881 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1883 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1885 err
= GetLastError();
1886 if (err
== ERROR_IO_PENDING
) {
1887 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1905 static int win_chr_read_poll(WinCharState
*s
)
1907 s
->max_size
= s
->fd_can_read(s
->win_opaque
);
1911 static void win_chr_readfile(WinCharState
*s
)
1917 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1918 s
->orecv
.hEvent
= s
->hrecv
;
1919 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1921 err
= GetLastError();
1922 if (err
== ERROR_IO_PENDING
) {
1923 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1928 s
->fd_read(s
->win_opaque
, buf
, size
);
1932 static void win_chr_read(WinCharState
*s
)
1934 if (s
->len
> s
->max_size
)
1935 s
->len
= s
->max_size
;
1939 win_chr_readfile(s
);
1942 static int win_chr_poll(void *opaque
)
1944 WinCharState
*s
= opaque
;
1948 ClearCommError(s
->hcom
, &comerr
, &status
);
1949 if (status
.cbInQue
> 0) {
1950 s
->len
= status
.cbInQue
;
1951 win_chr_read_poll(s
);
1958 static void win_chr_add_read_handler(CharDriverState
*chr
,
1959 IOCanRWHandler
*fd_can_read
,
1960 IOReadHandler
*fd_read
, void *opaque
)
1962 WinCharState
*s
= chr
->opaque
;
1964 s
->fd_can_read
= fd_can_read
;
1965 s
->fd_read
= fd_read
;
1966 s
->win_opaque
= opaque
;
1969 CharDriverState
*qemu_chr_open_win(const char *filename
)
1971 CharDriverState
*chr
;
1974 chr
= qemu_mallocz(sizeof(CharDriverState
));
1977 s
= qemu_mallocz(sizeof(WinCharState
));
1983 chr
->chr_write
= win_chr_write
;
1984 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
1985 chr
->chr_close
= win_chr_close
;
1987 if (win_chr_init(s
, filename
) < 0) {
1995 static int win_chr_pipe_poll(void *opaque
)
1997 WinCharState
*s
= opaque
;
2000 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2003 win_chr_read_poll(s
);
2010 static int win_chr_pipe_init(WinCharState
*s
, const char *filename
)
2019 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2021 fprintf(stderr
, "Failed CreateEvent\n");
2024 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2026 fprintf(stderr
, "Failed CreateEvent\n");
2030 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2031 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2032 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2034 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2035 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2036 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2041 ZeroMemory(&ov
, sizeof(ov
));
2042 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2043 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2045 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2049 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2051 fprintf(stderr
, "Failed GetOverlappedResult\n");
2053 CloseHandle(ov
.hEvent
);
2060 CloseHandle(ov
.hEvent
);
2063 qemu_add_polling_cb(win_chr_pipe_poll
, s
);
2072 CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2074 CharDriverState
*chr
;
2077 chr
= qemu_mallocz(sizeof(CharDriverState
));
2080 s
= qemu_mallocz(sizeof(WinCharState
));
2086 chr
->chr_write
= win_chr_write
;
2087 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2088 chr
->chr_close
= win_chr_close
;
2090 if (win_chr_pipe_init(s
, filename
) < 0) {
2098 CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2100 CharDriverState
*chr
;
2103 chr
= qemu_mallocz(sizeof(CharDriverState
));
2106 s
= qemu_mallocz(sizeof(WinCharState
));
2113 chr
->chr_write
= win_chr_write
;
2114 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2118 CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2122 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2123 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2124 if (fd_out
== INVALID_HANDLE_VALUE
)
2127 return qemu_chr_open_win_file(fd_out
);
2131 /***********************************************************/
2132 /* UDP Net console */
2135 IOCanRWHandler
*fd_can_read
;
2136 IOReadHandler
*fd_read
;
2139 struct sockaddr_in daddr
;
2146 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2148 NetCharDriver
*s
= chr
->opaque
;
2150 return sendto(s
->fd
, buf
, len
, 0,
2151 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2154 static int udp_chr_read_poll(void *opaque
)
2156 CharDriverState
*chr
= opaque
;
2157 NetCharDriver
*s
= chr
->opaque
;
2159 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2161 /* If there were any stray characters in the queue process them
2164 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2165 s
->fd_read(s
->fd_opaque
, &s
->buf
[s
->bufptr
], 1);
2167 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2172 static void udp_chr_read(void *opaque
)
2174 CharDriverState
*chr
= opaque
;
2175 NetCharDriver
*s
= chr
->opaque
;
2177 if (s
->max_size
== 0)
2179 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2180 s
->bufptr
= s
->bufcnt
;
2185 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2186 s
->fd_read(s
->fd_opaque
, &s
->buf
[s
->bufptr
], 1);
2188 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2192 static void udp_chr_add_read_handler(CharDriverState
*chr
,
2193 IOCanRWHandler
*fd_can_read
,
2194 IOReadHandler
*fd_read
, void *opaque
)
2196 NetCharDriver
*s
= chr
->opaque
;
2199 s
->fd_can_read
= fd_can_read
;
2200 s
->fd_read
= fd_read
;
2201 s
->fd_opaque
= opaque
;
2202 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2203 udp_chr_read
, NULL
, chr
);
2207 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2208 int parse_host_src_port(struct sockaddr_in
*haddr
,
2209 struct sockaddr_in
*saddr
,
2212 CharDriverState
*qemu_chr_open_udp(const char *def
)
2214 CharDriverState
*chr
= NULL
;
2215 NetCharDriver
*s
= NULL
;
2217 struct sockaddr_in saddr
;
2219 chr
= qemu_mallocz(sizeof(CharDriverState
));
2222 s
= qemu_mallocz(sizeof(NetCharDriver
));
2226 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2228 perror("socket(PF_INET, SOCK_DGRAM)");
2232 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2233 printf("Could not parse: %s\n", def
);
2237 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2247 chr
->chr_write
= udp_chr_write
;
2248 chr
->chr_add_read_handler
= udp_chr_add_read_handler
;
2261 /***********************************************************/
2262 /* TCP Net console */
2265 IOCanRWHandler
*fd_can_read
;
2266 IOReadHandler
*fd_read
;
2274 static void tcp_chr_accept(void *opaque
);
2276 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2278 TCPCharDriver
*s
= chr
->opaque
;
2280 return send_all(s
->fd
, buf
, len
);
2282 /* XXX: indicate an error ? */
2287 static int tcp_chr_read_poll(void *opaque
)
2289 CharDriverState
*chr
= opaque
;
2290 TCPCharDriver
*s
= chr
->opaque
;
2293 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2298 #define IAC_BREAK 243
2299 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2301 char *buf
, int *size
)
2303 /* Handle any telnet client's basic IAC options to satisfy char by
2304 * char mode with no echo. All IAC options will be removed from
2305 * the buf and the do_telnetopt variable will be used to track the
2306 * state of the width of the IAC information.
2308 * IAC commands come in sets of 3 bytes with the exception of the
2309 * "IAC BREAK" command and the double IAC.
2315 for (i
= 0; i
< *size
; i
++) {
2316 if (s
->do_telnetopt
> 1) {
2317 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2318 /* Double IAC means send an IAC */
2322 s
->do_telnetopt
= 1;
2324 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2325 /* Handle IAC break commands by sending a serial break */
2326 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
2331 if (s
->do_telnetopt
>= 4) {
2332 s
->do_telnetopt
= 1;
2335 if ((unsigned char)buf
[i
] == IAC
) {
2336 s
->do_telnetopt
= 2;
2347 static void tcp_chr_read(void *opaque
)
2349 CharDriverState
*chr
= opaque
;
2350 TCPCharDriver
*s
= chr
->opaque
;
2354 if (!s
->connected
|| s
->max_size
<= 0)
2357 if (len
> s
->max_size
)
2359 size
= recv(s
->fd
, buf
, len
, 0);
2361 /* connection closed */
2363 if (s
->listen_fd
>= 0) {
2364 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2366 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2369 } else if (size
> 0) {
2370 if (s
->do_telnetopt
)
2371 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2373 s
->fd_read(s
->fd_opaque
, buf
, size
);
2377 static void tcp_chr_add_read_handler(CharDriverState
*chr
,
2378 IOCanRWHandler
*fd_can_read
,
2379 IOReadHandler
*fd_read
, void *opaque
)
2381 TCPCharDriver
*s
= chr
->opaque
;
2383 s
->fd_can_read
= fd_can_read
;
2384 s
->fd_read
= fd_read
;
2385 s
->fd_opaque
= opaque
;
2388 static void tcp_chr_connect(void *opaque
)
2390 CharDriverState
*chr
= opaque
;
2391 TCPCharDriver
*s
= chr
->opaque
;
2394 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2395 tcp_chr_read
, NULL
, chr
);
2398 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2399 static void tcp_chr_telnet_init(int fd
)
2402 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2403 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2404 send(fd
, (char *)buf
, 3, 0);
2405 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2406 send(fd
, (char *)buf
, 3, 0);
2407 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2408 send(fd
, (char *)buf
, 3, 0);
2409 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2410 send(fd
, (char *)buf
, 3, 0);
2413 static void tcp_chr_accept(void *opaque
)
2415 CharDriverState
*chr
= opaque
;
2416 TCPCharDriver
*s
= chr
->opaque
;
2417 struct sockaddr_in saddr
;
2422 len
= sizeof(saddr
);
2423 fd
= accept(s
->listen_fd
, (struct sockaddr
*)&saddr
, &len
);
2424 if (fd
< 0 && errno
!= EINTR
) {
2426 } else if (fd
>= 0) {
2427 if (s
->do_telnetopt
)
2428 tcp_chr_telnet_init(fd
);
2432 socket_set_nonblock(fd
);
2434 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2435 tcp_chr_connect(chr
);
2438 static void tcp_chr_close(CharDriverState
*chr
)
2440 TCPCharDriver
*s
= chr
->opaque
;
2443 if (s
->listen_fd
>= 0)
2444 closesocket(s
->listen_fd
);
2448 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2451 CharDriverState
*chr
= NULL
;
2452 TCPCharDriver
*s
= NULL
;
2453 int fd
= -1, ret
, err
, val
;
2455 int is_waitconnect
= 1;
2457 struct sockaddr_in saddr
;
2459 if (parse_host_port(&saddr
, host_str
) < 0)
2463 while((ptr
= strchr(ptr
,','))) {
2465 if (!strncmp(ptr
,"server",6)) {
2467 } else if (!strncmp(ptr
,"nowait",6)) {
2470 printf("Unknown option: %s\n", ptr
);
2477 chr
= qemu_mallocz(sizeof(CharDriverState
));
2480 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2484 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2488 if (!is_waitconnect
)
2489 socket_set_nonblock(fd
);
2495 /* allow fast reuse */
2497 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2499 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2502 ret
= listen(fd
, 0);
2506 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2508 s
->do_telnetopt
= 1;
2511 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2513 err
= socket_error();
2514 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2515 } else if (err
== EINPROGRESS
) {
2527 tcp_chr_connect(chr
);
2529 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2533 chr
->chr_write
= tcp_chr_write
;
2534 chr
->chr_add_read_handler
= tcp_chr_add_read_handler
;
2535 chr
->chr_close
= tcp_chr_close
;
2536 if (is_listen
&& is_waitconnect
) {
2537 printf("QEMU waiting for connection on: %s\n", host_str
);
2538 tcp_chr_accept(chr
);
2539 socket_set_nonblock(s
->listen_fd
);
2551 CharDriverState
*qemu_chr_open(const char *filename
)
2555 if (!strcmp(filename
, "vc")) {
2556 return text_console_init(&display_state
);
2557 } else if (!strcmp(filename
, "null")) {
2558 return qemu_chr_open_null();
2560 if (strstart(filename
, "tcp:", &p
)) {
2561 return qemu_chr_open_tcp(p
, 0);
2563 if (strstart(filename
, "telnet:", &p
)) {
2564 return qemu_chr_open_tcp(p
, 1);
2566 if (strstart(filename
, "udp:", &p
)) {
2567 return qemu_chr_open_udp(p
);
2570 if (strstart(filename
, "file:", &p
)) {
2571 return qemu_chr_open_file_out(p
);
2572 } else if (strstart(filename
, "pipe:", &p
)) {
2573 return qemu_chr_open_pipe(p
);
2574 } else if (!strcmp(filename
, "pty")) {
2575 return qemu_chr_open_pty();
2576 } else if (!strcmp(filename
, "stdio")) {
2577 return qemu_chr_open_stdio();
2580 #if defined(__linux__)
2581 if (strstart(filename
, "/dev/parport", NULL
)) {
2582 return qemu_chr_open_pp(filename
);
2584 if (strstart(filename
, "/dev/", NULL
)) {
2585 return qemu_chr_open_tty(filename
);
2589 if (strstart(filename
, "COM", NULL
)) {
2590 return qemu_chr_open_win(filename
);
2592 if (strstart(filename
, "pipe:", &p
)) {
2593 return qemu_chr_open_win_pipe(p
);
2595 if (strstart(filename
, "file:", &p
)) {
2596 return qemu_chr_open_win_file_out(p
);
2604 void qemu_chr_close(CharDriverState
*chr
)
2607 chr
->chr_close(chr
);
2610 /***********************************************************/
2611 /* network device redirectors */
2613 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2617 for(i
=0;i
<size
;i
+=16) {
2621 fprintf(f
, "%08x ", i
);
2624 fprintf(f
, " %02x", buf
[i
+j
]);
2629 for(j
=0;j
<len
;j
++) {
2631 if (c
< ' ' || c
> '~')
2633 fprintf(f
, "%c", c
);
2639 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
2642 for(i
= 0; i
< 6; i
++) {
2643 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2656 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
2661 p1
= strchr(p
, sep
);
2667 if (len
> buf_size
- 1)
2669 memcpy(buf
, p
, len
);
2676 int parse_host_src_port(struct sockaddr_in
*haddr
,
2677 struct sockaddr_in
*saddr
,
2678 const char *input_str
)
2680 char *str
= strdup(input_str
);
2681 char *host_str
= str
;
2686 * Chop off any extra arguments at the end of the string which
2687 * would start with a comma, then fill in the src port information
2688 * if it was provided else use the "any address" and "any port".
2690 if ((ptr
= strchr(str
,',')))
2693 if ((src_str
= strchr(input_str
,'@'))) {
2698 if (parse_host_port(haddr
, host_str
) < 0)
2701 if (!src_str
|| *src_str
== '\0')
2704 if (parse_host_port(saddr
, src_str
) < 0)
2715 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
2723 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2725 saddr
->sin_family
= AF_INET
;
2726 if (buf
[0] == '\0') {
2727 saddr
->sin_addr
.s_addr
= 0;
2729 if (isdigit(buf
[0])) {
2730 if (!inet_aton(buf
, &saddr
->sin_addr
))
2733 if ((he
= gethostbyname(buf
)) == NULL
)
2735 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
2738 port
= strtol(p
, (char **)&r
, 0);
2741 saddr
->sin_port
= htons(port
);
2745 /* find or alloc a new VLAN */
2746 VLANState
*qemu_find_vlan(int id
)
2748 VLANState
**pvlan
, *vlan
;
2749 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2753 vlan
= qemu_mallocz(sizeof(VLANState
));
2758 pvlan
= &first_vlan
;
2759 while (*pvlan
!= NULL
)
2760 pvlan
= &(*pvlan
)->next
;
2765 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
2766 IOReadHandler
*fd_read
,
2767 IOCanRWHandler
*fd_can_read
,
2770 VLANClientState
*vc
, **pvc
;
2771 vc
= qemu_mallocz(sizeof(VLANClientState
));
2774 vc
->fd_read
= fd_read
;
2775 vc
->fd_can_read
= fd_can_read
;
2776 vc
->opaque
= opaque
;
2780 pvc
= &vlan
->first_client
;
2781 while (*pvc
!= NULL
)
2782 pvc
= &(*pvc
)->next
;
2787 int qemu_can_send_packet(VLANClientState
*vc1
)
2789 VLANState
*vlan
= vc1
->vlan
;
2790 VLANClientState
*vc
;
2792 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2794 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
2801 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
2803 VLANState
*vlan
= vc1
->vlan
;
2804 VLANClientState
*vc
;
2807 printf("vlan %d send:\n", vlan
->id
);
2808 hex_dump(stdout
, buf
, size
);
2810 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2812 vc
->fd_read(vc
->opaque
, buf
, size
);
2817 #if defined(CONFIG_SLIRP)
2819 /* slirp network adapter */
2821 static int slirp_inited
;
2822 static VLANClientState
*slirp_vc
;
2824 int slirp_can_output(void)
2826 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
2829 void slirp_output(const uint8_t *pkt
, int pkt_len
)
2832 printf("slirp output:\n");
2833 hex_dump(stdout
, pkt
, pkt_len
);
2837 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
2840 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
2843 printf("slirp input:\n");
2844 hex_dump(stdout
, buf
, size
);
2846 slirp_input(buf
, size
);
2849 static int net_slirp_init(VLANState
*vlan
)
2851 if (!slirp_inited
) {
2855 slirp_vc
= qemu_new_vlan_client(vlan
,
2856 slirp_receive
, NULL
, NULL
);
2857 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
2861 static void net_slirp_redir(const char *redir_str
)
2866 struct in_addr guest_addr
;
2867 int host_port
, guest_port
;
2869 if (!slirp_inited
) {
2875 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2877 if (!strcmp(buf
, "tcp")) {
2879 } else if (!strcmp(buf
, "udp")) {
2885 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2887 host_port
= strtol(buf
, &r
, 0);
2891 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2893 if (buf
[0] == '\0') {
2894 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
2896 if (!inet_aton(buf
, &guest_addr
))
2899 guest_port
= strtol(p
, &r
, 0);
2903 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
2904 fprintf(stderr
, "qemu: could not set up redirection\n");
2909 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
2917 static void smb_exit(void)
2921 char filename
[1024];
2923 /* erase all the files in the directory */
2924 d
= opendir(smb_dir
);
2929 if (strcmp(de
->d_name
, ".") != 0 &&
2930 strcmp(de
->d_name
, "..") != 0) {
2931 snprintf(filename
, sizeof(filename
), "%s/%s",
2932 smb_dir
, de
->d_name
);
2940 /* automatic user mode samba server configuration */
2941 void net_slirp_smb(const char *exported_dir
)
2943 char smb_conf
[1024];
2944 char smb_cmdline
[1024];
2947 if (!slirp_inited
) {
2952 /* XXX: better tmp dir construction */
2953 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
2954 if (mkdir(smb_dir
, 0700) < 0) {
2955 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
2958 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
2960 f
= fopen(smb_conf
, "w");
2962 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
2969 "socket address=127.0.0.1\n"
2970 "pid directory=%s\n"
2971 "lock directory=%s\n"
2972 "log file=%s/log.smbd\n"
2973 "smb passwd file=%s/smbpasswd\n"
2974 "security = share\n"
2989 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "/usr/sbin/smbd -s %s",
2992 slirp_add_exec(0, smb_cmdline
, 4, 139);
2995 #endif /* !defined(_WIN32) */
2997 #endif /* CONFIG_SLIRP */
2999 #if !defined(_WIN32)
3001 typedef struct TAPState
{
3002 VLANClientState
*vc
;
3006 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3008 TAPState
*s
= opaque
;
3011 ret
= write(s
->fd
, buf
, size
);
3012 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3019 static void tap_send(void *opaque
)
3021 TAPState
*s
= opaque
;
3025 size
= read(s
->fd
, buf
, sizeof(buf
));
3027 qemu_send_packet(s
->vc
, buf
, size
);
3033 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3037 s
= qemu_mallocz(sizeof(TAPState
));
3041 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3042 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3043 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3048 static int tap_open(char *ifname
, int ifname_size
)
3054 fd
= open("/dev/tap", O_RDWR
);
3056 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3061 dev
= devname(s
.st_rdev
, S_IFCHR
);
3062 pstrcpy(ifname
, ifname_size
, dev
);
3064 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3067 #elif defined(__sun__)
3068 static int tap_open(char *ifname
, int ifname_size
)
3070 fprintf(stderr
, "warning: tap_open not yet implemented\n");
3074 static int tap_open(char *ifname
, int ifname_size
)
3079 fd
= open("/dev/net/tun", O_RDWR
);
3081 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
3084 memset(&ifr
, 0, sizeof(ifr
));
3085 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
3086 if (ifname
[0] != '\0')
3087 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
3089 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
3090 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
3092 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
3096 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
3097 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3102 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
3103 const char *setup_script
)
3106 int pid
, status
, fd
;
3111 if (ifname1
!= NULL
)
3112 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
3115 fd
= tap_open(ifname
, sizeof(ifname
));
3121 if (setup_script
[0] != '\0') {
3122 /* try to launch network init script */
3127 *parg
++ = (char *)setup_script
;
3130 execv(setup_script
, args
);
3133 while (waitpid(pid
, &status
, 0) != pid
);
3134 if (!WIFEXITED(status
) ||
3135 WEXITSTATUS(status
) != 0) {
3136 fprintf(stderr
, "%s: could not launch network script\n",
3142 s
= net_tap_fd_init(vlan
, fd
);
3145 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3146 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
3150 #endif /* !_WIN32 */
3152 /* network connection */
3153 typedef struct NetSocketState
{
3154 VLANClientState
*vc
;
3156 int state
; /* 0 = getting length, 1 = getting data */
3160 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
3163 typedef struct NetSocketListenState
{
3166 } NetSocketListenState
;
3168 /* XXX: we consider we can send the whole packet without blocking */
3169 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
3171 NetSocketState
*s
= opaque
;
3175 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
3176 send_all(s
->fd
, buf
, size
);
3179 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
3181 NetSocketState
*s
= opaque
;
3182 sendto(s
->fd
, buf
, size
, 0,
3183 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
3186 static void net_socket_send(void *opaque
)
3188 NetSocketState
*s
= opaque
;
3193 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
3195 err
= socket_error();
3196 if (err
!= EWOULDBLOCK
)
3198 } else if (size
== 0) {
3199 /* end of connection */
3201 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3207 /* reassemble a packet from the network */
3213 memcpy(s
->buf
+ s
->index
, buf
, l
);
3217 if (s
->index
== 4) {
3219 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
3225 l
= s
->packet_len
- s
->index
;
3228 memcpy(s
->buf
+ s
->index
, buf
, l
);
3232 if (s
->index
>= s
->packet_len
) {
3233 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
3242 static void net_socket_send_dgram(void *opaque
)
3244 NetSocketState
*s
= opaque
;
3247 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
3251 /* end of connection */
3252 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3255 qemu_send_packet(s
->vc
, s
->buf
, size
);
3258 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
3263 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
3264 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
3265 inet_ntoa(mcastaddr
->sin_addr
),
3266 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
3270 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
3272 perror("socket(PF_INET, SOCK_DGRAM)");
3277 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
3278 (const char *)&val
, sizeof(val
));
3280 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
3284 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
3290 /* Add host to multicast group */
3291 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
3292 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
3294 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
3295 (const char *)&imr
, sizeof(struct ip_mreq
));
3297 perror("setsockopt(IP_ADD_MEMBERSHIP)");
3301 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
3303 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
3304 (const char *)&val
, sizeof(val
));
3306 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
3310 socket_set_nonblock(fd
);
3318 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
3321 struct sockaddr_in saddr
;
3323 socklen_t saddr_len
;
3326 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
3327 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
3328 * by ONLY ONE process: we must "clone" this dgram socket --jjo
3332 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
3334 if (saddr
.sin_addr
.s_addr
==0) {
3335 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
3339 /* clone dgram socket */
3340 newfd
= net_socket_mcast_create(&saddr
);
3342 /* error already reported by net_socket_mcast_create() */
3346 /* clone newfd to fd, close newfd */
3351 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
3352 fd
, strerror(errno
));
3357 s
= qemu_mallocz(sizeof(NetSocketState
));
3362 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
3363 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
3365 /* mcast: save bound address as dst */
3366 if (is_connected
) s
->dgram_dst
=saddr
;
3368 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3369 "socket: fd=%d (%s mcast=%s:%d)",
3370 fd
, is_connected
? "cloned" : "",
3371 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3375 static void net_socket_connect(void *opaque
)
3377 NetSocketState
*s
= opaque
;
3378 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
3381 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
3385 s
= qemu_mallocz(sizeof(NetSocketState
));
3389 s
->vc
= qemu_new_vlan_client(vlan
,
3390 net_socket_receive
, NULL
, s
);
3391 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3392 "socket: fd=%d", fd
);
3394 net_socket_connect(s
);
3396 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
3401 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
3404 int so_type
=-1, optlen
=sizeof(so_type
);
3406 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
3407 fprintf(stderr
, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd
);
3412 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
3414 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3416 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
3417 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
3418 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3423 static void net_socket_accept(void *opaque
)
3425 NetSocketListenState
*s
= opaque
;
3427 struct sockaddr_in saddr
;
3432 len
= sizeof(saddr
);
3433 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
3434 if (fd
< 0 && errno
!= EINTR
) {
3436 } else if (fd
>= 0) {
3440 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
3444 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
3445 "socket: connection from %s:%d",
3446 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3450 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
3452 NetSocketListenState
*s
;
3454 struct sockaddr_in saddr
;
3456 if (parse_host_port(&saddr
, host_str
) < 0)
3459 s
= qemu_mallocz(sizeof(NetSocketListenState
));
3463 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3468 socket_set_nonblock(fd
);
3470 /* allow fast reuse */
3472 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3474 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3479 ret
= listen(fd
, 0);
3486 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
3490 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
3493 int fd
, connected
, ret
, err
;
3494 struct sockaddr_in saddr
;
3496 if (parse_host_port(&saddr
, host_str
) < 0)
3499 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3504 socket_set_nonblock(fd
);
3508 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3510 err
= socket_error();
3511 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3512 } else if (err
== EINPROGRESS
) {
3524 s
= net_socket_fd_init(vlan
, fd
, connected
);
3527 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3528 "socket: connect to %s:%d",
3529 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3533 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
3537 struct sockaddr_in saddr
;
3539 if (parse_host_port(&saddr
, host_str
) < 0)
3543 fd
= net_socket_mcast_create(&saddr
);
3547 s
= net_socket_fd_init(vlan
, fd
, 0);
3551 s
->dgram_dst
= saddr
;
3553 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3554 "socket: mcast=%s:%d",
3555 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3560 static int get_param_value(char *buf
, int buf_size
,
3561 const char *tag
, const char *str
)
3570 while (*p
!= '\0' && *p
!= '=') {
3571 if ((q
- option
) < sizeof(option
) - 1)
3579 if (!strcmp(tag
, option
)) {
3581 while (*p
!= '\0' && *p
!= ',') {
3582 if ((q
- buf
) < buf_size
- 1)
3589 while (*p
!= '\0' && *p
!= ',') {
3600 int net_client_init(const char *str
)
3611 while (*p
!= '\0' && *p
!= ',') {
3612 if ((q
- device
) < sizeof(device
) - 1)
3620 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
3621 vlan_id
= strtol(buf
, NULL
, 0);
3623 vlan
= qemu_find_vlan(vlan_id
);
3625 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
3628 if (!strcmp(device
, "nic")) {
3632 if (nb_nics
>= MAX_NICS
) {
3633 fprintf(stderr
, "Too Many NICs\n");
3636 nd
= &nd_table
[nb_nics
];
3637 macaddr
= nd
->macaddr
;
3643 macaddr
[5] = 0x56 + nb_nics
;
3645 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
3646 if (parse_macaddr(macaddr
, buf
) < 0) {
3647 fprintf(stderr
, "invalid syntax for ethernet address\n");
3651 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
3652 nd
->model
= strdup(buf
);
3658 if (!strcmp(device
, "none")) {
3659 /* does nothing. It is needed to signal that no network cards
3664 if (!strcmp(device
, "user")) {
3665 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
3666 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
3668 ret
= net_slirp_init(vlan
);
3672 if (!strcmp(device
, "tap")) {
3674 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
3675 fprintf(stderr
, "tap: no interface name\n");
3678 ret
= tap_win32_init(vlan
, ifname
);
3681 if (!strcmp(device
, "tap")) {
3683 char setup_script
[1024];
3685 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3686 fd
= strtol(buf
, NULL
, 0);
3688 if (net_tap_fd_init(vlan
, fd
))
3691 get_param_value(ifname
, sizeof(ifname
), "ifname", p
);
3692 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
3693 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
3695 ret
= net_tap_init(vlan
, ifname
, setup_script
);
3699 if (!strcmp(device
, "socket")) {
3700 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3702 fd
= strtol(buf
, NULL
, 0);
3704 if (net_socket_fd_init(vlan
, fd
, 1))
3706 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
3707 ret
= net_socket_listen_init(vlan
, buf
);
3708 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
3709 ret
= net_socket_connect_init(vlan
, buf
);
3710 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
3711 ret
= net_socket_mcast_init(vlan
, buf
);
3713 fprintf(stderr
, "Unknown socket options: %s\n", p
);
3718 fprintf(stderr
, "Unknown network device: %s\n", device
);
3722 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
3728 void do_info_network(void)
3731 VLANClientState
*vc
;
3733 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3734 term_printf("VLAN %d devices:\n", vlan
->id
);
3735 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3736 term_printf(" %s\n", vc
->info_str
);
3740 /***********************************************************/
3743 static USBPort
*used_usb_ports
;
3744 static USBPort
*free_usb_ports
;
3746 /* ??? Maybe change this to register a hub to keep track of the topology. */
3747 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
3748 usb_attachfn attach
)
3750 port
->opaque
= opaque
;
3751 port
->index
= index
;
3752 port
->attach
= attach
;
3753 port
->next
= free_usb_ports
;
3754 free_usb_ports
= port
;
3757 static int usb_device_add(const char *devname
)
3763 if (!free_usb_ports
)
3766 if (strstart(devname
, "host:", &p
)) {
3767 dev
= usb_host_device_open(p
);
3768 } else if (!strcmp(devname
, "mouse")) {
3769 dev
= usb_mouse_init();
3770 } else if (!strcmp(devname
, "tablet")) {
3771 dev
= usb_tablet_init();
3772 } else if (strstart(devname
, "disk:", &p
)) {
3773 dev
= usb_msd_init(p
);
3780 /* Find a USB port to add the device to. */
3781 port
= free_usb_ports
;
3785 /* Create a new hub and chain it on. */
3786 free_usb_ports
= NULL
;
3787 port
->next
= used_usb_ports
;
3788 used_usb_ports
= port
;
3790 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
3791 usb_attach(port
, hub
);
3792 port
= free_usb_ports
;
3795 free_usb_ports
= port
->next
;
3796 port
->next
= used_usb_ports
;
3797 used_usb_ports
= port
;
3798 usb_attach(port
, dev
);
3802 static int usb_device_del(const char *devname
)
3810 if (!used_usb_ports
)
3813 p
= strchr(devname
, '.');
3816 bus_num
= strtoul(devname
, NULL
, 0);
3817 addr
= strtoul(p
+ 1, NULL
, 0);
3821 lastp
= &used_usb_ports
;
3822 port
= used_usb_ports
;
3823 while (port
&& port
->dev
->addr
!= addr
) {
3824 lastp
= &port
->next
;
3832 *lastp
= port
->next
;
3833 usb_attach(port
, NULL
);
3834 dev
->handle_destroy(dev
);
3835 port
->next
= free_usb_ports
;
3836 free_usb_ports
= port
;
3840 void do_usb_add(const char *devname
)
3843 ret
= usb_device_add(devname
);
3845 term_printf("Could not add USB device '%s'\n", devname
);
3848 void do_usb_del(const char *devname
)
3851 ret
= usb_device_del(devname
);
3853 term_printf("Could not remove USB device '%s'\n", devname
);
3860 const char *speed_str
;
3863 term_printf("USB support not enabled\n");
3867 for (port
= used_usb_ports
; port
; port
= port
->next
) {
3871 switch(dev
->speed
) {
3875 case USB_SPEED_FULL
:
3878 case USB_SPEED_HIGH
:
3885 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
3886 0, dev
->addr
, speed_str
, dev
->devname
);
3890 /***********************************************************/
3893 static char *pid_filename
;
3895 /* Remove PID file. Called on normal exit */
3897 static void remove_pidfile(void)
3899 unlink (pid_filename
);
3902 static void create_pidfile(const char *filename
)
3904 struct stat pidstat
;
3907 /* Try to write our PID to the named file */
3908 if (stat(filename
, &pidstat
) < 0) {
3909 if (errno
== ENOENT
) {
3910 if ((f
= fopen (filename
, "w")) == NULL
) {
3911 perror("Opening pidfile");
3914 fprintf(f
, "%d\n", getpid());
3916 pid_filename
= qemu_strdup(filename
);
3917 if (!pid_filename
) {
3918 fprintf(stderr
, "Could not save PID filename");
3921 atexit(remove_pidfile
);
3924 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
3930 /***********************************************************/
3933 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
3937 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
3941 static void dumb_refresh(DisplayState
*ds
)
3946 void dumb_display_init(DisplayState
*ds
)
3951 ds
->dpy_update
= dumb_update
;
3952 ds
->dpy_resize
= dumb_resize
;
3953 ds
->dpy_refresh
= dumb_refresh
;
3956 /***********************************************************/
3959 #define MAX_IO_HANDLERS 64
3961 typedef struct IOHandlerRecord
{
3963 IOCanRWHandler
*fd_read_poll
;
3965 IOHandler
*fd_write
;
3967 /* temporary data */
3969 struct IOHandlerRecord
*next
;
3972 static IOHandlerRecord
*first_io_handler
;
3974 /* XXX: fd_read_poll should be suppressed, but an API change is
3975 necessary in the character devices to suppress fd_can_read(). */
3976 int qemu_set_fd_handler2(int fd
,
3977 IOCanRWHandler
*fd_read_poll
,
3979 IOHandler
*fd_write
,
3982 IOHandlerRecord
**pioh
, *ioh
;
3984 if (!fd_read
&& !fd_write
) {
3985 pioh
= &first_io_handler
;
3990 if (ioh
->fd
== fd
) {
3998 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4002 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4005 ioh
->next
= first_io_handler
;
4006 first_io_handler
= ioh
;
4009 ioh
->fd_read_poll
= fd_read_poll
;
4010 ioh
->fd_read
= fd_read
;
4011 ioh
->fd_write
= fd_write
;
4012 ioh
->opaque
= opaque
;
4017 int qemu_set_fd_handler(int fd
,
4019 IOHandler
*fd_write
,
4022 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4025 /***********************************************************/
4026 /* Polling handling */
4028 typedef struct PollingEntry
{
4031 struct PollingEntry
*next
;
4034 static PollingEntry
*first_polling_entry
;
4036 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4038 PollingEntry
**ppe
, *pe
;
4039 pe
= qemu_mallocz(sizeof(PollingEntry
));
4043 pe
->opaque
= opaque
;
4044 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
4049 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
4051 PollingEntry
**ppe
, *pe
;
4052 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
4054 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
4063 /***********************************************************/
4064 /* Wait objects support */
4065 typedef struct WaitObjects
{
4067 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
4068 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
4069 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
4072 static WaitObjects wait_objects
= {0};
4074 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4076 WaitObjects
*w
= &wait_objects
;
4078 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
4080 w
->events
[w
->num
] = handle
;
4081 w
->func
[w
->num
] = func
;
4082 w
->opaque
[w
->num
] = opaque
;
4087 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4090 WaitObjects
*w
= &wait_objects
;
4093 for (i
= 0; i
< w
->num
; i
++) {
4094 if (w
->events
[i
] == handle
)
4097 w
->events
[i
] = w
->events
[i
+ 1];
4098 w
->func
[i
] = w
->func
[i
+ 1];
4099 w
->opaque
[i
] = w
->opaque
[i
+ 1];
4107 /***********************************************************/
4108 /* savevm/loadvm support */
4110 #define IO_BUF_SIZE 32768
4114 BlockDriverState
*bs
;
4117 int64_t base_offset
;
4118 int64_t buf_offset
; /* start of buffer when writing, end of buffer
4121 int buf_size
; /* 0 when writing */
4122 uint8_t buf
[IO_BUF_SIZE
];
4125 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
4129 f
= qemu_mallocz(sizeof(QEMUFile
));
4132 if (!strcmp(mode
, "wb")) {
4134 } else if (!strcmp(mode
, "rb")) {
4139 f
->outfile
= fopen(filename
, mode
);
4151 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
4155 f
= qemu_mallocz(sizeof(QEMUFile
));
4160 f
->is_writable
= is_writable
;
4161 f
->base_offset
= offset
;
4165 void qemu_fflush(QEMUFile
*f
)
4167 if (!f
->is_writable
)
4169 if (f
->buf_index
> 0) {
4171 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4172 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
4174 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4175 f
->buf
, f
->buf_index
);
4177 f
->buf_offset
+= f
->buf_index
;
4182 static void qemu_fill_buffer(QEMUFile
*f
)
4189 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4190 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
4194 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4195 f
->buf
, IO_BUF_SIZE
);
4201 f
->buf_offset
+= len
;
4204 void qemu_fclose(QEMUFile
*f
)
4214 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
4218 l
= IO_BUF_SIZE
- f
->buf_index
;
4221 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
4225 if (f
->buf_index
>= IO_BUF_SIZE
)
4230 void qemu_put_byte(QEMUFile
*f
, int v
)
4232 f
->buf
[f
->buf_index
++] = v
;
4233 if (f
->buf_index
>= IO_BUF_SIZE
)
4237 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
4243 l
= f
->buf_size
- f
->buf_index
;
4245 qemu_fill_buffer(f
);
4246 l
= f
->buf_size
- f
->buf_index
;
4252 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
4257 return size1
- size
;
4260 int qemu_get_byte(QEMUFile
*f
)
4262 if (f
->buf_index
>= f
->buf_size
) {
4263 qemu_fill_buffer(f
);
4264 if (f
->buf_index
>= f
->buf_size
)
4267 return f
->buf
[f
->buf_index
++];
4270 int64_t qemu_ftell(QEMUFile
*f
)
4272 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
4275 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
4277 if (whence
== SEEK_SET
) {
4279 } else if (whence
== SEEK_CUR
) {
4280 pos
+= qemu_ftell(f
);
4282 /* SEEK_END not supported */
4285 if (f
->is_writable
) {
4287 f
->buf_offset
= pos
;
4289 f
->buf_offset
= pos
;
4296 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
4298 qemu_put_byte(f
, v
>> 8);
4299 qemu_put_byte(f
, v
);
4302 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
4304 qemu_put_byte(f
, v
>> 24);
4305 qemu_put_byte(f
, v
>> 16);
4306 qemu_put_byte(f
, v
>> 8);
4307 qemu_put_byte(f
, v
);
4310 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
4312 qemu_put_be32(f
, v
>> 32);
4313 qemu_put_be32(f
, v
);
4316 unsigned int qemu_get_be16(QEMUFile
*f
)
4319 v
= qemu_get_byte(f
) << 8;
4320 v
|= qemu_get_byte(f
);
4324 unsigned int qemu_get_be32(QEMUFile
*f
)
4327 v
= qemu_get_byte(f
) << 24;
4328 v
|= qemu_get_byte(f
) << 16;
4329 v
|= qemu_get_byte(f
) << 8;
4330 v
|= qemu_get_byte(f
);
4334 uint64_t qemu_get_be64(QEMUFile
*f
)
4337 v
= (uint64_t)qemu_get_be32(f
) << 32;
4338 v
|= qemu_get_be32(f
);
4342 typedef struct SaveStateEntry
{
4346 SaveStateHandler
*save_state
;
4347 LoadStateHandler
*load_state
;
4349 struct SaveStateEntry
*next
;
4352 static SaveStateEntry
*first_se
;
4354 int register_savevm(const char *idstr
,
4357 SaveStateHandler
*save_state
,
4358 LoadStateHandler
*load_state
,
4361 SaveStateEntry
*se
, **pse
;
4363 se
= qemu_malloc(sizeof(SaveStateEntry
));
4366 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
4367 se
->instance_id
= instance_id
;
4368 se
->version_id
= version_id
;
4369 se
->save_state
= save_state
;
4370 se
->load_state
= load_state
;
4371 se
->opaque
= opaque
;
4374 /* add at the end of list */
4376 while (*pse
!= NULL
)
4377 pse
= &(*pse
)->next
;
4382 #define QEMU_VM_FILE_MAGIC 0x5145564d
4383 #define QEMU_VM_FILE_VERSION 0x00000002
4385 int qemu_savevm_state(QEMUFile
*f
)
4389 int64_t cur_pos
, len_pos
, total_len_pos
;
4391 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
4392 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
4393 total_len_pos
= qemu_ftell(f
);
4394 qemu_put_be64(f
, 0); /* total size */
4396 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4398 len
= strlen(se
->idstr
);
4399 qemu_put_byte(f
, len
);
4400 qemu_put_buffer(f
, se
->idstr
, len
);
4402 qemu_put_be32(f
, se
->instance_id
);
4403 qemu_put_be32(f
, se
->version_id
);
4405 /* record size: filled later */
4406 len_pos
= qemu_ftell(f
);
4407 qemu_put_be32(f
, 0);
4409 se
->save_state(f
, se
->opaque
);
4411 /* fill record size */
4412 cur_pos
= qemu_ftell(f
);
4413 len
= cur_pos
- len_pos
- 4;
4414 qemu_fseek(f
, len_pos
, SEEK_SET
);
4415 qemu_put_be32(f
, len
);
4416 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4418 cur_pos
= qemu_ftell(f
);
4419 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
4420 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
4421 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4427 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
4431 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4432 if (!strcmp(se
->idstr
, idstr
) &&
4433 instance_id
== se
->instance_id
)
4439 int qemu_loadvm_state(QEMUFile
*f
)
4442 int len
, ret
, instance_id
, record_len
, version_id
;
4443 int64_t total_len
, end_pos
, cur_pos
;
4447 v
= qemu_get_be32(f
);
4448 if (v
!= QEMU_VM_FILE_MAGIC
)
4450 v
= qemu_get_be32(f
);
4451 if (v
!= QEMU_VM_FILE_VERSION
) {
4456 total_len
= qemu_get_be64(f
);
4457 end_pos
= total_len
+ qemu_ftell(f
);
4459 if (qemu_ftell(f
) >= end_pos
)
4461 len
= qemu_get_byte(f
);
4462 qemu_get_buffer(f
, idstr
, len
);
4464 instance_id
= qemu_get_be32(f
);
4465 version_id
= qemu_get_be32(f
);
4466 record_len
= qemu_get_be32(f
);
4468 printf("idstr=%s instance=0x%x version=%d len=%d\n",
4469 idstr
, instance_id
, version_id
, record_len
);
4471 cur_pos
= qemu_ftell(f
);
4472 se
= find_se(idstr
, instance_id
);
4474 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
4475 instance_id
, idstr
);
4477 ret
= se
->load_state(f
, se
->opaque
, version_id
);
4479 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
4480 instance_id
, idstr
);
4483 /* always seek to exact end of record */
4484 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
4491 /* device can contain snapshots */
4492 static int bdrv_can_snapshot(BlockDriverState
*bs
)
4495 !bdrv_is_removable(bs
) &&
4496 !bdrv_is_read_only(bs
));
4499 /* device must be snapshots in order to have a reliable snapshot */
4500 static int bdrv_has_snapshot(BlockDriverState
*bs
)
4503 !bdrv_is_removable(bs
) &&
4504 !bdrv_is_read_only(bs
));
4507 static BlockDriverState
*get_bs_snapshots(void)
4509 BlockDriverState
*bs
;
4513 return bs_snapshots
;
4514 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4516 if (bdrv_can_snapshot(bs
))
4525 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
4528 QEMUSnapshotInfo
*sn_tab
, *sn
;
4532 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
4535 for(i
= 0; i
< nb_sns
; i
++) {
4537 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
4547 void do_savevm(const char *name
)
4549 BlockDriverState
*bs
, *bs1
;
4550 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
4551 int must_delete
, ret
, i
;
4552 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
4554 int saved_vm_running
;
4561 bs
= get_bs_snapshots();
4563 term_printf("No block device can accept snapshots\n");
4567 /* ??? Should this occur after vm_stop? */
4570 saved_vm_running
= vm_running
;
4575 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
4580 memset(sn
, 0, sizeof(*sn
));
4582 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
4583 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
4586 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
4589 /* fill auxiliary fields */
4592 sn
->date_sec
= tb
.time
;
4593 sn
->date_nsec
= tb
.millitm
* 1000000;
4595 gettimeofday(&tv
, NULL
);
4596 sn
->date_sec
= tv
.tv_sec
;
4597 sn
->date_nsec
= tv
.tv_usec
* 1000;
4599 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
4601 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
4602 term_printf("Device %s does not support VM state snapshots\n",
4603 bdrv_get_device_name(bs
));
4607 /* save the VM state */
4608 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
4610 term_printf("Could not open VM state file\n");
4613 ret
= qemu_savevm_state(f
);
4614 sn
->vm_state_size
= qemu_ftell(f
);
4617 term_printf("Error %d while writing VM\n", ret
);
4621 /* create the snapshots */
4623 for(i
= 0; i
< MAX_DISKS
; i
++) {
4625 if (bdrv_has_snapshot(bs1
)) {
4627 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
4629 term_printf("Error while deleting snapshot on '%s'\n",
4630 bdrv_get_device_name(bs1
));
4633 ret
= bdrv_snapshot_create(bs1
, sn
);
4635 term_printf("Error while creating snapshot on '%s'\n",
4636 bdrv_get_device_name(bs1
));
4642 if (saved_vm_running
)
4646 void do_loadvm(const char *name
)
4648 BlockDriverState
*bs
, *bs1
;
4649 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
4652 int saved_vm_running
;
4654 bs
= get_bs_snapshots();
4656 term_printf("No block device supports snapshots\n");
4660 /* Flush all IO requests so they don't interfere with the new state. */
4663 saved_vm_running
= vm_running
;
4666 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4668 if (bdrv_has_snapshot(bs1
)) {
4669 ret
= bdrv_snapshot_goto(bs1
, name
);
4672 term_printf("Warning: ");
4675 term_printf("Snapshots not supported on device '%s'\n",
4676 bdrv_get_device_name(bs1
));
4679 term_printf("Could not find snapshot '%s' on device '%s'\n",
4680 name
, bdrv_get_device_name(bs1
));
4683 term_printf("Error %d while activating snapshot on '%s'\n",
4684 ret
, bdrv_get_device_name(bs1
));
4687 /* fatal on snapshot block device */
4694 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
4695 term_printf("Device %s does not support VM state snapshots\n",
4696 bdrv_get_device_name(bs
));
4700 /* restore the VM state */
4701 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
4703 term_printf("Could not open VM state file\n");
4706 ret
= qemu_loadvm_state(f
);
4709 term_printf("Error %d while loading VM state\n", ret
);
4712 if (saved_vm_running
)
4716 void do_delvm(const char *name
)
4718 BlockDriverState
*bs
, *bs1
;
4721 bs
= get_bs_snapshots();
4723 term_printf("No block device supports snapshots\n");
4727 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4729 if (bdrv_has_snapshot(bs1
)) {
4730 ret
= bdrv_snapshot_delete(bs1
, name
);
4732 if (ret
== -ENOTSUP
)
4733 term_printf("Snapshots not supported on device '%s'\n",
4734 bdrv_get_device_name(bs1
));
4736 term_printf("Error %d while deleting snapshot on '%s'\n",
4737 ret
, bdrv_get_device_name(bs1
));
4743 void do_info_snapshots(void)
4745 BlockDriverState
*bs
, *bs1
;
4746 QEMUSnapshotInfo
*sn_tab
, *sn
;
4750 bs
= get_bs_snapshots();
4752 term_printf("No available block device supports snapshots\n");
4755 term_printf("Snapshot devices:");
4756 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4758 if (bdrv_has_snapshot(bs1
)) {
4760 term_printf(" %s", bdrv_get_device_name(bs1
));
4765 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
4767 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
4770 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
4771 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
4772 for(i
= 0; i
< nb_sns
; i
++) {
4774 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
4779 /***********************************************************/
4780 /* cpu save/restore */
4782 #if defined(TARGET_I386)
4784 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
4786 qemu_put_be32(f
, dt
->selector
);
4787 qemu_put_betl(f
, dt
->base
);
4788 qemu_put_be32(f
, dt
->limit
);
4789 qemu_put_be32(f
, dt
->flags
);
4792 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
4794 dt
->selector
= qemu_get_be32(f
);
4795 dt
->base
= qemu_get_betl(f
);
4796 dt
->limit
= qemu_get_be32(f
);
4797 dt
->flags
= qemu_get_be32(f
);
4800 void cpu_save(QEMUFile
*f
, void *opaque
)
4802 CPUState
*env
= opaque
;
4803 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
4807 for(i
= 0; i
< CPU_NB_REGS
; i
++)
4808 qemu_put_betls(f
, &env
->regs
[i
]);
4809 qemu_put_betls(f
, &env
->eip
);
4810 qemu_put_betls(f
, &env
->eflags
);
4811 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
4812 qemu_put_be32s(f
, &hflags
);
4816 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
4818 for(i
= 0; i
< 8; i
++) {
4819 fptag
|= ((!env
->fptags
[i
]) << i
);
4822 qemu_put_be16s(f
, &fpuc
);
4823 qemu_put_be16s(f
, &fpus
);
4824 qemu_put_be16s(f
, &fptag
);
4826 #ifdef USE_X86LDOUBLE
4831 qemu_put_be16s(f
, &fpregs_format
);
4833 for(i
= 0; i
< 8; i
++) {
4834 #ifdef USE_X86LDOUBLE
4838 /* we save the real CPU data (in case of MMX usage only 'mant'
4839 contains the MMX register */
4840 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
4841 qemu_put_be64(f
, mant
);
4842 qemu_put_be16(f
, exp
);
4845 /* if we use doubles for float emulation, we save the doubles to
4846 avoid losing information in case of MMX usage. It can give
4847 problems if the image is restored on a CPU where long
4848 doubles are used instead. */
4849 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
4853 for(i
= 0; i
< 6; i
++)
4854 cpu_put_seg(f
, &env
->segs
[i
]);
4855 cpu_put_seg(f
, &env
->ldt
);
4856 cpu_put_seg(f
, &env
->tr
);
4857 cpu_put_seg(f
, &env
->gdt
);
4858 cpu_put_seg(f
, &env
->idt
);
4860 qemu_put_be32s(f
, &env
->sysenter_cs
);
4861 qemu_put_be32s(f
, &env
->sysenter_esp
);
4862 qemu_put_be32s(f
, &env
->sysenter_eip
);
4864 qemu_put_betls(f
, &env
->cr
[0]);
4865 qemu_put_betls(f
, &env
->cr
[2]);
4866 qemu_put_betls(f
, &env
->cr
[3]);
4867 qemu_put_betls(f
, &env
->cr
[4]);
4869 for(i
= 0; i
< 8; i
++)
4870 qemu_put_betls(f
, &env
->dr
[i
]);
4873 qemu_put_be32s(f
, &env
->a20_mask
);
4876 qemu_put_be32s(f
, &env
->mxcsr
);
4877 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
4878 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
4879 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
4882 #ifdef TARGET_X86_64
4883 qemu_put_be64s(f
, &env
->efer
);
4884 qemu_put_be64s(f
, &env
->star
);
4885 qemu_put_be64s(f
, &env
->lstar
);
4886 qemu_put_be64s(f
, &env
->cstar
);
4887 qemu_put_be64s(f
, &env
->fmask
);
4888 qemu_put_be64s(f
, &env
->kernelgsbase
);
4890 qemu_put_be32s(f
, &env
->smbase
);
4893 #ifdef USE_X86LDOUBLE
4894 /* XXX: add that in a FPU generic layer */
4895 union x86_longdouble
{
4900 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
4901 #define EXPBIAS1 1023
4902 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
4903 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
4905 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
4909 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
4910 /* exponent + sign */
4911 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
4912 e
|= SIGND1(temp
) >> 16;
4917 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4919 CPUState
*env
= opaque
;
4922 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
4924 if (version_id
!= 3 && version_id
!= 4)
4926 for(i
= 0; i
< CPU_NB_REGS
; i
++)
4927 qemu_get_betls(f
, &env
->regs
[i
]);
4928 qemu_get_betls(f
, &env
->eip
);
4929 qemu_get_betls(f
, &env
->eflags
);
4930 qemu_get_be32s(f
, &hflags
);
4932 qemu_get_be16s(f
, &fpuc
);
4933 qemu_get_be16s(f
, &fpus
);
4934 qemu_get_be16s(f
, &fptag
);
4935 qemu_get_be16s(f
, &fpregs_format
);
4937 /* NOTE: we cannot always restore the FPU state if the image come
4938 from a host with a different 'USE_X86LDOUBLE' define. We guess
4939 if we are in an MMX state to restore correctly in that case. */
4940 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
4941 for(i
= 0; i
< 8; i
++) {
4945 switch(fpregs_format
) {
4947 mant
= qemu_get_be64(f
);
4948 exp
= qemu_get_be16(f
);
4949 #ifdef USE_X86LDOUBLE
4950 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
4952 /* difficult case */
4954 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
4956 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
4960 mant
= qemu_get_be64(f
);
4961 #ifdef USE_X86LDOUBLE
4963 union x86_longdouble
*p
;
4964 /* difficult case */
4965 p
= (void *)&env
->fpregs
[i
];
4970 fp64_to_fp80(p
, mant
);
4974 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
4983 /* XXX: restore FPU round state */
4984 env
->fpstt
= (fpus
>> 11) & 7;
4985 env
->fpus
= fpus
& ~0x3800;
4987 for(i
= 0; i
< 8; i
++) {
4988 env
->fptags
[i
] = (fptag
>> i
) & 1;
4991 for(i
= 0; i
< 6; i
++)
4992 cpu_get_seg(f
, &env
->segs
[i
]);
4993 cpu_get_seg(f
, &env
->ldt
);
4994 cpu_get_seg(f
, &env
->tr
);
4995 cpu_get_seg(f
, &env
->gdt
);
4996 cpu_get_seg(f
, &env
->idt
);
4998 qemu_get_be32s(f
, &env
->sysenter_cs
);
4999 qemu_get_be32s(f
, &env
->sysenter_esp
);
5000 qemu_get_be32s(f
, &env
->sysenter_eip
);
5002 qemu_get_betls(f
, &env
->cr
[0]);
5003 qemu_get_betls(f
, &env
->cr
[2]);
5004 qemu_get_betls(f
, &env
->cr
[3]);
5005 qemu_get_betls(f
, &env
->cr
[4]);
5007 for(i
= 0; i
< 8; i
++)
5008 qemu_get_betls(f
, &env
->dr
[i
]);
5011 qemu_get_be32s(f
, &env
->a20_mask
);
5013 qemu_get_be32s(f
, &env
->mxcsr
);
5014 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5015 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5016 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5019 #ifdef TARGET_X86_64
5020 qemu_get_be64s(f
, &env
->efer
);
5021 qemu_get_be64s(f
, &env
->star
);
5022 qemu_get_be64s(f
, &env
->lstar
);
5023 qemu_get_be64s(f
, &env
->cstar
);
5024 qemu_get_be64s(f
, &env
->fmask
);
5025 qemu_get_be64s(f
, &env
->kernelgsbase
);
5027 if (version_id
>= 4)
5028 qemu_get_be32s(f
, &env
->smbase
);
5030 /* XXX: compute hflags from scratch, except for CPL and IIF */
5031 env
->hflags
= hflags
;
5036 #elif defined(TARGET_PPC)
5037 void cpu_save(QEMUFile
*f
, void *opaque
)
5041 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5046 #elif defined(TARGET_MIPS)
5047 void cpu_save(QEMUFile
*f
, void *opaque
)
5051 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5056 #elif defined(TARGET_SPARC)
5057 void cpu_save(QEMUFile
*f
, void *opaque
)
5059 CPUState
*env
= opaque
;
5063 for(i
= 0; i
< 8; i
++)
5064 qemu_put_betls(f
, &env
->gregs
[i
]);
5065 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5066 qemu_put_betls(f
, &env
->regbase
[i
]);
5069 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5075 qemu_put_be32(f
, u
.i
);
5078 qemu_put_betls(f
, &env
->pc
);
5079 qemu_put_betls(f
, &env
->npc
);
5080 qemu_put_betls(f
, &env
->y
);
5082 qemu_put_be32(f
, tmp
);
5083 qemu_put_betls(f
, &env
->fsr
);
5084 qemu_put_betls(f
, &env
->tbr
);
5085 #ifndef TARGET_SPARC64
5086 qemu_put_be32s(f
, &env
->wim
);
5088 for(i
= 0; i
< 16; i
++)
5089 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
5093 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5095 CPUState
*env
= opaque
;
5099 for(i
= 0; i
< 8; i
++)
5100 qemu_get_betls(f
, &env
->gregs
[i
]);
5101 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5102 qemu_get_betls(f
, &env
->regbase
[i
]);
5105 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5110 u
.i
= qemu_get_be32(f
);
5114 qemu_get_betls(f
, &env
->pc
);
5115 qemu_get_betls(f
, &env
->npc
);
5116 qemu_get_betls(f
, &env
->y
);
5117 tmp
= qemu_get_be32(f
);
5118 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
5119 correctly updated */
5121 qemu_get_betls(f
, &env
->fsr
);
5122 qemu_get_betls(f
, &env
->tbr
);
5123 #ifndef TARGET_SPARC64
5124 qemu_get_be32s(f
, &env
->wim
);
5126 for(i
= 0; i
< 16; i
++)
5127 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
5133 #elif defined(TARGET_ARM)
5135 /* ??? Need to implement these. */
5136 void cpu_save(QEMUFile
*f
, void *opaque
)
5140 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5147 #warning No CPU save/restore functions
5151 /***********************************************************/
5152 /* ram save/restore */
5154 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
5158 v
= qemu_get_byte(f
);
5161 if (qemu_get_buffer(f
, buf
, len
) != len
)
5165 v
= qemu_get_byte(f
);
5166 memset(buf
, v
, len
);
5174 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
5178 if (qemu_get_be32(f
) != phys_ram_size
)
5180 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
5181 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
5188 #define BDRV_HASH_BLOCK_SIZE 1024
5189 #define IOBUF_SIZE 4096
5190 #define RAM_CBLOCK_MAGIC 0xfabe
5192 typedef struct RamCompressState
{
5195 uint8_t buf
[IOBUF_SIZE
];
5198 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
5201 memset(s
, 0, sizeof(*s
));
5203 ret
= deflateInit2(&s
->zstream
, 1,
5205 9, Z_DEFAULT_STRATEGY
);
5208 s
->zstream
.avail_out
= IOBUF_SIZE
;
5209 s
->zstream
.next_out
= s
->buf
;
5213 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
5215 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
5216 qemu_put_be16(s
->f
, len
);
5217 qemu_put_buffer(s
->f
, buf
, len
);
5220 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
5224 s
->zstream
.avail_in
= len
;
5225 s
->zstream
.next_in
= (uint8_t *)buf
;
5226 while (s
->zstream
.avail_in
> 0) {
5227 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
5230 if (s
->zstream
.avail_out
== 0) {
5231 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
5232 s
->zstream
.avail_out
= IOBUF_SIZE
;
5233 s
->zstream
.next_out
= s
->buf
;
5239 static void ram_compress_close(RamCompressState
*s
)
5243 /* compress last bytes */
5245 ret
= deflate(&s
->zstream
, Z_FINISH
);
5246 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
5247 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
5249 ram_put_cblock(s
, s
->buf
, len
);
5251 s
->zstream
.avail_out
= IOBUF_SIZE
;
5252 s
->zstream
.next_out
= s
->buf
;
5253 if (ret
== Z_STREAM_END
)
5260 deflateEnd(&s
->zstream
);
5263 typedef struct RamDecompressState
{
5266 uint8_t buf
[IOBUF_SIZE
];
5267 } RamDecompressState
;
5269 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
5272 memset(s
, 0, sizeof(*s
));
5274 ret
= inflateInit(&s
->zstream
);
5280 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
5284 s
->zstream
.avail_out
= len
;
5285 s
->zstream
.next_out
= buf
;
5286 while (s
->zstream
.avail_out
> 0) {
5287 if (s
->zstream
.avail_in
== 0) {
5288 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
5290 clen
= qemu_get_be16(s
->f
);
5291 if (clen
> IOBUF_SIZE
)
5293 qemu_get_buffer(s
->f
, s
->buf
, clen
);
5294 s
->zstream
.avail_in
= clen
;
5295 s
->zstream
.next_in
= s
->buf
;
5297 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
5298 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
5305 static void ram_decompress_close(RamDecompressState
*s
)
5307 inflateEnd(&s
->zstream
);
5310 static void ram_save(QEMUFile
*f
, void *opaque
)
5313 RamCompressState s1
, *s
= &s1
;
5316 qemu_put_be32(f
, phys_ram_size
);
5317 if (ram_compress_open(s
, f
) < 0)
5319 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5321 if (tight_savevm_enabled
) {
5325 /* find if the memory block is available on a virtual
5328 for(j
= 0; j
< MAX_DISKS
; j
++) {
5330 sector_num
= bdrv_hash_find(bs_table
[j
],
5331 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5332 if (sector_num
>= 0)
5337 goto normal_compress
;
5340 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
5341 ram_compress_buf(s
, buf
, 10);
5347 ram_compress_buf(s
, buf
, 1);
5348 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5351 ram_compress_close(s
);
5354 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
5356 RamDecompressState s1
, *s
= &s1
;
5360 if (version_id
== 1)
5361 return ram_load_v1(f
, opaque
);
5362 if (version_id
!= 2)
5364 if (qemu_get_be32(f
) != phys_ram_size
)
5366 if (ram_decompress_open(s
, f
) < 0)
5368 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5369 if (ram_decompress_buf(s
, buf
, 1) < 0) {
5370 fprintf(stderr
, "Error while reading ram block header\n");
5374 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
5375 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
5384 ram_decompress_buf(s
, buf
+ 1, 9);
5386 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
5387 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
5388 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
5391 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
5392 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
5393 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
5394 bs_index
, sector_num
);
5401 printf("Error block header\n");
5405 ram_decompress_close(s
);
5409 /***********************************************************/
5410 /* bottom halves (can be seen as timers which expire ASAP) */
5419 static QEMUBH
*first_bh
= NULL
;
5421 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
5424 bh
= qemu_mallocz(sizeof(QEMUBH
));
5428 bh
->opaque
= opaque
;
5432 int qemu_bh_poll(void)
5451 void qemu_bh_schedule(QEMUBH
*bh
)
5453 CPUState
*env
= cpu_single_env
;
5457 bh
->next
= first_bh
;
5460 /* stop the currently executing CPU to execute the BH ASAP */
5462 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
5466 void qemu_bh_cancel(QEMUBH
*bh
)
5469 if (bh
->scheduled
) {
5472 pbh
= &(*pbh
)->next
;
5478 void qemu_bh_delete(QEMUBH
*bh
)
5484 /***********************************************************/
5485 /* machine registration */
5487 QEMUMachine
*first_machine
= NULL
;
5489 int qemu_register_machine(QEMUMachine
*m
)
5492 pm
= &first_machine
;
5500 QEMUMachine
*find_machine(const char *name
)
5504 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5505 if (!strcmp(m
->name
, name
))
5511 /***********************************************************/
5512 /* main execution loop */
5514 void gui_update(void *opaque
)
5516 display_state
.dpy_refresh(&display_state
);
5517 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
5520 struct vm_change_state_entry
{
5521 VMChangeStateHandler
*cb
;
5523 LIST_ENTRY (vm_change_state_entry
) entries
;
5526 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
5528 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
5531 VMChangeStateEntry
*e
;
5533 e
= qemu_mallocz(sizeof (*e
));
5539 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
5543 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
5545 LIST_REMOVE (e
, entries
);
5549 static void vm_state_notify(int running
)
5551 VMChangeStateEntry
*e
;
5553 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
5554 e
->cb(e
->opaque
, running
);
5558 /* XXX: support several handlers */
5559 static VMStopHandler
*vm_stop_cb
;
5560 static void *vm_stop_opaque
;
5562 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
5565 vm_stop_opaque
= opaque
;
5569 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
5583 void vm_stop(int reason
)
5586 cpu_disable_ticks();
5590 vm_stop_cb(vm_stop_opaque
, reason
);
5597 /* reset/shutdown handler */
5599 typedef struct QEMUResetEntry
{
5600 QEMUResetHandler
*func
;
5602 struct QEMUResetEntry
*next
;
5605 static QEMUResetEntry
*first_reset_entry
;
5606 static int reset_requested
;
5607 static int shutdown_requested
;
5608 static int powerdown_requested
;
5610 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
5612 QEMUResetEntry
**pre
, *re
;
5614 pre
= &first_reset_entry
;
5615 while (*pre
!= NULL
)
5616 pre
= &(*pre
)->next
;
5617 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
5619 re
->opaque
= opaque
;
5624 void qemu_system_reset(void)
5628 /* reset all devices */
5629 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
5630 re
->func(re
->opaque
);
5634 void qemu_system_reset_request(void)
5637 shutdown_requested
= 1;
5639 reset_requested
= 1;
5642 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5645 void qemu_system_shutdown_request(void)
5647 shutdown_requested
= 1;
5649 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5652 void qemu_system_powerdown_request(void)
5654 powerdown_requested
= 1;
5656 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5659 void main_loop_wait(int timeout
)
5661 IOHandlerRecord
*ioh
, *ioh_next
;
5662 fd_set rfds
, wfds
, xfds
;
5668 /* XXX: need to suppress polling by better using win32 events */
5670 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
5671 ret
|= pe
->func(pe
->opaque
);
5674 if (ret
== 0 && timeout
> 0) {
5676 WaitObjects
*w
= &wait_objects
;
5678 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
5679 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
5680 if (w
->func
[ret
- WAIT_OBJECT_0
])
5681 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
5682 } else if (ret
== WAIT_TIMEOUT
) {
5684 err
= GetLastError();
5685 fprintf(stderr
, "Wait error %d %d\n", ret
, err
);
5689 /* poll any events */
5690 /* XXX: separate device handlers from system ones */
5695 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
5697 (!ioh
->fd_read_poll
||
5698 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
5699 FD_SET(ioh
->fd
, &rfds
);
5703 if (ioh
->fd_write
) {
5704 FD_SET(ioh
->fd
, &wfds
);
5714 tv
.tv_usec
= timeout
* 1000;
5716 #if defined(CONFIG_SLIRP)
5718 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
5721 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
5723 /* XXX: better handling of removal */
5724 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
5725 ioh_next
= ioh
->next
;
5726 if (FD_ISSET(ioh
->fd
, &rfds
)) {
5727 ioh
->fd_read(ioh
->opaque
);
5729 if (FD_ISSET(ioh
->fd
, &wfds
)) {
5730 ioh
->fd_write(ioh
->opaque
);
5734 #if defined(CONFIG_SLIRP)
5741 slirp_select_poll(&rfds
, &wfds
, &xfds
);
5748 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
5749 qemu_get_clock(vm_clock
));
5750 /* run dma transfers, if any */
5754 /* real time timers */
5755 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
5756 qemu_get_clock(rt_clock
));
5759 static CPUState
*cur_cpu
;
5764 #ifdef CONFIG_PROFILER
5769 cur_cpu
= first_cpu
;
5776 env
= env
->next_cpu
;
5779 #ifdef CONFIG_PROFILER
5780 ti
= profile_getclock();
5782 ret
= cpu_exec(env
);
5783 #ifdef CONFIG_PROFILER
5784 qemu_time
+= profile_getclock() - ti
;
5786 if (ret
!= EXCP_HALTED
)
5788 /* all CPUs are halted ? */
5789 if (env
== cur_cpu
) {
5796 if (shutdown_requested
) {
5797 ret
= EXCP_INTERRUPT
;
5800 if (reset_requested
) {
5801 reset_requested
= 0;
5802 qemu_system_reset();
5803 ret
= EXCP_INTERRUPT
;
5805 if (powerdown_requested
) {
5806 powerdown_requested
= 0;
5807 qemu_system_powerdown();
5808 ret
= EXCP_INTERRUPT
;
5810 if (ret
== EXCP_DEBUG
) {
5811 vm_stop(EXCP_DEBUG
);
5813 /* if hlt instruction, we wait until the next IRQ */
5814 /* XXX: use timeout computed from timers */
5815 if (ret
== EXCP_HLT
)
5822 #ifdef CONFIG_PROFILER
5823 ti
= profile_getclock();
5825 main_loop_wait(timeout
);
5826 #ifdef CONFIG_PROFILER
5827 dev_time
+= profile_getclock() - ti
;
5830 cpu_disable_ticks();
5836 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2006 Fabrice Bellard\n"
5837 "usage: %s [options] [disk_image]\n"
5839 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
5841 "Standard options:\n"
5842 "-M machine select emulated machine (-M ? for list)\n"
5843 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
5844 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
5845 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
5846 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
5847 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
5848 "-snapshot write to temporary files instead of disk image files\n"
5850 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
5852 "-m megs set virtual RAM size to megs MB [default=%d]\n"
5853 "-smp n set the number of CPUs to 'n' [default=1]\n"
5854 "-nographic disable graphical output and redirect serial I/Os to console\n"
5856 "-k language use keyboard layout (for example \"fr\" for French)\n"
5859 "-audio-help print list of audio drivers and their options\n"
5860 "-soundhw c1,... enable audio support\n"
5861 " and only specified sound cards (comma separated list)\n"
5862 " use -soundhw ? to get the list of supported cards\n"
5863 " use -soundhw all to enable all of them\n"
5865 "-localtime set the real time clock to local time [default=utc]\n"
5866 "-full-screen start in full screen\n"
5868 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
5870 "-usb enable the USB driver (will be the default soon)\n"
5871 "-usbdevice name add the host or guest USB device 'name'\n"
5872 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5873 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
5876 "Network options:\n"
5877 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
5878 " create a new Network Interface Card and connect it to VLAN 'n'\n"
5880 "-net user[,vlan=n][,hostname=host]\n"
5881 " connect the user mode network stack to VLAN 'n' and send\n"
5882 " hostname 'host' to DHCP clients\n"
5885 "-net tap[,vlan=n],ifname=name\n"
5886 " connect the host TAP network interface to VLAN 'n'\n"
5888 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
5889 " connect the host TAP network interface to VLAN 'n' and use\n"
5890 " the network script 'file' (default=%s);\n"
5891 " use 'fd=h' to connect to an already opened TAP interface\n"
5893 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
5894 " connect the vlan 'n' to another VLAN using a socket connection\n"
5895 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
5896 " connect the vlan 'n' to multicast maddr and port\n"
5897 "-net none use it alone to have zero network devices; if no -net option\n"
5898 " is provided, the default is '-net nic -net user'\n"
5901 "-tftp prefix allow tftp access to files starting with prefix [-net user]\n"
5903 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
5905 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
5906 " redirect TCP or UDP connections from host to guest [-net user]\n"
5909 "Linux boot specific:\n"
5910 "-kernel bzImage use 'bzImage' as kernel image\n"
5911 "-append cmdline use 'cmdline' as kernel command line\n"
5912 "-initrd file use 'file' as initial ram disk\n"
5914 "Debug/Expert options:\n"
5915 "-monitor dev redirect the monitor to char device 'dev'\n"
5916 "-serial dev redirect the serial port to char device 'dev'\n"
5917 "-parallel dev redirect the parallel port to char device 'dev'\n"
5918 "-pidfile file Write PID to 'file'\n"
5919 "-S freeze CPU at startup (use 'c' to start execution)\n"
5920 "-s wait gdb connection to port %d\n"
5921 "-p port change gdb connection port\n"
5922 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
5923 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
5924 " translation (t=none or lba) (usually qemu can guess them)\n"
5925 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
5927 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
5928 "-no-kqemu disable KQEMU kernel module usage\n"
5930 #ifdef USE_CODE_COPY
5931 "-no-code-copy disable code copy acceleration\n"
5934 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
5935 " (default is CL-GD5446 PCI VGA)\n"
5936 "-no-acpi disable ACPI\n"
5938 "-no-reboot exit instead of rebooting\n"
5939 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
5940 "-vnc display start a VNC server on display\n"
5942 "During emulation, the following keys are useful:\n"
5943 "ctrl-alt-f toggle full screen\n"
5944 "ctrl-alt-n switch to virtual console 'n'\n"
5945 "ctrl-alt toggle mouse and keyboard grab\n"
5947 "When using -nographic, press 'ctrl-a h' to get some help.\n"
5952 DEFAULT_NETWORK_SCRIPT
,
5954 DEFAULT_GDBSTUB_PORT
,
5959 #define HAS_ARG 0x0001
5973 QEMU_OPTION_snapshot
,
5975 QEMU_OPTION_no_fd_bootchk
,
5978 QEMU_OPTION_nographic
,
5980 QEMU_OPTION_audio_help
,
5981 QEMU_OPTION_soundhw
,
5999 QEMU_OPTION_no_code_copy
,
6001 QEMU_OPTION_localtime
,
6002 QEMU_OPTION_cirrusvga
,
6004 QEMU_OPTION_std_vga
,
6005 QEMU_OPTION_monitor
,
6007 QEMU_OPTION_parallel
,
6009 QEMU_OPTION_full_screen
,
6010 QEMU_OPTION_pidfile
,
6011 QEMU_OPTION_no_kqemu
,
6012 QEMU_OPTION_kernel_kqemu
,
6013 QEMU_OPTION_win2k_hack
,
6015 QEMU_OPTION_usbdevice
,
6018 QEMU_OPTION_no_acpi
,
6019 QEMU_OPTION_no_reboot
,
6022 typedef struct QEMUOption
{
6028 const QEMUOption qemu_options
[] = {
6029 { "h", 0, QEMU_OPTION_h
},
6031 { "M", HAS_ARG
, QEMU_OPTION_M
},
6032 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
6033 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
6034 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
6035 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
6036 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
6037 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
6038 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
6039 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
6040 { "snapshot", 0, QEMU_OPTION_snapshot
},
6042 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
6044 { "m", HAS_ARG
, QEMU_OPTION_m
},
6045 { "nographic", 0, QEMU_OPTION_nographic
},
6046 { "k", HAS_ARG
, QEMU_OPTION_k
},
6048 { "audio-help", 0, QEMU_OPTION_audio_help
},
6049 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
6052 { "net", HAS_ARG
, QEMU_OPTION_net
},
6054 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
6056 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
6058 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
6061 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
6062 { "append", HAS_ARG
, QEMU_OPTION_append
},
6063 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
6065 { "S", 0, QEMU_OPTION_S
},
6066 { "s", 0, QEMU_OPTION_s
},
6067 { "p", HAS_ARG
, QEMU_OPTION_p
},
6068 { "d", HAS_ARG
, QEMU_OPTION_d
},
6069 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
6070 { "L", HAS_ARG
, QEMU_OPTION_L
},
6071 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
6073 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
6074 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
6076 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6077 { "g", 1, QEMU_OPTION_g
},
6079 { "localtime", 0, QEMU_OPTION_localtime
},
6080 { "std-vga", 0, QEMU_OPTION_std_vga
},
6081 { "monitor", 1, QEMU_OPTION_monitor
},
6082 { "serial", 1, QEMU_OPTION_serial
},
6083 { "parallel", 1, QEMU_OPTION_parallel
},
6084 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
6085 { "full-screen", 0, QEMU_OPTION_full_screen
},
6086 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
6087 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
6088 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
6089 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
6090 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
6092 /* temporary options */
6093 { "usb", 0, QEMU_OPTION_usb
},
6094 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
6095 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
6096 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
6100 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
6102 /* this stack is only used during signal handling */
6103 #define SIGNAL_STACK_SIZE 32768
6105 static uint8_t *signal_stack
;
6109 /* password input */
6111 static BlockDriverState
*get_bdrv(int index
)
6113 BlockDriverState
*bs
;
6116 bs
= bs_table
[index
];
6117 } else if (index
< 6) {
6118 bs
= fd_table
[index
- 4];
6125 static void read_passwords(void)
6127 BlockDriverState
*bs
;
6131 for(i
= 0; i
< 6; i
++) {
6133 if (bs
&& bdrv_is_encrypted(bs
)) {
6134 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
6135 for(j
= 0; j
< 3; j
++) {
6136 monitor_readline("Password: ",
6137 1, password
, sizeof(password
));
6138 if (bdrv_set_key(bs
, password
) == 0)
6140 term_printf("invalid password\n");
6146 /* XXX: currently we cannot use simultaneously different CPUs */
6147 void register_machines(void)
6149 #if defined(TARGET_I386)
6150 qemu_register_machine(&pc_machine
);
6151 qemu_register_machine(&isapc_machine
);
6152 #elif defined(TARGET_PPC)
6153 qemu_register_machine(&heathrow_machine
);
6154 qemu_register_machine(&core99_machine
);
6155 qemu_register_machine(&prep_machine
);
6156 #elif defined(TARGET_MIPS)
6157 qemu_register_machine(&mips_machine
);
6158 #elif defined(TARGET_SPARC)
6159 #ifdef TARGET_SPARC64
6160 qemu_register_machine(&sun4u_machine
);
6162 qemu_register_machine(&sun4m_machine
);
6164 #elif defined(TARGET_ARM)
6165 qemu_register_machine(&integratorcp926_machine
);
6166 qemu_register_machine(&integratorcp1026_machine
);
6167 qemu_register_machine(&versatilepb_machine
);
6168 qemu_register_machine(&versatileab_machine
);
6169 qemu_register_machine(&realview_machine
);
6170 #elif defined(TARGET_SH4)
6171 qemu_register_machine(&shix_machine
);
6173 #error unsupported CPU
6178 struct soundhw soundhw
[] = {
6185 { .init_isa
= pcspk_audio_init
}
6190 "Creative Sound Blaster 16",
6193 { .init_isa
= SB16_init
}
6200 "Yamaha YMF262 (OPL3)",
6202 "Yamaha YM3812 (OPL2)",
6206 { .init_isa
= Adlib_init
}
6213 "Gravis Ultrasound GF1",
6216 { .init_isa
= GUS_init
}
6222 "ENSONIQ AudioPCI ES1370",
6225 { .init_pci
= es1370_init
}
6228 { NULL
, NULL
, 0, 0, { NULL
} }
6231 static void select_soundhw (const char *optarg
)
6235 if (*optarg
== '?') {
6238 printf ("Valid sound card names (comma separated):\n");
6239 for (c
= soundhw
; c
->name
; ++c
) {
6240 printf ("%-11s %s\n", c
->name
, c
->descr
);
6242 printf ("\n-soundhw all will enable all of the above\n");
6243 exit (*optarg
!= '?');
6251 if (!strcmp (optarg
, "all")) {
6252 for (c
= soundhw
; c
->name
; ++c
) {
6260 e
= strchr (p
, ',');
6261 l
= !e
? strlen (p
) : (size_t) (e
- p
);
6263 for (c
= soundhw
; c
->name
; ++c
) {
6264 if (!strncmp (c
->name
, p
, l
)) {
6273 "Unknown sound card name (too big to show)\n");
6276 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
6281 p
+= l
+ (e
!= NULL
);
6285 goto show_valid_cards
;
6291 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
6293 exit(STATUS_CONTROL_C_EXIT
);
6298 #define MAX_NET_CLIENTS 32
6300 int main(int argc
, char **argv
)
6302 #ifdef CONFIG_GDBSTUB
6303 int use_gdbstub
, gdbstub_port
;
6306 int snapshot
, linux_boot
;
6307 const char *initrd_filename
;
6308 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
6309 const char *kernel_filename
, *kernel_cmdline
;
6310 DisplayState
*ds
= &display_state
;
6311 int cyls
, heads
, secs
, translation
;
6312 int start_emulation
= 1;
6313 char net_clients
[MAX_NET_CLIENTS
][256];
6316 const char *r
, *optarg
;
6317 CharDriverState
*monitor_hd
;
6318 char monitor_device
[128];
6319 char serial_devices
[MAX_SERIAL_PORTS
][128];
6320 int serial_device_index
;
6321 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
6322 int parallel_device_index
;
6323 const char *loadvm
= NULL
;
6324 QEMUMachine
*machine
;
6325 char usb_devices
[MAX_USB_CMDLINE
][128];
6326 int usb_devices_index
;
6328 LIST_INIT (&vm_change_state_head
);
6331 struct sigaction act
;
6332 sigfillset(&act
.sa_mask
);
6334 act
.sa_handler
= SIG_IGN
;
6335 sigaction(SIGPIPE
, &act
, NULL
);
6338 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
6339 /* Note: cpu_interrupt() is currently not SMP safe, so we force
6340 QEMU to run on a single CPU */
6345 h
= GetCurrentProcess();
6346 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
6347 for(i
= 0; i
< 32; i
++) {
6348 if (mask
& (1 << i
))
6353 SetProcessAffinityMask(h
, mask
);
6359 register_machines();
6360 machine
= first_machine
;
6361 initrd_filename
= NULL
;
6362 for(i
= 0; i
< MAX_FD
; i
++)
6363 fd_filename
[i
] = NULL
;
6364 for(i
= 0; i
< MAX_DISKS
; i
++)
6365 hd_filename
[i
] = NULL
;
6366 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
6367 vga_ram_size
= VGA_RAM_SIZE
;
6368 bios_size
= BIOS_SIZE
;
6369 #ifdef CONFIG_GDBSTUB
6371 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
6375 kernel_filename
= NULL
;
6376 kernel_cmdline
= "";
6382 cyls
= heads
= secs
= 0;
6383 translation
= BIOS_ATA_TRANSLATION_AUTO
;
6384 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
6386 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
6387 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
6388 serial_devices
[i
][0] = '\0';
6389 serial_device_index
= 0;
6391 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
6392 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
6393 parallel_devices
[i
][0] = '\0';
6394 parallel_device_index
= 0;
6396 usb_devices_index
= 0;
6401 /* default mac address of the first network interface */
6409 hd_filename
[0] = argv
[optind
++];
6411 const QEMUOption
*popt
;
6414 popt
= qemu_options
;
6417 fprintf(stderr
, "%s: invalid option -- '%s'\n",
6421 if (!strcmp(popt
->name
, r
+ 1))
6425 if (popt
->flags
& HAS_ARG
) {
6426 if (optind
>= argc
) {
6427 fprintf(stderr
, "%s: option '%s' requires an argument\n",
6431 optarg
= argv
[optind
++];
6436 switch(popt
->index
) {
6438 machine
= find_machine(optarg
);
6441 printf("Supported machines are:\n");
6442 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6443 printf("%-10s %s%s\n",
6445 m
== first_machine
? " (default)" : "");
6450 case QEMU_OPTION_initrd
:
6451 initrd_filename
= optarg
;
6453 case QEMU_OPTION_hda
:
6454 case QEMU_OPTION_hdb
:
6455 case QEMU_OPTION_hdc
:
6456 case QEMU_OPTION_hdd
:
6459 hd_index
= popt
->index
- QEMU_OPTION_hda
;
6460 hd_filename
[hd_index
] = optarg
;
6461 if (hd_index
== cdrom_index
)
6465 case QEMU_OPTION_snapshot
:
6468 case QEMU_OPTION_hdachs
:
6472 cyls
= strtol(p
, (char **)&p
, 0);
6473 if (cyls
< 1 || cyls
> 16383)
6478 heads
= strtol(p
, (char **)&p
, 0);
6479 if (heads
< 1 || heads
> 16)
6484 secs
= strtol(p
, (char **)&p
, 0);
6485 if (secs
< 1 || secs
> 63)
6489 if (!strcmp(p
, "none"))
6490 translation
= BIOS_ATA_TRANSLATION_NONE
;
6491 else if (!strcmp(p
, "lba"))
6492 translation
= BIOS_ATA_TRANSLATION_LBA
;
6493 else if (!strcmp(p
, "auto"))
6494 translation
= BIOS_ATA_TRANSLATION_AUTO
;
6497 } else if (*p
!= '\0') {
6499 fprintf(stderr
, "qemu: invalid physical CHS format\n");
6504 case QEMU_OPTION_nographic
:
6505 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
6506 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
6509 case QEMU_OPTION_kernel
:
6510 kernel_filename
= optarg
;
6512 case QEMU_OPTION_append
:
6513 kernel_cmdline
= optarg
;
6515 case QEMU_OPTION_cdrom
:
6516 if (cdrom_index
>= 0) {
6517 hd_filename
[cdrom_index
] = optarg
;
6520 case QEMU_OPTION_boot
:
6521 boot_device
= optarg
[0];
6522 if (boot_device
!= 'a' &&
6525 boot_device
!= 'n' &&
6527 boot_device
!= 'c' && boot_device
!= 'd') {
6528 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
6532 case QEMU_OPTION_fda
:
6533 fd_filename
[0] = optarg
;
6535 case QEMU_OPTION_fdb
:
6536 fd_filename
[1] = optarg
;
6539 case QEMU_OPTION_no_fd_bootchk
:
6543 case QEMU_OPTION_no_code_copy
:
6544 code_copy_enabled
= 0;
6546 case QEMU_OPTION_net
:
6547 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
6548 fprintf(stderr
, "qemu: too many network clients\n");
6551 pstrcpy(net_clients
[nb_net_clients
],
6552 sizeof(net_clients
[0]),
6557 case QEMU_OPTION_tftp
:
6558 tftp_prefix
= optarg
;
6561 case QEMU_OPTION_smb
:
6562 net_slirp_smb(optarg
);
6565 case QEMU_OPTION_redir
:
6566 net_slirp_redir(optarg
);
6570 case QEMU_OPTION_audio_help
:
6574 case QEMU_OPTION_soundhw
:
6575 select_soundhw (optarg
);
6582 ram_size
= atoi(optarg
) * 1024 * 1024;
6585 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
6586 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
6587 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
6596 mask
= cpu_str_to_log_mask(optarg
);
6598 printf("Log items (comma separated):\n");
6599 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
6600 printf("%-10s %s\n", item
->name
, item
->help
);
6607 #ifdef CONFIG_GDBSTUB
6612 gdbstub_port
= atoi(optarg
);
6619 start_emulation
= 0;
6622 keyboard_layout
= optarg
;
6624 case QEMU_OPTION_localtime
:
6627 case QEMU_OPTION_cirrusvga
:
6628 cirrus_vga_enabled
= 1;
6630 case QEMU_OPTION_std_vga
:
6631 cirrus_vga_enabled
= 0;
6638 w
= strtol(p
, (char **)&p
, 10);
6641 fprintf(stderr
, "qemu: invalid resolution or depth\n");
6647 h
= strtol(p
, (char **)&p
, 10);
6652 depth
= strtol(p
, (char **)&p
, 10);
6653 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
6654 depth
!= 24 && depth
!= 32)
6656 } else if (*p
== '\0') {
6657 depth
= graphic_depth
;
6664 graphic_depth
= depth
;
6667 case QEMU_OPTION_monitor
:
6668 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
6670 case QEMU_OPTION_serial
:
6671 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
6672 fprintf(stderr
, "qemu: too many serial ports\n");
6675 pstrcpy(serial_devices
[serial_device_index
],
6676 sizeof(serial_devices
[0]), optarg
);
6677 serial_device_index
++;
6679 case QEMU_OPTION_parallel
:
6680 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
6681 fprintf(stderr
, "qemu: too many parallel ports\n");
6684 pstrcpy(parallel_devices
[parallel_device_index
],
6685 sizeof(parallel_devices
[0]), optarg
);
6686 parallel_device_index
++;
6688 case QEMU_OPTION_loadvm
:
6691 case QEMU_OPTION_full_screen
:
6694 case QEMU_OPTION_pidfile
:
6695 create_pidfile(optarg
);
6698 case QEMU_OPTION_win2k_hack
:
6699 win2k_install_hack
= 1;
6703 case QEMU_OPTION_no_kqemu
:
6706 case QEMU_OPTION_kernel_kqemu
:
6710 case QEMU_OPTION_usb
:
6713 case QEMU_OPTION_usbdevice
:
6715 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
6716 fprintf(stderr
, "Too many USB devices\n");
6719 pstrcpy(usb_devices
[usb_devices_index
],
6720 sizeof(usb_devices
[usb_devices_index
]),
6722 usb_devices_index
++;
6724 case QEMU_OPTION_smp
:
6725 smp_cpus
= atoi(optarg
);
6726 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
6727 fprintf(stderr
, "Invalid number of CPUs\n");
6731 case QEMU_OPTION_vnc
:
6732 vnc_display
= atoi(optarg
);
6733 if (vnc_display
< 0) {
6734 fprintf(stderr
, "Invalid VNC display\n");
6738 case QEMU_OPTION_no_acpi
:
6741 case QEMU_OPTION_no_reboot
:
6752 linux_boot
= (kernel_filename
!= NULL
);
6755 hd_filename
[0] == '\0' &&
6756 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
6757 fd_filename
[0] == '\0')
6760 /* boot to cd by default if no hard disk */
6761 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
6762 if (fd_filename
[0] != '\0')
6768 setvbuf(stdout
, NULL
, _IOLBF
, 0);
6778 /* init network clients */
6779 if (nb_net_clients
== 0) {
6780 /* if no clients, we use a default config */
6781 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
6783 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
6788 for(i
= 0;i
< nb_net_clients
; i
++) {
6789 if (net_client_init(net_clients
[i
]) < 0)
6793 /* init the memory */
6794 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
6796 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
6797 if (!phys_ram_base
) {
6798 fprintf(stderr
, "Could not allocate physical memory\n");
6802 /* we always create the cdrom drive, even if no disk is there */
6804 if (cdrom_index
>= 0) {
6805 bs_table
[cdrom_index
] = bdrv_new("cdrom");
6806 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
6809 /* open the virtual block devices */
6810 for(i
= 0; i
< MAX_DISKS
; i
++) {
6811 if (hd_filename
[i
]) {
6814 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
6815 bs_table
[i
] = bdrv_new(buf
);
6817 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
6818 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
6822 if (i
== 0 && cyls
!= 0) {
6823 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
6824 bdrv_set_translation_hint(bs_table
[i
], translation
);
6829 /* we always create at least one floppy disk */
6830 fd_table
[0] = bdrv_new("fda");
6831 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
6833 for(i
= 0; i
< MAX_FD
; i
++) {
6834 if (fd_filename
[i
]) {
6837 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
6838 fd_table
[i
] = bdrv_new(buf
);
6839 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
6841 if (fd_filename
[i
] != '\0') {
6842 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
6843 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
6844 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
6852 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
6853 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
6859 dumb_display_init(ds
);
6860 } else if (vnc_display
!= -1) {
6861 vnc_display_init(ds
, vnc_display
);
6863 #if defined(CONFIG_SDL)
6864 sdl_display_init(ds
, full_screen
);
6865 #elif defined(CONFIG_COCOA)
6866 cocoa_display_init(ds
, full_screen
);
6868 dumb_display_init(ds
);
6872 monitor_hd
= qemu_chr_open(monitor_device
);
6874 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
6877 monitor_init(monitor_hd
, !nographic
);
6879 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6880 const char *devname
= serial_devices
[i
];
6881 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
6882 serial_hds
[i
] = qemu_chr_open(devname
);
6883 if (!serial_hds
[i
]) {
6884 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
6888 if (!strcmp(devname
, "vc"))
6889 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6893 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6894 const char *devname
= parallel_devices
[i
];
6895 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
6896 parallel_hds
[i
] = qemu_chr_open(devname
);
6897 if (!parallel_hds
[i
]) {
6898 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
6902 if (!strcmp(devname
, "vc"))
6903 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6907 machine
->init(ram_size
, vga_ram_size
, boot_device
,
6908 ds
, fd_filename
, snapshot
,
6909 kernel_filename
, kernel_cmdline
, initrd_filename
);
6911 /* init USB devices */
6913 for(i
= 0; i
< usb_devices_index
; i
++) {
6914 if (usb_device_add(usb_devices
[i
]) < 0) {
6915 fprintf(stderr
, "Warning: could not add USB device %s\n",
6921 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
6922 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
6924 #ifdef CONFIG_GDBSTUB
6926 if (gdbserver_start(gdbstub_port
) < 0) {
6927 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
6931 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
6939 /* XXX: simplify init */
6941 if (start_emulation
) {