4 * Copyright (c) 2003-2006 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
53 #include <linux/if_tun.h>
56 #include <linux/rtc.h>
57 #include <linux/ppdev.h>
62 #if defined(CONFIG_SLIRP)
68 #include <sys/timeb.h>
70 #define getopt_long_only getopt_long
71 #define memalign(align, size) malloc(size)
74 #include "qemu_socket.h"
80 #endif /* CONFIG_SDL */
84 #define main qemu_main
85 #endif /* CONFIG_COCOA */
91 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
93 //#define DEBUG_UNUSED_IOPORT
94 //#define DEBUG_IOPORT
96 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
99 #define DEFAULT_RAM_SIZE 144
101 #define DEFAULT_RAM_SIZE 128
104 #define GUI_REFRESH_INTERVAL 30
106 /* Max number of USB devices that can be specified on the commandline. */
107 #define MAX_USB_CMDLINE 8
109 /* XXX: use a two level table to limit memory usage */
110 #define MAX_IOPORTS 65536
112 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
113 char phys_ram_file
[1024];
114 void *ioport_opaque
[MAX_IOPORTS
];
115 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
116 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
117 /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available
118 to store the VM snapshots */
119 BlockDriverState
*bs_table
[MAX_DISKS
+ 1], *fd_table
[MAX_FD
];
120 /* point to the block driver where the snapshots are managed */
121 BlockDriverState
*bs_snapshots
;
124 static DisplayState display_state
;
126 const char* keyboard_layout
= NULL
;
127 int64_t ticks_per_sec
;
128 int boot_device
= 'c';
130 int pit_min_timer_count
= 0;
132 NICInfo nd_table
[MAX_NICS
];
133 QEMUTimer
*gui_timer
;
136 int cirrus_vga_enabled
= 1;
138 int graphic_width
= 1024;
139 int graphic_height
= 768;
141 int graphic_width
= 800;
142 int graphic_height
= 600;
144 int graphic_depth
= 15;
147 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
148 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
150 int win2k_install_hack
= 0;
153 static VLANState
*first_vlan
;
155 const char *vnc_display
;
156 #if defined(TARGET_SPARC)
158 #elif defined(TARGET_I386)
163 int acpi_enabled
= 1;
167 /***********************************************************/
168 /* x86 ISA bus support */
170 target_phys_addr_t isa_mem_base
= 0;
173 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
175 #ifdef DEBUG_UNUSED_IOPORT
176 fprintf(stderr
, "inb: port=0x%04x\n", address
);
181 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
183 #ifdef DEBUG_UNUSED_IOPORT
184 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
188 /* default is to make two byte accesses */
189 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
192 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
193 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
194 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
198 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
200 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
201 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
202 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
205 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
207 #ifdef DEBUG_UNUSED_IOPORT
208 fprintf(stderr
, "inl: port=0x%04x\n", address
);
213 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
215 #ifdef DEBUG_UNUSED_IOPORT
216 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
220 void init_ioports(void)
224 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
225 ioport_read_table
[0][i
] = default_ioport_readb
;
226 ioport_write_table
[0][i
] = default_ioport_writeb
;
227 ioport_read_table
[1][i
] = default_ioport_readw
;
228 ioport_write_table
[1][i
] = default_ioport_writew
;
229 ioport_read_table
[2][i
] = default_ioport_readl
;
230 ioport_write_table
[2][i
] = default_ioport_writel
;
234 /* size is the word size in byte */
235 int register_ioport_read(int start
, int length
, int size
,
236 IOPortReadFunc
*func
, void *opaque
)
242 } else if (size
== 2) {
244 } else if (size
== 4) {
247 hw_error("register_ioport_read: invalid size");
250 for(i
= start
; i
< start
+ length
; i
+= size
) {
251 ioport_read_table
[bsize
][i
] = func
;
252 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
253 hw_error("register_ioport_read: invalid opaque");
254 ioport_opaque
[i
] = opaque
;
259 /* size is the word size in byte */
260 int register_ioport_write(int start
, int length
, int size
,
261 IOPortWriteFunc
*func
, void *opaque
)
267 } else if (size
== 2) {
269 } else if (size
== 4) {
272 hw_error("register_ioport_write: invalid size");
275 for(i
= start
; i
< start
+ length
; i
+= size
) {
276 ioport_write_table
[bsize
][i
] = func
;
277 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
278 hw_error("register_ioport_read: invalid opaque");
279 ioport_opaque
[i
] = opaque
;
284 void isa_unassign_ioport(int start
, int length
)
288 for(i
= start
; i
< start
+ length
; i
++) {
289 ioport_read_table
[0][i
] = default_ioport_readb
;
290 ioport_read_table
[1][i
] = default_ioport_readw
;
291 ioport_read_table
[2][i
] = default_ioport_readl
;
293 ioport_write_table
[0][i
] = default_ioport_writeb
;
294 ioport_write_table
[1][i
] = default_ioport_writew
;
295 ioport_write_table
[2][i
] = default_ioport_writel
;
299 /***********************************************************/
301 void pstrcpy(char *buf
, int buf_size
, const char *str
)
311 if (c
== 0 || q
>= buf
+ buf_size
- 1)
318 /* strcat and truncate. */
319 char *pstrcat(char *buf
, int buf_size
, const char *s
)
324 pstrcpy(buf
+ len
, buf_size
- len
, s
);
328 int strstart(const char *str
, const char *val
, const char **ptr
)
344 void cpu_outb(CPUState
*env
, int addr
, int val
)
347 if (loglevel
& CPU_LOG_IOPORT
)
348 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
350 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
353 env
->last_io_time
= cpu_get_time_fast();
357 void cpu_outw(CPUState
*env
, int addr
, int val
)
360 if (loglevel
& CPU_LOG_IOPORT
)
361 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
363 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
366 env
->last_io_time
= cpu_get_time_fast();
370 void cpu_outl(CPUState
*env
, int addr
, int val
)
373 if (loglevel
& CPU_LOG_IOPORT
)
374 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
376 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
379 env
->last_io_time
= cpu_get_time_fast();
383 int cpu_inb(CPUState
*env
, int addr
)
386 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
388 if (loglevel
& CPU_LOG_IOPORT
)
389 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
393 env
->last_io_time
= cpu_get_time_fast();
398 int cpu_inw(CPUState
*env
, int addr
)
401 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
403 if (loglevel
& CPU_LOG_IOPORT
)
404 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
408 env
->last_io_time
= cpu_get_time_fast();
413 int cpu_inl(CPUState
*env
, int addr
)
416 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
418 if (loglevel
& CPU_LOG_IOPORT
)
419 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
423 env
->last_io_time
= cpu_get_time_fast();
428 /***********************************************************/
429 void hw_error(const char *fmt
, ...)
435 fprintf(stderr
, "qemu: hardware error: ");
436 vfprintf(stderr
, fmt
, ap
);
437 fprintf(stderr
, "\n");
438 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
439 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
441 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
443 cpu_dump_state(env
, stderr
, fprintf
, 0);
450 /***********************************************************/
453 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
454 static void *qemu_put_kbd_event_opaque
;
455 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
456 static void *qemu_put_mouse_event_opaque
;
457 static int qemu_put_mouse_event_absolute
;
459 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
461 qemu_put_kbd_event_opaque
= opaque
;
462 qemu_put_kbd_event
= func
;
465 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
, int absolute
)
467 qemu_put_mouse_event_opaque
= opaque
;
468 qemu_put_mouse_event
= func
;
469 qemu_put_mouse_event_absolute
= absolute
;
472 void kbd_put_keycode(int keycode
)
474 if (qemu_put_kbd_event
) {
475 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
479 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
481 if (qemu_put_mouse_event
) {
482 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
483 dx
, dy
, dz
, buttons_state
);
487 int kbd_mouse_is_absolute(void)
489 return qemu_put_mouse_event_absolute
;
492 /* compute with 96 bit intermediate result: (a*b)/c */
493 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
498 #ifdef WORDS_BIGENDIAN
508 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
509 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
512 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
516 /***********************************************************/
517 /* real time host monotonic timer */
519 #define QEMU_TIMER_BASE 1000000000LL
523 static int64_t clock_freq
;
525 static void init_get_clock(void)
529 ret
= QueryPerformanceFrequency(&freq
);
531 fprintf(stderr
, "Could not calibrate ticks\n");
534 clock_freq
= freq
.QuadPart
;
537 static int64_t get_clock(void)
540 QueryPerformanceCounter(&ti
);
541 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
546 static int use_rt_clock
;
548 static void init_get_clock(void)
551 #if defined(__linux__)
554 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
561 static int64_t get_clock(void)
563 #if defined(__linux__)
566 clock_gettime(CLOCK_MONOTONIC
, &ts
);
567 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
571 /* XXX: using gettimeofday leads to problems if the date
572 changes, so it should be avoided. */
574 gettimeofday(&tv
, NULL
);
575 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
581 /***********************************************************/
582 /* guest cycle counter */
584 static int64_t cpu_ticks_prev
;
585 static int64_t cpu_ticks_offset
;
586 static int64_t cpu_clock_offset
;
587 static int cpu_ticks_enabled
;
589 /* return the host CPU cycle counter and handle stop/restart */
590 int64_t cpu_get_ticks(void)
592 if (!cpu_ticks_enabled
) {
593 return cpu_ticks_offset
;
596 ticks
= cpu_get_real_ticks();
597 if (cpu_ticks_prev
> ticks
) {
598 /* Note: non increasing ticks may happen if the host uses
600 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
602 cpu_ticks_prev
= ticks
;
603 return ticks
+ cpu_ticks_offset
;
607 /* return the host CPU monotonic timer and handle stop/restart */
608 static int64_t cpu_get_clock(void)
611 if (!cpu_ticks_enabled
) {
612 return cpu_clock_offset
;
615 return ti
+ cpu_clock_offset
;
619 /* enable cpu_get_ticks() */
620 void cpu_enable_ticks(void)
622 if (!cpu_ticks_enabled
) {
623 cpu_ticks_offset
-= cpu_get_real_ticks();
624 cpu_clock_offset
-= get_clock();
625 cpu_ticks_enabled
= 1;
629 /* disable cpu_get_ticks() : the clock is stopped. You must not call
630 cpu_get_ticks() after that. */
631 void cpu_disable_ticks(void)
633 if (cpu_ticks_enabled
) {
634 cpu_ticks_offset
= cpu_get_ticks();
635 cpu_clock_offset
= cpu_get_clock();
636 cpu_ticks_enabled
= 0;
640 /***********************************************************/
643 #define QEMU_TIMER_REALTIME 0
644 #define QEMU_TIMER_VIRTUAL 1
648 /* XXX: add frequency */
656 struct QEMUTimer
*next
;
662 static QEMUTimer
*active_timers
[2];
664 static MMRESULT timerID
;
665 static HANDLE host_alarm
= NULL
;
666 static unsigned int period
= 1;
668 /* frequency of the times() clock tick */
669 static int timer_freq
;
672 QEMUClock
*qemu_new_clock(int type
)
675 clock
= qemu_mallocz(sizeof(QEMUClock
));
682 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
686 ts
= qemu_mallocz(sizeof(QEMUTimer
));
693 void qemu_free_timer(QEMUTimer
*ts
)
698 /* stop a timer, but do not dealloc it */
699 void qemu_del_timer(QEMUTimer
*ts
)
703 /* NOTE: this code must be signal safe because
704 qemu_timer_expired() can be called from a signal. */
705 pt
= &active_timers
[ts
->clock
->type
];
718 /* modify the current timer so that it will be fired when current_time
719 >= expire_time. The corresponding callback will be called. */
720 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
726 /* add the timer in the sorted list */
727 /* NOTE: this code must be signal safe because
728 qemu_timer_expired() can be called from a signal. */
729 pt
= &active_timers
[ts
->clock
->type
];
734 if (t
->expire_time
> expire_time
)
738 ts
->expire_time
= expire_time
;
743 int qemu_timer_pending(QEMUTimer
*ts
)
746 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
753 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
757 return (timer_head
->expire_time
<= current_time
);
760 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
766 if (!ts
|| ts
->expire_time
> current_time
)
768 /* remove timer from the list before calling the callback */
769 *ptimer_head
= ts
->next
;
772 /* run the callback (the timer list can be modified) */
777 int64_t qemu_get_clock(QEMUClock
*clock
)
779 switch(clock
->type
) {
780 case QEMU_TIMER_REALTIME
:
781 return get_clock() / 1000000;
783 case QEMU_TIMER_VIRTUAL
:
784 return cpu_get_clock();
788 static void init_timers(void)
791 ticks_per_sec
= QEMU_TIMER_BASE
;
792 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
793 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
797 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
799 uint64_t expire_time
;
801 if (qemu_timer_pending(ts
)) {
802 expire_time
= ts
->expire_time
;
806 qemu_put_be64(f
, expire_time
);
809 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
811 uint64_t expire_time
;
813 expire_time
= qemu_get_be64(f
);
814 if (expire_time
!= -1) {
815 qemu_mod_timer(ts
, expire_time
);
821 static void timer_save(QEMUFile
*f
, void *opaque
)
823 if (cpu_ticks_enabled
) {
824 hw_error("cannot save state if virtual timers are running");
826 qemu_put_be64s(f
, &cpu_ticks_offset
);
827 qemu_put_be64s(f
, &ticks_per_sec
);
828 qemu_put_be64s(f
, &cpu_clock_offset
);
831 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
833 if (version_id
!= 1 && version_id
!= 2)
835 if (cpu_ticks_enabled
) {
838 qemu_get_be64s(f
, &cpu_ticks_offset
);
839 qemu_get_be64s(f
, &ticks_per_sec
);
840 if (version_id
== 2) {
841 qemu_get_be64s(f
, &cpu_clock_offset
);
847 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
848 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
850 static void host_alarm_handler(int host_signum
)
854 #define DISP_FREQ 1000
856 static int64_t delta_min
= INT64_MAX
;
857 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
859 ti
= qemu_get_clock(vm_clock
);
860 if (last_clock
!= 0) {
861 delta
= ti
- last_clock
;
862 if (delta
< delta_min
)
864 if (delta
> delta_max
)
867 if (++count
== DISP_FREQ
) {
868 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
869 muldiv64(delta_min
, 1000000, ticks_per_sec
),
870 muldiv64(delta_max
, 1000000, ticks_per_sec
),
871 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
872 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
874 delta_min
= INT64_MAX
;
882 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
883 qemu_get_clock(vm_clock
)) ||
884 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
885 qemu_get_clock(rt_clock
))) {
887 SetEvent(host_alarm
);
889 CPUState
*env
= cpu_single_env
;
891 /* stop the currently executing cpu because a timer occured */
892 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
894 if (env
->kqemu_enabled
) {
895 kqemu_cpu_interrupt(env
);
904 #if defined(__linux__)
906 #define RTC_FREQ 1024
910 static int start_rtc_timer(void)
912 rtc_fd
= open("/dev/rtc", O_RDONLY
);
915 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
916 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
917 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
918 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
921 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
926 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
932 static int start_rtc_timer(void)
937 #endif /* !defined(__linux__) */
939 #endif /* !defined(_WIN32) */
941 static void init_timer_alarm(void)
948 ZeroMemory(&tc
, sizeof(TIMECAPS
));
949 timeGetDevCaps(&tc
, sizeof(TIMECAPS
));
950 if (period
< tc
.wPeriodMin
)
951 period
= tc
.wPeriodMin
;
952 timeBeginPeriod(period
);
953 timerID
= timeSetEvent(1, // interval (ms)
954 period
, // resolution
955 host_alarm_handler
, // function
956 (DWORD
)&count
, // user parameter
957 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
959 perror("failed timer alarm");
962 host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
964 perror("failed CreateEvent");
967 qemu_add_wait_object(host_alarm
, NULL
, NULL
);
969 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
972 struct sigaction act
;
973 struct itimerval itv
;
975 /* get times() syscall frequency */
976 timer_freq
= sysconf(_SC_CLK_TCK
);
979 sigfillset(&act
.sa_mask
);
981 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
982 act
.sa_flags
|= SA_ONSTACK
;
984 act
.sa_handler
= host_alarm_handler
;
985 sigaction(SIGALRM
, &act
, NULL
);
987 itv
.it_interval
.tv_sec
= 0;
988 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
989 itv
.it_value
.tv_sec
= 0;
990 itv
.it_value
.tv_usec
= 10 * 1000;
991 setitimer(ITIMER_REAL
, &itv
, NULL
);
992 /* we probe the tick duration of the kernel to inform the user if
993 the emulated kernel requested a too high timer frequency */
994 getitimer(ITIMER_REAL
, &itv
);
996 #if defined(__linux__)
997 /* XXX: force /dev/rtc usage because even 2.6 kernels may not
998 have timers with 1 ms resolution. The correct solution will
999 be to use the POSIX real time timers available in recent
1001 if (itv
.it_interval
.tv_usec
> 1000 || 1) {
1002 /* try to use /dev/rtc to have a faster timer */
1003 if (start_rtc_timer() < 0)
1005 /* disable itimer */
1006 itv
.it_interval
.tv_sec
= 0;
1007 itv
.it_interval
.tv_usec
= 0;
1008 itv
.it_value
.tv_sec
= 0;
1009 itv
.it_value
.tv_usec
= 0;
1010 setitimer(ITIMER_REAL
, &itv
, NULL
);
1013 sigaction(SIGIO
, &act
, NULL
);
1014 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1015 fcntl(rtc_fd
, F_SETOWN
, getpid());
1017 #endif /* defined(__linux__) */
1020 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1021 PIT_FREQ
) / 1000000;
1027 void quit_timers(void)
1030 timeKillEvent(timerID
);
1031 timeEndPeriod(period
);
1033 CloseHandle(host_alarm
);
1039 /***********************************************************/
1040 /* character device */
1042 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1044 return s
->chr_write(s
, buf
, len
);
1047 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1051 return s
->chr_ioctl(s
, cmd
, arg
);
1054 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1059 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1060 qemu_chr_write(s
, buf
, strlen(buf
));
1064 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1066 if (s
->chr_send_event
)
1067 s
->chr_send_event(s
, event
);
1070 void qemu_chr_add_read_handler(CharDriverState
*s
,
1071 IOCanRWHandler
*fd_can_read
,
1072 IOReadHandler
*fd_read
, void *opaque
)
1074 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
1077 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
1079 s
->chr_event
= chr_event
;
1082 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1087 static void null_chr_add_read_handler(CharDriverState
*chr
,
1088 IOCanRWHandler
*fd_can_read
,
1089 IOReadHandler
*fd_read
, void *opaque
)
1093 CharDriverState
*qemu_chr_open_null(void)
1095 CharDriverState
*chr
;
1097 chr
= qemu_mallocz(sizeof(CharDriverState
));
1100 chr
->chr_write
= null_chr_write
;
1101 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1107 static void socket_cleanup(void)
1112 static int socket_init(void)
1117 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1119 err
= WSAGetLastError();
1120 fprintf(stderr
, "WSAStartup: %d\n", err
);
1123 atexit(socket_cleanup
);
1127 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1133 ret
= send(fd
, buf
, len
, 0);
1136 errno
= WSAGetLastError();
1137 if (errno
!= WSAEWOULDBLOCK
) {
1140 } else if (ret
== 0) {
1150 void socket_set_nonblock(int fd
)
1152 unsigned long opt
= 1;
1153 ioctlsocket(fd
, FIONBIO
, &opt
);
1158 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1164 ret
= write(fd
, buf
, len
);
1166 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1168 } else if (ret
== 0) {
1178 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1180 return unix_write(fd
, buf
, len1
);
1183 void socket_set_nonblock(int fd
)
1185 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1187 #endif /* !_WIN32 */
1193 IOCanRWHandler
*fd_can_read
;
1194 IOReadHandler
*fd_read
;
1199 #define STDIO_MAX_CLIENTS 2
1201 static int stdio_nb_clients
;
1202 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1204 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1206 FDCharDriver
*s
= chr
->opaque
;
1207 return unix_write(s
->fd_out
, buf
, len
);
1210 static int fd_chr_read_poll(void *opaque
)
1212 CharDriverState
*chr
= opaque
;
1213 FDCharDriver
*s
= chr
->opaque
;
1215 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
1219 static void fd_chr_read(void *opaque
)
1221 CharDriverState
*chr
= opaque
;
1222 FDCharDriver
*s
= chr
->opaque
;
1227 if (len
> s
->max_size
)
1231 size
= read(s
->fd_in
, buf
, len
);
1233 /* FD has been closed. Remove it from the active list. */
1234 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1238 s
->fd_read(s
->fd_opaque
, buf
, size
);
1242 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1243 IOCanRWHandler
*fd_can_read
,
1244 IOReadHandler
*fd_read
, void *opaque
)
1246 FDCharDriver
*s
= chr
->opaque
;
1248 if (s
->fd_in
>= 0) {
1249 s
->fd_can_read
= fd_can_read
;
1250 s
->fd_read
= fd_read
;
1251 s
->fd_opaque
= opaque
;
1252 if (nographic
&& s
->fd_in
== 0) {
1254 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1255 fd_chr_read
, NULL
, chr
);
1260 /* open a character device to a unix fd */
1261 CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1263 CharDriverState
*chr
;
1266 chr
= qemu_mallocz(sizeof(CharDriverState
));
1269 s
= qemu_mallocz(sizeof(FDCharDriver
));
1277 chr
->chr_write
= fd_chr_write
;
1278 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1282 CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1286 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1289 return qemu_chr_open_fd(-1, fd_out
);
1292 CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1296 fd
= open(filename
, O_RDWR
| O_BINARY
);
1299 return qemu_chr_open_fd(fd
, fd
);
1303 /* for STDIO, we handle the case where several clients use it
1306 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1308 #define TERM_FIFO_MAX_SIZE 1
1310 static int term_got_escape
, client_index
;
1311 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1312 static int term_fifo_size
;
1313 static int term_timestamps
;
1314 static int64_t term_timestamps_start
;
1316 void term_print_help(void)
1319 "C-a h print this help\n"
1320 "C-a x exit emulator\n"
1321 "C-a s save disk data back to file (if -snapshot)\n"
1322 "C-a b send break (magic sysrq)\n"
1323 "C-a t toggle console timestamps\n"
1324 "C-a c switch between console and monitor\n"
1325 "C-a C-a send C-a\n"
1329 /* called when a char is received */
1330 static void stdio_received_byte(int ch
)
1332 if (term_got_escape
) {
1333 term_got_escape
= 0;
1344 for (i
= 0; i
< MAX_DISKS
; i
++) {
1346 bdrv_commit(bs_table
[i
]);
1351 if (client_index
< stdio_nb_clients
) {
1352 CharDriverState
*chr
;
1355 chr
= stdio_clients
[client_index
];
1357 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1362 if (client_index
>= stdio_nb_clients
)
1364 if (client_index
== 0) {
1365 /* send a new line in the monitor to get the prompt */
1371 term_timestamps
= !term_timestamps
;
1372 term_timestamps_start
= -1;
1377 } else if (ch
== TERM_ESCAPE
) {
1378 term_got_escape
= 1;
1381 if (client_index
< stdio_nb_clients
) {
1383 CharDriverState
*chr
;
1386 chr
= stdio_clients
[client_index
];
1388 if (s
->fd_can_read(s
->fd_opaque
) > 0) {
1390 s
->fd_read(s
->fd_opaque
, buf
, 1);
1391 } else if (term_fifo_size
== 0) {
1392 term_fifo
[term_fifo_size
++] = ch
;
1398 static int stdio_read_poll(void *opaque
)
1400 CharDriverState
*chr
;
1403 if (client_index
< stdio_nb_clients
) {
1404 chr
= stdio_clients
[client_index
];
1406 /* try to flush the queue if needed */
1407 if (term_fifo_size
!= 0 && s
->fd_can_read(s
->fd_opaque
) > 0) {
1408 s
->fd_read(s
->fd_opaque
, term_fifo
, 1);
1411 /* see if we can absorb more chars */
1412 if (term_fifo_size
== 0)
1421 static void stdio_read(void *opaque
)
1426 size
= read(0, buf
, 1);
1428 /* stdin has been closed. Remove it from the active list. */
1429 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
1433 stdio_received_byte(buf
[0]);
1436 static int stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1438 FDCharDriver
*s
= chr
->opaque
;
1439 if (!term_timestamps
) {
1440 return unix_write(s
->fd_out
, buf
, len
);
1445 for(i
= 0; i
< len
; i
++) {
1446 unix_write(s
->fd_out
, buf
+ i
, 1);
1447 if (buf
[i
] == '\n') {
1452 if (term_timestamps_start
== -1)
1453 term_timestamps_start
= ti
;
1454 ti
-= term_timestamps_start
;
1455 secs
= ti
/ 1000000000;
1456 snprintf(buf1
, sizeof(buf1
),
1457 "[%02d:%02d:%02d.%03d] ",
1461 (int)((ti
/ 1000000) % 1000));
1462 unix_write(s
->fd_out
, buf1
, strlen(buf1
));
1469 /* init terminal so that we can grab keys */
1470 static struct termios oldtty
;
1471 static int old_fd0_flags
;
1473 static void term_exit(void)
1475 tcsetattr (0, TCSANOW
, &oldtty
);
1476 fcntl(0, F_SETFL
, old_fd0_flags
);
1479 static void term_init(void)
1483 tcgetattr (0, &tty
);
1485 old_fd0_flags
= fcntl(0, F_GETFL
);
1487 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1488 |INLCR
|IGNCR
|ICRNL
|IXON
);
1489 tty
.c_oflag
|= OPOST
;
1490 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1491 /* if graphical mode, we allow Ctrl-C handling */
1493 tty
.c_lflag
&= ~ISIG
;
1494 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1497 tty
.c_cc
[VTIME
] = 0;
1499 tcsetattr (0, TCSANOW
, &tty
);
1503 fcntl(0, F_SETFL
, O_NONBLOCK
);
1506 CharDriverState
*qemu_chr_open_stdio(void)
1508 CharDriverState
*chr
;
1511 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1513 chr
= qemu_chr_open_fd(0, 1);
1514 chr
->chr_write
= stdio_write
;
1515 if (stdio_nb_clients
== 0)
1516 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, NULL
);
1517 client_index
= stdio_nb_clients
;
1519 if (stdio_nb_clients
!= 0)
1521 chr
= qemu_chr_open_fd(0, 1);
1523 stdio_clients
[stdio_nb_clients
++] = chr
;
1524 if (stdio_nb_clients
== 1) {
1525 /* set the terminal in raw mode */
1531 #if defined(__linux__)
1532 CharDriverState
*qemu_chr_open_pty(void)
1535 char slave_name
[1024];
1536 int master_fd
, slave_fd
;
1538 /* Not satisfying */
1539 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1543 /* Disabling local echo and line-buffered output */
1544 tcgetattr (master_fd
, &tty
);
1545 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1547 tty
.c_cc
[VTIME
] = 0;
1548 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1550 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1551 return qemu_chr_open_fd(master_fd
, master_fd
);
1554 static void tty_serial_init(int fd
, int speed
,
1555 int parity
, int data_bits
, int stop_bits
)
1561 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1562 speed
, parity
, data_bits
, stop_bits
);
1564 tcgetattr (fd
, &tty
);
1606 cfsetispeed(&tty
, spd
);
1607 cfsetospeed(&tty
, spd
);
1609 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1610 |INLCR
|IGNCR
|ICRNL
|IXON
);
1611 tty
.c_oflag
|= OPOST
;
1612 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1613 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1634 tty
.c_cflag
|= PARENB
;
1637 tty
.c_cflag
|= PARENB
| PARODD
;
1641 tty
.c_cflag
|= CSTOPB
;
1643 tcsetattr (fd
, TCSANOW
, &tty
);
1646 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1648 FDCharDriver
*s
= chr
->opaque
;
1651 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1653 QEMUSerialSetParams
*ssp
= arg
;
1654 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1655 ssp
->data_bits
, ssp
->stop_bits
);
1658 case CHR_IOCTL_SERIAL_SET_BREAK
:
1660 int enable
= *(int *)arg
;
1662 tcsendbreak(s
->fd_in
, 1);
1671 CharDriverState
*qemu_chr_open_tty(const char *filename
)
1673 CharDriverState
*chr
;
1676 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1679 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1680 tty_serial_init(fd
, 115200, 'N', 8, 1);
1681 chr
= qemu_chr_open_fd(fd
, fd
);
1684 chr
->chr_ioctl
= tty_serial_ioctl
;
1688 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1690 int fd
= (int)chr
->opaque
;
1694 case CHR_IOCTL_PP_READ_DATA
:
1695 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1697 *(uint8_t *)arg
= b
;
1699 case CHR_IOCTL_PP_WRITE_DATA
:
1700 b
= *(uint8_t *)arg
;
1701 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1704 case CHR_IOCTL_PP_READ_CONTROL
:
1705 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1707 *(uint8_t *)arg
= b
;
1709 case CHR_IOCTL_PP_WRITE_CONTROL
:
1710 b
= *(uint8_t *)arg
;
1711 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1714 case CHR_IOCTL_PP_READ_STATUS
:
1715 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1717 *(uint8_t *)arg
= b
;
1725 CharDriverState
*qemu_chr_open_pp(const char *filename
)
1727 CharDriverState
*chr
;
1730 fd
= open(filename
, O_RDWR
);
1734 if (ioctl(fd
, PPCLAIM
) < 0) {
1739 chr
= qemu_mallocz(sizeof(CharDriverState
));
1744 chr
->opaque
= (void *)fd
;
1745 chr
->chr_write
= null_chr_write
;
1746 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1747 chr
->chr_ioctl
= pp_ioctl
;
1752 CharDriverState
*qemu_chr_open_pty(void)
1758 #endif /* !defined(_WIN32) */
1762 IOCanRWHandler
*fd_can_read
;
1763 IOReadHandler
*fd_read
;
1766 HANDLE hcom
, hrecv
, hsend
;
1767 OVERLAPPED orecv
, osend
;
1772 #define NSENDBUF 2048
1773 #define NRECVBUF 2048
1774 #define MAXCONNECT 1
1775 #define NTIMEOUT 5000
1777 static int win_chr_poll(void *opaque
);
1778 static int win_chr_pipe_poll(void *opaque
);
1780 static void win_chr_close2(WinCharState
*s
)
1783 CloseHandle(s
->hsend
);
1787 CloseHandle(s
->hrecv
);
1791 CloseHandle(s
->hcom
);
1795 qemu_del_polling_cb(win_chr_pipe_poll
, s
);
1797 qemu_del_polling_cb(win_chr_poll
, s
);
1800 static void win_chr_close(CharDriverState
*chr
)
1802 WinCharState
*s
= chr
->opaque
;
1806 static int win_chr_init(WinCharState
*s
, const char *filename
)
1809 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1814 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1816 fprintf(stderr
, "Failed CreateEvent\n");
1819 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1821 fprintf(stderr
, "Failed CreateEvent\n");
1825 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1826 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1827 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1828 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1833 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1834 fprintf(stderr
, "Failed SetupComm\n");
1838 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1839 size
= sizeof(COMMCONFIG
);
1840 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1841 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1842 CommConfigDialog(filename
, NULL
, &comcfg
);
1844 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1845 fprintf(stderr
, "Failed SetCommState\n");
1849 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1850 fprintf(stderr
, "Failed SetCommMask\n");
1854 cto
.ReadIntervalTimeout
= MAXDWORD
;
1855 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1856 fprintf(stderr
, "Failed SetCommTimeouts\n");
1860 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1861 fprintf(stderr
, "Failed ClearCommError\n");
1864 qemu_add_polling_cb(win_chr_poll
, s
);
1872 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1874 WinCharState
*s
= chr
->opaque
;
1875 DWORD len
, ret
, size
, err
;
1878 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1879 s
->osend
.hEvent
= s
->hsend
;
1882 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1884 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1886 err
= GetLastError();
1887 if (err
== ERROR_IO_PENDING
) {
1888 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1906 static int win_chr_read_poll(WinCharState
*s
)
1908 s
->max_size
= s
->fd_can_read(s
->win_opaque
);
1912 static void win_chr_readfile(WinCharState
*s
)
1918 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1919 s
->orecv
.hEvent
= s
->hrecv
;
1920 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1922 err
= GetLastError();
1923 if (err
== ERROR_IO_PENDING
) {
1924 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1929 s
->fd_read(s
->win_opaque
, buf
, size
);
1933 static void win_chr_read(WinCharState
*s
)
1935 if (s
->len
> s
->max_size
)
1936 s
->len
= s
->max_size
;
1940 win_chr_readfile(s
);
1943 static int win_chr_poll(void *opaque
)
1945 WinCharState
*s
= opaque
;
1949 ClearCommError(s
->hcom
, &comerr
, &status
);
1950 if (status
.cbInQue
> 0) {
1951 s
->len
= status
.cbInQue
;
1952 win_chr_read_poll(s
);
1959 static void win_chr_add_read_handler(CharDriverState
*chr
,
1960 IOCanRWHandler
*fd_can_read
,
1961 IOReadHandler
*fd_read
, void *opaque
)
1963 WinCharState
*s
= chr
->opaque
;
1965 s
->fd_can_read
= fd_can_read
;
1966 s
->fd_read
= fd_read
;
1967 s
->win_opaque
= opaque
;
1970 CharDriverState
*qemu_chr_open_win(const char *filename
)
1972 CharDriverState
*chr
;
1975 chr
= qemu_mallocz(sizeof(CharDriverState
));
1978 s
= qemu_mallocz(sizeof(WinCharState
));
1984 chr
->chr_write
= win_chr_write
;
1985 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
1986 chr
->chr_close
= win_chr_close
;
1988 if (win_chr_init(s
, filename
) < 0) {
1996 static int win_chr_pipe_poll(void *opaque
)
1998 WinCharState
*s
= opaque
;
2001 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2004 win_chr_read_poll(s
);
2011 static int win_chr_pipe_init(WinCharState
*s
, const char *filename
)
2020 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2022 fprintf(stderr
, "Failed CreateEvent\n");
2025 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2027 fprintf(stderr
, "Failed CreateEvent\n");
2031 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2032 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2033 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2035 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2036 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2037 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2042 ZeroMemory(&ov
, sizeof(ov
));
2043 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2044 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2046 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2050 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2052 fprintf(stderr
, "Failed GetOverlappedResult\n");
2054 CloseHandle(ov
.hEvent
);
2061 CloseHandle(ov
.hEvent
);
2064 qemu_add_polling_cb(win_chr_pipe_poll
, s
);
2073 CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2075 CharDriverState
*chr
;
2078 chr
= qemu_mallocz(sizeof(CharDriverState
));
2081 s
= qemu_mallocz(sizeof(WinCharState
));
2087 chr
->chr_write
= win_chr_write
;
2088 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2089 chr
->chr_close
= win_chr_close
;
2091 if (win_chr_pipe_init(s
, filename
) < 0) {
2099 CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2101 CharDriverState
*chr
;
2104 chr
= qemu_mallocz(sizeof(CharDriverState
));
2107 s
= qemu_mallocz(sizeof(WinCharState
));
2114 chr
->chr_write
= win_chr_write
;
2115 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2119 CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2123 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2124 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2125 if (fd_out
== INVALID_HANDLE_VALUE
)
2128 return qemu_chr_open_win_file(fd_out
);
2132 /***********************************************************/
2133 /* UDP Net console */
2136 IOCanRWHandler
*fd_can_read
;
2137 IOReadHandler
*fd_read
;
2140 struct sockaddr_in daddr
;
2147 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2149 NetCharDriver
*s
= chr
->opaque
;
2151 return sendto(s
->fd
, buf
, len
, 0,
2152 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2155 static int udp_chr_read_poll(void *opaque
)
2157 CharDriverState
*chr
= opaque
;
2158 NetCharDriver
*s
= chr
->opaque
;
2160 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2162 /* If there were any stray characters in the queue process them
2165 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2166 s
->fd_read(s
->fd_opaque
, &s
->buf
[s
->bufptr
], 1);
2168 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2173 static void udp_chr_read(void *opaque
)
2175 CharDriverState
*chr
= opaque
;
2176 NetCharDriver
*s
= chr
->opaque
;
2178 if (s
->max_size
== 0)
2180 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2181 s
->bufptr
= s
->bufcnt
;
2186 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2187 s
->fd_read(s
->fd_opaque
, &s
->buf
[s
->bufptr
], 1);
2189 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2193 static void udp_chr_add_read_handler(CharDriverState
*chr
,
2194 IOCanRWHandler
*fd_can_read
,
2195 IOReadHandler
*fd_read
, void *opaque
)
2197 NetCharDriver
*s
= chr
->opaque
;
2200 s
->fd_can_read
= fd_can_read
;
2201 s
->fd_read
= fd_read
;
2202 s
->fd_opaque
= opaque
;
2203 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2204 udp_chr_read
, NULL
, chr
);
2208 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2209 int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2210 int parse_host_src_port(struct sockaddr_in
*haddr
,
2211 struct sockaddr_in
*saddr
,
2214 CharDriverState
*qemu_chr_open_udp(const char *def
)
2216 CharDriverState
*chr
= NULL
;
2217 NetCharDriver
*s
= NULL
;
2219 struct sockaddr_in saddr
;
2221 chr
= qemu_mallocz(sizeof(CharDriverState
));
2224 s
= qemu_mallocz(sizeof(NetCharDriver
));
2228 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2230 perror("socket(PF_INET, SOCK_DGRAM)");
2234 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2235 printf("Could not parse: %s\n", def
);
2239 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2249 chr
->chr_write
= udp_chr_write
;
2250 chr
->chr_add_read_handler
= udp_chr_add_read_handler
;
2263 /***********************************************************/
2264 /* TCP Net console */
2267 IOCanRWHandler
*fd_can_read
;
2268 IOReadHandler
*fd_read
;
2277 static void tcp_chr_accept(void *opaque
);
2279 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2281 TCPCharDriver
*s
= chr
->opaque
;
2283 return send_all(s
->fd
, buf
, len
);
2285 /* XXX: indicate an error ? */
2290 static int tcp_chr_read_poll(void *opaque
)
2292 CharDriverState
*chr
= opaque
;
2293 TCPCharDriver
*s
= chr
->opaque
;
2296 if (!s
->fd_can_read
)
2298 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2303 #define IAC_BREAK 243
2304 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2306 char *buf
, int *size
)
2308 /* Handle any telnet client's basic IAC options to satisfy char by
2309 * char mode with no echo. All IAC options will be removed from
2310 * the buf and the do_telnetopt variable will be used to track the
2311 * state of the width of the IAC information.
2313 * IAC commands come in sets of 3 bytes with the exception of the
2314 * "IAC BREAK" command and the double IAC.
2320 for (i
= 0; i
< *size
; i
++) {
2321 if (s
->do_telnetopt
> 1) {
2322 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2323 /* Double IAC means send an IAC */
2327 s
->do_telnetopt
= 1;
2329 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2330 /* Handle IAC break commands by sending a serial break */
2331 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
2336 if (s
->do_telnetopt
>= 4) {
2337 s
->do_telnetopt
= 1;
2340 if ((unsigned char)buf
[i
] == IAC
) {
2341 s
->do_telnetopt
= 2;
2352 static void tcp_chr_read(void *opaque
)
2354 CharDriverState
*chr
= opaque
;
2355 TCPCharDriver
*s
= chr
->opaque
;
2359 if (!s
->connected
|| s
->max_size
<= 0)
2362 if (len
> s
->max_size
)
2364 size
= recv(s
->fd
, buf
, len
, 0);
2366 /* connection closed */
2368 if (s
->listen_fd
>= 0) {
2369 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2371 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2374 } else if (size
> 0) {
2375 if (s
->do_telnetopt
)
2376 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2378 s
->fd_read(s
->fd_opaque
, buf
, size
);
2382 static void tcp_chr_add_read_handler(CharDriverState
*chr
,
2383 IOCanRWHandler
*fd_can_read
,
2384 IOReadHandler
*fd_read
, void *opaque
)
2386 TCPCharDriver
*s
= chr
->opaque
;
2388 s
->fd_can_read
= fd_can_read
;
2389 s
->fd_read
= fd_read
;
2390 s
->fd_opaque
= opaque
;
2393 static void tcp_chr_connect(void *opaque
)
2395 CharDriverState
*chr
= opaque
;
2396 TCPCharDriver
*s
= chr
->opaque
;
2399 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2400 tcp_chr_read
, NULL
, chr
);
2403 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2404 static void tcp_chr_telnet_init(int fd
)
2407 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2408 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2409 send(fd
, (char *)buf
, 3, 0);
2410 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2411 send(fd
, (char *)buf
, 3, 0);
2412 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2413 send(fd
, (char *)buf
, 3, 0);
2414 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2415 send(fd
, (char *)buf
, 3, 0);
2418 static void tcp_chr_accept(void *opaque
)
2420 CharDriverState
*chr
= opaque
;
2421 TCPCharDriver
*s
= chr
->opaque
;
2422 struct sockaddr_in saddr
;
2424 struct sockaddr_un uaddr
;
2426 struct sockaddr
*addr
;
2433 len
= sizeof(uaddr
);
2434 addr
= (struct sockaddr
*)&uaddr
;
2438 len
= sizeof(saddr
);
2439 addr
= (struct sockaddr
*)&saddr
;
2441 fd
= accept(s
->listen_fd
, addr
, &len
);
2442 if (fd
< 0 && errno
!= EINTR
) {
2444 } else if (fd
>= 0) {
2445 if (s
->do_telnetopt
)
2446 tcp_chr_telnet_init(fd
);
2450 socket_set_nonblock(fd
);
2452 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2453 tcp_chr_connect(chr
);
2456 static void tcp_chr_close(CharDriverState
*chr
)
2458 TCPCharDriver
*s
= chr
->opaque
;
2461 if (s
->listen_fd
>= 0)
2462 closesocket(s
->listen_fd
);
2466 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2470 CharDriverState
*chr
= NULL
;
2471 TCPCharDriver
*s
= NULL
;
2472 int fd
= -1, ret
, err
, val
;
2474 int is_waitconnect
= 1;
2476 struct sockaddr_in saddr
;
2478 struct sockaddr_un uaddr
;
2480 struct sockaddr
*addr
;
2485 addr
= (struct sockaddr
*)&uaddr
;
2486 addrlen
= sizeof(uaddr
);
2487 if (parse_unix_path(&uaddr
, host_str
) < 0)
2492 addr
= (struct sockaddr
*)&saddr
;
2493 addrlen
= sizeof(saddr
);
2494 if (parse_host_port(&saddr
, host_str
) < 0)
2499 while((ptr
= strchr(ptr
,','))) {
2501 if (!strncmp(ptr
,"server",6)) {
2503 } else if (!strncmp(ptr
,"nowait",6)) {
2506 printf("Unknown option: %s\n", ptr
);
2513 chr
= qemu_mallocz(sizeof(CharDriverState
));
2516 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2522 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2525 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2530 if (!is_waitconnect
)
2531 socket_set_nonblock(fd
);
2536 s
->is_unix
= is_unix
;
2539 chr
->chr_write
= tcp_chr_write
;
2540 chr
->chr_add_read_handler
= tcp_chr_add_read_handler
;
2541 chr
->chr_close
= tcp_chr_close
;
2544 /* allow fast reuse */
2548 strncpy(path
, uaddr
.sun_path
, 108);
2555 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2558 ret
= bind(fd
, addr
, addrlen
);
2562 ret
= listen(fd
, 0);
2567 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2569 s
->do_telnetopt
= 1;
2572 ret
= connect(fd
, addr
, addrlen
);
2574 err
= socket_error();
2575 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2576 } else if (err
== EINPROGRESS
) {
2588 tcp_chr_connect(chr
);
2590 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2593 if (is_listen
&& is_waitconnect
) {
2594 printf("QEMU waiting for connection on: %s\n", host_str
);
2595 tcp_chr_accept(chr
);
2596 socket_set_nonblock(s
->listen_fd
);
2608 CharDriverState
*qemu_chr_open(const char *filename
)
2612 if (!strcmp(filename
, "vc")) {
2613 return text_console_init(&display_state
);
2614 } else if (!strcmp(filename
, "null")) {
2615 return qemu_chr_open_null();
2617 if (strstart(filename
, "tcp:", &p
)) {
2618 return qemu_chr_open_tcp(p
, 0, 0);
2620 if (strstart(filename
, "telnet:", &p
)) {
2621 return qemu_chr_open_tcp(p
, 1, 0);
2623 if (strstart(filename
, "udp:", &p
)) {
2624 return qemu_chr_open_udp(p
);
2627 if (strstart(filename
, "unix:", &p
)) {
2628 return qemu_chr_open_tcp(p
, 0, 1);
2629 } else if (strstart(filename
, "file:", &p
)) {
2630 return qemu_chr_open_file_out(p
);
2631 } else if (strstart(filename
, "pipe:", &p
)) {
2632 return qemu_chr_open_pipe(p
);
2633 } else if (!strcmp(filename
, "pty")) {
2634 return qemu_chr_open_pty();
2635 } else if (!strcmp(filename
, "stdio")) {
2636 return qemu_chr_open_stdio();
2639 #if defined(__linux__)
2640 if (strstart(filename
, "/dev/parport", NULL
)) {
2641 return qemu_chr_open_pp(filename
);
2643 if (strstart(filename
, "/dev/", NULL
)) {
2644 return qemu_chr_open_tty(filename
);
2648 if (strstart(filename
, "COM", NULL
)) {
2649 return qemu_chr_open_win(filename
);
2651 if (strstart(filename
, "pipe:", &p
)) {
2652 return qemu_chr_open_win_pipe(p
);
2654 if (strstart(filename
, "file:", &p
)) {
2655 return qemu_chr_open_win_file_out(p
);
2663 void qemu_chr_close(CharDriverState
*chr
)
2666 chr
->chr_close(chr
);
2669 /***********************************************************/
2670 /* network device redirectors */
2672 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2676 for(i
=0;i
<size
;i
+=16) {
2680 fprintf(f
, "%08x ", i
);
2683 fprintf(f
, " %02x", buf
[i
+j
]);
2688 for(j
=0;j
<len
;j
++) {
2690 if (c
< ' ' || c
> '~')
2692 fprintf(f
, "%c", c
);
2698 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
2701 for(i
= 0; i
< 6; i
++) {
2702 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2715 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
2720 p1
= strchr(p
, sep
);
2726 if (len
> buf_size
- 1)
2728 memcpy(buf
, p
, len
);
2735 int parse_host_src_port(struct sockaddr_in
*haddr
,
2736 struct sockaddr_in
*saddr
,
2737 const char *input_str
)
2739 char *str
= strdup(input_str
);
2740 char *host_str
= str
;
2745 * Chop off any extra arguments at the end of the string which
2746 * would start with a comma, then fill in the src port information
2747 * if it was provided else use the "any address" and "any port".
2749 if ((ptr
= strchr(str
,',')))
2752 if ((src_str
= strchr(input_str
,'@'))) {
2757 if (parse_host_port(haddr
, host_str
) < 0)
2760 if (!src_str
|| *src_str
== '\0')
2763 if (parse_host_port(saddr
, src_str
) < 0)
2774 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
2782 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2784 saddr
->sin_family
= AF_INET
;
2785 if (buf
[0] == '\0') {
2786 saddr
->sin_addr
.s_addr
= 0;
2788 if (isdigit(buf
[0])) {
2789 if (!inet_aton(buf
, &saddr
->sin_addr
))
2792 if ((he
= gethostbyname(buf
)) == NULL
)
2794 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
2797 port
= strtol(p
, (char **)&r
, 0);
2800 saddr
->sin_port
= htons(port
);
2804 int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
2809 len
= MIN(108, strlen(str
));
2810 p
= strchr(str
, ',');
2812 len
= MIN(len
, p
- str
);
2814 memset(uaddr
, 0, sizeof(*uaddr
));
2816 uaddr
->sun_family
= AF_UNIX
;
2817 memcpy(uaddr
->sun_path
, str
, len
);
2822 /* find or alloc a new VLAN */
2823 VLANState
*qemu_find_vlan(int id
)
2825 VLANState
**pvlan
, *vlan
;
2826 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2830 vlan
= qemu_mallocz(sizeof(VLANState
));
2835 pvlan
= &first_vlan
;
2836 while (*pvlan
!= NULL
)
2837 pvlan
= &(*pvlan
)->next
;
2842 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
2843 IOReadHandler
*fd_read
,
2844 IOCanRWHandler
*fd_can_read
,
2847 VLANClientState
*vc
, **pvc
;
2848 vc
= qemu_mallocz(sizeof(VLANClientState
));
2851 vc
->fd_read
= fd_read
;
2852 vc
->fd_can_read
= fd_can_read
;
2853 vc
->opaque
= opaque
;
2857 pvc
= &vlan
->first_client
;
2858 while (*pvc
!= NULL
)
2859 pvc
= &(*pvc
)->next
;
2864 int qemu_can_send_packet(VLANClientState
*vc1
)
2866 VLANState
*vlan
= vc1
->vlan
;
2867 VLANClientState
*vc
;
2869 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2871 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
2878 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
2880 VLANState
*vlan
= vc1
->vlan
;
2881 VLANClientState
*vc
;
2884 printf("vlan %d send:\n", vlan
->id
);
2885 hex_dump(stdout
, buf
, size
);
2887 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2889 vc
->fd_read(vc
->opaque
, buf
, size
);
2894 #if defined(CONFIG_SLIRP)
2896 /* slirp network adapter */
2898 static int slirp_inited
;
2899 static VLANClientState
*slirp_vc
;
2901 int slirp_can_output(void)
2903 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
2906 void slirp_output(const uint8_t *pkt
, int pkt_len
)
2909 printf("slirp output:\n");
2910 hex_dump(stdout
, pkt
, pkt_len
);
2914 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
2917 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
2920 printf("slirp input:\n");
2921 hex_dump(stdout
, buf
, size
);
2923 slirp_input(buf
, size
);
2926 static int net_slirp_init(VLANState
*vlan
)
2928 if (!slirp_inited
) {
2932 slirp_vc
= qemu_new_vlan_client(vlan
,
2933 slirp_receive
, NULL
, NULL
);
2934 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
2938 static void net_slirp_redir(const char *redir_str
)
2943 struct in_addr guest_addr
;
2944 int host_port
, guest_port
;
2946 if (!slirp_inited
) {
2952 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2954 if (!strcmp(buf
, "tcp")) {
2956 } else if (!strcmp(buf
, "udp")) {
2962 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2964 host_port
= strtol(buf
, &r
, 0);
2968 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2970 if (buf
[0] == '\0') {
2971 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
2973 if (!inet_aton(buf
, &guest_addr
))
2976 guest_port
= strtol(p
, &r
, 0);
2980 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
2981 fprintf(stderr
, "qemu: could not set up redirection\n");
2986 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
2994 static void smb_exit(void)
2998 char filename
[1024];
3000 /* erase all the files in the directory */
3001 d
= opendir(smb_dir
);
3006 if (strcmp(de
->d_name
, ".") != 0 &&
3007 strcmp(de
->d_name
, "..") != 0) {
3008 snprintf(filename
, sizeof(filename
), "%s/%s",
3009 smb_dir
, de
->d_name
);
3017 /* automatic user mode samba server configuration */
3018 void net_slirp_smb(const char *exported_dir
)
3020 char smb_conf
[1024];
3021 char smb_cmdline
[1024];
3024 if (!slirp_inited
) {
3029 /* XXX: better tmp dir construction */
3030 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3031 if (mkdir(smb_dir
, 0700) < 0) {
3032 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3035 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3037 f
= fopen(smb_conf
, "w");
3039 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3046 "socket address=127.0.0.1\n"
3047 "pid directory=%s\n"
3048 "lock directory=%s\n"
3049 "log file=%s/log.smbd\n"
3050 "smb passwd file=%s/smbpasswd\n"
3051 "security = share\n"
3066 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "/usr/sbin/smbd -s %s",
3069 slirp_add_exec(0, smb_cmdline
, 4, 139);
3072 #endif /* !defined(_WIN32) */
3074 #endif /* CONFIG_SLIRP */
3076 #if !defined(_WIN32)
3078 typedef struct TAPState
{
3079 VLANClientState
*vc
;
3083 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3085 TAPState
*s
= opaque
;
3088 ret
= write(s
->fd
, buf
, size
);
3089 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3096 static void tap_send(void *opaque
)
3098 TAPState
*s
= opaque
;
3102 size
= read(s
->fd
, buf
, sizeof(buf
));
3104 qemu_send_packet(s
->vc
, buf
, size
);
3110 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3114 s
= qemu_mallocz(sizeof(TAPState
));
3118 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3119 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3120 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3125 static int tap_open(char *ifname
, int ifname_size
)
3131 fd
= open("/dev/tap", O_RDWR
);
3133 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3138 dev
= devname(s
.st_rdev
, S_IFCHR
);
3139 pstrcpy(ifname
, ifname_size
, dev
);
3141 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3144 #elif defined(__sun__)
3145 static int tap_open(char *ifname
, int ifname_size
)
3147 fprintf(stderr
, "warning: tap_open not yet implemented\n");
3151 static int tap_open(char *ifname
, int ifname_size
)
3156 fd
= open("/dev/net/tun", O_RDWR
);
3158 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
3161 memset(&ifr
, 0, sizeof(ifr
));
3162 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
3163 if (ifname
[0] != '\0')
3164 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
3166 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
3167 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
3169 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
3173 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
3174 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3179 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
3180 const char *setup_script
)
3183 int pid
, status
, fd
;
3188 if (ifname1
!= NULL
)
3189 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
3192 fd
= tap_open(ifname
, sizeof(ifname
));
3198 if (setup_script
[0] != '\0') {
3199 /* try to launch network init script */
3204 *parg
++ = (char *)setup_script
;
3207 execv(setup_script
, args
);
3210 while (waitpid(pid
, &status
, 0) != pid
);
3211 if (!WIFEXITED(status
) ||
3212 WEXITSTATUS(status
) != 0) {
3213 fprintf(stderr
, "%s: could not launch network script\n",
3219 s
= net_tap_fd_init(vlan
, fd
);
3222 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3223 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
3227 #endif /* !_WIN32 */
3229 /* network connection */
3230 typedef struct NetSocketState
{
3231 VLANClientState
*vc
;
3233 int state
; /* 0 = getting length, 1 = getting data */
3237 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
3240 typedef struct NetSocketListenState
{
3243 } NetSocketListenState
;
3245 /* XXX: we consider we can send the whole packet without blocking */
3246 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
3248 NetSocketState
*s
= opaque
;
3252 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
3253 send_all(s
->fd
, buf
, size
);
3256 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
3258 NetSocketState
*s
= opaque
;
3259 sendto(s
->fd
, buf
, size
, 0,
3260 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
3263 static void net_socket_send(void *opaque
)
3265 NetSocketState
*s
= opaque
;
3270 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
3272 err
= socket_error();
3273 if (err
!= EWOULDBLOCK
)
3275 } else if (size
== 0) {
3276 /* end of connection */
3278 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3284 /* reassemble a packet from the network */
3290 memcpy(s
->buf
+ s
->index
, buf
, l
);
3294 if (s
->index
== 4) {
3296 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
3302 l
= s
->packet_len
- s
->index
;
3305 memcpy(s
->buf
+ s
->index
, buf
, l
);
3309 if (s
->index
>= s
->packet_len
) {
3310 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
3319 static void net_socket_send_dgram(void *opaque
)
3321 NetSocketState
*s
= opaque
;
3324 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
3328 /* end of connection */
3329 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3332 qemu_send_packet(s
->vc
, s
->buf
, size
);
3335 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
3340 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
3341 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
3342 inet_ntoa(mcastaddr
->sin_addr
),
3343 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
3347 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
3349 perror("socket(PF_INET, SOCK_DGRAM)");
3354 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
3355 (const char *)&val
, sizeof(val
));
3357 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
3361 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
3367 /* Add host to multicast group */
3368 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
3369 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
3371 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
3372 (const char *)&imr
, sizeof(struct ip_mreq
));
3374 perror("setsockopt(IP_ADD_MEMBERSHIP)");
3378 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
3380 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
3381 (const char *)&val
, sizeof(val
));
3383 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
3387 socket_set_nonblock(fd
);
3395 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
3398 struct sockaddr_in saddr
;
3400 socklen_t saddr_len
;
3403 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
3404 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
3405 * by ONLY ONE process: we must "clone" this dgram socket --jjo
3409 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
3411 if (saddr
.sin_addr
.s_addr
==0) {
3412 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
3416 /* clone dgram socket */
3417 newfd
= net_socket_mcast_create(&saddr
);
3419 /* error already reported by net_socket_mcast_create() */
3423 /* clone newfd to fd, close newfd */
3428 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
3429 fd
, strerror(errno
));
3434 s
= qemu_mallocz(sizeof(NetSocketState
));
3439 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
3440 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
3442 /* mcast: save bound address as dst */
3443 if (is_connected
) s
->dgram_dst
=saddr
;
3445 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3446 "socket: fd=%d (%s mcast=%s:%d)",
3447 fd
, is_connected
? "cloned" : "",
3448 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3452 static void net_socket_connect(void *opaque
)
3454 NetSocketState
*s
= opaque
;
3455 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
3458 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
3462 s
= qemu_mallocz(sizeof(NetSocketState
));
3466 s
->vc
= qemu_new_vlan_client(vlan
,
3467 net_socket_receive
, NULL
, s
);
3468 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3469 "socket: fd=%d", fd
);
3471 net_socket_connect(s
);
3473 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
3478 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
3481 int so_type
=-1, optlen
=sizeof(so_type
);
3483 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
3484 fprintf(stderr
, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd
);
3489 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
3491 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3493 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
3494 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
3495 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3500 static void net_socket_accept(void *opaque
)
3502 NetSocketListenState
*s
= opaque
;
3504 struct sockaddr_in saddr
;
3509 len
= sizeof(saddr
);
3510 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
3511 if (fd
< 0 && errno
!= EINTR
) {
3513 } else if (fd
>= 0) {
3517 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
3521 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
3522 "socket: connection from %s:%d",
3523 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3527 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
3529 NetSocketListenState
*s
;
3531 struct sockaddr_in saddr
;
3533 if (parse_host_port(&saddr
, host_str
) < 0)
3536 s
= qemu_mallocz(sizeof(NetSocketListenState
));
3540 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3545 socket_set_nonblock(fd
);
3547 /* allow fast reuse */
3549 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3551 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3556 ret
= listen(fd
, 0);
3563 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
3567 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
3570 int fd
, connected
, ret
, err
;
3571 struct sockaddr_in saddr
;
3573 if (parse_host_port(&saddr
, host_str
) < 0)
3576 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3581 socket_set_nonblock(fd
);
3585 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3587 err
= socket_error();
3588 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3589 } else if (err
== EINPROGRESS
) {
3601 s
= net_socket_fd_init(vlan
, fd
, connected
);
3604 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3605 "socket: connect to %s:%d",
3606 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3610 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
3614 struct sockaddr_in saddr
;
3616 if (parse_host_port(&saddr
, host_str
) < 0)
3620 fd
= net_socket_mcast_create(&saddr
);
3624 s
= net_socket_fd_init(vlan
, fd
, 0);
3628 s
->dgram_dst
= saddr
;
3630 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3631 "socket: mcast=%s:%d",
3632 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3637 static int get_param_value(char *buf
, int buf_size
,
3638 const char *tag
, const char *str
)
3647 while (*p
!= '\0' && *p
!= '=') {
3648 if ((q
- option
) < sizeof(option
) - 1)
3656 if (!strcmp(tag
, option
)) {
3658 while (*p
!= '\0' && *p
!= ',') {
3659 if ((q
- buf
) < buf_size
- 1)
3666 while (*p
!= '\0' && *p
!= ',') {
3677 int net_client_init(const char *str
)
3688 while (*p
!= '\0' && *p
!= ',') {
3689 if ((q
- device
) < sizeof(device
) - 1)
3697 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
3698 vlan_id
= strtol(buf
, NULL
, 0);
3700 vlan
= qemu_find_vlan(vlan_id
);
3702 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
3705 if (!strcmp(device
, "nic")) {
3709 if (nb_nics
>= MAX_NICS
) {
3710 fprintf(stderr
, "Too Many NICs\n");
3713 nd
= &nd_table
[nb_nics
];
3714 macaddr
= nd
->macaddr
;
3720 macaddr
[5] = 0x56 + nb_nics
;
3722 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
3723 if (parse_macaddr(macaddr
, buf
) < 0) {
3724 fprintf(stderr
, "invalid syntax for ethernet address\n");
3728 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
3729 nd
->model
= strdup(buf
);
3735 if (!strcmp(device
, "none")) {
3736 /* does nothing. It is needed to signal that no network cards
3741 if (!strcmp(device
, "user")) {
3742 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
3743 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
3745 ret
= net_slirp_init(vlan
);
3749 if (!strcmp(device
, "tap")) {
3751 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
3752 fprintf(stderr
, "tap: no interface name\n");
3755 ret
= tap_win32_init(vlan
, ifname
);
3758 if (!strcmp(device
, "tap")) {
3760 char setup_script
[1024];
3762 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3763 fd
= strtol(buf
, NULL
, 0);
3765 if (net_tap_fd_init(vlan
, fd
))
3768 get_param_value(ifname
, sizeof(ifname
), "ifname", p
);
3769 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
3770 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
3772 ret
= net_tap_init(vlan
, ifname
, setup_script
);
3776 if (!strcmp(device
, "socket")) {
3777 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3779 fd
= strtol(buf
, NULL
, 0);
3781 if (net_socket_fd_init(vlan
, fd
, 1))
3783 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
3784 ret
= net_socket_listen_init(vlan
, buf
);
3785 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
3786 ret
= net_socket_connect_init(vlan
, buf
);
3787 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
3788 ret
= net_socket_mcast_init(vlan
, buf
);
3790 fprintf(stderr
, "Unknown socket options: %s\n", p
);
3795 fprintf(stderr
, "Unknown network device: %s\n", device
);
3799 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
3805 void do_info_network(void)
3808 VLANClientState
*vc
;
3810 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3811 term_printf("VLAN %d devices:\n", vlan
->id
);
3812 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3813 term_printf(" %s\n", vc
->info_str
);
3817 /***********************************************************/
3820 static USBPort
*used_usb_ports
;
3821 static USBPort
*free_usb_ports
;
3823 /* ??? Maybe change this to register a hub to keep track of the topology. */
3824 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
3825 usb_attachfn attach
)
3827 port
->opaque
= opaque
;
3828 port
->index
= index
;
3829 port
->attach
= attach
;
3830 port
->next
= free_usb_ports
;
3831 free_usb_ports
= port
;
3834 static int usb_device_add(const char *devname
)
3840 if (!free_usb_ports
)
3843 if (strstart(devname
, "host:", &p
)) {
3844 dev
= usb_host_device_open(p
);
3845 } else if (!strcmp(devname
, "mouse")) {
3846 dev
= usb_mouse_init();
3847 } else if (!strcmp(devname
, "tablet")) {
3848 dev
= usb_tablet_init();
3849 } else if (strstart(devname
, "disk:", &p
)) {
3850 dev
= usb_msd_init(p
);
3857 /* Find a USB port to add the device to. */
3858 port
= free_usb_ports
;
3862 /* Create a new hub and chain it on. */
3863 free_usb_ports
= NULL
;
3864 port
->next
= used_usb_ports
;
3865 used_usb_ports
= port
;
3867 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
3868 usb_attach(port
, hub
);
3869 port
= free_usb_ports
;
3872 free_usb_ports
= port
->next
;
3873 port
->next
= used_usb_ports
;
3874 used_usb_ports
= port
;
3875 usb_attach(port
, dev
);
3879 static int usb_device_del(const char *devname
)
3887 if (!used_usb_ports
)
3890 p
= strchr(devname
, '.');
3893 bus_num
= strtoul(devname
, NULL
, 0);
3894 addr
= strtoul(p
+ 1, NULL
, 0);
3898 lastp
= &used_usb_ports
;
3899 port
= used_usb_ports
;
3900 while (port
&& port
->dev
->addr
!= addr
) {
3901 lastp
= &port
->next
;
3909 *lastp
= port
->next
;
3910 usb_attach(port
, NULL
);
3911 dev
->handle_destroy(dev
);
3912 port
->next
= free_usb_ports
;
3913 free_usb_ports
= port
;
3917 void do_usb_add(const char *devname
)
3920 ret
= usb_device_add(devname
);
3922 term_printf("Could not add USB device '%s'\n", devname
);
3925 void do_usb_del(const char *devname
)
3928 ret
= usb_device_del(devname
);
3930 term_printf("Could not remove USB device '%s'\n", devname
);
3937 const char *speed_str
;
3940 term_printf("USB support not enabled\n");
3944 for (port
= used_usb_ports
; port
; port
= port
->next
) {
3948 switch(dev
->speed
) {
3952 case USB_SPEED_FULL
:
3955 case USB_SPEED_HIGH
:
3962 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
3963 0, dev
->addr
, speed_str
, dev
->devname
);
3967 /***********************************************************/
3970 static char *pid_filename
;
3972 /* Remove PID file. Called on normal exit */
3974 static void remove_pidfile(void)
3976 unlink (pid_filename
);
3979 static void create_pidfile(const char *filename
)
3981 struct stat pidstat
;
3984 /* Try to write our PID to the named file */
3985 if (stat(filename
, &pidstat
) < 0) {
3986 if (errno
== ENOENT
) {
3987 if ((f
= fopen (filename
, "w")) == NULL
) {
3988 perror("Opening pidfile");
3991 fprintf(f
, "%d\n", getpid());
3993 pid_filename
= qemu_strdup(filename
);
3994 if (!pid_filename
) {
3995 fprintf(stderr
, "Could not save PID filename");
3998 atexit(remove_pidfile
);
4001 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
4007 /***********************************************************/
4010 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4014 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4018 static void dumb_refresh(DisplayState
*ds
)
4023 void dumb_display_init(DisplayState
*ds
)
4028 ds
->dpy_update
= dumb_update
;
4029 ds
->dpy_resize
= dumb_resize
;
4030 ds
->dpy_refresh
= dumb_refresh
;
4033 /***********************************************************/
4036 #define MAX_IO_HANDLERS 64
4038 typedef struct IOHandlerRecord
{
4040 IOCanRWHandler
*fd_read_poll
;
4042 IOHandler
*fd_write
;
4044 /* temporary data */
4046 struct IOHandlerRecord
*next
;
4049 static IOHandlerRecord
*first_io_handler
;
4051 /* XXX: fd_read_poll should be suppressed, but an API change is
4052 necessary in the character devices to suppress fd_can_read(). */
4053 int qemu_set_fd_handler2(int fd
,
4054 IOCanRWHandler
*fd_read_poll
,
4056 IOHandler
*fd_write
,
4059 IOHandlerRecord
**pioh
, *ioh
;
4061 if (!fd_read
&& !fd_write
) {
4062 pioh
= &first_io_handler
;
4067 if (ioh
->fd
== fd
) {
4075 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4079 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4082 ioh
->next
= first_io_handler
;
4083 first_io_handler
= ioh
;
4086 ioh
->fd_read_poll
= fd_read_poll
;
4087 ioh
->fd_read
= fd_read
;
4088 ioh
->fd_write
= fd_write
;
4089 ioh
->opaque
= opaque
;
4094 int qemu_set_fd_handler(int fd
,
4096 IOHandler
*fd_write
,
4099 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4102 /***********************************************************/
4103 /* Polling handling */
4105 typedef struct PollingEntry
{
4108 struct PollingEntry
*next
;
4111 static PollingEntry
*first_polling_entry
;
4113 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4115 PollingEntry
**ppe
, *pe
;
4116 pe
= qemu_mallocz(sizeof(PollingEntry
));
4120 pe
->opaque
= opaque
;
4121 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
4126 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
4128 PollingEntry
**ppe
, *pe
;
4129 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
4131 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
4140 /***********************************************************/
4141 /* Wait objects support */
4142 typedef struct WaitObjects
{
4144 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
4145 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
4146 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
4149 static WaitObjects wait_objects
= {0};
4151 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4153 WaitObjects
*w
= &wait_objects
;
4155 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
4157 w
->events
[w
->num
] = handle
;
4158 w
->func
[w
->num
] = func
;
4159 w
->opaque
[w
->num
] = opaque
;
4164 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4167 WaitObjects
*w
= &wait_objects
;
4170 for (i
= 0; i
< w
->num
; i
++) {
4171 if (w
->events
[i
] == handle
)
4174 w
->events
[i
] = w
->events
[i
+ 1];
4175 w
->func
[i
] = w
->func
[i
+ 1];
4176 w
->opaque
[i
] = w
->opaque
[i
+ 1];
4184 /***********************************************************/
4185 /* savevm/loadvm support */
4187 #define IO_BUF_SIZE 32768
4191 BlockDriverState
*bs
;
4194 int64_t base_offset
;
4195 int64_t buf_offset
; /* start of buffer when writing, end of buffer
4198 int buf_size
; /* 0 when writing */
4199 uint8_t buf
[IO_BUF_SIZE
];
4202 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
4206 f
= qemu_mallocz(sizeof(QEMUFile
));
4209 if (!strcmp(mode
, "wb")) {
4211 } else if (!strcmp(mode
, "rb")) {
4216 f
->outfile
= fopen(filename
, mode
);
4228 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
4232 f
= qemu_mallocz(sizeof(QEMUFile
));
4237 f
->is_writable
= is_writable
;
4238 f
->base_offset
= offset
;
4242 void qemu_fflush(QEMUFile
*f
)
4244 if (!f
->is_writable
)
4246 if (f
->buf_index
> 0) {
4248 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4249 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
4251 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4252 f
->buf
, f
->buf_index
);
4254 f
->buf_offset
+= f
->buf_index
;
4259 static void qemu_fill_buffer(QEMUFile
*f
)
4266 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4267 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
4271 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4272 f
->buf
, IO_BUF_SIZE
);
4278 f
->buf_offset
+= len
;
4281 void qemu_fclose(QEMUFile
*f
)
4291 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
4295 l
= IO_BUF_SIZE
- f
->buf_index
;
4298 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
4302 if (f
->buf_index
>= IO_BUF_SIZE
)
4307 void qemu_put_byte(QEMUFile
*f
, int v
)
4309 f
->buf
[f
->buf_index
++] = v
;
4310 if (f
->buf_index
>= IO_BUF_SIZE
)
4314 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
4320 l
= f
->buf_size
- f
->buf_index
;
4322 qemu_fill_buffer(f
);
4323 l
= f
->buf_size
- f
->buf_index
;
4329 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
4334 return size1
- size
;
4337 int qemu_get_byte(QEMUFile
*f
)
4339 if (f
->buf_index
>= f
->buf_size
) {
4340 qemu_fill_buffer(f
);
4341 if (f
->buf_index
>= f
->buf_size
)
4344 return f
->buf
[f
->buf_index
++];
4347 int64_t qemu_ftell(QEMUFile
*f
)
4349 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
4352 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
4354 if (whence
== SEEK_SET
) {
4356 } else if (whence
== SEEK_CUR
) {
4357 pos
+= qemu_ftell(f
);
4359 /* SEEK_END not supported */
4362 if (f
->is_writable
) {
4364 f
->buf_offset
= pos
;
4366 f
->buf_offset
= pos
;
4373 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
4375 qemu_put_byte(f
, v
>> 8);
4376 qemu_put_byte(f
, v
);
4379 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
4381 qemu_put_byte(f
, v
>> 24);
4382 qemu_put_byte(f
, v
>> 16);
4383 qemu_put_byte(f
, v
>> 8);
4384 qemu_put_byte(f
, v
);
4387 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
4389 qemu_put_be32(f
, v
>> 32);
4390 qemu_put_be32(f
, v
);
4393 unsigned int qemu_get_be16(QEMUFile
*f
)
4396 v
= qemu_get_byte(f
) << 8;
4397 v
|= qemu_get_byte(f
);
4401 unsigned int qemu_get_be32(QEMUFile
*f
)
4404 v
= qemu_get_byte(f
) << 24;
4405 v
|= qemu_get_byte(f
) << 16;
4406 v
|= qemu_get_byte(f
) << 8;
4407 v
|= qemu_get_byte(f
);
4411 uint64_t qemu_get_be64(QEMUFile
*f
)
4414 v
= (uint64_t)qemu_get_be32(f
) << 32;
4415 v
|= qemu_get_be32(f
);
4419 typedef struct SaveStateEntry
{
4423 SaveStateHandler
*save_state
;
4424 LoadStateHandler
*load_state
;
4426 struct SaveStateEntry
*next
;
4429 static SaveStateEntry
*first_se
;
4431 int register_savevm(const char *idstr
,
4434 SaveStateHandler
*save_state
,
4435 LoadStateHandler
*load_state
,
4438 SaveStateEntry
*se
, **pse
;
4440 se
= qemu_malloc(sizeof(SaveStateEntry
));
4443 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
4444 se
->instance_id
= instance_id
;
4445 se
->version_id
= version_id
;
4446 se
->save_state
= save_state
;
4447 se
->load_state
= load_state
;
4448 se
->opaque
= opaque
;
4451 /* add at the end of list */
4453 while (*pse
!= NULL
)
4454 pse
= &(*pse
)->next
;
4459 #define QEMU_VM_FILE_MAGIC 0x5145564d
4460 #define QEMU_VM_FILE_VERSION 0x00000002
4462 int qemu_savevm_state(QEMUFile
*f
)
4466 int64_t cur_pos
, len_pos
, total_len_pos
;
4468 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
4469 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
4470 total_len_pos
= qemu_ftell(f
);
4471 qemu_put_be64(f
, 0); /* total size */
4473 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4475 len
= strlen(se
->idstr
);
4476 qemu_put_byte(f
, len
);
4477 qemu_put_buffer(f
, se
->idstr
, len
);
4479 qemu_put_be32(f
, se
->instance_id
);
4480 qemu_put_be32(f
, se
->version_id
);
4482 /* record size: filled later */
4483 len_pos
= qemu_ftell(f
);
4484 qemu_put_be32(f
, 0);
4486 se
->save_state(f
, se
->opaque
);
4488 /* fill record size */
4489 cur_pos
= qemu_ftell(f
);
4490 len
= cur_pos
- len_pos
- 4;
4491 qemu_fseek(f
, len_pos
, SEEK_SET
);
4492 qemu_put_be32(f
, len
);
4493 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4495 cur_pos
= qemu_ftell(f
);
4496 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
4497 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
4498 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4504 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
4508 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4509 if (!strcmp(se
->idstr
, idstr
) &&
4510 instance_id
== se
->instance_id
)
4516 int qemu_loadvm_state(QEMUFile
*f
)
4519 int len
, ret
, instance_id
, record_len
, version_id
;
4520 int64_t total_len
, end_pos
, cur_pos
;
4524 v
= qemu_get_be32(f
);
4525 if (v
!= QEMU_VM_FILE_MAGIC
)
4527 v
= qemu_get_be32(f
);
4528 if (v
!= QEMU_VM_FILE_VERSION
) {
4533 total_len
= qemu_get_be64(f
);
4534 end_pos
= total_len
+ qemu_ftell(f
);
4536 if (qemu_ftell(f
) >= end_pos
)
4538 len
= qemu_get_byte(f
);
4539 qemu_get_buffer(f
, idstr
, len
);
4541 instance_id
= qemu_get_be32(f
);
4542 version_id
= qemu_get_be32(f
);
4543 record_len
= qemu_get_be32(f
);
4545 printf("idstr=%s instance=0x%x version=%d len=%d\n",
4546 idstr
, instance_id
, version_id
, record_len
);
4548 cur_pos
= qemu_ftell(f
);
4549 se
= find_se(idstr
, instance_id
);
4551 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
4552 instance_id
, idstr
);
4554 ret
= se
->load_state(f
, se
->opaque
, version_id
);
4556 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
4557 instance_id
, idstr
);
4560 /* always seek to exact end of record */
4561 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
4568 /* device can contain snapshots */
4569 static int bdrv_can_snapshot(BlockDriverState
*bs
)
4572 !bdrv_is_removable(bs
) &&
4573 !bdrv_is_read_only(bs
));
4576 /* device must be snapshots in order to have a reliable snapshot */
4577 static int bdrv_has_snapshot(BlockDriverState
*bs
)
4580 !bdrv_is_removable(bs
) &&
4581 !bdrv_is_read_only(bs
));
4584 static BlockDriverState
*get_bs_snapshots(void)
4586 BlockDriverState
*bs
;
4590 return bs_snapshots
;
4591 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4593 if (bdrv_can_snapshot(bs
))
4602 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
4605 QEMUSnapshotInfo
*sn_tab
, *sn
;
4609 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
4612 for(i
= 0; i
< nb_sns
; i
++) {
4614 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
4624 void do_savevm(const char *name
)
4626 BlockDriverState
*bs
, *bs1
;
4627 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
4628 int must_delete
, ret
, i
;
4629 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
4631 int saved_vm_running
;
4638 bs
= get_bs_snapshots();
4640 term_printf("No block device can accept snapshots\n");
4644 /* ??? Should this occur after vm_stop? */
4647 saved_vm_running
= vm_running
;
4652 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
4657 memset(sn
, 0, sizeof(*sn
));
4659 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
4660 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
4663 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
4666 /* fill auxiliary fields */
4669 sn
->date_sec
= tb
.time
;
4670 sn
->date_nsec
= tb
.millitm
* 1000000;
4672 gettimeofday(&tv
, NULL
);
4673 sn
->date_sec
= tv
.tv_sec
;
4674 sn
->date_nsec
= tv
.tv_usec
* 1000;
4676 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
4678 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
4679 term_printf("Device %s does not support VM state snapshots\n",
4680 bdrv_get_device_name(bs
));
4684 /* save the VM state */
4685 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
4687 term_printf("Could not open VM state file\n");
4690 ret
= qemu_savevm_state(f
);
4691 sn
->vm_state_size
= qemu_ftell(f
);
4694 term_printf("Error %d while writing VM\n", ret
);
4698 /* create the snapshots */
4700 for(i
= 0; i
< MAX_DISKS
; i
++) {
4702 if (bdrv_has_snapshot(bs1
)) {
4704 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
4706 term_printf("Error while deleting snapshot on '%s'\n",
4707 bdrv_get_device_name(bs1
));
4710 ret
= bdrv_snapshot_create(bs1
, sn
);
4712 term_printf("Error while creating snapshot on '%s'\n",
4713 bdrv_get_device_name(bs1
));
4719 if (saved_vm_running
)
4723 void do_loadvm(const char *name
)
4725 BlockDriverState
*bs
, *bs1
;
4726 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
4729 int saved_vm_running
;
4731 bs
= get_bs_snapshots();
4733 term_printf("No block device supports snapshots\n");
4737 /* Flush all IO requests so they don't interfere with the new state. */
4740 saved_vm_running
= vm_running
;
4743 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4745 if (bdrv_has_snapshot(bs1
)) {
4746 ret
= bdrv_snapshot_goto(bs1
, name
);
4749 term_printf("Warning: ");
4752 term_printf("Snapshots not supported on device '%s'\n",
4753 bdrv_get_device_name(bs1
));
4756 term_printf("Could not find snapshot '%s' on device '%s'\n",
4757 name
, bdrv_get_device_name(bs1
));
4760 term_printf("Error %d while activating snapshot on '%s'\n",
4761 ret
, bdrv_get_device_name(bs1
));
4764 /* fatal on snapshot block device */
4771 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
4772 term_printf("Device %s does not support VM state snapshots\n",
4773 bdrv_get_device_name(bs
));
4777 /* restore the VM state */
4778 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
4780 term_printf("Could not open VM state file\n");
4783 ret
= qemu_loadvm_state(f
);
4786 term_printf("Error %d while loading VM state\n", ret
);
4789 if (saved_vm_running
)
4793 void do_delvm(const char *name
)
4795 BlockDriverState
*bs
, *bs1
;
4798 bs
= get_bs_snapshots();
4800 term_printf("No block device supports snapshots\n");
4804 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4806 if (bdrv_has_snapshot(bs1
)) {
4807 ret
= bdrv_snapshot_delete(bs1
, name
);
4809 if (ret
== -ENOTSUP
)
4810 term_printf("Snapshots not supported on device '%s'\n",
4811 bdrv_get_device_name(bs1
));
4813 term_printf("Error %d while deleting snapshot on '%s'\n",
4814 ret
, bdrv_get_device_name(bs1
));
4820 void do_info_snapshots(void)
4822 BlockDriverState
*bs
, *bs1
;
4823 QEMUSnapshotInfo
*sn_tab
, *sn
;
4827 bs
= get_bs_snapshots();
4829 term_printf("No available block device supports snapshots\n");
4832 term_printf("Snapshot devices:");
4833 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4835 if (bdrv_has_snapshot(bs1
)) {
4837 term_printf(" %s", bdrv_get_device_name(bs1
));
4842 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
4844 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
4847 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
4848 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
4849 for(i
= 0; i
< nb_sns
; i
++) {
4851 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
4856 /***********************************************************/
4857 /* cpu save/restore */
4859 #if defined(TARGET_I386)
4861 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
4863 qemu_put_be32(f
, dt
->selector
);
4864 qemu_put_betl(f
, dt
->base
);
4865 qemu_put_be32(f
, dt
->limit
);
4866 qemu_put_be32(f
, dt
->flags
);
4869 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
4871 dt
->selector
= qemu_get_be32(f
);
4872 dt
->base
= qemu_get_betl(f
);
4873 dt
->limit
= qemu_get_be32(f
);
4874 dt
->flags
= qemu_get_be32(f
);
4877 void cpu_save(QEMUFile
*f
, void *opaque
)
4879 CPUState
*env
= opaque
;
4880 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
4884 for(i
= 0; i
< CPU_NB_REGS
; i
++)
4885 qemu_put_betls(f
, &env
->regs
[i
]);
4886 qemu_put_betls(f
, &env
->eip
);
4887 qemu_put_betls(f
, &env
->eflags
);
4888 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
4889 qemu_put_be32s(f
, &hflags
);
4893 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
4895 for(i
= 0; i
< 8; i
++) {
4896 fptag
|= ((!env
->fptags
[i
]) << i
);
4899 qemu_put_be16s(f
, &fpuc
);
4900 qemu_put_be16s(f
, &fpus
);
4901 qemu_put_be16s(f
, &fptag
);
4903 #ifdef USE_X86LDOUBLE
4908 qemu_put_be16s(f
, &fpregs_format
);
4910 for(i
= 0; i
< 8; i
++) {
4911 #ifdef USE_X86LDOUBLE
4915 /* we save the real CPU data (in case of MMX usage only 'mant'
4916 contains the MMX register */
4917 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
4918 qemu_put_be64(f
, mant
);
4919 qemu_put_be16(f
, exp
);
4922 /* if we use doubles for float emulation, we save the doubles to
4923 avoid losing information in case of MMX usage. It can give
4924 problems if the image is restored on a CPU where long
4925 doubles are used instead. */
4926 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
4930 for(i
= 0; i
< 6; i
++)
4931 cpu_put_seg(f
, &env
->segs
[i
]);
4932 cpu_put_seg(f
, &env
->ldt
);
4933 cpu_put_seg(f
, &env
->tr
);
4934 cpu_put_seg(f
, &env
->gdt
);
4935 cpu_put_seg(f
, &env
->idt
);
4937 qemu_put_be32s(f
, &env
->sysenter_cs
);
4938 qemu_put_be32s(f
, &env
->sysenter_esp
);
4939 qemu_put_be32s(f
, &env
->sysenter_eip
);
4941 qemu_put_betls(f
, &env
->cr
[0]);
4942 qemu_put_betls(f
, &env
->cr
[2]);
4943 qemu_put_betls(f
, &env
->cr
[3]);
4944 qemu_put_betls(f
, &env
->cr
[4]);
4946 for(i
= 0; i
< 8; i
++)
4947 qemu_put_betls(f
, &env
->dr
[i
]);
4950 qemu_put_be32s(f
, &env
->a20_mask
);
4953 qemu_put_be32s(f
, &env
->mxcsr
);
4954 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
4955 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
4956 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
4959 #ifdef TARGET_X86_64
4960 qemu_put_be64s(f
, &env
->efer
);
4961 qemu_put_be64s(f
, &env
->star
);
4962 qemu_put_be64s(f
, &env
->lstar
);
4963 qemu_put_be64s(f
, &env
->cstar
);
4964 qemu_put_be64s(f
, &env
->fmask
);
4965 qemu_put_be64s(f
, &env
->kernelgsbase
);
4967 qemu_put_be32s(f
, &env
->smbase
);
4970 #ifdef USE_X86LDOUBLE
4971 /* XXX: add that in a FPU generic layer */
4972 union x86_longdouble
{
4977 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
4978 #define EXPBIAS1 1023
4979 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
4980 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
4982 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
4986 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
4987 /* exponent + sign */
4988 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
4989 e
|= SIGND1(temp
) >> 16;
4994 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4996 CPUState
*env
= opaque
;
4999 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
5001 if (version_id
!= 3 && version_id
!= 4)
5003 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5004 qemu_get_betls(f
, &env
->regs
[i
]);
5005 qemu_get_betls(f
, &env
->eip
);
5006 qemu_get_betls(f
, &env
->eflags
);
5007 qemu_get_be32s(f
, &hflags
);
5009 qemu_get_be16s(f
, &fpuc
);
5010 qemu_get_be16s(f
, &fpus
);
5011 qemu_get_be16s(f
, &fptag
);
5012 qemu_get_be16s(f
, &fpregs_format
);
5014 /* NOTE: we cannot always restore the FPU state if the image come
5015 from a host with a different 'USE_X86LDOUBLE' define. We guess
5016 if we are in an MMX state to restore correctly in that case. */
5017 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
5018 for(i
= 0; i
< 8; i
++) {
5022 switch(fpregs_format
) {
5024 mant
= qemu_get_be64(f
);
5025 exp
= qemu_get_be16(f
);
5026 #ifdef USE_X86LDOUBLE
5027 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5029 /* difficult case */
5031 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5033 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5037 mant
= qemu_get_be64(f
);
5038 #ifdef USE_X86LDOUBLE
5040 union x86_longdouble
*p
;
5041 /* difficult case */
5042 p
= (void *)&env
->fpregs
[i
];
5047 fp64_to_fp80(p
, mant
);
5051 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5060 /* XXX: restore FPU round state */
5061 env
->fpstt
= (fpus
>> 11) & 7;
5062 env
->fpus
= fpus
& ~0x3800;
5064 for(i
= 0; i
< 8; i
++) {
5065 env
->fptags
[i
] = (fptag
>> i
) & 1;
5068 for(i
= 0; i
< 6; i
++)
5069 cpu_get_seg(f
, &env
->segs
[i
]);
5070 cpu_get_seg(f
, &env
->ldt
);
5071 cpu_get_seg(f
, &env
->tr
);
5072 cpu_get_seg(f
, &env
->gdt
);
5073 cpu_get_seg(f
, &env
->idt
);
5075 qemu_get_be32s(f
, &env
->sysenter_cs
);
5076 qemu_get_be32s(f
, &env
->sysenter_esp
);
5077 qemu_get_be32s(f
, &env
->sysenter_eip
);
5079 qemu_get_betls(f
, &env
->cr
[0]);
5080 qemu_get_betls(f
, &env
->cr
[2]);
5081 qemu_get_betls(f
, &env
->cr
[3]);
5082 qemu_get_betls(f
, &env
->cr
[4]);
5084 for(i
= 0; i
< 8; i
++)
5085 qemu_get_betls(f
, &env
->dr
[i
]);
5088 qemu_get_be32s(f
, &env
->a20_mask
);
5090 qemu_get_be32s(f
, &env
->mxcsr
);
5091 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5092 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5093 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5096 #ifdef TARGET_X86_64
5097 qemu_get_be64s(f
, &env
->efer
);
5098 qemu_get_be64s(f
, &env
->star
);
5099 qemu_get_be64s(f
, &env
->lstar
);
5100 qemu_get_be64s(f
, &env
->cstar
);
5101 qemu_get_be64s(f
, &env
->fmask
);
5102 qemu_get_be64s(f
, &env
->kernelgsbase
);
5104 if (version_id
>= 4)
5105 qemu_get_be32s(f
, &env
->smbase
);
5107 /* XXX: compute hflags from scratch, except for CPL and IIF */
5108 env
->hflags
= hflags
;
5113 #elif defined(TARGET_PPC)
5114 void cpu_save(QEMUFile
*f
, void *opaque
)
5118 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5123 #elif defined(TARGET_MIPS)
5124 void cpu_save(QEMUFile
*f
, void *opaque
)
5128 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5133 #elif defined(TARGET_SPARC)
5134 void cpu_save(QEMUFile
*f
, void *opaque
)
5136 CPUState
*env
= opaque
;
5140 for(i
= 0; i
< 8; i
++)
5141 qemu_put_betls(f
, &env
->gregs
[i
]);
5142 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5143 qemu_put_betls(f
, &env
->regbase
[i
]);
5146 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5152 qemu_put_be32(f
, u
.i
);
5155 qemu_put_betls(f
, &env
->pc
);
5156 qemu_put_betls(f
, &env
->npc
);
5157 qemu_put_betls(f
, &env
->y
);
5159 qemu_put_be32(f
, tmp
);
5160 qemu_put_betls(f
, &env
->fsr
);
5161 qemu_put_betls(f
, &env
->tbr
);
5162 #ifndef TARGET_SPARC64
5163 qemu_put_be32s(f
, &env
->wim
);
5165 for(i
= 0; i
< 16; i
++)
5166 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
5170 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5172 CPUState
*env
= opaque
;
5176 for(i
= 0; i
< 8; i
++)
5177 qemu_get_betls(f
, &env
->gregs
[i
]);
5178 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5179 qemu_get_betls(f
, &env
->regbase
[i
]);
5182 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5187 u
.i
= qemu_get_be32(f
);
5191 qemu_get_betls(f
, &env
->pc
);
5192 qemu_get_betls(f
, &env
->npc
);
5193 qemu_get_betls(f
, &env
->y
);
5194 tmp
= qemu_get_be32(f
);
5195 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
5196 correctly updated */
5198 qemu_get_betls(f
, &env
->fsr
);
5199 qemu_get_betls(f
, &env
->tbr
);
5200 #ifndef TARGET_SPARC64
5201 qemu_get_be32s(f
, &env
->wim
);
5203 for(i
= 0; i
< 16; i
++)
5204 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
5210 #elif defined(TARGET_ARM)
5212 /* ??? Need to implement these. */
5213 void cpu_save(QEMUFile
*f
, void *opaque
)
5217 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5224 #warning No CPU save/restore functions
5228 /***********************************************************/
5229 /* ram save/restore */
5231 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
5235 v
= qemu_get_byte(f
);
5238 if (qemu_get_buffer(f
, buf
, len
) != len
)
5242 v
= qemu_get_byte(f
);
5243 memset(buf
, v
, len
);
5251 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
5255 if (qemu_get_be32(f
) != phys_ram_size
)
5257 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
5258 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
5265 #define BDRV_HASH_BLOCK_SIZE 1024
5266 #define IOBUF_SIZE 4096
5267 #define RAM_CBLOCK_MAGIC 0xfabe
5269 typedef struct RamCompressState
{
5272 uint8_t buf
[IOBUF_SIZE
];
5275 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
5278 memset(s
, 0, sizeof(*s
));
5280 ret
= deflateInit2(&s
->zstream
, 1,
5282 9, Z_DEFAULT_STRATEGY
);
5285 s
->zstream
.avail_out
= IOBUF_SIZE
;
5286 s
->zstream
.next_out
= s
->buf
;
5290 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
5292 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
5293 qemu_put_be16(s
->f
, len
);
5294 qemu_put_buffer(s
->f
, buf
, len
);
5297 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
5301 s
->zstream
.avail_in
= len
;
5302 s
->zstream
.next_in
= (uint8_t *)buf
;
5303 while (s
->zstream
.avail_in
> 0) {
5304 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
5307 if (s
->zstream
.avail_out
== 0) {
5308 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
5309 s
->zstream
.avail_out
= IOBUF_SIZE
;
5310 s
->zstream
.next_out
= s
->buf
;
5316 static void ram_compress_close(RamCompressState
*s
)
5320 /* compress last bytes */
5322 ret
= deflate(&s
->zstream
, Z_FINISH
);
5323 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
5324 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
5326 ram_put_cblock(s
, s
->buf
, len
);
5328 s
->zstream
.avail_out
= IOBUF_SIZE
;
5329 s
->zstream
.next_out
= s
->buf
;
5330 if (ret
== Z_STREAM_END
)
5337 deflateEnd(&s
->zstream
);
5340 typedef struct RamDecompressState
{
5343 uint8_t buf
[IOBUF_SIZE
];
5344 } RamDecompressState
;
5346 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
5349 memset(s
, 0, sizeof(*s
));
5351 ret
= inflateInit(&s
->zstream
);
5357 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
5361 s
->zstream
.avail_out
= len
;
5362 s
->zstream
.next_out
= buf
;
5363 while (s
->zstream
.avail_out
> 0) {
5364 if (s
->zstream
.avail_in
== 0) {
5365 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
5367 clen
= qemu_get_be16(s
->f
);
5368 if (clen
> IOBUF_SIZE
)
5370 qemu_get_buffer(s
->f
, s
->buf
, clen
);
5371 s
->zstream
.avail_in
= clen
;
5372 s
->zstream
.next_in
= s
->buf
;
5374 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
5375 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
5382 static void ram_decompress_close(RamDecompressState
*s
)
5384 inflateEnd(&s
->zstream
);
5387 static void ram_save(QEMUFile
*f
, void *opaque
)
5390 RamCompressState s1
, *s
= &s1
;
5393 qemu_put_be32(f
, phys_ram_size
);
5394 if (ram_compress_open(s
, f
) < 0)
5396 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5398 if (tight_savevm_enabled
) {
5402 /* find if the memory block is available on a virtual
5405 for(j
= 0; j
< MAX_DISKS
; j
++) {
5407 sector_num
= bdrv_hash_find(bs_table
[j
],
5408 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5409 if (sector_num
>= 0)
5414 goto normal_compress
;
5417 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
5418 ram_compress_buf(s
, buf
, 10);
5424 ram_compress_buf(s
, buf
, 1);
5425 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5428 ram_compress_close(s
);
5431 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
5433 RamDecompressState s1
, *s
= &s1
;
5437 if (version_id
== 1)
5438 return ram_load_v1(f
, opaque
);
5439 if (version_id
!= 2)
5441 if (qemu_get_be32(f
) != phys_ram_size
)
5443 if (ram_decompress_open(s
, f
) < 0)
5445 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5446 if (ram_decompress_buf(s
, buf
, 1) < 0) {
5447 fprintf(stderr
, "Error while reading ram block header\n");
5451 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
5452 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
5461 ram_decompress_buf(s
, buf
+ 1, 9);
5463 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
5464 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
5465 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
5468 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
5469 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
5470 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
5471 bs_index
, sector_num
);
5478 printf("Error block header\n");
5482 ram_decompress_close(s
);
5486 /***********************************************************/
5487 /* bottom halves (can be seen as timers which expire ASAP) */
5496 static QEMUBH
*first_bh
= NULL
;
5498 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
5501 bh
= qemu_mallocz(sizeof(QEMUBH
));
5505 bh
->opaque
= opaque
;
5509 int qemu_bh_poll(void)
5528 void qemu_bh_schedule(QEMUBH
*bh
)
5530 CPUState
*env
= cpu_single_env
;
5534 bh
->next
= first_bh
;
5537 /* stop the currently executing CPU to execute the BH ASAP */
5539 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
5543 void qemu_bh_cancel(QEMUBH
*bh
)
5546 if (bh
->scheduled
) {
5549 pbh
= &(*pbh
)->next
;
5555 void qemu_bh_delete(QEMUBH
*bh
)
5561 /***********************************************************/
5562 /* machine registration */
5564 QEMUMachine
*first_machine
= NULL
;
5566 int qemu_register_machine(QEMUMachine
*m
)
5569 pm
= &first_machine
;
5577 QEMUMachine
*find_machine(const char *name
)
5581 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5582 if (!strcmp(m
->name
, name
))
5588 /***********************************************************/
5589 /* main execution loop */
5591 void gui_update(void *opaque
)
5593 display_state
.dpy_refresh(&display_state
);
5594 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
5597 struct vm_change_state_entry
{
5598 VMChangeStateHandler
*cb
;
5600 LIST_ENTRY (vm_change_state_entry
) entries
;
5603 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
5605 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
5608 VMChangeStateEntry
*e
;
5610 e
= qemu_mallocz(sizeof (*e
));
5616 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
5620 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
5622 LIST_REMOVE (e
, entries
);
5626 static void vm_state_notify(int running
)
5628 VMChangeStateEntry
*e
;
5630 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
5631 e
->cb(e
->opaque
, running
);
5635 /* XXX: support several handlers */
5636 static VMStopHandler
*vm_stop_cb
;
5637 static void *vm_stop_opaque
;
5639 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
5642 vm_stop_opaque
= opaque
;
5646 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
5660 void vm_stop(int reason
)
5663 cpu_disable_ticks();
5667 vm_stop_cb(vm_stop_opaque
, reason
);
5674 /* reset/shutdown handler */
5676 typedef struct QEMUResetEntry
{
5677 QEMUResetHandler
*func
;
5679 struct QEMUResetEntry
*next
;
5682 static QEMUResetEntry
*first_reset_entry
;
5683 static int reset_requested
;
5684 static int shutdown_requested
;
5685 static int powerdown_requested
;
5687 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
5689 QEMUResetEntry
**pre
, *re
;
5691 pre
= &first_reset_entry
;
5692 while (*pre
!= NULL
)
5693 pre
= &(*pre
)->next
;
5694 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
5696 re
->opaque
= opaque
;
5701 void qemu_system_reset(void)
5705 /* reset all devices */
5706 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
5707 re
->func(re
->opaque
);
5711 void qemu_system_reset_request(void)
5714 shutdown_requested
= 1;
5716 reset_requested
= 1;
5719 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5722 void qemu_system_shutdown_request(void)
5724 shutdown_requested
= 1;
5726 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5729 void qemu_system_powerdown_request(void)
5731 powerdown_requested
= 1;
5733 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5736 void main_loop_wait(int timeout
)
5738 IOHandlerRecord
*ioh
, *ioh_next
;
5739 fd_set rfds
, wfds
, xfds
;
5745 /* XXX: need to suppress polling by better using win32 events */
5747 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
5748 ret
|= pe
->func(pe
->opaque
);
5751 if (ret
== 0 && timeout
> 0) {
5753 WaitObjects
*w
= &wait_objects
;
5755 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
5756 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
5757 if (w
->func
[ret
- WAIT_OBJECT_0
])
5758 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
5759 } else if (ret
== WAIT_TIMEOUT
) {
5761 err
= GetLastError();
5762 fprintf(stderr
, "Wait error %d %d\n", ret
, err
);
5766 /* poll any events */
5767 /* XXX: separate device handlers from system ones */
5772 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
5774 (!ioh
->fd_read_poll
||
5775 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
5776 FD_SET(ioh
->fd
, &rfds
);
5780 if (ioh
->fd_write
) {
5781 FD_SET(ioh
->fd
, &wfds
);
5791 tv
.tv_usec
= timeout
* 1000;
5793 #if defined(CONFIG_SLIRP)
5795 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
5798 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
5800 /* XXX: better handling of removal */
5801 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
5802 ioh_next
= ioh
->next
;
5803 if (FD_ISSET(ioh
->fd
, &rfds
)) {
5804 ioh
->fd_read(ioh
->opaque
);
5806 if (FD_ISSET(ioh
->fd
, &wfds
)) {
5807 ioh
->fd_write(ioh
->opaque
);
5811 #if defined(CONFIG_SLIRP)
5818 slirp_select_poll(&rfds
, &wfds
, &xfds
);
5825 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
5826 qemu_get_clock(vm_clock
));
5827 /* run dma transfers, if any */
5831 /* real time timers */
5832 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
5833 qemu_get_clock(rt_clock
));
5836 static CPUState
*cur_cpu
;
5841 #ifdef CONFIG_PROFILER
5846 cur_cpu
= first_cpu
;
5853 env
= env
->next_cpu
;
5856 #ifdef CONFIG_PROFILER
5857 ti
= profile_getclock();
5859 ret
= cpu_exec(env
);
5860 #ifdef CONFIG_PROFILER
5861 qemu_time
+= profile_getclock() - ti
;
5863 if (ret
!= EXCP_HALTED
)
5865 /* all CPUs are halted ? */
5866 if (env
== cur_cpu
) {
5873 if (shutdown_requested
) {
5874 ret
= EXCP_INTERRUPT
;
5877 if (reset_requested
) {
5878 reset_requested
= 0;
5879 qemu_system_reset();
5880 ret
= EXCP_INTERRUPT
;
5882 if (powerdown_requested
) {
5883 powerdown_requested
= 0;
5884 qemu_system_powerdown();
5885 ret
= EXCP_INTERRUPT
;
5887 if (ret
== EXCP_DEBUG
) {
5888 vm_stop(EXCP_DEBUG
);
5890 /* if hlt instruction, we wait until the next IRQ */
5891 /* XXX: use timeout computed from timers */
5892 if (ret
== EXCP_HLT
)
5899 #ifdef CONFIG_PROFILER
5900 ti
= profile_getclock();
5902 main_loop_wait(timeout
);
5903 #ifdef CONFIG_PROFILER
5904 dev_time
+= profile_getclock() - ti
;
5907 cpu_disable_ticks();
5913 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2006 Fabrice Bellard\n"
5914 "usage: %s [options] [disk_image]\n"
5916 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
5918 "Standard options:\n"
5919 "-M machine select emulated machine (-M ? for list)\n"
5920 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
5921 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
5922 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
5923 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
5924 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
5925 "-snapshot write to temporary files instead of disk image files\n"
5927 "-no-quit disable SDL window close capability\n"
5930 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
5932 "-m megs set virtual RAM size to megs MB [default=%d]\n"
5933 "-smp n set the number of CPUs to 'n' [default=1]\n"
5934 "-nographic disable graphical output and redirect serial I/Os to console\n"
5936 "-k language use keyboard layout (for example \"fr\" for French)\n"
5939 "-audio-help print list of audio drivers and their options\n"
5940 "-soundhw c1,... enable audio support\n"
5941 " and only specified sound cards (comma separated list)\n"
5942 " use -soundhw ? to get the list of supported cards\n"
5943 " use -soundhw all to enable all of them\n"
5945 "-localtime set the real time clock to local time [default=utc]\n"
5946 "-full-screen start in full screen\n"
5948 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
5950 "-usb enable the USB driver (will be the default soon)\n"
5951 "-usbdevice name add the host or guest USB device 'name'\n"
5952 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5953 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
5956 "Network options:\n"
5957 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
5958 " create a new Network Interface Card and connect it to VLAN 'n'\n"
5960 "-net user[,vlan=n][,hostname=host]\n"
5961 " connect the user mode network stack to VLAN 'n' and send\n"
5962 " hostname 'host' to DHCP clients\n"
5965 "-net tap[,vlan=n],ifname=name\n"
5966 " connect the host TAP network interface to VLAN 'n'\n"
5968 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
5969 " connect the host TAP network interface to VLAN 'n' and use\n"
5970 " the network script 'file' (default=%s);\n"
5971 " use 'fd=h' to connect to an already opened TAP interface\n"
5973 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
5974 " connect the vlan 'n' to another VLAN using a socket connection\n"
5975 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
5976 " connect the vlan 'n' to multicast maddr and port\n"
5977 "-net none use it alone to have zero network devices; if no -net option\n"
5978 " is provided, the default is '-net nic -net user'\n"
5981 "-tftp prefix allow tftp access to files starting with prefix [-net user]\n"
5983 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
5985 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
5986 " redirect TCP or UDP connections from host to guest [-net user]\n"
5989 "Linux boot specific:\n"
5990 "-kernel bzImage use 'bzImage' as kernel image\n"
5991 "-append cmdline use 'cmdline' as kernel command line\n"
5992 "-initrd file use 'file' as initial ram disk\n"
5994 "Debug/Expert options:\n"
5995 "-monitor dev redirect the monitor to char device 'dev'\n"
5996 "-serial dev redirect the serial port to char device 'dev'\n"
5997 "-parallel dev redirect the parallel port to char device 'dev'\n"
5998 "-pidfile file Write PID to 'file'\n"
5999 "-S freeze CPU at startup (use 'c' to start execution)\n"
6000 "-s wait gdb connection to port %d\n"
6001 "-p port change gdb connection port\n"
6002 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
6003 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
6004 " translation (t=none or lba) (usually qemu can guess them)\n"
6005 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
6007 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
6008 "-no-kqemu disable KQEMU kernel module usage\n"
6010 #ifdef USE_CODE_COPY
6011 "-no-code-copy disable code copy acceleration\n"
6014 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
6015 " (default is CL-GD5446 PCI VGA)\n"
6016 "-no-acpi disable ACPI\n"
6018 "-no-reboot exit instead of rebooting\n"
6019 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
6020 "-vnc display start a VNC server on display\n"
6022 "During emulation, the following keys are useful:\n"
6023 "ctrl-alt-f toggle full screen\n"
6024 "ctrl-alt-n switch to virtual console 'n'\n"
6025 "ctrl-alt toggle mouse and keyboard grab\n"
6027 "When using -nographic, press 'ctrl-a h' to get some help.\n"
6032 DEFAULT_NETWORK_SCRIPT
,
6034 DEFAULT_GDBSTUB_PORT
,
6039 #define HAS_ARG 0x0001
6053 QEMU_OPTION_snapshot
,
6055 QEMU_OPTION_no_fd_bootchk
,
6058 QEMU_OPTION_nographic
,
6060 QEMU_OPTION_audio_help
,
6061 QEMU_OPTION_soundhw
,
6079 QEMU_OPTION_no_code_copy
,
6081 QEMU_OPTION_localtime
,
6082 QEMU_OPTION_cirrusvga
,
6084 QEMU_OPTION_std_vga
,
6085 QEMU_OPTION_monitor
,
6087 QEMU_OPTION_parallel
,
6089 QEMU_OPTION_full_screen
,
6090 QEMU_OPTION_no_quit
,
6091 QEMU_OPTION_pidfile
,
6092 QEMU_OPTION_no_kqemu
,
6093 QEMU_OPTION_kernel_kqemu
,
6094 QEMU_OPTION_win2k_hack
,
6096 QEMU_OPTION_usbdevice
,
6099 QEMU_OPTION_no_acpi
,
6100 QEMU_OPTION_no_reboot
,
6103 typedef struct QEMUOption
{
6109 const QEMUOption qemu_options
[] = {
6110 { "h", 0, QEMU_OPTION_h
},
6112 { "M", HAS_ARG
, QEMU_OPTION_M
},
6113 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
6114 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
6115 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
6116 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
6117 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
6118 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
6119 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
6120 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
6121 { "snapshot", 0, QEMU_OPTION_snapshot
},
6123 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
6125 { "m", HAS_ARG
, QEMU_OPTION_m
},
6126 { "nographic", 0, QEMU_OPTION_nographic
},
6127 { "k", HAS_ARG
, QEMU_OPTION_k
},
6129 { "audio-help", 0, QEMU_OPTION_audio_help
},
6130 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
6133 { "net", HAS_ARG
, QEMU_OPTION_net
},
6135 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
6137 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
6139 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
6142 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
6143 { "append", HAS_ARG
, QEMU_OPTION_append
},
6144 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
6146 { "S", 0, QEMU_OPTION_S
},
6147 { "s", 0, QEMU_OPTION_s
},
6148 { "p", HAS_ARG
, QEMU_OPTION_p
},
6149 { "d", HAS_ARG
, QEMU_OPTION_d
},
6150 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
6151 { "L", HAS_ARG
, QEMU_OPTION_L
},
6152 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
6154 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
6155 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
6157 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6158 { "g", 1, QEMU_OPTION_g
},
6160 { "localtime", 0, QEMU_OPTION_localtime
},
6161 { "std-vga", 0, QEMU_OPTION_std_vga
},
6162 { "monitor", 1, QEMU_OPTION_monitor
},
6163 { "serial", 1, QEMU_OPTION_serial
},
6164 { "parallel", 1, QEMU_OPTION_parallel
},
6165 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
6166 { "full-screen", 0, QEMU_OPTION_full_screen
},
6168 { "no-quit", 0, QEMU_OPTION_no_quit
},
6170 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
6171 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
6172 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
6173 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
6174 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
6176 /* temporary options */
6177 { "usb", 0, QEMU_OPTION_usb
},
6178 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
6179 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
6180 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
6184 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
6186 /* this stack is only used during signal handling */
6187 #define SIGNAL_STACK_SIZE 32768
6189 static uint8_t *signal_stack
;
6193 /* password input */
6195 static BlockDriverState
*get_bdrv(int index
)
6197 BlockDriverState
*bs
;
6200 bs
= bs_table
[index
];
6201 } else if (index
< 6) {
6202 bs
= fd_table
[index
- 4];
6209 static void read_passwords(void)
6211 BlockDriverState
*bs
;
6215 for(i
= 0; i
< 6; i
++) {
6217 if (bs
&& bdrv_is_encrypted(bs
)) {
6218 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
6219 for(j
= 0; j
< 3; j
++) {
6220 monitor_readline("Password: ",
6221 1, password
, sizeof(password
));
6222 if (bdrv_set_key(bs
, password
) == 0)
6224 term_printf("invalid password\n");
6230 /* XXX: currently we cannot use simultaneously different CPUs */
6231 void register_machines(void)
6233 #if defined(TARGET_I386)
6234 qemu_register_machine(&pc_machine
);
6235 qemu_register_machine(&isapc_machine
);
6236 #elif defined(TARGET_PPC)
6237 qemu_register_machine(&heathrow_machine
);
6238 qemu_register_machine(&core99_machine
);
6239 qemu_register_machine(&prep_machine
);
6240 #elif defined(TARGET_MIPS)
6241 qemu_register_machine(&mips_machine
);
6242 #elif defined(TARGET_SPARC)
6243 #ifdef TARGET_SPARC64
6244 qemu_register_machine(&sun4u_machine
);
6246 qemu_register_machine(&sun4m_machine
);
6248 #elif defined(TARGET_ARM)
6249 qemu_register_machine(&integratorcp926_machine
);
6250 qemu_register_machine(&integratorcp1026_machine
);
6251 qemu_register_machine(&versatilepb_machine
);
6252 qemu_register_machine(&versatileab_machine
);
6253 qemu_register_machine(&realview_machine
);
6254 #elif defined(TARGET_SH4)
6255 qemu_register_machine(&shix_machine
);
6257 #error unsupported CPU
6262 struct soundhw soundhw
[] = {
6269 { .init_isa
= pcspk_audio_init
}
6274 "Creative Sound Blaster 16",
6277 { .init_isa
= SB16_init
}
6284 "Yamaha YMF262 (OPL3)",
6286 "Yamaha YM3812 (OPL2)",
6290 { .init_isa
= Adlib_init
}
6297 "Gravis Ultrasound GF1",
6300 { .init_isa
= GUS_init
}
6306 "ENSONIQ AudioPCI ES1370",
6309 { .init_pci
= es1370_init
}
6312 { NULL
, NULL
, 0, 0, { NULL
} }
6315 static void select_soundhw (const char *optarg
)
6319 if (*optarg
== '?') {
6322 printf ("Valid sound card names (comma separated):\n");
6323 for (c
= soundhw
; c
->name
; ++c
) {
6324 printf ("%-11s %s\n", c
->name
, c
->descr
);
6326 printf ("\n-soundhw all will enable all of the above\n");
6327 exit (*optarg
!= '?');
6335 if (!strcmp (optarg
, "all")) {
6336 for (c
= soundhw
; c
->name
; ++c
) {
6344 e
= strchr (p
, ',');
6345 l
= !e
? strlen (p
) : (size_t) (e
- p
);
6347 for (c
= soundhw
; c
->name
; ++c
) {
6348 if (!strncmp (c
->name
, p
, l
)) {
6357 "Unknown sound card name (too big to show)\n");
6360 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
6365 p
+= l
+ (e
!= NULL
);
6369 goto show_valid_cards
;
6375 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
6377 exit(STATUS_CONTROL_C_EXIT
);
6382 #define MAX_NET_CLIENTS 32
6384 int main(int argc
, char **argv
)
6386 #ifdef CONFIG_GDBSTUB
6387 int use_gdbstub
, gdbstub_port
;
6390 int snapshot
, linux_boot
;
6391 const char *initrd_filename
;
6392 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
6393 const char *kernel_filename
, *kernel_cmdline
;
6394 DisplayState
*ds
= &display_state
;
6395 int cyls
, heads
, secs
, translation
;
6396 int start_emulation
= 1;
6397 char net_clients
[MAX_NET_CLIENTS
][256];
6400 const char *r
, *optarg
;
6401 CharDriverState
*monitor_hd
;
6402 char monitor_device
[128];
6403 char serial_devices
[MAX_SERIAL_PORTS
][128];
6404 int serial_device_index
;
6405 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
6406 int parallel_device_index
;
6407 const char *loadvm
= NULL
;
6408 QEMUMachine
*machine
;
6409 char usb_devices
[MAX_USB_CMDLINE
][128];
6410 int usb_devices_index
;
6412 LIST_INIT (&vm_change_state_head
);
6415 struct sigaction act
;
6416 sigfillset(&act
.sa_mask
);
6418 act
.sa_handler
= SIG_IGN
;
6419 sigaction(SIGPIPE
, &act
, NULL
);
6422 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
6423 /* Note: cpu_interrupt() is currently not SMP safe, so we force
6424 QEMU to run on a single CPU */
6429 h
= GetCurrentProcess();
6430 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
6431 for(i
= 0; i
< 32; i
++) {
6432 if (mask
& (1 << i
))
6437 SetProcessAffinityMask(h
, mask
);
6443 register_machines();
6444 machine
= first_machine
;
6445 initrd_filename
= NULL
;
6446 for(i
= 0; i
< MAX_FD
; i
++)
6447 fd_filename
[i
] = NULL
;
6448 for(i
= 0; i
< MAX_DISKS
; i
++)
6449 hd_filename
[i
] = NULL
;
6450 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
6451 vga_ram_size
= VGA_RAM_SIZE
;
6452 bios_size
= BIOS_SIZE
;
6453 #ifdef CONFIG_GDBSTUB
6455 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
6459 kernel_filename
= NULL
;
6460 kernel_cmdline
= "";
6466 cyls
= heads
= secs
= 0;
6467 translation
= BIOS_ATA_TRANSLATION_AUTO
;
6468 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
6470 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
6471 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
6472 serial_devices
[i
][0] = '\0';
6473 serial_device_index
= 0;
6475 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
6476 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
6477 parallel_devices
[i
][0] = '\0';
6478 parallel_device_index
= 0;
6480 usb_devices_index
= 0;
6485 /* default mac address of the first network interface */
6493 hd_filename
[0] = argv
[optind
++];
6495 const QEMUOption
*popt
;
6498 popt
= qemu_options
;
6501 fprintf(stderr
, "%s: invalid option -- '%s'\n",
6505 if (!strcmp(popt
->name
, r
+ 1))
6509 if (popt
->flags
& HAS_ARG
) {
6510 if (optind
>= argc
) {
6511 fprintf(stderr
, "%s: option '%s' requires an argument\n",
6515 optarg
= argv
[optind
++];
6520 switch(popt
->index
) {
6522 machine
= find_machine(optarg
);
6525 printf("Supported machines are:\n");
6526 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6527 printf("%-10s %s%s\n",
6529 m
== first_machine
? " (default)" : "");
6534 case QEMU_OPTION_initrd
:
6535 initrd_filename
= optarg
;
6537 case QEMU_OPTION_hda
:
6538 case QEMU_OPTION_hdb
:
6539 case QEMU_OPTION_hdc
:
6540 case QEMU_OPTION_hdd
:
6543 hd_index
= popt
->index
- QEMU_OPTION_hda
;
6544 hd_filename
[hd_index
] = optarg
;
6545 if (hd_index
== cdrom_index
)
6549 case QEMU_OPTION_snapshot
:
6552 case QEMU_OPTION_hdachs
:
6556 cyls
= strtol(p
, (char **)&p
, 0);
6557 if (cyls
< 1 || cyls
> 16383)
6562 heads
= strtol(p
, (char **)&p
, 0);
6563 if (heads
< 1 || heads
> 16)
6568 secs
= strtol(p
, (char **)&p
, 0);
6569 if (secs
< 1 || secs
> 63)
6573 if (!strcmp(p
, "none"))
6574 translation
= BIOS_ATA_TRANSLATION_NONE
;
6575 else if (!strcmp(p
, "lba"))
6576 translation
= BIOS_ATA_TRANSLATION_LBA
;
6577 else if (!strcmp(p
, "auto"))
6578 translation
= BIOS_ATA_TRANSLATION_AUTO
;
6581 } else if (*p
!= '\0') {
6583 fprintf(stderr
, "qemu: invalid physical CHS format\n");
6588 case QEMU_OPTION_nographic
:
6589 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
6590 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
6593 case QEMU_OPTION_kernel
:
6594 kernel_filename
= optarg
;
6596 case QEMU_OPTION_append
:
6597 kernel_cmdline
= optarg
;
6599 case QEMU_OPTION_cdrom
:
6600 if (cdrom_index
>= 0) {
6601 hd_filename
[cdrom_index
] = optarg
;
6604 case QEMU_OPTION_boot
:
6605 boot_device
= optarg
[0];
6606 if (boot_device
!= 'a' &&
6609 boot_device
!= 'n' &&
6611 boot_device
!= 'c' && boot_device
!= 'd') {
6612 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
6616 case QEMU_OPTION_fda
:
6617 fd_filename
[0] = optarg
;
6619 case QEMU_OPTION_fdb
:
6620 fd_filename
[1] = optarg
;
6623 case QEMU_OPTION_no_fd_bootchk
:
6627 case QEMU_OPTION_no_code_copy
:
6628 code_copy_enabled
= 0;
6630 case QEMU_OPTION_net
:
6631 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
6632 fprintf(stderr
, "qemu: too many network clients\n");
6635 pstrcpy(net_clients
[nb_net_clients
],
6636 sizeof(net_clients
[0]),
6641 case QEMU_OPTION_tftp
:
6642 tftp_prefix
= optarg
;
6645 case QEMU_OPTION_smb
:
6646 net_slirp_smb(optarg
);
6649 case QEMU_OPTION_redir
:
6650 net_slirp_redir(optarg
);
6654 case QEMU_OPTION_audio_help
:
6658 case QEMU_OPTION_soundhw
:
6659 select_soundhw (optarg
);
6666 ram_size
= atoi(optarg
) * 1024 * 1024;
6669 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
6670 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
6671 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
6680 mask
= cpu_str_to_log_mask(optarg
);
6682 printf("Log items (comma separated):\n");
6683 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
6684 printf("%-10s %s\n", item
->name
, item
->help
);
6691 #ifdef CONFIG_GDBSTUB
6696 gdbstub_port
= atoi(optarg
);
6703 start_emulation
= 0;
6706 keyboard_layout
= optarg
;
6708 case QEMU_OPTION_localtime
:
6711 case QEMU_OPTION_cirrusvga
:
6712 cirrus_vga_enabled
= 1;
6714 case QEMU_OPTION_std_vga
:
6715 cirrus_vga_enabled
= 0;
6722 w
= strtol(p
, (char **)&p
, 10);
6725 fprintf(stderr
, "qemu: invalid resolution or depth\n");
6731 h
= strtol(p
, (char **)&p
, 10);
6736 depth
= strtol(p
, (char **)&p
, 10);
6737 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
6738 depth
!= 24 && depth
!= 32)
6740 } else if (*p
== '\0') {
6741 depth
= graphic_depth
;
6748 graphic_depth
= depth
;
6751 case QEMU_OPTION_monitor
:
6752 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
6754 case QEMU_OPTION_serial
:
6755 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
6756 fprintf(stderr
, "qemu: too many serial ports\n");
6759 pstrcpy(serial_devices
[serial_device_index
],
6760 sizeof(serial_devices
[0]), optarg
);
6761 serial_device_index
++;
6763 case QEMU_OPTION_parallel
:
6764 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
6765 fprintf(stderr
, "qemu: too many parallel ports\n");
6768 pstrcpy(parallel_devices
[parallel_device_index
],
6769 sizeof(parallel_devices
[0]), optarg
);
6770 parallel_device_index
++;
6772 case QEMU_OPTION_loadvm
:
6775 case QEMU_OPTION_full_screen
:
6779 case QEMU_OPTION_no_quit
:
6783 case QEMU_OPTION_pidfile
:
6784 create_pidfile(optarg
);
6787 case QEMU_OPTION_win2k_hack
:
6788 win2k_install_hack
= 1;
6792 case QEMU_OPTION_no_kqemu
:
6795 case QEMU_OPTION_kernel_kqemu
:
6799 case QEMU_OPTION_usb
:
6802 case QEMU_OPTION_usbdevice
:
6804 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
6805 fprintf(stderr
, "Too many USB devices\n");
6808 pstrcpy(usb_devices
[usb_devices_index
],
6809 sizeof(usb_devices
[usb_devices_index
]),
6811 usb_devices_index
++;
6813 case QEMU_OPTION_smp
:
6814 smp_cpus
= atoi(optarg
);
6815 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
6816 fprintf(stderr
, "Invalid number of CPUs\n");
6820 case QEMU_OPTION_vnc
:
6821 vnc_display
= optarg
;
6823 case QEMU_OPTION_no_acpi
:
6826 case QEMU_OPTION_no_reboot
:
6837 linux_boot
= (kernel_filename
!= NULL
);
6840 hd_filename
[0] == '\0' &&
6841 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
6842 fd_filename
[0] == '\0')
6845 /* boot to cd by default if no hard disk */
6846 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
6847 if (fd_filename
[0] != '\0')
6853 setvbuf(stdout
, NULL
, _IOLBF
, 0);
6863 /* init network clients */
6864 if (nb_net_clients
== 0) {
6865 /* if no clients, we use a default config */
6866 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
6868 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
6873 for(i
= 0;i
< nb_net_clients
; i
++) {
6874 if (net_client_init(net_clients
[i
]) < 0)
6878 /* init the memory */
6879 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
6881 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
6882 if (!phys_ram_base
) {
6883 fprintf(stderr
, "Could not allocate physical memory\n");
6887 /* we always create the cdrom drive, even if no disk is there */
6889 if (cdrom_index
>= 0) {
6890 bs_table
[cdrom_index
] = bdrv_new("cdrom");
6891 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
6894 /* open the virtual block devices */
6895 for(i
= 0; i
< MAX_DISKS
; i
++) {
6896 if (hd_filename
[i
]) {
6899 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
6900 bs_table
[i
] = bdrv_new(buf
);
6902 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
6903 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
6907 if (i
== 0 && cyls
!= 0) {
6908 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
6909 bdrv_set_translation_hint(bs_table
[i
], translation
);
6914 /* we always create at least one floppy disk */
6915 fd_table
[0] = bdrv_new("fda");
6916 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
6918 for(i
= 0; i
< MAX_FD
; i
++) {
6919 if (fd_filename
[i
]) {
6922 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
6923 fd_table
[i
] = bdrv_new(buf
);
6924 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
6926 if (fd_filename
[i
] != '\0') {
6927 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
6928 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
6929 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
6937 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
6938 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
6944 dumb_display_init(ds
);
6945 } else if (vnc_display
!= NULL
) {
6946 vnc_display_init(ds
, vnc_display
);
6948 #if defined(CONFIG_SDL)
6949 sdl_display_init(ds
, full_screen
);
6950 #elif defined(CONFIG_COCOA)
6951 cocoa_display_init(ds
, full_screen
);
6953 dumb_display_init(ds
);
6957 monitor_hd
= qemu_chr_open(monitor_device
);
6959 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
6962 monitor_init(monitor_hd
, !nographic
);
6964 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6965 const char *devname
= serial_devices
[i
];
6966 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
6967 serial_hds
[i
] = qemu_chr_open(devname
);
6968 if (!serial_hds
[i
]) {
6969 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
6973 if (!strcmp(devname
, "vc"))
6974 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6978 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6979 const char *devname
= parallel_devices
[i
];
6980 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
6981 parallel_hds
[i
] = qemu_chr_open(devname
);
6982 if (!parallel_hds
[i
]) {
6983 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
6987 if (!strcmp(devname
, "vc"))
6988 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6992 machine
->init(ram_size
, vga_ram_size
, boot_device
,
6993 ds
, fd_filename
, snapshot
,
6994 kernel_filename
, kernel_cmdline
, initrd_filename
);
6996 /* init USB devices */
6998 for(i
= 0; i
< usb_devices_index
; i
++) {
6999 if (usb_device_add(usb_devices
[i
]) < 0) {
7000 fprintf(stderr
, "Warning: could not add USB device %s\n",
7006 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
7007 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
7009 #ifdef CONFIG_GDBSTUB
7011 if (gdbserver_start(gdbstub_port
) < 0) {
7012 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
7016 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
7024 /* XXX: simplify init */
7026 if (start_emulation
) {