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 void cpu_outb(CPUState
*env
, int addr
, int val
)
338 if (loglevel
& CPU_LOG_IOPORT
)
339 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
341 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
344 env
->last_io_time
= cpu_get_time_fast();
348 void cpu_outw(CPUState
*env
, int addr
, int val
)
351 if (loglevel
& CPU_LOG_IOPORT
)
352 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
354 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
357 env
->last_io_time
= cpu_get_time_fast();
361 void cpu_outl(CPUState
*env
, int addr
, int val
)
364 if (loglevel
& CPU_LOG_IOPORT
)
365 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
367 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
370 env
->last_io_time
= cpu_get_time_fast();
374 int cpu_inb(CPUState
*env
, int addr
)
377 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
379 if (loglevel
& CPU_LOG_IOPORT
)
380 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
384 env
->last_io_time
= cpu_get_time_fast();
389 int cpu_inw(CPUState
*env
, int addr
)
392 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
394 if (loglevel
& CPU_LOG_IOPORT
)
395 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
399 env
->last_io_time
= cpu_get_time_fast();
404 int cpu_inl(CPUState
*env
, int addr
)
407 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
409 if (loglevel
& CPU_LOG_IOPORT
)
410 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
414 env
->last_io_time
= cpu_get_time_fast();
419 /***********************************************************/
420 void hw_error(const char *fmt
, ...)
426 fprintf(stderr
, "qemu: hardware error: ");
427 vfprintf(stderr
, fmt
, ap
);
428 fprintf(stderr
, "\n");
429 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
430 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
432 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
434 cpu_dump_state(env
, stderr
, fprintf
, 0);
441 /***********************************************************/
444 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
445 static void *qemu_put_kbd_event_opaque
;
446 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
447 static void *qemu_put_mouse_event_opaque
;
448 static int qemu_put_mouse_event_absolute
;
450 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
452 qemu_put_kbd_event_opaque
= opaque
;
453 qemu_put_kbd_event
= func
;
456 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
, int absolute
)
458 qemu_put_mouse_event_opaque
= opaque
;
459 qemu_put_mouse_event
= func
;
460 qemu_put_mouse_event_absolute
= absolute
;
463 void kbd_put_keycode(int keycode
)
465 if (qemu_put_kbd_event
) {
466 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
470 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
472 if (qemu_put_mouse_event
) {
473 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
474 dx
, dy
, dz
, buttons_state
);
478 int kbd_mouse_is_absolute(void)
480 return qemu_put_mouse_event_absolute
;
483 /***********************************************************/
486 #if defined(__powerpc__)
488 static inline uint32_t get_tbl(void)
491 asm volatile("mftb %0" : "=r" (tbl
));
495 static inline uint32_t get_tbu(void)
498 asm volatile("mftbu %0" : "=r" (tbl
));
502 int64_t cpu_get_real_ticks(void)
505 /* NOTE: we test if wrapping has occurred */
511 return ((int64_t)h
<< 32) | l
;
514 #elif defined(__i386__)
516 int64_t cpu_get_real_ticks(void)
519 asm volatile ("rdtsc" : "=A" (val
));
523 #elif defined(__x86_64__)
525 int64_t cpu_get_real_ticks(void)
529 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
536 #elif defined(__ia64)
538 int64_t cpu_get_real_ticks(void)
541 asm volatile ("mov %0 = ar.itc" : "=r"(val
) :: "memory");
545 #elif defined(__s390__)
547 int64_t cpu_get_real_ticks(void)
550 asm volatile("stck 0(%1)" : "=m" (val
) : "a" (&val
) : "cc");
555 #error unsupported CPU
558 static int64_t cpu_ticks_offset
;
559 static int cpu_ticks_enabled
;
561 static inline int64_t cpu_get_ticks(void)
563 if (!cpu_ticks_enabled
) {
564 return cpu_ticks_offset
;
566 return cpu_get_real_ticks() + cpu_ticks_offset
;
570 /* enable cpu_get_ticks() */
571 void cpu_enable_ticks(void)
573 if (!cpu_ticks_enabled
) {
574 cpu_ticks_offset
-= cpu_get_real_ticks();
575 cpu_ticks_enabled
= 1;
579 /* disable cpu_get_ticks() : the clock is stopped. You must not call
580 cpu_get_ticks() after that. */
581 void cpu_disable_ticks(void)
583 if (cpu_ticks_enabled
) {
584 cpu_ticks_offset
= cpu_get_ticks();
585 cpu_ticks_enabled
= 0;
589 static int64_t get_clock(void)
594 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
597 gettimeofday(&tv
, NULL
);
598 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
602 void cpu_calibrate_ticks(void)
607 ticks
= cpu_get_real_ticks();
613 usec
= get_clock() - usec
;
614 ticks
= cpu_get_real_ticks() - ticks
;
615 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
618 /* compute with 96 bit intermediate result: (a*b)/c */
619 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
624 #ifdef WORDS_BIGENDIAN
634 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
635 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
638 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
642 #define QEMU_TIMER_REALTIME 0
643 #define QEMU_TIMER_VIRTUAL 1
647 /* XXX: add frequency */
655 struct QEMUTimer
*next
;
661 static QEMUTimer
*active_timers
[2];
663 static MMRESULT timerID
;
665 /* frequency of the times() clock tick */
666 static int timer_freq
;
669 QEMUClock
*qemu_new_clock(int type
)
672 clock
= qemu_mallocz(sizeof(QEMUClock
));
679 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
683 ts
= qemu_mallocz(sizeof(QEMUTimer
));
690 void qemu_free_timer(QEMUTimer
*ts
)
695 /* stop a timer, but do not dealloc it */
696 void qemu_del_timer(QEMUTimer
*ts
)
700 /* NOTE: this code must be signal safe because
701 qemu_timer_expired() can be called from a signal. */
702 pt
= &active_timers
[ts
->clock
->type
];
715 /* modify the current timer so that it will be fired when current_time
716 >= expire_time. The corresponding callback will be called. */
717 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
723 /* add the timer in the sorted list */
724 /* NOTE: this code must be signal safe because
725 qemu_timer_expired() can be called from a signal. */
726 pt
= &active_timers
[ts
->clock
->type
];
731 if (t
->expire_time
> expire_time
)
735 ts
->expire_time
= expire_time
;
740 int qemu_timer_pending(QEMUTimer
*ts
)
743 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
750 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
754 return (timer_head
->expire_time
<= current_time
);
757 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
763 if (!ts
|| ts
->expire_time
> current_time
)
765 /* remove timer from the list before calling the callback */
766 *ptimer_head
= ts
->next
;
769 /* run the callback (the timer list can be modified) */
774 int64_t qemu_get_clock(QEMUClock
*clock
)
776 switch(clock
->type
) {
777 case QEMU_TIMER_REALTIME
:
779 return GetTickCount();
784 /* Note that using gettimeofday() is not a good solution
785 for timers because its value change when the date is
787 if (timer_freq
== 100) {
788 return times(&tp
) * 10;
790 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
795 case QEMU_TIMER_VIRTUAL
:
796 return cpu_get_ticks();
801 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
803 uint64_t expire_time
;
805 if (qemu_timer_pending(ts
)) {
806 expire_time
= ts
->expire_time
;
810 qemu_put_be64(f
, expire_time
);
813 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
815 uint64_t expire_time
;
817 expire_time
= qemu_get_be64(f
);
818 if (expire_time
!= -1) {
819 qemu_mod_timer(ts
, expire_time
);
825 static void timer_save(QEMUFile
*f
, void *opaque
)
827 if (cpu_ticks_enabled
) {
828 hw_error("cannot save state if virtual timers are running");
830 qemu_put_be64s(f
, &cpu_ticks_offset
);
831 qemu_put_be64s(f
, &ticks_per_sec
);
834 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
838 if (cpu_ticks_enabled
) {
841 qemu_get_be64s(f
, &cpu_ticks_offset
);
842 qemu_get_be64s(f
, &ticks_per_sec
);
847 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
848 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
850 static void host_alarm_handler(int host_signum
)
854 #define DISP_FREQ 1000
856 static int64_t delta_min
= INT64_MAX
;
857 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
859 ti
= qemu_get_clock(vm_clock
);
860 if (last_clock
!= 0) {
861 delta
= ti
- last_clock
;
862 if (delta
< delta_min
)
864 if (delta
> delta_max
)
867 if (++count
== DISP_FREQ
) {
868 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
869 muldiv64(delta_min
, 1000000, ticks_per_sec
),
870 muldiv64(delta_max
, 1000000, ticks_per_sec
),
871 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
872 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
874 delta_min
= INT64_MAX
;
882 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
883 qemu_get_clock(vm_clock
)) ||
884 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
885 qemu_get_clock(rt_clock
))) {
886 CPUState
*env
= cpu_single_env
;
888 /* stop the currently executing cpu because a timer occured */
889 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
891 if (env
->kqemu_enabled
) {
892 kqemu_cpu_interrupt(env
);
901 #if defined(__linux__)
903 #define RTC_FREQ 1024
907 static int start_rtc_timer(void)
909 rtc_fd
= open("/dev/rtc", O_RDONLY
);
912 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
913 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
914 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
915 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
918 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
923 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
929 static int start_rtc_timer(void)
934 #endif /* !defined(__linux__) */
936 #endif /* !defined(_WIN32) */
938 static void init_timers(void)
940 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
941 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
946 timerID
= timeSetEvent(1, // interval (ms)
948 host_alarm_handler
, // function
949 (DWORD
)&count
, // user parameter
950 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
952 perror("failed timer alarm");
956 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
959 struct sigaction act
;
960 struct itimerval itv
;
962 /* get times() syscall frequency */
963 timer_freq
= sysconf(_SC_CLK_TCK
);
966 sigfillset(&act
.sa_mask
);
968 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
969 act
.sa_flags
|= SA_ONSTACK
;
971 act
.sa_handler
= host_alarm_handler
;
972 sigaction(SIGALRM
, &act
, NULL
);
974 itv
.it_interval
.tv_sec
= 0;
975 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
976 itv
.it_value
.tv_sec
= 0;
977 itv
.it_value
.tv_usec
= 10 * 1000;
978 setitimer(ITIMER_REAL
, &itv
, NULL
);
979 /* we probe the tick duration of the kernel to inform the user if
980 the emulated kernel requested a too high timer frequency */
981 getitimer(ITIMER_REAL
, &itv
);
983 #if defined(__linux__)
984 if (itv
.it_interval
.tv_usec
> 1000) {
985 /* try to use /dev/rtc to have a faster timer */
986 if (start_rtc_timer() < 0)
989 itv
.it_interval
.tv_sec
= 0;
990 itv
.it_interval
.tv_usec
= 0;
991 itv
.it_value
.tv_sec
= 0;
992 itv
.it_value
.tv_usec
= 0;
993 setitimer(ITIMER_REAL
, &itv
, NULL
);
996 sigaction(SIGIO
, &act
, NULL
);
997 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
998 fcntl(rtc_fd
, F_SETOWN
, getpid());
1000 #endif /* defined(__linux__) */
1003 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1004 PIT_FREQ
) / 1000000;
1010 void quit_timers(void)
1013 timeKillEvent(timerID
);
1017 /***********************************************************/
1018 /* character device */
1020 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1022 return s
->chr_write(s
, buf
, len
);
1025 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1029 return s
->chr_ioctl(s
, cmd
, arg
);
1032 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1037 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1038 qemu_chr_write(s
, buf
, strlen(buf
));
1042 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1044 if (s
->chr_send_event
)
1045 s
->chr_send_event(s
, event
);
1048 void qemu_chr_add_read_handler(CharDriverState
*s
,
1049 IOCanRWHandler
*fd_can_read
,
1050 IOReadHandler
*fd_read
, void *opaque
)
1052 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
1055 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
1057 s
->chr_event
= chr_event
;
1060 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1065 static void null_chr_add_read_handler(CharDriverState
*chr
,
1066 IOCanRWHandler
*fd_can_read
,
1067 IOReadHandler
*fd_read
, void *opaque
)
1071 CharDriverState
*qemu_chr_open_null(void)
1073 CharDriverState
*chr
;
1075 chr
= qemu_mallocz(sizeof(CharDriverState
));
1078 chr
->chr_write
= null_chr_write
;
1079 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1085 #define socket_error() WSAGetLastError()
1087 #define EWOULDBLOCK WSAEWOULDBLOCK
1088 #define EINTR WSAEINTR
1089 #define EINPROGRESS WSAEINPROGRESS
1091 static void socket_cleanup(void)
1096 static int socket_init(void)
1101 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1103 err
= WSAGetLastError();
1104 fprintf(stderr
, "WSAStartup: %d\n", err
);
1107 atexit(socket_cleanup
);
1111 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1117 ret
= send(fd
, buf
, len
, 0);
1120 errno
= WSAGetLastError();
1121 if (errno
!= WSAEWOULDBLOCK
) {
1124 } else if (ret
== 0) {
1134 void socket_set_nonblock(int fd
)
1136 unsigned long opt
= 1;
1137 ioctlsocket(fd
, FIONBIO
, &opt
);
1142 #define socket_error() errno
1143 #define closesocket(s) close(s)
1145 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1151 ret
= write(fd
, buf
, len
);
1153 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1155 } else if (ret
== 0) {
1165 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1167 return unix_write(fd
, buf
, len1
);
1170 void socket_set_nonblock(int fd
)
1172 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1174 #endif /* !_WIN32 */
1180 IOCanRWHandler
*fd_can_read
;
1181 IOReadHandler
*fd_read
;
1186 #define STDIO_MAX_CLIENTS 2
1188 static int stdio_nb_clients
;
1189 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1191 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1193 FDCharDriver
*s
= chr
->opaque
;
1194 return unix_write(s
->fd_out
, buf
, len
);
1197 static int fd_chr_read_poll(void *opaque
)
1199 CharDriverState
*chr
= opaque
;
1200 FDCharDriver
*s
= chr
->opaque
;
1202 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
1206 static void fd_chr_read(void *opaque
)
1208 CharDriverState
*chr
= opaque
;
1209 FDCharDriver
*s
= chr
->opaque
;
1214 if (len
> s
->max_size
)
1218 size
= read(s
->fd_in
, buf
, len
);
1220 s
->fd_read(s
->fd_opaque
, buf
, size
);
1224 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1225 IOCanRWHandler
*fd_can_read
,
1226 IOReadHandler
*fd_read
, void *opaque
)
1228 FDCharDriver
*s
= chr
->opaque
;
1230 if (s
->fd_in
>= 0) {
1231 s
->fd_can_read
= fd_can_read
;
1232 s
->fd_read
= fd_read
;
1233 s
->fd_opaque
= opaque
;
1234 if (nographic
&& s
->fd_in
== 0) {
1236 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1237 fd_chr_read
, NULL
, chr
);
1242 /* open a character device to a unix fd */
1243 CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1245 CharDriverState
*chr
;
1248 chr
= qemu_mallocz(sizeof(CharDriverState
));
1251 s
= qemu_mallocz(sizeof(FDCharDriver
));
1259 chr
->chr_write
= fd_chr_write
;
1260 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1264 CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1268 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1271 return qemu_chr_open_fd(-1, fd_out
);
1274 CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1278 fd
= open(filename
, O_RDWR
| O_BINARY
);
1281 return qemu_chr_open_fd(fd
, fd
);
1285 /* for STDIO, we handle the case where several clients use it
1288 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1290 #define TERM_FIFO_MAX_SIZE 1
1292 static int term_got_escape
, client_index
;
1293 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1296 void term_print_help(void)
1299 "C-a h print this help\n"
1300 "C-a x exit emulator\n"
1301 "C-a s save disk data back to file (if -snapshot)\n"
1302 "C-a b send break (magic sysrq)\n"
1303 "C-a c switch between console and monitor\n"
1304 "C-a C-a send C-a\n"
1308 /* called when a char is received */
1309 static void stdio_received_byte(int ch
)
1311 if (term_got_escape
) {
1312 term_got_escape
= 0;
1323 for (i
= 0; i
< MAX_DISKS
; i
++) {
1325 bdrv_commit(bs_table
[i
]);
1330 if (client_index
< stdio_nb_clients
) {
1331 CharDriverState
*chr
;
1334 chr
= stdio_clients
[client_index
];
1336 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1341 if (client_index
>= stdio_nb_clients
)
1343 if (client_index
== 0) {
1344 /* send a new line in the monitor to get the prompt */
1352 } else if (ch
== TERM_ESCAPE
) {
1353 term_got_escape
= 1;
1356 if (client_index
< stdio_nb_clients
) {
1358 CharDriverState
*chr
;
1361 chr
= stdio_clients
[client_index
];
1363 if (s
->fd_can_read(s
->fd_opaque
) > 0) {
1365 s
->fd_read(s
->fd_opaque
, buf
, 1);
1366 } else if (term_fifo_size
== 0) {
1367 term_fifo
[term_fifo_size
++] = ch
;
1373 static int stdio_read_poll(void *opaque
)
1375 CharDriverState
*chr
;
1378 if (client_index
< stdio_nb_clients
) {
1379 chr
= stdio_clients
[client_index
];
1381 /* try to flush the queue if needed */
1382 if (term_fifo_size
!= 0 && s
->fd_can_read(s
->fd_opaque
) > 0) {
1383 s
->fd_read(s
->fd_opaque
, term_fifo
, 1);
1386 /* see if we can absorb more chars */
1387 if (term_fifo_size
== 0)
1396 static void stdio_read(void *opaque
)
1401 size
= read(0, buf
, 1);
1403 stdio_received_byte(buf
[0]);
1406 /* init terminal so that we can grab keys */
1407 static struct termios oldtty
;
1408 static int old_fd0_flags
;
1410 static void term_exit(void)
1412 tcsetattr (0, TCSANOW
, &oldtty
);
1413 fcntl(0, F_SETFL
, old_fd0_flags
);
1416 static void term_init(void)
1420 tcgetattr (0, &tty
);
1422 old_fd0_flags
= fcntl(0, F_GETFL
);
1424 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1425 |INLCR
|IGNCR
|ICRNL
|IXON
);
1426 tty
.c_oflag
|= OPOST
;
1427 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1428 /* if graphical mode, we allow Ctrl-C handling */
1430 tty
.c_lflag
&= ~ISIG
;
1431 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1434 tty
.c_cc
[VTIME
] = 0;
1436 tcsetattr (0, TCSANOW
, &tty
);
1440 fcntl(0, F_SETFL
, O_NONBLOCK
);
1443 CharDriverState
*qemu_chr_open_stdio(void)
1445 CharDriverState
*chr
;
1448 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1450 chr
= qemu_chr_open_fd(0, 1);
1451 if (stdio_nb_clients
== 0)
1452 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, NULL
);
1453 client_index
= stdio_nb_clients
;
1455 if (stdio_nb_clients
!= 0)
1457 chr
= qemu_chr_open_fd(0, 1);
1459 stdio_clients
[stdio_nb_clients
++] = chr
;
1460 if (stdio_nb_clients
== 1) {
1461 /* set the terminal in raw mode */
1467 #if defined(__linux__)
1468 CharDriverState
*qemu_chr_open_pty(void)
1471 char slave_name
[1024];
1472 int master_fd
, slave_fd
;
1474 /* Not satisfying */
1475 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1479 /* Disabling local echo and line-buffered output */
1480 tcgetattr (master_fd
, &tty
);
1481 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1483 tty
.c_cc
[VTIME
] = 0;
1484 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1486 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1487 return qemu_chr_open_fd(master_fd
, master_fd
);
1490 static void tty_serial_init(int fd
, int speed
,
1491 int parity
, int data_bits
, int stop_bits
)
1497 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1498 speed
, parity
, data_bits
, stop_bits
);
1500 tcgetattr (fd
, &tty
);
1542 cfsetispeed(&tty
, spd
);
1543 cfsetospeed(&tty
, spd
);
1545 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1546 |INLCR
|IGNCR
|ICRNL
|IXON
);
1547 tty
.c_oflag
|= OPOST
;
1548 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1549 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
);
1570 tty
.c_cflag
|= PARENB
;
1573 tty
.c_cflag
|= PARENB
| PARODD
;
1577 tcsetattr (fd
, TCSANOW
, &tty
);
1580 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1582 FDCharDriver
*s
= chr
->opaque
;
1585 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1587 QEMUSerialSetParams
*ssp
= arg
;
1588 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1589 ssp
->data_bits
, ssp
->stop_bits
);
1592 case CHR_IOCTL_SERIAL_SET_BREAK
:
1594 int enable
= *(int *)arg
;
1596 tcsendbreak(s
->fd_in
, 1);
1605 CharDriverState
*qemu_chr_open_tty(const char *filename
)
1607 CharDriverState
*chr
;
1610 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1613 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1614 tty_serial_init(fd
, 115200, 'N', 8, 1);
1615 chr
= qemu_chr_open_fd(fd
, fd
);
1618 chr
->chr_ioctl
= tty_serial_ioctl
;
1622 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1624 int fd
= (int)chr
->opaque
;
1628 case CHR_IOCTL_PP_READ_DATA
:
1629 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1631 *(uint8_t *)arg
= b
;
1633 case CHR_IOCTL_PP_WRITE_DATA
:
1634 b
= *(uint8_t *)arg
;
1635 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1638 case CHR_IOCTL_PP_READ_CONTROL
:
1639 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1641 *(uint8_t *)arg
= b
;
1643 case CHR_IOCTL_PP_WRITE_CONTROL
:
1644 b
= *(uint8_t *)arg
;
1645 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1648 case CHR_IOCTL_PP_READ_STATUS
:
1649 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1651 *(uint8_t *)arg
= b
;
1659 CharDriverState
*qemu_chr_open_pp(const char *filename
)
1661 CharDriverState
*chr
;
1664 fd
= open(filename
, O_RDWR
);
1668 if (ioctl(fd
, PPCLAIM
) < 0) {
1673 chr
= qemu_mallocz(sizeof(CharDriverState
));
1678 chr
->opaque
= (void *)fd
;
1679 chr
->chr_write
= null_chr_write
;
1680 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1681 chr
->chr_ioctl
= pp_ioctl
;
1686 CharDriverState
*qemu_chr_open_pty(void)
1692 #endif /* !defined(_WIN32) */
1696 IOCanRWHandler
*fd_can_read
;
1697 IOReadHandler
*fd_read
;
1700 HANDLE hcom
, hrecv
, hsend
;
1701 OVERLAPPED orecv
, osend
;
1706 #define NSENDBUF 2048
1707 #define NRECVBUF 2048
1708 #define MAXCONNECT 1
1709 #define NTIMEOUT 5000
1711 static int win_chr_poll(void *opaque
);
1712 static int win_chr_pipe_poll(void *opaque
);
1714 static void win_chr_close2(WinCharState
*s
)
1717 CloseHandle(s
->hsend
);
1721 CloseHandle(s
->hrecv
);
1725 CloseHandle(s
->hcom
);
1729 qemu_del_polling_cb(win_chr_pipe_poll
, s
);
1731 qemu_del_polling_cb(win_chr_poll
, s
);
1734 static void win_chr_close(CharDriverState
*chr
)
1736 WinCharState
*s
= chr
->opaque
;
1740 static int win_chr_init(WinCharState
*s
, const char *filename
)
1743 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1748 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1750 fprintf(stderr
, "Failed CreateEvent\n");
1753 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1755 fprintf(stderr
, "Failed CreateEvent\n");
1759 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1760 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1761 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1762 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1767 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1768 fprintf(stderr
, "Failed SetupComm\n");
1772 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1773 size
= sizeof(COMMCONFIG
);
1774 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1775 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1776 CommConfigDialog(filename
, NULL
, &comcfg
);
1778 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1779 fprintf(stderr
, "Failed SetCommState\n");
1783 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1784 fprintf(stderr
, "Failed SetCommMask\n");
1788 cto
.ReadIntervalTimeout
= MAXDWORD
;
1789 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1790 fprintf(stderr
, "Failed SetCommTimeouts\n");
1794 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1795 fprintf(stderr
, "Failed ClearCommError\n");
1798 qemu_add_polling_cb(win_chr_poll
, s
);
1806 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1808 WinCharState
*s
= chr
->opaque
;
1809 DWORD len
, ret
, size
, err
;
1812 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1813 s
->osend
.hEvent
= s
->hsend
;
1816 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1818 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1820 err
= GetLastError();
1821 if (err
== ERROR_IO_PENDING
) {
1822 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1840 static int win_chr_read_poll(WinCharState
*s
)
1842 s
->max_size
= s
->fd_can_read(s
->win_opaque
);
1846 static void win_chr_readfile(WinCharState
*s
)
1852 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1853 s
->orecv
.hEvent
= s
->hrecv
;
1854 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1856 err
= GetLastError();
1857 if (err
== ERROR_IO_PENDING
) {
1858 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1863 s
->fd_read(s
->win_opaque
, buf
, size
);
1867 static void win_chr_read(WinCharState
*s
)
1869 if (s
->len
> s
->max_size
)
1870 s
->len
= s
->max_size
;
1874 win_chr_readfile(s
);
1877 static int win_chr_poll(void *opaque
)
1879 WinCharState
*s
= opaque
;
1883 ClearCommError(s
->hcom
, &comerr
, &status
);
1884 if (status
.cbInQue
> 0) {
1885 s
->len
= status
.cbInQue
;
1886 win_chr_read_poll(s
);
1893 static void win_chr_add_read_handler(CharDriverState
*chr
,
1894 IOCanRWHandler
*fd_can_read
,
1895 IOReadHandler
*fd_read
, void *opaque
)
1897 WinCharState
*s
= chr
->opaque
;
1899 s
->fd_can_read
= fd_can_read
;
1900 s
->fd_read
= fd_read
;
1901 s
->win_opaque
= opaque
;
1904 CharDriverState
*qemu_chr_open_win(const char *filename
)
1906 CharDriverState
*chr
;
1909 chr
= qemu_mallocz(sizeof(CharDriverState
));
1912 s
= qemu_mallocz(sizeof(WinCharState
));
1918 chr
->chr_write
= win_chr_write
;
1919 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
1920 chr
->chr_close
= win_chr_close
;
1922 if (win_chr_init(s
, filename
) < 0) {
1930 static int win_chr_pipe_poll(void *opaque
)
1932 WinCharState
*s
= opaque
;
1935 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1938 win_chr_read_poll(s
);
1945 static int win_chr_pipe_init(WinCharState
*s
, const char *filename
)
1954 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1956 fprintf(stderr
, "Failed CreateEvent\n");
1959 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1961 fprintf(stderr
, "Failed CreateEvent\n");
1965 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1966 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1967 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1969 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1970 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1971 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1976 ZeroMemory(&ov
, sizeof(ov
));
1977 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1978 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1980 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1984 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1986 fprintf(stderr
, "Failed GetOverlappedResult\n");
1988 CloseHandle(ov
.hEvent
);
1995 CloseHandle(ov
.hEvent
);
1998 qemu_add_polling_cb(win_chr_pipe_poll
, s
);
2007 CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2009 CharDriverState
*chr
;
2012 chr
= qemu_mallocz(sizeof(CharDriverState
));
2015 s
= qemu_mallocz(sizeof(WinCharState
));
2021 chr
->chr_write
= win_chr_write
;
2022 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2023 chr
->chr_close
= win_chr_close
;
2025 if (win_chr_pipe_init(s
, filename
) < 0) {
2033 CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2035 CharDriverState
*chr
;
2038 chr
= qemu_mallocz(sizeof(CharDriverState
));
2041 s
= qemu_mallocz(sizeof(WinCharState
));
2048 chr
->chr_write
= win_chr_write
;
2049 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2053 CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2057 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2058 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2059 if (fd_out
== INVALID_HANDLE_VALUE
)
2062 return qemu_chr_open_win_file(fd_out
);
2066 CharDriverState
*qemu_chr_open(const char *filename
)
2070 if (!strcmp(filename
, "vc")) {
2071 return text_console_init(&display_state
);
2072 } else if (!strcmp(filename
, "null")) {
2073 return qemu_chr_open_null();
2076 if (strstart(filename
, "file:", &p
)) {
2077 return qemu_chr_open_file_out(p
);
2078 } else if (strstart(filename
, "pipe:", &p
)) {
2079 return qemu_chr_open_pipe(p
);
2080 } else if (!strcmp(filename
, "pty")) {
2081 return qemu_chr_open_pty();
2082 } else if (!strcmp(filename
, "stdio")) {
2083 return qemu_chr_open_stdio();
2086 #if defined(__linux__)
2087 if (strstart(filename
, "/dev/parport", NULL
)) {
2088 return qemu_chr_open_pp(filename
);
2090 if (strstart(filename
, "/dev/", NULL
)) {
2091 return qemu_chr_open_tty(filename
);
2095 if (strstart(filename
, "COM", NULL
)) {
2096 return qemu_chr_open_win(filename
);
2098 if (strstart(filename
, "pipe:", &p
)) {
2099 return qemu_chr_open_win_pipe(p
);
2101 if (strstart(filename
, "file:", &p
)) {
2102 return qemu_chr_open_win_file_out(p
);
2110 void qemu_chr_close(CharDriverState
*chr
)
2113 chr
->chr_close(chr
);
2116 /***********************************************************/
2117 /* network device redirectors */
2119 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2123 for(i
=0;i
<size
;i
+=16) {
2127 fprintf(f
, "%08x ", i
);
2130 fprintf(f
, " %02x", buf
[i
+j
]);
2135 for(j
=0;j
<len
;j
++) {
2137 if (c
< ' ' || c
> '~')
2139 fprintf(f
, "%c", c
);
2145 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
2148 for(i
= 0; i
< 6; i
++) {
2149 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2162 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
2167 p1
= strchr(p
, sep
);
2173 if (len
> buf_size
- 1)
2175 memcpy(buf
, p
, len
);
2182 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
2190 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2192 saddr
->sin_family
= AF_INET
;
2193 if (buf
[0] == '\0') {
2194 saddr
->sin_addr
.s_addr
= 0;
2196 if (isdigit(buf
[0])) {
2197 if (!inet_aton(buf
, &saddr
->sin_addr
))
2200 if ((he
= gethostbyname(buf
)) == NULL
)
2202 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
2205 port
= strtol(p
, (char **)&r
, 0);
2208 saddr
->sin_port
= htons(port
);
2212 /* find or alloc a new VLAN */
2213 VLANState
*qemu_find_vlan(int id
)
2215 VLANState
**pvlan
, *vlan
;
2216 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2220 vlan
= qemu_mallocz(sizeof(VLANState
));
2225 pvlan
= &first_vlan
;
2226 while (*pvlan
!= NULL
)
2227 pvlan
= &(*pvlan
)->next
;
2232 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
2233 IOReadHandler
*fd_read
,
2234 IOCanRWHandler
*fd_can_read
,
2237 VLANClientState
*vc
, **pvc
;
2238 vc
= qemu_mallocz(sizeof(VLANClientState
));
2241 vc
->fd_read
= fd_read
;
2242 vc
->fd_can_read
= fd_can_read
;
2243 vc
->opaque
= opaque
;
2247 pvc
= &vlan
->first_client
;
2248 while (*pvc
!= NULL
)
2249 pvc
= &(*pvc
)->next
;
2254 int qemu_can_send_packet(VLANClientState
*vc1
)
2256 VLANState
*vlan
= vc1
->vlan
;
2257 VLANClientState
*vc
;
2259 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2261 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
2268 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
2270 VLANState
*vlan
= vc1
->vlan
;
2271 VLANClientState
*vc
;
2274 printf("vlan %d send:\n", vlan
->id
);
2275 hex_dump(stdout
, buf
, size
);
2277 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2279 vc
->fd_read(vc
->opaque
, buf
, size
);
2284 #if defined(CONFIG_SLIRP)
2286 /* slirp network adapter */
2288 static int slirp_inited
;
2289 static VLANClientState
*slirp_vc
;
2291 int slirp_can_output(void)
2293 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
2296 void slirp_output(const uint8_t *pkt
, int pkt_len
)
2299 printf("slirp output:\n");
2300 hex_dump(stdout
, pkt
, pkt_len
);
2304 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
2307 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
2310 printf("slirp input:\n");
2311 hex_dump(stdout
, buf
, size
);
2313 slirp_input(buf
, size
);
2316 static int net_slirp_init(VLANState
*vlan
)
2318 if (!slirp_inited
) {
2322 slirp_vc
= qemu_new_vlan_client(vlan
,
2323 slirp_receive
, NULL
, NULL
);
2324 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
2328 static void net_slirp_redir(const char *redir_str
)
2333 struct in_addr guest_addr
;
2334 int host_port
, guest_port
;
2336 if (!slirp_inited
) {
2342 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2344 if (!strcmp(buf
, "tcp")) {
2346 } else if (!strcmp(buf
, "udp")) {
2352 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2354 host_port
= strtol(buf
, &r
, 0);
2358 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2360 if (buf
[0] == '\0') {
2361 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
2363 if (!inet_aton(buf
, &guest_addr
))
2366 guest_port
= strtol(p
, &r
, 0);
2370 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
2371 fprintf(stderr
, "qemu: could not set up redirection\n");
2376 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
2384 static void smb_exit(void)
2388 char filename
[1024];
2390 /* erase all the files in the directory */
2391 d
= opendir(smb_dir
);
2396 if (strcmp(de
->d_name
, ".") != 0 &&
2397 strcmp(de
->d_name
, "..") != 0) {
2398 snprintf(filename
, sizeof(filename
), "%s/%s",
2399 smb_dir
, de
->d_name
);
2407 /* automatic user mode samba server configuration */
2408 void net_slirp_smb(const char *exported_dir
)
2410 char smb_conf
[1024];
2411 char smb_cmdline
[1024];
2414 if (!slirp_inited
) {
2419 /* XXX: better tmp dir construction */
2420 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
2421 if (mkdir(smb_dir
, 0700) < 0) {
2422 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
2425 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
2427 f
= fopen(smb_conf
, "w");
2429 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
2436 "socket address=127.0.0.1\n"
2437 "pid directory=%s\n"
2438 "lock directory=%s\n"
2439 "log file=%s/log.smbd\n"
2440 "smb passwd file=%s/smbpasswd\n"
2441 "security = share\n"
2456 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "/usr/sbin/smbd -s %s",
2459 slirp_add_exec(0, smb_cmdline
, 4, 139);
2462 #endif /* !defined(_WIN32) */
2464 #endif /* CONFIG_SLIRP */
2466 #if !defined(_WIN32)
2468 typedef struct TAPState
{
2469 VLANClientState
*vc
;
2473 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
2475 TAPState
*s
= opaque
;
2478 ret
= write(s
->fd
, buf
, size
);
2479 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
2486 static void tap_send(void *opaque
)
2488 TAPState
*s
= opaque
;
2492 size
= read(s
->fd
, buf
, sizeof(buf
));
2494 qemu_send_packet(s
->vc
, buf
, size
);
2500 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
2504 s
= qemu_mallocz(sizeof(TAPState
));
2508 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
2509 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
2510 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
2515 static int tap_open(char *ifname
, int ifname_size
)
2521 fd
= open("/dev/tap", O_RDWR
);
2523 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
2528 dev
= devname(s
.st_rdev
, S_IFCHR
);
2529 pstrcpy(ifname
, ifname_size
, dev
);
2531 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2535 static int tap_open(char *ifname
, int ifname_size
)
2540 fd
= open("/dev/net/tun", O_RDWR
);
2542 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
2545 memset(&ifr
, 0, sizeof(ifr
));
2546 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
2547 if (ifname
[0] != '\0')
2548 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
2550 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
2551 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
2553 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
2557 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
2558 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2563 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
2564 const char *setup_script
)
2567 int pid
, status
, fd
;
2572 if (ifname1
!= NULL
)
2573 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
2576 fd
= tap_open(ifname
, sizeof(ifname
));
2582 if (setup_script
[0] != '\0') {
2583 /* try to launch network init script */
2588 *parg
++ = (char *)setup_script
;
2591 execv(setup_script
, args
);
2594 while (waitpid(pid
, &status
, 0) != pid
);
2595 if (!WIFEXITED(status
) ||
2596 WEXITSTATUS(status
) != 0) {
2597 fprintf(stderr
, "%s: could not launch network script\n",
2603 s
= net_tap_fd_init(vlan
, fd
);
2606 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2607 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
2611 #endif /* !_WIN32 */
2613 /* network connection */
2614 typedef struct NetSocketState
{
2615 VLANClientState
*vc
;
2617 int state
; /* 0 = getting length, 1 = getting data */
2621 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
2624 typedef struct NetSocketListenState
{
2627 } NetSocketListenState
;
2629 /* XXX: we consider we can send the whole packet without blocking */
2630 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
2632 NetSocketState
*s
= opaque
;
2636 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
2637 send_all(s
->fd
, buf
, size
);
2640 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
2642 NetSocketState
*s
= opaque
;
2643 sendto(s
->fd
, buf
, size
, 0,
2644 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
2647 static void net_socket_send(void *opaque
)
2649 NetSocketState
*s
= opaque
;
2654 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
2656 err
= socket_error();
2657 if (err
!= EWOULDBLOCK
)
2659 } else if (size
== 0) {
2660 /* end of connection */
2662 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2668 /* reassemble a packet from the network */
2674 memcpy(s
->buf
+ s
->index
, buf
, l
);
2678 if (s
->index
== 4) {
2680 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
2686 l
= s
->packet_len
- s
->index
;
2689 memcpy(s
->buf
+ s
->index
, buf
, l
);
2693 if (s
->index
>= s
->packet_len
) {
2694 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
2703 static void net_socket_send_dgram(void *opaque
)
2705 NetSocketState
*s
= opaque
;
2708 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2712 /* end of connection */
2713 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2716 qemu_send_packet(s
->vc
, s
->buf
, size
);
2719 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
2724 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
2725 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
2726 inet_ntoa(mcastaddr
->sin_addr
),
2727 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
2731 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2733 perror("socket(PF_INET, SOCK_DGRAM)");
2738 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
2739 (const char *)&val
, sizeof(val
));
2741 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
2745 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
2751 /* Add host to multicast group */
2752 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
2753 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
2755 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
2756 (const char *)&imr
, sizeof(struct ip_mreq
));
2758 perror("setsockopt(IP_ADD_MEMBERSHIP)");
2762 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
2764 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
2765 (const char *)&val
, sizeof(val
));
2767 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
2771 socket_set_nonblock(fd
);
2774 if (fd
>=0) close(fd
);
2778 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
2781 struct sockaddr_in saddr
;
2783 socklen_t saddr_len
;
2786 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
2787 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
2788 * by ONLY ONE process: we must "clone" this dgram socket --jjo
2792 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
2794 if (saddr
.sin_addr
.s_addr
==0) {
2795 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
2799 /* clone dgram socket */
2800 newfd
= net_socket_mcast_create(&saddr
);
2802 /* error already reported by net_socket_mcast_create() */
2806 /* clone newfd to fd, close newfd */
2811 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
2812 fd
, strerror(errno
));
2817 s
= qemu_mallocz(sizeof(NetSocketState
));
2822 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
2823 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
2825 /* mcast: save bound address as dst */
2826 if (is_connected
) s
->dgram_dst
=saddr
;
2828 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2829 "socket: fd=%d (%s mcast=%s:%d)",
2830 fd
, is_connected
? "cloned" : "",
2831 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2835 static void net_socket_connect(void *opaque
)
2837 NetSocketState
*s
= opaque
;
2838 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
2841 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
2845 s
= qemu_mallocz(sizeof(NetSocketState
));
2849 s
->vc
= qemu_new_vlan_client(vlan
,
2850 net_socket_receive
, NULL
, s
);
2851 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2852 "socket: fd=%d", fd
);
2854 net_socket_connect(s
);
2856 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
2861 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
2864 int so_type
=-1, optlen
=sizeof(so_type
);
2866 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
2867 fprintf(stderr
, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd
);
2872 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
2874 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
2876 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
2877 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
2878 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
2883 static void net_socket_accept(void *opaque
)
2885 NetSocketListenState
*s
= opaque
;
2887 struct sockaddr_in saddr
;
2892 len
= sizeof(saddr
);
2893 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
2894 if (fd
< 0 && errno
!= EINTR
) {
2896 } else if (fd
>= 0) {
2900 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
2904 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
2905 "socket: connection from %s:%d",
2906 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2910 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
2912 NetSocketListenState
*s
;
2914 struct sockaddr_in saddr
;
2916 if (parse_host_port(&saddr
, host_str
) < 0)
2919 s
= qemu_mallocz(sizeof(NetSocketListenState
));
2923 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2928 socket_set_nonblock(fd
);
2930 /* allow fast reuse */
2932 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2934 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2939 ret
= listen(fd
, 0);
2946 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
2950 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
2953 int fd
, connected
, ret
, err
;
2954 struct sockaddr_in saddr
;
2956 if (parse_host_port(&saddr
, host_str
) < 0)
2959 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2964 socket_set_nonblock(fd
);
2968 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2970 err
= socket_error();
2971 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2972 } else if (err
== EINPROGRESS
) {
2984 s
= net_socket_fd_init(vlan
, fd
, connected
);
2987 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2988 "socket: connect to %s:%d",
2989 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2993 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
2997 struct sockaddr_in saddr
;
2999 if (parse_host_port(&saddr
, host_str
) < 0)
3003 fd
= net_socket_mcast_create(&saddr
);
3007 s
= net_socket_fd_init(vlan
, fd
, 0);
3011 s
->dgram_dst
= saddr
;
3013 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3014 "socket: mcast=%s:%d",
3015 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3020 static int get_param_value(char *buf
, int buf_size
,
3021 const char *tag
, const char *str
)
3030 while (*p
!= '\0' && *p
!= '=') {
3031 if ((q
- option
) < sizeof(option
) - 1)
3039 if (!strcmp(tag
, option
)) {
3041 while (*p
!= '\0' && *p
!= ',') {
3042 if ((q
- buf
) < buf_size
- 1)
3049 while (*p
!= '\0' && *p
!= ',') {
3060 int net_client_init(const char *str
)
3071 while (*p
!= '\0' && *p
!= ',') {
3072 if ((q
- device
) < sizeof(device
) - 1)
3080 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
3081 vlan_id
= strtol(buf
, NULL
, 0);
3083 vlan
= qemu_find_vlan(vlan_id
);
3085 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
3088 if (!strcmp(device
, "nic")) {
3092 if (nb_nics
>= MAX_NICS
) {
3093 fprintf(stderr
, "Too Many NICs\n");
3096 nd
= &nd_table
[nb_nics
];
3097 macaddr
= nd
->macaddr
;
3103 macaddr
[5] = 0x56 + nb_nics
;
3105 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
3106 if (parse_macaddr(macaddr
, buf
) < 0) {
3107 fprintf(stderr
, "invalid syntax for ethernet address\n");
3111 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
3112 nd
->model
= strdup(buf
);
3118 if (!strcmp(device
, "none")) {
3119 /* does nothing. It is needed to signal that no network cards
3124 if (!strcmp(device
, "user")) {
3125 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
3126 if (strlen(buf
) > 32)
3128 strcpy(slirp_hostname
, buf
);
3130 ret
= net_slirp_init(vlan
);
3134 if (!strcmp(device
, "tap")) {
3136 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
3137 fprintf(stderr
, "tap: no interface name\n");
3140 ret
= tap_win32_init(vlan
, ifname
);
3143 if (!strcmp(device
, "tap")) {
3145 char setup_script
[1024];
3147 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3148 fd
= strtol(buf
, NULL
, 0);
3150 if (net_tap_fd_init(vlan
, fd
))
3153 get_param_value(ifname
, sizeof(ifname
), "ifname", p
);
3154 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
3155 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
3157 ret
= net_tap_init(vlan
, ifname
, setup_script
);
3161 if (!strcmp(device
, "socket")) {
3162 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3164 fd
= strtol(buf
, NULL
, 0);
3166 if (net_socket_fd_init(vlan
, fd
, 1))
3168 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
3169 ret
= net_socket_listen_init(vlan
, buf
);
3170 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
3171 ret
= net_socket_connect_init(vlan
, buf
);
3172 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
3173 ret
= net_socket_mcast_init(vlan
, buf
);
3175 fprintf(stderr
, "Unknown socket options: %s\n", p
);
3180 fprintf(stderr
, "Unknown network device: %s\n", device
);
3184 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
3190 void do_info_network(void)
3193 VLANClientState
*vc
;
3195 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3196 term_printf("VLAN %d devices:\n", vlan
->id
);
3197 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3198 term_printf(" %s\n", vc
->info_str
);
3202 /***********************************************************/
3205 static int usb_device_add(const char *devname
)
3213 for(i
= 0;i
< MAX_VM_USB_PORTS
; i
++) {
3214 if (!vm_usb_ports
[i
]->dev
)
3217 if (i
== MAX_VM_USB_PORTS
)
3220 if (strstart(devname
, "host:", &p
)) {
3221 dev
= usb_host_device_open(p
);
3224 } else if (!strcmp(devname
, "mouse")) {
3225 dev
= usb_mouse_init();
3228 } else if (!strcmp(devname
, "tablet")) {
3229 dev
= usb_tablet_init();
3235 usb_attach(vm_usb_ports
[i
], dev
);
3239 static int usb_device_del(const char *devname
)
3242 int bus_num
, addr
, i
;
3248 p
= strchr(devname
, '.');
3251 bus_num
= strtoul(devname
, NULL
, 0);
3252 addr
= strtoul(p
+ 1, NULL
, 0);
3255 for(i
= 0;i
< MAX_VM_USB_PORTS
; i
++) {
3256 dev
= vm_usb_ports
[i
]->dev
;
3257 if (dev
&& dev
->addr
== addr
)
3260 if (i
== MAX_VM_USB_PORTS
)
3262 usb_attach(vm_usb_ports
[i
], NULL
);
3266 void do_usb_add(const char *devname
)
3269 ret
= usb_device_add(devname
);
3271 term_printf("Could not add USB device '%s'\n", devname
);
3274 void do_usb_del(const char *devname
)
3277 ret
= usb_device_del(devname
);
3279 term_printf("Could not remove USB device '%s'\n", devname
);
3286 const char *speed_str
;
3289 term_printf("USB support not enabled\n");
3293 for(i
= 0; i
< MAX_VM_USB_PORTS
; i
++) {
3294 dev
= vm_usb_ports
[i
]->dev
;
3296 term_printf("Hub port %d:\n", i
);
3297 switch(dev
->speed
) {
3301 case USB_SPEED_FULL
:
3304 case USB_SPEED_HIGH
:
3311 term_printf(" Device %d.%d, speed %s Mb/s\n",
3312 0, dev
->addr
, speed_str
);
3317 /***********************************************************/
3320 static char *pid_filename
;
3322 /* Remove PID file. Called on normal exit */
3324 static void remove_pidfile(void)
3326 unlink (pid_filename
);
3329 static void create_pidfile(const char *filename
)
3331 struct stat pidstat
;
3334 /* Try to write our PID to the named file */
3335 if (stat(filename
, &pidstat
) < 0) {
3336 if (errno
== ENOENT
) {
3337 if ((f
= fopen (filename
, "w")) == NULL
) {
3338 perror("Opening pidfile");
3341 fprintf(f
, "%d\n", getpid());
3343 pid_filename
= qemu_strdup(filename
);
3344 if (!pid_filename
) {
3345 fprintf(stderr
, "Could not save PID filename");
3348 atexit(remove_pidfile
);
3351 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
3357 /***********************************************************/
3360 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
3364 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
3368 static void dumb_refresh(DisplayState
*ds
)
3373 void dumb_display_init(DisplayState
*ds
)
3378 ds
->dpy_update
= dumb_update
;
3379 ds
->dpy_resize
= dumb_resize
;
3380 ds
->dpy_refresh
= dumb_refresh
;
3383 #if !defined(CONFIG_SOFTMMU)
3384 /***********************************************************/
3385 /* cpu signal handler */
3386 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
3389 if (cpu_signal_handler(host_signum
, info
, puc
))
3391 if (stdio_nb_clients
> 0)
3397 /***********************************************************/
3400 #define MAX_IO_HANDLERS 64
3402 typedef struct IOHandlerRecord
{
3404 IOCanRWHandler
*fd_read_poll
;
3406 IOHandler
*fd_write
;
3408 /* temporary data */
3410 struct IOHandlerRecord
*next
;
3413 static IOHandlerRecord
*first_io_handler
;
3415 /* XXX: fd_read_poll should be suppressed, but an API change is
3416 necessary in the character devices to suppress fd_can_read(). */
3417 int qemu_set_fd_handler2(int fd
,
3418 IOCanRWHandler
*fd_read_poll
,
3420 IOHandler
*fd_write
,
3423 IOHandlerRecord
**pioh
, *ioh
;
3425 if (!fd_read
&& !fd_write
) {
3426 pioh
= &first_io_handler
;
3431 if (ioh
->fd
== fd
) {
3439 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3443 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
3446 ioh
->next
= first_io_handler
;
3447 first_io_handler
= ioh
;
3450 ioh
->fd_read_poll
= fd_read_poll
;
3451 ioh
->fd_read
= fd_read
;
3452 ioh
->fd_write
= fd_write
;
3453 ioh
->opaque
= opaque
;
3458 int qemu_set_fd_handler(int fd
,
3460 IOHandler
*fd_write
,
3463 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
3466 /***********************************************************/
3467 /* Polling handling */
3469 typedef struct PollingEntry
{
3472 struct PollingEntry
*next
;
3475 static PollingEntry
*first_polling_entry
;
3477 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
3479 PollingEntry
**ppe
, *pe
;
3480 pe
= qemu_mallocz(sizeof(PollingEntry
));
3484 pe
->opaque
= opaque
;
3485 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
3490 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3492 PollingEntry
**ppe
, *pe
;
3493 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3495 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3503 /***********************************************************/
3504 /* savevm/loadvm support */
3506 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
3508 fwrite(buf
, 1, size
, f
);
3511 void qemu_put_byte(QEMUFile
*f
, int v
)
3516 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
3518 qemu_put_byte(f
, v
>> 8);
3519 qemu_put_byte(f
, v
);
3522 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
3524 qemu_put_byte(f
, v
>> 24);
3525 qemu_put_byte(f
, v
>> 16);
3526 qemu_put_byte(f
, v
>> 8);
3527 qemu_put_byte(f
, v
);
3530 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
3532 qemu_put_be32(f
, v
>> 32);
3533 qemu_put_be32(f
, v
);
3536 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
3538 return fread(buf
, 1, size
, f
);
3541 int qemu_get_byte(QEMUFile
*f
)
3551 unsigned int qemu_get_be16(QEMUFile
*f
)
3554 v
= qemu_get_byte(f
) << 8;
3555 v
|= qemu_get_byte(f
);
3559 unsigned int qemu_get_be32(QEMUFile
*f
)
3562 v
= qemu_get_byte(f
) << 24;
3563 v
|= qemu_get_byte(f
) << 16;
3564 v
|= qemu_get_byte(f
) << 8;
3565 v
|= qemu_get_byte(f
);
3569 uint64_t qemu_get_be64(QEMUFile
*f
)
3572 v
= (uint64_t)qemu_get_be32(f
) << 32;
3573 v
|= qemu_get_be32(f
);
3577 int64_t qemu_ftell(QEMUFile
*f
)
3582 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
3584 if (fseek(f
, pos
, whence
) < 0)
3589 typedef struct SaveStateEntry
{
3593 SaveStateHandler
*save_state
;
3594 LoadStateHandler
*load_state
;
3596 struct SaveStateEntry
*next
;
3599 static SaveStateEntry
*first_se
;
3601 int register_savevm(const char *idstr
,
3604 SaveStateHandler
*save_state
,
3605 LoadStateHandler
*load_state
,
3608 SaveStateEntry
*se
, **pse
;
3610 se
= qemu_malloc(sizeof(SaveStateEntry
));
3613 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
3614 se
->instance_id
= instance_id
;
3615 se
->version_id
= version_id
;
3616 se
->save_state
= save_state
;
3617 se
->load_state
= load_state
;
3618 se
->opaque
= opaque
;
3621 /* add at the end of list */
3623 while (*pse
!= NULL
)
3624 pse
= &(*pse
)->next
;
3629 #define QEMU_VM_FILE_MAGIC 0x5145564d
3630 #define QEMU_VM_FILE_VERSION 0x00000001
3632 int qemu_savevm(const char *filename
)
3636 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
3638 saved_vm_running
= vm_running
;
3641 f
= fopen(filename
, "wb");
3647 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
3648 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
3650 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3652 len
= strlen(se
->idstr
);
3653 qemu_put_byte(f
, len
);
3654 qemu_put_buffer(f
, se
->idstr
, len
);
3656 qemu_put_be32(f
, se
->instance_id
);
3657 qemu_put_be32(f
, se
->version_id
);
3659 /* record size: filled later */
3661 qemu_put_be32(f
, 0);
3663 se
->save_state(f
, se
->opaque
);
3665 /* fill record size */
3667 len
= ftell(f
) - len_pos
- 4;
3668 fseek(f
, len_pos
, SEEK_SET
);
3669 qemu_put_be32(f
, len
);
3670 fseek(f
, cur_pos
, SEEK_SET
);
3676 if (saved_vm_running
)
3681 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
3685 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3686 if (!strcmp(se
->idstr
, idstr
) &&
3687 instance_id
== se
->instance_id
)
3693 int qemu_loadvm(const char *filename
)
3697 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
3698 int saved_vm_running
;
3702 saved_vm_running
= vm_running
;
3705 f
= fopen(filename
, "rb");
3711 v
= qemu_get_be32(f
);
3712 if (v
!= QEMU_VM_FILE_MAGIC
)
3714 v
= qemu_get_be32(f
);
3715 if (v
!= QEMU_VM_FILE_VERSION
) {
3722 len
= qemu_get_byte(f
);
3725 qemu_get_buffer(f
, idstr
, len
);
3727 instance_id
= qemu_get_be32(f
);
3728 version_id
= qemu_get_be32(f
);
3729 record_len
= qemu_get_be32(f
);
3731 printf("idstr=%s instance=0x%x version=%d len=%d\n",
3732 idstr
, instance_id
, version_id
, record_len
);
3735 se
= find_se(idstr
, instance_id
);
3737 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
3738 instance_id
, idstr
);
3740 ret
= se
->load_state(f
, se
->opaque
, version_id
);
3742 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
3743 instance_id
, idstr
);
3746 /* always seek to exact end of record */
3747 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
3752 if (saved_vm_running
)
3757 /***********************************************************/
3758 /* cpu save/restore */
3760 #if defined(TARGET_I386)
3762 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
3764 qemu_put_be32(f
, dt
->selector
);
3765 qemu_put_betl(f
, dt
->base
);
3766 qemu_put_be32(f
, dt
->limit
);
3767 qemu_put_be32(f
, dt
->flags
);
3770 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
3772 dt
->selector
= qemu_get_be32(f
);
3773 dt
->base
= qemu_get_betl(f
);
3774 dt
->limit
= qemu_get_be32(f
);
3775 dt
->flags
= qemu_get_be32(f
);
3778 void cpu_save(QEMUFile
*f
, void *opaque
)
3780 CPUState
*env
= opaque
;
3781 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
3785 for(i
= 0; i
< CPU_NB_REGS
; i
++)
3786 qemu_put_betls(f
, &env
->regs
[i
]);
3787 qemu_put_betls(f
, &env
->eip
);
3788 qemu_put_betls(f
, &env
->eflags
);
3789 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
3790 qemu_put_be32s(f
, &hflags
);
3794 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
3796 for(i
= 0; i
< 8; i
++) {
3797 fptag
|= ((!env
->fptags
[i
]) << i
);
3800 qemu_put_be16s(f
, &fpuc
);
3801 qemu_put_be16s(f
, &fpus
);
3802 qemu_put_be16s(f
, &fptag
);
3804 #ifdef USE_X86LDOUBLE
3809 qemu_put_be16s(f
, &fpregs_format
);
3811 for(i
= 0; i
< 8; i
++) {
3812 #ifdef USE_X86LDOUBLE
3816 /* we save the real CPU data (in case of MMX usage only 'mant'
3817 contains the MMX register */
3818 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
3819 qemu_put_be64(f
, mant
);
3820 qemu_put_be16(f
, exp
);
3823 /* if we use doubles for float emulation, we save the doubles to
3824 avoid losing information in case of MMX usage. It can give
3825 problems if the image is restored on a CPU where long
3826 doubles are used instead. */
3827 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
3831 for(i
= 0; i
< 6; i
++)
3832 cpu_put_seg(f
, &env
->segs
[i
]);
3833 cpu_put_seg(f
, &env
->ldt
);
3834 cpu_put_seg(f
, &env
->tr
);
3835 cpu_put_seg(f
, &env
->gdt
);
3836 cpu_put_seg(f
, &env
->idt
);
3838 qemu_put_be32s(f
, &env
->sysenter_cs
);
3839 qemu_put_be32s(f
, &env
->sysenter_esp
);
3840 qemu_put_be32s(f
, &env
->sysenter_eip
);
3842 qemu_put_betls(f
, &env
->cr
[0]);
3843 qemu_put_betls(f
, &env
->cr
[2]);
3844 qemu_put_betls(f
, &env
->cr
[3]);
3845 qemu_put_betls(f
, &env
->cr
[4]);
3847 for(i
= 0; i
< 8; i
++)
3848 qemu_put_betls(f
, &env
->dr
[i
]);
3851 qemu_put_be32s(f
, &env
->a20_mask
);
3854 qemu_put_be32s(f
, &env
->mxcsr
);
3855 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
3856 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
3857 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
3860 #ifdef TARGET_X86_64
3861 qemu_put_be64s(f
, &env
->efer
);
3862 qemu_put_be64s(f
, &env
->star
);
3863 qemu_put_be64s(f
, &env
->lstar
);
3864 qemu_put_be64s(f
, &env
->cstar
);
3865 qemu_put_be64s(f
, &env
->fmask
);
3866 qemu_put_be64s(f
, &env
->kernelgsbase
);
3870 #ifdef USE_X86LDOUBLE
3871 /* XXX: add that in a FPU generic layer */
3872 union x86_longdouble
{
3877 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
3878 #define EXPBIAS1 1023
3879 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
3880 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
3882 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
3886 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
3887 /* exponent + sign */
3888 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
3889 e
|= SIGND1(temp
) >> 16;
3894 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
3896 CPUState
*env
= opaque
;
3899 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
3901 if (version_id
!= 3)
3903 for(i
= 0; i
< CPU_NB_REGS
; i
++)
3904 qemu_get_betls(f
, &env
->regs
[i
]);
3905 qemu_get_betls(f
, &env
->eip
);
3906 qemu_get_betls(f
, &env
->eflags
);
3907 qemu_get_be32s(f
, &hflags
);
3909 qemu_get_be16s(f
, &fpuc
);
3910 qemu_get_be16s(f
, &fpus
);
3911 qemu_get_be16s(f
, &fptag
);
3912 qemu_get_be16s(f
, &fpregs_format
);
3914 /* NOTE: we cannot always restore the FPU state if the image come
3915 from a host with a different 'USE_X86LDOUBLE' define. We guess
3916 if we are in an MMX state to restore correctly in that case. */
3917 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
3918 for(i
= 0; i
< 8; i
++) {
3922 switch(fpregs_format
) {
3924 mant
= qemu_get_be64(f
);
3925 exp
= qemu_get_be16(f
);
3926 #ifdef USE_X86LDOUBLE
3927 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
3929 /* difficult case */
3931 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
3933 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
3937 mant
= qemu_get_be64(f
);
3938 #ifdef USE_X86LDOUBLE
3940 union x86_longdouble
*p
;
3941 /* difficult case */
3942 p
= (void *)&env
->fpregs
[i
];
3947 fp64_to_fp80(p
, mant
);
3951 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
3960 /* XXX: restore FPU round state */
3961 env
->fpstt
= (fpus
>> 11) & 7;
3962 env
->fpus
= fpus
& ~0x3800;
3964 for(i
= 0; i
< 8; i
++) {
3965 env
->fptags
[i
] = (fptag
>> i
) & 1;
3968 for(i
= 0; i
< 6; i
++)
3969 cpu_get_seg(f
, &env
->segs
[i
]);
3970 cpu_get_seg(f
, &env
->ldt
);
3971 cpu_get_seg(f
, &env
->tr
);
3972 cpu_get_seg(f
, &env
->gdt
);
3973 cpu_get_seg(f
, &env
->idt
);
3975 qemu_get_be32s(f
, &env
->sysenter_cs
);
3976 qemu_get_be32s(f
, &env
->sysenter_esp
);
3977 qemu_get_be32s(f
, &env
->sysenter_eip
);
3979 qemu_get_betls(f
, &env
->cr
[0]);
3980 qemu_get_betls(f
, &env
->cr
[2]);
3981 qemu_get_betls(f
, &env
->cr
[3]);
3982 qemu_get_betls(f
, &env
->cr
[4]);
3984 for(i
= 0; i
< 8; i
++)
3985 qemu_get_betls(f
, &env
->dr
[i
]);
3988 qemu_get_be32s(f
, &env
->a20_mask
);
3990 qemu_get_be32s(f
, &env
->mxcsr
);
3991 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
3992 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
3993 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
3996 #ifdef TARGET_X86_64
3997 qemu_get_be64s(f
, &env
->efer
);
3998 qemu_get_be64s(f
, &env
->star
);
3999 qemu_get_be64s(f
, &env
->lstar
);
4000 qemu_get_be64s(f
, &env
->cstar
);
4001 qemu_get_be64s(f
, &env
->fmask
);
4002 qemu_get_be64s(f
, &env
->kernelgsbase
);
4005 /* XXX: compute hflags from scratch, except for CPL and IIF */
4006 env
->hflags
= hflags
;
4011 #elif defined(TARGET_PPC)
4012 void cpu_save(QEMUFile
*f
, void *opaque
)
4016 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4021 #elif defined(TARGET_MIPS)
4022 void cpu_save(QEMUFile
*f
, void *opaque
)
4026 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4031 #elif defined(TARGET_SPARC)
4032 void cpu_save(QEMUFile
*f
, void *opaque
)
4034 CPUState
*env
= opaque
;
4038 for(i
= 0; i
< 8; i
++)
4039 qemu_put_betls(f
, &env
->gregs
[i
]);
4040 for(i
= 0; i
< NWINDOWS
* 16; i
++)
4041 qemu_put_betls(f
, &env
->regbase
[i
]);
4044 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
4050 qemu_put_betl(f
, u
.i
);
4053 qemu_put_betls(f
, &env
->pc
);
4054 qemu_put_betls(f
, &env
->npc
);
4055 qemu_put_betls(f
, &env
->y
);
4057 qemu_put_be32(f
, tmp
);
4058 qemu_put_betls(f
, &env
->fsr
);
4059 qemu_put_betls(f
, &env
->tbr
);
4060 #ifndef TARGET_SPARC64
4061 qemu_put_be32s(f
, &env
->wim
);
4063 for(i
= 0; i
< 16; i
++)
4064 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
4068 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4070 CPUState
*env
= opaque
;
4074 for(i
= 0; i
< 8; i
++)
4075 qemu_get_betls(f
, &env
->gregs
[i
]);
4076 for(i
= 0; i
< NWINDOWS
* 16; i
++)
4077 qemu_get_betls(f
, &env
->regbase
[i
]);
4080 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
4085 u
.i
= qemu_get_betl(f
);
4089 qemu_get_betls(f
, &env
->pc
);
4090 qemu_get_betls(f
, &env
->npc
);
4091 qemu_get_betls(f
, &env
->y
);
4092 tmp
= qemu_get_be32(f
);
4093 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
4094 correctly updated */
4096 qemu_get_betls(f
, &env
->fsr
);
4097 qemu_get_betls(f
, &env
->tbr
);
4098 #ifndef TARGET_SPARC64
4099 qemu_get_be32s(f
, &env
->wim
);
4101 for(i
= 0; i
< 16; i
++)
4102 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
4108 #elif defined(TARGET_ARM)
4110 /* ??? Need to implement these. */
4111 void cpu_save(QEMUFile
*f
, void *opaque
)
4115 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4122 #warning No CPU save/restore functions
4126 /***********************************************************/
4127 /* ram save/restore */
4129 /* we just avoid storing empty pages */
4130 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
4135 for(i
= 1; i
< len
; i
++) {
4139 qemu_put_byte(f
, 1);
4140 qemu_put_byte(f
, v
);
4143 qemu_put_byte(f
, 0);
4144 qemu_put_buffer(f
, buf
, len
);
4147 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
4151 v
= qemu_get_byte(f
);
4154 if (qemu_get_buffer(f
, buf
, len
) != len
)
4158 v
= qemu_get_byte(f
);
4159 memset(buf
, v
, len
);
4167 static void ram_save(QEMUFile
*f
, void *opaque
)
4170 qemu_put_be32(f
, phys_ram_size
);
4171 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
4172 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
4176 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
4180 if (version_id
!= 1)
4182 if (qemu_get_be32(f
) != phys_ram_size
)
4184 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
4185 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
4192 /***********************************************************/
4193 /* machine registration */
4195 QEMUMachine
*first_machine
= NULL
;
4197 int qemu_register_machine(QEMUMachine
*m
)
4200 pm
= &first_machine
;
4208 QEMUMachine
*find_machine(const char *name
)
4212 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4213 if (!strcmp(m
->name
, name
))
4219 /***********************************************************/
4220 /* main execution loop */
4222 void gui_update(void *opaque
)
4224 display_state
.dpy_refresh(&display_state
);
4225 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
4228 struct vm_change_state_entry
{
4229 VMChangeStateHandler
*cb
;
4231 LIST_ENTRY (vm_change_state_entry
) entries
;
4234 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
4236 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
4239 VMChangeStateEntry
*e
;
4241 e
= qemu_mallocz(sizeof (*e
));
4247 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
4251 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
4253 LIST_REMOVE (e
, entries
);
4257 static void vm_state_notify(int running
)
4259 VMChangeStateEntry
*e
;
4261 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
4262 e
->cb(e
->opaque
, running
);
4266 /* XXX: support several handlers */
4267 static VMStopHandler
*vm_stop_cb
;
4268 static void *vm_stop_opaque
;
4270 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4273 vm_stop_opaque
= opaque
;
4277 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4291 void vm_stop(int reason
)
4294 cpu_disable_ticks();
4298 vm_stop_cb(vm_stop_opaque
, reason
);
4305 /* reset/shutdown handler */
4307 typedef struct QEMUResetEntry
{
4308 QEMUResetHandler
*func
;
4310 struct QEMUResetEntry
*next
;
4313 static QEMUResetEntry
*first_reset_entry
;
4314 static int reset_requested
;
4315 static int shutdown_requested
;
4316 static int powerdown_requested
;
4318 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
4320 QEMUResetEntry
**pre
, *re
;
4322 pre
= &first_reset_entry
;
4323 while (*pre
!= NULL
)
4324 pre
= &(*pre
)->next
;
4325 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
4327 re
->opaque
= opaque
;
4332 void qemu_system_reset(void)
4336 /* reset all devices */
4337 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
4338 re
->func(re
->opaque
);
4342 void qemu_system_reset_request(void)
4344 reset_requested
= 1;
4346 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4349 void qemu_system_shutdown_request(void)
4351 shutdown_requested
= 1;
4353 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4356 void qemu_system_powerdown_request(void)
4358 powerdown_requested
= 1;
4360 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4363 void main_loop_wait(int timeout
)
4365 IOHandlerRecord
*ioh
, *ioh_next
;
4372 /* XXX: need to suppress polling by better using win32 events */
4374 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4375 ret
|= pe
->func(pe
->opaque
);
4378 if (ret
== 0 && timeout
> 0) {
4382 /* poll any events */
4383 /* XXX: separate device handlers from system ones */
4387 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4389 (!ioh
->fd_read_poll
||
4390 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4391 FD_SET(ioh
->fd
, &rfds
);
4395 if (ioh
->fd_write
) {
4396 FD_SET(ioh
->fd
, &wfds
);
4406 tv
.tv_usec
= timeout
* 1000;
4408 ret
= select(nfds
+ 1, &rfds
, &wfds
, NULL
, &tv
);
4410 /* XXX: better handling of removal */
4411 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
4412 ioh_next
= ioh
->next
;
4413 if (FD_ISSET(ioh
->fd
, &rfds
)) {
4414 ioh
->fd_read(ioh
->opaque
);
4416 if (FD_ISSET(ioh
->fd
, &wfds
)) {
4417 ioh
->fd_write(ioh
->opaque
);
4425 #if defined(CONFIG_SLIRP)
4426 /* XXX: merge with the previous select() */
4428 fd_set rfds
, wfds
, xfds
;
4436 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4439 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4441 slirp_select_poll(&rfds
, &wfds
, &xfds
);
4447 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4448 qemu_get_clock(vm_clock
));
4449 /* run dma transfers, if any */
4453 /* real time timers */
4454 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4455 qemu_get_clock(rt_clock
));
4458 static CPUState
*cur_cpu
;
4463 #ifdef CONFIG_PROFILER
4468 cur_cpu
= first_cpu
;
4475 env
= env
->next_cpu
;
4478 #ifdef CONFIG_PROFILER
4479 ti
= profile_getclock();
4481 ret
= cpu_exec(env
);
4482 #ifdef CONFIG_PROFILER
4483 qemu_time
+= profile_getclock() - ti
;
4485 if (ret
!= EXCP_HALTED
)
4487 /* all CPUs are halted ? */
4488 if (env
== cur_cpu
) {
4495 if (shutdown_requested
) {
4496 ret
= EXCP_INTERRUPT
;
4499 if (reset_requested
) {
4500 reset_requested
= 0;
4501 qemu_system_reset();
4502 ret
= EXCP_INTERRUPT
;
4504 if (powerdown_requested
) {
4505 powerdown_requested
= 0;
4506 qemu_system_powerdown();
4507 ret
= EXCP_INTERRUPT
;
4509 if (ret
== EXCP_DEBUG
) {
4510 vm_stop(EXCP_DEBUG
);
4512 /* if hlt instruction, we wait until the next IRQ */
4513 /* XXX: use timeout computed from timers */
4514 if (ret
== EXCP_HLT
)
4521 #ifdef CONFIG_PROFILER
4522 ti
= profile_getclock();
4524 main_loop_wait(timeout
);
4525 #ifdef CONFIG_PROFILER
4526 dev_time
+= profile_getclock() - ti
;
4529 cpu_disable_ticks();
4535 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2005 Fabrice Bellard\n"
4536 "usage: %s [options] [disk_image]\n"
4538 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4540 "Standard options:\n"
4541 "-M machine select emulated machine (-M ? for list)\n"
4542 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
4543 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
4544 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
4545 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
4546 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
4547 "-snapshot write to temporary files instead of disk image files\n"
4548 "-m megs set virtual RAM size to megs MB [default=%d]\n"
4549 "-smp n set the number of CPUs to 'n' [default=1]\n"
4550 "-nographic disable graphical output and redirect serial I/Os to console\n"
4552 "-k language use keyboard layout (for example \"fr\" for French)\n"
4555 "-audio-help print list of audio drivers and their options\n"
4556 "-soundhw c1,... enable audio support\n"
4557 " and only specified sound cards (comma separated list)\n"
4558 " use -soundhw ? to get the list of supported cards\n"
4559 " use -soundhw all to enable all of them\n"
4561 "-localtime set the real time clock to local time [default=utc]\n"
4562 "-full-screen start in full screen\n"
4564 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4566 "-usb enable the USB driver (will be the default soon)\n"
4567 "-usbdevice name add the host or guest USB device 'name'\n"
4568 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4569 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4572 "Network options:\n"
4573 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
4574 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4576 "-net user[,vlan=n][,hostname=host]\n"
4577 " connect the user mode network stack to VLAN 'n' and send\n"
4578 " hostname 'host' to DHCP clients\n"
4581 "-net tap[,vlan=n],ifname=name\n"
4582 " connect the host TAP network interface to VLAN 'n'\n"
4584 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
4585 " connect the host TAP network interface to VLAN 'n' and use\n"
4586 " the network script 'file' (default=%s);\n"
4587 " use 'fd=h' to connect to an already opened TAP interface\n"
4589 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4590 " connect the vlan 'n' to another VLAN using a socket connection\n"
4591 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
4592 " connect the vlan 'n' to multicast maddr and port\n"
4593 "-net none use it alone to have zero network devices; if no -net option\n"
4594 " is provided, the default is '-net nic -net user'\n"
4597 "-tftp prefix allow tftp access to files starting with prefix [-net user]\n"
4599 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4601 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4602 " redirect TCP or UDP connections from host to guest [-net user]\n"
4605 "Linux boot specific:\n"
4606 "-kernel bzImage use 'bzImage' as kernel image\n"
4607 "-append cmdline use 'cmdline' as kernel command line\n"
4608 "-initrd file use 'file' as initial ram disk\n"
4610 "Debug/Expert options:\n"
4611 "-monitor dev redirect the monitor to char device 'dev'\n"
4612 "-serial dev redirect the serial port to char device 'dev'\n"
4613 "-parallel dev redirect the parallel port to char device 'dev'\n"
4614 "-pidfile file Write PID to 'file'\n"
4615 "-S freeze CPU at startup (use 'c' to start execution)\n"
4616 "-s wait gdb connection to port %d\n"
4617 "-p port change gdb connection port\n"
4618 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4619 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
4620 " translation (t=none or lba) (usually qemu can guess them)\n"
4621 "-L path set the directory for the BIOS and VGA BIOS\n"
4623 "-no-kqemu disable KQEMU kernel module usage\n"
4625 #ifdef USE_CODE_COPY
4626 "-no-code-copy disable code copy acceleration\n"
4629 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
4630 " (default is CL-GD5446 PCI VGA)\n"
4632 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
4634 "During emulation, the following keys are useful:\n"
4635 "ctrl-alt-f toggle full screen\n"
4636 "ctrl-alt-n switch to virtual console 'n'\n"
4637 "ctrl-alt toggle mouse and keyboard grab\n"
4639 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4641 #ifdef CONFIG_SOFTMMU
4648 DEFAULT_NETWORK_SCRIPT
,
4650 DEFAULT_GDBSTUB_PORT
,
4652 #ifndef CONFIG_SOFTMMU
4654 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
4655 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
4661 #define HAS_ARG 0x0001
4675 QEMU_OPTION_snapshot
,
4677 QEMU_OPTION_nographic
,
4679 QEMU_OPTION_audio_help
,
4680 QEMU_OPTION_soundhw
,
4698 QEMU_OPTION_no_code_copy
,
4700 QEMU_OPTION_localtime
,
4701 QEMU_OPTION_cirrusvga
,
4703 QEMU_OPTION_std_vga
,
4704 QEMU_OPTION_monitor
,
4706 QEMU_OPTION_parallel
,
4708 QEMU_OPTION_full_screen
,
4709 QEMU_OPTION_pidfile
,
4710 QEMU_OPTION_no_kqemu
,
4711 QEMU_OPTION_kernel_kqemu
,
4712 QEMU_OPTION_win2k_hack
,
4714 QEMU_OPTION_usbdevice
,
4718 typedef struct QEMUOption
{
4724 const QEMUOption qemu_options
[] = {
4725 { "h", 0, QEMU_OPTION_h
},
4727 { "M", HAS_ARG
, QEMU_OPTION_M
},
4728 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4729 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4730 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4731 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4732 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4733 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4734 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4735 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4736 { "snapshot", 0, QEMU_OPTION_snapshot
},
4737 { "m", HAS_ARG
, QEMU_OPTION_m
},
4738 { "nographic", 0, QEMU_OPTION_nographic
},
4739 { "k", HAS_ARG
, QEMU_OPTION_k
},
4741 { "audio-help", 0, QEMU_OPTION_audio_help
},
4742 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4745 { "net", HAS_ARG
, QEMU_OPTION_net
},
4747 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4749 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4751 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4754 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4755 { "append", HAS_ARG
, QEMU_OPTION_append
},
4756 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4758 { "S", 0, QEMU_OPTION_S
},
4759 { "s", 0, QEMU_OPTION_s
},
4760 { "p", HAS_ARG
, QEMU_OPTION_p
},
4761 { "d", HAS_ARG
, QEMU_OPTION_d
},
4762 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4763 { "L", HAS_ARG
, QEMU_OPTION_L
},
4764 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
4766 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4767 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4769 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4770 { "g", 1, QEMU_OPTION_g
},
4772 { "localtime", 0, QEMU_OPTION_localtime
},
4773 { "std-vga", 0, QEMU_OPTION_std_vga
},
4774 { "monitor", 1, QEMU_OPTION_monitor
},
4775 { "serial", 1, QEMU_OPTION_serial
},
4776 { "parallel", 1, QEMU_OPTION_parallel
},
4777 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4778 { "full-screen", 0, QEMU_OPTION_full_screen
},
4779 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4780 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4781 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4782 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4784 /* temporary options */
4785 { "usb", 0, QEMU_OPTION_usb
},
4786 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
4790 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
4792 /* this stack is only used during signal handling */
4793 #define SIGNAL_STACK_SIZE 32768
4795 static uint8_t *signal_stack
;
4799 /* password input */
4801 static BlockDriverState
*get_bdrv(int index
)
4803 BlockDriverState
*bs
;
4806 bs
= bs_table
[index
];
4807 } else if (index
< 6) {
4808 bs
= fd_table
[index
- 4];
4815 static void read_passwords(void)
4817 BlockDriverState
*bs
;
4821 for(i
= 0; i
< 6; i
++) {
4823 if (bs
&& bdrv_is_encrypted(bs
)) {
4824 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
4825 for(j
= 0; j
< 3; j
++) {
4826 monitor_readline("Password: ",
4827 1, password
, sizeof(password
));
4828 if (bdrv_set_key(bs
, password
) == 0)
4830 term_printf("invalid password\n");
4836 /* XXX: currently we cannot use simultaneously different CPUs */
4837 void register_machines(void)
4839 #if defined(TARGET_I386)
4840 qemu_register_machine(&pc_machine
);
4841 qemu_register_machine(&isapc_machine
);
4842 #elif defined(TARGET_PPC)
4843 qemu_register_machine(&heathrow_machine
);
4844 qemu_register_machine(&core99_machine
);
4845 qemu_register_machine(&prep_machine
);
4846 #elif defined(TARGET_MIPS)
4847 qemu_register_machine(&mips_machine
);
4848 #elif defined(TARGET_SPARC)
4849 #ifdef TARGET_SPARC64
4850 qemu_register_machine(&sun4u_machine
);
4852 qemu_register_machine(&sun4m_machine
);
4854 #elif defined(TARGET_ARM)
4855 qemu_register_machine(&integratorcp926_machine
);
4856 qemu_register_machine(&integratorcp1026_machine
);
4857 qemu_register_machine(&versatilepb_machine
);
4859 #error unsupported CPU
4864 struct soundhw soundhw
[] = {
4867 "Creative Sound Blaster 16",
4870 { .init_isa
= SB16_init
}
4877 "Yamaha YMF262 (OPL3)",
4879 "Yamaha YM3812 (OPL2)",
4883 { .init_isa
= Adlib_init
}
4890 "Gravis Ultrasound GF1",
4893 { .init_isa
= GUS_init
}
4899 "ENSONIQ AudioPCI ES1370",
4902 { .init_pci
= es1370_init
}
4905 { NULL
, NULL
, 0, 0, { NULL
} }
4908 static void select_soundhw (const char *optarg
)
4912 if (*optarg
== '?') {
4915 printf ("Valid sound card names (comma separated):\n");
4916 for (c
= soundhw
; c
->name
; ++c
) {
4917 printf ("%-11s %s\n", c
->name
, c
->descr
);
4919 printf ("\n-soundhw all will enable all of the above\n");
4920 exit (*optarg
!= '?');
4928 if (!strcmp (optarg
, "all")) {
4929 for (c
= soundhw
; c
->name
; ++c
) {
4937 e
= strchr (p
, ',');
4938 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4940 for (c
= soundhw
; c
->name
; ++c
) {
4941 if (!strncmp (c
->name
, p
, l
)) {
4950 "Unknown sound card name (too big to show)\n");
4953 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4958 p
+= l
+ (e
!= NULL
);
4962 goto show_valid_cards
;
4967 #define MAX_NET_CLIENTS 32
4969 int main(int argc
, char **argv
)
4971 #ifdef CONFIG_GDBSTUB
4972 int use_gdbstub
, gdbstub_port
;
4975 int snapshot
, linux_boot
;
4976 const char *initrd_filename
;
4977 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
4978 const char *kernel_filename
, *kernel_cmdline
;
4979 DisplayState
*ds
= &display_state
;
4980 int cyls
, heads
, secs
, translation
;
4981 int start_emulation
= 1;
4982 char net_clients
[MAX_NET_CLIENTS
][256];
4985 const char *r
, *optarg
;
4986 CharDriverState
*monitor_hd
;
4987 char monitor_device
[128];
4988 char serial_devices
[MAX_SERIAL_PORTS
][128];
4989 int serial_device_index
;
4990 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
4991 int parallel_device_index
;
4992 const char *loadvm
= NULL
;
4993 QEMUMachine
*machine
;
4994 char usb_devices
[MAX_VM_USB_PORTS
][128];
4995 int usb_devices_index
;
4997 LIST_INIT (&vm_change_state_head
);
4998 #if !defined(CONFIG_SOFTMMU)
4999 /* we never want that malloc() uses mmap() */
5000 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
5002 register_machines();
5003 machine
= first_machine
;
5004 initrd_filename
= NULL
;
5005 for(i
= 0; i
< MAX_FD
; i
++)
5006 fd_filename
[i
] = NULL
;
5007 for(i
= 0; i
< MAX_DISKS
; i
++)
5008 hd_filename
[i
] = NULL
;
5009 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5010 vga_ram_size
= VGA_RAM_SIZE
;
5011 bios_size
= BIOS_SIZE
;
5012 #ifdef CONFIG_GDBSTUB
5014 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
5018 kernel_filename
= NULL
;
5019 kernel_cmdline
= "";
5025 cyls
= heads
= secs
= 0;
5026 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5027 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
5029 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
5030 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
5031 serial_devices
[i
][0] = '\0';
5032 serial_device_index
= 0;
5034 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
5035 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
5036 parallel_devices
[i
][0] = '\0';
5037 parallel_device_index
= 0;
5039 usb_devices_index
= 0;
5044 /* default mac address of the first network interface */
5052 hd_filename
[0] = argv
[optind
++];
5054 const QEMUOption
*popt
;
5057 popt
= qemu_options
;
5060 fprintf(stderr
, "%s: invalid option -- '%s'\n",
5064 if (!strcmp(popt
->name
, r
+ 1))
5068 if (popt
->flags
& HAS_ARG
) {
5069 if (optind
>= argc
) {
5070 fprintf(stderr
, "%s: option '%s' requires an argument\n",
5074 optarg
= argv
[optind
++];
5079 switch(popt
->index
) {
5081 machine
= find_machine(optarg
);
5084 printf("Supported machines are:\n");
5085 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5086 printf("%-10s %s%s\n",
5088 m
== first_machine
? " (default)" : "");
5093 case QEMU_OPTION_initrd
:
5094 initrd_filename
= optarg
;
5096 case QEMU_OPTION_hda
:
5097 case QEMU_OPTION_hdb
:
5098 case QEMU_OPTION_hdc
:
5099 case QEMU_OPTION_hdd
:
5102 hd_index
= popt
->index
- QEMU_OPTION_hda
;
5103 hd_filename
[hd_index
] = optarg
;
5104 if (hd_index
== cdrom_index
)
5108 case QEMU_OPTION_snapshot
:
5111 case QEMU_OPTION_hdachs
:
5115 cyls
= strtol(p
, (char **)&p
, 0);
5116 if (cyls
< 1 || cyls
> 16383)
5121 heads
= strtol(p
, (char **)&p
, 0);
5122 if (heads
< 1 || heads
> 16)
5127 secs
= strtol(p
, (char **)&p
, 0);
5128 if (secs
< 1 || secs
> 63)
5132 if (!strcmp(p
, "none"))
5133 translation
= BIOS_ATA_TRANSLATION_NONE
;
5134 else if (!strcmp(p
, "lba"))
5135 translation
= BIOS_ATA_TRANSLATION_LBA
;
5136 else if (!strcmp(p
, "auto"))
5137 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5140 } else if (*p
!= '\0') {
5142 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5147 case QEMU_OPTION_nographic
:
5148 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
5149 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
5152 case QEMU_OPTION_kernel
:
5153 kernel_filename
= optarg
;
5155 case QEMU_OPTION_append
:
5156 kernel_cmdline
= optarg
;
5158 case QEMU_OPTION_cdrom
:
5159 if (cdrom_index
>= 0) {
5160 hd_filename
[cdrom_index
] = optarg
;
5163 case QEMU_OPTION_boot
:
5164 boot_device
= optarg
[0];
5165 if (boot_device
!= 'a' &&
5168 boot_device
!= 'n' &&
5170 boot_device
!= 'c' && boot_device
!= 'd') {
5171 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
5175 case QEMU_OPTION_fda
:
5176 fd_filename
[0] = optarg
;
5178 case QEMU_OPTION_fdb
:
5179 fd_filename
[1] = optarg
;
5181 case QEMU_OPTION_no_code_copy
:
5182 code_copy_enabled
= 0;
5184 case QEMU_OPTION_net
:
5185 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5186 fprintf(stderr
, "qemu: too many network clients\n");
5189 pstrcpy(net_clients
[nb_net_clients
],
5190 sizeof(net_clients
[0]),
5195 case QEMU_OPTION_tftp
:
5196 tftp_prefix
= optarg
;
5199 case QEMU_OPTION_smb
:
5200 net_slirp_smb(optarg
);
5203 case QEMU_OPTION_redir
:
5204 net_slirp_redir(optarg
);
5208 case QEMU_OPTION_audio_help
:
5212 case QEMU_OPTION_soundhw
:
5213 select_soundhw (optarg
);
5220 ram_size
= atoi(optarg
) * 1024 * 1024;
5223 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
5224 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
5225 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
5234 mask
= cpu_str_to_log_mask(optarg
);
5236 printf("Log items (comma separated):\n");
5237 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5238 printf("%-10s %s\n", item
->name
, item
->help
);
5245 #ifdef CONFIG_GDBSTUB
5250 gdbstub_port
= atoi(optarg
);
5257 start_emulation
= 0;
5260 keyboard_layout
= optarg
;
5262 case QEMU_OPTION_localtime
:
5265 case QEMU_OPTION_cirrusvga
:
5266 cirrus_vga_enabled
= 1;
5268 case QEMU_OPTION_std_vga
:
5269 cirrus_vga_enabled
= 0;
5276 w
= strtol(p
, (char **)&p
, 10);
5279 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5285 h
= strtol(p
, (char **)&p
, 10);
5290 depth
= strtol(p
, (char **)&p
, 10);
5291 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5292 depth
!= 24 && depth
!= 32)
5294 } else if (*p
== '\0') {
5295 depth
= graphic_depth
;
5302 graphic_depth
= depth
;
5305 case QEMU_OPTION_monitor
:
5306 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
5308 case QEMU_OPTION_serial
:
5309 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5310 fprintf(stderr
, "qemu: too many serial ports\n");
5313 pstrcpy(serial_devices
[serial_device_index
],
5314 sizeof(serial_devices
[0]), optarg
);
5315 serial_device_index
++;
5317 case QEMU_OPTION_parallel
:
5318 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5319 fprintf(stderr
, "qemu: too many parallel ports\n");
5322 pstrcpy(parallel_devices
[parallel_device_index
],
5323 sizeof(parallel_devices
[0]), optarg
);
5324 parallel_device_index
++;
5326 case QEMU_OPTION_loadvm
:
5329 case QEMU_OPTION_full_screen
:
5332 case QEMU_OPTION_pidfile
:
5333 create_pidfile(optarg
);
5336 case QEMU_OPTION_win2k_hack
:
5337 win2k_install_hack
= 1;
5341 case QEMU_OPTION_no_kqemu
:
5344 case QEMU_OPTION_kernel_kqemu
:
5348 case QEMU_OPTION_usb
:
5351 case QEMU_OPTION_usbdevice
:
5353 if (usb_devices_index
>= MAX_VM_USB_PORTS
) {
5354 fprintf(stderr
, "Too many USB devices\n");
5357 pstrcpy(usb_devices
[usb_devices_index
],
5358 sizeof(usb_devices
[usb_devices_index
]),
5360 usb_devices_index
++;
5362 case QEMU_OPTION_smp
:
5363 smp_cpus
= atoi(optarg
);
5364 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
5365 fprintf(stderr
, "Invalid number of CPUs\n");
5377 linux_boot
= (kernel_filename
!= NULL
);
5380 hd_filename
[0] == '\0' &&
5381 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
5382 fd_filename
[0] == '\0')
5385 /* boot to cd by default if no hard disk */
5386 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
5387 if (fd_filename
[0] != '\0')
5393 #if !defined(CONFIG_SOFTMMU)
5394 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
5396 static uint8_t stdout_buf
[4096];
5397 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
5400 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5407 /* init network clients */
5408 if (nb_net_clients
== 0) {
5409 /* if no clients, we use a default config */
5410 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
5412 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
5417 for(i
= 0;i
< nb_net_clients
; i
++) {
5418 if (net_client_init(net_clients
[i
]) < 0)
5422 /* init the memory */
5423 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
5425 #ifdef CONFIG_SOFTMMU
5426 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5427 if (!phys_ram_base
) {
5428 fprintf(stderr
, "Could not allocate physical memory\n");
5432 /* as we must map the same page at several addresses, we must use
5437 tmpdir
= getenv("QEMU_TMPDIR");
5440 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
5441 if (mkstemp(phys_ram_file
) < 0) {
5442 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
5446 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
5447 if (phys_ram_fd
< 0) {
5448 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
5452 ftruncate(phys_ram_fd
, phys_ram_size
);
5453 unlink(phys_ram_file
);
5454 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
5456 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
5458 if (phys_ram_base
== MAP_FAILED
) {
5459 fprintf(stderr
, "Could not map physical memory\n");
5465 /* we always create the cdrom drive, even if no disk is there */
5467 if (cdrom_index
>= 0) {
5468 bs_table
[cdrom_index
] = bdrv_new("cdrom");
5469 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
5472 /* open the virtual block devices */
5473 for(i
= 0; i
< MAX_DISKS
; i
++) {
5474 if (hd_filename
[i
]) {
5477 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
5478 bs_table
[i
] = bdrv_new(buf
);
5480 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
5481 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
5485 if (i
== 0 && cyls
!= 0) {
5486 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
5487 bdrv_set_translation_hint(bs_table
[i
], translation
);
5492 /* we always create at least one floppy disk */
5493 fd_table
[0] = bdrv_new("fda");
5494 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
5496 for(i
= 0; i
< MAX_FD
; i
++) {
5497 if (fd_filename
[i
]) {
5500 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
5501 fd_table
[i
] = bdrv_new(buf
);
5502 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
5504 if (fd_filename
[i
] != '\0') {
5505 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
5506 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
5514 /* init USB devices */
5516 vm_usb_hub
= usb_hub_init(vm_usb_ports
, MAX_VM_USB_PORTS
);
5517 for(i
= 0; i
< usb_devices_index
; i
++) {
5518 if (usb_device_add(usb_devices
[i
]) < 0) {
5519 fprintf(stderr
, "Warning: could not add USB device %s\n",
5525 register_savevm("timer", 0, 1, timer_save
, timer_load
, NULL
);
5526 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
5529 cpu_calibrate_ticks();
5533 dumb_display_init(ds
);
5535 #if defined(CONFIG_SDL)
5536 sdl_display_init(ds
, full_screen
);
5537 #elif defined(CONFIG_COCOA)
5538 cocoa_display_init(ds
, full_screen
);
5540 dumb_display_init(ds
);
5544 monitor_hd
= qemu_chr_open(monitor_device
);
5546 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5549 monitor_init(monitor_hd
, !nographic
);
5551 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5552 if (serial_devices
[i
][0] != '\0') {
5553 serial_hds
[i
] = qemu_chr_open(serial_devices
[i
]);
5554 if (!serial_hds
[i
]) {
5555 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5559 if (!strcmp(serial_devices
[i
], "vc"))
5560 qemu_chr_printf(serial_hds
[i
], "serial%d console\n", i
);
5564 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5565 if (parallel_devices
[i
][0] != '\0') {
5566 parallel_hds
[i
] = qemu_chr_open(parallel_devices
[i
]);
5567 if (!parallel_hds
[i
]) {
5568 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5569 parallel_devices
[i
]);
5572 if (!strcmp(parallel_devices
[i
], "vc"))
5573 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\n", i
);
5577 /* setup cpu signal handlers for MMU / self modifying code handling */
5578 #if !defined(CONFIG_SOFTMMU)
5580 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5583 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
5584 stk
.ss_sp
= signal_stack
;
5585 stk
.ss_size
= SIGNAL_STACK_SIZE
;
5588 if (sigaltstack(&stk
, NULL
) < 0) {
5589 perror("sigaltstack");
5595 struct sigaction act
;
5597 sigfillset(&act
.sa_mask
);
5598 act
.sa_flags
= SA_SIGINFO
;
5599 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5600 act
.sa_flags
|= SA_ONSTACK
;
5602 act
.sa_sigaction
= host_segv_handler
;
5603 sigaction(SIGSEGV
, &act
, NULL
);
5604 sigaction(SIGBUS
, &act
, NULL
);
5605 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5606 sigaction(SIGFPE
, &act
, NULL
);
5613 struct sigaction act
;
5614 sigfillset(&act
.sa_mask
);
5616 act
.sa_handler
= SIG_IGN
;
5617 sigaction(SIGPIPE
, &act
, NULL
);
5622 machine
->init(ram_size
, vga_ram_size
, boot_device
,
5623 ds
, fd_filename
, snapshot
,
5624 kernel_filename
, kernel_cmdline
, initrd_filename
);
5626 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
5627 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
5629 #ifdef CONFIG_GDBSTUB
5631 if (gdbserver_start(gdbstub_port
) < 0) {
5632 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
5636 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
5641 qemu_loadvm(loadvm
);
5644 /* XXX: simplify init */
5646 if (start_emulation
) {