4 * Copyright (c) 2003-2008 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
25 #include "hw/boards.h"
27 #include "hw/pcmcia.h"
30 #include "hw/audiodev.h"
36 #include "qemu-timer.h"
37 #include "qemu-char.h"
39 #include "audio/audio.h"
50 #include <sys/times.h>
55 #include <sys/ioctl.h>
56 #include <sys/socket.h>
57 #include <netinet/in.h>
60 #include <sys/select.h>
61 #include <arpa/inet.h>
67 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
68 #include <freebsd/stdlib.h>
72 #include <linux/if_tun.h>
75 #include <linux/rtc.h>
77 /* For the benefit of older linux systems which don't supply it,
78 we use a local copy of hpet.h. */
79 /* #include <linux/hpet.h> */
82 #include <linux/ppdev.h>
83 #include <linux/parport.h>
86 #include <sys/ethernet.h>
87 #include <sys/sockio.h>
88 #include <netinet/arp.h>
89 #include <netinet/in.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/ip.h>
92 #include <netinet/ip_icmp.h> // must come after ip.h
93 #include <netinet/udp.h>
94 #include <netinet/tcp.h>
101 #include <winsock2.h>
102 int inet_aton(const char *cp
, struct in_addr
*ia
);
105 #if defined(CONFIG_SLIRP)
106 #include "libslirp.h"
111 #include <sys/timeb.h>
112 #include <mmsystem.h>
113 #define getopt_long_only getopt_long
114 #define memalign(align, size) malloc(size)
117 #include "qemu_socket.h"
123 #endif /* CONFIG_SDL */
127 #define main qemu_main
128 #endif /* CONFIG_COCOA */
132 #include "exec-all.h"
134 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
135 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
137 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
139 #define SMBD_COMMAND "/usr/sbin/smbd"
142 //#define DEBUG_UNUSED_IOPORT
143 //#define DEBUG_IOPORT
145 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
148 #define DEFAULT_RAM_SIZE 144
150 #define DEFAULT_RAM_SIZE 128
153 #define GUI_REFRESH_INTERVAL 30
155 /* Max number of USB devices that can be specified on the commandline. */
156 #define MAX_USB_CMDLINE 8
158 /* XXX: use a two level table to limit memory usage */
159 #define MAX_IOPORTS 65536
161 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
162 const char *bios_name
= NULL
;
163 void *ioport_opaque
[MAX_IOPORTS
];
164 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
165 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
166 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
167 to store the VM snapshots */
168 DriveInfo drives_table
[MAX_DRIVES
+1];
170 /* point to the block driver where the snapshots are managed */
171 BlockDriverState
*bs_snapshots
;
173 static DisplayState display_state
;
175 const char* keyboard_layout
= NULL
;
176 int64_t ticks_per_sec
;
178 int pit_min_timer_count
= 0;
180 NICInfo nd_table
[MAX_NICS
];
183 int rtc_start_date
= -1; /* -1 means now */
184 int cirrus_vga_enabled
= 1;
185 int vmsvga_enabled
= 0;
187 int graphic_width
= 1024;
188 int graphic_height
= 768;
189 int graphic_depth
= 8;
191 int graphic_width
= 800;
192 int graphic_height
= 600;
193 int graphic_depth
= 15;
198 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
199 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
201 int win2k_install_hack
= 0;
204 static VLANState
*first_vlan
;
206 const char *vnc_display
;
207 #if defined(TARGET_SPARC)
209 #elif defined(TARGET_I386)
214 int acpi_enabled
= 1;
218 int graphic_rotate
= 0;
220 const char *option_rom
[MAX_OPTION_ROMS
];
222 int semihosting_enabled
= 0;
227 const char *qemu_name
;
230 unsigned int nb_prom_envs
= 0;
231 const char *prom_envs
[MAX_PROM_ENVS
];
234 char drives_opt
[MAX_DRIVES
][1024];
236 static CPUState
*cur_cpu
;
237 static CPUState
*next_cpu
;
238 static int event_pending
= 1;
240 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
242 /***********************************************************/
243 /* x86 ISA bus support */
245 target_phys_addr_t isa_mem_base
= 0;
248 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
250 #ifdef DEBUG_UNUSED_IOPORT
251 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
256 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
258 #ifdef DEBUG_UNUSED_IOPORT
259 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
263 /* default is to make two byte accesses */
264 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
267 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
268 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
269 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
273 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
275 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
276 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
277 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
280 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
282 #ifdef DEBUG_UNUSED_IOPORT
283 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
288 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
290 #ifdef DEBUG_UNUSED_IOPORT
291 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
295 static void init_ioports(void)
299 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
300 ioport_read_table
[0][i
] = default_ioport_readb
;
301 ioport_write_table
[0][i
] = default_ioport_writeb
;
302 ioport_read_table
[1][i
] = default_ioport_readw
;
303 ioport_write_table
[1][i
] = default_ioport_writew
;
304 ioport_read_table
[2][i
] = default_ioport_readl
;
305 ioport_write_table
[2][i
] = default_ioport_writel
;
309 /* size is the word size in byte */
310 int register_ioport_read(int start
, int length
, int size
,
311 IOPortReadFunc
*func
, void *opaque
)
317 } else if (size
== 2) {
319 } else if (size
== 4) {
322 hw_error("register_ioport_read: invalid size");
325 for(i
= start
; i
< start
+ length
; i
+= size
) {
326 ioport_read_table
[bsize
][i
] = func
;
327 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
328 hw_error("register_ioport_read: invalid opaque");
329 ioport_opaque
[i
] = opaque
;
334 /* size is the word size in byte */
335 int register_ioport_write(int start
, int length
, int size
,
336 IOPortWriteFunc
*func
, void *opaque
)
342 } else if (size
== 2) {
344 } else if (size
== 4) {
347 hw_error("register_ioport_write: invalid size");
350 for(i
= start
; i
< start
+ length
; i
+= size
) {
351 ioport_write_table
[bsize
][i
] = func
;
352 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
353 hw_error("register_ioport_write: invalid opaque");
354 ioport_opaque
[i
] = opaque
;
359 void isa_unassign_ioport(int start
, int length
)
363 for(i
= start
; i
< start
+ length
; i
++) {
364 ioport_read_table
[0][i
] = default_ioport_readb
;
365 ioport_read_table
[1][i
] = default_ioport_readw
;
366 ioport_read_table
[2][i
] = default_ioport_readl
;
368 ioport_write_table
[0][i
] = default_ioport_writeb
;
369 ioport_write_table
[1][i
] = default_ioport_writew
;
370 ioport_write_table
[2][i
] = default_ioport_writel
;
374 /***********************************************************/
376 void cpu_outb(CPUState
*env
, int addr
, int val
)
379 if (loglevel
& CPU_LOG_IOPORT
)
380 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
382 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
385 env
->last_io_time
= cpu_get_time_fast();
389 void cpu_outw(CPUState
*env
, int addr
, int val
)
392 if (loglevel
& CPU_LOG_IOPORT
)
393 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
395 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
398 env
->last_io_time
= cpu_get_time_fast();
402 void cpu_outl(CPUState
*env
, int addr
, int val
)
405 if (loglevel
& CPU_LOG_IOPORT
)
406 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
408 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
411 env
->last_io_time
= cpu_get_time_fast();
415 int cpu_inb(CPUState
*env
, int addr
)
418 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
420 if (loglevel
& CPU_LOG_IOPORT
)
421 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
425 env
->last_io_time
= cpu_get_time_fast();
430 int cpu_inw(CPUState
*env
, int addr
)
433 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
435 if (loglevel
& CPU_LOG_IOPORT
)
436 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
440 env
->last_io_time
= cpu_get_time_fast();
445 int cpu_inl(CPUState
*env
, int addr
)
448 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
450 if (loglevel
& CPU_LOG_IOPORT
)
451 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
455 env
->last_io_time
= cpu_get_time_fast();
460 /***********************************************************/
461 void hw_error(const char *fmt
, ...)
467 fprintf(stderr
, "qemu: hardware error: ");
468 vfprintf(stderr
, fmt
, ap
);
469 fprintf(stderr
, "\n");
470 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
471 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
473 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
475 cpu_dump_state(env
, stderr
, fprintf
, 0);
482 /***********************************************************/
485 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
486 static void *qemu_put_kbd_event_opaque
;
487 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
488 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
490 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
492 qemu_put_kbd_event_opaque
= opaque
;
493 qemu_put_kbd_event
= func
;
496 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
497 void *opaque
, int absolute
,
500 QEMUPutMouseEntry
*s
, *cursor
;
502 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
506 s
->qemu_put_mouse_event
= func
;
507 s
->qemu_put_mouse_event_opaque
= opaque
;
508 s
->qemu_put_mouse_event_absolute
= absolute
;
509 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
512 if (!qemu_put_mouse_event_head
) {
513 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
517 cursor
= qemu_put_mouse_event_head
;
518 while (cursor
->next
!= NULL
)
519 cursor
= cursor
->next
;
522 qemu_put_mouse_event_current
= s
;
527 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
529 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
531 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
534 cursor
= qemu_put_mouse_event_head
;
535 while (cursor
!= NULL
&& cursor
!= entry
) {
537 cursor
= cursor
->next
;
540 if (cursor
== NULL
) // does not exist or list empty
542 else if (prev
== NULL
) { // entry is head
543 qemu_put_mouse_event_head
= cursor
->next
;
544 if (qemu_put_mouse_event_current
== entry
)
545 qemu_put_mouse_event_current
= cursor
->next
;
546 qemu_free(entry
->qemu_put_mouse_event_name
);
551 prev
->next
= entry
->next
;
553 if (qemu_put_mouse_event_current
== entry
)
554 qemu_put_mouse_event_current
= prev
;
556 qemu_free(entry
->qemu_put_mouse_event_name
);
560 void kbd_put_keycode(int keycode
)
562 if (qemu_put_kbd_event
) {
563 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
567 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
569 QEMUPutMouseEvent
*mouse_event
;
570 void *mouse_event_opaque
;
573 if (!qemu_put_mouse_event_current
) {
578 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
580 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
583 if (graphic_rotate
) {
584 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
587 width
= graphic_width
;
588 mouse_event(mouse_event_opaque
,
589 width
- dy
, dx
, dz
, buttons_state
);
591 mouse_event(mouse_event_opaque
,
592 dx
, dy
, dz
, buttons_state
);
596 int kbd_mouse_is_absolute(void)
598 if (!qemu_put_mouse_event_current
)
601 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
604 void do_info_mice(void)
606 QEMUPutMouseEntry
*cursor
;
609 if (!qemu_put_mouse_event_head
) {
610 term_printf("No mouse devices connected\n");
614 term_printf("Mouse devices available:\n");
615 cursor
= qemu_put_mouse_event_head
;
616 while (cursor
!= NULL
) {
617 term_printf("%c Mouse #%d: %s\n",
618 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
619 index
, cursor
->qemu_put_mouse_event_name
);
621 cursor
= cursor
->next
;
625 void do_mouse_set(int index
)
627 QEMUPutMouseEntry
*cursor
;
630 if (!qemu_put_mouse_event_head
) {
631 term_printf("No mouse devices connected\n");
635 cursor
= qemu_put_mouse_event_head
;
636 while (cursor
!= NULL
&& index
!= i
) {
638 cursor
= cursor
->next
;
642 qemu_put_mouse_event_current
= cursor
;
644 term_printf("Mouse at given index not found\n");
647 /* compute with 96 bit intermediate result: (a*b)/c */
648 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
653 #ifdef WORDS_BIGENDIAN
663 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
664 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
667 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
671 /***********************************************************/
672 /* real time host monotonic timer */
674 #define QEMU_TIMER_BASE 1000000000LL
678 static int64_t clock_freq
;
680 static void init_get_clock(void)
684 ret
= QueryPerformanceFrequency(&freq
);
686 fprintf(stderr
, "Could not calibrate ticks\n");
689 clock_freq
= freq
.QuadPart
;
692 static int64_t get_clock(void)
695 QueryPerformanceCounter(&ti
);
696 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
701 static int use_rt_clock
;
703 static void init_get_clock(void)
706 #if defined(__linux__)
709 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
716 static int64_t get_clock(void)
718 #if defined(__linux__)
721 clock_gettime(CLOCK_MONOTONIC
, &ts
);
722 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
726 /* XXX: using gettimeofday leads to problems if the date
727 changes, so it should be avoided. */
729 gettimeofday(&tv
, NULL
);
730 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
736 /***********************************************************/
737 /* guest cycle counter */
739 static int64_t cpu_ticks_prev
;
740 static int64_t cpu_ticks_offset
;
741 static int64_t cpu_clock_offset
;
742 static int cpu_ticks_enabled
;
744 /* return the host CPU cycle counter and handle stop/restart */
745 int64_t cpu_get_ticks(void)
747 if (!cpu_ticks_enabled
) {
748 return cpu_ticks_offset
;
751 ticks
= cpu_get_real_ticks();
752 if (cpu_ticks_prev
> ticks
) {
753 /* Note: non increasing ticks may happen if the host uses
755 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
757 cpu_ticks_prev
= ticks
;
758 return ticks
+ cpu_ticks_offset
;
762 /* return the host CPU monotonic timer and handle stop/restart */
763 static int64_t cpu_get_clock(void)
766 if (!cpu_ticks_enabled
) {
767 return cpu_clock_offset
;
770 return ti
+ cpu_clock_offset
;
774 /* enable cpu_get_ticks() */
775 void cpu_enable_ticks(void)
777 if (!cpu_ticks_enabled
) {
778 cpu_ticks_offset
-= cpu_get_real_ticks();
779 cpu_clock_offset
-= get_clock();
780 cpu_ticks_enabled
= 1;
784 /* disable cpu_get_ticks() : the clock is stopped. You must not call
785 cpu_get_ticks() after that. */
786 void cpu_disable_ticks(void)
788 if (cpu_ticks_enabled
) {
789 cpu_ticks_offset
= cpu_get_ticks();
790 cpu_clock_offset
= cpu_get_clock();
791 cpu_ticks_enabled
= 0;
795 /***********************************************************/
798 #define QEMU_TIMER_REALTIME 0
799 #define QEMU_TIMER_VIRTUAL 1
803 /* XXX: add frequency */
811 struct QEMUTimer
*next
;
814 struct qemu_alarm_timer
{
818 int (*start
)(struct qemu_alarm_timer
*t
);
819 void (*stop
)(struct qemu_alarm_timer
*t
);
820 void (*rearm
)(struct qemu_alarm_timer
*t
);
824 #define ALARM_FLAG_DYNTICKS 0x1
825 #define ALARM_FLAG_EXPIRED 0x2
827 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
829 return t
->flags
& ALARM_FLAG_DYNTICKS
;
832 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
834 if (!alarm_has_dynticks(t
))
840 /* TODO: MIN_TIMER_REARM_US should be optimized */
841 #define MIN_TIMER_REARM_US 250
843 static struct qemu_alarm_timer
*alarm_timer
;
847 struct qemu_alarm_win32
{
851 } alarm_win32_data
= {0, NULL
, -1};
853 static int win32_start_timer(struct qemu_alarm_timer
*t
);
854 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
855 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
859 static int unix_start_timer(struct qemu_alarm_timer
*t
);
860 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
864 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
865 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
866 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
868 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
869 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
871 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
872 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
874 #endif /* __linux__ */
878 static struct qemu_alarm_timer alarm_timers
[] = {
881 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
882 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
883 /* HPET - if available - is preferred */
884 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
885 /* ...otherwise try RTC */
886 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
888 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
890 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
891 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
892 {"win32", 0, win32_start_timer
,
893 win32_stop_timer
, NULL
, &alarm_win32_data
},
898 static void show_available_alarms()
902 printf("Available alarm timers, in order of precedence:\n");
903 for (i
= 0; alarm_timers
[i
].name
; i
++)
904 printf("%s\n", alarm_timers
[i
].name
);
907 static void configure_alarms(char const *opt
)
911 int count
= (sizeof(alarm_timers
) / sizeof(*alarm_timers
)) - 1;
915 if (!strcmp(opt
, "help")) {
916 show_available_alarms();
922 /* Reorder the array */
923 name
= strtok(arg
, ",");
925 struct qemu_alarm_timer tmp
;
927 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
928 if (!strcmp(alarm_timers
[i
].name
, name
))
933 fprintf(stderr
, "Unknown clock %s\n", name
);
942 tmp
= alarm_timers
[i
];
943 alarm_timers
[i
] = alarm_timers
[cur
];
944 alarm_timers
[cur
] = tmp
;
948 name
= strtok(NULL
, ",");
954 /* Disable remaining timers */
955 for (i
= cur
; i
< count
; i
++)
956 alarm_timers
[i
].name
= NULL
;
960 show_available_alarms();
966 static QEMUTimer
*active_timers
[2];
968 static QEMUClock
*qemu_new_clock(int type
)
971 clock
= qemu_mallocz(sizeof(QEMUClock
));
978 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
982 ts
= qemu_mallocz(sizeof(QEMUTimer
));
989 void qemu_free_timer(QEMUTimer
*ts
)
994 /* stop a timer, but do not dealloc it */
995 void qemu_del_timer(QEMUTimer
*ts
)
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
];
1014 /* modify the current timer so that it will be fired when current_time
1015 >= expire_time. The corresponding callback will be called. */
1016 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1022 /* add the timer in the sorted list */
1023 /* NOTE: this code must be signal safe because
1024 qemu_timer_expired() can be called from a signal. */
1025 pt
= &active_timers
[ts
->clock
->type
];
1030 if (t
->expire_time
> expire_time
)
1034 ts
->expire_time
= expire_time
;
1038 /* Rearm if necessary */
1039 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0 &&
1040 pt
== &active_timers
[ts
->clock
->type
])
1041 qemu_rearm_alarm_timer(alarm_timer
);
1044 int qemu_timer_pending(QEMUTimer
*ts
)
1047 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1054 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1058 return (timer_head
->expire_time
<= current_time
);
1061 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1067 if (!ts
|| ts
->expire_time
> current_time
)
1069 /* remove timer from the list before calling the callback */
1070 *ptimer_head
= ts
->next
;
1073 /* run the callback (the timer list can be modified) */
1078 int64_t qemu_get_clock(QEMUClock
*clock
)
1080 switch(clock
->type
) {
1081 case QEMU_TIMER_REALTIME
:
1082 return get_clock() / 1000000;
1084 case QEMU_TIMER_VIRTUAL
:
1085 return cpu_get_clock();
1089 static void init_timers(void)
1092 ticks_per_sec
= QEMU_TIMER_BASE
;
1093 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1094 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1098 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1100 uint64_t expire_time
;
1102 if (qemu_timer_pending(ts
)) {
1103 expire_time
= ts
->expire_time
;
1107 qemu_put_be64(f
, expire_time
);
1110 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1112 uint64_t expire_time
;
1114 expire_time
= qemu_get_be64(f
);
1115 if (expire_time
!= -1) {
1116 qemu_mod_timer(ts
, expire_time
);
1122 static void timer_save(QEMUFile
*f
, void *opaque
)
1124 if (cpu_ticks_enabled
) {
1125 hw_error("cannot save state if virtual timers are running");
1127 qemu_put_be64(f
, cpu_ticks_offset
);
1128 qemu_put_be64(f
, ticks_per_sec
);
1129 qemu_put_be64(f
, cpu_clock_offset
);
1132 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1134 if (version_id
!= 1 && version_id
!= 2)
1136 if (cpu_ticks_enabled
) {
1139 cpu_ticks_offset
=qemu_get_be64(f
);
1140 ticks_per_sec
=qemu_get_be64(f
);
1141 if (version_id
== 2) {
1142 cpu_clock_offset
=qemu_get_be64(f
);
1148 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1149 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1151 static void host_alarm_handler(int host_signum
)
1155 #define DISP_FREQ 1000
1157 static int64_t delta_min
= INT64_MAX
;
1158 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1160 ti
= qemu_get_clock(vm_clock
);
1161 if (last_clock
!= 0) {
1162 delta
= ti
- last_clock
;
1163 if (delta
< delta_min
)
1165 if (delta
> delta_max
)
1168 if (++count
== DISP_FREQ
) {
1169 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1170 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1171 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1172 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1173 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1175 delta_min
= INT64_MAX
;
1183 if (alarm_has_dynticks(alarm_timer
) ||
1184 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1185 qemu_get_clock(vm_clock
)) ||
1186 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1187 qemu_get_clock(rt_clock
))) {
1189 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1190 SetEvent(data
->host_alarm
);
1192 CPUState
*env
= next_cpu
;
1194 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1197 /* stop the currently executing cpu because a timer occured */
1198 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1200 if (env
->kqemu_enabled
) {
1201 kqemu_cpu_interrupt(env
);
1209 static uint64_t qemu_next_deadline(void)
1211 int64_t nearest_delta_us
= INT64_MAX
;
1214 if (active_timers
[QEMU_TIMER_REALTIME
])
1215 nearest_delta_us
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1216 qemu_get_clock(rt_clock
))*1000;
1218 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1220 vmdelta_us
= (active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1221 qemu_get_clock(vm_clock
)+999)/1000;
1222 if (vmdelta_us
< nearest_delta_us
)
1223 nearest_delta_us
= vmdelta_us
;
1226 /* Avoid arming the timer to negative, zero, or too low values */
1227 if (nearest_delta_us
<= MIN_TIMER_REARM_US
)
1228 nearest_delta_us
= MIN_TIMER_REARM_US
;
1230 return nearest_delta_us
;
1235 #if defined(__linux__)
1237 #define RTC_FREQ 1024
1239 static void enable_sigio_timer(int fd
)
1241 struct sigaction act
;
1244 sigfillset(&act
.sa_mask
);
1246 act
.sa_handler
= host_alarm_handler
;
1248 sigaction(SIGIO
, &act
, NULL
);
1249 fcntl(fd
, F_SETFL
, O_ASYNC
);
1250 fcntl(fd
, F_SETOWN
, getpid());
1253 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1255 struct hpet_info info
;
1258 fd
= open("/dev/hpet", O_RDONLY
);
1263 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1265 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1266 "error, but for better emulation accuracy type:\n"
1267 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1271 /* Check capabilities */
1272 r
= ioctl(fd
, HPET_INFO
, &info
);
1276 /* Enable periodic mode */
1277 r
= ioctl(fd
, HPET_EPI
, 0);
1278 if (info
.hi_flags
&& (r
< 0))
1281 /* Enable interrupt */
1282 r
= ioctl(fd
, HPET_IE_ON
, 0);
1286 enable_sigio_timer(fd
);
1287 t
->priv
= (void *)(long)fd
;
1295 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1297 int fd
= (long)t
->priv
;
1302 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1306 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1309 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1310 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1311 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1312 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1315 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1321 enable_sigio_timer(rtc_fd
);
1323 t
->priv
= (void *)(long)rtc_fd
;
1328 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1330 int rtc_fd
= (long)t
->priv
;
1335 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1339 struct sigaction act
;
1341 sigfillset(&act
.sa_mask
);
1343 act
.sa_handler
= host_alarm_handler
;
1345 sigaction(SIGALRM
, &act
, NULL
);
1347 ev
.sigev_value
.sival_int
= 0;
1348 ev
.sigev_notify
= SIGEV_SIGNAL
;
1349 ev
.sigev_signo
= SIGALRM
;
1351 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1352 perror("timer_create");
1354 /* disable dynticks */
1355 fprintf(stderr
, "Dynamic Ticks disabled\n");
1360 t
->priv
= (void *)host_timer
;
1365 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1367 timer_t host_timer
= (timer_t
)t
->priv
;
1369 timer_delete(host_timer
);
1372 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1374 timer_t host_timer
= (timer_t
)t
->priv
;
1375 struct itimerspec timeout
;
1376 int64_t nearest_delta_us
= INT64_MAX
;
1379 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1380 !active_timers
[QEMU_TIMER_VIRTUAL
])
1383 nearest_delta_us
= qemu_next_deadline();
1385 /* check whether a timer is already running */
1386 if (timer_gettime(host_timer
, &timeout
)) {
1388 fprintf(stderr
, "Internal timer error: aborting\n");
1391 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1392 if (current_us
&& current_us
<= nearest_delta_us
)
1395 timeout
.it_interval
.tv_sec
= 0;
1396 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1397 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1398 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1399 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1401 fprintf(stderr
, "Internal timer error: aborting\n");
1406 #endif /* defined(__linux__) */
1408 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1410 struct sigaction act
;
1411 struct itimerval itv
;
1415 sigfillset(&act
.sa_mask
);
1417 act
.sa_handler
= host_alarm_handler
;
1419 sigaction(SIGALRM
, &act
, NULL
);
1421 itv
.it_interval
.tv_sec
= 0;
1422 /* for i386 kernel 2.6 to get 1 ms */
1423 itv
.it_interval
.tv_usec
= 999;
1424 itv
.it_value
.tv_sec
= 0;
1425 itv
.it_value
.tv_usec
= 10 * 1000;
1427 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1434 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1436 struct itimerval itv
;
1438 memset(&itv
, 0, sizeof(itv
));
1439 setitimer(ITIMER_REAL
, &itv
, NULL
);
1442 #endif /* !defined(_WIN32) */
1446 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1449 struct qemu_alarm_win32
*data
= t
->priv
;
1452 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1453 if (!data
->host_alarm
) {
1454 perror("Failed CreateEvent");
1458 memset(&tc
, 0, sizeof(tc
));
1459 timeGetDevCaps(&tc
, sizeof(tc
));
1461 if (data
->period
< tc
.wPeriodMin
)
1462 data
->period
= tc
.wPeriodMin
;
1464 timeBeginPeriod(data
->period
);
1466 flags
= TIME_CALLBACK_FUNCTION
;
1467 if (alarm_has_dynticks(t
))
1468 flags
|= TIME_ONESHOT
;
1470 flags
|= TIME_PERIODIC
;
1472 data
->timerId
= timeSetEvent(1, // interval (ms)
1473 data
->period
, // resolution
1474 host_alarm_handler
, // function
1475 (DWORD
)t
, // parameter
1478 if (!data
->timerId
) {
1479 perror("Failed to initialize win32 alarm timer");
1481 timeEndPeriod(data
->period
);
1482 CloseHandle(data
->host_alarm
);
1486 qemu_add_wait_object(data
->host_alarm
, NULL
, NULL
);
1491 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1493 struct qemu_alarm_win32
*data
= t
->priv
;
1495 timeKillEvent(data
->timerId
);
1496 timeEndPeriod(data
->period
);
1498 CloseHandle(data
->host_alarm
);
1501 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1503 struct qemu_alarm_win32
*data
= t
->priv
;
1504 uint64_t nearest_delta_us
;
1506 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1507 !active_timers
[QEMU_TIMER_VIRTUAL
])
1510 nearest_delta_us
= qemu_next_deadline();
1511 nearest_delta_us
/= 1000;
1513 timeKillEvent(data
->timerId
);
1515 data
->timerId
= timeSetEvent(1,
1519 TIME_ONESHOT
| TIME_PERIODIC
);
1521 if (!data
->timerId
) {
1522 perror("Failed to re-arm win32 alarm timer");
1524 timeEndPeriod(data
->period
);
1525 CloseHandle(data
->host_alarm
);
1532 static void init_timer_alarm(void)
1534 struct qemu_alarm_timer
*t
;
1537 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1538 t
= &alarm_timers
[i
];
1546 fprintf(stderr
, "Unable to find any suitable alarm timer.\n");
1547 fprintf(stderr
, "Terminating\n");
1554 static void quit_timers(void)
1556 alarm_timer
->stop(alarm_timer
);
1560 /***********************************************************/
1561 /* character device */
1563 static void qemu_chr_event(CharDriverState
*s
, int event
)
1567 s
->chr_event(s
->handler_opaque
, event
);
1570 static void qemu_chr_reset_bh(void *opaque
)
1572 CharDriverState
*s
= opaque
;
1573 qemu_chr_event(s
, CHR_EVENT_RESET
);
1574 qemu_bh_delete(s
->bh
);
1578 void qemu_chr_reset(CharDriverState
*s
)
1580 if (s
->bh
== NULL
) {
1581 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
1582 qemu_bh_schedule(s
->bh
);
1586 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1588 return s
->chr_write(s
, buf
, len
);
1591 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1595 return s
->chr_ioctl(s
, cmd
, arg
);
1598 int qemu_chr_can_read(CharDriverState
*s
)
1600 if (!s
->chr_can_read
)
1602 return s
->chr_can_read(s
->handler_opaque
);
1605 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
1607 s
->chr_read(s
->handler_opaque
, buf
, len
);
1610 void qemu_chr_accept_input(CharDriverState
*s
)
1612 if (s
->chr_accept_input
)
1613 s
->chr_accept_input(s
);
1616 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1621 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1622 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
1626 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1628 if (s
->chr_send_event
)
1629 s
->chr_send_event(s
, event
);
1632 void qemu_chr_add_handlers(CharDriverState
*s
,
1633 IOCanRWHandler
*fd_can_read
,
1634 IOReadHandler
*fd_read
,
1635 IOEventHandler
*fd_event
,
1638 s
->chr_can_read
= fd_can_read
;
1639 s
->chr_read
= fd_read
;
1640 s
->chr_event
= fd_event
;
1641 s
->handler_opaque
= opaque
;
1642 if (s
->chr_update_read_handler
)
1643 s
->chr_update_read_handler(s
);
1646 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1651 static CharDriverState
*qemu_chr_open_null(void)
1653 CharDriverState
*chr
;
1655 chr
= qemu_mallocz(sizeof(CharDriverState
));
1658 chr
->chr_write
= null_chr_write
;
1662 /* MUX driver for serial I/O splitting */
1663 static int term_timestamps
;
1664 static int64_t term_timestamps_start
;
1666 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
1667 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
1669 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
1670 IOReadHandler
*chr_read
[MAX_MUX
];
1671 IOEventHandler
*chr_event
[MAX_MUX
];
1672 void *ext_opaque
[MAX_MUX
];
1673 CharDriverState
*drv
;
1674 unsigned char buffer
[MUX_BUFFER_SIZE
];
1678 int term_got_escape
;
1683 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1685 MuxDriver
*d
= chr
->opaque
;
1687 if (!term_timestamps
) {
1688 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
1693 for(i
= 0; i
< len
; i
++) {
1694 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
1695 if (buf
[i
] == '\n') {
1701 if (term_timestamps_start
== -1)
1702 term_timestamps_start
= ti
;
1703 ti
-= term_timestamps_start
;
1704 secs
= ti
/ 1000000000;
1705 snprintf(buf1
, sizeof(buf1
),
1706 "[%02d:%02d:%02d.%03d] ",
1710 (int)((ti
/ 1000000) % 1000));
1711 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
1718 static char *mux_help
[] = {
1719 "% h print this help\n\r",
1720 "% x exit emulator\n\r",
1721 "% s save disk data back to file (if -snapshot)\n\r",
1722 "% t toggle console timestamps\n\r"
1723 "% b send break (magic sysrq)\n\r",
1724 "% c switch between console and monitor\n\r",
1729 static int term_escape_char
= 0x01; /* ctrl-a is used for escape */
1730 static void mux_print_help(CharDriverState
*chr
)
1733 char ebuf
[15] = "Escape-Char";
1734 char cbuf
[50] = "\n\r";
1736 if (term_escape_char
> 0 && term_escape_char
< 26) {
1737 sprintf(cbuf
,"\n\r");
1738 sprintf(ebuf
,"C-%c", term_escape_char
- 1 + 'a');
1740 sprintf(cbuf
,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
1743 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
1744 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
1745 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
1746 if (mux_help
[i
][j
] == '%')
1747 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
1749 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
1754 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
1756 if (d
->term_got_escape
) {
1757 d
->term_got_escape
= 0;
1758 if (ch
== term_escape_char
)
1763 mux_print_help(chr
);
1767 char *term
= "QEMU: Terminated\n\r";
1768 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
1775 for (i
= 0; i
< nb_drives
; i
++) {
1776 bdrv_commit(drives_table
[i
].bdrv
);
1781 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1784 /* Switch to the next registered device */
1786 if (chr
->focus
>= d
->mux_cnt
)
1790 term_timestamps
= !term_timestamps
;
1791 term_timestamps_start
= -1;
1794 } else if (ch
== term_escape_char
) {
1795 d
->term_got_escape
= 1;
1803 static void mux_chr_accept_input(CharDriverState
*chr
)
1806 MuxDriver
*d
= chr
->opaque
;
1808 while (d
->prod
!= d
->cons
&&
1809 d
->chr_can_read
[m
] &&
1810 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
1811 d
->chr_read
[m
](d
->ext_opaque
[m
],
1812 &d
->buffer
[d
->cons
++ & MUX_BUFFER_MASK
], 1);
1816 static int mux_chr_can_read(void *opaque
)
1818 CharDriverState
*chr
= opaque
;
1819 MuxDriver
*d
= chr
->opaque
;
1821 if ((d
->prod
- d
->cons
) < MUX_BUFFER_SIZE
)
1823 if (d
->chr_can_read
[chr
->focus
])
1824 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
1828 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
1830 CharDriverState
*chr
= opaque
;
1831 MuxDriver
*d
= chr
->opaque
;
1835 mux_chr_accept_input (opaque
);
1837 for(i
= 0; i
< size
; i
++)
1838 if (mux_proc_byte(chr
, d
, buf
[i
])) {
1839 if (d
->prod
== d
->cons
&&
1840 d
->chr_can_read
[m
] &&
1841 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
1842 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
1844 d
->buffer
[d
->prod
++ & MUX_BUFFER_MASK
] = buf
[i
];
1848 static void mux_chr_event(void *opaque
, int event
)
1850 CharDriverState
*chr
= opaque
;
1851 MuxDriver
*d
= chr
->opaque
;
1854 /* Send the event to all registered listeners */
1855 for (i
= 0; i
< d
->mux_cnt
; i
++)
1856 if (d
->chr_event
[i
])
1857 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
1860 static void mux_chr_update_read_handler(CharDriverState
*chr
)
1862 MuxDriver
*d
= chr
->opaque
;
1864 if (d
->mux_cnt
>= MAX_MUX
) {
1865 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
1868 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
1869 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
1870 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
1871 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
1872 /* Fix up the real driver with mux routines */
1873 if (d
->mux_cnt
== 0) {
1874 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
1875 mux_chr_event
, chr
);
1877 chr
->focus
= d
->mux_cnt
;
1881 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
1883 CharDriverState
*chr
;
1886 chr
= qemu_mallocz(sizeof(CharDriverState
));
1889 d
= qemu_mallocz(sizeof(MuxDriver
));
1898 chr
->chr_write
= mux_chr_write
;
1899 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
1900 chr
->chr_accept_input
= mux_chr_accept_input
;
1907 static void socket_cleanup(void)
1912 static int socket_init(void)
1917 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1919 err
= WSAGetLastError();
1920 fprintf(stderr
, "WSAStartup: %d\n", err
);
1923 atexit(socket_cleanup
);
1927 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1933 ret
= send(fd
, buf
, len
, 0);
1936 errno
= WSAGetLastError();
1937 if (errno
!= WSAEWOULDBLOCK
) {
1940 } else if (ret
== 0) {
1950 void socket_set_nonblock(int fd
)
1952 unsigned long opt
= 1;
1953 ioctlsocket(fd
, FIONBIO
, &opt
);
1958 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1964 ret
= write(fd
, buf
, len
);
1966 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1968 } else if (ret
== 0) {
1978 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1980 return unix_write(fd
, buf
, len1
);
1983 void socket_set_nonblock(int fd
)
1985 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1987 #endif /* !_WIN32 */
1996 #define STDIO_MAX_CLIENTS 1
1997 static int stdio_nb_clients
= 0;
1999 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2001 FDCharDriver
*s
= chr
->opaque
;
2002 return unix_write(s
->fd_out
, buf
, len
);
2005 static int fd_chr_read_poll(void *opaque
)
2007 CharDriverState
*chr
= opaque
;
2008 FDCharDriver
*s
= chr
->opaque
;
2010 s
->max_size
= qemu_chr_can_read(chr
);
2014 static void fd_chr_read(void *opaque
)
2016 CharDriverState
*chr
= opaque
;
2017 FDCharDriver
*s
= chr
->opaque
;
2022 if (len
> s
->max_size
)
2026 size
= read(s
->fd_in
, buf
, len
);
2028 /* FD has been closed. Remove it from the active list. */
2029 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
2033 qemu_chr_read(chr
, buf
, size
);
2037 static void fd_chr_update_read_handler(CharDriverState
*chr
)
2039 FDCharDriver
*s
= chr
->opaque
;
2041 if (s
->fd_in
>= 0) {
2042 if (nographic
&& s
->fd_in
== 0) {
2044 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
2045 fd_chr_read
, NULL
, chr
);
2050 /* open a character device to a unix fd */
2051 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
2053 CharDriverState
*chr
;
2056 chr
= qemu_mallocz(sizeof(CharDriverState
));
2059 s
= qemu_mallocz(sizeof(FDCharDriver
));
2067 chr
->chr_write
= fd_chr_write
;
2068 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
2070 qemu_chr_reset(chr
);
2075 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
2079 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
2082 return qemu_chr_open_fd(-1, fd_out
);
2085 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
2088 char filename_in
[256], filename_out
[256];
2090 snprintf(filename_in
, 256, "%s.in", filename
);
2091 snprintf(filename_out
, 256, "%s.out", filename
);
2092 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
2093 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
2094 if (fd_in
< 0 || fd_out
< 0) {
2099 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
2103 return qemu_chr_open_fd(fd_in
, fd_out
);
2107 /* for STDIO, we handle the case where several clients use it
2110 #define TERM_FIFO_MAX_SIZE 1
2112 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
2113 static int term_fifo_size
;
2115 static int stdio_read_poll(void *opaque
)
2117 CharDriverState
*chr
= opaque
;
2119 /* try to flush the queue if needed */
2120 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
2121 qemu_chr_read(chr
, term_fifo
, 1);
2124 /* see if we can absorb more chars */
2125 if (term_fifo_size
== 0)
2131 static void stdio_read(void *opaque
)
2135 CharDriverState
*chr
= opaque
;
2137 size
= read(0, buf
, 1);
2139 /* stdin has been closed. Remove it from the active list. */
2140 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
2144 if (qemu_chr_can_read(chr
) > 0) {
2145 qemu_chr_read(chr
, buf
, 1);
2146 } else if (term_fifo_size
== 0) {
2147 term_fifo
[term_fifo_size
++] = buf
[0];
2152 /* init terminal so that we can grab keys */
2153 static struct termios oldtty
;
2154 static int old_fd0_flags
;
2156 static void term_exit(void)
2158 tcsetattr (0, TCSANOW
, &oldtty
);
2159 fcntl(0, F_SETFL
, old_fd0_flags
);
2162 static void term_init(void)
2166 tcgetattr (0, &tty
);
2168 old_fd0_flags
= fcntl(0, F_GETFL
);
2170 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
2171 |INLCR
|IGNCR
|ICRNL
|IXON
);
2172 tty
.c_oflag
|= OPOST
;
2173 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
2174 /* if graphical mode, we allow Ctrl-C handling */
2176 tty
.c_lflag
&= ~ISIG
;
2177 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
2180 tty
.c_cc
[VTIME
] = 0;
2182 tcsetattr (0, TCSANOW
, &tty
);
2186 fcntl(0, F_SETFL
, O_NONBLOCK
);
2189 static CharDriverState
*qemu_chr_open_stdio(void)
2191 CharDriverState
*chr
;
2193 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
2195 chr
= qemu_chr_open_fd(0, 1);
2196 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
2203 #if defined(__linux__) || defined(__sun__)
2204 static CharDriverState
*qemu_chr_open_pty(void)
2207 char slave_name
[1024];
2208 int master_fd
, slave_fd
;
2210 #if defined(__linux__)
2211 /* Not satisfying */
2212 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
2217 /* Disabling local echo and line-buffered output */
2218 tcgetattr (master_fd
, &tty
);
2219 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
2221 tty
.c_cc
[VTIME
] = 0;
2222 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
2224 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
2225 return qemu_chr_open_fd(master_fd
, master_fd
);
2228 static void tty_serial_init(int fd
, int speed
,
2229 int parity
, int data_bits
, int stop_bits
)
2235 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
2236 speed
, parity
, data_bits
, stop_bits
);
2238 tcgetattr (fd
, &tty
);
2280 cfsetispeed(&tty
, spd
);
2281 cfsetospeed(&tty
, spd
);
2283 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
2284 |INLCR
|IGNCR
|ICRNL
|IXON
);
2285 tty
.c_oflag
|= OPOST
;
2286 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
2287 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
2308 tty
.c_cflag
|= PARENB
;
2311 tty
.c_cflag
|= PARENB
| PARODD
;
2315 tty
.c_cflag
|= CSTOPB
;
2317 tcsetattr (fd
, TCSANOW
, &tty
);
2320 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2322 FDCharDriver
*s
= chr
->opaque
;
2325 case CHR_IOCTL_SERIAL_SET_PARAMS
:
2327 QEMUSerialSetParams
*ssp
= arg
;
2328 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
2329 ssp
->data_bits
, ssp
->stop_bits
);
2332 case CHR_IOCTL_SERIAL_SET_BREAK
:
2334 int enable
= *(int *)arg
;
2336 tcsendbreak(s
->fd_in
, 1);
2345 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
2347 CharDriverState
*chr
;
2350 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
2351 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2352 tty_serial_init(fd
, 115200, 'N', 8, 1);
2353 chr
= qemu_chr_open_fd(fd
, fd
);
2358 chr
->chr_ioctl
= tty_serial_ioctl
;
2359 qemu_chr_reset(chr
);
2362 #else /* ! __linux__ && ! __sun__ */
2363 static CharDriverState
*qemu_chr_open_pty(void)
2367 #endif /* __linux__ || __sun__ */
2369 #if defined(__linux__)
2373 } ParallelCharDriver
;
2375 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
2377 if (s
->mode
!= mode
) {
2379 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
2386 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2388 ParallelCharDriver
*drv
= chr
->opaque
;
2393 case CHR_IOCTL_PP_READ_DATA
:
2394 if (ioctl(fd
, PPRDATA
, &b
) < 0)
2396 *(uint8_t *)arg
= b
;
2398 case CHR_IOCTL_PP_WRITE_DATA
:
2399 b
= *(uint8_t *)arg
;
2400 if (ioctl(fd
, PPWDATA
, &b
) < 0)
2403 case CHR_IOCTL_PP_READ_CONTROL
:
2404 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
2406 /* Linux gives only the lowest bits, and no way to know data
2407 direction! For better compatibility set the fixed upper
2409 *(uint8_t *)arg
= b
| 0xc0;
2411 case CHR_IOCTL_PP_WRITE_CONTROL
:
2412 b
= *(uint8_t *)arg
;
2413 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
2416 case CHR_IOCTL_PP_READ_STATUS
:
2417 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
2419 *(uint8_t *)arg
= b
;
2421 case CHR_IOCTL_PP_EPP_READ_ADDR
:
2422 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2423 struct ParallelIOArg
*parg
= arg
;
2424 int n
= read(fd
, parg
->buffer
, parg
->count
);
2425 if (n
!= parg
->count
) {
2430 case CHR_IOCTL_PP_EPP_READ
:
2431 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2432 struct ParallelIOArg
*parg
= arg
;
2433 int n
= read(fd
, parg
->buffer
, parg
->count
);
2434 if (n
!= parg
->count
) {
2439 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
2440 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2441 struct ParallelIOArg
*parg
= arg
;
2442 int n
= write(fd
, parg
->buffer
, parg
->count
);
2443 if (n
!= parg
->count
) {
2448 case CHR_IOCTL_PP_EPP_WRITE
:
2449 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2450 struct ParallelIOArg
*parg
= arg
;
2451 int n
= write(fd
, parg
->buffer
, parg
->count
);
2452 if (n
!= parg
->count
) {
2463 static void pp_close(CharDriverState
*chr
)
2465 ParallelCharDriver
*drv
= chr
->opaque
;
2468 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
2469 ioctl(fd
, PPRELEASE
);
2474 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
2476 CharDriverState
*chr
;
2477 ParallelCharDriver
*drv
;
2480 TFR(fd
= open(filename
, O_RDWR
));
2484 if (ioctl(fd
, PPCLAIM
) < 0) {
2489 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
2495 drv
->mode
= IEEE1284_MODE_COMPAT
;
2497 chr
= qemu_mallocz(sizeof(CharDriverState
));
2503 chr
->chr_write
= null_chr_write
;
2504 chr
->chr_ioctl
= pp_ioctl
;
2505 chr
->chr_close
= pp_close
;
2508 qemu_chr_reset(chr
);
2512 #endif /* __linux__ */
2518 HANDLE hcom
, hrecv
, hsend
;
2519 OVERLAPPED orecv
, osend
;
2524 #define NSENDBUF 2048
2525 #define NRECVBUF 2048
2526 #define MAXCONNECT 1
2527 #define NTIMEOUT 5000
2529 static int win_chr_poll(void *opaque
);
2530 static int win_chr_pipe_poll(void *opaque
);
2532 static void win_chr_close(CharDriverState
*chr
)
2534 WinCharState
*s
= chr
->opaque
;
2537 CloseHandle(s
->hsend
);
2541 CloseHandle(s
->hrecv
);
2545 CloseHandle(s
->hcom
);
2549 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
2551 qemu_del_polling_cb(win_chr_poll
, chr
);
2554 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
2556 WinCharState
*s
= chr
->opaque
;
2558 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2563 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2565 fprintf(stderr
, "Failed CreateEvent\n");
2568 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2570 fprintf(stderr
, "Failed CreateEvent\n");
2574 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2575 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2576 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2577 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
2582 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2583 fprintf(stderr
, "Failed SetupComm\n");
2587 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2588 size
= sizeof(COMMCONFIG
);
2589 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2590 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2591 CommConfigDialog(filename
, NULL
, &comcfg
);
2593 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2594 fprintf(stderr
, "Failed SetCommState\n");
2598 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2599 fprintf(stderr
, "Failed SetCommMask\n");
2603 cto
.ReadIntervalTimeout
= MAXDWORD
;
2604 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2605 fprintf(stderr
, "Failed SetCommTimeouts\n");
2609 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2610 fprintf(stderr
, "Failed ClearCommError\n");
2613 qemu_add_polling_cb(win_chr_poll
, chr
);
2621 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2623 WinCharState
*s
= chr
->opaque
;
2624 DWORD len
, ret
, size
, err
;
2627 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2628 s
->osend
.hEvent
= s
->hsend
;
2631 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2633 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2635 err
= GetLastError();
2636 if (err
== ERROR_IO_PENDING
) {
2637 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2655 static int win_chr_read_poll(CharDriverState
*chr
)
2657 WinCharState
*s
= chr
->opaque
;
2659 s
->max_size
= qemu_chr_can_read(chr
);
2663 static void win_chr_readfile(CharDriverState
*chr
)
2665 WinCharState
*s
= chr
->opaque
;
2670 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2671 s
->orecv
.hEvent
= s
->hrecv
;
2672 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2674 err
= GetLastError();
2675 if (err
== ERROR_IO_PENDING
) {
2676 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2681 qemu_chr_read(chr
, buf
, size
);
2685 static void win_chr_read(CharDriverState
*chr
)
2687 WinCharState
*s
= chr
->opaque
;
2689 if (s
->len
> s
->max_size
)
2690 s
->len
= s
->max_size
;
2694 win_chr_readfile(chr
);
2697 static int win_chr_poll(void *opaque
)
2699 CharDriverState
*chr
= opaque
;
2700 WinCharState
*s
= chr
->opaque
;
2704 ClearCommError(s
->hcom
, &comerr
, &status
);
2705 if (status
.cbInQue
> 0) {
2706 s
->len
= status
.cbInQue
;
2707 win_chr_read_poll(chr
);
2714 static CharDriverState
*qemu_chr_open_win(const char *filename
)
2716 CharDriverState
*chr
;
2719 chr
= qemu_mallocz(sizeof(CharDriverState
));
2722 s
= qemu_mallocz(sizeof(WinCharState
));
2728 chr
->chr_write
= win_chr_write
;
2729 chr
->chr_close
= win_chr_close
;
2731 if (win_chr_init(chr
, filename
) < 0) {
2736 qemu_chr_reset(chr
);
2740 static int win_chr_pipe_poll(void *opaque
)
2742 CharDriverState
*chr
= opaque
;
2743 WinCharState
*s
= chr
->opaque
;
2746 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2749 win_chr_read_poll(chr
);
2756 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
2758 WinCharState
*s
= chr
->opaque
;
2766 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2768 fprintf(stderr
, "Failed CreateEvent\n");
2771 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2773 fprintf(stderr
, "Failed CreateEvent\n");
2777 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2778 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2779 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2781 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2782 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2783 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2788 ZeroMemory(&ov
, sizeof(ov
));
2789 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2790 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2792 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2796 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2798 fprintf(stderr
, "Failed GetOverlappedResult\n");
2800 CloseHandle(ov
.hEvent
);
2807 CloseHandle(ov
.hEvent
);
2810 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2819 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2821 CharDriverState
*chr
;
2824 chr
= qemu_mallocz(sizeof(CharDriverState
));
2827 s
= qemu_mallocz(sizeof(WinCharState
));
2833 chr
->chr_write
= win_chr_write
;
2834 chr
->chr_close
= win_chr_close
;
2836 if (win_chr_pipe_init(chr
, filename
) < 0) {
2841 qemu_chr_reset(chr
);
2845 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2847 CharDriverState
*chr
;
2850 chr
= qemu_mallocz(sizeof(CharDriverState
));
2853 s
= qemu_mallocz(sizeof(WinCharState
));
2860 chr
->chr_write
= win_chr_write
;
2861 qemu_chr_reset(chr
);
2865 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
2867 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2870 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2874 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2875 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2876 if (fd_out
== INVALID_HANDLE_VALUE
)
2879 return qemu_chr_open_win_file(fd_out
);
2881 #endif /* !_WIN32 */
2883 /***********************************************************/
2884 /* UDP Net console */
2888 struct sockaddr_in daddr
;
2895 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2897 NetCharDriver
*s
= chr
->opaque
;
2899 return sendto(s
->fd
, buf
, len
, 0,
2900 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2903 static int udp_chr_read_poll(void *opaque
)
2905 CharDriverState
*chr
= opaque
;
2906 NetCharDriver
*s
= chr
->opaque
;
2908 s
->max_size
= qemu_chr_can_read(chr
);
2910 /* If there were any stray characters in the queue process them
2913 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2914 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2916 s
->max_size
= qemu_chr_can_read(chr
);
2921 static void udp_chr_read(void *opaque
)
2923 CharDriverState
*chr
= opaque
;
2924 NetCharDriver
*s
= chr
->opaque
;
2926 if (s
->max_size
== 0)
2928 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2929 s
->bufptr
= s
->bufcnt
;
2934 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2935 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2937 s
->max_size
= qemu_chr_can_read(chr
);
2941 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2943 NetCharDriver
*s
= chr
->opaque
;
2946 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2947 udp_chr_read
, NULL
, chr
);
2951 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2953 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2955 int parse_host_src_port(struct sockaddr_in
*haddr
,
2956 struct sockaddr_in
*saddr
,
2959 static CharDriverState
*qemu_chr_open_udp(const char *def
)
2961 CharDriverState
*chr
= NULL
;
2962 NetCharDriver
*s
= NULL
;
2964 struct sockaddr_in saddr
;
2966 chr
= qemu_mallocz(sizeof(CharDriverState
));
2969 s
= qemu_mallocz(sizeof(NetCharDriver
));
2973 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2975 perror("socket(PF_INET, SOCK_DGRAM)");
2979 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2980 printf("Could not parse: %s\n", def
);
2984 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2994 chr
->chr_write
= udp_chr_write
;
2995 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
3008 /***********************************************************/
3009 /* TCP Net console */
3020 static void tcp_chr_accept(void *opaque
);
3022 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
3024 TCPCharDriver
*s
= chr
->opaque
;
3026 return send_all(s
->fd
, buf
, len
);
3028 /* XXX: indicate an error ? */
3033 static int tcp_chr_read_poll(void *opaque
)
3035 CharDriverState
*chr
= opaque
;
3036 TCPCharDriver
*s
= chr
->opaque
;
3039 s
->max_size
= qemu_chr_can_read(chr
);
3044 #define IAC_BREAK 243
3045 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
3047 uint8_t *buf
, int *size
)
3049 /* Handle any telnet client's basic IAC options to satisfy char by
3050 * char mode with no echo. All IAC options will be removed from
3051 * the buf and the do_telnetopt variable will be used to track the
3052 * state of the width of the IAC information.
3054 * IAC commands come in sets of 3 bytes with the exception of the
3055 * "IAC BREAK" command and the double IAC.
3061 for (i
= 0; i
< *size
; i
++) {
3062 if (s
->do_telnetopt
> 1) {
3063 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
3064 /* Double IAC means send an IAC */
3068 s
->do_telnetopt
= 1;
3070 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
3071 /* Handle IAC break commands by sending a serial break */
3072 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
3077 if (s
->do_telnetopt
>= 4) {
3078 s
->do_telnetopt
= 1;
3081 if ((unsigned char)buf
[i
] == IAC
) {
3082 s
->do_telnetopt
= 2;
3093 static void tcp_chr_read(void *opaque
)
3095 CharDriverState
*chr
= opaque
;
3096 TCPCharDriver
*s
= chr
->opaque
;
3100 if (!s
->connected
|| s
->max_size
<= 0)
3103 if (len
> s
->max_size
)
3105 size
= recv(s
->fd
, buf
, len
, 0);
3107 /* connection closed */
3109 if (s
->listen_fd
>= 0) {
3110 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
3112 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3115 } else if (size
> 0) {
3116 if (s
->do_telnetopt
)
3117 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
3119 qemu_chr_read(chr
, buf
, size
);
3123 static void tcp_chr_connect(void *opaque
)
3125 CharDriverState
*chr
= opaque
;
3126 TCPCharDriver
*s
= chr
->opaque
;
3129 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
3130 tcp_chr_read
, NULL
, chr
);
3131 qemu_chr_reset(chr
);
3134 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3135 static void tcp_chr_telnet_init(int fd
)
3138 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3139 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3140 send(fd
, (char *)buf
, 3, 0);
3141 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3142 send(fd
, (char *)buf
, 3, 0);
3143 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3144 send(fd
, (char *)buf
, 3, 0);
3145 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3146 send(fd
, (char *)buf
, 3, 0);
3149 static void socket_set_nodelay(int fd
)
3152 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
3155 static void tcp_chr_accept(void *opaque
)
3157 CharDriverState
*chr
= opaque
;
3158 TCPCharDriver
*s
= chr
->opaque
;
3159 struct sockaddr_in saddr
;
3161 struct sockaddr_un uaddr
;
3163 struct sockaddr
*addr
;
3170 len
= sizeof(uaddr
);
3171 addr
= (struct sockaddr
*)&uaddr
;
3175 len
= sizeof(saddr
);
3176 addr
= (struct sockaddr
*)&saddr
;
3178 fd
= accept(s
->listen_fd
, addr
, &len
);
3179 if (fd
< 0 && errno
!= EINTR
) {
3181 } else if (fd
>= 0) {
3182 if (s
->do_telnetopt
)
3183 tcp_chr_telnet_init(fd
);
3187 socket_set_nonblock(fd
);
3189 socket_set_nodelay(fd
);
3191 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
3192 tcp_chr_connect(chr
);
3195 static void tcp_chr_close(CharDriverState
*chr
)
3197 TCPCharDriver
*s
= chr
->opaque
;
3200 if (s
->listen_fd
>= 0)
3201 closesocket(s
->listen_fd
);
3205 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
3209 CharDriverState
*chr
= NULL
;
3210 TCPCharDriver
*s
= NULL
;
3211 int fd
= -1, ret
, err
, val
;
3213 int is_waitconnect
= 1;
3216 struct sockaddr_in saddr
;
3218 struct sockaddr_un uaddr
;
3220 struct sockaddr
*addr
;
3225 addr
= (struct sockaddr
*)&uaddr
;
3226 addrlen
= sizeof(uaddr
);
3227 if (parse_unix_path(&uaddr
, host_str
) < 0)
3232 addr
= (struct sockaddr
*)&saddr
;
3233 addrlen
= sizeof(saddr
);
3234 if (parse_host_port(&saddr
, host_str
) < 0)
3239 while((ptr
= strchr(ptr
,','))) {
3241 if (!strncmp(ptr
,"server",6)) {
3243 } else if (!strncmp(ptr
,"nowait",6)) {
3245 } else if (!strncmp(ptr
,"nodelay",6)) {
3248 printf("Unknown option: %s\n", ptr
);
3255 chr
= qemu_mallocz(sizeof(CharDriverState
));
3258 s
= qemu_mallocz(sizeof(TCPCharDriver
));
3264 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
3267 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3272 if (!is_waitconnect
)
3273 socket_set_nonblock(fd
);
3278 s
->is_unix
= is_unix
;
3279 s
->do_nodelay
= do_nodelay
&& !is_unix
;
3282 chr
->chr_write
= tcp_chr_write
;
3283 chr
->chr_close
= tcp_chr_close
;
3286 /* allow fast reuse */
3290 strncpy(path
, uaddr
.sun_path
, 108);
3297 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3300 ret
= bind(fd
, addr
, addrlen
);
3304 ret
= listen(fd
, 0);
3309 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
3311 s
->do_telnetopt
= 1;
3314 ret
= connect(fd
, addr
, addrlen
);
3316 err
= socket_error();
3317 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3318 } else if (err
== EINPROGRESS
) {
3321 } else if (err
== WSAEALREADY
) {
3333 socket_set_nodelay(fd
);
3335 tcp_chr_connect(chr
);
3337 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
3340 if (is_listen
&& is_waitconnect
) {
3341 printf("QEMU waiting for connection on: %s\n", host_str
);
3342 tcp_chr_accept(chr
);
3343 socket_set_nonblock(s
->listen_fd
);
3355 CharDriverState
*qemu_chr_open(const char *filename
)
3359 if (!strcmp(filename
, "vc")) {
3360 return text_console_init(&display_state
, 0);
3361 } else if (strstart(filename
, "vc:", &p
)) {
3362 return text_console_init(&display_state
, p
);
3363 } else if (!strcmp(filename
, "null")) {
3364 return qemu_chr_open_null();
3366 if (strstart(filename
, "tcp:", &p
)) {
3367 return qemu_chr_open_tcp(p
, 0, 0);
3369 if (strstart(filename
, "telnet:", &p
)) {
3370 return qemu_chr_open_tcp(p
, 1, 0);
3372 if (strstart(filename
, "udp:", &p
)) {
3373 return qemu_chr_open_udp(p
);
3375 if (strstart(filename
, "mon:", &p
)) {
3376 CharDriverState
*drv
= qemu_chr_open(p
);
3378 drv
= qemu_chr_open_mux(drv
);
3379 monitor_init(drv
, !nographic
);
3382 printf("Unable to open driver: %s\n", p
);
3386 if (strstart(filename
, "unix:", &p
)) {
3387 return qemu_chr_open_tcp(p
, 0, 1);
3388 } else if (strstart(filename
, "file:", &p
)) {
3389 return qemu_chr_open_file_out(p
);
3390 } else if (strstart(filename
, "pipe:", &p
)) {
3391 return qemu_chr_open_pipe(p
);
3392 } else if (!strcmp(filename
, "pty")) {
3393 return qemu_chr_open_pty();
3394 } else if (!strcmp(filename
, "stdio")) {
3395 return qemu_chr_open_stdio();
3397 #if defined(__linux__)
3398 if (strstart(filename
, "/dev/parport", NULL
)) {
3399 return qemu_chr_open_pp(filename
);
3402 #if defined(__linux__) || defined(__sun__)
3403 if (strstart(filename
, "/dev/", NULL
)) {
3404 return qemu_chr_open_tty(filename
);
3408 if (strstart(filename
, "COM", NULL
)) {
3409 return qemu_chr_open_win(filename
);
3411 if (strstart(filename
, "pipe:", &p
)) {
3412 return qemu_chr_open_win_pipe(p
);
3414 if (strstart(filename
, "con:", NULL
)) {
3415 return qemu_chr_open_win_con(filename
);
3417 if (strstart(filename
, "file:", &p
)) {
3418 return qemu_chr_open_win_file_out(p
);
3426 void qemu_chr_close(CharDriverState
*chr
)
3429 chr
->chr_close(chr
);
3432 /***********************************************************/
3433 /* network device redirectors */
3435 __attribute__ (( unused
))
3436 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
3440 for(i
=0;i
<size
;i
+=16) {
3444 fprintf(f
, "%08x ", i
);
3447 fprintf(f
, " %02x", buf
[i
+j
]);
3452 for(j
=0;j
<len
;j
++) {
3454 if (c
< ' ' || c
> '~')
3456 fprintf(f
, "%c", c
);
3462 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
3469 offset
= strtol(p
, &last_char
, 0);
3470 if (0 == errno
&& '\0' == *last_char
&&
3471 offset
>= 0 && offset
<= 0xFFFFFF) {
3472 macaddr
[3] = (offset
& 0xFF0000) >> 16;
3473 macaddr
[4] = (offset
& 0xFF00) >> 8;
3474 macaddr
[5] = offset
& 0xFF;
3477 for(i
= 0; i
< 6; i
++) {
3478 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3483 if (*p
!= ':' && *p
!= '-')
3494 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
3499 p1
= strchr(p
, sep
);
3505 if (len
> buf_size
- 1)
3507 memcpy(buf
, p
, len
);
3514 int parse_host_src_port(struct sockaddr_in
*haddr
,
3515 struct sockaddr_in
*saddr
,
3516 const char *input_str
)
3518 char *str
= strdup(input_str
);
3519 char *host_str
= str
;
3524 * Chop off any extra arguments at the end of the string which
3525 * would start with a comma, then fill in the src port information
3526 * if it was provided else use the "any address" and "any port".
3528 if ((ptr
= strchr(str
,',')))
3531 if ((src_str
= strchr(input_str
,'@'))) {
3536 if (parse_host_port(haddr
, host_str
) < 0)
3539 if (!src_str
|| *src_str
== '\0')
3542 if (parse_host_port(saddr
, src_str
) < 0)
3553 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
3561 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3563 saddr
->sin_family
= AF_INET
;
3564 if (buf
[0] == '\0') {
3565 saddr
->sin_addr
.s_addr
= 0;
3567 if (isdigit(buf
[0])) {
3568 if (!inet_aton(buf
, &saddr
->sin_addr
))
3571 if ((he
= gethostbyname(buf
)) == NULL
)
3573 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
3576 port
= strtol(p
, (char **)&r
, 0);
3579 saddr
->sin_port
= htons(port
);
3584 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
3589 len
= MIN(108, strlen(str
));
3590 p
= strchr(str
, ',');
3592 len
= MIN(len
, p
- str
);
3594 memset(uaddr
, 0, sizeof(*uaddr
));
3596 uaddr
->sun_family
= AF_UNIX
;
3597 memcpy(uaddr
->sun_path
, str
, len
);
3603 /* find or alloc a new VLAN */
3604 VLANState
*qemu_find_vlan(int id
)
3606 VLANState
**pvlan
, *vlan
;
3607 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3611 vlan
= qemu_mallocz(sizeof(VLANState
));
3616 pvlan
= &first_vlan
;
3617 while (*pvlan
!= NULL
)
3618 pvlan
= &(*pvlan
)->next
;
3623 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
3624 IOReadHandler
*fd_read
,
3625 IOCanRWHandler
*fd_can_read
,
3628 VLANClientState
*vc
, **pvc
;
3629 vc
= qemu_mallocz(sizeof(VLANClientState
));
3632 vc
->fd_read
= fd_read
;
3633 vc
->fd_can_read
= fd_can_read
;
3634 vc
->opaque
= opaque
;
3638 pvc
= &vlan
->first_client
;
3639 while (*pvc
!= NULL
)
3640 pvc
= &(*pvc
)->next
;
3645 int qemu_can_send_packet(VLANClientState
*vc1
)
3647 VLANState
*vlan
= vc1
->vlan
;
3648 VLANClientState
*vc
;
3650 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3652 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
3659 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
3661 VLANState
*vlan
= vc1
->vlan
;
3662 VLANClientState
*vc
;
3665 printf("vlan %d send:\n", vlan
->id
);
3666 hex_dump(stdout
, buf
, size
);
3668 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3670 vc
->fd_read(vc
->opaque
, buf
, size
);
3675 #if defined(CONFIG_SLIRP)
3677 /* slirp network adapter */
3679 static int slirp_inited
;
3680 static VLANClientState
*slirp_vc
;
3682 int slirp_can_output(void)
3684 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
3687 void slirp_output(const uint8_t *pkt
, int pkt_len
)
3690 printf("slirp output:\n");
3691 hex_dump(stdout
, pkt
, pkt_len
);
3695 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
3698 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
3701 printf("slirp input:\n");
3702 hex_dump(stdout
, buf
, size
);
3704 slirp_input(buf
, size
);
3707 static int net_slirp_init(VLANState
*vlan
)
3709 if (!slirp_inited
) {
3713 slirp_vc
= qemu_new_vlan_client(vlan
,
3714 slirp_receive
, NULL
, NULL
);
3715 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
3719 static void net_slirp_redir(const char *redir_str
)
3724 struct in_addr guest_addr
;
3725 int host_port
, guest_port
;
3727 if (!slirp_inited
) {
3733 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3735 if (!strcmp(buf
, "tcp")) {
3737 } else if (!strcmp(buf
, "udp")) {
3743 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3745 host_port
= strtol(buf
, &r
, 0);
3749 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3751 if (buf
[0] == '\0') {
3752 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
3754 if (!inet_aton(buf
, &guest_addr
))
3757 guest_port
= strtol(p
, &r
, 0);
3761 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3762 fprintf(stderr
, "qemu: could not set up redirection\n");
3767 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3775 static void smb_exit(void)
3779 char filename
[1024];
3781 /* erase all the files in the directory */
3782 d
= opendir(smb_dir
);
3787 if (strcmp(de
->d_name
, ".") != 0 &&
3788 strcmp(de
->d_name
, "..") != 0) {
3789 snprintf(filename
, sizeof(filename
), "%s/%s",
3790 smb_dir
, de
->d_name
);
3798 /* automatic user mode samba server configuration */
3799 static void net_slirp_smb(const char *exported_dir
)
3801 char smb_conf
[1024];
3802 char smb_cmdline
[1024];
3805 if (!slirp_inited
) {
3810 /* XXX: better tmp dir construction */
3811 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3812 if (mkdir(smb_dir
, 0700) < 0) {
3813 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3816 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3818 f
= fopen(smb_conf
, "w");
3820 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3827 "socket address=127.0.0.1\n"
3828 "pid directory=%s\n"
3829 "lock directory=%s\n"
3830 "log file=%s/log.smbd\n"
3831 "smb passwd file=%s/smbpasswd\n"
3832 "security = share\n"
3847 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3848 SMBD_COMMAND
, smb_conf
);
3850 slirp_add_exec(0, smb_cmdline
, 4, 139);
3853 #endif /* !defined(_WIN32) */
3854 void do_info_slirp(void)
3859 #endif /* CONFIG_SLIRP */
3861 #if !defined(_WIN32)
3863 typedef struct TAPState
{
3864 VLANClientState
*vc
;
3866 char down_script
[1024];
3869 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3871 TAPState
*s
= opaque
;
3874 ret
= write(s
->fd
, buf
, size
);
3875 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3882 static void tap_send(void *opaque
)
3884 TAPState
*s
= opaque
;
3891 sbuf
.maxlen
= sizeof(buf
);
3893 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
3895 size
= read(s
->fd
, buf
, sizeof(buf
));
3898 qemu_send_packet(s
->vc
, buf
, size
);
3904 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3908 s
= qemu_mallocz(sizeof(TAPState
));
3912 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3913 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3914 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3918 #if defined (_BSD) || defined (__FreeBSD_kernel__)
3919 static int tap_open(char *ifname
, int ifname_size
)
3925 TFR(fd
= open("/dev/tap", O_RDWR
));
3927 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3932 dev
= devname(s
.st_rdev
, S_IFCHR
);
3933 pstrcpy(ifname
, ifname_size
, dev
);
3935 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3938 #elif defined(__sun__)
3939 #define TUNNEWPPA (('T'<<16) | 0x0001)
3941 * Allocate TAP device, returns opened fd.
3942 * Stores dev name in the first arg(must be large enough).
3944 int tap_alloc(char *dev
)
3946 int tap_fd
, if_fd
, ppa
= -1;
3947 static int ip_fd
= 0;
3950 static int arp_fd
= 0;
3951 int ip_muxid
, arp_muxid
;
3952 struct strioctl strioc_if
, strioc_ppa
;
3953 int link_type
= I_PLINK
;;
3955 char actual_name
[32] = "";
3957 memset(&ifr
, 0x0, sizeof(ifr
));
3961 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
3965 /* Check if IP device was opened */
3969 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
3971 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
3975 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
3977 syslog(LOG_ERR
, "Can't open /dev/tap");
3981 /* Assign a new PPA and get its unit number. */
3982 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
3983 strioc_ppa
.ic_timout
= 0;
3984 strioc_ppa
.ic_len
= sizeof(ppa
);
3985 strioc_ppa
.ic_dp
= (char *)&ppa
;
3986 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
3987 syslog (LOG_ERR
, "Can't assign new interface");
3989 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
3991 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
3994 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
3995 syslog(LOG_ERR
, "Can't push IP module");
3999 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
4000 syslog(LOG_ERR
, "Can't get flags\n");
4002 snprintf (actual_name
, 32, "tap%d", ppa
);
4003 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
4006 /* Assign ppa according to the unit number returned by tun device */
4008 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
4009 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
4010 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
4011 syslog (LOG_ERR
, "Can't get flags\n");
4012 /* Push arp module to if_fd */
4013 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
4014 syslog (LOG_ERR
, "Can't push ARP module (2)");
4016 /* Push arp module to ip_fd */
4017 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
4018 syslog (LOG_ERR
, "I_POP failed\n");
4019 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
4020 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
4022 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
4024 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
4026 /* Set ifname to arp */
4027 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
4028 strioc_if
.ic_timout
= 0;
4029 strioc_if
.ic_len
= sizeof(ifr
);
4030 strioc_if
.ic_dp
= (char *)&ifr
;
4031 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
4032 syslog (LOG_ERR
, "Can't set ifname to arp\n");
4035 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
4036 syslog(LOG_ERR
, "Can't link TAP device to IP");
4040 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
4041 syslog (LOG_ERR
, "Can't link TAP device to ARP");
4045 memset(&ifr
, 0x0, sizeof(ifr
));
4046 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
4047 ifr
.lifr_ip_muxid
= ip_muxid
;
4048 ifr
.lifr_arp_muxid
= arp_muxid
;
4050 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
4052 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
4053 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
4054 syslog (LOG_ERR
, "Can't set multiplexor id");
4057 sprintf(dev
, "tap%d", ppa
);
4061 static int tap_open(char *ifname
, int ifname_size
)
4065 if( (fd
= tap_alloc(dev
)) < 0 ){
4066 fprintf(stderr
, "Cannot allocate TAP device\n");
4069 pstrcpy(ifname
, ifname_size
, dev
);
4070 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
4074 static int tap_open(char *ifname
, int ifname_size
)
4079 TFR(fd
= open("/dev/net/tun", O_RDWR
));
4081 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
4084 memset(&ifr
, 0, sizeof(ifr
));
4085 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
4086 if (ifname
[0] != '\0')
4087 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
4089 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
4090 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
4092 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
4096 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
4097 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
4102 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
4108 /* try to launch network script */
4112 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
4113 for (i
= 0; i
< open_max
; i
++)
4114 if (i
!= STDIN_FILENO
&&
4115 i
!= STDOUT_FILENO
&&
4116 i
!= STDERR_FILENO
&&
4121 *parg
++ = (char *)setup_script
;
4122 *parg
++ = (char *)ifname
;
4124 execv(setup_script
, args
);
4127 while (waitpid(pid
, &status
, 0) != pid
);
4128 if (!WIFEXITED(status
) ||
4129 WEXITSTATUS(status
) != 0) {
4130 fprintf(stderr
, "%s: could not launch network script\n",
4138 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
4139 const char *setup_script
, const char *down_script
)
4145 if (ifname1
!= NULL
)
4146 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
4149 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
4153 if (!setup_script
|| !strcmp(setup_script
, "no"))
4155 if (setup_script
[0] != '\0') {
4156 if (launch_script(setup_script
, ifname
, fd
))
4159 s
= net_tap_fd_init(vlan
, fd
);
4162 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4163 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
4164 if (down_script
&& strcmp(down_script
, "no"))
4165 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
4169 #endif /* !_WIN32 */
4171 /* network connection */
4172 typedef struct NetSocketState
{
4173 VLANClientState
*vc
;
4175 int state
; /* 0 = getting length, 1 = getting data */
4179 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
4182 typedef struct NetSocketListenState
{
4185 } NetSocketListenState
;
4187 /* XXX: we consider we can send the whole packet without blocking */
4188 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
4190 NetSocketState
*s
= opaque
;
4194 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
4195 send_all(s
->fd
, buf
, size
);
4198 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
4200 NetSocketState
*s
= opaque
;
4201 sendto(s
->fd
, buf
, size
, 0,
4202 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
4205 static void net_socket_send(void *opaque
)
4207 NetSocketState
*s
= opaque
;
4212 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
4214 err
= socket_error();
4215 if (err
!= EWOULDBLOCK
)
4217 } else if (size
== 0) {
4218 /* end of connection */
4220 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
4226 /* reassemble a packet from the network */
4232 memcpy(s
->buf
+ s
->index
, buf
, l
);
4236 if (s
->index
== 4) {
4238 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
4244 l
= s
->packet_len
- s
->index
;
4247 memcpy(s
->buf
+ s
->index
, buf
, l
);
4251 if (s
->index
>= s
->packet_len
) {
4252 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
4261 static void net_socket_send_dgram(void *opaque
)
4263 NetSocketState
*s
= opaque
;
4266 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
4270 /* end of connection */
4271 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
4274 qemu_send_packet(s
->vc
, s
->buf
, size
);
4277 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
4282 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
4283 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
4284 inet_ntoa(mcastaddr
->sin_addr
),
4285 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
4289 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
4291 perror("socket(PF_INET, SOCK_DGRAM)");
4296 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
4297 (const char *)&val
, sizeof(val
));
4299 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
4303 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
4309 /* Add host to multicast group */
4310 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
4311 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
4313 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
4314 (const char *)&imr
, sizeof(struct ip_mreq
));
4316 perror("setsockopt(IP_ADD_MEMBERSHIP)");
4320 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
4322 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
4323 (const char *)&val
, sizeof(val
));
4325 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
4329 socket_set_nonblock(fd
);
4337 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
4340 struct sockaddr_in saddr
;
4342 socklen_t saddr_len
;
4345 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
4346 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
4347 * by ONLY ONE process: we must "clone" this dgram socket --jjo
4351 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
4353 if (saddr
.sin_addr
.s_addr
==0) {
4354 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
4358 /* clone dgram socket */
4359 newfd
= net_socket_mcast_create(&saddr
);
4361 /* error already reported by net_socket_mcast_create() */
4365 /* clone newfd to fd, close newfd */
4370 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
4371 fd
, strerror(errno
));
4376 s
= qemu_mallocz(sizeof(NetSocketState
));
4381 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
4382 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
4384 /* mcast: save bound address as dst */
4385 if (is_connected
) s
->dgram_dst
=saddr
;
4387 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4388 "socket: fd=%d (%s mcast=%s:%d)",
4389 fd
, is_connected
? "cloned" : "",
4390 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4394 static void net_socket_connect(void *opaque
)
4396 NetSocketState
*s
= opaque
;
4397 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
4400 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
4404 s
= qemu_mallocz(sizeof(NetSocketState
));
4408 s
->vc
= qemu_new_vlan_client(vlan
,
4409 net_socket_receive
, NULL
, s
);
4410 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4411 "socket: fd=%d", fd
);
4413 net_socket_connect(s
);
4415 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
4420 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
4423 int so_type
=-1, optlen
=sizeof(so_type
);
4425 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
4426 (socklen_t
*)&optlen
)< 0) {
4427 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
4432 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
4434 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
4436 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
4437 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
4438 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
4443 static void net_socket_accept(void *opaque
)
4445 NetSocketListenState
*s
= opaque
;
4447 struct sockaddr_in saddr
;
4452 len
= sizeof(saddr
);
4453 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
4454 if (fd
< 0 && errno
!= EINTR
) {
4456 } else if (fd
>= 0) {
4460 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
4464 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
4465 "socket: connection from %s:%d",
4466 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4470 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
4472 NetSocketListenState
*s
;
4474 struct sockaddr_in saddr
;
4476 if (parse_host_port(&saddr
, host_str
) < 0)
4479 s
= qemu_mallocz(sizeof(NetSocketListenState
));
4483 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4488 socket_set_nonblock(fd
);
4490 /* allow fast reuse */
4492 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
4494 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4499 ret
= listen(fd
, 0);
4506 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
4510 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
4513 int fd
, connected
, ret
, err
;
4514 struct sockaddr_in saddr
;
4516 if (parse_host_port(&saddr
, host_str
) < 0)
4519 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4524 socket_set_nonblock(fd
);
4528 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4530 err
= socket_error();
4531 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
4532 } else if (err
== EINPROGRESS
) {
4535 } else if (err
== WSAEALREADY
) {
4548 s
= net_socket_fd_init(vlan
, fd
, connected
);
4551 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4552 "socket: connect to %s:%d",
4553 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4557 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
4561 struct sockaddr_in saddr
;
4563 if (parse_host_port(&saddr
, host_str
) < 0)
4567 fd
= net_socket_mcast_create(&saddr
);
4571 s
= net_socket_fd_init(vlan
, fd
, 0);
4575 s
->dgram_dst
= saddr
;
4577 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4578 "socket: mcast=%s:%d",
4579 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4584 static const char *get_word(char *buf
, int buf_size
, const char *p
)
4591 while (*p
!= '\0') {
4596 } else if (*p
== '\"') {
4597 substring
= !substring
;
4600 } else if (!substring
&& (*p
== ',' || *p
== '='))
4602 if (q
&& (q
- buf
) < buf_size
- 1)
4612 static int get_param_value(char *buf
, int buf_size
,
4613 const char *tag
, const char *str
)
4620 p
= get_word(option
, sizeof(option
), p
);
4624 if (!strcmp(tag
, option
)) {
4625 (void)get_word(buf
, buf_size
, p
);
4628 p
= get_word(NULL
, 0, p
);
4637 static int check_params(char *buf
, int buf_size
,
4638 char **params
, const char *str
)
4645 p
= get_word(buf
, buf_size
, p
);
4649 for(i
= 0; params
[i
] != NULL
; i
++)
4650 if (!strcmp(params
[i
], buf
))
4652 if (params
[i
] == NULL
)
4654 p
= get_word(NULL
, 0, p
);
4663 static int net_client_init(const char *str
)
4674 while (*p
!= '\0' && *p
!= ',') {
4675 if ((q
- device
) < sizeof(device
) - 1)
4683 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
4684 vlan_id
= strtol(buf
, NULL
, 0);
4686 vlan
= qemu_find_vlan(vlan_id
);
4688 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
4691 if (!strcmp(device
, "nic")) {
4695 if (nb_nics
>= MAX_NICS
) {
4696 fprintf(stderr
, "Too Many NICs\n");
4699 nd
= &nd_table
[nb_nics
];
4700 macaddr
= nd
->macaddr
;
4706 macaddr
[5] = 0x56 + nb_nics
;
4708 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
4709 if (parse_macaddr(macaddr
, buf
) < 0) {
4710 fprintf(stderr
, "invalid syntax for ethernet address\n");
4714 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
4715 nd
->model
= strdup(buf
);
4719 vlan
->nb_guest_devs
++;
4722 if (!strcmp(device
, "none")) {
4723 /* does nothing. It is needed to signal that no network cards
4728 if (!strcmp(device
, "user")) {
4729 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
4730 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
4732 vlan
->nb_host_devs
++;
4733 ret
= net_slirp_init(vlan
);
4737 if (!strcmp(device
, "tap")) {
4739 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4740 fprintf(stderr
, "tap: no interface name\n");
4743 vlan
->nb_host_devs
++;
4744 ret
= tap_win32_init(vlan
, ifname
);
4747 if (!strcmp(device
, "tap")) {
4749 char setup_script
[1024], down_script
[1024];
4751 vlan
->nb_host_devs
++;
4752 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4753 fd
= strtol(buf
, NULL
, 0);
4755 if (net_tap_fd_init(vlan
, fd
))
4758 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4761 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
4762 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
4764 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
4765 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
4767 ret
= net_tap_init(vlan
, ifname
, setup_script
, down_script
);
4771 if (!strcmp(device
, "socket")) {
4772 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4774 fd
= strtol(buf
, NULL
, 0);
4776 if (net_socket_fd_init(vlan
, fd
, 1))
4778 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
4779 ret
= net_socket_listen_init(vlan
, buf
);
4780 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
4781 ret
= net_socket_connect_init(vlan
, buf
);
4782 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
4783 ret
= net_socket_mcast_init(vlan
, buf
);
4785 fprintf(stderr
, "Unknown socket options: %s\n", p
);
4788 vlan
->nb_host_devs
++;
4791 fprintf(stderr
, "Unknown network device: %s\n", device
);
4795 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
4801 void do_info_network(void)
4804 VLANClientState
*vc
;
4806 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
4807 term_printf("VLAN %d devices:\n", vlan
->id
);
4808 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
4809 term_printf(" %s\n", vc
->info_str
);
4813 #define HD_ALIAS "file=\"%s\",index=%d,media=disk"
4815 #define CDROM_ALIAS "index=1,media=cdrom"
4817 #define CDROM_ALIAS "index=2,media=cdrom"
4819 #define FD_ALIAS "index=%d,if=floppy"
4820 #define PFLASH_ALIAS "file=\"%s\",if=pflash"
4821 #define MTD_ALIAS "file=\"%s\",if=mtd"
4822 #define SD_ALIAS "index=0,if=sd"
4824 static int drive_add(const char *fmt
, ...)
4828 if (nb_drives_opt
>= MAX_DRIVES
) {
4829 fprintf(stderr
, "qemu: too many drives\n");
4834 vsnprintf(drives_opt
[nb_drives_opt
], sizeof(drives_opt
[0]), fmt
, ap
);
4837 return nb_drives_opt
++;
4840 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
4844 /* seek interface, bus and unit */
4846 for (index
= 0; index
< nb_drives
; index
++)
4847 if (drives_table
[index
].type
== type
&&
4848 drives_table
[index
].bus
== bus
&&
4849 drives_table
[index
].unit
== unit
)
4855 int drive_get_max_bus(BlockInterfaceType type
)
4861 for (index
= 0; index
< nb_drives
; index
++) {
4862 if(drives_table
[index
].type
== type
&&
4863 drives_table
[index
].bus
> max_bus
)
4864 max_bus
= drives_table
[index
].bus
;
4869 static int drive_init(const char *str
, int snapshot
, QEMUMachine
*machine
)
4874 const char *mediastr
= "";
4875 BlockInterfaceType type
;
4876 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
4877 int bus_id
, unit_id
;
4878 int cyls
, heads
, secs
, translation
;
4879 BlockDriverState
*bdrv
;
4884 char *params
[] = { "bus", "unit", "if", "index", "cyls", "heads",
4885 "secs", "trans", "media", "snapshot", "file",
4888 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
4889 fprintf(stderr
, "qemu: unknowm parameter '%s' in '%s'\n",
4895 cyls
= heads
= secs
= 0;
4898 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4902 if (!strcmp(machine
->name
, "realview") ||
4903 !strcmp(machine
->name
, "SS-5") ||
4904 !strcmp(machine
->name
, "SS-10") ||
4905 !strcmp(machine
->name
, "SS-600MP") ||
4906 !strcmp(machine
->name
, "versatilepb") ||
4907 !strcmp(machine
->name
, "versatileab")) {
4909 max_devs
= MAX_SCSI_DEVS
;
4910 strcpy(devname
, "scsi");
4913 max_devs
= MAX_IDE_DEVS
;
4914 strcpy(devname
, "ide");
4918 /* extract parameters */
4920 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
4921 bus_id
= strtol(buf
, NULL
, 0);
4923 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
4928 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
4929 unit_id
= strtol(buf
, NULL
, 0);
4931 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
4936 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
4937 strncpy(devname
, buf
, sizeof(devname
));
4938 if (!strcmp(buf
, "ide")) {
4940 max_devs
= MAX_IDE_DEVS
;
4941 } else if (!strcmp(buf
, "scsi")) {
4943 max_devs
= MAX_SCSI_DEVS
;
4944 } else if (!strcmp(buf
, "floppy")) {
4947 } else if (!strcmp(buf
, "pflash")) {
4950 } else if (!strcmp(buf
, "mtd")) {
4953 } else if (!strcmp(buf
, "sd")) {
4957 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
4962 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
4963 index
= strtol(buf
, NULL
, 0);
4965 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
4970 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
4971 cyls
= strtol(buf
, NULL
, 0);
4974 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
4975 heads
= strtol(buf
, NULL
, 0);
4978 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
4979 secs
= strtol(buf
, NULL
, 0);
4982 if (cyls
|| heads
|| secs
) {
4983 if (cyls
< 1 || cyls
> 16383) {
4984 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
4987 if (heads
< 1 || heads
> 16) {
4988 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
4991 if (secs
< 1 || secs
> 63) {
4992 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
4997 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
5000 "qemu: '%s' trans must be used with cyls,heads and secs\n",
5004 if (!strcmp(buf
, "none"))
5005 translation
= BIOS_ATA_TRANSLATION_NONE
;
5006 else if (!strcmp(buf
, "lba"))
5007 translation
= BIOS_ATA_TRANSLATION_LBA
;
5008 else if (!strcmp(buf
, "auto"))
5009 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5011 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
5016 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
5017 if (!strcmp(buf
, "disk")) {
5019 } else if (!strcmp(buf
, "cdrom")) {
5020 if (cyls
|| secs
|| heads
) {
5022 "qemu: '%s' invalid physical CHS format\n", str
);
5025 media
= MEDIA_CDROM
;
5027 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
5032 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
5033 if (!strcmp(buf
, "on"))
5035 else if (!strcmp(buf
, "off"))
5038 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
5043 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
5044 if (!strcmp(buf
, "off"))
5046 else if (!strcmp(buf
, "on"))
5049 fprintf(stderr
, "qemu: invalid cache option\n");
5054 get_param_value(file
, sizeof(file
), "file", str
);
5056 /* compute bus and unit according index */
5059 if (bus_id
!= 0 || unit_id
!= -1) {
5061 "qemu: '%s' index cannot be used with bus and unit\n", str
);
5069 unit_id
= index
% max_devs
;
5070 bus_id
= index
/ max_devs
;
5074 /* if user doesn't specify a unit_id,
5075 * try to find the first free
5078 if (unit_id
== -1) {
5080 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
5082 if (max_devs
&& unit_id
>= max_devs
) {
5083 unit_id
-= max_devs
;
5091 if (max_devs
&& unit_id
>= max_devs
) {
5092 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
5093 str
, unit_id
, max_devs
- 1);
5098 * ignore multiple definitions
5101 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
5106 if (type
== IF_IDE
|| type
== IF_SCSI
)
5107 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
5109 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
5110 devname
, bus_id
, mediastr
, unit_id
);
5112 snprintf(buf
, sizeof(buf
), "%s%s%i",
5113 devname
, mediastr
, unit_id
);
5114 bdrv
= bdrv_new(buf
);
5115 drives_table
[nb_drives
].bdrv
= bdrv
;
5116 drives_table
[nb_drives
].type
= type
;
5117 drives_table
[nb_drives
].bus
= bus_id
;
5118 drives_table
[nb_drives
].unit
= unit_id
;
5127 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
5128 bdrv_set_translation_hint(bdrv
, translation
);
5132 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
5137 /* FIXME: This isn't really a floppy, but it's a reasonable
5140 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
5150 bdrv_flags
|= BDRV_O_SNAPSHOT
;
5152 bdrv_flags
|= BDRV_O_DIRECT
;
5153 if (bdrv_open(bdrv
, file
, bdrv_flags
) < 0 || qemu_key_check(bdrv
, file
)) {
5154 fprintf(stderr
, "qemu: could not open disk image %s\n",
5161 /***********************************************************/
5164 static USBPort
*used_usb_ports
;
5165 static USBPort
*free_usb_ports
;
5167 /* ??? Maybe change this to register a hub to keep track of the topology. */
5168 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
5169 usb_attachfn attach
)
5171 port
->opaque
= opaque
;
5172 port
->index
= index
;
5173 port
->attach
= attach
;
5174 port
->next
= free_usb_ports
;
5175 free_usb_ports
= port
;
5178 static int usb_device_add(const char *devname
)
5184 if (!free_usb_ports
)
5187 if (strstart(devname
, "host:", &p
)) {
5188 dev
= usb_host_device_open(p
);
5189 } else if (!strcmp(devname
, "mouse")) {
5190 dev
= usb_mouse_init();
5191 } else if (!strcmp(devname
, "tablet")) {
5192 dev
= usb_tablet_init();
5193 } else if (!strcmp(devname
, "keyboard")) {
5194 dev
= usb_keyboard_init();
5195 } else if (strstart(devname
, "disk:", &p
)) {
5196 dev
= usb_msd_init(p
);
5197 } else if (!strcmp(devname
, "wacom-tablet")) {
5198 dev
= usb_wacom_init();
5205 /* Find a USB port to add the device to. */
5206 port
= free_usb_ports
;
5210 /* Create a new hub and chain it on. */
5211 free_usb_ports
= NULL
;
5212 port
->next
= used_usb_ports
;
5213 used_usb_ports
= port
;
5215 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
5216 usb_attach(port
, hub
);
5217 port
= free_usb_ports
;
5220 free_usb_ports
= port
->next
;
5221 port
->next
= used_usb_ports
;
5222 used_usb_ports
= port
;
5223 usb_attach(port
, dev
);
5227 static int usb_device_del(const char *devname
)
5235 if (!used_usb_ports
)
5238 p
= strchr(devname
, '.');
5241 bus_num
= strtoul(devname
, NULL
, 0);
5242 addr
= strtoul(p
+ 1, NULL
, 0);
5246 lastp
= &used_usb_ports
;
5247 port
= used_usb_ports
;
5248 while (port
&& port
->dev
->addr
!= addr
) {
5249 lastp
= &port
->next
;
5257 *lastp
= port
->next
;
5258 usb_attach(port
, NULL
);
5259 dev
->handle_destroy(dev
);
5260 port
->next
= free_usb_ports
;
5261 free_usb_ports
= port
;
5265 void do_usb_add(const char *devname
)
5268 ret
= usb_device_add(devname
);
5270 term_printf("Could not add USB device '%s'\n", devname
);
5273 void do_usb_del(const char *devname
)
5276 ret
= usb_device_del(devname
);
5278 term_printf("Could not remove USB device '%s'\n", devname
);
5285 const char *speed_str
;
5288 term_printf("USB support not enabled\n");
5292 for (port
= used_usb_ports
; port
; port
= port
->next
) {
5296 switch(dev
->speed
) {
5300 case USB_SPEED_FULL
:
5303 case USB_SPEED_HIGH
:
5310 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
5311 0, dev
->addr
, speed_str
, dev
->devname
);
5315 /***********************************************************/
5316 /* PCMCIA/Cardbus */
5318 static struct pcmcia_socket_entry_s
{
5319 struct pcmcia_socket_s
*socket
;
5320 struct pcmcia_socket_entry_s
*next
;
5321 } *pcmcia_sockets
= 0;
5323 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
5325 struct pcmcia_socket_entry_s
*entry
;
5327 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
5328 entry
->socket
= socket
;
5329 entry
->next
= pcmcia_sockets
;
5330 pcmcia_sockets
= entry
;
5333 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
5335 struct pcmcia_socket_entry_s
*entry
, **ptr
;
5337 ptr
= &pcmcia_sockets
;
5338 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
5339 if (entry
->socket
== socket
) {
5345 void pcmcia_info(void)
5347 struct pcmcia_socket_entry_s
*iter
;
5348 if (!pcmcia_sockets
)
5349 term_printf("No PCMCIA sockets\n");
5351 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
5352 term_printf("%s: %s\n", iter
->socket
->slot_string
,
5353 iter
->socket
->attached
? iter
->socket
->card_string
:
5357 /***********************************************************/
5360 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
5364 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
5368 static void dumb_refresh(DisplayState
*ds
)
5370 #if defined(CONFIG_SDL)
5375 static void dumb_display_init(DisplayState
*ds
)
5380 ds
->dpy_update
= dumb_update
;
5381 ds
->dpy_resize
= dumb_resize
;
5382 ds
->dpy_refresh
= dumb_refresh
;
5385 /***********************************************************/
5388 #define MAX_IO_HANDLERS 64
5390 typedef struct IOHandlerRecord
{
5392 IOCanRWHandler
*fd_read_poll
;
5394 IOHandler
*fd_write
;
5397 /* temporary data */
5399 struct IOHandlerRecord
*next
;
5402 static IOHandlerRecord
*first_io_handler
;
5404 /* XXX: fd_read_poll should be suppressed, but an API change is
5405 necessary in the character devices to suppress fd_can_read(). */
5406 int qemu_set_fd_handler2(int fd
,
5407 IOCanRWHandler
*fd_read_poll
,
5409 IOHandler
*fd_write
,
5412 IOHandlerRecord
**pioh
, *ioh
;
5414 if (!fd_read
&& !fd_write
) {
5415 pioh
= &first_io_handler
;
5420 if (ioh
->fd
== fd
) {
5427 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
5431 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
5434 ioh
->next
= first_io_handler
;
5435 first_io_handler
= ioh
;
5438 ioh
->fd_read_poll
= fd_read_poll
;
5439 ioh
->fd_read
= fd_read
;
5440 ioh
->fd_write
= fd_write
;
5441 ioh
->opaque
= opaque
;
5447 int qemu_set_fd_handler(int fd
,
5449 IOHandler
*fd_write
,
5452 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
5455 /***********************************************************/
5456 /* Polling handling */
5458 typedef struct PollingEntry
{
5461 struct PollingEntry
*next
;
5464 static PollingEntry
*first_polling_entry
;
5466 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
5468 PollingEntry
**ppe
, *pe
;
5469 pe
= qemu_mallocz(sizeof(PollingEntry
));
5473 pe
->opaque
= opaque
;
5474 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
5479 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
5481 PollingEntry
**ppe
, *pe
;
5482 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
5484 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
5493 /***********************************************************/
5494 /* Wait objects support */
5495 typedef struct WaitObjects
{
5497 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
5498 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
5499 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
5502 static WaitObjects wait_objects
= {0};
5504 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
5506 WaitObjects
*w
= &wait_objects
;
5508 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
5510 w
->events
[w
->num
] = handle
;
5511 w
->func
[w
->num
] = func
;
5512 w
->opaque
[w
->num
] = opaque
;
5517 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
5520 WaitObjects
*w
= &wait_objects
;
5523 for (i
= 0; i
< w
->num
; i
++) {
5524 if (w
->events
[i
] == handle
)
5527 w
->events
[i
] = w
->events
[i
+ 1];
5528 w
->func
[i
] = w
->func
[i
+ 1];
5529 w
->opaque
[i
] = w
->opaque
[i
+ 1];
5537 /***********************************************************/
5538 /* savevm/loadvm support */
5540 #define IO_BUF_SIZE 32768
5544 BlockDriverState
*bs
;
5547 int64_t base_offset
;
5548 int64_t buf_offset
; /* start of buffer when writing, end of buffer
5551 int buf_size
; /* 0 when writing */
5552 uint8_t buf
[IO_BUF_SIZE
];
5555 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
5559 f
= qemu_mallocz(sizeof(QEMUFile
));
5562 if (!strcmp(mode
, "wb")) {
5564 } else if (!strcmp(mode
, "rb")) {
5569 f
->outfile
= fopen(filename
, mode
);
5581 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
5585 f
= qemu_mallocz(sizeof(QEMUFile
));
5590 f
->is_writable
= is_writable
;
5591 f
->base_offset
= offset
;
5595 void qemu_fflush(QEMUFile
*f
)
5597 if (!f
->is_writable
)
5599 if (f
->buf_index
> 0) {
5601 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
5602 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
5604 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
5605 f
->buf
, f
->buf_index
);
5607 f
->buf_offset
+= f
->buf_index
;
5612 static void qemu_fill_buffer(QEMUFile
*f
)
5619 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
5620 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
5624 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
5625 f
->buf
, IO_BUF_SIZE
);
5631 f
->buf_offset
+= len
;
5634 void qemu_fclose(QEMUFile
*f
)
5644 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
5648 l
= IO_BUF_SIZE
- f
->buf_index
;
5651 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
5655 if (f
->buf_index
>= IO_BUF_SIZE
)
5660 void qemu_put_byte(QEMUFile
*f
, int v
)
5662 f
->buf
[f
->buf_index
++] = v
;
5663 if (f
->buf_index
>= IO_BUF_SIZE
)
5667 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
5673 l
= f
->buf_size
- f
->buf_index
;
5675 qemu_fill_buffer(f
);
5676 l
= f
->buf_size
- f
->buf_index
;
5682 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
5687 return size1
- size
;
5690 int qemu_get_byte(QEMUFile
*f
)
5692 if (f
->buf_index
>= f
->buf_size
) {
5693 qemu_fill_buffer(f
);
5694 if (f
->buf_index
>= f
->buf_size
)
5697 return f
->buf
[f
->buf_index
++];
5700 int64_t qemu_ftell(QEMUFile
*f
)
5702 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
5705 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
5707 if (whence
== SEEK_SET
) {
5709 } else if (whence
== SEEK_CUR
) {
5710 pos
+= qemu_ftell(f
);
5712 /* SEEK_END not supported */
5715 if (f
->is_writable
) {
5717 f
->buf_offset
= pos
;
5719 f
->buf_offset
= pos
;
5726 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
5728 qemu_put_byte(f
, v
>> 8);
5729 qemu_put_byte(f
, v
);
5732 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
5734 qemu_put_byte(f
, v
>> 24);
5735 qemu_put_byte(f
, v
>> 16);
5736 qemu_put_byte(f
, v
>> 8);
5737 qemu_put_byte(f
, v
);
5740 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
5742 qemu_put_be32(f
, v
>> 32);
5743 qemu_put_be32(f
, v
);
5746 unsigned int qemu_get_be16(QEMUFile
*f
)
5749 v
= qemu_get_byte(f
) << 8;
5750 v
|= qemu_get_byte(f
);
5754 unsigned int qemu_get_be32(QEMUFile
*f
)
5757 v
= qemu_get_byte(f
) << 24;
5758 v
|= qemu_get_byte(f
) << 16;
5759 v
|= qemu_get_byte(f
) << 8;
5760 v
|= qemu_get_byte(f
);
5764 uint64_t qemu_get_be64(QEMUFile
*f
)
5767 v
= (uint64_t)qemu_get_be32(f
) << 32;
5768 v
|= qemu_get_be32(f
);
5772 typedef struct SaveStateEntry
{
5776 SaveStateHandler
*save_state
;
5777 LoadStateHandler
*load_state
;
5779 struct SaveStateEntry
*next
;
5782 static SaveStateEntry
*first_se
;
5784 int register_savevm(const char *idstr
,
5787 SaveStateHandler
*save_state
,
5788 LoadStateHandler
*load_state
,
5791 SaveStateEntry
*se
, **pse
;
5793 se
= qemu_malloc(sizeof(SaveStateEntry
));
5796 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
5797 se
->instance_id
= instance_id
;
5798 se
->version_id
= version_id
;
5799 se
->save_state
= save_state
;
5800 se
->load_state
= load_state
;
5801 se
->opaque
= opaque
;
5804 /* add at the end of list */
5806 while (*pse
!= NULL
)
5807 pse
= &(*pse
)->next
;
5812 #define QEMU_VM_FILE_MAGIC 0x5145564d
5813 #define QEMU_VM_FILE_VERSION 0x00000002
5815 static int qemu_savevm_state(QEMUFile
*f
)
5819 int64_t cur_pos
, len_pos
, total_len_pos
;
5821 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
5822 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
5823 total_len_pos
= qemu_ftell(f
);
5824 qemu_put_be64(f
, 0); /* total size */
5826 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5828 len
= strlen(se
->idstr
);
5829 qemu_put_byte(f
, len
);
5830 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
5832 qemu_put_be32(f
, se
->instance_id
);
5833 qemu_put_be32(f
, se
->version_id
);
5835 /* record size: filled later */
5836 len_pos
= qemu_ftell(f
);
5837 qemu_put_be32(f
, 0);
5838 se
->save_state(f
, se
->opaque
);
5840 /* fill record size */
5841 cur_pos
= qemu_ftell(f
);
5842 len
= cur_pos
- len_pos
- 4;
5843 qemu_fseek(f
, len_pos
, SEEK_SET
);
5844 qemu_put_be32(f
, len
);
5845 qemu_fseek(f
, cur_pos
, SEEK_SET
);
5847 cur_pos
= qemu_ftell(f
);
5848 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
5849 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
5850 qemu_fseek(f
, cur_pos
, SEEK_SET
);
5856 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
5860 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5861 if (!strcmp(se
->idstr
, idstr
) &&
5862 instance_id
== se
->instance_id
)
5868 static int qemu_loadvm_state(QEMUFile
*f
)
5871 int len
, ret
, instance_id
, record_len
, version_id
;
5872 int64_t total_len
, end_pos
, cur_pos
;
5876 v
= qemu_get_be32(f
);
5877 if (v
!= QEMU_VM_FILE_MAGIC
)
5879 v
= qemu_get_be32(f
);
5880 if (v
!= QEMU_VM_FILE_VERSION
) {
5885 total_len
= qemu_get_be64(f
);
5886 end_pos
= total_len
+ qemu_ftell(f
);
5888 if (qemu_ftell(f
) >= end_pos
)
5890 len
= qemu_get_byte(f
);
5891 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
5893 instance_id
= qemu_get_be32(f
);
5894 version_id
= qemu_get_be32(f
);
5895 record_len
= qemu_get_be32(f
);
5897 printf("idstr=%s instance=0x%x version=%d len=%d\n",
5898 idstr
, instance_id
, version_id
, record_len
);
5900 cur_pos
= qemu_ftell(f
);
5901 se
= find_se(idstr
, instance_id
);
5903 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
5904 instance_id
, idstr
);
5906 ret
= se
->load_state(f
, se
->opaque
, version_id
);
5908 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
5909 instance_id
, idstr
);
5912 /* always seek to exact end of record */
5913 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
5920 /* device can contain snapshots */
5921 static int bdrv_can_snapshot(BlockDriverState
*bs
)
5924 !bdrv_is_removable(bs
) &&
5925 !bdrv_is_read_only(bs
));
5928 /* device must be snapshots in order to have a reliable snapshot */
5929 static int bdrv_has_snapshot(BlockDriverState
*bs
)
5932 !bdrv_is_removable(bs
) &&
5933 !bdrv_is_read_only(bs
));
5936 static BlockDriverState
*get_bs_snapshots(void)
5938 BlockDriverState
*bs
;
5942 return bs_snapshots
;
5943 for(i
= 0; i
<= nb_drives
; i
++) {
5944 bs
= drives_table
[i
].bdrv
;
5945 if (bdrv_can_snapshot(bs
))
5954 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
5957 QEMUSnapshotInfo
*sn_tab
, *sn
;
5961 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5964 for(i
= 0; i
< nb_sns
; i
++) {
5966 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
5976 void do_savevm(const char *name
)
5978 BlockDriverState
*bs
, *bs1
;
5979 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
5980 int must_delete
, ret
, i
;
5981 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5983 int saved_vm_running
;
5990 bs
= get_bs_snapshots();
5992 term_printf("No block device can accept snapshots\n");
5996 /* ??? Should this occur after vm_stop? */
5999 saved_vm_running
= vm_running
;
6004 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
6009 memset(sn
, 0, sizeof(*sn
));
6011 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
6012 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
6015 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
6018 /* fill auxiliary fields */
6021 sn
->date_sec
= tb
.time
;
6022 sn
->date_nsec
= tb
.millitm
* 1000000;
6024 gettimeofday(&tv
, NULL
);
6025 sn
->date_sec
= tv
.tv_sec
;
6026 sn
->date_nsec
= tv
.tv_usec
* 1000;
6028 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
6030 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
6031 term_printf("Device %s does not support VM state snapshots\n",
6032 bdrv_get_device_name(bs
));
6036 /* save the VM state */
6037 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
6039 term_printf("Could not open VM state file\n");
6042 ret
= qemu_savevm_state(f
);
6043 sn
->vm_state_size
= qemu_ftell(f
);
6046 term_printf("Error %d while writing VM\n", ret
);
6050 /* create the snapshots */
6052 for(i
= 0; i
< nb_drives
; i
++) {
6053 bs1
= drives_table
[i
].bdrv
;
6054 if (bdrv_has_snapshot(bs1
)) {
6056 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
6058 term_printf("Error while deleting snapshot on '%s'\n",
6059 bdrv_get_device_name(bs1
));
6062 ret
= bdrv_snapshot_create(bs1
, sn
);
6064 term_printf("Error while creating snapshot on '%s'\n",
6065 bdrv_get_device_name(bs1
));
6071 if (saved_vm_running
)
6075 void do_loadvm(const char *name
)
6077 BlockDriverState
*bs
, *bs1
;
6078 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
6081 int saved_vm_running
;
6083 bs
= get_bs_snapshots();
6085 term_printf("No block device supports snapshots\n");
6089 /* Flush all IO requests so they don't interfere with the new state. */
6092 saved_vm_running
= vm_running
;
6095 for(i
= 0; i
<= nb_drives
; i
++) {
6096 bs1
= drives_table
[i
].bdrv
;
6097 if (bdrv_has_snapshot(bs1
)) {
6098 ret
= bdrv_snapshot_goto(bs1
, name
);
6101 term_printf("Warning: ");
6104 term_printf("Snapshots not supported on device '%s'\n",
6105 bdrv_get_device_name(bs1
));
6108 term_printf("Could not find snapshot '%s' on device '%s'\n",
6109 name
, bdrv_get_device_name(bs1
));
6112 term_printf("Error %d while activating snapshot on '%s'\n",
6113 ret
, bdrv_get_device_name(bs1
));
6116 /* fatal on snapshot block device */
6123 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
6124 term_printf("Device %s does not support VM state snapshots\n",
6125 bdrv_get_device_name(bs
));
6129 /* restore the VM state */
6130 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
6132 term_printf("Could not open VM state file\n");
6135 ret
= qemu_loadvm_state(f
);
6138 term_printf("Error %d while loading VM state\n", ret
);
6141 if (saved_vm_running
)
6145 void do_delvm(const char *name
)
6147 BlockDriverState
*bs
, *bs1
;
6150 bs
= get_bs_snapshots();
6152 term_printf("No block device supports snapshots\n");
6156 for(i
= 0; i
<= nb_drives
; i
++) {
6157 bs1
= drives_table
[i
].bdrv
;
6158 if (bdrv_has_snapshot(bs1
)) {
6159 ret
= bdrv_snapshot_delete(bs1
, name
);
6161 if (ret
== -ENOTSUP
)
6162 term_printf("Snapshots not supported on device '%s'\n",
6163 bdrv_get_device_name(bs1
));
6165 term_printf("Error %d while deleting snapshot on '%s'\n",
6166 ret
, bdrv_get_device_name(bs1
));
6172 void do_info_snapshots(void)
6174 BlockDriverState
*bs
, *bs1
;
6175 QEMUSnapshotInfo
*sn_tab
, *sn
;
6179 bs
= get_bs_snapshots();
6181 term_printf("No available block device supports snapshots\n");
6184 term_printf("Snapshot devices:");
6185 for(i
= 0; i
<= nb_drives
; i
++) {
6186 bs1
= drives_table
[i
].bdrv
;
6187 if (bdrv_has_snapshot(bs1
)) {
6189 term_printf(" %s", bdrv_get_device_name(bs1
));
6194 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
6196 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
6199 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
6200 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
6201 for(i
= 0; i
< nb_sns
; i
++) {
6203 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
6208 /***********************************************************/
6209 /* cpu save/restore */
6211 #if defined(TARGET_I386)
6213 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
6215 qemu_put_be32(f
, dt
->selector
);
6216 qemu_put_betl(f
, dt
->base
);
6217 qemu_put_be32(f
, dt
->limit
);
6218 qemu_put_be32(f
, dt
->flags
);
6221 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
6223 dt
->selector
= qemu_get_be32(f
);
6224 dt
->base
= qemu_get_betl(f
);
6225 dt
->limit
= qemu_get_be32(f
);
6226 dt
->flags
= qemu_get_be32(f
);
6229 void cpu_save(QEMUFile
*f
, void *opaque
)
6231 CPUState
*env
= opaque
;
6232 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
6236 for(i
= 0; i
< CPU_NB_REGS
; i
++)
6237 qemu_put_betls(f
, &env
->regs
[i
]);
6238 qemu_put_betls(f
, &env
->eip
);
6239 qemu_put_betls(f
, &env
->eflags
);
6240 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
6241 qemu_put_be32s(f
, &hflags
);
6245 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
6247 for(i
= 0; i
< 8; i
++) {
6248 fptag
|= ((!env
->fptags
[i
]) << i
);
6251 qemu_put_be16s(f
, &fpuc
);
6252 qemu_put_be16s(f
, &fpus
);
6253 qemu_put_be16s(f
, &fptag
);
6255 #ifdef USE_X86LDOUBLE
6260 qemu_put_be16s(f
, &fpregs_format
);
6262 for(i
= 0; i
< 8; i
++) {
6263 #ifdef USE_X86LDOUBLE
6267 /* we save the real CPU data (in case of MMX usage only 'mant'
6268 contains the MMX register */
6269 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
6270 qemu_put_be64(f
, mant
);
6271 qemu_put_be16(f
, exp
);
6274 /* if we use doubles for float emulation, we save the doubles to
6275 avoid losing information in case of MMX usage. It can give
6276 problems if the image is restored on a CPU where long
6277 doubles are used instead. */
6278 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
6282 for(i
= 0; i
< 6; i
++)
6283 cpu_put_seg(f
, &env
->segs
[i
]);
6284 cpu_put_seg(f
, &env
->ldt
);
6285 cpu_put_seg(f
, &env
->tr
);
6286 cpu_put_seg(f
, &env
->gdt
);
6287 cpu_put_seg(f
, &env
->idt
);
6289 qemu_put_be32s(f
, &env
->sysenter_cs
);
6290 qemu_put_be32s(f
, &env
->sysenter_esp
);
6291 qemu_put_be32s(f
, &env
->sysenter_eip
);
6293 qemu_put_betls(f
, &env
->cr
[0]);
6294 qemu_put_betls(f
, &env
->cr
[2]);
6295 qemu_put_betls(f
, &env
->cr
[3]);
6296 qemu_put_betls(f
, &env
->cr
[4]);
6298 for(i
= 0; i
< 8; i
++)
6299 qemu_put_betls(f
, &env
->dr
[i
]);
6302 qemu_put_be32s(f
, &env
->a20_mask
);
6305 qemu_put_be32s(f
, &env
->mxcsr
);
6306 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
6307 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
6308 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
6311 #ifdef TARGET_X86_64
6312 qemu_put_be64s(f
, &env
->efer
);
6313 qemu_put_be64s(f
, &env
->star
);
6314 qemu_put_be64s(f
, &env
->lstar
);
6315 qemu_put_be64s(f
, &env
->cstar
);
6316 qemu_put_be64s(f
, &env
->fmask
);
6317 qemu_put_be64s(f
, &env
->kernelgsbase
);
6319 qemu_put_be32s(f
, &env
->smbase
);
6322 #ifdef USE_X86LDOUBLE
6323 /* XXX: add that in a FPU generic layer */
6324 union x86_longdouble
{
6329 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
6330 #define EXPBIAS1 1023
6331 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
6332 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
6334 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
6338 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
6339 /* exponent + sign */
6340 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
6341 e
|= SIGND1(temp
) >> 16;
6346 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6348 CPUState
*env
= opaque
;
6351 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
6353 if (version_id
!= 3 && version_id
!= 4)
6355 for(i
= 0; i
< CPU_NB_REGS
; i
++)
6356 qemu_get_betls(f
, &env
->regs
[i
]);
6357 qemu_get_betls(f
, &env
->eip
);
6358 qemu_get_betls(f
, &env
->eflags
);
6359 qemu_get_be32s(f
, &hflags
);
6361 qemu_get_be16s(f
, &fpuc
);
6362 qemu_get_be16s(f
, &fpus
);
6363 qemu_get_be16s(f
, &fptag
);
6364 qemu_get_be16s(f
, &fpregs_format
);
6366 /* NOTE: we cannot always restore the FPU state if the image come
6367 from a host with a different 'USE_X86LDOUBLE' define. We guess
6368 if we are in an MMX state to restore correctly in that case. */
6369 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
6370 for(i
= 0; i
< 8; i
++) {
6374 switch(fpregs_format
) {
6376 mant
= qemu_get_be64(f
);
6377 exp
= qemu_get_be16(f
);
6378 #ifdef USE_X86LDOUBLE
6379 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
6381 /* difficult case */
6383 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
6385 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
6389 mant
= qemu_get_be64(f
);
6390 #ifdef USE_X86LDOUBLE
6392 union x86_longdouble
*p
;
6393 /* difficult case */
6394 p
= (void *)&env
->fpregs
[i
];
6399 fp64_to_fp80(p
, mant
);
6403 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
6412 /* XXX: restore FPU round state */
6413 env
->fpstt
= (fpus
>> 11) & 7;
6414 env
->fpus
= fpus
& ~0x3800;
6416 for(i
= 0; i
< 8; i
++) {
6417 env
->fptags
[i
] = (fptag
>> i
) & 1;
6420 for(i
= 0; i
< 6; i
++)
6421 cpu_get_seg(f
, &env
->segs
[i
]);
6422 cpu_get_seg(f
, &env
->ldt
);
6423 cpu_get_seg(f
, &env
->tr
);
6424 cpu_get_seg(f
, &env
->gdt
);
6425 cpu_get_seg(f
, &env
->idt
);
6427 qemu_get_be32s(f
, &env
->sysenter_cs
);
6428 qemu_get_be32s(f
, &env
->sysenter_esp
);
6429 qemu_get_be32s(f
, &env
->sysenter_eip
);
6431 qemu_get_betls(f
, &env
->cr
[0]);
6432 qemu_get_betls(f
, &env
->cr
[2]);
6433 qemu_get_betls(f
, &env
->cr
[3]);
6434 qemu_get_betls(f
, &env
->cr
[4]);
6436 for(i
= 0; i
< 8; i
++)
6437 qemu_get_betls(f
, &env
->dr
[i
]);
6440 qemu_get_be32s(f
, &env
->a20_mask
);
6442 qemu_get_be32s(f
, &env
->mxcsr
);
6443 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
6444 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
6445 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
6448 #ifdef TARGET_X86_64
6449 qemu_get_be64s(f
, &env
->efer
);
6450 qemu_get_be64s(f
, &env
->star
);
6451 qemu_get_be64s(f
, &env
->lstar
);
6452 qemu_get_be64s(f
, &env
->cstar
);
6453 qemu_get_be64s(f
, &env
->fmask
);
6454 qemu_get_be64s(f
, &env
->kernelgsbase
);
6456 if (version_id
>= 4)
6457 qemu_get_be32s(f
, &env
->smbase
);
6459 /* XXX: compute hflags from scratch, except for CPL and IIF */
6460 env
->hflags
= hflags
;
6465 #elif defined(TARGET_PPC)
6466 void cpu_save(QEMUFile
*f
, void *opaque
)
6470 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6475 #elif defined(TARGET_MIPS)
6476 void cpu_save(QEMUFile
*f
, void *opaque
)
6480 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6485 #elif defined(TARGET_SPARC)
6486 void cpu_save(QEMUFile
*f
, void *opaque
)
6488 CPUState
*env
= opaque
;
6492 for(i
= 0; i
< 8; i
++)
6493 qemu_put_betls(f
, &env
->gregs
[i
]);
6494 for(i
= 0; i
< NWINDOWS
* 16; i
++)
6495 qemu_put_betls(f
, &env
->regbase
[i
]);
6498 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
6504 qemu_put_be32(f
, u
.i
);
6507 qemu_put_betls(f
, &env
->pc
);
6508 qemu_put_betls(f
, &env
->npc
);
6509 qemu_put_betls(f
, &env
->y
);
6511 qemu_put_be32(f
, tmp
);
6512 qemu_put_betls(f
, &env
->fsr
);
6513 qemu_put_betls(f
, &env
->tbr
);
6514 #ifndef TARGET_SPARC64
6515 qemu_put_be32s(f
, &env
->wim
);
6517 for(i
= 0; i
< 16; i
++)
6518 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
6522 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6524 CPUState
*env
= opaque
;
6528 for(i
= 0; i
< 8; i
++)
6529 qemu_get_betls(f
, &env
->gregs
[i
]);
6530 for(i
= 0; i
< NWINDOWS
* 16; i
++)
6531 qemu_get_betls(f
, &env
->regbase
[i
]);
6534 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
6539 u
.i
= qemu_get_be32(f
);
6543 qemu_get_betls(f
, &env
->pc
);
6544 qemu_get_betls(f
, &env
->npc
);
6545 qemu_get_betls(f
, &env
->y
);
6546 tmp
= qemu_get_be32(f
);
6547 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
6548 correctly updated */
6550 qemu_get_betls(f
, &env
->fsr
);
6551 qemu_get_betls(f
, &env
->tbr
);
6552 #ifndef TARGET_SPARC64
6553 qemu_get_be32s(f
, &env
->wim
);
6555 for(i
= 0; i
< 16; i
++)
6556 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
6562 #elif defined(TARGET_ARM)
6564 void cpu_save(QEMUFile
*f
, void *opaque
)
6567 CPUARMState
*env
= (CPUARMState
*)opaque
;
6569 for (i
= 0; i
< 16; i
++) {
6570 qemu_put_be32(f
, env
->regs
[i
]);
6572 qemu_put_be32(f
, cpsr_read(env
));
6573 qemu_put_be32(f
, env
->spsr
);
6574 for (i
= 0; i
< 6; i
++) {
6575 qemu_put_be32(f
, env
->banked_spsr
[i
]);
6576 qemu_put_be32(f
, env
->banked_r13
[i
]);
6577 qemu_put_be32(f
, env
->banked_r14
[i
]);
6579 for (i
= 0; i
< 5; i
++) {
6580 qemu_put_be32(f
, env
->usr_regs
[i
]);
6581 qemu_put_be32(f
, env
->fiq_regs
[i
]);
6583 qemu_put_be32(f
, env
->cp15
.c0_cpuid
);
6584 qemu_put_be32(f
, env
->cp15
.c0_cachetype
);
6585 qemu_put_be32(f
, env
->cp15
.c1_sys
);
6586 qemu_put_be32(f
, env
->cp15
.c1_coproc
);
6587 qemu_put_be32(f
, env
->cp15
.c1_xscaleauxcr
);
6588 qemu_put_be32(f
, env
->cp15
.c2_base0
);
6589 qemu_put_be32(f
, env
->cp15
.c2_base1
);
6590 qemu_put_be32(f
, env
->cp15
.c2_mask
);
6591 qemu_put_be32(f
, env
->cp15
.c2_data
);
6592 qemu_put_be32(f
, env
->cp15
.c2_insn
);
6593 qemu_put_be32(f
, env
->cp15
.c3
);
6594 qemu_put_be32(f
, env
->cp15
.c5_insn
);
6595 qemu_put_be32(f
, env
->cp15
.c5_data
);
6596 for (i
= 0; i
< 8; i
++) {
6597 qemu_put_be32(f
, env
->cp15
.c6_region
[i
]);
6599 qemu_put_be32(f
, env
->cp15
.c6_insn
);
6600 qemu_put_be32(f
, env
->cp15
.c6_data
);
6601 qemu_put_be32(f
, env
->cp15
.c9_insn
);
6602 qemu_put_be32(f
, env
->cp15
.c9_data
);
6603 qemu_put_be32(f
, env
->cp15
.c13_fcse
);
6604 qemu_put_be32(f
, env
->cp15
.c13_context
);
6605 qemu_put_be32(f
, env
->cp15
.c13_tls1
);
6606 qemu_put_be32(f
, env
->cp15
.c13_tls2
);
6607 qemu_put_be32(f
, env
->cp15
.c13_tls3
);
6608 qemu_put_be32(f
, env
->cp15
.c15_cpar
);
6610 qemu_put_be32(f
, env
->features
);
6612 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
6613 for (i
= 0; i
< 16; i
++) {
6615 u
.d
= env
->vfp
.regs
[i
];
6616 qemu_put_be32(f
, u
.l
.upper
);
6617 qemu_put_be32(f
, u
.l
.lower
);
6619 for (i
= 0; i
< 16; i
++) {
6620 qemu_put_be32(f
, env
->vfp
.xregs
[i
]);
6623 /* TODO: Should use proper FPSCR access functions. */
6624 qemu_put_be32(f
, env
->vfp
.vec_len
);
6625 qemu_put_be32(f
, env
->vfp
.vec_stride
);
6627 if (arm_feature(env
, ARM_FEATURE_VFP3
)) {
6628 for (i
= 16; i
< 32; i
++) {
6630 u
.d
= env
->vfp
.regs
[i
];
6631 qemu_put_be32(f
, u
.l
.upper
);
6632 qemu_put_be32(f
, u
.l
.lower
);
6637 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
6638 for (i
= 0; i
< 16; i
++) {
6639 qemu_put_be64(f
, env
->iwmmxt
.regs
[i
]);
6641 for (i
= 0; i
< 16; i
++) {
6642 qemu_put_be32(f
, env
->iwmmxt
.cregs
[i
]);
6646 if (arm_feature(env
, ARM_FEATURE_M
)) {
6647 qemu_put_be32(f
, env
->v7m
.other_sp
);
6648 qemu_put_be32(f
, env
->v7m
.vecbase
);
6649 qemu_put_be32(f
, env
->v7m
.basepri
);
6650 qemu_put_be32(f
, env
->v7m
.control
);
6651 qemu_put_be32(f
, env
->v7m
.current_sp
);
6652 qemu_put_be32(f
, env
->v7m
.exception
);
6656 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6658 CPUARMState
*env
= (CPUARMState
*)opaque
;
6661 if (version_id
!= ARM_CPU_SAVE_VERSION
)
6664 for (i
= 0; i
< 16; i
++) {
6665 env
->regs
[i
] = qemu_get_be32(f
);
6667 cpsr_write(env
, qemu_get_be32(f
), 0xffffffff);
6668 env
->spsr
= qemu_get_be32(f
);
6669 for (i
= 0; i
< 6; i
++) {
6670 env
->banked_spsr
[i
] = qemu_get_be32(f
);
6671 env
->banked_r13
[i
] = qemu_get_be32(f
);
6672 env
->banked_r14
[i
] = qemu_get_be32(f
);
6674 for (i
= 0; i
< 5; i
++) {
6675 env
->usr_regs
[i
] = qemu_get_be32(f
);
6676 env
->fiq_regs
[i
] = qemu_get_be32(f
);
6678 env
->cp15
.c0_cpuid
= qemu_get_be32(f
);
6679 env
->cp15
.c0_cachetype
= qemu_get_be32(f
);
6680 env
->cp15
.c1_sys
= qemu_get_be32(f
);
6681 env
->cp15
.c1_coproc
= qemu_get_be32(f
);
6682 env
->cp15
.c1_xscaleauxcr
= qemu_get_be32(f
);
6683 env
->cp15
.c2_base0
= qemu_get_be32(f
);
6684 env
->cp15
.c2_base1
= qemu_get_be32(f
);
6685 env
->cp15
.c2_mask
= qemu_get_be32(f
);
6686 env
->cp15
.c2_data
= qemu_get_be32(f
);
6687 env
->cp15
.c2_insn
= qemu_get_be32(f
);
6688 env
->cp15
.c3
= qemu_get_be32(f
);
6689 env
->cp15
.c5_insn
= qemu_get_be32(f
);
6690 env
->cp15
.c5_data
= qemu_get_be32(f
);
6691 for (i
= 0; i
< 8; i
++) {
6692 env
->cp15
.c6_region
[i
] = qemu_get_be32(f
);
6694 env
->cp15
.c6_insn
= qemu_get_be32(f
);
6695 env
->cp15
.c6_data
= qemu_get_be32(f
);
6696 env
->cp15
.c9_insn
= qemu_get_be32(f
);
6697 env
->cp15
.c9_data
= qemu_get_be32(f
);
6698 env
->cp15
.c13_fcse
= qemu_get_be32(f
);
6699 env
->cp15
.c13_context
= qemu_get_be32(f
);
6700 env
->cp15
.c13_tls1
= qemu_get_be32(f
);
6701 env
->cp15
.c13_tls2
= qemu_get_be32(f
);
6702 env
->cp15
.c13_tls3
= qemu_get_be32(f
);
6703 env
->cp15
.c15_cpar
= qemu_get_be32(f
);
6705 env
->features
= qemu_get_be32(f
);
6707 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
6708 for (i
= 0; i
< 16; i
++) {
6710 u
.l
.upper
= qemu_get_be32(f
);
6711 u
.l
.lower
= qemu_get_be32(f
);
6712 env
->vfp
.regs
[i
] = u
.d
;
6714 for (i
= 0; i
< 16; i
++) {
6715 env
->vfp
.xregs
[i
] = qemu_get_be32(f
);
6718 /* TODO: Should use proper FPSCR access functions. */
6719 env
->vfp
.vec_len
= qemu_get_be32(f
);
6720 env
->vfp
.vec_stride
= qemu_get_be32(f
);
6722 if (arm_feature(env
, ARM_FEATURE_VFP3
)) {
6723 for (i
= 0; i
< 16; i
++) {
6725 u
.l
.upper
= qemu_get_be32(f
);
6726 u
.l
.lower
= qemu_get_be32(f
);
6727 env
->vfp
.regs
[i
] = u
.d
;
6732 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
6733 for (i
= 0; i
< 16; i
++) {
6734 env
->iwmmxt
.regs
[i
] = qemu_get_be64(f
);
6736 for (i
= 0; i
< 16; i
++) {
6737 env
->iwmmxt
.cregs
[i
] = qemu_get_be32(f
);
6741 if (arm_feature(env
, ARM_FEATURE_M
)) {
6742 env
->v7m
.other_sp
= qemu_get_be32(f
);
6743 env
->v7m
.vecbase
= qemu_get_be32(f
);
6744 env
->v7m
.basepri
= qemu_get_be32(f
);
6745 env
->v7m
.control
= qemu_get_be32(f
);
6746 env
->v7m
.current_sp
= qemu_get_be32(f
);
6747 env
->v7m
.exception
= qemu_get_be32(f
);
6755 //#warning No CPU save/restore functions
6759 /***********************************************************/
6760 /* ram save/restore */
6762 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
6766 v
= qemu_get_byte(f
);
6769 if (qemu_get_buffer(f
, buf
, len
) != len
)
6773 v
= qemu_get_byte(f
);
6774 memset(buf
, v
, len
);
6782 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
6786 if (qemu_get_be32(f
) != phys_ram_size
)
6788 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
6789 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
6796 #define BDRV_HASH_BLOCK_SIZE 1024
6797 #define IOBUF_SIZE 4096
6798 #define RAM_CBLOCK_MAGIC 0xfabe
6800 typedef struct RamCompressState
{
6803 uint8_t buf
[IOBUF_SIZE
];
6806 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
6809 memset(s
, 0, sizeof(*s
));
6811 ret
= deflateInit2(&s
->zstream
, 1,
6813 9, Z_DEFAULT_STRATEGY
);
6816 s
->zstream
.avail_out
= IOBUF_SIZE
;
6817 s
->zstream
.next_out
= s
->buf
;
6821 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
6823 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
6824 qemu_put_be16(s
->f
, len
);
6825 qemu_put_buffer(s
->f
, buf
, len
);
6828 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
6832 s
->zstream
.avail_in
= len
;
6833 s
->zstream
.next_in
= (uint8_t *)buf
;
6834 while (s
->zstream
.avail_in
> 0) {
6835 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
6838 if (s
->zstream
.avail_out
== 0) {
6839 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
6840 s
->zstream
.avail_out
= IOBUF_SIZE
;
6841 s
->zstream
.next_out
= s
->buf
;
6847 static void ram_compress_close(RamCompressState
*s
)
6851 /* compress last bytes */
6853 ret
= deflate(&s
->zstream
, Z_FINISH
);
6854 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
6855 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
6857 ram_put_cblock(s
, s
->buf
, len
);
6859 s
->zstream
.avail_out
= IOBUF_SIZE
;
6860 s
->zstream
.next_out
= s
->buf
;
6861 if (ret
== Z_STREAM_END
)
6868 deflateEnd(&s
->zstream
);
6871 typedef struct RamDecompressState
{
6874 uint8_t buf
[IOBUF_SIZE
];
6875 } RamDecompressState
;
6877 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
6880 memset(s
, 0, sizeof(*s
));
6882 ret
= inflateInit(&s
->zstream
);
6888 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
6892 s
->zstream
.avail_out
= len
;
6893 s
->zstream
.next_out
= buf
;
6894 while (s
->zstream
.avail_out
> 0) {
6895 if (s
->zstream
.avail_in
== 0) {
6896 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
6898 clen
= qemu_get_be16(s
->f
);
6899 if (clen
> IOBUF_SIZE
)
6901 qemu_get_buffer(s
->f
, s
->buf
, clen
);
6902 s
->zstream
.avail_in
= clen
;
6903 s
->zstream
.next_in
= s
->buf
;
6905 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
6906 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
6913 static void ram_decompress_close(RamDecompressState
*s
)
6915 inflateEnd(&s
->zstream
);
6918 static void ram_save(QEMUFile
*f
, void *opaque
)
6921 RamCompressState s1
, *s
= &s1
;
6924 qemu_put_be32(f
, phys_ram_size
);
6925 if (ram_compress_open(s
, f
) < 0)
6927 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6929 if (tight_savevm_enabled
) {
6933 /* find if the memory block is available on a virtual
6936 for(j
= 0; j
< nb_drives
; j
++) {
6937 sector_num
= bdrv_hash_find(drives_table
[j
].bdrv
,
6939 BDRV_HASH_BLOCK_SIZE
);
6940 if (sector_num
>= 0)
6944 goto normal_compress
;
6947 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
6948 ram_compress_buf(s
, buf
, 10);
6954 ram_compress_buf(s
, buf
, 1);
6955 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6958 ram_compress_close(s
);
6961 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
6963 RamDecompressState s1
, *s
= &s1
;
6967 if (version_id
== 1)
6968 return ram_load_v1(f
, opaque
);
6969 if (version_id
!= 2)
6971 if (qemu_get_be32(f
) != phys_ram_size
)
6973 if (ram_decompress_open(s
, f
) < 0)
6975 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6976 if (ram_decompress_buf(s
, buf
, 1) < 0) {
6977 fprintf(stderr
, "Error while reading ram block header\n");
6981 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
6982 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
6991 ram_decompress_buf(s
, buf
+ 1, 9);
6993 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
6994 if (bs_index
>= nb_drives
) {
6995 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
6998 if (bdrv_read(drives_table
[bs_index
].bdrv
, sector_num
,
7000 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
7001 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
7002 bs_index
, sector_num
);
7009 printf("Error block header\n");
7013 ram_decompress_close(s
);
7017 /***********************************************************/
7018 /* bottom halves (can be seen as timers which expire ASAP) */
7027 static QEMUBH
*first_bh
= NULL
;
7029 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
7032 bh
= qemu_mallocz(sizeof(QEMUBH
));
7036 bh
->opaque
= opaque
;
7040 int qemu_bh_poll(void)
7059 void qemu_bh_schedule(QEMUBH
*bh
)
7061 CPUState
*env
= cpu_single_env
;
7065 bh
->next
= first_bh
;
7068 /* stop the currently executing CPU to execute the BH ASAP */
7070 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
7074 void qemu_bh_cancel(QEMUBH
*bh
)
7077 if (bh
->scheduled
) {
7080 pbh
= &(*pbh
)->next
;
7086 void qemu_bh_delete(QEMUBH
*bh
)
7092 /***********************************************************/
7093 /* machine registration */
7095 QEMUMachine
*first_machine
= NULL
;
7097 int qemu_register_machine(QEMUMachine
*m
)
7100 pm
= &first_machine
;
7108 static QEMUMachine
*find_machine(const char *name
)
7112 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
7113 if (!strcmp(m
->name
, name
))
7119 /***********************************************************/
7120 /* main execution loop */
7122 static void gui_update(void *opaque
)
7124 DisplayState
*ds
= opaque
;
7125 ds
->dpy_refresh(ds
);
7126 qemu_mod_timer(ds
->gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
7129 struct vm_change_state_entry
{
7130 VMChangeStateHandler
*cb
;
7132 LIST_ENTRY (vm_change_state_entry
) entries
;
7135 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
7137 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
7140 VMChangeStateEntry
*e
;
7142 e
= qemu_mallocz(sizeof (*e
));
7148 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
7152 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
7154 LIST_REMOVE (e
, entries
);
7158 static void vm_state_notify(int running
)
7160 VMChangeStateEntry
*e
;
7162 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
7163 e
->cb(e
->opaque
, running
);
7167 /* XXX: support several handlers */
7168 static VMStopHandler
*vm_stop_cb
;
7169 static void *vm_stop_opaque
;
7171 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
7174 vm_stop_opaque
= opaque
;
7178 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
7189 qemu_rearm_alarm_timer(alarm_timer
);
7193 void vm_stop(int reason
)
7196 cpu_disable_ticks();
7200 vm_stop_cb(vm_stop_opaque
, reason
);
7207 /* reset/shutdown handler */
7209 typedef struct QEMUResetEntry
{
7210 QEMUResetHandler
*func
;
7212 struct QEMUResetEntry
*next
;
7215 static QEMUResetEntry
*first_reset_entry
;
7216 static int reset_requested
;
7217 static int shutdown_requested
;
7218 static int powerdown_requested
;
7220 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
7222 QEMUResetEntry
**pre
, *re
;
7224 pre
= &first_reset_entry
;
7225 while (*pre
!= NULL
)
7226 pre
= &(*pre
)->next
;
7227 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
7229 re
->opaque
= opaque
;
7234 static void qemu_system_reset(void)
7238 /* reset all devices */
7239 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
7240 re
->func(re
->opaque
);
7244 void qemu_system_reset_request(void)
7247 shutdown_requested
= 1;
7249 reset_requested
= 1;
7252 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
7255 void qemu_system_shutdown_request(void)
7257 shutdown_requested
= 1;
7259 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
7262 void qemu_system_powerdown_request(void)
7264 powerdown_requested
= 1;
7266 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
7269 void main_loop_wait(int timeout
)
7271 IOHandlerRecord
*ioh
;
7272 fd_set rfds
, wfds
, xfds
;
7281 /* XXX: need to suppress polling by better using win32 events */
7283 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
7284 ret
|= pe
->func(pe
->opaque
);
7289 WaitObjects
*w
= &wait_objects
;
7291 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
7292 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
7293 if (w
->func
[ret
- WAIT_OBJECT_0
])
7294 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
7296 /* Check for additional signaled events */
7297 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
7299 /* Check if event is signaled */
7300 ret2
= WaitForSingleObject(w
->events
[i
], 0);
7301 if(ret2
== WAIT_OBJECT_0
) {
7303 w
->func
[i
](w
->opaque
[i
]);
7304 } else if (ret2
== WAIT_TIMEOUT
) {
7306 err
= GetLastError();
7307 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
7310 } else if (ret
== WAIT_TIMEOUT
) {
7312 err
= GetLastError();
7313 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
7317 /* poll any events */
7318 /* XXX: separate device handlers from system ones */
7323 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
7327 (!ioh
->fd_read_poll
||
7328 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
7329 FD_SET(ioh
->fd
, &rfds
);
7333 if (ioh
->fd_write
) {
7334 FD_SET(ioh
->fd
, &wfds
);
7344 tv
.tv_usec
= timeout
* 1000;
7346 #if defined(CONFIG_SLIRP)
7348 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
7351 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
7353 IOHandlerRecord
**pioh
;
7355 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
7356 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
7357 ioh
->fd_read(ioh
->opaque
);
7359 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
7360 ioh
->fd_write(ioh
->opaque
);
7364 /* remove deleted IO handlers */
7365 pioh
= &first_io_handler
;
7375 #if defined(CONFIG_SLIRP)
7382 slirp_select_poll(&rfds
, &wfds
, &xfds
);
7388 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
7389 qemu_get_clock(vm_clock
));
7390 /* run dma transfers, if any */
7394 /* real time timers */
7395 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
7396 qemu_get_clock(rt_clock
));
7398 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
7399 alarm_timer
->flags
&= ~(ALARM_FLAG_EXPIRED
);
7400 qemu_rearm_alarm_timer(alarm_timer
);
7403 /* Check bottom-halves last in case any of the earlier events triggered
7409 static int main_loop(void)
7412 #ifdef CONFIG_PROFILER
7417 cur_cpu
= first_cpu
;
7418 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
7425 #ifdef CONFIG_PROFILER
7426 ti
= profile_getclock();
7428 ret
= cpu_exec(env
);
7429 #ifdef CONFIG_PROFILER
7430 qemu_time
+= profile_getclock() - ti
;
7432 next_cpu
= env
->next_cpu
?: first_cpu
;
7433 if (event_pending
) {
7434 ret
= EXCP_INTERRUPT
;
7438 if (ret
== EXCP_HLT
) {
7439 /* Give the next CPU a chance to run. */
7443 if (ret
!= EXCP_HALTED
)
7445 /* all CPUs are halted ? */
7451 if (shutdown_requested
) {
7452 ret
= EXCP_INTERRUPT
;
7455 if (reset_requested
) {
7456 reset_requested
= 0;
7457 qemu_system_reset();
7458 ret
= EXCP_INTERRUPT
;
7460 if (powerdown_requested
) {
7461 powerdown_requested
= 0;
7462 qemu_system_powerdown();
7463 ret
= EXCP_INTERRUPT
;
7465 if (ret
== EXCP_DEBUG
) {
7466 vm_stop(EXCP_DEBUG
);
7468 /* If all cpus are halted then wait until the next IRQ */
7469 /* XXX: use timeout computed from timers */
7470 if (ret
== EXCP_HALTED
)
7477 #ifdef CONFIG_PROFILER
7478 ti
= profile_getclock();
7480 main_loop_wait(timeout
);
7481 #ifdef CONFIG_PROFILER
7482 dev_time
+= profile_getclock() - ti
;
7485 cpu_disable_ticks();
7489 static void help(int exitcode
)
7491 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
7492 "usage: %s [options] [disk_image]\n"
7494 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
7496 "Standard options:\n"
7497 "-M machine select emulated machine (-M ? for list)\n"
7498 "-cpu cpu select CPU (-cpu ? for list)\n"
7499 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
7500 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
7501 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
7502 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
7503 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][index=i]\n"
7504 " [,cyls=c,heads=h,secs=s[,trans=t]][snapshot=on|off]"
7505 " [,cache=on|off]\n"
7506 " use 'file' as a drive image\n"
7507 "-mtdblock file use 'file' as on-board Flash memory image\n"
7508 "-sd file use 'file' as SecureDigital card image\n"
7509 "-pflash file use 'file' as a parallel flash image\n"
7510 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
7511 "-snapshot write to temporary files instead of disk image files\n"
7513 "-no-frame open SDL window without a frame and window decorations\n"
7514 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
7515 "-no-quit disable SDL window close capability\n"
7518 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
7520 "-m megs set virtual RAM size to megs MB [default=%d]\n"
7521 "-smp n set the number of CPUs to 'n' [default=1]\n"
7522 "-nographic disable graphical output and redirect serial I/Os to console\n"
7523 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
7525 "-k language use keyboard layout (for example \"fr\" for French)\n"
7528 "-audio-help print list of audio drivers and their options\n"
7529 "-soundhw c1,... enable audio support\n"
7530 " and only specified sound cards (comma separated list)\n"
7531 " use -soundhw ? to get the list of supported cards\n"
7532 " use -soundhw all to enable all of them\n"
7534 "-localtime set the real time clock to local time [default=utc]\n"
7535 "-full-screen start in full screen\n"
7537 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
7539 "-usb enable the USB driver (will be the default soon)\n"
7540 "-usbdevice name add the host or guest USB device 'name'\n"
7541 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7542 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
7544 "-name string set the name of the guest\n"
7546 "Network options:\n"
7547 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
7548 " create a new Network Interface Card and connect it to VLAN 'n'\n"
7550 "-net user[,vlan=n][,hostname=host]\n"
7551 " connect the user mode network stack to VLAN 'n' and send\n"
7552 " hostname 'host' to DHCP clients\n"
7555 "-net tap[,vlan=n],ifname=name\n"
7556 " connect the host TAP network interface to VLAN 'n'\n"
7558 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
7559 " connect the host TAP network interface to VLAN 'n' and use the\n"
7560 " network scripts 'file' (default=%s)\n"
7561 " and 'dfile' (default=%s);\n"
7562 " use '[down]script=no' to disable script execution;\n"
7563 " use 'fd=h' to connect to an already opened TAP interface\n"
7565 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
7566 " connect the vlan 'n' to another VLAN using a socket connection\n"
7567 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
7568 " connect the vlan 'n' to multicast maddr and port\n"
7569 "-net none use it alone to have zero network devices; if no -net option\n"
7570 " is provided, the default is '-net nic -net user'\n"
7573 "-tftp dir allow tftp access to files in dir [-net user]\n"
7574 "-bootp file advertise file in BOOTP replies\n"
7576 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
7578 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
7579 " redirect TCP or UDP connections from host to guest [-net user]\n"
7582 "Linux boot specific:\n"
7583 "-kernel bzImage use 'bzImage' as kernel image\n"
7584 "-append cmdline use 'cmdline' as kernel command line\n"
7585 "-initrd file use 'file' as initial ram disk\n"
7587 "Debug/Expert options:\n"
7588 "-monitor dev redirect the monitor to char device 'dev'\n"
7589 "-serial dev redirect the serial port to char device 'dev'\n"
7590 "-parallel dev redirect the parallel port to char device 'dev'\n"
7591 "-pidfile file Write PID to 'file'\n"
7592 "-S freeze CPU at startup (use 'c' to start execution)\n"
7593 "-s wait gdb connection to port\n"
7594 "-p port set gdb connection port [default=%s]\n"
7595 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
7596 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
7597 " translation (t=none or lba) (usually qemu can guess them)\n"
7598 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
7600 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
7601 "-no-kqemu disable KQEMU kernel module usage\n"
7604 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
7605 " (default is CL-GD5446 PCI VGA)\n"
7606 "-no-acpi disable ACPI\n"
7608 "-no-reboot exit instead of rebooting\n"
7609 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
7610 "-vnc display start a VNC server on display\n"
7612 "-daemonize daemonize QEMU after initializing\n"
7614 "-option-rom rom load a file, rom, into the option ROM space\n"
7616 "-prom-env variable=value set OpenBIOS nvram variables\n"
7618 "-clock force the use of the given methods for timer alarm.\n"
7619 " To see what timers are available use -clock help\n"
7621 "During emulation, the following keys are useful:\n"
7622 "ctrl-alt-f toggle full screen\n"
7623 "ctrl-alt-n switch to virtual console 'n'\n"
7624 "ctrl-alt toggle mouse and keyboard grab\n"
7626 "When using -nographic, press 'ctrl-a h' to get some help.\n"
7631 DEFAULT_NETWORK_SCRIPT
,
7632 DEFAULT_NETWORK_DOWN_SCRIPT
,
7634 DEFAULT_GDBSTUB_PORT
,
7639 #define HAS_ARG 0x0001
7654 QEMU_OPTION_mtdblock
,
7658 QEMU_OPTION_snapshot
,
7660 QEMU_OPTION_no_fd_bootchk
,
7663 QEMU_OPTION_nographic
,
7664 QEMU_OPTION_portrait
,
7666 QEMU_OPTION_audio_help
,
7667 QEMU_OPTION_soundhw
,
7687 QEMU_OPTION_no_code_copy
,
7689 QEMU_OPTION_localtime
,
7690 QEMU_OPTION_cirrusvga
,
7693 QEMU_OPTION_std_vga
,
7695 QEMU_OPTION_monitor
,
7697 QEMU_OPTION_parallel
,
7699 QEMU_OPTION_full_screen
,
7700 QEMU_OPTION_no_frame
,
7701 QEMU_OPTION_alt_grab
,
7702 QEMU_OPTION_no_quit
,
7703 QEMU_OPTION_pidfile
,
7704 QEMU_OPTION_no_kqemu
,
7705 QEMU_OPTION_kernel_kqemu
,
7706 QEMU_OPTION_win2k_hack
,
7708 QEMU_OPTION_usbdevice
,
7711 QEMU_OPTION_no_acpi
,
7712 QEMU_OPTION_no_reboot
,
7713 QEMU_OPTION_show_cursor
,
7714 QEMU_OPTION_daemonize
,
7715 QEMU_OPTION_option_rom
,
7716 QEMU_OPTION_semihosting
,
7718 QEMU_OPTION_prom_env
,
7719 QEMU_OPTION_old_param
,
7721 QEMU_OPTION_startdate
,
7724 typedef struct QEMUOption
{
7730 const QEMUOption qemu_options
[] = {
7731 { "h", 0, QEMU_OPTION_h
},
7732 { "help", 0, QEMU_OPTION_h
},
7734 { "M", HAS_ARG
, QEMU_OPTION_M
},
7735 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
7736 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
7737 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
7738 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
7739 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
7740 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
7741 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
7742 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
7743 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
7744 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
7745 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
7746 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
7747 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
7748 { "snapshot", 0, QEMU_OPTION_snapshot
},
7750 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
7752 { "m", HAS_ARG
, QEMU_OPTION_m
},
7753 { "nographic", 0, QEMU_OPTION_nographic
},
7754 { "portrait", 0, QEMU_OPTION_portrait
},
7755 { "k", HAS_ARG
, QEMU_OPTION_k
},
7757 { "audio-help", 0, QEMU_OPTION_audio_help
},
7758 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
7761 { "net", HAS_ARG
, QEMU_OPTION_net
},
7763 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
7764 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
7766 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
7768 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
7771 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
7772 { "append", HAS_ARG
, QEMU_OPTION_append
},
7773 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
7775 { "S", 0, QEMU_OPTION_S
},
7776 { "s", 0, QEMU_OPTION_s
},
7777 { "p", HAS_ARG
, QEMU_OPTION_p
},
7778 { "d", HAS_ARG
, QEMU_OPTION_d
},
7779 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
7780 { "L", HAS_ARG
, QEMU_OPTION_L
},
7781 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
7782 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
7784 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
7785 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
7787 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7788 { "g", 1, QEMU_OPTION_g
},
7790 { "localtime", 0, QEMU_OPTION_localtime
},
7791 { "std-vga", 0, QEMU_OPTION_std_vga
},
7792 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
7793 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
7794 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
7795 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
7796 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
7797 { "full-screen", 0, QEMU_OPTION_full_screen
},
7799 { "no-frame", 0, QEMU_OPTION_no_frame
},
7800 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
7801 { "no-quit", 0, QEMU_OPTION_no_quit
},
7803 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
7804 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
7805 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
7806 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
7807 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
7809 /* temporary options */
7810 { "usb", 0, QEMU_OPTION_usb
},
7811 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
7812 { "vmwarevga", 0, QEMU_OPTION_vmsvga
},
7813 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
7814 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
7815 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
7816 { "daemonize", 0, QEMU_OPTION_daemonize
},
7817 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
7818 #if defined(TARGET_ARM) || defined(TARGET_M68K)
7819 { "semihosting", 0, QEMU_OPTION_semihosting
},
7821 { "name", HAS_ARG
, QEMU_OPTION_name
},
7822 #if defined(TARGET_SPARC)
7823 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
7825 #if defined(TARGET_ARM)
7826 { "old-param", 0, QEMU_OPTION_old_param
},
7828 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
7829 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
7833 /* password input */
7835 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
7840 if (!bdrv_is_encrypted(bs
))
7843 term_printf("%s is encrypted.\n", name
);
7844 for(i
= 0; i
< 3; i
++) {
7845 monitor_readline("Password: ", 1, password
, sizeof(password
));
7846 if (bdrv_set_key(bs
, password
) == 0)
7848 term_printf("invalid password\n");
7853 static BlockDriverState
*get_bdrv(int index
)
7855 if (index
> nb_drives
)
7857 return drives_table
[index
].bdrv
;
7860 static void read_passwords(void)
7862 BlockDriverState
*bs
;
7865 for(i
= 0; i
< 6; i
++) {
7868 qemu_key_check(bs
, bdrv_get_device_name(bs
));
7872 /* XXX: currently we cannot use simultaneously different CPUs */
7873 static void register_machines(void)
7875 #if defined(TARGET_I386)
7876 qemu_register_machine(&pc_machine
);
7877 qemu_register_machine(&isapc_machine
);
7878 #elif defined(TARGET_PPC)
7879 qemu_register_machine(&heathrow_machine
);
7880 qemu_register_machine(&core99_machine
);
7881 qemu_register_machine(&prep_machine
);
7882 qemu_register_machine(&ref405ep_machine
);
7883 qemu_register_machine(&taihu_machine
);
7884 #elif defined(TARGET_MIPS)
7885 qemu_register_machine(&mips_machine
);
7886 qemu_register_machine(&mips_malta_machine
);
7887 qemu_register_machine(&mips_pica61_machine
);
7888 qemu_register_machine(&mips_mipssim_machine
);
7889 #elif defined(TARGET_SPARC)
7890 #ifdef TARGET_SPARC64
7891 qemu_register_machine(&sun4u_machine
);
7893 qemu_register_machine(&ss5_machine
);
7894 qemu_register_machine(&ss10_machine
);
7895 qemu_register_machine(&ss600mp_machine
);
7896 qemu_register_machine(&ss20_machine
);
7897 qemu_register_machine(&ss2_machine
);
7898 qemu_register_machine(&ss1000_machine
);
7899 qemu_register_machine(&ss2000_machine
);
7901 #elif defined(TARGET_ARM)
7902 qemu_register_machine(&integratorcp_machine
);
7903 qemu_register_machine(&versatilepb_machine
);
7904 qemu_register_machine(&versatileab_machine
);
7905 qemu_register_machine(&realview_machine
);
7906 qemu_register_machine(&akitapda_machine
);
7907 qemu_register_machine(&spitzpda_machine
);
7908 qemu_register_machine(&borzoipda_machine
);
7909 qemu_register_machine(&terrierpda_machine
);
7910 qemu_register_machine(&palmte_machine
);
7911 qemu_register_machine(&lm3s811evb_machine
);
7912 qemu_register_machine(&lm3s6965evb_machine
);
7913 qemu_register_machine(&connex_machine
);
7914 qemu_register_machine(&verdex_machine
);
7915 qemu_register_machine(&mainstone2_machine
);
7916 #elif defined(TARGET_SH4)
7917 qemu_register_machine(&shix_machine
);
7918 qemu_register_machine(&r2d_machine
);
7919 #elif defined(TARGET_ALPHA)
7921 #elif defined(TARGET_M68K)
7922 qemu_register_machine(&mcf5208evb_machine
);
7923 qemu_register_machine(&an5206_machine
);
7924 qemu_register_machine(&dummy_m68k_machine
);
7925 #elif defined(TARGET_CRIS)
7926 qemu_register_machine(&bareetraxfs_machine
);
7928 #error unsupported CPU
7933 struct soundhw soundhw
[] = {
7934 #ifdef HAS_AUDIO_CHOICE
7941 { .init_isa
= pcspk_audio_init
}
7946 "Creative Sound Blaster 16",
7949 { .init_isa
= SB16_init
}
7956 "Yamaha YMF262 (OPL3)",
7958 "Yamaha YM3812 (OPL2)",
7962 { .init_isa
= Adlib_init
}
7969 "Gravis Ultrasound GF1",
7972 { .init_isa
= GUS_init
}
7978 "ENSONIQ AudioPCI ES1370",
7981 { .init_pci
= es1370_init
}
7985 { NULL
, NULL
, 0, 0, { NULL
} }
7988 static void select_soundhw (const char *optarg
)
7992 if (*optarg
== '?') {
7995 printf ("Valid sound card names (comma separated):\n");
7996 for (c
= soundhw
; c
->name
; ++c
) {
7997 printf ("%-11s %s\n", c
->name
, c
->descr
);
7999 printf ("\n-soundhw all will enable all of the above\n");
8000 exit (*optarg
!= '?');
8008 if (!strcmp (optarg
, "all")) {
8009 for (c
= soundhw
; c
->name
; ++c
) {
8017 e
= strchr (p
, ',');
8018 l
= !e
? strlen (p
) : (size_t) (e
- p
);
8020 for (c
= soundhw
; c
->name
; ++c
) {
8021 if (!strncmp (c
->name
, p
, l
)) {
8030 "Unknown sound card name (too big to show)\n");
8033 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
8038 p
+= l
+ (e
!= NULL
);
8042 goto show_valid_cards
;
8048 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
8050 exit(STATUS_CONTROL_C_EXIT
);
8055 #define MAX_NET_CLIENTS 32
8057 int main(int argc
, char **argv
)
8059 #ifdef CONFIG_GDBSTUB
8061 const char *gdbstub_port
;
8063 uint32_t boot_devices_bitmap
= 0;
8065 int snapshot
, linux_boot
, net_boot
;
8066 const char *initrd_filename
;
8067 const char *kernel_filename
, *kernel_cmdline
;
8068 const char *boot_devices
= "";
8069 DisplayState
*ds
= &display_state
;
8070 int cyls
, heads
, secs
, translation
;
8071 char net_clients
[MAX_NET_CLIENTS
][256];
8075 const char *r
, *optarg
;
8076 CharDriverState
*monitor_hd
;
8077 char monitor_device
[128];
8078 char serial_devices
[MAX_SERIAL_PORTS
][128];
8079 int serial_device_index
;
8080 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
8081 int parallel_device_index
;
8082 const char *loadvm
= NULL
;
8083 QEMUMachine
*machine
;
8084 const char *cpu_model
;
8085 char usb_devices
[MAX_USB_CMDLINE
][128];
8086 int usb_devices_index
;
8088 const char *pid_file
= NULL
;
8091 LIST_INIT (&vm_change_state_head
);
8094 struct sigaction act
;
8095 sigfillset(&act
.sa_mask
);
8097 act
.sa_handler
= SIG_IGN
;
8098 sigaction(SIGPIPE
, &act
, NULL
);
8101 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
8102 /* Note: cpu_interrupt() is currently not SMP safe, so we force
8103 QEMU to run on a single CPU */
8108 h
= GetCurrentProcess();
8109 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
8110 for(i
= 0; i
< 32; i
++) {
8111 if (mask
& (1 << i
))
8116 SetProcessAffinityMask(h
, mask
);
8122 register_machines();
8123 machine
= first_machine
;
8125 initrd_filename
= NULL
;
8126 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
8127 vga_ram_size
= VGA_RAM_SIZE
;
8128 #ifdef CONFIG_GDBSTUB
8130 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
8134 kernel_filename
= NULL
;
8135 kernel_cmdline
= "";
8136 cyls
= heads
= secs
= 0;
8137 translation
= BIOS_ATA_TRANSLATION_AUTO
;
8138 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
8140 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
8141 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
8142 serial_devices
[i
][0] = '\0';
8143 serial_device_index
= 0;
8145 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
8146 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
8147 parallel_devices
[i
][0] = '\0';
8148 parallel_device_index
= 0;
8150 usb_devices_index
= 0;
8158 /* default mac address of the first network interface */
8166 hda_index
= drive_add(HD_ALIAS
, argv
[optind
++], 0);
8168 const QEMUOption
*popt
;
8171 /* Treat --foo the same as -foo. */
8174 popt
= qemu_options
;
8177 fprintf(stderr
, "%s: invalid option -- '%s'\n",
8181 if (!strcmp(popt
->name
, r
+ 1))
8185 if (popt
->flags
& HAS_ARG
) {
8186 if (optind
>= argc
) {
8187 fprintf(stderr
, "%s: option '%s' requires an argument\n",
8191 optarg
= argv
[optind
++];
8196 switch(popt
->index
) {
8198 machine
= find_machine(optarg
);
8201 printf("Supported machines are:\n");
8202 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
8203 printf("%-10s %s%s\n",
8205 m
== first_machine
? " (default)" : "");
8207 exit(*optarg
!= '?');
8210 case QEMU_OPTION_cpu
:
8211 /* hw initialization will check this */
8212 if (*optarg
== '?') {
8213 /* XXX: implement xxx_cpu_list for targets that still miss it */
8214 #if defined(cpu_list)
8215 cpu_list(stdout
, &fprintf
);
8222 case QEMU_OPTION_initrd
:
8223 initrd_filename
= optarg
;
8225 case QEMU_OPTION_hda
:
8227 hda_index
= drive_add(HD_ALIAS
, optarg
, 0);
8229 hda_index
= drive_add(HD_ALIAS
8230 ",cyls=%d,heads=%d,secs=%d%s",
8231 optarg
, 0, cyls
, heads
, secs
,
8232 translation
== BIOS_ATA_TRANSLATION_LBA
?
8234 translation
== BIOS_ATA_TRANSLATION_NONE
?
8235 ",trans=none" : "");
8237 case QEMU_OPTION_hdb
:
8238 case QEMU_OPTION_hdc
:
8239 case QEMU_OPTION_hdd
:
8240 drive_add(HD_ALIAS
, optarg
, popt
->index
- QEMU_OPTION_hda
);
8242 case QEMU_OPTION_drive
:
8243 drive_add("%s", optarg
);
8245 case QEMU_OPTION_mtdblock
:
8246 drive_add(MTD_ALIAS
, optarg
);
8248 case QEMU_OPTION_sd
:
8249 drive_add("file=\"%s\"," SD_ALIAS
, optarg
);
8251 case QEMU_OPTION_pflash
:
8252 drive_add(PFLASH_ALIAS
, optarg
);
8254 case QEMU_OPTION_snapshot
:
8257 case QEMU_OPTION_hdachs
:
8261 cyls
= strtol(p
, (char **)&p
, 0);
8262 if (cyls
< 1 || cyls
> 16383)
8267 heads
= strtol(p
, (char **)&p
, 0);
8268 if (heads
< 1 || heads
> 16)
8273 secs
= strtol(p
, (char **)&p
, 0);
8274 if (secs
< 1 || secs
> 63)
8278 if (!strcmp(p
, "none"))
8279 translation
= BIOS_ATA_TRANSLATION_NONE
;
8280 else if (!strcmp(p
, "lba"))
8281 translation
= BIOS_ATA_TRANSLATION_LBA
;
8282 else if (!strcmp(p
, "auto"))
8283 translation
= BIOS_ATA_TRANSLATION_AUTO
;
8286 } else if (*p
!= '\0') {
8288 fprintf(stderr
, "qemu: invalid physical CHS format\n");
8291 if (hda_index
!= -1)
8292 snprintf(drives_opt
[hda_index
] +
8293 strlen(drives_opt
[hda_index
]),
8294 sizeof(drives_opt
[0]) -
8295 strlen(drives_opt
[hda_index
]),
8296 ",cyls=%d,heads=%d,secs=%d%s",
8298 translation
== BIOS_ATA_TRANSLATION_LBA
?
8300 translation
== BIOS_ATA_TRANSLATION_NONE
?
8301 ",trans=none" : "");
8304 case QEMU_OPTION_nographic
:
8305 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
8306 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "null");
8307 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
8310 case QEMU_OPTION_portrait
:
8313 case QEMU_OPTION_kernel
:
8314 kernel_filename
= optarg
;
8316 case QEMU_OPTION_append
:
8317 kernel_cmdline
= optarg
;
8319 case QEMU_OPTION_cdrom
:
8320 drive_add("file=\"%s\"," CDROM_ALIAS
, optarg
);
8322 case QEMU_OPTION_boot
:
8323 boot_devices
= optarg
;
8324 /* We just do some generic consistency checks */
8326 /* Could easily be extended to 64 devices if needed */
8329 boot_devices_bitmap
= 0;
8330 for (p
= boot_devices
; *p
!= '\0'; p
++) {
8331 /* Allowed boot devices are:
8332 * a b : floppy disk drives
8333 * c ... f : IDE disk drives
8334 * g ... m : machine implementation dependant drives
8335 * n ... p : network devices
8336 * It's up to each machine implementation to check
8337 * if the given boot devices match the actual hardware
8338 * implementation and firmware features.
8340 if (*p
< 'a' || *p
> 'q') {
8341 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
8344 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
8346 "Boot device '%c' was given twice\n",*p
);
8349 boot_devices_bitmap
|= 1 << (*p
- 'a');
8353 case QEMU_OPTION_fda
:
8354 case QEMU_OPTION_fdb
:
8355 drive_add("file=\"%s\"," FD_ALIAS
, optarg
,
8356 popt
->index
- QEMU_OPTION_fda
);
8359 case QEMU_OPTION_no_fd_bootchk
:
8363 case QEMU_OPTION_no_code_copy
:
8364 code_copy_enabled
= 0;
8366 case QEMU_OPTION_net
:
8367 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
8368 fprintf(stderr
, "qemu: too many network clients\n");
8371 pstrcpy(net_clients
[nb_net_clients
],
8372 sizeof(net_clients
[0]),
8377 case QEMU_OPTION_tftp
:
8378 tftp_prefix
= optarg
;
8380 case QEMU_OPTION_bootp
:
8381 bootp_filename
= optarg
;
8384 case QEMU_OPTION_smb
:
8385 net_slirp_smb(optarg
);
8388 case QEMU_OPTION_redir
:
8389 net_slirp_redir(optarg
);
8393 case QEMU_OPTION_audio_help
:
8397 case QEMU_OPTION_soundhw
:
8398 select_soundhw (optarg
);
8405 ram_size
= atoi(optarg
) * 1024 * 1024;
8408 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
8409 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
8410 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
8419 mask
= cpu_str_to_log_mask(optarg
);
8421 printf("Log items (comma separated):\n");
8422 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
8423 printf("%-10s %s\n", item
->name
, item
->help
);
8430 #ifdef CONFIG_GDBSTUB
8435 gdbstub_port
= optarg
;
8441 case QEMU_OPTION_bios
:
8448 keyboard_layout
= optarg
;
8450 case QEMU_OPTION_localtime
:
8453 case QEMU_OPTION_cirrusvga
:
8454 cirrus_vga_enabled
= 1;
8457 case QEMU_OPTION_vmsvga
:
8458 cirrus_vga_enabled
= 0;
8461 case QEMU_OPTION_std_vga
:
8462 cirrus_vga_enabled
= 0;
8470 w
= strtol(p
, (char **)&p
, 10);
8473 fprintf(stderr
, "qemu: invalid resolution or depth\n");
8479 h
= strtol(p
, (char **)&p
, 10);
8484 depth
= strtol(p
, (char **)&p
, 10);
8485 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
8486 depth
!= 24 && depth
!= 32)
8488 } else if (*p
== '\0') {
8489 depth
= graphic_depth
;
8496 graphic_depth
= depth
;
8499 case QEMU_OPTION_echr
:
8502 term_escape_char
= strtol(optarg
, &r
, 0);
8504 printf("Bad argument to echr\n");
8507 case QEMU_OPTION_monitor
:
8508 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
8510 case QEMU_OPTION_serial
:
8511 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
8512 fprintf(stderr
, "qemu: too many serial ports\n");
8515 pstrcpy(serial_devices
[serial_device_index
],
8516 sizeof(serial_devices
[0]), optarg
);
8517 serial_device_index
++;
8519 case QEMU_OPTION_parallel
:
8520 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
8521 fprintf(stderr
, "qemu: too many parallel ports\n");
8524 pstrcpy(parallel_devices
[parallel_device_index
],
8525 sizeof(parallel_devices
[0]), optarg
);
8526 parallel_device_index
++;
8528 case QEMU_OPTION_loadvm
:
8531 case QEMU_OPTION_full_screen
:
8535 case QEMU_OPTION_no_frame
:
8538 case QEMU_OPTION_alt_grab
:
8541 case QEMU_OPTION_no_quit
:
8545 case QEMU_OPTION_pidfile
:
8549 case QEMU_OPTION_win2k_hack
:
8550 win2k_install_hack
= 1;
8554 case QEMU_OPTION_no_kqemu
:
8557 case QEMU_OPTION_kernel_kqemu
:
8561 case QEMU_OPTION_usb
:
8564 case QEMU_OPTION_usbdevice
:
8566 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
8567 fprintf(stderr
, "Too many USB devices\n");
8570 pstrcpy(usb_devices
[usb_devices_index
],
8571 sizeof(usb_devices
[usb_devices_index
]),
8573 usb_devices_index
++;
8575 case QEMU_OPTION_smp
:
8576 smp_cpus
= atoi(optarg
);
8577 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
8578 fprintf(stderr
, "Invalid number of CPUs\n");
8582 case QEMU_OPTION_vnc
:
8583 vnc_display
= optarg
;
8585 case QEMU_OPTION_no_acpi
:
8588 case QEMU_OPTION_no_reboot
:
8591 case QEMU_OPTION_show_cursor
:
8594 case QEMU_OPTION_daemonize
:
8597 case QEMU_OPTION_option_rom
:
8598 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
8599 fprintf(stderr
, "Too many option ROMs\n");
8602 option_rom
[nb_option_roms
] = optarg
;
8605 case QEMU_OPTION_semihosting
:
8606 semihosting_enabled
= 1;
8608 case QEMU_OPTION_name
:
8612 case QEMU_OPTION_prom_env
:
8613 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
8614 fprintf(stderr
, "Too many prom variables\n");
8617 prom_envs
[nb_prom_envs
] = optarg
;
8622 case QEMU_OPTION_old_param
:
8626 case QEMU_OPTION_clock
:
8627 configure_alarms(optarg
);
8629 case QEMU_OPTION_startdate
:
8632 if (!strcmp(optarg
, "now")) {
8633 rtc_start_date
= -1;
8635 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
8643 } else if (sscanf(optarg
, "%d-%d-%d",
8646 &tm
.tm_mday
) == 3) {
8655 rtc_start_date
= mktimegm(&tm
);
8656 if (rtc_start_date
== -1) {
8658 fprintf(stderr
, "Invalid date format. Valid format are:\n"
8659 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
8670 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
8671 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
8678 if (pipe(fds
) == -1)
8689 len
= read(fds
[0], &status
, 1);
8690 if (len
== -1 && (errno
== EINTR
))
8695 else if (status
== 1) {
8696 fprintf(stderr
, "Could not acquire pidfile\n");
8714 signal(SIGTSTP
, SIG_IGN
);
8715 signal(SIGTTOU
, SIG_IGN
);
8716 signal(SIGTTIN
, SIG_IGN
);
8720 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
8723 write(fds
[1], &status
, 1);
8725 fprintf(stderr
, "Could not acquire pid file\n");
8733 linux_boot
= (kernel_filename
!= NULL
);
8734 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
8736 /* XXX: this should not be: some embedded targets just have flash */
8737 if (!linux_boot
&& net_boot
== 0 &&
8741 /* boot to floppy or the default cd if no hard disk defined yet */
8742 if (!boot_devices
[0]) {
8743 boot_devices
= "cad";
8745 setvbuf(stdout
, NULL
, _IOLBF
, 0);
8755 /* init network clients */
8756 if (nb_net_clients
== 0) {
8757 /* if no clients, we use a default config */
8758 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
8760 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
8765 for(i
= 0;i
< nb_net_clients
; i
++) {
8766 if (net_client_init(net_clients
[i
]) < 0)
8769 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
8770 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
8772 if (vlan
->nb_guest_devs
== 0) {
8773 fprintf(stderr
, "Invalid vlan (%d) with no nics\n", vlan
->id
);
8776 if (vlan
->nb_host_devs
== 0)
8778 "Warning: vlan %d is not connected to host network\n",
8783 /* XXX: this should be moved in the PC machine instantiation code */
8784 if (net_boot
!= 0) {
8786 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
8787 const char *model
= nd_table
[i
].model
;
8789 if (net_boot
& (1 << i
)) {
8792 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
8793 if (get_image_size(buf
) > 0) {
8794 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
8795 fprintf(stderr
, "Too many option ROMs\n");
8798 option_rom
[nb_option_roms
] = strdup(buf
);
8805 fprintf(stderr
, "No valid PXE rom found for network device\n");
8811 /* init the memory */
8812 phys_ram_size
= ram_size
+ vga_ram_size
+ MAX_BIOS_SIZE
;
8814 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
8815 if (!phys_ram_base
) {
8816 fprintf(stderr
, "Could not allocate physical memory\n");
8822 /* we always create the cdrom drive, even if no disk is there */
8824 if (nb_drives_opt
< MAX_DRIVES
)
8825 drive_add(CDROM_ALIAS
);
8827 /* we always create at least one floppy */
8829 if (nb_drives_opt
< MAX_DRIVES
)
8830 drive_add(FD_ALIAS
, 0);
8832 /* we always create one sd slot, even if no card is in it */
8834 if (nb_drives_opt
< MAX_DRIVES
)
8835 drive_add(SD_ALIAS
);
8837 /* open the virtual block devices */
8839 for(i
= 0; i
< nb_drives_opt
; i
++)
8840 if (drive_init(drives_opt
[i
], snapshot
, machine
) == -1)
8843 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
8844 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
8849 memset(&display_state
, 0, sizeof(display_state
));
8851 /* nearly nothing to do */
8852 dumb_display_init(ds
);
8853 } else if (vnc_display
!= NULL
) {
8854 vnc_display_init(ds
);
8855 if (vnc_display_open(ds
, vnc_display
) < 0)
8858 #if defined(CONFIG_SDL)
8859 sdl_display_init(ds
, full_screen
, no_frame
);
8860 #elif defined(CONFIG_COCOA)
8861 cocoa_display_init(ds
, full_screen
);
8863 dumb_display_init(ds
);
8867 /* Maintain compatibility with multiple stdio monitors */
8868 if (!strcmp(monitor_device
,"stdio")) {
8869 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
8870 if (!strcmp(serial_devices
[i
],"mon:stdio")) {
8871 monitor_device
[0] = '\0';
8873 } else if (!strcmp(serial_devices
[i
],"stdio")) {
8874 monitor_device
[0] = '\0';
8875 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "mon:stdio");
8880 if (monitor_device
[0] != '\0') {
8881 monitor_hd
= qemu_chr_open(monitor_device
);
8883 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
8886 monitor_init(monitor_hd
, !nographic
);
8889 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
8890 const char *devname
= serial_devices
[i
];
8891 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
8892 serial_hds
[i
] = qemu_chr_open(devname
);
8893 if (!serial_hds
[i
]) {
8894 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
8898 if (strstart(devname
, "vc", 0))
8899 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
8903 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
8904 const char *devname
= parallel_devices
[i
];
8905 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
8906 parallel_hds
[i
] = qemu_chr_open(devname
);
8907 if (!parallel_hds
[i
]) {
8908 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
8912 if (strstart(devname
, "vc", 0))
8913 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
8917 machine
->init(ram_size
, vga_ram_size
, boot_devices
, ds
,
8918 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
8920 /* init USB devices */
8922 for(i
= 0; i
< usb_devices_index
; i
++) {
8923 if (usb_device_add(usb_devices
[i
]) < 0) {
8924 fprintf(stderr
, "Warning: could not add USB device %s\n",
8930 if (display_state
.dpy_refresh
) {
8931 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
8932 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
8935 #ifdef CONFIG_GDBSTUB
8937 /* XXX: use standard host:port notation and modify options
8939 if (gdbserver_start(gdbstub_port
) < 0) {
8940 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
8951 /* XXX: simplify init */
8964 len
= write(fds
[1], &status
, 1);
8965 if (len
== -1 && (errno
== EINTR
))
8971 TFR(fd
= open("/dev/null", O_RDWR
));
8985 #if !defined(_WIN32)
8986 /* close network clients */
8987 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
8988 VLANClientState
*vc
;
8990 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
8991 if (vc
->fd_read
== tap_receive
) {
8993 TAPState
*s
= vc
->opaque
;
8995 if (sscanf(vc
->info_str
, "tap: ifname=%63s ", ifname
) == 1 &&
8997 launch_script(s
->down_script
, ifname
, s
->fd
);