4 * Copyright (c) 2003-2005 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 #include <sys/times.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
52 #include <linux/if_tun.h>
55 #include <linux/rtc.h>
56 #include <linux/ppdev.h>
61 #if defined(CONFIG_SLIRP)
67 #include <sys/timeb.h>
69 #define getopt_long_only getopt_long
70 #define memalign(align, size) malloc(size)
73 #include "qemu_socket.h"
79 #endif /* CONFIG_SDL */
83 #define main qemu_main
84 #endif /* CONFIG_COCOA */
90 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
92 //#define DEBUG_UNUSED_IOPORT
93 //#define DEBUG_IOPORT
95 #if !defined(CONFIG_SOFTMMU)
96 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
98 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
102 #define DEFAULT_RAM_SIZE 144
104 #define DEFAULT_RAM_SIZE 128
107 #define GUI_REFRESH_INTERVAL 30
109 /* XXX: use a two level table to limit memory usage */
110 #define MAX_IOPORTS 65536
112 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
113 char phys_ram_file
[1024];
114 void *ioport_opaque
[MAX_IOPORTS
];
115 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
116 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
117 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
120 static DisplayState display_state
;
122 const char* keyboard_layout
= NULL
;
123 int64_t ticks_per_sec
;
124 int boot_device
= 'c';
126 int pit_min_timer_count
= 0;
128 NICInfo nd_table
[MAX_NICS
];
129 QEMUTimer
*gui_timer
;
132 int cirrus_vga_enabled
= 1;
134 int graphic_width
= 1024;
135 int graphic_height
= 768;
137 int graphic_width
= 800;
138 int graphic_height
= 600;
140 int graphic_depth
= 15;
142 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
143 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
145 int win2k_install_hack
= 0;
148 USBPort
*vm_usb_ports
[MAX_VM_USB_PORTS
];
149 USBDevice
*vm_usb_hub
;
150 static VLANState
*first_vlan
;
152 int vnc_display
= -1;
153 #if defined(TARGET_SPARC)
155 #elif defined(TARGET_I386)
161 /***********************************************************/
162 /* x86 ISA bus support */
164 target_phys_addr_t isa_mem_base
= 0;
167 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
169 #ifdef DEBUG_UNUSED_IOPORT
170 fprintf(stderr
, "inb: port=0x%04x\n", address
);
175 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
177 #ifdef DEBUG_UNUSED_IOPORT
178 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
182 /* default is to make two byte accesses */
183 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
186 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
187 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
188 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
192 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
194 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
195 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
196 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
199 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
201 #ifdef DEBUG_UNUSED_IOPORT
202 fprintf(stderr
, "inl: port=0x%04x\n", address
);
207 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
209 #ifdef DEBUG_UNUSED_IOPORT
210 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
214 void init_ioports(void)
218 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
219 ioport_read_table
[0][i
] = default_ioport_readb
;
220 ioport_write_table
[0][i
] = default_ioport_writeb
;
221 ioport_read_table
[1][i
] = default_ioport_readw
;
222 ioport_write_table
[1][i
] = default_ioport_writew
;
223 ioport_read_table
[2][i
] = default_ioport_readl
;
224 ioport_write_table
[2][i
] = default_ioport_writel
;
228 /* size is the word size in byte */
229 int register_ioport_read(int start
, int length
, int size
,
230 IOPortReadFunc
*func
, void *opaque
)
236 } else if (size
== 2) {
238 } else if (size
== 4) {
241 hw_error("register_ioport_read: invalid size");
244 for(i
= start
; i
< start
+ length
; i
+= size
) {
245 ioport_read_table
[bsize
][i
] = func
;
246 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
247 hw_error("register_ioport_read: invalid opaque");
248 ioport_opaque
[i
] = opaque
;
253 /* size is the word size in byte */
254 int register_ioport_write(int start
, int length
, int size
,
255 IOPortWriteFunc
*func
, void *opaque
)
261 } else if (size
== 2) {
263 } else if (size
== 4) {
266 hw_error("register_ioport_write: invalid size");
269 for(i
= start
; i
< start
+ length
; i
+= size
) {
270 ioport_write_table
[bsize
][i
] = func
;
271 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
272 hw_error("register_ioport_read: invalid opaque");
273 ioport_opaque
[i
] = opaque
;
278 void isa_unassign_ioport(int start
, int length
)
282 for(i
= start
; i
< start
+ length
; i
++) {
283 ioport_read_table
[0][i
] = default_ioport_readb
;
284 ioport_read_table
[1][i
] = default_ioport_readw
;
285 ioport_read_table
[2][i
] = default_ioport_readl
;
287 ioport_write_table
[0][i
] = default_ioport_writeb
;
288 ioport_write_table
[1][i
] = default_ioport_writew
;
289 ioport_write_table
[2][i
] = default_ioport_writel
;
293 /***********************************************************/
295 void pstrcpy(char *buf
, int buf_size
, const char *str
)
305 if (c
== 0 || q
>= buf
+ buf_size
- 1)
312 /* strcat and truncate. */
313 char *pstrcat(char *buf
, int buf_size
, const char *s
)
318 pstrcpy(buf
+ len
, buf_size
- len
, s
);
322 int strstart(const char *str
, const char *val
, const char **ptr
)
338 void cpu_outb(CPUState
*env
, int addr
, int val
)
341 if (loglevel
& CPU_LOG_IOPORT
)
342 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
344 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
347 env
->last_io_time
= cpu_get_time_fast();
351 void cpu_outw(CPUState
*env
, int addr
, int val
)
354 if (loglevel
& CPU_LOG_IOPORT
)
355 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
357 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
360 env
->last_io_time
= cpu_get_time_fast();
364 void cpu_outl(CPUState
*env
, int addr
, int val
)
367 if (loglevel
& CPU_LOG_IOPORT
)
368 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
370 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
373 env
->last_io_time
= cpu_get_time_fast();
377 int cpu_inb(CPUState
*env
, int addr
)
380 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
382 if (loglevel
& CPU_LOG_IOPORT
)
383 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
387 env
->last_io_time
= cpu_get_time_fast();
392 int cpu_inw(CPUState
*env
, int addr
)
395 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
397 if (loglevel
& CPU_LOG_IOPORT
)
398 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
402 env
->last_io_time
= cpu_get_time_fast();
407 int cpu_inl(CPUState
*env
, int addr
)
410 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
412 if (loglevel
& CPU_LOG_IOPORT
)
413 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
417 env
->last_io_time
= cpu_get_time_fast();
422 /***********************************************************/
423 void hw_error(const char *fmt
, ...)
429 fprintf(stderr
, "qemu: hardware error: ");
430 vfprintf(stderr
, fmt
, ap
);
431 fprintf(stderr
, "\n");
432 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
433 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
435 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
437 cpu_dump_state(env
, stderr
, fprintf
, 0);
444 /***********************************************************/
447 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
448 static void *qemu_put_kbd_event_opaque
;
449 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
450 static void *qemu_put_mouse_event_opaque
;
451 static int qemu_put_mouse_event_absolute
;
453 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
455 qemu_put_kbd_event_opaque
= opaque
;
456 qemu_put_kbd_event
= func
;
459 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
, int absolute
)
461 qemu_put_mouse_event_opaque
= opaque
;
462 qemu_put_mouse_event
= func
;
463 qemu_put_mouse_event_absolute
= absolute
;
466 void kbd_put_keycode(int keycode
)
468 if (qemu_put_kbd_event
) {
469 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
473 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
475 if (qemu_put_mouse_event
) {
476 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
477 dx
, dy
, dz
, buttons_state
);
481 int kbd_mouse_is_absolute(void)
483 return qemu_put_mouse_event_absolute
;
486 /***********************************************************/
489 #if defined(__powerpc__)
491 static inline uint32_t get_tbl(void)
494 asm volatile("mftb %0" : "=r" (tbl
));
498 static inline uint32_t get_tbu(void)
501 asm volatile("mftbu %0" : "=r" (tbl
));
505 int64_t cpu_get_real_ticks(void)
508 /* NOTE: we test if wrapping has occurred */
514 return ((int64_t)h
<< 32) | l
;
517 #elif defined(__i386__)
519 int64_t cpu_get_real_ticks(void)
523 QueryPerformanceCounter(&ti
);
527 asm volatile ("rdtsc" : "=A" (val
));
532 #elif defined(__x86_64__)
534 int64_t cpu_get_real_ticks(void)
538 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
545 #elif defined(__ia64)
547 int64_t cpu_get_real_ticks(void)
550 asm volatile ("mov %0 = ar.itc" : "=r"(val
) :: "memory");
554 #elif defined(__s390__)
556 int64_t cpu_get_real_ticks(void)
559 asm volatile("stck 0(%1)" : "=m" (val
) : "a" (&val
) : "cc");
564 #error unsupported CPU
567 static int64_t cpu_ticks_prev
;
568 static int64_t cpu_ticks_offset
;
569 static int cpu_ticks_enabled
;
571 static inline int64_t cpu_get_ticks(void)
573 if (!cpu_ticks_enabled
) {
574 return cpu_ticks_offset
;
577 ticks
= cpu_get_real_ticks();
578 if (cpu_ticks_prev
> ticks
) {
579 /* Note: non increasing ticks may happen if the host uses
581 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
583 cpu_ticks_prev
= ticks
;
584 return ticks
+ cpu_ticks_offset
;
588 /* enable cpu_get_ticks() */
589 void cpu_enable_ticks(void)
591 if (!cpu_ticks_enabled
) {
592 cpu_ticks_offset
-= cpu_get_real_ticks();
593 cpu_ticks_enabled
= 1;
597 /* disable cpu_get_ticks() : the clock is stopped. You must not call
598 cpu_get_ticks() after that. */
599 void cpu_disable_ticks(void)
601 if (cpu_ticks_enabled
) {
602 cpu_ticks_offset
= cpu_get_ticks();
603 cpu_ticks_enabled
= 0;
608 void cpu_calibrate_ticks(void)
613 ret
= QueryPerformanceFrequency(&freq
);
615 fprintf(stderr
, "Could not calibrate ticks\n");
618 ticks_per_sec
= freq
.QuadPart
;
622 static int64_t get_clock(void)
625 gettimeofday(&tv
, NULL
);
626 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
629 void cpu_calibrate_ticks(void)
634 ticks
= cpu_get_real_ticks();
636 usec
= get_clock() - usec
;
637 ticks
= cpu_get_real_ticks() - ticks
;
638 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
642 /* compute with 96 bit intermediate result: (a*b)/c */
643 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
648 #ifdef WORDS_BIGENDIAN
658 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
659 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
662 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
666 #define QEMU_TIMER_REALTIME 0
667 #define QEMU_TIMER_VIRTUAL 1
671 /* XXX: add frequency */
679 struct QEMUTimer
*next
;
685 static QEMUTimer
*active_timers
[2];
687 static MMRESULT timerID
;
688 static HANDLE host_alarm
= NULL
;
689 static unsigned int period
= 1;
691 /* frequency of the times() clock tick */
692 static int timer_freq
;
695 QEMUClock
*qemu_new_clock(int type
)
698 clock
= qemu_mallocz(sizeof(QEMUClock
));
705 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
709 ts
= qemu_mallocz(sizeof(QEMUTimer
));
716 void qemu_free_timer(QEMUTimer
*ts
)
721 /* stop a timer, but do not dealloc it */
722 void qemu_del_timer(QEMUTimer
*ts
)
726 /* NOTE: this code must be signal safe because
727 qemu_timer_expired() can be called from a signal. */
728 pt
= &active_timers
[ts
->clock
->type
];
741 /* modify the current timer so that it will be fired when current_time
742 >= expire_time. The corresponding callback will be called. */
743 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
749 /* add the timer in the sorted list */
750 /* NOTE: this code must be signal safe because
751 qemu_timer_expired() can be called from a signal. */
752 pt
= &active_timers
[ts
->clock
->type
];
757 if (t
->expire_time
> expire_time
)
761 ts
->expire_time
= expire_time
;
766 int qemu_timer_pending(QEMUTimer
*ts
)
769 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
776 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
780 return (timer_head
->expire_time
<= current_time
);
783 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
789 if (!ts
|| ts
->expire_time
> current_time
)
791 /* remove timer from the list before calling the callback */
792 *ptimer_head
= ts
->next
;
795 /* run the callback (the timer list can be modified) */
800 int64_t qemu_get_clock(QEMUClock
*clock
)
802 switch(clock
->type
) {
803 case QEMU_TIMER_REALTIME
:
805 return GetTickCount();
810 /* Note that using gettimeofday() is not a good solution
811 for timers because its value change when the date is
813 if (timer_freq
== 100) {
814 return times(&tp
) * 10;
816 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
821 case QEMU_TIMER_VIRTUAL
:
822 return cpu_get_ticks();
827 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
829 uint64_t expire_time
;
831 if (qemu_timer_pending(ts
)) {
832 expire_time
= ts
->expire_time
;
836 qemu_put_be64(f
, expire_time
);
839 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
841 uint64_t expire_time
;
843 expire_time
= qemu_get_be64(f
);
844 if (expire_time
!= -1) {
845 qemu_mod_timer(ts
, expire_time
);
851 static void timer_save(QEMUFile
*f
, void *opaque
)
853 if (cpu_ticks_enabled
) {
854 hw_error("cannot save state if virtual timers are running");
856 qemu_put_be64s(f
, &cpu_ticks_offset
);
857 qemu_put_be64s(f
, &ticks_per_sec
);
860 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
864 if (cpu_ticks_enabled
) {
867 qemu_get_be64s(f
, &cpu_ticks_offset
);
868 qemu_get_be64s(f
, &ticks_per_sec
);
873 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
874 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
876 static void host_alarm_handler(int host_signum
)
880 #define DISP_FREQ 1000
882 static int64_t delta_min
= INT64_MAX
;
883 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
885 ti
= qemu_get_clock(vm_clock
);
886 if (last_clock
!= 0) {
887 delta
= ti
- last_clock
;
888 if (delta
< delta_min
)
890 if (delta
> delta_max
)
893 if (++count
== DISP_FREQ
) {
894 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
895 muldiv64(delta_min
, 1000000, ticks_per_sec
),
896 muldiv64(delta_max
, 1000000, ticks_per_sec
),
897 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
898 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
900 delta_min
= INT64_MAX
;
908 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
909 qemu_get_clock(vm_clock
)) ||
910 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
911 qemu_get_clock(rt_clock
))) {
913 SetEvent(host_alarm
);
915 CPUState
*env
= cpu_single_env
;
917 /* stop the currently executing cpu because a timer occured */
918 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
920 if (env
->kqemu_enabled
) {
921 kqemu_cpu_interrupt(env
);
930 #if defined(__linux__)
932 #define RTC_FREQ 1024
936 static int start_rtc_timer(void)
938 rtc_fd
= open("/dev/rtc", O_RDONLY
);
941 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
942 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
943 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
944 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
947 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
952 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
958 static int start_rtc_timer(void)
963 #endif /* !defined(__linux__) */
965 #endif /* !defined(_WIN32) */
967 static void init_timers(void)
969 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
970 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
977 ZeroMemory(&tc
, sizeof(TIMECAPS
));
978 timeGetDevCaps(&tc
, sizeof(TIMECAPS
));
979 if (period
< tc
.wPeriodMin
)
980 period
= tc
.wPeriodMin
;
981 timeBeginPeriod(period
);
982 timerID
= timeSetEvent(1, // interval (ms)
983 period
, // resolution
984 host_alarm_handler
, // function
985 (DWORD
)&count
, // user parameter
986 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
988 perror("failed timer alarm");
991 host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
993 perror("failed CreateEvent");
996 ResetEvent(host_alarm
);
998 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
1001 struct sigaction act
;
1002 struct itimerval itv
;
1004 /* get times() syscall frequency */
1005 timer_freq
= sysconf(_SC_CLK_TCK
);
1008 sigfillset(&act
.sa_mask
);
1010 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1011 act
.sa_flags
|= SA_ONSTACK
;
1013 act
.sa_handler
= host_alarm_handler
;
1014 sigaction(SIGALRM
, &act
, NULL
);
1016 itv
.it_interval
.tv_sec
= 0;
1017 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
1018 itv
.it_value
.tv_sec
= 0;
1019 itv
.it_value
.tv_usec
= 10 * 1000;
1020 setitimer(ITIMER_REAL
, &itv
, NULL
);
1021 /* we probe the tick duration of the kernel to inform the user if
1022 the emulated kernel requested a too high timer frequency */
1023 getitimer(ITIMER_REAL
, &itv
);
1025 #if defined(__linux__)
1026 /* XXX: force /dev/rtc usage because even 2.6 kernels may not
1027 have timers with 1 ms resolution. The correct solution will
1028 be to use the POSIX real time timers available in recent
1030 if (itv
.it_interval
.tv_usec
> 1000 || 1) {
1031 /* try to use /dev/rtc to have a faster timer */
1032 if (start_rtc_timer() < 0)
1034 /* disable itimer */
1035 itv
.it_interval
.tv_sec
= 0;
1036 itv
.it_interval
.tv_usec
= 0;
1037 itv
.it_value
.tv_sec
= 0;
1038 itv
.it_value
.tv_usec
= 0;
1039 setitimer(ITIMER_REAL
, &itv
, NULL
);
1042 sigaction(SIGIO
, &act
, NULL
);
1043 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1044 fcntl(rtc_fd
, F_SETOWN
, getpid());
1046 #endif /* defined(__linux__) */
1049 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1050 PIT_FREQ
) / 1000000;
1056 void quit_timers(void)
1059 timeKillEvent(timerID
);
1060 timeEndPeriod(period
);
1062 CloseHandle(host_alarm
);
1068 /***********************************************************/
1069 /* character device */
1071 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1073 return s
->chr_write(s
, buf
, len
);
1076 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1080 return s
->chr_ioctl(s
, cmd
, arg
);
1083 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1088 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1089 qemu_chr_write(s
, buf
, strlen(buf
));
1093 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1095 if (s
->chr_send_event
)
1096 s
->chr_send_event(s
, event
);
1099 void qemu_chr_add_read_handler(CharDriverState
*s
,
1100 IOCanRWHandler
*fd_can_read
,
1101 IOReadHandler
*fd_read
, void *opaque
)
1103 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
1106 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
1108 s
->chr_event
= chr_event
;
1111 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1116 static void null_chr_add_read_handler(CharDriverState
*chr
,
1117 IOCanRWHandler
*fd_can_read
,
1118 IOReadHandler
*fd_read
, void *opaque
)
1122 CharDriverState
*qemu_chr_open_null(void)
1124 CharDriverState
*chr
;
1126 chr
= qemu_mallocz(sizeof(CharDriverState
));
1129 chr
->chr_write
= null_chr_write
;
1130 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1136 static void socket_cleanup(void)
1141 static int socket_init(void)
1146 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1148 err
= WSAGetLastError();
1149 fprintf(stderr
, "WSAStartup: %d\n", err
);
1152 atexit(socket_cleanup
);
1156 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1162 ret
= send(fd
, buf
, len
, 0);
1165 errno
= WSAGetLastError();
1166 if (errno
!= WSAEWOULDBLOCK
) {
1169 } else if (ret
== 0) {
1179 void socket_set_nonblock(int fd
)
1181 unsigned long opt
= 1;
1182 ioctlsocket(fd
, FIONBIO
, &opt
);
1187 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1193 ret
= write(fd
, buf
, len
);
1195 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1197 } else if (ret
== 0) {
1207 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1209 return unix_write(fd
, buf
, len1
);
1212 void socket_set_nonblock(int fd
)
1214 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1216 #endif /* !_WIN32 */
1222 IOCanRWHandler
*fd_can_read
;
1223 IOReadHandler
*fd_read
;
1228 #define STDIO_MAX_CLIENTS 2
1230 static int stdio_nb_clients
;
1231 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1233 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1235 FDCharDriver
*s
= chr
->opaque
;
1236 return unix_write(s
->fd_out
, buf
, len
);
1239 static int fd_chr_read_poll(void *opaque
)
1241 CharDriverState
*chr
= opaque
;
1242 FDCharDriver
*s
= chr
->opaque
;
1244 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
1248 static void fd_chr_read(void *opaque
)
1250 CharDriverState
*chr
= opaque
;
1251 FDCharDriver
*s
= chr
->opaque
;
1256 if (len
> s
->max_size
)
1260 size
= read(s
->fd_in
, buf
, len
);
1262 s
->fd_read(s
->fd_opaque
, buf
, size
);
1266 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1267 IOCanRWHandler
*fd_can_read
,
1268 IOReadHandler
*fd_read
, void *opaque
)
1270 FDCharDriver
*s
= chr
->opaque
;
1272 if (s
->fd_in
>= 0) {
1273 s
->fd_can_read
= fd_can_read
;
1274 s
->fd_read
= fd_read
;
1275 s
->fd_opaque
= opaque
;
1276 if (nographic
&& s
->fd_in
== 0) {
1278 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1279 fd_chr_read
, NULL
, chr
);
1284 /* open a character device to a unix fd */
1285 CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1287 CharDriverState
*chr
;
1290 chr
= qemu_mallocz(sizeof(CharDriverState
));
1293 s
= qemu_mallocz(sizeof(FDCharDriver
));
1301 chr
->chr_write
= fd_chr_write
;
1302 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1306 CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1310 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1313 return qemu_chr_open_fd(-1, fd_out
);
1316 CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1320 fd
= open(filename
, O_RDWR
| O_BINARY
);
1323 return qemu_chr_open_fd(fd
, fd
);
1327 /* for STDIO, we handle the case where several clients use it
1330 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1332 #define TERM_FIFO_MAX_SIZE 1
1334 static int term_got_escape
, client_index
;
1335 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1338 void term_print_help(void)
1341 "C-a h print this help\n"
1342 "C-a x exit emulator\n"
1343 "C-a s save disk data back to file (if -snapshot)\n"
1344 "C-a b send break (magic sysrq)\n"
1345 "C-a c switch between console and monitor\n"
1346 "C-a C-a send C-a\n"
1350 /* called when a char is received */
1351 static void stdio_received_byte(int ch
)
1353 if (term_got_escape
) {
1354 term_got_escape
= 0;
1365 for (i
= 0; i
< MAX_DISKS
; i
++) {
1367 bdrv_commit(bs_table
[i
]);
1372 if (client_index
< stdio_nb_clients
) {
1373 CharDriverState
*chr
;
1376 chr
= stdio_clients
[client_index
];
1378 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1383 if (client_index
>= stdio_nb_clients
)
1385 if (client_index
== 0) {
1386 /* send a new line in the monitor to get the prompt */
1394 } else if (ch
== TERM_ESCAPE
) {
1395 term_got_escape
= 1;
1398 if (client_index
< stdio_nb_clients
) {
1400 CharDriverState
*chr
;
1403 chr
= stdio_clients
[client_index
];
1405 if (s
->fd_can_read(s
->fd_opaque
) > 0) {
1407 s
->fd_read(s
->fd_opaque
, buf
, 1);
1408 } else if (term_fifo_size
== 0) {
1409 term_fifo
[term_fifo_size
++] = ch
;
1415 static int stdio_read_poll(void *opaque
)
1417 CharDriverState
*chr
;
1420 if (client_index
< stdio_nb_clients
) {
1421 chr
= stdio_clients
[client_index
];
1423 /* try to flush the queue if needed */
1424 if (term_fifo_size
!= 0 && s
->fd_can_read(s
->fd_opaque
) > 0) {
1425 s
->fd_read(s
->fd_opaque
, term_fifo
, 1);
1428 /* see if we can absorb more chars */
1429 if (term_fifo_size
== 0)
1438 static void stdio_read(void *opaque
)
1443 size
= read(0, buf
, 1);
1445 stdio_received_byte(buf
[0]);
1448 /* init terminal so that we can grab keys */
1449 static struct termios oldtty
;
1450 static int old_fd0_flags
;
1452 static void term_exit(void)
1454 tcsetattr (0, TCSANOW
, &oldtty
);
1455 fcntl(0, F_SETFL
, old_fd0_flags
);
1458 static void term_init(void)
1462 tcgetattr (0, &tty
);
1464 old_fd0_flags
= fcntl(0, F_GETFL
);
1466 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1467 |INLCR
|IGNCR
|ICRNL
|IXON
);
1468 tty
.c_oflag
|= OPOST
;
1469 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1470 /* if graphical mode, we allow Ctrl-C handling */
1472 tty
.c_lflag
&= ~ISIG
;
1473 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1476 tty
.c_cc
[VTIME
] = 0;
1478 tcsetattr (0, TCSANOW
, &tty
);
1482 fcntl(0, F_SETFL
, O_NONBLOCK
);
1485 CharDriverState
*qemu_chr_open_stdio(void)
1487 CharDriverState
*chr
;
1490 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1492 chr
= qemu_chr_open_fd(0, 1);
1493 if (stdio_nb_clients
== 0)
1494 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, NULL
);
1495 client_index
= stdio_nb_clients
;
1497 if (stdio_nb_clients
!= 0)
1499 chr
= qemu_chr_open_fd(0, 1);
1501 stdio_clients
[stdio_nb_clients
++] = chr
;
1502 if (stdio_nb_clients
== 1) {
1503 /* set the terminal in raw mode */
1509 #if defined(__linux__)
1510 CharDriverState
*qemu_chr_open_pty(void)
1513 char slave_name
[1024];
1514 int master_fd
, slave_fd
;
1516 /* Not satisfying */
1517 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1521 /* Disabling local echo and line-buffered output */
1522 tcgetattr (master_fd
, &tty
);
1523 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1525 tty
.c_cc
[VTIME
] = 0;
1526 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1528 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1529 return qemu_chr_open_fd(master_fd
, master_fd
);
1532 static void tty_serial_init(int fd
, int speed
,
1533 int parity
, int data_bits
, int stop_bits
)
1539 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1540 speed
, parity
, data_bits
, stop_bits
);
1542 tcgetattr (fd
, &tty
);
1584 cfsetispeed(&tty
, spd
);
1585 cfsetospeed(&tty
, spd
);
1587 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1588 |INLCR
|IGNCR
|ICRNL
|IXON
);
1589 tty
.c_oflag
|= OPOST
;
1590 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1591 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
);
1612 tty
.c_cflag
|= PARENB
;
1615 tty
.c_cflag
|= PARENB
| PARODD
;
1619 tcsetattr (fd
, TCSANOW
, &tty
);
1622 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1624 FDCharDriver
*s
= chr
->opaque
;
1627 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1629 QEMUSerialSetParams
*ssp
= arg
;
1630 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1631 ssp
->data_bits
, ssp
->stop_bits
);
1634 case CHR_IOCTL_SERIAL_SET_BREAK
:
1636 int enable
= *(int *)arg
;
1638 tcsendbreak(s
->fd_in
, 1);
1647 CharDriverState
*qemu_chr_open_tty(const char *filename
)
1649 CharDriverState
*chr
;
1652 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1655 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1656 tty_serial_init(fd
, 115200, 'N', 8, 1);
1657 chr
= qemu_chr_open_fd(fd
, fd
);
1660 chr
->chr_ioctl
= tty_serial_ioctl
;
1664 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1666 int fd
= (int)chr
->opaque
;
1670 case CHR_IOCTL_PP_READ_DATA
:
1671 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1673 *(uint8_t *)arg
= b
;
1675 case CHR_IOCTL_PP_WRITE_DATA
:
1676 b
= *(uint8_t *)arg
;
1677 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1680 case CHR_IOCTL_PP_READ_CONTROL
:
1681 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1683 *(uint8_t *)arg
= b
;
1685 case CHR_IOCTL_PP_WRITE_CONTROL
:
1686 b
= *(uint8_t *)arg
;
1687 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1690 case CHR_IOCTL_PP_READ_STATUS
:
1691 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1693 *(uint8_t *)arg
= b
;
1701 CharDriverState
*qemu_chr_open_pp(const char *filename
)
1703 CharDriverState
*chr
;
1706 fd
= open(filename
, O_RDWR
);
1710 if (ioctl(fd
, PPCLAIM
) < 0) {
1715 chr
= qemu_mallocz(sizeof(CharDriverState
));
1720 chr
->opaque
= (void *)fd
;
1721 chr
->chr_write
= null_chr_write
;
1722 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1723 chr
->chr_ioctl
= pp_ioctl
;
1728 CharDriverState
*qemu_chr_open_pty(void)
1734 #endif /* !defined(_WIN32) */
1738 IOCanRWHandler
*fd_can_read
;
1739 IOReadHandler
*fd_read
;
1742 HANDLE hcom
, hrecv
, hsend
;
1743 OVERLAPPED orecv
, osend
;
1748 #define NSENDBUF 2048
1749 #define NRECVBUF 2048
1750 #define MAXCONNECT 1
1751 #define NTIMEOUT 5000
1753 static int win_chr_poll(void *opaque
);
1754 static int win_chr_pipe_poll(void *opaque
);
1756 static void win_chr_close2(WinCharState
*s
)
1759 CloseHandle(s
->hsend
);
1763 CloseHandle(s
->hrecv
);
1767 CloseHandle(s
->hcom
);
1771 qemu_del_polling_cb(win_chr_pipe_poll
, s
);
1773 qemu_del_polling_cb(win_chr_poll
, s
);
1776 static void win_chr_close(CharDriverState
*chr
)
1778 WinCharState
*s
= chr
->opaque
;
1782 static int win_chr_init(WinCharState
*s
, const char *filename
)
1785 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1790 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1792 fprintf(stderr
, "Failed CreateEvent\n");
1795 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1797 fprintf(stderr
, "Failed CreateEvent\n");
1801 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1802 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1803 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1804 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1809 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1810 fprintf(stderr
, "Failed SetupComm\n");
1814 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1815 size
= sizeof(COMMCONFIG
);
1816 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1817 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1818 CommConfigDialog(filename
, NULL
, &comcfg
);
1820 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1821 fprintf(stderr
, "Failed SetCommState\n");
1825 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1826 fprintf(stderr
, "Failed SetCommMask\n");
1830 cto
.ReadIntervalTimeout
= MAXDWORD
;
1831 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1832 fprintf(stderr
, "Failed SetCommTimeouts\n");
1836 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1837 fprintf(stderr
, "Failed ClearCommError\n");
1840 qemu_add_polling_cb(win_chr_poll
, s
);
1848 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1850 WinCharState
*s
= chr
->opaque
;
1851 DWORD len
, ret
, size
, err
;
1854 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1855 s
->osend
.hEvent
= s
->hsend
;
1858 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1860 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1862 err
= GetLastError();
1863 if (err
== ERROR_IO_PENDING
) {
1864 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1882 static int win_chr_read_poll(WinCharState
*s
)
1884 s
->max_size
= s
->fd_can_read(s
->win_opaque
);
1888 static void win_chr_readfile(WinCharState
*s
)
1894 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1895 s
->orecv
.hEvent
= s
->hrecv
;
1896 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1898 err
= GetLastError();
1899 if (err
== ERROR_IO_PENDING
) {
1900 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1905 s
->fd_read(s
->win_opaque
, buf
, size
);
1909 static void win_chr_read(WinCharState
*s
)
1911 if (s
->len
> s
->max_size
)
1912 s
->len
= s
->max_size
;
1916 win_chr_readfile(s
);
1919 static int win_chr_poll(void *opaque
)
1921 WinCharState
*s
= opaque
;
1925 ClearCommError(s
->hcom
, &comerr
, &status
);
1926 if (status
.cbInQue
> 0) {
1927 s
->len
= status
.cbInQue
;
1928 win_chr_read_poll(s
);
1935 static void win_chr_add_read_handler(CharDriverState
*chr
,
1936 IOCanRWHandler
*fd_can_read
,
1937 IOReadHandler
*fd_read
, void *opaque
)
1939 WinCharState
*s
= chr
->opaque
;
1941 s
->fd_can_read
= fd_can_read
;
1942 s
->fd_read
= fd_read
;
1943 s
->win_opaque
= opaque
;
1946 CharDriverState
*qemu_chr_open_win(const char *filename
)
1948 CharDriverState
*chr
;
1951 chr
= qemu_mallocz(sizeof(CharDriverState
));
1954 s
= qemu_mallocz(sizeof(WinCharState
));
1960 chr
->chr_write
= win_chr_write
;
1961 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
1962 chr
->chr_close
= win_chr_close
;
1964 if (win_chr_init(s
, filename
) < 0) {
1972 static int win_chr_pipe_poll(void *opaque
)
1974 WinCharState
*s
= opaque
;
1977 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1980 win_chr_read_poll(s
);
1987 static int win_chr_pipe_init(WinCharState
*s
, const char *filename
)
1996 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1998 fprintf(stderr
, "Failed CreateEvent\n");
2001 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2003 fprintf(stderr
, "Failed CreateEvent\n");
2007 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2008 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2009 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2011 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2012 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2013 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2018 ZeroMemory(&ov
, sizeof(ov
));
2019 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2020 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2022 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2026 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2028 fprintf(stderr
, "Failed GetOverlappedResult\n");
2030 CloseHandle(ov
.hEvent
);
2037 CloseHandle(ov
.hEvent
);
2040 qemu_add_polling_cb(win_chr_pipe_poll
, s
);
2049 CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2051 CharDriverState
*chr
;
2054 chr
= qemu_mallocz(sizeof(CharDriverState
));
2057 s
= qemu_mallocz(sizeof(WinCharState
));
2063 chr
->chr_write
= win_chr_write
;
2064 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2065 chr
->chr_close
= win_chr_close
;
2067 if (win_chr_pipe_init(s
, filename
) < 0) {
2075 CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2077 CharDriverState
*chr
;
2080 chr
= qemu_mallocz(sizeof(CharDriverState
));
2083 s
= qemu_mallocz(sizeof(WinCharState
));
2090 chr
->chr_write
= win_chr_write
;
2091 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2095 CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2099 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2100 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2101 if (fd_out
== INVALID_HANDLE_VALUE
)
2104 return qemu_chr_open_win_file(fd_out
);
2108 CharDriverState
*qemu_chr_open(const char *filename
)
2112 if (!strcmp(filename
, "vc")) {
2113 return text_console_init(&display_state
);
2114 } else if (!strcmp(filename
, "null")) {
2115 return qemu_chr_open_null();
2118 if (strstart(filename
, "file:", &p
)) {
2119 return qemu_chr_open_file_out(p
);
2120 } else if (strstart(filename
, "pipe:", &p
)) {
2121 return qemu_chr_open_pipe(p
);
2122 } else if (!strcmp(filename
, "pty")) {
2123 return qemu_chr_open_pty();
2124 } else if (!strcmp(filename
, "stdio")) {
2125 return qemu_chr_open_stdio();
2128 #if defined(__linux__)
2129 if (strstart(filename
, "/dev/parport", NULL
)) {
2130 return qemu_chr_open_pp(filename
);
2132 if (strstart(filename
, "/dev/", NULL
)) {
2133 return qemu_chr_open_tty(filename
);
2137 if (strstart(filename
, "COM", NULL
)) {
2138 return qemu_chr_open_win(filename
);
2140 if (strstart(filename
, "pipe:", &p
)) {
2141 return qemu_chr_open_win_pipe(p
);
2143 if (strstart(filename
, "file:", &p
)) {
2144 return qemu_chr_open_win_file_out(p
);
2152 void qemu_chr_close(CharDriverState
*chr
)
2155 chr
->chr_close(chr
);
2158 /***********************************************************/
2159 /* network device redirectors */
2161 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2165 for(i
=0;i
<size
;i
+=16) {
2169 fprintf(f
, "%08x ", i
);
2172 fprintf(f
, " %02x", buf
[i
+j
]);
2177 for(j
=0;j
<len
;j
++) {
2179 if (c
< ' ' || c
> '~')
2181 fprintf(f
, "%c", c
);
2187 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
2190 for(i
= 0; i
< 6; i
++) {
2191 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2204 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
2209 p1
= strchr(p
, sep
);
2215 if (len
> buf_size
- 1)
2217 memcpy(buf
, p
, len
);
2224 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
2232 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2234 saddr
->sin_family
= AF_INET
;
2235 if (buf
[0] == '\0') {
2236 saddr
->sin_addr
.s_addr
= 0;
2238 if (isdigit(buf
[0])) {
2239 if (!inet_aton(buf
, &saddr
->sin_addr
))
2242 if ((he
= gethostbyname(buf
)) == NULL
)
2244 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
2247 port
= strtol(p
, (char **)&r
, 0);
2250 saddr
->sin_port
= htons(port
);
2254 /* find or alloc a new VLAN */
2255 VLANState
*qemu_find_vlan(int id
)
2257 VLANState
**pvlan
, *vlan
;
2258 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2262 vlan
= qemu_mallocz(sizeof(VLANState
));
2267 pvlan
= &first_vlan
;
2268 while (*pvlan
!= NULL
)
2269 pvlan
= &(*pvlan
)->next
;
2274 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
2275 IOReadHandler
*fd_read
,
2276 IOCanRWHandler
*fd_can_read
,
2279 VLANClientState
*vc
, **pvc
;
2280 vc
= qemu_mallocz(sizeof(VLANClientState
));
2283 vc
->fd_read
= fd_read
;
2284 vc
->fd_can_read
= fd_can_read
;
2285 vc
->opaque
= opaque
;
2289 pvc
= &vlan
->first_client
;
2290 while (*pvc
!= NULL
)
2291 pvc
= &(*pvc
)->next
;
2296 int qemu_can_send_packet(VLANClientState
*vc1
)
2298 VLANState
*vlan
= vc1
->vlan
;
2299 VLANClientState
*vc
;
2301 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2303 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
2310 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
2312 VLANState
*vlan
= vc1
->vlan
;
2313 VLANClientState
*vc
;
2316 printf("vlan %d send:\n", vlan
->id
);
2317 hex_dump(stdout
, buf
, size
);
2319 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2321 vc
->fd_read(vc
->opaque
, buf
, size
);
2326 #if defined(CONFIG_SLIRP)
2328 /* slirp network adapter */
2330 static int slirp_inited
;
2331 static VLANClientState
*slirp_vc
;
2333 int slirp_can_output(void)
2335 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
2338 void slirp_output(const uint8_t *pkt
, int pkt_len
)
2341 printf("slirp output:\n");
2342 hex_dump(stdout
, pkt
, pkt_len
);
2346 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
2349 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
2352 printf("slirp input:\n");
2353 hex_dump(stdout
, buf
, size
);
2355 slirp_input(buf
, size
);
2358 static int net_slirp_init(VLANState
*vlan
)
2360 if (!slirp_inited
) {
2364 slirp_vc
= qemu_new_vlan_client(vlan
,
2365 slirp_receive
, NULL
, NULL
);
2366 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
2370 static void net_slirp_redir(const char *redir_str
)
2375 struct in_addr guest_addr
;
2376 int host_port
, guest_port
;
2378 if (!slirp_inited
) {
2384 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2386 if (!strcmp(buf
, "tcp")) {
2388 } else if (!strcmp(buf
, "udp")) {
2394 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2396 host_port
= strtol(buf
, &r
, 0);
2400 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2402 if (buf
[0] == '\0') {
2403 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
2405 if (!inet_aton(buf
, &guest_addr
))
2408 guest_port
= strtol(p
, &r
, 0);
2412 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
2413 fprintf(stderr
, "qemu: could not set up redirection\n");
2418 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
2426 static void smb_exit(void)
2430 char filename
[1024];
2432 /* erase all the files in the directory */
2433 d
= opendir(smb_dir
);
2438 if (strcmp(de
->d_name
, ".") != 0 &&
2439 strcmp(de
->d_name
, "..") != 0) {
2440 snprintf(filename
, sizeof(filename
), "%s/%s",
2441 smb_dir
, de
->d_name
);
2449 /* automatic user mode samba server configuration */
2450 void net_slirp_smb(const char *exported_dir
)
2452 char smb_conf
[1024];
2453 char smb_cmdline
[1024];
2456 if (!slirp_inited
) {
2461 /* XXX: better tmp dir construction */
2462 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
2463 if (mkdir(smb_dir
, 0700) < 0) {
2464 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
2467 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
2469 f
= fopen(smb_conf
, "w");
2471 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
2478 "socket address=127.0.0.1\n"
2479 "pid directory=%s\n"
2480 "lock directory=%s\n"
2481 "log file=%s/log.smbd\n"
2482 "smb passwd file=%s/smbpasswd\n"
2483 "security = share\n"
2498 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "/usr/sbin/smbd -s %s",
2501 slirp_add_exec(0, smb_cmdline
, 4, 139);
2504 #endif /* !defined(_WIN32) */
2506 #endif /* CONFIG_SLIRP */
2508 #if !defined(_WIN32)
2510 typedef struct TAPState
{
2511 VLANClientState
*vc
;
2515 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
2517 TAPState
*s
= opaque
;
2520 ret
= write(s
->fd
, buf
, size
);
2521 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
2528 static void tap_send(void *opaque
)
2530 TAPState
*s
= opaque
;
2534 size
= read(s
->fd
, buf
, sizeof(buf
));
2536 qemu_send_packet(s
->vc
, buf
, size
);
2542 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
2546 s
= qemu_mallocz(sizeof(TAPState
));
2550 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
2551 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
2552 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
2557 static int tap_open(char *ifname
, int ifname_size
)
2563 fd
= open("/dev/tap", O_RDWR
);
2565 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
2570 dev
= devname(s
.st_rdev
, S_IFCHR
);
2571 pstrcpy(ifname
, ifname_size
, dev
);
2573 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2576 #elif defined(__sun__)
2577 static int tap_open(char *ifname
, int ifname_size
)
2579 fprintf(stderr
, "warning: tap_open not yet implemented\n");
2583 static int tap_open(char *ifname
, int ifname_size
)
2588 fd
= open("/dev/net/tun", O_RDWR
);
2590 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
2593 memset(&ifr
, 0, sizeof(ifr
));
2594 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
2595 if (ifname
[0] != '\0')
2596 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
2598 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
2599 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
2601 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
2605 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
2606 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2611 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
2612 const char *setup_script
)
2615 int pid
, status
, fd
;
2620 if (ifname1
!= NULL
)
2621 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
2624 fd
= tap_open(ifname
, sizeof(ifname
));
2630 if (setup_script
[0] != '\0') {
2631 /* try to launch network init script */
2636 *parg
++ = (char *)setup_script
;
2639 execv(setup_script
, args
);
2642 while (waitpid(pid
, &status
, 0) != pid
);
2643 if (!WIFEXITED(status
) ||
2644 WEXITSTATUS(status
) != 0) {
2645 fprintf(stderr
, "%s: could not launch network script\n",
2651 s
= net_tap_fd_init(vlan
, fd
);
2654 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2655 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
2659 #endif /* !_WIN32 */
2661 /* network connection */
2662 typedef struct NetSocketState
{
2663 VLANClientState
*vc
;
2665 int state
; /* 0 = getting length, 1 = getting data */
2669 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
2672 typedef struct NetSocketListenState
{
2675 } NetSocketListenState
;
2677 /* XXX: we consider we can send the whole packet without blocking */
2678 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
2680 NetSocketState
*s
= opaque
;
2684 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
2685 send_all(s
->fd
, buf
, size
);
2688 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
2690 NetSocketState
*s
= opaque
;
2691 sendto(s
->fd
, buf
, size
, 0,
2692 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
2695 static void net_socket_send(void *opaque
)
2697 NetSocketState
*s
= opaque
;
2702 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
2704 err
= socket_error();
2705 if (err
!= EWOULDBLOCK
)
2707 } else if (size
== 0) {
2708 /* end of connection */
2710 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2716 /* reassemble a packet from the network */
2722 memcpy(s
->buf
+ s
->index
, buf
, l
);
2726 if (s
->index
== 4) {
2728 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
2734 l
= s
->packet_len
- s
->index
;
2737 memcpy(s
->buf
+ s
->index
, buf
, l
);
2741 if (s
->index
>= s
->packet_len
) {
2742 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
2751 static void net_socket_send_dgram(void *opaque
)
2753 NetSocketState
*s
= opaque
;
2756 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2760 /* end of connection */
2761 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2764 qemu_send_packet(s
->vc
, s
->buf
, size
);
2767 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
2772 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
2773 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
2774 inet_ntoa(mcastaddr
->sin_addr
),
2775 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
2779 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2781 perror("socket(PF_INET, SOCK_DGRAM)");
2786 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
2787 (const char *)&val
, sizeof(val
));
2789 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
2793 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
2799 /* Add host to multicast group */
2800 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
2801 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
2803 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
2804 (const char *)&imr
, sizeof(struct ip_mreq
));
2806 perror("setsockopt(IP_ADD_MEMBERSHIP)");
2810 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
2812 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
2813 (const char *)&val
, sizeof(val
));
2815 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
2819 socket_set_nonblock(fd
);
2822 if (fd
>=0) close(fd
);
2826 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
2829 struct sockaddr_in saddr
;
2831 socklen_t saddr_len
;
2834 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
2835 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
2836 * by ONLY ONE process: we must "clone" this dgram socket --jjo
2840 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
2842 if (saddr
.sin_addr
.s_addr
==0) {
2843 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
2847 /* clone dgram socket */
2848 newfd
= net_socket_mcast_create(&saddr
);
2850 /* error already reported by net_socket_mcast_create() */
2854 /* clone newfd to fd, close newfd */
2859 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
2860 fd
, strerror(errno
));
2865 s
= qemu_mallocz(sizeof(NetSocketState
));
2870 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
2871 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
2873 /* mcast: save bound address as dst */
2874 if (is_connected
) s
->dgram_dst
=saddr
;
2876 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2877 "socket: fd=%d (%s mcast=%s:%d)",
2878 fd
, is_connected
? "cloned" : "",
2879 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2883 static void net_socket_connect(void *opaque
)
2885 NetSocketState
*s
= opaque
;
2886 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
2889 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
2893 s
= qemu_mallocz(sizeof(NetSocketState
));
2897 s
->vc
= qemu_new_vlan_client(vlan
,
2898 net_socket_receive
, NULL
, s
);
2899 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2900 "socket: fd=%d", fd
);
2902 net_socket_connect(s
);
2904 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
2909 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
2912 int so_type
=-1, optlen
=sizeof(so_type
);
2914 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
2915 fprintf(stderr
, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd
);
2920 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
2922 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
2924 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
2925 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
2926 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
2931 static void net_socket_accept(void *opaque
)
2933 NetSocketListenState
*s
= opaque
;
2935 struct sockaddr_in saddr
;
2940 len
= sizeof(saddr
);
2941 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
2942 if (fd
< 0 && errno
!= EINTR
) {
2944 } else if (fd
>= 0) {
2948 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
2952 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
2953 "socket: connection from %s:%d",
2954 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2958 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
2960 NetSocketListenState
*s
;
2962 struct sockaddr_in saddr
;
2964 if (parse_host_port(&saddr
, host_str
) < 0)
2967 s
= qemu_mallocz(sizeof(NetSocketListenState
));
2971 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2976 socket_set_nonblock(fd
);
2978 /* allow fast reuse */
2980 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2982 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2987 ret
= listen(fd
, 0);
2994 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
2998 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
3001 int fd
, connected
, ret
, err
;
3002 struct sockaddr_in saddr
;
3004 if (parse_host_port(&saddr
, host_str
) < 0)
3007 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3012 socket_set_nonblock(fd
);
3016 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3018 err
= socket_error();
3019 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3020 } else if (err
== EINPROGRESS
) {
3032 s
= net_socket_fd_init(vlan
, fd
, connected
);
3035 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3036 "socket: connect to %s:%d",
3037 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3041 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
3045 struct sockaddr_in saddr
;
3047 if (parse_host_port(&saddr
, host_str
) < 0)
3051 fd
= net_socket_mcast_create(&saddr
);
3055 s
= net_socket_fd_init(vlan
, fd
, 0);
3059 s
->dgram_dst
= saddr
;
3061 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3062 "socket: mcast=%s:%d",
3063 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3068 static int get_param_value(char *buf
, int buf_size
,
3069 const char *tag
, const char *str
)
3078 while (*p
!= '\0' && *p
!= '=') {
3079 if ((q
- option
) < sizeof(option
) - 1)
3087 if (!strcmp(tag
, option
)) {
3089 while (*p
!= '\0' && *p
!= ',') {
3090 if ((q
- buf
) < buf_size
- 1)
3097 while (*p
!= '\0' && *p
!= ',') {
3108 int net_client_init(const char *str
)
3119 while (*p
!= '\0' && *p
!= ',') {
3120 if ((q
- device
) < sizeof(device
) - 1)
3128 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
3129 vlan_id
= strtol(buf
, NULL
, 0);
3131 vlan
= qemu_find_vlan(vlan_id
);
3133 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
3136 if (!strcmp(device
, "nic")) {
3140 if (nb_nics
>= MAX_NICS
) {
3141 fprintf(stderr
, "Too Many NICs\n");
3144 nd
= &nd_table
[nb_nics
];
3145 macaddr
= nd
->macaddr
;
3151 macaddr
[5] = 0x56 + nb_nics
;
3153 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
3154 if (parse_macaddr(macaddr
, buf
) < 0) {
3155 fprintf(stderr
, "invalid syntax for ethernet address\n");
3159 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
3160 nd
->model
= strdup(buf
);
3166 if (!strcmp(device
, "none")) {
3167 /* does nothing. It is needed to signal that no network cards
3172 if (!strcmp(device
, "user")) {
3173 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
3174 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
3176 ret
= net_slirp_init(vlan
);
3180 if (!strcmp(device
, "tap")) {
3182 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
3183 fprintf(stderr
, "tap: no interface name\n");
3186 ret
= tap_win32_init(vlan
, ifname
);
3189 if (!strcmp(device
, "tap")) {
3191 char setup_script
[1024];
3193 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3194 fd
= strtol(buf
, NULL
, 0);
3196 if (net_tap_fd_init(vlan
, fd
))
3199 get_param_value(ifname
, sizeof(ifname
), "ifname", p
);
3200 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
3201 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
3203 ret
= net_tap_init(vlan
, ifname
, setup_script
);
3207 if (!strcmp(device
, "socket")) {
3208 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3210 fd
= strtol(buf
, NULL
, 0);
3212 if (net_socket_fd_init(vlan
, fd
, 1))
3214 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
3215 ret
= net_socket_listen_init(vlan
, buf
);
3216 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
3217 ret
= net_socket_connect_init(vlan
, buf
);
3218 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
3219 ret
= net_socket_mcast_init(vlan
, buf
);
3221 fprintf(stderr
, "Unknown socket options: %s\n", p
);
3226 fprintf(stderr
, "Unknown network device: %s\n", device
);
3230 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
3236 void do_info_network(void)
3239 VLANClientState
*vc
;
3241 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3242 term_printf("VLAN %d devices:\n", vlan
->id
);
3243 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3244 term_printf(" %s\n", vc
->info_str
);
3248 /***********************************************************/
3251 static int usb_device_add(const char *devname
)
3259 for(i
= 0;i
< MAX_VM_USB_PORTS
; i
++) {
3260 if (!vm_usb_ports
[i
]->dev
)
3263 if (i
== MAX_VM_USB_PORTS
)
3266 if (strstart(devname
, "host:", &p
)) {
3267 dev
= usb_host_device_open(p
);
3270 } else if (!strcmp(devname
, "mouse")) {
3271 dev
= usb_mouse_init();
3274 } else if (!strcmp(devname
, "tablet")) {
3275 dev
= usb_tablet_init();
3281 usb_attach(vm_usb_ports
[i
], dev
);
3285 static int usb_device_del(const char *devname
)
3288 int bus_num
, addr
, i
;
3294 p
= strchr(devname
, '.');
3297 bus_num
= strtoul(devname
, NULL
, 0);
3298 addr
= strtoul(p
+ 1, NULL
, 0);
3301 for(i
= 0;i
< MAX_VM_USB_PORTS
; i
++) {
3302 dev
= vm_usb_ports
[i
]->dev
;
3303 if (dev
&& dev
->addr
== addr
)
3306 if (i
== MAX_VM_USB_PORTS
)
3308 usb_attach(vm_usb_ports
[i
], NULL
);
3312 void do_usb_add(const char *devname
)
3315 ret
= usb_device_add(devname
);
3317 term_printf("Could not add USB device '%s'\n", devname
);
3320 void do_usb_del(const char *devname
)
3323 ret
= usb_device_del(devname
);
3325 term_printf("Could not remove USB device '%s'\n", devname
);
3332 const char *speed_str
;
3335 term_printf("USB support not enabled\n");
3339 for(i
= 0; i
< MAX_VM_USB_PORTS
; i
++) {
3340 dev
= vm_usb_ports
[i
]->dev
;
3342 term_printf("Hub port %d:\n", i
);
3343 switch(dev
->speed
) {
3347 case USB_SPEED_FULL
:
3350 case USB_SPEED_HIGH
:
3357 term_printf(" Device %d.%d, speed %s Mb/s\n",
3358 0, dev
->addr
, speed_str
);
3363 /***********************************************************/
3366 static char *pid_filename
;
3368 /* Remove PID file. Called on normal exit */
3370 static void remove_pidfile(void)
3372 unlink (pid_filename
);
3375 static void create_pidfile(const char *filename
)
3377 struct stat pidstat
;
3380 /* Try to write our PID to the named file */
3381 if (stat(filename
, &pidstat
) < 0) {
3382 if (errno
== ENOENT
) {
3383 if ((f
= fopen (filename
, "w")) == NULL
) {
3384 perror("Opening pidfile");
3387 fprintf(f
, "%d\n", getpid());
3389 pid_filename
= qemu_strdup(filename
);
3390 if (!pid_filename
) {
3391 fprintf(stderr
, "Could not save PID filename");
3394 atexit(remove_pidfile
);
3397 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
3403 /***********************************************************/
3406 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
3410 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
3414 static void dumb_refresh(DisplayState
*ds
)
3419 void dumb_display_init(DisplayState
*ds
)
3424 ds
->dpy_update
= dumb_update
;
3425 ds
->dpy_resize
= dumb_resize
;
3426 ds
->dpy_refresh
= dumb_refresh
;
3429 #if !defined(CONFIG_SOFTMMU)
3430 /***********************************************************/
3431 /* cpu signal handler */
3432 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
3435 if (cpu_signal_handler(host_signum
, info
, puc
))
3437 if (stdio_nb_clients
> 0)
3443 /***********************************************************/
3446 #define MAX_IO_HANDLERS 64
3448 typedef struct IOHandlerRecord
{
3450 IOCanRWHandler
*fd_read_poll
;
3452 IOHandler
*fd_write
;
3454 /* temporary data */
3456 struct IOHandlerRecord
*next
;
3459 static IOHandlerRecord
*first_io_handler
;
3461 /* XXX: fd_read_poll should be suppressed, but an API change is
3462 necessary in the character devices to suppress fd_can_read(). */
3463 int qemu_set_fd_handler2(int fd
,
3464 IOCanRWHandler
*fd_read_poll
,
3466 IOHandler
*fd_write
,
3469 IOHandlerRecord
**pioh
, *ioh
;
3471 if (!fd_read
&& !fd_write
) {
3472 pioh
= &first_io_handler
;
3477 if (ioh
->fd
== fd
) {
3485 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3489 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
3492 ioh
->next
= first_io_handler
;
3493 first_io_handler
= ioh
;
3496 ioh
->fd_read_poll
= fd_read_poll
;
3497 ioh
->fd_read
= fd_read
;
3498 ioh
->fd_write
= fd_write
;
3499 ioh
->opaque
= opaque
;
3504 int qemu_set_fd_handler(int fd
,
3506 IOHandler
*fd_write
,
3509 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
3512 /***********************************************************/
3513 /* Polling handling */
3515 typedef struct PollingEntry
{
3518 struct PollingEntry
*next
;
3521 static PollingEntry
*first_polling_entry
;
3523 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
3525 PollingEntry
**ppe
, *pe
;
3526 pe
= qemu_mallocz(sizeof(PollingEntry
));
3530 pe
->opaque
= opaque
;
3531 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
3536 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3538 PollingEntry
**ppe
, *pe
;
3539 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3541 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3549 /***********************************************************/
3550 /* savevm/loadvm support */
3552 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
3554 fwrite(buf
, 1, size
, f
);
3557 void qemu_put_byte(QEMUFile
*f
, int v
)
3562 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
3564 qemu_put_byte(f
, v
>> 8);
3565 qemu_put_byte(f
, v
);
3568 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
3570 qemu_put_byte(f
, v
>> 24);
3571 qemu_put_byte(f
, v
>> 16);
3572 qemu_put_byte(f
, v
>> 8);
3573 qemu_put_byte(f
, v
);
3576 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
3578 qemu_put_be32(f
, v
>> 32);
3579 qemu_put_be32(f
, v
);
3582 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
3584 return fread(buf
, 1, size
, f
);
3587 int qemu_get_byte(QEMUFile
*f
)
3597 unsigned int qemu_get_be16(QEMUFile
*f
)
3600 v
= qemu_get_byte(f
) << 8;
3601 v
|= qemu_get_byte(f
);
3605 unsigned int qemu_get_be32(QEMUFile
*f
)
3608 v
= qemu_get_byte(f
) << 24;
3609 v
|= qemu_get_byte(f
) << 16;
3610 v
|= qemu_get_byte(f
) << 8;
3611 v
|= qemu_get_byte(f
);
3615 uint64_t qemu_get_be64(QEMUFile
*f
)
3618 v
= (uint64_t)qemu_get_be32(f
) << 32;
3619 v
|= qemu_get_be32(f
);
3623 int64_t qemu_ftell(QEMUFile
*f
)
3628 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
3630 if (fseek(f
, pos
, whence
) < 0)
3635 typedef struct SaveStateEntry
{
3639 SaveStateHandler
*save_state
;
3640 LoadStateHandler
*load_state
;
3642 struct SaveStateEntry
*next
;
3645 static SaveStateEntry
*first_se
;
3647 int register_savevm(const char *idstr
,
3650 SaveStateHandler
*save_state
,
3651 LoadStateHandler
*load_state
,
3654 SaveStateEntry
*se
, **pse
;
3656 se
= qemu_malloc(sizeof(SaveStateEntry
));
3659 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
3660 se
->instance_id
= instance_id
;
3661 se
->version_id
= version_id
;
3662 se
->save_state
= save_state
;
3663 se
->load_state
= load_state
;
3664 se
->opaque
= opaque
;
3667 /* add at the end of list */
3669 while (*pse
!= NULL
)
3670 pse
= &(*pse
)->next
;
3675 #define QEMU_VM_FILE_MAGIC 0x5145564d
3676 #define QEMU_VM_FILE_VERSION 0x00000001
3678 int qemu_savevm(const char *filename
)
3682 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
3684 saved_vm_running
= vm_running
;
3687 f
= fopen(filename
, "wb");
3693 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
3694 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
3696 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3698 len
= strlen(se
->idstr
);
3699 qemu_put_byte(f
, len
);
3700 qemu_put_buffer(f
, se
->idstr
, len
);
3702 qemu_put_be32(f
, se
->instance_id
);
3703 qemu_put_be32(f
, se
->version_id
);
3705 /* record size: filled later */
3707 qemu_put_be32(f
, 0);
3709 se
->save_state(f
, se
->opaque
);
3711 /* fill record size */
3713 len
= ftell(f
) - len_pos
- 4;
3714 fseek(f
, len_pos
, SEEK_SET
);
3715 qemu_put_be32(f
, len
);
3716 fseek(f
, cur_pos
, SEEK_SET
);
3722 if (saved_vm_running
)
3727 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
3731 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3732 if (!strcmp(se
->idstr
, idstr
) &&
3733 instance_id
== se
->instance_id
)
3739 int qemu_loadvm(const char *filename
)
3743 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
3744 int saved_vm_running
;
3748 saved_vm_running
= vm_running
;
3751 f
= fopen(filename
, "rb");
3757 v
= qemu_get_be32(f
);
3758 if (v
!= QEMU_VM_FILE_MAGIC
)
3760 v
= qemu_get_be32(f
);
3761 if (v
!= QEMU_VM_FILE_VERSION
) {
3768 len
= qemu_get_byte(f
);
3771 qemu_get_buffer(f
, idstr
, len
);
3773 instance_id
= qemu_get_be32(f
);
3774 version_id
= qemu_get_be32(f
);
3775 record_len
= qemu_get_be32(f
);
3777 printf("idstr=%s instance=0x%x version=%d len=%d\n",
3778 idstr
, instance_id
, version_id
, record_len
);
3781 se
= find_se(idstr
, instance_id
);
3783 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
3784 instance_id
, idstr
);
3786 ret
= se
->load_state(f
, se
->opaque
, version_id
);
3788 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
3789 instance_id
, idstr
);
3792 /* always seek to exact end of record */
3793 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
3798 if (saved_vm_running
)
3803 /***********************************************************/
3804 /* cpu save/restore */
3806 #if defined(TARGET_I386)
3808 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
3810 qemu_put_be32(f
, dt
->selector
);
3811 qemu_put_betl(f
, dt
->base
);
3812 qemu_put_be32(f
, dt
->limit
);
3813 qemu_put_be32(f
, dt
->flags
);
3816 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
3818 dt
->selector
= qemu_get_be32(f
);
3819 dt
->base
= qemu_get_betl(f
);
3820 dt
->limit
= qemu_get_be32(f
);
3821 dt
->flags
= qemu_get_be32(f
);
3824 void cpu_save(QEMUFile
*f
, void *opaque
)
3826 CPUState
*env
= opaque
;
3827 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
3831 for(i
= 0; i
< CPU_NB_REGS
; i
++)
3832 qemu_put_betls(f
, &env
->regs
[i
]);
3833 qemu_put_betls(f
, &env
->eip
);
3834 qemu_put_betls(f
, &env
->eflags
);
3835 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
3836 qemu_put_be32s(f
, &hflags
);
3840 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
3842 for(i
= 0; i
< 8; i
++) {
3843 fptag
|= ((!env
->fptags
[i
]) << i
);
3846 qemu_put_be16s(f
, &fpuc
);
3847 qemu_put_be16s(f
, &fpus
);
3848 qemu_put_be16s(f
, &fptag
);
3850 #ifdef USE_X86LDOUBLE
3855 qemu_put_be16s(f
, &fpregs_format
);
3857 for(i
= 0; i
< 8; i
++) {
3858 #ifdef USE_X86LDOUBLE
3862 /* we save the real CPU data (in case of MMX usage only 'mant'
3863 contains the MMX register */
3864 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
3865 qemu_put_be64(f
, mant
);
3866 qemu_put_be16(f
, exp
);
3869 /* if we use doubles for float emulation, we save the doubles to
3870 avoid losing information in case of MMX usage. It can give
3871 problems if the image is restored on a CPU where long
3872 doubles are used instead. */
3873 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
3877 for(i
= 0; i
< 6; i
++)
3878 cpu_put_seg(f
, &env
->segs
[i
]);
3879 cpu_put_seg(f
, &env
->ldt
);
3880 cpu_put_seg(f
, &env
->tr
);
3881 cpu_put_seg(f
, &env
->gdt
);
3882 cpu_put_seg(f
, &env
->idt
);
3884 qemu_put_be32s(f
, &env
->sysenter_cs
);
3885 qemu_put_be32s(f
, &env
->sysenter_esp
);
3886 qemu_put_be32s(f
, &env
->sysenter_eip
);
3888 qemu_put_betls(f
, &env
->cr
[0]);
3889 qemu_put_betls(f
, &env
->cr
[2]);
3890 qemu_put_betls(f
, &env
->cr
[3]);
3891 qemu_put_betls(f
, &env
->cr
[4]);
3893 for(i
= 0; i
< 8; i
++)
3894 qemu_put_betls(f
, &env
->dr
[i
]);
3897 qemu_put_be32s(f
, &env
->a20_mask
);
3900 qemu_put_be32s(f
, &env
->mxcsr
);
3901 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
3902 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
3903 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
3906 #ifdef TARGET_X86_64
3907 qemu_put_be64s(f
, &env
->efer
);
3908 qemu_put_be64s(f
, &env
->star
);
3909 qemu_put_be64s(f
, &env
->lstar
);
3910 qemu_put_be64s(f
, &env
->cstar
);
3911 qemu_put_be64s(f
, &env
->fmask
);
3912 qemu_put_be64s(f
, &env
->kernelgsbase
);
3916 #ifdef USE_X86LDOUBLE
3917 /* XXX: add that in a FPU generic layer */
3918 union x86_longdouble
{
3923 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
3924 #define EXPBIAS1 1023
3925 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
3926 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
3928 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
3932 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
3933 /* exponent + sign */
3934 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
3935 e
|= SIGND1(temp
) >> 16;
3940 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
3942 CPUState
*env
= opaque
;
3945 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
3947 if (version_id
!= 3)
3949 for(i
= 0; i
< CPU_NB_REGS
; i
++)
3950 qemu_get_betls(f
, &env
->regs
[i
]);
3951 qemu_get_betls(f
, &env
->eip
);
3952 qemu_get_betls(f
, &env
->eflags
);
3953 qemu_get_be32s(f
, &hflags
);
3955 qemu_get_be16s(f
, &fpuc
);
3956 qemu_get_be16s(f
, &fpus
);
3957 qemu_get_be16s(f
, &fptag
);
3958 qemu_get_be16s(f
, &fpregs_format
);
3960 /* NOTE: we cannot always restore the FPU state if the image come
3961 from a host with a different 'USE_X86LDOUBLE' define. We guess
3962 if we are in an MMX state to restore correctly in that case. */
3963 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
3964 for(i
= 0; i
< 8; i
++) {
3968 switch(fpregs_format
) {
3970 mant
= qemu_get_be64(f
);
3971 exp
= qemu_get_be16(f
);
3972 #ifdef USE_X86LDOUBLE
3973 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
3975 /* difficult case */
3977 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
3979 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
3983 mant
= qemu_get_be64(f
);
3984 #ifdef USE_X86LDOUBLE
3986 union x86_longdouble
*p
;
3987 /* difficult case */
3988 p
= (void *)&env
->fpregs
[i
];
3993 fp64_to_fp80(p
, mant
);
3997 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
4006 /* XXX: restore FPU round state */
4007 env
->fpstt
= (fpus
>> 11) & 7;
4008 env
->fpus
= fpus
& ~0x3800;
4010 for(i
= 0; i
< 8; i
++) {
4011 env
->fptags
[i
] = (fptag
>> i
) & 1;
4014 for(i
= 0; i
< 6; i
++)
4015 cpu_get_seg(f
, &env
->segs
[i
]);
4016 cpu_get_seg(f
, &env
->ldt
);
4017 cpu_get_seg(f
, &env
->tr
);
4018 cpu_get_seg(f
, &env
->gdt
);
4019 cpu_get_seg(f
, &env
->idt
);
4021 qemu_get_be32s(f
, &env
->sysenter_cs
);
4022 qemu_get_be32s(f
, &env
->sysenter_esp
);
4023 qemu_get_be32s(f
, &env
->sysenter_eip
);
4025 qemu_get_betls(f
, &env
->cr
[0]);
4026 qemu_get_betls(f
, &env
->cr
[2]);
4027 qemu_get_betls(f
, &env
->cr
[3]);
4028 qemu_get_betls(f
, &env
->cr
[4]);
4030 for(i
= 0; i
< 8; i
++)
4031 qemu_get_betls(f
, &env
->dr
[i
]);
4034 qemu_get_be32s(f
, &env
->a20_mask
);
4036 qemu_get_be32s(f
, &env
->mxcsr
);
4037 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
4038 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
4039 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
4042 #ifdef TARGET_X86_64
4043 qemu_get_be64s(f
, &env
->efer
);
4044 qemu_get_be64s(f
, &env
->star
);
4045 qemu_get_be64s(f
, &env
->lstar
);
4046 qemu_get_be64s(f
, &env
->cstar
);
4047 qemu_get_be64s(f
, &env
->fmask
);
4048 qemu_get_be64s(f
, &env
->kernelgsbase
);
4051 /* XXX: compute hflags from scratch, except for CPL and IIF */
4052 env
->hflags
= hflags
;
4057 #elif defined(TARGET_PPC)
4058 void cpu_save(QEMUFile
*f
, void *opaque
)
4062 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4067 #elif defined(TARGET_MIPS)
4068 void cpu_save(QEMUFile
*f
, void *opaque
)
4072 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4077 #elif defined(TARGET_SPARC)
4078 void cpu_save(QEMUFile
*f
, void *opaque
)
4080 CPUState
*env
= opaque
;
4084 for(i
= 0; i
< 8; i
++)
4085 qemu_put_betls(f
, &env
->gregs
[i
]);
4086 for(i
= 0; i
< NWINDOWS
* 16; i
++)
4087 qemu_put_betls(f
, &env
->regbase
[i
]);
4090 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
4096 qemu_put_betl(f
, u
.i
);
4099 qemu_put_betls(f
, &env
->pc
);
4100 qemu_put_betls(f
, &env
->npc
);
4101 qemu_put_betls(f
, &env
->y
);
4103 qemu_put_be32(f
, tmp
);
4104 qemu_put_betls(f
, &env
->fsr
);
4105 qemu_put_betls(f
, &env
->tbr
);
4106 #ifndef TARGET_SPARC64
4107 qemu_put_be32s(f
, &env
->wim
);
4109 for(i
= 0; i
< 16; i
++)
4110 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
4114 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4116 CPUState
*env
= opaque
;
4120 for(i
= 0; i
< 8; i
++)
4121 qemu_get_betls(f
, &env
->gregs
[i
]);
4122 for(i
= 0; i
< NWINDOWS
* 16; i
++)
4123 qemu_get_betls(f
, &env
->regbase
[i
]);
4126 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
4131 u
.i
= qemu_get_betl(f
);
4135 qemu_get_betls(f
, &env
->pc
);
4136 qemu_get_betls(f
, &env
->npc
);
4137 qemu_get_betls(f
, &env
->y
);
4138 tmp
= qemu_get_be32(f
);
4139 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
4140 correctly updated */
4142 qemu_get_betls(f
, &env
->fsr
);
4143 qemu_get_betls(f
, &env
->tbr
);
4144 #ifndef TARGET_SPARC64
4145 qemu_get_be32s(f
, &env
->wim
);
4147 for(i
= 0; i
< 16; i
++)
4148 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
4154 #elif defined(TARGET_ARM)
4156 /* ??? Need to implement these. */
4157 void cpu_save(QEMUFile
*f
, void *opaque
)
4161 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4168 #warning No CPU save/restore functions
4172 /***********************************************************/
4173 /* ram save/restore */
4175 /* we just avoid storing empty pages */
4176 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
4181 for(i
= 1; i
< len
; i
++) {
4185 qemu_put_byte(f
, 1);
4186 qemu_put_byte(f
, v
);
4189 qemu_put_byte(f
, 0);
4190 qemu_put_buffer(f
, buf
, len
);
4193 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
4197 v
= qemu_get_byte(f
);
4200 if (qemu_get_buffer(f
, buf
, len
) != len
)
4204 v
= qemu_get_byte(f
);
4205 memset(buf
, v
, len
);
4213 static void ram_save(QEMUFile
*f
, void *opaque
)
4216 qemu_put_be32(f
, phys_ram_size
);
4217 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
4218 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
4222 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
4226 if (version_id
!= 1)
4228 if (qemu_get_be32(f
) != phys_ram_size
)
4230 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
4231 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
4238 /***********************************************************/
4239 /* machine registration */
4241 QEMUMachine
*first_machine
= NULL
;
4243 int qemu_register_machine(QEMUMachine
*m
)
4246 pm
= &first_machine
;
4254 QEMUMachine
*find_machine(const char *name
)
4258 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4259 if (!strcmp(m
->name
, name
))
4265 /***********************************************************/
4266 /* main execution loop */
4268 void gui_update(void *opaque
)
4270 display_state
.dpy_refresh(&display_state
);
4271 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
4274 struct vm_change_state_entry
{
4275 VMChangeStateHandler
*cb
;
4277 LIST_ENTRY (vm_change_state_entry
) entries
;
4280 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
4282 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
4285 VMChangeStateEntry
*e
;
4287 e
= qemu_mallocz(sizeof (*e
));
4293 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
4297 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
4299 LIST_REMOVE (e
, entries
);
4303 static void vm_state_notify(int running
)
4305 VMChangeStateEntry
*e
;
4307 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
4308 e
->cb(e
->opaque
, running
);
4312 /* XXX: support several handlers */
4313 static VMStopHandler
*vm_stop_cb
;
4314 static void *vm_stop_opaque
;
4316 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4319 vm_stop_opaque
= opaque
;
4323 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4337 void vm_stop(int reason
)
4340 cpu_disable_ticks();
4344 vm_stop_cb(vm_stop_opaque
, reason
);
4351 /* reset/shutdown handler */
4353 typedef struct QEMUResetEntry
{
4354 QEMUResetHandler
*func
;
4356 struct QEMUResetEntry
*next
;
4359 static QEMUResetEntry
*first_reset_entry
;
4360 static int reset_requested
;
4361 static int shutdown_requested
;
4362 static int powerdown_requested
;
4364 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
4366 QEMUResetEntry
**pre
, *re
;
4368 pre
= &first_reset_entry
;
4369 while (*pre
!= NULL
)
4370 pre
= &(*pre
)->next
;
4371 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
4373 re
->opaque
= opaque
;
4378 void qemu_system_reset(void)
4382 /* reset all devices */
4383 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
4384 re
->func(re
->opaque
);
4388 void qemu_system_reset_request(void)
4390 reset_requested
= 1;
4392 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4395 void qemu_system_shutdown_request(void)
4397 shutdown_requested
= 1;
4399 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4402 void qemu_system_powerdown_request(void)
4404 powerdown_requested
= 1;
4406 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4409 void main_loop_wait(int timeout
)
4411 IOHandlerRecord
*ioh
, *ioh_next
;
4418 /* XXX: need to suppress polling by better using win32 events */
4420 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4421 ret
|= pe
->func(pe
->opaque
);
4424 if (ret
== 0 && timeout
> 0) {
4428 hEvents
[0] = host_alarm
;
4429 ret
= WaitForMultipleObjects(1, hEvents
, FALSE
, timeout
);
4431 case WAIT_OBJECT_0
+ 0:
4436 err
= GetLastError();
4437 fprintf(stderr
, "Wait error %d %d\n", ret
, err
);
4442 /* poll any events */
4443 /* XXX: separate device handlers from system ones */
4447 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4449 (!ioh
->fd_read_poll
||
4450 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4451 FD_SET(ioh
->fd
, &rfds
);
4455 if (ioh
->fd_write
) {
4456 FD_SET(ioh
->fd
, &wfds
);
4466 tv
.tv_usec
= timeout
* 1000;
4468 ret
= select(nfds
+ 1, &rfds
, &wfds
, NULL
, &tv
);
4470 /* XXX: better handling of removal */
4471 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
4472 ioh_next
= ioh
->next
;
4473 if (FD_ISSET(ioh
->fd
, &rfds
)) {
4474 ioh
->fd_read(ioh
->opaque
);
4476 if (FD_ISSET(ioh
->fd
, &wfds
)) {
4477 ioh
->fd_write(ioh
->opaque
);
4485 #if defined(CONFIG_SLIRP)
4486 /* XXX: merge with the previous select() */
4488 fd_set rfds
, wfds
, xfds
;
4496 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4499 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4501 slirp_select_poll(&rfds
, &wfds
, &xfds
);
4507 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4508 qemu_get_clock(vm_clock
));
4509 /* run dma transfers, if any */
4513 /* real time timers */
4514 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4515 qemu_get_clock(rt_clock
));
4518 static CPUState
*cur_cpu
;
4523 #ifdef CONFIG_PROFILER
4528 cur_cpu
= first_cpu
;
4535 env
= env
->next_cpu
;
4538 #ifdef CONFIG_PROFILER
4539 ti
= profile_getclock();
4541 ret
= cpu_exec(env
);
4542 #ifdef CONFIG_PROFILER
4543 qemu_time
+= profile_getclock() - ti
;
4545 if (ret
!= EXCP_HALTED
)
4547 /* all CPUs are halted ? */
4548 if (env
== cur_cpu
) {
4555 if (shutdown_requested
) {
4556 ret
= EXCP_INTERRUPT
;
4559 if (reset_requested
) {
4560 reset_requested
= 0;
4561 qemu_system_reset();
4562 ret
= EXCP_INTERRUPT
;
4564 if (powerdown_requested
) {
4565 powerdown_requested
= 0;
4566 qemu_system_powerdown();
4567 ret
= EXCP_INTERRUPT
;
4569 if (ret
== EXCP_DEBUG
) {
4570 vm_stop(EXCP_DEBUG
);
4572 /* if hlt instruction, we wait until the next IRQ */
4573 /* XXX: use timeout computed from timers */
4574 if (ret
== EXCP_HLT
)
4581 #ifdef CONFIG_PROFILER
4582 ti
= profile_getclock();
4584 main_loop_wait(timeout
);
4585 #ifdef CONFIG_PROFILER
4586 dev_time
+= profile_getclock() - ti
;
4589 cpu_disable_ticks();
4595 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2005 Fabrice Bellard\n"
4596 "usage: %s [options] [disk_image]\n"
4598 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4600 "Standard options:\n"
4601 "-M machine select emulated machine (-M ? for list)\n"
4602 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
4603 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
4604 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
4605 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
4606 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
4607 "-snapshot write to temporary files instead of disk image files\n"
4608 "-m megs set virtual RAM size to megs MB [default=%d]\n"
4609 "-smp n set the number of CPUs to 'n' [default=1]\n"
4610 "-nographic disable graphical output and redirect serial I/Os to console\n"
4612 "-k language use keyboard layout (for example \"fr\" for French)\n"
4615 "-audio-help print list of audio drivers and their options\n"
4616 "-soundhw c1,... enable audio support\n"
4617 " and only specified sound cards (comma separated list)\n"
4618 " use -soundhw ? to get the list of supported cards\n"
4619 " use -soundhw all to enable all of them\n"
4621 "-localtime set the real time clock to local time [default=utc]\n"
4622 "-full-screen start in full screen\n"
4624 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4626 "-usb enable the USB driver (will be the default soon)\n"
4627 "-usbdevice name add the host or guest USB device 'name'\n"
4628 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4629 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4632 "Network options:\n"
4633 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
4634 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4636 "-net user[,vlan=n][,hostname=host]\n"
4637 " connect the user mode network stack to VLAN 'n' and send\n"
4638 " hostname 'host' to DHCP clients\n"
4641 "-net tap[,vlan=n],ifname=name\n"
4642 " connect the host TAP network interface to VLAN 'n'\n"
4644 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
4645 " connect the host TAP network interface to VLAN 'n' and use\n"
4646 " the network script 'file' (default=%s);\n"
4647 " use 'fd=h' to connect to an already opened TAP interface\n"
4649 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4650 " connect the vlan 'n' to another VLAN using a socket connection\n"
4651 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
4652 " connect the vlan 'n' to multicast maddr and port\n"
4653 "-net none use it alone to have zero network devices; if no -net option\n"
4654 " is provided, the default is '-net nic -net user'\n"
4657 "-tftp prefix allow tftp access to files starting with prefix [-net user]\n"
4659 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4661 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4662 " redirect TCP or UDP connections from host to guest [-net user]\n"
4665 "Linux boot specific:\n"
4666 "-kernel bzImage use 'bzImage' as kernel image\n"
4667 "-append cmdline use 'cmdline' as kernel command line\n"
4668 "-initrd file use 'file' as initial ram disk\n"
4670 "Debug/Expert options:\n"
4671 "-monitor dev redirect the monitor to char device 'dev'\n"
4672 "-serial dev redirect the serial port to char device 'dev'\n"
4673 "-parallel dev redirect the parallel port to char device 'dev'\n"
4674 "-pidfile file Write PID to 'file'\n"
4675 "-S freeze CPU at startup (use 'c' to start execution)\n"
4676 "-s wait gdb connection to port %d\n"
4677 "-p port change gdb connection port\n"
4678 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4679 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
4680 " translation (t=none or lba) (usually qemu can guess them)\n"
4681 "-L path set the directory for the BIOS and VGA BIOS\n"
4683 "-no-kqemu disable KQEMU kernel module usage\n"
4685 #ifdef USE_CODE_COPY
4686 "-no-code-copy disable code copy acceleration\n"
4689 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
4690 " (default is CL-GD5446 PCI VGA)\n"
4692 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
4693 "-vnc display start a VNC server on display\n"
4695 "During emulation, the following keys are useful:\n"
4696 "ctrl-alt-f toggle full screen\n"
4697 "ctrl-alt-n switch to virtual console 'n'\n"
4698 "ctrl-alt toggle mouse and keyboard grab\n"
4700 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4702 #ifdef CONFIG_SOFTMMU
4709 DEFAULT_NETWORK_SCRIPT
,
4711 DEFAULT_GDBSTUB_PORT
,
4713 #ifndef CONFIG_SOFTMMU
4715 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
4716 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
4722 #define HAS_ARG 0x0001
4736 QEMU_OPTION_snapshot
,
4738 QEMU_OPTION_nographic
,
4740 QEMU_OPTION_audio_help
,
4741 QEMU_OPTION_soundhw
,
4759 QEMU_OPTION_no_code_copy
,
4761 QEMU_OPTION_localtime
,
4762 QEMU_OPTION_cirrusvga
,
4764 QEMU_OPTION_std_vga
,
4765 QEMU_OPTION_monitor
,
4767 QEMU_OPTION_parallel
,
4769 QEMU_OPTION_full_screen
,
4770 QEMU_OPTION_pidfile
,
4771 QEMU_OPTION_no_kqemu
,
4772 QEMU_OPTION_kernel_kqemu
,
4773 QEMU_OPTION_win2k_hack
,
4775 QEMU_OPTION_usbdevice
,
4780 typedef struct QEMUOption
{
4786 const QEMUOption qemu_options
[] = {
4787 { "h", 0, QEMU_OPTION_h
},
4789 { "M", HAS_ARG
, QEMU_OPTION_M
},
4790 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4791 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4792 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4793 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4794 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4795 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4796 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4797 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4798 { "snapshot", 0, QEMU_OPTION_snapshot
},
4799 { "m", HAS_ARG
, QEMU_OPTION_m
},
4800 { "nographic", 0, QEMU_OPTION_nographic
},
4801 { "k", HAS_ARG
, QEMU_OPTION_k
},
4803 { "audio-help", 0, QEMU_OPTION_audio_help
},
4804 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4807 { "net", HAS_ARG
, QEMU_OPTION_net
},
4809 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4811 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4813 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4816 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4817 { "append", HAS_ARG
, QEMU_OPTION_append
},
4818 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4820 { "S", 0, QEMU_OPTION_S
},
4821 { "s", 0, QEMU_OPTION_s
},
4822 { "p", HAS_ARG
, QEMU_OPTION_p
},
4823 { "d", HAS_ARG
, QEMU_OPTION_d
},
4824 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4825 { "L", HAS_ARG
, QEMU_OPTION_L
},
4826 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
4828 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4829 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4831 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4832 { "g", 1, QEMU_OPTION_g
},
4834 { "localtime", 0, QEMU_OPTION_localtime
},
4835 { "std-vga", 0, QEMU_OPTION_std_vga
},
4836 { "monitor", 1, QEMU_OPTION_monitor
},
4837 { "serial", 1, QEMU_OPTION_serial
},
4838 { "parallel", 1, QEMU_OPTION_parallel
},
4839 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4840 { "full-screen", 0, QEMU_OPTION_full_screen
},
4841 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4842 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4843 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4844 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4845 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4847 /* temporary options */
4848 { "usb", 0, QEMU_OPTION_usb
},
4849 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
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");
5459 linux_boot
= (kernel_filename
!= NULL
);
5462 hd_filename
[0] == '\0' &&
5463 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
5464 fd_filename
[0] == '\0')
5467 /* boot to cd by default if no hard disk */
5468 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
5469 if (fd_filename
[0] != '\0')
5475 #if !defined(CONFIG_SOFTMMU)
5476 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
5478 static uint8_t stdout_buf
[4096];
5479 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
5482 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5489 /* init network clients */
5490 if (nb_net_clients
== 0) {
5491 /* if no clients, we use a default config */
5492 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
5494 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
5499 for(i
= 0;i
< nb_net_clients
; i
++) {
5500 if (net_client_init(net_clients
[i
]) < 0)
5504 /* init the memory */
5505 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
5507 #ifdef CONFIG_SOFTMMU
5508 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5509 if (!phys_ram_base
) {
5510 fprintf(stderr
, "Could not allocate physical memory\n");
5514 /* as we must map the same page at several addresses, we must use
5519 tmpdir
= getenv("QEMU_TMPDIR");
5522 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
5523 if (mkstemp(phys_ram_file
) < 0) {
5524 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
5528 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
5529 if (phys_ram_fd
< 0) {
5530 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
5534 ftruncate(phys_ram_fd
, phys_ram_size
);
5535 unlink(phys_ram_file
);
5536 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
5538 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
5540 if (phys_ram_base
== MAP_FAILED
) {
5541 fprintf(stderr
, "Could not map physical memory\n");
5547 /* we always create the cdrom drive, even if no disk is there */
5549 if (cdrom_index
>= 0) {
5550 bs_table
[cdrom_index
] = bdrv_new("cdrom");
5551 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
5554 /* open the virtual block devices */
5555 for(i
= 0; i
< MAX_DISKS
; i
++) {
5556 if (hd_filename
[i
]) {
5559 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
5560 bs_table
[i
] = bdrv_new(buf
);
5562 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
5563 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
5567 if (i
== 0 && cyls
!= 0) {
5568 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
5569 bdrv_set_translation_hint(bs_table
[i
], translation
);
5574 /* we always create at least one floppy disk */
5575 fd_table
[0] = bdrv_new("fda");
5576 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
5578 for(i
= 0; i
< MAX_FD
; i
++) {
5579 if (fd_filename
[i
]) {
5582 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
5583 fd_table
[i
] = bdrv_new(buf
);
5584 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
5586 if (fd_filename
[i
] != '\0') {
5587 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
5588 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
5596 /* init USB devices */
5598 vm_usb_hub
= usb_hub_init(vm_usb_ports
, MAX_VM_USB_PORTS
);
5599 for(i
= 0; i
< usb_devices_index
; i
++) {
5600 if (usb_device_add(usb_devices
[i
]) < 0) {
5601 fprintf(stderr
, "Warning: could not add USB device %s\n",
5607 register_savevm("timer", 0, 1, timer_save
, timer_load
, NULL
);
5608 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
5611 cpu_calibrate_ticks();
5615 dumb_display_init(ds
);
5616 } else if (vnc_display
!= -1) {
5617 vnc_display_init(ds
, vnc_display
);
5619 #if defined(CONFIG_SDL)
5620 sdl_display_init(ds
, full_screen
);
5621 #elif defined(CONFIG_COCOA)
5622 cocoa_display_init(ds
, full_screen
);
5624 dumb_display_init(ds
);
5628 monitor_hd
= qemu_chr_open(monitor_device
);
5630 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5633 monitor_init(monitor_hd
, !nographic
);
5635 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5636 if (serial_devices
[i
][0] != '\0') {
5637 serial_hds
[i
] = qemu_chr_open(serial_devices
[i
]);
5638 if (!serial_hds
[i
]) {
5639 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5643 if (!strcmp(serial_devices
[i
], "vc"))
5644 qemu_chr_printf(serial_hds
[i
], "serial%d console\n", i
);
5648 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5649 if (parallel_devices
[i
][0] != '\0') {
5650 parallel_hds
[i
] = qemu_chr_open(parallel_devices
[i
]);
5651 if (!parallel_hds
[i
]) {
5652 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5653 parallel_devices
[i
]);
5656 if (!strcmp(parallel_devices
[i
], "vc"))
5657 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\n", i
);
5661 /* setup cpu signal handlers for MMU / self modifying code handling */
5662 #if !defined(CONFIG_SOFTMMU)
5664 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5667 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
5668 stk
.ss_sp
= signal_stack
;
5669 stk
.ss_size
= SIGNAL_STACK_SIZE
;
5672 if (sigaltstack(&stk
, NULL
) < 0) {
5673 perror("sigaltstack");
5679 struct sigaction act
;
5681 sigfillset(&act
.sa_mask
);
5682 act
.sa_flags
= SA_SIGINFO
;
5683 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5684 act
.sa_flags
|= SA_ONSTACK
;
5686 act
.sa_sigaction
= host_segv_handler
;
5687 sigaction(SIGSEGV
, &act
, NULL
);
5688 sigaction(SIGBUS
, &act
, NULL
);
5689 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5690 sigaction(SIGFPE
, &act
, NULL
);
5697 struct sigaction act
;
5698 sigfillset(&act
.sa_mask
);
5700 act
.sa_handler
= SIG_IGN
;
5701 sigaction(SIGPIPE
, &act
, NULL
);
5706 machine
->init(ram_size
, vga_ram_size
, boot_device
,
5707 ds
, fd_filename
, snapshot
,
5708 kernel_filename
, kernel_cmdline
, initrd_filename
);
5710 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
5711 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
5713 #ifdef CONFIG_GDBSTUB
5715 if (gdbserver_start(gdbstub_port
) < 0) {
5716 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
5720 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
5725 qemu_loadvm(loadvm
);
5728 /* XXX: simplify init */
5730 if (start_emulation
) {