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"
121 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
123 #define SMBD_COMMAND "/usr/sbin/smbd"
126 //#define DEBUG_UNUSED_IOPORT
127 //#define DEBUG_IOPORT
129 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
132 #define DEFAULT_RAM_SIZE 144
134 #define DEFAULT_RAM_SIZE 128
137 #define GUI_REFRESH_INTERVAL 30
139 /* Max number of USB devices that can be specified on the commandline. */
140 #define MAX_USB_CMDLINE 8
142 /* XXX: use a two level table to limit memory usage */
143 #define MAX_IOPORTS 65536
145 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
146 const char *bios_name
= NULL
;
147 char phys_ram_file
[1024];
148 void *ioport_opaque
[MAX_IOPORTS
];
149 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
150 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
151 /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available
152 to store the VM snapshots */
153 BlockDriverState
*bs_table
[MAX_DISKS
+ 1], *fd_table
[MAX_FD
];
154 BlockDriverState
*pflash_table
[MAX_PFLASH
];
155 BlockDriverState
*sd_bdrv
;
156 BlockDriverState
*mtd_bdrv
;
157 /* point to the block driver where the snapshots are managed */
158 BlockDriverState
*bs_snapshots
;
160 static DisplayState display_state
;
162 const char* keyboard_layout
= NULL
;
163 int64_t ticks_per_sec
;
164 int boot_device
= 'c';
166 int pit_min_timer_count
= 0;
168 NICInfo nd_table
[MAX_NICS
];
171 int cirrus_vga_enabled
= 1;
172 int vmsvga_enabled
= 0;
174 int graphic_width
= 1024;
175 int graphic_height
= 768;
176 int graphic_depth
= 8;
178 int graphic_width
= 800;
179 int graphic_height
= 600;
180 int graphic_depth
= 15;
185 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
186 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
188 int win2k_install_hack
= 0;
191 static VLANState
*first_vlan
;
193 const char *vnc_display
;
194 #if defined(TARGET_SPARC)
196 #elif defined(TARGET_I386)
201 int acpi_enabled
= 1;
205 int graphic_rotate
= 0;
207 const char *option_rom
[MAX_OPTION_ROMS
];
209 int semihosting_enabled
= 0;
214 const char *qemu_name
;
217 unsigned int nb_prom_envs
= 0;
218 const char *prom_envs
[MAX_PROM_ENVS
];
221 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
223 /***********************************************************/
224 /* x86 ISA bus support */
226 target_phys_addr_t isa_mem_base
= 0;
229 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
231 #ifdef DEBUG_UNUSED_IOPORT
232 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
237 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
239 #ifdef DEBUG_UNUSED_IOPORT
240 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
244 /* default is to make two byte accesses */
245 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
248 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
249 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
250 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
254 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
256 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
257 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
258 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
261 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
263 #ifdef DEBUG_UNUSED_IOPORT
264 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
269 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
271 #ifdef DEBUG_UNUSED_IOPORT
272 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
276 void init_ioports(void)
280 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
281 ioport_read_table
[0][i
] = default_ioport_readb
;
282 ioport_write_table
[0][i
] = default_ioport_writeb
;
283 ioport_read_table
[1][i
] = default_ioport_readw
;
284 ioport_write_table
[1][i
] = default_ioport_writew
;
285 ioport_read_table
[2][i
] = default_ioport_readl
;
286 ioport_write_table
[2][i
] = default_ioport_writel
;
290 /* size is the word size in byte */
291 int register_ioport_read(int start
, int length
, int size
,
292 IOPortReadFunc
*func
, void *opaque
)
298 } else if (size
== 2) {
300 } else if (size
== 4) {
303 hw_error("register_ioport_read: invalid size");
306 for(i
= start
; i
< start
+ length
; i
+= size
) {
307 ioport_read_table
[bsize
][i
] = func
;
308 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
309 hw_error("register_ioport_read: invalid opaque");
310 ioport_opaque
[i
] = opaque
;
315 /* size is the word size in byte */
316 int register_ioport_write(int start
, int length
, int size
,
317 IOPortWriteFunc
*func
, void *opaque
)
323 } else if (size
== 2) {
325 } else if (size
== 4) {
328 hw_error("register_ioport_write: invalid size");
331 for(i
= start
; i
< start
+ length
; i
+= size
) {
332 ioport_write_table
[bsize
][i
] = func
;
333 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
334 hw_error("register_ioport_write: invalid opaque");
335 ioport_opaque
[i
] = opaque
;
340 void isa_unassign_ioport(int start
, int length
)
344 for(i
= start
; i
< start
+ length
; i
++) {
345 ioport_read_table
[0][i
] = default_ioport_readb
;
346 ioport_read_table
[1][i
] = default_ioport_readw
;
347 ioport_read_table
[2][i
] = default_ioport_readl
;
349 ioport_write_table
[0][i
] = default_ioport_writeb
;
350 ioport_write_table
[1][i
] = default_ioport_writew
;
351 ioport_write_table
[2][i
] = default_ioport_writel
;
355 /***********************************************************/
357 void cpu_outb(CPUState
*env
, int addr
, int val
)
360 if (loglevel
& CPU_LOG_IOPORT
)
361 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
363 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
366 env
->last_io_time
= cpu_get_time_fast();
370 void cpu_outw(CPUState
*env
, int addr
, int val
)
373 if (loglevel
& CPU_LOG_IOPORT
)
374 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
376 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
379 env
->last_io_time
= cpu_get_time_fast();
383 void cpu_outl(CPUState
*env
, int addr
, int val
)
386 if (loglevel
& CPU_LOG_IOPORT
)
387 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
389 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
392 env
->last_io_time
= cpu_get_time_fast();
396 int cpu_inb(CPUState
*env
, int addr
)
399 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
401 if (loglevel
& CPU_LOG_IOPORT
)
402 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
406 env
->last_io_time
= cpu_get_time_fast();
411 int cpu_inw(CPUState
*env
, int addr
)
414 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
416 if (loglevel
& CPU_LOG_IOPORT
)
417 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
421 env
->last_io_time
= cpu_get_time_fast();
426 int cpu_inl(CPUState
*env
, int addr
)
429 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
431 if (loglevel
& CPU_LOG_IOPORT
)
432 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
436 env
->last_io_time
= cpu_get_time_fast();
441 /***********************************************************/
442 void hw_error(const char *fmt
, ...)
448 fprintf(stderr
, "qemu: hardware error: ");
449 vfprintf(stderr
, fmt
, ap
);
450 fprintf(stderr
, "\n");
451 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
452 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
454 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
456 cpu_dump_state(env
, stderr
, fprintf
, 0);
463 /***********************************************************/
466 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
467 static void *qemu_put_kbd_event_opaque
;
468 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
469 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
471 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
473 qemu_put_kbd_event_opaque
= opaque
;
474 qemu_put_kbd_event
= func
;
477 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
478 void *opaque
, int absolute
,
481 QEMUPutMouseEntry
*s
, *cursor
;
483 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
487 s
->qemu_put_mouse_event
= func
;
488 s
->qemu_put_mouse_event_opaque
= opaque
;
489 s
->qemu_put_mouse_event_absolute
= absolute
;
490 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
493 if (!qemu_put_mouse_event_head
) {
494 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
498 cursor
= qemu_put_mouse_event_head
;
499 while (cursor
->next
!= NULL
)
500 cursor
= cursor
->next
;
503 qemu_put_mouse_event_current
= s
;
508 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
510 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
512 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
515 cursor
= qemu_put_mouse_event_head
;
516 while (cursor
!= NULL
&& cursor
!= entry
) {
518 cursor
= cursor
->next
;
521 if (cursor
== NULL
) // does not exist or list empty
523 else if (prev
== NULL
) { // entry is head
524 qemu_put_mouse_event_head
= cursor
->next
;
525 if (qemu_put_mouse_event_current
== entry
)
526 qemu_put_mouse_event_current
= cursor
->next
;
527 qemu_free(entry
->qemu_put_mouse_event_name
);
532 prev
->next
= entry
->next
;
534 if (qemu_put_mouse_event_current
== entry
)
535 qemu_put_mouse_event_current
= prev
;
537 qemu_free(entry
->qemu_put_mouse_event_name
);
541 void kbd_put_keycode(int keycode
)
543 if (qemu_put_kbd_event
) {
544 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
548 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
550 QEMUPutMouseEvent
*mouse_event
;
551 void *mouse_event_opaque
;
554 if (!qemu_put_mouse_event_current
) {
559 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
561 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
564 if (graphic_rotate
) {
565 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
568 width
= graphic_width
;
569 mouse_event(mouse_event_opaque
,
570 width
- dy
, dx
, dz
, buttons_state
);
572 mouse_event(mouse_event_opaque
,
573 dx
, dy
, dz
, buttons_state
);
577 int kbd_mouse_is_absolute(void)
579 if (!qemu_put_mouse_event_current
)
582 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
585 void do_info_mice(void)
587 QEMUPutMouseEntry
*cursor
;
590 if (!qemu_put_mouse_event_head
) {
591 term_printf("No mouse devices connected\n");
595 term_printf("Mouse devices available:\n");
596 cursor
= qemu_put_mouse_event_head
;
597 while (cursor
!= NULL
) {
598 term_printf("%c Mouse #%d: %s\n",
599 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
600 index
, cursor
->qemu_put_mouse_event_name
);
602 cursor
= cursor
->next
;
606 void do_mouse_set(int index
)
608 QEMUPutMouseEntry
*cursor
;
611 if (!qemu_put_mouse_event_head
) {
612 term_printf("No mouse devices connected\n");
616 cursor
= qemu_put_mouse_event_head
;
617 while (cursor
!= NULL
&& index
!= i
) {
619 cursor
= cursor
->next
;
623 qemu_put_mouse_event_current
= cursor
;
625 term_printf("Mouse at given index not found\n");
628 /* compute with 96 bit intermediate result: (a*b)/c */
629 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
634 #ifdef WORDS_BIGENDIAN
644 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
645 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
648 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
652 /***********************************************************/
653 /* real time host monotonic timer */
655 #define QEMU_TIMER_BASE 1000000000LL
659 static int64_t clock_freq
;
661 static void init_get_clock(void)
665 ret
= QueryPerformanceFrequency(&freq
);
667 fprintf(stderr
, "Could not calibrate ticks\n");
670 clock_freq
= freq
.QuadPart
;
673 static int64_t get_clock(void)
676 QueryPerformanceCounter(&ti
);
677 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
682 static int use_rt_clock
;
684 static void init_get_clock(void)
687 #if defined(__linux__)
690 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
697 static int64_t get_clock(void)
699 #if defined(__linux__)
702 clock_gettime(CLOCK_MONOTONIC
, &ts
);
703 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
707 /* XXX: using gettimeofday leads to problems if the date
708 changes, so it should be avoided. */
710 gettimeofday(&tv
, NULL
);
711 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
717 /***********************************************************/
718 /* guest cycle counter */
720 static int64_t cpu_ticks_prev
;
721 static int64_t cpu_ticks_offset
;
722 static int64_t cpu_clock_offset
;
723 static int cpu_ticks_enabled
;
725 /* return the host CPU cycle counter and handle stop/restart */
726 int64_t cpu_get_ticks(void)
728 if (!cpu_ticks_enabled
) {
729 return cpu_ticks_offset
;
732 ticks
= cpu_get_real_ticks();
733 if (cpu_ticks_prev
> ticks
) {
734 /* Note: non increasing ticks may happen if the host uses
736 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
738 cpu_ticks_prev
= ticks
;
739 return ticks
+ cpu_ticks_offset
;
743 /* return the host CPU monotonic timer and handle stop/restart */
744 static int64_t cpu_get_clock(void)
747 if (!cpu_ticks_enabled
) {
748 return cpu_clock_offset
;
751 return ti
+ cpu_clock_offset
;
755 /* enable cpu_get_ticks() */
756 void cpu_enable_ticks(void)
758 if (!cpu_ticks_enabled
) {
759 cpu_ticks_offset
-= cpu_get_real_ticks();
760 cpu_clock_offset
-= get_clock();
761 cpu_ticks_enabled
= 1;
765 /* disable cpu_get_ticks() : the clock is stopped. You must not call
766 cpu_get_ticks() after that. */
767 void cpu_disable_ticks(void)
769 if (cpu_ticks_enabled
) {
770 cpu_ticks_offset
= cpu_get_ticks();
771 cpu_clock_offset
= cpu_get_clock();
772 cpu_ticks_enabled
= 0;
776 /***********************************************************/
779 #define QEMU_TIMER_REALTIME 0
780 #define QEMU_TIMER_VIRTUAL 1
784 /* XXX: add frequency */
792 struct QEMUTimer
*next
;
795 struct qemu_alarm_timer
{
799 int (*start
)(struct qemu_alarm_timer
*t
);
800 void (*stop
)(struct qemu_alarm_timer
*t
);
801 void (*rearm
)(struct qemu_alarm_timer
*t
);
805 #define ALARM_FLAG_DYNTICKS 0x1
807 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
809 return t
->flags
& ALARM_FLAG_DYNTICKS
;
812 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
814 if (!alarm_has_dynticks(t
))
820 /* TODO: MIN_TIMER_REARM_US should be optimized */
821 #define MIN_TIMER_REARM_US 250
823 static struct qemu_alarm_timer
*alarm_timer
;
827 struct qemu_alarm_win32
{
831 } alarm_win32_data
= {0, NULL
, -1};
833 static int win32_start_timer(struct qemu_alarm_timer
*t
);
834 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
835 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
839 static int unix_start_timer(struct qemu_alarm_timer
*t
);
840 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
844 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
845 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
846 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
848 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
849 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
851 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
852 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
854 #endif /* __linux__ */
858 static struct qemu_alarm_timer alarm_timers
[] = {
861 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
862 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
863 /* HPET - if available - is preferred */
864 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
865 /* ...otherwise try RTC */
866 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
868 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
870 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
871 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
872 {"win32", 0, win32_start_timer
,
873 win32_stop_timer
, NULL
, &alarm_win32_data
},
878 static void show_available_alarms()
882 printf("Available alarm timers, in order of precedence:\n");
883 for (i
= 0; alarm_timers
[i
].name
; i
++)
884 printf("%s\n", alarm_timers
[i
].name
);
887 static void configure_alarms(char const *opt
)
891 int count
= (sizeof(alarm_timers
) / sizeof(*alarm_timers
)) - 1;
895 if (!strcmp(opt
, "help")) {
896 show_available_alarms();
902 /* Reorder the array */
903 name
= strtok(arg
, ",");
905 struct qemu_alarm_timer tmp
;
907 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
908 if (!strcmp(alarm_timers
[i
].name
, name
))
913 fprintf(stderr
, "Unknown clock %s\n", name
);
922 tmp
= alarm_timers
[i
];
923 alarm_timers
[i
] = alarm_timers
[cur
];
924 alarm_timers
[cur
] = tmp
;
928 name
= strtok(NULL
, ",");
934 /* Disable remaining timers */
935 for (i
= cur
; i
< count
; i
++)
936 alarm_timers
[i
].name
= NULL
;
940 show_available_alarms();
946 static QEMUTimer
*active_timers
[2];
948 QEMUClock
*qemu_new_clock(int type
)
951 clock
= qemu_mallocz(sizeof(QEMUClock
));
958 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
962 ts
= qemu_mallocz(sizeof(QEMUTimer
));
969 void qemu_free_timer(QEMUTimer
*ts
)
974 /* stop a timer, but do not dealloc it */
975 void qemu_del_timer(QEMUTimer
*ts
)
979 /* NOTE: this code must be signal safe because
980 qemu_timer_expired() can be called from a signal. */
981 pt
= &active_timers
[ts
->clock
->type
];
994 /* modify the current timer so that it will be fired when current_time
995 >= expire_time. The corresponding callback will be called. */
996 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1002 /* add the timer in the sorted list */
1003 /* NOTE: this code must be signal safe because
1004 qemu_timer_expired() can be called from a signal. */
1005 pt
= &active_timers
[ts
->clock
->type
];
1010 if (t
->expire_time
> expire_time
)
1014 ts
->expire_time
= expire_time
;
1019 int qemu_timer_pending(QEMUTimer
*ts
)
1022 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1029 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1033 return (timer_head
->expire_time
<= current_time
);
1036 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1042 if (!ts
|| ts
->expire_time
> current_time
)
1044 /* remove timer from the list before calling the callback */
1045 *ptimer_head
= ts
->next
;
1048 /* run the callback (the timer list can be modified) */
1051 qemu_rearm_alarm_timer(alarm_timer
);
1054 int64_t qemu_get_clock(QEMUClock
*clock
)
1056 switch(clock
->type
) {
1057 case QEMU_TIMER_REALTIME
:
1058 return get_clock() / 1000000;
1060 case QEMU_TIMER_VIRTUAL
:
1061 return cpu_get_clock();
1065 static void init_timers(void)
1068 ticks_per_sec
= QEMU_TIMER_BASE
;
1069 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1070 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1074 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1076 uint64_t expire_time
;
1078 if (qemu_timer_pending(ts
)) {
1079 expire_time
= ts
->expire_time
;
1083 qemu_put_be64(f
, expire_time
);
1086 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1088 uint64_t expire_time
;
1090 expire_time
= qemu_get_be64(f
);
1091 if (expire_time
!= -1) {
1092 qemu_mod_timer(ts
, expire_time
);
1098 static void timer_save(QEMUFile
*f
, void *opaque
)
1100 if (cpu_ticks_enabled
) {
1101 hw_error("cannot save state if virtual timers are running");
1103 qemu_put_be64s(f
, &cpu_ticks_offset
);
1104 qemu_put_be64s(f
, &ticks_per_sec
);
1105 qemu_put_be64s(f
, &cpu_clock_offset
);
1108 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1110 if (version_id
!= 1 && version_id
!= 2)
1112 if (cpu_ticks_enabled
) {
1115 qemu_get_be64s(f
, &cpu_ticks_offset
);
1116 qemu_get_be64s(f
, &ticks_per_sec
);
1117 if (version_id
== 2) {
1118 qemu_get_be64s(f
, &cpu_clock_offset
);
1124 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1125 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1127 static void host_alarm_handler(int host_signum
)
1131 #define DISP_FREQ 1000
1133 static int64_t delta_min
= INT64_MAX
;
1134 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1136 ti
= qemu_get_clock(vm_clock
);
1137 if (last_clock
!= 0) {
1138 delta
= ti
- last_clock
;
1139 if (delta
< delta_min
)
1141 if (delta
> delta_max
)
1144 if (++count
== DISP_FREQ
) {
1145 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1146 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1147 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1148 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1149 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1151 delta_min
= INT64_MAX
;
1159 if (alarm_has_dynticks(alarm_timer
) ||
1160 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1161 qemu_get_clock(vm_clock
)) ||
1162 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1163 qemu_get_clock(rt_clock
))) {
1165 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1166 SetEvent(data
->host_alarm
);
1168 CPUState
*env
= cpu_single_env
;
1170 /* stop the currently executing cpu because a timer occured */
1171 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1173 if (env
->kqemu_enabled
) {
1174 kqemu_cpu_interrupt(env
);
1181 static uint64_t qemu_next_deadline(void)
1183 int64_t nearest_delta_us
= INT64_MAX
;
1186 if (active_timers
[QEMU_TIMER_REALTIME
])
1187 nearest_delta_us
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1188 qemu_get_clock(rt_clock
))*1000;
1190 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1192 vmdelta_us
= (active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1193 qemu_get_clock(vm_clock
)+999)/1000;
1194 if (vmdelta_us
< nearest_delta_us
)
1195 nearest_delta_us
= vmdelta_us
;
1198 /* Avoid arming the timer to negative, zero, or too low values */
1199 if (nearest_delta_us
<= MIN_TIMER_REARM_US
)
1200 nearest_delta_us
= MIN_TIMER_REARM_US
;
1202 return nearest_delta_us
;
1207 #if defined(__linux__)
1209 #define RTC_FREQ 1024
1211 static void enable_sigio_timer(int fd
)
1213 struct sigaction act
;
1216 sigfillset(&act
.sa_mask
);
1218 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1219 act
.sa_flags
|= SA_ONSTACK
;
1221 act
.sa_handler
= host_alarm_handler
;
1223 sigaction(SIGIO
, &act
, NULL
);
1224 fcntl(fd
, F_SETFL
, O_ASYNC
);
1225 fcntl(fd
, F_SETOWN
, getpid());
1228 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1230 struct hpet_info info
;
1233 fd
= open("/dev/hpet", O_RDONLY
);
1238 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1240 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1241 "error, but for better emulation accuracy type:\n"
1242 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1246 /* Check capabilities */
1247 r
= ioctl(fd
, HPET_INFO
, &info
);
1251 /* Enable periodic mode */
1252 r
= ioctl(fd
, HPET_EPI
, 0);
1253 if (info
.hi_flags
&& (r
< 0))
1256 /* Enable interrupt */
1257 r
= ioctl(fd
, HPET_IE_ON
, 0);
1261 enable_sigio_timer(fd
);
1262 t
->priv
= (void *)(long)fd
;
1270 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1272 int fd
= (long)t
->priv
;
1277 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1281 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1284 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1285 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1286 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1287 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1290 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1296 enable_sigio_timer(rtc_fd
);
1298 t
->priv
= (void *)(long)rtc_fd
;
1303 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1305 int rtc_fd
= (long)t
->priv
;
1310 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1314 struct sigaction act
;
1316 sigfillset(&act
.sa_mask
);
1318 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
1319 act
.sa_flags
|= SA_ONSTACK
;
1321 act
.sa_handler
= host_alarm_handler
;
1323 sigaction(SIGALRM
, &act
, NULL
);
1325 ev
.sigev_value
.sival_int
= 0;
1326 ev
.sigev_notify
= SIGEV_SIGNAL
;
1327 ev
.sigev_signo
= SIGALRM
;
1329 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1330 perror("timer_create");
1332 /* disable dynticks */
1333 fprintf(stderr
, "Dynamic Ticks disabled\n");
1338 t
->priv
= (void *)host_timer
;
1343 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1345 timer_t host_timer
= (timer_t
)t
->priv
;
1347 timer_delete(host_timer
);
1350 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1352 timer_t host_timer
= (timer_t
)t
->priv
;
1353 struct itimerspec timeout
;
1354 int64_t nearest_delta_us
= INT64_MAX
;
1357 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1358 !active_timers
[QEMU_TIMER_VIRTUAL
])
1361 nearest_delta_us
= qemu_next_deadline();
1363 /* check whether a timer is already running */
1364 if (timer_gettime(host_timer
, &timeout
)) {
1366 fprintf(stderr
, "Internal timer error: aborting\n");
1369 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1370 if (current_us
&& current_us
<= nearest_delta_us
)
1373 timeout
.it_interval
.tv_sec
= 0;
1374 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1375 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1376 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1377 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1379 fprintf(stderr
, "Internal timer error: aborting\n");
1384 #endif /* defined(__linux__) */
1386 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1388 struct sigaction act
;
1389 struct itimerval itv
;
1393 sigfillset(&act
.sa_mask
);
1395 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
1396 act
.sa_flags
|= SA_ONSTACK
;
1398 act
.sa_handler
= host_alarm_handler
;
1400 sigaction(SIGALRM
, &act
, NULL
);
1402 itv
.it_interval
.tv_sec
= 0;
1403 /* for i386 kernel 2.6 to get 1 ms */
1404 itv
.it_interval
.tv_usec
= 999;
1405 itv
.it_value
.tv_sec
= 0;
1406 itv
.it_value
.tv_usec
= 10 * 1000;
1408 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1415 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1417 struct itimerval itv
;
1419 memset(&itv
, 0, sizeof(itv
));
1420 setitimer(ITIMER_REAL
, &itv
, NULL
);
1423 #endif /* !defined(_WIN32) */
1427 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1430 struct qemu_alarm_win32
*data
= t
->priv
;
1433 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1434 if (!data
->host_alarm
) {
1435 perror("Failed CreateEvent");
1439 memset(&tc
, 0, sizeof(tc
));
1440 timeGetDevCaps(&tc
, sizeof(tc
));
1442 if (data
->period
< tc
.wPeriodMin
)
1443 data
->period
= tc
.wPeriodMin
;
1445 timeBeginPeriod(data
->period
);
1447 flags
= TIME_CALLBACK_FUNCTION
;
1448 if (alarm_has_dynticks(t
))
1449 flags
|= TIME_ONESHOT
;
1451 flags
|= TIME_PERIODIC
;
1453 data
->timerId
= timeSetEvent(1, // interval (ms)
1454 data
->period
, // resolution
1455 host_alarm_handler
, // function
1456 (DWORD
)t
, // parameter
1459 if (!data
->timerId
) {
1460 perror("Failed to initialize win32 alarm timer");
1462 timeEndPeriod(data
->period
);
1463 CloseHandle(data
->host_alarm
);
1467 qemu_add_wait_object(data
->host_alarm
, NULL
, NULL
);
1472 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1474 struct qemu_alarm_win32
*data
= t
->priv
;
1476 timeKillEvent(data
->timerId
);
1477 timeEndPeriod(data
->period
);
1479 CloseHandle(data
->host_alarm
);
1482 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1484 struct qemu_alarm_win32
*data
= t
->priv
;
1485 uint64_t nearest_delta_us
;
1487 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1488 !active_timers
[QEMU_TIMER_VIRTUAL
])
1491 nearest_delta_us
= qemu_next_deadline();
1492 nearest_delta_us
/= 1000;
1494 timeKillEvent(data
->timerId
);
1496 data
->timerId
= timeSetEvent(1,
1500 TIME_ONESHOT
| TIME_PERIODIC
);
1502 if (!data
->timerId
) {
1503 perror("Failed to re-arm win32 alarm timer");
1505 timeEndPeriod(data
->period
);
1506 CloseHandle(data
->host_alarm
);
1513 static void init_timer_alarm(void)
1515 struct qemu_alarm_timer
*t
;
1518 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1519 t
= &alarm_timers
[i
];
1527 fprintf(stderr
, "Unable to find any suitable alarm timer.\n");
1528 fprintf(stderr
, "Terminating\n");
1535 void quit_timers(void)
1537 alarm_timer
->stop(alarm_timer
);
1541 /***********************************************************/
1542 /* character device */
1544 static void qemu_chr_event(CharDriverState
*s
, int event
)
1548 s
->chr_event(s
->handler_opaque
, event
);
1551 static void qemu_chr_reset_bh(void *opaque
)
1553 CharDriverState
*s
= opaque
;
1554 qemu_chr_event(s
, CHR_EVENT_RESET
);
1555 qemu_bh_delete(s
->bh
);
1559 void qemu_chr_reset(CharDriverState
*s
)
1561 if (s
->bh
== NULL
) {
1562 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
1563 qemu_bh_schedule(s
->bh
);
1567 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1569 return s
->chr_write(s
, buf
, len
);
1572 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1576 return s
->chr_ioctl(s
, cmd
, arg
);
1579 int qemu_chr_can_read(CharDriverState
*s
)
1581 if (!s
->chr_can_read
)
1583 return s
->chr_can_read(s
->handler_opaque
);
1586 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
1588 s
->chr_read(s
->handler_opaque
, buf
, len
);
1592 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1597 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1598 qemu_chr_write(s
, buf
, strlen(buf
));
1602 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1604 if (s
->chr_send_event
)
1605 s
->chr_send_event(s
, event
);
1608 void qemu_chr_add_handlers(CharDriverState
*s
,
1609 IOCanRWHandler
*fd_can_read
,
1610 IOReadHandler
*fd_read
,
1611 IOEventHandler
*fd_event
,
1614 s
->chr_can_read
= fd_can_read
;
1615 s
->chr_read
= fd_read
;
1616 s
->chr_event
= fd_event
;
1617 s
->handler_opaque
= opaque
;
1618 if (s
->chr_update_read_handler
)
1619 s
->chr_update_read_handler(s
);
1622 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1627 static CharDriverState
*qemu_chr_open_null(void)
1629 CharDriverState
*chr
;
1631 chr
= qemu_mallocz(sizeof(CharDriverState
));
1634 chr
->chr_write
= null_chr_write
;
1638 /* MUX driver for serial I/O splitting */
1639 static int term_timestamps
;
1640 static int64_t term_timestamps_start
;
1643 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
1644 IOReadHandler
*chr_read
[MAX_MUX
];
1645 IOEventHandler
*chr_event
[MAX_MUX
];
1646 void *ext_opaque
[MAX_MUX
];
1647 CharDriverState
*drv
;
1649 int term_got_escape
;
1654 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1656 MuxDriver
*d
= chr
->opaque
;
1658 if (!term_timestamps
) {
1659 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
1664 for(i
= 0; i
< len
; i
++) {
1665 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
1666 if (buf
[i
] == '\n') {
1672 if (term_timestamps_start
== -1)
1673 term_timestamps_start
= ti
;
1674 ti
-= term_timestamps_start
;
1675 secs
= ti
/ 1000000000;
1676 snprintf(buf1
, sizeof(buf1
),
1677 "[%02d:%02d:%02d.%03d] ",
1681 (int)((ti
/ 1000000) % 1000));
1682 d
->drv
->chr_write(d
->drv
, buf1
, strlen(buf1
));
1689 static char *mux_help
[] = {
1690 "% h print this help\n\r",
1691 "% x exit emulator\n\r",
1692 "% s save disk data back to file (if -snapshot)\n\r",
1693 "% t toggle console timestamps\n\r"
1694 "% b send break (magic sysrq)\n\r",
1695 "% c switch between console and monitor\n\r",
1700 static int term_escape_char
= 0x01; /* ctrl-a is used for escape */
1701 static void mux_print_help(CharDriverState
*chr
)
1704 char ebuf
[15] = "Escape-Char";
1705 char cbuf
[50] = "\n\r";
1707 if (term_escape_char
> 0 && term_escape_char
< 26) {
1708 sprintf(cbuf
,"\n\r");
1709 sprintf(ebuf
,"C-%c", term_escape_char
- 1 + 'a');
1711 sprintf(cbuf
,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char
);
1713 chr
->chr_write(chr
, cbuf
, strlen(cbuf
));
1714 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
1715 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
1716 if (mux_help
[i
][j
] == '%')
1717 chr
->chr_write(chr
, ebuf
, strlen(ebuf
));
1719 chr
->chr_write(chr
, &mux_help
[i
][j
], 1);
1724 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
1726 if (d
->term_got_escape
) {
1727 d
->term_got_escape
= 0;
1728 if (ch
== term_escape_char
)
1733 mux_print_help(chr
);
1737 char *term
= "QEMU: Terminated\n\r";
1738 chr
->chr_write(chr
,term
,strlen(term
));
1745 for (i
= 0; i
< MAX_DISKS
; i
++) {
1747 bdrv_commit(bs_table
[i
]);
1750 bdrv_commit(mtd_bdrv
);
1754 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1757 /* Switch to the next registered device */
1759 if (chr
->focus
>= d
->mux_cnt
)
1763 term_timestamps
= !term_timestamps
;
1764 term_timestamps_start
= -1;
1767 } else if (ch
== term_escape_char
) {
1768 d
->term_got_escape
= 1;
1776 static int mux_chr_can_read(void *opaque
)
1778 CharDriverState
*chr
= opaque
;
1779 MuxDriver
*d
= chr
->opaque
;
1780 if (d
->chr_can_read
[chr
->focus
])
1781 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
1785 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
1787 CharDriverState
*chr
= opaque
;
1788 MuxDriver
*d
= chr
->opaque
;
1790 for(i
= 0; i
< size
; i
++)
1791 if (mux_proc_byte(chr
, d
, buf
[i
]))
1792 d
->chr_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
], &buf
[i
], 1);
1795 static void mux_chr_event(void *opaque
, int event
)
1797 CharDriverState
*chr
= opaque
;
1798 MuxDriver
*d
= chr
->opaque
;
1801 /* Send the event to all registered listeners */
1802 for (i
= 0; i
< d
->mux_cnt
; i
++)
1803 if (d
->chr_event
[i
])
1804 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
1807 static void mux_chr_update_read_handler(CharDriverState
*chr
)
1809 MuxDriver
*d
= chr
->opaque
;
1811 if (d
->mux_cnt
>= MAX_MUX
) {
1812 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
1815 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
1816 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
1817 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
1818 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
1819 /* Fix up the real driver with mux routines */
1820 if (d
->mux_cnt
== 0) {
1821 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
1822 mux_chr_event
, chr
);
1824 chr
->focus
= d
->mux_cnt
;
1828 CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
1830 CharDriverState
*chr
;
1833 chr
= qemu_mallocz(sizeof(CharDriverState
));
1836 d
= qemu_mallocz(sizeof(MuxDriver
));
1845 chr
->chr_write
= mux_chr_write
;
1846 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
1853 static void socket_cleanup(void)
1858 static int socket_init(void)
1863 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1865 err
= WSAGetLastError();
1866 fprintf(stderr
, "WSAStartup: %d\n", err
);
1869 atexit(socket_cleanup
);
1873 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1879 ret
= send(fd
, buf
, len
, 0);
1882 errno
= WSAGetLastError();
1883 if (errno
!= WSAEWOULDBLOCK
) {
1886 } else if (ret
== 0) {
1896 void socket_set_nonblock(int fd
)
1898 unsigned long opt
= 1;
1899 ioctlsocket(fd
, FIONBIO
, &opt
);
1904 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1910 ret
= write(fd
, buf
, len
);
1912 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1914 } else if (ret
== 0) {
1924 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1926 return unix_write(fd
, buf
, len1
);
1929 void socket_set_nonblock(int fd
)
1931 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1933 #endif /* !_WIN32 */
1942 #define STDIO_MAX_CLIENTS 1
1943 static int stdio_nb_clients
= 0;
1945 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1947 FDCharDriver
*s
= chr
->opaque
;
1948 return unix_write(s
->fd_out
, buf
, len
);
1951 static int fd_chr_read_poll(void *opaque
)
1953 CharDriverState
*chr
= opaque
;
1954 FDCharDriver
*s
= chr
->opaque
;
1956 s
->max_size
= qemu_chr_can_read(chr
);
1960 static void fd_chr_read(void *opaque
)
1962 CharDriverState
*chr
= opaque
;
1963 FDCharDriver
*s
= chr
->opaque
;
1968 if (len
> s
->max_size
)
1972 size
= read(s
->fd_in
, buf
, len
);
1974 /* FD has been closed. Remove it from the active list. */
1975 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1979 qemu_chr_read(chr
, buf
, size
);
1983 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1985 FDCharDriver
*s
= chr
->opaque
;
1987 if (s
->fd_in
>= 0) {
1988 if (nographic
&& s
->fd_in
== 0) {
1990 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1991 fd_chr_read
, NULL
, chr
);
1996 /* open a character device to a unix fd */
1997 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1999 CharDriverState
*chr
;
2002 chr
= qemu_mallocz(sizeof(CharDriverState
));
2005 s
= qemu_mallocz(sizeof(FDCharDriver
));
2013 chr
->chr_write
= fd_chr_write
;
2014 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
2016 qemu_chr_reset(chr
);
2021 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
2025 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
2028 return qemu_chr_open_fd(-1, fd_out
);
2031 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
2034 char filename_in
[256], filename_out
[256];
2036 snprintf(filename_in
, 256, "%s.in", filename
);
2037 snprintf(filename_out
, 256, "%s.out", filename
);
2038 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
2039 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
2040 if (fd_in
< 0 || fd_out
< 0) {
2045 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
2049 return qemu_chr_open_fd(fd_in
, fd_out
);
2053 /* for STDIO, we handle the case where several clients use it
2056 #define TERM_FIFO_MAX_SIZE 1
2058 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
2059 static int term_fifo_size
;
2061 static int stdio_read_poll(void *opaque
)
2063 CharDriverState
*chr
= opaque
;
2065 /* try to flush the queue if needed */
2066 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
2067 qemu_chr_read(chr
, term_fifo
, 1);
2070 /* see if we can absorb more chars */
2071 if (term_fifo_size
== 0)
2077 static void stdio_read(void *opaque
)
2081 CharDriverState
*chr
= opaque
;
2083 size
= read(0, buf
, 1);
2085 /* stdin has been closed. Remove it from the active list. */
2086 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
2090 if (qemu_chr_can_read(chr
) > 0) {
2091 qemu_chr_read(chr
, buf
, 1);
2092 } else if (term_fifo_size
== 0) {
2093 term_fifo
[term_fifo_size
++] = buf
[0];
2098 /* init terminal so that we can grab keys */
2099 static struct termios oldtty
;
2100 static int old_fd0_flags
;
2102 static void term_exit(void)
2104 tcsetattr (0, TCSANOW
, &oldtty
);
2105 fcntl(0, F_SETFL
, old_fd0_flags
);
2108 static void term_init(void)
2112 tcgetattr (0, &tty
);
2114 old_fd0_flags
= fcntl(0, F_GETFL
);
2116 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
2117 |INLCR
|IGNCR
|ICRNL
|IXON
);
2118 tty
.c_oflag
|= OPOST
;
2119 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
2120 /* if graphical mode, we allow Ctrl-C handling */
2122 tty
.c_lflag
&= ~ISIG
;
2123 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
2126 tty
.c_cc
[VTIME
] = 0;
2128 tcsetattr (0, TCSANOW
, &tty
);
2132 fcntl(0, F_SETFL
, O_NONBLOCK
);
2135 static CharDriverState
*qemu_chr_open_stdio(void)
2137 CharDriverState
*chr
;
2139 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
2141 chr
= qemu_chr_open_fd(0, 1);
2142 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
2149 #if defined(__linux__) || defined(__sun__)
2150 static CharDriverState
*qemu_chr_open_pty(void)
2153 char slave_name
[1024];
2154 int master_fd
, slave_fd
;
2156 #if defined(__linux__)
2157 /* Not satisfying */
2158 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
2163 /* Disabling local echo and line-buffered output */
2164 tcgetattr (master_fd
, &tty
);
2165 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
2167 tty
.c_cc
[VTIME
] = 0;
2168 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
2170 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
2171 return qemu_chr_open_fd(master_fd
, master_fd
);
2174 static void tty_serial_init(int fd
, int speed
,
2175 int parity
, int data_bits
, int stop_bits
)
2181 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
2182 speed
, parity
, data_bits
, stop_bits
);
2184 tcgetattr (fd
, &tty
);
2226 cfsetispeed(&tty
, spd
);
2227 cfsetospeed(&tty
, spd
);
2229 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
2230 |INLCR
|IGNCR
|ICRNL
|IXON
);
2231 tty
.c_oflag
|= OPOST
;
2232 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
2233 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
2254 tty
.c_cflag
|= PARENB
;
2257 tty
.c_cflag
|= PARENB
| PARODD
;
2261 tty
.c_cflag
|= CSTOPB
;
2263 tcsetattr (fd
, TCSANOW
, &tty
);
2266 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2268 FDCharDriver
*s
= chr
->opaque
;
2271 case CHR_IOCTL_SERIAL_SET_PARAMS
:
2273 QEMUSerialSetParams
*ssp
= arg
;
2274 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
2275 ssp
->data_bits
, ssp
->stop_bits
);
2278 case CHR_IOCTL_SERIAL_SET_BREAK
:
2280 int enable
= *(int *)arg
;
2282 tcsendbreak(s
->fd_in
, 1);
2291 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
2293 CharDriverState
*chr
;
2296 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
2297 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2298 tty_serial_init(fd
, 115200, 'N', 8, 1);
2299 chr
= qemu_chr_open_fd(fd
, fd
);
2304 chr
->chr_ioctl
= tty_serial_ioctl
;
2305 qemu_chr_reset(chr
);
2308 #else /* ! __linux__ && ! __sun__ */
2309 static CharDriverState
*qemu_chr_open_pty(void)
2313 #endif /* __linux__ || __sun__ */
2315 #if defined(__linux__)
2319 } ParallelCharDriver
;
2321 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
2323 if (s
->mode
!= mode
) {
2325 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
2332 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2334 ParallelCharDriver
*drv
= chr
->opaque
;
2339 case CHR_IOCTL_PP_READ_DATA
:
2340 if (ioctl(fd
, PPRDATA
, &b
) < 0)
2342 *(uint8_t *)arg
= b
;
2344 case CHR_IOCTL_PP_WRITE_DATA
:
2345 b
= *(uint8_t *)arg
;
2346 if (ioctl(fd
, PPWDATA
, &b
) < 0)
2349 case CHR_IOCTL_PP_READ_CONTROL
:
2350 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
2352 /* Linux gives only the lowest bits, and no way to know data
2353 direction! For better compatibility set the fixed upper
2355 *(uint8_t *)arg
= b
| 0xc0;
2357 case CHR_IOCTL_PP_WRITE_CONTROL
:
2358 b
= *(uint8_t *)arg
;
2359 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
2362 case CHR_IOCTL_PP_READ_STATUS
:
2363 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
2365 *(uint8_t *)arg
= b
;
2367 case CHR_IOCTL_PP_EPP_READ_ADDR
:
2368 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2369 struct ParallelIOArg
*parg
= arg
;
2370 int n
= read(fd
, parg
->buffer
, parg
->count
);
2371 if (n
!= parg
->count
) {
2376 case CHR_IOCTL_PP_EPP_READ
:
2377 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2378 struct ParallelIOArg
*parg
= arg
;
2379 int n
= read(fd
, parg
->buffer
, parg
->count
);
2380 if (n
!= parg
->count
) {
2385 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
2386 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2387 struct ParallelIOArg
*parg
= arg
;
2388 int n
= write(fd
, parg
->buffer
, parg
->count
);
2389 if (n
!= parg
->count
) {
2394 case CHR_IOCTL_PP_EPP_WRITE
:
2395 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2396 struct ParallelIOArg
*parg
= arg
;
2397 int n
= write(fd
, parg
->buffer
, parg
->count
);
2398 if (n
!= parg
->count
) {
2409 static void pp_close(CharDriverState
*chr
)
2411 ParallelCharDriver
*drv
= chr
->opaque
;
2414 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
2415 ioctl(fd
, PPRELEASE
);
2420 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
2422 CharDriverState
*chr
;
2423 ParallelCharDriver
*drv
;
2426 TFR(fd
= open(filename
, O_RDWR
));
2430 if (ioctl(fd
, PPCLAIM
) < 0) {
2435 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
2441 drv
->mode
= IEEE1284_MODE_COMPAT
;
2443 chr
= qemu_mallocz(sizeof(CharDriverState
));
2449 chr
->chr_write
= null_chr_write
;
2450 chr
->chr_ioctl
= pp_ioctl
;
2451 chr
->chr_close
= pp_close
;
2454 qemu_chr_reset(chr
);
2458 #endif /* __linux__ */
2464 HANDLE hcom
, hrecv
, hsend
;
2465 OVERLAPPED orecv
, osend
;
2470 #define NSENDBUF 2048
2471 #define NRECVBUF 2048
2472 #define MAXCONNECT 1
2473 #define NTIMEOUT 5000
2475 static int win_chr_poll(void *opaque
);
2476 static int win_chr_pipe_poll(void *opaque
);
2478 static void win_chr_close(CharDriverState
*chr
)
2480 WinCharState
*s
= chr
->opaque
;
2483 CloseHandle(s
->hsend
);
2487 CloseHandle(s
->hrecv
);
2491 CloseHandle(s
->hcom
);
2495 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
2497 qemu_del_polling_cb(win_chr_poll
, chr
);
2500 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
2502 WinCharState
*s
= chr
->opaque
;
2504 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2509 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2511 fprintf(stderr
, "Failed CreateEvent\n");
2514 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2516 fprintf(stderr
, "Failed CreateEvent\n");
2520 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2521 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2522 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2523 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
2528 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2529 fprintf(stderr
, "Failed SetupComm\n");
2533 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2534 size
= sizeof(COMMCONFIG
);
2535 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2536 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2537 CommConfigDialog(filename
, NULL
, &comcfg
);
2539 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2540 fprintf(stderr
, "Failed SetCommState\n");
2544 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2545 fprintf(stderr
, "Failed SetCommMask\n");
2549 cto
.ReadIntervalTimeout
= MAXDWORD
;
2550 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2551 fprintf(stderr
, "Failed SetCommTimeouts\n");
2555 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2556 fprintf(stderr
, "Failed ClearCommError\n");
2559 qemu_add_polling_cb(win_chr_poll
, chr
);
2567 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2569 WinCharState
*s
= chr
->opaque
;
2570 DWORD len
, ret
, size
, err
;
2573 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2574 s
->osend
.hEvent
= s
->hsend
;
2577 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2579 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2581 err
= GetLastError();
2582 if (err
== ERROR_IO_PENDING
) {
2583 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2601 static int win_chr_read_poll(CharDriverState
*chr
)
2603 WinCharState
*s
= chr
->opaque
;
2605 s
->max_size
= qemu_chr_can_read(chr
);
2609 static void win_chr_readfile(CharDriverState
*chr
)
2611 WinCharState
*s
= chr
->opaque
;
2616 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2617 s
->orecv
.hEvent
= s
->hrecv
;
2618 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2620 err
= GetLastError();
2621 if (err
== ERROR_IO_PENDING
) {
2622 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2627 qemu_chr_read(chr
, buf
, size
);
2631 static void win_chr_read(CharDriverState
*chr
)
2633 WinCharState
*s
= chr
->opaque
;
2635 if (s
->len
> s
->max_size
)
2636 s
->len
= s
->max_size
;
2640 win_chr_readfile(chr
);
2643 static int win_chr_poll(void *opaque
)
2645 CharDriverState
*chr
= opaque
;
2646 WinCharState
*s
= chr
->opaque
;
2650 ClearCommError(s
->hcom
, &comerr
, &status
);
2651 if (status
.cbInQue
> 0) {
2652 s
->len
= status
.cbInQue
;
2653 win_chr_read_poll(chr
);
2660 static CharDriverState
*qemu_chr_open_win(const char *filename
)
2662 CharDriverState
*chr
;
2665 chr
= qemu_mallocz(sizeof(CharDriverState
));
2668 s
= qemu_mallocz(sizeof(WinCharState
));
2674 chr
->chr_write
= win_chr_write
;
2675 chr
->chr_close
= win_chr_close
;
2677 if (win_chr_init(chr
, filename
) < 0) {
2682 qemu_chr_reset(chr
);
2686 static int win_chr_pipe_poll(void *opaque
)
2688 CharDriverState
*chr
= opaque
;
2689 WinCharState
*s
= chr
->opaque
;
2692 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2695 win_chr_read_poll(chr
);
2702 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
2704 WinCharState
*s
= chr
->opaque
;
2712 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2714 fprintf(stderr
, "Failed CreateEvent\n");
2717 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2719 fprintf(stderr
, "Failed CreateEvent\n");
2723 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2724 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2725 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2727 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2728 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2729 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2734 ZeroMemory(&ov
, sizeof(ov
));
2735 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2736 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2738 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2742 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2744 fprintf(stderr
, "Failed GetOverlappedResult\n");
2746 CloseHandle(ov
.hEvent
);
2753 CloseHandle(ov
.hEvent
);
2756 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2765 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2767 CharDriverState
*chr
;
2770 chr
= qemu_mallocz(sizeof(CharDriverState
));
2773 s
= qemu_mallocz(sizeof(WinCharState
));
2779 chr
->chr_write
= win_chr_write
;
2780 chr
->chr_close
= win_chr_close
;
2782 if (win_chr_pipe_init(chr
, filename
) < 0) {
2787 qemu_chr_reset(chr
);
2791 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2793 CharDriverState
*chr
;
2796 chr
= qemu_mallocz(sizeof(CharDriverState
));
2799 s
= qemu_mallocz(sizeof(WinCharState
));
2806 chr
->chr_write
= win_chr_write
;
2807 qemu_chr_reset(chr
);
2811 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
2813 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2816 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2820 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2821 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2822 if (fd_out
== INVALID_HANDLE_VALUE
)
2825 return qemu_chr_open_win_file(fd_out
);
2827 #endif /* !_WIN32 */
2829 /***********************************************************/
2830 /* UDP Net console */
2834 struct sockaddr_in daddr
;
2841 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2843 NetCharDriver
*s
= chr
->opaque
;
2845 return sendto(s
->fd
, buf
, len
, 0,
2846 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2849 static int udp_chr_read_poll(void *opaque
)
2851 CharDriverState
*chr
= opaque
;
2852 NetCharDriver
*s
= chr
->opaque
;
2854 s
->max_size
= qemu_chr_can_read(chr
);
2856 /* If there were any stray characters in the queue process them
2859 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2860 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2862 s
->max_size
= qemu_chr_can_read(chr
);
2867 static void udp_chr_read(void *opaque
)
2869 CharDriverState
*chr
= opaque
;
2870 NetCharDriver
*s
= chr
->opaque
;
2872 if (s
->max_size
== 0)
2874 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2875 s
->bufptr
= s
->bufcnt
;
2880 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2881 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2883 s
->max_size
= qemu_chr_can_read(chr
);
2887 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2889 NetCharDriver
*s
= chr
->opaque
;
2892 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2893 udp_chr_read
, NULL
, chr
);
2897 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2899 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2901 int parse_host_src_port(struct sockaddr_in
*haddr
,
2902 struct sockaddr_in
*saddr
,
2905 static CharDriverState
*qemu_chr_open_udp(const char *def
)
2907 CharDriverState
*chr
= NULL
;
2908 NetCharDriver
*s
= NULL
;
2910 struct sockaddr_in saddr
;
2912 chr
= qemu_mallocz(sizeof(CharDriverState
));
2915 s
= qemu_mallocz(sizeof(NetCharDriver
));
2919 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2921 perror("socket(PF_INET, SOCK_DGRAM)");
2925 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2926 printf("Could not parse: %s\n", def
);
2930 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2940 chr
->chr_write
= udp_chr_write
;
2941 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2954 /***********************************************************/
2955 /* TCP Net console */
2966 static void tcp_chr_accept(void *opaque
);
2968 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2970 TCPCharDriver
*s
= chr
->opaque
;
2972 return send_all(s
->fd
, buf
, len
);
2974 /* XXX: indicate an error ? */
2979 static int tcp_chr_read_poll(void *opaque
)
2981 CharDriverState
*chr
= opaque
;
2982 TCPCharDriver
*s
= chr
->opaque
;
2985 s
->max_size
= qemu_chr_can_read(chr
);
2990 #define IAC_BREAK 243
2991 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2993 char *buf
, int *size
)
2995 /* Handle any telnet client's basic IAC options to satisfy char by
2996 * char mode with no echo. All IAC options will be removed from
2997 * the buf and the do_telnetopt variable will be used to track the
2998 * state of the width of the IAC information.
3000 * IAC commands come in sets of 3 bytes with the exception of the
3001 * "IAC BREAK" command and the double IAC.
3007 for (i
= 0; i
< *size
; i
++) {
3008 if (s
->do_telnetopt
> 1) {
3009 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
3010 /* Double IAC means send an IAC */
3014 s
->do_telnetopt
= 1;
3016 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
3017 /* Handle IAC break commands by sending a serial break */
3018 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
3023 if (s
->do_telnetopt
>= 4) {
3024 s
->do_telnetopt
= 1;
3027 if ((unsigned char)buf
[i
] == IAC
) {
3028 s
->do_telnetopt
= 2;
3039 static void tcp_chr_read(void *opaque
)
3041 CharDriverState
*chr
= opaque
;
3042 TCPCharDriver
*s
= chr
->opaque
;
3046 if (!s
->connected
|| s
->max_size
<= 0)
3049 if (len
> s
->max_size
)
3051 size
= recv(s
->fd
, buf
, len
, 0);
3053 /* connection closed */
3055 if (s
->listen_fd
>= 0) {
3056 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
3058 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3061 } else if (size
> 0) {
3062 if (s
->do_telnetopt
)
3063 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
3065 qemu_chr_read(chr
, buf
, size
);
3069 static void tcp_chr_connect(void *opaque
)
3071 CharDriverState
*chr
= opaque
;
3072 TCPCharDriver
*s
= chr
->opaque
;
3075 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
3076 tcp_chr_read
, NULL
, chr
);
3077 qemu_chr_reset(chr
);
3080 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3081 static void tcp_chr_telnet_init(int fd
)
3084 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3085 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3086 send(fd
, (char *)buf
, 3, 0);
3087 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3088 send(fd
, (char *)buf
, 3, 0);
3089 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3090 send(fd
, (char *)buf
, 3, 0);
3091 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3092 send(fd
, (char *)buf
, 3, 0);
3095 static void socket_set_nodelay(int fd
)
3098 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
3101 static void tcp_chr_accept(void *opaque
)
3103 CharDriverState
*chr
= opaque
;
3104 TCPCharDriver
*s
= chr
->opaque
;
3105 struct sockaddr_in saddr
;
3107 struct sockaddr_un uaddr
;
3109 struct sockaddr
*addr
;
3116 len
= sizeof(uaddr
);
3117 addr
= (struct sockaddr
*)&uaddr
;
3121 len
= sizeof(saddr
);
3122 addr
= (struct sockaddr
*)&saddr
;
3124 fd
= accept(s
->listen_fd
, addr
, &len
);
3125 if (fd
< 0 && errno
!= EINTR
) {
3127 } else if (fd
>= 0) {
3128 if (s
->do_telnetopt
)
3129 tcp_chr_telnet_init(fd
);
3133 socket_set_nonblock(fd
);
3135 socket_set_nodelay(fd
);
3137 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
3138 tcp_chr_connect(chr
);
3141 static void tcp_chr_close(CharDriverState
*chr
)
3143 TCPCharDriver
*s
= chr
->opaque
;
3146 if (s
->listen_fd
>= 0)
3147 closesocket(s
->listen_fd
);
3151 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
3155 CharDriverState
*chr
= NULL
;
3156 TCPCharDriver
*s
= NULL
;
3157 int fd
= -1, ret
, err
, val
;
3159 int is_waitconnect
= 1;
3162 struct sockaddr_in saddr
;
3164 struct sockaddr_un uaddr
;
3166 struct sockaddr
*addr
;
3171 addr
= (struct sockaddr
*)&uaddr
;
3172 addrlen
= sizeof(uaddr
);
3173 if (parse_unix_path(&uaddr
, host_str
) < 0)
3178 addr
= (struct sockaddr
*)&saddr
;
3179 addrlen
= sizeof(saddr
);
3180 if (parse_host_port(&saddr
, host_str
) < 0)
3185 while((ptr
= strchr(ptr
,','))) {
3187 if (!strncmp(ptr
,"server",6)) {
3189 } else if (!strncmp(ptr
,"nowait",6)) {
3191 } else if (!strncmp(ptr
,"nodelay",6)) {
3194 printf("Unknown option: %s\n", ptr
);
3201 chr
= qemu_mallocz(sizeof(CharDriverState
));
3204 s
= qemu_mallocz(sizeof(TCPCharDriver
));
3210 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
3213 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3218 if (!is_waitconnect
)
3219 socket_set_nonblock(fd
);
3224 s
->is_unix
= is_unix
;
3225 s
->do_nodelay
= do_nodelay
&& !is_unix
;
3228 chr
->chr_write
= tcp_chr_write
;
3229 chr
->chr_close
= tcp_chr_close
;
3232 /* allow fast reuse */
3236 strncpy(path
, uaddr
.sun_path
, 108);
3243 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3246 ret
= bind(fd
, addr
, addrlen
);
3250 ret
= listen(fd
, 0);
3255 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
3257 s
->do_telnetopt
= 1;
3260 ret
= connect(fd
, addr
, addrlen
);
3262 err
= socket_error();
3263 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3264 } else if (err
== EINPROGRESS
) {
3267 } else if (err
== WSAEALREADY
) {
3279 socket_set_nodelay(fd
);
3281 tcp_chr_connect(chr
);
3283 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
3286 if (is_listen
&& is_waitconnect
) {
3287 printf("QEMU waiting for connection on: %s\n", host_str
);
3288 tcp_chr_accept(chr
);
3289 socket_set_nonblock(s
->listen_fd
);
3301 CharDriverState
*qemu_chr_open(const char *filename
)
3305 if (!strcmp(filename
, "vc")) {
3306 return text_console_init(&display_state
, 0);
3307 } else if (strstart(filename
, "vc:", &p
)) {
3308 return text_console_init(&display_state
, p
);
3309 } else if (!strcmp(filename
, "null")) {
3310 return qemu_chr_open_null();
3312 if (strstart(filename
, "tcp:", &p
)) {
3313 return qemu_chr_open_tcp(p
, 0, 0);
3315 if (strstart(filename
, "telnet:", &p
)) {
3316 return qemu_chr_open_tcp(p
, 1, 0);
3318 if (strstart(filename
, "udp:", &p
)) {
3319 return qemu_chr_open_udp(p
);
3321 if (strstart(filename
, "mon:", &p
)) {
3322 CharDriverState
*drv
= qemu_chr_open(p
);
3324 drv
= qemu_chr_open_mux(drv
);
3325 monitor_init(drv
, !nographic
);
3328 printf("Unable to open driver: %s\n", p
);
3332 if (strstart(filename
, "unix:", &p
)) {
3333 return qemu_chr_open_tcp(p
, 0, 1);
3334 } else if (strstart(filename
, "file:", &p
)) {
3335 return qemu_chr_open_file_out(p
);
3336 } else if (strstart(filename
, "pipe:", &p
)) {
3337 return qemu_chr_open_pipe(p
);
3338 } else if (!strcmp(filename
, "pty")) {
3339 return qemu_chr_open_pty();
3340 } else if (!strcmp(filename
, "stdio")) {
3341 return qemu_chr_open_stdio();
3343 #if defined(__linux__)
3344 if (strstart(filename
, "/dev/parport", NULL
)) {
3345 return qemu_chr_open_pp(filename
);
3348 #if defined(__linux__) || defined(__sun__)
3349 if (strstart(filename
, "/dev/", NULL
)) {
3350 return qemu_chr_open_tty(filename
);
3354 if (strstart(filename
, "COM", NULL
)) {
3355 return qemu_chr_open_win(filename
);
3357 if (strstart(filename
, "pipe:", &p
)) {
3358 return qemu_chr_open_win_pipe(p
);
3360 if (strstart(filename
, "con:", NULL
)) {
3361 return qemu_chr_open_win_con(filename
);
3363 if (strstart(filename
, "file:", &p
)) {
3364 return qemu_chr_open_win_file_out(p
);
3372 void qemu_chr_close(CharDriverState
*chr
)
3375 chr
->chr_close(chr
);
3378 /***********************************************************/
3379 /* network device redirectors */
3381 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
3385 for(i
=0;i
<size
;i
+=16) {
3389 fprintf(f
, "%08x ", i
);
3392 fprintf(f
, " %02x", buf
[i
+j
]);
3397 for(j
=0;j
<len
;j
++) {
3399 if (c
< ' ' || c
> '~')
3401 fprintf(f
, "%c", c
);
3407 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
3410 for(i
= 0; i
< 6; i
++) {
3411 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3424 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
3429 p1
= strchr(p
, sep
);
3435 if (len
> buf_size
- 1)
3437 memcpy(buf
, p
, len
);
3444 int parse_host_src_port(struct sockaddr_in
*haddr
,
3445 struct sockaddr_in
*saddr
,
3446 const char *input_str
)
3448 char *str
= strdup(input_str
);
3449 char *host_str
= str
;
3454 * Chop off any extra arguments at the end of the string which
3455 * would start with a comma, then fill in the src port information
3456 * if it was provided else use the "any address" and "any port".
3458 if ((ptr
= strchr(str
,',')))
3461 if ((src_str
= strchr(input_str
,'@'))) {
3466 if (parse_host_port(haddr
, host_str
) < 0)
3469 if (!src_str
|| *src_str
== '\0')
3472 if (parse_host_port(saddr
, src_str
) < 0)
3483 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
3491 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3493 saddr
->sin_family
= AF_INET
;
3494 if (buf
[0] == '\0') {
3495 saddr
->sin_addr
.s_addr
= 0;
3497 if (isdigit(buf
[0])) {
3498 if (!inet_aton(buf
, &saddr
->sin_addr
))
3501 if ((he
= gethostbyname(buf
)) == NULL
)
3503 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
3506 port
= strtol(p
, (char **)&r
, 0);
3509 saddr
->sin_port
= htons(port
);
3514 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
3519 len
= MIN(108, strlen(str
));
3520 p
= strchr(str
, ',');
3522 len
= MIN(len
, p
- str
);
3524 memset(uaddr
, 0, sizeof(*uaddr
));
3526 uaddr
->sun_family
= AF_UNIX
;
3527 memcpy(uaddr
->sun_path
, str
, len
);
3533 /* find or alloc a new VLAN */
3534 VLANState
*qemu_find_vlan(int id
)
3536 VLANState
**pvlan
, *vlan
;
3537 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3541 vlan
= qemu_mallocz(sizeof(VLANState
));
3546 pvlan
= &first_vlan
;
3547 while (*pvlan
!= NULL
)
3548 pvlan
= &(*pvlan
)->next
;
3553 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
3554 IOReadHandler
*fd_read
,
3555 IOCanRWHandler
*fd_can_read
,
3558 VLANClientState
*vc
, **pvc
;
3559 vc
= qemu_mallocz(sizeof(VLANClientState
));
3562 vc
->fd_read
= fd_read
;
3563 vc
->fd_can_read
= fd_can_read
;
3564 vc
->opaque
= opaque
;
3568 pvc
= &vlan
->first_client
;
3569 while (*pvc
!= NULL
)
3570 pvc
= &(*pvc
)->next
;
3575 int qemu_can_send_packet(VLANClientState
*vc1
)
3577 VLANState
*vlan
= vc1
->vlan
;
3578 VLANClientState
*vc
;
3580 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3582 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
3589 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
3591 VLANState
*vlan
= vc1
->vlan
;
3592 VLANClientState
*vc
;
3595 printf("vlan %d send:\n", vlan
->id
);
3596 hex_dump(stdout
, buf
, size
);
3598 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3600 vc
->fd_read(vc
->opaque
, buf
, size
);
3605 #if defined(CONFIG_SLIRP)
3607 /* slirp network adapter */
3609 static int slirp_inited
;
3610 static VLANClientState
*slirp_vc
;
3612 int slirp_can_output(void)
3614 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
3617 void slirp_output(const uint8_t *pkt
, int pkt_len
)
3620 printf("slirp output:\n");
3621 hex_dump(stdout
, pkt
, pkt_len
);
3625 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
3628 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
3631 printf("slirp input:\n");
3632 hex_dump(stdout
, buf
, size
);
3634 slirp_input(buf
, size
);
3637 static int net_slirp_init(VLANState
*vlan
)
3639 if (!slirp_inited
) {
3643 slirp_vc
= qemu_new_vlan_client(vlan
,
3644 slirp_receive
, NULL
, NULL
);
3645 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
3649 static void net_slirp_redir(const char *redir_str
)
3654 struct in_addr guest_addr
;
3655 int host_port
, guest_port
;
3657 if (!slirp_inited
) {
3663 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3665 if (!strcmp(buf
, "tcp")) {
3667 } else if (!strcmp(buf
, "udp")) {
3673 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3675 host_port
= strtol(buf
, &r
, 0);
3679 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3681 if (buf
[0] == '\0') {
3682 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
3684 if (!inet_aton(buf
, &guest_addr
))
3687 guest_port
= strtol(p
, &r
, 0);
3691 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3692 fprintf(stderr
, "qemu: could not set up redirection\n");
3697 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3705 static void smb_exit(void)
3709 char filename
[1024];
3711 /* erase all the files in the directory */
3712 d
= opendir(smb_dir
);
3717 if (strcmp(de
->d_name
, ".") != 0 &&
3718 strcmp(de
->d_name
, "..") != 0) {
3719 snprintf(filename
, sizeof(filename
), "%s/%s",
3720 smb_dir
, de
->d_name
);
3728 /* automatic user mode samba server configuration */
3729 void net_slirp_smb(const char *exported_dir
)
3731 char smb_conf
[1024];
3732 char smb_cmdline
[1024];
3735 if (!slirp_inited
) {
3740 /* XXX: better tmp dir construction */
3741 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3742 if (mkdir(smb_dir
, 0700) < 0) {
3743 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3746 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3748 f
= fopen(smb_conf
, "w");
3750 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3757 "socket address=127.0.0.1\n"
3758 "pid directory=%s\n"
3759 "lock directory=%s\n"
3760 "log file=%s/log.smbd\n"
3761 "smb passwd file=%s/smbpasswd\n"
3762 "security = share\n"
3777 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3778 SMBD_COMMAND
, smb_conf
);
3780 slirp_add_exec(0, smb_cmdline
, 4, 139);
3783 #endif /* !defined(_WIN32) */
3785 #endif /* CONFIG_SLIRP */
3787 #if !defined(_WIN32)
3789 typedef struct TAPState
{
3790 VLANClientState
*vc
;
3794 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3796 TAPState
*s
= opaque
;
3799 ret
= write(s
->fd
, buf
, size
);
3800 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3807 static void tap_send(void *opaque
)
3809 TAPState
*s
= opaque
;
3816 sbuf
.maxlen
= sizeof(buf
);
3818 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
3820 size
= read(s
->fd
, buf
, sizeof(buf
));
3823 qemu_send_packet(s
->vc
, buf
, size
);
3829 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3833 s
= qemu_mallocz(sizeof(TAPState
));
3837 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3838 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3839 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3843 #if defined (_BSD) || defined (__FreeBSD_kernel__)
3844 static int tap_open(char *ifname
, int ifname_size
)
3850 TFR(fd
= open("/dev/tap", O_RDWR
));
3852 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3857 dev
= devname(s
.st_rdev
, S_IFCHR
);
3858 pstrcpy(ifname
, ifname_size
, dev
);
3860 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3863 #elif defined(__sun__)
3864 #define TUNNEWPPA (('T'<<16) | 0x0001)
3866 * Allocate TAP device, returns opened fd.
3867 * Stores dev name in the first arg(must be large enough).
3869 int tap_alloc(char *dev
)
3871 int tap_fd
, if_fd
, ppa
= -1;
3872 static int ip_fd
= 0;
3875 static int arp_fd
= 0;
3876 int ip_muxid
, arp_muxid
;
3877 struct strioctl strioc_if
, strioc_ppa
;
3878 int link_type
= I_PLINK
;;
3880 char actual_name
[32] = "";
3882 memset(&ifr
, 0x0, sizeof(ifr
));
3886 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
3890 /* Check if IP device was opened */
3894 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
3896 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
3900 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
3902 syslog(LOG_ERR
, "Can't open /dev/tap");
3906 /* Assign a new PPA and get its unit number. */
3907 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
3908 strioc_ppa
.ic_timout
= 0;
3909 strioc_ppa
.ic_len
= sizeof(ppa
);
3910 strioc_ppa
.ic_dp
= (char *)&ppa
;
3911 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
3912 syslog (LOG_ERR
, "Can't assign new interface");
3914 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
3916 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
3919 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
3920 syslog(LOG_ERR
, "Can't push IP module");
3924 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
3925 syslog(LOG_ERR
, "Can't get flags\n");
3927 snprintf (actual_name
, 32, "tap%d", ppa
);
3928 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3931 /* Assign ppa according to the unit number returned by tun device */
3933 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
3934 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
3935 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
3936 syslog (LOG_ERR
, "Can't get flags\n");
3937 /* Push arp module to if_fd */
3938 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
3939 syslog (LOG_ERR
, "Can't push ARP module (2)");
3941 /* Push arp module to ip_fd */
3942 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
3943 syslog (LOG_ERR
, "I_POP failed\n");
3944 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
3945 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
3947 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
3949 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
3951 /* Set ifname to arp */
3952 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
3953 strioc_if
.ic_timout
= 0;
3954 strioc_if
.ic_len
= sizeof(ifr
);
3955 strioc_if
.ic_dp
= (char *)&ifr
;
3956 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
3957 syslog (LOG_ERR
, "Can't set ifname to arp\n");
3960 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
3961 syslog(LOG_ERR
, "Can't link TAP device to IP");
3965 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
3966 syslog (LOG_ERR
, "Can't link TAP device to ARP");
3970 memset(&ifr
, 0x0, sizeof(ifr
));
3971 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3972 ifr
.lifr_ip_muxid
= ip_muxid
;
3973 ifr
.lifr_arp_muxid
= arp_muxid
;
3975 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
3977 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
3978 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
3979 syslog (LOG_ERR
, "Can't set multiplexor id");
3982 sprintf(dev
, "tap%d", ppa
);
3986 static int tap_open(char *ifname
, int ifname_size
)
3990 if( (fd
= tap_alloc(dev
)) < 0 ){
3991 fprintf(stderr
, "Cannot allocate TAP device\n");
3994 pstrcpy(ifname
, ifname_size
, dev
);
3995 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3999 static int tap_open(char *ifname
, int ifname_size
)
4004 TFR(fd
= open("/dev/net/tun", O_RDWR
));
4006 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
4009 memset(&ifr
, 0, sizeof(ifr
));
4010 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
4011 if (ifname
[0] != '\0')
4012 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
4014 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
4015 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
4017 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
4021 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
4022 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
4027 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
4028 const char *setup_script
)
4031 int pid
, status
, fd
;
4036 if (ifname1
!= NULL
)
4037 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
4040 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
4044 if (!setup_script
|| !strcmp(setup_script
, "no"))
4046 if (setup_script
[0] != '\0') {
4047 /* try to launch network init script */
4051 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
4052 for (i
= 0; i
< open_max
; i
++)
4053 if (i
!= STDIN_FILENO
&&
4054 i
!= STDOUT_FILENO
&&
4055 i
!= STDERR_FILENO
&&
4060 *parg
++ = (char *)setup_script
;
4063 execv(setup_script
, args
);
4066 while (waitpid(pid
, &status
, 0) != pid
);
4067 if (!WIFEXITED(status
) ||
4068 WEXITSTATUS(status
) != 0) {
4069 fprintf(stderr
, "%s: could not launch network script\n",
4075 s
= net_tap_fd_init(vlan
, fd
);
4078 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4079 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
4083 #endif /* !_WIN32 */
4085 /* network connection */
4086 typedef struct NetSocketState
{
4087 VLANClientState
*vc
;
4089 int state
; /* 0 = getting length, 1 = getting data */
4093 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
4096 typedef struct NetSocketListenState
{
4099 } NetSocketListenState
;
4101 /* XXX: we consider we can send the whole packet without blocking */
4102 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
4104 NetSocketState
*s
= opaque
;
4108 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
4109 send_all(s
->fd
, buf
, size
);
4112 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
4114 NetSocketState
*s
= opaque
;
4115 sendto(s
->fd
, buf
, size
, 0,
4116 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
4119 static void net_socket_send(void *opaque
)
4121 NetSocketState
*s
= opaque
;
4126 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
4128 err
= socket_error();
4129 if (err
!= EWOULDBLOCK
)
4131 } else if (size
== 0) {
4132 /* end of connection */
4134 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
4140 /* reassemble a packet from the network */
4146 memcpy(s
->buf
+ s
->index
, buf
, l
);
4150 if (s
->index
== 4) {
4152 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
4158 l
= s
->packet_len
- s
->index
;
4161 memcpy(s
->buf
+ s
->index
, buf
, l
);
4165 if (s
->index
>= s
->packet_len
) {
4166 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
4175 static void net_socket_send_dgram(void *opaque
)
4177 NetSocketState
*s
= opaque
;
4180 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
4184 /* end of connection */
4185 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
4188 qemu_send_packet(s
->vc
, s
->buf
, size
);
4191 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
4196 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
4197 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
4198 inet_ntoa(mcastaddr
->sin_addr
),
4199 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
4203 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
4205 perror("socket(PF_INET, SOCK_DGRAM)");
4210 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
4211 (const char *)&val
, sizeof(val
));
4213 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
4217 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
4223 /* Add host to multicast group */
4224 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
4225 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
4227 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
4228 (const char *)&imr
, sizeof(struct ip_mreq
));
4230 perror("setsockopt(IP_ADD_MEMBERSHIP)");
4234 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
4236 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
4237 (const char *)&val
, sizeof(val
));
4239 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
4243 socket_set_nonblock(fd
);
4251 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
4254 struct sockaddr_in saddr
;
4256 socklen_t saddr_len
;
4259 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
4260 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
4261 * by ONLY ONE process: we must "clone" this dgram socket --jjo
4265 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
4267 if (saddr
.sin_addr
.s_addr
==0) {
4268 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
4272 /* clone dgram socket */
4273 newfd
= net_socket_mcast_create(&saddr
);
4275 /* error already reported by net_socket_mcast_create() */
4279 /* clone newfd to fd, close newfd */
4284 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
4285 fd
, strerror(errno
));
4290 s
= qemu_mallocz(sizeof(NetSocketState
));
4295 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
4296 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
4298 /* mcast: save bound address as dst */
4299 if (is_connected
) s
->dgram_dst
=saddr
;
4301 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4302 "socket: fd=%d (%s mcast=%s:%d)",
4303 fd
, is_connected
? "cloned" : "",
4304 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4308 static void net_socket_connect(void *opaque
)
4310 NetSocketState
*s
= opaque
;
4311 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
4314 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
4318 s
= qemu_mallocz(sizeof(NetSocketState
));
4322 s
->vc
= qemu_new_vlan_client(vlan
,
4323 net_socket_receive
, NULL
, s
);
4324 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4325 "socket: fd=%d", fd
);
4327 net_socket_connect(s
);
4329 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
4334 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
4337 int so_type
=-1, optlen
=sizeof(so_type
);
4339 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
4340 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
4345 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
4347 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
4349 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
4350 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
4351 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
4356 static void net_socket_accept(void *opaque
)
4358 NetSocketListenState
*s
= opaque
;
4360 struct sockaddr_in saddr
;
4365 len
= sizeof(saddr
);
4366 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
4367 if (fd
< 0 && errno
!= EINTR
) {
4369 } else if (fd
>= 0) {
4373 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
4377 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
4378 "socket: connection from %s:%d",
4379 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4383 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
4385 NetSocketListenState
*s
;
4387 struct sockaddr_in saddr
;
4389 if (parse_host_port(&saddr
, host_str
) < 0)
4392 s
= qemu_mallocz(sizeof(NetSocketListenState
));
4396 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4401 socket_set_nonblock(fd
);
4403 /* allow fast reuse */
4405 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
4407 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4412 ret
= listen(fd
, 0);
4419 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
4423 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
4426 int fd
, connected
, ret
, err
;
4427 struct sockaddr_in saddr
;
4429 if (parse_host_port(&saddr
, host_str
) < 0)
4432 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4437 socket_set_nonblock(fd
);
4441 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4443 err
= socket_error();
4444 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
4445 } else if (err
== EINPROGRESS
) {
4448 } else if (err
== WSAEALREADY
) {
4461 s
= net_socket_fd_init(vlan
, fd
, connected
);
4464 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4465 "socket: connect to %s:%d",
4466 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4470 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
4474 struct sockaddr_in saddr
;
4476 if (parse_host_port(&saddr
, host_str
) < 0)
4480 fd
= net_socket_mcast_create(&saddr
);
4484 s
= net_socket_fd_init(vlan
, fd
, 0);
4488 s
->dgram_dst
= saddr
;
4490 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4491 "socket: mcast=%s:%d",
4492 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4497 static int get_param_value(char *buf
, int buf_size
,
4498 const char *tag
, const char *str
)
4507 while (*p
!= '\0' && *p
!= '=') {
4508 if ((q
- option
) < sizeof(option
) - 1)
4516 if (!strcmp(tag
, option
)) {
4518 while (*p
!= '\0' && *p
!= ',') {
4519 if ((q
- buf
) < buf_size
- 1)
4526 while (*p
!= '\0' && *p
!= ',') {
4537 static int net_client_init(const char *str
)
4548 while (*p
!= '\0' && *p
!= ',') {
4549 if ((q
- device
) < sizeof(device
) - 1)
4557 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
4558 vlan_id
= strtol(buf
, NULL
, 0);
4560 vlan
= qemu_find_vlan(vlan_id
);
4562 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
4565 if (!strcmp(device
, "nic")) {
4569 if (nb_nics
>= MAX_NICS
) {
4570 fprintf(stderr
, "Too Many NICs\n");
4573 nd
= &nd_table
[nb_nics
];
4574 macaddr
= nd
->macaddr
;
4580 macaddr
[5] = 0x56 + nb_nics
;
4582 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
4583 if (parse_macaddr(macaddr
, buf
) < 0) {
4584 fprintf(stderr
, "invalid syntax for ethernet address\n");
4588 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
4589 nd
->model
= strdup(buf
);
4593 vlan
->nb_guest_devs
++;
4596 if (!strcmp(device
, "none")) {
4597 /* does nothing. It is needed to signal that no network cards
4602 if (!strcmp(device
, "user")) {
4603 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
4604 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
4606 vlan
->nb_host_devs
++;
4607 ret
= net_slirp_init(vlan
);
4611 if (!strcmp(device
, "tap")) {
4613 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4614 fprintf(stderr
, "tap: no interface name\n");
4617 vlan
->nb_host_devs
++;
4618 ret
= tap_win32_init(vlan
, ifname
);
4621 if (!strcmp(device
, "tap")) {
4623 char setup_script
[1024];
4625 vlan
->nb_host_devs
++;
4626 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4627 fd
= strtol(buf
, NULL
, 0);
4629 if (net_tap_fd_init(vlan
, fd
))
4632 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4635 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
4636 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
4638 ret
= net_tap_init(vlan
, ifname
, setup_script
);
4642 if (!strcmp(device
, "socket")) {
4643 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4645 fd
= strtol(buf
, NULL
, 0);
4647 if (net_socket_fd_init(vlan
, fd
, 1))
4649 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
4650 ret
= net_socket_listen_init(vlan
, buf
);
4651 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
4652 ret
= net_socket_connect_init(vlan
, buf
);
4653 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
4654 ret
= net_socket_mcast_init(vlan
, buf
);
4656 fprintf(stderr
, "Unknown socket options: %s\n", p
);
4659 vlan
->nb_host_devs
++;
4662 fprintf(stderr
, "Unknown network device: %s\n", device
);
4666 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
4672 void do_info_network(void)
4675 VLANClientState
*vc
;
4677 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
4678 term_printf("VLAN %d devices:\n", vlan
->id
);
4679 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
4680 term_printf(" %s\n", vc
->info_str
);
4684 /***********************************************************/
4687 static USBPort
*used_usb_ports
;
4688 static USBPort
*free_usb_ports
;
4690 /* ??? Maybe change this to register a hub to keep track of the topology. */
4691 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
4692 usb_attachfn attach
)
4694 port
->opaque
= opaque
;
4695 port
->index
= index
;
4696 port
->attach
= attach
;
4697 port
->next
= free_usb_ports
;
4698 free_usb_ports
= port
;
4701 static int usb_device_add(const char *devname
)
4707 if (!free_usb_ports
)
4710 if (strstart(devname
, "host:", &p
)) {
4711 dev
= usb_host_device_open(p
);
4712 } else if (!strcmp(devname
, "mouse")) {
4713 dev
= usb_mouse_init();
4714 } else if (!strcmp(devname
, "tablet")) {
4715 dev
= usb_tablet_init();
4716 } else if (!strcmp(devname
, "keyboard")) {
4717 dev
= usb_keyboard_init();
4718 } else if (strstart(devname
, "disk:", &p
)) {
4719 dev
= usb_msd_init(p
);
4720 } else if (!strcmp(devname
, "wacom-tablet")) {
4721 dev
= usb_wacom_init();
4728 /* Find a USB port to add the device to. */
4729 port
= free_usb_ports
;
4733 /* Create a new hub and chain it on. */
4734 free_usb_ports
= NULL
;
4735 port
->next
= used_usb_ports
;
4736 used_usb_ports
= port
;
4738 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
4739 usb_attach(port
, hub
);
4740 port
= free_usb_ports
;
4743 free_usb_ports
= port
->next
;
4744 port
->next
= used_usb_ports
;
4745 used_usb_ports
= port
;
4746 usb_attach(port
, dev
);
4750 static int usb_device_del(const char *devname
)
4758 if (!used_usb_ports
)
4761 p
= strchr(devname
, '.');
4764 bus_num
= strtoul(devname
, NULL
, 0);
4765 addr
= strtoul(p
+ 1, NULL
, 0);
4769 lastp
= &used_usb_ports
;
4770 port
= used_usb_ports
;
4771 while (port
&& port
->dev
->addr
!= addr
) {
4772 lastp
= &port
->next
;
4780 *lastp
= port
->next
;
4781 usb_attach(port
, NULL
);
4782 dev
->handle_destroy(dev
);
4783 port
->next
= free_usb_ports
;
4784 free_usb_ports
= port
;
4788 void do_usb_add(const char *devname
)
4791 ret
= usb_device_add(devname
);
4793 term_printf("Could not add USB device '%s'\n", devname
);
4796 void do_usb_del(const char *devname
)
4799 ret
= usb_device_del(devname
);
4801 term_printf("Could not remove USB device '%s'\n", devname
);
4808 const char *speed_str
;
4811 term_printf("USB support not enabled\n");
4815 for (port
= used_usb_ports
; port
; port
= port
->next
) {
4819 switch(dev
->speed
) {
4823 case USB_SPEED_FULL
:
4826 case USB_SPEED_HIGH
:
4833 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
4834 0, dev
->addr
, speed_str
, dev
->devname
);
4838 /***********************************************************/
4839 /* PCMCIA/Cardbus */
4841 static struct pcmcia_socket_entry_s
{
4842 struct pcmcia_socket_s
*socket
;
4843 struct pcmcia_socket_entry_s
*next
;
4844 } *pcmcia_sockets
= 0;
4846 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
4848 struct pcmcia_socket_entry_s
*entry
;
4850 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
4851 entry
->socket
= socket
;
4852 entry
->next
= pcmcia_sockets
;
4853 pcmcia_sockets
= entry
;
4856 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
4858 struct pcmcia_socket_entry_s
*entry
, **ptr
;
4860 ptr
= &pcmcia_sockets
;
4861 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
4862 if (entry
->socket
== socket
) {
4868 void pcmcia_info(void)
4870 struct pcmcia_socket_entry_s
*iter
;
4871 if (!pcmcia_sockets
)
4872 term_printf("No PCMCIA sockets\n");
4874 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
4875 term_printf("%s: %s\n", iter
->socket
->slot_string
,
4876 iter
->socket
->attached
? iter
->socket
->card_string
:
4880 /***********************************************************/
4883 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4887 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4891 static void dumb_refresh(DisplayState
*ds
)
4893 #if defined(CONFIG_SDL)
4898 static void dumb_display_init(DisplayState
*ds
)
4903 ds
->dpy_update
= dumb_update
;
4904 ds
->dpy_resize
= dumb_resize
;
4905 ds
->dpy_refresh
= dumb_refresh
;
4908 /***********************************************************/
4911 #define MAX_IO_HANDLERS 64
4913 typedef struct IOHandlerRecord
{
4915 IOCanRWHandler
*fd_read_poll
;
4917 IOHandler
*fd_write
;
4920 /* temporary data */
4922 struct IOHandlerRecord
*next
;
4925 static IOHandlerRecord
*first_io_handler
;
4927 /* XXX: fd_read_poll should be suppressed, but an API change is
4928 necessary in the character devices to suppress fd_can_read(). */
4929 int qemu_set_fd_handler2(int fd
,
4930 IOCanRWHandler
*fd_read_poll
,
4932 IOHandler
*fd_write
,
4935 IOHandlerRecord
**pioh
, *ioh
;
4937 if (!fd_read
&& !fd_write
) {
4938 pioh
= &first_io_handler
;
4943 if (ioh
->fd
== fd
) {
4950 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4954 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4957 ioh
->next
= first_io_handler
;
4958 first_io_handler
= ioh
;
4961 ioh
->fd_read_poll
= fd_read_poll
;
4962 ioh
->fd_read
= fd_read
;
4963 ioh
->fd_write
= fd_write
;
4964 ioh
->opaque
= opaque
;
4970 int qemu_set_fd_handler(int fd
,
4972 IOHandler
*fd_write
,
4975 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4978 /***********************************************************/
4979 /* Polling handling */
4981 typedef struct PollingEntry
{
4984 struct PollingEntry
*next
;
4987 static PollingEntry
*first_polling_entry
;
4989 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4991 PollingEntry
**ppe
, *pe
;
4992 pe
= qemu_mallocz(sizeof(PollingEntry
));
4996 pe
->opaque
= opaque
;
4997 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
5002 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
5004 PollingEntry
**ppe
, *pe
;
5005 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
5007 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
5016 /***********************************************************/
5017 /* Wait objects support */
5018 typedef struct WaitObjects
{
5020 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
5021 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
5022 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
5025 static WaitObjects wait_objects
= {0};
5027 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
5029 WaitObjects
*w
= &wait_objects
;
5031 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
5033 w
->events
[w
->num
] = handle
;
5034 w
->func
[w
->num
] = func
;
5035 w
->opaque
[w
->num
] = opaque
;
5040 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
5043 WaitObjects
*w
= &wait_objects
;
5046 for (i
= 0; i
< w
->num
; i
++) {
5047 if (w
->events
[i
] == handle
)
5050 w
->events
[i
] = w
->events
[i
+ 1];
5051 w
->func
[i
] = w
->func
[i
+ 1];
5052 w
->opaque
[i
] = w
->opaque
[i
+ 1];
5060 /***********************************************************/
5061 /* savevm/loadvm support */
5063 #define IO_BUF_SIZE 32768
5067 BlockDriverState
*bs
;
5070 int64_t base_offset
;
5071 int64_t buf_offset
; /* start of buffer when writing, end of buffer
5074 int buf_size
; /* 0 when writing */
5075 uint8_t buf
[IO_BUF_SIZE
];
5078 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
5082 f
= qemu_mallocz(sizeof(QEMUFile
));
5085 if (!strcmp(mode
, "wb")) {
5087 } else if (!strcmp(mode
, "rb")) {
5092 f
->outfile
= fopen(filename
, mode
);
5104 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
5108 f
= qemu_mallocz(sizeof(QEMUFile
));
5113 f
->is_writable
= is_writable
;
5114 f
->base_offset
= offset
;
5118 void qemu_fflush(QEMUFile
*f
)
5120 if (!f
->is_writable
)
5122 if (f
->buf_index
> 0) {
5124 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
5125 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
5127 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
5128 f
->buf
, f
->buf_index
);
5130 f
->buf_offset
+= f
->buf_index
;
5135 static void qemu_fill_buffer(QEMUFile
*f
)
5142 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
5143 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
5147 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
5148 f
->buf
, IO_BUF_SIZE
);
5154 f
->buf_offset
+= len
;
5157 void qemu_fclose(QEMUFile
*f
)
5167 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
5171 l
= IO_BUF_SIZE
- f
->buf_index
;
5174 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
5178 if (f
->buf_index
>= IO_BUF_SIZE
)
5183 void qemu_put_byte(QEMUFile
*f
, int v
)
5185 f
->buf
[f
->buf_index
++] = v
;
5186 if (f
->buf_index
>= IO_BUF_SIZE
)
5190 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
5196 l
= f
->buf_size
- f
->buf_index
;
5198 qemu_fill_buffer(f
);
5199 l
= f
->buf_size
- f
->buf_index
;
5205 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
5210 return size1
- size
;
5213 int qemu_get_byte(QEMUFile
*f
)
5215 if (f
->buf_index
>= f
->buf_size
) {
5216 qemu_fill_buffer(f
);
5217 if (f
->buf_index
>= f
->buf_size
)
5220 return f
->buf
[f
->buf_index
++];
5223 int64_t qemu_ftell(QEMUFile
*f
)
5225 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
5228 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
5230 if (whence
== SEEK_SET
) {
5232 } else if (whence
== SEEK_CUR
) {
5233 pos
+= qemu_ftell(f
);
5235 /* SEEK_END not supported */
5238 if (f
->is_writable
) {
5240 f
->buf_offset
= pos
;
5242 f
->buf_offset
= pos
;
5249 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
5251 qemu_put_byte(f
, v
>> 8);
5252 qemu_put_byte(f
, v
);
5255 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
5257 qemu_put_byte(f
, v
>> 24);
5258 qemu_put_byte(f
, v
>> 16);
5259 qemu_put_byte(f
, v
>> 8);
5260 qemu_put_byte(f
, v
);
5263 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
5265 qemu_put_be32(f
, v
>> 32);
5266 qemu_put_be32(f
, v
);
5269 unsigned int qemu_get_be16(QEMUFile
*f
)
5272 v
= qemu_get_byte(f
) << 8;
5273 v
|= qemu_get_byte(f
);
5277 unsigned int qemu_get_be32(QEMUFile
*f
)
5280 v
= qemu_get_byte(f
) << 24;
5281 v
|= qemu_get_byte(f
) << 16;
5282 v
|= qemu_get_byte(f
) << 8;
5283 v
|= qemu_get_byte(f
);
5287 uint64_t qemu_get_be64(QEMUFile
*f
)
5290 v
= (uint64_t)qemu_get_be32(f
) << 32;
5291 v
|= qemu_get_be32(f
);
5295 typedef struct SaveStateEntry
{
5299 SaveStateHandler
*save_state
;
5300 LoadStateHandler
*load_state
;
5302 struct SaveStateEntry
*next
;
5305 static SaveStateEntry
*first_se
;
5307 int register_savevm(const char *idstr
,
5310 SaveStateHandler
*save_state
,
5311 LoadStateHandler
*load_state
,
5314 SaveStateEntry
*se
, **pse
;
5316 se
= qemu_malloc(sizeof(SaveStateEntry
));
5319 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
5320 se
->instance_id
= instance_id
;
5321 se
->version_id
= version_id
;
5322 se
->save_state
= save_state
;
5323 se
->load_state
= load_state
;
5324 se
->opaque
= opaque
;
5327 /* add at the end of list */
5329 while (*pse
!= NULL
)
5330 pse
= &(*pse
)->next
;
5335 #define QEMU_VM_FILE_MAGIC 0x5145564d
5336 #define QEMU_VM_FILE_VERSION 0x00000002
5338 int qemu_savevm_state(QEMUFile
*f
)
5342 int64_t cur_pos
, len_pos
, total_len_pos
;
5344 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
5345 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
5346 total_len_pos
= qemu_ftell(f
);
5347 qemu_put_be64(f
, 0); /* total size */
5349 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5351 len
= strlen(se
->idstr
);
5352 qemu_put_byte(f
, len
);
5353 qemu_put_buffer(f
, se
->idstr
, len
);
5355 qemu_put_be32(f
, se
->instance_id
);
5356 qemu_put_be32(f
, se
->version_id
);
5358 /* record size: filled later */
5359 len_pos
= qemu_ftell(f
);
5360 qemu_put_be32(f
, 0);
5362 se
->save_state(f
, se
->opaque
);
5364 /* fill record size */
5365 cur_pos
= qemu_ftell(f
);
5366 len
= cur_pos
- len_pos
- 4;
5367 qemu_fseek(f
, len_pos
, SEEK_SET
);
5368 qemu_put_be32(f
, len
);
5369 qemu_fseek(f
, cur_pos
, SEEK_SET
);
5371 cur_pos
= qemu_ftell(f
);
5372 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
5373 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
5374 qemu_fseek(f
, cur_pos
, SEEK_SET
);
5380 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
5384 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5385 if (!strcmp(se
->idstr
, idstr
) &&
5386 instance_id
== se
->instance_id
)
5392 int qemu_loadvm_state(QEMUFile
*f
)
5395 int len
, ret
, instance_id
, record_len
, version_id
;
5396 int64_t total_len
, end_pos
, cur_pos
;
5400 v
= qemu_get_be32(f
);
5401 if (v
!= QEMU_VM_FILE_MAGIC
)
5403 v
= qemu_get_be32(f
);
5404 if (v
!= QEMU_VM_FILE_VERSION
) {
5409 total_len
= qemu_get_be64(f
);
5410 end_pos
= total_len
+ qemu_ftell(f
);
5412 if (qemu_ftell(f
) >= end_pos
)
5414 len
= qemu_get_byte(f
);
5415 qemu_get_buffer(f
, idstr
, len
);
5417 instance_id
= qemu_get_be32(f
);
5418 version_id
= qemu_get_be32(f
);
5419 record_len
= qemu_get_be32(f
);
5421 printf("idstr=%s instance=0x%x version=%d len=%d\n",
5422 idstr
, instance_id
, version_id
, record_len
);
5424 cur_pos
= qemu_ftell(f
);
5425 se
= find_se(idstr
, instance_id
);
5427 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
5428 instance_id
, idstr
);
5430 ret
= se
->load_state(f
, se
->opaque
, version_id
);
5432 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
5433 instance_id
, idstr
);
5436 /* always seek to exact end of record */
5437 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
5444 /* device can contain snapshots */
5445 static int bdrv_can_snapshot(BlockDriverState
*bs
)
5448 !bdrv_is_removable(bs
) &&
5449 !bdrv_is_read_only(bs
));
5452 /* device must be snapshots in order to have a reliable snapshot */
5453 static int bdrv_has_snapshot(BlockDriverState
*bs
)
5456 !bdrv_is_removable(bs
) &&
5457 !bdrv_is_read_only(bs
));
5460 static BlockDriverState
*get_bs_snapshots(void)
5462 BlockDriverState
*bs
;
5466 return bs_snapshots
;
5467 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5469 if (bdrv_can_snapshot(bs
))
5478 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
5481 QEMUSnapshotInfo
*sn_tab
, *sn
;
5485 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5488 for(i
= 0; i
< nb_sns
; i
++) {
5490 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
5500 void do_savevm(const char *name
)
5502 BlockDriverState
*bs
, *bs1
;
5503 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
5504 int must_delete
, ret
, i
;
5505 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5507 int saved_vm_running
;
5514 bs
= get_bs_snapshots();
5516 term_printf("No block device can accept snapshots\n");
5520 /* ??? Should this occur after vm_stop? */
5523 saved_vm_running
= vm_running
;
5528 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
5533 memset(sn
, 0, sizeof(*sn
));
5535 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
5536 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
5539 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
5542 /* fill auxiliary fields */
5545 sn
->date_sec
= tb
.time
;
5546 sn
->date_nsec
= tb
.millitm
* 1000000;
5548 gettimeofday(&tv
, NULL
);
5549 sn
->date_sec
= tv
.tv_sec
;
5550 sn
->date_nsec
= tv
.tv_usec
* 1000;
5552 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
5554 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5555 term_printf("Device %s does not support VM state snapshots\n",
5556 bdrv_get_device_name(bs
));
5560 /* save the VM state */
5561 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
5563 term_printf("Could not open VM state file\n");
5566 ret
= qemu_savevm_state(f
);
5567 sn
->vm_state_size
= qemu_ftell(f
);
5570 term_printf("Error %d while writing VM\n", ret
);
5574 /* create the snapshots */
5576 for(i
= 0; i
< MAX_DISKS
; i
++) {
5578 if (bdrv_has_snapshot(bs1
)) {
5580 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
5582 term_printf("Error while deleting snapshot on '%s'\n",
5583 bdrv_get_device_name(bs1
));
5586 ret
= bdrv_snapshot_create(bs1
, sn
);
5588 term_printf("Error while creating snapshot on '%s'\n",
5589 bdrv_get_device_name(bs1
));
5595 if (saved_vm_running
)
5599 void do_loadvm(const char *name
)
5601 BlockDriverState
*bs
, *bs1
;
5602 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5605 int saved_vm_running
;
5607 bs
= get_bs_snapshots();
5609 term_printf("No block device supports snapshots\n");
5613 /* Flush all IO requests so they don't interfere with the new state. */
5616 saved_vm_running
= vm_running
;
5619 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5621 if (bdrv_has_snapshot(bs1
)) {
5622 ret
= bdrv_snapshot_goto(bs1
, name
);
5625 term_printf("Warning: ");
5628 term_printf("Snapshots not supported on device '%s'\n",
5629 bdrv_get_device_name(bs1
));
5632 term_printf("Could not find snapshot '%s' on device '%s'\n",
5633 name
, bdrv_get_device_name(bs1
));
5636 term_printf("Error %d while activating snapshot on '%s'\n",
5637 ret
, bdrv_get_device_name(bs1
));
5640 /* fatal on snapshot block device */
5647 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5648 term_printf("Device %s does not support VM state snapshots\n",
5649 bdrv_get_device_name(bs
));
5653 /* restore the VM state */
5654 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
5656 term_printf("Could not open VM state file\n");
5659 ret
= qemu_loadvm_state(f
);
5662 term_printf("Error %d while loading VM state\n", ret
);
5665 if (saved_vm_running
)
5669 void do_delvm(const char *name
)
5671 BlockDriverState
*bs
, *bs1
;
5674 bs
= get_bs_snapshots();
5676 term_printf("No block device supports snapshots\n");
5680 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5682 if (bdrv_has_snapshot(bs1
)) {
5683 ret
= bdrv_snapshot_delete(bs1
, name
);
5685 if (ret
== -ENOTSUP
)
5686 term_printf("Snapshots not supported on device '%s'\n",
5687 bdrv_get_device_name(bs1
));
5689 term_printf("Error %d while deleting snapshot on '%s'\n",
5690 ret
, bdrv_get_device_name(bs1
));
5696 void do_info_snapshots(void)
5698 BlockDriverState
*bs
, *bs1
;
5699 QEMUSnapshotInfo
*sn_tab
, *sn
;
5703 bs
= get_bs_snapshots();
5705 term_printf("No available block device supports snapshots\n");
5708 term_printf("Snapshot devices:");
5709 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5711 if (bdrv_has_snapshot(bs1
)) {
5713 term_printf(" %s", bdrv_get_device_name(bs1
));
5718 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5720 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
5723 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
5724 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
5725 for(i
= 0; i
< nb_sns
; i
++) {
5727 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
5732 /***********************************************************/
5733 /* cpu save/restore */
5735 #if defined(TARGET_I386)
5737 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
5739 qemu_put_be32(f
, dt
->selector
);
5740 qemu_put_betl(f
, dt
->base
);
5741 qemu_put_be32(f
, dt
->limit
);
5742 qemu_put_be32(f
, dt
->flags
);
5745 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
5747 dt
->selector
= qemu_get_be32(f
);
5748 dt
->base
= qemu_get_betl(f
);
5749 dt
->limit
= qemu_get_be32(f
);
5750 dt
->flags
= qemu_get_be32(f
);
5753 void cpu_save(QEMUFile
*f
, void *opaque
)
5755 CPUState
*env
= opaque
;
5756 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
5760 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5761 qemu_put_betls(f
, &env
->regs
[i
]);
5762 qemu_put_betls(f
, &env
->eip
);
5763 qemu_put_betls(f
, &env
->eflags
);
5764 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
5765 qemu_put_be32s(f
, &hflags
);
5769 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
5771 for(i
= 0; i
< 8; i
++) {
5772 fptag
|= ((!env
->fptags
[i
]) << i
);
5775 qemu_put_be16s(f
, &fpuc
);
5776 qemu_put_be16s(f
, &fpus
);
5777 qemu_put_be16s(f
, &fptag
);
5779 #ifdef USE_X86LDOUBLE
5784 qemu_put_be16s(f
, &fpregs_format
);
5786 for(i
= 0; i
< 8; i
++) {
5787 #ifdef USE_X86LDOUBLE
5791 /* we save the real CPU data (in case of MMX usage only 'mant'
5792 contains the MMX register */
5793 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
5794 qemu_put_be64(f
, mant
);
5795 qemu_put_be16(f
, exp
);
5798 /* if we use doubles for float emulation, we save the doubles to
5799 avoid losing information in case of MMX usage. It can give
5800 problems if the image is restored on a CPU where long
5801 doubles are used instead. */
5802 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
5806 for(i
= 0; i
< 6; i
++)
5807 cpu_put_seg(f
, &env
->segs
[i
]);
5808 cpu_put_seg(f
, &env
->ldt
);
5809 cpu_put_seg(f
, &env
->tr
);
5810 cpu_put_seg(f
, &env
->gdt
);
5811 cpu_put_seg(f
, &env
->idt
);
5813 qemu_put_be32s(f
, &env
->sysenter_cs
);
5814 qemu_put_be32s(f
, &env
->sysenter_esp
);
5815 qemu_put_be32s(f
, &env
->sysenter_eip
);
5817 qemu_put_betls(f
, &env
->cr
[0]);
5818 qemu_put_betls(f
, &env
->cr
[2]);
5819 qemu_put_betls(f
, &env
->cr
[3]);
5820 qemu_put_betls(f
, &env
->cr
[4]);
5822 for(i
= 0; i
< 8; i
++)
5823 qemu_put_betls(f
, &env
->dr
[i
]);
5826 qemu_put_be32s(f
, &env
->a20_mask
);
5829 qemu_put_be32s(f
, &env
->mxcsr
);
5830 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5831 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5832 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5835 #ifdef TARGET_X86_64
5836 qemu_put_be64s(f
, &env
->efer
);
5837 qemu_put_be64s(f
, &env
->star
);
5838 qemu_put_be64s(f
, &env
->lstar
);
5839 qemu_put_be64s(f
, &env
->cstar
);
5840 qemu_put_be64s(f
, &env
->fmask
);
5841 qemu_put_be64s(f
, &env
->kernelgsbase
);
5843 qemu_put_be32s(f
, &env
->smbase
);
5846 #ifdef USE_X86LDOUBLE
5847 /* XXX: add that in a FPU generic layer */
5848 union x86_longdouble
{
5853 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
5854 #define EXPBIAS1 1023
5855 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
5856 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
5858 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
5862 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
5863 /* exponent + sign */
5864 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
5865 e
|= SIGND1(temp
) >> 16;
5870 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5872 CPUState
*env
= opaque
;
5875 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
5877 if (version_id
!= 3 && version_id
!= 4)
5879 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5880 qemu_get_betls(f
, &env
->regs
[i
]);
5881 qemu_get_betls(f
, &env
->eip
);
5882 qemu_get_betls(f
, &env
->eflags
);
5883 qemu_get_be32s(f
, &hflags
);
5885 qemu_get_be16s(f
, &fpuc
);
5886 qemu_get_be16s(f
, &fpus
);
5887 qemu_get_be16s(f
, &fptag
);
5888 qemu_get_be16s(f
, &fpregs_format
);
5890 /* NOTE: we cannot always restore the FPU state if the image come
5891 from a host with a different 'USE_X86LDOUBLE' define. We guess
5892 if we are in an MMX state to restore correctly in that case. */
5893 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
5894 for(i
= 0; i
< 8; i
++) {
5898 switch(fpregs_format
) {
5900 mant
= qemu_get_be64(f
);
5901 exp
= qemu_get_be16(f
);
5902 #ifdef USE_X86LDOUBLE
5903 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5905 /* difficult case */
5907 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5909 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5913 mant
= qemu_get_be64(f
);
5914 #ifdef USE_X86LDOUBLE
5916 union x86_longdouble
*p
;
5917 /* difficult case */
5918 p
= (void *)&env
->fpregs
[i
];
5923 fp64_to_fp80(p
, mant
);
5927 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5936 /* XXX: restore FPU round state */
5937 env
->fpstt
= (fpus
>> 11) & 7;
5938 env
->fpus
= fpus
& ~0x3800;
5940 for(i
= 0; i
< 8; i
++) {
5941 env
->fptags
[i
] = (fptag
>> i
) & 1;
5944 for(i
= 0; i
< 6; i
++)
5945 cpu_get_seg(f
, &env
->segs
[i
]);
5946 cpu_get_seg(f
, &env
->ldt
);
5947 cpu_get_seg(f
, &env
->tr
);
5948 cpu_get_seg(f
, &env
->gdt
);
5949 cpu_get_seg(f
, &env
->idt
);
5951 qemu_get_be32s(f
, &env
->sysenter_cs
);
5952 qemu_get_be32s(f
, &env
->sysenter_esp
);
5953 qemu_get_be32s(f
, &env
->sysenter_eip
);
5955 qemu_get_betls(f
, &env
->cr
[0]);
5956 qemu_get_betls(f
, &env
->cr
[2]);
5957 qemu_get_betls(f
, &env
->cr
[3]);
5958 qemu_get_betls(f
, &env
->cr
[4]);
5960 for(i
= 0; i
< 8; i
++)
5961 qemu_get_betls(f
, &env
->dr
[i
]);
5964 qemu_get_be32s(f
, &env
->a20_mask
);
5966 qemu_get_be32s(f
, &env
->mxcsr
);
5967 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5968 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5969 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5972 #ifdef TARGET_X86_64
5973 qemu_get_be64s(f
, &env
->efer
);
5974 qemu_get_be64s(f
, &env
->star
);
5975 qemu_get_be64s(f
, &env
->lstar
);
5976 qemu_get_be64s(f
, &env
->cstar
);
5977 qemu_get_be64s(f
, &env
->fmask
);
5978 qemu_get_be64s(f
, &env
->kernelgsbase
);
5980 if (version_id
>= 4)
5981 qemu_get_be32s(f
, &env
->smbase
);
5983 /* XXX: compute hflags from scratch, except for CPL and IIF */
5984 env
->hflags
= hflags
;
5989 #elif defined(TARGET_PPC)
5990 void cpu_save(QEMUFile
*f
, void *opaque
)
5994 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5999 #elif defined(TARGET_MIPS)
6000 void cpu_save(QEMUFile
*f
, void *opaque
)
6004 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6009 #elif defined(TARGET_SPARC)
6010 void cpu_save(QEMUFile
*f
, void *opaque
)
6012 CPUState
*env
= opaque
;
6016 for(i
= 0; i
< 8; i
++)
6017 qemu_put_betls(f
, &env
->gregs
[i
]);
6018 for(i
= 0; i
< NWINDOWS
* 16; i
++)
6019 qemu_put_betls(f
, &env
->regbase
[i
]);
6022 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
6028 qemu_put_be32(f
, u
.i
);
6031 qemu_put_betls(f
, &env
->pc
);
6032 qemu_put_betls(f
, &env
->npc
);
6033 qemu_put_betls(f
, &env
->y
);
6035 qemu_put_be32(f
, tmp
);
6036 qemu_put_betls(f
, &env
->fsr
);
6037 qemu_put_betls(f
, &env
->tbr
);
6038 #ifndef TARGET_SPARC64
6039 qemu_put_be32s(f
, &env
->wim
);
6041 for(i
= 0; i
< 16; i
++)
6042 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
6046 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6048 CPUState
*env
= opaque
;
6052 for(i
= 0; i
< 8; i
++)
6053 qemu_get_betls(f
, &env
->gregs
[i
]);
6054 for(i
= 0; i
< NWINDOWS
* 16; i
++)
6055 qemu_get_betls(f
, &env
->regbase
[i
]);
6058 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
6063 u
.i
= qemu_get_be32(f
);
6067 qemu_get_betls(f
, &env
->pc
);
6068 qemu_get_betls(f
, &env
->npc
);
6069 qemu_get_betls(f
, &env
->y
);
6070 tmp
= qemu_get_be32(f
);
6071 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
6072 correctly updated */
6074 qemu_get_betls(f
, &env
->fsr
);
6075 qemu_get_betls(f
, &env
->tbr
);
6076 #ifndef TARGET_SPARC64
6077 qemu_get_be32s(f
, &env
->wim
);
6079 for(i
= 0; i
< 16; i
++)
6080 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
6086 #elif defined(TARGET_ARM)
6088 void cpu_save(QEMUFile
*f
, void *opaque
)
6091 CPUARMState
*env
= (CPUARMState
*)opaque
;
6093 for (i
= 0; i
< 16; i
++) {
6094 qemu_put_be32(f
, env
->regs
[i
]);
6096 qemu_put_be32(f
, cpsr_read(env
));
6097 qemu_put_be32(f
, env
->spsr
);
6098 for (i
= 0; i
< 6; i
++) {
6099 qemu_put_be32(f
, env
->banked_spsr
[i
]);
6100 qemu_put_be32(f
, env
->banked_r13
[i
]);
6101 qemu_put_be32(f
, env
->banked_r14
[i
]);
6103 for (i
= 0; i
< 5; i
++) {
6104 qemu_put_be32(f
, env
->usr_regs
[i
]);
6105 qemu_put_be32(f
, env
->fiq_regs
[i
]);
6107 qemu_put_be32(f
, env
->cp15
.c0_cpuid
);
6108 qemu_put_be32(f
, env
->cp15
.c0_cachetype
);
6109 qemu_put_be32(f
, env
->cp15
.c1_sys
);
6110 qemu_put_be32(f
, env
->cp15
.c1_coproc
);
6111 qemu_put_be32(f
, env
->cp15
.c1_xscaleauxcr
);
6112 qemu_put_be32(f
, env
->cp15
.c2_base
);
6113 qemu_put_be32(f
, env
->cp15
.c2_data
);
6114 qemu_put_be32(f
, env
->cp15
.c2_insn
);
6115 qemu_put_be32(f
, env
->cp15
.c3
);
6116 qemu_put_be32(f
, env
->cp15
.c5_insn
);
6117 qemu_put_be32(f
, env
->cp15
.c5_data
);
6118 for (i
= 0; i
< 8; i
++) {
6119 qemu_put_be32(f
, env
->cp15
.c6_region
[i
]);
6121 qemu_put_be32(f
, env
->cp15
.c6_insn
);
6122 qemu_put_be32(f
, env
->cp15
.c6_data
);
6123 qemu_put_be32(f
, env
->cp15
.c9_insn
);
6124 qemu_put_be32(f
, env
->cp15
.c9_data
);
6125 qemu_put_be32(f
, env
->cp15
.c13_fcse
);
6126 qemu_put_be32(f
, env
->cp15
.c13_context
);
6127 qemu_put_be32(f
, env
->cp15
.c15_cpar
);
6129 qemu_put_be32(f
, env
->features
);
6131 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
6132 for (i
= 0; i
< 16; i
++) {
6134 u
.d
= env
->vfp
.regs
[i
];
6135 qemu_put_be32(f
, u
.l
.upper
);
6136 qemu_put_be32(f
, u
.l
.lower
);
6138 for (i
= 0; i
< 16; i
++) {
6139 qemu_put_be32(f
, env
->vfp
.xregs
[i
]);
6142 /* TODO: Should use proper FPSCR access functions. */
6143 qemu_put_be32(f
, env
->vfp
.vec_len
);
6144 qemu_put_be32(f
, env
->vfp
.vec_stride
);
6147 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
6148 for (i
= 0; i
< 16; i
++) {
6149 qemu_put_be64(f
, env
->iwmmxt
.regs
[i
]);
6151 for (i
= 0; i
< 16; i
++) {
6152 qemu_put_be32(f
, env
->iwmmxt
.cregs
[i
]);
6157 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6159 CPUARMState
*env
= (CPUARMState
*)opaque
;
6162 if (version_id
!= 0)
6165 for (i
= 0; i
< 16; i
++) {
6166 env
->regs
[i
] = qemu_get_be32(f
);
6168 cpsr_write(env
, qemu_get_be32(f
), 0xffffffff);
6169 env
->spsr
= qemu_get_be32(f
);
6170 for (i
= 0; i
< 6; i
++) {
6171 env
->banked_spsr
[i
] = qemu_get_be32(f
);
6172 env
->banked_r13
[i
] = qemu_get_be32(f
);
6173 env
->banked_r14
[i
] = qemu_get_be32(f
);
6175 for (i
= 0; i
< 5; i
++) {
6176 env
->usr_regs
[i
] = qemu_get_be32(f
);
6177 env
->fiq_regs
[i
] = qemu_get_be32(f
);
6179 env
->cp15
.c0_cpuid
= qemu_get_be32(f
);
6180 env
->cp15
.c0_cachetype
= qemu_get_be32(f
);
6181 env
->cp15
.c1_sys
= qemu_get_be32(f
);
6182 env
->cp15
.c1_coproc
= qemu_get_be32(f
);
6183 env
->cp15
.c1_xscaleauxcr
= qemu_get_be32(f
);
6184 env
->cp15
.c2_base
= qemu_get_be32(f
);
6185 env
->cp15
.c2_data
= qemu_get_be32(f
);
6186 env
->cp15
.c2_insn
= qemu_get_be32(f
);
6187 env
->cp15
.c3
= qemu_get_be32(f
);
6188 env
->cp15
.c5_insn
= qemu_get_be32(f
);
6189 env
->cp15
.c5_data
= qemu_get_be32(f
);
6190 for (i
= 0; i
< 8; i
++) {
6191 env
->cp15
.c6_region
[i
] = qemu_get_be32(f
);
6193 env
->cp15
.c6_insn
= qemu_get_be32(f
);
6194 env
->cp15
.c6_data
= qemu_get_be32(f
);
6195 env
->cp15
.c9_insn
= qemu_get_be32(f
);
6196 env
->cp15
.c9_data
= qemu_get_be32(f
);
6197 env
->cp15
.c13_fcse
= qemu_get_be32(f
);
6198 env
->cp15
.c13_context
= qemu_get_be32(f
);
6199 env
->cp15
.c15_cpar
= qemu_get_be32(f
);
6201 env
->features
= qemu_get_be32(f
);
6203 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
6204 for (i
= 0; i
< 16; i
++) {
6206 u
.l
.upper
= qemu_get_be32(f
);
6207 u
.l
.lower
= qemu_get_be32(f
);
6208 env
->vfp
.regs
[i
] = u
.d
;
6210 for (i
= 0; i
< 16; i
++) {
6211 env
->vfp
.xregs
[i
] = qemu_get_be32(f
);
6214 /* TODO: Should use proper FPSCR access functions. */
6215 env
->vfp
.vec_len
= qemu_get_be32(f
);
6216 env
->vfp
.vec_stride
= qemu_get_be32(f
);
6219 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
6220 for (i
= 0; i
< 16; i
++) {
6221 env
->iwmmxt
.regs
[i
] = qemu_get_be64(f
);
6223 for (i
= 0; i
< 16; i
++) {
6224 env
->iwmmxt
.cregs
[i
] = qemu_get_be32(f
);
6233 #warning No CPU save/restore functions
6237 /***********************************************************/
6238 /* ram save/restore */
6240 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
6244 v
= qemu_get_byte(f
);
6247 if (qemu_get_buffer(f
, buf
, len
) != len
)
6251 v
= qemu_get_byte(f
);
6252 memset(buf
, v
, len
);
6260 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
6264 if (qemu_get_be32(f
) != phys_ram_size
)
6266 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
6267 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
6274 #define BDRV_HASH_BLOCK_SIZE 1024
6275 #define IOBUF_SIZE 4096
6276 #define RAM_CBLOCK_MAGIC 0xfabe
6278 typedef struct RamCompressState
{
6281 uint8_t buf
[IOBUF_SIZE
];
6284 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
6287 memset(s
, 0, sizeof(*s
));
6289 ret
= deflateInit2(&s
->zstream
, 1,
6291 9, Z_DEFAULT_STRATEGY
);
6294 s
->zstream
.avail_out
= IOBUF_SIZE
;
6295 s
->zstream
.next_out
= s
->buf
;
6299 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
6301 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
6302 qemu_put_be16(s
->f
, len
);
6303 qemu_put_buffer(s
->f
, buf
, len
);
6306 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
6310 s
->zstream
.avail_in
= len
;
6311 s
->zstream
.next_in
= (uint8_t *)buf
;
6312 while (s
->zstream
.avail_in
> 0) {
6313 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
6316 if (s
->zstream
.avail_out
== 0) {
6317 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
6318 s
->zstream
.avail_out
= IOBUF_SIZE
;
6319 s
->zstream
.next_out
= s
->buf
;
6325 static void ram_compress_close(RamCompressState
*s
)
6329 /* compress last bytes */
6331 ret
= deflate(&s
->zstream
, Z_FINISH
);
6332 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
6333 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
6335 ram_put_cblock(s
, s
->buf
, len
);
6337 s
->zstream
.avail_out
= IOBUF_SIZE
;
6338 s
->zstream
.next_out
= s
->buf
;
6339 if (ret
== Z_STREAM_END
)
6346 deflateEnd(&s
->zstream
);
6349 typedef struct RamDecompressState
{
6352 uint8_t buf
[IOBUF_SIZE
];
6353 } RamDecompressState
;
6355 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
6358 memset(s
, 0, sizeof(*s
));
6360 ret
= inflateInit(&s
->zstream
);
6366 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
6370 s
->zstream
.avail_out
= len
;
6371 s
->zstream
.next_out
= buf
;
6372 while (s
->zstream
.avail_out
> 0) {
6373 if (s
->zstream
.avail_in
== 0) {
6374 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
6376 clen
= qemu_get_be16(s
->f
);
6377 if (clen
> IOBUF_SIZE
)
6379 qemu_get_buffer(s
->f
, s
->buf
, clen
);
6380 s
->zstream
.avail_in
= clen
;
6381 s
->zstream
.next_in
= s
->buf
;
6383 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
6384 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
6391 static void ram_decompress_close(RamDecompressState
*s
)
6393 inflateEnd(&s
->zstream
);
6396 static void ram_save(QEMUFile
*f
, void *opaque
)
6399 RamCompressState s1
, *s
= &s1
;
6402 qemu_put_be32(f
, phys_ram_size
);
6403 if (ram_compress_open(s
, f
) < 0)
6405 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6407 if (tight_savevm_enabled
) {
6411 /* find if the memory block is available on a virtual
6414 for(j
= 0; j
< MAX_DISKS
; j
++) {
6416 sector_num
= bdrv_hash_find(bs_table
[j
],
6417 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6418 if (sector_num
>= 0)
6423 goto normal_compress
;
6426 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
6427 ram_compress_buf(s
, buf
, 10);
6433 ram_compress_buf(s
, buf
, 1);
6434 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6437 ram_compress_close(s
);
6440 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
6442 RamDecompressState s1
, *s
= &s1
;
6446 if (version_id
== 1)
6447 return ram_load_v1(f
, opaque
);
6448 if (version_id
!= 2)
6450 if (qemu_get_be32(f
) != phys_ram_size
)
6452 if (ram_decompress_open(s
, f
) < 0)
6454 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6455 if (ram_decompress_buf(s
, buf
, 1) < 0) {
6456 fprintf(stderr
, "Error while reading ram block header\n");
6460 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
6461 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
6470 ram_decompress_buf(s
, buf
+ 1, 9);
6472 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
6473 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
6474 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
6477 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
6478 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
6479 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
6480 bs_index
, sector_num
);
6487 printf("Error block header\n");
6491 ram_decompress_close(s
);
6495 /***********************************************************/
6496 /* bottom halves (can be seen as timers which expire ASAP) */
6505 static QEMUBH
*first_bh
= NULL
;
6507 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
6510 bh
= qemu_mallocz(sizeof(QEMUBH
));
6514 bh
->opaque
= opaque
;
6518 int qemu_bh_poll(void)
6537 void qemu_bh_schedule(QEMUBH
*bh
)
6539 CPUState
*env
= cpu_single_env
;
6543 bh
->next
= first_bh
;
6546 /* stop the currently executing CPU to execute the BH ASAP */
6548 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
6552 void qemu_bh_cancel(QEMUBH
*bh
)
6555 if (bh
->scheduled
) {
6558 pbh
= &(*pbh
)->next
;
6564 void qemu_bh_delete(QEMUBH
*bh
)
6570 /***********************************************************/
6571 /* machine registration */
6573 QEMUMachine
*first_machine
= NULL
;
6575 int qemu_register_machine(QEMUMachine
*m
)
6578 pm
= &first_machine
;
6586 QEMUMachine
*find_machine(const char *name
)
6590 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6591 if (!strcmp(m
->name
, name
))
6597 /***********************************************************/
6598 /* main execution loop */
6600 void gui_update(void *opaque
)
6602 DisplayState
*ds
= opaque
;
6603 ds
->dpy_refresh(ds
);
6604 qemu_mod_timer(ds
->gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
6607 struct vm_change_state_entry
{
6608 VMChangeStateHandler
*cb
;
6610 LIST_ENTRY (vm_change_state_entry
) entries
;
6613 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
6615 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
6618 VMChangeStateEntry
*e
;
6620 e
= qemu_mallocz(sizeof (*e
));
6626 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
6630 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
6632 LIST_REMOVE (e
, entries
);
6636 static void vm_state_notify(int running
)
6638 VMChangeStateEntry
*e
;
6640 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
6641 e
->cb(e
->opaque
, running
);
6645 /* XXX: support several handlers */
6646 static VMStopHandler
*vm_stop_cb
;
6647 static void *vm_stop_opaque
;
6649 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6652 vm_stop_opaque
= opaque
;
6656 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6667 qemu_rearm_alarm_timer(alarm_timer
);
6671 void vm_stop(int reason
)
6674 cpu_disable_ticks();
6678 vm_stop_cb(vm_stop_opaque
, reason
);
6685 /* reset/shutdown handler */
6687 typedef struct QEMUResetEntry
{
6688 QEMUResetHandler
*func
;
6690 struct QEMUResetEntry
*next
;
6693 static QEMUResetEntry
*first_reset_entry
;
6694 static int reset_requested
;
6695 static int shutdown_requested
;
6696 static int powerdown_requested
;
6698 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
6700 QEMUResetEntry
**pre
, *re
;
6702 pre
= &first_reset_entry
;
6703 while (*pre
!= NULL
)
6704 pre
= &(*pre
)->next
;
6705 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
6707 re
->opaque
= opaque
;
6712 static void qemu_system_reset(void)
6716 /* reset all devices */
6717 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
6718 re
->func(re
->opaque
);
6722 void qemu_system_reset_request(void)
6725 shutdown_requested
= 1;
6727 reset_requested
= 1;
6730 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6733 void qemu_system_shutdown_request(void)
6735 shutdown_requested
= 1;
6737 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6740 void qemu_system_powerdown_request(void)
6742 powerdown_requested
= 1;
6744 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6747 void main_loop_wait(int timeout
)
6749 IOHandlerRecord
*ioh
;
6750 fd_set rfds
, wfds
, xfds
;
6759 /* XXX: need to suppress polling by better using win32 events */
6761 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
6762 ret
|= pe
->func(pe
->opaque
);
6767 WaitObjects
*w
= &wait_objects
;
6769 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
6770 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
6771 if (w
->func
[ret
- WAIT_OBJECT_0
])
6772 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
6774 /* Check for additional signaled events */
6775 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
6777 /* Check if event is signaled */
6778 ret2
= WaitForSingleObject(w
->events
[i
], 0);
6779 if(ret2
== WAIT_OBJECT_0
) {
6781 w
->func
[i
](w
->opaque
[i
]);
6782 } else if (ret2
== WAIT_TIMEOUT
) {
6784 err
= GetLastError();
6785 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
6788 } else if (ret
== WAIT_TIMEOUT
) {
6790 err
= GetLastError();
6791 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
6795 /* poll any events */
6796 /* XXX: separate device handlers from system ones */
6801 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6805 (!ioh
->fd_read_poll
||
6806 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
6807 FD_SET(ioh
->fd
, &rfds
);
6811 if (ioh
->fd_write
) {
6812 FD_SET(ioh
->fd
, &wfds
);
6822 tv
.tv_usec
= timeout
* 1000;
6824 #if defined(CONFIG_SLIRP)
6826 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
6829 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
6831 IOHandlerRecord
**pioh
;
6833 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6834 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
6835 ioh
->fd_read(ioh
->opaque
);
6837 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
6838 ioh
->fd_write(ioh
->opaque
);
6842 /* remove deleted IO handlers */
6843 pioh
= &first_io_handler
;
6853 #if defined(CONFIG_SLIRP)
6860 slirp_select_poll(&rfds
, &wfds
, &xfds
);
6866 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
6867 qemu_get_clock(vm_clock
));
6868 /* run dma transfers, if any */
6872 /* real time timers */
6873 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
6874 qemu_get_clock(rt_clock
));
6876 /* Check bottom-halves last in case any of the earlier events triggered
6882 static CPUState
*cur_cpu
;
6887 #ifdef CONFIG_PROFILER
6892 cur_cpu
= first_cpu
;
6899 env
= env
->next_cpu
;
6902 #ifdef CONFIG_PROFILER
6903 ti
= profile_getclock();
6905 ret
= cpu_exec(env
);
6906 #ifdef CONFIG_PROFILER
6907 qemu_time
+= profile_getclock() - ti
;
6909 if (ret
== EXCP_HLT
) {
6910 /* Give the next CPU a chance to run. */
6914 if (ret
!= EXCP_HALTED
)
6916 /* all CPUs are halted ? */
6922 if (shutdown_requested
) {
6923 ret
= EXCP_INTERRUPT
;
6926 if (reset_requested
) {
6927 reset_requested
= 0;
6928 qemu_system_reset();
6929 ret
= EXCP_INTERRUPT
;
6931 if (powerdown_requested
) {
6932 powerdown_requested
= 0;
6933 qemu_system_powerdown();
6934 ret
= EXCP_INTERRUPT
;
6936 if (ret
== EXCP_DEBUG
) {
6937 vm_stop(EXCP_DEBUG
);
6939 /* If all cpus are halted then wait until the next IRQ */
6940 /* XXX: use timeout computed from timers */
6941 if (ret
== EXCP_HALTED
)
6948 #ifdef CONFIG_PROFILER
6949 ti
= profile_getclock();
6951 main_loop_wait(timeout
);
6952 #ifdef CONFIG_PROFILER
6953 dev_time
+= profile_getclock() - ti
;
6956 cpu_disable_ticks();
6960 static void help(int exitcode
)
6962 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2007 Fabrice Bellard\n"
6963 "usage: %s [options] [disk_image]\n"
6965 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
6967 "Standard options:\n"
6968 "-M machine select emulated machine (-M ? for list)\n"
6969 "-cpu cpu select CPU (-cpu ? for list)\n"
6970 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
6971 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
6972 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
6973 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
6974 "-mtdblock file use 'file' as on-board Flash memory image\n"
6975 "-sd file use 'file' as SecureDigital card image\n"
6976 "-pflash file use 'file' as a parallel flash image\n"
6977 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
6978 "-snapshot write to temporary files instead of disk image files\n"
6980 "-no-frame open SDL window without a frame and window decorations\n"
6981 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
6982 "-no-quit disable SDL window close capability\n"
6985 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
6987 "-m megs set virtual RAM size to megs MB [default=%d]\n"
6988 "-smp n set the number of CPUs to 'n' [default=1]\n"
6989 "-nographic disable graphical output and redirect serial I/Os to console\n"
6990 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
6992 "-k language use keyboard layout (for example \"fr\" for French)\n"
6995 "-audio-help print list of audio drivers and their options\n"
6996 "-soundhw c1,... enable audio support\n"
6997 " and only specified sound cards (comma separated list)\n"
6998 " use -soundhw ? to get the list of supported cards\n"
6999 " use -soundhw all to enable all of them\n"
7001 "-localtime set the real time clock to local time [default=utc]\n"
7002 "-full-screen start in full screen\n"
7004 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
7006 "-usb enable the USB driver (will be the default soon)\n"
7007 "-usbdevice name add the host or guest USB device 'name'\n"
7008 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7009 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
7011 "-name string set the name of the guest\n"
7013 "Network options:\n"
7014 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
7015 " create a new Network Interface Card and connect it to VLAN 'n'\n"
7017 "-net user[,vlan=n][,hostname=host]\n"
7018 " connect the user mode network stack to VLAN 'n' and send\n"
7019 " hostname 'host' to DHCP clients\n"
7022 "-net tap[,vlan=n],ifname=name\n"
7023 " connect the host TAP network interface to VLAN 'n'\n"
7025 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
7026 " connect the host TAP network interface to VLAN 'n' and use\n"
7027 " the network script 'file' (default=%s);\n"
7028 " use 'script=no' to disable script execution;\n"
7029 " use 'fd=h' to connect to an already opened TAP interface\n"
7031 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
7032 " connect the vlan 'n' to another VLAN using a socket connection\n"
7033 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
7034 " connect the vlan 'n' to multicast maddr and port\n"
7035 "-net none use it alone to have zero network devices; if no -net option\n"
7036 " is provided, the default is '-net nic -net user'\n"
7039 "-tftp dir allow tftp access to files in dir [-net user]\n"
7040 "-bootp file advertise file in BOOTP replies\n"
7042 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
7044 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
7045 " redirect TCP or UDP connections from host to guest [-net user]\n"
7048 "Linux boot specific:\n"
7049 "-kernel bzImage use 'bzImage' as kernel image\n"
7050 "-append cmdline use 'cmdline' as kernel command line\n"
7051 "-initrd file use 'file' as initial ram disk\n"
7053 "Debug/Expert options:\n"
7054 "-monitor dev redirect the monitor to char device 'dev'\n"
7055 "-serial dev redirect the serial port to char device 'dev'\n"
7056 "-parallel dev redirect the parallel port to char device 'dev'\n"
7057 "-pidfile file Write PID to 'file'\n"
7058 "-S freeze CPU at startup (use 'c' to start execution)\n"
7059 "-s wait gdb connection to port\n"
7060 "-p port set gdb connection port [default=%s]\n"
7061 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
7062 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
7063 " translation (t=none or lba) (usually qemu can guess them)\n"
7064 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
7066 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
7067 "-no-kqemu disable KQEMU kernel module usage\n"
7069 #ifdef USE_CODE_COPY
7070 "-no-code-copy disable code copy acceleration\n"
7073 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
7074 " (default is CL-GD5446 PCI VGA)\n"
7075 "-no-acpi disable ACPI\n"
7077 "-no-reboot exit instead of rebooting\n"
7078 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
7079 "-vnc display start a VNC server on display\n"
7081 "-daemonize daemonize QEMU after initializing\n"
7083 "-option-rom rom load a file, rom, into the option ROM space\n"
7085 "-prom-env variable=value set OpenBIOS nvram variables\n"
7087 "-clock force the use of the given methods for timer alarm.\n"
7088 " To see what timers are available use -clock help\n"
7090 "During emulation, the following keys are useful:\n"
7091 "ctrl-alt-f toggle full screen\n"
7092 "ctrl-alt-n switch to virtual console 'n'\n"
7093 "ctrl-alt toggle mouse and keyboard grab\n"
7095 "When using -nographic, press 'ctrl-a h' to get some help.\n"
7100 DEFAULT_NETWORK_SCRIPT
,
7102 DEFAULT_GDBSTUB_PORT
,
7107 #define HAS_ARG 0x0001
7121 QEMU_OPTION_mtdblock
,
7125 QEMU_OPTION_snapshot
,
7127 QEMU_OPTION_no_fd_bootchk
,
7130 QEMU_OPTION_nographic
,
7131 QEMU_OPTION_portrait
,
7133 QEMU_OPTION_audio_help
,
7134 QEMU_OPTION_soundhw
,
7154 QEMU_OPTION_no_code_copy
,
7156 QEMU_OPTION_localtime
,
7157 QEMU_OPTION_cirrusvga
,
7160 QEMU_OPTION_std_vga
,
7162 QEMU_OPTION_monitor
,
7164 QEMU_OPTION_parallel
,
7166 QEMU_OPTION_full_screen
,
7167 QEMU_OPTION_no_frame
,
7168 QEMU_OPTION_alt_grab
,
7169 QEMU_OPTION_no_quit
,
7170 QEMU_OPTION_pidfile
,
7171 QEMU_OPTION_no_kqemu
,
7172 QEMU_OPTION_kernel_kqemu
,
7173 QEMU_OPTION_win2k_hack
,
7175 QEMU_OPTION_usbdevice
,
7178 QEMU_OPTION_no_acpi
,
7179 QEMU_OPTION_no_reboot
,
7180 QEMU_OPTION_show_cursor
,
7181 QEMU_OPTION_daemonize
,
7182 QEMU_OPTION_option_rom
,
7183 QEMU_OPTION_semihosting
,
7185 QEMU_OPTION_prom_env
,
7186 QEMU_OPTION_old_param
,
7190 typedef struct QEMUOption
{
7196 const QEMUOption qemu_options
[] = {
7197 { "h", 0, QEMU_OPTION_h
},
7198 { "help", 0, QEMU_OPTION_h
},
7200 { "M", HAS_ARG
, QEMU_OPTION_M
},
7201 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
7202 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
7203 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
7204 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
7205 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
7206 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
7207 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
7208 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
7209 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
7210 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
7211 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
7212 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
7213 { "snapshot", 0, QEMU_OPTION_snapshot
},
7215 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
7217 { "m", HAS_ARG
, QEMU_OPTION_m
},
7218 { "nographic", 0, QEMU_OPTION_nographic
},
7219 { "portrait", 0, QEMU_OPTION_portrait
},
7220 { "k", HAS_ARG
, QEMU_OPTION_k
},
7222 { "audio-help", 0, QEMU_OPTION_audio_help
},
7223 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
7226 { "net", HAS_ARG
, QEMU_OPTION_net
},
7228 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
7229 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
7231 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
7233 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
7236 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
7237 { "append", HAS_ARG
, QEMU_OPTION_append
},
7238 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
7240 { "S", 0, QEMU_OPTION_S
},
7241 { "s", 0, QEMU_OPTION_s
},
7242 { "p", HAS_ARG
, QEMU_OPTION_p
},
7243 { "d", HAS_ARG
, QEMU_OPTION_d
},
7244 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
7245 { "L", HAS_ARG
, QEMU_OPTION_L
},
7246 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
7247 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
7249 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
7250 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
7252 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7253 { "g", 1, QEMU_OPTION_g
},
7255 { "localtime", 0, QEMU_OPTION_localtime
},
7256 { "std-vga", 0, QEMU_OPTION_std_vga
},
7257 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
7258 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
7259 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
7260 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
7261 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
7262 { "full-screen", 0, QEMU_OPTION_full_screen
},
7264 { "no-frame", 0, QEMU_OPTION_no_frame
},
7265 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
7266 { "no-quit", 0, QEMU_OPTION_no_quit
},
7268 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
7269 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
7270 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
7271 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
7272 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
7274 /* temporary options */
7275 { "usb", 0, QEMU_OPTION_usb
},
7276 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
7277 { "vmwarevga", 0, QEMU_OPTION_vmsvga
},
7278 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
7279 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
7280 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
7281 { "daemonize", 0, QEMU_OPTION_daemonize
},
7282 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
7283 #if defined(TARGET_ARM) || defined(TARGET_M68K)
7284 { "semihosting", 0, QEMU_OPTION_semihosting
},
7286 { "name", HAS_ARG
, QEMU_OPTION_name
},
7287 #if defined(TARGET_SPARC)
7288 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
7290 #if defined(TARGET_ARM)
7291 { "old-param", 0, QEMU_OPTION_old_param
},
7293 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
7297 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
7299 /* this stack is only used during signal handling */
7300 #define SIGNAL_STACK_SIZE 32768
7302 static uint8_t *signal_stack
;
7306 /* password input */
7308 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
7313 if (!bdrv_is_encrypted(bs
))
7316 term_printf("%s is encrypted.\n", name
);
7317 for(i
= 0; i
< 3; i
++) {
7318 monitor_readline("Password: ", 1, password
, sizeof(password
));
7319 if (bdrv_set_key(bs
, password
) == 0)
7321 term_printf("invalid password\n");
7326 static BlockDriverState
*get_bdrv(int index
)
7328 BlockDriverState
*bs
;
7331 bs
= bs_table
[index
];
7332 } else if (index
< 6) {
7333 bs
= fd_table
[index
- 4];
7340 static void read_passwords(void)
7342 BlockDriverState
*bs
;
7345 for(i
= 0; i
< 6; i
++) {
7348 qemu_key_check(bs
, bdrv_get_device_name(bs
));
7352 /* XXX: currently we cannot use simultaneously different CPUs */
7353 void register_machines(void)
7355 #if defined(TARGET_I386)
7356 qemu_register_machine(&pc_machine
);
7357 qemu_register_machine(&isapc_machine
);
7358 #elif defined(TARGET_PPC)
7359 qemu_register_machine(&heathrow_machine
);
7360 qemu_register_machine(&core99_machine
);
7361 qemu_register_machine(&prep_machine
);
7362 qemu_register_machine(&ref405ep_machine
);
7363 qemu_register_machine(&taihu_machine
);
7364 #elif defined(TARGET_MIPS)
7365 qemu_register_machine(&mips_machine
);
7366 qemu_register_machine(&mips_malta_machine
);
7367 qemu_register_machine(&mips_pica61_machine
);
7368 qemu_register_machine(&mips_mipssim_machine
);
7369 #elif defined(TARGET_SPARC)
7370 #ifdef TARGET_SPARC64
7371 qemu_register_machine(&sun4u_machine
);
7373 qemu_register_machine(&ss5_machine
);
7374 qemu_register_machine(&ss10_machine
);
7376 #elif defined(TARGET_ARM)
7377 qemu_register_machine(&integratorcp_machine
);
7378 qemu_register_machine(&versatilepb_machine
);
7379 qemu_register_machine(&versatileab_machine
);
7380 qemu_register_machine(&realview_machine
);
7381 qemu_register_machine(&akitapda_machine
);
7382 qemu_register_machine(&spitzpda_machine
);
7383 qemu_register_machine(&borzoipda_machine
);
7384 qemu_register_machine(&terrierpda_machine
);
7385 qemu_register_machine(&palmte_machine
);
7386 #elif defined(TARGET_SH4)
7387 qemu_register_machine(&shix_machine
);
7388 qemu_register_machine(&r2d_machine
);
7389 #elif defined(TARGET_ALPHA)
7391 #elif defined(TARGET_M68K)
7392 qemu_register_machine(&mcf5208evb_machine
);
7393 qemu_register_machine(&an5206_machine
);
7394 #elif defined(TARGET_CRIS)
7395 qemu_register_machine(&bareetraxfs_machine
);
7397 #error unsupported CPU
7402 struct soundhw soundhw
[] = {
7403 #ifdef HAS_AUDIO_CHOICE
7410 { .init_isa
= pcspk_audio_init
}
7415 "Creative Sound Blaster 16",
7418 { .init_isa
= SB16_init
}
7425 "Yamaha YMF262 (OPL3)",
7427 "Yamaha YM3812 (OPL2)",
7431 { .init_isa
= Adlib_init
}
7438 "Gravis Ultrasound GF1",
7441 { .init_isa
= GUS_init
}
7447 "ENSONIQ AudioPCI ES1370",
7450 { .init_pci
= es1370_init
}
7454 { NULL
, NULL
, 0, 0, { NULL
} }
7457 static void select_soundhw (const char *optarg
)
7461 if (*optarg
== '?') {
7464 printf ("Valid sound card names (comma separated):\n");
7465 for (c
= soundhw
; c
->name
; ++c
) {
7466 printf ("%-11s %s\n", c
->name
, c
->descr
);
7468 printf ("\n-soundhw all will enable all of the above\n");
7469 exit (*optarg
!= '?');
7477 if (!strcmp (optarg
, "all")) {
7478 for (c
= soundhw
; c
->name
; ++c
) {
7486 e
= strchr (p
, ',');
7487 l
= !e
? strlen (p
) : (size_t) (e
- p
);
7489 for (c
= soundhw
; c
->name
; ++c
) {
7490 if (!strncmp (c
->name
, p
, l
)) {
7499 "Unknown sound card name (too big to show)\n");
7502 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
7507 p
+= l
+ (e
!= NULL
);
7511 goto show_valid_cards
;
7517 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
7519 exit(STATUS_CONTROL_C_EXIT
);
7524 #define MAX_NET_CLIENTS 32
7526 int main(int argc
, char **argv
)
7528 #ifdef CONFIG_GDBSTUB
7530 const char *gdbstub_port
;
7532 int i
, cdrom_index
, pflash_index
;
7533 int snapshot
, linux_boot
;
7534 const char *initrd_filename
;
7535 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
7536 const char *pflash_filename
[MAX_PFLASH
];
7537 const char *sd_filename
;
7538 const char *mtd_filename
;
7539 const char *kernel_filename
, *kernel_cmdline
;
7540 DisplayState
*ds
= &display_state
;
7541 int cyls
, heads
, secs
, translation
;
7542 char net_clients
[MAX_NET_CLIENTS
][256];
7545 const char *r
, *optarg
;
7546 CharDriverState
*monitor_hd
;
7547 char monitor_device
[128];
7548 char serial_devices
[MAX_SERIAL_PORTS
][128];
7549 int serial_device_index
;
7550 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
7551 int parallel_device_index
;
7552 const char *loadvm
= NULL
;
7553 QEMUMachine
*machine
;
7554 const char *cpu_model
;
7555 char usb_devices
[MAX_USB_CMDLINE
][128];
7556 int usb_devices_index
;
7558 const char *pid_file
= NULL
;
7561 LIST_INIT (&vm_change_state_head
);
7564 struct sigaction act
;
7565 sigfillset(&act
.sa_mask
);
7567 act
.sa_handler
= SIG_IGN
;
7568 sigaction(SIGPIPE
, &act
, NULL
);
7571 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
7572 /* Note: cpu_interrupt() is currently not SMP safe, so we force
7573 QEMU to run on a single CPU */
7578 h
= GetCurrentProcess();
7579 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
7580 for(i
= 0; i
< 32; i
++) {
7581 if (mask
& (1 << i
))
7586 SetProcessAffinityMask(h
, mask
);
7592 register_machines();
7593 machine
= first_machine
;
7595 initrd_filename
= NULL
;
7596 for(i
= 0; i
< MAX_FD
; i
++)
7597 fd_filename
[i
] = NULL
;
7598 for(i
= 0; i
< MAX_DISKS
; i
++)
7599 hd_filename
[i
] = NULL
;
7600 for(i
= 0; i
< MAX_PFLASH
; i
++)
7601 pflash_filename
[i
] = NULL
;
7604 mtd_filename
= NULL
;
7605 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
7606 vga_ram_size
= VGA_RAM_SIZE
;
7607 #ifdef CONFIG_GDBSTUB
7609 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
7613 kernel_filename
= NULL
;
7614 kernel_cmdline
= "";
7620 cyls
= heads
= secs
= 0;
7621 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7622 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
7624 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
7625 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
7626 serial_devices
[i
][0] = '\0';
7627 serial_device_index
= 0;
7629 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
7630 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
7631 parallel_devices
[i
][0] = '\0';
7632 parallel_device_index
= 0;
7634 usb_devices_index
= 0;
7639 /* default mac address of the first network interface */
7647 hd_filename
[0] = argv
[optind
++];
7649 const QEMUOption
*popt
;
7652 /* Treat --foo the same as -foo. */
7655 popt
= qemu_options
;
7658 fprintf(stderr
, "%s: invalid option -- '%s'\n",
7662 if (!strcmp(popt
->name
, r
+ 1))
7666 if (popt
->flags
& HAS_ARG
) {
7667 if (optind
>= argc
) {
7668 fprintf(stderr
, "%s: option '%s' requires an argument\n",
7672 optarg
= argv
[optind
++];
7677 switch(popt
->index
) {
7679 machine
= find_machine(optarg
);
7682 printf("Supported machines are:\n");
7683 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
7684 printf("%-10s %s%s\n",
7686 m
== first_machine
? " (default)" : "");
7688 exit(*optarg
!= '?');
7691 case QEMU_OPTION_cpu
:
7692 /* hw initialization will check this */
7693 if (*optarg
== '?') {
7694 /* XXX: implement xxx_cpu_list for targets that still miss it */
7695 #if defined(cpu_list)
7696 cpu_list(stdout
, &fprintf
);
7703 case QEMU_OPTION_initrd
:
7704 initrd_filename
= optarg
;
7706 case QEMU_OPTION_hda
:
7707 case QEMU_OPTION_hdb
:
7708 case QEMU_OPTION_hdc
:
7709 case QEMU_OPTION_hdd
:
7712 hd_index
= popt
->index
- QEMU_OPTION_hda
;
7713 hd_filename
[hd_index
] = optarg
;
7714 if (hd_index
== cdrom_index
)
7718 case QEMU_OPTION_mtdblock
:
7719 mtd_filename
= optarg
;
7721 case QEMU_OPTION_sd
:
7722 sd_filename
= optarg
;
7724 case QEMU_OPTION_pflash
:
7725 if (pflash_index
>= MAX_PFLASH
) {
7726 fprintf(stderr
, "qemu: too many parallel flash images\n");
7729 pflash_filename
[pflash_index
++] = optarg
;
7731 case QEMU_OPTION_snapshot
:
7734 case QEMU_OPTION_hdachs
:
7738 cyls
= strtol(p
, (char **)&p
, 0);
7739 if (cyls
< 1 || cyls
> 16383)
7744 heads
= strtol(p
, (char **)&p
, 0);
7745 if (heads
< 1 || heads
> 16)
7750 secs
= strtol(p
, (char **)&p
, 0);
7751 if (secs
< 1 || secs
> 63)
7755 if (!strcmp(p
, "none"))
7756 translation
= BIOS_ATA_TRANSLATION_NONE
;
7757 else if (!strcmp(p
, "lba"))
7758 translation
= BIOS_ATA_TRANSLATION_LBA
;
7759 else if (!strcmp(p
, "auto"))
7760 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7763 } else if (*p
!= '\0') {
7765 fprintf(stderr
, "qemu: invalid physical CHS format\n");
7770 case QEMU_OPTION_nographic
:
7771 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
7772 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "null");
7773 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
7776 case QEMU_OPTION_portrait
:
7779 case QEMU_OPTION_kernel
:
7780 kernel_filename
= optarg
;
7782 case QEMU_OPTION_append
:
7783 kernel_cmdline
= optarg
;
7785 case QEMU_OPTION_cdrom
:
7786 if (cdrom_index
>= 0) {
7787 hd_filename
[cdrom_index
] = optarg
;
7790 case QEMU_OPTION_boot
:
7791 boot_device
= optarg
[0];
7792 if (boot_device
!= 'a' &&
7793 #if defined(TARGET_SPARC) || defined(TARGET_I386)
7795 boot_device
!= 'n' &&
7797 boot_device
!= 'c' && boot_device
!= 'd') {
7798 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
7802 case QEMU_OPTION_fda
:
7803 fd_filename
[0] = optarg
;
7805 case QEMU_OPTION_fdb
:
7806 fd_filename
[1] = optarg
;
7809 case QEMU_OPTION_no_fd_bootchk
:
7813 case QEMU_OPTION_no_code_copy
:
7814 code_copy_enabled
= 0;
7816 case QEMU_OPTION_net
:
7817 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
7818 fprintf(stderr
, "qemu: too many network clients\n");
7821 pstrcpy(net_clients
[nb_net_clients
],
7822 sizeof(net_clients
[0]),
7827 case QEMU_OPTION_tftp
:
7828 tftp_prefix
= optarg
;
7830 case QEMU_OPTION_bootp
:
7831 bootp_filename
= optarg
;
7834 case QEMU_OPTION_smb
:
7835 net_slirp_smb(optarg
);
7838 case QEMU_OPTION_redir
:
7839 net_slirp_redir(optarg
);
7843 case QEMU_OPTION_audio_help
:
7847 case QEMU_OPTION_soundhw
:
7848 select_soundhw (optarg
);
7855 ram_size
= atoi(optarg
) * 1024 * 1024;
7858 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
7859 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
7860 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
7869 mask
= cpu_str_to_log_mask(optarg
);
7871 printf("Log items (comma separated):\n");
7872 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
7873 printf("%-10s %s\n", item
->name
, item
->help
);
7880 #ifdef CONFIG_GDBSTUB
7885 gdbstub_port
= optarg
;
7891 case QEMU_OPTION_bios
:
7898 keyboard_layout
= optarg
;
7900 case QEMU_OPTION_localtime
:
7903 case QEMU_OPTION_cirrusvga
:
7904 cirrus_vga_enabled
= 1;
7907 case QEMU_OPTION_vmsvga
:
7908 cirrus_vga_enabled
= 0;
7911 case QEMU_OPTION_std_vga
:
7912 cirrus_vga_enabled
= 0;
7920 w
= strtol(p
, (char **)&p
, 10);
7923 fprintf(stderr
, "qemu: invalid resolution or depth\n");
7929 h
= strtol(p
, (char **)&p
, 10);
7934 depth
= strtol(p
, (char **)&p
, 10);
7935 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
7936 depth
!= 24 && depth
!= 32)
7938 } else if (*p
== '\0') {
7939 depth
= graphic_depth
;
7946 graphic_depth
= depth
;
7949 case QEMU_OPTION_echr
:
7952 term_escape_char
= strtol(optarg
, &r
, 0);
7954 printf("Bad argument to echr\n");
7957 case QEMU_OPTION_monitor
:
7958 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
7960 case QEMU_OPTION_serial
:
7961 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
7962 fprintf(stderr
, "qemu: too many serial ports\n");
7965 pstrcpy(serial_devices
[serial_device_index
],
7966 sizeof(serial_devices
[0]), optarg
);
7967 serial_device_index
++;
7969 case QEMU_OPTION_parallel
:
7970 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
7971 fprintf(stderr
, "qemu: too many parallel ports\n");
7974 pstrcpy(parallel_devices
[parallel_device_index
],
7975 sizeof(parallel_devices
[0]), optarg
);
7976 parallel_device_index
++;
7978 case QEMU_OPTION_loadvm
:
7981 case QEMU_OPTION_full_screen
:
7985 case QEMU_OPTION_no_frame
:
7988 case QEMU_OPTION_alt_grab
:
7991 case QEMU_OPTION_no_quit
:
7995 case QEMU_OPTION_pidfile
:
7999 case QEMU_OPTION_win2k_hack
:
8000 win2k_install_hack
= 1;
8004 case QEMU_OPTION_no_kqemu
:
8007 case QEMU_OPTION_kernel_kqemu
:
8011 case QEMU_OPTION_usb
:
8014 case QEMU_OPTION_usbdevice
:
8016 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
8017 fprintf(stderr
, "Too many USB devices\n");
8020 pstrcpy(usb_devices
[usb_devices_index
],
8021 sizeof(usb_devices
[usb_devices_index
]),
8023 usb_devices_index
++;
8025 case QEMU_OPTION_smp
:
8026 smp_cpus
= atoi(optarg
);
8027 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
8028 fprintf(stderr
, "Invalid number of CPUs\n");
8032 case QEMU_OPTION_vnc
:
8033 vnc_display
= optarg
;
8035 case QEMU_OPTION_no_acpi
:
8038 case QEMU_OPTION_no_reboot
:
8041 case QEMU_OPTION_show_cursor
:
8044 case QEMU_OPTION_daemonize
:
8047 case QEMU_OPTION_option_rom
:
8048 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
8049 fprintf(stderr
, "Too many option ROMs\n");
8052 option_rom
[nb_option_roms
] = optarg
;
8055 case QEMU_OPTION_semihosting
:
8056 semihosting_enabled
= 1;
8058 case QEMU_OPTION_name
:
8062 case QEMU_OPTION_prom_env
:
8063 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
8064 fprintf(stderr
, "Too many prom variables\n");
8067 prom_envs
[nb_prom_envs
] = optarg
;
8072 case QEMU_OPTION_old_param
:
8075 case QEMU_OPTION_clock
:
8076 configure_alarms(optarg
);
8083 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
8084 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
8091 if (pipe(fds
) == -1)
8102 len
= read(fds
[0], &status
, 1);
8103 if (len
== -1 && (errno
== EINTR
))
8108 else if (status
== 1) {
8109 fprintf(stderr
, "Could not acquire pidfile\n");
8127 signal(SIGTSTP
, SIG_IGN
);
8128 signal(SIGTTOU
, SIG_IGN
);
8129 signal(SIGTTIN
, SIG_IGN
);
8133 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
8136 write(fds
[1], &status
, 1);
8138 fprintf(stderr
, "Could not acquire pid file\n");
8146 linux_boot
= (kernel_filename
!= NULL
);
8149 boot_device
!= 'n' &&
8150 hd_filename
[0] == '\0' &&
8151 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
8152 fd_filename
[0] == '\0')
8155 /* boot to floppy or the default cd if no hard disk defined yet */
8156 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
8157 if (fd_filename
[0] != '\0')
8163 setvbuf(stdout
, NULL
, _IOLBF
, 0);
8173 /* init network clients */
8174 if (nb_net_clients
== 0) {
8175 /* if no clients, we use a default config */
8176 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
8178 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
8183 for(i
= 0;i
< nb_net_clients
; i
++) {
8184 if (net_client_init(net_clients
[i
]) < 0)
8187 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
8188 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
8190 if (vlan
->nb_guest_devs
== 0) {
8191 fprintf(stderr
, "Invalid vlan (%d) with no nics\n", vlan
->id
);
8194 if (vlan
->nb_host_devs
== 0)
8196 "Warning: vlan %d is not connected to host network\n",
8201 if (boot_device
== 'n') {
8202 for (i
= 0; i
< nb_nics
; i
++) {
8203 const char *model
= nd_table
[i
].model
;
8207 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
8208 if (get_image_size(buf
) > 0) {
8209 option_rom
[nb_option_roms
] = strdup(buf
);
8215 fprintf(stderr
, "No valid PXE rom found for network device\n");
8221 /* init the memory */
8222 phys_ram_size
= ram_size
+ vga_ram_size
+ MAX_BIOS_SIZE
;
8224 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
8225 if (!phys_ram_base
) {
8226 fprintf(stderr
, "Could not allocate physical memory\n");
8230 /* we always create the cdrom drive, even if no disk is there */
8232 if (cdrom_index
>= 0) {
8233 bs_table
[cdrom_index
] = bdrv_new("cdrom");
8234 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
8237 /* open the virtual block devices */
8238 for(i
= 0; i
< MAX_DISKS
; i
++) {
8239 if (hd_filename
[i
]) {
8242 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
8243 bs_table
[i
] = bdrv_new(buf
);
8245 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8246 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
8250 if (i
== 0 && cyls
!= 0) {
8251 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
8252 bdrv_set_translation_hint(bs_table
[i
], translation
);
8257 /* we always create at least one floppy disk */
8258 fd_table
[0] = bdrv_new("fda");
8259 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
8261 for(i
= 0; i
< MAX_FD
; i
++) {
8262 if (fd_filename
[i
]) {
8265 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
8266 fd_table
[i
] = bdrv_new(buf
);
8267 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
8269 if (fd_filename
[i
][0] != '\0') {
8270 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
8271 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8272 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
8280 /* Open the virtual parallel flash block devices */
8281 for(i
= 0; i
< MAX_PFLASH
; i
++) {
8282 if (pflash_filename
[i
]) {
8283 if (!pflash_table
[i
]) {
8285 snprintf(buf
, sizeof(buf
), "fl%c", i
+ 'a');
8286 pflash_table
[i
] = bdrv_new(buf
);
8288 if (bdrv_open(pflash_table
[i
], pflash_filename
[i
],
8289 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8290 fprintf(stderr
, "qemu: could not open flash image '%s'\n",
8291 pflash_filename
[i
]);
8297 sd_bdrv
= bdrv_new ("sd");
8298 /* FIXME: This isn't really a floppy, but it's a reasonable
8300 bdrv_set_type_hint(sd_bdrv
, BDRV_TYPE_FLOPPY
);
8302 if (bdrv_open(sd_bdrv
, sd_filename
,
8303 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8304 fprintf(stderr
, "qemu: could not open SD card image %s\n",
8307 qemu_key_check(sd_bdrv
, sd_filename
);
8311 mtd_bdrv
= bdrv_new ("mtd");
8312 if (bdrv_open(mtd_bdrv
, mtd_filename
,
8313 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0 ||
8314 qemu_key_check(mtd_bdrv
, mtd_filename
)) {
8315 fprintf(stderr
, "qemu: could not open Flash image %s\n",
8317 bdrv_delete(mtd_bdrv
);
8322 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
8323 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
8328 memset(&display_state
, 0, sizeof(display_state
));
8330 /* nearly nothing to do */
8331 dumb_display_init(ds
);
8332 } else if (vnc_display
!= NULL
) {
8333 vnc_display_init(ds
);
8334 if (vnc_display_open(ds
, vnc_display
) < 0)
8337 #if defined(CONFIG_SDL)
8338 sdl_display_init(ds
, full_screen
, no_frame
);
8339 #elif defined(CONFIG_COCOA)
8340 cocoa_display_init(ds
, full_screen
);
8344 /* Maintain compatibility with multiple stdio monitors */
8345 if (!strcmp(monitor_device
,"stdio")) {
8346 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
8347 if (!strcmp(serial_devices
[i
],"mon:stdio")) {
8348 monitor_device
[0] = '\0';
8350 } else if (!strcmp(serial_devices
[i
],"stdio")) {
8351 monitor_device
[0] = '\0';
8352 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "mon:stdio");
8357 if (monitor_device
[0] != '\0') {
8358 monitor_hd
= qemu_chr_open(monitor_device
);
8360 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
8363 monitor_init(monitor_hd
, !nographic
);
8366 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
8367 const char *devname
= serial_devices
[i
];
8368 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
8369 serial_hds
[i
] = qemu_chr_open(devname
);
8370 if (!serial_hds
[i
]) {
8371 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
8375 if (strstart(devname
, "vc", 0))
8376 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
8380 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
8381 const char *devname
= parallel_devices
[i
];
8382 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
8383 parallel_hds
[i
] = qemu_chr_open(devname
);
8384 if (!parallel_hds
[i
]) {
8385 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
8389 if (strstart(devname
, "vc", 0))
8390 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
8394 machine
->init(ram_size
, vga_ram_size
, boot_device
,
8395 ds
, fd_filename
, snapshot
,
8396 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
8398 /* init USB devices */
8400 for(i
= 0; i
< usb_devices_index
; i
++) {
8401 if (usb_device_add(usb_devices
[i
]) < 0) {
8402 fprintf(stderr
, "Warning: could not add USB device %s\n",
8408 if (display_state
.dpy_refresh
) {
8409 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
8410 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
8413 #ifdef CONFIG_GDBSTUB
8415 /* XXX: use standard host:port notation and modify options
8417 if (gdbserver_start(gdbstub_port
) < 0) {
8418 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
8429 /* XXX: simplify init */
8442 len
= write(fds
[1], &status
, 1);
8443 if (len
== -1 && (errno
== EINTR
))
8449 TFR(fd
= open("/dev/null", O_RDWR
));