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>
71 #define getopt_long_only getopt_long
72 #define memalign(align, size) malloc(size)
79 #endif /* CONFIG_SDL */
83 #define main qemu_main
84 #endif /* CONFIG_COCOA */
90 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
92 //#define DEBUG_UNUSED_IOPORT
93 //#define DEBUG_IOPORT
95 #if !defined(CONFIG_SOFTMMU)
96 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
98 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
102 #define DEFAULT_RAM_SIZE 144
104 #define DEFAULT_RAM_SIZE 128
107 #define GUI_REFRESH_INTERVAL 30
109 /* XXX: use a two level table to limit memory usage */
110 #define MAX_IOPORTS 65536
112 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
113 char phys_ram_file
[1024];
114 void *ioport_opaque
[MAX_IOPORTS
];
115 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
116 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
117 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
120 static DisplayState display_state
;
122 const char* keyboard_layout
= NULL
;
123 int64_t ticks_per_sec
;
124 int boot_device
= 'c';
126 int pit_min_timer_count
= 0;
128 NICInfo nd_table
[MAX_NICS
];
129 QEMUTimer
*gui_timer
;
132 int cirrus_vga_enabled
= 1;
134 int graphic_width
= 1024;
135 int graphic_height
= 768;
137 int graphic_width
= 800;
138 int graphic_height
= 600;
140 int graphic_depth
= 15;
142 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
143 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
145 int win2k_install_hack
= 0;
148 USBPort
*vm_usb_ports
[MAX_VM_USB_PORTS
];
149 USBDevice
*vm_usb_hub
;
150 static VLANState
*first_vlan
;
152 int vnc_display
= -1;
153 #if defined(TARGET_SPARC)
155 #elif defined(TARGET_I386)
161 /***********************************************************/
162 /* x86 ISA bus support */
164 target_phys_addr_t isa_mem_base
= 0;
167 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
169 #ifdef DEBUG_UNUSED_IOPORT
170 fprintf(stderr
, "inb: port=0x%04x\n", address
);
175 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
177 #ifdef DEBUG_UNUSED_IOPORT
178 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
182 /* default is to make two byte accesses */
183 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
186 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
187 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
188 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
192 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
194 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
195 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
196 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
199 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
201 #ifdef DEBUG_UNUSED_IOPORT
202 fprintf(stderr
, "inl: port=0x%04x\n", address
);
207 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
209 #ifdef DEBUG_UNUSED_IOPORT
210 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
214 void init_ioports(void)
218 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
219 ioport_read_table
[0][i
] = default_ioport_readb
;
220 ioport_write_table
[0][i
] = default_ioport_writeb
;
221 ioport_read_table
[1][i
] = default_ioport_readw
;
222 ioport_write_table
[1][i
] = default_ioport_writew
;
223 ioport_read_table
[2][i
] = default_ioport_readl
;
224 ioport_write_table
[2][i
] = default_ioport_writel
;
228 /* size is the word size in byte */
229 int register_ioport_read(int start
, int length
, int size
,
230 IOPortReadFunc
*func
, void *opaque
)
236 } else if (size
== 2) {
238 } else if (size
== 4) {
241 hw_error("register_ioport_read: invalid size");
244 for(i
= start
; i
< start
+ length
; i
+= size
) {
245 ioport_read_table
[bsize
][i
] = func
;
246 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
247 hw_error("register_ioport_read: invalid opaque");
248 ioport_opaque
[i
] = opaque
;
253 /* size is the word size in byte */
254 int register_ioport_write(int start
, int length
, int size
,
255 IOPortWriteFunc
*func
, void *opaque
)
261 } else if (size
== 2) {
263 } else if (size
== 4) {
266 hw_error("register_ioport_write: invalid size");
269 for(i
= start
; i
< start
+ length
; i
+= size
) {
270 ioport_write_table
[bsize
][i
] = func
;
271 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
272 hw_error("register_ioport_read: invalid opaque");
273 ioport_opaque
[i
] = opaque
;
278 void isa_unassign_ioport(int start
, int length
)
282 for(i
= start
; i
< start
+ length
; i
++) {
283 ioport_read_table
[0][i
] = default_ioport_readb
;
284 ioport_read_table
[1][i
] = default_ioport_readw
;
285 ioport_read_table
[2][i
] = default_ioport_readl
;
287 ioport_write_table
[0][i
] = default_ioport_writeb
;
288 ioport_write_table
[1][i
] = default_ioport_writew
;
289 ioport_write_table
[2][i
] = default_ioport_writel
;
293 /***********************************************************/
295 void pstrcpy(char *buf
, int buf_size
, const char *str
)
305 if (c
== 0 || q
>= buf
+ buf_size
- 1)
312 /* strcat and truncate. */
313 char *pstrcat(char *buf
, int buf_size
, const char *s
)
318 pstrcpy(buf
+ len
, buf_size
- len
, s
);
322 int strstart(const char *str
, const char *val
, const char **ptr
)
338 void cpu_outb(CPUState
*env
, int addr
, int val
)
341 if (loglevel
& CPU_LOG_IOPORT
)
342 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
344 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
347 env
->last_io_time
= cpu_get_time_fast();
351 void cpu_outw(CPUState
*env
, int addr
, int val
)
354 if (loglevel
& CPU_LOG_IOPORT
)
355 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
357 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
360 env
->last_io_time
= cpu_get_time_fast();
364 void cpu_outl(CPUState
*env
, int addr
, int val
)
367 if (loglevel
& CPU_LOG_IOPORT
)
368 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
370 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
373 env
->last_io_time
= cpu_get_time_fast();
377 int cpu_inb(CPUState
*env
, int addr
)
380 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
382 if (loglevel
& CPU_LOG_IOPORT
)
383 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
387 env
->last_io_time
= cpu_get_time_fast();
392 int cpu_inw(CPUState
*env
, int addr
)
395 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
397 if (loglevel
& CPU_LOG_IOPORT
)
398 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
402 env
->last_io_time
= cpu_get_time_fast();
407 int cpu_inl(CPUState
*env
, int addr
)
410 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
412 if (loglevel
& CPU_LOG_IOPORT
)
413 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
417 env
->last_io_time
= cpu_get_time_fast();
422 /***********************************************************/
423 void hw_error(const char *fmt
, ...)
429 fprintf(stderr
, "qemu: hardware error: ");
430 vfprintf(stderr
, fmt
, ap
);
431 fprintf(stderr
, "\n");
432 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
433 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
435 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
437 cpu_dump_state(env
, stderr
, fprintf
, 0);
444 /***********************************************************/
447 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
448 static void *qemu_put_kbd_event_opaque
;
449 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
450 static void *qemu_put_mouse_event_opaque
;
451 static int qemu_put_mouse_event_absolute
;
453 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
455 qemu_put_kbd_event_opaque
= opaque
;
456 qemu_put_kbd_event
= func
;
459 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
, int absolute
)
461 qemu_put_mouse_event_opaque
= opaque
;
462 qemu_put_mouse_event
= func
;
463 qemu_put_mouse_event_absolute
= absolute
;
466 void kbd_put_keycode(int keycode
)
468 if (qemu_put_kbd_event
) {
469 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
473 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
475 if (qemu_put_mouse_event
) {
476 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
477 dx
, dy
, dz
, buttons_state
);
481 int kbd_mouse_is_absolute(void)
483 return qemu_put_mouse_event_absolute
;
486 /***********************************************************/
489 #if defined(__powerpc__)
491 static inline uint32_t get_tbl(void)
494 asm volatile("mftb %0" : "=r" (tbl
));
498 static inline uint32_t get_tbu(void)
501 asm volatile("mftbu %0" : "=r" (tbl
));
505 int64_t cpu_get_real_ticks(void)
508 /* NOTE: we test if wrapping has occurred */
514 return ((int64_t)h
<< 32) | l
;
517 #elif defined(__i386__)
519 int64_t cpu_get_real_ticks(void)
522 asm volatile ("rdtsc" : "=A" (val
));
526 #elif defined(__x86_64__)
528 int64_t cpu_get_real_ticks(void)
532 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
539 #elif defined(__ia64)
541 int64_t cpu_get_real_ticks(void)
544 asm volatile ("mov %0 = ar.itc" : "=r"(val
) :: "memory");
548 #elif defined(__s390__)
550 int64_t cpu_get_real_ticks(void)
553 asm volatile("stck 0(%1)" : "=m" (val
) : "a" (&val
) : "cc");
558 #error unsupported CPU
561 static int64_t cpu_ticks_offset
;
562 static int cpu_ticks_enabled
;
564 static inline int64_t cpu_get_ticks(void)
566 if (!cpu_ticks_enabled
) {
567 return cpu_ticks_offset
;
569 return cpu_get_real_ticks() + cpu_ticks_offset
;
573 /* enable cpu_get_ticks() */
574 void cpu_enable_ticks(void)
576 if (!cpu_ticks_enabled
) {
577 cpu_ticks_offset
-= cpu_get_real_ticks();
578 cpu_ticks_enabled
= 1;
582 /* disable cpu_get_ticks() : the clock is stopped. You must not call
583 cpu_get_ticks() after that. */
584 void cpu_disable_ticks(void)
586 if (cpu_ticks_enabled
) {
587 cpu_ticks_offset
= cpu_get_ticks();
588 cpu_ticks_enabled
= 0;
592 static int64_t get_clock(void)
597 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
600 gettimeofday(&tv
, NULL
);
601 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
605 void cpu_calibrate_ticks(void)
610 ticks
= cpu_get_real_ticks();
616 usec
= get_clock() - usec
;
617 ticks
= cpu_get_real_ticks() - ticks
;
618 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
621 /* compute with 96 bit intermediate result: (a*b)/c */
622 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
627 #ifdef WORDS_BIGENDIAN
637 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
638 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
641 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
645 #define QEMU_TIMER_REALTIME 0
646 #define QEMU_TIMER_VIRTUAL 1
650 /* XXX: add frequency */
658 struct QEMUTimer
*next
;
664 static QEMUTimer
*active_timers
[2];
666 static MMRESULT timerID
;
668 /* frequency of the times() clock tick */
669 static int timer_freq
;
672 QEMUClock
*qemu_new_clock(int type
)
675 clock
= qemu_mallocz(sizeof(QEMUClock
));
682 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
686 ts
= qemu_mallocz(sizeof(QEMUTimer
));
693 void qemu_free_timer(QEMUTimer
*ts
)
698 /* stop a timer, but do not dealloc it */
699 void qemu_del_timer(QEMUTimer
*ts
)
703 /* NOTE: this code must be signal safe because
704 qemu_timer_expired() can be called from a signal. */
705 pt
= &active_timers
[ts
->clock
->type
];
718 /* modify the current timer so that it will be fired when current_time
719 >= expire_time. The corresponding callback will be called. */
720 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
726 /* add the timer in the sorted list */
727 /* NOTE: this code must be signal safe because
728 qemu_timer_expired() can be called from a signal. */
729 pt
= &active_timers
[ts
->clock
->type
];
734 if (t
->expire_time
> expire_time
)
738 ts
->expire_time
= expire_time
;
743 int qemu_timer_pending(QEMUTimer
*ts
)
746 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
753 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
757 return (timer_head
->expire_time
<= current_time
);
760 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
766 if (!ts
|| ts
->expire_time
> current_time
)
768 /* remove timer from the list before calling the callback */
769 *ptimer_head
= ts
->next
;
772 /* run the callback (the timer list can be modified) */
777 int64_t qemu_get_clock(QEMUClock
*clock
)
779 switch(clock
->type
) {
780 case QEMU_TIMER_REALTIME
:
782 return GetTickCount();
787 /* Note that using gettimeofday() is not a good solution
788 for timers because its value change when the date is
790 if (timer_freq
== 100) {
791 return times(&tp
) * 10;
793 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
798 case QEMU_TIMER_VIRTUAL
:
799 return cpu_get_ticks();
804 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
806 uint64_t expire_time
;
808 if (qemu_timer_pending(ts
)) {
809 expire_time
= ts
->expire_time
;
813 qemu_put_be64(f
, expire_time
);
816 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
818 uint64_t expire_time
;
820 expire_time
= qemu_get_be64(f
);
821 if (expire_time
!= -1) {
822 qemu_mod_timer(ts
, expire_time
);
828 static void timer_save(QEMUFile
*f
, void *opaque
)
830 if (cpu_ticks_enabled
) {
831 hw_error("cannot save state if virtual timers are running");
833 qemu_put_be64s(f
, &cpu_ticks_offset
);
834 qemu_put_be64s(f
, &ticks_per_sec
);
837 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
841 if (cpu_ticks_enabled
) {
844 qemu_get_be64s(f
, &cpu_ticks_offset
);
845 qemu_get_be64s(f
, &ticks_per_sec
);
850 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
851 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
853 static void host_alarm_handler(int host_signum
)
857 #define DISP_FREQ 1000
859 static int64_t delta_min
= INT64_MAX
;
860 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
862 ti
= qemu_get_clock(vm_clock
);
863 if (last_clock
!= 0) {
864 delta
= ti
- last_clock
;
865 if (delta
< delta_min
)
867 if (delta
> delta_max
)
870 if (++count
== DISP_FREQ
) {
871 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
872 muldiv64(delta_min
, 1000000, ticks_per_sec
),
873 muldiv64(delta_max
, 1000000, ticks_per_sec
),
874 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
875 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
877 delta_min
= INT64_MAX
;
885 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
886 qemu_get_clock(vm_clock
)) ||
887 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
888 qemu_get_clock(rt_clock
))) {
889 CPUState
*env
= cpu_single_env
;
891 /* stop the currently executing cpu because a timer occured */
892 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
894 if (env
->kqemu_enabled
) {
895 kqemu_cpu_interrupt(env
);
904 #if defined(__linux__)
906 #define RTC_FREQ 1024
910 static int start_rtc_timer(void)
912 rtc_fd
= open("/dev/rtc", O_RDONLY
);
915 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
916 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
917 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
918 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
921 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
926 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
932 static int start_rtc_timer(void)
937 #endif /* !defined(__linux__) */
939 #endif /* !defined(_WIN32) */
941 static void init_timers(void)
943 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
944 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
949 timerID
= timeSetEvent(1, // interval (ms)
951 host_alarm_handler
, // function
952 (DWORD
)&count
, // user parameter
953 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
955 perror("failed timer alarm");
959 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
962 struct sigaction act
;
963 struct itimerval itv
;
965 /* get times() syscall frequency */
966 timer_freq
= sysconf(_SC_CLK_TCK
);
969 sigfillset(&act
.sa_mask
);
971 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
972 act
.sa_flags
|= SA_ONSTACK
;
974 act
.sa_handler
= host_alarm_handler
;
975 sigaction(SIGALRM
, &act
, NULL
);
977 itv
.it_interval
.tv_sec
= 0;
978 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
979 itv
.it_value
.tv_sec
= 0;
980 itv
.it_value
.tv_usec
= 10 * 1000;
981 setitimer(ITIMER_REAL
, &itv
, NULL
);
982 /* we probe the tick duration of the kernel to inform the user if
983 the emulated kernel requested a too high timer frequency */
984 getitimer(ITIMER_REAL
, &itv
);
986 #if defined(__linux__)
987 if (itv
.it_interval
.tv_usec
> 1000) {
988 /* try to use /dev/rtc to have a faster timer */
989 if (start_rtc_timer() < 0)
992 itv
.it_interval
.tv_sec
= 0;
993 itv
.it_interval
.tv_usec
= 0;
994 itv
.it_value
.tv_sec
= 0;
995 itv
.it_value
.tv_usec
= 0;
996 setitimer(ITIMER_REAL
, &itv
, NULL
);
999 sigaction(SIGIO
, &act
, NULL
);
1000 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1001 fcntl(rtc_fd
, F_SETOWN
, getpid());
1003 #endif /* defined(__linux__) */
1006 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1007 PIT_FREQ
) / 1000000;
1013 void quit_timers(void)
1016 timeKillEvent(timerID
);
1020 /***********************************************************/
1021 /* character device */
1023 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1025 return s
->chr_write(s
, buf
, len
);
1028 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1032 return s
->chr_ioctl(s
, cmd
, arg
);
1035 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1040 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1041 qemu_chr_write(s
, buf
, strlen(buf
));
1045 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1047 if (s
->chr_send_event
)
1048 s
->chr_send_event(s
, event
);
1051 void qemu_chr_add_read_handler(CharDriverState
*s
,
1052 IOCanRWHandler
*fd_can_read
,
1053 IOReadHandler
*fd_read
, void *opaque
)
1055 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
1058 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
1060 s
->chr_event
= chr_event
;
1063 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1068 static void null_chr_add_read_handler(CharDriverState
*chr
,
1069 IOCanRWHandler
*fd_can_read
,
1070 IOReadHandler
*fd_read
, void *opaque
)
1074 CharDriverState
*qemu_chr_open_null(void)
1076 CharDriverState
*chr
;
1078 chr
= qemu_mallocz(sizeof(CharDriverState
));
1081 chr
->chr_write
= null_chr_write
;
1082 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1088 #define socket_error() WSAGetLastError()
1090 #define EWOULDBLOCK WSAEWOULDBLOCK
1091 #define EINTR WSAEINTR
1092 #define EINPROGRESS WSAEINPROGRESS
1094 static void socket_cleanup(void)
1099 static int socket_init(void)
1104 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1106 err
= WSAGetLastError();
1107 fprintf(stderr
, "WSAStartup: %d\n", err
);
1110 atexit(socket_cleanup
);
1114 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1120 ret
= send(fd
, buf
, len
, 0);
1123 errno
= WSAGetLastError();
1124 if (errno
!= WSAEWOULDBLOCK
) {
1127 } else if (ret
== 0) {
1137 void socket_set_nonblock(int fd
)
1139 unsigned long opt
= 1;
1140 ioctlsocket(fd
, FIONBIO
, &opt
);
1145 #define socket_error() errno
1146 #define closesocket(s) close(s)
1148 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1154 ret
= write(fd
, buf
, len
);
1156 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1158 } else if (ret
== 0) {
1168 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1170 return unix_write(fd
, buf
, len1
);
1173 void socket_set_nonblock(int fd
)
1175 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1177 #endif /* !_WIN32 */
1183 IOCanRWHandler
*fd_can_read
;
1184 IOReadHandler
*fd_read
;
1189 #define STDIO_MAX_CLIENTS 2
1191 static int stdio_nb_clients
;
1192 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1194 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1196 FDCharDriver
*s
= chr
->opaque
;
1197 return unix_write(s
->fd_out
, buf
, len
);
1200 static int fd_chr_read_poll(void *opaque
)
1202 CharDriverState
*chr
= opaque
;
1203 FDCharDriver
*s
= chr
->opaque
;
1205 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
1209 static void fd_chr_read(void *opaque
)
1211 CharDriverState
*chr
= opaque
;
1212 FDCharDriver
*s
= chr
->opaque
;
1217 if (len
> s
->max_size
)
1221 size
= read(s
->fd_in
, buf
, len
);
1223 s
->fd_read(s
->fd_opaque
, buf
, size
);
1227 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1228 IOCanRWHandler
*fd_can_read
,
1229 IOReadHandler
*fd_read
, void *opaque
)
1231 FDCharDriver
*s
= chr
->opaque
;
1233 if (s
->fd_in
>= 0) {
1234 s
->fd_can_read
= fd_can_read
;
1235 s
->fd_read
= fd_read
;
1236 s
->fd_opaque
= opaque
;
1237 if (nographic
&& s
->fd_in
== 0) {
1239 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1240 fd_chr_read
, NULL
, chr
);
1245 /* open a character device to a unix fd */
1246 CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1248 CharDriverState
*chr
;
1251 chr
= qemu_mallocz(sizeof(CharDriverState
));
1254 s
= qemu_mallocz(sizeof(FDCharDriver
));
1262 chr
->chr_write
= fd_chr_write
;
1263 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1267 CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1271 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1274 return qemu_chr_open_fd(-1, fd_out
);
1277 CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1281 fd
= open(filename
, O_RDWR
| O_BINARY
);
1284 return qemu_chr_open_fd(fd
, fd
);
1288 /* for STDIO, we handle the case where several clients use it
1291 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1293 #define TERM_FIFO_MAX_SIZE 1
1295 static int term_got_escape
, client_index
;
1296 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1299 void term_print_help(void)
1302 "C-a h print this help\n"
1303 "C-a x exit emulator\n"
1304 "C-a s save disk data back to file (if -snapshot)\n"
1305 "C-a b send break (magic sysrq)\n"
1306 "C-a c switch between console and monitor\n"
1307 "C-a C-a send C-a\n"
1311 /* called when a char is received */
1312 static void stdio_received_byte(int ch
)
1314 if (term_got_escape
) {
1315 term_got_escape
= 0;
1326 for (i
= 0; i
< MAX_DISKS
; i
++) {
1328 bdrv_commit(bs_table
[i
]);
1333 if (client_index
< stdio_nb_clients
) {
1334 CharDriverState
*chr
;
1337 chr
= stdio_clients
[client_index
];
1339 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1344 if (client_index
>= stdio_nb_clients
)
1346 if (client_index
== 0) {
1347 /* send a new line in the monitor to get the prompt */
1355 } else if (ch
== TERM_ESCAPE
) {
1356 term_got_escape
= 1;
1359 if (client_index
< stdio_nb_clients
) {
1361 CharDriverState
*chr
;
1364 chr
= stdio_clients
[client_index
];
1366 if (s
->fd_can_read(s
->fd_opaque
) > 0) {
1368 s
->fd_read(s
->fd_opaque
, buf
, 1);
1369 } else if (term_fifo_size
== 0) {
1370 term_fifo
[term_fifo_size
++] = ch
;
1376 static int stdio_read_poll(void *opaque
)
1378 CharDriverState
*chr
;
1381 if (client_index
< stdio_nb_clients
) {
1382 chr
= stdio_clients
[client_index
];
1384 /* try to flush the queue if needed */
1385 if (term_fifo_size
!= 0 && s
->fd_can_read(s
->fd_opaque
) > 0) {
1386 s
->fd_read(s
->fd_opaque
, term_fifo
, 1);
1389 /* see if we can absorb more chars */
1390 if (term_fifo_size
== 0)
1399 static void stdio_read(void *opaque
)
1404 size
= read(0, buf
, 1);
1406 stdio_received_byte(buf
[0]);
1409 /* init terminal so that we can grab keys */
1410 static struct termios oldtty
;
1411 static int old_fd0_flags
;
1413 static void term_exit(void)
1415 tcsetattr (0, TCSANOW
, &oldtty
);
1416 fcntl(0, F_SETFL
, old_fd0_flags
);
1419 static void term_init(void)
1423 tcgetattr (0, &tty
);
1425 old_fd0_flags
= fcntl(0, F_GETFL
);
1427 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1428 |INLCR
|IGNCR
|ICRNL
|IXON
);
1429 tty
.c_oflag
|= OPOST
;
1430 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1431 /* if graphical mode, we allow Ctrl-C handling */
1433 tty
.c_lflag
&= ~ISIG
;
1434 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1437 tty
.c_cc
[VTIME
] = 0;
1439 tcsetattr (0, TCSANOW
, &tty
);
1443 fcntl(0, F_SETFL
, O_NONBLOCK
);
1446 CharDriverState
*qemu_chr_open_stdio(void)
1448 CharDriverState
*chr
;
1451 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1453 chr
= qemu_chr_open_fd(0, 1);
1454 if (stdio_nb_clients
== 0)
1455 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, NULL
);
1456 client_index
= stdio_nb_clients
;
1458 if (stdio_nb_clients
!= 0)
1460 chr
= qemu_chr_open_fd(0, 1);
1462 stdio_clients
[stdio_nb_clients
++] = chr
;
1463 if (stdio_nb_clients
== 1) {
1464 /* set the terminal in raw mode */
1470 #if defined(__linux__)
1471 CharDriverState
*qemu_chr_open_pty(void)
1474 char slave_name
[1024];
1475 int master_fd
, slave_fd
;
1477 /* Not satisfying */
1478 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1482 /* Disabling local echo and line-buffered output */
1483 tcgetattr (master_fd
, &tty
);
1484 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1486 tty
.c_cc
[VTIME
] = 0;
1487 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1489 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1490 return qemu_chr_open_fd(master_fd
, master_fd
);
1493 static void tty_serial_init(int fd
, int speed
,
1494 int parity
, int data_bits
, int stop_bits
)
1500 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1501 speed
, parity
, data_bits
, stop_bits
);
1503 tcgetattr (fd
, &tty
);
1545 cfsetispeed(&tty
, spd
);
1546 cfsetospeed(&tty
, spd
);
1548 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1549 |INLCR
|IGNCR
|ICRNL
|IXON
);
1550 tty
.c_oflag
|= OPOST
;
1551 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1552 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
);
1573 tty
.c_cflag
|= PARENB
;
1576 tty
.c_cflag
|= PARENB
| PARODD
;
1580 tcsetattr (fd
, TCSANOW
, &tty
);
1583 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1585 FDCharDriver
*s
= chr
->opaque
;
1588 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1590 QEMUSerialSetParams
*ssp
= arg
;
1591 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1592 ssp
->data_bits
, ssp
->stop_bits
);
1595 case CHR_IOCTL_SERIAL_SET_BREAK
:
1597 int enable
= *(int *)arg
;
1599 tcsendbreak(s
->fd_in
, 1);
1608 CharDriverState
*qemu_chr_open_tty(const char *filename
)
1610 CharDriverState
*chr
;
1613 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1616 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1617 tty_serial_init(fd
, 115200, 'N', 8, 1);
1618 chr
= qemu_chr_open_fd(fd
, fd
);
1621 chr
->chr_ioctl
= tty_serial_ioctl
;
1625 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1627 int fd
= (int)chr
->opaque
;
1631 case CHR_IOCTL_PP_READ_DATA
:
1632 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1634 *(uint8_t *)arg
= b
;
1636 case CHR_IOCTL_PP_WRITE_DATA
:
1637 b
= *(uint8_t *)arg
;
1638 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1641 case CHR_IOCTL_PP_READ_CONTROL
:
1642 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1644 *(uint8_t *)arg
= b
;
1646 case CHR_IOCTL_PP_WRITE_CONTROL
:
1647 b
= *(uint8_t *)arg
;
1648 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1651 case CHR_IOCTL_PP_READ_STATUS
:
1652 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1654 *(uint8_t *)arg
= b
;
1662 CharDriverState
*qemu_chr_open_pp(const char *filename
)
1664 CharDriverState
*chr
;
1667 fd
= open(filename
, O_RDWR
);
1671 if (ioctl(fd
, PPCLAIM
) < 0) {
1676 chr
= qemu_mallocz(sizeof(CharDriverState
));
1681 chr
->opaque
= (void *)fd
;
1682 chr
->chr_write
= null_chr_write
;
1683 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1684 chr
->chr_ioctl
= pp_ioctl
;
1689 CharDriverState
*qemu_chr_open_pty(void)
1695 #endif /* !defined(_WIN32) */
1699 IOCanRWHandler
*fd_can_read
;
1700 IOReadHandler
*fd_read
;
1703 HANDLE hcom
, hrecv
, hsend
;
1704 OVERLAPPED orecv
, osend
;
1709 #define NSENDBUF 2048
1710 #define NRECVBUF 2048
1711 #define MAXCONNECT 1
1712 #define NTIMEOUT 5000
1714 static int win_chr_poll(void *opaque
);
1715 static int win_chr_pipe_poll(void *opaque
);
1717 static void win_chr_close2(WinCharState
*s
)
1720 CloseHandle(s
->hsend
);
1724 CloseHandle(s
->hrecv
);
1728 CloseHandle(s
->hcom
);
1732 qemu_del_polling_cb(win_chr_pipe_poll
, s
);
1734 qemu_del_polling_cb(win_chr_poll
, s
);
1737 static void win_chr_close(CharDriverState
*chr
)
1739 WinCharState
*s
= chr
->opaque
;
1743 static int win_chr_init(WinCharState
*s
, const char *filename
)
1746 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1751 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1753 fprintf(stderr
, "Failed CreateEvent\n");
1756 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1758 fprintf(stderr
, "Failed CreateEvent\n");
1762 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1763 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1764 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1765 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1770 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1771 fprintf(stderr
, "Failed SetupComm\n");
1775 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1776 size
= sizeof(COMMCONFIG
);
1777 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1778 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1779 CommConfigDialog(filename
, NULL
, &comcfg
);
1781 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1782 fprintf(stderr
, "Failed SetCommState\n");
1786 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1787 fprintf(stderr
, "Failed SetCommMask\n");
1791 cto
.ReadIntervalTimeout
= MAXDWORD
;
1792 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1793 fprintf(stderr
, "Failed SetCommTimeouts\n");
1797 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1798 fprintf(stderr
, "Failed ClearCommError\n");
1801 qemu_add_polling_cb(win_chr_poll
, s
);
1809 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1811 WinCharState
*s
= chr
->opaque
;
1812 DWORD len
, ret
, size
, err
;
1815 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1816 s
->osend
.hEvent
= s
->hsend
;
1819 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1821 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1823 err
= GetLastError();
1824 if (err
== ERROR_IO_PENDING
) {
1825 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1843 static int win_chr_read_poll(WinCharState
*s
)
1845 s
->max_size
= s
->fd_can_read(s
->win_opaque
);
1849 static void win_chr_readfile(WinCharState
*s
)
1855 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1856 s
->orecv
.hEvent
= s
->hrecv
;
1857 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1859 err
= GetLastError();
1860 if (err
== ERROR_IO_PENDING
) {
1861 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1866 s
->fd_read(s
->win_opaque
, buf
, size
);
1870 static void win_chr_read(WinCharState
*s
)
1872 if (s
->len
> s
->max_size
)
1873 s
->len
= s
->max_size
;
1877 win_chr_readfile(s
);
1880 static int win_chr_poll(void *opaque
)
1882 WinCharState
*s
= opaque
;
1886 ClearCommError(s
->hcom
, &comerr
, &status
);
1887 if (status
.cbInQue
> 0) {
1888 s
->len
= status
.cbInQue
;
1889 win_chr_read_poll(s
);
1896 static void win_chr_add_read_handler(CharDriverState
*chr
,
1897 IOCanRWHandler
*fd_can_read
,
1898 IOReadHandler
*fd_read
, void *opaque
)
1900 WinCharState
*s
= chr
->opaque
;
1902 s
->fd_can_read
= fd_can_read
;
1903 s
->fd_read
= fd_read
;
1904 s
->win_opaque
= opaque
;
1907 CharDriverState
*qemu_chr_open_win(const char *filename
)
1909 CharDriverState
*chr
;
1912 chr
= qemu_mallocz(sizeof(CharDriverState
));
1915 s
= qemu_mallocz(sizeof(WinCharState
));
1921 chr
->chr_write
= win_chr_write
;
1922 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
1923 chr
->chr_close
= win_chr_close
;
1925 if (win_chr_init(s
, filename
) < 0) {
1933 static int win_chr_pipe_poll(void *opaque
)
1935 WinCharState
*s
= opaque
;
1938 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1941 win_chr_read_poll(s
);
1948 static int win_chr_pipe_init(WinCharState
*s
, const char *filename
)
1957 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1959 fprintf(stderr
, "Failed CreateEvent\n");
1962 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1964 fprintf(stderr
, "Failed CreateEvent\n");
1968 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1969 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1970 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1972 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1973 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1974 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1979 ZeroMemory(&ov
, sizeof(ov
));
1980 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1981 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1983 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1987 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1989 fprintf(stderr
, "Failed GetOverlappedResult\n");
1991 CloseHandle(ov
.hEvent
);
1998 CloseHandle(ov
.hEvent
);
2001 qemu_add_polling_cb(win_chr_pipe_poll
, s
);
2010 CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2012 CharDriverState
*chr
;
2015 chr
= qemu_mallocz(sizeof(CharDriverState
));
2018 s
= qemu_mallocz(sizeof(WinCharState
));
2024 chr
->chr_write
= win_chr_write
;
2025 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2026 chr
->chr_close
= win_chr_close
;
2028 if (win_chr_pipe_init(s
, filename
) < 0) {
2036 CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2038 CharDriverState
*chr
;
2041 chr
= qemu_mallocz(sizeof(CharDriverState
));
2044 s
= qemu_mallocz(sizeof(WinCharState
));
2051 chr
->chr_write
= win_chr_write
;
2052 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2056 CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2060 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2061 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2062 if (fd_out
== INVALID_HANDLE_VALUE
)
2065 return qemu_chr_open_win_file(fd_out
);
2069 CharDriverState
*qemu_chr_open(const char *filename
)
2073 if (!strcmp(filename
, "vc")) {
2074 return text_console_init(&display_state
);
2075 } else if (!strcmp(filename
, "null")) {
2076 return qemu_chr_open_null();
2079 if (strstart(filename
, "file:", &p
)) {
2080 return qemu_chr_open_file_out(p
);
2081 } else if (strstart(filename
, "pipe:", &p
)) {
2082 return qemu_chr_open_pipe(p
);
2083 } else if (!strcmp(filename
, "pty")) {
2084 return qemu_chr_open_pty();
2085 } else if (!strcmp(filename
, "stdio")) {
2086 return qemu_chr_open_stdio();
2089 #if defined(__linux__)
2090 if (strstart(filename
, "/dev/parport", NULL
)) {
2091 return qemu_chr_open_pp(filename
);
2093 if (strstart(filename
, "/dev/", NULL
)) {
2094 return qemu_chr_open_tty(filename
);
2098 if (strstart(filename
, "COM", NULL
)) {
2099 return qemu_chr_open_win(filename
);
2101 if (strstart(filename
, "pipe:", &p
)) {
2102 return qemu_chr_open_win_pipe(p
);
2104 if (strstart(filename
, "file:", &p
)) {
2105 return qemu_chr_open_win_file_out(p
);
2113 void qemu_chr_close(CharDriverState
*chr
)
2116 chr
->chr_close(chr
);
2119 /***********************************************************/
2120 /* network device redirectors */
2122 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2126 for(i
=0;i
<size
;i
+=16) {
2130 fprintf(f
, "%08x ", i
);
2133 fprintf(f
, " %02x", buf
[i
+j
]);
2138 for(j
=0;j
<len
;j
++) {
2140 if (c
< ' ' || c
> '~')
2142 fprintf(f
, "%c", c
);
2148 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
2151 for(i
= 0; i
< 6; i
++) {
2152 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2165 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
2170 p1
= strchr(p
, sep
);
2176 if (len
> buf_size
- 1)
2178 memcpy(buf
, p
, len
);
2185 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
2193 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2195 saddr
->sin_family
= AF_INET
;
2196 if (buf
[0] == '\0') {
2197 saddr
->sin_addr
.s_addr
= 0;
2199 if (isdigit(buf
[0])) {
2200 if (!inet_aton(buf
, &saddr
->sin_addr
))
2203 if ((he
= gethostbyname(buf
)) == NULL
)
2205 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
2208 port
= strtol(p
, (char **)&r
, 0);
2211 saddr
->sin_port
= htons(port
);
2215 /* find or alloc a new VLAN */
2216 VLANState
*qemu_find_vlan(int id
)
2218 VLANState
**pvlan
, *vlan
;
2219 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2223 vlan
= qemu_mallocz(sizeof(VLANState
));
2228 pvlan
= &first_vlan
;
2229 while (*pvlan
!= NULL
)
2230 pvlan
= &(*pvlan
)->next
;
2235 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
2236 IOReadHandler
*fd_read
,
2237 IOCanRWHandler
*fd_can_read
,
2240 VLANClientState
*vc
, **pvc
;
2241 vc
= qemu_mallocz(sizeof(VLANClientState
));
2244 vc
->fd_read
= fd_read
;
2245 vc
->fd_can_read
= fd_can_read
;
2246 vc
->opaque
= opaque
;
2250 pvc
= &vlan
->first_client
;
2251 while (*pvc
!= NULL
)
2252 pvc
= &(*pvc
)->next
;
2257 int qemu_can_send_packet(VLANClientState
*vc1
)
2259 VLANState
*vlan
= vc1
->vlan
;
2260 VLANClientState
*vc
;
2262 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2264 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
2271 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
2273 VLANState
*vlan
= vc1
->vlan
;
2274 VLANClientState
*vc
;
2277 printf("vlan %d send:\n", vlan
->id
);
2278 hex_dump(stdout
, buf
, size
);
2280 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2282 vc
->fd_read(vc
->opaque
, buf
, size
);
2287 #if defined(CONFIG_SLIRP)
2289 /* slirp network adapter */
2291 static int slirp_inited
;
2292 static VLANClientState
*slirp_vc
;
2294 int slirp_can_output(void)
2296 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
2299 void slirp_output(const uint8_t *pkt
, int pkt_len
)
2302 printf("slirp output:\n");
2303 hex_dump(stdout
, pkt
, pkt_len
);
2307 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
2310 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
2313 printf("slirp input:\n");
2314 hex_dump(stdout
, buf
, size
);
2316 slirp_input(buf
, size
);
2319 static int net_slirp_init(VLANState
*vlan
)
2321 if (!slirp_inited
) {
2325 slirp_vc
= qemu_new_vlan_client(vlan
,
2326 slirp_receive
, NULL
, NULL
);
2327 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
2331 static void net_slirp_redir(const char *redir_str
)
2336 struct in_addr guest_addr
;
2337 int host_port
, guest_port
;
2339 if (!slirp_inited
) {
2345 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2347 if (!strcmp(buf
, "tcp")) {
2349 } else if (!strcmp(buf
, "udp")) {
2355 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2357 host_port
= strtol(buf
, &r
, 0);
2361 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2363 if (buf
[0] == '\0') {
2364 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
2366 if (!inet_aton(buf
, &guest_addr
))
2369 guest_port
= strtol(p
, &r
, 0);
2373 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
2374 fprintf(stderr
, "qemu: could not set up redirection\n");
2379 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
2387 static void smb_exit(void)
2391 char filename
[1024];
2393 /* erase all the files in the directory */
2394 d
= opendir(smb_dir
);
2399 if (strcmp(de
->d_name
, ".") != 0 &&
2400 strcmp(de
->d_name
, "..") != 0) {
2401 snprintf(filename
, sizeof(filename
), "%s/%s",
2402 smb_dir
, de
->d_name
);
2410 /* automatic user mode samba server configuration */
2411 void net_slirp_smb(const char *exported_dir
)
2413 char smb_conf
[1024];
2414 char smb_cmdline
[1024];
2417 if (!slirp_inited
) {
2422 /* XXX: better tmp dir construction */
2423 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
2424 if (mkdir(smb_dir
, 0700) < 0) {
2425 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
2428 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
2430 f
= fopen(smb_conf
, "w");
2432 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
2439 "socket address=127.0.0.1\n"
2440 "pid directory=%s\n"
2441 "lock directory=%s\n"
2442 "log file=%s/log.smbd\n"
2443 "smb passwd file=%s/smbpasswd\n"
2444 "security = share\n"
2459 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "/usr/sbin/smbd -s %s",
2462 slirp_add_exec(0, smb_cmdline
, 4, 139);
2465 #endif /* !defined(_WIN32) */
2467 #endif /* CONFIG_SLIRP */
2469 #if !defined(_WIN32)
2471 typedef struct TAPState
{
2472 VLANClientState
*vc
;
2476 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
2478 TAPState
*s
= opaque
;
2481 ret
= write(s
->fd
, buf
, size
);
2482 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
2489 static void tap_send(void *opaque
)
2491 TAPState
*s
= opaque
;
2495 size
= read(s
->fd
, buf
, sizeof(buf
));
2497 qemu_send_packet(s
->vc
, buf
, size
);
2503 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
2507 s
= qemu_mallocz(sizeof(TAPState
));
2511 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
2512 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
2513 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
2518 static int tap_open(char *ifname
, int ifname_size
)
2524 fd
= open("/dev/tap", O_RDWR
);
2526 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
2531 dev
= devname(s
.st_rdev
, S_IFCHR
);
2532 pstrcpy(ifname
, ifname_size
, dev
);
2534 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2537 #elif defined(__sun__)
2538 static int tap_open(char *ifname
, int ifname_size
)
2540 fprintf(stderr
, "warning: tap_open not yet implemented\n");
2544 static int tap_open(char *ifname
, int ifname_size
)
2549 fd
= open("/dev/net/tun", O_RDWR
);
2551 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
2554 memset(&ifr
, 0, sizeof(ifr
));
2555 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
2556 if (ifname
[0] != '\0')
2557 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
2559 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
2560 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
2562 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
2566 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
2567 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2572 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
2573 const char *setup_script
)
2576 int pid
, status
, fd
;
2581 if (ifname1
!= NULL
)
2582 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
2585 fd
= tap_open(ifname
, sizeof(ifname
));
2591 if (setup_script
[0] != '\0') {
2592 /* try to launch network init script */
2597 *parg
++ = (char *)setup_script
;
2600 execv(setup_script
, args
);
2603 while (waitpid(pid
, &status
, 0) != pid
);
2604 if (!WIFEXITED(status
) ||
2605 WEXITSTATUS(status
) != 0) {
2606 fprintf(stderr
, "%s: could not launch network script\n",
2612 s
= net_tap_fd_init(vlan
, fd
);
2615 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2616 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
2620 #endif /* !_WIN32 */
2622 /* network connection */
2623 typedef struct NetSocketState
{
2624 VLANClientState
*vc
;
2626 int state
; /* 0 = getting length, 1 = getting data */
2630 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
2633 typedef struct NetSocketListenState
{
2636 } NetSocketListenState
;
2638 /* XXX: we consider we can send the whole packet without blocking */
2639 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
2641 NetSocketState
*s
= opaque
;
2645 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
2646 send_all(s
->fd
, buf
, size
);
2649 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
2651 NetSocketState
*s
= opaque
;
2652 sendto(s
->fd
, buf
, size
, 0,
2653 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
2656 static void net_socket_send(void *opaque
)
2658 NetSocketState
*s
= opaque
;
2663 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
2665 err
= socket_error();
2666 if (err
!= EWOULDBLOCK
)
2668 } else if (size
== 0) {
2669 /* end of connection */
2671 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2677 /* reassemble a packet from the network */
2683 memcpy(s
->buf
+ s
->index
, buf
, l
);
2687 if (s
->index
== 4) {
2689 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
2695 l
= s
->packet_len
- s
->index
;
2698 memcpy(s
->buf
+ s
->index
, buf
, l
);
2702 if (s
->index
>= s
->packet_len
) {
2703 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
2712 static void net_socket_send_dgram(void *opaque
)
2714 NetSocketState
*s
= opaque
;
2717 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2721 /* end of connection */
2722 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2725 qemu_send_packet(s
->vc
, s
->buf
, size
);
2728 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
2733 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
2734 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
2735 inet_ntoa(mcastaddr
->sin_addr
),
2736 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
2740 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2742 perror("socket(PF_INET, SOCK_DGRAM)");
2747 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
2748 (const char *)&val
, sizeof(val
));
2750 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
2754 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
2760 /* Add host to multicast group */
2761 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
2762 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
2764 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
2765 (const char *)&imr
, sizeof(struct ip_mreq
));
2767 perror("setsockopt(IP_ADD_MEMBERSHIP)");
2771 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
2773 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
2774 (const char *)&val
, sizeof(val
));
2776 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
2780 socket_set_nonblock(fd
);
2783 if (fd
>=0) close(fd
);
2787 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
2790 struct sockaddr_in saddr
;
2792 socklen_t saddr_len
;
2795 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
2796 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
2797 * by ONLY ONE process: we must "clone" this dgram socket --jjo
2801 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
2803 if (saddr
.sin_addr
.s_addr
==0) {
2804 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
2808 /* clone dgram socket */
2809 newfd
= net_socket_mcast_create(&saddr
);
2811 /* error already reported by net_socket_mcast_create() */
2815 /* clone newfd to fd, close newfd */
2820 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
2821 fd
, strerror(errno
));
2826 s
= qemu_mallocz(sizeof(NetSocketState
));
2831 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
2832 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
2834 /* mcast: save bound address as dst */
2835 if (is_connected
) s
->dgram_dst
=saddr
;
2837 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2838 "socket: fd=%d (%s mcast=%s:%d)",
2839 fd
, is_connected
? "cloned" : "",
2840 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2844 static void net_socket_connect(void *opaque
)
2846 NetSocketState
*s
= opaque
;
2847 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
2850 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
2854 s
= qemu_mallocz(sizeof(NetSocketState
));
2858 s
->vc
= qemu_new_vlan_client(vlan
,
2859 net_socket_receive
, NULL
, s
);
2860 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2861 "socket: fd=%d", fd
);
2863 net_socket_connect(s
);
2865 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
2870 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
2873 int so_type
=-1, optlen
=sizeof(so_type
);
2875 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
2876 fprintf(stderr
, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd
);
2881 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
2883 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
2885 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
2886 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
2887 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
2892 static void net_socket_accept(void *opaque
)
2894 NetSocketListenState
*s
= opaque
;
2896 struct sockaddr_in saddr
;
2901 len
= sizeof(saddr
);
2902 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
2903 if (fd
< 0 && errno
!= EINTR
) {
2905 } else if (fd
>= 0) {
2909 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
2913 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
2914 "socket: connection from %s:%d",
2915 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2919 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
2921 NetSocketListenState
*s
;
2923 struct sockaddr_in saddr
;
2925 if (parse_host_port(&saddr
, host_str
) < 0)
2928 s
= qemu_mallocz(sizeof(NetSocketListenState
));
2932 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2937 socket_set_nonblock(fd
);
2939 /* allow fast reuse */
2941 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2943 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2948 ret
= listen(fd
, 0);
2955 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
2959 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
2962 int fd
, connected
, ret
, err
;
2963 struct sockaddr_in saddr
;
2965 if (parse_host_port(&saddr
, host_str
) < 0)
2968 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2973 socket_set_nonblock(fd
);
2977 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2979 err
= socket_error();
2980 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2981 } else if (err
== EINPROGRESS
) {
2993 s
= net_socket_fd_init(vlan
, fd
, connected
);
2996 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2997 "socket: connect to %s:%d",
2998 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3002 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
3006 struct sockaddr_in saddr
;
3008 if (parse_host_port(&saddr
, host_str
) < 0)
3012 fd
= net_socket_mcast_create(&saddr
);
3016 s
= net_socket_fd_init(vlan
, fd
, 0);
3020 s
->dgram_dst
= saddr
;
3022 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3023 "socket: mcast=%s:%d",
3024 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3029 static int get_param_value(char *buf
, int buf_size
,
3030 const char *tag
, const char *str
)
3039 while (*p
!= '\0' && *p
!= '=') {
3040 if ((q
- option
) < sizeof(option
) - 1)
3048 if (!strcmp(tag
, option
)) {
3050 while (*p
!= '\0' && *p
!= ',') {
3051 if ((q
- buf
) < buf_size
- 1)
3058 while (*p
!= '\0' && *p
!= ',') {
3069 int net_client_init(const char *str
)
3080 while (*p
!= '\0' && *p
!= ',') {
3081 if ((q
- device
) < sizeof(device
) - 1)
3089 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
3090 vlan_id
= strtol(buf
, NULL
, 0);
3092 vlan
= qemu_find_vlan(vlan_id
);
3094 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
3097 if (!strcmp(device
, "nic")) {
3101 if (nb_nics
>= MAX_NICS
) {
3102 fprintf(stderr
, "Too Many NICs\n");
3105 nd
= &nd_table
[nb_nics
];
3106 macaddr
= nd
->macaddr
;
3112 macaddr
[5] = 0x56 + nb_nics
;
3114 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
3115 if (parse_macaddr(macaddr
, buf
) < 0) {
3116 fprintf(stderr
, "invalid syntax for ethernet address\n");
3120 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
3121 nd
->model
= strdup(buf
);
3127 if (!strcmp(device
, "none")) {
3128 /* does nothing. It is needed to signal that no network cards
3133 if (!strcmp(device
, "user")) {
3134 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
3135 if (strlen(buf
) > 32)
3137 strcpy(slirp_hostname
, buf
);
3139 ret
= net_slirp_init(vlan
);
3143 if (!strcmp(device
, "tap")) {
3145 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
3146 fprintf(stderr
, "tap: no interface name\n");
3149 ret
= tap_win32_init(vlan
, ifname
);
3152 if (!strcmp(device
, "tap")) {
3154 char setup_script
[1024];
3156 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3157 fd
= strtol(buf
, NULL
, 0);
3159 if (net_tap_fd_init(vlan
, fd
))
3162 get_param_value(ifname
, sizeof(ifname
), "ifname", p
);
3163 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
3164 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
3166 ret
= net_tap_init(vlan
, ifname
, setup_script
);
3170 if (!strcmp(device
, "socket")) {
3171 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3173 fd
= strtol(buf
, NULL
, 0);
3175 if (net_socket_fd_init(vlan
, fd
, 1))
3177 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
3178 ret
= net_socket_listen_init(vlan
, buf
);
3179 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
3180 ret
= net_socket_connect_init(vlan
, buf
);
3181 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
3182 ret
= net_socket_mcast_init(vlan
, buf
);
3184 fprintf(stderr
, "Unknown socket options: %s\n", p
);
3189 fprintf(stderr
, "Unknown network device: %s\n", device
);
3193 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
3199 void do_info_network(void)
3202 VLANClientState
*vc
;
3204 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3205 term_printf("VLAN %d devices:\n", vlan
->id
);
3206 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3207 term_printf(" %s\n", vc
->info_str
);
3211 /***********************************************************/
3214 static int usb_device_add(const char *devname
)
3222 for(i
= 0;i
< MAX_VM_USB_PORTS
; i
++) {
3223 if (!vm_usb_ports
[i
]->dev
)
3226 if (i
== MAX_VM_USB_PORTS
)
3229 if (strstart(devname
, "host:", &p
)) {
3230 dev
= usb_host_device_open(p
);
3233 } else if (!strcmp(devname
, "mouse")) {
3234 dev
= usb_mouse_init();
3237 } else if (!strcmp(devname
, "tablet")) {
3238 dev
= usb_tablet_init();
3244 usb_attach(vm_usb_ports
[i
], dev
);
3248 static int usb_device_del(const char *devname
)
3251 int bus_num
, addr
, i
;
3257 p
= strchr(devname
, '.');
3260 bus_num
= strtoul(devname
, NULL
, 0);
3261 addr
= strtoul(p
+ 1, NULL
, 0);
3264 for(i
= 0;i
< MAX_VM_USB_PORTS
; i
++) {
3265 dev
= vm_usb_ports
[i
]->dev
;
3266 if (dev
&& dev
->addr
== addr
)
3269 if (i
== MAX_VM_USB_PORTS
)
3271 usb_attach(vm_usb_ports
[i
], NULL
);
3275 void do_usb_add(const char *devname
)
3278 ret
= usb_device_add(devname
);
3280 term_printf("Could not add USB device '%s'\n", devname
);
3283 void do_usb_del(const char *devname
)
3286 ret
= usb_device_del(devname
);
3288 term_printf("Could not remove USB device '%s'\n", devname
);
3295 const char *speed_str
;
3298 term_printf("USB support not enabled\n");
3302 for(i
= 0; i
< MAX_VM_USB_PORTS
; i
++) {
3303 dev
= vm_usb_ports
[i
]->dev
;
3305 term_printf("Hub port %d:\n", i
);
3306 switch(dev
->speed
) {
3310 case USB_SPEED_FULL
:
3313 case USB_SPEED_HIGH
:
3320 term_printf(" Device %d.%d, speed %s Mb/s\n",
3321 0, dev
->addr
, speed_str
);
3326 /***********************************************************/
3329 static char *pid_filename
;
3331 /* Remove PID file. Called on normal exit */
3333 static void remove_pidfile(void)
3335 unlink (pid_filename
);
3338 static void create_pidfile(const char *filename
)
3340 struct stat pidstat
;
3343 /* Try to write our PID to the named file */
3344 if (stat(filename
, &pidstat
) < 0) {
3345 if (errno
== ENOENT
) {
3346 if ((f
= fopen (filename
, "w")) == NULL
) {
3347 perror("Opening pidfile");
3350 fprintf(f
, "%d\n", getpid());
3352 pid_filename
= qemu_strdup(filename
);
3353 if (!pid_filename
) {
3354 fprintf(stderr
, "Could not save PID filename");
3357 atexit(remove_pidfile
);
3360 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
3366 /***********************************************************/
3369 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
3373 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
3377 static void dumb_refresh(DisplayState
*ds
)
3382 void dumb_display_init(DisplayState
*ds
)
3387 ds
->dpy_update
= dumb_update
;
3388 ds
->dpy_resize
= dumb_resize
;
3389 ds
->dpy_refresh
= dumb_refresh
;
3392 #if !defined(CONFIG_SOFTMMU)
3393 /***********************************************************/
3394 /* cpu signal handler */
3395 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
3398 if (cpu_signal_handler(host_signum
, info
, puc
))
3400 if (stdio_nb_clients
> 0)
3406 /***********************************************************/
3409 #define MAX_IO_HANDLERS 64
3411 typedef struct IOHandlerRecord
{
3413 IOCanRWHandler
*fd_read_poll
;
3415 IOHandler
*fd_write
;
3417 /* temporary data */
3419 struct IOHandlerRecord
*next
;
3422 static IOHandlerRecord
*first_io_handler
;
3424 /* XXX: fd_read_poll should be suppressed, but an API change is
3425 necessary in the character devices to suppress fd_can_read(). */
3426 int qemu_set_fd_handler2(int fd
,
3427 IOCanRWHandler
*fd_read_poll
,
3429 IOHandler
*fd_write
,
3432 IOHandlerRecord
**pioh
, *ioh
;
3434 if (!fd_read
&& !fd_write
) {
3435 pioh
= &first_io_handler
;
3440 if (ioh
->fd
== fd
) {
3448 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3452 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
3455 ioh
->next
= first_io_handler
;
3456 first_io_handler
= ioh
;
3459 ioh
->fd_read_poll
= fd_read_poll
;
3460 ioh
->fd_read
= fd_read
;
3461 ioh
->fd_write
= fd_write
;
3462 ioh
->opaque
= opaque
;
3467 int qemu_set_fd_handler(int fd
,
3469 IOHandler
*fd_write
,
3472 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
3475 /***********************************************************/
3476 /* Polling handling */
3478 typedef struct PollingEntry
{
3481 struct PollingEntry
*next
;
3484 static PollingEntry
*first_polling_entry
;
3486 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
3488 PollingEntry
**ppe
, *pe
;
3489 pe
= qemu_mallocz(sizeof(PollingEntry
));
3493 pe
->opaque
= opaque
;
3494 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
3499 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3501 PollingEntry
**ppe
, *pe
;
3502 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3504 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3512 /***********************************************************/
3513 /* savevm/loadvm support */
3515 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
3517 fwrite(buf
, 1, size
, f
);
3520 void qemu_put_byte(QEMUFile
*f
, int v
)
3525 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
3527 qemu_put_byte(f
, v
>> 8);
3528 qemu_put_byte(f
, v
);
3531 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
3533 qemu_put_byte(f
, v
>> 24);
3534 qemu_put_byte(f
, v
>> 16);
3535 qemu_put_byte(f
, v
>> 8);
3536 qemu_put_byte(f
, v
);
3539 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
3541 qemu_put_be32(f
, v
>> 32);
3542 qemu_put_be32(f
, v
);
3545 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
3547 return fread(buf
, 1, size
, f
);
3550 int qemu_get_byte(QEMUFile
*f
)
3560 unsigned int qemu_get_be16(QEMUFile
*f
)
3563 v
= qemu_get_byte(f
) << 8;
3564 v
|= qemu_get_byte(f
);
3568 unsigned int qemu_get_be32(QEMUFile
*f
)
3571 v
= qemu_get_byte(f
) << 24;
3572 v
|= qemu_get_byte(f
) << 16;
3573 v
|= qemu_get_byte(f
) << 8;
3574 v
|= qemu_get_byte(f
);
3578 uint64_t qemu_get_be64(QEMUFile
*f
)
3581 v
= (uint64_t)qemu_get_be32(f
) << 32;
3582 v
|= qemu_get_be32(f
);
3586 int64_t qemu_ftell(QEMUFile
*f
)
3591 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
3593 if (fseek(f
, pos
, whence
) < 0)
3598 typedef struct SaveStateEntry
{
3602 SaveStateHandler
*save_state
;
3603 LoadStateHandler
*load_state
;
3605 struct SaveStateEntry
*next
;
3608 static SaveStateEntry
*first_se
;
3610 int register_savevm(const char *idstr
,
3613 SaveStateHandler
*save_state
,
3614 LoadStateHandler
*load_state
,
3617 SaveStateEntry
*se
, **pse
;
3619 se
= qemu_malloc(sizeof(SaveStateEntry
));
3622 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
3623 se
->instance_id
= instance_id
;
3624 se
->version_id
= version_id
;
3625 se
->save_state
= save_state
;
3626 se
->load_state
= load_state
;
3627 se
->opaque
= opaque
;
3630 /* add at the end of list */
3632 while (*pse
!= NULL
)
3633 pse
= &(*pse
)->next
;
3638 #define QEMU_VM_FILE_MAGIC 0x5145564d
3639 #define QEMU_VM_FILE_VERSION 0x00000001
3641 int qemu_savevm(const char *filename
)
3645 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
3647 saved_vm_running
= vm_running
;
3650 f
= fopen(filename
, "wb");
3656 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
3657 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
3659 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3661 len
= strlen(se
->idstr
);
3662 qemu_put_byte(f
, len
);
3663 qemu_put_buffer(f
, se
->idstr
, len
);
3665 qemu_put_be32(f
, se
->instance_id
);
3666 qemu_put_be32(f
, se
->version_id
);
3668 /* record size: filled later */
3670 qemu_put_be32(f
, 0);
3672 se
->save_state(f
, se
->opaque
);
3674 /* fill record size */
3676 len
= ftell(f
) - len_pos
- 4;
3677 fseek(f
, len_pos
, SEEK_SET
);
3678 qemu_put_be32(f
, len
);
3679 fseek(f
, cur_pos
, SEEK_SET
);
3685 if (saved_vm_running
)
3690 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
3694 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3695 if (!strcmp(se
->idstr
, idstr
) &&
3696 instance_id
== se
->instance_id
)
3702 int qemu_loadvm(const char *filename
)
3706 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
3707 int saved_vm_running
;
3711 saved_vm_running
= vm_running
;
3714 f
= fopen(filename
, "rb");
3720 v
= qemu_get_be32(f
);
3721 if (v
!= QEMU_VM_FILE_MAGIC
)
3723 v
= qemu_get_be32(f
);
3724 if (v
!= QEMU_VM_FILE_VERSION
) {
3731 len
= qemu_get_byte(f
);
3734 qemu_get_buffer(f
, idstr
, len
);
3736 instance_id
= qemu_get_be32(f
);
3737 version_id
= qemu_get_be32(f
);
3738 record_len
= qemu_get_be32(f
);
3740 printf("idstr=%s instance=0x%x version=%d len=%d\n",
3741 idstr
, instance_id
, version_id
, record_len
);
3744 se
= find_se(idstr
, instance_id
);
3746 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
3747 instance_id
, idstr
);
3749 ret
= se
->load_state(f
, se
->opaque
, version_id
);
3751 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
3752 instance_id
, idstr
);
3755 /* always seek to exact end of record */
3756 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
3761 if (saved_vm_running
)
3766 /***********************************************************/
3767 /* cpu save/restore */
3769 #if defined(TARGET_I386)
3771 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
3773 qemu_put_be32(f
, dt
->selector
);
3774 qemu_put_betl(f
, dt
->base
);
3775 qemu_put_be32(f
, dt
->limit
);
3776 qemu_put_be32(f
, dt
->flags
);
3779 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
3781 dt
->selector
= qemu_get_be32(f
);
3782 dt
->base
= qemu_get_betl(f
);
3783 dt
->limit
= qemu_get_be32(f
);
3784 dt
->flags
= qemu_get_be32(f
);
3787 void cpu_save(QEMUFile
*f
, void *opaque
)
3789 CPUState
*env
= opaque
;
3790 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
3794 for(i
= 0; i
< CPU_NB_REGS
; i
++)
3795 qemu_put_betls(f
, &env
->regs
[i
]);
3796 qemu_put_betls(f
, &env
->eip
);
3797 qemu_put_betls(f
, &env
->eflags
);
3798 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
3799 qemu_put_be32s(f
, &hflags
);
3803 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
3805 for(i
= 0; i
< 8; i
++) {
3806 fptag
|= ((!env
->fptags
[i
]) << i
);
3809 qemu_put_be16s(f
, &fpuc
);
3810 qemu_put_be16s(f
, &fpus
);
3811 qemu_put_be16s(f
, &fptag
);
3813 #ifdef USE_X86LDOUBLE
3818 qemu_put_be16s(f
, &fpregs_format
);
3820 for(i
= 0; i
< 8; i
++) {
3821 #ifdef USE_X86LDOUBLE
3825 /* we save the real CPU data (in case of MMX usage only 'mant'
3826 contains the MMX register */
3827 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
3828 qemu_put_be64(f
, mant
);
3829 qemu_put_be16(f
, exp
);
3832 /* if we use doubles for float emulation, we save the doubles to
3833 avoid losing information in case of MMX usage. It can give
3834 problems if the image is restored on a CPU where long
3835 doubles are used instead. */
3836 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
3840 for(i
= 0; i
< 6; i
++)
3841 cpu_put_seg(f
, &env
->segs
[i
]);
3842 cpu_put_seg(f
, &env
->ldt
);
3843 cpu_put_seg(f
, &env
->tr
);
3844 cpu_put_seg(f
, &env
->gdt
);
3845 cpu_put_seg(f
, &env
->idt
);
3847 qemu_put_be32s(f
, &env
->sysenter_cs
);
3848 qemu_put_be32s(f
, &env
->sysenter_esp
);
3849 qemu_put_be32s(f
, &env
->sysenter_eip
);
3851 qemu_put_betls(f
, &env
->cr
[0]);
3852 qemu_put_betls(f
, &env
->cr
[2]);
3853 qemu_put_betls(f
, &env
->cr
[3]);
3854 qemu_put_betls(f
, &env
->cr
[4]);
3856 for(i
= 0; i
< 8; i
++)
3857 qemu_put_betls(f
, &env
->dr
[i
]);
3860 qemu_put_be32s(f
, &env
->a20_mask
);
3863 qemu_put_be32s(f
, &env
->mxcsr
);
3864 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
3865 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
3866 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
3869 #ifdef TARGET_X86_64
3870 qemu_put_be64s(f
, &env
->efer
);
3871 qemu_put_be64s(f
, &env
->star
);
3872 qemu_put_be64s(f
, &env
->lstar
);
3873 qemu_put_be64s(f
, &env
->cstar
);
3874 qemu_put_be64s(f
, &env
->fmask
);
3875 qemu_put_be64s(f
, &env
->kernelgsbase
);
3879 #ifdef USE_X86LDOUBLE
3880 /* XXX: add that in a FPU generic layer */
3881 union x86_longdouble
{
3886 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
3887 #define EXPBIAS1 1023
3888 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
3889 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
3891 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
3895 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
3896 /* exponent + sign */
3897 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
3898 e
|= SIGND1(temp
) >> 16;
3903 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
3905 CPUState
*env
= opaque
;
3908 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
3910 if (version_id
!= 3)
3912 for(i
= 0; i
< CPU_NB_REGS
; i
++)
3913 qemu_get_betls(f
, &env
->regs
[i
]);
3914 qemu_get_betls(f
, &env
->eip
);
3915 qemu_get_betls(f
, &env
->eflags
);
3916 qemu_get_be32s(f
, &hflags
);
3918 qemu_get_be16s(f
, &fpuc
);
3919 qemu_get_be16s(f
, &fpus
);
3920 qemu_get_be16s(f
, &fptag
);
3921 qemu_get_be16s(f
, &fpregs_format
);
3923 /* NOTE: we cannot always restore the FPU state if the image come
3924 from a host with a different 'USE_X86LDOUBLE' define. We guess
3925 if we are in an MMX state to restore correctly in that case. */
3926 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
3927 for(i
= 0; i
< 8; i
++) {
3931 switch(fpregs_format
) {
3933 mant
= qemu_get_be64(f
);
3934 exp
= qemu_get_be16(f
);
3935 #ifdef USE_X86LDOUBLE
3936 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
3938 /* difficult case */
3940 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
3942 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
3946 mant
= qemu_get_be64(f
);
3947 #ifdef USE_X86LDOUBLE
3949 union x86_longdouble
*p
;
3950 /* difficult case */
3951 p
= (void *)&env
->fpregs
[i
];
3956 fp64_to_fp80(p
, mant
);
3960 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
3969 /* XXX: restore FPU round state */
3970 env
->fpstt
= (fpus
>> 11) & 7;
3971 env
->fpus
= fpus
& ~0x3800;
3973 for(i
= 0; i
< 8; i
++) {
3974 env
->fptags
[i
] = (fptag
>> i
) & 1;
3977 for(i
= 0; i
< 6; i
++)
3978 cpu_get_seg(f
, &env
->segs
[i
]);
3979 cpu_get_seg(f
, &env
->ldt
);
3980 cpu_get_seg(f
, &env
->tr
);
3981 cpu_get_seg(f
, &env
->gdt
);
3982 cpu_get_seg(f
, &env
->idt
);
3984 qemu_get_be32s(f
, &env
->sysenter_cs
);
3985 qemu_get_be32s(f
, &env
->sysenter_esp
);
3986 qemu_get_be32s(f
, &env
->sysenter_eip
);
3988 qemu_get_betls(f
, &env
->cr
[0]);
3989 qemu_get_betls(f
, &env
->cr
[2]);
3990 qemu_get_betls(f
, &env
->cr
[3]);
3991 qemu_get_betls(f
, &env
->cr
[4]);
3993 for(i
= 0; i
< 8; i
++)
3994 qemu_get_betls(f
, &env
->dr
[i
]);
3997 qemu_get_be32s(f
, &env
->a20_mask
);
3999 qemu_get_be32s(f
, &env
->mxcsr
);
4000 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
4001 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
4002 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
4005 #ifdef TARGET_X86_64
4006 qemu_get_be64s(f
, &env
->efer
);
4007 qemu_get_be64s(f
, &env
->star
);
4008 qemu_get_be64s(f
, &env
->lstar
);
4009 qemu_get_be64s(f
, &env
->cstar
);
4010 qemu_get_be64s(f
, &env
->fmask
);
4011 qemu_get_be64s(f
, &env
->kernelgsbase
);
4014 /* XXX: compute hflags from scratch, except for CPL and IIF */
4015 env
->hflags
= hflags
;
4020 #elif defined(TARGET_PPC)
4021 void cpu_save(QEMUFile
*f
, void *opaque
)
4025 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4030 #elif defined(TARGET_MIPS)
4031 void cpu_save(QEMUFile
*f
, void *opaque
)
4035 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4040 #elif defined(TARGET_SPARC)
4041 void cpu_save(QEMUFile
*f
, void *opaque
)
4043 CPUState
*env
= opaque
;
4047 for(i
= 0; i
< 8; i
++)
4048 qemu_put_betls(f
, &env
->gregs
[i
]);
4049 for(i
= 0; i
< NWINDOWS
* 16; i
++)
4050 qemu_put_betls(f
, &env
->regbase
[i
]);
4053 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
4059 qemu_put_betl(f
, u
.i
);
4062 qemu_put_betls(f
, &env
->pc
);
4063 qemu_put_betls(f
, &env
->npc
);
4064 qemu_put_betls(f
, &env
->y
);
4066 qemu_put_be32(f
, tmp
);
4067 qemu_put_betls(f
, &env
->fsr
);
4068 qemu_put_betls(f
, &env
->tbr
);
4069 #ifndef TARGET_SPARC64
4070 qemu_put_be32s(f
, &env
->wim
);
4072 for(i
= 0; i
< 16; i
++)
4073 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
4077 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4079 CPUState
*env
= opaque
;
4083 for(i
= 0; i
< 8; i
++)
4084 qemu_get_betls(f
, &env
->gregs
[i
]);
4085 for(i
= 0; i
< NWINDOWS
* 16; i
++)
4086 qemu_get_betls(f
, &env
->regbase
[i
]);
4089 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
4094 u
.i
= qemu_get_betl(f
);
4098 qemu_get_betls(f
, &env
->pc
);
4099 qemu_get_betls(f
, &env
->npc
);
4100 qemu_get_betls(f
, &env
->y
);
4101 tmp
= qemu_get_be32(f
);
4102 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
4103 correctly updated */
4105 qemu_get_betls(f
, &env
->fsr
);
4106 qemu_get_betls(f
, &env
->tbr
);
4107 #ifndef TARGET_SPARC64
4108 qemu_get_be32s(f
, &env
->wim
);
4110 for(i
= 0; i
< 16; i
++)
4111 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
4117 #elif defined(TARGET_ARM)
4119 /* ??? Need to implement these. */
4120 void cpu_save(QEMUFile
*f
, void *opaque
)
4124 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4131 #warning No CPU save/restore functions
4135 /***********************************************************/
4136 /* ram save/restore */
4138 /* we just avoid storing empty pages */
4139 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
4144 for(i
= 1; i
< len
; i
++) {
4148 qemu_put_byte(f
, 1);
4149 qemu_put_byte(f
, v
);
4152 qemu_put_byte(f
, 0);
4153 qemu_put_buffer(f
, buf
, len
);
4156 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
4160 v
= qemu_get_byte(f
);
4163 if (qemu_get_buffer(f
, buf
, len
) != len
)
4167 v
= qemu_get_byte(f
);
4168 memset(buf
, v
, len
);
4176 static void ram_save(QEMUFile
*f
, void *opaque
)
4179 qemu_put_be32(f
, phys_ram_size
);
4180 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
4181 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
4185 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
4189 if (version_id
!= 1)
4191 if (qemu_get_be32(f
) != phys_ram_size
)
4193 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
4194 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
4201 /***********************************************************/
4202 /* machine registration */
4204 QEMUMachine
*first_machine
= NULL
;
4206 int qemu_register_machine(QEMUMachine
*m
)
4209 pm
= &first_machine
;
4217 QEMUMachine
*find_machine(const char *name
)
4221 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4222 if (!strcmp(m
->name
, name
))
4228 /***********************************************************/
4229 /* main execution loop */
4231 void gui_update(void *opaque
)
4233 display_state
.dpy_refresh(&display_state
);
4234 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
4237 struct vm_change_state_entry
{
4238 VMChangeStateHandler
*cb
;
4240 LIST_ENTRY (vm_change_state_entry
) entries
;
4243 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
4245 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
4248 VMChangeStateEntry
*e
;
4250 e
= qemu_mallocz(sizeof (*e
));
4256 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
4260 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
4262 LIST_REMOVE (e
, entries
);
4266 static void vm_state_notify(int running
)
4268 VMChangeStateEntry
*e
;
4270 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
4271 e
->cb(e
->opaque
, running
);
4275 /* XXX: support several handlers */
4276 static VMStopHandler
*vm_stop_cb
;
4277 static void *vm_stop_opaque
;
4279 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4282 vm_stop_opaque
= opaque
;
4286 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4300 void vm_stop(int reason
)
4303 cpu_disable_ticks();
4307 vm_stop_cb(vm_stop_opaque
, reason
);
4314 /* reset/shutdown handler */
4316 typedef struct QEMUResetEntry
{
4317 QEMUResetHandler
*func
;
4319 struct QEMUResetEntry
*next
;
4322 static QEMUResetEntry
*first_reset_entry
;
4323 static int reset_requested
;
4324 static int shutdown_requested
;
4325 static int powerdown_requested
;
4327 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
4329 QEMUResetEntry
**pre
, *re
;
4331 pre
= &first_reset_entry
;
4332 while (*pre
!= NULL
)
4333 pre
= &(*pre
)->next
;
4334 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
4336 re
->opaque
= opaque
;
4341 void qemu_system_reset(void)
4345 /* reset all devices */
4346 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
4347 re
->func(re
->opaque
);
4351 void qemu_system_reset_request(void)
4353 reset_requested
= 1;
4355 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4358 void qemu_system_shutdown_request(void)
4360 shutdown_requested
= 1;
4362 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4365 void qemu_system_powerdown_request(void)
4367 powerdown_requested
= 1;
4369 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4372 void main_loop_wait(int timeout
)
4374 IOHandlerRecord
*ioh
, *ioh_next
;
4381 /* XXX: need to suppress polling by better using win32 events */
4383 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4384 ret
|= pe
->func(pe
->opaque
);
4387 if (ret
== 0 && timeout
> 0) {
4391 /* poll any events */
4392 /* XXX: separate device handlers from system ones */
4396 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4398 (!ioh
->fd_read_poll
||
4399 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4400 FD_SET(ioh
->fd
, &rfds
);
4404 if (ioh
->fd_write
) {
4405 FD_SET(ioh
->fd
, &wfds
);
4415 tv
.tv_usec
= timeout
* 1000;
4417 ret
= select(nfds
+ 1, &rfds
, &wfds
, NULL
, &tv
);
4419 /* XXX: better handling of removal */
4420 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
4421 ioh_next
= ioh
->next
;
4422 if (FD_ISSET(ioh
->fd
, &rfds
)) {
4423 ioh
->fd_read(ioh
->opaque
);
4425 if (FD_ISSET(ioh
->fd
, &wfds
)) {
4426 ioh
->fd_write(ioh
->opaque
);
4434 #if defined(CONFIG_SLIRP)
4435 /* XXX: merge with the previous select() */
4437 fd_set rfds
, wfds
, xfds
;
4445 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4448 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4450 slirp_select_poll(&rfds
, &wfds
, &xfds
);
4456 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4457 qemu_get_clock(vm_clock
));
4458 /* run dma transfers, if any */
4462 /* real time timers */
4463 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4464 qemu_get_clock(rt_clock
));
4467 static CPUState
*cur_cpu
;
4472 #ifdef CONFIG_PROFILER
4477 cur_cpu
= first_cpu
;
4484 env
= env
->next_cpu
;
4487 #ifdef CONFIG_PROFILER
4488 ti
= profile_getclock();
4490 ret
= cpu_exec(env
);
4491 #ifdef CONFIG_PROFILER
4492 qemu_time
+= profile_getclock() - ti
;
4494 if (ret
!= EXCP_HALTED
)
4496 /* all CPUs are halted ? */
4497 if (env
== cur_cpu
) {
4504 if (shutdown_requested
) {
4505 ret
= EXCP_INTERRUPT
;
4508 if (reset_requested
) {
4509 reset_requested
= 0;
4510 qemu_system_reset();
4511 ret
= EXCP_INTERRUPT
;
4513 if (powerdown_requested
) {
4514 powerdown_requested
= 0;
4515 qemu_system_powerdown();
4516 ret
= EXCP_INTERRUPT
;
4518 if (ret
== EXCP_DEBUG
) {
4519 vm_stop(EXCP_DEBUG
);
4521 /* if hlt instruction, we wait until the next IRQ */
4522 /* XXX: use timeout computed from timers */
4523 if (ret
== EXCP_HLT
)
4530 #ifdef CONFIG_PROFILER
4531 ti
= profile_getclock();
4533 main_loop_wait(timeout
);
4534 #ifdef CONFIG_PROFILER
4535 dev_time
+= profile_getclock() - ti
;
4538 cpu_disable_ticks();
4544 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2005 Fabrice Bellard\n"
4545 "usage: %s [options] [disk_image]\n"
4547 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4549 "Standard options:\n"
4550 "-M machine select emulated machine (-M ? for list)\n"
4551 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
4552 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
4553 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
4554 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
4555 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
4556 "-snapshot write to temporary files instead of disk image files\n"
4557 "-m megs set virtual RAM size to megs MB [default=%d]\n"
4558 "-smp n set the number of CPUs to 'n' [default=1]\n"
4559 "-nographic disable graphical output and redirect serial I/Os to console\n"
4561 "-k language use keyboard layout (for example \"fr\" for French)\n"
4564 "-audio-help print list of audio drivers and their options\n"
4565 "-soundhw c1,... enable audio support\n"
4566 " and only specified sound cards (comma separated list)\n"
4567 " use -soundhw ? to get the list of supported cards\n"
4568 " use -soundhw all to enable all of them\n"
4570 "-localtime set the real time clock to local time [default=utc]\n"
4571 "-full-screen start in full screen\n"
4573 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4575 "-usb enable the USB driver (will be the default soon)\n"
4576 "-usbdevice name add the host or guest USB device 'name'\n"
4577 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4578 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4581 "Network options:\n"
4582 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
4583 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4585 "-net user[,vlan=n][,hostname=host]\n"
4586 " connect the user mode network stack to VLAN 'n' and send\n"
4587 " hostname 'host' to DHCP clients\n"
4590 "-net tap[,vlan=n],ifname=name\n"
4591 " connect the host TAP network interface to VLAN 'n'\n"
4593 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
4594 " connect the host TAP network interface to VLAN 'n' and use\n"
4595 " the network script 'file' (default=%s);\n"
4596 " use 'fd=h' to connect to an already opened TAP interface\n"
4598 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4599 " connect the vlan 'n' to another VLAN using a socket connection\n"
4600 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
4601 " connect the vlan 'n' to multicast maddr and port\n"
4602 "-net none use it alone to have zero network devices; if no -net option\n"
4603 " is provided, the default is '-net nic -net user'\n"
4606 "-tftp prefix allow tftp access to files starting with prefix [-net user]\n"
4608 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4610 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4611 " redirect TCP or UDP connections from host to guest [-net user]\n"
4614 "Linux boot specific:\n"
4615 "-kernel bzImage use 'bzImage' as kernel image\n"
4616 "-append cmdline use 'cmdline' as kernel command line\n"
4617 "-initrd file use 'file' as initial ram disk\n"
4619 "Debug/Expert options:\n"
4620 "-monitor dev redirect the monitor to char device 'dev'\n"
4621 "-serial dev redirect the serial port to char device 'dev'\n"
4622 "-parallel dev redirect the parallel port to char device 'dev'\n"
4623 "-pidfile file Write PID to 'file'\n"
4624 "-S freeze CPU at startup (use 'c' to start execution)\n"
4625 "-s wait gdb connection to port %d\n"
4626 "-p port change gdb connection port\n"
4627 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4628 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
4629 " translation (t=none or lba) (usually qemu can guess them)\n"
4630 "-L path set the directory for the BIOS and VGA BIOS\n"
4632 "-no-kqemu disable KQEMU kernel module usage\n"
4634 #ifdef USE_CODE_COPY
4635 "-no-code-copy disable code copy acceleration\n"
4638 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
4639 " (default is CL-GD5446 PCI VGA)\n"
4641 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
4642 "-vnc display start a VNC server on display\n"
4644 "During emulation, the following keys are useful:\n"
4645 "ctrl-alt-f toggle full screen\n"
4646 "ctrl-alt-n switch to virtual console 'n'\n"
4647 "ctrl-alt toggle mouse and keyboard grab\n"
4649 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4651 #ifdef CONFIG_SOFTMMU
4658 DEFAULT_NETWORK_SCRIPT
,
4660 DEFAULT_GDBSTUB_PORT
,
4662 #ifndef CONFIG_SOFTMMU
4664 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
4665 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
4671 #define HAS_ARG 0x0001
4685 QEMU_OPTION_snapshot
,
4687 QEMU_OPTION_nographic
,
4689 QEMU_OPTION_audio_help
,
4690 QEMU_OPTION_soundhw
,
4708 QEMU_OPTION_no_code_copy
,
4710 QEMU_OPTION_localtime
,
4711 QEMU_OPTION_cirrusvga
,
4713 QEMU_OPTION_std_vga
,
4714 QEMU_OPTION_monitor
,
4716 QEMU_OPTION_parallel
,
4718 QEMU_OPTION_full_screen
,
4719 QEMU_OPTION_pidfile
,
4720 QEMU_OPTION_no_kqemu
,
4721 QEMU_OPTION_kernel_kqemu
,
4722 QEMU_OPTION_win2k_hack
,
4724 QEMU_OPTION_usbdevice
,
4729 typedef struct QEMUOption
{
4735 const QEMUOption qemu_options
[] = {
4736 { "h", 0, QEMU_OPTION_h
},
4738 { "M", HAS_ARG
, QEMU_OPTION_M
},
4739 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4740 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4741 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4742 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4743 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4744 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4745 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4746 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4747 { "snapshot", 0, QEMU_OPTION_snapshot
},
4748 { "m", HAS_ARG
, QEMU_OPTION_m
},
4749 { "nographic", 0, QEMU_OPTION_nographic
},
4750 { "k", HAS_ARG
, QEMU_OPTION_k
},
4752 { "audio-help", 0, QEMU_OPTION_audio_help
},
4753 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4756 { "net", HAS_ARG
, QEMU_OPTION_net
},
4758 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4760 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4762 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4765 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4766 { "append", HAS_ARG
, QEMU_OPTION_append
},
4767 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4769 { "S", 0, QEMU_OPTION_S
},
4770 { "s", 0, QEMU_OPTION_s
},
4771 { "p", HAS_ARG
, QEMU_OPTION_p
},
4772 { "d", HAS_ARG
, QEMU_OPTION_d
},
4773 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4774 { "L", HAS_ARG
, QEMU_OPTION_L
},
4775 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
4777 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4778 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4780 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4781 { "g", 1, QEMU_OPTION_g
},
4783 { "localtime", 0, QEMU_OPTION_localtime
},
4784 { "std-vga", 0, QEMU_OPTION_std_vga
},
4785 { "monitor", 1, QEMU_OPTION_monitor
},
4786 { "serial", 1, QEMU_OPTION_serial
},
4787 { "parallel", 1, QEMU_OPTION_parallel
},
4788 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4789 { "full-screen", 0, QEMU_OPTION_full_screen
},
4790 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4791 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4792 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4793 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4794 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4796 /* temporary options */
4797 { "usb", 0, QEMU_OPTION_usb
},
4798 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
4802 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
4804 /* this stack is only used during signal handling */
4805 #define SIGNAL_STACK_SIZE 32768
4807 static uint8_t *signal_stack
;
4811 /* password input */
4813 static BlockDriverState
*get_bdrv(int index
)
4815 BlockDriverState
*bs
;
4818 bs
= bs_table
[index
];
4819 } else if (index
< 6) {
4820 bs
= fd_table
[index
- 4];
4827 static void read_passwords(void)
4829 BlockDriverState
*bs
;
4833 for(i
= 0; i
< 6; i
++) {
4835 if (bs
&& bdrv_is_encrypted(bs
)) {
4836 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
4837 for(j
= 0; j
< 3; j
++) {
4838 monitor_readline("Password: ",
4839 1, password
, sizeof(password
));
4840 if (bdrv_set_key(bs
, password
) == 0)
4842 term_printf("invalid password\n");
4848 /* XXX: currently we cannot use simultaneously different CPUs */
4849 void register_machines(void)
4851 #if defined(TARGET_I386)
4852 qemu_register_machine(&pc_machine
);
4853 qemu_register_machine(&isapc_machine
);
4854 #elif defined(TARGET_PPC)
4855 qemu_register_machine(&heathrow_machine
);
4856 qemu_register_machine(&core99_machine
);
4857 qemu_register_machine(&prep_machine
);
4858 #elif defined(TARGET_MIPS)
4859 qemu_register_machine(&mips_machine
);
4860 #elif defined(TARGET_SPARC)
4861 #ifdef TARGET_SPARC64
4862 qemu_register_machine(&sun4u_machine
);
4864 qemu_register_machine(&sun4m_machine
);
4866 #elif defined(TARGET_ARM)
4867 qemu_register_machine(&integratorcp926_machine
);
4868 qemu_register_machine(&integratorcp1026_machine
);
4869 qemu_register_machine(&versatilepb_machine
);
4870 qemu_register_machine(&versatileab_machine
);
4871 #elif defined(TARGET_SH4)
4872 qemu_register_machine(&shix_machine
);
4874 #error unsupported CPU
4879 struct soundhw soundhw
[] = {
4886 { .init_isa
= pcspk_audio_init
}
4891 "Creative Sound Blaster 16",
4894 { .init_isa
= SB16_init
}
4901 "Yamaha YMF262 (OPL3)",
4903 "Yamaha YM3812 (OPL2)",
4907 { .init_isa
= Adlib_init
}
4914 "Gravis Ultrasound GF1",
4917 { .init_isa
= GUS_init
}
4923 "ENSONIQ AudioPCI ES1370",
4926 { .init_pci
= es1370_init
}
4929 { NULL
, NULL
, 0, 0, { NULL
} }
4932 static void select_soundhw (const char *optarg
)
4936 if (*optarg
== '?') {
4939 printf ("Valid sound card names (comma separated):\n");
4940 for (c
= soundhw
; c
->name
; ++c
) {
4941 printf ("%-11s %s\n", c
->name
, c
->descr
);
4943 printf ("\n-soundhw all will enable all of the above\n");
4944 exit (*optarg
!= '?');
4952 if (!strcmp (optarg
, "all")) {
4953 for (c
= soundhw
; c
->name
; ++c
) {
4961 e
= strchr (p
, ',');
4962 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4964 for (c
= soundhw
; c
->name
; ++c
) {
4965 if (!strncmp (c
->name
, p
, l
)) {
4974 "Unknown sound card name (too big to show)\n");
4977 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4982 p
+= l
+ (e
!= NULL
);
4986 goto show_valid_cards
;
4991 #define MAX_NET_CLIENTS 32
4993 int main(int argc
, char **argv
)
4995 #ifdef CONFIG_GDBSTUB
4996 int use_gdbstub
, gdbstub_port
;
4999 int snapshot
, linux_boot
;
5000 const char *initrd_filename
;
5001 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
5002 const char *kernel_filename
, *kernel_cmdline
;
5003 DisplayState
*ds
= &display_state
;
5004 int cyls
, heads
, secs
, translation
;
5005 int start_emulation
= 1;
5006 char net_clients
[MAX_NET_CLIENTS
][256];
5009 const char *r
, *optarg
;
5010 CharDriverState
*monitor_hd
;
5011 char monitor_device
[128];
5012 char serial_devices
[MAX_SERIAL_PORTS
][128];
5013 int serial_device_index
;
5014 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
5015 int parallel_device_index
;
5016 const char *loadvm
= NULL
;
5017 QEMUMachine
*machine
;
5018 char usb_devices
[MAX_VM_USB_PORTS
][128];
5019 int usb_devices_index
;
5021 LIST_INIT (&vm_change_state_head
);
5022 #if !defined(CONFIG_SOFTMMU)
5023 /* we never want that malloc() uses mmap() */
5024 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
5026 register_machines();
5027 machine
= first_machine
;
5028 initrd_filename
= NULL
;
5029 for(i
= 0; i
< MAX_FD
; i
++)
5030 fd_filename
[i
] = NULL
;
5031 for(i
= 0; i
< MAX_DISKS
; i
++)
5032 hd_filename
[i
] = NULL
;
5033 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5034 vga_ram_size
= VGA_RAM_SIZE
;
5035 bios_size
= BIOS_SIZE
;
5036 #ifdef CONFIG_GDBSTUB
5038 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
5042 kernel_filename
= NULL
;
5043 kernel_cmdline
= "";
5049 cyls
= heads
= secs
= 0;
5050 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5051 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
5053 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
5054 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
5055 serial_devices
[i
][0] = '\0';
5056 serial_device_index
= 0;
5058 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
5059 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
5060 parallel_devices
[i
][0] = '\0';
5061 parallel_device_index
= 0;
5063 usb_devices_index
= 0;
5068 /* default mac address of the first network interface */
5076 hd_filename
[0] = argv
[optind
++];
5078 const QEMUOption
*popt
;
5081 popt
= qemu_options
;
5084 fprintf(stderr
, "%s: invalid option -- '%s'\n",
5088 if (!strcmp(popt
->name
, r
+ 1))
5092 if (popt
->flags
& HAS_ARG
) {
5093 if (optind
>= argc
) {
5094 fprintf(stderr
, "%s: option '%s' requires an argument\n",
5098 optarg
= argv
[optind
++];
5103 switch(popt
->index
) {
5105 machine
= find_machine(optarg
);
5108 printf("Supported machines are:\n");
5109 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5110 printf("%-10s %s%s\n",
5112 m
== first_machine
? " (default)" : "");
5117 case QEMU_OPTION_initrd
:
5118 initrd_filename
= optarg
;
5120 case QEMU_OPTION_hda
:
5121 case QEMU_OPTION_hdb
:
5122 case QEMU_OPTION_hdc
:
5123 case QEMU_OPTION_hdd
:
5126 hd_index
= popt
->index
- QEMU_OPTION_hda
;
5127 hd_filename
[hd_index
] = optarg
;
5128 if (hd_index
== cdrom_index
)
5132 case QEMU_OPTION_snapshot
:
5135 case QEMU_OPTION_hdachs
:
5139 cyls
= strtol(p
, (char **)&p
, 0);
5140 if (cyls
< 1 || cyls
> 16383)
5145 heads
= strtol(p
, (char **)&p
, 0);
5146 if (heads
< 1 || heads
> 16)
5151 secs
= strtol(p
, (char **)&p
, 0);
5152 if (secs
< 1 || secs
> 63)
5156 if (!strcmp(p
, "none"))
5157 translation
= BIOS_ATA_TRANSLATION_NONE
;
5158 else if (!strcmp(p
, "lba"))
5159 translation
= BIOS_ATA_TRANSLATION_LBA
;
5160 else if (!strcmp(p
, "auto"))
5161 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5164 } else if (*p
!= '\0') {
5166 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5171 case QEMU_OPTION_nographic
:
5172 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
5173 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
5176 case QEMU_OPTION_kernel
:
5177 kernel_filename
= optarg
;
5179 case QEMU_OPTION_append
:
5180 kernel_cmdline
= optarg
;
5182 case QEMU_OPTION_cdrom
:
5183 if (cdrom_index
>= 0) {
5184 hd_filename
[cdrom_index
] = optarg
;
5187 case QEMU_OPTION_boot
:
5188 boot_device
= optarg
[0];
5189 if (boot_device
!= 'a' &&
5192 boot_device
!= 'n' &&
5194 boot_device
!= 'c' && boot_device
!= 'd') {
5195 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
5199 case QEMU_OPTION_fda
:
5200 fd_filename
[0] = optarg
;
5202 case QEMU_OPTION_fdb
:
5203 fd_filename
[1] = optarg
;
5205 case QEMU_OPTION_no_code_copy
:
5206 code_copy_enabled
= 0;
5208 case QEMU_OPTION_net
:
5209 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5210 fprintf(stderr
, "qemu: too many network clients\n");
5213 pstrcpy(net_clients
[nb_net_clients
],
5214 sizeof(net_clients
[0]),
5219 case QEMU_OPTION_tftp
:
5220 tftp_prefix
= optarg
;
5223 case QEMU_OPTION_smb
:
5224 net_slirp_smb(optarg
);
5227 case QEMU_OPTION_redir
:
5228 net_slirp_redir(optarg
);
5232 case QEMU_OPTION_audio_help
:
5236 case QEMU_OPTION_soundhw
:
5237 select_soundhw (optarg
);
5244 ram_size
= atoi(optarg
) * 1024 * 1024;
5247 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
5248 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
5249 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
5258 mask
= cpu_str_to_log_mask(optarg
);
5260 printf("Log items (comma separated):\n");
5261 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5262 printf("%-10s %s\n", item
->name
, item
->help
);
5269 #ifdef CONFIG_GDBSTUB
5274 gdbstub_port
= atoi(optarg
);
5281 start_emulation
= 0;
5284 keyboard_layout
= optarg
;
5286 case QEMU_OPTION_localtime
:
5289 case QEMU_OPTION_cirrusvga
:
5290 cirrus_vga_enabled
= 1;
5292 case QEMU_OPTION_std_vga
:
5293 cirrus_vga_enabled
= 0;
5300 w
= strtol(p
, (char **)&p
, 10);
5303 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5309 h
= strtol(p
, (char **)&p
, 10);
5314 depth
= strtol(p
, (char **)&p
, 10);
5315 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5316 depth
!= 24 && depth
!= 32)
5318 } else if (*p
== '\0') {
5319 depth
= graphic_depth
;
5326 graphic_depth
= depth
;
5329 case QEMU_OPTION_monitor
:
5330 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
5332 case QEMU_OPTION_serial
:
5333 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5334 fprintf(stderr
, "qemu: too many serial ports\n");
5337 pstrcpy(serial_devices
[serial_device_index
],
5338 sizeof(serial_devices
[0]), optarg
);
5339 serial_device_index
++;
5341 case QEMU_OPTION_parallel
:
5342 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5343 fprintf(stderr
, "qemu: too many parallel ports\n");
5346 pstrcpy(parallel_devices
[parallel_device_index
],
5347 sizeof(parallel_devices
[0]), optarg
);
5348 parallel_device_index
++;
5350 case QEMU_OPTION_loadvm
:
5353 case QEMU_OPTION_full_screen
:
5356 case QEMU_OPTION_pidfile
:
5357 create_pidfile(optarg
);
5360 case QEMU_OPTION_win2k_hack
:
5361 win2k_install_hack
= 1;
5365 case QEMU_OPTION_no_kqemu
:
5368 case QEMU_OPTION_kernel_kqemu
:
5372 case QEMU_OPTION_usb
:
5375 case QEMU_OPTION_usbdevice
:
5377 if (usb_devices_index
>= MAX_VM_USB_PORTS
) {
5378 fprintf(stderr
, "Too many USB devices\n");
5381 pstrcpy(usb_devices
[usb_devices_index
],
5382 sizeof(usb_devices
[usb_devices_index
]),
5384 usb_devices_index
++;
5386 case QEMU_OPTION_smp
:
5387 smp_cpus
= atoi(optarg
);
5388 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
5389 fprintf(stderr
, "Invalid number of CPUs\n");
5393 case QEMU_OPTION_vnc
:
5394 vnc_display
= atoi(optarg
);
5395 if (vnc_display
< 0) {
5396 fprintf(stderr
, "Invalid VNC display\n");
5408 linux_boot
= (kernel_filename
!= NULL
);
5411 hd_filename
[0] == '\0' &&
5412 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
5413 fd_filename
[0] == '\0')
5416 /* boot to cd by default if no hard disk */
5417 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
5418 if (fd_filename
[0] != '\0')
5424 #if !defined(CONFIG_SOFTMMU)
5425 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
5427 static uint8_t stdout_buf
[4096];
5428 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
5431 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5438 /* init network clients */
5439 if (nb_net_clients
== 0) {
5440 /* if no clients, we use a default config */
5441 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
5443 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
5448 for(i
= 0;i
< nb_net_clients
; i
++) {
5449 if (net_client_init(net_clients
[i
]) < 0)
5453 /* init the memory */
5454 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
5456 #ifdef CONFIG_SOFTMMU
5457 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5458 if (!phys_ram_base
) {
5459 fprintf(stderr
, "Could not allocate physical memory\n");
5463 /* as we must map the same page at several addresses, we must use
5468 tmpdir
= getenv("QEMU_TMPDIR");
5471 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
5472 if (mkstemp(phys_ram_file
) < 0) {
5473 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
5477 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
5478 if (phys_ram_fd
< 0) {
5479 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
5483 ftruncate(phys_ram_fd
, phys_ram_size
);
5484 unlink(phys_ram_file
);
5485 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
5487 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
5489 if (phys_ram_base
== MAP_FAILED
) {
5490 fprintf(stderr
, "Could not map physical memory\n");
5496 /* we always create the cdrom drive, even if no disk is there */
5498 if (cdrom_index
>= 0) {
5499 bs_table
[cdrom_index
] = bdrv_new("cdrom");
5500 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
5503 /* open the virtual block devices */
5504 for(i
= 0; i
< MAX_DISKS
; i
++) {
5505 if (hd_filename
[i
]) {
5508 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
5509 bs_table
[i
] = bdrv_new(buf
);
5511 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
5512 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
5516 if (i
== 0 && cyls
!= 0) {
5517 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
5518 bdrv_set_translation_hint(bs_table
[i
], translation
);
5523 /* we always create at least one floppy disk */
5524 fd_table
[0] = bdrv_new("fda");
5525 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
5527 for(i
= 0; i
< MAX_FD
; i
++) {
5528 if (fd_filename
[i
]) {
5531 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
5532 fd_table
[i
] = bdrv_new(buf
);
5533 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
5535 if (fd_filename
[i
] != '\0') {
5536 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
5537 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
5545 /* init USB devices */
5547 vm_usb_hub
= usb_hub_init(vm_usb_ports
, MAX_VM_USB_PORTS
);
5548 for(i
= 0; i
< usb_devices_index
; i
++) {
5549 if (usb_device_add(usb_devices
[i
]) < 0) {
5550 fprintf(stderr
, "Warning: could not add USB device %s\n",
5556 register_savevm("timer", 0, 1, timer_save
, timer_load
, NULL
);
5557 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
5560 cpu_calibrate_ticks();
5564 dumb_display_init(ds
);
5565 } if (vnc_display
!= -1) {
5566 vnc_display_init(ds
, vnc_display
);
5568 #if defined(CONFIG_SDL)
5569 sdl_display_init(ds
, full_screen
);
5570 #elif defined(CONFIG_COCOA)
5571 cocoa_display_init(ds
, full_screen
);
5573 dumb_display_init(ds
);
5577 monitor_hd
= qemu_chr_open(monitor_device
);
5579 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5582 monitor_init(monitor_hd
, !nographic
);
5584 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5585 if (serial_devices
[i
][0] != '\0') {
5586 serial_hds
[i
] = qemu_chr_open(serial_devices
[i
]);
5587 if (!serial_hds
[i
]) {
5588 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5592 if (!strcmp(serial_devices
[i
], "vc"))
5593 qemu_chr_printf(serial_hds
[i
], "serial%d console\n", i
);
5597 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5598 if (parallel_devices
[i
][0] != '\0') {
5599 parallel_hds
[i
] = qemu_chr_open(parallel_devices
[i
]);
5600 if (!parallel_hds
[i
]) {
5601 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5602 parallel_devices
[i
]);
5605 if (!strcmp(parallel_devices
[i
], "vc"))
5606 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\n", i
);
5610 /* setup cpu signal handlers for MMU / self modifying code handling */
5611 #if !defined(CONFIG_SOFTMMU)
5613 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5616 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
5617 stk
.ss_sp
= signal_stack
;
5618 stk
.ss_size
= SIGNAL_STACK_SIZE
;
5621 if (sigaltstack(&stk
, NULL
) < 0) {
5622 perror("sigaltstack");
5628 struct sigaction act
;
5630 sigfillset(&act
.sa_mask
);
5631 act
.sa_flags
= SA_SIGINFO
;
5632 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5633 act
.sa_flags
|= SA_ONSTACK
;
5635 act
.sa_sigaction
= host_segv_handler
;
5636 sigaction(SIGSEGV
, &act
, NULL
);
5637 sigaction(SIGBUS
, &act
, NULL
);
5638 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5639 sigaction(SIGFPE
, &act
, NULL
);
5646 struct sigaction act
;
5647 sigfillset(&act
.sa_mask
);
5649 act
.sa_handler
= SIG_IGN
;
5650 sigaction(SIGPIPE
, &act
, NULL
);
5655 machine
->init(ram_size
, vga_ram_size
, boot_device
,
5656 ds
, fd_filename
, snapshot
,
5657 kernel_filename
, kernel_cmdline
, initrd_filename
);
5659 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
5660 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
5662 #ifdef CONFIG_GDBSTUB
5664 if (gdbserver_start(gdbstub_port
) < 0) {
5665 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
5669 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
5674 qemu_loadvm(loadvm
);
5677 /* XXX: simplify init */
5679 if (start_emulation
) {