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: setsockopt(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 /***********************************************************/
4417 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4421 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4425 static void dumb_refresh(DisplayState
*ds
)
4430 void dumb_display_init(DisplayState
*ds
)
4435 ds
->dpy_update
= dumb_update
;
4436 ds
->dpy_resize
= dumb_resize
;
4437 ds
->dpy_refresh
= dumb_refresh
;
4440 /***********************************************************/
4443 #define MAX_IO_HANDLERS 64
4445 typedef struct IOHandlerRecord
{
4447 IOCanRWHandler
*fd_read_poll
;
4449 IOHandler
*fd_write
;
4452 /* temporary data */
4454 struct IOHandlerRecord
*next
;
4457 static IOHandlerRecord
*first_io_handler
;
4459 /* XXX: fd_read_poll should be suppressed, but an API change is
4460 necessary in the character devices to suppress fd_can_read(). */
4461 int qemu_set_fd_handler2(int fd
,
4462 IOCanRWHandler
*fd_read_poll
,
4464 IOHandler
*fd_write
,
4467 IOHandlerRecord
**pioh
, *ioh
;
4469 if (!fd_read
&& !fd_write
) {
4470 pioh
= &first_io_handler
;
4475 if (ioh
->fd
== fd
) {
4482 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4486 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4489 ioh
->next
= first_io_handler
;
4490 first_io_handler
= ioh
;
4493 ioh
->fd_read_poll
= fd_read_poll
;
4494 ioh
->fd_read
= fd_read
;
4495 ioh
->fd_write
= fd_write
;
4496 ioh
->opaque
= opaque
;
4502 int qemu_set_fd_handler(int fd
,
4504 IOHandler
*fd_write
,
4507 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4510 /***********************************************************/
4511 /* Polling handling */
4513 typedef struct PollingEntry
{
4516 struct PollingEntry
*next
;
4519 static PollingEntry
*first_polling_entry
;
4521 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4523 PollingEntry
**ppe
, *pe
;
4524 pe
= qemu_mallocz(sizeof(PollingEntry
));
4528 pe
->opaque
= opaque
;
4529 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
4534 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
4536 PollingEntry
**ppe
, *pe
;
4537 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
4539 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
4548 /***********************************************************/
4549 /* Wait objects support */
4550 typedef struct WaitObjects
{
4552 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
4553 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
4554 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
4557 static WaitObjects wait_objects
= {0};
4559 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4561 WaitObjects
*w
= &wait_objects
;
4563 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
4565 w
->events
[w
->num
] = handle
;
4566 w
->func
[w
->num
] = func
;
4567 w
->opaque
[w
->num
] = opaque
;
4572 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4575 WaitObjects
*w
= &wait_objects
;
4578 for (i
= 0; i
< w
->num
; i
++) {
4579 if (w
->events
[i
] == handle
)
4582 w
->events
[i
] = w
->events
[i
+ 1];
4583 w
->func
[i
] = w
->func
[i
+ 1];
4584 w
->opaque
[i
] = w
->opaque
[i
+ 1];
4592 /***********************************************************/
4593 /* savevm/loadvm support */
4595 #define IO_BUF_SIZE 32768
4599 BlockDriverState
*bs
;
4602 int64_t base_offset
;
4603 int64_t buf_offset
; /* start of buffer when writing, end of buffer
4606 int buf_size
; /* 0 when writing */
4607 uint8_t buf
[IO_BUF_SIZE
];
4610 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
4614 f
= qemu_mallocz(sizeof(QEMUFile
));
4617 if (!strcmp(mode
, "wb")) {
4619 } else if (!strcmp(mode
, "rb")) {
4624 f
->outfile
= fopen(filename
, mode
);
4636 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
4640 f
= qemu_mallocz(sizeof(QEMUFile
));
4645 f
->is_writable
= is_writable
;
4646 f
->base_offset
= offset
;
4650 void qemu_fflush(QEMUFile
*f
)
4652 if (!f
->is_writable
)
4654 if (f
->buf_index
> 0) {
4656 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4657 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
4659 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4660 f
->buf
, f
->buf_index
);
4662 f
->buf_offset
+= f
->buf_index
;
4667 static void qemu_fill_buffer(QEMUFile
*f
)
4674 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4675 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
4679 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4680 f
->buf
, IO_BUF_SIZE
);
4686 f
->buf_offset
+= len
;
4689 void qemu_fclose(QEMUFile
*f
)
4699 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
4703 l
= IO_BUF_SIZE
- f
->buf_index
;
4706 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
4710 if (f
->buf_index
>= IO_BUF_SIZE
)
4715 void qemu_put_byte(QEMUFile
*f
, int v
)
4717 f
->buf
[f
->buf_index
++] = v
;
4718 if (f
->buf_index
>= IO_BUF_SIZE
)
4722 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
4728 l
= f
->buf_size
- f
->buf_index
;
4730 qemu_fill_buffer(f
);
4731 l
= f
->buf_size
- f
->buf_index
;
4737 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
4742 return size1
- size
;
4745 int qemu_get_byte(QEMUFile
*f
)
4747 if (f
->buf_index
>= f
->buf_size
) {
4748 qemu_fill_buffer(f
);
4749 if (f
->buf_index
>= f
->buf_size
)
4752 return f
->buf
[f
->buf_index
++];
4755 int64_t qemu_ftell(QEMUFile
*f
)
4757 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
4760 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
4762 if (whence
== SEEK_SET
) {
4764 } else if (whence
== SEEK_CUR
) {
4765 pos
+= qemu_ftell(f
);
4767 /* SEEK_END not supported */
4770 if (f
->is_writable
) {
4772 f
->buf_offset
= pos
;
4774 f
->buf_offset
= pos
;
4781 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
4783 qemu_put_byte(f
, v
>> 8);
4784 qemu_put_byte(f
, v
);
4787 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
4789 qemu_put_byte(f
, v
>> 24);
4790 qemu_put_byte(f
, v
>> 16);
4791 qemu_put_byte(f
, v
>> 8);
4792 qemu_put_byte(f
, v
);
4795 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
4797 qemu_put_be32(f
, v
>> 32);
4798 qemu_put_be32(f
, v
);
4801 unsigned int qemu_get_be16(QEMUFile
*f
)
4804 v
= qemu_get_byte(f
) << 8;
4805 v
|= qemu_get_byte(f
);
4809 unsigned int qemu_get_be32(QEMUFile
*f
)
4812 v
= qemu_get_byte(f
) << 24;
4813 v
|= qemu_get_byte(f
) << 16;
4814 v
|= qemu_get_byte(f
) << 8;
4815 v
|= qemu_get_byte(f
);
4819 uint64_t qemu_get_be64(QEMUFile
*f
)
4822 v
= (uint64_t)qemu_get_be32(f
) << 32;
4823 v
|= qemu_get_be32(f
);
4827 typedef struct SaveStateEntry
{
4831 SaveStateHandler
*save_state
;
4832 LoadStateHandler
*load_state
;
4834 struct SaveStateEntry
*next
;
4837 static SaveStateEntry
*first_se
;
4839 int register_savevm(const char *idstr
,
4842 SaveStateHandler
*save_state
,
4843 LoadStateHandler
*load_state
,
4846 SaveStateEntry
*se
, **pse
;
4848 se
= qemu_malloc(sizeof(SaveStateEntry
));
4851 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
4852 se
->instance_id
= instance_id
;
4853 se
->version_id
= version_id
;
4854 se
->save_state
= save_state
;
4855 se
->load_state
= load_state
;
4856 se
->opaque
= opaque
;
4859 /* add at the end of list */
4861 while (*pse
!= NULL
)
4862 pse
= &(*pse
)->next
;
4867 #define QEMU_VM_FILE_MAGIC 0x5145564d
4868 #define QEMU_VM_FILE_VERSION 0x00000002
4870 int qemu_savevm_state(QEMUFile
*f
)
4874 int64_t cur_pos
, len_pos
, total_len_pos
;
4876 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
4877 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
4878 total_len_pos
= qemu_ftell(f
);
4879 qemu_put_be64(f
, 0); /* total size */
4881 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4883 len
= strlen(se
->idstr
);
4884 qemu_put_byte(f
, len
);
4885 qemu_put_buffer(f
, se
->idstr
, len
);
4887 qemu_put_be32(f
, se
->instance_id
);
4888 qemu_put_be32(f
, se
->version_id
);
4890 /* record size: filled later */
4891 len_pos
= qemu_ftell(f
);
4892 qemu_put_be32(f
, 0);
4894 se
->save_state(f
, se
->opaque
);
4896 /* fill record size */
4897 cur_pos
= qemu_ftell(f
);
4898 len
= cur_pos
- len_pos
- 4;
4899 qemu_fseek(f
, len_pos
, SEEK_SET
);
4900 qemu_put_be32(f
, len
);
4901 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4903 cur_pos
= qemu_ftell(f
);
4904 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
4905 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
4906 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4912 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
4916 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4917 if (!strcmp(se
->idstr
, idstr
) &&
4918 instance_id
== se
->instance_id
)
4924 int qemu_loadvm_state(QEMUFile
*f
)
4927 int len
, ret
, instance_id
, record_len
, version_id
;
4928 int64_t total_len
, end_pos
, cur_pos
;
4932 v
= qemu_get_be32(f
);
4933 if (v
!= QEMU_VM_FILE_MAGIC
)
4935 v
= qemu_get_be32(f
);
4936 if (v
!= QEMU_VM_FILE_VERSION
) {
4941 total_len
= qemu_get_be64(f
);
4942 end_pos
= total_len
+ qemu_ftell(f
);
4944 if (qemu_ftell(f
) >= end_pos
)
4946 len
= qemu_get_byte(f
);
4947 qemu_get_buffer(f
, idstr
, len
);
4949 instance_id
= qemu_get_be32(f
);
4950 version_id
= qemu_get_be32(f
);
4951 record_len
= qemu_get_be32(f
);
4953 printf("idstr=%s instance=0x%x version=%d len=%d\n",
4954 idstr
, instance_id
, version_id
, record_len
);
4956 cur_pos
= qemu_ftell(f
);
4957 se
= find_se(idstr
, instance_id
);
4959 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
4960 instance_id
, idstr
);
4962 ret
= se
->load_state(f
, se
->opaque
, version_id
);
4964 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
4965 instance_id
, idstr
);
4968 /* always seek to exact end of record */
4969 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
4976 /* device can contain snapshots */
4977 static int bdrv_can_snapshot(BlockDriverState
*bs
)
4980 !bdrv_is_removable(bs
) &&
4981 !bdrv_is_read_only(bs
));
4984 /* device must be snapshots in order to have a reliable snapshot */
4985 static int bdrv_has_snapshot(BlockDriverState
*bs
)
4988 !bdrv_is_removable(bs
) &&
4989 !bdrv_is_read_only(bs
));
4992 static BlockDriverState
*get_bs_snapshots(void)
4994 BlockDriverState
*bs
;
4998 return bs_snapshots
;
4999 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5001 if (bdrv_can_snapshot(bs
))
5010 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
5013 QEMUSnapshotInfo
*sn_tab
, *sn
;
5017 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5020 for(i
= 0; i
< nb_sns
; i
++) {
5022 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
5032 void do_savevm(const char *name
)
5034 BlockDriverState
*bs
, *bs1
;
5035 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
5036 int must_delete
, ret
, i
;
5037 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5039 int saved_vm_running
;
5046 bs
= get_bs_snapshots();
5048 term_printf("No block device can accept snapshots\n");
5052 /* ??? Should this occur after vm_stop? */
5055 saved_vm_running
= vm_running
;
5060 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
5065 memset(sn
, 0, sizeof(*sn
));
5067 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
5068 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
5071 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
5074 /* fill auxiliary fields */
5077 sn
->date_sec
= tb
.time
;
5078 sn
->date_nsec
= tb
.millitm
* 1000000;
5080 gettimeofday(&tv
, NULL
);
5081 sn
->date_sec
= tv
.tv_sec
;
5082 sn
->date_nsec
= tv
.tv_usec
* 1000;
5084 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
5086 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5087 term_printf("Device %s does not support VM state snapshots\n",
5088 bdrv_get_device_name(bs
));
5092 /* save the VM state */
5093 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
5095 term_printf("Could not open VM state file\n");
5098 ret
= qemu_savevm_state(f
);
5099 sn
->vm_state_size
= qemu_ftell(f
);
5102 term_printf("Error %d while writing VM\n", ret
);
5106 /* create the snapshots */
5108 for(i
= 0; i
< MAX_DISKS
; i
++) {
5110 if (bdrv_has_snapshot(bs1
)) {
5112 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
5114 term_printf("Error while deleting snapshot on '%s'\n",
5115 bdrv_get_device_name(bs1
));
5118 ret
= bdrv_snapshot_create(bs1
, sn
);
5120 term_printf("Error while creating snapshot on '%s'\n",
5121 bdrv_get_device_name(bs1
));
5127 if (saved_vm_running
)
5131 void do_loadvm(const char *name
)
5133 BlockDriverState
*bs
, *bs1
;
5134 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5137 int saved_vm_running
;
5139 bs
= get_bs_snapshots();
5141 term_printf("No block device supports snapshots\n");
5145 /* Flush all IO requests so they don't interfere with the new state. */
5148 saved_vm_running
= vm_running
;
5151 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5153 if (bdrv_has_snapshot(bs1
)) {
5154 ret
= bdrv_snapshot_goto(bs1
, name
);
5157 term_printf("Warning: ");
5160 term_printf("Snapshots not supported on device '%s'\n",
5161 bdrv_get_device_name(bs1
));
5164 term_printf("Could not find snapshot '%s' on device '%s'\n",
5165 name
, bdrv_get_device_name(bs1
));
5168 term_printf("Error %d while activating snapshot on '%s'\n",
5169 ret
, bdrv_get_device_name(bs1
));
5172 /* fatal on snapshot block device */
5179 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5180 term_printf("Device %s does not support VM state snapshots\n",
5181 bdrv_get_device_name(bs
));
5185 /* restore the VM state */
5186 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
5188 term_printf("Could not open VM state file\n");
5191 ret
= qemu_loadvm_state(f
);
5194 term_printf("Error %d while loading VM state\n", ret
);
5197 if (saved_vm_running
)
5201 void do_delvm(const char *name
)
5203 BlockDriverState
*bs
, *bs1
;
5206 bs
= get_bs_snapshots();
5208 term_printf("No block device supports snapshots\n");
5212 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5214 if (bdrv_has_snapshot(bs1
)) {
5215 ret
= bdrv_snapshot_delete(bs1
, name
);
5217 if (ret
== -ENOTSUP
)
5218 term_printf("Snapshots not supported on device '%s'\n",
5219 bdrv_get_device_name(bs1
));
5221 term_printf("Error %d while deleting snapshot on '%s'\n",
5222 ret
, bdrv_get_device_name(bs1
));
5228 void do_info_snapshots(void)
5230 BlockDriverState
*bs
, *bs1
;
5231 QEMUSnapshotInfo
*sn_tab
, *sn
;
5235 bs
= get_bs_snapshots();
5237 term_printf("No available block device supports snapshots\n");
5240 term_printf("Snapshot devices:");
5241 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5243 if (bdrv_has_snapshot(bs1
)) {
5245 term_printf(" %s", bdrv_get_device_name(bs1
));
5250 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5252 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
5255 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
5256 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
5257 for(i
= 0; i
< nb_sns
; i
++) {
5259 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
5264 /***********************************************************/
5265 /* cpu save/restore */
5267 #if defined(TARGET_I386)
5269 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
5271 qemu_put_be32(f
, dt
->selector
);
5272 qemu_put_betl(f
, dt
->base
);
5273 qemu_put_be32(f
, dt
->limit
);
5274 qemu_put_be32(f
, dt
->flags
);
5277 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
5279 dt
->selector
= qemu_get_be32(f
);
5280 dt
->base
= qemu_get_betl(f
);
5281 dt
->limit
= qemu_get_be32(f
);
5282 dt
->flags
= qemu_get_be32(f
);
5285 void cpu_save(QEMUFile
*f
, void *opaque
)
5287 CPUState
*env
= opaque
;
5288 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
5292 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5293 qemu_put_betls(f
, &env
->regs
[i
]);
5294 qemu_put_betls(f
, &env
->eip
);
5295 qemu_put_betls(f
, &env
->eflags
);
5296 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
5297 qemu_put_be32s(f
, &hflags
);
5301 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
5303 for(i
= 0; i
< 8; i
++) {
5304 fptag
|= ((!env
->fptags
[i
]) << i
);
5307 qemu_put_be16s(f
, &fpuc
);
5308 qemu_put_be16s(f
, &fpus
);
5309 qemu_put_be16s(f
, &fptag
);
5311 #ifdef USE_X86LDOUBLE
5316 qemu_put_be16s(f
, &fpregs_format
);
5318 for(i
= 0; i
< 8; i
++) {
5319 #ifdef USE_X86LDOUBLE
5323 /* we save the real CPU data (in case of MMX usage only 'mant'
5324 contains the MMX register */
5325 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
5326 qemu_put_be64(f
, mant
);
5327 qemu_put_be16(f
, exp
);
5330 /* if we use doubles for float emulation, we save the doubles to
5331 avoid losing information in case of MMX usage. It can give
5332 problems if the image is restored on a CPU where long
5333 doubles are used instead. */
5334 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
5338 for(i
= 0; i
< 6; i
++)
5339 cpu_put_seg(f
, &env
->segs
[i
]);
5340 cpu_put_seg(f
, &env
->ldt
);
5341 cpu_put_seg(f
, &env
->tr
);
5342 cpu_put_seg(f
, &env
->gdt
);
5343 cpu_put_seg(f
, &env
->idt
);
5345 qemu_put_be32s(f
, &env
->sysenter_cs
);
5346 qemu_put_be32s(f
, &env
->sysenter_esp
);
5347 qemu_put_be32s(f
, &env
->sysenter_eip
);
5349 qemu_put_betls(f
, &env
->cr
[0]);
5350 qemu_put_betls(f
, &env
->cr
[2]);
5351 qemu_put_betls(f
, &env
->cr
[3]);
5352 qemu_put_betls(f
, &env
->cr
[4]);
5354 for(i
= 0; i
< 8; i
++)
5355 qemu_put_betls(f
, &env
->dr
[i
]);
5358 qemu_put_be32s(f
, &env
->a20_mask
);
5361 qemu_put_be32s(f
, &env
->mxcsr
);
5362 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5363 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5364 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5367 #ifdef TARGET_X86_64
5368 qemu_put_be64s(f
, &env
->efer
);
5369 qemu_put_be64s(f
, &env
->star
);
5370 qemu_put_be64s(f
, &env
->lstar
);
5371 qemu_put_be64s(f
, &env
->cstar
);
5372 qemu_put_be64s(f
, &env
->fmask
);
5373 qemu_put_be64s(f
, &env
->kernelgsbase
);
5375 qemu_put_be32s(f
, &env
->smbase
);
5378 #ifdef USE_X86LDOUBLE
5379 /* XXX: add that in a FPU generic layer */
5380 union x86_longdouble
{
5385 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
5386 #define EXPBIAS1 1023
5387 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
5388 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
5390 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
5394 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
5395 /* exponent + sign */
5396 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
5397 e
|= SIGND1(temp
) >> 16;
5402 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5404 CPUState
*env
= opaque
;
5407 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
5409 if (version_id
!= 3 && version_id
!= 4)
5411 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5412 qemu_get_betls(f
, &env
->regs
[i
]);
5413 qemu_get_betls(f
, &env
->eip
);
5414 qemu_get_betls(f
, &env
->eflags
);
5415 qemu_get_be32s(f
, &hflags
);
5417 qemu_get_be16s(f
, &fpuc
);
5418 qemu_get_be16s(f
, &fpus
);
5419 qemu_get_be16s(f
, &fptag
);
5420 qemu_get_be16s(f
, &fpregs_format
);
5422 /* NOTE: we cannot always restore the FPU state if the image come
5423 from a host with a different 'USE_X86LDOUBLE' define. We guess
5424 if we are in an MMX state to restore correctly in that case. */
5425 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
5426 for(i
= 0; i
< 8; i
++) {
5430 switch(fpregs_format
) {
5432 mant
= qemu_get_be64(f
);
5433 exp
= qemu_get_be16(f
);
5434 #ifdef USE_X86LDOUBLE
5435 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5437 /* difficult case */
5439 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5441 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5445 mant
= qemu_get_be64(f
);
5446 #ifdef USE_X86LDOUBLE
5448 union x86_longdouble
*p
;
5449 /* difficult case */
5450 p
= (void *)&env
->fpregs
[i
];
5455 fp64_to_fp80(p
, mant
);
5459 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5468 /* XXX: restore FPU round state */
5469 env
->fpstt
= (fpus
>> 11) & 7;
5470 env
->fpus
= fpus
& ~0x3800;
5472 for(i
= 0; i
< 8; i
++) {
5473 env
->fptags
[i
] = (fptag
>> i
) & 1;
5476 for(i
= 0; i
< 6; i
++)
5477 cpu_get_seg(f
, &env
->segs
[i
]);
5478 cpu_get_seg(f
, &env
->ldt
);
5479 cpu_get_seg(f
, &env
->tr
);
5480 cpu_get_seg(f
, &env
->gdt
);
5481 cpu_get_seg(f
, &env
->idt
);
5483 qemu_get_be32s(f
, &env
->sysenter_cs
);
5484 qemu_get_be32s(f
, &env
->sysenter_esp
);
5485 qemu_get_be32s(f
, &env
->sysenter_eip
);
5487 qemu_get_betls(f
, &env
->cr
[0]);
5488 qemu_get_betls(f
, &env
->cr
[2]);
5489 qemu_get_betls(f
, &env
->cr
[3]);
5490 qemu_get_betls(f
, &env
->cr
[4]);
5492 for(i
= 0; i
< 8; i
++)
5493 qemu_get_betls(f
, &env
->dr
[i
]);
5496 qemu_get_be32s(f
, &env
->a20_mask
);
5498 qemu_get_be32s(f
, &env
->mxcsr
);
5499 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5500 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5501 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5504 #ifdef TARGET_X86_64
5505 qemu_get_be64s(f
, &env
->efer
);
5506 qemu_get_be64s(f
, &env
->star
);
5507 qemu_get_be64s(f
, &env
->lstar
);
5508 qemu_get_be64s(f
, &env
->cstar
);
5509 qemu_get_be64s(f
, &env
->fmask
);
5510 qemu_get_be64s(f
, &env
->kernelgsbase
);
5512 if (version_id
>= 4)
5513 qemu_get_be32s(f
, &env
->smbase
);
5515 /* XXX: compute hflags from scratch, except for CPL and IIF */
5516 env
->hflags
= hflags
;
5521 #elif defined(TARGET_PPC)
5522 void cpu_save(QEMUFile
*f
, void *opaque
)
5526 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5531 #elif defined(TARGET_MIPS)
5532 void cpu_save(QEMUFile
*f
, void *opaque
)
5536 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5541 #elif defined(TARGET_SPARC)
5542 void cpu_save(QEMUFile
*f
, void *opaque
)
5544 CPUState
*env
= opaque
;
5548 for(i
= 0; i
< 8; i
++)
5549 qemu_put_betls(f
, &env
->gregs
[i
]);
5550 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5551 qemu_put_betls(f
, &env
->regbase
[i
]);
5554 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5560 qemu_put_be32(f
, u
.i
);
5563 qemu_put_betls(f
, &env
->pc
);
5564 qemu_put_betls(f
, &env
->npc
);
5565 qemu_put_betls(f
, &env
->y
);
5567 qemu_put_be32(f
, tmp
);
5568 qemu_put_betls(f
, &env
->fsr
);
5569 qemu_put_betls(f
, &env
->tbr
);
5570 #ifndef TARGET_SPARC64
5571 qemu_put_be32s(f
, &env
->wim
);
5573 for(i
= 0; i
< 16; i
++)
5574 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
5578 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5580 CPUState
*env
= opaque
;
5584 for(i
= 0; i
< 8; i
++)
5585 qemu_get_betls(f
, &env
->gregs
[i
]);
5586 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5587 qemu_get_betls(f
, &env
->regbase
[i
]);
5590 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5595 u
.i
= qemu_get_be32(f
);
5599 qemu_get_betls(f
, &env
->pc
);
5600 qemu_get_betls(f
, &env
->npc
);
5601 qemu_get_betls(f
, &env
->y
);
5602 tmp
= qemu_get_be32(f
);
5603 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
5604 correctly updated */
5606 qemu_get_betls(f
, &env
->fsr
);
5607 qemu_get_betls(f
, &env
->tbr
);
5608 #ifndef TARGET_SPARC64
5609 qemu_get_be32s(f
, &env
->wim
);
5611 for(i
= 0; i
< 16; i
++)
5612 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
5618 #elif defined(TARGET_ARM)
5620 /* ??? Need to implement these. */
5621 void cpu_save(QEMUFile
*f
, void *opaque
)
5625 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5632 #warning No CPU save/restore functions
5636 /***********************************************************/
5637 /* ram save/restore */
5639 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
5643 v
= qemu_get_byte(f
);
5646 if (qemu_get_buffer(f
, buf
, len
) != len
)
5650 v
= qemu_get_byte(f
);
5651 memset(buf
, v
, len
);
5659 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
5663 if (qemu_get_be32(f
) != phys_ram_size
)
5665 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
5666 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
5673 #define BDRV_HASH_BLOCK_SIZE 1024
5674 #define IOBUF_SIZE 4096
5675 #define RAM_CBLOCK_MAGIC 0xfabe
5677 typedef struct RamCompressState
{
5680 uint8_t buf
[IOBUF_SIZE
];
5683 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
5686 memset(s
, 0, sizeof(*s
));
5688 ret
= deflateInit2(&s
->zstream
, 1,
5690 9, Z_DEFAULT_STRATEGY
);
5693 s
->zstream
.avail_out
= IOBUF_SIZE
;
5694 s
->zstream
.next_out
= s
->buf
;
5698 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
5700 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
5701 qemu_put_be16(s
->f
, len
);
5702 qemu_put_buffer(s
->f
, buf
, len
);
5705 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
5709 s
->zstream
.avail_in
= len
;
5710 s
->zstream
.next_in
= (uint8_t *)buf
;
5711 while (s
->zstream
.avail_in
> 0) {
5712 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
5715 if (s
->zstream
.avail_out
== 0) {
5716 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
5717 s
->zstream
.avail_out
= IOBUF_SIZE
;
5718 s
->zstream
.next_out
= s
->buf
;
5724 static void ram_compress_close(RamCompressState
*s
)
5728 /* compress last bytes */
5730 ret
= deflate(&s
->zstream
, Z_FINISH
);
5731 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
5732 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
5734 ram_put_cblock(s
, s
->buf
, len
);
5736 s
->zstream
.avail_out
= IOBUF_SIZE
;
5737 s
->zstream
.next_out
= s
->buf
;
5738 if (ret
== Z_STREAM_END
)
5745 deflateEnd(&s
->zstream
);
5748 typedef struct RamDecompressState
{
5751 uint8_t buf
[IOBUF_SIZE
];
5752 } RamDecompressState
;
5754 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
5757 memset(s
, 0, sizeof(*s
));
5759 ret
= inflateInit(&s
->zstream
);
5765 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
5769 s
->zstream
.avail_out
= len
;
5770 s
->zstream
.next_out
= buf
;
5771 while (s
->zstream
.avail_out
> 0) {
5772 if (s
->zstream
.avail_in
== 0) {
5773 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
5775 clen
= qemu_get_be16(s
->f
);
5776 if (clen
> IOBUF_SIZE
)
5778 qemu_get_buffer(s
->f
, s
->buf
, clen
);
5779 s
->zstream
.avail_in
= clen
;
5780 s
->zstream
.next_in
= s
->buf
;
5782 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
5783 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
5790 static void ram_decompress_close(RamDecompressState
*s
)
5792 inflateEnd(&s
->zstream
);
5795 static void ram_save(QEMUFile
*f
, void *opaque
)
5798 RamCompressState s1
, *s
= &s1
;
5801 qemu_put_be32(f
, phys_ram_size
);
5802 if (ram_compress_open(s
, f
) < 0)
5804 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5806 if (tight_savevm_enabled
) {
5810 /* find if the memory block is available on a virtual
5813 for(j
= 0; j
< MAX_DISKS
; j
++) {
5815 sector_num
= bdrv_hash_find(bs_table
[j
],
5816 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5817 if (sector_num
>= 0)
5822 goto normal_compress
;
5825 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
5826 ram_compress_buf(s
, buf
, 10);
5832 ram_compress_buf(s
, buf
, 1);
5833 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5836 ram_compress_close(s
);
5839 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
5841 RamDecompressState s1
, *s
= &s1
;
5845 if (version_id
== 1)
5846 return ram_load_v1(f
, opaque
);
5847 if (version_id
!= 2)
5849 if (qemu_get_be32(f
) != phys_ram_size
)
5851 if (ram_decompress_open(s
, f
) < 0)
5853 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5854 if (ram_decompress_buf(s
, buf
, 1) < 0) {
5855 fprintf(stderr
, "Error while reading ram block header\n");
5859 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
5860 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
5869 ram_decompress_buf(s
, buf
+ 1, 9);
5871 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
5872 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
5873 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
5876 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
5877 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
5878 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
5879 bs_index
, sector_num
);
5886 printf("Error block header\n");
5890 ram_decompress_close(s
);
5894 /***********************************************************/
5895 /* bottom halves (can be seen as timers which expire ASAP) */
5904 static QEMUBH
*first_bh
= NULL
;
5906 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
5909 bh
= qemu_mallocz(sizeof(QEMUBH
));
5913 bh
->opaque
= opaque
;
5917 int qemu_bh_poll(void)
5936 void qemu_bh_schedule(QEMUBH
*bh
)
5938 CPUState
*env
= cpu_single_env
;
5942 bh
->next
= first_bh
;
5945 /* stop the currently executing CPU to execute the BH ASAP */
5947 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
5951 void qemu_bh_cancel(QEMUBH
*bh
)
5954 if (bh
->scheduled
) {
5957 pbh
= &(*pbh
)->next
;
5963 void qemu_bh_delete(QEMUBH
*bh
)
5969 /***********************************************************/
5970 /* machine registration */
5972 QEMUMachine
*first_machine
= NULL
;
5974 int qemu_register_machine(QEMUMachine
*m
)
5977 pm
= &first_machine
;
5985 QEMUMachine
*find_machine(const char *name
)
5989 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5990 if (!strcmp(m
->name
, name
))
5996 /***********************************************************/
5997 /* main execution loop */
5999 void gui_update(void *opaque
)
6001 display_state
.dpy_refresh(&display_state
);
6002 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
6005 struct vm_change_state_entry
{
6006 VMChangeStateHandler
*cb
;
6008 LIST_ENTRY (vm_change_state_entry
) entries
;
6011 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
6013 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
6016 VMChangeStateEntry
*e
;
6018 e
= qemu_mallocz(sizeof (*e
));
6024 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
6028 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
6030 LIST_REMOVE (e
, entries
);
6034 static void vm_state_notify(int running
)
6036 VMChangeStateEntry
*e
;
6038 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
6039 e
->cb(e
->opaque
, running
);
6043 /* XXX: support several handlers */
6044 static VMStopHandler
*vm_stop_cb
;
6045 static void *vm_stop_opaque
;
6047 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6050 vm_stop_opaque
= opaque
;
6054 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6068 void vm_stop(int reason
)
6071 cpu_disable_ticks();
6075 vm_stop_cb(vm_stop_opaque
, reason
);
6082 /* reset/shutdown handler */
6084 typedef struct QEMUResetEntry
{
6085 QEMUResetHandler
*func
;
6087 struct QEMUResetEntry
*next
;
6090 static QEMUResetEntry
*first_reset_entry
;
6091 static int reset_requested
;
6092 static int shutdown_requested
;
6093 static int powerdown_requested
;
6095 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
6097 QEMUResetEntry
**pre
, *re
;
6099 pre
= &first_reset_entry
;
6100 while (*pre
!= NULL
)
6101 pre
= &(*pre
)->next
;
6102 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
6104 re
->opaque
= opaque
;
6109 static void qemu_system_reset(void)
6113 /* reset all devices */
6114 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
6115 re
->func(re
->opaque
);
6119 void qemu_system_reset_request(void)
6122 shutdown_requested
= 1;
6124 reset_requested
= 1;
6127 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6130 void qemu_system_shutdown_request(void)
6132 shutdown_requested
= 1;
6134 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6137 void qemu_system_powerdown_request(void)
6139 powerdown_requested
= 1;
6141 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6144 void main_loop_wait(int timeout
)
6146 IOHandlerRecord
*ioh
;
6147 fd_set rfds
, wfds
, xfds
;
6156 /* XXX: need to suppress polling by better using win32 events */
6158 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
6159 ret
|= pe
->func(pe
->opaque
);
6164 WaitObjects
*w
= &wait_objects
;
6166 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
6167 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
6168 if (w
->func
[ret
- WAIT_OBJECT_0
])
6169 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
6171 /* Check for additional signaled events */
6172 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
6174 /* Check if event is signaled */
6175 ret2
= WaitForSingleObject(w
->events
[i
], 0);
6176 if(ret2
== WAIT_OBJECT_0
) {
6178 w
->func
[i
](w
->opaque
[i
]);
6179 } else if (ret2
== WAIT_TIMEOUT
) {
6181 err
= GetLastError();
6182 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
6185 } else if (ret
== WAIT_TIMEOUT
) {
6187 err
= GetLastError();
6188 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
6192 /* poll any events */
6193 /* XXX: separate device handlers from system ones */
6198 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6202 (!ioh
->fd_read_poll
||
6203 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
6204 FD_SET(ioh
->fd
, &rfds
);
6208 if (ioh
->fd_write
) {
6209 FD_SET(ioh
->fd
, &wfds
);
6219 tv
.tv_usec
= timeout
* 1000;
6221 #if defined(CONFIG_SLIRP)
6223 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
6226 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
6228 IOHandlerRecord
**pioh
;
6230 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6233 if (FD_ISSET(ioh
->fd
, &rfds
)) {
6234 ioh
->fd_read(ioh
->opaque
);
6236 if (FD_ISSET(ioh
->fd
, &wfds
)) {
6237 ioh
->fd_write(ioh
->opaque
);
6241 /* remove deleted IO handlers */
6242 pioh
= &first_io_handler
;
6252 #if defined(CONFIG_SLIRP)
6259 slirp_select_poll(&rfds
, &wfds
, &xfds
);
6266 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
6267 qemu_get_clock(vm_clock
));
6268 /* run dma transfers, if any */
6272 /* real time timers */
6273 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
6274 qemu_get_clock(rt_clock
));
6277 static CPUState
*cur_cpu
;
6282 #ifdef CONFIG_PROFILER
6287 cur_cpu
= first_cpu
;
6294 env
= env
->next_cpu
;
6297 #ifdef CONFIG_PROFILER
6298 ti
= profile_getclock();
6300 ret
= cpu_exec(env
);
6301 #ifdef CONFIG_PROFILER
6302 qemu_time
+= profile_getclock() - ti
;
6304 if (ret
== EXCP_HLT
) {
6305 /* Give the next CPU a chance to run. */
6309 if (ret
!= EXCP_HALTED
)
6311 /* all CPUs are halted ? */
6317 if (shutdown_requested
) {
6318 ret
= EXCP_INTERRUPT
;
6321 if (reset_requested
) {
6322 reset_requested
= 0;
6323 qemu_system_reset();
6324 ret
= EXCP_INTERRUPT
;
6326 if (powerdown_requested
) {
6327 powerdown_requested
= 0;
6328 qemu_system_powerdown();
6329 ret
= EXCP_INTERRUPT
;
6331 if (ret
== EXCP_DEBUG
) {
6332 vm_stop(EXCP_DEBUG
);
6334 /* If all cpus are halted then wait until the next IRQ */
6335 /* XXX: use timeout computed from timers */
6336 if (ret
== EXCP_HALTED
)
6343 #ifdef CONFIG_PROFILER
6344 ti
= profile_getclock();
6346 main_loop_wait(timeout
);
6347 #ifdef CONFIG_PROFILER
6348 dev_time
+= profile_getclock() - ti
;
6351 cpu_disable_ticks();
6357 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2007 Fabrice Bellard\n"
6358 "usage: %s [options] [disk_image]\n"
6360 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
6362 "Standard options:\n"
6363 "-M machine select emulated machine (-M ? for list)\n"
6364 "-cpu cpu select CPU (-cpu ? for list)\n"
6365 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
6366 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
6367 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
6368 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
6369 "-sd file use 'file' as SecureDigital card image\n"
6370 "-pflash file use 'file' as a parallel flash image\n"
6371 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
6372 "-snapshot write to temporary files instead of disk image files\n"
6374 "-no-frame open SDL window without a frame and window decorations\n"
6375 "-no-quit disable SDL window close capability\n"
6378 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
6380 "-m megs set virtual RAM size to megs MB [default=%d]\n"
6381 "-smp n set the number of CPUs to 'n' [default=1]\n"
6382 "-nographic disable graphical output and redirect serial I/Os to console\n"
6384 "-k language use keyboard layout (for example \"fr\" for French)\n"
6387 "-audio-help print list of audio drivers and their options\n"
6388 "-soundhw c1,... enable audio support\n"
6389 " and only specified sound cards (comma separated list)\n"
6390 " use -soundhw ? to get the list of supported cards\n"
6391 " use -soundhw all to enable all of them\n"
6393 "-localtime set the real time clock to local time [default=utc]\n"
6394 "-full-screen start in full screen\n"
6396 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
6398 "-usb enable the USB driver (will be the default soon)\n"
6399 "-usbdevice name add the host or guest USB device 'name'\n"
6400 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6401 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
6403 "-name string set the name of the guest\n"
6405 "Network options:\n"
6406 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
6407 " create a new Network Interface Card and connect it to VLAN 'n'\n"
6409 "-net user[,vlan=n][,hostname=host]\n"
6410 " connect the user mode network stack to VLAN 'n' and send\n"
6411 " hostname 'host' to DHCP clients\n"
6414 "-net tap[,vlan=n],ifname=name\n"
6415 " connect the host TAP network interface to VLAN 'n'\n"
6417 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
6418 " connect the host TAP network interface to VLAN 'n' and use\n"
6419 " the network script 'file' (default=%s);\n"
6420 " use 'script=no' to disable script execution;\n"
6421 " use 'fd=h' to connect to an already opened TAP interface\n"
6423 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
6424 " connect the vlan 'n' to another VLAN using a socket connection\n"
6425 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
6426 " connect the vlan 'n' to multicast maddr and port\n"
6427 "-net none use it alone to have zero network devices; if no -net option\n"
6428 " is provided, the default is '-net nic -net user'\n"
6431 "-tftp dir allow tftp access to files in dir [-net user]\n"
6432 "-bootp file advertise file in BOOTP replies\n"
6434 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
6436 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
6437 " redirect TCP or UDP connections from host to guest [-net user]\n"
6440 "Linux boot specific:\n"
6441 "-kernel bzImage use 'bzImage' as kernel image\n"
6442 "-append cmdline use 'cmdline' as kernel command line\n"
6443 "-initrd file use 'file' as initial ram disk\n"
6445 "Debug/Expert options:\n"
6446 "-monitor dev redirect the monitor to char device 'dev'\n"
6447 "-serial dev redirect the serial port to char device 'dev'\n"
6448 "-parallel dev redirect the parallel port to char device 'dev'\n"
6449 "-pidfile file Write PID to 'file'\n"
6450 "-S freeze CPU at startup (use 'c' to start execution)\n"
6451 "-s wait gdb connection to port\n"
6452 "-p port set gdb connection port [default=%s]\n"
6453 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
6454 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
6455 " translation (t=none or lba) (usually qemu can guess them)\n"
6456 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
6458 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
6459 "-no-kqemu disable KQEMU kernel module usage\n"
6461 #ifdef USE_CODE_COPY
6462 "-no-code-copy disable code copy acceleration\n"
6465 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
6466 " (default is CL-GD5446 PCI VGA)\n"
6467 "-no-acpi disable ACPI\n"
6469 "-no-reboot exit instead of rebooting\n"
6470 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
6471 "-vnc display start a VNC server on display\n"
6473 "-daemonize daemonize QEMU after initializing\n"
6475 "-option-rom rom load a file, rom, into the option ROM space\n"
6477 "During emulation, the following keys are useful:\n"
6478 "ctrl-alt-f toggle full screen\n"
6479 "ctrl-alt-n switch to virtual console 'n'\n"
6480 "ctrl-alt toggle mouse and keyboard grab\n"
6482 "When using -nographic, press 'ctrl-a h' to get some help.\n"
6487 DEFAULT_NETWORK_SCRIPT
,
6489 DEFAULT_GDBSTUB_PORT
,
6494 #define HAS_ARG 0x0001
6511 QEMU_OPTION_snapshot
,
6513 QEMU_OPTION_no_fd_bootchk
,
6516 QEMU_OPTION_nographic
,
6518 QEMU_OPTION_audio_help
,
6519 QEMU_OPTION_soundhw
,
6538 QEMU_OPTION_no_code_copy
,
6540 QEMU_OPTION_localtime
,
6541 QEMU_OPTION_cirrusvga
,
6544 QEMU_OPTION_std_vga
,
6546 QEMU_OPTION_monitor
,
6548 QEMU_OPTION_parallel
,
6550 QEMU_OPTION_full_screen
,
6551 QEMU_OPTION_no_frame
,
6552 QEMU_OPTION_no_quit
,
6553 QEMU_OPTION_pidfile
,
6554 QEMU_OPTION_no_kqemu
,
6555 QEMU_OPTION_kernel_kqemu
,
6556 QEMU_OPTION_win2k_hack
,
6558 QEMU_OPTION_usbdevice
,
6561 QEMU_OPTION_no_acpi
,
6562 QEMU_OPTION_no_reboot
,
6563 QEMU_OPTION_daemonize
,
6564 QEMU_OPTION_option_rom
,
6565 QEMU_OPTION_semihosting
,
6569 typedef struct QEMUOption
{
6575 const QEMUOption qemu_options
[] = {
6576 { "h", 0, QEMU_OPTION_h
},
6577 { "help", 0, QEMU_OPTION_h
},
6579 { "M", HAS_ARG
, QEMU_OPTION_M
},
6580 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
6581 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
6582 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
6583 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
6584 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
6585 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
6586 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
6587 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
6588 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
6589 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
6590 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
6591 { "snapshot", 0, QEMU_OPTION_snapshot
},
6593 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
6595 { "m", HAS_ARG
, QEMU_OPTION_m
},
6596 { "nographic", 0, QEMU_OPTION_nographic
},
6597 { "k", HAS_ARG
, QEMU_OPTION_k
},
6599 { "audio-help", 0, QEMU_OPTION_audio_help
},
6600 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
6603 { "net", HAS_ARG
, QEMU_OPTION_net
},
6605 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
6606 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
6608 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
6610 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
6613 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
6614 { "append", HAS_ARG
, QEMU_OPTION_append
},
6615 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
6617 { "S", 0, QEMU_OPTION_S
},
6618 { "s", 0, QEMU_OPTION_s
},
6619 { "p", HAS_ARG
, QEMU_OPTION_p
},
6620 { "d", HAS_ARG
, QEMU_OPTION_d
},
6621 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
6622 { "L", HAS_ARG
, QEMU_OPTION_L
},
6623 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
6625 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
6626 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
6628 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6629 { "g", 1, QEMU_OPTION_g
},
6631 { "localtime", 0, QEMU_OPTION_localtime
},
6632 { "std-vga", 0, QEMU_OPTION_std_vga
},
6633 { "echr", 1, QEMU_OPTION_echr
},
6634 { "monitor", 1, QEMU_OPTION_monitor
},
6635 { "serial", 1, QEMU_OPTION_serial
},
6636 { "parallel", 1, QEMU_OPTION_parallel
},
6637 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
6638 { "full-screen", 0, QEMU_OPTION_full_screen
},
6640 { "no-frame", 0, QEMU_OPTION_no_frame
},
6641 { "no-quit", 0, QEMU_OPTION_no_quit
},
6643 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
6644 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
6645 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
6646 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
6647 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
6649 /* temporary options */
6650 { "usb", 0, QEMU_OPTION_usb
},
6651 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
6652 { "vmwarevga", 0, QEMU_OPTION_vmsvga
},
6653 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
6654 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
6655 { "daemonize", 0, QEMU_OPTION_daemonize
},
6656 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
6657 #if defined(TARGET_ARM)
6658 { "semihosting", 0, QEMU_OPTION_semihosting
},
6660 { "name", HAS_ARG
, QEMU_OPTION_name
},
6664 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
6666 /* this stack is only used during signal handling */
6667 #define SIGNAL_STACK_SIZE 32768
6669 static uint8_t *signal_stack
;
6673 /* password input */
6675 static BlockDriverState
*get_bdrv(int index
)
6677 BlockDriverState
*bs
;
6680 bs
= bs_table
[index
];
6681 } else if (index
< 6) {
6682 bs
= fd_table
[index
- 4];
6689 static void read_passwords(void)
6691 BlockDriverState
*bs
;
6695 for(i
= 0; i
< 6; i
++) {
6697 if (bs
&& bdrv_is_encrypted(bs
)) {
6698 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
6699 for(j
= 0; j
< 3; j
++) {
6700 monitor_readline("Password: ",
6701 1, password
, sizeof(password
));
6702 if (bdrv_set_key(bs
, password
) == 0)
6704 term_printf("invalid password\n");
6710 /* XXX: currently we cannot use simultaneously different CPUs */
6711 void register_machines(void)
6713 #if defined(TARGET_I386)
6714 qemu_register_machine(&pc_machine
);
6715 qemu_register_machine(&isapc_machine
);
6716 #elif defined(TARGET_PPC)
6717 qemu_register_machine(&heathrow_machine
);
6718 qemu_register_machine(&core99_machine
);
6719 qemu_register_machine(&prep_machine
);
6720 #elif defined(TARGET_MIPS)
6721 qemu_register_machine(&mips_machine
);
6722 qemu_register_machine(&mips_malta_machine
);
6723 qemu_register_machine(&mips_pica61_machine
);
6724 #elif defined(TARGET_SPARC)
6725 #ifdef TARGET_SPARC64
6726 qemu_register_machine(&sun4u_machine
);
6728 qemu_register_machine(&ss5_machine
);
6729 qemu_register_machine(&ss10_machine
);
6731 #elif defined(TARGET_ARM)
6732 qemu_register_machine(&integratorcp_machine
);
6733 qemu_register_machine(&versatilepb_machine
);
6734 qemu_register_machine(&versatileab_machine
);
6735 qemu_register_machine(&realview_machine
);
6736 #elif defined(TARGET_SH4)
6737 qemu_register_machine(&shix_machine
);
6738 #elif defined(TARGET_ALPHA)
6741 #error unsupported CPU
6746 struct soundhw soundhw
[] = {
6753 { .init_isa
= pcspk_audio_init
}
6758 "Creative Sound Blaster 16",
6761 { .init_isa
= SB16_init
}
6768 "Yamaha YMF262 (OPL3)",
6770 "Yamaha YM3812 (OPL2)",
6774 { .init_isa
= Adlib_init
}
6781 "Gravis Ultrasound GF1",
6784 { .init_isa
= GUS_init
}
6790 "ENSONIQ AudioPCI ES1370",
6793 { .init_pci
= es1370_init
}
6796 { NULL
, NULL
, 0, 0, { NULL
} }
6799 static void select_soundhw (const char *optarg
)
6803 if (*optarg
== '?') {
6806 printf ("Valid sound card names (comma separated):\n");
6807 for (c
= soundhw
; c
->name
; ++c
) {
6808 printf ("%-11s %s\n", c
->name
, c
->descr
);
6810 printf ("\n-soundhw all will enable all of the above\n");
6811 exit (*optarg
!= '?');
6819 if (!strcmp (optarg
, "all")) {
6820 for (c
= soundhw
; c
->name
; ++c
) {
6828 e
= strchr (p
, ',');
6829 l
= !e
? strlen (p
) : (size_t) (e
- p
);
6831 for (c
= soundhw
; c
->name
; ++c
) {
6832 if (!strncmp (c
->name
, p
, l
)) {
6841 "Unknown sound card name (too big to show)\n");
6844 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
6849 p
+= l
+ (e
!= NULL
);
6853 goto show_valid_cards
;
6859 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
6861 exit(STATUS_CONTROL_C_EXIT
);
6866 #define MAX_NET_CLIENTS 32
6868 int main(int argc
, char **argv
)
6870 #ifdef CONFIG_GDBSTUB
6872 const char *gdbstub_port
;
6874 int i
, cdrom_index
, pflash_index
;
6875 int snapshot
, linux_boot
;
6876 const char *initrd_filename
;
6877 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
6878 const char *pflash_filename
[MAX_PFLASH
];
6879 const char *sd_filename
;
6880 const char *kernel_filename
, *kernel_cmdline
;
6881 DisplayState
*ds
= &display_state
;
6882 int cyls
, heads
, secs
, translation
;
6883 char net_clients
[MAX_NET_CLIENTS
][256];
6886 const char *r
, *optarg
;
6887 CharDriverState
*monitor_hd
;
6888 char monitor_device
[128];
6889 char serial_devices
[MAX_SERIAL_PORTS
][128];
6890 int serial_device_index
;
6891 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
6892 int parallel_device_index
;
6893 const char *loadvm
= NULL
;
6894 QEMUMachine
*machine
;
6895 const char *cpu_model
;
6896 char usb_devices
[MAX_USB_CMDLINE
][128];
6897 int usb_devices_index
;
6899 const char *pid_file
= NULL
;
6901 LIST_INIT (&vm_change_state_head
);
6904 struct sigaction act
;
6905 sigfillset(&act
.sa_mask
);
6907 act
.sa_handler
= SIG_IGN
;
6908 sigaction(SIGPIPE
, &act
, NULL
);
6911 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
6912 /* Note: cpu_interrupt() is currently not SMP safe, so we force
6913 QEMU to run on a single CPU */
6918 h
= GetCurrentProcess();
6919 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
6920 for(i
= 0; i
< 32; i
++) {
6921 if (mask
& (1 << i
))
6926 SetProcessAffinityMask(h
, mask
);
6932 register_machines();
6933 machine
= first_machine
;
6935 initrd_filename
= NULL
;
6936 for(i
= 0; i
< MAX_FD
; i
++)
6937 fd_filename
[i
] = NULL
;
6938 for(i
= 0; i
< MAX_DISKS
; i
++)
6939 hd_filename
[i
] = NULL
;
6940 for(i
= 0; i
< MAX_PFLASH
; i
++)
6941 pflash_filename
[i
] = NULL
;
6944 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
6945 vga_ram_size
= VGA_RAM_SIZE
;
6946 #ifdef CONFIG_GDBSTUB
6948 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
6952 kernel_filename
= NULL
;
6953 kernel_cmdline
= "";
6959 cyls
= heads
= secs
= 0;
6960 translation
= BIOS_ATA_TRANSLATION_AUTO
;
6961 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
6963 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
6964 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
6965 serial_devices
[i
][0] = '\0';
6966 serial_device_index
= 0;
6968 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
6969 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
6970 parallel_devices
[i
][0] = '\0';
6971 parallel_device_index
= 0;
6973 usb_devices_index
= 0;
6978 /* default mac address of the first network interface */
6986 hd_filename
[0] = argv
[optind
++];
6988 const QEMUOption
*popt
;
6991 /* Treat --foo the same as -foo. */
6994 popt
= qemu_options
;
6997 fprintf(stderr
, "%s: invalid option -- '%s'\n",
7001 if (!strcmp(popt
->name
, r
+ 1))
7005 if (popt
->flags
& HAS_ARG
) {
7006 if (optind
>= argc
) {
7007 fprintf(stderr
, "%s: option '%s' requires an argument\n",
7011 optarg
= argv
[optind
++];
7016 switch(popt
->index
) {
7018 machine
= find_machine(optarg
);
7021 printf("Supported machines are:\n");
7022 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
7023 printf("%-10s %s%s\n",
7025 m
== first_machine
? " (default)" : "");
7030 case QEMU_OPTION_cpu
:
7031 /* hw initialization will check this */
7032 if (optarg
[0] == '?') {
7033 #if defined(TARGET_PPC)
7034 ppc_cpu_list(stdout
, &fprintf
);
7035 #elif defined(TARGET_ARM)
7037 #elif defined(TARGET_MIPS)
7038 mips_cpu_list(stdout
, &fprintf
);
7039 #elif defined(TARGET_SPARC)
7040 sparc_cpu_list(stdout
, &fprintf
);
7047 case QEMU_OPTION_initrd
:
7048 initrd_filename
= optarg
;
7050 case QEMU_OPTION_hda
:
7051 case QEMU_OPTION_hdb
:
7052 case QEMU_OPTION_hdc
:
7053 case QEMU_OPTION_hdd
:
7056 hd_index
= popt
->index
- QEMU_OPTION_hda
;
7057 hd_filename
[hd_index
] = optarg
;
7058 if (hd_index
== cdrom_index
)
7062 case QEMU_OPTION_sd
:
7063 sd_filename
= optarg
;
7065 case QEMU_OPTION_pflash
:
7066 if (pflash_index
>= MAX_PFLASH
) {
7067 fprintf(stderr
, "qemu: too many parallel flash images\n");
7070 pflash_filename
[pflash_index
++] = optarg
;
7072 case QEMU_OPTION_snapshot
:
7075 case QEMU_OPTION_hdachs
:
7079 cyls
= strtol(p
, (char **)&p
, 0);
7080 if (cyls
< 1 || cyls
> 16383)
7085 heads
= strtol(p
, (char **)&p
, 0);
7086 if (heads
< 1 || heads
> 16)
7091 secs
= strtol(p
, (char **)&p
, 0);
7092 if (secs
< 1 || secs
> 63)
7096 if (!strcmp(p
, "none"))
7097 translation
= BIOS_ATA_TRANSLATION_NONE
;
7098 else if (!strcmp(p
, "lba"))
7099 translation
= BIOS_ATA_TRANSLATION_LBA
;
7100 else if (!strcmp(p
, "auto"))
7101 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7104 } else if (*p
!= '\0') {
7106 fprintf(stderr
, "qemu: invalid physical CHS format\n");
7111 case QEMU_OPTION_nographic
:
7112 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
7113 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "null");
7114 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
7117 case QEMU_OPTION_kernel
:
7118 kernel_filename
= optarg
;
7120 case QEMU_OPTION_append
:
7121 kernel_cmdline
= optarg
;
7123 case QEMU_OPTION_cdrom
:
7124 if (cdrom_index
>= 0) {
7125 hd_filename
[cdrom_index
] = optarg
;
7128 case QEMU_OPTION_boot
:
7129 boot_device
= optarg
[0];
7130 if (boot_device
!= 'a' &&
7131 #if defined(TARGET_SPARC) || defined(TARGET_I386)
7133 boot_device
!= 'n' &&
7135 boot_device
!= 'c' && boot_device
!= 'd') {
7136 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
7140 case QEMU_OPTION_fda
:
7141 fd_filename
[0] = optarg
;
7143 case QEMU_OPTION_fdb
:
7144 fd_filename
[1] = optarg
;
7147 case QEMU_OPTION_no_fd_bootchk
:
7151 case QEMU_OPTION_no_code_copy
:
7152 code_copy_enabled
= 0;
7154 case QEMU_OPTION_net
:
7155 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
7156 fprintf(stderr
, "qemu: too many network clients\n");
7159 pstrcpy(net_clients
[nb_net_clients
],
7160 sizeof(net_clients
[0]),
7165 case QEMU_OPTION_tftp
:
7166 tftp_prefix
= optarg
;
7168 case QEMU_OPTION_bootp
:
7169 bootp_filename
= optarg
;
7172 case QEMU_OPTION_smb
:
7173 net_slirp_smb(optarg
);
7176 case QEMU_OPTION_redir
:
7177 net_slirp_redir(optarg
);
7181 case QEMU_OPTION_audio_help
:
7185 case QEMU_OPTION_soundhw
:
7186 select_soundhw (optarg
);
7193 ram_size
= atoi(optarg
) * 1024 * 1024;
7196 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
7197 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
7198 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
7207 mask
= cpu_str_to_log_mask(optarg
);
7209 printf("Log items (comma separated):\n");
7210 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
7211 printf("%-10s %s\n", item
->name
, item
->help
);
7218 #ifdef CONFIG_GDBSTUB
7223 gdbstub_port
= optarg
;
7233 keyboard_layout
= optarg
;
7235 case QEMU_OPTION_localtime
:
7238 case QEMU_OPTION_cirrusvga
:
7239 cirrus_vga_enabled
= 1;
7242 case QEMU_OPTION_vmsvga
:
7243 cirrus_vga_enabled
= 0;
7246 case QEMU_OPTION_std_vga
:
7247 cirrus_vga_enabled
= 0;
7255 w
= strtol(p
, (char **)&p
, 10);
7258 fprintf(stderr
, "qemu: invalid resolution or depth\n");
7264 h
= strtol(p
, (char **)&p
, 10);
7269 depth
= strtol(p
, (char **)&p
, 10);
7270 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
7271 depth
!= 24 && depth
!= 32)
7273 } else if (*p
== '\0') {
7274 depth
= graphic_depth
;
7281 graphic_depth
= depth
;
7284 case QEMU_OPTION_echr
:
7287 term_escape_char
= strtol(optarg
, &r
, 0);
7289 printf("Bad argument to echr\n");
7292 case QEMU_OPTION_monitor
:
7293 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
7295 case QEMU_OPTION_serial
:
7296 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
7297 fprintf(stderr
, "qemu: too many serial ports\n");
7300 pstrcpy(serial_devices
[serial_device_index
],
7301 sizeof(serial_devices
[0]), optarg
);
7302 serial_device_index
++;
7304 case QEMU_OPTION_parallel
:
7305 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
7306 fprintf(stderr
, "qemu: too many parallel ports\n");
7309 pstrcpy(parallel_devices
[parallel_device_index
],
7310 sizeof(parallel_devices
[0]), optarg
);
7311 parallel_device_index
++;
7313 case QEMU_OPTION_loadvm
:
7316 case QEMU_OPTION_full_screen
:
7320 case QEMU_OPTION_no_frame
:
7323 case QEMU_OPTION_no_quit
:
7327 case QEMU_OPTION_pidfile
:
7331 case QEMU_OPTION_win2k_hack
:
7332 win2k_install_hack
= 1;
7336 case QEMU_OPTION_no_kqemu
:
7339 case QEMU_OPTION_kernel_kqemu
:
7343 case QEMU_OPTION_usb
:
7346 case QEMU_OPTION_usbdevice
:
7348 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
7349 fprintf(stderr
, "Too many USB devices\n");
7352 pstrcpy(usb_devices
[usb_devices_index
],
7353 sizeof(usb_devices
[usb_devices_index
]),
7355 usb_devices_index
++;
7357 case QEMU_OPTION_smp
:
7358 smp_cpus
= atoi(optarg
);
7359 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
7360 fprintf(stderr
, "Invalid number of CPUs\n");
7364 case QEMU_OPTION_vnc
:
7365 vnc_display
= optarg
;
7367 case QEMU_OPTION_no_acpi
:
7370 case QEMU_OPTION_no_reboot
:
7373 case QEMU_OPTION_daemonize
:
7376 case QEMU_OPTION_option_rom
:
7377 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
7378 fprintf(stderr
, "Too many option ROMs\n");
7381 option_rom
[nb_option_roms
] = optarg
;
7384 case QEMU_OPTION_semihosting
:
7385 semihosting_enabled
= 1;
7387 case QEMU_OPTION_name
:
7395 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
7396 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
7403 if (pipe(fds
) == -1)
7414 len
= read(fds
[0], &status
, 1);
7415 if (len
== -1 && (errno
== EINTR
))
7420 else if (status
== 1) {
7421 fprintf(stderr
, "Could not acquire pidfile\n");
7439 signal(SIGTSTP
, SIG_IGN
);
7440 signal(SIGTTOU
, SIG_IGN
);
7441 signal(SIGTTIN
, SIG_IGN
);
7445 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
7448 write(fds
[1], &status
, 1);
7450 fprintf(stderr
, "Could not acquire pid file\n");
7458 linux_boot
= (kernel_filename
!= NULL
);
7461 boot_device
!= 'n' &&
7462 hd_filename
[0] == '\0' &&
7463 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
7464 fd_filename
[0] == '\0')
7467 /* boot to floppy or the default cd if no hard disk defined yet */
7468 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
7469 if (fd_filename
[0] != '\0')
7475 setvbuf(stdout
, NULL
, _IOLBF
, 0);
7485 /* init network clients */
7486 if (nb_net_clients
== 0) {
7487 /* if no clients, we use a default config */
7488 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
7490 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
7495 for(i
= 0;i
< nb_net_clients
; i
++) {
7496 if (net_client_init(net_clients
[i
]) < 0)
7501 if (boot_device
== 'n') {
7502 for (i
= 0; i
< nb_nics
; i
++) {
7503 const char *model
= nd_table
[i
].model
;
7507 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
7508 if (get_image_size(buf
) > 0) {
7509 option_rom
[nb_option_roms
] = strdup(buf
);
7515 fprintf(stderr
, "No valid PXE rom found for network device\n");
7518 boot_device
= 'c'; /* to prevent confusion by the BIOS */
7522 /* init the memory */
7523 phys_ram_size
= ram_size
+ vga_ram_size
+ MAX_BIOS_SIZE
;
7525 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
7526 if (!phys_ram_base
) {
7527 fprintf(stderr
, "Could not allocate physical memory\n");
7531 /* we always create the cdrom drive, even if no disk is there */
7533 if (cdrom_index
>= 0) {
7534 bs_table
[cdrom_index
] = bdrv_new("cdrom");
7535 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
7538 /* open the virtual block devices */
7539 for(i
= 0; i
< MAX_DISKS
; i
++) {
7540 if (hd_filename
[i
]) {
7543 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
7544 bs_table
[i
] = bdrv_new(buf
);
7546 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7547 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
7551 if (i
== 0 && cyls
!= 0) {
7552 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
7553 bdrv_set_translation_hint(bs_table
[i
], translation
);
7558 /* we always create at least one floppy disk */
7559 fd_table
[0] = bdrv_new("fda");
7560 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
7562 for(i
= 0; i
< MAX_FD
; i
++) {
7563 if (fd_filename
[i
]) {
7566 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
7567 fd_table
[i
] = bdrv_new(buf
);
7568 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
7570 if (fd_filename
[i
][0] != '\0') {
7571 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
7572 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7573 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
7581 /* Open the virtual parallel flash bloc devices */
7582 for(i
= 0; i
< MAX_PFLASH
; i
++) {
7583 if (pflash_filename
[i
]) {
7584 if (!pflash_table
[i
]) {
7586 snprintf(buf
, sizeof(buf
), "fl%c", i
+ 'a');
7587 pflash_table
[i
] = bdrv_new(buf
);
7589 if (bdrv_open(pflash_table
[i
], pflash_filename
[i
],
7590 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7591 fprintf(stderr
, "qemu: could not open flash image '%s'\n",
7592 pflash_filename
[i
]);
7598 sd_bdrv
= bdrv_new ("sd");
7599 /* FIXME: This isn't really a floppy, but it's a reasonable
7601 bdrv_set_type_hint(sd_bdrv
, BDRV_TYPE_FLOPPY
);
7603 if (bdrv_open(sd_bdrv
, sd_filename
,
7604 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7605 fprintf(stderr
, "qemu: could not open SD card image %s\n",
7610 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
7611 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
7617 dumb_display_init(ds
);
7618 } else if (vnc_display
!= NULL
) {
7619 vnc_display_init(ds
, vnc_display
);
7621 #if defined(CONFIG_SDL)
7622 sdl_display_init(ds
, full_screen
, no_frame
);
7623 #elif defined(CONFIG_COCOA)
7624 cocoa_display_init(ds
, full_screen
);
7626 dumb_display_init(ds
);
7630 /* Maintain compatibility with multiple stdio monitors */
7631 if (!strcmp(monitor_device
,"stdio")) {
7632 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7633 if (!strcmp(serial_devices
[i
],"mon:stdio")) {
7634 monitor_device
[0] = '\0';
7636 } else if (!strcmp(serial_devices
[i
],"stdio")) {
7637 monitor_device
[0] = '\0';
7638 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "mon:stdio");
7643 if (monitor_device
[0] != '\0') {
7644 monitor_hd
= qemu_chr_open(monitor_device
);
7646 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
7649 monitor_init(monitor_hd
, !nographic
);
7652 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7653 const char *devname
= serial_devices
[i
];
7654 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7655 serial_hds
[i
] = qemu_chr_open(devname
);
7656 if (!serial_hds
[i
]) {
7657 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
7661 if (!strcmp(devname
, "vc"))
7662 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
7666 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
7667 const char *devname
= parallel_devices
[i
];
7668 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7669 parallel_hds
[i
] = qemu_chr_open(devname
);
7670 if (!parallel_hds
[i
]) {
7671 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
7675 if (!strcmp(devname
, "vc"))
7676 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
7680 machine
->init(ram_size
, vga_ram_size
, boot_device
,
7681 ds
, fd_filename
, snapshot
,
7682 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
7684 /* init USB devices */
7686 for(i
= 0; i
< usb_devices_index
; i
++) {
7687 if (usb_device_add(usb_devices
[i
]) < 0) {
7688 fprintf(stderr
, "Warning: could not add USB device %s\n",
7694 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
7695 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
7697 #ifdef CONFIG_GDBSTUB
7699 /* XXX: use standard host:port notation and modify options
7701 if (gdbserver_start(gdbstub_port
) < 0) {
7702 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
7712 /* XXX: simplify init */
7725 len
= write(fds
[1], &status
, 1);
7726 if (len
== -1 && (errno
== EINTR
))
7732 fd
= open("/dev/null", O_RDWR
);