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>
61 #include <linux/hpet.h>
62 #include <linux/ppdev.h>
63 #include <linux/parport.h>
66 #include <sys/ethernet.h>
67 #include <sys/sockio.h>
68 #include <netinet/arp.h>
69 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip.h>
72 #include <netinet/ip_icmp.h> // must come after ip.h
73 #include <netinet/udp.h>
74 #include <netinet/tcp.h>
82 int inet_aton(const char *cp
, struct in_addr
*ia
);
85 #if defined(CONFIG_SLIRP)
91 #include <sys/timeb.h>
93 #define getopt_long_only getopt_long
94 #define memalign(align, size) malloc(size)
97 #include "qemu_socket.h"
103 #endif /* CONFIG_SDL */
107 #define main qemu_main
108 #endif /* CONFIG_COCOA */
112 #include "exec-all.h"
114 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
116 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
118 #define SMBD_COMMAND "/usr/sbin/smbd"
121 //#define DEBUG_UNUSED_IOPORT
122 //#define DEBUG_IOPORT
124 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
127 #define DEFAULT_RAM_SIZE 144
129 #define DEFAULT_RAM_SIZE 128
132 #define GUI_REFRESH_INTERVAL 30
134 /* Max number of USB devices that can be specified on the commandline. */
135 #define MAX_USB_CMDLINE 8
137 /* XXX: use a two level table to limit memory usage */
138 #define MAX_IOPORTS 65536
140 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
141 char phys_ram_file
[1024];
142 void *ioport_opaque
[MAX_IOPORTS
];
143 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
144 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
145 /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available
146 to store the VM snapshots */
147 BlockDriverState
*bs_table
[MAX_DISKS
+ 1], *fd_table
[MAX_FD
];
148 BlockDriverState
*pflash_table
[MAX_PFLASH
];
149 BlockDriverState
*sd_bdrv
;
150 BlockDriverState
*mtd_bdrv
;
151 /* point to the block driver where the snapshots are managed */
152 BlockDriverState
*bs_snapshots
;
154 static DisplayState display_state
;
156 const char* keyboard_layout
= NULL
;
157 int64_t ticks_per_sec
;
158 int boot_device
= 'c';
160 int pit_min_timer_count
= 0;
162 NICInfo nd_table
[MAX_NICS
];
165 int cirrus_vga_enabled
= 1;
166 int vmsvga_enabled
= 0;
168 int graphic_width
= 1024;
169 int graphic_height
= 768;
170 int graphic_depth
= 8;
172 int graphic_width
= 800;
173 int graphic_height
= 600;
174 int graphic_depth
= 15;
179 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
180 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
182 int win2k_install_hack
= 0;
185 static VLANState
*first_vlan
;
187 const char *vnc_display
;
188 #if defined(TARGET_SPARC)
190 #elif defined(TARGET_I386)
195 int acpi_enabled
= 1;
199 int graphic_rotate
= 0;
201 const char *option_rom
[MAX_OPTION_ROMS
];
203 int semihosting_enabled
= 0;
208 const char *qemu_name
;
211 unsigned int nb_prom_envs
= 0;
212 const char *prom_envs
[MAX_PROM_ENVS
];
215 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
217 /***********************************************************/
218 /* x86 ISA bus support */
220 target_phys_addr_t isa_mem_base
= 0;
223 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
225 #ifdef DEBUG_UNUSED_IOPORT
226 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
231 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
233 #ifdef DEBUG_UNUSED_IOPORT
234 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
238 /* default is to make two byte accesses */
239 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
242 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
243 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
244 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
248 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
250 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
251 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
252 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
255 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
257 #ifdef DEBUG_UNUSED_IOPORT
258 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
263 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
265 #ifdef DEBUG_UNUSED_IOPORT
266 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
270 void init_ioports(void)
274 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
275 ioport_read_table
[0][i
] = default_ioport_readb
;
276 ioport_write_table
[0][i
] = default_ioport_writeb
;
277 ioport_read_table
[1][i
] = default_ioport_readw
;
278 ioport_write_table
[1][i
] = default_ioport_writew
;
279 ioport_read_table
[2][i
] = default_ioport_readl
;
280 ioport_write_table
[2][i
] = default_ioport_writel
;
284 /* size is the word size in byte */
285 int register_ioport_read(int start
, int length
, int size
,
286 IOPortReadFunc
*func
, void *opaque
)
292 } else if (size
== 2) {
294 } else if (size
== 4) {
297 hw_error("register_ioport_read: invalid size");
300 for(i
= start
; i
< start
+ length
; i
+= size
) {
301 ioport_read_table
[bsize
][i
] = func
;
302 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
303 hw_error("register_ioport_read: invalid opaque");
304 ioport_opaque
[i
] = opaque
;
309 /* size is the word size in byte */
310 int register_ioport_write(int start
, int length
, int size
,
311 IOPortWriteFunc
*func
, void *opaque
)
317 } else if (size
== 2) {
319 } else if (size
== 4) {
322 hw_error("register_ioport_write: invalid size");
325 for(i
= start
; i
< start
+ length
; i
+= size
) {
326 ioport_write_table
[bsize
][i
] = func
;
327 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
328 hw_error("register_ioport_write: invalid opaque");
329 ioport_opaque
[i
] = opaque
;
334 void isa_unassign_ioport(int start
, int length
)
338 for(i
= start
; i
< start
+ length
; i
++) {
339 ioport_read_table
[0][i
] = default_ioport_readb
;
340 ioport_read_table
[1][i
] = default_ioport_readw
;
341 ioport_read_table
[2][i
] = default_ioport_readl
;
343 ioport_write_table
[0][i
] = default_ioport_writeb
;
344 ioport_write_table
[1][i
] = default_ioport_writew
;
345 ioport_write_table
[2][i
] = default_ioport_writel
;
349 /***********************************************************/
351 void cpu_outb(CPUState
*env
, int addr
, int val
)
354 if (loglevel
& CPU_LOG_IOPORT
)
355 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
357 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
360 env
->last_io_time
= cpu_get_time_fast();
364 void cpu_outw(CPUState
*env
, int addr
, int val
)
367 if (loglevel
& CPU_LOG_IOPORT
)
368 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
370 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
373 env
->last_io_time
= cpu_get_time_fast();
377 void cpu_outl(CPUState
*env
, int addr
, int val
)
380 if (loglevel
& CPU_LOG_IOPORT
)
381 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
383 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
386 env
->last_io_time
= cpu_get_time_fast();
390 int cpu_inb(CPUState
*env
, int addr
)
393 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
395 if (loglevel
& CPU_LOG_IOPORT
)
396 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
400 env
->last_io_time
= cpu_get_time_fast();
405 int cpu_inw(CPUState
*env
, int addr
)
408 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
410 if (loglevel
& CPU_LOG_IOPORT
)
411 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
415 env
->last_io_time
= cpu_get_time_fast();
420 int cpu_inl(CPUState
*env
, int addr
)
423 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
425 if (loglevel
& CPU_LOG_IOPORT
)
426 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
430 env
->last_io_time
= cpu_get_time_fast();
435 /***********************************************************/
436 void hw_error(const char *fmt
, ...)
442 fprintf(stderr
, "qemu: hardware error: ");
443 vfprintf(stderr
, fmt
, ap
);
444 fprintf(stderr
, "\n");
445 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
446 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
448 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
450 cpu_dump_state(env
, stderr
, fprintf
, 0);
457 /***********************************************************/
460 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
461 static void *qemu_put_kbd_event_opaque
;
462 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
463 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
465 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
467 qemu_put_kbd_event_opaque
= opaque
;
468 qemu_put_kbd_event
= func
;
471 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
472 void *opaque
, int absolute
,
475 QEMUPutMouseEntry
*s
, *cursor
;
477 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
481 s
->qemu_put_mouse_event
= func
;
482 s
->qemu_put_mouse_event_opaque
= opaque
;
483 s
->qemu_put_mouse_event_absolute
= absolute
;
484 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
487 if (!qemu_put_mouse_event_head
) {
488 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
492 cursor
= qemu_put_mouse_event_head
;
493 while (cursor
->next
!= NULL
)
494 cursor
= cursor
->next
;
497 qemu_put_mouse_event_current
= s
;
502 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
504 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
506 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
509 cursor
= qemu_put_mouse_event_head
;
510 while (cursor
!= NULL
&& cursor
!= entry
) {
512 cursor
= cursor
->next
;
515 if (cursor
== NULL
) // does not exist or list empty
517 else if (prev
== NULL
) { // entry is head
518 qemu_put_mouse_event_head
= cursor
->next
;
519 if (qemu_put_mouse_event_current
== entry
)
520 qemu_put_mouse_event_current
= cursor
->next
;
521 qemu_free(entry
->qemu_put_mouse_event_name
);
526 prev
->next
= entry
->next
;
528 if (qemu_put_mouse_event_current
== entry
)
529 qemu_put_mouse_event_current
= prev
;
531 qemu_free(entry
->qemu_put_mouse_event_name
);
535 void kbd_put_keycode(int keycode
)
537 if (qemu_put_kbd_event
) {
538 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
542 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
544 QEMUPutMouseEvent
*mouse_event
;
545 void *mouse_event_opaque
;
548 if (!qemu_put_mouse_event_current
) {
553 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
555 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
558 if (graphic_rotate
) {
559 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
562 width
= graphic_width
;
563 mouse_event(mouse_event_opaque
,
564 width
- dy
, dx
, dz
, buttons_state
);
566 mouse_event(mouse_event_opaque
,
567 dx
, dy
, dz
, buttons_state
);
571 int kbd_mouse_is_absolute(void)
573 if (!qemu_put_mouse_event_current
)
576 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
579 void do_info_mice(void)
581 QEMUPutMouseEntry
*cursor
;
584 if (!qemu_put_mouse_event_head
) {
585 term_printf("No mouse devices connected\n");
589 term_printf("Mouse devices available:\n");
590 cursor
= qemu_put_mouse_event_head
;
591 while (cursor
!= NULL
) {
592 term_printf("%c Mouse #%d: %s\n",
593 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
594 index
, cursor
->qemu_put_mouse_event_name
);
596 cursor
= cursor
->next
;
600 void do_mouse_set(int index
)
602 QEMUPutMouseEntry
*cursor
;
605 if (!qemu_put_mouse_event_head
) {
606 term_printf("No mouse devices connected\n");
610 cursor
= qemu_put_mouse_event_head
;
611 while (cursor
!= NULL
&& index
!= i
) {
613 cursor
= cursor
->next
;
617 qemu_put_mouse_event_current
= cursor
;
619 term_printf("Mouse at given index not found\n");
622 /* compute with 96 bit intermediate result: (a*b)/c */
623 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
628 #ifdef WORDS_BIGENDIAN
638 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
639 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
642 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
646 /***********************************************************/
647 /* real time host monotonic timer */
649 #define QEMU_TIMER_BASE 1000000000LL
653 static int64_t clock_freq
;
655 static void init_get_clock(void)
659 ret
= QueryPerformanceFrequency(&freq
);
661 fprintf(stderr
, "Could not calibrate ticks\n");
664 clock_freq
= freq
.QuadPart
;
667 static int64_t get_clock(void)
670 QueryPerformanceCounter(&ti
);
671 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
676 static int use_rt_clock
;
678 static void init_get_clock(void)
681 #if defined(__linux__)
684 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
691 static int64_t get_clock(void)
693 #if defined(__linux__)
696 clock_gettime(CLOCK_MONOTONIC
, &ts
);
697 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
701 /* XXX: using gettimeofday leads to problems if the date
702 changes, so it should be avoided. */
704 gettimeofday(&tv
, NULL
);
705 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
711 /***********************************************************/
712 /* guest cycle counter */
714 static int64_t cpu_ticks_prev
;
715 static int64_t cpu_ticks_offset
;
716 static int64_t cpu_clock_offset
;
717 static int cpu_ticks_enabled
;
719 /* return the host CPU cycle counter and handle stop/restart */
720 int64_t cpu_get_ticks(void)
722 if (!cpu_ticks_enabled
) {
723 return cpu_ticks_offset
;
726 ticks
= cpu_get_real_ticks();
727 if (cpu_ticks_prev
> ticks
) {
728 /* Note: non increasing ticks may happen if the host uses
730 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
732 cpu_ticks_prev
= ticks
;
733 return ticks
+ cpu_ticks_offset
;
737 /* return the host CPU monotonic timer and handle stop/restart */
738 static int64_t cpu_get_clock(void)
741 if (!cpu_ticks_enabled
) {
742 return cpu_clock_offset
;
745 return ti
+ cpu_clock_offset
;
749 /* enable cpu_get_ticks() */
750 void cpu_enable_ticks(void)
752 if (!cpu_ticks_enabled
) {
753 cpu_ticks_offset
-= cpu_get_real_ticks();
754 cpu_clock_offset
-= get_clock();
755 cpu_ticks_enabled
= 1;
759 /* disable cpu_get_ticks() : the clock is stopped. You must not call
760 cpu_get_ticks() after that. */
761 void cpu_disable_ticks(void)
763 if (cpu_ticks_enabled
) {
764 cpu_ticks_offset
= cpu_get_ticks();
765 cpu_clock_offset
= cpu_get_clock();
766 cpu_ticks_enabled
= 0;
770 /***********************************************************/
773 #define QEMU_TIMER_REALTIME 0
774 #define QEMU_TIMER_VIRTUAL 1
778 /* XXX: add frequency */
786 struct QEMUTimer
*next
;
789 struct qemu_alarm_timer
{
793 int (*start
)(struct qemu_alarm_timer
*t
);
794 void (*stop
)(struct qemu_alarm_timer
*t
);
795 void (*rearm
)(struct qemu_alarm_timer
*t
);
799 #define ALARM_FLAG_DYNTICKS 0x1
801 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
803 return t
->flags
& ALARM_FLAG_DYNTICKS
;
806 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
808 if (!alarm_has_dynticks(t
))
814 /* TODO: MIN_TIMER_REARM_US should be optimized */
815 #define MIN_TIMER_REARM_US 250
817 static struct qemu_alarm_timer
*alarm_timer
;
821 struct qemu_alarm_win32
{
825 } alarm_win32_data
= {0, NULL
, -1};
827 static int win32_start_timer(struct qemu_alarm_timer
*t
);
828 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
829 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
833 static int unix_start_timer(struct qemu_alarm_timer
*t
);
834 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
838 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
839 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
840 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
842 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
843 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
845 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
846 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
848 #endif /* __linux__ */
852 static struct qemu_alarm_timer alarm_timers
[] = {
855 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
856 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
857 /* HPET - if available - is preferred */
858 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
859 /* ...otherwise try RTC */
860 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
862 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
864 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
865 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
866 {"win32", 0, win32_start_timer
,
867 win32_stop_timer
, NULL
, &alarm_win32_data
},
872 static void show_available_alarms()
876 printf("Available alarm timers, in order of precedence:\n");
877 for (i
= 0; alarm_timers
[i
].name
; i
++)
878 printf("%s\n", alarm_timers
[i
].name
);
881 static void configure_alarms(char const *opt
)
885 int count
= (sizeof(alarm_timers
) / sizeof(*alarm_timers
)) - 1;
889 if (!strcmp(opt
, "help")) {
890 show_available_alarms();
896 /* Reorder the array */
897 name
= strtok(arg
, ",");
899 struct qemu_alarm_timer tmp
;
901 for (i
= 0; i
< count
; i
++) {
902 if (!strcmp(alarm_timers
[i
].name
, name
))
907 fprintf(stderr
, "Unknown clock %s\n", name
);
916 tmp
= alarm_timers
[i
];
917 alarm_timers
[i
] = alarm_timers
[cur
];
918 alarm_timers
[cur
] = tmp
;
922 name
= strtok(NULL
, ",");
928 /* Disable remaining timers */
929 for (i
= cur
; i
< count
; i
++)
930 alarm_timers
[i
].name
= NULL
;
934 show_available_alarms();
940 static QEMUTimer
*active_timers
[2];
942 QEMUClock
*qemu_new_clock(int type
)
945 clock
= qemu_mallocz(sizeof(QEMUClock
));
952 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
956 ts
= qemu_mallocz(sizeof(QEMUTimer
));
963 void qemu_free_timer(QEMUTimer
*ts
)
968 /* stop a timer, but do not dealloc it */
969 void qemu_del_timer(QEMUTimer
*ts
)
973 /* NOTE: this code must be signal safe because
974 qemu_timer_expired() can be called from a signal. */
975 pt
= &active_timers
[ts
->clock
->type
];
987 qemu_rearm_alarm_timer(alarm_timer
);
990 /* modify the current timer so that it will be fired when current_time
991 >= expire_time. The corresponding callback will be called. */
992 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
998 /* add the timer in the sorted list */
999 /* NOTE: this code must be signal safe because
1000 qemu_timer_expired() can be called from a signal. */
1001 pt
= &active_timers
[ts
->clock
->type
];
1006 if (t
->expire_time
> expire_time
)
1010 ts
->expire_time
= expire_time
;
1015 int qemu_timer_pending(QEMUTimer
*ts
)
1018 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1025 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1029 return (timer_head
->expire_time
<= current_time
);
1032 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1038 if (!ts
|| ts
->expire_time
> current_time
)
1040 /* remove timer from the list before calling the callback */
1041 *ptimer_head
= ts
->next
;
1044 /* run the callback (the timer list can be modified) */
1047 qemu_rearm_alarm_timer(alarm_timer
);
1050 int64_t qemu_get_clock(QEMUClock
*clock
)
1052 switch(clock
->type
) {
1053 case QEMU_TIMER_REALTIME
:
1054 return get_clock() / 1000000;
1056 case QEMU_TIMER_VIRTUAL
:
1057 return cpu_get_clock();
1061 static void init_timers(void)
1064 ticks_per_sec
= QEMU_TIMER_BASE
;
1065 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1066 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1070 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1072 uint64_t expire_time
;
1074 if (qemu_timer_pending(ts
)) {
1075 expire_time
= ts
->expire_time
;
1079 qemu_put_be64(f
, expire_time
);
1082 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1084 uint64_t expire_time
;
1086 expire_time
= qemu_get_be64(f
);
1087 if (expire_time
!= -1) {
1088 qemu_mod_timer(ts
, expire_time
);
1094 static void timer_save(QEMUFile
*f
, void *opaque
)
1096 if (cpu_ticks_enabled
) {
1097 hw_error("cannot save state if virtual timers are running");
1099 qemu_put_be64s(f
, &cpu_ticks_offset
);
1100 qemu_put_be64s(f
, &ticks_per_sec
);
1101 qemu_put_be64s(f
, &cpu_clock_offset
);
1104 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1106 if (version_id
!= 1 && version_id
!= 2)
1108 if (cpu_ticks_enabled
) {
1111 qemu_get_be64s(f
, &cpu_ticks_offset
);
1112 qemu_get_be64s(f
, &ticks_per_sec
);
1113 if (version_id
== 2) {
1114 qemu_get_be64s(f
, &cpu_clock_offset
);
1120 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1121 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1123 static void host_alarm_handler(int host_signum
)
1127 #define DISP_FREQ 1000
1129 static int64_t delta_min
= INT64_MAX
;
1130 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1132 ti
= qemu_get_clock(vm_clock
);
1133 if (last_clock
!= 0) {
1134 delta
= ti
- last_clock
;
1135 if (delta
< delta_min
)
1137 if (delta
> delta_max
)
1140 if (++count
== DISP_FREQ
) {
1141 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1142 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1143 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1144 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1145 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1147 delta_min
= INT64_MAX
;
1155 if (alarm_has_dynticks(alarm_timer
) ||
1156 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1157 qemu_get_clock(vm_clock
)) ||
1158 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1159 qemu_get_clock(rt_clock
))) {
1161 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1162 SetEvent(data
->host_alarm
);
1164 CPUState
*env
= cpu_single_env
;
1166 /* stop the currently executing cpu because a timer occured */
1167 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1169 if (env
->kqemu_enabled
) {
1170 kqemu_cpu_interrupt(env
);
1177 static uint64_t qemu_next_deadline(void)
1179 int64_t nearest_delta_us
= UINT64_MAX
;
1182 if (active_timers
[QEMU_TIMER_REALTIME
])
1183 nearest_delta_us
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1184 qemu_get_clock(rt_clock
))*1000;
1186 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1188 vmdelta_us
= (active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1189 qemu_get_clock(vm_clock
)+999)/1000;
1190 if (vmdelta_us
< nearest_delta_us
)
1191 nearest_delta_us
= vmdelta_us
;
1194 /* Avoid arming the timer to negative, zero, or too low values */
1195 if (nearest_delta_us
<= MIN_TIMER_REARM_US
)
1196 nearest_delta_us
= MIN_TIMER_REARM_US
;
1198 return nearest_delta_us
;
1203 #if defined(__linux__)
1205 #define RTC_FREQ 1024
1207 static void enable_sigio_timer(int fd
)
1209 struct sigaction act
;
1212 sigfillset(&act
.sa_mask
);
1214 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1215 act
.sa_flags
|= SA_ONSTACK
;
1217 act
.sa_handler
= host_alarm_handler
;
1219 sigaction(SIGIO
, &act
, NULL
);
1220 fcntl(fd
, F_SETFL
, O_ASYNC
);
1221 fcntl(fd
, F_SETOWN
, getpid());
1224 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1226 struct hpet_info info
;
1229 fd
= open("/dev/hpet", O_RDONLY
);
1234 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1236 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1237 "error, but for better emulation accuracy type:\n"
1238 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1242 /* Check capabilities */
1243 r
= ioctl(fd
, HPET_INFO
, &info
);
1247 /* Enable periodic mode */
1248 r
= ioctl(fd
, HPET_EPI
, 0);
1249 if (info
.hi_flags
&& (r
< 0))
1252 /* Enable interrupt */
1253 r
= ioctl(fd
, HPET_IE_ON
, 0);
1257 enable_sigio_timer(fd
);
1258 t
->priv
= (void *)(long)fd
;
1266 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1268 int fd
= (long)t
->priv
;
1273 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1277 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1280 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1281 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1282 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1283 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1286 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1292 enable_sigio_timer(rtc_fd
);
1294 t
->priv
= (void *)(long)rtc_fd
;
1299 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1301 int rtc_fd
= (long)t
->priv
;
1306 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1310 struct sigaction act
;
1312 sigfillset(&act
.sa_mask
);
1314 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
1315 act
.sa_flags
|= SA_ONSTACK
;
1317 act
.sa_handler
= host_alarm_handler
;
1319 sigaction(SIGALRM
, &act
, NULL
);
1321 ev
.sigev_value
.sival_int
= 0;
1322 ev
.sigev_notify
= SIGEV_SIGNAL
;
1323 ev
.sigev_signo
= SIGALRM
;
1325 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1326 perror("timer_create");
1328 /* disable dynticks */
1329 fprintf(stderr
, "Dynamic Ticks disabled\n");
1334 t
->priv
= (void *)host_timer
;
1339 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1341 timer_t host_timer
= (timer_t
)t
->priv
;
1343 timer_delete(host_timer
);
1346 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1348 timer_t host_timer
= (timer_t
)t
->priv
;
1349 struct itimerspec timeout
;
1350 int64_t nearest_delta_us
= INT64_MAX
;
1353 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1354 !active_timers
[QEMU_TIMER_VIRTUAL
])
1357 nearest_delta_us
= qemu_next_deadline();
1359 /* check whether a timer is already running */
1360 if (timer_gettime(host_timer
, &timeout
)) {
1362 fprintf(stderr
, "Internal timer error: aborting\n");
1365 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1366 if (current_us
&& current_us
<= nearest_delta_us
)
1369 timeout
.it_interval
.tv_sec
= 0;
1370 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1371 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1372 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1373 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1375 fprintf(stderr
, "Internal timer error: aborting\n");
1380 #endif /* defined(__linux__) */
1382 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1384 struct sigaction act
;
1385 struct itimerval itv
;
1389 sigfillset(&act
.sa_mask
);
1391 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
1392 act
.sa_flags
|= SA_ONSTACK
;
1394 act
.sa_handler
= host_alarm_handler
;
1396 sigaction(SIGALRM
, &act
, NULL
);
1398 itv
.it_interval
.tv_sec
= 0;
1399 /* for i386 kernel 2.6 to get 1 ms */
1400 itv
.it_interval
.tv_usec
= 999;
1401 itv
.it_value
.tv_sec
= 0;
1402 itv
.it_value
.tv_usec
= 10 * 1000;
1404 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1411 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1413 struct itimerval itv
;
1415 memset(&itv
, 0, sizeof(itv
));
1416 setitimer(ITIMER_REAL
, &itv
, NULL
);
1419 #endif /* !defined(_WIN32) */
1423 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1426 struct qemu_alarm_win32
*data
= t
->priv
;
1429 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1430 if (!data
->host_alarm
) {
1431 perror("Failed CreateEvent");
1435 memset(&tc
, 0, sizeof(tc
));
1436 timeGetDevCaps(&tc
, sizeof(tc
));
1438 if (data
->period
< tc
.wPeriodMin
)
1439 data
->period
= tc
.wPeriodMin
;
1441 timeBeginPeriod(data
->period
);
1443 flags
= TIME_CALLBACK_FUNCTION
;
1444 if (alarm_has_dynticks(t
))
1445 flags
|= TIME_ONESHOT
;
1447 flags
|= TIME_PERIODIC
;
1449 data
->timerId
= timeSetEvent(1, // interval (ms)
1450 data
->period
, // resolution
1451 host_alarm_handler
, // function
1452 (DWORD
)t
, // parameter
1455 if (!data
->timerId
) {
1456 perror("Failed to initialize win32 alarm timer");
1458 timeEndPeriod(data
->period
);
1459 CloseHandle(data
->host_alarm
);
1463 qemu_add_wait_object(data
->host_alarm
, NULL
, NULL
);
1468 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1470 struct qemu_alarm_win32
*data
= t
->priv
;
1472 timeKillEvent(data
->timerId
);
1473 timeEndPeriod(data
->period
);
1475 CloseHandle(data
->host_alarm
);
1478 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1480 struct qemu_alarm_win32
*data
= t
->priv
;
1481 uint64_t nearest_delta_us
;
1483 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1484 !active_timers
[QEMU_TIMER_VIRTUAL
])
1487 nearest_delta_us
= qemu_next_deadline();
1488 nearest_delta_us
/= 1000;
1490 timeKillEvent(data
->timerId
);
1492 data
->timerId
= timeSetEvent(1,
1496 TIME_ONESHOT
| TIME_PERIODIC
);
1498 if (!data
->timerId
) {
1499 perror("Failed to re-arm win32 alarm timer");
1501 timeEndPeriod(data
->period
);
1502 CloseHandle(data
->host_alarm
);
1509 static void init_timer_alarm(void)
1511 struct qemu_alarm_timer
*t
;
1514 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1515 t
= &alarm_timers
[i
];
1523 fprintf(stderr
, "Unable to find any suitable alarm timer.\n");
1524 fprintf(stderr
, "Terminating\n");
1531 void quit_timers(void)
1533 alarm_timer
->stop(alarm_timer
);
1537 /***********************************************************/
1538 /* character device */
1540 static void qemu_chr_event(CharDriverState
*s
, int event
)
1544 s
->chr_event(s
->handler_opaque
, event
);
1547 static void qemu_chr_reset_bh(void *opaque
)
1549 CharDriverState
*s
= opaque
;
1550 qemu_chr_event(s
, CHR_EVENT_RESET
);
1551 qemu_bh_delete(s
->bh
);
1555 void qemu_chr_reset(CharDriverState
*s
)
1557 if (s
->bh
== NULL
) {
1558 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
1559 qemu_bh_schedule(s
->bh
);
1563 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1565 return s
->chr_write(s
, buf
, len
);
1568 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1572 return s
->chr_ioctl(s
, cmd
, arg
);
1575 int qemu_chr_can_read(CharDriverState
*s
)
1577 if (!s
->chr_can_read
)
1579 return s
->chr_can_read(s
->handler_opaque
);
1582 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
1584 s
->chr_read(s
->handler_opaque
, buf
, len
);
1588 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1593 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1594 qemu_chr_write(s
, buf
, strlen(buf
));
1598 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1600 if (s
->chr_send_event
)
1601 s
->chr_send_event(s
, event
);
1604 void qemu_chr_add_handlers(CharDriverState
*s
,
1605 IOCanRWHandler
*fd_can_read
,
1606 IOReadHandler
*fd_read
,
1607 IOEventHandler
*fd_event
,
1610 s
->chr_can_read
= fd_can_read
;
1611 s
->chr_read
= fd_read
;
1612 s
->chr_event
= fd_event
;
1613 s
->handler_opaque
= opaque
;
1614 if (s
->chr_update_read_handler
)
1615 s
->chr_update_read_handler(s
);
1618 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1623 static CharDriverState
*qemu_chr_open_null(void)
1625 CharDriverState
*chr
;
1627 chr
= qemu_mallocz(sizeof(CharDriverState
));
1630 chr
->chr_write
= null_chr_write
;
1634 /* MUX driver for serial I/O splitting */
1635 static int term_timestamps
;
1636 static int64_t term_timestamps_start
;
1639 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
1640 IOReadHandler
*chr_read
[MAX_MUX
];
1641 IOEventHandler
*chr_event
[MAX_MUX
];
1642 void *ext_opaque
[MAX_MUX
];
1643 CharDriverState
*drv
;
1645 int term_got_escape
;
1650 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1652 MuxDriver
*d
= chr
->opaque
;
1654 if (!term_timestamps
) {
1655 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
1660 for(i
= 0; i
< len
; i
++) {
1661 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
1662 if (buf
[i
] == '\n') {
1668 if (term_timestamps_start
== -1)
1669 term_timestamps_start
= ti
;
1670 ti
-= term_timestamps_start
;
1671 secs
= ti
/ 1000000000;
1672 snprintf(buf1
, sizeof(buf1
),
1673 "[%02d:%02d:%02d.%03d] ",
1677 (int)((ti
/ 1000000) % 1000));
1678 d
->drv
->chr_write(d
->drv
, buf1
, strlen(buf1
));
1685 static char *mux_help
[] = {
1686 "% h print this help\n\r",
1687 "% x exit emulator\n\r",
1688 "% s save disk data back to file (if -snapshot)\n\r",
1689 "% t toggle console timestamps\n\r"
1690 "% b send break (magic sysrq)\n\r",
1691 "% c switch between console and monitor\n\r",
1696 static int term_escape_char
= 0x01; /* ctrl-a is used for escape */
1697 static void mux_print_help(CharDriverState
*chr
)
1700 char ebuf
[15] = "Escape-Char";
1701 char cbuf
[50] = "\n\r";
1703 if (term_escape_char
> 0 && term_escape_char
< 26) {
1704 sprintf(cbuf
,"\n\r");
1705 sprintf(ebuf
,"C-%c", term_escape_char
- 1 + 'a');
1707 sprintf(cbuf
,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char
);
1709 chr
->chr_write(chr
, cbuf
, strlen(cbuf
));
1710 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
1711 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
1712 if (mux_help
[i
][j
] == '%')
1713 chr
->chr_write(chr
, ebuf
, strlen(ebuf
));
1715 chr
->chr_write(chr
, &mux_help
[i
][j
], 1);
1720 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
1722 if (d
->term_got_escape
) {
1723 d
->term_got_escape
= 0;
1724 if (ch
== term_escape_char
)
1729 mux_print_help(chr
);
1733 char *term
= "QEMU: Terminated\n\r";
1734 chr
->chr_write(chr
,term
,strlen(term
));
1741 for (i
= 0; i
< MAX_DISKS
; i
++) {
1743 bdrv_commit(bs_table
[i
]);
1746 bdrv_commit(mtd_bdrv
);
1750 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1753 /* Switch to the next registered device */
1755 if (chr
->focus
>= d
->mux_cnt
)
1759 term_timestamps
= !term_timestamps
;
1760 term_timestamps_start
= -1;
1763 } else if (ch
== term_escape_char
) {
1764 d
->term_got_escape
= 1;
1772 static int mux_chr_can_read(void *opaque
)
1774 CharDriverState
*chr
= opaque
;
1775 MuxDriver
*d
= chr
->opaque
;
1776 if (d
->chr_can_read
[chr
->focus
])
1777 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
1781 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
1783 CharDriverState
*chr
= opaque
;
1784 MuxDriver
*d
= chr
->opaque
;
1786 for(i
= 0; i
< size
; i
++)
1787 if (mux_proc_byte(chr
, d
, buf
[i
]))
1788 d
->chr_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
], &buf
[i
], 1);
1791 static void mux_chr_event(void *opaque
, int event
)
1793 CharDriverState
*chr
= opaque
;
1794 MuxDriver
*d
= chr
->opaque
;
1797 /* Send the event to all registered listeners */
1798 for (i
= 0; i
< d
->mux_cnt
; i
++)
1799 if (d
->chr_event
[i
])
1800 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
1803 static void mux_chr_update_read_handler(CharDriverState
*chr
)
1805 MuxDriver
*d
= chr
->opaque
;
1807 if (d
->mux_cnt
>= MAX_MUX
) {
1808 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
1811 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
1812 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
1813 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
1814 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
1815 /* Fix up the real driver with mux routines */
1816 if (d
->mux_cnt
== 0) {
1817 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
1818 mux_chr_event
, chr
);
1820 chr
->focus
= d
->mux_cnt
;
1824 CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
1826 CharDriverState
*chr
;
1829 chr
= qemu_mallocz(sizeof(CharDriverState
));
1832 d
= qemu_mallocz(sizeof(MuxDriver
));
1841 chr
->chr_write
= mux_chr_write
;
1842 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
1849 static void socket_cleanup(void)
1854 static int socket_init(void)
1859 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1861 err
= WSAGetLastError();
1862 fprintf(stderr
, "WSAStartup: %d\n", err
);
1865 atexit(socket_cleanup
);
1869 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1875 ret
= send(fd
, buf
, len
, 0);
1878 errno
= WSAGetLastError();
1879 if (errno
!= WSAEWOULDBLOCK
) {
1882 } else if (ret
== 0) {
1892 void socket_set_nonblock(int fd
)
1894 unsigned long opt
= 1;
1895 ioctlsocket(fd
, FIONBIO
, &opt
);
1900 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1906 ret
= write(fd
, buf
, len
);
1908 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1910 } else if (ret
== 0) {
1920 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1922 return unix_write(fd
, buf
, len1
);
1925 void socket_set_nonblock(int fd
)
1927 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1929 #endif /* !_WIN32 */
1938 #define STDIO_MAX_CLIENTS 1
1939 static int stdio_nb_clients
= 0;
1941 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1943 FDCharDriver
*s
= chr
->opaque
;
1944 return unix_write(s
->fd_out
, buf
, len
);
1947 static int fd_chr_read_poll(void *opaque
)
1949 CharDriverState
*chr
= opaque
;
1950 FDCharDriver
*s
= chr
->opaque
;
1952 s
->max_size
= qemu_chr_can_read(chr
);
1956 static void fd_chr_read(void *opaque
)
1958 CharDriverState
*chr
= opaque
;
1959 FDCharDriver
*s
= chr
->opaque
;
1964 if (len
> s
->max_size
)
1968 size
= read(s
->fd_in
, buf
, len
);
1970 /* FD has been closed. Remove it from the active list. */
1971 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1975 qemu_chr_read(chr
, buf
, size
);
1979 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1981 FDCharDriver
*s
= chr
->opaque
;
1983 if (s
->fd_in
>= 0) {
1984 if (nographic
&& s
->fd_in
== 0) {
1986 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1987 fd_chr_read
, NULL
, chr
);
1992 /* open a character device to a unix fd */
1993 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1995 CharDriverState
*chr
;
1998 chr
= qemu_mallocz(sizeof(CharDriverState
));
2001 s
= qemu_mallocz(sizeof(FDCharDriver
));
2009 chr
->chr_write
= fd_chr_write
;
2010 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
2012 qemu_chr_reset(chr
);
2017 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
2021 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
2024 return qemu_chr_open_fd(-1, fd_out
);
2027 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
2030 char filename_in
[256], filename_out
[256];
2032 snprintf(filename_in
, 256, "%s.in", filename
);
2033 snprintf(filename_out
, 256, "%s.out", filename
);
2034 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
2035 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
2036 if (fd_in
< 0 || fd_out
< 0) {
2041 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
2045 return qemu_chr_open_fd(fd_in
, fd_out
);
2049 /* for STDIO, we handle the case where several clients use it
2052 #define TERM_FIFO_MAX_SIZE 1
2054 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
2055 static int term_fifo_size
;
2057 static int stdio_read_poll(void *opaque
)
2059 CharDriverState
*chr
= opaque
;
2061 /* try to flush the queue if needed */
2062 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
2063 qemu_chr_read(chr
, term_fifo
, 1);
2066 /* see if we can absorb more chars */
2067 if (term_fifo_size
== 0)
2073 static void stdio_read(void *opaque
)
2077 CharDriverState
*chr
= opaque
;
2079 size
= read(0, buf
, 1);
2081 /* stdin has been closed. Remove it from the active list. */
2082 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
2086 if (qemu_chr_can_read(chr
) > 0) {
2087 qemu_chr_read(chr
, buf
, 1);
2088 } else if (term_fifo_size
== 0) {
2089 term_fifo
[term_fifo_size
++] = buf
[0];
2094 /* init terminal so that we can grab keys */
2095 static struct termios oldtty
;
2096 static int old_fd0_flags
;
2098 static void term_exit(void)
2100 tcsetattr (0, TCSANOW
, &oldtty
);
2101 fcntl(0, F_SETFL
, old_fd0_flags
);
2104 static void term_init(void)
2108 tcgetattr (0, &tty
);
2110 old_fd0_flags
= fcntl(0, F_GETFL
);
2112 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
2113 |INLCR
|IGNCR
|ICRNL
|IXON
);
2114 tty
.c_oflag
|= OPOST
;
2115 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
2116 /* if graphical mode, we allow Ctrl-C handling */
2118 tty
.c_lflag
&= ~ISIG
;
2119 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
2122 tty
.c_cc
[VTIME
] = 0;
2124 tcsetattr (0, TCSANOW
, &tty
);
2128 fcntl(0, F_SETFL
, O_NONBLOCK
);
2131 static CharDriverState
*qemu_chr_open_stdio(void)
2133 CharDriverState
*chr
;
2135 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
2137 chr
= qemu_chr_open_fd(0, 1);
2138 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
2145 #if defined(__linux__) || defined(__sun__)
2146 static CharDriverState
*qemu_chr_open_pty(void)
2149 char slave_name
[1024];
2150 int master_fd
, slave_fd
;
2152 #if defined(__linux__)
2153 /* Not satisfying */
2154 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
2159 /* Disabling local echo and line-buffered output */
2160 tcgetattr (master_fd
, &tty
);
2161 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
2163 tty
.c_cc
[VTIME
] = 0;
2164 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
2166 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
2167 return qemu_chr_open_fd(master_fd
, master_fd
);
2170 static void tty_serial_init(int fd
, int speed
,
2171 int parity
, int data_bits
, int stop_bits
)
2177 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
2178 speed
, parity
, data_bits
, stop_bits
);
2180 tcgetattr (fd
, &tty
);
2222 cfsetispeed(&tty
, spd
);
2223 cfsetospeed(&tty
, spd
);
2225 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
2226 |INLCR
|IGNCR
|ICRNL
|IXON
);
2227 tty
.c_oflag
|= OPOST
;
2228 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
2229 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
2250 tty
.c_cflag
|= PARENB
;
2253 tty
.c_cflag
|= PARENB
| PARODD
;
2257 tty
.c_cflag
|= CSTOPB
;
2259 tcsetattr (fd
, TCSANOW
, &tty
);
2262 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2264 FDCharDriver
*s
= chr
->opaque
;
2267 case CHR_IOCTL_SERIAL_SET_PARAMS
:
2269 QEMUSerialSetParams
*ssp
= arg
;
2270 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
2271 ssp
->data_bits
, ssp
->stop_bits
);
2274 case CHR_IOCTL_SERIAL_SET_BREAK
:
2276 int enable
= *(int *)arg
;
2278 tcsendbreak(s
->fd_in
, 1);
2287 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
2289 CharDriverState
*chr
;
2292 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
2293 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2294 tty_serial_init(fd
, 115200, 'N', 8, 1);
2295 chr
= qemu_chr_open_fd(fd
, fd
);
2300 chr
->chr_ioctl
= tty_serial_ioctl
;
2301 qemu_chr_reset(chr
);
2304 #else /* ! __linux__ && ! __sun__ */
2305 static CharDriverState
*qemu_chr_open_pty(void)
2309 #endif /* __linux__ || __sun__ */
2311 #if defined(__linux__)
2315 } ParallelCharDriver
;
2317 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
2319 if (s
->mode
!= mode
) {
2321 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
2328 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2330 ParallelCharDriver
*drv
= chr
->opaque
;
2335 case CHR_IOCTL_PP_READ_DATA
:
2336 if (ioctl(fd
, PPRDATA
, &b
) < 0)
2338 *(uint8_t *)arg
= b
;
2340 case CHR_IOCTL_PP_WRITE_DATA
:
2341 b
= *(uint8_t *)arg
;
2342 if (ioctl(fd
, PPWDATA
, &b
) < 0)
2345 case CHR_IOCTL_PP_READ_CONTROL
:
2346 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
2348 /* Linux gives only the lowest bits, and no way to know data
2349 direction! For better compatibility set the fixed upper
2351 *(uint8_t *)arg
= b
| 0xc0;
2353 case CHR_IOCTL_PP_WRITE_CONTROL
:
2354 b
= *(uint8_t *)arg
;
2355 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
2358 case CHR_IOCTL_PP_READ_STATUS
:
2359 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
2361 *(uint8_t *)arg
= b
;
2363 case CHR_IOCTL_PP_EPP_READ_ADDR
:
2364 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2365 struct ParallelIOArg
*parg
= arg
;
2366 int n
= read(fd
, parg
->buffer
, parg
->count
);
2367 if (n
!= parg
->count
) {
2372 case CHR_IOCTL_PP_EPP_READ
:
2373 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2374 struct ParallelIOArg
*parg
= arg
;
2375 int n
= read(fd
, parg
->buffer
, parg
->count
);
2376 if (n
!= parg
->count
) {
2381 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
2382 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2383 struct ParallelIOArg
*parg
= arg
;
2384 int n
= write(fd
, parg
->buffer
, parg
->count
);
2385 if (n
!= parg
->count
) {
2390 case CHR_IOCTL_PP_EPP_WRITE
:
2391 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2392 struct ParallelIOArg
*parg
= arg
;
2393 int n
= write(fd
, parg
->buffer
, parg
->count
);
2394 if (n
!= parg
->count
) {
2405 static void pp_close(CharDriverState
*chr
)
2407 ParallelCharDriver
*drv
= chr
->opaque
;
2410 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
2411 ioctl(fd
, PPRELEASE
);
2416 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
2418 CharDriverState
*chr
;
2419 ParallelCharDriver
*drv
;
2422 TFR(fd
= open(filename
, O_RDWR
));
2426 if (ioctl(fd
, PPCLAIM
) < 0) {
2431 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
2437 drv
->mode
= IEEE1284_MODE_COMPAT
;
2439 chr
= qemu_mallocz(sizeof(CharDriverState
));
2445 chr
->chr_write
= null_chr_write
;
2446 chr
->chr_ioctl
= pp_ioctl
;
2447 chr
->chr_close
= pp_close
;
2450 qemu_chr_reset(chr
);
2454 #endif /* __linux__ */
2460 HANDLE hcom
, hrecv
, hsend
;
2461 OVERLAPPED orecv
, osend
;
2466 #define NSENDBUF 2048
2467 #define NRECVBUF 2048
2468 #define MAXCONNECT 1
2469 #define NTIMEOUT 5000
2471 static int win_chr_poll(void *opaque
);
2472 static int win_chr_pipe_poll(void *opaque
);
2474 static void win_chr_close(CharDriverState
*chr
)
2476 WinCharState
*s
= chr
->opaque
;
2479 CloseHandle(s
->hsend
);
2483 CloseHandle(s
->hrecv
);
2487 CloseHandle(s
->hcom
);
2491 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
2493 qemu_del_polling_cb(win_chr_poll
, chr
);
2496 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
2498 WinCharState
*s
= chr
->opaque
;
2500 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2505 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2507 fprintf(stderr
, "Failed CreateEvent\n");
2510 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2512 fprintf(stderr
, "Failed CreateEvent\n");
2516 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2517 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2518 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2519 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
2524 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2525 fprintf(stderr
, "Failed SetupComm\n");
2529 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2530 size
= sizeof(COMMCONFIG
);
2531 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2532 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2533 CommConfigDialog(filename
, NULL
, &comcfg
);
2535 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2536 fprintf(stderr
, "Failed SetCommState\n");
2540 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2541 fprintf(stderr
, "Failed SetCommMask\n");
2545 cto
.ReadIntervalTimeout
= MAXDWORD
;
2546 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2547 fprintf(stderr
, "Failed SetCommTimeouts\n");
2551 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2552 fprintf(stderr
, "Failed ClearCommError\n");
2555 qemu_add_polling_cb(win_chr_poll
, chr
);
2563 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2565 WinCharState
*s
= chr
->opaque
;
2566 DWORD len
, ret
, size
, err
;
2569 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2570 s
->osend
.hEvent
= s
->hsend
;
2573 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2575 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2577 err
= GetLastError();
2578 if (err
== ERROR_IO_PENDING
) {
2579 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2597 static int win_chr_read_poll(CharDriverState
*chr
)
2599 WinCharState
*s
= chr
->opaque
;
2601 s
->max_size
= qemu_chr_can_read(chr
);
2605 static void win_chr_readfile(CharDriverState
*chr
)
2607 WinCharState
*s
= chr
->opaque
;
2612 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2613 s
->orecv
.hEvent
= s
->hrecv
;
2614 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2616 err
= GetLastError();
2617 if (err
== ERROR_IO_PENDING
) {
2618 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2623 qemu_chr_read(chr
, buf
, size
);
2627 static void win_chr_read(CharDriverState
*chr
)
2629 WinCharState
*s
= chr
->opaque
;
2631 if (s
->len
> s
->max_size
)
2632 s
->len
= s
->max_size
;
2636 win_chr_readfile(chr
);
2639 static int win_chr_poll(void *opaque
)
2641 CharDriverState
*chr
= opaque
;
2642 WinCharState
*s
= chr
->opaque
;
2646 ClearCommError(s
->hcom
, &comerr
, &status
);
2647 if (status
.cbInQue
> 0) {
2648 s
->len
= status
.cbInQue
;
2649 win_chr_read_poll(chr
);
2656 static CharDriverState
*qemu_chr_open_win(const char *filename
)
2658 CharDriverState
*chr
;
2661 chr
= qemu_mallocz(sizeof(CharDriverState
));
2664 s
= qemu_mallocz(sizeof(WinCharState
));
2670 chr
->chr_write
= win_chr_write
;
2671 chr
->chr_close
= win_chr_close
;
2673 if (win_chr_init(chr
, filename
) < 0) {
2678 qemu_chr_reset(chr
);
2682 static int win_chr_pipe_poll(void *opaque
)
2684 CharDriverState
*chr
= opaque
;
2685 WinCharState
*s
= chr
->opaque
;
2688 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2691 win_chr_read_poll(chr
);
2698 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
2700 WinCharState
*s
= chr
->opaque
;
2708 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2710 fprintf(stderr
, "Failed CreateEvent\n");
2713 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2715 fprintf(stderr
, "Failed CreateEvent\n");
2719 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2720 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2721 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2723 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2724 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2725 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2730 ZeroMemory(&ov
, sizeof(ov
));
2731 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2732 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2734 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2738 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2740 fprintf(stderr
, "Failed GetOverlappedResult\n");
2742 CloseHandle(ov
.hEvent
);
2749 CloseHandle(ov
.hEvent
);
2752 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2761 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2763 CharDriverState
*chr
;
2766 chr
= qemu_mallocz(sizeof(CharDriverState
));
2769 s
= qemu_mallocz(sizeof(WinCharState
));
2775 chr
->chr_write
= win_chr_write
;
2776 chr
->chr_close
= win_chr_close
;
2778 if (win_chr_pipe_init(chr
, filename
) < 0) {
2783 qemu_chr_reset(chr
);
2787 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2789 CharDriverState
*chr
;
2792 chr
= qemu_mallocz(sizeof(CharDriverState
));
2795 s
= qemu_mallocz(sizeof(WinCharState
));
2802 chr
->chr_write
= win_chr_write
;
2803 qemu_chr_reset(chr
);
2807 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
2809 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2812 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2816 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2817 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2818 if (fd_out
== INVALID_HANDLE_VALUE
)
2821 return qemu_chr_open_win_file(fd_out
);
2823 #endif /* !_WIN32 */
2825 /***********************************************************/
2826 /* UDP Net console */
2830 struct sockaddr_in daddr
;
2837 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2839 NetCharDriver
*s
= chr
->opaque
;
2841 return sendto(s
->fd
, buf
, len
, 0,
2842 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2845 static int udp_chr_read_poll(void *opaque
)
2847 CharDriverState
*chr
= opaque
;
2848 NetCharDriver
*s
= chr
->opaque
;
2850 s
->max_size
= qemu_chr_can_read(chr
);
2852 /* If there were any stray characters in the queue process them
2855 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2856 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2858 s
->max_size
= qemu_chr_can_read(chr
);
2863 static void udp_chr_read(void *opaque
)
2865 CharDriverState
*chr
= opaque
;
2866 NetCharDriver
*s
= chr
->opaque
;
2868 if (s
->max_size
== 0)
2870 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2871 s
->bufptr
= s
->bufcnt
;
2876 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2877 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2879 s
->max_size
= qemu_chr_can_read(chr
);
2883 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2885 NetCharDriver
*s
= chr
->opaque
;
2888 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2889 udp_chr_read
, NULL
, chr
);
2893 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2895 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2897 int parse_host_src_port(struct sockaddr_in
*haddr
,
2898 struct sockaddr_in
*saddr
,
2901 static CharDriverState
*qemu_chr_open_udp(const char *def
)
2903 CharDriverState
*chr
= NULL
;
2904 NetCharDriver
*s
= NULL
;
2906 struct sockaddr_in saddr
;
2908 chr
= qemu_mallocz(sizeof(CharDriverState
));
2911 s
= qemu_mallocz(sizeof(NetCharDriver
));
2915 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2917 perror("socket(PF_INET, SOCK_DGRAM)");
2921 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2922 printf("Could not parse: %s\n", def
);
2926 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2936 chr
->chr_write
= udp_chr_write
;
2937 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2950 /***********************************************************/
2951 /* TCP Net console */
2962 static void tcp_chr_accept(void *opaque
);
2964 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2966 TCPCharDriver
*s
= chr
->opaque
;
2968 return send_all(s
->fd
, buf
, len
);
2970 /* XXX: indicate an error ? */
2975 static int tcp_chr_read_poll(void *opaque
)
2977 CharDriverState
*chr
= opaque
;
2978 TCPCharDriver
*s
= chr
->opaque
;
2981 s
->max_size
= qemu_chr_can_read(chr
);
2986 #define IAC_BREAK 243
2987 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2989 char *buf
, int *size
)
2991 /* Handle any telnet client's basic IAC options to satisfy char by
2992 * char mode with no echo. All IAC options will be removed from
2993 * the buf and the do_telnetopt variable will be used to track the
2994 * state of the width of the IAC information.
2996 * IAC commands come in sets of 3 bytes with the exception of the
2997 * "IAC BREAK" command and the double IAC.
3003 for (i
= 0; i
< *size
; i
++) {
3004 if (s
->do_telnetopt
> 1) {
3005 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
3006 /* Double IAC means send an IAC */
3010 s
->do_telnetopt
= 1;
3012 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
3013 /* Handle IAC break commands by sending a serial break */
3014 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
3019 if (s
->do_telnetopt
>= 4) {
3020 s
->do_telnetopt
= 1;
3023 if ((unsigned char)buf
[i
] == IAC
) {
3024 s
->do_telnetopt
= 2;
3035 static void tcp_chr_read(void *opaque
)
3037 CharDriverState
*chr
= opaque
;
3038 TCPCharDriver
*s
= chr
->opaque
;
3042 if (!s
->connected
|| s
->max_size
<= 0)
3045 if (len
> s
->max_size
)
3047 size
= recv(s
->fd
, buf
, len
, 0);
3049 /* connection closed */
3051 if (s
->listen_fd
>= 0) {
3052 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
3054 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3057 } else if (size
> 0) {
3058 if (s
->do_telnetopt
)
3059 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
3061 qemu_chr_read(chr
, buf
, size
);
3065 static void tcp_chr_connect(void *opaque
)
3067 CharDriverState
*chr
= opaque
;
3068 TCPCharDriver
*s
= chr
->opaque
;
3071 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
3072 tcp_chr_read
, NULL
, chr
);
3073 qemu_chr_reset(chr
);
3076 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3077 static void tcp_chr_telnet_init(int fd
)
3080 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3081 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3082 send(fd
, (char *)buf
, 3, 0);
3083 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3084 send(fd
, (char *)buf
, 3, 0);
3085 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3086 send(fd
, (char *)buf
, 3, 0);
3087 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3088 send(fd
, (char *)buf
, 3, 0);
3091 static void socket_set_nodelay(int fd
)
3094 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
3097 static void tcp_chr_accept(void *opaque
)
3099 CharDriverState
*chr
= opaque
;
3100 TCPCharDriver
*s
= chr
->opaque
;
3101 struct sockaddr_in saddr
;
3103 struct sockaddr_un uaddr
;
3105 struct sockaddr
*addr
;
3112 len
= sizeof(uaddr
);
3113 addr
= (struct sockaddr
*)&uaddr
;
3117 len
= sizeof(saddr
);
3118 addr
= (struct sockaddr
*)&saddr
;
3120 fd
= accept(s
->listen_fd
, addr
, &len
);
3121 if (fd
< 0 && errno
!= EINTR
) {
3123 } else if (fd
>= 0) {
3124 if (s
->do_telnetopt
)
3125 tcp_chr_telnet_init(fd
);
3129 socket_set_nonblock(fd
);
3131 socket_set_nodelay(fd
);
3133 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
3134 tcp_chr_connect(chr
);
3137 static void tcp_chr_close(CharDriverState
*chr
)
3139 TCPCharDriver
*s
= chr
->opaque
;
3142 if (s
->listen_fd
>= 0)
3143 closesocket(s
->listen_fd
);
3147 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
3151 CharDriverState
*chr
= NULL
;
3152 TCPCharDriver
*s
= NULL
;
3153 int fd
= -1, ret
, err
, val
;
3155 int is_waitconnect
= 1;
3158 struct sockaddr_in saddr
;
3160 struct sockaddr_un uaddr
;
3162 struct sockaddr
*addr
;
3167 addr
= (struct sockaddr
*)&uaddr
;
3168 addrlen
= sizeof(uaddr
);
3169 if (parse_unix_path(&uaddr
, host_str
) < 0)
3174 addr
= (struct sockaddr
*)&saddr
;
3175 addrlen
= sizeof(saddr
);
3176 if (parse_host_port(&saddr
, host_str
) < 0)
3181 while((ptr
= strchr(ptr
,','))) {
3183 if (!strncmp(ptr
,"server",6)) {
3185 } else if (!strncmp(ptr
,"nowait",6)) {
3187 } else if (!strncmp(ptr
,"nodelay",6)) {
3190 printf("Unknown option: %s\n", ptr
);
3197 chr
= qemu_mallocz(sizeof(CharDriverState
));
3200 s
= qemu_mallocz(sizeof(TCPCharDriver
));
3206 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
3209 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3214 if (!is_waitconnect
)
3215 socket_set_nonblock(fd
);
3220 s
->is_unix
= is_unix
;
3221 s
->do_nodelay
= do_nodelay
&& !is_unix
;
3224 chr
->chr_write
= tcp_chr_write
;
3225 chr
->chr_close
= tcp_chr_close
;
3228 /* allow fast reuse */
3232 strncpy(path
, uaddr
.sun_path
, 108);
3239 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3242 ret
= bind(fd
, addr
, addrlen
);
3246 ret
= listen(fd
, 0);
3251 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
3253 s
->do_telnetopt
= 1;
3256 ret
= connect(fd
, addr
, addrlen
);
3258 err
= socket_error();
3259 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3260 } else if (err
== EINPROGRESS
) {
3263 } else if (err
== WSAEALREADY
) {
3275 socket_set_nodelay(fd
);
3277 tcp_chr_connect(chr
);
3279 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
3282 if (is_listen
&& is_waitconnect
) {
3283 printf("QEMU waiting for connection on: %s\n", host_str
);
3284 tcp_chr_accept(chr
);
3285 socket_set_nonblock(s
->listen_fd
);
3297 CharDriverState
*qemu_chr_open(const char *filename
)
3301 if (!strcmp(filename
, "vc")) {
3302 return text_console_init(&display_state
, 0);
3303 } else if (strstart(filename
, "vc:", &p
)) {
3304 return text_console_init(&display_state
, p
);
3305 } else if (!strcmp(filename
, "null")) {
3306 return qemu_chr_open_null();
3308 if (strstart(filename
, "tcp:", &p
)) {
3309 return qemu_chr_open_tcp(p
, 0, 0);
3311 if (strstart(filename
, "telnet:", &p
)) {
3312 return qemu_chr_open_tcp(p
, 1, 0);
3314 if (strstart(filename
, "udp:", &p
)) {
3315 return qemu_chr_open_udp(p
);
3317 if (strstart(filename
, "mon:", &p
)) {
3318 CharDriverState
*drv
= qemu_chr_open(p
);
3320 drv
= qemu_chr_open_mux(drv
);
3321 monitor_init(drv
, !nographic
);
3324 printf("Unable to open driver: %s\n", p
);
3328 if (strstart(filename
, "unix:", &p
)) {
3329 return qemu_chr_open_tcp(p
, 0, 1);
3330 } else if (strstart(filename
, "file:", &p
)) {
3331 return qemu_chr_open_file_out(p
);
3332 } else if (strstart(filename
, "pipe:", &p
)) {
3333 return qemu_chr_open_pipe(p
);
3334 } else if (!strcmp(filename
, "pty")) {
3335 return qemu_chr_open_pty();
3336 } else if (!strcmp(filename
, "stdio")) {
3337 return qemu_chr_open_stdio();
3339 #if defined(__linux__)
3340 if (strstart(filename
, "/dev/parport", NULL
)) {
3341 return qemu_chr_open_pp(filename
);
3344 #if defined(__linux__) || defined(__sun__)
3345 if (strstart(filename
, "/dev/", NULL
)) {
3346 return qemu_chr_open_tty(filename
);
3350 if (strstart(filename
, "COM", NULL
)) {
3351 return qemu_chr_open_win(filename
);
3353 if (strstart(filename
, "pipe:", &p
)) {
3354 return qemu_chr_open_win_pipe(p
);
3356 if (strstart(filename
, "con:", NULL
)) {
3357 return qemu_chr_open_win_con(filename
);
3359 if (strstart(filename
, "file:", &p
)) {
3360 return qemu_chr_open_win_file_out(p
);
3368 void qemu_chr_close(CharDriverState
*chr
)
3371 chr
->chr_close(chr
);
3374 /***********************************************************/
3375 /* network device redirectors */
3377 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
3381 for(i
=0;i
<size
;i
+=16) {
3385 fprintf(f
, "%08x ", i
);
3388 fprintf(f
, " %02x", buf
[i
+j
]);
3393 for(j
=0;j
<len
;j
++) {
3395 if (c
< ' ' || c
> '~')
3397 fprintf(f
, "%c", c
);
3403 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
3406 for(i
= 0; i
< 6; i
++) {
3407 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3420 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
3425 p1
= strchr(p
, sep
);
3431 if (len
> buf_size
- 1)
3433 memcpy(buf
, p
, len
);
3440 int parse_host_src_port(struct sockaddr_in
*haddr
,
3441 struct sockaddr_in
*saddr
,
3442 const char *input_str
)
3444 char *str
= strdup(input_str
);
3445 char *host_str
= str
;
3450 * Chop off any extra arguments at the end of the string which
3451 * would start with a comma, then fill in the src port information
3452 * if it was provided else use the "any address" and "any port".
3454 if ((ptr
= strchr(str
,',')))
3457 if ((src_str
= strchr(input_str
,'@'))) {
3462 if (parse_host_port(haddr
, host_str
) < 0)
3465 if (!src_str
|| *src_str
== '\0')
3468 if (parse_host_port(saddr
, src_str
) < 0)
3479 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
3487 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3489 saddr
->sin_family
= AF_INET
;
3490 if (buf
[0] == '\0') {
3491 saddr
->sin_addr
.s_addr
= 0;
3493 if (isdigit(buf
[0])) {
3494 if (!inet_aton(buf
, &saddr
->sin_addr
))
3497 if ((he
= gethostbyname(buf
)) == NULL
)
3499 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
3502 port
= strtol(p
, (char **)&r
, 0);
3505 saddr
->sin_port
= htons(port
);
3510 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
3515 len
= MIN(108, strlen(str
));
3516 p
= strchr(str
, ',');
3518 len
= MIN(len
, p
- str
);
3520 memset(uaddr
, 0, sizeof(*uaddr
));
3522 uaddr
->sun_family
= AF_UNIX
;
3523 memcpy(uaddr
->sun_path
, str
, len
);
3529 /* find or alloc a new VLAN */
3530 VLANState
*qemu_find_vlan(int id
)
3532 VLANState
**pvlan
, *vlan
;
3533 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3537 vlan
= qemu_mallocz(sizeof(VLANState
));
3542 pvlan
= &first_vlan
;
3543 while (*pvlan
!= NULL
)
3544 pvlan
= &(*pvlan
)->next
;
3549 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
3550 IOReadHandler
*fd_read
,
3551 IOCanRWHandler
*fd_can_read
,
3554 VLANClientState
*vc
, **pvc
;
3555 vc
= qemu_mallocz(sizeof(VLANClientState
));
3558 vc
->fd_read
= fd_read
;
3559 vc
->fd_can_read
= fd_can_read
;
3560 vc
->opaque
= opaque
;
3564 pvc
= &vlan
->first_client
;
3565 while (*pvc
!= NULL
)
3566 pvc
= &(*pvc
)->next
;
3571 int qemu_can_send_packet(VLANClientState
*vc1
)
3573 VLANState
*vlan
= vc1
->vlan
;
3574 VLANClientState
*vc
;
3576 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3578 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
3585 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
3587 VLANState
*vlan
= vc1
->vlan
;
3588 VLANClientState
*vc
;
3591 printf("vlan %d send:\n", vlan
->id
);
3592 hex_dump(stdout
, buf
, size
);
3594 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3596 vc
->fd_read(vc
->opaque
, buf
, size
);
3601 #if defined(CONFIG_SLIRP)
3603 /* slirp network adapter */
3605 static int slirp_inited
;
3606 static VLANClientState
*slirp_vc
;
3608 int slirp_can_output(void)
3610 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
3613 void slirp_output(const uint8_t *pkt
, int pkt_len
)
3616 printf("slirp output:\n");
3617 hex_dump(stdout
, pkt
, pkt_len
);
3621 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
3624 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
3627 printf("slirp input:\n");
3628 hex_dump(stdout
, buf
, size
);
3630 slirp_input(buf
, size
);
3633 static int net_slirp_init(VLANState
*vlan
)
3635 if (!slirp_inited
) {
3639 slirp_vc
= qemu_new_vlan_client(vlan
,
3640 slirp_receive
, NULL
, NULL
);
3641 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
3645 static void net_slirp_redir(const char *redir_str
)
3650 struct in_addr guest_addr
;
3651 int host_port
, guest_port
;
3653 if (!slirp_inited
) {
3659 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3661 if (!strcmp(buf
, "tcp")) {
3663 } else if (!strcmp(buf
, "udp")) {
3669 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3671 host_port
= strtol(buf
, &r
, 0);
3675 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3677 if (buf
[0] == '\0') {
3678 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
3680 if (!inet_aton(buf
, &guest_addr
))
3683 guest_port
= strtol(p
, &r
, 0);
3687 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3688 fprintf(stderr
, "qemu: could not set up redirection\n");
3693 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3701 static void smb_exit(void)
3705 char filename
[1024];
3707 /* erase all the files in the directory */
3708 d
= opendir(smb_dir
);
3713 if (strcmp(de
->d_name
, ".") != 0 &&
3714 strcmp(de
->d_name
, "..") != 0) {
3715 snprintf(filename
, sizeof(filename
), "%s/%s",
3716 smb_dir
, de
->d_name
);
3724 /* automatic user mode samba server configuration */
3725 void net_slirp_smb(const char *exported_dir
)
3727 char smb_conf
[1024];
3728 char smb_cmdline
[1024];
3731 if (!slirp_inited
) {
3736 /* XXX: better tmp dir construction */
3737 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3738 if (mkdir(smb_dir
, 0700) < 0) {
3739 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3742 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3744 f
= fopen(smb_conf
, "w");
3746 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3753 "socket address=127.0.0.1\n"
3754 "pid directory=%s\n"
3755 "lock directory=%s\n"
3756 "log file=%s/log.smbd\n"
3757 "smb passwd file=%s/smbpasswd\n"
3758 "security = share\n"
3773 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3774 SMBD_COMMAND
, smb_conf
);
3776 slirp_add_exec(0, smb_cmdline
, 4, 139);
3779 #endif /* !defined(_WIN32) */
3781 #endif /* CONFIG_SLIRP */
3783 #if !defined(_WIN32)
3785 typedef struct TAPState
{
3786 VLANClientState
*vc
;
3790 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3792 TAPState
*s
= opaque
;
3795 ret
= write(s
->fd
, buf
, size
);
3796 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3803 static void tap_send(void *opaque
)
3805 TAPState
*s
= opaque
;
3812 sbuf
.maxlen
= sizeof(buf
);
3814 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
3816 size
= read(s
->fd
, buf
, sizeof(buf
));
3819 qemu_send_packet(s
->vc
, buf
, size
);
3825 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3829 s
= qemu_mallocz(sizeof(TAPState
));
3833 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3834 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3835 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3839 #if defined (_BSD) || defined (__FreeBSD_kernel__)
3840 static int tap_open(char *ifname
, int ifname_size
)
3846 TFR(fd
= open("/dev/tap", O_RDWR
));
3848 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3853 dev
= devname(s
.st_rdev
, S_IFCHR
);
3854 pstrcpy(ifname
, ifname_size
, dev
);
3856 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3859 #elif defined(__sun__)
3860 #define TUNNEWPPA (('T'<<16) | 0x0001)
3862 * Allocate TAP device, returns opened fd.
3863 * Stores dev name in the first arg(must be large enough).
3865 int tap_alloc(char *dev
)
3867 int tap_fd
, if_fd
, ppa
= -1;
3868 static int ip_fd
= 0;
3871 static int arp_fd
= 0;
3872 int ip_muxid
, arp_muxid
;
3873 struct strioctl strioc_if
, strioc_ppa
;
3874 int link_type
= I_PLINK
;;
3876 char actual_name
[32] = "";
3878 memset(&ifr
, 0x0, sizeof(ifr
));
3882 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
3886 /* Check if IP device was opened */
3890 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
3892 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
3896 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
3898 syslog(LOG_ERR
, "Can't open /dev/tap");
3902 /* Assign a new PPA and get its unit number. */
3903 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
3904 strioc_ppa
.ic_timout
= 0;
3905 strioc_ppa
.ic_len
= sizeof(ppa
);
3906 strioc_ppa
.ic_dp
= (char *)&ppa
;
3907 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
3908 syslog (LOG_ERR
, "Can't assign new interface");
3910 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
3912 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
3915 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
3916 syslog(LOG_ERR
, "Can't push IP module");
3920 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
3921 syslog(LOG_ERR
, "Can't get flags\n");
3923 snprintf (actual_name
, 32, "tap%d", ppa
);
3924 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3927 /* Assign ppa according to the unit number returned by tun device */
3929 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
3930 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
3931 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
3932 syslog (LOG_ERR
, "Can't get flags\n");
3933 /* Push arp module to if_fd */
3934 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
3935 syslog (LOG_ERR
, "Can't push ARP module (2)");
3937 /* Push arp module to ip_fd */
3938 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
3939 syslog (LOG_ERR
, "I_POP failed\n");
3940 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
3941 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
3943 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
3945 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
3947 /* Set ifname to arp */
3948 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
3949 strioc_if
.ic_timout
= 0;
3950 strioc_if
.ic_len
= sizeof(ifr
);
3951 strioc_if
.ic_dp
= (char *)&ifr
;
3952 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
3953 syslog (LOG_ERR
, "Can't set ifname to arp\n");
3956 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
3957 syslog(LOG_ERR
, "Can't link TAP device to IP");
3961 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
3962 syslog (LOG_ERR
, "Can't link TAP device to ARP");
3966 memset(&ifr
, 0x0, sizeof(ifr
));
3967 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3968 ifr
.lifr_ip_muxid
= ip_muxid
;
3969 ifr
.lifr_arp_muxid
= arp_muxid
;
3971 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
3973 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
3974 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
3975 syslog (LOG_ERR
, "Can't set multiplexor id");
3978 sprintf(dev
, "tap%d", ppa
);
3982 static int tap_open(char *ifname
, int ifname_size
)
3986 if( (fd
= tap_alloc(dev
)) < 0 ){
3987 fprintf(stderr
, "Cannot allocate TAP device\n");
3990 pstrcpy(ifname
, ifname_size
, dev
);
3991 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3995 static int tap_open(char *ifname
, int ifname_size
)
4000 TFR(fd
= open("/dev/net/tun", O_RDWR
));
4002 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
4005 memset(&ifr
, 0, sizeof(ifr
));
4006 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
4007 if (ifname
[0] != '\0')
4008 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
4010 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
4011 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
4013 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
4017 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
4018 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
4023 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
4024 const char *setup_script
)
4027 int pid
, status
, fd
;
4032 if (ifname1
!= NULL
)
4033 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
4036 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
4040 if (!setup_script
|| !strcmp(setup_script
, "no"))
4042 if (setup_script
[0] != '\0') {
4043 /* try to launch network init script */
4047 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
4048 for (i
= 0; i
< open_max
; i
++)
4049 if (i
!= STDIN_FILENO
&&
4050 i
!= STDOUT_FILENO
&&
4051 i
!= STDERR_FILENO
&&
4056 *parg
++ = (char *)setup_script
;
4059 execv(setup_script
, args
);
4062 while (waitpid(pid
, &status
, 0) != pid
);
4063 if (!WIFEXITED(status
) ||
4064 WEXITSTATUS(status
) != 0) {
4065 fprintf(stderr
, "%s: could not launch network script\n",
4071 s
= net_tap_fd_init(vlan
, fd
);
4074 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4075 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
4079 #endif /* !_WIN32 */
4081 /* network connection */
4082 typedef struct NetSocketState
{
4083 VLANClientState
*vc
;
4085 int state
; /* 0 = getting length, 1 = getting data */
4089 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
4092 typedef struct NetSocketListenState
{
4095 } NetSocketListenState
;
4097 /* XXX: we consider we can send the whole packet without blocking */
4098 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
4100 NetSocketState
*s
= opaque
;
4104 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
4105 send_all(s
->fd
, buf
, size
);
4108 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
4110 NetSocketState
*s
= opaque
;
4111 sendto(s
->fd
, buf
, size
, 0,
4112 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
4115 static void net_socket_send(void *opaque
)
4117 NetSocketState
*s
= opaque
;
4122 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
4124 err
= socket_error();
4125 if (err
!= EWOULDBLOCK
)
4127 } else if (size
== 0) {
4128 /* end of connection */
4130 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
4136 /* reassemble a packet from the network */
4142 memcpy(s
->buf
+ s
->index
, buf
, l
);
4146 if (s
->index
== 4) {
4148 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
4154 l
= s
->packet_len
- s
->index
;
4157 memcpy(s
->buf
+ s
->index
, buf
, l
);
4161 if (s
->index
>= s
->packet_len
) {
4162 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
4171 static void net_socket_send_dgram(void *opaque
)
4173 NetSocketState
*s
= opaque
;
4176 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
4180 /* end of connection */
4181 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
4184 qemu_send_packet(s
->vc
, s
->buf
, size
);
4187 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
4192 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
4193 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
4194 inet_ntoa(mcastaddr
->sin_addr
),
4195 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
4199 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
4201 perror("socket(PF_INET, SOCK_DGRAM)");
4206 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
4207 (const char *)&val
, sizeof(val
));
4209 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
4213 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
4219 /* Add host to multicast group */
4220 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
4221 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
4223 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
4224 (const char *)&imr
, sizeof(struct ip_mreq
));
4226 perror("setsockopt(IP_ADD_MEMBERSHIP)");
4230 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
4232 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
4233 (const char *)&val
, sizeof(val
));
4235 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
4239 socket_set_nonblock(fd
);
4247 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
4250 struct sockaddr_in saddr
;
4252 socklen_t saddr_len
;
4255 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
4256 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
4257 * by ONLY ONE process: we must "clone" this dgram socket --jjo
4261 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
4263 if (saddr
.sin_addr
.s_addr
==0) {
4264 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
4268 /* clone dgram socket */
4269 newfd
= net_socket_mcast_create(&saddr
);
4271 /* error already reported by net_socket_mcast_create() */
4275 /* clone newfd to fd, close newfd */
4280 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
4281 fd
, strerror(errno
));
4286 s
= qemu_mallocz(sizeof(NetSocketState
));
4291 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
4292 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
4294 /* mcast: save bound address as dst */
4295 if (is_connected
) s
->dgram_dst
=saddr
;
4297 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4298 "socket: fd=%d (%s mcast=%s:%d)",
4299 fd
, is_connected
? "cloned" : "",
4300 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4304 static void net_socket_connect(void *opaque
)
4306 NetSocketState
*s
= opaque
;
4307 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
4310 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
4314 s
= qemu_mallocz(sizeof(NetSocketState
));
4318 s
->vc
= qemu_new_vlan_client(vlan
,
4319 net_socket_receive
, NULL
, s
);
4320 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4321 "socket: fd=%d", fd
);
4323 net_socket_connect(s
);
4325 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
4330 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
4333 int so_type
=-1, optlen
=sizeof(so_type
);
4335 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
4336 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
4341 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
4343 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
4345 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
4346 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
4347 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
4352 static void net_socket_accept(void *opaque
)
4354 NetSocketListenState
*s
= opaque
;
4356 struct sockaddr_in saddr
;
4361 len
= sizeof(saddr
);
4362 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
4363 if (fd
< 0 && errno
!= EINTR
) {
4365 } else if (fd
>= 0) {
4369 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
4373 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
4374 "socket: connection from %s:%d",
4375 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4379 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
4381 NetSocketListenState
*s
;
4383 struct sockaddr_in saddr
;
4385 if (parse_host_port(&saddr
, host_str
) < 0)
4388 s
= qemu_mallocz(sizeof(NetSocketListenState
));
4392 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4397 socket_set_nonblock(fd
);
4399 /* allow fast reuse */
4401 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
4403 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4408 ret
= listen(fd
, 0);
4415 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
4419 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
4422 int fd
, connected
, ret
, err
;
4423 struct sockaddr_in saddr
;
4425 if (parse_host_port(&saddr
, host_str
) < 0)
4428 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4433 socket_set_nonblock(fd
);
4437 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4439 err
= socket_error();
4440 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
4441 } else if (err
== EINPROGRESS
) {
4444 } else if (err
== WSAEALREADY
) {
4457 s
= net_socket_fd_init(vlan
, fd
, connected
);
4460 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4461 "socket: connect to %s:%d",
4462 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4466 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
4470 struct sockaddr_in saddr
;
4472 if (parse_host_port(&saddr
, host_str
) < 0)
4476 fd
= net_socket_mcast_create(&saddr
);
4480 s
= net_socket_fd_init(vlan
, fd
, 0);
4484 s
->dgram_dst
= saddr
;
4486 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4487 "socket: mcast=%s:%d",
4488 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4493 static int get_param_value(char *buf
, int buf_size
,
4494 const char *tag
, const char *str
)
4503 while (*p
!= '\0' && *p
!= '=') {
4504 if ((q
- option
) < sizeof(option
) - 1)
4512 if (!strcmp(tag
, option
)) {
4514 while (*p
!= '\0' && *p
!= ',') {
4515 if ((q
- buf
) < buf_size
- 1)
4522 while (*p
!= '\0' && *p
!= ',') {
4533 static int net_client_init(const char *str
)
4544 while (*p
!= '\0' && *p
!= ',') {
4545 if ((q
- device
) < sizeof(device
) - 1)
4553 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
4554 vlan_id
= strtol(buf
, NULL
, 0);
4556 vlan
= qemu_find_vlan(vlan_id
);
4558 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
4561 if (!strcmp(device
, "nic")) {
4565 if (nb_nics
>= MAX_NICS
) {
4566 fprintf(stderr
, "Too Many NICs\n");
4569 nd
= &nd_table
[nb_nics
];
4570 macaddr
= nd
->macaddr
;
4576 macaddr
[5] = 0x56 + nb_nics
;
4578 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
4579 if (parse_macaddr(macaddr
, buf
) < 0) {
4580 fprintf(stderr
, "invalid syntax for ethernet address\n");
4584 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
4585 nd
->model
= strdup(buf
);
4589 vlan
->nb_guest_devs
++;
4592 if (!strcmp(device
, "none")) {
4593 /* does nothing. It is needed to signal that no network cards
4598 if (!strcmp(device
, "user")) {
4599 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
4600 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
4602 vlan
->nb_host_devs
++;
4603 ret
= net_slirp_init(vlan
);
4607 if (!strcmp(device
, "tap")) {
4609 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4610 fprintf(stderr
, "tap: no interface name\n");
4613 vlan
->nb_host_devs
++;
4614 ret
= tap_win32_init(vlan
, ifname
);
4617 if (!strcmp(device
, "tap")) {
4619 char setup_script
[1024];
4621 vlan
->nb_host_devs
++;
4622 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4623 fd
= strtol(buf
, NULL
, 0);
4625 if (net_tap_fd_init(vlan
, fd
))
4628 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4631 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
4632 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
4634 ret
= net_tap_init(vlan
, ifname
, setup_script
);
4638 if (!strcmp(device
, "socket")) {
4639 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4641 fd
= strtol(buf
, NULL
, 0);
4643 if (net_socket_fd_init(vlan
, fd
, 1))
4645 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
4646 ret
= net_socket_listen_init(vlan
, buf
);
4647 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
4648 ret
= net_socket_connect_init(vlan
, buf
);
4649 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
4650 ret
= net_socket_mcast_init(vlan
, buf
);
4652 fprintf(stderr
, "Unknown socket options: %s\n", p
);
4655 vlan
->nb_host_devs
++;
4658 fprintf(stderr
, "Unknown network device: %s\n", device
);
4662 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
4668 void do_info_network(void)
4671 VLANClientState
*vc
;
4673 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
4674 term_printf("VLAN %d devices:\n", vlan
->id
);
4675 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
4676 term_printf(" %s\n", vc
->info_str
);
4680 /***********************************************************/
4683 static USBPort
*used_usb_ports
;
4684 static USBPort
*free_usb_ports
;
4686 /* ??? Maybe change this to register a hub to keep track of the topology. */
4687 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
4688 usb_attachfn attach
)
4690 port
->opaque
= opaque
;
4691 port
->index
= index
;
4692 port
->attach
= attach
;
4693 port
->next
= free_usb_ports
;
4694 free_usb_ports
= port
;
4697 static int usb_device_add(const char *devname
)
4703 if (!free_usb_ports
)
4706 if (strstart(devname
, "host:", &p
)) {
4707 dev
= usb_host_device_open(p
);
4708 } else if (!strcmp(devname
, "mouse")) {
4709 dev
= usb_mouse_init();
4710 } else if (!strcmp(devname
, "tablet")) {
4711 dev
= usb_tablet_init();
4712 } else if (!strcmp(devname
, "keyboard")) {
4713 dev
= usb_keyboard_init();
4714 } else if (strstart(devname
, "disk:", &p
)) {
4715 dev
= usb_msd_init(p
);
4716 } else if (!strcmp(devname
, "wacom-tablet")) {
4717 dev
= usb_wacom_init();
4724 /* Find a USB port to add the device to. */
4725 port
= free_usb_ports
;
4729 /* Create a new hub and chain it on. */
4730 free_usb_ports
= NULL
;
4731 port
->next
= used_usb_ports
;
4732 used_usb_ports
= port
;
4734 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
4735 usb_attach(port
, hub
);
4736 port
= free_usb_ports
;
4739 free_usb_ports
= port
->next
;
4740 port
->next
= used_usb_ports
;
4741 used_usb_ports
= port
;
4742 usb_attach(port
, dev
);
4746 static int usb_device_del(const char *devname
)
4754 if (!used_usb_ports
)
4757 p
= strchr(devname
, '.');
4760 bus_num
= strtoul(devname
, NULL
, 0);
4761 addr
= strtoul(p
+ 1, NULL
, 0);
4765 lastp
= &used_usb_ports
;
4766 port
= used_usb_ports
;
4767 while (port
&& port
->dev
->addr
!= addr
) {
4768 lastp
= &port
->next
;
4776 *lastp
= port
->next
;
4777 usb_attach(port
, NULL
);
4778 dev
->handle_destroy(dev
);
4779 port
->next
= free_usb_ports
;
4780 free_usb_ports
= port
;
4784 void do_usb_add(const char *devname
)
4787 ret
= usb_device_add(devname
);
4789 term_printf("Could not add USB device '%s'\n", devname
);
4792 void do_usb_del(const char *devname
)
4795 ret
= usb_device_del(devname
);
4797 term_printf("Could not remove USB device '%s'\n", devname
);
4804 const char *speed_str
;
4807 term_printf("USB support not enabled\n");
4811 for (port
= used_usb_ports
; port
; port
= port
->next
) {
4815 switch(dev
->speed
) {
4819 case USB_SPEED_FULL
:
4822 case USB_SPEED_HIGH
:
4829 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
4830 0, dev
->addr
, speed_str
, dev
->devname
);
4834 /***********************************************************/
4835 /* PCMCIA/Cardbus */
4837 static struct pcmcia_socket_entry_s
{
4838 struct pcmcia_socket_s
*socket
;
4839 struct pcmcia_socket_entry_s
*next
;
4840 } *pcmcia_sockets
= 0;
4842 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
4844 struct pcmcia_socket_entry_s
*entry
;
4846 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
4847 entry
->socket
= socket
;
4848 entry
->next
= pcmcia_sockets
;
4849 pcmcia_sockets
= entry
;
4852 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
4854 struct pcmcia_socket_entry_s
*entry
, **ptr
;
4856 ptr
= &pcmcia_sockets
;
4857 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
4858 if (entry
->socket
== socket
) {
4864 void pcmcia_info(void)
4866 struct pcmcia_socket_entry_s
*iter
;
4867 if (!pcmcia_sockets
)
4868 term_printf("No PCMCIA sockets\n");
4870 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
4871 term_printf("%s: %s\n", iter
->socket
->slot_string
,
4872 iter
->socket
->attached
? iter
->socket
->card_string
:
4876 /***********************************************************/
4879 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4883 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4887 static void dumb_refresh(DisplayState
*ds
)
4889 #if defined(CONFIG_SDL)
4894 static void dumb_display_init(DisplayState
*ds
)
4899 ds
->dpy_update
= dumb_update
;
4900 ds
->dpy_resize
= dumb_resize
;
4901 ds
->dpy_refresh
= dumb_refresh
;
4904 /***********************************************************/
4907 #define MAX_IO_HANDLERS 64
4909 typedef struct IOHandlerRecord
{
4911 IOCanRWHandler
*fd_read_poll
;
4913 IOHandler
*fd_write
;
4916 /* temporary data */
4918 struct IOHandlerRecord
*next
;
4921 static IOHandlerRecord
*first_io_handler
;
4923 /* XXX: fd_read_poll should be suppressed, but an API change is
4924 necessary in the character devices to suppress fd_can_read(). */
4925 int qemu_set_fd_handler2(int fd
,
4926 IOCanRWHandler
*fd_read_poll
,
4928 IOHandler
*fd_write
,
4931 IOHandlerRecord
**pioh
, *ioh
;
4933 if (!fd_read
&& !fd_write
) {
4934 pioh
= &first_io_handler
;
4939 if (ioh
->fd
== fd
) {
4946 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4950 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4953 ioh
->next
= first_io_handler
;
4954 first_io_handler
= ioh
;
4957 ioh
->fd_read_poll
= fd_read_poll
;
4958 ioh
->fd_read
= fd_read
;
4959 ioh
->fd_write
= fd_write
;
4960 ioh
->opaque
= opaque
;
4966 int qemu_set_fd_handler(int fd
,
4968 IOHandler
*fd_write
,
4971 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4974 /***********************************************************/
4975 /* Polling handling */
4977 typedef struct PollingEntry
{
4980 struct PollingEntry
*next
;
4983 static PollingEntry
*first_polling_entry
;
4985 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4987 PollingEntry
**ppe
, *pe
;
4988 pe
= qemu_mallocz(sizeof(PollingEntry
));
4992 pe
->opaque
= opaque
;
4993 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
4998 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
5000 PollingEntry
**ppe
, *pe
;
5001 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
5003 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
5012 /***********************************************************/
5013 /* Wait objects support */
5014 typedef struct WaitObjects
{
5016 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
5017 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
5018 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
5021 static WaitObjects wait_objects
= {0};
5023 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
5025 WaitObjects
*w
= &wait_objects
;
5027 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
5029 w
->events
[w
->num
] = handle
;
5030 w
->func
[w
->num
] = func
;
5031 w
->opaque
[w
->num
] = opaque
;
5036 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
5039 WaitObjects
*w
= &wait_objects
;
5042 for (i
= 0; i
< w
->num
; i
++) {
5043 if (w
->events
[i
] == handle
)
5046 w
->events
[i
] = w
->events
[i
+ 1];
5047 w
->func
[i
] = w
->func
[i
+ 1];
5048 w
->opaque
[i
] = w
->opaque
[i
+ 1];
5056 /***********************************************************/
5057 /* savevm/loadvm support */
5059 #define IO_BUF_SIZE 32768
5063 BlockDriverState
*bs
;
5066 int64_t base_offset
;
5067 int64_t buf_offset
; /* start of buffer when writing, end of buffer
5070 int buf_size
; /* 0 when writing */
5071 uint8_t buf
[IO_BUF_SIZE
];
5074 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
5078 f
= qemu_mallocz(sizeof(QEMUFile
));
5081 if (!strcmp(mode
, "wb")) {
5083 } else if (!strcmp(mode
, "rb")) {
5088 f
->outfile
= fopen(filename
, mode
);
5100 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
5104 f
= qemu_mallocz(sizeof(QEMUFile
));
5109 f
->is_writable
= is_writable
;
5110 f
->base_offset
= offset
;
5114 void qemu_fflush(QEMUFile
*f
)
5116 if (!f
->is_writable
)
5118 if (f
->buf_index
> 0) {
5120 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
5121 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
5123 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
5124 f
->buf
, f
->buf_index
);
5126 f
->buf_offset
+= f
->buf_index
;
5131 static void qemu_fill_buffer(QEMUFile
*f
)
5138 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
5139 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
5143 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
5144 f
->buf
, IO_BUF_SIZE
);
5150 f
->buf_offset
+= len
;
5153 void qemu_fclose(QEMUFile
*f
)
5163 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
5167 l
= IO_BUF_SIZE
- f
->buf_index
;
5170 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
5174 if (f
->buf_index
>= IO_BUF_SIZE
)
5179 void qemu_put_byte(QEMUFile
*f
, int v
)
5181 f
->buf
[f
->buf_index
++] = v
;
5182 if (f
->buf_index
>= IO_BUF_SIZE
)
5186 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
5192 l
= f
->buf_size
- f
->buf_index
;
5194 qemu_fill_buffer(f
);
5195 l
= f
->buf_size
- f
->buf_index
;
5201 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
5206 return size1
- size
;
5209 int qemu_get_byte(QEMUFile
*f
)
5211 if (f
->buf_index
>= f
->buf_size
) {
5212 qemu_fill_buffer(f
);
5213 if (f
->buf_index
>= f
->buf_size
)
5216 return f
->buf
[f
->buf_index
++];
5219 int64_t qemu_ftell(QEMUFile
*f
)
5221 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
5224 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
5226 if (whence
== SEEK_SET
) {
5228 } else if (whence
== SEEK_CUR
) {
5229 pos
+= qemu_ftell(f
);
5231 /* SEEK_END not supported */
5234 if (f
->is_writable
) {
5236 f
->buf_offset
= pos
;
5238 f
->buf_offset
= pos
;
5245 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
5247 qemu_put_byte(f
, v
>> 8);
5248 qemu_put_byte(f
, v
);
5251 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
5253 qemu_put_byte(f
, v
>> 24);
5254 qemu_put_byte(f
, v
>> 16);
5255 qemu_put_byte(f
, v
>> 8);
5256 qemu_put_byte(f
, v
);
5259 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
5261 qemu_put_be32(f
, v
>> 32);
5262 qemu_put_be32(f
, v
);
5265 unsigned int qemu_get_be16(QEMUFile
*f
)
5268 v
= qemu_get_byte(f
) << 8;
5269 v
|= qemu_get_byte(f
);
5273 unsigned int qemu_get_be32(QEMUFile
*f
)
5276 v
= qemu_get_byte(f
) << 24;
5277 v
|= qemu_get_byte(f
) << 16;
5278 v
|= qemu_get_byte(f
) << 8;
5279 v
|= qemu_get_byte(f
);
5283 uint64_t qemu_get_be64(QEMUFile
*f
)
5286 v
= (uint64_t)qemu_get_be32(f
) << 32;
5287 v
|= qemu_get_be32(f
);
5291 typedef struct SaveStateEntry
{
5295 SaveStateHandler
*save_state
;
5296 LoadStateHandler
*load_state
;
5298 struct SaveStateEntry
*next
;
5301 static SaveStateEntry
*first_se
;
5303 int register_savevm(const char *idstr
,
5306 SaveStateHandler
*save_state
,
5307 LoadStateHandler
*load_state
,
5310 SaveStateEntry
*se
, **pse
;
5312 se
= qemu_malloc(sizeof(SaveStateEntry
));
5315 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
5316 se
->instance_id
= instance_id
;
5317 se
->version_id
= version_id
;
5318 se
->save_state
= save_state
;
5319 se
->load_state
= load_state
;
5320 se
->opaque
= opaque
;
5323 /* add at the end of list */
5325 while (*pse
!= NULL
)
5326 pse
= &(*pse
)->next
;
5331 #define QEMU_VM_FILE_MAGIC 0x5145564d
5332 #define QEMU_VM_FILE_VERSION 0x00000002
5334 int qemu_savevm_state(QEMUFile
*f
)
5338 int64_t cur_pos
, len_pos
, total_len_pos
;
5340 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
5341 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
5342 total_len_pos
= qemu_ftell(f
);
5343 qemu_put_be64(f
, 0); /* total size */
5345 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5347 len
= strlen(se
->idstr
);
5348 qemu_put_byte(f
, len
);
5349 qemu_put_buffer(f
, se
->idstr
, len
);
5351 qemu_put_be32(f
, se
->instance_id
);
5352 qemu_put_be32(f
, se
->version_id
);
5354 /* record size: filled later */
5355 len_pos
= qemu_ftell(f
);
5356 qemu_put_be32(f
, 0);
5358 se
->save_state(f
, se
->opaque
);
5360 /* fill record size */
5361 cur_pos
= qemu_ftell(f
);
5362 len
= cur_pos
- len_pos
- 4;
5363 qemu_fseek(f
, len_pos
, SEEK_SET
);
5364 qemu_put_be32(f
, len
);
5365 qemu_fseek(f
, cur_pos
, SEEK_SET
);
5367 cur_pos
= qemu_ftell(f
);
5368 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
5369 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
5370 qemu_fseek(f
, cur_pos
, SEEK_SET
);
5376 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
5380 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5381 if (!strcmp(se
->idstr
, idstr
) &&
5382 instance_id
== se
->instance_id
)
5388 int qemu_loadvm_state(QEMUFile
*f
)
5391 int len
, ret
, instance_id
, record_len
, version_id
;
5392 int64_t total_len
, end_pos
, cur_pos
;
5396 v
= qemu_get_be32(f
);
5397 if (v
!= QEMU_VM_FILE_MAGIC
)
5399 v
= qemu_get_be32(f
);
5400 if (v
!= QEMU_VM_FILE_VERSION
) {
5405 total_len
= qemu_get_be64(f
);
5406 end_pos
= total_len
+ qemu_ftell(f
);
5408 if (qemu_ftell(f
) >= end_pos
)
5410 len
= qemu_get_byte(f
);
5411 qemu_get_buffer(f
, idstr
, len
);
5413 instance_id
= qemu_get_be32(f
);
5414 version_id
= qemu_get_be32(f
);
5415 record_len
= qemu_get_be32(f
);
5417 printf("idstr=%s instance=0x%x version=%d len=%d\n",
5418 idstr
, instance_id
, version_id
, record_len
);
5420 cur_pos
= qemu_ftell(f
);
5421 se
= find_se(idstr
, instance_id
);
5423 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
5424 instance_id
, idstr
);
5426 ret
= se
->load_state(f
, se
->opaque
, version_id
);
5428 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
5429 instance_id
, idstr
);
5432 /* always seek to exact end of record */
5433 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
5440 /* device can contain snapshots */
5441 static int bdrv_can_snapshot(BlockDriverState
*bs
)
5444 !bdrv_is_removable(bs
) &&
5445 !bdrv_is_read_only(bs
));
5448 /* device must be snapshots in order to have a reliable snapshot */
5449 static int bdrv_has_snapshot(BlockDriverState
*bs
)
5452 !bdrv_is_removable(bs
) &&
5453 !bdrv_is_read_only(bs
));
5456 static BlockDriverState
*get_bs_snapshots(void)
5458 BlockDriverState
*bs
;
5462 return bs_snapshots
;
5463 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5465 if (bdrv_can_snapshot(bs
))
5474 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
5477 QEMUSnapshotInfo
*sn_tab
, *sn
;
5481 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5484 for(i
= 0; i
< nb_sns
; i
++) {
5486 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
5496 void do_savevm(const char *name
)
5498 BlockDriverState
*bs
, *bs1
;
5499 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
5500 int must_delete
, ret
, i
;
5501 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5503 int saved_vm_running
;
5510 bs
= get_bs_snapshots();
5512 term_printf("No block device can accept snapshots\n");
5516 /* ??? Should this occur after vm_stop? */
5519 saved_vm_running
= vm_running
;
5524 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
5529 memset(sn
, 0, sizeof(*sn
));
5531 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
5532 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
5535 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
5538 /* fill auxiliary fields */
5541 sn
->date_sec
= tb
.time
;
5542 sn
->date_nsec
= tb
.millitm
* 1000000;
5544 gettimeofday(&tv
, NULL
);
5545 sn
->date_sec
= tv
.tv_sec
;
5546 sn
->date_nsec
= tv
.tv_usec
* 1000;
5548 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
5550 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5551 term_printf("Device %s does not support VM state snapshots\n",
5552 bdrv_get_device_name(bs
));
5556 /* save the VM state */
5557 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
5559 term_printf("Could not open VM state file\n");
5562 ret
= qemu_savevm_state(f
);
5563 sn
->vm_state_size
= qemu_ftell(f
);
5566 term_printf("Error %d while writing VM\n", ret
);
5570 /* create the snapshots */
5572 for(i
= 0; i
< MAX_DISKS
; i
++) {
5574 if (bdrv_has_snapshot(bs1
)) {
5576 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
5578 term_printf("Error while deleting snapshot on '%s'\n",
5579 bdrv_get_device_name(bs1
));
5582 ret
= bdrv_snapshot_create(bs1
, sn
);
5584 term_printf("Error while creating snapshot on '%s'\n",
5585 bdrv_get_device_name(bs1
));
5591 if (saved_vm_running
)
5595 void do_loadvm(const char *name
)
5597 BlockDriverState
*bs
, *bs1
;
5598 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5601 int saved_vm_running
;
5603 bs
= get_bs_snapshots();
5605 term_printf("No block device supports snapshots\n");
5609 /* Flush all IO requests so they don't interfere with the new state. */
5612 saved_vm_running
= vm_running
;
5615 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5617 if (bdrv_has_snapshot(bs1
)) {
5618 ret
= bdrv_snapshot_goto(bs1
, name
);
5621 term_printf("Warning: ");
5624 term_printf("Snapshots not supported on device '%s'\n",
5625 bdrv_get_device_name(bs1
));
5628 term_printf("Could not find snapshot '%s' on device '%s'\n",
5629 name
, bdrv_get_device_name(bs1
));
5632 term_printf("Error %d while activating snapshot on '%s'\n",
5633 ret
, bdrv_get_device_name(bs1
));
5636 /* fatal on snapshot block device */
5643 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5644 term_printf("Device %s does not support VM state snapshots\n",
5645 bdrv_get_device_name(bs
));
5649 /* restore the VM state */
5650 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
5652 term_printf("Could not open VM state file\n");
5655 ret
= qemu_loadvm_state(f
);
5658 term_printf("Error %d while loading VM state\n", ret
);
5661 if (saved_vm_running
)
5665 void do_delvm(const char *name
)
5667 BlockDriverState
*bs
, *bs1
;
5670 bs
= get_bs_snapshots();
5672 term_printf("No block device supports snapshots\n");
5676 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5678 if (bdrv_has_snapshot(bs1
)) {
5679 ret
= bdrv_snapshot_delete(bs1
, name
);
5681 if (ret
== -ENOTSUP
)
5682 term_printf("Snapshots not supported on device '%s'\n",
5683 bdrv_get_device_name(bs1
));
5685 term_printf("Error %d while deleting snapshot on '%s'\n",
5686 ret
, bdrv_get_device_name(bs1
));
5692 void do_info_snapshots(void)
5694 BlockDriverState
*bs
, *bs1
;
5695 QEMUSnapshotInfo
*sn_tab
, *sn
;
5699 bs
= get_bs_snapshots();
5701 term_printf("No available block device supports snapshots\n");
5704 term_printf("Snapshot devices:");
5705 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5707 if (bdrv_has_snapshot(bs1
)) {
5709 term_printf(" %s", bdrv_get_device_name(bs1
));
5714 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5716 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
5719 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
5720 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
5721 for(i
= 0; i
< nb_sns
; i
++) {
5723 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
5728 /***********************************************************/
5729 /* cpu save/restore */
5731 #if defined(TARGET_I386)
5733 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
5735 qemu_put_be32(f
, dt
->selector
);
5736 qemu_put_betl(f
, dt
->base
);
5737 qemu_put_be32(f
, dt
->limit
);
5738 qemu_put_be32(f
, dt
->flags
);
5741 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
5743 dt
->selector
= qemu_get_be32(f
);
5744 dt
->base
= qemu_get_betl(f
);
5745 dt
->limit
= qemu_get_be32(f
);
5746 dt
->flags
= qemu_get_be32(f
);
5749 void cpu_save(QEMUFile
*f
, void *opaque
)
5751 CPUState
*env
= opaque
;
5752 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
5756 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5757 qemu_put_betls(f
, &env
->regs
[i
]);
5758 qemu_put_betls(f
, &env
->eip
);
5759 qemu_put_betls(f
, &env
->eflags
);
5760 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
5761 qemu_put_be32s(f
, &hflags
);
5765 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
5767 for(i
= 0; i
< 8; i
++) {
5768 fptag
|= ((!env
->fptags
[i
]) << i
);
5771 qemu_put_be16s(f
, &fpuc
);
5772 qemu_put_be16s(f
, &fpus
);
5773 qemu_put_be16s(f
, &fptag
);
5775 #ifdef USE_X86LDOUBLE
5780 qemu_put_be16s(f
, &fpregs_format
);
5782 for(i
= 0; i
< 8; i
++) {
5783 #ifdef USE_X86LDOUBLE
5787 /* we save the real CPU data (in case of MMX usage only 'mant'
5788 contains the MMX register */
5789 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
5790 qemu_put_be64(f
, mant
);
5791 qemu_put_be16(f
, exp
);
5794 /* if we use doubles for float emulation, we save the doubles to
5795 avoid losing information in case of MMX usage. It can give
5796 problems if the image is restored on a CPU where long
5797 doubles are used instead. */
5798 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
5802 for(i
= 0; i
< 6; i
++)
5803 cpu_put_seg(f
, &env
->segs
[i
]);
5804 cpu_put_seg(f
, &env
->ldt
);
5805 cpu_put_seg(f
, &env
->tr
);
5806 cpu_put_seg(f
, &env
->gdt
);
5807 cpu_put_seg(f
, &env
->idt
);
5809 qemu_put_be32s(f
, &env
->sysenter_cs
);
5810 qemu_put_be32s(f
, &env
->sysenter_esp
);
5811 qemu_put_be32s(f
, &env
->sysenter_eip
);
5813 qemu_put_betls(f
, &env
->cr
[0]);
5814 qemu_put_betls(f
, &env
->cr
[2]);
5815 qemu_put_betls(f
, &env
->cr
[3]);
5816 qemu_put_betls(f
, &env
->cr
[4]);
5818 for(i
= 0; i
< 8; i
++)
5819 qemu_put_betls(f
, &env
->dr
[i
]);
5822 qemu_put_be32s(f
, &env
->a20_mask
);
5825 qemu_put_be32s(f
, &env
->mxcsr
);
5826 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5827 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5828 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5831 #ifdef TARGET_X86_64
5832 qemu_put_be64s(f
, &env
->efer
);
5833 qemu_put_be64s(f
, &env
->star
);
5834 qemu_put_be64s(f
, &env
->lstar
);
5835 qemu_put_be64s(f
, &env
->cstar
);
5836 qemu_put_be64s(f
, &env
->fmask
);
5837 qemu_put_be64s(f
, &env
->kernelgsbase
);
5839 qemu_put_be32s(f
, &env
->smbase
);
5842 #ifdef USE_X86LDOUBLE
5843 /* XXX: add that in a FPU generic layer */
5844 union x86_longdouble
{
5849 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
5850 #define EXPBIAS1 1023
5851 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
5852 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
5854 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
5858 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
5859 /* exponent + sign */
5860 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
5861 e
|= SIGND1(temp
) >> 16;
5866 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5868 CPUState
*env
= opaque
;
5871 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
5873 if (version_id
!= 3 && version_id
!= 4)
5875 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5876 qemu_get_betls(f
, &env
->regs
[i
]);
5877 qemu_get_betls(f
, &env
->eip
);
5878 qemu_get_betls(f
, &env
->eflags
);
5879 qemu_get_be32s(f
, &hflags
);
5881 qemu_get_be16s(f
, &fpuc
);
5882 qemu_get_be16s(f
, &fpus
);
5883 qemu_get_be16s(f
, &fptag
);
5884 qemu_get_be16s(f
, &fpregs_format
);
5886 /* NOTE: we cannot always restore the FPU state if the image come
5887 from a host with a different 'USE_X86LDOUBLE' define. We guess
5888 if we are in an MMX state to restore correctly in that case. */
5889 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
5890 for(i
= 0; i
< 8; i
++) {
5894 switch(fpregs_format
) {
5896 mant
= qemu_get_be64(f
);
5897 exp
= qemu_get_be16(f
);
5898 #ifdef USE_X86LDOUBLE
5899 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5901 /* difficult case */
5903 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5905 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5909 mant
= qemu_get_be64(f
);
5910 #ifdef USE_X86LDOUBLE
5912 union x86_longdouble
*p
;
5913 /* difficult case */
5914 p
= (void *)&env
->fpregs
[i
];
5919 fp64_to_fp80(p
, mant
);
5923 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5932 /* XXX: restore FPU round state */
5933 env
->fpstt
= (fpus
>> 11) & 7;
5934 env
->fpus
= fpus
& ~0x3800;
5936 for(i
= 0; i
< 8; i
++) {
5937 env
->fptags
[i
] = (fptag
>> i
) & 1;
5940 for(i
= 0; i
< 6; i
++)
5941 cpu_get_seg(f
, &env
->segs
[i
]);
5942 cpu_get_seg(f
, &env
->ldt
);
5943 cpu_get_seg(f
, &env
->tr
);
5944 cpu_get_seg(f
, &env
->gdt
);
5945 cpu_get_seg(f
, &env
->idt
);
5947 qemu_get_be32s(f
, &env
->sysenter_cs
);
5948 qemu_get_be32s(f
, &env
->sysenter_esp
);
5949 qemu_get_be32s(f
, &env
->sysenter_eip
);
5951 qemu_get_betls(f
, &env
->cr
[0]);
5952 qemu_get_betls(f
, &env
->cr
[2]);
5953 qemu_get_betls(f
, &env
->cr
[3]);
5954 qemu_get_betls(f
, &env
->cr
[4]);
5956 for(i
= 0; i
< 8; i
++)
5957 qemu_get_betls(f
, &env
->dr
[i
]);
5960 qemu_get_be32s(f
, &env
->a20_mask
);
5962 qemu_get_be32s(f
, &env
->mxcsr
);
5963 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5964 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5965 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5968 #ifdef TARGET_X86_64
5969 qemu_get_be64s(f
, &env
->efer
);
5970 qemu_get_be64s(f
, &env
->star
);
5971 qemu_get_be64s(f
, &env
->lstar
);
5972 qemu_get_be64s(f
, &env
->cstar
);
5973 qemu_get_be64s(f
, &env
->fmask
);
5974 qemu_get_be64s(f
, &env
->kernelgsbase
);
5976 if (version_id
>= 4)
5977 qemu_get_be32s(f
, &env
->smbase
);
5979 /* XXX: compute hflags from scratch, except for CPL and IIF */
5980 env
->hflags
= hflags
;
5985 #elif defined(TARGET_PPC)
5986 void cpu_save(QEMUFile
*f
, void *opaque
)
5990 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5995 #elif defined(TARGET_MIPS)
5996 void cpu_save(QEMUFile
*f
, void *opaque
)
6000 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6005 #elif defined(TARGET_SPARC)
6006 void cpu_save(QEMUFile
*f
, void *opaque
)
6008 CPUState
*env
= opaque
;
6012 for(i
= 0; i
< 8; i
++)
6013 qemu_put_betls(f
, &env
->gregs
[i
]);
6014 for(i
= 0; i
< NWINDOWS
* 16; i
++)
6015 qemu_put_betls(f
, &env
->regbase
[i
]);
6018 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
6024 qemu_put_be32(f
, u
.i
);
6027 qemu_put_betls(f
, &env
->pc
);
6028 qemu_put_betls(f
, &env
->npc
);
6029 qemu_put_betls(f
, &env
->y
);
6031 qemu_put_be32(f
, tmp
);
6032 qemu_put_betls(f
, &env
->fsr
);
6033 qemu_put_betls(f
, &env
->tbr
);
6034 #ifndef TARGET_SPARC64
6035 qemu_put_be32s(f
, &env
->wim
);
6037 for(i
= 0; i
< 16; i
++)
6038 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
6042 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6044 CPUState
*env
= opaque
;
6048 for(i
= 0; i
< 8; i
++)
6049 qemu_get_betls(f
, &env
->gregs
[i
]);
6050 for(i
= 0; i
< NWINDOWS
* 16; i
++)
6051 qemu_get_betls(f
, &env
->regbase
[i
]);
6054 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
6059 u
.i
= qemu_get_be32(f
);
6063 qemu_get_betls(f
, &env
->pc
);
6064 qemu_get_betls(f
, &env
->npc
);
6065 qemu_get_betls(f
, &env
->y
);
6066 tmp
= qemu_get_be32(f
);
6067 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
6068 correctly updated */
6070 qemu_get_betls(f
, &env
->fsr
);
6071 qemu_get_betls(f
, &env
->tbr
);
6072 #ifndef TARGET_SPARC64
6073 qemu_get_be32s(f
, &env
->wim
);
6075 for(i
= 0; i
< 16; i
++)
6076 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
6082 #elif defined(TARGET_ARM)
6084 void cpu_save(QEMUFile
*f
, void *opaque
)
6087 CPUARMState
*env
= (CPUARMState
*)opaque
;
6089 for (i
= 0; i
< 16; i
++) {
6090 qemu_put_be32(f
, env
->regs
[i
]);
6092 qemu_put_be32(f
, cpsr_read(env
));
6093 qemu_put_be32(f
, env
->spsr
);
6094 for (i
= 0; i
< 6; i
++) {
6095 qemu_put_be32(f
, env
->banked_spsr
[i
]);
6096 qemu_put_be32(f
, env
->banked_r13
[i
]);
6097 qemu_put_be32(f
, env
->banked_r14
[i
]);
6099 for (i
= 0; i
< 5; i
++) {
6100 qemu_put_be32(f
, env
->usr_regs
[i
]);
6101 qemu_put_be32(f
, env
->fiq_regs
[i
]);
6103 qemu_put_be32(f
, env
->cp15
.c0_cpuid
);
6104 qemu_put_be32(f
, env
->cp15
.c0_cachetype
);
6105 qemu_put_be32(f
, env
->cp15
.c1_sys
);
6106 qemu_put_be32(f
, env
->cp15
.c1_coproc
);
6107 qemu_put_be32(f
, env
->cp15
.c1_xscaleauxcr
);
6108 qemu_put_be32(f
, env
->cp15
.c2_base
);
6109 qemu_put_be32(f
, env
->cp15
.c2_data
);
6110 qemu_put_be32(f
, env
->cp15
.c2_insn
);
6111 qemu_put_be32(f
, env
->cp15
.c3
);
6112 qemu_put_be32(f
, env
->cp15
.c5_insn
);
6113 qemu_put_be32(f
, env
->cp15
.c5_data
);
6114 for (i
= 0; i
< 8; i
++) {
6115 qemu_put_be32(f
, env
->cp15
.c6_region
[i
]);
6117 qemu_put_be32(f
, env
->cp15
.c6_insn
);
6118 qemu_put_be32(f
, env
->cp15
.c6_data
);
6119 qemu_put_be32(f
, env
->cp15
.c9_insn
);
6120 qemu_put_be32(f
, env
->cp15
.c9_data
);
6121 qemu_put_be32(f
, env
->cp15
.c13_fcse
);
6122 qemu_put_be32(f
, env
->cp15
.c13_context
);
6123 qemu_put_be32(f
, env
->cp15
.c15_cpar
);
6125 qemu_put_be32(f
, env
->features
);
6127 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
6128 for (i
= 0; i
< 16; i
++) {
6130 u
.d
= env
->vfp
.regs
[i
];
6131 qemu_put_be32(f
, u
.l
.upper
);
6132 qemu_put_be32(f
, u
.l
.lower
);
6134 for (i
= 0; i
< 16; i
++) {
6135 qemu_put_be32(f
, env
->vfp
.xregs
[i
]);
6138 /* TODO: Should use proper FPSCR access functions. */
6139 qemu_put_be32(f
, env
->vfp
.vec_len
);
6140 qemu_put_be32(f
, env
->vfp
.vec_stride
);
6143 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
6144 for (i
= 0; i
< 16; i
++) {
6145 qemu_put_be64(f
, env
->iwmmxt
.regs
[i
]);
6147 for (i
= 0; i
< 16; i
++) {
6148 qemu_put_be32(f
, env
->iwmmxt
.cregs
[i
]);
6153 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6155 CPUARMState
*env
= (CPUARMState
*)opaque
;
6158 if (version_id
!= 0)
6161 for (i
= 0; i
< 16; i
++) {
6162 env
->regs
[i
] = qemu_get_be32(f
);
6164 cpsr_write(env
, qemu_get_be32(f
), 0xffffffff);
6165 env
->spsr
= qemu_get_be32(f
);
6166 for (i
= 0; i
< 6; i
++) {
6167 env
->banked_spsr
[i
] = qemu_get_be32(f
);
6168 env
->banked_r13
[i
] = qemu_get_be32(f
);
6169 env
->banked_r14
[i
] = qemu_get_be32(f
);
6171 for (i
= 0; i
< 5; i
++) {
6172 env
->usr_regs
[i
] = qemu_get_be32(f
);
6173 env
->fiq_regs
[i
] = qemu_get_be32(f
);
6175 env
->cp15
.c0_cpuid
= qemu_get_be32(f
);
6176 env
->cp15
.c0_cachetype
= qemu_get_be32(f
);
6177 env
->cp15
.c1_sys
= qemu_get_be32(f
);
6178 env
->cp15
.c1_coproc
= qemu_get_be32(f
);
6179 env
->cp15
.c1_xscaleauxcr
= qemu_get_be32(f
);
6180 env
->cp15
.c2_base
= qemu_get_be32(f
);
6181 env
->cp15
.c2_data
= qemu_get_be32(f
);
6182 env
->cp15
.c2_insn
= qemu_get_be32(f
);
6183 env
->cp15
.c3
= qemu_get_be32(f
);
6184 env
->cp15
.c5_insn
= qemu_get_be32(f
);
6185 env
->cp15
.c5_data
= qemu_get_be32(f
);
6186 for (i
= 0; i
< 8; i
++) {
6187 env
->cp15
.c6_region
[i
] = qemu_get_be32(f
);
6189 env
->cp15
.c6_insn
= qemu_get_be32(f
);
6190 env
->cp15
.c6_data
= qemu_get_be32(f
);
6191 env
->cp15
.c9_insn
= qemu_get_be32(f
);
6192 env
->cp15
.c9_data
= qemu_get_be32(f
);
6193 env
->cp15
.c13_fcse
= qemu_get_be32(f
);
6194 env
->cp15
.c13_context
= qemu_get_be32(f
);
6195 env
->cp15
.c15_cpar
= qemu_get_be32(f
);
6197 env
->features
= qemu_get_be32(f
);
6199 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
6200 for (i
= 0; i
< 16; i
++) {
6202 u
.l
.upper
= qemu_get_be32(f
);
6203 u
.l
.lower
= qemu_get_be32(f
);
6204 env
->vfp
.regs
[i
] = u
.d
;
6206 for (i
= 0; i
< 16; i
++) {
6207 env
->vfp
.xregs
[i
] = qemu_get_be32(f
);
6210 /* TODO: Should use proper FPSCR access functions. */
6211 env
->vfp
.vec_len
= qemu_get_be32(f
);
6212 env
->vfp
.vec_stride
= qemu_get_be32(f
);
6215 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
6216 for (i
= 0; i
< 16; i
++) {
6217 env
->iwmmxt
.regs
[i
] = qemu_get_be64(f
);
6219 for (i
= 0; i
< 16; i
++) {
6220 env
->iwmmxt
.cregs
[i
] = qemu_get_be32(f
);
6229 #warning No CPU save/restore functions
6233 /***********************************************************/
6234 /* ram save/restore */
6236 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
6240 v
= qemu_get_byte(f
);
6243 if (qemu_get_buffer(f
, buf
, len
) != len
)
6247 v
= qemu_get_byte(f
);
6248 memset(buf
, v
, len
);
6256 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
6260 if (qemu_get_be32(f
) != phys_ram_size
)
6262 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
6263 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
6270 #define BDRV_HASH_BLOCK_SIZE 1024
6271 #define IOBUF_SIZE 4096
6272 #define RAM_CBLOCK_MAGIC 0xfabe
6274 typedef struct RamCompressState
{
6277 uint8_t buf
[IOBUF_SIZE
];
6280 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
6283 memset(s
, 0, sizeof(*s
));
6285 ret
= deflateInit2(&s
->zstream
, 1,
6287 9, Z_DEFAULT_STRATEGY
);
6290 s
->zstream
.avail_out
= IOBUF_SIZE
;
6291 s
->zstream
.next_out
= s
->buf
;
6295 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
6297 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
6298 qemu_put_be16(s
->f
, len
);
6299 qemu_put_buffer(s
->f
, buf
, len
);
6302 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
6306 s
->zstream
.avail_in
= len
;
6307 s
->zstream
.next_in
= (uint8_t *)buf
;
6308 while (s
->zstream
.avail_in
> 0) {
6309 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
6312 if (s
->zstream
.avail_out
== 0) {
6313 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
6314 s
->zstream
.avail_out
= IOBUF_SIZE
;
6315 s
->zstream
.next_out
= s
->buf
;
6321 static void ram_compress_close(RamCompressState
*s
)
6325 /* compress last bytes */
6327 ret
= deflate(&s
->zstream
, Z_FINISH
);
6328 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
6329 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
6331 ram_put_cblock(s
, s
->buf
, len
);
6333 s
->zstream
.avail_out
= IOBUF_SIZE
;
6334 s
->zstream
.next_out
= s
->buf
;
6335 if (ret
== Z_STREAM_END
)
6342 deflateEnd(&s
->zstream
);
6345 typedef struct RamDecompressState
{
6348 uint8_t buf
[IOBUF_SIZE
];
6349 } RamDecompressState
;
6351 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
6354 memset(s
, 0, sizeof(*s
));
6356 ret
= inflateInit(&s
->zstream
);
6362 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
6366 s
->zstream
.avail_out
= len
;
6367 s
->zstream
.next_out
= buf
;
6368 while (s
->zstream
.avail_out
> 0) {
6369 if (s
->zstream
.avail_in
== 0) {
6370 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
6372 clen
= qemu_get_be16(s
->f
);
6373 if (clen
> IOBUF_SIZE
)
6375 qemu_get_buffer(s
->f
, s
->buf
, clen
);
6376 s
->zstream
.avail_in
= clen
;
6377 s
->zstream
.next_in
= s
->buf
;
6379 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
6380 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
6387 static void ram_decompress_close(RamDecompressState
*s
)
6389 inflateEnd(&s
->zstream
);
6392 static void ram_save(QEMUFile
*f
, void *opaque
)
6395 RamCompressState s1
, *s
= &s1
;
6398 qemu_put_be32(f
, phys_ram_size
);
6399 if (ram_compress_open(s
, f
) < 0)
6401 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6403 if (tight_savevm_enabled
) {
6407 /* find if the memory block is available on a virtual
6410 for(j
= 0; j
< MAX_DISKS
; j
++) {
6412 sector_num
= bdrv_hash_find(bs_table
[j
],
6413 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6414 if (sector_num
>= 0)
6419 goto normal_compress
;
6422 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
6423 ram_compress_buf(s
, buf
, 10);
6429 ram_compress_buf(s
, buf
, 1);
6430 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6433 ram_compress_close(s
);
6436 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
6438 RamDecompressState s1
, *s
= &s1
;
6442 if (version_id
== 1)
6443 return ram_load_v1(f
, opaque
);
6444 if (version_id
!= 2)
6446 if (qemu_get_be32(f
) != phys_ram_size
)
6448 if (ram_decompress_open(s
, f
) < 0)
6450 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6451 if (ram_decompress_buf(s
, buf
, 1) < 0) {
6452 fprintf(stderr
, "Error while reading ram block header\n");
6456 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
6457 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
6466 ram_decompress_buf(s
, buf
+ 1, 9);
6468 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
6469 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
6470 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
6473 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
6474 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
6475 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
6476 bs_index
, sector_num
);
6483 printf("Error block header\n");
6487 ram_decompress_close(s
);
6491 /***********************************************************/
6492 /* bottom halves (can be seen as timers which expire ASAP) */
6501 static QEMUBH
*first_bh
= NULL
;
6503 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
6506 bh
= qemu_mallocz(sizeof(QEMUBH
));
6510 bh
->opaque
= opaque
;
6514 int qemu_bh_poll(void)
6533 void qemu_bh_schedule(QEMUBH
*bh
)
6535 CPUState
*env
= cpu_single_env
;
6539 bh
->next
= first_bh
;
6542 /* stop the currently executing CPU to execute the BH ASAP */
6544 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
6548 void qemu_bh_cancel(QEMUBH
*bh
)
6551 if (bh
->scheduled
) {
6554 pbh
= &(*pbh
)->next
;
6560 void qemu_bh_delete(QEMUBH
*bh
)
6566 /***********************************************************/
6567 /* machine registration */
6569 QEMUMachine
*first_machine
= NULL
;
6571 int qemu_register_machine(QEMUMachine
*m
)
6574 pm
= &first_machine
;
6582 QEMUMachine
*find_machine(const char *name
)
6586 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6587 if (!strcmp(m
->name
, name
))
6593 /***********************************************************/
6594 /* main execution loop */
6596 void gui_update(void *opaque
)
6598 DisplayState
*ds
= opaque
;
6599 ds
->dpy_refresh(ds
);
6600 qemu_mod_timer(ds
->gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
6603 struct vm_change_state_entry
{
6604 VMChangeStateHandler
*cb
;
6606 LIST_ENTRY (vm_change_state_entry
) entries
;
6609 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
6611 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
6614 VMChangeStateEntry
*e
;
6616 e
= qemu_mallocz(sizeof (*e
));
6622 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
6626 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
6628 LIST_REMOVE (e
, entries
);
6632 static void vm_state_notify(int running
)
6634 VMChangeStateEntry
*e
;
6636 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
6637 e
->cb(e
->opaque
, running
);
6641 /* XXX: support several handlers */
6642 static VMStopHandler
*vm_stop_cb
;
6643 static void *vm_stop_opaque
;
6645 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6648 vm_stop_opaque
= opaque
;
6652 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6663 qemu_rearm_alarm_timer(alarm_timer
);
6667 void vm_stop(int reason
)
6670 cpu_disable_ticks();
6674 vm_stop_cb(vm_stop_opaque
, reason
);
6681 /* reset/shutdown handler */
6683 typedef struct QEMUResetEntry
{
6684 QEMUResetHandler
*func
;
6686 struct QEMUResetEntry
*next
;
6689 static QEMUResetEntry
*first_reset_entry
;
6690 static int reset_requested
;
6691 static int shutdown_requested
;
6692 static int powerdown_requested
;
6694 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
6696 QEMUResetEntry
**pre
, *re
;
6698 pre
= &first_reset_entry
;
6699 while (*pre
!= NULL
)
6700 pre
= &(*pre
)->next
;
6701 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
6703 re
->opaque
= opaque
;
6708 static void qemu_system_reset(void)
6712 /* reset all devices */
6713 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
6714 re
->func(re
->opaque
);
6718 void qemu_system_reset_request(void)
6721 shutdown_requested
= 1;
6723 reset_requested
= 1;
6726 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6729 void qemu_system_shutdown_request(void)
6731 shutdown_requested
= 1;
6733 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6736 void qemu_system_powerdown_request(void)
6738 powerdown_requested
= 1;
6740 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6743 void main_loop_wait(int timeout
)
6745 IOHandlerRecord
*ioh
;
6746 fd_set rfds
, wfds
, xfds
;
6755 /* XXX: need to suppress polling by better using win32 events */
6757 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
6758 ret
|= pe
->func(pe
->opaque
);
6763 WaitObjects
*w
= &wait_objects
;
6765 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
6766 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
6767 if (w
->func
[ret
- WAIT_OBJECT_0
])
6768 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
6770 /* Check for additional signaled events */
6771 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
6773 /* Check if event is signaled */
6774 ret2
= WaitForSingleObject(w
->events
[i
], 0);
6775 if(ret2
== WAIT_OBJECT_0
) {
6777 w
->func
[i
](w
->opaque
[i
]);
6778 } else if (ret2
== WAIT_TIMEOUT
) {
6780 err
= GetLastError();
6781 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
6784 } else if (ret
== WAIT_TIMEOUT
) {
6786 err
= GetLastError();
6787 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
6791 /* poll any events */
6792 /* XXX: separate device handlers from system ones */
6797 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6801 (!ioh
->fd_read_poll
||
6802 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
6803 FD_SET(ioh
->fd
, &rfds
);
6807 if (ioh
->fd_write
) {
6808 FD_SET(ioh
->fd
, &wfds
);
6818 tv
.tv_usec
= timeout
* 1000;
6820 #if defined(CONFIG_SLIRP)
6822 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
6825 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
6827 IOHandlerRecord
**pioh
;
6829 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6830 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
6831 ioh
->fd_read(ioh
->opaque
);
6833 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
6834 ioh
->fd_write(ioh
->opaque
);
6838 /* remove deleted IO handlers */
6839 pioh
= &first_io_handler
;
6849 #if defined(CONFIG_SLIRP)
6856 slirp_select_poll(&rfds
, &wfds
, &xfds
);
6862 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
6863 qemu_get_clock(vm_clock
));
6864 /* run dma transfers, if any */
6868 /* real time timers */
6869 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
6870 qemu_get_clock(rt_clock
));
6872 /* Check bottom-halves last in case any of the earlier events triggered
6878 static CPUState
*cur_cpu
;
6883 #ifdef CONFIG_PROFILER
6888 cur_cpu
= first_cpu
;
6895 env
= env
->next_cpu
;
6898 #ifdef CONFIG_PROFILER
6899 ti
= profile_getclock();
6901 ret
= cpu_exec(env
);
6902 #ifdef CONFIG_PROFILER
6903 qemu_time
+= profile_getclock() - ti
;
6905 if (ret
== EXCP_HLT
) {
6906 /* Give the next CPU a chance to run. */
6910 if (ret
!= EXCP_HALTED
)
6912 /* all CPUs are halted ? */
6918 if (shutdown_requested
) {
6919 ret
= EXCP_INTERRUPT
;
6922 if (reset_requested
) {
6923 reset_requested
= 0;
6924 qemu_system_reset();
6925 ret
= EXCP_INTERRUPT
;
6927 if (powerdown_requested
) {
6928 powerdown_requested
= 0;
6929 qemu_system_powerdown();
6930 ret
= EXCP_INTERRUPT
;
6932 if (ret
== EXCP_DEBUG
) {
6933 vm_stop(EXCP_DEBUG
);
6935 /* If all cpus are halted then wait until the next IRQ */
6936 /* XXX: use timeout computed from timers */
6937 if (ret
== EXCP_HALTED
)
6944 #ifdef CONFIG_PROFILER
6945 ti
= profile_getclock();
6947 main_loop_wait(timeout
);
6948 #ifdef CONFIG_PROFILER
6949 dev_time
+= profile_getclock() - ti
;
6952 cpu_disable_ticks();
6956 static void help(int exitcode
)
6958 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2007 Fabrice Bellard\n"
6959 "usage: %s [options] [disk_image]\n"
6961 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
6963 "Standard options:\n"
6964 "-M machine select emulated machine (-M ? for list)\n"
6965 "-cpu cpu select CPU (-cpu ? for list)\n"
6966 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
6967 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
6968 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
6969 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
6970 "-mtdblock file use 'file' as on-board Flash memory image\n"
6971 "-sd file use 'file' as SecureDigital card image\n"
6972 "-pflash file use 'file' as a parallel flash image\n"
6973 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
6974 "-snapshot write to temporary files instead of disk image files\n"
6976 "-no-frame open SDL window without a frame and window decorations\n"
6977 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
6978 "-no-quit disable SDL window close capability\n"
6981 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
6983 "-m megs set virtual RAM size to megs MB [default=%d]\n"
6984 "-smp n set the number of CPUs to 'n' [default=1]\n"
6985 "-nographic disable graphical output and redirect serial I/Os to console\n"
6986 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
6988 "-k language use keyboard layout (for example \"fr\" for French)\n"
6991 "-audio-help print list of audio drivers and their options\n"
6992 "-soundhw c1,... enable audio support\n"
6993 " and only specified sound cards (comma separated list)\n"
6994 " use -soundhw ? to get the list of supported cards\n"
6995 " use -soundhw all to enable all of them\n"
6997 "-localtime set the real time clock to local time [default=utc]\n"
6998 "-full-screen start in full screen\n"
7000 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
7002 "-usb enable the USB driver (will be the default soon)\n"
7003 "-usbdevice name add the host or guest USB device 'name'\n"
7004 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7005 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
7007 "-name string set the name of the guest\n"
7009 "Network options:\n"
7010 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
7011 " create a new Network Interface Card and connect it to VLAN 'n'\n"
7013 "-net user[,vlan=n][,hostname=host]\n"
7014 " connect the user mode network stack to VLAN 'n' and send\n"
7015 " hostname 'host' to DHCP clients\n"
7018 "-net tap[,vlan=n],ifname=name\n"
7019 " connect the host TAP network interface to VLAN 'n'\n"
7021 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
7022 " connect the host TAP network interface to VLAN 'n' and use\n"
7023 " the network script 'file' (default=%s);\n"
7024 " use 'script=no' to disable script execution;\n"
7025 " use 'fd=h' to connect to an already opened TAP interface\n"
7027 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
7028 " connect the vlan 'n' to another VLAN using a socket connection\n"
7029 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
7030 " connect the vlan 'n' to multicast maddr and port\n"
7031 "-net none use it alone to have zero network devices; if no -net option\n"
7032 " is provided, the default is '-net nic -net user'\n"
7035 "-tftp dir allow tftp access to files in dir [-net user]\n"
7036 "-bootp file advertise file in BOOTP replies\n"
7038 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
7040 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
7041 " redirect TCP or UDP connections from host to guest [-net user]\n"
7044 "Linux boot specific:\n"
7045 "-kernel bzImage use 'bzImage' as kernel image\n"
7046 "-append cmdline use 'cmdline' as kernel command line\n"
7047 "-initrd file use 'file' as initial ram disk\n"
7049 "Debug/Expert options:\n"
7050 "-monitor dev redirect the monitor to char device 'dev'\n"
7051 "-serial dev redirect the serial port to char device 'dev'\n"
7052 "-parallel dev redirect the parallel port to char device 'dev'\n"
7053 "-pidfile file Write PID to 'file'\n"
7054 "-S freeze CPU at startup (use 'c' to start execution)\n"
7055 "-s wait gdb connection to port\n"
7056 "-p port set gdb connection port [default=%s]\n"
7057 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
7058 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
7059 " translation (t=none or lba) (usually qemu can guess them)\n"
7060 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
7062 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
7063 "-no-kqemu disable KQEMU kernel module usage\n"
7065 #ifdef USE_CODE_COPY
7066 "-no-code-copy disable code copy acceleration\n"
7069 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
7070 " (default is CL-GD5446 PCI VGA)\n"
7071 "-no-acpi disable ACPI\n"
7073 "-no-reboot exit instead of rebooting\n"
7074 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
7075 "-vnc display start a VNC server on display\n"
7077 "-daemonize daemonize QEMU after initializing\n"
7079 "-option-rom rom load a file, rom, into the option ROM space\n"
7081 "-prom-env variable=value set OpenBIOS nvram variables\n"
7083 "-clock force the use of the given methods for timer alarm.\n"
7084 " To see what timers are available use -clock help\n"
7086 "During emulation, the following keys are useful:\n"
7087 "ctrl-alt-f toggle full screen\n"
7088 "ctrl-alt-n switch to virtual console 'n'\n"
7089 "ctrl-alt toggle mouse and keyboard grab\n"
7091 "When using -nographic, press 'ctrl-a h' to get some help.\n"
7096 DEFAULT_NETWORK_SCRIPT
,
7098 DEFAULT_GDBSTUB_PORT
,
7103 #define HAS_ARG 0x0001
7117 QEMU_OPTION_mtdblock
,
7121 QEMU_OPTION_snapshot
,
7123 QEMU_OPTION_no_fd_bootchk
,
7126 QEMU_OPTION_nographic
,
7127 QEMU_OPTION_portrait
,
7129 QEMU_OPTION_audio_help
,
7130 QEMU_OPTION_soundhw
,
7149 QEMU_OPTION_no_code_copy
,
7151 QEMU_OPTION_localtime
,
7152 QEMU_OPTION_cirrusvga
,
7155 QEMU_OPTION_std_vga
,
7157 QEMU_OPTION_monitor
,
7159 QEMU_OPTION_parallel
,
7161 QEMU_OPTION_full_screen
,
7162 QEMU_OPTION_no_frame
,
7163 QEMU_OPTION_alt_grab
,
7164 QEMU_OPTION_no_quit
,
7165 QEMU_OPTION_pidfile
,
7166 QEMU_OPTION_no_kqemu
,
7167 QEMU_OPTION_kernel_kqemu
,
7168 QEMU_OPTION_win2k_hack
,
7170 QEMU_OPTION_usbdevice
,
7173 QEMU_OPTION_no_acpi
,
7174 QEMU_OPTION_no_reboot
,
7175 QEMU_OPTION_show_cursor
,
7176 QEMU_OPTION_daemonize
,
7177 QEMU_OPTION_option_rom
,
7178 QEMU_OPTION_semihosting
,
7180 QEMU_OPTION_prom_env
,
7181 QEMU_OPTION_old_param
,
7185 typedef struct QEMUOption
{
7191 const QEMUOption qemu_options
[] = {
7192 { "h", 0, QEMU_OPTION_h
},
7193 { "help", 0, QEMU_OPTION_h
},
7195 { "M", HAS_ARG
, QEMU_OPTION_M
},
7196 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
7197 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
7198 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
7199 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
7200 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
7201 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
7202 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
7203 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
7204 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
7205 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
7206 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
7207 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
7208 { "snapshot", 0, QEMU_OPTION_snapshot
},
7210 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
7212 { "m", HAS_ARG
, QEMU_OPTION_m
},
7213 { "nographic", 0, QEMU_OPTION_nographic
},
7214 { "portrait", 0, QEMU_OPTION_portrait
},
7215 { "k", HAS_ARG
, QEMU_OPTION_k
},
7217 { "audio-help", 0, QEMU_OPTION_audio_help
},
7218 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
7221 { "net", HAS_ARG
, QEMU_OPTION_net
},
7223 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
7224 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
7226 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
7228 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
7231 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
7232 { "append", HAS_ARG
, QEMU_OPTION_append
},
7233 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
7235 { "S", 0, QEMU_OPTION_S
},
7236 { "s", 0, QEMU_OPTION_s
},
7237 { "p", HAS_ARG
, QEMU_OPTION_p
},
7238 { "d", HAS_ARG
, QEMU_OPTION_d
},
7239 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
7240 { "L", HAS_ARG
, QEMU_OPTION_L
},
7241 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
7243 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
7244 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
7246 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7247 { "g", 1, QEMU_OPTION_g
},
7249 { "localtime", 0, QEMU_OPTION_localtime
},
7250 { "std-vga", 0, QEMU_OPTION_std_vga
},
7251 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
7252 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
7253 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
7254 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
7255 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
7256 { "full-screen", 0, QEMU_OPTION_full_screen
},
7258 { "no-frame", 0, QEMU_OPTION_no_frame
},
7259 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
7260 { "no-quit", 0, QEMU_OPTION_no_quit
},
7262 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
7263 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
7264 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
7265 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
7266 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
7268 /* temporary options */
7269 { "usb", 0, QEMU_OPTION_usb
},
7270 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
7271 { "vmwarevga", 0, QEMU_OPTION_vmsvga
},
7272 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
7273 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
7274 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
7275 { "daemonize", 0, QEMU_OPTION_daemonize
},
7276 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
7277 #if defined(TARGET_ARM) || defined(TARGET_M68K)
7278 { "semihosting", 0, QEMU_OPTION_semihosting
},
7280 { "name", HAS_ARG
, QEMU_OPTION_name
},
7281 #if defined(TARGET_SPARC)
7282 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
7284 #if defined(TARGET_ARM)
7285 { "old-param", 0, QEMU_OPTION_old_param
},
7287 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
7291 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
7293 /* this stack is only used during signal handling */
7294 #define SIGNAL_STACK_SIZE 32768
7296 static uint8_t *signal_stack
;
7300 /* password input */
7302 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
7307 if (!bdrv_is_encrypted(bs
))
7310 term_printf("%s is encrypted.\n", name
);
7311 for(i
= 0; i
< 3; i
++) {
7312 monitor_readline("Password: ", 1, password
, sizeof(password
));
7313 if (bdrv_set_key(bs
, password
) == 0)
7315 term_printf("invalid password\n");
7320 static BlockDriverState
*get_bdrv(int index
)
7322 BlockDriverState
*bs
;
7325 bs
= bs_table
[index
];
7326 } else if (index
< 6) {
7327 bs
= fd_table
[index
- 4];
7334 static void read_passwords(void)
7336 BlockDriverState
*bs
;
7339 for(i
= 0; i
< 6; i
++) {
7342 qemu_key_check(bs
, bdrv_get_device_name(bs
));
7346 /* XXX: currently we cannot use simultaneously different CPUs */
7347 void register_machines(void)
7349 #if defined(TARGET_I386)
7350 qemu_register_machine(&pc_machine
);
7351 qemu_register_machine(&isapc_machine
);
7352 #elif defined(TARGET_PPC)
7353 qemu_register_machine(&heathrow_machine
);
7354 qemu_register_machine(&core99_machine
);
7355 qemu_register_machine(&prep_machine
);
7356 qemu_register_machine(&ref405ep_machine
);
7357 qemu_register_machine(&taihu_machine
);
7358 #elif defined(TARGET_MIPS)
7359 qemu_register_machine(&mips_machine
);
7360 qemu_register_machine(&mips_malta_machine
);
7361 qemu_register_machine(&mips_pica61_machine
);
7362 #elif defined(TARGET_SPARC)
7363 #ifdef TARGET_SPARC64
7364 qemu_register_machine(&sun4u_machine
);
7366 qemu_register_machine(&ss5_machine
);
7367 qemu_register_machine(&ss10_machine
);
7369 #elif defined(TARGET_ARM)
7370 qemu_register_machine(&integratorcp_machine
);
7371 qemu_register_machine(&versatilepb_machine
);
7372 qemu_register_machine(&versatileab_machine
);
7373 qemu_register_machine(&realview_machine
);
7374 qemu_register_machine(&akitapda_machine
);
7375 qemu_register_machine(&spitzpda_machine
);
7376 qemu_register_machine(&borzoipda_machine
);
7377 qemu_register_machine(&terrierpda_machine
);
7378 qemu_register_machine(&palmte_machine
);
7379 #elif defined(TARGET_SH4)
7380 qemu_register_machine(&shix_machine
);
7381 #elif defined(TARGET_ALPHA)
7383 #elif defined(TARGET_M68K)
7384 qemu_register_machine(&mcf5208evb_machine
);
7385 qemu_register_machine(&an5206_machine
);
7387 #error unsupported CPU
7392 struct soundhw soundhw
[] = {
7393 #ifdef HAS_AUDIO_CHOICE
7400 { .init_isa
= pcspk_audio_init
}
7405 "Creative Sound Blaster 16",
7408 { .init_isa
= SB16_init
}
7415 "Yamaha YMF262 (OPL3)",
7417 "Yamaha YM3812 (OPL2)",
7421 { .init_isa
= Adlib_init
}
7428 "Gravis Ultrasound GF1",
7431 { .init_isa
= GUS_init
}
7437 "ENSONIQ AudioPCI ES1370",
7440 { .init_pci
= es1370_init
}
7444 { NULL
, NULL
, 0, 0, { NULL
} }
7447 static void select_soundhw (const char *optarg
)
7451 if (*optarg
== '?') {
7454 printf ("Valid sound card names (comma separated):\n");
7455 for (c
= soundhw
; c
->name
; ++c
) {
7456 printf ("%-11s %s\n", c
->name
, c
->descr
);
7458 printf ("\n-soundhw all will enable all of the above\n");
7459 exit (*optarg
!= '?');
7467 if (!strcmp (optarg
, "all")) {
7468 for (c
= soundhw
; c
->name
; ++c
) {
7476 e
= strchr (p
, ',');
7477 l
= !e
? strlen (p
) : (size_t) (e
- p
);
7479 for (c
= soundhw
; c
->name
; ++c
) {
7480 if (!strncmp (c
->name
, p
, l
)) {
7489 "Unknown sound card name (too big to show)\n");
7492 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
7497 p
+= l
+ (e
!= NULL
);
7501 goto show_valid_cards
;
7507 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
7509 exit(STATUS_CONTROL_C_EXIT
);
7514 #define MAX_NET_CLIENTS 32
7516 int main(int argc
, char **argv
)
7518 #ifdef CONFIG_GDBSTUB
7520 const char *gdbstub_port
;
7522 int i
, cdrom_index
, pflash_index
;
7523 int snapshot
, linux_boot
;
7524 const char *initrd_filename
;
7525 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
7526 const char *pflash_filename
[MAX_PFLASH
];
7527 const char *sd_filename
;
7528 const char *mtd_filename
;
7529 const char *kernel_filename
, *kernel_cmdline
;
7530 DisplayState
*ds
= &display_state
;
7531 int cyls
, heads
, secs
, translation
;
7532 char net_clients
[MAX_NET_CLIENTS
][256];
7535 const char *r
, *optarg
;
7536 CharDriverState
*monitor_hd
;
7537 char monitor_device
[128];
7538 char serial_devices
[MAX_SERIAL_PORTS
][128];
7539 int serial_device_index
;
7540 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
7541 int parallel_device_index
;
7542 const char *loadvm
= NULL
;
7543 QEMUMachine
*machine
;
7544 const char *cpu_model
;
7545 char usb_devices
[MAX_USB_CMDLINE
][128];
7546 int usb_devices_index
;
7548 const char *pid_file
= NULL
;
7551 LIST_INIT (&vm_change_state_head
);
7554 struct sigaction act
;
7555 sigfillset(&act
.sa_mask
);
7557 act
.sa_handler
= SIG_IGN
;
7558 sigaction(SIGPIPE
, &act
, NULL
);
7561 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
7562 /* Note: cpu_interrupt() is currently not SMP safe, so we force
7563 QEMU to run on a single CPU */
7568 h
= GetCurrentProcess();
7569 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
7570 for(i
= 0; i
< 32; i
++) {
7571 if (mask
& (1 << i
))
7576 SetProcessAffinityMask(h
, mask
);
7582 register_machines();
7583 machine
= first_machine
;
7585 initrd_filename
= NULL
;
7586 for(i
= 0; i
< MAX_FD
; i
++)
7587 fd_filename
[i
] = NULL
;
7588 for(i
= 0; i
< MAX_DISKS
; i
++)
7589 hd_filename
[i
] = NULL
;
7590 for(i
= 0; i
< MAX_PFLASH
; i
++)
7591 pflash_filename
[i
] = NULL
;
7594 mtd_filename
= NULL
;
7595 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
7596 vga_ram_size
= VGA_RAM_SIZE
;
7597 #ifdef CONFIG_GDBSTUB
7599 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
7603 kernel_filename
= NULL
;
7604 kernel_cmdline
= "";
7610 cyls
= heads
= secs
= 0;
7611 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7612 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
7614 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
7615 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
7616 serial_devices
[i
][0] = '\0';
7617 serial_device_index
= 0;
7619 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
7620 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
7621 parallel_devices
[i
][0] = '\0';
7622 parallel_device_index
= 0;
7624 usb_devices_index
= 0;
7629 /* default mac address of the first network interface */
7637 hd_filename
[0] = argv
[optind
++];
7639 const QEMUOption
*popt
;
7642 /* Treat --foo the same as -foo. */
7645 popt
= qemu_options
;
7648 fprintf(stderr
, "%s: invalid option -- '%s'\n",
7652 if (!strcmp(popt
->name
, r
+ 1))
7656 if (popt
->flags
& HAS_ARG
) {
7657 if (optind
>= argc
) {
7658 fprintf(stderr
, "%s: option '%s' requires an argument\n",
7662 optarg
= argv
[optind
++];
7667 switch(popt
->index
) {
7669 machine
= find_machine(optarg
);
7672 printf("Supported machines are:\n");
7673 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
7674 printf("%-10s %s%s\n",
7676 m
== first_machine
? " (default)" : "");
7678 exit(*optarg
!= '?');
7681 case QEMU_OPTION_cpu
:
7682 /* hw initialization will check this */
7683 if (*optarg
== '?') {
7684 #if defined(TARGET_PPC)
7685 ppc_cpu_list(stdout
, &fprintf
);
7686 #elif defined(TARGET_ARM)
7688 #elif defined(TARGET_MIPS)
7689 mips_cpu_list(stdout
, &fprintf
);
7690 #elif defined(TARGET_SPARC)
7691 sparc_cpu_list(stdout
, &fprintf
);
7698 case QEMU_OPTION_initrd
:
7699 initrd_filename
= optarg
;
7701 case QEMU_OPTION_hda
:
7702 case QEMU_OPTION_hdb
:
7703 case QEMU_OPTION_hdc
:
7704 case QEMU_OPTION_hdd
:
7707 hd_index
= popt
->index
- QEMU_OPTION_hda
;
7708 hd_filename
[hd_index
] = optarg
;
7709 if (hd_index
== cdrom_index
)
7713 case QEMU_OPTION_mtdblock
:
7714 mtd_filename
= optarg
;
7716 case QEMU_OPTION_sd
:
7717 sd_filename
= optarg
;
7719 case QEMU_OPTION_pflash
:
7720 if (pflash_index
>= MAX_PFLASH
) {
7721 fprintf(stderr
, "qemu: too many parallel flash images\n");
7724 pflash_filename
[pflash_index
++] = optarg
;
7726 case QEMU_OPTION_snapshot
:
7729 case QEMU_OPTION_hdachs
:
7733 cyls
= strtol(p
, (char **)&p
, 0);
7734 if (cyls
< 1 || cyls
> 16383)
7739 heads
= strtol(p
, (char **)&p
, 0);
7740 if (heads
< 1 || heads
> 16)
7745 secs
= strtol(p
, (char **)&p
, 0);
7746 if (secs
< 1 || secs
> 63)
7750 if (!strcmp(p
, "none"))
7751 translation
= BIOS_ATA_TRANSLATION_NONE
;
7752 else if (!strcmp(p
, "lba"))
7753 translation
= BIOS_ATA_TRANSLATION_LBA
;
7754 else if (!strcmp(p
, "auto"))
7755 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7758 } else if (*p
!= '\0') {
7760 fprintf(stderr
, "qemu: invalid physical CHS format\n");
7765 case QEMU_OPTION_nographic
:
7766 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
7767 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "null");
7768 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
7771 case QEMU_OPTION_portrait
:
7774 case QEMU_OPTION_kernel
:
7775 kernel_filename
= optarg
;
7777 case QEMU_OPTION_append
:
7778 kernel_cmdline
= optarg
;
7780 case QEMU_OPTION_cdrom
:
7781 if (cdrom_index
>= 0) {
7782 hd_filename
[cdrom_index
] = optarg
;
7785 case QEMU_OPTION_boot
:
7786 boot_device
= optarg
[0];
7787 if (boot_device
!= 'a' &&
7788 #if defined(TARGET_SPARC) || defined(TARGET_I386)
7790 boot_device
!= 'n' &&
7792 boot_device
!= 'c' && boot_device
!= 'd') {
7793 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
7797 case QEMU_OPTION_fda
:
7798 fd_filename
[0] = optarg
;
7800 case QEMU_OPTION_fdb
:
7801 fd_filename
[1] = optarg
;
7804 case QEMU_OPTION_no_fd_bootchk
:
7808 case QEMU_OPTION_no_code_copy
:
7809 code_copy_enabled
= 0;
7811 case QEMU_OPTION_net
:
7812 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
7813 fprintf(stderr
, "qemu: too many network clients\n");
7816 pstrcpy(net_clients
[nb_net_clients
],
7817 sizeof(net_clients
[0]),
7822 case QEMU_OPTION_tftp
:
7823 tftp_prefix
= optarg
;
7825 case QEMU_OPTION_bootp
:
7826 bootp_filename
= optarg
;
7829 case QEMU_OPTION_smb
:
7830 net_slirp_smb(optarg
);
7833 case QEMU_OPTION_redir
:
7834 net_slirp_redir(optarg
);
7838 case QEMU_OPTION_audio_help
:
7842 case QEMU_OPTION_soundhw
:
7843 select_soundhw (optarg
);
7850 ram_size
= atoi(optarg
) * 1024 * 1024;
7853 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
7854 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
7855 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
7864 mask
= cpu_str_to_log_mask(optarg
);
7866 printf("Log items (comma separated):\n");
7867 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
7868 printf("%-10s %s\n", item
->name
, item
->help
);
7875 #ifdef CONFIG_GDBSTUB
7880 gdbstub_port
= optarg
;
7890 keyboard_layout
= optarg
;
7892 case QEMU_OPTION_localtime
:
7895 case QEMU_OPTION_cirrusvga
:
7896 cirrus_vga_enabled
= 1;
7899 case QEMU_OPTION_vmsvga
:
7900 cirrus_vga_enabled
= 0;
7903 case QEMU_OPTION_std_vga
:
7904 cirrus_vga_enabled
= 0;
7912 w
= strtol(p
, (char **)&p
, 10);
7915 fprintf(stderr
, "qemu: invalid resolution or depth\n");
7921 h
= strtol(p
, (char **)&p
, 10);
7926 depth
= strtol(p
, (char **)&p
, 10);
7927 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
7928 depth
!= 24 && depth
!= 32)
7930 } else if (*p
== '\0') {
7931 depth
= graphic_depth
;
7938 graphic_depth
= depth
;
7941 case QEMU_OPTION_echr
:
7944 term_escape_char
= strtol(optarg
, &r
, 0);
7946 printf("Bad argument to echr\n");
7949 case QEMU_OPTION_monitor
:
7950 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
7952 case QEMU_OPTION_serial
:
7953 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
7954 fprintf(stderr
, "qemu: too many serial ports\n");
7957 pstrcpy(serial_devices
[serial_device_index
],
7958 sizeof(serial_devices
[0]), optarg
);
7959 serial_device_index
++;
7961 case QEMU_OPTION_parallel
:
7962 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
7963 fprintf(stderr
, "qemu: too many parallel ports\n");
7966 pstrcpy(parallel_devices
[parallel_device_index
],
7967 sizeof(parallel_devices
[0]), optarg
);
7968 parallel_device_index
++;
7970 case QEMU_OPTION_loadvm
:
7973 case QEMU_OPTION_full_screen
:
7977 case QEMU_OPTION_no_frame
:
7980 case QEMU_OPTION_alt_grab
:
7983 case QEMU_OPTION_no_quit
:
7987 case QEMU_OPTION_pidfile
:
7991 case QEMU_OPTION_win2k_hack
:
7992 win2k_install_hack
= 1;
7996 case QEMU_OPTION_no_kqemu
:
7999 case QEMU_OPTION_kernel_kqemu
:
8003 case QEMU_OPTION_usb
:
8006 case QEMU_OPTION_usbdevice
:
8008 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
8009 fprintf(stderr
, "Too many USB devices\n");
8012 pstrcpy(usb_devices
[usb_devices_index
],
8013 sizeof(usb_devices
[usb_devices_index
]),
8015 usb_devices_index
++;
8017 case QEMU_OPTION_smp
:
8018 smp_cpus
= atoi(optarg
);
8019 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
8020 fprintf(stderr
, "Invalid number of CPUs\n");
8024 case QEMU_OPTION_vnc
:
8025 vnc_display
= optarg
;
8027 case QEMU_OPTION_no_acpi
:
8030 case QEMU_OPTION_no_reboot
:
8033 case QEMU_OPTION_show_cursor
:
8036 case QEMU_OPTION_daemonize
:
8039 case QEMU_OPTION_option_rom
:
8040 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
8041 fprintf(stderr
, "Too many option ROMs\n");
8044 option_rom
[nb_option_roms
] = optarg
;
8047 case QEMU_OPTION_semihosting
:
8048 semihosting_enabled
= 1;
8050 case QEMU_OPTION_name
:
8054 case QEMU_OPTION_prom_env
:
8055 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
8056 fprintf(stderr
, "Too many prom variables\n");
8059 prom_envs
[nb_prom_envs
] = optarg
;
8064 case QEMU_OPTION_old_param
:
8067 case QEMU_OPTION_clock
:
8068 configure_alarms(optarg
);
8075 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
8076 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
8083 if (pipe(fds
) == -1)
8094 len
= read(fds
[0], &status
, 1);
8095 if (len
== -1 && (errno
== EINTR
))
8100 else if (status
== 1) {
8101 fprintf(stderr
, "Could not acquire pidfile\n");
8119 signal(SIGTSTP
, SIG_IGN
);
8120 signal(SIGTTOU
, SIG_IGN
);
8121 signal(SIGTTIN
, SIG_IGN
);
8125 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
8128 write(fds
[1], &status
, 1);
8130 fprintf(stderr
, "Could not acquire pid file\n");
8138 linux_boot
= (kernel_filename
!= NULL
);
8141 boot_device
!= 'n' &&
8142 hd_filename
[0] == '\0' &&
8143 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
8144 fd_filename
[0] == '\0')
8147 /* boot to floppy or the default cd if no hard disk defined yet */
8148 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
8149 if (fd_filename
[0] != '\0')
8155 setvbuf(stdout
, NULL
, _IOLBF
, 0);
8165 /* init network clients */
8166 if (nb_net_clients
== 0) {
8167 /* if no clients, we use a default config */
8168 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
8170 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
8175 for(i
= 0;i
< nb_net_clients
; i
++) {
8176 if (net_client_init(net_clients
[i
]) < 0)
8179 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
8180 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
8182 if (vlan
->nb_guest_devs
== 0) {
8183 fprintf(stderr
, "Invalid vlan (%d) with no nics\n", vlan
->id
);
8186 if (vlan
->nb_host_devs
== 0)
8188 "Warning: vlan %d is not connected to host network\n",
8193 if (boot_device
== 'n') {
8194 for (i
= 0; i
< nb_nics
; i
++) {
8195 const char *model
= nd_table
[i
].model
;
8199 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
8200 if (get_image_size(buf
) > 0) {
8201 option_rom
[nb_option_roms
] = strdup(buf
);
8207 fprintf(stderr
, "No valid PXE rom found for network device\n");
8213 /* init the memory */
8214 phys_ram_size
= ram_size
+ vga_ram_size
+ MAX_BIOS_SIZE
;
8216 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
8217 if (!phys_ram_base
) {
8218 fprintf(stderr
, "Could not allocate physical memory\n");
8222 /* we always create the cdrom drive, even if no disk is there */
8224 if (cdrom_index
>= 0) {
8225 bs_table
[cdrom_index
] = bdrv_new("cdrom");
8226 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
8229 /* open the virtual block devices */
8230 for(i
= 0; i
< MAX_DISKS
; i
++) {
8231 if (hd_filename
[i
]) {
8234 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
8235 bs_table
[i
] = bdrv_new(buf
);
8237 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8238 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
8242 if (i
== 0 && cyls
!= 0) {
8243 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
8244 bdrv_set_translation_hint(bs_table
[i
], translation
);
8249 /* we always create at least one floppy disk */
8250 fd_table
[0] = bdrv_new("fda");
8251 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
8253 for(i
= 0; i
< MAX_FD
; i
++) {
8254 if (fd_filename
[i
]) {
8257 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
8258 fd_table
[i
] = bdrv_new(buf
);
8259 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
8261 if (fd_filename
[i
][0] != '\0') {
8262 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
8263 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8264 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
8272 /* Open the virtual parallel flash block devices */
8273 for(i
= 0; i
< MAX_PFLASH
; i
++) {
8274 if (pflash_filename
[i
]) {
8275 if (!pflash_table
[i
]) {
8277 snprintf(buf
, sizeof(buf
), "fl%c", i
+ 'a');
8278 pflash_table
[i
] = bdrv_new(buf
);
8280 if (bdrv_open(pflash_table
[i
], pflash_filename
[i
],
8281 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8282 fprintf(stderr
, "qemu: could not open flash image '%s'\n",
8283 pflash_filename
[i
]);
8289 sd_bdrv
= bdrv_new ("sd");
8290 /* FIXME: This isn't really a floppy, but it's a reasonable
8292 bdrv_set_type_hint(sd_bdrv
, BDRV_TYPE_FLOPPY
);
8294 if (bdrv_open(sd_bdrv
, sd_filename
,
8295 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8296 fprintf(stderr
, "qemu: could not open SD card image %s\n",
8299 qemu_key_check(sd_bdrv
, sd_filename
);
8303 mtd_bdrv
= bdrv_new ("mtd");
8304 if (bdrv_open(mtd_bdrv
, mtd_filename
,
8305 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0 ||
8306 qemu_key_check(mtd_bdrv
, mtd_filename
)) {
8307 fprintf(stderr
, "qemu: could not open Flash image %s\n",
8309 bdrv_delete(mtd_bdrv
);
8314 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
8315 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
8320 memset(&display_state
, 0, sizeof(display_state
));
8322 /* nearly nothing to do */
8323 dumb_display_init(ds
);
8324 } else if (vnc_display
!= NULL
) {
8325 vnc_display_init(ds
);
8326 if (vnc_display_open(ds
, vnc_display
) < 0)
8329 #if defined(CONFIG_SDL)
8330 sdl_display_init(ds
, full_screen
, no_frame
);
8331 #elif defined(CONFIG_COCOA)
8332 cocoa_display_init(ds
, full_screen
);
8336 /* Maintain compatibility with multiple stdio monitors */
8337 if (!strcmp(monitor_device
,"stdio")) {
8338 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
8339 if (!strcmp(serial_devices
[i
],"mon:stdio")) {
8340 monitor_device
[0] = '\0';
8342 } else if (!strcmp(serial_devices
[i
],"stdio")) {
8343 monitor_device
[0] = '\0';
8344 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "mon:stdio");
8349 if (monitor_device
[0] != '\0') {
8350 monitor_hd
= qemu_chr_open(monitor_device
);
8352 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
8355 monitor_init(monitor_hd
, !nographic
);
8358 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
8359 const char *devname
= serial_devices
[i
];
8360 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
8361 serial_hds
[i
] = qemu_chr_open(devname
);
8362 if (!serial_hds
[i
]) {
8363 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
8367 if (strstart(devname
, "vc", 0))
8368 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
8372 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
8373 const char *devname
= parallel_devices
[i
];
8374 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
8375 parallel_hds
[i
] = qemu_chr_open(devname
);
8376 if (!parallel_hds
[i
]) {
8377 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
8381 if (strstart(devname
, "vc", 0))
8382 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
8386 machine
->init(ram_size
, vga_ram_size
, boot_device
,
8387 ds
, fd_filename
, snapshot
,
8388 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
8390 /* init USB devices */
8392 for(i
= 0; i
< usb_devices_index
; i
++) {
8393 if (usb_device_add(usb_devices
[i
]) < 0) {
8394 fprintf(stderr
, "Warning: could not add USB device %s\n",
8400 if (display_state
.dpy_refresh
) {
8401 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
8402 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
8405 #ifdef CONFIG_GDBSTUB
8407 /* XXX: use standard host:port notation and modify options
8409 if (gdbserver_start(gdbstub_port
) < 0) {
8410 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
8421 /* XXX: simplify init */
8434 len
= write(fds
[1], &status
, 1);
8435 if (len
== -1 && (errno
== EINTR
))
8441 TFR(fd
= open("/dev/null", O_RDWR
));