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;
192 const char *option_rom
[MAX_OPTION_ROMS
];
194 int semihosting_enabled
= 0;
196 const char *qemu_name
;
198 /***********************************************************/
199 /* x86 ISA bus support */
201 target_phys_addr_t isa_mem_base
= 0;
204 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
206 #ifdef DEBUG_UNUSED_IOPORT
207 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
212 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
214 #ifdef DEBUG_UNUSED_IOPORT
215 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
219 /* default is to make two byte accesses */
220 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
223 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
224 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
225 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
229 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
231 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
232 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
233 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
236 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
238 #ifdef DEBUG_UNUSED_IOPORT
239 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
244 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
246 #ifdef DEBUG_UNUSED_IOPORT
247 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
251 void init_ioports(void)
255 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
256 ioport_read_table
[0][i
] = default_ioport_readb
;
257 ioport_write_table
[0][i
] = default_ioport_writeb
;
258 ioport_read_table
[1][i
] = default_ioport_readw
;
259 ioport_write_table
[1][i
] = default_ioport_writew
;
260 ioport_read_table
[2][i
] = default_ioport_readl
;
261 ioport_write_table
[2][i
] = default_ioport_writel
;
265 /* size is the word size in byte */
266 int register_ioport_read(int start
, int length
, int size
,
267 IOPortReadFunc
*func
, void *opaque
)
273 } else if (size
== 2) {
275 } else if (size
== 4) {
278 hw_error("register_ioport_read: invalid size");
281 for(i
= start
; i
< start
+ length
; i
+= size
) {
282 ioport_read_table
[bsize
][i
] = func
;
283 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
284 hw_error("register_ioport_read: invalid opaque");
285 ioport_opaque
[i
] = opaque
;
290 /* size is the word size in byte */
291 int register_ioport_write(int start
, int length
, int size
,
292 IOPortWriteFunc
*func
, void *opaque
)
298 } else if (size
== 2) {
300 } else if (size
== 4) {
303 hw_error("register_ioport_write: invalid size");
306 for(i
= start
; i
< start
+ length
; i
+= size
) {
307 ioport_write_table
[bsize
][i
] = func
;
308 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
309 hw_error("register_ioport_write: invalid opaque");
310 ioport_opaque
[i
] = opaque
;
315 void isa_unassign_ioport(int start
, int length
)
319 for(i
= start
; i
< start
+ length
; i
++) {
320 ioport_read_table
[0][i
] = default_ioport_readb
;
321 ioport_read_table
[1][i
] = default_ioport_readw
;
322 ioport_read_table
[2][i
] = default_ioport_readl
;
324 ioport_write_table
[0][i
] = default_ioport_writeb
;
325 ioport_write_table
[1][i
] = default_ioport_writew
;
326 ioport_write_table
[2][i
] = default_ioport_writel
;
330 /***********************************************************/
332 void cpu_outb(CPUState
*env
, int addr
, int val
)
335 if (loglevel
& CPU_LOG_IOPORT
)
336 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
338 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
341 env
->last_io_time
= cpu_get_time_fast();
345 void cpu_outw(CPUState
*env
, int addr
, int val
)
348 if (loglevel
& CPU_LOG_IOPORT
)
349 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
351 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
354 env
->last_io_time
= cpu_get_time_fast();
358 void cpu_outl(CPUState
*env
, int addr
, int val
)
361 if (loglevel
& CPU_LOG_IOPORT
)
362 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
364 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
367 env
->last_io_time
= cpu_get_time_fast();
371 int cpu_inb(CPUState
*env
, int addr
)
374 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
376 if (loglevel
& CPU_LOG_IOPORT
)
377 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
381 env
->last_io_time
= cpu_get_time_fast();
386 int cpu_inw(CPUState
*env
, int addr
)
389 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
391 if (loglevel
& CPU_LOG_IOPORT
)
392 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
396 env
->last_io_time
= cpu_get_time_fast();
401 int cpu_inl(CPUState
*env
, int addr
)
404 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
406 if (loglevel
& CPU_LOG_IOPORT
)
407 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
411 env
->last_io_time
= cpu_get_time_fast();
416 /***********************************************************/
417 void hw_error(const char *fmt
, ...)
423 fprintf(stderr
, "qemu: hardware error: ");
424 vfprintf(stderr
, fmt
, ap
);
425 fprintf(stderr
, "\n");
426 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
427 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
429 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
431 cpu_dump_state(env
, stderr
, fprintf
, 0);
438 /***********************************************************/
441 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
442 static void *qemu_put_kbd_event_opaque
;
443 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
444 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
446 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
448 qemu_put_kbd_event_opaque
= opaque
;
449 qemu_put_kbd_event
= func
;
452 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
453 void *opaque
, int absolute
,
456 QEMUPutMouseEntry
*s
, *cursor
;
458 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
462 s
->qemu_put_mouse_event
= func
;
463 s
->qemu_put_mouse_event_opaque
= opaque
;
464 s
->qemu_put_mouse_event_absolute
= absolute
;
465 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
468 if (!qemu_put_mouse_event_head
) {
469 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
473 cursor
= qemu_put_mouse_event_head
;
474 while (cursor
->next
!= NULL
)
475 cursor
= cursor
->next
;
478 qemu_put_mouse_event_current
= s
;
483 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
485 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
487 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
490 cursor
= qemu_put_mouse_event_head
;
491 while (cursor
!= NULL
&& cursor
!= entry
) {
493 cursor
= cursor
->next
;
496 if (cursor
== NULL
) // does not exist or list empty
498 else if (prev
== NULL
) { // entry is head
499 qemu_put_mouse_event_head
= cursor
->next
;
500 if (qemu_put_mouse_event_current
== entry
)
501 qemu_put_mouse_event_current
= cursor
->next
;
502 qemu_free(entry
->qemu_put_mouse_event_name
);
507 prev
->next
= entry
->next
;
509 if (qemu_put_mouse_event_current
== entry
)
510 qemu_put_mouse_event_current
= prev
;
512 qemu_free(entry
->qemu_put_mouse_event_name
);
516 void kbd_put_keycode(int keycode
)
518 if (qemu_put_kbd_event
) {
519 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
523 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
525 QEMUPutMouseEvent
*mouse_event
;
526 void *mouse_event_opaque
;
528 if (!qemu_put_mouse_event_current
) {
533 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
535 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
538 mouse_event(mouse_event_opaque
, dx
, dy
, dz
, buttons_state
);
542 int kbd_mouse_is_absolute(void)
544 if (!qemu_put_mouse_event_current
)
547 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
550 void (*kbd_mouse_set
)(int x
, int y
, int on
) = NULL
;
551 void (*kbd_cursor_define
)(int width
, int height
, int bpp
, int hot_x
, int hot_y
,
552 uint8_t *image
, uint8_t *mask
) = NULL
;
554 void do_info_mice(void)
556 QEMUPutMouseEntry
*cursor
;
559 if (!qemu_put_mouse_event_head
) {
560 term_printf("No mouse devices connected\n");
564 term_printf("Mouse devices available:\n");
565 cursor
= qemu_put_mouse_event_head
;
566 while (cursor
!= NULL
) {
567 term_printf("%c Mouse #%d: %s\n",
568 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
569 index
, cursor
->qemu_put_mouse_event_name
);
571 cursor
= cursor
->next
;
575 void do_mouse_set(int index
)
577 QEMUPutMouseEntry
*cursor
;
580 if (!qemu_put_mouse_event_head
) {
581 term_printf("No mouse devices connected\n");
585 cursor
= qemu_put_mouse_event_head
;
586 while (cursor
!= NULL
&& index
!= i
) {
588 cursor
= cursor
->next
;
592 qemu_put_mouse_event_current
= cursor
;
594 term_printf("Mouse at given index not found\n");
597 /* compute with 96 bit intermediate result: (a*b)/c */
598 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
603 #ifdef WORDS_BIGENDIAN
613 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
614 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
617 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
621 /***********************************************************/
622 /* real time host monotonic timer */
624 #define QEMU_TIMER_BASE 1000000000LL
628 static int64_t clock_freq
;
630 static void init_get_clock(void)
634 ret
= QueryPerformanceFrequency(&freq
);
636 fprintf(stderr
, "Could not calibrate ticks\n");
639 clock_freq
= freq
.QuadPart
;
642 static int64_t get_clock(void)
645 QueryPerformanceCounter(&ti
);
646 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
651 static int use_rt_clock
;
653 static void init_get_clock(void)
656 #if defined(__linux__)
659 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
666 static int64_t get_clock(void)
668 #if defined(__linux__)
671 clock_gettime(CLOCK_MONOTONIC
, &ts
);
672 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
676 /* XXX: using gettimeofday leads to problems if the date
677 changes, so it should be avoided. */
679 gettimeofday(&tv
, NULL
);
680 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
686 /***********************************************************/
687 /* guest cycle counter */
689 static int64_t cpu_ticks_prev
;
690 static int64_t cpu_ticks_offset
;
691 static int64_t cpu_clock_offset
;
692 static int cpu_ticks_enabled
;
694 /* return the host CPU cycle counter and handle stop/restart */
695 int64_t cpu_get_ticks(void)
697 if (!cpu_ticks_enabled
) {
698 return cpu_ticks_offset
;
701 ticks
= cpu_get_real_ticks();
702 if (cpu_ticks_prev
> ticks
) {
703 /* Note: non increasing ticks may happen if the host uses
705 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
707 cpu_ticks_prev
= ticks
;
708 return ticks
+ cpu_ticks_offset
;
712 /* return the host CPU monotonic timer and handle stop/restart */
713 static int64_t cpu_get_clock(void)
716 if (!cpu_ticks_enabled
) {
717 return cpu_clock_offset
;
720 return ti
+ cpu_clock_offset
;
724 /* enable cpu_get_ticks() */
725 void cpu_enable_ticks(void)
727 if (!cpu_ticks_enabled
) {
728 cpu_ticks_offset
-= cpu_get_real_ticks();
729 cpu_clock_offset
-= get_clock();
730 cpu_ticks_enabled
= 1;
734 /* disable cpu_get_ticks() : the clock is stopped. You must not call
735 cpu_get_ticks() after that. */
736 void cpu_disable_ticks(void)
738 if (cpu_ticks_enabled
) {
739 cpu_ticks_offset
= cpu_get_ticks();
740 cpu_clock_offset
= cpu_get_clock();
741 cpu_ticks_enabled
= 0;
745 /***********************************************************/
748 #define QEMU_TIMER_REALTIME 0
749 #define QEMU_TIMER_VIRTUAL 1
753 /* XXX: add frequency */
761 struct QEMUTimer
*next
;
767 static QEMUTimer
*active_timers
[2];
769 static MMRESULT timerID
;
770 static HANDLE host_alarm
= NULL
;
771 static unsigned int period
= 1;
773 /* frequency of the times() clock tick */
774 static int timer_freq
;
777 QEMUClock
*qemu_new_clock(int type
)
780 clock
= qemu_mallocz(sizeof(QEMUClock
));
787 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
791 ts
= qemu_mallocz(sizeof(QEMUTimer
));
798 void qemu_free_timer(QEMUTimer
*ts
)
803 /* stop a timer, but do not dealloc it */
804 void qemu_del_timer(QEMUTimer
*ts
)
808 /* NOTE: this code must be signal safe because
809 qemu_timer_expired() can be called from a signal. */
810 pt
= &active_timers
[ts
->clock
->type
];
823 /* modify the current timer so that it will be fired when current_time
824 >= expire_time. The corresponding callback will be called. */
825 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
831 /* add the timer in the sorted list */
832 /* NOTE: this code must be signal safe because
833 qemu_timer_expired() can be called from a signal. */
834 pt
= &active_timers
[ts
->clock
->type
];
839 if (t
->expire_time
> expire_time
)
843 ts
->expire_time
= expire_time
;
848 int qemu_timer_pending(QEMUTimer
*ts
)
851 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
858 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
862 return (timer_head
->expire_time
<= current_time
);
865 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
871 if (!ts
|| ts
->expire_time
> current_time
)
873 /* remove timer from the list before calling the callback */
874 *ptimer_head
= ts
->next
;
877 /* run the callback (the timer list can be modified) */
882 int64_t qemu_get_clock(QEMUClock
*clock
)
884 switch(clock
->type
) {
885 case QEMU_TIMER_REALTIME
:
886 return get_clock() / 1000000;
888 case QEMU_TIMER_VIRTUAL
:
889 return cpu_get_clock();
893 static void init_timers(void)
896 ticks_per_sec
= QEMU_TIMER_BASE
;
897 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
898 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
902 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
904 uint64_t expire_time
;
906 if (qemu_timer_pending(ts
)) {
907 expire_time
= ts
->expire_time
;
911 qemu_put_be64(f
, expire_time
);
914 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
916 uint64_t expire_time
;
918 expire_time
= qemu_get_be64(f
);
919 if (expire_time
!= -1) {
920 qemu_mod_timer(ts
, expire_time
);
926 static void timer_save(QEMUFile
*f
, void *opaque
)
928 if (cpu_ticks_enabled
) {
929 hw_error("cannot save state if virtual timers are running");
931 qemu_put_be64s(f
, &cpu_ticks_offset
);
932 qemu_put_be64s(f
, &ticks_per_sec
);
933 qemu_put_be64s(f
, &cpu_clock_offset
);
936 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
938 if (version_id
!= 1 && version_id
!= 2)
940 if (cpu_ticks_enabled
) {
943 qemu_get_be64s(f
, &cpu_ticks_offset
);
944 qemu_get_be64s(f
, &ticks_per_sec
);
945 if (version_id
== 2) {
946 qemu_get_be64s(f
, &cpu_clock_offset
);
952 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
953 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
955 static void host_alarm_handler(int host_signum
)
959 #define DISP_FREQ 1000
961 static int64_t delta_min
= INT64_MAX
;
962 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
964 ti
= qemu_get_clock(vm_clock
);
965 if (last_clock
!= 0) {
966 delta
= ti
- last_clock
;
967 if (delta
< delta_min
)
969 if (delta
> delta_max
)
972 if (++count
== DISP_FREQ
) {
973 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
974 muldiv64(delta_min
, 1000000, ticks_per_sec
),
975 muldiv64(delta_max
, 1000000, ticks_per_sec
),
976 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
977 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
979 delta_min
= INT64_MAX
;
987 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
988 qemu_get_clock(vm_clock
)) ||
989 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
990 qemu_get_clock(rt_clock
))) {
992 SetEvent(host_alarm
);
994 CPUState
*env
= cpu_single_env
;
996 /* stop the currently executing cpu because a timer occured */
997 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
999 if (env
->kqemu_enabled
) {
1000 kqemu_cpu_interrupt(env
);
1009 #if defined(__linux__)
1011 #define RTC_FREQ 1024
1015 static int start_rtc_timer(void)
1017 rtc_fd
= open("/dev/rtc", O_RDONLY
);
1020 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1021 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1022 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1023 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1026 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1031 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
1037 static int start_rtc_timer(void)
1042 #endif /* !defined(__linux__) */
1044 #endif /* !defined(_WIN32) */
1046 static void init_timer_alarm(void)
1053 ZeroMemory(&tc
, sizeof(TIMECAPS
));
1054 timeGetDevCaps(&tc
, sizeof(TIMECAPS
));
1055 if (period
< tc
.wPeriodMin
)
1056 period
= tc
.wPeriodMin
;
1057 timeBeginPeriod(period
);
1058 timerID
= timeSetEvent(1, // interval (ms)
1059 period
, // resolution
1060 host_alarm_handler
, // function
1061 (DWORD
)&count
, // user parameter
1062 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
1064 perror("failed timer alarm");
1067 host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1069 perror("failed CreateEvent");
1072 qemu_add_wait_object(host_alarm
, NULL
, NULL
);
1074 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
1077 struct sigaction act
;
1078 struct itimerval itv
;
1080 /* get times() syscall frequency */
1081 timer_freq
= sysconf(_SC_CLK_TCK
);
1084 sigfillset(&act
.sa_mask
);
1086 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1087 act
.sa_flags
|= SA_ONSTACK
;
1089 act
.sa_handler
= host_alarm_handler
;
1090 sigaction(SIGALRM
, &act
, NULL
);
1092 itv
.it_interval
.tv_sec
= 0;
1093 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
1094 itv
.it_value
.tv_sec
= 0;
1095 itv
.it_value
.tv_usec
= 10 * 1000;
1096 setitimer(ITIMER_REAL
, &itv
, NULL
);
1097 /* we probe the tick duration of the kernel to inform the user if
1098 the emulated kernel requested a too high timer frequency */
1099 getitimer(ITIMER_REAL
, &itv
);
1101 #if defined(__linux__)
1102 /* XXX: force /dev/rtc usage because even 2.6 kernels may not
1103 have timers with 1 ms resolution. The correct solution will
1104 be to use the POSIX real time timers available in recent
1106 if (itv
.it_interval
.tv_usec
> 1000 || 1) {
1107 /* try to use /dev/rtc to have a faster timer */
1108 if (start_rtc_timer() < 0)
1110 /* disable itimer */
1111 itv
.it_interval
.tv_sec
= 0;
1112 itv
.it_interval
.tv_usec
= 0;
1113 itv
.it_value
.tv_sec
= 0;
1114 itv
.it_value
.tv_usec
= 0;
1115 setitimer(ITIMER_REAL
, &itv
, NULL
);
1118 sigaction(SIGIO
, &act
, NULL
);
1119 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1120 fcntl(rtc_fd
, F_SETOWN
, getpid());
1122 #endif /* defined(__linux__) */
1125 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1126 PIT_FREQ
) / 1000000;
1132 void quit_timers(void)
1135 timeKillEvent(timerID
);
1136 timeEndPeriod(period
);
1138 CloseHandle(host_alarm
);
1144 /***********************************************************/
1145 /* character device */
1147 static void qemu_chr_event(CharDriverState
*s
, int event
)
1151 s
->chr_event(s
->handler_opaque
, event
);
1154 static void qemu_chr_reset_bh(void *opaque
)
1156 CharDriverState
*s
= opaque
;
1157 qemu_chr_event(s
, CHR_EVENT_RESET
);
1158 qemu_bh_delete(s
->bh
);
1162 void qemu_chr_reset(CharDriverState
*s
)
1164 if (s
->bh
== NULL
) {
1165 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
1166 qemu_bh_schedule(s
->bh
);
1170 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1172 return s
->chr_write(s
, buf
, len
);
1175 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1179 return s
->chr_ioctl(s
, cmd
, arg
);
1182 int qemu_chr_can_read(CharDriverState
*s
)
1184 if (!s
->chr_can_read
)
1186 return s
->chr_can_read(s
->handler_opaque
);
1189 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
1191 s
->chr_read(s
->handler_opaque
, buf
, len
);
1195 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1200 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1201 qemu_chr_write(s
, buf
, strlen(buf
));
1205 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1207 if (s
->chr_send_event
)
1208 s
->chr_send_event(s
, event
);
1211 void qemu_chr_add_handlers(CharDriverState
*s
,
1212 IOCanRWHandler
*fd_can_read
,
1213 IOReadHandler
*fd_read
,
1214 IOEventHandler
*fd_event
,
1217 s
->chr_can_read
= fd_can_read
;
1218 s
->chr_read
= fd_read
;
1219 s
->chr_event
= fd_event
;
1220 s
->handler_opaque
= opaque
;
1221 if (s
->chr_update_read_handler
)
1222 s
->chr_update_read_handler(s
);
1225 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1230 static CharDriverState
*qemu_chr_open_null(void)
1232 CharDriverState
*chr
;
1234 chr
= qemu_mallocz(sizeof(CharDriverState
));
1237 chr
->chr_write
= null_chr_write
;
1241 /* MUX driver for serial I/O splitting */
1242 static int term_timestamps
;
1243 static int64_t term_timestamps_start
;
1246 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
1247 IOReadHandler
*chr_read
[MAX_MUX
];
1248 IOEventHandler
*chr_event
[MAX_MUX
];
1249 void *ext_opaque
[MAX_MUX
];
1250 CharDriverState
*drv
;
1252 int term_got_escape
;
1257 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1259 MuxDriver
*d
= chr
->opaque
;
1261 if (!term_timestamps
) {
1262 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
1267 for(i
= 0; i
< len
; i
++) {
1268 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
1269 if (buf
[i
] == '\n') {
1275 if (term_timestamps_start
== -1)
1276 term_timestamps_start
= ti
;
1277 ti
-= term_timestamps_start
;
1278 secs
= ti
/ 1000000000;
1279 snprintf(buf1
, sizeof(buf1
),
1280 "[%02d:%02d:%02d.%03d] ",
1284 (int)((ti
/ 1000000) % 1000));
1285 d
->drv
->chr_write(d
->drv
, buf1
, strlen(buf1
));
1292 static char *mux_help
[] = {
1293 "% h print this help\n\r",
1294 "% x exit emulator\n\r",
1295 "% s save disk data back to file (if -snapshot)\n\r",
1296 "% t toggle console timestamps\n\r"
1297 "% b send break (magic sysrq)\n\r",
1298 "% c switch between console and monitor\n\r",
1303 static int term_escape_char
= 0x01; /* ctrl-a is used for escape */
1304 static void mux_print_help(CharDriverState
*chr
)
1307 char ebuf
[15] = "Escape-Char";
1308 char cbuf
[50] = "\n\r";
1310 if (term_escape_char
> 0 && term_escape_char
< 26) {
1311 sprintf(cbuf
,"\n\r");
1312 sprintf(ebuf
,"C-%c", term_escape_char
- 1 + 'a');
1314 sprintf(cbuf
,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char
);
1316 chr
->chr_write(chr
, cbuf
, strlen(cbuf
));
1317 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
1318 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
1319 if (mux_help
[i
][j
] == '%')
1320 chr
->chr_write(chr
, ebuf
, strlen(ebuf
));
1322 chr
->chr_write(chr
, &mux_help
[i
][j
], 1);
1327 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
1329 if (d
->term_got_escape
) {
1330 d
->term_got_escape
= 0;
1331 if (ch
== term_escape_char
)
1336 mux_print_help(chr
);
1340 char *term
= "QEMU: Terminated\n\r";
1341 chr
->chr_write(chr
,term
,strlen(term
));
1348 for (i
= 0; i
< MAX_DISKS
; i
++) {
1350 bdrv_commit(bs_table
[i
]);
1356 chr
->chr_event(chr
->opaque
, CHR_EVENT_BREAK
);
1359 /* Switch to the next registered device */
1361 if (chr
->focus
>= d
->mux_cnt
)
1365 term_timestamps
= !term_timestamps
;
1366 term_timestamps_start
= -1;
1369 } else if (ch
== term_escape_char
) {
1370 d
->term_got_escape
= 1;
1378 static int mux_chr_can_read(void *opaque
)
1380 CharDriverState
*chr
= opaque
;
1381 MuxDriver
*d
= chr
->opaque
;
1382 if (d
->chr_can_read
[chr
->focus
])
1383 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
1387 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
1389 CharDriverState
*chr
= opaque
;
1390 MuxDriver
*d
= chr
->opaque
;
1392 for(i
= 0; i
< size
; i
++)
1393 if (mux_proc_byte(chr
, d
, buf
[i
]))
1394 d
->chr_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
], &buf
[i
], 1);
1397 static void mux_chr_event(void *opaque
, int event
)
1399 CharDriverState
*chr
= opaque
;
1400 MuxDriver
*d
= chr
->opaque
;
1403 /* Send the event to all registered listeners */
1404 for (i
= 0; i
< d
->mux_cnt
; i
++)
1405 if (d
->chr_event
[i
])
1406 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
1409 static void mux_chr_update_read_handler(CharDriverState
*chr
)
1411 MuxDriver
*d
= chr
->opaque
;
1413 if (d
->mux_cnt
>= MAX_MUX
) {
1414 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
1417 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
1418 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
1419 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
1420 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
1421 /* Fix up the real driver with mux routines */
1422 if (d
->mux_cnt
== 0) {
1423 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
1424 mux_chr_event
, chr
);
1426 chr
->focus
= d
->mux_cnt
;
1430 CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
1432 CharDriverState
*chr
;
1435 chr
= qemu_mallocz(sizeof(CharDriverState
));
1438 d
= qemu_mallocz(sizeof(MuxDriver
));
1447 chr
->chr_write
= mux_chr_write
;
1448 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
1455 static void socket_cleanup(void)
1460 static int socket_init(void)
1465 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1467 err
= WSAGetLastError();
1468 fprintf(stderr
, "WSAStartup: %d\n", err
);
1471 atexit(socket_cleanup
);
1475 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1481 ret
= send(fd
, buf
, len
, 0);
1484 errno
= WSAGetLastError();
1485 if (errno
!= WSAEWOULDBLOCK
) {
1488 } else if (ret
== 0) {
1498 void socket_set_nonblock(int fd
)
1500 unsigned long opt
= 1;
1501 ioctlsocket(fd
, FIONBIO
, &opt
);
1506 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1512 ret
= write(fd
, buf
, len
);
1514 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1516 } else if (ret
== 0) {
1526 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1528 return unix_write(fd
, buf
, len1
);
1531 void socket_set_nonblock(int fd
)
1533 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1535 #endif /* !_WIN32 */
1544 #define STDIO_MAX_CLIENTS 1
1545 static int stdio_nb_clients
= 0;
1547 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1549 FDCharDriver
*s
= chr
->opaque
;
1550 return unix_write(s
->fd_out
, buf
, len
);
1553 static int fd_chr_read_poll(void *opaque
)
1555 CharDriverState
*chr
= opaque
;
1556 FDCharDriver
*s
= chr
->opaque
;
1558 s
->max_size
= qemu_chr_can_read(chr
);
1562 static void fd_chr_read(void *opaque
)
1564 CharDriverState
*chr
= opaque
;
1565 FDCharDriver
*s
= chr
->opaque
;
1570 if (len
> s
->max_size
)
1574 size
= read(s
->fd_in
, buf
, len
);
1576 /* FD has been closed. Remove it from the active list. */
1577 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1581 qemu_chr_read(chr
, buf
, size
);
1585 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1587 FDCharDriver
*s
= chr
->opaque
;
1589 if (s
->fd_in
>= 0) {
1590 if (nographic
&& s
->fd_in
== 0) {
1592 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1593 fd_chr_read
, NULL
, chr
);
1598 /* open a character device to a unix fd */
1599 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1601 CharDriverState
*chr
;
1604 chr
= qemu_mallocz(sizeof(CharDriverState
));
1607 s
= qemu_mallocz(sizeof(FDCharDriver
));
1615 chr
->chr_write
= fd_chr_write
;
1616 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
1618 qemu_chr_reset(chr
);
1623 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1627 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1630 return qemu_chr_open_fd(-1, fd_out
);
1633 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1636 char filename_in
[256], filename_out
[256];
1638 snprintf(filename_in
, 256, "%s.in", filename
);
1639 snprintf(filename_out
, 256, "%s.out", filename
);
1640 fd_in
= open(filename_in
, O_RDWR
| O_BINARY
);
1641 fd_out
= open(filename_out
, O_RDWR
| O_BINARY
);
1642 if (fd_in
< 0 || fd_out
< 0) {
1647 fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
);
1651 return qemu_chr_open_fd(fd_in
, fd_out
);
1655 /* for STDIO, we handle the case where several clients use it
1658 #define TERM_FIFO_MAX_SIZE 1
1660 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1661 static int term_fifo_size
;
1663 static int stdio_read_poll(void *opaque
)
1665 CharDriverState
*chr
= opaque
;
1667 /* try to flush the queue if needed */
1668 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
1669 qemu_chr_read(chr
, term_fifo
, 1);
1672 /* see if we can absorb more chars */
1673 if (term_fifo_size
== 0)
1679 static void stdio_read(void *opaque
)
1683 CharDriverState
*chr
= opaque
;
1685 size
= read(0, buf
, 1);
1687 /* stdin has been closed. Remove it from the active list. */
1688 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
1692 if (qemu_chr_can_read(chr
) > 0) {
1693 qemu_chr_read(chr
, buf
, 1);
1694 } else if (term_fifo_size
== 0) {
1695 term_fifo
[term_fifo_size
++] = buf
[0];
1700 /* init terminal so that we can grab keys */
1701 static struct termios oldtty
;
1702 static int old_fd0_flags
;
1704 static void term_exit(void)
1706 tcsetattr (0, TCSANOW
, &oldtty
);
1707 fcntl(0, F_SETFL
, old_fd0_flags
);
1710 static void term_init(void)
1714 tcgetattr (0, &tty
);
1716 old_fd0_flags
= fcntl(0, F_GETFL
);
1718 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1719 |INLCR
|IGNCR
|ICRNL
|IXON
);
1720 tty
.c_oflag
|= OPOST
;
1721 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1722 /* if graphical mode, we allow Ctrl-C handling */
1724 tty
.c_lflag
&= ~ISIG
;
1725 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1728 tty
.c_cc
[VTIME
] = 0;
1730 tcsetattr (0, TCSANOW
, &tty
);
1734 fcntl(0, F_SETFL
, O_NONBLOCK
);
1737 static CharDriverState
*qemu_chr_open_stdio(void)
1739 CharDriverState
*chr
;
1741 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1743 chr
= qemu_chr_open_fd(0, 1);
1744 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
1751 #if defined(__linux__)
1752 static CharDriverState
*qemu_chr_open_pty(void)
1755 char slave_name
[1024];
1756 int master_fd
, slave_fd
;
1758 /* Not satisfying */
1759 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1763 /* Disabling local echo and line-buffered output */
1764 tcgetattr (master_fd
, &tty
);
1765 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1767 tty
.c_cc
[VTIME
] = 0;
1768 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1770 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1771 return qemu_chr_open_fd(master_fd
, master_fd
);
1774 static void tty_serial_init(int fd
, int speed
,
1775 int parity
, int data_bits
, int stop_bits
)
1781 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1782 speed
, parity
, data_bits
, stop_bits
);
1784 tcgetattr (fd
, &tty
);
1826 cfsetispeed(&tty
, spd
);
1827 cfsetospeed(&tty
, spd
);
1829 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1830 |INLCR
|IGNCR
|ICRNL
|IXON
);
1831 tty
.c_oflag
|= OPOST
;
1832 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1833 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1854 tty
.c_cflag
|= PARENB
;
1857 tty
.c_cflag
|= PARENB
| PARODD
;
1861 tty
.c_cflag
|= CSTOPB
;
1863 tcsetattr (fd
, TCSANOW
, &tty
);
1866 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1868 FDCharDriver
*s
= chr
->opaque
;
1871 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1873 QEMUSerialSetParams
*ssp
= arg
;
1874 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1875 ssp
->data_bits
, ssp
->stop_bits
);
1878 case CHR_IOCTL_SERIAL_SET_BREAK
:
1880 int enable
= *(int *)arg
;
1882 tcsendbreak(s
->fd_in
, 1);
1891 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1893 CharDriverState
*chr
;
1896 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1899 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1900 tty_serial_init(fd
, 115200, 'N', 8, 1);
1901 chr
= qemu_chr_open_fd(fd
, fd
);
1904 chr
->chr_ioctl
= tty_serial_ioctl
;
1905 qemu_chr_reset(chr
);
1912 } ParallelCharDriver
;
1914 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1916 if (s
->mode
!= mode
) {
1918 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1925 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1927 ParallelCharDriver
*drv
= chr
->opaque
;
1932 case CHR_IOCTL_PP_READ_DATA
:
1933 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1935 *(uint8_t *)arg
= b
;
1937 case CHR_IOCTL_PP_WRITE_DATA
:
1938 b
= *(uint8_t *)arg
;
1939 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1942 case CHR_IOCTL_PP_READ_CONTROL
:
1943 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1945 /* Linux gives only the lowest bits, and no way to know data
1946 direction! For better compatibility set the fixed upper
1948 *(uint8_t *)arg
= b
| 0xc0;
1950 case CHR_IOCTL_PP_WRITE_CONTROL
:
1951 b
= *(uint8_t *)arg
;
1952 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1955 case CHR_IOCTL_PP_READ_STATUS
:
1956 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1958 *(uint8_t *)arg
= b
;
1960 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1961 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1962 struct ParallelIOArg
*parg
= arg
;
1963 int n
= read(fd
, parg
->buffer
, parg
->count
);
1964 if (n
!= parg
->count
) {
1969 case CHR_IOCTL_PP_EPP_READ
:
1970 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1971 struct ParallelIOArg
*parg
= arg
;
1972 int n
= read(fd
, parg
->buffer
, parg
->count
);
1973 if (n
!= parg
->count
) {
1978 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1979 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1980 struct ParallelIOArg
*parg
= arg
;
1981 int n
= write(fd
, parg
->buffer
, parg
->count
);
1982 if (n
!= parg
->count
) {
1987 case CHR_IOCTL_PP_EPP_WRITE
:
1988 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1989 struct ParallelIOArg
*parg
= arg
;
1990 int n
= write(fd
, parg
->buffer
, parg
->count
);
1991 if (n
!= parg
->count
) {
2002 static void pp_close(CharDriverState
*chr
)
2004 ParallelCharDriver
*drv
= chr
->opaque
;
2007 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
2008 ioctl(fd
, PPRELEASE
);
2013 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
2015 CharDriverState
*chr
;
2016 ParallelCharDriver
*drv
;
2019 fd
= open(filename
, O_RDWR
);
2023 if (ioctl(fd
, PPCLAIM
) < 0) {
2028 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
2034 drv
->mode
= IEEE1284_MODE_COMPAT
;
2036 chr
= qemu_mallocz(sizeof(CharDriverState
));
2042 chr
->chr_write
= null_chr_write
;
2043 chr
->chr_ioctl
= pp_ioctl
;
2044 chr
->chr_close
= pp_close
;
2047 qemu_chr_reset(chr
);
2053 static CharDriverState
*qemu_chr_open_pty(void)
2059 #endif /* !defined(_WIN32) */
2064 HANDLE hcom
, hrecv
, hsend
;
2065 OVERLAPPED orecv
, osend
;
2070 #define NSENDBUF 2048
2071 #define NRECVBUF 2048
2072 #define MAXCONNECT 1
2073 #define NTIMEOUT 5000
2075 static int win_chr_poll(void *opaque
);
2076 static int win_chr_pipe_poll(void *opaque
);
2078 static void win_chr_close(CharDriverState
*chr
)
2080 WinCharState
*s
= chr
->opaque
;
2083 CloseHandle(s
->hsend
);
2087 CloseHandle(s
->hrecv
);
2091 CloseHandle(s
->hcom
);
2095 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
2097 qemu_del_polling_cb(win_chr_poll
, chr
);
2100 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
2102 WinCharState
*s
= chr
->opaque
;
2104 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2109 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2111 fprintf(stderr
, "Failed CreateEvent\n");
2114 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2116 fprintf(stderr
, "Failed CreateEvent\n");
2120 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2121 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2122 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2123 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
2128 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2129 fprintf(stderr
, "Failed SetupComm\n");
2133 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2134 size
= sizeof(COMMCONFIG
);
2135 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2136 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2137 CommConfigDialog(filename
, NULL
, &comcfg
);
2139 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2140 fprintf(stderr
, "Failed SetCommState\n");
2144 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2145 fprintf(stderr
, "Failed SetCommMask\n");
2149 cto
.ReadIntervalTimeout
= MAXDWORD
;
2150 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2151 fprintf(stderr
, "Failed SetCommTimeouts\n");
2155 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2156 fprintf(stderr
, "Failed ClearCommError\n");
2159 qemu_add_polling_cb(win_chr_poll
, chr
);
2167 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2169 WinCharState
*s
= chr
->opaque
;
2170 DWORD len
, ret
, size
, err
;
2173 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2174 s
->osend
.hEvent
= s
->hsend
;
2177 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2179 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2181 err
= GetLastError();
2182 if (err
== ERROR_IO_PENDING
) {
2183 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2201 static int win_chr_read_poll(CharDriverState
*chr
)
2203 WinCharState
*s
= chr
->opaque
;
2205 s
->max_size
= qemu_chr_can_read(chr
);
2209 static void win_chr_readfile(CharDriverState
*chr
)
2211 WinCharState
*s
= chr
->opaque
;
2216 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2217 s
->orecv
.hEvent
= s
->hrecv
;
2218 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2220 err
= GetLastError();
2221 if (err
== ERROR_IO_PENDING
) {
2222 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2227 qemu_chr_read(chr
, buf
, size
);
2231 static void win_chr_read(CharDriverState
*chr
)
2233 WinCharState
*s
= chr
->opaque
;
2235 if (s
->len
> s
->max_size
)
2236 s
->len
= s
->max_size
;
2240 win_chr_readfile(chr
);
2243 static int win_chr_poll(void *opaque
)
2245 CharDriverState
*chr
= opaque
;
2246 WinCharState
*s
= chr
->opaque
;
2250 ClearCommError(s
->hcom
, &comerr
, &status
);
2251 if (status
.cbInQue
> 0) {
2252 s
->len
= status
.cbInQue
;
2253 win_chr_read_poll(chr
);
2260 static CharDriverState
*qemu_chr_open_win(const char *filename
)
2262 CharDriverState
*chr
;
2265 chr
= qemu_mallocz(sizeof(CharDriverState
));
2268 s
= qemu_mallocz(sizeof(WinCharState
));
2274 chr
->chr_write
= win_chr_write
;
2275 chr
->chr_close
= win_chr_close
;
2277 if (win_chr_init(chr
, filename
) < 0) {
2282 qemu_chr_reset(chr
);
2286 static int win_chr_pipe_poll(void *opaque
)
2288 CharDriverState
*chr
= opaque
;
2289 WinCharState
*s
= chr
->opaque
;
2292 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2295 win_chr_read_poll(chr
);
2302 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
2304 WinCharState
*s
= chr
->opaque
;
2312 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2314 fprintf(stderr
, "Failed CreateEvent\n");
2317 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2319 fprintf(stderr
, "Failed CreateEvent\n");
2323 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2324 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2325 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2327 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2328 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2329 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2334 ZeroMemory(&ov
, sizeof(ov
));
2335 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2336 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2338 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2342 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2344 fprintf(stderr
, "Failed GetOverlappedResult\n");
2346 CloseHandle(ov
.hEvent
);
2353 CloseHandle(ov
.hEvent
);
2356 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2365 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2367 CharDriverState
*chr
;
2370 chr
= qemu_mallocz(sizeof(CharDriverState
));
2373 s
= qemu_mallocz(sizeof(WinCharState
));
2379 chr
->chr_write
= win_chr_write
;
2380 chr
->chr_close
= win_chr_close
;
2382 if (win_chr_pipe_init(chr
, filename
) < 0) {
2387 qemu_chr_reset(chr
);
2391 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2393 CharDriverState
*chr
;
2396 chr
= qemu_mallocz(sizeof(CharDriverState
));
2399 s
= qemu_mallocz(sizeof(WinCharState
));
2406 chr
->chr_write
= win_chr_write
;
2407 qemu_chr_reset(chr
);
2411 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2415 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2416 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2417 if (fd_out
== INVALID_HANDLE_VALUE
)
2420 return qemu_chr_open_win_file(fd_out
);
2424 /***********************************************************/
2425 /* UDP Net console */
2429 struct sockaddr_in daddr
;
2436 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2438 NetCharDriver
*s
= chr
->opaque
;
2440 return sendto(s
->fd
, buf
, len
, 0,
2441 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2444 static int udp_chr_read_poll(void *opaque
)
2446 CharDriverState
*chr
= opaque
;
2447 NetCharDriver
*s
= chr
->opaque
;
2449 s
->max_size
= qemu_chr_can_read(chr
);
2451 /* If there were any stray characters in the queue process them
2454 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2455 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2457 s
->max_size
= qemu_chr_can_read(chr
);
2462 static void udp_chr_read(void *opaque
)
2464 CharDriverState
*chr
= opaque
;
2465 NetCharDriver
*s
= chr
->opaque
;
2467 if (s
->max_size
== 0)
2469 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2470 s
->bufptr
= s
->bufcnt
;
2475 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2476 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2478 s
->max_size
= qemu_chr_can_read(chr
);
2482 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2484 NetCharDriver
*s
= chr
->opaque
;
2487 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2488 udp_chr_read
, NULL
, chr
);
2492 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2494 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2496 int parse_host_src_port(struct sockaddr_in
*haddr
,
2497 struct sockaddr_in
*saddr
,
2500 static CharDriverState
*qemu_chr_open_udp(const char *def
)
2502 CharDriverState
*chr
= NULL
;
2503 NetCharDriver
*s
= NULL
;
2505 struct sockaddr_in saddr
;
2507 chr
= qemu_mallocz(sizeof(CharDriverState
));
2510 s
= qemu_mallocz(sizeof(NetCharDriver
));
2514 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2516 perror("socket(PF_INET, SOCK_DGRAM)");
2520 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2521 printf("Could not parse: %s\n", def
);
2525 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2535 chr
->chr_write
= udp_chr_write
;
2536 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2549 /***********************************************************/
2550 /* TCP Net console */
2561 static void tcp_chr_accept(void *opaque
);
2563 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2565 TCPCharDriver
*s
= chr
->opaque
;
2567 return send_all(s
->fd
, buf
, len
);
2569 /* XXX: indicate an error ? */
2574 static int tcp_chr_read_poll(void *opaque
)
2576 CharDriverState
*chr
= opaque
;
2577 TCPCharDriver
*s
= chr
->opaque
;
2580 s
->max_size
= qemu_chr_can_read(chr
);
2585 #define IAC_BREAK 243
2586 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2588 char *buf
, int *size
)
2590 /* Handle any telnet client's basic IAC options to satisfy char by
2591 * char mode with no echo. All IAC options will be removed from
2592 * the buf and the do_telnetopt variable will be used to track the
2593 * state of the width of the IAC information.
2595 * IAC commands come in sets of 3 bytes with the exception of the
2596 * "IAC BREAK" command and the double IAC.
2602 for (i
= 0; i
< *size
; i
++) {
2603 if (s
->do_telnetopt
> 1) {
2604 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2605 /* Double IAC means send an IAC */
2609 s
->do_telnetopt
= 1;
2611 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2612 /* Handle IAC break commands by sending a serial break */
2613 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
2618 if (s
->do_telnetopt
>= 4) {
2619 s
->do_telnetopt
= 1;
2622 if ((unsigned char)buf
[i
] == IAC
) {
2623 s
->do_telnetopt
= 2;
2634 static void tcp_chr_read(void *opaque
)
2636 CharDriverState
*chr
= opaque
;
2637 TCPCharDriver
*s
= chr
->opaque
;
2641 if (!s
->connected
|| s
->max_size
<= 0)
2644 if (len
> s
->max_size
)
2646 size
= recv(s
->fd
, buf
, len
, 0);
2648 /* connection closed */
2650 if (s
->listen_fd
>= 0) {
2651 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2653 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2656 } else if (size
> 0) {
2657 if (s
->do_telnetopt
)
2658 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2660 qemu_chr_read(chr
, buf
, size
);
2664 static void tcp_chr_connect(void *opaque
)
2666 CharDriverState
*chr
= opaque
;
2667 TCPCharDriver
*s
= chr
->opaque
;
2670 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2671 tcp_chr_read
, NULL
, chr
);
2672 qemu_chr_reset(chr
);
2675 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2676 static void tcp_chr_telnet_init(int fd
)
2679 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2680 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2681 send(fd
, (char *)buf
, 3, 0);
2682 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2683 send(fd
, (char *)buf
, 3, 0);
2684 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2685 send(fd
, (char *)buf
, 3, 0);
2686 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2687 send(fd
, (char *)buf
, 3, 0);
2690 static void socket_set_nodelay(int fd
)
2693 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2696 static void tcp_chr_accept(void *opaque
)
2698 CharDriverState
*chr
= opaque
;
2699 TCPCharDriver
*s
= chr
->opaque
;
2700 struct sockaddr_in saddr
;
2702 struct sockaddr_un uaddr
;
2704 struct sockaddr
*addr
;
2711 len
= sizeof(uaddr
);
2712 addr
= (struct sockaddr
*)&uaddr
;
2716 len
= sizeof(saddr
);
2717 addr
= (struct sockaddr
*)&saddr
;
2719 fd
= accept(s
->listen_fd
, addr
, &len
);
2720 if (fd
< 0 && errno
!= EINTR
) {
2722 } else if (fd
>= 0) {
2723 if (s
->do_telnetopt
)
2724 tcp_chr_telnet_init(fd
);
2728 socket_set_nonblock(fd
);
2730 socket_set_nodelay(fd
);
2732 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2733 tcp_chr_connect(chr
);
2736 static void tcp_chr_close(CharDriverState
*chr
)
2738 TCPCharDriver
*s
= chr
->opaque
;
2741 if (s
->listen_fd
>= 0)
2742 closesocket(s
->listen_fd
);
2746 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2750 CharDriverState
*chr
= NULL
;
2751 TCPCharDriver
*s
= NULL
;
2752 int fd
= -1, ret
, err
, val
;
2754 int is_waitconnect
= 1;
2757 struct sockaddr_in saddr
;
2759 struct sockaddr_un uaddr
;
2761 struct sockaddr
*addr
;
2766 addr
= (struct sockaddr
*)&uaddr
;
2767 addrlen
= sizeof(uaddr
);
2768 if (parse_unix_path(&uaddr
, host_str
) < 0)
2773 addr
= (struct sockaddr
*)&saddr
;
2774 addrlen
= sizeof(saddr
);
2775 if (parse_host_port(&saddr
, host_str
) < 0)
2780 while((ptr
= strchr(ptr
,','))) {
2782 if (!strncmp(ptr
,"server",6)) {
2784 } else if (!strncmp(ptr
,"nowait",6)) {
2786 } else if (!strncmp(ptr
,"nodelay",6)) {
2789 printf("Unknown option: %s\n", ptr
);
2796 chr
= qemu_mallocz(sizeof(CharDriverState
));
2799 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2805 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2808 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2813 if (!is_waitconnect
)
2814 socket_set_nonblock(fd
);
2819 s
->is_unix
= is_unix
;
2820 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2823 chr
->chr_write
= tcp_chr_write
;
2824 chr
->chr_close
= tcp_chr_close
;
2827 /* allow fast reuse */
2831 strncpy(path
, uaddr
.sun_path
, 108);
2838 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2841 ret
= bind(fd
, addr
, addrlen
);
2845 ret
= listen(fd
, 0);
2850 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2852 s
->do_telnetopt
= 1;
2855 ret
= connect(fd
, addr
, addrlen
);
2857 err
= socket_error();
2858 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2859 } else if (err
== EINPROGRESS
) {
2862 } else if (err
== WSAEALREADY
) {
2874 socket_set_nodelay(fd
);
2876 tcp_chr_connect(chr
);
2878 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2881 if (is_listen
&& is_waitconnect
) {
2882 printf("QEMU waiting for connection on: %s\n", host_str
);
2883 tcp_chr_accept(chr
);
2884 socket_set_nonblock(s
->listen_fd
);
2896 CharDriverState
*qemu_chr_open(const char *filename
)
2900 if (!strcmp(filename
, "vc")) {
2901 return text_console_init(&display_state
);
2902 } else if (!strcmp(filename
, "null")) {
2903 return qemu_chr_open_null();
2905 if (strstart(filename
, "tcp:", &p
)) {
2906 return qemu_chr_open_tcp(p
, 0, 0);
2908 if (strstart(filename
, "telnet:", &p
)) {
2909 return qemu_chr_open_tcp(p
, 1, 0);
2911 if (strstart(filename
, "udp:", &p
)) {
2912 return qemu_chr_open_udp(p
);
2914 if (strstart(filename
, "mon:", &p
)) {
2915 CharDriverState
*drv
= qemu_chr_open(p
);
2917 drv
= qemu_chr_open_mux(drv
);
2918 monitor_init(drv
, !nographic
);
2921 printf("Unable to open driver: %s\n", p
);
2925 if (strstart(filename
, "unix:", &p
)) {
2926 return qemu_chr_open_tcp(p
, 0, 1);
2927 } else if (strstart(filename
, "file:", &p
)) {
2928 return qemu_chr_open_file_out(p
);
2929 } else if (strstart(filename
, "pipe:", &p
)) {
2930 return qemu_chr_open_pipe(p
);
2931 } else if (!strcmp(filename
, "pty")) {
2932 return qemu_chr_open_pty();
2933 } else if (!strcmp(filename
, "stdio")) {
2934 return qemu_chr_open_stdio();
2937 #if defined(__linux__)
2938 if (strstart(filename
, "/dev/parport", NULL
)) {
2939 return qemu_chr_open_pp(filename
);
2941 if (strstart(filename
, "/dev/", NULL
)) {
2942 return qemu_chr_open_tty(filename
);
2946 if (strstart(filename
, "COM", NULL
)) {
2947 return qemu_chr_open_win(filename
);
2949 if (strstart(filename
, "pipe:", &p
)) {
2950 return qemu_chr_open_win_pipe(p
);
2952 if (strstart(filename
, "file:", &p
)) {
2953 return qemu_chr_open_win_file_out(p
);
2961 void qemu_chr_close(CharDriverState
*chr
)
2964 chr
->chr_close(chr
);
2967 /***********************************************************/
2968 /* network device redirectors */
2970 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2974 for(i
=0;i
<size
;i
+=16) {
2978 fprintf(f
, "%08x ", i
);
2981 fprintf(f
, " %02x", buf
[i
+j
]);
2986 for(j
=0;j
<len
;j
++) {
2988 if (c
< ' ' || c
> '~')
2990 fprintf(f
, "%c", c
);
2996 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
2999 for(i
= 0; i
< 6; i
++) {
3000 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3013 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
3018 p1
= strchr(p
, sep
);
3024 if (len
> buf_size
- 1)
3026 memcpy(buf
, p
, len
);
3033 int parse_host_src_port(struct sockaddr_in
*haddr
,
3034 struct sockaddr_in
*saddr
,
3035 const char *input_str
)
3037 char *str
= strdup(input_str
);
3038 char *host_str
= str
;
3043 * Chop off any extra arguments at the end of the string which
3044 * would start with a comma, then fill in the src port information
3045 * if it was provided else use the "any address" and "any port".
3047 if ((ptr
= strchr(str
,',')))
3050 if ((src_str
= strchr(input_str
,'@'))) {
3055 if (parse_host_port(haddr
, host_str
) < 0)
3058 if (!src_str
|| *src_str
== '\0')
3061 if (parse_host_port(saddr
, src_str
) < 0)
3072 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
3080 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3082 saddr
->sin_family
= AF_INET
;
3083 if (buf
[0] == '\0') {
3084 saddr
->sin_addr
.s_addr
= 0;
3086 if (isdigit(buf
[0])) {
3087 if (!inet_aton(buf
, &saddr
->sin_addr
))
3090 if ((he
= gethostbyname(buf
)) == NULL
)
3092 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
3095 port
= strtol(p
, (char **)&r
, 0);
3098 saddr
->sin_port
= htons(port
);
3103 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
3108 len
= MIN(108, strlen(str
));
3109 p
= strchr(str
, ',');
3111 len
= MIN(len
, p
- str
);
3113 memset(uaddr
, 0, sizeof(*uaddr
));
3115 uaddr
->sun_family
= AF_UNIX
;
3116 memcpy(uaddr
->sun_path
, str
, len
);
3122 /* find or alloc a new VLAN */
3123 VLANState
*qemu_find_vlan(int id
)
3125 VLANState
**pvlan
, *vlan
;
3126 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3130 vlan
= qemu_mallocz(sizeof(VLANState
));
3135 pvlan
= &first_vlan
;
3136 while (*pvlan
!= NULL
)
3137 pvlan
= &(*pvlan
)->next
;
3142 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
3143 IOReadHandler
*fd_read
,
3144 IOCanRWHandler
*fd_can_read
,
3147 VLANClientState
*vc
, **pvc
;
3148 vc
= qemu_mallocz(sizeof(VLANClientState
));
3151 vc
->fd_read
= fd_read
;
3152 vc
->fd_can_read
= fd_can_read
;
3153 vc
->opaque
= opaque
;
3157 pvc
= &vlan
->first_client
;
3158 while (*pvc
!= NULL
)
3159 pvc
= &(*pvc
)->next
;
3164 int qemu_can_send_packet(VLANClientState
*vc1
)
3166 VLANState
*vlan
= vc1
->vlan
;
3167 VLANClientState
*vc
;
3169 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3171 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
3178 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
3180 VLANState
*vlan
= vc1
->vlan
;
3181 VLANClientState
*vc
;
3184 printf("vlan %d send:\n", vlan
->id
);
3185 hex_dump(stdout
, buf
, size
);
3187 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3189 vc
->fd_read(vc
->opaque
, buf
, size
);
3194 #if defined(CONFIG_SLIRP)
3196 /* slirp network adapter */
3198 static int slirp_inited
;
3199 static VLANClientState
*slirp_vc
;
3201 int slirp_can_output(void)
3203 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
3206 void slirp_output(const uint8_t *pkt
, int pkt_len
)
3209 printf("slirp output:\n");
3210 hex_dump(stdout
, pkt
, pkt_len
);
3214 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
3217 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
3220 printf("slirp input:\n");
3221 hex_dump(stdout
, buf
, size
);
3223 slirp_input(buf
, size
);
3226 static int net_slirp_init(VLANState
*vlan
)
3228 if (!slirp_inited
) {
3232 slirp_vc
= qemu_new_vlan_client(vlan
,
3233 slirp_receive
, NULL
, NULL
);
3234 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
3238 static void net_slirp_redir(const char *redir_str
)
3243 struct in_addr guest_addr
;
3244 int host_port
, guest_port
;
3246 if (!slirp_inited
) {
3252 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3254 if (!strcmp(buf
, "tcp")) {
3256 } else if (!strcmp(buf
, "udp")) {
3262 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3264 host_port
= strtol(buf
, &r
, 0);
3268 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3270 if (buf
[0] == '\0') {
3271 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
3273 if (!inet_aton(buf
, &guest_addr
))
3276 guest_port
= strtol(p
, &r
, 0);
3280 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3281 fprintf(stderr
, "qemu: could not set up redirection\n");
3286 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3294 static void smb_exit(void)
3298 char filename
[1024];
3300 /* erase all the files in the directory */
3301 d
= opendir(smb_dir
);
3306 if (strcmp(de
->d_name
, ".") != 0 &&
3307 strcmp(de
->d_name
, "..") != 0) {
3308 snprintf(filename
, sizeof(filename
), "%s/%s",
3309 smb_dir
, de
->d_name
);
3317 /* automatic user mode samba server configuration */
3318 void net_slirp_smb(const char *exported_dir
)
3320 char smb_conf
[1024];
3321 char smb_cmdline
[1024];
3324 if (!slirp_inited
) {
3329 /* XXX: better tmp dir construction */
3330 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3331 if (mkdir(smb_dir
, 0700) < 0) {
3332 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3335 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3337 f
= fopen(smb_conf
, "w");
3339 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3346 "socket address=127.0.0.1\n"
3347 "pid directory=%s\n"
3348 "lock directory=%s\n"
3349 "log file=%s/log.smbd\n"
3350 "smb passwd file=%s/smbpasswd\n"
3351 "security = share\n"
3366 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3367 SMBD_COMMAND
, smb_conf
);
3369 slirp_add_exec(0, smb_cmdline
, 4, 139);
3372 #endif /* !defined(_WIN32) */
3374 #endif /* CONFIG_SLIRP */
3376 #if !defined(_WIN32)
3378 typedef struct TAPState
{
3379 VLANClientState
*vc
;
3383 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3385 TAPState
*s
= opaque
;
3388 ret
= write(s
->fd
, buf
, size
);
3389 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3396 static void tap_send(void *opaque
)
3398 TAPState
*s
= opaque
;
3405 sbuf
.maxlen
= sizeof(buf
);
3407 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
3409 size
= read(s
->fd
, buf
, sizeof(buf
));
3412 qemu_send_packet(s
->vc
, buf
, size
);
3418 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3422 s
= qemu_mallocz(sizeof(TAPState
));
3426 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3427 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3428 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3433 static int tap_open(char *ifname
, int ifname_size
)
3439 fd
= open("/dev/tap", O_RDWR
);
3441 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3446 dev
= devname(s
.st_rdev
, S_IFCHR
);
3447 pstrcpy(ifname
, ifname_size
, dev
);
3449 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3452 #elif defined(__sun__)
3453 #define TUNNEWPPA (('T'<<16) | 0x0001)
3455 * Allocate TAP device, returns opened fd.
3456 * Stores dev name in the first arg(must be large enough).
3458 int tap_alloc(char *dev
)
3460 int tap_fd
, if_fd
, ppa
= -1;
3461 static int ip_fd
= 0;
3464 static int arp_fd
= 0;
3465 int ip_muxid
, arp_muxid
;
3466 struct strioctl strioc_if
, strioc_ppa
;
3467 int link_type
= I_PLINK
;;
3469 char actual_name
[32] = "";
3471 memset(&ifr
, 0x0, sizeof(ifr
));
3475 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
3479 /* Check if IP device was opened */
3483 if( (ip_fd
= open("/dev/udp", O_RDWR
, 0)) < 0){
3484 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
3488 if( (tap_fd
= open("/dev/tap", O_RDWR
, 0)) < 0){
3489 syslog(LOG_ERR
, "Can't open /dev/tap");
3493 /* Assign a new PPA and get its unit number. */
3494 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
3495 strioc_ppa
.ic_timout
= 0;
3496 strioc_ppa
.ic_len
= sizeof(ppa
);
3497 strioc_ppa
.ic_dp
= (char *)&ppa
;
3498 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
3499 syslog (LOG_ERR
, "Can't assign new interface");
3501 if( (if_fd
= open("/dev/tap", O_RDWR
, 0)) < 0){
3502 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
3505 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
3506 syslog(LOG_ERR
, "Can't push IP module");
3510 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
3511 syslog(LOG_ERR
, "Can't get flags\n");
3513 snprintf (actual_name
, 32, "tap%d", ppa
);
3514 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3517 /* Assign ppa according to the unit number returned by tun device */
3519 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
3520 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
3521 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
3522 syslog (LOG_ERR
, "Can't get flags\n");
3523 /* Push arp module to if_fd */
3524 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
3525 syslog (LOG_ERR
, "Can't push ARP module (2)");
3527 /* Push arp module to ip_fd */
3528 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
3529 syslog (LOG_ERR
, "I_POP failed\n");
3530 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
3531 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
3533 if ((arp_fd
= open ("/dev/tap", O_RDWR
, 0)) < 0)
3534 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
3536 /* Set ifname to arp */
3537 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
3538 strioc_if
.ic_timout
= 0;
3539 strioc_if
.ic_len
= sizeof(ifr
);
3540 strioc_if
.ic_dp
= (char *)&ifr
;
3541 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
3542 syslog (LOG_ERR
, "Can't set ifname to arp\n");
3545 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
3546 syslog(LOG_ERR
, "Can't link TAP device to IP");
3550 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
3551 syslog (LOG_ERR
, "Can't link TAP device to ARP");
3555 memset(&ifr
, 0x0, sizeof(ifr
));
3556 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3557 ifr
.lifr_ip_muxid
= ip_muxid
;
3558 ifr
.lifr_arp_muxid
= arp_muxid
;
3560 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
3562 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
3563 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
3564 syslog (LOG_ERR
, "Can't set multiplexor id");
3567 sprintf(dev
, "tap%d", ppa
);
3571 static int tap_open(char *ifname
, int ifname_size
)
3575 if( (fd
= tap_alloc(dev
)) < 0 ){
3576 fprintf(stderr
, "Cannot allocate TAP device\n");
3579 pstrcpy(ifname
, ifname_size
, dev
);
3580 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3584 static int tap_open(char *ifname
, int ifname_size
)
3589 fd
= open("/dev/net/tun", O_RDWR
);
3591 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
3594 memset(&ifr
, 0, sizeof(ifr
));
3595 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
3596 if (ifname
[0] != '\0')
3597 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
3599 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
3600 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
3602 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
3606 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
3607 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3612 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
3613 const char *setup_script
)
3616 int pid
, status
, fd
;
3621 if (ifname1
!= NULL
)
3622 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
3625 fd
= tap_open(ifname
, sizeof(ifname
));
3629 if (!setup_script
|| !strcmp(setup_script
, "no"))
3631 if (setup_script
[0] != '\0') {
3632 /* try to launch network init script */
3636 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
3637 for (i
= 0; i
< open_max
; i
++)
3638 if (i
!= STDIN_FILENO
&&
3639 i
!= STDOUT_FILENO
&&
3640 i
!= STDERR_FILENO
&&
3645 *parg
++ = (char *)setup_script
;
3648 execv(setup_script
, args
);
3651 while (waitpid(pid
, &status
, 0) != pid
);
3652 if (!WIFEXITED(status
) ||
3653 WEXITSTATUS(status
) != 0) {
3654 fprintf(stderr
, "%s: could not launch network script\n",
3660 s
= net_tap_fd_init(vlan
, fd
);
3663 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3664 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
3668 #endif /* !_WIN32 */
3670 /* network connection */
3671 typedef struct NetSocketState
{
3672 VLANClientState
*vc
;
3674 int state
; /* 0 = getting length, 1 = getting data */
3678 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
3681 typedef struct NetSocketListenState
{
3684 } NetSocketListenState
;
3686 /* XXX: we consider we can send the whole packet without blocking */
3687 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
3689 NetSocketState
*s
= opaque
;
3693 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
3694 send_all(s
->fd
, buf
, size
);
3697 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
3699 NetSocketState
*s
= opaque
;
3700 sendto(s
->fd
, buf
, size
, 0,
3701 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
3704 static void net_socket_send(void *opaque
)
3706 NetSocketState
*s
= opaque
;
3711 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
3713 err
= socket_error();
3714 if (err
!= EWOULDBLOCK
)
3716 } else if (size
== 0) {
3717 /* end of connection */
3719 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3725 /* reassemble a packet from the network */
3731 memcpy(s
->buf
+ s
->index
, buf
, l
);
3735 if (s
->index
== 4) {
3737 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
3743 l
= s
->packet_len
- s
->index
;
3746 memcpy(s
->buf
+ s
->index
, buf
, l
);
3750 if (s
->index
>= s
->packet_len
) {
3751 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
3760 static void net_socket_send_dgram(void *opaque
)
3762 NetSocketState
*s
= opaque
;
3765 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
3769 /* end of connection */
3770 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3773 qemu_send_packet(s
->vc
, s
->buf
, size
);
3776 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
3781 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
3782 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
3783 inet_ntoa(mcastaddr
->sin_addr
),
3784 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
3788 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
3790 perror("socket(PF_INET, SOCK_DGRAM)");
3795 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
3796 (const char *)&val
, sizeof(val
));
3798 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
3802 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
3808 /* Add host to multicast group */
3809 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
3810 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
3812 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
3813 (const char *)&imr
, sizeof(struct ip_mreq
));
3815 perror("setsockopt(IP_ADD_MEMBERSHIP)");
3819 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
3821 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
3822 (const char *)&val
, sizeof(val
));
3824 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
3828 socket_set_nonblock(fd
);
3836 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
3839 struct sockaddr_in saddr
;
3841 socklen_t saddr_len
;
3844 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
3845 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
3846 * by ONLY ONE process: we must "clone" this dgram socket --jjo
3850 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
3852 if (saddr
.sin_addr
.s_addr
==0) {
3853 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
3857 /* clone dgram socket */
3858 newfd
= net_socket_mcast_create(&saddr
);
3860 /* error already reported by net_socket_mcast_create() */
3864 /* clone newfd to fd, close newfd */
3869 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
3870 fd
, strerror(errno
));
3875 s
= qemu_mallocz(sizeof(NetSocketState
));
3880 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
3881 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
3883 /* mcast: save bound address as dst */
3884 if (is_connected
) s
->dgram_dst
=saddr
;
3886 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3887 "socket: fd=%d (%s mcast=%s:%d)",
3888 fd
, is_connected
? "cloned" : "",
3889 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3893 static void net_socket_connect(void *opaque
)
3895 NetSocketState
*s
= opaque
;
3896 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
3899 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
3903 s
= qemu_mallocz(sizeof(NetSocketState
));
3907 s
->vc
= qemu_new_vlan_client(vlan
,
3908 net_socket_receive
, NULL
, s
);
3909 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3910 "socket: fd=%d", fd
);
3912 net_socket_connect(s
);
3914 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
3919 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
3922 int so_type
=-1, optlen
=sizeof(so_type
);
3924 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
3925 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
3930 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
3932 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3934 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
3935 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
3936 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3941 static void net_socket_accept(void *opaque
)
3943 NetSocketListenState
*s
= opaque
;
3945 struct sockaddr_in saddr
;
3950 len
= sizeof(saddr
);
3951 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
3952 if (fd
< 0 && errno
!= EINTR
) {
3954 } else if (fd
>= 0) {
3958 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
3962 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
3963 "socket: connection from %s:%d",
3964 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3968 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
3970 NetSocketListenState
*s
;
3972 struct sockaddr_in saddr
;
3974 if (parse_host_port(&saddr
, host_str
) < 0)
3977 s
= qemu_mallocz(sizeof(NetSocketListenState
));
3981 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3986 socket_set_nonblock(fd
);
3988 /* allow fast reuse */
3990 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3992 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3997 ret
= listen(fd
, 0);
4004 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
4008 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
4011 int fd
, connected
, ret
, err
;
4012 struct sockaddr_in saddr
;
4014 if (parse_host_port(&saddr
, host_str
) < 0)
4017 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4022 socket_set_nonblock(fd
);
4026 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4028 err
= socket_error();
4029 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
4030 } else if (err
== EINPROGRESS
) {
4033 } else if (err
== WSAEALREADY
) {
4046 s
= net_socket_fd_init(vlan
, fd
, connected
);
4049 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4050 "socket: connect to %s:%d",
4051 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4055 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
4059 struct sockaddr_in saddr
;
4061 if (parse_host_port(&saddr
, host_str
) < 0)
4065 fd
= net_socket_mcast_create(&saddr
);
4069 s
= net_socket_fd_init(vlan
, fd
, 0);
4073 s
->dgram_dst
= saddr
;
4075 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4076 "socket: mcast=%s:%d",
4077 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4082 static int get_param_value(char *buf
, int buf_size
,
4083 const char *tag
, const char *str
)
4092 while (*p
!= '\0' && *p
!= '=') {
4093 if ((q
- option
) < sizeof(option
) - 1)
4101 if (!strcmp(tag
, option
)) {
4103 while (*p
!= '\0' && *p
!= ',') {
4104 if ((q
- buf
) < buf_size
- 1)
4111 while (*p
!= '\0' && *p
!= ',') {
4122 static int net_client_init(const char *str
)
4133 while (*p
!= '\0' && *p
!= ',') {
4134 if ((q
- device
) < sizeof(device
) - 1)
4142 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
4143 vlan_id
= strtol(buf
, NULL
, 0);
4145 vlan
= qemu_find_vlan(vlan_id
);
4147 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
4150 if (!strcmp(device
, "nic")) {
4154 if (nb_nics
>= MAX_NICS
) {
4155 fprintf(stderr
, "Too Many NICs\n");
4158 nd
= &nd_table
[nb_nics
];
4159 macaddr
= nd
->macaddr
;
4165 macaddr
[5] = 0x56 + nb_nics
;
4167 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
4168 if (parse_macaddr(macaddr
, buf
) < 0) {
4169 fprintf(stderr
, "invalid syntax for ethernet address\n");
4173 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
4174 nd
->model
= strdup(buf
);
4180 if (!strcmp(device
, "none")) {
4181 /* does nothing. It is needed to signal that no network cards
4186 if (!strcmp(device
, "user")) {
4187 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
4188 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
4190 ret
= net_slirp_init(vlan
);
4194 if (!strcmp(device
, "tap")) {
4196 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4197 fprintf(stderr
, "tap: no interface name\n");
4200 ret
= tap_win32_init(vlan
, ifname
);
4203 if (!strcmp(device
, "tap")) {
4205 char setup_script
[1024];
4207 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4208 fd
= strtol(buf
, NULL
, 0);
4210 if (net_tap_fd_init(vlan
, fd
))
4213 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4216 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
4217 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
4219 ret
= net_tap_init(vlan
, ifname
, setup_script
);
4223 if (!strcmp(device
, "socket")) {
4224 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4226 fd
= strtol(buf
, NULL
, 0);
4228 if (net_socket_fd_init(vlan
, fd
, 1))
4230 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
4231 ret
= net_socket_listen_init(vlan
, buf
);
4232 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
4233 ret
= net_socket_connect_init(vlan
, buf
);
4234 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
4235 ret
= net_socket_mcast_init(vlan
, buf
);
4237 fprintf(stderr
, "Unknown socket options: %s\n", p
);
4242 fprintf(stderr
, "Unknown network device: %s\n", device
);
4246 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
4252 void do_info_network(void)
4255 VLANClientState
*vc
;
4257 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
4258 term_printf("VLAN %d devices:\n", vlan
->id
);
4259 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
4260 term_printf(" %s\n", vc
->info_str
);
4264 /***********************************************************/
4267 static USBPort
*used_usb_ports
;
4268 static USBPort
*free_usb_ports
;
4270 /* ??? Maybe change this to register a hub to keep track of the topology. */
4271 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
4272 usb_attachfn attach
)
4274 port
->opaque
= opaque
;
4275 port
->index
= index
;
4276 port
->attach
= attach
;
4277 port
->next
= free_usb_ports
;
4278 free_usb_ports
= port
;
4281 static int usb_device_add(const char *devname
)
4287 if (!free_usb_ports
)
4290 if (strstart(devname
, "host:", &p
)) {
4291 dev
= usb_host_device_open(p
);
4292 } else if (!strcmp(devname
, "mouse")) {
4293 dev
= usb_mouse_init();
4294 } else if (!strcmp(devname
, "tablet")) {
4295 dev
= usb_tablet_init();
4296 } else if (strstart(devname
, "disk:", &p
)) {
4297 dev
= usb_msd_init(p
);
4304 /* Find a USB port to add the device to. */
4305 port
= free_usb_ports
;
4309 /* Create a new hub and chain it on. */
4310 free_usb_ports
= NULL
;
4311 port
->next
= used_usb_ports
;
4312 used_usb_ports
= port
;
4314 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
4315 usb_attach(port
, hub
);
4316 port
= free_usb_ports
;
4319 free_usb_ports
= port
->next
;
4320 port
->next
= used_usb_ports
;
4321 used_usb_ports
= port
;
4322 usb_attach(port
, dev
);
4326 static int usb_device_del(const char *devname
)
4334 if (!used_usb_ports
)
4337 p
= strchr(devname
, '.');
4340 bus_num
= strtoul(devname
, NULL
, 0);
4341 addr
= strtoul(p
+ 1, NULL
, 0);
4345 lastp
= &used_usb_ports
;
4346 port
= used_usb_ports
;
4347 while (port
&& port
->dev
->addr
!= addr
) {
4348 lastp
= &port
->next
;
4356 *lastp
= port
->next
;
4357 usb_attach(port
, NULL
);
4358 dev
->handle_destroy(dev
);
4359 port
->next
= free_usb_ports
;
4360 free_usb_ports
= port
;
4364 void do_usb_add(const char *devname
)
4367 ret
= usb_device_add(devname
);
4369 term_printf("Could not add USB device '%s'\n", devname
);
4372 void do_usb_del(const char *devname
)
4375 ret
= usb_device_del(devname
);
4377 term_printf("Could not remove USB device '%s'\n", devname
);
4384 const char *speed_str
;
4387 term_printf("USB support not enabled\n");
4391 for (port
= used_usb_ports
; port
; port
= port
->next
) {
4395 switch(dev
->speed
) {
4399 case USB_SPEED_FULL
:
4402 case USB_SPEED_HIGH
:
4409 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
4410 0, dev
->addr
, speed_str
, dev
->devname
);
4414 /***********************************************************/
4415 /* PCMCIA/Cardbus */
4417 static struct pcmcia_socket_entry_s
{
4418 struct pcmcia_socket_s
*socket
;
4419 struct pcmcia_socket_entry_s
*next
;
4420 } *pcmcia_sockets
= 0;
4422 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
4424 struct pcmcia_socket_entry_s
*entry
;
4426 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
4427 entry
->socket
= socket
;
4428 entry
->next
= pcmcia_sockets
;
4429 pcmcia_sockets
= entry
;
4432 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
4434 struct pcmcia_socket_entry_s
*entry
, **ptr
;
4436 ptr
= &pcmcia_sockets
;
4437 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
4438 if (entry
->socket
== socket
) {
4444 void pcmcia_info(void)
4446 struct pcmcia_socket_entry_s
*iter
;
4447 if (!pcmcia_sockets
)
4448 term_printf("No PCMCIA sockets\n");
4450 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
4451 term_printf("%s: %s\n", iter
->socket
->slot_string
,
4452 iter
->socket
->attached
? iter
->socket
->card_string
:
4456 /***********************************************************/
4459 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4463 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4467 static void dumb_refresh(DisplayState
*ds
)
4472 void dumb_display_init(DisplayState
*ds
)
4477 ds
->dpy_update
= dumb_update
;
4478 ds
->dpy_resize
= dumb_resize
;
4479 ds
->dpy_refresh
= dumb_refresh
;
4482 /***********************************************************/
4485 #define MAX_IO_HANDLERS 64
4487 typedef struct IOHandlerRecord
{
4489 IOCanRWHandler
*fd_read_poll
;
4491 IOHandler
*fd_write
;
4494 /* temporary data */
4496 struct IOHandlerRecord
*next
;
4499 static IOHandlerRecord
*first_io_handler
;
4501 /* XXX: fd_read_poll should be suppressed, but an API change is
4502 necessary in the character devices to suppress fd_can_read(). */
4503 int qemu_set_fd_handler2(int fd
,
4504 IOCanRWHandler
*fd_read_poll
,
4506 IOHandler
*fd_write
,
4509 IOHandlerRecord
**pioh
, *ioh
;
4511 if (!fd_read
&& !fd_write
) {
4512 pioh
= &first_io_handler
;
4517 if (ioh
->fd
== fd
) {
4524 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4528 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4531 ioh
->next
= first_io_handler
;
4532 first_io_handler
= ioh
;
4535 ioh
->fd_read_poll
= fd_read_poll
;
4536 ioh
->fd_read
= fd_read
;
4537 ioh
->fd_write
= fd_write
;
4538 ioh
->opaque
= opaque
;
4544 int qemu_set_fd_handler(int fd
,
4546 IOHandler
*fd_write
,
4549 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4552 /***********************************************************/
4553 /* Polling handling */
4555 typedef struct PollingEntry
{
4558 struct PollingEntry
*next
;
4561 static PollingEntry
*first_polling_entry
;
4563 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4565 PollingEntry
**ppe
, *pe
;
4566 pe
= qemu_mallocz(sizeof(PollingEntry
));
4570 pe
->opaque
= opaque
;
4571 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
4576 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
4578 PollingEntry
**ppe
, *pe
;
4579 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
4581 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
4590 /***********************************************************/
4591 /* Wait objects support */
4592 typedef struct WaitObjects
{
4594 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
4595 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
4596 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
4599 static WaitObjects wait_objects
= {0};
4601 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4603 WaitObjects
*w
= &wait_objects
;
4605 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
4607 w
->events
[w
->num
] = handle
;
4608 w
->func
[w
->num
] = func
;
4609 w
->opaque
[w
->num
] = opaque
;
4614 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4617 WaitObjects
*w
= &wait_objects
;
4620 for (i
= 0; i
< w
->num
; i
++) {
4621 if (w
->events
[i
] == handle
)
4624 w
->events
[i
] = w
->events
[i
+ 1];
4625 w
->func
[i
] = w
->func
[i
+ 1];
4626 w
->opaque
[i
] = w
->opaque
[i
+ 1];
4634 /***********************************************************/
4635 /* savevm/loadvm support */
4637 #define IO_BUF_SIZE 32768
4641 BlockDriverState
*bs
;
4644 int64_t base_offset
;
4645 int64_t buf_offset
; /* start of buffer when writing, end of buffer
4648 int buf_size
; /* 0 when writing */
4649 uint8_t buf
[IO_BUF_SIZE
];
4652 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
4656 f
= qemu_mallocz(sizeof(QEMUFile
));
4659 if (!strcmp(mode
, "wb")) {
4661 } else if (!strcmp(mode
, "rb")) {
4666 f
->outfile
= fopen(filename
, mode
);
4678 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
4682 f
= qemu_mallocz(sizeof(QEMUFile
));
4687 f
->is_writable
= is_writable
;
4688 f
->base_offset
= offset
;
4692 void qemu_fflush(QEMUFile
*f
)
4694 if (!f
->is_writable
)
4696 if (f
->buf_index
> 0) {
4698 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4699 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
4701 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4702 f
->buf
, f
->buf_index
);
4704 f
->buf_offset
+= f
->buf_index
;
4709 static void qemu_fill_buffer(QEMUFile
*f
)
4716 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4717 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
4721 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4722 f
->buf
, IO_BUF_SIZE
);
4728 f
->buf_offset
+= len
;
4731 void qemu_fclose(QEMUFile
*f
)
4741 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
4745 l
= IO_BUF_SIZE
- f
->buf_index
;
4748 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
4752 if (f
->buf_index
>= IO_BUF_SIZE
)
4757 void qemu_put_byte(QEMUFile
*f
, int v
)
4759 f
->buf
[f
->buf_index
++] = v
;
4760 if (f
->buf_index
>= IO_BUF_SIZE
)
4764 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
4770 l
= f
->buf_size
- f
->buf_index
;
4772 qemu_fill_buffer(f
);
4773 l
= f
->buf_size
- f
->buf_index
;
4779 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
4784 return size1
- size
;
4787 int qemu_get_byte(QEMUFile
*f
)
4789 if (f
->buf_index
>= f
->buf_size
) {
4790 qemu_fill_buffer(f
);
4791 if (f
->buf_index
>= f
->buf_size
)
4794 return f
->buf
[f
->buf_index
++];
4797 int64_t qemu_ftell(QEMUFile
*f
)
4799 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
4802 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
4804 if (whence
== SEEK_SET
) {
4806 } else if (whence
== SEEK_CUR
) {
4807 pos
+= qemu_ftell(f
);
4809 /* SEEK_END not supported */
4812 if (f
->is_writable
) {
4814 f
->buf_offset
= pos
;
4816 f
->buf_offset
= pos
;
4823 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
4825 qemu_put_byte(f
, v
>> 8);
4826 qemu_put_byte(f
, v
);
4829 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
4831 qemu_put_byte(f
, v
>> 24);
4832 qemu_put_byte(f
, v
>> 16);
4833 qemu_put_byte(f
, v
>> 8);
4834 qemu_put_byte(f
, v
);
4837 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
4839 qemu_put_be32(f
, v
>> 32);
4840 qemu_put_be32(f
, v
);
4843 unsigned int qemu_get_be16(QEMUFile
*f
)
4846 v
= qemu_get_byte(f
) << 8;
4847 v
|= qemu_get_byte(f
);
4851 unsigned int qemu_get_be32(QEMUFile
*f
)
4854 v
= qemu_get_byte(f
) << 24;
4855 v
|= qemu_get_byte(f
) << 16;
4856 v
|= qemu_get_byte(f
) << 8;
4857 v
|= qemu_get_byte(f
);
4861 uint64_t qemu_get_be64(QEMUFile
*f
)
4864 v
= (uint64_t)qemu_get_be32(f
) << 32;
4865 v
|= qemu_get_be32(f
);
4869 typedef struct SaveStateEntry
{
4873 SaveStateHandler
*save_state
;
4874 LoadStateHandler
*load_state
;
4876 struct SaveStateEntry
*next
;
4879 static SaveStateEntry
*first_se
;
4881 int register_savevm(const char *idstr
,
4884 SaveStateHandler
*save_state
,
4885 LoadStateHandler
*load_state
,
4888 SaveStateEntry
*se
, **pse
;
4890 se
= qemu_malloc(sizeof(SaveStateEntry
));
4893 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
4894 se
->instance_id
= instance_id
;
4895 se
->version_id
= version_id
;
4896 se
->save_state
= save_state
;
4897 se
->load_state
= load_state
;
4898 se
->opaque
= opaque
;
4901 /* add at the end of list */
4903 while (*pse
!= NULL
)
4904 pse
= &(*pse
)->next
;
4909 #define QEMU_VM_FILE_MAGIC 0x5145564d
4910 #define QEMU_VM_FILE_VERSION 0x00000002
4912 int qemu_savevm_state(QEMUFile
*f
)
4916 int64_t cur_pos
, len_pos
, total_len_pos
;
4918 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
4919 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
4920 total_len_pos
= qemu_ftell(f
);
4921 qemu_put_be64(f
, 0); /* total size */
4923 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4925 len
= strlen(se
->idstr
);
4926 qemu_put_byte(f
, len
);
4927 qemu_put_buffer(f
, se
->idstr
, len
);
4929 qemu_put_be32(f
, se
->instance_id
);
4930 qemu_put_be32(f
, se
->version_id
);
4932 /* record size: filled later */
4933 len_pos
= qemu_ftell(f
);
4934 qemu_put_be32(f
, 0);
4936 se
->save_state(f
, se
->opaque
);
4938 /* fill record size */
4939 cur_pos
= qemu_ftell(f
);
4940 len
= cur_pos
- len_pos
- 4;
4941 qemu_fseek(f
, len_pos
, SEEK_SET
);
4942 qemu_put_be32(f
, len
);
4943 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4945 cur_pos
= qemu_ftell(f
);
4946 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
4947 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
4948 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4954 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
4958 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4959 if (!strcmp(se
->idstr
, idstr
) &&
4960 instance_id
== se
->instance_id
)
4966 int qemu_loadvm_state(QEMUFile
*f
)
4969 int len
, ret
, instance_id
, record_len
, version_id
;
4970 int64_t total_len
, end_pos
, cur_pos
;
4974 v
= qemu_get_be32(f
);
4975 if (v
!= QEMU_VM_FILE_MAGIC
)
4977 v
= qemu_get_be32(f
);
4978 if (v
!= QEMU_VM_FILE_VERSION
) {
4983 total_len
= qemu_get_be64(f
);
4984 end_pos
= total_len
+ qemu_ftell(f
);
4986 if (qemu_ftell(f
) >= end_pos
)
4988 len
= qemu_get_byte(f
);
4989 qemu_get_buffer(f
, idstr
, len
);
4991 instance_id
= qemu_get_be32(f
);
4992 version_id
= qemu_get_be32(f
);
4993 record_len
= qemu_get_be32(f
);
4995 printf("idstr=%s instance=0x%x version=%d len=%d\n",
4996 idstr
, instance_id
, version_id
, record_len
);
4998 cur_pos
= qemu_ftell(f
);
4999 se
= find_se(idstr
, instance_id
);
5001 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
5002 instance_id
, idstr
);
5004 ret
= se
->load_state(f
, se
->opaque
, version_id
);
5006 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
5007 instance_id
, idstr
);
5010 /* always seek to exact end of record */
5011 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
5018 /* device can contain snapshots */
5019 static int bdrv_can_snapshot(BlockDriverState
*bs
)
5022 !bdrv_is_removable(bs
) &&
5023 !bdrv_is_read_only(bs
));
5026 /* device must be snapshots in order to have a reliable snapshot */
5027 static int bdrv_has_snapshot(BlockDriverState
*bs
)
5030 !bdrv_is_removable(bs
) &&
5031 !bdrv_is_read_only(bs
));
5034 static BlockDriverState
*get_bs_snapshots(void)
5036 BlockDriverState
*bs
;
5040 return bs_snapshots
;
5041 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5043 if (bdrv_can_snapshot(bs
))
5052 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
5055 QEMUSnapshotInfo
*sn_tab
, *sn
;
5059 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5062 for(i
= 0; i
< nb_sns
; i
++) {
5064 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
5074 void do_savevm(const char *name
)
5076 BlockDriverState
*bs
, *bs1
;
5077 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
5078 int must_delete
, ret
, i
;
5079 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5081 int saved_vm_running
;
5088 bs
= get_bs_snapshots();
5090 term_printf("No block device can accept snapshots\n");
5094 /* ??? Should this occur after vm_stop? */
5097 saved_vm_running
= vm_running
;
5102 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
5107 memset(sn
, 0, sizeof(*sn
));
5109 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
5110 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
5113 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
5116 /* fill auxiliary fields */
5119 sn
->date_sec
= tb
.time
;
5120 sn
->date_nsec
= tb
.millitm
* 1000000;
5122 gettimeofday(&tv
, NULL
);
5123 sn
->date_sec
= tv
.tv_sec
;
5124 sn
->date_nsec
= tv
.tv_usec
* 1000;
5126 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
5128 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5129 term_printf("Device %s does not support VM state snapshots\n",
5130 bdrv_get_device_name(bs
));
5134 /* save the VM state */
5135 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
5137 term_printf("Could not open VM state file\n");
5140 ret
= qemu_savevm_state(f
);
5141 sn
->vm_state_size
= qemu_ftell(f
);
5144 term_printf("Error %d while writing VM\n", ret
);
5148 /* create the snapshots */
5150 for(i
= 0; i
< MAX_DISKS
; i
++) {
5152 if (bdrv_has_snapshot(bs1
)) {
5154 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
5156 term_printf("Error while deleting snapshot on '%s'\n",
5157 bdrv_get_device_name(bs1
));
5160 ret
= bdrv_snapshot_create(bs1
, sn
);
5162 term_printf("Error while creating snapshot on '%s'\n",
5163 bdrv_get_device_name(bs1
));
5169 if (saved_vm_running
)
5173 void do_loadvm(const char *name
)
5175 BlockDriverState
*bs
, *bs1
;
5176 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5179 int saved_vm_running
;
5181 bs
= get_bs_snapshots();
5183 term_printf("No block device supports snapshots\n");
5187 /* Flush all IO requests so they don't interfere with the new state. */
5190 saved_vm_running
= vm_running
;
5193 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5195 if (bdrv_has_snapshot(bs1
)) {
5196 ret
= bdrv_snapshot_goto(bs1
, name
);
5199 term_printf("Warning: ");
5202 term_printf("Snapshots not supported on device '%s'\n",
5203 bdrv_get_device_name(bs1
));
5206 term_printf("Could not find snapshot '%s' on device '%s'\n",
5207 name
, bdrv_get_device_name(bs1
));
5210 term_printf("Error %d while activating snapshot on '%s'\n",
5211 ret
, bdrv_get_device_name(bs1
));
5214 /* fatal on snapshot block device */
5221 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5222 term_printf("Device %s does not support VM state snapshots\n",
5223 bdrv_get_device_name(bs
));
5227 /* restore the VM state */
5228 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
5230 term_printf("Could not open VM state file\n");
5233 ret
= qemu_loadvm_state(f
);
5236 term_printf("Error %d while loading VM state\n", ret
);
5239 if (saved_vm_running
)
5243 void do_delvm(const char *name
)
5245 BlockDriverState
*bs
, *bs1
;
5248 bs
= get_bs_snapshots();
5250 term_printf("No block device supports snapshots\n");
5254 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5256 if (bdrv_has_snapshot(bs1
)) {
5257 ret
= bdrv_snapshot_delete(bs1
, name
);
5259 if (ret
== -ENOTSUP
)
5260 term_printf("Snapshots not supported on device '%s'\n",
5261 bdrv_get_device_name(bs1
));
5263 term_printf("Error %d while deleting snapshot on '%s'\n",
5264 ret
, bdrv_get_device_name(bs1
));
5270 void do_info_snapshots(void)
5272 BlockDriverState
*bs
, *bs1
;
5273 QEMUSnapshotInfo
*sn_tab
, *sn
;
5277 bs
= get_bs_snapshots();
5279 term_printf("No available block device supports snapshots\n");
5282 term_printf("Snapshot devices:");
5283 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5285 if (bdrv_has_snapshot(bs1
)) {
5287 term_printf(" %s", bdrv_get_device_name(bs1
));
5292 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5294 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
5297 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
5298 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
5299 for(i
= 0; i
< nb_sns
; i
++) {
5301 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
5306 /***********************************************************/
5307 /* cpu save/restore */
5309 #if defined(TARGET_I386)
5311 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
5313 qemu_put_be32(f
, dt
->selector
);
5314 qemu_put_betl(f
, dt
->base
);
5315 qemu_put_be32(f
, dt
->limit
);
5316 qemu_put_be32(f
, dt
->flags
);
5319 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
5321 dt
->selector
= qemu_get_be32(f
);
5322 dt
->base
= qemu_get_betl(f
);
5323 dt
->limit
= qemu_get_be32(f
);
5324 dt
->flags
= qemu_get_be32(f
);
5327 void cpu_save(QEMUFile
*f
, void *opaque
)
5329 CPUState
*env
= opaque
;
5330 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
5334 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5335 qemu_put_betls(f
, &env
->regs
[i
]);
5336 qemu_put_betls(f
, &env
->eip
);
5337 qemu_put_betls(f
, &env
->eflags
);
5338 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
5339 qemu_put_be32s(f
, &hflags
);
5343 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
5345 for(i
= 0; i
< 8; i
++) {
5346 fptag
|= ((!env
->fptags
[i
]) << i
);
5349 qemu_put_be16s(f
, &fpuc
);
5350 qemu_put_be16s(f
, &fpus
);
5351 qemu_put_be16s(f
, &fptag
);
5353 #ifdef USE_X86LDOUBLE
5358 qemu_put_be16s(f
, &fpregs_format
);
5360 for(i
= 0; i
< 8; i
++) {
5361 #ifdef USE_X86LDOUBLE
5365 /* we save the real CPU data (in case of MMX usage only 'mant'
5366 contains the MMX register */
5367 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
5368 qemu_put_be64(f
, mant
);
5369 qemu_put_be16(f
, exp
);
5372 /* if we use doubles for float emulation, we save the doubles to
5373 avoid losing information in case of MMX usage. It can give
5374 problems if the image is restored on a CPU where long
5375 doubles are used instead. */
5376 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
5380 for(i
= 0; i
< 6; i
++)
5381 cpu_put_seg(f
, &env
->segs
[i
]);
5382 cpu_put_seg(f
, &env
->ldt
);
5383 cpu_put_seg(f
, &env
->tr
);
5384 cpu_put_seg(f
, &env
->gdt
);
5385 cpu_put_seg(f
, &env
->idt
);
5387 qemu_put_be32s(f
, &env
->sysenter_cs
);
5388 qemu_put_be32s(f
, &env
->sysenter_esp
);
5389 qemu_put_be32s(f
, &env
->sysenter_eip
);
5391 qemu_put_betls(f
, &env
->cr
[0]);
5392 qemu_put_betls(f
, &env
->cr
[2]);
5393 qemu_put_betls(f
, &env
->cr
[3]);
5394 qemu_put_betls(f
, &env
->cr
[4]);
5396 for(i
= 0; i
< 8; i
++)
5397 qemu_put_betls(f
, &env
->dr
[i
]);
5400 qemu_put_be32s(f
, &env
->a20_mask
);
5403 qemu_put_be32s(f
, &env
->mxcsr
);
5404 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5405 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5406 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5409 #ifdef TARGET_X86_64
5410 qemu_put_be64s(f
, &env
->efer
);
5411 qemu_put_be64s(f
, &env
->star
);
5412 qemu_put_be64s(f
, &env
->lstar
);
5413 qemu_put_be64s(f
, &env
->cstar
);
5414 qemu_put_be64s(f
, &env
->fmask
);
5415 qemu_put_be64s(f
, &env
->kernelgsbase
);
5417 qemu_put_be32s(f
, &env
->smbase
);
5420 #ifdef USE_X86LDOUBLE
5421 /* XXX: add that in a FPU generic layer */
5422 union x86_longdouble
{
5427 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
5428 #define EXPBIAS1 1023
5429 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
5430 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
5432 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
5436 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
5437 /* exponent + sign */
5438 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
5439 e
|= SIGND1(temp
) >> 16;
5444 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5446 CPUState
*env
= opaque
;
5449 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
5451 if (version_id
!= 3 && version_id
!= 4)
5453 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5454 qemu_get_betls(f
, &env
->regs
[i
]);
5455 qemu_get_betls(f
, &env
->eip
);
5456 qemu_get_betls(f
, &env
->eflags
);
5457 qemu_get_be32s(f
, &hflags
);
5459 qemu_get_be16s(f
, &fpuc
);
5460 qemu_get_be16s(f
, &fpus
);
5461 qemu_get_be16s(f
, &fptag
);
5462 qemu_get_be16s(f
, &fpregs_format
);
5464 /* NOTE: we cannot always restore the FPU state if the image come
5465 from a host with a different 'USE_X86LDOUBLE' define. We guess
5466 if we are in an MMX state to restore correctly in that case. */
5467 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
5468 for(i
= 0; i
< 8; i
++) {
5472 switch(fpregs_format
) {
5474 mant
= qemu_get_be64(f
);
5475 exp
= qemu_get_be16(f
);
5476 #ifdef USE_X86LDOUBLE
5477 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5479 /* difficult case */
5481 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5483 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5487 mant
= qemu_get_be64(f
);
5488 #ifdef USE_X86LDOUBLE
5490 union x86_longdouble
*p
;
5491 /* difficult case */
5492 p
= (void *)&env
->fpregs
[i
];
5497 fp64_to_fp80(p
, mant
);
5501 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5510 /* XXX: restore FPU round state */
5511 env
->fpstt
= (fpus
>> 11) & 7;
5512 env
->fpus
= fpus
& ~0x3800;
5514 for(i
= 0; i
< 8; i
++) {
5515 env
->fptags
[i
] = (fptag
>> i
) & 1;
5518 for(i
= 0; i
< 6; i
++)
5519 cpu_get_seg(f
, &env
->segs
[i
]);
5520 cpu_get_seg(f
, &env
->ldt
);
5521 cpu_get_seg(f
, &env
->tr
);
5522 cpu_get_seg(f
, &env
->gdt
);
5523 cpu_get_seg(f
, &env
->idt
);
5525 qemu_get_be32s(f
, &env
->sysenter_cs
);
5526 qemu_get_be32s(f
, &env
->sysenter_esp
);
5527 qemu_get_be32s(f
, &env
->sysenter_eip
);
5529 qemu_get_betls(f
, &env
->cr
[0]);
5530 qemu_get_betls(f
, &env
->cr
[2]);
5531 qemu_get_betls(f
, &env
->cr
[3]);
5532 qemu_get_betls(f
, &env
->cr
[4]);
5534 for(i
= 0; i
< 8; i
++)
5535 qemu_get_betls(f
, &env
->dr
[i
]);
5538 qemu_get_be32s(f
, &env
->a20_mask
);
5540 qemu_get_be32s(f
, &env
->mxcsr
);
5541 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5542 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5543 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5546 #ifdef TARGET_X86_64
5547 qemu_get_be64s(f
, &env
->efer
);
5548 qemu_get_be64s(f
, &env
->star
);
5549 qemu_get_be64s(f
, &env
->lstar
);
5550 qemu_get_be64s(f
, &env
->cstar
);
5551 qemu_get_be64s(f
, &env
->fmask
);
5552 qemu_get_be64s(f
, &env
->kernelgsbase
);
5554 if (version_id
>= 4)
5555 qemu_get_be32s(f
, &env
->smbase
);
5557 /* XXX: compute hflags from scratch, except for CPL and IIF */
5558 env
->hflags
= hflags
;
5563 #elif defined(TARGET_PPC)
5564 void cpu_save(QEMUFile
*f
, void *opaque
)
5568 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5573 #elif defined(TARGET_MIPS)
5574 void cpu_save(QEMUFile
*f
, void *opaque
)
5578 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5583 #elif defined(TARGET_SPARC)
5584 void cpu_save(QEMUFile
*f
, void *opaque
)
5586 CPUState
*env
= opaque
;
5590 for(i
= 0; i
< 8; i
++)
5591 qemu_put_betls(f
, &env
->gregs
[i
]);
5592 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5593 qemu_put_betls(f
, &env
->regbase
[i
]);
5596 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5602 qemu_put_be32(f
, u
.i
);
5605 qemu_put_betls(f
, &env
->pc
);
5606 qemu_put_betls(f
, &env
->npc
);
5607 qemu_put_betls(f
, &env
->y
);
5609 qemu_put_be32(f
, tmp
);
5610 qemu_put_betls(f
, &env
->fsr
);
5611 qemu_put_betls(f
, &env
->tbr
);
5612 #ifndef TARGET_SPARC64
5613 qemu_put_be32s(f
, &env
->wim
);
5615 for(i
= 0; i
< 16; i
++)
5616 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
5620 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5622 CPUState
*env
= opaque
;
5626 for(i
= 0; i
< 8; i
++)
5627 qemu_get_betls(f
, &env
->gregs
[i
]);
5628 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5629 qemu_get_betls(f
, &env
->regbase
[i
]);
5632 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5637 u
.i
= qemu_get_be32(f
);
5641 qemu_get_betls(f
, &env
->pc
);
5642 qemu_get_betls(f
, &env
->npc
);
5643 qemu_get_betls(f
, &env
->y
);
5644 tmp
= qemu_get_be32(f
);
5645 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
5646 correctly updated */
5648 qemu_get_betls(f
, &env
->fsr
);
5649 qemu_get_betls(f
, &env
->tbr
);
5650 #ifndef TARGET_SPARC64
5651 qemu_get_be32s(f
, &env
->wim
);
5653 for(i
= 0; i
< 16; i
++)
5654 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
5660 #elif defined(TARGET_ARM)
5662 /* ??? Need to implement these. */
5663 void cpu_save(QEMUFile
*f
, void *opaque
)
5667 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5674 #warning No CPU save/restore functions
5678 /***********************************************************/
5679 /* ram save/restore */
5681 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
5685 v
= qemu_get_byte(f
);
5688 if (qemu_get_buffer(f
, buf
, len
) != len
)
5692 v
= qemu_get_byte(f
);
5693 memset(buf
, v
, len
);
5701 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
5705 if (qemu_get_be32(f
) != phys_ram_size
)
5707 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
5708 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
5715 #define BDRV_HASH_BLOCK_SIZE 1024
5716 #define IOBUF_SIZE 4096
5717 #define RAM_CBLOCK_MAGIC 0xfabe
5719 typedef struct RamCompressState
{
5722 uint8_t buf
[IOBUF_SIZE
];
5725 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
5728 memset(s
, 0, sizeof(*s
));
5730 ret
= deflateInit2(&s
->zstream
, 1,
5732 9, Z_DEFAULT_STRATEGY
);
5735 s
->zstream
.avail_out
= IOBUF_SIZE
;
5736 s
->zstream
.next_out
= s
->buf
;
5740 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
5742 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
5743 qemu_put_be16(s
->f
, len
);
5744 qemu_put_buffer(s
->f
, buf
, len
);
5747 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
5751 s
->zstream
.avail_in
= len
;
5752 s
->zstream
.next_in
= (uint8_t *)buf
;
5753 while (s
->zstream
.avail_in
> 0) {
5754 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
5757 if (s
->zstream
.avail_out
== 0) {
5758 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
5759 s
->zstream
.avail_out
= IOBUF_SIZE
;
5760 s
->zstream
.next_out
= s
->buf
;
5766 static void ram_compress_close(RamCompressState
*s
)
5770 /* compress last bytes */
5772 ret
= deflate(&s
->zstream
, Z_FINISH
);
5773 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
5774 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
5776 ram_put_cblock(s
, s
->buf
, len
);
5778 s
->zstream
.avail_out
= IOBUF_SIZE
;
5779 s
->zstream
.next_out
= s
->buf
;
5780 if (ret
== Z_STREAM_END
)
5787 deflateEnd(&s
->zstream
);
5790 typedef struct RamDecompressState
{
5793 uint8_t buf
[IOBUF_SIZE
];
5794 } RamDecompressState
;
5796 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
5799 memset(s
, 0, sizeof(*s
));
5801 ret
= inflateInit(&s
->zstream
);
5807 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
5811 s
->zstream
.avail_out
= len
;
5812 s
->zstream
.next_out
= buf
;
5813 while (s
->zstream
.avail_out
> 0) {
5814 if (s
->zstream
.avail_in
== 0) {
5815 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
5817 clen
= qemu_get_be16(s
->f
);
5818 if (clen
> IOBUF_SIZE
)
5820 qemu_get_buffer(s
->f
, s
->buf
, clen
);
5821 s
->zstream
.avail_in
= clen
;
5822 s
->zstream
.next_in
= s
->buf
;
5824 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
5825 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
5832 static void ram_decompress_close(RamDecompressState
*s
)
5834 inflateEnd(&s
->zstream
);
5837 static void ram_save(QEMUFile
*f
, void *opaque
)
5840 RamCompressState s1
, *s
= &s1
;
5843 qemu_put_be32(f
, phys_ram_size
);
5844 if (ram_compress_open(s
, f
) < 0)
5846 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5848 if (tight_savevm_enabled
) {
5852 /* find if the memory block is available on a virtual
5855 for(j
= 0; j
< MAX_DISKS
; j
++) {
5857 sector_num
= bdrv_hash_find(bs_table
[j
],
5858 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5859 if (sector_num
>= 0)
5864 goto normal_compress
;
5867 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
5868 ram_compress_buf(s
, buf
, 10);
5874 ram_compress_buf(s
, buf
, 1);
5875 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5878 ram_compress_close(s
);
5881 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
5883 RamDecompressState s1
, *s
= &s1
;
5887 if (version_id
== 1)
5888 return ram_load_v1(f
, opaque
);
5889 if (version_id
!= 2)
5891 if (qemu_get_be32(f
) != phys_ram_size
)
5893 if (ram_decompress_open(s
, f
) < 0)
5895 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5896 if (ram_decompress_buf(s
, buf
, 1) < 0) {
5897 fprintf(stderr
, "Error while reading ram block header\n");
5901 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
5902 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
5911 ram_decompress_buf(s
, buf
+ 1, 9);
5913 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
5914 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
5915 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
5918 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
5919 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
5920 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
5921 bs_index
, sector_num
);
5928 printf("Error block header\n");
5932 ram_decompress_close(s
);
5936 /***********************************************************/
5937 /* bottom halves (can be seen as timers which expire ASAP) */
5946 static QEMUBH
*first_bh
= NULL
;
5948 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
5951 bh
= qemu_mallocz(sizeof(QEMUBH
));
5955 bh
->opaque
= opaque
;
5959 int qemu_bh_poll(void)
5978 void qemu_bh_schedule(QEMUBH
*bh
)
5980 CPUState
*env
= cpu_single_env
;
5984 bh
->next
= first_bh
;
5987 /* stop the currently executing CPU to execute the BH ASAP */
5989 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
5993 void qemu_bh_cancel(QEMUBH
*bh
)
5996 if (bh
->scheduled
) {
5999 pbh
= &(*pbh
)->next
;
6005 void qemu_bh_delete(QEMUBH
*bh
)
6011 /***********************************************************/
6012 /* machine registration */
6014 QEMUMachine
*first_machine
= NULL
;
6016 int qemu_register_machine(QEMUMachine
*m
)
6019 pm
= &first_machine
;
6027 QEMUMachine
*find_machine(const char *name
)
6031 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6032 if (!strcmp(m
->name
, name
))
6038 /***********************************************************/
6039 /* main execution loop */
6041 void gui_update(void *opaque
)
6043 display_state
.dpy_refresh(&display_state
);
6044 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
6047 struct vm_change_state_entry
{
6048 VMChangeStateHandler
*cb
;
6050 LIST_ENTRY (vm_change_state_entry
) entries
;
6053 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
6055 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
6058 VMChangeStateEntry
*e
;
6060 e
= qemu_mallocz(sizeof (*e
));
6066 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
6070 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
6072 LIST_REMOVE (e
, entries
);
6076 static void vm_state_notify(int running
)
6078 VMChangeStateEntry
*e
;
6080 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
6081 e
->cb(e
->opaque
, running
);
6085 /* XXX: support several handlers */
6086 static VMStopHandler
*vm_stop_cb
;
6087 static void *vm_stop_opaque
;
6089 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6092 vm_stop_opaque
= opaque
;
6096 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6110 void vm_stop(int reason
)
6113 cpu_disable_ticks();
6117 vm_stop_cb(vm_stop_opaque
, reason
);
6124 /* reset/shutdown handler */
6126 typedef struct QEMUResetEntry
{
6127 QEMUResetHandler
*func
;
6129 struct QEMUResetEntry
*next
;
6132 static QEMUResetEntry
*first_reset_entry
;
6133 static int reset_requested
;
6134 static int shutdown_requested
;
6135 static int powerdown_requested
;
6137 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
6139 QEMUResetEntry
**pre
, *re
;
6141 pre
= &first_reset_entry
;
6142 while (*pre
!= NULL
)
6143 pre
= &(*pre
)->next
;
6144 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
6146 re
->opaque
= opaque
;
6151 static void qemu_system_reset(void)
6155 /* reset all devices */
6156 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
6157 re
->func(re
->opaque
);
6161 void qemu_system_reset_request(void)
6164 shutdown_requested
= 1;
6166 reset_requested
= 1;
6169 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6172 void qemu_system_shutdown_request(void)
6174 shutdown_requested
= 1;
6176 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6179 void qemu_system_powerdown_request(void)
6181 powerdown_requested
= 1;
6183 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6186 void main_loop_wait(int timeout
)
6188 IOHandlerRecord
*ioh
;
6189 fd_set rfds
, wfds
, xfds
;
6198 /* XXX: need to suppress polling by better using win32 events */
6200 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
6201 ret
|= pe
->func(pe
->opaque
);
6206 WaitObjects
*w
= &wait_objects
;
6208 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
6209 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
6210 if (w
->func
[ret
- WAIT_OBJECT_0
])
6211 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
6213 /* Check for additional signaled events */
6214 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
6216 /* Check if event is signaled */
6217 ret2
= WaitForSingleObject(w
->events
[i
], 0);
6218 if(ret2
== WAIT_OBJECT_0
) {
6220 w
->func
[i
](w
->opaque
[i
]);
6221 } else if (ret2
== WAIT_TIMEOUT
) {
6223 err
= GetLastError();
6224 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
6227 } else if (ret
== WAIT_TIMEOUT
) {
6229 err
= GetLastError();
6230 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
6234 /* poll any events */
6235 /* XXX: separate device handlers from system ones */
6240 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6244 (!ioh
->fd_read_poll
||
6245 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
6246 FD_SET(ioh
->fd
, &rfds
);
6250 if (ioh
->fd_write
) {
6251 FD_SET(ioh
->fd
, &wfds
);
6261 tv
.tv_usec
= timeout
* 1000;
6263 #if defined(CONFIG_SLIRP)
6265 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
6268 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
6270 IOHandlerRecord
**pioh
;
6272 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6275 if (FD_ISSET(ioh
->fd
, &rfds
)) {
6276 ioh
->fd_read(ioh
->opaque
);
6278 if (FD_ISSET(ioh
->fd
, &wfds
)) {
6279 ioh
->fd_write(ioh
->opaque
);
6283 /* remove deleted IO handlers */
6284 pioh
= &first_io_handler
;
6294 #if defined(CONFIG_SLIRP)
6301 slirp_select_poll(&rfds
, &wfds
, &xfds
);
6308 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
6309 qemu_get_clock(vm_clock
));
6310 /* run dma transfers, if any */
6314 /* real time timers */
6315 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
6316 qemu_get_clock(rt_clock
));
6319 static CPUState
*cur_cpu
;
6324 #ifdef CONFIG_PROFILER
6329 cur_cpu
= first_cpu
;
6336 env
= env
->next_cpu
;
6339 #ifdef CONFIG_PROFILER
6340 ti
= profile_getclock();
6342 ret
= cpu_exec(env
);
6343 #ifdef CONFIG_PROFILER
6344 qemu_time
+= profile_getclock() - ti
;
6346 if (ret
== EXCP_HLT
) {
6347 /* Give the next CPU a chance to run. */
6351 if (ret
!= EXCP_HALTED
)
6353 /* all CPUs are halted ? */
6359 if (shutdown_requested
) {
6360 ret
= EXCP_INTERRUPT
;
6363 if (reset_requested
) {
6364 reset_requested
= 0;
6365 qemu_system_reset();
6366 ret
= EXCP_INTERRUPT
;
6368 if (powerdown_requested
) {
6369 powerdown_requested
= 0;
6370 qemu_system_powerdown();
6371 ret
= EXCP_INTERRUPT
;
6373 if (ret
== EXCP_DEBUG
) {
6374 vm_stop(EXCP_DEBUG
);
6376 /* If all cpus are halted then wait until the next IRQ */
6377 /* XXX: use timeout computed from timers */
6378 if (ret
== EXCP_HALTED
)
6385 #ifdef CONFIG_PROFILER
6386 ti
= profile_getclock();
6388 main_loop_wait(timeout
);
6389 #ifdef CONFIG_PROFILER
6390 dev_time
+= profile_getclock() - ti
;
6393 cpu_disable_ticks();
6399 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2007 Fabrice Bellard\n"
6400 "usage: %s [options] [disk_image]\n"
6402 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
6404 "Standard options:\n"
6405 "-M machine select emulated machine (-M ? for list)\n"
6406 "-cpu cpu select CPU (-cpu ? for list)\n"
6407 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
6408 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
6409 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
6410 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
6411 "-sd file use 'file' as SecureDigital card image\n"
6412 "-pflash file use 'file' as a parallel flash image\n"
6413 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
6414 "-snapshot write to temporary files instead of disk image files\n"
6416 "-no-frame open SDL window without a frame and window decorations\n"
6417 "-no-quit disable SDL window close capability\n"
6420 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
6422 "-m megs set virtual RAM size to megs MB [default=%d]\n"
6423 "-smp n set the number of CPUs to 'n' [default=1]\n"
6424 "-nographic disable graphical output and redirect serial I/Os to console\n"
6426 "-k language use keyboard layout (for example \"fr\" for French)\n"
6429 "-audio-help print list of audio drivers and their options\n"
6430 "-soundhw c1,... enable audio support\n"
6431 " and only specified sound cards (comma separated list)\n"
6432 " use -soundhw ? to get the list of supported cards\n"
6433 " use -soundhw all to enable all of them\n"
6435 "-localtime set the real time clock to local time [default=utc]\n"
6436 "-full-screen start in full screen\n"
6438 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
6440 "-usb enable the USB driver (will be the default soon)\n"
6441 "-usbdevice name add the host or guest USB device 'name'\n"
6442 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6443 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
6445 "-name string set the name of the guest\n"
6447 "Network options:\n"
6448 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
6449 " create a new Network Interface Card and connect it to VLAN 'n'\n"
6451 "-net user[,vlan=n][,hostname=host]\n"
6452 " connect the user mode network stack to VLAN 'n' and send\n"
6453 " hostname 'host' to DHCP clients\n"
6456 "-net tap[,vlan=n],ifname=name\n"
6457 " connect the host TAP network interface to VLAN 'n'\n"
6459 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
6460 " connect the host TAP network interface to VLAN 'n' and use\n"
6461 " the network script 'file' (default=%s);\n"
6462 " use 'script=no' to disable script execution;\n"
6463 " use 'fd=h' to connect to an already opened TAP interface\n"
6465 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
6466 " connect the vlan 'n' to another VLAN using a socket connection\n"
6467 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
6468 " connect the vlan 'n' to multicast maddr and port\n"
6469 "-net none use it alone to have zero network devices; if no -net option\n"
6470 " is provided, the default is '-net nic -net user'\n"
6473 "-tftp dir allow tftp access to files in dir [-net user]\n"
6474 "-bootp file advertise file in BOOTP replies\n"
6476 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
6478 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
6479 " redirect TCP or UDP connections from host to guest [-net user]\n"
6482 "Linux boot specific:\n"
6483 "-kernel bzImage use 'bzImage' as kernel image\n"
6484 "-append cmdline use 'cmdline' as kernel command line\n"
6485 "-initrd file use 'file' as initial ram disk\n"
6487 "Debug/Expert options:\n"
6488 "-monitor dev redirect the monitor to char device 'dev'\n"
6489 "-serial dev redirect the serial port to char device 'dev'\n"
6490 "-parallel dev redirect the parallel port to char device 'dev'\n"
6491 "-pidfile file Write PID to 'file'\n"
6492 "-S freeze CPU at startup (use 'c' to start execution)\n"
6493 "-s wait gdb connection to port\n"
6494 "-p port set gdb connection port [default=%s]\n"
6495 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
6496 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
6497 " translation (t=none or lba) (usually qemu can guess them)\n"
6498 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
6500 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
6501 "-no-kqemu disable KQEMU kernel module usage\n"
6503 #ifdef USE_CODE_COPY
6504 "-no-code-copy disable code copy acceleration\n"
6507 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
6508 " (default is CL-GD5446 PCI VGA)\n"
6509 "-no-acpi disable ACPI\n"
6511 "-no-reboot exit instead of rebooting\n"
6512 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
6513 "-vnc display start a VNC server on display\n"
6515 "-daemonize daemonize QEMU after initializing\n"
6517 "-option-rom rom load a file, rom, into the option ROM space\n"
6519 "During emulation, the following keys are useful:\n"
6520 "ctrl-alt-f toggle full screen\n"
6521 "ctrl-alt-n switch to virtual console 'n'\n"
6522 "ctrl-alt toggle mouse and keyboard grab\n"
6524 "When using -nographic, press 'ctrl-a h' to get some help.\n"
6529 DEFAULT_NETWORK_SCRIPT
,
6531 DEFAULT_GDBSTUB_PORT
,
6536 #define HAS_ARG 0x0001
6553 QEMU_OPTION_snapshot
,
6555 QEMU_OPTION_no_fd_bootchk
,
6558 QEMU_OPTION_nographic
,
6560 QEMU_OPTION_audio_help
,
6561 QEMU_OPTION_soundhw
,
6580 QEMU_OPTION_no_code_copy
,
6582 QEMU_OPTION_localtime
,
6583 QEMU_OPTION_cirrusvga
,
6586 QEMU_OPTION_std_vga
,
6588 QEMU_OPTION_monitor
,
6590 QEMU_OPTION_parallel
,
6592 QEMU_OPTION_full_screen
,
6593 QEMU_OPTION_no_frame
,
6594 QEMU_OPTION_no_quit
,
6595 QEMU_OPTION_pidfile
,
6596 QEMU_OPTION_no_kqemu
,
6597 QEMU_OPTION_kernel_kqemu
,
6598 QEMU_OPTION_win2k_hack
,
6600 QEMU_OPTION_usbdevice
,
6603 QEMU_OPTION_no_acpi
,
6604 QEMU_OPTION_no_reboot
,
6605 QEMU_OPTION_daemonize
,
6606 QEMU_OPTION_option_rom
,
6607 QEMU_OPTION_semihosting
,
6611 typedef struct QEMUOption
{
6617 const QEMUOption qemu_options
[] = {
6618 { "h", 0, QEMU_OPTION_h
},
6619 { "help", 0, QEMU_OPTION_h
},
6621 { "M", HAS_ARG
, QEMU_OPTION_M
},
6622 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
6623 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
6624 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
6625 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
6626 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
6627 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
6628 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
6629 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
6630 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
6631 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
6632 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
6633 { "snapshot", 0, QEMU_OPTION_snapshot
},
6635 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
6637 { "m", HAS_ARG
, QEMU_OPTION_m
},
6638 { "nographic", 0, QEMU_OPTION_nographic
},
6639 { "k", HAS_ARG
, QEMU_OPTION_k
},
6641 { "audio-help", 0, QEMU_OPTION_audio_help
},
6642 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
6645 { "net", HAS_ARG
, QEMU_OPTION_net
},
6647 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
6648 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
6650 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
6652 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
6655 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
6656 { "append", HAS_ARG
, QEMU_OPTION_append
},
6657 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
6659 { "S", 0, QEMU_OPTION_S
},
6660 { "s", 0, QEMU_OPTION_s
},
6661 { "p", HAS_ARG
, QEMU_OPTION_p
},
6662 { "d", HAS_ARG
, QEMU_OPTION_d
},
6663 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
6664 { "L", HAS_ARG
, QEMU_OPTION_L
},
6665 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
6667 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
6668 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
6670 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6671 { "g", 1, QEMU_OPTION_g
},
6673 { "localtime", 0, QEMU_OPTION_localtime
},
6674 { "std-vga", 0, QEMU_OPTION_std_vga
},
6675 { "echr", 1, QEMU_OPTION_echr
},
6676 { "monitor", 1, QEMU_OPTION_monitor
},
6677 { "serial", 1, QEMU_OPTION_serial
},
6678 { "parallel", 1, QEMU_OPTION_parallel
},
6679 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
6680 { "full-screen", 0, QEMU_OPTION_full_screen
},
6682 { "no-frame", 0, QEMU_OPTION_no_frame
},
6683 { "no-quit", 0, QEMU_OPTION_no_quit
},
6685 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
6686 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
6687 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
6688 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
6689 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
6691 /* temporary options */
6692 { "usb", 0, QEMU_OPTION_usb
},
6693 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
6694 { "vmwarevga", 0, QEMU_OPTION_vmsvga
},
6695 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
6696 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
6697 { "daemonize", 0, QEMU_OPTION_daemonize
},
6698 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
6699 #if defined(TARGET_ARM)
6700 { "semihosting", 0, QEMU_OPTION_semihosting
},
6702 { "name", HAS_ARG
, QEMU_OPTION_name
},
6706 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
6708 /* this stack is only used during signal handling */
6709 #define SIGNAL_STACK_SIZE 32768
6711 static uint8_t *signal_stack
;
6715 /* password input */
6717 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
6722 if (!bdrv_is_encrypted(bs
))
6725 term_printf("%s is encrypted.\n", name
);
6726 for(i
= 0; i
< 3; i
++) {
6727 monitor_readline("Password: ", 1, password
, sizeof(password
));
6728 if (bdrv_set_key(bs
, password
) == 0)
6730 term_printf("invalid password\n");
6735 static BlockDriverState
*get_bdrv(int index
)
6737 BlockDriverState
*bs
;
6740 bs
= bs_table
[index
];
6741 } else if (index
< 6) {
6742 bs
= fd_table
[index
- 4];
6749 static void read_passwords(void)
6751 BlockDriverState
*bs
;
6754 for(i
= 0; i
< 6; i
++) {
6757 qemu_key_check(bs
, bdrv_get_device_name(bs
));
6761 /* XXX: currently we cannot use simultaneously different CPUs */
6762 void register_machines(void)
6764 #if defined(TARGET_I386)
6765 qemu_register_machine(&pc_machine
);
6766 qemu_register_machine(&isapc_machine
);
6767 #elif defined(TARGET_PPC)
6768 qemu_register_machine(&heathrow_machine
);
6769 qemu_register_machine(&core99_machine
);
6770 qemu_register_machine(&prep_machine
);
6771 qemu_register_machine(&ref405ep_machine
);
6772 qemu_register_machine(&taihu_machine
);
6773 #elif defined(TARGET_MIPS)
6774 qemu_register_machine(&mips_machine
);
6775 qemu_register_machine(&mips_malta_machine
);
6776 qemu_register_machine(&mips_pica61_machine
);
6777 #elif defined(TARGET_SPARC)
6778 #ifdef TARGET_SPARC64
6779 qemu_register_machine(&sun4u_machine
);
6781 qemu_register_machine(&ss5_machine
);
6782 qemu_register_machine(&ss10_machine
);
6784 #elif defined(TARGET_ARM)
6785 qemu_register_machine(&integratorcp_machine
);
6786 qemu_register_machine(&versatilepb_machine
);
6787 qemu_register_machine(&versatileab_machine
);
6788 qemu_register_machine(&realview_machine
);
6789 #elif defined(TARGET_SH4)
6790 qemu_register_machine(&shix_machine
);
6791 #elif defined(TARGET_ALPHA)
6794 #error unsupported CPU
6799 struct soundhw soundhw
[] = {
6806 { .init_isa
= pcspk_audio_init
}
6811 "Creative Sound Blaster 16",
6814 { .init_isa
= SB16_init
}
6821 "Yamaha YMF262 (OPL3)",
6823 "Yamaha YM3812 (OPL2)",
6827 { .init_isa
= Adlib_init
}
6834 "Gravis Ultrasound GF1",
6837 { .init_isa
= GUS_init
}
6843 "ENSONIQ AudioPCI ES1370",
6846 { .init_pci
= es1370_init
}
6849 { NULL
, NULL
, 0, 0, { NULL
} }
6852 static void select_soundhw (const char *optarg
)
6856 if (*optarg
== '?') {
6859 printf ("Valid sound card names (comma separated):\n");
6860 for (c
= soundhw
; c
->name
; ++c
) {
6861 printf ("%-11s %s\n", c
->name
, c
->descr
);
6863 printf ("\n-soundhw all will enable all of the above\n");
6864 exit (*optarg
!= '?');
6872 if (!strcmp (optarg
, "all")) {
6873 for (c
= soundhw
; c
->name
; ++c
) {
6881 e
= strchr (p
, ',');
6882 l
= !e
? strlen (p
) : (size_t) (e
- p
);
6884 for (c
= soundhw
; c
->name
; ++c
) {
6885 if (!strncmp (c
->name
, p
, l
)) {
6894 "Unknown sound card name (too big to show)\n");
6897 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
6902 p
+= l
+ (e
!= NULL
);
6906 goto show_valid_cards
;
6912 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
6914 exit(STATUS_CONTROL_C_EXIT
);
6919 #define MAX_NET_CLIENTS 32
6921 int main(int argc
, char **argv
)
6923 #ifdef CONFIG_GDBSTUB
6925 const char *gdbstub_port
;
6927 int i
, cdrom_index
, pflash_index
;
6928 int snapshot
, linux_boot
;
6929 const char *initrd_filename
;
6930 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
6931 const char *pflash_filename
[MAX_PFLASH
];
6932 const char *sd_filename
;
6933 const char *kernel_filename
, *kernel_cmdline
;
6934 DisplayState
*ds
= &display_state
;
6935 int cyls
, heads
, secs
, translation
;
6936 char net_clients
[MAX_NET_CLIENTS
][256];
6939 const char *r
, *optarg
;
6940 CharDriverState
*monitor_hd
;
6941 char monitor_device
[128];
6942 char serial_devices
[MAX_SERIAL_PORTS
][128];
6943 int serial_device_index
;
6944 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
6945 int parallel_device_index
;
6946 const char *loadvm
= NULL
;
6947 QEMUMachine
*machine
;
6948 const char *cpu_model
;
6949 char usb_devices
[MAX_USB_CMDLINE
][128];
6950 int usb_devices_index
;
6952 const char *pid_file
= NULL
;
6954 LIST_INIT (&vm_change_state_head
);
6957 struct sigaction act
;
6958 sigfillset(&act
.sa_mask
);
6960 act
.sa_handler
= SIG_IGN
;
6961 sigaction(SIGPIPE
, &act
, NULL
);
6964 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
6965 /* Note: cpu_interrupt() is currently not SMP safe, so we force
6966 QEMU to run on a single CPU */
6971 h
= GetCurrentProcess();
6972 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
6973 for(i
= 0; i
< 32; i
++) {
6974 if (mask
& (1 << i
))
6979 SetProcessAffinityMask(h
, mask
);
6985 register_machines();
6986 machine
= first_machine
;
6988 initrd_filename
= NULL
;
6989 for(i
= 0; i
< MAX_FD
; i
++)
6990 fd_filename
[i
] = NULL
;
6991 for(i
= 0; i
< MAX_DISKS
; i
++)
6992 hd_filename
[i
] = NULL
;
6993 for(i
= 0; i
< MAX_PFLASH
; i
++)
6994 pflash_filename
[i
] = NULL
;
6997 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
6998 vga_ram_size
= VGA_RAM_SIZE
;
6999 #ifdef CONFIG_GDBSTUB
7001 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
7005 kernel_filename
= NULL
;
7006 kernel_cmdline
= "";
7012 cyls
= heads
= secs
= 0;
7013 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7014 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
7016 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
7017 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
7018 serial_devices
[i
][0] = '\0';
7019 serial_device_index
= 0;
7021 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
7022 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
7023 parallel_devices
[i
][0] = '\0';
7024 parallel_device_index
= 0;
7026 usb_devices_index
= 0;
7031 /* default mac address of the first network interface */
7039 hd_filename
[0] = argv
[optind
++];
7041 const QEMUOption
*popt
;
7044 /* Treat --foo the same as -foo. */
7047 popt
= qemu_options
;
7050 fprintf(stderr
, "%s: invalid option -- '%s'\n",
7054 if (!strcmp(popt
->name
, r
+ 1))
7058 if (popt
->flags
& HAS_ARG
) {
7059 if (optind
>= argc
) {
7060 fprintf(stderr
, "%s: option '%s' requires an argument\n",
7064 optarg
= argv
[optind
++];
7069 switch(popt
->index
) {
7071 machine
= find_machine(optarg
);
7074 printf("Supported machines are:\n");
7075 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
7076 printf("%-10s %s%s\n",
7078 m
== first_machine
? " (default)" : "");
7083 case QEMU_OPTION_cpu
:
7084 /* hw initialization will check this */
7085 if (optarg
[0] == '?') {
7086 #if defined(TARGET_PPC)
7087 ppc_cpu_list(stdout
, &fprintf
);
7088 #elif defined(TARGET_ARM)
7090 #elif defined(TARGET_MIPS)
7091 mips_cpu_list(stdout
, &fprintf
);
7092 #elif defined(TARGET_SPARC)
7093 sparc_cpu_list(stdout
, &fprintf
);
7100 case QEMU_OPTION_initrd
:
7101 initrd_filename
= optarg
;
7103 case QEMU_OPTION_hda
:
7104 case QEMU_OPTION_hdb
:
7105 case QEMU_OPTION_hdc
:
7106 case QEMU_OPTION_hdd
:
7109 hd_index
= popt
->index
- QEMU_OPTION_hda
;
7110 hd_filename
[hd_index
] = optarg
;
7111 if (hd_index
== cdrom_index
)
7115 case QEMU_OPTION_sd
:
7116 sd_filename
= optarg
;
7118 case QEMU_OPTION_pflash
:
7119 if (pflash_index
>= MAX_PFLASH
) {
7120 fprintf(stderr
, "qemu: too many parallel flash images\n");
7123 pflash_filename
[pflash_index
++] = optarg
;
7125 case QEMU_OPTION_snapshot
:
7128 case QEMU_OPTION_hdachs
:
7132 cyls
= strtol(p
, (char **)&p
, 0);
7133 if (cyls
< 1 || cyls
> 16383)
7138 heads
= strtol(p
, (char **)&p
, 0);
7139 if (heads
< 1 || heads
> 16)
7144 secs
= strtol(p
, (char **)&p
, 0);
7145 if (secs
< 1 || secs
> 63)
7149 if (!strcmp(p
, "none"))
7150 translation
= BIOS_ATA_TRANSLATION_NONE
;
7151 else if (!strcmp(p
, "lba"))
7152 translation
= BIOS_ATA_TRANSLATION_LBA
;
7153 else if (!strcmp(p
, "auto"))
7154 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7157 } else if (*p
!= '\0') {
7159 fprintf(stderr
, "qemu: invalid physical CHS format\n");
7164 case QEMU_OPTION_nographic
:
7165 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
7166 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "null");
7167 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
7170 case QEMU_OPTION_kernel
:
7171 kernel_filename
= optarg
;
7173 case QEMU_OPTION_append
:
7174 kernel_cmdline
= optarg
;
7176 case QEMU_OPTION_cdrom
:
7177 if (cdrom_index
>= 0) {
7178 hd_filename
[cdrom_index
] = optarg
;
7181 case QEMU_OPTION_boot
:
7182 boot_device
= optarg
[0];
7183 if (boot_device
!= 'a' &&
7184 #if defined(TARGET_SPARC) || defined(TARGET_I386)
7186 boot_device
!= 'n' &&
7188 boot_device
!= 'c' && boot_device
!= 'd') {
7189 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
7193 case QEMU_OPTION_fda
:
7194 fd_filename
[0] = optarg
;
7196 case QEMU_OPTION_fdb
:
7197 fd_filename
[1] = optarg
;
7200 case QEMU_OPTION_no_fd_bootchk
:
7204 case QEMU_OPTION_no_code_copy
:
7205 code_copy_enabled
= 0;
7207 case QEMU_OPTION_net
:
7208 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
7209 fprintf(stderr
, "qemu: too many network clients\n");
7212 pstrcpy(net_clients
[nb_net_clients
],
7213 sizeof(net_clients
[0]),
7218 case QEMU_OPTION_tftp
:
7219 tftp_prefix
= optarg
;
7221 case QEMU_OPTION_bootp
:
7222 bootp_filename
= optarg
;
7225 case QEMU_OPTION_smb
:
7226 net_slirp_smb(optarg
);
7229 case QEMU_OPTION_redir
:
7230 net_slirp_redir(optarg
);
7234 case QEMU_OPTION_audio_help
:
7238 case QEMU_OPTION_soundhw
:
7239 select_soundhw (optarg
);
7246 ram_size
= atoi(optarg
) * 1024 * 1024;
7249 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
7250 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
7251 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
7260 mask
= cpu_str_to_log_mask(optarg
);
7262 printf("Log items (comma separated):\n");
7263 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
7264 printf("%-10s %s\n", item
->name
, item
->help
);
7271 #ifdef CONFIG_GDBSTUB
7276 gdbstub_port
= optarg
;
7286 keyboard_layout
= optarg
;
7288 case QEMU_OPTION_localtime
:
7291 case QEMU_OPTION_cirrusvga
:
7292 cirrus_vga_enabled
= 1;
7295 case QEMU_OPTION_vmsvga
:
7296 cirrus_vga_enabled
= 0;
7299 case QEMU_OPTION_std_vga
:
7300 cirrus_vga_enabled
= 0;
7308 w
= strtol(p
, (char **)&p
, 10);
7311 fprintf(stderr
, "qemu: invalid resolution or depth\n");
7317 h
= strtol(p
, (char **)&p
, 10);
7322 depth
= strtol(p
, (char **)&p
, 10);
7323 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
7324 depth
!= 24 && depth
!= 32)
7326 } else if (*p
== '\0') {
7327 depth
= graphic_depth
;
7334 graphic_depth
= depth
;
7337 case QEMU_OPTION_echr
:
7340 term_escape_char
= strtol(optarg
, &r
, 0);
7342 printf("Bad argument to echr\n");
7345 case QEMU_OPTION_monitor
:
7346 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
7348 case QEMU_OPTION_serial
:
7349 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
7350 fprintf(stderr
, "qemu: too many serial ports\n");
7353 pstrcpy(serial_devices
[serial_device_index
],
7354 sizeof(serial_devices
[0]), optarg
);
7355 serial_device_index
++;
7357 case QEMU_OPTION_parallel
:
7358 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
7359 fprintf(stderr
, "qemu: too many parallel ports\n");
7362 pstrcpy(parallel_devices
[parallel_device_index
],
7363 sizeof(parallel_devices
[0]), optarg
);
7364 parallel_device_index
++;
7366 case QEMU_OPTION_loadvm
:
7369 case QEMU_OPTION_full_screen
:
7373 case QEMU_OPTION_no_frame
:
7376 case QEMU_OPTION_no_quit
:
7380 case QEMU_OPTION_pidfile
:
7384 case QEMU_OPTION_win2k_hack
:
7385 win2k_install_hack
= 1;
7389 case QEMU_OPTION_no_kqemu
:
7392 case QEMU_OPTION_kernel_kqemu
:
7396 case QEMU_OPTION_usb
:
7399 case QEMU_OPTION_usbdevice
:
7401 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
7402 fprintf(stderr
, "Too many USB devices\n");
7405 pstrcpy(usb_devices
[usb_devices_index
],
7406 sizeof(usb_devices
[usb_devices_index
]),
7408 usb_devices_index
++;
7410 case QEMU_OPTION_smp
:
7411 smp_cpus
= atoi(optarg
);
7412 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
7413 fprintf(stderr
, "Invalid number of CPUs\n");
7417 case QEMU_OPTION_vnc
:
7418 vnc_display
= optarg
;
7420 case QEMU_OPTION_no_acpi
:
7423 case QEMU_OPTION_no_reboot
:
7426 case QEMU_OPTION_daemonize
:
7429 case QEMU_OPTION_option_rom
:
7430 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
7431 fprintf(stderr
, "Too many option ROMs\n");
7434 option_rom
[nb_option_roms
] = optarg
;
7437 case QEMU_OPTION_semihosting
:
7438 semihosting_enabled
= 1;
7440 case QEMU_OPTION_name
:
7448 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
7449 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
7456 if (pipe(fds
) == -1)
7467 len
= read(fds
[0], &status
, 1);
7468 if (len
== -1 && (errno
== EINTR
))
7473 else if (status
== 1) {
7474 fprintf(stderr
, "Could not acquire pidfile\n");
7492 signal(SIGTSTP
, SIG_IGN
);
7493 signal(SIGTTOU
, SIG_IGN
);
7494 signal(SIGTTIN
, SIG_IGN
);
7498 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
7501 write(fds
[1], &status
, 1);
7503 fprintf(stderr
, "Could not acquire pid file\n");
7511 linux_boot
= (kernel_filename
!= NULL
);
7514 boot_device
!= 'n' &&
7515 hd_filename
[0] == '\0' &&
7516 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
7517 fd_filename
[0] == '\0')
7520 /* boot to floppy or the default cd if no hard disk defined yet */
7521 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
7522 if (fd_filename
[0] != '\0')
7528 setvbuf(stdout
, NULL
, _IOLBF
, 0);
7538 /* init network clients */
7539 if (nb_net_clients
== 0) {
7540 /* if no clients, we use a default config */
7541 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
7543 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
7548 for(i
= 0;i
< nb_net_clients
; i
++) {
7549 if (net_client_init(net_clients
[i
]) < 0)
7554 if (boot_device
== 'n') {
7555 for (i
= 0; i
< nb_nics
; i
++) {
7556 const char *model
= nd_table
[i
].model
;
7560 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
7561 if (get_image_size(buf
) > 0) {
7562 option_rom
[nb_option_roms
] = strdup(buf
);
7568 fprintf(stderr
, "No valid PXE rom found for network device\n");
7571 boot_device
= 'c'; /* to prevent confusion by the BIOS */
7575 /* init the memory */
7576 phys_ram_size
= ram_size
+ vga_ram_size
+ MAX_BIOS_SIZE
;
7578 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
7579 if (!phys_ram_base
) {
7580 fprintf(stderr
, "Could not allocate physical memory\n");
7584 /* we always create the cdrom drive, even if no disk is there */
7586 if (cdrom_index
>= 0) {
7587 bs_table
[cdrom_index
] = bdrv_new("cdrom");
7588 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
7591 /* open the virtual block devices */
7592 for(i
= 0; i
< MAX_DISKS
; i
++) {
7593 if (hd_filename
[i
]) {
7596 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
7597 bs_table
[i
] = bdrv_new(buf
);
7599 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7600 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
7604 if (i
== 0 && cyls
!= 0) {
7605 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
7606 bdrv_set_translation_hint(bs_table
[i
], translation
);
7611 /* we always create at least one floppy disk */
7612 fd_table
[0] = bdrv_new("fda");
7613 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
7615 for(i
= 0; i
< MAX_FD
; i
++) {
7616 if (fd_filename
[i
]) {
7619 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
7620 fd_table
[i
] = bdrv_new(buf
);
7621 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
7623 if (fd_filename
[i
][0] != '\0') {
7624 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
7625 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7626 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
7634 /* Open the virtual parallel flash block devices */
7635 for(i
= 0; i
< MAX_PFLASH
; i
++) {
7636 if (pflash_filename
[i
]) {
7637 if (!pflash_table
[i
]) {
7639 snprintf(buf
, sizeof(buf
), "fl%c", i
+ 'a');
7640 pflash_table
[i
] = bdrv_new(buf
);
7642 if (bdrv_open(pflash_table
[i
], pflash_filename
[i
],
7643 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7644 fprintf(stderr
, "qemu: could not open flash image '%s'\n",
7645 pflash_filename
[i
]);
7651 sd_bdrv
= bdrv_new ("sd");
7652 /* FIXME: This isn't really a floppy, but it's a reasonable
7654 bdrv_set_type_hint(sd_bdrv
, BDRV_TYPE_FLOPPY
);
7656 if (bdrv_open(sd_bdrv
, sd_filename
,
7657 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7658 fprintf(stderr
, "qemu: could not open SD card image %s\n",
7661 qemu_key_check(bs
, sd_filename
);
7664 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
7665 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
7671 dumb_display_init(ds
);
7672 } else if (vnc_display
!= NULL
) {
7673 vnc_display_init(ds
, vnc_display
);
7675 #if defined(CONFIG_SDL)
7676 sdl_display_init(ds
, full_screen
, no_frame
);
7677 #elif defined(CONFIG_COCOA)
7678 cocoa_display_init(ds
, full_screen
);
7680 dumb_display_init(ds
);
7684 /* Maintain compatibility with multiple stdio monitors */
7685 if (!strcmp(monitor_device
,"stdio")) {
7686 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7687 if (!strcmp(serial_devices
[i
],"mon:stdio")) {
7688 monitor_device
[0] = '\0';
7690 } else if (!strcmp(serial_devices
[i
],"stdio")) {
7691 monitor_device
[0] = '\0';
7692 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "mon:stdio");
7697 if (monitor_device
[0] != '\0') {
7698 monitor_hd
= qemu_chr_open(monitor_device
);
7700 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
7703 monitor_init(monitor_hd
, !nographic
);
7706 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7707 const char *devname
= serial_devices
[i
];
7708 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7709 serial_hds
[i
] = qemu_chr_open(devname
);
7710 if (!serial_hds
[i
]) {
7711 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
7715 if (!strcmp(devname
, "vc"))
7716 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
7720 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
7721 const char *devname
= parallel_devices
[i
];
7722 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7723 parallel_hds
[i
] = qemu_chr_open(devname
);
7724 if (!parallel_hds
[i
]) {
7725 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
7729 if (!strcmp(devname
, "vc"))
7730 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
7734 machine
->init(ram_size
, vga_ram_size
, boot_device
,
7735 ds
, fd_filename
, snapshot
,
7736 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
7738 /* init USB devices */
7740 for(i
= 0; i
< usb_devices_index
; i
++) {
7741 if (usb_device_add(usb_devices
[i
]) < 0) {
7742 fprintf(stderr
, "Warning: could not add USB device %s\n",
7748 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
7749 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
7751 #ifdef CONFIG_GDBSTUB
7753 /* XXX: use standard host:port notation and modify options
7755 if (gdbserver_start(gdbstub_port
) < 0) {
7756 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
7766 /* XXX: simplify init */
7779 len
= write(fds
[1], &status
, 1);
7780 if (len
== -1 && (errno
== EINTR
))
7786 fd
= open("/dev/null", O_RDWR
);