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 DEBUG_UNUSED_IOPORT
94 //#define DEBUG_IOPORT
96 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
99 #define DEFAULT_RAM_SIZE 144
101 #define DEFAULT_RAM_SIZE 128
104 #define GUI_REFRESH_INTERVAL 30
106 /* Max number of USB devices that can be specified on the commandline. */
107 #define MAX_USB_CMDLINE 8
109 /* XXX: use a two level table to limit memory usage */
110 #define MAX_IOPORTS 65536
112 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
113 char phys_ram_file
[1024];
114 void *ioport_opaque
[MAX_IOPORTS
];
115 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
116 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
117 /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available
118 to store the VM snapshots */
119 BlockDriverState
*bs_table
[MAX_DISKS
+ 1], *fd_table
[MAX_FD
];
120 /* point to the block driver where the snapshots are managed */
121 BlockDriverState
*bs_snapshots
;
124 static DisplayState display_state
;
126 const char* keyboard_layout
= NULL
;
127 int64_t ticks_per_sec
;
128 int boot_device
= 'c';
130 int pit_min_timer_count
= 0;
132 NICInfo nd_table
[MAX_NICS
];
133 QEMUTimer
*gui_timer
;
136 int cirrus_vga_enabled
= 1;
138 int graphic_width
= 1024;
139 int graphic_height
= 768;
141 int graphic_width
= 800;
142 int graphic_height
= 600;
144 int graphic_depth
= 15;
146 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
147 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
149 int win2k_install_hack
= 0;
152 static VLANState
*first_vlan
;
154 int vnc_display
= -1;
155 #if defined(TARGET_SPARC)
157 #elif defined(TARGET_I386)
162 int acpi_enabled
= 1;
165 /***********************************************************/
166 /* x86 ISA bus support */
168 target_phys_addr_t isa_mem_base
= 0;
171 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
173 #ifdef DEBUG_UNUSED_IOPORT
174 fprintf(stderr
, "inb: port=0x%04x\n", address
);
179 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
181 #ifdef DEBUG_UNUSED_IOPORT
182 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
186 /* default is to make two byte accesses */
187 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
190 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
191 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
192 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
196 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
198 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
199 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
200 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
203 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
205 #ifdef DEBUG_UNUSED_IOPORT
206 fprintf(stderr
, "inl: port=0x%04x\n", address
);
211 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
213 #ifdef DEBUG_UNUSED_IOPORT
214 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
218 void init_ioports(void)
222 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
223 ioport_read_table
[0][i
] = default_ioport_readb
;
224 ioport_write_table
[0][i
] = default_ioport_writeb
;
225 ioport_read_table
[1][i
] = default_ioport_readw
;
226 ioport_write_table
[1][i
] = default_ioport_writew
;
227 ioport_read_table
[2][i
] = default_ioport_readl
;
228 ioport_write_table
[2][i
] = default_ioport_writel
;
232 /* size is the word size in byte */
233 int register_ioport_read(int start
, int length
, int size
,
234 IOPortReadFunc
*func
, void *opaque
)
240 } else if (size
== 2) {
242 } else if (size
== 4) {
245 hw_error("register_ioport_read: invalid size");
248 for(i
= start
; i
< start
+ length
; i
+= size
) {
249 ioport_read_table
[bsize
][i
] = func
;
250 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
251 hw_error("register_ioport_read: invalid opaque");
252 ioport_opaque
[i
] = opaque
;
257 /* size is the word size in byte */
258 int register_ioport_write(int start
, int length
, int size
,
259 IOPortWriteFunc
*func
, void *opaque
)
265 } else if (size
== 2) {
267 } else if (size
== 4) {
270 hw_error("register_ioport_write: invalid size");
273 for(i
= start
; i
< start
+ length
; i
+= size
) {
274 ioport_write_table
[bsize
][i
] = func
;
275 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
276 hw_error("register_ioport_read: invalid opaque");
277 ioport_opaque
[i
] = opaque
;
282 void isa_unassign_ioport(int start
, int length
)
286 for(i
= start
; i
< start
+ length
; i
++) {
287 ioport_read_table
[0][i
] = default_ioport_readb
;
288 ioport_read_table
[1][i
] = default_ioport_readw
;
289 ioport_read_table
[2][i
] = default_ioport_readl
;
291 ioport_write_table
[0][i
] = default_ioport_writeb
;
292 ioport_write_table
[1][i
] = default_ioport_writew
;
293 ioport_write_table
[2][i
] = default_ioport_writel
;
297 /***********************************************************/
299 void pstrcpy(char *buf
, int buf_size
, const char *str
)
309 if (c
== 0 || q
>= buf
+ buf_size
- 1)
316 /* strcat and truncate. */
317 char *pstrcat(char *buf
, int buf_size
, const char *s
)
322 pstrcpy(buf
+ len
, buf_size
- len
, s
);
326 int strstart(const char *str
, const char *val
, const char **ptr
)
342 void cpu_outb(CPUState
*env
, int addr
, int val
)
345 if (loglevel
& CPU_LOG_IOPORT
)
346 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
348 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
351 env
->last_io_time
= cpu_get_time_fast();
355 void cpu_outw(CPUState
*env
, int addr
, int val
)
358 if (loglevel
& CPU_LOG_IOPORT
)
359 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
361 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
364 env
->last_io_time
= cpu_get_time_fast();
368 void cpu_outl(CPUState
*env
, int addr
, int val
)
371 if (loglevel
& CPU_LOG_IOPORT
)
372 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
374 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
377 env
->last_io_time
= cpu_get_time_fast();
381 int cpu_inb(CPUState
*env
, int addr
)
384 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
386 if (loglevel
& CPU_LOG_IOPORT
)
387 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
391 env
->last_io_time
= cpu_get_time_fast();
396 int cpu_inw(CPUState
*env
, int addr
)
399 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
401 if (loglevel
& CPU_LOG_IOPORT
)
402 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
406 env
->last_io_time
= cpu_get_time_fast();
411 int cpu_inl(CPUState
*env
, int addr
)
414 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
416 if (loglevel
& CPU_LOG_IOPORT
)
417 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
421 env
->last_io_time
= cpu_get_time_fast();
426 /***********************************************************/
427 void hw_error(const char *fmt
, ...)
433 fprintf(stderr
, "qemu: hardware error: ");
434 vfprintf(stderr
, fmt
, ap
);
435 fprintf(stderr
, "\n");
436 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
437 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
439 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
441 cpu_dump_state(env
, stderr
, fprintf
, 0);
448 /***********************************************************/
451 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
452 static void *qemu_put_kbd_event_opaque
;
453 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
454 static void *qemu_put_mouse_event_opaque
;
455 static int qemu_put_mouse_event_absolute
;
457 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
459 qemu_put_kbd_event_opaque
= opaque
;
460 qemu_put_kbd_event
= func
;
463 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
, int absolute
)
465 qemu_put_mouse_event_opaque
= opaque
;
466 qemu_put_mouse_event
= func
;
467 qemu_put_mouse_event_absolute
= absolute
;
470 void kbd_put_keycode(int keycode
)
472 if (qemu_put_kbd_event
) {
473 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
477 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
479 if (qemu_put_mouse_event
) {
480 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
481 dx
, dy
, dz
, buttons_state
);
485 int kbd_mouse_is_absolute(void)
487 return qemu_put_mouse_event_absolute
;
490 /* compute with 96 bit intermediate result: (a*b)/c */
491 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
496 #ifdef WORDS_BIGENDIAN
506 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
507 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
510 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
514 /***********************************************************/
515 /* real time host monotonic timer */
517 #define QEMU_TIMER_BASE 1000000000LL
521 static int64_t clock_freq
;
523 static void init_get_clock(void)
527 ret
= QueryPerformanceFrequency(&freq
);
529 fprintf(stderr
, "Could not calibrate ticks\n");
532 clock_freq
= freq
.QuadPart
;
535 static int64_t get_clock(void)
538 QueryPerformanceCounter(&ti
);
539 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
544 static int use_rt_clock
;
546 static void init_get_clock(void)
549 #if defined(__linux__)
552 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
559 static int64_t get_clock(void)
561 #if defined(__linux__)
564 clock_gettime(CLOCK_MONOTONIC
, &ts
);
565 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
569 /* XXX: using gettimeofday leads to problems if the date
570 changes, so it should be avoided. */
572 gettimeofday(&tv
, NULL
);
573 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
579 /***********************************************************/
580 /* guest cycle counter */
582 static int64_t cpu_ticks_prev
;
583 static int64_t cpu_ticks_offset
;
584 static int64_t cpu_clock_offset
;
585 static int cpu_ticks_enabled
;
587 /* return the host CPU cycle counter and handle stop/restart */
588 int64_t cpu_get_ticks(void)
590 if (!cpu_ticks_enabled
) {
591 return cpu_ticks_offset
;
594 ticks
= cpu_get_real_ticks();
595 if (cpu_ticks_prev
> ticks
) {
596 /* Note: non increasing ticks may happen if the host uses
598 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
600 cpu_ticks_prev
= ticks
;
601 return ticks
+ cpu_ticks_offset
;
605 /* return the host CPU monotonic timer and handle stop/restart */
606 static int64_t cpu_get_clock(void)
609 if (!cpu_ticks_enabled
) {
610 return cpu_clock_offset
;
613 return ti
+ cpu_clock_offset
;
617 /* enable cpu_get_ticks() */
618 void cpu_enable_ticks(void)
620 if (!cpu_ticks_enabled
) {
621 cpu_ticks_offset
-= cpu_get_real_ticks();
622 cpu_clock_offset
-= get_clock();
623 cpu_ticks_enabled
= 1;
627 /* disable cpu_get_ticks() : the clock is stopped. You must not call
628 cpu_get_ticks() after that. */
629 void cpu_disable_ticks(void)
631 if (cpu_ticks_enabled
) {
632 cpu_ticks_offset
= cpu_get_ticks();
633 cpu_clock_offset
= cpu_get_clock();
634 cpu_ticks_enabled
= 0;
638 /***********************************************************/
641 #define QEMU_TIMER_REALTIME 0
642 #define QEMU_TIMER_VIRTUAL 1
646 /* XXX: add frequency */
654 struct QEMUTimer
*next
;
660 static QEMUTimer
*active_timers
[2];
662 static MMRESULT timerID
;
663 static HANDLE host_alarm
= NULL
;
664 static unsigned int period
= 1;
666 /* frequency of the times() clock tick */
667 static int timer_freq
;
670 QEMUClock
*qemu_new_clock(int type
)
673 clock
= qemu_mallocz(sizeof(QEMUClock
));
680 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
684 ts
= qemu_mallocz(sizeof(QEMUTimer
));
691 void qemu_free_timer(QEMUTimer
*ts
)
696 /* stop a timer, but do not dealloc it */
697 void qemu_del_timer(QEMUTimer
*ts
)
701 /* NOTE: this code must be signal safe because
702 qemu_timer_expired() can be called from a signal. */
703 pt
= &active_timers
[ts
->clock
->type
];
716 /* modify the current timer so that it will be fired when current_time
717 >= expire_time. The corresponding callback will be called. */
718 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
724 /* add the timer in the sorted list */
725 /* NOTE: this code must be signal safe because
726 qemu_timer_expired() can be called from a signal. */
727 pt
= &active_timers
[ts
->clock
->type
];
732 if (t
->expire_time
> expire_time
)
736 ts
->expire_time
= expire_time
;
741 int qemu_timer_pending(QEMUTimer
*ts
)
744 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
751 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
755 return (timer_head
->expire_time
<= current_time
);
758 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
764 if (!ts
|| ts
->expire_time
> current_time
)
766 /* remove timer from the list before calling the callback */
767 *ptimer_head
= ts
->next
;
770 /* run the callback (the timer list can be modified) */
775 int64_t qemu_get_clock(QEMUClock
*clock
)
777 switch(clock
->type
) {
778 case QEMU_TIMER_REALTIME
:
779 return get_clock() / 1000000;
781 case QEMU_TIMER_VIRTUAL
:
782 return cpu_get_clock();
786 static void init_timers(void)
789 ticks_per_sec
= QEMU_TIMER_BASE
;
790 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
791 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
795 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
797 uint64_t expire_time
;
799 if (qemu_timer_pending(ts
)) {
800 expire_time
= ts
->expire_time
;
804 qemu_put_be64(f
, expire_time
);
807 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
809 uint64_t expire_time
;
811 expire_time
= qemu_get_be64(f
);
812 if (expire_time
!= -1) {
813 qemu_mod_timer(ts
, expire_time
);
819 static void timer_save(QEMUFile
*f
, void *opaque
)
821 if (cpu_ticks_enabled
) {
822 hw_error("cannot save state if virtual timers are running");
824 qemu_put_be64s(f
, &cpu_ticks_offset
);
825 qemu_put_be64s(f
, &ticks_per_sec
);
826 qemu_put_be64s(f
, &cpu_clock_offset
);
829 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
831 if (version_id
!= 1 && version_id
!= 2)
833 if (cpu_ticks_enabled
) {
836 qemu_get_be64s(f
, &cpu_ticks_offset
);
837 qemu_get_be64s(f
, &ticks_per_sec
);
838 if (version_id
== 2) {
839 qemu_get_be64s(f
, &cpu_clock_offset
);
845 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
846 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
848 static void host_alarm_handler(int host_signum
)
852 #define DISP_FREQ 1000
854 static int64_t delta_min
= INT64_MAX
;
855 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
857 ti
= qemu_get_clock(vm_clock
);
858 if (last_clock
!= 0) {
859 delta
= ti
- last_clock
;
860 if (delta
< delta_min
)
862 if (delta
> delta_max
)
865 if (++count
== DISP_FREQ
) {
866 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
867 muldiv64(delta_min
, 1000000, ticks_per_sec
),
868 muldiv64(delta_max
, 1000000, ticks_per_sec
),
869 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
870 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
872 delta_min
= INT64_MAX
;
880 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
881 qemu_get_clock(vm_clock
)) ||
882 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
883 qemu_get_clock(rt_clock
))) {
885 SetEvent(host_alarm
);
887 CPUState
*env
= cpu_single_env
;
889 /* stop the currently executing cpu because a timer occured */
890 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
892 if (env
->kqemu_enabled
) {
893 kqemu_cpu_interrupt(env
);
902 #if defined(__linux__)
904 #define RTC_FREQ 1024
908 static int start_rtc_timer(void)
910 rtc_fd
= open("/dev/rtc", O_RDONLY
);
913 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
914 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
915 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
916 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
919 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
924 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
930 static int start_rtc_timer(void)
935 #endif /* !defined(__linux__) */
937 #endif /* !defined(_WIN32) */
939 static void init_timer_alarm(void)
946 ZeroMemory(&tc
, sizeof(TIMECAPS
));
947 timeGetDevCaps(&tc
, sizeof(TIMECAPS
));
948 if (period
< tc
.wPeriodMin
)
949 period
= tc
.wPeriodMin
;
950 timeBeginPeriod(period
);
951 timerID
= timeSetEvent(1, // interval (ms)
952 period
, // resolution
953 host_alarm_handler
, // function
954 (DWORD
)&count
, // user parameter
955 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
957 perror("failed timer alarm");
960 host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
962 perror("failed CreateEvent");
965 qemu_add_wait_object(host_alarm
, NULL
, NULL
);
967 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
970 struct sigaction act
;
971 struct itimerval itv
;
973 /* get times() syscall frequency */
974 timer_freq
= sysconf(_SC_CLK_TCK
);
977 sigfillset(&act
.sa_mask
);
979 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
980 act
.sa_flags
|= SA_ONSTACK
;
982 act
.sa_handler
= host_alarm_handler
;
983 sigaction(SIGALRM
, &act
, NULL
);
985 itv
.it_interval
.tv_sec
= 0;
986 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
987 itv
.it_value
.tv_sec
= 0;
988 itv
.it_value
.tv_usec
= 10 * 1000;
989 setitimer(ITIMER_REAL
, &itv
, NULL
);
990 /* we probe the tick duration of the kernel to inform the user if
991 the emulated kernel requested a too high timer frequency */
992 getitimer(ITIMER_REAL
, &itv
);
994 #if defined(__linux__)
995 /* XXX: force /dev/rtc usage because even 2.6 kernels may not
996 have timers with 1 ms resolution. The correct solution will
997 be to use the POSIX real time timers available in recent
999 if (itv
.it_interval
.tv_usec
> 1000 || 1) {
1000 /* try to use /dev/rtc to have a faster timer */
1001 if (start_rtc_timer() < 0)
1003 /* disable itimer */
1004 itv
.it_interval
.tv_sec
= 0;
1005 itv
.it_interval
.tv_usec
= 0;
1006 itv
.it_value
.tv_sec
= 0;
1007 itv
.it_value
.tv_usec
= 0;
1008 setitimer(ITIMER_REAL
, &itv
, NULL
);
1011 sigaction(SIGIO
, &act
, NULL
);
1012 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1013 fcntl(rtc_fd
, F_SETOWN
, getpid());
1015 #endif /* defined(__linux__) */
1018 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1019 PIT_FREQ
) / 1000000;
1025 void quit_timers(void)
1028 timeKillEvent(timerID
);
1029 timeEndPeriod(period
);
1031 CloseHandle(host_alarm
);
1037 /***********************************************************/
1038 /* character device */
1040 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1042 return s
->chr_write(s
, buf
, len
);
1045 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1049 return s
->chr_ioctl(s
, cmd
, arg
);
1052 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1057 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1058 qemu_chr_write(s
, buf
, strlen(buf
));
1062 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1064 if (s
->chr_send_event
)
1065 s
->chr_send_event(s
, event
);
1068 void qemu_chr_add_read_handler(CharDriverState
*s
,
1069 IOCanRWHandler
*fd_can_read
,
1070 IOReadHandler
*fd_read
, void *opaque
)
1072 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
1075 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
1077 s
->chr_event
= chr_event
;
1080 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1085 static void null_chr_add_read_handler(CharDriverState
*chr
,
1086 IOCanRWHandler
*fd_can_read
,
1087 IOReadHandler
*fd_read
, void *opaque
)
1091 CharDriverState
*qemu_chr_open_null(void)
1093 CharDriverState
*chr
;
1095 chr
= qemu_mallocz(sizeof(CharDriverState
));
1098 chr
->chr_write
= null_chr_write
;
1099 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1105 static void socket_cleanup(void)
1110 static int socket_init(void)
1115 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1117 err
= WSAGetLastError();
1118 fprintf(stderr
, "WSAStartup: %d\n", err
);
1121 atexit(socket_cleanup
);
1125 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1131 ret
= send(fd
, buf
, len
, 0);
1134 errno
= WSAGetLastError();
1135 if (errno
!= WSAEWOULDBLOCK
) {
1138 } else if (ret
== 0) {
1148 void socket_set_nonblock(int fd
)
1150 unsigned long opt
= 1;
1151 ioctlsocket(fd
, FIONBIO
, &opt
);
1156 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1162 ret
= write(fd
, buf
, len
);
1164 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1166 } else if (ret
== 0) {
1176 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1178 return unix_write(fd
, buf
, len1
);
1181 void socket_set_nonblock(int fd
)
1183 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1185 #endif /* !_WIN32 */
1191 IOCanRWHandler
*fd_can_read
;
1192 IOReadHandler
*fd_read
;
1197 #define STDIO_MAX_CLIENTS 2
1199 static int stdio_nb_clients
;
1200 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1202 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1204 FDCharDriver
*s
= chr
->opaque
;
1205 return unix_write(s
->fd_out
, buf
, len
);
1208 static int fd_chr_read_poll(void *opaque
)
1210 CharDriverState
*chr
= opaque
;
1211 FDCharDriver
*s
= chr
->opaque
;
1213 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
1217 static void fd_chr_read(void *opaque
)
1219 CharDriverState
*chr
= opaque
;
1220 FDCharDriver
*s
= chr
->opaque
;
1225 if (len
> s
->max_size
)
1229 size
= read(s
->fd_in
, buf
, len
);
1231 s
->fd_read(s
->fd_opaque
, buf
, size
);
1235 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1236 IOCanRWHandler
*fd_can_read
,
1237 IOReadHandler
*fd_read
, void *opaque
)
1239 FDCharDriver
*s
= chr
->opaque
;
1241 if (s
->fd_in
>= 0) {
1242 s
->fd_can_read
= fd_can_read
;
1243 s
->fd_read
= fd_read
;
1244 s
->fd_opaque
= opaque
;
1245 if (nographic
&& s
->fd_in
== 0) {
1247 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1248 fd_chr_read
, NULL
, chr
);
1253 /* open a character device to a unix fd */
1254 CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1256 CharDriverState
*chr
;
1259 chr
= qemu_mallocz(sizeof(CharDriverState
));
1262 s
= qemu_mallocz(sizeof(FDCharDriver
));
1270 chr
->chr_write
= fd_chr_write
;
1271 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1275 CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1279 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1282 return qemu_chr_open_fd(-1, fd_out
);
1285 CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1289 fd
= open(filename
, O_RDWR
| O_BINARY
);
1292 return qemu_chr_open_fd(fd
, fd
);
1296 /* for STDIO, we handle the case where several clients use it
1299 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1301 #define TERM_FIFO_MAX_SIZE 1
1303 static int term_got_escape
, client_index
;
1304 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1305 static int term_fifo_size
;
1306 static int term_timestamps
;
1307 static int64_t term_timestamps_start
;
1309 void term_print_help(void)
1312 "C-a h print this help\n"
1313 "C-a x exit emulator\n"
1314 "C-a s save disk data back to file (if -snapshot)\n"
1315 "C-a b send break (magic sysrq)\n"
1316 "C-a t toggle console timestamps\n"
1317 "C-a c switch between console and monitor\n"
1318 "C-a C-a send C-a\n"
1322 /* called when a char is received */
1323 static void stdio_received_byte(int ch
)
1325 if (term_got_escape
) {
1326 term_got_escape
= 0;
1337 for (i
= 0; i
< MAX_DISKS
; i
++) {
1339 bdrv_commit(bs_table
[i
]);
1344 if (client_index
< stdio_nb_clients
) {
1345 CharDriverState
*chr
;
1348 chr
= stdio_clients
[client_index
];
1350 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1355 if (client_index
>= stdio_nb_clients
)
1357 if (client_index
== 0) {
1358 /* send a new line in the monitor to get the prompt */
1364 term_timestamps
= !term_timestamps
;
1365 term_timestamps_start
= -1;
1370 } else if (ch
== TERM_ESCAPE
) {
1371 term_got_escape
= 1;
1374 if (client_index
< stdio_nb_clients
) {
1376 CharDriverState
*chr
;
1379 chr
= stdio_clients
[client_index
];
1381 if (s
->fd_can_read(s
->fd_opaque
) > 0) {
1383 s
->fd_read(s
->fd_opaque
, buf
, 1);
1384 } else if (term_fifo_size
== 0) {
1385 term_fifo
[term_fifo_size
++] = ch
;
1391 static int stdio_read_poll(void *opaque
)
1393 CharDriverState
*chr
;
1396 if (client_index
< stdio_nb_clients
) {
1397 chr
= stdio_clients
[client_index
];
1399 /* try to flush the queue if needed */
1400 if (term_fifo_size
!= 0 && s
->fd_can_read(s
->fd_opaque
) > 0) {
1401 s
->fd_read(s
->fd_opaque
, term_fifo
, 1);
1404 /* see if we can absorb more chars */
1405 if (term_fifo_size
== 0)
1414 static void stdio_read(void *opaque
)
1419 size
= read(0, buf
, 1);
1421 stdio_received_byte(buf
[0]);
1424 static int stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1426 FDCharDriver
*s
= chr
->opaque
;
1427 if (!term_timestamps
) {
1428 return unix_write(s
->fd_out
, buf
, len
);
1433 for(i
= 0; i
< len
; i
++) {
1434 unix_write(s
->fd_out
, buf
+ i
, 1);
1435 if (buf
[i
] == '\n') {
1440 if (term_timestamps_start
== -1)
1441 term_timestamps_start
= ti
;
1442 ti
-= term_timestamps_start
;
1443 secs
= ti
/ 1000000000;
1444 snprintf(buf1
, sizeof(buf1
),
1445 "[%02d:%02d:%02d.%03d] ",
1449 (int)((ti
/ 1000000) % 1000));
1450 unix_write(s
->fd_out
, buf1
, strlen(buf1
));
1457 /* init terminal so that we can grab keys */
1458 static struct termios oldtty
;
1459 static int old_fd0_flags
;
1461 static void term_exit(void)
1463 tcsetattr (0, TCSANOW
, &oldtty
);
1464 fcntl(0, F_SETFL
, old_fd0_flags
);
1467 static void term_init(void)
1471 tcgetattr (0, &tty
);
1473 old_fd0_flags
= fcntl(0, F_GETFL
);
1475 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1476 |INLCR
|IGNCR
|ICRNL
|IXON
);
1477 tty
.c_oflag
|= OPOST
;
1478 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1479 /* if graphical mode, we allow Ctrl-C handling */
1481 tty
.c_lflag
&= ~ISIG
;
1482 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1485 tty
.c_cc
[VTIME
] = 0;
1487 tcsetattr (0, TCSANOW
, &tty
);
1491 fcntl(0, F_SETFL
, O_NONBLOCK
);
1494 CharDriverState
*qemu_chr_open_stdio(void)
1496 CharDriverState
*chr
;
1499 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1501 chr
= qemu_chr_open_fd(0, 1);
1502 chr
->chr_write
= stdio_write
;
1503 if (stdio_nb_clients
== 0)
1504 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, NULL
);
1505 client_index
= stdio_nb_clients
;
1507 if (stdio_nb_clients
!= 0)
1509 chr
= qemu_chr_open_fd(0, 1);
1511 stdio_clients
[stdio_nb_clients
++] = chr
;
1512 if (stdio_nb_clients
== 1) {
1513 /* set the terminal in raw mode */
1519 #if defined(__linux__)
1520 CharDriverState
*qemu_chr_open_pty(void)
1523 char slave_name
[1024];
1524 int master_fd
, slave_fd
;
1526 /* Not satisfying */
1527 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1531 /* Disabling local echo and line-buffered output */
1532 tcgetattr (master_fd
, &tty
);
1533 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1535 tty
.c_cc
[VTIME
] = 0;
1536 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1538 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1539 return qemu_chr_open_fd(master_fd
, master_fd
);
1542 static void tty_serial_init(int fd
, int speed
,
1543 int parity
, int data_bits
, int stop_bits
)
1549 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1550 speed
, parity
, data_bits
, stop_bits
);
1552 tcgetattr (fd
, &tty
);
1594 cfsetispeed(&tty
, spd
);
1595 cfsetospeed(&tty
, spd
);
1597 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1598 |INLCR
|IGNCR
|ICRNL
|IXON
);
1599 tty
.c_oflag
|= OPOST
;
1600 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1601 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1622 tty
.c_cflag
|= PARENB
;
1625 tty
.c_cflag
|= PARENB
| PARODD
;
1629 tty
.c_cflag
|= CSTOPB
;
1631 tcsetattr (fd
, TCSANOW
, &tty
);
1634 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1636 FDCharDriver
*s
= chr
->opaque
;
1639 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1641 QEMUSerialSetParams
*ssp
= arg
;
1642 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1643 ssp
->data_bits
, ssp
->stop_bits
);
1646 case CHR_IOCTL_SERIAL_SET_BREAK
:
1648 int enable
= *(int *)arg
;
1650 tcsendbreak(s
->fd_in
, 1);
1659 CharDriverState
*qemu_chr_open_tty(const char *filename
)
1661 CharDriverState
*chr
;
1664 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1667 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1668 tty_serial_init(fd
, 115200, 'N', 8, 1);
1669 chr
= qemu_chr_open_fd(fd
, fd
);
1672 chr
->chr_ioctl
= tty_serial_ioctl
;
1676 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1678 int fd
= (int)chr
->opaque
;
1682 case CHR_IOCTL_PP_READ_DATA
:
1683 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1685 *(uint8_t *)arg
= b
;
1687 case CHR_IOCTL_PP_WRITE_DATA
:
1688 b
= *(uint8_t *)arg
;
1689 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1692 case CHR_IOCTL_PP_READ_CONTROL
:
1693 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1695 *(uint8_t *)arg
= b
;
1697 case CHR_IOCTL_PP_WRITE_CONTROL
:
1698 b
= *(uint8_t *)arg
;
1699 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1702 case CHR_IOCTL_PP_READ_STATUS
:
1703 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1705 *(uint8_t *)arg
= b
;
1713 CharDriverState
*qemu_chr_open_pp(const char *filename
)
1715 CharDriverState
*chr
;
1718 fd
= open(filename
, O_RDWR
);
1722 if (ioctl(fd
, PPCLAIM
) < 0) {
1727 chr
= qemu_mallocz(sizeof(CharDriverState
));
1732 chr
->opaque
= (void *)fd
;
1733 chr
->chr_write
= null_chr_write
;
1734 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1735 chr
->chr_ioctl
= pp_ioctl
;
1740 CharDriverState
*qemu_chr_open_pty(void)
1746 #endif /* !defined(_WIN32) */
1750 IOCanRWHandler
*fd_can_read
;
1751 IOReadHandler
*fd_read
;
1754 HANDLE hcom
, hrecv
, hsend
;
1755 OVERLAPPED orecv
, osend
;
1760 #define NSENDBUF 2048
1761 #define NRECVBUF 2048
1762 #define MAXCONNECT 1
1763 #define NTIMEOUT 5000
1765 static int win_chr_poll(void *opaque
);
1766 static int win_chr_pipe_poll(void *opaque
);
1768 static void win_chr_close2(WinCharState
*s
)
1771 CloseHandle(s
->hsend
);
1775 CloseHandle(s
->hrecv
);
1779 CloseHandle(s
->hcom
);
1783 qemu_del_polling_cb(win_chr_pipe_poll
, s
);
1785 qemu_del_polling_cb(win_chr_poll
, s
);
1788 static void win_chr_close(CharDriverState
*chr
)
1790 WinCharState
*s
= chr
->opaque
;
1794 static int win_chr_init(WinCharState
*s
, const char *filename
)
1797 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1802 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1804 fprintf(stderr
, "Failed CreateEvent\n");
1807 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1809 fprintf(stderr
, "Failed CreateEvent\n");
1813 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1814 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1815 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1816 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1821 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1822 fprintf(stderr
, "Failed SetupComm\n");
1826 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1827 size
= sizeof(COMMCONFIG
);
1828 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1829 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1830 CommConfigDialog(filename
, NULL
, &comcfg
);
1832 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1833 fprintf(stderr
, "Failed SetCommState\n");
1837 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1838 fprintf(stderr
, "Failed SetCommMask\n");
1842 cto
.ReadIntervalTimeout
= MAXDWORD
;
1843 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1844 fprintf(stderr
, "Failed SetCommTimeouts\n");
1848 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1849 fprintf(stderr
, "Failed ClearCommError\n");
1852 qemu_add_polling_cb(win_chr_poll
, s
);
1860 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1862 WinCharState
*s
= chr
->opaque
;
1863 DWORD len
, ret
, size
, err
;
1866 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1867 s
->osend
.hEvent
= s
->hsend
;
1870 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1872 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1874 err
= GetLastError();
1875 if (err
== ERROR_IO_PENDING
) {
1876 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1894 static int win_chr_read_poll(WinCharState
*s
)
1896 s
->max_size
= s
->fd_can_read(s
->win_opaque
);
1900 static void win_chr_readfile(WinCharState
*s
)
1906 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1907 s
->orecv
.hEvent
= s
->hrecv
;
1908 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1910 err
= GetLastError();
1911 if (err
== ERROR_IO_PENDING
) {
1912 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1917 s
->fd_read(s
->win_opaque
, buf
, size
);
1921 static void win_chr_read(WinCharState
*s
)
1923 if (s
->len
> s
->max_size
)
1924 s
->len
= s
->max_size
;
1928 win_chr_readfile(s
);
1931 static int win_chr_poll(void *opaque
)
1933 WinCharState
*s
= opaque
;
1937 ClearCommError(s
->hcom
, &comerr
, &status
);
1938 if (status
.cbInQue
> 0) {
1939 s
->len
= status
.cbInQue
;
1940 win_chr_read_poll(s
);
1947 static void win_chr_add_read_handler(CharDriverState
*chr
,
1948 IOCanRWHandler
*fd_can_read
,
1949 IOReadHandler
*fd_read
, void *opaque
)
1951 WinCharState
*s
= chr
->opaque
;
1953 s
->fd_can_read
= fd_can_read
;
1954 s
->fd_read
= fd_read
;
1955 s
->win_opaque
= opaque
;
1958 CharDriverState
*qemu_chr_open_win(const char *filename
)
1960 CharDriverState
*chr
;
1963 chr
= qemu_mallocz(sizeof(CharDriverState
));
1966 s
= qemu_mallocz(sizeof(WinCharState
));
1972 chr
->chr_write
= win_chr_write
;
1973 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
1974 chr
->chr_close
= win_chr_close
;
1976 if (win_chr_init(s
, filename
) < 0) {
1984 static int win_chr_pipe_poll(void *opaque
)
1986 WinCharState
*s
= opaque
;
1989 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1992 win_chr_read_poll(s
);
1999 static int win_chr_pipe_init(WinCharState
*s
, const char *filename
)
2008 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2010 fprintf(stderr
, "Failed CreateEvent\n");
2013 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2015 fprintf(stderr
, "Failed CreateEvent\n");
2019 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2020 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2021 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2023 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2024 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2025 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2030 ZeroMemory(&ov
, sizeof(ov
));
2031 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2032 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2034 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2038 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2040 fprintf(stderr
, "Failed GetOverlappedResult\n");
2042 CloseHandle(ov
.hEvent
);
2049 CloseHandle(ov
.hEvent
);
2052 qemu_add_polling_cb(win_chr_pipe_poll
, s
);
2061 CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2063 CharDriverState
*chr
;
2066 chr
= qemu_mallocz(sizeof(CharDriverState
));
2069 s
= qemu_mallocz(sizeof(WinCharState
));
2075 chr
->chr_write
= win_chr_write
;
2076 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2077 chr
->chr_close
= win_chr_close
;
2079 if (win_chr_pipe_init(s
, filename
) < 0) {
2087 CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2089 CharDriverState
*chr
;
2092 chr
= qemu_mallocz(sizeof(CharDriverState
));
2095 s
= qemu_mallocz(sizeof(WinCharState
));
2102 chr
->chr_write
= win_chr_write
;
2103 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2107 CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2111 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2112 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2113 if (fd_out
== INVALID_HANDLE_VALUE
)
2116 return qemu_chr_open_win_file(fd_out
);
2120 /***********************************************************/
2121 /* UDP Net console */
2124 IOCanRWHandler
*fd_can_read
;
2125 IOReadHandler
*fd_read
;
2128 struct sockaddr_in daddr
;
2135 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2137 NetCharDriver
*s
= chr
->opaque
;
2139 return sendto(s
->fd
, buf
, len
, 0,
2140 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2143 static int udp_chr_read_poll(void *opaque
)
2145 CharDriverState
*chr
= opaque
;
2146 NetCharDriver
*s
= chr
->opaque
;
2148 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2150 /* If there were any stray characters in the queue process them
2153 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2154 s
->fd_read(s
->fd_opaque
, &s
->buf
[s
->bufptr
], 1);
2156 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2161 static void udp_chr_read(void *opaque
)
2163 CharDriverState
*chr
= opaque
;
2164 NetCharDriver
*s
= chr
->opaque
;
2166 if (s
->max_size
== 0)
2168 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2169 s
->bufptr
= s
->bufcnt
;
2174 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2175 s
->fd_read(s
->fd_opaque
, &s
->buf
[s
->bufptr
], 1);
2177 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2181 static void udp_chr_add_read_handler(CharDriverState
*chr
,
2182 IOCanRWHandler
*fd_can_read
,
2183 IOReadHandler
*fd_read
, void *opaque
)
2185 NetCharDriver
*s
= chr
->opaque
;
2188 s
->fd_can_read
= fd_can_read
;
2189 s
->fd_read
= fd_read
;
2190 s
->fd_opaque
= opaque
;
2191 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2192 udp_chr_read
, NULL
, chr
);
2196 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2197 int parse_host_src_port(struct sockaddr_in
*haddr
,
2198 struct sockaddr_in
*saddr
,
2201 CharDriverState
*qemu_chr_open_udp(const char *def
)
2203 CharDriverState
*chr
= NULL
;
2204 NetCharDriver
*s
= NULL
;
2206 struct sockaddr_in saddr
;
2208 chr
= qemu_mallocz(sizeof(CharDriverState
));
2211 s
= qemu_mallocz(sizeof(NetCharDriver
));
2215 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2217 perror("socket(PF_INET, SOCK_DGRAM)");
2221 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2222 printf("Could not parse: %s\n", def
);
2226 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2236 chr
->chr_write
= udp_chr_write
;
2237 chr
->chr_add_read_handler
= udp_chr_add_read_handler
;
2250 /***********************************************************/
2251 /* TCP Net console */
2254 IOCanRWHandler
*fd_can_read
;
2255 IOReadHandler
*fd_read
;
2263 static void tcp_chr_accept(void *opaque
);
2265 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2267 TCPCharDriver
*s
= chr
->opaque
;
2269 return send_all(s
->fd
, buf
, len
);
2271 /* XXX: indicate an error ? */
2276 static int tcp_chr_read_poll(void *opaque
)
2278 CharDriverState
*chr
= opaque
;
2279 TCPCharDriver
*s
= chr
->opaque
;
2282 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2287 #define IAC_BREAK 243
2288 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2290 char *buf
, int *size
)
2292 /* Handle any telnet client's basic IAC options to satisfy char by
2293 * char mode with no echo. All IAC options will be removed from
2294 * the buf and the do_telnetopt variable will be used to track the
2295 * state of the width of the IAC information.
2297 * IAC commands come in sets of 3 bytes with the exception of the
2298 * "IAC BREAK" command and the double IAC.
2304 for (i
= 0; i
< *size
; i
++) {
2305 if (s
->do_telnetopt
> 1) {
2306 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2307 /* Double IAC means send an IAC */
2311 s
->do_telnetopt
= 1;
2313 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2314 /* Handle IAC break commands by sending a serial break */
2315 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
2320 if (s
->do_telnetopt
>= 4) {
2321 s
->do_telnetopt
= 1;
2324 if ((unsigned char)buf
[i
] == IAC
) {
2325 s
->do_telnetopt
= 2;
2336 static void tcp_chr_read(void *opaque
)
2338 CharDriverState
*chr
= opaque
;
2339 TCPCharDriver
*s
= chr
->opaque
;
2343 if (!s
->connected
|| s
->max_size
<= 0)
2346 if (len
> s
->max_size
)
2348 size
= recv(s
->fd
, buf
, len
, 0);
2350 /* connection closed */
2352 if (s
->listen_fd
>= 0) {
2353 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2355 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2358 } else if (size
> 0) {
2359 if (s
->do_telnetopt
)
2360 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2362 s
->fd_read(s
->fd_opaque
, buf
, size
);
2366 static void tcp_chr_add_read_handler(CharDriverState
*chr
,
2367 IOCanRWHandler
*fd_can_read
,
2368 IOReadHandler
*fd_read
, void *opaque
)
2370 TCPCharDriver
*s
= chr
->opaque
;
2372 s
->fd_can_read
= fd_can_read
;
2373 s
->fd_read
= fd_read
;
2374 s
->fd_opaque
= opaque
;
2377 static void tcp_chr_connect(void *opaque
)
2379 CharDriverState
*chr
= opaque
;
2380 TCPCharDriver
*s
= chr
->opaque
;
2383 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2384 tcp_chr_read
, NULL
, chr
);
2387 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2388 static void tcp_chr_telnet_init(int fd
)
2391 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2392 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2393 send(fd
, (char *)buf
, 3, 0);
2394 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2395 send(fd
, (char *)buf
, 3, 0);
2396 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2397 send(fd
, (char *)buf
, 3, 0);
2398 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2399 send(fd
, (char *)buf
, 3, 0);
2402 static void tcp_chr_accept(void *opaque
)
2404 CharDriverState
*chr
= opaque
;
2405 TCPCharDriver
*s
= chr
->opaque
;
2406 struct sockaddr_in saddr
;
2411 len
= sizeof(saddr
);
2412 fd
= accept(s
->listen_fd
, (struct sockaddr
*)&saddr
, &len
);
2413 if (fd
< 0 && errno
!= EINTR
) {
2415 } else if (fd
>= 0) {
2416 if (s
->do_telnetopt
)
2417 tcp_chr_telnet_init(fd
);
2421 socket_set_nonblock(fd
);
2423 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2424 tcp_chr_connect(chr
);
2427 static void tcp_chr_close(CharDriverState
*chr
)
2429 TCPCharDriver
*s
= chr
->opaque
;
2432 if (s
->listen_fd
>= 0)
2433 closesocket(s
->listen_fd
);
2437 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2440 CharDriverState
*chr
= NULL
;
2441 TCPCharDriver
*s
= NULL
;
2442 int fd
= -1, ret
, err
, val
;
2444 int is_waitconnect
= 1;
2446 struct sockaddr_in saddr
;
2448 if (parse_host_port(&saddr
, host_str
) < 0)
2452 while((ptr
= strchr(ptr
,','))) {
2454 if (!strncmp(ptr
,"server",6)) {
2456 } else if (!strncmp(ptr
,"nowait",6)) {
2459 printf("Unknown option: %s\n", ptr
);
2466 chr
= qemu_mallocz(sizeof(CharDriverState
));
2469 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2473 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2477 if (!is_waitconnect
)
2478 socket_set_nonblock(fd
);
2484 /* allow fast reuse */
2486 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2488 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2491 ret
= listen(fd
, 0);
2495 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2497 s
->do_telnetopt
= 1;
2500 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2502 err
= socket_error();
2503 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2504 } else if (err
== EINPROGRESS
) {
2516 tcp_chr_connect(chr
);
2518 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2522 chr
->chr_write
= tcp_chr_write
;
2523 chr
->chr_add_read_handler
= tcp_chr_add_read_handler
;
2524 chr
->chr_close
= tcp_chr_close
;
2525 if (is_listen
&& is_waitconnect
) {
2526 printf("QEMU waiting for connection on: %s\n", host_str
);
2527 tcp_chr_accept(chr
);
2528 socket_set_nonblock(s
->listen_fd
);
2540 CharDriverState
*qemu_chr_open(const char *filename
)
2544 if (!strcmp(filename
, "vc")) {
2545 return text_console_init(&display_state
);
2546 } else if (!strcmp(filename
, "null")) {
2547 return qemu_chr_open_null();
2549 if (strstart(filename
, "tcp:", &p
)) {
2550 return qemu_chr_open_tcp(p
, 0);
2552 if (strstart(filename
, "telnet:", &p
)) {
2553 return qemu_chr_open_tcp(p
, 1);
2555 if (strstart(filename
, "udp:", &p
)) {
2556 return qemu_chr_open_udp(p
);
2559 if (strstart(filename
, "file:", &p
)) {
2560 return qemu_chr_open_file_out(p
);
2561 } else if (strstart(filename
, "pipe:", &p
)) {
2562 return qemu_chr_open_pipe(p
);
2563 } else if (!strcmp(filename
, "pty")) {
2564 return qemu_chr_open_pty();
2565 } else if (!strcmp(filename
, "stdio")) {
2566 return qemu_chr_open_stdio();
2569 #if defined(__linux__)
2570 if (strstart(filename
, "/dev/parport", NULL
)) {
2571 return qemu_chr_open_pp(filename
);
2573 if (strstart(filename
, "/dev/", NULL
)) {
2574 return qemu_chr_open_tty(filename
);
2578 if (strstart(filename
, "COM", NULL
)) {
2579 return qemu_chr_open_win(filename
);
2581 if (strstart(filename
, "pipe:", &p
)) {
2582 return qemu_chr_open_win_pipe(p
);
2584 if (strstart(filename
, "file:", &p
)) {
2585 return qemu_chr_open_win_file_out(p
);
2593 void qemu_chr_close(CharDriverState
*chr
)
2596 chr
->chr_close(chr
);
2599 /***********************************************************/
2600 /* network device redirectors */
2602 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2606 for(i
=0;i
<size
;i
+=16) {
2610 fprintf(f
, "%08x ", i
);
2613 fprintf(f
, " %02x", buf
[i
+j
]);
2618 for(j
=0;j
<len
;j
++) {
2620 if (c
< ' ' || c
> '~')
2622 fprintf(f
, "%c", c
);
2628 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
2631 for(i
= 0; i
< 6; i
++) {
2632 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2645 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
2650 p1
= strchr(p
, sep
);
2656 if (len
> buf_size
- 1)
2658 memcpy(buf
, p
, len
);
2665 int parse_host_src_port(struct sockaddr_in
*haddr
,
2666 struct sockaddr_in
*saddr
,
2667 const char *input_str
)
2669 char *str
= strdup(input_str
);
2670 char *host_str
= str
;
2675 * Chop off any extra arguments at the end of the string which
2676 * would start with a comma, then fill in the src port information
2677 * if it was provided else use the "any address" and "any port".
2679 if ((ptr
= strchr(str
,',')))
2682 if ((src_str
= strchr(input_str
,'@'))) {
2687 if (parse_host_port(haddr
, host_str
) < 0)
2690 if (!src_str
|| *src_str
== '\0')
2693 if (parse_host_port(saddr
, src_str
) < 0)
2704 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
2712 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2714 saddr
->sin_family
= AF_INET
;
2715 if (buf
[0] == '\0') {
2716 saddr
->sin_addr
.s_addr
= 0;
2718 if (isdigit(buf
[0])) {
2719 if (!inet_aton(buf
, &saddr
->sin_addr
))
2722 if ((he
= gethostbyname(buf
)) == NULL
)
2724 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
2727 port
= strtol(p
, (char **)&r
, 0);
2730 saddr
->sin_port
= htons(port
);
2734 /* find or alloc a new VLAN */
2735 VLANState
*qemu_find_vlan(int id
)
2737 VLANState
**pvlan
, *vlan
;
2738 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2742 vlan
= qemu_mallocz(sizeof(VLANState
));
2747 pvlan
= &first_vlan
;
2748 while (*pvlan
!= NULL
)
2749 pvlan
= &(*pvlan
)->next
;
2754 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
2755 IOReadHandler
*fd_read
,
2756 IOCanRWHandler
*fd_can_read
,
2759 VLANClientState
*vc
, **pvc
;
2760 vc
= qemu_mallocz(sizeof(VLANClientState
));
2763 vc
->fd_read
= fd_read
;
2764 vc
->fd_can_read
= fd_can_read
;
2765 vc
->opaque
= opaque
;
2769 pvc
= &vlan
->first_client
;
2770 while (*pvc
!= NULL
)
2771 pvc
= &(*pvc
)->next
;
2776 int qemu_can_send_packet(VLANClientState
*vc1
)
2778 VLANState
*vlan
= vc1
->vlan
;
2779 VLANClientState
*vc
;
2781 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2783 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
2790 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
2792 VLANState
*vlan
= vc1
->vlan
;
2793 VLANClientState
*vc
;
2796 printf("vlan %d send:\n", vlan
->id
);
2797 hex_dump(stdout
, buf
, size
);
2799 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2801 vc
->fd_read(vc
->opaque
, buf
, size
);
2806 #if defined(CONFIG_SLIRP)
2808 /* slirp network adapter */
2810 static int slirp_inited
;
2811 static VLANClientState
*slirp_vc
;
2813 int slirp_can_output(void)
2815 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
2818 void slirp_output(const uint8_t *pkt
, int pkt_len
)
2821 printf("slirp output:\n");
2822 hex_dump(stdout
, pkt
, pkt_len
);
2826 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
2829 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
2832 printf("slirp input:\n");
2833 hex_dump(stdout
, buf
, size
);
2835 slirp_input(buf
, size
);
2838 static int net_slirp_init(VLANState
*vlan
)
2840 if (!slirp_inited
) {
2844 slirp_vc
= qemu_new_vlan_client(vlan
,
2845 slirp_receive
, NULL
, NULL
);
2846 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
2850 static void net_slirp_redir(const char *redir_str
)
2855 struct in_addr guest_addr
;
2856 int host_port
, guest_port
;
2858 if (!slirp_inited
) {
2864 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2866 if (!strcmp(buf
, "tcp")) {
2868 } else if (!strcmp(buf
, "udp")) {
2874 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2876 host_port
= strtol(buf
, &r
, 0);
2880 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2882 if (buf
[0] == '\0') {
2883 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
2885 if (!inet_aton(buf
, &guest_addr
))
2888 guest_port
= strtol(p
, &r
, 0);
2892 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
2893 fprintf(stderr
, "qemu: could not set up redirection\n");
2898 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
2906 static void smb_exit(void)
2910 char filename
[1024];
2912 /* erase all the files in the directory */
2913 d
= opendir(smb_dir
);
2918 if (strcmp(de
->d_name
, ".") != 0 &&
2919 strcmp(de
->d_name
, "..") != 0) {
2920 snprintf(filename
, sizeof(filename
), "%s/%s",
2921 smb_dir
, de
->d_name
);
2929 /* automatic user mode samba server configuration */
2930 void net_slirp_smb(const char *exported_dir
)
2932 char smb_conf
[1024];
2933 char smb_cmdline
[1024];
2936 if (!slirp_inited
) {
2941 /* XXX: better tmp dir construction */
2942 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
2943 if (mkdir(smb_dir
, 0700) < 0) {
2944 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
2947 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
2949 f
= fopen(smb_conf
, "w");
2951 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
2958 "socket address=127.0.0.1\n"
2959 "pid directory=%s\n"
2960 "lock directory=%s\n"
2961 "log file=%s/log.smbd\n"
2962 "smb passwd file=%s/smbpasswd\n"
2963 "security = share\n"
2978 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "/usr/sbin/smbd -s %s",
2981 slirp_add_exec(0, smb_cmdline
, 4, 139);
2984 #endif /* !defined(_WIN32) */
2986 #endif /* CONFIG_SLIRP */
2988 #if !defined(_WIN32)
2990 typedef struct TAPState
{
2991 VLANClientState
*vc
;
2995 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
2997 TAPState
*s
= opaque
;
3000 ret
= write(s
->fd
, buf
, size
);
3001 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3008 static void tap_send(void *opaque
)
3010 TAPState
*s
= opaque
;
3014 size
= read(s
->fd
, buf
, sizeof(buf
));
3016 qemu_send_packet(s
->vc
, buf
, size
);
3022 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3026 s
= qemu_mallocz(sizeof(TAPState
));
3030 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3031 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3032 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3037 static int tap_open(char *ifname
, int ifname_size
)
3043 fd
= open("/dev/tap", O_RDWR
);
3045 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3050 dev
= devname(s
.st_rdev
, S_IFCHR
);
3051 pstrcpy(ifname
, ifname_size
, dev
);
3053 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3056 #elif defined(__sun__)
3057 static int tap_open(char *ifname
, int ifname_size
)
3059 fprintf(stderr
, "warning: tap_open not yet implemented\n");
3063 static int tap_open(char *ifname
, int ifname_size
)
3068 fd
= open("/dev/net/tun", O_RDWR
);
3070 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
3073 memset(&ifr
, 0, sizeof(ifr
));
3074 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
3075 if (ifname
[0] != '\0')
3076 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
3078 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
3079 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
3081 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
3085 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
3086 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3091 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
3092 const char *setup_script
)
3095 int pid
, status
, fd
;
3100 if (ifname1
!= NULL
)
3101 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
3104 fd
= tap_open(ifname
, sizeof(ifname
));
3110 if (setup_script
[0] != '\0') {
3111 /* try to launch network init script */
3116 *parg
++ = (char *)setup_script
;
3119 execv(setup_script
, args
);
3122 while (waitpid(pid
, &status
, 0) != pid
);
3123 if (!WIFEXITED(status
) ||
3124 WEXITSTATUS(status
) != 0) {
3125 fprintf(stderr
, "%s: could not launch network script\n",
3131 s
= net_tap_fd_init(vlan
, fd
);
3134 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3135 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
3139 #endif /* !_WIN32 */
3141 /* network connection */
3142 typedef struct NetSocketState
{
3143 VLANClientState
*vc
;
3145 int state
; /* 0 = getting length, 1 = getting data */
3149 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
3152 typedef struct NetSocketListenState
{
3155 } NetSocketListenState
;
3157 /* XXX: we consider we can send the whole packet without blocking */
3158 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
3160 NetSocketState
*s
= opaque
;
3164 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
3165 send_all(s
->fd
, buf
, size
);
3168 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
3170 NetSocketState
*s
= opaque
;
3171 sendto(s
->fd
, buf
, size
, 0,
3172 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
3175 static void net_socket_send(void *opaque
)
3177 NetSocketState
*s
= opaque
;
3182 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
3184 err
= socket_error();
3185 if (err
!= EWOULDBLOCK
)
3187 } else if (size
== 0) {
3188 /* end of connection */
3190 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3196 /* reassemble a packet from the network */
3202 memcpy(s
->buf
+ s
->index
, buf
, l
);
3206 if (s
->index
== 4) {
3208 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
3214 l
= s
->packet_len
- s
->index
;
3217 memcpy(s
->buf
+ s
->index
, buf
, l
);
3221 if (s
->index
>= s
->packet_len
) {
3222 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
3231 static void net_socket_send_dgram(void *opaque
)
3233 NetSocketState
*s
= opaque
;
3236 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
3240 /* end of connection */
3241 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3244 qemu_send_packet(s
->vc
, s
->buf
, size
);
3247 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
3252 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
3253 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
3254 inet_ntoa(mcastaddr
->sin_addr
),
3255 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
3259 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
3261 perror("socket(PF_INET, SOCK_DGRAM)");
3266 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
3267 (const char *)&val
, sizeof(val
));
3269 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
3273 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
3279 /* Add host to multicast group */
3280 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
3281 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
3283 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
3284 (const char *)&imr
, sizeof(struct ip_mreq
));
3286 perror("setsockopt(IP_ADD_MEMBERSHIP)");
3290 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
3292 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
3293 (const char *)&val
, sizeof(val
));
3295 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
3299 socket_set_nonblock(fd
);
3307 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
3310 struct sockaddr_in saddr
;
3312 socklen_t saddr_len
;
3315 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
3316 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
3317 * by ONLY ONE process: we must "clone" this dgram socket --jjo
3321 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
3323 if (saddr
.sin_addr
.s_addr
==0) {
3324 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
3328 /* clone dgram socket */
3329 newfd
= net_socket_mcast_create(&saddr
);
3331 /* error already reported by net_socket_mcast_create() */
3335 /* clone newfd to fd, close newfd */
3340 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
3341 fd
, strerror(errno
));
3346 s
= qemu_mallocz(sizeof(NetSocketState
));
3351 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
3352 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
3354 /* mcast: save bound address as dst */
3355 if (is_connected
) s
->dgram_dst
=saddr
;
3357 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3358 "socket: fd=%d (%s mcast=%s:%d)",
3359 fd
, is_connected
? "cloned" : "",
3360 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3364 static void net_socket_connect(void *opaque
)
3366 NetSocketState
*s
= opaque
;
3367 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
3370 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
3374 s
= qemu_mallocz(sizeof(NetSocketState
));
3378 s
->vc
= qemu_new_vlan_client(vlan
,
3379 net_socket_receive
, NULL
, s
);
3380 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3381 "socket: fd=%d", fd
);
3383 net_socket_connect(s
);
3385 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
3390 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
3393 int so_type
=-1, optlen
=sizeof(so_type
);
3395 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
3396 fprintf(stderr
, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd
);
3401 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
3403 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3405 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
3406 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
3407 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3412 static void net_socket_accept(void *opaque
)
3414 NetSocketListenState
*s
= opaque
;
3416 struct sockaddr_in saddr
;
3421 len
= sizeof(saddr
);
3422 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
3423 if (fd
< 0 && errno
!= EINTR
) {
3425 } else if (fd
>= 0) {
3429 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
3433 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
3434 "socket: connection from %s:%d",
3435 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3439 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
3441 NetSocketListenState
*s
;
3443 struct sockaddr_in saddr
;
3445 if (parse_host_port(&saddr
, host_str
) < 0)
3448 s
= qemu_mallocz(sizeof(NetSocketListenState
));
3452 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3457 socket_set_nonblock(fd
);
3459 /* allow fast reuse */
3461 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3463 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3468 ret
= listen(fd
, 0);
3475 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
3479 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
3482 int fd
, connected
, ret
, err
;
3483 struct sockaddr_in saddr
;
3485 if (parse_host_port(&saddr
, host_str
) < 0)
3488 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3493 socket_set_nonblock(fd
);
3497 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3499 err
= socket_error();
3500 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3501 } else if (err
== EINPROGRESS
) {
3513 s
= net_socket_fd_init(vlan
, fd
, connected
);
3516 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3517 "socket: connect to %s:%d",
3518 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3522 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
3526 struct sockaddr_in saddr
;
3528 if (parse_host_port(&saddr
, host_str
) < 0)
3532 fd
= net_socket_mcast_create(&saddr
);
3536 s
= net_socket_fd_init(vlan
, fd
, 0);
3540 s
->dgram_dst
= saddr
;
3542 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3543 "socket: mcast=%s:%d",
3544 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3549 static int get_param_value(char *buf
, int buf_size
,
3550 const char *tag
, const char *str
)
3559 while (*p
!= '\0' && *p
!= '=') {
3560 if ((q
- option
) < sizeof(option
) - 1)
3568 if (!strcmp(tag
, option
)) {
3570 while (*p
!= '\0' && *p
!= ',') {
3571 if ((q
- buf
) < buf_size
- 1)
3578 while (*p
!= '\0' && *p
!= ',') {
3589 int net_client_init(const char *str
)
3600 while (*p
!= '\0' && *p
!= ',') {
3601 if ((q
- device
) < sizeof(device
) - 1)
3609 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
3610 vlan_id
= strtol(buf
, NULL
, 0);
3612 vlan
= qemu_find_vlan(vlan_id
);
3614 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
3617 if (!strcmp(device
, "nic")) {
3621 if (nb_nics
>= MAX_NICS
) {
3622 fprintf(stderr
, "Too Many NICs\n");
3625 nd
= &nd_table
[nb_nics
];
3626 macaddr
= nd
->macaddr
;
3632 macaddr
[5] = 0x56 + nb_nics
;
3634 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
3635 if (parse_macaddr(macaddr
, buf
) < 0) {
3636 fprintf(stderr
, "invalid syntax for ethernet address\n");
3640 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
3641 nd
->model
= strdup(buf
);
3647 if (!strcmp(device
, "none")) {
3648 /* does nothing. It is needed to signal that no network cards
3653 if (!strcmp(device
, "user")) {
3654 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
3655 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
3657 ret
= net_slirp_init(vlan
);
3661 if (!strcmp(device
, "tap")) {
3663 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
3664 fprintf(stderr
, "tap: no interface name\n");
3667 ret
= tap_win32_init(vlan
, ifname
);
3670 if (!strcmp(device
, "tap")) {
3672 char setup_script
[1024];
3674 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3675 fd
= strtol(buf
, NULL
, 0);
3677 if (net_tap_fd_init(vlan
, fd
))
3680 get_param_value(ifname
, sizeof(ifname
), "ifname", p
);
3681 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
3682 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
3684 ret
= net_tap_init(vlan
, ifname
, setup_script
);
3688 if (!strcmp(device
, "socket")) {
3689 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3691 fd
= strtol(buf
, NULL
, 0);
3693 if (net_socket_fd_init(vlan
, fd
, 1))
3695 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
3696 ret
= net_socket_listen_init(vlan
, buf
);
3697 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
3698 ret
= net_socket_connect_init(vlan
, buf
);
3699 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
3700 ret
= net_socket_mcast_init(vlan
, buf
);
3702 fprintf(stderr
, "Unknown socket options: %s\n", p
);
3707 fprintf(stderr
, "Unknown network device: %s\n", device
);
3711 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
3717 void do_info_network(void)
3720 VLANClientState
*vc
;
3722 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3723 term_printf("VLAN %d devices:\n", vlan
->id
);
3724 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3725 term_printf(" %s\n", vc
->info_str
);
3729 /***********************************************************/
3732 static USBPort
*used_usb_ports
;
3733 static USBPort
*free_usb_ports
;
3735 /* ??? Maybe change this to register a hub to keep track of the topology. */
3736 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
3737 usb_attachfn attach
)
3739 port
->opaque
= opaque
;
3740 port
->index
= index
;
3741 port
->attach
= attach
;
3742 port
->next
= free_usb_ports
;
3743 free_usb_ports
= port
;
3746 static int usb_device_add(const char *devname
)
3752 if (!free_usb_ports
)
3755 if (strstart(devname
, "host:", &p
)) {
3756 dev
= usb_host_device_open(p
);
3757 } else if (!strcmp(devname
, "mouse")) {
3758 dev
= usb_mouse_init();
3759 } else if (!strcmp(devname
, "tablet")) {
3760 dev
= usb_tablet_init();
3761 } else if (strstart(devname
, "disk:", &p
)) {
3762 dev
= usb_msd_init(p
);
3769 /* Find a USB port to add the device to. */
3770 port
= free_usb_ports
;
3774 /* Create a new hub and chain it on. */
3775 free_usb_ports
= NULL
;
3776 port
->next
= used_usb_ports
;
3777 used_usb_ports
= port
;
3779 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
3780 usb_attach(port
, hub
);
3781 port
= free_usb_ports
;
3784 free_usb_ports
= port
->next
;
3785 port
->next
= used_usb_ports
;
3786 used_usb_ports
= port
;
3787 usb_attach(port
, dev
);
3791 static int usb_device_del(const char *devname
)
3799 if (!used_usb_ports
)
3802 p
= strchr(devname
, '.');
3805 bus_num
= strtoul(devname
, NULL
, 0);
3806 addr
= strtoul(p
+ 1, NULL
, 0);
3810 lastp
= &used_usb_ports
;
3811 port
= used_usb_ports
;
3812 while (port
&& port
->dev
->addr
!= addr
) {
3813 lastp
= &port
->next
;
3821 *lastp
= port
->next
;
3822 usb_attach(port
, NULL
);
3823 dev
->handle_destroy(dev
);
3824 port
->next
= free_usb_ports
;
3825 free_usb_ports
= port
;
3829 void do_usb_add(const char *devname
)
3832 ret
= usb_device_add(devname
);
3834 term_printf("Could not add USB device '%s'\n", devname
);
3837 void do_usb_del(const char *devname
)
3840 ret
= usb_device_del(devname
);
3842 term_printf("Could not remove USB device '%s'\n", devname
);
3849 const char *speed_str
;
3852 term_printf("USB support not enabled\n");
3856 for (port
= used_usb_ports
; port
; port
= port
->next
) {
3860 switch(dev
->speed
) {
3864 case USB_SPEED_FULL
:
3867 case USB_SPEED_HIGH
:
3874 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
3875 0, dev
->addr
, speed_str
, dev
->devname
);
3879 /***********************************************************/
3882 static char *pid_filename
;
3884 /* Remove PID file. Called on normal exit */
3886 static void remove_pidfile(void)
3888 unlink (pid_filename
);
3891 static void create_pidfile(const char *filename
)
3893 struct stat pidstat
;
3896 /* Try to write our PID to the named file */
3897 if (stat(filename
, &pidstat
) < 0) {
3898 if (errno
== ENOENT
) {
3899 if ((f
= fopen (filename
, "w")) == NULL
) {
3900 perror("Opening pidfile");
3903 fprintf(f
, "%d\n", getpid());
3905 pid_filename
= qemu_strdup(filename
);
3906 if (!pid_filename
) {
3907 fprintf(stderr
, "Could not save PID filename");
3910 atexit(remove_pidfile
);
3913 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
3919 /***********************************************************/
3922 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
3926 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
3930 static void dumb_refresh(DisplayState
*ds
)
3935 void dumb_display_init(DisplayState
*ds
)
3940 ds
->dpy_update
= dumb_update
;
3941 ds
->dpy_resize
= dumb_resize
;
3942 ds
->dpy_refresh
= dumb_refresh
;
3945 /***********************************************************/
3948 #define MAX_IO_HANDLERS 64
3950 typedef struct IOHandlerRecord
{
3952 IOCanRWHandler
*fd_read_poll
;
3954 IOHandler
*fd_write
;
3956 /* temporary data */
3958 struct IOHandlerRecord
*next
;
3961 static IOHandlerRecord
*first_io_handler
;
3963 /* XXX: fd_read_poll should be suppressed, but an API change is
3964 necessary in the character devices to suppress fd_can_read(). */
3965 int qemu_set_fd_handler2(int fd
,
3966 IOCanRWHandler
*fd_read_poll
,
3968 IOHandler
*fd_write
,
3971 IOHandlerRecord
**pioh
, *ioh
;
3973 if (!fd_read
&& !fd_write
) {
3974 pioh
= &first_io_handler
;
3979 if (ioh
->fd
== fd
) {
3987 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3991 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
3994 ioh
->next
= first_io_handler
;
3995 first_io_handler
= ioh
;
3998 ioh
->fd_read_poll
= fd_read_poll
;
3999 ioh
->fd_read
= fd_read
;
4000 ioh
->fd_write
= fd_write
;
4001 ioh
->opaque
= opaque
;
4006 int qemu_set_fd_handler(int fd
,
4008 IOHandler
*fd_write
,
4011 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4014 /***********************************************************/
4015 /* Polling handling */
4017 typedef struct PollingEntry
{
4020 struct PollingEntry
*next
;
4023 static PollingEntry
*first_polling_entry
;
4025 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4027 PollingEntry
**ppe
, *pe
;
4028 pe
= qemu_mallocz(sizeof(PollingEntry
));
4032 pe
->opaque
= opaque
;
4033 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
4038 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
4040 PollingEntry
**ppe
, *pe
;
4041 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
4043 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
4052 /***********************************************************/
4053 /* Wait objects support */
4054 typedef struct WaitObjects
{
4056 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
4057 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
4058 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
4061 static WaitObjects wait_objects
= {0};
4063 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4065 WaitObjects
*w
= &wait_objects
;
4067 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
4069 w
->events
[w
->num
] = handle
;
4070 w
->func
[w
->num
] = func
;
4071 w
->opaque
[w
->num
] = opaque
;
4076 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4079 WaitObjects
*w
= &wait_objects
;
4082 for (i
= 0; i
< w
->num
; i
++) {
4083 if (w
->events
[i
] == handle
)
4086 w
->events
[i
] = w
->events
[i
+ 1];
4087 w
->func
[i
] = w
->func
[i
+ 1];
4088 w
->opaque
[i
] = w
->opaque
[i
+ 1];
4096 /***********************************************************/
4097 /* savevm/loadvm support */
4099 #define IO_BUF_SIZE 32768
4103 BlockDriverState
*bs
;
4106 int64_t base_offset
;
4107 int64_t buf_offset
; /* start of buffer when writing, end of buffer
4110 int buf_size
; /* 0 when writing */
4111 uint8_t buf
[IO_BUF_SIZE
];
4114 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
4118 f
= qemu_mallocz(sizeof(QEMUFile
));
4121 if (!strcmp(mode
, "wb")) {
4123 } else if (!strcmp(mode
, "rb")) {
4128 f
->outfile
= fopen(filename
, mode
);
4140 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
4144 f
= qemu_mallocz(sizeof(QEMUFile
));
4149 f
->is_writable
= is_writable
;
4150 f
->base_offset
= offset
;
4154 void qemu_fflush(QEMUFile
*f
)
4156 if (!f
->is_writable
)
4158 if (f
->buf_index
> 0) {
4160 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4161 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
4163 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4164 f
->buf
, f
->buf_index
);
4166 f
->buf_offset
+= f
->buf_index
;
4171 static void qemu_fill_buffer(QEMUFile
*f
)
4178 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4179 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
4183 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4184 f
->buf
, IO_BUF_SIZE
);
4190 f
->buf_offset
+= len
;
4193 void qemu_fclose(QEMUFile
*f
)
4203 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
4207 l
= IO_BUF_SIZE
- f
->buf_index
;
4210 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
4214 if (f
->buf_index
>= IO_BUF_SIZE
)
4219 void qemu_put_byte(QEMUFile
*f
, int v
)
4221 f
->buf
[f
->buf_index
++] = v
;
4222 if (f
->buf_index
>= IO_BUF_SIZE
)
4226 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
4232 l
= f
->buf_size
- f
->buf_index
;
4234 qemu_fill_buffer(f
);
4235 l
= f
->buf_size
- f
->buf_index
;
4241 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
4246 return size1
- size
;
4249 int qemu_get_byte(QEMUFile
*f
)
4251 if (f
->buf_index
>= f
->buf_size
) {
4252 qemu_fill_buffer(f
);
4253 if (f
->buf_index
>= f
->buf_size
)
4256 return f
->buf
[f
->buf_index
++];
4259 int64_t qemu_ftell(QEMUFile
*f
)
4261 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
4264 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
4266 if (whence
== SEEK_SET
) {
4268 } else if (whence
== SEEK_CUR
) {
4269 pos
+= qemu_ftell(f
);
4271 /* SEEK_END not supported */
4274 if (f
->is_writable
) {
4276 f
->buf_offset
= pos
;
4278 f
->buf_offset
= pos
;
4285 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
4287 qemu_put_byte(f
, v
>> 8);
4288 qemu_put_byte(f
, v
);
4291 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
4293 qemu_put_byte(f
, v
>> 24);
4294 qemu_put_byte(f
, v
>> 16);
4295 qemu_put_byte(f
, v
>> 8);
4296 qemu_put_byte(f
, v
);
4299 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
4301 qemu_put_be32(f
, v
>> 32);
4302 qemu_put_be32(f
, v
);
4305 unsigned int qemu_get_be16(QEMUFile
*f
)
4308 v
= qemu_get_byte(f
) << 8;
4309 v
|= qemu_get_byte(f
);
4313 unsigned int qemu_get_be32(QEMUFile
*f
)
4316 v
= qemu_get_byte(f
) << 24;
4317 v
|= qemu_get_byte(f
) << 16;
4318 v
|= qemu_get_byte(f
) << 8;
4319 v
|= qemu_get_byte(f
);
4323 uint64_t qemu_get_be64(QEMUFile
*f
)
4326 v
= (uint64_t)qemu_get_be32(f
) << 32;
4327 v
|= qemu_get_be32(f
);
4331 typedef struct SaveStateEntry
{
4335 SaveStateHandler
*save_state
;
4336 LoadStateHandler
*load_state
;
4338 struct SaveStateEntry
*next
;
4341 static SaveStateEntry
*first_se
;
4343 int register_savevm(const char *idstr
,
4346 SaveStateHandler
*save_state
,
4347 LoadStateHandler
*load_state
,
4350 SaveStateEntry
*se
, **pse
;
4352 se
= qemu_malloc(sizeof(SaveStateEntry
));
4355 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
4356 se
->instance_id
= instance_id
;
4357 se
->version_id
= version_id
;
4358 se
->save_state
= save_state
;
4359 se
->load_state
= load_state
;
4360 se
->opaque
= opaque
;
4363 /* add at the end of list */
4365 while (*pse
!= NULL
)
4366 pse
= &(*pse
)->next
;
4371 #define QEMU_VM_FILE_MAGIC 0x5145564d
4372 #define QEMU_VM_FILE_VERSION 0x00000002
4374 int qemu_savevm_state(QEMUFile
*f
)
4378 int64_t cur_pos
, len_pos
, total_len_pos
;
4380 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
4381 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
4382 total_len_pos
= qemu_ftell(f
);
4383 qemu_put_be64(f
, 0); /* total size */
4385 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4387 len
= strlen(se
->idstr
);
4388 qemu_put_byte(f
, len
);
4389 qemu_put_buffer(f
, se
->idstr
, len
);
4391 qemu_put_be32(f
, se
->instance_id
);
4392 qemu_put_be32(f
, se
->version_id
);
4394 /* record size: filled later */
4395 len_pos
= qemu_ftell(f
);
4396 qemu_put_be32(f
, 0);
4398 se
->save_state(f
, se
->opaque
);
4400 /* fill record size */
4401 cur_pos
= qemu_ftell(f
);
4402 len
= cur_pos
- len_pos
- 4;
4403 qemu_fseek(f
, len_pos
, SEEK_SET
);
4404 qemu_put_be32(f
, len
);
4405 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4407 cur_pos
= qemu_ftell(f
);
4408 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
4409 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
4410 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4416 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
4420 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4421 if (!strcmp(se
->idstr
, idstr
) &&
4422 instance_id
== se
->instance_id
)
4428 int qemu_loadvm_state(QEMUFile
*f
)
4431 int len
, ret
, instance_id
, record_len
, version_id
;
4432 int64_t total_len
, end_pos
, cur_pos
;
4436 v
= qemu_get_be32(f
);
4437 if (v
!= QEMU_VM_FILE_MAGIC
)
4439 v
= qemu_get_be32(f
);
4440 if (v
!= QEMU_VM_FILE_VERSION
) {
4445 total_len
= qemu_get_be64(f
);
4446 end_pos
= total_len
+ qemu_ftell(f
);
4448 if (qemu_ftell(f
) >= end_pos
)
4450 len
= qemu_get_byte(f
);
4451 qemu_get_buffer(f
, idstr
, len
);
4453 instance_id
= qemu_get_be32(f
);
4454 version_id
= qemu_get_be32(f
);
4455 record_len
= qemu_get_be32(f
);
4457 printf("idstr=%s instance=0x%x version=%d len=%d\n",
4458 idstr
, instance_id
, version_id
, record_len
);
4460 cur_pos
= qemu_ftell(f
);
4461 se
= find_se(idstr
, instance_id
);
4463 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
4464 instance_id
, idstr
);
4466 ret
= se
->load_state(f
, se
->opaque
, version_id
);
4468 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
4469 instance_id
, idstr
);
4472 /* always seek to exact end of record */
4473 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
4480 /* device can contain snapshots */
4481 static int bdrv_can_snapshot(BlockDriverState
*bs
)
4484 !bdrv_is_removable(bs
) &&
4485 !bdrv_is_read_only(bs
));
4488 /* device must be snapshots in order to have a reliable snapshot */
4489 static int bdrv_has_snapshot(BlockDriverState
*bs
)
4492 !bdrv_is_removable(bs
) &&
4493 !bdrv_is_read_only(bs
));
4496 static BlockDriverState
*get_bs_snapshots(void)
4498 BlockDriverState
*bs
;
4502 return bs_snapshots
;
4503 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4505 if (bdrv_can_snapshot(bs
))
4514 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
4517 QEMUSnapshotInfo
*sn_tab
, *sn
;
4521 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
4524 for(i
= 0; i
< nb_sns
; i
++) {
4526 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
4536 void do_savevm(const char *name
)
4538 BlockDriverState
*bs
, *bs1
;
4539 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
4540 int must_delete
, ret
, i
;
4541 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
4543 int saved_vm_running
;
4550 bs
= get_bs_snapshots();
4552 term_printf("No block device can accept snapshots\n");
4556 /* ??? Should this occur after vm_stop? */
4559 saved_vm_running
= vm_running
;
4564 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
4569 memset(sn
, 0, sizeof(*sn
));
4571 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
4572 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
4575 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
4578 /* fill auxiliary fields */
4581 sn
->date_sec
= tb
.time
;
4582 sn
->date_nsec
= tb
.millitm
* 1000000;
4584 gettimeofday(&tv
, NULL
);
4585 sn
->date_sec
= tv
.tv_sec
;
4586 sn
->date_nsec
= tv
.tv_usec
* 1000;
4588 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
4590 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
4591 term_printf("Device %s does not support VM state snapshots\n",
4592 bdrv_get_device_name(bs
));
4596 /* save the VM state */
4597 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
4599 term_printf("Could not open VM state file\n");
4602 ret
= qemu_savevm_state(f
);
4603 sn
->vm_state_size
= qemu_ftell(f
);
4606 term_printf("Error %d while writing VM\n", ret
);
4610 /* create the snapshots */
4612 for(i
= 0; i
< MAX_DISKS
; i
++) {
4614 if (bdrv_has_snapshot(bs1
)) {
4616 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
4618 term_printf("Error while deleting snapshot on '%s'\n",
4619 bdrv_get_device_name(bs1
));
4622 ret
= bdrv_snapshot_create(bs1
, sn
);
4624 term_printf("Error while creating snapshot on '%s'\n",
4625 bdrv_get_device_name(bs1
));
4631 if (saved_vm_running
)
4635 void do_loadvm(const char *name
)
4637 BlockDriverState
*bs
, *bs1
;
4638 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
4641 int saved_vm_running
;
4643 bs
= get_bs_snapshots();
4645 term_printf("No block device supports snapshots\n");
4649 /* Flush all IO requests so they don't interfere with the new state. */
4652 saved_vm_running
= vm_running
;
4655 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4657 if (bdrv_has_snapshot(bs1
)) {
4658 ret
= bdrv_snapshot_goto(bs1
, name
);
4661 term_printf("Warning: ");
4664 term_printf("Snapshots not supported on device '%s'\n",
4665 bdrv_get_device_name(bs1
));
4668 term_printf("Could not find snapshot '%s' on device '%s'\n",
4669 name
, bdrv_get_device_name(bs1
));
4672 term_printf("Error %d while activating snapshot on '%s'\n",
4673 ret
, bdrv_get_device_name(bs1
));
4676 /* fatal on snapshot block device */
4683 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
4684 term_printf("Device %s does not support VM state snapshots\n",
4685 bdrv_get_device_name(bs
));
4689 /* restore the VM state */
4690 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
4692 term_printf("Could not open VM state file\n");
4695 ret
= qemu_loadvm_state(f
);
4698 term_printf("Error %d while loading VM state\n", ret
);
4701 if (saved_vm_running
)
4705 void do_delvm(const char *name
)
4707 BlockDriverState
*bs
, *bs1
;
4710 bs
= get_bs_snapshots();
4712 term_printf("No block device supports snapshots\n");
4716 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4718 if (bdrv_has_snapshot(bs1
)) {
4719 ret
= bdrv_snapshot_delete(bs1
, name
);
4721 if (ret
== -ENOTSUP
)
4722 term_printf("Snapshots not supported on device '%s'\n",
4723 bdrv_get_device_name(bs1
));
4725 term_printf("Error %d while deleting snapshot on '%s'\n",
4726 ret
, bdrv_get_device_name(bs1
));
4732 void do_info_snapshots(void)
4734 BlockDriverState
*bs
, *bs1
;
4735 QEMUSnapshotInfo
*sn_tab
, *sn
;
4739 bs
= get_bs_snapshots();
4741 term_printf("No available block device supports snapshots\n");
4744 term_printf("Snapshot devices:");
4745 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4747 if (bdrv_has_snapshot(bs1
)) {
4749 term_printf(" %s", bdrv_get_device_name(bs1
));
4754 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
4756 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
4759 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
4760 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
4761 for(i
= 0; i
< nb_sns
; i
++) {
4763 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
4768 /***********************************************************/
4769 /* cpu save/restore */
4771 #if defined(TARGET_I386)
4773 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
4775 qemu_put_be32(f
, dt
->selector
);
4776 qemu_put_betl(f
, dt
->base
);
4777 qemu_put_be32(f
, dt
->limit
);
4778 qemu_put_be32(f
, dt
->flags
);
4781 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
4783 dt
->selector
= qemu_get_be32(f
);
4784 dt
->base
= qemu_get_betl(f
);
4785 dt
->limit
= qemu_get_be32(f
);
4786 dt
->flags
= qemu_get_be32(f
);
4789 void cpu_save(QEMUFile
*f
, void *opaque
)
4791 CPUState
*env
= opaque
;
4792 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
4796 for(i
= 0; i
< CPU_NB_REGS
; i
++)
4797 qemu_put_betls(f
, &env
->regs
[i
]);
4798 qemu_put_betls(f
, &env
->eip
);
4799 qemu_put_betls(f
, &env
->eflags
);
4800 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
4801 qemu_put_be32s(f
, &hflags
);
4805 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
4807 for(i
= 0; i
< 8; i
++) {
4808 fptag
|= ((!env
->fptags
[i
]) << i
);
4811 qemu_put_be16s(f
, &fpuc
);
4812 qemu_put_be16s(f
, &fpus
);
4813 qemu_put_be16s(f
, &fptag
);
4815 #ifdef USE_X86LDOUBLE
4820 qemu_put_be16s(f
, &fpregs_format
);
4822 for(i
= 0; i
< 8; i
++) {
4823 #ifdef USE_X86LDOUBLE
4827 /* we save the real CPU data (in case of MMX usage only 'mant'
4828 contains the MMX register */
4829 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
4830 qemu_put_be64(f
, mant
);
4831 qemu_put_be16(f
, exp
);
4834 /* if we use doubles for float emulation, we save the doubles to
4835 avoid losing information in case of MMX usage. It can give
4836 problems if the image is restored on a CPU where long
4837 doubles are used instead. */
4838 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
4842 for(i
= 0; i
< 6; i
++)
4843 cpu_put_seg(f
, &env
->segs
[i
]);
4844 cpu_put_seg(f
, &env
->ldt
);
4845 cpu_put_seg(f
, &env
->tr
);
4846 cpu_put_seg(f
, &env
->gdt
);
4847 cpu_put_seg(f
, &env
->idt
);
4849 qemu_put_be32s(f
, &env
->sysenter_cs
);
4850 qemu_put_be32s(f
, &env
->sysenter_esp
);
4851 qemu_put_be32s(f
, &env
->sysenter_eip
);
4853 qemu_put_betls(f
, &env
->cr
[0]);
4854 qemu_put_betls(f
, &env
->cr
[2]);
4855 qemu_put_betls(f
, &env
->cr
[3]);
4856 qemu_put_betls(f
, &env
->cr
[4]);
4858 for(i
= 0; i
< 8; i
++)
4859 qemu_put_betls(f
, &env
->dr
[i
]);
4862 qemu_put_be32s(f
, &env
->a20_mask
);
4865 qemu_put_be32s(f
, &env
->mxcsr
);
4866 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
4867 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
4868 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
4871 #ifdef TARGET_X86_64
4872 qemu_put_be64s(f
, &env
->efer
);
4873 qemu_put_be64s(f
, &env
->star
);
4874 qemu_put_be64s(f
, &env
->lstar
);
4875 qemu_put_be64s(f
, &env
->cstar
);
4876 qemu_put_be64s(f
, &env
->fmask
);
4877 qemu_put_be64s(f
, &env
->kernelgsbase
);
4881 #ifdef USE_X86LDOUBLE
4882 /* XXX: add that in a FPU generic layer */
4883 union x86_longdouble
{
4888 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
4889 #define EXPBIAS1 1023
4890 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
4891 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
4893 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
4897 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
4898 /* exponent + sign */
4899 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
4900 e
|= SIGND1(temp
) >> 16;
4905 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
4907 CPUState
*env
= opaque
;
4910 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
4912 if (version_id
!= 3)
4914 for(i
= 0; i
< CPU_NB_REGS
; i
++)
4915 qemu_get_betls(f
, &env
->regs
[i
]);
4916 qemu_get_betls(f
, &env
->eip
);
4917 qemu_get_betls(f
, &env
->eflags
);
4918 qemu_get_be32s(f
, &hflags
);
4920 qemu_get_be16s(f
, &fpuc
);
4921 qemu_get_be16s(f
, &fpus
);
4922 qemu_get_be16s(f
, &fptag
);
4923 qemu_get_be16s(f
, &fpregs_format
);
4925 /* NOTE: we cannot always restore the FPU state if the image come
4926 from a host with a different 'USE_X86LDOUBLE' define. We guess
4927 if we are in an MMX state to restore correctly in that case. */
4928 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
4929 for(i
= 0; i
< 8; i
++) {
4933 switch(fpregs_format
) {
4935 mant
= qemu_get_be64(f
);
4936 exp
= qemu_get_be16(f
);
4937 #ifdef USE_X86LDOUBLE
4938 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
4940 /* difficult case */
4942 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
4944 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
4948 mant
= qemu_get_be64(f
);
4949 #ifdef USE_X86LDOUBLE
4951 union x86_longdouble
*p
;
4952 /* difficult case */
4953 p
= (void *)&env
->fpregs
[i
];
4958 fp64_to_fp80(p
, mant
);
4962 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
4971 /* XXX: restore FPU round state */
4972 env
->fpstt
= (fpus
>> 11) & 7;
4973 env
->fpus
= fpus
& ~0x3800;
4975 for(i
= 0; i
< 8; i
++) {
4976 env
->fptags
[i
] = (fptag
>> i
) & 1;
4979 for(i
= 0; i
< 6; i
++)
4980 cpu_get_seg(f
, &env
->segs
[i
]);
4981 cpu_get_seg(f
, &env
->ldt
);
4982 cpu_get_seg(f
, &env
->tr
);
4983 cpu_get_seg(f
, &env
->gdt
);
4984 cpu_get_seg(f
, &env
->idt
);
4986 qemu_get_be32s(f
, &env
->sysenter_cs
);
4987 qemu_get_be32s(f
, &env
->sysenter_esp
);
4988 qemu_get_be32s(f
, &env
->sysenter_eip
);
4990 qemu_get_betls(f
, &env
->cr
[0]);
4991 qemu_get_betls(f
, &env
->cr
[2]);
4992 qemu_get_betls(f
, &env
->cr
[3]);
4993 qemu_get_betls(f
, &env
->cr
[4]);
4995 for(i
= 0; i
< 8; i
++)
4996 qemu_get_betls(f
, &env
->dr
[i
]);
4999 qemu_get_be32s(f
, &env
->a20_mask
);
5001 qemu_get_be32s(f
, &env
->mxcsr
);
5002 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5003 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5004 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5007 #ifdef TARGET_X86_64
5008 qemu_get_be64s(f
, &env
->efer
);
5009 qemu_get_be64s(f
, &env
->star
);
5010 qemu_get_be64s(f
, &env
->lstar
);
5011 qemu_get_be64s(f
, &env
->cstar
);
5012 qemu_get_be64s(f
, &env
->fmask
);
5013 qemu_get_be64s(f
, &env
->kernelgsbase
);
5016 /* XXX: compute hflags from scratch, except for CPL and IIF */
5017 env
->hflags
= hflags
;
5022 #elif defined(TARGET_PPC)
5023 void cpu_save(QEMUFile
*f
, void *opaque
)
5027 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5032 #elif defined(TARGET_MIPS)
5033 void cpu_save(QEMUFile
*f
, void *opaque
)
5037 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5042 #elif defined(TARGET_SPARC)
5043 void cpu_save(QEMUFile
*f
, void *opaque
)
5045 CPUState
*env
= opaque
;
5049 for(i
= 0; i
< 8; i
++)
5050 qemu_put_betls(f
, &env
->gregs
[i
]);
5051 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5052 qemu_put_betls(f
, &env
->regbase
[i
]);
5055 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5061 qemu_put_be32(f
, u
.i
);
5064 qemu_put_betls(f
, &env
->pc
);
5065 qemu_put_betls(f
, &env
->npc
);
5066 qemu_put_betls(f
, &env
->y
);
5068 qemu_put_be32(f
, tmp
);
5069 qemu_put_betls(f
, &env
->fsr
);
5070 qemu_put_betls(f
, &env
->tbr
);
5071 #ifndef TARGET_SPARC64
5072 qemu_put_be32s(f
, &env
->wim
);
5074 for(i
= 0; i
< 16; i
++)
5075 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
5079 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5081 CPUState
*env
= opaque
;
5085 for(i
= 0; i
< 8; i
++)
5086 qemu_get_betls(f
, &env
->gregs
[i
]);
5087 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5088 qemu_get_betls(f
, &env
->regbase
[i
]);
5091 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5096 u
.i
= qemu_get_be32(f
);
5100 qemu_get_betls(f
, &env
->pc
);
5101 qemu_get_betls(f
, &env
->npc
);
5102 qemu_get_betls(f
, &env
->y
);
5103 tmp
= qemu_get_be32(f
);
5104 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
5105 correctly updated */
5107 qemu_get_betls(f
, &env
->fsr
);
5108 qemu_get_betls(f
, &env
->tbr
);
5109 #ifndef TARGET_SPARC64
5110 qemu_get_be32s(f
, &env
->wim
);
5112 for(i
= 0; i
< 16; i
++)
5113 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
5119 #elif defined(TARGET_ARM)
5121 /* ??? Need to implement these. */
5122 void cpu_save(QEMUFile
*f
, void *opaque
)
5126 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5133 #warning No CPU save/restore functions
5137 /***********************************************************/
5138 /* ram save/restore */
5140 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
5144 v
= qemu_get_byte(f
);
5147 if (qemu_get_buffer(f
, buf
, len
) != len
)
5151 v
= qemu_get_byte(f
);
5152 memset(buf
, v
, len
);
5160 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
5164 if (qemu_get_be32(f
) != phys_ram_size
)
5166 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
5167 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
5174 #define BDRV_HASH_BLOCK_SIZE 1024
5175 #define IOBUF_SIZE 4096
5176 #define RAM_CBLOCK_MAGIC 0xfabe
5178 typedef struct RamCompressState
{
5181 uint8_t buf
[IOBUF_SIZE
];
5184 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
5187 memset(s
, 0, sizeof(*s
));
5189 ret
= deflateInit2(&s
->zstream
, 1,
5191 9, Z_DEFAULT_STRATEGY
);
5194 s
->zstream
.avail_out
= IOBUF_SIZE
;
5195 s
->zstream
.next_out
= s
->buf
;
5199 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
5201 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
5202 qemu_put_be16(s
->f
, len
);
5203 qemu_put_buffer(s
->f
, buf
, len
);
5206 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
5210 s
->zstream
.avail_in
= len
;
5211 s
->zstream
.next_in
= (uint8_t *)buf
;
5212 while (s
->zstream
.avail_in
> 0) {
5213 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
5216 if (s
->zstream
.avail_out
== 0) {
5217 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
5218 s
->zstream
.avail_out
= IOBUF_SIZE
;
5219 s
->zstream
.next_out
= s
->buf
;
5225 static void ram_compress_close(RamCompressState
*s
)
5229 /* compress last bytes */
5231 ret
= deflate(&s
->zstream
, Z_FINISH
);
5232 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
5233 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
5235 ram_put_cblock(s
, s
->buf
, len
);
5237 s
->zstream
.avail_out
= IOBUF_SIZE
;
5238 s
->zstream
.next_out
= s
->buf
;
5239 if (ret
== Z_STREAM_END
)
5246 deflateEnd(&s
->zstream
);
5249 typedef struct RamDecompressState
{
5252 uint8_t buf
[IOBUF_SIZE
];
5253 } RamDecompressState
;
5255 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
5258 memset(s
, 0, sizeof(*s
));
5260 ret
= inflateInit(&s
->zstream
);
5266 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
5270 s
->zstream
.avail_out
= len
;
5271 s
->zstream
.next_out
= buf
;
5272 while (s
->zstream
.avail_out
> 0) {
5273 if (s
->zstream
.avail_in
== 0) {
5274 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
5276 clen
= qemu_get_be16(s
->f
);
5277 if (clen
> IOBUF_SIZE
)
5279 qemu_get_buffer(s
->f
, s
->buf
, clen
);
5280 s
->zstream
.avail_in
= clen
;
5281 s
->zstream
.next_in
= s
->buf
;
5283 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
5284 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
5291 static void ram_decompress_close(RamDecompressState
*s
)
5293 inflateEnd(&s
->zstream
);
5296 static void ram_save(QEMUFile
*f
, void *opaque
)
5299 RamCompressState s1
, *s
= &s1
;
5302 qemu_put_be32(f
, phys_ram_size
);
5303 if (ram_compress_open(s
, f
) < 0)
5305 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5307 if (tight_savevm_enabled
) {
5311 /* find if the memory block is available on a virtual
5314 for(j
= 0; j
< MAX_DISKS
; j
++) {
5316 sector_num
= bdrv_hash_find(bs_table
[j
],
5317 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5318 if (sector_num
>= 0)
5323 goto normal_compress
;
5326 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
5327 ram_compress_buf(s
, buf
, 10);
5333 ram_compress_buf(s
, buf
, 1);
5334 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5337 ram_compress_close(s
);
5340 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
5342 RamDecompressState s1
, *s
= &s1
;
5346 if (version_id
== 1)
5347 return ram_load_v1(f
, opaque
);
5348 if (version_id
!= 2)
5350 if (qemu_get_be32(f
) != phys_ram_size
)
5352 if (ram_decompress_open(s
, f
) < 0)
5354 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5355 if (ram_decompress_buf(s
, buf
, 1) < 0) {
5356 fprintf(stderr
, "Error while reading ram block header\n");
5360 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
5361 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
5370 ram_decompress_buf(s
, buf
+ 1, 9);
5372 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
5373 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
5374 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
5377 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
5378 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
5379 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
5380 bs_index
, sector_num
);
5387 printf("Error block header\n");
5391 ram_decompress_close(s
);
5395 /***********************************************************/
5396 /* bottom halves (can be seen as timers which expire ASAP) */
5405 static QEMUBH
*first_bh
= NULL
;
5407 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
5410 bh
= qemu_mallocz(sizeof(QEMUBH
));
5414 bh
->opaque
= opaque
;
5418 int qemu_bh_poll(void)
5437 void qemu_bh_schedule(QEMUBH
*bh
)
5439 CPUState
*env
= cpu_single_env
;
5443 bh
->next
= first_bh
;
5446 /* stop the currently executing CPU to execute the BH ASAP */
5448 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
5452 void qemu_bh_cancel(QEMUBH
*bh
)
5455 if (bh
->scheduled
) {
5458 pbh
= &(*pbh
)->next
;
5464 void qemu_bh_delete(QEMUBH
*bh
)
5470 /***********************************************************/
5471 /* machine registration */
5473 QEMUMachine
*first_machine
= NULL
;
5475 int qemu_register_machine(QEMUMachine
*m
)
5478 pm
= &first_machine
;
5486 QEMUMachine
*find_machine(const char *name
)
5490 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5491 if (!strcmp(m
->name
, name
))
5497 /***********************************************************/
5498 /* main execution loop */
5500 void gui_update(void *opaque
)
5502 display_state
.dpy_refresh(&display_state
);
5503 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
5506 struct vm_change_state_entry
{
5507 VMChangeStateHandler
*cb
;
5509 LIST_ENTRY (vm_change_state_entry
) entries
;
5512 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
5514 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
5517 VMChangeStateEntry
*e
;
5519 e
= qemu_mallocz(sizeof (*e
));
5525 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
5529 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
5531 LIST_REMOVE (e
, entries
);
5535 static void vm_state_notify(int running
)
5537 VMChangeStateEntry
*e
;
5539 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
5540 e
->cb(e
->opaque
, running
);
5544 /* XXX: support several handlers */
5545 static VMStopHandler
*vm_stop_cb
;
5546 static void *vm_stop_opaque
;
5548 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
5551 vm_stop_opaque
= opaque
;
5555 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
5569 void vm_stop(int reason
)
5572 cpu_disable_ticks();
5576 vm_stop_cb(vm_stop_opaque
, reason
);
5583 /* reset/shutdown handler */
5585 typedef struct QEMUResetEntry
{
5586 QEMUResetHandler
*func
;
5588 struct QEMUResetEntry
*next
;
5591 static QEMUResetEntry
*first_reset_entry
;
5592 static int reset_requested
;
5593 static int shutdown_requested
;
5594 static int powerdown_requested
;
5596 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
5598 QEMUResetEntry
**pre
, *re
;
5600 pre
= &first_reset_entry
;
5601 while (*pre
!= NULL
)
5602 pre
= &(*pre
)->next
;
5603 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
5605 re
->opaque
= opaque
;
5610 void qemu_system_reset(void)
5614 /* reset all devices */
5615 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
5616 re
->func(re
->opaque
);
5620 void qemu_system_reset_request(void)
5622 reset_requested
= 1;
5624 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5627 void qemu_system_shutdown_request(void)
5629 shutdown_requested
= 1;
5631 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5634 void qemu_system_powerdown_request(void)
5636 powerdown_requested
= 1;
5638 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5641 void main_loop_wait(int timeout
)
5643 IOHandlerRecord
*ioh
, *ioh_next
;
5644 fd_set rfds
, wfds
, xfds
;
5650 /* XXX: need to suppress polling by better using win32 events */
5652 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
5653 ret
|= pe
->func(pe
->opaque
);
5656 if (ret
== 0 && timeout
> 0) {
5658 WaitObjects
*w
= &wait_objects
;
5660 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
5661 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
5662 if (w
->func
[ret
- WAIT_OBJECT_0
])
5663 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
5664 } else if (ret
== WAIT_TIMEOUT
) {
5666 err
= GetLastError();
5667 fprintf(stderr
, "Wait error %d %d\n", ret
, err
);
5671 /* poll any events */
5672 /* XXX: separate device handlers from system ones */
5677 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
5679 (!ioh
->fd_read_poll
||
5680 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
5681 FD_SET(ioh
->fd
, &rfds
);
5685 if (ioh
->fd_write
) {
5686 FD_SET(ioh
->fd
, &wfds
);
5696 tv
.tv_usec
= timeout
* 1000;
5698 #if defined(CONFIG_SLIRP)
5700 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
5703 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
5705 /* XXX: better handling of removal */
5706 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
5707 ioh_next
= ioh
->next
;
5708 if (FD_ISSET(ioh
->fd
, &rfds
)) {
5709 ioh
->fd_read(ioh
->opaque
);
5711 if (FD_ISSET(ioh
->fd
, &wfds
)) {
5712 ioh
->fd_write(ioh
->opaque
);
5716 #if defined(CONFIG_SLIRP)
5723 slirp_select_poll(&rfds
, &wfds
, &xfds
);
5730 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
5731 qemu_get_clock(vm_clock
));
5732 /* run dma transfers, if any */
5736 /* real time timers */
5737 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
5738 qemu_get_clock(rt_clock
));
5741 static CPUState
*cur_cpu
;
5746 #ifdef CONFIG_PROFILER
5751 cur_cpu
= first_cpu
;
5758 env
= env
->next_cpu
;
5761 #ifdef CONFIG_PROFILER
5762 ti
= profile_getclock();
5764 ret
= cpu_exec(env
);
5765 #ifdef CONFIG_PROFILER
5766 qemu_time
+= profile_getclock() - ti
;
5768 if (ret
!= EXCP_HALTED
)
5770 /* all CPUs are halted ? */
5771 if (env
== cur_cpu
) {
5778 if (shutdown_requested
) {
5779 ret
= EXCP_INTERRUPT
;
5782 if (reset_requested
) {
5783 reset_requested
= 0;
5784 qemu_system_reset();
5785 ret
= EXCP_INTERRUPT
;
5787 if (powerdown_requested
) {
5788 powerdown_requested
= 0;
5789 qemu_system_powerdown();
5790 ret
= EXCP_INTERRUPT
;
5792 if (ret
== EXCP_DEBUG
) {
5793 vm_stop(EXCP_DEBUG
);
5795 /* if hlt instruction, we wait until the next IRQ */
5796 /* XXX: use timeout computed from timers */
5797 if (ret
== EXCP_HLT
)
5804 #ifdef CONFIG_PROFILER
5805 ti
= profile_getclock();
5807 main_loop_wait(timeout
);
5808 #ifdef CONFIG_PROFILER
5809 dev_time
+= profile_getclock() - ti
;
5812 cpu_disable_ticks();
5818 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2006 Fabrice Bellard\n"
5819 "usage: %s [options] [disk_image]\n"
5821 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
5823 "Standard options:\n"
5824 "-M machine select emulated machine (-M ? for list)\n"
5825 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
5826 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
5827 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
5828 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
5829 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
5830 "-snapshot write to temporary files instead of disk image files\n"
5832 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
5834 "-m megs set virtual RAM size to megs MB [default=%d]\n"
5835 "-smp n set the number of CPUs to 'n' [default=1]\n"
5836 "-nographic disable graphical output and redirect serial I/Os to console\n"
5838 "-k language use keyboard layout (for example \"fr\" for French)\n"
5841 "-audio-help print list of audio drivers and their options\n"
5842 "-soundhw c1,... enable audio support\n"
5843 " and only specified sound cards (comma separated list)\n"
5844 " use -soundhw ? to get the list of supported cards\n"
5845 " use -soundhw all to enable all of them\n"
5847 "-localtime set the real time clock to local time [default=utc]\n"
5848 "-full-screen start in full screen\n"
5850 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
5852 "-usb enable the USB driver (will be the default soon)\n"
5853 "-usbdevice name add the host or guest USB device 'name'\n"
5854 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5855 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
5858 "Network options:\n"
5859 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
5860 " create a new Network Interface Card and connect it to VLAN 'n'\n"
5862 "-net user[,vlan=n][,hostname=host]\n"
5863 " connect the user mode network stack to VLAN 'n' and send\n"
5864 " hostname 'host' to DHCP clients\n"
5867 "-net tap[,vlan=n],ifname=name\n"
5868 " connect the host TAP network interface to VLAN 'n'\n"
5870 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
5871 " connect the host TAP network interface to VLAN 'n' and use\n"
5872 " the network script 'file' (default=%s);\n"
5873 " use 'fd=h' to connect to an already opened TAP interface\n"
5875 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
5876 " connect the vlan 'n' to another VLAN using a socket connection\n"
5877 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
5878 " connect the vlan 'n' to multicast maddr and port\n"
5879 "-net none use it alone to have zero network devices; if no -net option\n"
5880 " is provided, the default is '-net nic -net user'\n"
5883 "-tftp prefix allow tftp access to files starting with prefix [-net user]\n"
5885 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
5887 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
5888 " redirect TCP or UDP connections from host to guest [-net user]\n"
5891 "Linux boot specific:\n"
5892 "-kernel bzImage use 'bzImage' as kernel image\n"
5893 "-append cmdline use 'cmdline' as kernel command line\n"
5894 "-initrd file use 'file' as initial ram disk\n"
5896 "Debug/Expert options:\n"
5897 "-monitor dev redirect the monitor to char device 'dev'\n"
5898 "-serial dev redirect the serial port to char device 'dev'\n"
5899 "-parallel dev redirect the parallel port to char device 'dev'\n"
5900 "-pidfile file Write PID to 'file'\n"
5901 "-S freeze CPU at startup (use 'c' to start execution)\n"
5902 "-s wait gdb connection to port %d\n"
5903 "-p port change gdb connection port\n"
5904 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
5905 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
5906 " translation (t=none or lba) (usually qemu can guess them)\n"
5907 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
5909 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
5910 "-no-kqemu disable KQEMU kernel module usage\n"
5912 #ifdef USE_CODE_COPY
5913 "-no-code-copy disable code copy acceleration\n"
5916 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
5917 " (default is CL-GD5446 PCI VGA)\n"
5918 "-no-acpi disable ACPI\n"
5920 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
5921 "-vnc display start a VNC server on display\n"
5923 "During emulation, the following keys are useful:\n"
5924 "ctrl-alt-f toggle full screen\n"
5925 "ctrl-alt-n switch to virtual console 'n'\n"
5926 "ctrl-alt toggle mouse and keyboard grab\n"
5928 "When using -nographic, press 'ctrl-a h' to get some help.\n"
5933 DEFAULT_NETWORK_SCRIPT
,
5935 DEFAULT_GDBSTUB_PORT
,
5940 #define HAS_ARG 0x0001
5954 QEMU_OPTION_snapshot
,
5956 QEMU_OPTION_no_fd_bootchk
,
5959 QEMU_OPTION_nographic
,
5961 QEMU_OPTION_audio_help
,
5962 QEMU_OPTION_soundhw
,
5980 QEMU_OPTION_no_code_copy
,
5982 QEMU_OPTION_localtime
,
5983 QEMU_OPTION_cirrusvga
,
5985 QEMU_OPTION_std_vga
,
5986 QEMU_OPTION_monitor
,
5988 QEMU_OPTION_parallel
,
5990 QEMU_OPTION_full_screen
,
5991 QEMU_OPTION_pidfile
,
5992 QEMU_OPTION_no_kqemu
,
5993 QEMU_OPTION_kernel_kqemu
,
5994 QEMU_OPTION_win2k_hack
,
5996 QEMU_OPTION_usbdevice
,
5999 QEMU_OPTION_no_acpi
,
6002 typedef struct QEMUOption
{
6008 const QEMUOption qemu_options
[] = {
6009 { "h", 0, QEMU_OPTION_h
},
6011 { "M", HAS_ARG
, QEMU_OPTION_M
},
6012 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
6013 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
6014 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
6015 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
6016 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
6017 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
6018 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
6019 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
6020 { "snapshot", 0, QEMU_OPTION_snapshot
},
6022 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
6024 { "m", HAS_ARG
, QEMU_OPTION_m
},
6025 { "nographic", 0, QEMU_OPTION_nographic
},
6026 { "k", HAS_ARG
, QEMU_OPTION_k
},
6028 { "audio-help", 0, QEMU_OPTION_audio_help
},
6029 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
6032 { "net", HAS_ARG
, QEMU_OPTION_net
},
6034 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
6036 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
6038 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
6041 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
6042 { "append", HAS_ARG
, QEMU_OPTION_append
},
6043 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
6045 { "S", 0, QEMU_OPTION_S
},
6046 { "s", 0, QEMU_OPTION_s
},
6047 { "p", HAS_ARG
, QEMU_OPTION_p
},
6048 { "d", HAS_ARG
, QEMU_OPTION_d
},
6049 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
6050 { "L", HAS_ARG
, QEMU_OPTION_L
},
6051 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
6053 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
6054 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
6056 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6057 { "g", 1, QEMU_OPTION_g
},
6059 { "localtime", 0, QEMU_OPTION_localtime
},
6060 { "std-vga", 0, QEMU_OPTION_std_vga
},
6061 { "monitor", 1, QEMU_OPTION_monitor
},
6062 { "serial", 1, QEMU_OPTION_serial
},
6063 { "parallel", 1, QEMU_OPTION_parallel
},
6064 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
6065 { "full-screen", 0, QEMU_OPTION_full_screen
},
6066 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
6067 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
6068 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
6069 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
6070 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
6072 /* temporary options */
6073 { "usb", 0, QEMU_OPTION_usb
},
6074 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
6075 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
6079 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
6081 /* this stack is only used during signal handling */
6082 #define SIGNAL_STACK_SIZE 32768
6084 static uint8_t *signal_stack
;
6088 /* password input */
6090 static BlockDriverState
*get_bdrv(int index
)
6092 BlockDriverState
*bs
;
6095 bs
= bs_table
[index
];
6096 } else if (index
< 6) {
6097 bs
= fd_table
[index
- 4];
6104 static void read_passwords(void)
6106 BlockDriverState
*bs
;
6110 for(i
= 0; i
< 6; i
++) {
6112 if (bs
&& bdrv_is_encrypted(bs
)) {
6113 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
6114 for(j
= 0; j
< 3; j
++) {
6115 monitor_readline("Password: ",
6116 1, password
, sizeof(password
));
6117 if (bdrv_set_key(bs
, password
) == 0)
6119 term_printf("invalid password\n");
6125 /* XXX: currently we cannot use simultaneously different CPUs */
6126 void register_machines(void)
6128 #if defined(TARGET_I386)
6129 qemu_register_machine(&pc_machine
);
6130 qemu_register_machine(&isapc_machine
);
6131 #elif defined(TARGET_PPC)
6132 qemu_register_machine(&heathrow_machine
);
6133 qemu_register_machine(&core99_machine
);
6134 qemu_register_machine(&prep_machine
);
6135 #elif defined(TARGET_MIPS)
6136 qemu_register_machine(&mips_machine
);
6137 #elif defined(TARGET_SPARC)
6138 #ifdef TARGET_SPARC64
6139 qemu_register_machine(&sun4u_machine
);
6141 qemu_register_machine(&sun4m_machine
);
6143 #elif defined(TARGET_ARM)
6144 qemu_register_machine(&integratorcp926_machine
);
6145 qemu_register_machine(&integratorcp1026_machine
);
6146 qemu_register_machine(&versatilepb_machine
);
6147 qemu_register_machine(&versatileab_machine
);
6148 #elif defined(TARGET_SH4)
6149 qemu_register_machine(&shix_machine
);
6151 #error unsupported CPU
6156 struct soundhw soundhw
[] = {
6163 { .init_isa
= pcspk_audio_init
}
6168 "Creative Sound Blaster 16",
6171 { .init_isa
= SB16_init
}
6178 "Yamaha YMF262 (OPL3)",
6180 "Yamaha YM3812 (OPL2)",
6184 { .init_isa
= Adlib_init
}
6191 "Gravis Ultrasound GF1",
6194 { .init_isa
= GUS_init
}
6200 "ENSONIQ AudioPCI ES1370",
6203 { .init_pci
= es1370_init
}
6206 { NULL
, NULL
, 0, 0, { NULL
} }
6209 static void select_soundhw (const char *optarg
)
6213 if (*optarg
== '?') {
6216 printf ("Valid sound card names (comma separated):\n");
6217 for (c
= soundhw
; c
->name
; ++c
) {
6218 printf ("%-11s %s\n", c
->name
, c
->descr
);
6220 printf ("\n-soundhw all will enable all of the above\n");
6221 exit (*optarg
!= '?');
6229 if (!strcmp (optarg
, "all")) {
6230 for (c
= soundhw
; c
->name
; ++c
) {
6238 e
= strchr (p
, ',');
6239 l
= !e
? strlen (p
) : (size_t) (e
- p
);
6241 for (c
= soundhw
; c
->name
; ++c
) {
6242 if (!strncmp (c
->name
, p
, l
)) {
6251 "Unknown sound card name (too big to show)\n");
6254 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
6259 p
+= l
+ (e
!= NULL
);
6263 goto show_valid_cards
;
6269 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
6271 exit(STATUS_CONTROL_C_EXIT
);
6276 #define MAX_NET_CLIENTS 32
6278 int main(int argc
, char **argv
)
6280 #ifdef CONFIG_GDBSTUB
6281 int use_gdbstub
, gdbstub_port
;
6284 int snapshot
, linux_boot
;
6285 const char *initrd_filename
;
6286 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
6287 const char *kernel_filename
, *kernel_cmdline
;
6288 DisplayState
*ds
= &display_state
;
6289 int cyls
, heads
, secs
, translation
;
6290 int start_emulation
= 1;
6291 char net_clients
[MAX_NET_CLIENTS
][256];
6294 const char *r
, *optarg
;
6295 CharDriverState
*monitor_hd
;
6296 char monitor_device
[128];
6297 char serial_devices
[MAX_SERIAL_PORTS
][128];
6298 int serial_device_index
;
6299 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
6300 int parallel_device_index
;
6301 const char *loadvm
= NULL
;
6302 QEMUMachine
*machine
;
6303 char usb_devices
[MAX_USB_CMDLINE
][128];
6304 int usb_devices_index
;
6306 LIST_INIT (&vm_change_state_head
);
6309 struct sigaction act
;
6310 sigfillset(&act
.sa_mask
);
6312 act
.sa_handler
= SIG_IGN
;
6313 sigaction(SIGPIPE
, &act
, NULL
);
6316 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
6317 /* Note: cpu_interrupt() is currently not SMP safe, so we force
6318 QEMU to run on a single CPU */
6323 h
= GetCurrentProcess();
6324 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
6325 for(i
= 0; i
< 32; i
++) {
6326 if (mask
& (1 << i
))
6331 SetProcessAffinityMask(h
, mask
);
6337 register_machines();
6338 machine
= first_machine
;
6339 initrd_filename
= NULL
;
6340 for(i
= 0; i
< MAX_FD
; i
++)
6341 fd_filename
[i
] = NULL
;
6342 for(i
= 0; i
< MAX_DISKS
; i
++)
6343 hd_filename
[i
] = NULL
;
6344 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
6345 vga_ram_size
= VGA_RAM_SIZE
;
6346 bios_size
= BIOS_SIZE
;
6347 #ifdef CONFIG_GDBSTUB
6349 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
6353 kernel_filename
= NULL
;
6354 kernel_cmdline
= "";
6360 cyls
= heads
= secs
= 0;
6361 translation
= BIOS_ATA_TRANSLATION_AUTO
;
6362 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
6364 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
6365 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
6366 serial_devices
[i
][0] = '\0';
6367 serial_device_index
= 0;
6369 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
6370 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
6371 parallel_devices
[i
][0] = '\0';
6372 parallel_device_index
= 0;
6374 usb_devices_index
= 0;
6379 /* default mac address of the first network interface */
6387 hd_filename
[0] = argv
[optind
++];
6389 const QEMUOption
*popt
;
6392 popt
= qemu_options
;
6395 fprintf(stderr
, "%s: invalid option -- '%s'\n",
6399 if (!strcmp(popt
->name
, r
+ 1))
6403 if (popt
->flags
& HAS_ARG
) {
6404 if (optind
>= argc
) {
6405 fprintf(stderr
, "%s: option '%s' requires an argument\n",
6409 optarg
= argv
[optind
++];
6414 switch(popt
->index
) {
6416 machine
= find_machine(optarg
);
6419 printf("Supported machines are:\n");
6420 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6421 printf("%-10s %s%s\n",
6423 m
== first_machine
? " (default)" : "");
6428 case QEMU_OPTION_initrd
:
6429 initrd_filename
= optarg
;
6431 case QEMU_OPTION_hda
:
6432 case QEMU_OPTION_hdb
:
6433 case QEMU_OPTION_hdc
:
6434 case QEMU_OPTION_hdd
:
6437 hd_index
= popt
->index
- QEMU_OPTION_hda
;
6438 hd_filename
[hd_index
] = optarg
;
6439 if (hd_index
== cdrom_index
)
6443 case QEMU_OPTION_snapshot
:
6446 case QEMU_OPTION_hdachs
:
6450 cyls
= strtol(p
, (char **)&p
, 0);
6451 if (cyls
< 1 || cyls
> 16383)
6456 heads
= strtol(p
, (char **)&p
, 0);
6457 if (heads
< 1 || heads
> 16)
6462 secs
= strtol(p
, (char **)&p
, 0);
6463 if (secs
< 1 || secs
> 63)
6467 if (!strcmp(p
, "none"))
6468 translation
= BIOS_ATA_TRANSLATION_NONE
;
6469 else if (!strcmp(p
, "lba"))
6470 translation
= BIOS_ATA_TRANSLATION_LBA
;
6471 else if (!strcmp(p
, "auto"))
6472 translation
= BIOS_ATA_TRANSLATION_AUTO
;
6475 } else if (*p
!= '\0') {
6477 fprintf(stderr
, "qemu: invalid physical CHS format\n");
6482 case QEMU_OPTION_nographic
:
6483 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
6484 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
6487 case QEMU_OPTION_kernel
:
6488 kernel_filename
= optarg
;
6490 case QEMU_OPTION_append
:
6491 kernel_cmdline
= optarg
;
6493 case QEMU_OPTION_cdrom
:
6494 if (cdrom_index
>= 0) {
6495 hd_filename
[cdrom_index
] = optarg
;
6498 case QEMU_OPTION_boot
:
6499 boot_device
= optarg
[0];
6500 if (boot_device
!= 'a' &&
6503 boot_device
!= 'n' &&
6505 boot_device
!= 'c' && boot_device
!= 'd') {
6506 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
6510 case QEMU_OPTION_fda
:
6511 fd_filename
[0] = optarg
;
6513 case QEMU_OPTION_fdb
:
6514 fd_filename
[1] = optarg
;
6517 case QEMU_OPTION_no_fd_bootchk
:
6521 case QEMU_OPTION_no_code_copy
:
6522 code_copy_enabled
= 0;
6524 case QEMU_OPTION_net
:
6525 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
6526 fprintf(stderr
, "qemu: too many network clients\n");
6529 pstrcpy(net_clients
[nb_net_clients
],
6530 sizeof(net_clients
[0]),
6535 case QEMU_OPTION_tftp
:
6536 tftp_prefix
= optarg
;
6539 case QEMU_OPTION_smb
:
6540 net_slirp_smb(optarg
);
6543 case QEMU_OPTION_redir
:
6544 net_slirp_redir(optarg
);
6548 case QEMU_OPTION_audio_help
:
6552 case QEMU_OPTION_soundhw
:
6553 select_soundhw (optarg
);
6560 ram_size
= atoi(optarg
) * 1024 * 1024;
6563 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
6564 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
6565 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
6574 mask
= cpu_str_to_log_mask(optarg
);
6576 printf("Log items (comma separated):\n");
6577 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
6578 printf("%-10s %s\n", item
->name
, item
->help
);
6585 #ifdef CONFIG_GDBSTUB
6590 gdbstub_port
= atoi(optarg
);
6597 start_emulation
= 0;
6600 keyboard_layout
= optarg
;
6602 case QEMU_OPTION_localtime
:
6605 case QEMU_OPTION_cirrusvga
:
6606 cirrus_vga_enabled
= 1;
6608 case QEMU_OPTION_std_vga
:
6609 cirrus_vga_enabled
= 0;
6616 w
= strtol(p
, (char **)&p
, 10);
6619 fprintf(stderr
, "qemu: invalid resolution or depth\n");
6625 h
= strtol(p
, (char **)&p
, 10);
6630 depth
= strtol(p
, (char **)&p
, 10);
6631 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
6632 depth
!= 24 && depth
!= 32)
6634 } else if (*p
== '\0') {
6635 depth
= graphic_depth
;
6642 graphic_depth
= depth
;
6645 case QEMU_OPTION_monitor
:
6646 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
6648 case QEMU_OPTION_serial
:
6649 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
6650 fprintf(stderr
, "qemu: too many serial ports\n");
6653 pstrcpy(serial_devices
[serial_device_index
],
6654 sizeof(serial_devices
[0]), optarg
);
6655 serial_device_index
++;
6657 case QEMU_OPTION_parallel
:
6658 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
6659 fprintf(stderr
, "qemu: too many parallel ports\n");
6662 pstrcpy(parallel_devices
[parallel_device_index
],
6663 sizeof(parallel_devices
[0]), optarg
);
6664 parallel_device_index
++;
6666 case QEMU_OPTION_loadvm
:
6669 case QEMU_OPTION_full_screen
:
6672 case QEMU_OPTION_pidfile
:
6673 create_pidfile(optarg
);
6676 case QEMU_OPTION_win2k_hack
:
6677 win2k_install_hack
= 1;
6681 case QEMU_OPTION_no_kqemu
:
6684 case QEMU_OPTION_kernel_kqemu
:
6688 case QEMU_OPTION_usb
:
6691 case QEMU_OPTION_usbdevice
:
6693 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
6694 fprintf(stderr
, "Too many USB devices\n");
6697 pstrcpy(usb_devices
[usb_devices_index
],
6698 sizeof(usb_devices
[usb_devices_index
]),
6700 usb_devices_index
++;
6702 case QEMU_OPTION_smp
:
6703 smp_cpus
= atoi(optarg
);
6704 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
6705 fprintf(stderr
, "Invalid number of CPUs\n");
6709 case QEMU_OPTION_vnc
:
6710 vnc_display
= atoi(optarg
);
6711 if (vnc_display
< 0) {
6712 fprintf(stderr
, "Invalid VNC display\n");
6716 case QEMU_OPTION_no_acpi
:
6727 linux_boot
= (kernel_filename
!= NULL
);
6730 hd_filename
[0] == '\0' &&
6731 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
6732 fd_filename
[0] == '\0')
6735 /* boot to cd by default if no hard disk */
6736 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
6737 if (fd_filename
[0] != '\0')
6743 setvbuf(stdout
, NULL
, _IOLBF
, 0);
6753 /* init network clients */
6754 if (nb_net_clients
== 0) {
6755 /* if no clients, we use a default config */
6756 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
6758 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
6763 for(i
= 0;i
< nb_net_clients
; i
++) {
6764 if (net_client_init(net_clients
[i
]) < 0)
6768 /* init the memory */
6769 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
6771 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
6772 if (!phys_ram_base
) {
6773 fprintf(stderr
, "Could not allocate physical memory\n");
6777 /* we always create the cdrom drive, even if no disk is there */
6779 if (cdrom_index
>= 0) {
6780 bs_table
[cdrom_index
] = bdrv_new("cdrom");
6781 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
6784 /* open the virtual block devices */
6785 for(i
= 0; i
< MAX_DISKS
; i
++) {
6786 if (hd_filename
[i
]) {
6789 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
6790 bs_table
[i
] = bdrv_new(buf
);
6792 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
6793 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
6797 if (i
== 0 && cyls
!= 0) {
6798 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
6799 bdrv_set_translation_hint(bs_table
[i
], translation
);
6804 /* we always create at least one floppy disk */
6805 fd_table
[0] = bdrv_new("fda");
6806 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
6808 for(i
= 0; i
< MAX_FD
; i
++) {
6809 if (fd_filename
[i
]) {
6812 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
6813 fd_table
[i
] = bdrv_new(buf
);
6814 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
6816 if (fd_filename
[i
] != '\0') {
6817 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
6818 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
6819 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
6827 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
6828 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
6834 dumb_display_init(ds
);
6835 } else if (vnc_display
!= -1) {
6836 vnc_display_init(ds
, vnc_display
);
6838 #if defined(CONFIG_SDL)
6839 sdl_display_init(ds
, full_screen
);
6840 #elif defined(CONFIG_COCOA)
6841 cocoa_display_init(ds
, full_screen
);
6843 dumb_display_init(ds
);
6847 monitor_hd
= qemu_chr_open(monitor_device
);
6849 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
6852 monitor_init(monitor_hd
, !nographic
);
6854 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6855 const char *devname
= serial_devices
[i
];
6856 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
6857 serial_hds
[i
] = qemu_chr_open(devname
);
6858 if (!serial_hds
[i
]) {
6859 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
6863 if (!strcmp(devname
, "vc"))
6864 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6868 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6869 const char *devname
= parallel_devices
[i
];
6870 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
6871 parallel_hds
[i
] = qemu_chr_open(devname
);
6872 if (!parallel_hds
[i
]) {
6873 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
6877 if (!strcmp(devname
, "vc"))
6878 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6882 machine
->init(ram_size
, vga_ram_size
, boot_device
,
6883 ds
, fd_filename
, snapshot
,
6884 kernel_filename
, kernel_cmdline
, initrd_filename
);
6886 /* init USB devices */
6888 for(i
= 0; i
< usb_devices_index
; i
++) {
6889 if (usb_device_add(usb_devices
[i
]) < 0) {
6890 fprintf(stderr
, "Warning: could not add USB device %s\n",
6896 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
6897 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
6899 #ifdef CONFIG_GDBSTUB
6901 if (gdbserver_start(gdbstub_port
) < 0) {
6902 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
6906 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
6914 /* XXX: simplify init */
6916 if (start_emulation
) {