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 */
94 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
96 //#define DEBUG_UNUSED_IOPORT
97 //#define DEBUG_IOPORT
99 #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 /* compute with 96 bit intermediate result: (a*b)/c */
490 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
495 #ifdef WORDS_BIGENDIAN
505 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
506 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
509 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
513 /***********************************************************/
514 /* real time host monotonic timer */
516 #define QEMU_TIMER_BASE 1000000000LL
520 static int64_t clock_freq
;
522 static void init_get_clock(void)
526 ret
= QueryPerformanceFrequency(&freq
);
528 fprintf(stderr
, "Could not calibrate ticks\n");
531 clock_freq
= freq
.QuadPart
;
534 static int64_t get_clock(void)
537 QueryPerformanceCounter(&ti
);
538 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
543 static int use_rt_clock
;
545 static void init_get_clock(void)
548 #if defined(__linux__)
551 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
558 static int64_t get_clock(void)
560 #if defined(__linux__)
563 clock_gettime(CLOCK_MONOTONIC
, &ts
);
564 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
568 /* XXX: using gettimeofday leads to problems if the date
569 changes, so it should be avoided. */
571 gettimeofday(&tv
, NULL
);
572 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
578 /***********************************************************/
579 /* guest cycle counter */
581 static int64_t cpu_ticks_prev
;
582 static int64_t cpu_ticks_offset
;
583 static int64_t cpu_clock_offset
;
584 static int cpu_ticks_enabled
;
586 /* return the host CPU cycle counter and handle stop/restart */
587 int64_t cpu_get_ticks(void)
589 if (!cpu_ticks_enabled
) {
590 return cpu_ticks_offset
;
593 ticks
= cpu_get_real_ticks();
594 if (cpu_ticks_prev
> ticks
) {
595 /* Note: non increasing ticks may happen if the host uses
597 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
599 cpu_ticks_prev
= ticks
;
600 return ticks
+ cpu_ticks_offset
;
604 /* return the host CPU monotonic timer and handle stop/restart */
605 static int64_t cpu_get_clock(void)
608 if (!cpu_ticks_enabled
) {
609 return cpu_clock_offset
;
612 return ti
+ cpu_clock_offset
;
616 /* enable cpu_get_ticks() */
617 void cpu_enable_ticks(void)
619 if (!cpu_ticks_enabled
) {
620 cpu_ticks_offset
-= cpu_get_real_ticks();
621 cpu_clock_offset
-= get_clock();
622 cpu_ticks_enabled
= 1;
626 /* disable cpu_get_ticks() : the clock is stopped. You must not call
627 cpu_get_ticks() after that. */
628 void cpu_disable_ticks(void)
630 if (cpu_ticks_enabled
) {
631 cpu_ticks_offset
= cpu_get_ticks();
632 cpu_clock_offset
= cpu_get_clock();
633 cpu_ticks_enabled
= 0;
637 /***********************************************************/
640 #define QEMU_TIMER_REALTIME 0
641 #define QEMU_TIMER_VIRTUAL 1
645 /* XXX: add frequency */
653 struct QEMUTimer
*next
;
659 static QEMUTimer
*active_timers
[2];
661 static MMRESULT timerID
;
662 static HANDLE host_alarm
= NULL
;
663 static unsigned int period
= 1;
665 /* frequency of the times() clock tick */
666 static int timer_freq
;
669 QEMUClock
*qemu_new_clock(int type
)
672 clock
= qemu_mallocz(sizeof(QEMUClock
));
679 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
683 ts
= qemu_mallocz(sizeof(QEMUTimer
));
690 void qemu_free_timer(QEMUTimer
*ts
)
695 /* stop a timer, but do not dealloc it */
696 void qemu_del_timer(QEMUTimer
*ts
)
700 /* NOTE: this code must be signal safe because
701 qemu_timer_expired() can be called from a signal. */
702 pt
= &active_timers
[ts
->clock
->type
];
715 /* modify the current timer so that it will be fired when current_time
716 >= expire_time. The corresponding callback will be called. */
717 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
723 /* add the timer in the sorted list */
724 /* NOTE: this code must be signal safe because
725 qemu_timer_expired() can be called from a signal. */
726 pt
= &active_timers
[ts
->clock
->type
];
731 if (t
->expire_time
> expire_time
)
735 ts
->expire_time
= expire_time
;
740 int qemu_timer_pending(QEMUTimer
*ts
)
743 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
750 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
754 return (timer_head
->expire_time
<= current_time
);
757 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
763 if (!ts
|| ts
->expire_time
> current_time
)
765 /* remove timer from the list before calling the callback */
766 *ptimer_head
= ts
->next
;
769 /* run the callback (the timer list can be modified) */
774 int64_t qemu_get_clock(QEMUClock
*clock
)
776 switch(clock
->type
) {
777 case QEMU_TIMER_REALTIME
:
778 return get_clock() / 1000000;
780 case QEMU_TIMER_VIRTUAL
:
781 return cpu_get_clock();
785 static void init_timers(void)
788 ticks_per_sec
= QEMU_TIMER_BASE
;
789 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
790 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
794 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
796 uint64_t expire_time
;
798 if (qemu_timer_pending(ts
)) {
799 expire_time
= ts
->expire_time
;
803 qemu_put_be64(f
, expire_time
);
806 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
808 uint64_t expire_time
;
810 expire_time
= qemu_get_be64(f
);
811 if (expire_time
!= -1) {
812 qemu_mod_timer(ts
, expire_time
);
818 static void timer_save(QEMUFile
*f
, void *opaque
)
820 if (cpu_ticks_enabled
) {
821 hw_error("cannot save state if virtual timers are running");
823 qemu_put_be64s(f
, &cpu_ticks_offset
);
824 qemu_put_be64s(f
, &ticks_per_sec
);
825 qemu_put_be64s(f
, &cpu_clock_offset
);
828 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
832 if (cpu_ticks_enabled
) {
835 qemu_get_be64s(f
, &cpu_ticks_offset
);
836 qemu_get_be64s(f
, &ticks_per_sec
);
837 qemu_get_be64s(f
, &cpu_clock_offset
);
842 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
843 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
845 static void host_alarm_handler(int host_signum
)
849 #define DISP_FREQ 1000
851 static int64_t delta_min
= INT64_MAX
;
852 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
854 ti
= qemu_get_clock(vm_clock
);
855 if (last_clock
!= 0) {
856 delta
= ti
- last_clock
;
857 if (delta
< delta_min
)
859 if (delta
> delta_max
)
862 if (++count
== DISP_FREQ
) {
863 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
864 muldiv64(delta_min
, 1000000, ticks_per_sec
),
865 muldiv64(delta_max
, 1000000, ticks_per_sec
),
866 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
867 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
869 delta_min
= INT64_MAX
;
877 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
878 qemu_get_clock(vm_clock
)) ||
879 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
880 qemu_get_clock(rt_clock
))) {
882 SetEvent(host_alarm
);
884 CPUState
*env
= cpu_single_env
;
886 /* stop the currently executing cpu because a timer occured */
887 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
889 if (env
->kqemu_enabled
) {
890 kqemu_cpu_interrupt(env
);
899 #if defined(__linux__)
901 #define RTC_FREQ 1024
905 static int start_rtc_timer(void)
907 rtc_fd
= open("/dev/rtc", O_RDONLY
);
910 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
911 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
912 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
913 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
916 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
921 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
927 static int start_rtc_timer(void)
932 #endif /* !defined(__linux__) */
934 #endif /* !defined(_WIN32) */
936 static void init_timer_alarm(void)
943 ZeroMemory(&tc
, sizeof(TIMECAPS
));
944 timeGetDevCaps(&tc
, sizeof(TIMECAPS
));
945 if (period
< tc
.wPeriodMin
)
946 period
= tc
.wPeriodMin
;
947 timeBeginPeriod(period
);
948 timerID
= timeSetEvent(1, // interval (ms)
949 period
, // resolution
950 host_alarm_handler
, // function
951 (DWORD
)&count
, // user parameter
952 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
954 perror("failed timer alarm");
957 host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
959 perror("failed CreateEvent");
962 qemu_add_wait_object(host_alarm
, NULL
, NULL
);
964 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
967 struct sigaction act
;
968 struct itimerval itv
;
970 /* get times() syscall frequency */
971 timer_freq
= sysconf(_SC_CLK_TCK
);
974 sigfillset(&act
.sa_mask
);
976 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
977 act
.sa_flags
|= SA_ONSTACK
;
979 act
.sa_handler
= host_alarm_handler
;
980 sigaction(SIGALRM
, &act
, NULL
);
982 itv
.it_interval
.tv_sec
= 0;
983 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
984 itv
.it_value
.tv_sec
= 0;
985 itv
.it_value
.tv_usec
= 10 * 1000;
986 setitimer(ITIMER_REAL
, &itv
, NULL
);
987 /* we probe the tick duration of the kernel to inform the user if
988 the emulated kernel requested a too high timer frequency */
989 getitimer(ITIMER_REAL
, &itv
);
991 #if defined(__linux__)
992 /* XXX: force /dev/rtc usage because even 2.6 kernels may not
993 have timers with 1 ms resolution. The correct solution will
994 be to use the POSIX real time timers available in recent
996 if (itv
.it_interval
.tv_usec
> 1000 || 1) {
997 /* try to use /dev/rtc to have a faster timer */
998 if (start_rtc_timer() < 0)
1000 /* disable itimer */
1001 itv
.it_interval
.tv_sec
= 0;
1002 itv
.it_interval
.tv_usec
= 0;
1003 itv
.it_value
.tv_sec
= 0;
1004 itv
.it_value
.tv_usec
= 0;
1005 setitimer(ITIMER_REAL
, &itv
, NULL
);
1008 sigaction(SIGIO
, &act
, NULL
);
1009 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1010 fcntl(rtc_fd
, F_SETOWN
, getpid());
1012 #endif /* defined(__linux__) */
1015 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1016 PIT_FREQ
) / 1000000;
1022 void quit_timers(void)
1025 timeKillEvent(timerID
);
1026 timeEndPeriod(period
);
1028 CloseHandle(host_alarm
);
1034 /***********************************************************/
1035 /* character device */
1037 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1039 return s
->chr_write(s
, buf
, len
);
1042 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1046 return s
->chr_ioctl(s
, cmd
, arg
);
1049 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1054 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1055 qemu_chr_write(s
, buf
, strlen(buf
));
1059 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1061 if (s
->chr_send_event
)
1062 s
->chr_send_event(s
, event
);
1065 void qemu_chr_add_read_handler(CharDriverState
*s
,
1066 IOCanRWHandler
*fd_can_read
,
1067 IOReadHandler
*fd_read
, void *opaque
)
1069 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
1072 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
1074 s
->chr_event
= chr_event
;
1077 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1082 static void null_chr_add_read_handler(CharDriverState
*chr
,
1083 IOCanRWHandler
*fd_can_read
,
1084 IOReadHandler
*fd_read
, void *opaque
)
1088 CharDriverState
*qemu_chr_open_null(void)
1090 CharDriverState
*chr
;
1092 chr
= qemu_mallocz(sizeof(CharDriverState
));
1095 chr
->chr_write
= null_chr_write
;
1096 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1102 static void socket_cleanup(void)
1107 static int socket_init(void)
1112 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1114 err
= WSAGetLastError();
1115 fprintf(stderr
, "WSAStartup: %d\n", err
);
1118 atexit(socket_cleanup
);
1122 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1128 ret
= send(fd
, buf
, len
, 0);
1131 errno
= WSAGetLastError();
1132 if (errno
!= WSAEWOULDBLOCK
) {
1135 } else if (ret
== 0) {
1145 void socket_set_nonblock(int fd
)
1147 unsigned long opt
= 1;
1148 ioctlsocket(fd
, FIONBIO
, &opt
);
1153 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1159 ret
= write(fd
, buf
, len
);
1161 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1163 } else if (ret
== 0) {
1173 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1175 return unix_write(fd
, buf
, len1
);
1178 void socket_set_nonblock(int fd
)
1180 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1182 #endif /* !_WIN32 */
1188 IOCanRWHandler
*fd_can_read
;
1189 IOReadHandler
*fd_read
;
1194 #define STDIO_MAX_CLIENTS 2
1196 static int stdio_nb_clients
;
1197 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1199 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1201 FDCharDriver
*s
= chr
->opaque
;
1202 return unix_write(s
->fd_out
, buf
, len
);
1205 static int fd_chr_read_poll(void *opaque
)
1207 CharDriverState
*chr
= opaque
;
1208 FDCharDriver
*s
= chr
->opaque
;
1210 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
1214 static void fd_chr_read(void *opaque
)
1216 CharDriverState
*chr
= opaque
;
1217 FDCharDriver
*s
= chr
->opaque
;
1222 if (len
> s
->max_size
)
1226 size
= read(s
->fd_in
, buf
, len
);
1228 s
->fd_read(s
->fd_opaque
, buf
, size
);
1232 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1233 IOCanRWHandler
*fd_can_read
,
1234 IOReadHandler
*fd_read
, void *opaque
)
1236 FDCharDriver
*s
= chr
->opaque
;
1238 if (s
->fd_in
>= 0) {
1239 s
->fd_can_read
= fd_can_read
;
1240 s
->fd_read
= fd_read
;
1241 s
->fd_opaque
= opaque
;
1242 if (nographic
&& s
->fd_in
== 0) {
1244 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1245 fd_chr_read
, NULL
, chr
);
1250 /* open a character device to a unix fd */
1251 CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1253 CharDriverState
*chr
;
1256 chr
= qemu_mallocz(sizeof(CharDriverState
));
1259 s
= qemu_mallocz(sizeof(FDCharDriver
));
1267 chr
->chr_write
= fd_chr_write
;
1268 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1272 CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1276 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1279 return qemu_chr_open_fd(-1, fd_out
);
1282 CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1286 fd
= open(filename
, O_RDWR
| O_BINARY
);
1289 return qemu_chr_open_fd(fd
, fd
);
1293 /* for STDIO, we handle the case where several clients use it
1296 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1298 #define TERM_FIFO_MAX_SIZE 1
1300 static int term_got_escape
, client_index
;
1301 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1302 static int term_fifo_size
;
1303 static int term_timestamps
;
1304 static int64_t term_timestamps_start
;
1306 void term_print_help(void)
1309 "C-a h print this help\n"
1310 "C-a x exit emulator\n"
1311 "C-a s save disk data back to file (if -snapshot)\n"
1312 "C-a b send break (magic sysrq)\n"
1313 "C-a t toggle console timestamps\n"
1314 "C-a c switch between console and monitor\n"
1315 "C-a C-a send C-a\n"
1319 /* called when a char is received */
1320 static void stdio_received_byte(int ch
)
1322 if (term_got_escape
) {
1323 term_got_escape
= 0;
1334 for (i
= 0; i
< MAX_DISKS
; i
++) {
1336 bdrv_commit(bs_table
[i
]);
1341 if (client_index
< stdio_nb_clients
) {
1342 CharDriverState
*chr
;
1345 chr
= stdio_clients
[client_index
];
1347 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1352 if (client_index
>= stdio_nb_clients
)
1354 if (client_index
== 0) {
1355 /* send a new line in the monitor to get the prompt */
1361 term_timestamps
= !term_timestamps
;
1362 term_timestamps_start
= -1;
1367 } else if (ch
== TERM_ESCAPE
) {
1368 term_got_escape
= 1;
1371 if (client_index
< stdio_nb_clients
) {
1373 CharDriverState
*chr
;
1376 chr
= stdio_clients
[client_index
];
1378 if (s
->fd_can_read(s
->fd_opaque
) > 0) {
1380 s
->fd_read(s
->fd_opaque
, buf
, 1);
1381 } else if (term_fifo_size
== 0) {
1382 term_fifo
[term_fifo_size
++] = ch
;
1388 static int stdio_read_poll(void *opaque
)
1390 CharDriverState
*chr
;
1393 if (client_index
< stdio_nb_clients
) {
1394 chr
= stdio_clients
[client_index
];
1396 /* try to flush the queue if needed */
1397 if (term_fifo_size
!= 0 && s
->fd_can_read(s
->fd_opaque
) > 0) {
1398 s
->fd_read(s
->fd_opaque
, term_fifo
, 1);
1401 /* see if we can absorb more chars */
1402 if (term_fifo_size
== 0)
1411 static void stdio_read(void *opaque
)
1416 size
= read(0, buf
, 1);
1418 stdio_received_byte(buf
[0]);
1421 static int stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1423 FDCharDriver
*s
= chr
->opaque
;
1424 if (!term_timestamps
) {
1425 return unix_write(s
->fd_out
, buf
, len
);
1430 for(i
= 0; i
< len
; i
++) {
1431 unix_write(s
->fd_out
, buf
+ i
, 1);
1432 if (buf
[i
] == '\n') {
1437 if (term_timestamps_start
== -1)
1438 term_timestamps_start
= ti
;
1439 ti
-= term_timestamps_start
;
1440 secs
= ti
/ 1000000000;
1441 snprintf(buf1
, sizeof(buf1
),
1442 "[%02d:%02d:%02d.%03d] ",
1446 (int)((ti
/ 1000000) % 1000));
1447 unix_write(s
->fd_out
, buf1
, strlen(buf1
));
1454 /* init terminal so that we can grab keys */
1455 static struct termios oldtty
;
1456 static int old_fd0_flags
;
1458 static void term_exit(void)
1460 tcsetattr (0, TCSANOW
, &oldtty
);
1461 fcntl(0, F_SETFL
, old_fd0_flags
);
1464 static void term_init(void)
1468 tcgetattr (0, &tty
);
1470 old_fd0_flags
= fcntl(0, F_GETFL
);
1472 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1473 |INLCR
|IGNCR
|ICRNL
|IXON
);
1474 tty
.c_oflag
|= OPOST
;
1475 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1476 /* if graphical mode, we allow Ctrl-C handling */
1478 tty
.c_lflag
&= ~ISIG
;
1479 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1482 tty
.c_cc
[VTIME
] = 0;
1484 tcsetattr (0, TCSANOW
, &tty
);
1488 fcntl(0, F_SETFL
, O_NONBLOCK
);
1491 CharDriverState
*qemu_chr_open_stdio(void)
1493 CharDriverState
*chr
;
1496 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1498 chr
= qemu_chr_open_fd(0, 1);
1499 chr
->chr_write
= stdio_write
;
1500 if (stdio_nb_clients
== 0)
1501 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, NULL
);
1502 client_index
= stdio_nb_clients
;
1504 if (stdio_nb_clients
!= 0)
1506 chr
= qemu_chr_open_fd(0, 1);
1508 stdio_clients
[stdio_nb_clients
++] = chr
;
1509 if (stdio_nb_clients
== 1) {
1510 /* set the terminal in raw mode */
1516 #if defined(__linux__)
1517 CharDriverState
*qemu_chr_open_pty(void)
1520 char slave_name
[1024];
1521 int master_fd
, slave_fd
;
1523 /* Not satisfying */
1524 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1528 /* Disabling local echo and line-buffered output */
1529 tcgetattr (master_fd
, &tty
);
1530 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1532 tty
.c_cc
[VTIME
] = 0;
1533 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1535 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1536 return qemu_chr_open_fd(master_fd
, master_fd
);
1539 static void tty_serial_init(int fd
, int speed
,
1540 int parity
, int data_bits
, int stop_bits
)
1546 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1547 speed
, parity
, data_bits
, stop_bits
);
1549 tcgetattr (fd
, &tty
);
1591 cfsetispeed(&tty
, spd
);
1592 cfsetospeed(&tty
, spd
);
1594 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1595 |INLCR
|IGNCR
|ICRNL
|IXON
);
1596 tty
.c_oflag
|= OPOST
;
1597 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1598 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
);
1619 tty
.c_cflag
|= PARENB
;
1622 tty
.c_cflag
|= PARENB
| PARODD
;
1626 tcsetattr (fd
, TCSANOW
, &tty
);
1629 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1631 FDCharDriver
*s
= chr
->opaque
;
1634 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1636 QEMUSerialSetParams
*ssp
= arg
;
1637 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1638 ssp
->data_bits
, ssp
->stop_bits
);
1641 case CHR_IOCTL_SERIAL_SET_BREAK
:
1643 int enable
= *(int *)arg
;
1645 tcsendbreak(s
->fd_in
, 1);
1654 CharDriverState
*qemu_chr_open_tty(const char *filename
)
1656 CharDriverState
*chr
;
1659 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1662 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1663 tty_serial_init(fd
, 115200, 'N', 8, 1);
1664 chr
= qemu_chr_open_fd(fd
, fd
);
1667 chr
->chr_ioctl
= tty_serial_ioctl
;
1671 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1673 int fd
= (int)chr
->opaque
;
1677 case CHR_IOCTL_PP_READ_DATA
:
1678 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1680 *(uint8_t *)arg
= b
;
1682 case CHR_IOCTL_PP_WRITE_DATA
:
1683 b
= *(uint8_t *)arg
;
1684 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1687 case CHR_IOCTL_PP_READ_CONTROL
:
1688 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1690 *(uint8_t *)arg
= b
;
1692 case CHR_IOCTL_PP_WRITE_CONTROL
:
1693 b
= *(uint8_t *)arg
;
1694 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1697 case CHR_IOCTL_PP_READ_STATUS
:
1698 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1700 *(uint8_t *)arg
= b
;
1708 CharDriverState
*qemu_chr_open_pp(const char *filename
)
1710 CharDriverState
*chr
;
1713 fd
= open(filename
, O_RDWR
);
1717 if (ioctl(fd
, PPCLAIM
) < 0) {
1722 chr
= qemu_mallocz(sizeof(CharDriverState
));
1727 chr
->opaque
= (void *)fd
;
1728 chr
->chr_write
= null_chr_write
;
1729 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1730 chr
->chr_ioctl
= pp_ioctl
;
1735 CharDriverState
*qemu_chr_open_pty(void)
1741 #endif /* !defined(_WIN32) */
1745 IOCanRWHandler
*fd_can_read
;
1746 IOReadHandler
*fd_read
;
1749 HANDLE hcom
, hrecv
, hsend
;
1750 OVERLAPPED orecv
, osend
;
1755 #define NSENDBUF 2048
1756 #define NRECVBUF 2048
1757 #define MAXCONNECT 1
1758 #define NTIMEOUT 5000
1760 static int win_chr_poll(void *opaque
);
1761 static int win_chr_pipe_poll(void *opaque
);
1763 static void win_chr_close2(WinCharState
*s
)
1766 CloseHandle(s
->hsend
);
1770 CloseHandle(s
->hrecv
);
1774 CloseHandle(s
->hcom
);
1778 qemu_del_polling_cb(win_chr_pipe_poll
, s
);
1780 qemu_del_polling_cb(win_chr_poll
, s
);
1783 static void win_chr_close(CharDriverState
*chr
)
1785 WinCharState
*s
= chr
->opaque
;
1789 static int win_chr_init(WinCharState
*s
, const char *filename
)
1792 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1797 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1799 fprintf(stderr
, "Failed CreateEvent\n");
1802 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1804 fprintf(stderr
, "Failed CreateEvent\n");
1808 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1809 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1810 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1811 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1816 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1817 fprintf(stderr
, "Failed SetupComm\n");
1821 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1822 size
= sizeof(COMMCONFIG
);
1823 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1824 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1825 CommConfigDialog(filename
, NULL
, &comcfg
);
1827 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1828 fprintf(stderr
, "Failed SetCommState\n");
1832 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1833 fprintf(stderr
, "Failed SetCommMask\n");
1837 cto
.ReadIntervalTimeout
= MAXDWORD
;
1838 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1839 fprintf(stderr
, "Failed SetCommTimeouts\n");
1843 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1844 fprintf(stderr
, "Failed ClearCommError\n");
1847 qemu_add_polling_cb(win_chr_poll
, s
);
1855 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1857 WinCharState
*s
= chr
->opaque
;
1858 DWORD len
, ret
, size
, err
;
1861 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1862 s
->osend
.hEvent
= s
->hsend
;
1865 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1867 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1869 err
= GetLastError();
1870 if (err
== ERROR_IO_PENDING
) {
1871 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1889 static int win_chr_read_poll(WinCharState
*s
)
1891 s
->max_size
= s
->fd_can_read(s
->win_opaque
);
1895 static void win_chr_readfile(WinCharState
*s
)
1901 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1902 s
->orecv
.hEvent
= s
->hrecv
;
1903 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1905 err
= GetLastError();
1906 if (err
== ERROR_IO_PENDING
) {
1907 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1912 s
->fd_read(s
->win_opaque
, buf
, size
);
1916 static void win_chr_read(WinCharState
*s
)
1918 if (s
->len
> s
->max_size
)
1919 s
->len
= s
->max_size
;
1923 win_chr_readfile(s
);
1926 static int win_chr_poll(void *opaque
)
1928 WinCharState
*s
= opaque
;
1932 ClearCommError(s
->hcom
, &comerr
, &status
);
1933 if (status
.cbInQue
> 0) {
1934 s
->len
= status
.cbInQue
;
1935 win_chr_read_poll(s
);
1942 static void win_chr_add_read_handler(CharDriverState
*chr
,
1943 IOCanRWHandler
*fd_can_read
,
1944 IOReadHandler
*fd_read
, void *opaque
)
1946 WinCharState
*s
= chr
->opaque
;
1948 s
->fd_can_read
= fd_can_read
;
1949 s
->fd_read
= fd_read
;
1950 s
->win_opaque
= opaque
;
1953 CharDriverState
*qemu_chr_open_win(const char *filename
)
1955 CharDriverState
*chr
;
1958 chr
= qemu_mallocz(sizeof(CharDriverState
));
1961 s
= qemu_mallocz(sizeof(WinCharState
));
1967 chr
->chr_write
= win_chr_write
;
1968 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
1969 chr
->chr_close
= win_chr_close
;
1971 if (win_chr_init(s
, filename
) < 0) {
1979 static int win_chr_pipe_poll(void *opaque
)
1981 WinCharState
*s
= opaque
;
1984 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1987 win_chr_read_poll(s
);
1994 static int win_chr_pipe_init(WinCharState
*s
, const char *filename
)
2003 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2005 fprintf(stderr
, "Failed CreateEvent\n");
2008 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2010 fprintf(stderr
, "Failed CreateEvent\n");
2014 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2015 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2016 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2018 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2019 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2020 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2025 ZeroMemory(&ov
, sizeof(ov
));
2026 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2027 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2029 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2033 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2035 fprintf(stderr
, "Failed GetOverlappedResult\n");
2037 CloseHandle(ov
.hEvent
);
2044 CloseHandle(ov
.hEvent
);
2047 qemu_add_polling_cb(win_chr_pipe_poll
, s
);
2056 CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2058 CharDriverState
*chr
;
2061 chr
= qemu_mallocz(sizeof(CharDriverState
));
2064 s
= qemu_mallocz(sizeof(WinCharState
));
2070 chr
->chr_write
= win_chr_write
;
2071 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2072 chr
->chr_close
= win_chr_close
;
2074 if (win_chr_pipe_init(s
, filename
) < 0) {
2082 CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2084 CharDriverState
*chr
;
2087 chr
= qemu_mallocz(sizeof(CharDriverState
));
2090 s
= qemu_mallocz(sizeof(WinCharState
));
2097 chr
->chr_write
= win_chr_write
;
2098 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2102 CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2106 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2107 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2108 if (fd_out
== INVALID_HANDLE_VALUE
)
2111 return qemu_chr_open_win_file(fd_out
);
2115 /***********************************************************/
2116 /* UDP Net console */
2119 IOCanRWHandler
*fd_can_read
;
2120 IOReadHandler
*fd_read
;
2123 struct sockaddr_in daddr
;
2130 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2132 NetCharDriver
*s
= chr
->opaque
;
2134 return sendto(s
->fd
, buf
, len
, 0,
2135 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2138 static int udp_chr_read_poll(void *opaque
)
2140 CharDriverState
*chr
= opaque
;
2141 NetCharDriver
*s
= chr
->opaque
;
2143 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2145 /* If there were any stray characters in the queue process them
2148 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2149 s
->fd_read(s
->fd_opaque
, &s
->buf
[s
->bufptr
], 1);
2151 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2156 static void udp_chr_read(void *opaque
)
2158 CharDriverState
*chr
= opaque
;
2159 NetCharDriver
*s
= chr
->opaque
;
2161 if (s
->max_size
== 0)
2163 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2164 s
->bufptr
= s
->bufcnt
;
2169 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2170 s
->fd_read(s
->fd_opaque
, &s
->buf
[s
->bufptr
], 1);
2172 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2176 static void udp_chr_add_read_handler(CharDriverState
*chr
,
2177 IOCanRWHandler
*fd_can_read
,
2178 IOReadHandler
*fd_read
, void *opaque
)
2180 NetCharDriver
*s
= chr
->opaque
;
2183 s
->fd_can_read
= fd_can_read
;
2184 s
->fd_read
= fd_read
;
2185 s
->fd_opaque
= opaque
;
2186 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2187 udp_chr_read
, NULL
, chr
);
2191 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2192 int parse_host_src_port(struct sockaddr_in
*haddr
,
2193 struct sockaddr_in
*saddr
,
2196 CharDriverState
*qemu_chr_open_udp(const char *def
)
2198 CharDriverState
*chr
= NULL
;
2199 NetCharDriver
*s
= NULL
;
2201 struct sockaddr_in saddr
;
2203 chr
= qemu_mallocz(sizeof(CharDriverState
));
2206 s
= qemu_mallocz(sizeof(NetCharDriver
));
2210 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2212 perror("socket(PF_INET, SOCK_DGRAM)");
2216 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2217 printf("Could not parse: %s\n", def
);
2221 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2231 chr
->chr_write
= udp_chr_write
;
2232 chr
->chr_add_read_handler
= udp_chr_add_read_handler
;
2245 /***********************************************************/
2246 /* TCP Net console */
2249 IOCanRWHandler
*fd_can_read
;
2250 IOReadHandler
*fd_read
;
2258 static void tcp_chr_accept(void *opaque
);
2260 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2262 TCPCharDriver
*s
= chr
->opaque
;
2264 return send_all(s
->fd
, buf
, len
);
2266 /* XXX: indicate an error ? */
2271 static int tcp_chr_read_poll(void *opaque
)
2273 CharDriverState
*chr
= opaque
;
2274 TCPCharDriver
*s
= chr
->opaque
;
2277 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2282 #define IAC_BREAK 243
2283 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2285 char *buf
, int *size
)
2287 /* Handle any telnet client's basic IAC options to satisfy char by
2288 * char mode with no echo. All IAC options will be removed from
2289 * the buf and the do_telnetopt variable will be used to track the
2290 * state of the width of the IAC information.
2292 * IAC commands come in sets of 3 bytes with the exception of the
2293 * "IAC BREAK" command and the double IAC.
2299 for (i
= 0; i
< *size
; i
++) {
2300 if (s
->do_telnetopt
> 1) {
2301 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2302 /* Double IAC means send an IAC */
2306 s
->do_telnetopt
= 1;
2308 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2309 /* Handle IAC break commands by sending a serial break */
2310 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
2315 if (s
->do_telnetopt
>= 4) {
2316 s
->do_telnetopt
= 1;
2319 if ((unsigned char)buf
[i
] == IAC
) {
2320 s
->do_telnetopt
= 2;
2331 static void tcp_chr_read(void *opaque
)
2333 CharDriverState
*chr
= opaque
;
2334 TCPCharDriver
*s
= chr
->opaque
;
2338 if (!s
->connected
|| s
->max_size
<= 0)
2341 if (len
> s
->max_size
)
2343 size
= recv(s
->fd
, buf
, len
, 0);
2345 /* connection closed */
2347 if (s
->listen_fd
>= 0) {
2348 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2350 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2353 } else if (size
> 0) {
2354 if (s
->do_telnetopt
)
2355 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2357 s
->fd_read(s
->fd_opaque
, buf
, size
);
2361 static void tcp_chr_add_read_handler(CharDriverState
*chr
,
2362 IOCanRWHandler
*fd_can_read
,
2363 IOReadHandler
*fd_read
, void *opaque
)
2365 TCPCharDriver
*s
= chr
->opaque
;
2367 s
->fd_can_read
= fd_can_read
;
2368 s
->fd_read
= fd_read
;
2369 s
->fd_opaque
= opaque
;
2372 static void tcp_chr_connect(void *opaque
)
2374 CharDriverState
*chr
= opaque
;
2375 TCPCharDriver
*s
= chr
->opaque
;
2378 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2379 tcp_chr_read
, NULL
, chr
);
2382 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2383 static void tcp_chr_telnet_init(int fd
)
2386 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2387 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2388 send(fd
, (char *)buf
, 3, 0);
2389 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2390 send(fd
, (char *)buf
, 3, 0);
2391 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2392 send(fd
, (char *)buf
, 3, 0);
2393 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2394 send(fd
, (char *)buf
, 3, 0);
2397 static void tcp_chr_accept(void *opaque
)
2399 CharDriverState
*chr
= opaque
;
2400 TCPCharDriver
*s
= chr
->opaque
;
2401 struct sockaddr_in saddr
;
2406 len
= sizeof(saddr
);
2407 fd
= accept(s
->listen_fd
, (struct sockaddr
*)&saddr
, &len
);
2408 if (fd
< 0 && errno
!= EINTR
) {
2410 } else if (fd
>= 0) {
2411 if (s
->do_telnetopt
)
2412 tcp_chr_telnet_init(fd
);
2416 socket_set_nonblock(fd
);
2418 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2419 tcp_chr_connect(chr
);
2422 static void tcp_chr_close(CharDriverState
*chr
)
2424 TCPCharDriver
*s
= chr
->opaque
;
2427 if (s
->listen_fd
>= 0)
2428 closesocket(s
->listen_fd
);
2432 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2435 CharDriverState
*chr
= NULL
;
2436 TCPCharDriver
*s
= NULL
;
2437 int fd
= -1, ret
, err
, val
;
2439 int is_waitconnect
= 1;
2441 struct sockaddr_in saddr
;
2443 if (parse_host_port(&saddr
, host_str
) < 0)
2447 while((ptr
= strchr(ptr
,','))) {
2449 if (!strncmp(ptr
,"server",6)) {
2451 } else if (!strncmp(ptr
,"nowait",6)) {
2454 printf("Unknown option: %s\n", ptr
);
2461 chr
= qemu_mallocz(sizeof(CharDriverState
));
2464 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2468 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2472 if (!is_waitconnect
)
2473 socket_set_nonblock(fd
);
2479 /* allow fast reuse */
2481 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2483 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2486 ret
= listen(fd
, 0);
2490 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2492 s
->do_telnetopt
= 1;
2495 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2497 err
= socket_error();
2498 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2499 } else if (err
== EINPROGRESS
) {
2511 tcp_chr_connect(chr
);
2513 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2517 chr
->chr_write
= tcp_chr_write
;
2518 chr
->chr_add_read_handler
= tcp_chr_add_read_handler
;
2519 chr
->chr_close
= tcp_chr_close
;
2520 if (is_listen
&& is_waitconnect
) {
2521 printf("QEMU waiting for connection on: %s\n", host_str
);
2522 tcp_chr_accept(chr
);
2523 socket_set_nonblock(s
->listen_fd
);
2535 CharDriverState
*qemu_chr_open(const char *filename
)
2539 if (!strcmp(filename
, "vc")) {
2540 return text_console_init(&display_state
);
2541 } else if (!strcmp(filename
, "null")) {
2542 return qemu_chr_open_null();
2544 if (strstart(filename
, "tcp:", &p
)) {
2545 return qemu_chr_open_tcp(p
, 0);
2547 if (strstart(filename
, "telnet:", &p
)) {
2548 return qemu_chr_open_tcp(p
, 1);
2550 if (strstart(filename
, "udp:", &p
)) {
2551 return qemu_chr_open_udp(p
);
2554 if (strstart(filename
, "file:", &p
)) {
2555 return qemu_chr_open_file_out(p
);
2556 } else if (strstart(filename
, "pipe:", &p
)) {
2557 return qemu_chr_open_pipe(p
);
2558 } else if (!strcmp(filename
, "pty")) {
2559 return qemu_chr_open_pty();
2560 } else if (!strcmp(filename
, "stdio")) {
2561 return qemu_chr_open_stdio();
2564 #if defined(__linux__)
2565 if (strstart(filename
, "/dev/parport", NULL
)) {
2566 return qemu_chr_open_pp(filename
);
2568 if (strstart(filename
, "/dev/", NULL
)) {
2569 return qemu_chr_open_tty(filename
);
2573 if (strstart(filename
, "COM", NULL
)) {
2574 return qemu_chr_open_win(filename
);
2576 if (strstart(filename
, "pipe:", &p
)) {
2577 return qemu_chr_open_win_pipe(p
);
2579 if (strstart(filename
, "file:", &p
)) {
2580 return qemu_chr_open_win_file_out(p
);
2588 void qemu_chr_close(CharDriverState
*chr
)
2591 chr
->chr_close(chr
);
2594 /***********************************************************/
2595 /* network device redirectors */
2597 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2601 for(i
=0;i
<size
;i
+=16) {
2605 fprintf(f
, "%08x ", i
);
2608 fprintf(f
, " %02x", buf
[i
+j
]);
2613 for(j
=0;j
<len
;j
++) {
2615 if (c
< ' ' || c
> '~')
2617 fprintf(f
, "%c", c
);
2623 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
2626 for(i
= 0; i
< 6; i
++) {
2627 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2640 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
2645 p1
= strchr(p
, sep
);
2651 if (len
> buf_size
- 1)
2653 memcpy(buf
, p
, len
);
2660 int parse_host_src_port(struct sockaddr_in
*haddr
,
2661 struct sockaddr_in
*saddr
,
2662 const char *input_str
)
2664 char *str
= strdup(input_str
);
2665 char *host_str
= str
;
2670 * Chop off any extra arguments at the end of the string which
2671 * would start with a comma, then fill in the src port information
2672 * if it was provided else use the "any address" and "any port".
2674 if ((ptr
= strchr(str
,',')))
2677 if ((src_str
= strchr(input_str
,'@'))) {
2682 if (parse_host_port(haddr
, host_str
) < 0)
2685 if (!src_str
|| *src_str
== '\0')
2688 if (parse_host_port(saddr
, src_str
) < 0)
2699 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
2707 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2709 saddr
->sin_family
= AF_INET
;
2710 if (buf
[0] == '\0') {
2711 saddr
->sin_addr
.s_addr
= 0;
2713 if (isdigit(buf
[0])) {
2714 if (!inet_aton(buf
, &saddr
->sin_addr
))
2717 if ((he
= gethostbyname(buf
)) == NULL
)
2719 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
2722 port
= strtol(p
, (char **)&r
, 0);
2725 saddr
->sin_port
= htons(port
);
2729 /* find or alloc a new VLAN */
2730 VLANState
*qemu_find_vlan(int id
)
2732 VLANState
**pvlan
, *vlan
;
2733 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2737 vlan
= qemu_mallocz(sizeof(VLANState
));
2742 pvlan
= &first_vlan
;
2743 while (*pvlan
!= NULL
)
2744 pvlan
= &(*pvlan
)->next
;
2749 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
2750 IOReadHandler
*fd_read
,
2751 IOCanRWHandler
*fd_can_read
,
2754 VLANClientState
*vc
, **pvc
;
2755 vc
= qemu_mallocz(sizeof(VLANClientState
));
2758 vc
->fd_read
= fd_read
;
2759 vc
->fd_can_read
= fd_can_read
;
2760 vc
->opaque
= opaque
;
2764 pvc
= &vlan
->first_client
;
2765 while (*pvc
!= NULL
)
2766 pvc
= &(*pvc
)->next
;
2771 int qemu_can_send_packet(VLANClientState
*vc1
)
2773 VLANState
*vlan
= vc1
->vlan
;
2774 VLANClientState
*vc
;
2776 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2778 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
2785 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
2787 VLANState
*vlan
= vc1
->vlan
;
2788 VLANClientState
*vc
;
2791 printf("vlan %d send:\n", vlan
->id
);
2792 hex_dump(stdout
, buf
, size
);
2794 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2796 vc
->fd_read(vc
->opaque
, buf
, size
);
2801 #if defined(CONFIG_SLIRP)
2803 /* slirp network adapter */
2805 static int slirp_inited
;
2806 static VLANClientState
*slirp_vc
;
2808 int slirp_can_output(void)
2810 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
2813 void slirp_output(const uint8_t *pkt
, int pkt_len
)
2816 printf("slirp output:\n");
2817 hex_dump(stdout
, pkt
, pkt_len
);
2821 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
2824 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
2827 printf("slirp input:\n");
2828 hex_dump(stdout
, buf
, size
);
2830 slirp_input(buf
, size
);
2833 static int net_slirp_init(VLANState
*vlan
)
2835 if (!slirp_inited
) {
2839 slirp_vc
= qemu_new_vlan_client(vlan
,
2840 slirp_receive
, NULL
, NULL
);
2841 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
2845 static void net_slirp_redir(const char *redir_str
)
2850 struct in_addr guest_addr
;
2851 int host_port
, guest_port
;
2853 if (!slirp_inited
) {
2859 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2861 if (!strcmp(buf
, "tcp")) {
2863 } else if (!strcmp(buf
, "udp")) {
2869 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2871 host_port
= strtol(buf
, &r
, 0);
2875 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2877 if (buf
[0] == '\0') {
2878 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
2880 if (!inet_aton(buf
, &guest_addr
))
2883 guest_port
= strtol(p
, &r
, 0);
2887 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
2888 fprintf(stderr
, "qemu: could not set up redirection\n");
2893 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
2901 static void smb_exit(void)
2905 char filename
[1024];
2907 /* erase all the files in the directory */
2908 d
= opendir(smb_dir
);
2913 if (strcmp(de
->d_name
, ".") != 0 &&
2914 strcmp(de
->d_name
, "..") != 0) {
2915 snprintf(filename
, sizeof(filename
), "%s/%s",
2916 smb_dir
, de
->d_name
);
2924 /* automatic user mode samba server configuration */
2925 void net_slirp_smb(const char *exported_dir
)
2927 char smb_conf
[1024];
2928 char smb_cmdline
[1024];
2931 if (!slirp_inited
) {
2936 /* XXX: better tmp dir construction */
2937 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
2938 if (mkdir(smb_dir
, 0700) < 0) {
2939 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
2942 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
2944 f
= fopen(smb_conf
, "w");
2946 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
2953 "socket address=127.0.0.1\n"
2954 "pid directory=%s\n"
2955 "lock directory=%s\n"
2956 "log file=%s/log.smbd\n"
2957 "smb passwd file=%s/smbpasswd\n"
2958 "security = share\n"
2973 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "/usr/sbin/smbd -s %s",
2976 slirp_add_exec(0, smb_cmdline
, 4, 139);
2979 #endif /* !defined(_WIN32) */
2981 #endif /* CONFIG_SLIRP */
2983 #if !defined(_WIN32)
2985 typedef struct TAPState
{
2986 VLANClientState
*vc
;
2990 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
2992 TAPState
*s
= opaque
;
2995 ret
= write(s
->fd
, buf
, size
);
2996 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3003 static void tap_send(void *opaque
)
3005 TAPState
*s
= opaque
;
3009 size
= read(s
->fd
, buf
, sizeof(buf
));
3011 qemu_send_packet(s
->vc
, buf
, size
);
3017 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3021 s
= qemu_mallocz(sizeof(TAPState
));
3025 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3026 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3027 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3032 static int tap_open(char *ifname
, int ifname_size
)
3038 fd
= open("/dev/tap", O_RDWR
);
3040 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3045 dev
= devname(s
.st_rdev
, S_IFCHR
);
3046 pstrcpy(ifname
, ifname_size
, dev
);
3048 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3051 #elif defined(__sun__)
3052 static int tap_open(char *ifname
, int ifname_size
)
3054 fprintf(stderr
, "warning: tap_open not yet implemented\n");
3058 static int tap_open(char *ifname
, int ifname_size
)
3063 fd
= open("/dev/net/tun", O_RDWR
);
3065 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
3068 memset(&ifr
, 0, sizeof(ifr
));
3069 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
3070 if (ifname
[0] != '\0')
3071 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
3073 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
3074 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
3076 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
3080 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
3081 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3086 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
3087 const char *setup_script
)
3090 int pid
, status
, fd
;
3095 if (ifname1
!= NULL
)
3096 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
3099 fd
= tap_open(ifname
, sizeof(ifname
));
3105 if (setup_script
[0] != '\0') {
3106 /* try to launch network init script */
3111 *parg
++ = (char *)setup_script
;
3114 execv(setup_script
, args
);
3117 while (waitpid(pid
, &status
, 0) != pid
);
3118 if (!WIFEXITED(status
) ||
3119 WEXITSTATUS(status
) != 0) {
3120 fprintf(stderr
, "%s: could not launch network script\n",
3126 s
= net_tap_fd_init(vlan
, fd
);
3129 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3130 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
3134 #endif /* !_WIN32 */
3136 /* network connection */
3137 typedef struct NetSocketState
{
3138 VLANClientState
*vc
;
3140 int state
; /* 0 = getting length, 1 = getting data */
3144 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
3147 typedef struct NetSocketListenState
{
3150 } NetSocketListenState
;
3152 /* XXX: we consider we can send the whole packet without blocking */
3153 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
3155 NetSocketState
*s
= opaque
;
3159 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
3160 send_all(s
->fd
, buf
, size
);
3163 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
3165 NetSocketState
*s
= opaque
;
3166 sendto(s
->fd
, buf
, size
, 0,
3167 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
3170 static void net_socket_send(void *opaque
)
3172 NetSocketState
*s
= opaque
;
3177 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
3179 err
= socket_error();
3180 if (err
!= EWOULDBLOCK
)
3182 } else if (size
== 0) {
3183 /* end of connection */
3185 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3191 /* reassemble a packet from the network */
3197 memcpy(s
->buf
+ s
->index
, buf
, l
);
3201 if (s
->index
== 4) {
3203 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
3209 l
= s
->packet_len
- s
->index
;
3212 memcpy(s
->buf
+ s
->index
, buf
, l
);
3216 if (s
->index
>= s
->packet_len
) {
3217 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
3226 static void net_socket_send_dgram(void *opaque
)
3228 NetSocketState
*s
= opaque
;
3231 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
3235 /* end of connection */
3236 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3239 qemu_send_packet(s
->vc
, s
->buf
, size
);
3242 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
3247 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
3248 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
3249 inet_ntoa(mcastaddr
->sin_addr
),
3250 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
3254 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
3256 perror("socket(PF_INET, SOCK_DGRAM)");
3261 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
3262 (const char *)&val
, sizeof(val
));
3264 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
3268 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
3274 /* Add host to multicast group */
3275 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
3276 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
3278 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
3279 (const char *)&imr
, sizeof(struct ip_mreq
));
3281 perror("setsockopt(IP_ADD_MEMBERSHIP)");
3285 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
3287 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
3288 (const char *)&val
, sizeof(val
));
3290 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
3294 socket_set_nonblock(fd
);
3302 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
3305 struct sockaddr_in saddr
;
3307 socklen_t saddr_len
;
3310 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
3311 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
3312 * by ONLY ONE process: we must "clone" this dgram socket --jjo
3316 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
3318 if (saddr
.sin_addr
.s_addr
==0) {
3319 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
3323 /* clone dgram socket */
3324 newfd
= net_socket_mcast_create(&saddr
);
3326 /* error already reported by net_socket_mcast_create() */
3330 /* clone newfd to fd, close newfd */
3335 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
3336 fd
, strerror(errno
));
3341 s
= qemu_mallocz(sizeof(NetSocketState
));
3346 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
3347 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
3349 /* mcast: save bound address as dst */
3350 if (is_connected
) s
->dgram_dst
=saddr
;
3352 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3353 "socket: fd=%d (%s mcast=%s:%d)",
3354 fd
, is_connected
? "cloned" : "",
3355 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3359 static void net_socket_connect(void *opaque
)
3361 NetSocketState
*s
= opaque
;
3362 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
3365 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
3369 s
= qemu_mallocz(sizeof(NetSocketState
));
3373 s
->vc
= qemu_new_vlan_client(vlan
,
3374 net_socket_receive
, NULL
, s
);
3375 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3376 "socket: fd=%d", fd
);
3378 net_socket_connect(s
);
3380 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
3385 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
3388 int so_type
=-1, optlen
=sizeof(so_type
);
3390 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
3391 fprintf(stderr
, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd
);
3396 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
3398 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3400 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
3401 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
3402 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3407 static void net_socket_accept(void *opaque
)
3409 NetSocketListenState
*s
= opaque
;
3411 struct sockaddr_in saddr
;
3416 len
= sizeof(saddr
);
3417 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
3418 if (fd
< 0 && errno
!= EINTR
) {
3420 } else if (fd
>= 0) {
3424 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
3428 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
3429 "socket: connection from %s:%d",
3430 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3434 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
3436 NetSocketListenState
*s
;
3438 struct sockaddr_in saddr
;
3440 if (parse_host_port(&saddr
, host_str
) < 0)
3443 s
= qemu_mallocz(sizeof(NetSocketListenState
));
3447 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3452 socket_set_nonblock(fd
);
3454 /* allow fast reuse */
3456 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3458 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3463 ret
= listen(fd
, 0);
3470 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
3474 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
3477 int fd
, connected
, ret
, err
;
3478 struct sockaddr_in saddr
;
3480 if (parse_host_port(&saddr
, host_str
) < 0)
3483 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3488 socket_set_nonblock(fd
);
3492 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3494 err
= socket_error();
3495 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3496 } else if (err
== EINPROGRESS
) {
3508 s
= net_socket_fd_init(vlan
, fd
, connected
);
3511 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3512 "socket: connect to %s:%d",
3513 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3517 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
3521 struct sockaddr_in saddr
;
3523 if (parse_host_port(&saddr
, host_str
) < 0)
3527 fd
= net_socket_mcast_create(&saddr
);
3531 s
= net_socket_fd_init(vlan
, fd
, 0);
3535 s
->dgram_dst
= saddr
;
3537 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3538 "socket: mcast=%s:%d",
3539 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3544 static int get_param_value(char *buf
, int buf_size
,
3545 const char *tag
, const char *str
)
3554 while (*p
!= '\0' && *p
!= '=') {
3555 if ((q
- option
) < sizeof(option
) - 1)
3563 if (!strcmp(tag
, option
)) {
3565 while (*p
!= '\0' && *p
!= ',') {
3566 if ((q
- buf
) < buf_size
- 1)
3573 while (*p
!= '\0' && *p
!= ',') {
3584 int net_client_init(const char *str
)
3595 while (*p
!= '\0' && *p
!= ',') {
3596 if ((q
- device
) < sizeof(device
) - 1)
3604 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
3605 vlan_id
= strtol(buf
, NULL
, 0);
3607 vlan
= qemu_find_vlan(vlan_id
);
3609 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
3612 if (!strcmp(device
, "nic")) {
3616 if (nb_nics
>= MAX_NICS
) {
3617 fprintf(stderr
, "Too Many NICs\n");
3620 nd
= &nd_table
[nb_nics
];
3621 macaddr
= nd
->macaddr
;
3627 macaddr
[5] = 0x56 + nb_nics
;
3629 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
3630 if (parse_macaddr(macaddr
, buf
) < 0) {
3631 fprintf(stderr
, "invalid syntax for ethernet address\n");
3635 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
3636 nd
->model
= strdup(buf
);
3642 if (!strcmp(device
, "none")) {
3643 /* does nothing. It is needed to signal that no network cards
3648 if (!strcmp(device
, "user")) {
3649 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
3650 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
3652 ret
= net_slirp_init(vlan
);
3656 if (!strcmp(device
, "tap")) {
3658 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
3659 fprintf(stderr
, "tap: no interface name\n");
3662 ret
= tap_win32_init(vlan
, ifname
);
3665 if (!strcmp(device
, "tap")) {
3667 char setup_script
[1024];
3669 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3670 fd
= strtol(buf
, NULL
, 0);
3672 if (net_tap_fd_init(vlan
, fd
))
3675 get_param_value(ifname
, sizeof(ifname
), "ifname", p
);
3676 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
3677 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
3679 ret
= net_tap_init(vlan
, ifname
, setup_script
);
3683 if (!strcmp(device
, "socket")) {
3684 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3686 fd
= strtol(buf
, NULL
, 0);
3688 if (net_socket_fd_init(vlan
, fd
, 1))
3690 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
3691 ret
= net_socket_listen_init(vlan
, buf
);
3692 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
3693 ret
= net_socket_connect_init(vlan
, buf
);
3694 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
3695 ret
= net_socket_mcast_init(vlan
, buf
);
3697 fprintf(stderr
, "Unknown socket options: %s\n", p
);
3702 fprintf(stderr
, "Unknown network device: %s\n", device
);
3706 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
3712 void do_info_network(void)
3715 VLANClientState
*vc
;
3717 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3718 term_printf("VLAN %d devices:\n", vlan
->id
);
3719 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3720 term_printf(" %s\n", vc
->info_str
);
3724 /***********************************************************/
3727 static USBPort
*used_usb_ports
;
3728 static USBPort
*free_usb_ports
;
3730 /* ??? Maybe change this to register a hub to keep track of the topology. */
3731 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
3732 usb_attachfn attach
)
3734 port
->opaque
= opaque
;
3735 port
->index
= index
;
3736 port
->attach
= attach
;
3737 port
->next
= free_usb_ports
;
3738 free_usb_ports
= port
;
3741 static int usb_device_add(const char *devname
)
3747 if (!free_usb_ports
)
3750 if (strstart(devname
, "host:", &p
)) {
3751 dev
= usb_host_device_open(p
);
3752 } else if (!strcmp(devname
, "mouse")) {
3753 dev
= usb_mouse_init();
3754 } else if (!strcmp(devname
, "tablet")) {
3755 dev
= usb_tablet_init();
3756 } else if (strstart(devname
, "disk:", &p
)) {
3757 dev
= usb_msd_init(p
);
3764 /* Find a USB port to add the device to. */
3765 port
= free_usb_ports
;
3769 /* Create a new hub and chain it on. */
3770 free_usb_ports
= NULL
;
3771 port
->next
= used_usb_ports
;
3772 used_usb_ports
= port
;
3774 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
3775 usb_attach(port
, hub
);
3776 port
= free_usb_ports
;
3779 free_usb_ports
= port
->next
;
3780 port
->next
= used_usb_ports
;
3781 used_usb_ports
= port
;
3782 usb_attach(port
, dev
);
3786 static int usb_device_del(const char *devname
)
3794 if (!used_usb_ports
)
3797 p
= strchr(devname
, '.');
3800 bus_num
= strtoul(devname
, NULL
, 0);
3801 addr
= strtoul(p
+ 1, NULL
, 0);
3805 lastp
= &used_usb_ports
;
3806 port
= used_usb_ports
;
3807 while (port
&& port
->dev
->addr
!= addr
) {
3808 lastp
= &port
->next
;
3816 *lastp
= port
->next
;
3817 usb_attach(port
, NULL
);
3818 dev
->handle_destroy(dev
);
3819 port
->next
= free_usb_ports
;
3820 free_usb_ports
= port
;
3824 void do_usb_add(const char *devname
)
3827 ret
= usb_device_add(devname
);
3829 term_printf("Could not add USB device '%s'\n", devname
);
3832 void do_usb_del(const char *devname
)
3835 ret
= usb_device_del(devname
);
3837 term_printf("Could not remove USB device '%s'\n", devname
);
3844 const char *speed_str
;
3847 term_printf("USB support not enabled\n");
3851 for (port
= used_usb_ports
; port
; port
= port
->next
) {
3855 switch(dev
->speed
) {
3859 case USB_SPEED_FULL
:
3862 case USB_SPEED_HIGH
:
3869 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
3870 0, dev
->addr
, speed_str
, dev
->devname
);
3874 /***********************************************************/
3877 static char *pid_filename
;
3879 /* Remove PID file. Called on normal exit */
3881 static void remove_pidfile(void)
3883 unlink (pid_filename
);
3886 static void create_pidfile(const char *filename
)
3888 struct stat pidstat
;
3891 /* Try to write our PID to the named file */
3892 if (stat(filename
, &pidstat
) < 0) {
3893 if (errno
== ENOENT
) {
3894 if ((f
= fopen (filename
, "w")) == NULL
) {
3895 perror("Opening pidfile");
3898 fprintf(f
, "%d\n", getpid());
3900 pid_filename
= qemu_strdup(filename
);
3901 if (!pid_filename
) {
3902 fprintf(stderr
, "Could not save PID filename");
3905 atexit(remove_pidfile
);
3908 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
3914 /***********************************************************/
3917 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
3921 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
3925 static void dumb_refresh(DisplayState
*ds
)
3930 void dumb_display_init(DisplayState
*ds
)
3935 ds
->dpy_update
= dumb_update
;
3936 ds
->dpy_resize
= dumb_resize
;
3937 ds
->dpy_refresh
= dumb_refresh
;
3940 /***********************************************************/
3943 #define MAX_IO_HANDLERS 64
3945 typedef struct IOHandlerRecord
{
3947 IOCanRWHandler
*fd_read_poll
;
3949 IOHandler
*fd_write
;
3951 /* temporary data */
3953 struct IOHandlerRecord
*next
;
3956 static IOHandlerRecord
*first_io_handler
;
3958 /* XXX: fd_read_poll should be suppressed, but an API change is
3959 necessary in the character devices to suppress fd_can_read(). */
3960 int qemu_set_fd_handler2(int fd
,
3961 IOCanRWHandler
*fd_read_poll
,
3963 IOHandler
*fd_write
,
3966 IOHandlerRecord
**pioh
, *ioh
;
3968 if (!fd_read
&& !fd_write
) {
3969 pioh
= &first_io_handler
;
3974 if (ioh
->fd
== fd
) {
3982 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3986 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
3989 ioh
->next
= first_io_handler
;
3990 first_io_handler
= ioh
;
3993 ioh
->fd_read_poll
= fd_read_poll
;
3994 ioh
->fd_read
= fd_read
;
3995 ioh
->fd_write
= fd_write
;
3996 ioh
->opaque
= opaque
;
4001 int qemu_set_fd_handler(int fd
,
4003 IOHandler
*fd_write
,
4006 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4009 /***********************************************************/
4010 /* Polling handling */
4012 typedef struct PollingEntry
{
4015 struct PollingEntry
*next
;
4018 static PollingEntry
*first_polling_entry
;
4020 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4022 PollingEntry
**ppe
, *pe
;
4023 pe
= qemu_mallocz(sizeof(PollingEntry
));
4027 pe
->opaque
= opaque
;
4028 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
4033 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
4035 PollingEntry
**ppe
, *pe
;
4036 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
4038 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
4047 /***********************************************************/
4048 /* Wait objects support */
4049 typedef struct WaitObjects
{
4051 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
4052 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
4053 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
4056 static WaitObjects wait_objects
= {0};
4058 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4060 WaitObjects
*w
= &wait_objects
;
4062 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
4064 w
->events
[w
->num
] = handle
;
4065 w
->func
[w
->num
] = func
;
4066 w
->opaque
[w
->num
] = opaque
;
4071 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4074 WaitObjects
*w
= &wait_objects
;
4077 for (i
= 0; i
< w
->num
; i
++) {
4078 if (w
->events
[i
] == handle
)
4081 w
->events
[i
] = w
->events
[i
+ 1];
4082 w
->func
[i
] = w
->func
[i
+ 1];
4083 w
->opaque
[i
] = w
->opaque
[i
+ 1];
4091 /***********************************************************/
4092 /* savevm/loadvm support */
4094 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
4096 fwrite(buf
, 1, size
, f
);
4099 void qemu_put_byte(QEMUFile
*f
, int v
)
4104 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
4106 qemu_put_byte(f
, v
>> 8);
4107 qemu_put_byte(f
, v
);
4110 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
4112 qemu_put_byte(f
, v
>> 24);
4113 qemu_put_byte(f
, v
>> 16);
4114 qemu_put_byte(f
, v
>> 8);
4115 qemu_put_byte(f
, v
);
4118 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
4120 qemu_put_be32(f
, v
>> 32);
4121 qemu_put_be32(f
, v
);
4124 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
4126 return fread(buf
, 1, size
, f
);
4129 int qemu_get_byte(QEMUFile
*f
)
4139 unsigned int qemu_get_be16(QEMUFile
*f
)
4142 v
= qemu_get_byte(f
) << 8;
4143 v
|= qemu_get_byte(f
);
4147 unsigned int qemu_get_be32(QEMUFile
*f
)
4150 v
= qemu_get_byte(f
) << 24;
4151 v
|= qemu_get_byte(f
) << 16;
4152 v
|= qemu_get_byte(f
) << 8;
4153 v
|= qemu_get_byte(f
);
4157 uint64_t qemu_get_be64(QEMUFile
*f
)
4160 v
= (uint64_t)qemu_get_be32(f
) << 32;
4161 v
|= qemu_get_be32(f
);
4165 int64_t qemu_ftell(QEMUFile
*f
)
4170 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
4172 if (fseek(f
, pos
, whence
) < 0)
4177 typedef struct SaveStateEntry
{
4181 SaveStateHandler
*save_state
;
4182 LoadStateHandler
*load_state
;
4184 struct SaveStateEntry
*next
;
4187 static SaveStateEntry
*first_se
;
4189 int register_savevm(const char *idstr
,
4192 SaveStateHandler
*save_state
,
4193 LoadStateHandler
*load_state
,
4196 SaveStateEntry
*se
, **pse
;
4198 se
= qemu_malloc(sizeof(SaveStateEntry
));
4201 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
4202 se
->instance_id
= instance_id
;
4203 se
->version_id
= version_id
;
4204 se
->save_state
= save_state
;
4205 se
->load_state
= load_state
;
4206 se
->opaque
= opaque
;
4209 /* add at the end of list */
4211 while (*pse
!= NULL
)
4212 pse
= &(*pse
)->next
;
4217 #define QEMU_VM_FILE_MAGIC 0x5145564d
4218 #define QEMU_VM_FILE_VERSION 0x00000001
4220 int qemu_savevm(const char *filename
)
4224 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
4226 saved_vm_running
= vm_running
;
4229 f
= fopen(filename
, "wb");
4235 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
4236 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
4238 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4240 len
= strlen(se
->idstr
);
4241 qemu_put_byte(f
, len
);
4242 qemu_put_buffer(f
, se
->idstr
, len
);
4244 qemu_put_be32(f
, se
->instance_id
);
4245 qemu_put_be32(f
, se
->version_id
);
4247 /* record size: filled later */
4249 qemu_put_be32(f
, 0);
4251 se
->save_state(f
, se
->opaque
);
4253 /* fill record size */
4255 len
= ftell(f
) - len_pos
- 4;
4256 fseek(f
, len_pos
, SEEK_SET
);
4257 qemu_put_be32(f
, len
);
4258 fseek(f
, cur_pos
, SEEK_SET
);
4264 if (saved_vm_running
)
4269 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
4273 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4274 if (!strcmp(se
->idstr
, idstr
) &&
4275 instance_id
== se
->instance_id
)
4281 int qemu_loadvm(const char *filename
)
4285 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
4286 int saved_vm_running
;
4290 saved_vm_running
= vm_running
;
4293 f
= fopen(filename
, "rb");
4299 v
= qemu_get_be32(f
);
4300 if (v
!= QEMU_VM_FILE_MAGIC
)
4302 v
= qemu_get_be32(f
);
4303 if (v
!= QEMU_VM_FILE_VERSION
) {
4310 len
= qemu_get_byte(f
);
4313 qemu_get_buffer(f
, idstr
, len
);
4315 instance_id
= qemu_get_be32(f
);
4316 version_id
= qemu_get_be32(f
);
4317 record_len
= qemu_get_be32(f
);
4319 printf("idstr=%s instance=0x%x version=%d len=%d\n",
4320 idstr
, instance_id
, version_id
, record_len
);
4323 se
= find_se(idstr
, instance_id
);
4325 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
4326 instance_id
, idstr
);
4328 ret
= se
->load_state(f
, se
->opaque
, version_id
);
4330 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
4331 instance_id
, idstr
);
4334 /* always seek to exact end of record */
4335 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
4340 if (saved_vm_running
)
4345 /***********************************************************/
4346 /* cpu save/restore */
4348 #if defined(TARGET_I386)
4350 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
4352 qemu_put_be32(f
, dt
->selector
);
4353 qemu_put_betl(f
, dt
->base
);
4354 qemu_put_be32(f
, dt
->limit
);
4355 qemu_put_be32(f
, dt
->flags
);
4358 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
4360 dt
->selector
= qemu_get_be32(f
);
4361 dt
->base
= qemu_get_betl(f
);
4362 dt
->limit
= qemu_get_be32(f
);
4363 dt
->flags
= qemu_get_be32(f
);
4366 void cpu_save(QEMUFile
*f
, void *opaque
)
4368 CPUState
*env
= opaque
;
4369 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
4373 for(i
= 0; i
< CPU_NB_REGS
; i
++)
4374 qemu_put_betls(f
, &env
->regs
[i
]);
4375 qemu_put_betls(f
, &env
->eip
);
4376 qemu_put_betls(f
, &env
->eflags
);
4377 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
4378 qemu_put_be32s(f
, &hflags
);
4382 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
4384 for(i
= 0; i
< 8; i
++) {
4385 fptag
|= ((!env
->fptags
[i
]) << i
);
4388 qemu_put_be16s(f
, &fpuc
);
4389 qemu_put_be16s(f
, &fpus
);
4390 qemu_put_be16s(f
, &fptag
);
4392 #ifdef USE_X86LDOUBLE
4397 qemu_put_be16s(f
, &fpregs_format
);
4399 for(i
= 0; i
< 8; i
++) {
4400 #ifdef USE_X86LDOUBLE
4404 /* we save the real CPU data (in case of MMX usage only 'mant'
4405 contains the MMX register */
4406 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
4407 qemu_put_be64(f
, mant
);
4408 qemu_put_be16(f
, exp
);
4411 /* if we use doubles for float emulation, we save the doubles to
4412 avoid losing information in case of MMX usage. It can give
4413 problems if the image is restored on a CPU where long
4414 doubles are used instead. */
4415 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
4419 for(i
= 0; i
< 6; i
++)
4420 cpu_put_seg(f
, &env
->segs
[i
]);
4421 cpu_put_seg(f
, &env
->ldt
);
4422 cpu_put_seg(f
, &env
->tr
);
4423 cpu_put_seg(f
, &env
->gdt
);
4424 cpu_put_seg(f
, &env
->idt
);
4426 qemu_put_be32s(f
, &env
->sysenter_cs
);
4427 qemu_put_be32s(f
, &env
->sysenter_esp
);
4428 qemu_put_be32s(f
, &env
->sysenter_eip
);
4430 qemu_put_betls(f
, &env
->cr
[0]);
4431 qemu_put_betls(f
, &env
->cr
[2]);
4432 qemu_put_betls(f
, &env
->cr
[3]);
4433 qemu_put_betls(f
, &env
->cr
[4]);
4435 for(i
= 0; i
< 8; i
++)
4436 qemu_put_betls(f
, &env
->dr
[i
]);
4439 qemu_put_be32s(f
, &env
->a20_mask
);
4442 qemu_put_be32s(f
, &env
->mxcsr
);
4443 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
4444 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
4445 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
4448 #ifdef TARGET_X86_64
4449 qemu_put_be64s(f
, &env
->efer
);
4450 qemu_put_be64s(f
, &env
->star
);
4451 qemu_put_be64s(f
, &env
->lstar
);
4452 qemu_put_be64s(f
, &env
->cstar
);
4453 qemu_put_be64s(f
, &env
->fmask
);
4454 qemu_put_be64s(f
, &env
->kernelgsbase
);
4458 qemu_put_be32s(f
, &env
->kvm_interrupt_summary
);
4459 for (i
= 0; i
< NR_IRQ_WORDS
; i
++) {
4460 qemu_put_betls(f
, &env
->kvm_interrupt_bitmap
[i
]);
4465 #ifdef USE_X86LDOUBLE
4466 /* XXX: add that in a FPU generic layer */
4467 union x86_longdouble
{
4472 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
4473 #define EXPBIAS1 1023
4474 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
4475 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
4477 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
4481 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
4482 /* exponent + sign */
4483 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
4484 e
|= SIGND1(temp
) >> 16;
4489 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4491 CPUState
*env
= opaque
;
4494 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
4496 if (version_id
!= 3)
4498 for(i
= 0; i
< CPU_NB_REGS
; i
++)
4499 qemu_get_betls(f
, &env
->regs
[i
]);
4500 qemu_get_betls(f
, &env
->eip
);
4501 qemu_get_betls(f
, &env
->eflags
);
4502 qemu_get_be32s(f
, &hflags
);
4504 qemu_get_be16s(f
, &fpuc
);
4505 qemu_get_be16s(f
, &fpus
);
4506 qemu_get_be16s(f
, &fptag
);
4507 qemu_get_be16s(f
, &fpregs_format
);
4509 /* NOTE: we cannot always restore the FPU state if the image come
4510 from a host with a different 'USE_X86LDOUBLE' define. We guess
4511 if we are in an MMX state to restore correctly in that case. */
4512 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
4513 for(i
= 0; i
< 8; i
++) {
4517 switch(fpregs_format
) {
4519 mant
= qemu_get_be64(f
);
4520 exp
= qemu_get_be16(f
);
4521 #ifdef USE_X86LDOUBLE
4522 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
4524 /* difficult case */
4526 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
4528 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
4532 mant
= qemu_get_be64(f
);
4533 #ifdef USE_X86LDOUBLE
4535 union x86_longdouble
*p
;
4536 /* difficult case */
4537 p
= (void *)&env
->fpregs
[i
];
4542 fp64_to_fp80(p
, mant
);
4546 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
4555 /* XXX: restore FPU round state */
4556 env
->fpstt
= (fpus
>> 11) & 7;
4557 env
->fpus
= fpus
& ~0x3800;
4559 for(i
= 0; i
< 8; i
++) {
4560 env
->fptags
[i
] = (fptag
>> i
) & 1;
4563 for(i
= 0; i
< 6; i
++)
4564 cpu_get_seg(f
, &env
->segs
[i
]);
4565 cpu_get_seg(f
, &env
->ldt
);
4566 cpu_get_seg(f
, &env
->tr
);
4567 cpu_get_seg(f
, &env
->gdt
);
4568 cpu_get_seg(f
, &env
->idt
);
4570 qemu_get_be32s(f
, &env
->sysenter_cs
);
4571 qemu_get_be32s(f
, &env
->sysenter_esp
);
4572 qemu_get_be32s(f
, &env
->sysenter_eip
);
4574 qemu_get_betls(f
, &env
->cr
[0]);
4575 qemu_get_betls(f
, &env
->cr
[2]);
4576 qemu_get_betls(f
, &env
->cr
[3]);
4577 qemu_get_betls(f
, &env
->cr
[4]);
4579 for(i
= 0; i
< 8; i
++)
4580 qemu_get_betls(f
, &env
->dr
[i
]);
4583 qemu_get_be32s(f
, &env
->a20_mask
);
4585 qemu_get_be32s(f
, &env
->mxcsr
);
4586 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
4587 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
4588 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
4591 #ifdef TARGET_X86_64
4592 qemu_get_be64s(f
, &env
->efer
);
4593 qemu_get_be64s(f
, &env
->star
);
4594 qemu_get_be64s(f
, &env
->lstar
);
4595 qemu_get_be64s(f
, &env
->cstar
);
4596 qemu_get_be64s(f
, &env
->fmask
);
4597 qemu_get_be64s(f
, &env
->kernelgsbase
);
4600 /* XXX: compute hflags from scratch, except for CPL and IIF */
4601 env
->hflags
= hflags
;
4604 qemu_get_be32s(f
, &env
->kvm_interrupt_summary
);
4605 for (i
= 0; i
< NR_IRQ_WORDS
; i
++) {
4606 qemu_get_betls(f
, &env
->kvm_interrupt_bitmap
[i
]);
4608 kvm_load_registers(env
);
4613 #elif defined(TARGET_PPC)
4614 void cpu_save(QEMUFile
*f
, void *opaque
)
4618 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4623 #elif defined(TARGET_MIPS)
4624 void cpu_save(QEMUFile
*f
, void *opaque
)
4628 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4633 #elif defined(TARGET_SPARC)
4634 void cpu_save(QEMUFile
*f
, void *opaque
)
4636 CPUState
*env
= opaque
;
4640 for(i
= 0; i
< 8; i
++)
4641 qemu_put_betls(f
, &env
->gregs
[i
]);
4642 for(i
= 0; i
< NWINDOWS
* 16; i
++)
4643 qemu_put_betls(f
, &env
->regbase
[i
]);
4646 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
4652 qemu_put_be32(f
, u
.i
);
4655 qemu_put_betls(f
, &env
->pc
);
4656 qemu_put_betls(f
, &env
->npc
);
4657 qemu_put_betls(f
, &env
->y
);
4659 qemu_put_be32(f
, tmp
);
4660 qemu_put_betls(f
, &env
->fsr
);
4661 qemu_put_betls(f
, &env
->tbr
);
4662 #ifndef TARGET_SPARC64
4663 qemu_put_be32s(f
, &env
->wim
);
4665 for(i
= 0; i
< 16; i
++)
4666 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
4670 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4672 CPUState
*env
= opaque
;
4676 for(i
= 0; i
< 8; i
++)
4677 qemu_get_betls(f
, &env
->gregs
[i
]);
4678 for(i
= 0; i
< NWINDOWS
* 16; i
++)
4679 qemu_get_betls(f
, &env
->regbase
[i
]);
4682 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
4687 u
.i
= qemu_get_be32(f
);
4691 qemu_get_betls(f
, &env
->pc
);
4692 qemu_get_betls(f
, &env
->npc
);
4693 qemu_get_betls(f
, &env
->y
);
4694 tmp
= qemu_get_be32(f
);
4695 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
4696 correctly updated */
4698 qemu_get_betls(f
, &env
->fsr
);
4699 qemu_get_betls(f
, &env
->tbr
);
4700 #ifndef TARGET_SPARC64
4701 qemu_get_be32s(f
, &env
->wim
);
4703 for(i
= 0; i
< 16; i
++)
4704 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
4710 #elif defined(TARGET_ARM)
4712 /* ??? Need to implement these. */
4713 void cpu_save(QEMUFile
*f
, void *opaque
)
4717 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4724 #warning No CPU save/restore functions
4728 /***********************************************************/
4729 /* ram save/restore */
4731 /* we just avoid storing empty pages */
4732 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
4737 for(i
= 1; i
< len
; i
++) {
4741 qemu_put_byte(f
, 1);
4742 qemu_put_byte(f
, v
);
4745 qemu_put_byte(f
, 0);
4746 qemu_put_buffer(f
, buf
, len
);
4749 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
4753 v
= qemu_get_byte(f
);
4756 if (qemu_get_buffer(f
, buf
, len
) != len
)
4760 v
= qemu_get_byte(f
);
4761 memset(buf
, v
, len
);
4769 static void ram_save(QEMUFile
*f
, void *opaque
)
4772 qemu_put_be32(f
, phys_ram_size
);
4773 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
4775 if ((i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
4778 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
4782 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
4786 if (version_id
!= 1)
4788 if (qemu_get_be32(f
) != phys_ram_size
)
4790 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
4792 if ((i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
4795 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
4802 /***********************************************************/
4803 /* machine registration */
4805 QEMUMachine
*first_machine
= NULL
;
4807 int qemu_register_machine(QEMUMachine
*m
)
4810 pm
= &first_machine
;
4818 QEMUMachine
*find_machine(const char *name
)
4822 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4823 if (!strcmp(m
->name
, name
))
4829 /***********************************************************/
4830 /* main execution loop */
4832 void gui_update(void *opaque
)
4834 display_state
.dpy_refresh(&display_state
);
4835 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
4838 struct vm_change_state_entry
{
4839 VMChangeStateHandler
*cb
;
4841 LIST_ENTRY (vm_change_state_entry
) entries
;
4844 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
4846 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
4849 VMChangeStateEntry
*e
;
4851 e
= qemu_mallocz(sizeof (*e
));
4857 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
4861 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
4863 LIST_REMOVE (e
, entries
);
4867 static void vm_state_notify(int running
)
4869 VMChangeStateEntry
*e
;
4871 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
4872 e
->cb(e
->opaque
, running
);
4876 /* XXX: support several handlers */
4877 static VMStopHandler
*vm_stop_cb
;
4878 static void *vm_stop_opaque
;
4880 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4883 vm_stop_opaque
= opaque
;
4887 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4901 void vm_stop(int reason
)
4904 cpu_disable_ticks();
4908 vm_stop_cb(vm_stop_opaque
, reason
);
4915 /* reset/shutdown handler */
4917 typedef struct QEMUResetEntry
{
4918 QEMUResetHandler
*func
;
4920 struct QEMUResetEntry
*next
;
4923 static QEMUResetEntry
*first_reset_entry
;
4924 static int reset_requested
;
4925 static int shutdown_requested
;
4926 static int powerdown_requested
;
4928 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
4930 QEMUResetEntry
**pre
, *re
;
4932 pre
= &first_reset_entry
;
4933 while (*pre
!= NULL
)
4934 pre
= &(*pre
)->next
;
4935 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
4937 re
->opaque
= opaque
;
4942 void qemu_system_reset(void)
4946 /* reset all devices */
4947 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
4948 re
->func(re
->opaque
);
4952 void qemu_system_reset_request(void)
4954 reset_requested
= 1;
4956 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4959 void qemu_system_shutdown_request(void)
4961 shutdown_requested
= 1;
4963 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4966 void qemu_system_powerdown_request(void)
4968 powerdown_requested
= 1;
4970 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4973 void main_loop_wait(int timeout
)
4975 IOHandlerRecord
*ioh
, *ioh_next
;
4976 fd_set rfds
, wfds
, xfds
;
4982 /* XXX: need to suppress polling by better using win32 events */
4984 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4985 ret
|= pe
->func(pe
->opaque
);
4988 if (ret
== 0 && timeout
> 0) {
4990 WaitObjects
*w
= &wait_objects
;
4992 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
4993 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
4994 if (w
->func
[ret
- WAIT_OBJECT_0
])
4995 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
4996 } else if (ret
== WAIT_TIMEOUT
) {
4998 err
= GetLastError();
4999 fprintf(stderr
, "Wait error %d %d\n", ret
, err
);
5003 /* poll any events */
5004 /* XXX: separate device handlers from system ones */
5009 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
5011 (!ioh
->fd_read_poll
||
5012 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
5013 FD_SET(ioh
->fd
, &rfds
);
5017 if (ioh
->fd_write
) {
5018 FD_SET(ioh
->fd
, &wfds
);
5028 tv
.tv_usec
= timeout
* 1000;
5030 #if defined(CONFIG_SLIRP)
5032 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
5035 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
5037 /* XXX: better handling of removal */
5038 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
5039 ioh_next
= ioh
->next
;
5040 if (FD_ISSET(ioh
->fd
, &rfds
)) {
5041 ioh
->fd_read(ioh
->opaque
);
5043 if (FD_ISSET(ioh
->fd
, &wfds
)) {
5044 ioh
->fd_write(ioh
->opaque
);
5048 #if defined(CONFIG_SLIRP)
5055 slirp_select_poll(&rfds
, &wfds
, &xfds
);
5063 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
5064 qemu_get_clock(vm_clock
));
5065 /* run dma transfers, if any */
5069 /* real time timers */
5070 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
5071 qemu_get_clock(rt_clock
));
5074 static CPUState
*cur_cpu
;
5079 #ifdef CONFIG_PROFILER
5084 cur_cpu
= first_cpu
;
5091 env
= env
->next_cpu
;
5094 #ifdef CONFIG_PROFILER
5095 ti
= profile_getclock();
5097 ret
= cpu_exec(env
);
5098 #ifdef CONFIG_PROFILER
5099 qemu_time
+= profile_getclock() - ti
;
5101 if (ret
!= EXCP_HALTED
)
5103 /* all CPUs are halted ? */
5104 if (env
== cur_cpu
) {
5111 if (shutdown_requested
) {
5112 ret
= EXCP_INTERRUPT
;
5115 if (reset_requested
) {
5116 reset_requested
= 0;
5117 qemu_system_reset();
5118 ret
= EXCP_INTERRUPT
;
5120 if (powerdown_requested
) {
5121 powerdown_requested
= 0;
5122 qemu_system_powerdown();
5123 ret
= EXCP_INTERRUPT
;
5125 if (ret
== EXCP_DEBUG
) {
5126 vm_stop(EXCP_DEBUG
);
5128 /* if hlt instruction, we wait until the next IRQ */
5129 /* XXX: use timeout computed from timers */
5130 if (ret
== EXCP_HLT
)
5137 #ifdef CONFIG_PROFILER
5138 ti
= profile_getclock();
5140 main_loop_wait(timeout
);
5141 #ifdef CONFIG_PROFILER
5142 dev_time
+= profile_getclock() - ti
;
5145 cpu_disable_ticks();
5151 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2005 Fabrice Bellard\n"
5152 "usage: %s [options] [disk_image]\n"
5154 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
5156 "Standard options:\n"
5157 "-M machine select emulated machine (-M ? for list)\n"
5158 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
5159 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
5160 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
5161 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
5162 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
5163 "-snapshot write to temporary files instead of disk image files\n"
5165 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
5167 "-m megs set virtual RAM size to megs MB [default=%d]\n"
5168 "-smp n set the number of CPUs to 'n' [default=1]\n"
5169 "-nographic disable graphical output and redirect serial I/Os to console\n"
5171 "-k language use keyboard layout (for example \"fr\" for French)\n"
5174 "-audio-help print list of audio drivers and their options\n"
5175 "-soundhw c1,... enable audio support\n"
5176 " and only specified sound cards (comma separated list)\n"
5177 " use -soundhw ? to get the list of supported cards\n"
5178 " use -soundhw all to enable all of them\n"
5180 "-localtime set the real time clock to local time [default=utc]\n"
5181 "-full-screen start in full screen\n"
5183 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
5185 "-usb enable the USB driver (will be the default soon)\n"
5186 "-usbdevice name add the host or guest USB device 'name'\n"
5187 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5188 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
5191 "Network options:\n"
5192 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
5193 " create a new Network Interface Card and connect it to VLAN 'n'\n"
5195 "-net user[,vlan=n][,hostname=host]\n"
5196 " connect the user mode network stack to VLAN 'n' and send\n"
5197 " hostname 'host' to DHCP clients\n"
5200 "-net tap[,vlan=n],ifname=name\n"
5201 " connect the host TAP network interface to VLAN 'n'\n"
5203 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
5204 " connect the host TAP network interface to VLAN 'n' and use\n"
5205 " the network script 'file' (default=%s);\n"
5206 " use 'fd=h' to connect to an already opened TAP interface\n"
5208 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
5209 " connect the vlan 'n' to another VLAN using a socket connection\n"
5210 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
5211 " connect the vlan 'n' to multicast maddr and port\n"
5212 "-net none use it alone to have zero network devices; if no -net option\n"
5213 " is provided, the default is '-net nic -net user'\n"
5216 "-tftp prefix allow tftp access to files starting with prefix [-net user]\n"
5218 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
5220 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
5221 " redirect TCP or UDP connections from host to guest [-net user]\n"
5224 "Linux boot specific:\n"
5225 "-kernel bzImage use 'bzImage' as kernel image\n"
5226 "-append cmdline use 'cmdline' as kernel command line\n"
5227 "-initrd file use 'file' as initial ram disk\n"
5229 "Debug/Expert options:\n"
5230 "-monitor dev redirect the monitor to char device 'dev'\n"
5231 "-serial dev redirect the serial port to char device 'dev'\n"
5232 "-parallel dev redirect the parallel port to char device 'dev'\n"
5233 "-pidfile file Write PID to 'file'\n"
5234 "-S freeze CPU at startup (use 'c' to start execution)\n"
5235 "-s wait gdb connection to port %d\n"
5236 "-p port change gdb connection port\n"
5237 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
5238 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
5239 " translation (t=none or lba) (usually qemu can guess them)\n"
5240 "-L path set the directory for the BIOS and VGA BIOS\n"
5242 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
5243 "-no-kqemu disable KQEMU kernel module usage\n"
5245 #ifdef USE_CODE_COPY
5246 "-no-code-copy disable code copy acceleration\n"
5249 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
5250 " (default is CL-GD5446 PCI VGA)\n"
5251 "-no-acpi disable ACPI\n"
5253 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
5254 "-vnc display start a VNC server on display\n"
5256 "During emulation, the following keys are useful:\n"
5257 "ctrl-alt-f toggle full screen\n"
5258 "ctrl-alt-n switch to virtual console 'n'\n"
5259 "ctrl-alt toggle mouse and keyboard grab\n"
5261 "When using -nographic, press 'ctrl-a h' to get some help.\n"
5266 DEFAULT_NETWORK_SCRIPT
,
5268 DEFAULT_GDBSTUB_PORT
,
5273 #define HAS_ARG 0x0001
5287 QEMU_OPTION_snapshot
,
5289 QEMU_OPTION_no_fd_bootchk
,
5292 QEMU_OPTION_nographic
,
5294 QEMU_OPTION_audio_help
,
5295 QEMU_OPTION_soundhw
,
5313 QEMU_OPTION_no_code_copy
,
5315 QEMU_OPTION_localtime
,
5316 QEMU_OPTION_cirrusvga
,
5318 QEMU_OPTION_std_vga
,
5319 QEMU_OPTION_monitor
,
5321 QEMU_OPTION_parallel
,
5323 QEMU_OPTION_full_screen
,
5324 QEMU_OPTION_pidfile
,
5325 QEMU_OPTION_no_kqemu
,
5326 QEMU_OPTION_kernel_kqemu
,
5327 QEMU_OPTION_win2k_hack
,
5329 QEMU_OPTION_usbdevice
,
5332 QEMU_OPTION_no_acpi
,
5335 typedef struct QEMUOption
{
5341 const QEMUOption qemu_options
[] = {
5342 { "h", 0, QEMU_OPTION_h
},
5344 { "M", HAS_ARG
, QEMU_OPTION_M
},
5345 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
5346 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
5347 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
5348 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
5349 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
5350 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
5351 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
5352 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
5353 { "snapshot", 0, QEMU_OPTION_snapshot
},
5355 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
5357 { "m", HAS_ARG
, QEMU_OPTION_m
},
5358 { "nographic", 0, QEMU_OPTION_nographic
},
5359 { "k", HAS_ARG
, QEMU_OPTION_k
},
5361 { "audio-help", 0, QEMU_OPTION_audio_help
},
5362 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
5365 { "net", HAS_ARG
, QEMU_OPTION_net
},
5367 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
5369 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
5371 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
5374 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
5375 { "append", HAS_ARG
, QEMU_OPTION_append
},
5376 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
5378 { "S", 0, QEMU_OPTION_S
},
5379 { "s", 0, QEMU_OPTION_s
},
5380 { "p", HAS_ARG
, QEMU_OPTION_p
},
5381 { "d", HAS_ARG
, QEMU_OPTION_d
},
5382 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
5383 { "L", HAS_ARG
, QEMU_OPTION_L
},
5384 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
5386 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
5387 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
5389 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5390 { "g", 1, QEMU_OPTION_g
},
5392 { "localtime", 0, QEMU_OPTION_localtime
},
5393 { "std-vga", 0, QEMU_OPTION_std_vga
},
5394 { "monitor", 1, QEMU_OPTION_monitor
},
5395 { "serial", 1, QEMU_OPTION_serial
},
5396 { "parallel", 1, QEMU_OPTION_parallel
},
5397 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
5398 { "full-screen", 0, QEMU_OPTION_full_screen
},
5399 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
5400 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
5401 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
5402 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
5403 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
5405 /* temporary options */
5406 { "usb", 0, QEMU_OPTION_usb
},
5407 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
5408 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
5412 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5414 /* this stack is only used during signal handling */
5415 #define SIGNAL_STACK_SIZE 32768
5417 static uint8_t *signal_stack
;
5421 /* password input */
5423 static BlockDriverState
*get_bdrv(int index
)
5425 BlockDriverState
*bs
;
5428 bs
= bs_table
[index
];
5429 } else if (index
< 6) {
5430 bs
= fd_table
[index
- 4];
5437 static void read_passwords(void)
5439 BlockDriverState
*bs
;
5443 for(i
= 0; i
< 6; i
++) {
5445 if (bs
&& bdrv_is_encrypted(bs
)) {
5446 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
5447 for(j
= 0; j
< 3; j
++) {
5448 monitor_readline("Password: ",
5449 1, password
, sizeof(password
));
5450 if (bdrv_set_key(bs
, password
) == 0)
5452 term_printf("invalid password\n");
5458 /* XXX: currently we cannot use simultaneously different CPUs */
5459 void register_machines(void)
5461 #if defined(TARGET_I386)
5462 qemu_register_machine(&pc_machine
);
5463 qemu_register_machine(&isapc_machine
);
5464 #elif defined(TARGET_PPC)
5465 qemu_register_machine(&heathrow_machine
);
5466 qemu_register_machine(&core99_machine
);
5467 qemu_register_machine(&prep_machine
);
5468 #elif defined(TARGET_MIPS)
5469 qemu_register_machine(&mips_machine
);
5470 #elif defined(TARGET_SPARC)
5471 #ifdef TARGET_SPARC64
5472 qemu_register_machine(&sun4u_machine
);
5474 qemu_register_machine(&sun4m_machine
);
5476 #elif defined(TARGET_ARM)
5477 qemu_register_machine(&integratorcp926_machine
);
5478 qemu_register_machine(&integratorcp1026_machine
);
5479 qemu_register_machine(&versatilepb_machine
);
5480 qemu_register_machine(&versatileab_machine
);
5481 #elif defined(TARGET_SH4)
5482 qemu_register_machine(&shix_machine
);
5484 #error unsupported CPU
5489 struct soundhw soundhw
[] = {
5496 { .init_isa
= pcspk_audio_init
}
5501 "Creative Sound Blaster 16",
5504 { .init_isa
= SB16_init
}
5511 "Yamaha YMF262 (OPL3)",
5513 "Yamaha YM3812 (OPL2)",
5517 { .init_isa
= Adlib_init
}
5524 "Gravis Ultrasound GF1",
5527 { .init_isa
= GUS_init
}
5533 "ENSONIQ AudioPCI ES1370",
5536 { .init_pci
= es1370_init
}
5539 { NULL
, NULL
, 0, 0, { NULL
} }
5542 static void select_soundhw (const char *optarg
)
5546 if (*optarg
== '?') {
5549 printf ("Valid sound card names (comma separated):\n");
5550 for (c
= soundhw
; c
->name
; ++c
) {
5551 printf ("%-11s %s\n", c
->name
, c
->descr
);
5553 printf ("\n-soundhw all will enable all of the above\n");
5554 exit (*optarg
!= '?');
5562 if (!strcmp (optarg
, "all")) {
5563 for (c
= soundhw
; c
->name
; ++c
) {
5571 e
= strchr (p
, ',');
5572 l
= !e
? strlen (p
) : (size_t) (e
- p
);
5574 for (c
= soundhw
; c
->name
; ++c
) {
5575 if (!strncmp (c
->name
, p
, l
)) {
5584 "Unknown sound card name (too big to show)\n");
5587 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
5592 p
+= l
+ (e
!= NULL
);
5596 goto show_valid_cards
;
5602 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
5604 exit(STATUS_CONTROL_C_EXIT
);
5609 #define MAX_NET_CLIENTS 32
5611 int main(int argc
, char **argv
)
5613 #ifdef CONFIG_GDBSTUB
5614 int use_gdbstub
, gdbstub_port
;
5617 int snapshot
, linux_boot
;
5618 const char *initrd_filename
;
5619 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
5620 const char *kernel_filename
, *kernel_cmdline
;
5621 DisplayState
*ds
= &display_state
;
5622 int cyls
, heads
, secs
, translation
;
5623 int start_emulation
= 1;
5624 char net_clients
[MAX_NET_CLIENTS
][256];
5627 const char *r
, *optarg
;
5628 CharDriverState
*monitor_hd
;
5629 char monitor_device
[128];
5630 char serial_devices
[MAX_SERIAL_PORTS
][128];
5631 int serial_device_index
;
5632 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
5633 int parallel_device_index
;
5634 const char *loadvm
= NULL
;
5635 QEMUMachine
*machine
;
5636 char usb_devices
[MAX_USB_CMDLINE
][128];
5637 int usb_devices_index
;
5639 LIST_INIT (&vm_change_state_head
);
5642 struct sigaction act
;
5643 sigfillset(&act
.sa_mask
);
5645 act
.sa_handler
= SIG_IGN
;
5646 sigaction(SIGPIPE
, &act
, NULL
);
5649 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
5650 /* Note: cpu_interrupt() is currently not SMP safe, so we force
5651 QEMU to run on a single CPU */
5656 h
= GetCurrentProcess();
5657 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
5658 for(i
= 0; i
< 32; i
++) {
5659 if (mask
& (1 << i
))
5664 SetProcessAffinityMask(h
, mask
);
5670 register_machines();
5671 machine
= first_machine
;
5672 initrd_filename
= NULL
;
5673 for(i
= 0; i
< MAX_FD
; i
++)
5674 fd_filename
[i
] = NULL
;
5675 for(i
= 0; i
< MAX_DISKS
; i
++)
5676 hd_filename
[i
] = NULL
;
5677 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5678 vga_ram_size
= VGA_RAM_SIZE
;
5679 bios_size
= BIOS_SIZE
;
5680 #ifdef CONFIG_GDBSTUB
5682 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
5686 kernel_filename
= NULL
;
5687 kernel_cmdline
= "";
5693 cyls
= heads
= secs
= 0;
5694 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5695 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
5697 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
5698 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
5699 serial_devices
[i
][0] = '\0';
5700 serial_device_index
= 0;
5702 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
5703 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
5704 parallel_devices
[i
][0] = '\0';
5705 parallel_device_index
= 0;
5707 usb_devices_index
= 0;
5712 /* default mac address of the first network interface */
5720 hd_filename
[0] = argv
[optind
++];
5722 const QEMUOption
*popt
;
5725 popt
= qemu_options
;
5728 fprintf(stderr
, "%s: invalid option -- '%s'\n",
5732 if (!strcmp(popt
->name
, r
+ 1))
5736 if (popt
->flags
& HAS_ARG
) {
5737 if (optind
>= argc
) {
5738 fprintf(stderr
, "%s: option '%s' requires an argument\n",
5742 optarg
= argv
[optind
++];
5747 switch(popt
->index
) {
5749 machine
= find_machine(optarg
);
5752 printf("Supported machines are:\n");
5753 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5754 printf("%-10s %s%s\n",
5756 m
== first_machine
? " (default)" : "");
5761 case QEMU_OPTION_initrd
:
5762 initrd_filename
= optarg
;
5764 case QEMU_OPTION_hda
:
5765 case QEMU_OPTION_hdb
:
5766 case QEMU_OPTION_hdc
:
5767 case QEMU_OPTION_hdd
:
5770 hd_index
= popt
->index
- QEMU_OPTION_hda
;
5771 hd_filename
[hd_index
] = optarg
;
5772 if (hd_index
== cdrom_index
)
5776 case QEMU_OPTION_snapshot
:
5779 case QEMU_OPTION_hdachs
:
5783 cyls
= strtol(p
, (char **)&p
, 0);
5784 if (cyls
< 1 || cyls
> 16383)
5789 heads
= strtol(p
, (char **)&p
, 0);
5790 if (heads
< 1 || heads
> 16)
5795 secs
= strtol(p
, (char **)&p
, 0);
5796 if (secs
< 1 || secs
> 63)
5800 if (!strcmp(p
, "none"))
5801 translation
= BIOS_ATA_TRANSLATION_NONE
;
5802 else if (!strcmp(p
, "lba"))
5803 translation
= BIOS_ATA_TRANSLATION_LBA
;
5804 else if (!strcmp(p
, "auto"))
5805 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5808 } else if (*p
!= '\0') {
5810 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5815 case QEMU_OPTION_nographic
:
5816 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
5817 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
5820 case QEMU_OPTION_kernel
:
5821 kernel_filename
= optarg
;
5823 case QEMU_OPTION_append
:
5824 kernel_cmdline
= optarg
;
5826 case QEMU_OPTION_cdrom
:
5827 if (cdrom_index
>= 0) {
5828 hd_filename
[cdrom_index
] = optarg
;
5831 case QEMU_OPTION_boot
:
5832 boot_device
= optarg
[0];
5833 if (boot_device
!= 'a' &&
5836 boot_device
!= 'n' &&
5838 boot_device
!= 'c' && boot_device
!= 'd') {
5839 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
5843 case QEMU_OPTION_fda
:
5844 fd_filename
[0] = optarg
;
5846 case QEMU_OPTION_fdb
:
5847 fd_filename
[1] = optarg
;
5850 case QEMU_OPTION_no_fd_bootchk
:
5854 case QEMU_OPTION_no_code_copy
:
5855 code_copy_enabled
= 0;
5857 case QEMU_OPTION_net
:
5858 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5859 fprintf(stderr
, "qemu: too many network clients\n");
5862 pstrcpy(net_clients
[nb_net_clients
],
5863 sizeof(net_clients
[0]),
5868 case QEMU_OPTION_tftp
:
5869 tftp_prefix
= optarg
;
5872 case QEMU_OPTION_smb
:
5873 net_slirp_smb(optarg
);
5876 case QEMU_OPTION_redir
:
5877 net_slirp_redir(optarg
);
5881 case QEMU_OPTION_audio_help
:
5885 case QEMU_OPTION_soundhw
:
5886 select_soundhw (optarg
);
5893 ram_size
= atoi(optarg
) * 1024 * 1024;
5896 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
5897 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
5898 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
5907 mask
= cpu_str_to_log_mask(optarg
);
5909 printf("Log items (comma separated):\n");
5910 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5911 printf("%-10s %s\n", item
->name
, item
->help
);
5918 #ifdef CONFIG_GDBSTUB
5923 gdbstub_port
= atoi(optarg
);
5930 start_emulation
= 0;
5933 keyboard_layout
= optarg
;
5935 case QEMU_OPTION_localtime
:
5938 case QEMU_OPTION_cirrusvga
:
5939 cirrus_vga_enabled
= 1;
5941 case QEMU_OPTION_std_vga
:
5942 cirrus_vga_enabled
= 0;
5949 w
= strtol(p
, (char **)&p
, 10);
5952 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5958 h
= strtol(p
, (char **)&p
, 10);
5963 depth
= strtol(p
, (char **)&p
, 10);
5964 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5965 depth
!= 24 && depth
!= 32)
5967 } else if (*p
== '\0') {
5968 depth
= graphic_depth
;
5975 graphic_depth
= depth
;
5978 case QEMU_OPTION_monitor
:
5979 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
5981 case QEMU_OPTION_serial
:
5982 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5983 fprintf(stderr
, "qemu: too many serial ports\n");
5986 pstrcpy(serial_devices
[serial_device_index
],
5987 sizeof(serial_devices
[0]), optarg
);
5988 serial_device_index
++;
5990 case QEMU_OPTION_parallel
:
5991 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5992 fprintf(stderr
, "qemu: too many parallel ports\n");
5995 pstrcpy(parallel_devices
[parallel_device_index
],
5996 sizeof(parallel_devices
[0]), optarg
);
5997 parallel_device_index
++;
5999 case QEMU_OPTION_loadvm
:
6002 case QEMU_OPTION_full_screen
:
6005 case QEMU_OPTION_pidfile
:
6006 create_pidfile(optarg
);
6009 case QEMU_OPTION_win2k_hack
:
6010 win2k_install_hack
= 1;
6014 case QEMU_OPTION_no_kqemu
:
6017 case QEMU_OPTION_kernel_kqemu
:
6021 case QEMU_OPTION_usb
:
6024 case QEMU_OPTION_usbdevice
:
6026 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
6027 fprintf(stderr
, "Too many USB devices\n");
6030 pstrcpy(usb_devices
[usb_devices_index
],
6031 sizeof(usb_devices
[usb_devices_index
]),
6033 usb_devices_index
++;
6035 case QEMU_OPTION_smp
:
6036 smp_cpus
= atoi(optarg
);
6037 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
6038 fprintf(stderr
, "Invalid number of CPUs\n");
6042 case QEMU_OPTION_vnc
:
6043 vnc_display
= atoi(optarg
);
6044 if (vnc_display
< 0) {
6045 fprintf(stderr
, "Invalid VNC display\n");
6049 case QEMU_OPTION_no_acpi
:
6060 linux_boot
= (kernel_filename
!= NULL
);
6063 hd_filename
[0] == '\0' &&
6064 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
6065 fd_filename
[0] == '\0')
6068 /* boot to cd by default if no hard disk */
6069 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
6070 if (fd_filename
[0] != '\0')
6076 setvbuf(stdout
, NULL
, _IOLBF
, 0);
6085 /* init network clients */
6086 if (nb_net_clients
== 0) {
6087 /* if no clients, we use a default config */
6088 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
6090 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
6095 for(i
= 0;i
< nb_net_clients
; i
++) {
6096 if (net_client_init(net_clients
[i
]) < 0)
6100 /* init the memory */
6102 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
+ KVM_EXTRA_PAGES
* 4096;
6105 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
6106 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
6107 if (!phys_ram_base
) {
6108 fprintf(stderr
, "Could not allocate physical memory\n");
6113 /* we always create the cdrom drive, even if no disk is there */
6115 if (cdrom_index
>= 0) {
6116 bs_table
[cdrom_index
] = bdrv_new("cdrom");
6117 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
6120 /* open the virtual block devices */
6121 for(i
= 0; i
< MAX_DISKS
; i
++) {
6122 if (hd_filename
[i
]) {
6125 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
6126 bs_table
[i
] = bdrv_new(buf
);
6128 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
6129 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
6133 if (i
== 0 && cyls
!= 0) {
6134 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
6135 bdrv_set_translation_hint(bs_table
[i
], translation
);
6140 /* we always create at least one floppy disk */
6141 fd_table
[0] = bdrv_new("fda");
6142 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
6144 for(i
= 0; i
< MAX_FD
; i
++) {
6145 if (fd_filename
[i
]) {
6148 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
6149 fd_table
[i
] = bdrv_new(buf
);
6150 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
6152 if (fd_filename
[i
] != '\0') {
6153 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
6154 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
6162 register_savevm("timer", 0, 1, timer_save
, timer_load
, NULL
);
6163 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
6169 dumb_display_init(ds
);
6170 } else if (vnc_display
!= -1) {
6171 vnc_display_init(ds
, vnc_display
);
6173 #if defined(CONFIG_SDL)
6174 sdl_display_init(ds
, full_screen
);
6175 #elif defined(CONFIG_COCOA)
6176 cocoa_display_init(ds
, full_screen
);
6178 dumb_display_init(ds
);
6182 monitor_hd
= qemu_chr_open(monitor_device
);
6184 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
6187 monitor_init(monitor_hd
, !nographic
);
6189 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6190 if (serial_devices
[i
][0] != '\0') {
6191 serial_hds
[i
] = qemu_chr_open(serial_devices
[i
]);
6192 if (!serial_hds
[i
]) {
6193 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
6197 if (!strcmp(serial_devices
[i
], "vc"))
6198 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6202 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6203 if (parallel_devices
[i
][0] != '\0') {
6204 parallel_hds
[i
] = qemu_chr_open(parallel_devices
[i
]);
6205 if (!parallel_hds
[i
]) {
6206 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
6207 parallel_devices
[i
]);
6210 if (!strcmp(parallel_devices
[i
], "vc"))
6211 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6215 machine
->init(ram_size
, vga_ram_size
, boot_device
,
6216 ds
, fd_filename
, snapshot
,
6217 kernel_filename
, kernel_cmdline
, initrd_filename
);
6219 /* init USB devices */
6221 for(i
= 0; i
< usb_devices_index
; i
++) {
6222 if (usb_device_add(usb_devices
[i
]) < 0) {
6223 fprintf(stderr
, "Warning: could not add USB device %s\n",
6229 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
6230 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
6232 #ifdef CONFIG_GDBSTUB
6234 if (gdbserver_start(gdbstub_port
) < 0) {
6235 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
6239 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
6244 qemu_loadvm(loadvm
);
6247 /* XXX: simplify init */
6249 if (start_emulation
) {