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>
45 #include <sys/select.h>
46 #include <arpa/inet.h>
52 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
53 #include <freebsd/stdlib.h>
57 #include <linux/if_tun.h>
60 #include <linux/rtc.h>
62 /* For the benefit of older linux systems which don't supply it,
63 we use a local copy of hpet.h. */
64 /* #include <linux/hpet.h> */
67 #include <linux/ppdev.h>
68 #include <linux/parport.h>
71 #include <sys/ethernet.h>
72 #include <sys/sockio.h>
73 #include <netinet/arp.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_icmp.h> // must come after ip.h
78 #include <netinet/udp.h>
79 #include <netinet/tcp.h>
87 int inet_aton(const char *cp
, struct in_addr
*ia
);
90 #if defined(CONFIG_SLIRP)
96 #include <sys/timeb.h>
98 #define getopt_long_only getopt_long
99 #define memalign(align, size) malloc(size)
102 #include "qemu_socket.h"
108 #endif /* CONFIG_SDL */
112 #define main qemu_main
113 #endif /* CONFIG_COCOA */
117 #include "exec-all.h"
119 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
120 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
122 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
124 #define SMBD_COMMAND "/usr/sbin/smbd"
127 //#define DEBUG_UNUSED_IOPORT
128 //#define DEBUG_IOPORT
130 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
133 #define DEFAULT_RAM_SIZE 144
135 #define DEFAULT_RAM_SIZE 128
138 #define GUI_REFRESH_INTERVAL 30
140 /* Max number of USB devices that can be specified on the commandline. */
141 #define MAX_USB_CMDLINE 8
143 /* XXX: use a two level table to limit memory usage */
144 #define MAX_IOPORTS 65536
146 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
147 const char *bios_name
= NULL
;
148 char phys_ram_file
[1024];
149 void *ioport_opaque
[MAX_IOPORTS
];
150 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
151 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
152 /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available
153 to store the VM snapshots */
154 BlockDriverState
*bs_table
[MAX_DISKS
+ 1], *fd_table
[MAX_FD
];
155 BlockDriverState
*pflash_table
[MAX_PFLASH
];
156 BlockDriverState
*sd_bdrv
;
157 BlockDriverState
*mtd_bdrv
;
158 /* point to the block driver where the snapshots are managed */
159 BlockDriverState
*bs_snapshots
;
161 static DisplayState display_state
;
163 const char* keyboard_layout
= NULL
;
164 int64_t ticks_per_sec
;
166 int pit_min_timer_count
= 0;
168 NICInfo nd_table
[MAX_NICS
];
171 int rtc_start_date
= -1; /* -1 means now */
172 int cirrus_vga_enabled
= 1;
173 int vmsvga_enabled
= 0;
175 int graphic_width
= 1024;
176 int graphic_height
= 768;
177 int graphic_depth
= 8;
179 int graphic_width
= 800;
180 int graphic_height
= 600;
181 int graphic_depth
= 15;
186 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
187 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
189 int win2k_install_hack
= 0;
192 static VLANState
*first_vlan
;
194 const char *vnc_display
;
195 #if defined(TARGET_SPARC)
197 #elif defined(TARGET_I386)
202 int acpi_enabled
= 1;
206 int graphic_rotate
= 0;
208 const char *option_rom
[MAX_OPTION_ROMS
];
210 int semihosting_enabled
= 0;
215 const char *qemu_name
;
218 unsigned int nb_prom_envs
= 0;
219 const char *prom_envs
[MAX_PROM_ENVS
];
222 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
224 /***********************************************************/
225 /* x86 ISA bus support */
227 target_phys_addr_t isa_mem_base
= 0;
230 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
232 #ifdef DEBUG_UNUSED_IOPORT
233 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
238 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
240 #ifdef DEBUG_UNUSED_IOPORT
241 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
245 /* default is to make two byte accesses */
246 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
249 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
250 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
251 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
255 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
257 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
258 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
259 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
262 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
264 #ifdef DEBUG_UNUSED_IOPORT
265 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
270 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
272 #ifdef DEBUG_UNUSED_IOPORT
273 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
277 void init_ioports(void)
281 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
282 ioport_read_table
[0][i
] = default_ioport_readb
;
283 ioport_write_table
[0][i
] = default_ioport_writeb
;
284 ioport_read_table
[1][i
] = default_ioport_readw
;
285 ioport_write_table
[1][i
] = default_ioport_writew
;
286 ioport_read_table
[2][i
] = default_ioport_readl
;
287 ioport_write_table
[2][i
] = default_ioport_writel
;
291 /* size is the word size in byte */
292 int register_ioport_read(int start
, int length
, int size
,
293 IOPortReadFunc
*func
, void *opaque
)
299 } else if (size
== 2) {
301 } else if (size
== 4) {
304 hw_error("register_ioport_read: invalid size");
307 for(i
= start
; i
< start
+ length
; i
+= size
) {
308 ioport_read_table
[bsize
][i
] = func
;
309 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
310 hw_error("register_ioport_read: invalid opaque");
311 ioport_opaque
[i
] = opaque
;
316 /* size is the word size in byte */
317 int register_ioport_write(int start
, int length
, int size
,
318 IOPortWriteFunc
*func
, void *opaque
)
324 } else if (size
== 2) {
326 } else if (size
== 4) {
329 hw_error("register_ioport_write: invalid size");
332 for(i
= start
; i
< start
+ length
; i
+= size
) {
333 ioport_write_table
[bsize
][i
] = func
;
334 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
335 hw_error("register_ioport_write: invalid opaque");
336 ioport_opaque
[i
] = opaque
;
341 void isa_unassign_ioport(int start
, int length
)
345 for(i
= start
; i
< start
+ length
; i
++) {
346 ioport_read_table
[0][i
] = default_ioport_readb
;
347 ioport_read_table
[1][i
] = default_ioport_readw
;
348 ioport_read_table
[2][i
] = default_ioport_readl
;
350 ioport_write_table
[0][i
] = default_ioport_writeb
;
351 ioport_write_table
[1][i
] = default_ioport_writew
;
352 ioport_write_table
[2][i
] = default_ioport_writel
;
356 /***********************************************************/
358 void cpu_outb(CPUState
*env
, int addr
, int val
)
361 if (loglevel
& CPU_LOG_IOPORT
)
362 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
364 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
367 env
->last_io_time
= cpu_get_time_fast();
371 void cpu_outw(CPUState
*env
, int addr
, int val
)
374 if (loglevel
& CPU_LOG_IOPORT
)
375 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
377 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
380 env
->last_io_time
= cpu_get_time_fast();
384 void cpu_outl(CPUState
*env
, int addr
, int val
)
387 if (loglevel
& CPU_LOG_IOPORT
)
388 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
390 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
393 env
->last_io_time
= cpu_get_time_fast();
397 int cpu_inb(CPUState
*env
, int addr
)
400 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
402 if (loglevel
& CPU_LOG_IOPORT
)
403 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
407 env
->last_io_time
= cpu_get_time_fast();
412 int cpu_inw(CPUState
*env
, int addr
)
415 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
417 if (loglevel
& CPU_LOG_IOPORT
)
418 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
422 env
->last_io_time
= cpu_get_time_fast();
427 int cpu_inl(CPUState
*env
, int addr
)
430 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
432 if (loglevel
& CPU_LOG_IOPORT
)
433 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
437 env
->last_io_time
= cpu_get_time_fast();
442 /***********************************************************/
443 void hw_error(const char *fmt
, ...)
449 fprintf(stderr
, "qemu: hardware error: ");
450 vfprintf(stderr
, fmt
, ap
);
451 fprintf(stderr
, "\n");
452 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
453 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
455 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
457 cpu_dump_state(env
, stderr
, fprintf
, 0);
464 /***********************************************************/
467 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
468 static void *qemu_put_kbd_event_opaque
;
469 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
470 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
472 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
474 qemu_put_kbd_event_opaque
= opaque
;
475 qemu_put_kbd_event
= func
;
478 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
479 void *opaque
, int absolute
,
482 QEMUPutMouseEntry
*s
, *cursor
;
484 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
488 s
->qemu_put_mouse_event
= func
;
489 s
->qemu_put_mouse_event_opaque
= opaque
;
490 s
->qemu_put_mouse_event_absolute
= absolute
;
491 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
494 if (!qemu_put_mouse_event_head
) {
495 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
499 cursor
= qemu_put_mouse_event_head
;
500 while (cursor
->next
!= NULL
)
501 cursor
= cursor
->next
;
504 qemu_put_mouse_event_current
= s
;
509 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
511 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
513 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
516 cursor
= qemu_put_mouse_event_head
;
517 while (cursor
!= NULL
&& cursor
!= entry
) {
519 cursor
= cursor
->next
;
522 if (cursor
== NULL
) // does not exist or list empty
524 else if (prev
== NULL
) { // entry is head
525 qemu_put_mouse_event_head
= cursor
->next
;
526 if (qemu_put_mouse_event_current
== entry
)
527 qemu_put_mouse_event_current
= cursor
->next
;
528 qemu_free(entry
->qemu_put_mouse_event_name
);
533 prev
->next
= entry
->next
;
535 if (qemu_put_mouse_event_current
== entry
)
536 qemu_put_mouse_event_current
= prev
;
538 qemu_free(entry
->qemu_put_mouse_event_name
);
542 void kbd_put_keycode(int keycode
)
544 if (qemu_put_kbd_event
) {
545 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
549 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
551 QEMUPutMouseEvent
*mouse_event
;
552 void *mouse_event_opaque
;
555 if (!qemu_put_mouse_event_current
) {
560 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
562 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
565 if (graphic_rotate
) {
566 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
569 width
= graphic_width
;
570 mouse_event(mouse_event_opaque
,
571 width
- dy
, dx
, dz
, buttons_state
);
573 mouse_event(mouse_event_opaque
,
574 dx
, dy
, dz
, buttons_state
);
578 int kbd_mouse_is_absolute(void)
580 if (!qemu_put_mouse_event_current
)
583 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
586 void do_info_mice(void)
588 QEMUPutMouseEntry
*cursor
;
591 if (!qemu_put_mouse_event_head
) {
592 term_printf("No mouse devices connected\n");
596 term_printf("Mouse devices available:\n");
597 cursor
= qemu_put_mouse_event_head
;
598 while (cursor
!= NULL
) {
599 term_printf("%c Mouse #%d: %s\n",
600 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
601 index
, cursor
->qemu_put_mouse_event_name
);
603 cursor
= cursor
->next
;
607 void do_mouse_set(int index
)
609 QEMUPutMouseEntry
*cursor
;
612 if (!qemu_put_mouse_event_head
) {
613 term_printf("No mouse devices connected\n");
617 cursor
= qemu_put_mouse_event_head
;
618 while (cursor
!= NULL
&& index
!= i
) {
620 cursor
= cursor
->next
;
624 qemu_put_mouse_event_current
= cursor
;
626 term_printf("Mouse at given index not found\n");
629 /* compute with 96 bit intermediate result: (a*b)/c */
630 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
635 #ifdef WORDS_BIGENDIAN
645 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
646 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
649 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
653 /***********************************************************/
654 /* real time host monotonic timer */
656 #define QEMU_TIMER_BASE 1000000000LL
660 static int64_t clock_freq
;
662 static void init_get_clock(void)
666 ret
= QueryPerformanceFrequency(&freq
);
668 fprintf(stderr
, "Could not calibrate ticks\n");
671 clock_freq
= freq
.QuadPart
;
674 static int64_t get_clock(void)
677 QueryPerformanceCounter(&ti
);
678 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
683 static int use_rt_clock
;
685 static void init_get_clock(void)
688 #if defined(__linux__)
691 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
698 static int64_t get_clock(void)
700 #if defined(__linux__)
703 clock_gettime(CLOCK_MONOTONIC
, &ts
);
704 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
708 /* XXX: using gettimeofday leads to problems if the date
709 changes, so it should be avoided. */
711 gettimeofday(&tv
, NULL
);
712 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
718 /***********************************************************/
719 /* guest cycle counter */
721 static int64_t cpu_ticks_prev
;
722 static int64_t cpu_ticks_offset
;
723 static int64_t cpu_clock_offset
;
724 static int cpu_ticks_enabled
;
726 /* return the host CPU cycle counter and handle stop/restart */
727 int64_t cpu_get_ticks(void)
729 if (!cpu_ticks_enabled
) {
730 return cpu_ticks_offset
;
733 ticks
= cpu_get_real_ticks();
734 if (cpu_ticks_prev
> ticks
) {
735 /* Note: non increasing ticks may happen if the host uses
737 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
739 cpu_ticks_prev
= ticks
;
740 return ticks
+ cpu_ticks_offset
;
744 /* return the host CPU monotonic timer and handle stop/restart */
745 static int64_t cpu_get_clock(void)
748 if (!cpu_ticks_enabled
) {
749 return cpu_clock_offset
;
752 return ti
+ cpu_clock_offset
;
756 /* enable cpu_get_ticks() */
757 void cpu_enable_ticks(void)
759 if (!cpu_ticks_enabled
) {
760 cpu_ticks_offset
-= cpu_get_real_ticks();
761 cpu_clock_offset
-= get_clock();
762 cpu_ticks_enabled
= 1;
766 /* disable cpu_get_ticks() : the clock is stopped. You must not call
767 cpu_get_ticks() after that. */
768 void cpu_disable_ticks(void)
770 if (cpu_ticks_enabled
) {
771 cpu_ticks_offset
= cpu_get_ticks();
772 cpu_clock_offset
= cpu_get_clock();
773 cpu_ticks_enabled
= 0;
777 /***********************************************************/
780 #define QEMU_TIMER_REALTIME 0
781 #define QEMU_TIMER_VIRTUAL 1
785 /* XXX: add frequency */
793 struct QEMUTimer
*next
;
796 struct qemu_alarm_timer
{
800 int (*start
)(struct qemu_alarm_timer
*t
);
801 void (*stop
)(struct qemu_alarm_timer
*t
);
802 void (*rearm
)(struct qemu_alarm_timer
*t
);
806 #define ALARM_FLAG_DYNTICKS 0x1
808 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
810 return t
->flags
& ALARM_FLAG_DYNTICKS
;
813 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
815 if (!alarm_has_dynticks(t
))
821 /* TODO: MIN_TIMER_REARM_US should be optimized */
822 #define MIN_TIMER_REARM_US 250
824 static struct qemu_alarm_timer
*alarm_timer
;
828 struct qemu_alarm_win32
{
832 } alarm_win32_data
= {0, NULL
, -1};
834 static int win32_start_timer(struct qemu_alarm_timer
*t
);
835 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
836 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
840 static int unix_start_timer(struct qemu_alarm_timer
*t
);
841 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
845 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
846 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
847 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
849 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
850 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
852 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
853 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
855 #endif /* __linux__ */
859 static struct qemu_alarm_timer alarm_timers
[] = {
862 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
863 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
864 /* HPET - if available - is preferred */
865 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
866 /* ...otherwise try RTC */
867 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
869 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
871 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
872 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
873 {"win32", 0, win32_start_timer
,
874 win32_stop_timer
, NULL
, &alarm_win32_data
},
879 static void show_available_alarms()
883 printf("Available alarm timers, in order of precedence:\n");
884 for (i
= 0; alarm_timers
[i
].name
; i
++)
885 printf("%s\n", alarm_timers
[i
].name
);
888 static void configure_alarms(char const *opt
)
892 int count
= (sizeof(alarm_timers
) / sizeof(*alarm_timers
)) - 1;
896 if (!strcmp(opt
, "help")) {
897 show_available_alarms();
903 /* Reorder the array */
904 name
= strtok(arg
, ",");
906 struct qemu_alarm_timer tmp
;
908 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
909 if (!strcmp(alarm_timers
[i
].name
, name
))
914 fprintf(stderr
, "Unknown clock %s\n", name
);
923 tmp
= alarm_timers
[i
];
924 alarm_timers
[i
] = alarm_timers
[cur
];
925 alarm_timers
[cur
] = tmp
;
929 name
= strtok(NULL
, ",");
935 /* Disable remaining timers */
936 for (i
= cur
; i
< count
; i
++)
937 alarm_timers
[i
].name
= NULL
;
941 show_available_alarms();
947 static QEMUTimer
*active_timers
[2];
949 QEMUClock
*qemu_new_clock(int type
)
952 clock
= qemu_mallocz(sizeof(QEMUClock
));
959 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
963 ts
= qemu_mallocz(sizeof(QEMUTimer
));
970 void qemu_free_timer(QEMUTimer
*ts
)
975 /* stop a timer, but do not dealloc it */
976 void qemu_del_timer(QEMUTimer
*ts
)
980 /* NOTE: this code must be signal safe because
981 qemu_timer_expired() can be called from a signal. */
982 pt
= &active_timers
[ts
->clock
->type
];
995 /* modify the current timer so that it will be fired when current_time
996 >= expire_time. The corresponding callback will be called. */
997 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1003 /* add the timer in the sorted list */
1004 /* NOTE: this code must be signal safe because
1005 qemu_timer_expired() can be called from a signal. */
1006 pt
= &active_timers
[ts
->clock
->type
];
1011 if (t
->expire_time
> expire_time
)
1015 ts
->expire_time
= expire_time
;
1020 int qemu_timer_pending(QEMUTimer
*ts
)
1023 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1030 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1034 return (timer_head
->expire_time
<= current_time
);
1037 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1043 if (!ts
|| ts
->expire_time
> current_time
)
1045 /* remove timer from the list before calling the callback */
1046 *ptimer_head
= ts
->next
;
1049 /* run the callback (the timer list can be modified) */
1052 qemu_rearm_alarm_timer(alarm_timer
);
1055 int64_t qemu_get_clock(QEMUClock
*clock
)
1057 switch(clock
->type
) {
1058 case QEMU_TIMER_REALTIME
:
1059 return get_clock() / 1000000;
1061 case QEMU_TIMER_VIRTUAL
:
1062 return cpu_get_clock();
1066 static void init_timers(void)
1069 ticks_per_sec
= QEMU_TIMER_BASE
;
1070 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1071 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1075 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1077 uint64_t expire_time
;
1079 if (qemu_timer_pending(ts
)) {
1080 expire_time
= ts
->expire_time
;
1084 qemu_put_be64(f
, expire_time
);
1087 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1089 uint64_t expire_time
;
1091 expire_time
= qemu_get_be64(f
);
1092 if (expire_time
!= -1) {
1093 qemu_mod_timer(ts
, expire_time
);
1099 static void timer_save(QEMUFile
*f
, void *opaque
)
1101 if (cpu_ticks_enabled
) {
1102 hw_error("cannot save state if virtual timers are running");
1104 qemu_put_be64s(f
, &cpu_ticks_offset
);
1105 qemu_put_be64s(f
, &ticks_per_sec
);
1106 qemu_put_be64s(f
, &cpu_clock_offset
);
1109 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1111 if (version_id
!= 1 && version_id
!= 2)
1113 if (cpu_ticks_enabled
) {
1116 qemu_get_be64s(f
, &cpu_ticks_offset
);
1117 qemu_get_be64s(f
, &ticks_per_sec
);
1118 if (version_id
== 2) {
1119 qemu_get_be64s(f
, &cpu_clock_offset
);
1125 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1126 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1128 static void host_alarm_handler(int host_signum
)
1132 #define DISP_FREQ 1000
1134 static int64_t delta_min
= INT64_MAX
;
1135 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1137 ti
= qemu_get_clock(vm_clock
);
1138 if (last_clock
!= 0) {
1139 delta
= ti
- last_clock
;
1140 if (delta
< delta_min
)
1142 if (delta
> delta_max
)
1145 if (++count
== DISP_FREQ
) {
1146 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1147 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1148 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1149 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1150 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1152 delta_min
= INT64_MAX
;
1160 if (alarm_has_dynticks(alarm_timer
) ||
1161 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1162 qemu_get_clock(vm_clock
)) ||
1163 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1164 qemu_get_clock(rt_clock
))) {
1166 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1167 SetEvent(data
->host_alarm
);
1169 CPUState
*env
= cpu_single_env
;
1171 /* stop the currently executing cpu because a timer occured */
1172 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1174 if (env
->kqemu_enabled
) {
1175 kqemu_cpu_interrupt(env
);
1182 static uint64_t qemu_next_deadline(void)
1184 int64_t nearest_delta_us
= INT64_MAX
;
1187 if (active_timers
[QEMU_TIMER_REALTIME
])
1188 nearest_delta_us
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1189 qemu_get_clock(rt_clock
))*1000;
1191 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1193 vmdelta_us
= (active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1194 qemu_get_clock(vm_clock
)+999)/1000;
1195 if (vmdelta_us
< nearest_delta_us
)
1196 nearest_delta_us
= vmdelta_us
;
1199 /* Avoid arming the timer to negative, zero, or too low values */
1200 if (nearest_delta_us
<= MIN_TIMER_REARM_US
)
1201 nearest_delta_us
= MIN_TIMER_REARM_US
;
1203 return nearest_delta_us
;
1208 #if defined(__linux__)
1210 #define RTC_FREQ 1024
1212 static void enable_sigio_timer(int fd
)
1214 struct sigaction act
;
1217 sigfillset(&act
.sa_mask
);
1219 act
.sa_handler
= host_alarm_handler
;
1221 sigaction(SIGIO
, &act
, NULL
);
1222 fcntl(fd
, F_SETFL
, O_ASYNC
);
1223 fcntl(fd
, F_SETOWN
, getpid());
1226 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1228 struct hpet_info info
;
1231 fd
= open("/dev/hpet", O_RDONLY
);
1236 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1238 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1239 "error, but for better emulation accuracy type:\n"
1240 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1244 /* Check capabilities */
1245 r
= ioctl(fd
, HPET_INFO
, &info
);
1249 /* Enable periodic mode */
1250 r
= ioctl(fd
, HPET_EPI
, 0);
1251 if (info
.hi_flags
&& (r
< 0))
1254 /* Enable interrupt */
1255 r
= ioctl(fd
, HPET_IE_ON
, 0);
1259 enable_sigio_timer(fd
);
1260 t
->priv
= (void *)(long)fd
;
1268 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1270 int fd
= (long)t
->priv
;
1275 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1279 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1282 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1283 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1284 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1285 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1288 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1294 enable_sigio_timer(rtc_fd
);
1296 t
->priv
= (void *)(long)rtc_fd
;
1301 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1303 int rtc_fd
= (long)t
->priv
;
1308 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1312 struct sigaction act
;
1314 sigfillset(&act
.sa_mask
);
1316 act
.sa_handler
= host_alarm_handler
;
1318 sigaction(SIGALRM
, &act
, NULL
);
1320 ev
.sigev_value
.sival_int
= 0;
1321 ev
.sigev_notify
= SIGEV_SIGNAL
;
1322 ev
.sigev_signo
= SIGALRM
;
1324 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1325 perror("timer_create");
1327 /* disable dynticks */
1328 fprintf(stderr
, "Dynamic Ticks disabled\n");
1333 t
->priv
= (void *)host_timer
;
1338 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1340 timer_t host_timer
= (timer_t
)t
->priv
;
1342 timer_delete(host_timer
);
1345 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1347 timer_t host_timer
= (timer_t
)t
->priv
;
1348 struct itimerspec timeout
;
1349 int64_t nearest_delta_us
= INT64_MAX
;
1352 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1353 !active_timers
[QEMU_TIMER_VIRTUAL
])
1356 nearest_delta_us
= qemu_next_deadline();
1358 /* check whether a timer is already running */
1359 if (timer_gettime(host_timer
, &timeout
)) {
1361 fprintf(stderr
, "Internal timer error: aborting\n");
1364 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1365 if (current_us
&& current_us
<= nearest_delta_us
)
1368 timeout
.it_interval
.tv_sec
= 0;
1369 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1370 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1371 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1372 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1374 fprintf(stderr
, "Internal timer error: aborting\n");
1379 #endif /* defined(__linux__) */
1381 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1383 struct sigaction act
;
1384 struct itimerval itv
;
1388 sigfillset(&act
.sa_mask
);
1390 act
.sa_handler
= host_alarm_handler
;
1392 sigaction(SIGALRM
, &act
, NULL
);
1394 itv
.it_interval
.tv_sec
= 0;
1395 /* for i386 kernel 2.6 to get 1 ms */
1396 itv
.it_interval
.tv_usec
= 999;
1397 itv
.it_value
.tv_sec
= 0;
1398 itv
.it_value
.tv_usec
= 10 * 1000;
1400 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1407 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1409 struct itimerval itv
;
1411 memset(&itv
, 0, sizeof(itv
));
1412 setitimer(ITIMER_REAL
, &itv
, NULL
);
1415 #endif /* !defined(_WIN32) */
1419 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1422 struct qemu_alarm_win32
*data
= t
->priv
;
1425 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1426 if (!data
->host_alarm
) {
1427 perror("Failed CreateEvent");
1431 memset(&tc
, 0, sizeof(tc
));
1432 timeGetDevCaps(&tc
, sizeof(tc
));
1434 if (data
->period
< tc
.wPeriodMin
)
1435 data
->period
= tc
.wPeriodMin
;
1437 timeBeginPeriod(data
->period
);
1439 flags
= TIME_CALLBACK_FUNCTION
;
1440 if (alarm_has_dynticks(t
))
1441 flags
|= TIME_ONESHOT
;
1443 flags
|= TIME_PERIODIC
;
1445 data
->timerId
= timeSetEvent(1, // interval (ms)
1446 data
->period
, // resolution
1447 host_alarm_handler
, // function
1448 (DWORD
)t
, // parameter
1451 if (!data
->timerId
) {
1452 perror("Failed to initialize win32 alarm timer");
1454 timeEndPeriod(data
->period
);
1455 CloseHandle(data
->host_alarm
);
1459 qemu_add_wait_object(data
->host_alarm
, NULL
, NULL
);
1464 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1466 struct qemu_alarm_win32
*data
= t
->priv
;
1468 timeKillEvent(data
->timerId
);
1469 timeEndPeriod(data
->period
);
1471 CloseHandle(data
->host_alarm
);
1474 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1476 struct qemu_alarm_win32
*data
= t
->priv
;
1477 uint64_t nearest_delta_us
;
1479 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1480 !active_timers
[QEMU_TIMER_VIRTUAL
])
1483 nearest_delta_us
= qemu_next_deadline();
1484 nearest_delta_us
/= 1000;
1486 timeKillEvent(data
->timerId
);
1488 data
->timerId
= timeSetEvent(1,
1492 TIME_ONESHOT
| TIME_PERIODIC
);
1494 if (!data
->timerId
) {
1495 perror("Failed to re-arm win32 alarm timer");
1497 timeEndPeriod(data
->period
);
1498 CloseHandle(data
->host_alarm
);
1505 static void init_timer_alarm(void)
1507 struct qemu_alarm_timer
*t
;
1510 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1511 t
= &alarm_timers
[i
];
1519 fprintf(stderr
, "Unable to find any suitable alarm timer.\n");
1520 fprintf(stderr
, "Terminating\n");
1527 void quit_timers(void)
1529 alarm_timer
->stop(alarm_timer
);
1533 /***********************************************************/
1534 /* character device */
1536 static void qemu_chr_event(CharDriverState
*s
, int event
)
1540 s
->chr_event(s
->handler_opaque
, event
);
1543 static void qemu_chr_reset_bh(void *opaque
)
1545 CharDriverState
*s
= opaque
;
1546 qemu_chr_event(s
, CHR_EVENT_RESET
);
1547 qemu_bh_delete(s
->bh
);
1551 void qemu_chr_reset(CharDriverState
*s
)
1553 if (s
->bh
== NULL
) {
1554 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
1555 qemu_bh_schedule(s
->bh
);
1559 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1561 return s
->chr_write(s
, buf
, len
);
1564 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1568 return s
->chr_ioctl(s
, cmd
, arg
);
1571 int qemu_chr_can_read(CharDriverState
*s
)
1573 if (!s
->chr_can_read
)
1575 return s
->chr_can_read(s
->handler_opaque
);
1578 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
1580 s
->chr_read(s
->handler_opaque
, buf
, len
);
1584 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1589 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1590 qemu_chr_write(s
, buf
, strlen(buf
));
1594 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1596 if (s
->chr_send_event
)
1597 s
->chr_send_event(s
, event
);
1600 void qemu_chr_add_handlers(CharDriverState
*s
,
1601 IOCanRWHandler
*fd_can_read
,
1602 IOReadHandler
*fd_read
,
1603 IOEventHandler
*fd_event
,
1606 s
->chr_can_read
= fd_can_read
;
1607 s
->chr_read
= fd_read
;
1608 s
->chr_event
= fd_event
;
1609 s
->handler_opaque
= opaque
;
1610 if (s
->chr_update_read_handler
)
1611 s
->chr_update_read_handler(s
);
1614 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1619 static CharDriverState
*qemu_chr_open_null(void)
1621 CharDriverState
*chr
;
1623 chr
= qemu_mallocz(sizeof(CharDriverState
));
1626 chr
->chr_write
= null_chr_write
;
1630 /* MUX driver for serial I/O splitting */
1631 static int term_timestamps
;
1632 static int64_t term_timestamps_start
;
1635 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
1636 IOReadHandler
*chr_read
[MAX_MUX
];
1637 IOEventHandler
*chr_event
[MAX_MUX
];
1638 void *ext_opaque
[MAX_MUX
];
1639 CharDriverState
*drv
;
1641 int term_got_escape
;
1646 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1648 MuxDriver
*d
= chr
->opaque
;
1650 if (!term_timestamps
) {
1651 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
1656 for(i
= 0; i
< len
; i
++) {
1657 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
1658 if (buf
[i
] == '\n') {
1664 if (term_timestamps_start
== -1)
1665 term_timestamps_start
= ti
;
1666 ti
-= term_timestamps_start
;
1667 secs
= ti
/ 1000000000;
1668 snprintf(buf1
, sizeof(buf1
),
1669 "[%02d:%02d:%02d.%03d] ",
1673 (int)((ti
/ 1000000) % 1000));
1674 d
->drv
->chr_write(d
->drv
, buf1
, strlen(buf1
));
1681 static char *mux_help
[] = {
1682 "% h print this help\n\r",
1683 "% x exit emulator\n\r",
1684 "% s save disk data back to file (if -snapshot)\n\r",
1685 "% t toggle console timestamps\n\r"
1686 "% b send break (magic sysrq)\n\r",
1687 "% c switch between console and monitor\n\r",
1692 static int term_escape_char
= 0x01; /* ctrl-a is used for escape */
1693 static void mux_print_help(CharDriverState
*chr
)
1696 char ebuf
[15] = "Escape-Char";
1697 char cbuf
[50] = "\n\r";
1699 if (term_escape_char
> 0 && term_escape_char
< 26) {
1700 sprintf(cbuf
,"\n\r");
1701 sprintf(ebuf
,"C-%c", term_escape_char
- 1 + 'a');
1703 sprintf(cbuf
,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char
);
1705 chr
->chr_write(chr
, cbuf
, strlen(cbuf
));
1706 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
1707 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
1708 if (mux_help
[i
][j
] == '%')
1709 chr
->chr_write(chr
, ebuf
, strlen(ebuf
));
1711 chr
->chr_write(chr
, &mux_help
[i
][j
], 1);
1716 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
1718 if (d
->term_got_escape
) {
1719 d
->term_got_escape
= 0;
1720 if (ch
== term_escape_char
)
1725 mux_print_help(chr
);
1729 char *term
= "QEMU: Terminated\n\r";
1730 chr
->chr_write(chr
,term
,strlen(term
));
1737 for (i
= 0; i
< MAX_DISKS
; i
++) {
1739 bdrv_commit(bs_table
[i
]);
1742 bdrv_commit(mtd_bdrv
);
1746 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1749 /* Switch to the next registered device */
1751 if (chr
->focus
>= d
->mux_cnt
)
1755 term_timestamps
= !term_timestamps
;
1756 term_timestamps_start
= -1;
1759 } else if (ch
== term_escape_char
) {
1760 d
->term_got_escape
= 1;
1768 static int mux_chr_can_read(void *opaque
)
1770 CharDriverState
*chr
= opaque
;
1771 MuxDriver
*d
= chr
->opaque
;
1772 if (d
->chr_can_read
[chr
->focus
])
1773 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
1777 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
1779 CharDriverState
*chr
= opaque
;
1780 MuxDriver
*d
= chr
->opaque
;
1782 for(i
= 0; i
< size
; i
++)
1783 if (mux_proc_byte(chr
, d
, buf
[i
]))
1784 d
->chr_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
], &buf
[i
], 1);
1787 static void mux_chr_event(void *opaque
, int event
)
1789 CharDriverState
*chr
= opaque
;
1790 MuxDriver
*d
= chr
->opaque
;
1793 /* Send the event to all registered listeners */
1794 for (i
= 0; i
< d
->mux_cnt
; i
++)
1795 if (d
->chr_event
[i
])
1796 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
1799 static void mux_chr_update_read_handler(CharDriverState
*chr
)
1801 MuxDriver
*d
= chr
->opaque
;
1803 if (d
->mux_cnt
>= MAX_MUX
) {
1804 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
1807 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
1808 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
1809 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
1810 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
1811 /* Fix up the real driver with mux routines */
1812 if (d
->mux_cnt
== 0) {
1813 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
1814 mux_chr_event
, chr
);
1816 chr
->focus
= d
->mux_cnt
;
1820 CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
1822 CharDriverState
*chr
;
1825 chr
= qemu_mallocz(sizeof(CharDriverState
));
1828 d
= qemu_mallocz(sizeof(MuxDriver
));
1837 chr
->chr_write
= mux_chr_write
;
1838 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
1845 static void socket_cleanup(void)
1850 static int socket_init(void)
1855 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1857 err
= WSAGetLastError();
1858 fprintf(stderr
, "WSAStartup: %d\n", err
);
1861 atexit(socket_cleanup
);
1865 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1871 ret
= send(fd
, buf
, len
, 0);
1874 errno
= WSAGetLastError();
1875 if (errno
!= WSAEWOULDBLOCK
) {
1878 } else if (ret
== 0) {
1888 void socket_set_nonblock(int fd
)
1890 unsigned long opt
= 1;
1891 ioctlsocket(fd
, FIONBIO
, &opt
);
1896 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1902 ret
= write(fd
, buf
, len
);
1904 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1906 } else if (ret
== 0) {
1916 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1918 return unix_write(fd
, buf
, len1
);
1921 void socket_set_nonblock(int fd
)
1923 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1925 #endif /* !_WIN32 */
1934 #define STDIO_MAX_CLIENTS 1
1935 static int stdio_nb_clients
= 0;
1937 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1939 FDCharDriver
*s
= chr
->opaque
;
1940 return unix_write(s
->fd_out
, buf
, len
);
1943 static int fd_chr_read_poll(void *opaque
)
1945 CharDriverState
*chr
= opaque
;
1946 FDCharDriver
*s
= chr
->opaque
;
1948 s
->max_size
= qemu_chr_can_read(chr
);
1952 static void fd_chr_read(void *opaque
)
1954 CharDriverState
*chr
= opaque
;
1955 FDCharDriver
*s
= chr
->opaque
;
1960 if (len
> s
->max_size
)
1964 size
= read(s
->fd_in
, buf
, len
);
1966 /* FD has been closed. Remove it from the active list. */
1967 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1971 qemu_chr_read(chr
, buf
, size
);
1975 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1977 FDCharDriver
*s
= chr
->opaque
;
1979 if (s
->fd_in
>= 0) {
1980 if (nographic
&& s
->fd_in
== 0) {
1982 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1983 fd_chr_read
, NULL
, chr
);
1988 /* open a character device to a unix fd */
1989 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1991 CharDriverState
*chr
;
1994 chr
= qemu_mallocz(sizeof(CharDriverState
));
1997 s
= qemu_mallocz(sizeof(FDCharDriver
));
2005 chr
->chr_write
= fd_chr_write
;
2006 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
2008 qemu_chr_reset(chr
);
2013 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
2017 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
2020 return qemu_chr_open_fd(-1, fd_out
);
2023 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
2026 char filename_in
[256], filename_out
[256];
2028 snprintf(filename_in
, 256, "%s.in", filename
);
2029 snprintf(filename_out
, 256, "%s.out", filename
);
2030 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
2031 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
2032 if (fd_in
< 0 || fd_out
< 0) {
2037 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
2041 return qemu_chr_open_fd(fd_in
, fd_out
);
2045 /* for STDIO, we handle the case where several clients use it
2048 #define TERM_FIFO_MAX_SIZE 1
2050 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
2051 static int term_fifo_size
;
2053 static int stdio_read_poll(void *opaque
)
2055 CharDriverState
*chr
= opaque
;
2057 /* try to flush the queue if needed */
2058 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
2059 qemu_chr_read(chr
, term_fifo
, 1);
2062 /* see if we can absorb more chars */
2063 if (term_fifo_size
== 0)
2069 static void stdio_read(void *opaque
)
2073 CharDriverState
*chr
= opaque
;
2075 size
= read(0, buf
, 1);
2077 /* stdin has been closed. Remove it from the active list. */
2078 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
2082 if (qemu_chr_can_read(chr
) > 0) {
2083 qemu_chr_read(chr
, buf
, 1);
2084 } else if (term_fifo_size
== 0) {
2085 term_fifo
[term_fifo_size
++] = buf
[0];
2090 /* init terminal so that we can grab keys */
2091 static struct termios oldtty
;
2092 static int old_fd0_flags
;
2094 static void term_exit(void)
2096 tcsetattr (0, TCSANOW
, &oldtty
);
2097 fcntl(0, F_SETFL
, old_fd0_flags
);
2100 static void term_init(void)
2104 tcgetattr (0, &tty
);
2106 old_fd0_flags
= fcntl(0, F_GETFL
);
2108 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
2109 |INLCR
|IGNCR
|ICRNL
|IXON
);
2110 tty
.c_oflag
|= OPOST
;
2111 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
2112 /* if graphical mode, we allow Ctrl-C handling */
2114 tty
.c_lflag
&= ~ISIG
;
2115 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
2118 tty
.c_cc
[VTIME
] = 0;
2120 tcsetattr (0, TCSANOW
, &tty
);
2124 fcntl(0, F_SETFL
, O_NONBLOCK
);
2127 static CharDriverState
*qemu_chr_open_stdio(void)
2129 CharDriverState
*chr
;
2131 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
2133 chr
= qemu_chr_open_fd(0, 1);
2134 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
2141 #if defined(__linux__) || defined(__sun__)
2142 static CharDriverState
*qemu_chr_open_pty(void)
2145 char slave_name
[1024];
2146 int master_fd
, slave_fd
;
2148 #if defined(__linux__)
2149 /* Not satisfying */
2150 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
2155 /* Disabling local echo and line-buffered output */
2156 tcgetattr (master_fd
, &tty
);
2157 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
2159 tty
.c_cc
[VTIME
] = 0;
2160 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
2162 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
2163 return qemu_chr_open_fd(master_fd
, master_fd
);
2166 static void tty_serial_init(int fd
, int speed
,
2167 int parity
, int data_bits
, int stop_bits
)
2173 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
2174 speed
, parity
, data_bits
, stop_bits
);
2176 tcgetattr (fd
, &tty
);
2218 cfsetispeed(&tty
, spd
);
2219 cfsetospeed(&tty
, spd
);
2221 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
2222 |INLCR
|IGNCR
|ICRNL
|IXON
);
2223 tty
.c_oflag
|= OPOST
;
2224 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
2225 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
2246 tty
.c_cflag
|= PARENB
;
2249 tty
.c_cflag
|= PARENB
| PARODD
;
2253 tty
.c_cflag
|= CSTOPB
;
2255 tcsetattr (fd
, TCSANOW
, &tty
);
2258 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2260 FDCharDriver
*s
= chr
->opaque
;
2263 case CHR_IOCTL_SERIAL_SET_PARAMS
:
2265 QEMUSerialSetParams
*ssp
= arg
;
2266 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
2267 ssp
->data_bits
, ssp
->stop_bits
);
2270 case CHR_IOCTL_SERIAL_SET_BREAK
:
2272 int enable
= *(int *)arg
;
2274 tcsendbreak(s
->fd_in
, 1);
2283 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
2285 CharDriverState
*chr
;
2288 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
2289 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2290 tty_serial_init(fd
, 115200, 'N', 8, 1);
2291 chr
= qemu_chr_open_fd(fd
, fd
);
2296 chr
->chr_ioctl
= tty_serial_ioctl
;
2297 qemu_chr_reset(chr
);
2300 #else /* ! __linux__ && ! __sun__ */
2301 static CharDriverState
*qemu_chr_open_pty(void)
2305 #endif /* __linux__ || __sun__ */
2307 #if defined(__linux__)
2311 } ParallelCharDriver
;
2313 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
2315 if (s
->mode
!= mode
) {
2317 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
2324 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2326 ParallelCharDriver
*drv
= chr
->opaque
;
2331 case CHR_IOCTL_PP_READ_DATA
:
2332 if (ioctl(fd
, PPRDATA
, &b
) < 0)
2334 *(uint8_t *)arg
= b
;
2336 case CHR_IOCTL_PP_WRITE_DATA
:
2337 b
= *(uint8_t *)arg
;
2338 if (ioctl(fd
, PPWDATA
, &b
) < 0)
2341 case CHR_IOCTL_PP_READ_CONTROL
:
2342 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
2344 /* Linux gives only the lowest bits, and no way to know data
2345 direction! For better compatibility set the fixed upper
2347 *(uint8_t *)arg
= b
| 0xc0;
2349 case CHR_IOCTL_PP_WRITE_CONTROL
:
2350 b
= *(uint8_t *)arg
;
2351 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
2354 case CHR_IOCTL_PP_READ_STATUS
:
2355 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
2357 *(uint8_t *)arg
= b
;
2359 case CHR_IOCTL_PP_EPP_READ_ADDR
:
2360 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2361 struct ParallelIOArg
*parg
= arg
;
2362 int n
= read(fd
, parg
->buffer
, parg
->count
);
2363 if (n
!= parg
->count
) {
2368 case CHR_IOCTL_PP_EPP_READ
:
2369 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2370 struct ParallelIOArg
*parg
= arg
;
2371 int n
= read(fd
, parg
->buffer
, parg
->count
);
2372 if (n
!= parg
->count
) {
2377 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
2378 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2379 struct ParallelIOArg
*parg
= arg
;
2380 int n
= write(fd
, parg
->buffer
, parg
->count
);
2381 if (n
!= parg
->count
) {
2386 case CHR_IOCTL_PP_EPP_WRITE
:
2387 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2388 struct ParallelIOArg
*parg
= arg
;
2389 int n
= write(fd
, parg
->buffer
, parg
->count
);
2390 if (n
!= parg
->count
) {
2401 static void pp_close(CharDriverState
*chr
)
2403 ParallelCharDriver
*drv
= chr
->opaque
;
2406 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
2407 ioctl(fd
, PPRELEASE
);
2412 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
2414 CharDriverState
*chr
;
2415 ParallelCharDriver
*drv
;
2418 TFR(fd
= open(filename
, O_RDWR
));
2422 if (ioctl(fd
, PPCLAIM
) < 0) {
2427 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
2433 drv
->mode
= IEEE1284_MODE_COMPAT
;
2435 chr
= qemu_mallocz(sizeof(CharDriverState
));
2441 chr
->chr_write
= null_chr_write
;
2442 chr
->chr_ioctl
= pp_ioctl
;
2443 chr
->chr_close
= pp_close
;
2446 qemu_chr_reset(chr
);
2450 #endif /* __linux__ */
2456 HANDLE hcom
, hrecv
, hsend
;
2457 OVERLAPPED orecv
, osend
;
2462 #define NSENDBUF 2048
2463 #define NRECVBUF 2048
2464 #define MAXCONNECT 1
2465 #define NTIMEOUT 5000
2467 static int win_chr_poll(void *opaque
);
2468 static int win_chr_pipe_poll(void *opaque
);
2470 static void win_chr_close(CharDriverState
*chr
)
2472 WinCharState
*s
= chr
->opaque
;
2475 CloseHandle(s
->hsend
);
2479 CloseHandle(s
->hrecv
);
2483 CloseHandle(s
->hcom
);
2487 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
2489 qemu_del_polling_cb(win_chr_poll
, chr
);
2492 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
2494 WinCharState
*s
= chr
->opaque
;
2496 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2501 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2503 fprintf(stderr
, "Failed CreateEvent\n");
2506 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2508 fprintf(stderr
, "Failed CreateEvent\n");
2512 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2513 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2514 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2515 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
2520 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2521 fprintf(stderr
, "Failed SetupComm\n");
2525 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2526 size
= sizeof(COMMCONFIG
);
2527 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2528 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2529 CommConfigDialog(filename
, NULL
, &comcfg
);
2531 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2532 fprintf(stderr
, "Failed SetCommState\n");
2536 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2537 fprintf(stderr
, "Failed SetCommMask\n");
2541 cto
.ReadIntervalTimeout
= MAXDWORD
;
2542 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2543 fprintf(stderr
, "Failed SetCommTimeouts\n");
2547 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2548 fprintf(stderr
, "Failed ClearCommError\n");
2551 qemu_add_polling_cb(win_chr_poll
, chr
);
2559 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2561 WinCharState
*s
= chr
->opaque
;
2562 DWORD len
, ret
, size
, err
;
2565 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2566 s
->osend
.hEvent
= s
->hsend
;
2569 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2571 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2573 err
= GetLastError();
2574 if (err
== ERROR_IO_PENDING
) {
2575 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2593 static int win_chr_read_poll(CharDriverState
*chr
)
2595 WinCharState
*s
= chr
->opaque
;
2597 s
->max_size
= qemu_chr_can_read(chr
);
2601 static void win_chr_readfile(CharDriverState
*chr
)
2603 WinCharState
*s
= chr
->opaque
;
2608 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2609 s
->orecv
.hEvent
= s
->hrecv
;
2610 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2612 err
= GetLastError();
2613 if (err
== ERROR_IO_PENDING
) {
2614 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2619 qemu_chr_read(chr
, buf
, size
);
2623 static void win_chr_read(CharDriverState
*chr
)
2625 WinCharState
*s
= chr
->opaque
;
2627 if (s
->len
> s
->max_size
)
2628 s
->len
= s
->max_size
;
2632 win_chr_readfile(chr
);
2635 static int win_chr_poll(void *opaque
)
2637 CharDriverState
*chr
= opaque
;
2638 WinCharState
*s
= chr
->opaque
;
2642 ClearCommError(s
->hcom
, &comerr
, &status
);
2643 if (status
.cbInQue
> 0) {
2644 s
->len
= status
.cbInQue
;
2645 win_chr_read_poll(chr
);
2652 static CharDriverState
*qemu_chr_open_win(const char *filename
)
2654 CharDriverState
*chr
;
2657 chr
= qemu_mallocz(sizeof(CharDriverState
));
2660 s
= qemu_mallocz(sizeof(WinCharState
));
2666 chr
->chr_write
= win_chr_write
;
2667 chr
->chr_close
= win_chr_close
;
2669 if (win_chr_init(chr
, filename
) < 0) {
2674 qemu_chr_reset(chr
);
2678 static int win_chr_pipe_poll(void *opaque
)
2680 CharDriverState
*chr
= opaque
;
2681 WinCharState
*s
= chr
->opaque
;
2684 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2687 win_chr_read_poll(chr
);
2694 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
2696 WinCharState
*s
= chr
->opaque
;
2704 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2706 fprintf(stderr
, "Failed CreateEvent\n");
2709 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2711 fprintf(stderr
, "Failed CreateEvent\n");
2715 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2716 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2717 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2719 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2720 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2721 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2726 ZeroMemory(&ov
, sizeof(ov
));
2727 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2728 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2730 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2734 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2736 fprintf(stderr
, "Failed GetOverlappedResult\n");
2738 CloseHandle(ov
.hEvent
);
2745 CloseHandle(ov
.hEvent
);
2748 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2757 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2759 CharDriverState
*chr
;
2762 chr
= qemu_mallocz(sizeof(CharDriverState
));
2765 s
= qemu_mallocz(sizeof(WinCharState
));
2771 chr
->chr_write
= win_chr_write
;
2772 chr
->chr_close
= win_chr_close
;
2774 if (win_chr_pipe_init(chr
, filename
) < 0) {
2779 qemu_chr_reset(chr
);
2783 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2785 CharDriverState
*chr
;
2788 chr
= qemu_mallocz(sizeof(CharDriverState
));
2791 s
= qemu_mallocz(sizeof(WinCharState
));
2798 chr
->chr_write
= win_chr_write
;
2799 qemu_chr_reset(chr
);
2803 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
2805 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2808 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2812 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2813 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2814 if (fd_out
== INVALID_HANDLE_VALUE
)
2817 return qemu_chr_open_win_file(fd_out
);
2819 #endif /* !_WIN32 */
2821 /***********************************************************/
2822 /* UDP Net console */
2826 struct sockaddr_in daddr
;
2833 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2835 NetCharDriver
*s
= chr
->opaque
;
2837 return sendto(s
->fd
, buf
, len
, 0,
2838 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2841 static int udp_chr_read_poll(void *opaque
)
2843 CharDriverState
*chr
= opaque
;
2844 NetCharDriver
*s
= chr
->opaque
;
2846 s
->max_size
= qemu_chr_can_read(chr
);
2848 /* If there were any stray characters in the queue process them
2851 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2852 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2854 s
->max_size
= qemu_chr_can_read(chr
);
2859 static void udp_chr_read(void *opaque
)
2861 CharDriverState
*chr
= opaque
;
2862 NetCharDriver
*s
= chr
->opaque
;
2864 if (s
->max_size
== 0)
2866 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2867 s
->bufptr
= s
->bufcnt
;
2872 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2873 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2875 s
->max_size
= qemu_chr_can_read(chr
);
2879 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2881 NetCharDriver
*s
= chr
->opaque
;
2884 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2885 udp_chr_read
, NULL
, chr
);
2889 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2891 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2893 int parse_host_src_port(struct sockaddr_in
*haddr
,
2894 struct sockaddr_in
*saddr
,
2897 static CharDriverState
*qemu_chr_open_udp(const char *def
)
2899 CharDriverState
*chr
= NULL
;
2900 NetCharDriver
*s
= NULL
;
2902 struct sockaddr_in saddr
;
2904 chr
= qemu_mallocz(sizeof(CharDriverState
));
2907 s
= qemu_mallocz(sizeof(NetCharDriver
));
2911 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2913 perror("socket(PF_INET, SOCK_DGRAM)");
2917 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2918 printf("Could not parse: %s\n", def
);
2922 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2932 chr
->chr_write
= udp_chr_write
;
2933 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2946 /***********************************************************/
2947 /* TCP Net console */
2958 static void tcp_chr_accept(void *opaque
);
2960 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2962 TCPCharDriver
*s
= chr
->opaque
;
2964 return send_all(s
->fd
, buf
, len
);
2966 /* XXX: indicate an error ? */
2971 static int tcp_chr_read_poll(void *opaque
)
2973 CharDriverState
*chr
= opaque
;
2974 TCPCharDriver
*s
= chr
->opaque
;
2977 s
->max_size
= qemu_chr_can_read(chr
);
2982 #define IAC_BREAK 243
2983 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2985 char *buf
, int *size
)
2987 /* Handle any telnet client's basic IAC options to satisfy char by
2988 * char mode with no echo. All IAC options will be removed from
2989 * the buf and the do_telnetopt variable will be used to track the
2990 * state of the width of the IAC information.
2992 * IAC commands come in sets of 3 bytes with the exception of the
2993 * "IAC BREAK" command and the double IAC.
2999 for (i
= 0; i
< *size
; i
++) {
3000 if (s
->do_telnetopt
> 1) {
3001 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
3002 /* Double IAC means send an IAC */
3006 s
->do_telnetopt
= 1;
3008 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
3009 /* Handle IAC break commands by sending a serial break */
3010 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
3015 if (s
->do_telnetopt
>= 4) {
3016 s
->do_telnetopt
= 1;
3019 if ((unsigned char)buf
[i
] == IAC
) {
3020 s
->do_telnetopt
= 2;
3031 static void tcp_chr_read(void *opaque
)
3033 CharDriverState
*chr
= opaque
;
3034 TCPCharDriver
*s
= chr
->opaque
;
3038 if (!s
->connected
|| s
->max_size
<= 0)
3041 if (len
> s
->max_size
)
3043 size
= recv(s
->fd
, buf
, len
, 0);
3045 /* connection closed */
3047 if (s
->listen_fd
>= 0) {
3048 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
3050 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3053 } else if (size
> 0) {
3054 if (s
->do_telnetopt
)
3055 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
3057 qemu_chr_read(chr
, buf
, size
);
3061 static void tcp_chr_connect(void *opaque
)
3063 CharDriverState
*chr
= opaque
;
3064 TCPCharDriver
*s
= chr
->opaque
;
3067 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
3068 tcp_chr_read
, NULL
, chr
);
3069 qemu_chr_reset(chr
);
3072 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3073 static void tcp_chr_telnet_init(int fd
)
3076 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3077 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3078 send(fd
, (char *)buf
, 3, 0);
3079 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3080 send(fd
, (char *)buf
, 3, 0);
3081 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3082 send(fd
, (char *)buf
, 3, 0);
3083 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3084 send(fd
, (char *)buf
, 3, 0);
3087 static void socket_set_nodelay(int fd
)
3090 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
3093 static void tcp_chr_accept(void *opaque
)
3095 CharDriverState
*chr
= opaque
;
3096 TCPCharDriver
*s
= chr
->opaque
;
3097 struct sockaddr_in saddr
;
3099 struct sockaddr_un uaddr
;
3101 struct sockaddr
*addr
;
3108 len
= sizeof(uaddr
);
3109 addr
= (struct sockaddr
*)&uaddr
;
3113 len
= sizeof(saddr
);
3114 addr
= (struct sockaddr
*)&saddr
;
3116 fd
= accept(s
->listen_fd
, addr
, &len
);
3117 if (fd
< 0 && errno
!= EINTR
) {
3119 } else if (fd
>= 0) {
3120 if (s
->do_telnetopt
)
3121 tcp_chr_telnet_init(fd
);
3125 socket_set_nonblock(fd
);
3127 socket_set_nodelay(fd
);
3129 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
3130 tcp_chr_connect(chr
);
3133 static void tcp_chr_close(CharDriverState
*chr
)
3135 TCPCharDriver
*s
= chr
->opaque
;
3138 if (s
->listen_fd
>= 0)
3139 closesocket(s
->listen_fd
);
3143 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
3147 CharDriverState
*chr
= NULL
;
3148 TCPCharDriver
*s
= NULL
;
3149 int fd
= -1, ret
, err
, val
;
3151 int is_waitconnect
= 1;
3154 struct sockaddr_in saddr
;
3156 struct sockaddr_un uaddr
;
3158 struct sockaddr
*addr
;
3163 addr
= (struct sockaddr
*)&uaddr
;
3164 addrlen
= sizeof(uaddr
);
3165 if (parse_unix_path(&uaddr
, host_str
) < 0)
3170 addr
= (struct sockaddr
*)&saddr
;
3171 addrlen
= sizeof(saddr
);
3172 if (parse_host_port(&saddr
, host_str
) < 0)
3177 while((ptr
= strchr(ptr
,','))) {
3179 if (!strncmp(ptr
,"server",6)) {
3181 } else if (!strncmp(ptr
,"nowait",6)) {
3183 } else if (!strncmp(ptr
,"nodelay",6)) {
3186 printf("Unknown option: %s\n", ptr
);
3193 chr
= qemu_mallocz(sizeof(CharDriverState
));
3196 s
= qemu_mallocz(sizeof(TCPCharDriver
));
3202 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
3205 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3210 if (!is_waitconnect
)
3211 socket_set_nonblock(fd
);
3216 s
->is_unix
= is_unix
;
3217 s
->do_nodelay
= do_nodelay
&& !is_unix
;
3220 chr
->chr_write
= tcp_chr_write
;
3221 chr
->chr_close
= tcp_chr_close
;
3224 /* allow fast reuse */
3228 strncpy(path
, uaddr
.sun_path
, 108);
3235 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3238 ret
= bind(fd
, addr
, addrlen
);
3242 ret
= listen(fd
, 0);
3247 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
3249 s
->do_telnetopt
= 1;
3252 ret
= connect(fd
, addr
, addrlen
);
3254 err
= socket_error();
3255 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3256 } else if (err
== EINPROGRESS
) {
3259 } else if (err
== WSAEALREADY
) {
3271 socket_set_nodelay(fd
);
3273 tcp_chr_connect(chr
);
3275 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
3278 if (is_listen
&& is_waitconnect
) {
3279 printf("QEMU waiting for connection on: %s\n", host_str
);
3280 tcp_chr_accept(chr
);
3281 socket_set_nonblock(s
->listen_fd
);
3293 CharDriverState
*qemu_chr_open(const char *filename
)
3297 if (!strcmp(filename
, "vc")) {
3298 return text_console_init(&display_state
, 0);
3299 } else if (strstart(filename
, "vc:", &p
)) {
3300 return text_console_init(&display_state
, p
);
3301 } else if (!strcmp(filename
, "null")) {
3302 return qemu_chr_open_null();
3304 if (strstart(filename
, "tcp:", &p
)) {
3305 return qemu_chr_open_tcp(p
, 0, 0);
3307 if (strstart(filename
, "telnet:", &p
)) {
3308 return qemu_chr_open_tcp(p
, 1, 0);
3310 if (strstart(filename
, "udp:", &p
)) {
3311 return qemu_chr_open_udp(p
);
3313 if (strstart(filename
, "mon:", &p
)) {
3314 CharDriverState
*drv
= qemu_chr_open(p
);
3316 drv
= qemu_chr_open_mux(drv
);
3317 monitor_init(drv
, !nographic
);
3320 printf("Unable to open driver: %s\n", p
);
3324 if (strstart(filename
, "unix:", &p
)) {
3325 return qemu_chr_open_tcp(p
, 0, 1);
3326 } else if (strstart(filename
, "file:", &p
)) {
3327 return qemu_chr_open_file_out(p
);
3328 } else if (strstart(filename
, "pipe:", &p
)) {
3329 return qemu_chr_open_pipe(p
);
3330 } else if (!strcmp(filename
, "pty")) {
3331 return qemu_chr_open_pty();
3332 } else if (!strcmp(filename
, "stdio")) {
3333 return qemu_chr_open_stdio();
3335 #if defined(__linux__)
3336 if (strstart(filename
, "/dev/parport", NULL
)) {
3337 return qemu_chr_open_pp(filename
);
3340 #if defined(__linux__) || defined(__sun__)
3341 if (strstart(filename
, "/dev/", NULL
)) {
3342 return qemu_chr_open_tty(filename
);
3346 if (strstart(filename
, "COM", NULL
)) {
3347 return qemu_chr_open_win(filename
);
3349 if (strstart(filename
, "pipe:", &p
)) {
3350 return qemu_chr_open_win_pipe(p
);
3352 if (strstart(filename
, "con:", NULL
)) {
3353 return qemu_chr_open_win_con(filename
);
3355 if (strstart(filename
, "file:", &p
)) {
3356 return qemu_chr_open_win_file_out(p
);
3364 void qemu_chr_close(CharDriverState
*chr
)
3367 chr
->chr_close(chr
);
3370 /***********************************************************/
3371 /* network device redirectors */
3373 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
3377 for(i
=0;i
<size
;i
+=16) {
3381 fprintf(f
, "%08x ", i
);
3384 fprintf(f
, " %02x", buf
[i
+j
]);
3389 for(j
=0;j
<len
;j
++) {
3391 if (c
< ' ' || c
> '~')
3393 fprintf(f
, "%c", c
);
3399 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
3402 for(i
= 0; i
< 6; i
++) {
3403 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3416 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
3421 p1
= strchr(p
, sep
);
3427 if (len
> buf_size
- 1)
3429 memcpy(buf
, p
, len
);
3436 int parse_host_src_port(struct sockaddr_in
*haddr
,
3437 struct sockaddr_in
*saddr
,
3438 const char *input_str
)
3440 char *str
= strdup(input_str
);
3441 char *host_str
= str
;
3446 * Chop off any extra arguments at the end of the string which
3447 * would start with a comma, then fill in the src port information
3448 * if it was provided else use the "any address" and "any port".
3450 if ((ptr
= strchr(str
,',')))
3453 if ((src_str
= strchr(input_str
,'@'))) {
3458 if (parse_host_port(haddr
, host_str
) < 0)
3461 if (!src_str
|| *src_str
== '\0')
3464 if (parse_host_port(saddr
, src_str
) < 0)
3475 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
3483 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3485 saddr
->sin_family
= AF_INET
;
3486 if (buf
[0] == '\0') {
3487 saddr
->sin_addr
.s_addr
= 0;
3489 if (isdigit(buf
[0])) {
3490 if (!inet_aton(buf
, &saddr
->sin_addr
))
3493 if ((he
= gethostbyname(buf
)) == NULL
)
3495 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
3498 port
= strtol(p
, (char **)&r
, 0);
3501 saddr
->sin_port
= htons(port
);
3506 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
3511 len
= MIN(108, strlen(str
));
3512 p
= strchr(str
, ',');
3514 len
= MIN(len
, p
- str
);
3516 memset(uaddr
, 0, sizeof(*uaddr
));
3518 uaddr
->sun_family
= AF_UNIX
;
3519 memcpy(uaddr
->sun_path
, str
, len
);
3525 /* find or alloc a new VLAN */
3526 VLANState
*qemu_find_vlan(int id
)
3528 VLANState
**pvlan
, *vlan
;
3529 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3533 vlan
= qemu_mallocz(sizeof(VLANState
));
3538 pvlan
= &first_vlan
;
3539 while (*pvlan
!= NULL
)
3540 pvlan
= &(*pvlan
)->next
;
3545 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
3546 IOReadHandler
*fd_read
,
3547 IOCanRWHandler
*fd_can_read
,
3550 VLANClientState
*vc
, **pvc
;
3551 vc
= qemu_mallocz(sizeof(VLANClientState
));
3554 vc
->fd_read
= fd_read
;
3555 vc
->fd_can_read
= fd_can_read
;
3556 vc
->opaque
= opaque
;
3560 pvc
= &vlan
->first_client
;
3561 while (*pvc
!= NULL
)
3562 pvc
= &(*pvc
)->next
;
3567 int qemu_can_send_packet(VLANClientState
*vc1
)
3569 VLANState
*vlan
= vc1
->vlan
;
3570 VLANClientState
*vc
;
3572 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3574 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
3581 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
3583 VLANState
*vlan
= vc1
->vlan
;
3584 VLANClientState
*vc
;
3587 printf("vlan %d send:\n", vlan
->id
);
3588 hex_dump(stdout
, buf
, size
);
3590 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3592 vc
->fd_read(vc
->opaque
, buf
, size
);
3597 #if defined(CONFIG_SLIRP)
3599 /* slirp network adapter */
3601 static int slirp_inited
;
3602 static VLANClientState
*slirp_vc
;
3604 int slirp_can_output(void)
3606 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
3609 void slirp_output(const uint8_t *pkt
, int pkt_len
)
3612 printf("slirp output:\n");
3613 hex_dump(stdout
, pkt
, pkt_len
);
3617 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
3620 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
3623 printf("slirp input:\n");
3624 hex_dump(stdout
, buf
, size
);
3626 slirp_input(buf
, size
);
3629 static int net_slirp_init(VLANState
*vlan
)
3631 if (!slirp_inited
) {
3635 slirp_vc
= qemu_new_vlan_client(vlan
,
3636 slirp_receive
, NULL
, NULL
);
3637 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
3641 static void net_slirp_redir(const char *redir_str
)
3646 struct in_addr guest_addr
;
3647 int host_port
, guest_port
;
3649 if (!slirp_inited
) {
3655 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3657 if (!strcmp(buf
, "tcp")) {
3659 } else if (!strcmp(buf
, "udp")) {
3665 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3667 host_port
= strtol(buf
, &r
, 0);
3671 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3673 if (buf
[0] == '\0') {
3674 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
3676 if (!inet_aton(buf
, &guest_addr
))
3679 guest_port
= strtol(p
, &r
, 0);
3683 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3684 fprintf(stderr
, "qemu: could not set up redirection\n");
3689 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3697 static void smb_exit(void)
3701 char filename
[1024];
3703 /* erase all the files in the directory */
3704 d
= opendir(smb_dir
);
3709 if (strcmp(de
->d_name
, ".") != 0 &&
3710 strcmp(de
->d_name
, "..") != 0) {
3711 snprintf(filename
, sizeof(filename
), "%s/%s",
3712 smb_dir
, de
->d_name
);
3720 /* automatic user mode samba server configuration */
3721 void net_slirp_smb(const char *exported_dir
)
3723 char smb_conf
[1024];
3724 char smb_cmdline
[1024];
3727 if (!slirp_inited
) {
3732 /* XXX: better tmp dir construction */
3733 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3734 if (mkdir(smb_dir
, 0700) < 0) {
3735 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3738 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3740 f
= fopen(smb_conf
, "w");
3742 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3749 "socket address=127.0.0.1\n"
3750 "pid directory=%s\n"
3751 "lock directory=%s\n"
3752 "log file=%s/log.smbd\n"
3753 "smb passwd file=%s/smbpasswd\n"
3754 "security = share\n"
3769 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3770 SMBD_COMMAND
, smb_conf
);
3772 slirp_add_exec(0, smb_cmdline
, 4, 139);
3775 #endif /* !defined(_WIN32) */
3776 void do_info_slirp(void)
3781 #endif /* CONFIG_SLIRP */
3783 #if !defined(_WIN32)
3785 typedef struct TAPState
{
3786 VLANClientState
*vc
;
3788 char down_script
[1024];
3791 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3793 TAPState
*s
= opaque
;
3796 ret
= write(s
->fd
, buf
, size
);
3797 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3804 static void tap_send(void *opaque
)
3806 TAPState
*s
= opaque
;
3813 sbuf
.maxlen
= sizeof(buf
);
3815 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
3817 size
= read(s
->fd
, buf
, sizeof(buf
));
3820 qemu_send_packet(s
->vc
, buf
, size
);
3826 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3830 s
= qemu_mallocz(sizeof(TAPState
));
3834 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3835 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3836 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3840 #if defined (_BSD) || defined (__FreeBSD_kernel__)
3841 static int tap_open(char *ifname
, int ifname_size
)
3847 TFR(fd
= open("/dev/tap", O_RDWR
));
3849 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3854 dev
= devname(s
.st_rdev
, S_IFCHR
);
3855 pstrcpy(ifname
, ifname_size
, dev
);
3857 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3860 #elif defined(__sun__)
3861 #define TUNNEWPPA (('T'<<16) | 0x0001)
3863 * Allocate TAP device, returns opened fd.
3864 * Stores dev name in the first arg(must be large enough).
3866 int tap_alloc(char *dev
)
3868 int tap_fd
, if_fd
, ppa
= -1;
3869 static int ip_fd
= 0;
3872 static int arp_fd
= 0;
3873 int ip_muxid
, arp_muxid
;
3874 struct strioctl strioc_if
, strioc_ppa
;
3875 int link_type
= I_PLINK
;;
3877 char actual_name
[32] = "";
3879 memset(&ifr
, 0x0, sizeof(ifr
));
3883 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
3887 /* Check if IP device was opened */
3891 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
3893 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
3897 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
3899 syslog(LOG_ERR
, "Can't open /dev/tap");
3903 /* Assign a new PPA and get its unit number. */
3904 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
3905 strioc_ppa
.ic_timout
= 0;
3906 strioc_ppa
.ic_len
= sizeof(ppa
);
3907 strioc_ppa
.ic_dp
= (char *)&ppa
;
3908 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
3909 syslog (LOG_ERR
, "Can't assign new interface");
3911 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
3913 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
3916 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
3917 syslog(LOG_ERR
, "Can't push IP module");
3921 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
3922 syslog(LOG_ERR
, "Can't get flags\n");
3924 snprintf (actual_name
, 32, "tap%d", ppa
);
3925 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3928 /* Assign ppa according to the unit number returned by tun device */
3930 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
3931 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
3932 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
3933 syslog (LOG_ERR
, "Can't get flags\n");
3934 /* Push arp module to if_fd */
3935 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
3936 syslog (LOG_ERR
, "Can't push ARP module (2)");
3938 /* Push arp module to ip_fd */
3939 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
3940 syslog (LOG_ERR
, "I_POP failed\n");
3941 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
3942 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
3944 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
3946 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
3948 /* Set ifname to arp */
3949 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
3950 strioc_if
.ic_timout
= 0;
3951 strioc_if
.ic_len
= sizeof(ifr
);
3952 strioc_if
.ic_dp
= (char *)&ifr
;
3953 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
3954 syslog (LOG_ERR
, "Can't set ifname to arp\n");
3957 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
3958 syslog(LOG_ERR
, "Can't link TAP device to IP");
3962 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
3963 syslog (LOG_ERR
, "Can't link TAP device to ARP");
3967 memset(&ifr
, 0x0, sizeof(ifr
));
3968 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3969 ifr
.lifr_ip_muxid
= ip_muxid
;
3970 ifr
.lifr_arp_muxid
= arp_muxid
;
3972 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
3974 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
3975 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
3976 syslog (LOG_ERR
, "Can't set multiplexor id");
3979 sprintf(dev
, "tap%d", ppa
);
3983 static int tap_open(char *ifname
, int ifname_size
)
3987 if( (fd
= tap_alloc(dev
)) < 0 ){
3988 fprintf(stderr
, "Cannot allocate TAP device\n");
3991 pstrcpy(ifname
, ifname_size
, dev
);
3992 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3996 static int tap_open(char *ifname
, int ifname_size
)
4001 TFR(fd
= open("/dev/net/tun", O_RDWR
));
4003 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
4006 memset(&ifr
, 0, sizeof(ifr
));
4007 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
4008 if (ifname
[0] != '\0')
4009 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
4011 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
4012 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
4014 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
4018 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
4019 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
4024 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
4030 /* try to launch network script */
4034 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
4035 for (i
= 0; i
< open_max
; i
++)
4036 if (i
!= STDIN_FILENO
&&
4037 i
!= STDOUT_FILENO
&&
4038 i
!= STDERR_FILENO
&&
4043 *parg
++ = (char *)setup_script
;
4044 *parg
++ = (char *)ifname
;
4046 execv(setup_script
, args
);
4049 while (waitpid(pid
, &status
, 0) != pid
);
4050 if (!WIFEXITED(status
) ||
4051 WEXITSTATUS(status
) != 0) {
4052 fprintf(stderr
, "%s: could not launch network script\n",
4060 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
4061 const char *setup_script
, const char *down_script
)
4067 if (ifname1
!= NULL
)
4068 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
4071 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
4075 if (!setup_script
|| !strcmp(setup_script
, "no"))
4077 if (setup_script
[0] != '\0') {
4078 if (launch_script(setup_script
, ifname
, fd
))
4081 s
= net_tap_fd_init(vlan
, fd
);
4084 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4085 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
4086 if (down_script
&& strcmp(down_script
, "no"))
4087 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
4091 #endif /* !_WIN32 */
4093 /* network connection */
4094 typedef struct NetSocketState
{
4095 VLANClientState
*vc
;
4097 int state
; /* 0 = getting length, 1 = getting data */
4101 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
4104 typedef struct NetSocketListenState
{
4107 } NetSocketListenState
;
4109 /* XXX: we consider we can send the whole packet without blocking */
4110 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
4112 NetSocketState
*s
= opaque
;
4116 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
4117 send_all(s
->fd
, buf
, size
);
4120 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
4122 NetSocketState
*s
= opaque
;
4123 sendto(s
->fd
, buf
, size
, 0,
4124 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
4127 static void net_socket_send(void *opaque
)
4129 NetSocketState
*s
= opaque
;
4134 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
4136 err
= socket_error();
4137 if (err
!= EWOULDBLOCK
)
4139 } else if (size
== 0) {
4140 /* end of connection */
4142 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
4148 /* reassemble a packet from the network */
4154 memcpy(s
->buf
+ s
->index
, buf
, l
);
4158 if (s
->index
== 4) {
4160 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
4166 l
= s
->packet_len
- s
->index
;
4169 memcpy(s
->buf
+ s
->index
, buf
, l
);
4173 if (s
->index
>= s
->packet_len
) {
4174 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
4183 static void net_socket_send_dgram(void *opaque
)
4185 NetSocketState
*s
= opaque
;
4188 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
4192 /* end of connection */
4193 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
4196 qemu_send_packet(s
->vc
, s
->buf
, size
);
4199 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
4204 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
4205 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
4206 inet_ntoa(mcastaddr
->sin_addr
),
4207 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
4211 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
4213 perror("socket(PF_INET, SOCK_DGRAM)");
4218 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
4219 (const char *)&val
, sizeof(val
));
4221 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
4225 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
4231 /* Add host to multicast group */
4232 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
4233 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
4235 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
4236 (const char *)&imr
, sizeof(struct ip_mreq
));
4238 perror("setsockopt(IP_ADD_MEMBERSHIP)");
4242 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
4244 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
4245 (const char *)&val
, sizeof(val
));
4247 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
4251 socket_set_nonblock(fd
);
4259 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
4262 struct sockaddr_in saddr
;
4264 socklen_t saddr_len
;
4267 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
4268 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
4269 * by ONLY ONE process: we must "clone" this dgram socket --jjo
4273 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
4275 if (saddr
.sin_addr
.s_addr
==0) {
4276 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
4280 /* clone dgram socket */
4281 newfd
= net_socket_mcast_create(&saddr
);
4283 /* error already reported by net_socket_mcast_create() */
4287 /* clone newfd to fd, close newfd */
4292 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
4293 fd
, strerror(errno
));
4298 s
= qemu_mallocz(sizeof(NetSocketState
));
4303 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
4304 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
4306 /* mcast: save bound address as dst */
4307 if (is_connected
) s
->dgram_dst
=saddr
;
4309 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4310 "socket: fd=%d (%s mcast=%s:%d)",
4311 fd
, is_connected
? "cloned" : "",
4312 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4316 static void net_socket_connect(void *opaque
)
4318 NetSocketState
*s
= opaque
;
4319 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
4322 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
4326 s
= qemu_mallocz(sizeof(NetSocketState
));
4330 s
->vc
= qemu_new_vlan_client(vlan
,
4331 net_socket_receive
, NULL
, s
);
4332 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4333 "socket: fd=%d", fd
);
4335 net_socket_connect(s
);
4337 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
4342 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
4345 int so_type
=-1, optlen
=sizeof(so_type
);
4347 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
4348 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
4353 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
4355 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
4357 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
4358 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
4359 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
4364 static void net_socket_accept(void *opaque
)
4366 NetSocketListenState
*s
= opaque
;
4368 struct sockaddr_in saddr
;
4373 len
= sizeof(saddr
);
4374 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
4375 if (fd
< 0 && errno
!= EINTR
) {
4377 } else if (fd
>= 0) {
4381 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
4385 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
4386 "socket: connection from %s:%d",
4387 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4391 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
4393 NetSocketListenState
*s
;
4395 struct sockaddr_in saddr
;
4397 if (parse_host_port(&saddr
, host_str
) < 0)
4400 s
= qemu_mallocz(sizeof(NetSocketListenState
));
4404 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4409 socket_set_nonblock(fd
);
4411 /* allow fast reuse */
4413 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
4415 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4420 ret
= listen(fd
, 0);
4427 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
4431 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
4434 int fd
, connected
, ret
, err
;
4435 struct sockaddr_in saddr
;
4437 if (parse_host_port(&saddr
, host_str
) < 0)
4440 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4445 socket_set_nonblock(fd
);
4449 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4451 err
= socket_error();
4452 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
4453 } else if (err
== EINPROGRESS
) {
4456 } else if (err
== WSAEALREADY
) {
4469 s
= net_socket_fd_init(vlan
, fd
, connected
);
4472 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4473 "socket: connect to %s:%d",
4474 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4478 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
4482 struct sockaddr_in saddr
;
4484 if (parse_host_port(&saddr
, host_str
) < 0)
4488 fd
= net_socket_mcast_create(&saddr
);
4492 s
= net_socket_fd_init(vlan
, fd
, 0);
4496 s
->dgram_dst
= saddr
;
4498 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4499 "socket: mcast=%s:%d",
4500 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4505 static int get_param_value(char *buf
, int buf_size
,
4506 const char *tag
, const char *str
)
4515 while (*p
!= '\0' && *p
!= '=') {
4516 if ((q
- option
) < sizeof(option
) - 1)
4524 if (!strcmp(tag
, option
)) {
4526 while (*p
!= '\0' && *p
!= ',') {
4527 if ((q
- buf
) < buf_size
- 1)
4534 while (*p
!= '\0' && *p
!= ',') {
4545 static int net_client_init(const char *str
)
4556 while (*p
!= '\0' && *p
!= ',') {
4557 if ((q
- device
) < sizeof(device
) - 1)
4565 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
4566 vlan_id
= strtol(buf
, NULL
, 0);
4568 vlan
= qemu_find_vlan(vlan_id
);
4570 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
4573 if (!strcmp(device
, "nic")) {
4577 if (nb_nics
>= MAX_NICS
) {
4578 fprintf(stderr
, "Too Many NICs\n");
4581 nd
= &nd_table
[nb_nics
];
4582 macaddr
= nd
->macaddr
;
4588 macaddr
[5] = 0x56 + nb_nics
;
4590 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
4591 if (parse_macaddr(macaddr
, buf
) < 0) {
4592 fprintf(stderr
, "invalid syntax for ethernet address\n");
4596 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
4597 nd
->model
= strdup(buf
);
4601 vlan
->nb_guest_devs
++;
4604 if (!strcmp(device
, "none")) {
4605 /* does nothing. It is needed to signal that no network cards
4610 if (!strcmp(device
, "user")) {
4611 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
4612 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
4614 vlan
->nb_host_devs
++;
4615 ret
= net_slirp_init(vlan
);
4619 if (!strcmp(device
, "tap")) {
4621 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4622 fprintf(stderr
, "tap: no interface name\n");
4625 vlan
->nb_host_devs
++;
4626 ret
= tap_win32_init(vlan
, ifname
);
4629 if (!strcmp(device
, "tap")) {
4631 char setup_script
[1024], down_script
[1024];
4633 vlan
->nb_host_devs
++;
4634 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4635 fd
= strtol(buf
, NULL
, 0);
4637 if (net_tap_fd_init(vlan
, fd
))
4640 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4643 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
4644 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
4646 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
4647 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
4649 ret
= net_tap_init(vlan
, ifname
, setup_script
, down_script
);
4653 if (!strcmp(device
, "socket")) {
4654 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4656 fd
= strtol(buf
, NULL
, 0);
4658 if (net_socket_fd_init(vlan
, fd
, 1))
4660 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
4661 ret
= net_socket_listen_init(vlan
, buf
);
4662 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
4663 ret
= net_socket_connect_init(vlan
, buf
);
4664 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
4665 ret
= net_socket_mcast_init(vlan
, buf
);
4667 fprintf(stderr
, "Unknown socket options: %s\n", p
);
4670 vlan
->nb_host_devs
++;
4673 fprintf(stderr
, "Unknown network device: %s\n", device
);
4677 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
4683 void do_info_network(void)
4686 VLANClientState
*vc
;
4688 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
4689 term_printf("VLAN %d devices:\n", vlan
->id
);
4690 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
4691 term_printf(" %s\n", vc
->info_str
);
4695 /***********************************************************/
4698 static USBPort
*used_usb_ports
;
4699 static USBPort
*free_usb_ports
;
4701 /* ??? Maybe change this to register a hub to keep track of the topology. */
4702 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
4703 usb_attachfn attach
)
4705 port
->opaque
= opaque
;
4706 port
->index
= index
;
4707 port
->attach
= attach
;
4708 port
->next
= free_usb_ports
;
4709 free_usb_ports
= port
;
4712 static int usb_device_add(const char *devname
)
4718 if (!free_usb_ports
)
4721 if (strstart(devname
, "host:", &p
)) {
4722 dev
= usb_host_device_open(p
);
4723 } else if (!strcmp(devname
, "mouse")) {
4724 dev
= usb_mouse_init();
4725 } else if (!strcmp(devname
, "tablet")) {
4726 dev
= usb_tablet_init();
4727 } else if (!strcmp(devname
, "keyboard")) {
4728 dev
= usb_keyboard_init();
4729 } else if (strstart(devname
, "disk:", &p
)) {
4730 dev
= usb_msd_init(p
);
4731 } else if (!strcmp(devname
, "wacom-tablet")) {
4732 dev
= usb_wacom_init();
4739 /* Find a USB port to add the device to. */
4740 port
= free_usb_ports
;
4744 /* Create a new hub and chain it on. */
4745 free_usb_ports
= NULL
;
4746 port
->next
= used_usb_ports
;
4747 used_usb_ports
= port
;
4749 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
4750 usb_attach(port
, hub
);
4751 port
= free_usb_ports
;
4754 free_usb_ports
= port
->next
;
4755 port
->next
= used_usb_ports
;
4756 used_usb_ports
= port
;
4757 usb_attach(port
, dev
);
4761 static int usb_device_del(const char *devname
)
4769 if (!used_usb_ports
)
4772 p
= strchr(devname
, '.');
4775 bus_num
= strtoul(devname
, NULL
, 0);
4776 addr
= strtoul(p
+ 1, NULL
, 0);
4780 lastp
= &used_usb_ports
;
4781 port
= used_usb_ports
;
4782 while (port
&& port
->dev
->addr
!= addr
) {
4783 lastp
= &port
->next
;
4791 *lastp
= port
->next
;
4792 usb_attach(port
, NULL
);
4793 dev
->handle_destroy(dev
);
4794 port
->next
= free_usb_ports
;
4795 free_usb_ports
= port
;
4799 void do_usb_add(const char *devname
)
4802 ret
= usb_device_add(devname
);
4804 term_printf("Could not add USB device '%s'\n", devname
);
4807 void do_usb_del(const char *devname
)
4810 ret
= usb_device_del(devname
);
4812 term_printf("Could not remove USB device '%s'\n", devname
);
4819 const char *speed_str
;
4822 term_printf("USB support not enabled\n");
4826 for (port
= used_usb_ports
; port
; port
= port
->next
) {
4830 switch(dev
->speed
) {
4834 case USB_SPEED_FULL
:
4837 case USB_SPEED_HIGH
:
4844 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
4845 0, dev
->addr
, speed_str
, dev
->devname
);
4849 /***********************************************************/
4850 /* PCMCIA/Cardbus */
4852 static struct pcmcia_socket_entry_s
{
4853 struct pcmcia_socket_s
*socket
;
4854 struct pcmcia_socket_entry_s
*next
;
4855 } *pcmcia_sockets
= 0;
4857 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
4859 struct pcmcia_socket_entry_s
*entry
;
4861 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
4862 entry
->socket
= socket
;
4863 entry
->next
= pcmcia_sockets
;
4864 pcmcia_sockets
= entry
;
4867 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
4869 struct pcmcia_socket_entry_s
*entry
, **ptr
;
4871 ptr
= &pcmcia_sockets
;
4872 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
4873 if (entry
->socket
== socket
) {
4879 void pcmcia_info(void)
4881 struct pcmcia_socket_entry_s
*iter
;
4882 if (!pcmcia_sockets
)
4883 term_printf("No PCMCIA sockets\n");
4885 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
4886 term_printf("%s: %s\n", iter
->socket
->slot_string
,
4887 iter
->socket
->attached
? iter
->socket
->card_string
:
4891 /***********************************************************/
4894 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4898 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4902 static void dumb_refresh(DisplayState
*ds
)
4904 #if defined(CONFIG_SDL)
4909 static void dumb_display_init(DisplayState
*ds
)
4914 ds
->dpy_update
= dumb_update
;
4915 ds
->dpy_resize
= dumb_resize
;
4916 ds
->dpy_refresh
= dumb_refresh
;
4919 /***********************************************************/
4922 #define MAX_IO_HANDLERS 64
4924 typedef struct IOHandlerRecord
{
4926 IOCanRWHandler
*fd_read_poll
;
4928 IOHandler
*fd_write
;
4931 /* temporary data */
4933 struct IOHandlerRecord
*next
;
4936 static IOHandlerRecord
*first_io_handler
;
4938 /* XXX: fd_read_poll should be suppressed, but an API change is
4939 necessary in the character devices to suppress fd_can_read(). */
4940 int qemu_set_fd_handler2(int fd
,
4941 IOCanRWHandler
*fd_read_poll
,
4943 IOHandler
*fd_write
,
4946 IOHandlerRecord
**pioh
, *ioh
;
4948 if (!fd_read
&& !fd_write
) {
4949 pioh
= &first_io_handler
;
4954 if (ioh
->fd
== fd
) {
4961 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4965 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4968 ioh
->next
= first_io_handler
;
4969 first_io_handler
= ioh
;
4972 ioh
->fd_read_poll
= fd_read_poll
;
4973 ioh
->fd_read
= fd_read
;
4974 ioh
->fd_write
= fd_write
;
4975 ioh
->opaque
= opaque
;
4981 int qemu_set_fd_handler(int fd
,
4983 IOHandler
*fd_write
,
4986 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4989 /***********************************************************/
4990 /* Polling handling */
4992 typedef struct PollingEntry
{
4995 struct PollingEntry
*next
;
4998 static PollingEntry
*first_polling_entry
;
5000 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
5002 PollingEntry
**ppe
, *pe
;
5003 pe
= qemu_mallocz(sizeof(PollingEntry
));
5007 pe
->opaque
= opaque
;
5008 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
5013 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
5015 PollingEntry
**ppe
, *pe
;
5016 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
5018 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
5027 /***********************************************************/
5028 /* Wait objects support */
5029 typedef struct WaitObjects
{
5031 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
5032 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
5033 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
5036 static WaitObjects wait_objects
= {0};
5038 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
5040 WaitObjects
*w
= &wait_objects
;
5042 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
5044 w
->events
[w
->num
] = handle
;
5045 w
->func
[w
->num
] = func
;
5046 w
->opaque
[w
->num
] = opaque
;
5051 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
5054 WaitObjects
*w
= &wait_objects
;
5057 for (i
= 0; i
< w
->num
; i
++) {
5058 if (w
->events
[i
] == handle
)
5061 w
->events
[i
] = w
->events
[i
+ 1];
5062 w
->func
[i
] = w
->func
[i
+ 1];
5063 w
->opaque
[i
] = w
->opaque
[i
+ 1];
5071 /***********************************************************/
5072 /* savevm/loadvm support */
5074 #define IO_BUF_SIZE 32768
5078 BlockDriverState
*bs
;
5081 int64_t base_offset
;
5082 int64_t buf_offset
; /* start of buffer when writing, end of buffer
5085 int buf_size
; /* 0 when writing */
5086 uint8_t buf
[IO_BUF_SIZE
];
5089 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
5093 f
= qemu_mallocz(sizeof(QEMUFile
));
5096 if (!strcmp(mode
, "wb")) {
5098 } else if (!strcmp(mode
, "rb")) {
5103 f
->outfile
= fopen(filename
, mode
);
5115 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
5119 f
= qemu_mallocz(sizeof(QEMUFile
));
5124 f
->is_writable
= is_writable
;
5125 f
->base_offset
= offset
;
5129 void qemu_fflush(QEMUFile
*f
)
5131 if (!f
->is_writable
)
5133 if (f
->buf_index
> 0) {
5135 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
5136 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
5138 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
5139 f
->buf
, f
->buf_index
);
5141 f
->buf_offset
+= f
->buf_index
;
5146 static void qemu_fill_buffer(QEMUFile
*f
)
5153 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
5154 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
5158 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
5159 f
->buf
, IO_BUF_SIZE
);
5165 f
->buf_offset
+= len
;
5168 void qemu_fclose(QEMUFile
*f
)
5178 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
5182 l
= IO_BUF_SIZE
- f
->buf_index
;
5185 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
5189 if (f
->buf_index
>= IO_BUF_SIZE
)
5194 void qemu_put_byte(QEMUFile
*f
, int v
)
5196 f
->buf
[f
->buf_index
++] = v
;
5197 if (f
->buf_index
>= IO_BUF_SIZE
)
5201 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
5207 l
= f
->buf_size
- f
->buf_index
;
5209 qemu_fill_buffer(f
);
5210 l
= f
->buf_size
- f
->buf_index
;
5216 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
5221 return size1
- size
;
5224 int qemu_get_byte(QEMUFile
*f
)
5226 if (f
->buf_index
>= f
->buf_size
) {
5227 qemu_fill_buffer(f
);
5228 if (f
->buf_index
>= f
->buf_size
)
5231 return f
->buf
[f
->buf_index
++];
5234 int64_t qemu_ftell(QEMUFile
*f
)
5236 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
5239 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
5241 if (whence
== SEEK_SET
) {
5243 } else if (whence
== SEEK_CUR
) {
5244 pos
+= qemu_ftell(f
);
5246 /* SEEK_END not supported */
5249 if (f
->is_writable
) {
5251 f
->buf_offset
= pos
;
5253 f
->buf_offset
= pos
;
5260 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
5262 qemu_put_byte(f
, v
>> 8);
5263 qemu_put_byte(f
, v
);
5266 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
5268 qemu_put_byte(f
, v
>> 24);
5269 qemu_put_byte(f
, v
>> 16);
5270 qemu_put_byte(f
, v
>> 8);
5271 qemu_put_byte(f
, v
);
5274 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
5276 qemu_put_be32(f
, v
>> 32);
5277 qemu_put_be32(f
, v
);
5280 unsigned int qemu_get_be16(QEMUFile
*f
)
5283 v
= qemu_get_byte(f
) << 8;
5284 v
|= qemu_get_byte(f
);
5288 unsigned int qemu_get_be32(QEMUFile
*f
)
5291 v
= qemu_get_byte(f
) << 24;
5292 v
|= qemu_get_byte(f
) << 16;
5293 v
|= qemu_get_byte(f
) << 8;
5294 v
|= qemu_get_byte(f
);
5298 uint64_t qemu_get_be64(QEMUFile
*f
)
5301 v
= (uint64_t)qemu_get_be32(f
) << 32;
5302 v
|= qemu_get_be32(f
);
5306 typedef struct SaveStateEntry
{
5310 SaveStateHandler
*save_state
;
5311 LoadStateHandler
*load_state
;
5313 struct SaveStateEntry
*next
;
5316 static SaveStateEntry
*first_se
;
5318 int register_savevm(const char *idstr
,
5321 SaveStateHandler
*save_state
,
5322 LoadStateHandler
*load_state
,
5325 SaveStateEntry
*se
, **pse
;
5327 se
= qemu_malloc(sizeof(SaveStateEntry
));
5330 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
5331 se
->instance_id
= instance_id
;
5332 se
->version_id
= version_id
;
5333 se
->save_state
= save_state
;
5334 se
->load_state
= load_state
;
5335 se
->opaque
= opaque
;
5338 /* add at the end of list */
5340 while (*pse
!= NULL
)
5341 pse
= &(*pse
)->next
;
5346 #define QEMU_VM_FILE_MAGIC 0x5145564d
5347 #define QEMU_VM_FILE_VERSION 0x00000002
5349 int qemu_savevm_state(QEMUFile
*f
)
5353 int64_t cur_pos
, len_pos
, total_len_pos
;
5355 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
5356 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
5357 total_len_pos
= qemu_ftell(f
);
5358 qemu_put_be64(f
, 0); /* total size */
5360 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5362 len
= strlen(se
->idstr
);
5363 qemu_put_byte(f
, len
);
5364 qemu_put_buffer(f
, se
->idstr
, len
);
5366 qemu_put_be32(f
, se
->instance_id
);
5367 qemu_put_be32(f
, se
->version_id
);
5369 /* record size: filled later */
5370 len_pos
= qemu_ftell(f
);
5371 qemu_put_be32(f
, 0);
5373 se
->save_state(f
, se
->opaque
);
5375 /* fill record size */
5376 cur_pos
= qemu_ftell(f
);
5377 len
= cur_pos
- len_pos
- 4;
5378 qemu_fseek(f
, len_pos
, SEEK_SET
);
5379 qemu_put_be32(f
, len
);
5380 qemu_fseek(f
, cur_pos
, SEEK_SET
);
5382 cur_pos
= qemu_ftell(f
);
5383 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
5384 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
5385 qemu_fseek(f
, cur_pos
, SEEK_SET
);
5391 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
5395 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5396 if (!strcmp(se
->idstr
, idstr
) &&
5397 instance_id
== se
->instance_id
)
5403 int qemu_loadvm_state(QEMUFile
*f
)
5406 int len
, ret
, instance_id
, record_len
, version_id
;
5407 int64_t total_len
, end_pos
, cur_pos
;
5411 v
= qemu_get_be32(f
);
5412 if (v
!= QEMU_VM_FILE_MAGIC
)
5414 v
= qemu_get_be32(f
);
5415 if (v
!= QEMU_VM_FILE_VERSION
) {
5420 total_len
= qemu_get_be64(f
);
5421 end_pos
= total_len
+ qemu_ftell(f
);
5423 if (qemu_ftell(f
) >= end_pos
)
5425 len
= qemu_get_byte(f
);
5426 qemu_get_buffer(f
, idstr
, len
);
5428 instance_id
= qemu_get_be32(f
);
5429 version_id
= qemu_get_be32(f
);
5430 record_len
= qemu_get_be32(f
);
5432 printf("idstr=%s instance=0x%x version=%d len=%d\n",
5433 idstr
, instance_id
, version_id
, record_len
);
5435 cur_pos
= qemu_ftell(f
);
5436 se
= find_se(idstr
, instance_id
);
5438 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
5439 instance_id
, idstr
);
5441 ret
= se
->load_state(f
, se
->opaque
, version_id
);
5443 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
5444 instance_id
, idstr
);
5447 /* always seek to exact end of record */
5448 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
5455 /* device can contain snapshots */
5456 static int bdrv_can_snapshot(BlockDriverState
*bs
)
5459 !bdrv_is_removable(bs
) &&
5460 !bdrv_is_read_only(bs
));
5463 /* device must be snapshots in order to have a reliable snapshot */
5464 static int bdrv_has_snapshot(BlockDriverState
*bs
)
5467 !bdrv_is_removable(bs
) &&
5468 !bdrv_is_read_only(bs
));
5471 static BlockDriverState
*get_bs_snapshots(void)
5473 BlockDriverState
*bs
;
5477 return bs_snapshots
;
5478 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5480 if (bdrv_can_snapshot(bs
))
5489 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
5492 QEMUSnapshotInfo
*sn_tab
, *sn
;
5496 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5499 for(i
= 0; i
< nb_sns
; i
++) {
5501 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
5511 void do_savevm(const char *name
)
5513 BlockDriverState
*bs
, *bs1
;
5514 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
5515 int must_delete
, ret
, i
;
5516 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5518 int saved_vm_running
;
5525 bs
= get_bs_snapshots();
5527 term_printf("No block device can accept snapshots\n");
5531 /* ??? Should this occur after vm_stop? */
5534 saved_vm_running
= vm_running
;
5539 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
5544 memset(sn
, 0, sizeof(*sn
));
5546 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
5547 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
5550 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
5553 /* fill auxiliary fields */
5556 sn
->date_sec
= tb
.time
;
5557 sn
->date_nsec
= tb
.millitm
* 1000000;
5559 gettimeofday(&tv
, NULL
);
5560 sn
->date_sec
= tv
.tv_sec
;
5561 sn
->date_nsec
= tv
.tv_usec
* 1000;
5563 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
5565 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5566 term_printf("Device %s does not support VM state snapshots\n",
5567 bdrv_get_device_name(bs
));
5571 /* save the VM state */
5572 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
5574 term_printf("Could not open VM state file\n");
5577 ret
= qemu_savevm_state(f
);
5578 sn
->vm_state_size
= qemu_ftell(f
);
5581 term_printf("Error %d while writing VM\n", ret
);
5585 /* create the snapshots */
5587 for(i
= 0; i
< MAX_DISKS
; i
++) {
5589 if (bdrv_has_snapshot(bs1
)) {
5591 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
5593 term_printf("Error while deleting snapshot on '%s'\n",
5594 bdrv_get_device_name(bs1
));
5597 ret
= bdrv_snapshot_create(bs1
, sn
);
5599 term_printf("Error while creating snapshot on '%s'\n",
5600 bdrv_get_device_name(bs1
));
5606 if (saved_vm_running
)
5610 void do_loadvm(const char *name
)
5612 BlockDriverState
*bs
, *bs1
;
5613 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5616 int saved_vm_running
;
5618 bs
= get_bs_snapshots();
5620 term_printf("No block device supports snapshots\n");
5624 /* Flush all IO requests so they don't interfere with the new state. */
5627 saved_vm_running
= vm_running
;
5630 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5632 if (bdrv_has_snapshot(bs1
)) {
5633 ret
= bdrv_snapshot_goto(bs1
, name
);
5636 term_printf("Warning: ");
5639 term_printf("Snapshots not supported on device '%s'\n",
5640 bdrv_get_device_name(bs1
));
5643 term_printf("Could not find snapshot '%s' on device '%s'\n",
5644 name
, bdrv_get_device_name(bs1
));
5647 term_printf("Error %d while activating snapshot on '%s'\n",
5648 ret
, bdrv_get_device_name(bs1
));
5651 /* fatal on snapshot block device */
5658 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5659 term_printf("Device %s does not support VM state snapshots\n",
5660 bdrv_get_device_name(bs
));
5664 /* restore the VM state */
5665 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
5667 term_printf("Could not open VM state file\n");
5670 ret
= qemu_loadvm_state(f
);
5673 term_printf("Error %d while loading VM state\n", ret
);
5676 if (saved_vm_running
)
5680 void do_delvm(const char *name
)
5682 BlockDriverState
*bs
, *bs1
;
5685 bs
= get_bs_snapshots();
5687 term_printf("No block device supports snapshots\n");
5691 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5693 if (bdrv_has_snapshot(bs1
)) {
5694 ret
= bdrv_snapshot_delete(bs1
, name
);
5696 if (ret
== -ENOTSUP
)
5697 term_printf("Snapshots not supported on device '%s'\n",
5698 bdrv_get_device_name(bs1
));
5700 term_printf("Error %d while deleting snapshot on '%s'\n",
5701 ret
, bdrv_get_device_name(bs1
));
5707 void do_info_snapshots(void)
5709 BlockDriverState
*bs
, *bs1
;
5710 QEMUSnapshotInfo
*sn_tab
, *sn
;
5714 bs
= get_bs_snapshots();
5716 term_printf("No available block device supports snapshots\n");
5719 term_printf("Snapshot devices:");
5720 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5722 if (bdrv_has_snapshot(bs1
)) {
5724 term_printf(" %s", bdrv_get_device_name(bs1
));
5729 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5731 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
5734 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
5735 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
5736 for(i
= 0; i
< nb_sns
; i
++) {
5738 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
5743 /***********************************************************/
5744 /* cpu save/restore */
5746 #if defined(TARGET_I386)
5748 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
5750 qemu_put_be32(f
, dt
->selector
);
5751 qemu_put_betl(f
, dt
->base
);
5752 qemu_put_be32(f
, dt
->limit
);
5753 qemu_put_be32(f
, dt
->flags
);
5756 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
5758 dt
->selector
= qemu_get_be32(f
);
5759 dt
->base
= qemu_get_betl(f
);
5760 dt
->limit
= qemu_get_be32(f
);
5761 dt
->flags
= qemu_get_be32(f
);
5764 void cpu_save(QEMUFile
*f
, void *opaque
)
5766 CPUState
*env
= opaque
;
5767 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
5771 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5772 qemu_put_betls(f
, &env
->regs
[i
]);
5773 qemu_put_betls(f
, &env
->eip
);
5774 qemu_put_betls(f
, &env
->eflags
);
5775 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
5776 qemu_put_be32s(f
, &hflags
);
5780 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
5782 for(i
= 0; i
< 8; i
++) {
5783 fptag
|= ((!env
->fptags
[i
]) << i
);
5786 qemu_put_be16s(f
, &fpuc
);
5787 qemu_put_be16s(f
, &fpus
);
5788 qemu_put_be16s(f
, &fptag
);
5790 #ifdef USE_X86LDOUBLE
5795 qemu_put_be16s(f
, &fpregs_format
);
5797 for(i
= 0; i
< 8; i
++) {
5798 #ifdef USE_X86LDOUBLE
5802 /* we save the real CPU data (in case of MMX usage only 'mant'
5803 contains the MMX register */
5804 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
5805 qemu_put_be64(f
, mant
);
5806 qemu_put_be16(f
, exp
);
5809 /* if we use doubles for float emulation, we save the doubles to
5810 avoid losing information in case of MMX usage. It can give
5811 problems if the image is restored on a CPU where long
5812 doubles are used instead. */
5813 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
5817 for(i
= 0; i
< 6; i
++)
5818 cpu_put_seg(f
, &env
->segs
[i
]);
5819 cpu_put_seg(f
, &env
->ldt
);
5820 cpu_put_seg(f
, &env
->tr
);
5821 cpu_put_seg(f
, &env
->gdt
);
5822 cpu_put_seg(f
, &env
->idt
);
5824 qemu_put_be32s(f
, &env
->sysenter_cs
);
5825 qemu_put_be32s(f
, &env
->sysenter_esp
);
5826 qemu_put_be32s(f
, &env
->sysenter_eip
);
5828 qemu_put_betls(f
, &env
->cr
[0]);
5829 qemu_put_betls(f
, &env
->cr
[2]);
5830 qemu_put_betls(f
, &env
->cr
[3]);
5831 qemu_put_betls(f
, &env
->cr
[4]);
5833 for(i
= 0; i
< 8; i
++)
5834 qemu_put_betls(f
, &env
->dr
[i
]);
5837 qemu_put_be32s(f
, &env
->a20_mask
);
5840 qemu_put_be32s(f
, &env
->mxcsr
);
5841 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5842 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5843 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5846 #ifdef TARGET_X86_64
5847 qemu_put_be64s(f
, &env
->efer
);
5848 qemu_put_be64s(f
, &env
->star
);
5849 qemu_put_be64s(f
, &env
->lstar
);
5850 qemu_put_be64s(f
, &env
->cstar
);
5851 qemu_put_be64s(f
, &env
->fmask
);
5852 qemu_put_be64s(f
, &env
->kernelgsbase
);
5854 qemu_put_be32s(f
, &env
->smbase
);
5857 #ifdef USE_X86LDOUBLE
5858 /* XXX: add that in a FPU generic layer */
5859 union x86_longdouble
{
5864 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
5865 #define EXPBIAS1 1023
5866 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
5867 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
5869 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
5873 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
5874 /* exponent + sign */
5875 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
5876 e
|= SIGND1(temp
) >> 16;
5881 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5883 CPUState
*env
= opaque
;
5886 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
5888 if (version_id
!= 3 && version_id
!= 4)
5890 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5891 qemu_get_betls(f
, &env
->regs
[i
]);
5892 qemu_get_betls(f
, &env
->eip
);
5893 qemu_get_betls(f
, &env
->eflags
);
5894 qemu_get_be32s(f
, &hflags
);
5896 qemu_get_be16s(f
, &fpuc
);
5897 qemu_get_be16s(f
, &fpus
);
5898 qemu_get_be16s(f
, &fptag
);
5899 qemu_get_be16s(f
, &fpregs_format
);
5901 /* NOTE: we cannot always restore the FPU state if the image come
5902 from a host with a different 'USE_X86LDOUBLE' define. We guess
5903 if we are in an MMX state to restore correctly in that case. */
5904 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
5905 for(i
= 0; i
< 8; i
++) {
5909 switch(fpregs_format
) {
5911 mant
= qemu_get_be64(f
);
5912 exp
= qemu_get_be16(f
);
5913 #ifdef USE_X86LDOUBLE
5914 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5916 /* difficult case */
5918 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5920 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5924 mant
= qemu_get_be64(f
);
5925 #ifdef USE_X86LDOUBLE
5927 union x86_longdouble
*p
;
5928 /* difficult case */
5929 p
= (void *)&env
->fpregs
[i
];
5934 fp64_to_fp80(p
, mant
);
5938 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5947 /* XXX: restore FPU round state */
5948 env
->fpstt
= (fpus
>> 11) & 7;
5949 env
->fpus
= fpus
& ~0x3800;
5951 for(i
= 0; i
< 8; i
++) {
5952 env
->fptags
[i
] = (fptag
>> i
) & 1;
5955 for(i
= 0; i
< 6; i
++)
5956 cpu_get_seg(f
, &env
->segs
[i
]);
5957 cpu_get_seg(f
, &env
->ldt
);
5958 cpu_get_seg(f
, &env
->tr
);
5959 cpu_get_seg(f
, &env
->gdt
);
5960 cpu_get_seg(f
, &env
->idt
);
5962 qemu_get_be32s(f
, &env
->sysenter_cs
);
5963 qemu_get_be32s(f
, &env
->sysenter_esp
);
5964 qemu_get_be32s(f
, &env
->sysenter_eip
);
5966 qemu_get_betls(f
, &env
->cr
[0]);
5967 qemu_get_betls(f
, &env
->cr
[2]);
5968 qemu_get_betls(f
, &env
->cr
[3]);
5969 qemu_get_betls(f
, &env
->cr
[4]);
5971 for(i
= 0; i
< 8; i
++)
5972 qemu_get_betls(f
, &env
->dr
[i
]);
5975 qemu_get_be32s(f
, &env
->a20_mask
);
5977 qemu_get_be32s(f
, &env
->mxcsr
);
5978 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5979 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5980 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5983 #ifdef TARGET_X86_64
5984 qemu_get_be64s(f
, &env
->efer
);
5985 qemu_get_be64s(f
, &env
->star
);
5986 qemu_get_be64s(f
, &env
->lstar
);
5987 qemu_get_be64s(f
, &env
->cstar
);
5988 qemu_get_be64s(f
, &env
->fmask
);
5989 qemu_get_be64s(f
, &env
->kernelgsbase
);
5991 if (version_id
>= 4)
5992 qemu_get_be32s(f
, &env
->smbase
);
5994 /* XXX: compute hflags from scratch, except for CPL and IIF */
5995 env
->hflags
= hflags
;
6000 #elif defined(TARGET_PPC)
6001 void cpu_save(QEMUFile
*f
, void *opaque
)
6005 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6010 #elif defined(TARGET_MIPS)
6011 void cpu_save(QEMUFile
*f
, void *opaque
)
6015 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6020 #elif defined(TARGET_SPARC)
6021 void cpu_save(QEMUFile
*f
, void *opaque
)
6023 CPUState
*env
= opaque
;
6027 for(i
= 0; i
< 8; i
++)
6028 qemu_put_betls(f
, &env
->gregs
[i
]);
6029 for(i
= 0; i
< NWINDOWS
* 16; i
++)
6030 qemu_put_betls(f
, &env
->regbase
[i
]);
6033 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
6039 qemu_put_be32(f
, u
.i
);
6042 qemu_put_betls(f
, &env
->pc
);
6043 qemu_put_betls(f
, &env
->npc
);
6044 qemu_put_betls(f
, &env
->y
);
6046 qemu_put_be32(f
, tmp
);
6047 qemu_put_betls(f
, &env
->fsr
);
6048 qemu_put_betls(f
, &env
->tbr
);
6049 #ifndef TARGET_SPARC64
6050 qemu_put_be32s(f
, &env
->wim
);
6052 for(i
= 0; i
< 16; i
++)
6053 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
6057 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6059 CPUState
*env
= opaque
;
6063 for(i
= 0; i
< 8; i
++)
6064 qemu_get_betls(f
, &env
->gregs
[i
]);
6065 for(i
= 0; i
< NWINDOWS
* 16; i
++)
6066 qemu_get_betls(f
, &env
->regbase
[i
]);
6069 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
6074 u
.i
= qemu_get_be32(f
);
6078 qemu_get_betls(f
, &env
->pc
);
6079 qemu_get_betls(f
, &env
->npc
);
6080 qemu_get_betls(f
, &env
->y
);
6081 tmp
= qemu_get_be32(f
);
6082 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
6083 correctly updated */
6085 qemu_get_betls(f
, &env
->fsr
);
6086 qemu_get_betls(f
, &env
->tbr
);
6087 #ifndef TARGET_SPARC64
6088 qemu_get_be32s(f
, &env
->wim
);
6090 for(i
= 0; i
< 16; i
++)
6091 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
6097 #elif defined(TARGET_ARM)
6099 void cpu_save(QEMUFile
*f
, void *opaque
)
6102 CPUARMState
*env
= (CPUARMState
*)opaque
;
6104 for (i
= 0; i
< 16; i
++) {
6105 qemu_put_be32(f
, env
->regs
[i
]);
6107 qemu_put_be32(f
, cpsr_read(env
));
6108 qemu_put_be32(f
, env
->spsr
);
6109 for (i
= 0; i
< 6; i
++) {
6110 qemu_put_be32(f
, env
->banked_spsr
[i
]);
6111 qemu_put_be32(f
, env
->banked_r13
[i
]);
6112 qemu_put_be32(f
, env
->banked_r14
[i
]);
6114 for (i
= 0; i
< 5; i
++) {
6115 qemu_put_be32(f
, env
->usr_regs
[i
]);
6116 qemu_put_be32(f
, env
->fiq_regs
[i
]);
6118 qemu_put_be32(f
, env
->cp15
.c0_cpuid
);
6119 qemu_put_be32(f
, env
->cp15
.c0_cachetype
);
6120 qemu_put_be32(f
, env
->cp15
.c1_sys
);
6121 qemu_put_be32(f
, env
->cp15
.c1_coproc
);
6122 qemu_put_be32(f
, env
->cp15
.c1_xscaleauxcr
);
6123 qemu_put_be32(f
, env
->cp15
.c2_base0
);
6124 qemu_put_be32(f
, env
->cp15
.c2_base1
);
6125 qemu_put_be32(f
, env
->cp15
.c2_mask
);
6126 qemu_put_be32(f
, env
->cp15
.c2_data
);
6127 qemu_put_be32(f
, env
->cp15
.c2_insn
);
6128 qemu_put_be32(f
, env
->cp15
.c3
);
6129 qemu_put_be32(f
, env
->cp15
.c5_insn
);
6130 qemu_put_be32(f
, env
->cp15
.c5_data
);
6131 for (i
= 0; i
< 8; i
++) {
6132 qemu_put_be32(f
, env
->cp15
.c6_region
[i
]);
6134 qemu_put_be32(f
, env
->cp15
.c6_insn
);
6135 qemu_put_be32(f
, env
->cp15
.c6_data
);
6136 qemu_put_be32(f
, env
->cp15
.c9_insn
);
6137 qemu_put_be32(f
, env
->cp15
.c9_data
);
6138 qemu_put_be32(f
, env
->cp15
.c13_fcse
);
6139 qemu_put_be32(f
, env
->cp15
.c13_context
);
6140 qemu_put_be32(f
, env
->cp15
.c13_tls1
);
6141 qemu_put_be32(f
, env
->cp15
.c13_tls2
);
6142 qemu_put_be32(f
, env
->cp15
.c13_tls3
);
6143 qemu_put_be32(f
, env
->cp15
.c15_cpar
);
6145 qemu_put_be32(f
, env
->features
);
6147 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
6148 for (i
= 0; i
< 16; i
++) {
6150 u
.d
= env
->vfp
.regs
[i
];
6151 qemu_put_be32(f
, u
.l
.upper
);
6152 qemu_put_be32(f
, u
.l
.lower
);
6154 for (i
= 0; i
< 16; i
++) {
6155 qemu_put_be32(f
, env
->vfp
.xregs
[i
]);
6158 /* TODO: Should use proper FPSCR access functions. */
6159 qemu_put_be32(f
, env
->vfp
.vec_len
);
6160 qemu_put_be32(f
, env
->vfp
.vec_stride
);
6162 if (arm_feature(env
, ARM_FEATURE_VFP3
)) {
6163 for (i
= 16; i
< 32; i
++) {
6165 u
.d
= env
->vfp
.regs
[i
];
6166 qemu_put_be32(f
, u
.l
.upper
);
6167 qemu_put_be32(f
, u
.l
.lower
);
6172 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
6173 for (i
= 0; i
< 16; i
++) {
6174 qemu_put_be64(f
, env
->iwmmxt
.regs
[i
]);
6176 for (i
= 0; i
< 16; i
++) {
6177 qemu_put_be32(f
, env
->iwmmxt
.cregs
[i
]);
6181 if (arm_feature(env
, ARM_FEATURE_M
)) {
6182 qemu_put_be32(f
, env
->v7m
.other_sp
);
6183 qemu_put_be32(f
, env
->v7m
.vecbase
);
6184 qemu_put_be32(f
, env
->v7m
.basepri
);
6185 qemu_put_be32(f
, env
->v7m
.control
);
6186 qemu_put_be32(f
, env
->v7m
.current_sp
);
6187 qemu_put_be32(f
, env
->v7m
.exception
);
6191 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6193 CPUARMState
*env
= (CPUARMState
*)opaque
;
6196 if (version_id
!= ARM_CPU_SAVE_VERSION
)
6199 for (i
= 0; i
< 16; i
++) {
6200 env
->regs
[i
] = qemu_get_be32(f
);
6202 cpsr_write(env
, qemu_get_be32(f
), 0xffffffff);
6203 env
->spsr
= qemu_get_be32(f
);
6204 for (i
= 0; i
< 6; i
++) {
6205 env
->banked_spsr
[i
] = qemu_get_be32(f
);
6206 env
->banked_r13
[i
] = qemu_get_be32(f
);
6207 env
->banked_r14
[i
] = qemu_get_be32(f
);
6209 for (i
= 0; i
< 5; i
++) {
6210 env
->usr_regs
[i
] = qemu_get_be32(f
);
6211 env
->fiq_regs
[i
] = qemu_get_be32(f
);
6213 env
->cp15
.c0_cpuid
= qemu_get_be32(f
);
6214 env
->cp15
.c0_cachetype
= qemu_get_be32(f
);
6215 env
->cp15
.c1_sys
= qemu_get_be32(f
);
6216 env
->cp15
.c1_coproc
= qemu_get_be32(f
);
6217 env
->cp15
.c1_xscaleauxcr
= qemu_get_be32(f
);
6218 env
->cp15
.c2_base0
= qemu_get_be32(f
);
6219 env
->cp15
.c2_base1
= qemu_get_be32(f
);
6220 env
->cp15
.c2_mask
= qemu_get_be32(f
);
6221 env
->cp15
.c2_data
= qemu_get_be32(f
);
6222 env
->cp15
.c2_insn
= qemu_get_be32(f
);
6223 env
->cp15
.c3
= qemu_get_be32(f
);
6224 env
->cp15
.c5_insn
= qemu_get_be32(f
);
6225 env
->cp15
.c5_data
= qemu_get_be32(f
);
6226 for (i
= 0; i
< 8; i
++) {
6227 env
->cp15
.c6_region
[i
] = qemu_get_be32(f
);
6229 env
->cp15
.c6_insn
= qemu_get_be32(f
);
6230 env
->cp15
.c6_data
= qemu_get_be32(f
);
6231 env
->cp15
.c9_insn
= qemu_get_be32(f
);
6232 env
->cp15
.c9_data
= qemu_get_be32(f
);
6233 env
->cp15
.c13_fcse
= qemu_get_be32(f
);
6234 env
->cp15
.c13_context
= qemu_get_be32(f
);
6235 env
->cp15
.c13_tls1
= qemu_get_be32(f
);
6236 env
->cp15
.c13_tls2
= qemu_get_be32(f
);
6237 env
->cp15
.c13_tls3
= qemu_get_be32(f
);
6238 env
->cp15
.c15_cpar
= qemu_get_be32(f
);
6240 env
->features
= qemu_get_be32(f
);
6242 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
6243 for (i
= 0; i
< 16; i
++) {
6245 u
.l
.upper
= qemu_get_be32(f
);
6246 u
.l
.lower
= qemu_get_be32(f
);
6247 env
->vfp
.regs
[i
] = u
.d
;
6249 for (i
= 0; i
< 16; i
++) {
6250 env
->vfp
.xregs
[i
] = qemu_get_be32(f
);
6253 /* TODO: Should use proper FPSCR access functions. */
6254 env
->vfp
.vec_len
= qemu_get_be32(f
);
6255 env
->vfp
.vec_stride
= qemu_get_be32(f
);
6257 if (arm_feature(env
, ARM_FEATURE_VFP3
)) {
6258 for (i
= 0; i
< 16; i
++) {
6260 u
.l
.upper
= qemu_get_be32(f
);
6261 u
.l
.lower
= qemu_get_be32(f
);
6262 env
->vfp
.regs
[i
] = u
.d
;
6267 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
6268 for (i
= 0; i
< 16; i
++) {
6269 env
->iwmmxt
.regs
[i
] = qemu_get_be64(f
);
6271 for (i
= 0; i
< 16; i
++) {
6272 env
->iwmmxt
.cregs
[i
] = qemu_get_be32(f
);
6276 if (arm_feature(env
, ARM_FEATURE_M
)) {
6277 env
->v7m
.other_sp
= qemu_get_be32(f
);
6278 env
->v7m
.vecbase
= qemu_get_be32(f
);
6279 env
->v7m
.basepri
= qemu_get_be32(f
);
6280 env
->v7m
.control
= qemu_get_be32(f
);
6281 env
->v7m
.current_sp
= qemu_get_be32(f
);
6282 env
->v7m
.exception
= qemu_get_be32(f
);
6290 #warning No CPU save/restore functions
6294 /***********************************************************/
6295 /* ram save/restore */
6297 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
6301 v
= qemu_get_byte(f
);
6304 if (qemu_get_buffer(f
, buf
, len
) != len
)
6308 v
= qemu_get_byte(f
);
6309 memset(buf
, v
, len
);
6317 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
6321 if (qemu_get_be32(f
) != phys_ram_size
)
6323 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
6324 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
6331 #define BDRV_HASH_BLOCK_SIZE 1024
6332 #define IOBUF_SIZE 4096
6333 #define RAM_CBLOCK_MAGIC 0xfabe
6335 typedef struct RamCompressState
{
6338 uint8_t buf
[IOBUF_SIZE
];
6341 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
6344 memset(s
, 0, sizeof(*s
));
6346 ret
= deflateInit2(&s
->zstream
, 1,
6348 9, Z_DEFAULT_STRATEGY
);
6351 s
->zstream
.avail_out
= IOBUF_SIZE
;
6352 s
->zstream
.next_out
= s
->buf
;
6356 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
6358 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
6359 qemu_put_be16(s
->f
, len
);
6360 qemu_put_buffer(s
->f
, buf
, len
);
6363 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
6367 s
->zstream
.avail_in
= len
;
6368 s
->zstream
.next_in
= (uint8_t *)buf
;
6369 while (s
->zstream
.avail_in
> 0) {
6370 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
6373 if (s
->zstream
.avail_out
== 0) {
6374 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
6375 s
->zstream
.avail_out
= IOBUF_SIZE
;
6376 s
->zstream
.next_out
= s
->buf
;
6382 static void ram_compress_close(RamCompressState
*s
)
6386 /* compress last bytes */
6388 ret
= deflate(&s
->zstream
, Z_FINISH
);
6389 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
6390 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
6392 ram_put_cblock(s
, s
->buf
, len
);
6394 s
->zstream
.avail_out
= IOBUF_SIZE
;
6395 s
->zstream
.next_out
= s
->buf
;
6396 if (ret
== Z_STREAM_END
)
6403 deflateEnd(&s
->zstream
);
6406 typedef struct RamDecompressState
{
6409 uint8_t buf
[IOBUF_SIZE
];
6410 } RamDecompressState
;
6412 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
6415 memset(s
, 0, sizeof(*s
));
6417 ret
= inflateInit(&s
->zstream
);
6423 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
6427 s
->zstream
.avail_out
= len
;
6428 s
->zstream
.next_out
= buf
;
6429 while (s
->zstream
.avail_out
> 0) {
6430 if (s
->zstream
.avail_in
== 0) {
6431 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
6433 clen
= qemu_get_be16(s
->f
);
6434 if (clen
> IOBUF_SIZE
)
6436 qemu_get_buffer(s
->f
, s
->buf
, clen
);
6437 s
->zstream
.avail_in
= clen
;
6438 s
->zstream
.next_in
= s
->buf
;
6440 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
6441 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
6448 static void ram_decompress_close(RamDecompressState
*s
)
6450 inflateEnd(&s
->zstream
);
6453 static void ram_save(QEMUFile
*f
, void *opaque
)
6456 RamCompressState s1
, *s
= &s1
;
6459 qemu_put_be32(f
, phys_ram_size
);
6460 if (ram_compress_open(s
, f
) < 0)
6462 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6464 if (tight_savevm_enabled
) {
6468 /* find if the memory block is available on a virtual
6471 for(j
= 0; j
< MAX_DISKS
; j
++) {
6473 sector_num
= bdrv_hash_find(bs_table
[j
],
6474 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6475 if (sector_num
>= 0)
6480 goto normal_compress
;
6483 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
6484 ram_compress_buf(s
, buf
, 10);
6490 ram_compress_buf(s
, buf
, 1);
6491 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6494 ram_compress_close(s
);
6497 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
6499 RamDecompressState s1
, *s
= &s1
;
6503 if (version_id
== 1)
6504 return ram_load_v1(f
, opaque
);
6505 if (version_id
!= 2)
6507 if (qemu_get_be32(f
) != phys_ram_size
)
6509 if (ram_decompress_open(s
, f
) < 0)
6511 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6512 if (ram_decompress_buf(s
, buf
, 1) < 0) {
6513 fprintf(stderr
, "Error while reading ram block header\n");
6517 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
6518 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
6527 ram_decompress_buf(s
, buf
+ 1, 9);
6529 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
6530 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
6531 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
6534 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
6535 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
6536 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
6537 bs_index
, sector_num
);
6544 printf("Error block header\n");
6548 ram_decompress_close(s
);
6552 /***********************************************************/
6553 /* bottom halves (can be seen as timers which expire ASAP) */
6562 static QEMUBH
*first_bh
= NULL
;
6564 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
6567 bh
= qemu_mallocz(sizeof(QEMUBH
));
6571 bh
->opaque
= opaque
;
6575 int qemu_bh_poll(void)
6594 void qemu_bh_schedule(QEMUBH
*bh
)
6596 CPUState
*env
= cpu_single_env
;
6600 bh
->next
= first_bh
;
6603 /* stop the currently executing CPU to execute the BH ASAP */
6605 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
6609 void qemu_bh_cancel(QEMUBH
*bh
)
6612 if (bh
->scheduled
) {
6615 pbh
= &(*pbh
)->next
;
6621 void qemu_bh_delete(QEMUBH
*bh
)
6627 /***********************************************************/
6628 /* machine registration */
6630 QEMUMachine
*first_machine
= NULL
;
6632 int qemu_register_machine(QEMUMachine
*m
)
6635 pm
= &first_machine
;
6643 QEMUMachine
*find_machine(const char *name
)
6647 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6648 if (!strcmp(m
->name
, name
))
6654 /***********************************************************/
6655 /* main execution loop */
6657 void gui_update(void *opaque
)
6659 DisplayState
*ds
= opaque
;
6660 ds
->dpy_refresh(ds
);
6661 qemu_mod_timer(ds
->gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
6664 struct vm_change_state_entry
{
6665 VMChangeStateHandler
*cb
;
6667 LIST_ENTRY (vm_change_state_entry
) entries
;
6670 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
6672 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
6675 VMChangeStateEntry
*e
;
6677 e
= qemu_mallocz(sizeof (*e
));
6683 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
6687 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
6689 LIST_REMOVE (e
, entries
);
6693 static void vm_state_notify(int running
)
6695 VMChangeStateEntry
*e
;
6697 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
6698 e
->cb(e
->opaque
, running
);
6702 /* XXX: support several handlers */
6703 static VMStopHandler
*vm_stop_cb
;
6704 static void *vm_stop_opaque
;
6706 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6709 vm_stop_opaque
= opaque
;
6713 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6724 qemu_rearm_alarm_timer(alarm_timer
);
6728 void vm_stop(int reason
)
6731 cpu_disable_ticks();
6735 vm_stop_cb(vm_stop_opaque
, reason
);
6742 /* reset/shutdown handler */
6744 typedef struct QEMUResetEntry
{
6745 QEMUResetHandler
*func
;
6747 struct QEMUResetEntry
*next
;
6750 static QEMUResetEntry
*first_reset_entry
;
6751 static int reset_requested
;
6752 static int shutdown_requested
;
6753 static int powerdown_requested
;
6755 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
6757 QEMUResetEntry
**pre
, *re
;
6759 pre
= &first_reset_entry
;
6760 while (*pre
!= NULL
)
6761 pre
= &(*pre
)->next
;
6762 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
6764 re
->opaque
= opaque
;
6769 static void qemu_system_reset(void)
6773 /* reset all devices */
6774 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
6775 re
->func(re
->opaque
);
6779 void qemu_system_reset_request(void)
6782 shutdown_requested
= 1;
6784 reset_requested
= 1;
6787 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6790 void qemu_system_shutdown_request(void)
6792 shutdown_requested
= 1;
6794 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6797 void qemu_system_powerdown_request(void)
6799 powerdown_requested
= 1;
6801 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6804 void main_loop_wait(int timeout
)
6806 IOHandlerRecord
*ioh
;
6807 fd_set rfds
, wfds
, xfds
;
6816 /* XXX: need to suppress polling by better using win32 events */
6818 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
6819 ret
|= pe
->func(pe
->opaque
);
6824 WaitObjects
*w
= &wait_objects
;
6826 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
6827 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
6828 if (w
->func
[ret
- WAIT_OBJECT_0
])
6829 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
6831 /* Check for additional signaled events */
6832 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
6834 /* Check if event is signaled */
6835 ret2
= WaitForSingleObject(w
->events
[i
], 0);
6836 if(ret2
== WAIT_OBJECT_0
) {
6838 w
->func
[i
](w
->opaque
[i
]);
6839 } else if (ret2
== WAIT_TIMEOUT
) {
6841 err
= GetLastError();
6842 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
6845 } else if (ret
== WAIT_TIMEOUT
) {
6847 err
= GetLastError();
6848 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
6852 /* poll any events */
6853 /* XXX: separate device handlers from system ones */
6858 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6862 (!ioh
->fd_read_poll
||
6863 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
6864 FD_SET(ioh
->fd
, &rfds
);
6868 if (ioh
->fd_write
) {
6869 FD_SET(ioh
->fd
, &wfds
);
6879 tv
.tv_usec
= timeout
* 1000;
6881 #if defined(CONFIG_SLIRP)
6883 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
6886 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
6888 IOHandlerRecord
**pioh
;
6890 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6891 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
6892 ioh
->fd_read(ioh
->opaque
);
6894 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
6895 ioh
->fd_write(ioh
->opaque
);
6899 /* remove deleted IO handlers */
6900 pioh
= &first_io_handler
;
6910 #if defined(CONFIG_SLIRP)
6917 slirp_select_poll(&rfds
, &wfds
, &xfds
);
6923 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
6924 qemu_get_clock(vm_clock
));
6925 /* run dma transfers, if any */
6929 /* real time timers */
6930 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
6931 qemu_get_clock(rt_clock
));
6933 /* Check bottom-halves last in case any of the earlier events triggered
6939 static CPUState
*cur_cpu
;
6944 #ifdef CONFIG_PROFILER
6949 cur_cpu
= first_cpu
;
6956 env
= env
->next_cpu
;
6959 #ifdef CONFIG_PROFILER
6960 ti
= profile_getclock();
6962 ret
= cpu_exec(env
);
6963 #ifdef CONFIG_PROFILER
6964 qemu_time
+= profile_getclock() - ti
;
6966 if (ret
== EXCP_HLT
) {
6967 /* Give the next CPU a chance to run. */
6971 if (ret
!= EXCP_HALTED
)
6973 /* all CPUs are halted ? */
6979 if (shutdown_requested
) {
6980 ret
= EXCP_INTERRUPT
;
6983 if (reset_requested
) {
6984 reset_requested
= 0;
6985 qemu_system_reset();
6986 ret
= EXCP_INTERRUPT
;
6988 if (powerdown_requested
) {
6989 powerdown_requested
= 0;
6990 qemu_system_powerdown();
6991 ret
= EXCP_INTERRUPT
;
6993 if (ret
== EXCP_DEBUG
) {
6994 vm_stop(EXCP_DEBUG
);
6996 /* If all cpus are halted then wait until the next IRQ */
6997 /* XXX: use timeout computed from timers */
6998 if (ret
== EXCP_HALTED
)
7005 #ifdef CONFIG_PROFILER
7006 ti
= profile_getclock();
7008 main_loop_wait(timeout
);
7009 #ifdef CONFIG_PROFILER
7010 dev_time
+= profile_getclock() - ti
;
7013 cpu_disable_ticks();
7017 static void help(int exitcode
)
7019 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2007 Fabrice Bellard\n"
7020 "usage: %s [options] [disk_image]\n"
7022 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
7024 "Standard options:\n"
7025 "-M machine select emulated machine (-M ? for list)\n"
7026 "-cpu cpu select CPU (-cpu ? for list)\n"
7027 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
7028 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
7029 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
7030 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
7031 "-mtdblock file use 'file' as on-board Flash memory image\n"
7032 "-sd file use 'file' as SecureDigital card image\n"
7033 "-pflash file use 'file' as a parallel flash image\n"
7034 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
7035 "-snapshot write to temporary files instead of disk image files\n"
7037 "-no-frame open SDL window without a frame and window decorations\n"
7038 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
7039 "-no-quit disable SDL window close capability\n"
7042 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
7044 "-m megs set virtual RAM size to megs MB [default=%d]\n"
7045 "-smp n set the number of CPUs to 'n' [default=1]\n"
7046 "-nographic disable graphical output and redirect serial I/Os to console\n"
7047 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
7049 "-k language use keyboard layout (for example \"fr\" for French)\n"
7052 "-audio-help print list of audio drivers and their options\n"
7053 "-soundhw c1,... enable audio support\n"
7054 " and only specified sound cards (comma separated list)\n"
7055 " use -soundhw ? to get the list of supported cards\n"
7056 " use -soundhw all to enable all of them\n"
7058 "-localtime set the real time clock to local time [default=utc]\n"
7059 "-full-screen start in full screen\n"
7061 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
7063 "-usb enable the USB driver (will be the default soon)\n"
7064 "-usbdevice name add the host or guest USB device 'name'\n"
7065 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7066 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
7068 "-name string set the name of the guest\n"
7070 "Network options:\n"
7071 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
7072 " create a new Network Interface Card and connect it to VLAN 'n'\n"
7074 "-net user[,vlan=n][,hostname=host]\n"
7075 " connect the user mode network stack to VLAN 'n' and send\n"
7076 " hostname 'host' to DHCP clients\n"
7079 "-net tap[,vlan=n],ifname=name\n"
7080 " connect the host TAP network interface to VLAN 'n'\n"
7082 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
7083 " connect the host TAP network interface to VLAN 'n' and use the\n"
7084 " network scripts 'file' (default=%s)\n"
7085 " and 'dfile' (default=%s);\n"
7086 " use '[down]script=no' to disable script execution;\n"
7087 " use 'fd=h' to connect to an already opened TAP interface\n"
7089 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
7090 " connect the vlan 'n' to another VLAN using a socket connection\n"
7091 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
7092 " connect the vlan 'n' to multicast maddr and port\n"
7093 "-net none use it alone to have zero network devices; if no -net option\n"
7094 " is provided, the default is '-net nic -net user'\n"
7097 "-tftp dir allow tftp access to files in dir [-net user]\n"
7098 "-bootp file advertise file in BOOTP replies\n"
7100 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
7102 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
7103 " redirect TCP or UDP connections from host to guest [-net user]\n"
7106 "Linux boot specific:\n"
7107 "-kernel bzImage use 'bzImage' as kernel image\n"
7108 "-append cmdline use 'cmdline' as kernel command line\n"
7109 "-initrd file use 'file' as initial ram disk\n"
7111 "Debug/Expert options:\n"
7112 "-monitor dev redirect the monitor to char device 'dev'\n"
7113 "-serial dev redirect the serial port to char device 'dev'\n"
7114 "-parallel dev redirect the parallel port to char device 'dev'\n"
7115 "-pidfile file Write PID to 'file'\n"
7116 "-S freeze CPU at startup (use 'c' to start execution)\n"
7117 "-s wait gdb connection to port\n"
7118 "-p port set gdb connection port [default=%s]\n"
7119 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
7120 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
7121 " translation (t=none or lba) (usually qemu can guess them)\n"
7122 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
7124 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
7125 "-no-kqemu disable KQEMU kernel module usage\n"
7128 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
7129 " (default is CL-GD5446 PCI VGA)\n"
7130 "-no-acpi disable ACPI\n"
7132 "-no-reboot exit instead of rebooting\n"
7133 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
7134 "-vnc display start a VNC server on display\n"
7136 "-daemonize daemonize QEMU after initializing\n"
7138 "-option-rom rom load a file, rom, into the option ROM space\n"
7140 "-prom-env variable=value set OpenBIOS nvram variables\n"
7142 "-clock force the use of the given methods for timer alarm.\n"
7143 " To see what timers are available use -clock help\n"
7145 "During emulation, the following keys are useful:\n"
7146 "ctrl-alt-f toggle full screen\n"
7147 "ctrl-alt-n switch to virtual console 'n'\n"
7148 "ctrl-alt toggle mouse and keyboard grab\n"
7150 "When using -nographic, press 'ctrl-a h' to get some help.\n"
7155 DEFAULT_NETWORK_SCRIPT
,
7156 DEFAULT_NETWORK_DOWN_SCRIPT
,
7158 DEFAULT_GDBSTUB_PORT
,
7163 #define HAS_ARG 0x0001
7177 QEMU_OPTION_mtdblock
,
7181 QEMU_OPTION_snapshot
,
7183 QEMU_OPTION_no_fd_bootchk
,
7186 QEMU_OPTION_nographic
,
7187 QEMU_OPTION_portrait
,
7189 QEMU_OPTION_audio_help
,
7190 QEMU_OPTION_soundhw
,
7210 QEMU_OPTION_no_code_copy
,
7212 QEMU_OPTION_localtime
,
7213 QEMU_OPTION_cirrusvga
,
7216 QEMU_OPTION_std_vga
,
7218 QEMU_OPTION_monitor
,
7220 QEMU_OPTION_parallel
,
7222 QEMU_OPTION_full_screen
,
7223 QEMU_OPTION_no_frame
,
7224 QEMU_OPTION_alt_grab
,
7225 QEMU_OPTION_no_quit
,
7226 QEMU_OPTION_pidfile
,
7227 QEMU_OPTION_no_kqemu
,
7228 QEMU_OPTION_kernel_kqemu
,
7229 QEMU_OPTION_win2k_hack
,
7231 QEMU_OPTION_usbdevice
,
7234 QEMU_OPTION_no_acpi
,
7235 QEMU_OPTION_no_reboot
,
7236 QEMU_OPTION_show_cursor
,
7237 QEMU_OPTION_daemonize
,
7238 QEMU_OPTION_option_rom
,
7239 QEMU_OPTION_semihosting
,
7241 QEMU_OPTION_prom_env
,
7242 QEMU_OPTION_old_param
,
7244 QEMU_OPTION_startdate
,
7247 typedef struct QEMUOption
{
7253 const QEMUOption qemu_options
[] = {
7254 { "h", 0, QEMU_OPTION_h
},
7255 { "help", 0, QEMU_OPTION_h
},
7257 { "M", HAS_ARG
, QEMU_OPTION_M
},
7258 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
7259 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
7260 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
7261 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
7262 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
7263 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
7264 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
7265 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
7266 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
7267 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
7268 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
7269 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
7270 { "snapshot", 0, QEMU_OPTION_snapshot
},
7272 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
7274 { "m", HAS_ARG
, QEMU_OPTION_m
},
7275 { "nographic", 0, QEMU_OPTION_nographic
},
7276 { "portrait", 0, QEMU_OPTION_portrait
},
7277 { "k", HAS_ARG
, QEMU_OPTION_k
},
7279 { "audio-help", 0, QEMU_OPTION_audio_help
},
7280 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
7283 { "net", HAS_ARG
, QEMU_OPTION_net
},
7285 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
7286 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
7288 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
7290 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
7293 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
7294 { "append", HAS_ARG
, QEMU_OPTION_append
},
7295 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
7297 { "S", 0, QEMU_OPTION_S
},
7298 { "s", 0, QEMU_OPTION_s
},
7299 { "p", HAS_ARG
, QEMU_OPTION_p
},
7300 { "d", HAS_ARG
, QEMU_OPTION_d
},
7301 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
7302 { "L", HAS_ARG
, QEMU_OPTION_L
},
7303 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
7304 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
7306 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
7307 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
7309 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7310 { "g", 1, QEMU_OPTION_g
},
7312 { "localtime", 0, QEMU_OPTION_localtime
},
7313 { "std-vga", 0, QEMU_OPTION_std_vga
},
7314 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
7315 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
7316 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
7317 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
7318 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
7319 { "full-screen", 0, QEMU_OPTION_full_screen
},
7321 { "no-frame", 0, QEMU_OPTION_no_frame
},
7322 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
7323 { "no-quit", 0, QEMU_OPTION_no_quit
},
7325 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
7326 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
7327 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
7328 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
7329 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
7331 /* temporary options */
7332 { "usb", 0, QEMU_OPTION_usb
},
7333 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
7334 { "vmwarevga", 0, QEMU_OPTION_vmsvga
},
7335 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
7336 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
7337 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
7338 { "daemonize", 0, QEMU_OPTION_daemonize
},
7339 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
7340 #if defined(TARGET_ARM) || defined(TARGET_M68K)
7341 { "semihosting", 0, QEMU_OPTION_semihosting
},
7343 { "name", HAS_ARG
, QEMU_OPTION_name
},
7344 #if defined(TARGET_SPARC)
7345 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
7347 #if defined(TARGET_ARM)
7348 { "old-param", 0, QEMU_OPTION_old_param
},
7350 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
7351 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
7355 /* password input */
7357 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
7362 if (!bdrv_is_encrypted(bs
))
7365 term_printf("%s is encrypted.\n", name
);
7366 for(i
= 0; i
< 3; i
++) {
7367 monitor_readline("Password: ", 1, password
, sizeof(password
));
7368 if (bdrv_set_key(bs
, password
) == 0)
7370 term_printf("invalid password\n");
7375 static BlockDriverState
*get_bdrv(int index
)
7377 BlockDriverState
*bs
;
7380 bs
= bs_table
[index
];
7381 } else if (index
< 6) {
7382 bs
= fd_table
[index
- 4];
7389 static void read_passwords(void)
7391 BlockDriverState
*bs
;
7394 for(i
= 0; i
< 6; i
++) {
7397 qemu_key_check(bs
, bdrv_get_device_name(bs
));
7401 /* XXX: currently we cannot use simultaneously different CPUs */
7402 void register_machines(void)
7404 #if defined(TARGET_I386)
7405 qemu_register_machine(&pc_machine
);
7406 qemu_register_machine(&isapc_machine
);
7407 #elif defined(TARGET_PPC)
7408 qemu_register_machine(&heathrow_machine
);
7409 qemu_register_machine(&core99_machine
);
7410 qemu_register_machine(&prep_machine
);
7411 qemu_register_machine(&ref405ep_machine
);
7412 qemu_register_machine(&taihu_machine
);
7413 #elif defined(TARGET_MIPS)
7414 qemu_register_machine(&mips_machine
);
7415 qemu_register_machine(&mips_malta_machine
);
7416 qemu_register_machine(&mips_pica61_machine
);
7417 qemu_register_machine(&mips_mipssim_machine
);
7418 #elif defined(TARGET_SPARC)
7419 #ifdef TARGET_SPARC64
7420 qemu_register_machine(&sun4u_machine
);
7422 qemu_register_machine(&ss5_machine
);
7423 qemu_register_machine(&ss10_machine
);
7424 qemu_register_machine(&ss600mp_machine
);
7426 #elif defined(TARGET_ARM)
7427 qemu_register_machine(&integratorcp_machine
);
7428 qemu_register_machine(&versatilepb_machine
);
7429 qemu_register_machine(&versatileab_machine
);
7430 qemu_register_machine(&realview_machine
);
7431 qemu_register_machine(&akitapda_machine
);
7432 qemu_register_machine(&spitzpda_machine
);
7433 qemu_register_machine(&borzoipda_machine
);
7434 qemu_register_machine(&terrierpda_machine
);
7435 qemu_register_machine(&palmte_machine
);
7436 qemu_register_machine(&lm3s811evb_machine
);
7437 qemu_register_machine(&lm3s6965evb_machine
);
7438 #elif defined(TARGET_SH4)
7439 qemu_register_machine(&shix_machine
);
7440 qemu_register_machine(&r2d_machine
);
7441 #elif defined(TARGET_ALPHA)
7443 #elif defined(TARGET_M68K)
7444 qemu_register_machine(&mcf5208evb_machine
);
7445 qemu_register_machine(&an5206_machine
);
7446 qemu_register_machine(&dummy_m68k_machine
);
7447 #elif defined(TARGET_CRIS)
7448 qemu_register_machine(&bareetraxfs_machine
);
7450 #error unsupported CPU
7455 struct soundhw soundhw
[] = {
7456 #ifdef HAS_AUDIO_CHOICE
7463 { .init_isa
= pcspk_audio_init
}
7468 "Creative Sound Blaster 16",
7471 { .init_isa
= SB16_init
}
7478 "Yamaha YMF262 (OPL3)",
7480 "Yamaha YM3812 (OPL2)",
7484 { .init_isa
= Adlib_init
}
7491 "Gravis Ultrasound GF1",
7494 { .init_isa
= GUS_init
}
7500 "ENSONIQ AudioPCI ES1370",
7503 { .init_pci
= es1370_init
}
7507 { NULL
, NULL
, 0, 0, { NULL
} }
7510 static void select_soundhw (const char *optarg
)
7514 if (*optarg
== '?') {
7517 printf ("Valid sound card names (comma separated):\n");
7518 for (c
= soundhw
; c
->name
; ++c
) {
7519 printf ("%-11s %s\n", c
->name
, c
->descr
);
7521 printf ("\n-soundhw all will enable all of the above\n");
7522 exit (*optarg
!= '?');
7530 if (!strcmp (optarg
, "all")) {
7531 for (c
= soundhw
; c
->name
; ++c
) {
7539 e
= strchr (p
, ',');
7540 l
= !e
? strlen (p
) : (size_t) (e
- p
);
7542 for (c
= soundhw
; c
->name
; ++c
) {
7543 if (!strncmp (c
->name
, p
, l
)) {
7552 "Unknown sound card name (too big to show)\n");
7555 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
7560 p
+= l
+ (e
!= NULL
);
7564 goto show_valid_cards
;
7570 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
7572 exit(STATUS_CONTROL_C_EXIT
);
7577 #define MAX_NET_CLIENTS 32
7579 int main(int argc
, char **argv
)
7581 #ifdef CONFIG_GDBSTUB
7583 const char *gdbstub_port
;
7585 uint32_t boot_devices_bitmap
= 0;
7586 int i
, cdrom_index
, pflash_index
;
7587 int snapshot
, linux_boot
, net_boot
;
7588 const char *initrd_filename
;
7589 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
7590 const char *pflash_filename
[MAX_PFLASH
];
7591 const char *sd_filename
;
7592 const char *mtd_filename
;
7593 const char *kernel_filename
, *kernel_cmdline
;
7594 const char *boot_devices
= "";
7595 DisplayState
*ds
= &display_state
;
7596 int cyls
, heads
, secs
, translation
;
7597 char net_clients
[MAX_NET_CLIENTS
][256];
7600 const char *r
, *optarg
;
7601 CharDriverState
*monitor_hd
;
7602 char monitor_device
[128];
7603 char serial_devices
[MAX_SERIAL_PORTS
][128];
7604 int serial_device_index
;
7605 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
7606 int parallel_device_index
;
7607 const char *loadvm
= NULL
;
7608 QEMUMachine
*machine
;
7609 const char *cpu_model
;
7610 char usb_devices
[MAX_USB_CMDLINE
][128];
7611 int usb_devices_index
;
7613 const char *pid_file
= NULL
;
7616 LIST_INIT (&vm_change_state_head
);
7619 struct sigaction act
;
7620 sigfillset(&act
.sa_mask
);
7622 act
.sa_handler
= SIG_IGN
;
7623 sigaction(SIGPIPE
, &act
, NULL
);
7626 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
7627 /* Note: cpu_interrupt() is currently not SMP safe, so we force
7628 QEMU to run on a single CPU */
7633 h
= GetCurrentProcess();
7634 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
7635 for(i
= 0; i
< 32; i
++) {
7636 if (mask
& (1 << i
))
7641 SetProcessAffinityMask(h
, mask
);
7647 register_machines();
7648 machine
= first_machine
;
7650 initrd_filename
= NULL
;
7651 for(i
= 0; i
< MAX_FD
; i
++)
7652 fd_filename
[i
] = NULL
;
7653 for(i
= 0; i
< MAX_DISKS
; i
++)
7654 hd_filename
[i
] = NULL
;
7655 for(i
= 0; i
< MAX_PFLASH
; i
++)
7656 pflash_filename
[i
] = NULL
;
7659 mtd_filename
= NULL
;
7660 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
7661 vga_ram_size
= VGA_RAM_SIZE
;
7662 #ifdef CONFIG_GDBSTUB
7664 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
7668 kernel_filename
= NULL
;
7669 kernel_cmdline
= "";
7675 cyls
= heads
= secs
= 0;
7676 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7677 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
7679 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
7680 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
7681 serial_devices
[i
][0] = '\0';
7682 serial_device_index
= 0;
7684 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
7685 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
7686 parallel_devices
[i
][0] = '\0';
7687 parallel_device_index
= 0;
7689 usb_devices_index
= 0;
7694 /* default mac address of the first network interface */
7702 hd_filename
[0] = argv
[optind
++];
7704 const QEMUOption
*popt
;
7707 /* Treat --foo the same as -foo. */
7710 popt
= qemu_options
;
7713 fprintf(stderr
, "%s: invalid option -- '%s'\n",
7717 if (!strcmp(popt
->name
, r
+ 1))
7721 if (popt
->flags
& HAS_ARG
) {
7722 if (optind
>= argc
) {
7723 fprintf(stderr
, "%s: option '%s' requires an argument\n",
7727 optarg
= argv
[optind
++];
7732 switch(popt
->index
) {
7734 machine
= find_machine(optarg
);
7737 printf("Supported machines are:\n");
7738 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
7739 printf("%-10s %s%s\n",
7741 m
== first_machine
? " (default)" : "");
7743 exit(*optarg
!= '?');
7746 case QEMU_OPTION_cpu
:
7747 /* hw initialization will check this */
7748 if (*optarg
== '?') {
7749 /* XXX: implement xxx_cpu_list for targets that still miss it */
7750 #if defined(cpu_list)
7751 cpu_list(stdout
, &fprintf
);
7758 case QEMU_OPTION_initrd
:
7759 initrd_filename
= optarg
;
7761 case QEMU_OPTION_hda
:
7762 case QEMU_OPTION_hdb
:
7763 case QEMU_OPTION_hdc
:
7764 case QEMU_OPTION_hdd
:
7767 hd_index
= popt
->index
- QEMU_OPTION_hda
;
7768 hd_filename
[hd_index
] = optarg
;
7769 if (hd_index
== cdrom_index
)
7773 case QEMU_OPTION_mtdblock
:
7774 mtd_filename
= optarg
;
7776 case QEMU_OPTION_sd
:
7777 sd_filename
= optarg
;
7779 case QEMU_OPTION_pflash
:
7780 if (pflash_index
>= MAX_PFLASH
) {
7781 fprintf(stderr
, "qemu: too many parallel flash images\n");
7784 pflash_filename
[pflash_index
++] = optarg
;
7786 case QEMU_OPTION_snapshot
:
7789 case QEMU_OPTION_hdachs
:
7793 cyls
= strtol(p
, (char **)&p
, 0);
7794 if (cyls
< 1 || cyls
> 16383)
7799 heads
= strtol(p
, (char **)&p
, 0);
7800 if (heads
< 1 || heads
> 16)
7805 secs
= strtol(p
, (char **)&p
, 0);
7806 if (secs
< 1 || secs
> 63)
7810 if (!strcmp(p
, "none"))
7811 translation
= BIOS_ATA_TRANSLATION_NONE
;
7812 else if (!strcmp(p
, "lba"))
7813 translation
= BIOS_ATA_TRANSLATION_LBA
;
7814 else if (!strcmp(p
, "auto"))
7815 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7818 } else if (*p
!= '\0') {
7820 fprintf(stderr
, "qemu: invalid physical CHS format\n");
7825 case QEMU_OPTION_nographic
:
7826 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
7827 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "null");
7828 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
7831 case QEMU_OPTION_portrait
:
7834 case QEMU_OPTION_kernel
:
7835 kernel_filename
= optarg
;
7837 case QEMU_OPTION_append
:
7838 kernel_cmdline
= optarg
;
7840 case QEMU_OPTION_cdrom
:
7841 if (cdrom_index
>= 0) {
7842 hd_filename
[cdrom_index
] = optarg
;
7845 case QEMU_OPTION_boot
:
7846 boot_devices
= optarg
;
7847 /* We just do some generic consistency checks */
7849 /* Could easily be extended to 64 devices if needed */
7850 const unsigned char *p
;
7852 boot_devices_bitmap
= 0;
7853 for (p
= boot_devices
; *p
!= '\0'; p
++) {
7854 /* Allowed boot devices are:
7855 * a b : floppy disk drives
7856 * c ... f : IDE disk drives
7857 * g ... m : machine implementation dependant drives
7858 * n ... p : network devices
7859 * It's up to each machine implementation to check
7860 * if the given boot devices match the actual hardware
7861 * implementation and firmware features.
7863 if (*p
< 'a' || *p
> 'q') {
7864 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
7867 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
7869 "Boot device '%c' was given twice\n",*p
);
7872 boot_devices_bitmap
|= 1 << (*p
- 'a');
7876 case QEMU_OPTION_fda
:
7877 fd_filename
[0] = optarg
;
7879 case QEMU_OPTION_fdb
:
7880 fd_filename
[1] = optarg
;
7883 case QEMU_OPTION_no_fd_bootchk
:
7887 case QEMU_OPTION_no_code_copy
:
7888 code_copy_enabled
= 0;
7890 case QEMU_OPTION_net
:
7891 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
7892 fprintf(stderr
, "qemu: too many network clients\n");
7895 pstrcpy(net_clients
[nb_net_clients
],
7896 sizeof(net_clients
[0]),
7901 case QEMU_OPTION_tftp
:
7902 tftp_prefix
= optarg
;
7904 case QEMU_OPTION_bootp
:
7905 bootp_filename
= optarg
;
7908 case QEMU_OPTION_smb
:
7909 net_slirp_smb(optarg
);
7912 case QEMU_OPTION_redir
:
7913 net_slirp_redir(optarg
);
7917 case QEMU_OPTION_audio_help
:
7921 case QEMU_OPTION_soundhw
:
7922 select_soundhw (optarg
);
7929 ram_size
= atoi(optarg
) * 1024 * 1024;
7932 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
7933 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
7934 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
7943 mask
= cpu_str_to_log_mask(optarg
);
7945 printf("Log items (comma separated):\n");
7946 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
7947 printf("%-10s %s\n", item
->name
, item
->help
);
7954 #ifdef CONFIG_GDBSTUB
7959 gdbstub_port
= optarg
;
7965 case QEMU_OPTION_bios
:
7972 keyboard_layout
= optarg
;
7974 case QEMU_OPTION_localtime
:
7977 case QEMU_OPTION_cirrusvga
:
7978 cirrus_vga_enabled
= 1;
7981 case QEMU_OPTION_vmsvga
:
7982 cirrus_vga_enabled
= 0;
7985 case QEMU_OPTION_std_vga
:
7986 cirrus_vga_enabled
= 0;
7994 w
= strtol(p
, (char **)&p
, 10);
7997 fprintf(stderr
, "qemu: invalid resolution or depth\n");
8003 h
= strtol(p
, (char **)&p
, 10);
8008 depth
= strtol(p
, (char **)&p
, 10);
8009 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
8010 depth
!= 24 && depth
!= 32)
8012 } else if (*p
== '\0') {
8013 depth
= graphic_depth
;
8020 graphic_depth
= depth
;
8023 case QEMU_OPTION_echr
:
8026 term_escape_char
= strtol(optarg
, &r
, 0);
8028 printf("Bad argument to echr\n");
8031 case QEMU_OPTION_monitor
:
8032 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
8034 case QEMU_OPTION_serial
:
8035 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
8036 fprintf(stderr
, "qemu: too many serial ports\n");
8039 pstrcpy(serial_devices
[serial_device_index
],
8040 sizeof(serial_devices
[0]), optarg
);
8041 serial_device_index
++;
8043 case QEMU_OPTION_parallel
:
8044 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
8045 fprintf(stderr
, "qemu: too many parallel ports\n");
8048 pstrcpy(parallel_devices
[parallel_device_index
],
8049 sizeof(parallel_devices
[0]), optarg
);
8050 parallel_device_index
++;
8052 case QEMU_OPTION_loadvm
:
8055 case QEMU_OPTION_full_screen
:
8059 case QEMU_OPTION_no_frame
:
8062 case QEMU_OPTION_alt_grab
:
8065 case QEMU_OPTION_no_quit
:
8069 case QEMU_OPTION_pidfile
:
8073 case QEMU_OPTION_win2k_hack
:
8074 win2k_install_hack
= 1;
8078 case QEMU_OPTION_no_kqemu
:
8081 case QEMU_OPTION_kernel_kqemu
:
8085 case QEMU_OPTION_usb
:
8088 case QEMU_OPTION_usbdevice
:
8090 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
8091 fprintf(stderr
, "Too many USB devices\n");
8094 pstrcpy(usb_devices
[usb_devices_index
],
8095 sizeof(usb_devices
[usb_devices_index
]),
8097 usb_devices_index
++;
8099 case QEMU_OPTION_smp
:
8100 smp_cpus
= atoi(optarg
);
8101 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
8102 fprintf(stderr
, "Invalid number of CPUs\n");
8106 case QEMU_OPTION_vnc
:
8107 vnc_display
= optarg
;
8109 case QEMU_OPTION_no_acpi
:
8112 case QEMU_OPTION_no_reboot
:
8115 case QEMU_OPTION_show_cursor
:
8118 case QEMU_OPTION_daemonize
:
8121 case QEMU_OPTION_option_rom
:
8122 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
8123 fprintf(stderr
, "Too many option ROMs\n");
8126 option_rom
[nb_option_roms
] = optarg
;
8129 case QEMU_OPTION_semihosting
:
8130 semihosting_enabled
= 1;
8132 case QEMU_OPTION_name
:
8136 case QEMU_OPTION_prom_env
:
8137 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
8138 fprintf(stderr
, "Too many prom variables\n");
8141 prom_envs
[nb_prom_envs
] = optarg
;
8146 case QEMU_OPTION_old_param
:
8149 case QEMU_OPTION_clock
:
8150 configure_alarms(optarg
);
8152 case QEMU_OPTION_startdate
:
8155 if (!strcmp(optarg
, "now")) {
8156 rtc_start_date
= -1;
8158 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
8166 } else if (sscanf(optarg
, "%d-%d-%d",
8169 &tm
.tm_mday
) == 3) {
8178 rtc_start_date
= mktimegm(&tm
);
8179 if (rtc_start_date
== -1) {
8181 fprintf(stderr
, "Invalid date format. Valid format are:\n"
8182 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
8193 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
8194 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
8201 if (pipe(fds
) == -1)
8212 len
= read(fds
[0], &status
, 1);
8213 if (len
== -1 && (errno
== EINTR
))
8218 else if (status
== 1) {
8219 fprintf(stderr
, "Could not acquire pidfile\n");
8237 signal(SIGTSTP
, SIG_IGN
);
8238 signal(SIGTTOU
, SIG_IGN
);
8239 signal(SIGTTIN
, SIG_IGN
);
8243 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
8246 write(fds
[1], &status
, 1);
8248 fprintf(stderr
, "Could not acquire pid file\n");
8256 linux_boot
= (kernel_filename
!= NULL
);
8257 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) && 0xF;
8259 /* XXX: this should not be: some embedded targets just have flash */
8260 if (!linux_boot
&& net_boot
== 0 &&
8261 hd_filename
[0] == '\0' &&
8262 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
8263 fd_filename
[0] == '\0')
8266 /* boot to floppy or the default cd if no hard disk defined yet */
8267 if (!boot_devices
[0]) {
8268 if (hd_filename
[0] != '\0')
8270 else if (fd_filename
[0] != '\0')
8275 setvbuf(stdout
, NULL
, _IOLBF
, 0);
8285 /* init network clients */
8286 if (nb_net_clients
== 0) {
8287 /* if no clients, we use a default config */
8288 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
8290 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
8295 for(i
= 0;i
< nb_net_clients
; i
++) {
8296 if (net_client_init(net_clients
[i
]) < 0)
8299 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
8300 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
8302 if (vlan
->nb_guest_devs
== 0) {
8303 fprintf(stderr
, "Invalid vlan (%d) with no nics\n", vlan
->id
);
8306 if (vlan
->nb_host_devs
== 0)
8308 "Warning: vlan %d is not connected to host network\n",
8313 /* XXX: this should be moved in the PC machine instanciation code */
8314 if (net_boot
!= 0) {
8316 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
8317 const char *model
= nd_table
[i
].model
;
8319 if (net_boot
& (1 << i
)) {
8322 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
8323 if (get_image_size(buf
) > 0) {
8324 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
8325 fprintf(stderr
, "Too many option ROMs\n");
8328 option_rom
[nb_option_roms
] = strdup(buf
);
8335 fprintf(stderr
, "No valid PXE rom found for network device\n");
8341 /* init the memory */
8342 phys_ram_size
= ram_size
+ vga_ram_size
+ MAX_BIOS_SIZE
;
8344 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
8345 if (!phys_ram_base
) {
8346 fprintf(stderr
, "Could not allocate physical memory\n");
8350 /* we always create the cdrom drive, even if no disk is there */
8352 if (cdrom_index
>= 0) {
8353 bs_table
[cdrom_index
] = bdrv_new("cdrom");
8354 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
8357 /* open the virtual block devices */
8358 for(i
= 0; i
< MAX_DISKS
; i
++) {
8359 if (hd_filename
[i
]) {
8362 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
8363 bs_table
[i
] = bdrv_new(buf
);
8365 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8366 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
8370 if (i
== 0 && cyls
!= 0) {
8371 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
8372 bdrv_set_translation_hint(bs_table
[i
], translation
);
8377 /* we always create at least one floppy disk */
8378 fd_table
[0] = bdrv_new("fda");
8379 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
8381 for(i
= 0; i
< MAX_FD
; i
++) {
8382 if (fd_filename
[i
]) {
8385 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
8386 fd_table
[i
] = bdrv_new(buf
);
8387 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
8389 if (fd_filename
[i
][0] != '\0') {
8390 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
8391 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8392 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
8400 /* Open the virtual parallel flash block devices */
8401 for(i
= 0; i
< MAX_PFLASH
; i
++) {
8402 if (pflash_filename
[i
]) {
8403 if (!pflash_table
[i
]) {
8405 snprintf(buf
, sizeof(buf
), "fl%c", i
+ 'a');
8406 pflash_table
[i
] = bdrv_new(buf
);
8408 if (bdrv_open(pflash_table
[i
], pflash_filename
[i
],
8409 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8410 fprintf(stderr
, "qemu: could not open flash image '%s'\n",
8411 pflash_filename
[i
]);
8417 sd_bdrv
= bdrv_new ("sd");
8418 /* FIXME: This isn't really a floppy, but it's a reasonable
8420 bdrv_set_type_hint(sd_bdrv
, BDRV_TYPE_FLOPPY
);
8422 if (bdrv_open(sd_bdrv
, sd_filename
,
8423 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8424 fprintf(stderr
, "qemu: could not open SD card image %s\n",
8427 qemu_key_check(sd_bdrv
, sd_filename
);
8431 mtd_bdrv
= bdrv_new ("mtd");
8432 if (bdrv_open(mtd_bdrv
, mtd_filename
,
8433 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0 ||
8434 qemu_key_check(mtd_bdrv
, mtd_filename
)) {
8435 fprintf(stderr
, "qemu: could not open Flash image %s\n",
8437 bdrv_delete(mtd_bdrv
);
8442 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
8443 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
8448 memset(&display_state
, 0, sizeof(display_state
));
8450 /* nearly nothing to do */
8451 dumb_display_init(ds
);
8452 } else if (vnc_display
!= NULL
) {
8453 vnc_display_init(ds
);
8454 if (vnc_display_open(ds
, vnc_display
) < 0)
8457 #if defined(CONFIG_SDL)
8458 sdl_display_init(ds
, full_screen
, no_frame
);
8459 #elif defined(CONFIG_COCOA)
8460 cocoa_display_init(ds
, full_screen
);
8464 /* Maintain compatibility with multiple stdio monitors */
8465 if (!strcmp(monitor_device
,"stdio")) {
8466 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
8467 if (!strcmp(serial_devices
[i
],"mon:stdio")) {
8468 monitor_device
[0] = '\0';
8470 } else if (!strcmp(serial_devices
[i
],"stdio")) {
8471 monitor_device
[0] = '\0';
8472 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "mon:stdio");
8477 if (monitor_device
[0] != '\0') {
8478 monitor_hd
= qemu_chr_open(monitor_device
);
8480 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
8483 monitor_init(monitor_hd
, !nographic
);
8486 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
8487 const char *devname
= serial_devices
[i
];
8488 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
8489 serial_hds
[i
] = qemu_chr_open(devname
);
8490 if (!serial_hds
[i
]) {
8491 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
8495 if (strstart(devname
, "vc", 0))
8496 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
8500 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
8501 const char *devname
= parallel_devices
[i
];
8502 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
8503 parallel_hds
[i
] = qemu_chr_open(devname
);
8504 if (!parallel_hds
[i
]) {
8505 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
8509 if (strstart(devname
, "vc", 0))
8510 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
8514 machine
->init(ram_size
, vga_ram_size
, boot_devices
,
8515 ds
, fd_filename
, snapshot
,
8516 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
8518 /* init USB devices */
8520 for(i
= 0; i
< usb_devices_index
; i
++) {
8521 if (usb_device_add(usb_devices
[i
]) < 0) {
8522 fprintf(stderr
, "Warning: could not add USB device %s\n",
8528 if (display_state
.dpy_refresh
) {
8529 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
8530 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
8533 #ifdef CONFIG_GDBSTUB
8535 /* XXX: use standard host:port notation and modify options
8537 if (gdbserver_start(gdbstub_port
) < 0) {
8538 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
8549 /* XXX: simplify init */
8562 len
= write(fds
[1], &status
, 1);
8563 if (len
== -1 && (errno
== EINTR
))
8569 TFR(fd
= open("/dev/null", O_RDWR
));
8583 #if !defined(_WIN32)
8584 /* close network clients */
8585 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
8586 VLANClientState
*vc
;
8588 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
8589 if (vc
->fd_read
== tap_receive
) {
8591 TAPState
*s
= vc
->opaque
;
8593 if (sscanf(vc
->info_str
, "tap: ifname=%63s ", ifname
) == 1 &&
8595 launch_script(s
->down_script
, ifname
, s
->fd
);