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>
51 #include <linux/if_tun.h>
54 #include <linux/rtc.h>
55 #include <linux/ppdev.h>
59 #if defined(CONFIG_SLIRP)
65 #include <sys/timeb.h>
69 #define getopt_long_only getopt_long
70 #define memalign(align, size) malloc(size)
77 #endif /* CONFIG_SDL */
81 #define main qemu_main
82 #endif /* CONFIG_COCOA */
88 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
90 //#define DEBUG_UNUSED_IOPORT
91 //#define DEBUG_IOPORT
93 #if !defined(CONFIG_SOFTMMU)
94 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
96 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
100 #define DEFAULT_RAM_SIZE 144
102 #define DEFAULT_RAM_SIZE 128
105 #define GUI_REFRESH_INTERVAL 30
107 /* XXX: use a two level table to limit memory usage */
108 #define MAX_IOPORTS 65536
110 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
111 char phys_ram_file
[1024];
112 void *ioport_opaque
[MAX_IOPORTS
];
113 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
114 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
115 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
118 static DisplayState display_state
;
120 const char* keyboard_layout
= NULL
;
121 int64_t ticks_per_sec
;
122 int boot_device
= 'c';
124 int pit_min_timer_count
= 0;
126 NICInfo nd_table
[MAX_NICS
];
127 QEMUTimer
*gui_timer
;
130 int cirrus_vga_enabled
= 1;
132 int graphic_width
= 1024;
133 int graphic_height
= 768;
135 int graphic_width
= 800;
136 int graphic_height
= 600;
138 int graphic_depth
= 15;
140 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
141 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
143 int win2k_install_hack
= 0;
146 USBPort
*vm_usb_ports
[MAX_VM_USB_PORTS
];
147 USBDevice
*vm_usb_hub
;
148 static VLANState
*first_vlan
;
150 #if defined(TARGET_SPARC)
152 #elif defined(TARGET_I386)
158 /***********************************************************/
159 /* x86 ISA bus support */
161 target_phys_addr_t isa_mem_base
= 0;
164 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
166 #ifdef DEBUG_UNUSED_IOPORT
167 fprintf(stderr
, "inb: port=0x%04x\n", address
);
172 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
174 #ifdef DEBUG_UNUSED_IOPORT
175 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
179 /* default is to make two byte accesses */
180 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
183 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
184 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
185 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
189 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
191 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
192 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
193 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
196 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
198 #ifdef DEBUG_UNUSED_IOPORT
199 fprintf(stderr
, "inl: port=0x%04x\n", address
);
204 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
206 #ifdef DEBUG_UNUSED_IOPORT
207 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
211 void init_ioports(void)
215 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
216 ioport_read_table
[0][i
] = default_ioport_readb
;
217 ioport_write_table
[0][i
] = default_ioport_writeb
;
218 ioport_read_table
[1][i
] = default_ioport_readw
;
219 ioport_write_table
[1][i
] = default_ioport_writew
;
220 ioport_read_table
[2][i
] = default_ioport_readl
;
221 ioport_write_table
[2][i
] = default_ioport_writel
;
225 /* size is the word size in byte */
226 int register_ioport_read(int start
, int length
, int size
,
227 IOPortReadFunc
*func
, void *opaque
)
233 } else if (size
== 2) {
235 } else if (size
== 4) {
238 hw_error("register_ioport_read: invalid size");
241 for(i
= start
; i
< start
+ length
; i
+= size
) {
242 ioport_read_table
[bsize
][i
] = func
;
243 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
244 hw_error("register_ioport_read: invalid opaque");
245 ioport_opaque
[i
] = opaque
;
250 /* size is the word size in byte */
251 int register_ioport_write(int start
, int length
, int size
,
252 IOPortWriteFunc
*func
, void *opaque
)
258 } else if (size
== 2) {
260 } else if (size
== 4) {
263 hw_error("register_ioport_write: invalid size");
266 for(i
= start
; i
< start
+ length
; i
+= size
) {
267 ioport_write_table
[bsize
][i
] = func
;
268 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
269 hw_error("register_ioport_read: invalid opaque");
270 ioport_opaque
[i
] = opaque
;
275 void isa_unassign_ioport(int start
, int length
)
279 for(i
= start
; i
< start
+ length
; i
++) {
280 ioport_read_table
[0][i
] = default_ioport_readb
;
281 ioport_read_table
[1][i
] = default_ioport_readw
;
282 ioport_read_table
[2][i
] = default_ioport_readl
;
284 ioport_write_table
[0][i
] = default_ioport_writeb
;
285 ioport_write_table
[1][i
] = default_ioport_writew
;
286 ioport_write_table
[2][i
] = default_ioport_writel
;
290 /***********************************************************/
292 void pstrcpy(char *buf
, int buf_size
, const char *str
)
302 if (c
== 0 || q
>= buf
+ buf_size
- 1)
309 /* strcat and truncate. */
310 char *pstrcat(char *buf
, int buf_size
, const char *s
)
315 pstrcpy(buf
+ len
, buf_size
- len
, s
);
319 int strstart(const char *str
, const char *val
, const char **ptr
)
335 /* return the size or -1 if error */
336 int get_image_size(const char *filename
)
339 fd
= open(filename
, O_RDONLY
| O_BINARY
);
342 size
= lseek(fd
, 0, SEEK_END
);
347 /* return the size or -1 if error */
348 int load_image(const char *filename
, uint8_t *addr
)
351 fd
= open(filename
, O_RDONLY
| O_BINARY
);
354 size
= lseek(fd
, 0, SEEK_END
);
355 lseek(fd
, 0, SEEK_SET
);
356 if (read(fd
, addr
, size
) != size
) {
364 void cpu_outb(CPUState
*env
, int addr
, int val
)
367 if (loglevel
& CPU_LOG_IOPORT
)
368 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
370 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
373 env
->last_io_time
= cpu_get_time_fast();
377 void cpu_outw(CPUState
*env
, int addr
, int val
)
380 if (loglevel
& CPU_LOG_IOPORT
)
381 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
383 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
386 env
->last_io_time
= cpu_get_time_fast();
390 void cpu_outl(CPUState
*env
, int addr
, int val
)
393 if (loglevel
& CPU_LOG_IOPORT
)
394 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
396 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
399 env
->last_io_time
= cpu_get_time_fast();
403 int cpu_inb(CPUState
*env
, int addr
)
406 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
408 if (loglevel
& CPU_LOG_IOPORT
)
409 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
413 env
->last_io_time
= cpu_get_time_fast();
418 int cpu_inw(CPUState
*env
, int addr
)
421 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
423 if (loglevel
& CPU_LOG_IOPORT
)
424 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
428 env
->last_io_time
= cpu_get_time_fast();
433 int cpu_inl(CPUState
*env
, int addr
)
436 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
438 if (loglevel
& CPU_LOG_IOPORT
)
439 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
443 env
->last_io_time
= cpu_get_time_fast();
448 /***********************************************************/
449 void hw_error(const char *fmt
, ...)
455 fprintf(stderr
, "qemu: hardware error: ");
456 vfprintf(stderr
, fmt
, ap
);
457 fprintf(stderr
, "\n");
458 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
459 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
461 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
463 cpu_dump_state(env
, stderr
, fprintf
, 0);
470 /***********************************************************/
473 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
474 static void *qemu_put_kbd_event_opaque
;
475 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
476 static void *qemu_put_mouse_event_opaque
;
477 static int qemu_put_mouse_event_absolute
;
479 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
481 qemu_put_kbd_event_opaque
= opaque
;
482 qemu_put_kbd_event
= func
;
485 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
, int absolute
)
487 qemu_put_mouse_event_opaque
= opaque
;
488 qemu_put_mouse_event
= func
;
489 qemu_put_mouse_event_absolute
= absolute
;
492 void kbd_put_keycode(int keycode
)
494 if (qemu_put_kbd_event
) {
495 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
499 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
501 if (qemu_put_mouse_event
) {
502 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
503 dx
, dy
, dz
, buttons_state
);
507 int kbd_mouse_is_absolute(void)
509 return qemu_put_mouse_event_absolute
;
512 /***********************************************************/
515 #if defined(__powerpc__)
517 static inline uint32_t get_tbl(void)
520 asm volatile("mftb %0" : "=r" (tbl
));
524 static inline uint32_t get_tbu(void)
527 asm volatile("mftbu %0" : "=r" (tbl
));
531 int64_t cpu_get_real_ticks(void)
534 /* NOTE: we test if wrapping has occurred */
540 return ((int64_t)h
<< 32) | l
;
543 #elif defined(__i386__)
545 int64_t cpu_get_real_ticks(void)
548 asm volatile ("rdtsc" : "=A" (val
));
552 #elif defined(__x86_64__)
554 int64_t cpu_get_real_ticks(void)
558 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
565 #elif defined(__ia64)
567 int64_t cpu_get_real_ticks(void)
570 asm volatile ("mov %0 = ar.itc" : "=r"(val
) :: "memory");
574 #elif defined(__s390__)
576 int64_t cpu_get_real_ticks(void)
579 asm volatile("stck 0(%1)" : "=m" (val
) : "a" (&val
) : "cc");
584 #error unsupported CPU
587 static int64_t cpu_ticks_offset
;
588 static int cpu_ticks_enabled
;
590 static inline int64_t cpu_get_ticks(void)
592 if (!cpu_ticks_enabled
) {
593 return cpu_ticks_offset
;
595 return cpu_get_real_ticks() + cpu_ticks_offset
;
599 /* enable cpu_get_ticks() */
600 void cpu_enable_ticks(void)
602 if (!cpu_ticks_enabled
) {
603 cpu_ticks_offset
-= cpu_get_real_ticks();
604 cpu_ticks_enabled
= 1;
608 /* disable cpu_get_ticks() : the clock is stopped. You must not call
609 cpu_get_ticks() after that. */
610 void cpu_disable_ticks(void)
612 if (cpu_ticks_enabled
) {
613 cpu_ticks_offset
= cpu_get_ticks();
614 cpu_ticks_enabled
= 0;
618 static int64_t get_clock(void)
623 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
626 gettimeofday(&tv
, NULL
);
627 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
631 void cpu_calibrate_ticks(void)
636 ticks
= cpu_get_real_ticks();
642 usec
= get_clock() - usec
;
643 ticks
= cpu_get_real_ticks() - ticks
;
644 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
647 /* compute with 96 bit intermediate result: (a*b)/c */
648 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
653 #ifdef WORDS_BIGENDIAN
663 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
664 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
667 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
671 #define QEMU_TIMER_REALTIME 0
672 #define QEMU_TIMER_VIRTUAL 1
676 /* XXX: add frequency */
684 struct QEMUTimer
*next
;
690 static QEMUTimer
*active_timers
[2];
692 static MMRESULT timerID
;
694 /* frequency of the times() clock tick */
695 static int timer_freq
;
698 QEMUClock
*qemu_new_clock(int type
)
701 clock
= qemu_mallocz(sizeof(QEMUClock
));
708 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
712 ts
= qemu_mallocz(sizeof(QEMUTimer
));
719 void qemu_free_timer(QEMUTimer
*ts
)
724 /* stop a timer, but do not dealloc it */
725 void qemu_del_timer(QEMUTimer
*ts
)
729 /* NOTE: this code must be signal safe because
730 qemu_timer_expired() can be called from a signal. */
731 pt
= &active_timers
[ts
->clock
->type
];
744 /* modify the current timer so that it will be fired when current_time
745 >= expire_time. The corresponding callback will be called. */
746 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
752 /* add the timer in the sorted list */
753 /* NOTE: this code must be signal safe because
754 qemu_timer_expired() can be called from a signal. */
755 pt
= &active_timers
[ts
->clock
->type
];
760 if (t
->expire_time
> expire_time
)
764 ts
->expire_time
= expire_time
;
769 int qemu_timer_pending(QEMUTimer
*ts
)
772 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
779 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
783 return (timer_head
->expire_time
<= current_time
);
786 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
792 if (!ts
|| ts
->expire_time
> current_time
)
794 /* remove timer from the list before calling the callback */
795 *ptimer_head
= ts
->next
;
798 /* run the callback (the timer list can be modified) */
803 int64_t qemu_get_clock(QEMUClock
*clock
)
805 switch(clock
->type
) {
806 case QEMU_TIMER_REALTIME
:
808 return GetTickCount();
813 /* Note that using gettimeofday() is not a good solution
814 for timers because its value change when the date is
816 if (timer_freq
== 100) {
817 return times(&tp
) * 10;
819 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
824 case QEMU_TIMER_VIRTUAL
:
825 return cpu_get_ticks();
830 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
832 uint64_t expire_time
;
834 if (qemu_timer_pending(ts
)) {
835 expire_time
= ts
->expire_time
;
839 qemu_put_be64(f
, expire_time
);
842 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
844 uint64_t expire_time
;
846 expire_time
= qemu_get_be64(f
);
847 if (expire_time
!= -1) {
848 qemu_mod_timer(ts
, expire_time
);
854 static void timer_save(QEMUFile
*f
, void *opaque
)
856 if (cpu_ticks_enabled
) {
857 hw_error("cannot save state if virtual timers are running");
859 qemu_put_be64s(f
, &cpu_ticks_offset
);
860 qemu_put_be64s(f
, &ticks_per_sec
);
863 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
867 if (cpu_ticks_enabled
) {
870 qemu_get_be64s(f
, &cpu_ticks_offset
);
871 qemu_get_be64s(f
, &ticks_per_sec
);
876 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
877 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
879 static void host_alarm_handler(int host_signum
)
883 #define DISP_FREQ 1000
885 static int64_t delta_min
= INT64_MAX
;
886 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
888 ti
= qemu_get_clock(vm_clock
);
889 if (last_clock
!= 0) {
890 delta
= ti
- last_clock
;
891 if (delta
< delta_min
)
893 if (delta
> delta_max
)
896 if (++count
== DISP_FREQ
) {
897 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
898 muldiv64(delta_min
, 1000000, ticks_per_sec
),
899 muldiv64(delta_max
, 1000000, ticks_per_sec
),
900 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
901 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
903 delta_min
= INT64_MAX
;
911 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
912 qemu_get_clock(vm_clock
)) ||
913 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
914 qemu_get_clock(rt_clock
))) {
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
);
975 timerID
= timeSetEvent(1, // interval (ms)
977 host_alarm_handler
, // function
978 (DWORD
)&count
, // user parameter
979 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
981 perror("failed timer alarm");
985 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
988 struct sigaction act
;
989 struct itimerval itv
;
991 /* get times() syscall frequency */
992 timer_freq
= sysconf(_SC_CLK_TCK
);
995 sigfillset(&act
.sa_mask
);
997 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
998 act
.sa_flags
|= SA_ONSTACK
;
1000 act
.sa_handler
= host_alarm_handler
;
1001 sigaction(SIGALRM
, &act
, NULL
);
1003 itv
.it_interval
.tv_sec
= 0;
1004 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
1005 itv
.it_value
.tv_sec
= 0;
1006 itv
.it_value
.tv_usec
= 10 * 1000;
1007 setitimer(ITIMER_REAL
, &itv
, NULL
);
1008 /* we probe the tick duration of the kernel to inform the user if
1009 the emulated kernel requested a too high timer frequency */
1010 getitimer(ITIMER_REAL
, &itv
);
1012 #if defined(__linux__)
1013 if (itv
.it_interval
.tv_usec
> 1000) {
1014 /* try to use /dev/rtc to have a faster timer */
1015 if (start_rtc_timer() < 0)
1017 /* disable itimer */
1018 itv
.it_interval
.tv_sec
= 0;
1019 itv
.it_interval
.tv_usec
= 0;
1020 itv
.it_value
.tv_sec
= 0;
1021 itv
.it_value
.tv_usec
= 0;
1022 setitimer(ITIMER_REAL
, &itv
, NULL
);
1025 sigaction(SIGIO
, &act
, NULL
);
1026 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1027 fcntl(rtc_fd
, F_SETOWN
, getpid());
1029 #endif /* defined(__linux__) */
1032 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1033 PIT_FREQ
) / 1000000;
1039 void quit_timers(void)
1042 timeKillEvent(timerID
);
1046 /***********************************************************/
1047 /* character device */
1049 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1051 return s
->chr_write(s
, buf
, len
);
1054 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1058 return s
->chr_ioctl(s
, cmd
, arg
);
1061 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1066 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1067 qemu_chr_write(s
, buf
, strlen(buf
));
1071 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1073 if (s
->chr_send_event
)
1074 s
->chr_send_event(s
, event
);
1077 void qemu_chr_add_read_handler(CharDriverState
*s
,
1078 IOCanRWHandler
*fd_can_read
,
1079 IOReadHandler
*fd_read
, void *opaque
)
1081 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
1084 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
1086 s
->chr_event
= chr_event
;
1089 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1094 static void null_chr_add_read_handler(CharDriverState
*chr
,
1095 IOCanRWHandler
*fd_can_read
,
1096 IOReadHandler
*fd_read
, void *opaque
)
1100 CharDriverState
*qemu_chr_open_null(void)
1102 CharDriverState
*chr
;
1104 chr
= qemu_mallocz(sizeof(CharDriverState
));
1107 chr
->chr_write
= null_chr_write
;
1108 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1114 #define socket_error() WSAGetLastError()
1116 #define EWOULDBLOCK WSAEWOULDBLOCK
1117 #define EINTR WSAEINTR
1118 #define EINPROGRESS WSAEINPROGRESS
1120 static void socket_cleanup(void)
1125 static int socket_init(void)
1130 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1132 err
= WSAGetLastError();
1133 fprintf(stderr
, "WSAStartup: %d\n", err
);
1136 atexit(socket_cleanup
);
1140 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1146 ret
= send(fd
, buf
, len
, 0);
1149 errno
= WSAGetLastError();
1150 if (errno
!= WSAEWOULDBLOCK
) {
1153 } else if (ret
== 0) {
1163 void socket_set_nonblock(int fd
)
1165 unsigned long opt
= 1;
1166 ioctlsocket(fd
, FIONBIO
, &opt
);
1171 #define socket_error() errno
1172 #define closesocket(s) close(s)
1174 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1180 ret
= write(fd
, buf
, len
);
1182 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1184 } else if (ret
== 0) {
1194 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1196 return unix_write(fd
, buf
, len1
);
1199 void socket_set_nonblock(int fd
)
1201 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1203 #endif /* !_WIN32 */
1209 IOCanRWHandler
*fd_can_read
;
1210 IOReadHandler
*fd_read
;
1215 #define STDIO_MAX_CLIENTS 2
1217 static int stdio_nb_clients
;
1218 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1220 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1222 FDCharDriver
*s
= chr
->opaque
;
1223 return unix_write(s
->fd_out
, buf
, len
);
1226 static int fd_chr_read_poll(void *opaque
)
1228 CharDriverState
*chr
= opaque
;
1229 FDCharDriver
*s
= chr
->opaque
;
1231 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
1235 static void fd_chr_read(void *opaque
)
1237 CharDriverState
*chr
= opaque
;
1238 FDCharDriver
*s
= chr
->opaque
;
1243 if (len
> s
->max_size
)
1247 size
= read(s
->fd_in
, buf
, len
);
1249 s
->fd_read(s
->fd_opaque
, buf
, size
);
1253 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1254 IOCanRWHandler
*fd_can_read
,
1255 IOReadHandler
*fd_read
, void *opaque
)
1257 FDCharDriver
*s
= chr
->opaque
;
1259 if (s
->fd_in
>= 0) {
1260 s
->fd_can_read
= fd_can_read
;
1261 s
->fd_read
= fd_read
;
1262 s
->fd_opaque
= opaque
;
1263 if (nographic
&& s
->fd_in
== 0) {
1265 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1266 fd_chr_read
, NULL
, chr
);
1271 /* open a character device to a unix fd */
1272 CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1274 CharDriverState
*chr
;
1277 chr
= qemu_mallocz(sizeof(CharDriverState
));
1280 s
= qemu_mallocz(sizeof(FDCharDriver
));
1288 chr
->chr_write
= fd_chr_write
;
1289 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1293 CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1297 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1300 return qemu_chr_open_fd(-1, fd_out
);
1303 CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1307 fd
= open(filename
, O_RDWR
| O_BINARY
);
1310 return qemu_chr_open_fd(fd
, fd
);
1314 /* for STDIO, we handle the case where several clients use it
1317 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1319 #define TERM_FIFO_MAX_SIZE 1
1321 static int term_got_escape
, client_index
;
1322 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1325 void term_print_help(void)
1328 "C-a h print this help\n"
1329 "C-a x exit emulator\n"
1330 "C-a s save disk data back to file (if -snapshot)\n"
1331 "C-a b send break (magic sysrq)\n"
1332 "C-a c switch between console and monitor\n"
1333 "C-a C-a send C-a\n"
1337 /* called when a char is received */
1338 static void stdio_received_byte(int ch
)
1340 if (term_got_escape
) {
1341 term_got_escape
= 0;
1352 for (i
= 0; i
< MAX_DISKS
; i
++) {
1354 bdrv_commit(bs_table
[i
]);
1359 if (client_index
< stdio_nb_clients
) {
1360 CharDriverState
*chr
;
1363 chr
= stdio_clients
[client_index
];
1365 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1370 if (client_index
>= stdio_nb_clients
)
1372 if (client_index
== 0) {
1373 /* send a new line in the monitor to get the prompt */
1381 } else if (ch
== TERM_ESCAPE
) {
1382 term_got_escape
= 1;
1385 if (client_index
< stdio_nb_clients
) {
1387 CharDriverState
*chr
;
1390 chr
= stdio_clients
[client_index
];
1392 if (s
->fd_can_read(s
->fd_opaque
) > 0) {
1394 s
->fd_read(s
->fd_opaque
, buf
, 1);
1395 } else if (term_fifo_size
== 0) {
1396 term_fifo
[term_fifo_size
++] = ch
;
1402 static int stdio_read_poll(void *opaque
)
1404 CharDriverState
*chr
;
1407 if (client_index
< stdio_nb_clients
) {
1408 chr
= stdio_clients
[client_index
];
1410 /* try to flush the queue if needed */
1411 if (term_fifo_size
!= 0 && s
->fd_can_read(s
->fd_opaque
) > 0) {
1412 s
->fd_read(s
->fd_opaque
, term_fifo
, 1);
1415 /* see if we can absorb more chars */
1416 if (term_fifo_size
== 0)
1425 static void stdio_read(void *opaque
)
1430 size
= read(0, buf
, 1);
1432 stdio_received_byte(buf
[0]);
1435 /* init terminal so that we can grab keys */
1436 static struct termios oldtty
;
1437 static int old_fd0_flags
;
1439 static void term_exit(void)
1441 tcsetattr (0, TCSANOW
, &oldtty
);
1442 fcntl(0, F_SETFL
, old_fd0_flags
);
1445 static void term_init(void)
1449 tcgetattr (0, &tty
);
1451 old_fd0_flags
= fcntl(0, F_GETFL
);
1453 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1454 |INLCR
|IGNCR
|ICRNL
|IXON
);
1455 tty
.c_oflag
|= OPOST
;
1456 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1457 /* if graphical mode, we allow Ctrl-C handling */
1459 tty
.c_lflag
&= ~ISIG
;
1460 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1463 tty
.c_cc
[VTIME
] = 0;
1465 tcsetattr (0, TCSANOW
, &tty
);
1469 fcntl(0, F_SETFL
, O_NONBLOCK
);
1472 CharDriverState
*qemu_chr_open_stdio(void)
1474 CharDriverState
*chr
;
1477 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1479 chr
= qemu_chr_open_fd(0, 1);
1480 if (stdio_nb_clients
== 0)
1481 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, NULL
);
1482 client_index
= stdio_nb_clients
;
1484 if (stdio_nb_clients
!= 0)
1486 chr
= qemu_chr_open_fd(0, 1);
1488 stdio_clients
[stdio_nb_clients
++] = chr
;
1489 if (stdio_nb_clients
== 1) {
1490 /* set the terminal in raw mode */
1496 #if defined(__linux__)
1497 CharDriverState
*qemu_chr_open_pty(void)
1500 char slave_name
[1024];
1501 int master_fd
, slave_fd
;
1503 /* Not satisfying */
1504 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1508 /* Disabling local echo and line-buffered output */
1509 tcgetattr (master_fd
, &tty
);
1510 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1512 tty
.c_cc
[VTIME
] = 0;
1513 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1515 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1516 return qemu_chr_open_fd(master_fd
, master_fd
);
1519 static void tty_serial_init(int fd
, int speed
,
1520 int parity
, int data_bits
, int stop_bits
)
1526 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1527 speed
, parity
, data_bits
, stop_bits
);
1529 tcgetattr (fd
, &tty
);
1571 cfsetispeed(&tty
, spd
);
1572 cfsetospeed(&tty
, spd
);
1574 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1575 |INLCR
|IGNCR
|ICRNL
|IXON
);
1576 tty
.c_oflag
|= OPOST
;
1577 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1578 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
);
1599 tty
.c_cflag
|= PARENB
;
1602 tty
.c_cflag
|= PARENB
| PARODD
;
1606 tcsetattr (fd
, TCSANOW
, &tty
);
1609 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1611 FDCharDriver
*s
= chr
->opaque
;
1614 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1616 QEMUSerialSetParams
*ssp
= arg
;
1617 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1618 ssp
->data_bits
, ssp
->stop_bits
);
1621 case CHR_IOCTL_SERIAL_SET_BREAK
:
1623 int enable
= *(int *)arg
;
1625 tcsendbreak(s
->fd_in
, 1);
1634 CharDriverState
*qemu_chr_open_tty(const char *filename
)
1636 CharDriverState
*chr
;
1639 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1642 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1643 tty_serial_init(fd
, 115200, 'N', 8, 1);
1644 chr
= qemu_chr_open_fd(fd
, fd
);
1647 chr
->chr_ioctl
= tty_serial_ioctl
;
1651 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1653 int fd
= (int)chr
->opaque
;
1657 case CHR_IOCTL_PP_READ_DATA
:
1658 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1660 *(uint8_t *)arg
= b
;
1662 case CHR_IOCTL_PP_WRITE_DATA
:
1663 b
= *(uint8_t *)arg
;
1664 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1667 case CHR_IOCTL_PP_READ_CONTROL
:
1668 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1670 *(uint8_t *)arg
= b
;
1672 case CHR_IOCTL_PP_WRITE_CONTROL
:
1673 b
= *(uint8_t *)arg
;
1674 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1677 case CHR_IOCTL_PP_READ_STATUS
:
1678 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1680 *(uint8_t *)arg
= b
;
1688 CharDriverState
*qemu_chr_open_pp(const char *filename
)
1690 CharDriverState
*chr
;
1693 fd
= open(filename
, O_RDWR
);
1697 if (ioctl(fd
, PPCLAIM
) < 0) {
1702 chr
= qemu_mallocz(sizeof(CharDriverState
));
1707 chr
->opaque
= (void *)fd
;
1708 chr
->chr_write
= null_chr_write
;
1709 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1710 chr
->chr_ioctl
= pp_ioctl
;
1715 CharDriverState
*qemu_chr_open_pty(void)
1721 #endif /* !defined(_WIN32) */
1725 IOCanRWHandler
*fd_can_read
;
1726 IOReadHandler
*fd_read
;
1729 HANDLE hcom
, hrecv
, hsend
;
1730 OVERLAPPED orecv
, osend
;
1735 #define NSENDBUF 2048
1736 #define NRECVBUF 2048
1737 #define MAXCONNECT 1
1738 #define NTIMEOUT 5000
1740 static int win_chr_poll(void *opaque
);
1741 static int win_chr_pipe_poll(void *opaque
);
1743 static void win_chr_close2(WinCharState
*s
)
1746 CloseHandle(s
->hsend
);
1750 CloseHandle(s
->hrecv
);
1754 CloseHandle(s
->hcom
);
1758 qemu_del_polling_cb(win_chr_pipe_poll
, s
);
1760 qemu_del_polling_cb(win_chr_poll
, s
);
1763 static void win_chr_close(CharDriverState
*chr
)
1765 WinCharState
*s
= chr
->opaque
;
1769 static int win_chr_init(WinCharState
*s
, const char *filename
)
1772 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1777 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1779 fprintf(stderr
, "Failed CreateEvent\n");
1782 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1784 fprintf(stderr
, "Failed CreateEvent\n");
1788 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1789 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1790 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1791 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1796 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1797 fprintf(stderr
, "Failed SetupComm\n");
1801 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1802 size
= sizeof(COMMCONFIG
);
1803 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1804 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1805 CommConfigDialog(filename
, NULL
, &comcfg
);
1807 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1808 fprintf(stderr
, "Failed SetCommState\n");
1812 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1813 fprintf(stderr
, "Failed SetCommMask\n");
1817 cto
.ReadIntervalTimeout
= MAXDWORD
;
1818 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1819 fprintf(stderr
, "Failed SetCommTimeouts\n");
1823 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1824 fprintf(stderr
, "Failed ClearCommError\n");
1827 qemu_add_polling_cb(win_chr_poll
, s
);
1835 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1837 WinCharState
*s
= chr
->opaque
;
1838 DWORD len
, ret
, size
, err
;
1841 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1842 s
->osend
.hEvent
= s
->hsend
;
1845 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1847 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1849 err
= GetLastError();
1850 if (err
== ERROR_IO_PENDING
) {
1851 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1869 static int win_chr_read_poll(WinCharState
*s
)
1871 s
->max_size
= s
->fd_can_read(s
->win_opaque
);
1875 static void win_chr_readfile(WinCharState
*s
)
1881 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1882 s
->orecv
.hEvent
= s
->hrecv
;
1883 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1885 err
= GetLastError();
1886 if (err
== ERROR_IO_PENDING
) {
1887 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1892 s
->fd_read(s
->win_opaque
, buf
, size
);
1896 static void win_chr_read(WinCharState
*s
)
1898 if (s
->len
> s
->max_size
)
1899 s
->len
= s
->max_size
;
1903 win_chr_readfile(s
);
1906 static int win_chr_poll(void *opaque
)
1908 WinCharState
*s
= opaque
;
1912 ClearCommError(s
->hcom
, &comerr
, &status
);
1913 if (status
.cbInQue
> 0) {
1914 s
->len
= status
.cbInQue
;
1915 win_chr_read_poll(s
);
1922 static void win_chr_add_read_handler(CharDriverState
*chr
,
1923 IOCanRWHandler
*fd_can_read
,
1924 IOReadHandler
*fd_read
, void *opaque
)
1926 WinCharState
*s
= chr
->opaque
;
1928 s
->fd_can_read
= fd_can_read
;
1929 s
->fd_read
= fd_read
;
1930 s
->win_opaque
= opaque
;
1933 CharDriverState
*qemu_chr_open_win(const char *filename
)
1935 CharDriverState
*chr
;
1938 chr
= qemu_mallocz(sizeof(CharDriverState
));
1941 s
= qemu_mallocz(sizeof(WinCharState
));
1947 chr
->chr_write
= win_chr_write
;
1948 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
1949 chr
->chr_close
= win_chr_close
;
1951 if (win_chr_init(s
, filename
) < 0) {
1959 static int win_chr_pipe_poll(void *opaque
)
1961 WinCharState
*s
= opaque
;
1964 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1967 win_chr_read_poll(s
);
1974 static int win_chr_pipe_init(WinCharState
*s
, const char *filename
)
1983 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1985 fprintf(stderr
, "Failed CreateEvent\n");
1988 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1990 fprintf(stderr
, "Failed CreateEvent\n");
1994 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1995 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1996 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1998 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1999 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2000 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2005 ZeroMemory(&ov
, sizeof(ov
));
2006 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2007 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2009 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2013 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2015 fprintf(stderr
, "Failed GetOverlappedResult\n");
2017 CloseHandle(ov
.hEvent
);
2024 CloseHandle(ov
.hEvent
);
2027 qemu_add_polling_cb(win_chr_pipe_poll
, s
);
2036 CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2038 CharDriverState
*chr
;
2041 chr
= qemu_mallocz(sizeof(CharDriverState
));
2044 s
= qemu_mallocz(sizeof(WinCharState
));
2050 chr
->chr_write
= win_chr_write
;
2051 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2052 chr
->chr_close
= win_chr_close
;
2054 if (win_chr_pipe_init(s
, filename
) < 0) {
2062 CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2064 CharDriverState
*chr
;
2067 chr
= qemu_mallocz(sizeof(CharDriverState
));
2070 s
= qemu_mallocz(sizeof(WinCharState
));
2077 chr
->chr_write
= win_chr_write
;
2078 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2082 CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2086 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2087 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2088 if (fd_out
== INVALID_HANDLE_VALUE
)
2091 return qemu_chr_open_win_file(fd_out
);
2095 CharDriverState
*qemu_chr_open(const char *filename
)
2099 if (!strcmp(filename
, "vc")) {
2100 return text_console_init(&display_state
);
2101 } else if (!strcmp(filename
, "null")) {
2102 return qemu_chr_open_null();
2105 if (strstart(filename
, "file:", &p
)) {
2106 return qemu_chr_open_file_out(p
);
2107 } else if (strstart(filename
, "pipe:", &p
)) {
2108 return qemu_chr_open_pipe(p
);
2109 } else if (!strcmp(filename
, "pty")) {
2110 return qemu_chr_open_pty();
2111 } else if (!strcmp(filename
, "stdio")) {
2112 return qemu_chr_open_stdio();
2115 #if defined(__linux__)
2116 if (strstart(filename
, "/dev/parport", NULL
)) {
2117 return qemu_chr_open_pp(filename
);
2119 if (strstart(filename
, "/dev/", NULL
)) {
2120 return qemu_chr_open_tty(filename
);
2124 if (strstart(filename
, "COM", NULL
)) {
2125 return qemu_chr_open_win(filename
);
2127 if (strstart(filename
, "pipe:", &p
)) {
2128 return qemu_chr_open_win_pipe(p
);
2130 if (strstart(filename
, "file:", &p
)) {
2131 return qemu_chr_open_win_file_out(p
);
2139 void qemu_chr_close(CharDriverState
*chr
)
2142 chr
->chr_close(chr
);
2145 /***********************************************************/
2146 /* network device redirectors */
2148 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2152 for(i
=0;i
<size
;i
+=16) {
2156 fprintf(f
, "%08x ", i
);
2159 fprintf(f
, " %02x", buf
[i
+j
]);
2164 for(j
=0;j
<len
;j
++) {
2166 if (c
< ' ' || c
> '~')
2168 fprintf(f
, "%c", c
);
2174 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
2177 for(i
= 0; i
< 6; i
++) {
2178 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2191 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
2196 p1
= strchr(p
, sep
);
2202 if (len
> buf_size
- 1)
2204 memcpy(buf
, p
, len
);
2211 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
2219 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2221 saddr
->sin_family
= AF_INET
;
2222 if (buf
[0] == '\0') {
2223 saddr
->sin_addr
.s_addr
= 0;
2225 if (isdigit(buf
[0])) {
2226 if (!inet_aton(buf
, &saddr
->sin_addr
))
2229 if ((he
= gethostbyname(buf
)) == NULL
)
2231 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
2234 port
= strtol(p
, (char **)&r
, 0);
2237 saddr
->sin_port
= htons(port
);
2241 /* find or alloc a new VLAN */
2242 VLANState
*qemu_find_vlan(int id
)
2244 VLANState
**pvlan
, *vlan
;
2245 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2249 vlan
= qemu_mallocz(sizeof(VLANState
));
2254 pvlan
= &first_vlan
;
2255 while (*pvlan
!= NULL
)
2256 pvlan
= &(*pvlan
)->next
;
2261 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
2262 IOReadHandler
*fd_read
,
2263 IOCanRWHandler
*fd_can_read
,
2266 VLANClientState
*vc
, **pvc
;
2267 vc
= qemu_mallocz(sizeof(VLANClientState
));
2270 vc
->fd_read
= fd_read
;
2271 vc
->fd_can_read
= fd_can_read
;
2272 vc
->opaque
= opaque
;
2276 pvc
= &vlan
->first_client
;
2277 while (*pvc
!= NULL
)
2278 pvc
= &(*pvc
)->next
;
2283 int qemu_can_send_packet(VLANClientState
*vc1
)
2285 VLANState
*vlan
= vc1
->vlan
;
2286 VLANClientState
*vc
;
2288 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2290 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
2297 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
2299 VLANState
*vlan
= vc1
->vlan
;
2300 VLANClientState
*vc
;
2303 printf("vlan %d send:\n", vlan
->id
);
2304 hex_dump(stdout
, buf
, size
);
2306 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2308 vc
->fd_read(vc
->opaque
, buf
, size
);
2313 #if defined(CONFIG_SLIRP)
2315 /* slirp network adapter */
2317 static int slirp_inited
;
2318 static VLANClientState
*slirp_vc
;
2320 int slirp_can_output(void)
2322 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
2325 void slirp_output(const uint8_t *pkt
, int pkt_len
)
2328 printf("slirp output:\n");
2329 hex_dump(stdout
, pkt
, pkt_len
);
2333 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
2336 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
2339 printf("slirp input:\n");
2340 hex_dump(stdout
, buf
, size
);
2342 slirp_input(buf
, size
);
2345 static int net_slirp_init(VLANState
*vlan
)
2347 if (!slirp_inited
) {
2351 slirp_vc
= qemu_new_vlan_client(vlan
,
2352 slirp_receive
, NULL
, NULL
);
2353 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
2357 static void net_slirp_redir(const char *redir_str
)
2362 struct in_addr guest_addr
;
2363 int host_port
, guest_port
;
2365 if (!slirp_inited
) {
2371 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2373 if (!strcmp(buf
, "tcp")) {
2375 } else if (!strcmp(buf
, "udp")) {
2381 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2383 host_port
= strtol(buf
, &r
, 0);
2387 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2389 if (buf
[0] == '\0') {
2390 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
2392 if (!inet_aton(buf
, &guest_addr
))
2395 guest_port
= strtol(p
, &r
, 0);
2399 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
2400 fprintf(stderr
, "qemu: could not set up redirection\n");
2405 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
2413 static void smb_exit(void)
2417 char filename
[1024];
2419 /* erase all the files in the directory */
2420 d
= opendir(smb_dir
);
2425 if (strcmp(de
->d_name
, ".") != 0 &&
2426 strcmp(de
->d_name
, "..") != 0) {
2427 snprintf(filename
, sizeof(filename
), "%s/%s",
2428 smb_dir
, de
->d_name
);
2436 /* automatic user mode samba server configuration */
2437 void net_slirp_smb(const char *exported_dir
)
2439 char smb_conf
[1024];
2440 char smb_cmdline
[1024];
2443 if (!slirp_inited
) {
2448 /* XXX: better tmp dir construction */
2449 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
2450 if (mkdir(smb_dir
, 0700) < 0) {
2451 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
2454 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
2456 f
= fopen(smb_conf
, "w");
2458 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
2465 "socket address=127.0.0.1\n"
2466 "pid directory=%s\n"
2467 "lock directory=%s\n"
2468 "log file=%s/log.smbd\n"
2469 "smb passwd file=%s/smbpasswd\n"
2470 "security = share\n"
2485 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "/usr/sbin/smbd -s %s",
2488 slirp_add_exec(0, smb_cmdline
, 4, 139);
2491 #endif /* !defined(_WIN32) */
2493 #endif /* CONFIG_SLIRP */
2495 #if !defined(_WIN32)
2497 typedef struct TAPState
{
2498 VLANClientState
*vc
;
2502 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
2504 TAPState
*s
= opaque
;
2507 ret
= write(s
->fd
, buf
, size
);
2508 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
2515 static void tap_send(void *opaque
)
2517 TAPState
*s
= opaque
;
2521 size
= read(s
->fd
, buf
, sizeof(buf
));
2523 qemu_send_packet(s
->vc
, buf
, size
);
2529 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
2533 s
= qemu_mallocz(sizeof(TAPState
));
2537 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
2538 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
2539 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
2544 static int tap_open(char *ifname
, int ifname_size
)
2550 fd
= open("/dev/tap", O_RDWR
);
2552 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
2557 dev
= devname(s
.st_rdev
, S_IFCHR
);
2558 pstrcpy(ifname
, ifname_size
, dev
);
2560 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2564 static int tap_open(char *ifname
, int ifname_size
)
2569 fd
= open("/dev/net/tun", O_RDWR
);
2571 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
2574 memset(&ifr
, 0, sizeof(ifr
));
2575 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
2576 if (ifname
[0] != '\0')
2577 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
2579 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
2580 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
2582 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
2586 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
2587 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2592 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
2593 const char *setup_script
)
2596 int pid
, status
, fd
;
2601 if (ifname1
!= NULL
)
2602 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
2605 fd
= tap_open(ifname
, sizeof(ifname
));
2611 if (setup_script
[0] != '\0') {
2612 /* try to launch network init script */
2617 *parg
++ = (char *)setup_script
;
2620 execv(setup_script
, args
);
2623 while (waitpid(pid
, &status
, 0) != pid
);
2624 if (!WIFEXITED(status
) ||
2625 WEXITSTATUS(status
) != 0) {
2626 fprintf(stderr
, "%s: could not launch network script\n",
2632 s
= net_tap_fd_init(vlan
, fd
);
2635 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2636 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
2640 #endif /* !_WIN32 */
2642 /* network connection */
2643 typedef struct NetSocketState
{
2644 VLANClientState
*vc
;
2646 int state
; /* 0 = getting length, 1 = getting data */
2650 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
2653 typedef struct NetSocketListenState
{
2656 } NetSocketListenState
;
2658 /* XXX: we consider we can send the whole packet without blocking */
2659 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
2661 NetSocketState
*s
= opaque
;
2665 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
2666 send_all(s
->fd
, buf
, size
);
2669 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
2671 NetSocketState
*s
= opaque
;
2672 sendto(s
->fd
, buf
, size
, 0,
2673 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
2676 static void net_socket_send(void *opaque
)
2678 NetSocketState
*s
= opaque
;
2683 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
2685 err
= socket_error();
2686 if (err
!= EWOULDBLOCK
)
2688 } else if (size
== 0) {
2689 /* end of connection */
2691 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2697 /* reassemble a packet from the network */
2703 memcpy(s
->buf
+ s
->index
, buf
, l
);
2707 if (s
->index
== 4) {
2709 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
2715 l
= s
->packet_len
- s
->index
;
2718 memcpy(s
->buf
+ s
->index
, buf
, l
);
2722 if (s
->index
>= s
->packet_len
) {
2723 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
2732 static void net_socket_send_dgram(void *opaque
)
2734 NetSocketState
*s
= opaque
;
2737 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2741 /* end of connection */
2742 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2745 qemu_send_packet(s
->vc
, s
->buf
, size
);
2748 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
2753 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
2754 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
2755 inet_ntoa(mcastaddr
->sin_addr
),
2756 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
2760 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2762 perror("socket(PF_INET, SOCK_DGRAM)");
2767 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
2768 (const char *)&val
, sizeof(val
));
2770 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
2774 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
2780 /* Add host to multicast group */
2781 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
2782 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
2784 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
2785 (const char *)&imr
, sizeof(struct ip_mreq
));
2787 perror("setsockopt(IP_ADD_MEMBERSHIP)");
2791 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
2793 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
2794 (const char *)&val
, sizeof(val
));
2796 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
2800 socket_set_nonblock(fd
);
2803 if (fd
>=0) close(fd
);
2807 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
2810 struct sockaddr_in saddr
;
2812 socklen_t saddr_len
;
2815 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
2816 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
2817 * by ONLY ONE process: we must "clone" this dgram socket --jjo
2821 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
2823 if (saddr
.sin_addr
.s_addr
==0) {
2824 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
2828 /* clone dgram socket */
2829 newfd
= net_socket_mcast_create(&saddr
);
2831 /* error already reported by net_socket_mcast_create() */
2835 /* clone newfd to fd, close newfd */
2840 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
2841 fd
, strerror(errno
));
2846 s
= qemu_mallocz(sizeof(NetSocketState
));
2851 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
2852 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
2854 /* mcast: save bound address as dst */
2855 if (is_connected
) s
->dgram_dst
=saddr
;
2857 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2858 "socket: fd=%d (%s mcast=%s:%d)",
2859 fd
, is_connected
? "cloned" : "",
2860 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2864 static void net_socket_connect(void *opaque
)
2866 NetSocketState
*s
= opaque
;
2867 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
2870 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
2874 s
= qemu_mallocz(sizeof(NetSocketState
));
2878 s
->vc
= qemu_new_vlan_client(vlan
,
2879 net_socket_receive
, NULL
, s
);
2880 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2881 "socket: fd=%d", fd
);
2883 net_socket_connect(s
);
2885 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
2890 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
2893 int so_type
=-1, optlen
=sizeof(so_type
);
2895 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
2896 fprintf(stderr
, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd
);
2901 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
2903 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
2905 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
2906 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
2907 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
2912 static void net_socket_accept(void *opaque
)
2914 NetSocketListenState
*s
= opaque
;
2916 struct sockaddr_in saddr
;
2921 len
= sizeof(saddr
);
2922 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
2923 if (fd
< 0 && errno
!= EINTR
) {
2925 } else if (fd
>= 0) {
2929 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
2933 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
2934 "socket: connection from %s:%d",
2935 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2939 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
2941 NetSocketListenState
*s
;
2943 struct sockaddr_in saddr
;
2945 if (parse_host_port(&saddr
, host_str
) < 0)
2948 s
= qemu_mallocz(sizeof(NetSocketListenState
));
2952 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2957 socket_set_nonblock(fd
);
2959 /* allow fast reuse */
2961 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2963 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2968 ret
= listen(fd
, 0);
2975 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
2979 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
2982 int fd
, connected
, ret
, err
;
2983 struct sockaddr_in saddr
;
2985 if (parse_host_port(&saddr
, host_str
) < 0)
2988 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2993 socket_set_nonblock(fd
);
2997 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2999 err
= socket_error();
3000 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3001 } else if (err
== EINPROGRESS
) {
3013 s
= net_socket_fd_init(vlan
, fd
, connected
);
3016 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3017 "socket: connect to %s:%d",
3018 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3022 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
3026 struct sockaddr_in saddr
;
3028 if (parse_host_port(&saddr
, host_str
) < 0)
3032 fd
= net_socket_mcast_create(&saddr
);
3036 s
= net_socket_fd_init(vlan
, fd
, 0);
3040 s
->dgram_dst
= saddr
;
3042 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3043 "socket: mcast=%s:%d",
3044 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3049 static int get_param_value(char *buf
, int buf_size
,
3050 const char *tag
, const char *str
)
3059 while (*p
!= '\0' && *p
!= '=') {
3060 if ((q
- option
) < sizeof(option
) - 1)
3068 if (!strcmp(tag
, option
)) {
3070 while (*p
!= '\0' && *p
!= ',') {
3071 if ((q
- buf
) < buf_size
- 1)
3078 while (*p
!= '\0' && *p
!= ',') {
3089 int net_client_init(const char *str
)
3100 while (*p
!= '\0' && *p
!= ',') {
3101 if ((q
- device
) < sizeof(device
) - 1)
3109 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
3110 vlan_id
= strtol(buf
, NULL
, 0);
3112 vlan
= qemu_find_vlan(vlan_id
);
3114 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
3117 if (!strcmp(device
, "nic")) {
3121 if (nb_nics
>= MAX_NICS
) {
3122 fprintf(stderr
, "Too Many NICs\n");
3125 nd
= &nd_table
[nb_nics
];
3126 macaddr
= nd
->macaddr
;
3132 macaddr
[5] = 0x56 + nb_nics
;
3134 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
3135 if (parse_macaddr(macaddr
, buf
) < 0) {
3136 fprintf(stderr
, "invalid syntax for ethernet address\n");
3140 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
3141 nd
->model
= strdup(buf
);
3147 if (!strcmp(device
, "none")) {
3148 /* does nothing. It is needed to signal that no network cards
3153 if (!strcmp(device
, "user")) {
3154 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
3155 if (strlen(buf
) > 32)
3157 strcpy(slirp_hostname
, buf
);
3159 ret
= net_slirp_init(vlan
);
3163 if (!strcmp(device
, "tap")) {
3165 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
3166 fprintf(stderr
, "tap: no interface name\n");
3169 ret
= tap_win32_init(vlan
, ifname
);
3172 if (!strcmp(device
, "tap")) {
3174 char setup_script
[1024];
3176 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3177 fd
= strtol(buf
, NULL
, 0);
3179 if (net_tap_fd_init(vlan
, fd
))
3182 get_param_value(ifname
, sizeof(ifname
), "ifname", p
);
3183 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
3184 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
3186 ret
= net_tap_init(vlan
, ifname
, setup_script
);
3190 if (!strcmp(device
, "socket")) {
3191 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3193 fd
= strtol(buf
, NULL
, 0);
3195 if (net_socket_fd_init(vlan
, fd
, 1))
3197 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
3198 ret
= net_socket_listen_init(vlan
, buf
);
3199 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
3200 ret
= net_socket_connect_init(vlan
, buf
);
3201 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
3202 ret
= net_socket_mcast_init(vlan
, buf
);
3204 fprintf(stderr
, "Unknown socket options: %s\n", p
);
3209 fprintf(stderr
, "Unknown network device: %s\n", device
);
3213 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
3219 void do_info_network(void)
3222 VLANClientState
*vc
;
3224 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3225 term_printf("VLAN %d devices:\n", vlan
->id
);
3226 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3227 term_printf(" %s\n", vc
->info_str
);
3231 /***********************************************************/
3234 static int usb_device_add(const char *devname
)
3242 for(i
= 0;i
< MAX_VM_USB_PORTS
; i
++) {
3243 if (!vm_usb_ports
[i
]->dev
)
3246 if (i
== MAX_VM_USB_PORTS
)
3249 if (strstart(devname
, "host:", &p
)) {
3250 dev
= usb_host_device_open(p
);
3253 } else if (!strcmp(devname
, "mouse")) {
3254 dev
= usb_mouse_init();
3257 } else if (!strcmp(devname
, "tablet")) {
3258 dev
= usb_tablet_init();
3264 usb_attach(vm_usb_ports
[i
], dev
);
3268 static int usb_device_del(const char *devname
)
3271 int bus_num
, addr
, i
;
3277 p
= strchr(devname
, '.');
3280 bus_num
= strtoul(devname
, NULL
, 0);
3281 addr
= strtoul(p
+ 1, NULL
, 0);
3284 for(i
= 0;i
< MAX_VM_USB_PORTS
; i
++) {
3285 dev
= vm_usb_ports
[i
]->dev
;
3286 if (dev
&& dev
->addr
== addr
)
3289 if (i
== MAX_VM_USB_PORTS
)
3291 usb_attach(vm_usb_ports
[i
], NULL
);
3295 void do_usb_add(const char *devname
)
3298 ret
= usb_device_add(devname
);
3300 term_printf("Could not add USB device '%s'\n", devname
);
3303 void do_usb_del(const char *devname
)
3306 ret
= usb_device_del(devname
);
3308 term_printf("Could not remove USB device '%s'\n", devname
);
3315 const char *speed_str
;
3318 term_printf("USB support not enabled\n");
3322 for(i
= 0; i
< MAX_VM_USB_PORTS
; i
++) {
3323 dev
= vm_usb_ports
[i
]->dev
;
3325 term_printf("Hub port %d:\n", i
);
3326 switch(dev
->speed
) {
3330 case USB_SPEED_FULL
:
3333 case USB_SPEED_HIGH
:
3340 term_printf(" Device %d.%d, speed %s Mb/s\n",
3341 0, dev
->addr
, speed_str
);
3346 /***********************************************************/
3349 static char *pid_filename
;
3351 /* Remove PID file. Called on normal exit */
3353 static void remove_pidfile(void)
3355 unlink (pid_filename
);
3358 static void create_pidfile(const char *filename
)
3360 struct stat pidstat
;
3363 /* Try to write our PID to the named file */
3364 if (stat(filename
, &pidstat
) < 0) {
3365 if (errno
== ENOENT
) {
3366 if ((f
= fopen (filename
, "w")) == NULL
) {
3367 perror("Opening pidfile");
3370 fprintf(f
, "%d\n", getpid());
3372 pid_filename
= qemu_strdup(filename
);
3373 if (!pid_filename
) {
3374 fprintf(stderr
, "Could not save PID filename");
3377 atexit(remove_pidfile
);
3380 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
3386 /***********************************************************/
3389 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
3393 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
3397 static void dumb_refresh(DisplayState
*ds
)
3402 void dumb_display_init(DisplayState
*ds
)
3407 ds
->dpy_update
= dumb_update
;
3408 ds
->dpy_resize
= dumb_resize
;
3409 ds
->dpy_refresh
= dumb_refresh
;
3412 #if !defined(CONFIG_SOFTMMU)
3413 /***********************************************************/
3414 /* cpu signal handler */
3415 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
3418 if (cpu_signal_handler(host_signum
, info
, puc
))
3420 if (stdio_nb_clients
> 0)
3426 /***********************************************************/
3429 #define MAX_IO_HANDLERS 64
3431 typedef struct IOHandlerRecord
{
3433 IOCanRWHandler
*fd_read_poll
;
3435 IOHandler
*fd_write
;
3437 /* temporary data */
3439 struct IOHandlerRecord
*next
;
3442 static IOHandlerRecord
*first_io_handler
;
3444 /* XXX: fd_read_poll should be suppressed, but an API change is
3445 necessary in the character devices to suppress fd_can_read(). */
3446 int qemu_set_fd_handler2(int fd
,
3447 IOCanRWHandler
*fd_read_poll
,
3449 IOHandler
*fd_write
,
3452 IOHandlerRecord
**pioh
, *ioh
;
3454 if (!fd_read
&& !fd_write
) {
3455 pioh
= &first_io_handler
;
3460 if (ioh
->fd
== fd
) {
3468 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3472 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
3475 ioh
->next
= first_io_handler
;
3476 first_io_handler
= ioh
;
3479 ioh
->fd_read_poll
= fd_read_poll
;
3480 ioh
->fd_read
= fd_read
;
3481 ioh
->fd_write
= fd_write
;
3482 ioh
->opaque
= opaque
;
3487 int qemu_set_fd_handler(int fd
,
3489 IOHandler
*fd_write
,
3492 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
3495 /***********************************************************/
3496 /* Polling handling */
3498 typedef struct PollingEntry
{
3501 struct PollingEntry
*next
;
3504 static PollingEntry
*first_polling_entry
;
3506 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
3508 PollingEntry
**ppe
, *pe
;
3509 pe
= qemu_mallocz(sizeof(PollingEntry
));
3513 pe
->opaque
= opaque
;
3514 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
3519 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3521 PollingEntry
**ppe
, *pe
;
3522 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3524 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3532 /***********************************************************/
3533 /* savevm/loadvm support */
3535 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
3537 fwrite(buf
, 1, size
, f
);
3540 void qemu_put_byte(QEMUFile
*f
, int v
)
3545 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
3547 qemu_put_byte(f
, v
>> 8);
3548 qemu_put_byte(f
, v
);
3551 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
3553 qemu_put_byte(f
, v
>> 24);
3554 qemu_put_byte(f
, v
>> 16);
3555 qemu_put_byte(f
, v
>> 8);
3556 qemu_put_byte(f
, v
);
3559 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
3561 qemu_put_be32(f
, v
>> 32);
3562 qemu_put_be32(f
, v
);
3565 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
3567 return fread(buf
, 1, size
, f
);
3570 int qemu_get_byte(QEMUFile
*f
)
3580 unsigned int qemu_get_be16(QEMUFile
*f
)
3583 v
= qemu_get_byte(f
) << 8;
3584 v
|= qemu_get_byte(f
);
3588 unsigned int qemu_get_be32(QEMUFile
*f
)
3591 v
= qemu_get_byte(f
) << 24;
3592 v
|= qemu_get_byte(f
) << 16;
3593 v
|= qemu_get_byte(f
) << 8;
3594 v
|= qemu_get_byte(f
);
3598 uint64_t qemu_get_be64(QEMUFile
*f
)
3601 v
= (uint64_t)qemu_get_be32(f
) << 32;
3602 v
|= qemu_get_be32(f
);
3606 int64_t qemu_ftell(QEMUFile
*f
)
3611 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
3613 if (fseek(f
, pos
, whence
) < 0)
3618 typedef struct SaveStateEntry
{
3622 SaveStateHandler
*save_state
;
3623 LoadStateHandler
*load_state
;
3625 struct SaveStateEntry
*next
;
3628 static SaveStateEntry
*first_se
;
3630 int register_savevm(const char *idstr
,
3633 SaveStateHandler
*save_state
,
3634 LoadStateHandler
*load_state
,
3637 SaveStateEntry
*se
, **pse
;
3639 se
= qemu_malloc(sizeof(SaveStateEntry
));
3642 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
3643 se
->instance_id
= instance_id
;
3644 se
->version_id
= version_id
;
3645 se
->save_state
= save_state
;
3646 se
->load_state
= load_state
;
3647 se
->opaque
= opaque
;
3650 /* add at the end of list */
3652 while (*pse
!= NULL
)
3653 pse
= &(*pse
)->next
;
3658 #define QEMU_VM_FILE_MAGIC 0x5145564d
3659 #define QEMU_VM_FILE_VERSION 0x00000001
3661 int qemu_savevm(const char *filename
)
3665 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
3667 saved_vm_running
= vm_running
;
3670 f
= fopen(filename
, "wb");
3676 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
3677 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
3679 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3681 len
= strlen(se
->idstr
);
3682 qemu_put_byte(f
, len
);
3683 qemu_put_buffer(f
, se
->idstr
, len
);
3685 qemu_put_be32(f
, se
->instance_id
);
3686 qemu_put_be32(f
, se
->version_id
);
3688 /* record size: filled later */
3690 qemu_put_be32(f
, 0);
3692 se
->save_state(f
, se
->opaque
);
3694 /* fill record size */
3696 len
= ftell(f
) - len_pos
- 4;
3697 fseek(f
, len_pos
, SEEK_SET
);
3698 qemu_put_be32(f
, len
);
3699 fseek(f
, cur_pos
, SEEK_SET
);
3705 if (saved_vm_running
)
3710 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
3714 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3715 if (!strcmp(se
->idstr
, idstr
) &&
3716 instance_id
== se
->instance_id
)
3722 int qemu_loadvm(const char *filename
)
3726 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
3727 int saved_vm_running
;
3731 saved_vm_running
= vm_running
;
3734 f
= fopen(filename
, "rb");
3740 v
= qemu_get_be32(f
);
3741 if (v
!= QEMU_VM_FILE_MAGIC
)
3743 v
= qemu_get_be32(f
);
3744 if (v
!= QEMU_VM_FILE_VERSION
) {
3751 len
= qemu_get_byte(f
);
3754 qemu_get_buffer(f
, idstr
, len
);
3756 instance_id
= qemu_get_be32(f
);
3757 version_id
= qemu_get_be32(f
);
3758 record_len
= qemu_get_be32(f
);
3760 printf("idstr=%s instance=0x%x version=%d len=%d\n",
3761 idstr
, instance_id
, version_id
, record_len
);
3764 se
= find_se(idstr
, instance_id
);
3766 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
3767 instance_id
, idstr
);
3769 ret
= se
->load_state(f
, se
->opaque
, version_id
);
3771 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
3772 instance_id
, idstr
);
3775 /* always seek to exact end of record */
3776 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
3781 if (saved_vm_running
)
3786 /***********************************************************/
3787 /* cpu save/restore */
3789 #if defined(TARGET_I386)
3791 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
3793 qemu_put_be32(f
, dt
->selector
);
3794 qemu_put_betl(f
, dt
->base
);
3795 qemu_put_be32(f
, dt
->limit
);
3796 qemu_put_be32(f
, dt
->flags
);
3799 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
3801 dt
->selector
= qemu_get_be32(f
);
3802 dt
->base
= qemu_get_betl(f
);
3803 dt
->limit
= qemu_get_be32(f
);
3804 dt
->flags
= qemu_get_be32(f
);
3807 void cpu_save(QEMUFile
*f
, void *opaque
)
3809 CPUState
*env
= opaque
;
3810 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
3814 for(i
= 0; i
< CPU_NB_REGS
; i
++)
3815 qemu_put_betls(f
, &env
->regs
[i
]);
3816 qemu_put_betls(f
, &env
->eip
);
3817 qemu_put_betls(f
, &env
->eflags
);
3818 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
3819 qemu_put_be32s(f
, &hflags
);
3823 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
3825 for(i
= 0; i
< 8; i
++) {
3826 fptag
|= ((!env
->fptags
[i
]) << i
);
3829 qemu_put_be16s(f
, &fpuc
);
3830 qemu_put_be16s(f
, &fpus
);
3831 qemu_put_be16s(f
, &fptag
);
3833 #ifdef USE_X86LDOUBLE
3838 qemu_put_be16s(f
, &fpregs_format
);
3840 for(i
= 0; i
< 8; i
++) {
3841 #ifdef USE_X86LDOUBLE
3845 /* we save the real CPU data (in case of MMX usage only 'mant'
3846 contains the MMX register */
3847 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
3848 qemu_put_be64(f
, mant
);
3849 qemu_put_be16(f
, exp
);
3852 /* if we use doubles for float emulation, we save the doubles to
3853 avoid losing information in case of MMX usage. It can give
3854 problems if the image is restored on a CPU where long
3855 doubles are used instead. */
3856 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
3860 for(i
= 0; i
< 6; i
++)
3861 cpu_put_seg(f
, &env
->segs
[i
]);
3862 cpu_put_seg(f
, &env
->ldt
);
3863 cpu_put_seg(f
, &env
->tr
);
3864 cpu_put_seg(f
, &env
->gdt
);
3865 cpu_put_seg(f
, &env
->idt
);
3867 qemu_put_be32s(f
, &env
->sysenter_cs
);
3868 qemu_put_be32s(f
, &env
->sysenter_esp
);
3869 qemu_put_be32s(f
, &env
->sysenter_eip
);
3871 qemu_put_betls(f
, &env
->cr
[0]);
3872 qemu_put_betls(f
, &env
->cr
[2]);
3873 qemu_put_betls(f
, &env
->cr
[3]);
3874 qemu_put_betls(f
, &env
->cr
[4]);
3876 for(i
= 0; i
< 8; i
++)
3877 qemu_put_betls(f
, &env
->dr
[i
]);
3880 qemu_put_be32s(f
, &env
->a20_mask
);
3883 qemu_put_be32s(f
, &env
->mxcsr
);
3884 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
3885 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
3886 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
3889 #ifdef TARGET_X86_64
3890 qemu_put_be64s(f
, &env
->efer
);
3891 qemu_put_be64s(f
, &env
->star
);
3892 qemu_put_be64s(f
, &env
->lstar
);
3893 qemu_put_be64s(f
, &env
->cstar
);
3894 qemu_put_be64s(f
, &env
->fmask
);
3895 qemu_put_be64s(f
, &env
->kernelgsbase
);
3899 #ifdef USE_X86LDOUBLE
3900 /* XXX: add that in a FPU generic layer */
3901 union x86_longdouble
{
3906 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
3907 #define EXPBIAS1 1023
3908 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
3909 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
3911 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
3915 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
3916 /* exponent + sign */
3917 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
3918 e
|= SIGND1(temp
) >> 16;
3923 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
3925 CPUState
*env
= opaque
;
3928 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
3930 if (version_id
!= 3)
3932 for(i
= 0; i
< CPU_NB_REGS
; i
++)
3933 qemu_get_betls(f
, &env
->regs
[i
]);
3934 qemu_get_betls(f
, &env
->eip
);
3935 qemu_get_betls(f
, &env
->eflags
);
3936 qemu_get_be32s(f
, &hflags
);
3938 qemu_get_be16s(f
, &fpuc
);
3939 qemu_get_be16s(f
, &fpus
);
3940 qemu_get_be16s(f
, &fptag
);
3941 qemu_get_be16s(f
, &fpregs_format
);
3943 /* NOTE: we cannot always restore the FPU state if the image come
3944 from a host with a different 'USE_X86LDOUBLE' define. We guess
3945 if we are in an MMX state to restore correctly in that case. */
3946 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
3947 for(i
= 0; i
< 8; i
++) {
3951 switch(fpregs_format
) {
3953 mant
= qemu_get_be64(f
);
3954 exp
= qemu_get_be16(f
);
3955 #ifdef USE_X86LDOUBLE
3956 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
3958 /* difficult case */
3960 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
3962 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
3966 mant
= qemu_get_be64(f
);
3967 #ifdef USE_X86LDOUBLE
3969 union x86_longdouble
*p
;
3970 /* difficult case */
3971 p
= (void *)&env
->fpregs
[i
];
3976 fp64_to_fp80(p
, mant
);
3980 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
3989 /* XXX: restore FPU round state */
3990 env
->fpstt
= (fpus
>> 11) & 7;
3991 env
->fpus
= fpus
& ~0x3800;
3993 for(i
= 0; i
< 8; i
++) {
3994 env
->fptags
[i
] = (fptag
>> i
) & 1;
3997 for(i
= 0; i
< 6; i
++)
3998 cpu_get_seg(f
, &env
->segs
[i
]);
3999 cpu_get_seg(f
, &env
->ldt
);
4000 cpu_get_seg(f
, &env
->tr
);
4001 cpu_get_seg(f
, &env
->gdt
);
4002 cpu_get_seg(f
, &env
->idt
);
4004 qemu_get_be32s(f
, &env
->sysenter_cs
);
4005 qemu_get_be32s(f
, &env
->sysenter_esp
);
4006 qemu_get_be32s(f
, &env
->sysenter_eip
);
4008 qemu_get_betls(f
, &env
->cr
[0]);
4009 qemu_get_betls(f
, &env
->cr
[2]);
4010 qemu_get_betls(f
, &env
->cr
[3]);
4011 qemu_get_betls(f
, &env
->cr
[4]);
4013 for(i
= 0; i
< 8; i
++)
4014 qemu_get_betls(f
, &env
->dr
[i
]);
4017 qemu_get_be32s(f
, &env
->a20_mask
);
4019 qemu_get_be32s(f
, &env
->mxcsr
);
4020 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
4021 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
4022 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
4025 #ifdef TARGET_X86_64
4026 qemu_get_be64s(f
, &env
->efer
);
4027 qemu_get_be64s(f
, &env
->star
);
4028 qemu_get_be64s(f
, &env
->lstar
);
4029 qemu_get_be64s(f
, &env
->cstar
);
4030 qemu_get_be64s(f
, &env
->fmask
);
4031 qemu_get_be64s(f
, &env
->kernelgsbase
);
4034 /* XXX: compute hflags from scratch, except for CPL and IIF */
4035 env
->hflags
= hflags
;
4040 #elif defined(TARGET_PPC)
4041 void cpu_save(QEMUFile
*f
, void *opaque
)
4045 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4050 #elif defined(TARGET_MIPS)
4051 void cpu_save(QEMUFile
*f
, void *opaque
)
4055 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4060 #elif defined(TARGET_SPARC)
4061 void cpu_save(QEMUFile
*f
, void *opaque
)
4063 CPUState
*env
= opaque
;
4067 for(i
= 0; i
< 8; i
++)
4068 qemu_put_betls(f
, &env
->gregs
[i
]);
4069 for(i
= 0; i
< NWINDOWS
* 16; i
++)
4070 qemu_put_betls(f
, &env
->regbase
[i
]);
4073 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
4079 qemu_put_betl(f
, u
.i
);
4082 qemu_put_betls(f
, &env
->pc
);
4083 qemu_put_betls(f
, &env
->npc
);
4084 qemu_put_betls(f
, &env
->y
);
4086 qemu_put_be32(f
, tmp
);
4087 qemu_put_betls(f
, &env
->fsr
);
4088 qemu_put_betls(f
, &env
->tbr
);
4089 #ifndef TARGET_SPARC64
4090 qemu_put_be32s(f
, &env
->wim
);
4092 for(i
= 0; i
< 16; i
++)
4093 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
4097 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4099 CPUState
*env
= opaque
;
4103 for(i
= 0; i
< 8; i
++)
4104 qemu_get_betls(f
, &env
->gregs
[i
]);
4105 for(i
= 0; i
< NWINDOWS
* 16; i
++)
4106 qemu_get_betls(f
, &env
->regbase
[i
]);
4109 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
4114 u
.i
= qemu_get_betl(f
);
4118 qemu_get_betls(f
, &env
->pc
);
4119 qemu_get_betls(f
, &env
->npc
);
4120 qemu_get_betls(f
, &env
->y
);
4121 tmp
= qemu_get_be32(f
);
4122 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
4123 correctly updated */
4125 qemu_get_betls(f
, &env
->fsr
);
4126 qemu_get_betls(f
, &env
->tbr
);
4127 #ifndef TARGET_SPARC64
4128 qemu_get_be32s(f
, &env
->wim
);
4130 for(i
= 0; i
< 16; i
++)
4131 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
4137 #elif defined(TARGET_ARM)
4139 /* ??? Need to implement these. */
4140 void cpu_save(QEMUFile
*f
, void *opaque
)
4144 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4151 #warning No CPU save/restore functions
4155 /***********************************************************/
4156 /* ram save/restore */
4158 /* we just avoid storing empty pages */
4159 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
4164 for(i
= 1; i
< len
; i
++) {
4168 qemu_put_byte(f
, 1);
4169 qemu_put_byte(f
, v
);
4172 qemu_put_byte(f
, 0);
4173 qemu_put_buffer(f
, buf
, len
);
4176 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
4180 v
= qemu_get_byte(f
);
4183 if (qemu_get_buffer(f
, buf
, len
) != len
)
4187 v
= qemu_get_byte(f
);
4188 memset(buf
, v
, len
);
4196 static void ram_save(QEMUFile
*f
, void *opaque
)
4199 qemu_put_be32(f
, phys_ram_size
);
4200 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
4201 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
4205 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
4209 if (version_id
!= 1)
4211 if (qemu_get_be32(f
) != phys_ram_size
)
4213 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
4214 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
4221 /***********************************************************/
4222 /* machine registration */
4224 QEMUMachine
*first_machine
= NULL
;
4226 int qemu_register_machine(QEMUMachine
*m
)
4229 pm
= &first_machine
;
4237 QEMUMachine
*find_machine(const char *name
)
4241 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4242 if (!strcmp(m
->name
, name
))
4248 /***********************************************************/
4249 /* main execution loop */
4251 void gui_update(void *opaque
)
4253 display_state
.dpy_refresh(&display_state
);
4254 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
4257 struct vm_change_state_entry
{
4258 VMChangeStateHandler
*cb
;
4260 LIST_ENTRY (vm_change_state_entry
) entries
;
4263 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
4265 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
4268 VMChangeStateEntry
*e
;
4270 e
= qemu_mallocz(sizeof (*e
));
4276 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
4280 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
4282 LIST_REMOVE (e
, entries
);
4286 static void vm_state_notify(int running
)
4288 VMChangeStateEntry
*e
;
4290 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
4291 e
->cb(e
->opaque
, running
);
4295 /* XXX: support several handlers */
4296 static VMStopHandler
*vm_stop_cb
;
4297 static void *vm_stop_opaque
;
4299 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4302 vm_stop_opaque
= opaque
;
4306 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4320 void vm_stop(int reason
)
4323 cpu_disable_ticks();
4327 vm_stop_cb(vm_stop_opaque
, reason
);
4334 /* reset/shutdown handler */
4336 typedef struct QEMUResetEntry
{
4337 QEMUResetHandler
*func
;
4339 struct QEMUResetEntry
*next
;
4342 static QEMUResetEntry
*first_reset_entry
;
4343 static int reset_requested
;
4344 static int shutdown_requested
;
4345 static int powerdown_requested
;
4347 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
4349 QEMUResetEntry
**pre
, *re
;
4351 pre
= &first_reset_entry
;
4352 while (*pre
!= NULL
)
4353 pre
= &(*pre
)->next
;
4354 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
4356 re
->opaque
= opaque
;
4361 void qemu_system_reset(void)
4365 /* reset all devices */
4366 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
4367 re
->func(re
->opaque
);
4371 void qemu_system_reset_request(void)
4373 reset_requested
= 1;
4375 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4378 void qemu_system_shutdown_request(void)
4380 shutdown_requested
= 1;
4382 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4385 void qemu_system_powerdown_request(void)
4387 powerdown_requested
= 1;
4389 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4392 void main_loop_wait(int timeout
)
4394 IOHandlerRecord
*ioh
, *ioh_next
;
4401 /* XXX: need to suppress polling by better using win32 events */
4403 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4404 ret
|= pe
->func(pe
->opaque
);
4407 if (ret
== 0 && timeout
> 0) {
4411 /* poll any events */
4412 /* XXX: separate device handlers from system ones */
4416 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4418 (!ioh
->fd_read_poll
||
4419 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4420 FD_SET(ioh
->fd
, &rfds
);
4424 if (ioh
->fd_write
) {
4425 FD_SET(ioh
->fd
, &wfds
);
4435 tv
.tv_usec
= timeout
* 1000;
4437 ret
= select(nfds
+ 1, &rfds
, &wfds
, NULL
, &tv
);
4439 /* XXX: better handling of removal */
4440 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
4441 ioh_next
= ioh
->next
;
4442 if (FD_ISSET(ioh
->fd
, &rfds
)) {
4443 ioh
->fd_read(ioh
->opaque
);
4445 if (FD_ISSET(ioh
->fd
, &wfds
)) {
4446 ioh
->fd_write(ioh
->opaque
);
4454 #if defined(CONFIG_SLIRP)
4455 /* XXX: merge with the previous select() */
4457 fd_set rfds
, wfds
, xfds
;
4465 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4468 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4470 slirp_select_poll(&rfds
, &wfds
, &xfds
);
4476 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4477 qemu_get_clock(vm_clock
));
4478 /* run dma transfers, if any */
4482 /* real time timers */
4483 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4484 qemu_get_clock(rt_clock
));
4487 static CPUState
*cur_cpu
;
4492 #ifdef CONFIG_PROFILER
4497 cur_cpu
= first_cpu
;
4504 env
= env
->next_cpu
;
4507 #ifdef CONFIG_PROFILER
4508 ti
= profile_getclock();
4510 ret
= cpu_exec(env
);
4511 #ifdef CONFIG_PROFILER
4512 qemu_time
+= profile_getclock() - ti
;
4514 if (ret
!= EXCP_HALTED
)
4516 /* all CPUs are halted ? */
4517 if (env
== cur_cpu
) {
4524 if (shutdown_requested
) {
4525 ret
= EXCP_INTERRUPT
;
4528 if (reset_requested
) {
4529 reset_requested
= 0;
4530 qemu_system_reset();
4531 ret
= EXCP_INTERRUPT
;
4533 if (powerdown_requested
) {
4534 powerdown_requested
= 0;
4535 qemu_system_powerdown();
4536 ret
= EXCP_INTERRUPT
;
4538 if (ret
== EXCP_DEBUG
) {
4539 vm_stop(EXCP_DEBUG
);
4541 /* if hlt instruction, we wait until the next IRQ */
4542 /* XXX: use timeout computed from timers */
4543 if (ret
== EXCP_HLT
)
4550 #ifdef CONFIG_PROFILER
4551 ti
= profile_getclock();
4553 main_loop_wait(timeout
);
4554 #ifdef CONFIG_PROFILER
4555 dev_time
+= profile_getclock() - ti
;
4558 cpu_disable_ticks();
4564 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2005 Fabrice Bellard\n"
4565 "usage: %s [options] [disk_image]\n"
4567 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4569 "Standard options:\n"
4570 "-M machine select emulated machine (-M ? for list)\n"
4571 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
4572 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
4573 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
4574 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
4575 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
4576 "-snapshot write to temporary files instead of disk image files\n"
4577 "-m megs set virtual RAM size to megs MB [default=%d]\n"
4578 "-smp n set the number of CPUs to 'n' [default=1]\n"
4579 "-nographic disable graphical output and redirect serial I/Os to console\n"
4581 "-k language use keyboard layout (for example \"fr\" for French)\n"
4584 "-audio-help print list of audio drivers and their options\n"
4585 "-soundhw c1,... enable audio support\n"
4586 " and only specified sound cards (comma separated list)\n"
4587 " use -soundhw ? to get the list of supported cards\n"
4588 " use -soundhw all to enable all of them\n"
4590 "-localtime set the real time clock to local time [default=utc]\n"
4591 "-full-screen start in full screen\n"
4593 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4595 "-usb enable the USB driver (will be the default soon)\n"
4596 "-usbdevice name add the host or guest USB device 'name'\n"
4597 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4598 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4601 "Network options:\n"
4602 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
4603 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4605 "-net user[,vlan=n][,hostname=host]\n"
4606 " connect the user mode network stack to VLAN 'n' and send\n"
4607 " hostname 'host' to DHCP clients\n"
4610 "-net tap[,vlan=n],ifname=name\n"
4611 " connect the host TAP network interface to VLAN 'n'\n"
4613 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
4614 " connect the host TAP network interface to VLAN 'n' and use\n"
4615 " the network script 'file' (default=%s);\n"
4616 " use 'fd=h' to connect to an already opened TAP interface\n"
4618 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4619 " connect the vlan 'n' to another VLAN using a socket connection\n"
4620 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
4621 " connect the vlan 'n' to multicast maddr and port\n"
4622 "-net none use it alone to have zero network devices; if no -net option\n"
4623 " is provided, the default is '-net nic -net user'\n"
4626 "-tftp prefix allow tftp access to files starting with prefix [-net user]\n"
4628 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4630 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4631 " redirect TCP or UDP connections from host to guest [-net user]\n"
4634 "Linux boot specific:\n"
4635 "-kernel bzImage use 'bzImage' as kernel image\n"
4636 "-append cmdline use 'cmdline' as kernel command line\n"
4637 "-initrd file use 'file' as initial ram disk\n"
4639 "Debug/Expert options:\n"
4640 "-monitor dev redirect the monitor to char device 'dev'\n"
4641 "-serial dev redirect the serial port to char device 'dev'\n"
4642 "-parallel dev redirect the parallel port to char device 'dev'\n"
4643 "-pidfile file Write PID to 'file'\n"
4644 "-S freeze CPU at startup (use 'c' to start execution)\n"
4645 "-s wait gdb connection to port %d\n"
4646 "-p port change gdb connection port\n"
4647 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4648 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
4649 " translation (t=none or lba) (usually qemu can guess them)\n"
4650 "-L path set the directory for the BIOS and VGA BIOS\n"
4652 "-no-kqemu disable KQEMU kernel module usage\n"
4654 #ifdef USE_CODE_COPY
4655 "-no-code-copy disable code copy acceleration\n"
4658 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
4659 " (default is CL-GD5446 PCI VGA)\n"
4661 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
4663 "During emulation, the following keys are useful:\n"
4664 "ctrl-alt-f toggle full screen\n"
4665 "ctrl-alt-n switch to virtual console 'n'\n"
4666 "ctrl-alt toggle mouse and keyboard grab\n"
4668 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4670 #ifdef CONFIG_SOFTMMU
4677 DEFAULT_NETWORK_SCRIPT
,
4679 DEFAULT_GDBSTUB_PORT
,
4681 #ifndef CONFIG_SOFTMMU
4683 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
4684 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
4690 #define HAS_ARG 0x0001
4704 QEMU_OPTION_snapshot
,
4706 QEMU_OPTION_nographic
,
4708 QEMU_OPTION_audio_help
,
4709 QEMU_OPTION_soundhw
,
4727 QEMU_OPTION_no_code_copy
,
4729 QEMU_OPTION_localtime
,
4730 QEMU_OPTION_cirrusvga
,
4732 QEMU_OPTION_std_vga
,
4733 QEMU_OPTION_monitor
,
4735 QEMU_OPTION_parallel
,
4737 QEMU_OPTION_full_screen
,
4738 QEMU_OPTION_pidfile
,
4739 QEMU_OPTION_no_kqemu
,
4740 QEMU_OPTION_kernel_kqemu
,
4741 QEMU_OPTION_win2k_hack
,
4743 QEMU_OPTION_usbdevice
,
4747 typedef struct QEMUOption
{
4753 const QEMUOption qemu_options
[] = {
4754 { "h", 0, QEMU_OPTION_h
},
4756 { "M", HAS_ARG
, QEMU_OPTION_M
},
4757 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4758 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4759 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4760 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4761 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4762 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4763 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4764 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4765 { "snapshot", 0, QEMU_OPTION_snapshot
},
4766 { "m", HAS_ARG
, QEMU_OPTION_m
},
4767 { "nographic", 0, QEMU_OPTION_nographic
},
4768 { "k", HAS_ARG
, QEMU_OPTION_k
},
4770 { "audio-help", 0, QEMU_OPTION_audio_help
},
4771 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4774 { "net", HAS_ARG
, QEMU_OPTION_net
},
4776 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4778 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4780 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4783 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4784 { "append", HAS_ARG
, QEMU_OPTION_append
},
4785 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4787 { "S", 0, QEMU_OPTION_S
},
4788 { "s", 0, QEMU_OPTION_s
},
4789 { "p", HAS_ARG
, QEMU_OPTION_p
},
4790 { "d", HAS_ARG
, QEMU_OPTION_d
},
4791 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4792 { "L", HAS_ARG
, QEMU_OPTION_L
},
4793 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
4795 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4796 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4798 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4799 { "g", 1, QEMU_OPTION_g
},
4801 { "localtime", 0, QEMU_OPTION_localtime
},
4802 { "std-vga", 0, QEMU_OPTION_std_vga
},
4803 { "monitor", 1, QEMU_OPTION_monitor
},
4804 { "serial", 1, QEMU_OPTION_serial
},
4805 { "parallel", 1, QEMU_OPTION_parallel
},
4806 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4807 { "full-screen", 0, QEMU_OPTION_full_screen
},
4808 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4809 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4810 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4811 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4813 /* temporary options */
4814 { "usb", 0, QEMU_OPTION_usb
},
4815 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
4819 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
4821 /* this stack is only used during signal handling */
4822 #define SIGNAL_STACK_SIZE 32768
4824 static uint8_t *signal_stack
;
4828 /* password input */
4830 static BlockDriverState
*get_bdrv(int index
)
4832 BlockDriverState
*bs
;
4835 bs
= bs_table
[index
];
4836 } else if (index
< 6) {
4837 bs
= fd_table
[index
- 4];
4844 static void read_passwords(void)
4846 BlockDriverState
*bs
;
4850 for(i
= 0; i
< 6; i
++) {
4852 if (bs
&& bdrv_is_encrypted(bs
)) {
4853 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
4854 for(j
= 0; j
< 3; j
++) {
4855 monitor_readline("Password: ",
4856 1, password
, sizeof(password
));
4857 if (bdrv_set_key(bs
, password
) == 0)
4859 term_printf("invalid password\n");
4865 /* XXX: currently we cannot use simultaneously different CPUs */
4866 void register_machines(void)
4868 #if defined(TARGET_I386)
4869 qemu_register_machine(&pc_machine
);
4870 qemu_register_machine(&isapc_machine
);
4871 #elif defined(TARGET_PPC)
4872 qemu_register_machine(&heathrow_machine
);
4873 qemu_register_machine(&core99_machine
);
4874 qemu_register_machine(&prep_machine
);
4875 #elif defined(TARGET_MIPS)
4876 qemu_register_machine(&mips_machine
);
4877 #elif defined(TARGET_SPARC)
4878 #ifdef TARGET_SPARC64
4879 qemu_register_machine(&sun4u_machine
);
4881 qemu_register_machine(&sun4m_machine
);
4883 #elif defined(TARGET_ARM)
4884 qemu_register_machine(&integratorcp926_machine
);
4885 qemu_register_machine(&integratorcp1026_machine
);
4886 qemu_register_machine(&versatilepb_machine
);
4888 #error unsupported CPU
4893 struct soundhw soundhw
[] = {
4896 "Creative Sound Blaster 16",
4899 { .init_isa
= SB16_init
}
4906 "Yamaha YMF262 (OPL3)",
4908 "Yamaha YM3812 (OPL2)",
4912 { .init_isa
= Adlib_init
}
4919 "Gravis Ultrasound GF1",
4922 { .init_isa
= GUS_init
}
4928 "ENSONIQ AudioPCI ES1370",
4931 { .init_pci
= es1370_init
}
4934 { NULL
, NULL
, 0, 0, { NULL
} }
4937 static void select_soundhw (const char *optarg
)
4941 if (*optarg
== '?') {
4944 printf ("Valid sound card names (comma separated):\n");
4945 for (c
= soundhw
; c
->name
; ++c
) {
4946 printf ("%-11s %s\n", c
->name
, c
->descr
);
4948 printf ("\n-soundhw all will enable all of the above\n");
4949 exit (*optarg
!= '?');
4957 if (!strcmp (optarg
, "all")) {
4958 for (c
= soundhw
; c
->name
; ++c
) {
4966 e
= strchr (p
, ',');
4967 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4969 for (c
= soundhw
; c
->name
; ++c
) {
4970 if (!strncmp (c
->name
, p
, l
)) {
4979 "Unknown sound card name (too big to show)\n");
4982 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4987 p
+= l
+ (e
!= NULL
);
4991 goto show_valid_cards
;
4996 #define MAX_NET_CLIENTS 32
4998 int main(int argc
, char **argv
)
5000 #ifdef CONFIG_GDBSTUB
5001 int use_gdbstub
, gdbstub_port
;
5004 int snapshot
, linux_boot
;
5005 const char *initrd_filename
;
5006 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
5007 const char *kernel_filename
, *kernel_cmdline
;
5008 DisplayState
*ds
= &display_state
;
5009 int cyls
, heads
, secs
, translation
;
5010 int start_emulation
= 1;
5011 char net_clients
[MAX_NET_CLIENTS
][256];
5014 const char *r
, *optarg
;
5015 CharDriverState
*monitor_hd
;
5016 char monitor_device
[128];
5017 char serial_devices
[MAX_SERIAL_PORTS
][128];
5018 int serial_device_index
;
5019 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
5020 int parallel_device_index
;
5021 const char *loadvm
= NULL
;
5022 QEMUMachine
*machine
;
5023 char usb_devices
[MAX_VM_USB_PORTS
][128];
5024 int usb_devices_index
;
5026 LIST_INIT (&vm_change_state_head
);
5027 #if !defined(CONFIG_SOFTMMU)
5028 /* we never want that malloc() uses mmap() */
5029 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
5031 register_machines();
5032 machine
= first_machine
;
5033 initrd_filename
= NULL
;
5034 for(i
= 0; i
< MAX_FD
; i
++)
5035 fd_filename
[i
] = NULL
;
5036 for(i
= 0; i
< MAX_DISKS
; i
++)
5037 hd_filename
[i
] = NULL
;
5038 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5039 vga_ram_size
= VGA_RAM_SIZE
;
5040 bios_size
= BIOS_SIZE
;
5041 #ifdef CONFIG_GDBSTUB
5043 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
5047 kernel_filename
= NULL
;
5048 kernel_cmdline
= "";
5054 cyls
= heads
= secs
= 0;
5055 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5056 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
5058 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
5059 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
5060 serial_devices
[i
][0] = '\0';
5061 serial_device_index
= 0;
5063 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
5064 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
5065 parallel_devices
[i
][0] = '\0';
5066 parallel_device_index
= 0;
5068 usb_devices_index
= 0;
5073 /* default mac address of the first network interface */
5081 hd_filename
[0] = argv
[optind
++];
5083 const QEMUOption
*popt
;
5086 popt
= qemu_options
;
5089 fprintf(stderr
, "%s: invalid option -- '%s'\n",
5093 if (!strcmp(popt
->name
, r
+ 1))
5097 if (popt
->flags
& HAS_ARG
) {
5098 if (optind
>= argc
) {
5099 fprintf(stderr
, "%s: option '%s' requires an argument\n",
5103 optarg
= argv
[optind
++];
5108 switch(popt
->index
) {
5110 machine
= find_machine(optarg
);
5113 printf("Supported machines are:\n");
5114 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5115 printf("%-10s %s%s\n",
5117 m
== first_machine
? " (default)" : "");
5122 case QEMU_OPTION_initrd
:
5123 initrd_filename
= optarg
;
5125 case QEMU_OPTION_hda
:
5126 case QEMU_OPTION_hdb
:
5127 case QEMU_OPTION_hdc
:
5128 case QEMU_OPTION_hdd
:
5131 hd_index
= popt
->index
- QEMU_OPTION_hda
;
5132 hd_filename
[hd_index
] = optarg
;
5133 if (hd_index
== cdrom_index
)
5137 case QEMU_OPTION_snapshot
:
5140 case QEMU_OPTION_hdachs
:
5144 cyls
= strtol(p
, (char **)&p
, 0);
5145 if (cyls
< 1 || cyls
> 16383)
5150 heads
= strtol(p
, (char **)&p
, 0);
5151 if (heads
< 1 || heads
> 16)
5156 secs
= strtol(p
, (char **)&p
, 0);
5157 if (secs
< 1 || secs
> 63)
5161 if (!strcmp(p
, "none"))
5162 translation
= BIOS_ATA_TRANSLATION_NONE
;
5163 else if (!strcmp(p
, "lba"))
5164 translation
= BIOS_ATA_TRANSLATION_LBA
;
5165 else if (!strcmp(p
, "auto"))
5166 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5169 } else if (*p
!= '\0') {
5171 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5176 case QEMU_OPTION_nographic
:
5177 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
5178 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
5181 case QEMU_OPTION_kernel
:
5182 kernel_filename
= optarg
;
5184 case QEMU_OPTION_append
:
5185 kernel_cmdline
= optarg
;
5187 case QEMU_OPTION_cdrom
:
5188 if (cdrom_index
>= 0) {
5189 hd_filename
[cdrom_index
] = optarg
;
5192 case QEMU_OPTION_boot
:
5193 boot_device
= optarg
[0];
5194 if (boot_device
!= 'a' &&
5197 boot_device
!= 'n' &&
5199 boot_device
!= 'c' && boot_device
!= 'd') {
5200 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
5204 case QEMU_OPTION_fda
:
5205 fd_filename
[0] = optarg
;
5207 case QEMU_OPTION_fdb
:
5208 fd_filename
[1] = optarg
;
5210 case QEMU_OPTION_no_code_copy
:
5211 code_copy_enabled
= 0;
5213 case QEMU_OPTION_net
:
5214 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5215 fprintf(stderr
, "qemu: too many network clients\n");
5218 pstrcpy(net_clients
[nb_net_clients
],
5219 sizeof(net_clients
[0]),
5224 case QEMU_OPTION_tftp
:
5225 tftp_prefix
= optarg
;
5228 case QEMU_OPTION_smb
:
5229 net_slirp_smb(optarg
);
5232 case QEMU_OPTION_redir
:
5233 net_slirp_redir(optarg
);
5237 case QEMU_OPTION_audio_help
:
5241 case QEMU_OPTION_soundhw
:
5242 select_soundhw (optarg
);
5249 ram_size
= atoi(optarg
) * 1024 * 1024;
5252 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
5253 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
5254 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
5263 mask
= cpu_str_to_log_mask(optarg
);
5265 printf("Log items (comma separated):\n");
5266 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5267 printf("%-10s %s\n", item
->name
, item
->help
);
5274 #ifdef CONFIG_GDBSTUB
5279 gdbstub_port
= atoi(optarg
);
5286 start_emulation
= 0;
5289 keyboard_layout
= optarg
;
5291 case QEMU_OPTION_localtime
:
5294 case QEMU_OPTION_cirrusvga
:
5295 cirrus_vga_enabled
= 1;
5297 case QEMU_OPTION_std_vga
:
5298 cirrus_vga_enabled
= 0;
5305 w
= strtol(p
, (char **)&p
, 10);
5308 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5314 h
= strtol(p
, (char **)&p
, 10);
5319 depth
= strtol(p
, (char **)&p
, 10);
5320 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5321 depth
!= 24 && depth
!= 32)
5323 } else if (*p
== '\0') {
5324 depth
= graphic_depth
;
5331 graphic_depth
= depth
;
5334 case QEMU_OPTION_monitor
:
5335 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
5337 case QEMU_OPTION_serial
:
5338 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5339 fprintf(stderr
, "qemu: too many serial ports\n");
5342 pstrcpy(serial_devices
[serial_device_index
],
5343 sizeof(serial_devices
[0]), optarg
);
5344 serial_device_index
++;
5346 case QEMU_OPTION_parallel
:
5347 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5348 fprintf(stderr
, "qemu: too many parallel ports\n");
5351 pstrcpy(parallel_devices
[parallel_device_index
],
5352 sizeof(parallel_devices
[0]), optarg
);
5353 parallel_device_index
++;
5355 case QEMU_OPTION_loadvm
:
5358 case QEMU_OPTION_full_screen
:
5361 case QEMU_OPTION_pidfile
:
5362 create_pidfile(optarg
);
5365 case QEMU_OPTION_win2k_hack
:
5366 win2k_install_hack
= 1;
5370 case QEMU_OPTION_no_kqemu
:
5373 case QEMU_OPTION_kernel_kqemu
:
5377 case QEMU_OPTION_usb
:
5380 case QEMU_OPTION_usbdevice
:
5382 if (usb_devices_index
>= MAX_VM_USB_PORTS
) {
5383 fprintf(stderr
, "Too many USB devices\n");
5386 pstrcpy(usb_devices
[usb_devices_index
],
5387 sizeof(usb_devices
[usb_devices_index
]),
5389 usb_devices_index
++;
5391 case QEMU_OPTION_smp
:
5392 smp_cpus
= atoi(optarg
);
5393 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
5394 fprintf(stderr
, "Invalid number of CPUs\n");
5406 linux_boot
= (kernel_filename
!= NULL
);
5409 hd_filename
[0] == '\0' &&
5410 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
5411 fd_filename
[0] == '\0')
5414 /* boot to cd by default if no hard disk */
5415 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
5416 if (fd_filename
[0] != '\0')
5422 #if !defined(CONFIG_SOFTMMU)
5423 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
5425 static uint8_t stdout_buf
[4096];
5426 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
5429 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5436 /* init network clients */
5437 if (nb_net_clients
== 0) {
5438 /* if no clients, we use a default config */
5439 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
5441 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
5446 for(i
= 0;i
< nb_net_clients
; i
++) {
5447 if (net_client_init(net_clients
[i
]) < 0)
5451 /* init the memory */
5452 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
5454 #ifdef CONFIG_SOFTMMU
5455 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5456 if (!phys_ram_base
) {
5457 fprintf(stderr
, "Could not allocate physical memory\n");
5461 /* as we must map the same page at several addresses, we must use
5466 tmpdir
= getenv("QEMU_TMPDIR");
5469 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
5470 if (mkstemp(phys_ram_file
) < 0) {
5471 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
5475 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
5476 if (phys_ram_fd
< 0) {
5477 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
5481 ftruncate(phys_ram_fd
, phys_ram_size
);
5482 unlink(phys_ram_file
);
5483 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
5485 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
5487 if (phys_ram_base
== MAP_FAILED
) {
5488 fprintf(stderr
, "Could not map physical memory\n");
5494 /* we always create the cdrom drive, even if no disk is there */
5496 if (cdrom_index
>= 0) {
5497 bs_table
[cdrom_index
] = bdrv_new("cdrom");
5498 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
5501 /* open the virtual block devices */
5502 for(i
= 0; i
< MAX_DISKS
; i
++) {
5503 if (hd_filename
[i
]) {
5506 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
5507 bs_table
[i
] = bdrv_new(buf
);
5509 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
5510 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
5514 if (i
== 0 && cyls
!= 0) {
5515 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
5516 bdrv_set_translation_hint(bs_table
[i
], translation
);
5521 /* we always create at least one floppy disk */
5522 fd_table
[0] = bdrv_new("fda");
5523 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
5525 for(i
= 0; i
< MAX_FD
; i
++) {
5526 if (fd_filename
[i
]) {
5529 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
5530 fd_table
[i
] = bdrv_new(buf
);
5531 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
5533 if (fd_filename
[i
] != '\0') {
5534 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
5535 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
5543 /* init USB devices */
5545 vm_usb_hub
= usb_hub_init(vm_usb_ports
, MAX_VM_USB_PORTS
);
5546 for(i
= 0; i
< usb_devices_index
; i
++) {
5547 if (usb_device_add(usb_devices
[i
]) < 0) {
5548 fprintf(stderr
, "Warning: could not add USB device %s\n",
5554 register_savevm("timer", 0, 1, timer_save
, timer_load
, NULL
);
5555 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
5558 cpu_calibrate_ticks();
5562 dumb_display_init(ds
);
5564 #if defined(CONFIG_SDL)
5565 sdl_display_init(ds
, full_screen
);
5566 #elif defined(CONFIG_COCOA)
5567 cocoa_display_init(ds
, full_screen
);
5569 dumb_display_init(ds
);
5573 monitor_hd
= qemu_chr_open(monitor_device
);
5575 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5578 monitor_init(monitor_hd
, !nographic
);
5580 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5581 if (serial_devices
[i
][0] != '\0') {
5582 serial_hds
[i
] = qemu_chr_open(serial_devices
[i
]);
5583 if (!serial_hds
[i
]) {
5584 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5588 if (!strcmp(serial_devices
[i
], "vc"))
5589 qemu_chr_printf(serial_hds
[i
], "serial%d console\n", i
);
5593 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5594 if (parallel_devices
[i
][0] != '\0') {
5595 parallel_hds
[i
] = qemu_chr_open(parallel_devices
[i
]);
5596 if (!parallel_hds
[i
]) {
5597 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5598 parallel_devices
[i
]);
5601 if (!strcmp(parallel_devices
[i
], "vc"))
5602 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\n", i
);
5606 /* setup cpu signal handlers for MMU / self modifying code handling */
5607 #if !defined(CONFIG_SOFTMMU)
5609 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5612 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
5613 stk
.ss_sp
= signal_stack
;
5614 stk
.ss_size
= SIGNAL_STACK_SIZE
;
5617 if (sigaltstack(&stk
, NULL
) < 0) {
5618 perror("sigaltstack");
5624 struct sigaction act
;
5626 sigfillset(&act
.sa_mask
);
5627 act
.sa_flags
= SA_SIGINFO
;
5628 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5629 act
.sa_flags
|= SA_ONSTACK
;
5631 act
.sa_sigaction
= host_segv_handler
;
5632 sigaction(SIGSEGV
, &act
, NULL
);
5633 sigaction(SIGBUS
, &act
, NULL
);
5634 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5635 sigaction(SIGFPE
, &act
, NULL
);
5642 struct sigaction act
;
5643 sigfillset(&act
.sa_mask
);
5645 act
.sa_handler
= SIG_IGN
;
5646 sigaction(SIGPIPE
, &act
, NULL
);
5651 machine
->init(ram_size
, vga_ram_size
, boot_device
,
5652 ds
, fd_filename
, snapshot
,
5653 kernel_filename
, kernel_cmdline
, initrd_filename
);
5655 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
5656 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
5658 #ifdef CONFIG_GDBSTUB
5660 if (gdbserver_start(gdbstub_port
) < 0) {
5661 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
5665 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
5670 qemu_loadvm(loadvm
);
5673 /* XXX: simplify init */
5675 if (start_emulation
) {