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
]);
1369 chr
->chr_event(chr
->opaque
, CHR_EVENT_BREAK
);
1372 /* Switch to the next registered device */
1374 if (chr
->focus
>= d
->mux_cnt
)
1378 term_timestamps
= !term_timestamps
;
1379 term_timestamps_start
= -1;
1382 } else if (ch
== term_escape_char
) {
1383 d
->term_got_escape
= 1;
1391 static int mux_chr_can_read(void *opaque
)
1393 CharDriverState
*chr
= opaque
;
1394 MuxDriver
*d
= chr
->opaque
;
1395 if (d
->chr_can_read
[chr
->focus
])
1396 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
1400 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
1402 CharDriverState
*chr
= opaque
;
1403 MuxDriver
*d
= chr
->opaque
;
1405 for(i
= 0; i
< size
; i
++)
1406 if (mux_proc_byte(chr
, d
, buf
[i
]))
1407 d
->chr_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
], &buf
[i
], 1);
1410 static void mux_chr_event(void *opaque
, int event
)
1412 CharDriverState
*chr
= opaque
;
1413 MuxDriver
*d
= chr
->opaque
;
1416 /* Send the event to all registered listeners */
1417 for (i
= 0; i
< d
->mux_cnt
; i
++)
1418 if (d
->chr_event
[i
])
1419 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
1422 static void mux_chr_update_read_handler(CharDriverState
*chr
)
1424 MuxDriver
*d
= chr
->opaque
;
1426 if (d
->mux_cnt
>= MAX_MUX
) {
1427 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
1430 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
1431 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
1432 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
1433 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
1434 /* Fix up the real driver with mux routines */
1435 if (d
->mux_cnt
== 0) {
1436 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
1437 mux_chr_event
, chr
);
1439 chr
->focus
= d
->mux_cnt
;
1443 CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
1445 CharDriverState
*chr
;
1448 chr
= qemu_mallocz(sizeof(CharDriverState
));
1451 d
= qemu_mallocz(sizeof(MuxDriver
));
1460 chr
->chr_write
= mux_chr_write
;
1461 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
1468 static void socket_cleanup(void)
1473 static int socket_init(void)
1478 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1480 err
= WSAGetLastError();
1481 fprintf(stderr
, "WSAStartup: %d\n", err
);
1484 atexit(socket_cleanup
);
1488 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1494 ret
= send(fd
, buf
, len
, 0);
1497 errno
= WSAGetLastError();
1498 if (errno
!= WSAEWOULDBLOCK
) {
1501 } else if (ret
== 0) {
1511 void socket_set_nonblock(int fd
)
1513 unsigned long opt
= 1;
1514 ioctlsocket(fd
, FIONBIO
, &opt
);
1519 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1525 ret
= write(fd
, buf
, len
);
1527 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1529 } else if (ret
== 0) {
1539 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1541 return unix_write(fd
, buf
, len1
);
1544 void socket_set_nonblock(int fd
)
1546 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1548 #endif /* !_WIN32 */
1557 #define STDIO_MAX_CLIENTS 1
1558 static int stdio_nb_clients
= 0;
1560 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1562 FDCharDriver
*s
= chr
->opaque
;
1563 return unix_write(s
->fd_out
, buf
, len
);
1566 static int fd_chr_read_poll(void *opaque
)
1568 CharDriverState
*chr
= opaque
;
1569 FDCharDriver
*s
= chr
->opaque
;
1571 s
->max_size
= qemu_chr_can_read(chr
);
1575 static void fd_chr_read(void *opaque
)
1577 CharDriverState
*chr
= opaque
;
1578 FDCharDriver
*s
= chr
->opaque
;
1583 if (len
> s
->max_size
)
1587 size
= read(s
->fd_in
, buf
, len
);
1589 /* FD has been closed. Remove it from the active list. */
1590 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1594 qemu_chr_read(chr
, buf
, size
);
1598 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1600 FDCharDriver
*s
= chr
->opaque
;
1602 if (s
->fd_in
>= 0) {
1603 if (nographic
&& s
->fd_in
== 0) {
1605 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1606 fd_chr_read
, NULL
, chr
);
1611 /* open a character device to a unix fd */
1612 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1614 CharDriverState
*chr
;
1617 chr
= qemu_mallocz(sizeof(CharDriverState
));
1620 s
= qemu_mallocz(sizeof(FDCharDriver
));
1628 chr
->chr_write
= fd_chr_write
;
1629 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
1631 qemu_chr_reset(chr
);
1636 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1640 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1643 return qemu_chr_open_fd(-1, fd_out
);
1646 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1649 char filename_in
[256], filename_out
[256];
1651 snprintf(filename_in
, 256, "%s.in", filename
);
1652 snprintf(filename_out
, 256, "%s.out", filename
);
1653 fd_in
= open(filename_in
, O_RDWR
| O_BINARY
);
1654 fd_out
= open(filename_out
, O_RDWR
| O_BINARY
);
1655 if (fd_in
< 0 || fd_out
< 0) {
1660 fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
);
1664 return qemu_chr_open_fd(fd_in
, fd_out
);
1668 /* for STDIO, we handle the case where several clients use it
1671 #define TERM_FIFO_MAX_SIZE 1
1673 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1674 static int term_fifo_size
;
1676 static int stdio_read_poll(void *opaque
)
1678 CharDriverState
*chr
= opaque
;
1680 /* try to flush the queue if needed */
1681 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
1682 qemu_chr_read(chr
, term_fifo
, 1);
1685 /* see if we can absorb more chars */
1686 if (term_fifo_size
== 0)
1692 static void stdio_read(void *opaque
)
1696 CharDriverState
*chr
= opaque
;
1698 size
= read(0, buf
, 1);
1700 /* stdin has been closed. Remove it from the active list. */
1701 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
1705 if (qemu_chr_can_read(chr
) > 0) {
1706 qemu_chr_read(chr
, buf
, 1);
1707 } else if (term_fifo_size
== 0) {
1708 term_fifo
[term_fifo_size
++] = buf
[0];
1713 /* init terminal so that we can grab keys */
1714 static struct termios oldtty
;
1715 static int old_fd0_flags
;
1717 static void term_exit(void)
1719 tcsetattr (0, TCSANOW
, &oldtty
);
1720 fcntl(0, F_SETFL
, old_fd0_flags
);
1723 static void term_init(void)
1727 tcgetattr (0, &tty
);
1729 old_fd0_flags
= fcntl(0, F_GETFL
);
1731 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1732 |INLCR
|IGNCR
|ICRNL
|IXON
);
1733 tty
.c_oflag
|= OPOST
;
1734 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1735 /* if graphical mode, we allow Ctrl-C handling */
1737 tty
.c_lflag
&= ~ISIG
;
1738 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1741 tty
.c_cc
[VTIME
] = 0;
1743 tcsetattr (0, TCSANOW
, &tty
);
1747 fcntl(0, F_SETFL
, O_NONBLOCK
);
1750 static CharDriverState
*qemu_chr_open_stdio(void)
1752 CharDriverState
*chr
;
1754 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1756 chr
= qemu_chr_open_fd(0, 1);
1757 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
1764 #if defined(__linux__)
1765 static CharDriverState
*qemu_chr_open_pty(void)
1768 char slave_name
[1024];
1769 int master_fd
, slave_fd
;
1771 /* Not satisfying */
1772 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1776 /* Disabling local echo and line-buffered output */
1777 tcgetattr (master_fd
, &tty
);
1778 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1780 tty
.c_cc
[VTIME
] = 0;
1781 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1783 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1784 return qemu_chr_open_fd(master_fd
, master_fd
);
1787 static void tty_serial_init(int fd
, int speed
,
1788 int parity
, int data_bits
, int stop_bits
)
1794 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1795 speed
, parity
, data_bits
, stop_bits
);
1797 tcgetattr (fd
, &tty
);
1839 cfsetispeed(&tty
, spd
);
1840 cfsetospeed(&tty
, spd
);
1842 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1843 |INLCR
|IGNCR
|ICRNL
|IXON
);
1844 tty
.c_oflag
|= OPOST
;
1845 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1846 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1867 tty
.c_cflag
|= PARENB
;
1870 tty
.c_cflag
|= PARENB
| PARODD
;
1874 tty
.c_cflag
|= CSTOPB
;
1876 tcsetattr (fd
, TCSANOW
, &tty
);
1879 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1881 FDCharDriver
*s
= chr
->opaque
;
1884 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1886 QEMUSerialSetParams
*ssp
= arg
;
1887 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1888 ssp
->data_bits
, ssp
->stop_bits
);
1891 case CHR_IOCTL_SERIAL_SET_BREAK
:
1893 int enable
= *(int *)arg
;
1895 tcsendbreak(s
->fd_in
, 1);
1904 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1906 CharDriverState
*chr
;
1909 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1912 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1913 tty_serial_init(fd
, 115200, 'N', 8, 1);
1914 chr
= qemu_chr_open_fd(fd
, fd
);
1917 chr
->chr_ioctl
= tty_serial_ioctl
;
1918 qemu_chr_reset(chr
);
1925 } ParallelCharDriver
;
1927 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1929 if (s
->mode
!= mode
) {
1931 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1938 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1940 ParallelCharDriver
*drv
= chr
->opaque
;
1945 case CHR_IOCTL_PP_READ_DATA
:
1946 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1948 *(uint8_t *)arg
= b
;
1950 case CHR_IOCTL_PP_WRITE_DATA
:
1951 b
= *(uint8_t *)arg
;
1952 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1955 case CHR_IOCTL_PP_READ_CONTROL
:
1956 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1958 /* Linux gives only the lowest bits, and no way to know data
1959 direction! For better compatibility set the fixed upper
1961 *(uint8_t *)arg
= b
| 0xc0;
1963 case CHR_IOCTL_PP_WRITE_CONTROL
:
1964 b
= *(uint8_t *)arg
;
1965 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1968 case CHR_IOCTL_PP_READ_STATUS
:
1969 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1971 *(uint8_t *)arg
= b
;
1973 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1974 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1975 struct ParallelIOArg
*parg
= arg
;
1976 int n
= read(fd
, parg
->buffer
, parg
->count
);
1977 if (n
!= parg
->count
) {
1982 case CHR_IOCTL_PP_EPP_READ
:
1983 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1984 struct ParallelIOArg
*parg
= arg
;
1985 int n
= read(fd
, parg
->buffer
, parg
->count
);
1986 if (n
!= parg
->count
) {
1991 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1992 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1993 struct ParallelIOArg
*parg
= arg
;
1994 int n
= write(fd
, parg
->buffer
, parg
->count
);
1995 if (n
!= parg
->count
) {
2000 case CHR_IOCTL_PP_EPP_WRITE
:
2001 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2002 struct ParallelIOArg
*parg
= arg
;
2003 int n
= write(fd
, parg
->buffer
, parg
->count
);
2004 if (n
!= parg
->count
) {
2015 static void pp_close(CharDriverState
*chr
)
2017 ParallelCharDriver
*drv
= chr
->opaque
;
2020 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
2021 ioctl(fd
, PPRELEASE
);
2026 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
2028 CharDriverState
*chr
;
2029 ParallelCharDriver
*drv
;
2032 fd
= open(filename
, O_RDWR
);
2036 if (ioctl(fd
, PPCLAIM
) < 0) {
2041 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
2047 drv
->mode
= IEEE1284_MODE_COMPAT
;
2049 chr
= qemu_mallocz(sizeof(CharDriverState
));
2055 chr
->chr_write
= null_chr_write
;
2056 chr
->chr_ioctl
= pp_ioctl
;
2057 chr
->chr_close
= pp_close
;
2060 qemu_chr_reset(chr
);
2066 static CharDriverState
*qemu_chr_open_pty(void)
2072 #endif /* !defined(_WIN32) */
2077 HANDLE hcom
, hrecv
, hsend
;
2078 OVERLAPPED orecv
, osend
;
2083 #define NSENDBUF 2048
2084 #define NRECVBUF 2048
2085 #define MAXCONNECT 1
2086 #define NTIMEOUT 5000
2088 static int win_chr_poll(void *opaque
);
2089 static int win_chr_pipe_poll(void *opaque
);
2091 static void win_chr_close(CharDriverState
*chr
)
2093 WinCharState
*s
= chr
->opaque
;
2096 CloseHandle(s
->hsend
);
2100 CloseHandle(s
->hrecv
);
2104 CloseHandle(s
->hcom
);
2108 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
2110 qemu_del_polling_cb(win_chr_poll
, chr
);
2113 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
2115 WinCharState
*s
= chr
->opaque
;
2117 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2122 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2124 fprintf(stderr
, "Failed CreateEvent\n");
2127 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2129 fprintf(stderr
, "Failed CreateEvent\n");
2133 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2134 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2135 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2136 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
2141 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2142 fprintf(stderr
, "Failed SetupComm\n");
2146 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2147 size
= sizeof(COMMCONFIG
);
2148 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2149 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2150 CommConfigDialog(filename
, NULL
, &comcfg
);
2152 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2153 fprintf(stderr
, "Failed SetCommState\n");
2157 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2158 fprintf(stderr
, "Failed SetCommMask\n");
2162 cto
.ReadIntervalTimeout
= MAXDWORD
;
2163 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2164 fprintf(stderr
, "Failed SetCommTimeouts\n");
2168 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2169 fprintf(stderr
, "Failed ClearCommError\n");
2172 qemu_add_polling_cb(win_chr_poll
, chr
);
2180 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2182 WinCharState
*s
= chr
->opaque
;
2183 DWORD len
, ret
, size
, err
;
2186 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2187 s
->osend
.hEvent
= s
->hsend
;
2190 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2192 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2194 err
= GetLastError();
2195 if (err
== ERROR_IO_PENDING
) {
2196 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2214 static int win_chr_read_poll(CharDriverState
*chr
)
2216 WinCharState
*s
= chr
->opaque
;
2218 s
->max_size
= qemu_chr_can_read(chr
);
2222 static void win_chr_readfile(CharDriverState
*chr
)
2224 WinCharState
*s
= chr
->opaque
;
2229 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2230 s
->orecv
.hEvent
= s
->hrecv
;
2231 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2233 err
= GetLastError();
2234 if (err
== ERROR_IO_PENDING
) {
2235 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2240 qemu_chr_read(chr
, buf
, size
);
2244 static void win_chr_read(CharDriverState
*chr
)
2246 WinCharState
*s
= chr
->opaque
;
2248 if (s
->len
> s
->max_size
)
2249 s
->len
= s
->max_size
;
2253 win_chr_readfile(chr
);
2256 static int win_chr_poll(void *opaque
)
2258 CharDriverState
*chr
= opaque
;
2259 WinCharState
*s
= chr
->opaque
;
2263 ClearCommError(s
->hcom
, &comerr
, &status
);
2264 if (status
.cbInQue
> 0) {
2265 s
->len
= status
.cbInQue
;
2266 win_chr_read_poll(chr
);
2273 static CharDriverState
*qemu_chr_open_win(const char *filename
)
2275 CharDriverState
*chr
;
2278 chr
= qemu_mallocz(sizeof(CharDriverState
));
2281 s
= qemu_mallocz(sizeof(WinCharState
));
2287 chr
->chr_write
= win_chr_write
;
2288 chr
->chr_close
= win_chr_close
;
2290 if (win_chr_init(chr
, filename
) < 0) {
2295 qemu_chr_reset(chr
);
2299 static int win_chr_pipe_poll(void *opaque
)
2301 CharDriverState
*chr
= opaque
;
2302 WinCharState
*s
= chr
->opaque
;
2305 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2308 win_chr_read_poll(chr
);
2315 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
2317 WinCharState
*s
= chr
->opaque
;
2325 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2327 fprintf(stderr
, "Failed CreateEvent\n");
2330 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2332 fprintf(stderr
, "Failed CreateEvent\n");
2336 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2337 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2338 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2340 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2341 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2342 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2347 ZeroMemory(&ov
, sizeof(ov
));
2348 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2349 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2351 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2355 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2357 fprintf(stderr
, "Failed GetOverlappedResult\n");
2359 CloseHandle(ov
.hEvent
);
2366 CloseHandle(ov
.hEvent
);
2369 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2378 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2380 CharDriverState
*chr
;
2383 chr
= qemu_mallocz(sizeof(CharDriverState
));
2386 s
= qemu_mallocz(sizeof(WinCharState
));
2392 chr
->chr_write
= win_chr_write
;
2393 chr
->chr_close
= win_chr_close
;
2395 if (win_chr_pipe_init(chr
, filename
) < 0) {
2400 qemu_chr_reset(chr
);
2404 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2406 CharDriverState
*chr
;
2409 chr
= qemu_mallocz(sizeof(CharDriverState
));
2412 s
= qemu_mallocz(sizeof(WinCharState
));
2419 chr
->chr_write
= win_chr_write
;
2420 qemu_chr_reset(chr
);
2424 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2428 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2429 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2430 if (fd_out
== INVALID_HANDLE_VALUE
)
2433 return qemu_chr_open_win_file(fd_out
);
2437 /***********************************************************/
2438 /* UDP Net console */
2442 struct sockaddr_in daddr
;
2449 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2451 NetCharDriver
*s
= chr
->opaque
;
2453 return sendto(s
->fd
, buf
, len
, 0,
2454 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2457 static int udp_chr_read_poll(void *opaque
)
2459 CharDriverState
*chr
= opaque
;
2460 NetCharDriver
*s
= chr
->opaque
;
2462 s
->max_size
= qemu_chr_can_read(chr
);
2464 /* If there were any stray characters in the queue process them
2467 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2468 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2470 s
->max_size
= qemu_chr_can_read(chr
);
2475 static void udp_chr_read(void *opaque
)
2477 CharDriverState
*chr
= opaque
;
2478 NetCharDriver
*s
= chr
->opaque
;
2480 if (s
->max_size
== 0)
2482 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2483 s
->bufptr
= s
->bufcnt
;
2488 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2489 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2491 s
->max_size
= qemu_chr_can_read(chr
);
2495 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2497 NetCharDriver
*s
= chr
->opaque
;
2500 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2501 udp_chr_read
, NULL
, chr
);
2505 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2507 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2509 int parse_host_src_port(struct sockaddr_in
*haddr
,
2510 struct sockaddr_in
*saddr
,
2513 static CharDriverState
*qemu_chr_open_udp(const char *def
)
2515 CharDriverState
*chr
= NULL
;
2516 NetCharDriver
*s
= NULL
;
2518 struct sockaddr_in saddr
;
2520 chr
= qemu_mallocz(sizeof(CharDriverState
));
2523 s
= qemu_mallocz(sizeof(NetCharDriver
));
2527 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2529 perror("socket(PF_INET, SOCK_DGRAM)");
2533 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2534 printf("Could not parse: %s\n", def
);
2538 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2548 chr
->chr_write
= udp_chr_write
;
2549 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2562 /***********************************************************/
2563 /* TCP Net console */
2574 static void tcp_chr_accept(void *opaque
);
2576 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2578 TCPCharDriver
*s
= chr
->opaque
;
2580 return send_all(s
->fd
, buf
, len
);
2582 /* XXX: indicate an error ? */
2587 static int tcp_chr_read_poll(void *opaque
)
2589 CharDriverState
*chr
= opaque
;
2590 TCPCharDriver
*s
= chr
->opaque
;
2593 s
->max_size
= qemu_chr_can_read(chr
);
2598 #define IAC_BREAK 243
2599 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2601 char *buf
, int *size
)
2603 /* Handle any telnet client's basic IAC options to satisfy char by
2604 * char mode with no echo. All IAC options will be removed from
2605 * the buf and the do_telnetopt variable will be used to track the
2606 * state of the width of the IAC information.
2608 * IAC commands come in sets of 3 bytes with the exception of the
2609 * "IAC BREAK" command and the double IAC.
2615 for (i
= 0; i
< *size
; i
++) {
2616 if (s
->do_telnetopt
> 1) {
2617 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2618 /* Double IAC means send an IAC */
2622 s
->do_telnetopt
= 1;
2624 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2625 /* Handle IAC break commands by sending a serial break */
2626 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
2631 if (s
->do_telnetopt
>= 4) {
2632 s
->do_telnetopt
= 1;
2635 if ((unsigned char)buf
[i
] == IAC
) {
2636 s
->do_telnetopt
= 2;
2647 static void tcp_chr_read(void *opaque
)
2649 CharDriverState
*chr
= opaque
;
2650 TCPCharDriver
*s
= chr
->opaque
;
2654 if (!s
->connected
|| s
->max_size
<= 0)
2657 if (len
> s
->max_size
)
2659 size
= recv(s
->fd
, buf
, len
, 0);
2661 /* connection closed */
2663 if (s
->listen_fd
>= 0) {
2664 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2666 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2669 } else if (size
> 0) {
2670 if (s
->do_telnetopt
)
2671 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2673 qemu_chr_read(chr
, buf
, size
);
2677 static void tcp_chr_connect(void *opaque
)
2679 CharDriverState
*chr
= opaque
;
2680 TCPCharDriver
*s
= chr
->opaque
;
2683 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2684 tcp_chr_read
, NULL
, chr
);
2685 qemu_chr_reset(chr
);
2688 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2689 static void tcp_chr_telnet_init(int fd
)
2692 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2693 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2694 send(fd
, (char *)buf
, 3, 0);
2695 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2696 send(fd
, (char *)buf
, 3, 0);
2697 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2698 send(fd
, (char *)buf
, 3, 0);
2699 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2700 send(fd
, (char *)buf
, 3, 0);
2703 static void socket_set_nodelay(int fd
)
2706 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2709 static void tcp_chr_accept(void *opaque
)
2711 CharDriverState
*chr
= opaque
;
2712 TCPCharDriver
*s
= chr
->opaque
;
2713 struct sockaddr_in saddr
;
2715 struct sockaddr_un uaddr
;
2717 struct sockaddr
*addr
;
2724 len
= sizeof(uaddr
);
2725 addr
= (struct sockaddr
*)&uaddr
;
2729 len
= sizeof(saddr
);
2730 addr
= (struct sockaddr
*)&saddr
;
2732 fd
= accept(s
->listen_fd
, addr
, &len
);
2733 if (fd
< 0 && errno
!= EINTR
) {
2735 } else if (fd
>= 0) {
2736 if (s
->do_telnetopt
)
2737 tcp_chr_telnet_init(fd
);
2741 socket_set_nonblock(fd
);
2743 socket_set_nodelay(fd
);
2745 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2746 tcp_chr_connect(chr
);
2749 static void tcp_chr_close(CharDriverState
*chr
)
2751 TCPCharDriver
*s
= chr
->opaque
;
2754 if (s
->listen_fd
>= 0)
2755 closesocket(s
->listen_fd
);
2759 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2763 CharDriverState
*chr
= NULL
;
2764 TCPCharDriver
*s
= NULL
;
2765 int fd
= -1, ret
, err
, val
;
2767 int is_waitconnect
= 1;
2770 struct sockaddr_in saddr
;
2772 struct sockaddr_un uaddr
;
2774 struct sockaddr
*addr
;
2779 addr
= (struct sockaddr
*)&uaddr
;
2780 addrlen
= sizeof(uaddr
);
2781 if (parse_unix_path(&uaddr
, host_str
) < 0)
2786 addr
= (struct sockaddr
*)&saddr
;
2787 addrlen
= sizeof(saddr
);
2788 if (parse_host_port(&saddr
, host_str
) < 0)
2793 while((ptr
= strchr(ptr
,','))) {
2795 if (!strncmp(ptr
,"server",6)) {
2797 } else if (!strncmp(ptr
,"nowait",6)) {
2799 } else if (!strncmp(ptr
,"nodelay",6)) {
2802 printf("Unknown option: %s\n", ptr
);
2809 chr
= qemu_mallocz(sizeof(CharDriverState
));
2812 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2818 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2821 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2826 if (!is_waitconnect
)
2827 socket_set_nonblock(fd
);
2832 s
->is_unix
= is_unix
;
2833 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2836 chr
->chr_write
= tcp_chr_write
;
2837 chr
->chr_close
= tcp_chr_close
;
2840 /* allow fast reuse */
2844 strncpy(path
, uaddr
.sun_path
, 108);
2851 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2854 ret
= bind(fd
, addr
, addrlen
);
2858 ret
= listen(fd
, 0);
2863 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2865 s
->do_telnetopt
= 1;
2868 ret
= connect(fd
, addr
, addrlen
);
2870 err
= socket_error();
2871 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2872 } else if (err
== EINPROGRESS
) {
2875 } else if (err
== WSAEALREADY
) {
2887 socket_set_nodelay(fd
);
2889 tcp_chr_connect(chr
);
2891 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2894 if (is_listen
&& is_waitconnect
) {
2895 printf("QEMU waiting for connection on: %s\n", host_str
);
2896 tcp_chr_accept(chr
);
2897 socket_set_nonblock(s
->listen_fd
);
2909 CharDriverState
*qemu_chr_open(const char *filename
)
2913 if (!strcmp(filename
, "vc")) {
2914 return text_console_init(&display_state
);
2915 } else if (!strcmp(filename
, "null")) {
2916 return qemu_chr_open_null();
2918 if (strstart(filename
, "tcp:", &p
)) {
2919 return qemu_chr_open_tcp(p
, 0, 0);
2921 if (strstart(filename
, "telnet:", &p
)) {
2922 return qemu_chr_open_tcp(p
, 1, 0);
2924 if (strstart(filename
, "udp:", &p
)) {
2925 return qemu_chr_open_udp(p
);
2927 if (strstart(filename
, "mon:", &p
)) {
2928 CharDriverState
*drv
= qemu_chr_open(p
);
2930 drv
= qemu_chr_open_mux(drv
);
2931 monitor_init(drv
, !nographic
);
2934 printf("Unable to open driver: %s\n", p
);
2938 if (strstart(filename
, "unix:", &p
)) {
2939 return qemu_chr_open_tcp(p
, 0, 1);
2940 } else if (strstart(filename
, "file:", &p
)) {
2941 return qemu_chr_open_file_out(p
);
2942 } else if (strstart(filename
, "pipe:", &p
)) {
2943 return qemu_chr_open_pipe(p
);
2944 } else if (!strcmp(filename
, "pty")) {
2945 return qemu_chr_open_pty();
2946 } else if (!strcmp(filename
, "stdio")) {
2947 return qemu_chr_open_stdio();
2950 #if defined(__linux__)
2951 if (strstart(filename
, "/dev/parport", NULL
)) {
2952 return qemu_chr_open_pp(filename
);
2954 if (strstart(filename
, "/dev/", NULL
)) {
2955 return qemu_chr_open_tty(filename
);
2959 if (strstart(filename
, "COM", NULL
)) {
2960 return qemu_chr_open_win(filename
);
2962 if (strstart(filename
, "pipe:", &p
)) {
2963 return qemu_chr_open_win_pipe(p
);
2965 if (strstart(filename
, "file:", &p
)) {
2966 return qemu_chr_open_win_file_out(p
);
2974 void qemu_chr_close(CharDriverState
*chr
)
2977 chr
->chr_close(chr
);
2980 /***********************************************************/
2981 /* network device redirectors */
2983 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2987 for(i
=0;i
<size
;i
+=16) {
2991 fprintf(f
, "%08x ", i
);
2994 fprintf(f
, " %02x", buf
[i
+j
]);
2999 for(j
=0;j
<len
;j
++) {
3001 if (c
< ' ' || c
> '~')
3003 fprintf(f
, "%c", c
);
3009 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
3012 for(i
= 0; i
< 6; i
++) {
3013 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3026 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
3031 p1
= strchr(p
, sep
);
3037 if (len
> buf_size
- 1)
3039 memcpy(buf
, p
, len
);
3046 int parse_host_src_port(struct sockaddr_in
*haddr
,
3047 struct sockaddr_in
*saddr
,
3048 const char *input_str
)
3050 char *str
= strdup(input_str
);
3051 char *host_str
= str
;
3056 * Chop off any extra arguments at the end of the string which
3057 * would start with a comma, then fill in the src port information
3058 * if it was provided else use the "any address" and "any port".
3060 if ((ptr
= strchr(str
,',')))
3063 if ((src_str
= strchr(input_str
,'@'))) {
3068 if (parse_host_port(haddr
, host_str
) < 0)
3071 if (!src_str
|| *src_str
== '\0')
3074 if (parse_host_port(saddr
, src_str
) < 0)
3085 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
3093 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3095 saddr
->sin_family
= AF_INET
;
3096 if (buf
[0] == '\0') {
3097 saddr
->sin_addr
.s_addr
= 0;
3099 if (isdigit(buf
[0])) {
3100 if (!inet_aton(buf
, &saddr
->sin_addr
))
3103 if ((he
= gethostbyname(buf
)) == NULL
)
3105 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
3108 port
= strtol(p
, (char **)&r
, 0);
3111 saddr
->sin_port
= htons(port
);
3116 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
3121 len
= MIN(108, strlen(str
));
3122 p
= strchr(str
, ',');
3124 len
= MIN(len
, p
- str
);
3126 memset(uaddr
, 0, sizeof(*uaddr
));
3128 uaddr
->sun_family
= AF_UNIX
;
3129 memcpy(uaddr
->sun_path
, str
, len
);
3135 /* find or alloc a new VLAN */
3136 VLANState
*qemu_find_vlan(int id
)
3138 VLANState
**pvlan
, *vlan
;
3139 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3143 vlan
= qemu_mallocz(sizeof(VLANState
));
3148 pvlan
= &first_vlan
;
3149 while (*pvlan
!= NULL
)
3150 pvlan
= &(*pvlan
)->next
;
3155 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
3156 IOReadHandler
*fd_read
,
3157 IOCanRWHandler
*fd_can_read
,
3160 VLANClientState
*vc
, **pvc
;
3161 vc
= qemu_mallocz(sizeof(VLANClientState
));
3164 vc
->fd_read
= fd_read
;
3165 vc
->fd_can_read
= fd_can_read
;
3166 vc
->opaque
= opaque
;
3170 pvc
= &vlan
->first_client
;
3171 while (*pvc
!= NULL
)
3172 pvc
= &(*pvc
)->next
;
3177 int qemu_can_send_packet(VLANClientState
*vc1
)
3179 VLANState
*vlan
= vc1
->vlan
;
3180 VLANClientState
*vc
;
3182 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3184 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
3191 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
3193 VLANState
*vlan
= vc1
->vlan
;
3194 VLANClientState
*vc
;
3197 printf("vlan %d send:\n", vlan
->id
);
3198 hex_dump(stdout
, buf
, size
);
3200 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3202 vc
->fd_read(vc
->opaque
, buf
, size
);
3207 #if defined(CONFIG_SLIRP)
3209 /* slirp network adapter */
3211 static int slirp_inited
;
3212 static VLANClientState
*slirp_vc
;
3214 int slirp_can_output(void)
3216 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
3219 void slirp_output(const uint8_t *pkt
, int pkt_len
)
3222 printf("slirp output:\n");
3223 hex_dump(stdout
, pkt
, pkt_len
);
3227 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
3230 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
3233 printf("slirp input:\n");
3234 hex_dump(stdout
, buf
, size
);
3236 slirp_input(buf
, size
);
3239 static int net_slirp_init(VLANState
*vlan
)
3241 if (!slirp_inited
) {
3245 slirp_vc
= qemu_new_vlan_client(vlan
,
3246 slirp_receive
, NULL
, NULL
);
3247 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
3251 static void net_slirp_redir(const char *redir_str
)
3256 struct in_addr guest_addr
;
3257 int host_port
, guest_port
;
3259 if (!slirp_inited
) {
3265 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3267 if (!strcmp(buf
, "tcp")) {
3269 } else if (!strcmp(buf
, "udp")) {
3275 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3277 host_port
= strtol(buf
, &r
, 0);
3281 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3283 if (buf
[0] == '\0') {
3284 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
3286 if (!inet_aton(buf
, &guest_addr
))
3289 guest_port
= strtol(p
, &r
, 0);
3293 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3294 fprintf(stderr
, "qemu: could not set up redirection\n");
3299 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3307 static void smb_exit(void)
3311 char filename
[1024];
3313 /* erase all the files in the directory */
3314 d
= opendir(smb_dir
);
3319 if (strcmp(de
->d_name
, ".") != 0 &&
3320 strcmp(de
->d_name
, "..") != 0) {
3321 snprintf(filename
, sizeof(filename
), "%s/%s",
3322 smb_dir
, de
->d_name
);
3330 /* automatic user mode samba server configuration */
3331 void net_slirp_smb(const char *exported_dir
)
3333 char smb_conf
[1024];
3334 char smb_cmdline
[1024];
3337 if (!slirp_inited
) {
3342 /* XXX: better tmp dir construction */
3343 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3344 if (mkdir(smb_dir
, 0700) < 0) {
3345 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3348 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3350 f
= fopen(smb_conf
, "w");
3352 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3359 "socket address=127.0.0.1\n"
3360 "pid directory=%s\n"
3361 "lock directory=%s\n"
3362 "log file=%s/log.smbd\n"
3363 "smb passwd file=%s/smbpasswd\n"
3364 "security = share\n"
3379 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3380 SMBD_COMMAND
, smb_conf
);
3382 slirp_add_exec(0, smb_cmdline
, 4, 139);
3385 #endif /* !defined(_WIN32) */
3387 #endif /* CONFIG_SLIRP */
3389 #if !defined(_WIN32)
3391 typedef struct TAPState
{
3392 VLANClientState
*vc
;
3396 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3398 TAPState
*s
= opaque
;
3401 ret
= write(s
->fd
, buf
, size
);
3402 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3409 static void tap_send(void *opaque
)
3411 TAPState
*s
= opaque
;
3418 sbuf
.maxlen
= sizeof(buf
);
3420 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
3422 size
= read(s
->fd
, buf
, sizeof(buf
));
3425 qemu_send_packet(s
->vc
, buf
, size
);
3431 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3435 s
= qemu_mallocz(sizeof(TAPState
));
3439 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3440 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3441 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3446 static int tap_open(char *ifname
, int ifname_size
)
3452 fd
= open("/dev/tap", O_RDWR
);
3454 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3459 dev
= devname(s
.st_rdev
, S_IFCHR
);
3460 pstrcpy(ifname
, ifname_size
, dev
);
3462 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3465 #elif defined(__sun__)
3466 #define TUNNEWPPA (('T'<<16) | 0x0001)
3468 * Allocate TAP device, returns opened fd.
3469 * Stores dev name in the first arg(must be large enough).
3471 int tap_alloc(char *dev
)
3473 int tap_fd
, if_fd
, ppa
= -1;
3474 static int ip_fd
= 0;
3477 static int arp_fd
= 0;
3478 int ip_muxid
, arp_muxid
;
3479 struct strioctl strioc_if
, strioc_ppa
;
3480 int link_type
= I_PLINK
;;
3482 char actual_name
[32] = "";
3484 memset(&ifr
, 0x0, sizeof(ifr
));
3488 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
3492 /* Check if IP device was opened */
3496 if( (ip_fd
= open("/dev/udp", O_RDWR
, 0)) < 0){
3497 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
3501 if( (tap_fd
= open("/dev/tap", O_RDWR
, 0)) < 0){
3502 syslog(LOG_ERR
, "Can't open /dev/tap");
3506 /* Assign a new PPA and get its unit number. */
3507 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
3508 strioc_ppa
.ic_timout
= 0;
3509 strioc_ppa
.ic_len
= sizeof(ppa
);
3510 strioc_ppa
.ic_dp
= (char *)&ppa
;
3511 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
3512 syslog (LOG_ERR
, "Can't assign new interface");
3514 if( (if_fd
= open("/dev/tap", O_RDWR
, 0)) < 0){
3515 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
3518 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
3519 syslog(LOG_ERR
, "Can't push IP module");
3523 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
3524 syslog(LOG_ERR
, "Can't get flags\n");
3526 snprintf (actual_name
, 32, "tap%d", ppa
);
3527 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3530 /* Assign ppa according to the unit number returned by tun device */
3532 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
3533 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
3534 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
3535 syslog (LOG_ERR
, "Can't get flags\n");
3536 /* Push arp module to if_fd */
3537 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
3538 syslog (LOG_ERR
, "Can't push ARP module (2)");
3540 /* Push arp module to ip_fd */
3541 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
3542 syslog (LOG_ERR
, "I_POP failed\n");
3543 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
3544 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
3546 if ((arp_fd
= open ("/dev/tap", O_RDWR
, 0)) < 0)
3547 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
3549 /* Set ifname to arp */
3550 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
3551 strioc_if
.ic_timout
= 0;
3552 strioc_if
.ic_len
= sizeof(ifr
);
3553 strioc_if
.ic_dp
= (char *)&ifr
;
3554 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
3555 syslog (LOG_ERR
, "Can't set ifname to arp\n");
3558 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
3559 syslog(LOG_ERR
, "Can't link TAP device to IP");
3563 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
3564 syslog (LOG_ERR
, "Can't link TAP device to ARP");
3568 memset(&ifr
, 0x0, sizeof(ifr
));
3569 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3570 ifr
.lifr_ip_muxid
= ip_muxid
;
3571 ifr
.lifr_arp_muxid
= arp_muxid
;
3573 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
3575 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
3576 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
3577 syslog (LOG_ERR
, "Can't set multiplexor id");
3580 sprintf(dev
, "tap%d", ppa
);
3584 static int tap_open(char *ifname
, int ifname_size
)
3588 if( (fd
= tap_alloc(dev
)) < 0 ){
3589 fprintf(stderr
, "Cannot allocate TAP device\n");
3592 pstrcpy(ifname
, ifname_size
, dev
);
3593 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3597 static int tap_open(char *ifname
, int ifname_size
)
3602 fd
= open("/dev/net/tun", O_RDWR
);
3604 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
3607 memset(&ifr
, 0, sizeof(ifr
));
3608 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
3609 if (ifname
[0] != '\0')
3610 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
3612 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
3613 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
3615 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
3619 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
3620 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3625 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
3626 const char *setup_script
)
3629 int pid
, status
, fd
;
3634 if (ifname1
!= NULL
)
3635 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
3638 fd
= tap_open(ifname
, sizeof(ifname
));
3642 if (!setup_script
|| !strcmp(setup_script
, "no"))
3644 if (setup_script
[0] != '\0') {
3645 /* try to launch network init script */
3649 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
3650 for (i
= 0; i
< open_max
; i
++)
3651 if (i
!= STDIN_FILENO
&&
3652 i
!= STDOUT_FILENO
&&
3653 i
!= STDERR_FILENO
&&
3658 *parg
++ = (char *)setup_script
;
3661 execv(setup_script
, args
);
3664 while (waitpid(pid
, &status
, 0) != pid
);
3665 if (!WIFEXITED(status
) ||
3666 WEXITSTATUS(status
) != 0) {
3667 fprintf(stderr
, "%s: could not launch network script\n",
3673 s
= net_tap_fd_init(vlan
, fd
);
3676 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3677 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
3681 #endif /* !_WIN32 */
3683 /* network connection */
3684 typedef struct NetSocketState
{
3685 VLANClientState
*vc
;
3687 int state
; /* 0 = getting length, 1 = getting data */
3691 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
3694 typedef struct NetSocketListenState
{
3697 } NetSocketListenState
;
3699 /* XXX: we consider we can send the whole packet without blocking */
3700 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
3702 NetSocketState
*s
= opaque
;
3706 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
3707 send_all(s
->fd
, buf
, size
);
3710 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
3712 NetSocketState
*s
= opaque
;
3713 sendto(s
->fd
, buf
, size
, 0,
3714 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
3717 static void net_socket_send(void *opaque
)
3719 NetSocketState
*s
= opaque
;
3724 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
3726 err
= socket_error();
3727 if (err
!= EWOULDBLOCK
)
3729 } else if (size
== 0) {
3730 /* end of connection */
3732 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3738 /* reassemble a packet from the network */
3744 memcpy(s
->buf
+ s
->index
, buf
, l
);
3748 if (s
->index
== 4) {
3750 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
3756 l
= s
->packet_len
- s
->index
;
3759 memcpy(s
->buf
+ s
->index
, buf
, l
);
3763 if (s
->index
>= s
->packet_len
) {
3764 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
3773 static void net_socket_send_dgram(void *opaque
)
3775 NetSocketState
*s
= opaque
;
3778 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
3782 /* end of connection */
3783 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3786 qemu_send_packet(s
->vc
, s
->buf
, size
);
3789 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
3794 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
3795 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
3796 inet_ntoa(mcastaddr
->sin_addr
),
3797 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
3801 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
3803 perror("socket(PF_INET, SOCK_DGRAM)");
3808 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
3809 (const char *)&val
, sizeof(val
));
3811 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
3815 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
3821 /* Add host to multicast group */
3822 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
3823 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
3825 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
3826 (const char *)&imr
, sizeof(struct ip_mreq
));
3828 perror("setsockopt(IP_ADD_MEMBERSHIP)");
3832 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
3834 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
3835 (const char *)&val
, sizeof(val
));
3837 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
3841 socket_set_nonblock(fd
);
3849 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
3852 struct sockaddr_in saddr
;
3854 socklen_t saddr_len
;
3857 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
3858 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
3859 * by ONLY ONE process: we must "clone" this dgram socket --jjo
3863 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
3865 if (saddr
.sin_addr
.s_addr
==0) {
3866 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
3870 /* clone dgram socket */
3871 newfd
= net_socket_mcast_create(&saddr
);
3873 /* error already reported by net_socket_mcast_create() */
3877 /* clone newfd to fd, close newfd */
3882 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
3883 fd
, strerror(errno
));
3888 s
= qemu_mallocz(sizeof(NetSocketState
));
3893 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
3894 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
3896 /* mcast: save bound address as dst */
3897 if (is_connected
) s
->dgram_dst
=saddr
;
3899 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3900 "socket: fd=%d (%s mcast=%s:%d)",
3901 fd
, is_connected
? "cloned" : "",
3902 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3906 static void net_socket_connect(void *opaque
)
3908 NetSocketState
*s
= opaque
;
3909 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
3912 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
3916 s
= qemu_mallocz(sizeof(NetSocketState
));
3920 s
->vc
= qemu_new_vlan_client(vlan
,
3921 net_socket_receive
, NULL
, s
);
3922 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3923 "socket: fd=%d", fd
);
3925 net_socket_connect(s
);
3927 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
3932 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
3935 int so_type
=-1, optlen
=sizeof(so_type
);
3937 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
3938 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
3943 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
3945 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3947 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
3948 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
3949 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3954 static void net_socket_accept(void *opaque
)
3956 NetSocketListenState
*s
= opaque
;
3958 struct sockaddr_in saddr
;
3963 len
= sizeof(saddr
);
3964 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
3965 if (fd
< 0 && errno
!= EINTR
) {
3967 } else if (fd
>= 0) {
3971 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
3975 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
3976 "socket: connection from %s:%d",
3977 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3981 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
3983 NetSocketListenState
*s
;
3985 struct sockaddr_in saddr
;
3987 if (parse_host_port(&saddr
, host_str
) < 0)
3990 s
= qemu_mallocz(sizeof(NetSocketListenState
));
3994 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3999 socket_set_nonblock(fd
);
4001 /* allow fast reuse */
4003 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
4005 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4010 ret
= listen(fd
, 0);
4017 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
4021 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
4024 int fd
, connected
, ret
, err
;
4025 struct sockaddr_in saddr
;
4027 if (parse_host_port(&saddr
, host_str
) < 0)
4030 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4035 socket_set_nonblock(fd
);
4039 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4041 err
= socket_error();
4042 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
4043 } else if (err
== EINPROGRESS
) {
4046 } else if (err
== WSAEALREADY
) {
4059 s
= net_socket_fd_init(vlan
, fd
, connected
);
4062 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4063 "socket: connect to %s:%d",
4064 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4068 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
4072 struct sockaddr_in saddr
;
4074 if (parse_host_port(&saddr
, host_str
) < 0)
4078 fd
= net_socket_mcast_create(&saddr
);
4082 s
= net_socket_fd_init(vlan
, fd
, 0);
4086 s
->dgram_dst
= saddr
;
4088 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4089 "socket: mcast=%s:%d",
4090 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4095 static int get_param_value(char *buf
, int buf_size
,
4096 const char *tag
, const char *str
)
4105 while (*p
!= '\0' && *p
!= '=') {
4106 if ((q
- option
) < sizeof(option
) - 1)
4114 if (!strcmp(tag
, option
)) {
4116 while (*p
!= '\0' && *p
!= ',') {
4117 if ((q
- buf
) < buf_size
- 1)
4124 while (*p
!= '\0' && *p
!= ',') {
4135 static int net_client_init(const char *str
)
4146 while (*p
!= '\0' && *p
!= ',') {
4147 if ((q
- device
) < sizeof(device
) - 1)
4155 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
4156 vlan_id
= strtol(buf
, NULL
, 0);
4158 vlan
= qemu_find_vlan(vlan_id
);
4160 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
4163 if (!strcmp(device
, "nic")) {
4167 if (nb_nics
>= MAX_NICS
) {
4168 fprintf(stderr
, "Too Many NICs\n");
4171 nd
= &nd_table
[nb_nics
];
4172 macaddr
= nd
->macaddr
;
4178 macaddr
[5] = 0x56 + nb_nics
;
4180 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
4181 if (parse_macaddr(macaddr
, buf
) < 0) {
4182 fprintf(stderr
, "invalid syntax for ethernet address\n");
4186 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
4187 nd
->model
= strdup(buf
);
4193 if (!strcmp(device
, "none")) {
4194 /* does nothing. It is needed to signal that no network cards
4199 if (!strcmp(device
, "user")) {
4200 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
4201 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
4203 ret
= net_slirp_init(vlan
);
4207 if (!strcmp(device
, "tap")) {
4209 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4210 fprintf(stderr
, "tap: no interface name\n");
4213 ret
= tap_win32_init(vlan
, ifname
);
4216 if (!strcmp(device
, "tap")) {
4218 char setup_script
[1024];
4220 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4221 fd
= strtol(buf
, NULL
, 0);
4223 if (net_tap_fd_init(vlan
, fd
))
4226 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4229 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
4230 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
4232 ret
= net_tap_init(vlan
, ifname
, setup_script
);
4236 if (!strcmp(device
, "socket")) {
4237 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4239 fd
= strtol(buf
, NULL
, 0);
4241 if (net_socket_fd_init(vlan
, fd
, 1))
4243 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
4244 ret
= net_socket_listen_init(vlan
, buf
);
4245 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
4246 ret
= net_socket_connect_init(vlan
, buf
);
4247 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
4248 ret
= net_socket_mcast_init(vlan
, buf
);
4250 fprintf(stderr
, "Unknown socket options: %s\n", p
);
4255 fprintf(stderr
, "Unknown network device: %s\n", device
);
4259 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
4265 void do_info_network(void)
4268 VLANClientState
*vc
;
4270 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
4271 term_printf("VLAN %d devices:\n", vlan
->id
);
4272 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
4273 term_printf(" %s\n", vc
->info_str
);
4277 /***********************************************************/
4280 static USBPort
*used_usb_ports
;
4281 static USBPort
*free_usb_ports
;
4283 /* ??? Maybe change this to register a hub to keep track of the topology. */
4284 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
4285 usb_attachfn attach
)
4287 port
->opaque
= opaque
;
4288 port
->index
= index
;
4289 port
->attach
= attach
;
4290 port
->next
= free_usb_ports
;
4291 free_usb_ports
= port
;
4294 static int usb_device_add(const char *devname
)
4300 if (!free_usb_ports
)
4303 if (strstart(devname
, "host:", &p
)) {
4304 dev
= usb_host_device_open(p
);
4305 } else if (!strcmp(devname
, "mouse")) {
4306 dev
= usb_mouse_init();
4307 } else if (!strcmp(devname
, "tablet")) {
4308 dev
= usb_tablet_init();
4309 } else if (strstart(devname
, "disk:", &p
)) {
4310 dev
= usb_msd_init(p
);
4317 /* Find a USB port to add the device to. */
4318 port
= free_usb_ports
;
4322 /* Create a new hub and chain it on. */
4323 free_usb_ports
= NULL
;
4324 port
->next
= used_usb_ports
;
4325 used_usb_ports
= port
;
4327 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
4328 usb_attach(port
, hub
);
4329 port
= free_usb_ports
;
4332 free_usb_ports
= port
->next
;
4333 port
->next
= used_usb_ports
;
4334 used_usb_ports
= port
;
4335 usb_attach(port
, dev
);
4339 static int usb_device_del(const char *devname
)
4347 if (!used_usb_ports
)
4350 p
= strchr(devname
, '.');
4353 bus_num
= strtoul(devname
, NULL
, 0);
4354 addr
= strtoul(p
+ 1, NULL
, 0);
4358 lastp
= &used_usb_ports
;
4359 port
= used_usb_ports
;
4360 while (port
&& port
->dev
->addr
!= addr
) {
4361 lastp
= &port
->next
;
4369 *lastp
= port
->next
;
4370 usb_attach(port
, NULL
);
4371 dev
->handle_destroy(dev
);
4372 port
->next
= free_usb_ports
;
4373 free_usb_ports
= port
;
4377 void do_usb_add(const char *devname
)
4380 ret
= usb_device_add(devname
);
4382 term_printf("Could not add USB device '%s'\n", devname
);
4385 void do_usb_del(const char *devname
)
4388 ret
= usb_device_del(devname
);
4390 term_printf("Could not remove USB device '%s'\n", devname
);
4397 const char *speed_str
;
4400 term_printf("USB support not enabled\n");
4404 for (port
= used_usb_ports
; port
; port
= port
->next
) {
4408 switch(dev
->speed
) {
4412 case USB_SPEED_FULL
:
4415 case USB_SPEED_HIGH
:
4422 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
4423 0, dev
->addr
, speed_str
, dev
->devname
);
4427 /***********************************************************/
4428 /* PCMCIA/Cardbus */
4430 static struct pcmcia_socket_entry_s
{
4431 struct pcmcia_socket_s
*socket
;
4432 struct pcmcia_socket_entry_s
*next
;
4433 } *pcmcia_sockets
= 0;
4435 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
4437 struct pcmcia_socket_entry_s
*entry
;
4439 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
4440 entry
->socket
= socket
;
4441 entry
->next
= pcmcia_sockets
;
4442 pcmcia_sockets
= entry
;
4445 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
4447 struct pcmcia_socket_entry_s
*entry
, **ptr
;
4449 ptr
= &pcmcia_sockets
;
4450 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
4451 if (entry
->socket
== socket
) {
4457 void pcmcia_info(void)
4459 struct pcmcia_socket_entry_s
*iter
;
4460 if (!pcmcia_sockets
)
4461 term_printf("No PCMCIA sockets\n");
4463 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
4464 term_printf("%s: %s\n", iter
->socket
->slot_string
,
4465 iter
->socket
->attached
? iter
->socket
->card_string
:
4469 /***********************************************************/
4472 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4476 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4480 static void dumb_refresh(DisplayState
*ds
)
4485 void dumb_display_init(DisplayState
*ds
)
4490 ds
->dpy_update
= dumb_update
;
4491 ds
->dpy_resize
= dumb_resize
;
4492 ds
->dpy_refresh
= dumb_refresh
;
4495 /***********************************************************/
4498 #define MAX_IO_HANDLERS 64
4500 typedef struct IOHandlerRecord
{
4502 IOCanRWHandler
*fd_read_poll
;
4504 IOHandler
*fd_write
;
4507 /* temporary data */
4509 struct IOHandlerRecord
*next
;
4512 static IOHandlerRecord
*first_io_handler
;
4514 /* XXX: fd_read_poll should be suppressed, but an API change is
4515 necessary in the character devices to suppress fd_can_read(). */
4516 int qemu_set_fd_handler2(int fd
,
4517 IOCanRWHandler
*fd_read_poll
,
4519 IOHandler
*fd_write
,
4522 IOHandlerRecord
**pioh
, *ioh
;
4524 if (!fd_read
&& !fd_write
) {
4525 pioh
= &first_io_handler
;
4530 if (ioh
->fd
== fd
) {
4537 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4541 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4544 ioh
->next
= first_io_handler
;
4545 first_io_handler
= ioh
;
4548 ioh
->fd_read_poll
= fd_read_poll
;
4549 ioh
->fd_read
= fd_read
;
4550 ioh
->fd_write
= fd_write
;
4551 ioh
->opaque
= opaque
;
4557 int qemu_set_fd_handler(int fd
,
4559 IOHandler
*fd_write
,
4562 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4565 /***********************************************************/
4566 /* Polling handling */
4568 typedef struct PollingEntry
{
4571 struct PollingEntry
*next
;
4574 static PollingEntry
*first_polling_entry
;
4576 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4578 PollingEntry
**ppe
, *pe
;
4579 pe
= qemu_mallocz(sizeof(PollingEntry
));
4583 pe
->opaque
= opaque
;
4584 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
4589 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
4591 PollingEntry
**ppe
, *pe
;
4592 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
4594 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
4603 /***********************************************************/
4604 /* Wait objects support */
4605 typedef struct WaitObjects
{
4607 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
4608 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
4609 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
4612 static WaitObjects wait_objects
= {0};
4614 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4616 WaitObjects
*w
= &wait_objects
;
4618 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
4620 w
->events
[w
->num
] = handle
;
4621 w
->func
[w
->num
] = func
;
4622 w
->opaque
[w
->num
] = opaque
;
4627 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4630 WaitObjects
*w
= &wait_objects
;
4633 for (i
= 0; i
< w
->num
; i
++) {
4634 if (w
->events
[i
] == handle
)
4637 w
->events
[i
] = w
->events
[i
+ 1];
4638 w
->func
[i
] = w
->func
[i
+ 1];
4639 w
->opaque
[i
] = w
->opaque
[i
+ 1];
4647 /***********************************************************/
4648 /* savevm/loadvm support */
4650 #define IO_BUF_SIZE 32768
4654 BlockDriverState
*bs
;
4657 int64_t base_offset
;
4658 int64_t buf_offset
; /* start of buffer when writing, end of buffer
4661 int buf_size
; /* 0 when writing */
4662 uint8_t buf
[IO_BUF_SIZE
];
4665 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
4669 f
= qemu_mallocz(sizeof(QEMUFile
));
4672 if (!strcmp(mode
, "wb")) {
4674 } else if (!strcmp(mode
, "rb")) {
4679 f
->outfile
= fopen(filename
, mode
);
4691 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
4695 f
= qemu_mallocz(sizeof(QEMUFile
));
4700 f
->is_writable
= is_writable
;
4701 f
->base_offset
= offset
;
4705 void qemu_fflush(QEMUFile
*f
)
4707 if (!f
->is_writable
)
4709 if (f
->buf_index
> 0) {
4711 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4712 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
4714 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4715 f
->buf
, f
->buf_index
);
4717 f
->buf_offset
+= f
->buf_index
;
4722 static void qemu_fill_buffer(QEMUFile
*f
)
4729 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4730 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
4734 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4735 f
->buf
, IO_BUF_SIZE
);
4741 f
->buf_offset
+= len
;
4744 void qemu_fclose(QEMUFile
*f
)
4754 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
4758 l
= IO_BUF_SIZE
- f
->buf_index
;
4761 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
4765 if (f
->buf_index
>= IO_BUF_SIZE
)
4770 void qemu_put_byte(QEMUFile
*f
, int v
)
4772 f
->buf
[f
->buf_index
++] = v
;
4773 if (f
->buf_index
>= IO_BUF_SIZE
)
4777 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
4783 l
= f
->buf_size
- f
->buf_index
;
4785 qemu_fill_buffer(f
);
4786 l
= f
->buf_size
- f
->buf_index
;
4792 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
4797 return size1
- size
;
4800 int qemu_get_byte(QEMUFile
*f
)
4802 if (f
->buf_index
>= f
->buf_size
) {
4803 qemu_fill_buffer(f
);
4804 if (f
->buf_index
>= f
->buf_size
)
4807 return f
->buf
[f
->buf_index
++];
4810 int64_t qemu_ftell(QEMUFile
*f
)
4812 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
4815 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
4817 if (whence
== SEEK_SET
) {
4819 } else if (whence
== SEEK_CUR
) {
4820 pos
+= qemu_ftell(f
);
4822 /* SEEK_END not supported */
4825 if (f
->is_writable
) {
4827 f
->buf_offset
= pos
;
4829 f
->buf_offset
= pos
;
4836 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
4838 qemu_put_byte(f
, v
>> 8);
4839 qemu_put_byte(f
, v
);
4842 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
4844 qemu_put_byte(f
, v
>> 24);
4845 qemu_put_byte(f
, v
>> 16);
4846 qemu_put_byte(f
, v
>> 8);
4847 qemu_put_byte(f
, v
);
4850 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
4852 qemu_put_be32(f
, v
>> 32);
4853 qemu_put_be32(f
, v
);
4856 unsigned int qemu_get_be16(QEMUFile
*f
)
4859 v
= qemu_get_byte(f
) << 8;
4860 v
|= qemu_get_byte(f
);
4864 unsigned int qemu_get_be32(QEMUFile
*f
)
4867 v
= qemu_get_byte(f
) << 24;
4868 v
|= qemu_get_byte(f
) << 16;
4869 v
|= qemu_get_byte(f
) << 8;
4870 v
|= qemu_get_byte(f
);
4874 uint64_t qemu_get_be64(QEMUFile
*f
)
4877 v
= (uint64_t)qemu_get_be32(f
) << 32;
4878 v
|= qemu_get_be32(f
);
4882 typedef struct SaveStateEntry
{
4886 SaveStateHandler
*save_state
;
4887 LoadStateHandler
*load_state
;
4889 struct SaveStateEntry
*next
;
4892 static SaveStateEntry
*first_se
;
4894 int register_savevm(const char *idstr
,
4897 SaveStateHandler
*save_state
,
4898 LoadStateHandler
*load_state
,
4901 SaveStateEntry
*se
, **pse
;
4903 se
= qemu_malloc(sizeof(SaveStateEntry
));
4906 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
4907 se
->instance_id
= instance_id
;
4908 se
->version_id
= version_id
;
4909 se
->save_state
= save_state
;
4910 se
->load_state
= load_state
;
4911 se
->opaque
= opaque
;
4914 /* add at the end of list */
4916 while (*pse
!= NULL
)
4917 pse
= &(*pse
)->next
;
4922 #define QEMU_VM_FILE_MAGIC 0x5145564d
4923 #define QEMU_VM_FILE_VERSION 0x00000002
4925 int qemu_savevm_state(QEMUFile
*f
)
4929 int64_t cur_pos
, len_pos
, total_len_pos
;
4931 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
4932 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
4933 total_len_pos
= qemu_ftell(f
);
4934 qemu_put_be64(f
, 0); /* total size */
4936 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4938 len
= strlen(se
->idstr
);
4939 qemu_put_byte(f
, len
);
4940 qemu_put_buffer(f
, se
->idstr
, len
);
4942 qemu_put_be32(f
, se
->instance_id
);
4943 qemu_put_be32(f
, se
->version_id
);
4945 /* record size: filled later */
4946 len_pos
= qemu_ftell(f
);
4947 qemu_put_be32(f
, 0);
4949 se
->save_state(f
, se
->opaque
);
4951 /* fill record size */
4952 cur_pos
= qemu_ftell(f
);
4953 len
= cur_pos
- len_pos
- 4;
4954 qemu_fseek(f
, len_pos
, SEEK_SET
);
4955 qemu_put_be32(f
, len
);
4956 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4958 cur_pos
= qemu_ftell(f
);
4959 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
4960 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
4961 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4967 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
4971 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4972 if (!strcmp(se
->idstr
, idstr
) &&
4973 instance_id
== se
->instance_id
)
4979 int qemu_loadvm_state(QEMUFile
*f
)
4982 int len
, ret
, instance_id
, record_len
, version_id
;
4983 int64_t total_len
, end_pos
, cur_pos
;
4987 v
= qemu_get_be32(f
);
4988 if (v
!= QEMU_VM_FILE_MAGIC
)
4990 v
= qemu_get_be32(f
);
4991 if (v
!= QEMU_VM_FILE_VERSION
) {
4996 total_len
= qemu_get_be64(f
);
4997 end_pos
= total_len
+ qemu_ftell(f
);
4999 if (qemu_ftell(f
) >= end_pos
)
5001 len
= qemu_get_byte(f
);
5002 qemu_get_buffer(f
, idstr
, len
);
5004 instance_id
= qemu_get_be32(f
);
5005 version_id
= qemu_get_be32(f
);
5006 record_len
= qemu_get_be32(f
);
5008 printf("idstr=%s instance=0x%x version=%d len=%d\n",
5009 idstr
, instance_id
, version_id
, record_len
);
5011 cur_pos
= qemu_ftell(f
);
5012 se
= find_se(idstr
, instance_id
);
5014 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
5015 instance_id
, idstr
);
5017 ret
= se
->load_state(f
, se
->opaque
, version_id
);
5019 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
5020 instance_id
, idstr
);
5023 /* always seek to exact end of record */
5024 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
5031 /* device can contain snapshots */
5032 static int bdrv_can_snapshot(BlockDriverState
*bs
)
5035 !bdrv_is_removable(bs
) &&
5036 !bdrv_is_read_only(bs
));
5039 /* device must be snapshots in order to have a reliable snapshot */
5040 static int bdrv_has_snapshot(BlockDriverState
*bs
)
5043 !bdrv_is_removable(bs
) &&
5044 !bdrv_is_read_only(bs
));
5047 static BlockDriverState
*get_bs_snapshots(void)
5049 BlockDriverState
*bs
;
5053 return bs_snapshots
;
5054 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5056 if (bdrv_can_snapshot(bs
))
5065 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
5068 QEMUSnapshotInfo
*sn_tab
, *sn
;
5072 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5075 for(i
= 0; i
< nb_sns
; i
++) {
5077 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
5087 void do_savevm(const char *name
)
5089 BlockDriverState
*bs
, *bs1
;
5090 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
5091 int must_delete
, ret
, i
;
5092 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5094 int saved_vm_running
;
5101 bs
= get_bs_snapshots();
5103 term_printf("No block device can accept snapshots\n");
5107 /* ??? Should this occur after vm_stop? */
5110 saved_vm_running
= vm_running
;
5115 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
5120 memset(sn
, 0, sizeof(*sn
));
5122 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
5123 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
5126 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
5129 /* fill auxiliary fields */
5132 sn
->date_sec
= tb
.time
;
5133 sn
->date_nsec
= tb
.millitm
* 1000000;
5135 gettimeofday(&tv
, NULL
);
5136 sn
->date_sec
= tv
.tv_sec
;
5137 sn
->date_nsec
= tv
.tv_usec
* 1000;
5139 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
5141 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5142 term_printf("Device %s does not support VM state snapshots\n",
5143 bdrv_get_device_name(bs
));
5147 /* save the VM state */
5148 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
5150 term_printf("Could not open VM state file\n");
5153 ret
= qemu_savevm_state(f
);
5154 sn
->vm_state_size
= qemu_ftell(f
);
5157 term_printf("Error %d while writing VM\n", ret
);
5161 /* create the snapshots */
5163 for(i
= 0; i
< MAX_DISKS
; i
++) {
5165 if (bdrv_has_snapshot(bs1
)) {
5167 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
5169 term_printf("Error while deleting snapshot on '%s'\n",
5170 bdrv_get_device_name(bs1
));
5173 ret
= bdrv_snapshot_create(bs1
, sn
);
5175 term_printf("Error while creating snapshot on '%s'\n",
5176 bdrv_get_device_name(bs1
));
5182 if (saved_vm_running
)
5186 void do_loadvm(const char *name
)
5188 BlockDriverState
*bs
, *bs1
;
5189 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5192 int saved_vm_running
;
5194 bs
= get_bs_snapshots();
5196 term_printf("No block device supports snapshots\n");
5200 /* Flush all IO requests so they don't interfere with the new state. */
5203 saved_vm_running
= vm_running
;
5206 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5208 if (bdrv_has_snapshot(bs1
)) {
5209 ret
= bdrv_snapshot_goto(bs1
, name
);
5212 term_printf("Warning: ");
5215 term_printf("Snapshots not supported on device '%s'\n",
5216 bdrv_get_device_name(bs1
));
5219 term_printf("Could not find snapshot '%s' on device '%s'\n",
5220 name
, bdrv_get_device_name(bs1
));
5223 term_printf("Error %d while activating snapshot on '%s'\n",
5224 ret
, bdrv_get_device_name(bs1
));
5227 /* fatal on snapshot block device */
5234 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5235 term_printf("Device %s does not support VM state snapshots\n",
5236 bdrv_get_device_name(bs
));
5240 /* restore the VM state */
5241 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
5243 term_printf("Could not open VM state file\n");
5246 ret
= qemu_loadvm_state(f
);
5249 term_printf("Error %d while loading VM state\n", ret
);
5252 if (saved_vm_running
)
5256 void do_delvm(const char *name
)
5258 BlockDriverState
*bs
, *bs1
;
5261 bs
= get_bs_snapshots();
5263 term_printf("No block device supports snapshots\n");
5267 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5269 if (bdrv_has_snapshot(bs1
)) {
5270 ret
= bdrv_snapshot_delete(bs1
, name
);
5272 if (ret
== -ENOTSUP
)
5273 term_printf("Snapshots not supported on device '%s'\n",
5274 bdrv_get_device_name(bs1
));
5276 term_printf("Error %d while deleting snapshot on '%s'\n",
5277 ret
, bdrv_get_device_name(bs1
));
5283 void do_info_snapshots(void)
5285 BlockDriverState
*bs
, *bs1
;
5286 QEMUSnapshotInfo
*sn_tab
, *sn
;
5290 bs
= get_bs_snapshots();
5292 term_printf("No available block device supports snapshots\n");
5295 term_printf("Snapshot devices:");
5296 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5298 if (bdrv_has_snapshot(bs1
)) {
5300 term_printf(" %s", bdrv_get_device_name(bs1
));
5305 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5307 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
5310 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
5311 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
5312 for(i
= 0; i
< nb_sns
; i
++) {
5314 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
5319 /***********************************************************/
5320 /* cpu save/restore */
5322 #if defined(TARGET_I386)
5324 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
5326 qemu_put_be32(f
, dt
->selector
);
5327 qemu_put_betl(f
, dt
->base
);
5328 qemu_put_be32(f
, dt
->limit
);
5329 qemu_put_be32(f
, dt
->flags
);
5332 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
5334 dt
->selector
= qemu_get_be32(f
);
5335 dt
->base
= qemu_get_betl(f
);
5336 dt
->limit
= qemu_get_be32(f
);
5337 dt
->flags
= qemu_get_be32(f
);
5340 void cpu_save(QEMUFile
*f
, void *opaque
)
5342 CPUState
*env
= opaque
;
5343 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
5347 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5348 qemu_put_betls(f
, &env
->regs
[i
]);
5349 qemu_put_betls(f
, &env
->eip
);
5350 qemu_put_betls(f
, &env
->eflags
);
5351 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
5352 qemu_put_be32s(f
, &hflags
);
5356 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
5358 for(i
= 0; i
< 8; i
++) {
5359 fptag
|= ((!env
->fptags
[i
]) << i
);
5362 qemu_put_be16s(f
, &fpuc
);
5363 qemu_put_be16s(f
, &fpus
);
5364 qemu_put_be16s(f
, &fptag
);
5366 #ifdef USE_X86LDOUBLE
5371 qemu_put_be16s(f
, &fpregs_format
);
5373 for(i
= 0; i
< 8; i
++) {
5374 #ifdef USE_X86LDOUBLE
5378 /* we save the real CPU data (in case of MMX usage only 'mant'
5379 contains the MMX register */
5380 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
5381 qemu_put_be64(f
, mant
);
5382 qemu_put_be16(f
, exp
);
5385 /* if we use doubles for float emulation, we save the doubles to
5386 avoid losing information in case of MMX usage. It can give
5387 problems if the image is restored on a CPU where long
5388 doubles are used instead. */
5389 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
5393 for(i
= 0; i
< 6; i
++)
5394 cpu_put_seg(f
, &env
->segs
[i
]);
5395 cpu_put_seg(f
, &env
->ldt
);
5396 cpu_put_seg(f
, &env
->tr
);
5397 cpu_put_seg(f
, &env
->gdt
);
5398 cpu_put_seg(f
, &env
->idt
);
5400 qemu_put_be32s(f
, &env
->sysenter_cs
);
5401 qemu_put_be32s(f
, &env
->sysenter_esp
);
5402 qemu_put_be32s(f
, &env
->sysenter_eip
);
5404 qemu_put_betls(f
, &env
->cr
[0]);
5405 qemu_put_betls(f
, &env
->cr
[2]);
5406 qemu_put_betls(f
, &env
->cr
[3]);
5407 qemu_put_betls(f
, &env
->cr
[4]);
5409 for(i
= 0; i
< 8; i
++)
5410 qemu_put_betls(f
, &env
->dr
[i
]);
5413 qemu_put_be32s(f
, &env
->a20_mask
);
5416 qemu_put_be32s(f
, &env
->mxcsr
);
5417 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5418 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5419 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5422 #ifdef TARGET_X86_64
5423 qemu_put_be64s(f
, &env
->efer
);
5424 qemu_put_be64s(f
, &env
->star
);
5425 qemu_put_be64s(f
, &env
->lstar
);
5426 qemu_put_be64s(f
, &env
->cstar
);
5427 qemu_put_be64s(f
, &env
->fmask
);
5428 qemu_put_be64s(f
, &env
->kernelgsbase
);
5430 qemu_put_be32s(f
, &env
->smbase
);
5433 #ifdef USE_X86LDOUBLE
5434 /* XXX: add that in a FPU generic layer */
5435 union x86_longdouble
{
5440 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
5441 #define EXPBIAS1 1023
5442 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
5443 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
5445 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
5449 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
5450 /* exponent + sign */
5451 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
5452 e
|= SIGND1(temp
) >> 16;
5457 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5459 CPUState
*env
= opaque
;
5462 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
5464 if (version_id
!= 3 && version_id
!= 4)
5466 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5467 qemu_get_betls(f
, &env
->regs
[i
]);
5468 qemu_get_betls(f
, &env
->eip
);
5469 qemu_get_betls(f
, &env
->eflags
);
5470 qemu_get_be32s(f
, &hflags
);
5472 qemu_get_be16s(f
, &fpuc
);
5473 qemu_get_be16s(f
, &fpus
);
5474 qemu_get_be16s(f
, &fptag
);
5475 qemu_get_be16s(f
, &fpregs_format
);
5477 /* NOTE: we cannot always restore the FPU state if the image come
5478 from a host with a different 'USE_X86LDOUBLE' define. We guess
5479 if we are in an MMX state to restore correctly in that case. */
5480 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
5481 for(i
= 0; i
< 8; i
++) {
5485 switch(fpregs_format
) {
5487 mant
= qemu_get_be64(f
);
5488 exp
= qemu_get_be16(f
);
5489 #ifdef USE_X86LDOUBLE
5490 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5492 /* difficult case */
5494 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5496 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5500 mant
= qemu_get_be64(f
);
5501 #ifdef USE_X86LDOUBLE
5503 union x86_longdouble
*p
;
5504 /* difficult case */
5505 p
= (void *)&env
->fpregs
[i
];
5510 fp64_to_fp80(p
, mant
);
5514 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5523 /* XXX: restore FPU round state */
5524 env
->fpstt
= (fpus
>> 11) & 7;
5525 env
->fpus
= fpus
& ~0x3800;
5527 for(i
= 0; i
< 8; i
++) {
5528 env
->fptags
[i
] = (fptag
>> i
) & 1;
5531 for(i
= 0; i
< 6; i
++)
5532 cpu_get_seg(f
, &env
->segs
[i
]);
5533 cpu_get_seg(f
, &env
->ldt
);
5534 cpu_get_seg(f
, &env
->tr
);
5535 cpu_get_seg(f
, &env
->gdt
);
5536 cpu_get_seg(f
, &env
->idt
);
5538 qemu_get_be32s(f
, &env
->sysenter_cs
);
5539 qemu_get_be32s(f
, &env
->sysenter_esp
);
5540 qemu_get_be32s(f
, &env
->sysenter_eip
);
5542 qemu_get_betls(f
, &env
->cr
[0]);
5543 qemu_get_betls(f
, &env
->cr
[2]);
5544 qemu_get_betls(f
, &env
->cr
[3]);
5545 qemu_get_betls(f
, &env
->cr
[4]);
5547 for(i
= 0; i
< 8; i
++)
5548 qemu_get_betls(f
, &env
->dr
[i
]);
5551 qemu_get_be32s(f
, &env
->a20_mask
);
5553 qemu_get_be32s(f
, &env
->mxcsr
);
5554 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5555 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5556 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5559 #ifdef TARGET_X86_64
5560 qemu_get_be64s(f
, &env
->efer
);
5561 qemu_get_be64s(f
, &env
->star
);
5562 qemu_get_be64s(f
, &env
->lstar
);
5563 qemu_get_be64s(f
, &env
->cstar
);
5564 qemu_get_be64s(f
, &env
->fmask
);
5565 qemu_get_be64s(f
, &env
->kernelgsbase
);
5567 if (version_id
>= 4)
5568 qemu_get_be32s(f
, &env
->smbase
);
5570 /* XXX: compute hflags from scratch, except for CPL and IIF */
5571 env
->hflags
= hflags
;
5576 #elif defined(TARGET_PPC)
5577 void cpu_save(QEMUFile
*f
, void *opaque
)
5581 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5586 #elif defined(TARGET_MIPS)
5587 void cpu_save(QEMUFile
*f
, void *opaque
)
5591 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5596 #elif defined(TARGET_SPARC)
5597 void cpu_save(QEMUFile
*f
, void *opaque
)
5599 CPUState
*env
= opaque
;
5603 for(i
= 0; i
< 8; i
++)
5604 qemu_put_betls(f
, &env
->gregs
[i
]);
5605 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5606 qemu_put_betls(f
, &env
->regbase
[i
]);
5609 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5615 qemu_put_be32(f
, u
.i
);
5618 qemu_put_betls(f
, &env
->pc
);
5619 qemu_put_betls(f
, &env
->npc
);
5620 qemu_put_betls(f
, &env
->y
);
5622 qemu_put_be32(f
, tmp
);
5623 qemu_put_betls(f
, &env
->fsr
);
5624 qemu_put_betls(f
, &env
->tbr
);
5625 #ifndef TARGET_SPARC64
5626 qemu_put_be32s(f
, &env
->wim
);
5628 for(i
= 0; i
< 16; i
++)
5629 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
5633 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5635 CPUState
*env
= opaque
;
5639 for(i
= 0; i
< 8; i
++)
5640 qemu_get_betls(f
, &env
->gregs
[i
]);
5641 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5642 qemu_get_betls(f
, &env
->regbase
[i
]);
5645 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5650 u
.i
= qemu_get_be32(f
);
5654 qemu_get_betls(f
, &env
->pc
);
5655 qemu_get_betls(f
, &env
->npc
);
5656 qemu_get_betls(f
, &env
->y
);
5657 tmp
= qemu_get_be32(f
);
5658 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
5659 correctly updated */
5661 qemu_get_betls(f
, &env
->fsr
);
5662 qemu_get_betls(f
, &env
->tbr
);
5663 #ifndef TARGET_SPARC64
5664 qemu_get_be32s(f
, &env
->wim
);
5666 for(i
= 0; i
< 16; i
++)
5667 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
5673 #elif defined(TARGET_ARM)
5675 /* ??? Need to implement these. */
5676 void cpu_save(QEMUFile
*f
, void *opaque
)
5680 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5687 #warning No CPU save/restore functions
5691 /***********************************************************/
5692 /* ram save/restore */
5694 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
5698 v
= qemu_get_byte(f
);
5701 if (qemu_get_buffer(f
, buf
, len
) != len
)
5705 v
= qemu_get_byte(f
);
5706 memset(buf
, v
, len
);
5714 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
5718 if (qemu_get_be32(f
) != phys_ram_size
)
5720 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
5721 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
5728 #define BDRV_HASH_BLOCK_SIZE 1024
5729 #define IOBUF_SIZE 4096
5730 #define RAM_CBLOCK_MAGIC 0xfabe
5732 typedef struct RamCompressState
{
5735 uint8_t buf
[IOBUF_SIZE
];
5738 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
5741 memset(s
, 0, sizeof(*s
));
5743 ret
= deflateInit2(&s
->zstream
, 1,
5745 9, Z_DEFAULT_STRATEGY
);
5748 s
->zstream
.avail_out
= IOBUF_SIZE
;
5749 s
->zstream
.next_out
= s
->buf
;
5753 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
5755 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
5756 qemu_put_be16(s
->f
, len
);
5757 qemu_put_buffer(s
->f
, buf
, len
);
5760 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
5764 s
->zstream
.avail_in
= len
;
5765 s
->zstream
.next_in
= (uint8_t *)buf
;
5766 while (s
->zstream
.avail_in
> 0) {
5767 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
5770 if (s
->zstream
.avail_out
== 0) {
5771 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
5772 s
->zstream
.avail_out
= IOBUF_SIZE
;
5773 s
->zstream
.next_out
= s
->buf
;
5779 static void ram_compress_close(RamCompressState
*s
)
5783 /* compress last bytes */
5785 ret
= deflate(&s
->zstream
, Z_FINISH
);
5786 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
5787 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
5789 ram_put_cblock(s
, s
->buf
, len
);
5791 s
->zstream
.avail_out
= IOBUF_SIZE
;
5792 s
->zstream
.next_out
= s
->buf
;
5793 if (ret
== Z_STREAM_END
)
5800 deflateEnd(&s
->zstream
);
5803 typedef struct RamDecompressState
{
5806 uint8_t buf
[IOBUF_SIZE
];
5807 } RamDecompressState
;
5809 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
5812 memset(s
, 0, sizeof(*s
));
5814 ret
= inflateInit(&s
->zstream
);
5820 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
5824 s
->zstream
.avail_out
= len
;
5825 s
->zstream
.next_out
= buf
;
5826 while (s
->zstream
.avail_out
> 0) {
5827 if (s
->zstream
.avail_in
== 0) {
5828 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
5830 clen
= qemu_get_be16(s
->f
);
5831 if (clen
> IOBUF_SIZE
)
5833 qemu_get_buffer(s
->f
, s
->buf
, clen
);
5834 s
->zstream
.avail_in
= clen
;
5835 s
->zstream
.next_in
= s
->buf
;
5837 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
5838 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
5845 static void ram_decompress_close(RamDecompressState
*s
)
5847 inflateEnd(&s
->zstream
);
5850 static void ram_save(QEMUFile
*f
, void *opaque
)
5853 RamCompressState s1
, *s
= &s1
;
5856 qemu_put_be32(f
, phys_ram_size
);
5857 if (ram_compress_open(s
, f
) < 0)
5859 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5861 if (tight_savevm_enabled
) {
5865 /* find if the memory block is available on a virtual
5868 for(j
= 0; j
< MAX_DISKS
; j
++) {
5870 sector_num
= bdrv_hash_find(bs_table
[j
],
5871 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5872 if (sector_num
>= 0)
5877 goto normal_compress
;
5880 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
5881 ram_compress_buf(s
, buf
, 10);
5887 ram_compress_buf(s
, buf
, 1);
5888 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5891 ram_compress_close(s
);
5894 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
5896 RamDecompressState s1
, *s
= &s1
;
5900 if (version_id
== 1)
5901 return ram_load_v1(f
, opaque
);
5902 if (version_id
!= 2)
5904 if (qemu_get_be32(f
) != phys_ram_size
)
5906 if (ram_decompress_open(s
, f
) < 0)
5908 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5909 if (ram_decompress_buf(s
, buf
, 1) < 0) {
5910 fprintf(stderr
, "Error while reading ram block header\n");
5914 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
5915 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
5924 ram_decompress_buf(s
, buf
+ 1, 9);
5926 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
5927 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
5928 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
5931 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
5932 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
5933 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
5934 bs_index
, sector_num
);
5941 printf("Error block header\n");
5945 ram_decompress_close(s
);
5949 /***********************************************************/
5950 /* bottom halves (can be seen as timers which expire ASAP) */
5959 static QEMUBH
*first_bh
= NULL
;
5961 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
5964 bh
= qemu_mallocz(sizeof(QEMUBH
));
5968 bh
->opaque
= opaque
;
5972 int qemu_bh_poll(void)
5991 void qemu_bh_schedule(QEMUBH
*bh
)
5993 CPUState
*env
= cpu_single_env
;
5997 bh
->next
= first_bh
;
6000 /* stop the currently executing CPU to execute the BH ASAP */
6002 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
6006 void qemu_bh_cancel(QEMUBH
*bh
)
6009 if (bh
->scheduled
) {
6012 pbh
= &(*pbh
)->next
;
6018 void qemu_bh_delete(QEMUBH
*bh
)
6024 /***********************************************************/
6025 /* machine registration */
6027 QEMUMachine
*first_machine
= NULL
;
6029 int qemu_register_machine(QEMUMachine
*m
)
6032 pm
= &first_machine
;
6040 QEMUMachine
*find_machine(const char *name
)
6044 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6045 if (!strcmp(m
->name
, name
))
6051 /***********************************************************/
6052 /* main execution loop */
6054 void gui_update(void *opaque
)
6056 display_state
.dpy_refresh(&display_state
);
6057 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
6060 struct vm_change_state_entry
{
6061 VMChangeStateHandler
*cb
;
6063 LIST_ENTRY (vm_change_state_entry
) entries
;
6066 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
6068 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
6071 VMChangeStateEntry
*e
;
6073 e
= qemu_mallocz(sizeof (*e
));
6079 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
6083 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
6085 LIST_REMOVE (e
, entries
);
6089 static void vm_state_notify(int running
)
6091 VMChangeStateEntry
*e
;
6093 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
6094 e
->cb(e
->opaque
, running
);
6098 /* XXX: support several handlers */
6099 static VMStopHandler
*vm_stop_cb
;
6100 static void *vm_stop_opaque
;
6102 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6105 vm_stop_opaque
= opaque
;
6109 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6123 void vm_stop(int reason
)
6126 cpu_disable_ticks();
6130 vm_stop_cb(vm_stop_opaque
, reason
);
6137 /* reset/shutdown handler */
6139 typedef struct QEMUResetEntry
{
6140 QEMUResetHandler
*func
;
6142 struct QEMUResetEntry
*next
;
6145 static QEMUResetEntry
*first_reset_entry
;
6146 static int reset_requested
;
6147 static int shutdown_requested
;
6148 static int powerdown_requested
;
6150 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
6152 QEMUResetEntry
**pre
, *re
;
6154 pre
= &first_reset_entry
;
6155 while (*pre
!= NULL
)
6156 pre
= &(*pre
)->next
;
6157 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
6159 re
->opaque
= opaque
;
6164 static void qemu_system_reset(void)
6168 /* reset all devices */
6169 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
6170 re
->func(re
->opaque
);
6174 void qemu_system_reset_request(void)
6177 shutdown_requested
= 1;
6179 reset_requested
= 1;
6182 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6185 void qemu_system_shutdown_request(void)
6187 shutdown_requested
= 1;
6189 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6192 void qemu_system_powerdown_request(void)
6194 powerdown_requested
= 1;
6196 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6199 void main_loop_wait(int timeout
)
6201 IOHandlerRecord
*ioh
;
6202 fd_set rfds
, wfds
, xfds
;
6211 /* XXX: need to suppress polling by better using win32 events */
6213 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
6214 ret
|= pe
->func(pe
->opaque
);
6219 WaitObjects
*w
= &wait_objects
;
6221 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
6222 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
6223 if (w
->func
[ret
- WAIT_OBJECT_0
])
6224 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
6226 /* Check for additional signaled events */
6227 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
6229 /* Check if event is signaled */
6230 ret2
= WaitForSingleObject(w
->events
[i
], 0);
6231 if(ret2
== WAIT_OBJECT_0
) {
6233 w
->func
[i
](w
->opaque
[i
]);
6234 } else if (ret2
== WAIT_TIMEOUT
) {
6236 err
= GetLastError();
6237 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
6240 } else if (ret
== WAIT_TIMEOUT
) {
6242 err
= GetLastError();
6243 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
6247 /* poll any events */
6248 /* XXX: separate device handlers from system ones */
6253 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6257 (!ioh
->fd_read_poll
||
6258 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
6259 FD_SET(ioh
->fd
, &rfds
);
6263 if (ioh
->fd_write
) {
6264 FD_SET(ioh
->fd
, &wfds
);
6274 tv
.tv_usec
= timeout
* 1000;
6276 #if defined(CONFIG_SLIRP)
6278 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
6281 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
6283 IOHandlerRecord
**pioh
;
6285 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6288 if (FD_ISSET(ioh
->fd
, &rfds
)) {
6289 ioh
->fd_read(ioh
->opaque
);
6291 if (FD_ISSET(ioh
->fd
, &wfds
)) {
6292 ioh
->fd_write(ioh
->opaque
);
6296 /* remove deleted IO handlers */
6297 pioh
= &first_io_handler
;
6307 #if defined(CONFIG_SLIRP)
6314 slirp_select_poll(&rfds
, &wfds
, &xfds
);
6321 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
6322 qemu_get_clock(vm_clock
));
6323 /* run dma transfers, if any */
6327 /* real time timers */
6328 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
6329 qemu_get_clock(rt_clock
));
6332 static CPUState
*cur_cpu
;
6337 #ifdef CONFIG_PROFILER
6342 cur_cpu
= first_cpu
;
6349 env
= env
->next_cpu
;
6352 #ifdef CONFIG_PROFILER
6353 ti
= profile_getclock();
6355 ret
= cpu_exec(env
);
6356 #ifdef CONFIG_PROFILER
6357 qemu_time
+= profile_getclock() - ti
;
6359 if (ret
== EXCP_HLT
) {
6360 /* Give the next CPU a chance to run. */
6364 if (ret
!= EXCP_HALTED
)
6366 /* all CPUs are halted ? */
6372 if (shutdown_requested
) {
6373 ret
= EXCP_INTERRUPT
;
6376 if (reset_requested
) {
6377 reset_requested
= 0;
6378 qemu_system_reset();
6379 ret
= EXCP_INTERRUPT
;
6381 if (powerdown_requested
) {
6382 powerdown_requested
= 0;
6383 qemu_system_powerdown();
6384 ret
= EXCP_INTERRUPT
;
6386 if (ret
== EXCP_DEBUG
) {
6387 vm_stop(EXCP_DEBUG
);
6389 /* If all cpus are halted then wait until the next IRQ */
6390 /* XXX: use timeout computed from timers */
6391 if (ret
== EXCP_HALTED
)
6398 #ifdef CONFIG_PROFILER
6399 ti
= profile_getclock();
6401 main_loop_wait(timeout
);
6402 #ifdef CONFIG_PROFILER
6403 dev_time
+= profile_getclock() - ti
;
6406 cpu_disable_ticks();
6412 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2007 Fabrice Bellard\n"
6413 "usage: %s [options] [disk_image]\n"
6415 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
6417 "Standard options:\n"
6418 "-M machine select emulated machine (-M ? for list)\n"
6419 "-cpu cpu select CPU (-cpu ? for list)\n"
6420 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
6421 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
6422 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
6423 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
6424 "-mtdblock file use 'file' as on-board Flash memory image\n"
6425 "-sd file use 'file' as SecureDigital card image\n"
6426 "-pflash file use 'file' as a parallel flash image\n"
6427 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
6428 "-snapshot write to temporary files instead of disk image files\n"
6430 "-no-frame open SDL window without a frame and window decorations\n"
6431 "-no-quit disable SDL window close capability\n"
6434 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
6436 "-m megs set virtual RAM size to megs MB [default=%d]\n"
6437 "-smp n set the number of CPUs to 'n' [default=1]\n"
6438 "-nographic disable graphical output and redirect serial I/Os to console\n"
6439 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
6441 "-k language use keyboard layout (for example \"fr\" for French)\n"
6444 "-audio-help print list of audio drivers and their options\n"
6445 "-soundhw c1,... enable audio support\n"
6446 " and only specified sound cards (comma separated list)\n"
6447 " use -soundhw ? to get the list of supported cards\n"
6448 " use -soundhw all to enable all of them\n"
6450 "-localtime set the real time clock to local time [default=utc]\n"
6451 "-full-screen start in full screen\n"
6453 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
6455 "-usb enable the USB driver (will be the default soon)\n"
6456 "-usbdevice name add the host or guest USB device 'name'\n"
6457 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6458 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
6460 "-name string set the name of the guest\n"
6462 "Network options:\n"
6463 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
6464 " create a new Network Interface Card and connect it to VLAN 'n'\n"
6466 "-net user[,vlan=n][,hostname=host]\n"
6467 " connect the user mode network stack to VLAN 'n' and send\n"
6468 " hostname 'host' to DHCP clients\n"
6471 "-net tap[,vlan=n],ifname=name\n"
6472 " connect the host TAP network interface to VLAN 'n'\n"
6474 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
6475 " connect the host TAP network interface to VLAN 'n' and use\n"
6476 " the network script 'file' (default=%s);\n"
6477 " use 'script=no' to disable script execution;\n"
6478 " use 'fd=h' to connect to an already opened TAP interface\n"
6480 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
6481 " connect the vlan 'n' to another VLAN using a socket connection\n"
6482 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
6483 " connect the vlan 'n' to multicast maddr and port\n"
6484 "-net none use it alone to have zero network devices; if no -net option\n"
6485 " is provided, the default is '-net nic -net user'\n"
6488 "-tftp dir allow tftp access to files in dir [-net user]\n"
6489 "-bootp file advertise file in BOOTP replies\n"
6491 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
6493 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
6494 " redirect TCP or UDP connections from host to guest [-net user]\n"
6497 "Linux boot specific:\n"
6498 "-kernel bzImage use 'bzImage' as kernel image\n"
6499 "-append cmdline use 'cmdline' as kernel command line\n"
6500 "-initrd file use 'file' as initial ram disk\n"
6502 "Debug/Expert options:\n"
6503 "-monitor dev redirect the monitor to char device 'dev'\n"
6504 "-serial dev redirect the serial port to char device 'dev'\n"
6505 "-parallel dev redirect the parallel port to char device 'dev'\n"
6506 "-pidfile file Write PID to 'file'\n"
6507 "-S freeze CPU at startup (use 'c' to start execution)\n"
6508 "-s wait gdb connection to port\n"
6509 "-p port set gdb connection port [default=%s]\n"
6510 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
6511 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
6512 " translation (t=none or lba) (usually qemu can guess them)\n"
6513 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
6515 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
6516 "-no-kqemu disable KQEMU kernel module usage\n"
6518 #ifdef USE_CODE_COPY
6519 "-no-code-copy disable code copy acceleration\n"
6522 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
6523 " (default is CL-GD5446 PCI VGA)\n"
6524 "-no-acpi disable ACPI\n"
6526 "-no-reboot exit instead of rebooting\n"
6527 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
6528 "-vnc display start a VNC server on display\n"
6530 "-daemonize daemonize QEMU after initializing\n"
6532 "-option-rom rom load a file, rom, into the option ROM space\n"
6534 "-prom-env variable=value set OpenBIOS nvram variables\n"
6537 "During emulation, the following keys are useful:\n"
6538 "ctrl-alt-f toggle full screen\n"
6539 "ctrl-alt-n switch to virtual console 'n'\n"
6540 "ctrl-alt toggle mouse and keyboard grab\n"
6542 "When using -nographic, press 'ctrl-a h' to get some help.\n"
6547 DEFAULT_NETWORK_SCRIPT
,
6549 DEFAULT_GDBSTUB_PORT
,
6554 #define HAS_ARG 0x0001
6568 QEMU_OPTION_mtdblock
,
6572 QEMU_OPTION_snapshot
,
6574 QEMU_OPTION_no_fd_bootchk
,
6577 QEMU_OPTION_nographic
,
6578 QEMU_OPTION_portrait
,
6580 QEMU_OPTION_audio_help
,
6581 QEMU_OPTION_soundhw
,
6600 QEMU_OPTION_no_code_copy
,
6602 QEMU_OPTION_localtime
,
6603 QEMU_OPTION_cirrusvga
,
6606 QEMU_OPTION_std_vga
,
6608 QEMU_OPTION_monitor
,
6610 QEMU_OPTION_parallel
,
6612 QEMU_OPTION_full_screen
,
6613 QEMU_OPTION_no_frame
,
6614 QEMU_OPTION_no_quit
,
6615 QEMU_OPTION_pidfile
,
6616 QEMU_OPTION_no_kqemu
,
6617 QEMU_OPTION_kernel_kqemu
,
6618 QEMU_OPTION_win2k_hack
,
6620 QEMU_OPTION_usbdevice
,
6623 QEMU_OPTION_no_acpi
,
6624 QEMU_OPTION_no_reboot
,
6625 QEMU_OPTION_show_cursor
,
6626 QEMU_OPTION_daemonize
,
6627 QEMU_OPTION_option_rom
,
6628 QEMU_OPTION_semihosting
,
6630 QEMU_OPTION_prom_env
,
6633 typedef struct QEMUOption
{
6639 const QEMUOption qemu_options
[] = {
6640 { "h", 0, QEMU_OPTION_h
},
6641 { "help", 0, QEMU_OPTION_h
},
6643 { "M", HAS_ARG
, QEMU_OPTION_M
},
6644 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
6645 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
6646 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
6647 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
6648 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
6649 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
6650 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
6651 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
6652 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
6653 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
6654 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
6655 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
6656 { "snapshot", 0, QEMU_OPTION_snapshot
},
6658 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
6660 { "m", HAS_ARG
, QEMU_OPTION_m
},
6661 { "nographic", 0, QEMU_OPTION_nographic
},
6662 { "portrait", 0, QEMU_OPTION_portrait
},
6663 { "k", HAS_ARG
, QEMU_OPTION_k
},
6665 { "audio-help", 0, QEMU_OPTION_audio_help
},
6666 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
6669 { "net", HAS_ARG
, QEMU_OPTION_net
},
6671 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
6672 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
6674 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
6676 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
6679 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
6680 { "append", HAS_ARG
, QEMU_OPTION_append
},
6681 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
6683 { "S", 0, QEMU_OPTION_S
},
6684 { "s", 0, QEMU_OPTION_s
},
6685 { "p", HAS_ARG
, QEMU_OPTION_p
},
6686 { "d", HAS_ARG
, QEMU_OPTION_d
},
6687 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
6688 { "L", HAS_ARG
, QEMU_OPTION_L
},
6689 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
6691 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
6692 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
6694 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6695 { "g", 1, QEMU_OPTION_g
},
6697 { "localtime", 0, QEMU_OPTION_localtime
},
6698 { "std-vga", 0, QEMU_OPTION_std_vga
},
6699 { "echr", 1, QEMU_OPTION_echr
},
6700 { "monitor", 1, QEMU_OPTION_monitor
},
6701 { "serial", 1, QEMU_OPTION_serial
},
6702 { "parallel", 1, QEMU_OPTION_parallel
},
6703 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
6704 { "full-screen", 0, QEMU_OPTION_full_screen
},
6706 { "no-frame", 0, QEMU_OPTION_no_frame
},
6707 { "no-quit", 0, QEMU_OPTION_no_quit
},
6709 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
6710 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
6711 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
6712 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
6713 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
6715 /* temporary options */
6716 { "usb", 0, QEMU_OPTION_usb
},
6717 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
6718 { "vmwarevga", 0, QEMU_OPTION_vmsvga
},
6719 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
6720 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
6721 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
6722 { "daemonize", 0, QEMU_OPTION_daemonize
},
6723 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
6724 #if defined(TARGET_ARM)
6725 { "semihosting", 0, QEMU_OPTION_semihosting
},
6727 { "name", HAS_ARG
, QEMU_OPTION_name
},
6728 #if defined(TARGET_SPARC)
6729 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
6734 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
6736 /* this stack is only used during signal handling */
6737 #define SIGNAL_STACK_SIZE 32768
6739 static uint8_t *signal_stack
;
6743 /* password input */
6745 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
6750 if (!bdrv_is_encrypted(bs
))
6753 term_printf("%s is encrypted.\n", name
);
6754 for(i
= 0; i
< 3; i
++) {
6755 monitor_readline("Password: ", 1, password
, sizeof(password
));
6756 if (bdrv_set_key(bs
, password
) == 0)
6758 term_printf("invalid password\n");
6763 static BlockDriverState
*get_bdrv(int index
)
6765 BlockDriverState
*bs
;
6768 bs
= bs_table
[index
];
6769 } else if (index
< 6) {
6770 bs
= fd_table
[index
- 4];
6777 static void read_passwords(void)
6779 BlockDriverState
*bs
;
6782 for(i
= 0; i
< 6; i
++) {
6785 qemu_key_check(bs
, bdrv_get_device_name(bs
));
6789 /* XXX: currently we cannot use simultaneously different CPUs */
6790 void register_machines(void)
6792 #if defined(TARGET_I386)
6793 qemu_register_machine(&pc_machine
);
6794 qemu_register_machine(&isapc_machine
);
6795 #elif defined(TARGET_PPC)
6796 qemu_register_machine(&heathrow_machine
);
6797 qemu_register_machine(&core99_machine
);
6798 qemu_register_machine(&prep_machine
);
6799 qemu_register_machine(&ref405ep_machine
);
6800 qemu_register_machine(&taihu_machine
);
6801 #elif defined(TARGET_MIPS)
6802 qemu_register_machine(&mips_machine
);
6803 qemu_register_machine(&mips_malta_machine
);
6804 qemu_register_machine(&mips_pica61_machine
);
6805 #elif defined(TARGET_SPARC)
6806 #ifdef TARGET_SPARC64
6807 qemu_register_machine(&sun4u_machine
);
6809 qemu_register_machine(&ss5_machine
);
6810 qemu_register_machine(&ss10_machine
);
6812 #elif defined(TARGET_ARM)
6813 qemu_register_machine(&integratorcp_machine
);
6814 qemu_register_machine(&versatilepb_machine
);
6815 qemu_register_machine(&versatileab_machine
);
6816 qemu_register_machine(&realview_machine
);
6817 qemu_register_machine(&akitapda_machine
);
6818 qemu_register_machine(&spitzpda_machine
);
6819 qemu_register_machine(&borzoipda_machine
);
6820 qemu_register_machine(&terrierpda_machine
);
6821 #elif defined(TARGET_SH4)
6822 qemu_register_machine(&shix_machine
);
6823 #elif defined(TARGET_ALPHA)
6826 #error unsupported CPU
6831 struct soundhw soundhw
[] = {
6832 #ifdef HAS_AUDIO_CHOICE
6839 { .init_isa
= pcspk_audio_init
}
6844 "Creative Sound Blaster 16",
6847 { .init_isa
= SB16_init
}
6854 "Yamaha YMF262 (OPL3)",
6856 "Yamaha YM3812 (OPL2)",
6860 { .init_isa
= Adlib_init
}
6867 "Gravis Ultrasound GF1",
6870 { .init_isa
= GUS_init
}
6876 "ENSONIQ AudioPCI ES1370",
6879 { .init_pci
= es1370_init
}
6883 { NULL
, NULL
, 0, 0, { NULL
} }
6886 static void select_soundhw (const char *optarg
)
6890 if (*optarg
== '?') {
6893 printf ("Valid sound card names (comma separated):\n");
6894 for (c
= soundhw
; c
->name
; ++c
) {
6895 printf ("%-11s %s\n", c
->name
, c
->descr
);
6897 printf ("\n-soundhw all will enable all of the above\n");
6898 exit (*optarg
!= '?');
6906 if (!strcmp (optarg
, "all")) {
6907 for (c
= soundhw
; c
->name
; ++c
) {
6915 e
= strchr (p
, ',');
6916 l
= !e
? strlen (p
) : (size_t) (e
- p
);
6918 for (c
= soundhw
; c
->name
; ++c
) {
6919 if (!strncmp (c
->name
, p
, l
)) {
6928 "Unknown sound card name (too big to show)\n");
6931 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
6936 p
+= l
+ (e
!= NULL
);
6940 goto show_valid_cards
;
6946 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
6948 exit(STATUS_CONTROL_C_EXIT
);
6953 #define MAX_NET_CLIENTS 32
6955 int main(int argc
, char **argv
)
6957 #ifdef CONFIG_GDBSTUB
6959 const char *gdbstub_port
;
6961 int i
, cdrom_index
, pflash_index
;
6962 int snapshot
, linux_boot
;
6963 const char *initrd_filename
;
6964 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
6965 const char *pflash_filename
[MAX_PFLASH
];
6966 const char *sd_filename
;
6967 const char *mtd_filename
;
6968 const char *kernel_filename
, *kernel_cmdline
;
6969 DisplayState
*ds
= &display_state
;
6970 int cyls
, heads
, secs
, translation
;
6971 char net_clients
[MAX_NET_CLIENTS
][256];
6974 const char *r
, *optarg
;
6975 CharDriverState
*monitor_hd
;
6976 char monitor_device
[128];
6977 char serial_devices
[MAX_SERIAL_PORTS
][128];
6978 int serial_device_index
;
6979 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
6980 int parallel_device_index
;
6981 const char *loadvm
= NULL
;
6982 QEMUMachine
*machine
;
6983 const char *cpu_model
;
6984 char usb_devices
[MAX_USB_CMDLINE
][128];
6985 int usb_devices_index
;
6987 const char *pid_file
= NULL
;
6989 LIST_INIT (&vm_change_state_head
);
6992 struct sigaction act
;
6993 sigfillset(&act
.sa_mask
);
6995 act
.sa_handler
= SIG_IGN
;
6996 sigaction(SIGPIPE
, &act
, NULL
);
6999 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
7000 /* Note: cpu_interrupt() is currently not SMP safe, so we force
7001 QEMU to run on a single CPU */
7006 h
= GetCurrentProcess();
7007 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
7008 for(i
= 0; i
< 32; i
++) {
7009 if (mask
& (1 << i
))
7014 SetProcessAffinityMask(h
, mask
);
7020 register_machines();
7021 machine
= first_machine
;
7023 initrd_filename
= NULL
;
7024 for(i
= 0; i
< MAX_FD
; i
++)
7025 fd_filename
[i
] = NULL
;
7026 for(i
= 0; i
< MAX_DISKS
; i
++)
7027 hd_filename
[i
] = NULL
;
7028 for(i
= 0; i
< MAX_PFLASH
; i
++)
7029 pflash_filename
[i
] = NULL
;
7032 mtd_filename
= NULL
;
7033 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
7034 vga_ram_size
= VGA_RAM_SIZE
;
7035 #ifdef CONFIG_GDBSTUB
7037 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
7041 kernel_filename
= NULL
;
7042 kernel_cmdline
= "";
7048 cyls
= heads
= secs
= 0;
7049 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7050 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
7052 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
7053 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
7054 serial_devices
[i
][0] = '\0';
7055 serial_device_index
= 0;
7057 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
7058 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
7059 parallel_devices
[i
][0] = '\0';
7060 parallel_device_index
= 0;
7062 usb_devices_index
= 0;
7067 /* default mac address of the first network interface */
7075 hd_filename
[0] = argv
[optind
++];
7077 const QEMUOption
*popt
;
7080 /* Treat --foo the same as -foo. */
7083 popt
= qemu_options
;
7086 fprintf(stderr
, "%s: invalid option -- '%s'\n",
7090 if (!strcmp(popt
->name
, r
+ 1))
7094 if (popt
->flags
& HAS_ARG
) {
7095 if (optind
>= argc
) {
7096 fprintf(stderr
, "%s: option '%s' requires an argument\n",
7100 optarg
= argv
[optind
++];
7105 switch(popt
->index
) {
7107 machine
= find_machine(optarg
);
7110 printf("Supported machines are:\n");
7111 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
7112 printf("%-10s %s%s\n",
7114 m
== first_machine
? " (default)" : "");
7119 case QEMU_OPTION_cpu
:
7120 /* hw initialization will check this */
7121 if (optarg
[0] == '?') {
7122 #if defined(TARGET_PPC)
7123 ppc_cpu_list(stdout
, &fprintf
);
7124 #elif defined(TARGET_ARM)
7126 #elif defined(TARGET_MIPS)
7127 mips_cpu_list(stdout
, &fprintf
);
7128 #elif defined(TARGET_SPARC)
7129 sparc_cpu_list(stdout
, &fprintf
);
7136 case QEMU_OPTION_initrd
:
7137 initrd_filename
= optarg
;
7139 case QEMU_OPTION_hda
:
7140 case QEMU_OPTION_hdb
:
7141 case QEMU_OPTION_hdc
:
7142 case QEMU_OPTION_hdd
:
7145 hd_index
= popt
->index
- QEMU_OPTION_hda
;
7146 hd_filename
[hd_index
] = optarg
;
7147 if (hd_index
== cdrom_index
)
7151 case QEMU_OPTION_mtdblock
:
7152 mtd_filename
= optarg
;
7154 case QEMU_OPTION_sd
:
7155 sd_filename
= optarg
;
7157 case QEMU_OPTION_pflash
:
7158 if (pflash_index
>= MAX_PFLASH
) {
7159 fprintf(stderr
, "qemu: too many parallel flash images\n");
7162 pflash_filename
[pflash_index
++] = optarg
;
7164 case QEMU_OPTION_snapshot
:
7167 case QEMU_OPTION_hdachs
:
7171 cyls
= strtol(p
, (char **)&p
, 0);
7172 if (cyls
< 1 || cyls
> 16383)
7177 heads
= strtol(p
, (char **)&p
, 0);
7178 if (heads
< 1 || heads
> 16)
7183 secs
= strtol(p
, (char **)&p
, 0);
7184 if (secs
< 1 || secs
> 63)
7188 if (!strcmp(p
, "none"))
7189 translation
= BIOS_ATA_TRANSLATION_NONE
;
7190 else if (!strcmp(p
, "lba"))
7191 translation
= BIOS_ATA_TRANSLATION_LBA
;
7192 else if (!strcmp(p
, "auto"))
7193 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7196 } else if (*p
!= '\0') {
7198 fprintf(stderr
, "qemu: invalid physical CHS format\n");
7203 case QEMU_OPTION_nographic
:
7204 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
7205 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "null");
7206 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
7209 case QEMU_OPTION_portrait
:
7212 case QEMU_OPTION_kernel
:
7213 kernel_filename
= optarg
;
7215 case QEMU_OPTION_append
:
7216 kernel_cmdline
= optarg
;
7218 case QEMU_OPTION_cdrom
:
7219 if (cdrom_index
>= 0) {
7220 hd_filename
[cdrom_index
] = optarg
;
7223 case QEMU_OPTION_boot
:
7224 boot_device
= optarg
[0];
7225 if (boot_device
!= 'a' &&
7226 #if defined(TARGET_SPARC) || defined(TARGET_I386)
7228 boot_device
!= 'n' &&
7230 boot_device
!= 'c' && boot_device
!= 'd') {
7231 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
7235 case QEMU_OPTION_fda
:
7236 fd_filename
[0] = optarg
;
7238 case QEMU_OPTION_fdb
:
7239 fd_filename
[1] = optarg
;
7242 case QEMU_OPTION_no_fd_bootchk
:
7246 case QEMU_OPTION_no_code_copy
:
7247 code_copy_enabled
= 0;
7249 case QEMU_OPTION_net
:
7250 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
7251 fprintf(stderr
, "qemu: too many network clients\n");
7254 pstrcpy(net_clients
[nb_net_clients
],
7255 sizeof(net_clients
[0]),
7260 case QEMU_OPTION_tftp
:
7261 tftp_prefix
= optarg
;
7263 case QEMU_OPTION_bootp
:
7264 bootp_filename
= optarg
;
7267 case QEMU_OPTION_smb
:
7268 net_slirp_smb(optarg
);
7271 case QEMU_OPTION_redir
:
7272 net_slirp_redir(optarg
);
7276 case QEMU_OPTION_audio_help
:
7280 case QEMU_OPTION_soundhw
:
7281 select_soundhw (optarg
);
7288 ram_size
= atoi(optarg
) * 1024 * 1024;
7291 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
7292 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
7293 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
7302 mask
= cpu_str_to_log_mask(optarg
);
7304 printf("Log items (comma separated):\n");
7305 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
7306 printf("%-10s %s\n", item
->name
, item
->help
);
7313 #ifdef CONFIG_GDBSTUB
7318 gdbstub_port
= optarg
;
7328 keyboard_layout
= optarg
;
7330 case QEMU_OPTION_localtime
:
7333 case QEMU_OPTION_cirrusvga
:
7334 cirrus_vga_enabled
= 1;
7337 case QEMU_OPTION_vmsvga
:
7338 cirrus_vga_enabled
= 0;
7341 case QEMU_OPTION_std_vga
:
7342 cirrus_vga_enabled
= 0;
7350 w
= strtol(p
, (char **)&p
, 10);
7353 fprintf(stderr
, "qemu: invalid resolution or depth\n");
7359 h
= strtol(p
, (char **)&p
, 10);
7364 depth
= strtol(p
, (char **)&p
, 10);
7365 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
7366 depth
!= 24 && depth
!= 32)
7368 } else if (*p
== '\0') {
7369 depth
= graphic_depth
;
7376 graphic_depth
= depth
;
7379 case QEMU_OPTION_echr
:
7382 term_escape_char
= strtol(optarg
, &r
, 0);
7384 printf("Bad argument to echr\n");
7387 case QEMU_OPTION_monitor
:
7388 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
7390 case QEMU_OPTION_serial
:
7391 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
7392 fprintf(stderr
, "qemu: too many serial ports\n");
7395 pstrcpy(serial_devices
[serial_device_index
],
7396 sizeof(serial_devices
[0]), optarg
);
7397 serial_device_index
++;
7399 case QEMU_OPTION_parallel
:
7400 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
7401 fprintf(stderr
, "qemu: too many parallel ports\n");
7404 pstrcpy(parallel_devices
[parallel_device_index
],
7405 sizeof(parallel_devices
[0]), optarg
);
7406 parallel_device_index
++;
7408 case QEMU_OPTION_loadvm
:
7411 case QEMU_OPTION_full_screen
:
7415 case QEMU_OPTION_no_frame
:
7418 case QEMU_OPTION_no_quit
:
7422 case QEMU_OPTION_pidfile
:
7426 case QEMU_OPTION_win2k_hack
:
7427 win2k_install_hack
= 1;
7431 case QEMU_OPTION_no_kqemu
:
7434 case QEMU_OPTION_kernel_kqemu
:
7438 case QEMU_OPTION_usb
:
7441 case QEMU_OPTION_usbdevice
:
7443 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
7444 fprintf(stderr
, "Too many USB devices\n");
7447 pstrcpy(usb_devices
[usb_devices_index
],
7448 sizeof(usb_devices
[usb_devices_index
]),
7450 usb_devices_index
++;
7452 case QEMU_OPTION_smp
:
7453 smp_cpus
= atoi(optarg
);
7454 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
7455 fprintf(stderr
, "Invalid number of CPUs\n");
7459 case QEMU_OPTION_vnc
:
7460 vnc_display
= optarg
;
7462 case QEMU_OPTION_no_acpi
:
7465 case QEMU_OPTION_no_reboot
:
7468 case QEMU_OPTION_show_cursor
:
7471 case QEMU_OPTION_daemonize
:
7474 case QEMU_OPTION_option_rom
:
7475 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
7476 fprintf(stderr
, "Too many option ROMs\n");
7479 option_rom
[nb_option_roms
] = optarg
;
7482 case QEMU_OPTION_semihosting
:
7483 semihosting_enabled
= 1;
7485 case QEMU_OPTION_name
:
7489 case QEMU_OPTION_prom_env
:
7490 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
7491 fprintf(stderr
, "Too many prom variables\n");
7494 prom_envs
[nb_prom_envs
] = optarg
;
7503 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
7504 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
7511 if (pipe(fds
) == -1)
7522 len
= read(fds
[0], &status
, 1);
7523 if (len
== -1 && (errno
== EINTR
))
7528 else if (status
== 1) {
7529 fprintf(stderr
, "Could not acquire pidfile\n");
7547 signal(SIGTSTP
, SIG_IGN
);
7548 signal(SIGTTOU
, SIG_IGN
);
7549 signal(SIGTTIN
, SIG_IGN
);
7553 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
7556 write(fds
[1], &status
, 1);
7558 fprintf(stderr
, "Could not acquire pid file\n");
7566 linux_boot
= (kernel_filename
!= NULL
);
7569 boot_device
!= 'n' &&
7570 hd_filename
[0] == '\0' &&
7571 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
7572 fd_filename
[0] == '\0')
7575 /* boot to floppy or the default cd if no hard disk defined yet */
7576 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
7577 if (fd_filename
[0] != '\0')
7583 setvbuf(stdout
, NULL
, _IOLBF
, 0);
7593 /* init network clients */
7594 if (nb_net_clients
== 0) {
7595 /* if no clients, we use a default config */
7596 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
7598 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
7603 for(i
= 0;i
< nb_net_clients
; i
++) {
7604 if (net_client_init(net_clients
[i
]) < 0)
7609 if (boot_device
== 'n') {
7610 for (i
= 0; i
< nb_nics
; i
++) {
7611 const char *model
= nd_table
[i
].model
;
7615 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
7616 if (get_image_size(buf
) > 0) {
7617 option_rom
[nb_option_roms
] = strdup(buf
);
7623 fprintf(stderr
, "No valid PXE rom found for network device\n");
7626 boot_device
= 'c'; /* to prevent confusion by the BIOS */
7630 /* init the memory */
7631 phys_ram_size
= ram_size
+ vga_ram_size
+ MAX_BIOS_SIZE
;
7633 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
7634 if (!phys_ram_base
) {
7635 fprintf(stderr
, "Could not allocate physical memory\n");
7639 /* we always create the cdrom drive, even if no disk is there */
7641 if (cdrom_index
>= 0) {
7642 bs_table
[cdrom_index
] = bdrv_new("cdrom");
7643 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
7646 /* open the virtual block devices */
7647 for(i
= 0; i
< MAX_DISKS
; i
++) {
7648 if (hd_filename
[i
]) {
7651 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
7652 bs_table
[i
] = bdrv_new(buf
);
7654 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7655 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
7659 if (i
== 0 && cyls
!= 0) {
7660 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
7661 bdrv_set_translation_hint(bs_table
[i
], translation
);
7666 /* we always create at least one floppy disk */
7667 fd_table
[0] = bdrv_new("fda");
7668 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
7670 for(i
= 0; i
< MAX_FD
; i
++) {
7671 if (fd_filename
[i
]) {
7674 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
7675 fd_table
[i
] = bdrv_new(buf
);
7676 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
7678 if (fd_filename
[i
][0] != '\0') {
7679 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
7680 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7681 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
7689 /* Open the virtual parallel flash block devices */
7690 for(i
= 0; i
< MAX_PFLASH
; i
++) {
7691 if (pflash_filename
[i
]) {
7692 if (!pflash_table
[i
]) {
7694 snprintf(buf
, sizeof(buf
), "fl%c", i
+ 'a');
7695 pflash_table
[i
] = bdrv_new(buf
);
7697 if (bdrv_open(pflash_table
[i
], pflash_filename
[i
],
7698 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7699 fprintf(stderr
, "qemu: could not open flash image '%s'\n",
7700 pflash_filename
[i
]);
7706 sd_bdrv
= bdrv_new ("sd");
7707 /* FIXME: This isn't really a floppy, but it's a reasonable
7709 bdrv_set_type_hint(sd_bdrv
, BDRV_TYPE_FLOPPY
);
7711 if (bdrv_open(sd_bdrv
, sd_filename
,
7712 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7713 fprintf(stderr
, "qemu: could not open SD card image %s\n",
7716 qemu_key_check(sd_bdrv
, sd_filename
);
7720 mtd_bdrv
= bdrv_new ("mtd");
7721 if (bdrv_open(mtd_bdrv
, mtd_filename
,
7722 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0 ||
7723 qemu_key_check(mtd_bdrv
, mtd_filename
)) {
7724 fprintf(stderr
, "qemu: could not open Flash image %s\n",
7726 bdrv_delete(mtd_bdrv
);
7731 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
7732 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
7738 dumb_display_init(ds
);
7739 } else if (vnc_display
!= NULL
) {
7740 vnc_display_init(ds
, vnc_display
);
7742 #if defined(CONFIG_SDL)
7743 sdl_display_init(ds
, full_screen
, no_frame
);
7744 #elif defined(CONFIG_COCOA)
7745 cocoa_display_init(ds
, full_screen
);
7747 dumb_display_init(ds
);
7751 /* Maintain compatibility with multiple stdio monitors */
7752 if (!strcmp(monitor_device
,"stdio")) {
7753 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7754 if (!strcmp(serial_devices
[i
],"mon:stdio")) {
7755 monitor_device
[0] = '\0';
7757 } else if (!strcmp(serial_devices
[i
],"stdio")) {
7758 monitor_device
[0] = '\0';
7759 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "mon:stdio");
7764 if (monitor_device
[0] != '\0') {
7765 monitor_hd
= qemu_chr_open(monitor_device
);
7767 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
7770 monitor_init(monitor_hd
, !nographic
);
7773 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7774 const char *devname
= serial_devices
[i
];
7775 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7776 serial_hds
[i
] = qemu_chr_open(devname
);
7777 if (!serial_hds
[i
]) {
7778 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
7782 if (!strcmp(devname
, "vc"))
7783 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
7787 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
7788 const char *devname
= parallel_devices
[i
];
7789 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7790 parallel_hds
[i
] = qemu_chr_open(devname
);
7791 if (!parallel_hds
[i
]) {
7792 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
7796 if (!strcmp(devname
, "vc"))
7797 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
7801 machine
->init(ram_size
, vga_ram_size
, boot_device
,
7802 ds
, fd_filename
, snapshot
,
7803 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
7805 /* init USB devices */
7807 for(i
= 0; i
< usb_devices_index
; i
++) {
7808 if (usb_device_add(usb_devices
[i
]) < 0) {
7809 fprintf(stderr
, "Warning: could not add USB device %s\n",
7815 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
7816 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
7818 #ifdef CONFIG_GDBSTUB
7820 /* XXX: use standard host:port notation and modify options
7822 if (gdbserver_start(gdbstub_port
) < 0) {
7823 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
7833 /* XXX: simplify init */
7846 len
= write(fds
[1], &status
, 1);
7847 if (len
== -1 && (errno
== EINTR
))
7853 fd
= open("/dev/null", O_RDWR
);