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 BlockDriverState
*mtd_bdrv
;
144 /* point to the block driver where the snapshots are managed */
145 BlockDriverState
*bs_snapshots
;
147 static DisplayState display_state
;
149 const char* keyboard_layout
= NULL
;
150 int64_t ticks_per_sec
;
151 int boot_device
= 'c';
153 int pit_min_timer_count
= 0;
155 NICInfo nd_table
[MAX_NICS
];
156 QEMUTimer
*gui_timer
;
159 int cirrus_vga_enabled
= 1;
160 int vmsvga_enabled
= 0;
162 int graphic_width
= 1024;
163 int graphic_height
= 768;
164 int graphic_depth
= 8;
166 int graphic_width
= 800;
167 int graphic_height
= 600;
168 int graphic_depth
= 15;
173 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
174 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
176 int win2k_install_hack
= 0;
179 static VLANState
*first_vlan
;
181 const char *vnc_display
;
182 #if defined(TARGET_SPARC)
184 #elif defined(TARGET_I386)
189 int acpi_enabled
= 1;
193 int graphic_rotate
= 0;
195 const char *option_rom
[MAX_OPTION_ROMS
];
197 int semihosting_enabled
= 0;
199 const char *qemu_name
;
201 unsigned int nb_prom_envs
= 0;
202 const char *prom_envs
[MAX_PROM_ENVS
];
205 /***********************************************************/
206 /* x86 ISA bus support */
208 target_phys_addr_t isa_mem_base
= 0;
211 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
213 #ifdef DEBUG_UNUSED_IOPORT
214 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
219 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
221 #ifdef DEBUG_UNUSED_IOPORT
222 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
226 /* default is to make two byte accesses */
227 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
230 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
231 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
232 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
236 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
238 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
239 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
240 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
243 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
245 #ifdef DEBUG_UNUSED_IOPORT
246 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
251 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
253 #ifdef DEBUG_UNUSED_IOPORT
254 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
258 void init_ioports(void)
262 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
263 ioport_read_table
[0][i
] = default_ioport_readb
;
264 ioport_write_table
[0][i
] = default_ioport_writeb
;
265 ioport_read_table
[1][i
] = default_ioport_readw
;
266 ioport_write_table
[1][i
] = default_ioport_writew
;
267 ioport_read_table
[2][i
] = default_ioport_readl
;
268 ioport_write_table
[2][i
] = default_ioport_writel
;
272 /* size is the word size in byte */
273 int register_ioport_read(int start
, int length
, int size
,
274 IOPortReadFunc
*func
, void *opaque
)
280 } else if (size
== 2) {
282 } else if (size
== 4) {
285 hw_error("register_ioport_read: invalid size");
288 for(i
= start
; i
< start
+ length
; i
+= size
) {
289 ioport_read_table
[bsize
][i
] = func
;
290 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
291 hw_error("register_ioport_read: invalid opaque");
292 ioport_opaque
[i
] = opaque
;
297 /* size is the word size in byte */
298 int register_ioport_write(int start
, int length
, int size
,
299 IOPortWriteFunc
*func
, void *opaque
)
305 } else if (size
== 2) {
307 } else if (size
== 4) {
310 hw_error("register_ioport_write: invalid size");
313 for(i
= start
; i
< start
+ length
; i
+= size
) {
314 ioport_write_table
[bsize
][i
] = func
;
315 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
316 hw_error("register_ioport_write: invalid opaque");
317 ioport_opaque
[i
] = opaque
;
322 void isa_unassign_ioport(int start
, int length
)
326 for(i
= start
; i
< start
+ length
; i
++) {
327 ioport_read_table
[0][i
] = default_ioport_readb
;
328 ioport_read_table
[1][i
] = default_ioport_readw
;
329 ioport_read_table
[2][i
] = default_ioport_readl
;
331 ioport_write_table
[0][i
] = default_ioport_writeb
;
332 ioport_write_table
[1][i
] = default_ioport_writew
;
333 ioport_write_table
[2][i
] = default_ioport_writel
;
337 /***********************************************************/
339 void cpu_outb(CPUState
*env
, int addr
, int val
)
342 if (loglevel
& CPU_LOG_IOPORT
)
343 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
345 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
348 env
->last_io_time
= cpu_get_time_fast();
352 void cpu_outw(CPUState
*env
, int addr
, int val
)
355 if (loglevel
& CPU_LOG_IOPORT
)
356 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
358 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
361 env
->last_io_time
= cpu_get_time_fast();
365 void cpu_outl(CPUState
*env
, int addr
, int val
)
368 if (loglevel
& CPU_LOG_IOPORT
)
369 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
371 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
374 env
->last_io_time
= cpu_get_time_fast();
378 int cpu_inb(CPUState
*env
, int addr
)
381 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
383 if (loglevel
& CPU_LOG_IOPORT
)
384 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
388 env
->last_io_time
= cpu_get_time_fast();
393 int cpu_inw(CPUState
*env
, int addr
)
396 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
398 if (loglevel
& CPU_LOG_IOPORT
)
399 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
403 env
->last_io_time
= cpu_get_time_fast();
408 int cpu_inl(CPUState
*env
, int addr
)
411 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
413 if (loglevel
& CPU_LOG_IOPORT
)
414 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
418 env
->last_io_time
= cpu_get_time_fast();
423 /***********************************************************/
424 void hw_error(const char *fmt
, ...)
430 fprintf(stderr
, "qemu: hardware error: ");
431 vfprintf(stderr
, fmt
, ap
);
432 fprintf(stderr
, "\n");
433 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
434 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
436 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
438 cpu_dump_state(env
, stderr
, fprintf
, 0);
445 /***********************************************************/
448 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
449 static void *qemu_put_kbd_event_opaque
;
450 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
451 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
453 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
455 qemu_put_kbd_event_opaque
= opaque
;
456 qemu_put_kbd_event
= func
;
459 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
460 void *opaque
, int absolute
,
463 QEMUPutMouseEntry
*s
, *cursor
;
465 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
469 s
->qemu_put_mouse_event
= func
;
470 s
->qemu_put_mouse_event_opaque
= opaque
;
471 s
->qemu_put_mouse_event_absolute
= absolute
;
472 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
475 if (!qemu_put_mouse_event_head
) {
476 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
480 cursor
= qemu_put_mouse_event_head
;
481 while (cursor
->next
!= NULL
)
482 cursor
= cursor
->next
;
485 qemu_put_mouse_event_current
= s
;
490 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
492 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
494 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
497 cursor
= qemu_put_mouse_event_head
;
498 while (cursor
!= NULL
&& cursor
!= entry
) {
500 cursor
= cursor
->next
;
503 if (cursor
== NULL
) // does not exist or list empty
505 else if (prev
== NULL
) { // entry is head
506 qemu_put_mouse_event_head
= cursor
->next
;
507 if (qemu_put_mouse_event_current
== entry
)
508 qemu_put_mouse_event_current
= cursor
->next
;
509 qemu_free(entry
->qemu_put_mouse_event_name
);
514 prev
->next
= entry
->next
;
516 if (qemu_put_mouse_event_current
== entry
)
517 qemu_put_mouse_event_current
= prev
;
519 qemu_free(entry
->qemu_put_mouse_event_name
);
523 void kbd_put_keycode(int keycode
)
525 if (qemu_put_kbd_event
) {
526 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
530 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
532 QEMUPutMouseEvent
*mouse_event
;
533 void *mouse_event_opaque
;
536 if (!qemu_put_mouse_event_current
) {
541 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
543 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
546 if (graphic_rotate
) {
547 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
550 width
= graphic_width
;
551 mouse_event(mouse_event_opaque
,
552 width
- dy
, dx
, dz
, buttons_state
);
554 mouse_event(mouse_event_opaque
,
555 dx
, dy
, dz
, buttons_state
);
559 int kbd_mouse_is_absolute(void)
561 if (!qemu_put_mouse_event_current
)
564 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
567 void do_info_mice(void)
569 QEMUPutMouseEntry
*cursor
;
572 if (!qemu_put_mouse_event_head
) {
573 term_printf("No mouse devices connected\n");
577 term_printf("Mouse devices available:\n");
578 cursor
= qemu_put_mouse_event_head
;
579 while (cursor
!= NULL
) {
580 term_printf("%c Mouse #%d: %s\n",
581 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
582 index
, cursor
->qemu_put_mouse_event_name
);
584 cursor
= cursor
->next
;
588 void do_mouse_set(int index
)
590 QEMUPutMouseEntry
*cursor
;
593 if (!qemu_put_mouse_event_head
) {
594 term_printf("No mouse devices connected\n");
598 cursor
= qemu_put_mouse_event_head
;
599 while (cursor
!= NULL
&& index
!= i
) {
601 cursor
= cursor
->next
;
605 qemu_put_mouse_event_current
= cursor
;
607 term_printf("Mouse at given index not found\n");
610 /* compute with 96 bit intermediate result: (a*b)/c */
611 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
616 #ifdef WORDS_BIGENDIAN
626 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
627 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
630 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
634 /***********************************************************/
635 /* real time host monotonic timer */
637 #define QEMU_TIMER_BASE 1000000000LL
641 static int64_t clock_freq
;
643 static void init_get_clock(void)
647 ret
= QueryPerformanceFrequency(&freq
);
649 fprintf(stderr
, "Could not calibrate ticks\n");
652 clock_freq
= freq
.QuadPart
;
655 static int64_t get_clock(void)
658 QueryPerformanceCounter(&ti
);
659 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
664 static int use_rt_clock
;
666 static void init_get_clock(void)
669 #if defined(__linux__)
672 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
679 static int64_t get_clock(void)
681 #if defined(__linux__)
684 clock_gettime(CLOCK_MONOTONIC
, &ts
);
685 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
689 /* XXX: using gettimeofday leads to problems if the date
690 changes, so it should be avoided. */
692 gettimeofday(&tv
, NULL
);
693 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
699 /***********************************************************/
700 /* guest cycle counter */
702 static int64_t cpu_ticks_prev
;
703 static int64_t cpu_ticks_offset
;
704 static int64_t cpu_clock_offset
;
705 static int cpu_ticks_enabled
;
707 /* return the host CPU cycle counter and handle stop/restart */
708 int64_t cpu_get_ticks(void)
710 if (!cpu_ticks_enabled
) {
711 return cpu_ticks_offset
;
714 ticks
= cpu_get_real_ticks();
715 if (cpu_ticks_prev
> ticks
) {
716 /* Note: non increasing ticks may happen if the host uses
718 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
720 cpu_ticks_prev
= ticks
;
721 return ticks
+ cpu_ticks_offset
;
725 /* return the host CPU monotonic timer and handle stop/restart */
726 static int64_t cpu_get_clock(void)
729 if (!cpu_ticks_enabled
) {
730 return cpu_clock_offset
;
733 return ti
+ cpu_clock_offset
;
737 /* enable cpu_get_ticks() */
738 void cpu_enable_ticks(void)
740 if (!cpu_ticks_enabled
) {
741 cpu_ticks_offset
-= cpu_get_real_ticks();
742 cpu_clock_offset
-= get_clock();
743 cpu_ticks_enabled
= 1;
747 /* disable cpu_get_ticks() : the clock is stopped. You must not call
748 cpu_get_ticks() after that. */
749 void cpu_disable_ticks(void)
751 if (cpu_ticks_enabled
) {
752 cpu_ticks_offset
= cpu_get_ticks();
753 cpu_clock_offset
= cpu_get_clock();
754 cpu_ticks_enabled
= 0;
758 /***********************************************************/
761 #define QEMU_TIMER_REALTIME 0
762 #define QEMU_TIMER_VIRTUAL 1
766 /* XXX: add frequency */
774 struct QEMUTimer
*next
;
780 static QEMUTimer
*active_timers
[2];
782 static MMRESULT timerID
;
783 static HANDLE host_alarm
= NULL
;
784 static unsigned int period
= 1;
786 /* frequency of the times() clock tick */
787 static int timer_freq
;
790 QEMUClock
*qemu_new_clock(int type
)
793 clock
= qemu_mallocz(sizeof(QEMUClock
));
800 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
804 ts
= qemu_mallocz(sizeof(QEMUTimer
));
811 void qemu_free_timer(QEMUTimer
*ts
)
816 /* stop a timer, but do not dealloc it */
817 void qemu_del_timer(QEMUTimer
*ts
)
821 /* NOTE: this code must be signal safe because
822 qemu_timer_expired() can be called from a signal. */
823 pt
= &active_timers
[ts
->clock
->type
];
836 /* modify the current timer so that it will be fired when current_time
837 >= expire_time. The corresponding callback will be called. */
838 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
844 /* add the timer in the sorted list */
845 /* NOTE: this code must be signal safe because
846 qemu_timer_expired() can be called from a signal. */
847 pt
= &active_timers
[ts
->clock
->type
];
852 if (t
->expire_time
> expire_time
)
856 ts
->expire_time
= expire_time
;
861 int qemu_timer_pending(QEMUTimer
*ts
)
864 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
871 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
875 return (timer_head
->expire_time
<= current_time
);
878 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
884 if (!ts
|| ts
->expire_time
> current_time
)
886 /* remove timer from the list before calling the callback */
887 *ptimer_head
= ts
->next
;
890 /* run the callback (the timer list can be modified) */
895 int64_t qemu_get_clock(QEMUClock
*clock
)
897 switch(clock
->type
) {
898 case QEMU_TIMER_REALTIME
:
899 return get_clock() / 1000000;
901 case QEMU_TIMER_VIRTUAL
:
902 return cpu_get_clock();
906 static void init_timers(void)
909 ticks_per_sec
= QEMU_TIMER_BASE
;
910 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
911 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
915 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
917 uint64_t expire_time
;
919 if (qemu_timer_pending(ts
)) {
920 expire_time
= ts
->expire_time
;
924 qemu_put_be64(f
, expire_time
);
927 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
929 uint64_t expire_time
;
931 expire_time
= qemu_get_be64(f
);
932 if (expire_time
!= -1) {
933 qemu_mod_timer(ts
, expire_time
);
939 static void timer_save(QEMUFile
*f
, void *opaque
)
941 if (cpu_ticks_enabled
) {
942 hw_error("cannot save state if virtual timers are running");
944 qemu_put_be64s(f
, &cpu_ticks_offset
);
945 qemu_put_be64s(f
, &ticks_per_sec
);
946 qemu_put_be64s(f
, &cpu_clock_offset
);
949 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
951 if (version_id
!= 1 && version_id
!= 2)
953 if (cpu_ticks_enabled
) {
956 qemu_get_be64s(f
, &cpu_ticks_offset
);
957 qemu_get_be64s(f
, &ticks_per_sec
);
958 if (version_id
== 2) {
959 qemu_get_be64s(f
, &cpu_clock_offset
);
965 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
966 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
968 static void host_alarm_handler(int host_signum
)
972 #define DISP_FREQ 1000
974 static int64_t delta_min
= INT64_MAX
;
975 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
977 ti
= qemu_get_clock(vm_clock
);
978 if (last_clock
!= 0) {
979 delta
= ti
- last_clock
;
980 if (delta
< delta_min
)
982 if (delta
> delta_max
)
985 if (++count
== DISP_FREQ
) {
986 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
987 muldiv64(delta_min
, 1000000, ticks_per_sec
),
988 muldiv64(delta_max
, 1000000, ticks_per_sec
),
989 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
990 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
992 delta_min
= INT64_MAX
;
1000 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1001 qemu_get_clock(vm_clock
)) ||
1002 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1003 qemu_get_clock(rt_clock
))) {
1005 SetEvent(host_alarm
);
1007 CPUState
*env
= cpu_single_env
;
1009 /* stop the currently executing cpu because a timer occured */
1010 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1012 if (env
->kqemu_enabled
) {
1013 kqemu_cpu_interrupt(env
);
1022 #if defined(__linux__)
1024 #define RTC_FREQ 1024
1028 static int start_rtc_timer(void)
1030 rtc_fd
= open("/dev/rtc", O_RDONLY
);
1033 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1034 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1035 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1036 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1039 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1044 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
1050 static int start_rtc_timer(void)
1055 #endif /* !defined(__linux__) */
1057 #endif /* !defined(_WIN32) */
1059 static void init_timer_alarm(void)
1066 ZeroMemory(&tc
, sizeof(TIMECAPS
));
1067 timeGetDevCaps(&tc
, sizeof(TIMECAPS
));
1068 if (period
< tc
.wPeriodMin
)
1069 period
= tc
.wPeriodMin
;
1070 timeBeginPeriod(period
);
1071 timerID
= timeSetEvent(1, // interval (ms)
1072 period
, // resolution
1073 host_alarm_handler
, // function
1074 (DWORD
)&count
, // user parameter
1075 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
1077 perror("failed timer alarm");
1080 host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1082 perror("failed CreateEvent");
1085 qemu_add_wait_object(host_alarm
, NULL
, NULL
);
1087 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
1090 struct sigaction act
;
1091 struct itimerval itv
;
1093 /* get times() syscall frequency */
1094 timer_freq
= sysconf(_SC_CLK_TCK
);
1097 sigfillset(&act
.sa_mask
);
1099 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1100 act
.sa_flags
|= SA_ONSTACK
;
1102 act
.sa_handler
= host_alarm_handler
;
1103 sigaction(SIGALRM
, &act
, NULL
);
1105 itv
.it_interval
.tv_sec
= 0;
1106 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
1107 itv
.it_value
.tv_sec
= 0;
1108 itv
.it_value
.tv_usec
= 10 * 1000;
1109 setitimer(ITIMER_REAL
, &itv
, NULL
);
1110 /* we probe the tick duration of the kernel to inform the user if
1111 the emulated kernel requested a too high timer frequency */
1112 getitimer(ITIMER_REAL
, &itv
);
1114 #if defined(__linux__)
1115 /* XXX: force /dev/rtc usage because even 2.6 kernels may not
1116 have timers with 1 ms resolution. The correct solution will
1117 be to use the POSIX real time timers available in recent
1119 if (itv
.it_interval
.tv_usec
> 1000 || 1) {
1120 /* try to use /dev/rtc to have a faster timer */
1121 if (start_rtc_timer() < 0)
1123 /* disable itimer */
1124 itv
.it_interval
.tv_sec
= 0;
1125 itv
.it_interval
.tv_usec
= 0;
1126 itv
.it_value
.tv_sec
= 0;
1127 itv
.it_value
.tv_usec
= 0;
1128 setitimer(ITIMER_REAL
, &itv
, NULL
);
1131 sigaction(SIGIO
, &act
, NULL
);
1132 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1133 fcntl(rtc_fd
, F_SETOWN
, getpid());
1135 #endif /* defined(__linux__) */
1138 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1139 PIT_FREQ
) / 1000000;
1145 void quit_timers(void)
1148 timeKillEvent(timerID
);
1149 timeEndPeriod(period
);
1151 CloseHandle(host_alarm
);
1157 /***********************************************************/
1158 /* character device */
1160 static void qemu_chr_event(CharDriverState
*s
, int event
)
1164 s
->chr_event(s
->handler_opaque
, event
);
1167 static void qemu_chr_reset_bh(void *opaque
)
1169 CharDriverState
*s
= opaque
;
1170 qemu_chr_event(s
, CHR_EVENT_RESET
);
1171 qemu_bh_delete(s
->bh
);
1175 void qemu_chr_reset(CharDriverState
*s
)
1177 if (s
->bh
== NULL
) {
1178 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
1179 qemu_bh_schedule(s
->bh
);
1183 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1185 return s
->chr_write(s
, buf
, len
);
1188 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1192 return s
->chr_ioctl(s
, cmd
, arg
);
1195 int qemu_chr_can_read(CharDriverState
*s
)
1197 if (!s
->chr_can_read
)
1199 return s
->chr_can_read(s
->handler_opaque
);
1202 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
1204 s
->chr_read(s
->handler_opaque
, buf
, len
);
1208 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1213 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1214 qemu_chr_write(s
, buf
, strlen(buf
));
1218 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1220 if (s
->chr_send_event
)
1221 s
->chr_send_event(s
, event
);
1224 void qemu_chr_add_handlers(CharDriverState
*s
,
1225 IOCanRWHandler
*fd_can_read
,
1226 IOReadHandler
*fd_read
,
1227 IOEventHandler
*fd_event
,
1230 s
->chr_can_read
= fd_can_read
;
1231 s
->chr_read
= fd_read
;
1232 s
->chr_event
= fd_event
;
1233 s
->handler_opaque
= opaque
;
1234 if (s
->chr_update_read_handler
)
1235 s
->chr_update_read_handler(s
);
1238 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1243 static CharDriverState
*qemu_chr_open_null(void)
1245 CharDriverState
*chr
;
1247 chr
= qemu_mallocz(sizeof(CharDriverState
));
1250 chr
->chr_write
= null_chr_write
;
1254 /* MUX driver for serial I/O splitting */
1255 static int term_timestamps
;
1256 static int64_t term_timestamps_start
;
1259 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
1260 IOReadHandler
*chr_read
[MAX_MUX
];
1261 IOEventHandler
*chr_event
[MAX_MUX
];
1262 void *ext_opaque
[MAX_MUX
];
1263 CharDriverState
*drv
;
1265 int term_got_escape
;
1270 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1272 MuxDriver
*d
= chr
->opaque
;
1274 if (!term_timestamps
) {
1275 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
1280 for(i
= 0; i
< len
; i
++) {
1281 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
1282 if (buf
[i
] == '\n') {
1288 if (term_timestamps_start
== -1)
1289 term_timestamps_start
= ti
;
1290 ti
-= term_timestamps_start
;
1291 secs
= ti
/ 1000000000;
1292 snprintf(buf1
, sizeof(buf1
),
1293 "[%02d:%02d:%02d.%03d] ",
1297 (int)((ti
/ 1000000) % 1000));
1298 d
->drv
->chr_write(d
->drv
, buf1
, strlen(buf1
));
1305 static char *mux_help
[] = {
1306 "% h print this help\n\r",
1307 "% x exit emulator\n\r",
1308 "% s save disk data back to file (if -snapshot)\n\r",
1309 "% t toggle console timestamps\n\r"
1310 "% b send break (magic sysrq)\n\r",
1311 "% c switch between console and monitor\n\r",
1316 static int term_escape_char
= 0x01; /* ctrl-a is used for escape */
1317 static void mux_print_help(CharDriverState
*chr
)
1320 char ebuf
[15] = "Escape-Char";
1321 char cbuf
[50] = "\n\r";
1323 if (term_escape_char
> 0 && term_escape_char
< 26) {
1324 sprintf(cbuf
,"\n\r");
1325 sprintf(ebuf
,"C-%c", term_escape_char
- 1 + 'a');
1327 sprintf(cbuf
,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char
);
1329 chr
->chr_write(chr
, cbuf
, strlen(cbuf
));
1330 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
1331 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
1332 if (mux_help
[i
][j
] == '%')
1333 chr
->chr_write(chr
, ebuf
, strlen(ebuf
));
1335 chr
->chr_write(chr
, &mux_help
[i
][j
], 1);
1340 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
1342 if (d
->term_got_escape
) {
1343 d
->term_got_escape
= 0;
1344 if (ch
== term_escape_char
)
1349 mux_print_help(chr
);
1353 char *term
= "QEMU: Terminated\n\r";
1354 chr
->chr_write(chr
,term
,strlen(term
));
1361 for (i
= 0; i
< MAX_DISKS
; i
++) {
1363 bdrv_commit(bs_table
[i
]);
1368 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1371 /* Switch to the next registered device */
1373 if (chr
->focus
>= d
->mux_cnt
)
1377 term_timestamps
= !term_timestamps
;
1378 term_timestamps_start
= -1;
1381 } else if (ch
== term_escape_char
) {
1382 d
->term_got_escape
= 1;
1390 static int mux_chr_can_read(void *opaque
)
1392 CharDriverState
*chr
= opaque
;
1393 MuxDriver
*d
= chr
->opaque
;
1394 if (d
->chr_can_read
[chr
->focus
])
1395 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
1399 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
1401 CharDriverState
*chr
= opaque
;
1402 MuxDriver
*d
= chr
->opaque
;
1404 for(i
= 0; i
< size
; i
++)
1405 if (mux_proc_byte(chr
, d
, buf
[i
]))
1406 d
->chr_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
], &buf
[i
], 1);
1409 static void mux_chr_event(void *opaque
, int event
)
1411 CharDriverState
*chr
= opaque
;
1412 MuxDriver
*d
= chr
->opaque
;
1415 /* Send the event to all registered listeners */
1416 for (i
= 0; i
< d
->mux_cnt
; i
++)
1417 if (d
->chr_event
[i
])
1418 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
1421 static void mux_chr_update_read_handler(CharDriverState
*chr
)
1423 MuxDriver
*d
= chr
->opaque
;
1425 if (d
->mux_cnt
>= MAX_MUX
) {
1426 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
1429 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
1430 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
1431 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
1432 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
1433 /* Fix up the real driver with mux routines */
1434 if (d
->mux_cnt
== 0) {
1435 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
1436 mux_chr_event
, chr
);
1438 chr
->focus
= d
->mux_cnt
;
1442 CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
1444 CharDriverState
*chr
;
1447 chr
= qemu_mallocz(sizeof(CharDriverState
));
1450 d
= qemu_mallocz(sizeof(MuxDriver
));
1459 chr
->chr_write
= mux_chr_write
;
1460 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
1467 static void socket_cleanup(void)
1472 static int socket_init(void)
1477 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1479 err
= WSAGetLastError();
1480 fprintf(stderr
, "WSAStartup: %d\n", err
);
1483 atexit(socket_cleanup
);
1487 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1493 ret
= send(fd
, buf
, len
, 0);
1496 errno
= WSAGetLastError();
1497 if (errno
!= WSAEWOULDBLOCK
) {
1500 } else if (ret
== 0) {
1510 void socket_set_nonblock(int fd
)
1512 unsigned long opt
= 1;
1513 ioctlsocket(fd
, FIONBIO
, &opt
);
1518 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1524 ret
= write(fd
, buf
, len
);
1526 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1528 } else if (ret
== 0) {
1538 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1540 return unix_write(fd
, buf
, len1
);
1543 void socket_set_nonblock(int fd
)
1545 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1547 #endif /* !_WIN32 */
1556 #define STDIO_MAX_CLIENTS 1
1557 static int stdio_nb_clients
= 0;
1559 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1561 FDCharDriver
*s
= chr
->opaque
;
1562 return unix_write(s
->fd_out
, buf
, len
);
1565 static int fd_chr_read_poll(void *opaque
)
1567 CharDriverState
*chr
= opaque
;
1568 FDCharDriver
*s
= chr
->opaque
;
1570 s
->max_size
= qemu_chr_can_read(chr
);
1574 static void fd_chr_read(void *opaque
)
1576 CharDriverState
*chr
= opaque
;
1577 FDCharDriver
*s
= chr
->opaque
;
1582 if (len
> s
->max_size
)
1586 size
= read(s
->fd_in
, buf
, len
);
1588 /* FD has been closed. Remove it from the active list. */
1589 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1593 qemu_chr_read(chr
, buf
, size
);
1597 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1599 FDCharDriver
*s
= chr
->opaque
;
1601 if (s
->fd_in
>= 0) {
1602 if (nographic
&& s
->fd_in
== 0) {
1604 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1605 fd_chr_read
, NULL
, chr
);
1610 /* open a character device to a unix fd */
1611 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1613 CharDriverState
*chr
;
1616 chr
= qemu_mallocz(sizeof(CharDriverState
));
1619 s
= qemu_mallocz(sizeof(FDCharDriver
));
1627 chr
->chr_write
= fd_chr_write
;
1628 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
1630 qemu_chr_reset(chr
);
1635 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1639 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1642 return qemu_chr_open_fd(-1, fd_out
);
1645 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1648 char filename_in
[256], filename_out
[256];
1650 snprintf(filename_in
, 256, "%s.in", filename
);
1651 snprintf(filename_out
, 256, "%s.out", filename
);
1652 fd_in
= open(filename_in
, O_RDWR
| O_BINARY
);
1653 fd_out
= open(filename_out
, O_RDWR
| O_BINARY
);
1654 if (fd_in
< 0 || fd_out
< 0) {
1659 fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
);
1663 return qemu_chr_open_fd(fd_in
, fd_out
);
1667 /* for STDIO, we handle the case where several clients use it
1670 #define TERM_FIFO_MAX_SIZE 1
1672 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1673 static int term_fifo_size
;
1675 static int stdio_read_poll(void *opaque
)
1677 CharDriverState
*chr
= opaque
;
1679 /* try to flush the queue if needed */
1680 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
1681 qemu_chr_read(chr
, term_fifo
, 1);
1684 /* see if we can absorb more chars */
1685 if (term_fifo_size
== 0)
1691 static void stdio_read(void *opaque
)
1695 CharDriverState
*chr
= opaque
;
1697 size
= read(0, buf
, 1);
1699 /* stdin has been closed. Remove it from the active list. */
1700 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
1704 if (qemu_chr_can_read(chr
) > 0) {
1705 qemu_chr_read(chr
, buf
, 1);
1706 } else if (term_fifo_size
== 0) {
1707 term_fifo
[term_fifo_size
++] = buf
[0];
1712 /* init terminal so that we can grab keys */
1713 static struct termios oldtty
;
1714 static int old_fd0_flags
;
1716 static void term_exit(void)
1718 tcsetattr (0, TCSANOW
, &oldtty
);
1719 fcntl(0, F_SETFL
, old_fd0_flags
);
1722 static void term_init(void)
1726 tcgetattr (0, &tty
);
1728 old_fd0_flags
= fcntl(0, F_GETFL
);
1730 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1731 |INLCR
|IGNCR
|ICRNL
|IXON
);
1732 tty
.c_oflag
|= OPOST
;
1733 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1734 /* if graphical mode, we allow Ctrl-C handling */
1736 tty
.c_lflag
&= ~ISIG
;
1737 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1740 tty
.c_cc
[VTIME
] = 0;
1742 tcsetattr (0, TCSANOW
, &tty
);
1746 fcntl(0, F_SETFL
, O_NONBLOCK
);
1749 static CharDriverState
*qemu_chr_open_stdio(void)
1751 CharDriverState
*chr
;
1753 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1755 chr
= qemu_chr_open_fd(0, 1);
1756 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
1763 #if defined(__linux__)
1764 static CharDriverState
*qemu_chr_open_pty(void)
1767 char slave_name
[1024];
1768 int master_fd
, slave_fd
;
1770 /* Not satisfying */
1771 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1775 /* Disabling local echo and line-buffered output */
1776 tcgetattr (master_fd
, &tty
);
1777 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1779 tty
.c_cc
[VTIME
] = 0;
1780 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1782 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1783 return qemu_chr_open_fd(master_fd
, master_fd
);
1786 static void tty_serial_init(int fd
, int speed
,
1787 int parity
, int data_bits
, int stop_bits
)
1793 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1794 speed
, parity
, data_bits
, stop_bits
);
1796 tcgetattr (fd
, &tty
);
1838 cfsetispeed(&tty
, spd
);
1839 cfsetospeed(&tty
, spd
);
1841 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1842 |INLCR
|IGNCR
|ICRNL
|IXON
);
1843 tty
.c_oflag
|= OPOST
;
1844 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1845 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1866 tty
.c_cflag
|= PARENB
;
1869 tty
.c_cflag
|= PARENB
| PARODD
;
1873 tty
.c_cflag
|= CSTOPB
;
1875 tcsetattr (fd
, TCSANOW
, &tty
);
1878 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1880 FDCharDriver
*s
= chr
->opaque
;
1883 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1885 QEMUSerialSetParams
*ssp
= arg
;
1886 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1887 ssp
->data_bits
, ssp
->stop_bits
);
1890 case CHR_IOCTL_SERIAL_SET_BREAK
:
1892 int enable
= *(int *)arg
;
1894 tcsendbreak(s
->fd_in
, 1);
1903 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1905 CharDriverState
*chr
;
1908 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1911 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1912 tty_serial_init(fd
, 115200, 'N', 8, 1);
1913 chr
= qemu_chr_open_fd(fd
, fd
);
1916 chr
->chr_ioctl
= tty_serial_ioctl
;
1917 qemu_chr_reset(chr
);
1924 } ParallelCharDriver
;
1926 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1928 if (s
->mode
!= mode
) {
1930 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1937 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1939 ParallelCharDriver
*drv
= chr
->opaque
;
1944 case CHR_IOCTL_PP_READ_DATA
:
1945 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1947 *(uint8_t *)arg
= b
;
1949 case CHR_IOCTL_PP_WRITE_DATA
:
1950 b
= *(uint8_t *)arg
;
1951 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1954 case CHR_IOCTL_PP_READ_CONTROL
:
1955 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1957 /* Linux gives only the lowest bits, and no way to know data
1958 direction! For better compatibility set the fixed upper
1960 *(uint8_t *)arg
= b
| 0xc0;
1962 case CHR_IOCTL_PP_WRITE_CONTROL
:
1963 b
= *(uint8_t *)arg
;
1964 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1967 case CHR_IOCTL_PP_READ_STATUS
:
1968 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1970 *(uint8_t *)arg
= b
;
1972 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1973 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1974 struct ParallelIOArg
*parg
= arg
;
1975 int n
= read(fd
, parg
->buffer
, parg
->count
);
1976 if (n
!= parg
->count
) {
1981 case CHR_IOCTL_PP_EPP_READ
:
1982 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1983 struct ParallelIOArg
*parg
= arg
;
1984 int n
= read(fd
, parg
->buffer
, parg
->count
);
1985 if (n
!= parg
->count
) {
1990 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1991 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1992 struct ParallelIOArg
*parg
= arg
;
1993 int n
= write(fd
, parg
->buffer
, parg
->count
);
1994 if (n
!= parg
->count
) {
1999 case CHR_IOCTL_PP_EPP_WRITE
:
2000 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2001 struct ParallelIOArg
*parg
= arg
;
2002 int n
= write(fd
, parg
->buffer
, parg
->count
);
2003 if (n
!= parg
->count
) {
2014 static void pp_close(CharDriverState
*chr
)
2016 ParallelCharDriver
*drv
= chr
->opaque
;
2019 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
2020 ioctl(fd
, PPRELEASE
);
2025 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
2027 CharDriverState
*chr
;
2028 ParallelCharDriver
*drv
;
2031 fd
= open(filename
, O_RDWR
);
2035 if (ioctl(fd
, PPCLAIM
) < 0) {
2040 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
2046 drv
->mode
= IEEE1284_MODE_COMPAT
;
2048 chr
= qemu_mallocz(sizeof(CharDriverState
));
2054 chr
->chr_write
= null_chr_write
;
2055 chr
->chr_ioctl
= pp_ioctl
;
2056 chr
->chr_close
= pp_close
;
2059 qemu_chr_reset(chr
);
2065 static CharDriverState
*qemu_chr_open_pty(void)
2071 #endif /* !defined(_WIN32) */
2076 HANDLE hcom
, hrecv
, hsend
;
2077 OVERLAPPED orecv
, osend
;
2082 #define NSENDBUF 2048
2083 #define NRECVBUF 2048
2084 #define MAXCONNECT 1
2085 #define NTIMEOUT 5000
2087 static int win_chr_poll(void *opaque
);
2088 static int win_chr_pipe_poll(void *opaque
);
2090 static void win_chr_close(CharDriverState
*chr
)
2092 WinCharState
*s
= chr
->opaque
;
2095 CloseHandle(s
->hsend
);
2099 CloseHandle(s
->hrecv
);
2103 CloseHandle(s
->hcom
);
2107 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
2109 qemu_del_polling_cb(win_chr_poll
, chr
);
2112 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
2114 WinCharState
*s
= chr
->opaque
;
2116 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2121 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2123 fprintf(stderr
, "Failed CreateEvent\n");
2126 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2128 fprintf(stderr
, "Failed CreateEvent\n");
2132 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2133 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2134 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2135 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
2140 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2141 fprintf(stderr
, "Failed SetupComm\n");
2145 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2146 size
= sizeof(COMMCONFIG
);
2147 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2148 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2149 CommConfigDialog(filename
, NULL
, &comcfg
);
2151 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2152 fprintf(stderr
, "Failed SetCommState\n");
2156 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2157 fprintf(stderr
, "Failed SetCommMask\n");
2161 cto
.ReadIntervalTimeout
= MAXDWORD
;
2162 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2163 fprintf(stderr
, "Failed SetCommTimeouts\n");
2167 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2168 fprintf(stderr
, "Failed ClearCommError\n");
2171 qemu_add_polling_cb(win_chr_poll
, chr
);
2179 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2181 WinCharState
*s
= chr
->opaque
;
2182 DWORD len
, ret
, size
, err
;
2185 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2186 s
->osend
.hEvent
= s
->hsend
;
2189 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2191 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2193 err
= GetLastError();
2194 if (err
== ERROR_IO_PENDING
) {
2195 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2213 static int win_chr_read_poll(CharDriverState
*chr
)
2215 WinCharState
*s
= chr
->opaque
;
2217 s
->max_size
= qemu_chr_can_read(chr
);
2221 static void win_chr_readfile(CharDriverState
*chr
)
2223 WinCharState
*s
= chr
->opaque
;
2228 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2229 s
->orecv
.hEvent
= s
->hrecv
;
2230 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2232 err
= GetLastError();
2233 if (err
== ERROR_IO_PENDING
) {
2234 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2239 qemu_chr_read(chr
, buf
, size
);
2243 static void win_chr_read(CharDriverState
*chr
)
2245 WinCharState
*s
= chr
->opaque
;
2247 if (s
->len
> s
->max_size
)
2248 s
->len
= s
->max_size
;
2252 win_chr_readfile(chr
);
2255 static int win_chr_poll(void *opaque
)
2257 CharDriverState
*chr
= opaque
;
2258 WinCharState
*s
= chr
->opaque
;
2262 ClearCommError(s
->hcom
, &comerr
, &status
);
2263 if (status
.cbInQue
> 0) {
2264 s
->len
= status
.cbInQue
;
2265 win_chr_read_poll(chr
);
2272 static CharDriverState
*qemu_chr_open_win(const char *filename
)
2274 CharDriverState
*chr
;
2277 chr
= qemu_mallocz(sizeof(CharDriverState
));
2280 s
= qemu_mallocz(sizeof(WinCharState
));
2286 chr
->chr_write
= win_chr_write
;
2287 chr
->chr_close
= win_chr_close
;
2289 if (win_chr_init(chr
, filename
) < 0) {
2294 qemu_chr_reset(chr
);
2298 static int win_chr_pipe_poll(void *opaque
)
2300 CharDriverState
*chr
= opaque
;
2301 WinCharState
*s
= chr
->opaque
;
2304 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2307 win_chr_read_poll(chr
);
2314 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
2316 WinCharState
*s
= chr
->opaque
;
2324 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2326 fprintf(stderr
, "Failed CreateEvent\n");
2329 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2331 fprintf(stderr
, "Failed CreateEvent\n");
2335 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2336 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2337 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2339 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2340 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2341 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2346 ZeroMemory(&ov
, sizeof(ov
));
2347 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2348 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2350 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2354 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2356 fprintf(stderr
, "Failed GetOverlappedResult\n");
2358 CloseHandle(ov
.hEvent
);
2365 CloseHandle(ov
.hEvent
);
2368 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2377 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2379 CharDriverState
*chr
;
2382 chr
= qemu_mallocz(sizeof(CharDriverState
));
2385 s
= qemu_mallocz(sizeof(WinCharState
));
2391 chr
->chr_write
= win_chr_write
;
2392 chr
->chr_close
= win_chr_close
;
2394 if (win_chr_pipe_init(chr
, filename
) < 0) {
2399 qemu_chr_reset(chr
);
2403 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2405 CharDriverState
*chr
;
2408 chr
= qemu_mallocz(sizeof(CharDriverState
));
2411 s
= qemu_mallocz(sizeof(WinCharState
));
2418 chr
->chr_write
= win_chr_write
;
2419 qemu_chr_reset(chr
);
2423 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
2425 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2428 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2432 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2433 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2434 if (fd_out
== INVALID_HANDLE_VALUE
)
2437 return qemu_chr_open_win_file(fd_out
);
2441 /***********************************************************/
2442 /* UDP Net console */
2446 struct sockaddr_in daddr
;
2453 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2455 NetCharDriver
*s
= chr
->opaque
;
2457 return sendto(s
->fd
, buf
, len
, 0,
2458 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2461 static int udp_chr_read_poll(void *opaque
)
2463 CharDriverState
*chr
= opaque
;
2464 NetCharDriver
*s
= chr
->opaque
;
2466 s
->max_size
= qemu_chr_can_read(chr
);
2468 /* If there were any stray characters in the queue process them
2471 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2472 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2474 s
->max_size
= qemu_chr_can_read(chr
);
2479 static void udp_chr_read(void *opaque
)
2481 CharDriverState
*chr
= opaque
;
2482 NetCharDriver
*s
= chr
->opaque
;
2484 if (s
->max_size
== 0)
2486 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2487 s
->bufptr
= s
->bufcnt
;
2492 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2493 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2495 s
->max_size
= qemu_chr_can_read(chr
);
2499 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2501 NetCharDriver
*s
= chr
->opaque
;
2504 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2505 udp_chr_read
, NULL
, chr
);
2509 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2511 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2513 int parse_host_src_port(struct sockaddr_in
*haddr
,
2514 struct sockaddr_in
*saddr
,
2517 static CharDriverState
*qemu_chr_open_udp(const char *def
)
2519 CharDriverState
*chr
= NULL
;
2520 NetCharDriver
*s
= NULL
;
2522 struct sockaddr_in saddr
;
2524 chr
= qemu_mallocz(sizeof(CharDriverState
));
2527 s
= qemu_mallocz(sizeof(NetCharDriver
));
2531 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2533 perror("socket(PF_INET, SOCK_DGRAM)");
2537 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2538 printf("Could not parse: %s\n", def
);
2542 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2552 chr
->chr_write
= udp_chr_write
;
2553 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2566 /***********************************************************/
2567 /* TCP Net console */
2578 static void tcp_chr_accept(void *opaque
);
2580 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2582 TCPCharDriver
*s
= chr
->opaque
;
2584 return send_all(s
->fd
, buf
, len
);
2586 /* XXX: indicate an error ? */
2591 static int tcp_chr_read_poll(void *opaque
)
2593 CharDriverState
*chr
= opaque
;
2594 TCPCharDriver
*s
= chr
->opaque
;
2597 s
->max_size
= qemu_chr_can_read(chr
);
2602 #define IAC_BREAK 243
2603 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2605 char *buf
, int *size
)
2607 /* Handle any telnet client's basic IAC options to satisfy char by
2608 * char mode with no echo. All IAC options will be removed from
2609 * the buf and the do_telnetopt variable will be used to track the
2610 * state of the width of the IAC information.
2612 * IAC commands come in sets of 3 bytes with the exception of the
2613 * "IAC BREAK" command and the double IAC.
2619 for (i
= 0; i
< *size
; i
++) {
2620 if (s
->do_telnetopt
> 1) {
2621 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2622 /* Double IAC means send an IAC */
2626 s
->do_telnetopt
= 1;
2628 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2629 /* Handle IAC break commands by sending a serial break */
2630 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
2635 if (s
->do_telnetopt
>= 4) {
2636 s
->do_telnetopt
= 1;
2639 if ((unsigned char)buf
[i
] == IAC
) {
2640 s
->do_telnetopt
= 2;
2651 static void tcp_chr_read(void *opaque
)
2653 CharDriverState
*chr
= opaque
;
2654 TCPCharDriver
*s
= chr
->opaque
;
2658 if (!s
->connected
|| s
->max_size
<= 0)
2661 if (len
> s
->max_size
)
2663 size
= recv(s
->fd
, buf
, len
, 0);
2665 /* connection closed */
2667 if (s
->listen_fd
>= 0) {
2668 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2670 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2673 } else if (size
> 0) {
2674 if (s
->do_telnetopt
)
2675 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2677 qemu_chr_read(chr
, buf
, size
);
2681 static void tcp_chr_connect(void *opaque
)
2683 CharDriverState
*chr
= opaque
;
2684 TCPCharDriver
*s
= chr
->opaque
;
2687 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2688 tcp_chr_read
, NULL
, chr
);
2689 qemu_chr_reset(chr
);
2692 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2693 static void tcp_chr_telnet_init(int fd
)
2696 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2697 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2698 send(fd
, (char *)buf
, 3, 0);
2699 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2700 send(fd
, (char *)buf
, 3, 0);
2701 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2702 send(fd
, (char *)buf
, 3, 0);
2703 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2704 send(fd
, (char *)buf
, 3, 0);
2707 static void socket_set_nodelay(int fd
)
2710 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2713 static void tcp_chr_accept(void *opaque
)
2715 CharDriverState
*chr
= opaque
;
2716 TCPCharDriver
*s
= chr
->opaque
;
2717 struct sockaddr_in saddr
;
2719 struct sockaddr_un uaddr
;
2721 struct sockaddr
*addr
;
2728 len
= sizeof(uaddr
);
2729 addr
= (struct sockaddr
*)&uaddr
;
2733 len
= sizeof(saddr
);
2734 addr
= (struct sockaddr
*)&saddr
;
2736 fd
= accept(s
->listen_fd
, addr
, &len
);
2737 if (fd
< 0 && errno
!= EINTR
) {
2739 } else if (fd
>= 0) {
2740 if (s
->do_telnetopt
)
2741 tcp_chr_telnet_init(fd
);
2745 socket_set_nonblock(fd
);
2747 socket_set_nodelay(fd
);
2749 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2750 tcp_chr_connect(chr
);
2753 static void tcp_chr_close(CharDriverState
*chr
)
2755 TCPCharDriver
*s
= chr
->opaque
;
2758 if (s
->listen_fd
>= 0)
2759 closesocket(s
->listen_fd
);
2763 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2767 CharDriverState
*chr
= NULL
;
2768 TCPCharDriver
*s
= NULL
;
2769 int fd
= -1, ret
, err
, val
;
2771 int is_waitconnect
= 1;
2774 struct sockaddr_in saddr
;
2776 struct sockaddr_un uaddr
;
2778 struct sockaddr
*addr
;
2783 addr
= (struct sockaddr
*)&uaddr
;
2784 addrlen
= sizeof(uaddr
);
2785 if (parse_unix_path(&uaddr
, host_str
) < 0)
2790 addr
= (struct sockaddr
*)&saddr
;
2791 addrlen
= sizeof(saddr
);
2792 if (parse_host_port(&saddr
, host_str
) < 0)
2797 while((ptr
= strchr(ptr
,','))) {
2799 if (!strncmp(ptr
,"server",6)) {
2801 } else if (!strncmp(ptr
,"nowait",6)) {
2803 } else if (!strncmp(ptr
,"nodelay",6)) {
2806 printf("Unknown option: %s\n", ptr
);
2813 chr
= qemu_mallocz(sizeof(CharDriverState
));
2816 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2822 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2825 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2830 if (!is_waitconnect
)
2831 socket_set_nonblock(fd
);
2836 s
->is_unix
= is_unix
;
2837 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2840 chr
->chr_write
= tcp_chr_write
;
2841 chr
->chr_close
= tcp_chr_close
;
2844 /* allow fast reuse */
2848 strncpy(path
, uaddr
.sun_path
, 108);
2855 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2858 ret
= bind(fd
, addr
, addrlen
);
2862 ret
= listen(fd
, 0);
2867 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2869 s
->do_telnetopt
= 1;
2872 ret
= connect(fd
, addr
, addrlen
);
2874 err
= socket_error();
2875 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2876 } else if (err
== EINPROGRESS
) {
2879 } else if (err
== WSAEALREADY
) {
2891 socket_set_nodelay(fd
);
2893 tcp_chr_connect(chr
);
2895 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2898 if (is_listen
&& is_waitconnect
) {
2899 printf("QEMU waiting for connection on: %s\n", host_str
);
2900 tcp_chr_accept(chr
);
2901 socket_set_nonblock(s
->listen_fd
);
2913 CharDriverState
*qemu_chr_open(const char *filename
)
2917 if (!strcmp(filename
, "vc")) {
2918 return text_console_init(&display_state
);
2919 } else if (!strcmp(filename
, "null")) {
2920 return qemu_chr_open_null();
2922 if (strstart(filename
, "tcp:", &p
)) {
2923 return qemu_chr_open_tcp(p
, 0, 0);
2925 if (strstart(filename
, "telnet:", &p
)) {
2926 return qemu_chr_open_tcp(p
, 1, 0);
2928 if (strstart(filename
, "udp:", &p
)) {
2929 return qemu_chr_open_udp(p
);
2931 if (strstart(filename
, "mon:", &p
)) {
2932 CharDriverState
*drv
= qemu_chr_open(p
);
2934 drv
= qemu_chr_open_mux(drv
);
2935 monitor_init(drv
, !nographic
);
2938 printf("Unable to open driver: %s\n", p
);
2942 if (strstart(filename
, "unix:", &p
)) {
2943 return qemu_chr_open_tcp(p
, 0, 1);
2944 } else if (strstart(filename
, "file:", &p
)) {
2945 return qemu_chr_open_file_out(p
);
2946 } else if (strstart(filename
, "pipe:", &p
)) {
2947 return qemu_chr_open_pipe(p
);
2948 } else if (!strcmp(filename
, "pty")) {
2949 return qemu_chr_open_pty();
2950 } else if (!strcmp(filename
, "stdio")) {
2951 return qemu_chr_open_stdio();
2954 #if defined(__linux__)
2955 if (strstart(filename
, "/dev/parport", NULL
)) {
2956 return qemu_chr_open_pp(filename
);
2958 if (strstart(filename
, "/dev/", NULL
)) {
2959 return qemu_chr_open_tty(filename
);
2963 if (strstart(filename
, "COM", NULL
)) {
2964 return qemu_chr_open_win(filename
);
2966 if (strstart(filename
, "pipe:", &p
)) {
2967 return qemu_chr_open_win_pipe(p
);
2969 if (strstart(filename
, "con:", NULL
)) {
2970 return qemu_chr_open_win_con(filename
);
2972 if (strstart(filename
, "file:", &p
)) {
2973 return qemu_chr_open_win_file_out(p
);
2981 void qemu_chr_close(CharDriverState
*chr
)
2984 chr
->chr_close(chr
);
2987 /***********************************************************/
2988 /* network device redirectors */
2990 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2994 for(i
=0;i
<size
;i
+=16) {
2998 fprintf(f
, "%08x ", i
);
3001 fprintf(f
, " %02x", buf
[i
+j
]);
3006 for(j
=0;j
<len
;j
++) {
3008 if (c
< ' ' || c
> '~')
3010 fprintf(f
, "%c", c
);
3016 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
3019 for(i
= 0; i
< 6; i
++) {
3020 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3033 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
3038 p1
= strchr(p
, sep
);
3044 if (len
> buf_size
- 1)
3046 memcpy(buf
, p
, len
);
3053 int parse_host_src_port(struct sockaddr_in
*haddr
,
3054 struct sockaddr_in
*saddr
,
3055 const char *input_str
)
3057 char *str
= strdup(input_str
);
3058 char *host_str
= str
;
3063 * Chop off any extra arguments at the end of the string which
3064 * would start with a comma, then fill in the src port information
3065 * if it was provided else use the "any address" and "any port".
3067 if ((ptr
= strchr(str
,',')))
3070 if ((src_str
= strchr(input_str
,'@'))) {
3075 if (parse_host_port(haddr
, host_str
) < 0)
3078 if (!src_str
|| *src_str
== '\0')
3081 if (parse_host_port(saddr
, src_str
) < 0)
3092 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
3100 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3102 saddr
->sin_family
= AF_INET
;
3103 if (buf
[0] == '\0') {
3104 saddr
->sin_addr
.s_addr
= 0;
3106 if (isdigit(buf
[0])) {
3107 if (!inet_aton(buf
, &saddr
->sin_addr
))
3110 if ((he
= gethostbyname(buf
)) == NULL
)
3112 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
3115 port
= strtol(p
, (char **)&r
, 0);
3118 saddr
->sin_port
= htons(port
);
3123 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
3128 len
= MIN(108, strlen(str
));
3129 p
= strchr(str
, ',');
3131 len
= MIN(len
, p
- str
);
3133 memset(uaddr
, 0, sizeof(*uaddr
));
3135 uaddr
->sun_family
= AF_UNIX
;
3136 memcpy(uaddr
->sun_path
, str
, len
);
3142 /* find or alloc a new VLAN */
3143 VLANState
*qemu_find_vlan(int id
)
3145 VLANState
**pvlan
, *vlan
;
3146 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3150 vlan
= qemu_mallocz(sizeof(VLANState
));
3155 pvlan
= &first_vlan
;
3156 while (*pvlan
!= NULL
)
3157 pvlan
= &(*pvlan
)->next
;
3162 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
3163 IOReadHandler
*fd_read
,
3164 IOCanRWHandler
*fd_can_read
,
3167 VLANClientState
*vc
, **pvc
;
3168 vc
= qemu_mallocz(sizeof(VLANClientState
));
3171 vc
->fd_read
= fd_read
;
3172 vc
->fd_can_read
= fd_can_read
;
3173 vc
->opaque
= opaque
;
3177 pvc
= &vlan
->first_client
;
3178 while (*pvc
!= NULL
)
3179 pvc
= &(*pvc
)->next
;
3184 int qemu_can_send_packet(VLANClientState
*vc1
)
3186 VLANState
*vlan
= vc1
->vlan
;
3187 VLANClientState
*vc
;
3189 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3191 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
3198 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
3200 VLANState
*vlan
= vc1
->vlan
;
3201 VLANClientState
*vc
;
3204 printf("vlan %d send:\n", vlan
->id
);
3205 hex_dump(stdout
, buf
, size
);
3207 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3209 vc
->fd_read(vc
->opaque
, buf
, size
);
3214 #if defined(CONFIG_SLIRP)
3216 /* slirp network adapter */
3218 static int slirp_inited
;
3219 static VLANClientState
*slirp_vc
;
3221 int slirp_can_output(void)
3223 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
3226 void slirp_output(const uint8_t *pkt
, int pkt_len
)
3229 printf("slirp output:\n");
3230 hex_dump(stdout
, pkt
, pkt_len
);
3234 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
3237 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
3240 printf("slirp input:\n");
3241 hex_dump(stdout
, buf
, size
);
3243 slirp_input(buf
, size
);
3246 static int net_slirp_init(VLANState
*vlan
)
3248 if (!slirp_inited
) {
3252 slirp_vc
= qemu_new_vlan_client(vlan
,
3253 slirp_receive
, NULL
, NULL
);
3254 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
3258 static void net_slirp_redir(const char *redir_str
)
3263 struct in_addr guest_addr
;
3264 int host_port
, guest_port
;
3266 if (!slirp_inited
) {
3272 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3274 if (!strcmp(buf
, "tcp")) {
3276 } else if (!strcmp(buf
, "udp")) {
3282 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3284 host_port
= strtol(buf
, &r
, 0);
3288 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3290 if (buf
[0] == '\0') {
3291 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
3293 if (!inet_aton(buf
, &guest_addr
))
3296 guest_port
= strtol(p
, &r
, 0);
3300 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3301 fprintf(stderr
, "qemu: could not set up redirection\n");
3306 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3314 static void smb_exit(void)
3318 char filename
[1024];
3320 /* erase all the files in the directory */
3321 d
= opendir(smb_dir
);
3326 if (strcmp(de
->d_name
, ".") != 0 &&
3327 strcmp(de
->d_name
, "..") != 0) {
3328 snprintf(filename
, sizeof(filename
), "%s/%s",
3329 smb_dir
, de
->d_name
);
3337 /* automatic user mode samba server configuration */
3338 void net_slirp_smb(const char *exported_dir
)
3340 char smb_conf
[1024];
3341 char smb_cmdline
[1024];
3344 if (!slirp_inited
) {
3349 /* XXX: better tmp dir construction */
3350 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3351 if (mkdir(smb_dir
, 0700) < 0) {
3352 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3355 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3357 f
= fopen(smb_conf
, "w");
3359 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3366 "socket address=127.0.0.1\n"
3367 "pid directory=%s\n"
3368 "lock directory=%s\n"
3369 "log file=%s/log.smbd\n"
3370 "smb passwd file=%s/smbpasswd\n"
3371 "security = share\n"
3386 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3387 SMBD_COMMAND
, smb_conf
);
3389 slirp_add_exec(0, smb_cmdline
, 4, 139);
3392 #endif /* !defined(_WIN32) */
3394 #endif /* CONFIG_SLIRP */
3396 #if !defined(_WIN32)
3398 typedef struct TAPState
{
3399 VLANClientState
*vc
;
3403 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3405 TAPState
*s
= opaque
;
3408 ret
= write(s
->fd
, buf
, size
);
3409 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3416 static void tap_send(void *opaque
)
3418 TAPState
*s
= opaque
;
3425 sbuf
.maxlen
= sizeof(buf
);
3427 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
3429 size
= read(s
->fd
, buf
, sizeof(buf
));
3432 qemu_send_packet(s
->vc
, buf
, size
);
3438 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3442 s
= qemu_mallocz(sizeof(TAPState
));
3446 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3447 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3448 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3453 static int tap_open(char *ifname
, int ifname_size
)
3459 fd
= open("/dev/tap", O_RDWR
);
3461 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3466 dev
= devname(s
.st_rdev
, S_IFCHR
);
3467 pstrcpy(ifname
, ifname_size
, dev
);
3469 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3472 #elif defined(__sun__)
3473 #define TUNNEWPPA (('T'<<16) | 0x0001)
3475 * Allocate TAP device, returns opened fd.
3476 * Stores dev name in the first arg(must be large enough).
3478 int tap_alloc(char *dev
)
3480 int tap_fd
, if_fd
, ppa
= -1;
3481 static int ip_fd
= 0;
3484 static int arp_fd
= 0;
3485 int ip_muxid
, arp_muxid
;
3486 struct strioctl strioc_if
, strioc_ppa
;
3487 int link_type
= I_PLINK
;;
3489 char actual_name
[32] = "";
3491 memset(&ifr
, 0x0, sizeof(ifr
));
3495 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
3499 /* Check if IP device was opened */
3503 if( (ip_fd
= open("/dev/udp", O_RDWR
, 0)) < 0){
3504 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
3508 if( (tap_fd
= open("/dev/tap", O_RDWR
, 0)) < 0){
3509 syslog(LOG_ERR
, "Can't open /dev/tap");
3513 /* Assign a new PPA and get its unit number. */
3514 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
3515 strioc_ppa
.ic_timout
= 0;
3516 strioc_ppa
.ic_len
= sizeof(ppa
);
3517 strioc_ppa
.ic_dp
= (char *)&ppa
;
3518 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
3519 syslog (LOG_ERR
, "Can't assign new interface");
3521 if( (if_fd
= open("/dev/tap", O_RDWR
, 0)) < 0){
3522 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
3525 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
3526 syslog(LOG_ERR
, "Can't push IP module");
3530 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
3531 syslog(LOG_ERR
, "Can't get flags\n");
3533 snprintf (actual_name
, 32, "tap%d", ppa
);
3534 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3537 /* Assign ppa according to the unit number returned by tun device */
3539 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
3540 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
3541 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
3542 syslog (LOG_ERR
, "Can't get flags\n");
3543 /* Push arp module to if_fd */
3544 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
3545 syslog (LOG_ERR
, "Can't push ARP module (2)");
3547 /* Push arp module to ip_fd */
3548 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
3549 syslog (LOG_ERR
, "I_POP failed\n");
3550 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
3551 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
3553 if ((arp_fd
= open ("/dev/tap", O_RDWR
, 0)) < 0)
3554 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
3556 /* Set ifname to arp */
3557 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
3558 strioc_if
.ic_timout
= 0;
3559 strioc_if
.ic_len
= sizeof(ifr
);
3560 strioc_if
.ic_dp
= (char *)&ifr
;
3561 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
3562 syslog (LOG_ERR
, "Can't set ifname to arp\n");
3565 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
3566 syslog(LOG_ERR
, "Can't link TAP device to IP");
3570 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
3571 syslog (LOG_ERR
, "Can't link TAP device to ARP");
3575 memset(&ifr
, 0x0, sizeof(ifr
));
3576 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3577 ifr
.lifr_ip_muxid
= ip_muxid
;
3578 ifr
.lifr_arp_muxid
= arp_muxid
;
3580 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
3582 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
3583 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
3584 syslog (LOG_ERR
, "Can't set multiplexor id");
3587 sprintf(dev
, "tap%d", ppa
);
3591 static int tap_open(char *ifname
, int ifname_size
)
3595 if( (fd
= tap_alloc(dev
)) < 0 ){
3596 fprintf(stderr
, "Cannot allocate TAP device\n");
3599 pstrcpy(ifname
, ifname_size
, dev
);
3600 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3604 static int tap_open(char *ifname
, int ifname_size
)
3609 fd
= open("/dev/net/tun", O_RDWR
);
3611 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
3614 memset(&ifr
, 0, sizeof(ifr
));
3615 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
3616 if (ifname
[0] != '\0')
3617 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
3619 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
3620 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
3622 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
3626 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
3627 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3632 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
3633 const char *setup_script
)
3636 int pid
, status
, fd
;
3641 if (ifname1
!= NULL
)
3642 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
3645 fd
= tap_open(ifname
, sizeof(ifname
));
3649 if (!setup_script
|| !strcmp(setup_script
, "no"))
3651 if (setup_script
[0] != '\0') {
3652 /* try to launch network init script */
3656 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
3657 for (i
= 0; i
< open_max
; i
++)
3658 if (i
!= STDIN_FILENO
&&
3659 i
!= STDOUT_FILENO
&&
3660 i
!= STDERR_FILENO
&&
3665 *parg
++ = (char *)setup_script
;
3668 execv(setup_script
, args
);
3671 while (waitpid(pid
, &status
, 0) != pid
);
3672 if (!WIFEXITED(status
) ||
3673 WEXITSTATUS(status
) != 0) {
3674 fprintf(stderr
, "%s: could not launch network script\n",
3680 s
= net_tap_fd_init(vlan
, fd
);
3683 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3684 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
3688 #endif /* !_WIN32 */
3690 /* network connection */
3691 typedef struct NetSocketState
{
3692 VLANClientState
*vc
;
3694 int state
; /* 0 = getting length, 1 = getting data */
3698 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
3701 typedef struct NetSocketListenState
{
3704 } NetSocketListenState
;
3706 /* XXX: we consider we can send the whole packet without blocking */
3707 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
3709 NetSocketState
*s
= opaque
;
3713 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
3714 send_all(s
->fd
, buf
, size
);
3717 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
3719 NetSocketState
*s
= opaque
;
3720 sendto(s
->fd
, buf
, size
, 0,
3721 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
3724 static void net_socket_send(void *opaque
)
3726 NetSocketState
*s
= opaque
;
3731 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
3733 err
= socket_error();
3734 if (err
!= EWOULDBLOCK
)
3736 } else if (size
== 0) {
3737 /* end of connection */
3739 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3745 /* reassemble a packet from the network */
3751 memcpy(s
->buf
+ s
->index
, buf
, l
);
3755 if (s
->index
== 4) {
3757 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
3763 l
= s
->packet_len
- s
->index
;
3766 memcpy(s
->buf
+ s
->index
, buf
, l
);
3770 if (s
->index
>= s
->packet_len
) {
3771 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
3780 static void net_socket_send_dgram(void *opaque
)
3782 NetSocketState
*s
= opaque
;
3785 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
3789 /* end of connection */
3790 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3793 qemu_send_packet(s
->vc
, s
->buf
, size
);
3796 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
3801 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
3802 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
3803 inet_ntoa(mcastaddr
->sin_addr
),
3804 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
3808 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
3810 perror("socket(PF_INET, SOCK_DGRAM)");
3815 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
3816 (const char *)&val
, sizeof(val
));
3818 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
3822 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
3828 /* Add host to multicast group */
3829 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
3830 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
3832 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
3833 (const char *)&imr
, sizeof(struct ip_mreq
));
3835 perror("setsockopt(IP_ADD_MEMBERSHIP)");
3839 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
3841 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
3842 (const char *)&val
, sizeof(val
));
3844 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
3848 socket_set_nonblock(fd
);
3856 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
3859 struct sockaddr_in saddr
;
3861 socklen_t saddr_len
;
3864 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
3865 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
3866 * by ONLY ONE process: we must "clone" this dgram socket --jjo
3870 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
3872 if (saddr
.sin_addr
.s_addr
==0) {
3873 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
3877 /* clone dgram socket */
3878 newfd
= net_socket_mcast_create(&saddr
);
3880 /* error already reported by net_socket_mcast_create() */
3884 /* clone newfd to fd, close newfd */
3889 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
3890 fd
, strerror(errno
));
3895 s
= qemu_mallocz(sizeof(NetSocketState
));
3900 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
3901 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
3903 /* mcast: save bound address as dst */
3904 if (is_connected
) s
->dgram_dst
=saddr
;
3906 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3907 "socket: fd=%d (%s mcast=%s:%d)",
3908 fd
, is_connected
? "cloned" : "",
3909 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3913 static void net_socket_connect(void *opaque
)
3915 NetSocketState
*s
= opaque
;
3916 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
3919 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
3923 s
= qemu_mallocz(sizeof(NetSocketState
));
3927 s
->vc
= qemu_new_vlan_client(vlan
,
3928 net_socket_receive
, NULL
, s
);
3929 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3930 "socket: fd=%d", fd
);
3932 net_socket_connect(s
);
3934 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
3939 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
3942 int so_type
=-1, optlen
=sizeof(so_type
);
3944 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
3945 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
3950 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
3952 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3954 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
3955 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
3956 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3961 static void net_socket_accept(void *opaque
)
3963 NetSocketListenState
*s
= opaque
;
3965 struct sockaddr_in saddr
;
3970 len
= sizeof(saddr
);
3971 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
3972 if (fd
< 0 && errno
!= EINTR
) {
3974 } else if (fd
>= 0) {
3978 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
3982 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
3983 "socket: connection from %s:%d",
3984 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3988 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
3990 NetSocketListenState
*s
;
3992 struct sockaddr_in saddr
;
3994 if (parse_host_port(&saddr
, host_str
) < 0)
3997 s
= qemu_mallocz(sizeof(NetSocketListenState
));
4001 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4006 socket_set_nonblock(fd
);
4008 /* allow fast reuse */
4010 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
4012 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4017 ret
= listen(fd
, 0);
4024 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
4028 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
4031 int fd
, connected
, ret
, err
;
4032 struct sockaddr_in saddr
;
4034 if (parse_host_port(&saddr
, host_str
) < 0)
4037 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4042 socket_set_nonblock(fd
);
4046 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4048 err
= socket_error();
4049 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
4050 } else if (err
== EINPROGRESS
) {
4053 } else if (err
== WSAEALREADY
) {
4066 s
= net_socket_fd_init(vlan
, fd
, connected
);
4069 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4070 "socket: connect to %s:%d",
4071 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4075 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
4079 struct sockaddr_in saddr
;
4081 if (parse_host_port(&saddr
, host_str
) < 0)
4085 fd
= net_socket_mcast_create(&saddr
);
4089 s
= net_socket_fd_init(vlan
, fd
, 0);
4093 s
->dgram_dst
= saddr
;
4095 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4096 "socket: mcast=%s:%d",
4097 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4102 static int get_param_value(char *buf
, int buf_size
,
4103 const char *tag
, const char *str
)
4112 while (*p
!= '\0' && *p
!= '=') {
4113 if ((q
- option
) < sizeof(option
) - 1)
4121 if (!strcmp(tag
, option
)) {
4123 while (*p
!= '\0' && *p
!= ',') {
4124 if ((q
- buf
) < buf_size
- 1)
4131 while (*p
!= '\0' && *p
!= ',') {
4142 static int net_client_init(const char *str
)
4153 while (*p
!= '\0' && *p
!= ',') {
4154 if ((q
- device
) < sizeof(device
) - 1)
4162 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
4163 vlan_id
= strtol(buf
, NULL
, 0);
4165 vlan
= qemu_find_vlan(vlan_id
);
4167 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
4170 if (!strcmp(device
, "nic")) {
4174 if (nb_nics
>= MAX_NICS
) {
4175 fprintf(stderr
, "Too Many NICs\n");
4178 nd
= &nd_table
[nb_nics
];
4179 macaddr
= nd
->macaddr
;
4185 macaddr
[5] = 0x56 + nb_nics
;
4187 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
4188 if (parse_macaddr(macaddr
, buf
) < 0) {
4189 fprintf(stderr
, "invalid syntax for ethernet address\n");
4193 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
4194 nd
->model
= strdup(buf
);
4200 if (!strcmp(device
, "none")) {
4201 /* does nothing. It is needed to signal that no network cards
4206 if (!strcmp(device
, "user")) {
4207 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
4208 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
4210 ret
= net_slirp_init(vlan
);
4214 if (!strcmp(device
, "tap")) {
4216 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4217 fprintf(stderr
, "tap: no interface name\n");
4220 ret
= tap_win32_init(vlan
, ifname
);
4223 if (!strcmp(device
, "tap")) {
4225 char setup_script
[1024];
4227 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4228 fd
= strtol(buf
, NULL
, 0);
4230 if (net_tap_fd_init(vlan
, fd
))
4233 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4236 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
4237 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
4239 ret
= net_tap_init(vlan
, ifname
, setup_script
);
4243 if (!strcmp(device
, "socket")) {
4244 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4246 fd
= strtol(buf
, NULL
, 0);
4248 if (net_socket_fd_init(vlan
, fd
, 1))
4250 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
4251 ret
= net_socket_listen_init(vlan
, buf
);
4252 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
4253 ret
= net_socket_connect_init(vlan
, buf
);
4254 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
4255 ret
= net_socket_mcast_init(vlan
, buf
);
4257 fprintf(stderr
, "Unknown socket options: %s\n", p
);
4262 fprintf(stderr
, "Unknown network device: %s\n", device
);
4266 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
4272 void do_info_network(void)
4275 VLANClientState
*vc
;
4277 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
4278 term_printf("VLAN %d devices:\n", vlan
->id
);
4279 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
4280 term_printf(" %s\n", vc
->info_str
);
4284 /***********************************************************/
4287 static USBPort
*used_usb_ports
;
4288 static USBPort
*free_usb_ports
;
4290 /* ??? Maybe change this to register a hub to keep track of the topology. */
4291 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
4292 usb_attachfn attach
)
4294 port
->opaque
= opaque
;
4295 port
->index
= index
;
4296 port
->attach
= attach
;
4297 port
->next
= free_usb_ports
;
4298 free_usb_ports
= port
;
4301 static int usb_device_add(const char *devname
)
4307 if (!free_usb_ports
)
4310 if (strstart(devname
, "host:", &p
)) {
4311 dev
= usb_host_device_open(p
);
4312 } else if (!strcmp(devname
, "mouse")) {
4313 dev
= usb_mouse_init();
4314 } else if (!strcmp(devname
, "tablet")) {
4315 dev
= usb_tablet_init();
4316 } else if (strstart(devname
, "disk:", &p
)) {
4317 dev
= usb_msd_init(p
);
4324 /* Find a USB port to add the device to. */
4325 port
= free_usb_ports
;
4329 /* Create a new hub and chain it on. */
4330 free_usb_ports
= NULL
;
4331 port
->next
= used_usb_ports
;
4332 used_usb_ports
= port
;
4334 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
4335 usb_attach(port
, hub
);
4336 port
= free_usb_ports
;
4339 free_usb_ports
= port
->next
;
4340 port
->next
= used_usb_ports
;
4341 used_usb_ports
= port
;
4342 usb_attach(port
, dev
);
4346 static int usb_device_del(const char *devname
)
4354 if (!used_usb_ports
)
4357 p
= strchr(devname
, '.');
4360 bus_num
= strtoul(devname
, NULL
, 0);
4361 addr
= strtoul(p
+ 1, NULL
, 0);
4365 lastp
= &used_usb_ports
;
4366 port
= used_usb_ports
;
4367 while (port
&& port
->dev
->addr
!= addr
) {
4368 lastp
= &port
->next
;
4376 *lastp
= port
->next
;
4377 usb_attach(port
, NULL
);
4378 dev
->handle_destroy(dev
);
4379 port
->next
= free_usb_ports
;
4380 free_usb_ports
= port
;
4384 void do_usb_add(const char *devname
)
4387 ret
= usb_device_add(devname
);
4389 term_printf("Could not add USB device '%s'\n", devname
);
4392 void do_usb_del(const char *devname
)
4395 ret
= usb_device_del(devname
);
4397 term_printf("Could not remove USB device '%s'\n", devname
);
4404 const char *speed_str
;
4407 term_printf("USB support not enabled\n");
4411 for (port
= used_usb_ports
; port
; port
= port
->next
) {
4415 switch(dev
->speed
) {
4419 case USB_SPEED_FULL
:
4422 case USB_SPEED_HIGH
:
4429 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
4430 0, dev
->addr
, speed_str
, dev
->devname
);
4434 /***********************************************************/
4435 /* PCMCIA/Cardbus */
4437 static struct pcmcia_socket_entry_s
{
4438 struct pcmcia_socket_s
*socket
;
4439 struct pcmcia_socket_entry_s
*next
;
4440 } *pcmcia_sockets
= 0;
4442 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
4444 struct pcmcia_socket_entry_s
*entry
;
4446 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
4447 entry
->socket
= socket
;
4448 entry
->next
= pcmcia_sockets
;
4449 pcmcia_sockets
= entry
;
4452 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
4454 struct pcmcia_socket_entry_s
*entry
, **ptr
;
4456 ptr
= &pcmcia_sockets
;
4457 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
4458 if (entry
->socket
== socket
) {
4464 void pcmcia_info(void)
4466 struct pcmcia_socket_entry_s
*iter
;
4467 if (!pcmcia_sockets
)
4468 term_printf("No PCMCIA sockets\n");
4470 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
4471 term_printf("%s: %s\n", iter
->socket
->slot_string
,
4472 iter
->socket
->attached
? iter
->socket
->card_string
:
4476 /***********************************************************/
4479 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4483 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4487 static void dumb_refresh(DisplayState
*ds
)
4492 void dumb_display_init(DisplayState
*ds
)
4497 ds
->dpy_update
= dumb_update
;
4498 ds
->dpy_resize
= dumb_resize
;
4499 ds
->dpy_refresh
= dumb_refresh
;
4502 /***********************************************************/
4505 #define MAX_IO_HANDLERS 64
4507 typedef struct IOHandlerRecord
{
4509 IOCanRWHandler
*fd_read_poll
;
4511 IOHandler
*fd_write
;
4514 /* temporary data */
4516 struct IOHandlerRecord
*next
;
4519 static IOHandlerRecord
*first_io_handler
;
4521 /* XXX: fd_read_poll should be suppressed, but an API change is
4522 necessary in the character devices to suppress fd_can_read(). */
4523 int qemu_set_fd_handler2(int fd
,
4524 IOCanRWHandler
*fd_read_poll
,
4526 IOHandler
*fd_write
,
4529 IOHandlerRecord
**pioh
, *ioh
;
4531 if (!fd_read
&& !fd_write
) {
4532 pioh
= &first_io_handler
;
4537 if (ioh
->fd
== fd
) {
4544 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4548 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4551 ioh
->next
= first_io_handler
;
4552 first_io_handler
= ioh
;
4555 ioh
->fd_read_poll
= fd_read_poll
;
4556 ioh
->fd_read
= fd_read
;
4557 ioh
->fd_write
= fd_write
;
4558 ioh
->opaque
= opaque
;
4564 int qemu_set_fd_handler(int fd
,
4566 IOHandler
*fd_write
,
4569 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4572 /***********************************************************/
4573 /* Polling handling */
4575 typedef struct PollingEntry
{
4578 struct PollingEntry
*next
;
4581 static PollingEntry
*first_polling_entry
;
4583 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4585 PollingEntry
**ppe
, *pe
;
4586 pe
= qemu_mallocz(sizeof(PollingEntry
));
4590 pe
->opaque
= opaque
;
4591 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
4596 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
4598 PollingEntry
**ppe
, *pe
;
4599 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
4601 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
4610 /***********************************************************/
4611 /* Wait objects support */
4612 typedef struct WaitObjects
{
4614 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
4615 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
4616 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
4619 static WaitObjects wait_objects
= {0};
4621 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4623 WaitObjects
*w
= &wait_objects
;
4625 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
4627 w
->events
[w
->num
] = handle
;
4628 w
->func
[w
->num
] = func
;
4629 w
->opaque
[w
->num
] = opaque
;
4634 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4637 WaitObjects
*w
= &wait_objects
;
4640 for (i
= 0; i
< w
->num
; i
++) {
4641 if (w
->events
[i
] == handle
)
4644 w
->events
[i
] = w
->events
[i
+ 1];
4645 w
->func
[i
] = w
->func
[i
+ 1];
4646 w
->opaque
[i
] = w
->opaque
[i
+ 1];
4654 /***********************************************************/
4655 /* savevm/loadvm support */
4657 #define IO_BUF_SIZE 32768
4661 BlockDriverState
*bs
;
4664 int64_t base_offset
;
4665 int64_t buf_offset
; /* start of buffer when writing, end of buffer
4668 int buf_size
; /* 0 when writing */
4669 uint8_t buf
[IO_BUF_SIZE
];
4672 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
4676 f
= qemu_mallocz(sizeof(QEMUFile
));
4679 if (!strcmp(mode
, "wb")) {
4681 } else if (!strcmp(mode
, "rb")) {
4686 f
->outfile
= fopen(filename
, mode
);
4698 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
4702 f
= qemu_mallocz(sizeof(QEMUFile
));
4707 f
->is_writable
= is_writable
;
4708 f
->base_offset
= offset
;
4712 void qemu_fflush(QEMUFile
*f
)
4714 if (!f
->is_writable
)
4716 if (f
->buf_index
> 0) {
4718 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4719 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
4721 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4722 f
->buf
, f
->buf_index
);
4724 f
->buf_offset
+= f
->buf_index
;
4729 static void qemu_fill_buffer(QEMUFile
*f
)
4736 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4737 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
4741 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4742 f
->buf
, IO_BUF_SIZE
);
4748 f
->buf_offset
+= len
;
4751 void qemu_fclose(QEMUFile
*f
)
4761 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
4765 l
= IO_BUF_SIZE
- f
->buf_index
;
4768 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
4772 if (f
->buf_index
>= IO_BUF_SIZE
)
4777 void qemu_put_byte(QEMUFile
*f
, int v
)
4779 f
->buf
[f
->buf_index
++] = v
;
4780 if (f
->buf_index
>= IO_BUF_SIZE
)
4784 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
4790 l
= f
->buf_size
- f
->buf_index
;
4792 qemu_fill_buffer(f
);
4793 l
= f
->buf_size
- f
->buf_index
;
4799 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
4804 return size1
- size
;
4807 int qemu_get_byte(QEMUFile
*f
)
4809 if (f
->buf_index
>= f
->buf_size
) {
4810 qemu_fill_buffer(f
);
4811 if (f
->buf_index
>= f
->buf_size
)
4814 return f
->buf
[f
->buf_index
++];
4817 int64_t qemu_ftell(QEMUFile
*f
)
4819 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
4822 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
4824 if (whence
== SEEK_SET
) {
4826 } else if (whence
== SEEK_CUR
) {
4827 pos
+= qemu_ftell(f
);
4829 /* SEEK_END not supported */
4832 if (f
->is_writable
) {
4834 f
->buf_offset
= pos
;
4836 f
->buf_offset
= pos
;
4843 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
4845 qemu_put_byte(f
, v
>> 8);
4846 qemu_put_byte(f
, v
);
4849 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
4851 qemu_put_byte(f
, v
>> 24);
4852 qemu_put_byte(f
, v
>> 16);
4853 qemu_put_byte(f
, v
>> 8);
4854 qemu_put_byte(f
, v
);
4857 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
4859 qemu_put_be32(f
, v
>> 32);
4860 qemu_put_be32(f
, v
);
4863 unsigned int qemu_get_be16(QEMUFile
*f
)
4866 v
= qemu_get_byte(f
) << 8;
4867 v
|= qemu_get_byte(f
);
4871 unsigned int qemu_get_be32(QEMUFile
*f
)
4874 v
= qemu_get_byte(f
) << 24;
4875 v
|= qemu_get_byte(f
) << 16;
4876 v
|= qemu_get_byte(f
) << 8;
4877 v
|= qemu_get_byte(f
);
4881 uint64_t qemu_get_be64(QEMUFile
*f
)
4884 v
= (uint64_t)qemu_get_be32(f
) << 32;
4885 v
|= qemu_get_be32(f
);
4889 typedef struct SaveStateEntry
{
4893 SaveStateHandler
*save_state
;
4894 LoadStateHandler
*load_state
;
4896 struct SaveStateEntry
*next
;
4899 static SaveStateEntry
*first_se
;
4901 int register_savevm(const char *idstr
,
4904 SaveStateHandler
*save_state
,
4905 LoadStateHandler
*load_state
,
4908 SaveStateEntry
*se
, **pse
;
4910 se
= qemu_malloc(sizeof(SaveStateEntry
));
4913 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
4914 se
->instance_id
= instance_id
;
4915 se
->version_id
= version_id
;
4916 se
->save_state
= save_state
;
4917 se
->load_state
= load_state
;
4918 se
->opaque
= opaque
;
4921 /* add at the end of list */
4923 while (*pse
!= NULL
)
4924 pse
= &(*pse
)->next
;
4929 #define QEMU_VM_FILE_MAGIC 0x5145564d
4930 #define QEMU_VM_FILE_VERSION 0x00000002
4932 int qemu_savevm_state(QEMUFile
*f
)
4936 int64_t cur_pos
, len_pos
, total_len_pos
;
4938 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
4939 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
4940 total_len_pos
= qemu_ftell(f
);
4941 qemu_put_be64(f
, 0); /* total size */
4943 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4945 len
= strlen(se
->idstr
);
4946 qemu_put_byte(f
, len
);
4947 qemu_put_buffer(f
, se
->idstr
, len
);
4949 qemu_put_be32(f
, se
->instance_id
);
4950 qemu_put_be32(f
, se
->version_id
);
4952 /* record size: filled later */
4953 len_pos
= qemu_ftell(f
);
4954 qemu_put_be32(f
, 0);
4956 se
->save_state(f
, se
->opaque
);
4958 /* fill record size */
4959 cur_pos
= qemu_ftell(f
);
4960 len
= cur_pos
- len_pos
- 4;
4961 qemu_fseek(f
, len_pos
, SEEK_SET
);
4962 qemu_put_be32(f
, len
);
4963 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4965 cur_pos
= qemu_ftell(f
);
4966 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
4967 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
4968 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4974 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
4978 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4979 if (!strcmp(se
->idstr
, idstr
) &&
4980 instance_id
== se
->instance_id
)
4986 int qemu_loadvm_state(QEMUFile
*f
)
4989 int len
, ret
, instance_id
, record_len
, version_id
;
4990 int64_t total_len
, end_pos
, cur_pos
;
4994 v
= qemu_get_be32(f
);
4995 if (v
!= QEMU_VM_FILE_MAGIC
)
4997 v
= qemu_get_be32(f
);
4998 if (v
!= QEMU_VM_FILE_VERSION
) {
5003 total_len
= qemu_get_be64(f
);
5004 end_pos
= total_len
+ qemu_ftell(f
);
5006 if (qemu_ftell(f
) >= end_pos
)
5008 len
= qemu_get_byte(f
);
5009 qemu_get_buffer(f
, idstr
, len
);
5011 instance_id
= qemu_get_be32(f
);
5012 version_id
= qemu_get_be32(f
);
5013 record_len
= qemu_get_be32(f
);
5015 printf("idstr=%s instance=0x%x version=%d len=%d\n",
5016 idstr
, instance_id
, version_id
, record_len
);
5018 cur_pos
= qemu_ftell(f
);
5019 se
= find_se(idstr
, instance_id
);
5021 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
5022 instance_id
, idstr
);
5024 ret
= se
->load_state(f
, se
->opaque
, version_id
);
5026 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
5027 instance_id
, idstr
);
5030 /* always seek to exact end of record */
5031 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
5038 /* device can contain snapshots */
5039 static int bdrv_can_snapshot(BlockDriverState
*bs
)
5042 !bdrv_is_removable(bs
) &&
5043 !bdrv_is_read_only(bs
));
5046 /* device must be snapshots in order to have a reliable snapshot */
5047 static int bdrv_has_snapshot(BlockDriverState
*bs
)
5050 !bdrv_is_removable(bs
) &&
5051 !bdrv_is_read_only(bs
));
5054 static BlockDriverState
*get_bs_snapshots(void)
5056 BlockDriverState
*bs
;
5060 return bs_snapshots
;
5061 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5063 if (bdrv_can_snapshot(bs
))
5072 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
5075 QEMUSnapshotInfo
*sn_tab
, *sn
;
5079 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5082 for(i
= 0; i
< nb_sns
; i
++) {
5084 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
5094 void do_savevm(const char *name
)
5096 BlockDriverState
*bs
, *bs1
;
5097 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
5098 int must_delete
, ret
, i
;
5099 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5101 int saved_vm_running
;
5108 bs
= get_bs_snapshots();
5110 term_printf("No block device can accept snapshots\n");
5114 /* ??? Should this occur after vm_stop? */
5117 saved_vm_running
= vm_running
;
5122 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
5127 memset(sn
, 0, sizeof(*sn
));
5129 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
5130 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
5133 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
5136 /* fill auxiliary fields */
5139 sn
->date_sec
= tb
.time
;
5140 sn
->date_nsec
= tb
.millitm
* 1000000;
5142 gettimeofday(&tv
, NULL
);
5143 sn
->date_sec
= tv
.tv_sec
;
5144 sn
->date_nsec
= tv
.tv_usec
* 1000;
5146 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
5148 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5149 term_printf("Device %s does not support VM state snapshots\n",
5150 bdrv_get_device_name(bs
));
5154 /* save the VM state */
5155 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
5157 term_printf("Could not open VM state file\n");
5160 ret
= qemu_savevm_state(f
);
5161 sn
->vm_state_size
= qemu_ftell(f
);
5164 term_printf("Error %d while writing VM\n", ret
);
5168 /* create the snapshots */
5170 for(i
= 0; i
< MAX_DISKS
; i
++) {
5172 if (bdrv_has_snapshot(bs1
)) {
5174 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
5176 term_printf("Error while deleting snapshot on '%s'\n",
5177 bdrv_get_device_name(bs1
));
5180 ret
= bdrv_snapshot_create(bs1
, sn
);
5182 term_printf("Error while creating snapshot on '%s'\n",
5183 bdrv_get_device_name(bs1
));
5189 if (saved_vm_running
)
5193 void do_loadvm(const char *name
)
5195 BlockDriverState
*bs
, *bs1
;
5196 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5199 int saved_vm_running
;
5201 bs
= get_bs_snapshots();
5203 term_printf("No block device supports snapshots\n");
5207 /* Flush all IO requests so they don't interfere with the new state. */
5210 saved_vm_running
= vm_running
;
5213 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5215 if (bdrv_has_snapshot(bs1
)) {
5216 ret
= bdrv_snapshot_goto(bs1
, name
);
5219 term_printf("Warning: ");
5222 term_printf("Snapshots not supported on device '%s'\n",
5223 bdrv_get_device_name(bs1
));
5226 term_printf("Could not find snapshot '%s' on device '%s'\n",
5227 name
, bdrv_get_device_name(bs1
));
5230 term_printf("Error %d while activating snapshot on '%s'\n",
5231 ret
, bdrv_get_device_name(bs1
));
5234 /* fatal on snapshot block device */
5241 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5242 term_printf("Device %s does not support VM state snapshots\n",
5243 bdrv_get_device_name(bs
));
5247 /* restore the VM state */
5248 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
5250 term_printf("Could not open VM state file\n");
5253 ret
= qemu_loadvm_state(f
);
5256 term_printf("Error %d while loading VM state\n", ret
);
5259 if (saved_vm_running
)
5263 void do_delvm(const char *name
)
5265 BlockDriverState
*bs
, *bs1
;
5268 bs
= get_bs_snapshots();
5270 term_printf("No block device supports snapshots\n");
5274 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5276 if (bdrv_has_snapshot(bs1
)) {
5277 ret
= bdrv_snapshot_delete(bs1
, name
);
5279 if (ret
== -ENOTSUP
)
5280 term_printf("Snapshots not supported on device '%s'\n",
5281 bdrv_get_device_name(bs1
));
5283 term_printf("Error %d while deleting snapshot on '%s'\n",
5284 ret
, bdrv_get_device_name(bs1
));
5290 void do_info_snapshots(void)
5292 BlockDriverState
*bs
, *bs1
;
5293 QEMUSnapshotInfo
*sn_tab
, *sn
;
5297 bs
= get_bs_snapshots();
5299 term_printf("No available block device supports snapshots\n");
5302 term_printf("Snapshot devices:");
5303 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5305 if (bdrv_has_snapshot(bs1
)) {
5307 term_printf(" %s", bdrv_get_device_name(bs1
));
5312 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5314 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
5317 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
5318 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
5319 for(i
= 0; i
< nb_sns
; i
++) {
5321 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
5326 /***********************************************************/
5327 /* cpu save/restore */
5329 #if defined(TARGET_I386)
5331 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
5333 qemu_put_be32(f
, dt
->selector
);
5334 qemu_put_betl(f
, dt
->base
);
5335 qemu_put_be32(f
, dt
->limit
);
5336 qemu_put_be32(f
, dt
->flags
);
5339 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
5341 dt
->selector
= qemu_get_be32(f
);
5342 dt
->base
= qemu_get_betl(f
);
5343 dt
->limit
= qemu_get_be32(f
);
5344 dt
->flags
= qemu_get_be32(f
);
5347 void cpu_save(QEMUFile
*f
, void *opaque
)
5349 CPUState
*env
= opaque
;
5350 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
5354 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5355 qemu_put_betls(f
, &env
->regs
[i
]);
5356 qemu_put_betls(f
, &env
->eip
);
5357 qemu_put_betls(f
, &env
->eflags
);
5358 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
5359 qemu_put_be32s(f
, &hflags
);
5363 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
5365 for(i
= 0; i
< 8; i
++) {
5366 fptag
|= ((!env
->fptags
[i
]) << i
);
5369 qemu_put_be16s(f
, &fpuc
);
5370 qemu_put_be16s(f
, &fpus
);
5371 qemu_put_be16s(f
, &fptag
);
5373 #ifdef USE_X86LDOUBLE
5378 qemu_put_be16s(f
, &fpregs_format
);
5380 for(i
= 0; i
< 8; i
++) {
5381 #ifdef USE_X86LDOUBLE
5385 /* we save the real CPU data (in case of MMX usage only 'mant'
5386 contains the MMX register */
5387 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
5388 qemu_put_be64(f
, mant
);
5389 qemu_put_be16(f
, exp
);
5392 /* if we use doubles for float emulation, we save the doubles to
5393 avoid losing information in case of MMX usage. It can give
5394 problems if the image is restored on a CPU where long
5395 doubles are used instead. */
5396 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
5400 for(i
= 0; i
< 6; i
++)
5401 cpu_put_seg(f
, &env
->segs
[i
]);
5402 cpu_put_seg(f
, &env
->ldt
);
5403 cpu_put_seg(f
, &env
->tr
);
5404 cpu_put_seg(f
, &env
->gdt
);
5405 cpu_put_seg(f
, &env
->idt
);
5407 qemu_put_be32s(f
, &env
->sysenter_cs
);
5408 qemu_put_be32s(f
, &env
->sysenter_esp
);
5409 qemu_put_be32s(f
, &env
->sysenter_eip
);
5411 qemu_put_betls(f
, &env
->cr
[0]);
5412 qemu_put_betls(f
, &env
->cr
[2]);
5413 qemu_put_betls(f
, &env
->cr
[3]);
5414 qemu_put_betls(f
, &env
->cr
[4]);
5416 for(i
= 0; i
< 8; i
++)
5417 qemu_put_betls(f
, &env
->dr
[i
]);
5420 qemu_put_be32s(f
, &env
->a20_mask
);
5423 qemu_put_be32s(f
, &env
->mxcsr
);
5424 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5425 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5426 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5429 #ifdef TARGET_X86_64
5430 qemu_put_be64s(f
, &env
->efer
);
5431 qemu_put_be64s(f
, &env
->star
);
5432 qemu_put_be64s(f
, &env
->lstar
);
5433 qemu_put_be64s(f
, &env
->cstar
);
5434 qemu_put_be64s(f
, &env
->fmask
);
5435 qemu_put_be64s(f
, &env
->kernelgsbase
);
5437 qemu_put_be32s(f
, &env
->smbase
);
5440 #ifdef USE_X86LDOUBLE
5441 /* XXX: add that in a FPU generic layer */
5442 union x86_longdouble
{
5447 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
5448 #define EXPBIAS1 1023
5449 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
5450 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
5452 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
5456 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
5457 /* exponent + sign */
5458 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
5459 e
|= SIGND1(temp
) >> 16;
5464 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5466 CPUState
*env
= opaque
;
5469 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
5471 if (version_id
!= 3 && version_id
!= 4)
5473 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5474 qemu_get_betls(f
, &env
->regs
[i
]);
5475 qemu_get_betls(f
, &env
->eip
);
5476 qemu_get_betls(f
, &env
->eflags
);
5477 qemu_get_be32s(f
, &hflags
);
5479 qemu_get_be16s(f
, &fpuc
);
5480 qemu_get_be16s(f
, &fpus
);
5481 qemu_get_be16s(f
, &fptag
);
5482 qemu_get_be16s(f
, &fpregs_format
);
5484 /* NOTE: we cannot always restore the FPU state if the image come
5485 from a host with a different 'USE_X86LDOUBLE' define. We guess
5486 if we are in an MMX state to restore correctly in that case. */
5487 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
5488 for(i
= 0; i
< 8; i
++) {
5492 switch(fpregs_format
) {
5494 mant
= qemu_get_be64(f
);
5495 exp
= qemu_get_be16(f
);
5496 #ifdef USE_X86LDOUBLE
5497 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5499 /* difficult case */
5501 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5503 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5507 mant
= qemu_get_be64(f
);
5508 #ifdef USE_X86LDOUBLE
5510 union x86_longdouble
*p
;
5511 /* difficult case */
5512 p
= (void *)&env
->fpregs
[i
];
5517 fp64_to_fp80(p
, mant
);
5521 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5530 /* XXX: restore FPU round state */
5531 env
->fpstt
= (fpus
>> 11) & 7;
5532 env
->fpus
= fpus
& ~0x3800;
5534 for(i
= 0; i
< 8; i
++) {
5535 env
->fptags
[i
] = (fptag
>> i
) & 1;
5538 for(i
= 0; i
< 6; i
++)
5539 cpu_get_seg(f
, &env
->segs
[i
]);
5540 cpu_get_seg(f
, &env
->ldt
);
5541 cpu_get_seg(f
, &env
->tr
);
5542 cpu_get_seg(f
, &env
->gdt
);
5543 cpu_get_seg(f
, &env
->idt
);
5545 qemu_get_be32s(f
, &env
->sysenter_cs
);
5546 qemu_get_be32s(f
, &env
->sysenter_esp
);
5547 qemu_get_be32s(f
, &env
->sysenter_eip
);
5549 qemu_get_betls(f
, &env
->cr
[0]);
5550 qemu_get_betls(f
, &env
->cr
[2]);
5551 qemu_get_betls(f
, &env
->cr
[3]);
5552 qemu_get_betls(f
, &env
->cr
[4]);
5554 for(i
= 0; i
< 8; i
++)
5555 qemu_get_betls(f
, &env
->dr
[i
]);
5558 qemu_get_be32s(f
, &env
->a20_mask
);
5560 qemu_get_be32s(f
, &env
->mxcsr
);
5561 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5562 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5563 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5566 #ifdef TARGET_X86_64
5567 qemu_get_be64s(f
, &env
->efer
);
5568 qemu_get_be64s(f
, &env
->star
);
5569 qemu_get_be64s(f
, &env
->lstar
);
5570 qemu_get_be64s(f
, &env
->cstar
);
5571 qemu_get_be64s(f
, &env
->fmask
);
5572 qemu_get_be64s(f
, &env
->kernelgsbase
);
5574 if (version_id
>= 4)
5575 qemu_get_be32s(f
, &env
->smbase
);
5577 /* XXX: compute hflags from scratch, except for CPL and IIF */
5578 env
->hflags
= hflags
;
5583 #elif defined(TARGET_PPC)
5584 void cpu_save(QEMUFile
*f
, void *opaque
)
5588 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5593 #elif defined(TARGET_MIPS)
5594 void cpu_save(QEMUFile
*f
, void *opaque
)
5598 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5603 #elif defined(TARGET_SPARC)
5604 void cpu_save(QEMUFile
*f
, void *opaque
)
5606 CPUState
*env
= opaque
;
5610 for(i
= 0; i
< 8; i
++)
5611 qemu_put_betls(f
, &env
->gregs
[i
]);
5612 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5613 qemu_put_betls(f
, &env
->regbase
[i
]);
5616 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5622 qemu_put_be32(f
, u
.i
);
5625 qemu_put_betls(f
, &env
->pc
);
5626 qemu_put_betls(f
, &env
->npc
);
5627 qemu_put_betls(f
, &env
->y
);
5629 qemu_put_be32(f
, tmp
);
5630 qemu_put_betls(f
, &env
->fsr
);
5631 qemu_put_betls(f
, &env
->tbr
);
5632 #ifndef TARGET_SPARC64
5633 qemu_put_be32s(f
, &env
->wim
);
5635 for(i
= 0; i
< 16; i
++)
5636 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
5640 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5642 CPUState
*env
= opaque
;
5646 for(i
= 0; i
< 8; i
++)
5647 qemu_get_betls(f
, &env
->gregs
[i
]);
5648 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5649 qemu_get_betls(f
, &env
->regbase
[i
]);
5652 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5657 u
.i
= qemu_get_be32(f
);
5661 qemu_get_betls(f
, &env
->pc
);
5662 qemu_get_betls(f
, &env
->npc
);
5663 qemu_get_betls(f
, &env
->y
);
5664 tmp
= qemu_get_be32(f
);
5665 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
5666 correctly updated */
5668 qemu_get_betls(f
, &env
->fsr
);
5669 qemu_get_betls(f
, &env
->tbr
);
5670 #ifndef TARGET_SPARC64
5671 qemu_get_be32s(f
, &env
->wim
);
5673 for(i
= 0; i
< 16; i
++)
5674 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
5680 #elif defined(TARGET_ARM)
5682 /* ??? Need to implement these. */
5683 void cpu_save(QEMUFile
*f
, void *opaque
)
5687 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5694 #warning No CPU save/restore functions
5698 /***********************************************************/
5699 /* ram save/restore */
5701 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
5705 v
= qemu_get_byte(f
);
5708 if (qemu_get_buffer(f
, buf
, len
) != len
)
5712 v
= qemu_get_byte(f
);
5713 memset(buf
, v
, len
);
5721 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
5725 if (qemu_get_be32(f
) != phys_ram_size
)
5727 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
5728 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
5735 #define BDRV_HASH_BLOCK_SIZE 1024
5736 #define IOBUF_SIZE 4096
5737 #define RAM_CBLOCK_MAGIC 0xfabe
5739 typedef struct RamCompressState
{
5742 uint8_t buf
[IOBUF_SIZE
];
5745 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
5748 memset(s
, 0, sizeof(*s
));
5750 ret
= deflateInit2(&s
->zstream
, 1,
5752 9, Z_DEFAULT_STRATEGY
);
5755 s
->zstream
.avail_out
= IOBUF_SIZE
;
5756 s
->zstream
.next_out
= s
->buf
;
5760 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
5762 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
5763 qemu_put_be16(s
->f
, len
);
5764 qemu_put_buffer(s
->f
, buf
, len
);
5767 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
5771 s
->zstream
.avail_in
= len
;
5772 s
->zstream
.next_in
= (uint8_t *)buf
;
5773 while (s
->zstream
.avail_in
> 0) {
5774 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
5777 if (s
->zstream
.avail_out
== 0) {
5778 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
5779 s
->zstream
.avail_out
= IOBUF_SIZE
;
5780 s
->zstream
.next_out
= s
->buf
;
5786 static void ram_compress_close(RamCompressState
*s
)
5790 /* compress last bytes */
5792 ret
= deflate(&s
->zstream
, Z_FINISH
);
5793 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
5794 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
5796 ram_put_cblock(s
, s
->buf
, len
);
5798 s
->zstream
.avail_out
= IOBUF_SIZE
;
5799 s
->zstream
.next_out
= s
->buf
;
5800 if (ret
== Z_STREAM_END
)
5807 deflateEnd(&s
->zstream
);
5810 typedef struct RamDecompressState
{
5813 uint8_t buf
[IOBUF_SIZE
];
5814 } RamDecompressState
;
5816 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
5819 memset(s
, 0, sizeof(*s
));
5821 ret
= inflateInit(&s
->zstream
);
5827 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
5831 s
->zstream
.avail_out
= len
;
5832 s
->zstream
.next_out
= buf
;
5833 while (s
->zstream
.avail_out
> 0) {
5834 if (s
->zstream
.avail_in
== 0) {
5835 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
5837 clen
= qemu_get_be16(s
->f
);
5838 if (clen
> IOBUF_SIZE
)
5840 qemu_get_buffer(s
->f
, s
->buf
, clen
);
5841 s
->zstream
.avail_in
= clen
;
5842 s
->zstream
.next_in
= s
->buf
;
5844 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
5845 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
5852 static void ram_decompress_close(RamDecompressState
*s
)
5854 inflateEnd(&s
->zstream
);
5857 static void ram_save(QEMUFile
*f
, void *opaque
)
5860 RamCompressState s1
, *s
= &s1
;
5863 qemu_put_be32(f
, phys_ram_size
);
5864 if (ram_compress_open(s
, f
) < 0)
5866 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5868 if (tight_savevm_enabled
) {
5872 /* find if the memory block is available on a virtual
5875 for(j
= 0; j
< MAX_DISKS
; j
++) {
5877 sector_num
= bdrv_hash_find(bs_table
[j
],
5878 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5879 if (sector_num
>= 0)
5884 goto normal_compress
;
5887 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
5888 ram_compress_buf(s
, buf
, 10);
5894 ram_compress_buf(s
, buf
, 1);
5895 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5898 ram_compress_close(s
);
5901 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
5903 RamDecompressState s1
, *s
= &s1
;
5907 if (version_id
== 1)
5908 return ram_load_v1(f
, opaque
);
5909 if (version_id
!= 2)
5911 if (qemu_get_be32(f
) != phys_ram_size
)
5913 if (ram_decompress_open(s
, f
) < 0)
5915 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5916 if (ram_decompress_buf(s
, buf
, 1) < 0) {
5917 fprintf(stderr
, "Error while reading ram block header\n");
5921 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
5922 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
5931 ram_decompress_buf(s
, buf
+ 1, 9);
5933 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
5934 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
5935 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
5938 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
5939 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
5940 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
5941 bs_index
, sector_num
);
5948 printf("Error block header\n");
5952 ram_decompress_close(s
);
5956 /***********************************************************/
5957 /* bottom halves (can be seen as timers which expire ASAP) */
5966 static QEMUBH
*first_bh
= NULL
;
5968 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
5971 bh
= qemu_mallocz(sizeof(QEMUBH
));
5975 bh
->opaque
= opaque
;
5979 int qemu_bh_poll(void)
5998 void qemu_bh_schedule(QEMUBH
*bh
)
6000 CPUState
*env
= cpu_single_env
;
6004 bh
->next
= first_bh
;
6007 /* stop the currently executing CPU to execute the BH ASAP */
6009 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
6013 void qemu_bh_cancel(QEMUBH
*bh
)
6016 if (bh
->scheduled
) {
6019 pbh
= &(*pbh
)->next
;
6025 void qemu_bh_delete(QEMUBH
*bh
)
6031 /***********************************************************/
6032 /* machine registration */
6034 QEMUMachine
*first_machine
= NULL
;
6036 int qemu_register_machine(QEMUMachine
*m
)
6039 pm
= &first_machine
;
6047 QEMUMachine
*find_machine(const char *name
)
6051 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6052 if (!strcmp(m
->name
, name
))
6058 /***********************************************************/
6059 /* main execution loop */
6061 void gui_update(void *opaque
)
6063 display_state
.dpy_refresh(&display_state
);
6064 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
6067 struct vm_change_state_entry
{
6068 VMChangeStateHandler
*cb
;
6070 LIST_ENTRY (vm_change_state_entry
) entries
;
6073 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
6075 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
6078 VMChangeStateEntry
*e
;
6080 e
= qemu_mallocz(sizeof (*e
));
6086 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
6090 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
6092 LIST_REMOVE (e
, entries
);
6096 static void vm_state_notify(int running
)
6098 VMChangeStateEntry
*e
;
6100 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
6101 e
->cb(e
->opaque
, running
);
6105 /* XXX: support several handlers */
6106 static VMStopHandler
*vm_stop_cb
;
6107 static void *vm_stop_opaque
;
6109 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6112 vm_stop_opaque
= opaque
;
6116 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6130 void vm_stop(int reason
)
6133 cpu_disable_ticks();
6137 vm_stop_cb(vm_stop_opaque
, reason
);
6144 /* reset/shutdown handler */
6146 typedef struct QEMUResetEntry
{
6147 QEMUResetHandler
*func
;
6149 struct QEMUResetEntry
*next
;
6152 static QEMUResetEntry
*first_reset_entry
;
6153 static int reset_requested
;
6154 static int shutdown_requested
;
6155 static int powerdown_requested
;
6157 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
6159 QEMUResetEntry
**pre
, *re
;
6161 pre
= &first_reset_entry
;
6162 while (*pre
!= NULL
)
6163 pre
= &(*pre
)->next
;
6164 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
6166 re
->opaque
= opaque
;
6171 static void qemu_system_reset(void)
6175 /* reset all devices */
6176 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
6177 re
->func(re
->opaque
);
6181 void qemu_system_reset_request(void)
6184 shutdown_requested
= 1;
6186 reset_requested
= 1;
6189 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6192 void qemu_system_shutdown_request(void)
6194 shutdown_requested
= 1;
6196 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6199 void qemu_system_powerdown_request(void)
6201 powerdown_requested
= 1;
6203 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6206 void main_loop_wait(int timeout
)
6208 IOHandlerRecord
*ioh
;
6209 fd_set rfds
, wfds
, xfds
;
6218 /* XXX: need to suppress polling by better using win32 events */
6220 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
6221 ret
|= pe
->func(pe
->opaque
);
6226 WaitObjects
*w
= &wait_objects
;
6228 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
6229 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
6230 if (w
->func
[ret
- WAIT_OBJECT_0
])
6231 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
6233 /* Check for additional signaled events */
6234 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
6236 /* Check if event is signaled */
6237 ret2
= WaitForSingleObject(w
->events
[i
], 0);
6238 if(ret2
== WAIT_OBJECT_0
) {
6240 w
->func
[i
](w
->opaque
[i
]);
6241 } else if (ret2
== WAIT_TIMEOUT
) {
6243 err
= GetLastError();
6244 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
6247 } else if (ret
== WAIT_TIMEOUT
) {
6249 err
= GetLastError();
6250 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
6254 /* poll any events */
6255 /* XXX: separate device handlers from system ones */
6260 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6264 (!ioh
->fd_read_poll
||
6265 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
6266 FD_SET(ioh
->fd
, &rfds
);
6270 if (ioh
->fd_write
) {
6271 FD_SET(ioh
->fd
, &wfds
);
6281 tv
.tv_usec
= timeout
* 1000;
6283 #if defined(CONFIG_SLIRP)
6285 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
6288 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
6290 IOHandlerRecord
**pioh
;
6292 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6295 if (FD_ISSET(ioh
->fd
, &rfds
)) {
6296 ioh
->fd_read(ioh
->opaque
);
6298 if (FD_ISSET(ioh
->fd
, &wfds
)) {
6299 ioh
->fd_write(ioh
->opaque
);
6303 /* remove deleted IO handlers */
6304 pioh
= &first_io_handler
;
6314 #if defined(CONFIG_SLIRP)
6321 slirp_select_poll(&rfds
, &wfds
, &xfds
);
6327 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
6328 qemu_get_clock(vm_clock
));
6329 /* run dma transfers, if any */
6333 /* real time timers */
6334 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
6335 qemu_get_clock(rt_clock
));
6337 /* Check bottom-halves last in case any of the earlier events triggered
6343 static CPUState
*cur_cpu
;
6348 #ifdef CONFIG_PROFILER
6353 cur_cpu
= first_cpu
;
6360 env
= env
->next_cpu
;
6363 #ifdef CONFIG_PROFILER
6364 ti
= profile_getclock();
6366 ret
= cpu_exec(env
);
6367 #ifdef CONFIG_PROFILER
6368 qemu_time
+= profile_getclock() - ti
;
6370 if (ret
== EXCP_HLT
) {
6371 /* Give the next CPU a chance to run. */
6375 if (ret
!= EXCP_HALTED
)
6377 /* all CPUs are halted ? */
6383 if (shutdown_requested
) {
6384 ret
= EXCP_INTERRUPT
;
6387 if (reset_requested
) {
6388 reset_requested
= 0;
6389 qemu_system_reset();
6390 ret
= EXCP_INTERRUPT
;
6392 if (powerdown_requested
) {
6393 powerdown_requested
= 0;
6394 qemu_system_powerdown();
6395 ret
= EXCP_INTERRUPT
;
6397 if (ret
== EXCP_DEBUG
) {
6398 vm_stop(EXCP_DEBUG
);
6400 /* If all cpus are halted then wait until the next IRQ */
6401 /* XXX: use timeout computed from timers */
6402 if (ret
== EXCP_HALTED
)
6409 #ifdef CONFIG_PROFILER
6410 ti
= profile_getclock();
6412 main_loop_wait(timeout
);
6413 #ifdef CONFIG_PROFILER
6414 dev_time
+= profile_getclock() - ti
;
6417 cpu_disable_ticks();
6423 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2007 Fabrice Bellard\n"
6424 "usage: %s [options] [disk_image]\n"
6426 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
6428 "Standard options:\n"
6429 "-M machine select emulated machine (-M ? for list)\n"
6430 "-cpu cpu select CPU (-cpu ? for list)\n"
6431 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
6432 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
6433 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
6434 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
6435 "-mtdblock file use 'file' as on-board Flash memory image\n"
6436 "-sd file use 'file' as SecureDigital card image\n"
6437 "-pflash file use 'file' as a parallel flash image\n"
6438 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
6439 "-snapshot write to temporary files instead of disk image files\n"
6441 "-no-frame open SDL window without a frame and window decorations\n"
6442 "-no-quit disable SDL window close capability\n"
6445 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
6447 "-m megs set virtual RAM size to megs MB [default=%d]\n"
6448 "-smp n set the number of CPUs to 'n' [default=1]\n"
6449 "-nographic disable graphical output and redirect serial I/Os to console\n"
6450 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
6452 "-k language use keyboard layout (for example \"fr\" for French)\n"
6455 "-audio-help print list of audio drivers and their options\n"
6456 "-soundhw c1,... enable audio support\n"
6457 " and only specified sound cards (comma separated list)\n"
6458 " use -soundhw ? to get the list of supported cards\n"
6459 " use -soundhw all to enable all of them\n"
6461 "-localtime set the real time clock to local time [default=utc]\n"
6462 "-full-screen start in full screen\n"
6464 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
6466 "-usb enable the USB driver (will be the default soon)\n"
6467 "-usbdevice name add the host or guest USB device 'name'\n"
6468 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6469 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
6471 "-name string set the name of the guest\n"
6473 "Network options:\n"
6474 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
6475 " create a new Network Interface Card and connect it to VLAN 'n'\n"
6477 "-net user[,vlan=n][,hostname=host]\n"
6478 " connect the user mode network stack to VLAN 'n' and send\n"
6479 " hostname 'host' to DHCP clients\n"
6482 "-net tap[,vlan=n],ifname=name\n"
6483 " connect the host TAP network interface to VLAN 'n'\n"
6485 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
6486 " connect the host TAP network interface to VLAN 'n' and use\n"
6487 " the network script 'file' (default=%s);\n"
6488 " use 'script=no' to disable script execution;\n"
6489 " use 'fd=h' to connect to an already opened TAP interface\n"
6491 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
6492 " connect the vlan 'n' to another VLAN using a socket connection\n"
6493 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
6494 " connect the vlan 'n' to multicast maddr and port\n"
6495 "-net none use it alone to have zero network devices; if no -net option\n"
6496 " is provided, the default is '-net nic -net user'\n"
6499 "-tftp dir allow tftp access to files in dir [-net user]\n"
6500 "-bootp file advertise file in BOOTP replies\n"
6502 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
6504 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
6505 " redirect TCP or UDP connections from host to guest [-net user]\n"
6508 "Linux boot specific:\n"
6509 "-kernel bzImage use 'bzImage' as kernel image\n"
6510 "-append cmdline use 'cmdline' as kernel command line\n"
6511 "-initrd file use 'file' as initial ram disk\n"
6513 "Debug/Expert options:\n"
6514 "-monitor dev redirect the monitor to char device 'dev'\n"
6515 "-serial dev redirect the serial port to char device 'dev'\n"
6516 "-parallel dev redirect the parallel port to char device 'dev'\n"
6517 "-pidfile file Write PID to 'file'\n"
6518 "-S freeze CPU at startup (use 'c' to start execution)\n"
6519 "-s wait gdb connection to port\n"
6520 "-p port set gdb connection port [default=%s]\n"
6521 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
6522 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
6523 " translation (t=none or lba) (usually qemu can guess them)\n"
6524 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
6526 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
6527 "-no-kqemu disable KQEMU kernel module usage\n"
6529 #ifdef USE_CODE_COPY
6530 "-no-code-copy disable code copy acceleration\n"
6533 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
6534 " (default is CL-GD5446 PCI VGA)\n"
6535 "-no-acpi disable ACPI\n"
6537 "-no-reboot exit instead of rebooting\n"
6538 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
6539 "-vnc display start a VNC server on display\n"
6541 "-daemonize daemonize QEMU after initializing\n"
6543 "-option-rom rom load a file, rom, into the option ROM space\n"
6545 "-prom-env variable=value set OpenBIOS nvram variables\n"
6548 "During emulation, the following keys are useful:\n"
6549 "ctrl-alt-f toggle full screen\n"
6550 "ctrl-alt-n switch to virtual console 'n'\n"
6551 "ctrl-alt toggle mouse and keyboard grab\n"
6553 "When using -nographic, press 'ctrl-a h' to get some help.\n"
6558 DEFAULT_NETWORK_SCRIPT
,
6560 DEFAULT_GDBSTUB_PORT
,
6565 #define HAS_ARG 0x0001
6579 QEMU_OPTION_mtdblock
,
6583 QEMU_OPTION_snapshot
,
6585 QEMU_OPTION_no_fd_bootchk
,
6588 QEMU_OPTION_nographic
,
6589 QEMU_OPTION_portrait
,
6591 QEMU_OPTION_audio_help
,
6592 QEMU_OPTION_soundhw
,
6611 QEMU_OPTION_no_code_copy
,
6613 QEMU_OPTION_localtime
,
6614 QEMU_OPTION_cirrusvga
,
6617 QEMU_OPTION_std_vga
,
6619 QEMU_OPTION_monitor
,
6621 QEMU_OPTION_parallel
,
6623 QEMU_OPTION_full_screen
,
6624 QEMU_OPTION_no_frame
,
6625 QEMU_OPTION_no_quit
,
6626 QEMU_OPTION_pidfile
,
6627 QEMU_OPTION_no_kqemu
,
6628 QEMU_OPTION_kernel_kqemu
,
6629 QEMU_OPTION_win2k_hack
,
6631 QEMU_OPTION_usbdevice
,
6634 QEMU_OPTION_no_acpi
,
6635 QEMU_OPTION_no_reboot
,
6636 QEMU_OPTION_show_cursor
,
6637 QEMU_OPTION_daemonize
,
6638 QEMU_OPTION_option_rom
,
6639 QEMU_OPTION_semihosting
,
6641 QEMU_OPTION_prom_env
,
6644 typedef struct QEMUOption
{
6650 const QEMUOption qemu_options
[] = {
6651 { "h", 0, QEMU_OPTION_h
},
6652 { "help", 0, QEMU_OPTION_h
},
6654 { "M", HAS_ARG
, QEMU_OPTION_M
},
6655 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
6656 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
6657 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
6658 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
6659 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
6660 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
6661 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
6662 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
6663 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
6664 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
6665 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
6666 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
6667 { "snapshot", 0, QEMU_OPTION_snapshot
},
6669 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
6671 { "m", HAS_ARG
, QEMU_OPTION_m
},
6672 { "nographic", 0, QEMU_OPTION_nographic
},
6673 { "portrait", 0, QEMU_OPTION_portrait
},
6674 { "k", HAS_ARG
, QEMU_OPTION_k
},
6676 { "audio-help", 0, QEMU_OPTION_audio_help
},
6677 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
6680 { "net", HAS_ARG
, QEMU_OPTION_net
},
6682 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
6683 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
6685 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
6687 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
6690 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
6691 { "append", HAS_ARG
, QEMU_OPTION_append
},
6692 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
6694 { "S", 0, QEMU_OPTION_S
},
6695 { "s", 0, QEMU_OPTION_s
},
6696 { "p", HAS_ARG
, QEMU_OPTION_p
},
6697 { "d", HAS_ARG
, QEMU_OPTION_d
},
6698 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
6699 { "L", HAS_ARG
, QEMU_OPTION_L
},
6700 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
6702 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
6703 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
6705 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6706 { "g", 1, QEMU_OPTION_g
},
6708 { "localtime", 0, QEMU_OPTION_localtime
},
6709 { "std-vga", 0, QEMU_OPTION_std_vga
},
6710 { "echr", 1, QEMU_OPTION_echr
},
6711 { "monitor", 1, QEMU_OPTION_monitor
},
6712 { "serial", 1, QEMU_OPTION_serial
},
6713 { "parallel", 1, QEMU_OPTION_parallel
},
6714 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
6715 { "full-screen", 0, QEMU_OPTION_full_screen
},
6717 { "no-frame", 0, QEMU_OPTION_no_frame
},
6718 { "no-quit", 0, QEMU_OPTION_no_quit
},
6720 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
6721 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
6722 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
6723 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
6724 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
6726 /* temporary options */
6727 { "usb", 0, QEMU_OPTION_usb
},
6728 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
6729 { "vmwarevga", 0, QEMU_OPTION_vmsvga
},
6730 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
6731 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
6732 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
6733 { "daemonize", 0, QEMU_OPTION_daemonize
},
6734 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
6735 #if defined(TARGET_ARM)
6736 { "semihosting", 0, QEMU_OPTION_semihosting
},
6738 { "name", HAS_ARG
, QEMU_OPTION_name
},
6739 #if defined(TARGET_SPARC)
6740 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
6745 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
6747 /* this stack is only used during signal handling */
6748 #define SIGNAL_STACK_SIZE 32768
6750 static uint8_t *signal_stack
;
6754 /* password input */
6756 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
6761 if (!bdrv_is_encrypted(bs
))
6764 term_printf("%s is encrypted.\n", name
);
6765 for(i
= 0; i
< 3; i
++) {
6766 monitor_readline("Password: ", 1, password
, sizeof(password
));
6767 if (bdrv_set_key(bs
, password
) == 0)
6769 term_printf("invalid password\n");
6774 static BlockDriverState
*get_bdrv(int index
)
6776 BlockDriverState
*bs
;
6779 bs
= bs_table
[index
];
6780 } else if (index
< 6) {
6781 bs
= fd_table
[index
- 4];
6788 static void read_passwords(void)
6790 BlockDriverState
*bs
;
6793 for(i
= 0; i
< 6; i
++) {
6796 qemu_key_check(bs
, bdrv_get_device_name(bs
));
6800 /* XXX: currently we cannot use simultaneously different CPUs */
6801 void register_machines(void)
6803 #if defined(TARGET_I386)
6804 qemu_register_machine(&pc_machine
);
6805 qemu_register_machine(&isapc_machine
);
6806 #elif defined(TARGET_PPC)
6807 qemu_register_machine(&heathrow_machine
);
6808 qemu_register_machine(&core99_machine
);
6809 qemu_register_machine(&prep_machine
);
6810 qemu_register_machine(&ref405ep_machine
);
6811 qemu_register_machine(&taihu_machine
);
6812 #elif defined(TARGET_MIPS)
6813 qemu_register_machine(&mips_machine
);
6814 qemu_register_machine(&mips_malta_machine
);
6815 qemu_register_machine(&mips_pica61_machine
);
6816 #elif defined(TARGET_SPARC)
6817 #ifdef TARGET_SPARC64
6818 qemu_register_machine(&sun4u_machine
);
6820 qemu_register_machine(&ss5_machine
);
6821 qemu_register_machine(&ss10_machine
);
6823 #elif defined(TARGET_ARM)
6824 qemu_register_machine(&integratorcp_machine
);
6825 qemu_register_machine(&versatilepb_machine
);
6826 qemu_register_machine(&versatileab_machine
);
6827 qemu_register_machine(&realview_machine
);
6828 qemu_register_machine(&akitapda_machine
);
6829 qemu_register_machine(&spitzpda_machine
);
6830 qemu_register_machine(&borzoipda_machine
);
6831 qemu_register_machine(&terrierpda_machine
);
6832 #elif defined(TARGET_SH4)
6833 qemu_register_machine(&shix_machine
);
6834 #elif defined(TARGET_ALPHA)
6836 #elif defined(TARGET_M68K)
6837 qemu_register_machine(&an5206_machine
);
6839 #error unsupported CPU
6844 struct soundhw soundhw
[] = {
6845 #ifdef HAS_AUDIO_CHOICE
6852 { .init_isa
= pcspk_audio_init
}
6857 "Creative Sound Blaster 16",
6860 { .init_isa
= SB16_init
}
6867 "Yamaha YMF262 (OPL3)",
6869 "Yamaha YM3812 (OPL2)",
6873 { .init_isa
= Adlib_init
}
6880 "Gravis Ultrasound GF1",
6883 { .init_isa
= GUS_init
}
6889 "ENSONIQ AudioPCI ES1370",
6892 { .init_pci
= es1370_init
}
6896 { NULL
, NULL
, 0, 0, { NULL
} }
6899 static void select_soundhw (const char *optarg
)
6903 if (*optarg
== '?') {
6906 printf ("Valid sound card names (comma separated):\n");
6907 for (c
= soundhw
; c
->name
; ++c
) {
6908 printf ("%-11s %s\n", c
->name
, c
->descr
);
6910 printf ("\n-soundhw all will enable all of the above\n");
6911 exit (*optarg
!= '?');
6919 if (!strcmp (optarg
, "all")) {
6920 for (c
= soundhw
; c
->name
; ++c
) {
6928 e
= strchr (p
, ',');
6929 l
= !e
? strlen (p
) : (size_t) (e
- p
);
6931 for (c
= soundhw
; c
->name
; ++c
) {
6932 if (!strncmp (c
->name
, p
, l
)) {
6941 "Unknown sound card name (too big to show)\n");
6944 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
6949 p
+= l
+ (e
!= NULL
);
6953 goto show_valid_cards
;
6959 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
6961 exit(STATUS_CONTROL_C_EXIT
);
6966 #define MAX_NET_CLIENTS 32
6968 int main(int argc
, char **argv
)
6970 #ifdef CONFIG_GDBSTUB
6972 const char *gdbstub_port
;
6974 int i
, cdrom_index
, pflash_index
;
6975 int snapshot
, linux_boot
;
6976 const char *initrd_filename
;
6977 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
6978 const char *pflash_filename
[MAX_PFLASH
];
6979 const char *sd_filename
;
6980 const char *mtd_filename
;
6981 const char *kernel_filename
, *kernel_cmdline
;
6982 DisplayState
*ds
= &display_state
;
6983 int cyls
, heads
, secs
, translation
;
6984 char net_clients
[MAX_NET_CLIENTS
][256];
6987 const char *r
, *optarg
;
6988 CharDriverState
*monitor_hd
;
6989 char monitor_device
[128];
6990 char serial_devices
[MAX_SERIAL_PORTS
][128];
6991 int serial_device_index
;
6992 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
6993 int parallel_device_index
;
6994 const char *loadvm
= NULL
;
6995 QEMUMachine
*machine
;
6996 const char *cpu_model
;
6997 char usb_devices
[MAX_USB_CMDLINE
][128];
6998 int usb_devices_index
;
7000 const char *pid_file
= NULL
;
7002 LIST_INIT (&vm_change_state_head
);
7005 struct sigaction act
;
7006 sigfillset(&act
.sa_mask
);
7008 act
.sa_handler
= SIG_IGN
;
7009 sigaction(SIGPIPE
, &act
, NULL
);
7012 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
7013 /* Note: cpu_interrupt() is currently not SMP safe, so we force
7014 QEMU to run on a single CPU */
7019 h
= GetCurrentProcess();
7020 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
7021 for(i
= 0; i
< 32; i
++) {
7022 if (mask
& (1 << i
))
7027 SetProcessAffinityMask(h
, mask
);
7033 register_machines();
7034 machine
= first_machine
;
7036 initrd_filename
= NULL
;
7037 for(i
= 0; i
< MAX_FD
; i
++)
7038 fd_filename
[i
] = NULL
;
7039 for(i
= 0; i
< MAX_DISKS
; i
++)
7040 hd_filename
[i
] = NULL
;
7041 for(i
= 0; i
< MAX_PFLASH
; i
++)
7042 pflash_filename
[i
] = NULL
;
7045 mtd_filename
= NULL
;
7046 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
7047 vga_ram_size
= VGA_RAM_SIZE
;
7048 #ifdef CONFIG_GDBSTUB
7050 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
7054 kernel_filename
= NULL
;
7055 kernel_cmdline
= "";
7061 cyls
= heads
= secs
= 0;
7062 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7063 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
7065 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
7066 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
7067 serial_devices
[i
][0] = '\0';
7068 serial_device_index
= 0;
7070 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
7071 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
7072 parallel_devices
[i
][0] = '\0';
7073 parallel_device_index
= 0;
7075 usb_devices_index
= 0;
7080 /* default mac address of the first network interface */
7088 hd_filename
[0] = argv
[optind
++];
7090 const QEMUOption
*popt
;
7093 /* Treat --foo the same as -foo. */
7096 popt
= qemu_options
;
7099 fprintf(stderr
, "%s: invalid option -- '%s'\n",
7103 if (!strcmp(popt
->name
, r
+ 1))
7107 if (popt
->flags
& HAS_ARG
) {
7108 if (optind
>= argc
) {
7109 fprintf(stderr
, "%s: option '%s' requires an argument\n",
7113 optarg
= argv
[optind
++];
7118 switch(popt
->index
) {
7120 machine
= find_machine(optarg
);
7123 printf("Supported machines are:\n");
7124 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
7125 printf("%-10s %s%s\n",
7127 m
== first_machine
? " (default)" : "");
7132 case QEMU_OPTION_cpu
:
7133 /* hw initialization will check this */
7134 if (optarg
[0] == '?') {
7135 #if defined(TARGET_PPC)
7136 ppc_cpu_list(stdout
, &fprintf
);
7137 #elif defined(TARGET_ARM)
7139 #elif defined(TARGET_MIPS)
7140 mips_cpu_list(stdout
, &fprintf
);
7141 #elif defined(TARGET_SPARC)
7142 sparc_cpu_list(stdout
, &fprintf
);
7149 case QEMU_OPTION_initrd
:
7150 initrd_filename
= optarg
;
7152 case QEMU_OPTION_hda
:
7153 case QEMU_OPTION_hdb
:
7154 case QEMU_OPTION_hdc
:
7155 case QEMU_OPTION_hdd
:
7158 hd_index
= popt
->index
- QEMU_OPTION_hda
;
7159 hd_filename
[hd_index
] = optarg
;
7160 if (hd_index
== cdrom_index
)
7164 case QEMU_OPTION_mtdblock
:
7165 mtd_filename
= optarg
;
7167 case QEMU_OPTION_sd
:
7168 sd_filename
= optarg
;
7170 case QEMU_OPTION_pflash
:
7171 if (pflash_index
>= MAX_PFLASH
) {
7172 fprintf(stderr
, "qemu: too many parallel flash images\n");
7175 pflash_filename
[pflash_index
++] = optarg
;
7177 case QEMU_OPTION_snapshot
:
7180 case QEMU_OPTION_hdachs
:
7184 cyls
= strtol(p
, (char **)&p
, 0);
7185 if (cyls
< 1 || cyls
> 16383)
7190 heads
= strtol(p
, (char **)&p
, 0);
7191 if (heads
< 1 || heads
> 16)
7196 secs
= strtol(p
, (char **)&p
, 0);
7197 if (secs
< 1 || secs
> 63)
7201 if (!strcmp(p
, "none"))
7202 translation
= BIOS_ATA_TRANSLATION_NONE
;
7203 else if (!strcmp(p
, "lba"))
7204 translation
= BIOS_ATA_TRANSLATION_LBA
;
7205 else if (!strcmp(p
, "auto"))
7206 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7209 } else if (*p
!= '\0') {
7211 fprintf(stderr
, "qemu: invalid physical CHS format\n");
7216 case QEMU_OPTION_nographic
:
7217 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
7218 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "null");
7219 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
7222 case QEMU_OPTION_portrait
:
7225 case QEMU_OPTION_kernel
:
7226 kernel_filename
= optarg
;
7228 case QEMU_OPTION_append
:
7229 kernel_cmdline
= optarg
;
7231 case QEMU_OPTION_cdrom
:
7232 if (cdrom_index
>= 0) {
7233 hd_filename
[cdrom_index
] = optarg
;
7236 case QEMU_OPTION_boot
:
7237 boot_device
= optarg
[0];
7238 if (boot_device
!= 'a' &&
7239 #if defined(TARGET_SPARC) || defined(TARGET_I386)
7241 boot_device
!= 'n' &&
7243 boot_device
!= 'c' && boot_device
!= 'd') {
7244 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
7248 case QEMU_OPTION_fda
:
7249 fd_filename
[0] = optarg
;
7251 case QEMU_OPTION_fdb
:
7252 fd_filename
[1] = optarg
;
7255 case QEMU_OPTION_no_fd_bootchk
:
7259 case QEMU_OPTION_no_code_copy
:
7260 code_copy_enabled
= 0;
7262 case QEMU_OPTION_net
:
7263 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
7264 fprintf(stderr
, "qemu: too many network clients\n");
7267 pstrcpy(net_clients
[nb_net_clients
],
7268 sizeof(net_clients
[0]),
7273 case QEMU_OPTION_tftp
:
7274 tftp_prefix
= optarg
;
7276 case QEMU_OPTION_bootp
:
7277 bootp_filename
= optarg
;
7280 case QEMU_OPTION_smb
:
7281 net_slirp_smb(optarg
);
7284 case QEMU_OPTION_redir
:
7285 net_slirp_redir(optarg
);
7289 case QEMU_OPTION_audio_help
:
7293 case QEMU_OPTION_soundhw
:
7294 select_soundhw (optarg
);
7301 ram_size
= atoi(optarg
) * 1024 * 1024;
7304 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
7305 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
7306 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
7315 mask
= cpu_str_to_log_mask(optarg
);
7317 printf("Log items (comma separated):\n");
7318 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
7319 printf("%-10s %s\n", item
->name
, item
->help
);
7326 #ifdef CONFIG_GDBSTUB
7331 gdbstub_port
= optarg
;
7341 keyboard_layout
= optarg
;
7343 case QEMU_OPTION_localtime
:
7346 case QEMU_OPTION_cirrusvga
:
7347 cirrus_vga_enabled
= 1;
7350 case QEMU_OPTION_vmsvga
:
7351 cirrus_vga_enabled
= 0;
7354 case QEMU_OPTION_std_vga
:
7355 cirrus_vga_enabled
= 0;
7363 w
= strtol(p
, (char **)&p
, 10);
7366 fprintf(stderr
, "qemu: invalid resolution or depth\n");
7372 h
= strtol(p
, (char **)&p
, 10);
7377 depth
= strtol(p
, (char **)&p
, 10);
7378 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
7379 depth
!= 24 && depth
!= 32)
7381 } else if (*p
== '\0') {
7382 depth
= graphic_depth
;
7389 graphic_depth
= depth
;
7392 case QEMU_OPTION_echr
:
7395 term_escape_char
= strtol(optarg
, &r
, 0);
7397 printf("Bad argument to echr\n");
7400 case QEMU_OPTION_monitor
:
7401 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
7403 case QEMU_OPTION_serial
:
7404 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
7405 fprintf(stderr
, "qemu: too many serial ports\n");
7408 pstrcpy(serial_devices
[serial_device_index
],
7409 sizeof(serial_devices
[0]), optarg
);
7410 serial_device_index
++;
7412 case QEMU_OPTION_parallel
:
7413 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
7414 fprintf(stderr
, "qemu: too many parallel ports\n");
7417 pstrcpy(parallel_devices
[parallel_device_index
],
7418 sizeof(parallel_devices
[0]), optarg
);
7419 parallel_device_index
++;
7421 case QEMU_OPTION_loadvm
:
7424 case QEMU_OPTION_full_screen
:
7428 case QEMU_OPTION_no_frame
:
7431 case QEMU_OPTION_no_quit
:
7435 case QEMU_OPTION_pidfile
:
7439 case QEMU_OPTION_win2k_hack
:
7440 win2k_install_hack
= 1;
7444 case QEMU_OPTION_no_kqemu
:
7447 case QEMU_OPTION_kernel_kqemu
:
7451 case QEMU_OPTION_usb
:
7454 case QEMU_OPTION_usbdevice
:
7456 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
7457 fprintf(stderr
, "Too many USB devices\n");
7460 pstrcpy(usb_devices
[usb_devices_index
],
7461 sizeof(usb_devices
[usb_devices_index
]),
7463 usb_devices_index
++;
7465 case QEMU_OPTION_smp
:
7466 smp_cpus
= atoi(optarg
);
7467 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
7468 fprintf(stderr
, "Invalid number of CPUs\n");
7472 case QEMU_OPTION_vnc
:
7473 vnc_display
= optarg
;
7475 case QEMU_OPTION_no_acpi
:
7478 case QEMU_OPTION_no_reboot
:
7481 case QEMU_OPTION_show_cursor
:
7484 case QEMU_OPTION_daemonize
:
7487 case QEMU_OPTION_option_rom
:
7488 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
7489 fprintf(stderr
, "Too many option ROMs\n");
7492 option_rom
[nb_option_roms
] = optarg
;
7495 case QEMU_OPTION_semihosting
:
7496 semihosting_enabled
= 1;
7498 case QEMU_OPTION_name
:
7502 case QEMU_OPTION_prom_env
:
7503 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
7504 fprintf(stderr
, "Too many prom variables\n");
7507 prom_envs
[nb_prom_envs
] = optarg
;
7516 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
7517 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
7524 if (pipe(fds
) == -1)
7535 len
= read(fds
[0], &status
, 1);
7536 if (len
== -1 && (errno
== EINTR
))
7541 else if (status
== 1) {
7542 fprintf(stderr
, "Could not acquire pidfile\n");
7560 signal(SIGTSTP
, SIG_IGN
);
7561 signal(SIGTTOU
, SIG_IGN
);
7562 signal(SIGTTIN
, SIG_IGN
);
7566 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
7569 write(fds
[1], &status
, 1);
7571 fprintf(stderr
, "Could not acquire pid file\n");
7579 linux_boot
= (kernel_filename
!= NULL
);
7582 boot_device
!= 'n' &&
7583 hd_filename
[0] == '\0' &&
7584 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
7585 fd_filename
[0] == '\0')
7588 /* boot to floppy or the default cd if no hard disk defined yet */
7589 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
7590 if (fd_filename
[0] != '\0')
7596 setvbuf(stdout
, NULL
, _IOLBF
, 0);
7606 /* init network clients */
7607 if (nb_net_clients
== 0) {
7608 /* if no clients, we use a default config */
7609 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
7611 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
7616 for(i
= 0;i
< nb_net_clients
; i
++) {
7617 if (net_client_init(net_clients
[i
]) < 0)
7622 if (boot_device
== 'n') {
7623 for (i
= 0; i
< nb_nics
; i
++) {
7624 const char *model
= nd_table
[i
].model
;
7628 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
7629 if (get_image_size(buf
) > 0) {
7630 option_rom
[nb_option_roms
] = strdup(buf
);
7636 fprintf(stderr
, "No valid PXE rom found for network device\n");
7639 boot_device
= 'c'; /* to prevent confusion by the BIOS */
7643 /* init the memory */
7644 phys_ram_size
= ram_size
+ vga_ram_size
+ MAX_BIOS_SIZE
;
7646 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
7647 if (!phys_ram_base
) {
7648 fprintf(stderr
, "Could not allocate physical memory\n");
7652 /* we always create the cdrom drive, even if no disk is there */
7654 if (cdrom_index
>= 0) {
7655 bs_table
[cdrom_index
] = bdrv_new("cdrom");
7656 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
7659 /* open the virtual block devices */
7660 for(i
= 0; i
< MAX_DISKS
; i
++) {
7661 if (hd_filename
[i
]) {
7664 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
7665 bs_table
[i
] = bdrv_new(buf
);
7667 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7668 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
7672 if (i
== 0 && cyls
!= 0) {
7673 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
7674 bdrv_set_translation_hint(bs_table
[i
], translation
);
7679 /* we always create at least one floppy disk */
7680 fd_table
[0] = bdrv_new("fda");
7681 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
7683 for(i
= 0; i
< MAX_FD
; i
++) {
7684 if (fd_filename
[i
]) {
7687 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
7688 fd_table
[i
] = bdrv_new(buf
);
7689 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
7691 if (fd_filename
[i
][0] != '\0') {
7692 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
7693 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7694 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
7702 /* Open the virtual parallel flash block devices */
7703 for(i
= 0; i
< MAX_PFLASH
; i
++) {
7704 if (pflash_filename
[i
]) {
7705 if (!pflash_table
[i
]) {
7707 snprintf(buf
, sizeof(buf
), "fl%c", i
+ 'a');
7708 pflash_table
[i
] = bdrv_new(buf
);
7710 if (bdrv_open(pflash_table
[i
], pflash_filename
[i
],
7711 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7712 fprintf(stderr
, "qemu: could not open flash image '%s'\n",
7713 pflash_filename
[i
]);
7719 sd_bdrv
= bdrv_new ("sd");
7720 /* FIXME: This isn't really a floppy, but it's a reasonable
7722 bdrv_set_type_hint(sd_bdrv
, BDRV_TYPE_FLOPPY
);
7724 if (bdrv_open(sd_bdrv
, sd_filename
,
7725 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7726 fprintf(stderr
, "qemu: could not open SD card image %s\n",
7729 qemu_key_check(sd_bdrv
, sd_filename
);
7733 mtd_bdrv
= bdrv_new ("mtd");
7734 if (bdrv_open(mtd_bdrv
, mtd_filename
,
7735 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0 ||
7736 qemu_key_check(mtd_bdrv
, mtd_filename
)) {
7737 fprintf(stderr
, "qemu: could not open Flash image %s\n",
7739 bdrv_delete(mtd_bdrv
);
7744 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
7745 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
7751 dumb_display_init(ds
);
7752 } else if (vnc_display
!= NULL
) {
7753 vnc_display_init(ds
, vnc_display
);
7755 #if defined(CONFIG_SDL)
7756 sdl_display_init(ds
, full_screen
, no_frame
);
7757 #elif defined(CONFIG_COCOA)
7758 cocoa_display_init(ds
, full_screen
);
7760 dumb_display_init(ds
);
7764 /* Maintain compatibility with multiple stdio monitors */
7765 if (!strcmp(monitor_device
,"stdio")) {
7766 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7767 if (!strcmp(serial_devices
[i
],"mon:stdio")) {
7768 monitor_device
[0] = '\0';
7770 } else if (!strcmp(serial_devices
[i
],"stdio")) {
7771 monitor_device
[0] = '\0';
7772 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "mon:stdio");
7777 if (monitor_device
[0] != '\0') {
7778 monitor_hd
= qemu_chr_open(monitor_device
);
7780 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
7783 monitor_init(monitor_hd
, !nographic
);
7786 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7787 const char *devname
= serial_devices
[i
];
7788 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7789 serial_hds
[i
] = qemu_chr_open(devname
);
7790 if (!serial_hds
[i
]) {
7791 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
7795 if (!strcmp(devname
, "vc"))
7796 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
7800 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
7801 const char *devname
= parallel_devices
[i
];
7802 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7803 parallel_hds
[i
] = qemu_chr_open(devname
);
7804 if (!parallel_hds
[i
]) {
7805 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
7809 if (!strcmp(devname
, "vc"))
7810 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
7814 machine
->init(ram_size
, vga_ram_size
, boot_device
,
7815 ds
, fd_filename
, snapshot
,
7816 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
7818 /* init USB devices */
7820 for(i
= 0; i
< usb_devices_index
; i
++) {
7821 if (usb_device_add(usb_devices
[i
]) < 0) {
7822 fprintf(stderr
, "Warning: could not add USB device %s\n",
7828 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
7829 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
7831 #ifdef CONFIG_GDBSTUB
7833 /* XXX: use standard host:port notation and modify options
7835 if (gdbserver_start(gdbstub_port
) < 0) {
7836 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
7846 /* XXX: simplify init */
7859 len
= write(fds
[1], &status
, 1);
7860 if (len
== -1 && (errno
== EINTR
))
7866 fd
= open("/dev/null", O_RDWR
);