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)
160 int acpi_enabled
= 1;
162 /***********************************************************/
163 /* x86 ISA bus support */
165 target_phys_addr_t isa_mem_base
= 0;
168 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
170 #ifdef DEBUG_UNUSED_IOPORT
171 fprintf(stderr
, "inb: port=0x%04x\n", address
);
176 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
178 #ifdef DEBUG_UNUSED_IOPORT
179 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
183 /* default is to make two byte accesses */
184 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
187 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
188 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
189 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
193 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
195 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
196 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
197 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
200 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
202 #ifdef DEBUG_UNUSED_IOPORT
203 fprintf(stderr
, "inl: port=0x%04x\n", address
);
208 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
210 #ifdef DEBUG_UNUSED_IOPORT
211 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
215 void init_ioports(void)
219 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
220 ioport_read_table
[0][i
] = default_ioport_readb
;
221 ioport_write_table
[0][i
] = default_ioport_writeb
;
222 ioport_read_table
[1][i
] = default_ioport_readw
;
223 ioport_write_table
[1][i
] = default_ioport_writew
;
224 ioport_read_table
[2][i
] = default_ioport_readl
;
225 ioport_write_table
[2][i
] = default_ioport_writel
;
229 /* size is the word size in byte */
230 int register_ioport_read(int start
, int length
, int size
,
231 IOPortReadFunc
*func
, void *opaque
)
237 } else if (size
== 2) {
239 } else if (size
== 4) {
242 hw_error("register_ioport_read: invalid size");
245 for(i
= start
; i
< start
+ length
; i
+= size
) {
246 ioport_read_table
[bsize
][i
] = func
;
247 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
248 hw_error("register_ioport_read: invalid opaque");
249 ioport_opaque
[i
] = opaque
;
254 /* size is the word size in byte */
255 int register_ioport_write(int start
, int length
, int size
,
256 IOPortWriteFunc
*func
, void *opaque
)
262 } else if (size
== 2) {
264 } else if (size
== 4) {
267 hw_error("register_ioport_write: invalid size");
270 for(i
= start
; i
< start
+ length
; i
+= size
) {
271 ioport_write_table
[bsize
][i
] = func
;
272 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
273 hw_error("register_ioport_read: invalid opaque");
274 ioport_opaque
[i
] = opaque
;
279 void isa_unassign_ioport(int start
, int length
)
283 for(i
= start
; i
< start
+ length
; i
++) {
284 ioport_read_table
[0][i
] = default_ioport_readb
;
285 ioport_read_table
[1][i
] = default_ioport_readw
;
286 ioport_read_table
[2][i
] = default_ioport_readl
;
288 ioport_write_table
[0][i
] = default_ioport_writeb
;
289 ioport_write_table
[1][i
] = default_ioport_writew
;
290 ioport_write_table
[2][i
] = default_ioport_writel
;
294 /***********************************************************/
296 void pstrcpy(char *buf
, int buf_size
, const char *str
)
306 if (c
== 0 || q
>= buf
+ buf_size
- 1)
313 /* strcat and truncate. */
314 char *pstrcat(char *buf
, int buf_size
, const char *s
)
319 pstrcpy(buf
+ len
, buf_size
- len
, s
);
323 int strstart(const char *str
, const char *val
, const char **ptr
)
339 void cpu_outb(CPUState
*env
, int addr
, int val
)
342 if (loglevel
& CPU_LOG_IOPORT
)
343 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
345 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
348 env
->last_io_time
= cpu_get_time_fast();
352 void cpu_outw(CPUState
*env
, int addr
, int val
)
355 if (loglevel
& CPU_LOG_IOPORT
)
356 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
358 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
361 env
->last_io_time
= cpu_get_time_fast();
365 void cpu_outl(CPUState
*env
, int addr
, int val
)
368 if (loglevel
& CPU_LOG_IOPORT
)
369 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
371 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
374 env
->last_io_time
= cpu_get_time_fast();
378 int cpu_inb(CPUState
*env
, int addr
)
381 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
383 if (loglevel
& CPU_LOG_IOPORT
)
384 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
388 env
->last_io_time
= cpu_get_time_fast();
393 int cpu_inw(CPUState
*env
, int addr
)
396 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
398 if (loglevel
& CPU_LOG_IOPORT
)
399 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
403 env
->last_io_time
= cpu_get_time_fast();
408 int cpu_inl(CPUState
*env
, int addr
)
411 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
413 if (loglevel
& CPU_LOG_IOPORT
)
414 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
418 env
->last_io_time
= cpu_get_time_fast();
423 /***********************************************************/
424 void hw_error(const char *fmt
, ...)
430 fprintf(stderr
, "qemu: hardware error: ");
431 vfprintf(stderr
, fmt
, ap
);
432 fprintf(stderr
, "\n");
433 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
434 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
436 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
438 cpu_dump_state(env
, stderr
, fprintf
, 0);
445 /***********************************************************/
448 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
449 static void *qemu_put_kbd_event_opaque
;
450 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
451 static void *qemu_put_mouse_event_opaque
;
452 static int qemu_put_mouse_event_absolute
;
454 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
456 qemu_put_kbd_event_opaque
= opaque
;
457 qemu_put_kbd_event
= func
;
460 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
, int absolute
)
462 qemu_put_mouse_event_opaque
= opaque
;
463 qemu_put_mouse_event
= func
;
464 qemu_put_mouse_event_absolute
= absolute
;
467 void kbd_put_keycode(int keycode
)
469 if (qemu_put_kbd_event
) {
470 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
474 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
476 if (qemu_put_mouse_event
) {
477 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
478 dx
, dy
, dz
, buttons_state
);
482 int kbd_mouse_is_absolute(void)
484 return qemu_put_mouse_event_absolute
;
487 /***********************************************************/
490 #if defined(__powerpc__)
492 static inline uint32_t get_tbl(void)
495 asm volatile("mftb %0" : "=r" (tbl
));
499 static inline uint32_t get_tbu(void)
502 asm volatile("mftbu %0" : "=r" (tbl
));
506 int64_t cpu_get_real_ticks(void)
509 /* NOTE: we test if wrapping has occurred */
515 return ((int64_t)h
<< 32) | l
;
518 #elif defined(__i386__)
520 int64_t cpu_get_real_ticks(void)
524 QueryPerformanceCounter(&ti
);
528 asm volatile ("rdtsc" : "=A" (val
));
533 #elif defined(__x86_64__)
535 int64_t cpu_get_real_ticks(void)
539 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
546 #elif defined(__ia64)
548 int64_t cpu_get_real_ticks(void)
551 asm volatile ("mov %0 = ar.itc" : "=r"(val
) :: "memory");
555 #elif defined(__s390__)
557 int64_t cpu_get_real_ticks(void)
560 asm volatile("stck 0(%1)" : "=m" (val
) : "a" (&val
) : "cc");
565 #error unsupported CPU
568 static int64_t cpu_ticks_prev
;
569 static int64_t cpu_ticks_offset
;
570 static int cpu_ticks_enabled
;
572 static inline int64_t cpu_get_ticks(void)
574 if (!cpu_ticks_enabled
) {
575 return cpu_ticks_offset
;
578 ticks
= cpu_get_real_ticks();
579 if (cpu_ticks_prev
> ticks
) {
580 /* Note: non increasing ticks may happen if the host uses
582 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
584 cpu_ticks_prev
= ticks
;
585 return ticks
+ cpu_ticks_offset
;
589 /* enable cpu_get_ticks() */
590 void cpu_enable_ticks(void)
592 if (!cpu_ticks_enabled
) {
593 cpu_ticks_offset
-= cpu_get_real_ticks();
594 cpu_ticks_enabled
= 1;
598 /* disable cpu_get_ticks() : the clock is stopped. You must not call
599 cpu_get_ticks() after that. */
600 void cpu_disable_ticks(void)
602 if (cpu_ticks_enabled
) {
603 cpu_ticks_offset
= cpu_get_ticks();
604 cpu_ticks_enabled
= 0;
609 void cpu_calibrate_ticks(void)
614 ret
= QueryPerformanceFrequency(&freq
);
616 fprintf(stderr
, "Could not calibrate ticks\n");
619 ticks_per_sec
= freq
.QuadPart
;
623 static int64_t get_clock(void)
626 gettimeofday(&tv
, NULL
);
627 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
630 void cpu_calibrate_ticks(void)
635 ticks
= cpu_get_real_ticks();
637 usec
= get_clock() - usec
;
638 ticks
= cpu_get_real_ticks() - ticks
;
639 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
643 /* compute with 96 bit intermediate result: (a*b)/c */
644 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
649 #ifdef WORDS_BIGENDIAN
659 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
660 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
663 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
667 #define QEMU_TIMER_REALTIME 0
668 #define QEMU_TIMER_VIRTUAL 1
672 /* XXX: add frequency */
680 struct QEMUTimer
*next
;
686 static QEMUTimer
*active_timers
[2];
688 static MMRESULT timerID
;
689 static HANDLE host_alarm
= NULL
;
690 static unsigned int period
= 1;
692 /* frequency of the times() clock tick */
693 static int timer_freq
;
696 QEMUClock
*qemu_new_clock(int type
)
699 clock
= qemu_mallocz(sizeof(QEMUClock
));
706 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
710 ts
= qemu_mallocz(sizeof(QEMUTimer
));
717 void qemu_free_timer(QEMUTimer
*ts
)
722 /* stop a timer, but do not dealloc it */
723 void qemu_del_timer(QEMUTimer
*ts
)
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
];
742 /* modify the current timer so that it will be fired when current_time
743 >= expire_time. The corresponding callback will be called. */
744 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
750 /* add the timer in the sorted list */
751 /* NOTE: this code must be signal safe because
752 qemu_timer_expired() can be called from a signal. */
753 pt
= &active_timers
[ts
->clock
->type
];
758 if (t
->expire_time
> expire_time
)
762 ts
->expire_time
= expire_time
;
767 int qemu_timer_pending(QEMUTimer
*ts
)
770 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
777 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
781 return (timer_head
->expire_time
<= current_time
);
784 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
790 if (!ts
|| ts
->expire_time
> current_time
)
792 /* remove timer from the list before calling the callback */
793 *ptimer_head
= ts
->next
;
796 /* run the callback (the timer list can be modified) */
801 int64_t qemu_get_clock(QEMUClock
*clock
)
803 switch(clock
->type
) {
804 case QEMU_TIMER_REALTIME
:
806 return GetTickCount();
811 /* Note that using gettimeofday() is not a good solution
812 for timers because its value change when the date is
814 if (timer_freq
== 100) {
815 return times(&tp
) * 10;
817 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
822 case QEMU_TIMER_VIRTUAL
:
823 return cpu_get_ticks();
828 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
830 uint64_t expire_time
;
832 if (qemu_timer_pending(ts
)) {
833 expire_time
= ts
->expire_time
;
837 qemu_put_be64(f
, expire_time
);
840 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
842 uint64_t expire_time
;
844 expire_time
= qemu_get_be64(f
);
845 if (expire_time
!= -1) {
846 qemu_mod_timer(ts
, expire_time
);
852 static void timer_save(QEMUFile
*f
, void *opaque
)
854 if (cpu_ticks_enabled
) {
855 hw_error("cannot save state if virtual timers are running");
857 qemu_put_be64s(f
, &cpu_ticks_offset
);
858 qemu_put_be64s(f
, &ticks_per_sec
);
861 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
865 if (cpu_ticks_enabled
) {
868 qemu_get_be64s(f
, &cpu_ticks_offset
);
869 qemu_get_be64s(f
, &ticks_per_sec
);
874 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
875 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
877 static void host_alarm_handler(int host_signum
)
881 #define DISP_FREQ 1000
883 static int64_t delta_min
= INT64_MAX
;
884 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
886 ti
= qemu_get_clock(vm_clock
);
887 if (last_clock
!= 0) {
888 delta
= ti
- last_clock
;
889 if (delta
< delta_min
)
891 if (delta
> delta_max
)
894 if (++count
== DISP_FREQ
) {
895 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
896 muldiv64(delta_min
, 1000000, ticks_per_sec
),
897 muldiv64(delta_max
, 1000000, ticks_per_sec
),
898 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
899 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
901 delta_min
= INT64_MAX
;
909 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
910 qemu_get_clock(vm_clock
)) ||
911 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
912 qemu_get_clock(rt_clock
))) {
914 SetEvent(host_alarm
);
916 CPUState
*env
= cpu_single_env
;
918 /* stop the currently executing cpu because a timer occured */
919 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
921 if (env
->kqemu_enabled
) {
922 kqemu_cpu_interrupt(env
);
931 #if defined(__linux__)
933 #define RTC_FREQ 1024
937 static int start_rtc_timer(void)
939 rtc_fd
= open("/dev/rtc", O_RDONLY
);
942 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
943 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
944 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
945 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
948 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
953 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
959 static int start_rtc_timer(void)
964 #endif /* !defined(__linux__) */
966 #endif /* !defined(_WIN32) */
968 static void init_timers(void)
970 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
971 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
978 ZeroMemory(&tc
, sizeof(TIMECAPS
));
979 timeGetDevCaps(&tc
, sizeof(TIMECAPS
));
980 if (period
< tc
.wPeriodMin
)
981 period
= tc
.wPeriodMin
;
982 timeBeginPeriod(period
);
983 timerID
= timeSetEvent(1, // interval (ms)
984 period
, // resolution
985 host_alarm_handler
, // function
986 (DWORD
)&count
, // user parameter
987 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
989 perror("failed timer alarm");
992 host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
994 perror("failed CreateEvent");
997 ResetEvent(host_alarm
);
999 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
1002 struct sigaction act
;
1003 struct itimerval itv
;
1005 /* get times() syscall frequency */
1006 timer_freq
= sysconf(_SC_CLK_TCK
);
1009 sigfillset(&act
.sa_mask
);
1011 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1012 act
.sa_flags
|= SA_ONSTACK
;
1014 act
.sa_handler
= host_alarm_handler
;
1015 sigaction(SIGALRM
, &act
, NULL
);
1017 itv
.it_interval
.tv_sec
= 0;
1018 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
1019 itv
.it_value
.tv_sec
= 0;
1020 itv
.it_value
.tv_usec
= 10 * 1000;
1021 setitimer(ITIMER_REAL
, &itv
, NULL
);
1022 /* we probe the tick duration of the kernel to inform the user if
1023 the emulated kernel requested a too high timer frequency */
1024 getitimer(ITIMER_REAL
, &itv
);
1026 #if defined(__linux__)
1027 /* XXX: force /dev/rtc usage because even 2.6 kernels may not
1028 have timers with 1 ms resolution. The correct solution will
1029 be to use the POSIX real time timers available in recent
1031 if (itv
.it_interval
.tv_usec
> 1000 || 1) {
1032 /* try to use /dev/rtc to have a faster timer */
1033 if (start_rtc_timer() < 0)
1035 /* disable itimer */
1036 itv
.it_interval
.tv_sec
= 0;
1037 itv
.it_interval
.tv_usec
= 0;
1038 itv
.it_value
.tv_sec
= 0;
1039 itv
.it_value
.tv_usec
= 0;
1040 setitimer(ITIMER_REAL
, &itv
, NULL
);
1043 sigaction(SIGIO
, &act
, NULL
);
1044 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1045 fcntl(rtc_fd
, F_SETOWN
, getpid());
1047 #endif /* defined(__linux__) */
1050 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1051 PIT_FREQ
) / 1000000;
1057 void quit_timers(void)
1060 timeKillEvent(timerID
);
1061 timeEndPeriod(period
);
1063 CloseHandle(host_alarm
);
1069 /***********************************************************/
1070 /* character device */
1072 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1074 return s
->chr_write(s
, buf
, len
);
1077 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1081 return s
->chr_ioctl(s
, cmd
, arg
);
1084 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1089 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1090 qemu_chr_write(s
, buf
, strlen(buf
));
1094 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1096 if (s
->chr_send_event
)
1097 s
->chr_send_event(s
, event
);
1100 void qemu_chr_add_read_handler(CharDriverState
*s
,
1101 IOCanRWHandler
*fd_can_read
,
1102 IOReadHandler
*fd_read
, void *opaque
)
1104 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
1107 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
1109 s
->chr_event
= chr_event
;
1112 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1117 static void null_chr_add_read_handler(CharDriverState
*chr
,
1118 IOCanRWHandler
*fd_can_read
,
1119 IOReadHandler
*fd_read
, void *opaque
)
1123 CharDriverState
*qemu_chr_open_null(void)
1125 CharDriverState
*chr
;
1127 chr
= qemu_mallocz(sizeof(CharDriverState
));
1130 chr
->chr_write
= null_chr_write
;
1131 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1137 static void socket_cleanup(void)
1142 static int socket_init(void)
1147 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1149 err
= WSAGetLastError();
1150 fprintf(stderr
, "WSAStartup: %d\n", err
);
1153 atexit(socket_cleanup
);
1157 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1163 ret
= send(fd
, buf
, len
, 0);
1166 errno
= WSAGetLastError();
1167 if (errno
!= WSAEWOULDBLOCK
) {
1170 } else if (ret
== 0) {
1180 void socket_set_nonblock(int fd
)
1182 unsigned long opt
= 1;
1183 ioctlsocket(fd
, FIONBIO
, &opt
);
1188 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1194 ret
= write(fd
, buf
, len
);
1196 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1198 } else if (ret
== 0) {
1208 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1210 return unix_write(fd
, buf
, len1
);
1213 void socket_set_nonblock(int fd
)
1215 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1217 #endif /* !_WIN32 */
1223 IOCanRWHandler
*fd_can_read
;
1224 IOReadHandler
*fd_read
;
1229 #define STDIO_MAX_CLIENTS 2
1231 static int stdio_nb_clients
;
1232 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1234 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1236 FDCharDriver
*s
= chr
->opaque
;
1237 return unix_write(s
->fd_out
, buf
, len
);
1240 static int fd_chr_read_poll(void *opaque
)
1242 CharDriverState
*chr
= opaque
;
1243 FDCharDriver
*s
= chr
->opaque
;
1245 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
1249 static void fd_chr_read(void *opaque
)
1251 CharDriverState
*chr
= opaque
;
1252 FDCharDriver
*s
= chr
->opaque
;
1257 if (len
> s
->max_size
)
1261 size
= read(s
->fd_in
, buf
, len
);
1263 s
->fd_read(s
->fd_opaque
, buf
, size
);
1267 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1268 IOCanRWHandler
*fd_can_read
,
1269 IOReadHandler
*fd_read
, void *opaque
)
1271 FDCharDriver
*s
= chr
->opaque
;
1273 if (s
->fd_in
>= 0) {
1274 s
->fd_can_read
= fd_can_read
;
1275 s
->fd_read
= fd_read
;
1276 s
->fd_opaque
= opaque
;
1277 if (nographic
&& s
->fd_in
== 0) {
1279 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1280 fd_chr_read
, NULL
, chr
);
1285 /* open a character device to a unix fd */
1286 CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1288 CharDriverState
*chr
;
1291 chr
= qemu_mallocz(sizeof(CharDriverState
));
1294 s
= qemu_mallocz(sizeof(FDCharDriver
));
1302 chr
->chr_write
= fd_chr_write
;
1303 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1307 CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1311 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1314 return qemu_chr_open_fd(-1, fd_out
);
1317 CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1321 fd
= open(filename
, O_RDWR
| O_BINARY
);
1324 return qemu_chr_open_fd(fd
, fd
);
1328 /* for STDIO, we handle the case where several clients use it
1331 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1333 #define TERM_FIFO_MAX_SIZE 1
1335 static int term_got_escape
, client_index
;
1336 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1339 void term_print_help(void)
1342 "C-a h print this help\n"
1343 "C-a x exit emulator\n"
1344 "C-a s save disk data back to file (if -snapshot)\n"
1345 "C-a b send break (magic sysrq)\n"
1346 "C-a c switch between console and monitor\n"
1347 "C-a C-a send C-a\n"
1351 /* called when a char is received */
1352 static void stdio_received_byte(int ch
)
1354 if (term_got_escape
) {
1355 term_got_escape
= 0;
1366 for (i
= 0; i
< MAX_DISKS
; i
++) {
1368 bdrv_commit(bs_table
[i
]);
1373 if (client_index
< stdio_nb_clients
) {
1374 CharDriverState
*chr
;
1377 chr
= stdio_clients
[client_index
];
1379 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1384 if (client_index
>= stdio_nb_clients
)
1386 if (client_index
== 0) {
1387 /* send a new line in the monitor to get the prompt */
1395 } else if (ch
== TERM_ESCAPE
) {
1396 term_got_escape
= 1;
1399 if (client_index
< stdio_nb_clients
) {
1401 CharDriverState
*chr
;
1404 chr
= stdio_clients
[client_index
];
1406 if (s
->fd_can_read(s
->fd_opaque
) > 0) {
1408 s
->fd_read(s
->fd_opaque
, buf
, 1);
1409 } else if (term_fifo_size
== 0) {
1410 term_fifo
[term_fifo_size
++] = ch
;
1416 static int stdio_read_poll(void *opaque
)
1418 CharDriverState
*chr
;
1421 if (client_index
< stdio_nb_clients
) {
1422 chr
= stdio_clients
[client_index
];
1424 /* try to flush the queue if needed */
1425 if (term_fifo_size
!= 0 && s
->fd_can_read(s
->fd_opaque
) > 0) {
1426 s
->fd_read(s
->fd_opaque
, term_fifo
, 1);
1429 /* see if we can absorb more chars */
1430 if (term_fifo_size
== 0)
1439 static void stdio_read(void *opaque
)
1444 size
= read(0, buf
, 1);
1446 stdio_received_byte(buf
[0]);
1449 /* init terminal so that we can grab keys */
1450 static struct termios oldtty
;
1451 static int old_fd0_flags
;
1453 static void term_exit(void)
1455 tcsetattr (0, TCSANOW
, &oldtty
);
1456 fcntl(0, F_SETFL
, old_fd0_flags
);
1459 static void term_init(void)
1463 tcgetattr (0, &tty
);
1465 old_fd0_flags
= fcntl(0, F_GETFL
);
1467 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1468 |INLCR
|IGNCR
|ICRNL
|IXON
);
1469 tty
.c_oflag
|= OPOST
;
1470 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1471 /* if graphical mode, we allow Ctrl-C handling */
1473 tty
.c_lflag
&= ~ISIG
;
1474 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1477 tty
.c_cc
[VTIME
] = 0;
1479 tcsetattr (0, TCSANOW
, &tty
);
1483 fcntl(0, F_SETFL
, O_NONBLOCK
);
1486 CharDriverState
*qemu_chr_open_stdio(void)
1488 CharDriverState
*chr
;
1491 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1493 chr
= qemu_chr_open_fd(0, 1);
1494 if (stdio_nb_clients
== 0)
1495 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, NULL
);
1496 client_index
= stdio_nb_clients
;
1498 if (stdio_nb_clients
!= 0)
1500 chr
= qemu_chr_open_fd(0, 1);
1502 stdio_clients
[stdio_nb_clients
++] = chr
;
1503 if (stdio_nb_clients
== 1) {
1504 /* set the terminal in raw mode */
1510 #if defined(__linux__)
1511 CharDriverState
*qemu_chr_open_pty(void)
1514 char slave_name
[1024];
1515 int master_fd
, slave_fd
;
1517 /* Not satisfying */
1518 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1522 /* Disabling local echo and line-buffered output */
1523 tcgetattr (master_fd
, &tty
);
1524 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1526 tty
.c_cc
[VTIME
] = 0;
1527 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1529 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1530 return qemu_chr_open_fd(master_fd
, master_fd
);
1533 static void tty_serial_init(int fd
, int speed
,
1534 int parity
, int data_bits
, int stop_bits
)
1540 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1541 speed
, parity
, data_bits
, stop_bits
);
1543 tcgetattr (fd
, &tty
);
1585 cfsetispeed(&tty
, spd
);
1586 cfsetospeed(&tty
, spd
);
1588 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1589 |INLCR
|IGNCR
|ICRNL
|IXON
);
1590 tty
.c_oflag
|= OPOST
;
1591 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1592 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
);
1613 tty
.c_cflag
|= PARENB
;
1616 tty
.c_cflag
|= PARENB
| PARODD
;
1620 tcsetattr (fd
, TCSANOW
, &tty
);
1623 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1625 FDCharDriver
*s
= chr
->opaque
;
1628 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1630 QEMUSerialSetParams
*ssp
= arg
;
1631 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1632 ssp
->data_bits
, ssp
->stop_bits
);
1635 case CHR_IOCTL_SERIAL_SET_BREAK
:
1637 int enable
= *(int *)arg
;
1639 tcsendbreak(s
->fd_in
, 1);
1648 CharDriverState
*qemu_chr_open_tty(const char *filename
)
1650 CharDriverState
*chr
;
1653 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1656 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1657 tty_serial_init(fd
, 115200, 'N', 8, 1);
1658 chr
= qemu_chr_open_fd(fd
, fd
);
1661 chr
->chr_ioctl
= tty_serial_ioctl
;
1665 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1667 int fd
= (int)chr
->opaque
;
1671 case CHR_IOCTL_PP_READ_DATA
:
1672 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1674 *(uint8_t *)arg
= b
;
1676 case CHR_IOCTL_PP_WRITE_DATA
:
1677 b
= *(uint8_t *)arg
;
1678 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1681 case CHR_IOCTL_PP_READ_CONTROL
:
1682 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1684 *(uint8_t *)arg
= b
;
1686 case CHR_IOCTL_PP_WRITE_CONTROL
:
1687 b
= *(uint8_t *)arg
;
1688 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1691 case CHR_IOCTL_PP_READ_STATUS
:
1692 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1694 *(uint8_t *)arg
= b
;
1702 CharDriverState
*qemu_chr_open_pp(const char *filename
)
1704 CharDriverState
*chr
;
1707 fd
= open(filename
, O_RDWR
);
1711 if (ioctl(fd
, PPCLAIM
) < 0) {
1716 chr
= qemu_mallocz(sizeof(CharDriverState
));
1721 chr
->opaque
= (void *)fd
;
1722 chr
->chr_write
= null_chr_write
;
1723 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1724 chr
->chr_ioctl
= pp_ioctl
;
1729 CharDriverState
*qemu_chr_open_pty(void)
1735 #endif /* !defined(_WIN32) */
1739 IOCanRWHandler
*fd_can_read
;
1740 IOReadHandler
*fd_read
;
1743 HANDLE hcom
, hrecv
, hsend
;
1744 OVERLAPPED orecv
, osend
;
1749 #define NSENDBUF 2048
1750 #define NRECVBUF 2048
1751 #define MAXCONNECT 1
1752 #define NTIMEOUT 5000
1754 static int win_chr_poll(void *opaque
);
1755 static int win_chr_pipe_poll(void *opaque
);
1757 static void win_chr_close2(WinCharState
*s
)
1760 CloseHandle(s
->hsend
);
1764 CloseHandle(s
->hrecv
);
1768 CloseHandle(s
->hcom
);
1772 qemu_del_polling_cb(win_chr_pipe_poll
, s
);
1774 qemu_del_polling_cb(win_chr_poll
, s
);
1777 static void win_chr_close(CharDriverState
*chr
)
1779 WinCharState
*s
= chr
->opaque
;
1783 static int win_chr_init(WinCharState
*s
, const char *filename
)
1786 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1791 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1793 fprintf(stderr
, "Failed CreateEvent\n");
1796 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1798 fprintf(stderr
, "Failed CreateEvent\n");
1802 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1803 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1804 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1805 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1810 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1811 fprintf(stderr
, "Failed SetupComm\n");
1815 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1816 size
= sizeof(COMMCONFIG
);
1817 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1818 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1819 CommConfigDialog(filename
, NULL
, &comcfg
);
1821 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1822 fprintf(stderr
, "Failed SetCommState\n");
1826 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1827 fprintf(stderr
, "Failed SetCommMask\n");
1831 cto
.ReadIntervalTimeout
= MAXDWORD
;
1832 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1833 fprintf(stderr
, "Failed SetCommTimeouts\n");
1837 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1838 fprintf(stderr
, "Failed ClearCommError\n");
1841 qemu_add_polling_cb(win_chr_poll
, s
);
1849 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1851 WinCharState
*s
= chr
->opaque
;
1852 DWORD len
, ret
, size
, err
;
1855 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1856 s
->osend
.hEvent
= s
->hsend
;
1859 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1861 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1863 err
= GetLastError();
1864 if (err
== ERROR_IO_PENDING
) {
1865 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1883 static int win_chr_read_poll(WinCharState
*s
)
1885 s
->max_size
= s
->fd_can_read(s
->win_opaque
);
1889 static void win_chr_readfile(WinCharState
*s
)
1895 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1896 s
->orecv
.hEvent
= s
->hrecv
;
1897 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1899 err
= GetLastError();
1900 if (err
== ERROR_IO_PENDING
) {
1901 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1906 s
->fd_read(s
->win_opaque
, buf
, size
);
1910 static void win_chr_read(WinCharState
*s
)
1912 if (s
->len
> s
->max_size
)
1913 s
->len
= s
->max_size
;
1917 win_chr_readfile(s
);
1920 static int win_chr_poll(void *opaque
)
1922 WinCharState
*s
= opaque
;
1926 ClearCommError(s
->hcom
, &comerr
, &status
);
1927 if (status
.cbInQue
> 0) {
1928 s
->len
= status
.cbInQue
;
1929 win_chr_read_poll(s
);
1936 static void win_chr_add_read_handler(CharDriverState
*chr
,
1937 IOCanRWHandler
*fd_can_read
,
1938 IOReadHandler
*fd_read
, void *opaque
)
1940 WinCharState
*s
= chr
->opaque
;
1942 s
->fd_can_read
= fd_can_read
;
1943 s
->fd_read
= fd_read
;
1944 s
->win_opaque
= opaque
;
1947 CharDriverState
*qemu_chr_open_win(const char *filename
)
1949 CharDriverState
*chr
;
1952 chr
= qemu_mallocz(sizeof(CharDriverState
));
1955 s
= qemu_mallocz(sizeof(WinCharState
));
1961 chr
->chr_write
= win_chr_write
;
1962 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
1963 chr
->chr_close
= win_chr_close
;
1965 if (win_chr_init(s
, filename
) < 0) {
1973 static int win_chr_pipe_poll(void *opaque
)
1975 WinCharState
*s
= opaque
;
1978 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1981 win_chr_read_poll(s
);
1988 static int win_chr_pipe_init(WinCharState
*s
, const char *filename
)
1997 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1999 fprintf(stderr
, "Failed CreateEvent\n");
2002 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2004 fprintf(stderr
, "Failed CreateEvent\n");
2008 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2009 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2010 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2012 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2013 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2014 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2019 ZeroMemory(&ov
, sizeof(ov
));
2020 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2021 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2023 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2027 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2029 fprintf(stderr
, "Failed GetOverlappedResult\n");
2031 CloseHandle(ov
.hEvent
);
2038 CloseHandle(ov
.hEvent
);
2041 qemu_add_polling_cb(win_chr_pipe_poll
, s
);
2050 CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2052 CharDriverState
*chr
;
2055 chr
= qemu_mallocz(sizeof(CharDriverState
));
2058 s
= qemu_mallocz(sizeof(WinCharState
));
2064 chr
->chr_write
= win_chr_write
;
2065 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2066 chr
->chr_close
= win_chr_close
;
2068 if (win_chr_pipe_init(s
, filename
) < 0) {
2076 CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2078 CharDriverState
*chr
;
2081 chr
= qemu_mallocz(sizeof(CharDriverState
));
2084 s
= qemu_mallocz(sizeof(WinCharState
));
2091 chr
->chr_write
= win_chr_write
;
2092 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2096 CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2100 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2101 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2102 if (fd_out
== INVALID_HANDLE_VALUE
)
2105 return qemu_chr_open_win_file(fd_out
);
2109 CharDriverState
*qemu_chr_open(const char *filename
)
2113 if (!strcmp(filename
, "vc")) {
2114 return text_console_init(&display_state
);
2115 } else if (!strcmp(filename
, "null")) {
2116 return qemu_chr_open_null();
2119 if (strstart(filename
, "file:", &p
)) {
2120 return qemu_chr_open_file_out(p
);
2121 } else if (strstart(filename
, "pipe:", &p
)) {
2122 return qemu_chr_open_pipe(p
);
2123 } else if (!strcmp(filename
, "pty")) {
2124 return qemu_chr_open_pty();
2125 } else if (!strcmp(filename
, "stdio")) {
2126 return qemu_chr_open_stdio();
2129 #if defined(__linux__)
2130 if (strstart(filename
, "/dev/parport", NULL
)) {
2131 return qemu_chr_open_pp(filename
);
2133 if (strstart(filename
, "/dev/", NULL
)) {
2134 return qemu_chr_open_tty(filename
);
2138 if (strstart(filename
, "COM", NULL
)) {
2139 return qemu_chr_open_win(filename
);
2141 if (strstart(filename
, "pipe:", &p
)) {
2142 return qemu_chr_open_win_pipe(p
);
2144 if (strstart(filename
, "file:", &p
)) {
2145 return qemu_chr_open_win_file_out(p
);
2153 void qemu_chr_close(CharDriverState
*chr
)
2156 chr
->chr_close(chr
);
2159 /***********************************************************/
2160 /* network device redirectors */
2162 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2166 for(i
=0;i
<size
;i
+=16) {
2170 fprintf(f
, "%08x ", i
);
2173 fprintf(f
, " %02x", buf
[i
+j
]);
2178 for(j
=0;j
<len
;j
++) {
2180 if (c
< ' ' || c
> '~')
2182 fprintf(f
, "%c", c
);
2188 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
2191 for(i
= 0; i
< 6; i
++) {
2192 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2205 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
2210 p1
= strchr(p
, sep
);
2216 if (len
> buf_size
- 1)
2218 memcpy(buf
, p
, len
);
2225 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
2233 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2235 saddr
->sin_family
= AF_INET
;
2236 if (buf
[0] == '\0') {
2237 saddr
->sin_addr
.s_addr
= 0;
2239 if (isdigit(buf
[0])) {
2240 if (!inet_aton(buf
, &saddr
->sin_addr
))
2243 if ((he
= gethostbyname(buf
)) == NULL
)
2245 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
2248 port
= strtol(p
, (char **)&r
, 0);
2251 saddr
->sin_port
= htons(port
);
2255 /* find or alloc a new VLAN */
2256 VLANState
*qemu_find_vlan(int id
)
2258 VLANState
**pvlan
, *vlan
;
2259 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2263 vlan
= qemu_mallocz(sizeof(VLANState
));
2268 pvlan
= &first_vlan
;
2269 while (*pvlan
!= NULL
)
2270 pvlan
= &(*pvlan
)->next
;
2275 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
2276 IOReadHandler
*fd_read
,
2277 IOCanRWHandler
*fd_can_read
,
2280 VLANClientState
*vc
, **pvc
;
2281 vc
= qemu_mallocz(sizeof(VLANClientState
));
2284 vc
->fd_read
= fd_read
;
2285 vc
->fd_can_read
= fd_can_read
;
2286 vc
->opaque
= opaque
;
2290 pvc
= &vlan
->first_client
;
2291 while (*pvc
!= NULL
)
2292 pvc
= &(*pvc
)->next
;
2297 int qemu_can_send_packet(VLANClientState
*vc1
)
2299 VLANState
*vlan
= vc1
->vlan
;
2300 VLANClientState
*vc
;
2302 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2304 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
2311 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
2313 VLANState
*vlan
= vc1
->vlan
;
2314 VLANClientState
*vc
;
2317 printf("vlan %d send:\n", vlan
->id
);
2318 hex_dump(stdout
, buf
, size
);
2320 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2322 vc
->fd_read(vc
->opaque
, buf
, size
);
2327 #if defined(CONFIG_SLIRP)
2329 /* slirp network adapter */
2331 static int slirp_inited
;
2332 static VLANClientState
*slirp_vc
;
2334 int slirp_can_output(void)
2336 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
2339 void slirp_output(const uint8_t *pkt
, int pkt_len
)
2342 printf("slirp output:\n");
2343 hex_dump(stdout
, pkt
, pkt_len
);
2347 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
2350 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
2353 printf("slirp input:\n");
2354 hex_dump(stdout
, buf
, size
);
2356 slirp_input(buf
, size
);
2359 static int net_slirp_init(VLANState
*vlan
)
2361 if (!slirp_inited
) {
2365 slirp_vc
= qemu_new_vlan_client(vlan
,
2366 slirp_receive
, NULL
, NULL
);
2367 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
2371 static void net_slirp_redir(const char *redir_str
)
2376 struct in_addr guest_addr
;
2377 int host_port
, guest_port
;
2379 if (!slirp_inited
) {
2385 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2387 if (!strcmp(buf
, "tcp")) {
2389 } else if (!strcmp(buf
, "udp")) {
2395 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2397 host_port
= strtol(buf
, &r
, 0);
2401 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2403 if (buf
[0] == '\0') {
2404 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
2406 if (!inet_aton(buf
, &guest_addr
))
2409 guest_port
= strtol(p
, &r
, 0);
2413 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
2414 fprintf(stderr
, "qemu: could not set up redirection\n");
2419 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
2427 static void smb_exit(void)
2431 char filename
[1024];
2433 /* erase all the files in the directory */
2434 d
= opendir(smb_dir
);
2439 if (strcmp(de
->d_name
, ".") != 0 &&
2440 strcmp(de
->d_name
, "..") != 0) {
2441 snprintf(filename
, sizeof(filename
), "%s/%s",
2442 smb_dir
, de
->d_name
);
2450 /* automatic user mode samba server configuration */
2451 void net_slirp_smb(const char *exported_dir
)
2453 char smb_conf
[1024];
2454 char smb_cmdline
[1024];
2457 if (!slirp_inited
) {
2462 /* XXX: better tmp dir construction */
2463 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
2464 if (mkdir(smb_dir
, 0700) < 0) {
2465 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
2468 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
2470 f
= fopen(smb_conf
, "w");
2472 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
2479 "socket address=127.0.0.1\n"
2480 "pid directory=%s\n"
2481 "lock directory=%s\n"
2482 "log file=%s/log.smbd\n"
2483 "smb passwd file=%s/smbpasswd\n"
2484 "security = share\n"
2499 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "/usr/sbin/smbd -s %s",
2502 slirp_add_exec(0, smb_cmdline
, 4, 139);
2505 #endif /* !defined(_WIN32) */
2507 #endif /* CONFIG_SLIRP */
2509 #if !defined(_WIN32)
2511 typedef struct TAPState
{
2512 VLANClientState
*vc
;
2516 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
2518 TAPState
*s
= opaque
;
2521 ret
= write(s
->fd
, buf
, size
);
2522 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
2529 static void tap_send(void *opaque
)
2531 TAPState
*s
= opaque
;
2535 size
= read(s
->fd
, buf
, sizeof(buf
));
2537 qemu_send_packet(s
->vc
, buf
, size
);
2543 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
2547 s
= qemu_mallocz(sizeof(TAPState
));
2551 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
2552 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
2553 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
2558 static int tap_open(char *ifname
, int ifname_size
)
2564 fd
= open("/dev/tap", O_RDWR
);
2566 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
2571 dev
= devname(s
.st_rdev
, S_IFCHR
);
2572 pstrcpy(ifname
, ifname_size
, dev
);
2574 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2577 #elif defined(__sun__)
2578 static int tap_open(char *ifname
, int ifname_size
)
2580 fprintf(stderr
, "warning: tap_open not yet implemented\n");
2584 static int tap_open(char *ifname
, int ifname_size
)
2589 fd
= open("/dev/net/tun", O_RDWR
);
2591 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
2594 memset(&ifr
, 0, sizeof(ifr
));
2595 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
2596 if (ifname
[0] != '\0')
2597 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
2599 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
2600 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
2602 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
2606 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
2607 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2612 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
2613 const char *setup_script
)
2616 int pid
, status
, fd
;
2621 if (ifname1
!= NULL
)
2622 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
2625 fd
= tap_open(ifname
, sizeof(ifname
));
2631 if (setup_script
[0] != '\0') {
2632 /* try to launch network init script */
2637 *parg
++ = (char *)setup_script
;
2640 execv(setup_script
, args
);
2643 while (waitpid(pid
, &status
, 0) != pid
);
2644 if (!WIFEXITED(status
) ||
2645 WEXITSTATUS(status
) != 0) {
2646 fprintf(stderr
, "%s: could not launch network script\n",
2652 s
= net_tap_fd_init(vlan
, fd
);
2655 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2656 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
2660 #endif /* !_WIN32 */
2662 /* network connection */
2663 typedef struct NetSocketState
{
2664 VLANClientState
*vc
;
2666 int state
; /* 0 = getting length, 1 = getting data */
2670 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
2673 typedef struct NetSocketListenState
{
2676 } NetSocketListenState
;
2678 /* XXX: we consider we can send the whole packet without blocking */
2679 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
2681 NetSocketState
*s
= opaque
;
2685 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
2686 send_all(s
->fd
, buf
, size
);
2689 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
2691 NetSocketState
*s
= opaque
;
2692 sendto(s
->fd
, buf
, size
, 0,
2693 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
2696 static void net_socket_send(void *opaque
)
2698 NetSocketState
*s
= opaque
;
2703 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
2705 err
= socket_error();
2706 if (err
!= EWOULDBLOCK
)
2708 } else if (size
== 0) {
2709 /* end of connection */
2711 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2717 /* reassemble a packet from the network */
2723 memcpy(s
->buf
+ s
->index
, buf
, l
);
2727 if (s
->index
== 4) {
2729 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
2735 l
= s
->packet_len
- s
->index
;
2738 memcpy(s
->buf
+ s
->index
, buf
, l
);
2742 if (s
->index
>= s
->packet_len
) {
2743 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
2752 static void net_socket_send_dgram(void *opaque
)
2754 NetSocketState
*s
= opaque
;
2757 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2761 /* end of connection */
2762 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2765 qemu_send_packet(s
->vc
, s
->buf
, size
);
2768 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
2773 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
2774 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
2775 inet_ntoa(mcastaddr
->sin_addr
),
2776 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
2780 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2782 perror("socket(PF_INET, SOCK_DGRAM)");
2787 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
2788 (const char *)&val
, sizeof(val
));
2790 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
2794 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
2800 /* Add host to multicast group */
2801 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
2802 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
2804 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
2805 (const char *)&imr
, sizeof(struct ip_mreq
));
2807 perror("setsockopt(IP_ADD_MEMBERSHIP)");
2811 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
2813 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
2814 (const char *)&val
, sizeof(val
));
2816 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
2820 socket_set_nonblock(fd
);
2823 if (fd
>=0) close(fd
);
2827 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
2830 struct sockaddr_in saddr
;
2832 socklen_t saddr_len
;
2835 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
2836 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
2837 * by ONLY ONE process: we must "clone" this dgram socket --jjo
2841 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
2843 if (saddr
.sin_addr
.s_addr
==0) {
2844 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
2848 /* clone dgram socket */
2849 newfd
= net_socket_mcast_create(&saddr
);
2851 /* error already reported by net_socket_mcast_create() */
2855 /* clone newfd to fd, close newfd */
2860 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
2861 fd
, strerror(errno
));
2866 s
= qemu_mallocz(sizeof(NetSocketState
));
2871 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
2872 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
2874 /* mcast: save bound address as dst */
2875 if (is_connected
) s
->dgram_dst
=saddr
;
2877 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2878 "socket: fd=%d (%s mcast=%s:%d)",
2879 fd
, is_connected
? "cloned" : "",
2880 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2884 static void net_socket_connect(void *opaque
)
2886 NetSocketState
*s
= opaque
;
2887 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
2890 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
2894 s
= qemu_mallocz(sizeof(NetSocketState
));
2898 s
->vc
= qemu_new_vlan_client(vlan
,
2899 net_socket_receive
, NULL
, s
);
2900 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2901 "socket: fd=%d", fd
);
2903 net_socket_connect(s
);
2905 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
2910 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
2913 int so_type
=-1, optlen
=sizeof(so_type
);
2915 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
2916 fprintf(stderr
, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd
);
2921 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
2923 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
2925 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
2926 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
2927 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
2932 static void net_socket_accept(void *opaque
)
2934 NetSocketListenState
*s
= opaque
;
2936 struct sockaddr_in saddr
;
2941 len
= sizeof(saddr
);
2942 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
2943 if (fd
< 0 && errno
!= EINTR
) {
2945 } else if (fd
>= 0) {
2949 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
2953 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
2954 "socket: connection from %s:%d",
2955 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2959 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
2961 NetSocketListenState
*s
;
2963 struct sockaddr_in saddr
;
2965 if (parse_host_port(&saddr
, host_str
) < 0)
2968 s
= qemu_mallocz(sizeof(NetSocketListenState
));
2972 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2977 socket_set_nonblock(fd
);
2979 /* allow fast reuse */
2981 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2983 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2988 ret
= listen(fd
, 0);
2995 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
2999 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
3002 int fd
, connected
, ret
, err
;
3003 struct sockaddr_in saddr
;
3005 if (parse_host_port(&saddr
, host_str
) < 0)
3008 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3013 socket_set_nonblock(fd
);
3017 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3019 err
= socket_error();
3020 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3021 } else if (err
== EINPROGRESS
) {
3033 s
= net_socket_fd_init(vlan
, fd
, connected
);
3036 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3037 "socket: connect to %s:%d",
3038 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3042 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
3046 struct sockaddr_in saddr
;
3048 if (parse_host_port(&saddr
, host_str
) < 0)
3052 fd
= net_socket_mcast_create(&saddr
);
3056 s
= net_socket_fd_init(vlan
, fd
, 0);
3060 s
->dgram_dst
= saddr
;
3062 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3063 "socket: mcast=%s:%d",
3064 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3069 static int get_param_value(char *buf
, int buf_size
,
3070 const char *tag
, const char *str
)
3079 while (*p
!= '\0' && *p
!= '=') {
3080 if ((q
- option
) < sizeof(option
) - 1)
3088 if (!strcmp(tag
, option
)) {
3090 while (*p
!= '\0' && *p
!= ',') {
3091 if ((q
- buf
) < buf_size
- 1)
3098 while (*p
!= '\0' && *p
!= ',') {
3109 int net_client_init(const char *str
)
3120 while (*p
!= '\0' && *p
!= ',') {
3121 if ((q
- device
) < sizeof(device
) - 1)
3129 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
3130 vlan_id
= strtol(buf
, NULL
, 0);
3132 vlan
= qemu_find_vlan(vlan_id
);
3134 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
3137 if (!strcmp(device
, "nic")) {
3141 if (nb_nics
>= MAX_NICS
) {
3142 fprintf(stderr
, "Too Many NICs\n");
3145 nd
= &nd_table
[nb_nics
];
3146 macaddr
= nd
->macaddr
;
3152 macaddr
[5] = 0x56 + nb_nics
;
3154 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
3155 if (parse_macaddr(macaddr
, buf
) < 0) {
3156 fprintf(stderr
, "invalid syntax for ethernet address\n");
3160 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
3161 nd
->model
= strdup(buf
);
3167 if (!strcmp(device
, "none")) {
3168 /* does nothing. It is needed to signal that no network cards
3173 if (!strcmp(device
, "user")) {
3174 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
3175 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
3177 ret
= net_slirp_init(vlan
);
3181 if (!strcmp(device
, "tap")) {
3183 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
3184 fprintf(stderr
, "tap: no interface name\n");
3187 ret
= tap_win32_init(vlan
, ifname
);
3190 if (!strcmp(device
, "tap")) {
3192 char setup_script
[1024];
3194 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3195 fd
= strtol(buf
, NULL
, 0);
3197 if (net_tap_fd_init(vlan
, fd
))
3200 get_param_value(ifname
, sizeof(ifname
), "ifname", p
);
3201 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
3202 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
3204 ret
= net_tap_init(vlan
, ifname
, setup_script
);
3208 if (!strcmp(device
, "socket")) {
3209 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3211 fd
= strtol(buf
, NULL
, 0);
3213 if (net_socket_fd_init(vlan
, fd
, 1))
3215 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
3216 ret
= net_socket_listen_init(vlan
, buf
);
3217 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
3218 ret
= net_socket_connect_init(vlan
, buf
);
3219 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
3220 ret
= net_socket_mcast_init(vlan
, buf
);
3222 fprintf(stderr
, "Unknown socket options: %s\n", p
);
3227 fprintf(stderr
, "Unknown network device: %s\n", device
);
3231 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
3237 void do_info_network(void)
3240 VLANClientState
*vc
;
3242 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3243 term_printf("VLAN %d devices:\n", vlan
->id
);
3244 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3245 term_printf(" %s\n", vc
->info_str
);
3249 /***********************************************************/
3252 static int usb_device_add(const char *devname
)
3260 for(i
= 0;i
< MAX_VM_USB_PORTS
; i
++) {
3261 if (!vm_usb_ports
[i
]->dev
)
3264 if (i
== MAX_VM_USB_PORTS
)
3267 if (strstart(devname
, "host:", &p
)) {
3268 dev
= usb_host_device_open(p
);
3271 } else if (!strcmp(devname
, "mouse")) {
3272 dev
= usb_mouse_init();
3275 } else if (!strcmp(devname
, "tablet")) {
3276 dev
= usb_tablet_init();
3282 usb_attach(vm_usb_ports
[i
], dev
);
3286 static int usb_device_del(const char *devname
)
3289 int bus_num
, addr
, i
;
3295 p
= strchr(devname
, '.');
3298 bus_num
= strtoul(devname
, NULL
, 0);
3299 addr
= strtoul(p
+ 1, NULL
, 0);
3302 for(i
= 0;i
< MAX_VM_USB_PORTS
; i
++) {
3303 dev
= vm_usb_ports
[i
]->dev
;
3304 if (dev
&& dev
->addr
== addr
)
3307 if (i
== MAX_VM_USB_PORTS
)
3309 usb_attach(vm_usb_ports
[i
], NULL
);
3313 void do_usb_add(const char *devname
)
3316 ret
= usb_device_add(devname
);
3318 term_printf("Could not add USB device '%s'\n", devname
);
3321 void do_usb_del(const char *devname
)
3324 ret
= usb_device_del(devname
);
3326 term_printf("Could not remove USB device '%s'\n", devname
);
3333 const char *speed_str
;
3336 term_printf("USB support not enabled\n");
3340 for(i
= 0; i
< MAX_VM_USB_PORTS
; i
++) {
3341 dev
= vm_usb_ports
[i
]->dev
;
3343 term_printf("Hub port %d:\n", i
);
3344 switch(dev
->speed
) {
3348 case USB_SPEED_FULL
:
3351 case USB_SPEED_HIGH
:
3358 term_printf(" Device %d.%d, speed %s Mb/s\n",
3359 0, dev
->addr
, speed_str
);
3364 /***********************************************************/
3367 static char *pid_filename
;
3369 /* Remove PID file. Called on normal exit */
3371 static void remove_pidfile(void)
3373 unlink (pid_filename
);
3376 static void create_pidfile(const char *filename
)
3378 struct stat pidstat
;
3381 /* Try to write our PID to the named file */
3382 if (stat(filename
, &pidstat
) < 0) {
3383 if (errno
== ENOENT
) {
3384 if ((f
= fopen (filename
, "w")) == NULL
) {
3385 perror("Opening pidfile");
3388 fprintf(f
, "%d\n", getpid());
3390 pid_filename
= qemu_strdup(filename
);
3391 if (!pid_filename
) {
3392 fprintf(stderr
, "Could not save PID filename");
3395 atexit(remove_pidfile
);
3398 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
3404 /***********************************************************/
3407 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
3411 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
3415 static void dumb_refresh(DisplayState
*ds
)
3420 void dumb_display_init(DisplayState
*ds
)
3425 ds
->dpy_update
= dumb_update
;
3426 ds
->dpy_resize
= dumb_resize
;
3427 ds
->dpy_refresh
= dumb_refresh
;
3430 #if !defined(CONFIG_SOFTMMU)
3431 /***********************************************************/
3432 /* cpu signal handler */
3433 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
3436 if (cpu_signal_handler(host_signum
, info
, puc
))
3438 if (stdio_nb_clients
> 0)
3444 /***********************************************************/
3447 #define MAX_IO_HANDLERS 64
3449 typedef struct IOHandlerRecord
{
3451 IOCanRWHandler
*fd_read_poll
;
3453 IOHandler
*fd_write
;
3455 /* temporary data */
3457 struct IOHandlerRecord
*next
;
3460 static IOHandlerRecord
*first_io_handler
;
3462 /* XXX: fd_read_poll should be suppressed, but an API change is
3463 necessary in the character devices to suppress fd_can_read(). */
3464 int qemu_set_fd_handler2(int fd
,
3465 IOCanRWHandler
*fd_read_poll
,
3467 IOHandler
*fd_write
,
3470 IOHandlerRecord
**pioh
, *ioh
;
3472 if (!fd_read
&& !fd_write
) {
3473 pioh
= &first_io_handler
;
3478 if (ioh
->fd
== fd
) {
3486 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3490 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
3493 ioh
->next
= first_io_handler
;
3494 first_io_handler
= ioh
;
3497 ioh
->fd_read_poll
= fd_read_poll
;
3498 ioh
->fd_read
= fd_read
;
3499 ioh
->fd_write
= fd_write
;
3500 ioh
->opaque
= opaque
;
3505 int qemu_set_fd_handler(int fd
,
3507 IOHandler
*fd_write
,
3510 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
3513 /***********************************************************/
3514 /* Polling handling */
3516 typedef struct PollingEntry
{
3519 struct PollingEntry
*next
;
3522 static PollingEntry
*first_polling_entry
;
3524 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
3526 PollingEntry
**ppe
, *pe
;
3527 pe
= qemu_mallocz(sizeof(PollingEntry
));
3531 pe
->opaque
= opaque
;
3532 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
3537 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3539 PollingEntry
**ppe
, *pe
;
3540 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3542 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3550 /***********************************************************/
3551 /* savevm/loadvm support */
3553 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
3555 fwrite(buf
, 1, size
, f
);
3558 void qemu_put_byte(QEMUFile
*f
, int v
)
3563 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
3565 qemu_put_byte(f
, v
>> 8);
3566 qemu_put_byte(f
, v
);
3569 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
3571 qemu_put_byte(f
, v
>> 24);
3572 qemu_put_byte(f
, v
>> 16);
3573 qemu_put_byte(f
, v
>> 8);
3574 qemu_put_byte(f
, v
);
3577 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
3579 qemu_put_be32(f
, v
>> 32);
3580 qemu_put_be32(f
, v
);
3583 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
3585 return fread(buf
, 1, size
, f
);
3588 int qemu_get_byte(QEMUFile
*f
)
3598 unsigned int qemu_get_be16(QEMUFile
*f
)
3601 v
= qemu_get_byte(f
) << 8;
3602 v
|= qemu_get_byte(f
);
3606 unsigned int qemu_get_be32(QEMUFile
*f
)
3609 v
= qemu_get_byte(f
) << 24;
3610 v
|= qemu_get_byte(f
) << 16;
3611 v
|= qemu_get_byte(f
) << 8;
3612 v
|= qemu_get_byte(f
);
3616 uint64_t qemu_get_be64(QEMUFile
*f
)
3619 v
= (uint64_t)qemu_get_be32(f
) << 32;
3620 v
|= qemu_get_be32(f
);
3624 int64_t qemu_ftell(QEMUFile
*f
)
3629 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
3631 if (fseek(f
, pos
, whence
) < 0)
3636 typedef struct SaveStateEntry
{
3640 SaveStateHandler
*save_state
;
3641 LoadStateHandler
*load_state
;
3643 struct SaveStateEntry
*next
;
3646 static SaveStateEntry
*first_se
;
3648 int register_savevm(const char *idstr
,
3651 SaveStateHandler
*save_state
,
3652 LoadStateHandler
*load_state
,
3655 SaveStateEntry
*se
, **pse
;
3657 se
= qemu_malloc(sizeof(SaveStateEntry
));
3660 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
3661 se
->instance_id
= instance_id
;
3662 se
->version_id
= version_id
;
3663 se
->save_state
= save_state
;
3664 se
->load_state
= load_state
;
3665 se
->opaque
= opaque
;
3668 /* add at the end of list */
3670 while (*pse
!= NULL
)
3671 pse
= &(*pse
)->next
;
3676 #define QEMU_VM_FILE_MAGIC 0x5145564d
3677 #define QEMU_VM_FILE_VERSION 0x00000001
3679 int qemu_savevm(const char *filename
)
3683 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
3685 saved_vm_running
= vm_running
;
3688 f
= fopen(filename
, "wb");
3694 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
3695 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
3697 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3699 len
= strlen(se
->idstr
);
3700 qemu_put_byte(f
, len
);
3701 qemu_put_buffer(f
, se
->idstr
, len
);
3703 qemu_put_be32(f
, se
->instance_id
);
3704 qemu_put_be32(f
, se
->version_id
);
3706 /* record size: filled later */
3708 qemu_put_be32(f
, 0);
3710 se
->save_state(f
, se
->opaque
);
3712 /* fill record size */
3714 len
= ftell(f
) - len_pos
- 4;
3715 fseek(f
, len_pos
, SEEK_SET
);
3716 qemu_put_be32(f
, len
);
3717 fseek(f
, cur_pos
, SEEK_SET
);
3723 if (saved_vm_running
)
3728 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
3732 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3733 if (!strcmp(se
->idstr
, idstr
) &&
3734 instance_id
== se
->instance_id
)
3740 int qemu_loadvm(const char *filename
)
3744 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
3745 int saved_vm_running
;
3749 saved_vm_running
= vm_running
;
3752 f
= fopen(filename
, "rb");
3758 v
= qemu_get_be32(f
);
3759 if (v
!= QEMU_VM_FILE_MAGIC
)
3761 v
= qemu_get_be32(f
);
3762 if (v
!= QEMU_VM_FILE_VERSION
) {
3769 len
= qemu_get_byte(f
);
3772 qemu_get_buffer(f
, idstr
, len
);
3774 instance_id
= qemu_get_be32(f
);
3775 version_id
= qemu_get_be32(f
);
3776 record_len
= qemu_get_be32(f
);
3778 printf("idstr=%s instance=0x%x version=%d len=%d\n",
3779 idstr
, instance_id
, version_id
, record_len
);
3782 se
= find_se(idstr
, instance_id
);
3784 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
3785 instance_id
, idstr
);
3787 ret
= se
->load_state(f
, se
->opaque
, version_id
);
3789 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
3790 instance_id
, idstr
);
3793 /* always seek to exact end of record */
3794 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
3799 if (saved_vm_running
)
3804 /***********************************************************/
3805 /* cpu save/restore */
3807 #if defined(TARGET_I386)
3809 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
3811 qemu_put_be32(f
, dt
->selector
);
3812 qemu_put_betl(f
, dt
->base
);
3813 qemu_put_be32(f
, dt
->limit
);
3814 qemu_put_be32(f
, dt
->flags
);
3817 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
3819 dt
->selector
= qemu_get_be32(f
);
3820 dt
->base
= qemu_get_betl(f
);
3821 dt
->limit
= qemu_get_be32(f
);
3822 dt
->flags
= qemu_get_be32(f
);
3825 void cpu_save(QEMUFile
*f
, void *opaque
)
3827 CPUState
*env
= opaque
;
3828 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
3832 for(i
= 0; i
< CPU_NB_REGS
; i
++)
3833 qemu_put_betls(f
, &env
->regs
[i
]);
3834 qemu_put_betls(f
, &env
->eip
);
3835 qemu_put_betls(f
, &env
->eflags
);
3836 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
3837 qemu_put_be32s(f
, &hflags
);
3841 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
3843 for(i
= 0; i
< 8; i
++) {
3844 fptag
|= ((!env
->fptags
[i
]) << i
);
3847 qemu_put_be16s(f
, &fpuc
);
3848 qemu_put_be16s(f
, &fpus
);
3849 qemu_put_be16s(f
, &fptag
);
3851 #ifdef USE_X86LDOUBLE
3856 qemu_put_be16s(f
, &fpregs_format
);
3858 for(i
= 0; i
< 8; i
++) {
3859 #ifdef USE_X86LDOUBLE
3863 /* we save the real CPU data (in case of MMX usage only 'mant'
3864 contains the MMX register */
3865 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
3866 qemu_put_be64(f
, mant
);
3867 qemu_put_be16(f
, exp
);
3870 /* if we use doubles for float emulation, we save the doubles to
3871 avoid losing information in case of MMX usage. It can give
3872 problems if the image is restored on a CPU where long
3873 doubles are used instead. */
3874 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
3878 for(i
= 0; i
< 6; i
++)
3879 cpu_put_seg(f
, &env
->segs
[i
]);
3880 cpu_put_seg(f
, &env
->ldt
);
3881 cpu_put_seg(f
, &env
->tr
);
3882 cpu_put_seg(f
, &env
->gdt
);
3883 cpu_put_seg(f
, &env
->idt
);
3885 qemu_put_be32s(f
, &env
->sysenter_cs
);
3886 qemu_put_be32s(f
, &env
->sysenter_esp
);
3887 qemu_put_be32s(f
, &env
->sysenter_eip
);
3889 qemu_put_betls(f
, &env
->cr
[0]);
3890 qemu_put_betls(f
, &env
->cr
[2]);
3891 qemu_put_betls(f
, &env
->cr
[3]);
3892 qemu_put_betls(f
, &env
->cr
[4]);
3894 for(i
= 0; i
< 8; i
++)
3895 qemu_put_betls(f
, &env
->dr
[i
]);
3898 qemu_put_be32s(f
, &env
->a20_mask
);
3901 qemu_put_be32s(f
, &env
->mxcsr
);
3902 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
3903 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
3904 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
3907 #ifdef TARGET_X86_64
3908 qemu_put_be64s(f
, &env
->efer
);
3909 qemu_put_be64s(f
, &env
->star
);
3910 qemu_put_be64s(f
, &env
->lstar
);
3911 qemu_put_be64s(f
, &env
->cstar
);
3912 qemu_put_be64s(f
, &env
->fmask
);
3913 qemu_put_be64s(f
, &env
->kernelgsbase
);
3917 #ifdef USE_X86LDOUBLE
3918 /* XXX: add that in a FPU generic layer */
3919 union x86_longdouble
{
3924 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
3925 #define EXPBIAS1 1023
3926 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
3927 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
3929 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
3933 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
3934 /* exponent + sign */
3935 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
3936 e
|= SIGND1(temp
) >> 16;
3941 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
3943 CPUState
*env
= opaque
;
3946 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
3948 if (version_id
!= 3)
3950 for(i
= 0; i
< CPU_NB_REGS
; i
++)
3951 qemu_get_betls(f
, &env
->regs
[i
]);
3952 qemu_get_betls(f
, &env
->eip
);
3953 qemu_get_betls(f
, &env
->eflags
);
3954 qemu_get_be32s(f
, &hflags
);
3956 qemu_get_be16s(f
, &fpuc
);
3957 qemu_get_be16s(f
, &fpus
);
3958 qemu_get_be16s(f
, &fptag
);
3959 qemu_get_be16s(f
, &fpregs_format
);
3961 /* NOTE: we cannot always restore the FPU state if the image come
3962 from a host with a different 'USE_X86LDOUBLE' define. We guess
3963 if we are in an MMX state to restore correctly in that case. */
3964 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
3965 for(i
= 0; i
< 8; i
++) {
3969 switch(fpregs_format
) {
3971 mant
= qemu_get_be64(f
);
3972 exp
= qemu_get_be16(f
);
3973 #ifdef USE_X86LDOUBLE
3974 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
3976 /* difficult case */
3978 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
3980 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
3984 mant
= qemu_get_be64(f
);
3985 #ifdef USE_X86LDOUBLE
3987 union x86_longdouble
*p
;
3988 /* difficult case */
3989 p
= (void *)&env
->fpregs
[i
];
3994 fp64_to_fp80(p
, mant
);
3998 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
4007 /* XXX: restore FPU round state */
4008 env
->fpstt
= (fpus
>> 11) & 7;
4009 env
->fpus
= fpus
& ~0x3800;
4011 for(i
= 0; i
< 8; i
++) {
4012 env
->fptags
[i
] = (fptag
>> i
) & 1;
4015 for(i
= 0; i
< 6; i
++)
4016 cpu_get_seg(f
, &env
->segs
[i
]);
4017 cpu_get_seg(f
, &env
->ldt
);
4018 cpu_get_seg(f
, &env
->tr
);
4019 cpu_get_seg(f
, &env
->gdt
);
4020 cpu_get_seg(f
, &env
->idt
);
4022 qemu_get_be32s(f
, &env
->sysenter_cs
);
4023 qemu_get_be32s(f
, &env
->sysenter_esp
);
4024 qemu_get_be32s(f
, &env
->sysenter_eip
);
4026 qemu_get_betls(f
, &env
->cr
[0]);
4027 qemu_get_betls(f
, &env
->cr
[2]);
4028 qemu_get_betls(f
, &env
->cr
[3]);
4029 qemu_get_betls(f
, &env
->cr
[4]);
4031 for(i
= 0; i
< 8; i
++)
4032 qemu_get_betls(f
, &env
->dr
[i
]);
4035 qemu_get_be32s(f
, &env
->a20_mask
);
4037 qemu_get_be32s(f
, &env
->mxcsr
);
4038 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
4039 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
4040 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
4043 #ifdef TARGET_X86_64
4044 qemu_get_be64s(f
, &env
->efer
);
4045 qemu_get_be64s(f
, &env
->star
);
4046 qemu_get_be64s(f
, &env
->lstar
);
4047 qemu_get_be64s(f
, &env
->cstar
);
4048 qemu_get_be64s(f
, &env
->fmask
);
4049 qemu_get_be64s(f
, &env
->kernelgsbase
);
4052 /* XXX: compute hflags from scratch, except for CPL and IIF */
4053 env
->hflags
= hflags
;
4058 #elif defined(TARGET_PPC)
4059 void cpu_save(QEMUFile
*f
, void *opaque
)
4063 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4068 #elif defined(TARGET_MIPS)
4069 void cpu_save(QEMUFile
*f
, void *opaque
)
4073 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4078 #elif defined(TARGET_SPARC)
4079 void cpu_save(QEMUFile
*f
, void *opaque
)
4081 CPUState
*env
= opaque
;
4085 for(i
= 0; i
< 8; i
++)
4086 qemu_put_betls(f
, &env
->gregs
[i
]);
4087 for(i
= 0; i
< NWINDOWS
* 16; i
++)
4088 qemu_put_betls(f
, &env
->regbase
[i
]);
4091 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
4097 qemu_put_betl(f
, u
.i
);
4100 qemu_put_betls(f
, &env
->pc
);
4101 qemu_put_betls(f
, &env
->npc
);
4102 qemu_put_betls(f
, &env
->y
);
4104 qemu_put_be32(f
, tmp
);
4105 qemu_put_betls(f
, &env
->fsr
);
4106 qemu_put_betls(f
, &env
->tbr
);
4107 #ifndef TARGET_SPARC64
4108 qemu_put_be32s(f
, &env
->wim
);
4110 for(i
= 0; i
< 16; i
++)
4111 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
4115 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4117 CPUState
*env
= opaque
;
4121 for(i
= 0; i
< 8; i
++)
4122 qemu_get_betls(f
, &env
->gregs
[i
]);
4123 for(i
= 0; i
< NWINDOWS
* 16; i
++)
4124 qemu_get_betls(f
, &env
->regbase
[i
]);
4127 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
4132 u
.i
= qemu_get_betl(f
);
4136 qemu_get_betls(f
, &env
->pc
);
4137 qemu_get_betls(f
, &env
->npc
);
4138 qemu_get_betls(f
, &env
->y
);
4139 tmp
= qemu_get_be32(f
);
4140 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
4141 correctly updated */
4143 qemu_get_betls(f
, &env
->fsr
);
4144 qemu_get_betls(f
, &env
->tbr
);
4145 #ifndef TARGET_SPARC64
4146 qemu_get_be32s(f
, &env
->wim
);
4148 for(i
= 0; i
< 16; i
++)
4149 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
4155 #elif defined(TARGET_ARM)
4157 /* ??? Need to implement these. */
4158 void cpu_save(QEMUFile
*f
, void *opaque
)
4162 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4169 #warning No CPU save/restore functions
4173 /***********************************************************/
4174 /* ram save/restore */
4176 /* we just avoid storing empty pages */
4177 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
4182 for(i
= 1; i
< len
; i
++) {
4186 qemu_put_byte(f
, 1);
4187 qemu_put_byte(f
, v
);
4190 qemu_put_byte(f
, 0);
4191 qemu_put_buffer(f
, buf
, len
);
4194 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
4198 v
= qemu_get_byte(f
);
4201 if (qemu_get_buffer(f
, buf
, len
) != len
)
4205 v
= qemu_get_byte(f
);
4206 memset(buf
, v
, len
);
4214 static void ram_save(QEMUFile
*f
, void *opaque
)
4217 qemu_put_be32(f
, phys_ram_size
);
4218 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
4219 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
4223 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
4227 if (version_id
!= 1)
4229 if (qemu_get_be32(f
) != phys_ram_size
)
4231 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
4232 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
4239 /***********************************************************/
4240 /* machine registration */
4242 QEMUMachine
*first_machine
= NULL
;
4244 int qemu_register_machine(QEMUMachine
*m
)
4247 pm
= &first_machine
;
4255 QEMUMachine
*find_machine(const char *name
)
4259 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4260 if (!strcmp(m
->name
, name
))
4266 /***********************************************************/
4267 /* main execution loop */
4269 void gui_update(void *opaque
)
4271 display_state
.dpy_refresh(&display_state
);
4272 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
4275 struct vm_change_state_entry
{
4276 VMChangeStateHandler
*cb
;
4278 LIST_ENTRY (vm_change_state_entry
) entries
;
4281 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
4283 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
4286 VMChangeStateEntry
*e
;
4288 e
= qemu_mallocz(sizeof (*e
));
4294 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
4298 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
4300 LIST_REMOVE (e
, entries
);
4304 static void vm_state_notify(int running
)
4306 VMChangeStateEntry
*e
;
4308 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
4309 e
->cb(e
->opaque
, running
);
4313 /* XXX: support several handlers */
4314 static VMStopHandler
*vm_stop_cb
;
4315 static void *vm_stop_opaque
;
4317 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4320 vm_stop_opaque
= opaque
;
4324 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4338 void vm_stop(int reason
)
4341 cpu_disable_ticks();
4345 vm_stop_cb(vm_stop_opaque
, reason
);
4352 /* reset/shutdown handler */
4354 typedef struct QEMUResetEntry
{
4355 QEMUResetHandler
*func
;
4357 struct QEMUResetEntry
*next
;
4360 static QEMUResetEntry
*first_reset_entry
;
4361 static int reset_requested
;
4362 static int shutdown_requested
;
4363 static int powerdown_requested
;
4365 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
4367 QEMUResetEntry
**pre
, *re
;
4369 pre
= &first_reset_entry
;
4370 while (*pre
!= NULL
)
4371 pre
= &(*pre
)->next
;
4372 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
4374 re
->opaque
= opaque
;
4379 void qemu_system_reset(void)
4383 /* reset all devices */
4384 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
4385 re
->func(re
->opaque
);
4389 void qemu_system_reset_request(void)
4391 reset_requested
= 1;
4393 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4396 void qemu_system_shutdown_request(void)
4398 shutdown_requested
= 1;
4400 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4403 void qemu_system_powerdown_request(void)
4405 powerdown_requested
= 1;
4407 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4410 void main_loop_wait(int timeout
)
4412 IOHandlerRecord
*ioh
, *ioh_next
;
4413 fd_set rfds
, wfds
, xfds
;
4419 /* XXX: need to suppress polling by better using win32 events */
4421 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4422 ret
|= pe
->func(pe
->opaque
);
4425 if (ret
== 0 && timeout
> 0) {
4429 hEvents
[0] = host_alarm
;
4430 ret
= WaitForMultipleObjects(1, hEvents
, FALSE
, timeout
);
4432 case WAIT_OBJECT_0
+ 0:
4437 err
= GetLastError();
4438 fprintf(stderr
, "Wait error %d %d\n", ret
, err
);
4443 /* poll any events */
4444 /* XXX: separate device handlers from system ones */
4449 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4451 (!ioh
->fd_read_poll
||
4452 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4453 FD_SET(ioh
->fd
, &rfds
);
4457 if (ioh
->fd_write
) {
4458 FD_SET(ioh
->fd
, &wfds
);
4468 tv
.tv_usec
= timeout
* 1000;
4470 #if defined(CONFIG_SLIRP)
4472 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4475 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4477 /* XXX: better handling of removal */
4478 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
4479 ioh_next
= ioh
->next
;
4480 if (FD_ISSET(ioh
->fd
, &rfds
)) {
4481 ioh
->fd_read(ioh
->opaque
);
4483 if (FD_ISSET(ioh
->fd
, &wfds
)) {
4484 ioh
->fd_write(ioh
->opaque
);
4488 #if defined(CONFIG_SLIRP)
4495 slirp_select_poll(&rfds
, &wfds
, &xfds
);
4503 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4504 qemu_get_clock(vm_clock
));
4505 /* run dma transfers, if any */
4509 /* real time timers */
4510 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4511 qemu_get_clock(rt_clock
));
4514 static CPUState
*cur_cpu
;
4519 #ifdef CONFIG_PROFILER
4524 cur_cpu
= first_cpu
;
4531 env
= env
->next_cpu
;
4534 #ifdef CONFIG_PROFILER
4535 ti
= profile_getclock();
4537 ret
= cpu_exec(env
);
4538 #ifdef CONFIG_PROFILER
4539 qemu_time
+= profile_getclock() - ti
;
4541 if (ret
!= EXCP_HALTED
)
4543 /* all CPUs are halted ? */
4544 if (env
== cur_cpu
) {
4551 if (shutdown_requested
) {
4552 ret
= EXCP_INTERRUPT
;
4555 if (reset_requested
) {
4556 reset_requested
= 0;
4557 qemu_system_reset();
4558 ret
= EXCP_INTERRUPT
;
4560 if (powerdown_requested
) {
4561 powerdown_requested
= 0;
4562 qemu_system_powerdown();
4563 ret
= EXCP_INTERRUPT
;
4565 if (ret
== EXCP_DEBUG
) {
4566 vm_stop(EXCP_DEBUG
);
4568 /* if hlt instruction, we wait until the next IRQ */
4569 /* XXX: use timeout computed from timers */
4570 if (ret
== EXCP_HLT
)
4577 #ifdef CONFIG_PROFILER
4578 ti
= profile_getclock();
4580 main_loop_wait(timeout
);
4581 #ifdef CONFIG_PROFILER
4582 dev_time
+= profile_getclock() - ti
;
4585 cpu_disable_ticks();
4591 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2005 Fabrice Bellard\n"
4592 "usage: %s [options] [disk_image]\n"
4594 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4596 "Standard options:\n"
4597 "-M machine select emulated machine (-M ? for list)\n"
4598 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
4599 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
4600 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
4601 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
4602 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
4603 "-snapshot write to temporary files instead of disk image files\n"
4604 "-m megs set virtual RAM size to megs MB [default=%d]\n"
4605 "-smp n set the number of CPUs to 'n' [default=1]\n"
4606 "-nographic disable graphical output and redirect serial I/Os to console\n"
4608 "-k language use keyboard layout (for example \"fr\" for French)\n"
4611 "-audio-help print list of audio drivers and their options\n"
4612 "-soundhw c1,... enable audio support\n"
4613 " and only specified sound cards (comma separated list)\n"
4614 " use -soundhw ? to get the list of supported cards\n"
4615 " use -soundhw all to enable all of them\n"
4617 "-localtime set the real time clock to local time [default=utc]\n"
4618 "-full-screen start in full screen\n"
4620 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4622 "-usb enable the USB driver (will be the default soon)\n"
4623 "-usbdevice name add the host or guest USB device 'name'\n"
4624 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4625 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4628 "Network options:\n"
4629 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
4630 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4632 "-net user[,vlan=n][,hostname=host]\n"
4633 " connect the user mode network stack to VLAN 'n' and send\n"
4634 " hostname 'host' to DHCP clients\n"
4637 "-net tap[,vlan=n],ifname=name\n"
4638 " connect the host TAP network interface to VLAN 'n'\n"
4640 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
4641 " connect the host TAP network interface to VLAN 'n' and use\n"
4642 " the network script 'file' (default=%s);\n"
4643 " use 'fd=h' to connect to an already opened TAP interface\n"
4645 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4646 " connect the vlan 'n' to another VLAN using a socket connection\n"
4647 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
4648 " connect the vlan 'n' to multicast maddr and port\n"
4649 "-net none use it alone to have zero network devices; if no -net option\n"
4650 " is provided, the default is '-net nic -net user'\n"
4653 "-tftp prefix allow tftp access to files starting with prefix [-net user]\n"
4655 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4657 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4658 " redirect TCP or UDP connections from host to guest [-net user]\n"
4661 "Linux boot specific:\n"
4662 "-kernel bzImage use 'bzImage' as kernel image\n"
4663 "-append cmdline use 'cmdline' as kernel command line\n"
4664 "-initrd file use 'file' as initial ram disk\n"
4666 "Debug/Expert options:\n"
4667 "-monitor dev redirect the monitor to char device 'dev'\n"
4668 "-serial dev redirect the serial port to char device 'dev'\n"
4669 "-parallel dev redirect the parallel port to char device 'dev'\n"
4670 "-pidfile file Write PID to 'file'\n"
4671 "-S freeze CPU at startup (use 'c' to start execution)\n"
4672 "-s wait gdb connection to port %d\n"
4673 "-p port change gdb connection port\n"
4674 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4675 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
4676 " translation (t=none or lba) (usually qemu can guess them)\n"
4677 "-L path set the directory for the BIOS and VGA BIOS\n"
4679 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4680 "-no-kqemu disable KQEMU kernel module usage\n"
4682 #ifdef USE_CODE_COPY
4683 "-no-code-copy disable code copy acceleration\n"
4686 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
4687 " (default is CL-GD5446 PCI VGA)\n"
4688 "-no-acpi disable ACPI\n"
4690 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
4691 "-vnc display start a VNC server on display\n"
4693 "During emulation, the following keys are useful:\n"
4694 "ctrl-alt-f toggle full screen\n"
4695 "ctrl-alt-n switch to virtual console 'n'\n"
4696 "ctrl-alt toggle mouse and keyboard grab\n"
4698 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4700 #ifdef CONFIG_SOFTMMU
4707 DEFAULT_NETWORK_SCRIPT
,
4709 DEFAULT_GDBSTUB_PORT
,
4711 #ifndef CONFIG_SOFTMMU
4713 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
4714 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
4720 #define HAS_ARG 0x0001
4734 QEMU_OPTION_snapshot
,
4736 QEMU_OPTION_nographic
,
4738 QEMU_OPTION_audio_help
,
4739 QEMU_OPTION_soundhw
,
4757 QEMU_OPTION_no_code_copy
,
4759 QEMU_OPTION_localtime
,
4760 QEMU_OPTION_cirrusvga
,
4762 QEMU_OPTION_std_vga
,
4763 QEMU_OPTION_monitor
,
4765 QEMU_OPTION_parallel
,
4767 QEMU_OPTION_full_screen
,
4768 QEMU_OPTION_pidfile
,
4769 QEMU_OPTION_no_kqemu
,
4770 QEMU_OPTION_kernel_kqemu
,
4771 QEMU_OPTION_win2k_hack
,
4773 QEMU_OPTION_usbdevice
,
4776 QEMU_OPTION_no_acpi
,
4779 typedef struct QEMUOption
{
4785 const QEMUOption qemu_options
[] = {
4786 { "h", 0, QEMU_OPTION_h
},
4788 { "M", HAS_ARG
, QEMU_OPTION_M
},
4789 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4790 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4791 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4792 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4793 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4794 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4795 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4796 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4797 { "snapshot", 0, QEMU_OPTION_snapshot
},
4798 { "m", HAS_ARG
, QEMU_OPTION_m
},
4799 { "nographic", 0, QEMU_OPTION_nographic
},
4800 { "k", HAS_ARG
, QEMU_OPTION_k
},
4802 { "audio-help", 0, QEMU_OPTION_audio_help
},
4803 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4806 { "net", HAS_ARG
, QEMU_OPTION_net
},
4808 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4810 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4812 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4815 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4816 { "append", HAS_ARG
, QEMU_OPTION_append
},
4817 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4819 { "S", 0, QEMU_OPTION_S
},
4820 { "s", 0, QEMU_OPTION_s
},
4821 { "p", HAS_ARG
, QEMU_OPTION_p
},
4822 { "d", HAS_ARG
, QEMU_OPTION_d
},
4823 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4824 { "L", HAS_ARG
, QEMU_OPTION_L
},
4825 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
4827 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4828 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4830 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4831 { "g", 1, QEMU_OPTION_g
},
4833 { "localtime", 0, QEMU_OPTION_localtime
},
4834 { "std-vga", 0, QEMU_OPTION_std_vga
},
4835 { "monitor", 1, QEMU_OPTION_monitor
},
4836 { "serial", 1, QEMU_OPTION_serial
},
4837 { "parallel", 1, QEMU_OPTION_parallel
},
4838 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4839 { "full-screen", 0, QEMU_OPTION_full_screen
},
4840 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4841 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4842 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4843 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4844 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4846 /* temporary options */
4847 { "usb", 0, QEMU_OPTION_usb
},
4848 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
4849 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4853 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
4855 /* this stack is only used during signal handling */
4856 #define SIGNAL_STACK_SIZE 32768
4858 static uint8_t *signal_stack
;
4862 /* password input */
4864 static BlockDriverState
*get_bdrv(int index
)
4866 BlockDriverState
*bs
;
4869 bs
= bs_table
[index
];
4870 } else if (index
< 6) {
4871 bs
= fd_table
[index
- 4];
4878 static void read_passwords(void)
4880 BlockDriverState
*bs
;
4884 for(i
= 0; i
< 6; i
++) {
4886 if (bs
&& bdrv_is_encrypted(bs
)) {
4887 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
4888 for(j
= 0; j
< 3; j
++) {
4889 monitor_readline("Password: ",
4890 1, password
, sizeof(password
));
4891 if (bdrv_set_key(bs
, password
) == 0)
4893 term_printf("invalid password\n");
4899 /* XXX: currently we cannot use simultaneously different CPUs */
4900 void register_machines(void)
4902 #if defined(TARGET_I386)
4903 qemu_register_machine(&pc_machine
);
4904 qemu_register_machine(&isapc_machine
);
4905 #elif defined(TARGET_PPC)
4906 qemu_register_machine(&heathrow_machine
);
4907 qemu_register_machine(&core99_machine
);
4908 qemu_register_machine(&prep_machine
);
4909 #elif defined(TARGET_MIPS)
4910 qemu_register_machine(&mips_machine
);
4911 #elif defined(TARGET_SPARC)
4912 #ifdef TARGET_SPARC64
4913 qemu_register_machine(&sun4u_machine
);
4915 qemu_register_machine(&sun4m_machine
);
4917 #elif defined(TARGET_ARM)
4918 qemu_register_machine(&integratorcp926_machine
);
4919 qemu_register_machine(&integratorcp1026_machine
);
4920 qemu_register_machine(&versatilepb_machine
);
4921 qemu_register_machine(&versatileab_machine
);
4922 #elif defined(TARGET_SH4)
4923 qemu_register_machine(&shix_machine
);
4925 #error unsupported CPU
4930 struct soundhw soundhw
[] = {
4937 { .init_isa
= pcspk_audio_init
}
4942 "Creative Sound Blaster 16",
4945 { .init_isa
= SB16_init
}
4952 "Yamaha YMF262 (OPL3)",
4954 "Yamaha YM3812 (OPL2)",
4958 { .init_isa
= Adlib_init
}
4965 "Gravis Ultrasound GF1",
4968 { .init_isa
= GUS_init
}
4974 "ENSONIQ AudioPCI ES1370",
4977 { .init_pci
= es1370_init
}
4980 { NULL
, NULL
, 0, 0, { NULL
} }
4983 static void select_soundhw (const char *optarg
)
4987 if (*optarg
== '?') {
4990 printf ("Valid sound card names (comma separated):\n");
4991 for (c
= soundhw
; c
->name
; ++c
) {
4992 printf ("%-11s %s\n", c
->name
, c
->descr
);
4994 printf ("\n-soundhw all will enable all of the above\n");
4995 exit (*optarg
!= '?');
5003 if (!strcmp (optarg
, "all")) {
5004 for (c
= soundhw
; c
->name
; ++c
) {
5012 e
= strchr (p
, ',');
5013 l
= !e
? strlen (p
) : (size_t) (e
- p
);
5015 for (c
= soundhw
; c
->name
; ++c
) {
5016 if (!strncmp (c
->name
, p
, l
)) {
5025 "Unknown sound card name (too big to show)\n");
5028 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
5033 p
+= l
+ (e
!= NULL
);
5037 goto show_valid_cards
;
5042 #define MAX_NET_CLIENTS 32
5044 int main(int argc
, char **argv
)
5046 #ifdef CONFIG_GDBSTUB
5047 int use_gdbstub
, gdbstub_port
;
5050 int snapshot
, linux_boot
;
5051 const char *initrd_filename
;
5052 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
5053 const char *kernel_filename
, *kernel_cmdline
;
5054 DisplayState
*ds
= &display_state
;
5055 int cyls
, heads
, secs
, translation
;
5056 int start_emulation
= 1;
5057 char net_clients
[MAX_NET_CLIENTS
][256];
5060 const char *r
, *optarg
;
5061 CharDriverState
*monitor_hd
;
5062 char monitor_device
[128];
5063 char serial_devices
[MAX_SERIAL_PORTS
][128];
5064 int serial_device_index
;
5065 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
5066 int parallel_device_index
;
5067 const char *loadvm
= NULL
;
5068 QEMUMachine
*machine
;
5069 char usb_devices
[MAX_VM_USB_PORTS
][128];
5070 int usb_devices_index
;
5072 LIST_INIT (&vm_change_state_head
);
5073 #if !defined(CONFIG_SOFTMMU)
5074 /* we never want that malloc() uses mmap() */
5075 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
5077 register_machines();
5078 machine
= first_machine
;
5079 initrd_filename
= NULL
;
5080 for(i
= 0; i
< MAX_FD
; i
++)
5081 fd_filename
[i
] = NULL
;
5082 for(i
= 0; i
< MAX_DISKS
; i
++)
5083 hd_filename
[i
] = NULL
;
5084 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5085 vga_ram_size
= VGA_RAM_SIZE
;
5086 bios_size
= BIOS_SIZE
;
5087 #ifdef CONFIG_GDBSTUB
5089 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
5093 kernel_filename
= NULL
;
5094 kernel_cmdline
= "";
5100 cyls
= heads
= secs
= 0;
5101 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5102 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
5104 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
5105 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
5106 serial_devices
[i
][0] = '\0';
5107 serial_device_index
= 0;
5109 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
5110 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
5111 parallel_devices
[i
][0] = '\0';
5112 parallel_device_index
= 0;
5114 usb_devices_index
= 0;
5119 /* default mac address of the first network interface */
5127 hd_filename
[0] = argv
[optind
++];
5129 const QEMUOption
*popt
;
5132 popt
= qemu_options
;
5135 fprintf(stderr
, "%s: invalid option -- '%s'\n",
5139 if (!strcmp(popt
->name
, r
+ 1))
5143 if (popt
->flags
& HAS_ARG
) {
5144 if (optind
>= argc
) {
5145 fprintf(stderr
, "%s: option '%s' requires an argument\n",
5149 optarg
= argv
[optind
++];
5154 switch(popt
->index
) {
5156 machine
= find_machine(optarg
);
5159 printf("Supported machines are:\n");
5160 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5161 printf("%-10s %s%s\n",
5163 m
== first_machine
? " (default)" : "");
5168 case QEMU_OPTION_initrd
:
5169 initrd_filename
= optarg
;
5171 case QEMU_OPTION_hda
:
5172 case QEMU_OPTION_hdb
:
5173 case QEMU_OPTION_hdc
:
5174 case QEMU_OPTION_hdd
:
5177 hd_index
= popt
->index
- QEMU_OPTION_hda
;
5178 hd_filename
[hd_index
] = optarg
;
5179 if (hd_index
== cdrom_index
)
5183 case QEMU_OPTION_snapshot
:
5186 case QEMU_OPTION_hdachs
:
5190 cyls
= strtol(p
, (char **)&p
, 0);
5191 if (cyls
< 1 || cyls
> 16383)
5196 heads
= strtol(p
, (char **)&p
, 0);
5197 if (heads
< 1 || heads
> 16)
5202 secs
= strtol(p
, (char **)&p
, 0);
5203 if (secs
< 1 || secs
> 63)
5207 if (!strcmp(p
, "none"))
5208 translation
= BIOS_ATA_TRANSLATION_NONE
;
5209 else if (!strcmp(p
, "lba"))
5210 translation
= BIOS_ATA_TRANSLATION_LBA
;
5211 else if (!strcmp(p
, "auto"))
5212 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5215 } else if (*p
!= '\0') {
5217 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5222 case QEMU_OPTION_nographic
:
5223 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
5224 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
5227 case QEMU_OPTION_kernel
:
5228 kernel_filename
= optarg
;
5230 case QEMU_OPTION_append
:
5231 kernel_cmdline
= optarg
;
5233 case QEMU_OPTION_cdrom
:
5234 if (cdrom_index
>= 0) {
5235 hd_filename
[cdrom_index
] = optarg
;
5238 case QEMU_OPTION_boot
:
5239 boot_device
= optarg
[0];
5240 if (boot_device
!= 'a' &&
5243 boot_device
!= 'n' &&
5245 boot_device
!= 'c' && boot_device
!= 'd') {
5246 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
5250 case QEMU_OPTION_fda
:
5251 fd_filename
[0] = optarg
;
5253 case QEMU_OPTION_fdb
:
5254 fd_filename
[1] = optarg
;
5256 case QEMU_OPTION_no_code_copy
:
5257 code_copy_enabled
= 0;
5259 case QEMU_OPTION_net
:
5260 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5261 fprintf(stderr
, "qemu: too many network clients\n");
5264 pstrcpy(net_clients
[nb_net_clients
],
5265 sizeof(net_clients
[0]),
5270 case QEMU_OPTION_tftp
:
5271 tftp_prefix
= optarg
;
5274 case QEMU_OPTION_smb
:
5275 net_slirp_smb(optarg
);
5278 case QEMU_OPTION_redir
:
5279 net_slirp_redir(optarg
);
5283 case QEMU_OPTION_audio_help
:
5287 case QEMU_OPTION_soundhw
:
5288 select_soundhw (optarg
);
5295 ram_size
= atoi(optarg
) * 1024 * 1024;
5298 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
5299 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
5300 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
5309 mask
= cpu_str_to_log_mask(optarg
);
5311 printf("Log items (comma separated):\n");
5312 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5313 printf("%-10s %s\n", item
->name
, item
->help
);
5320 #ifdef CONFIG_GDBSTUB
5325 gdbstub_port
= atoi(optarg
);
5332 start_emulation
= 0;
5335 keyboard_layout
= optarg
;
5337 case QEMU_OPTION_localtime
:
5340 case QEMU_OPTION_cirrusvga
:
5341 cirrus_vga_enabled
= 1;
5343 case QEMU_OPTION_std_vga
:
5344 cirrus_vga_enabled
= 0;
5351 w
= strtol(p
, (char **)&p
, 10);
5354 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5360 h
= strtol(p
, (char **)&p
, 10);
5365 depth
= strtol(p
, (char **)&p
, 10);
5366 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5367 depth
!= 24 && depth
!= 32)
5369 } else if (*p
== '\0') {
5370 depth
= graphic_depth
;
5377 graphic_depth
= depth
;
5380 case QEMU_OPTION_monitor
:
5381 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
5383 case QEMU_OPTION_serial
:
5384 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5385 fprintf(stderr
, "qemu: too many serial ports\n");
5388 pstrcpy(serial_devices
[serial_device_index
],
5389 sizeof(serial_devices
[0]), optarg
);
5390 serial_device_index
++;
5392 case QEMU_OPTION_parallel
:
5393 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5394 fprintf(stderr
, "qemu: too many parallel ports\n");
5397 pstrcpy(parallel_devices
[parallel_device_index
],
5398 sizeof(parallel_devices
[0]), optarg
);
5399 parallel_device_index
++;
5401 case QEMU_OPTION_loadvm
:
5404 case QEMU_OPTION_full_screen
:
5407 case QEMU_OPTION_pidfile
:
5408 create_pidfile(optarg
);
5411 case QEMU_OPTION_win2k_hack
:
5412 win2k_install_hack
= 1;
5416 case QEMU_OPTION_no_kqemu
:
5419 case QEMU_OPTION_kernel_kqemu
:
5423 case QEMU_OPTION_usb
:
5426 case QEMU_OPTION_usbdevice
:
5428 if (usb_devices_index
>= MAX_VM_USB_PORTS
) {
5429 fprintf(stderr
, "Too many USB devices\n");
5432 pstrcpy(usb_devices
[usb_devices_index
],
5433 sizeof(usb_devices
[usb_devices_index
]),
5435 usb_devices_index
++;
5437 case QEMU_OPTION_smp
:
5438 smp_cpus
= atoi(optarg
);
5439 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
5440 fprintf(stderr
, "Invalid number of CPUs\n");
5444 case QEMU_OPTION_vnc
:
5445 vnc_display
= atoi(optarg
);
5446 if (vnc_display
< 0) {
5447 fprintf(stderr
, "Invalid VNC display\n");
5451 case QEMU_OPTION_no_acpi
:
5462 linux_boot
= (kernel_filename
!= NULL
);
5465 hd_filename
[0] == '\0' &&
5466 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
5467 fd_filename
[0] == '\0')
5470 /* boot to cd by default if no hard disk */
5471 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
5472 if (fd_filename
[0] != '\0')
5478 #if !defined(CONFIG_SOFTMMU)
5479 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
5481 static uint8_t stdout_buf
[4096];
5482 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
5485 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5492 /* init network clients */
5493 if (nb_net_clients
== 0) {
5494 /* if no clients, we use a default config */
5495 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
5497 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
5502 for(i
= 0;i
< nb_net_clients
; i
++) {
5503 if (net_client_init(net_clients
[i
]) < 0)
5507 /* init the memory */
5508 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
5510 #ifdef CONFIG_SOFTMMU
5511 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5512 if (!phys_ram_base
) {
5513 fprintf(stderr
, "Could not allocate physical memory\n");
5517 /* as we must map the same page at several addresses, we must use
5522 tmpdir
= getenv("QEMU_TMPDIR");
5525 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
5526 if (mkstemp(phys_ram_file
) < 0) {
5527 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
5531 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
5532 if (phys_ram_fd
< 0) {
5533 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
5537 ftruncate(phys_ram_fd
, phys_ram_size
);
5538 unlink(phys_ram_file
);
5539 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
5541 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
5543 if (phys_ram_base
== MAP_FAILED
) {
5544 fprintf(stderr
, "Could not map physical memory\n");
5550 /* we always create the cdrom drive, even if no disk is there */
5552 if (cdrom_index
>= 0) {
5553 bs_table
[cdrom_index
] = bdrv_new("cdrom");
5554 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
5557 /* open the virtual block devices */
5558 for(i
= 0; i
< MAX_DISKS
; i
++) {
5559 if (hd_filename
[i
]) {
5562 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
5563 bs_table
[i
] = bdrv_new(buf
);
5565 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
5566 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
5570 if (i
== 0 && cyls
!= 0) {
5571 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
5572 bdrv_set_translation_hint(bs_table
[i
], translation
);
5577 /* we always create at least one floppy disk */
5578 fd_table
[0] = bdrv_new("fda");
5579 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
5581 for(i
= 0; i
< MAX_FD
; i
++) {
5582 if (fd_filename
[i
]) {
5585 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
5586 fd_table
[i
] = bdrv_new(buf
);
5587 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
5589 if (fd_filename
[i
] != '\0') {
5590 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
5591 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
5599 /* init USB devices */
5601 vm_usb_hub
= usb_hub_init(vm_usb_ports
, MAX_VM_USB_PORTS
);
5602 for(i
= 0; i
< usb_devices_index
; i
++) {
5603 if (usb_device_add(usb_devices
[i
]) < 0) {
5604 fprintf(stderr
, "Warning: could not add USB device %s\n",
5610 register_savevm("timer", 0, 1, timer_save
, timer_load
, NULL
);
5611 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
5614 cpu_calibrate_ticks();
5618 dumb_display_init(ds
);
5619 } else if (vnc_display
!= -1) {
5620 vnc_display_init(ds
, vnc_display
);
5622 #if defined(CONFIG_SDL)
5623 sdl_display_init(ds
, full_screen
);
5624 #elif defined(CONFIG_COCOA)
5625 cocoa_display_init(ds
, full_screen
);
5627 dumb_display_init(ds
);
5631 monitor_hd
= qemu_chr_open(monitor_device
);
5633 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5636 monitor_init(monitor_hd
, !nographic
);
5638 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5639 if (serial_devices
[i
][0] != '\0') {
5640 serial_hds
[i
] = qemu_chr_open(serial_devices
[i
]);
5641 if (!serial_hds
[i
]) {
5642 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5646 if (!strcmp(serial_devices
[i
], "vc"))
5647 qemu_chr_printf(serial_hds
[i
], "serial%d console\n", i
);
5651 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5652 if (parallel_devices
[i
][0] != '\0') {
5653 parallel_hds
[i
] = qemu_chr_open(parallel_devices
[i
]);
5654 if (!parallel_hds
[i
]) {
5655 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5656 parallel_devices
[i
]);
5659 if (!strcmp(parallel_devices
[i
], "vc"))
5660 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\n", i
);
5664 /* setup cpu signal handlers for MMU / self modifying code handling */
5665 #if !defined(CONFIG_SOFTMMU)
5667 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5670 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
5671 stk
.ss_sp
= signal_stack
;
5672 stk
.ss_size
= SIGNAL_STACK_SIZE
;
5675 if (sigaltstack(&stk
, NULL
) < 0) {
5676 perror("sigaltstack");
5682 struct sigaction act
;
5684 sigfillset(&act
.sa_mask
);
5685 act
.sa_flags
= SA_SIGINFO
;
5686 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5687 act
.sa_flags
|= SA_ONSTACK
;
5689 act
.sa_sigaction
= host_segv_handler
;
5690 sigaction(SIGSEGV
, &act
, NULL
);
5691 sigaction(SIGBUS
, &act
, NULL
);
5692 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5693 sigaction(SIGFPE
, &act
, NULL
);
5700 struct sigaction act
;
5701 sigfillset(&act
.sa_mask
);
5703 act
.sa_handler
= SIG_IGN
;
5704 sigaction(SIGPIPE
, &act
, NULL
);
5709 machine
->init(ram_size
, vga_ram_size
, boot_device
,
5710 ds
, fd_filename
, snapshot
,
5711 kernel_filename
, kernel_cmdline
, initrd_filename
);
5713 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
5714 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
5716 #ifdef CONFIG_GDBSTUB
5718 if (gdbserver_start(gdbstub_port
) < 0) {
5719 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
5723 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
5728 qemu_loadvm(loadvm
);
5731 /* XXX: simplify init */
5733 if (start_emulation
) {