4 * Copyright (c) 2003-2007 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
45 #include <sys/select.h>
46 #include <arpa/inet.h>
52 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
53 #include <freebsd/stdlib.h>
57 #include <linux/if_tun.h>
60 #include <linux/rtc.h>
62 /* For the benefit of older linux systems which don't supply it,
63 we use a local copy of hpet.h. */
64 /* #include <linux/hpet.h> */
67 #include <linux/ppdev.h>
68 #include <linux/parport.h>
71 #include <sys/ethernet.h>
72 #include <sys/sockio.h>
73 #include <netinet/arp.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_icmp.h> // must come after ip.h
78 #include <netinet/udp.h>
79 #include <netinet/tcp.h>
87 int inet_aton(const char *cp
, struct in_addr
*ia
);
90 #if defined(CONFIG_SLIRP)
96 #include <sys/timeb.h>
98 #define getopt_long_only getopt_long
99 #define memalign(align, size) malloc(size)
102 #include "qemu_socket.h"
108 #endif /* CONFIG_SDL */
112 #define main qemu_main
113 #endif /* CONFIG_COCOA */
117 #include "exec-all.h"
120 #include "qemu-kvm.h"
123 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
125 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
127 #define SMBD_COMMAND "/usr/sbin/smbd"
130 //#define DEBUG_UNUSED_IOPORT
131 //#define DEBUG_IOPORT
133 #if HOST_LONG_BITS < 64
134 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
136 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024 * 1024ULL)
140 #define DEFAULT_RAM_SIZE 144
142 #define DEFAULT_RAM_SIZE 128
145 #define GUI_REFRESH_INTERVAL 30
147 /* Max number of USB devices that can be specified on the commandline. */
148 #define MAX_USB_CMDLINE 8
150 /* XXX: use a two level table to limit memory usage */
151 #define MAX_IOPORTS 65536
153 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
154 char phys_ram_file
[1024];
155 void *ioport_opaque
[MAX_IOPORTS
];
156 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
157 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
158 /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available
159 to store the VM snapshots */
160 BlockDriverState
*bs_table
[MAX_DISKS
+ 1], *fd_table
[MAX_FD
];
161 BlockDriverState
*pflash_table
[MAX_PFLASH
];
162 BlockDriverState
*sd_bdrv
;
163 BlockDriverState
*mtd_bdrv
;
164 /* point to the block driver where the snapshots are managed */
165 BlockDriverState
*bs_snapshots
;
167 static DisplayState display_state
;
169 const char* keyboard_layout
= NULL
;
170 int64_t ticks_per_sec
;
171 int boot_device
= 'c';
173 int pit_min_timer_count
= 0;
175 NICInfo nd_table
[MAX_NICS
];
178 int cirrus_vga_enabled
= 1;
179 int vmsvga_enabled
= 0;
181 int graphic_width
= 1024;
182 int graphic_height
= 768;
183 int graphic_depth
= 8;
185 int graphic_width
= 800;
186 int graphic_height
= 600;
187 int graphic_depth
= 15;
192 int balloon_used
= 0;
193 CharDriverState
*vmchannel_hds
[MAX_VMCHANNEL_DEVICES
];
194 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
195 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
197 int win2k_install_hack
= 0;
200 static VLANState
*first_vlan
;
202 const char *vnc_display
;
203 #if defined(TARGET_SPARC)
205 #elif defined(TARGET_I386)
210 int acpi_enabled
= 1;
214 int graphic_rotate
= 0;
216 const char *incoming
;
217 const char *option_rom
[MAX_OPTION_ROMS
];
219 int semihosting_enabled
= 0;
221 int time_drift_fix
= 0;
222 unsigned int kvm_shadow_memory
= 0;
223 const char *cpu_vendor_string
;
227 const char *qemu_name
;
230 unsigned int nb_prom_envs
= 0;
231 const char *prom_envs
[MAX_PROM_ENVS
];
234 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
236 /***********************************************************/
237 /* x86 ISA bus support */
239 target_phys_addr_t isa_mem_base
= 0;
242 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
244 #ifdef DEBUG_UNUSED_IOPORT
245 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
250 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
252 #ifdef DEBUG_UNUSED_IOPORT
253 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
257 /* default is to make two byte accesses */
258 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
261 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
262 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
263 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
267 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
269 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
270 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
271 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
274 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
276 #ifdef DEBUG_UNUSED_IOPORT
277 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
282 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
284 #ifdef DEBUG_UNUSED_IOPORT
285 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
289 void init_ioports(void)
293 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
294 ioport_read_table
[0][i
] = default_ioport_readb
;
295 ioport_write_table
[0][i
] = default_ioport_writeb
;
296 ioport_read_table
[1][i
] = default_ioport_readw
;
297 ioport_write_table
[1][i
] = default_ioport_writew
;
298 ioport_read_table
[2][i
] = default_ioport_readl
;
299 ioport_write_table
[2][i
] = default_ioport_writel
;
303 /* size is the word size in byte */
304 int register_ioport_read(int start
, int length
, int size
,
305 IOPortReadFunc
*func
, void *opaque
)
311 } else if (size
== 2) {
313 } else if (size
== 4) {
316 hw_error("register_ioport_read: invalid size");
319 for(i
= start
; i
< start
+ length
; i
+= size
) {
320 ioport_read_table
[bsize
][i
] = func
;
321 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
322 hw_error("register_ioport_read: invalid opaque");
323 ioport_opaque
[i
] = opaque
;
328 /* size is the word size in byte */
329 int register_ioport_write(int start
, int length
, int size
,
330 IOPortWriteFunc
*func
, void *opaque
)
336 } else if (size
== 2) {
338 } else if (size
== 4) {
341 hw_error("register_ioport_write: invalid size");
344 for(i
= start
; i
< start
+ length
; i
+= size
) {
345 ioport_write_table
[bsize
][i
] = func
;
346 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
347 hw_error("register_ioport_write: invalid opaque");
348 ioport_opaque
[i
] = opaque
;
353 void isa_unassign_ioport(int start
, int length
)
357 for(i
= start
; i
< start
+ length
; i
++) {
358 ioport_read_table
[0][i
] = default_ioport_readb
;
359 ioport_read_table
[1][i
] = default_ioport_readw
;
360 ioport_read_table
[2][i
] = default_ioport_readl
;
362 ioport_write_table
[0][i
] = default_ioport_writeb
;
363 ioport_write_table
[1][i
] = default_ioport_writew
;
364 ioport_write_table
[2][i
] = default_ioport_writel
;
368 /***********************************************************/
370 void cpu_outb(CPUState
*env
, int addr
, int val
)
373 if (loglevel
& CPU_LOG_IOPORT
)
374 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
376 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
379 env
->last_io_time
= cpu_get_time_fast();
383 void cpu_outw(CPUState
*env
, int addr
, int val
)
386 if (loglevel
& CPU_LOG_IOPORT
)
387 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
389 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
392 env
->last_io_time
= cpu_get_time_fast();
396 void cpu_outl(CPUState
*env
, int addr
, int val
)
399 if (loglevel
& CPU_LOG_IOPORT
)
400 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
402 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
405 env
->last_io_time
= cpu_get_time_fast();
409 int cpu_inb(CPUState
*env
, int addr
)
412 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
414 if (loglevel
& CPU_LOG_IOPORT
)
415 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
419 env
->last_io_time
= cpu_get_time_fast();
424 int cpu_inw(CPUState
*env
, int addr
)
427 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
429 if (loglevel
& CPU_LOG_IOPORT
)
430 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
434 env
->last_io_time
= cpu_get_time_fast();
439 int cpu_inl(CPUState
*env
, int addr
)
442 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
444 if (loglevel
& CPU_LOG_IOPORT
)
445 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
449 env
->last_io_time
= cpu_get_time_fast();
454 /***********************************************************/
455 void hw_error(const char *fmt
, ...)
461 fprintf(stderr
, "qemu: hardware error: ");
462 vfprintf(stderr
, fmt
, ap
);
463 fprintf(stderr
, "\n");
464 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
465 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
467 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
469 cpu_dump_state(env
, stderr
, fprintf
, 0);
476 /***********************************************************/
479 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
480 static void *qemu_put_kbd_event_opaque
;
481 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
482 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
484 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
486 qemu_put_kbd_event_opaque
= opaque
;
487 qemu_put_kbd_event
= func
;
490 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
491 void *opaque
, int absolute
,
494 QEMUPutMouseEntry
*s
, *cursor
;
496 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
500 s
->qemu_put_mouse_event
= func
;
501 s
->qemu_put_mouse_event_opaque
= opaque
;
502 s
->qemu_put_mouse_event_absolute
= absolute
;
503 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
506 if (!qemu_put_mouse_event_head
) {
507 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
511 cursor
= qemu_put_mouse_event_head
;
512 while (cursor
->next
!= NULL
)
513 cursor
= cursor
->next
;
516 qemu_put_mouse_event_current
= s
;
521 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
523 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
525 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
528 cursor
= qemu_put_mouse_event_head
;
529 while (cursor
!= NULL
&& cursor
!= entry
) {
531 cursor
= cursor
->next
;
534 if (cursor
== NULL
) // does not exist or list empty
536 else if (prev
== NULL
) { // entry is head
537 qemu_put_mouse_event_head
= cursor
->next
;
538 if (qemu_put_mouse_event_current
== entry
)
539 qemu_put_mouse_event_current
= cursor
->next
;
540 qemu_free(entry
->qemu_put_mouse_event_name
);
545 prev
->next
= entry
->next
;
547 if (qemu_put_mouse_event_current
== entry
)
548 qemu_put_mouse_event_current
= prev
;
550 qemu_free(entry
->qemu_put_mouse_event_name
);
554 void kbd_put_keycode(int keycode
)
556 if (qemu_put_kbd_event
) {
557 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
561 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
563 QEMUPutMouseEvent
*mouse_event
;
564 void *mouse_event_opaque
;
567 if (!qemu_put_mouse_event_current
) {
572 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
574 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
577 if (graphic_rotate
) {
578 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
581 width
= graphic_width
;
582 mouse_event(mouse_event_opaque
,
583 width
- dy
, dx
, dz
, buttons_state
);
585 mouse_event(mouse_event_opaque
,
586 dx
, dy
, dz
, buttons_state
);
590 int kbd_mouse_is_absolute(void)
592 if (!qemu_put_mouse_event_current
)
595 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
598 void do_info_mice(void)
600 QEMUPutMouseEntry
*cursor
;
603 if (!qemu_put_mouse_event_head
) {
604 term_printf("No mouse devices connected\n");
608 term_printf("Mouse devices available:\n");
609 cursor
= qemu_put_mouse_event_head
;
610 while (cursor
!= NULL
) {
611 term_printf("%c Mouse #%d: %s\n",
612 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
613 index
, cursor
->qemu_put_mouse_event_name
);
615 cursor
= cursor
->next
;
619 void do_mouse_set(int index
)
621 QEMUPutMouseEntry
*cursor
;
624 if (!qemu_put_mouse_event_head
) {
625 term_printf("No mouse devices connected\n");
629 cursor
= qemu_put_mouse_event_head
;
630 while (cursor
!= NULL
&& index
!= i
) {
632 cursor
= cursor
->next
;
636 qemu_put_mouse_event_current
= cursor
;
638 term_printf("Mouse at given index not found\n");
641 /* compute with 96 bit intermediate result: (a*b)/c */
642 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
647 #ifdef WORDS_BIGENDIAN
657 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
658 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
661 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
665 /***********************************************************/
666 /* real time host monotonic timer */
668 #define QEMU_TIMER_BASE 1000000000LL
672 static int64_t clock_freq
;
674 static void init_get_clock(void)
678 ret
= QueryPerformanceFrequency(&freq
);
680 fprintf(stderr
, "Could not calibrate ticks\n");
683 clock_freq
= freq
.QuadPart
;
686 static int64_t get_clock(void)
689 QueryPerformanceCounter(&ti
);
690 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
695 static int use_rt_clock
;
697 static void init_get_clock(void)
700 #if defined(__linux__)
703 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
710 static int64_t get_clock(void)
712 #if defined(__linux__)
715 clock_gettime(CLOCK_MONOTONIC
, &ts
);
716 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
720 /* XXX: using gettimeofday leads to problems if the date
721 changes, so it should be avoided. */
723 gettimeofday(&tv
, NULL
);
724 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
730 /***********************************************************/
731 /* guest cycle counter */
733 static int64_t cpu_ticks_prev
;
734 static int64_t cpu_ticks_offset
;
735 static int64_t cpu_clock_offset
;
736 static int cpu_ticks_enabled
;
738 /* return the host CPU cycle counter and handle stop/restart */
739 int64_t cpu_get_ticks(void)
741 if (!cpu_ticks_enabled
) {
742 return cpu_ticks_offset
;
745 ticks
= cpu_get_real_ticks();
746 if (cpu_ticks_prev
> ticks
) {
747 /* Note: non increasing ticks may happen if the host uses
749 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
751 cpu_ticks_prev
= ticks
;
752 return ticks
+ cpu_ticks_offset
;
756 /* return the host CPU monotonic timer and handle stop/restart */
757 static int64_t cpu_get_clock(void)
760 if (!cpu_ticks_enabled
) {
761 return cpu_clock_offset
;
764 return ti
+ cpu_clock_offset
;
768 /* enable cpu_get_ticks() */
769 void cpu_enable_ticks(void)
771 if (!cpu_ticks_enabled
) {
772 cpu_ticks_offset
-= cpu_get_real_ticks();
773 cpu_clock_offset
-= get_clock();
774 cpu_ticks_enabled
= 1;
778 /* disable cpu_get_ticks() : the clock is stopped. You must not call
779 cpu_get_ticks() after that. */
780 void cpu_disable_ticks(void)
782 if (cpu_ticks_enabled
) {
783 cpu_ticks_offset
= cpu_get_ticks();
784 cpu_clock_offset
= cpu_get_clock();
785 cpu_ticks_enabled
= 0;
789 /***********************************************************/
792 #define QEMU_TIMER_REALTIME 0
793 #define QEMU_TIMER_VIRTUAL 1
797 /* XXX: add frequency */
805 struct QEMUTimer
*next
;
808 struct qemu_alarm_timer
{
812 int (*start
)(struct qemu_alarm_timer
*t
);
813 void (*stop
)(struct qemu_alarm_timer
*t
);
814 void (*rearm
)(struct qemu_alarm_timer
*t
);
818 #define ALARM_FLAG_DYNTICKS 0x1
820 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
822 return t
->flags
& ALARM_FLAG_DYNTICKS
;
825 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
827 if (!alarm_has_dynticks(t
))
833 /* TODO: MIN_TIMER_REARM_US should be optimized */
834 #define MIN_TIMER_REARM_US 250
836 static struct qemu_alarm_timer
*alarm_timer
;
840 struct qemu_alarm_win32
{
844 } alarm_win32_data
= {0, NULL
, -1};
846 static int win32_start_timer(struct qemu_alarm_timer
*t
);
847 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
848 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
852 static int unix_start_timer(struct qemu_alarm_timer
*t
);
853 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
857 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
858 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
859 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
861 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
862 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
864 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
865 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
867 #endif /* __linux__ */
871 static struct qemu_alarm_timer alarm_timers
[] = {
874 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
875 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
876 /* HPET - if available - is preferred */
877 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
878 /* ...otherwise try RTC */
879 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
881 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
883 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
884 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
885 {"win32", 0, win32_start_timer
,
886 win32_stop_timer
, NULL
, &alarm_win32_data
},
891 static void show_available_alarms()
895 printf("Available alarm timers, in order of precedence:\n");
896 for (i
= 0; alarm_timers
[i
].name
; i
++)
897 printf("%s\n", alarm_timers
[i
].name
);
900 static void configure_alarms(char const *opt
)
904 int count
= (sizeof(alarm_timers
) / sizeof(*alarm_timers
)) - 1;
908 if (!strcmp(opt
, "help")) {
909 show_available_alarms();
915 /* Reorder the array */
916 name
= strtok(arg
, ",");
918 struct qemu_alarm_timer tmp
;
920 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
921 if (!strcmp(alarm_timers
[i
].name
, name
))
926 fprintf(stderr
, "Unknown clock %s\n", name
);
935 tmp
= alarm_timers
[i
];
936 alarm_timers
[i
] = alarm_timers
[cur
];
937 alarm_timers
[cur
] = tmp
;
941 name
= strtok(NULL
, ",");
947 /* Disable remaining timers */
948 for (i
= cur
; i
< count
; i
++)
949 alarm_timers
[i
].name
= NULL
;
953 show_available_alarms();
959 static QEMUTimer
*active_timers
[2];
961 QEMUClock
*qemu_new_clock(int type
)
964 clock
= qemu_mallocz(sizeof(QEMUClock
));
971 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
975 ts
= qemu_mallocz(sizeof(QEMUTimer
));
982 void qemu_free_timer(QEMUTimer
*ts
)
987 /* stop a timer, but do not dealloc it */
988 void qemu_del_timer(QEMUTimer
*ts
)
992 /* NOTE: this code must be signal safe because
993 qemu_timer_expired() can be called from a signal. */
994 pt
= &active_timers
[ts
->clock
->type
];
1006 qemu_rearm_alarm_timer(alarm_timer
);
1009 /* modify the current timer so that it will be fired when current_time
1010 >= expire_time. The corresponding callback will be called. */
1011 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1017 /* add the timer in the sorted list */
1018 /* NOTE: this code must be signal safe because
1019 qemu_timer_expired() can be called from a signal. */
1020 pt
= &active_timers
[ts
->clock
->type
];
1025 if (t
->expire_time
> expire_time
)
1029 ts
->expire_time
= expire_time
;
1034 int qemu_timer_pending(QEMUTimer
*ts
)
1037 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1044 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1048 return (timer_head
->expire_time
<= current_time
);
1051 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1057 if (!ts
|| ts
->expire_time
> current_time
)
1059 /* remove timer from the list before calling the callback */
1060 *ptimer_head
= ts
->next
;
1063 /* run the callback (the timer list can be modified) */
1066 qemu_rearm_alarm_timer(alarm_timer
);
1069 int64_t qemu_get_clock(QEMUClock
*clock
)
1071 switch(clock
->type
) {
1072 case QEMU_TIMER_REALTIME
:
1073 return get_clock() / 1000000;
1075 case QEMU_TIMER_VIRTUAL
:
1076 return cpu_get_clock();
1080 static void init_timers(void)
1083 ticks_per_sec
= QEMU_TIMER_BASE
;
1084 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1085 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1089 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1091 uint64_t expire_time
;
1093 if (qemu_timer_pending(ts
)) {
1094 expire_time
= ts
->expire_time
;
1098 qemu_put_be64(f
, expire_time
);
1101 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1103 uint64_t expire_time
;
1105 expire_time
= qemu_get_be64(f
);
1106 if (expire_time
!= -1) {
1107 qemu_mod_timer(ts
, expire_time
);
1113 static void timer_save(QEMUFile
*f
, void *opaque
)
1115 if (cpu_ticks_enabled
) {
1116 hw_error("cannot save state if virtual timers are running");
1118 qemu_put_be64s(f
, &cpu_ticks_offset
);
1119 qemu_put_be64s(f
, &ticks_per_sec
);
1120 qemu_put_be64s(f
, &cpu_clock_offset
);
1123 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1125 if (version_id
!= 1 && version_id
!= 2)
1127 if (cpu_ticks_enabled
) {
1130 qemu_get_be64s(f
, &cpu_ticks_offset
);
1131 qemu_get_be64s(f
, &ticks_per_sec
);
1132 if (version_id
== 2) {
1133 qemu_get_be64s(f
, &cpu_clock_offset
);
1139 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1140 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1142 static void host_alarm_handler(int host_signum
)
1146 #define DISP_FREQ 1000
1148 static int64_t delta_min
= INT64_MAX
;
1149 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1151 ti
= qemu_get_clock(vm_clock
);
1152 if (last_clock
!= 0) {
1153 delta
= ti
- last_clock
;
1154 if (delta
< delta_min
)
1156 if (delta
> delta_max
)
1159 if (++count
== DISP_FREQ
) {
1160 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1161 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1162 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1163 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1164 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1166 delta_min
= INT64_MAX
;
1175 alarm_has_dynticks(alarm_timer
) ||
1176 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1177 qemu_get_clock(vm_clock
)) ||
1178 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1179 qemu_get_clock(rt_clock
))) {
1181 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1182 SetEvent(data
->host_alarm
);
1184 CPUState
*env
= cpu_single_env
;
1186 /* stop the currently executing cpu because a timer occured */
1187 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1189 if (env
->kqemu_enabled
) {
1190 kqemu_cpu_interrupt(env
);
1197 static uint64_t qemu_next_deadline(void)
1199 int64_t nearest_delta_us
= UINT64_MAX
;
1202 if (active_timers
[QEMU_TIMER_REALTIME
])
1203 nearest_delta_us
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1204 qemu_get_clock(rt_clock
))*1000;
1206 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1208 vmdelta_us
= (active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1209 qemu_get_clock(vm_clock
)+999)/1000;
1210 if (vmdelta_us
< nearest_delta_us
)
1211 nearest_delta_us
= vmdelta_us
;
1214 /* Avoid arming the timer to negative, zero, or too low values */
1215 if (nearest_delta_us
<= MIN_TIMER_REARM_US
)
1216 nearest_delta_us
= MIN_TIMER_REARM_US
;
1218 return nearest_delta_us
;
1223 #if defined(__linux__)
1225 #define RTC_FREQ 1024
1227 static void enable_sigio_timer(int fd
)
1229 struct sigaction act
;
1232 sigfillset(&act
.sa_mask
);
1234 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1235 act
.sa_flags
|= SA_ONSTACK
;
1237 act
.sa_handler
= host_alarm_handler
;
1239 sigaction(SIGIO
, &act
, NULL
);
1240 fcntl(fd
, F_SETFL
, O_ASYNC
);
1241 fcntl(fd
, F_SETOWN
, getpid());
1244 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1246 struct hpet_info info
;
1249 fd
= open("/dev/hpet", O_RDONLY
);
1254 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1256 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1257 "error, but for better emulation accuracy type:\n"
1258 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1262 /* Check capabilities */
1263 r
= ioctl(fd
, HPET_INFO
, &info
);
1267 /* Enable periodic mode */
1268 r
= ioctl(fd
, HPET_EPI
, 0);
1269 if (info
.hi_flags
&& (r
< 0))
1272 /* Enable interrupt */
1273 r
= ioctl(fd
, HPET_IE_ON
, 0);
1277 enable_sigio_timer(fd
);
1278 t
->priv
= (void *)(long)fd
;
1286 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1288 int fd
= (long)t
->priv
;
1293 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1297 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1300 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1301 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1302 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1303 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1306 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1312 enable_sigio_timer(rtc_fd
);
1314 t
->priv
= (void *)(long)rtc_fd
;
1319 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1321 int rtc_fd
= (long)t
->priv
;
1326 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1330 struct sigaction act
;
1332 sigfillset(&act
.sa_mask
);
1334 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
1335 act
.sa_flags
|= SA_ONSTACK
;
1337 act
.sa_handler
= host_alarm_handler
;
1339 sigaction(SIGALRM
, &act
, NULL
);
1341 ev
.sigev_value
.sival_int
= 0;
1342 ev
.sigev_notify
= SIGEV_SIGNAL
;
1343 ev
.sigev_signo
= SIGALRM
;
1345 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1346 perror("timer_create");
1348 /* disable dynticks */
1349 fprintf(stderr
, "Dynamic Ticks disabled\n");
1354 t
->priv
= (void *)host_timer
;
1359 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1361 timer_t host_timer
= (timer_t
)t
->priv
;
1363 timer_delete(host_timer
);
1366 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1368 timer_t host_timer
= (timer_t
)t
->priv
;
1369 struct itimerspec timeout
;
1370 int64_t nearest_delta_us
= INT64_MAX
;
1373 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1374 !active_timers
[QEMU_TIMER_VIRTUAL
])
1377 nearest_delta_us
= qemu_next_deadline();
1379 /* check whether a timer is already running */
1380 if (timer_gettime(host_timer
, &timeout
)) {
1382 fprintf(stderr
, "Internal timer error: aborting\n");
1385 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1386 if (current_us
&& current_us
<= nearest_delta_us
)
1389 timeout
.it_interval
.tv_sec
= 0;
1390 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1391 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1392 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1393 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1395 fprintf(stderr
, "Internal timer error: aborting\n");
1400 #endif /* defined(__linux__) */
1402 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1404 struct sigaction act
;
1405 struct itimerval itv
;
1409 sigfillset(&act
.sa_mask
);
1411 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
1412 act
.sa_flags
|= SA_ONSTACK
;
1414 act
.sa_handler
= host_alarm_handler
;
1416 sigaction(SIGALRM
, &act
, NULL
);
1418 itv
.it_interval
.tv_sec
= 0;
1419 /* for i386 kernel 2.6 to get 1 ms */
1420 itv
.it_interval
.tv_usec
= 999;
1421 itv
.it_value
.tv_sec
= 0;
1422 itv
.it_value
.tv_usec
= 10 * 1000;
1424 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1431 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1433 struct itimerval itv
;
1435 memset(&itv
, 0, sizeof(itv
));
1436 setitimer(ITIMER_REAL
, &itv
, NULL
);
1439 #endif /* !defined(_WIN32) */
1443 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1446 struct qemu_alarm_win32
*data
= t
->priv
;
1449 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1450 if (!data
->host_alarm
) {
1451 perror("Failed CreateEvent");
1455 memset(&tc
, 0, sizeof(tc
));
1456 timeGetDevCaps(&tc
, sizeof(tc
));
1458 if (data
->period
< tc
.wPeriodMin
)
1459 data
->period
= tc
.wPeriodMin
;
1461 timeBeginPeriod(data
->period
);
1463 flags
= TIME_CALLBACK_FUNCTION
;
1464 if (alarm_has_dynticks(t
))
1465 flags
|= TIME_ONESHOT
;
1467 flags
|= TIME_PERIODIC
;
1469 data
->timerId
= timeSetEvent(1, // interval (ms)
1470 data
->period
, // resolution
1471 host_alarm_handler
, // function
1472 (DWORD
)t
, // parameter
1475 if (!data
->timerId
) {
1476 perror("Failed to initialize win32 alarm timer");
1478 timeEndPeriod(data
->period
);
1479 CloseHandle(data
->host_alarm
);
1483 qemu_add_wait_object(data
->host_alarm
, NULL
, NULL
);
1488 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1490 struct qemu_alarm_win32
*data
= t
->priv
;
1492 timeKillEvent(data
->timerId
);
1493 timeEndPeriod(data
->period
);
1495 CloseHandle(data
->host_alarm
);
1498 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1500 struct qemu_alarm_win32
*data
= t
->priv
;
1501 uint64_t nearest_delta_us
;
1503 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1504 !active_timers
[QEMU_TIMER_VIRTUAL
])
1507 nearest_delta_us
= qemu_next_deadline();
1508 nearest_delta_us
/= 1000;
1510 timeKillEvent(data
->timerId
);
1512 data
->timerId
= timeSetEvent(1,
1516 TIME_ONESHOT
| TIME_PERIODIC
);
1518 if (!data
->timerId
) {
1519 perror("Failed to re-arm win32 alarm timer");
1521 timeEndPeriod(data
->period
);
1522 CloseHandle(data
->host_alarm
);
1529 static void init_timer_alarm(void)
1531 struct qemu_alarm_timer
*t
;
1534 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1535 t
= &alarm_timers
[i
];
1543 fprintf(stderr
, "Unable to find any suitable alarm timer.\n");
1544 fprintf(stderr
, "Terminating\n");
1551 void quit_timers(void)
1553 alarm_timer
->stop(alarm_timer
);
1557 /***********************************************************/
1558 /* character device */
1560 static void qemu_chr_event(CharDriverState
*s
, int event
)
1564 s
->chr_event(s
->handler_opaque
, event
);
1567 static void qemu_chr_reset_bh(void *opaque
)
1569 CharDriverState
*s
= opaque
;
1570 qemu_chr_event(s
, CHR_EVENT_RESET
);
1571 qemu_bh_delete(s
->bh
);
1575 void qemu_chr_reset(CharDriverState
*s
)
1577 if (s
->bh
== NULL
) {
1578 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
1579 qemu_bh_schedule(s
->bh
);
1583 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1585 return s
->chr_write(s
, buf
, len
);
1588 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1592 return s
->chr_ioctl(s
, cmd
, arg
);
1595 int qemu_chr_can_read(CharDriverState
*s
)
1597 if (!s
->chr_can_read
)
1599 return s
->chr_can_read(s
->handler_opaque
);
1602 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
1604 s
->chr_read(s
->handler_opaque
, buf
, len
);
1608 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1613 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1614 qemu_chr_write(s
, buf
, strlen(buf
));
1618 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1620 if (s
->chr_send_event
)
1621 s
->chr_send_event(s
, event
);
1624 void qemu_chr_add_handlers(CharDriverState
*s
,
1625 IOCanRWHandler
*fd_can_read
,
1626 IOReadHandler
*fd_read
,
1627 IOEventHandler
*fd_event
,
1630 s
->chr_can_read
= fd_can_read
;
1631 s
->chr_read
= fd_read
;
1632 s
->chr_event
= fd_event
;
1633 s
->handler_opaque
= opaque
;
1634 if (s
->chr_update_read_handler
)
1635 s
->chr_update_read_handler(s
);
1638 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1643 static CharDriverState
*qemu_chr_open_null(void)
1645 CharDriverState
*chr
;
1647 chr
= qemu_mallocz(sizeof(CharDriverState
));
1650 chr
->chr_write
= null_chr_write
;
1654 /* MUX driver for serial I/O splitting */
1655 static int term_timestamps
;
1656 static int64_t term_timestamps_start
;
1659 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
1660 IOReadHandler
*chr_read
[MAX_MUX
];
1661 IOEventHandler
*chr_event
[MAX_MUX
];
1662 void *ext_opaque
[MAX_MUX
];
1663 CharDriverState
*drv
;
1665 int term_got_escape
;
1670 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1672 MuxDriver
*d
= chr
->opaque
;
1674 if (!term_timestamps
) {
1675 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
1680 for(i
= 0; i
< len
; i
++) {
1681 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
1682 if (buf
[i
] == '\n') {
1688 if (term_timestamps_start
== -1)
1689 term_timestamps_start
= ti
;
1690 ti
-= term_timestamps_start
;
1691 secs
= ti
/ 1000000000;
1692 snprintf(buf1
, sizeof(buf1
),
1693 "[%02d:%02d:%02d.%03d] ",
1697 (int)((ti
/ 1000000) % 1000));
1698 d
->drv
->chr_write(d
->drv
, buf1
, strlen(buf1
));
1705 static char *mux_help
[] = {
1706 "% h print this help\n\r",
1707 "% x exit emulator\n\r",
1708 "% s save disk data back to file (if -snapshot)\n\r",
1709 "% t toggle console timestamps\n\r"
1710 "% b send break (magic sysrq)\n\r",
1711 "% c switch between console and monitor\n\r",
1716 static int term_escape_char
= 0x01; /* ctrl-a is used for escape */
1717 static void mux_print_help(CharDriverState
*chr
)
1720 char ebuf
[15] = "Escape-Char";
1721 char cbuf
[50] = "\n\r";
1723 if (term_escape_char
> 0 && term_escape_char
< 26) {
1724 sprintf(cbuf
,"\n\r");
1725 sprintf(ebuf
,"C-%c", term_escape_char
- 1 + 'a');
1727 sprintf(cbuf
,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char
);
1729 chr
->chr_write(chr
, cbuf
, strlen(cbuf
));
1730 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
1731 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
1732 if (mux_help
[i
][j
] == '%')
1733 chr
->chr_write(chr
, ebuf
, strlen(ebuf
));
1735 chr
->chr_write(chr
, &mux_help
[i
][j
], 1);
1740 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
1742 if (d
->term_got_escape
) {
1743 d
->term_got_escape
= 0;
1744 if (ch
== term_escape_char
)
1749 mux_print_help(chr
);
1753 char *term
= "QEMU: Terminated\n\r";
1754 chr
->chr_write(chr
,term
,strlen(term
));
1761 for (i
= 0; i
< MAX_DISKS
; i
++) {
1763 bdrv_commit(bs_table
[i
]);
1766 bdrv_commit(mtd_bdrv
);
1770 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1773 /* Switch to the next registered device */
1775 if (chr
->focus
>= d
->mux_cnt
)
1779 term_timestamps
= !term_timestamps
;
1780 term_timestamps_start
= -1;
1783 } else if (ch
== term_escape_char
) {
1784 d
->term_got_escape
= 1;
1792 static int mux_chr_can_read(void *opaque
)
1794 CharDriverState
*chr
= opaque
;
1795 MuxDriver
*d
= chr
->opaque
;
1796 if (d
->chr_can_read
[chr
->focus
])
1797 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
1801 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
1803 CharDriverState
*chr
= opaque
;
1804 MuxDriver
*d
= chr
->opaque
;
1806 for(i
= 0; i
< size
; i
++)
1807 if (mux_proc_byte(chr
, d
, buf
[i
]))
1808 d
->chr_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
], &buf
[i
], 1);
1811 static void mux_chr_event(void *opaque
, int event
)
1813 CharDriverState
*chr
= opaque
;
1814 MuxDriver
*d
= chr
->opaque
;
1817 /* Send the event to all registered listeners */
1818 for (i
= 0; i
< d
->mux_cnt
; i
++)
1819 if (d
->chr_event
[i
])
1820 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
1823 static void mux_chr_update_read_handler(CharDriverState
*chr
)
1825 MuxDriver
*d
= chr
->opaque
;
1827 if (d
->mux_cnt
>= MAX_MUX
) {
1828 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
1831 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
1832 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
1833 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
1834 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
1835 /* Fix up the real driver with mux routines */
1836 if (d
->mux_cnt
== 0) {
1837 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
1838 mux_chr_event
, chr
);
1840 chr
->focus
= d
->mux_cnt
;
1844 CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
1846 CharDriverState
*chr
;
1849 chr
= qemu_mallocz(sizeof(CharDriverState
));
1852 d
= qemu_mallocz(sizeof(MuxDriver
));
1861 chr
->chr_write
= mux_chr_write
;
1862 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
1869 static void socket_cleanup(void)
1874 static int socket_init(void)
1879 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1881 err
= WSAGetLastError();
1882 fprintf(stderr
, "WSAStartup: %d\n", err
);
1885 atexit(socket_cleanup
);
1889 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1895 ret
= send(fd
, buf
, len
, 0);
1898 errno
= WSAGetLastError();
1899 if (errno
!= WSAEWOULDBLOCK
) {
1902 } else if (ret
== 0) {
1912 void socket_set_nonblock(int fd
)
1914 unsigned long opt
= 1;
1915 ioctlsocket(fd
, FIONBIO
, &opt
);
1920 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1926 ret
= write(fd
, buf
, len
);
1928 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1930 } else if (ret
== 0) {
1940 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1942 return unix_write(fd
, buf
, len1
);
1945 void socket_set_nonblock(int fd
)
1947 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1949 #endif /* !_WIN32 */
1958 #define STDIO_MAX_CLIENTS 1
1959 static int stdio_nb_clients
= 0;
1961 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1963 FDCharDriver
*s
= chr
->opaque
;
1964 return unix_write(s
->fd_out
, buf
, len
);
1967 static int fd_chr_read_poll(void *opaque
)
1969 CharDriverState
*chr
= opaque
;
1970 FDCharDriver
*s
= chr
->opaque
;
1972 s
->max_size
= qemu_chr_can_read(chr
);
1976 static void fd_chr_read(void *opaque
)
1978 CharDriverState
*chr
= opaque
;
1979 FDCharDriver
*s
= chr
->opaque
;
1984 if (len
> s
->max_size
)
1988 size
= read(s
->fd_in
, buf
, len
);
1990 /* FD has been closed. Remove it from the active list. */
1991 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1995 qemu_chr_read(chr
, buf
, size
);
1999 static void fd_chr_update_read_handler(CharDriverState
*chr
)
2001 FDCharDriver
*s
= chr
->opaque
;
2003 if (s
->fd_in
>= 0) {
2004 if (nographic
&& s
->fd_in
== 0) {
2006 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
2007 fd_chr_read
, NULL
, chr
);
2012 /* open a character device to a unix fd */
2013 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
2015 CharDriverState
*chr
;
2018 chr
= qemu_mallocz(sizeof(CharDriverState
));
2021 s
= qemu_mallocz(sizeof(FDCharDriver
));
2029 chr
->chr_write
= fd_chr_write
;
2030 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
2032 qemu_chr_reset(chr
);
2037 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
2041 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
2044 return qemu_chr_open_fd(-1, fd_out
);
2047 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
2050 char filename_in
[256], filename_out
[256];
2052 snprintf(filename_in
, 256, "%s.in", filename
);
2053 snprintf(filename_out
, 256, "%s.out", filename
);
2054 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
2055 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
2056 if (fd_in
< 0 || fd_out
< 0) {
2061 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
2065 return qemu_chr_open_fd(fd_in
, fd_out
);
2069 /* for STDIO, we handle the case where several clients use it
2072 #define TERM_FIFO_MAX_SIZE 1
2074 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
2075 static int term_fifo_size
;
2077 static int stdio_read_poll(void *opaque
)
2079 CharDriverState
*chr
= opaque
;
2081 /* try to flush the queue if needed */
2082 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
2083 qemu_chr_read(chr
, term_fifo
, 1);
2086 /* see if we can absorb more chars */
2087 if (term_fifo_size
== 0)
2093 static void stdio_read(void *opaque
)
2097 CharDriverState
*chr
= opaque
;
2099 size
= read(0, buf
, 1);
2101 /* stdin has been closed. Remove it from the active list. */
2102 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
2106 if (qemu_chr_can_read(chr
) > 0) {
2107 qemu_chr_read(chr
, buf
, 1);
2108 } else if (term_fifo_size
== 0) {
2109 term_fifo
[term_fifo_size
++] = buf
[0];
2114 /* init terminal so that we can grab keys */
2115 static struct termios oldtty
;
2116 static int old_fd0_flags
;
2118 static void term_exit(void)
2120 tcsetattr (0, TCSANOW
, &oldtty
);
2121 fcntl(0, F_SETFL
, old_fd0_flags
);
2124 static void term_init(void)
2128 tcgetattr (0, &tty
);
2130 old_fd0_flags
= fcntl(0, F_GETFL
);
2132 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
2133 |INLCR
|IGNCR
|ICRNL
|IXON
);
2134 tty
.c_oflag
|= OPOST
;
2135 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
2136 /* if graphical mode, we allow Ctrl-C handling */
2138 tty
.c_lflag
&= ~ISIG
;
2139 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
2142 tty
.c_cc
[VTIME
] = 0;
2144 tcsetattr (0, TCSANOW
, &tty
);
2148 fcntl(0, F_SETFL
, O_NONBLOCK
);
2151 static CharDriverState
*qemu_chr_open_stdio(void)
2153 CharDriverState
*chr
;
2155 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
2157 chr
= qemu_chr_open_fd(0, 1);
2158 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
2165 #if defined(__linux__) || defined(__sun__)
2166 static CharDriverState
*qemu_chr_open_pty(void)
2169 char slave_name
[1024];
2170 int master_fd
, slave_fd
;
2172 #if defined(__linux__)
2173 /* Not satisfying */
2174 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
2179 /* Disabling local echo and line-buffered output */
2180 tcgetattr (master_fd
, &tty
);
2181 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
2183 tty
.c_cc
[VTIME
] = 0;
2184 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
2186 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
2187 return qemu_chr_open_fd(master_fd
, master_fd
);
2190 static void tty_serial_init(int fd
, int speed
,
2191 int parity
, int data_bits
, int stop_bits
)
2197 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
2198 speed
, parity
, data_bits
, stop_bits
);
2200 tcgetattr (fd
, &tty
);
2242 cfsetispeed(&tty
, spd
);
2243 cfsetospeed(&tty
, spd
);
2245 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
2246 |INLCR
|IGNCR
|ICRNL
|IXON
);
2247 tty
.c_oflag
|= OPOST
;
2248 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
2249 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
2270 tty
.c_cflag
|= PARENB
;
2273 tty
.c_cflag
|= PARENB
| PARODD
;
2277 tty
.c_cflag
|= CSTOPB
;
2279 tcsetattr (fd
, TCSANOW
, &tty
);
2282 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2284 FDCharDriver
*s
= chr
->opaque
;
2287 case CHR_IOCTL_SERIAL_SET_PARAMS
:
2289 QEMUSerialSetParams
*ssp
= arg
;
2290 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
2291 ssp
->data_bits
, ssp
->stop_bits
);
2294 case CHR_IOCTL_SERIAL_SET_BREAK
:
2296 int enable
= *(int *)arg
;
2298 tcsendbreak(s
->fd_in
, 1);
2307 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
2309 CharDriverState
*chr
;
2312 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
2313 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2314 tty_serial_init(fd
, 115200, 'N', 8, 1);
2315 chr
= qemu_chr_open_fd(fd
, fd
);
2320 chr
->chr_ioctl
= tty_serial_ioctl
;
2321 qemu_chr_reset(chr
);
2324 #else /* ! __linux__ && ! __sun__ */
2325 static CharDriverState
*qemu_chr_open_pty(void)
2329 #endif /* __linux__ || __sun__ */
2331 #if defined(__linux__)
2335 } ParallelCharDriver
;
2337 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
2339 if (s
->mode
!= mode
) {
2341 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
2348 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2350 ParallelCharDriver
*drv
= chr
->opaque
;
2355 case CHR_IOCTL_PP_READ_DATA
:
2356 if (ioctl(fd
, PPRDATA
, &b
) < 0)
2358 *(uint8_t *)arg
= b
;
2360 case CHR_IOCTL_PP_WRITE_DATA
:
2361 b
= *(uint8_t *)arg
;
2362 if (ioctl(fd
, PPWDATA
, &b
) < 0)
2365 case CHR_IOCTL_PP_READ_CONTROL
:
2366 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
2368 /* Linux gives only the lowest bits, and no way to know data
2369 direction! For better compatibility set the fixed upper
2371 *(uint8_t *)arg
= b
| 0xc0;
2373 case CHR_IOCTL_PP_WRITE_CONTROL
:
2374 b
= *(uint8_t *)arg
;
2375 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
2378 case CHR_IOCTL_PP_READ_STATUS
:
2379 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
2381 *(uint8_t *)arg
= b
;
2383 case CHR_IOCTL_PP_EPP_READ_ADDR
:
2384 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2385 struct ParallelIOArg
*parg
= arg
;
2386 int n
= read(fd
, parg
->buffer
, parg
->count
);
2387 if (n
!= parg
->count
) {
2392 case CHR_IOCTL_PP_EPP_READ
:
2393 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2394 struct ParallelIOArg
*parg
= arg
;
2395 int n
= read(fd
, parg
->buffer
, parg
->count
);
2396 if (n
!= parg
->count
) {
2401 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
2402 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2403 struct ParallelIOArg
*parg
= arg
;
2404 int n
= write(fd
, parg
->buffer
, parg
->count
);
2405 if (n
!= parg
->count
) {
2410 case CHR_IOCTL_PP_EPP_WRITE
:
2411 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2412 struct ParallelIOArg
*parg
= arg
;
2413 int n
= write(fd
, parg
->buffer
, parg
->count
);
2414 if (n
!= parg
->count
) {
2425 static void pp_close(CharDriverState
*chr
)
2427 ParallelCharDriver
*drv
= chr
->opaque
;
2430 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
2431 ioctl(fd
, PPRELEASE
);
2436 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
2438 CharDriverState
*chr
;
2439 ParallelCharDriver
*drv
;
2442 TFR(fd
= open(filename
, O_RDWR
));
2446 if (ioctl(fd
, PPCLAIM
) < 0) {
2451 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
2457 drv
->mode
= IEEE1284_MODE_COMPAT
;
2459 chr
= qemu_mallocz(sizeof(CharDriverState
));
2465 chr
->chr_write
= null_chr_write
;
2466 chr
->chr_ioctl
= pp_ioctl
;
2467 chr
->chr_close
= pp_close
;
2470 qemu_chr_reset(chr
);
2474 #endif /* __linux__ */
2480 HANDLE hcom
, hrecv
, hsend
;
2481 OVERLAPPED orecv
, osend
;
2486 #define NSENDBUF 2048
2487 #define NRECVBUF 2048
2488 #define MAXCONNECT 1
2489 #define NTIMEOUT 5000
2491 static int win_chr_poll(void *opaque
);
2492 static int win_chr_pipe_poll(void *opaque
);
2494 static void win_chr_close(CharDriverState
*chr
)
2496 WinCharState
*s
= chr
->opaque
;
2499 CloseHandle(s
->hsend
);
2503 CloseHandle(s
->hrecv
);
2507 CloseHandle(s
->hcom
);
2511 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
2513 qemu_del_polling_cb(win_chr_poll
, chr
);
2516 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
2518 WinCharState
*s
= chr
->opaque
;
2520 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2525 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2527 fprintf(stderr
, "Failed CreateEvent\n");
2530 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2532 fprintf(stderr
, "Failed CreateEvent\n");
2536 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2537 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2538 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2539 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
2544 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2545 fprintf(stderr
, "Failed SetupComm\n");
2549 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2550 size
= sizeof(COMMCONFIG
);
2551 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2552 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2553 CommConfigDialog(filename
, NULL
, &comcfg
);
2555 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2556 fprintf(stderr
, "Failed SetCommState\n");
2560 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2561 fprintf(stderr
, "Failed SetCommMask\n");
2565 cto
.ReadIntervalTimeout
= MAXDWORD
;
2566 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2567 fprintf(stderr
, "Failed SetCommTimeouts\n");
2571 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2572 fprintf(stderr
, "Failed ClearCommError\n");
2575 qemu_add_polling_cb(win_chr_poll
, chr
);
2583 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2585 WinCharState
*s
= chr
->opaque
;
2586 DWORD len
, ret
, size
, err
;
2589 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2590 s
->osend
.hEvent
= s
->hsend
;
2593 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2595 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2597 err
= GetLastError();
2598 if (err
== ERROR_IO_PENDING
) {
2599 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2617 static int win_chr_read_poll(CharDriverState
*chr
)
2619 WinCharState
*s
= chr
->opaque
;
2621 s
->max_size
= qemu_chr_can_read(chr
);
2625 static void win_chr_readfile(CharDriverState
*chr
)
2627 WinCharState
*s
= chr
->opaque
;
2632 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2633 s
->orecv
.hEvent
= s
->hrecv
;
2634 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2636 err
= GetLastError();
2637 if (err
== ERROR_IO_PENDING
) {
2638 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2643 qemu_chr_read(chr
, buf
, size
);
2647 static void win_chr_read(CharDriverState
*chr
)
2649 WinCharState
*s
= chr
->opaque
;
2651 if (s
->len
> s
->max_size
)
2652 s
->len
= s
->max_size
;
2656 win_chr_readfile(chr
);
2659 static int win_chr_poll(void *opaque
)
2661 CharDriverState
*chr
= opaque
;
2662 WinCharState
*s
= chr
->opaque
;
2666 ClearCommError(s
->hcom
, &comerr
, &status
);
2667 if (status
.cbInQue
> 0) {
2668 s
->len
= status
.cbInQue
;
2669 win_chr_read_poll(chr
);
2676 static CharDriverState
*qemu_chr_open_win(const char *filename
)
2678 CharDriverState
*chr
;
2681 chr
= qemu_mallocz(sizeof(CharDriverState
));
2684 s
= qemu_mallocz(sizeof(WinCharState
));
2690 chr
->chr_write
= win_chr_write
;
2691 chr
->chr_close
= win_chr_close
;
2693 if (win_chr_init(chr
, filename
) < 0) {
2698 qemu_chr_reset(chr
);
2702 static int win_chr_pipe_poll(void *opaque
)
2704 CharDriverState
*chr
= opaque
;
2705 WinCharState
*s
= chr
->opaque
;
2708 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2711 win_chr_read_poll(chr
);
2718 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
2720 WinCharState
*s
= chr
->opaque
;
2728 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2730 fprintf(stderr
, "Failed CreateEvent\n");
2733 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2735 fprintf(stderr
, "Failed CreateEvent\n");
2739 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2740 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2741 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2743 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2744 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2745 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2750 ZeroMemory(&ov
, sizeof(ov
));
2751 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2752 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2754 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2758 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2760 fprintf(stderr
, "Failed GetOverlappedResult\n");
2762 CloseHandle(ov
.hEvent
);
2769 CloseHandle(ov
.hEvent
);
2772 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2781 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2783 CharDriverState
*chr
;
2786 chr
= qemu_mallocz(sizeof(CharDriverState
));
2789 s
= qemu_mallocz(sizeof(WinCharState
));
2795 chr
->chr_write
= win_chr_write
;
2796 chr
->chr_close
= win_chr_close
;
2798 if (win_chr_pipe_init(chr
, filename
) < 0) {
2803 qemu_chr_reset(chr
);
2807 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2809 CharDriverState
*chr
;
2812 chr
= qemu_mallocz(sizeof(CharDriverState
));
2815 s
= qemu_mallocz(sizeof(WinCharState
));
2822 chr
->chr_write
= win_chr_write
;
2823 qemu_chr_reset(chr
);
2827 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
2829 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2832 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2836 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2837 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2838 if (fd_out
== INVALID_HANDLE_VALUE
)
2841 return qemu_chr_open_win_file(fd_out
);
2843 #endif /* !_WIN32 */
2845 /***********************************************************/
2846 /* UDP Net console */
2850 struct sockaddr_in daddr
;
2857 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2859 NetCharDriver
*s
= chr
->opaque
;
2861 return sendto(s
->fd
, buf
, len
, 0,
2862 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2865 static int udp_chr_read_poll(void *opaque
)
2867 CharDriverState
*chr
= opaque
;
2868 NetCharDriver
*s
= chr
->opaque
;
2870 s
->max_size
= qemu_chr_can_read(chr
);
2872 /* If there were any stray characters in the queue process them
2875 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2876 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2878 s
->max_size
= qemu_chr_can_read(chr
);
2883 static void udp_chr_read(void *opaque
)
2885 CharDriverState
*chr
= opaque
;
2886 NetCharDriver
*s
= chr
->opaque
;
2888 if (s
->max_size
== 0)
2890 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2891 s
->bufptr
= s
->bufcnt
;
2896 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2897 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2899 s
->max_size
= qemu_chr_can_read(chr
);
2903 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2905 NetCharDriver
*s
= chr
->opaque
;
2908 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2909 udp_chr_read
, NULL
, chr
);
2914 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2916 int parse_host_src_port(struct sockaddr_in
*haddr
,
2917 struct sockaddr_in
*saddr
,
2920 static CharDriverState
*qemu_chr_open_udp(const char *def
)
2922 CharDriverState
*chr
= NULL
;
2923 NetCharDriver
*s
= NULL
;
2925 struct sockaddr_in saddr
;
2927 chr
= qemu_mallocz(sizeof(CharDriverState
));
2930 s
= qemu_mallocz(sizeof(NetCharDriver
));
2934 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2936 perror("socket(PF_INET, SOCK_DGRAM)");
2940 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2941 printf("Could not parse: %s\n", def
);
2945 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2955 chr
->chr_write
= udp_chr_write
;
2956 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2969 /***********************************************************/
2970 /* TCP Net console */
2981 static void tcp_chr_accept(void *opaque
);
2983 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2985 TCPCharDriver
*s
= chr
->opaque
;
2987 return send_all(s
->fd
, buf
, len
);
2989 /* XXX: indicate an error ? */
2994 static int tcp_chr_read_poll(void *opaque
)
2996 CharDriverState
*chr
= opaque
;
2997 TCPCharDriver
*s
= chr
->opaque
;
3000 s
->max_size
= qemu_chr_can_read(chr
);
3005 #define IAC_BREAK 243
3006 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
3008 char *buf
, int *size
)
3010 /* Handle any telnet client's basic IAC options to satisfy char by
3011 * char mode with no echo. All IAC options will be removed from
3012 * the buf and the do_telnetopt variable will be used to track the
3013 * state of the width of the IAC information.
3015 * IAC commands come in sets of 3 bytes with the exception of the
3016 * "IAC BREAK" command and the double IAC.
3022 for (i
= 0; i
< *size
; i
++) {
3023 if (s
->do_telnetopt
> 1) {
3024 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
3025 /* Double IAC means send an IAC */
3029 s
->do_telnetopt
= 1;
3031 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
3032 /* Handle IAC break commands by sending a serial break */
3033 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
3038 if (s
->do_telnetopt
>= 4) {
3039 s
->do_telnetopt
= 1;
3042 if ((unsigned char)buf
[i
] == IAC
) {
3043 s
->do_telnetopt
= 2;
3054 static void tcp_chr_read(void *opaque
)
3056 CharDriverState
*chr
= opaque
;
3057 TCPCharDriver
*s
= chr
->opaque
;
3061 if (!s
->connected
|| s
->max_size
<= 0)
3064 if (len
> s
->max_size
)
3066 size
= recv(s
->fd
, buf
, len
, 0);
3068 /* connection closed */
3070 if (s
->listen_fd
>= 0) {
3071 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
3073 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3076 } else if (size
> 0) {
3077 if (s
->do_telnetopt
)
3078 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
3080 qemu_chr_read(chr
, buf
, size
);
3084 static void tcp_chr_connect(void *opaque
)
3086 CharDriverState
*chr
= opaque
;
3087 TCPCharDriver
*s
= chr
->opaque
;
3090 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
3091 tcp_chr_read
, NULL
, chr
);
3092 qemu_chr_reset(chr
);
3095 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3096 static void tcp_chr_telnet_init(int fd
)
3099 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3100 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3101 send(fd
, (char *)buf
, 3, 0);
3102 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3103 send(fd
, (char *)buf
, 3, 0);
3104 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3105 send(fd
, (char *)buf
, 3, 0);
3106 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3107 send(fd
, (char *)buf
, 3, 0);
3110 static void socket_set_nodelay(int fd
)
3113 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
3116 static void tcp_chr_accept(void *opaque
)
3118 CharDriverState
*chr
= opaque
;
3119 TCPCharDriver
*s
= chr
->opaque
;
3120 struct sockaddr_in saddr
;
3122 struct sockaddr_un uaddr
;
3124 struct sockaddr
*addr
;
3131 len
= sizeof(uaddr
);
3132 addr
= (struct sockaddr
*)&uaddr
;
3136 len
= sizeof(saddr
);
3137 addr
= (struct sockaddr
*)&saddr
;
3139 fd
= accept(s
->listen_fd
, addr
, &len
);
3140 if (fd
< 0 && errno
!= EINTR
) {
3142 } else if (fd
>= 0) {
3143 if (s
->do_telnetopt
)
3144 tcp_chr_telnet_init(fd
);
3148 socket_set_nonblock(fd
);
3150 socket_set_nodelay(fd
);
3152 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
3153 tcp_chr_connect(chr
);
3156 static void tcp_chr_close(CharDriverState
*chr
)
3158 TCPCharDriver
*s
= chr
->opaque
;
3161 if (s
->listen_fd
>= 0)
3162 closesocket(s
->listen_fd
);
3166 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
3170 CharDriverState
*chr
= NULL
;
3171 TCPCharDriver
*s
= NULL
;
3172 int fd
= -1, ret
, err
, val
;
3174 int is_waitconnect
= 1;
3177 struct sockaddr_in saddr
;
3179 struct sockaddr_un uaddr
;
3181 struct sockaddr
*addr
;
3186 addr
= (struct sockaddr
*)&uaddr
;
3187 addrlen
= sizeof(uaddr
);
3188 if (parse_unix_path(&uaddr
, host_str
) < 0)
3193 addr
= (struct sockaddr
*)&saddr
;
3194 addrlen
= sizeof(saddr
);
3195 if (parse_host_port(&saddr
, host_str
) < 0)
3200 while((ptr
= strchr(ptr
,','))) {
3202 if (!strncmp(ptr
,"server",6)) {
3204 } else if (!strncmp(ptr
,"nowait",6)) {
3206 } else if (!strncmp(ptr
,"nodelay",6)) {
3209 printf("Unknown option: %s\n", ptr
);
3216 chr
= qemu_mallocz(sizeof(CharDriverState
));
3219 s
= qemu_mallocz(sizeof(TCPCharDriver
));
3225 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
3228 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3233 if (!is_waitconnect
)
3234 socket_set_nonblock(fd
);
3239 s
->is_unix
= is_unix
;
3240 s
->do_nodelay
= do_nodelay
&& !is_unix
;
3243 chr
->chr_write
= tcp_chr_write
;
3244 chr
->chr_close
= tcp_chr_close
;
3247 /* allow fast reuse */
3251 strncpy(path
, uaddr
.sun_path
, 108);
3258 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3261 ret
= bind(fd
, addr
, addrlen
);
3265 ret
= listen(fd
, 0);
3270 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
3272 s
->do_telnetopt
= 1;
3275 ret
= connect(fd
, addr
, addrlen
);
3277 err
= socket_error();
3278 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3279 } else if (err
== EINPROGRESS
) {
3282 } else if (err
== WSAEALREADY
) {
3294 socket_set_nodelay(fd
);
3296 tcp_chr_connect(chr
);
3298 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
3301 if (is_listen
&& is_waitconnect
) {
3302 printf("QEMU waiting for connection on: %s\n", host_str
);
3303 tcp_chr_accept(chr
);
3304 socket_set_nonblock(s
->listen_fd
);
3316 CharDriverState
*qemu_chr_open(const char *filename
)
3320 if (!strcmp(filename
, "vc")) {
3321 return text_console_init(&display_state
, 0);
3322 } else if (strstart(filename
, "vc:", &p
)) {
3323 return text_console_init(&display_state
, p
);
3324 } else if (!strcmp(filename
, "null")) {
3325 return qemu_chr_open_null();
3327 if (strstart(filename
, "tcp:", &p
)) {
3328 return qemu_chr_open_tcp(p
, 0, 0);
3330 if (strstart(filename
, "telnet:", &p
)) {
3331 return qemu_chr_open_tcp(p
, 1, 0);
3333 if (strstart(filename
, "udp:", &p
)) {
3334 return qemu_chr_open_udp(p
);
3336 if (strstart(filename
, "mon:", &p
)) {
3337 CharDriverState
*drv
= qemu_chr_open(p
);
3339 drv
= qemu_chr_open_mux(drv
);
3340 monitor_init(drv
, !nographic
);
3343 printf("Unable to open driver: %s\n", p
);
3347 if (strstart(filename
, "unix:", &p
)) {
3348 return qemu_chr_open_tcp(p
, 0, 1);
3349 } else if (strstart(filename
, "file:", &p
)) {
3350 return qemu_chr_open_file_out(p
);
3351 } else if (strstart(filename
, "pipe:", &p
)) {
3352 return qemu_chr_open_pipe(p
);
3353 } else if (!strcmp(filename
, "pty")) {
3354 return qemu_chr_open_pty();
3355 } else if (!strcmp(filename
, "stdio")) {
3356 return qemu_chr_open_stdio();
3358 #if defined(__linux__)
3359 if (strstart(filename
, "/dev/parport", NULL
)) {
3360 return qemu_chr_open_pp(filename
);
3363 #if defined(__linux__) || defined(__sun__)
3364 if (strstart(filename
, "/dev/", NULL
)) {
3365 return qemu_chr_open_tty(filename
);
3369 if (strstart(filename
, "COM", NULL
)) {
3370 return qemu_chr_open_win(filename
);
3372 if (strstart(filename
, "pipe:", &p
)) {
3373 return qemu_chr_open_win_pipe(p
);
3375 if (strstart(filename
, "con:", NULL
)) {
3376 return qemu_chr_open_win_con(filename
);
3378 if (strstart(filename
, "file:", &p
)) {
3379 return qemu_chr_open_win_file_out(p
);
3387 void qemu_chr_close(CharDriverState
*chr
)
3390 chr
->chr_close(chr
);
3393 /***********************************************************/
3394 /* network device redirectors */
3396 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
3400 for(i
=0;i
<size
;i
+=16) {
3404 fprintf(f
, "%08x ", i
);
3407 fprintf(f
, " %02x", buf
[i
+j
]);
3412 for(j
=0;j
<len
;j
++) {
3414 if (c
< ' ' || c
> '~')
3416 fprintf(f
, "%c", c
);
3422 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
3425 for(i
= 0; i
< 6; i
++) {
3426 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3439 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
3444 p1
= strchr(p
, sep
);
3450 if (len
> buf_size
- 1)
3452 memcpy(buf
, p
, len
);
3459 int parse_host_src_port(struct sockaddr_in
*haddr
,
3460 struct sockaddr_in
*saddr
,
3461 const char *input_str
)
3463 char *str
= strdup(input_str
);
3464 char *host_str
= str
;
3469 * Chop off any extra arguments at the end of the string which
3470 * would start with a comma, then fill in the src port information
3471 * if it was provided else use the "any address" and "any port".
3473 if ((ptr
= strchr(str
,',')))
3476 if ((src_str
= strchr(input_str
,'@'))) {
3481 if (parse_host_port(haddr
, host_str
) < 0)
3484 if (!src_str
|| *src_str
== '\0')
3487 if (parse_host_port(saddr
, src_str
) < 0)
3498 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
3506 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3508 saddr
->sin_family
= AF_INET
;
3509 if (buf
[0] == '\0') {
3510 saddr
->sin_addr
.s_addr
= 0;
3512 if (isdigit(buf
[0])) {
3513 if (!inet_aton(buf
, &saddr
->sin_addr
))
3516 if ((he
= gethostbyname(buf
)) == NULL
)
3518 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
3521 port
= strtol(p
, (char **)&r
, 0);
3524 saddr
->sin_port
= htons(port
);
3529 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
3534 len
= MIN(108, strlen(str
));
3535 p
= strchr(str
, ',');
3537 len
= MIN(len
, p
- str
);
3539 memset(uaddr
, 0, sizeof(*uaddr
));
3541 uaddr
->sun_family
= AF_UNIX
;
3542 memcpy(uaddr
->sun_path
, str
, len
);
3548 /* find or alloc a new VLAN */
3549 VLANState
*qemu_find_vlan(int id
)
3551 VLANState
**pvlan
, *vlan
;
3552 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3556 vlan
= qemu_mallocz(sizeof(VLANState
));
3561 pvlan
= &first_vlan
;
3562 while (*pvlan
!= NULL
)
3563 pvlan
= &(*pvlan
)->next
;
3568 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
3569 IOReadHandler
*fd_read
,
3570 IOCanRWHandler
*fd_can_read
,
3573 VLANClientState
*vc
, **pvc
;
3574 vc
= qemu_mallocz(sizeof(VLANClientState
));
3577 vc
->fd_read
= fd_read
;
3578 vc
->fd_can_read
= fd_can_read
;
3579 vc
->opaque
= opaque
;
3583 pvc
= &vlan
->first_client
;
3584 while (*pvc
!= NULL
)
3585 pvc
= &(*pvc
)->next
;
3590 int qemu_can_send_packet(VLANClientState
*vc1
)
3592 VLANState
*vlan
= vc1
->vlan
;
3593 VLANClientState
*vc
;
3595 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3597 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
3604 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
3606 VLANState
*vlan
= vc1
->vlan
;
3607 VLANClientState
*vc
;
3610 printf("vlan %d send:\n", vlan
->id
);
3611 hex_dump(stdout
, buf
, size
);
3613 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3615 vc
->fd_read(vc
->opaque
, buf
, size
);
3620 #if defined(CONFIG_SLIRP)
3622 /* slirp network adapter */
3624 static int slirp_inited
;
3625 static VLANClientState
*slirp_vc
;
3627 int slirp_can_output(void)
3629 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
3632 void slirp_output(const uint8_t *pkt
, int pkt_len
)
3635 printf("slirp output:\n");
3636 hex_dump(stdout
, pkt
, pkt_len
);
3640 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
3643 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
3646 printf("slirp input:\n");
3647 hex_dump(stdout
, buf
, size
);
3649 slirp_input(buf
, size
);
3652 static int net_slirp_init(VLANState
*vlan
)
3654 if (!slirp_inited
) {
3658 slirp_vc
= qemu_new_vlan_client(vlan
,
3659 slirp_receive
, NULL
, NULL
);
3660 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
3664 static void net_slirp_redir(const char *redir_str
)
3669 struct in_addr guest_addr
;
3670 int host_port
, guest_port
;
3672 if (!slirp_inited
) {
3678 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3680 if (!strcmp(buf
, "tcp")) {
3682 } else if (!strcmp(buf
, "udp")) {
3688 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3690 host_port
= strtol(buf
, &r
, 0);
3694 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3696 if (buf
[0] == '\0') {
3697 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
3699 if (!inet_aton(buf
, &guest_addr
))
3702 guest_port
= strtol(p
, &r
, 0);
3706 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3707 fprintf(stderr
, "qemu: could not set up redirection\n");
3712 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3720 static void smb_exit(void)
3724 char filename
[1024];
3726 /* erase all the files in the directory */
3727 d
= opendir(smb_dir
);
3732 if (strcmp(de
->d_name
, ".") != 0 &&
3733 strcmp(de
->d_name
, "..") != 0) {
3734 snprintf(filename
, sizeof(filename
), "%s/%s",
3735 smb_dir
, de
->d_name
);
3743 /* automatic user mode samba server configuration */
3744 void net_slirp_smb(const char *exported_dir
)
3746 char smb_conf
[1024];
3747 char smb_cmdline
[1024];
3750 if (!slirp_inited
) {
3755 /* XXX: better tmp dir construction */
3756 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3757 if (mkdir(smb_dir
, 0700) < 0) {
3758 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3761 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3763 f
= fopen(smb_conf
, "w");
3765 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3772 "socket address=127.0.0.1\n"
3773 "pid directory=%s\n"
3774 "lock directory=%s\n"
3775 "log file=%s/log.smbd\n"
3776 "smb passwd file=%s/smbpasswd\n"
3777 "security = share\n"
3792 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3793 SMBD_COMMAND
, smb_conf
);
3795 slirp_add_exec(0, smb_cmdline
, 4, 139);
3798 #endif /* !defined(_WIN32) */
3800 #endif /* CONFIG_SLIRP */
3802 #if !defined(_WIN32)
3804 typedef struct TAPState
{
3805 VLANClientState
*vc
;
3809 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3811 TAPState
*s
= opaque
;
3814 ret
= write(s
->fd
, buf
, size
);
3815 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3822 static void tap_send(void *opaque
)
3824 TAPState
*s
= opaque
;
3831 sbuf
.maxlen
= sizeof(buf
);
3833 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
3835 size
= read(s
->fd
, buf
, sizeof(buf
));
3838 qemu_send_packet(s
->vc
, buf
, size
);
3844 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3848 s
= qemu_mallocz(sizeof(TAPState
));
3852 enable_sigio_timer(fd
);
3853 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3854 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3855 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3859 #if defined (_BSD) || defined (__FreeBSD_kernel__)
3860 static int tap_open(char *ifname
, int ifname_size
)
3866 TFR(fd
= open("/dev/tap", O_RDWR
));
3868 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3873 dev
= devname(s
.st_rdev
, S_IFCHR
);
3874 pstrcpy(ifname
, ifname_size
, dev
);
3876 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3879 #elif defined(__sun__)
3880 #define TUNNEWPPA (('T'<<16) | 0x0001)
3882 * Allocate TAP device, returns opened fd.
3883 * Stores dev name in the first arg(must be large enough).
3885 int tap_alloc(char *dev
)
3887 int tap_fd
, if_fd
, ppa
= -1;
3888 static int ip_fd
= 0;
3891 static int arp_fd
= 0;
3892 int ip_muxid
, arp_muxid
;
3893 struct strioctl strioc_if
, strioc_ppa
;
3894 int link_type
= I_PLINK
;;
3896 char actual_name
[32] = "";
3898 memset(&ifr
, 0x0, sizeof(ifr
));
3902 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
3906 /* Check if IP device was opened */
3910 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
3912 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
3916 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
3918 syslog(LOG_ERR
, "Can't open /dev/tap");
3922 /* Assign a new PPA and get its unit number. */
3923 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
3924 strioc_ppa
.ic_timout
= 0;
3925 strioc_ppa
.ic_len
= sizeof(ppa
);
3926 strioc_ppa
.ic_dp
= (char *)&ppa
;
3927 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
3928 syslog (LOG_ERR
, "Can't assign new interface");
3930 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
3932 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
3935 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
3936 syslog(LOG_ERR
, "Can't push IP module");
3940 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
3941 syslog(LOG_ERR
, "Can't get flags\n");
3943 snprintf (actual_name
, 32, "tap%d", ppa
);
3944 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3947 /* Assign ppa according to the unit number returned by tun device */
3949 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
3950 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
3951 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
3952 syslog (LOG_ERR
, "Can't get flags\n");
3953 /* Push arp module to if_fd */
3954 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
3955 syslog (LOG_ERR
, "Can't push ARP module (2)");
3957 /* Push arp module to ip_fd */
3958 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
3959 syslog (LOG_ERR
, "I_POP failed\n");
3960 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
3961 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
3963 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
3965 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
3967 /* Set ifname to arp */
3968 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
3969 strioc_if
.ic_timout
= 0;
3970 strioc_if
.ic_len
= sizeof(ifr
);
3971 strioc_if
.ic_dp
= (char *)&ifr
;
3972 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
3973 syslog (LOG_ERR
, "Can't set ifname to arp\n");
3976 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
3977 syslog(LOG_ERR
, "Can't link TAP device to IP");
3981 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
3982 syslog (LOG_ERR
, "Can't link TAP device to ARP");
3986 memset(&ifr
, 0x0, sizeof(ifr
));
3987 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3988 ifr
.lifr_ip_muxid
= ip_muxid
;
3989 ifr
.lifr_arp_muxid
= arp_muxid
;
3991 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
3993 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
3994 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
3995 syslog (LOG_ERR
, "Can't set multiplexor id");
3998 sprintf(dev
, "tap%d", ppa
);
4002 static int tap_open(char *ifname
, int ifname_size
)
4006 if( (fd
= tap_alloc(dev
)) < 0 ){
4007 fprintf(stderr
, "Cannot allocate TAP device\n");
4010 pstrcpy(ifname
, ifname_size
, dev
);
4011 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
4015 static int tap_open(char *ifname
, int ifname_size
)
4020 TFR(fd
= open("/dev/net/tun", O_RDWR
));
4022 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
4025 memset(&ifr
, 0, sizeof(ifr
));
4026 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
4027 if (ifname
[0] != '\0')
4028 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
4030 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
4031 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
4033 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
4037 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
4038 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
4043 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
4044 const char *setup_script
)
4047 int pid
, status
, fd
;
4052 if (ifname1
!= NULL
)
4053 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
4056 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
4060 if (!setup_script
|| !strcmp(setup_script
, "no"))
4062 if (setup_script
[0] != '\0') {
4063 /* try to launch network init script */
4067 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
4068 for (i
= 0; i
< open_max
; i
++)
4069 if (i
!= STDIN_FILENO
&&
4070 i
!= STDOUT_FILENO
&&
4071 i
!= STDERR_FILENO
&&
4076 *parg
++ = (char *)setup_script
;
4079 execv(setup_script
, args
);
4082 while (waitpid(pid
, &status
, 0) != pid
);
4083 if (!WIFEXITED(status
) ||
4084 WEXITSTATUS(status
) != 0) {
4085 fprintf(stderr
, "%s: could not launch network script\n",
4091 s
= net_tap_fd_init(vlan
, fd
);
4094 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4095 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
4099 #endif /* !_WIN32 */
4101 /* network connection */
4102 typedef struct NetSocketState
{
4103 VLANClientState
*vc
;
4105 int state
; /* 0 = getting length, 1 = getting data */
4109 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
4112 typedef struct NetSocketListenState
{
4115 } NetSocketListenState
;
4117 /* XXX: we consider we can send the whole packet without blocking */
4118 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
4120 NetSocketState
*s
= opaque
;
4124 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
4125 send_all(s
->fd
, buf
, size
);
4128 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
4130 NetSocketState
*s
= opaque
;
4131 sendto(s
->fd
, buf
, size
, 0,
4132 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
4135 static void net_socket_send(void *opaque
)
4137 NetSocketState
*s
= opaque
;
4142 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
4144 err
= socket_error();
4145 if (err
!= EWOULDBLOCK
)
4147 } else if (size
== 0) {
4148 /* end of connection */
4150 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
4156 /* reassemble a packet from the network */
4162 memcpy(s
->buf
+ s
->index
, buf
, l
);
4166 if (s
->index
== 4) {
4168 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
4174 l
= s
->packet_len
- s
->index
;
4177 memcpy(s
->buf
+ s
->index
, buf
, l
);
4181 if (s
->index
>= s
->packet_len
) {
4182 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
4191 static void net_socket_send_dgram(void *opaque
)
4193 NetSocketState
*s
= opaque
;
4196 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
4200 /* end of connection */
4201 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
4204 qemu_send_packet(s
->vc
, s
->buf
, size
);
4207 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
4212 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
4213 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
4214 inet_ntoa(mcastaddr
->sin_addr
),
4215 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
4219 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
4221 perror("socket(PF_INET, SOCK_DGRAM)");
4226 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
4227 (const char *)&val
, sizeof(val
));
4229 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
4233 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
4239 /* Add host to multicast group */
4240 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
4241 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
4243 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
4244 (const char *)&imr
, sizeof(struct ip_mreq
));
4246 perror("setsockopt(IP_ADD_MEMBERSHIP)");
4250 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
4252 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
4253 (const char *)&val
, sizeof(val
));
4255 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
4259 socket_set_nonblock(fd
);
4267 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
4270 struct sockaddr_in saddr
;
4272 socklen_t saddr_len
;
4275 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
4276 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
4277 * by ONLY ONE process: we must "clone" this dgram socket --jjo
4281 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
4283 if (saddr
.sin_addr
.s_addr
==0) {
4284 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
4288 /* clone dgram socket */
4289 newfd
= net_socket_mcast_create(&saddr
);
4291 /* error already reported by net_socket_mcast_create() */
4295 /* clone newfd to fd, close newfd */
4300 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
4301 fd
, strerror(errno
));
4306 s
= qemu_mallocz(sizeof(NetSocketState
));
4311 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
4312 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
4314 /* mcast: save bound address as dst */
4315 if (is_connected
) s
->dgram_dst
=saddr
;
4317 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4318 "socket: fd=%d (%s mcast=%s:%d)",
4319 fd
, is_connected
? "cloned" : "",
4320 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4324 static void net_socket_connect(void *opaque
)
4326 NetSocketState
*s
= opaque
;
4327 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
4330 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
4334 s
= qemu_mallocz(sizeof(NetSocketState
));
4338 s
->vc
= qemu_new_vlan_client(vlan
,
4339 net_socket_receive
, NULL
, s
);
4340 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4341 "socket: fd=%d", fd
);
4343 net_socket_connect(s
);
4345 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
4350 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
4353 int so_type
=-1, optlen
=sizeof(so_type
);
4355 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
4356 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
4361 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
4363 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
4365 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
4366 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
4367 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
4372 static void net_socket_accept(void *opaque
)
4374 NetSocketListenState
*s
= opaque
;
4376 struct sockaddr_in saddr
;
4381 len
= sizeof(saddr
);
4382 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
4383 if (fd
< 0 && errno
!= EINTR
) {
4385 } else if (fd
>= 0) {
4389 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
4393 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
4394 "socket: connection from %s:%d",
4395 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4399 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
4401 NetSocketListenState
*s
;
4403 struct sockaddr_in saddr
;
4405 if (parse_host_port(&saddr
, host_str
) < 0)
4408 s
= qemu_mallocz(sizeof(NetSocketListenState
));
4412 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4417 socket_set_nonblock(fd
);
4419 /* allow fast reuse */
4421 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
4423 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4428 ret
= listen(fd
, 0);
4435 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
4439 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
4442 int fd
, connected
, ret
, err
;
4443 struct sockaddr_in saddr
;
4445 if (parse_host_port(&saddr
, host_str
) < 0)
4448 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4453 socket_set_nonblock(fd
);
4457 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4459 err
= socket_error();
4460 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
4461 } else if (err
== EINPROGRESS
) {
4464 } else if (err
== WSAEALREADY
) {
4477 s
= net_socket_fd_init(vlan
, fd
, connected
);
4480 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4481 "socket: connect to %s:%d",
4482 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4486 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
4490 struct sockaddr_in saddr
;
4492 if (parse_host_port(&saddr
, host_str
) < 0)
4496 fd
= net_socket_mcast_create(&saddr
);
4500 s
= net_socket_fd_init(vlan
, fd
, 0);
4504 s
->dgram_dst
= saddr
;
4506 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4507 "socket: mcast=%s:%d",
4508 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4513 static int get_param_value(char *buf
, int buf_size
,
4514 const char *tag
, const char *str
)
4523 while (*p
!= '\0' && *p
!= '=') {
4524 if ((q
- option
) < sizeof(option
) - 1)
4532 if (!strcmp(tag
, option
)) {
4534 while (*p
!= '\0' && *p
!= ',') {
4535 if ((q
- buf
) < buf_size
- 1)
4542 while (*p
!= '\0' && *p
!= ',') {
4553 static int net_client_init(const char *str
)
4564 while (*p
!= '\0' && *p
!= ',') {
4565 if ((q
- device
) < sizeof(device
) - 1)
4573 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
4574 vlan_id
= strtol(buf
, NULL
, 0);
4576 vlan
= qemu_find_vlan(vlan_id
);
4578 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
4581 if (!strcmp(device
, "nic")) {
4585 if (nb_nics
>= MAX_NICS
) {
4586 fprintf(stderr
, "Too Many NICs\n");
4589 nd
= &nd_table
[nb_nics
];
4590 macaddr
= nd
->macaddr
;
4596 macaddr
[5] = 0x56 + nb_nics
;
4598 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
4599 if (parse_macaddr(macaddr
, buf
) < 0) {
4600 fprintf(stderr
, "invalid syntax for ethernet address\n");
4604 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
4605 nd
->model
= strdup(buf
);
4609 vlan
->nb_guest_devs
++;
4612 if (!strcmp(device
, "none")) {
4613 /* does nothing. It is needed to signal that no network cards
4618 if (!strcmp(device
, "user")) {
4619 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
4620 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
4622 vlan
->nb_host_devs
++;
4623 ret
= net_slirp_init(vlan
);
4627 if (!strcmp(device
, "tap")) {
4629 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4630 fprintf(stderr
, "tap: no interface name\n");
4633 vlan
->nb_host_devs
++;
4634 ret
= tap_win32_init(vlan
, ifname
);
4637 if (!strcmp(device
, "tap")) {
4639 char setup_script
[1024];
4641 vlan
->nb_host_devs
++;
4642 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4643 fd
= strtol(buf
, NULL
, 0);
4645 if (net_tap_fd_init(vlan
, fd
))
4648 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4651 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
4652 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
4654 ret
= net_tap_init(vlan
, ifname
, setup_script
);
4658 if (!strcmp(device
, "socket")) {
4659 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4661 fd
= strtol(buf
, NULL
, 0);
4663 if (net_socket_fd_init(vlan
, fd
, 1))
4665 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
4666 ret
= net_socket_listen_init(vlan
, buf
);
4667 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
4668 ret
= net_socket_connect_init(vlan
, buf
);
4669 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
4670 ret
= net_socket_mcast_init(vlan
, buf
);
4672 fprintf(stderr
, "Unknown socket options: %s\n", p
);
4675 vlan
->nb_host_devs
++;
4678 fprintf(stderr
, "Unknown network device: %s\n", device
);
4682 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
4688 void do_info_network(void)
4691 VLANClientState
*vc
;
4693 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
4694 term_printf("VLAN %d devices:\n", vlan
->id
);
4695 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
4696 term_printf(" %s\n", vc
->info_str
);
4700 /***********************************************************/
4703 static USBPort
*used_usb_ports
;
4704 static USBPort
*free_usb_ports
;
4706 /* ??? Maybe change this to register a hub to keep track of the topology. */
4707 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
4708 usb_attachfn attach
)
4710 port
->opaque
= opaque
;
4711 port
->index
= index
;
4712 port
->attach
= attach
;
4713 port
->next
= free_usb_ports
;
4714 free_usb_ports
= port
;
4717 static int usb_device_add(const char *devname
)
4723 if (!free_usb_ports
)
4726 if (strstart(devname
, "host:", &p
)) {
4727 dev
= usb_host_device_open(p
);
4728 } else if (!strcmp(devname
, "mouse")) {
4729 dev
= usb_mouse_init();
4730 } else if (!strcmp(devname
, "tablet")) {
4731 dev
= usb_tablet_init();
4732 } else if (!strcmp(devname
, "keyboard")) {
4733 dev
= usb_keyboard_init();
4734 } else if (strstart(devname
, "disk:", &p
)) {
4735 dev
= usb_msd_init(p
);
4736 } else if (!strcmp(devname
, "wacom-tablet")) {
4737 dev
= usb_wacom_init();
4744 /* Find a USB port to add the device to. */
4745 port
= free_usb_ports
;
4749 /* Create a new hub and chain it on. */
4750 free_usb_ports
= NULL
;
4751 port
->next
= used_usb_ports
;
4752 used_usb_ports
= port
;
4754 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
4755 usb_attach(port
, hub
);
4756 port
= free_usb_ports
;
4759 free_usb_ports
= port
->next
;
4760 port
->next
= used_usb_ports
;
4761 used_usb_ports
= port
;
4762 usb_attach(port
, dev
);
4766 static int usb_device_del(const char *devname
)
4774 if (!used_usb_ports
)
4777 p
= strchr(devname
, '.');
4780 bus_num
= strtoul(devname
, NULL
, 0);
4781 addr
= strtoul(p
+ 1, NULL
, 0);
4785 lastp
= &used_usb_ports
;
4786 port
= used_usb_ports
;
4787 while (port
&& port
->dev
->addr
!= addr
) {
4788 lastp
= &port
->next
;
4796 *lastp
= port
->next
;
4797 usb_attach(port
, NULL
);
4798 dev
->handle_destroy(dev
);
4799 port
->next
= free_usb_ports
;
4800 free_usb_ports
= port
;
4804 void do_usb_add(const char *devname
)
4807 ret
= usb_device_add(devname
);
4809 term_printf("Could not add USB device '%s'\n", devname
);
4812 void do_usb_del(const char *devname
)
4815 ret
= usb_device_del(devname
);
4817 term_printf("Could not remove USB device '%s'\n", devname
);
4824 const char *speed_str
;
4827 term_printf("USB support not enabled\n");
4831 for (port
= used_usb_ports
; port
; port
= port
->next
) {
4835 switch(dev
->speed
) {
4839 case USB_SPEED_FULL
:
4842 case USB_SPEED_HIGH
:
4849 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
4850 0, dev
->addr
, speed_str
, dev
->devname
);
4854 /***********************************************************/
4855 /* PCMCIA/Cardbus */
4857 static struct pcmcia_socket_entry_s
{
4858 struct pcmcia_socket_s
*socket
;
4859 struct pcmcia_socket_entry_s
*next
;
4860 } *pcmcia_sockets
= 0;
4862 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
4864 struct pcmcia_socket_entry_s
*entry
;
4866 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
4867 entry
->socket
= socket
;
4868 entry
->next
= pcmcia_sockets
;
4869 pcmcia_sockets
= entry
;
4872 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
4874 struct pcmcia_socket_entry_s
*entry
, **ptr
;
4876 ptr
= &pcmcia_sockets
;
4877 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
4878 if (entry
->socket
== socket
) {
4884 void pcmcia_info(void)
4886 struct pcmcia_socket_entry_s
*iter
;
4887 if (!pcmcia_sockets
)
4888 term_printf("No PCMCIA sockets\n");
4890 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
4891 term_printf("%s: %s\n", iter
->socket
->slot_string
,
4892 iter
->socket
->attached
? iter
->socket
->card_string
:
4896 /***********************************************************/
4899 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4903 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4907 static void dumb_refresh(DisplayState
*ds
)
4909 #if defined(CONFIG_SDL)
4914 static void dumb_display_init(DisplayState
*ds
)
4919 ds
->dpy_update
= dumb_update
;
4920 ds
->dpy_resize
= dumb_resize
;
4921 ds
->dpy_refresh
= dumb_refresh
;
4924 /***********************************************************/
4927 #define MAX_IO_HANDLERS 64
4929 typedef struct IOHandlerRecord
{
4931 IOCanRWHandler
*fd_read_poll
;
4933 IOHandler
*fd_write
;
4936 /* temporary data */
4938 struct IOHandlerRecord
*next
;
4941 static IOHandlerRecord
*first_io_handler
;
4943 /* XXX: fd_read_poll should be suppressed, but an API change is
4944 necessary in the character devices to suppress fd_can_read(). */
4945 int qemu_set_fd_handler2(int fd
,
4946 IOCanRWHandler
*fd_read_poll
,
4948 IOHandler
*fd_write
,
4951 IOHandlerRecord
**pioh
, *ioh
;
4953 if (!fd_read
&& !fd_write
) {
4954 pioh
= &first_io_handler
;
4959 if (ioh
->fd
== fd
) {
4966 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4970 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4973 ioh
->next
= first_io_handler
;
4974 first_io_handler
= ioh
;
4977 ioh
->fd_read_poll
= fd_read_poll
;
4978 ioh
->fd_read
= fd_read
;
4979 ioh
->fd_write
= fd_write
;
4980 ioh
->opaque
= opaque
;
4986 int qemu_set_fd_handler(int fd
,
4988 IOHandler
*fd_write
,
4991 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4994 /***********************************************************/
4995 /* Polling handling */
4997 typedef struct PollingEntry
{
5000 struct PollingEntry
*next
;
5003 static PollingEntry
*first_polling_entry
;
5005 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
5007 PollingEntry
**ppe
, *pe
;
5008 pe
= qemu_mallocz(sizeof(PollingEntry
));
5012 pe
->opaque
= opaque
;
5013 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
5018 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
5020 PollingEntry
**ppe
, *pe
;
5021 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
5023 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
5032 /***********************************************************/
5033 /* Wait objects support */
5034 typedef struct WaitObjects
{
5036 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
5037 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
5038 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
5041 static WaitObjects wait_objects
= {0};
5043 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
5045 WaitObjects
*w
= &wait_objects
;
5047 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
5049 w
->events
[w
->num
] = handle
;
5050 w
->func
[w
->num
] = func
;
5051 w
->opaque
[w
->num
] = opaque
;
5056 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
5059 WaitObjects
*w
= &wait_objects
;
5062 for (i
= 0; i
< w
->num
; i
++) {
5063 if (w
->events
[i
] == handle
)
5066 w
->events
[i
] = w
->events
[i
+ 1];
5067 w
->func
[i
] = w
->func
[i
+ 1];
5068 w
->opaque
[i
] = w
->opaque
[i
+ 1];
5076 #define SELF_ANNOUNCE_ROUNDS 5
5077 #define ETH_P_EXPERIMENTAL 0x01F1 /* just a number */
5078 //#define ETH_P_EXPERIMENTAL 0x0012 /* make it the size of the packet */
5079 #define EXPERIMENTAL_MAGIC 0xf1f23f4f
5081 static int announce_self_create(uint8_t *buf
,
5084 uint32_t magic
= EXPERIMENTAL_MAGIC
;
5085 uint16_t proto
= htons(ETH_P_EXPERIMENTAL
);
5087 /* FIXME: should we send a different packet (arp/rarp/ping)? */
5089 memset(buf
, 0xff, 6); /* h_dst */
5090 memcpy(buf
+ 6, mac_addr
, 6); /* h_src */
5091 memcpy(buf
+ 12, &proto
, 2); /* h_proto */
5092 memcpy(buf
+ 14, &magic
, 4); /* magic */
5094 return 18; /* len */
5097 static void qemu_announce_self(void)
5101 VLANClientState
*vc
;
5104 for (i
= 0; i
< nb_nics
; i
++) {
5105 len
= announce_self_create(buf
, nd_table
[i
].macaddr
);
5106 vlan
= nd_table
[i
].vlan
;
5107 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
5108 if (vc
->fd_read
== tap_receive
) /* send only if tap */
5109 for (j
=0; j
< SELF_ANNOUNCE_ROUNDS
; j
++)
5110 vc
->fd_read(vc
->opaque
, buf
, len
);
5115 /***********************************************************/
5116 /* savevm/loadvm support */
5118 #define IO_BUF_SIZE 32768
5121 QEMUFilePutBufferFunc
*put_buffer
;
5122 QEMUFileGetBufferFunc
*get_buffer
;
5123 QEMUFileCloseFunc
*close
;
5126 int64_t buf_offset
; /* start of buffer when writing, end of buffer
5129 int buf_size
; /* 0 when writing */
5130 uint8_t buf
[IO_BUF_SIZE
];
5133 typedef struct QEMUFileFD
5138 static int fd_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
5140 QEMUFileFD
*s
= opaque
;
5145 len
= read(s
->fd
, buf
+ offset
, size
- offset
);
5147 if (errno
== EINTR
|| errno
== EAGAIN
)
5154 QEMUFile
*qemu_fopen_fd(int fd
)
5156 QEMUFileFD
*s
= qemu_mallocz(sizeof(QEMUFileFD
));
5158 return qemu_fopen(s
, NULL
, fd_get_buffer
, qemu_free
);
5161 typedef struct QEMUFileUnix
5166 static void file_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
5168 QEMUFileUnix
*s
= opaque
;
5169 fseek(s
->outfile
, pos
, SEEK_SET
);
5170 fwrite(buf
, 1, size
, s
->outfile
);
5173 static int file_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
5175 QEMUFileUnix
*s
= opaque
;
5176 fseek(s
->outfile
, pos
, SEEK_SET
);
5177 return fread(buf
, 1, size
, s
->outfile
);
5180 static void file_close(void *opaque
)
5182 QEMUFileUnix
*s
= opaque
;
5187 QEMUFile
*qemu_fopen_file(const char *filename
, const char *mode
)
5191 s
= qemu_mallocz(sizeof(QEMUFileUnix
));
5195 s
->outfile
= fopen(filename
, mode
);
5199 if (!strcmp(mode
, "wb"))
5200 return qemu_fopen(s
, file_put_buffer
, NULL
, file_close
);
5201 else if (!strcmp(mode
, "rb"))
5202 return qemu_fopen(s
, NULL
, file_get_buffer
, file_close
);
5211 typedef struct QEMUFileBdrv
5213 BlockDriverState
*bs
;
5214 int64_t base_offset
;
5217 static void bdrv_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
5219 QEMUFileBdrv
*s
= opaque
;
5220 bdrv_pwrite(s
->bs
, s
->base_offset
+ pos
, buf
, size
);
5223 static int bdrv_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
5225 QEMUFileBdrv
*s
= opaque
;
5226 return bdrv_pread(s
->bs
, s
->base_offset
+ pos
, buf
, size
);
5229 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
5233 s
= qemu_mallocz(sizeof(QEMUFileBdrv
));
5238 s
->base_offset
= offset
;
5241 return qemu_fopen(s
, bdrv_put_buffer
, NULL
, qemu_free
);
5243 return qemu_fopen(s
, NULL
, bdrv_get_buffer
, qemu_free
);
5246 QEMUFile
*qemu_fopen(void *opaque
, QEMUFilePutBufferFunc
*put_buffer
,
5247 QEMUFileGetBufferFunc
*get_buffer
, QEMUFileCloseFunc
*close
)
5251 f
= qemu_mallocz(sizeof(QEMUFile
));
5256 f
->put_buffer
= put_buffer
;
5257 f
->get_buffer
= get_buffer
;
5263 void qemu_fflush(QEMUFile
*f
)
5268 if (f
->buf_index
> 0) {
5269 f
->put_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, f
->buf_index
);
5270 f
->buf_offset
+= f
->buf_index
;
5275 static void qemu_fill_buffer(QEMUFile
*f
)
5282 len
= f
->get_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, IO_BUF_SIZE
);
5288 f
->buf_offset
+= len
;
5291 void qemu_fclose(QEMUFile
*f
)
5295 f
->close(f
->opaque
);
5299 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
5303 l
= IO_BUF_SIZE
- f
->buf_index
;
5306 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
5310 if (f
->buf_index
>= IO_BUF_SIZE
)
5315 void qemu_put_byte(QEMUFile
*f
, int v
)
5317 f
->buf
[f
->buf_index
++] = v
;
5318 if (f
->buf_index
>= IO_BUF_SIZE
)
5322 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
5328 l
= f
->buf_size
- f
->buf_index
;
5330 qemu_fill_buffer(f
);
5331 l
= f
->buf_size
- f
->buf_index
;
5337 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
5342 return size1
- size
;
5345 int qemu_get_byte(QEMUFile
*f
)
5347 if (f
->buf_index
>= f
->buf_size
) {
5348 qemu_fill_buffer(f
);
5349 if (f
->buf_index
>= f
->buf_size
)
5352 return f
->buf
[f
->buf_index
++];
5355 int64_t qemu_ftell(QEMUFile
*f
)
5357 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
5360 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
5362 if (whence
== SEEK_SET
) {
5364 } else if (whence
== SEEK_CUR
) {
5365 pos
+= qemu_ftell(f
);
5367 /* SEEK_END not supported */
5370 if (f
->put_buffer
) {
5372 f
->buf_offset
= pos
;
5374 f
->buf_offset
= pos
;
5381 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
5383 qemu_put_byte(f
, v
>> 8);
5384 qemu_put_byte(f
, v
);
5387 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
5389 qemu_put_byte(f
, v
>> 24);
5390 qemu_put_byte(f
, v
>> 16);
5391 qemu_put_byte(f
, v
>> 8);
5392 qemu_put_byte(f
, v
);
5395 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
5397 qemu_put_be32(f
, v
>> 32);
5398 qemu_put_be32(f
, v
);
5401 unsigned int qemu_get_be16(QEMUFile
*f
)
5404 v
= qemu_get_byte(f
) << 8;
5405 v
|= qemu_get_byte(f
);
5409 unsigned int qemu_get_be32(QEMUFile
*f
)
5412 v
= qemu_get_byte(f
) << 24;
5413 v
|= qemu_get_byte(f
) << 16;
5414 v
|= qemu_get_byte(f
) << 8;
5415 v
|= qemu_get_byte(f
);
5419 uint64_t qemu_get_be64(QEMUFile
*f
)
5422 v
= (uint64_t)qemu_get_be32(f
) << 32;
5423 v
|= qemu_get_be32(f
);
5427 typedef struct SaveStateEntry
{
5431 SaveStateHandler
*save_state
;
5432 LoadStateHandler
*load_state
;
5434 struct SaveStateEntry
*next
;
5437 static SaveStateEntry
*first_se
;
5439 int register_savevm(const char *idstr
,
5442 SaveStateHandler
*save_state
,
5443 LoadStateHandler
*load_state
,
5446 SaveStateEntry
*se
, **pse
;
5448 se
= qemu_malloc(sizeof(SaveStateEntry
));
5451 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
5452 se
->instance_id
= instance_id
;
5453 se
->version_id
= version_id
;
5454 se
->save_state
= save_state
;
5455 se
->load_state
= load_state
;
5456 se
->opaque
= opaque
;
5459 /* add at the end of list */
5461 while (*pse
!= NULL
)
5462 pse
= &(*pse
)->next
;
5467 #define QEMU_VM_FILE_MAGIC 0x5145564d
5468 #define QEMU_VM_FILE_VERSION 0x00000002
5470 int qemu_savevm_state(QEMUFile
*f
)
5474 int64_t cur_pos
, len_pos
, total_len_pos
;
5476 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
5477 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
5478 total_len_pos
= qemu_ftell(f
);
5479 qemu_put_be64(f
, 0); /* total size */
5481 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5483 len
= strlen(se
->idstr
);
5484 qemu_put_byte(f
, len
);
5485 qemu_put_buffer(f
, se
->idstr
, len
);
5487 qemu_put_be32(f
, se
->instance_id
);
5488 qemu_put_be32(f
, se
->version_id
);
5490 /* record size: filled later */
5491 len_pos
= qemu_ftell(f
);
5492 qemu_put_be32(f
, 0);
5494 se
->save_state(f
, se
->opaque
);
5496 /* fill record size */
5497 cur_pos
= qemu_ftell(f
);
5498 len
= cur_pos
- len_pos
- 4;
5499 qemu_fseek(f
, len_pos
, SEEK_SET
);
5500 qemu_put_be32(f
, len
);
5501 qemu_fseek(f
, cur_pos
, SEEK_SET
);
5503 cur_pos
= qemu_ftell(f
);
5504 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
5505 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
5506 qemu_fseek(f
, cur_pos
, SEEK_SET
);
5512 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
5516 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5517 if (!strcmp(se
->idstr
, idstr
) &&
5518 instance_id
== se
->instance_id
)
5524 int qemu_loadvm_state(QEMUFile
*f
)
5527 int len
, ret
, instance_id
, record_len
, version_id
;
5528 int64_t total_len
, end_pos
, cur_pos
;
5532 v
= qemu_get_be32(f
);
5533 if (v
!= QEMU_VM_FILE_MAGIC
)
5535 v
= qemu_get_be32(f
);
5536 if (v
!= QEMU_VM_FILE_VERSION
) {
5541 total_len
= qemu_get_be64(f
);
5542 end_pos
= total_len
+ qemu_ftell(f
);
5544 if (qemu_ftell(f
) >= end_pos
)
5546 len
= qemu_get_byte(f
);
5547 qemu_get_buffer(f
, idstr
, len
);
5549 instance_id
= qemu_get_be32(f
);
5550 version_id
= qemu_get_be32(f
);
5551 record_len
= qemu_get_be32(f
);
5553 printf("idstr=%s instance=0x%x version=%d len=%d\n",
5554 idstr
, instance_id
, version_id
, record_len
);
5556 cur_pos
= qemu_ftell(f
);
5557 se
= find_se(idstr
, instance_id
);
5559 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
5560 instance_id
, idstr
);
5562 ret
= se
->load_state(f
, se
->opaque
, version_id
);
5564 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
5565 instance_id
, idstr
);
5569 /* always seek to exact end of record */
5570 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
5577 int qemu_live_savevm_state(QEMUFile
*f
)
5582 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
5583 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
5585 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5586 len
= strlen(se
->idstr
);
5588 qemu_put_byte(f
, len
);
5589 qemu_put_buffer(f
, se
->idstr
, len
);
5590 qemu_put_be32(f
, se
->instance_id
);
5591 qemu_put_be32(f
, se
->version_id
);
5593 se
->save_state(f
, se
->opaque
);
5596 qemu_put_byte(f
, 0);
5602 int qemu_live_loadvm_state(QEMUFile
*f
)
5605 int len
, ret
, instance_id
, version_id
;
5609 v
= qemu_get_be32(f
);
5610 if (v
!= QEMU_VM_FILE_MAGIC
)
5612 v
= qemu_get_be32(f
);
5613 if (v
!= QEMU_VM_FILE_VERSION
) {
5620 len
= qemu_get_byte(f
);
5623 qemu_get_buffer(f
, idstr
, len
);
5625 instance_id
= qemu_get_be32(f
);
5626 version_id
= qemu_get_be32(f
);
5627 se
= find_se(idstr
, instance_id
);
5629 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
5630 instance_id
, idstr
);
5632 if (version_id
> se
->version_id
) { /* src version > dst version */
5633 fprintf(stderr
, "migration:version mismatch:%s:%d(s)>%d(d)\n",
5634 idstr
, version_id
, se
->version_id
);
5638 ret
= se
->load_state(f
, se
->opaque
, version_id
);
5640 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
5641 instance_id
, idstr
);
5648 qemu_announce_self();
5654 /* device can contain snapshots */
5655 static int bdrv_can_snapshot(BlockDriverState
*bs
)
5658 !bdrv_is_removable(bs
) &&
5659 !bdrv_is_read_only(bs
));
5662 /* device must be snapshots in order to have a reliable snapshot */
5663 static int bdrv_has_snapshot(BlockDriverState
*bs
)
5666 !bdrv_is_removable(bs
) &&
5667 !bdrv_is_read_only(bs
));
5670 static BlockDriverState
*get_bs_snapshots(void)
5672 BlockDriverState
*bs
;
5676 return bs_snapshots
;
5677 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5679 if (bdrv_can_snapshot(bs
))
5688 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
5691 QEMUSnapshotInfo
*sn_tab
, *sn
;
5695 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5698 for(i
= 0; i
< nb_sns
; i
++) {
5700 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
5710 void do_savevm(const char *name
)
5712 BlockDriverState
*bs
, *bs1
;
5713 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
5714 int must_delete
, ret
, i
;
5715 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5717 int saved_vm_running
;
5724 bs
= get_bs_snapshots();
5726 term_printf("No block device can accept snapshots\n");
5730 /* ??? Should this occur after vm_stop? */
5733 saved_vm_running
= vm_running
;
5738 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
5743 memset(sn
, 0, sizeof(*sn
));
5745 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
5746 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
5749 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
5752 /* fill auxiliary fields */
5755 sn
->date_sec
= tb
.time
;
5756 sn
->date_nsec
= tb
.millitm
* 1000000;
5758 gettimeofday(&tv
, NULL
);
5759 sn
->date_sec
= tv
.tv_sec
;
5760 sn
->date_nsec
= tv
.tv_usec
* 1000;
5762 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
5764 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5765 term_printf("Device %s does not support VM state snapshots\n",
5766 bdrv_get_device_name(bs
));
5770 /* save the VM state */
5771 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
5773 term_printf("Could not open VM state file\n");
5776 ret
= qemu_savevm_state(f
);
5777 sn
->vm_state_size
= qemu_ftell(f
);
5780 term_printf("Error %d while writing VM\n", ret
);
5784 /* create the snapshots */
5786 for(i
= 0; i
< MAX_DISKS
; i
++) {
5788 if (bdrv_has_snapshot(bs1
)) {
5790 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
5792 term_printf("Error while deleting snapshot on '%s'\n",
5793 bdrv_get_device_name(bs1
));
5796 ret
= bdrv_snapshot_create(bs1
, sn
);
5798 term_printf("Error while creating snapshot on '%s'\n",
5799 bdrv_get_device_name(bs1
));
5805 if (saved_vm_running
)
5809 void do_loadvm(const char *name
)
5811 BlockDriverState
*bs
, *bs1
;
5812 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5815 int saved_vm_running
;
5817 bs
= get_bs_snapshots();
5819 term_printf("No block device supports snapshots\n");
5823 /* Flush all IO requests so they don't interfere with the new state. */
5826 saved_vm_running
= vm_running
;
5829 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5831 if (bdrv_has_snapshot(bs1
)) {
5832 ret
= bdrv_snapshot_goto(bs1
, name
);
5835 term_printf("Warning: ");
5838 term_printf("Snapshots not supported on device '%s'\n",
5839 bdrv_get_device_name(bs1
));
5842 term_printf("Could not find snapshot '%s' on device '%s'\n",
5843 name
, bdrv_get_device_name(bs1
));
5846 term_printf("Error %d while activating snapshot on '%s'\n",
5847 ret
, bdrv_get_device_name(bs1
));
5850 /* fatal on snapshot block device */
5857 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5858 term_printf("Device %s does not support VM state snapshots\n",
5859 bdrv_get_device_name(bs
));
5863 /* restore the VM state */
5864 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
5866 term_printf("Could not open VM state file\n");
5869 ret
= qemu_loadvm_state(f
);
5872 term_printf("Error %d while loading VM state\n", ret
);
5875 if (saved_vm_running
)
5879 void do_delvm(const char *name
)
5881 BlockDriverState
*bs
, *bs1
;
5884 bs
= get_bs_snapshots();
5886 term_printf("No block device supports snapshots\n");
5890 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5892 if (bdrv_has_snapshot(bs1
)) {
5893 ret
= bdrv_snapshot_delete(bs1
, name
);
5895 if (ret
== -ENOTSUP
)
5896 term_printf("Snapshots not supported on device '%s'\n",
5897 bdrv_get_device_name(bs1
));
5899 term_printf("Error %d while deleting snapshot on '%s'\n",
5900 ret
, bdrv_get_device_name(bs1
));
5906 void do_info_snapshots(void)
5908 BlockDriverState
*bs
, *bs1
;
5909 QEMUSnapshotInfo
*sn_tab
, *sn
;
5913 bs
= get_bs_snapshots();
5915 term_printf("No available block device supports snapshots\n");
5918 term_printf("Snapshot devices:");
5919 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5921 if (bdrv_has_snapshot(bs1
)) {
5923 term_printf(" %s", bdrv_get_device_name(bs1
));
5928 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5930 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
5933 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
5934 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
5935 for(i
= 0; i
< nb_sns
; i
++) {
5937 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
5942 /***********************************************************/
5943 /* cpu save/restore */
5945 #if defined(TARGET_I386)
5947 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
5949 qemu_put_be32(f
, dt
->selector
);
5950 qemu_put_betl(f
, dt
->base
);
5951 qemu_put_be32(f
, dt
->limit
);
5952 qemu_put_be32(f
, dt
->flags
);
5955 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
5957 dt
->selector
= qemu_get_be32(f
);
5958 dt
->base
= qemu_get_betl(f
);
5959 dt
->limit
= qemu_get_be32(f
);
5960 dt
->flags
= qemu_get_be32(f
);
5963 void cpu_save(QEMUFile
*f
, void *opaque
)
5965 CPUState
*env
= opaque
;
5966 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
5972 kvm_save_registers(env
);
5975 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5976 qemu_put_betls(f
, &env
->regs
[i
]);
5977 qemu_put_betls(f
, &env
->eip
);
5978 qemu_put_betls(f
, &env
->eflags
);
5979 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
5980 qemu_put_be32s(f
, &hflags
);
5984 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
5986 for(i
= 0; i
< 8; i
++) {
5987 fptag
|= ((!env
->fptags
[i
]) << i
);
5990 qemu_put_be16s(f
, &fpuc
);
5991 qemu_put_be16s(f
, &fpus
);
5992 qemu_put_be16s(f
, &fptag
);
5994 #ifdef USE_X86LDOUBLE
5999 qemu_put_be16s(f
, &fpregs_format
);
6001 for(i
= 0; i
< 8; i
++) {
6002 #ifdef USE_X86LDOUBLE
6006 /* we save the real CPU data (in case of MMX usage only 'mant'
6007 contains the MMX register */
6008 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
6009 qemu_put_be64(f
, mant
);
6010 qemu_put_be16(f
, exp
);
6013 /* if we use doubles for float emulation, we save the doubles to
6014 avoid losing information in case of MMX usage. It can give
6015 problems if the image is restored on a CPU where long
6016 doubles are used instead. */
6017 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
6021 for(i
= 0; i
< 6; i
++)
6022 cpu_put_seg(f
, &env
->segs
[i
]);
6023 cpu_put_seg(f
, &env
->ldt
);
6024 cpu_put_seg(f
, &env
->tr
);
6025 cpu_put_seg(f
, &env
->gdt
);
6026 cpu_put_seg(f
, &env
->idt
);
6028 qemu_put_be32s(f
, &env
->sysenter_cs
);
6029 qemu_put_be32s(f
, &env
->sysenter_esp
);
6030 qemu_put_be32s(f
, &env
->sysenter_eip
);
6032 qemu_put_betls(f
, &env
->cr
[0]);
6033 qemu_put_betls(f
, &env
->cr
[2]);
6034 qemu_put_betls(f
, &env
->cr
[3]);
6035 qemu_put_betls(f
, &env
->cr
[4]);
6037 for(i
= 0; i
< 8; i
++)
6038 qemu_put_betls(f
, &env
->dr
[i
]);
6041 qemu_put_be32s(f
, &env
->a20_mask
);
6044 qemu_put_be32s(f
, &env
->mxcsr
);
6045 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
6046 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
6047 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
6050 #ifdef TARGET_X86_64
6051 qemu_put_be64s(f
, &env
->efer
);
6052 qemu_put_be64s(f
, &env
->star
);
6053 qemu_put_be64s(f
, &env
->lstar
);
6054 qemu_put_be64s(f
, &env
->cstar
);
6055 qemu_put_be64s(f
, &env
->fmask
);
6056 qemu_put_be64s(f
, &env
->kernelgsbase
);
6058 qemu_put_be32s(f
, &env
->smbase
);
6062 for (i
= 0; i
< NR_IRQ_WORDS
; i
++) {
6063 qemu_put_be32s(f
, &env
->kvm_interrupt_bitmap
[i
]);
6065 qemu_put_be64s(f
, &env
->tsc
);
6071 #ifdef USE_X86LDOUBLE
6072 /* XXX: add that in a FPU generic layer */
6073 union x86_longdouble
{
6078 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
6079 #define EXPBIAS1 1023
6080 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
6081 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
6083 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
6087 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
6088 /* exponent + sign */
6089 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
6090 e
|= SIGND1(temp
) >> 16;
6095 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6097 CPUState
*env
= opaque
;
6100 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
6102 if (version_id
!= 3 && version_id
!= 4)
6104 for(i
= 0; i
< CPU_NB_REGS
; i
++)
6105 qemu_get_betls(f
, &env
->regs
[i
]);
6106 qemu_get_betls(f
, &env
->eip
);
6107 qemu_get_betls(f
, &env
->eflags
);
6108 qemu_get_be32s(f
, &hflags
);
6110 qemu_get_be16s(f
, &fpuc
);
6111 qemu_get_be16s(f
, &fpus
);
6112 qemu_get_be16s(f
, &fptag
);
6113 qemu_get_be16s(f
, &fpregs_format
);
6115 /* NOTE: we cannot always restore the FPU state if the image come
6116 from a host with a different 'USE_X86LDOUBLE' define. We guess
6117 if we are in an MMX state to restore correctly in that case. */
6118 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
6119 for(i
= 0; i
< 8; i
++) {
6123 switch(fpregs_format
) {
6125 mant
= qemu_get_be64(f
);
6126 exp
= qemu_get_be16(f
);
6127 #ifdef USE_X86LDOUBLE
6128 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
6130 /* difficult case */
6132 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
6134 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
6138 mant
= qemu_get_be64(f
);
6139 #ifdef USE_X86LDOUBLE
6141 union x86_longdouble
*p
;
6142 /* difficult case */
6143 p
= (void *)&env
->fpregs
[i
];
6148 fp64_to_fp80(p
, mant
);
6152 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
6161 /* XXX: restore FPU round state */
6162 env
->fpstt
= (fpus
>> 11) & 7;
6163 env
->fpus
= fpus
& ~0x3800;
6165 for(i
= 0; i
< 8; i
++) {
6166 env
->fptags
[i
] = (fptag
>> i
) & 1;
6169 for(i
= 0; i
< 6; i
++)
6170 cpu_get_seg(f
, &env
->segs
[i
]);
6171 cpu_get_seg(f
, &env
->ldt
);
6172 cpu_get_seg(f
, &env
->tr
);
6173 cpu_get_seg(f
, &env
->gdt
);
6174 cpu_get_seg(f
, &env
->idt
);
6176 qemu_get_be32s(f
, &env
->sysenter_cs
);
6177 qemu_get_be32s(f
, &env
->sysenter_esp
);
6178 qemu_get_be32s(f
, &env
->sysenter_eip
);
6180 qemu_get_betls(f
, &env
->cr
[0]);
6181 qemu_get_betls(f
, &env
->cr
[2]);
6182 qemu_get_betls(f
, &env
->cr
[3]);
6183 qemu_get_betls(f
, &env
->cr
[4]);
6185 for(i
= 0; i
< 8; i
++)
6186 qemu_get_betls(f
, &env
->dr
[i
]);
6189 qemu_get_be32s(f
, &env
->a20_mask
);
6191 qemu_get_be32s(f
, &env
->mxcsr
);
6192 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
6193 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
6194 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
6197 #ifdef TARGET_X86_64
6198 qemu_get_be64s(f
, &env
->efer
);
6199 qemu_get_be64s(f
, &env
->star
);
6200 qemu_get_be64s(f
, &env
->lstar
);
6201 qemu_get_be64s(f
, &env
->cstar
);
6202 qemu_get_be64s(f
, &env
->fmask
);
6203 qemu_get_be64s(f
, &env
->kernelgsbase
);
6205 if (version_id
>= 4)
6206 qemu_get_be32s(f
, &env
->smbase
);
6208 /* XXX: compute hflags from scratch, except for CPL and IIF */
6209 env
->hflags
= hflags
;
6213 /* when in-kernel irqchip is used, HF_HALTED_MASK causes deadlock
6214 because no userspace IRQs will ever clear this flag */
6215 env
->hflags
&= ~HF_HALTED_MASK
;
6216 for (i
= 0; i
< NR_IRQ_WORDS
; i
++) {
6217 qemu_get_be32s(f
, &env
->kvm_interrupt_bitmap
[i
]);
6219 qemu_get_be64s(f
, &env
->tsc
);
6220 kvm_load_registers(env
);
6226 #elif defined(TARGET_PPC)
6227 void cpu_save(QEMUFile
*f
, void *opaque
)
6231 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6236 #elif defined(TARGET_MIPS)
6237 void cpu_save(QEMUFile
*f
, void *opaque
)
6241 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6246 #elif defined(TARGET_SPARC)
6247 void cpu_save(QEMUFile
*f
, void *opaque
)
6249 CPUState
*env
= opaque
;
6253 for(i
= 0; i
< 8; i
++)
6254 qemu_put_betls(f
, &env
->gregs
[i
]);
6255 for(i
= 0; i
< NWINDOWS
* 16; i
++)
6256 qemu_put_betls(f
, &env
->regbase
[i
]);
6259 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
6265 qemu_put_be32(f
, u
.i
);
6268 qemu_put_betls(f
, &env
->pc
);
6269 qemu_put_betls(f
, &env
->npc
);
6270 qemu_put_betls(f
, &env
->y
);
6272 qemu_put_be32(f
, tmp
);
6273 qemu_put_betls(f
, &env
->fsr
);
6274 qemu_put_betls(f
, &env
->tbr
);
6275 #ifndef TARGET_SPARC64
6276 qemu_put_be32s(f
, &env
->wim
);
6278 for(i
= 0; i
< 16; i
++)
6279 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
6283 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6285 CPUState
*env
= opaque
;
6289 for(i
= 0; i
< 8; i
++)
6290 qemu_get_betls(f
, &env
->gregs
[i
]);
6291 for(i
= 0; i
< NWINDOWS
* 16; i
++)
6292 qemu_get_betls(f
, &env
->regbase
[i
]);
6295 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
6300 u
.i
= qemu_get_be32(f
);
6304 qemu_get_betls(f
, &env
->pc
);
6305 qemu_get_betls(f
, &env
->npc
);
6306 qemu_get_betls(f
, &env
->y
);
6307 tmp
= qemu_get_be32(f
);
6308 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
6309 correctly updated */
6311 qemu_get_betls(f
, &env
->fsr
);
6312 qemu_get_betls(f
, &env
->tbr
);
6313 #ifndef TARGET_SPARC64
6314 qemu_get_be32s(f
, &env
->wim
);
6316 for(i
= 0; i
< 16; i
++)
6317 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
6323 #elif defined(TARGET_ARM)
6325 void cpu_save(QEMUFile
*f
, void *opaque
)
6328 CPUARMState
*env
= (CPUARMState
*)opaque
;
6330 for (i
= 0; i
< 16; i
++) {
6331 qemu_put_be32(f
, env
->regs
[i
]);
6333 qemu_put_be32(f
, cpsr_read(env
));
6334 qemu_put_be32(f
, env
->spsr
);
6335 for (i
= 0; i
< 6; i
++) {
6336 qemu_put_be32(f
, env
->banked_spsr
[i
]);
6337 qemu_put_be32(f
, env
->banked_r13
[i
]);
6338 qemu_put_be32(f
, env
->banked_r14
[i
]);
6340 for (i
= 0; i
< 5; i
++) {
6341 qemu_put_be32(f
, env
->usr_regs
[i
]);
6342 qemu_put_be32(f
, env
->fiq_regs
[i
]);
6344 qemu_put_be32(f
, env
->cp15
.c0_cpuid
);
6345 qemu_put_be32(f
, env
->cp15
.c0_cachetype
);
6346 qemu_put_be32(f
, env
->cp15
.c1_sys
);
6347 qemu_put_be32(f
, env
->cp15
.c1_coproc
);
6348 qemu_put_be32(f
, env
->cp15
.c1_xscaleauxcr
);
6349 qemu_put_be32(f
, env
->cp15
.c2_base
);
6350 qemu_put_be32(f
, env
->cp15
.c2_data
);
6351 qemu_put_be32(f
, env
->cp15
.c2_insn
);
6352 qemu_put_be32(f
, env
->cp15
.c3
);
6353 qemu_put_be32(f
, env
->cp15
.c5_insn
);
6354 qemu_put_be32(f
, env
->cp15
.c5_data
);
6355 for (i
= 0; i
< 8; i
++) {
6356 qemu_put_be32(f
, env
->cp15
.c6_region
[i
]);
6358 qemu_put_be32(f
, env
->cp15
.c6_insn
);
6359 qemu_put_be32(f
, env
->cp15
.c6_data
);
6360 qemu_put_be32(f
, env
->cp15
.c9_insn
);
6361 qemu_put_be32(f
, env
->cp15
.c9_data
);
6362 qemu_put_be32(f
, env
->cp15
.c13_fcse
);
6363 qemu_put_be32(f
, env
->cp15
.c13_context
);
6364 qemu_put_be32(f
, env
->cp15
.c15_cpar
);
6366 qemu_put_be32(f
, env
->features
);
6368 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
6369 for (i
= 0; i
< 16; i
++) {
6371 u
.d
= env
->vfp
.regs
[i
];
6372 qemu_put_be32(f
, u
.l
.upper
);
6373 qemu_put_be32(f
, u
.l
.lower
);
6375 for (i
= 0; i
< 16; i
++) {
6376 qemu_put_be32(f
, env
->vfp
.xregs
[i
]);
6379 /* TODO: Should use proper FPSCR access functions. */
6380 qemu_put_be32(f
, env
->vfp
.vec_len
);
6381 qemu_put_be32(f
, env
->vfp
.vec_stride
);
6384 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
6385 for (i
= 0; i
< 16; i
++) {
6386 qemu_put_be64(f
, env
->iwmmxt
.regs
[i
]);
6388 for (i
= 0; i
< 16; i
++) {
6389 qemu_put_be32(f
, env
->iwmmxt
.cregs
[i
]);
6394 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
6396 CPUARMState
*env
= (CPUARMState
*)opaque
;
6399 if (version_id
!= 0)
6402 for (i
= 0; i
< 16; i
++) {
6403 env
->regs
[i
] = qemu_get_be32(f
);
6405 cpsr_write(env
, qemu_get_be32(f
), 0xffffffff);
6406 env
->spsr
= qemu_get_be32(f
);
6407 for (i
= 0; i
< 6; i
++) {
6408 env
->banked_spsr
[i
] = qemu_get_be32(f
);
6409 env
->banked_r13
[i
] = qemu_get_be32(f
);
6410 env
->banked_r14
[i
] = qemu_get_be32(f
);
6412 for (i
= 0; i
< 5; i
++) {
6413 env
->usr_regs
[i
] = qemu_get_be32(f
);
6414 env
->fiq_regs
[i
] = qemu_get_be32(f
);
6416 env
->cp15
.c0_cpuid
= qemu_get_be32(f
);
6417 env
->cp15
.c0_cachetype
= qemu_get_be32(f
);
6418 env
->cp15
.c1_sys
= qemu_get_be32(f
);
6419 env
->cp15
.c1_coproc
= qemu_get_be32(f
);
6420 env
->cp15
.c1_xscaleauxcr
= qemu_get_be32(f
);
6421 env
->cp15
.c2_base
= qemu_get_be32(f
);
6422 env
->cp15
.c2_data
= qemu_get_be32(f
);
6423 env
->cp15
.c2_insn
= qemu_get_be32(f
);
6424 env
->cp15
.c3
= qemu_get_be32(f
);
6425 env
->cp15
.c5_insn
= qemu_get_be32(f
);
6426 env
->cp15
.c5_data
= qemu_get_be32(f
);
6427 for (i
= 0; i
< 8; i
++) {
6428 env
->cp15
.c6_region
[i
] = qemu_get_be32(f
);
6430 env
->cp15
.c6_insn
= qemu_get_be32(f
);
6431 env
->cp15
.c6_data
= qemu_get_be32(f
);
6432 env
->cp15
.c9_insn
= qemu_get_be32(f
);
6433 env
->cp15
.c9_data
= qemu_get_be32(f
);
6434 env
->cp15
.c13_fcse
= qemu_get_be32(f
);
6435 env
->cp15
.c13_context
= qemu_get_be32(f
);
6436 env
->cp15
.c15_cpar
= qemu_get_be32(f
);
6438 env
->features
= qemu_get_be32(f
);
6440 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
6441 for (i
= 0; i
< 16; i
++) {
6443 u
.l
.upper
= qemu_get_be32(f
);
6444 u
.l
.lower
= qemu_get_be32(f
);
6445 env
->vfp
.regs
[i
] = u
.d
;
6447 for (i
= 0; i
< 16; i
++) {
6448 env
->vfp
.xregs
[i
] = qemu_get_be32(f
);
6451 /* TODO: Should use proper FPSCR access functions. */
6452 env
->vfp
.vec_len
= qemu_get_be32(f
);
6453 env
->vfp
.vec_stride
= qemu_get_be32(f
);
6456 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
6457 for (i
= 0; i
< 16; i
++) {
6458 env
->iwmmxt
.regs
[i
] = qemu_get_be64(f
);
6460 for (i
= 0; i
< 16; i
++) {
6461 env
->iwmmxt
.cregs
[i
] = qemu_get_be32(f
);
6470 #warning No CPU save/restore functions
6474 /***********************************************************/
6475 /* ram save/restore */
6477 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
6481 v
= qemu_get_byte(f
);
6484 if (qemu_get_buffer(f
, buf
, len
) != len
)
6488 v
= qemu_get_byte(f
);
6489 memset(buf
, v
, len
);
6497 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
6501 if (qemu_get_be32(f
) != phys_ram_size
)
6503 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
6505 if (kvm_allowed
&& (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
6508 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
6515 #define BDRV_HASH_BLOCK_SIZE 1024
6516 #define IOBUF_SIZE 4096
6517 #define RAM_CBLOCK_MAGIC 0xfabe
6519 typedef struct RamCompressState
{
6522 uint8_t buf
[IOBUF_SIZE
];
6525 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
6528 memset(s
, 0, sizeof(*s
));
6530 ret
= deflateInit2(&s
->zstream
, 1,
6532 9, Z_DEFAULT_STRATEGY
);
6535 s
->zstream
.avail_out
= IOBUF_SIZE
;
6536 s
->zstream
.next_out
= s
->buf
;
6540 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
6542 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
6543 qemu_put_be16(s
->f
, len
);
6544 qemu_put_buffer(s
->f
, buf
, len
);
6547 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
6551 s
->zstream
.avail_in
= len
;
6552 s
->zstream
.next_in
= (uint8_t *)buf
;
6553 while (s
->zstream
.avail_in
> 0) {
6554 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
6557 if (s
->zstream
.avail_out
== 0) {
6558 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
6559 s
->zstream
.avail_out
= IOBUF_SIZE
;
6560 s
->zstream
.next_out
= s
->buf
;
6566 static void ram_compress_close(RamCompressState
*s
)
6570 /* compress last bytes */
6572 ret
= deflate(&s
->zstream
, Z_FINISH
);
6573 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
6574 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
6576 ram_put_cblock(s
, s
->buf
, len
);
6578 s
->zstream
.avail_out
= IOBUF_SIZE
;
6579 s
->zstream
.next_out
= s
->buf
;
6580 if (ret
== Z_STREAM_END
)
6587 deflateEnd(&s
->zstream
);
6590 typedef struct RamDecompressState
{
6593 uint8_t buf
[IOBUF_SIZE
];
6594 } RamDecompressState
;
6596 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
6599 memset(s
, 0, sizeof(*s
));
6601 ret
= inflateInit(&s
->zstream
);
6607 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
6611 s
->zstream
.avail_out
= len
;
6612 s
->zstream
.next_out
= buf
;
6613 while (s
->zstream
.avail_out
> 0) {
6614 if (s
->zstream
.avail_in
== 0) {
6615 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
6617 clen
= qemu_get_be16(s
->f
);
6618 if (clen
> IOBUF_SIZE
)
6620 qemu_get_buffer(s
->f
, s
->buf
, clen
);
6621 s
->zstream
.avail_in
= clen
;
6622 s
->zstream
.next_in
= s
->buf
;
6624 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
6625 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
6632 static void ram_decompress_close(RamDecompressState
*s
)
6634 inflateEnd(&s
->zstream
);
6637 static void ram_save_live(QEMUFile
*f
, void *opaque
)
6641 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
6643 if (kvm_allowed
&& (addr
>=0xa0000) && (addr
<0xc0000)) /* do not access video-addresses */
6646 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
)) {
6647 qemu_put_be32(f
, addr
);
6648 qemu_put_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
6651 qemu_put_be32(f
, 1);
6654 static void ram_save_static(QEMUFile
*f
, void *opaque
)
6657 RamCompressState s1
, *s
= &s1
;
6660 qemu_put_be32(f
, phys_ram_size
);
6661 if (ram_compress_open(s
, f
) < 0)
6663 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6665 if (kvm_allowed
&& (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
6669 if (tight_savevm_enabled
) {
6673 /* find if the memory block is available on a virtual
6676 for(j
= 0; j
< MAX_DISKS
; j
++) {
6678 sector_num
= bdrv_hash_find(bs_table
[j
],
6679 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6680 if (sector_num
>= 0)
6685 goto normal_compress
;
6688 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
6689 ram_compress_buf(s
, buf
, 10);
6695 ram_compress_buf(s
, buf
, 1);
6696 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6699 ram_compress_close(s
);
6702 static void ram_save(QEMUFile
*f
, void *opaque
)
6704 int in_migration
= cpu_physical_memory_get_dirty_tracking();
6706 qemu_put_byte(f
, in_migration
);
6709 ram_save_live(f
, opaque
);
6711 ram_save_static(f
, opaque
);
6714 static int ram_load_live(QEMUFile
*f
, void *opaque
)
6719 addr
= qemu_get_be32(f
);
6723 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
6729 static int ram_load_static(QEMUFile
*f
, void *opaque
)
6731 RamDecompressState s1
, *s
= &s1
;
6735 if (qemu_get_be32(f
) != phys_ram_size
)
6737 if (ram_decompress_open(s
, f
) < 0)
6739 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6741 if (kvm_allowed
&& (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
6744 if (ram_decompress_buf(s
, buf
, 1) < 0) {
6745 fprintf(stderr
, "Error while reading ram block header\n");
6749 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
6750 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
6759 ram_decompress_buf(s
, buf
+ 1, 9);
6761 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
6762 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
6763 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
6766 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
6767 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
6768 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
6769 bs_index
, sector_num
);
6776 printf("Error block header\n");
6780 ram_decompress_close(s
);
6784 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
6788 switch (version_id
) {
6790 ret
= ram_load_v1(f
, opaque
);
6793 if (qemu_get_byte(f
)) {
6794 ret
= ram_load_live(f
, opaque
);
6798 ret
= ram_load_static(f
, opaque
);
6808 /***********************************************************/
6809 /* bottom halves (can be seen as timers which expire ASAP) */
6818 static QEMUBH
*first_bh
= NULL
;
6820 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
6823 bh
= qemu_mallocz(sizeof(QEMUBH
));
6827 bh
->opaque
= opaque
;
6831 int qemu_bh_poll(void)
6850 void qemu_bh_schedule(QEMUBH
*bh
)
6852 CPUState
*env
= cpu_single_env
;
6856 bh
->next
= first_bh
;
6859 /* stop the currently executing CPU to execute the BH ASAP */
6861 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
6865 void qemu_bh_cancel(QEMUBH
*bh
)
6868 if (bh
->scheduled
) {
6871 pbh
= &(*pbh
)->next
;
6877 void qemu_bh_delete(QEMUBH
*bh
)
6883 /***********************************************************/
6884 /* machine registration */
6886 QEMUMachine
*first_machine
= NULL
;
6888 int qemu_register_machine(QEMUMachine
*m
)
6891 pm
= &first_machine
;
6899 QEMUMachine
*find_machine(const char *name
)
6903 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6904 if (!strcmp(m
->name
, name
))
6910 /***********************************************************/
6911 /* main execution loop */
6913 void gui_update(void *opaque
)
6915 DisplayState
*ds
= opaque
;
6916 ds
->dpy_refresh(ds
);
6917 qemu_mod_timer(ds
->gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
6920 struct vm_change_state_entry
{
6921 VMChangeStateHandler
*cb
;
6923 LIST_ENTRY (vm_change_state_entry
) entries
;
6926 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
6928 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
6931 VMChangeStateEntry
*e
;
6933 e
= qemu_mallocz(sizeof (*e
));
6939 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
6943 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
6945 LIST_REMOVE (e
, entries
);
6949 static void vm_state_notify(int running
)
6951 VMChangeStateEntry
*e
;
6953 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
6954 e
->cb(e
->opaque
, running
);
6958 /* XXX: support several handlers */
6959 static VMStopHandler
*vm_stop_cb
;
6960 static void *vm_stop_opaque
;
6962 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6965 vm_stop_opaque
= opaque
;
6969 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6980 qemu_rearm_alarm_timer(alarm_timer
);
6984 void vm_stop(int reason
)
6987 cpu_disable_ticks();
6991 vm_stop_cb(vm_stop_opaque
, reason
);
6998 /* reset/shutdown handler */
7000 typedef struct QEMUResetEntry
{
7001 QEMUResetHandler
*func
;
7003 struct QEMUResetEntry
*next
;
7006 static QEMUResetEntry
*first_reset_entry
;
7007 static int reset_requested
;
7008 static int shutdown_requested
;
7009 static int powerdown_requested
;
7011 int qemu_shutdown_requested(void)
7013 int r
= shutdown_requested
;
7014 shutdown_requested
= 0;
7018 int qemu_reset_requested(void)
7020 int r
= reset_requested
;
7021 reset_requested
= 0;
7025 int qemu_powerdown_requested(void)
7027 int r
= powerdown_requested
;
7028 powerdown_requested
= 0;
7032 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
7034 QEMUResetEntry
**pre
, *re
;
7036 pre
= &first_reset_entry
;
7037 while (*pre
!= NULL
)
7038 pre
= &(*pre
)->next
;
7039 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
7041 re
->opaque
= opaque
;
7046 void qemu_system_reset(void)
7050 /* reset all devices */
7051 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
7052 re
->func(re
->opaque
);
7056 void qemu_system_reset_request(void)
7059 shutdown_requested
= 1;
7061 reset_requested
= 1;
7064 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
7067 void qemu_system_shutdown_request(void)
7069 shutdown_requested
= 1;
7071 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
7074 void qemu_system_powerdown_request(void)
7076 powerdown_requested
= 1;
7078 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
7081 void main_loop_wait(int timeout
)
7083 IOHandlerRecord
*ioh
;
7084 fd_set rfds
, wfds
, xfds
;
7093 /* XXX: need to suppress polling by better using win32 events */
7095 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
7096 ret
|= pe
->func(pe
->opaque
);
7101 WaitObjects
*w
= &wait_objects
;
7103 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
7104 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
7105 if (w
->func
[ret
- WAIT_OBJECT_0
])
7106 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
7108 /* Check for additional signaled events */
7109 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
7111 /* Check if event is signaled */
7112 ret2
= WaitForSingleObject(w
->events
[i
], 0);
7113 if(ret2
== WAIT_OBJECT_0
) {
7115 w
->func
[i
](w
->opaque
[i
]);
7116 } else if (ret2
== WAIT_TIMEOUT
) {
7118 err
= GetLastError();
7119 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
7122 } else if (ret
== WAIT_TIMEOUT
) {
7124 err
= GetLastError();
7125 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
7129 /* poll any events */
7130 /* XXX: separate device handlers from system ones */
7135 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
7139 (!ioh
->fd_read_poll
||
7140 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
7141 FD_SET(ioh
->fd
, &rfds
);
7145 if (ioh
->fd_write
) {
7146 FD_SET(ioh
->fd
, &wfds
);
7156 tv
.tv_usec
= timeout
* 1000;
7158 #if defined(CONFIG_SLIRP)
7160 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
7163 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
7165 IOHandlerRecord
**pioh
;
7167 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
7168 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
7169 ioh
->fd_read(ioh
->opaque
);
7171 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
7172 ioh
->fd_write(ioh
->opaque
);
7176 /* remove deleted IO handlers */
7177 pioh
= &first_io_handler
;
7187 #if defined(CONFIG_SLIRP)
7194 slirp_select_poll(&rfds
, &wfds
, &xfds
);
7200 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
7201 qemu_get_clock(vm_clock
));
7202 /* run dma transfers, if any */
7206 /* real time timers */
7207 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
7208 qemu_get_clock(rt_clock
));
7210 /* Check bottom-halves last in case any of the earlier events triggered
7216 static CPUState
*cur_cpu
;
7221 #ifdef CONFIG_PROFILER
7230 cpu_disable_ticks();
7234 cur_cpu
= first_cpu
;
7241 env
= env
->next_cpu
;
7244 #ifdef CONFIG_PROFILER
7245 ti
= profile_getclock();
7247 ret
= cpu_exec(env
);
7248 #ifdef CONFIG_PROFILER
7249 qemu_time
+= profile_getclock() - ti
;
7251 if (ret
== EXCP_HLT
) {
7252 /* Give the next CPU a chance to run. */
7256 if (ret
!= EXCP_HALTED
)
7258 /* all CPUs are halted ? */
7264 if (shutdown_requested
) {
7265 ret
= EXCP_INTERRUPT
;
7268 if (reset_requested
) {
7269 reset_requested
= 0;
7270 qemu_system_reset();
7273 kvm_load_registers(env
);
7275 ret
= EXCP_INTERRUPT
;
7277 if (powerdown_requested
) {
7278 powerdown_requested
= 0;
7279 qemu_system_powerdown();
7280 ret
= EXCP_INTERRUPT
;
7282 if (ret
== EXCP_DEBUG
) {
7283 vm_stop(EXCP_DEBUG
);
7285 /* If all cpus are halted then wait until the next IRQ */
7286 /* XXX: use timeout computed from timers */
7287 if (ret
== EXCP_HALTED
)
7294 #ifdef CONFIG_PROFILER
7295 ti
= profile_getclock();
7297 main_loop_wait(timeout
);
7298 #ifdef CONFIG_PROFILER
7299 dev_time
+= profile_getclock() - ti
;
7302 cpu_disable_ticks();
7306 static void help(int exitcode
)
7308 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2007 Fabrice Bellard\n"
7309 "usage: %s [options] [disk_image]\n"
7311 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
7313 "Standard options:\n"
7314 "-M machine select emulated machine (-M ? for list)\n"
7315 "-cpu cpu select CPU (-cpu ? for list)\n"
7316 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
7317 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
7318 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
7319 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
7320 "-mtdblock file use 'file' as on-board Flash memory image\n"
7321 "-sd file use 'file' as SecureDigital card image\n"
7322 "-pflash file use 'file' as a parallel flash image\n"
7323 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
7324 "-snapshot write to temporary files instead of disk image files\n"
7326 "-no-frame open SDL window without a frame and window decorations\n"
7327 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
7328 "-no-quit disable SDL window close capability\n"
7331 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
7333 "-m megs set virtual RAM size to megs MB [default=%d]\n"
7334 "-smp n set the number of CPUs to 'n' [default=1]\n"
7335 "-nographic disable graphical output and redirect serial I/Os to console\n"
7336 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
7338 "-k language use keyboard layout (for example \"fr\" for French)\n"
7341 "-audio-help print list of audio drivers and their options\n"
7342 "-soundhw c1,... enable audio support\n"
7343 " and only specified sound cards (comma separated list)\n"
7344 " use -soundhw ? to get the list of supported cards\n"
7345 " use -soundhw all to enable all of them\n"
7347 "-localtime set the real time clock to local time [default=utc]\n"
7348 "-full-screen start in full screen\n"
7350 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
7352 "-usb enable the USB driver (will be the default soon)\n"
7353 "-usbdevice name add the host or guest USB device 'name'\n"
7354 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7355 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
7357 "-name string set the name of the guest\n"
7359 "Network options:\n"
7360 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
7361 " create a new Network Interface Card and connect it to VLAN 'n'\n"
7363 "-net user[,vlan=n][,hostname=host]\n"
7364 " connect the user mode network stack to VLAN 'n' and send\n"
7365 " hostname 'host' to DHCP clients\n"
7368 "-net tap[,vlan=n],ifname=name\n"
7369 " connect the host TAP network interface to VLAN 'n'\n"
7371 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
7372 " connect the host TAP network interface to VLAN 'n' and use\n"
7373 " the network script 'file' (default=%s);\n"
7374 " use 'script=no' to disable script execution;\n"
7375 " use 'fd=h' to connect to an already opened TAP interface\n"
7377 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
7378 " connect the vlan 'n' to another VLAN using a socket connection\n"
7379 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
7380 " connect the vlan 'n' to multicast maddr and port\n"
7381 "-net none use it alone to have zero network devices; if no -net option\n"
7382 " is provided, the default is '-net nic -net user'\n"
7385 "-tftp dir allow tftp access to files in dir [-net user]\n"
7386 "-bootp file advertise file in BOOTP replies\n"
7388 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
7390 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
7391 " redirect TCP or UDP connections from host to guest [-net user]\n"
7394 "Linux boot specific:\n"
7395 "-kernel bzImage use 'bzImage' as kernel image\n"
7396 "-append cmdline use 'cmdline' as kernel command line\n"
7397 "-initrd file use 'file' as initial ram disk\n"
7399 "Debug/Expert options:\n"
7400 "-monitor dev redirect the monitor to char device 'dev'\n"
7401 "-vmchannel di:DI,dev redirect the hypercall device with device id DI, to char device 'dev'\n"
7402 "-balloon dev redirect the balloon hypercall device to char device 'dev'\n"
7403 "-serial dev redirect the serial port to char device 'dev'\n"
7404 "-parallel dev redirect the parallel port to char device 'dev'\n"
7405 "-pidfile file Write PID to 'file'\n"
7406 "-S freeze CPU at startup (use 'c' to start execution)\n"
7407 "-s wait gdb connection to port\n"
7408 "-p port set gdb connection port [default=%s]\n"
7409 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
7410 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
7411 " translation (t=none or lba) (usually qemu can guess them)\n"
7412 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
7414 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
7415 "-no-kqemu disable KQEMU kernel module usage\n"
7418 "-no-kvm disable KVM hardware virtualization\n"
7419 "-no-kvm-irqchip disable KVM kernel mode PIC/IOAPIC/LAPIC\n"
7421 #ifdef USE_CODE_COPY
7422 "-no-code-copy disable code copy acceleration\n"
7425 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
7426 " (default is CL-GD5446 PCI VGA)\n"
7427 "-no-acpi disable ACPI\n"
7429 "-no-reboot exit instead of rebooting\n"
7430 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
7431 "-vnc display start a VNC server on display\n"
7433 "-daemonize daemonize QEMU after initializing\n"
7435 "-tdf inject timer interrupts that got lost\n"
7436 "-kvm-shadow-memory megs set the amount of shadow pages to be allocated\n"
7437 "-option-rom rom load a file, rom, into the option ROM space\n"
7439 "-prom-env variable=value set OpenBIOS nvram variables\n"
7441 "-clock force the use of the given methods for timer alarm.\n"
7442 " To see what timers are available use -clock help\n"
7444 "During emulation, the following keys are useful:\n"
7445 "ctrl-alt-f toggle full screen\n"
7446 "ctrl-alt-n switch to virtual console 'n'\n"
7447 "ctrl-alt toggle mouse and keyboard grab\n"
7449 "When using -nographic, press 'ctrl-a h' to get some help.\n"
7454 DEFAULT_NETWORK_SCRIPT
,
7456 DEFAULT_GDBSTUB_PORT
,
7461 #define HAS_ARG 0x0001
7475 QEMU_OPTION_mtdblock
,
7479 QEMU_OPTION_snapshot
,
7481 QEMU_OPTION_no_fd_bootchk
,
7484 QEMU_OPTION_nographic
,
7485 QEMU_OPTION_portrait
,
7487 QEMU_OPTION_audio_help
,
7488 QEMU_OPTION_soundhw
,
7507 QEMU_OPTION_no_code_copy
,
7509 QEMU_OPTION_localtime
,
7510 QEMU_OPTION_cirrusvga
,
7513 QEMU_OPTION_std_vga
,
7515 QEMU_OPTION_monitor
,
7516 QEMU_OPTION_balloon
,
7517 QEMU_OPTION_vmchannel
,
7519 QEMU_OPTION_parallel
,
7521 QEMU_OPTION_full_screen
,
7522 QEMU_OPTION_no_frame
,
7523 QEMU_OPTION_alt_grab
,
7524 QEMU_OPTION_no_quit
,
7525 QEMU_OPTION_pidfile
,
7526 QEMU_OPTION_no_kqemu
,
7527 QEMU_OPTION_kernel_kqemu
,
7528 QEMU_OPTION_win2k_hack
,
7530 QEMU_OPTION_usbdevice
,
7533 QEMU_OPTION_no_acpi
,
7535 QEMU_OPTION_no_kvm_irqchip
,
7536 QEMU_OPTION_no_reboot
,
7537 QEMU_OPTION_show_cursor
,
7538 QEMU_OPTION_daemonize
,
7539 QEMU_OPTION_option_rom
,
7540 QEMU_OPTION_semihosting
,
7541 QEMU_OPTION_cpu_vendor
,
7543 QEMU_OPTION_prom_env
,
7544 QEMU_OPTION_old_param
,
7546 QEMU_OPTION_incoming
,
7548 QEMU_OPTION_kvm_shadow_memory
,
7551 typedef struct QEMUOption
{
7557 const QEMUOption qemu_options
[] = {
7558 { "h", 0, QEMU_OPTION_h
},
7559 { "help", 0, QEMU_OPTION_h
},
7561 { "M", HAS_ARG
, QEMU_OPTION_M
},
7562 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
7563 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
7564 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
7565 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
7566 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
7567 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
7568 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
7569 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
7570 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
7571 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
7572 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
7573 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
7574 { "snapshot", 0, QEMU_OPTION_snapshot
},
7576 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
7578 { "m", HAS_ARG
, QEMU_OPTION_m
},
7579 { "nographic", 0, QEMU_OPTION_nographic
},
7580 { "portrait", 0, QEMU_OPTION_portrait
},
7581 { "k", HAS_ARG
, QEMU_OPTION_k
},
7583 { "audio-help", 0, QEMU_OPTION_audio_help
},
7584 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
7587 { "net", HAS_ARG
, QEMU_OPTION_net
},
7589 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
7590 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
7592 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
7594 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
7597 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
7598 { "append", HAS_ARG
, QEMU_OPTION_append
},
7599 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
7601 { "S", 0, QEMU_OPTION_S
},
7602 { "s", 0, QEMU_OPTION_s
},
7603 { "p", HAS_ARG
, QEMU_OPTION_p
},
7604 { "d", HAS_ARG
, QEMU_OPTION_d
},
7605 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
7606 { "L", HAS_ARG
, QEMU_OPTION_L
},
7607 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
7609 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
7610 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
7613 { "no-kvm", 0, QEMU_OPTION_no_kvm
},
7614 { "no-kvm-irqchip", 0, QEMU_OPTION_no_kvm_irqchip
},
7616 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
7617 { "g", 1, QEMU_OPTION_g
},
7619 { "localtime", 0, QEMU_OPTION_localtime
},
7620 { "std-vga", 0, QEMU_OPTION_std_vga
},
7621 { "monitor", 1, QEMU_OPTION_monitor
},
7622 { "balloon", 1, QEMU_OPTION_balloon
},
7623 { "vmchannel", 1, QEMU_OPTION_vmchannel
},
7624 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
7625 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
7626 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
7627 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
7628 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
7629 { "incoming", 1, QEMU_OPTION_incoming
},
7630 { "full-screen", 0, QEMU_OPTION_full_screen
},
7632 { "no-frame", 0, QEMU_OPTION_no_frame
},
7633 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
7634 { "no-quit", 0, QEMU_OPTION_no_quit
},
7636 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
7637 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
7638 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
7639 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
7640 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
7642 /* temporary options */
7643 { "usb", 0, QEMU_OPTION_usb
},
7644 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
7645 { "vmwarevga", 0, QEMU_OPTION_vmsvga
},
7646 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
7647 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
7648 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
7649 { "daemonize", 0, QEMU_OPTION_daemonize
},
7650 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
7651 #if defined(TARGET_ARM) || defined(TARGET_M68K)
7652 { "semihosting", 0, QEMU_OPTION_semihosting
},
7654 { "tdf", 0, QEMU_OPTION_tdf
}, /* enable time drift fix */
7655 { "kvm-shadow-memory", HAS_ARG
, QEMU_OPTION_kvm_shadow_memory
},
7656 { "name", HAS_ARG
, QEMU_OPTION_name
},
7657 #if defined(TARGET_SPARC)
7658 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
7660 { "cpu-vendor", HAS_ARG
, QEMU_OPTION_cpu_vendor
},
7661 #if defined(TARGET_ARM)
7662 { "old-param", 0, QEMU_OPTION_old_param
},
7664 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
7668 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
7670 /* this stack is only used during signal handling */
7671 #define SIGNAL_STACK_SIZE 32768
7673 static uint8_t *signal_stack
;
7677 /* password input */
7679 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
7684 if (!bdrv_is_encrypted(bs
))
7687 term_printf("%s is encrypted.\n", name
);
7688 for(i
= 0; i
< 3; i
++) {
7689 monitor_readline("Password: ", 1, password
, sizeof(password
));
7690 if (bdrv_set_key(bs
, password
) == 0)
7692 term_printf("invalid password\n");
7697 static BlockDriverState
*get_bdrv(int index
)
7699 BlockDriverState
*bs
;
7702 bs
= bs_table
[index
];
7703 } else if (index
< 6) {
7704 bs
= fd_table
[index
- 4];
7711 static void read_passwords(void)
7713 BlockDriverState
*bs
;
7716 for(i
= 0; i
< 6; i
++) {
7719 qemu_key_check(bs
, bdrv_get_device_name(bs
));
7723 /* XXX: currently we cannot use simultaneously different CPUs */
7724 void register_machines(void)
7726 #if defined(TARGET_I386)
7727 qemu_register_machine(&pc_machine
);
7728 qemu_register_machine(&isapc_machine
);
7729 #elif defined(TARGET_PPC)
7730 qemu_register_machine(&heathrow_machine
);
7731 qemu_register_machine(&core99_machine
);
7732 qemu_register_machine(&prep_machine
);
7733 qemu_register_machine(&ref405ep_machine
);
7734 qemu_register_machine(&taihu_machine
);
7735 #elif defined(TARGET_MIPS)
7736 qemu_register_machine(&mips_machine
);
7737 qemu_register_machine(&mips_malta_machine
);
7738 qemu_register_machine(&mips_pica61_machine
);
7739 #elif defined(TARGET_SPARC)
7740 #ifdef TARGET_SPARC64
7741 qemu_register_machine(&sun4u_machine
);
7743 qemu_register_machine(&ss5_machine
);
7744 qemu_register_machine(&ss10_machine
);
7746 #elif defined(TARGET_ARM)
7747 qemu_register_machine(&integratorcp_machine
);
7748 qemu_register_machine(&versatilepb_machine
);
7749 qemu_register_machine(&versatileab_machine
);
7750 qemu_register_machine(&realview_machine
);
7751 qemu_register_machine(&akitapda_machine
);
7752 qemu_register_machine(&spitzpda_machine
);
7753 qemu_register_machine(&borzoipda_machine
);
7754 qemu_register_machine(&terrierpda_machine
);
7755 qemu_register_machine(&palmte_machine
);
7756 #elif defined(TARGET_SH4)
7757 qemu_register_machine(&shix_machine
);
7758 qemu_register_machine(&r2d_machine
);
7759 #elif defined(TARGET_ALPHA)
7761 #elif defined(TARGET_M68K)
7762 qemu_register_machine(&mcf5208evb_machine
);
7763 qemu_register_machine(&an5206_machine
);
7765 #error unsupported CPU
7770 struct soundhw soundhw
[] = {
7771 #ifdef HAS_AUDIO_CHOICE
7778 { .init_isa
= pcspk_audio_init
}
7783 "Creative Sound Blaster 16",
7786 { .init_isa
= SB16_init
}
7793 "Yamaha YMF262 (OPL3)",
7795 "Yamaha YM3812 (OPL2)",
7799 { .init_isa
= Adlib_init
}
7806 "Gravis Ultrasound GF1",
7809 { .init_isa
= GUS_init
}
7815 "ENSONIQ AudioPCI ES1370",
7818 { .init_pci
= es1370_init
}
7822 { NULL
, NULL
, 0, 0, { NULL
} }
7825 static void select_soundhw (const char *optarg
)
7829 if (*optarg
== '?') {
7832 printf ("Valid sound card names (comma separated):\n");
7833 for (c
= soundhw
; c
->name
; ++c
) {
7834 printf ("%-11s %s\n", c
->name
, c
->descr
);
7836 printf ("\n-soundhw all will enable all of the above\n");
7837 exit (*optarg
!= '?');
7845 if (!strcmp (optarg
, "all")) {
7846 for (c
= soundhw
; c
->name
; ++c
) {
7854 e
= strchr (p
, ',');
7855 l
= !e
? strlen (p
) : (size_t) (e
- p
);
7857 for (c
= soundhw
; c
->name
; ++c
) {
7858 if (!strncmp (c
->name
, p
, l
)) {
7867 "Unknown sound card name (too big to show)\n");
7870 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
7875 p
+= l
+ (e
!= NULL
);
7879 goto show_valid_cards
;
7885 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
7887 exit(STATUS_CONTROL_C_EXIT
);
7892 #define MAX_NET_CLIENTS 32
7894 static int saved_argc
;
7895 static char **saved_argv
;
7897 void qemu_get_launch_info(int *argc
, char ***argv
, int *opt_daemonize
, const char **opt_incoming
)
7901 *opt_daemonize
= daemonize
;
7902 *opt_incoming
= incoming
;
7905 int main(int argc
, char **argv
)
7907 #ifdef CONFIG_GDBSTUB
7909 const char *gdbstub_port
;
7911 int i
, cdrom_index
, pflash_index
;
7912 int snapshot
, linux_boot
;
7913 const char *initrd_filename
;
7914 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
7915 const char *pflash_filename
[MAX_PFLASH
];
7916 const char *sd_filename
;
7917 const char *mtd_filename
;
7918 const char *kernel_filename
, *kernel_cmdline
;
7919 DisplayState
*ds
= &display_state
;
7920 int cyls
, heads
, secs
, translation
;
7921 char net_clients
[MAX_NET_CLIENTS
][256];
7924 const char *r
, *optarg
;
7925 CharDriverState
*monitor_hd
;
7926 char monitor_device
[128];
7927 char vmchannel_devices
[MAX_VMCHANNEL_DEVICES
][128];
7928 int vmchannel_device_index
;
7929 char serial_devices
[MAX_SERIAL_PORTS
][128];
7930 int serial_device_index
;
7931 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
7932 int parallel_device_index
;
7933 const char *loadvm
= NULL
;
7934 QEMUMachine
*machine
;
7935 const char *cpu_model
;
7936 char usb_devices
[MAX_USB_CMDLINE
][128];
7937 int usb_devices_index
;
7939 const char *pid_file
= NULL
;
7945 LIST_INIT (&vm_change_state_head
);
7948 struct sigaction act
;
7949 sigfillset(&act
.sa_mask
);
7951 act
.sa_handler
= SIG_IGN
;
7952 sigaction(SIGPIPE
, &act
, NULL
);
7955 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
7956 /* Note: cpu_interrupt() is currently not SMP safe, so we force
7957 QEMU to run on a single CPU */
7962 h
= GetCurrentProcess();
7963 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
7964 for(i
= 0; i
< 32; i
++) {
7965 if (mask
& (1 << i
))
7970 SetProcessAffinityMask(h
, mask
);
7976 register_machines();
7977 machine
= first_machine
;
7979 initrd_filename
= NULL
;
7980 for(i
= 0; i
< MAX_FD
; i
++)
7981 fd_filename
[i
] = NULL
;
7982 for(i
= 0; i
< MAX_DISKS
; i
++)
7983 hd_filename
[i
] = NULL
;
7984 for(i
= 0; i
< MAX_PFLASH
; i
++)
7985 pflash_filename
[i
] = NULL
;
7988 mtd_filename
= NULL
;
7989 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
7990 vga_ram_size
= VGA_RAM_SIZE
;
7991 #ifdef CONFIG_GDBSTUB
7993 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
7997 kernel_filename
= NULL
;
7998 kernel_cmdline
= "";
8004 cyls
= heads
= secs
= 0;
8005 translation
= BIOS_ATA_TRANSLATION_AUTO
;
8006 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
8008 for(i
= 0; i
< MAX_VMCHANNEL_DEVICES
; i
++)
8009 vmchannel_devices
[i
][0] = '\0';
8010 vmchannel_device_index
= 0;
8012 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
8013 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
8014 serial_devices
[i
][0] = '\0';
8015 serial_device_index
= 0;
8017 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
8018 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
8019 parallel_devices
[i
][0] = '\0';
8020 parallel_device_index
= 0;
8022 usb_devices_index
= 0;
8027 /* default mac address of the first network interface */
8035 hd_filename
[0] = argv
[optind
++];
8037 const QEMUOption
*popt
;
8040 /* Treat --foo the same as -foo. */
8043 popt
= qemu_options
;
8046 fprintf(stderr
, "%s: invalid option -- '%s'\n",
8050 if (!strcmp(popt
->name
, r
+ 1))
8054 if (popt
->flags
& HAS_ARG
) {
8055 if (optind
>= argc
) {
8056 fprintf(stderr
, "%s: option '%s' requires an argument\n",
8060 optarg
= argv
[optind
++];
8065 switch(popt
->index
) {
8067 machine
= find_machine(optarg
);
8070 printf("Supported machines are:\n");
8071 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
8072 printf("%-10s %s%s\n",
8074 m
== first_machine
? " (default)" : "");
8076 exit(*optarg
!= '?');
8079 case QEMU_OPTION_cpu
:
8080 /* hw initialization will check this */
8081 if (*optarg
== '?') {
8082 #if defined(TARGET_PPC)
8083 ppc_cpu_list(stdout
, &fprintf
);
8084 #elif defined(TARGET_ARM)
8086 #elif defined(TARGET_MIPS)
8087 mips_cpu_list(stdout
, &fprintf
);
8088 #elif defined(TARGET_SPARC)
8089 sparc_cpu_list(stdout
, &fprintf
);
8096 case QEMU_OPTION_initrd
:
8097 initrd_filename
= optarg
;
8099 case QEMU_OPTION_hda
:
8100 case QEMU_OPTION_hdb
:
8101 case QEMU_OPTION_hdc
:
8102 case QEMU_OPTION_hdd
:
8105 hd_index
= popt
->index
- QEMU_OPTION_hda
;
8106 hd_filename
[hd_index
] = optarg
;
8107 if (hd_index
== cdrom_index
)
8111 case QEMU_OPTION_mtdblock
:
8112 mtd_filename
= optarg
;
8114 case QEMU_OPTION_sd
:
8115 sd_filename
= optarg
;
8117 case QEMU_OPTION_pflash
:
8118 if (pflash_index
>= MAX_PFLASH
) {
8119 fprintf(stderr
, "qemu: too many parallel flash images\n");
8122 pflash_filename
[pflash_index
++] = optarg
;
8124 case QEMU_OPTION_snapshot
:
8127 case QEMU_OPTION_hdachs
:
8131 cyls
= strtol(p
, (char **)&p
, 0);
8132 if (cyls
< 1 || cyls
> 16383)
8137 heads
= strtol(p
, (char **)&p
, 0);
8138 if (heads
< 1 || heads
> 16)
8143 secs
= strtol(p
, (char **)&p
, 0);
8144 if (secs
< 1 || secs
> 63)
8148 if (!strcmp(p
, "none"))
8149 translation
= BIOS_ATA_TRANSLATION_NONE
;
8150 else if (!strcmp(p
, "lba"))
8151 translation
= BIOS_ATA_TRANSLATION_LBA
;
8152 else if (!strcmp(p
, "auto"))
8153 translation
= BIOS_ATA_TRANSLATION_AUTO
;
8156 } else if (*p
!= '\0') {
8158 fprintf(stderr
, "qemu: invalid physical CHS format\n");
8163 case QEMU_OPTION_nographic
:
8164 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
8165 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "null");
8166 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
8169 case QEMU_OPTION_portrait
:
8172 case QEMU_OPTION_kernel
:
8173 kernel_filename
= optarg
;
8175 case QEMU_OPTION_append
:
8176 kernel_cmdline
= optarg
;
8178 case QEMU_OPTION_cdrom
:
8179 if (cdrom_index
>= 0) {
8180 hd_filename
[cdrom_index
] = optarg
;
8183 case QEMU_OPTION_boot
:
8184 boot_device
= optarg
[0];
8185 if (boot_device
!= 'a' &&
8186 #if defined(TARGET_SPARC) || defined(TARGET_I386)
8188 boot_device
!= 'n' &&
8190 boot_device
!= 'c' && boot_device
!= 'd') {
8191 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
8195 case QEMU_OPTION_fda
:
8196 fd_filename
[0] = optarg
;
8198 case QEMU_OPTION_fdb
:
8199 fd_filename
[1] = optarg
;
8202 case QEMU_OPTION_no_fd_bootchk
:
8206 case QEMU_OPTION_no_code_copy
:
8207 code_copy_enabled
= 0;
8209 case QEMU_OPTION_net
:
8210 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
8211 fprintf(stderr
, "qemu: too many network clients\n");
8214 pstrcpy(net_clients
[nb_net_clients
],
8215 sizeof(net_clients
[0]),
8220 case QEMU_OPTION_tftp
:
8221 tftp_prefix
= optarg
;
8223 case QEMU_OPTION_bootp
:
8224 bootp_filename
= optarg
;
8227 case QEMU_OPTION_smb
:
8228 net_slirp_smb(optarg
);
8231 case QEMU_OPTION_redir
:
8232 net_slirp_redir(optarg
);
8236 case QEMU_OPTION_audio_help
:
8240 case QEMU_OPTION_soundhw
:
8241 select_soundhw (optarg
);
8248 ram_size
= (int64_t)atoi(optarg
) * 1024 * 1024;
8251 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
8252 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
8253 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
8262 mask
= cpu_str_to_log_mask(optarg
);
8264 printf("Log items (comma separated):\n");
8265 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
8266 printf("%-10s %s\n", item
->name
, item
->help
);
8273 #ifdef CONFIG_GDBSTUB
8278 gdbstub_port
= optarg
;
8288 keyboard_layout
= optarg
;
8290 case QEMU_OPTION_localtime
:
8293 case QEMU_OPTION_cirrusvga
:
8294 cirrus_vga_enabled
= 1;
8297 case QEMU_OPTION_vmsvga
:
8298 cirrus_vga_enabled
= 0;
8301 case QEMU_OPTION_std_vga
:
8302 cirrus_vga_enabled
= 0;
8310 w
= strtol(p
, (char **)&p
, 10);
8313 fprintf(stderr
, "qemu: invalid resolution or depth\n");
8319 h
= strtol(p
, (char **)&p
, 10);
8324 depth
= strtol(p
, (char **)&p
, 10);
8325 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
8326 depth
!= 24 && depth
!= 32)
8328 } else if (*p
== '\0') {
8329 depth
= graphic_depth
;
8336 graphic_depth
= depth
;
8339 case QEMU_OPTION_echr
:
8342 term_escape_char
= strtol(optarg
, &r
, 0);
8344 printf("Bad argument to echr\n");
8347 case QEMU_OPTION_monitor
:
8348 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
8350 case QEMU_OPTION_balloon
:
8351 if (vmchannel_device_index
>= MAX_VMCHANNEL_DEVICES
) {
8352 fprintf(stderr
, "qemu: too many balloon/vmchannel devices\n");
8356 fprintf(stderr
, "qemu: only one balloon device can be used\n");
8359 sprintf(vmchannel_devices
[vmchannel_device_index
],"di:cdcd,%s", optarg
);
8360 vmchannel_device_index
++;
8363 case QEMU_OPTION_vmchannel
:
8364 if (vmchannel_device_index
>= MAX_VMCHANNEL_DEVICES
) {
8365 fprintf(stderr
, "qemu: too many balloon/vmchannel devices\n");
8368 pstrcpy(vmchannel_devices
[vmchannel_device_index
],
8369 sizeof(vmchannel_devices
[0]), optarg
);
8370 vmchannel_device_index
++;
8372 case QEMU_OPTION_serial
:
8373 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
8374 fprintf(stderr
, "qemu: too many serial ports\n");
8377 pstrcpy(serial_devices
[serial_device_index
],
8378 sizeof(serial_devices
[0]), optarg
);
8379 serial_device_index
++;
8381 case QEMU_OPTION_parallel
:
8382 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
8383 fprintf(stderr
, "qemu: too many parallel ports\n");
8386 pstrcpy(parallel_devices
[parallel_device_index
],
8387 sizeof(parallel_devices
[0]), optarg
);
8388 parallel_device_index
++;
8390 case QEMU_OPTION_loadvm
:
8393 case QEMU_OPTION_incoming
:
8396 case QEMU_OPTION_full_screen
:
8400 case QEMU_OPTION_no_frame
:
8403 case QEMU_OPTION_alt_grab
:
8406 case QEMU_OPTION_no_quit
:
8410 case QEMU_OPTION_pidfile
:
8414 case QEMU_OPTION_win2k_hack
:
8415 win2k_install_hack
= 1;
8419 case QEMU_OPTION_no_kqemu
:
8422 case QEMU_OPTION_kernel_kqemu
:
8427 case QEMU_OPTION_no_kvm
:
8430 case QEMU_OPTION_no_kvm_irqchip
:
8434 case QEMU_OPTION_usb
:
8437 case QEMU_OPTION_usbdevice
:
8439 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
8440 fprintf(stderr
, "Too many USB devices\n");
8443 pstrcpy(usb_devices
[usb_devices_index
],
8444 sizeof(usb_devices
[usb_devices_index
]),
8446 usb_devices_index
++;
8448 case QEMU_OPTION_smp
:
8449 smp_cpus
= atoi(optarg
);
8450 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
8451 fprintf(stderr
, "Invalid number of CPUs\n");
8455 case QEMU_OPTION_vnc
:
8456 vnc_display
= optarg
;
8458 case QEMU_OPTION_no_acpi
:
8461 case QEMU_OPTION_no_reboot
:
8464 case QEMU_OPTION_show_cursor
:
8467 case QEMU_OPTION_daemonize
:
8470 case QEMU_OPTION_option_rom
:
8471 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
8472 fprintf(stderr
, "Too many option ROMs\n");
8475 option_rom
[nb_option_roms
] = optarg
;
8478 case QEMU_OPTION_semihosting
:
8479 semihosting_enabled
= 1;
8481 case QEMU_OPTION_tdf
:
8484 case QEMU_OPTION_kvm_shadow_memory
:
8485 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
8487 case QEMU_OPTION_name
:
8491 case QEMU_OPTION_prom_env
:
8492 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
8493 fprintf(stderr
, "Too many prom variables\n");
8496 prom_envs
[nb_prom_envs
] = optarg
;
8500 case QEMU_OPTION_cpu_vendor
:
8501 cpu_vendor_string
= optarg
;
8504 case QEMU_OPTION_old_param
:
8507 case QEMU_OPTION_clock
:
8508 configure_alarms(optarg
);
8518 if (pipe(fds
) == -1)
8529 len
= read(fds
[0], &status
, 1);
8530 if (len
== -1 && (errno
== EINTR
))
8535 else if (status
== 1) {
8536 fprintf(stderr
, "Could not acquire pidfile\n");
8553 signal(SIGTSTP
, SIG_IGN
);
8554 signal(SIGTTOU
, SIG_IGN
);
8555 signal(SIGTTIN
, SIG_IGN
);
8561 if (kvm_qemu_init() < 0) {
8562 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
8568 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
8571 write(fds
[1], &status
, 1);
8573 fprintf(stderr
, "Could not acquire pid file\n");
8581 linux_boot
= (kernel_filename
!= NULL
);
8584 boot_device
!= 'n' &&
8585 hd_filename
[0] == '\0' &&
8586 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
8587 fd_filename
[0] == '\0')
8590 /* boot to floppy or the default cd if no hard disk defined yet */
8591 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
8592 if (fd_filename
[0] != '\0')
8598 setvbuf(stdout
, NULL
, _IOLBF
, 0);
8608 /* init network clients */
8609 if (nb_net_clients
== 0) {
8610 /* if no clients, we use a default config */
8611 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
8613 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
8618 for(i
= 0;i
< nb_net_clients
; i
++) {
8619 if (net_client_init(net_clients
[i
]) < 0)
8622 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
8623 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
8625 if (vlan
->nb_guest_devs
== 0) {
8626 fprintf(stderr
, "Invalid vlan (%d) with no nics\n", vlan
->id
);
8629 if (vlan
->nb_host_devs
== 0)
8631 "Warning: vlan %d is not connected to host network\n",
8636 if (boot_device
== 'n') {
8637 for (i
= 0; i
< nb_nics
; i
++) {
8638 const char *model
= nd_table
[i
].model
;
8642 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
8643 if (get_image_size(buf
) > 0) {
8644 option_rom
[nb_option_roms
] = strdup(buf
);
8650 fprintf(stderr
, "No valid PXE rom found for network device\n");
8656 /* init the memory */
8657 phys_ram_size
= ram_size
+ vga_ram_size
+ MAX_BIOS_SIZE
;
8660 /* Initialize kvm */
8662 phys_ram_size
+= KVM_EXTRA_PAGES
* 4096;
8663 if (kvm_qemu_create_context() < 0) {
8664 fprintf(stderr
, "Could not create KVM context\n");
8668 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
8669 if (!phys_ram_base
) {
8670 fprintf(stderr
, "Could not allocate physical memory\n");
8675 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
8676 if (!phys_ram_base
) {
8677 fprintf(stderr
, "Could not allocate physical memory\n");
8682 /* we always create the cdrom drive, even if no disk is there */
8684 if (cdrom_index
>= 0) {
8685 bs_table
[cdrom_index
] = bdrv_new("cdrom");
8686 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
8689 /* open the virtual block devices */
8690 for(i
= 0; i
< MAX_DISKS
; i
++) {
8691 if (hd_filename
[i
]) {
8694 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
8695 bs_table
[i
] = bdrv_new(buf
);
8697 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8698 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
8702 if (i
== 0 && cyls
!= 0) {
8703 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
8704 bdrv_set_translation_hint(bs_table
[i
], translation
);
8709 /* we always create at least one floppy disk */
8710 fd_table
[0] = bdrv_new("fda");
8711 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
8713 for(i
= 0; i
< MAX_FD
; i
++) {
8714 if (fd_filename
[i
]) {
8717 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
8718 fd_table
[i
] = bdrv_new(buf
);
8719 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
8721 if (fd_filename
[i
][0] != '\0') {
8722 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
8723 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8724 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
8732 /* Open the virtual parallel flash block devices */
8733 for(i
= 0; i
< MAX_PFLASH
; i
++) {
8734 if (pflash_filename
[i
]) {
8735 if (!pflash_table
[i
]) {
8737 snprintf(buf
, sizeof(buf
), "fl%c", i
+ 'a');
8738 pflash_table
[i
] = bdrv_new(buf
);
8740 if (bdrv_open(pflash_table
[i
], pflash_filename
[i
],
8741 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8742 fprintf(stderr
, "qemu: could not open flash image '%s'\n",
8743 pflash_filename
[i
]);
8749 sd_bdrv
= bdrv_new ("sd");
8750 /* FIXME: This isn't really a floppy, but it's a reasonable
8752 bdrv_set_type_hint(sd_bdrv
, BDRV_TYPE_FLOPPY
);
8754 if (bdrv_open(sd_bdrv
, sd_filename
,
8755 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
8756 fprintf(stderr
, "qemu: could not open SD card image %s\n",
8759 qemu_key_check(sd_bdrv
, sd_filename
);
8763 mtd_bdrv
= bdrv_new ("mtd");
8764 if (bdrv_open(mtd_bdrv
, mtd_filename
,
8765 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0 ||
8766 qemu_key_check(mtd_bdrv
, mtd_filename
)) {
8767 fprintf(stderr
, "qemu: could not open Flash image %s\n",
8769 bdrv_delete(mtd_bdrv
);
8774 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
8775 register_savevm("ram", 0, 3, ram_save
, ram_load
, NULL
);
8780 memset(&display_state
, 0, sizeof(display_state
));
8782 /* nearly nothing to do */
8783 dumb_display_init(ds
);
8784 } else if (vnc_display
!= NULL
) {
8785 vnc_display_init(ds
);
8786 if (vnc_display_open(ds
, vnc_display
) < 0)
8789 #if defined(CONFIG_SDL)
8790 sdl_display_init(ds
, full_screen
, no_frame
);
8791 #elif defined(CONFIG_COCOA)
8792 cocoa_display_init(ds
, full_screen
);
8796 /* Maintain compatibility with multiple stdio monitors */
8797 if (!strcmp(monitor_device
,"stdio")) {
8798 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
8799 if (!strcmp(serial_devices
[i
],"mon:stdio")) {
8800 monitor_device
[0] = '\0';
8802 } else if (!strcmp(serial_devices
[i
],"stdio")) {
8803 monitor_device
[0] = '\0';
8804 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "mon:stdio");
8809 if (monitor_device
[0] != '\0') {
8810 monitor_hd
= qemu_chr_open(monitor_device
);
8812 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
8815 monitor_init(monitor_hd
, !nographic
);
8818 for(i
= 0; i
< MAX_VMCHANNEL_DEVICES
; i
++) {
8819 const char *devname
= vmchannel_devices
[i
];
8820 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
8824 if (strstart(devname
, "di:", &devname
)) {
8825 devid
= strtol(devname
, &termn
, 16);
8826 devname
= termn
+ 1;
8829 fprintf(stderr
, "qemu: could not find vmchannel device id '%s'\n",
8833 vmchannel_hds
[i
] = qemu_chr_open(devname
);
8834 if (!vmchannel_hds
[i
]) {
8835 fprintf(stderr
, "qemu: could not open vmchannel device '%s'\n",
8839 vmchannel_init(vmchannel_hds
[i
], devid
, i
);
8843 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
8844 const char *devname
= serial_devices
[i
];
8845 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
8846 serial_hds
[i
] = qemu_chr_open(devname
);
8847 if (!serial_hds
[i
]) {
8848 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
8852 if (strstart(devname
, "vc", 0))
8853 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
8857 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
8858 const char *devname
= parallel_devices
[i
];
8859 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
8860 parallel_hds
[i
] = qemu_chr_open(devname
);
8861 if (!parallel_hds
[i
]) {
8862 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
8866 if (strstart(devname
, "vc", 0))
8867 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
8871 machine
->init(ram_size
, vga_ram_size
, boot_device
,
8872 ds
, fd_filename
, snapshot
,
8873 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
8875 /* init USB devices */
8877 for(i
= 0; i
< usb_devices_index
; i
++) {
8878 if (usb_device_add(usb_devices
[i
]) < 0) {
8879 fprintf(stderr
, "Warning: could not add USB device %s\n",
8885 if (display_state
.dpy_refresh
) {
8886 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
8887 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
8895 #ifdef CONFIG_GDBSTUB
8897 /* XXX: use standard host:port notation and modify options
8899 if (gdbserver_start(gdbstub_port
) < 0) {
8900 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
8912 rc
= migrate_incoming(incoming
);
8914 fprintf(stderr
, "Migration failed rc=%d\n", rc
);
8920 /* XXX: simplify init */
8933 len
= write(fds
[1], &status
, 1);
8934 if (len
== -1 && (errno
== EINTR
))
8941 TFR(fd
= open("/dev/null", O_RDWR
));