4 * Copyright (c) 2003-2005 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
34 #include <sys/times.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
52 #include <linux/if_tun.h>
55 #include <linux/rtc.h>
56 #include <linux/ppdev.h>
61 #if defined(CONFIG_SLIRP)
67 #include <sys/timeb.h>
69 #define getopt_long_only getopt_long
70 #define memalign(align, size) malloc(size)
73 #include "qemu_socket.h"
79 #endif /* CONFIG_SDL */
83 #define main qemu_main
84 #endif /* CONFIG_COCOA */
90 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
92 //#define DEBUG_UNUSED_IOPORT
93 //#define DEBUG_IOPORT
95 #if !defined(CONFIG_SOFTMMU)
96 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
98 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
102 #define DEFAULT_RAM_SIZE 144
104 #define DEFAULT_RAM_SIZE 128
107 #define GUI_REFRESH_INTERVAL 30
109 /* Max number of USB devices that can be specified on the commandline. */
110 #define MAX_USB_CMDLINE 8
112 /* XXX: use a two level table to limit memory usage */
113 #define MAX_IOPORTS 65536
115 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
116 char phys_ram_file
[1024];
117 void *ioport_opaque
[MAX_IOPORTS
];
118 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
119 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
120 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
123 static DisplayState display_state
;
125 const char* keyboard_layout
= NULL
;
126 int64_t ticks_per_sec
;
127 int boot_device
= 'c';
129 int pit_min_timer_count
= 0;
131 NICInfo nd_table
[MAX_NICS
];
132 QEMUTimer
*gui_timer
;
135 int cirrus_vga_enabled
= 1;
137 int graphic_width
= 1024;
138 int graphic_height
= 768;
140 int graphic_width
= 800;
141 int graphic_height
= 600;
143 int graphic_depth
= 15;
145 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
146 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
148 int win2k_install_hack
= 0;
151 static VLANState
*first_vlan
;
153 int vnc_display
= -1;
154 #if defined(TARGET_SPARC)
156 #elif defined(TARGET_I386)
161 int acpi_enabled
= 1;
164 /***********************************************************/
165 /* x86 ISA bus support */
167 target_phys_addr_t isa_mem_base
= 0;
170 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
172 #ifdef DEBUG_UNUSED_IOPORT
173 fprintf(stderr
, "inb: port=0x%04x\n", address
);
178 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
180 #ifdef DEBUG_UNUSED_IOPORT
181 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
185 /* default is to make two byte accesses */
186 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
189 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
190 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
191 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
195 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
197 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
198 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
199 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
202 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
204 #ifdef DEBUG_UNUSED_IOPORT
205 fprintf(stderr
, "inl: port=0x%04x\n", address
);
210 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
212 #ifdef DEBUG_UNUSED_IOPORT
213 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
217 void init_ioports(void)
221 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
222 ioport_read_table
[0][i
] = default_ioport_readb
;
223 ioport_write_table
[0][i
] = default_ioport_writeb
;
224 ioport_read_table
[1][i
] = default_ioport_readw
;
225 ioport_write_table
[1][i
] = default_ioport_writew
;
226 ioport_read_table
[2][i
] = default_ioport_readl
;
227 ioport_write_table
[2][i
] = default_ioport_writel
;
231 /* size is the word size in byte */
232 int register_ioport_read(int start
, int length
, int size
,
233 IOPortReadFunc
*func
, void *opaque
)
239 } else if (size
== 2) {
241 } else if (size
== 4) {
244 hw_error("register_ioport_read: invalid size");
247 for(i
= start
; i
< start
+ length
; i
+= size
) {
248 ioport_read_table
[bsize
][i
] = func
;
249 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
250 hw_error("register_ioport_read: invalid opaque");
251 ioport_opaque
[i
] = opaque
;
256 /* size is the word size in byte */
257 int register_ioport_write(int start
, int length
, int size
,
258 IOPortWriteFunc
*func
, void *opaque
)
264 } else if (size
== 2) {
266 } else if (size
== 4) {
269 hw_error("register_ioport_write: invalid size");
272 for(i
= start
; i
< start
+ length
; i
+= size
) {
273 ioport_write_table
[bsize
][i
] = func
;
274 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
275 hw_error("register_ioport_read: invalid opaque");
276 ioport_opaque
[i
] = opaque
;
281 void isa_unassign_ioport(int start
, int length
)
285 for(i
= start
; i
< start
+ length
; i
++) {
286 ioport_read_table
[0][i
] = default_ioport_readb
;
287 ioport_read_table
[1][i
] = default_ioport_readw
;
288 ioport_read_table
[2][i
] = default_ioport_readl
;
290 ioport_write_table
[0][i
] = default_ioport_writeb
;
291 ioport_write_table
[1][i
] = default_ioport_writew
;
292 ioport_write_table
[2][i
] = default_ioport_writel
;
296 /***********************************************************/
298 void pstrcpy(char *buf
, int buf_size
, const char *str
)
308 if (c
== 0 || q
>= buf
+ buf_size
- 1)
315 /* strcat and truncate. */
316 char *pstrcat(char *buf
, int buf_size
, const char *s
)
321 pstrcpy(buf
+ len
, buf_size
- len
, s
);
325 int strstart(const char *str
, const char *val
, const char **ptr
)
341 void cpu_outb(CPUState
*env
, int addr
, int val
)
344 if (loglevel
& CPU_LOG_IOPORT
)
345 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
347 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
350 env
->last_io_time
= cpu_get_time_fast();
354 void cpu_outw(CPUState
*env
, int addr
, int val
)
357 if (loglevel
& CPU_LOG_IOPORT
)
358 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
360 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
363 env
->last_io_time
= cpu_get_time_fast();
367 void cpu_outl(CPUState
*env
, int addr
, int val
)
370 if (loglevel
& CPU_LOG_IOPORT
)
371 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
373 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
376 env
->last_io_time
= cpu_get_time_fast();
380 int cpu_inb(CPUState
*env
, int addr
)
383 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
385 if (loglevel
& CPU_LOG_IOPORT
)
386 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
390 env
->last_io_time
= cpu_get_time_fast();
395 int cpu_inw(CPUState
*env
, int addr
)
398 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
400 if (loglevel
& CPU_LOG_IOPORT
)
401 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
405 env
->last_io_time
= cpu_get_time_fast();
410 int cpu_inl(CPUState
*env
, int addr
)
413 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
415 if (loglevel
& CPU_LOG_IOPORT
)
416 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
420 env
->last_io_time
= cpu_get_time_fast();
425 /***********************************************************/
426 void hw_error(const char *fmt
, ...)
432 fprintf(stderr
, "qemu: hardware error: ");
433 vfprintf(stderr
, fmt
, ap
);
434 fprintf(stderr
, "\n");
435 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
436 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
438 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
440 cpu_dump_state(env
, stderr
, fprintf
, 0);
447 /***********************************************************/
450 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
451 static void *qemu_put_kbd_event_opaque
;
452 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
453 static void *qemu_put_mouse_event_opaque
;
454 static int qemu_put_mouse_event_absolute
;
456 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
458 qemu_put_kbd_event_opaque
= opaque
;
459 qemu_put_kbd_event
= func
;
462 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
, int absolute
)
464 qemu_put_mouse_event_opaque
= opaque
;
465 qemu_put_mouse_event
= func
;
466 qemu_put_mouse_event_absolute
= absolute
;
469 void kbd_put_keycode(int keycode
)
471 if (qemu_put_kbd_event
) {
472 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
476 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
478 if (qemu_put_mouse_event
) {
479 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
480 dx
, dy
, dz
, buttons_state
);
484 int kbd_mouse_is_absolute(void)
486 return qemu_put_mouse_event_absolute
;
489 /***********************************************************/
492 #if defined(__powerpc__)
494 static inline uint32_t get_tbl(void)
497 asm volatile("mftb %0" : "=r" (tbl
));
501 static inline uint32_t get_tbu(void)
504 asm volatile("mftbu %0" : "=r" (tbl
));
508 int64_t cpu_get_real_ticks(void)
511 /* NOTE: we test if wrapping has occurred */
517 return ((int64_t)h
<< 32) | l
;
520 #elif defined(__i386__)
522 int64_t cpu_get_real_ticks(void)
526 QueryPerformanceCounter(&ti
);
530 asm volatile ("rdtsc" : "=A" (val
));
535 #elif defined(__x86_64__)
537 int64_t cpu_get_real_ticks(void)
541 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
548 #elif defined(__ia64)
550 int64_t cpu_get_real_ticks(void)
553 asm volatile ("mov %0 = ar.itc" : "=r"(val
) :: "memory");
557 #elif defined(__s390__)
559 int64_t cpu_get_real_ticks(void)
562 asm volatile("stck 0(%1)" : "=m" (val
) : "a" (&val
) : "cc");
566 #elif defined(__sparc__) && defined(HOST_SOLARIS)
568 uint64_t cpu_get_real_ticks (void)
572 asm volatile("rd %%tick,%0" : "=r"(rval
));
582 asm volatile("rd %%tick,%1; srlx %1,32,%0"
583 : "=r"(rval
.i32
.high
), "=r"(rval
.i32
.low
));
589 #error unsupported CPU
592 static int64_t cpu_ticks_prev
;
593 static int64_t cpu_ticks_offset
;
594 static int cpu_ticks_enabled
;
596 static inline int64_t cpu_get_ticks(void)
598 if (!cpu_ticks_enabled
) {
599 return cpu_ticks_offset
;
602 ticks
= cpu_get_real_ticks();
603 if (cpu_ticks_prev
> ticks
) {
604 /* Note: non increasing ticks may happen if the host uses
606 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
608 cpu_ticks_prev
= ticks
;
609 return ticks
+ cpu_ticks_offset
;
613 /* enable cpu_get_ticks() */
614 void cpu_enable_ticks(void)
616 if (!cpu_ticks_enabled
) {
617 cpu_ticks_offset
-= cpu_get_real_ticks();
618 cpu_ticks_enabled
= 1;
622 /* disable cpu_get_ticks() : the clock is stopped. You must not call
623 cpu_get_ticks() after that. */
624 void cpu_disable_ticks(void)
626 if (cpu_ticks_enabled
) {
627 cpu_ticks_offset
= cpu_get_ticks();
628 cpu_ticks_enabled
= 0;
633 void cpu_calibrate_ticks(void)
638 ret
= QueryPerformanceFrequency(&freq
);
640 fprintf(stderr
, "Could not calibrate ticks\n");
643 ticks_per_sec
= freq
.QuadPart
;
647 static int64_t get_clock(void)
650 gettimeofday(&tv
, NULL
);
651 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
654 void cpu_calibrate_ticks(void)
659 ticks
= cpu_get_real_ticks();
661 usec
= get_clock() - usec
;
662 ticks
= cpu_get_real_ticks() - ticks
;
663 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
667 /* compute with 96 bit intermediate result: (a*b)/c */
668 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
673 #ifdef WORDS_BIGENDIAN
683 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
684 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
687 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
691 #define QEMU_TIMER_REALTIME 0
692 #define QEMU_TIMER_VIRTUAL 1
696 /* XXX: add frequency */
704 struct QEMUTimer
*next
;
710 static QEMUTimer
*active_timers
[2];
712 static MMRESULT timerID
;
713 static HANDLE host_alarm
= NULL
;
714 static unsigned int period
= 1;
716 /* frequency of the times() clock tick */
717 static int timer_freq
;
720 QEMUClock
*qemu_new_clock(int type
)
723 clock
= qemu_mallocz(sizeof(QEMUClock
));
730 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
734 ts
= qemu_mallocz(sizeof(QEMUTimer
));
741 void qemu_free_timer(QEMUTimer
*ts
)
746 /* stop a timer, but do not dealloc it */
747 void qemu_del_timer(QEMUTimer
*ts
)
751 /* NOTE: this code must be signal safe because
752 qemu_timer_expired() can be called from a signal. */
753 pt
= &active_timers
[ts
->clock
->type
];
766 /* modify the current timer so that it will be fired when current_time
767 >= expire_time. The corresponding callback will be called. */
768 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
774 /* add the timer in the sorted list */
775 /* NOTE: this code must be signal safe because
776 qemu_timer_expired() can be called from a signal. */
777 pt
= &active_timers
[ts
->clock
->type
];
782 if (t
->expire_time
> expire_time
)
786 ts
->expire_time
= expire_time
;
791 int qemu_timer_pending(QEMUTimer
*ts
)
794 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
801 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
805 return (timer_head
->expire_time
<= current_time
);
808 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
814 if (!ts
|| ts
->expire_time
> current_time
)
816 /* remove timer from the list before calling the callback */
817 *ptimer_head
= ts
->next
;
820 /* run the callback (the timer list can be modified) */
825 int64_t qemu_get_clock(QEMUClock
*clock
)
827 switch(clock
->type
) {
828 case QEMU_TIMER_REALTIME
:
830 return GetTickCount();
835 /* Note that using gettimeofday() is not a good solution
836 for timers because its value change when the date is
838 if (timer_freq
== 100) {
839 return times(&tp
) * 10;
841 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
846 case QEMU_TIMER_VIRTUAL
:
847 return cpu_get_ticks();
852 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
854 uint64_t expire_time
;
856 if (qemu_timer_pending(ts
)) {
857 expire_time
= ts
->expire_time
;
861 qemu_put_be64(f
, expire_time
);
864 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
866 uint64_t expire_time
;
868 expire_time
= qemu_get_be64(f
);
869 if (expire_time
!= -1) {
870 qemu_mod_timer(ts
, expire_time
);
876 static void timer_save(QEMUFile
*f
, void *opaque
)
878 if (cpu_ticks_enabled
) {
879 hw_error("cannot save state if virtual timers are running");
881 qemu_put_be64s(f
, &cpu_ticks_offset
);
882 qemu_put_be64s(f
, &ticks_per_sec
);
885 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
889 if (cpu_ticks_enabled
) {
892 qemu_get_be64s(f
, &cpu_ticks_offset
);
893 qemu_get_be64s(f
, &ticks_per_sec
);
898 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
899 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
901 static void host_alarm_handler(int host_signum
)
905 #define DISP_FREQ 1000
907 static int64_t delta_min
= INT64_MAX
;
908 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
910 ti
= qemu_get_clock(vm_clock
);
911 if (last_clock
!= 0) {
912 delta
= ti
- last_clock
;
913 if (delta
< delta_min
)
915 if (delta
> delta_max
)
918 if (++count
== DISP_FREQ
) {
919 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
920 muldiv64(delta_min
, 1000000, ticks_per_sec
),
921 muldiv64(delta_max
, 1000000, ticks_per_sec
),
922 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
923 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
925 delta_min
= INT64_MAX
;
933 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
934 qemu_get_clock(vm_clock
)) ||
935 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
936 qemu_get_clock(rt_clock
))) {
938 SetEvent(host_alarm
);
940 CPUState
*env
= cpu_single_env
;
942 /* stop the currently executing cpu because a timer occured */
943 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
945 if (env
->kqemu_enabled
) {
946 kqemu_cpu_interrupt(env
);
955 #if defined(__linux__)
957 #define RTC_FREQ 1024
961 static int start_rtc_timer(void)
963 rtc_fd
= open("/dev/rtc", O_RDONLY
);
966 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
967 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
968 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
969 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
972 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
977 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
983 static int start_rtc_timer(void)
988 #endif /* !defined(__linux__) */
990 #endif /* !defined(_WIN32) */
992 static void init_timers(void)
994 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
995 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1002 ZeroMemory(&tc
, sizeof(TIMECAPS
));
1003 timeGetDevCaps(&tc
, sizeof(TIMECAPS
));
1004 if (period
< tc
.wPeriodMin
)
1005 period
= tc
.wPeriodMin
;
1006 timeBeginPeriod(period
);
1007 timerID
= timeSetEvent(1, // interval (ms)
1008 period
, // resolution
1009 host_alarm_handler
, // function
1010 (DWORD
)&count
, // user parameter
1011 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
1013 perror("failed timer alarm");
1016 host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1018 perror("failed CreateEvent");
1021 ResetEvent(host_alarm
);
1023 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
1026 struct sigaction act
;
1027 struct itimerval itv
;
1029 /* get times() syscall frequency */
1030 timer_freq
= sysconf(_SC_CLK_TCK
);
1033 sigfillset(&act
.sa_mask
);
1035 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1036 act
.sa_flags
|= SA_ONSTACK
;
1038 act
.sa_handler
= host_alarm_handler
;
1039 sigaction(SIGALRM
, &act
, NULL
);
1041 itv
.it_interval
.tv_sec
= 0;
1042 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
1043 itv
.it_value
.tv_sec
= 0;
1044 itv
.it_value
.tv_usec
= 10 * 1000;
1045 setitimer(ITIMER_REAL
, &itv
, NULL
);
1046 /* we probe the tick duration of the kernel to inform the user if
1047 the emulated kernel requested a too high timer frequency */
1048 getitimer(ITIMER_REAL
, &itv
);
1050 #if defined(__linux__)
1051 /* XXX: force /dev/rtc usage because even 2.6 kernels may not
1052 have timers with 1 ms resolution. The correct solution will
1053 be to use the POSIX real time timers available in recent
1055 if (itv
.it_interval
.tv_usec
> 1000 || 1) {
1056 /* try to use /dev/rtc to have a faster timer */
1057 if (start_rtc_timer() < 0)
1059 /* disable itimer */
1060 itv
.it_interval
.tv_sec
= 0;
1061 itv
.it_interval
.tv_usec
= 0;
1062 itv
.it_value
.tv_sec
= 0;
1063 itv
.it_value
.tv_usec
= 0;
1064 setitimer(ITIMER_REAL
, &itv
, NULL
);
1067 sigaction(SIGIO
, &act
, NULL
);
1068 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1069 fcntl(rtc_fd
, F_SETOWN
, getpid());
1071 #endif /* defined(__linux__) */
1074 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1075 PIT_FREQ
) / 1000000;
1081 void quit_timers(void)
1084 timeKillEvent(timerID
);
1085 timeEndPeriod(period
);
1087 CloseHandle(host_alarm
);
1093 /***********************************************************/
1094 /* character device */
1096 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1098 return s
->chr_write(s
, buf
, len
);
1101 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1105 return s
->chr_ioctl(s
, cmd
, arg
);
1108 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1113 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1114 qemu_chr_write(s
, buf
, strlen(buf
));
1118 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1120 if (s
->chr_send_event
)
1121 s
->chr_send_event(s
, event
);
1124 void qemu_chr_add_read_handler(CharDriverState
*s
,
1125 IOCanRWHandler
*fd_can_read
,
1126 IOReadHandler
*fd_read
, void *opaque
)
1128 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
1131 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
1133 s
->chr_event
= chr_event
;
1136 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1141 static void null_chr_add_read_handler(CharDriverState
*chr
,
1142 IOCanRWHandler
*fd_can_read
,
1143 IOReadHandler
*fd_read
, void *opaque
)
1147 CharDriverState
*qemu_chr_open_null(void)
1149 CharDriverState
*chr
;
1151 chr
= qemu_mallocz(sizeof(CharDriverState
));
1154 chr
->chr_write
= null_chr_write
;
1155 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1161 static void socket_cleanup(void)
1166 static int socket_init(void)
1171 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1173 err
= WSAGetLastError();
1174 fprintf(stderr
, "WSAStartup: %d\n", err
);
1177 atexit(socket_cleanup
);
1181 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1187 ret
= send(fd
, buf
, len
, 0);
1190 errno
= WSAGetLastError();
1191 if (errno
!= WSAEWOULDBLOCK
) {
1194 } else if (ret
== 0) {
1204 void socket_set_nonblock(int fd
)
1206 unsigned long opt
= 1;
1207 ioctlsocket(fd
, FIONBIO
, &opt
);
1212 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1218 ret
= write(fd
, buf
, len
);
1220 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1222 } else if (ret
== 0) {
1232 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1234 return unix_write(fd
, buf
, len1
);
1237 void socket_set_nonblock(int fd
)
1239 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1241 #endif /* !_WIN32 */
1247 IOCanRWHandler
*fd_can_read
;
1248 IOReadHandler
*fd_read
;
1253 #define STDIO_MAX_CLIENTS 2
1255 static int stdio_nb_clients
;
1256 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1258 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1260 FDCharDriver
*s
= chr
->opaque
;
1261 return unix_write(s
->fd_out
, buf
, len
);
1264 static int fd_chr_read_poll(void *opaque
)
1266 CharDriverState
*chr
= opaque
;
1267 FDCharDriver
*s
= chr
->opaque
;
1269 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
1273 static void fd_chr_read(void *opaque
)
1275 CharDriverState
*chr
= opaque
;
1276 FDCharDriver
*s
= chr
->opaque
;
1281 if (len
> s
->max_size
)
1285 size
= read(s
->fd_in
, buf
, len
);
1287 s
->fd_read(s
->fd_opaque
, buf
, size
);
1291 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1292 IOCanRWHandler
*fd_can_read
,
1293 IOReadHandler
*fd_read
, void *opaque
)
1295 FDCharDriver
*s
= chr
->opaque
;
1297 if (s
->fd_in
>= 0) {
1298 s
->fd_can_read
= fd_can_read
;
1299 s
->fd_read
= fd_read
;
1300 s
->fd_opaque
= opaque
;
1301 if (nographic
&& s
->fd_in
== 0) {
1303 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1304 fd_chr_read
, NULL
, chr
);
1309 /* open a character device to a unix fd */
1310 CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1312 CharDriverState
*chr
;
1315 chr
= qemu_mallocz(sizeof(CharDriverState
));
1318 s
= qemu_mallocz(sizeof(FDCharDriver
));
1326 chr
->chr_write
= fd_chr_write
;
1327 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1331 CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1335 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1338 return qemu_chr_open_fd(-1, fd_out
);
1341 CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1345 fd
= open(filename
, O_RDWR
| O_BINARY
);
1348 return qemu_chr_open_fd(fd
, fd
);
1352 /* for STDIO, we handle the case where several clients use it
1355 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1357 #define TERM_FIFO_MAX_SIZE 1
1359 static int term_got_escape
, client_index
;
1360 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1363 void term_print_help(void)
1366 "C-a h print this help\n"
1367 "C-a x exit emulator\n"
1368 "C-a s save disk data back to file (if -snapshot)\n"
1369 "C-a b send break (magic sysrq)\n"
1370 "C-a c switch between console and monitor\n"
1371 "C-a C-a send C-a\n"
1375 /* called when a char is received */
1376 static void stdio_received_byte(int ch
)
1378 if (term_got_escape
) {
1379 term_got_escape
= 0;
1390 for (i
= 0; i
< MAX_DISKS
; i
++) {
1392 bdrv_commit(bs_table
[i
]);
1397 if (client_index
< stdio_nb_clients
) {
1398 CharDriverState
*chr
;
1401 chr
= stdio_clients
[client_index
];
1403 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1408 if (client_index
>= stdio_nb_clients
)
1410 if (client_index
== 0) {
1411 /* send a new line in the monitor to get the prompt */
1419 } else if (ch
== TERM_ESCAPE
) {
1420 term_got_escape
= 1;
1423 if (client_index
< stdio_nb_clients
) {
1425 CharDriverState
*chr
;
1428 chr
= stdio_clients
[client_index
];
1430 if (s
->fd_can_read(s
->fd_opaque
) > 0) {
1432 s
->fd_read(s
->fd_opaque
, buf
, 1);
1433 } else if (term_fifo_size
== 0) {
1434 term_fifo
[term_fifo_size
++] = ch
;
1440 static int stdio_read_poll(void *opaque
)
1442 CharDriverState
*chr
;
1445 if (client_index
< stdio_nb_clients
) {
1446 chr
= stdio_clients
[client_index
];
1448 /* try to flush the queue if needed */
1449 if (term_fifo_size
!= 0 && s
->fd_can_read(s
->fd_opaque
) > 0) {
1450 s
->fd_read(s
->fd_opaque
, term_fifo
, 1);
1453 /* see if we can absorb more chars */
1454 if (term_fifo_size
== 0)
1463 static void stdio_read(void *opaque
)
1468 size
= read(0, buf
, 1);
1470 stdio_received_byte(buf
[0]);
1473 /* init terminal so that we can grab keys */
1474 static struct termios oldtty
;
1475 static int old_fd0_flags
;
1477 static void term_exit(void)
1479 tcsetattr (0, TCSANOW
, &oldtty
);
1480 fcntl(0, F_SETFL
, old_fd0_flags
);
1483 static void term_init(void)
1487 tcgetattr (0, &tty
);
1489 old_fd0_flags
= fcntl(0, F_GETFL
);
1491 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1492 |INLCR
|IGNCR
|ICRNL
|IXON
);
1493 tty
.c_oflag
|= OPOST
;
1494 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1495 /* if graphical mode, we allow Ctrl-C handling */
1497 tty
.c_lflag
&= ~ISIG
;
1498 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1501 tty
.c_cc
[VTIME
] = 0;
1503 tcsetattr (0, TCSANOW
, &tty
);
1507 fcntl(0, F_SETFL
, O_NONBLOCK
);
1510 CharDriverState
*qemu_chr_open_stdio(void)
1512 CharDriverState
*chr
;
1515 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1517 chr
= qemu_chr_open_fd(0, 1);
1518 if (stdio_nb_clients
== 0)
1519 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, NULL
);
1520 client_index
= stdio_nb_clients
;
1522 if (stdio_nb_clients
!= 0)
1524 chr
= qemu_chr_open_fd(0, 1);
1526 stdio_clients
[stdio_nb_clients
++] = chr
;
1527 if (stdio_nb_clients
== 1) {
1528 /* set the terminal in raw mode */
1534 #if defined(__linux__)
1535 CharDriverState
*qemu_chr_open_pty(void)
1538 char slave_name
[1024];
1539 int master_fd
, slave_fd
;
1541 /* Not satisfying */
1542 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1546 /* Disabling local echo and line-buffered output */
1547 tcgetattr (master_fd
, &tty
);
1548 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1550 tty
.c_cc
[VTIME
] = 0;
1551 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1553 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1554 return qemu_chr_open_fd(master_fd
, master_fd
);
1557 static void tty_serial_init(int fd
, int speed
,
1558 int parity
, int data_bits
, int stop_bits
)
1564 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1565 speed
, parity
, data_bits
, stop_bits
);
1567 tcgetattr (fd
, &tty
);
1609 cfsetispeed(&tty
, spd
);
1610 cfsetospeed(&tty
, spd
);
1612 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1613 |INLCR
|IGNCR
|ICRNL
|IXON
);
1614 tty
.c_oflag
|= OPOST
;
1615 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1616 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
);
1637 tty
.c_cflag
|= PARENB
;
1640 tty
.c_cflag
|= PARENB
| PARODD
;
1644 tcsetattr (fd
, TCSANOW
, &tty
);
1647 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1649 FDCharDriver
*s
= chr
->opaque
;
1652 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1654 QEMUSerialSetParams
*ssp
= arg
;
1655 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1656 ssp
->data_bits
, ssp
->stop_bits
);
1659 case CHR_IOCTL_SERIAL_SET_BREAK
:
1661 int enable
= *(int *)arg
;
1663 tcsendbreak(s
->fd_in
, 1);
1672 CharDriverState
*qemu_chr_open_tty(const char *filename
)
1674 CharDriverState
*chr
;
1677 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1680 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1681 tty_serial_init(fd
, 115200, 'N', 8, 1);
1682 chr
= qemu_chr_open_fd(fd
, fd
);
1685 chr
->chr_ioctl
= tty_serial_ioctl
;
1689 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1691 int fd
= (int)chr
->opaque
;
1695 case CHR_IOCTL_PP_READ_DATA
:
1696 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1698 *(uint8_t *)arg
= b
;
1700 case CHR_IOCTL_PP_WRITE_DATA
:
1701 b
= *(uint8_t *)arg
;
1702 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1705 case CHR_IOCTL_PP_READ_CONTROL
:
1706 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1708 *(uint8_t *)arg
= b
;
1710 case CHR_IOCTL_PP_WRITE_CONTROL
:
1711 b
= *(uint8_t *)arg
;
1712 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1715 case CHR_IOCTL_PP_READ_STATUS
:
1716 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1718 *(uint8_t *)arg
= b
;
1726 CharDriverState
*qemu_chr_open_pp(const char *filename
)
1728 CharDriverState
*chr
;
1731 fd
= open(filename
, O_RDWR
);
1735 if (ioctl(fd
, PPCLAIM
) < 0) {
1740 chr
= qemu_mallocz(sizeof(CharDriverState
));
1745 chr
->opaque
= (void *)fd
;
1746 chr
->chr_write
= null_chr_write
;
1747 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1748 chr
->chr_ioctl
= pp_ioctl
;
1753 CharDriverState
*qemu_chr_open_pty(void)
1759 #endif /* !defined(_WIN32) */
1763 IOCanRWHandler
*fd_can_read
;
1764 IOReadHandler
*fd_read
;
1767 HANDLE hcom
, hrecv
, hsend
;
1768 OVERLAPPED orecv
, osend
;
1773 #define NSENDBUF 2048
1774 #define NRECVBUF 2048
1775 #define MAXCONNECT 1
1776 #define NTIMEOUT 5000
1778 static int win_chr_poll(void *opaque
);
1779 static int win_chr_pipe_poll(void *opaque
);
1781 static void win_chr_close2(WinCharState
*s
)
1784 CloseHandle(s
->hsend
);
1788 CloseHandle(s
->hrecv
);
1792 CloseHandle(s
->hcom
);
1796 qemu_del_polling_cb(win_chr_pipe_poll
, s
);
1798 qemu_del_polling_cb(win_chr_poll
, s
);
1801 static void win_chr_close(CharDriverState
*chr
)
1803 WinCharState
*s
= chr
->opaque
;
1807 static int win_chr_init(WinCharState
*s
, const char *filename
)
1810 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1815 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1817 fprintf(stderr
, "Failed CreateEvent\n");
1820 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1822 fprintf(stderr
, "Failed CreateEvent\n");
1826 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1827 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1828 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1829 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1834 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1835 fprintf(stderr
, "Failed SetupComm\n");
1839 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1840 size
= sizeof(COMMCONFIG
);
1841 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1842 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1843 CommConfigDialog(filename
, NULL
, &comcfg
);
1845 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1846 fprintf(stderr
, "Failed SetCommState\n");
1850 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1851 fprintf(stderr
, "Failed SetCommMask\n");
1855 cto
.ReadIntervalTimeout
= MAXDWORD
;
1856 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1857 fprintf(stderr
, "Failed SetCommTimeouts\n");
1861 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1862 fprintf(stderr
, "Failed ClearCommError\n");
1865 qemu_add_polling_cb(win_chr_poll
, s
);
1873 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1875 WinCharState
*s
= chr
->opaque
;
1876 DWORD len
, ret
, size
, err
;
1879 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1880 s
->osend
.hEvent
= s
->hsend
;
1883 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1885 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1887 err
= GetLastError();
1888 if (err
== ERROR_IO_PENDING
) {
1889 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1907 static int win_chr_read_poll(WinCharState
*s
)
1909 s
->max_size
= s
->fd_can_read(s
->win_opaque
);
1913 static void win_chr_readfile(WinCharState
*s
)
1919 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1920 s
->orecv
.hEvent
= s
->hrecv
;
1921 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1923 err
= GetLastError();
1924 if (err
== ERROR_IO_PENDING
) {
1925 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1930 s
->fd_read(s
->win_opaque
, buf
, size
);
1934 static void win_chr_read(WinCharState
*s
)
1936 if (s
->len
> s
->max_size
)
1937 s
->len
= s
->max_size
;
1941 win_chr_readfile(s
);
1944 static int win_chr_poll(void *opaque
)
1946 WinCharState
*s
= opaque
;
1950 ClearCommError(s
->hcom
, &comerr
, &status
);
1951 if (status
.cbInQue
> 0) {
1952 s
->len
= status
.cbInQue
;
1953 win_chr_read_poll(s
);
1960 static void win_chr_add_read_handler(CharDriverState
*chr
,
1961 IOCanRWHandler
*fd_can_read
,
1962 IOReadHandler
*fd_read
, void *opaque
)
1964 WinCharState
*s
= chr
->opaque
;
1966 s
->fd_can_read
= fd_can_read
;
1967 s
->fd_read
= fd_read
;
1968 s
->win_opaque
= opaque
;
1971 CharDriverState
*qemu_chr_open_win(const char *filename
)
1973 CharDriverState
*chr
;
1976 chr
= qemu_mallocz(sizeof(CharDriverState
));
1979 s
= qemu_mallocz(sizeof(WinCharState
));
1985 chr
->chr_write
= win_chr_write
;
1986 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
1987 chr
->chr_close
= win_chr_close
;
1989 if (win_chr_init(s
, filename
) < 0) {
1997 static int win_chr_pipe_poll(void *opaque
)
1999 WinCharState
*s
= opaque
;
2002 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2005 win_chr_read_poll(s
);
2012 static int win_chr_pipe_init(WinCharState
*s
, const char *filename
)
2021 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2023 fprintf(stderr
, "Failed CreateEvent\n");
2026 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2028 fprintf(stderr
, "Failed CreateEvent\n");
2032 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2033 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2034 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2036 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2037 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2038 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2043 ZeroMemory(&ov
, sizeof(ov
));
2044 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2045 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2047 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2051 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2053 fprintf(stderr
, "Failed GetOverlappedResult\n");
2055 CloseHandle(ov
.hEvent
);
2062 CloseHandle(ov
.hEvent
);
2065 qemu_add_polling_cb(win_chr_pipe_poll
, s
);
2074 CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2076 CharDriverState
*chr
;
2079 chr
= qemu_mallocz(sizeof(CharDriverState
));
2082 s
= qemu_mallocz(sizeof(WinCharState
));
2088 chr
->chr_write
= win_chr_write
;
2089 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2090 chr
->chr_close
= win_chr_close
;
2092 if (win_chr_pipe_init(s
, filename
) < 0) {
2100 CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2102 CharDriverState
*chr
;
2105 chr
= qemu_mallocz(sizeof(CharDriverState
));
2108 s
= qemu_mallocz(sizeof(WinCharState
));
2115 chr
->chr_write
= win_chr_write
;
2116 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2120 CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2124 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2125 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2126 if (fd_out
== INVALID_HANDLE_VALUE
)
2129 return qemu_chr_open_win_file(fd_out
);
2133 CharDriverState
*qemu_chr_open(const char *filename
)
2137 if (!strcmp(filename
, "vc")) {
2138 return text_console_init(&display_state
);
2139 } else if (!strcmp(filename
, "null")) {
2140 return qemu_chr_open_null();
2143 if (strstart(filename
, "file:", &p
)) {
2144 return qemu_chr_open_file_out(p
);
2145 } else if (strstart(filename
, "pipe:", &p
)) {
2146 return qemu_chr_open_pipe(p
);
2147 } else if (!strcmp(filename
, "pty")) {
2148 return qemu_chr_open_pty();
2149 } else if (!strcmp(filename
, "stdio")) {
2150 return qemu_chr_open_stdio();
2153 #if defined(__linux__)
2154 if (strstart(filename
, "/dev/parport", NULL
)) {
2155 return qemu_chr_open_pp(filename
);
2157 if (strstart(filename
, "/dev/", NULL
)) {
2158 return qemu_chr_open_tty(filename
);
2162 if (strstart(filename
, "COM", NULL
)) {
2163 return qemu_chr_open_win(filename
);
2165 if (strstart(filename
, "pipe:", &p
)) {
2166 return qemu_chr_open_win_pipe(p
);
2168 if (strstart(filename
, "file:", &p
)) {
2169 return qemu_chr_open_win_file_out(p
);
2177 void qemu_chr_close(CharDriverState
*chr
)
2180 chr
->chr_close(chr
);
2183 /***********************************************************/
2184 /* network device redirectors */
2186 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2190 for(i
=0;i
<size
;i
+=16) {
2194 fprintf(f
, "%08x ", i
);
2197 fprintf(f
, " %02x", buf
[i
+j
]);
2202 for(j
=0;j
<len
;j
++) {
2204 if (c
< ' ' || c
> '~')
2206 fprintf(f
, "%c", c
);
2212 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
2215 for(i
= 0; i
< 6; i
++) {
2216 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2229 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
2234 p1
= strchr(p
, sep
);
2240 if (len
> buf_size
- 1)
2242 memcpy(buf
, p
, len
);
2249 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
2257 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2259 saddr
->sin_family
= AF_INET
;
2260 if (buf
[0] == '\0') {
2261 saddr
->sin_addr
.s_addr
= 0;
2263 if (isdigit(buf
[0])) {
2264 if (!inet_aton(buf
, &saddr
->sin_addr
))
2267 if ((he
= gethostbyname(buf
)) == NULL
)
2269 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
2272 port
= strtol(p
, (char **)&r
, 0);
2275 saddr
->sin_port
= htons(port
);
2279 /* find or alloc a new VLAN */
2280 VLANState
*qemu_find_vlan(int id
)
2282 VLANState
**pvlan
, *vlan
;
2283 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2287 vlan
= qemu_mallocz(sizeof(VLANState
));
2292 pvlan
= &first_vlan
;
2293 while (*pvlan
!= NULL
)
2294 pvlan
= &(*pvlan
)->next
;
2299 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
2300 IOReadHandler
*fd_read
,
2301 IOCanRWHandler
*fd_can_read
,
2304 VLANClientState
*vc
, **pvc
;
2305 vc
= qemu_mallocz(sizeof(VLANClientState
));
2308 vc
->fd_read
= fd_read
;
2309 vc
->fd_can_read
= fd_can_read
;
2310 vc
->opaque
= opaque
;
2314 pvc
= &vlan
->first_client
;
2315 while (*pvc
!= NULL
)
2316 pvc
= &(*pvc
)->next
;
2321 int qemu_can_send_packet(VLANClientState
*vc1
)
2323 VLANState
*vlan
= vc1
->vlan
;
2324 VLANClientState
*vc
;
2326 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2328 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
2335 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
2337 VLANState
*vlan
= vc1
->vlan
;
2338 VLANClientState
*vc
;
2341 printf("vlan %d send:\n", vlan
->id
);
2342 hex_dump(stdout
, buf
, size
);
2344 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2346 vc
->fd_read(vc
->opaque
, buf
, size
);
2351 #if defined(CONFIG_SLIRP)
2353 /* slirp network adapter */
2355 static int slirp_inited
;
2356 static VLANClientState
*slirp_vc
;
2358 int slirp_can_output(void)
2360 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
2363 void slirp_output(const uint8_t *pkt
, int pkt_len
)
2366 printf("slirp output:\n");
2367 hex_dump(stdout
, pkt
, pkt_len
);
2371 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
2374 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
2377 printf("slirp input:\n");
2378 hex_dump(stdout
, buf
, size
);
2380 slirp_input(buf
, size
);
2383 static int net_slirp_init(VLANState
*vlan
)
2385 if (!slirp_inited
) {
2389 slirp_vc
= qemu_new_vlan_client(vlan
,
2390 slirp_receive
, NULL
, NULL
);
2391 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
2395 static void net_slirp_redir(const char *redir_str
)
2400 struct in_addr guest_addr
;
2401 int host_port
, guest_port
;
2403 if (!slirp_inited
) {
2409 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2411 if (!strcmp(buf
, "tcp")) {
2413 } else if (!strcmp(buf
, "udp")) {
2419 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2421 host_port
= strtol(buf
, &r
, 0);
2425 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2427 if (buf
[0] == '\0') {
2428 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
2430 if (!inet_aton(buf
, &guest_addr
))
2433 guest_port
= strtol(p
, &r
, 0);
2437 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
2438 fprintf(stderr
, "qemu: could not set up redirection\n");
2443 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
2451 static void smb_exit(void)
2455 char filename
[1024];
2457 /* erase all the files in the directory */
2458 d
= opendir(smb_dir
);
2463 if (strcmp(de
->d_name
, ".") != 0 &&
2464 strcmp(de
->d_name
, "..") != 0) {
2465 snprintf(filename
, sizeof(filename
), "%s/%s",
2466 smb_dir
, de
->d_name
);
2474 /* automatic user mode samba server configuration */
2475 void net_slirp_smb(const char *exported_dir
)
2477 char smb_conf
[1024];
2478 char smb_cmdline
[1024];
2481 if (!slirp_inited
) {
2486 /* XXX: better tmp dir construction */
2487 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
2488 if (mkdir(smb_dir
, 0700) < 0) {
2489 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
2492 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
2494 f
= fopen(smb_conf
, "w");
2496 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
2503 "socket address=127.0.0.1\n"
2504 "pid directory=%s\n"
2505 "lock directory=%s\n"
2506 "log file=%s/log.smbd\n"
2507 "smb passwd file=%s/smbpasswd\n"
2508 "security = share\n"
2523 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "/usr/sbin/smbd -s %s",
2526 slirp_add_exec(0, smb_cmdline
, 4, 139);
2529 #endif /* !defined(_WIN32) */
2531 #endif /* CONFIG_SLIRP */
2533 #if !defined(_WIN32)
2535 typedef struct TAPState
{
2536 VLANClientState
*vc
;
2540 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
2542 TAPState
*s
= opaque
;
2545 ret
= write(s
->fd
, buf
, size
);
2546 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
2553 static void tap_send(void *opaque
)
2555 TAPState
*s
= opaque
;
2559 size
= read(s
->fd
, buf
, sizeof(buf
));
2561 qemu_send_packet(s
->vc
, buf
, size
);
2567 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
2571 s
= qemu_mallocz(sizeof(TAPState
));
2575 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
2576 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
2577 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
2582 static int tap_open(char *ifname
, int ifname_size
)
2588 fd
= open("/dev/tap", O_RDWR
);
2590 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
2595 dev
= devname(s
.st_rdev
, S_IFCHR
);
2596 pstrcpy(ifname
, ifname_size
, dev
);
2598 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2601 #elif defined(__sun__)
2602 static int tap_open(char *ifname
, int ifname_size
)
2604 fprintf(stderr
, "warning: tap_open not yet implemented\n");
2608 static int tap_open(char *ifname
, int ifname_size
)
2613 fd
= open("/dev/net/tun", O_RDWR
);
2615 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
2618 memset(&ifr
, 0, sizeof(ifr
));
2619 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
2620 if (ifname
[0] != '\0')
2621 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
2623 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
2624 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
2626 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
2630 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
2631 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2636 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
2637 const char *setup_script
)
2640 int pid
, status
, fd
;
2645 if (ifname1
!= NULL
)
2646 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
2649 fd
= tap_open(ifname
, sizeof(ifname
));
2655 if (setup_script
[0] != '\0') {
2656 /* try to launch network init script */
2661 *parg
++ = (char *)setup_script
;
2664 execv(setup_script
, args
);
2667 while (waitpid(pid
, &status
, 0) != pid
);
2668 if (!WIFEXITED(status
) ||
2669 WEXITSTATUS(status
) != 0) {
2670 fprintf(stderr
, "%s: could not launch network script\n",
2676 s
= net_tap_fd_init(vlan
, fd
);
2679 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2680 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
2684 #endif /* !_WIN32 */
2686 /* network connection */
2687 typedef struct NetSocketState
{
2688 VLANClientState
*vc
;
2690 int state
; /* 0 = getting length, 1 = getting data */
2694 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
2697 typedef struct NetSocketListenState
{
2700 } NetSocketListenState
;
2702 /* XXX: we consider we can send the whole packet without blocking */
2703 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
2705 NetSocketState
*s
= opaque
;
2709 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
2710 send_all(s
->fd
, buf
, size
);
2713 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
2715 NetSocketState
*s
= opaque
;
2716 sendto(s
->fd
, buf
, size
, 0,
2717 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
2720 static void net_socket_send(void *opaque
)
2722 NetSocketState
*s
= opaque
;
2727 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
2729 err
= socket_error();
2730 if (err
!= EWOULDBLOCK
)
2732 } else if (size
== 0) {
2733 /* end of connection */
2735 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2741 /* reassemble a packet from the network */
2747 memcpy(s
->buf
+ s
->index
, buf
, l
);
2751 if (s
->index
== 4) {
2753 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
2759 l
= s
->packet_len
- s
->index
;
2762 memcpy(s
->buf
+ s
->index
, buf
, l
);
2766 if (s
->index
>= s
->packet_len
) {
2767 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
2776 static void net_socket_send_dgram(void *opaque
)
2778 NetSocketState
*s
= opaque
;
2781 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2785 /* end of connection */
2786 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2789 qemu_send_packet(s
->vc
, s
->buf
, size
);
2792 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
2797 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
2798 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
2799 inet_ntoa(mcastaddr
->sin_addr
),
2800 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
2804 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2806 perror("socket(PF_INET, SOCK_DGRAM)");
2811 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
2812 (const char *)&val
, sizeof(val
));
2814 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
2818 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
2824 /* Add host to multicast group */
2825 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
2826 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
2828 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
2829 (const char *)&imr
, sizeof(struct ip_mreq
));
2831 perror("setsockopt(IP_ADD_MEMBERSHIP)");
2835 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
2837 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
2838 (const char *)&val
, sizeof(val
));
2840 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
2844 socket_set_nonblock(fd
);
2847 if (fd
>=0) close(fd
);
2851 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
2854 struct sockaddr_in saddr
;
2856 socklen_t saddr_len
;
2859 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
2860 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
2861 * by ONLY ONE process: we must "clone" this dgram socket --jjo
2865 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
2867 if (saddr
.sin_addr
.s_addr
==0) {
2868 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
2872 /* clone dgram socket */
2873 newfd
= net_socket_mcast_create(&saddr
);
2875 /* error already reported by net_socket_mcast_create() */
2879 /* clone newfd to fd, close newfd */
2884 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
2885 fd
, strerror(errno
));
2890 s
= qemu_mallocz(sizeof(NetSocketState
));
2895 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
2896 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
2898 /* mcast: save bound address as dst */
2899 if (is_connected
) s
->dgram_dst
=saddr
;
2901 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2902 "socket: fd=%d (%s mcast=%s:%d)",
2903 fd
, is_connected
? "cloned" : "",
2904 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2908 static void net_socket_connect(void *opaque
)
2910 NetSocketState
*s
= opaque
;
2911 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
2914 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
2918 s
= qemu_mallocz(sizeof(NetSocketState
));
2922 s
->vc
= qemu_new_vlan_client(vlan
,
2923 net_socket_receive
, NULL
, s
);
2924 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2925 "socket: fd=%d", fd
);
2927 net_socket_connect(s
);
2929 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
2934 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
2937 int so_type
=-1, optlen
=sizeof(so_type
);
2939 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
2940 fprintf(stderr
, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd
);
2945 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
2947 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
2949 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
2950 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
2951 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
2956 static void net_socket_accept(void *opaque
)
2958 NetSocketListenState
*s
= opaque
;
2960 struct sockaddr_in saddr
;
2965 len
= sizeof(saddr
);
2966 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
2967 if (fd
< 0 && errno
!= EINTR
) {
2969 } else if (fd
>= 0) {
2973 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
2977 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
2978 "socket: connection from %s:%d",
2979 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2983 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
2985 NetSocketListenState
*s
;
2987 struct sockaddr_in saddr
;
2989 if (parse_host_port(&saddr
, host_str
) < 0)
2992 s
= qemu_mallocz(sizeof(NetSocketListenState
));
2996 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3001 socket_set_nonblock(fd
);
3003 /* allow fast reuse */
3005 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3007 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3012 ret
= listen(fd
, 0);
3019 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
3023 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
3026 int fd
, connected
, ret
, err
;
3027 struct sockaddr_in saddr
;
3029 if (parse_host_port(&saddr
, host_str
) < 0)
3032 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3037 socket_set_nonblock(fd
);
3041 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3043 err
= socket_error();
3044 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3045 } else if (err
== EINPROGRESS
) {
3057 s
= net_socket_fd_init(vlan
, fd
, connected
);
3060 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3061 "socket: connect to %s:%d",
3062 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3066 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
3070 struct sockaddr_in saddr
;
3072 if (parse_host_port(&saddr
, host_str
) < 0)
3076 fd
= net_socket_mcast_create(&saddr
);
3080 s
= net_socket_fd_init(vlan
, fd
, 0);
3084 s
->dgram_dst
= saddr
;
3086 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3087 "socket: mcast=%s:%d",
3088 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3093 static int get_param_value(char *buf
, int buf_size
,
3094 const char *tag
, const char *str
)
3103 while (*p
!= '\0' && *p
!= '=') {
3104 if ((q
- option
) < sizeof(option
) - 1)
3112 if (!strcmp(tag
, option
)) {
3114 while (*p
!= '\0' && *p
!= ',') {
3115 if ((q
- buf
) < buf_size
- 1)
3122 while (*p
!= '\0' && *p
!= ',') {
3133 int net_client_init(const char *str
)
3144 while (*p
!= '\0' && *p
!= ',') {
3145 if ((q
- device
) < sizeof(device
) - 1)
3153 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
3154 vlan_id
= strtol(buf
, NULL
, 0);
3156 vlan
= qemu_find_vlan(vlan_id
);
3158 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
3161 if (!strcmp(device
, "nic")) {
3165 if (nb_nics
>= MAX_NICS
) {
3166 fprintf(stderr
, "Too Many NICs\n");
3169 nd
= &nd_table
[nb_nics
];
3170 macaddr
= nd
->macaddr
;
3176 macaddr
[5] = 0x56 + nb_nics
;
3178 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
3179 if (parse_macaddr(macaddr
, buf
) < 0) {
3180 fprintf(stderr
, "invalid syntax for ethernet address\n");
3184 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
3185 nd
->model
= strdup(buf
);
3191 if (!strcmp(device
, "none")) {
3192 /* does nothing. It is needed to signal that no network cards
3197 if (!strcmp(device
, "user")) {
3198 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
3199 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
3201 ret
= net_slirp_init(vlan
);
3205 if (!strcmp(device
, "tap")) {
3207 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
3208 fprintf(stderr
, "tap: no interface name\n");
3211 ret
= tap_win32_init(vlan
, ifname
);
3214 if (!strcmp(device
, "tap")) {
3216 char setup_script
[1024];
3218 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3219 fd
= strtol(buf
, NULL
, 0);
3221 if (net_tap_fd_init(vlan
, fd
))
3224 get_param_value(ifname
, sizeof(ifname
), "ifname", p
);
3225 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
3226 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
3228 ret
= net_tap_init(vlan
, ifname
, setup_script
);
3232 if (!strcmp(device
, "socket")) {
3233 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3235 fd
= strtol(buf
, NULL
, 0);
3237 if (net_socket_fd_init(vlan
, fd
, 1))
3239 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
3240 ret
= net_socket_listen_init(vlan
, buf
);
3241 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
3242 ret
= net_socket_connect_init(vlan
, buf
);
3243 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
3244 ret
= net_socket_mcast_init(vlan
, buf
);
3246 fprintf(stderr
, "Unknown socket options: %s\n", p
);
3251 fprintf(stderr
, "Unknown network device: %s\n", device
);
3255 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
3261 void do_info_network(void)
3264 VLANClientState
*vc
;
3266 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3267 term_printf("VLAN %d devices:\n", vlan
->id
);
3268 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3269 term_printf(" %s\n", vc
->info_str
);
3273 /***********************************************************/
3276 static USBPort
*used_usb_ports
;
3277 static USBPort
*free_usb_ports
;
3279 /* ??? Maybe change this to register a hub to keep track of the topology. */
3280 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
3281 usb_attachfn attach
)
3283 port
->opaque
= opaque
;
3284 port
->index
= index
;
3285 port
->attach
= attach
;
3286 port
->next
= free_usb_ports
;
3287 free_usb_ports
= port
;
3290 static int usb_device_add(const char *devname
)
3296 if (!free_usb_ports
)
3299 if (strstart(devname
, "host:", &p
)) {
3300 dev
= usb_host_device_open(p
);
3301 } else if (!strcmp(devname
, "mouse")) {
3302 dev
= usb_mouse_init();
3303 } else if (!strcmp(devname
, "tablet")) {
3304 dev
= usb_tablet_init();
3305 } else if (strstart(devname
, "disk:", &p
)) {
3306 dev
= usb_msd_init(p
);
3313 /* Find a USB port to add the device to. */
3314 port
= free_usb_ports
;
3318 /* Create a new hub and chain it on. */
3319 free_usb_ports
= NULL
;
3320 port
->next
= used_usb_ports
;
3321 used_usb_ports
= port
;
3323 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
3324 usb_attach(port
, hub
);
3325 port
= free_usb_ports
;
3328 free_usb_ports
= port
->next
;
3329 port
->next
= used_usb_ports
;
3330 used_usb_ports
= port
;
3331 usb_attach(port
, dev
);
3335 static int usb_device_del(const char *devname
)
3342 if (!used_usb_ports
)
3345 p
= strchr(devname
, '.');
3348 bus_num
= strtoul(devname
, NULL
, 0);
3349 addr
= strtoul(p
+ 1, NULL
, 0);
3353 lastp
= &used_usb_ports
;
3354 port
= used_usb_ports
;
3355 while (port
&& port
->dev
->addr
!= addr
) {
3356 lastp
= &port
->next
;
3363 *lastp
= port
->next
;
3364 usb_attach(port
, NULL
);
3365 port
->next
= free_usb_ports
;
3366 free_usb_ports
= port
;
3370 void do_usb_add(const char *devname
)
3373 ret
= usb_device_add(devname
);
3375 term_printf("Could not add USB device '%s'\n", devname
);
3378 void do_usb_del(const char *devname
)
3381 ret
= usb_device_del(devname
);
3383 term_printf("Could not remove USB device '%s'\n", devname
);
3390 const char *speed_str
;
3393 term_printf("USB support not enabled\n");
3397 for (port
= used_usb_ports
; port
; port
= port
->next
) {
3401 switch(dev
->speed
) {
3405 case USB_SPEED_FULL
:
3408 case USB_SPEED_HIGH
:
3415 term_printf(" Device %d.%d, speed %s Mb/s\n",
3416 0, dev
->addr
, speed_str
);
3420 /***********************************************************/
3423 static char *pid_filename
;
3425 /* Remove PID file. Called on normal exit */
3427 static void remove_pidfile(void)
3429 unlink (pid_filename
);
3432 static void create_pidfile(const char *filename
)
3434 struct stat pidstat
;
3437 /* Try to write our PID to the named file */
3438 if (stat(filename
, &pidstat
) < 0) {
3439 if (errno
== ENOENT
) {
3440 if ((f
= fopen (filename
, "w")) == NULL
) {
3441 perror("Opening pidfile");
3444 fprintf(f
, "%d\n", getpid());
3446 pid_filename
= qemu_strdup(filename
);
3447 if (!pid_filename
) {
3448 fprintf(stderr
, "Could not save PID filename");
3451 atexit(remove_pidfile
);
3454 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
3460 /***********************************************************/
3463 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
3467 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
3471 static void dumb_refresh(DisplayState
*ds
)
3476 void dumb_display_init(DisplayState
*ds
)
3481 ds
->dpy_update
= dumb_update
;
3482 ds
->dpy_resize
= dumb_resize
;
3483 ds
->dpy_refresh
= dumb_refresh
;
3486 #if !defined(CONFIG_SOFTMMU)
3487 /***********************************************************/
3488 /* cpu signal handler */
3489 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
3492 if (cpu_signal_handler(host_signum
, info
, puc
))
3494 if (stdio_nb_clients
> 0)
3500 /***********************************************************/
3503 #define MAX_IO_HANDLERS 64
3505 typedef struct IOHandlerRecord
{
3507 IOCanRWHandler
*fd_read_poll
;
3509 IOHandler
*fd_write
;
3511 /* temporary data */
3513 struct IOHandlerRecord
*next
;
3516 static IOHandlerRecord
*first_io_handler
;
3518 /* XXX: fd_read_poll should be suppressed, but an API change is
3519 necessary in the character devices to suppress fd_can_read(). */
3520 int qemu_set_fd_handler2(int fd
,
3521 IOCanRWHandler
*fd_read_poll
,
3523 IOHandler
*fd_write
,
3526 IOHandlerRecord
**pioh
, *ioh
;
3528 if (!fd_read
&& !fd_write
) {
3529 pioh
= &first_io_handler
;
3534 if (ioh
->fd
== fd
) {
3542 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3546 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
3549 ioh
->next
= first_io_handler
;
3550 first_io_handler
= ioh
;
3553 ioh
->fd_read_poll
= fd_read_poll
;
3554 ioh
->fd_read
= fd_read
;
3555 ioh
->fd_write
= fd_write
;
3556 ioh
->opaque
= opaque
;
3561 int qemu_set_fd_handler(int fd
,
3563 IOHandler
*fd_write
,
3566 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
3569 /***********************************************************/
3570 /* Polling handling */
3572 typedef struct PollingEntry
{
3575 struct PollingEntry
*next
;
3578 static PollingEntry
*first_polling_entry
;
3580 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
3582 PollingEntry
**ppe
, *pe
;
3583 pe
= qemu_mallocz(sizeof(PollingEntry
));
3587 pe
->opaque
= opaque
;
3588 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
3593 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3595 PollingEntry
**ppe
, *pe
;
3596 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3598 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3606 /***********************************************************/
3607 /* savevm/loadvm support */
3609 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
3611 fwrite(buf
, 1, size
, f
);
3614 void qemu_put_byte(QEMUFile
*f
, int v
)
3619 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
3621 qemu_put_byte(f
, v
>> 8);
3622 qemu_put_byte(f
, v
);
3625 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
3627 qemu_put_byte(f
, v
>> 24);
3628 qemu_put_byte(f
, v
>> 16);
3629 qemu_put_byte(f
, v
>> 8);
3630 qemu_put_byte(f
, v
);
3633 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
3635 qemu_put_be32(f
, v
>> 32);
3636 qemu_put_be32(f
, v
);
3639 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
3641 return fread(buf
, 1, size
, f
);
3644 int qemu_get_byte(QEMUFile
*f
)
3654 unsigned int qemu_get_be16(QEMUFile
*f
)
3657 v
= qemu_get_byte(f
) << 8;
3658 v
|= qemu_get_byte(f
);
3662 unsigned int qemu_get_be32(QEMUFile
*f
)
3665 v
= qemu_get_byte(f
) << 24;
3666 v
|= qemu_get_byte(f
) << 16;
3667 v
|= qemu_get_byte(f
) << 8;
3668 v
|= qemu_get_byte(f
);
3672 uint64_t qemu_get_be64(QEMUFile
*f
)
3675 v
= (uint64_t)qemu_get_be32(f
) << 32;
3676 v
|= qemu_get_be32(f
);
3680 int64_t qemu_ftell(QEMUFile
*f
)
3685 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
3687 if (fseek(f
, pos
, whence
) < 0)
3692 typedef struct SaveStateEntry
{
3696 SaveStateHandler
*save_state
;
3697 LoadStateHandler
*load_state
;
3699 struct SaveStateEntry
*next
;
3702 static SaveStateEntry
*first_se
;
3704 int register_savevm(const char *idstr
,
3707 SaveStateHandler
*save_state
,
3708 LoadStateHandler
*load_state
,
3711 SaveStateEntry
*se
, **pse
;
3713 se
= qemu_malloc(sizeof(SaveStateEntry
));
3716 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
3717 se
->instance_id
= instance_id
;
3718 se
->version_id
= version_id
;
3719 se
->save_state
= save_state
;
3720 se
->load_state
= load_state
;
3721 se
->opaque
= opaque
;
3724 /* add at the end of list */
3726 while (*pse
!= NULL
)
3727 pse
= &(*pse
)->next
;
3732 #define QEMU_VM_FILE_MAGIC 0x5145564d
3733 #define QEMU_VM_FILE_VERSION 0x00000001
3735 int qemu_savevm(const char *filename
)
3739 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
3741 saved_vm_running
= vm_running
;
3744 f
= fopen(filename
, "wb");
3750 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
3751 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
3753 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3755 len
= strlen(se
->idstr
);
3756 qemu_put_byte(f
, len
);
3757 qemu_put_buffer(f
, se
->idstr
, len
);
3759 qemu_put_be32(f
, se
->instance_id
);
3760 qemu_put_be32(f
, se
->version_id
);
3762 /* record size: filled later */
3764 qemu_put_be32(f
, 0);
3766 se
->save_state(f
, se
->opaque
);
3768 /* fill record size */
3770 len
= ftell(f
) - len_pos
- 4;
3771 fseek(f
, len_pos
, SEEK_SET
);
3772 qemu_put_be32(f
, len
);
3773 fseek(f
, cur_pos
, SEEK_SET
);
3779 if (saved_vm_running
)
3784 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
3788 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3789 if (!strcmp(se
->idstr
, idstr
) &&
3790 instance_id
== se
->instance_id
)
3796 int qemu_loadvm(const char *filename
)
3800 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
3801 int saved_vm_running
;
3805 saved_vm_running
= vm_running
;
3808 f
= fopen(filename
, "rb");
3814 v
= qemu_get_be32(f
);
3815 if (v
!= QEMU_VM_FILE_MAGIC
)
3817 v
= qemu_get_be32(f
);
3818 if (v
!= QEMU_VM_FILE_VERSION
) {
3825 len
= qemu_get_byte(f
);
3828 qemu_get_buffer(f
, idstr
, len
);
3830 instance_id
= qemu_get_be32(f
);
3831 version_id
= qemu_get_be32(f
);
3832 record_len
= qemu_get_be32(f
);
3834 printf("idstr=%s instance=0x%x version=%d len=%d\n",
3835 idstr
, instance_id
, version_id
, record_len
);
3838 se
= find_se(idstr
, instance_id
);
3840 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
3841 instance_id
, idstr
);
3843 ret
= se
->load_state(f
, se
->opaque
, version_id
);
3845 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
3846 instance_id
, idstr
);
3849 /* always seek to exact end of record */
3850 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
3855 if (saved_vm_running
)
3860 /***********************************************************/
3861 /* cpu save/restore */
3863 #if defined(TARGET_I386)
3865 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
3867 qemu_put_be32(f
, dt
->selector
);
3868 qemu_put_betl(f
, dt
->base
);
3869 qemu_put_be32(f
, dt
->limit
);
3870 qemu_put_be32(f
, dt
->flags
);
3873 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
3875 dt
->selector
= qemu_get_be32(f
);
3876 dt
->base
= qemu_get_betl(f
);
3877 dt
->limit
= qemu_get_be32(f
);
3878 dt
->flags
= qemu_get_be32(f
);
3881 void cpu_save(QEMUFile
*f
, void *opaque
)
3883 CPUState
*env
= opaque
;
3884 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
3888 for(i
= 0; i
< CPU_NB_REGS
; i
++)
3889 qemu_put_betls(f
, &env
->regs
[i
]);
3890 qemu_put_betls(f
, &env
->eip
);
3891 qemu_put_betls(f
, &env
->eflags
);
3892 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
3893 qemu_put_be32s(f
, &hflags
);
3897 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
3899 for(i
= 0; i
< 8; i
++) {
3900 fptag
|= ((!env
->fptags
[i
]) << i
);
3903 qemu_put_be16s(f
, &fpuc
);
3904 qemu_put_be16s(f
, &fpus
);
3905 qemu_put_be16s(f
, &fptag
);
3907 #ifdef USE_X86LDOUBLE
3912 qemu_put_be16s(f
, &fpregs_format
);
3914 for(i
= 0; i
< 8; i
++) {
3915 #ifdef USE_X86LDOUBLE
3919 /* we save the real CPU data (in case of MMX usage only 'mant'
3920 contains the MMX register */
3921 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
3922 qemu_put_be64(f
, mant
);
3923 qemu_put_be16(f
, exp
);
3926 /* if we use doubles for float emulation, we save the doubles to
3927 avoid losing information in case of MMX usage. It can give
3928 problems if the image is restored on a CPU where long
3929 doubles are used instead. */
3930 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
3934 for(i
= 0; i
< 6; i
++)
3935 cpu_put_seg(f
, &env
->segs
[i
]);
3936 cpu_put_seg(f
, &env
->ldt
);
3937 cpu_put_seg(f
, &env
->tr
);
3938 cpu_put_seg(f
, &env
->gdt
);
3939 cpu_put_seg(f
, &env
->idt
);
3941 qemu_put_be32s(f
, &env
->sysenter_cs
);
3942 qemu_put_be32s(f
, &env
->sysenter_esp
);
3943 qemu_put_be32s(f
, &env
->sysenter_eip
);
3945 qemu_put_betls(f
, &env
->cr
[0]);
3946 qemu_put_betls(f
, &env
->cr
[2]);
3947 qemu_put_betls(f
, &env
->cr
[3]);
3948 qemu_put_betls(f
, &env
->cr
[4]);
3950 for(i
= 0; i
< 8; i
++)
3951 qemu_put_betls(f
, &env
->dr
[i
]);
3954 qemu_put_be32s(f
, &env
->a20_mask
);
3957 qemu_put_be32s(f
, &env
->mxcsr
);
3958 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
3959 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
3960 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
3963 #ifdef TARGET_X86_64
3964 qemu_put_be64s(f
, &env
->efer
);
3965 qemu_put_be64s(f
, &env
->star
);
3966 qemu_put_be64s(f
, &env
->lstar
);
3967 qemu_put_be64s(f
, &env
->cstar
);
3968 qemu_put_be64s(f
, &env
->fmask
);
3969 qemu_put_be64s(f
, &env
->kernelgsbase
);
3973 #ifdef USE_X86LDOUBLE
3974 /* XXX: add that in a FPU generic layer */
3975 union x86_longdouble
{
3980 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
3981 #define EXPBIAS1 1023
3982 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
3983 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
3985 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
3989 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
3990 /* exponent + sign */
3991 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
3992 e
|= SIGND1(temp
) >> 16;
3997 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
3999 CPUState
*env
= opaque
;
4002 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
4004 if (version_id
!= 3)
4006 for(i
= 0; i
< CPU_NB_REGS
; i
++)
4007 qemu_get_betls(f
, &env
->regs
[i
]);
4008 qemu_get_betls(f
, &env
->eip
);
4009 qemu_get_betls(f
, &env
->eflags
);
4010 qemu_get_be32s(f
, &hflags
);
4012 qemu_get_be16s(f
, &fpuc
);
4013 qemu_get_be16s(f
, &fpus
);
4014 qemu_get_be16s(f
, &fptag
);
4015 qemu_get_be16s(f
, &fpregs_format
);
4017 /* NOTE: we cannot always restore the FPU state if the image come
4018 from a host with a different 'USE_X86LDOUBLE' define. We guess
4019 if we are in an MMX state to restore correctly in that case. */
4020 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
4021 for(i
= 0; i
< 8; i
++) {
4025 switch(fpregs_format
) {
4027 mant
= qemu_get_be64(f
);
4028 exp
= qemu_get_be16(f
);
4029 #ifdef USE_X86LDOUBLE
4030 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
4032 /* difficult case */
4034 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
4036 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
4040 mant
= qemu_get_be64(f
);
4041 #ifdef USE_X86LDOUBLE
4043 union x86_longdouble
*p
;
4044 /* difficult case */
4045 p
= (void *)&env
->fpregs
[i
];
4050 fp64_to_fp80(p
, mant
);
4054 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
4063 /* XXX: restore FPU round state */
4064 env
->fpstt
= (fpus
>> 11) & 7;
4065 env
->fpus
= fpus
& ~0x3800;
4067 for(i
= 0; i
< 8; i
++) {
4068 env
->fptags
[i
] = (fptag
>> i
) & 1;
4071 for(i
= 0; i
< 6; i
++)
4072 cpu_get_seg(f
, &env
->segs
[i
]);
4073 cpu_get_seg(f
, &env
->ldt
);
4074 cpu_get_seg(f
, &env
->tr
);
4075 cpu_get_seg(f
, &env
->gdt
);
4076 cpu_get_seg(f
, &env
->idt
);
4078 qemu_get_be32s(f
, &env
->sysenter_cs
);
4079 qemu_get_be32s(f
, &env
->sysenter_esp
);
4080 qemu_get_be32s(f
, &env
->sysenter_eip
);
4082 qemu_get_betls(f
, &env
->cr
[0]);
4083 qemu_get_betls(f
, &env
->cr
[2]);
4084 qemu_get_betls(f
, &env
->cr
[3]);
4085 qemu_get_betls(f
, &env
->cr
[4]);
4087 for(i
= 0; i
< 8; i
++)
4088 qemu_get_betls(f
, &env
->dr
[i
]);
4091 qemu_get_be32s(f
, &env
->a20_mask
);
4093 qemu_get_be32s(f
, &env
->mxcsr
);
4094 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
4095 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
4096 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
4099 #ifdef TARGET_X86_64
4100 qemu_get_be64s(f
, &env
->efer
);
4101 qemu_get_be64s(f
, &env
->star
);
4102 qemu_get_be64s(f
, &env
->lstar
);
4103 qemu_get_be64s(f
, &env
->cstar
);
4104 qemu_get_be64s(f
, &env
->fmask
);
4105 qemu_get_be64s(f
, &env
->kernelgsbase
);
4108 /* XXX: compute hflags from scratch, except for CPL and IIF */
4109 env
->hflags
= hflags
;
4114 #elif defined(TARGET_PPC)
4115 void cpu_save(QEMUFile
*f
, void *opaque
)
4119 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4124 #elif defined(TARGET_MIPS)
4125 void cpu_save(QEMUFile
*f
, void *opaque
)
4129 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4134 #elif defined(TARGET_SPARC)
4135 void cpu_save(QEMUFile
*f
, void *opaque
)
4137 CPUState
*env
= opaque
;
4141 for(i
= 0; i
< 8; i
++)
4142 qemu_put_betls(f
, &env
->gregs
[i
]);
4143 for(i
= 0; i
< NWINDOWS
* 16; i
++)
4144 qemu_put_betls(f
, &env
->regbase
[i
]);
4147 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
4153 qemu_put_betl(f
, u
.i
);
4156 qemu_put_betls(f
, &env
->pc
);
4157 qemu_put_betls(f
, &env
->npc
);
4158 qemu_put_betls(f
, &env
->y
);
4160 qemu_put_be32(f
, tmp
);
4161 qemu_put_betls(f
, &env
->fsr
);
4162 qemu_put_betls(f
, &env
->tbr
);
4163 #ifndef TARGET_SPARC64
4164 qemu_put_be32s(f
, &env
->wim
);
4166 for(i
= 0; i
< 16; i
++)
4167 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
4171 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4173 CPUState
*env
= opaque
;
4177 for(i
= 0; i
< 8; i
++)
4178 qemu_get_betls(f
, &env
->gregs
[i
]);
4179 for(i
= 0; i
< NWINDOWS
* 16; i
++)
4180 qemu_get_betls(f
, &env
->regbase
[i
]);
4183 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
4188 u
.i
= qemu_get_betl(f
);
4192 qemu_get_betls(f
, &env
->pc
);
4193 qemu_get_betls(f
, &env
->npc
);
4194 qemu_get_betls(f
, &env
->y
);
4195 tmp
= qemu_get_be32(f
);
4196 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
4197 correctly updated */
4199 qemu_get_betls(f
, &env
->fsr
);
4200 qemu_get_betls(f
, &env
->tbr
);
4201 #ifndef TARGET_SPARC64
4202 qemu_get_be32s(f
, &env
->wim
);
4204 for(i
= 0; i
< 16; i
++)
4205 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
4211 #elif defined(TARGET_ARM)
4213 /* ??? Need to implement these. */
4214 void cpu_save(QEMUFile
*f
, void *opaque
)
4218 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4225 #warning No CPU save/restore functions
4229 /***********************************************************/
4230 /* ram save/restore */
4232 /* we just avoid storing empty pages */
4233 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
4238 for(i
= 1; i
< len
; i
++) {
4242 qemu_put_byte(f
, 1);
4243 qemu_put_byte(f
, v
);
4246 qemu_put_byte(f
, 0);
4247 qemu_put_buffer(f
, buf
, len
);
4250 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
4254 v
= qemu_get_byte(f
);
4257 if (qemu_get_buffer(f
, buf
, len
) != len
)
4261 v
= qemu_get_byte(f
);
4262 memset(buf
, v
, len
);
4270 static void ram_save(QEMUFile
*f
, void *opaque
)
4273 qemu_put_be32(f
, phys_ram_size
);
4274 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
4275 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
4279 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
4283 if (version_id
!= 1)
4285 if (qemu_get_be32(f
) != phys_ram_size
)
4287 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
4288 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
4295 /***********************************************************/
4296 /* machine registration */
4298 QEMUMachine
*first_machine
= NULL
;
4300 int qemu_register_machine(QEMUMachine
*m
)
4303 pm
= &first_machine
;
4311 QEMUMachine
*find_machine(const char *name
)
4315 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4316 if (!strcmp(m
->name
, name
))
4322 /***********************************************************/
4323 /* main execution loop */
4325 void gui_update(void *opaque
)
4327 display_state
.dpy_refresh(&display_state
);
4328 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
4331 struct vm_change_state_entry
{
4332 VMChangeStateHandler
*cb
;
4334 LIST_ENTRY (vm_change_state_entry
) entries
;
4337 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
4339 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
4342 VMChangeStateEntry
*e
;
4344 e
= qemu_mallocz(sizeof (*e
));
4350 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
4354 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
4356 LIST_REMOVE (e
, entries
);
4360 static void vm_state_notify(int running
)
4362 VMChangeStateEntry
*e
;
4364 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
4365 e
->cb(e
->opaque
, running
);
4369 /* XXX: support several handlers */
4370 static VMStopHandler
*vm_stop_cb
;
4371 static void *vm_stop_opaque
;
4373 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4376 vm_stop_opaque
= opaque
;
4380 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4394 void vm_stop(int reason
)
4397 cpu_disable_ticks();
4401 vm_stop_cb(vm_stop_opaque
, reason
);
4408 /* reset/shutdown handler */
4410 typedef struct QEMUResetEntry
{
4411 QEMUResetHandler
*func
;
4413 struct QEMUResetEntry
*next
;
4416 static QEMUResetEntry
*first_reset_entry
;
4417 static int reset_requested
;
4418 static int shutdown_requested
;
4419 static int powerdown_requested
;
4421 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
4423 QEMUResetEntry
**pre
, *re
;
4425 pre
= &first_reset_entry
;
4426 while (*pre
!= NULL
)
4427 pre
= &(*pre
)->next
;
4428 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
4430 re
->opaque
= opaque
;
4435 void qemu_system_reset(void)
4439 /* reset all devices */
4440 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
4441 re
->func(re
->opaque
);
4445 void qemu_system_reset_request(void)
4447 reset_requested
= 1;
4449 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4452 void qemu_system_shutdown_request(void)
4454 shutdown_requested
= 1;
4456 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4459 void qemu_system_powerdown_request(void)
4461 powerdown_requested
= 1;
4463 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4466 void main_loop_wait(int timeout
)
4468 IOHandlerRecord
*ioh
, *ioh_next
;
4469 fd_set rfds
, wfds
, xfds
;
4475 /* XXX: need to suppress polling by better using win32 events */
4477 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4478 ret
|= pe
->func(pe
->opaque
);
4481 if (ret
== 0 && timeout
> 0) {
4485 hEvents
[0] = host_alarm
;
4486 ret
= WaitForMultipleObjects(1, hEvents
, FALSE
, timeout
);
4488 case WAIT_OBJECT_0
+ 0:
4493 err
= GetLastError();
4494 fprintf(stderr
, "Wait error %d %d\n", ret
, err
);
4499 /* poll any events */
4500 /* XXX: separate device handlers from system ones */
4505 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4507 (!ioh
->fd_read_poll
||
4508 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4509 FD_SET(ioh
->fd
, &rfds
);
4513 if (ioh
->fd_write
) {
4514 FD_SET(ioh
->fd
, &wfds
);
4524 tv
.tv_usec
= timeout
* 1000;
4526 #if defined(CONFIG_SLIRP)
4528 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4531 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4533 /* XXX: better handling of removal */
4534 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
4535 ioh_next
= ioh
->next
;
4536 if (FD_ISSET(ioh
->fd
, &rfds
)) {
4537 ioh
->fd_read(ioh
->opaque
);
4539 if (FD_ISSET(ioh
->fd
, &wfds
)) {
4540 ioh
->fd_write(ioh
->opaque
);
4544 #if defined(CONFIG_SLIRP)
4551 slirp_select_poll(&rfds
, &wfds
, &xfds
);
4559 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4560 qemu_get_clock(vm_clock
));
4561 /* run dma transfers, if any */
4565 /* real time timers */
4566 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4567 qemu_get_clock(rt_clock
));
4570 static CPUState
*cur_cpu
;
4575 #ifdef CONFIG_PROFILER
4580 cur_cpu
= first_cpu
;
4587 env
= env
->next_cpu
;
4590 #ifdef CONFIG_PROFILER
4591 ti
= profile_getclock();
4593 ret
= cpu_exec(env
);
4594 #ifdef CONFIG_PROFILER
4595 qemu_time
+= profile_getclock() - ti
;
4597 if (ret
!= EXCP_HALTED
)
4599 /* all CPUs are halted ? */
4600 if (env
== cur_cpu
) {
4607 if (shutdown_requested
) {
4608 ret
= EXCP_INTERRUPT
;
4611 if (reset_requested
) {
4612 reset_requested
= 0;
4613 qemu_system_reset();
4614 ret
= EXCP_INTERRUPT
;
4616 if (powerdown_requested
) {
4617 powerdown_requested
= 0;
4618 qemu_system_powerdown();
4619 ret
= EXCP_INTERRUPT
;
4621 if (ret
== EXCP_DEBUG
) {
4622 vm_stop(EXCP_DEBUG
);
4624 /* if hlt instruction, we wait until the next IRQ */
4625 /* XXX: use timeout computed from timers */
4626 if (ret
== EXCP_HLT
)
4633 #ifdef CONFIG_PROFILER
4634 ti
= profile_getclock();
4636 main_loop_wait(timeout
);
4637 #ifdef CONFIG_PROFILER
4638 dev_time
+= profile_getclock() - ti
;
4641 cpu_disable_ticks();
4647 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2005 Fabrice Bellard\n"
4648 "usage: %s [options] [disk_image]\n"
4650 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4652 "Standard options:\n"
4653 "-M machine select emulated machine (-M ? for list)\n"
4654 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
4655 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
4656 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
4657 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
4658 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
4659 "-snapshot write to temporary files instead of disk image files\n"
4661 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4663 "-m megs set virtual RAM size to megs MB [default=%d]\n"
4664 "-smp n set the number of CPUs to 'n' [default=1]\n"
4665 "-nographic disable graphical output and redirect serial I/Os to console\n"
4667 "-k language use keyboard layout (for example \"fr\" for French)\n"
4670 "-audio-help print list of audio drivers and their options\n"
4671 "-soundhw c1,... enable audio support\n"
4672 " and only specified sound cards (comma separated list)\n"
4673 " use -soundhw ? to get the list of supported cards\n"
4674 " use -soundhw all to enable all of them\n"
4676 "-localtime set the real time clock to local time [default=utc]\n"
4677 "-full-screen start in full screen\n"
4679 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4681 "-usb enable the USB driver (will be the default soon)\n"
4682 "-usbdevice name add the host or guest USB device 'name'\n"
4683 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4684 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4687 "Network options:\n"
4688 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
4689 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4691 "-net user[,vlan=n][,hostname=host]\n"
4692 " connect the user mode network stack to VLAN 'n' and send\n"
4693 " hostname 'host' to DHCP clients\n"
4696 "-net tap[,vlan=n],ifname=name\n"
4697 " connect the host TAP network interface to VLAN 'n'\n"
4699 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
4700 " connect the host TAP network interface to VLAN 'n' and use\n"
4701 " the network script 'file' (default=%s);\n"
4702 " use 'fd=h' to connect to an already opened TAP interface\n"
4704 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4705 " connect the vlan 'n' to another VLAN using a socket connection\n"
4706 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
4707 " connect the vlan 'n' to multicast maddr and port\n"
4708 "-net none use it alone to have zero network devices; if no -net option\n"
4709 " is provided, the default is '-net nic -net user'\n"
4712 "-tftp prefix allow tftp access to files starting with prefix [-net user]\n"
4714 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4716 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4717 " redirect TCP or UDP connections from host to guest [-net user]\n"
4720 "Linux boot specific:\n"
4721 "-kernel bzImage use 'bzImage' as kernel image\n"
4722 "-append cmdline use 'cmdline' as kernel command line\n"
4723 "-initrd file use 'file' as initial ram disk\n"
4725 "Debug/Expert options:\n"
4726 "-monitor dev redirect the monitor to char device 'dev'\n"
4727 "-serial dev redirect the serial port to char device 'dev'\n"
4728 "-parallel dev redirect the parallel port to char device 'dev'\n"
4729 "-pidfile file Write PID to 'file'\n"
4730 "-S freeze CPU at startup (use 'c' to start execution)\n"
4731 "-s wait gdb connection to port %d\n"
4732 "-p port change gdb connection port\n"
4733 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4734 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
4735 " translation (t=none or lba) (usually qemu can guess them)\n"
4736 "-L path set the directory for the BIOS and VGA BIOS\n"
4738 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4739 "-no-kqemu disable KQEMU kernel module usage\n"
4741 #ifdef USE_CODE_COPY
4742 "-no-code-copy disable code copy acceleration\n"
4745 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
4746 " (default is CL-GD5446 PCI VGA)\n"
4747 "-no-acpi disable ACPI\n"
4749 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
4750 "-vnc display start a VNC server on display\n"
4752 "During emulation, the following keys are useful:\n"
4753 "ctrl-alt-f toggle full screen\n"
4754 "ctrl-alt-n switch to virtual console 'n'\n"
4755 "ctrl-alt toggle mouse and keyboard grab\n"
4757 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4759 #ifdef CONFIG_SOFTMMU
4766 DEFAULT_NETWORK_SCRIPT
,
4768 DEFAULT_GDBSTUB_PORT
,
4770 #ifndef CONFIG_SOFTMMU
4772 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
4773 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
4779 #define HAS_ARG 0x0001
4793 QEMU_OPTION_snapshot
,
4795 QEMU_OPTION_no_fd_bootchk
,
4798 QEMU_OPTION_nographic
,
4800 QEMU_OPTION_audio_help
,
4801 QEMU_OPTION_soundhw
,
4819 QEMU_OPTION_no_code_copy
,
4821 QEMU_OPTION_localtime
,
4822 QEMU_OPTION_cirrusvga
,
4824 QEMU_OPTION_std_vga
,
4825 QEMU_OPTION_monitor
,
4827 QEMU_OPTION_parallel
,
4829 QEMU_OPTION_full_screen
,
4830 QEMU_OPTION_pidfile
,
4831 QEMU_OPTION_no_kqemu
,
4832 QEMU_OPTION_kernel_kqemu
,
4833 QEMU_OPTION_win2k_hack
,
4835 QEMU_OPTION_usbdevice
,
4838 QEMU_OPTION_no_acpi
,
4841 typedef struct QEMUOption
{
4847 const QEMUOption qemu_options
[] = {
4848 { "h", 0, QEMU_OPTION_h
},
4850 { "M", HAS_ARG
, QEMU_OPTION_M
},
4851 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4852 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4853 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4854 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4855 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4856 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4857 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4858 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4859 { "snapshot", 0, QEMU_OPTION_snapshot
},
4861 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4863 { "m", HAS_ARG
, QEMU_OPTION_m
},
4864 { "nographic", 0, QEMU_OPTION_nographic
},
4865 { "k", HAS_ARG
, QEMU_OPTION_k
},
4867 { "audio-help", 0, QEMU_OPTION_audio_help
},
4868 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4871 { "net", HAS_ARG
, QEMU_OPTION_net
},
4873 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4875 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4877 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4880 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4881 { "append", HAS_ARG
, QEMU_OPTION_append
},
4882 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4884 { "S", 0, QEMU_OPTION_S
},
4885 { "s", 0, QEMU_OPTION_s
},
4886 { "p", HAS_ARG
, QEMU_OPTION_p
},
4887 { "d", HAS_ARG
, QEMU_OPTION_d
},
4888 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4889 { "L", HAS_ARG
, QEMU_OPTION_L
},
4890 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
4892 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4893 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4895 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4896 { "g", 1, QEMU_OPTION_g
},
4898 { "localtime", 0, QEMU_OPTION_localtime
},
4899 { "std-vga", 0, QEMU_OPTION_std_vga
},
4900 { "monitor", 1, QEMU_OPTION_monitor
},
4901 { "serial", 1, QEMU_OPTION_serial
},
4902 { "parallel", 1, QEMU_OPTION_parallel
},
4903 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4904 { "full-screen", 0, QEMU_OPTION_full_screen
},
4905 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4906 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4907 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4908 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4909 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4911 /* temporary options */
4912 { "usb", 0, QEMU_OPTION_usb
},
4913 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
4914 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4918 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
4920 /* this stack is only used during signal handling */
4921 #define SIGNAL_STACK_SIZE 32768
4923 static uint8_t *signal_stack
;
4927 /* password input */
4929 static BlockDriverState
*get_bdrv(int index
)
4931 BlockDriverState
*bs
;
4934 bs
= bs_table
[index
];
4935 } else if (index
< 6) {
4936 bs
= fd_table
[index
- 4];
4943 static void read_passwords(void)
4945 BlockDriverState
*bs
;
4949 for(i
= 0; i
< 6; i
++) {
4951 if (bs
&& bdrv_is_encrypted(bs
)) {
4952 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
4953 for(j
= 0; j
< 3; j
++) {
4954 monitor_readline("Password: ",
4955 1, password
, sizeof(password
));
4956 if (bdrv_set_key(bs
, password
) == 0)
4958 term_printf("invalid password\n");
4964 /* XXX: currently we cannot use simultaneously different CPUs */
4965 void register_machines(void)
4967 #if defined(TARGET_I386)
4968 qemu_register_machine(&pc_machine
);
4969 qemu_register_machine(&isapc_machine
);
4970 #elif defined(TARGET_PPC)
4971 qemu_register_machine(&heathrow_machine
);
4972 qemu_register_machine(&core99_machine
);
4973 qemu_register_machine(&prep_machine
);
4974 #elif defined(TARGET_MIPS)
4975 qemu_register_machine(&mips_machine
);
4976 #elif defined(TARGET_SPARC)
4977 #ifdef TARGET_SPARC64
4978 qemu_register_machine(&sun4u_machine
);
4980 qemu_register_machine(&sun4m_machine
);
4982 #elif defined(TARGET_ARM)
4983 qemu_register_machine(&integratorcp926_machine
);
4984 qemu_register_machine(&integratorcp1026_machine
);
4985 qemu_register_machine(&versatilepb_machine
);
4986 qemu_register_machine(&versatileab_machine
);
4987 #elif defined(TARGET_SH4)
4988 qemu_register_machine(&shix_machine
);
4990 #error unsupported CPU
4995 struct soundhw soundhw
[] = {
5002 { .init_isa
= pcspk_audio_init
}
5007 "Creative Sound Blaster 16",
5010 { .init_isa
= SB16_init
}
5017 "Yamaha YMF262 (OPL3)",
5019 "Yamaha YM3812 (OPL2)",
5023 { .init_isa
= Adlib_init
}
5030 "Gravis Ultrasound GF1",
5033 { .init_isa
= GUS_init
}
5039 "ENSONIQ AudioPCI ES1370",
5042 { .init_pci
= es1370_init
}
5045 { NULL
, NULL
, 0, 0, { NULL
} }
5048 static void select_soundhw (const char *optarg
)
5052 if (*optarg
== '?') {
5055 printf ("Valid sound card names (comma separated):\n");
5056 for (c
= soundhw
; c
->name
; ++c
) {
5057 printf ("%-11s %s\n", c
->name
, c
->descr
);
5059 printf ("\n-soundhw all will enable all of the above\n");
5060 exit (*optarg
!= '?');
5068 if (!strcmp (optarg
, "all")) {
5069 for (c
= soundhw
; c
->name
; ++c
) {
5077 e
= strchr (p
, ',');
5078 l
= !e
? strlen (p
) : (size_t) (e
- p
);
5080 for (c
= soundhw
; c
->name
; ++c
) {
5081 if (!strncmp (c
->name
, p
, l
)) {
5090 "Unknown sound card name (too big to show)\n");
5093 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
5098 p
+= l
+ (e
!= NULL
);
5102 goto show_valid_cards
;
5107 #define MAX_NET_CLIENTS 32
5109 int main(int argc
, char **argv
)
5111 #ifdef CONFIG_GDBSTUB
5112 int use_gdbstub
, gdbstub_port
;
5115 int snapshot
, linux_boot
;
5116 const char *initrd_filename
;
5117 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
5118 const char *kernel_filename
, *kernel_cmdline
;
5119 DisplayState
*ds
= &display_state
;
5120 int cyls
, heads
, secs
, translation
;
5121 int start_emulation
= 1;
5122 char net_clients
[MAX_NET_CLIENTS
][256];
5125 const char *r
, *optarg
;
5126 CharDriverState
*monitor_hd
;
5127 char monitor_device
[128];
5128 char serial_devices
[MAX_SERIAL_PORTS
][128];
5129 int serial_device_index
;
5130 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
5131 int parallel_device_index
;
5132 const char *loadvm
= NULL
;
5133 QEMUMachine
*machine
;
5134 char usb_devices
[MAX_USB_CMDLINE
][128];
5135 int usb_devices_index
;
5137 LIST_INIT (&vm_change_state_head
);
5138 #if !defined(CONFIG_SOFTMMU)
5139 /* we never want that malloc() uses mmap() */
5140 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
5142 register_machines();
5143 machine
= first_machine
;
5144 initrd_filename
= NULL
;
5145 for(i
= 0; i
< MAX_FD
; i
++)
5146 fd_filename
[i
] = NULL
;
5147 for(i
= 0; i
< MAX_DISKS
; i
++)
5148 hd_filename
[i
] = NULL
;
5149 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5150 vga_ram_size
= VGA_RAM_SIZE
;
5151 bios_size
= BIOS_SIZE
;
5152 #ifdef CONFIG_GDBSTUB
5154 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
5158 kernel_filename
= NULL
;
5159 kernel_cmdline
= "";
5165 cyls
= heads
= secs
= 0;
5166 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5167 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
5169 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
5170 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
5171 serial_devices
[i
][0] = '\0';
5172 serial_device_index
= 0;
5174 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
5175 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
5176 parallel_devices
[i
][0] = '\0';
5177 parallel_device_index
= 0;
5179 usb_devices_index
= 0;
5184 /* default mac address of the first network interface */
5192 hd_filename
[0] = argv
[optind
++];
5194 const QEMUOption
*popt
;
5197 popt
= qemu_options
;
5200 fprintf(stderr
, "%s: invalid option -- '%s'\n",
5204 if (!strcmp(popt
->name
, r
+ 1))
5208 if (popt
->flags
& HAS_ARG
) {
5209 if (optind
>= argc
) {
5210 fprintf(stderr
, "%s: option '%s' requires an argument\n",
5214 optarg
= argv
[optind
++];
5219 switch(popt
->index
) {
5221 machine
= find_machine(optarg
);
5224 printf("Supported machines are:\n");
5225 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5226 printf("%-10s %s%s\n",
5228 m
== first_machine
? " (default)" : "");
5233 case QEMU_OPTION_initrd
:
5234 initrd_filename
= optarg
;
5236 case QEMU_OPTION_hda
:
5237 case QEMU_OPTION_hdb
:
5238 case QEMU_OPTION_hdc
:
5239 case QEMU_OPTION_hdd
:
5242 hd_index
= popt
->index
- QEMU_OPTION_hda
;
5243 hd_filename
[hd_index
] = optarg
;
5244 if (hd_index
== cdrom_index
)
5248 case QEMU_OPTION_snapshot
:
5251 case QEMU_OPTION_hdachs
:
5255 cyls
= strtol(p
, (char **)&p
, 0);
5256 if (cyls
< 1 || cyls
> 16383)
5261 heads
= strtol(p
, (char **)&p
, 0);
5262 if (heads
< 1 || heads
> 16)
5267 secs
= strtol(p
, (char **)&p
, 0);
5268 if (secs
< 1 || secs
> 63)
5272 if (!strcmp(p
, "none"))
5273 translation
= BIOS_ATA_TRANSLATION_NONE
;
5274 else if (!strcmp(p
, "lba"))
5275 translation
= BIOS_ATA_TRANSLATION_LBA
;
5276 else if (!strcmp(p
, "auto"))
5277 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5280 } else if (*p
!= '\0') {
5282 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5287 case QEMU_OPTION_nographic
:
5288 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
5289 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
5292 case QEMU_OPTION_kernel
:
5293 kernel_filename
= optarg
;
5295 case QEMU_OPTION_append
:
5296 kernel_cmdline
= optarg
;
5298 case QEMU_OPTION_cdrom
:
5299 if (cdrom_index
>= 0) {
5300 hd_filename
[cdrom_index
] = optarg
;
5303 case QEMU_OPTION_boot
:
5304 boot_device
= optarg
[0];
5305 if (boot_device
!= 'a' &&
5308 boot_device
!= 'n' &&
5310 boot_device
!= 'c' && boot_device
!= 'd') {
5311 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
5315 case QEMU_OPTION_fda
:
5316 fd_filename
[0] = optarg
;
5318 case QEMU_OPTION_fdb
:
5319 fd_filename
[1] = optarg
;
5322 case QEMU_OPTION_no_fd_bootchk
:
5326 case QEMU_OPTION_no_code_copy
:
5327 code_copy_enabled
= 0;
5329 case QEMU_OPTION_net
:
5330 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5331 fprintf(stderr
, "qemu: too many network clients\n");
5334 pstrcpy(net_clients
[nb_net_clients
],
5335 sizeof(net_clients
[0]),
5340 case QEMU_OPTION_tftp
:
5341 tftp_prefix
= optarg
;
5344 case QEMU_OPTION_smb
:
5345 net_slirp_smb(optarg
);
5348 case QEMU_OPTION_redir
:
5349 net_slirp_redir(optarg
);
5353 case QEMU_OPTION_audio_help
:
5357 case QEMU_OPTION_soundhw
:
5358 select_soundhw (optarg
);
5365 ram_size
= atoi(optarg
) * 1024 * 1024;
5368 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
5369 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
5370 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
5379 mask
= cpu_str_to_log_mask(optarg
);
5381 printf("Log items (comma separated):\n");
5382 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5383 printf("%-10s %s\n", item
->name
, item
->help
);
5390 #ifdef CONFIG_GDBSTUB
5395 gdbstub_port
= atoi(optarg
);
5402 start_emulation
= 0;
5405 keyboard_layout
= optarg
;
5407 case QEMU_OPTION_localtime
:
5410 case QEMU_OPTION_cirrusvga
:
5411 cirrus_vga_enabled
= 1;
5413 case QEMU_OPTION_std_vga
:
5414 cirrus_vga_enabled
= 0;
5421 w
= strtol(p
, (char **)&p
, 10);
5424 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5430 h
= strtol(p
, (char **)&p
, 10);
5435 depth
= strtol(p
, (char **)&p
, 10);
5436 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5437 depth
!= 24 && depth
!= 32)
5439 } else if (*p
== '\0') {
5440 depth
= graphic_depth
;
5447 graphic_depth
= depth
;
5450 case QEMU_OPTION_monitor
:
5451 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
5453 case QEMU_OPTION_serial
:
5454 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5455 fprintf(stderr
, "qemu: too many serial ports\n");
5458 pstrcpy(serial_devices
[serial_device_index
],
5459 sizeof(serial_devices
[0]), optarg
);
5460 serial_device_index
++;
5462 case QEMU_OPTION_parallel
:
5463 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5464 fprintf(stderr
, "qemu: too many parallel ports\n");
5467 pstrcpy(parallel_devices
[parallel_device_index
],
5468 sizeof(parallel_devices
[0]), optarg
);
5469 parallel_device_index
++;
5471 case QEMU_OPTION_loadvm
:
5474 case QEMU_OPTION_full_screen
:
5477 case QEMU_OPTION_pidfile
:
5478 create_pidfile(optarg
);
5481 case QEMU_OPTION_win2k_hack
:
5482 win2k_install_hack
= 1;
5486 case QEMU_OPTION_no_kqemu
:
5489 case QEMU_OPTION_kernel_kqemu
:
5493 case QEMU_OPTION_usb
:
5496 case QEMU_OPTION_usbdevice
:
5498 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5499 fprintf(stderr
, "Too many USB devices\n");
5502 pstrcpy(usb_devices
[usb_devices_index
],
5503 sizeof(usb_devices
[usb_devices_index
]),
5505 usb_devices_index
++;
5507 case QEMU_OPTION_smp
:
5508 smp_cpus
= atoi(optarg
);
5509 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
5510 fprintf(stderr
, "Invalid number of CPUs\n");
5514 case QEMU_OPTION_vnc
:
5515 vnc_display
= atoi(optarg
);
5516 if (vnc_display
< 0) {
5517 fprintf(stderr
, "Invalid VNC display\n");
5521 case QEMU_OPTION_no_acpi
:
5532 linux_boot
= (kernel_filename
!= NULL
);
5535 hd_filename
[0] == '\0' &&
5536 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
5537 fd_filename
[0] == '\0')
5540 /* boot to cd by default if no hard disk */
5541 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
5542 if (fd_filename
[0] != '\0')
5548 #if !defined(CONFIG_SOFTMMU)
5549 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
5551 static uint8_t stdout_buf
[4096];
5552 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
5555 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5562 /* init network clients */
5563 if (nb_net_clients
== 0) {
5564 /* if no clients, we use a default config */
5565 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
5567 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
5572 for(i
= 0;i
< nb_net_clients
; i
++) {
5573 if (net_client_init(net_clients
[i
]) < 0)
5577 /* init the memory */
5578 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
5580 #ifdef CONFIG_SOFTMMU
5581 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5582 if (!phys_ram_base
) {
5583 fprintf(stderr
, "Could not allocate physical memory\n");
5587 /* as we must map the same page at several addresses, we must use
5592 tmpdir
= getenv("QEMU_TMPDIR");
5595 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
5596 if (mkstemp(phys_ram_file
) < 0) {
5597 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
5601 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
5602 if (phys_ram_fd
< 0) {
5603 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
5607 ftruncate(phys_ram_fd
, phys_ram_size
);
5608 unlink(phys_ram_file
);
5609 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
5611 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
5613 if (phys_ram_base
== MAP_FAILED
) {
5614 fprintf(stderr
, "Could not map physical memory\n");
5620 /* we always create the cdrom drive, even if no disk is there */
5622 if (cdrom_index
>= 0) {
5623 bs_table
[cdrom_index
] = bdrv_new("cdrom");
5624 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
5627 /* open the virtual block devices */
5628 for(i
= 0; i
< MAX_DISKS
; i
++) {
5629 if (hd_filename
[i
]) {
5632 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
5633 bs_table
[i
] = bdrv_new(buf
);
5635 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
5636 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
5640 if (i
== 0 && cyls
!= 0) {
5641 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
5642 bdrv_set_translation_hint(bs_table
[i
], translation
);
5647 /* we always create at least one floppy disk */
5648 fd_table
[0] = bdrv_new("fda");
5649 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
5651 for(i
= 0; i
< MAX_FD
; i
++) {
5652 if (fd_filename
[i
]) {
5655 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
5656 fd_table
[i
] = bdrv_new(buf
);
5657 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
5659 if (fd_filename
[i
] != '\0') {
5660 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
5661 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
5669 register_savevm("timer", 0, 1, timer_save
, timer_load
, NULL
);
5670 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
5673 cpu_calibrate_ticks();
5677 dumb_display_init(ds
);
5678 } else if (vnc_display
!= -1) {
5679 vnc_display_init(ds
, vnc_display
);
5681 #if defined(CONFIG_SDL)
5682 sdl_display_init(ds
, full_screen
);
5683 #elif defined(CONFIG_COCOA)
5684 cocoa_display_init(ds
, full_screen
);
5686 dumb_display_init(ds
);
5690 monitor_hd
= qemu_chr_open(monitor_device
);
5692 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5695 monitor_init(monitor_hd
, !nographic
);
5697 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5698 if (serial_devices
[i
][0] != '\0') {
5699 serial_hds
[i
] = qemu_chr_open(serial_devices
[i
]);
5700 if (!serial_hds
[i
]) {
5701 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5705 if (!strcmp(serial_devices
[i
], "vc"))
5706 qemu_chr_printf(serial_hds
[i
], "serial%d console\n", i
);
5710 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5711 if (parallel_devices
[i
][0] != '\0') {
5712 parallel_hds
[i
] = qemu_chr_open(parallel_devices
[i
]);
5713 if (!parallel_hds
[i
]) {
5714 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5715 parallel_devices
[i
]);
5718 if (!strcmp(parallel_devices
[i
], "vc"))
5719 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\n", i
);
5723 /* setup cpu signal handlers for MMU / self modifying code handling */
5724 #if !defined(CONFIG_SOFTMMU)
5726 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5729 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
5730 stk
.ss_sp
= signal_stack
;
5731 stk
.ss_size
= SIGNAL_STACK_SIZE
;
5734 if (sigaltstack(&stk
, NULL
) < 0) {
5735 perror("sigaltstack");
5741 struct sigaction act
;
5743 sigfillset(&act
.sa_mask
);
5744 act
.sa_flags
= SA_SIGINFO
;
5745 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5746 act
.sa_flags
|= SA_ONSTACK
;
5748 act
.sa_sigaction
= host_segv_handler
;
5749 sigaction(SIGSEGV
, &act
, NULL
);
5750 sigaction(SIGBUS
, &act
, NULL
);
5751 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5752 sigaction(SIGFPE
, &act
, NULL
);
5759 struct sigaction act
;
5760 sigfillset(&act
.sa_mask
);
5762 act
.sa_handler
= SIG_IGN
;
5763 sigaction(SIGPIPE
, &act
, NULL
);
5768 machine
->init(ram_size
, vga_ram_size
, boot_device
,
5769 ds
, fd_filename
, snapshot
,
5770 kernel_filename
, kernel_cmdline
, initrd_filename
);
5772 /* init USB devices */
5774 for(i
= 0; i
< usb_devices_index
; i
++) {
5775 if (usb_device_add(usb_devices
[i
]) < 0) {
5776 fprintf(stderr
, "Warning: could not add USB device %s\n",
5782 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
5783 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
5785 #ifdef CONFIG_GDBSTUB
5787 if (gdbserver_start(gdbstub_port
) < 0) {
5788 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
5792 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
5797 qemu_loadvm(loadvm
);
5800 /* XXX: simplify init */
5802 if (start_emulation
) {