4 * Copyright (c) 2003-2007 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>
58 #include <linux/parport.h>
61 #include <sys/ethernet.h>
62 #include <sys/sockio.h>
63 #include <arpa/inet.h>
64 #include <netinet/arp.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/ip.h>
68 #include <netinet/ip_icmp.h> // must come after ip.h
69 #include <netinet/udp.h>
70 #include <netinet/tcp.h>
78 #if defined(CONFIG_SLIRP)
84 #include <sys/timeb.h>
86 #define getopt_long_only getopt_long
87 #define memalign(align, size) malloc(size)
90 #include "qemu_socket.h"
96 #endif /* CONFIG_SDL */
100 #define main qemu_main
101 #endif /* CONFIG_COCOA */
105 #include "exec-all.h"
107 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
109 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
111 #define SMBD_COMMAND "/usr/sbin/smbd"
114 //#define DEBUG_UNUSED_IOPORT
115 //#define DEBUG_IOPORT
117 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
120 #define DEFAULT_RAM_SIZE 144
122 #define DEFAULT_RAM_SIZE 128
125 #define GUI_REFRESH_INTERVAL 30
127 /* Max number of USB devices that can be specified on the commandline. */
128 #define MAX_USB_CMDLINE 8
130 /* XXX: use a two level table to limit memory usage */
131 #define MAX_IOPORTS 65536
133 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
134 char phys_ram_file
[1024];
135 void *ioport_opaque
[MAX_IOPORTS
];
136 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
137 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
138 /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available
139 to store the VM snapshots */
140 BlockDriverState
*bs_table
[MAX_DISKS
+ 1], *fd_table
[MAX_FD
];
141 BlockDriverState
*pflash_table
[MAX_PFLASH
];
142 BlockDriverState
*sd_bdrv
;
143 /* point to the block driver where the snapshots are managed */
144 BlockDriverState
*bs_snapshots
;
146 static DisplayState display_state
;
148 const char* keyboard_layout
= NULL
;
149 int64_t ticks_per_sec
;
150 int boot_device
= 'c';
152 int pit_min_timer_count
= 0;
154 NICInfo nd_table
[MAX_NICS
];
155 QEMUTimer
*gui_timer
;
158 int cirrus_vga_enabled
= 1;
159 int vmsvga_enabled
= 0;
161 int graphic_width
= 1024;
162 int graphic_height
= 768;
163 int graphic_depth
= 8;
165 int graphic_width
= 800;
166 int graphic_height
= 600;
167 int graphic_depth
= 15;
172 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
173 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
175 int win2k_install_hack
= 0;
178 static VLANState
*first_vlan
;
180 const char *vnc_display
;
181 #if defined(TARGET_SPARC)
183 #elif defined(TARGET_I386)
188 int acpi_enabled
= 1;
191 int graphic_rotate
= 0;
193 const char *option_rom
[MAX_OPTION_ROMS
];
195 int semihosting_enabled
= 0;
197 const char *qemu_name
;
199 /***********************************************************/
200 /* x86 ISA bus support */
202 target_phys_addr_t isa_mem_base
= 0;
205 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
207 #ifdef DEBUG_UNUSED_IOPORT
208 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
213 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
215 #ifdef DEBUG_UNUSED_IOPORT
216 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
220 /* default is to make two byte accesses */
221 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
224 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
225 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
226 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
230 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
232 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
233 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
234 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
237 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
239 #ifdef DEBUG_UNUSED_IOPORT
240 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
245 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
247 #ifdef DEBUG_UNUSED_IOPORT
248 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
252 void init_ioports(void)
256 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
257 ioport_read_table
[0][i
] = default_ioport_readb
;
258 ioport_write_table
[0][i
] = default_ioport_writeb
;
259 ioport_read_table
[1][i
] = default_ioport_readw
;
260 ioport_write_table
[1][i
] = default_ioport_writew
;
261 ioport_read_table
[2][i
] = default_ioport_readl
;
262 ioport_write_table
[2][i
] = default_ioport_writel
;
266 /* size is the word size in byte */
267 int register_ioport_read(int start
, int length
, int size
,
268 IOPortReadFunc
*func
, void *opaque
)
274 } else if (size
== 2) {
276 } else if (size
== 4) {
279 hw_error("register_ioport_read: invalid size");
282 for(i
= start
; i
< start
+ length
; i
+= size
) {
283 ioport_read_table
[bsize
][i
] = func
;
284 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
285 hw_error("register_ioport_read: invalid opaque");
286 ioport_opaque
[i
] = opaque
;
291 /* size is the word size in byte */
292 int register_ioport_write(int start
, int length
, int size
,
293 IOPortWriteFunc
*func
, void *opaque
)
299 } else if (size
== 2) {
301 } else if (size
== 4) {
304 hw_error("register_ioport_write: invalid size");
307 for(i
= start
; i
< start
+ length
; i
+= size
) {
308 ioport_write_table
[bsize
][i
] = func
;
309 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
310 hw_error("register_ioport_write: invalid opaque");
311 ioport_opaque
[i
] = opaque
;
316 void isa_unassign_ioport(int start
, int length
)
320 for(i
= start
; i
< start
+ length
; i
++) {
321 ioport_read_table
[0][i
] = default_ioport_readb
;
322 ioport_read_table
[1][i
] = default_ioport_readw
;
323 ioport_read_table
[2][i
] = default_ioport_readl
;
325 ioport_write_table
[0][i
] = default_ioport_writeb
;
326 ioport_write_table
[1][i
] = default_ioport_writew
;
327 ioport_write_table
[2][i
] = default_ioport_writel
;
331 /***********************************************************/
333 void cpu_outb(CPUState
*env
, int addr
, int val
)
336 if (loglevel
& CPU_LOG_IOPORT
)
337 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
339 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
342 env
->last_io_time
= cpu_get_time_fast();
346 void cpu_outw(CPUState
*env
, int addr
, int val
)
349 if (loglevel
& CPU_LOG_IOPORT
)
350 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
352 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
355 env
->last_io_time
= cpu_get_time_fast();
359 void cpu_outl(CPUState
*env
, int addr
, int val
)
362 if (loglevel
& CPU_LOG_IOPORT
)
363 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
365 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
368 env
->last_io_time
= cpu_get_time_fast();
372 int cpu_inb(CPUState
*env
, int addr
)
375 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
377 if (loglevel
& CPU_LOG_IOPORT
)
378 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
382 env
->last_io_time
= cpu_get_time_fast();
387 int cpu_inw(CPUState
*env
, int addr
)
390 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
392 if (loglevel
& CPU_LOG_IOPORT
)
393 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
397 env
->last_io_time
= cpu_get_time_fast();
402 int cpu_inl(CPUState
*env
, int addr
)
405 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
407 if (loglevel
& CPU_LOG_IOPORT
)
408 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
412 env
->last_io_time
= cpu_get_time_fast();
417 /***********************************************************/
418 void hw_error(const char *fmt
, ...)
424 fprintf(stderr
, "qemu: hardware error: ");
425 vfprintf(stderr
, fmt
, ap
);
426 fprintf(stderr
, "\n");
427 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
428 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
430 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
432 cpu_dump_state(env
, stderr
, fprintf
, 0);
439 /***********************************************************/
442 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
443 static void *qemu_put_kbd_event_opaque
;
444 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
445 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
447 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
449 qemu_put_kbd_event_opaque
= opaque
;
450 qemu_put_kbd_event
= func
;
453 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
454 void *opaque
, int absolute
,
457 QEMUPutMouseEntry
*s
, *cursor
;
459 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
463 s
->qemu_put_mouse_event
= func
;
464 s
->qemu_put_mouse_event_opaque
= opaque
;
465 s
->qemu_put_mouse_event_absolute
= absolute
;
466 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
469 if (!qemu_put_mouse_event_head
) {
470 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
474 cursor
= qemu_put_mouse_event_head
;
475 while (cursor
->next
!= NULL
)
476 cursor
= cursor
->next
;
479 qemu_put_mouse_event_current
= s
;
484 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
486 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
488 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
491 cursor
= qemu_put_mouse_event_head
;
492 while (cursor
!= NULL
&& cursor
!= entry
) {
494 cursor
= cursor
->next
;
497 if (cursor
== NULL
) // does not exist or list empty
499 else if (prev
== NULL
) { // entry is head
500 qemu_put_mouse_event_head
= cursor
->next
;
501 if (qemu_put_mouse_event_current
== entry
)
502 qemu_put_mouse_event_current
= cursor
->next
;
503 qemu_free(entry
->qemu_put_mouse_event_name
);
508 prev
->next
= entry
->next
;
510 if (qemu_put_mouse_event_current
== entry
)
511 qemu_put_mouse_event_current
= prev
;
513 qemu_free(entry
->qemu_put_mouse_event_name
);
517 void kbd_put_keycode(int keycode
)
519 if (qemu_put_kbd_event
) {
520 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
524 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
526 QEMUPutMouseEvent
*mouse_event
;
527 void *mouse_event_opaque
;
530 if (!qemu_put_mouse_event_current
) {
535 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
537 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
540 if (graphic_rotate
) {
541 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
544 width
= graphic_width
;
545 mouse_event(mouse_event_opaque
,
546 width
- dy
, dx
, dz
, buttons_state
);
548 mouse_event(mouse_event_opaque
,
549 dx
, dy
, dz
, buttons_state
);
553 int kbd_mouse_is_absolute(void)
555 if (!qemu_put_mouse_event_current
)
558 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
561 void (*kbd_mouse_set
)(int x
, int y
, int on
) = NULL
;
562 void (*kbd_cursor_define
)(int width
, int height
, int bpp
, int hot_x
, int hot_y
,
563 uint8_t *image
, uint8_t *mask
) = NULL
;
565 void do_info_mice(void)
567 QEMUPutMouseEntry
*cursor
;
570 if (!qemu_put_mouse_event_head
) {
571 term_printf("No mouse devices connected\n");
575 term_printf("Mouse devices available:\n");
576 cursor
= qemu_put_mouse_event_head
;
577 while (cursor
!= NULL
) {
578 term_printf("%c Mouse #%d: %s\n",
579 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
580 index
, cursor
->qemu_put_mouse_event_name
);
582 cursor
= cursor
->next
;
586 void do_mouse_set(int index
)
588 QEMUPutMouseEntry
*cursor
;
591 if (!qemu_put_mouse_event_head
) {
592 term_printf("No mouse devices connected\n");
596 cursor
= qemu_put_mouse_event_head
;
597 while (cursor
!= NULL
&& index
!= i
) {
599 cursor
= cursor
->next
;
603 qemu_put_mouse_event_current
= cursor
;
605 term_printf("Mouse at given index not found\n");
608 /* compute with 96 bit intermediate result: (a*b)/c */
609 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
614 #ifdef WORDS_BIGENDIAN
624 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
625 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
628 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
632 /***********************************************************/
633 /* real time host monotonic timer */
635 #define QEMU_TIMER_BASE 1000000000LL
639 static int64_t clock_freq
;
641 static void init_get_clock(void)
645 ret
= QueryPerformanceFrequency(&freq
);
647 fprintf(stderr
, "Could not calibrate ticks\n");
650 clock_freq
= freq
.QuadPart
;
653 static int64_t get_clock(void)
656 QueryPerformanceCounter(&ti
);
657 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
662 static int use_rt_clock
;
664 static void init_get_clock(void)
667 #if defined(__linux__)
670 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
677 static int64_t get_clock(void)
679 #if defined(__linux__)
682 clock_gettime(CLOCK_MONOTONIC
, &ts
);
683 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
687 /* XXX: using gettimeofday leads to problems if the date
688 changes, so it should be avoided. */
690 gettimeofday(&tv
, NULL
);
691 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
697 /***********************************************************/
698 /* guest cycle counter */
700 static int64_t cpu_ticks_prev
;
701 static int64_t cpu_ticks_offset
;
702 static int64_t cpu_clock_offset
;
703 static int cpu_ticks_enabled
;
705 /* return the host CPU cycle counter and handle stop/restart */
706 int64_t cpu_get_ticks(void)
708 if (!cpu_ticks_enabled
) {
709 return cpu_ticks_offset
;
712 ticks
= cpu_get_real_ticks();
713 if (cpu_ticks_prev
> ticks
) {
714 /* Note: non increasing ticks may happen if the host uses
716 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
718 cpu_ticks_prev
= ticks
;
719 return ticks
+ cpu_ticks_offset
;
723 /* return the host CPU monotonic timer and handle stop/restart */
724 static int64_t cpu_get_clock(void)
727 if (!cpu_ticks_enabled
) {
728 return cpu_clock_offset
;
731 return ti
+ cpu_clock_offset
;
735 /* enable cpu_get_ticks() */
736 void cpu_enable_ticks(void)
738 if (!cpu_ticks_enabled
) {
739 cpu_ticks_offset
-= cpu_get_real_ticks();
740 cpu_clock_offset
-= get_clock();
741 cpu_ticks_enabled
= 1;
745 /* disable cpu_get_ticks() : the clock is stopped. You must not call
746 cpu_get_ticks() after that. */
747 void cpu_disable_ticks(void)
749 if (cpu_ticks_enabled
) {
750 cpu_ticks_offset
= cpu_get_ticks();
751 cpu_clock_offset
= cpu_get_clock();
752 cpu_ticks_enabled
= 0;
756 /***********************************************************/
759 #define QEMU_TIMER_REALTIME 0
760 #define QEMU_TIMER_VIRTUAL 1
764 /* XXX: add frequency */
772 struct QEMUTimer
*next
;
778 static QEMUTimer
*active_timers
[2];
780 static MMRESULT timerID
;
781 static HANDLE host_alarm
= NULL
;
782 static unsigned int period
= 1;
784 /* frequency of the times() clock tick */
785 static int timer_freq
;
788 QEMUClock
*qemu_new_clock(int type
)
791 clock
= qemu_mallocz(sizeof(QEMUClock
));
798 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
802 ts
= qemu_mallocz(sizeof(QEMUTimer
));
809 void qemu_free_timer(QEMUTimer
*ts
)
814 /* stop a timer, but do not dealloc it */
815 void qemu_del_timer(QEMUTimer
*ts
)
819 /* NOTE: this code must be signal safe because
820 qemu_timer_expired() can be called from a signal. */
821 pt
= &active_timers
[ts
->clock
->type
];
834 /* modify the current timer so that it will be fired when current_time
835 >= expire_time. The corresponding callback will be called. */
836 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
842 /* add the timer in the sorted list */
843 /* NOTE: this code must be signal safe because
844 qemu_timer_expired() can be called from a signal. */
845 pt
= &active_timers
[ts
->clock
->type
];
850 if (t
->expire_time
> expire_time
)
854 ts
->expire_time
= expire_time
;
859 int qemu_timer_pending(QEMUTimer
*ts
)
862 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
869 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
873 return (timer_head
->expire_time
<= current_time
);
876 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
882 if (!ts
|| ts
->expire_time
> current_time
)
884 /* remove timer from the list before calling the callback */
885 *ptimer_head
= ts
->next
;
888 /* run the callback (the timer list can be modified) */
893 int64_t qemu_get_clock(QEMUClock
*clock
)
895 switch(clock
->type
) {
896 case QEMU_TIMER_REALTIME
:
897 return get_clock() / 1000000;
899 case QEMU_TIMER_VIRTUAL
:
900 return cpu_get_clock();
904 static void init_timers(void)
907 ticks_per_sec
= QEMU_TIMER_BASE
;
908 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
909 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
913 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
915 uint64_t expire_time
;
917 if (qemu_timer_pending(ts
)) {
918 expire_time
= ts
->expire_time
;
922 qemu_put_be64(f
, expire_time
);
925 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
927 uint64_t expire_time
;
929 expire_time
= qemu_get_be64(f
);
930 if (expire_time
!= -1) {
931 qemu_mod_timer(ts
, expire_time
);
937 static void timer_save(QEMUFile
*f
, void *opaque
)
939 if (cpu_ticks_enabled
) {
940 hw_error("cannot save state if virtual timers are running");
942 qemu_put_be64s(f
, &cpu_ticks_offset
);
943 qemu_put_be64s(f
, &ticks_per_sec
);
944 qemu_put_be64s(f
, &cpu_clock_offset
);
947 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
949 if (version_id
!= 1 && version_id
!= 2)
951 if (cpu_ticks_enabled
) {
954 qemu_get_be64s(f
, &cpu_ticks_offset
);
955 qemu_get_be64s(f
, &ticks_per_sec
);
956 if (version_id
== 2) {
957 qemu_get_be64s(f
, &cpu_clock_offset
);
963 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
964 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
966 static void host_alarm_handler(int host_signum
)
970 #define DISP_FREQ 1000
972 static int64_t delta_min
= INT64_MAX
;
973 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
975 ti
= qemu_get_clock(vm_clock
);
976 if (last_clock
!= 0) {
977 delta
= ti
- last_clock
;
978 if (delta
< delta_min
)
980 if (delta
> delta_max
)
983 if (++count
== DISP_FREQ
) {
984 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
985 muldiv64(delta_min
, 1000000, ticks_per_sec
),
986 muldiv64(delta_max
, 1000000, ticks_per_sec
),
987 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
988 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
990 delta_min
= INT64_MAX
;
998 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
999 qemu_get_clock(vm_clock
)) ||
1000 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1001 qemu_get_clock(rt_clock
))) {
1003 SetEvent(host_alarm
);
1005 CPUState
*env
= cpu_single_env
;
1007 /* stop the currently executing cpu because a timer occured */
1008 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1010 if (env
->kqemu_enabled
) {
1011 kqemu_cpu_interrupt(env
);
1020 #if defined(__linux__)
1022 #define RTC_FREQ 1024
1026 static int start_rtc_timer(void)
1028 rtc_fd
= open("/dev/rtc", O_RDONLY
);
1031 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1032 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1033 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1034 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1037 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1042 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
1048 static int start_rtc_timer(void)
1053 #endif /* !defined(__linux__) */
1055 #endif /* !defined(_WIN32) */
1057 static void init_timer_alarm(void)
1064 ZeroMemory(&tc
, sizeof(TIMECAPS
));
1065 timeGetDevCaps(&tc
, sizeof(TIMECAPS
));
1066 if (period
< tc
.wPeriodMin
)
1067 period
= tc
.wPeriodMin
;
1068 timeBeginPeriod(period
);
1069 timerID
= timeSetEvent(1, // interval (ms)
1070 period
, // resolution
1071 host_alarm_handler
, // function
1072 (DWORD
)&count
, // user parameter
1073 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
1075 perror("failed timer alarm");
1078 host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1080 perror("failed CreateEvent");
1083 qemu_add_wait_object(host_alarm
, NULL
, NULL
);
1085 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
1088 struct sigaction act
;
1089 struct itimerval itv
;
1091 /* get times() syscall frequency */
1092 timer_freq
= sysconf(_SC_CLK_TCK
);
1095 sigfillset(&act
.sa_mask
);
1097 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1098 act
.sa_flags
|= SA_ONSTACK
;
1100 act
.sa_handler
= host_alarm_handler
;
1101 sigaction(SIGALRM
, &act
, NULL
);
1103 itv
.it_interval
.tv_sec
= 0;
1104 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
1105 itv
.it_value
.tv_sec
= 0;
1106 itv
.it_value
.tv_usec
= 10 * 1000;
1107 setitimer(ITIMER_REAL
, &itv
, NULL
);
1108 /* we probe the tick duration of the kernel to inform the user if
1109 the emulated kernel requested a too high timer frequency */
1110 getitimer(ITIMER_REAL
, &itv
);
1112 #if defined(__linux__)
1113 /* XXX: force /dev/rtc usage because even 2.6 kernels may not
1114 have timers with 1 ms resolution. The correct solution will
1115 be to use the POSIX real time timers available in recent
1117 if (itv
.it_interval
.tv_usec
> 1000 || 1) {
1118 /* try to use /dev/rtc to have a faster timer */
1119 if (start_rtc_timer() < 0)
1121 /* disable itimer */
1122 itv
.it_interval
.tv_sec
= 0;
1123 itv
.it_interval
.tv_usec
= 0;
1124 itv
.it_value
.tv_sec
= 0;
1125 itv
.it_value
.tv_usec
= 0;
1126 setitimer(ITIMER_REAL
, &itv
, NULL
);
1129 sigaction(SIGIO
, &act
, NULL
);
1130 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1131 fcntl(rtc_fd
, F_SETOWN
, getpid());
1133 #endif /* defined(__linux__) */
1136 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1137 PIT_FREQ
) / 1000000;
1143 void quit_timers(void)
1146 timeKillEvent(timerID
);
1147 timeEndPeriod(period
);
1149 CloseHandle(host_alarm
);
1155 /***********************************************************/
1156 /* character device */
1158 static void qemu_chr_event(CharDriverState
*s
, int event
)
1162 s
->chr_event(s
->handler_opaque
, event
);
1165 static void qemu_chr_reset_bh(void *opaque
)
1167 CharDriverState
*s
= opaque
;
1168 qemu_chr_event(s
, CHR_EVENT_RESET
);
1169 qemu_bh_delete(s
->bh
);
1173 void qemu_chr_reset(CharDriverState
*s
)
1175 if (s
->bh
== NULL
) {
1176 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
1177 qemu_bh_schedule(s
->bh
);
1181 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1183 return s
->chr_write(s
, buf
, len
);
1186 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1190 return s
->chr_ioctl(s
, cmd
, arg
);
1193 int qemu_chr_can_read(CharDriverState
*s
)
1195 if (!s
->chr_can_read
)
1197 return s
->chr_can_read(s
->handler_opaque
);
1200 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
1202 s
->chr_read(s
->handler_opaque
, buf
, len
);
1206 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1211 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1212 qemu_chr_write(s
, buf
, strlen(buf
));
1216 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1218 if (s
->chr_send_event
)
1219 s
->chr_send_event(s
, event
);
1222 void qemu_chr_add_handlers(CharDriverState
*s
,
1223 IOCanRWHandler
*fd_can_read
,
1224 IOReadHandler
*fd_read
,
1225 IOEventHandler
*fd_event
,
1228 s
->chr_can_read
= fd_can_read
;
1229 s
->chr_read
= fd_read
;
1230 s
->chr_event
= fd_event
;
1231 s
->handler_opaque
= opaque
;
1232 if (s
->chr_update_read_handler
)
1233 s
->chr_update_read_handler(s
);
1236 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1241 static CharDriverState
*qemu_chr_open_null(void)
1243 CharDriverState
*chr
;
1245 chr
= qemu_mallocz(sizeof(CharDriverState
));
1248 chr
->chr_write
= null_chr_write
;
1252 /* MUX driver for serial I/O splitting */
1253 static int term_timestamps
;
1254 static int64_t term_timestamps_start
;
1257 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
1258 IOReadHandler
*chr_read
[MAX_MUX
];
1259 IOEventHandler
*chr_event
[MAX_MUX
];
1260 void *ext_opaque
[MAX_MUX
];
1261 CharDriverState
*drv
;
1263 int term_got_escape
;
1268 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1270 MuxDriver
*d
= chr
->opaque
;
1272 if (!term_timestamps
) {
1273 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
1278 for(i
= 0; i
< len
; i
++) {
1279 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
1280 if (buf
[i
] == '\n') {
1286 if (term_timestamps_start
== -1)
1287 term_timestamps_start
= ti
;
1288 ti
-= term_timestamps_start
;
1289 secs
= ti
/ 1000000000;
1290 snprintf(buf1
, sizeof(buf1
),
1291 "[%02d:%02d:%02d.%03d] ",
1295 (int)((ti
/ 1000000) % 1000));
1296 d
->drv
->chr_write(d
->drv
, buf1
, strlen(buf1
));
1303 static char *mux_help
[] = {
1304 "% h print this help\n\r",
1305 "% x exit emulator\n\r",
1306 "% s save disk data back to file (if -snapshot)\n\r",
1307 "% t toggle console timestamps\n\r"
1308 "% b send break (magic sysrq)\n\r",
1309 "% c switch between console and monitor\n\r",
1314 static int term_escape_char
= 0x01; /* ctrl-a is used for escape */
1315 static void mux_print_help(CharDriverState
*chr
)
1318 char ebuf
[15] = "Escape-Char";
1319 char cbuf
[50] = "\n\r";
1321 if (term_escape_char
> 0 && term_escape_char
< 26) {
1322 sprintf(cbuf
,"\n\r");
1323 sprintf(ebuf
,"C-%c", term_escape_char
- 1 + 'a');
1325 sprintf(cbuf
,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char
);
1327 chr
->chr_write(chr
, cbuf
, strlen(cbuf
));
1328 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
1329 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
1330 if (mux_help
[i
][j
] == '%')
1331 chr
->chr_write(chr
, ebuf
, strlen(ebuf
));
1333 chr
->chr_write(chr
, &mux_help
[i
][j
], 1);
1338 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
1340 if (d
->term_got_escape
) {
1341 d
->term_got_escape
= 0;
1342 if (ch
== term_escape_char
)
1347 mux_print_help(chr
);
1351 char *term
= "QEMU: Terminated\n\r";
1352 chr
->chr_write(chr
,term
,strlen(term
));
1359 for (i
= 0; i
< MAX_DISKS
; i
++) {
1361 bdrv_commit(bs_table
[i
]);
1367 chr
->chr_event(chr
->opaque
, CHR_EVENT_BREAK
);
1370 /* Switch to the next registered device */
1372 if (chr
->focus
>= d
->mux_cnt
)
1376 term_timestamps
= !term_timestamps
;
1377 term_timestamps_start
= -1;
1380 } else if (ch
== term_escape_char
) {
1381 d
->term_got_escape
= 1;
1389 static int mux_chr_can_read(void *opaque
)
1391 CharDriverState
*chr
= opaque
;
1392 MuxDriver
*d
= chr
->opaque
;
1393 if (d
->chr_can_read
[chr
->focus
])
1394 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
1398 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
1400 CharDriverState
*chr
= opaque
;
1401 MuxDriver
*d
= chr
->opaque
;
1403 for(i
= 0; i
< size
; i
++)
1404 if (mux_proc_byte(chr
, d
, buf
[i
]))
1405 d
->chr_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
], &buf
[i
], 1);
1408 static void mux_chr_event(void *opaque
, int event
)
1410 CharDriverState
*chr
= opaque
;
1411 MuxDriver
*d
= chr
->opaque
;
1414 /* Send the event to all registered listeners */
1415 for (i
= 0; i
< d
->mux_cnt
; i
++)
1416 if (d
->chr_event
[i
])
1417 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
1420 static void mux_chr_update_read_handler(CharDriverState
*chr
)
1422 MuxDriver
*d
= chr
->opaque
;
1424 if (d
->mux_cnt
>= MAX_MUX
) {
1425 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
1428 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
1429 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
1430 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
1431 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
1432 /* Fix up the real driver with mux routines */
1433 if (d
->mux_cnt
== 0) {
1434 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
1435 mux_chr_event
, chr
);
1437 chr
->focus
= d
->mux_cnt
;
1441 CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
1443 CharDriverState
*chr
;
1446 chr
= qemu_mallocz(sizeof(CharDriverState
));
1449 d
= qemu_mallocz(sizeof(MuxDriver
));
1458 chr
->chr_write
= mux_chr_write
;
1459 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
1466 static void socket_cleanup(void)
1471 static int socket_init(void)
1476 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1478 err
= WSAGetLastError();
1479 fprintf(stderr
, "WSAStartup: %d\n", err
);
1482 atexit(socket_cleanup
);
1486 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1492 ret
= send(fd
, buf
, len
, 0);
1495 errno
= WSAGetLastError();
1496 if (errno
!= WSAEWOULDBLOCK
) {
1499 } else if (ret
== 0) {
1509 void socket_set_nonblock(int fd
)
1511 unsigned long opt
= 1;
1512 ioctlsocket(fd
, FIONBIO
, &opt
);
1517 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1523 ret
= write(fd
, buf
, len
);
1525 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1527 } else if (ret
== 0) {
1537 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1539 return unix_write(fd
, buf
, len1
);
1542 void socket_set_nonblock(int fd
)
1544 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1546 #endif /* !_WIN32 */
1555 #define STDIO_MAX_CLIENTS 1
1556 static int stdio_nb_clients
= 0;
1558 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1560 FDCharDriver
*s
= chr
->opaque
;
1561 return unix_write(s
->fd_out
, buf
, len
);
1564 static int fd_chr_read_poll(void *opaque
)
1566 CharDriverState
*chr
= opaque
;
1567 FDCharDriver
*s
= chr
->opaque
;
1569 s
->max_size
= qemu_chr_can_read(chr
);
1573 static void fd_chr_read(void *opaque
)
1575 CharDriverState
*chr
= opaque
;
1576 FDCharDriver
*s
= chr
->opaque
;
1581 if (len
> s
->max_size
)
1585 size
= read(s
->fd_in
, buf
, len
);
1587 /* FD has been closed. Remove it from the active list. */
1588 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1592 qemu_chr_read(chr
, buf
, size
);
1596 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1598 FDCharDriver
*s
= chr
->opaque
;
1600 if (s
->fd_in
>= 0) {
1601 if (nographic
&& s
->fd_in
== 0) {
1603 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1604 fd_chr_read
, NULL
, chr
);
1609 /* open a character device to a unix fd */
1610 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1612 CharDriverState
*chr
;
1615 chr
= qemu_mallocz(sizeof(CharDriverState
));
1618 s
= qemu_mallocz(sizeof(FDCharDriver
));
1626 chr
->chr_write
= fd_chr_write
;
1627 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
1629 qemu_chr_reset(chr
);
1634 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1638 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1641 return qemu_chr_open_fd(-1, fd_out
);
1644 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1647 char filename_in
[256], filename_out
[256];
1649 snprintf(filename_in
, 256, "%s.in", filename
);
1650 snprintf(filename_out
, 256, "%s.out", filename
);
1651 fd_in
= open(filename_in
, O_RDWR
| O_BINARY
);
1652 fd_out
= open(filename_out
, O_RDWR
| O_BINARY
);
1653 if (fd_in
< 0 || fd_out
< 0) {
1658 fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
);
1662 return qemu_chr_open_fd(fd_in
, fd_out
);
1666 /* for STDIO, we handle the case where several clients use it
1669 #define TERM_FIFO_MAX_SIZE 1
1671 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1672 static int term_fifo_size
;
1674 static int stdio_read_poll(void *opaque
)
1676 CharDriverState
*chr
= opaque
;
1678 /* try to flush the queue if needed */
1679 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
1680 qemu_chr_read(chr
, term_fifo
, 1);
1683 /* see if we can absorb more chars */
1684 if (term_fifo_size
== 0)
1690 static void stdio_read(void *opaque
)
1694 CharDriverState
*chr
= opaque
;
1696 size
= read(0, buf
, 1);
1698 /* stdin has been closed. Remove it from the active list. */
1699 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
1703 if (qemu_chr_can_read(chr
) > 0) {
1704 qemu_chr_read(chr
, buf
, 1);
1705 } else if (term_fifo_size
== 0) {
1706 term_fifo
[term_fifo_size
++] = buf
[0];
1711 /* init terminal so that we can grab keys */
1712 static struct termios oldtty
;
1713 static int old_fd0_flags
;
1715 static void term_exit(void)
1717 tcsetattr (0, TCSANOW
, &oldtty
);
1718 fcntl(0, F_SETFL
, old_fd0_flags
);
1721 static void term_init(void)
1725 tcgetattr (0, &tty
);
1727 old_fd0_flags
= fcntl(0, F_GETFL
);
1729 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1730 |INLCR
|IGNCR
|ICRNL
|IXON
);
1731 tty
.c_oflag
|= OPOST
;
1732 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1733 /* if graphical mode, we allow Ctrl-C handling */
1735 tty
.c_lflag
&= ~ISIG
;
1736 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1739 tty
.c_cc
[VTIME
] = 0;
1741 tcsetattr (0, TCSANOW
, &tty
);
1745 fcntl(0, F_SETFL
, O_NONBLOCK
);
1748 static CharDriverState
*qemu_chr_open_stdio(void)
1750 CharDriverState
*chr
;
1752 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1754 chr
= qemu_chr_open_fd(0, 1);
1755 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
1762 #if defined(__linux__)
1763 static CharDriverState
*qemu_chr_open_pty(void)
1766 char slave_name
[1024];
1767 int master_fd
, slave_fd
;
1769 /* Not satisfying */
1770 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1774 /* Disabling local echo and line-buffered output */
1775 tcgetattr (master_fd
, &tty
);
1776 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1778 tty
.c_cc
[VTIME
] = 0;
1779 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1781 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1782 return qemu_chr_open_fd(master_fd
, master_fd
);
1785 static void tty_serial_init(int fd
, int speed
,
1786 int parity
, int data_bits
, int stop_bits
)
1792 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1793 speed
, parity
, data_bits
, stop_bits
);
1795 tcgetattr (fd
, &tty
);
1837 cfsetispeed(&tty
, spd
);
1838 cfsetospeed(&tty
, spd
);
1840 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1841 |INLCR
|IGNCR
|ICRNL
|IXON
);
1842 tty
.c_oflag
|= OPOST
;
1843 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1844 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1865 tty
.c_cflag
|= PARENB
;
1868 tty
.c_cflag
|= PARENB
| PARODD
;
1872 tty
.c_cflag
|= CSTOPB
;
1874 tcsetattr (fd
, TCSANOW
, &tty
);
1877 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1879 FDCharDriver
*s
= chr
->opaque
;
1882 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1884 QEMUSerialSetParams
*ssp
= arg
;
1885 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1886 ssp
->data_bits
, ssp
->stop_bits
);
1889 case CHR_IOCTL_SERIAL_SET_BREAK
:
1891 int enable
= *(int *)arg
;
1893 tcsendbreak(s
->fd_in
, 1);
1902 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1904 CharDriverState
*chr
;
1907 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1910 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1911 tty_serial_init(fd
, 115200, 'N', 8, 1);
1912 chr
= qemu_chr_open_fd(fd
, fd
);
1915 chr
->chr_ioctl
= tty_serial_ioctl
;
1916 qemu_chr_reset(chr
);
1923 } ParallelCharDriver
;
1925 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1927 if (s
->mode
!= mode
) {
1929 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1936 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1938 ParallelCharDriver
*drv
= chr
->opaque
;
1943 case CHR_IOCTL_PP_READ_DATA
:
1944 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1946 *(uint8_t *)arg
= b
;
1948 case CHR_IOCTL_PP_WRITE_DATA
:
1949 b
= *(uint8_t *)arg
;
1950 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1953 case CHR_IOCTL_PP_READ_CONTROL
:
1954 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1956 /* Linux gives only the lowest bits, and no way to know data
1957 direction! For better compatibility set the fixed upper
1959 *(uint8_t *)arg
= b
| 0xc0;
1961 case CHR_IOCTL_PP_WRITE_CONTROL
:
1962 b
= *(uint8_t *)arg
;
1963 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1966 case CHR_IOCTL_PP_READ_STATUS
:
1967 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1969 *(uint8_t *)arg
= b
;
1971 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1972 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1973 struct ParallelIOArg
*parg
= arg
;
1974 int n
= read(fd
, parg
->buffer
, parg
->count
);
1975 if (n
!= parg
->count
) {
1980 case CHR_IOCTL_PP_EPP_READ
:
1981 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1982 struct ParallelIOArg
*parg
= arg
;
1983 int n
= read(fd
, parg
->buffer
, parg
->count
);
1984 if (n
!= parg
->count
) {
1989 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1990 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1991 struct ParallelIOArg
*parg
= arg
;
1992 int n
= write(fd
, parg
->buffer
, parg
->count
);
1993 if (n
!= parg
->count
) {
1998 case CHR_IOCTL_PP_EPP_WRITE
:
1999 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2000 struct ParallelIOArg
*parg
= arg
;
2001 int n
= write(fd
, parg
->buffer
, parg
->count
);
2002 if (n
!= parg
->count
) {
2013 static void pp_close(CharDriverState
*chr
)
2015 ParallelCharDriver
*drv
= chr
->opaque
;
2018 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
2019 ioctl(fd
, PPRELEASE
);
2024 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
2026 CharDriverState
*chr
;
2027 ParallelCharDriver
*drv
;
2030 fd
= open(filename
, O_RDWR
);
2034 if (ioctl(fd
, PPCLAIM
) < 0) {
2039 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
2045 drv
->mode
= IEEE1284_MODE_COMPAT
;
2047 chr
= qemu_mallocz(sizeof(CharDriverState
));
2053 chr
->chr_write
= null_chr_write
;
2054 chr
->chr_ioctl
= pp_ioctl
;
2055 chr
->chr_close
= pp_close
;
2058 qemu_chr_reset(chr
);
2064 static CharDriverState
*qemu_chr_open_pty(void)
2070 #endif /* !defined(_WIN32) */
2075 HANDLE hcom
, hrecv
, hsend
;
2076 OVERLAPPED orecv
, osend
;
2081 #define NSENDBUF 2048
2082 #define NRECVBUF 2048
2083 #define MAXCONNECT 1
2084 #define NTIMEOUT 5000
2086 static int win_chr_poll(void *opaque
);
2087 static int win_chr_pipe_poll(void *opaque
);
2089 static void win_chr_close(CharDriverState
*chr
)
2091 WinCharState
*s
= chr
->opaque
;
2094 CloseHandle(s
->hsend
);
2098 CloseHandle(s
->hrecv
);
2102 CloseHandle(s
->hcom
);
2106 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
2108 qemu_del_polling_cb(win_chr_poll
, chr
);
2111 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
2113 WinCharState
*s
= chr
->opaque
;
2115 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2120 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2122 fprintf(stderr
, "Failed CreateEvent\n");
2125 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2127 fprintf(stderr
, "Failed CreateEvent\n");
2131 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2132 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2133 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2134 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
2139 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2140 fprintf(stderr
, "Failed SetupComm\n");
2144 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2145 size
= sizeof(COMMCONFIG
);
2146 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2147 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2148 CommConfigDialog(filename
, NULL
, &comcfg
);
2150 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2151 fprintf(stderr
, "Failed SetCommState\n");
2155 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2156 fprintf(stderr
, "Failed SetCommMask\n");
2160 cto
.ReadIntervalTimeout
= MAXDWORD
;
2161 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2162 fprintf(stderr
, "Failed SetCommTimeouts\n");
2166 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2167 fprintf(stderr
, "Failed ClearCommError\n");
2170 qemu_add_polling_cb(win_chr_poll
, chr
);
2178 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2180 WinCharState
*s
= chr
->opaque
;
2181 DWORD len
, ret
, size
, err
;
2184 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2185 s
->osend
.hEvent
= s
->hsend
;
2188 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2190 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2192 err
= GetLastError();
2193 if (err
== ERROR_IO_PENDING
) {
2194 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2212 static int win_chr_read_poll(CharDriverState
*chr
)
2214 WinCharState
*s
= chr
->opaque
;
2216 s
->max_size
= qemu_chr_can_read(chr
);
2220 static void win_chr_readfile(CharDriverState
*chr
)
2222 WinCharState
*s
= chr
->opaque
;
2227 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2228 s
->orecv
.hEvent
= s
->hrecv
;
2229 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2231 err
= GetLastError();
2232 if (err
== ERROR_IO_PENDING
) {
2233 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2238 qemu_chr_read(chr
, buf
, size
);
2242 static void win_chr_read(CharDriverState
*chr
)
2244 WinCharState
*s
= chr
->opaque
;
2246 if (s
->len
> s
->max_size
)
2247 s
->len
= s
->max_size
;
2251 win_chr_readfile(chr
);
2254 static int win_chr_poll(void *opaque
)
2256 CharDriverState
*chr
= opaque
;
2257 WinCharState
*s
= chr
->opaque
;
2261 ClearCommError(s
->hcom
, &comerr
, &status
);
2262 if (status
.cbInQue
> 0) {
2263 s
->len
= status
.cbInQue
;
2264 win_chr_read_poll(chr
);
2271 static CharDriverState
*qemu_chr_open_win(const char *filename
)
2273 CharDriverState
*chr
;
2276 chr
= qemu_mallocz(sizeof(CharDriverState
));
2279 s
= qemu_mallocz(sizeof(WinCharState
));
2285 chr
->chr_write
= win_chr_write
;
2286 chr
->chr_close
= win_chr_close
;
2288 if (win_chr_init(chr
, filename
) < 0) {
2293 qemu_chr_reset(chr
);
2297 static int win_chr_pipe_poll(void *opaque
)
2299 CharDriverState
*chr
= opaque
;
2300 WinCharState
*s
= chr
->opaque
;
2303 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2306 win_chr_read_poll(chr
);
2313 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
2315 WinCharState
*s
= chr
->opaque
;
2323 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2325 fprintf(stderr
, "Failed CreateEvent\n");
2328 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2330 fprintf(stderr
, "Failed CreateEvent\n");
2334 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2335 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2336 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2338 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2339 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2340 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2345 ZeroMemory(&ov
, sizeof(ov
));
2346 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2347 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2349 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2353 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2355 fprintf(stderr
, "Failed GetOverlappedResult\n");
2357 CloseHandle(ov
.hEvent
);
2364 CloseHandle(ov
.hEvent
);
2367 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2376 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2378 CharDriverState
*chr
;
2381 chr
= qemu_mallocz(sizeof(CharDriverState
));
2384 s
= qemu_mallocz(sizeof(WinCharState
));
2390 chr
->chr_write
= win_chr_write
;
2391 chr
->chr_close
= win_chr_close
;
2393 if (win_chr_pipe_init(chr
, filename
) < 0) {
2398 qemu_chr_reset(chr
);
2402 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2404 CharDriverState
*chr
;
2407 chr
= qemu_mallocz(sizeof(CharDriverState
));
2410 s
= qemu_mallocz(sizeof(WinCharState
));
2417 chr
->chr_write
= win_chr_write
;
2418 qemu_chr_reset(chr
);
2422 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2426 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2427 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2428 if (fd_out
== INVALID_HANDLE_VALUE
)
2431 return qemu_chr_open_win_file(fd_out
);
2435 /***********************************************************/
2436 /* UDP Net console */
2440 struct sockaddr_in daddr
;
2447 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2449 NetCharDriver
*s
= chr
->opaque
;
2451 return sendto(s
->fd
, buf
, len
, 0,
2452 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2455 static int udp_chr_read_poll(void *opaque
)
2457 CharDriverState
*chr
= opaque
;
2458 NetCharDriver
*s
= chr
->opaque
;
2460 s
->max_size
= qemu_chr_can_read(chr
);
2462 /* If there were any stray characters in the queue process them
2465 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2466 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2468 s
->max_size
= qemu_chr_can_read(chr
);
2473 static void udp_chr_read(void *opaque
)
2475 CharDriverState
*chr
= opaque
;
2476 NetCharDriver
*s
= chr
->opaque
;
2478 if (s
->max_size
== 0)
2480 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2481 s
->bufptr
= s
->bufcnt
;
2486 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2487 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2489 s
->max_size
= qemu_chr_can_read(chr
);
2493 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2495 NetCharDriver
*s
= chr
->opaque
;
2498 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2499 udp_chr_read
, NULL
, chr
);
2503 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2505 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2507 int parse_host_src_port(struct sockaddr_in
*haddr
,
2508 struct sockaddr_in
*saddr
,
2511 static CharDriverState
*qemu_chr_open_udp(const char *def
)
2513 CharDriverState
*chr
= NULL
;
2514 NetCharDriver
*s
= NULL
;
2516 struct sockaddr_in saddr
;
2518 chr
= qemu_mallocz(sizeof(CharDriverState
));
2521 s
= qemu_mallocz(sizeof(NetCharDriver
));
2525 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2527 perror("socket(PF_INET, SOCK_DGRAM)");
2531 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2532 printf("Could not parse: %s\n", def
);
2536 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2546 chr
->chr_write
= udp_chr_write
;
2547 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2560 /***********************************************************/
2561 /* TCP Net console */
2572 static void tcp_chr_accept(void *opaque
);
2574 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2576 TCPCharDriver
*s
= chr
->opaque
;
2578 return send_all(s
->fd
, buf
, len
);
2580 /* XXX: indicate an error ? */
2585 static int tcp_chr_read_poll(void *opaque
)
2587 CharDriverState
*chr
= opaque
;
2588 TCPCharDriver
*s
= chr
->opaque
;
2591 s
->max_size
= qemu_chr_can_read(chr
);
2596 #define IAC_BREAK 243
2597 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2599 char *buf
, int *size
)
2601 /* Handle any telnet client's basic IAC options to satisfy char by
2602 * char mode with no echo. All IAC options will be removed from
2603 * the buf and the do_telnetopt variable will be used to track the
2604 * state of the width of the IAC information.
2606 * IAC commands come in sets of 3 bytes with the exception of the
2607 * "IAC BREAK" command and the double IAC.
2613 for (i
= 0; i
< *size
; i
++) {
2614 if (s
->do_telnetopt
> 1) {
2615 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2616 /* Double IAC means send an IAC */
2620 s
->do_telnetopt
= 1;
2622 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2623 /* Handle IAC break commands by sending a serial break */
2624 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
2629 if (s
->do_telnetopt
>= 4) {
2630 s
->do_telnetopt
= 1;
2633 if ((unsigned char)buf
[i
] == IAC
) {
2634 s
->do_telnetopt
= 2;
2645 static void tcp_chr_read(void *opaque
)
2647 CharDriverState
*chr
= opaque
;
2648 TCPCharDriver
*s
= chr
->opaque
;
2652 if (!s
->connected
|| s
->max_size
<= 0)
2655 if (len
> s
->max_size
)
2657 size
= recv(s
->fd
, buf
, len
, 0);
2659 /* connection closed */
2661 if (s
->listen_fd
>= 0) {
2662 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2664 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2667 } else if (size
> 0) {
2668 if (s
->do_telnetopt
)
2669 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2671 qemu_chr_read(chr
, buf
, size
);
2675 static void tcp_chr_connect(void *opaque
)
2677 CharDriverState
*chr
= opaque
;
2678 TCPCharDriver
*s
= chr
->opaque
;
2681 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2682 tcp_chr_read
, NULL
, chr
);
2683 qemu_chr_reset(chr
);
2686 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2687 static void tcp_chr_telnet_init(int fd
)
2690 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2691 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2692 send(fd
, (char *)buf
, 3, 0);
2693 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2694 send(fd
, (char *)buf
, 3, 0);
2695 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2696 send(fd
, (char *)buf
, 3, 0);
2697 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2698 send(fd
, (char *)buf
, 3, 0);
2701 static void socket_set_nodelay(int fd
)
2704 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2707 static void tcp_chr_accept(void *opaque
)
2709 CharDriverState
*chr
= opaque
;
2710 TCPCharDriver
*s
= chr
->opaque
;
2711 struct sockaddr_in saddr
;
2713 struct sockaddr_un uaddr
;
2715 struct sockaddr
*addr
;
2722 len
= sizeof(uaddr
);
2723 addr
= (struct sockaddr
*)&uaddr
;
2727 len
= sizeof(saddr
);
2728 addr
= (struct sockaddr
*)&saddr
;
2730 fd
= accept(s
->listen_fd
, addr
, &len
);
2731 if (fd
< 0 && errno
!= EINTR
) {
2733 } else if (fd
>= 0) {
2734 if (s
->do_telnetopt
)
2735 tcp_chr_telnet_init(fd
);
2739 socket_set_nonblock(fd
);
2741 socket_set_nodelay(fd
);
2743 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2744 tcp_chr_connect(chr
);
2747 static void tcp_chr_close(CharDriverState
*chr
)
2749 TCPCharDriver
*s
= chr
->opaque
;
2752 if (s
->listen_fd
>= 0)
2753 closesocket(s
->listen_fd
);
2757 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2761 CharDriverState
*chr
= NULL
;
2762 TCPCharDriver
*s
= NULL
;
2763 int fd
= -1, ret
, err
, val
;
2765 int is_waitconnect
= 1;
2768 struct sockaddr_in saddr
;
2770 struct sockaddr_un uaddr
;
2772 struct sockaddr
*addr
;
2777 addr
= (struct sockaddr
*)&uaddr
;
2778 addrlen
= sizeof(uaddr
);
2779 if (parse_unix_path(&uaddr
, host_str
) < 0)
2784 addr
= (struct sockaddr
*)&saddr
;
2785 addrlen
= sizeof(saddr
);
2786 if (parse_host_port(&saddr
, host_str
) < 0)
2791 while((ptr
= strchr(ptr
,','))) {
2793 if (!strncmp(ptr
,"server",6)) {
2795 } else if (!strncmp(ptr
,"nowait",6)) {
2797 } else if (!strncmp(ptr
,"nodelay",6)) {
2800 printf("Unknown option: %s\n", ptr
);
2807 chr
= qemu_mallocz(sizeof(CharDriverState
));
2810 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2816 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2819 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2824 if (!is_waitconnect
)
2825 socket_set_nonblock(fd
);
2830 s
->is_unix
= is_unix
;
2831 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2834 chr
->chr_write
= tcp_chr_write
;
2835 chr
->chr_close
= tcp_chr_close
;
2838 /* allow fast reuse */
2842 strncpy(path
, uaddr
.sun_path
, 108);
2849 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2852 ret
= bind(fd
, addr
, addrlen
);
2856 ret
= listen(fd
, 0);
2861 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2863 s
->do_telnetopt
= 1;
2866 ret
= connect(fd
, addr
, addrlen
);
2868 err
= socket_error();
2869 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2870 } else if (err
== EINPROGRESS
) {
2873 } else if (err
== WSAEALREADY
) {
2885 socket_set_nodelay(fd
);
2887 tcp_chr_connect(chr
);
2889 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2892 if (is_listen
&& is_waitconnect
) {
2893 printf("QEMU waiting for connection on: %s\n", host_str
);
2894 tcp_chr_accept(chr
);
2895 socket_set_nonblock(s
->listen_fd
);
2907 CharDriverState
*qemu_chr_open(const char *filename
)
2911 if (!strcmp(filename
, "vc")) {
2912 return text_console_init(&display_state
);
2913 } else if (!strcmp(filename
, "null")) {
2914 return qemu_chr_open_null();
2916 if (strstart(filename
, "tcp:", &p
)) {
2917 return qemu_chr_open_tcp(p
, 0, 0);
2919 if (strstart(filename
, "telnet:", &p
)) {
2920 return qemu_chr_open_tcp(p
, 1, 0);
2922 if (strstart(filename
, "udp:", &p
)) {
2923 return qemu_chr_open_udp(p
);
2925 if (strstart(filename
, "mon:", &p
)) {
2926 CharDriverState
*drv
= qemu_chr_open(p
);
2928 drv
= qemu_chr_open_mux(drv
);
2929 monitor_init(drv
, !nographic
);
2932 printf("Unable to open driver: %s\n", p
);
2936 if (strstart(filename
, "unix:", &p
)) {
2937 return qemu_chr_open_tcp(p
, 0, 1);
2938 } else if (strstart(filename
, "file:", &p
)) {
2939 return qemu_chr_open_file_out(p
);
2940 } else if (strstart(filename
, "pipe:", &p
)) {
2941 return qemu_chr_open_pipe(p
);
2942 } else if (!strcmp(filename
, "pty")) {
2943 return qemu_chr_open_pty();
2944 } else if (!strcmp(filename
, "stdio")) {
2945 return qemu_chr_open_stdio();
2948 #if defined(__linux__)
2949 if (strstart(filename
, "/dev/parport", NULL
)) {
2950 return qemu_chr_open_pp(filename
);
2952 if (strstart(filename
, "/dev/", NULL
)) {
2953 return qemu_chr_open_tty(filename
);
2957 if (strstart(filename
, "COM", NULL
)) {
2958 return qemu_chr_open_win(filename
);
2960 if (strstart(filename
, "pipe:", &p
)) {
2961 return qemu_chr_open_win_pipe(p
);
2963 if (strstart(filename
, "file:", &p
)) {
2964 return qemu_chr_open_win_file_out(p
);
2972 void qemu_chr_close(CharDriverState
*chr
)
2975 chr
->chr_close(chr
);
2978 /***********************************************************/
2979 /* network device redirectors */
2981 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2985 for(i
=0;i
<size
;i
+=16) {
2989 fprintf(f
, "%08x ", i
);
2992 fprintf(f
, " %02x", buf
[i
+j
]);
2997 for(j
=0;j
<len
;j
++) {
2999 if (c
< ' ' || c
> '~')
3001 fprintf(f
, "%c", c
);
3007 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
3010 for(i
= 0; i
< 6; i
++) {
3011 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3024 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
3029 p1
= strchr(p
, sep
);
3035 if (len
> buf_size
- 1)
3037 memcpy(buf
, p
, len
);
3044 int parse_host_src_port(struct sockaddr_in
*haddr
,
3045 struct sockaddr_in
*saddr
,
3046 const char *input_str
)
3048 char *str
= strdup(input_str
);
3049 char *host_str
= str
;
3054 * Chop off any extra arguments at the end of the string which
3055 * would start with a comma, then fill in the src port information
3056 * if it was provided else use the "any address" and "any port".
3058 if ((ptr
= strchr(str
,',')))
3061 if ((src_str
= strchr(input_str
,'@'))) {
3066 if (parse_host_port(haddr
, host_str
) < 0)
3069 if (!src_str
|| *src_str
== '\0')
3072 if (parse_host_port(saddr
, src_str
) < 0)
3083 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
3091 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3093 saddr
->sin_family
= AF_INET
;
3094 if (buf
[0] == '\0') {
3095 saddr
->sin_addr
.s_addr
= 0;
3097 if (isdigit(buf
[0])) {
3098 if (!inet_aton(buf
, &saddr
->sin_addr
))
3101 if ((he
= gethostbyname(buf
)) == NULL
)
3103 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
3106 port
= strtol(p
, (char **)&r
, 0);
3109 saddr
->sin_port
= htons(port
);
3114 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
3119 len
= MIN(108, strlen(str
));
3120 p
= strchr(str
, ',');
3122 len
= MIN(len
, p
- str
);
3124 memset(uaddr
, 0, sizeof(*uaddr
));
3126 uaddr
->sun_family
= AF_UNIX
;
3127 memcpy(uaddr
->sun_path
, str
, len
);
3133 /* find or alloc a new VLAN */
3134 VLANState
*qemu_find_vlan(int id
)
3136 VLANState
**pvlan
, *vlan
;
3137 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3141 vlan
= qemu_mallocz(sizeof(VLANState
));
3146 pvlan
= &first_vlan
;
3147 while (*pvlan
!= NULL
)
3148 pvlan
= &(*pvlan
)->next
;
3153 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
3154 IOReadHandler
*fd_read
,
3155 IOCanRWHandler
*fd_can_read
,
3158 VLANClientState
*vc
, **pvc
;
3159 vc
= qemu_mallocz(sizeof(VLANClientState
));
3162 vc
->fd_read
= fd_read
;
3163 vc
->fd_can_read
= fd_can_read
;
3164 vc
->opaque
= opaque
;
3168 pvc
= &vlan
->first_client
;
3169 while (*pvc
!= NULL
)
3170 pvc
= &(*pvc
)->next
;
3175 int qemu_can_send_packet(VLANClientState
*vc1
)
3177 VLANState
*vlan
= vc1
->vlan
;
3178 VLANClientState
*vc
;
3180 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3182 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
3189 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
3191 VLANState
*vlan
= vc1
->vlan
;
3192 VLANClientState
*vc
;
3195 printf("vlan %d send:\n", vlan
->id
);
3196 hex_dump(stdout
, buf
, size
);
3198 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3200 vc
->fd_read(vc
->opaque
, buf
, size
);
3205 #if defined(CONFIG_SLIRP)
3207 /* slirp network adapter */
3209 static int slirp_inited
;
3210 static VLANClientState
*slirp_vc
;
3212 int slirp_can_output(void)
3214 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
3217 void slirp_output(const uint8_t *pkt
, int pkt_len
)
3220 printf("slirp output:\n");
3221 hex_dump(stdout
, pkt
, pkt_len
);
3225 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
3228 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
3231 printf("slirp input:\n");
3232 hex_dump(stdout
, buf
, size
);
3234 slirp_input(buf
, size
);
3237 static int net_slirp_init(VLANState
*vlan
)
3239 if (!slirp_inited
) {
3243 slirp_vc
= qemu_new_vlan_client(vlan
,
3244 slirp_receive
, NULL
, NULL
);
3245 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
3249 static void net_slirp_redir(const char *redir_str
)
3254 struct in_addr guest_addr
;
3255 int host_port
, guest_port
;
3257 if (!slirp_inited
) {
3263 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3265 if (!strcmp(buf
, "tcp")) {
3267 } else if (!strcmp(buf
, "udp")) {
3273 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3275 host_port
= strtol(buf
, &r
, 0);
3279 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3281 if (buf
[0] == '\0') {
3282 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
3284 if (!inet_aton(buf
, &guest_addr
))
3287 guest_port
= strtol(p
, &r
, 0);
3291 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3292 fprintf(stderr
, "qemu: could not set up redirection\n");
3297 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3305 static void smb_exit(void)
3309 char filename
[1024];
3311 /* erase all the files in the directory */
3312 d
= opendir(smb_dir
);
3317 if (strcmp(de
->d_name
, ".") != 0 &&
3318 strcmp(de
->d_name
, "..") != 0) {
3319 snprintf(filename
, sizeof(filename
), "%s/%s",
3320 smb_dir
, de
->d_name
);
3328 /* automatic user mode samba server configuration */
3329 void net_slirp_smb(const char *exported_dir
)
3331 char smb_conf
[1024];
3332 char smb_cmdline
[1024];
3335 if (!slirp_inited
) {
3340 /* XXX: better tmp dir construction */
3341 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3342 if (mkdir(smb_dir
, 0700) < 0) {
3343 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3346 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3348 f
= fopen(smb_conf
, "w");
3350 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3357 "socket address=127.0.0.1\n"
3358 "pid directory=%s\n"
3359 "lock directory=%s\n"
3360 "log file=%s/log.smbd\n"
3361 "smb passwd file=%s/smbpasswd\n"
3362 "security = share\n"
3377 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3378 SMBD_COMMAND
, smb_conf
);
3380 slirp_add_exec(0, smb_cmdline
, 4, 139);
3383 #endif /* !defined(_WIN32) */
3385 #endif /* CONFIG_SLIRP */
3387 #if !defined(_WIN32)
3389 typedef struct TAPState
{
3390 VLANClientState
*vc
;
3394 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3396 TAPState
*s
= opaque
;
3399 ret
= write(s
->fd
, buf
, size
);
3400 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3407 static void tap_send(void *opaque
)
3409 TAPState
*s
= opaque
;
3416 sbuf
.maxlen
= sizeof(buf
);
3418 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
3420 size
= read(s
->fd
, buf
, sizeof(buf
));
3423 qemu_send_packet(s
->vc
, buf
, size
);
3429 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3433 s
= qemu_mallocz(sizeof(TAPState
));
3437 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3438 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3439 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3444 static int tap_open(char *ifname
, int ifname_size
)
3450 fd
= open("/dev/tap", O_RDWR
);
3452 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3457 dev
= devname(s
.st_rdev
, S_IFCHR
);
3458 pstrcpy(ifname
, ifname_size
, dev
);
3460 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3463 #elif defined(__sun__)
3464 #define TUNNEWPPA (('T'<<16) | 0x0001)
3466 * Allocate TAP device, returns opened fd.
3467 * Stores dev name in the first arg(must be large enough).
3469 int tap_alloc(char *dev
)
3471 int tap_fd
, if_fd
, ppa
= -1;
3472 static int ip_fd
= 0;
3475 static int arp_fd
= 0;
3476 int ip_muxid
, arp_muxid
;
3477 struct strioctl strioc_if
, strioc_ppa
;
3478 int link_type
= I_PLINK
;;
3480 char actual_name
[32] = "";
3482 memset(&ifr
, 0x0, sizeof(ifr
));
3486 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
3490 /* Check if IP device was opened */
3494 if( (ip_fd
= open("/dev/udp", O_RDWR
, 0)) < 0){
3495 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
3499 if( (tap_fd
= open("/dev/tap", O_RDWR
, 0)) < 0){
3500 syslog(LOG_ERR
, "Can't open /dev/tap");
3504 /* Assign a new PPA and get its unit number. */
3505 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
3506 strioc_ppa
.ic_timout
= 0;
3507 strioc_ppa
.ic_len
= sizeof(ppa
);
3508 strioc_ppa
.ic_dp
= (char *)&ppa
;
3509 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
3510 syslog (LOG_ERR
, "Can't assign new interface");
3512 if( (if_fd
= open("/dev/tap", O_RDWR
, 0)) < 0){
3513 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
3516 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
3517 syslog(LOG_ERR
, "Can't push IP module");
3521 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
3522 syslog(LOG_ERR
, "Can't get flags\n");
3524 snprintf (actual_name
, 32, "tap%d", ppa
);
3525 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3528 /* Assign ppa according to the unit number returned by tun device */
3530 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
3531 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
3532 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
3533 syslog (LOG_ERR
, "Can't get flags\n");
3534 /* Push arp module to if_fd */
3535 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
3536 syslog (LOG_ERR
, "Can't push ARP module (2)");
3538 /* Push arp module to ip_fd */
3539 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
3540 syslog (LOG_ERR
, "I_POP failed\n");
3541 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
3542 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
3544 if ((arp_fd
= open ("/dev/tap", O_RDWR
, 0)) < 0)
3545 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
3547 /* Set ifname to arp */
3548 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
3549 strioc_if
.ic_timout
= 0;
3550 strioc_if
.ic_len
= sizeof(ifr
);
3551 strioc_if
.ic_dp
= (char *)&ifr
;
3552 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
3553 syslog (LOG_ERR
, "Can't set ifname to arp\n");
3556 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
3557 syslog(LOG_ERR
, "Can't link TAP device to IP");
3561 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
3562 syslog (LOG_ERR
, "Can't link TAP device to ARP");
3566 memset(&ifr
, 0x0, sizeof(ifr
));
3567 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3568 ifr
.lifr_ip_muxid
= ip_muxid
;
3569 ifr
.lifr_arp_muxid
= arp_muxid
;
3571 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
3573 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
3574 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
3575 syslog (LOG_ERR
, "Can't set multiplexor id");
3578 sprintf(dev
, "tap%d", ppa
);
3582 static int tap_open(char *ifname
, int ifname_size
)
3586 if( (fd
= tap_alloc(dev
)) < 0 ){
3587 fprintf(stderr
, "Cannot allocate TAP device\n");
3590 pstrcpy(ifname
, ifname_size
, dev
);
3591 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3595 static int tap_open(char *ifname
, int ifname_size
)
3600 fd
= open("/dev/net/tun", O_RDWR
);
3602 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
3605 memset(&ifr
, 0, sizeof(ifr
));
3606 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
3607 if (ifname
[0] != '\0')
3608 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
3610 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
3611 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
3613 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
3617 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
3618 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3623 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
3624 const char *setup_script
)
3627 int pid
, status
, fd
;
3632 if (ifname1
!= NULL
)
3633 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
3636 fd
= tap_open(ifname
, sizeof(ifname
));
3640 if (!setup_script
|| !strcmp(setup_script
, "no"))
3642 if (setup_script
[0] != '\0') {
3643 /* try to launch network init script */
3647 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
3648 for (i
= 0; i
< open_max
; i
++)
3649 if (i
!= STDIN_FILENO
&&
3650 i
!= STDOUT_FILENO
&&
3651 i
!= STDERR_FILENO
&&
3656 *parg
++ = (char *)setup_script
;
3659 execv(setup_script
, args
);
3662 while (waitpid(pid
, &status
, 0) != pid
);
3663 if (!WIFEXITED(status
) ||
3664 WEXITSTATUS(status
) != 0) {
3665 fprintf(stderr
, "%s: could not launch network script\n",
3671 s
= net_tap_fd_init(vlan
, fd
);
3674 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3675 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
3679 #endif /* !_WIN32 */
3681 /* network connection */
3682 typedef struct NetSocketState
{
3683 VLANClientState
*vc
;
3685 int state
; /* 0 = getting length, 1 = getting data */
3689 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
3692 typedef struct NetSocketListenState
{
3695 } NetSocketListenState
;
3697 /* XXX: we consider we can send the whole packet without blocking */
3698 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
3700 NetSocketState
*s
= opaque
;
3704 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
3705 send_all(s
->fd
, buf
, size
);
3708 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
3710 NetSocketState
*s
= opaque
;
3711 sendto(s
->fd
, buf
, size
, 0,
3712 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
3715 static void net_socket_send(void *opaque
)
3717 NetSocketState
*s
= opaque
;
3722 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
3724 err
= socket_error();
3725 if (err
!= EWOULDBLOCK
)
3727 } else if (size
== 0) {
3728 /* end of connection */
3730 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3736 /* reassemble a packet from the network */
3742 memcpy(s
->buf
+ s
->index
, buf
, l
);
3746 if (s
->index
== 4) {
3748 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
3754 l
= s
->packet_len
- s
->index
;
3757 memcpy(s
->buf
+ s
->index
, buf
, l
);
3761 if (s
->index
>= s
->packet_len
) {
3762 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
3771 static void net_socket_send_dgram(void *opaque
)
3773 NetSocketState
*s
= opaque
;
3776 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
3780 /* end of connection */
3781 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3784 qemu_send_packet(s
->vc
, s
->buf
, size
);
3787 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
3792 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
3793 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
3794 inet_ntoa(mcastaddr
->sin_addr
),
3795 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
3799 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
3801 perror("socket(PF_INET, SOCK_DGRAM)");
3806 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
3807 (const char *)&val
, sizeof(val
));
3809 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
3813 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
3819 /* Add host to multicast group */
3820 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
3821 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
3823 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
3824 (const char *)&imr
, sizeof(struct ip_mreq
));
3826 perror("setsockopt(IP_ADD_MEMBERSHIP)");
3830 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
3832 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
3833 (const char *)&val
, sizeof(val
));
3835 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
3839 socket_set_nonblock(fd
);
3847 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
3850 struct sockaddr_in saddr
;
3852 socklen_t saddr_len
;
3855 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
3856 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
3857 * by ONLY ONE process: we must "clone" this dgram socket --jjo
3861 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
3863 if (saddr
.sin_addr
.s_addr
==0) {
3864 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
3868 /* clone dgram socket */
3869 newfd
= net_socket_mcast_create(&saddr
);
3871 /* error already reported by net_socket_mcast_create() */
3875 /* clone newfd to fd, close newfd */
3880 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
3881 fd
, strerror(errno
));
3886 s
= qemu_mallocz(sizeof(NetSocketState
));
3891 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
3892 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
3894 /* mcast: save bound address as dst */
3895 if (is_connected
) s
->dgram_dst
=saddr
;
3897 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3898 "socket: fd=%d (%s mcast=%s:%d)",
3899 fd
, is_connected
? "cloned" : "",
3900 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3904 static void net_socket_connect(void *opaque
)
3906 NetSocketState
*s
= opaque
;
3907 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
3910 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
3914 s
= qemu_mallocz(sizeof(NetSocketState
));
3918 s
->vc
= qemu_new_vlan_client(vlan
,
3919 net_socket_receive
, NULL
, s
);
3920 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3921 "socket: fd=%d", fd
);
3923 net_socket_connect(s
);
3925 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
3930 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
3933 int so_type
=-1, optlen
=sizeof(so_type
);
3935 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
3936 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
3941 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
3943 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3945 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
3946 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
3947 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3952 static void net_socket_accept(void *opaque
)
3954 NetSocketListenState
*s
= opaque
;
3956 struct sockaddr_in saddr
;
3961 len
= sizeof(saddr
);
3962 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
3963 if (fd
< 0 && errno
!= EINTR
) {
3965 } else if (fd
>= 0) {
3969 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
3973 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
3974 "socket: connection from %s:%d",
3975 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3979 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
3981 NetSocketListenState
*s
;
3983 struct sockaddr_in saddr
;
3985 if (parse_host_port(&saddr
, host_str
) < 0)
3988 s
= qemu_mallocz(sizeof(NetSocketListenState
));
3992 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3997 socket_set_nonblock(fd
);
3999 /* allow fast reuse */
4001 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
4003 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4008 ret
= listen(fd
, 0);
4015 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
4019 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
4022 int fd
, connected
, ret
, err
;
4023 struct sockaddr_in saddr
;
4025 if (parse_host_port(&saddr
, host_str
) < 0)
4028 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4033 socket_set_nonblock(fd
);
4037 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4039 err
= socket_error();
4040 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
4041 } else if (err
== EINPROGRESS
) {
4044 } else if (err
== WSAEALREADY
) {
4057 s
= net_socket_fd_init(vlan
, fd
, connected
);
4060 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4061 "socket: connect to %s:%d",
4062 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4066 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
4070 struct sockaddr_in saddr
;
4072 if (parse_host_port(&saddr
, host_str
) < 0)
4076 fd
= net_socket_mcast_create(&saddr
);
4080 s
= net_socket_fd_init(vlan
, fd
, 0);
4084 s
->dgram_dst
= saddr
;
4086 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4087 "socket: mcast=%s:%d",
4088 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4093 static int get_param_value(char *buf
, int buf_size
,
4094 const char *tag
, const char *str
)
4103 while (*p
!= '\0' && *p
!= '=') {
4104 if ((q
- option
) < sizeof(option
) - 1)
4112 if (!strcmp(tag
, option
)) {
4114 while (*p
!= '\0' && *p
!= ',') {
4115 if ((q
- buf
) < buf_size
- 1)
4122 while (*p
!= '\0' && *p
!= ',') {
4133 static int net_client_init(const char *str
)
4144 while (*p
!= '\0' && *p
!= ',') {
4145 if ((q
- device
) < sizeof(device
) - 1)
4153 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
4154 vlan_id
= strtol(buf
, NULL
, 0);
4156 vlan
= qemu_find_vlan(vlan_id
);
4158 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
4161 if (!strcmp(device
, "nic")) {
4165 if (nb_nics
>= MAX_NICS
) {
4166 fprintf(stderr
, "Too Many NICs\n");
4169 nd
= &nd_table
[nb_nics
];
4170 macaddr
= nd
->macaddr
;
4176 macaddr
[5] = 0x56 + nb_nics
;
4178 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
4179 if (parse_macaddr(macaddr
, buf
) < 0) {
4180 fprintf(stderr
, "invalid syntax for ethernet address\n");
4184 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
4185 nd
->model
= strdup(buf
);
4191 if (!strcmp(device
, "none")) {
4192 /* does nothing. It is needed to signal that no network cards
4197 if (!strcmp(device
, "user")) {
4198 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
4199 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
4201 ret
= net_slirp_init(vlan
);
4205 if (!strcmp(device
, "tap")) {
4207 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4208 fprintf(stderr
, "tap: no interface name\n");
4211 ret
= tap_win32_init(vlan
, ifname
);
4214 if (!strcmp(device
, "tap")) {
4216 char setup_script
[1024];
4218 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4219 fd
= strtol(buf
, NULL
, 0);
4221 if (net_tap_fd_init(vlan
, fd
))
4224 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4227 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
4228 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
4230 ret
= net_tap_init(vlan
, ifname
, setup_script
);
4234 if (!strcmp(device
, "socket")) {
4235 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4237 fd
= strtol(buf
, NULL
, 0);
4239 if (net_socket_fd_init(vlan
, fd
, 1))
4241 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
4242 ret
= net_socket_listen_init(vlan
, buf
);
4243 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
4244 ret
= net_socket_connect_init(vlan
, buf
);
4245 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
4246 ret
= net_socket_mcast_init(vlan
, buf
);
4248 fprintf(stderr
, "Unknown socket options: %s\n", p
);
4253 fprintf(stderr
, "Unknown network device: %s\n", device
);
4257 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
4263 void do_info_network(void)
4266 VLANClientState
*vc
;
4268 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
4269 term_printf("VLAN %d devices:\n", vlan
->id
);
4270 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
4271 term_printf(" %s\n", vc
->info_str
);
4275 /***********************************************************/
4278 static USBPort
*used_usb_ports
;
4279 static USBPort
*free_usb_ports
;
4281 /* ??? Maybe change this to register a hub to keep track of the topology. */
4282 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
4283 usb_attachfn attach
)
4285 port
->opaque
= opaque
;
4286 port
->index
= index
;
4287 port
->attach
= attach
;
4288 port
->next
= free_usb_ports
;
4289 free_usb_ports
= port
;
4292 static int usb_device_add(const char *devname
)
4298 if (!free_usb_ports
)
4301 if (strstart(devname
, "host:", &p
)) {
4302 dev
= usb_host_device_open(p
);
4303 } else if (!strcmp(devname
, "mouse")) {
4304 dev
= usb_mouse_init();
4305 } else if (!strcmp(devname
, "tablet")) {
4306 dev
= usb_tablet_init();
4307 } else if (strstart(devname
, "disk:", &p
)) {
4308 dev
= usb_msd_init(p
);
4315 /* Find a USB port to add the device to. */
4316 port
= free_usb_ports
;
4320 /* Create a new hub and chain it on. */
4321 free_usb_ports
= NULL
;
4322 port
->next
= used_usb_ports
;
4323 used_usb_ports
= port
;
4325 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
4326 usb_attach(port
, hub
);
4327 port
= free_usb_ports
;
4330 free_usb_ports
= port
->next
;
4331 port
->next
= used_usb_ports
;
4332 used_usb_ports
= port
;
4333 usb_attach(port
, dev
);
4337 static int usb_device_del(const char *devname
)
4345 if (!used_usb_ports
)
4348 p
= strchr(devname
, '.');
4351 bus_num
= strtoul(devname
, NULL
, 0);
4352 addr
= strtoul(p
+ 1, NULL
, 0);
4356 lastp
= &used_usb_ports
;
4357 port
= used_usb_ports
;
4358 while (port
&& port
->dev
->addr
!= addr
) {
4359 lastp
= &port
->next
;
4367 *lastp
= port
->next
;
4368 usb_attach(port
, NULL
);
4369 dev
->handle_destroy(dev
);
4370 port
->next
= free_usb_ports
;
4371 free_usb_ports
= port
;
4375 void do_usb_add(const char *devname
)
4378 ret
= usb_device_add(devname
);
4380 term_printf("Could not add USB device '%s'\n", devname
);
4383 void do_usb_del(const char *devname
)
4386 ret
= usb_device_del(devname
);
4388 term_printf("Could not remove USB device '%s'\n", devname
);
4395 const char *speed_str
;
4398 term_printf("USB support not enabled\n");
4402 for (port
= used_usb_ports
; port
; port
= port
->next
) {
4406 switch(dev
->speed
) {
4410 case USB_SPEED_FULL
:
4413 case USB_SPEED_HIGH
:
4420 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
4421 0, dev
->addr
, speed_str
, dev
->devname
);
4425 /***********************************************************/
4426 /* PCMCIA/Cardbus */
4428 static struct pcmcia_socket_entry_s
{
4429 struct pcmcia_socket_s
*socket
;
4430 struct pcmcia_socket_entry_s
*next
;
4431 } *pcmcia_sockets
= 0;
4433 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
4435 struct pcmcia_socket_entry_s
*entry
;
4437 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
4438 entry
->socket
= socket
;
4439 entry
->next
= pcmcia_sockets
;
4440 pcmcia_sockets
= entry
;
4443 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
4445 struct pcmcia_socket_entry_s
*entry
, **ptr
;
4447 ptr
= &pcmcia_sockets
;
4448 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
4449 if (entry
->socket
== socket
) {
4455 void pcmcia_info(void)
4457 struct pcmcia_socket_entry_s
*iter
;
4458 if (!pcmcia_sockets
)
4459 term_printf("No PCMCIA sockets\n");
4461 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
4462 term_printf("%s: %s\n", iter
->socket
->slot_string
,
4463 iter
->socket
->attached
? iter
->socket
->card_string
:
4467 /***********************************************************/
4470 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4474 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4478 static void dumb_refresh(DisplayState
*ds
)
4483 void dumb_display_init(DisplayState
*ds
)
4488 ds
->dpy_update
= dumb_update
;
4489 ds
->dpy_resize
= dumb_resize
;
4490 ds
->dpy_refresh
= dumb_refresh
;
4493 /***********************************************************/
4496 #define MAX_IO_HANDLERS 64
4498 typedef struct IOHandlerRecord
{
4500 IOCanRWHandler
*fd_read_poll
;
4502 IOHandler
*fd_write
;
4505 /* temporary data */
4507 struct IOHandlerRecord
*next
;
4510 static IOHandlerRecord
*first_io_handler
;
4512 /* XXX: fd_read_poll should be suppressed, but an API change is
4513 necessary in the character devices to suppress fd_can_read(). */
4514 int qemu_set_fd_handler2(int fd
,
4515 IOCanRWHandler
*fd_read_poll
,
4517 IOHandler
*fd_write
,
4520 IOHandlerRecord
**pioh
, *ioh
;
4522 if (!fd_read
&& !fd_write
) {
4523 pioh
= &first_io_handler
;
4528 if (ioh
->fd
== fd
) {
4535 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4539 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4542 ioh
->next
= first_io_handler
;
4543 first_io_handler
= ioh
;
4546 ioh
->fd_read_poll
= fd_read_poll
;
4547 ioh
->fd_read
= fd_read
;
4548 ioh
->fd_write
= fd_write
;
4549 ioh
->opaque
= opaque
;
4555 int qemu_set_fd_handler(int fd
,
4557 IOHandler
*fd_write
,
4560 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4563 /***********************************************************/
4564 /* Polling handling */
4566 typedef struct PollingEntry
{
4569 struct PollingEntry
*next
;
4572 static PollingEntry
*first_polling_entry
;
4574 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4576 PollingEntry
**ppe
, *pe
;
4577 pe
= qemu_mallocz(sizeof(PollingEntry
));
4581 pe
->opaque
= opaque
;
4582 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
4587 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
4589 PollingEntry
**ppe
, *pe
;
4590 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
4592 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
4601 /***********************************************************/
4602 /* Wait objects support */
4603 typedef struct WaitObjects
{
4605 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
4606 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
4607 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
4610 static WaitObjects wait_objects
= {0};
4612 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4614 WaitObjects
*w
= &wait_objects
;
4616 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
4618 w
->events
[w
->num
] = handle
;
4619 w
->func
[w
->num
] = func
;
4620 w
->opaque
[w
->num
] = opaque
;
4625 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4628 WaitObjects
*w
= &wait_objects
;
4631 for (i
= 0; i
< w
->num
; i
++) {
4632 if (w
->events
[i
] == handle
)
4635 w
->events
[i
] = w
->events
[i
+ 1];
4636 w
->func
[i
] = w
->func
[i
+ 1];
4637 w
->opaque
[i
] = w
->opaque
[i
+ 1];
4645 /***********************************************************/
4646 /* savevm/loadvm support */
4648 #define IO_BUF_SIZE 32768
4652 BlockDriverState
*bs
;
4655 int64_t base_offset
;
4656 int64_t buf_offset
; /* start of buffer when writing, end of buffer
4659 int buf_size
; /* 0 when writing */
4660 uint8_t buf
[IO_BUF_SIZE
];
4663 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
4667 f
= qemu_mallocz(sizeof(QEMUFile
));
4670 if (!strcmp(mode
, "wb")) {
4672 } else if (!strcmp(mode
, "rb")) {
4677 f
->outfile
= fopen(filename
, mode
);
4689 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
4693 f
= qemu_mallocz(sizeof(QEMUFile
));
4698 f
->is_writable
= is_writable
;
4699 f
->base_offset
= offset
;
4703 void qemu_fflush(QEMUFile
*f
)
4705 if (!f
->is_writable
)
4707 if (f
->buf_index
> 0) {
4709 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4710 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
4712 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4713 f
->buf
, f
->buf_index
);
4715 f
->buf_offset
+= f
->buf_index
;
4720 static void qemu_fill_buffer(QEMUFile
*f
)
4727 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4728 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
4732 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4733 f
->buf
, IO_BUF_SIZE
);
4739 f
->buf_offset
+= len
;
4742 void qemu_fclose(QEMUFile
*f
)
4752 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
4756 l
= IO_BUF_SIZE
- f
->buf_index
;
4759 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
4763 if (f
->buf_index
>= IO_BUF_SIZE
)
4768 void qemu_put_byte(QEMUFile
*f
, int v
)
4770 f
->buf
[f
->buf_index
++] = v
;
4771 if (f
->buf_index
>= IO_BUF_SIZE
)
4775 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
4781 l
= f
->buf_size
- f
->buf_index
;
4783 qemu_fill_buffer(f
);
4784 l
= f
->buf_size
- f
->buf_index
;
4790 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
4795 return size1
- size
;
4798 int qemu_get_byte(QEMUFile
*f
)
4800 if (f
->buf_index
>= f
->buf_size
) {
4801 qemu_fill_buffer(f
);
4802 if (f
->buf_index
>= f
->buf_size
)
4805 return f
->buf
[f
->buf_index
++];
4808 int64_t qemu_ftell(QEMUFile
*f
)
4810 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
4813 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
4815 if (whence
== SEEK_SET
) {
4817 } else if (whence
== SEEK_CUR
) {
4818 pos
+= qemu_ftell(f
);
4820 /* SEEK_END not supported */
4823 if (f
->is_writable
) {
4825 f
->buf_offset
= pos
;
4827 f
->buf_offset
= pos
;
4834 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
4836 qemu_put_byte(f
, v
>> 8);
4837 qemu_put_byte(f
, v
);
4840 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
4842 qemu_put_byte(f
, v
>> 24);
4843 qemu_put_byte(f
, v
>> 16);
4844 qemu_put_byte(f
, v
>> 8);
4845 qemu_put_byte(f
, v
);
4848 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
4850 qemu_put_be32(f
, v
>> 32);
4851 qemu_put_be32(f
, v
);
4854 unsigned int qemu_get_be16(QEMUFile
*f
)
4857 v
= qemu_get_byte(f
) << 8;
4858 v
|= qemu_get_byte(f
);
4862 unsigned int qemu_get_be32(QEMUFile
*f
)
4865 v
= qemu_get_byte(f
) << 24;
4866 v
|= qemu_get_byte(f
) << 16;
4867 v
|= qemu_get_byte(f
) << 8;
4868 v
|= qemu_get_byte(f
);
4872 uint64_t qemu_get_be64(QEMUFile
*f
)
4875 v
= (uint64_t)qemu_get_be32(f
) << 32;
4876 v
|= qemu_get_be32(f
);
4880 typedef struct SaveStateEntry
{
4884 SaveStateHandler
*save_state
;
4885 LoadStateHandler
*load_state
;
4887 struct SaveStateEntry
*next
;
4890 static SaveStateEntry
*first_se
;
4892 int register_savevm(const char *idstr
,
4895 SaveStateHandler
*save_state
,
4896 LoadStateHandler
*load_state
,
4899 SaveStateEntry
*se
, **pse
;
4901 se
= qemu_malloc(sizeof(SaveStateEntry
));
4904 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
4905 se
->instance_id
= instance_id
;
4906 se
->version_id
= version_id
;
4907 se
->save_state
= save_state
;
4908 se
->load_state
= load_state
;
4909 se
->opaque
= opaque
;
4912 /* add at the end of list */
4914 while (*pse
!= NULL
)
4915 pse
= &(*pse
)->next
;
4920 #define QEMU_VM_FILE_MAGIC 0x5145564d
4921 #define QEMU_VM_FILE_VERSION 0x00000002
4923 int qemu_savevm_state(QEMUFile
*f
)
4927 int64_t cur_pos
, len_pos
, total_len_pos
;
4929 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
4930 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
4931 total_len_pos
= qemu_ftell(f
);
4932 qemu_put_be64(f
, 0); /* total size */
4934 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4936 len
= strlen(se
->idstr
);
4937 qemu_put_byte(f
, len
);
4938 qemu_put_buffer(f
, se
->idstr
, len
);
4940 qemu_put_be32(f
, se
->instance_id
);
4941 qemu_put_be32(f
, se
->version_id
);
4943 /* record size: filled later */
4944 len_pos
= qemu_ftell(f
);
4945 qemu_put_be32(f
, 0);
4947 se
->save_state(f
, se
->opaque
);
4949 /* fill record size */
4950 cur_pos
= qemu_ftell(f
);
4951 len
= cur_pos
- len_pos
- 4;
4952 qemu_fseek(f
, len_pos
, SEEK_SET
);
4953 qemu_put_be32(f
, len
);
4954 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4956 cur_pos
= qemu_ftell(f
);
4957 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
4958 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
4959 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4965 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
4969 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4970 if (!strcmp(se
->idstr
, idstr
) &&
4971 instance_id
== se
->instance_id
)
4977 int qemu_loadvm_state(QEMUFile
*f
)
4980 int len
, ret
, instance_id
, record_len
, version_id
;
4981 int64_t total_len
, end_pos
, cur_pos
;
4985 v
= qemu_get_be32(f
);
4986 if (v
!= QEMU_VM_FILE_MAGIC
)
4988 v
= qemu_get_be32(f
);
4989 if (v
!= QEMU_VM_FILE_VERSION
) {
4994 total_len
= qemu_get_be64(f
);
4995 end_pos
= total_len
+ qemu_ftell(f
);
4997 if (qemu_ftell(f
) >= end_pos
)
4999 len
= qemu_get_byte(f
);
5000 qemu_get_buffer(f
, idstr
, len
);
5002 instance_id
= qemu_get_be32(f
);
5003 version_id
= qemu_get_be32(f
);
5004 record_len
= qemu_get_be32(f
);
5006 printf("idstr=%s instance=0x%x version=%d len=%d\n",
5007 idstr
, instance_id
, version_id
, record_len
);
5009 cur_pos
= qemu_ftell(f
);
5010 se
= find_se(idstr
, instance_id
);
5012 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
5013 instance_id
, idstr
);
5015 ret
= se
->load_state(f
, se
->opaque
, version_id
);
5017 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
5018 instance_id
, idstr
);
5021 /* always seek to exact end of record */
5022 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
5029 /* device can contain snapshots */
5030 static int bdrv_can_snapshot(BlockDriverState
*bs
)
5033 !bdrv_is_removable(bs
) &&
5034 !bdrv_is_read_only(bs
));
5037 /* device must be snapshots in order to have a reliable snapshot */
5038 static int bdrv_has_snapshot(BlockDriverState
*bs
)
5041 !bdrv_is_removable(bs
) &&
5042 !bdrv_is_read_only(bs
));
5045 static BlockDriverState
*get_bs_snapshots(void)
5047 BlockDriverState
*bs
;
5051 return bs_snapshots
;
5052 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5054 if (bdrv_can_snapshot(bs
))
5063 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
5066 QEMUSnapshotInfo
*sn_tab
, *sn
;
5070 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5073 for(i
= 0; i
< nb_sns
; i
++) {
5075 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
5085 void do_savevm(const char *name
)
5087 BlockDriverState
*bs
, *bs1
;
5088 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
5089 int must_delete
, ret
, i
;
5090 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5092 int saved_vm_running
;
5099 bs
= get_bs_snapshots();
5101 term_printf("No block device can accept snapshots\n");
5105 /* ??? Should this occur after vm_stop? */
5108 saved_vm_running
= vm_running
;
5113 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
5118 memset(sn
, 0, sizeof(*sn
));
5120 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
5121 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
5124 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
5127 /* fill auxiliary fields */
5130 sn
->date_sec
= tb
.time
;
5131 sn
->date_nsec
= tb
.millitm
* 1000000;
5133 gettimeofday(&tv
, NULL
);
5134 sn
->date_sec
= tv
.tv_sec
;
5135 sn
->date_nsec
= tv
.tv_usec
* 1000;
5137 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
5139 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5140 term_printf("Device %s does not support VM state snapshots\n",
5141 bdrv_get_device_name(bs
));
5145 /* save the VM state */
5146 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
5148 term_printf("Could not open VM state file\n");
5151 ret
= qemu_savevm_state(f
);
5152 sn
->vm_state_size
= qemu_ftell(f
);
5155 term_printf("Error %d while writing VM\n", ret
);
5159 /* create the snapshots */
5161 for(i
= 0; i
< MAX_DISKS
; i
++) {
5163 if (bdrv_has_snapshot(bs1
)) {
5165 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
5167 term_printf("Error while deleting snapshot on '%s'\n",
5168 bdrv_get_device_name(bs1
));
5171 ret
= bdrv_snapshot_create(bs1
, sn
);
5173 term_printf("Error while creating snapshot on '%s'\n",
5174 bdrv_get_device_name(bs1
));
5180 if (saved_vm_running
)
5184 void do_loadvm(const char *name
)
5186 BlockDriverState
*bs
, *bs1
;
5187 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5190 int saved_vm_running
;
5192 bs
= get_bs_snapshots();
5194 term_printf("No block device supports snapshots\n");
5198 /* Flush all IO requests so they don't interfere with the new state. */
5201 saved_vm_running
= vm_running
;
5204 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5206 if (bdrv_has_snapshot(bs1
)) {
5207 ret
= bdrv_snapshot_goto(bs1
, name
);
5210 term_printf("Warning: ");
5213 term_printf("Snapshots not supported on device '%s'\n",
5214 bdrv_get_device_name(bs1
));
5217 term_printf("Could not find snapshot '%s' on device '%s'\n",
5218 name
, bdrv_get_device_name(bs1
));
5221 term_printf("Error %d while activating snapshot on '%s'\n",
5222 ret
, bdrv_get_device_name(bs1
));
5225 /* fatal on snapshot block device */
5232 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5233 term_printf("Device %s does not support VM state snapshots\n",
5234 bdrv_get_device_name(bs
));
5238 /* restore the VM state */
5239 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
5241 term_printf("Could not open VM state file\n");
5244 ret
= qemu_loadvm_state(f
);
5247 term_printf("Error %d while loading VM state\n", ret
);
5250 if (saved_vm_running
)
5254 void do_delvm(const char *name
)
5256 BlockDriverState
*bs
, *bs1
;
5259 bs
= get_bs_snapshots();
5261 term_printf("No block device supports snapshots\n");
5265 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5267 if (bdrv_has_snapshot(bs1
)) {
5268 ret
= bdrv_snapshot_delete(bs1
, name
);
5270 if (ret
== -ENOTSUP
)
5271 term_printf("Snapshots not supported on device '%s'\n",
5272 bdrv_get_device_name(bs1
));
5274 term_printf("Error %d while deleting snapshot on '%s'\n",
5275 ret
, bdrv_get_device_name(bs1
));
5281 void do_info_snapshots(void)
5283 BlockDriverState
*bs
, *bs1
;
5284 QEMUSnapshotInfo
*sn_tab
, *sn
;
5288 bs
= get_bs_snapshots();
5290 term_printf("No available block device supports snapshots\n");
5293 term_printf("Snapshot devices:");
5294 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5296 if (bdrv_has_snapshot(bs1
)) {
5298 term_printf(" %s", bdrv_get_device_name(bs1
));
5303 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5305 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
5308 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
5309 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
5310 for(i
= 0; i
< nb_sns
; i
++) {
5312 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
5317 /***********************************************************/
5318 /* cpu save/restore */
5320 #if defined(TARGET_I386)
5322 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
5324 qemu_put_be32(f
, dt
->selector
);
5325 qemu_put_betl(f
, dt
->base
);
5326 qemu_put_be32(f
, dt
->limit
);
5327 qemu_put_be32(f
, dt
->flags
);
5330 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
5332 dt
->selector
= qemu_get_be32(f
);
5333 dt
->base
= qemu_get_betl(f
);
5334 dt
->limit
= qemu_get_be32(f
);
5335 dt
->flags
= qemu_get_be32(f
);
5338 void cpu_save(QEMUFile
*f
, void *opaque
)
5340 CPUState
*env
= opaque
;
5341 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
5345 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5346 qemu_put_betls(f
, &env
->regs
[i
]);
5347 qemu_put_betls(f
, &env
->eip
);
5348 qemu_put_betls(f
, &env
->eflags
);
5349 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
5350 qemu_put_be32s(f
, &hflags
);
5354 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
5356 for(i
= 0; i
< 8; i
++) {
5357 fptag
|= ((!env
->fptags
[i
]) << i
);
5360 qemu_put_be16s(f
, &fpuc
);
5361 qemu_put_be16s(f
, &fpus
);
5362 qemu_put_be16s(f
, &fptag
);
5364 #ifdef USE_X86LDOUBLE
5369 qemu_put_be16s(f
, &fpregs_format
);
5371 for(i
= 0; i
< 8; i
++) {
5372 #ifdef USE_X86LDOUBLE
5376 /* we save the real CPU data (in case of MMX usage only 'mant'
5377 contains the MMX register */
5378 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
5379 qemu_put_be64(f
, mant
);
5380 qemu_put_be16(f
, exp
);
5383 /* if we use doubles for float emulation, we save the doubles to
5384 avoid losing information in case of MMX usage. It can give
5385 problems if the image is restored on a CPU where long
5386 doubles are used instead. */
5387 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
5391 for(i
= 0; i
< 6; i
++)
5392 cpu_put_seg(f
, &env
->segs
[i
]);
5393 cpu_put_seg(f
, &env
->ldt
);
5394 cpu_put_seg(f
, &env
->tr
);
5395 cpu_put_seg(f
, &env
->gdt
);
5396 cpu_put_seg(f
, &env
->idt
);
5398 qemu_put_be32s(f
, &env
->sysenter_cs
);
5399 qemu_put_be32s(f
, &env
->sysenter_esp
);
5400 qemu_put_be32s(f
, &env
->sysenter_eip
);
5402 qemu_put_betls(f
, &env
->cr
[0]);
5403 qemu_put_betls(f
, &env
->cr
[2]);
5404 qemu_put_betls(f
, &env
->cr
[3]);
5405 qemu_put_betls(f
, &env
->cr
[4]);
5407 for(i
= 0; i
< 8; i
++)
5408 qemu_put_betls(f
, &env
->dr
[i
]);
5411 qemu_put_be32s(f
, &env
->a20_mask
);
5414 qemu_put_be32s(f
, &env
->mxcsr
);
5415 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5416 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5417 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5420 #ifdef TARGET_X86_64
5421 qemu_put_be64s(f
, &env
->efer
);
5422 qemu_put_be64s(f
, &env
->star
);
5423 qemu_put_be64s(f
, &env
->lstar
);
5424 qemu_put_be64s(f
, &env
->cstar
);
5425 qemu_put_be64s(f
, &env
->fmask
);
5426 qemu_put_be64s(f
, &env
->kernelgsbase
);
5428 qemu_put_be32s(f
, &env
->smbase
);
5431 #ifdef USE_X86LDOUBLE
5432 /* XXX: add that in a FPU generic layer */
5433 union x86_longdouble
{
5438 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
5439 #define EXPBIAS1 1023
5440 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
5441 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
5443 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
5447 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
5448 /* exponent + sign */
5449 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
5450 e
|= SIGND1(temp
) >> 16;
5455 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5457 CPUState
*env
= opaque
;
5460 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
5462 if (version_id
!= 3 && version_id
!= 4)
5464 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5465 qemu_get_betls(f
, &env
->regs
[i
]);
5466 qemu_get_betls(f
, &env
->eip
);
5467 qemu_get_betls(f
, &env
->eflags
);
5468 qemu_get_be32s(f
, &hflags
);
5470 qemu_get_be16s(f
, &fpuc
);
5471 qemu_get_be16s(f
, &fpus
);
5472 qemu_get_be16s(f
, &fptag
);
5473 qemu_get_be16s(f
, &fpregs_format
);
5475 /* NOTE: we cannot always restore the FPU state if the image come
5476 from a host with a different 'USE_X86LDOUBLE' define. We guess
5477 if we are in an MMX state to restore correctly in that case. */
5478 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
5479 for(i
= 0; i
< 8; i
++) {
5483 switch(fpregs_format
) {
5485 mant
= qemu_get_be64(f
);
5486 exp
= qemu_get_be16(f
);
5487 #ifdef USE_X86LDOUBLE
5488 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5490 /* difficult case */
5492 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5494 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5498 mant
= qemu_get_be64(f
);
5499 #ifdef USE_X86LDOUBLE
5501 union x86_longdouble
*p
;
5502 /* difficult case */
5503 p
= (void *)&env
->fpregs
[i
];
5508 fp64_to_fp80(p
, mant
);
5512 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5521 /* XXX: restore FPU round state */
5522 env
->fpstt
= (fpus
>> 11) & 7;
5523 env
->fpus
= fpus
& ~0x3800;
5525 for(i
= 0; i
< 8; i
++) {
5526 env
->fptags
[i
] = (fptag
>> i
) & 1;
5529 for(i
= 0; i
< 6; i
++)
5530 cpu_get_seg(f
, &env
->segs
[i
]);
5531 cpu_get_seg(f
, &env
->ldt
);
5532 cpu_get_seg(f
, &env
->tr
);
5533 cpu_get_seg(f
, &env
->gdt
);
5534 cpu_get_seg(f
, &env
->idt
);
5536 qemu_get_be32s(f
, &env
->sysenter_cs
);
5537 qemu_get_be32s(f
, &env
->sysenter_esp
);
5538 qemu_get_be32s(f
, &env
->sysenter_eip
);
5540 qemu_get_betls(f
, &env
->cr
[0]);
5541 qemu_get_betls(f
, &env
->cr
[2]);
5542 qemu_get_betls(f
, &env
->cr
[3]);
5543 qemu_get_betls(f
, &env
->cr
[4]);
5545 for(i
= 0; i
< 8; i
++)
5546 qemu_get_betls(f
, &env
->dr
[i
]);
5549 qemu_get_be32s(f
, &env
->a20_mask
);
5551 qemu_get_be32s(f
, &env
->mxcsr
);
5552 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5553 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5554 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5557 #ifdef TARGET_X86_64
5558 qemu_get_be64s(f
, &env
->efer
);
5559 qemu_get_be64s(f
, &env
->star
);
5560 qemu_get_be64s(f
, &env
->lstar
);
5561 qemu_get_be64s(f
, &env
->cstar
);
5562 qemu_get_be64s(f
, &env
->fmask
);
5563 qemu_get_be64s(f
, &env
->kernelgsbase
);
5565 if (version_id
>= 4)
5566 qemu_get_be32s(f
, &env
->smbase
);
5568 /* XXX: compute hflags from scratch, except for CPL and IIF */
5569 env
->hflags
= hflags
;
5574 #elif defined(TARGET_PPC)
5575 void cpu_save(QEMUFile
*f
, void *opaque
)
5579 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5584 #elif defined(TARGET_MIPS)
5585 void cpu_save(QEMUFile
*f
, void *opaque
)
5589 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5594 #elif defined(TARGET_SPARC)
5595 void cpu_save(QEMUFile
*f
, void *opaque
)
5597 CPUState
*env
= opaque
;
5601 for(i
= 0; i
< 8; i
++)
5602 qemu_put_betls(f
, &env
->gregs
[i
]);
5603 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5604 qemu_put_betls(f
, &env
->regbase
[i
]);
5607 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5613 qemu_put_be32(f
, u
.i
);
5616 qemu_put_betls(f
, &env
->pc
);
5617 qemu_put_betls(f
, &env
->npc
);
5618 qemu_put_betls(f
, &env
->y
);
5620 qemu_put_be32(f
, tmp
);
5621 qemu_put_betls(f
, &env
->fsr
);
5622 qemu_put_betls(f
, &env
->tbr
);
5623 #ifndef TARGET_SPARC64
5624 qemu_put_be32s(f
, &env
->wim
);
5626 for(i
= 0; i
< 16; i
++)
5627 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
5631 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5633 CPUState
*env
= opaque
;
5637 for(i
= 0; i
< 8; i
++)
5638 qemu_get_betls(f
, &env
->gregs
[i
]);
5639 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5640 qemu_get_betls(f
, &env
->regbase
[i
]);
5643 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5648 u
.i
= qemu_get_be32(f
);
5652 qemu_get_betls(f
, &env
->pc
);
5653 qemu_get_betls(f
, &env
->npc
);
5654 qemu_get_betls(f
, &env
->y
);
5655 tmp
= qemu_get_be32(f
);
5656 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
5657 correctly updated */
5659 qemu_get_betls(f
, &env
->fsr
);
5660 qemu_get_betls(f
, &env
->tbr
);
5661 #ifndef TARGET_SPARC64
5662 qemu_get_be32s(f
, &env
->wim
);
5664 for(i
= 0; i
< 16; i
++)
5665 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
5671 #elif defined(TARGET_ARM)
5673 /* ??? Need to implement these. */
5674 void cpu_save(QEMUFile
*f
, void *opaque
)
5678 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5685 #warning No CPU save/restore functions
5689 /***********************************************************/
5690 /* ram save/restore */
5692 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
5696 v
= qemu_get_byte(f
);
5699 if (qemu_get_buffer(f
, buf
, len
) != len
)
5703 v
= qemu_get_byte(f
);
5704 memset(buf
, v
, len
);
5712 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
5716 if (qemu_get_be32(f
) != phys_ram_size
)
5718 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
5719 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
5726 #define BDRV_HASH_BLOCK_SIZE 1024
5727 #define IOBUF_SIZE 4096
5728 #define RAM_CBLOCK_MAGIC 0xfabe
5730 typedef struct RamCompressState
{
5733 uint8_t buf
[IOBUF_SIZE
];
5736 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
5739 memset(s
, 0, sizeof(*s
));
5741 ret
= deflateInit2(&s
->zstream
, 1,
5743 9, Z_DEFAULT_STRATEGY
);
5746 s
->zstream
.avail_out
= IOBUF_SIZE
;
5747 s
->zstream
.next_out
= s
->buf
;
5751 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
5753 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
5754 qemu_put_be16(s
->f
, len
);
5755 qemu_put_buffer(s
->f
, buf
, len
);
5758 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
5762 s
->zstream
.avail_in
= len
;
5763 s
->zstream
.next_in
= (uint8_t *)buf
;
5764 while (s
->zstream
.avail_in
> 0) {
5765 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
5768 if (s
->zstream
.avail_out
== 0) {
5769 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
5770 s
->zstream
.avail_out
= IOBUF_SIZE
;
5771 s
->zstream
.next_out
= s
->buf
;
5777 static void ram_compress_close(RamCompressState
*s
)
5781 /* compress last bytes */
5783 ret
= deflate(&s
->zstream
, Z_FINISH
);
5784 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
5785 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
5787 ram_put_cblock(s
, s
->buf
, len
);
5789 s
->zstream
.avail_out
= IOBUF_SIZE
;
5790 s
->zstream
.next_out
= s
->buf
;
5791 if (ret
== Z_STREAM_END
)
5798 deflateEnd(&s
->zstream
);
5801 typedef struct RamDecompressState
{
5804 uint8_t buf
[IOBUF_SIZE
];
5805 } RamDecompressState
;
5807 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
5810 memset(s
, 0, sizeof(*s
));
5812 ret
= inflateInit(&s
->zstream
);
5818 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
5822 s
->zstream
.avail_out
= len
;
5823 s
->zstream
.next_out
= buf
;
5824 while (s
->zstream
.avail_out
> 0) {
5825 if (s
->zstream
.avail_in
== 0) {
5826 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
5828 clen
= qemu_get_be16(s
->f
);
5829 if (clen
> IOBUF_SIZE
)
5831 qemu_get_buffer(s
->f
, s
->buf
, clen
);
5832 s
->zstream
.avail_in
= clen
;
5833 s
->zstream
.next_in
= s
->buf
;
5835 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
5836 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
5843 static void ram_decompress_close(RamDecompressState
*s
)
5845 inflateEnd(&s
->zstream
);
5848 static void ram_save(QEMUFile
*f
, void *opaque
)
5851 RamCompressState s1
, *s
= &s1
;
5854 qemu_put_be32(f
, phys_ram_size
);
5855 if (ram_compress_open(s
, f
) < 0)
5857 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5859 if (tight_savevm_enabled
) {
5863 /* find if the memory block is available on a virtual
5866 for(j
= 0; j
< MAX_DISKS
; j
++) {
5868 sector_num
= bdrv_hash_find(bs_table
[j
],
5869 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5870 if (sector_num
>= 0)
5875 goto normal_compress
;
5878 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
5879 ram_compress_buf(s
, buf
, 10);
5885 ram_compress_buf(s
, buf
, 1);
5886 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5889 ram_compress_close(s
);
5892 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
5894 RamDecompressState s1
, *s
= &s1
;
5898 if (version_id
== 1)
5899 return ram_load_v1(f
, opaque
);
5900 if (version_id
!= 2)
5902 if (qemu_get_be32(f
) != phys_ram_size
)
5904 if (ram_decompress_open(s
, f
) < 0)
5906 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5907 if (ram_decompress_buf(s
, buf
, 1) < 0) {
5908 fprintf(stderr
, "Error while reading ram block header\n");
5912 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
5913 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
5922 ram_decompress_buf(s
, buf
+ 1, 9);
5924 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
5925 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
5926 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
5929 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
5930 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
5931 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
5932 bs_index
, sector_num
);
5939 printf("Error block header\n");
5943 ram_decompress_close(s
);
5947 /***********************************************************/
5948 /* bottom halves (can be seen as timers which expire ASAP) */
5957 static QEMUBH
*first_bh
= NULL
;
5959 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
5962 bh
= qemu_mallocz(sizeof(QEMUBH
));
5966 bh
->opaque
= opaque
;
5970 int qemu_bh_poll(void)
5989 void qemu_bh_schedule(QEMUBH
*bh
)
5991 CPUState
*env
= cpu_single_env
;
5995 bh
->next
= first_bh
;
5998 /* stop the currently executing CPU to execute the BH ASAP */
6000 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
6004 void qemu_bh_cancel(QEMUBH
*bh
)
6007 if (bh
->scheduled
) {
6010 pbh
= &(*pbh
)->next
;
6016 void qemu_bh_delete(QEMUBH
*bh
)
6022 /***********************************************************/
6023 /* machine registration */
6025 QEMUMachine
*first_machine
= NULL
;
6027 int qemu_register_machine(QEMUMachine
*m
)
6030 pm
= &first_machine
;
6038 QEMUMachine
*find_machine(const char *name
)
6042 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6043 if (!strcmp(m
->name
, name
))
6049 /***********************************************************/
6050 /* main execution loop */
6052 void gui_update(void *opaque
)
6054 display_state
.dpy_refresh(&display_state
);
6055 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
6058 struct vm_change_state_entry
{
6059 VMChangeStateHandler
*cb
;
6061 LIST_ENTRY (vm_change_state_entry
) entries
;
6064 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
6066 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
6069 VMChangeStateEntry
*e
;
6071 e
= qemu_mallocz(sizeof (*e
));
6077 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
6081 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
6083 LIST_REMOVE (e
, entries
);
6087 static void vm_state_notify(int running
)
6089 VMChangeStateEntry
*e
;
6091 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
6092 e
->cb(e
->opaque
, running
);
6096 /* XXX: support several handlers */
6097 static VMStopHandler
*vm_stop_cb
;
6098 static void *vm_stop_opaque
;
6100 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6103 vm_stop_opaque
= opaque
;
6107 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6121 void vm_stop(int reason
)
6124 cpu_disable_ticks();
6128 vm_stop_cb(vm_stop_opaque
, reason
);
6135 /* reset/shutdown handler */
6137 typedef struct QEMUResetEntry
{
6138 QEMUResetHandler
*func
;
6140 struct QEMUResetEntry
*next
;
6143 static QEMUResetEntry
*first_reset_entry
;
6144 static int reset_requested
;
6145 static int shutdown_requested
;
6146 static int powerdown_requested
;
6148 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
6150 QEMUResetEntry
**pre
, *re
;
6152 pre
= &first_reset_entry
;
6153 while (*pre
!= NULL
)
6154 pre
= &(*pre
)->next
;
6155 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
6157 re
->opaque
= opaque
;
6162 static void qemu_system_reset(void)
6166 /* reset all devices */
6167 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
6168 re
->func(re
->opaque
);
6172 void qemu_system_reset_request(void)
6175 shutdown_requested
= 1;
6177 reset_requested
= 1;
6180 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6183 void qemu_system_shutdown_request(void)
6185 shutdown_requested
= 1;
6187 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6190 void qemu_system_powerdown_request(void)
6192 powerdown_requested
= 1;
6194 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6197 void main_loop_wait(int timeout
)
6199 IOHandlerRecord
*ioh
;
6200 fd_set rfds
, wfds
, xfds
;
6209 /* XXX: need to suppress polling by better using win32 events */
6211 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
6212 ret
|= pe
->func(pe
->opaque
);
6217 WaitObjects
*w
= &wait_objects
;
6219 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
6220 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
6221 if (w
->func
[ret
- WAIT_OBJECT_0
])
6222 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
6224 /* Check for additional signaled events */
6225 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
6227 /* Check if event is signaled */
6228 ret2
= WaitForSingleObject(w
->events
[i
], 0);
6229 if(ret2
== WAIT_OBJECT_0
) {
6231 w
->func
[i
](w
->opaque
[i
]);
6232 } else if (ret2
== WAIT_TIMEOUT
) {
6234 err
= GetLastError();
6235 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
6238 } else if (ret
== WAIT_TIMEOUT
) {
6240 err
= GetLastError();
6241 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
6245 /* poll any events */
6246 /* XXX: separate device handlers from system ones */
6251 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6255 (!ioh
->fd_read_poll
||
6256 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
6257 FD_SET(ioh
->fd
, &rfds
);
6261 if (ioh
->fd_write
) {
6262 FD_SET(ioh
->fd
, &wfds
);
6272 tv
.tv_usec
= timeout
* 1000;
6274 #if defined(CONFIG_SLIRP)
6276 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
6279 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
6281 IOHandlerRecord
**pioh
;
6283 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6286 if (FD_ISSET(ioh
->fd
, &rfds
)) {
6287 ioh
->fd_read(ioh
->opaque
);
6289 if (FD_ISSET(ioh
->fd
, &wfds
)) {
6290 ioh
->fd_write(ioh
->opaque
);
6294 /* remove deleted IO handlers */
6295 pioh
= &first_io_handler
;
6305 #if defined(CONFIG_SLIRP)
6312 slirp_select_poll(&rfds
, &wfds
, &xfds
);
6319 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
6320 qemu_get_clock(vm_clock
));
6321 /* run dma transfers, if any */
6325 /* real time timers */
6326 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
6327 qemu_get_clock(rt_clock
));
6330 static CPUState
*cur_cpu
;
6335 #ifdef CONFIG_PROFILER
6340 cur_cpu
= first_cpu
;
6347 env
= env
->next_cpu
;
6350 #ifdef CONFIG_PROFILER
6351 ti
= profile_getclock();
6353 ret
= cpu_exec(env
);
6354 #ifdef CONFIG_PROFILER
6355 qemu_time
+= profile_getclock() - ti
;
6357 if (ret
== EXCP_HLT
) {
6358 /* Give the next CPU a chance to run. */
6362 if (ret
!= EXCP_HALTED
)
6364 /* all CPUs are halted ? */
6370 if (shutdown_requested
) {
6371 ret
= EXCP_INTERRUPT
;
6374 if (reset_requested
) {
6375 reset_requested
= 0;
6376 qemu_system_reset();
6377 ret
= EXCP_INTERRUPT
;
6379 if (powerdown_requested
) {
6380 powerdown_requested
= 0;
6381 qemu_system_powerdown();
6382 ret
= EXCP_INTERRUPT
;
6384 if (ret
== EXCP_DEBUG
) {
6385 vm_stop(EXCP_DEBUG
);
6387 /* If all cpus are halted then wait until the next IRQ */
6388 /* XXX: use timeout computed from timers */
6389 if (ret
== EXCP_HALTED
)
6396 #ifdef CONFIG_PROFILER
6397 ti
= profile_getclock();
6399 main_loop_wait(timeout
);
6400 #ifdef CONFIG_PROFILER
6401 dev_time
+= profile_getclock() - ti
;
6404 cpu_disable_ticks();
6410 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2007 Fabrice Bellard\n"
6411 "usage: %s [options] [disk_image]\n"
6413 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
6415 "Standard options:\n"
6416 "-M machine select emulated machine (-M ? for list)\n"
6417 "-cpu cpu select CPU (-cpu ? for list)\n"
6418 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
6419 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
6420 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
6421 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
6422 "-sd file use 'file' as SecureDigital card image\n"
6423 "-pflash file use 'file' as a parallel flash image\n"
6424 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
6425 "-snapshot write to temporary files instead of disk image files\n"
6427 "-no-frame open SDL window without a frame and window decorations\n"
6428 "-no-quit disable SDL window close capability\n"
6431 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
6433 "-m megs set virtual RAM size to megs MB [default=%d]\n"
6434 "-smp n set the number of CPUs to 'n' [default=1]\n"
6435 "-nographic disable graphical output and redirect serial I/Os to console\n"
6436 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
6438 "-k language use keyboard layout (for example \"fr\" for French)\n"
6441 "-audio-help print list of audio drivers and their options\n"
6442 "-soundhw c1,... enable audio support\n"
6443 " and only specified sound cards (comma separated list)\n"
6444 " use -soundhw ? to get the list of supported cards\n"
6445 " use -soundhw all to enable all of them\n"
6447 "-localtime set the real time clock to local time [default=utc]\n"
6448 "-full-screen start in full screen\n"
6450 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
6452 "-usb enable the USB driver (will be the default soon)\n"
6453 "-usbdevice name add the host or guest USB device 'name'\n"
6454 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6455 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
6457 "-name string set the name of the guest\n"
6459 "Network options:\n"
6460 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
6461 " create a new Network Interface Card and connect it to VLAN 'n'\n"
6463 "-net user[,vlan=n][,hostname=host]\n"
6464 " connect the user mode network stack to VLAN 'n' and send\n"
6465 " hostname 'host' to DHCP clients\n"
6468 "-net tap[,vlan=n],ifname=name\n"
6469 " connect the host TAP network interface to VLAN 'n'\n"
6471 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
6472 " connect the host TAP network interface to VLAN 'n' and use\n"
6473 " the network script 'file' (default=%s);\n"
6474 " use 'script=no' to disable script execution;\n"
6475 " use 'fd=h' to connect to an already opened TAP interface\n"
6477 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
6478 " connect the vlan 'n' to another VLAN using a socket connection\n"
6479 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
6480 " connect the vlan 'n' to multicast maddr and port\n"
6481 "-net none use it alone to have zero network devices; if no -net option\n"
6482 " is provided, the default is '-net nic -net user'\n"
6485 "-tftp dir allow tftp access to files in dir [-net user]\n"
6486 "-bootp file advertise file in BOOTP replies\n"
6488 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
6490 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
6491 " redirect TCP or UDP connections from host to guest [-net user]\n"
6494 "Linux boot specific:\n"
6495 "-kernel bzImage use 'bzImage' as kernel image\n"
6496 "-append cmdline use 'cmdline' as kernel command line\n"
6497 "-initrd file use 'file' as initial ram disk\n"
6499 "Debug/Expert options:\n"
6500 "-monitor dev redirect the monitor to char device 'dev'\n"
6501 "-serial dev redirect the serial port to char device 'dev'\n"
6502 "-parallel dev redirect the parallel port to char device 'dev'\n"
6503 "-pidfile file Write PID to 'file'\n"
6504 "-S freeze CPU at startup (use 'c' to start execution)\n"
6505 "-s wait gdb connection to port\n"
6506 "-p port set gdb connection port [default=%s]\n"
6507 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
6508 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
6509 " translation (t=none or lba) (usually qemu can guess them)\n"
6510 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
6512 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
6513 "-no-kqemu disable KQEMU kernel module usage\n"
6515 #ifdef USE_CODE_COPY
6516 "-no-code-copy disable code copy acceleration\n"
6519 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
6520 " (default is CL-GD5446 PCI VGA)\n"
6521 "-no-acpi disable ACPI\n"
6523 "-no-reboot exit instead of rebooting\n"
6524 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
6525 "-vnc display start a VNC server on display\n"
6527 "-daemonize daemonize QEMU after initializing\n"
6529 "-option-rom rom load a file, rom, into the option ROM space\n"
6531 "During emulation, the following keys are useful:\n"
6532 "ctrl-alt-f toggle full screen\n"
6533 "ctrl-alt-n switch to virtual console 'n'\n"
6534 "ctrl-alt toggle mouse and keyboard grab\n"
6536 "When using -nographic, press 'ctrl-a h' to get some help.\n"
6541 DEFAULT_NETWORK_SCRIPT
,
6543 DEFAULT_GDBSTUB_PORT
,
6548 #define HAS_ARG 0x0001
6565 QEMU_OPTION_snapshot
,
6567 QEMU_OPTION_no_fd_bootchk
,
6570 QEMU_OPTION_nographic
,
6571 QEMU_OPTION_portrait
,
6573 QEMU_OPTION_audio_help
,
6574 QEMU_OPTION_soundhw
,
6593 QEMU_OPTION_no_code_copy
,
6595 QEMU_OPTION_localtime
,
6596 QEMU_OPTION_cirrusvga
,
6599 QEMU_OPTION_std_vga
,
6601 QEMU_OPTION_monitor
,
6603 QEMU_OPTION_parallel
,
6605 QEMU_OPTION_full_screen
,
6606 QEMU_OPTION_no_frame
,
6607 QEMU_OPTION_no_quit
,
6608 QEMU_OPTION_pidfile
,
6609 QEMU_OPTION_no_kqemu
,
6610 QEMU_OPTION_kernel_kqemu
,
6611 QEMU_OPTION_win2k_hack
,
6613 QEMU_OPTION_usbdevice
,
6616 QEMU_OPTION_no_acpi
,
6617 QEMU_OPTION_no_reboot
,
6618 QEMU_OPTION_daemonize
,
6619 QEMU_OPTION_option_rom
,
6620 QEMU_OPTION_semihosting
,
6624 typedef struct QEMUOption
{
6630 const QEMUOption qemu_options
[] = {
6631 { "h", 0, QEMU_OPTION_h
},
6632 { "help", 0, QEMU_OPTION_h
},
6634 { "M", HAS_ARG
, QEMU_OPTION_M
},
6635 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
6636 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
6637 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
6638 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
6639 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
6640 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
6641 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
6642 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
6643 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
6644 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
6645 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
6646 { "snapshot", 0, QEMU_OPTION_snapshot
},
6648 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
6650 { "m", HAS_ARG
, QEMU_OPTION_m
},
6651 { "nographic", 0, QEMU_OPTION_nographic
},
6652 { "portrait", 0, QEMU_OPTION_portrait
},
6653 { "k", HAS_ARG
, QEMU_OPTION_k
},
6655 { "audio-help", 0, QEMU_OPTION_audio_help
},
6656 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
6659 { "net", HAS_ARG
, QEMU_OPTION_net
},
6661 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
6662 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
6664 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
6666 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
6669 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
6670 { "append", HAS_ARG
, QEMU_OPTION_append
},
6671 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
6673 { "S", 0, QEMU_OPTION_S
},
6674 { "s", 0, QEMU_OPTION_s
},
6675 { "p", HAS_ARG
, QEMU_OPTION_p
},
6676 { "d", HAS_ARG
, QEMU_OPTION_d
},
6677 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
6678 { "L", HAS_ARG
, QEMU_OPTION_L
},
6679 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
6681 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
6682 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
6684 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6685 { "g", 1, QEMU_OPTION_g
},
6687 { "localtime", 0, QEMU_OPTION_localtime
},
6688 { "std-vga", 0, QEMU_OPTION_std_vga
},
6689 { "echr", 1, QEMU_OPTION_echr
},
6690 { "monitor", 1, QEMU_OPTION_monitor
},
6691 { "serial", 1, QEMU_OPTION_serial
},
6692 { "parallel", 1, QEMU_OPTION_parallel
},
6693 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
6694 { "full-screen", 0, QEMU_OPTION_full_screen
},
6696 { "no-frame", 0, QEMU_OPTION_no_frame
},
6697 { "no-quit", 0, QEMU_OPTION_no_quit
},
6699 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
6700 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
6701 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
6702 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
6703 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
6705 /* temporary options */
6706 { "usb", 0, QEMU_OPTION_usb
},
6707 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
6708 { "vmwarevga", 0, QEMU_OPTION_vmsvga
},
6709 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
6710 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
6711 { "daemonize", 0, QEMU_OPTION_daemonize
},
6712 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
6713 #if defined(TARGET_ARM)
6714 { "semihosting", 0, QEMU_OPTION_semihosting
},
6716 { "name", HAS_ARG
, QEMU_OPTION_name
},
6720 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
6722 /* this stack is only used during signal handling */
6723 #define SIGNAL_STACK_SIZE 32768
6725 static uint8_t *signal_stack
;
6729 /* password input */
6731 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
6736 if (!bdrv_is_encrypted(bs
))
6739 term_printf("%s is encrypted.\n", name
);
6740 for(i
= 0; i
< 3; i
++) {
6741 monitor_readline("Password: ", 1, password
, sizeof(password
));
6742 if (bdrv_set_key(bs
, password
) == 0)
6744 term_printf("invalid password\n");
6749 static BlockDriverState
*get_bdrv(int index
)
6751 BlockDriverState
*bs
;
6754 bs
= bs_table
[index
];
6755 } else if (index
< 6) {
6756 bs
= fd_table
[index
- 4];
6763 static void read_passwords(void)
6765 BlockDriverState
*bs
;
6768 for(i
= 0; i
< 6; i
++) {
6771 qemu_key_check(bs
, bdrv_get_device_name(bs
));
6775 /* XXX: currently we cannot use simultaneously different CPUs */
6776 void register_machines(void)
6778 #if defined(TARGET_I386)
6779 qemu_register_machine(&pc_machine
);
6780 qemu_register_machine(&isapc_machine
);
6781 #elif defined(TARGET_PPC)
6782 qemu_register_machine(&heathrow_machine
);
6783 qemu_register_machine(&core99_machine
);
6784 qemu_register_machine(&prep_machine
);
6785 qemu_register_machine(&ref405ep_machine
);
6786 qemu_register_machine(&taihu_machine
);
6787 #elif defined(TARGET_MIPS)
6788 qemu_register_machine(&mips_machine
);
6789 qemu_register_machine(&mips_malta_machine
);
6790 qemu_register_machine(&mips_pica61_machine
);
6791 #elif defined(TARGET_SPARC)
6792 #ifdef TARGET_SPARC64
6793 qemu_register_machine(&sun4u_machine
);
6795 qemu_register_machine(&ss5_machine
);
6796 qemu_register_machine(&ss10_machine
);
6798 #elif defined(TARGET_ARM)
6799 qemu_register_machine(&integratorcp_machine
);
6800 qemu_register_machine(&versatilepb_machine
);
6801 qemu_register_machine(&versatileab_machine
);
6802 qemu_register_machine(&realview_machine
);
6803 #elif defined(TARGET_SH4)
6804 qemu_register_machine(&shix_machine
);
6805 #elif defined(TARGET_ALPHA)
6808 #error unsupported CPU
6813 struct soundhw soundhw
[] = {
6820 { .init_isa
= pcspk_audio_init
}
6825 "Creative Sound Blaster 16",
6828 { .init_isa
= SB16_init
}
6835 "Yamaha YMF262 (OPL3)",
6837 "Yamaha YM3812 (OPL2)",
6841 { .init_isa
= Adlib_init
}
6848 "Gravis Ultrasound GF1",
6851 { .init_isa
= GUS_init
}
6857 "ENSONIQ AudioPCI ES1370",
6860 { .init_pci
= es1370_init
}
6863 { NULL
, NULL
, 0, 0, { NULL
} }
6866 static void select_soundhw (const char *optarg
)
6870 if (*optarg
== '?') {
6873 printf ("Valid sound card names (comma separated):\n");
6874 for (c
= soundhw
; c
->name
; ++c
) {
6875 printf ("%-11s %s\n", c
->name
, c
->descr
);
6877 printf ("\n-soundhw all will enable all of the above\n");
6878 exit (*optarg
!= '?');
6886 if (!strcmp (optarg
, "all")) {
6887 for (c
= soundhw
; c
->name
; ++c
) {
6895 e
= strchr (p
, ',');
6896 l
= !e
? strlen (p
) : (size_t) (e
- p
);
6898 for (c
= soundhw
; c
->name
; ++c
) {
6899 if (!strncmp (c
->name
, p
, l
)) {
6908 "Unknown sound card name (too big to show)\n");
6911 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
6916 p
+= l
+ (e
!= NULL
);
6920 goto show_valid_cards
;
6926 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
6928 exit(STATUS_CONTROL_C_EXIT
);
6933 #define MAX_NET_CLIENTS 32
6935 int main(int argc
, char **argv
)
6937 #ifdef CONFIG_GDBSTUB
6939 const char *gdbstub_port
;
6941 int i
, cdrom_index
, pflash_index
;
6942 int snapshot
, linux_boot
;
6943 const char *initrd_filename
;
6944 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
6945 const char *pflash_filename
[MAX_PFLASH
];
6946 const char *sd_filename
;
6947 const char *kernel_filename
, *kernel_cmdline
;
6948 DisplayState
*ds
= &display_state
;
6949 int cyls
, heads
, secs
, translation
;
6950 char net_clients
[MAX_NET_CLIENTS
][256];
6953 const char *r
, *optarg
;
6954 CharDriverState
*monitor_hd
;
6955 char monitor_device
[128];
6956 char serial_devices
[MAX_SERIAL_PORTS
][128];
6957 int serial_device_index
;
6958 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
6959 int parallel_device_index
;
6960 const char *loadvm
= NULL
;
6961 QEMUMachine
*machine
;
6962 const char *cpu_model
;
6963 char usb_devices
[MAX_USB_CMDLINE
][128];
6964 int usb_devices_index
;
6966 const char *pid_file
= NULL
;
6968 LIST_INIT (&vm_change_state_head
);
6971 struct sigaction act
;
6972 sigfillset(&act
.sa_mask
);
6974 act
.sa_handler
= SIG_IGN
;
6975 sigaction(SIGPIPE
, &act
, NULL
);
6978 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
6979 /* Note: cpu_interrupt() is currently not SMP safe, so we force
6980 QEMU to run on a single CPU */
6985 h
= GetCurrentProcess();
6986 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
6987 for(i
= 0; i
< 32; i
++) {
6988 if (mask
& (1 << i
))
6993 SetProcessAffinityMask(h
, mask
);
6999 register_machines();
7000 machine
= first_machine
;
7002 initrd_filename
= NULL
;
7003 for(i
= 0; i
< MAX_FD
; i
++)
7004 fd_filename
[i
] = NULL
;
7005 for(i
= 0; i
< MAX_DISKS
; i
++)
7006 hd_filename
[i
] = NULL
;
7007 for(i
= 0; i
< MAX_PFLASH
; i
++)
7008 pflash_filename
[i
] = NULL
;
7011 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
7012 vga_ram_size
= VGA_RAM_SIZE
;
7013 #ifdef CONFIG_GDBSTUB
7015 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
7019 kernel_filename
= NULL
;
7020 kernel_cmdline
= "";
7026 cyls
= heads
= secs
= 0;
7027 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7028 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
7030 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
7031 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
7032 serial_devices
[i
][0] = '\0';
7033 serial_device_index
= 0;
7035 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
7036 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
7037 parallel_devices
[i
][0] = '\0';
7038 parallel_device_index
= 0;
7040 usb_devices_index
= 0;
7045 /* default mac address of the first network interface */
7053 hd_filename
[0] = argv
[optind
++];
7055 const QEMUOption
*popt
;
7058 /* Treat --foo the same as -foo. */
7061 popt
= qemu_options
;
7064 fprintf(stderr
, "%s: invalid option -- '%s'\n",
7068 if (!strcmp(popt
->name
, r
+ 1))
7072 if (popt
->flags
& HAS_ARG
) {
7073 if (optind
>= argc
) {
7074 fprintf(stderr
, "%s: option '%s' requires an argument\n",
7078 optarg
= argv
[optind
++];
7083 switch(popt
->index
) {
7085 machine
= find_machine(optarg
);
7088 printf("Supported machines are:\n");
7089 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
7090 printf("%-10s %s%s\n",
7092 m
== first_machine
? " (default)" : "");
7097 case QEMU_OPTION_cpu
:
7098 /* hw initialization will check this */
7099 if (optarg
[0] == '?') {
7100 #if defined(TARGET_PPC)
7101 ppc_cpu_list(stdout
, &fprintf
);
7102 #elif defined(TARGET_ARM)
7104 #elif defined(TARGET_MIPS)
7105 mips_cpu_list(stdout
, &fprintf
);
7106 #elif defined(TARGET_SPARC)
7107 sparc_cpu_list(stdout
, &fprintf
);
7114 case QEMU_OPTION_initrd
:
7115 initrd_filename
= optarg
;
7117 case QEMU_OPTION_hda
:
7118 case QEMU_OPTION_hdb
:
7119 case QEMU_OPTION_hdc
:
7120 case QEMU_OPTION_hdd
:
7123 hd_index
= popt
->index
- QEMU_OPTION_hda
;
7124 hd_filename
[hd_index
] = optarg
;
7125 if (hd_index
== cdrom_index
)
7129 case QEMU_OPTION_sd
:
7130 sd_filename
= optarg
;
7132 case QEMU_OPTION_pflash
:
7133 if (pflash_index
>= MAX_PFLASH
) {
7134 fprintf(stderr
, "qemu: too many parallel flash images\n");
7137 pflash_filename
[pflash_index
++] = optarg
;
7139 case QEMU_OPTION_snapshot
:
7142 case QEMU_OPTION_hdachs
:
7146 cyls
= strtol(p
, (char **)&p
, 0);
7147 if (cyls
< 1 || cyls
> 16383)
7152 heads
= strtol(p
, (char **)&p
, 0);
7153 if (heads
< 1 || heads
> 16)
7158 secs
= strtol(p
, (char **)&p
, 0);
7159 if (secs
< 1 || secs
> 63)
7163 if (!strcmp(p
, "none"))
7164 translation
= BIOS_ATA_TRANSLATION_NONE
;
7165 else if (!strcmp(p
, "lba"))
7166 translation
= BIOS_ATA_TRANSLATION_LBA
;
7167 else if (!strcmp(p
, "auto"))
7168 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7171 } else if (*p
!= '\0') {
7173 fprintf(stderr
, "qemu: invalid physical CHS format\n");
7178 case QEMU_OPTION_nographic
:
7179 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
7180 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "null");
7181 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
7184 case QEMU_OPTION_portrait
:
7187 case QEMU_OPTION_kernel
:
7188 kernel_filename
= optarg
;
7190 case QEMU_OPTION_append
:
7191 kernel_cmdline
= optarg
;
7193 case QEMU_OPTION_cdrom
:
7194 if (cdrom_index
>= 0) {
7195 hd_filename
[cdrom_index
] = optarg
;
7198 case QEMU_OPTION_boot
:
7199 boot_device
= optarg
[0];
7200 if (boot_device
!= 'a' &&
7201 #if defined(TARGET_SPARC) || defined(TARGET_I386)
7203 boot_device
!= 'n' &&
7205 boot_device
!= 'c' && boot_device
!= 'd') {
7206 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
7210 case QEMU_OPTION_fda
:
7211 fd_filename
[0] = optarg
;
7213 case QEMU_OPTION_fdb
:
7214 fd_filename
[1] = optarg
;
7217 case QEMU_OPTION_no_fd_bootchk
:
7221 case QEMU_OPTION_no_code_copy
:
7222 code_copy_enabled
= 0;
7224 case QEMU_OPTION_net
:
7225 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
7226 fprintf(stderr
, "qemu: too many network clients\n");
7229 pstrcpy(net_clients
[nb_net_clients
],
7230 sizeof(net_clients
[0]),
7235 case QEMU_OPTION_tftp
:
7236 tftp_prefix
= optarg
;
7238 case QEMU_OPTION_bootp
:
7239 bootp_filename
= optarg
;
7242 case QEMU_OPTION_smb
:
7243 net_slirp_smb(optarg
);
7246 case QEMU_OPTION_redir
:
7247 net_slirp_redir(optarg
);
7251 case QEMU_OPTION_audio_help
:
7255 case QEMU_OPTION_soundhw
:
7256 select_soundhw (optarg
);
7263 ram_size
= atoi(optarg
) * 1024 * 1024;
7266 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
7267 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
7268 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
7277 mask
= cpu_str_to_log_mask(optarg
);
7279 printf("Log items (comma separated):\n");
7280 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
7281 printf("%-10s %s\n", item
->name
, item
->help
);
7288 #ifdef CONFIG_GDBSTUB
7293 gdbstub_port
= optarg
;
7303 keyboard_layout
= optarg
;
7305 case QEMU_OPTION_localtime
:
7308 case QEMU_OPTION_cirrusvga
:
7309 cirrus_vga_enabled
= 1;
7312 case QEMU_OPTION_vmsvga
:
7313 cirrus_vga_enabled
= 0;
7316 case QEMU_OPTION_std_vga
:
7317 cirrus_vga_enabled
= 0;
7325 w
= strtol(p
, (char **)&p
, 10);
7328 fprintf(stderr
, "qemu: invalid resolution or depth\n");
7334 h
= strtol(p
, (char **)&p
, 10);
7339 depth
= strtol(p
, (char **)&p
, 10);
7340 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
7341 depth
!= 24 && depth
!= 32)
7343 } else if (*p
== '\0') {
7344 depth
= graphic_depth
;
7351 graphic_depth
= depth
;
7354 case QEMU_OPTION_echr
:
7357 term_escape_char
= strtol(optarg
, &r
, 0);
7359 printf("Bad argument to echr\n");
7362 case QEMU_OPTION_monitor
:
7363 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
7365 case QEMU_OPTION_serial
:
7366 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
7367 fprintf(stderr
, "qemu: too many serial ports\n");
7370 pstrcpy(serial_devices
[serial_device_index
],
7371 sizeof(serial_devices
[0]), optarg
);
7372 serial_device_index
++;
7374 case QEMU_OPTION_parallel
:
7375 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
7376 fprintf(stderr
, "qemu: too many parallel ports\n");
7379 pstrcpy(parallel_devices
[parallel_device_index
],
7380 sizeof(parallel_devices
[0]), optarg
);
7381 parallel_device_index
++;
7383 case QEMU_OPTION_loadvm
:
7386 case QEMU_OPTION_full_screen
:
7390 case QEMU_OPTION_no_frame
:
7393 case QEMU_OPTION_no_quit
:
7397 case QEMU_OPTION_pidfile
:
7401 case QEMU_OPTION_win2k_hack
:
7402 win2k_install_hack
= 1;
7406 case QEMU_OPTION_no_kqemu
:
7409 case QEMU_OPTION_kernel_kqemu
:
7413 case QEMU_OPTION_usb
:
7416 case QEMU_OPTION_usbdevice
:
7418 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
7419 fprintf(stderr
, "Too many USB devices\n");
7422 pstrcpy(usb_devices
[usb_devices_index
],
7423 sizeof(usb_devices
[usb_devices_index
]),
7425 usb_devices_index
++;
7427 case QEMU_OPTION_smp
:
7428 smp_cpus
= atoi(optarg
);
7429 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
7430 fprintf(stderr
, "Invalid number of CPUs\n");
7434 case QEMU_OPTION_vnc
:
7435 vnc_display
= optarg
;
7437 case QEMU_OPTION_no_acpi
:
7440 case QEMU_OPTION_no_reboot
:
7443 case QEMU_OPTION_daemonize
:
7446 case QEMU_OPTION_option_rom
:
7447 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
7448 fprintf(stderr
, "Too many option ROMs\n");
7451 option_rom
[nb_option_roms
] = optarg
;
7454 case QEMU_OPTION_semihosting
:
7455 semihosting_enabled
= 1;
7457 case QEMU_OPTION_name
:
7465 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
7466 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
7473 if (pipe(fds
) == -1)
7484 len
= read(fds
[0], &status
, 1);
7485 if (len
== -1 && (errno
== EINTR
))
7490 else if (status
== 1) {
7491 fprintf(stderr
, "Could not acquire pidfile\n");
7509 signal(SIGTSTP
, SIG_IGN
);
7510 signal(SIGTTOU
, SIG_IGN
);
7511 signal(SIGTTIN
, SIG_IGN
);
7515 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
7518 write(fds
[1], &status
, 1);
7520 fprintf(stderr
, "Could not acquire pid file\n");
7528 linux_boot
= (kernel_filename
!= NULL
);
7531 boot_device
!= 'n' &&
7532 hd_filename
[0] == '\0' &&
7533 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
7534 fd_filename
[0] == '\0')
7537 /* boot to floppy or the default cd if no hard disk defined yet */
7538 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
7539 if (fd_filename
[0] != '\0')
7545 setvbuf(stdout
, NULL
, _IOLBF
, 0);
7555 /* init network clients */
7556 if (nb_net_clients
== 0) {
7557 /* if no clients, we use a default config */
7558 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
7560 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
7565 for(i
= 0;i
< nb_net_clients
; i
++) {
7566 if (net_client_init(net_clients
[i
]) < 0)
7571 if (boot_device
== 'n') {
7572 for (i
= 0; i
< nb_nics
; i
++) {
7573 const char *model
= nd_table
[i
].model
;
7577 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
7578 if (get_image_size(buf
) > 0) {
7579 option_rom
[nb_option_roms
] = strdup(buf
);
7585 fprintf(stderr
, "No valid PXE rom found for network device\n");
7588 boot_device
= 'c'; /* to prevent confusion by the BIOS */
7592 /* init the memory */
7593 phys_ram_size
= ram_size
+ vga_ram_size
+ MAX_BIOS_SIZE
;
7595 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
7596 if (!phys_ram_base
) {
7597 fprintf(stderr
, "Could not allocate physical memory\n");
7601 /* we always create the cdrom drive, even if no disk is there */
7603 if (cdrom_index
>= 0) {
7604 bs_table
[cdrom_index
] = bdrv_new("cdrom");
7605 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
7608 /* open the virtual block devices */
7609 for(i
= 0; i
< MAX_DISKS
; i
++) {
7610 if (hd_filename
[i
]) {
7613 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
7614 bs_table
[i
] = bdrv_new(buf
);
7616 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7617 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
7621 if (i
== 0 && cyls
!= 0) {
7622 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
7623 bdrv_set_translation_hint(bs_table
[i
], translation
);
7628 /* we always create at least one floppy disk */
7629 fd_table
[0] = bdrv_new("fda");
7630 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
7632 for(i
= 0; i
< MAX_FD
; i
++) {
7633 if (fd_filename
[i
]) {
7636 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
7637 fd_table
[i
] = bdrv_new(buf
);
7638 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
7640 if (fd_filename
[i
][0] != '\0') {
7641 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
7642 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7643 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
7651 /* Open the virtual parallel flash block devices */
7652 for(i
= 0; i
< MAX_PFLASH
; i
++) {
7653 if (pflash_filename
[i
]) {
7654 if (!pflash_table
[i
]) {
7656 snprintf(buf
, sizeof(buf
), "fl%c", i
+ 'a');
7657 pflash_table
[i
] = bdrv_new(buf
);
7659 if (bdrv_open(pflash_table
[i
], pflash_filename
[i
],
7660 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7661 fprintf(stderr
, "qemu: could not open flash image '%s'\n",
7662 pflash_filename
[i
]);
7668 sd_bdrv
= bdrv_new ("sd");
7669 /* FIXME: This isn't really a floppy, but it's a reasonable
7671 bdrv_set_type_hint(sd_bdrv
, BDRV_TYPE_FLOPPY
);
7673 if (bdrv_open(sd_bdrv
, sd_filename
,
7674 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7675 fprintf(stderr
, "qemu: could not open SD card image %s\n",
7678 qemu_key_check(sd_bdrv
, sd_filename
);
7681 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
7682 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
7688 dumb_display_init(ds
);
7689 } else if (vnc_display
!= NULL
) {
7690 vnc_display_init(ds
, vnc_display
);
7692 #if defined(CONFIG_SDL)
7693 sdl_display_init(ds
, full_screen
, no_frame
);
7694 #elif defined(CONFIG_COCOA)
7695 cocoa_display_init(ds
, full_screen
);
7697 dumb_display_init(ds
);
7701 /* Maintain compatibility with multiple stdio monitors */
7702 if (!strcmp(monitor_device
,"stdio")) {
7703 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7704 if (!strcmp(serial_devices
[i
],"mon:stdio")) {
7705 monitor_device
[0] = '\0';
7707 } else if (!strcmp(serial_devices
[i
],"stdio")) {
7708 monitor_device
[0] = '\0';
7709 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "mon:stdio");
7714 if (monitor_device
[0] != '\0') {
7715 monitor_hd
= qemu_chr_open(monitor_device
);
7717 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
7720 monitor_init(monitor_hd
, !nographic
);
7723 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7724 const char *devname
= serial_devices
[i
];
7725 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7726 serial_hds
[i
] = qemu_chr_open(devname
);
7727 if (!serial_hds
[i
]) {
7728 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
7732 if (!strcmp(devname
, "vc"))
7733 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
7737 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
7738 const char *devname
= parallel_devices
[i
];
7739 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7740 parallel_hds
[i
] = qemu_chr_open(devname
);
7741 if (!parallel_hds
[i
]) {
7742 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
7746 if (!strcmp(devname
, "vc"))
7747 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
7751 machine
->init(ram_size
, vga_ram_size
, boot_device
,
7752 ds
, fd_filename
, snapshot
,
7753 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
7755 /* init USB devices */
7757 for(i
= 0; i
< usb_devices_index
; i
++) {
7758 if (usb_device_add(usb_devices
[i
]) < 0) {
7759 fprintf(stderr
, "Warning: could not add USB device %s\n",
7765 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
7766 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
7768 #ifdef CONFIG_GDBSTUB
7770 /* XXX: use standard host:port notation and modify options
7772 if (gdbserver_start(gdbstub_port
) < 0) {
7773 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
7783 /* XXX: simplify init */
7796 len
= write(fds
[1], &status
, 1);
7797 if (len
== -1 && (errno
== EINTR
))
7803 fd
= open("/dev/null", O_RDWR
);