4 * Copyright (c) 2003-2005 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 #include <sys/times.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
52 #include <linux/if_tun.h>
55 #include <linux/rtc.h>
56 #include <linux/ppdev.h>
61 #if defined(CONFIG_SLIRP)
67 #include <sys/timeb.h>
69 #define getopt_long_only getopt_long
70 #define memalign(align, size) malloc(size)
73 #include "qemu_socket.h"
79 #endif /* CONFIG_SDL */
83 #define main qemu_main
84 #endif /* CONFIG_COCOA */
90 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
92 //#define DEBUG_UNUSED_IOPORT
93 //#define DEBUG_IOPORT
95 #if !defined(CONFIG_SOFTMMU)
96 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
98 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
102 #define DEFAULT_RAM_SIZE 144
104 #define DEFAULT_RAM_SIZE 128
107 #define GUI_REFRESH_INTERVAL 30
109 /* 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 static void socket_cleanup(void)
1093 static int socket_init(void)
1098 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1100 err
= WSAGetLastError();
1101 fprintf(stderr
, "WSAStartup: %d\n", err
);
1104 atexit(socket_cleanup
);
1108 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1114 ret
= send(fd
, buf
, len
, 0);
1117 errno
= WSAGetLastError();
1118 if (errno
!= WSAEWOULDBLOCK
) {
1121 } else if (ret
== 0) {
1131 void socket_set_nonblock(int fd
)
1133 unsigned long opt
= 1;
1134 ioctlsocket(fd
, FIONBIO
, &opt
);
1139 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1145 ret
= write(fd
, buf
, len
);
1147 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1149 } else if (ret
== 0) {
1159 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1161 return unix_write(fd
, buf
, len1
);
1164 void socket_set_nonblock(int fd
)
1166 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1168 #endif /* !_WIN32 */
1174 IOCanRWHandler
*fd_can_read
;
1175 IOReadHandler
*fd_read
;
1180 #define STDIO_MAX_CLIENTS 2
1182 static int stdio_nb_clients
;
1183 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1185 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1187 FDCharDriver
*s
= chr
->opaque
;
1188 return unix_write(s
->fd_out
, buf
, len
);
1191 static int fd_chr_read_poll(void *opaque
)
1193 CharDriverState
*chr
= opaque
;
1194 FDCharDriver
*s
= chr
->opaque
;
1196 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
1200 static void fd_chr_read(void *opaque
)
1202 CharDriverState
*chr
= opaque
;
1203 FDCharDriver
*s
= chr
->opaque
;
1208 if (len
> s
->max_size
)
1212 size
= read(s
->fd_in
, buf
, len
);
1214 s
->fd_read(s
->fd_opaque
, buf
, size
);
1218 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1219 IOCanRWHandler
*fd_can_read
,
1220 IOReadHandler
*fd_read
, void *opaque
)
1222 FDCharDriver
*s
= chr
->opaque
;
1224 if (s
->fd_in
>= 0) {
1225 s
->fd_can_read
= fd_can_read
;
1226 s
->fd_read
= fd_read
;
1227 s
->fd_opaque
= opaque
;
1228 if (nographic
&& s
->fd_in
== 0) {
1230 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1231 fd_chr_read
, NULL
, chr
);
1236 /* open a character device to a unix fd */
1237 CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1239 CharDriverState
*chr
;
1242 chr
= qemu_mallocz(sizeof(CharDriverState
));
1245 s
= qemu_mallocz(sizeof(FDCharDriver
));
1253 chr
->chr_write
= fd_chr_write
;
1254 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1258 CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1262 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1265 return qemu_chr_open_fd(-1, fd_out
);
1268 CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1272 fd
= open(filename
, O_RDWR
| O_BINARY
);
1275 return qemu_chr_open_fd(fd
, fd
);
1279 /* for STDIO, we handle the case where several clients use it
1282 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1284 #define TERM_FIFO_MAX_SIZE 1
1286 static int term_got_escape
, client_index
;
1287 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1290 void term_print_help(void)
1293 "C-a h print this help\n"
1294 "C-a x exit emulator\n"
1295 "C-a s save disk data back to file (if -snapshot)\n"
1296 "C-a b send break (magic sysrq)\n"
1297 "C-a c switch between console and monitor\n"
1298 "C-a C-a send C-a\n"
1302 /* called when a char is received */
1303 static void stdio_received_byte(int ch
)
1305 if (term_got_escape
) {
1306 term_got_escape
= 0;
1317 for (i
= 0; i
< MAX_DISKS
; i
++) {
1319 bdrv_commit(bs_table
[i
]);
1324 if (client_index
< stdio_nb_clients
) {
1325 CharDriverState
*chr
;
1328 chr
= stdio_clients
[client_index
];
1330 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1335 if (client_index
>= stdio_nb_clients
)
1337 if (client_index
== 0) {
1338 /* send a new line in the monitor to get the prompt */
1346 } else if (ch
== TERM_ESCAPE
) {
1347 term_got_escape
= 1;
1350 if (client_index
< stdio_nb_clients
) {
1352 CharDriverState
*chr
;
1355 chr
= stdio_clients
[client_index
];
1357 if (s
->fd_can_read(s
->fd_opaque
) > 0) {
1359 s
->fd_read(s
->fd_opaque
, buf
, 1);
1360 } else if (term_fifo_size
== 0) {
1361 term_fifo
[term_fifo_size
++] = ch
;
1367 static int stdio_read_poll(void *opaque
)
1369 CharDriverState
*chr
;
1372 if (client_index
< stdio_nb_clients
) {
1373 chr
= stdio_clients
[client_index
];
1375 /* try to flush the queue if needed */
1376 if (term_fifo_size
!= 0 && s
->fd_can_read(s
->fd_opaque
) > 0) {
1377 s
->fd_read(s
->fd_opaque
, term_fifo
, 1);
1380 /* see if we can absorb more chars */
1381 if (term_fifo_size
== 0)
1390 static void stdio_read(void *opaque
)
1395 size
= read(0, buf
, 1);
1397 stdio_received_byte(buf
[0]);
1400 /* init terminal so that we can grab keys */
1401 static struct termios oldtty
;
1402 static int old_fd0_flags
;
1404 static void term_exit(void)
1406 tcsetattr (0, TCSANOW
, &oldtty
);
1407 fcntl(0, F_SETFL
, old_fd0_flags
);
1410 static void term_init(void)
1414 tcgetattr (0, &tty
);
1416 old_fd0_flags
= fcntl(0, F_GETFL
);
1418 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1419 |INLCR
|IGNCR
|ICRNL
|IXON
);
1420 tty
.c_oflag
|= OPOST
;
1421 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1422 /* if graphical mode, we allow Ctrl-C handling */
1424 tty
.c_lflag
&= ~ISIG
;
1425 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1428 tty
.c_cc
[VTIME
] = 0;
1430 tcsetattr (0, TCSANOW
, &tty
);
1434 fcntl(0, F_SETFL
, O_NONBLOCK
);
1437 CharDriverState
*qemu_chr_open_stdio(void)
1439 CharDriverState
*chr
;
1442 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1444 chr
= qemu_chr_open_fd(0, 1);
1445 if (stdio_nb_clients
== 0)
1446 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, NULL
);
1447 client_index
= stdio_nb_clients
;
1449 if (stdio_nb_clients
!= 0)
1451 chr
= qemu_chr_open_fd(0, 1);
1453 stdio_clients
[stdio_nb_clients
++] = chr
;
1454 if (stdio_nb_clients
== 1) {
1455 /* set the terminal in raw mode */
1461 #if defined(__linux__)
1462 CharDriverState
*qemu_chr_open_pty(void)
1465 char slave_name
[1024];
1466 int master_fd
, slave_fd
;
1468 /* Not satisfying */
1469 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1473 /* Disabling local echo and line-buffered output */
1474 tcgetattr (master_fd
, &tty
);
1475 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1477 tty
.c_cc
[VTIME
] = 0;
1478 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1480 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1481 return qemu_chr_open_fd(master_fd
, master_fd
);
1484 static void tty_serial_init(int fd
, int speed
,
1485 int parity
, int data_bits
, int stop_bits
)
1491 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1492 speed
, parity
, data_bits
, stop_bits
);
1494 tcgetattr (fd
, &tty
);
1536 cfsetispeed(&tty
, spd
);
1537 cfsetospeed(&tty
, spd
);
1539 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1540 |INLCR
|IGNCR
|ICRNL
|IXON
);
1541 tty
.c_oflag
|= OPOST
;
1542 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1543 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
);
1564 tty
.c_cflag
|= PARENB
;
1567 tty
.c_cflag
|= PARENB
| PARODD
;
1571 tcsetattr (fd
, TCSANOW
, &tty
);
1574 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1576 FDCharDriver
*s
= chr
->opaque
;
1579 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1581 QEMUSerialSetParams
*ssp
= arg
;
1582 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1583 ssp
->data_bits
, ssp
->stop_bits
);
1586 case CHR_IOCTL_SERIAL_SET_BREAK
:
1588 int enable
= *(int *)arg
;
1590 tcsendbreak(s
->fd_in
, 1);
1599 CharDriverState
*qemu_chr_open_tty(const char *filename
)
1601 CharDriverState
*chr
;
1604 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1607 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1608 tty_serial_init(fd
, 115200, 'N', 8, 1);
1609 chr
= qemu_chr_open_fd(fd
, fd
);
1612 chr
->chr_ioctl
= tty_serial_ioctl
;
1616 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1618 int fd
= (int)chr
->opaque
;
1622 case CHR_IOCTL_PP_READ_DATA
:
1623 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1625 *(uint8_t *)arg
= b
;
1627 case CHR_IOCTL_PP_WRITE_DATA
:
1628 b
= *(uint8_t *)arg
;
1629 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1632 case CHR_IOCTL_PP_READ_CONTROL
:
1633 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1635 *(uint8_t *)arg
= b
;
1637 case CHR_IOCTL_PP_WRITE_CONTROL
:
1638 b
= *(uint8_t *)arg
;
1639 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1642 case CHR_IOCTL_PP_READ_STATUS
:
1643 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1645 *(uint8_t *)arg
= b
;
1653 CharDriverState
*qemu_chr_open_pp(const char *filename
)
1655 CharDriverState
*chr
;
1658 fd
= open(filename
, O_RDWR
);
1662 if (ioctl(fd
, PPCLAIM
) < 0) {
1667 chr
= qemu_mallocz(sizeof(CharDriverState
));
1672 chr
->opaque
= (void *)fd
;
1673 chr
->chr_write
= null_chr_write
;
1674 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1675 chr
->chr_ioctl
= pp_ioctl
;
1680 CharDriverState
*qemu_chr_open_pty(void)
1686 #endif /* !defined(_WIN32) */
1690 IOCanRWHandler
*fd_can_read
;
1691 IOReadHandler
*fd_read
;
1694 HANDLE hcom
, hrecv
, hsend
;
1695 OVERLAPPED orecv
, osend
;
1700 #define NSENDBUF 2048
1701 #define NRECVBUF 2048
1702 #define MAXCONNECT 1
1703 #define NTIMEOUT 5000
1705 static int win_chr_poll(void *opaque
);
1706 static int win_chr_pipe_poll(void *opaque
);
1708 static void win_chr_close2(WinCharState
*s
)
1711 CloseHandle(s
->hsend
);
1715 CloseHandle(s
->hrecv
);
1719 CloseHandle(s
->hcom
);
1723 qemu_del_polling_cb(win_chr_pipe_poll
, s
);
1725 qemu_del_polling_cb(win_chr_poll
, s
);
1728 static void win_chr_close(CharDriverState
*chr
)
1730 WinCharState
*s
= chr
->opaque
;
1734 static int win_chr_init(WinCharState
*s
, const char *filename
)
1737 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1742 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1744 fprintf(stderr
, "Failed CreateEvent\n");
1747 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1749 fprintf(stderr
, "Failed CreateEvent\n");
1753 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1754 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1755 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1756 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1761 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1762 fprintf(stderr
, "Failed SetupComm\n");
1766 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1767 size
= sizeof(COMMCONFIG
);
1768 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1769 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1770 CommConfigDialog(filename
, NULL
, &comcfg
);
1772 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1773 fprintf(stderr
, "Failed SetCommState\n");
1777 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1778 fprintf(stderr
, "Failed SetCommMask\n");
1782 cto
.ReadIntervalTimeout
= MAXDWORD
;
1783 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1784 fprintf(stderr
, "Failed SetCommTimeouts\n");
1788 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1789 fprintf(stderr
, "Failed ClearCommError\n");
1792 qemu_add_polling_cb(win_chr_poll
, s
);
1800 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1802 WinCharState
*s
= chr
->opaque
;
1803 DWORD len
, ret
, size
, err
;
1806 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1807 s
->osend
.hEvent
= s
->hsend
;
1810 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1812 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1814 err
= GetLastError();
1815 if (err
== ERROR_IO_PENDING
) {
1816 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1834 static int win_chr_read_poll(WinCharState
*s
)
1836 s
->max_size
= s
->fd_can_read(s
->win_opaque
);
1840 static void win_chr_readfile(WinCharState
*s
)
1846 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1847 s
->orecv
.hEvent
= s
->hrecv
;
1848 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1850 err
= GetLastError();
1851 if (err
== ERROR_IO_PENDING
) {
1852 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1857 s
->fd_read(s
->win_opaque
, buf
, size
);
1861 static void win_chr_read(WinCharState
*s
)
1863 if (s
->len
> s
->max_size
)
1864 s
->len
= s
->max_size
;
1868 win_chr_readfile(s
);
1871 static int win_chr_poll(void *opaque
)
1873 WinCharState
*s
= opaque
;
1877 ClearCommError(s
->hcom
, &comerr
, &status
);
1878 if (status
.cbInQue
> 0) {
1879 s
->len
= status
.cbInQue
;
1880 win_chr_read_poll(s
);
1887 static void win_chr_add_read_handler(CharDriverState
*chr
,
1888 IOCanRWHandler
*fd_can_read
,
1889 IOReadHandler
*fd_read
, void *opaque
)
1891 WinCharState
*s
= chr
->opaque
;
1893 s
->fd_can_read
= fd_can_read
;
1894 s
->fd_read
= fd_read
;
1895 s
->win_opaque
= opaque
;
1898 CharDriverState
*qemu_chr_open_win(const char *filename
)
1900 CharDriverState
*chr
;
1903 chr
= qemu_mallocz(sizeof(CharDriverState
));
1906 s
= qemu_mallocz(sizeof(WinCharState
));
1912 chr
->chr_write
= win_chr_write
;
1913 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
1914 chr
->chr_close
= win_chr_close
;
1916 if (win_chr_init(s
, filename
) < 0) {
1924 static int win_chr_pipe_poll(void *opaque
)
1926 WinCharState
*s
= opaque
;
1929 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1932 win_chr_read_poll(s
);
1939 static int win_chr_pipe_init(WinCharState
*s
, const char *filename
)
1948 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1950 fprintf(stderr
, "Failed CreateEvent\n");
1953 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1955 fprintf(stderr
, "Failed CreateEvent\n");
1959 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1960 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1961 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1963 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1964 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1965 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1970 ZeroMemory(&ov
, sizeof(ov
));
1971 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1972 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1974 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1978 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1980 fprintf(stderr
, "Failed GetOverlappedResult\n");
1982 CloseHandle(ov
.hEvent
);
1989 CloseHandle(ov
.hEvent
);
1992 qemu_add_polling_cb(win_chr_pipe_poll
, s
);
2001 CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2003 CharDriverState
*chr
;
2006 chr
= qemu_mallocz(sizeof(CharDriverState
));
2009 s
= qemu_mallocz(sizeof(WinCharState
));
2015 chr
->chr_write
= win_chr_write
;
2016 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2017 chr
->chr_close
= win_chr_close
;
2019 if (win_chr_pipe_init(s
, filename
) < 0) {
2027 CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2029 CharDriverState
*chr
;
2032 chr
= qemu_mallocz(sizeof(CharDriverState
));
2035 s
= qemu_mallocz(sizeof(WinCharState
));
2042 chr
->chr_write
= win_chr_write
;
2043 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2047 CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2051 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2052 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2053 if (fd_out
== INVALID_HANDLE_VALUE
)
2056 return qemu_chr_open_win_file(fd_out
);
2060 CharDriverState
*qemu_chr_open(const char *filename
)
2064 if (!strcmp(filename
, "vc")) {
2065 return text_console_init(&display_state
);
2066 } else if (!strcmp(filename
, "null")) {
2067 return qemu_chr_open_null();
2070 if (strstart(filename
, "file:", &p
)) {
2071 return qemu_chr_open_file_out(p
);
2072 } else if (strstart(filename
, "pipe:", &p
)) {
2073 return qemu_chr_open_pipe(p
);
2074 } else if (!strcmp(filename
, "pty")) {
2075 return qemu_chr_open_pty();
2076 } else if (!strcmp(filename
, "stdio")) {
2077 return qemu_chr_open_stdio();
2080 #if defined(__linux__)
2081 if (strstart(filename
, "/dev/parport", NULL
)) {
2082 return qemu_chr_open_pp(filename
);
2084 if (strstart(filename
, "/dev/", NULL
)) {
2085 return qemu_chr_open_tty(filename
);
2089 if (strstart(filename
, "COM", NULL
)) {
2090 return qemu_chr_open_win(filename
);
2092 if (strstart(filename
, "pipe:", &p
)) {
2093 return qemu_chr_open_win_pipe(p
);
2095 if (strstart(filename
, "file:", &p
)) {
2096 return qemu_chr_open_win_file_out(p
);
2104 void qemu_chr_close(CharDriverState
*chr
)
2107 chr
->chr_close(chr
);
2110 /***********************************************************/
2111 /* network device redirectors */
2113 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2117 for(i
=0;i
<size
;i
+=16) {
2121 fprintf(f
, "%08x ", i
);
2124 fprintf(f
, " %02x", buf
[i
+j
]);
2129 for(j
=0;j
<len
;j
++) {
2131 if (c
< ' ' || c
> '~')
2133 fprintf(f
, "%c", c
);
2139 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
2142 for(i
= 0; i
< 6; i
++) {
2143 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2156 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
2161 p1
= strchr(p
, sep
);
2167 if (len
> buf_size
- 1)
2169 memcpy(buf
, p
, len
);
2176 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
2184 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2186 saddr
->sin_family
= AF_INET
;
2187 if (buf
[0] == '\0') {
2188 saddr
->sin_addr
.s_addr
= 0;
2190 if (isdigit(buf
[0])) {
2191 if (!inet_aton(buf
, &saddr
->sin_addr
))
2194 if ((he
= gethostbyname(buf
)) == NULL
)
2196 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
2199 port
= strtol(p
, (char **)&r
, 0);
2202 saddr
->sin_port
= htons(port
);
2206 /* find or alloc a new VLAN */
2207 VLANState
*qemu_find_vlan(int id
)
2209 VLANState
**pvlan
, *vlan
;
2210 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2214 vlan
= qemu_mallocz(sizeof(VLANState
));
2219 pvlan
= &first_vlan
;
2220 while (*pvlan
!= NULL
)
2221 pvlan
= &(*pvlan
)->next
;
2226 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
2227 IOReadHandler
*fd_read
,
2228 IOCanRWHandler
*fd_can_read
,
2231 VLANClientState
*vc
, **pvc
;
2232 vc
= qemu_mallocz(sizeof(VLANClientState
));
2235 vc
->fd_read
= fd_read
;
2236 vc
->fd_can_read
= fd_can_read
;
2237 vc
->opaque
= opaque
;
2241 pvc
= &vlan
->first_client
;
2242 while (*pvc
!= NULL
)
2243 pvc
= &(*pvc
)->next
;
2248 int qemu_can_send_packet(VLANClientState
*vc1
)
2250 VLANState
*vlan
= vc1
->vlan
;
2251 VLANClientState
*vc
;
2253 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2255 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
2262 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
2264 VLANState
*vlan
= vc1
->vlan
;
2265 VLANClientState
*vc
;
2268 printf("vlan %d send:\n", vlan
->id
);
2269 hex_dump(stdout
, buf
, size
);
2271 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2273 vc
->fd_read(vc
->opaque
, buf
, size
);
2278 #if defined(CONFIG_SLIRP)
2280 /* slirp network adapter */
2282 static int slirp_inited
;
2283 static VLANClientState
*slirp_vc
;
2285 int slirp_can_output(void)
2287 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
2290 void slirp_output(const uint8_t *pkt
, int pkt_len
)
2293 printf("slirp output:\n");
2294 hex_dump(stdout
, pkt
, pkt_len
);
2298 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
2301 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
2304 printf("slirp input:\n");
2305 hex_dump(stdout
, buf
, size
);
2307 slirp_input(buf
, size
);
2310 static int net_slirp_init(VLANState
*vlan
)
2312 if (!slirp_inited
) {
2316 slirp_vc
= qemu_new_vlan_client(vlan
,
2317 slirp_receive
, NULL
, NULL
);
2318 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
2322 static void net_slirp_redir(const char *redir_str
)
2327 struct in_addr guest_addr
;
2328 int host_port
, guest_port
;
2330 if (!slirp_inited
) {
2336 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2338 if (!strcmp(buf
, "tcp")) {
2340 } else if (!strcmp(buf
, "udp")) {
2346 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2348 host_port
= strtol(buf
, &r
, 0);
2352 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2354 if (buf
[0] == '\0') {
2355 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
2357 if (!inet_aton(buf
, &guest_addr
))
2360 guest_port
= strtol(p
, &r
, 0);
2364 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
2365 fprintf(stderr
, "qemu: could not set up redirection\n");
2370 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
2378 static void smb_exit(void)
2382 char filename
[1024];
2384 /* erase all the files in the directory */
2385 d
= opendir(smb_dir
);
2390 if (strcmp(de
->d_name
, ".") != 0 &&
2391 strcmp(de
->d_name
, "..") != 0) {
2392 snprintf(filename
, sizeof(filename
), "%s/%s",
2393 smb_dir
, de
->d_name
);
2401 /* automatic user mode samba server configuration */
2402 void net_slirp_smb(const char *exported_dir
)
2404 char smb_conf
[1024];
2405 char smb_cmdline
[1024];
2408 if (!slirp_inited
) {
2413 /* XXX: better tmp dir construction */
2414 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
2415 if (mkdir(smb_dir
, 0700) < 0) {
2416 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
2419 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
2421 f
= fopen(smb_conf
, "w");
2423 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
2430 "socket address=127.0.0.1\n"
2431 "pid directory=%s\n"
2432 "lock directory=%s\n"
2433 "log file=%s/log.smbd\n"
2434 "smb passwd file=%s/smbpasswd\n"
2435 "security = share\n"
2450 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "/usr/sbin/smbd -s %s",
2453 slirp_add_exec(0, smb_cmdline
, 4, 139);
2456 #endif /* !defined(_WIN32) */
2458 #endif /* CONFIG_SLIRP */
2460 #if !defined(_WIN32)
2462 typedef struct TAPState
{
2463 VLANClientState
*vc
;
2467 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
2469 TAPState
*s
= opaque
;
2472 ret
= write(s
->fd
, buf
, size
);
2473 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
2480 static void tap_send(void *opaque
)
2482 TAPState
*s
= opaque
;
2486 size
= read(s
->fd
, buf
, sizeof(buf
));
2488 qemu_send_packet(s
->vc
, buf
, size
);
2494 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
2498 s
= qemu_mallocz(sizeof(TAPState
));
2502 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
2503 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
2504 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
2509 static int tap_open(char *ifname
, int ifname_size
)
2515 fd
= open("/dev/tap", O_RDWR
);
2517 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
2522 dev
= devname(s
.st_rdev
, S_IFCHR
);
2523 pstrcpy(ifname
, ifname_size
, dev
);
2525 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2528 #elif defined(__sun__)
2529 static int tap_open(char *ifname
, int ifname_size
)
2531 fprintf(stderr
, "warning: tap_open not yet implemented\n");
2535 static int tap_open(char *ifname
, int ifname_size
)
2540 fd
= open("/dev/net/tun", O_RDWR
);
2542 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
2545 memset(&ifr
, 0, sizeof(ifr
));
2546 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
2547 if (ifname
[0] != '\0')
2548 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
2550 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
2551 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
2553 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
2557 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
2558 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2563 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
2564 const char *setup_script
)
2567 int pid
, status
, fd
;
2572 if (ifname1
!= NULL
)
2573 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
2576 fd
= tap_open(ifname
, sizeof(ifname
));
2582 if (setup_script
[0] != '\0') {
2583 /* try to launch network init script */
2588 *parg
++ = (char *)setup_script
;
2591 execv(setup_script
, args
);
2594 while (waitpid(pid
, &status
, 0) != pid
);
2595 if (!WIFEXITED(status
) ||
2596 WEXITSTATUS(status
) != 0) {
2597 fprintf(stderr
, "%s: could not launch network script\n",
2603 s
= net_tap_fd_init(vlan
, fd
);
2606 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2607 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
2611 #endif /* !_WIN32 */
2613 /* network connection */
2614 typedef struct NetSocketState
{
2615 VLANClientState
*vc
;
2617 int state
; /* 0 = getting length, 1 = getting data */
2621 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
2624 typedef struct NetSocketListenState
{
2627 } NetSocketListenState
;
2629 /* XXX: we consider we can send the whole packet without blocking */
2630 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
2632 NetSocketState
*s
= opaque
;
2636 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
2637 send_all(s
->fd
, buf
, size
);
2640 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
2642 NetSocketState
*s
= opaque
;
2643 sendto(s
->fd
, buf
, size
, 0,
2644 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
2647 static void net_socket_send(void *opaque
)
2649 NetSocketState
*s
= opaque
;
2654 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
2656 err
= socket_error();
2657 if (err
!= EWOULDBLOCK
)
2659 } else if (size
== 0) {
2660 /* end of connection */
2662 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2668 /* reassemble a packet from the network */
2674 memcpy(s
->buf
+ s
->index
, buf
, l
);
2678 if (s
->index
== 4) {
2680 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
2686 l
= s
->packet_len
- s
->index
;
2689 memcpy(s
->buf
+ s
->index
, buf
, l
);
2693 if (s
->index
>= s
->packet_len
) {
2694 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
2703 static void net_socket_send_dgram(void *opaque
)
2705 NetSocketState
*s
= opaque
;
2708 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2712 /* end of connection */
2713 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2716 qemu_send_packet(s
->vc
, s
->buf
, size
);
2719 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
2724 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
2725 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
2726 inet_ntoa(mcastaddr
->sin_addr
),
2727 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
2731 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2733 perror("socket(PF_INET, SOCK_DGRAM)");
2738 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
2739 (const char *)&val
, sizeof(val
));
2741 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
2745 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
2751 /* Add host to multicast group */
2752 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
2753 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
2755 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
2756 (const char *)&imr
, sizeof(struct ip_mreq
));
2758 perror("setsockopt(IP_ADD_MEMBERSHIP)");
2762 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
2764 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
2765 (const char *)&val
, sizeof(val
));
2767 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
2771 socket_set_nonblock(fd
);
2774 if (fd
>=0) close(fd
);
2778 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
2781 struct sockaddr_in saddr
;
2783 socklen_t saddr_len
;
2786 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
2787 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
2788 * by ONLY ONE process: we must "clone" this dgram socket --jjo
2792 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
2794 if (saddr
.sin_addr
.s_addr
==0) {
2795 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
2799 /* clone dgram socket */
2800 newfd
= net_socket_mcast_create(&saddr
);
2802 /* error already reported by net_socket_mcast_create() */
2806 /* clone newfd to fd, close newfd */
2811 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
2812 fd
, strerror(errno
));
2817 s
= qemu_mallocz(sizeof(NetSocketState
));
2822 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
2823 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
2825 /* mcast: save bound address as dst */
2826 if (is_connected
) s
->dgram_dst
=saddr
;
2828 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2829 "socket: fd=%d (%s mcast=%s:%d)",
2830 fd
, is_connected
? "cloned" : "",
2831 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2835 static void net_socket_connect(void *opaque
)
2837 NetSocketState
*s
= opaque
;
2838 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
2841 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
2845 s
= qemu_mallocz(sizeof(NetSocketState
));
2849 s
->vc
= qemu_new_vlan_client(vlan
,
2850 net_socket_receive
, NULL
, s
);
2851 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2852 "socket: fd=%d", fd
);
2854 net_socket_connect(s
);
2856 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
2861 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
2864 int so_type
=-1, optlen
=sizeof(so_type
);
2866 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
2867 fprintf(stderr
, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd
);
2872 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
2874 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
2876 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
2877 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
2878 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
2883 static void net_socket_accept(void *opaque
)
2885 NetSocketListenState
*s
= opaque
;
2887 struct sockaddr_in saddr
;
2892 len
= sizeof(saddr
);
2893 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
2894 if (fd
< 0 && errno
!= EINTR
) {
2896 } else if (fd
>= 0) {
2900 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
2904 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
2905 "socket: connection from %s:%d",
2906 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2910 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
2912 NetSocketListenState
*s
;
2914 struct sockaddr_in saddr
;
2916 if (parse_host_port(&saddr
, host_str
) < 0)
2919 s
= qemu_mallocz(sizeof(NetSocketListenState
));
2923 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2928 socket_set_nonblock(fd
);
2930 /* allow fast reuse */
2932 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2934 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2939 ret
= listen(fd
, 0);
2946 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
2950 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
2953 int fd
, connected
, ret
, err
;
2954 struct sockaddr_in saddr
;
2956 if (parse_host_port(&saddr
, host_str
) < 0)
2959 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2964 socket_set_nonblock(fd
);
2968 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2970 err
= socket_error();
2971 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2972 } else if (err
== EINPROGRESS
) {
2984 s
= net_socket_fd_init(vlan
, fd
, connected
);
2987 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2988 "socket: connect to %s:%d",
2989 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2993 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
2997 struct sockaddr_in saddr
;
2999 if (parse_host_port(&saddr
, host_str
) < 0)
3003 fd
= net_socket_mcast_create(&saddr
);
3007 s
= net_socket_fd_init(vlan
, fd
, 0);
3011 s
->dgram_dst
= saddr
;
3013 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3014 "socket: mcast=%s:%d",
3015 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3020 static int get_param_value(char *buf
, int buf_size
,
3021 const char *tag
, const char *str
)
3030 while (*p
!= '\0' && *p
!= '=') {
3031 if ((q
- option
) < sizeof(option
) - 1)
3039 if (!strcmp(tag
, option
)) {
3041 while (*p
!= '\0' && *p
!= ',') {
3042 if ((q
- buf
) < buf_size
- 1)
3049 while (*p
!= '\0' && *p
!= ',') {
3060 int net_client_init(const char *str
)
3071 while (*p
!= '\0' && *p
!= ',') {
3072 if ((q
- device
) < sizeof(device
) - 1)
3080 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
3081 vlan_id
= strtol(buf
, NULL
, 0);
3083 vlan
= qemu_find_vlan(vlan_id
);
3085 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
3088 if (!strcmp(device
, "nic")) {
3092 if (nb_nics
>= MAX_NICS
) {
3093 fprintf(stderr
, "Too Many NICs\n");
3096 nd
= &nd_table
[nb_nics
];
3097 macaddr
= nd
->macaddr
;
3103 macaddr
[5] = 0x56 + nb_nics
;
3105 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
3106 if (parse_macaddr(macaddr
, buf
) < 0) {
3107 fprintf(stderr
, "invalid syntax for ethernet address\n");
3111 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
3112 nd
->model
= strdup(buf
);
3118 if (!strcmp(device
, "none")) {
3119 /* does nothing. It is needed to signal that no network cards
3124 if (!strcmp(device
, "user")) {
3125 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
3126 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
3128 ret
= net_slirp_init(vlan
);
3132 if (!strcmp(device
, "tap")) {
3134 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
3135 fprintf(stderr
, "tap: no interface name\n");
3138 ret
= tap_win32_init(vlan
, ifname
);
3141 if (!strcmp(device
, "tap")) {
3143 char setup_script
[1024];
3145 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3146 fd
= strtol(buf
, NULL
, 0);
3148 if (net_tap_fd_init(vlan
, fd
))
3151 get_param_value(ifname
, sizeof(ifname
), "ifname", p
);
3152 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
3153 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
3155 ret
= net_tap_init(vlan
, ifname
, setup_script
);
3159 if (!strcmp(device
, "socket")) {
3160 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3162 fd
= strtol(buf
, NULL
, 0);
3164 if (net_socket_fd_init(vlan
, fd
, 1))
3166 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
3167 ret
= net_socket_listen_init(vlan
, buf
);
3168 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
3169 ret
= net_socket_connect_init(vlan
, buf
);
3170 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
3171 ret
= net_socket_mcast_init(vlan
, buf
);
3173 fprintf(stderr
, "Unknown socket options: %s\n", p
);
3178 fprintf(stderr
, "Unknown network device: %s\n", device
);
3182 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
3188 void do_info_network(void)
3191 VLANClientState
*vc
;
3193 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3194 term_printf("VLAN %d devices:\n", vlan
->id
);
3195 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3196 term_printf(" %s\n", vc
->info_str
);
3200 /***********************************************************/
3203 static int usb_device_add(const char *devname
)
3211 for(i
= 0;i
< MAX_VM_USB_PORTS
; i
++) {
3212 if (!vm_usb_ports
[i
]->dev
)
3215 if (i
== MAX_VM_USB_PORTS
)
3218 if (strstart(devname
, "host:", &p
)) {
3219 dev
= usb_host_device_open(p
);
3222 } else if (!strcmp(devname
, "mouse")) {
3223 dev
= usb_mouse_init();
3226 } else if (!strcmp(devname
, "tablet")) {
3227 dev
= usb_tablet_init();
3233 usb_attach(vm_usb_ports
[i
], dev
);
3237 static int usb_device_del(const char *devname
)
3240 int bus_num
, addr
, i
;
3246 p
= strchr(devname
, '.');
3249 bus_num
= strtoul(devname
, NULL
, 0);
3250 addr
= strtoul(p
+ 1, NULL
, 0);
3253 for(i
= 0;i
< MAX_VM_USB_PORTS
; i
++) {
3254 dev
= vm_usb_ports
[i
]->dev
;
3255 if (dev
&& dev
->addr
== addr
)
3258 if (i
== MAX_VM_USB_PORTS
)
3260 usb_attach(vm_usb_ports
[i
], NULL
);
3264 void do_usb_add(const char *devname
)
3267 ret
= usb_device_add(devname
);
3269 term_printf("Could not add USB device '%s'\n", devname
);
3272 void do_usb_del(const char *devname
)
3275 ret
= usb_device_del(devname
);
3277 term_printf("Could not remove USB device '%s'\n", devname
);
3284 const char *speed_str
;
3287 term_printf("USB support not enabled\n");
3291 for(i
= 0; i
< MAX_VM_USB_PORTS
; i
++) {
3292 dev
= vm_usb_ports
[i
]->dev
;
3294 term_printf("Hub port %d:\n", i
);
3295 switch(dev
->speed
) {
3299 case USB_SPEED_FULL
:
3302 case USB_SPEED_HIGH
:
3309 term_printf(" Device %d.%d, speed %s Mb/s\n",
3310 0, dev
->addr
, speed_str
);
3315 /***********************************************************/
3318 static char *pid_filename
;
3320 /* Remove PID file. Called on normal exit */
3322 static void remove_pidfile(void)
3324 unlink (pid_filename
);
3327 static void create_pidfile(const char *filename
)
3329 struct stat pidstat
;
3332 /* Try to write our PID to the named file */
3333 if (stat(filename
, &pidstat
) < 0) {
3334 if (errno
== ENOENT
) {
3335 if ((f
= fopen (filename
, "w")) == NULL
) {
3336 perror("Opening pidfile");
3339 fprintf(f
, "%d\n", getpid());
3341 pid_filename
= qemu_strdup(filename
);
3342 if (!pid_filename
) {
3343 fprintf(stderr
, "Could not save PID filename");
3346 atexit(remove_pidfile
);
3349 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
3355 /***********************************************************/
3358 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
3362 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
3366 static void dumb_refresh(DisplayState
*ds
)
3371 void dumb_display_init(DisplayState
*ds
)
3376 ds
->dpy_update
= dumb_update
;
3377 ds
->dpy_resize
= dumb_resize
;
3378 ds
->dpy_refresh
= dumb_refresh
;
3381 #if !defined(CONFIG_SOFTMMU)
3382 /***********************************************************/
3383 /* cpu signal handler */
3384 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
3387 if (cpu_signal_handler(host_signum
, info
, puc
))
3389 if (stdio_nb_clients
> 0)
3395 /***********************************************************/
3398 #define MAX_IO_HANDLERS 64
3400 typedef struct IOHandlerRecord
{
3402 IOCanRWHandler
*fd_read_poll
;
3404 IOHandler
*fd_write
;
3406 /* temporary data */
3408 struct IOHandlerRecord
*next
;
3411 static IOHandlerRecord
*first_io_handler
;
3413 /* XXX: fd_read_poll should be suppressed, but an API change is
3414 necessary in the character devices to suppress fd_can_read(). */
3415 int qemu_set_fd_handler2(int fd
,
3416 IOCanRWHandler
*fd_read_poll
,
3418 IOHandler
*fd_write
,
3421 IOHandlerRecord
**pioh
, *ioh
;
3423 if (!fd_read
&& !fd_write
) {
3424 pioh
= &first_io_handler
;
3429 if (ioh
->fd
== fd
) {
3437 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3441 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
3444 ioh
->next
= first_io_handler
;
3445 first_io_handler
= ioh
;
3448 ioh
->fd_read_poll
= fd_read_poll
;
3449 ioh
->fd_read
= fd_read
;
3450 ioh
->fd_write
= fd_write
;
3451 ioh
->opaque
= opaque
;
3456 int qemu_set_fd_handler(int fd
,
3458 IOHandler
*fd_write
,
3461 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
3464 /***********************************************************/
3465 /* Polling handling */
3467 typedef struct PollingEntry
{
3470 struct PollingEntry
*next
;
3473 static PollingEntry
*first_polling_entry
;
3475 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
3477 PollingEntry
**ppe
, *pe
;
3478 pe
= qemu_mallocz(sizeof(PollingEntry
));
3482 pe
->opaque
= opaque
;
3483 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
3488 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3490 PollingEntry
**ppe
, *pe
;
3491 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3493 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3501 /***********************************************************/
3502 /* savevm/loadvm support */
3504 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
3506 fwrite(buf
, 1, size
, f
);
3509 void qemu_put_byte(QEMUFile
*f
, int v
)
3514 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
3516 qemu_put_byte(f
, v
>> 8);
3517 qemu_put_byte(f
, v
);
3520 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
3522 qemu_put_byte(f
, v
>> 24);
3523 qemu_put_byte(f
, v
>> 16);
3524 qemu_put_byte(f
, v
>> 8);
3525 qemu_put_byte(f
, v
);
3528 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
3530 qemu_put_be32(f
, v
>> 32);
3531 qemu_put_be32(f
, v
);
3534 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
3536 return fread(buf
, 1, size
, f
);
3539 int qemu_get_byte(QEMUFile
*f
)
3549 unsigned int qemu_get_be16(QEMUFile
*f
)
3552 v
= qemu_get_byte(f
) << 8;
3553 v
|= qemu_get_byte(f
);
3557 unsigned int qemu_get_be32(QEMUFile
*f
)
3560 v
= qemu_get_byte(f
) << 24;
3561 v
|= qemu_get_byte(f
) << 16;
3562 v
|= qemu_get_byte(f
) << 8;
3563 v
|= qemu_get_byte(f
);
3567 uint64_t qemu_get_be64(QEMUFile
*f
)
3570 v
= (uint64_t)qemu_get_be32(f
) << 32;
3571 v
|= qemu_get_be32(f
);
3575 int64_t qemu_ftell(QEMUFile
*f
)
3580 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
3582 if (fseek(f
, pos
, whence
) < 0)
3587 typedef struct SaveStateEntry
{
3591 SaveStateHandler
*save_state
;
3592 LoadStateHandler
*load_state
;
3594 struct SaveStateEntry
*next
;
3597 static SaveStateEntry
*first_se
;
3599 int register_savevm(const char *idstr
,
3602 SaveStateHandler
*save_state
,
3603 LoadStateHandler
*load_state
,
3606 SaveStateEntry
*se
, **pse
;
3608 se
= qemu_malloc(sizeof(SaveStateEntry
));
3611 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
3612 se
->instance_id
= instance_id
;
3613 se
->version_id
= version_id
;
3614 se
->save_state
= save_state
;
3615 se
->load_state
= load_state
;
3616 se
->opaque
= opaque
;
3619 /* add at the end of list */
3621 while (*pse
!= NULL
)
3622 pse
= &(*pse
)->next
;
3627 #define QEMU_VM_FILE_MAGIC 0x5145564d
3628 #define QEMU_VM_FILE_VERSION 0x00000001
3630 int qemu_savevm(const char *filename
)
3634 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
3636 saved_vm_running
= vm_running
;
3639 f
= fopen(filename
, "wb");
3645 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
3646 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
3648 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3650 len
= strlen(se
->idstr
);
3651 qemu_put_byte(f
, len
);
3652 qemu_put_buffer(f
, se
->idstr
, len
);
3654 qemu_put_be32(f
, se
->instance_id
);
3655 qemu_put_be32(f
, se
->version_id
);
3657 /* record size: filled later */
3659 qemu_put_be32(f
, 0);
3661 se
->save_state(f
, se
->opaque
);
3663 /* fill record size */
3665 len
= ftell(f
) - len_pos
- 4;
3666 fseek(f
, len_pos
, SEEK_SET
);
3667 qemu_put_be32(f
, len
);
3668 fseek(f
, cur_pos
, SEEK_SET
);
3674 if (saved_vm_running
)
3679 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
3683 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3684 if (!strcmp(se
->idstr
, idstr
) &&
3685 instance_id
== se
->instance_id
)
3691 int qemu_loadvm(const char *filename
)
3695 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
3696 int saved_vm_running
;
3700 saved_vm_running
= vm_running
;
3703 f
= fopen(filename
, "rb");
3709 v
= qemu_get_be32(f
);
3710 if (v
!= QEMU_VM_FILE_MAGIC
)
3712 v
= qemu_get_be32(f
);
3713 if (v
!= QEMU_VM_FILE_VERSION
) {
3720 len
= qemu_get_byte(f
);
3723 qemu_get_buffer(f
, idstr
, len
);
3725 instance_id
= qemu_get_be32(f
);
3726 version_id
= qemu_get_be32(f
);
3727 record_len
= qemu_get_be32(f
);
3729 printf("idstr=%s instance=0x%x version=%d len=%d\n",
3730 idstr
, instance_id
, version_id
, record_len
);
3733 se
= find_se(idstr
, instance_id
);
3735 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
3736 instance_id
, idstr
);
3738 ret
= se
->load_state(f
, se
->opaque
, version_id
);
3740 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
3741 instance_id
, idstr
);
3744 /* always seek to exact end of record */
3745 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
3750 if (saved_vm_running
)
3755 /***********************************************************/
3756 /* cpu save/restore */
3758 #if defined(TARGET_I386)
3760 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
3762 qemu_put_be32(f
, dt
->selector
);
3763 qemu_put_betl(f
, dt
->base
);
3764 qemu_put_be32(f
, dt
->limit
);
3765 qemu_put_be32(f
, dt
->flags
);
3768 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
3770 dt
->selector
= qemu_get_be32(f
);
3771 dt
->base
= qemu_get_betl(f
);
3772 dt
->limit
= qemu_get_be32(f
);
3773 dt
->flags
= qemu_get_be32(f
);
3776 void cpu_save(QEMUFile
*f
, void *opaque
)
3778 CPUState
*env
= opaque
;
3779 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
3783 for(i
= 0; i
< CPU_NB_REGS
; i
++)
3784 qemu_put_betls(f
, &env
->regs
[i
]);
3785 qemu_put_betls(f
, &env
->eip
);
3786 qemu_put_betls(f
, &env
->eflags
);
3787 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
3788 qemu_put_be32s(f
, &hflags
);
3792 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
3794 for(i
= 0; i
< 8; i
++) {
3795 fptag
|= ((!env
->fptags
[i
]) << i
);
3798 qemu_put_be16s(f
, &fpuc
);
3799 qemu_put_be16s(f
, &fpus
);
3800 qemu_put_be16s(f
, &fptag
);
3802 #ifdef USE_X86LDOUBLE
3807 qemu_put_be16s(f
, &fpregs_format
);
3809 for(i
= 0; i
< 8; i
++) {
3810 #ifdef USE_X86LDOUBLE
3814 /* we save the real CPU data (in case of MMX usage only 'mant'
3815 contains the MMX register */
3816 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
3817 qemu_put_be64(f
, mant
);
3818 qemu_put_be16(f
, exp
);
3821 /* if we use doubles for float emulation, we save the doubles to
3822 avoid losing information in case of MMX usage. It can give
3823 problems if the image is restored on a CPU where long
3824 doubles are used instead. */
3825 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
3829 for(i
= 0; i
< 6; i
++)
3830 cpu_put_seg(f
, &env
->segs
[i
]);
3831 cpu_put_seg(f
, &env
->ldt
);
3832 cpu_put_seg(f
, &env
->tr
);
3833 cpu_put_seg(f
, &env
->gdt
);
3834 cpu_put_seg(f
, &env
->idt
);
3836 qemu_put_be32s(f
, &env
->sysenter_cs
);
3837 qemu_put_be32s(f
, &env
->sysenter_esp
);
3838 qemu_put_be32s(f
, &env
->sysenter_eip
);
3840 qemu_put_betls(f
, &env
->cr
[0]);
3841 qemu_put_betls(f
, &env
->cr
[2]);
3842 qemu_put_betls(f
, &env
->cr
[3]);
3843 qemu_put_betls(f
, &env
->cr
[4]);
3845 for(i
= 0; i
< 8; i
++)
3846 qemu_put_betls(f
, &env
->dr
[i
]);
3849 qemu_put_be32s(f
, &env
->a20_mask
);
3852 qemu_put_be32s(f
, &env
->mxcsr
);
3853 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
3854 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
3855 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
3858 #ifdef TARGET_X86_64
3859 qemu_put_be64s(f
, &env
->efer
);
3860 qemu_put_be64s(f
, &env
->star
);
3861 qemu_put_be64s(f
, &env
->lstar
);
3862 qemu_put_be64s(f
, &env
->cstar
);
3863 qemu_put_be64s(f
, &env
->fmask
);
3864 qemu_put_be64s(f
, &env
->kernelgsbase
);
3868 #ifdef USE_X86LDOUBLE
3869 /* XXX: add that in a FPU generic layer */
3870 union x86_longdouble
{
3875 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
3876 #define EXPBIAS1 1023
3877 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
3878 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
3880 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
3884 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
3885 /* exponent + sign */
3886 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
3887 e
|= SIGND1(temp
) >> 16;
3892 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
3894 CPUState
*env
= opaque
;
3897 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
3899 if (version_id
!= 3)
3901 for(i
= 0; i
< CPU_NB_REGS
; i
++)
3902 qemu_get_betls(f
, &env
->regs
[i
]);
3903 qemu_get_betls(f
, &env
->eip
);
3904 qemu_get_betls(f
, &env
->eflags
);
3905 qemu_get_be32s(f
, &hflags
);
3907 qemu_get_be16s(f
, &fpuc
);
3908 qemu_get_be16s(f
, &fpus
);
3909 qemu_get_be16s(f
, &fptag
);
3910 qemu_get_be16s(f
, &fpregs_format
);
3912 /* NOTE: we cannot always restore the FPU state if the image come
3913 from a host with a different 'USE_X86LDOUBLE' define. We guess
3914 if we are in an MMX state to restore correctly in that case. */
3915 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
3916 for(i
= 0; i
< 8; i
++) {
3920 switch(fpregs_format
) {
3922 mant
= qemu_get_be64(f
);
3923 exp
= qemu_get_be16(f
);
3924 #ifdef USE_X86LDOUBLE
3925 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
3927 /* difficult case */
3929 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
3931 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
3935 mant
= qemu_get_be64(f
);
3936 #ifdef USE_X86LDOUBLE
3938 union x86_longdouble
*p
;
3939 /* difficult case */
3940 p
= (void *)&env
->fpregs
[i
];
3945 fp64_to_fp80(p
, mant
);
3949 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
3958 /* XXX: restore FPU round state */
3959 env
->fpstt
= (fpus
>> 11) & 7;
3960 env
->fpus
= fpus
& ~0x3800;
3962 for(i
= 0; i
< 8; i
++) {
3963 env
->fptags
[i
] = (fptag
>> i
) & 1;
3966 for(i
= 0; i
< 6; i
++)
3967 cpu_get_seg(f
, &env
->segs
[i
]);
3968 cpu_get_seg(f
, &env
->ldt
);
3969 cpu_get_seg(f
, &env
->tr
);
3970 cpu_get_seg(f
, &env
->gdt
);
3971 cpu_get_seg(f
, &env
->idt
);
3973 qemu_get_be32s(f
, &env
->sysenter_cs
);
3974 qemu_get_be32s(f
, &env
->sysenter_esp
);
3975 qemu_get_be32s(f
, &env
->sysenter_eip
);
3977 qemu_get_betls(f
, &env
->cr
[0]);
3978 qemu_get_betls(f
, &env
->cr
[2]);
3979 qemu_get_betls(f
, &env
->cr
[3]);
3980 qemu_get_betls(f
, &env
->cr
[4]);
3982 for(i
= 0; i
< 8; i
++)
3983 qemu_get_betls(f
, &env
->dr
[i
]);
3986 qemu_get_be32s(f
, &env
->a20_mask
);
3988 qemu_get_be32s(f
, &env
->mxcsr
);
3989 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
3990 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
3991 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
3994 #ifdef TARGET_X86_64
3995 qemu_get_be64s(f
, &env
->efer
);
3996 qemu_get_be64s(f
, &env
->star
);
3997 qemu_get_be64s(f
, &env
->lstar
);
3998 qemu_get_be64s(f
, &env
->cstar
);
3999 qemu_get_be64s(f
, &env
->fmask
);
4000 qemu_get_be64s(f
, &env
->kernelgsbase
);
4003 /* XXX: compute hflags from scratch, except for CPL and IIF */
4004 env
->hflags
= hflags
;
4009 #elif defined(TARGET_PPC)
4010 void cpu_save(QEMUFile
*f
, void *opaque
)
4014 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4019 #elif defined(TARGET_MIPS)
4020 void cpu_save(QEMUFile
*f
, void *opaque
)
4024 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4029 #elif defined(TARGET_SPARC)
4030 void cpu_save(QEMUFile
*f
, void *opaque
)
4032 CPUState
*env
= opaque
;
4036 for(i
= 0; i
< 8; i
++)
4037 qemu_put_betls(f
, &env
->gregs
[i
]);
4038 for(i
= 0; i
< NWINDOWS
* 16; i
++)
4039 qemu_put_betls(f
, &env
->regbase
[i
]);
4042 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
4048 qemu_put_betl(f
, u
.i
);
4051 qemu_put_betls(f
, &env
->pc
);
4052 qemu_put_betls(f
, &env
->npc
);
4053 qemu_put_betls(f
, &env
->y
);
4055 qemu_put_be32(f
, tmp
);
4056 qemu_put_betls(f
, &env
->fsr
);
4057 qemu_put_betls(f
, &env
->tbr
);
4058 #ifndef TARGET_SPARC64
4059 qemu_put_be32s(f
, &env
->wim
);
4061 for(i
= 0; i
< 16; i
++)
4062 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
4066 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4068 CPUState
*env
= opaque
;
4072 for(i
= 0; i
< 8; i
++)
4073 qemu_get_betls(f
, &env
->gregs
[i
]);
4074 for(i
= 0; i
< NWINDOWS
* 16; i
++)
4075 qemu_get_betls(f
, &env
->regbase
[i
]);
4078 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
4083 u
.i
= qemu_get_betl(f
);
4087 qemu_get_betls(f
, &env
->pc
);
4088 qemu_get_betls(f
, &env
->npc
);
4089 qemu_get_betls(f
, &env
->y
);
4090 tmp
= qemu_get_be32(f
);
4091 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
4092 correctly updated */
4094 qemu_get_betls(f
, &env
->fsr
);
4095 qemu_get_betls(f
, &env
->tbr
);
4096 #ifndef TARGET_SPARC64
4097 qemu_get_be32s(f
, &env
->wim
);
4099 for(i
= 0; i
< 16; i
++)
4100 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
4106 #elif defined(TARGET_ARM)
4108 /* ??? Need to implement these. */
4109 void cpu_save(QEMUFile
*f
, void *opaque
)
4113 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4120 #warning No CPU save/restore functions
4124 /***********************************************************/
4125 /* ram save/restore */
4127 /* we just avoid storing empty pages */
4128 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
4133 for(i
= 1; i
< len
; i
++) {
4137 qemu_put_byte(f
, 1);
4138 qemu_put_byte(f
, v
);
4141 qemu_put_byte(f
, 0);
4142 qemu_put_buffer(f
, buf
, len
);
4145 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
4149 v
= qemu_get_byte(f
);
4152 if (qemu_get_buffer(f
, buf
, len
) != len
)
4156 v
= qemu_get_byte(f
);
4157 memset(buf
, v
, len
);
4165 static void ram_save(QEMUFile
*f
, void *opaque
)
4168 qemu_put_be32(f
, phys_ram_size
);
4169 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
4170 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
4174 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
4178 if (version_id
!= 1)
4180 if (qemu_get_be32(f
) != phys_ram_size
)
4182 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
4183 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
4190 /***********************************************************/
4191 /* machine registration */
4193 QEMUMachine
*first_machine
= NULL
;
4195 int qemu_register_machine(QEMUMachine
*m
)
4198 pm
= &first_machine
;
4206 QEMUMachine
*find_machine(const char *name
)
4210 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4211 if (!strcmp(m
->name
, name
))
4217 /***********************************************************/
4218 /* main execution loop */
4220 void gui_update(void *opaque
)
4222 display_state
.dpy_refresh(&display_state
);
4223 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
4226 struct vm_change_state_entry
{
4227 VMChangeStateHandler
*cb
;
4229 LIST_ENTRY (vm_change_state_entry
) entries
;
4232 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
4234 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
4237 VMChangeStateEntry
*e
;
4239 e
= qemu_mallocz(sizeof (*e
));
4245 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
4249 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
4251 LIST_REMOVE (e
, entries
);
4255 static void vm_state_notify(int running
)
4257 VMChangeStateEntry
*e
;
4259 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
4260 e
->cb(e
->opaque
, running
);
4264 /* XXX: support several handlers */
4265 static VMStopHandler
*vm_stop_cb
;
4266 static void *vm_stop_opaque
;
4268 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4271 vm_stop_opaque
= opaque
;
4275 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4289 void vm_stop(int reason
)
4292 cpu_disable_ticks();
4296 vm_stop_cb(vm_stop_opaque
, reason
);
4303 /* reset/shutdown handler */
4305 typedef struct QEMUResetEntry
{
4306 QEMUResetHandler
*func
;
4308 struct QEMUResetEntry
*next
;
4311 static QEMUResetEntry
*first_reset_entry
;
4312 static int reset_requested
;
4313 static int shutdown_requested
;
4314 static int powerdown_requested
;
4316 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
4318 QEMUResetEntry
**pre
, *re
;
4320 pre
= &first_reset_entry
;
4321 while (*pre
!= NULL
)
4322 pre
= &(*pre
)->next
;
4323 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
4325 re
->opaque
= opaque
;
4330 void qemu_system_reset(void)
4334 /* reset all devices */
4335 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
4336 re
->func(re
->opaque
);
4340 void qemu_system_reset_request(void)
4342 reset_requested
= 1;
4344 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4347 void qemu_system_shutdown_request(void)
4349 shutdown_requested
= 1;
4351 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4354 void qemu_system_powerdown_request(void)
4356 powerdown_requested
= 1;
4358 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4361 void main_loop_wait(int timeout
)
4363 IOHandlerRecord
*ioh
, *ioh_next
;
4370 /* XXX: need to suppress polling by better using win32 events */
4372 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4373 ret
|= pe
->func(pe
->opaque
);
4376 if (ret
== 0 && timeout
> 0) {
4380 /* poll any events */
4381 /* XXX: separate device handlers from system ones */
4385 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4387 (!ioh
->fd_read_poll
||
4388 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4389 FD_SET(ioh
->fd
, &rfds
);
4393 if (ioh
->fd_write
) {
4394 FD_SET(ioh
->fd
, &wfds
);
4404 tv
.tv_usec
= timeout
* 1000;
4406 ret
= select(nfds
+ 1, &rfds
, &wfds
, NULL
, &tv
);
4408 /* XXX: better handling of removal */
4409 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
4410 ioh_next
= ioh
->next
;
4411 if (FD_ISSET(ioh
->fd
, &rfds
)) {
4412 ioh
->fd_read(ioh
->opaque
);
4414 if (FD_ISSET(ioh
->fd
, &wfds
)) {
4415 ioh
->fd_write(ioh
->opaque
);
4423 #if defined(CONFIG_SLIRP)
4424 /* XXX: merge with the previous select() */
4426 fd_set rfds
, wfds
, xfds
;
4434 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4437 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4439 slirp_select_poll(&rfds
, &wfds
, &xfds
);
4445 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4446 qemu_get_clock(vm_clock
));
4447 /* run dma transfers, if any */
4451 /* real time timers */
4452 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4453 qemu_get_clock(rt_clock
));
4456 static CPUState
*cur_cpu
;
4461 #ifdef CONFIG_PROFILER
4466 cur_cpu
= first_cpu
;
4473 env
= env
->next_cpu
;
4476 #ifdef CONFIG_PROFILER
4477 ti
= profile_getclock();
4479 ret
= cpu_exec(env
);
4480 #ifdef CONFIG_PROFILER
4481 qemu_time
+= profile_getclock() - ti
;
4483 if (ret
!= EXCP_HALTED
)
4485 /* all CPUs are halted ? */
4486 if (env
== cur_cpu
) {
4493 if (shutdown_requested
) {
4494 ret
= EXCP_INTERRUPT
;
4497 if (reset_requested
) {
4498 reset_requested
= 0;
4499 qemu_system_reset();
4500 ret
= EXCP_INTERRUPT
;
4502 if (powerdown_requested
) {
4503 powerdown_requested
= 0;
4504 qemu_system_powerdown();
4505 ret
= EXCP_INTERRUPT
;
4507 if (ret
== EXCP_DEBUG
) {
4508 vm_stop(EXCP_DEBUG
);
4510 /* if hlt instruction, we wait until the next IRQ */
4511 /* XXX: use timeout computed from timers */
4512 if (ret
== EXCP_HLT
)
4519 #ifdef CONFIG_PROFILER
4520 ti
= profile_getclock();
4522 main_loop_wait(timeout
);
4523 #ifdef CONFIG_PROFILER
4524 dev_time
+= profile_getclock() - ti
;
4527 cpu_disable_ticks();
4533 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2005 Fabrice Bellard\n"
4534 "usage: %s [options] [disk_image]\n"
4536 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4538 "Standard options:\n"
4539 "-M machine select emulated machine (-M ? for list)\n"
4540 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
4541 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
4542 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
4543 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
4544 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
4545 "-snapshot write to temporary files instead of disk image files\n"
4546 "-m megs set virtual RAM size to megs MB [default=%d]\n"
4547 "-smp n set the number of CPUs to 'n' [default=1]\n"
4548 "-nographic disable graphical output and redirect serial I/Os to console\n"
4550 "-k language use keyboard layout (for example \"fr\" for French)\n"
4553 "-audio-help print list of audio drivers and their options\n"
4554 "-soundhw c1,... enable audio support\n"
4555 " and only specified sound cards (comma separated list)\n"
4556 " use -soundhw ? to get the list of supported cards\n"
4557 " use -soundhw all to enable all of them\n"
4559 "-localtime set the real time clock to local time [default=utc]\n"
4560 "-full-screen start in full screen\n"
4562 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4564 "-usb enable the USB driver (will be the default soon)\n"
4565 "-usbdevice name add the host or guest USB device 'name'\n"
4566 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4567 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4570 "Network options:\n"
4571 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
4572 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4574 "-net user[,vlan=n][,hostname=host]\n"
4575 " connect the user mode network stack to VLAN 'n' and send\n"
4576 " hostname 'host' to DHCP clients\n"
4579 "-net tap[,vlan=n],ifname=name\n"
4580 " connect the host TAP network interface to VLAN 'n'\n"
4582 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
4583 " connect the host TAP network interface to VLAN 'n' and use\n"
4584 " the network script 'file' (default=%s);\n"
4585 " use 'fd=h' to connect to an already opened TAP interface\n"
4587 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4588 " connect the vlan 'n' to another VLAN using a socket connection\n"
4589 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
4590 " connect the vlan 'n' to multicast maddr and port\n"
4591 "-net none use it alone to have zero network devices; if no -net option\n"
4592 " is provided, the default is '-net nic -net user'\n"
4595 "-tftp prefix allow tftp access to files starting with prefix [-net user]\n"
4597 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4599 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4600 " redirect TCP or UDP connections from host to guest [-net user]\n"
4603 "Linux boot specific:\n"
4604 "-kernel bzImage use 'bzImage' as kernel image\n"
4605 "-append cmdline use 'cmdline' as kernel command line\n"
4606 "-initrd file use 'file' as initial ram disk\n"
4608 "Debug/Expert options:\n"
4609 "-monitor dev redirect the monitor to char device 'dev'\n"
4610 "-serial dev redirect the serial port to char device 'dev'\n"
4611 "-parallel dev redirect the parallel port to char device 'dev'\n"
4612 "-pidfile file Write PID to 'file'\n"
4613 "-S freeze CPU at startup (use 'c' to start execution)\n"
4614 "-s wait gdb connection to port %d\n"
4615 "-p port change gdb connection port\n"
4616 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4617 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
4618 " translation (t=none or lba) (usually qemu can guess them)\n"
4619 "-L path set the directory for the BIOS and VGA BIOS\n"
4621 "-no-kqemu disable KQEMU kernel module usage\n"
4623 #ifdef USE_CODE_COPY
4624 "-no-code-copy disable code copy acceleration\n"
4627 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
4628 " (default is CL-GD5446 PCI VGA)\n"
4630 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
4631 "-vnc display start a VNC server on display\n"
4633 "During emulation, the following keys are useful:\n"
4634 "ctrl-alt-f toggle full screen\n"
4635 "ctrl-alt-n switch to virtual console 'n'\n"
4636 "ctrl-alt toggle mouse and keyboard grab\n"
4638 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4640 #ifdef CONFIG_SOFTMMU
4647 DEFAULT_NETWORK_SCRIPT
,
4649 DEFAULT_GDBSTUB_PORT
,
4651 #ifndef CONFIG_SOFTMMU
4653 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
4654 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
4660 #define HAS_ARG 0x0001
4674 QEMU_OPTION_snapshot
,
4676 QEMU_OPTION_nographic
,
4678 QEMU_OPTION_audio_help
,
4679 QEMU_OPTION_soundhw
,
4697 QEMU_OPTION_no_code_copy
,
4699 QEMU_OPTION_localtime
,
4700 QEMU_OPTION_cirrusvga
,
4702 QEMU_OPTION_std_vga
,
4703 QEMU_OPTION_monitor
,
4705 QEMU_OPTION_parallel
,
4707 QEMU_OPTION_full_screen
,
4708 QEMU_OPTION_pidfile
,
4709 QEMU_OPTION_no_kqemu
,
4710 QEMU_OPTION_kernel_kqemu
,
4711 QEMU_OPTION_win2k_hack
,
4713 QEMU_OPTION_usbdevice
,
4718 typedef struct QEMUOption
{
4724 const QEMUOption qemu_options
[] = {
4725 { "h", 0, QEMU_OPTION_h
},
4727 { "M", HAS_ARG
, QEMU_OPTION_M
},
4728 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4729 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4730 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4731 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4732 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4733 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4734 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4735 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4736 { "snapshot", 0, QEMU_OPTION_snapshot
},
4737 { "m", HAS_ARG
, QEMU_OPTION_m
},
4738 { "nographic", 0, QEMU_OPTION_nographic
},
4739 { "k", HAS_ARG
, QEMU_OPTION_k
},
4741 { "audio-help", 0, QEMU_OPTION_audio_help
},
4742 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4745 { "net", HAS_ARG
, QEMU_OPTION_net
},
4747 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4749 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4751 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4754 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4755 { "append", HAS_ARG
, QEMU_OPTION_append
},
4756 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4758 { "S", 0, QEMU_OPTION_S
},
4759 { "s", 0, QEMU_OPTION_s
},
4760 { "p", HAS_ARG
, QEMU_OPTION_p
},
4761 { "d", HAS_ARG
, QEMU_OPTION_d
},
4762 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4763 { "L", HAS_ARG
, QEMU_OPTION_L
},
4764 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
4766 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4767 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4769 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4770 { "g", 1, QEMU_OPTION_g
},
4772 { "localtime", 0, QEMU_OPTION_localtime
},
4773 { "std-vga", 0, QEMU_OPTION_std_vga
},
4774 { "monitor", 1, QEMU_OPTION_monitor
},
4775 { "serial", 1, QEMU_OPTION_serial
},
4776 { "parallel", 1, QEMU_OPTION_parallel
},
4777 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4778 { "full-screen", 0, QEMU_OPTION_full_screen
},
4779 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4780 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4781 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4782 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4783 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4785 /* temporary options */
4786 { "usb", 0, QEMU_OPTION_usb
},
4787 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
4791 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
4793 /* this stack is only used during signal handling */
4794 #define SIGNAL_STACK_SIZE 32768
4796 static uint8_t *signal_stack
;
4800 /* password input */
4802 static BlockDriverState
*get_bdrv(int index
)
4804 BlockDriverState
*bs
;
4807 bs
= bs_table
[index
];
4808 } else if (index
< 6) {
4809 bs
= fd_table
[index
- 4];
4816 static void read_passwords(void)
4818 BlockDriverState
*bs
;
4822 for(i
= 0; i
< 6; i
++) {
4824 if (bs
&& bdrv_is_encrypted(bs
)) {
4825 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
4826 for(j
= 0; j
< 3; j
++) {
4827 monitor_readline("Password: ",
4828 1, password
, sizeof(password
));
4829 if (bdrv_set_key(bs
, password
) == 0)
4831 term_printf("invalid password\n");
4837 /* XXX: currently we cannot use simultaneously different CPUs */
4838 void register_machines(void)
4840 #if defined(TARGET_I386)
4841 qemu_register_machine(&pc_machine
);
4842 qemu_register_machine(&isapc_machine
);
4843 #elif defined(TARGET_PPC)
4844 qemu_register_machine(&heathrow_machine
);
4845 qemu_register_machine(&core99_machine
);
4846 qemu_register_machine(&prep_machine
);
4847 #elif defined(TARGET_MIPS)
4848 qemu_register_machine(&mips_machine
);
4849 #elif defined(TARGET_SPARC)
4850 #ifdef TARGET_SPARC64
4851 qemu_register_machine(&sun4u_machine
);
4853 qemu_register_machine(&sun4m_machine
);
4855 #elif defined(TARGET_ARM)
4856 qemu_register_machine(&integratorcp926_machine
);
4857 qemu_register_machine(&integratorcp1026_machine
);
4858 qemu_register_machine(&versatilepb_machine
);
4859 qemu_register_machine(&versatileab_machine
);
4860 #elif defined(TARGET_SH4)
4861 qemu_register_machine(&shix_machine
);
4863 #error unsupported CPU
4868 struct soundhw soundhw
[] = {
4875 { .init_isa
= pcspk_audio_init
}
4880 "Creative Sound Blaster 16",
4883 { .init_isa
= SB16_init
}
4890 "Yamaha YMF262 (OPL3)",
4892 "Yamaha YM3812 (OPL2)",
4896 { .init_isa
= Adlib_init
}
4903 "Gravis Ultrasound GF1",
4906 { .init_isa
= GUS_init
}
4912 "ENSONIQ AudioPCI ES1370",
4915 { .init_pci
= es1370_init
}
4918 { NULL
, NULL
, 0, 0, { NULL
} }
4921 static void select_soundhw (const char *optarg
)
4925 if (*optarg
== '?') {
4928 printf ("Valid sound card names (comma separated):\n");
4929 for (c
= soundhw
; c
->name
; ++c
) {
4930 printf ("%-11s %s\n", c
->name
, c
->descr
);
4932 printf ("\n-soundhw all will enable all of the above\n");
4933 exit (*optarg
!= '?');
4941 if (!strcmp (optarg
, "all")) {
4942 for (c
= soundhw
; c
->name
; ++c
) {
4950 e
= strchr (p
, ',');
4951 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4953 for (c
= soundhw
; c
->name
; ++c
) {
4954 if (!strncmp (c
->name
, p
, l
)) {
4963 "Unknown sound card name (too big to show)\n");
4966 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4971 p
+= l
+ (e
!= NULL
);
4975 goto show_valid_cards
;
4980 #define MAX_NET_CLIENTS 32
4982 int main(int argc
, char **argv
)
4984 #ifdef CONFIG_GDBSTUB
4985 int use_gdbstub
, gdbstub_port
;
4988 int snapshot
, linux_boot
;
4989 const char *initrd_filename
;
4990 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
4991 const char *kernel_filename
, *kernel_cmdline
;
4992 DisplayState
*ds
= &display_state
;
4993 int cyls
, heads
, secs
, translation
;
4994 int start_emulation
= 1;
4995 char net_clients
[MAX_NET_CLIENTS
][256];
4998 const char *r
, *optarg
;
4999 CharDriverState
*monitor_hd
;
5000 char monitor_device
[128];
5001 char serial_devices
[MAX_SERIAL_PORTS
][128];
5002 int serial_device_index
;
5003 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
5004 int parallel_device_index
;
5005 const char *loadvm
= NULL
;
5006 QEMUMachine
*machine
;
5007 char usb_devices
[MAX_VM_USB_PORTS
][128];
5008 int usb_devices_index
;
5010 LIST_INIT (&vm_change_state_head
);
5011 #if !defined(CONFIG_SOFTMMU)
5012 /* we never want that malloc() uses mmap() */
5013 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
5015 register_machines();
5016 machine
= first_machine
;
5017 initrd_filename
= NULL
;
5018 for(i
= 0; i
< MAX_FD
; i
++)
5019 fd_filename
[i
] = NULL
;
5020 for(i
= 0; i
< MAX_DISKS
; i
++)
5021 hd_filename
[i
] = NULL
;
5022 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5023 vga_ram_size
= VGA_RAM_SIZE
;
5024 bios_size
= BIOS_SIZE
;
5025 #ifdef CONFIG_GDBSTUB
5027 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
5031 kernel_filename
= NULL
;
5032 kernel_cmdline
= "";
5038 cyls
= heads
= secs
= 0;
5039 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5040 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
5042 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
5043 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
5044 serial_devices
[i
][0] = '\0';
5045 serial_device_index
= 0;
5047 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
5048 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
5049 parallel_devices
[i
][0] = '\0';
5050 parallel_device_index
= 0;
5052 usb_devices_index
= 0;
5057 /* default mac address of the first network interface */
5065 hd_filename
[0] = argv
[optind
++];
5067 const QEMUOption
*popt
;
5070 popt
= qemu_options
;
5073 fprintf(stderr
, "%s: invalid option -- '%s'\n",
5077 if (!strcmp(popt
->name
, r
+ 1))
5081 if (popt
->flags
& HAS_ARG
) {
5082 if (optind
>= argc
) {
5083 fprintf(stderr
, "%s: option '%s' requires an argument\n",
5087 optarg
= argv
[optind
++];
5092 switch(popt
->index
) {
5094 machine
= find_machine(optarg
);
5097 printf("Supported machines are:\n");
5098 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5099 printf("%-10s %s%s\n",
5101 m
== first_machine
? " (default)" : "");
5106 case QEMU_OPTION_initrd
:
5107 initrd_filename
= optarg
;
5109 case QEMU_OPTION_hda
:
5110 case QEMU_OPTION_hdb
:
5111 case QEMU_OPTION_hdc
:
5112 case QEMU_OPTION_hdd
:
5115 hd_index
= popt
->index
- QEMU_OPTION_hda
;
5116 hd_filename
[hd_index
] = optarg
;
5117 if (hd_index
== cdrom_index
)
5121 case QEMU_OPTION_snapshot
:
5124 case QEMU_OPTION_hdachs
:
5128 cyls
= strtol(p
, (char **)&p
, 0);
5129 if (cyls
< 1 || cyls
> 16383)
5134 heads
= strtol(p
, (char **)&p
, 0);
5135 if (heads
< 1 || heads
> 16)
5140 secs
= strtol(p
, (char **)&p
, 0);
5141 if (secs
< 1 || secs
> 63)
5145 if (!strcmp(p
, "none"))
5146 translation
= BIOS_ATA_TRANSLATION_NONE
;
5147 else if (!strcmp(p
, "lba"))
5148 translation
= BIOS_ATA_TRANSLATION_LBA
;
5149 else if (!strcmp(p
, "auto"))
5150 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5153 } else if (*p
!= '\0') {
5155 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5160 case QEMU_OPTION_nographic
:
5161 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
5162 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
5165 case QEMU_OPTION_kernel
:
5166 kernel_filename
= optarg
;
5168 case QEMU_OPTION_append
:
5169 kernel_cmdline
= optarg
;
5171 case QEMU_OPTION_cdrom
:
5172 if (cdrom_index
>= 0) {
5173 hd_filename
[cdrom_index
] = optarg
;
5176 case QEMU_OPTION_boot
:
5177 boot_device
= optarg
[0];
5178 if (boot_device
!= 'a' &&
5181 boot_device
!= 'n' &&
5183 boot_device
!= 'c' && boot_device
!= 'd') {
5184 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
5188 case QEMU_OPTION_fda
:
5189 fd_filename
[0] = optarg
;
5191 case QEMU_OPTION_fdb
:
5192 fd_filename
[1] = optarg
;
5194 case QEMU_OPTION_no_code_copy
:
5195 code_copy_enabled
= 0;
5197 case QEMU_OPTION_net
:
5198 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5199 fprintf(stderr
, "qemu: too many network clients\n");
5202 pstrcpy(net_clients
[nb_net_clients
],
5203 sizeof(net_clients
[0]),
5208 case QEMU_OPTION_tftp
:
5209 tftp_prefix
= optarg
;
5212 case QEMU_OPTION_smb
:
5213 net_slirp_smb(optarg
);
5216 case QEMU_OPTION_redir
:
5217 net_slirp_redir(optarg
);
5221 case QEMU_OPTION_audio_help
:
5225 case QEMU_OPTION_soundhw
:
5226 select_soundhw (optarg
);
5233 ram_size
= atoi(optarg
) * 1024 * 1024;
5236 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
5237 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
5238 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
5247 mask
= cpu_str_to_log_mask(optarg
);
5249 printf("Log items (comma separated):\n");
5250 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5251 printf("%-10s %s\n", item
->name
, item
->help
);
5258 #ifdef CONFIG_GDBSTUB
5263 gdbstub_port
= atoi(optarg
);
5270 start_emulation
= 0;
5273 keyboard_layout
= optarg
;
5275 case QEMU_OPTION_localtime
:
5278 case QEMU_OPTION_cirrusvga
:
5279 cirrus_vga_enabled
= 1;
5281 case QEMU_OPTION_std_vga
:
5282 cirrus_vga_enabled
= 0;
5289 w
= strtol(p
, (char **)&p
, 10);
5292 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5298 h
= strtol(p
, (char **)&p
, 10);
5303 depth
= strtol(p
, (char **)&p
, 10);
5304 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5305 depth
!= 24 && depth
!= 32)
5307 } else if (*p
== '\0') {
5308 depth
= graphic_depth
;
5315 graphic_depth
= depth
;
5318 case QEMU_OPTION_monitor
:
5319 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
5321 case QEMU_OPTION_serial
:
5322 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5323 fprintf(stderr
, "qemu: too many serial ports\n");
5326 pstrcpy(serial_devices
[serial_device_index
],
5327 sizeof(serial_devices
[0]), optarg
);
5328 serial_device_index
++;
5330 case QEMU_OPTION_parallel
:
5331 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5332 fprintf(stderr
, "qemu: too many parallel ports\n");
5335 pstrcpy(parallel_devices
[parallel_device_index
],
5336 sizeof(parallel_devices
[0]), optarg
);
5337 parallel_device_index
++;
5339 case QEMU_OPTION_loadvm
:
5342 case QEMU_OPTION_full_screen
:
5345 case QEMU_OPTION_pidfile
:
5346 create_pidfile(optarg
);
5349 case QEMU_OPTION_win2k_hack
:
5350 win2k_install_hack
= 1;
5354 case QEMU_OPTION_no_kqemu
:
5357 case QEMU_OPTION_kernel_kqemu
:
5361 case QEMU_OPTION_usb
:
5364 case QEMU_OPTION_usbdevice
:
5366 if (usb_devices_index
>= MAX_VM_USB_PORTS
) {
5367 fprintf(stderr
, "Too many USB devices\n");
5370 pstrcpy(usb_devices
[usb_devices_index
],
5371 sizeof(usb_devices
[usb_devices_index
]),
5373 usb_devices_index
++;
5375 case QEMU_OPTION_smp
:
5376 smp_cpus
= atoi(optarg
);
5377 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
5378 fprintf(stderr
, "Invalid number of CPUs\n");
5382 case QEMU_OPTION_vnc
:
5383 vnc_display
= atoi(optarg
);
5384 if (vnc_display
< 0) {
5385 fprintf(stderr
, "Invalid VNC display\n");
5397 linux_boot
= (kernel_filename
!= NULL
);
5400 hd_filename
[0] == '\0' &&
5401 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
5402 fd_filename
[0] == '\0')
5405 /* boot to cd by default if no hard disk */
5406 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
5407 if (fd_filename
[0] != '\0')
5413 #if !defined(CONFIG_SOFTMMU)
5414 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
5416 static uint8_t stdout_buf
[4096];
5417 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
5420 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5427 /* init network clients */
5428 if (nb_net_clients
== 0) {
5429 /* if no clients, we use a default config */
5430 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
5432 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
5437 for(i
= 0;i
< nb_net_clients
; i
++) {
5438 if (net_client_init(net_clients
[i
]) < 0)
5442 /* init the memory */
5443 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
5445 #ifdef CONFIG_SOFTMMU
5446 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5447 if (!phys_ram_base
) {
5448 fprintf(stderr
, "Could not allocate physical memory\n");
5452 /* as we must map the same page at several addresses, we must use
5457 tmpdir
= getenv("QEMU_TMPDIR");
5460 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
5461 if (mkstemp(phys_ram_file
) < 0) {
5462 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
5466 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
5467 if (phys_ram_fd
< 0) {
5468 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
5472 ftruncate(phys_ram_fd
, phys_ram_size
);
5473 unlink(phys_ram_file
);
5474 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
5476 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
5478 if (phys_ram_base
== MAP_FAILED
) {
5479 fprintf(stderr
, "Could not map physical memory\n");
5485 /* we always create the cdrom drive, even if no disk is there */
5487 if (cdrom_index
>= 0) {
5488 bs_table
[cdrom_index
] = bdrv_new("cdrom");
5489 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
5492 /* open the virtual block devices */
5493 for(i
= 0; i
< MAX_DISKS
; i
++) {
5494 if (hd_filename
[i
]) {
5497 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
5498 bs_table
[i
] = bdrv_new(buf
);
5500 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
5501 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
5505 if (i
== 0 && cyls
!= 0) {
5506 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
5507 bdrv_set_translation_hint(bs_table
[i
], translation
);
5512 /* we always create at least one floppy disk */
5513 fd_table
[0] = bdrv_new("fda");
5514 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
5516 for(i
= 0; i
< MAX_FD
; i
++) {
5517 if (fd_filename
[i
]) {
5520 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
5521 fd_table
[i
] = bdrv_new(buf
);
5522 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
5524 if (fd_filename
[i
] != '\0') {
5525 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
5526 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
5534 /* init USB devices */
5536 vm_usb_hub
= usb_hub_init(vm_usb_ports
, MAX_VM_USB_PORTS
);
5537 for(i
= 0; i
< usb_devices_index
; i
++) {
5538 if (usb_device_add(usb_devices
[i
]) < 0) {
5539 fprintf(stderr
, "Warning: could not add USB device %s\n",
5545 register_savevm("timer", 0, 1, timer_save
, timer_load
, NULL
);
5546 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
5549 cpu_calibrate_ticks();
5553 dumb_display_init(ds
);
5554 } else if (vnc_display
!= -1) {
5555 vnc_display_init(ds
, vnc_display
);
5557 #if defined(CONFIG_SDL)
5558 sdl_display_init(ds
, full_screen
);
5559 #elif defined(CONFIG_COCOA)
5560 cocoa_display_init(ds
, full_screen
);
5562 dumb_display_init(ds
);
5566 monitor_hd
= qemu_chr_open(monitor_device
);
5568 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5571 monitor_init(monitor_hd
, !nographic
);
5573 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5574 if (serial_devices
[i
][0] != '\0') {
5575 serial_hds
[i
] = qemu_chr_open(serial_devices
[i
]);
5576 if (!serial_hds
[i
]) {
5577 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5581 if (!strcmp(serial_devices
[i
], "vc"))
5582 qemu_chr_printf(serial_hds
[i
], "serial%d console\n", i
);
5586 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5587 if (parallel_devices
[i
][0] != '\0') {
5588 parallel_hds
[i
] = qemu_chr_open(parallel_devices
[i
]);
5589 if (!parallel_hds
[i
]) {
5590 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5591 parallel_devices
[i
]);
5594 if (!strcmp(parallel_devices
[i
], "vc"))
5595 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\n", i
);
5599 /* setup cpu signal handlers for MMU / self modifying code handling */
5600 #if !defined(CONFIG_SOFTMMU)
5602 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5605 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
5606 stk
.ss_sp
= signal_stack
;
5607 stk
.ss_size
= SIGNAL_STACK_SIZE
;
5610 if (sigaltstack(&stk
, NULL
) < 0) {
5611 perror("sigaltstack");
5617 struct sigaction act
;
5619 sigfillset(&act
.sa_mask
);
5620 act
.sa_flags
= SA_SIGINFO
;
5621 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5622 act
.sa_flags
|= SA_ONSTACK
;
5624 act
.sa_sigaction
= host_segv_handler
;
5625 sigaction(SIGSEGV
, &act
, NULL
);
5626 sigaction(SIGBUS
, &act
, NULL
);
5627 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5628 sigaction(SIGFPE
, &act
, NULL
);
5635 struct sigaction act
;
5636 sigfillset(&act
.sa_mask
);
5638 act
.sa_handler
= SIG_IGN
;
5639 sigaction(SIGPIPE
, &act
, NULL
);
5644 machine
->init(ram_size
, vga_ram_size
, boot_device
,
5645 ds
, fd_filename
, snapshot
,
5646 kernel_filename
, kernel_cmdline
, initrd_filename
);
5648 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
5649 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
5651 #ifdef CONFIG_GDBSTUB
5653 if (gdbserver_start(gdbstub_port
) < 0) {
5654 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
5658 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
5663 qemu_loadvm(loadvm
);
5666 /* XXX: simplify init */
5668 if (start_emulation
) {