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>
50 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
51 #include <freebsd/stdlib.h>
55 #include <linux/if_tun.h>
58 #include <linux/rtc.h>
59 #include <linux/ppdev.h>
60 #include <linux/parport.h>
63 #include <sys/ethernet.h>
64 #include <sys/sockio.h>
65 #include <arpa/inet.h>
66 #include <netinet/arp.h>
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_icmp.h> // must come after ip.h
71 #include <netinet/udp.h>
72 #include <netinet/tcp.h>
80 #if defined(CONFIG_SLIRP)
86 #include <sys/timeb.h>
88 #define getopt_long_only getopt_long
89 #define memalign(align, size) malloc(size)
92 #include "qemu_socket.h"
98 #endif /* CONFIG_SDL */
102 #define main qemu_main
103 #endif /* CONFIG_COCOA */
107 #include "exec-all.h"
109 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
111 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
113 #define SMBD_COMMAND "/usr/sbin/smbd"
116 //#define DEBUG_UNUSED_IOPORT
117 //#define DEBUG_IOPORT
119 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
122 #define DEFAULT_RAM_SIZE 144
124 #define DEFAULT_RAM_SIZE 128
127 #define GUI_REFRESH_INTERVAL 30
129 /* Max number of USB devices that can be specified on the commandline. */
130 #define MAX_USB_CMDLINE 8
132 /* XXX: use a two level table to limit memory usage */
133 #define MAX_IOPORTS 65536
135 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
136 char phys_ram_file
[1024];
137 void *ioport_opaque
[MAX_IOPORTS
];
138 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
139 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
140 /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available
141 to store the VM snapshots */
142 BlockDriverState
*bs_table
[MAX_DISKS
+ 1], *fd_table
[MAX_FD
];
143 BlockDriverState
*pflash_table
[MAX_PFLASH
];
144 BlockDriverState
*sd_bdrv
;
145 BlockDriverState
*mtd_bdrv
;
146 /* point to the block driver where the snapshots are managed */
147 BlockDriverState
*bs_snapshots
;
149 static DisplayState display_state
;
151 const char* keyboard_layout
= NULL
;
152 int64_t ticks_per_sec
;
153 int boot_device
= 'c';
155 int pit_min_timer_count
= 0;
157 NICInfo nd_table
[MAX_NICS
];
160 int cirrus_vga_enabled
= 1;
161 int vmsvga_enabled
= 0;
163 int graphic_width
= 1024;
164 int graphic_height
= 768;
165 int graphic_depth
= 8;
167 int graphic_width
= 800;
168 int graphic_height
= 600;
169 int graphic_depth
= 15;
174 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
175 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
177 int win2k_install_hack
= 0;
180 static VLANState
*first_vlan
;
182 const char *vnc_display
;
183 #if defined(TARGET_SPARC)
185 #elif defined(TARGET_I386)
190 int acpi_enabled
= 1;
194 int graphic_rotate
= 0;
196 const char *option_rom
[MAX_OPTION_ROMS
];
198 int semihosting_enabled
= 0;
203 const char *qemu_name
;
206 unsigned int nb_prom_envs
= 0;
207 const char *prom_envs
[MAX_PROM_ENVS
];
210 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
212 /***********************************************************/
213 /* x86 ISA bus support */
215 target_phys_addr_t isa_mem_base
= 0;
218 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
220 #ifdef DEBUG_UNUSED_IOPORT
221 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
226 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
228 #ifdef DEBUG_UNUSED_IOPORT
229 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
233 /* default is to make two byte accesses */
234 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
237 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
238 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
239 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
243 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
245 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
246 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
247 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
250 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
252 #ifdef DEBUG_UNUSED_IOPORT
253 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
258 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
260 #ifdef DEBUG_UNUSED_IOPORT
261 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
265 void init_ioports(void)
269 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
270 ioport_read_table
[0][i
] = default_ioport_readb
;
271 ioport_write_table
[0][i
] = default_ioport_writeb
;
272 ioport_read_table
[1][i
] = default_ioport_readw
;
273 ioport_write_table
[1][i
] = default_ioport_writew
;
274 ioport_read_table
[2][i
] = default_ioport_readl
;
275 ioport_write_table
[2][i
] = default_ioport_writel
;
279 /* size is the word size in byte */
280 int register_ioport_read(int start
, int length
, int size
,
281 IOPortReadFunc
*func
, void *opaque
)
287 } else if (size
== 2) {
289 } else if (size
== 4) {
292 hw_error("register_ioport_read: invalid size");
295 for(i
= start
; i
< start
+ length
; i
+= size
) {
296 ioport_read_table
[bsize
][i
] = func
;
297 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
298 hw_error("register_ioport_read: invalid opaque");
299 ioport_opaque
[i
] = opaque
;
304 /* size is the word size in byte */
305 int register_ioport_write(int start
, int length
, int size
,
306 IOPortWriteFunc
*func
, void *opaque
)
312 } else if (size
== 2) {
314 } else if (size
== 4) {
317 hw_error("register_ioport_write: invalid size");
320 for(i
= start
; i
< start
+ length
; i
+= size
) {
321 ioport_write_table
[bsize
][i
] = func
;
322 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
323 hw_error("register_ioport_write: invalid opaque");
324 ioport_opaque
[i
] = opaque
;
329 void isa_unassign_ioport(int start
, int length
)
333 for(i
= start
; i
< start
+ length
; i
++) {
334 ioport_read_table
[0][i
] = default_ioport_readb
;
335 ioport_read_table
[1][i
] = default_ioport_readw
;
336 ioport_read_table
[2][i
] = default_ioport_readl
;
338 ioport_write_table
[0][i
] = default_ioport_writeb
;
339 ioport_write_table
[1][i
] = default_ioport_writew
;
340 ioport_write_table
[2][i
] = default_ioport_writel
;
344 /***********************************************************/
346 void cpu_outb(CPUState
*env
, int addr
, int val
)
349 if (loglevel
& CPU_LOG_IOPORT
)
350 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
352 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
355 env
->last_io_time
= cpu_get_time_fast();
359 void cpu_outw(CPUState
*env
, int addr
, int val
)
362 if (loglevel
& CPU_LOG_IOPORT
)
363 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
365 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
368 env
->last_io_time
= cpu_get_time_fast();
372 void cpu_outl(CPUState
*env
, int addr
, int val
)
375 if (loglevel
& CPU_LOG_IOPORT
)
376 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
378 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
381 env
->last_io_time
= cpu_get_time_fast();
385 int cpu_inb(CPUState
*env
, int addr
)
388 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
390 if (loglevel
& CPU_LOG_IOPORT
)
391 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
395 env
->last_io_time
= cpu_get_time_fast();
400 int cpu_inw(CPUState
*env
, int addr
)
403 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
405 if (loglevel
& CPU_LOG_IOPORT
)
406 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
410 env
->last_io_time
= cpu_get_time_fast();
415 int cpu_inl(CPUState
*env
, int addr
)
418 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
420 if (loglevel
& CPU_LOG_IOPORT
)
421 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
425 env
->last_io_time
= cpu_get_time_fast();
430 /***********************************************************/
431 void hw_error(const char *fmt
, ...)
437 fprintf(stderr
, "qemu: hardware error: ");
438 vfprintf(stderr
, fmt
, ap
);
439 fprintf(stderr
, "\n");
440 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
441 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
443 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
445 cpu_dump_state(env
, stderr
, fprintf
, 0);
452 /***********************************************************/
455 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
456 static void *qemu_put_kbd_event_opaque
;
457 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
458 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
460 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
462 qemu_put_kbd_event_opaque
= opaque
;
463 qemu_put_kbd_event
= func
;
466 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
467 void *opaque
, int absolute
,
470 QEMUPutMouseEntry
*s
, *cursor
;
472 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
476 s
->qemu_put_mouse_event
= func
;
477 s
->qemu_put_mouse_event_opaque
= opaque
;
478 s
->qemu_put_mouse_event_absolute
= absolute
;
479 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
482 if (!qemu_put_mouse_event_head
) {
483 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
487 cursor
= qemu_put_mouse_event_head
;
488 while (cursor
->next
!= NULL
)
489 cursor
= cursor
->next
;
492 qemu_put_mouse_event_current
= s
;
497 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
499 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
501 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
504 cursor
= qemu_put_mouse_event_head
;
505 while (cursor
!= NULL
&& cursor
!= entry
) {
507 cursor
= cursor
->next
;
510 if (cursor
== NULL
) // does not exist or list empty
512 else if (prev
== NULL
) { // entry is head
513 qemu_put_mouse_event_head
= cursor
->next
;
514 if (qemu_put_mouse_event_current
== entry
)
515 qemu_put_mouse_event_current
= cursor
->next
;
516 qemu_free(entry
->qemu_put_mouse_event_name
);
521 prev
->next
= entry
->next
;
523 if (qemu_put_mouse_event_current
== entry
)
524 qemu_put_mouse_event_current
= prev
;
526 qemu_free(entry
->qemu_put_mouse_event_name
);
530 void kbd_put_keycode(int keycode
)
532 if (qemu_put_kbd_event
) {
533 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
537 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
539 QEMUPutMouseEvent
*mouse_event
;
540 void *mouse_event_opaque
;
543 if (!qemu_put_mouse_event_current
) {
548 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
550 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
553 if (graphic_rotate
) {
554 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
557 width
= graphic_width
;
558 mouse_event(mouse_event_opaque
,
559 width
- dy
, dx
, dz
, buttons_state
);
561 mouse_event(mouse_event_opaque
,
562 dx
, dy
, dz
, buttons_state
);
566 int kbd_mouse_is_absolute(void)
568 if (!qemu_put_mouse_event_current
)
571 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
574 void do_info_mice(void)
576 QEMUPutMouseEntry
*cursor
;
579 if (!qemu_put_mouse_event_head
) {
580 term_printf("No mouse devices connected\n");
584 term_printf("Mouse devices available:\n");
585 cursor
= qemu_put_mouse_event_head
;
586 while (cursor
!= NULL
) {
587 term_printf("%c Mouse #%d: %s\n",
588 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
589 index
, cursor
->qemu_put_mouse_event_name
);
591 cursor
= cursor
->next
;
595 void do_mouse_set(int index
)
597 QEMUPutMouseEntry
*cursor
;
600 if (!qemu_put_mouse_event_head
) {
601 term_printf("No mouse devices connected\n");
605 cursor
= qemu_put_mouse_event_head
;
606 while (cursor
!= NULL
&& index
!= i
) {
608 cursor
= cursor
->next
;
612 qemu_put_mouse_event_current
= cursor
;
614 term_printf("Mouse at given index not found\n");
617 /* compute with 96 bit intermediate result: (a*b)/c */
618 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
623 #ifdef WORDS_BIGENDIAN
633 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
634 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
637 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
641 /***********************************************************/
642 /* real time host monotonic timer */
644 #define QEMU_TIMER_BASE 1000000000LL
648 static int64_t clock_freq
;
650 static void init_get_clock(void)
654 ret
= QueryPerformanceFrequency(&freq
);
656 fprintf(stderr
, "Could not calibrate ticks\n");
659 clock_freq
= freq
.QuadPart
;
662 static int64_t get_clock(void)
665 QueryPerformanceCounter(&ti
);
666 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
671 static int use_rt_clock
;
673 static void init_get_clock(void)
676 #if defined(__linux__)
679 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
686 static int64_t get_clock(void)
688 #if defined(__linux__)
691 clock_gettime(CLOCK_MONOTONIC
, &ts
);
692 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
696 /* XXX: using gettimeofday leads to problems if the date
697 changes, so it should be avoided. */
699 gettimeofday(&tv
, NULL
);
700 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
706 /***********************************************************/
707 /* guest cycle counter */
709 static int64_t cpu_ticks_prev
;
710 static int64_t cpu_ticks_offset
;
711 static int64_t cpu_clock_offset
;
712 static int cpu_ticks_enabled
;
714 /* return the host CPU cycle counter and handle stop/restart */
715 int64_t cpu_get_ticks(void)
717 if (!cpu_ticks_enabled
) {
718 return cpu_ticks_offset
;
721 ticks
= cpu_get_real_ticks();
722 if (cpu_ticks_prev
> ticks
) {
723 /* Note: non increasing ticks may happen if the host uses
725 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
727 cpu_ticks_prev
= ticks
;
728 return ticks
+ cpu_ticks_offset
;
732 /* return the host CPU monotonic timer and handle stop/restart */
733 static int64_t cpu_get_clock(void)
736 if (!cpu_ticks_enabled
) {
737 return cpu_clock_offset
;
740 return ti
+ cpu_clock_offset
;
744 /* enable cpu_get_ticks() */
745 void cpu_enable_ticks(void)
747 if (!cpu_ticks_enabled
) {
748 cpu_ticks_offset
-= cpu_get_real_ticks();
749 cpu_clock_offset
-= get_clock();
750 cpu_ticks_enabled
= 1;
754 /* disable cpu_get_ticks() : the clock is stopped. You must not call
755 cpu_get_ticks() after that. */
756 void cpu_disable_ticks(void)
758 if (cpu_ticks_enabled
) {
759 cpu_ticks_offset
= cpu_get_ticks();
760 cpu_clock_offset
= cpu_get_clock();
761 cpu_ticks_enabled
= 0;
765 /***********************************************************/
768 #define QEMU_TIMER_REALTIME 0
769 #define QEMU_TIMER_VIRTUAL 1
773 /* XXX: add frequency */
781 struct QEMUTimer
*next
;
787 static QEMUTimer
*active_timers
[2];
789 static MMRESULT timerID
;
790 static HANDLE host_alarm
= NULL
;
791 static unsigned int period
= 1;
793 /* frequency of the times() clock tick */
794 static int timer_freq
;
797 QEMUClock
*qemu_new_clock(int type
)
800 clock
= qemu_mallocz(sizeof(QEMUClock
));
807 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
811 ts
= qemu_mallocz(sizeof(QEMUTimer
));
818 void qemu_free_timer(QEMUTimer
*ts
)
823 /* stop a timer, but do not dealloc it */
824 void qemu_del_timer(QEMUTimer
*ts
)
828 /* NOTE: this code must be signal safe because
829 qemu_timer_expired() can be called from a signal. */
830 pt
= &active_timers
[ts
->clock
->type
];
843 /* modify the current timer so that it will be fired when current_time
844 >= expire_time. The corresponding callback will be called. */
845 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
851 /* add the timer in the sorted list */
852 /* NOTE: this code must be signal safe because
853 qemu_timer_expired() can be called from a signal. */
854 pt
= &active_timers
[ts
->clock
->type
];
859 if (t
->expire_time
> expire_time
)
863 ts
->expire_time
= expire_time
;
868 int qemu_timer_pending(QEMUTimer
*ts
)
871 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
878 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
882 return (timer_head
->expire_time
<= current_time
);
885 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
891 if (!ts
|| ts
->expire_time
> current_time
)
893 /* remove timer from the list before calling the callback */
894 *ptimer_head
= ts
->next
;
897 /* run the callback (the timer list can be modified) */
902 int64_t qemu_get_clock(QEMUClock
*clock
)
904 switch(clock
->type
) {
905 case QEMU_TIMER_REALTIME
:
906 return get_clock() / 1000000;
908 case QEMU_TIMER_VIRTUAL
:
909 return cpu_get_clock();
913 static void init_timers(void)
916 ticks_per_sec
= QEMU_TIMER_BASE
;
917 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
918 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
922 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
924 uint64_t expire_time
;
926 if (qemu_timer_pending(ts
)) {
927 expire_time
= ts
->expire_time
;
931 qemu_put_be64(f
, expire_time
);
934 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
936 uint64_t expire_time
;
938 expire_time
= qemu_get_be64(f
);
939 if (expire_time
!= -1) {
940 qemu_mod_timer(ts
, expire_time
);
946 static void timer_save(QEMUFile
*f
, void *opaque
)
948 if (cpu_ticks_enabled
) {
949 hw_error("cannot save state if virtual timers are running");
951 qemu_put_be64s(f
, &cpu_ticks_offset
);
952 qemu_put_be64s(f
, &ticks_per_sec
);
953 qemu_put_be64s(f
, &cpu_clock_offset
);
956 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
958 if (version_id
!= 1 && version_id
!= 2)
960 if (cpu_ticks_enabled
) {
963 qemu_get_be64s(f
, &cpu_ticks_offset
);
964 qemu_get_be64s(f
, &ticks_per_sec
);
965 if (version_id
== 2) {
966 qemu_get_be64s(f
, &cpu_clock_offset
);
972 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
973 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
975 static void host_alarm_handler(int host_signum
)
979 #define DISP_FREQ 1000
981 static int64_t delta_min
= INT64_MAX
;
982 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
984 ti
= qemu_get_clock(vm_clock
);
985 if (last_clock
!= 0) {
986 delta
= ti
- last_clock
;
987 if (delta
< delta_min
)
989 if (delta
> delta_max
)
992 if (++count
== DISP_FREQ
) {
993 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
994 muldiv64(delta_min
, 1000000, ticks_per_sec
),
995 muldiv64(delta_max
, 1000000, ticks_per_sec
),
996 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
997 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
999 delta_min
= INT64_MAX
;
1007 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1008 qemu_get_clock(vm_clock
)) ||
1009 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1010 qemu_get_clock(rt_clock
))) {
1012 SetEvent(host_alarm
);
1014 CPUState
*env
= cpu_single_env
;
1016 /* stop the currently executing cpu because a timer occured */
1017 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1019 if (env
->kqemu_enabled
) {
1020 kqemu_cpu_interrupt(env
);
1029 #if defined(__linux__)
1031 #define RTC_FREQ 1024
1035 static int start_rtc_timer(void)
1037 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1040 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1041 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1042 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1043 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1046 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1051 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
1057 static int start_rtc_timer(void)
1062 #endif /* !defined(__linux__) */
1064 #endif /* !defined(_WIN32) */
1066 static void init_timer_alarm(void)
1073 ZeroMemory(&tc
, sizeof(TIMECAPS
));
1074 timeGetDevCaps(&tc
, sizeof(TIMECAPS
));
1075 if (period
< tc
.wPeriodMin
)
1076 period
= tc
.wPeriodMin
;
1077 timeBeginPeriod(period
);
1078 timerID
= timeSetEvent(1, // interval (ms)
1079 period
, // resolution
1080 host_alarm_handler
, // function
1081 (DWORD
)&count
, // user parameter
1082 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
1084 perror("failed timer alarm");
1087 host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1089 perror("failed CreateEvent");
1092 qemu_add_wait_object(host_alarm
, NULL
, NULL
);
1094 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
1097 struct sigaction act
;
1098 struct itimerval itv
;
1100 /* get times() syscall frequency */
1101 timer_freq
= sysconf(_SC_CLK_TCK
);
1104 sigfillset(&act
.sa_mask
);
1106 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1107 act
.sa_flags
|= SA_ONSTACK
;
1109 act
.sa_handler
= host_alarm_handler
;
1110 sigaction(SIGALRM
, &act
, NULL
);
1112 itv
.it_interval
.tv_sec
= 0;
1113 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
1114 itv
.it_value
.tv_sec
= 0;
1115 itv
.it_value
.tv_usec
= 10 * 1000;
1116 setitimer(ITIMER_REAL
, &itv
, NULL
);
1117 /* we probe the tick duration of the kernel to inform the user if
1118 the emulated kernel requested a too high timer frequency */
1119 getitimer(ITIMER_REAL
, &itv
);
1121 #if defined(__linux__)
1122 /* XXX: force /dev/rtc usage because even 2.6 kernels may not
1123 have timers with 1 ms resolution. The correct solution will
1124 be to use the POSIX real time timers available in recent
1126 if (itv
.it_interval
.tv_usec
> 1000 || 1) {
1127 /* try to use /dev/rtc to have a faster timer */
1128 if (start_rtc_timer() < 0)
1130 /* disable itimer */
1131 itv
.it_interval
.tv_sec
= 0;
1132 itv
.it_interval
.tv_usec
= 0;
1133 itv
.it_value
.tv_sec
= 0;
1134 itv
.it_value
.tv_usec
= 0;
1135 setitimer(ITIMER_REAL
, &itv
, NULL
);
1138 sigaction(SIGIO
, &act
, NULL
);
1139 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1140 fcntl(rtc_fd
, F_SETOWN
, getpid());
1142 #endif /* defined(__linux__) */
1145 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1146 PIT_FREQ
) / 1000000;
1152 void quit_timers(void)
1155 timeKillEvent(timerID
);
1156 timeEndPeriod(period
);
1158 CloseHandle(host_alarm
);
1164 /***********************************************************/
1165 /* character device */
1167 static void qemu_chr_event(CharDriverState
*s
, int event
)
1171 s
->chr_event(s
->handler_opaque
, event
);
1174 static void qemu_chr_reset_bh(void *opaque
)
1176 CharDriverState
*s
= opaque
;
1177 qemu_chr_event(s
, CHR_EVENT_RESET
);
1178 qemu_bh_delete(s
->bh
);
1182 void qemu_chr_reset(CharDriverState
*s
)
1184 if (s
->bh
== NULL
) {
1185 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
1186 qemu_bh_schedule(s
->bh
);
1190 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1192 return s
->chr_write(s
, buf
, len
);
1195 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1199 return s
->chr_ioctl(s
, cmd
, arg
);
1202 int qemu_chr_can_read(CharDriverState
*s
)
1204 if (!s
->chr_can_read
)
1206 return s
->chr_can_read(s
->handler_opaque
);
1209 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
1211 s
->chr_read(s
->handler_opaque
, buf
, len
);
1215 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1220 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1221 qemu_chr_write(s
, buf
, strlen(buf
));
1225 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1227 if (s
->chr_send_event
)
1228 s
->chr_send_event(s
, event
);
1231 void qemu_chr_add_handlers(CharDriverState
*s
,
1232 IOCanRWHandler
*fd_can_read
,
1233 IOReadHandler
*fd_read
,
1234 IOEventHandler
*fd_event
,
1237 s
->chr_can_read
= fd_can_read
;
1238 s
->chr_read
= fd_read
;
1239 s
->chr_event
= fd_event
;
1240 s
->handler_opaque
= opaque
;
1241 if (s
->chr_update_read_handler
)
1242 s
->chr_update_read_handler(s
);
1245 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1250 static CharDriverState
*qemu_chr_open_null(void)
1252 CharDriverState
*chr
;
1254 chr
= qemu_mallocz(sizeof(CharDriverState
));
1257 chr
->chr_write
= null_chr_write
;
1261 /* MUX driver for serial I/O splitting */
1262 static int term_timestamps
;
1263 static int64_t term_timestamps_start
;
1266 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
1267 IOReadHandler
*chr_read
[MAX_MUX
];
1268 IOEventHandler
*chr_event
[MAX_MUX
];
1269 void *ext_opaque
[MAX_MUX
];
1270 CharDriverState
*drv
;
1272 int term_got_escape
;
1277 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1279 MuxDriver
*d
= chr
->opaque
;
1281 if (!term_timestamps
) {
1282 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
1287 for(i
= 0; i
< len
; i
++) {
1288 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
1289 if (buf
[i
] == '\n') {
1295 if (term_timestamps_start
== -1)
1296 term_timestamps_start
= ti
;
1297 ti
-= term_timestamps_start
;
1298 secs
= ti
/ 1000000000;
1299 snprintf(buf1
, sizeof(buf1
),
1300 "[%02d:%02d:%02d.%03d] ",
1304 (int)((ti
/ 1000000) % 1000));
1305 d
->drv
->chr_write(d
->drv
, buf1
, strlen(buf1
));
1312 static char *mux_help
[] = {
1313 "% h print this help\n\r",
1314 "% x exit emulator\n\r",
1315 "% s save disk data back to file (if -snapshot)\n\r",
1316 "% t toggle console timestamps\n\r"
1317 "% b send break (magic sysrq)\n\r",
1318 "% c switch between console and monitor\n\r",
1323 static int term_escape_char
= 0x01; /* ctrl-a is used for escape */
1324 static void mux_print_help(CharDriverState
*chr
)
1327 char ebuf
[15] = "Escape-Char";
1328 char cbuf
[50] = "\n\r";
1330 if (term_escape_char
> 0 && term_escape_char
< 26) {
1331 sprintf(cbuf
,"\n\r");
1332 sprintf(ebuf
,"C-%c", term_escape_char
- 1 + 'a');
1334 sprintf(cbuf
,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char
);
1336 chr
->chr_write(chr
, cbuf
, strlen(cbuf
));
1337 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
1338 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
1339 if (mux_help
[i
][j
] == '%')
1340 chr
->chr_write(chr
, ebuf
, strlen(ebuf
));
1342 chr
->chr_write(chr
, &mux_help
[i
][j
], 1);
1347 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
1349 if (d
->term_got_escape
) {
1350 d
->term_got_escape
= 0;
1351 if (ch
== term_escape_char
)
1356 mux_print_help(chr
);
1360 char *term
= "QEMU: Terminated\n\r";
1361 chr
->chr_write(chr
,term
,strlen(term
));
1368 for (i
= 0; i
< MAX_DISKS
; i
++) {
1370 bdrv_commit(bs_table
[i
]);
1373 bdrv_commit(mtd_bdrv
);
1377 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1380 /* Switch to the next registered device */
1382 if (chr
->focus
>= d
->mux_cnt
)
1386 term_timestamps
= !term_timestamps
;
1387 term_timestamps_start
= -1;
1390 } else if (ch
== term_escape_char
) {
1391 d
->term_got_escape
= 1;
1399 static int mux_chr_can_read(void *opaque
)
1401 CharDriverState
*chr
= opaque
;
1402 MuxDriver
*d
= chr
->opaque
;
1403 if (d
->chr_can_read
[chr
->focus
])
1404 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
1408 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
1410 CharDriverState
*chr
= opaque
;
1411 MuxDriver
*d
= chr
->opaque
;
1413 for(i
= 0; i
< size
; i
++)
1414 if (mux_proc_byte(chr
, d
, buf
[i
]))
1415 d
->chr_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
], &buf
[i
], 1);
1418 static void mux_chr_event(void *opaque
, int event
)
1420 CharDriverState
*chr
= opaque
;
1421 MuxDriver
*d
= chr
->opaque
;
1424 /* Send the event to all registered listeners */
1425 for (i
= 0; i
< d
->mux_cnt
; i
++)
1426 if (d
->chr_event
[i
])
1427 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
1430 static void mux_chr_update_read_handler(CharDriverState
*chr
)
1432 MuxDriver
*d
= chr
->opaque
;
1434 if (d
->mux_cnt
>= MAX_MUX
) {
1435 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
1438 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
1439 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
1440 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
1441 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
1442 /* Fix up the real driver with mux routines */
1443 if (d
->mux_cnt
== 0) {
1444 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
1445 mux_chr_event
, chr
);
1447 chr
->focus
= d
->mux_cnt
;
1451 CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
1453 CharDriverState
*chr
;
1456 chr
= qemu_mallocz(sizeof(CharDriverState
));
1459 d
= qemu_mallocz(sizeof(MuxDriver
));
1468 chr
->chr_write
= mux_chr_write
;
1469 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
1476 static void socket_cleanup(void)
1481 static int socket_init(void)
1486 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1488 err
= WSAGetLastError();
1489 fprintf(stderr
, "WSAStartup: %d\n", err
);
1492 atexit(socket_cleanup
);
1496 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1502 ret
= send(fd
, buf
, len
, 0);
1505 errno
= WSAGetLastError();
1506 if (errno
!= WSAEWOULDBLOCK
) {
1509 } else if (ret
== 0) {
1519 void socket_set_nonblock(int fd
)
1521 unsigned long opt
= 1;
1522 ioctlsocket(fd
, FIONBIO
, &opt
);
1527 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1533 ret
= write(fd
, buf
, len
);
1535 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1537 } else if (ret
== 0) {
1547 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1549 return unix_write(fd
, buf
, len1
);
1552 void socket_set_nonblock(int fd
)
1554 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1556 #endif /* !_WIN32 */
1565 #define STDIO_MAX_CLIENTS 1
1566 static int stdio_nb_clients
= 0;
1568 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1570 FDCharDriver
*s
= chr
->opaque
;
1571 return unix_write(s
->fd_out
, buf
, len
);
1574 static int fd_chr_read_poll(void *opaque
)
1576 CharDriverState
*chr
= opaque
;
1577 FDCharDriver
*s
= chr
->opaque
;
1579 s
->max_size
= qemu_chr_can_read(chr
);
1583 static void fd_chr_read(void *opaque
)
1585 CharDriverState
*chr
= opaque
;
1586 FDCharDriver
*s
= chr
->opaque
;
1591 if (len
> s
->max_size
)
1595 size
= read(s
->fd_in
, buf
, len
);
1597 /* FD has been closed. Remove it from the active list. */
1598 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1602 qemu_chr_read(chr
, buf
, size
);
1606 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1608 FDCharDriver
*s
= chr
->opaque
;
1610 if (s
->fd_in
>= 0) {
1611 if (nographic
&& s
->fd_in
== 0) {
1613 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1614 fd_chr_read
, NULL
, chr
);
1619 /* open a character device to a unix fd */
1620 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1622 CharDriverState
*chr
;
1625 chr
= qemu_mallocz(sizeof(CharDriverState
));
1628 s
= qemu_mallocz(sizeof(FDCharDriver
));
1636 chr
->chr_write
= fd_chr_write
;
1637 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
1639 qemu_chr_reset(chr
);
1644 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1648 TFR(fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
1651 return qemu_chr_open_fd(-1, fd_out
);
1654 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1657 char filename_in
[256], filename_out
[256];
1659 snprintf(filename_in
, 256, "%s.in", filename
);
1660 snprintf(filename_out
, 256, "%s.out", filename
);
1661 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
1662 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
1663 if (fd_in
< 0 || fd_out
< 0) {
1668 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
1672 return qemu_chr_open_fd(fd_in
, fd_out
);
1676 /* for STDIO, we handle the case where several clients use it
1679 #define TERM_FIFO_MAX_SIZE 1
1681 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1682 static int term_fifo_size
;
1684 static int stdio_read_poll(void *opaque
)
1686 CharDriverState
*chr
= opaque
;
1688 /* try to flush the queue if needed */
1689 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
1690 qemu_chr_read(chr
, term_fifo
, 1);
1693 /* see if we can absorb more chars */
1694 if (term_fifo_size
== 0)
1700 static void stdio_read(void *opaque
)
1704 CharDriverState
*chr
= opaque
;
1706 size
= read(0, buf
, 1);
1708 /* stdin has been closed. Remove it from the active list. */
1709 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
1713 if (qemu_chr_can_read(chr
) > 0) {
1714 qemu_chr_read(chr
, buf
, 1);
1715 } else if (term_fifo_size
== 0) {
1716 term_fifo
[term_fifo_size
++] = buf
[0];
1721 /* init terminal so that we can grab keys */
1722 static struct termios oldtty
;
1723 static int old_fd0_flags
;
1725 static void term_exit(void)
1727 tcsetattr (0, TCSANOW
, &oldtty
);
1728 fcntl(0, F_SETFL
, old_fd0_flags
);
1731 static void term_init(void)
1735 tcgetattr (0, &tty
);
1737 old_fd0_flags
= fcntl(0, F_GETFL
);
1739 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1740 |INLCR
|IGNCR
|ICRNL
|IXON
);
1741 tty
.c_oflag
|= OPOST
;
1742 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1743 /* if graphical mode, we allow Ctrl-C handling */
1745 tty
.c_lflag
&= ~ISIG
;
1746 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1749 tty
.c_cc
[VTIME
] = 0;
1751 tcsetattr (0, TCSANOW
, &tty
);
1755 fcntl(0, F_SETFL
, O_NONBLOCK
);
1758 static CharDriverState
*qemu_chr_open_stdio(void)
1760 CharDriverState
*chr
;
1762 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1764 chr
= qemu_chr_open_fd(0, 1);
1765 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
1772 #if defined(__linux__) || defined(__sun__)
1773 static CharDriverState
*qemu_chr_open_pty(void)
1776 char slave_name
[1024];
1777 int master_fd
, slave_fd
;
1779 #if defined(__linux__)
1780 /* Not satisfying */
1781 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1786 /* Disabling local echo and line-buffered output */
1787 tcgetattr (master_fd
, &tty
);
1788 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1790 tty
.c_cc
[VTIME
] = 0;
1791 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1793 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1794 return qemu_chr_open_fd(master_fd
, master_fd
);
1797 static void tty_serial_init(int fd
, int speed
,
1798 int parity
, int data_bits
, int stop_bits
)
1804 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1805 speed
, parity
, data_bits
, stop_bits
);
1807 tcgetattr (fd
, &tty
);
1849 cfsetispeed(&tty
, spd
);
1850 cfsetospeed(&tty
, spd
);
1852 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1853 |INLCR
|IGNCR
|ICRNL
|IXON
);
1854 tty
.c_oflag
|= OPOST
;
1855 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1856 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1877 tty
.c_cflag
|= PARENB
;
1880 tty
.c_cflag
|= PARENB
| PARODD
;
1884 tty
.c_cflag
|= CSTOPB
;
1886 tcsetattr (fd
, TCSANOW
, &tty
);
1889 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1891 FDCharDriver
*s
= chr
->opaque
;
1894 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1896 QEMUSerialSetParams
*ssp
= arg
;
1897 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1898 ssp
->data_bits
, ssp
->stop_bits
);
1901 case CHR_IOCTL_SERIAL_SET_BREAK
:
1903 int enable
= *(int *)arg
;
1905 tcsendbreak(s
->fd_in
, 1);
1914 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1916 CharDriverState
*chr
;
1919 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1920 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1921 tty_serial_init(fd
, 115200, 'N', 8, 1);
1922 chr
= qemu_chr_open_fd(fd
, fd
);
1927 chr
->chr_ioctl
= tty_serial_ioctl
;
1928 qemu_chr_reset(chr
);
1931 #else /* ! __linux__ && ! __sun__ */
1932 static CharDriverState
*qemu_chr_open_pty(void)
1936 #endif /* __linux__ || __sun__ */
1938 #if defined(__linux__)
1942 } ParallelCharDriver
;
1944 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1946 if (s
->mode
!= mode
) {
1948 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1955 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1957 ParallelCharDriver
*drv
= chr
->opaque
;
1962 case CHR_IOCTL_PP_READ_DATA
:
1963 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1965 *(uint8_t *)arg
= b
;
1967 case CHR_IOCTL_PP_WRITE_DATA
:
1968 b
= *(uint8_t *)arg
;
1969 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1972 case CHR_IOCTL_PP_READ_CONTROL
:
1973 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1975 /* Linux gives only the lowest bits, and no way to know data
1976 direction! For better compatibility set the fixed upper
1978 *(uint8_t *)arg
= b
| 0xc0;
1980 case CHR_IOCTL_PP_WRITE_CONTROL
:
1981 b
= *(uint8_t *)arg
;
1982 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1985 case CHR_IOCTL_PP_READ_STATUS
:
1986 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1988 *(uint8_t *)arg
= b
;
1990 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1991 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1992 struct ParallelIOArg
*parg
= arg
;
1993 int n
= read(fd
, parg
->buffer
, parg
->count
);
1994 if (n
!= parg
->count
) {
1999 case CHR_IOCTL_PP_EPP_READ
:
2000 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2001 struct ParallelIOArg
*parg
= arg
;
2002 int n
= read(fd
, parg
->buffer
, parg
->count
);
2003 if (n
!= parg
->count
) {
2008 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
2009 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
2010 struct ParallelIOArg
*parg
= arg
;
2011 int n
= write(fd
, parg
->buffer
, parg
->count
);
2012 if (n
!= parg
->count
) {
2017 case CHR_IOCTL_PP_EPP_WRITE
:
2018 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2019 struct ParallelIOArg
*parg
= arg
;
2020 int n
= write(fd
, parg
->buffer
, parg
->count
);
2021 if (n
!= parg
->count
) {
2032 static void pp_close(CharDriverState
*chr
)
2034 ParallelCharDriver
*drv
= chr
->opaque
;
2037 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
2038 ioctl(fd
, PPRELEASE
);
2043 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
2045 CharDriverState
*chr
;
2046 ParallelCharDriver
*drv
;
2049 TFR(fd
= open(filename
, O_RDWR
));
2053 if (ioctl(fd
, PPCLAIM
) < 0) {
2058 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
2064 drv
->mode
= IEEE1284_MODE_COMPAT
;
2066 chr
= qemu_mallocz(sizeof(CharDriverState
));
2072 chr
->chr_write
= null_chr_write
;
2073 chr
->chr_ioctl
= pp_ioctl
;
2074 chr
->chr_close
= pp_close
;
2077 qemu_chr_reset(chr
);
2081 #endif /* __linux__ */
2087 HANDLE hcom
, hrecv
, hsend
;
2088 OVERLAPPED orecv
, osend
;
2093 #define NSENDBUF 2048
2094 #define NRECVBUF 2048
2095 #define MAXCONNECT 1
2096 #define NTIMEOUT 5000
2098 static int win_chr_poll(void *opaque
);
2099 static int win_chr_pipe_poll(void *opaque
);
2101 static void win_chr_close(CharDriverState
*chr
)
2103 WinCharState
*s
= chr
->opaque
;
2106 CloseHandle(s
->hsend
);
2110 CloseHandle(s
->hrecv
);
2114 CloseHandle(s
->hcom
);
2118 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
2120 qemu_del_polling_cb(win_chr_poll
, chr
);
2123 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
2125 WinCharState
*s
= chr
->opaque
;
2127 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2132 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2134 fprintf(stderr
, "Failed CreateEvent\n");
2137 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2139 fprintf(stderr
, "Failed CreateEvent\n");
2143 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2144 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2145 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2146 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
2151 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2152 fprintf(stderr
, "Failed SetupComm\n");
2156 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2157 size
= sizeof(COMMCONFIG
);
2158 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2159 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2160 CommConfigDialog(filename
, NULL
, &comcfg
);
2162 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2163 fprintf(stderr
, "Failed SetCommState\n");
2167 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2168 fprintf(stderr
, "Failed SetCommMask\n");
2172 cto
.ReadIntervalTimeout
= MAXDWORD
;
2173 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2174 fprintf(stderr
, "Failed SetCommTimeouts\n");
2178 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2179 fprintf(stderr
, "Failed ClearCommError\n");
2182 qemu_add_polling_cb(win_chr_poll
, chr
);
2190 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2192 WinCharState
*s
= chr
->opaque
;
2193 DWORD len
, ret
, size
, err
;
2196 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2197 s
->osend
.hEvent
= s
->hsend
;
2200 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2202 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2204 err
= GetLastError();
2205 if (err
== ERROR_IO_PENDING
) {
2206 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2224 static int win_chr_read_poll(CharDriverState
*chr
)
2226 WinCharState
*s
= chr
->opaque
;
2228 s
->max_size
= qemu_chr_can_read(chr
);
2232 static void win_chr_readfile(CharDriverState
*chr
)
2234 WinCharState
*s
= chr
->opaque
;
2239 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2240 s
->orecv
.hEvent
= s
->hrecv
;
2241 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2243 err
= GetLastError();
2244 if (err
== ERROR_IO_PENDING
) {
2245 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2250 qemu_chr_read(chr
, buf
, size
);
2254 static void win_chr_read(CharDriverState
*chr
)
2256 WinCharState
*s
= chr
->opaque
;
2258 if (s
->len
> s
->max_size
)
2259 s
->len
= s
->max_size
;
2263 win_chr_readfile(chr
);
2266 static int win_chr_poll(void *opaque
)
2268 CharDriverState
*chr
= opaque
;
2269 WinCharState
*s
= chr
->opaque
;
2273 ClearCommError(s
->hcom
, &comerr
, &status
);
2274 if (status
.cbInQue
> 0) {
2275 s
->len
= status
.cbInQue
;
2276 win_chr_read_poll(chr
);
2283 static CharDriverState
*qemu_chr_open_win(const char *filename
)
2285 CharDriverState
*chr
;
2288 chr
= qemu_mallocz(sizeof(CharDriverState
));
2291 s
= qemu_mallocz(sizeof(WinCharState
));
2297 chr
->chr_write
= win_chr_write
;
2298 chr
->chr_close
= win_chr_close
;
2300 if (win_chr_init(chr
, filename
) < 0) {
2305 qemu_chr_reset(chr
);
2309 static int win_chr_pipe_poll(void *opaque
)
2311 CharDriverState
*chr
= opaque
;
2312 WinCharState
*s
= chr
->opaque
;
2315 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2318 win_chr_read_poll(chr
);
2325 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
2327 WinCharState
*s
= chr
->opaque
;
2335 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2337 fprintf(stderr
, "Failed CreateEvent\n");
2340 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2342 fprintf(stderr
, "Failed CreateEvent\n");
2346 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2347 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2348 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2350 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2351 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2352 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2357 ZeroMemory(&ov
, sizeof(ov
));
2358 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2359 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2361 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2365 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2367 fprintf(stderr
, "Failed GetOverlappedResult\n");
2369 CloseHandle(ov
.hEvent
);
2376 CloseHandle(ov
.hEvent
);
2379 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2388 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2390 CharDriverState
*chr
;
2393 chr
= qemu_mallocz(sizeof(CharDriverState
));
2396 s
= qemu_mallocz(sizeof(WinCharState
));
2402 chr
->chr_write
= win_chr_write
;
2403 chr
->chr_close
= win_chr_close
;
2405 if (win_chr_pipe_init(chr
, filename
) < 0) {
2410 qemu_chr_reset(chr
);
2414 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2416 CharDriverState
*chr
;
2419 chr
= qemu_mallocz(sizeof(CharDriverState
));
2422 s
= qemu_mallocz(sizeof(WinCharState
));
2429 chr
->chr_write
= win_chr_write
;
2430 qemu_chr_reset(chr
);
2434 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
2436 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2439 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2443 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2444 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2445 if (fd_out
== INVALID_HANDLE_VALUE
)
2448 return qemu_chr_open_win_file(fd_out
);
2450 #endif /* !_WIN32 */
2452 /***********************************************************/
2453 /* UDP Net console */
2457 struct sockaddr_in daddr
;
2464 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2466 NetCharDriver
*s
= chr
->opaque
;
2468 return sendto(s
->fd
, buf
, len
, 0,
2469 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2472 static int udp_chr_read_poll(void *opaque
)
2474 CharDriverState
*chr
= opaque
;
2475 NetCharDriver
*s
= chr
->opaque
;
2477 s
->max_size
= qemu_chr_can_read(chr
);
2479 /* If there were any stray characters in the queue process them
2482 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2483 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2485 s
->max_size
= qemu_chr_can_read(chr
);
2490 static void udp_chr_read(void *opaque
)
2492 CharDriverState
*chr
= opaque
;
2493 NetCharDriver
*s
= chr
->opaque
;
2495 if (s
->max_size
== 0)
2497 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2498 s
->bufptr
= s
->bufcnt
;
2503 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2504 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2506 s
->max_size
= qemu_chr_can_read(chr
);
2510 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2512 NetCharDriver
*s
= chr
->opaque
;
2515 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2516 udp_chr_read
, NULL
, chr
);
2520 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2522 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2524 int parse_host_src_port(struct sockaddr_in
*haddr
,
2525 struct sockaddr_in
*saddr
,
2528 static CharDriverState
*qemu_chr_open_udp(const char *def
)
2530 CharDriverState
*chr
= NULL
;
2531 NetCharDriver
*s
= NULL
;
2533 struct sockaddr_in saddr
;
2535 chr
= qemu_mallocz(sizeof(CharDriverState
));
2538 s
= qemu_mallocz(sizeof(NetCharDriver
));
2542 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2544 perror("socket(PF_INET, SOCK_DGRAM)");
2548 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2549 printf("Could not parse: %s\n", def
);
2553 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2563 chr
->chr_write
= udp_chr_write
;
2564 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2577 /***********************************************************/
2578 /* TCP Net console */
2589 static void tcp_chr_accept(void *opaque
);
2591 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2593 TCPCharDriver
*s
= chr
->opaque
;
2595 return send_all(s
->fd
, buf
, len
);
2597 /* XXX: indicate an error ? */
2602 static int tcp_chr_read_poll(void *opaque
)
2604 CharDriverState
*chr
= opaque
;
2605 TCPCharDriver
*s
= chr
->opaque
;
2608 s
->max_size
= qemu_chr_can_read(chr
);
2613 #define IAC_BREAK 243
2614 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2616 char *buf
, int *size
)
2618 /* Handle any telnet client's basic IAC options to satisfy char by
2619 * char mode with no echo. All IAC options will be removed from
2620 * the buf and the do_telnetopt variable will be used to track the
2621 * state of the width of the IAC information.
2623 * IAC commands come in sets of 3 bytes with the exception of the
2624 * "IAC BREAK" command and the double IAC.
2630 for (i
= 0; i
< *size
; i
++) {
2631 if (s
->do_telnetopt
> 1) {
2632 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2633 /* Double IAC means send an IAC */
2637 s
->do_telnetopt
= 1;
2639 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2640 /* Handle IAC break commands by sending a serial break */
2641 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
2646 if (s
->do_telnetopt
>= 4) {
2647 s
->do_telnetopt
= 1;
2650 if ((unsigned char)buf
[i
] == IAC
) {
2651 s
->do_telnetopt
= 2;
2662 static void tcp_chr_read(void *opaque
)
2664 CharDriverState
*chr
= opaque
;
2665 TCPCharDriver
*s
= chr
->opaque
;
2669 if (!s
->connected
|| s
->max_size
<= 0)
2672 if (len
> s
->max_size
)
2674 size
= recv(s
->fd
, buf
, len
, 0);
2676 /* connection closed */
2678 if (s
->listen_fd
>= 0) {
2679 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2681 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2684 } else if (size
> 0) {
2685 if (s
->do_telnetopt
)
2686 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2688 qemu_chr_read(chr
, buf
, size
);
2692 static void tcp_chr_connect(void *opaque
)
2694 CharDriverState
*chr
= opaque
;
2695 TCPCharDriver
*s
= chr
->opaque
;
2698 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2699 tcp_chr_read
, NULL
, chr
);
2700 qemu_chr_reset(chr
);
2703 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2704 static void tcp_chr_telnet_init(int fd
)
2707 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2708 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2709 send(fd
, (char *)buf
, 3, 0);
2710 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2711 send(fd
, (char *)buf
, 3, 0);
2712 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2713 send(fd
, (char *)buf
, 3, 0);
2714 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2715 send(fd
, (char *)buf
, 3, 0);
2718 static void socket_set_nodelay(int fd
)
2721 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2724 static void tcp_chr_accept(void *opaque
)
2726 CharDriverState
*chr
= opaque
;
2727 TCPCharDriver
*s
= chr
->opaque
;
2728 struct sockaddr_in saddr
;
2730 struct sockaddr_un uaddr
;
2732 struct sockaddr
*addr
;
2739 len
= sizeof(uaddr
);
2740 addr
= (struct sockaddr
*)&uaddr
;
2744 len
= sizeof(saddr
);
2745 addr
= (struct sockaddr
*)&saddr
;
2747 fd
= accept(s
->listen_fd
, addr
, &len
);
2748 if (fd
< 0 && errno
!= EINTR
) {
2750 } else if (fd
>= 0) {
2751 if (s
->do_telnetopt
)
2752 tcp_chr_telnet_init(fd
);
2756 socket_set_nonblock(fd
);
2758 socket_set_nodelay(fd
);
2760 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2761 tcp_chr_connect(chr
);
2764 static void tcp_chr_close(CharDriverState
*chr
)
2766 TCPCharDriver
*s
= chr
->opaque
;
2769 if (s
->listen_fd
>= 0)
2770 closesocket(s
->listen_fd
);
2774 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2778 CharDriverState
*chr
= NULL
;
2779 TCPCharDriver
*s
= NULL
;
2780 int fd
= -1, ret
, err
, val
;
2782 int is_waitconnect
= 1;
2785 struct sockaddr_in saddr
;
2787 struct sockaddr_un uaddr
;
2789 struct sockaddr
*addr
;
2794 addr
= (struct sockaddr
*)&uaddr
;
2795 addrlen
= sizeof(uaddr
);
2796 if (parse_unix_path(&uaddr
, host_str
) < 0)
2801 addr
= (struct sockaddr
*)&saddr
;
2802 addrlen
= sizeof(saddr
);
2803 if (parse_host_port(&saddr
, host_str
) < 0)
2808 while((ptr
= strchr(ptr
,','))) {
2810 if (!strncmp(ptr
,"server",6)) {
2812 } else if (!strncmp(ptr
,"nowait",6)) {
2814 } else if (!strncmp(ptr
,"nodelay",6)) {
2817 printf("Unknown option: %s\n", ptr
);
2824 chr
= qemu_mallocz(sizeof(CharDriverState
));
2827 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2833 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2836 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2841 if (!is_waitconnect
)
2842 socket_set_nonblock(fd
);
2847 s
->is_unix
= is_unix
;
2848 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2851 chr
->chr_write
= tcp_chr_write
;
2852 chr
->chr_close
= tcp_chr_close
;
2855 /* allow fast reuse */
2859 strncpy(path
, uaddr
.sun_path
, 108);
2866 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2869 ret
= bind(fd
, addr
, addrlen
);
2873 ret
= listen(fd
, 0);
2878 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2880 s
->do_telnetopt
= 1;
2883 ret
= connect(fd
, addr
, addrlen
);
2885 err
= socket_error();
2886 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2887 } else if (err
== EINPROGRESS
) {
2890 } else if (err
== WSAEALREADY
) {
2902 socket_set_nodelay(fd
);
2904 tcp_chr_connect(chr
);
2906 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2909 if (is_listen
&& is_waitconnect
) {
2910 printf("QEMU waiting for connection on: %s\n", host_str
);
2911 tcp_chr_accept(chr
);
2912 socket_set_nonblock(s
->listen_fd
);
2924 CharDriverState
*qemu_chr_open(const char *filename
)
2928 if (!strcmp(filename
, "vc")) {
2929 return text_console_init(&display_state
, 0);
2930 } else if (strstart(filename
, "vc:", &p
)) {
2931 return text_console_init(&display_state
, p
);
2932 } else if (!strcmp(filename
, "null")) {
2933 return qemu_chr_open_null();
2935 if (strstart(filename
, "tcp:", &p
)) {
2936 return qemu_chr_open_tcp(p
, 0, 0);
2938 if (strstart(filename
, "telnet:", &p
)) {
2939 return qemu_chr_open_tcp(p
, 1, 0);
2941 if (strstart(filename
, "udp:", &p
)) {
2942 return qemu_chr_open_udp(p
);
2944 if (strstart(filename
, "mon:", &p
)) {
2945 CharDriverState
*drv
= qemu_chr_open(p
);
2947 drv
= qemu_chr_open_mux(drv
);
2948 monitor_init(drv
, !nographic
);
2951 printf("Unable to open driver: %s\n", p
);
2955 if (strstart(filename
, "unix:", &p
)) {
2956 return qemu_chr_open_tcp(p
, 0, 1);
2957 } else if (strstart(filename
, "file:", &p
)) {
2958 return qemu_chr_open_file_out(p
);
2959 } else if (strstart(filename
, "pipe:", &p
)) {
2960 return qemu_chr_open_pipe(p
);
2961 } else if (!strcmp(filename
, "pty")) {
2962 return qemu_chr_open_pty();
2963 } else if (!strcmp(filename
, "stdio")) {
2964 return qemu_chr_open_stdio();
2966 #if defined(__linux__)
2967 if (strstart(filename
, "/dev/parport", NULL
)) {
2968 return qemu_chr_open_pp(filename
);
2971 #if defined(__linux__) || defined(__sun__)
2972 if (strstart(filename
, "/dev/", NULL
)) {
2973 return qemu_chr_open_tty(filename
);
2977 if (strstart(filename
, "COM", NULL
)) {
2978 return qemu_chr_open_win(filename
);
2980 if (strstart(filename
, "pipe:", &p
)) {
2981 return qemu_chr_open_win_pipe(p
);
2983 if (strstart(filename
, "con:", NULL
)) {
2984 return qemu_chr_open_win_con(filename
);
2986 if (strstart(filename
, "file:", &p
)) {
2987 return qemu_chr_open_win_file_out(p
);
2995 void qemu_chr_close(CharDriverState
*chr
)
2998 chr
->chr_close(chr
);
3001 /***********************************************************/
3002 /* network device redirectors */
3004 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
3008 for(i
=0;i
<size
;i
+=16) {
3012 fprintf(f
, "%08x ", i
);
3015 fprintf(f
, " %02x", buf
[i
+j
]);
3020 for(j
=0;j
<len
;j
++) {
3022 if (c
< ' ' || c
> '~')
3024 fprintf(f
, "%c", c
);
3030 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
3033 for(i
= 0; i
< 6; i
++) {
3034 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3047 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
3052 p1
= strchr(p
, sep
);
3058 if (len
> buf_size
- 1)
3060 memcpy(buf
, p
, len
);
3067 int parse_host_src_port(struct sockaddr_in
*haddr
,
3068 struct sockaddr_in
*saddr
,
3069 const char *input_str
)
3071 char *str
= strdup(input_str
);
3072 char *host_str
= str
;
3077 * Chop off any extra arguments at the end of the string which
3078 * would start with a comma, then fill in the src port information
3079 * if it was provided else use the "any address" and "any port".
3081 if ((ptr
= strchr(str
,',')))
3084 if ((src_str
= strchr(input_str
,'@'))) {
3089 if (parse_host_port(haddr
, host_str
) < 0)
3092 if (!src_str
|| *src_str
== '\0')
3095 if (parse_host_port(saddr
, src_str
) < 0)
3106 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
3114 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3116 saddr
->sin_family
= AF_INET
;
3117 if (buf
[0] == '\0') {
3118 saddr
->sin_addr
.s_addr
= 0;
3120 if (isdigit(buf
[0])) {
3121 if (!inet_aton(buf
, &saddr
->sin_addr
))
3124 if ((he
= gethostbyname(buf
)) == NULL
)
3126 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
3129 port
= strtol(p
, (char **)&r
, 0);
3132 saddr
->sin_port
= htons(port
);
3137 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
3142 len
= MIN(108, strlen(str
));
3143 p
= strchr(str
, ',');
3145 len
= MIN(len
, p
- str
);
3147 memset(uaddr
, 0, sizeof(*uaddr
));
3149 uaddr
->sun_family
= AF_UNIX
;
3150 memcpy(uaddr
->sun_path
, str
, len
);
3156 /* find or alloc a new VLAN */
3157 VLANState
*qemu_find_vlan(int id
)
3159 VLANState
**pvlan
, *vlan
;
3160 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3164 vlan
= qemu_mallocz(sizeof(VLANState
));
3169 pvlan
= &first_vlan
;
3170 while (*pvlan
!= NULL
)
3171 pvlan
= &(*pvlan
)->next
;
3176 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
3177 IOReadHandler
*fd_read
,
3178 IOCanRWHandler
*fd_can_read
,
3181 VLANClientState
*vc
, **pvc
;
3182 vc
= qemu_mallocz(sizeof(VLANClientState
));
3185 vc
->fd_read
= fd_read
;
3186 vc
->fd_can_read
= fd_can_read
;
3187 vc
->opaque
= opaque
;
3191 pvc
= &vlan
->first_client
;
3192 while (*pvc
!= NULL
)
3193 pvc
= &(*pvc
)->next
;
3198 int qemu_can_send_packet(VLANClientState
*vc1
)
3200 VLANState
*vlan
= vc1
->vlan
;
3201 VLANClientState
*vc
;
3203 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3205 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
3212 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
3214 VLANState
*vlan
= vc1
->vlan
;
3215 VLANClientState
*vc
;
3218 printf("vlan %d send:\n", vlan
->id
);
3219 hex_dump(stdout
, buf
, size
);
3221 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3223 vc
->fd_read(vc
->opaque
, buf
, size
);
3228 #if defined(CONFIG_SLIRP)
3230 /* slirp network adapter */
3232 static int slirp_inited
;
3233 static VLANClientState
*slirp_vc
;
3235 int slirp_can_output(void)
3237 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
3240 void slirp_output(const uint8_t *pkt
, int pkt_len
)
3243 printf("slirp output:\n");
3244 hex_dump(stdout
, pkt
, pkt_len
);
3248 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
3251 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
3254 printf("slirp input:\n");
3255 hex_dump(stdout
, buf
, size
);
3257 slirp_input(buf
, size
);
3260 static int net_slirp_init(VLANState
*vlan
)
3262 if (!slirp_inited
) {
3266 slirp_vc
= qemu_new_vlan_client(vlan
,
3267 slirp_receive
, NULL
, NULL
);
3268 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
3272 static void net_slirp_redir(const char *redir_str
)
3277 struct in_addr guest_addr
;
3278 int host_port
, guest_port
;
3280 if (!slirp_inited
) {
3286 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3288 if (!strcmp(buf
, "tcp")) {
3290 } else if (!strcmp(buf
, "udp")) {
3296 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3298 host_port
= strtol(buf
, &r
, 0);
3302 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3304 if (buf
[0] == '\0') {
3305 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
3307 if (!inet_aton(buf
, &guest_addr
))
3310 guest_port
= strtol(p
, &r
, 0);
3314 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3315 fprintf(stderr
, "qemu: could not set up redirection\n");
3320 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3328 static void smb_exit(void)
3332 char filename
[1024];
3334 /* erase all the files in the directory */
3335 d
= opendir(smb_dir
);
3340 if (strcmp(de
->d_name
, ".") != 0 &&
3341 strcmp(de
->d_name
, "..") != 0) {
3342 snprintf(filename
, sizeof(filename
), "%s/%s",
3343 smb_dir
, de
->d_name
);
3351 /* automatic user mode samba server configuration */
3352 void net_slirp_smb(const char *exported_dir
)
3354 char smb_conf
[1024];
3355 char smb_cmdline
[1024];
3358 if (!slirp_inited
) {
3363 /* XXX: better tmp dir construction */
3364 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3365 if (mkdir(smb_dir
, 0700) < 0) {
3366 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3369 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3371 f
= fopen(smb_conf
, "w");
3373 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3380 "socket address=127.0.0.1\n"
3381 "pid directory=%s\n"
3382 "lock directory=%s\n"
3383 "log file=%s/log.smbd\n"
3384 "smb passwd file=%s/smbpasswd\n"
3385 "security = share\n"
3400 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3401 SMBD_COMMAND
, smb_conf
);
3403 slirp_add_exec(0, smb_cmdline
, 4, 139);
3406 #endif /* !defined(_WIN32) */
3408 #endif /* CONFIG_SLIRP */
3410 #if !defined(_WIN32)
3412 typedef struct TAPState
{
3413 VLANClientState
*vc
;
3417 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3419 TAPState
*s
= opaque
;
3422 ret
= write(s
->fd
, buf
, size
);
3423 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3430 static void tap_send(void *opaque
)
3432 TAPState
*s
= opaque
;
3439 sbuf
.maxlen
= sizeof(buf
);
3441 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
3443 size
= read(s
->fd
, buf
, sizeof(buf
));
3446 qemu_send_packet(s
->vc
, buf
, size
);
3452 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3456 s
= qemu_mallocz(sizeof(TAPState
));
3460 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3461 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3462 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3466 #if defined (_BSD) || defined (__FreeBSD_kernel__)
3467 static int tap_open(char *ifname
, int ifname_size
)
3473 TFR(fd
= open("/dev/tap", O_RDWR
));
3475 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3480 dev
= devname(s
.st_rdev
, S_IFCHR
);
3481 pstrcpy(ifname
, ifname_size
, dev
);
3483 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3486 #elif defined(__sun__)
3487 #define TUNNEWPPA (('T'<<16) | 0x0001)
3489 * Allocate TAP device, returns opened fd.
3490 * Stores dev name in the first arg(must be large enough).
3492 int tap_alloc(char *dev
)
3494 int tap_fd
, if_fd
, ppa
= -1;
3495 static int ip_fd
= 0;
3498 static int arp_fd
= 0;
3499 int ip_muxid
, arp_muxid
;
3500 struct strioctl strioc_if
, strioc_ppa
;
3501 int link_type
= I_PLINK
;;
3503 char actual_name
[32] = "";
3505 memset(&ifr
, 0x0, sizeof(ifr
));
3509 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
3513 /* Check if IP device was opened */
3517 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
3519 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
3523 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
3525 syslog(LOG_ERR
, "Can't open /dev/tap");
3529 /* Assign a new PPA and get its unit number. */
3530 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
3531 strioc_ppa
.ic_timout
= 0;
3532 strioc_ppa
.ic_len
= sizeof(ppa
);
3533 strioc_ppa
.ic_dp
= (char *)&ppa
;
3534 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
3535 syslog (LOG_ERR
, "Can't assign new interface");
3537 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
3539 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
3542 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
3543 syslog(LOG_ERR
, "Can't push IP module");
3547 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
3548 syslog(LOG_ERR
, "Can't get flags\n");
3550 snprintf (actual_name
, 32, "tap%d", ppa
);
3551 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3554 /* Assign ppa according to the unit number returned by tun device */
3556 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
3557 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
3558 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
3559 syslog (LOG_ERR
, "Can't get flags\n");
3560 /* Push arp module to if_fd */
3561 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
3562 syslog (LOG_ERR
, "Can't push ARP module (2)");
3564 /* Push arp module to ip_fd */
3565 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
3566 syslog (LOG_ERR
, "I_POP failed\n");
3567 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
3568 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
3570 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
3572 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
3574 /* Set ifname to arp */
3575 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
3576 strioc_if
.ic_timout
= 0;
3577 strioc_if
.ic_len
= sizeof(ifr
);
3578 strioc_if
.ic_dp
= (char *)&ifr
;
3579 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
3580 syslog (LOG_ERR
, "Can't set ifname to arp\n");
3583 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
3584 syslog(LOG_ERR
, "Can't link TAP device to IP");
3588 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
3589 syslog (LOG_ERR
, "Can't link TAP device to ARP");
3593 memset(&ifr
, 0x0, sizeof(ifr
));
3594 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3595 ifr
.lifr_ip_muxid
= ip_muxid
;
3596 ifr
.lifr_arp_muxid
= arp_muxid
;
3598 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
3600 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
3601 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
3602 syslog (LOG_ERR
, "Can't set multiplexor id");
3605 sprintf(dev
, "tap%d", ppa
);
3609 static int tap_open(char *ifname
, int ifname_size
)
3613 if( (fd
= tap_alloc(dev
)) < 0 ){
3614 fprintf(stderr
, "Cannot allocate TAP device\n");
3617 pstrcpy(ifname
, ifname_size
, dev
);
3618 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3622 static int tap_open(char *ifname
, int ifname_size
)
3627 TFR(fd
= open("/dev/net/tun", O_RDWR
));
3629 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
3632 memset(&ifr
, 0, sizeof(ifr
));
3633 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
3634 if (ifname
[0] != '\0')
3635 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
3637 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
3638 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
3640 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
3644 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
3645 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3650 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
3651 const char *setup_script
)
3654 int pid
, status
, fd
;
3659 if (ifname1
!= NULL
)
3660 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
3663 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
3667 if (!setup_script
|| !strcmp(setup_script
, "no"))
3669 if (setup_script
[0] != '\0') {
3670 /* try to launch network init script */
3674 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
3675 for (i
= 0; i
< open_max
; i
++)
3676 if (i
!= STDIN_FILENO
&&
3677 i
!= STDOUT_FILENO
&&
3678 i
!= STDERR_FILENO
&&
3683 *parg
++ = (char *)setup_script
;
3686 execv(setup_script
, args
);
3689 while (waitpid(pid
, &status
, 0) != pid
);
3690 if (!WIFEXITED(status
) ||
3691 WEXITSTATUS(status
) != 0) {
3692 fprintf(stderr
, "%s: could not launch network script\n",
3698 s
= net_tap_fd_init(vlan
, fd
);
3701 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3702 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
3706 #endif /* !_WIN32 */
3708 /* network connection */
3709 typedef struct NetSocketState
{
3710 VLANClientState
*vc
;
3712 int state
; /* 0 = getting length, 1 = getting data */
3716 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
3719 typedef struct NetSocketListenState
{
3722 } NetSocketListenState
;
3724 /* XXX: we consider we can send the whole packet without blocking */
3725 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
3727 NetSocketState
*s
= opaque
;
3731 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
3732 send_all(s
->fd
, buf
, size
);
3735 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
3737 NetSocketState
*s
= opaque
;
3738 sendto(s
->fd
, buf
, size
, 0,
3739 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
3742 static void net_socket_send(void *opaque
)
3744 NetSocketState
*s
= opaque
;
3749 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
3751 err
= socket_error();
3752 if (err
!= EWOULDBLOCK
)
3754 } else if (size
== 0) {
3755 /* end of connection */
3757 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3763 /* reassemble a packet from the network */
3769 memcpy(s
->buf
+ s
->index
, buf
, l
);
3773 if (s
->index
== 4) {
3775 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
3781 l
= s
->packet_len
- s
->index
;
3784 memcpy(s
->buf
+ s
->index
, buf
, l
);
3788 if (s
->index
>= s
->packet_len
) {
3789 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
3798 static void net_socket_send_dgram(void *opaque
)
3800 NetSocketState
*s
= opaque
;
3803 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
3807 /* end of connection */
3808 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3811 qemu_send_packet(s
->vc
, s
->buf
, size
);
3814 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
3819 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
3820 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
3821 inet_ntoa(mcastaddr
->sin_addr
),
3822 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
3826 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
3828 perror("socket(PF_INET, SOCK_DGRAM)");
3833 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
3834 (const char *)&val
, sizeof(val
));
3836 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
3840 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
3846 /* Add host to multicast group */
3847 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
3848 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
3850 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
3851 (const char *)&imr
, sizeof(struct ip_mreq
));
3853 perror("setsockopt(IP_ADD_MEMBERSHIP)");
3857 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
3859 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
3860 (const char *)&val
, sizeof(val
));
3862 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
3866 socket_set_nonblock(fd
);
3874 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
3877 struct sockaddr_in saddr
;
3879 socklen_t saddr_len
;
3882 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
3883 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
3884 * by ONLY ONE process: we must "clone" this dgram socket --jjo
3888 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
3890 if (saddr
.sin_addr
.s_addr
==0) {
3891 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
3895 /* clone dgram socket */
3896 newfd
= net_socket_mcast_create(&saddr
);
3898 /* error already reported by net_socket_mcast_create() */
3902 /* clone newfd to fd, close newfd */
3907 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
3908 fd
, strerror(errno
));
3913 s
= qemu_mallocz(sizeof(NetSocketState
));
3918 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
3919 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
3921 /* mcast: save bound address as dst */
3922 if (is_connected
) s
->dgram_dst
=saddr
;
3924 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3925 "socket: fd=%d (%s mcast=%s:%d)",
3926 fd
, is_connected
? "cloned" : "",
3927 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3931 static void net_socket_connect(void *opaque
)
3933 NetSocketState
*s
= opaque
;
3934 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
3937 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
3941 s
= qemu_mallocz(sizeof(NetSocketState
));
3945 s
->vc
= qemu_new_vlan_client(vlan
,
3946 net_socket_receive
, NULL
, s
);
3947 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3948 "socket: fd=%d", fd
);
3950 net_socket_connect(s
);
3952 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
3957 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
3960 int so_type
=-1, optlen
=sizeof(so_type
);
3962 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
3963 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
3968 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
3970 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3972 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
3973 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
3974 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3979 static void net_socket_accept(void *opaque
)
3981 NetSocketListenState
*s
= opaque
;
3983 struct sockaddr_in saddr
;
3988 len
= sizeof(saddr
);
3989 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
3990 if (fd
< 0 && errno
!= EINTR
) {
3992 } else if (fd
>= 0) {
3996 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
4000 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
4001 "socket: connection from %s:%d",
4002 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4006 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
4008 NetSocketListenState
*s
;
4010 struct sockaddr_in saddr
;
4012 if (parse_host_port(&saddr
, host_str
) < 0)
4015 s
= qemu_mallocz(sizeof(NetSocketListenState
));
4019 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4024 socket_set_nonblock(fd
);
4026 /* allow fast reuse */
4028 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
4030 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4035 ret
= listen(fd
, 0);
4042 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
4046 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
4049 int fd
, connected
, ret
, err
;
4050 struct sockaddr_in saddr
;
4052 if (parse_host_port(&saddr
, host_str
) < 0)
4055 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4060 socket_set_nonblock(fd
);
4064 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4066 err
= socket_error();
4067 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
4068 } else if (err
== EINPROGRESS
) {
4071 } else if (err
== WSAEALREADY
) {
4084 s
= net_socket_fd_init(vlan
, fd
, connected
);
4087 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4088 "socket: connect to %s:%d",
4089 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4093 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
4097 struct sockaddr_in saddr
;
4099 if (parse_host_port(&saddr
, host_str
) < 0)
4103 fd
= net_socket_mcast_create(&saddr
);
4107 s
= net_socket_fd_init(vlan
, fd
, 0);
4111 s
->dgram_dst
= saddr
;
4113 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4114 "socket: mcast=%s:%d",
4115 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4120 static int get_param_value(char *buf
, int buf_size
,
4121 const char *tag
, const char *str
)
4130 while (*p
!= '\0' && *p
!= '=') {
4131 if ((q
- option
) < sizeof(option
) - 1)
4139 if (!strcmp(tag
, option
)) {
4141 while (*p
!= '\0' && *p
!= ',') {
4142 if ((q
- buf
) < buf_size
- 1)
4149 while (*p
!= '\0' && *p
!= ',') {
4160 static int net_client_init(const char *str
)
4171 while (*p
!= '\0' && *p
!= ',') {
4172 if ((q
- device
) < sizeof(device
) - 1)
4180 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
4181 vlan_id
= strtol(buf
, NULL
, 0);
4183 vlan
= qemu_find_vlan(vlan_id
);
4185 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
4188 if (!strcmp(device
, "nic")) {
4192 if (nb_nics
>= MAX_NICS
) {
4193 fprintf(stderr
, "Too Many NICs\n");
4196 nd
= &nd_table
[nb_nics
];
4197 macaddr
= nd
->macaddr
;
4203 macaddr
[5] = 0x56 + nb_nics
;
4205 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
4206 if (parse_macaddr(macaddr
, buf
) < 0) {
4207 fprintf(stderr
, "invalid syntax for ethernet address\n");
4211 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
4212 nd
->model
= strdup(buf
);
4216 vlan
->nb_guest_devs
++;
4219 if (!strcmp(device
, "none")) {
4220 /* does nothing. It is needed to signal that no network cards
4225 if (!strcmp(device
, "user")) {
4226 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
4227 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
4229 vlan
->nb_host_devs
++;
4230 ret
= net_slirp_init(vlan
);
4234 if (!strcmp(device
, "tap")) {
4236 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4237 fprintf(stderr
, "tap: no interface name\n");
4240 vlan
->nb_host_devs
++;
4241 ret
= tap_win32_init(vlan
, ifname
);
4244 if (!strcmp(device
, "tap")) {
4246 char setup_script
[1024];
4248 vlan
->nb_host_devs
++;
4249 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4250 fd
= strtol(buf
, NULL
, 0);
4252 if (net_tap_fd_init(vlan
, fd
))
4255 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4258 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
4259 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
4261 ret
= net_tap_init(vlan
, ifname
, setup_script
);
4265 if (!strcmp(device
, "socket")) {
4266 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4268 fd
= strtol(buf
, NULL
, 0);
4270 if (net_socket_fd_init(vlan
, fd
, 1))
4272 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
4273 ret
= net_socket_listen_init(vlan
, buf
);
4274 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
4275 ret
= net_socket_connect_init(vlan
, buf
);
4276 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
4277 ret
= net_socket_mcast_init(vlan
, buf
);
4279 fprintf(stderr
, "Unknown socket options: %s\n", p
);
4282 vlan
->nb_host_devs
++;
4285 fprintf(stderr
, "Unknown network device: %s\n", device
);
4289 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
4295 void do_info_network(void)
4298 VLANClientState
*vc
;
4300 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
4301 term_printf("VLAN %d devices:\n", vlan
->id
);
4302 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
4303 term_printf(" %s\n", vc
->info_str
);
4307 /***********************************************************/
4310 static USBPort
*used_usb_ports
;
4311 static USBPort
*free_usb_ports
;
4313 /* ??? Maybe change this to register a hub to keep track of the topology. */
4314 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
4315 usb_attachfn attach
)
4317 port
->opaque
= opaque
;
4318 port
->index
= index
;
4319 port
->attach
= attach
;
4320 port
->next
= free_usb_ports
;
4321 free_usb_ports
= port
;
4324 static int usb_device_add(const char *devname
)
4330 if (!free_usb_ports
)
4333 if (strstart(devname
, "host:", &p
)) {
4334 dev
= usb_host_device_open(p
);
4335 } else if (!strcmp(devname
, "mouse")) {
4336 dev
= usb_mouse_init();
4337 } else if (!strcmp(devname
, "tablet")) {
4338 dev
= usb_tablet_init();
4339 } else if (!strcmp(devname
, "keyboard")) {
4340 dev
= usb_keyboard_init();
4341 } else if (strstart(devname
, "disk:", &p
)) {
4342 dev
= usb_msd_init(p
);
4343 } else if (!strcmp(devname
, "wacom-tablet")) {
4344 dev
= usb_wacom_init();
4351 /* Find a USB port to add the device to. */
4352 port
= free_usb_ports
;
4356 /* Create a new hub and chain it on. */
4357 free_usb_ports
= NULL
;
4358 port
->next
= used_usb_ports
;
4359 used_usb_ports
= port
;
4361 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
4362 usb_attach(port
, hub
);
4363 port
= free_usb_ports
;
4366 free_usb_ports
= port
->next
;
4367 port
->next
= used_usb_ports
;
4368 used_usb_ports
= port
;
4369 usb_attach(port
, dev
);
4373 static int usb_device_del(const char *devname
)
4381 if (!used_usb_ports
)
4384 p
= strchr(devname
, '.');
4387 bus_num
= strtoul(devname
, NULL
, 0);
4388 addr
= strtoul(p
+ 1, NULL
, 0);
4392 lastp
= &used_usb_ports
;
4393 port
= used_usb_ports
;
4394 while (port
&& port
->dev
->addr
!= addr
) {
4395 lastp
= &port
->next
;
4403 *lastp
= port
->next
;
4404 usb_attach(port
, NULL
);
4405 dev
->handle_destroy(dev
);
4406 port
->next
= free_usb_ports
;
4407 free_usb_ports
= port
;
4411 void do_usb_add(const char *devname
)
4414 ret
= usb_device_add(devname
);
4416 term_printf("Could not add USB device '%s'\n", devname
);
4419 void do_usb_del(const char *devname
)
4422 ret
= usb_device_del(devname
);
4424 term_printf("Could not remove USB device '%s'\n", devname
);
4431 const char *speed_str
;
4434 term_printf("USB support not enabled\n");
4438 for (port
= used_usb_ports
; port
; port
= port
->next
) {
4442 switch(dev
->speed
) {
4446 case USB_SPEED_FULL
:
4449 case USB_SPEED_HIGH
:
4456 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
4457 0, dev
->addr
, speed_str
, dev
->devname
);
4461 /***********************************************************/
4462 /* PCMCIA/Cardbus */
4464 static struct pcmcia_socket_entry_s
{
4465 struct pcmcia_socket_s
*socket
;
4466 struct pcmcia_socket_entry_s
*next
;
4467 } *pcmcia_sockets
= 0;
4469 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
4471 struct pcmcia_socket_entry_s
*entry
;
4473 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
4474 entry
->socket
= socket
;
4475 entry
->next
= pcmcia_sockets
;
4476 pcmcia_sockets
= entry
;
4479 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
4481 struct pcmcia_socket_entry_s
*entry
, **ptr
;
4483 ptr
= &pcmcia_sockets
;
4484 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
4485 if (entry
->socket
== socket
) {
4491 void pcmcia_info(void)
4493 struct pcmcia_socket_entry_s
*iter
;
4494 if (!pcmcia_sockets
)
4495 term_printf("No PCMCIA sockets\n");
4497 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
4498 term_printf("%s: %s\n", iter
->socket
->slot_string
,
4499 iter
->socket
->attached
? iter
->socket
->card_string
:
4503 /***********************************************************/
4506 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4510 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4514 static void dumb_refresh(DisplayState
*ds
)
4516 #if defined(CONFIG_SDL)
4521 static void dumb_display_init(DisplayState
*ds
)
4526 ds
->dpy_update
= dumb_update
;
4527 ds
->dpy_resize
= dumb_resize
;
4528 ds
->dpy_refresh
= dumb_refresh
;
4531 /***********************************************************/
4534 #define MAX_IO_HANDLERS 64
4536 typedef struct IOHandlerRecord
{
4538 IOCanRWHandler
*fd_read_poll
;
4540 IOHandler
*fd_write
;
4543 /* temporary data */
4545 struct IOHandlerRecord
*next
;
4548 static IOHandlerRecord
*first_io_handler
;
4550 /* XXX: fd_read_poll should be suppressed, but an API change is
4551 necessary in the character devices to suppress fd_can_read(). */
4552 int qemu_set_fd_handler2(int fd
,
4553 IOCanRWHandler
*fd_read_poll
,
4555 IOHandler
*fd_write
,
4558 IOHandlerRecord
**pioh
, *ioh
;
4560 if (!fd_read
&& !fd_write
) {
4561 pioh
= &first_io_handler
;
4566 if (ioh
->fd
== fd
) {
4573 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4577 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4580 ioh
->next
= first_io_handler
;
4581 first_io_handler
= ioh
;
4584 ioh
->fd_read_poll
= fd_read_poll
;
4585 ioh
->fd_read
= fd_read
;
4586 ioh
->fd_write
= fd_write
;
4587 ioh
->opaque
= opaque
;
4593 int qemu_set_fd_handler(int fd
,
4595 IOHandler
*fd_write
,
4598 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4601 /***********************************************************/
4602 /* Polling handling */
4604 typedef struct PollingEntry
{
4607 struct PollingEntry
*next
;
4610 static PollingEntry
*first_polling_entry
;
4612 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4614 PollingEntry
**ppe
, *pe
;
4615 pe
= qemu_mallocz(sizeof(PollingEntry
));
4619 pe
->opaque
= opaque
;
4620 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
4625 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
4627 PollingEntry
**ppe
, *pe
;
4628 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
4630 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
4639 /***********************************************************/
4640 /* Wait objects support */
4641 typedef struct WaitObjects
{
4643 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
4644 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
4645 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
4648 static WaitObjects wait_objects
= {0};
4650 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4652 WaitObjects
*w
= &wait_objects
;
4654 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
4656 w
->events
[w
->num
] = handle
;
4657 w
->func
[w
->num
] = func
;
4658 w
->opaque
[w
->num
] = opaque
;
4663 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4666 WaitObjects
*w
= &wait_objects
;
4669 for (i
= 0; i
< w
->num
; i
++) {
4670 if (w
->events
[i
] == handle
)
4673 w
->events
[i
] = w
->events
[i
+ 1];
4674 w
->func
[i
] = w
->func
[i
+ 1];
4675 w
->opaque
[i
] = w
->opaque
[i
+ 1];
4683 /***********************************************************/
4684 /* savevm/loadvm support */
4686 #define IO_BUF_SIZE 32768
4690 BlockDriverState
*bs
;
4693 int64_t base_offset
;
4694 int64_t buf_offset
; /* start of buffer when writing, end of buffer
4697 int buf_size
; /* 0 when writing */
4698 uint8_t buf
[IO_BUF_SIZE
];
4701 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
4705 f
= qemu_mallocz(sizeof(QEMUFile
));
4708 if (!strcmp(mode
, "wb")) {
4710 } else if (!strcmp(mode
, "rb")) {
4715 f
->outfile
= fopen(filename
, mode
);
4727 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
4731 f
= qemu_mallocz(sizeof(QEMUFile
));
4736 f
->is_writable
= is_writable
;
4737 f
->base_offset
= offset
;
4741 void qemu_fflush(QEMUFile
*f
)
4743 if (!f
->is_writable
)
4745 if (f
->buf_index
> 0) {
4747 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4748 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
4750 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4751 f
->buf
, f
->buf_index
);
4753 f
->buf_offset
+= f
->buf_index
;
4758 static void qemu_fill_buffer(QEMUFile
*f
)
4765 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4766 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
4770 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4771 f
->buf
, IO_BUF_SIZE
);
4777 f
->buf_offset
+= len
;
4780 void qemu_fclose(QEMUFile
*f
)
4790 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
4794 l
= IO_BUF_SIZE
- f
->buf_index
;
4797 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
4801 if (f
->buf_index
>= IO_BUF_SIZE
)
4806 void qemu_put_byte(QEMUFile
*f
, int v
)
4808 f
->buf
[f
->buf_index
++] = v
;
4809 if (f
->buf_index
>= IO_BUF_SIZE
)
4813 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
4819 l
= f
->buf_size
- f
->buf_index
;
4821 qemu_fill_buffer(f
);
4822 l
= f
->buf_size
- f
->buf_index
;
4828 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
4833 return size1
- size
;
4836 int qemu_get_byte(QEMUFile
*f
)
4838 if (f
->buf_index
>= f
->buf_size
) {
4839 qemu_fill_buffer(f
);
4840 if (f
->buf_index
>= f
->buf_size
)
4843 return f
->buf
[f
->buf_index
++];
4846 int64_t qemu_ftell(QEMUFile
*f
)
4848 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
4851 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
4853 if (whence
== SEEK_SET
) {
4855 } else if (whence
== SEEK_CUR
) {
4856 pos
+= qemu_ftell(f
);
4858 /* SEEK_END not supported */
4861 if (f
->is_writable
) {
4863 f
->buf_offset
= pos
;
4865 f
->buf_offset
= pos
;
4872 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
4874 qemu_put_byte(f
, v
>> 8);
4875 qemu_put_byte(f
, v
);
4878 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
4880 qemu_put_byte(f
, v
>> 24);
4881 qemu_put_byte(f
, v
>> 16);
4882 qemu_put_byte(f
, v
>> 8);
4883 qemu_put_byte(f
, v
);
4886 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
4888 qemu_put_be32(f
, v
>> 32);
4889 qemu_put_be32(f
, v
);
4892 unsigned int qemu_get_be16(QEMUFile
*f
)
4895 v
= qemu_get_byte(f
) << 8;
4896 v
|= qemu_get_byte(f
);
4900 unsigned int qemu_get_be32(QEMUFile
*f
)
4903 v
= qemu_get_byte(f
) << 24;
4904 v
|= qemu_get_byte(f
) << 16;
4905 v
|= qemu_get_byte(f
) << 8;
4906 v
|= qemu_get_byte(f
);
4910 uint64_t qemu_get_be64(QEMUFile
*f
)
4913 v
= (uint64_t)qemu_get_be32(f
) << 32;
4914 v
|= qemu_get_be32(f
);
4918 typedef struct SaveStateEntry
{
4922 SaveStateHandler
*save_state
;
4923 LoadStateHandler
*load_state
;
4925 struct SaveStateEntry
*next
;
4928 static SaveStateEntry
*first_se
;
4930 int register_savevm(const char *idstr
,
4933 SaveStateHandler
*save_state
,
4934 LoadStateHandler
*load_state
,
4937 SaveStateEntry
*se
, **pse
;
4939 se
= qemu_malloc(sizeof(SaveStateEntry
));
4942 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
4943 se
->instance_id
= instance_id
;
4944 se
->version_id
= version_id
;
4945 se
->save_state
= save_state
;
4946 se
->load_state
= load_state
;
4947 se
->opaque
= opaque
;
4950 /* add at the end of list */
4952 while (*pse
!= NULL
)
4953 pse
= &(*pse
)->next
;
4958 #define QEMU_VM_FILE_MAGIC 0x5145564d
4959 #define QEMU_VM_FILE_VERSION 0x00000002
4961 int qemu_savevm_state(QEMUFile
*f
)
4965 int64_t cur_pos
, len_pos
, total_len_pos
;
4967 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
4968 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
4969 total_len_pos
= qemu_ftell(f
);
4970 qemu_put_be64(f
, 0); /* total size */
4972 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4974 len
= strlen(se
->idstr
);
4975 qemu_put_byte(f
, len
);
4976 qemu_put_buffer(f
, se
->idstr
, len
);
4978 qemu_put_be32(f
, se
->instance_id
);
4979 qemu_put_be32(f
, se
->version_id
);
4981 /* record size: filled later */
4982 len_pos
= qemu_ftell(f
);
4983 qemu_put_be32(f
, 0);
4985 se
->save_state(f
, se
->opaque
);
4987 /* fill record size */
4988 cur_pos
= qemu_ftell(f
);
4989 len
= cur_pos
- len_pos
- 4;
4990 qemu_fseek(f
, len_pos
, SEEK_SET
);
4991 qemu_put_be32(f
, len
);
4992 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4994 cur_pos
= qemu_ftell(f
);
4995 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
4996 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
4997 qemu_fseek(f
, cur_pos
, SEEK_SET
);
5003 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
5007 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
5008 if (!strcmp(se
->idstr
, idstr
) &&
5009 instance_id
== se
->instance_id
)
5015 int qemu_loadvm_state(QEMUFile
*f
)
5018 int len
, ret
, instance_id
, record_len
, version_id
;
5019 int64_t total_len
, end_pos
, cur_pos
;
5023 v
= qemu_get_be32(f
);
5024 if (v
!= QEMU_VM_FILE_MAGIC
)
5026 v
= qemu_get_be32(f
);
5027 if (v
!= QEMU_VM_FILE_VERSION
) {
5032 total_len
= qemu_get_be64(f
);
5033 end_pos
= total_len
+ qemu_ftell(f
);
5035 if (qemu_ftell(f
) >= end_pos
)
5037 len
= qemu_get_byte(f
);
5038 qemu_get_buffer(f
, idstr
, len
);
5040 instance_id
= qemu_get_be32(f
);
5041 version_id
= qemu_get_be32(f
);
5042 record_len
= qemu_get_be32(f
);
5044 printf("idstr=%s instance=0x%x version=%d len=%d\n",
5045 idstr
, instance_id
, version_id
, record_len
);
5047 cur_pos
= qemu_ftell(f
);
5048 se
= find_se(idstr
, instance_id
);
5050 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
5051 instance_id
, idstr
);
5053 ret
= se
->load_state(f
, se
->opaque
, version_id
);
5055 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
5056 instance_id
, idstr
);
5059 /* always seek to exact end of record */
5060 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
5067 /* device can contain snapshots */
5068 static int bdrv_can_snapshot(BlockDriverState
*bs
)
5071 !bdrv_is_removable(bs
) &&
5072 !bdrv_is_read_only(bs
));
5075 /* device must be snapshots in order to have a reliable snapshot */
5076 static int bdrv_has_snapshot(BlockDriverState
*bs
)
5079 !bdrv_is_removable(bs
) &&
5080 !bdrv_is_read_only(bs
));
5083 static BlockDriverState
*get_bs_snapshots(void)
5085 BlockDriverState
*bs
;
5089 return bs_snapshots
;
5090 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5092 if (bdrv_can_snapshot(bs
))
5101 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
5104 QEMUSnapshotInfo
*sn_tab
, *sn
;
5108 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5111 for(i
= 0; i
< nb_sns
; i
++) {
5113 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
5123 void do_savevm(const char *name
)
5125 BlockDriverState
*bs
, *bs1
;
5126 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
5127 int must_delete
, ret
, i
;
5128 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5130 int saved_vm_running
;
5137 bs
= get_bs_snapshots();
5139 term_printf("No block device can accept snapshots\n");
5143 /* ??? Should this occur after vm_stop? */
5146 saved_vm_running
= vm_running
;
5151 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
5156 memset(sn
, 0, sizeof(*sn
));
5158 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
5159 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
5162 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
5165 /* fill auxiliary fields */
5168 sn
->date_sec
= tb
.time
;
5169 sn
->date_nsec
= tb
.millitm
* 1000000;
5171 gettimeofday(&tv
, NULL
);
5172 sn
->date_sec
= tv
.tv_sec
;
5173 sn
->date_nsec
= tv
.tv_usec
* 1000;
5175 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
5177 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5178 term_printf("Device %s does not support VM state snapshots\n",
5179 bdrv_get_device_name(bs
));
5183 /* save the VM state */
5184 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
5186 term_printf("Could not open VM state file\n");
5189 ret
= qemu_savevm_state(f
);
5190 sn
->vm_state_size
= qemu_ftell(f
);
5193 term_printf("Error %d while writing VM\n", ret
);
5197 /* create the snapshots */
5199 for(i
= 0; i
< MAX_DISKS
; i
++) {
5201 if (bdrv_has_snapshot(bs1
)) {
5203 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
5205 term_printf("Error while deleting snapshot on '%s'\n",
5206 bdrv_get_device_name(bs1
));
5209 ret
= bdrv_snapshot_create(bs1
, sn
);
5211 term_printf("Error while creating snapshot on '%s'\n",
5212 bdrv_get_device_name(bs1
));
5218 if (saved_vm_running
)
5222 void do_loadvm(const char *name
)
5224 BlockDriverState
*bs
, *bs1
;
5225 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5228 int saved_vm_running
;
5230 bs
= get_bs_snapshots();
5232 term_printf("No block device supports snapshots\n");
5236 /* Flush all IO requests so they don't interfere with the new state. */
5239 saved_vm_running
= vm_running
;
5242 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5244 if (bdrv_has_snapshot(bs1
)) {
5245 ret
= bdrv_snapshot_goto(bs1
, name
);
5248 term_printf("Warning: ");
5251 term_printf("Snapshots not supported on device '%s'\n",
5252 bdrv_get_device_name(bs1
));
5255 term_printf("Could not find snapshot '%s' on device '%s'\n",
5256 name
, bdrv_get_device_name(bs1
));
5259 term_printf("Error %d while activating snapshot on '%s'\n",
5260 ret
, bdrv_get_device_name(bs1
));
5263 /* fatal on snapshot block device */
5270 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5271 term_printf("Device %s does not support VM state snapshots\n",
5272 bdrv_get_device_name(bs
));
5276 /* restore the VM state */
5277 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
5279 term_printf("Could not open VM state file\n");
5282 ret
= qemu_loadvm_state(f
);
5285 term_printf("Error %d while loading VM state\n", ret
);
5288 if (saved_vm_running
)
5292 void do_delvm(const char *name
)
5294 BlockDriverState
*bs
, *bs1
;
5297 bs
= get_bs_snapshots();
5299 term_printf("No block device supports snapshots\n");
5303 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5305 if (bdrv_has_snapshot(bs1
)) {
5306 ret
= bdrv_snapshot_delete(bs1
, name
);
5308 if (ret
== -ENOTSUP
)
5309 term_printf("Snapshots not supported on device '%s'\n",
5310 bdrv_get_device_name(bs1
));
5312 term_printf("Error %d while deleting snapshot on '%s'\n",
5313 ret
, bdrv_get_device_name(bs1
));
5319 void do_info_snapshots(void)
5321 BlockDriverState
*bs
, *bs1
;
5322 QEMUSnapshotInfo
*sn_tab
, *sn
;
5326 bs
= get_bs_snapshots();
5328 term_printf("No available block device supports snapshots\n");
5331 term_printf("Snapshot devices:");
5332 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5334 if (bdrv_has_snapshot(bs1
)) {
5336 term_printf(" %s", bdrv_get_device_name(bs1
));
5341 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5343 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
5346 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
5347 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
5348 for(i
= 0; i
< nb_sns
; i
++) {
5350 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
5355 /***********************************************************/
5356 /* cpu save/restore */
5358 #if defined(TARGET_I386)
5360 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
5362 qemu_put_be32(f
, dt
->selector
);
5363 qemu_put_betl(f
, dt
->base
);
5364 qemu_put_be32(f
, dt
->limit
);
5365 qemu_put_be32(f
, dt
->flags
);
5368 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
5370 dt
->selector
= qemu_get_be32(f
);
5371 dt
->base
= qemu_get_betl(f
);
5372 dt
->limit
= qemu_get_be32(f
);
5373 dt
->flags
= qemu_get_be32(f
);
5376 void cpu_save(QEMUFile
*f
, void *opaque
)
5378 CPUState
*env
= opaque
;
5379 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
5383 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5384 qemu_put_betls(f
, &env
->regs
[i
]);
5385 qemu_put_betls(f
, &env
->eip
);
5386 qemu_put_betls(f
, &env
->eflags
);
5387 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
5388 qemu_put_be32s(f
, &hflags
);
5392 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
5394 for(i
= 0; i
< 8; i
++) {
5395 fptag
|= ((!env
->fptags
[i
]) << i
);
5398 qemu_put_be16s(f
, &fpuc
);
5399 qemu_put_be16s(f
, &fpus
);
5400 qemu_put_be16s(f
, &fptag
);
5402 #ifdef USE_X86LDOUBLE
5407 qemu_put_be16s(f
, &fpregs_format
);
5409 for(i
= 0; i
< 8; i
++) {
5410 #ifdef USE_X86LDOUBLE
5414 /* we save the real CPU data (in case of MMX usage only 'mant'
5415 contains the MMX register */
5416 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
5417 qemu_put_be64(f
, mant
);
5418 qemu_put_be16(f
, exp
);
5421 /* if we use doubles for float emulation, we save the doubles to
5422 avoid losing information in case of MMX usage. It can give
5423 problems if the image is restored on a CPU where long
5424 doubles are used instead. */
5425 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
5429 for(i
= 0; i
< 6; i
++)
5430 cpu_put_seg(f
, &env
->segs
[i
]);
5431 cpu_put_seg(f
, &env
->ldt
);
5432 cpu_put_seg(f
, &env
->tr
);
5433 cpu_put_seg(f
, &env
->gdt
);
5434 cpu_put_seg(f
, &env
->idt
);
5436 qemu_put_be32s(f
, &env
->sysenter_cs
);
5437 qemu_put_be32s(f
, &env
->sysenter_esp
);
5438 qemu_put_be32s(f
, &env
->sysenter_eip
);
5440 qemu_put_betls(f
, &env
->cr
[0]);
5441 qemu_put_betls(f
, &env
->cr
[2]);
5442 qemu_put_betls(f
, &env
->cr
[3]);
5443 qemu_put_betls(f
, &env
->cr
[4]);
5445 for(i
= 0; i
< 8; i
++)
5446 qemu_put_betls(f
, &env
->dr
[i
]);
5449 qemu_put_be32s(f
, &env
->a20_mask
);
5452 qemu_put_be32s(f
, &env
->mxcsr
);
5453 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5454 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5455 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5458 #ifdef TARGET_X86_64
5459 qemu_put_be64s(f
, &env
->efer
);
5460 qemu_put_be64s(f
, &env
->star
);
5461 qemu_put_be64s(f
, &env
->lstar
);
5462 qemu_put_be64s(f
, &env
->cstar
);
5463 qemu_put_be64s(f
, &env
->fmask
);
5464 qemu_put_be64s(f
, &env
->kernelgsbase
);
5466 qemu_put_be32s(f
, &env
->smbase
);
5469 #ifdef USE_X86LDOUBLE
5470 /* XXX: add that in a FPU generic layer */
5471 union x86_longdouble
{
5476 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
5477 #define EXPBIAS1 1023
5478 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
5479 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
5481 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
5485 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
5486 /* exponent + sign */
5487 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
5488 e
|= SIGND1(temp
) >> 16;
5493 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5495 CPUState
*env
= opaque
;
5498 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
5500 if (version_id
!= 3 && version_id
!= 4)
5502 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5503 qemu_get_betls(f
, &env
->regs
[i
]);
5504 qemu_get_betls(f
, &env
->eip
);
5505 qemu_get_betls(f
, &env
->eflags
);
5506 qemu_get_be32s(f
, &hflags
);
5508 qemu_get_be16s(f
, &fpuc
);
5509 qemu_get_be16s(f
, &fpus
);
5510 qemu_get_be16s(f
, &fptag
);
5511 qemu_get_be16s(f
, &fpregs_format
);
5513 /* NOTE: we cannot always restore the FPU state if the image come
5514 from a host with a different 'USE_X86LDOUBLE' define. We guess
5515 if we are in an MMX state to restore correctly in that case. */
5516 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
5517 for(i
= 0; i
< 8; i
++) {
5521 switch(fpregs_format
) {
5523 mant
= qemu_get_be64(f
);
5524 exp
= qemu_get_be16(f
);
5525 #ifdef USE_X86LDOUBLE
5526 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5528 /* difficult case */
5530 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5532 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5536 mant
= qemu_get_be64(f
);
5537 #ifdef USE_X86LDOUBLE
5539 union x86_longdouble
*p
;
5540 /* difficult case */
5541 p
= (void *)&env
->fpregs
[i
];
5546 fp64_to_fp80(p
, mant
);
5550 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5559 /* XXX: restore FPU round state */
5560 env
->fpstt
= (fpus
>> 11) & 7;
5561 env
->fpus
= fpus
& ~0x3800;
5563 for(i
= 0; i
< 8; i
++) {
5564 env
->fptags
[i
] = (fptag
>> i
) & 1;
5567 for(i
= 0; i
< 6; i
++)
5568 cpu_get_seg(f
, &env
->segs
[i
]);
5569 cpu_get_seg(f
, &env
->ldt
);
5570 cpu_get_seg(f
, &env
->tr
);
5571 cpu_get_seg(f
, &env
->gdt
);
5572 cpu_get_seg(f
, &env
->idt
);
5574 qemu_get_be32s(f
, &env
->sysenter_cs
);
5575 qemu_get_be32s(f
, &env
->sysenter_esp
);
5576 qemu_get_be32s(f
, &env
->sysenter_eip
);
5578 qemu_get_betls(f
, &env
->cr
[0]);
5579 qemu_get_betls(f
, &env
->cr
[2]);
5580 qemu_get_betls(f
, &env
->cr
[3]);
5581 qemu_get_betls(f
, &env
->cr
[4]);
5583 for(i
= 0; i
< 8; i
++)
5584 qemu_get_betls(f
, &env
->dr
[i
]);
5587 qemu_get_be32s(f
, &env
->a20_mask
);
5589 qemu_get_be32s(f
, &env
->mxcsr
);
5590 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5591 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5592 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5595 #ifdef TARGET_X86_64
5596 qemu_get_be64s(f
, &env
->efer
);
5597 qemu_get_be64s(f
, &env
->star
);
5598 qemu_get_be64s(f
, &env
->lstar
);
5599 qemu_get_be64s(f
, &env
->cstar
);
5600 qemu_get_be64s(f
, &env
->fmask
);
5601 qemu_get_be64s(f
, &env
->kernelgsbase
);
5603 if (version_id
>= 4)
5604 qemu_get_be32s(f
, &env
->smbase
);
5606 /* XXX: compute hflags from scratch, except for CPL and IIF */
5607 env
->hflags
= hflags
;
5612 #elif defined(TARGET_PPC)
5613 void cpu_save(QEMUFile
*f
, void *opaque
)
5617 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5622 #elif defined(TARGET_MIPS)
5623 void cpu_save(QEMUFile
*f
, void *opaque
)
5627 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5632 #elif defined(TARGET_SPARC)
5633 void cpu_save(QEMUFile
*f
, void *opaque
)
5635 CPUState
*env
= opaque
;
5639 for(i
= 0; i
< 8; i
++)
5640 qemu_put_betls(f
, &env
->gregs
[i
]);
5641 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5642 qemu_put_betls(f
, &env
->regbase
[i
]);
5645 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5651 qemu_put_be32(f
, u
.i
);
5654 qemu_put_betls(f
, &env
->pc
);
5655 qemu_put_betls(f
, &env
->npc
);
5656 qemu_put_betls(f
, &env
->y
);
5658 qemu_put_be32(f
, tmp
);
5659 qemu_put_betls(f
, &env
->fsr
);
5660 qemu_put_betls(f
, &env
->tbr
);
5661 #ifndef TARGET_SPARC64
5662 qemu_put_be32s(f
, &env
->wim
);
5664 for(i
= 0; i
< 16; i
++)
5665 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
5669 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5671 CPUState
*env
= opaque
;
5675 for(i
= 0; i
< 8; i
++)
5676 qemu_get_betls(f
, &env
->gregs
[i
]);
5677 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5678 qemu_get_betls(f
, &env
->regbase
[i
]);
5681 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5686 u
.i
= qemu_get_be32(f
);
5690 qemu_get_betls(f
, &env
->pc
);
5691 qemu_get_betls(f
, &env
->npc
);
5692 qemu_get_betls(f
, &env
->y
);
5693 tmp
= qemu_get_be32(f
);
5694 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
5695 correctly updated */
5697 qemu_get_betls(f
, &env
->fsr
);
5698 qemu_get_betls(f
, &env
->tbr
);
5699 #ifndef TARGET_SPARC64
5700 qemu_get_be32s(f
, &env
->wim
);
5702 for(i
= 0; i
< 16; i
++)
5703 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
5709 #elif defined(TARGET_ARM)
5711 void cpu_save(QEMUFile
*f
, void *opaque
)
5714 CPUARMState
*env
= (CPUARMState
*)opaque
;
5716 for (i
= 0; i
< 16; i
++) {
5717 qemu_put_be32(f
, env
->regs
[i
]);
5719 qemu_put_be32(f
, cpsr_read(env
));
5720 qemu_put_be32(f
, env
->spsr
);
5721 for (i
= 0; i
< 6; i
++) {
5722 qemu_put_be32(f
, env
->banked_spsr
[i
]);
5723 qemu_put_be32(f
, env
->banked_r13
[i
]);
5724 qemu_put_be32(f
, env
->banked_r14
[i
]);
5726 for (i
= 0; i
< 5; i
++) {
5727 qemu_put_be32(f
, env
->usr_regs
[i
]);
5728 qemu_put_be32(f
, env
->fiq_regs
[i
]);
5730 qemu_put_be32(f
, env
->cp15
.c0_cpuid
);
5731 qemu_put_be32(f
, env
->cp15
.c0_cachetype
);
5732 qemu_put_be32(f
, env
->cp15
.c1_sys
);
5733 qemu_put_be32(f
, env
->cp15
.c1_coproc
);
5734 qemu_put_be32(f
, env
->cp15
.c1_xscaleauxcr
);
5735 qemu_put_be32(f
, env
->cp15
.c2_base
);
5736 qemu_put_be32(f
, env
->cp15
.c2_data
);
5737 qemu_put_be32(f
, env
->cp15
.c2_insn
);
5738 qemu_put_be32(f
, env
->cp15
.c3
);
5739 qemu_put_be32(f
, env
->cp15
.c5_insn
);
5740 qemu_put_be32(f
, env
->cp15
.c5_data
);
5741 for (i
= 0; i
< 8; i
++) {
5742 qemu_put_be32(f
, env
->cp15
.c6_region
[i
]);
5744 qemu_put_be32(f
, env
->cp15
.c6_insn
);
5745 qemu_put_be32(f
, env
->cp15
.c6_data
);
5746 qemu_put_be32(f
, env
->cp15
.c9_insn
);
5747 qemu_put_be32(f
, env
->cp15
.c9_data
);
5748 qemu_put_be32(f
, env
->cp15
.c13_fcse
);
5749 qemu_put_be32(f
, env
->cp15
.c13_context
);
5750 qemu_put_be32(f
, env
->cp15
.c15_cpar
);
5752 qemu_put_be32(f
, env
->features
);
5754 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
5755 for (i
= 0; i
< 16; i
++) {
5757 u
.d
= env
->vfp
.regs
[i
];
5758 qemu_put_be32(f
, u
.l
.upper
);
5759 qemu_put_be32(f
, u
.l
.lower
);
5761 for (i
= 0; i
< 16; i
++) {
5762 qemu_put_be32(f
, env
->vfp
.xregs
[i
]);
5765 /* TODO: Should use proper FPSCR access functions. */
5766 qemu_put_be32(f
, env
->vfp
.vec_len
);
5767 qemu_put_be32(f
, env
->vfp
.vec_stride
);
5770 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
5771 for (i
= 0; i
< 16; i
++) {
5772 qemu_put_be64(f
, env
->iwmmxt
.regs
[i
]);
5774 for (i
= 0; i
< 16; i
++) {
5775 qemu_put_be32(f
, env
->iwmmxt
.cregs
[i
]);
5780 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5782 CPUARMState
*env
= (CPUARMState
*)opaque
;
5785 if (version_id
!= 0)
5788 for (i
= 0; i
< 16; i
++) {
5789 env
->regs
[i
] = qemu_get_be32(f
);
5791 cpsr_write(env
, qemu_get_be32(f
), 0xffffffff);
5792 env
->spsr
= qemu_get_be32(f
);
5793 for (i
= 0; i
< 6; i
++) {
5794 env
->banked_spsr
[i
] = qemu_get_be32(f
);
5795 env
->banked_r13
[i
] = qemu_get_be32(f
);
5796 env
->banked_r14
[i
] = qemu_get_be32(f
);
5798 for (i
= 0; i
< 5; i
++) {
5799 env
->usr_regs
[i
] = qemu_get_be32(f
);
5800 env
->fiq_regs
[i
] = qemu_get_be32(f
);
5802 env
->cp15
.c0_cpuid
= qemu_get_be32(f
);
5803 env
->cp15
.c0_cachetype
= qemu_get_be32(f
);
5804 env
->cp15
.c1_sys
= qemu_get_be32(f
);
5805 env
->cp15
.c1_coproc
= qemu_get_be32(f
);
5806 env
->cp15
.c1_xscaleauxcr
= qemu_get_be32(f
);
5807 env
->cp15
.c2_base
= qemu_get_be32(f
);
5808 env
->cp15
.c2_data
= qemu_get_be32(f
);
5809 env
->cp15
.c2_insn
= qemu_get_be32(f
);
5810 env
->cp15
.c3
= qemu_get_be32(f
);
5811 env
->cp15
.c5_insn
= qemu_get_be32(f
);
5812 env
->cp15
.c5_data
= qemu_get_be32(f
);
5813 for (i
= 0; i
< 8; i
++) {
5814 env
->cp15
.c6_region
[i
] = qemu_get_be32(f
);
5816 env
->cp15
.c6_insn
= qemu_get_be32(f
);
5817 env
->cp15
.c6_data
= qemu_get_be32(f
);
5818 env
->cp15
.c9_insn
= qemu_get_be32(f
);
5819 env
->cp15
.c9_data
= qemu_get_be32(f
);
5820 env
->cp15
.c13_fcse
= qemu_get_be32(f
);
5821 env
->cp15
.c13_context
= qemu_get_be32(f
);
5822 env
->cp15
.c15_cpar
= qemu_get_be32(f
);
5824 env
->features
= qemu_get_be32(f
);
5826 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
5827 for (i
= 0; i
< 16; i
++) {
5829 u
.l
.upper
= qemu_get_be32(f
);
5830 u
.l
.lower
= qemu_get_be32(f
);
5831 env
->vfp
.regs
[i
] = u
.d
;
5833 for (i
= 0; i
< 16; i
++) {
5834 env
->vfp
.xregs
[i
] = qemu_get_be32(f
);
5837 /* TODO: Should use proper FPSCR access functions. */
5838 env
->vfp
.vec_len
= qemu_get_be32(f
);
5839 env
->vfp
.vec_stride
= qemu_get_be32(f
);
5842 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
5843 for (i
= 0; i
< 16; i
++) {
5844 env
->iwmmxt
.regs
[i
] = qemu_get_be64(f
);
5846 for (i
= 0; i
< 16; i
++) {
5847 env
->iwmmxt
.cregs
[i
] = qemu_get_be32(f
);
5856 #warning No CPU save/restore functions
5860 /***********************************************************/
5861 /* ram save/restore */
5863 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
5867 v
= qemu_get_byte(f
);
5870 if (qemu_get_buffer(f
, buf
, len
) != len
)
5874 v
= qemu_get_byte(f
);
5875 memset(buf
, v
, len
);
5883 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
5887 if (qemu_get_be32(f
) != phys_ram_size
)
5889 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
5890 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
5897 #define BDRV_HASH_BLOCK_SIZE 1024
5898 #define IOBUF_SIZE 4096
5899 #define RAM_CBLOCK_MAGIC 0xfabe
5901 typedef struct RamCompressState
{
5904 uint8_t buf
[IOBUF_SIZE
];
5907 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
5910 memset(s
, 0, sizeof(*s
));
5912 ret
= deflateInit2(&s
->zstream
, 1,
5914 9, Z_DEFAULT_STRATEGY
);
5917 s
->zstream
.avail_out
= IOBUF_SIZE
;
5918 s
->zstream
.next_out
= s
->buf
;
5922 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
5924 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
5925 qemu_put_be16(s
->f
, len
);
5926 qemu_put_buffer(s
->f
, buf
, len
);
5929 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
5933 s
->zstream
.avail_in
= len
;
5934 s
->zstream
.next_in
= (uint8_t *)buf
;
5935 while (s
->zstream
.avail_in
> 0) {
5936 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
5939 if (s
->zstream
.avail_out
== 0) {
5940 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
5941 s
->zstream
.avail_out
= IOBUF_SIZE
;
5942 s
->zstream
.next_out
= s
->buf
;
5948 static void ram_compress_close(RamCompressState
*s
)
5952 /* compress last bytes */
5954 ret
= deflate(&s
->zstream
, Z_FINISH
);
5955 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
5956 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
5958 ram_put_cblock(s
, s
->buf
, len
);
5960 s
->zstream
.avail_out
= IOBUF_SIZE
;
5961 s
->zstream
.next_out
= s
->buf
;
5962 if (ret
== Z_STREAM_END
)
5969 deflateEnd(&s
->zstream
);
5972 typedef struct RamDecompressState
{
5975 uint8_t buf
[IOBUF_SIZE
];
5976 } RamDecompressState
;
5978 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
5981 memset(s
, 0, sizeof(*s
));
5983 ret
= inflateInit(&s
->zstream
);
5989 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
5993 s
->zstream
.avail_out
= len
;
5994 s
->zstream
.next_out
= buf
;
5995 while (s
->zstream
.avail_out
> 0) {
5996 if (s
->zstream
.avail_in
== 0) {
5997 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
5999 clen
= qemu_get_be16(s
->f
);
6000 if (clen
> IOBUF_SIZE
)
6002 qemu_get_buffer(s
->f
, s
->buf
, clen
);
6003 s
->zstream
.avail_in
= clen
;
6004 s
->zstream
.next_in
= s
->buf
;
6006 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
6007 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
6014 static void ram_decompress_close(RamDecompressState
*s
)
6016 inflateEnd(&s
->zstream
);
6019 static void ram_save(QEMUFile
*f
, void *opaque
)
6022 RamCompressState s1
, *s
= &s1
;
6025 qemu_put_be32(f
, phys_ram_size
);
6026 if (ram_compress_open(s
, f
) < 0)
6028 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6030 if (tight_savevm_enabled
) {
6034 /* find if the memory block is available on a virtual
6037 for(j
= 0; j
< MAX_DISKS
; j
++) {
6039 sector_num
= bdrv_hash_find(bs_table
[j
],
6040 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6041 if (sector_num
>= 0)
6046 goto normal_compress
;
6049 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
6050 ram_compress_buf(s
, buf
, 10);
6056 ram_compress_buf(s
, buf
, 1);
6057 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6060 ram_compress_close(s
);
6063 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
6065 RamDecompressState s1
, *s
= &s1
;
6069 if (version_id
== 1)
6070 return ram_load_v1(f
, opaque
);
6071 if (version_id
!= 2)
6073 if (qemu_get_be32(f
) != phys_ram_size
)
6075 if (ram_decompress_open(s
, f
) < 0)
6077 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6078 if (ram_decompress_buf(s
, buf
, 1) < 0) {
6079 fprintf(stderr
, "Error while reading ram block header\n");
6083 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
6084 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
6093 ram_decompress_buf(s
, buf
+ 1, 9);
6095 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
6096 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
6097 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
6100 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
6101 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
6102 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
6103 bs_index
, sector_num
);
6110 printf("Error block header\n");
6114 ram_decompress_close(s
);
6118 /***********************************************************/
6119 /* bottom halves (can be seen as timers which expire ASAP) */
6128 static QEMUBH
*first_bh
= NULL
;
6130 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
6133 bh
= qemu_mallocz(sizeof(QEMUBH
));
6137 bh
->opaque
= opaque
;
6141 int qemu_bh_poll(void)
6160 void qemu_bh_schedule(QEMUBH
*bh
)
6162 CPUState
*env
= cpu_single_env
;
6166 bh
->next
= first_bh
;
6169 /* stop the currently executing CPU to execute the BH ASAP */
6171 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
6175 void qemu_bh_cancel(QEMUBH
*bh
)
6178 if (bh
->scheduled
) {
6181 pbh
= &(*pbh
)->next
;
6187 void qemu_bh_delete(QEMUBH
*bh
)
6193 /***********************************************************/
6194 /* machine registration */
6196 QEMUMachine
*first_machine
= NULL
;
6198 int qemu_register_machine(QEMUMachine
*m
)
6201 pm
= &first_machine
;
6209 QEMUMachine
*find_machine(const char *name
)
6213 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6214 if (!strcmp(m
->name
, name
))
6220 /***********************************************************/
6221 /* main execution loop */
6223 void gui_update(void *opaque
)
6225 DisplayState
*ds
= opaque
;
6226 ds
->dpy_refresh(ds
);
6227 qemu_mod_timer(ds
->gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
6230 struct vm_change_state_entry
{
6231 VMChangeStateHandler
*cb
;
6233 LIST_ENTRY (vm_change_state_entry
) entries
;
6236 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
6238 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
6241 VMChangeStateEntry
*e
;
6243 e
= qemu_mallocz(sizeof (*e
));
6249 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
6253 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
6255 LIST_REMOVE (e
, entries
);
6259 static void vm_state_notify(int running
)
6261 VMChangeStateEntry
*e
;
6263 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
6264 e
->cb(e
->opaque
, running
);
6268 /* XXX: support several handlers */
6269 static VMStopHandler
*vm_stop_cb
;
6270 static void *vm_stop_opaque
;
6272 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6275 vm_stop_opaque
= opaque
;
6279 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6293 void vm_stop(int reason
)
6296 cpu_disable_ticks();
6300 vm_stop_cb(vm_stop_opaque
, reason
);
6307 /* reset/shutdown handler */
6309 typedef struct QEMUResetEntry
{
6310 QEMUResetHandler
*func
;
6312 struct QEMUResetEntry
*next
;
6315 static QEMUResetEntry
*first_reset_entry
;
6316 static int reset_requested
;
6317 static int shutdown_requested
;
6318 static int powerdown_requested
;
6320 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
6322 QEMUResetEntry
**pre
, *re
;
6324 pre
= &first_reset_entry
;
6325 while (*pre
!= NULL
)
6326 pre
= &(*pre
)->next
;
6327 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
6329 re
->opaque
= opaque
;
6334 static void qemu_system_reset(void)
6338 /* reset all devices */
6339 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
6340 re
->func(re
->opaque
);
6344 void qemu_system_reset_request(void)
6347 shutdown_requested
= 1;
6349 reset_requested
= 1;
6352 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6355 void qemu_system_shutdown_request(void)
6357 shutdown_requested
= 1;
6359 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6362 void qemu_system_powerdown_request(void)
6364 powerdown_requested
= 1;
6366 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6369 void main_loop_wait(int timeout
)
6371 IOHandlerRecord
*ioh
;
6372 fd_set rfds
, wfds
, xfds
;
6381 /* XXX: need to suppress polling by better using win32 events */
6383 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
6384 ret
|= pe
->func(pe
->opaque
);
6389 WaitObjects
*w
= &wait_objects
;
6391 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
6392 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
6393 if (w
->func
[ret
- WAIT_OBJECT_0
])
6394 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
6396 /* Check for additional signaled events */
6397 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
6399 /* Check if event is signaled */
6400 ret2
= WaitForSingleObject(w
->events
[i
], 0);
6401 if(ret2
== WAIT_OBJECT_0
) {
6403 w
->func
[i
](w
->opaque
[i
]);
6404 } else if (ret2
== WAIT_TIMEOUT
) {
6406 err
= GetLastError();
6407 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
6410 } else if (ret
== WAIT_TIMEOUT
) {
6412 err
= GetLastError();
6413 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
6417 /* poll any events */
6418 /* XXX: separate device handlers from system ones */
6423 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6427 (!ioh
->fd_read_poll
||
6428 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
6429 FD_SET(ioh
->fd
, &rfds
);
6433 if (ioh
->fd_write
) {
6434 FD_SET(ioh
->fd
, &wfds
);
6444 tv
.tv_usec
= timeout
* 1000;
6446 #if defined(CONFIG_SLIRP)
6448 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
6451 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
6453 IOHandlerRecord
**pioh
;
6455 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6458 if (FD_ISSET(ioh
->fd
, &rfds
)) {
6459 ioh
->fd_read(ioh
->opaque
);
6461 if (FD_ISSET(ioh
->fd
, &wfds
)) {
6462 ioh
->fd_write(ioh
->opaque
);
6466 /* remove deleted IO handlers */
6467 pioh
= &first_io_handler
;
6477 #if defined(CONFIG_SLIRP)
6484 slirp_select_poll(&rfds
, &wfds
, &xfds
);
6490 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
6491 qemu_get_clock(vm_clock
));
6492 /* run dma transfers, if any */
6496 /* real time timers */
6497 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
6498 qemu_get_clock(rt_clock
));
6500 /* Check bottom-halves last in case any of the earlier events triggered
6506 static CPUState
*cur_cpu
;
6511 #ifdef CONFIG_PROFILER
6516 cur_cpu
= first_cpu
;
6523 env
= env
->next_cpu
;
6526 #ifdef CONFIG_PROFILER
6527 ti
= profile_getclock();
6529 ret
= cpu_exec(env
);
6530 #ifdef CONFIG_PROFILER
6531 qemu_time
+= profile_getclock() - ti
;
6533 if (ret
== EXCP_HLT
) {
6534 /* Give the next CPU a chance to run. */
6538 if (ret
!= EXCP_HALTED
)
6540 /* all CPUs are halted ? */
6546 if (shutdown_requested
) {
6547 ret
= EXCP_INTERRUPT
;
6550 if (reset_requested
) {
6551 reset_requested
= 0;
6552 qemu_system_reset();
6553 ret
= EXCP_INTERRUPT
;
6555 if (powerdown_requested
) {
6556 powerdown_requested
= 0;
6557 qemu_system_powerdown();
6558 ret
= EXCP_INTERRUPT
;
6560 if (ret
== EXCP_DEBUG
) {
6561 vm_stop(EXCP_DEBUG
);
6563 /* If all cpus are halted then wait until the next IRQ */
6564 /* XXX: use timeout computed from timers */
6565 if (ret
== EXCP_HALTED
)
6572 #ifdef CONFIG_PROFILER
6573 ti
= profile_getclock();
6575 main_loop_wait(timeout
);
6576 #ifdef CONFIG_PROFILER
6577 dev_time
+= profile_getclock() - ti
;
6580 cpu_disable_ticks();
6584 static void help(int exitcode
)
6586 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2007 Fabrice Bellard\n"
6587 "usage: %s [options] [disk_image]\n"
6589 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
6591 "Standard options:\n"
6592 "-M machine select emulated machine (-M ? for list)\n"
6593 "-cpu cpu select CPU (-cpu ? for list)\n"
6594 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
6595 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
6596 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
6597 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
6598 "-mtdblock file use 'file' as on-board Flash memory image\n"
6599 "-sd file use 'file' as SecureDigital card image\n"
6600 "-pflash file use 'file' as a parallel flash image\n"
6601 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
6602 "-snapshot write to temporary files instead of disk image files\n"
6604 "-no-frame open SDL window without a frame and window decorations\n"
6605 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
6606 "-no-quit disable SDL window close capability\n"
6609 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
6611 "-m megs set virtual RAM size to megs MB [default=%d]\n"
6612 "-smp n set the number of CPUs to 'n' [default=1]\n"
6613 "-nographic disable graphical output and redirect serial I/Os to console\n"
6614 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
6616 "-k language use keyboard layout (for example \"fr\" for French)\n"
6619 "-audio-help print list of audio drivers and their options\n"
6620 "-soundhw c1,... enable audio support\n"
6621 " and only specified sound cards (comma separated list)\n"
6622 " use -soundhw ? to get the list of supported cards\n"
6623 " use -soundhw all to enable all of them\n"
6625 "-localtime set the real time clock to local time [default=utc]\n"
6626 "-full-screen start in full screen\n"
6628 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
6630 "-usb enable the USB driver (will be the default soon)\n"
6631 "-usbdevice name add the host or guest USB device 'name'\n"
6632 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6633 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
6635 "-name string set the name of the guest\n"
6637 "Network options:\n"
6638 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
6639 " create a new Network Interface Card and connect it to VLAN 'n'\n"
6641 "-net user[,vlan=n][,hostname=host]\n"
6642 " connect the user mode network stack to VLAN 'n' and send\n"
6643 " hostname 'host' to DHCP clients\n"
6646 "-net tap[,vlan=n],ifname=name\n"
6647 " connect the host TAP network interface to VLAN 'n'\n"
6649 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
6650 " connect the host TAP network interface to VLAN 'n' and use\n"
6651 " the network script 'file' (default=%s);\n"
6652 " use 'script=no' to disable script execution;\n"
6653 " use 'fd=h' to connect to an already opened TAP interface\n"
6655 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
6656 " connect the vlan 'n' to another VLAN using a socket connection\n"
6657 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
6658 " connect the vlan 'n' to multicast maddr and port\n"
6659 "-net none use it alone to have zero network devices; if no -net option\n"
6660 " is provided, the default is '-net nic -net user'\n"
6663 "-tftp dir allow tftp access to files in dir [-net user]\n"
6664 "-bootp file advertise file in BOOTP replies\n"
6666 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
6668 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
6669 " redirect TCP or UDP connections from host to guest [-net user]\n"
6672 "Linux boot specific:\n"
6673 "-kernel bzImage use 'bzImage' as kernel image\n"
6674 "-append cmdline use 'cmdline' as kernel command line\n"
6675 "-initrd file use 'file' as initial ram disk\n"
6677 "Debug/Expert options:\n"
6678 "-monitor dev redirect the monitor to char device 'dev'\n"
6679 "-serial dev redirect the serial port to char device 'dev'\n"
6680 "-parallel dev redirect the parallel port to char device 'dev'\n"
6681 "-pidfile file Write PID to 'file'\n"
6682 "-S freeze CPU at startup (use 'c' to start execution)\n"
6683 "-s wait gdb connection to port\n"
6684 "-p port set gdb connection port [default=%s]\n"
6685 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
6686 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
6687 " translation (t=none or lba) (usually qemu can guess them)\n"
6688 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
6690 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
6691 "-no-kqemu disable KQEMU kernel module usage\n"
6693 #ifdef USE_CODE_COPY
6694 "-no-code-copy disable code copy acceleration\n"
6697 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
6698 " (default is CL-GD5446 PCI VGA)\n"
6699 "-no-acpi disable ACPI\n"
6701 "-no-reboot exit instead of rebooting\n"
6702 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
6703 "-vnc display start a VNC server on display\n"
6705 "-daemonize daemonize QEMU after initializing\n"
6707 "-option-rom rom load a file, rom, into the option ROM space\n"
6709 "-prom-env variable=value set OpenBIOS nvram variables\n"
6712 "During emulation, the following keys are useful:\n"
6713 "ctrl-alt-f toggle full screen\n"
6714 "ctrl-alt-n switch to virtual console 'n'\n"
6715 "ctrl-alt toggle mouse and keyboard grab\n"
6717 "When using -nographic, press 'ctrl-a h' to get some help.\n"
6722 DEFAULT_NETWORK_SCRIPT
,
6724 DEFAULT_GDBSTUB_PORT
,
6729 #define HAS_ARG 0x0001
6743 QEMU_OPTION_mtdblock
,
6747 QEMU_OPTION_snapshot
,
6749 QEMU_OPTION_no_fd_bootchk
,
6752 QEMU_OPTION_nographic
,
6753 QEMU_OPTION_portrait
,
6755 QEMU_OPTION_audio_help
,
6756 QEMU_OPTION_soundhw
,
6775 QEMU_OPTION_no_code_copy
,
6777 QEMU_OPTION_localtime
,
6778 QEMU_OPTION_cirrusvga
,
6781 QEMU_OPTION_std_vga
,
6783 QEMU_OPTION_monitor
,
6785 QEMU_OPTION_parallel
,
6787 QEMU_OPTION_full_screen
,
6788 QEMU_OPTION_no_frame
,
6789 QEMU_OPTION_alt_grab
,
6790 QEMU_OPTION_no_quit
,
6791 QEMU_OPTION_pidfile
,
6792 QEMU_OPTION_no_kqemu
,
6793 QEMU_OPTION_kernel_kqemu
,
6794 QEMU_OPTION_win2k_hack
,
6796 QEMU_OPTION_usbdevice
,
6799 QEMU_OPTION_no_acpi
,
6800 QEMU_OPTION_no_reboot
,
6801 QEMU_OPTION_show_cursor
,
6802 QEMU_OPTION_daemonize
,
6803 QEMU_OPTION_option_rom
,
6804 QEMU_OPTION_semihosting
,
6806 QEMU_OPTION_prom_env
,
6807 QEMU_OPTION_old_param
,
6810 typedef struct QEMUOption
{
6816 const QEMUOption qemu_options
[] = {
6817 { "h", 0, QEMU_OPTION_h
},
6818 { "help", 0, QEMU_OPTION_h
},
6820 { "M", HAS_ARG
, QEMU_OPTION_M
},
6821 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
6822 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
6823 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
6824 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
6825 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
6826 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
6827 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
6828 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
6829 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
6830 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
6831 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
6832 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
6833 { "snapshot", 0, QEMU_OPTION_snapshot
},
6835 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
6837 { "m", HAS_ARG
, QEMU_OPTION_m
},
6838 { "nographic", 0, QEMU_OPTION_nographic
},
6839 { "portrait", 0, QEMU_OPTION_portrait
},
6840 { "k", HAS_ARG
, QEMU_OPTION_k
},
6842 { "audio-help", 0, QEMU_OPTION_audio_help
},
6843 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
6846 { "net", HAS_ARG
, QEMU_OPTION_net
},
6848 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
6849 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
6851 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
6853 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
6856 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
6857 { "append", HAS_ARG
, QEMU_OPTION_append
},
6858 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
6860 { "S", 0, QEMU_OPTION_S
},
6861 { "s", 0, QEMU_OPTION_s
},
6862 { "p", HAS_ARG
, QEMU_OPTION_p
},
6863 { "d", HAS_ARG
, QEMU_OPTION_d
},
6864 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
6865 { "L", HAS_ARG
, QEMU_OPTION_L
},
6866 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
6868 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
6869 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
6871 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6872 { "g", 1, QEMU_OPTION_g
},
6874 { "localtime", 0, QEMU_OPTION_localtime
},
6875 { "std-vga", 0, QEMU_OPTION_std_vga
},
6876 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
6877 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
6878 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
6879 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
6880 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
6881 { "full-screen", 0, QEMU_OPTION_full_screen
},
6883 { "no-frame", 0, QEMU_OPTION_no_frame
},
6884 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
6885 { "no-quit", 0, QEMU_OPTION_no_quit
},
6887 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
6888 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
6889 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
6890 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
6891 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
6893 /* temporary options */
6894 { "usb", 0, QEMU_OPTION_usb
},
6895 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
6896 { "vmwarevga", 0, QEMU_OPTION_vmsvga
},
6897 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
6898 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
6899 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
6900 { "daemonize", 0, QEMU_OPTION_daemonize
},
6901 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
6902 #if defined(TARGET_ARM) || defined(TARGET_M68K)
6903 { "semihosting", 0, QEMU_OPTION_semihosting
},
6905 { "name", HAS_ARG
, QEMU_OPTION_name
},
6906 #if defined(TARGET_SPARC)
6907 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
6909 #if defined(TARGET_ARM)
6910 { "old-param", 0, QEMU_OPTION_old_param
},
6915 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
6917 /* this stack is only used during signal handling */
6918 #define SIGNAL_STACK_SIZE 32768
6920 static uint8_t *signal_stack
;
6924 /* password input */
6926 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
6931 if (!bdrv_is_encrypted(bs
))
6934 term_printf("%s is encrypted.\n", name
);
6935 for(i
= 0; i
< 3; i
++) {
6936 monitor_readline("Password: ", 1, password
, sizeof(password
));
6937 if (bdrv_set_key(bs
, password
) == 0)
6939 term_printf("invalid password\n");
6944 static BlockDriverState
*get_bdrv(int index
)
6946 BlockDriverState
*bs
;
6949 bs
= bs_table
[index
];
6950 } else if (index
< 6) {
6951 bs
= fd_table
[index
- 4];
6958 static void read_passwords(void)
6960 BlockDriverState
*bs
;
6963 for(i
= 0; i
< 6; i
++) {
6966 qemu_key_check(bs
, bdrv_get_device_name(bs
));
6970 /* XXX: currently we cannot use simultaneously different CPUs */
6971 void register_machines(void)
6973 #if defined(TARGET_I386)
6974 qemu_register_machine(&pc_machine
);
6975 qemu_register_machine(&isapc_machine
);
6976 #elif defined(TARGET_PPC)
6977 qemu_register_machine(&heathrow_machine
);
6978 qemu_register_machine(&core99_machine
);
6979 qemu_register_machine(&prep_machine
);
6980 qemu_register_machine(&ref405ep_machine
);
6981 qemu_register_machine(&taihu_machine
);
6982 #elif defined(TARGET_MIPS)
6983 qemu_register_machine(&mips_machine
);
6984 qemu_register_machine(&mips_malta_machine
);
6985 qemu_register_machine(&mips_pica61_machine
);
6986 #elif defined(TARGET_SPARC)
6987 #ifdef TARGET_SPARC64
6988 qemu_register_machine(&sun4u_machine
);
6990 qemu_register_machine(&ss5_machine
);
6991 qemu_register_machine(&ss10_machine
);
6993 #elif defined(TARGET_ARM)
6994 qemu_register_machine(&integratorcp_machine
);
6995 qemu_register_machine(&versatilepb_machine
);
6996 qemu_register_machine(&versatileab_machine
);
6997 qemu_register_machine(&realview_machine
);
6998 qemu_register_machine(&akitapda_machine
);
6999 qemu_register_machine(&spitzpda_machine
);
7000 qemu_register_machine(&borzoipda_machine
);
7001 qemu_register_machine(&terrierpda_machine
);
7002 qemu_register_machine(&palmte_machine
);
7003 #elif defined(TARGET_SH4)
7004 qemu_register_machine(&shix_machine
);
7005 #elif defined(TARGET_ALPHA)
7007 #elif defined(TARGET_M68K)
7008 qemu_register_machine(&mcf5208evb_machine
);
7009 qemu_register_machine(&an5206_machine
);
7011 #error unsupported CPU
7016 struct soundhw soundhw
[] = {
7017 #ifdef HAS_AUDIO_CHOICE
7024 { .init_isa
= pcspk_audio_init
}
7029 "Creative Sound Blaster 16",
7032 { .init_isa
= SB16_init
}
7039 "Yamaha YMF262 (OPL3)",
7041 "Yamaha YM3812 (OPL2)",
7045 { .init_isa
= Adlib_init
}
7052 "Gravis Ultrasound GF1",
7055 { .init_isa
= GUS_init
}
7061 "ENSONIQ AudioPCI ES1370",
7064 { .init_pci
= es1370_init
}
7068 { NULL
, NULL
, 0, 0, { NULL
} }
7071 static void select_soundhw (const char *optarg
)
7075 if (*optarg
== '?') {
7078 printf ("Valid sound card names (comma separated):\n");
7079 for (c
= soundhw
; c
->name
; ++c
) {
7080 printf ("%-11s %s\n", c
->name
, c
->descr
);
7082 printf ("\n-soundhw all will enable all of the above\n");
7083 exit (*optarg
!= '?');
7091 if (!strcmp (optarg
, "all")) {
7092 for (c
= soundhw
; c
->name
; ++c
) {
7100 e
= strchr (p
, ',');
7101 l
= !e
? strlen (p
) : (size_t) (e
- p
);
7103 for (c
= soundhw
; c
->name
; ++c
) {
7104 if (!strncmp (c
->name
, p
, l
)) {
7113 "Unknown sound card name (too big to show)\n");
7116 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
7121 p
+= l
+ (e
!= NULL
);
7125 goto show_valid_cards
;
7131 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
7133 exit(STATUS_CONTROL_C_EXIT
);
7138 #define MAX_NET_CLIENTS 32
7140 int main(int argc
, char **argv
)
7142 #ifdef CONFIG_GDBSTUB
7144 const char *gdbstub_port
;
7146 int i
, cdrom_index
, pflash_index
;
7147 int snapshot
, linux_boot
;
7148 const char *initrd_filename
;
7149 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
7150 const char *pflash_filename
[MAX_PFLASH
];
7151 const char *sd_filename
;
7152 const char *mtd_filename
;
7153 const char *kernel_filename
, *kernel_cmdline
;
7154 DisplayState
*ds
= &display_state
;
7155 int cyls
, heads
, secs
, translation
;
7156 char net_clients
[MAX_NET_CLIENTS
][256];
7159 const char *r
, *optarg
;
7160 CharDriverState
*monitor_hd
;
7161 char monitor_device
[128];
7162 char serial_devices
[MAX_SERIAL_PORTS
][128];
7163 int serial_device_index
;
7164 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
7165 int parallel_device_index
;
7166 const char *loadvm
= NULL
;
7167 QEMUMachine
*machine
;
7168 const char *cpu_model
;
7169 char usb_devices
[MAX_USB_CMDLINE
][128];
7170 int usb_devices_index
;
7172 const char *pid_file
= NULL
;
7175 LIST_INIT (&vm_change_state_head
);
7178 struct sigaction act
;
7179 sigfillset(&act
.sa_mask
);
7181 act
.sa_handler
= SIG_IGN
;
7182 sigaction(SIGPIPE
, &act
, NULL
);
7185 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
7186 /* Note: cpu_interrupt() is currently not SMP safe, so we force
7187 QEMU to run on a single CPU */
7192 h
= GetCurrentProcess();
7193 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
7194 for(i
= 0; i
< 32; i
++) {
7195 if (mask
& (1 << i
))
7200 SetProcessAffinityMask(h
, mask
);
7206 register_machines();
7207 machine
= first_machine
;
7209 initrd_filename
= NULL
;
7210 for(i
= 0; i
< MAX_FD
; i
++)
7211 fd_filename
[i
] = NULL
;
7212 for(i
= 0; i
< MAX_DISKS
; i
++)
7213 hd_filename
[i
] = NULL
;
7214 for(i
= 0; i
< MAX_PFLASH
; i
++)
7215 pflash_filename
[i
] = NULL
;
7218 mtd_filename
= NULL
;
7219 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
7220 vga_ram_size
= VGA_RAM_SIZE
;
7221 #ifdef CONFIG_GDBSTUB
7223 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
7227 kernel_filename
= NULL
;
7228 kernel_cmdline
= "";
7234 cyls
= heads
= secs
= 0;
7235 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7236 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
7238 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
7239 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
7240 serial_devices
[i
][0] = '\0';
7241 serial_device_index
= 0;
7243 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
7244 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
7245 parallel_devices
[i
][0] = '\0';
7246 parallel_device_index
= 0;
7248 usb_devices_index
= 0;
7253 /* default mac address of the first network interface */
7261 hd_filename
[0] = argv
[optind
++];
7263 const QEMUOption
*popt
;
7266 /* Treat --foo the same as -foo. */
7269 popt
= qemu_options
;
7272 fprintf(stderr
, "%s: invalid option -- '%s'\n",
7276 if (!strcmp(popt
->name
, r
+ 1))
7280 if (popt
->flags
& HAS_ARG
) {
7281 if (optind
>= argc
) {
7282 fprintf(stderr
, "%s: option '%s' requires an argument\n",
7286 optarg
= argv
[optind
++];
7291 switch(popt
->index
) {
7293 machine
= find_machine(optarg
);
7296 printf("Supported machines are:\n");
7297 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
7298 printf("%-10s %s%s\n",
7300 m
== first_machine
? " (default)" : "");
7302 exit(*optarg
!= '?');
7305 case QEMU_OPTION_cpu
:
7306 /* hw initialization will check this */
7307 if (*optarg
== '?') {
7308 #if defined(TARGET_PPC)
7309 ppc_cpu_list(stdout
, &fprintf
);
7310 #elif defined(TARGET_ARM)
7312 #elif defined(TARGET_MIPS)
7313 mips_cpu_list(stdout
, &fprintf
);
7314 #elif defined(TARGET_SPARC)
7315 sparc_cpu_list(stdout
, &fprintf
);
7322 case QEMU_OPTION_initrd
:
7323 initrd_filename
= optarg
;
7325 case QEMU_OPTION_hda
:
7326 case QEMU_OPTION_hdb
:
7327 case QEMU_OPTION_hdc
:
7328 case QEMU_OPTION_hdd
:
7331 hd_index
= popt
->index
- QEMU_OPTION_hda
;
7332 hd_filename
[hd_index
] = optarg
;
7333 if (hd_index
== cdrom_index
)
7337 case QEMU_OPTION_mtdblock
:
7338 mtd_filename
= optarg
;
7340 case QEMU_OPTION_sd
:
7341 sd_filename
= optarg
;
7343 case QEMU_OPTION_pflash
:
7344 if (pflash_index
>= MAX_PFLASH
) {
7345 fprintf(stderr
, "qemu: too many parallel flash images\n");
7348 pflash_filename
[pflash_index
++] = optarg
;
7350 case QEMU_OPTION_snapshot
:
7353 case QEMU_OPTION_hdachs
:
7357 cyls
= strtol(p
, (char **)&p
, 0);
7358 if (cyls
< 1 || cyls
> 16383)
7363 heads
= strtol(p
, (char **)&p
, 0);
7364 if (heads
< 1 || heads
> 16)
7369 secs
= strtol(p
, (char **)&p
, 0);
7370 if (secs
< 1 || secs
> 63)
7374 if (!strcmp(p
, "none"))
7375 translation
= BIOS_ATA_TRANSLATION_NONE
;
7376 else if (!strcmp(p
, "lba"))
7377 translation
= BIOS_ATA_TRANSLATION_LBA
;
7378 else if (!strcmp(p
, "auto"))
7379 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7382 } else if (*p
!= '\0') {
7384 fprintf(stderr
, "qemu: invalid physical CHS format\n");
7389 case QEMU_OPTION_nographic
:
7390 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
7391 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "null");
7392 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
7395 case QEMU_OPTION_portrait
:
7398 case QEMU_OPTION_kernel
:
7399 kernel_filename
= optarg
;
7401 case QEMU_OPTION_append
:
7402 kernel_cmdline
= optarg
;
7404 case QEMU_OPTION_cdrom
:
7405 if (cdrom_index
>= 0) {
7406 hd_filename
[cdrom_index
] = optarg
;
7409 case QEMU_OPTION_boot
:
7410 boot_device
= optarg
[0];
7411 if (boot_device
!= 'a' &&
7412 #if defined(TARGET_SPARC) || defined(TARGET_I386)
7414 boot_device
!= 'n' &&
7416 boot_device
!= 'c' && boot_device
!= 'd') {
7417 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
7421 case QEMU_OPTION_fda
:
7422 fd_filename
[0] = optarg
;
7424 case QEMU_OPTION_fdb
:
7425 fd_filename
[1] = optarg
;
7428 case QEMU_OPTION_no_fd_bootchk
:
7432 case QEMU_OPTION_no_code_copy
:
7433 code_copy_enabled
= 0;
7435 case QEMU_OPTION_net
:
7436 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
7437 fprintf(stderr
, "qemu: too many network clients\n");
7440 pstrcpy(net_clients
[nb_net_clients
],
7441 sizeof(net_clients
[0]),
7446 case QEMU_OPTION_tftp
:
7447 tftp_prefix
= optarg
;
7449 case QEMU_OPTION_bootp
:
7450 bootp_filename
= optarg
;
7453 case QEMU_OPTION_smb
:
7454 net_slirp_smb(optarg
);
7457 case QEMU_OPTION_redir
:
7458 net_slirp_redir(optarg
);
7462 case QEMU_OPTION_audio_help
:
7466 case QEMU_OPTION_soundhw
:
7467 select_soundhw (optarg
);
7474 ram_size
= atoi(optarg
) * 1024 * 1024;
7477 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
7478 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
7479 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
7488 mask
= cpu_str_to_log_mask(optarg
);
7490 printf("Log items (comma separated):\n");
7491 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
7492 printf("%-10s %s\n", item
->name
, item
->help
);
7499 #ifdef CONFIG_GDBSTUB
7504 gdbstub_port
= optarg
;
7514 keyboard_layout
= optarg
;
7516 case QEMU_OPTION_localtime
:
7519 case QEMU_OPTION_cirrusvga
:
7520 cirrus_vga_enabled
= 1;
7523 case QEMU_OPTION_vmsvga
:
7524 cirrus_vga_enabled
= 0;
7527 case QEMU_OPTION_std_vga
:
7528 cirrus_vga_enabled
= 0;
7536 w
= strtol(p
, (char **)&p
, 10);
7539 fprintf(stderr
, "qemu: invalid resolution or depth\n");
7545 h
= strtol(p
, (char **)&p
, 10);
7550 depth
= strtol(p
, (char **)&p
, 10);
7551 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
7552 depth
!= 24 && depth
!= 32)
7554 } else if (*p
== '\0') {
7555 depth
= graphic_depth
;
7562 graphic_depth
= depth
;
7565 case QEMU_OPTION_echr
:
7568 term_escape_char
= strtol(optarg
, &r
, 0);
7570 printf("Bad argument to echr\n");
7573 case QEMU_OPTION_monitor
:
7574 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
7576 case QEMU_OPTION_serial
:
7577 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
7578 fprintf(stderr
, "qemu: too many serial ports\n");
7581 pstrcpy(serial_devices
[serial_device_index
],
7582 sizeof(serial_devices
[0]), optarg
);
7583 serial_device_index
++;
7585 case QEMU_OPTION_parallel
:
7586 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
7587 fprintf(stderr
, "qemu: too many parallel ports\n");
7590 pstrcpy(parallel_devices
[parallel_device_index
],
7591 sizeof(parallel_devices
[0]), optarg
);
7592 parallel_device_index
++;
7594 case QEMU_OPTION_loadvm
:
7597 case QEMU_OPTION_full_screen
:
7601 case QEMU_OPTION_no_frame
:
7604 case QEMU_OPTION_alt_grab
:
7607 case QEMU_OPTION_no_quit
:
7611 case QEMU_OPTION_pidfile
:
7615 case QEMU_OPTION_win2k_hack
:
7616 win2k_install_hack
= 1;
7620 case QEMU_OPTION_no_kqemu
:
7623 case QEMU_OPTION_kernel_kqemu
:
7627 case QEMU_OPTION_usb
:
7630 case QEMU_OPTION_usbdevice
:
7632 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
7633 fprintf(stderr
, "Too many USB devices\n");
7636 pstrcpy(usb_devices
[usb_devices_index
],
7637 sizeof(usb_devices
[usb_devices_index
]),
7639 usb_devices_index
++;
7641 case QEMU_OPTION_smp
:
7642 smp_cpus
= atoi(optarg
);
7643 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
7644 fprintf(stderr
, "Invalid number of CPUs\n");
7648 case QEMU_OPTION_vnc
:
7649 vnc_display
= optarg
;
7651 case QEMU_OPTION_no_acpi
:
7654 case QEMU_OPTION_no_reboot
:
7657 case QEMU_OPTION_show_cursor
:
7660 case QEMU_OPTION_daemonize
:
7663 case QEMU_OPTION_option_rom
:
7664 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
7665 fprintf(stderr
, "Too many option ROMs\n");
7668 option_rom
[nb_option_roms
] = optarg
;
7671 case QEMU_OPTION_semihosting
:
7672 semihosting_enabled
= 1;
7674 case QEMU_OPTION_name
:
7678 case QEMU_OPTION_prom_env
:
7679 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
7680 fprintf(stderr
, "Too many prom variables\n");
7683 prom_envs
[nb_prom_envs
] = optarg
;
7688 case QEMU_OPTION_old_param
:
7696 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
7697 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
7704 if (pipe(fds
) == -1)
7715 len
= read(fds
[0], &status
, 1);
7716 if (len
== -1 && (errno
== EINTR
))
7721 else if (status
== 1) {
7722 fprintf(stderr
, "Could not acquire pidfile\n");
7740 signal(SIGTSTP
, SIG_IGN
);
7741 signal(SIGTTOU
, SIG_IGN
);
7742 signal(SIGTTIN
, SIG_IGN
);
7746 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
7749 write(fds
[1], &status
, 1);
7751 fprintf(stderr
, "Could not acquire pid file\n");
7759 linux_boot
= (kernel_filename
!= NULL
);
7762 boot_device
!= 'n' &&
7763 hd_filename
[0] == '\0' &&
7764 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
7765 fd_filename
[0] == '\0')
7768 /* boot to floppy or the default cd if no hard disk defined yet */
7769 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
7770 if (fd_filename
[0] != '\0')
7776 setvbuf(stdout
, NULL
, _IOLBF
, 0);
7786 /* init network clients */
7787 if (nb_net_clients
== 0) {
7788 /* if no clients, we use a default config */
7789 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
7791 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
7796 for(i
= 0;i
< nb_net_clients
; i
++) {
7797 if (net_client_init(net_clients
[i
]) < 0)
7800 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
7801 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
7803 if (vlan
->nb_guest_devs
== 0) {
7804 fprintf(stderr
, "Invalid vlan (%d) with no nics\n", vlan
->id
);
7807 if (vlan
->nb_host_devs
== 0)
7809 "Warning: vlan %d is not connected to host network\n",
7814 if (boot_device
== 'n') {
7815 for (i
= 0; i
< nb_nics
; i
++) {
7816 const char *model
= nd_table
[i
].model
;
7820 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
7821 if (get_image_size(buf
) > 0) {
7822 option_rom
[nb_option_roms
] = strdup(buf
);
7828 fprintf(stderr
, "No valid PXE rom found for network device\n");
7831 boot_device
= 'c'; /* to prevent confusion by the BIOS */
7835 /* init the memory */
7836 phys_ram_size
= ram_size
+ vga_ram_size
+ MAX_BIOS_SIZE
;
7838 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
7839 if (!phys_ram_base
) {
7840 fprintf(stderr
, "Could not allocate physical memory\n");
7844 /* we always create the cdrom drive, even if no disk is there */
7846 if (cdrom_index
>= 0) {
7847 bs_table
[cdrom_index
] = bdrv_new("cdrom");
7848 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
7851 /* open the virtual block devices */
7852 for(i
= 0; i
< MAX_DISKS
; i
++) {
7853 if (hd_filename
[i
]) {
7856 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
7857 bs_table
[i
] = bdrv_new(buf
);
7859 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7860 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
7864 if (i
== 0 && cyls
!= 0) {
7865 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
7866 bdrv_set_translation_hint(bs_table
[i
], translation
);
7871 /* we always create at least one floppy disk */
7872 fd_table
[0] = bdrv_new("fda");
7873 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
7875 for(i
= 0; i
< MAX_FD
; i
++) {
7876 if (fd_filename
[i
]) {
7879 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
7880 fd_table
[i
] = bdrv_new(buf
);
7881 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
7883 if (fd_filename
[i
][0] != '\0') {
7884 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
7885 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7886 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
7894 /* Open the virtual parallel flash block devices */
7895 for(i
= 0; i
< MAX_PFLASH
; i
++) {
7896 if (pflash_filename
[i
]) {
7897 if (!pflash_table
[i
]) {
7899 snprintf(buf
, sizeof(buf
), "fl%c", i
+ 'a');
7900 pflash_table
[i
] = bdrv_new(buf
);
7902 if (bdrv_open(pflash_table
[i
], pflash_filename
[i
],
7903 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7904 fprintf(stderr
, "qemu: could not open flash image '%s'\n",
7905 pflash_filename
[i
]);
7911 sd_bdrv
= bdrv_new ("sd");
7912 /* FIXME: This isn't really a floppy, but it's a reasonable
7914 bdrv_set_type_hint(sd_bdrv
, BDRV_TYPE_FLOPPY
);
7916 if (bdrv_open(sd_bdrv
, sd_filename
,
7917 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7918 fprintf(stderr
, "qemu: could not open SD card image %s\n",
7921 qemu_key_check(sd_bdrv
, sd_filename
);
7925 mtd_bdrv
= bdrv_new ("mtd");
7926 if (bdrv_open(mtd_bdrv
, mtd_filename
,
7927 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0 ||
7928 qemu_key_check(mtd_bdrv
, mtd_filename
)) {
7929 fprintf(stderr
, "qemu: could not open Flash image %s\n",
7931 bdrv_delete(mtd_bdrv
);
7936 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
7937 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
7942 memset(&display_state
, 0, sizeof(display_state
));
7944 /* nearly nothing to do */
7945 dumb_display_init(ds
);
7946 } else if (vnc_display
!= NULL
) {
7947 vnc_display_init(ds
, vnc_display
);
7949 #if defined(CONFIG_SDL)
7950 sdl_display_init(ds
, full_screen
, no_frame
);
7951 #elif defined(CONFIG_COCOA)
7952 cocoa_display_init(ds
, full_screen
);
7956 /* Maintain compatibility with multiple stdio monitors */
7957 if (!strcmp(monitor_device
,"stdio")) {
7958 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7959 if (!strcmp(serial_devices
[i
],"mon:stdio")) {
7960 monitor_device
[0] = '\0';
7962 } else if (!strcmp(serial_devices
[i
],"stdio")) {
7963 monitor_device
[0] = '\0';
7964 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "mon:stdio");
7969 if (monitor_device
[0] != '\0') {
7970 monitor_hd
= qemu_chr_open(monitor_device
);
7972 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
7975 monitor_init(monitor_hd
, !nographic
);
7978 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7979 const char *devname
= serial_devices
[i
];
7980 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7981 serial_hds
[i
] = qemu_chr_open(devname
);
7982 if (!serial_hds
[i
]) {
7983 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
7987 if (strstart(devname
, "vc", 0))
7988 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
7992 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
7993 const char *devname
= parallel_devices
[i
];
7994 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7995 parallel_hds
[i
] = qemu_chr_open(devname
);
7996 if (!parallel_hds
[i
]) {
7997 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
8001 if (strstart(devname
, "vc", 0))
8002 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
8006 machine
->init(ram_size
, vga_ram_size
, boot_device
,
8007 ds
, fd_filename
, snapshot
,
8008 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
8010 /* init USB devices */
8012 for(i
= 0; i
< usb_devices_index
; i
++) {
8013 if (usb_device_add(usb_devices
[i
]) < 0) {
8014 fprintf(stderr
, "Warning: could not add USB device %s\n",
8020 if (display_state
.dpy_refresh
) {
8021 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
8022 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
8025 #ifdef CONFIG_GDBSTUB
8027 /* XXX: use standard host:port notation and modify options
8029 if (gdbserver_start(gdbstub_port
) < 0) {
8030 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
8041 /* XXX: simplify init */
8054 len
= write(fds
[1], &status
, 1);
8055 if (len
== -1 && (errno
== EINTR
))
8061 TFR(fd
= open("/dev/null", O_RDWR
));