4 * Copyright (c) 2003-2006 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
35 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
53 #include <linux/if_tun.h>
56 #include <linux/rtc.h>
57 #include <linux/ppdev.h>
62 #if defined(CONFIG_SLIRP)
68 #include <sys/timeb.h>
70 #define getopt_long_only getopt_long
71 #define memalign(align, size) malloc(size)
74 #include "qemu_socket.h"
80 #endif /* CONFIG_SDL */
84 #define main qemu_main
85 #endif /* CONFIG_COCOA */
91 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
93 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
95 #define SMBD_COMMAND "/usr/sbin/smbd"
98 //#define DEBUG_UNUSED_IOPORT
99 //#define DEBUG_IOPORT
101 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
104 #define DEFAULT_RAM_SIZE 144
106 #define DEFAULT_RAM_SIZE 128
109 #define GUI_REFRESH_INTERVAL 30
111 /* Max number of USB devices that can be specified on the commandline. */
112 #define MAX_USB_CMDLINE 8
114 /* XXX: use a two level table to limit memory usage */
115 #define MAX_IOPORTS 65536
117 #define DISK_OPTIONS_SIZE 256
119 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
120 char phys_ram_file
[1024];
121 void *ioport_opaque
[MAX_IOPORTS
];
122 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
123 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
124 /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available
125 to store the VM snapshots */
126 BlockDriverState
*bs_table
[MAX_DISKS
+ 1], *fd_table
[MAX_FD
];
127 /* point to the block driver where the snapshots are managed */
128 BlockDriverState
*bs_snapshots
;
129 BlockDriverState
*bs_scsi_table
[MAX_SCSI_DISKS
];
130 SCSIDiskInfo scsi_disks_info
[MAX_SCSI_DISKS
];
131 int scsi_hba_lsi
; /* Count of scsi disks/cdrom using this lsi adapter */
134 static DisplayState display_state
;
136 const char* keyboard_layout
= NULL
;
137 int64_t ticks_per_sec
;
138 int boot_device
= 'c';
140 int pit_min_timer_count
= 0;
142 NICInfo nd_table
[MAX_NICS
];
143 QEMUTimer
*gui_timer
;
146 int cirrus_vga_enabled
= 1;
148 int graphic_width
= 1024;
149 int graphic_height
= 768;
151 int graphic_width
= 800;
152 int graphic_height
= 600;
154 int graphic_depth
= 15;
157 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
158 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
160 int win2k_install_hack
= 0;
163 static VLANState
*first_vlan
;
165 const char *vnc_display
;
166 #if defined(TARGET_SPARC)
168 #elif defined(TARGET_I386)
173 int acpi_enabled
= 1;
178 /***********************************************************/
179 /* x86 ISA bus support */
181 target_phys_addr_t isa_mem_base
= 0;
184 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
186 #ifdef DEBUG_UNUSED_IOPORT
187 fprintf(stderr
, "inb: port=0x%04x\n", address
);
192 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
194 #ifdef DEBUG_UNUSED_IOPORT
195 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
199 /* default is to make two byte accesses */
200 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
203 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
204 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
205 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
209 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
211 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
212 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
213 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
216 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
218 #ifdef DEBUG_UNUSED_IOPORT
219 fprintf(stderr
, "inl: port=0x%04x\n", address
);
224 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
226 #ifdef DEBUG_UNUSED_IOPORT
227 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
231 void init_ioports(void)
235 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
236 ioport_read_table
[0][i
] = default_ioport_readb
;
237 ioport_write_table
[0][i
] = default_ioport_writeb
;
238 ioport_read_table
[1][i
] = default_ioport_readw
;
239 ioport_write_table
[1][i
] = default_ioport_writew
;
240 ioport_read_table
[2][i
] = default_ioport_readl
;
241 ioport_write_table
[2][i
] = default_ioport_writel
;
245 /* size is the word size in byte */
246 int register_ioport_read(int start
, int length
, int size
,
247 IOPortReadFunc
*func
, void *opaque
)
253 } else if (size
== 2) {
255 } else if (size
== 4) {
258 hw_error("register_ioport_read: invalid size");
261 for(i
= start
; i
< start
+ length
; i
+= size
) {
262 ioport_read_table
[bsize
][i
] = func
;
263 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
264 hw_error("register_ioport_read: invalid opaque");
265 ioport_opaque
[i
] = opaque
;
270 /* size is the word size in byte */
271 int register_ioport_write(int start
, int length
, int size
,
272 IOPortWriteFunc
*func
, void *opaque
)
278 } else if (size
== 2) {
280 } else if (size
== 4) {
283 hw_error("register_ioport_write: invalid size");
286 for(i
= start
; i
< start
+ length
; i
+= size
) {
287 ioport_write_table
[bsize
][i
] = func
;
288 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
289 hw_error("register_ioport_write: invalid opaque");
290 ioport_opaque
[i
] = opaque
;
295 void isa_unassign_ioport(int start
, int length
)
299 for(i
= start
; i
< start
+ length
; i
++) {
300 ioport_read_table
[0][i
] = default_ioport_readb
;
301 ioport_read_table
[1][i
] = default_ioport_readw
;
302 ioport_read_table
[2][i
] = default_ioport_readl
;
304 ioport_write_table
[0][i
] = default_ioport_writeb
;
305 ioport_write_table
[1][i
] = default_ioport_writew
;
306 ioport_write_table
[2][i
] = default_ioport_writel
;
310 /***********************************************************/
312 void pstrcpy(char *buf
, int buf_size
, const char *str
)
322 if (c
== 0 || q
>= buf
+ buf_size
- 1)
329 /* strcat and truncate. */
330 char *pstrcat(char *buf
, int buf_size
, const char *s
)
335 pstrcpy(buf
+ len
, buf_size
- len
, s
);
339 int strstart(const char *str
, const char *val
, const char **ptr
)
355 void cpu_outb(CPUState
*env
, int addr
, int val
)
358 if (loglevel
& CPU_LOG_IOPORT
)
359 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
361 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
364 env
->last_io_time
= cpu_get_time_fast();
368 void cpu_outw(CPUState
*env
, int addr
, int val
)
371 if (loglevel
& CPU_LOG_IOPORT
)
372 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
374 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
377 env
->last_io_time
= cpu_get_time_fast();
381 void cpu_outl(CPUState
*env
, int addr
, int val
)
384 if (loglevel
& CPU_LOG_IOPORT
)
385 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
387 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
390 env
->last_io_time
= cpu_get_time_fast();
394 int cpu_inb(CPUState
*env
, int addr
)
397 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
399 if (loglevel
& CPU_LOG_IOPORT
)
400 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
404 env
->last_io_time
= cpu_get_time_fast();
409 int cpu_inw(CPUState
*env
, int addr
)
412 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
414 if (loglevel
& CPU_LOG_IOPORT
)
415 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
419 env
->last_io_time
= cpu_get_time_fast();
424 int cpu_inl(CPUState
*env
, int addr
)
427 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
429 if (loglevel
& CPU_LOG_IOPORT
)
430 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
434 env
->last_io_time
= cpu_get_time_fast();
439 /***********************************************************/
440 void hw_error(const char *fmt
, ...)
446 fprintf(stderr
, "qemu: hardware error: ");
447 vfprintf(stderr
, fmt
, ap
);
448 fprintf(stderr
, "\n");
449 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
450 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
452 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
454 cpu_dump_state(env
, stderr
, fprintf
, 0);
461 /***********************************************************/
464 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
465 static void *qemu_put_kbd_event_opaque
;
466 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
467 static void *qemu_put_mouse_event_opaque
;
468 static int qemu_put_mouse_event_absolute
;
470 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
472 qemu_put_kbd_event_opaque
= opaque
;
473 qemu_put_kbd_event
= func
;
476 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
, int absolute
)
478 qemu_put_mouse_event_opaque
= opaque
;
479 qemu_put_mouse_event
= func
;
480 qemu_put_mouse_event_absolute
= absolute
;
483 void kbd_put_keycode(int keycode
)
485 if (qemu_put_kbd_event
) {
486 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
490 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
492 if (qemu_put_mouse_event
) {
493 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
494 dx
, dy
, dz
, buttons_state
);
498 int kbd_mouse_is_absolute(void)
500 return qemu_put_mouse_event_absolute
;
503 /* compute with 96 bit intermediate result: (a*b)/c */
504 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
509 #ifdef WORDS_BIGENDIAN
519 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
520 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
523 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
527 /***********************************************************/
528 /* real time host monotonic timer */
530 #define QEMU_TIMER_BASE 1000000000LL
534 static int64_t clock_freq
;
536 static void init_get_clock(void)
540 ret
= QueryPerformanceFrequency(&freq
);
542 fprintf(stderr
, "Could not calibrate ticks\n");
545 clock_freq
= freq
.QuadPart
;
548 static int64_t get_clock(void)
551 QueryPerformanceCounter(&ti
);
552 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
557 static int use_rt_clock
;
559 static void init_get_clock(void)
562 #if defined(__linux__)
565 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
572 static int64_t get_clock(void)
574 #if defined(__linux__)
577 clock_gettime(CLOCK_MONOTONIC
, &ts
);
578 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
582 /* XXX: using gettimeofday leads to problems if the date
583 changes, so it should be avoided. */
585 gettimeofday(&tv
, NULL
);
586 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
592 /***********************************************************/
593 /* guest cycle counter */
595 static int64_t cpu_ticks_prev
;
596 static int64_t cpu_ticks_offset
;
597 static int64_t cpu_clock_offset
;
598 static int cpu_ticks_enabled
;
600 /* return the host CPU cycle counter and handle stop/restart */
601 int64_t cpu_get_ticks(void)
603 if (!cpu_ticks_enabled
) {
604 return cpu_ticks_offset
;
607 ticks
= cpu_get_real_ticks();
608 if (cpu_ticks_prev
> ticks
) {
609 /* Note: non increasing ticks may happen if the host uses
611 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
613 cpu_ticks_prev
= ticks
;
614 return ticks
+ cpu_ticks_offset
;
618 /* return the host CPU monotonic timer and handle stop/restart */
619 static int64_t cpu_get_clock(void)
622 if (!cpu_ticks_enabled
) {
623 return cpu_clock_offset
;
626 return ti
+ cpu_clock_offset
;
630 /* enable cpu_get_ticks() */
631 void cpu_enable_ticks(void)
633 if (!cpu_ticks_enabled
) {
634 cpu_ticks_offset
-= cpu_get_real_ticks();
635 cpu_clock_offset
-= get_clock();
636 cpu_ticks_enabled
= 1;
640 /* disable cpu_get_ticks() : the clock is stopped. You must not call
641 cpu_get_ticks() after that. */
642 void cpu_disable_ticks(void)
644 if (cpu_ticks_enabled
) {
645 cpu_ticks_offset
= cpu_get_ticks();
646 cpu_clock_offset
= cpu_get_clock();
647 cpu_ticks_enabled
= 0;
651 /***********************************************************/
654 #define QEMU_TIMER_REALTIME 0
655 #define QEMU_TIMER_VIRTUAL 1
659 /* XXX: add frequency */
667 struct QEMUTimer
*next
;
673 static QEMUTimer
*active_timers
[2];
675 static MMRESULT timerID
;
676 static HANDLE host_alarm
= NULL
;
677 static unsigned int period
= 1;
679 /* frequency of the times() clock tick */
680 static int timer_freq
;
683 QEMUClock
*qemu_new_clock(int type
)
686 clock
= qemu_mallocz(sizeof(QEMUClock
));
693 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
697 ts
= qemu_mallocz(sizeof(QEMUTimer
));
704 void qemu_free_timer(QEMUTimer
*ts
)
709 /* stop a timer, but do not dealloc it */
710 void qemu_del_timer(QEMUTimer
*ts
)
714 /* NOTE: this code must be signal safe because
715 qemu_timer_expired() can be called from a signal. */
716 pt
= &active_timers
[ts
->clock
->type
];
729 /* modify the current timer so that it will be fired when current_time
730 >= expire_time. The corresponding callback will be called. */
731 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
737 /* add the timer in the sorted list */
738 /* NOTE: this code must be signal safe because
739 qemu_timer_expired() can be called from a signal. */
740 pt
= &active_timers
[ts
->clock
->type
];
745 if (t
->expire_time
> expire_time
)
749 ts
->expire_time
= expire_time
;
754 int qemu_timer_pending(QEMUTimer
*ts
)
757 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
764 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
768 return (timer_head
->expire_time
<= current_time
);
771 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
777 if (!ts
|| ts
->expire_time
> current_time
)
779 /* remove timer from the list before calling the callback */
780 *ptimer_head
= ts
->next
;
783 /* run the callback (the timer list can be modified) */
788 int64_t qemu_get_clock(QEMUClock
*clock
)
790 switch(clock
->type
) {
791 case QEMU_TIMER_REALTIME
:
792 return get_clock() / 1000000;
794 case QEMU_TIMER_VIRTUAL
:
795 return cpu_get_clock();
799 static void init_timers(void)
802 ticks_per_sec
= QEMU_TIMER_BASE
;
803 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
804 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
808 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
810 uint64_t expire_time
;
812 if (qemu_timer_pending(ts
)) {
813 expire_time
= ts
->expire_time
;
817 qemu_put_be64(f
, expire_time
);
820 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
822 uint64_t expire_time
;
824 expire_time
= qemu_get_be64(f
);
825 if (expire_time
!= -1) {
826 qemu_mod_timer(ts
, expire_time
);
832 static void timer_save(QEMUFile
*f
, void *opaque
)
834 if (cpu_ticks_enabled
) {
835 hw_error("cannot save state if virtual timers are running");
837 qemu_put_be64s(f
, &cpu_ticks_offset
);
838 qemu_put_be64s(f
, &ticks_per_sec
);
839 qemu_put_be64s(f
, &cpu_clock_offset
);
842 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
844 if (version_id
!= 1 && version_id
!= 2)
846 if (cpu_ticks_enabled
) {
849 qemu_get_be64s(f
, &cpu_ticks_offset
);
850 qemu_get_be64s(f
, &ticks_per_sec
);
851 if (version_id
== 2) {
852 qemu_get_be64s(f
, &cpu_clock_offset
);
858 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
859 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
861 static void host_alarm_handler(int host_signum
)
865 #define DISP_FREQ 1000
867 static int64_t delta_min
= INT64_MAX
;
868 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
870 ti
= qemu_get_clock(vm_clock
);
871 if (last_clock
!= 0) {
872 delta
= ti
- last_clock
;
873 if (delta
< delta_min
)
875 if (delta
> delta_max
)
878 if (++count
== DISP_FREQ
) {
879 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
880 muldiv64(delta_min
, 1000000, ticks_per_sec
),
881 muldiv64(delta_max
, 1000000, ticks_per_sec
),
882 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
883 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
885 delta_min
= INT64_MAX
;
893 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
894 qemu_get_clock(vm_clock
)) ||
895 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
896 qemu_get_clock(rt_clock
))) {
898 SetEvent(host_alarm
);
900 CPUState
*env
= cpu_single_env
;
902 /* stop the currently executing cpu because a timer occured */
903 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
905 if (env
->kqemu_enabled
) {
906 kqemu_cpu_interrupt(env
);
915 #if defined(__linux__)
917 #define RTC_FREQ 1024
921 static int start_rtc_timer(void)
923 rtc_fd
= open("/dev/rtc", O_RDONLY
);
926 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
927 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
928 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
929 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
932 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
937 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
943 static int start_rtc_timer(void)
948 #endif /* !defined(__linux__) */
950 #endif /* !defined(_WIN32) */
952 static void init_timer_alarm(void)
959 ZeroMemory(&tc
, sizeof(TIMECAPS
));
960 timeGetDevCaps(&tc
, sizeof(TIMECAPS
));
961 if (period
< tc
.wPeriodMin
)
962 period
= tc
.wPeriodMin
;
963 timeBeginPeriod(period
);
964 timerID
= timeSetEvent(1, // interval (ms)
965 period
, // resolution
966 host_alarm_handler
, // function
967 (DWORD
)&count
, // user parameter
968 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
970 perror("failed timer alarm");
973 host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
975 perror("failed CreateEvent");
978 qemu_add_wait_object(host_alarm
, NULL
, NULL
);
980 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
983 struct sigaction act
;
984 struct itimerval itv
;
986 /* get times() syscall frequency */
987 timer_freq
= sysconf(_SC_CLK_TCK
);
990 sigfillset(&act
.sa_mask
);
992 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
993 act
.sa_flags
|= SA_ONSTACK
;
995 act
.sa_handler
= host_alarm_handler
;
996 sigaction(SIGALRM
, &act
, NULL
);
998 itv
.it_interval
.tv_sec
= 0;
999 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
1000 itv
.it_value
.tv_sec
= 0;
1001 itv
.it_value
.tv_usec
= 10 * 1000;
1002 setitimer(ITIMER_REAL
, &itv
, NULL
);
1003 /* we probe the tick duration of the kernel to inform the user if
1004 the emulated kernel requested a too high timer frequency */
1005 getitimer(ITIMER_REAL
, &itv
);
1007 #if defined(__linux__)
1008 /* XXX: force /dev/rtc usage because even 2.6 kernels may not
1009 have timers with 1 ms resolution. The correct solution will
1010 be to use the POSIX real time timers available in recent
1012 if (itv
.it_interval
.tv_usec
> 1000 || 1) {
1013 /* try to use /dev/rtc to have a faster timer */
1014 if (start_rtc_timer() < 0)
1016 /* disable itimer */
1017 itv
.it_interval
.tv_sec
= 0;
1018 itv
.it_interval
.tv_usec
= 0;
1019 itv
.it_value
.tv_sec
= 0;
1020 itv
.it_value
.tv_usec
= 0;
1021 setitimer(ITIMER_REAL
, &itv
, NULL
);
1024 sigaction(SIGIO
, &act
, NULL
);
1025 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1026 fcntl(rtc_fd
, F_SETOWN
, getpid());
1028 #endif /* defined(__linux__) */
1031 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1032 PIT_FREQ
) / 1000000;
1038 void quit_timers(void)
1041 timeKillEvent(timerID
);
1042 timeEndPeriod(period
);
1044 CloseHandle(host_alarm
);
1050 /***********************************************************/
1051 /* character device */
1053 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1055 return s
->chr_write(s
, buf
, len
);
1058 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1062 return s
->chr_ioctl(s
, cmd
, arg
);
1065 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1070 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1071 qemu_chr_write(s
, buf
, strlen(buf
));
1075 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1077 if (s
->chr_send_event
)
1078 s
->chr_send_event(s
, event
);
1081 void qemu_chr_add_read_handler(CharDriverState
*s
,
1082 IOCanRWHandler
*fd_can_read
,
1083 IOReadHandler
*fd_read
, void *opaque
)
1085 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
1088 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
1090 s
->chr_event
= chr_event
;
1093 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1098 static void null_chr_add_read_handler(CharDriverState
*chr
,
1099 IOCanRWHandler
*fd_can_read
,
1100 IOReadHandler
*fd_read
, void *opaque
)
1104 static CharDriverState
*qemu_chr_open_null(void)
1106 CharDriverState
*chr
;
1108 chr
= qemu_mallocz(sizeof(CharDriverState
));
1111 chr
->chr_write
= null_chr_write
;
1112 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1118 static void socket_cleanup(void)
1123 static int socket_init(void)
1128 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1130 err
= WSAGetLastError();
1131 fprintf(stderr
, "WSAStartup: %d\n", err
);
1134 atexit(socket_cleanup
);
1138 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1144 ret
= send(fd
, buf
, len
, 0);
1147 errno
= WSAGetLastError();
1148 if (errno
!= WSAEWOULDBLOCK
) {
1151 } else if (ret
== 0) {
1161 void socket_set_nonblock(int fd
)
1163 unsigned long opt
= 1;
1164 ioctlsocket(fd
, FIONBIO
, &opt
);
1169 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1175 ret
= write(fd
, buf
, len
);
1177 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1179 } else if (ret
== 0) {
1189 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1191 return unix_write(fd
, buf
, len1
);
1194 void socket_set_nonblock(int fd
)
1196 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1198 #endif /* !_WIN32 */
1204 IOCanRWHandler
*fd_can_read
;
1205 IOReadHandler
*fd_read
;
1210 #define STDIO_MAX_CLIENTS 2
1212 static int stdio_nb_clients
;
1213 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1215 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1217 FDCharDriver
*s
= chr
->opaque
;
1218 return unix_write(s
->fd_out
, buf
, len
);
1221 static int fd_chr_read_poll(void *opaque
)
1223 CharDriverState
*chr
= opaque
;
1224 FDCharDriver
*s
= chr
->opaque
;
1226 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
1230 static void fd_chr_read(void *opaque
)
1232 CharDriverState
*chr
= opaque
;
1233 FDCharDriver
*s
= chr
->opaque
;
1238 if (len
> s
->max_size
)
1242 size
= read(s
->fd_in
, buf
, len
);
1244 /* FD has been closed. Remove it from the active list. */
1245 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1249 s
->fd_read(s
->fd_opaque
, buf
, size
);
1253 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1254 IOCanRWHandler
*fd_can_read
,
1255 IOReadHandler
*fd_read
, void *opaque
)
1257 FDCharDriver
*s
= chr
->opaque
;
1259 if (s
->fd_in
>= 0) {
1260 s
->fd_can_read
= fd_can_read
;
1261 s
->fd_read
= fd_read
;
1262 s
->fd_opaque
= opaque
;
1263 if (nographic
&& s
->fd_in
== 0) {
1265 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1266 fd_chr_read
, NULL
, chr
);
1271 /* open a character device to a unix fd */
1272 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1274 CharDriverState
*chr
;
1277 chr
= qemu_mallocz(sizeof(CharDriverState
));
1280 s
= qemu_mallocz(sizeof(FDCharDriver
));
1288 chr
->chr_write
= fd_chr_write
;
1289 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1293 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1297 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1300 return qemu_chr_open_fd(-1, fd_out
);
1303 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1306 char filename_in
[256], filename_out
[256];
1308 snprintf(filename_in
, 256, "%s.in", filename
);
1309 snprintf(filename_out
, 256, "%s.out", filename
);
1310 fd_in
= open(filename_in
, O_RDWR
| O_BINARY
);
1311 fd_out
= open(filename_out
, O_RDWR
| O_BINARY
);
1312 if (fd_in
< 0 || fd_out
< 0) {
1317 fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
);
1321 return qemu_chr_open_fd(fd_in
, fd_out
);
1325 /* for STDIO, we handle the case where several clients use it
1328 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1330 #define TERM_FIFO_MAX_SIZE 1
1332 static int term_got_escape
, client_index
;
1333 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1334 static int term_fifo_size
;
1335 static int term_timestamps
;
1336 static int64_t term_timestamps_start
;
1338 void term_print_help(void)
1341 "C-a h print this help\n"
1342 "C-a x exit emulator\n"
1343 "C-a s save disk data back to file (if -snapshot)\n"
1344 "C-a b send break (magic sysrq)\n"
1345 "C-a t toggle console timestamps\n"
1346 "C-a c switch between console and monitor\n"
1347 "C-a C-a send C-a\n"
1351 /* called when a char is received */
1352 static void stdio_received_byte(int ch
)
1354 if (term_got_escape
) {
1355 term_got_escape
= 0;
1366 for (i
= 0; i
< MAX_DISKS
; i
++) {
1368 bdrv_commit(bs_table
[i
]);
1373 if (client_index
< stdio_nb_clients
) {
1374 CharDriverState
*chr
;
1377 chr
= stdio_clients
[client_index
];
1379 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1384 if (client_index
>= stdio_nb_clients
)
1386 if (client_index
== 0) {
1387 /* send a new line in the monitor to get the prompt */
1393 term_timestamps
= !term_timestamps
;
1394 term_timestamps_start
= -1;
1399 } else if (ch
== TERM_ESCAPE
) {
1400 term_got_escape
= 1;
1403 if (client_index
< stdio_nb_clients
) {
1405 CharDriverState
*chr
;
1408 chr
= stdio_clients
[client_index
];
1410 if (s
->fd_can_read(s
->fd_opaque
) > 0) {
1412 s
->fd_read(s
->fd_opaque
, buf
, 1);
1413 } else if (term_fifo_size
== 0) {
1414 term_fifo
[term_fifo_size
++] = ch
;
1420 static int stdio_read_poll(void *opaque
)
1422 CharDriverState
*chr
;
1425 if (client_index
< stdio_nb_clients
) {
1426 chr
= stdio_clients
[client_index
];
1428 /* try to flush the queue if needed */
1429 if (term_fifo_size
!= 0 && s
->fd_can_read(s
->fd_opaque
) > 0) {
1430 s
->fd_read(s
->fd_opaque
, term_fifo
, 1);
1433 /* see if we can absorb more chars */
1434 if (term_fifo_size
== 0)
1443 static void stdio_read(void *opaque
)
1448 size
= read(0, buf
, 1);
1450 /* stdin has been closed. Remove it from the active list. */
1451 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
1455 stdio_received_byte(buf
[0]);
1458 static int stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1460 FDCharDriver
*s
= chr
->opaque
;
1461 if (!term_timestamps
) {
1462 return unix_write(s
->fd_out
, buf
, len
);
1467 for(i
= 0; i
< len
; i
++) {
1468 unix_write(s
->fd_out
, buf
+ i
, 1);
1469 if (buf
[i
] == '\n') {
1474 if (term_timestamps_start
== -1)
1475 term_timestamps_start
= ti
;
1476 ti
-= term_timestamps_start
;
1477 secs
= ti
/ 1000000000;
1478 snprintf(buf1
, sizeof(buf1
),
1479 "[%02d:%02d:%02d.%03d] ",
1483 (int)((ti
/ 1000000) % 1000));
1484 unix_write(s
->fd_out
, buf1
, strlen(buf1
));
1491 /* init terminal so that we can grab keys */
1492 static struct termios oldtty
;
1493 static int old_fd0_flags
;
1495 static void term_exit(void)
1497 tcsetattr (0, TCSANOW
, &oldtty
);
1498 fcntl(0, F_SETFL
, old_fd0_flags
);
1501 static void term_init(void)
1505 tcgetattr (0, &tty
);
1507 old_fd0_flags
= fcntl(0, F_GETFL
);
1509 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1510 |INLCR
|IGNCR
|ICRNL
|IXON
);
1511 tty
.c_oflag
|= OPOST
;
1512 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1513 /* if graphical mode, we allow Ctrl-C handling */
1515 tty
.c_lflag
&= ~ISIG
;
1516 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1519 tty
.c_cc
[VTIME
] = 0;
1521 tcsetattr (0, TCSANOW
, &tty
);
1525 fcntl(0, F_SETFL
, O_NONBLOCK
);
1528 static CharDriverState
*qemu_chr_open_stdio(void)
1530 CharDriverState
*chr
;
1533 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1535 chr
= qemu_chr_open_fd(0, 1);
1536 chr
->chr_write
= stdio_write
;
1537 if (stdio_nb_clients
== 0)
1538 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, NULL
);
1539 client_index
= stdio_nb_clients
;
1541 if (stdio_nb_clients
!= 0)
1543 chr
= qemu_chr_open_fd(0, 1);
1545 stdio_clients
[stdio_nb_clients
++] = chr
;
1546 if (stdio_nb_clients
== 1) {
1547 /* set the terminal in raw mode */
1553 #if defined(__linux__)
1554 static CharDriverState
*qemu_chr_open_pty(void)
1557 char slave_name
[1024];
1558 int master_fd
, slave_fd
;
1560 /* Not satisfying */
1561 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1565 /* Disabling local echo and line-buffered output */
1566 tcgetattr (master_fd
, &tty
);
1567 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1569 tty
.c_cc
[VTIME
] = 0;
1570 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1572 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1573 return qemu_chr_open_fd(master_fd
, master_fd
);
1576 static void tty_serial_init(int fd
, int speed
,
1577 int parity
, int data_bits
, int stop_bits
)
1583 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1584 speed
, parity
, data_bits
, stop_bits
);
1586 tcgetattr (fd
, &tty
);
1628 cfsetispeed(&tty
, spd
);
1629 cfsetospeed(&tty
, spd
);
1631 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1632 |INLCR
|IGNCR
|ICRNL
|IXON
);
1633 tty
.c_oflag
|= OPOST
;
1634 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1635 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1656 tty
.c_cflag
|= PARENB
;
1659 tty
.c_cflag
|= PARENB
| PARODD
;
1663 tty
.c_cflag
|= CSTOPB
;
1665 tcsetattr (fd
, TCSANOW
, &tty
);
1668 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1670 FDCharDriver
*s
= chr
->opaque
;
1673 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1675 QEMUSerialSetParams
*ssp
= arg
;
1676 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1677 ssp
->data_bits
, ssp
->stop_bits
);
1680 case CHR_IOCTL_SERIAL_SET_BREAK
:
1682 int enable
= *(int *)arg
;
1684 tcsendbreak(s
->fd_in
, 1);
1693 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1695 CharDriverState
*chr
;
1698 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1701 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1702 tty_serial_init(fd
, 115200, 'N', 8, 1);
1703 chr
= qemu_chr_open_fd(fd
, fd
);
1706 chr
->chr_ioctl
= tty_serial_ioctl
;
1710 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1712 int fd
= (int)chr
->opaque
;
1716 case CHR_IOCTL_PP_READ_DATA
:
1717 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1719 *(uint8_t *)arg
= b
;
1721 case CHR_IOCTL_PP_WRITE_DATA
:
1722 b
= *(uint8_t *)arg
;
1723 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1726 case CHR_IOCTL_PP_READ_CONTROL
:
1727 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1729 *(uint8_t *)arg
= b
;
1731 case CHR_IOCTL_PP_WRITE_CONTROL
:
1732 b
= *(uint8_t *)arg
;
1733 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1736 case CHR_IOCTL_PP_READ_STATUS
:
1737 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1739 *(uint8_t *)arg
= b
;
1747 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1749 CharDriverState
*chr
;
1752 fd
= open(filename
, O_RDWR
);
1756 if (ioctl(fd
, PPCLAIM
) < 0) {
1761 chr
= qemu_mallocz(sizeof(CharDriverState
));
1766 chr
->opaque
= (void *)fd
;
1767 chr
->chr_write
= null_chr_write
;
1768 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1769 chr
->chr_ioctl
= pp_ioctl
;
1774 static CharDriverState
*qemu_chr_open_pty(void)
1780 #endif /* !defined(_WIN32) */
1784 IOCanRWHandler
*fd_can_read
;
1785 IOReadHandler
*fd_read
;
1788 HANDLE hcom
, hrecv
, hsend
;
1789 OVERLAPPED orecv
, osend
;
1794 #define NSENDBUF 2048
1795 #define NRECVBUF 2048
1796 #define MAXCONNECT 1
1797 #define NTIMEOUT 5000
1799 static int win_chr_poll(void *opaque
);
1800 static int win_chr_pipe_poll(void *opaque
);
1802 static void win_chr_close2(WinCharState
*s
)
1805 CloseHandle(s
->hsend
);
1809 CloseHandle(s
->hrecv
);
1813 CloseHandle(s
->hcom
);
1817 qemu_del_polling_cb(win_chr_pipe_poll
, s
);
1819 qemu_del_polling_cb(win_chr_poll
, s
);
1822 static void win_chr_close(CharDriverState
*chr
)
1824 WinCharState
*s
= chr
->opaque
;
1828 static int win_chr_init(WinCharState
*s
, const char *filename
)
1831 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1836 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1838 fprintf(stderr
, "Failed CreateEvent\n");
1841 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1843 fprintf(stderr
, "Failed CreateEvent\n");
1847 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1848 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1849 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1850 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1855 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1856 fprintf(stderr
, "Failed SetupComm\n");
1860 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1861 size
= sizeof(COMMCONFIG
);
1862 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1863 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1864 CommConfigDialog(filename
, NULL
, &comcfg
);
1866 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1867 fprintf(stderr
, "Failed SetCommState\n");
1871 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1872 fprintf(stderr
, "Failed SetCommMask\n");
1876 cto
.ReadIntervalTimeout
= MAXDWORD
;
1877 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1878 fprintf(stderr
, "Failed SetCommTimeouts\n");
1882 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1883 fprintf(stderr
, "Failed ClearCommError\n");
1886 qemu_add_polling_cb(win_chr_poll
, s
);
1894 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1896 WinCharState
*s
= chr
->opaque
;
1897 DWORD len
, ret
, size
, err
;
1900 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1901 s
->osend
.hEvent
= s
->hsend
;
1904 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1906 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1908 err
= GetLastError();
1909 if (err
== ERROR_IO_PENDING
) {
1910 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1928 static int win_chr_read_poll(WinCharState
*s
)
1930 s
->max_size
= s
->fd_can_read(s
->win_opaque
);
1934 static void win_chr_readfile(WinCharState
*s
)
1940 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1941 s
->orecv
.hEvent
= s
->hrecv
;
1942 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1944 err
= GetLastError();
1945 if (err
== ERROR_IO_PENDING
) {
1946 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1951 s
->fd_read(s
->win_opaque
, buf
, size
);
1955 static void win_chr_read(WinCharState
*s
)
1957 if (s
->len
> s
->max_size
)
1958 s
->len
= s
->max_size
;
1962 win_chr_readfile(s
);
1965 static int win_chr_poll(void *opaque
)
1967 WinCharState
*s
= opaque
;
1971 ClearCommError(s
->hcom
, &comerr
, &status
);
1972 if (status
.cbInQue
> 0) {
1973 s
->len
= status
.cbInQue
;
1974 win_chr_read_poll(s
);
1981 static void win_chr_add_read_handler(CharDriverState
*chr
,
1982 IOCanRWHandler
*fd_can_read
,
1983 IOReadHandler
*fd_read
, void *opaque
)
1985 WinCharState
*s
= chr
->opaque
;
1987 s
->fd_can_read
= fd_can_read
;
1988 s
->fd_read
= fd_read
;
1989 s
->win_opaque
= opaque
;
1992 static CharDriverState
*qemu_chr_open_win(const char *filename
)
1994 CharDriverState
*chr
;
1997 chr
= qemu_mallocz(sizeof(CharDriverState
));
2000 s
= qemu_mallocz(sizeof(WinCharState
));
2006 chr
->chr_write
= win_chr_write
;
2007 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2008 chr
->chr_close
= win_chr_close
;
2010 if (win_chr_init(s
, filename
) < 0) {
2018 static int win_chr_pipe_poll(void *opaque
)
2020 WinCharState
*s
= opaque
;
2023 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2026 win_chr_read_poll(s
);
2033 static int win_chr_pipe_init(WinCharState
*s
, const char *filename
)
2042 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2044 fprintf(stderr
, "Failed CreateEvent\n");
2047 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2049 fprintf(stderr
, "Failed CreateEvent\n");
2053 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2054 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2055 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2057 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2058 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2059 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2064 ZeroMemory(&ov
, sizeof(ov
));
2065 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2066 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2068 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2072 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2074 fprintf(stderr
, "Failed GetOverlappedResult\n");
2076 CloseHandle(ov
.hEvent
);
2083 CloseHandle(ov
.hEvent
);
2086 qemu_add_polling_cb(win_chr_pipe_poll
, s
);
2095 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2097 CharDriverState
*chr
;
2100 chr
= qemu_mallocz(sizeof(CharDriverState
));
2103 s
= qemu_mallocz(sizeof(WinCharState
));
2109 chr
->chr_write
= win_chr_write
;
2110 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2111 chr
->chr_close
= win_chr_close
;
2113 if (win_chr_pipe_init(s
, filename
) < 0) {
2121 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2123 CharDriverState
*chr
;
2126 chr
= qemu_mallocz(sizeof(CharDriverState
));
2129 s
= qemu_mallocz(sizeof(WinCharState
));
2136 chr
->chr_write
= win_chr_write
;
2137 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2141 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2145 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2146 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2147 if (fd_out
== INVALID_HANDLE_VALUE
)
2150 return qemu_chr_open_win_file(fd_out
);
2154 /***********************************************************/
2155 /* UDP Net console */
2158 IOCanRWHandler
*fd_can_read
;
2159 IOReadHandler
*fd_read
;
2162 struct sockaddr_in daddr
;
2169 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2171 NetCharDriver
*s
= chr
->opaque
;
2173 return sendto(s
->fd
, buf
, len
, 0,
2174 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2177 static int udp_chr_read_poll(void *opaque
)
2179 CharDriverState
*chr
= opaque
;
2180 NetCharDriver
*s
= chr
->opaque
;
2182 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2184 /* If there were any stray characters in the queue process them
2187 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2188 s
->fd_read(s
->fd_opaque
, &s
->buf
[s
->bufptr
], 1);
2190 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2195 static void udp_chr_read(void *opaque
)
2197 CharDriverState
*chr
= opaque
;
2198 NetCharDriver
*s
= chr
->opaque
;
2200 if (s
->max_size
== 0)
2202 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2203 s
->bufptr
= s
->bufcnt
;
2208 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2209 s
->fd_read(s
->fd_opaque
, &s
->buf
[s
->bufptr
], 1);
2211 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2215 static void udp_chr_add_read_handler(CharDriverState
*chr
,
2216 IOCanRWHandler
*fd_can_read
,
2217 IOReadHandler
*fd_read
, void *opaque
)
2219 NetCharDriver
*s
= chr
->opaque
;
2222 s
->fd_can_read
= fd_can_read
;
2223 s
->fd_read
= fd_read
;
2224 s
->fd_opaque
= opaque
;
2225 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2226 udp_chr_read
, NULL
, chr
);
2230 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2232 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2234 int parse_host_src_port(struct sockaddr_in
*haddr
,
2235 struct sockaddr_in
*saddr
,
2238 static CharDriverState
*qemu_chr_open_udp(const char *def
)
2240 CharDriverState
*chr
= NULL
;
2241 NetCharDriver
*s
= NULL
;
2243 struct sockaddr_in saddr
;
2245 chr
= qemu_mallocz(sizeof(CharDriverState
));
2248 s
= qemu_mallocz(sizeof(NetCharDriver
));
2252 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2254 perror("socket(PF_INET, SOCK_DGRAM)");
2258 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2259 printf("Could not parse: %s\n", def
);
2263 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2273 chr
->chr_write
= udp_chr_write
;
2274 chr
->chr_add_read_handler
= udp_chr_add_read_handler
;
2287 /***********************************************************/
2288 /* TCP Net console */
2291 IOCanRWHandler
*fd_can_read
;
2292 IOReadHandler
*fd_read
;
2301 static void tcp_chr_accept(void *opaque
);
2303 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2305 TCPCharDriver
*s
= chr
->opaque
;
2307 return send_all(s
->fd
, buf
, len
);
2309 /* XXX: indicate an error ? */
2314 static int tcp_chr_read_poll(void *opaque
)
2316 CharDriverState
*chr
= opaque
;
2317 TCPCharDriver
*s
= chr
->opaque
;
2320 if (!s
->fd_can_read
)
2322 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2327 #define IAC_BREAK 243
2328 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2330 char *buf
, int *size
)
2332 /* Handle any telnet client's basic IAC options to satisfy char by
2333 * char mode with no echo. All IAC options will be removed from
2334 * the buf and the do_telnetopt variable will be used to track the
2335 * state of the width of the IAC information.
2337 * IAC commands come in sets of 3 bytes with the exception of the
2338 * "IAC BREAK" command and the double IAC.
2344 for (i
= 0; i
< *size
; i
++) {
2345 if (s
->do_telnetopt
> 1) {
2346 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2347 /* Double IAC means send an IAC */
2351 s
->do_telnetopt
= 1;
2353 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2354 /* Handle IAC break commands by sending a serial break */
2355 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
2360 if (s
->do_telnetopt
>= 4) {
2361 s
->do_telnetopt
= 1;
2364 if ((unsigned char)buf
[i
] == IAC
) {
2365 s
->do_telnetopt
= 2;
2376 static void tcp_chr_read(void *opaque
)
2378 CharDriverState
*chr
= opaque
;
2379 TCPCharDriver
*s
= chr
->opaque
;
2383 if (!s
->connected
|| s
->max_size
<= 0)
2386 if (len
> s
->max_size
)
2388 size
= recv(s
->fd
, buf
, len
, 0);
2390 /* connection closed */
2392 if (s
->listen_fd
>= 0) {
2393 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2395 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2398 } else if (size
> 0) {
2399 if (s
->do_telnetopt
)
2400 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2402 s
->fd_read(s
->fd_opaque
, buf
, size
);
2406 static void tcp_chr_add_read_handler(CharDriverState
*chr
,
2407 IOCanRWHandler
*fd_can_read
,
2408 IOReadHandler
*fd_read
, void *opaque
)
2410 TCPCharDriver
*s
= chr
->opaque
;
2412 s
->fd_can_read
= fd_can_read
;
2413 s
->fd_read
= fd_read
;
2414 s
->fd_opaque
= opaque
;
2417 static void tcp_chr_connect(void *opaque
)
2419 CharDriverState
*chr
= opaque
;
2420 TCPCharDriver
*s
= chr
->opaque
;
2423 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2424 tcp_chr_read
, NULL
, chr
);
2427 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2428 static void tcp_chr_telnet_init(int fd
)
2431 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2432 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2433 send(fd
, (char *)buf
, 3, 0);
2434 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2435 send(fd
, (char *)buf
, 3, 0);
2436 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2437 send(fd
, (char *)buf
, 3, 0);
2438 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2439 send(fd
, (char *)buf
, 3, 0);
2442 static void tcp_chr_accept(void *opaque
)
2444 CharDriverState
*chr
= opaque
;
2445 TCPCharDriver
*s
= chr
->opaque
;
2446 struct sockaddr_in saddr
;
2448 struct sockaddr_un uaddr
;
2450 struct sockaddr
*addr
;
2457 len
= sizeof(uaddr
);
2458 addr
= (struct sockaddr
*)&uaddr
;
2462 len
= sizeof(saddr
);
2463 addr
= (struct sockaddr
*)&saddr
;
2465 fd
= accept(s
->listen_fd
, addr
, &len
);
2466 if (fd
< 0 && errno
!= EINTR
) {
2468 } else if (fd
>= 0) {
2469 if (s
->do_telnetopt
)
2470 tcp_chr_telnet_init(fd
);
2474 socket_set_nonblock(fd
);
2476 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2477 tcp_chr_connect(chr
);
2480 static void tcp_chr_close(CharDriverState
*chr
)
2482 TCPCharDriver
*s
= chr
->opaque
;
2485 if (s
->listen_fd
>= 0)
2486 closesocket(s
->listen_fd
);
2490 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2494 CharDriverState
*chr
= NULL
;
2495 TCPCharDriver
*s
= NULL
;
2496 int fd
= -1, ret
, err
, val
;
2498 int is_waitconnect
= 1;
2500 struct sockaddr_in saddr
;
2502 struct sockaddr_un uaddr
;
2504 struct sockaddr
*addr
;
2509 addr
= (struct sockaddr
*)&uaddr
;
2510 addrlen
= sizeof(uaddr
);
2511 if (parse_unix_path(&uaddr
, host_str
) < 0)
2516 addr
= (struct sockaddr
*)&saddr
;
2517 addrlen
= sizeof(saddr
);
2518 if (parse_host_port(&saddr
, host_str
) < 0)
2523 while((ptr
= strchr(ptr
,','))) {
2525 if (!strncmp(ptr
,"server",6)) {
2527 } else if (!strncmp(ptr
,"nowait",6)) {
2530 printf("Unknown option: %s\n", ptr
);
2537 chr
= qemu_mallocz(sizeof(CharDriverState
));
2540 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2546 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2549 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2554 if (!is_waitconnect
)
2555 socket_set_nonblock(fd
);
2560 s
->is_unix
= is_unix
;
2563 chr
->chr_write
= tcp_chr_write
;
2564 chr
->chr_add_read_handler
= tcp_chr_add_read_handler
;
2565 chr
->chr_close
= tcp_chr_close
;
2568 /* allow fast reuse */
2572 strncpy(path
, uaddr
.sun_path
, 108);
2579 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2582 ret
= bind(fd
, addr
, addrlen
);
2586 ret
= listen(fd
, 0);
2591 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2593 s
->do_telnetopt
= 1;
2596 ret
= connect(fd
, addr
, addrlen
);
2598 err
= socket_error();
2599 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2600 } else if (err
== EINPROGRESS
) {
2612 tcp_chr_connect(chr
);
2614 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2617 if (is_listen
&& is_waitconnect
) {
2618 printf("QEMU waiting for connection on: %s\n", host_str
);
2619 tcp_chr_accept(chr
);
2620 socket_set_nonblock(s
->listen_fd
);
2632 CharDriverState
*qemu_chr_open(const char *filename
)
2636 if (!strcmp(filename
, "vc")) {
2637 return text_console_init(&display_state
);
2638 } else if (!strcmp(filename
, "null")) {
2639 return qemu_chr_open_null();
2641 if (strstart(filename
, "tcp:", &p
)) {
2642 return qemu_chr_open_tcp(p
, 0, 0);
2644 if (strstart(filename
, "telnet:", &p
)) {
2645 return qemu_chr_open_tcp(p
, 1, 0);
2647 if (strstart(filename
, "udp:", &p
)) {
2648 return qemu_chr_open_udp(p
);
2651 if (strstart(filename
, "unix:", &p
)) {
2652 return qemu_chr_open_tcp(p
, 0, 1);
2653 } else if (strstart(filename
, "file:", &p
)) {
2654 return qemu_chr_open_file_out(p
);
2655 } else if (strstart(filename
, "pipe:", &p
)) {
2656 return qemu_chr_open_pipe(p
);
2657 } else if (!strcmp(filename
, "pty")) {
2658 return qemu_chr_open_pty();
2659 } else if (!strcmp(filename
, "stdio")) {
2660 return qemu_chr_open_stdio();
2663 #if defined(__linux__)
2664 if (strstart(filename
, "/dev/parport", NULL
)) {
2665 return qemu_chr_open_pp(filename
);
2667 if (strstart(filename
, "/dev/", NULL
)) {
2668 return qemu_chr_open_tty(filename
);
2672 if (strstart(filename
, "COM", NULL
)) {
2673 return qemu_chr_open_win(filename
);
2675 if (strstart(filename
, "pipe:", &p
)) {
2676 return qemu_chr_open_win_pipe(p
);
2678 if (strstart(filename
, "file:", &p
)) {
2679 return qemu_chr_open_win_file_out(p
);
2687 void qemu_chr_close(CharDriverState
*chr
)
2690 chr
->chr_close(chr
);
2693 /***********************************************************/
2694 /* network device redirectors */
2696 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2700 for(i
=0;i
<size
;i
+=16) {
2704 fprintf(f
, "%08x ", i
);
2707 fprintf(f
, " %02x", buf
[i
+j
]);
2712 for(j
=0;j
<len
;j
++) {
2714 if (c
< ' ' || c
> '~')
2716 fprintf(f
, "%c", c
);
2722 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
2725 for(i
= 0; i
< 6; i
++) {
2726 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2739 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
2744 p1
= strchr(p
, sep
);
2750 if (len
> buf_size
- 1)
2752 memcpy(buf
, p
, len
);
2759 int parse_host_src_port(struct sockaddr_in
*haddr
,
2760 struct sockaddr_in
*saddr
,
2761 const char *input_str
)
2763 char *str
= strdup(input_str
);
2764 char *host_str
= str
;
2769 * Chop off any extra arguments at the end of the string which
2770 * would start with a comma, then fill in the src port information
2771 * if it was provided else use the "any address" and "any port".
2773 if ((ptr
= strchr(str
,',')))
2776 if ((src_str
= strchr(input_str
,'@'))) {
2781 if (parse_host_port(haddr
, host_str
) < 0)
2784 if (!src_str
|| *src_str
== '\0')
2787 if (parse_host_port(saddr
, src_str
) < 0)
2798 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
2806 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2808 saddr
->sin_family
= AF_INET
;
2809 if (buf
[0] == '\0') {
2810 saddr
->sin_addr
.s_addr
= 0;
2812 if (isdigit(buf
[0])) {
2813 if (!inet_aton(buf
, &saddr
->sin_addr
))
2816 if ((he
= gethostbyname(buf
)) == NULL
)
2818 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
2821 port
= strtol(p
, (char **)&r
, 0);
2824 saddr
->sin_port
= htons(port
);
2829 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
2834 len
= MIN(108, strlen(str
));
2835 p
= strchr(str
, ',');
2837 len
= MIN(len
, p
- str
);
2839 memset(uaddr
, 0, sizeof(*uaddr
));
2841 uaddr
->sun_family
= AF_UNIX
;
2842 memcpy(uaddr
->sun_path
, str
, len
);
2848 /* find or alloc a new VLAN */
2849 VLANState
*qemu_find_vlan(int id
)
2851 VLANState
**pvlan
, *vlan
;
2852 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2856 vlan
= qemu_mallocz(sizeof(VLANState
));
2861 pvlan
= &first_vlan
;
2862 while (*pvlan
!= NULL
)
2863 pvlan
= &(*pvlan
)->next
;
2868 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
2869 IOReadHandler
*fd_read
,
2870 IOCanRWHandler
*fd_can_read
,
2873 VLANClientState
*vc
, **pvc
;
2874 vc
= qemu_mallocz(sizeof(VLANClientState
));
2877 vc
->fd_read
= fd_read
;
2878 vc
->fd_can_read
= fd_can_read
;
2879 vc
->opaque
= opaque
;
2883 pvc
= &vlan
->first_client
;
2884 while (*pvc
!= NULL
)
2885 pvc
= &(*pvc
)->next
;
2890 int qemu_can_send_packet(VLANClientState
*vc1
)
2892 VLANState
*vlan
= vc1
->vlan
;
2893 VLANClientState
*vc
;
2895 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2897 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
2904 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
2906 VLANState
*vlan
= vc1
->vlan
;
2907 VLANClientState
*vc
;
2910 printf("vlan %d send:\n", vlan
->id
);
2911 hex_dump(stdout
, buf
, size
);
2913 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2915 vc
->fd_read(vc
->opaque
, buf
, size
);
2920 #if defined(CONFIG_SLIRP)
2922 /* slirp network adapter */
2924 static int slirp_inited
;
2925 static VLANClientState
*slirp_vc
;
2927 int slirp_can_output(void)
2929 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
2932 void slirp_output(const uint8_t *pkt
, int pkt_len
)
2935 printf("slirp output:\n");
2936 hex_dump(stdout
, pkt
, pkt_len
);
2940 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
2943 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
2946 printf("slirp input:\n");
2947 hex_dump(stdout
, buf
, size
);
2949 slirp_input(buf
, size
);
2952 static int net_slirp_init(VLANState
*vlan
)
2954 if (!slirp_inited
) {
2958 slirp_vc
= qemu_new_vlan_client(vlan
,
2959 slirp_receive
, NULL
, NULL
);
2960 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
2964 static void net_slirp_redir(const char *redir_str
)
2969 struct in_addr guest_addr
;
2970 int host_port
, guest_port
;
2972 if (!slirp_inited
) {
2978 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2980 if (!strcmp(buf
, "tcp")) {
2982 } else if (!strcmp(buf
, "udp")) {
2988 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2990 host_port
= strtol(buf
, &r
, 0);
2994 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2996 if (buf
[0] == '\0') {
2997 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
2999 if (!inet_aton(buf
, &guest_addr
))
3002 guest_port
= strtol(p
, &r
, 0);
3006 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3007 fprintf(stderr
, "qemu: could not set up redirection\n");
3012 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3020 static void smb_exit(void)
3024 char filename
[1024];
3026 /* erase all the files in the directory */
3027 d
= opendir(smb_dir
);
3032 if (strcmp(de
->d_name
, ".") != 0 &&
3033 strcmp(de
->d_name
, "..") != 0) {
3034 snprintf(filename
, sizeof(filename
), "%s/%s",
3035 smb_dir
, de
->d_name
);
3043 /* automatic user mode samba server configuration */
3044 void net_slirp_smb(const char *exported_dir
)
3046 char smb_conf
[1024];
3047 char smb_cmdline
[1024];
3050 if (!slirp_inited
) {
3055 /* XXX: better tmp dir construction */
3056 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3057 if (mkdir(smb_dir
, 0700) < 0) {
3058 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3061 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3063 f
= fopen(smb_conf
, "w");
3065 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3072 "socket address=127.0.0.1\n"
3073 "pid directory=%s\n"
3074 "lock directory=%s\n"
3075 "log file=%s/log.smbd\n"
3076 "smb passwd file=%s/smbpasswd\n"
3077 "security = share\n"
3092 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3093 SMBD_COMMAND
, smb_conf
);
3095 slirp_add_exec(0, smb_cmdline
, 4, 139);
3098 #endif /* !defined(_WIN32) */
3100 #endif /* CONFIG_SLIRP */
3102 #if !defined(_WIN32)
3104 typedef struct TAPState
{
3105 VLANClientState
*vc
;
3109 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3111 TAPState
*s
= opaque
;
3114 ret
= write(s
->fd
, buf
, size
);
3115 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3122 static void tap_send(void *opaque
)
3124 TAPState
*s
= opaque
;
3128 size
= read(s
->fd
, buf
, sizeof(buf
));
3130 qemu_send_packet(s
->vc
, buf
, size
);
3136 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3140 s
= qemu_mallocz(sizeof(TAPState
));
3144 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3145 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3146 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3151 static int tap_open(char *ifname
, int ifname_size
)
3157 fd
= open("/dev/tap", O_RDWR
);
3159 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3164 dev
= devname(s
.st_rdev
, S_IFCHR
);
3165 pstrcpy(ifname
, ifname_size
, dev
);
3167 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3170 #elif defined(__sun__)
3171 static int tap_open(char *ifname
, int ifname_size
)
3173 fprintf(stderr
, "warning: tap_open not yet implemented\n");
3177 static int tap_open(char *ifname
, int ifname_size
)
3182 fd
= open("/dev/net/tun", O_RDWR
);
3184 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
3187 memset(&ifr
, 0, sizeof(ifr
));
3188 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
3189 if (ifname
[0] != '\0')
3190 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
3192 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
3193 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
3195 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
3199 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
3200 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3205 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
3206 const char *setup_script
)
3209 int pid
, status
, fd
;
3214 if (ifname1
!= NULL
)
3215 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
3218 fd
= tap_open(ifname
, sizeof(ifname
));
3224 if (setup_script
[0] != '\0') {
3225 /* try to launch network init script */
3230 *parg
++ = (char *)setup_script
;
3233 execv(setup_script
, args
);
3236 while (waitpid(pid
, &status
, 0) != pid
);
3237 if (!WIFEXITED(status
) ||
3238 WEXITSTATUS(status
) != 0) {
3239 fprintf(stderr
, "%s: could not launch network script\n",
3245 s
= net_tap_fd_init(vlan
, fd
);
3248 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3249 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
3253 #endif /* !_WIN32 */
3255 /* network connection */
3256 typedef struct NetSocketState
{
3257 VLANClientState
*vc
;
3259 int state
; /* 0 = getting length, 1 = getting data */
3263 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
3266 typedef struct NetSocketListenState
{
3269 } NetSocketListenState
;
3271 /* XXX: we consider we can send the whole packet without blocking */
3272 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
3274 NetSocketState
*s
= opaque
;
3278 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
3279 send_all(s
->fd
, buf
, size
);
3282 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
3284 NetSocketState
*s
= opaque
;
3285 sendto(s
->fd
, buf
, size
, 0,
3286 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
3289 static void net_socket_send(void *opaque
)
3291 NetSocketState
*s
= opaque
;
3296 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
3298 err
= socket_error();
3299 if (err
!= EWOULDBLOCK
)
3301 } else if (size
== 0) {
3302 /* end of connection */
3304 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3310 /* reassemble a packet from the network */
3316 memcpy(s
->buf
+ s
->index
, buf
, l
);
3320 if (s
->index
== 4) {
3322 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
3328 l
= s
->packet_len
- s
->index
;
3331 memcpy(s
->buf
+ s
->index
, buf
, l
);
3335 if (s
->index
>= s
->packet_len
) {
3336 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
3345 static void net_socket_send_dgram(void *opaque
)
3347 NetSocketState
*s
= opaque
;
3350 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
3354 /* end of connection */
3355 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3358 qemu_send_packet(s
->vc
, s
->buf
, size
);
3361 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
3366 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
3367 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
3368 inet_ntoa(mcastaddr
->sin_addr
),
3369 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
3373 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
3375 perror("socket(PF_INET, SOCK_DGRAM)");
3380 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
3381 (const char *)&val
, sizeof(val
));
3383 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
3387 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
3393 /* Add host to multicast group */
3394 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
3395 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
3397 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
3398 (const char *)&imr
, sizeof(struct ip_mreq
));
3400 perror("setsockopt(IP_ADD_MEMBERSHIP)");
3404 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
3406 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
3407 (const char *)&val
, sizeof(val
));
3409 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
3413 socket_set_nonblock(fd
);
3421 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
3424 struct sockaddr_in saddr
;
3426 socklen_t saddr_len
;
3429 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
3430 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
3431 * by ONLY ONE process: we must "clone" this dgram socket --jjo
3435 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
3437 if (saddr
.sin_addr
.s_addr
==0) {
3438 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
3442 /* clone dgram socket */
3443 newfd
= net_socket_mcast_create(&saddr
);
3445 /* error already reported by net_socket_mcast_create() */
3449 /* clone newfd to fd, close newfd */
3454 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
3455 fd
, strerror(errno
));
3460 s
= qemu_mallocz(sizeof(NetSocketState
));
3465 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
3466 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
3468 /* mcast: save bound address as dst */
3469 if (is_connected
) s
->dgram_dst
=saddr
;
3471 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3472 "socket: fd=%d (%s mcast=%s:%d)",
3473 fd
, is_connected
? "cloned" : "",
3474 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3478 static void net_socket_connect(void *opaque
)
3480 NetSocketState
*s
= opaque
;
3481 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
3484 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
3488 s
= qemu_mallocz(sizeof(NetSocketState
));
3492 s
->vc
= qemu_new_vlan_client(vlan
,
3493 net_socket_receive
, NULL
, s
);
3494 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3495 "socket: fd=%d", fd
);
3497 net_socket_connect(s
);
3499 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
3504 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
3507 int so_type
=-1, optlen
=sizeof(so_type
);
3509 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
3510 fprintf(stderr
, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd
);
3515 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
3517 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3519 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
3520 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
3521 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3526 static void net_socket_accept(void *opaque
)
3528 NetSocketListenState
*s
= opaque
;
3530 struct sockaddr_in saddr
;
3535 len
= sizeof(saddr
);
3536 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
3537 if (fd
< 0 && errno
!= EINTR
) {
3539 } else if (fd
>= 0) {
3543 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
3547 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
3548 "socket: connection from %s:%d",
3549 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3553 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
3555 NetSocketListenState
*s
;
3557 struct sockaddr_in saddr
;
3559 if (parse_host_port(&saddr
, host_str
) < 0)
3562 s
= qemu_mallocz(sizeof(NetSocketListenState
));
3566 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3571 socket_set_nonblock(fd
);
3573 /* allow fast reuse */
3575 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3577 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3582 ret
= listen(fd
, 0);
3589 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
3593 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
3596 int fd
, connected
, ret
, err
;
3597 struct sockaddr_in saddr
;
3599 if (parse_host_port(&saddr
, host_str
) < 0)
3602 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3607 socket_set_nonblock(fd
);
3611 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3613 err
= socket_error();
3614 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3615 } else if (err
== EINPROGRESS
) {
3627 s
= net_socket_fd_init(vlan
, fd
, connected
);
3630 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3631 "socket: connect to %s:%d",
3632 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3636 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
3640 struct sockaddr_in saddr
;
3642 if (parse_host_port(&saddr
, host_str
) < 0)
3646 fd
= net_socket_mcast_create(&saddr
);
3650 s
= net_socket_fd_init(vlan
, fd
, 0);
3654 s
->dgram_dst
= saddr
;
3656 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3657 "socket: mcast=%s:%d",
3658 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3663 static int get_param_value(char *buf
, int buf_size
,
3664 const char *tag
, const char *str
)
3673 while (*p
!= '\0' && *p
!= '=') {
3674 if ((q
- option
) < sizeof(option
) - 1)
3682 if (!strcmp(tag
, option
)) {
3684 while (*p
!= '\0' && *p
!= ',') {
3685 if ((q
- buf
) < buf_size
- 1)
3692 while (*p
!= '\0' && *p
!= ',') {
3703 static int net_client_init(const char *str
)
3714 while (*p
!= '\0' && *p
!= ',') {
3715 if ((q
- device
) < sizeof(device
) - 1)
3723 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
3724 vlan_id
= strtol(buf
, NULL
, 0);
3726 vlan
= qemu_find_vlan(vlan_id
);
3728 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
3731 if (!strcmp(device
, "nic")) {
3735 if (nb_nics
>= MAX_NICS
) {
3736 fprintf(stderr
, "Too Many NICs\n");
3739 nd
= &nd_table
[nb_nics
];
3740 macaddr
= nd
->macaddr
;
3746 macaddr
[5] = 0x56 + nb_nics
;
3748 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
3749 if (parse_macaddr(macaddr
, buf
) < 0) {
3750 fprintf(stderr
, "invalid syntax for ethernet address\n");
3754 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
3755 nd
->model
= strdup(buf
);
3761 if (!strcmp(device
, "none")) {
3762 /* does nothing. It is needed to signal that no network cards
3767 if (!strcmp(device
, "user")) {
3768 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
3769 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
3771 ret
= net_slirp_init(vlan
);
3775 if (!strcmp(device
, "tap")) {
3777 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
3778 fprintf(stderr
, "tap: no interface name\n");
3781 ret
= tap_win32_init(vlan
, ifname
);
3784 if (!strcmp(device
, "tap")) {
3786 char setup_script
[1024];
3788 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3789 fd
= strtol(buf
, NULL
, 0);
3791 if (net_tap_fd_init(vlan
, fd
))
3794 get_param_value(ifname
, sizeof(ifname
), "ifname", p
);
3795 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
3796 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
3798 ret
= net_tap_init(vlan
, ifname
, setup_script
);
3802 if (!strcmp(device
, "socket")) {
3803 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3805 fd
= strtol(buf
, NULL
, 0);
3807 if (net_socket_fd_init(vlan
, fd
, 1))
3809 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
3810 ret
= net_socket_listen_init(vlan
, buf
);
3811 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
3812 ret
= net_socket_connect_init(vlan
, buf
);
3813 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
3814 ret
= net_socket_mcast_init(vlan
, buf
);
3816 fprintf(stderr
, "Unknown socket options: %s\n", p
);
3821 fprintf(stderr
, "Unknown network device: %s\n", device
);
3825 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
3831 void do_info_network(void)
3834 VLANClientState
*vc
;
3836 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3837 term_printf("VLAN %d devices:\n", vlan
->id
);
3838 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3839 term_printf(" %s\n", vc
->info_str
);
3843 /* Parse IDE and SCSI disk options */
3844 static int disk_options_init(int num_ide_disks
,
3845 char ide_disk_options
[][DISK_OPTIONS_SIZE
],
3848 char scsi_disk_options
[][DISK_OPTIONS_SIZE
],
3859 int ide_cdrom_created
= 0;
3861 scsi_host_adapters temp_adapter
;
3863 /* Process any IDE disks/cdroms */
3864 for (i
=0; i
< num_ide_disks
; i
++) {
3865 for (j
=0; j
<MAX_DISKS
; j
++) {
3866 if (ide_disk_options
[j
][0] == '\0')
3869 if (get_param_value(buf
, sizeof(buf
),"type",ide_disk_options
[j
])) {
3870 if (!strcmp(buf
, "disk")) {
3872 } else if (!strcmp(buf
, "cdrom")) {
3874 ide_cdrom_created
= 1;
3876 fprintf(stderr
, "qemu: invalid IDE disk type= value: %s\n", buf
);
3884 snprintf(dev_name
, sizeof(dev_name
), "cdrom%c", i
+ '0');
3886 snprintf(dev_name
, sizeof(dev_name
), "hd%c", i
+ 'a');
3889 if (!(get_param_value(buf
, sizeof(buf
),"img",ide_disk_options
[j
]))) {
3890 fprintf(stderr
, "qemu: missing IDE disk img= value.\n");
3894 if (!(bs_table
[i
] = bdrv_new(dev_name
))) {
3895 fprintf(stderr
, "qemu: unable to create new block device for:%s\n",dev_name
);
3900 bdrv_set_type_hint(bs_table
[i
], BDRV_TYPE_CDROM
);
3903 if (bdrv_open(bs_table
[i
], buf
, snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
3904 fprintf(stderr
, "qemu: could not open hard disk image: '%s'\n",
3908 if (i
== 0 && cyls
!= 0) {
3909 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
3910 bdrv_set_translation_hint(bs_table
[i
], translation
);
3912 ide_disk_options
[j
][0] = '\0';
3914 if (i
== cdrom_index
) {
3917 break; /* finished with this IDE device*/
3921 if (cdrom_index
>= 0 && (!ide_cdrom_created
)) {
3922 bs_table
[cdrom_index
] = bdrv_new("cdrom");
3923 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
3926 for(i
= 0; i
< num_scsi_disks
; i
++) {
3928 temp_adapter
= SCSI_LSI_53C895A
;
3931 /*Check for sdx= parameter */
3932 if (get_param_value(buf
, sizeof(buf
), "sdx", scsi_disk_options
[i
])) {
3933 if (buf
[0] >= 'a' && buf
[0] <= 'g') {
3934 scsi_index
= buf
[0] - 'a';
3936 fprintf(stderr
, "qemu: sdx= option for SCSI must be one letter from a-g. %s \n",buf
);
3943 /* Check for SCSI id specified. */
3944 if (get_param_value(buf
, sizeof(buf
),"id",scsi_disk_options
[i
])) {
3945 id
= strtol(buf
, NULL
, 0);
3946 if (id
< 0 || id
> 6) {
3947 fprintf(stderr
, "qemu: SCSI id must be from 0-6: %d\n", id
);
3950 /* Check if id already used */
3951 for(j
= 0; j
< MAX_SCSI_DISKS
; j
++) {
3952 if (scsi_disks_info
[j
].device_type
!= SCSI_NONE
&&
3954 scsi_disks_info
[j
].adapter
== temp_adapter
&&
3955 scsi_disks_info
[j
].id
== id
) {
3956 fprintf(stderr
, "qemu: SCSI id already used: %u\n", id
);
3963 scsi_disks_info
[i
].adapter
= temp_adapter
;
3964 scsi_disks_info
[i
].id
= id
;
3966 if (get_param_value(buf
, sizeof(buf
),"type",scsi_disk_options
[i
])) {
3967 if (!strcmp(buf
, "disk")) {
3969 } else if (!strcmp(buf
, "cdrom")) {
3972 fprintf(stderr
, "qemu: invalid SCSI disk type= value: %s\n", buf
);
3980 snprintf(dev_name
, sizeof(buf
), "cdrom%c", scsi_index
+ '0');
3981 scsi_disks_info
[scsi_index
].device_type
= SCSI_CDROM
;
3983 snprintf(dev_name
, sizeof(buf
), "sd%c", scsi_index
+ 'a');
3984 scsi_disks_info
[scsi_index
].device_type
= SCSI_DISK
;
3987 if (!(bs_scsi_table
[scsi_index
] = bdrv_new(dev_name
))) {
3988 fprintf(stderr
, "qemu: unable to create new block device for:%s\n",dev_name
);
3992 /* Get image filename from options and then try to open it */
3993 if (get_param_value(buf
, sizeof(buf
),"img",scsi_disk_options
[i
])) {
3994 if (bdrv_open(bs_scsi_table
[scsi_index
], buf
, 0) < 0) {
3995 fprintf(stderr
, "qemu: could not open SCSI disk image img='%s'\n",buf
);
3999 fprintf(stderr
, "qemu: SCSI disk image not specified for sd%c \n", i
+ 'a');
4008 /***********************************************************/
4011 static USBPort
*used_usb_ports
;
4012 static USBPort
*free_usb_ports
;
4014 /* ??? Maybe change this to register a hub to keep track of the topology. */
4015 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
4016 usb_attachfn attach
)
4018 port
->opaque
= opaque
;
4019 port
->index
= index
;
4020 port
->attach
= attach
;
4021 port
->next
= free_usb_ports
;
4022 free_usb_ports
= port
;
4025 static int usb_device_add(const char *devname
)
4031 if (!free_usb_ports
)
4034 if (strstart(devname
, "host:", &p
)) {
4035 dev
= usb_host_device_open(p
);
4036 } else if (!strcmp(devname
, "mouse")) {
4037 dev
= usb_mouse_init();
4038 } else if (!strcmp(devname
, "tablet")) {
4039 dev
= usb_tablet_init();
4040 } else if (strstart(devname
, "disk:", &p
)) {
4041 dev
= usb_msd_init(p
);
4048 /* Find a USB port to add the device to. */
4049 port
= free_usb_ports
;
4053 /* Create a new hub and chain it on. */
4054 free_usb_ports
= NULL
;
4055 port
->next
= used_usb_ports
;
4056 used_usb_ports
= port
;
4058 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
4059 usb_attach(port
, hub
);
4060 port
= free_usb_ports
;
4063 free_usb_ports
= port
->next
;
4064 port
->next
= used_usb_ports
;
4065 used_usb_ports
= port
;
4066 usb_attach(port
, dev
);
4070 static int usb_device_del(const char *devname
)
4078 if (!used_usb_ports
)
4081 p
= strchr(devname
, '.');
4084 bus_num
= strtoul(devname
, NULL
, 0);
4085 addr
= strtoul(p
+ 1, NULL
, 0);
4089 lastp
= &used_usb_ports
;
4090 port
= used_usb_ports
;
4091 while (port
&& port
->dev
->addr
!= addr
) {
4092 lastp
= &port
->next
;
4100 *lastp
= port
->next
;
4101 usb_attach(port
, NULL
);
4102 dev
->handle_destroy(dev
);
4103 port
->next
= free_usb_ports
;
4104 free_usb_ports
= port
;
4108 void do_usb_add(const char *devname
)
4111 ret
= usb_device_add(devname
);
4113 term_printf("Could not add USB device '%s'\n", devname
);
4116 void do_usb_del(const char *devname
)
4119 ret
= usb_device_del(devname
);
4121 term_printf("Could not remove USB device '%s'\n", devname
);
4128 const char *speed_str
;
4131 term_printf("USB support not enabled\n");
4135 for (port
= used_usb_ports
; port
; port
= port
->next
) {
4139 switch(dev
->speed
) {
4143 case USB_SPEED_FULL
:
4146 case USB_SPEED_HIGH
:
4153 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
4154 0, dev
->addr
, speed_str
, dev
->devname
);
4158 /***********************************************************/
4161 static char *pid_filename
;
4163 /* Remove PID file. Called on normal exit */
4165 static void remove_pidfile(void)
4167 unlink (pid_filename
);
4170 static void create_pidfile(const char *filename
)
4172 struct stat pidstat
;
4175 /* Try to write our PID to the named file */
4176 if (stat(filename
, &pidstat
) < 0) {
4177 if (errno
== ENOENT
) {
4178 if ((f
= fopen (filename
, "w")) == NULL
) {
4179 perror("Opening pidfile");
4182 fprintf(f
, "%d\n", getpid());
4184 pid_filename
= qemu_strdup(filename
);
4185 if (!pid_filename
) {
4186 fprintf(stderr
, "Could not save PID filename");
4189 atexit(remove_pidfile
);
4192 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
4198 /***********************************************************/
4201 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4205 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4209 static void dumb_refresh(DisplayState
*ds
)
4214 void dumb_display_init(DisplayState
*ds
)
4219 ds
->dpy_update
= dumb_update
;
4220 ds
->dpy_resize
= dumb_resize
;
4221 ds
->dpy_refresh
= dumb_refresh
;
4224 /***********************************************************/
4227 #define MAX_IO_HANDLERS 64
4229 typedef struct IOHandlerRecord
{
4231 IOCanRWHandler
*fd_read_poll
;
4233 IOHandler
*fd_write
;
4235 /* temporary data */
4237 struct IOHandlerRecord
*next
;
4240 static IOHandlerRecord
*first_io_handler
;
4242 /* XXX: fd_read_poll should be suppressed, but an API change is
4243 necessary in the character devices to suppress fd_can_read(). */
4244 int qemu_set_fd_handler2(int fd
,
4245 IOCanRWHandler
*fd_read_poll
,
4247 IOHandler
*fd_write
,
4250 IOHandlerRecord
**pioh
, *ioh
;
4252 if (!fd_read
&& !fd_write
) {
4253 pioh
= &first_io_handler
;
4258 if (ioh
->fd
== fd
) {
4266 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4270 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4273 ioh
->next
= first_io_handler
;
4274 first_io_handler
= ioh
;
4277 ioh
->fd_read_poll
= fd_read_poll
;
4278 ioh
->fd_read
= fd_read
;
4279 ioh
->fd_write
= fd_write
;
4280 ioh
->opaque
= opaque
;
4285 int qemu_set_fd_handler(int fd
,
4287 IOHandler
*fd_write
,
4290 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4293 /***********************************************************/
4294 /* Polling handling */
4296 typedef struct PollingEntry
{
4299 struct PollingEntry
*next
;
4302 static PollingEntry
*first_polling_entry
;
4304 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4306 PollingEntry
**ppe
, *pe
;
4307 pe
= qemu_mallocz(sizeof(PollingEntry
));
4311 pe
->opaque
= opaque
;
4312 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
4317 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
4319 PollingEntry
**ppe
, *pe
;
4320 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
4322 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
4331 /***********************************************************/
4332 /* Wait objects support */
4333 typedef struct WaitObjects
{
4335 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
4336 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
4337 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
4340 static WaitObjects wait_objects
= {0};
4342 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4344 WaitObjects
*w
= &wait_objects
;
4346 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
4348 w
->events
[w
->num
] = handle
;
4349 w
->func
[w
->num
] = func
;
4350 w
->opaque
[w
->num
] = opaque
;
4355 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4358 WaitObjects
*w
= &wait_objects
;
4361 for (i
= 0; i
< w
->num
; i
++) {
4362 if (w
->events
[i
] == handle
)
4365 w
->events
[i
] = w
->events
[i
+ 1];
4366 w
->func
[i
] = w
->func
[i
+ 1];
4367 w
->opaque
[i
] = w
->opaque
[i
+ 1];
4375 /***********************************************************/
4376 /* savevm/loadvm support */
4378 #define IO_BUF_SIZE 32768
4382 BlockDriverState
*bs
;
4385 int64_t base_offset
;
4386 int64_t buf_offset
; /* start of buffer when writing, end of buffer
4389 int buf_size
; /* 0 when writing */
4390 uint8_t buf
[IO_BUF_SIZE
];
4393 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
4397 f
= qemu_mallocz(sizeof(QEMUFile
));
4400 if (!strcmp(mode
, "wb")) {
4402 } else if (!strcmp(mode
, "rb")) {
4407 f
->outfile
= fopen(filename
, mode
);
4419 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
4423 f
= qemu_mallocz(sizeof(QEMUFile
));
4428 f
->is_writable
= is_writable
;
4429 f
->base_offset
= offset
;
4433 void qemu_fflush(QEMUFile
*f
)
4435 if (!f
->is_writable
)
4437 if (f
->buf_index
> 0) {
4439 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4440 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
4442 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4443 f
->buf
, f
->buf_index
);
4445 f
->buf_offset
+= f
->buf_index
;
4450 static void qemu_fill_buffer(QEMUFile
*f
)
4457 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4458 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
4462 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4463 f
->buf
, IO_BUF_SIZE
);
4469 f
->buf_offset
+= len
;
4472 void qemu_fclose(QEMUFile
*f
)
4482 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
4486 l
= IO_BUF_SIZE
- f
->buf_index
;
4489 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
4493 if (f
->buf_index
>= IO_BUF_SIZE
)
4498 void qemu_put_byte(QEMUFile
*f
, int v
)
4500 f
->buf
[f
->buf_index
++] = v
;
4501 if (f
->buf_index
>= IO_BUF_SIZE
)
4505 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
4511 l
= f
->buf_size
- f
->buf_index
;
4513 qemu_fill_buffer(f
);
4514 l
= f
->buf_size
- f
->buf_index
;
4520 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
4525 return size1
- size
;
4528 int qemu_get_byte(QEMUFile
*f
)
4530 if (f
->buf_index
>= f
->buf_size
) {
4531 qemu_fill_buffer(f
);
4532 if (f
->buf_index
>= f
->buf_size
)
4535 return f
->buf
[f
->buf_index
++];
4538 int64_t qemu_ftell(QEMUFile
*f
)
4540 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
4543 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
4545 if (whence
== SEEK_SET
) {
4547 } else if (whence
== SEEK_CUR
) {
4548 pos
+= qemu_ftell(f
);
4550 /* SEEK_END not supported */
4553 if (f
->is_writable
) {
4555 f
->buf_offset
= pos
;
4557 f
->buf_offset
= pos
;
4564 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
4566 qemu_put_byte(f
, v
>> 8);
4567 qemu_put_byte(f
, v
);
4570 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
4572 qemu_put_byte(f
, v
>> 24);
4573 qemu_put_byte(f
, v
>> 16);
4574 qemu_put_byte(f
, v
>> 8);
4575 qemu_put_byte(f
, v
);
4578 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
4580 qemu_put_be32(f
, v
>> 32);
4581 qemu_put_be32(f
, v
);
4584 unsigned int qemu_get_be16(QEMUFile
*f
)
4587 v
= qemu_get_byte(f
) << 8;
4588 v
|= qemu_get_byte(f
);
4592 unsigned int qemu_get_be32(QEMUFile
*f
)
4595 v
= qemu_get_byte(f
) << 24;
4596 v
|= qemu_get_byte(f
) << 16;
4597 v
|= qemu_get_byte(f
) << 8;
4598 v
|= qemu_get_byte(f
);
4602 uint64_t qemu_get_be64(QEMUFile
*f
)
4605 v
= (uint64_t)qemu_get_be32(f
) << 32;
4606 v
|= qemu_get_be32(f
);
4610 typedef struct SaveStateEntry
{
4614 SaveStateHandler
*save_state
;
4615 LoadStateHandler
*load_state
;
4617 struct SaveStateEntry
*next
;
4620 static SaveStateEntry
*first_se
;
4622 int register_savevm(const char *idstr
,
4625 SaveStateHandler
*save_state
,
4626 LoadStateHandler
*load_state
,
4629 SaveStateEntry
*se
, **pse
;
4631 se
= qemu_malloc(sizeof(SaveStateEntry
));
4634 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
4635 se
->instance_id
= instance_id
;
4636 se
->version_id
= version_id
;
4637 se
->save_state
= save_state
;
4638 se
->load_state
= load_state
;
4639 se
->opaque
= opaque
;
4642 /* add at the end of list */
4644 while (*pse
!= NULL
)
4645 pse
= &(*pse
)->next
;
4650 #define QEMU_VM_FILE_MAGIC 0x5145564d
4651 #define QEMU_VM_FILE_VERSION 0x00000002
4653 int qemu_savevm_state(QEMUFile
*f
)
4657 int64_t cur_pos
, len_pos
, total_len_pos
;
4659 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
4660 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
4661 total_len_pos
= qemu_ftell(f
);
4662 qemu_put_be64(f
, 0); /* total size */
4664 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4666 len
= strlen(se
->idstr
);
4667 qemu_put_byte(f
, len
);
4668 qemu_put_buffer(f
, se
->idstr
, len
);
4670 qemu_put_be32(f
, se
->instance_id
);
4671 qemu_put_be32(f
, se
->version_id
);
4673 /* record size: filled later */
4674 len_pos
= qemu_ftell(f
);
4675 qemu_put_be32(f
, 0);
4677 se
->save_state(f
, se
->opaque
);
4679 /* fill record size */
4680 cur_pos
= qemu_ftell(f
);
4681 len
= cur_pos
- len_pos
- 4;
4682 qemu_fseek(f
, len_pos
, SEEK_SET
);
4683 qemu_put_be32(f
, len
);
4684 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4686 cur_pos
= qemu_ftell(f
);
4687 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
4688 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
4689 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4695 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
4699 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4700 if (!strcmp(se
->idstr
, idstr
) &&
4701 instance_id
== se
->instance_id
)
4707 int qemu_loadvm_state(QEMUFile
*f
)
4710 int len
, ret
, instance_id
, record_len
, version_id
;
4711 int64_t total_len
, end_pos
, cur_pos
;
4715 v
= qemu_get_be32(f
);
4716 if (v
!= QEMU_VM_FILE_MAGIC
)
4718 v
= qemu_get_be32(f
);
4719 if (v
!= QEMU_VM_FILE_VERSION
) {
4724 total_len
= qemu_get_be64(f
);
4725 end_pos
= total_len
+ qemu_ftell(f
);
4727 if (qemu_ftell(f
) >= end_pos
)
4729 len
= qemu_get_byte(f
);
4730 qemu_get_buffer(f
, idstr
, len
);
4732 instance_id
= qemu_get_be32(f
);
4733 version_id
= qemu_get_be32(f
);
4734 record_len
= qemu_get_be32(f
);
4736 printf("idstr=%s instance=0x%x version=%d len=%d\n",
4737 idstr
, instance_id
, version_id
, record_len
);
4739 cur_pos
= qemu_ftell(f
);
4740 se
= find_se(idstr
, instance_id
);
4742 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
4743 instance_id
, idstr
);
4745 ret
= se
->load_state(f
, se
->opaque
, version_id
);
4747 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
4748 instance_id
, idstr
);
4751 /* always seek to exact end of record */
4752 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
4759 /* device can contain snapshots */
4760 static int bdrv_can_snapshot(BlockDriverState
*bs
)
4763 !bdrv_is_removable(bs
) &&
4764 !bdrv_is_read_only(bs
));
4767 /* device must be snapshots in order to have a reliable snapshot */
4768 static int bdrv_has_snapshot(BlockDriverState
*bs
)
4771 !bdrv_is_removable(bs
) &&
4772 !bdrv_is_read_only(bs
));
4775 static BlockDriverState
*get_bs_snapshots(void)
4777 BlockDriverState
*bs
;
4781 return bs_snapshots
;
4782 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4784 if (bdrv_can_snapshot(bs
))
4793 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
4796 QEMUSnapshotInfo
*sn_tab
, *sn
;
4800 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
4803 for(i
= 0; i
< nb_sns
; i
++) {
4805 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
4815 void do_savevm(const char *name
)
4817 BlockDriverState
*bs
, *bs1
;
4818 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
4819 int must_delete
, ret
, i
;
4820 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
4822 int saved_vm_running
;
4829 bs
= get_bs_snapshots();
4831 term_printf("No block device can accept snapshots\n");
4835 /* ??? Should this occur after vm_stop? */
4838 saved_vm_running
= vm_running
;
4843 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
4848 memset(sn
, 0, sizeof(*sn
));
4850 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
4851 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
4854 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
4857 /* fill auxiliary fields */
4860 sn
->date_sec
= tb
.time
;
4861 sn
->date_nsec
= tb
.millitm
* 1000000;
4863 gettimeofday(&tv
, NULL
);
4864 sn
->date_sec
= tv
.tv_sec
;
4865 sn
->date_nsec
= tv
.tv_usec
* 1000;
4867 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
4869 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
4870 term_printf("Device %s does not support VM state snapshots\n",
4871 bdrv_get_device_name(bs
));
4875 /* save the VM state */
4876 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
4878 term_printf("Could not open VM state file\n");
4881 ret
= qemu_savevm_state(f
);
4882 sn
->vm_state_size
= qemu_ftell(f
);
4885 term_printf("Error %d while writing VM\n", ret
);
4889 /* create the snapshots */
4891 for(i
= 0; i
< MAX_DISKS
; i
++) {
4893 if (bdrv_has_snapshot(bs1
)) {
4895 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
4897 term_printf("Error while deleting snapshot on '%s'\n",
4898 bdrv_get_device_name(bs1
));
4901 ret
= bdrv_snapshot_create(bs1
, sn
);
4903 term_printf("Error while creating snapshot on '%s'\n",
4904 bdrv_get_device_name(bs1
));
4910 if (saved_vm_running
)
4914 void do_loadvm(const char *name
)
4916 BlockDriverState
*bs
, *bs1
;
4917 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
4920 int saved_vm_running
;
4922 bs
= get_bs_snapshots();
4924 term_printf("No block device supports snapshots\n");
4928 /* Flush all IO requests so they don't interfere with the new state. */
4931 saved_vm_running
= vm_running
;
4934 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4936 if (bdrv_has_snapshot(bs1
)) {
4937 ret
= bdrv_snapshot_goto(bs1
, name
);
4940 term_printf("Warning: ");
4943 term_printf("Snapshots not supported on device '%s'\n",
4944 bdrv_get_device_name(bs1
));
4947 term_printf("Could not find snapshot '%s' on device '%s'\n",
4948 name
, bdrv_get_device_name(bs1
));
4951 term_printf("Error %d while activating snapshot on '%s'\n",
4952 ret
, bdrv_get_device_name(bs1
));
4955 /* fatal on snapshot block device */
4962 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
4963 term_printf("Device %s does not support VM state snapshots\n",
4964 bdrv_get_device_name(bs
));
4968 /* restore the VM state */
4969 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
4971 term_printf("Could not open VM state file\n");
4974 ret
= qemu_loadvm_state(f
);
4977 term_printf("Error %d while loading VM state\n", ret
);
4980 if (saved_vm_running
)
4984 void do_delvm(const char *name
)
4986 BlockDriverState
*bs
, *bs1
;
4989 bs
= get_bs_snapshots();
4991 term_printf("No block device supports snapshots\n");
4995 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4997 if (bdrv_has_snapshot(bs1
)) {
4998 ret
= bdrv_snapshot_delete(bs1
, name
);
5000 if (ret
== -ENOTSUP
)
5001 term_printf("Snapshots not supported on device '%s'\n",
5002 bdrv_get_device_name(bs1
));
5004 term_printf("Error %d while deleting snapshot on '%s'\n",
5005 ret
, bdrv_get_device_name(bs1
));
5011 void do_info_snapshots(void)
5013 BlockDriverState
*bs
, *bs1
;
5014 QEMUSnapshotInfo
*sn_tab
, *sn
;
5018 bs
= get_bs_snapshots();
5020 term_printf("No available block device supports snapshots\n");
5023 term_printf("Snapshot devices:");
5024 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5026 if (bdrv_has_snapshot(bs1
)) {
5028 term_printf(" %s", bdrv_get_device_name(bs1
));
5033 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5035 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
5038 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
5039 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
5040 for(i
= 0; i
< nb_sns
; i
++) {
5042 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
5047 /***********************************************************/
5048 /* cpu save/restore */
5050 #if defined(TARGET_I386)
5052 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
5054 qemu_put_be32(f
, dt
->selector
);
5055 qemu_put_betl(f
, dt
->base
);
5056 qemu_put_be32(f
, dt
->limit
);
5057 qemu_put_be32(f
, dt
->flags
);
5060 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
5062 dt
->selector
= qemu_get_be32(f
);
5063 dt
->base
= qemu_get_betl(f
);
5064 dt
->limit
= qemu_get_be32(f
);
5065 dt
->flags
= qemu_get_be32(f
);
5068 void cpu_save(QEMUFile
*f
, void *opaque
)
5070 CPUState
*env
= opaque
;
5071 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
5075 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5076 qemu_put_betls(f
, &env
->regs
[i
]);
5077 qemu_put_betls(f
, &env
->eip
);
5078 qemu_put_betls(f
, &env
->eflags
);
5079 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
5080 qemu_put_be32s(f
, &hflags
);
5084 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
5086 for(i
= 0; i
< 8; i
++) {
5087 fptag
|= ((!env
->fptags
[i
]) << i
);
5090 qemu_put_be16s(f
, &fpuc
);
5091 qemu_put_be16s(f
, &fpus
);
5092 qemu_put_be16s(f
, &fptag
);
5094 #ifdef USE_X86LDOUBLE
5099 qemu_put_be16s(f
, &fpregs_format
);
5101 for(i
= 0; i
< 8; i
++) {
5102 #ifdef USE_X86LDOUBLE
5106 /* we save the real CPU data (in case of MMX usage only 'mant'
5107 contains the MMX register */
5108 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
5109 qemu_put_be64(f
, mant
);
5110 qemu_put_be16(f
, exp
);
5113 /* if we use doubles for float emulation, we save the doubles to
5114 avoid losing information in case of MMX usage. It can give
5115 problems if the image is restored on a CPU where long
5116 doubles are used instead. */
5117 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
5121 for(i
= 0; i
< 6; i
++)
5122 cpu_put_seg(f
, &env
->segs
[i
]);
5123 cpu_put_seg(f
, &env
->ldt
);
5124 cpu_put_seg(f
, &env
->tr
);
5125 cpu_put_seg(f
, &env
->gdt
);
5126 cpu_put_seg(f
, &env
->idt
);
5128 qemu_put_be32s(f
, &env
->sysenter_cs
);
5129 qemu_put_be32s(f
, &env
->sysenter_esp
);
5130 qemu_put_be32s(f
, &env
->sysenter_eip
);
5132 qemu_put_betls(f
, &env
->cr
[0]);
5133 qemu_put_betls(f
, &env
->cr
[2]);
5134 qemu_put_betls(f
, &env
->cr
[3]);
5135 qemu_put_betls(f
, &env
->cr
[4]);
5137 for(i
= 0; i
< 8; i
++)
5138 qemu_put_betls(f
, &env
->dr
[i
]);
5141 qemu_put_be32s(f
, &env
->a20_mask
);
5144 qemu_put_be32s(f
, &env
->mxcsr
);
5145 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5146 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5147 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5150 #ifdef TARGET_X86_64
5151 qemu_put_be64s(f
, &env
->efer
);
5152 qemu_put_be64s(f
, &env
->star
);
5153 qemu_put_be64s(f
, &env
->lstar
);
5154 qemu_put_be64s(f
, &env
->cstar
);
5155 qemu_put_be64s(f
, &env
->fmask
);
5156 qemu_put_be64s(f
, &env
->kernelgsbase
);
5158 qemu_put_be32s(f
, &env
->smbase
);
5161 #ifdef USE_X86LDOUBLE
5162 /* XXX: add that in a FPU generic layer */
5163 union x86_longdouble
{
5168 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
5169 #define EXPBIAS1 1023
5170 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
5171 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
5173 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
5177 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
5178 /* exponent + sign */
5179 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
5180 e
|= SIGND1(temp
) >> 16;
5185 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5187 CPUState
*env
= opaque
;
5190 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
5192 if (version_id
!= 3 && version_id
!= 4)
5194 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5195 qemu_get_betls(f
, &env
->regs
[i
]);
5196 qemu_get_betls(f
, &env
->eip
);
5197 qemu_get_betls(f
, &env
->eflags
);
5198 qemu_get_be32s(f
, &hflags
);
5200 qemu_get_be16s(f
, &fpuc
);
5201 qemu_get_be16s(f
, &fpus
);
5202 qemu_get_be16s(f
, &fptag
);
5203 qemu_get_be16s(f
, &fpregs_format
);
5205 /* NOTE: we cannot always restore the FPU state if the image come
5206 from a host with a different 'USE_X86LDOUBLE' define. We guess
5207 if we are in an MMX state to restore correctly in that case. */
5208 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
5209 for(i
= 0; i
< 8; i
++) {
5213 switch(fpregs_format
) {
5215 mant
= qemu_get_be64(f
);
5216 exp
= qemu_get_be16(f
);
5217 #ifdef USE_X86LDOUBLE
5218 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5220 /* difficult case */
5222 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5224 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5228 mant
= qemu_get_be64(f
);
5229 #ifdef USE_X86LDOUBLE
5231 union x86_longdouble
*p
;
5232 /* difficult case */
5233 p
= (void *)&env
->fpregs
[i
];
5238 fp64_to_fp80(p
, mant
);
5242 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5251 /* XXX: restore FPU round state */
5252 env
->fpstt
= (fpus
>> 11) & 7;
5253 env
->fpus
= fpus
& ~0x3800;
5255 for(i
= 0; i
< 8; i
++) {
5256 env
->fptags
[i
] = (fptag
>> i
) & 1;
5259 for(i
= 0; i
< 6; i
++)
5260 cpu_get_seg(f
, &env
->segs
[i
]);
5261 cpu_get_seg(f
, &env
->ldt
);
5262 cpu_get_seg(f
, &env
->tr
);
5263 cpu_get_seg(f
, &env
->gdt
);
5264 cpu_get_seg(f
, &env
->idt
);
5266 qemu_get_be32s(f
, &env
->sysenter_cs
);
5267 qemu_get_be32s(f
, &env
->sysenter_esp
);
5268 qemu_get_be32s(f
, &env
->sysenter_eip
);
5270 qemu_get_betls(f
, &env
->cr
[0]);
5271 qemu_get_betls(f
, &env
->cr
[2]);
5272 qemu_get_betls(f
, &env
->cr
[3]);
5273 qemu_get_betls(f
, &env
->cr
[4]);
5275 for(i
= 0; i
< 8; i
++)
5276 qemu_get_betls(f
, &env
->dr
[i
]);
5279 qemu_get_be32s(f
, &env
->a20_mask
);
5281 qemu_get_be32s(f
, &env
->mxcsr
);
5282 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5283 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5284 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5287 #ifdef TARGET_X86_64
5288 qemu_get_be64s(f
, &env
->efer
);
5289 qemu_get_be64s(f
, &env
->star
);
5290 qemu_get_be64s(f
, &env
->lstar
);
5291 qemu_get_be64s(f
, &env
->cstar
);
5292 qemu_get_be64s(f
, &env
->fmask
);
5293 qemu_get_be64s(f
, &env
->kernelgsbase
);
5295 if (version_id
>= 4)
5296 qemu_get_be32s(f
, &env
->smbase
);
5298 /* XXX: compute hflags from scratch, except for CPL and IIF */
5299 env
->hflags
= hflags
;
5304 #elif defined(TARGET_PPC)
5305 void cpu_save(QEMUFile
*f
, void *opaque
)
5309 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5314 #elif defined(TARGET_MIPS)
5315 void cpu_save(QEMUFile
*f
, void *opaque
)
5319 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5324 #elif defined(TARGET_SPARC)
5325 void cpu_save(QEMUFile
*f
, void *opaque
)
5327 CPUState
*env
= opaque
;
5331 for(i
= 0; i
< 8; i
++)
5332 qemu_put_betls(f
, &env
->gregs
[i
]);
5333 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5334 qemu_put_betls(f
, &env
->regbase
[i
]);
5337 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5343 qemu_put_be32(f
, u
.i
);
5346 qemu_put_betls(f
, &env
->pc
);
5347 qemu_put_betls(f
, &env
->npc
);
5348 qemu_put_betls(f
, &env
->y
);
5350 qemu_put_be32(f
, tmp
);
5351 qemu_put_betls(f
, &env
->fsr
);
5352 qemu_put_betls(f
, &env
->tbr
);
5353 #ifndef TARGET_SPARC64
5354 qemu_put_be32s(f
, &env
->wim
);
5356 for(i
= 0; i
< 16; i
++)
5357 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
5361 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5363 CPUState
*env
= opaque
;
5367 for(i
= 0; i
< 8; i
++)
5368 qemu_get_betls(f
, &env
->gregs
[i
]);
5369 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5370 qemu_get_betls(f
, &env
->regbase
[i
]);
5373 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5378 u
.i
= qemu_get_be32(f
);
5382 qemu_get_betls(f
, &env
->pc
);
5383 qemu_get_betls(f
, &env
->npc
);
5384 qemu_get_betls(f
, &env
->y
);
5385 tmp
= qemu_get_be32(f
);
5386 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
5387 correctly updated */
5389 qemu_get_betls(f
, &env
->fsr
);
5390 qemu_get_betls(f
, &env
->tbr
);
5391 #ifndef TARGET_SPARC64
5392 qemu_get_be32s(f
, &env
->wim
);
5394 for(i
= 0; i
< 16; i
++)
5395 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
5401 #elif defined(TARGET_ARM)
5403 /* ??? Need to implement these. */
5404 void cpu_save(QEMUFile
*f
, void *opaque
)
5408 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5415 #warning No CPU save/restore functions
5419 /***********************************************************/
5420 /* ram save/restore */
5422 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
5426 v
= qemu_get_byte(f
);
5429 if (qemu_get_buffer(f
, buf
, len
) != len
)
5433 v
= qemu_get_byte(f
);
5434 memset(buf
, v
, len
);
5442 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
5446 if (qemu_get_be32(f
) != phys_ram_size
)
5448 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
5449 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
5456 #define BDRV_HASH_BLOCK_SIZE 1024
5457 #define IOBUF_SIZE 4096
5458 #define RAM_CBLOCK_MAGIC 0xfabe
5460 typedef struct RamCompressState
{
5463 uint8_t buf
[IOBUF_SIZE
];
5466 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
5469 memset(s
, 0, sizeof(*s
));
5471 ret
= deflateInit2(&s
->zstream
, 1,
5473 9, Z_DEFAULT_STRATEGY
);
5476 s
->zstream
.avail_out
= IOBUF_SIZE
;
5477 s
->zstream
.next_out
= s
->buf
;
5481 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
5483 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
5484 qemu_put_be16(s
->f
, len
);
5485 qemu_put_buffer(s
->f
, buf
, len
);
5488 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
5492 s
->zstream
.avail_in
= len
;
5493 s
->zstream
.next_in
= (uint8_t *)buf
;
5494 while (s
->zstream
.avail_in
> 0) {
5495 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
5498 if (s
->zstream
.avail_out
== 0) {
5499 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
5500 s
->zstream
.avail_out
= IOBUF_SIZE
;
5501 s
->zstream
.next_out
= s
->buf
;
5507 static void ram_compress_close(RamCompressState
*s
)
5511 /* compress last bytes */
5513 ret
= deflate(&s
->zstream
, Z_FINISH
);
5514 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
5515 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
5517 ram_put_cblock(s
, s
->buf
, len
);
5519 s
->zstream
.avail_out
= IOBUF_SIZE
;
5520 s
->zstream
.next_out
= s
->buf
;
5521 if (ret
== Z_STREAM_END
)
5528 deflateEnd(&s
->zstream
);
5531 typedef struct RamDecompressState
{
5534 uint8_t buf
[IOBUF_SIZE
];
5535 } RamDecompressState
;
5537 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
5540 memset(s
, 0, sizeof(*s
));
5542 ret
= inflateInit(&s
->zstream
);
5548 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
5552 s
->zstream
.avail_out
= len
;
5553 s
->zstream
.next_out
= buf
;
5554 while (s
->zstream
.avail_out
> 0) {
5555 if (s
->zstream
.avail_in
== 0) {
5556 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
5558 clen
= qemu_get_be16(s
->f
);
5559 if (clen
> IOBUF_SIZE
)
5561 qemu_get_buffer(s
->f
, s
->buf
, clen
);
5562 s
->zstream
.avail_in
= clen
;
5563 s
->zstream
.next_in
= s
->buf
;
5565 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
5566 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
5573 static void ram_decompress_close(RamDecompressState
*s
)
5575 inflateEnd(&s
->zstream
);
5578 static void ram_save(QEMUFile
*f
, void *opaque
)
5581 RamCompressState s1
, *s
= &s1
;
5584 qemu_put_be32(f
, phys_ram_size
);
5585 if (ram_compress_open(s
, f
) < 0)
5587 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5589 if (tight_savevm_enabled
) {
5593 /* find if the memory block is available on a virtual
5596 for(j
= 0; j
< MAX_DISKS
; j
++) {
5598 sector_num
= bdrv_hash_find(bs_table
[j
],
5599 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5600 if (sector_num
>= 0)
5605 goto normal_compress
;
5608 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
5609 ram_compress_buf(s
, buf
, 10);
5615 ram_compress_buf(s
, buf
, 1);
5616 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5619 ram_compress_close(s
);
5622 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
5624 RamDecompressState s1
, *s
= &s1
;
5628 if (version_id
== 1)
5629 return ram_load_v1(f
, opaque
);
5630 if (version_id
!= 2)
5632 if (qemu_get_be32(f
) != phys_ram_size
)
5634 if (ram_decompress_open(s
, f
) < 0)
5636 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5637 if (ram_decompress_buf(s
, buf
, 1) < 0) {
5638 fprintf(stderr
, "Error while reading ram block header\n");
5642 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
5643 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
5652 ram_decompress_buf(s
, buf
+ 1, 9);
5654 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
5655 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
5656 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
5659 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
5660 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
5661 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
5662 bs_index
, sector_num
);
5669 printf("Error block header\n");
5673 ram_decompress_close(s
);
5677 /***********************************************************/
5678 /* bottom halves (can be seen as timers which expire ASAP) */
5687 static QEMUBH
*first_bh
= NULL
;
5689 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
5692 bh
= qemu_mallocz(sizeof(QEMUBH
));
5696 bh
->opaque
= opaque
;
5700 int qemu_bh_poll(void)
5719 void qemu_bh_schedule(QEMUBH
*bh
)
5721 CPUState
*env
= cpu_single_env
;
5725 bh
->next
= first_bh
;
5728 /* stop the currently executing CPU to execute the BH ASAP */
5730 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
5734 void qemu_bh_cancel(QEMUBH
*bh
)
5737 if (bh
->scheduled
) {
5740 pbh
= &(*pbh
)->next
;
5746 void qemu_bh_delete(QEMUBH
*bh
)
5752 /***********************************************************/
5753 /* machine registration */
5755 QEMUMachine
*first_machine
= NULL
;
5757 int qemu_register_machine(QEMUMachine
*m
)
5760 pm
= &first_machine
;
5768 QEMUMachine
*find_machine(const char *name
)
5772 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5773 if (!strcmp(m
->name
, name
))
5779 /***********************************************************/
5780 /* main execution loop */
5782 void gui_update(void *opaque
)
5784 display_state
.dpy_refresh(&display_state
);
5785 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
5788 struct vm_change_state_entry
{
5789 VMChangeStateHandler
*cb
;
5791 LIST_ENTRY (vm_change_state_entry
) entries
;
5794 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
5796 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
5799 VMChangeStateEntry
*e
;
5801 e
= qemu_mallocz(sizeof (*e
));
5807 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
5811 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
5813 LIST_REMOVE (e
, entries
);
5817 static void vm_state_notify(int running
)
5819 VMChangeStateEntry
*e
;
5821 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
5822 e
->cb(e
->opaque
, running
);
5826 /* XXX: support several handlers */
5827 static VMStopHandler
*vm_stop_cb
;
5828 static void *vm_stop_opaque
;
5830 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
5833 vm_stop_opaque
= opaque
;
5837 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
5851 void vm_stop(int reason
)
5854 cpu_disable_ticks();
5858 vm_stop_cb(vm_stop_opaque
, reason
);
5865 /* reset/shutdown handler */
5867 typedef struct QEMUResetEntry
{
5868 QEMUResetHandler
*func
;
5870 struct QEMUResetEntry
*next
;
5873 static QEMUResetEntry
*first_reset_entry
;
5874 static int reset_requested
;
5875 static int shutdown_requested
;
5876 static int powerdown_requested
;
5878 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
5880 QEMUResetEntry
**pre
, *re
;
5882 pre
= &first_reset_entry
;
5883 while (*pre
!= NULL
)
5884 pre
= &(*pre
)->next
;
5885 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
5887 re
->opaque
= opaque
;
5892 static void qemu_system_reset(void)
5896 /* reset all devices */
5897 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
5898 re
->func(re
->opaque
);
5902 void qemu_system_reset_request(void)
5905 shutdown_requested
= 1;
5907 reset_requested
= 1;
5910 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5913 void qemu_system_shutdown_request(void)
5915 shutdown_requested
= 1;
5917 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5920 void qemu_system_powerdown_request(void)
5922 powerdown_requested
= 1;
5924 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5927 void main_loop_wait(int timeout
)
5929 IOHandlerRecord
*ioh
, *ioh_next
;
5930 fd_set rfds
, wfds
, xfds
;
5936 /* XXX: need to suppress polling by better using win32 events */
5938 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
5939 ret
|= pe
->func(pe
->opaque
);
5942 if (ret
== 0 && timeout
> 0) {
5944 WaitObjects
*w
= &wait_objects
;
5946 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
5947 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
5948 if (w
->func
[ret
- WAIT_OBJECT_0
])
5949 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
5950 } else if (ret
== WAIT_TIMEOUT
) {
5952 err
= GetLastError();
5953 fprintf(stderr
, "Wait error %d %d\n", ret
, err
);
5957 /* poll any events */
5958 /* XXX: separate device handlers from system ones */
5963 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
5965 (!ioh
->fd_read_poll
||
5966 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
5967 FD_SET(ioh
->fd
, &rfds
);
5971 if (ioh
->fd_write
) {
5972 FD_SET(ioh
->fd
, &wfds
);
5982 tv
.tv_usec
= timeout
* 1000;
5984 #if defined(CONFIG_SLIRP)
5986 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
5989 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
5991 /* XXX: better handling of removal */
5992 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
5993 ioh_next
= ioh
->next
;
5994 if (FD_ISSET(ioh
->fd
, &rfds
)) {
5995 ioh
->fd_read(ioh
->opaque
);
5997 if (FD_ISSET(ioh
->fd
, &wfds
)) {
5998 ioh
->fd_write(ioh
->opaque
);
6002 #if defined(CONFIG_SLIRP)
6009 slirp_select_poll(&rfds
, &wfds
, &xfds
);
6016 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
6017 qemu_get_clock(vm_clock
));
6018 /* run dma transfers, if any */
6022 /* real time timers */
6023 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
6024 qemu_get_clock(rt_clock
));
6027 static CPUState
*cur_cpu
;
6032 #ifdef CONFIG_PROFILER
6037 cur_cpu
= first_cpu
;
6044 env
= env
->next_cpu
;
6047 #ifdef CONFIG_PROFILER
6048 ti
= profile_getclock();
6050 ret
= cpu_exec(env
);
6051 #ifdef CONFIG_PROFILER
6052 qemu_time
+= profile_getclock() - ti
;
6054 if (ret
!= EXCP_HALTED
)
6056 /* all CPUs are halted ? */
6057 if (env
== cur_cpu
) {
6064 if (shutdown_requested
) {
6065 ret
= EXCP_INTERRUPT
;
6068 if (reset_requested
) {
6069 reset_requested
= 0;
6070 qemu_system_reset();
6071 ret
= EXCP_INTERRUPT
;
6073 if (powerdown_requested
) {
6074 powerdown_requested
= 0;
6075 qemu_system_powerdown();
6076 ret
= EXCP_INTERRUPT
;
6078 if (ret
== EXCP_DEBUG
) {
6079 vm_stop(EXCP_DEBUG
);
6081 /* if hlt instruction, we wait until the next IRQ */
6082 /* XXX: use timeout computed from timers */
6083 if (ret
== EXCP_HLT
)
6090 #ifdef CONFIG_PROFILER
6091 ti
= profile_getclock();
6093 main_loop_wait(timeout
);
6094 #ifdef CONFIG_PROFILER
6095 dev_time
+= profile_getclock() - ti
;
6098 cpu_disable_ticks();
6104 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2006 Fabrice Bellard\n"
6105 "usage: %s [options] [disk_image]\n"
6107 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
6109 "Standard options:\n"
6110 "-M machine select emulated machine (-M ? for list)\n"
6111 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
6112 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
6113 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
6114 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
6115 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
6116 "-disk ide,img=file[,hdx=a..dd][,type=disk|cdrom] \n"
6117 " defaults are: hdx=a,type=disk \n"
6118 "-disk scsi,img=file[,sdx=a..g][,type=disk|cdrom][,id=n] \n"
6119 " defaults are: sdx=a,type=disk,id='auto assign' \n"
6120 "-snapshot write to temporary files instead of disk image files\n"
6122 "-no-quit disable SDL window close capability\n"
6125 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
6127 "-m megs set virtual RAM size to megs MB [default=%d]\n"
6128 "-smp n set the number of CPUs to 'n' [default=1]\n"
6129 "-nographic disable graphical output and redirect serial I/Os to console\n"
6131 "-k language use keyboard layout (for example \"fr\" for French)\n"
6134 "-audio-help print list of audio drivers and their options\n"
6135 "-soundhw c1,... enable audio support\n"
6136 " and only specified sound cards (comma separated list)\n"
6137 " use -soundhw ? to get the list of supported cards\n"
6138 " use -soundhw all to enable all of them\n"
6140 "-localtime set the real time clock to local time [default=utc]\n"
6141 "-full-screen start in full screen\n"
6143 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
6145 "-usb enable the USB driver (will be the default soon)\n"
6146 "-usbdevice name add the host or guest USB device 'name'\n"
6147 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6148 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
6151 "Network options:\n"
6152 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
6153 " create a new Network Interface Card and connect it to VLAN 'n'\n"
6155 "-net user[,vlan=n][,hostname=host]\n"
6156 " connect the user mode network stack to VLAN 'n' and send\n"
6157 " hostname 'host' to DHCP clients\n"
6160 "-net tap[,vlan=n],ifname=name\n"
6161 " connect the host TAP network interface to VLAN 'n'\n"
6163 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
6164 " connect the host TAP network interface to VLAN 'n' and use\n"
6165 " the network script 'file' (default=%s);\n"
6166 " use 'fd=h' to connect to an already opened TAP interface\n"
6168 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
6169 " connect the vlan 'n' to another VLAN using a socket connection\n"
6170 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
6171 " connect the vlan 'n' to multicast maddr and port\n"
6172 "-net none use it alone to have zero network devices; if no -net option\n"
6173 " is provided, the default is '-net nic -net user'\n"
6176 "-tftp prefix allow tftp access to files starting with prefix [-net user]\n"
6178 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
6180 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
6181 " redirect TCP or UDP connections from host to guest [-net user]\n"
6184 "Linux boot specific:\n"
6185 "-kernel bzImage use 'bzImage' as kernel image\n"
6186 "-append cmdline use 'cmdline' as kernel command line\n"
6187 "-initrd file use 'file' as initial ram disk\n"
6189 "Debug/Expert options:\n"
6190 "-monitor dev redirect the monitor to char device 'dev'\n"
6191 "-serial dev redirect the serial port to char device 'dev'\n"
6192 "-parallel dev redirect the parallel port to char device 'dev'\n"
6193 "-pidfile file Write PID to 'file'\n"
6194 "-S freeze CPU at startup (use 'c' to start execution)\n"
6195 "-s wait gdb connection to port %d\n"
6196 "-p port change gdb connection port\n"
6197 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
6198 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
6199 " translation (t=none or lba) (usually qemu can guess them)\n"
6200 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
6202 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
6203 "-no-kqemu disable KQEMU kernel module usage\n"
6205 #ifdef USE_CODE_COPY
6206 "-no-code-copy disable code copy acceleration\n"
6209 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
6210 " (default is CL-GD5446 PCI VGA)\n"
6211 "-no-acpi disable ACPI\n"
6213 "-no-reboot exit instead of rebooting\n"
6214 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
6215 "-vnc display start a VNC server on display\n"
6217 "-daemonize daemonize QEMU after initializing\n"
6220 "During emulation, the following keys are useful:\n"
6221 "ctrl-alt-f toggle full screen\n"
6222 "ctrl-alt-n switch to virtual console 'n'\n"
6223 "ctrl-alt toggle mouse and keyboard grab\n"
6225 "When using -nographic, press 'ctrl-a h' to get some help.\n"
6230 DEFAULT_NETWORK_SCRIPT
,
6232 DEFAULT_GDBSTUB_PORT
,
6237 #define HAS_ARG 0x0001
6251 QEMU_OPTION_snapshot
,
6253 QEMU_OPTION_no_fd_bootchk
,
6256 QEMU_OPTION_nographic
,
6258 QEMU_OPTION_audio_help
,
6259 QEMU_OPTION_soundhw
,
6277 QEMU_OPTION_no_code_copy
,
6279 QEMU_OPTION_localtime
,
6280 QEMU_OPTION_cirrusvga
,
6282 QEMU_OPTION_std_vga
,
6283 QEMU_OPTION_monitor
,
6285 QEMU_OPTION_parallel
,
6287 QEMU_OPTION_full_screen
,
6288 QEMU_OPTION_no_quit
,
6289 QEMU_OPTION_pidfile
,
6290 QEMU_OPTION_no_kqemu
,
6291 QEMU_OPTION_kernel_kqemu
,
6292 QEMU_OPTION_win2k_hack
,
6294 QEMU_OPTION_usbdevice
,
6297 QEMU_OPTION_no_acpi
,
6298 QEMU_OPTION_no_reboot
,
6299 QEMU_OPTION_daemonize
,
6303 typedef struct QEMUOption
{
6309 const QEMUOption qemu_options
[] = {
6310 { "h", 0, QEMU_OPTION_h
},
6312 { "M", HAS_ARG
, QEMU_OPTION_M
},
6313 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
6314 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
6315 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
6316 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
6317 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
6318 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
6319 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
6320 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
6321 { "snapshot", 0, QEMU_OPTION_snapshot
},
6323 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
6325 { "m", HAS_ARG
, QEMU_OPTION_m
},
6326 { "nographic", 0, QEMU_OPTION_nographic
},
6327 { "k", HAS_ARG
, QEMU_OPTION_k
},
6329 { "audio-help", 0, QEMU_OPTION_audio_help
},
6330 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
6333 { "net", HAS_ARG
, QEMU_OPTION_net
},
6335 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
6337 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
6339 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
6342 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
6343 { "append", HAS_ARG
, QEMU_OPTION_append
},
6344 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
6346 { "S", 0, QEMU_OPTION_S
},
6347 { "s", 0, QEMU_OPTION_s
},
6348 { "p", HAS_ARG
, QEMU_OPTION_p
},
6349 { "d", HAS_ARG
, QEMU_OPTION_d
},
6350 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
6351 { "L", HAS_ARG
, QEMU_OPTION_L
},
6352 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
6354 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
6355 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
6357 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6358 { "g", 1, QEMU_OPTION_g
},
6360 { "localtime", 0, QEMU_OPTION_localtime
},
6361 { "std-vga", 0, QEMU_OPTION_std_vga
},
6362 { "monitor", 1, QEMU_OPTION_monitor
},
6363 { "serial", 1, QEMU_OPTION_serial
},
6364 { "parallel", 1, QEMU_OPTION_parallel
},
6365 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
6366 { "full-screen", 0, QEMU_OPTION_full_screen
},
6368 { "no-quit", 0, QEMU_OPTION_no_quit
},
6370 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
6371 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
6372 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
6373 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
6374 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
6375 { "disk", HAS_ARG
, QEMU_OPTION_disk
},
6377 /* temporary options */
6378 { "usb", 0, QEMU_OPTION_usb
},
6379 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
6380 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
6381 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
6382 { "daemonize", 0, QEMU_OPTION_daemonize
},
6386 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
6388 /* this stack is only used during signal handling */
6389 #define SIGNAL_STACK_SIZE 32768
6391 static uint8_t *signal_stack
;
6395 /* password input */
6397 static BlockDriverState
*get_bdrv(int index
)
6399 BlockDriverState
*bs
;
6402 bs
= bs_table
[index
];
6403 } else if (index
< 6) {
6404 bs
= fd_table
[index
- 4];
6411 static void read_passwords(void)
6413 BlockDriverState
*bs
;
6417 for(i
= 0; i
< 6; i
++) {
6419 if (bs
&& bdrv_is_encrypted(bs
)) {
6420 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
6421 for(j
= 0; j
< 3; j
++) {
6422 monitor_readline("Password: ",
6423 1, password
, sizeof(password
));
6424 if (bdrv_set_key(bs
, password
) == 0)
6426 term_printf("invalid password\n");
6432 /* XXX: currently we cannot use simultaneously different CPUs */
6433 void register_machines(void)
6435 #if defined(TARGET_I386)
6436 qemu_register_machine(&pc_machine
);
6437 qemu_register_machine(&isapc_machine
);
6438 #elif defined(TARGET_PPC)
6439 qemu_register_machine(&heathrow_machine
);
6440 qemu_register_machine(&core99_machine
);
6441 qemu_register_machine(&prep_machine
);
6442 #elif defined(TARGET_MIPS)
6443 qemu_register_machine(&mips_machine
);
6444 #elif defined(TARGET_SPARC)
6445 #ifdef TARGET_SPARC64
6446 qemu_register_machine(&sun4u_machine
);
6448 qemu_register_machine(&sun4m_machine
);
6450 #elif defined(TARGET_ARM)
6451 qemu_register_machine(&integratorcp926_machine
);
6452 qemu_register_machine(&integratorcp1026_machine
);
6453 qemu_register_machine(&versatilepb_machine
);
6454 qemu_register_machine(&versatileab_machine
);
6455 qemu_register_machine(&realview_machine
);
6456 #elif defined(TARGET_SH4)
6457 qemu_register_machine(&shix_machine
);
6459 #error unsupported CPU
6464 struct soundhw soundhw
[] = {
6471 { .init_isa
= pcspk_audio_init
}
6476 "Creative Sound Blaster 16",
6479 { .init_isa
= SB16_init
}
6486 "Yamaha YMF262 (OPL3)",
6488 "Yamaha YM3812 (OPL2)",
6492 { .init_isa
= Adlib_init
}
6499 "Gravis Ultrasound GF1",
6502 { .init_isa
= GUS_init
}
6508 "ENSONIQ AudioPCI ES1370",
6511 { .init_pci
= es1370_init
}
6514 { NULL
, NULL
, 0, 0, { NULL
} }
6517 static void select_soundhw (const char *optarg
)
6521 if (*optarg
== '?') {
6524 printf ("Valid sound card names (comma separated):\n");
6525 for (c
= soundhw
; c
->name
; ++c
) {
6526 printf ("%-11s %s\n", c
->name
, c
->descr
);
6528 printf ("\n-soundhw all will enable all of the above\n");
6529 exit (*optarg
!= '?');
6537 if (!strcmp (optarg
, "all")) {
6538 for (c
= soundhw
; c
->name
; ++c
) {
6546 e
= strchr (p
, ',');
6547 l
= !e
? strlen (p
) : (size_t) (e
- p
);
6549 for (c
= soundhw
; c
->name
; ++c
) {
6550 if (!strncmp (c
->name
, p
, l
)) {
6559 "Unknown sound card name (too big to show)\n");
6562 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
6567 p
+= l
+ (e
!= NULL
);
6571 goto show_valid_cards
;
6577 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
6579 exit(STATUS_CONTROL_C_EXIT
);
6584 #define MAX_NET_CLIENTS 32
6586 int main(int argc
, char **argv
)
6588 #ifdef CONFIG_GDBSTUB
6589 int use_gdbstub
, gdbstub_port
;
6592 int snapshot
, linux_boot
;
6593 const char *initrd_filename
;
6594 const char *fd_filename
[MAX_FD
];
6595 char scsi_options
[MAX_SCSI_DISKS
] [DISK_OPTIONS_SIZE
];
6596 char ide_options
[MAX_DISKS
] [DISK_OPTIONS_SIZE
];
6599 const char *kernel_filename
, *kernel_cmdline
;
6600 DisplayState
*ds
= &display_state
;
6601 int cyls
, heads
, secs
, translation
;
6602 int start_emulation
= 1;
6603 char net_clients
[MAX_NET_CLIENTS
][256];
6606 const char *r
, *optarg
;
6607 CharDriverState
*monitor_hd
;
6608 char monitor_device
[128];
6609 char serial_devices
[MAX_SERIAL_PORTS
][128];
6610 int serial_device_index
;
6611 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
6612 int parallel_device_index
;
6613 const char *loadvm
= NULL
;
6614 QEMUMachine
*machine
;
6615 char usb_devices
[MAX_USB_CMDLINE
][128];
6616 int usb_devices_index
;
6619 LIST_INIT (&vm_change_state_head
);
6622 struct sigaction act
;
6623 sigfillset(&act
.sa_mask
);
6625 act
.sa_handler
= SIG_IGN
;
6626 sigaction(SIGPIPE
, &act
, NULL
);
6629 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
6630 /* Note: cpu_interrupt() is currently not SMP safe, so we force
6631 QEMU to run on a single CPU */
6636 h
= GetCurrentProcess();
6637 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
6638 for(i
= 0; i
< 32; i
++) {
6639 if (mask
& (1 << i
))
6644 SetProcessAffinityMask(h
, mask
);
6650 register_machines();
6651 machine
= first_machine
;
6652 initrd_filename
= NULL
;
6653 for(i
= 0; i
< MAX_SCSI_DISKS
; i
++) {
6654 scsi_disks_info
[i
].device_type
= SCSI_NONE
;
6655 bs_scsi_table
[i
] = NULL
;
6661 for(i
= 0; i
< MAX_FD
; i
++)
6662 fd_filename
[i
] = NULL
;
6663 for(i
= 0; i
< MAX_DISKS
; i
++) {
6664 ide_options
[i
][0] = '\0';
6666 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
6667 vga_ram_size
= VGA_RAM_SIZE
;
6668 bios_size
= BIOS_SIZE
;
6669 #ifdef CONFIG_GDBSTUB
6671 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
6675 kernel_filename
= NULL
;
6676 kernel_cmdline
= "";
6682 cyls
= heads
= secs
= 0;
6683 translation
= BIOS_ATA_TRANSLATION_AUTO
;
6684 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
6686 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
6687 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
6688 serial_devices
[i
][0] = '\0';
6689 serial_device_index
= 0;
6691 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
6692 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
6693 parallel_devices
[i
][0] = '\0';
6694 parallel_device_index
= 0;
6696 usb_devices_index
= 0;
6701 /* default mac address of the first network interface */
6710 /* Build new disk IDE syntax string */
6711 pstrcpy(ide_options
[0],
6714 /*Add on image filename */
6715 pstrcpy(&(ide_options
[0][13]),
6716 sizeof(ide_options
[0])-13,
6720 const QEMUOption
*popt
;
6723 popt
= qemu_options
;
6726 fprintf(stderr
, "%s: invalid option -- '%s'\n",
6730 if (!strcmp(popt
->name
, r
+ 1))
6734 if (popt
->flags
& HAS_ARG
) {
6735 if (optind
>= argc
) {
6736 fprintf(stderr
, "%s: option '%s' requires an argument\n",
6740 optarg
= argv
[optind
++];
6745 switch(popt
->index
) {
6747 machine
= find_machine(optarg
);
6750 printf("Supported machines are:\n");
6751 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6752 printf("%-10s %s%s\n",
6754 m
== first_machine
? " (default)" : "");
6759 case QEMU_OPTION_initrd
:
6760 initrd_filename
= optarg
;
6762 case QEMU_OPTION_hda
:
6763 case QEMU_OPTION_hdb
:
6764 case QEMU_OPTION_hdc
:
6765 case QEMU_OPTION_hdd
:
6768 const char newIDE_DiskSyntax
[][10] = {
6769 "hdx=a,img=", "hdx=b,img=", "hdx=c,img=", "hdx=d,img=" };
6771 hd_index
= popt
->index
- QEMU_OPTION_hda
;
6772 if (num_ide_disks
>= MAX_DISKS
) {
6773 fprintf(stderr
, "qemu: too many IDE disks defined.\n");
6776 /* Build new disk IDE syntax string */
6777 pstrcpy(ide_options
[hd_index
],
6779 newIDE_DiskSyntax
[hd_index
]);
6780 /* Add on image filename */
6781 pstrcpy(&(ide_options
[hd_index
][10]),
6782 sizeof(ide_options
[0])-10,
6787 case QEMU_OPTION_disk
: /*Combined IDE and SCSI, for disk and CDROM */
6789 const char *p_input_char
;
6790 char *p_output_string
;
6794 p_input_char
= optarg
;
6795 p_output_string
= device
;
6796 while (*p_input_char
!= '\0' && *p_input_char
!= ',') {
6797 if ((p_output_string
- device
) < sizeof(device
) - 1)
6798 *p_output_string
++ = *p_input_char
;
6801 *p_output_string
= '\0';
6802 if (*p_input_char
== ',')
6805 if (!strcmp(device
, "scsi")) {
6806 if (num_scsi_disks
>= MAX_SCSI_DISKS
) {
6807 fprintf(stderr
, "qemu: too many SCSI disks defined.\n");
6810 pstrcpy(scsi_options
[num_scsi_disks
],
6811 sizeof(scsi_options
[0]),
6814 } else if (!strcmp(device
,"ide")) {
6815 if (num_ide_disks
>= MAX_DISKS
) {
6816 fprintf(stderr
, "qemu: too many IDE disks/cdroms defined.\n");
6819 disk_index
= 0; /* default is hda */
6820 if (get_param_value(device
, sizeof(device
),"hdx",p_input_char
)) {
6821 if (device
[0] >= 'a' && device
[0] <= 'd') {
6822 disk_index
= device
[0] - 'a';
6824 fprintf(stderr
, "qemu: invalid IDE disk hdx= value: %s\n", device
);
6829 pstrcpy(ide_options
[disk_index
],
6830 sizeof(ide_options
[0]),
6834 fprintf(stderr
, "qemu: -disk option must specify IDE or SCSI: %s \n",device
);
6839 case QEMU_OPTION_snapshot
:
6842 case QEMU_OPTION_hdachs
:
6846 cyls
= strtol(p
, (char **)&p
, 0);
6847 if (cyls
< 1 || cyls
> 16383)
6852 heads
= strtol(p
, (char **)&p
, 0);
6853 if (heads
< 1 || heads
> 16)
6858 secs
= strtol(p
, (char **)&p
, 0);
6859 if (secs
< 1 || secs
> 63)
6863 if (!strcmp(p
, "none"))
6864 translation
= BIOS_ATA_TRANSLATION_NONE
;
6865 else if (!strcmp(p
, "lba"))
6866 translation
= BIOS_ATA_TRANSLATION_LBA
;
6867 else if (!strcmp(p
, "auto"))
6868 translation
= BIOS_ATA_TRANSLATION_AUTO
;
6871 } else if (*p
!= '\0') {
6873 fprintf(stderr
, "qemu: invalid physical CHS format\n");
6878 case QEMU_OPTION_nographic
:
6879 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
6880 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
6883 case QEMU_OPTION_kernel
:
6884 kernel_filename
= optarg
;
6886 case QEMU_OPTION_append
:
6887 kernel_cmdline
= optarg
;
6889 case QEMU_OPTION_cdrom
:
6892 if (num_ide_disks
>= MAX_DISKS
) {
6893 fprintf(stderr
, "qemu: too many IDE disks/cdroms defined.\n");
6896 snprintf(buf
, sizeof(buf
), "type=cdrom,hdx=%c,img=", cdrom_index
+ 'a');
6897 /* Build new disk IDE syntax string */
6898 pstrcpy(ide_options
[cdrom_index
],
6901 /* Add on image filename */
6902 pstrcpy(&(ide_options
[cdrom_index
][21]),
6903 sizeof(ide_options
[0])-21,
6908 case QEMU_OPTION_boot
:
6909 boot_device
= optarg
[0];
6910 if (boot_device
!= 'a' &&
6913 boot_device
!= 'n' &&
6915 boot_device
!= 'c' && boot_device
!= 'd') {
6916 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
6920 case QEMU_OPTION_fda
:
6921 fd_filename
[0] = optarg
;
6923 case QEMU_OPTION_fdb
:
6924 fd_filename
[1] = optarg
;
6927 case QEMU_OPTION_no_fd_bootchk
:
6931 case QEMU_OPTION_no_code_copy
:
6932 code_copy_enabled
= 0;
6934 case QEMU_OPTION_net
:
6935 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
6936 fprintf(stderr
, "qemu: too many network clients\n");
6939 pstrcpy(net_clients
[nb_net_clients
],
6940 sizeof(net_clients
[0]),
6945 case QEMU_OPTION_tftp
:
6946 tftp_prefix
= optarg
;
6949 case QEMU_OPTION_smb
:
6950 net_slirp_smb(optarg
);
6953 case QEMU_OPTION_redir
:
6954 net_slirp_redir(optarg
);
6958 case QEMU_OPTION_audio_help
:
6962 case QEMU_OPTION_soundhw
:
6963 select_soundhw (optarg
);
6970 ram_size
= atoi(optarg
) * 1024 * 1024;
6973 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
6974 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
6975 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
6984 mask
= cpu_str_to_log_mask(optarg
);
6986 printf("Log items (comma separated):\n");
6987 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
6988 printf("%-10s %s\n", item
->name
, item
->help
);
6995 #ifdef CONFIG_GDBSTUB
7000 gdbstub_port
= atoi(optarg
);
7007 start_emulation
= 0;
7010 keyboard_layout
= optarg
;
7012 case QEMU_OPTION_localtime
:
7015 case QEMU_OPTION_cirrusvga
:
7016 cirrus_vga_enabled
= 1;
7018 case QEMU_OPTION_std_vga
:
7019 cirrus_vga_enabled
= 0;
7026 w
= strtol(p
, (char **)&p
, 10);
7029 fprintf(stderr
, "qemu: invalid resolution or depth\n");
7035 h
= strtol(p
, (char **)&p
, 10);
7040 depth
= strtol(p
, (char **)&p
, 10);
7041 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
7042 depth
!= 24 && depth
!= 32)
7044 } else if (*p
== '\0') {
7045 depth
= graphic_depth
;
7052 graphic_depth
= depth
;
7055 case QEMU_OPTION_monitor
:
7056 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
7058 case QEMU_OPTION_serial
:
7059 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
7060 fprintf(stderr
, "qemu: too many serial ports\n");
7063 pstrcpy(serial_devices
[serial_device_index
],
7064 sizeof(serial_devices
[0]), optarg
);
7065 serial_device_index
++;
7067 case QEMU_OPTION_parallel
:
7068 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
7069 fprintf(stderr
, "qemu: too many parallel ports\n");
7072 pstrcpy(parallel_devices
[parallel_device_index
],
7073 sizeof(parallel_devices
[0]), optarg
);
7074 parallel_device_index
++;
7076 case QEMU_OPTION_loadvm
:
7079 case QEMU_OPTION_full_screen
:
7083 case QEMU_OPTION_no_quit
:
7087 case QEMU_OPTION_pidfile
:
7088 create_pidfile(optarg
);
7091 case QEMU_OPTION_win2k_hack
:
7092 win2k_install_hack
= 1;
7096 case QEMU_OPTION_no_kqemu
:
7099 case QEMU_OPTION_kernel_kqemu
:
7103 case QEMU_OPTION_usb
:
7106 case QEMU_OPTION_usbdevice
:
7108 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
7109 fprintf(stderr
, "Too many USB devices\n");
7112 pstrcpy(usb_devices
[usb_devices_index
],
7113 sizeof(usb_devices
[usb_devices_index
]),
7115 usb_devices_index
++;
7117 case QEMU_OPTION_smp
:
7118 smp_cpus
= atoi(optarg
);
7119 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
7120 fprintf(stderr
, "Invalid number of CPUs\n");
7124 case QEMU_OPTION_vnc
:
7125 vnc_display
= optarg
;
7127 case QEMU_OPTION_no_acpi
:
7130 case QEMU_OPTION_no_reboot
:
7133 case QEMU_OPTION_daemonize
:
7141 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
7142 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
7149 if (pipe(fds
) == -1)
7160 len
= read(fds
[0], &status
, 1);
7161 if (len
== -1 && (errno
== EINTR
))
7164 if (len
!= 1 || status
!= 0)
7182 signal(SIGTSTP
, SIG_IGN
);
7183 signal(SIGTTOU
, SIG_IGN
);
7184 signal(SIGTTIN
, SIG_IGN
);
7192 linux_boot
= (kernel_filename
!= NULL
);
7195 num_ide_disks
== 0 &&
7196 fd_filename
[0] == '\0')
7199 setvbuf(stdout
, NULL
, _IOLBF
, 0);
7209 /* init network clients */
7210 if (nb_net_clients
== 0) {
7211 /* if no clients, we use a default config */
7212 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
7214 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
7219 for(i
= 0;i
< nb_net_clients
; i
++) {
7220 if (net_client_init(net_clients
[i
]) < 0)
7224 /* init the memory */
7225 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
7227 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
7228 if (!phys_ram_base
) {
7229 fprintf(stderr
, "Could not allocate physical memory\n");
7235 /* open the virtual block devices, disks or CDRoms */
7236 if (disk_options_init(num_ide_disks
,ide_options
,snapshot
,
7237 num_scsi_disks
,scsi_options
,
7239 cyls
, heads
, secs
, translation
)){
7243 /* boot to floppy or default cd if no hard disk */
7244 if (num_ide_disks
== 0 && boot_device
== 'c') {
7245 if (fd_filename
[0] != '\0')
7251 /* we always create at least one floppy disk */
7252 fd_table
[0] = bdrv_new("fda");
7253 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
7255 for(i
= 0; i
< MAX_FD
; i
++) {
7256 if (fd_filename
[i
]) {
7259 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
7260 fd_table
[i
] = bdrv_new(buf
);
7261 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
7263 if (fd_filename
[i
] != '\0') {
7264 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
7265 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7266 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
7274 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
7275 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
7281 dumb_display_init(ds
);
7282 } else if (vnc_display
!= NULL
) {
7283 vnc_display_init(ds
, vnc_display
);
7285 #if defined(CONFIG_SDL)
7286 sdl_display_init(ds
, full_screen
);
7287 #elif defined(CONFIG_COCOA)
7288 cocoa_display_init(ds
, full_screen
);
7290 dumb_display_init(ds
);
7294 monitor_hd
= qemu_chr_open(monitor_device
);
7296 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
7299 monitor_init(monitor_hd
, !nographic
);
7301 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7302 const char *devname
= serial_devices
[i
];
7303 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7304 serial_hds
[i
] = qemu_chr_open(devname
);
7305 if (!serial_hds
[i
]) {
7306 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
7310 if (!strcmp(devname
, "vc"))
7311 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
7315 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
7316 const char *devname
= parallel_devices
[i
];
7317 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7318 parallel_hds
[i
] = qemu_chr_open(devname
);
7319 if (!parallel_hds
[i
]) {
7320 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
7324 if (!strcmp(devname
, "vc"))
7325 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
7329 machine
->init(ram_size
, vga_ram_size
, boot_device
,
7330 ds
, fd_filename
, snapshot
,
7331 kernel_filename
, kernel_cmdline
, initrd_filename
);
7333 /* init USB devices */
7335 for(i
= 0; i
< usb_devices_index
; i
++) {
7336 if (usb_device_add(usb_devices
[i
]) < 0) {
7337 fprintf(stderr
, "Warning: could not add USB device %s\n",
7343 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
7344 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
7346 #ifdef CONFIG_GDBSTUB
7348 if (gdbserver_start(gdbstub_port
) < 0) {
7349 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
7353 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
7361 /* XXX: simplify init */
7363 if (start_emulation
) {
7374 len
= write(fds
[1], &status
, 1);
7375 if (len
== -1 && (errno
== EINTR
))
7381 fd
= open("/dev/null", O_RDWR
);