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>
53 #include <linux/if_tun.h>
56 #include <linux/rtc.h>
57 #include <linux/ppdev.h>
58 #include <linux/parport.h>
61 #include <sys/ethernet.h>
62 #include <sys/sockio.h>
63 #include <arpa/inet.h>
64 #include <netinet/arp.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/ip.h>
68 #include <netinet/ip_icmp.h> // must come after ip.h
69 #include <netinet/udp.h>
70 #include <netinet/tcp.h>
78 #if defined(CONFIG_SLIRP)
84 #include <sys/timeb.h>
86 #define getopt_long_only getopt_long
87 #define memalign(align, size) malloc(size)
90 #include "qemu_socket.h"
96 #endif /* CONFIG_SDL */
100 #define main qemu_main
101 #endif /* CONFIG_COCOA */
105 #include "exec-all.h"
107 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
109 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
111 #define SMBD_COMMAND "/usr/sbin/smbd"
114 //#define DEBUG_UNUSED_IOPORT
115 //#define DEBUG_IOPORT
117 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
120 #define DEFAULT_RAM_SIZE 144
122 #define DEFAULT_RAM_SIZE 128
125 #define GUI_REFRESH_INTERVAL 30
127 /* Max number of USB devices that can be specified on the commandline. */
128 #define MAX_USB_CMDLINE 8
130 /* XXX: use a two level table to limit memory usage */
131 #define MAX_IOPORTS 65536
133 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
134 char phys_ram_file
[1024];
135 void *ioport_opaque
[MAX_IOPORTS
];
136 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
137 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
138 /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available
139 to store the VM snapshots */
140 BlockDriverState
*bs_table
[MAX_DISKS
+ 1], *fd_table
[MAX_FD
];
141 BlockDriverState
*pflash_table
[MAX_PFLASH
];
142 BlockDriverState
*sd_bdrv
;
143 BlockDriverState
*mtd_bdrv
;
144 /* point to the block driver where the snapshots are managed */
145 BlockDriverState
*bs_snapshots
;
147 static DisplayState display_state
;
149 const char* keyboard_layout
= NULL
;
150 int64_t ticks_per_sec
;
151 int boot_device
= 'c';
153 int pit_min_timer_count
= 0;
155 NICInfo nd_table
[MAX_NICS
];
156 QEMUTimer
*gui_timer
;
159 int cirrus_vga_enabled
= 1;
160 int vmsvga_enabled
= 0;
162 int graphic_width
= 1024;
163 int graphic_height
= 768;
164 int graphic_depth
= 8;
166 int graphic_width
= 800;
167 int graphic_height
= 600;
168 int graphic_depth
= 15;
173 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
174 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
176 int win2k_install_hack
= 0;
179 static VLANState
*first_vlan
;
181 const char *vnc_display
;
182 #if defined(TARGET_SPARC)
184 #elif defined(TARGET_I386)
189 int acpi_enabled
= 1;
193 int graphic_rotate
= 0;
195 const char *option_rom
[MAX_OPTION_ROMS
];
197 int semihosting_enabled
= 0;
199 const char *qemu_name
;
201 unsigned int nb_prom_envs
= 0;
202 const char *prom_envs
[MAX_PROM_ENVS
];
205 /***********************************************************/
206 /* x86 ISA bus support */
208 target_phys_addr_t isa_mem_base
= 0;
211 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
213 #ifdef DEBUG_UNUSED_IOPORT
214 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
219 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
221 #ifdef DEBUG_UNUSED_IOPORT
222 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
226 /* default is to make two byte accesses */
227 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
230 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
231 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
232 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
236 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
238 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
239 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
240 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
243 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
245 #ifdef DEBUG_UNUSED_IOPORT
246 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
251 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
253 #ifdef DEBUG_UNUSED_IOPORT
254 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
258 void init_ioports(void)
262 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
263 ioport_read_table
[0][i
] = default_ioport_readb
;
264 ioport_write_table
[0][i
] = default_ioport_writeb
;
265 ioport_read_table
[1][i
] = default_ioport_readw
;
266 ioport_write_table
[1][i
] = default_ioport_writew
;
267 ioport_read_table
[2][i
] = default_ioport_readl
;
268 ioport_write_table
[2][i
] = default_ioport_writel
;
272 /* size is the word size in byte */
273 int register_ioport_read(int start
, int length
, int size
,
274 IOPortReadFunc
*func
, void *opaque
)
280 } else if (size
== 2) {
282 } else if (size
== 4) {
285 hw_error("register_ioport_read: invalid size");
288 for(i
= start
; i
< start
+ length
; i
+= size
) {
289 ioport_read_table
[bsize
][i
] = func
;
290 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
291 hw_error("register_ioport_read: invalid opaque");
292 ioport_opaque
[i
] = opaque
;
297 /* size is the word size in byte */
298 int register_ioport_write(int start
, int length
, int size
,
299 IOPortWriteFunc
*func
, void *opaque
)
305 } else if (size
== 2) {
307 } else if (size
== 4) {
310 hw_error("register_ioport_write: invalid size");
313 for(i
= start
; i
< start
+ length
; i
+= size
) {
314 ioport_write_table
[bsize
][i
] = func
;
315 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
316 hw_error("register_ioport_write: invalid opaque");
317 ioport_opaque
[i
] = opaque
;
322 void isa_unassign_ioport(int start
, int length
)
326 for(i
= start
; i
< start
+ length
; i
++) {
327 ioport_read_table
[0][i
] = default_ioport_readb
;
328 ioport_read_table
[1][i
] = default_ioport_readw
;
329 ioport_read_table
[2][i
] = default_ioport_readl
;
331 ioport_write_table
[0][i
] = default_ioport_writeb
;
332 ioport_write_table
[1][i
] = default_ioport_writew
;
333 ioport_write_table
[2][i
] = default_ioport_writel
;
337 /***********************************************************/
339 void cpu_outb(CPUState
*env
, int addr
, int val
)
342 if (loglevel
& CPU_LOG_IOPORT
)
343 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
345 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
348 env
->last_io_time
= cpu_get_time_fast();
352 void cpu_outw(CPUState
*env
, int addr
, int val
)
355 if (loglevel
& CPU_LOG_IOPORT
)
356 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
358 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
361 env
->last_io_time
= cpu_get_time_fast();
365 void cpu_outl(CPUState
*env
, int addr
, int val
)
368 if (loglevel
& CPU_LOG_IOPORT
)
369 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
371 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
374 env
->last_io_time
= cpu_get_time_fast();
378 int cpu_inb(CPUState
*env
, int addr
)
381 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
383 if (loglevel
& CPU_LOG_IOPORT
)
384 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
388 env
->last_io_time
= cpu_get_time_fast();
393 int cpu_inw(CPUState
*env
, int addr
)
396 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
398 if (loglevel
& CPU_LOG_IOPORT
)
399 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
403 env
->last_io_time
= cpu_get_time_fast();
408 int cpu_inl(CPUState
*env
, int addr
)
411 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
413 if (loglevel
& CPU_LOG_IOPORT
)
414 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
418 env
->last_io_time
= cpu_get_time_fast();
423 /***********************************************************/
424 void hw_error(const char *fmt
, ...)
430 fprintf(stderr
, "qemu: hardware error: ");
431 vfprintf(stderr
, fmt
, ap
);
432 fprintf(stderr
, "\n");
433 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
434 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
436 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
438 cpu_dump_state(env
, stderr
, fprintf
, 0);
445 /***********************************************************/
448 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
449 static void *qemu_put_kbd_event_opaque
;
450 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
451 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
453 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
455 qemu_put_kbd_event_opaque
= opaque
;
456 qemu_put_kbd_event
= func
;
459 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
460 void *opaque
, int absolute
,
463 QEMUPutMouseEntry
*s
, *cursor
;
465 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
469 s
->qemu_put_mouse_event
= func
;
470 s
->qemu_put_mouse_event_opaque
= opaque
;
471 s
->qemu_put_mouse_event_absolute
= absolute
;
472 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
475 if (!qemu_put_mouse_event_head
) {
476 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
480 cursor
= qemu_put_mouse_event_head
;
481 while (cursor
->next
!= NULL
)
482 cursor
= cursor
->next
;
485 qemu_put_mouse_event_current
= s
;
490 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
492 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
494 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
497 cursor
= qemu_put_mouse_event_head
;
498 while (cursor
!= NULL
&& cursor
!= entry
) {
500 cursor
= cursor
->next
;
503 if (cursor
== NULL
) // does not exist or list empty
505 else if (prev
== NULL
) { // entry is head
506 qemu_put_mouse_event_head
= cursor
->next
;
507 if (qemu_put_mouse_event_current
== entry
)
508 qemu_put_mouse_event_current
= cursor
->next
;
509 qemu_free(entry
->qemu_put_mouse_event_name
);
514 prev
->next
= entry
->next
;
516 if (qemu_put_mouse_event_current
== entry
)
517 qemu_put_mouse_event_current
= prev
;
519 qemu_free(entry
->qemu_put_mouse_event_name
);
523 void kbd_put_keycode(int keycode
)
525 if (qemu_put_kbd_event
) {
526 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
530 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
532 QEMUPutMouseEvent
*mouse_event
;
533 void *mouse_event_opaque
;
536 if (!qemu_put_mouse_event_current
) {
541 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
543 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
546 if (graphic_rotate
) {
547 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
550 width
= graphic_width
;
551 mouse_event(mouse_event_opaque
,
552 width
- dy
, dx
, dz
, buttons_state
);
554 mouse_event(mouse_event_opaque
,
555 dx
, dy
, dz
, buttons_state
);
559 int kbd_mouse_is_absolute(void)
561 if (!qemu_put_mouse_event_current
)
564 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
567 void do_info_mice(void)
569 QEMUPutMouseEntry
*cursor
;
572 if (!qemu_put_mouse_event_head
) {
573 term_printf("No mouse devices connected\n");
577 term_printf("Mouse devices available:\n");
578 cursor
= qemu_put_mouse_event_head
;
579 while (cursor
!= NULL
) {
580 term_printf("%c Mouse #%d: %s\n",
581 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
582 index
, cursor
->qemu_put_mouse_event_name
);
584 cursor
= cursor
->next
;
588 void do_mouse_set(int index
)
590 QEMUPutMouseEntry
*cursor
;
593 if (!qemu_put_mouse_event_head
) {
594 term_printf("No mouse devices connected\n");
598 cursor
= qemu_put_mouse_event_head
;
599 while (cursor
!= NULL
&& index
!= i
) {
601 cursor
= cursor
->next
;
605 qemu_put_mouse_event_current
= cursor
;
607 term_printf("Mouse at given index not found\n");
610 /* compute with 96 bit intermediate result: (a*b)/c */
611 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
616 #ifdef WORDS_BIGENDIAN
626 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
627 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
630 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
634 /***********************************************************/
635 /* real time host monotonic timer */
637 #define QEMU_TIMER_BASE 1000000000LL
641 static int64_t clock_freq
;
643 static void init_get_clock(void)
647 ret
= QueryPerformanceFrequency(&freq
);
649 fprintf(stderr
, "Could not calibrate ticks\n");
652 clock_freq
= freq
.QuadPart
;
655 static int64_t get_clock(void)
658 QueryPerformanceCounter(&ti
);
659 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
664 static int use_rt_clock
;
666 static void init_get_clock(void)
669 #if defined(__linux__)
672 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
679 static int64_t get_clock(void)
681 #if defined(__linux__)
684 clock_gettime(CLOCK_MONOTONIC
, &ts
);
685 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
689 /* XXX: using gettimeofday leads to problems if the date
690 changes, so it should be avoided. */
692 gettimeofday(&tv
, NULL
);
693 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
699 /***********************************************************/
700 /* guest cycle counter */
702 static int64_t cpu_ticks_prev
;
703 static int64_t cpu_ticks_offset
;
704 static int64_t cpu_clock_offset
;
705 static int cpu_ticks_enabled
;
707 /* return the host CPU cycle counter and handle stop/restart */
708 int64_t cpu_get_ticks(void)
710 if (!cpu_ticks_enabled
) {
711 return cpu_ticks_offset
;
714 ticks
= cpu_get_real_ticks();
715 if (cpu_ticks_prev
> ticks
) {
716 /* Note: non increasing ticks may happen if the host uses
718 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
720 cpu_ticks_prev
= ticks
;
721 return ticks
+ cpu_ticks_offset
;
725 /* return the host CPU monotonic timer and handle stop/restart */
726 static int64_t cpu_get_clock(void)
729 if (!cpu_ticks_enabled
) {
730 return cpu_clock_offset
;
733 return ti
+ cpu_clock_offset
;
737 /* enable cpu_get_ticks() */
738 void cpu_enable_ticks(void)
740 if (!cpu_ticks_enabled
) {
741 cpu_ticks_offset
-= cpu_get_real_ticks();
742 cpu_clock_offset
-= get_clock();
743 cpu_ticks_enabled
= 1;
747 /* disable cpu_get_ticks() : the clock is stopped. You must not call
748 cpu_get_ticks() after that. */
749 void cpu_disable_ticks(void)
751 if (cpu_ticks_enabled
) {
752 cpu_ticks_offset
= cpu_get_ticks();
753 cpu_clock_offset
= cpu_get_clock();
754 cpu_ticks_enabled
= 0;
758 /***********************************************************/
761 #define QEMU_TIMER_REALTIME 0
762 #define QEMU_TIMER_VIRTUAL 1
766 /* XXX: add frequency */
774 struct QEMUTimer
*next
;
780 static QEMUTimer
*active_timers
[2];
782 static MMRESULT timerID
;
783 static HANDLE host_alarm
= NULL
;
784 static unsigned int period
= 1;
786 /* frequency of the times() clock tick */
787 static int timer_freq
;
790 QEMUClock
*qemu_new_clock(int type
)
793 clock
= qemu_mallocz(sizeof(QEMUClock
));
800 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
804 ts
= qemu_mallocz(sizeof(QEMUTimer
));
811 void qemu_free_timer(QEMUTimer
*ts
)
816 /* stop a timer, but do not dealloc it */
817 void qemu_del_timer(QEMUTimer
*ts
)
821 /* NOTE: this code must be signal safe because
822 qemu_timer_expired() can be called from a signal. */
823 pt
= &active_timers
[ts
->clock
->type
];
836 /* modify the current timer so that it will be fired when current_time
837 >= expire_time. The corresponding callback will be called. */
838 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
844 /* add the timer in the sorted list */
845 /* NOTE: this code must be signal safe because
846 qemu_timer_expired() can be called from a signal. */
847 pt
= &active_timers
[ts
->clock
->type
];
852 if (t
->expire_time
> expire_time
)
856 ts
->expire_time
= expire_time
;
861 int qemu_timer_pending(QEMUTimer
*ts
)
864 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
871 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
875 return (timer_head
->expire_time
<= current_time
);
878 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
884 if (!ts
|| ts
->expire_time
> current_time
)
886 /* remove timer from the list before calling the callback */
887 *ptimer_head
= ts
->next
;
890 /* run the callback (the timer list can be modified) */
895 int64_t qemu_get_clock(QEMUClock
*clock
)
897 switch(clock
->type
) {
898 case QEMU_TIMER_REALTIME
:
899 return get_clock() / 1000000;
901 case QEMU_TIMER_VIRTUAL
:
902 return cpu_get_clock();
906 static void init_timers(void)
909 ticks_per_sec
= QEMU_TIMER_BASE
;
910 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
911 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
915 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
917 uint64_t expire_time
;
919 if (qemu_timer_pending(ts
)) {
920 expire_time
= ts
->expire_time
;
924 qemu_put_be64(f
, expire_time
);
927 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
929 uint64_t expire_time
;
931 expire_time
= qemu_get_be64(f
);
932 if (expire_time
!= -1) {
933 qemu_mod_timer(ts
, expire_time
);
939 static void timer_save(QEMUFile
*f
, void *opaque
)
941 if (cpu_ticks_enabled
) {
942 hw_error("cannot save state if virtual timers are running");
944 qemu_put_be64s(f
, &cpu_ticks_offset
);
945 qemu_put_be64s(f
, &ticks_per_sec
);
946 qemu_put_be64s(f
, &cpu_clock_offset
);
949 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
951 if (version_id
!= 1 && version_id
!= 2)
953 if (cpu_ticks_enabled
) {
956 qemu_get_be64s(f
, &cpu_ticks_offset
);
957 qemu_get_be64s(f
, &ticks_per_sec
);
958 if (version_id
== 2) {
959 qemu_get_be64s(f
, &cpu_clock_offset
);
965 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
966 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
968 static void host_alarm_handler(int host_signum
)
972 #define DISP_FREQ 1000
974 static int64_t delta_min
= INT64_MAX
;
975 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
977 ti
= qemu_get_clock(vm_clock
);
978 if (last_clock
!= 0) {
979 delta
= ti
- last_clock
;
980 if (delta
< delta_min
)
982 if (delta
> delta_max
)
985 if (++count
== DISP_FREQ
) {
986 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
987 muldiv64(delta_min
, 1000000, ticks_per_sec
),
988 muldiv64(delta_max
, 1000000, ticks_per_sec
),
989 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
990 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
992 delta_min
= INT64_MAX
;
1000 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1001 qemu_get_clock(vm_clock
)) ||
1002 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1003 qemu_get_clock(rt_clock
))) {
1005 SetEvent(host_alarm
);
1007 CPUState
*env
= cpu_single_env
;
1009 /* stop the currently executing cpu because a timer occured */
1010 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1012 if (env
->kqemu_enabled
) {
1013 kqemu_cpu_interrupt(env
);
1022 #if defined(__linux__)
1024 #define RTC_FREQ 1024
1028 static int start_rtc_timer(void)
1030 rtc_fd
= open("/dev/rtc", O_RDONLY
);
1033 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1034 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1035 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1036 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1039 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1044 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
1050 static int start_rtc_timer(void)
1055 #endif /* !defined(__linux__) */
1057 #endif /* !defined(_WIN32) */
1059 static void init_timer_alarm(void)
1066 ZeroMemory(&tc
, sizeof(TIMECAPS
));
1067 timeGetDevCaps(&tc
, sizeof(TIMECAPS
));
1068 if (period
< tc
.wPeriodMin
)
1069 period
= tc
.wPeriodMin
;
1070 timeBeginPeriod(period
);
1071 timerID
= timeSetEvent(1, // interval (ms)
1072 period
, // resolution
1073 host_alarm_handler
, // function
1074 (DWORD
)&count
, // user parameter
1075 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
1077 perror("failed timer alarm");
1080 host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1082 perror("failed CreateEvent");
1085 qemu_add_wait_object(host_alarm
, NULL
, NULL
);
1087 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
1090 struct sigaction act
;
1091 struct itimerval itv
;
1093 /* get times() syscall frequency */
1094 timer_freq
= sysconf(_SC_CLK_TCK
);
1097 sigfillset(&act
.sa_mask
);
1099 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1100 act
.sa_flags
|= SA_ONSTACK
;
1102 act
.sa_handler
= host_alarm_handler
;
1103 sigaction(SIGALRM
, &act
, NULL
);
1105 itv
.it_interval
.tv_sec
= 0;
1106 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
1107 itv
.it_value
.tv_sec
= 0;
1108 itv
.it_value
.tv_usec
= 10 * 1000;
1109 setitimer(ITIMER_REAL
, &itv
, NULL
);
1110 /* we probe the tick duration of the kernel to inform the user if
1111 the emulated kernel requested a too high timer frequency */
1112 getitimer(ITIMER_REAL
, &itv
);
1114 #if defined(__linux__)
1115 /* XXX: force /dev/rtc usage because even 2.6 kernels may not
1116 have timers with 1 ms resolution. The correct solution will
1117 be to use the POSIX real time timers available in recent
1119 if (itv
.it_interval
.tv_usec
> 1000 || 1) {
1120 /* try to use /dev/rtc to have a faster timer */
1121 if (start_rtc_timer() < 0)
1123 /* disable itimer */
1124 itv
.it_interval
.tv_sec
= 0;
1125 itv
.it_interval
.tv_usec
= 0;
1126 itv
.it_value
.tv_sec
= 0;
1127 itv
.it_value
.tv_usec
= 0;
1128 setitimer(ITIMER_REAL
, &itv
, NULL
);
1131 sigaction(SIGIO
, &act
, NULL
);
1132 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1133 fcntl(rtc_fd
, F_SETOWN
, getpid());
1135 #endif /* defined(__linux__) */
1138 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1139 PIT_FREQ
) / 1000000;
1145 void quit_timers(void)
1148 timeKillEvent(timerID
);
1149 timeEndPeriod(period
);
1151 CloseHandle(host_alarm
);
1157 /***********************************************************/
1158 /* character device */
1160 static void qemu_chr_event(CharDriverState
*s
, int event
)
1164 s
->chr_event(s
->handler_opaque
, event
);
1167 static void qemu_chr_reset_bh(void *opaque
)
1169 CharDriverState
*s
= opaque
;
1170 qemu_chr_event(s
, CHR_EVENT_RESET
);
1171 qemu_bh_delete(s
->bh
);
1175 void qemu_chr_reset(CharDriverState
*s
)
1177 if (s
->bh
== NULL
) {
1178 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
1179 qemu_bh_schedule(s
->bh
);
1183 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1185 return s
->chr_write(s
, buf
, len
);
1188 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1192 return s
->chr_ioctl(s
, cmd
, arg
);
1195 int qemu_chr_can_read(CharDriverState
*s
)
1197 if (!s
->chr_can_read
)
1199 return s
->chr_can_read(s
->handler_opaque
);
1202 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
1204 s
->chr_read(s
->handler_opaque
, buf
, len
);
1208 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1213 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1214 qemu_chr_write(s
, buf
, strlen(buf
));
1218 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1220 if (s
->chr_send_event
)
1221 s
->chr_send_event(s
, event
);
1224 void qemu_chr_add_handlers(CharDriverState
*s
,
1225 IOCanRWHandler
*fd_can_read
,
1226 IOReadHandler
*fd_read
,
1227 IOEventHandler
*fd_event
,
1230 s
->chr_can_read
= fd_can_read
;
1231 s
->chr_read
= fd_read
;
1232 s
->chr_event
= fd_event
;
1233 s
->handler_opaque
= opaque
;
1234 if (s
->chr_update_read_handler
)
1235 s
->chr_update_read_handler(s
);
1238 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1243 static CharDriverState
*qemu_chr_open_null(void)
1245 CharDriverState
*chr
;
1247 chr
= qemu_mallocz(sizeof(CharDriverState
));
1250 chr
->chr_write
= null_chr_write
;
1254 /* MUX driver for serial I/O splitting */
1255 static int term_timestamps
;
1256 static int64_t term_timestamps_start
;
1259 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
1260 IOReadHandler
*chr_read
[MAX_MUX
];
1261 IOEventHandler
*chr_event
[MAX_MUX
];
1262 void *ext_opaque
[MAX_MUX
];
1263 CharDriverState
*drv
;
1265 int term_got_escape
;
1270 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1272 MuxDriver
*d
= chr
->opaque
;
1274 if (!term_timestamps
) {
1275 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
1280 for(i
= 0; i
< len
; i
++) {
1281 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
1282 if (buf
[i
] == '\n') {
1288 if (term_timestamps_start
== -1)
1289 term_timestamps_start
= ti
;
1290 ti
-= term_timestamps_start
;
1291 secs
= ti
/ 1000000000;
1292 snprintf(buf1
, sizeof(buf1
),
1293 "[%02d:%02d:%02d.%03d] ",
1297 (int)((ti
/ 1000000) % 1000));
1298 d
->drv
->chr_write(d
->drv
, buf1
, strlen(buf1
));
1305 static char *mux_help
[] = {
1306 "% h print this help\n\r",
1307 "% x exit emulator\n\r",
1308 "% s save disk data back to file (if -snapshot)\n\r",
1309 "% t toggle console timestamps\n\r"
1310 "% b send break (magic sysrq)\n\r",
1311 "% c switch between console and monitor\n\r",
1316 static int term_escape_char
= 0x01; /* ctrl-a is used for escape */
1317 static void mux_print_help(CharDriverState
*chr
)
1320 char ebuf
[15] = "Escape-Char";
1321 char cbuf
[50] = "\n\r";
1323 if (term_escape_char
> 0 && term_escape_char
< 26) {
1324 sprintf(cbuf
,"\n\r");
1325 sprintf(ebuf
,"C-%c", term_escape_char
- 1 + 'a');
1327 sprintf(cbuf
,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char
);
1329 chr
->chr_write(chr
, cbuf
, strlen(cbuf
));
1330 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
1331 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
1332 if (mux_help
[i
][j
] == '%')
1333 chr
->chr_write(chr
, ebuf
, strlen(ebuf
));
1335 chr
->chr_write(chr
, &mux_help
[i
][j
], 1);
1340 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
1342 if (d
->term_got_escape
) {
1343 d
->term_got_escape
= 0;
1344 if (ch
== term_escape_char
)
1349 mux_print_help(chr
);
1353 char *term
= "QEMU: Terminated\n\r";
1354 chr
->chr_write(chr
,term
,strlen(term
));
1361 for (i
= 0; i
< MAX_DISKS
; i
++) {
1363 bdrv_commit(bs_table
[i
]);
1366 bdrv_commit(mtd_bdrv
);
1370 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1373 /* Switch to the next registered device */
1375 if (chr
->focus
>= d
->mux_cnt
)
1379 term_timestamps
= !term_timestamps
;
1380 term_timestamps_start
= -1;
1383 } else if (ch
== term_escape_char
) {
1384 d
->term_got_escape
= 1;
1392 static int mux_chr_can_read(void *opaque
)
1394 CharDriverState
*chr
= opaque
;
1395 MuxDriver
*d
= chr
->opaque
;
1396 if (d
->chr_can_read
[chr
->focus
])
1397 return d
->chr_can_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
]);
1401 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
1403 CharDriverState
*chr
= opaque
;
1404 MuxDriver
*d
= chr
->opaque
;
1406 for(i
= 0; i
< size
; i
++)
1407 if (mux_proc_byte(chr
, d
, buf
[i
]))
1408 d
->chr_read
[chr
->focus
](d
->ext_opaque
[chr
->focus
], &buf
[i
], 1);
1411 static void mux_chr_event(void *opaque
, int event
)
1413 CharDriverState
*chr
= opaque
;
1414 MuxDriver
*d
= chr
->opaque
;
1417 /* Send the event to all registered listeners */
1418 for (i
= 0; i
< d
->mux_cnt
; i
++)
1419 if (d
->chr_event
[i
])
1420 d
->chr_event
[i
](d
->ext_opaque
[i
], event
);
1423 static void mux_chr_update_read_handler(CharDriverState
*chr
)
1425 MuxDriver
*d
= chr
->opaque
;
1427 if (d
->mux_cnt
>= MAX_MUX
) {
1428 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
1431 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
1432 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
1433 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
1434 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
1435 /* Fix up the real driver with mux routines */
1436 if (d
->mux_cnt
== 0) {
1437 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
1438 mux_chr_event
, chr
);
1440 chr
->focus
= d
->mux_cnt
;
1444 CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
1446 CharDriverState
*chr
;
1449 chr
= qemu_mallocz(sizeof(CharDriverState
));
1452 d
= qemu_mallocz(sizeof(MuxDriver
));
1461 chr
->chr_write
= mux_chr_write
;
1462 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
1469 static void socket_cleanup(void)
1474 static int socket_init(void)
1479 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1481 err
= WSAGetLastError();
1482 fprintf(stderr
, "WSAStartup: %d\n", err
);
1485 atexit(socket_cleanup
);
1489 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1495 ret
= send(fd
, buf
, len
, 0);
1498 errno
= WSAGetLastError();
1499 if (errno
!= WSAEWOULDBLOCK
) {
1502 } else if (ret
== 0) {
1512 void socket_set_nonblock(int fd
)
1514 unsigned long opt
= 1;
1515 ioctlsocket(fd
, FIONBIO
, &opt
);
1520 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1526 ret
= write(fd
, buf
, len
);
1528 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1530 } else if (ret
== 0) {
1540 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1542 return unix_write(fd
, buf
, len1
);
1545 void socket_set_nonblock(int fd
)
1547 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1549 #endif /* !_WIN32 */
1558 #define STDIO_MAX_CLIENTS 1
1559 static int stdio_nb_clients
= 0;
1561 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1563 FDCharDriver
*s
= chr
->opaque
;
1564 return unix_write(s
->fd_out
, buf
, len
);
1567 static int fd_chr_read_poll(void *opaque
)
1569 CharDriverState
*chr
= opaque
;
1570 FDCharDriver
*s
= chr
->opaque
;
1572 s
->max_size
= qemu_chr_can_read(chr
);
1576 static void fd_chr_read(void *opaque
)
1578 CharDriverState
*chr
= opaque
;
1579 FDCharDriver
*s
= chr
->opaque
;
1584 if (len
> s
->max_size
)
1588 size
= read(s
->fd_in
, buf
, len
);
1590 /* FD has been closed. Remove it from the active list. */
1591 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1595 qemu_chr_read(chr
, buf
, size
);
1599 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1601 FDCharDriver
*s
= chr
->opaque
;
1603 if (s
->fd_in
>= 0) {
1604 if (nographic
&& s
->fd_in
== 0) {
1606 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1607 fd_chr_read
, NULL
, chr
);
1612 /* open a character device to a unix fd */
1613 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1615 CharDriverState
*chr
;
1618 chr
= qemu_mallocz(sizeof(CharDriverState
));
1621 s
= qemu_mallocz(sizeof(FDCharDriver
));
1629 chr
->chr_write
= fd_chr_write
;
1630 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
1632 qemu_chr_reset(chr
);
1637 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1641 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1644 return qemu_chr_open_fd(-1, fd_out
);
1647 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1650 char filename_in
[256], filename_out
[256];
1652 snprintf(filename_in
, 256, "%s.in", filename
);
1653 snprintf(filename_out
, 256, "%s.out", filename
);
1654 fd_in
= open(filename_in
, O_RDWR
| O_BINARY
);
1655 fd_out
= open(filename_out
, O_RDWR
| O_BINARY
);
1656 if (fd_in
< 0 || fd_out
< 0) {
1661 fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
);
1665 return qemu_chr_open_fd(fd_in
, fd_out
);
1669 /* for STDIO, we handle the case where several clients use it
1672 #define TERM_FIFO_MAX_SIZE 1
1674 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1675 static int term_fifo_size
;
1677 static int stdio_read_poll(void *opaque
)
1679 CharDriverState
*chr
= opaque
;
1681 /* try to flush the queue if needed */
1682 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
1683 qemu_chr_read(chr
, term_fifo
, 1);
1686 /* see if we can absorb more chars */
1687 if (term_fifo_size
== 0)
1693 static void stdio_read(void *opaque
)
1697 CharDriverState
*chr
= opaque
;
1699 size
= read(0, buf
, 1);
1701 /* stdin has been closed. Remove it from the active list. */
1702 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
1706 if (qemu_chr_can_read(chr
) > 0) {
1707 qemu_chr_read(chr
, buf
, 1);
1708 } else if (term_fifo_size
== 0) {
1709 term_fifo
[term_fifo_size
++] = buf
[0];
1714 /* init terminal so that we can grab keys */
1715 static struct termios oldtty
;
1716 static int old_fd0_flags
;
1718 static void term_exit(void)
1720 tcsetattr (0, TCSANOW
, &oldtty
);
1721 fcntl(0, F_SETFL
, old_fd0_flags
);
1724 static void term_init(void)
1728 tcgetattr (0, &tty
);
1730 old_fd0_flags
= fcntl(0, F_GETFL
);
1732 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1733 |INLCR
|IGNCR
|ICRNL
|IXON
);
1734 tty
.c_oflag
|= OPOST
;
1735 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1736 /* if graphical mode, we allow Ctrl-C handling */
1738 tty
.c_lflag
&= ~ISIG
;
1739 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1742 tty
.c_cc
[VTIME
] = 0;
1744 tcsetattr (0, TCSANOW
, &tty
);
1748 fcntl(0, F_SETFL
, O_NONBLOCK
);
1751 static CharDriverState
*qemu_chr_open_stdio(void)
1753 CharDriverState
*chr
;
1755 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1757 chr
= qemu_chr_open_fd(0, 1);
1758 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
1765 #if defined(__linux__)
1766 static CharDriverState
*qemu_chr_open_pty(void)
1769 char slave_name
[1024];
1770 int master_fd
, slave_fd
;
1772 /* Not satisfying */
1773 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1777 /* Disabling local echo and line-buffered output */
1778 tcgetattr (master_fd
, &tty
);
1779 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1781 tty
.c_cc
[VTIME
] = 0;
1782 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1784 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1785 return qemu_chr_open_fd(master_fd
, master_fd
);
1788 static void tty_serial_init(int fd
, int speed
,
1789 int parity
, int data_bits
, int stop_bits
)
1795 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1796 speed
, parity
, data_bits
, stop_bits
);
1798 tcgetattr (fd
, &tty
);
1840 cfsetispeed(&tty
, spd
);
1841 cfsetospeed(&tty
, spd
);
1843 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1844 |INLCR
|IGNCR
|ICRNL
|IXON
);
1845 tty
.c_oflag
|= OPOST
;
1846 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1847 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1868 tty
.c_cflag
|= PARENB
;
1871 tty
.c_cflag
|= PARENB
| PARODD
;
1875 tty
.c_cflag
|= CSTOPB
;
1877 tcsetattr (fd
, TCSANOW
, &tty
);
1880 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1882 FDCharDriver
*s
= chr
->opaque
;
1885 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1887 QEMUSerialSetParams
*ssp
= arg
;
1888 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1889 ssp
->data_bits
, ssp
->stop_bits
);
1892 case CHR_IOCTL_SERIAL_SET_BREAK
:
1894 int enable
= *(int *)arg
;
1896 tcsendbreak(s
->fd_in
, 1);
1905 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1907 CharDriverState
*chr
;
1910 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1913 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1914 tty_serial_init(fd
, 115200, 'N', 8, 1);
1915 chr
= qemu_chr_open_fd(fd
, fd
);
1918 chr
->chr_ioctl
= tty_serial_ioctl
;
1919 qemu_chr_reset(chr
);
1926 } ParallelCharDriver
;
1928 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1930 if (s
->mode
!= mode
) {
1932 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1939 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1941 ParallelCharDriver
*drv
= chr
->opaque
;
1946 case CHR_IOCTL_PP_READ_DATA
:
1947 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1949 *(uint8_t *)arg
= b
;
1951 case CHR_IOCTL_PP_WRITE_DATA
:
1952 b
= *(uint8_t *)arg
;
1953 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1956 case CHR_IOCTL_PP_READ_CONTROL
:
1957 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1959 /* Linux gives only the lowest bits, and no way to know data
1960 direction! For better compatibility set the fixed upper
1962 *(uint8_t *)arg
= b
| 0xc0;
1964 case CHR_IOCTL_PP_WRITE_CONTROL
:
1965 b
= *(uint8_t *)arg
;
1966 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1969 case CHR_IOCTL_PP_READ_STATUS
:
1970 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1972 *(uint8_t *)arg
= b
;
1974 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1975 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1976 struct ParallelIOArg
*parg
= arg
;
1977 int n
= read(fd
, parg
->buffer
, parg
->count
);
1978 if (n
!= parg
->count
) {
1983 case CHR_IOCTL_PP_EPP_READ
:
1984 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1985 struct ParallelIOArg
*parg
= arg
;
1986 int n
= read(fd
, parg
->buffer
, parg
->count
);
1987 if (n
!= parg
->count
) {
1992 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1993 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1994 struct ParallelIOArg
*parg
= arg
;
1995 int n
= write(fd
, parg
->buffer
, parg
->count
);
1996 if (n
!= parg
->count
) {
2001 case CHR_IOCTL_PP_EPP_WRITE
:
2002 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
2003 struct ParallelIOArg
*parg
= arg
;
2004 int n
= write(fd
, parg
->buffer
, parg
->count
);
2005 if (n
!= parg
->count
) {
2016 static void pp_close(CharDriverState
*chr
)
2018 ParallelCharDriver
*drv
= chr
->opaque
;
2021 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
2022 ioctl(fd
, PPRELEASE
);
2027 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
2029 CharDriverState
*chr
;
2030 ParallelCharDriver
*drv
;
2033 fd
= open(filename
, O_RDWR
);
2037 if (ioctl(fd
, PPCLAIM
) < 0) {
2042 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
2048 drv
->mode
= IEEE1284_MODE_COMPAT
;
2050 chr
= qemu_mallocz(sizeof(CharDriverState
));
2056 chr
->chr_write
= null_chr_write
;
2057 chr
->chr_ioctl
= pp_ioctl
;
2058 chr
->chr_close
= pp_close
;
2061 qemu_chr_reset(chr
);
2067 static CharDriverState
*qemu_chr_open_pty(void)
2073 #endif /* !defined(_WIN32) */
2078 HANDLE hcom
, hrecv
, hsend
;
2079 OVERLAPPED orecv
, osend
;
2084 #define NSENDBUF 2048
2085 #define NRECVBUF 2048
2086 #define MAXCONNECT 1
2087 #define NTIMEOUT 5000
2089 static int win_chr_poll(void *opaque
);
2090 static int win_chr_pipe_poll(void *opaque
);
2092 static void win_chr_close(CharDriverState
*chr
)
2094 WinCharState
*s
= chr
->opaque
;
2097 CloseHandle(s
->hsend
);
2101 CloseHandle(s
->hrecv
);
2105 CloseHandle(s
->hcom
);
2109 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
2111 qemu_del_polling_cb(win_chr_poll
, chr
);
2114 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
2116 WinCharState
*s
= chr
->opaque
;
2118 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2123 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2125 fprintf(stderr
, "Failed CreateEvent\n");
2128 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2130 fprintf(stderr
, "Failed CreateEvent\n");
2134 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2135 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2136 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2137 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
2142 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2143 fprintf(stderr
, "Failed SetupComm\n");
2147 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2148 size
= sizeof(COMMCONFIG
);
2149 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2150 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2151 CommConfigDialog(filename
, NULL
, &comcfg
);
2153 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2154 fprintf(stderr
, "Failed SetCommState\n");
2158 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2159 fprintf(stderr
, "Failed SetCommMask\n");
2163 cto
.ReadIntervalTimeout
= MAXDWORD
;
2164 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2165 fprintf(stderr
, "Failed SetCommTimeouts\n");
2169 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2170 fprintf(stderr
, "Failed ClearCommError\n");
2173 qemu_add_polling_cb(win_chr_poll
, chr
);
2181 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2183 WinCharState
*s
= chr
->opaque
;
2184 DWORD len
, ret
, size
, err
;
2187 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2188 s
->osend
.hEvent
= s
->hsend
;
2191 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2193 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2195 err
= GetLastError();
2196 if (err
== ERROR_IO_PENDING
) {
2197 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2215 static int win_chr_read_poll(CharDriverState
*chr
)
2217 WinCharState
*s
= chr
->opaque
;
2219 s
->max_size
= qemu_chr_can_read(chr
);
2223 static void win_chr_readfile(CharDriverState
*chr
)
2225 WinCharState
*s
= chr
->opaque
;
2230 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2231 s
->orecv
.hEvent
= s
->hrecv
;
2232 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2234 err
= GetLastError();
2235 if (err
== ERROR_IO_PENDING
) {
2236 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2241 qemu_chr_read(chr
, buf
, size
);
2245 static void win_chr_read(CharDriverState
*chr
)
2247 WinCharState
*s
= chr
->opaque
;
2249 if (s
->len
> s
->max_size
)
2250 s
->len
= s
->max_size
;
2254 win_chr_readfile(chr
);
2257 static int win_chr_poll(void *opaque
)
2259 CharDriverState
*chr
= opaque
;
2260 WinCharState
*s
= chr
->opaque
;
2264 ClearCommError(s
->hcom
, &comerr
, &status
);
2265 if (status
.cbInQue
> 0) {
2266 s
->len
= status
.cbInQue
;
2267 win_chr_read_poll(chr
);
2274 static CharDriverState
*qemu_chr_open_win(const char *filename
)
2276 CharDriverState
*chr
;
2279 chr
= qemu_mallocz(sizeof(CharDriverState
));
2282 s
= qemu_mallocz(sizeof(WinCharState
));
2288 chr
->chr_write
= win_chr_write
;
2289 chr
->chr_close
= win_chr_close
;
2291 if (win_chr_init(chr
, filename
) < 0) {
2296 qemu_chr_reset(chr
);
2300 static int win_chr_pipe_poll(void *opaque
)
2302 CharDriverState
*chr
= opaque
;
2303 WinCharState
*s
= chr
->opaque
;
2306 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2309 win_chr_read_poll(chr
);
2316 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
2318 WinCharState
*s
= chr
->opaque
;
2326 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2328 fprintf(stderr
, "Failed CreateEvent\n");
2331 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2333 fprintf(stderr
, "Failed CreateEvent\n");
2337 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2338 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2339 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2341 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2342 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2343 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2348 ZeroMemory(&ov
, sizeof(ov
));
2349 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2350 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2352 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2356 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2358 fprintf(stderr
, "Failed GetOverlappedResult\n");
2360 CloseHandle(ov
.hEvent
);
2367 CloseHandle(ov
.hEvent
);
2370 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2379 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2381 CharDriverState
*chr
;
2384 chr
= qemu_mallocz(sizeof(CharDriverState
));
2387 s
= qemu_mallocz(sizeof(WinCharState
));
2393 chr
->chr_write
= win_chr_write
;
2394 chr
->chr_close
= win_chr_close
;
2396 if (win_chr_pipe_init(chr
, filename
) < 0) {
2401 qemu_chr_reset(chr
);
2405 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2407 CharDriverState
*chr
;
2410 chr
= qemu_mallocz(sizeof(CharDriverState
));
2413 s
= qemu_mallocz(sizeof(WinCharState
));
2420 chr
->chr_write
= win_chr_write
;
2421 qemu_chr_reset(chr
);
2425 static CharDriverState
*qemu_chr_open_win_con(const char *filename
)
2427 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2430 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2434 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2435 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2436 if (fd_out
== INVALID_HANDLE_VALUE
)
2439 return qemu_chr_open_win_file(fd_out
);
2443 /***********************************************************/
2444 /* UDP Net console */
2448 struct sockaddr_in daddr
;
2455 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2457 NetCharDriver
*s
= chr
->opaque
;
2459 return sendto(s
->fd
, buf
, len
, 0,
2460 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2463 static int udp_chr_read_poll(void *opaque
)
2465 CharDriverState
*chr
= opaque
;
2466 NetCharDriver
*s
= chr
->opaque
;
2468 s
->max_size
= qemu_chr_can_read(chr
);
2470 /* If there were any stray characters in the queue process them
2473 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2474 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2476 s
->max_size
= qemu_chr_can_read(chr
);
2481 static void udp_chr_read(void *opaque
)
2483 CharDriverState
*chr
= opaque
;
2484 NetCharDriver
*s
= chr
->opaque
;
2486 if (s
->max_size
== 0)
2488 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2489 s
->bufptr
= s
->bufcnt
;
2494 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2495 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
2497 s
->max_size
= qemu_chr_can_read(chr
);
2501 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2503 NetCharDriver
*s
= chr
->opaque
;
2506 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2507 udp_chr_read
, NULL
, chr
);
2511 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2513 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2515 int parse_host_src_port(struct sockaddr_in
*haddr
,
2516 struct sockaddr_in
*saddr
,
2519 static CharDriverState
*qemu_chr_open_udp(const char *def
)
2521 CharDriverState
*chr
= NULL
;
2522 NetCharDriver
*s
= NULL
;
2524 struct sockaddr_in saddr
;
2526 chr
= qemu_mallocz(sizeof(CharDriverState
));
2529 s
= qemu_mallocz(sizeof(NetCharDriver
));
2533 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2535 perror("socket(PF_INET, SOCK_DGRAM)");
2539 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2540 printf("Could not parse: %s\n", def
);
2544 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2554 chr
->chr_write
= udp_chr_write
;
2555 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2568 /***********************************************************/
2569 /* TCP Net console */
2580 static void tcp_chr_accept(void *opaque
);
2582 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2584 TCPCharDriver
*s
= chr
->opaque
;
2586 return send_all(s
->fd
, buf
, len
);
2588 /* XXX: indicate an error ? */
2593 static int tcp_chr_read_poll(void *opaque
)
2595 CharDriverState
*chr
= opaque
;
2596 TCPCharDriver
*s
= chr
->opaque
;
2599 s
->max_size
= qemu_chr_can_read(chr
);
2604 #define IAC_BREAK 243
2605 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2607 char *buf
, int *size
)
2609 /* Handle any telnet client's basic IAC options to satisfy char by
2610 * char mode with no echo. All IAC options will be removed from
2611 * the buf and the do_telnetopt variable will be used to track the
2612 * state of the width of the IAC information.
2614 * IAC commands come in sets of 3 bytes with the exception of the
2615 * "IAC BREAK" command and the double IAC.
2621 for (i
= 0; i
< *size
; i
++) {
2622 if (s
->do_telnetopt
> 1) {
2623 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2624 /* Double IAC means send an IAC */
2628 s
->do_telnetopt
= 1;
2630 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2631 /* Handle IAC break commands by sending a serial break */
2632 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
2637 if (s
->do_telnetopt
>= 4) {
2638 s
->do_telnetopt
= 1;
2641 if ((unsigned char)buf
[i
] == IAC
) {
2642 s
->do_telnetopt
= 2;
2653 static void tcp_chr_read(void *opaque
)
2655 CharDriverState
*chr
= opaque
;
2656 TCPCharDriver
*s
= chr
->opaque
;
2660 if (!s
->connected
|| s
->max_size
<= 0)
2663 if (len
> s
->max_size
)
2665 size
= recv(s
->fd
, buf
, len
, 0);
2667 /* connection closed */
2669 if (s
->listen_fd
>= 0) {
2670 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2672 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2675 } else if (size
> 0) {
2676 if (s
->do_telnetopt
)
2677 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2679 qemu_chr_read(chr
, buf
, size
);
2683 static void tcp_chr_connect(void *opaque
)
2685 CharDriverState
*chr
= opaque
;
2686 TCPCharDriver
*s
= chr
->opaque
;
2689 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2690 tcp_chr_read
, NULL
, chr
);
2691 qemu_chr_reset(chr
);
2694 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2695 static void tcp_chr_telnet_init(int fd
)
2698 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2699 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2700 send(fd
, (char *)buf
, 3, 0);
2701 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2702 send(fd
, (char *)buf
, 3, 0);
2703 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2704 send(fd
, (char *)buf
, 3, 0);
2705 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2706 send(fd
, (char *)buf
, 3, 0);
2709 static void socket_set_nodelay(int fd
)
2712 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2715 static void tcp_chr_accept(void *opaque
)
2717 CharDriverState
*chr
= opaque
;
2718 TCPCharDriver
*s
= chr
->opaque
;
2719 struct sockaddr_in saddr
;
2721 struct sockaddr_un uaddr
;
2723 struct sockaddr
*addr
;
2730 len
= sizeof(uaddr
);
2731 addr
= (struct sockaddr
*)&uaddr
;
2735 len
= sizeof(saddr
);
2736 addr
= (struct sockaddr
*)&saddr
;
2738 fd
= accept(s
->listen_fd
, addr
, &len
);
2739 if (fd
< 0 && errno
!= EINTR
) {
2741 } else if (fd
>= 0) {
2742 if (s
->do_telnetopt
)
2743 tcp_chr_telnet_init(fd
);
2747 socket_set_nonblock(fd
);
2749 socket_set_nodelay(fd
);
2751 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2752 tcp_chr_connect(chr
);
2755 static void tcp_chr_close(CharDriverState
*chr
)
2757 TCPCharDriver
*s
= chr
->opaque
;
2760 if (s
->listen_fd
>= 0)
2761 closesocket(s
->listen_fd
);
2765 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2769 CharDriverState
*chr
= NULL
;
2770 TCPCharDriver
*s
= NULL
;
2771 int fd
= -1, ret
, err
, val
;
2773 int is_waitconnect
= 1;
2776 struct sockaddr_in saddr
;
2778 struct sockaddr_un uaddr
;
2780 struct sockaddr
*addr
;
2785 addr
= (struct sockaddr
*)&uaddr
;
2786 addrlen
= sizeof(uaddr
);
2787 if (parse_unix_path(&uaddr
, host_str
) < 0)
2792 addr
= (struct sockaddr
*)&saddr
;
2793 addrlen
= sizeof(saddr
);
2794 if (parse_host_port(&saddr
, host_str
) < 0)
2799 while((ptr
= strchr(ptr
,','))) {
2801 if (!strncmp(ptr
,"server",6)) {
2803 } else if (!strncmp(ptr
,"nowait",6)) {
2805 } else if (!strncmp(ptr
,"nodelay",6)) {
2808 printf("Unknown option: %s\n", ptr
);
2815 chr
= qemu_mallocz(sizeof(CharDriverState
));
2818 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2824 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2827 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2832 if (!is_waitconnect
)
2833 socket_set_nonblock(fd
);
2838 s
->is_unix
= is_unix
;
2839 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2842 chr
->chr_write
= tcp_chr_write
;
2843 chr
->chr_close
= tcp_chr_close
;
2846 /* allow fast reuse */
2850 strncpy(path
, uaddr
.sun_path
, 108);
2857 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2860 ret
= bind(fd
, addr
, addrlen
);
2864 ret
= listen(fd
, 0);
2869 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2871 s
->do_telnetopt
= 1;
2874 ret
= connect(fd
, addr
, addrlen
);
2876 err
= socket_error();
2877 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2878 } else if (err
== EINPROGRESS
) {
2881 } else if (err
== WSAEALREADY
) {
2893 socket_set_nodelay(fd
);
2895 tcp_chr_connect(chr
);
2897 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2900 if (is_listen
&& is_waitconnect
) {
2901 printf("QEMU waiting for connection on: %s\n", host_str
);
2902 tcp_chr_accept(chr
);
2903 socket_set_nonblock(s
->listen_fd
);
2915 CharDriverState
*qemu_chr_open(const char *filename
)
2919 if (!strcmp(filename
, "vc")) {
2920 return text_console_init(&display_state
);
2921 } else if (!strcmp(filename
, "null")) {
2922 return qemu_chr_open_null();
2924 if (strstart(filename
, "tcp:", &p
)) {
2925 return qemu_chr_open_tcp(p
, 0, 0);
2927 if (strstart(filename
, "telnet:", &p
)) {
2928 return qemu_chr_open_tcp(p
, 1, 0);
2930 if (strstart(filename
, "udp:", &p
)) {
2931 return qemu_chr_open_udp(p
);
2933 if (strstart(filename
, "mon:", &p
)) {
2934 CharDriverState
*drv
= qemu_chr_open(p
);
2936 drv
= qemu_chr_open_mux(drv
);
2937 monitor_init(drv
, !nographic
);
2940 printf("Unable to open driver: %s\n", p
);
2944 if (strstart(filename
, "unix:", &p
)) {
2945 return qemu_chr_open_tcp(p
, 0, 1);
2946 } else if (strstart(filename
, "file:", &p
)) {
2947 return qemu_chr_open_file_out(p
);
2948 } else if (strstart(filename
, "pipe:", &p
)) {
2949 return qemu_chr_open_pipe(p
);
2950 } else if (!strcmp(filename
, "pty")) {
2951 return qemu_chr_open_pty();
2952 } else if (!strcmp(filename
, "stdio")) {
2953 return qemu_chr_open_stdio();
2956 #if defined(__linux__)
2957 if (strstart(filename
, "/dev/parport", NULL
)) {
2958 return qemu_chr_open_pp(filename
);
2960 if (strstart(filename
, "/dev/", NULL
)) {
2961 return qemu_chr_open_tty(filename
);
2965 if (strstart(filename
, "COM", NULL
)) {
2966 return qemu_chr_open_win(filename
);
2968 if (strstart(filename
, "pipe:", &p
)) {
2969 return qemu_chr_open_win_pipe(p
);
2971 if (strstart(filename
, "con:", NULL
)) {
2972 return qemu_chr_open_win_con(filename
);
2974 if (strstart(filename
, "file:", &p
)) {
2975 return qemu_chr_open_win_file_out(p
);
2983 void qemu_chr_close(CharDriverState
*chr
)
2986 chr
->chr_close(chr
);
2989 /***********************************************************/
2990 /* network device redirectors */
2992 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2996 for(i
=0;i
<size
;i
+=16) {
3000 fprintf(f
, "%08x ", i
);
3003 fprintf(f
, " %02x", buf
[i
+j
]);
3008 for(j
=0;j
<len
;j
++) {
3010 if (c
< ' ' || c
> '~')
3012 fprintf(f
, "%c", c
);
3018 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
3021 for(i
= 0; i
< 6; i
++) {
3022 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3035 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
3040 p1
= strchr(p
, sep
);
3046 if (len
> buf_size
- 1)
3048 memcpy(buf
, p
, len
);
3055 int parse_host_src_port(struct sockaddr_in
*haddr
,
3056 struct sockaddr_in
*saddr
,
3057 const char *input_str
)
3059 char *str
= strdup(input_str
);
3060 char *host_str
= str
;
3065 * Chop off any extra arguments at the end of the string which
3066 * would start with a comma, then fill in the src port information
3067 * if it was provided else use the "any address" and "any port".
3069 if ((ptr
= strchr(str
,',')))
3072 if ((src_str
= strchr(input_str
,'@'))) {
3077 if (parse_host_port(haddr
, host_str
) < 0)
3080 if (!src_str
|| *src_str
== '\0')
3083 if (parse_host_port(saddr
, src_str
) < 0)
3094 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
3102 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3104 saddr
->sin_family
= AF_INET
;
3105 if (buf
[0] == '\0') {
3106 saddr
->sin_addr
.s_addr
= 0;
3108 if (isdigit(buf
[0])) {
3109 if (!inet_aton(buf
, &saddr
->sin_addr
))
3112 if ((he
= gethostbyname(buf
)) == NULL
)
3114 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
3117 port
= strtol(p
, (char **)&r
, 0);
3120 saddr
->sin_port
= htons(port
);
3125 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
3130 len
= MIN(108, strlen(str
));
3131 p
= strchr(str
, ',');
3133 len
= MIN(len
, p
- str
);
3135 memset(uaddr
, 0, sizeof(*uaddr
));
3137 uaddr
->sun_family
= AF_UNIX
;
3138 memcpy(uaddr
->sun_path
, str
, len
);
3144 /* find or alloc a new VLAN */
3145 VLANState
*qemu_find_vlan(int id
)
3147 VLANState
**pvlan
, *vlan
;
3148 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3152 vlan
= qemu_mallocz(sizeof(VLANState
));
3157 pvlan
= &first_vlan
;
3158 while (*pvlan
!= NULL
)
3159 pvlan
= &(*pvlan
)->next
;
3164 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
3165 IOReadHandler
*fd_read
,
3166 IOCanRWHandler
*fd_can_read
,
3169 VLANClientState
*vc
, **pvc
;
3170 vc
= qemu_mallocz(sizeof(VLANClientState
));
3173 vc
->fd_read
= fd_read
;
3174 vc
->fd_can_read
= fd_can_read
;
3175 vc
->opaque
= opaque
;
3179 pvc
= &vlan
->first_client
;
3180 while (*pvc
!= NULL
)
3181 pvc
= &(*pvc
)->next
;
3186 int qemu_can_send_packet(VLANClientState
*vc1
)
3188 VLANState
*vlan
= vc1
->vlan
;
3189 VLANClientState
*vc
;
3191 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3193 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
3200 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
3202 VLANState
*vlan
= vc1
->vlan
;
3203 VLANClientState
*vc
;
3206 printf("vlan %d send:\n", vlan
->id
);
3207 hex_dump(stdout
, buf
, size
);
3209 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3211 vc
->fd_read(vc
->opaque
, buf
, size
);
3216 #if defined(CONFIG_SLIRP)
3218 /* slirp network adapter */
3220 static int slirp_inited
;
3221 static VLANClientState
*slirp_vc
;
3223 int slirp_can_output(void)
3225 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
3228 void slirp_output(const uint8_t *pkt
, int pkt_len
)
3231 printf("slirp output:\n");
3232 hex_dump(stdout
, pkt
, pkt_len
);
3236 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
3239 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
3242 printf("slirp input:\n");
3243 hex_dump(stdout
, buf
, size
);
3245 slirp_input(buf
, size
);
3248 static int net_slirp_init(VLANState
*vlan
)
3250 if (!slirp_inited
) {
3254 slirp_vc
= qemu_new_vlan_client(vlan
,
3255 slirp_receive
, NULL
, NULL
);
3256 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
3260 static void net_slirp_redir(const char *redir_str
)
3265 struct in_addr guest_addr
;
3266 int host_port
, guest_port
;
3268 if (!slirp_inited
) {
3274 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3276 if (!strcmp(buf
, "tcp")) {
3278 } else if (!strcmp(buf
, "udp")) {
3284 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3286 host_port
= strtol(buf
, &r
, 0);
3290 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3292 if (buf
[0] == '\0') {
3293 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
3295 if (!inet_aton(buf
, &guest_addr
))
3298 guest_port
= strtol(p
, &r
, 0);
3302 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3303 fprintf(stderr
, "qemu: could not set up redirection\n");
3308 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3316 static void smb_exit(void)
3320 char filename
[1024];
3322 /* erase all the files in the directory */
3323 d
= opendir(smb_dir
);
3328 if (strcmp(de
->d_name
, ".") != 0 &&
3329 strcmp(de
->d_name
, "..") != 0) {
3330 snprintf(filename
, sizeof(filename
), "%s/%s",
3331 smb_dir
, de
->d_name
);
3339 /* automatic user mode samba server configuration */
3340 void net_slirp_smb(const char *exported_dir
)
3342 char smb_conf
[1024];
3343 char smb_cmdline
[1024];
3346 if (!slirp_inited
) {
3351 /* XXX: better tmp dir construction */
3352 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3353 if (mkdir(smb_dir
, 0700) < 0) {
3354 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3357 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3359 f
= fopen(smb_conf
, "w");
3361 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3368 "socket address=127.0.0.1\n"
3369 "pid directory=%s\n"
3370 "lock directory=%s\n"
3371 "log file=%s/log.smbd\n"
3372 "smb passwd file=%s/smbpasswd\n"
3373 "security = share\n"
3388 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3389 SMBD_COMMAND
, smb_conf
);
3391 slirp_add_exec(0, smb_cmdline
, 4, 139);
3394 #endif /* !defined(_WIN32) */
3396 #endif /* CONFIG_SLIRP */
3398 #if !defined(_WIN32)
3400 typedef struct TAPState
{
3401 VLANClientState
*vc
;
3405 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3407 TAPState
*s
= opaque
;
3410 ret
= write(s
->fd
, buf
, size
);
3411 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3418 static void tap_send(void *opaque
)
3420 TAPState
*s
= opaque
;
3427 sbuf
.maxlen
= sizeof(buf
);
3429 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
3431 size
= read(s
->fd
, buf
, sizeof(buf
));
3434 qemu_send_packet(s
->vc
, buf
, size
);
3440 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3444 s
= qemu_mallocz(sizeof(TAPState
));
3448 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3449 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3450 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3455 static int tap_open(char *ifname
, int ifname_size
)
3461 fd
= open("/dev/tap", O_RDWR
);
3463 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3468 dev
= devname(s
.st_rdev
, S_IFCHR
);
3469 pstrcpy(ifname
, ifname_size
, dev
);
3471 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3474 #elif defined(__sun__)
3475 #define TUNNEWPPA (('T'<<16) | 0x0001)
3477 * Allocate TAP device, returns opened fd.
3478 * Stores dev name in the first arg(must be large enough).
3480 int tap_alloc(char *dev
)
3482 int tap_fd
, if_fd
, ppa
= -1;
3483 static int ip_fd
= 0;
3486 static int arp_fd
= 0;
3487 int ip_muxid
, arp_muxid
;
3488 struct strioctl strioc_if
, strioc_ppa
;
3489 int link_type
= I_PLINK
;;
3491 char actual_name
[32] = "";
3493 memset(&ifr
, 0x0, sizeof(ifr
));
3497 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
3501 /* Check if IP device was opened */
3505 if( (ip_fd
= open("/dev/udp", O_RDWR
, 0)) < 0){
3506 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
3510 if( (tap_fd
= open("/dev/tap", O_RDWR
, 0)) < 0){
3511 syslog(LOG_ERR
, "Can't open /dev/tap");
3515 /* Assign a new PPA and get its unit number. */
3516 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
3517 strioc_ppa
.ic_timout
= 0;
3518 strioc_ppa
.ic_len
= sizeof(ppa
);
3519 strioc_ppa
.ic_dp
= (char *)&ppa
;
3520 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
3521 syslog (LOG_ERR
, "Can't assign new interface");
3523 if( (if_fd
= open("/dev/tap", O_RDWR
, 0)) < 0){
3524 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
3527 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
3528 syslog(LOG_ERR
, "Can't push IP module");
3532 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
3533 syslog(LOG_ERR
, "Can't get flags\n");
3535 snprintf (actual_name
, 32, "tap%d", ppa
);
3536 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3539 /* Assign ppa according to the unit number returned by tun device */
3541 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
3542 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
3543 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
3544 syslog (LOG_ERR
, "Can't get flags\n");
3545 /* Push arp module to if_fd */
3546 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
3547 syslog (LOG_ERR
, "Can't push ARP module (2)");
3549 /* Push arp module to ip_fd */
3550 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
3551 syslog (LOG_ERR
, "I_POP failed\n");
3552 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
3553 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
3555 if ((arp_fd
= open ("/dev/tap", O_RDWR
, 0)) < 0)
3556 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
3558 /* Set ifname to arp */
3559 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
3560 strioc_if
.ic_timout
= 0;
3561 strioc_if
.ic_len
= sizeof(ifr
);
3562 strioc_if
.ic_dp
= (char *)&ifr
;
3563 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
3564 syslog (LOG_ERR
, "Can't set ifname to arp\n");
3567 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
3568 syslog(LOG_ERR
, "Can't link TAP device to IP");
3572 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
3573 syslog (LOG_ERR
, "Can't link TAP device to ARP");
3577 memset(&ifr
, 0x0, sizeof(ifr
));
3578 strncpy (ifr
.lifr_name
, actual_name
, sizeof (ifr
.lifr_name
));
3579 ifr
.lifr_ip_muxid
= ip_muxid
;
3580 ifr
.lifr_arp_muxid
= arp_muxid
;
3582 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
3584 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
3585 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
3586 syslog (LOG_ERR
, "Can't set multiplexor id");
3589 sprintf(dev
, "tap%d", ppa
);
3593 static int tap_open(char *ifname
, int ifname_size
)
3597 if( (fd
= tap_alloc(dev
)) < 0 ){
3598 fprintf(stderr
, "Cannot allocate TAP device\n");
3601 pstrcpy(ifname
, ifname_size
, dev
);
3602 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3606 static int tap_open(char *ifname
, int ifname_size
)
3611 fd
= open("/dev/net/tun", O_RDWR
);
3613 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
3616 memset(&ifr
, 0, sizeof(ifr
));
3617 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
3618 if (ifname
[0] != '\0')
3619 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
3621 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
3622 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
3624 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
3628 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
3629 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3634 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
3635 const char *setup_script
)
3638 int pid
, status
, fd
;
3643 if (ifname1
!= NULL
)
3644 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
3647 fd
= tap_open(ifname
, sizeof(ifname
));
3651 if (!setup_script
|| !strcmp(setup_script
, "no"))
3653 if (setup_script
[0] != '\0') {
3654 /* try to launch network init script */
3658 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
3659 for (i
= 0; i
< open_max
; i
++)
3660 if (i
!= STDIN_FILENO
&&
3661 i
!= STDOUT_FILENO
&&
3662 i
!= STDERR_FILENO
&&
3667 *parg
++ = (char *)setup_script
;
3670 execv(setup_script
, args
);
3673 while (waitpid(pid
, &status
, 0) != pid
);
3674 if (!WIFEXITED(status
) ||
3675 WEXITSTATUS(status
) != 0) {
3676 fprintf(stderr
, "%s: could not launch network script\n",
3682 s
= net_tap_fd_init(vlan
, fd
);
3685 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3686 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
3690 #endif /* !_WIN32 */
3692 /* network connection */
3693 typedef struct NetSocketState
{
3694 VLANClientState
*vc
;
3696 int state
; /* 0 = getting length, 1 = getting data */
3700 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
3703 typedef struct NetSocketListenState
{
3706 } NetSocketListenState
;
3708 /* XXX: we consider we can send the whole packet without blocking */
3709 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
3711 NetSocketState
*s
= opaque
;
3715 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
3716 send_all(s
->fd
, buf
, size
);
3719 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
3721 NetSocketState
*s
= opaque
;
3722 sendto(s
->fd
, buf
, size
, 0,
3723 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
3726 static void net_socket_send(void *opaque
)
3728 NetSocketState
*s
= opaque
;
3733 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
3735 err
= socket_error();
3736 if (err
!= EWOULDBLOCK
)
3738 } else if (size
== 0) {
3739 /* end of connection */
3741 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3747 /* reassemble a packet from the network */
3753 memcpy(s
->buf
+ s
->index
, buf
, l
);
3757 if (s
->index
== 4) {
3759 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
3765 l
= s
->packet_len
- s
->index
;
3768 memcpy(s
->buf
+ s
->index
, buf
, l
);
3772 if (s
->index
>= s
->packet_len
) {
3773 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
3782 static void net_socket_send_dgram(void *opaque
)
3784 NetSocketState
*s
= opaque
;
3787 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
3791 /* end of connection */
3792 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3795 qemu_send_packet(s
->vc
, s
->buf
, size
);
3798 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
3803 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
3804 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
3805 inet_ntoa(mcastaddr
->sin_addr
),
3806 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
3810 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
3812 perror("socket(PF_INET, SOCK_DGRAM)");
3817 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
3818 (const char *)&val
, sizeof(val
));
3820 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
3824 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
3830 /* Add host to multicast group */
3831 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
3832 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
3834 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
3835 (const char *)&imr
, sizeof(struct ip_mreq
));
3837 perror("setsockopt(IP_ADD_MEMBERSHIP)");
3841 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
3843 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
3844 (const char *)&val
, sizeof(val
));
3846 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
3850 socket_set_nonblock(fd
);
3858 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
3861 struct sockaddr_in saddr
;
3863 socklen_t saddr_len
;
3866 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
3867 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
3868 * by ONLY ONE process: we must "clone" this dgram socket --jjo
3872 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
3874 if (saddr
.sin_addr
.s_addr
==0) {
3875 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
3879 /* clone dgram socket */
3880 newfd
= net_socket_mcast_create(&saddr
);
3882 /* error already reported by net_socket_mcast_create() */
3886 /* clone newfd to fd, close newfd */
3891 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
3892 fd
, strerror(errno
));
3897 s
= qemu_mallocz(sizeof(NetSocketState
));
3902 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
3903 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
3905 /* mcast: save bound address as dst */
3906 if (is_connected
) s
->dgram_dst
=saddr
;
3908 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3909 "socket: fd=%d (%s mcast=%s:%d)",
3910 fd
, is_connected
? "cloned" : "",
3911 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3915 static void net_socket_connect(void *opaque
)
3917 NetSocketState
*s
= opaque
;
3918 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
3921 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
3925 s
= qemu_mallocz(sizeof(NetSocketState
));
3929 s
->vc
= qemu_new_vlan_client(vlan
,
3930 net_socket_receive
, NULL
, s
);
3931 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3932 "socket: fd=%d", fd
);
3934 net_socket_connect(s
);
3936 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
3941 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
3944 int so_type
=-1, optlen
=sizeof(so_type
);
3946 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
3947 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
3952 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
3954 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3956 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
3957 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
3958 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3963 static void net_socket_accept(void *opaque
)
3965 NetSocketListenState
*s
= opaque
;
3967 struct sockaddr_in saddr
;
3972 len
= sizeof(saddr
);
3973 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
3974 if (fd
< 0 && errno
!= EINTR
) {
3976 } else if (fd
>= 0) {
3980 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
3984 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
3985 "socket: connection from %s:%d",
3986 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3990 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
3992 NetSocketListenState
*s
;
3994 struct sockaddr_in saddr
;
3996 if (parse_host_port(&saddr
, host_str
) < 0)
3999 s
= qemu_mallocz(sizeof(NetSocketListenState
));
4003 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4008 socket_set_nonblock(fd
);
4010 /* allow fast reuse */
4012 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
4014 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4019 ret
= listen(fd
, 0);
4026 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
4030 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
4033 int fd
, connected
, ret
, err
;
4034 struct sockaddr_in saddr
;
4036 if (parse_host_port(&saddr
, host_str
) < 0)
4039 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
4044 socket_set_nonblock(fd
);
4048 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
4050 err
= socket_error();
4051 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
4052 } else if (err
== EINPROGRESS
) {
4055 } else if (err
== WSAEALREADY
) {
4068 s
= net_socket_fd_init(vlan
, fd
, connected
);
4071 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4072 "socket: connect to %s:%d",
4073 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4077 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
4081 struct sockaddr_in saddr
;
4083 if (parse_host_port(&saddr
, host_str
) < 0)
4087 fd
= net_socket_mcast_create(&saddr
);
4091 s
= net_socket_fd_init(vlan
, fd
, 0);
4095 s
->dgram_dst
= saddr
;
4097 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
4098 "socket: mcast=%s:%d",
4099 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
4104 static int get_param_value(char *buf
, int buf_size
,
4105 const char *tag
, const char *str
)
4114 while (*p
!= '\0' && *p
!= '=') {
4115 if ((q
- option
) < sizeof(option
) - 1)
4123 if (!strcmp(tag
, option
)) {
4125 while (*p
!= '\0' && *p
!= ',') {
4126 if ((q
- buf
) < buf_size
- 1)
4133 while (*p
!= '\0' && *p
!= ',') {
4144 static int net_client_init(const char *str
)
4155 while (*p
!= '\0' && *p
!= ',') {
4156 if ((q
- device
) < sizeof(device
) - 1)
4164 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
4165 vlan_id
= strtol(buf
, NULL
, 0);
4167 vlan
= qemu_find_vlan(vlan_id
);
4169 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
4172 if (!strcmp(device
, "nic")) {
4176 if (nb_nics
>= MAX_NICS
) {
4177 fprintf(stderr
, "Too Many NICs\n");
4180 nd
= &nd_table
[nb_nics
];
4181 macaddr
= nd
->macaddr
;
4187 macaddr
[5] = 0x56 + nb_nics
;
4189 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
4190 if (parse_macaddr(macaddr
, buf
) < 0) {
4191 fprintf(stderr
, "invalid syntax for ethernet address\n");
4195 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
4196 nd
->model
= strdup(buf
);
4200 vlan
->nb_guest_devs
++;
4203 if (!strcmp(device
, "none")) {
4204 /* does nothing. It is needed to signal that no network cards
4209 if (!strcmp(device
, "user")) {
4210 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
4211 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
4213 vlan
->nb_host_devs
++;
4214 ret
= net_slirp_init(vlan
);
4218 if (!strcmp(device
, "tap")) {
4220 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4221 fprintf(stderr
, "tap: no interface name\n");
4224 vlan
->nb_host_devs
++;
4225 ret
= tap_win32_init(vlan
, ifname
);
4228 if (!strcmp(device
, "tap")) {
4230 char setup_script
[1024];
4232 vlan
->nb_host_devs
++;
4233 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4234 fd
= strtol(buf
, NULL
, 0);
4236 if (net_tap_fd_init(vlan
, fd
))
4239 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
4242 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
4243 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
4245 ret
= net_tap_init(vlan
, ifname
, setup_script
);
4249 if (!strcmp(device
, "socket")) {
4250 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
4252 fd
= strtol(buf
, NULL
, 0);
4254 if (net_socket_fd_init(vlan
, fd
, 1))
4256 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
4257 ret
= net_socket_listen_init(vlan
, buf
);
4258 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
4259 ret
= net_socket_connect_init(vlan
, buf
);
4260 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
4261 ret
= net_socket_mcast_init(vlan
, buf
);
4263 fprintf(stderr
, "Unknown socket options: %s\n", p
);
4266 vlan
->nb_host_devs
++;
4269 fprintf(stderr
, "Unknown network device: %s\n", device
);
4273 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
4279 void do_info_network(void)
4282 VLANClientState
*vc
;
4284 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
4285 term_printf("VLAN %d devices:\n", vlan
->id
);
4286 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
4287 term_printf(" %s\n", vc
->info_str
);
4291 /***********************************************************/
4294 static USBPort
*used_usb_ports
;
4295 static USBPort
*free_usb_ports
;
4297 /* ??? Maybe change this to register a hub to keep track of the topology. */
4298 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
4299 usb_attachfn attach
)
4301 port
->opaque
= opaque
;
4302 port
->index
= index
;
4303 port
->attach
= attach
;
4304 port
->next
= free_usb_ports
;
4305 free_usb_ports
= port
;
4308 static int usb_device_add(const char *devname
)
4314 if (!free_usb_ports
)
4317 if (strstart(devname
, "host:", &p
)) {
4318 dev
= usb_host_device_open(p
);
4319 } else if (!strcmp(devname
, "mouse")) {
4320 dev
= usb_mouse_init();
4321 } else if (!strcmp(devname
, "tablet")) {
4322 dev
= usb_tablet_init();
4323 } else if (strstart(devname
, "disk:", &p
)) {
4324 dev
= usb_msd_init(p
);
4331 /* Find a USB port to add the device to. */
4332 port
= free_usb_ports
;
4336 /* Create a new hub and chain it on. */
4337 free_usb_ports
= NULL
;
4338 port
->next
= used_usb_ports
;
4339 used_usb_ports
= port
;
4341 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
4342 usb_attach(port
, hub
);
4343 port
= free_usb_ports
;
4346 free_usb_ports
= port
->next
;
4347 port
->next
= used_usb_ports
;
4348 used_usb_ports
= port
;
4349 usb_attach(port
, dev
);
4353 static int usb_device_del(const char *devname
)
4361 if (!used_usb_ports
)
4364 p
= strchr(devname
, '.');
4367 bus_num
= strtoul(devname
, NULL
, 0);
4368 addr
= strtoul(p
+ 1, NULL
, 0);
4372 lastp
= &used_usb_ports
;
4373 port
= used_usb_ports
;
4374 while (port
&& port
->dev
->addr
!= addr
) {
4375 lastp
= &port
->next
;
4383 *lastp
= port
->next
;
4384 usb_attach(port
, NULL
);
4385 dev
->handle_destroy(dev
);
4386 port
->next
= free_usb_ports
;
4387 free_usb_ports
= port
;
4391 void do_usb_add(const char *devname
)
4394 ret
= usb_device_add(devname
);
4396 term_printf("Could not add USB device '%s'\n", devname
);
4399 void do_usb_del(const char *devname
)
4402 ret
= usb_device_del(devname
);
4404 term_printf("Could not remove USB device '%s'\n", devname
);
4411 const char *speed_str
;
4414 term_printf("USB support not enabled\n");
4418 for (port
= used_usb_ports
; port
; port
= port
->next
) {
4422 switch(dev
->speed
) {
4426 case USB_SPEED_FULL
:
4429 case USB_SPEED_HIGH
:
4436 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
4437 0, dev
->addr
, speed_str
, dev
->devname
);
4441 /***********************************************************/
4442 /* PCMCIA/Cardbus */
4444 static struct pcmcia_socket_entry_s
{
4445 struct pcmcia_socket_s
*socket
;
4446 struct pcmcia_socket_entry_s
*next
;
4447 } *pcmcia_sockets
= 0;
4449 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
4451 struct pcmcia_socket_entry_s
*entry
;
4453 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
4454 entry
->socket
= socket
;
4455 entry
->next
= pcmcia_sockets
;
4456 pcmcia_sockets
= entry
;
4459 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
4461 struct pcmcia_socket_entry_s
*entry
, **ptr
;
4463 ptr
= &pcmcia_sockets
;
4464 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
4465 if (entry
->socket
== socket
) {
4471 void pcmcia_info(void)
4473 struct pcmcia_socket_entry_s
*iter
;
4474 if (!pcmcia_sockets
)
4475 term_printf("No PCMCIA sockets\n");
4477 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
4478 term_printf("%s: %s\n", iter
->socket
->slot_string
,
4479 iter
->socket
->attached
? iter
->socket
->card_string
:
4483 /***********************************************************/
4486 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4490 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4494 static void dumb_refresh(DisplayState
*ds
)
4499 void dumb_display_init(DisplayState
*ds
)
4504 ds
->dpy_update
= dumb_update
;
4505 ds
->dpy_resize
= dumb_resize
;
4506 ds
->dpy_refresh
= dumb_refresh
;
4509 /***********************************************************/
4512 #define MAX_IO_HANDLERS 64
4514 typedef struct IOHandlerRecord
{
4516 IOCanRWHandler
*fd_read_poll
;
4518 IOHandler
*fd_write
;
4521 /* temporary data */
4523 struct IOHandlerRecord
*next
;
4526 static IOHandlerRecord
*first_io_handler
;
4528 /* XXX: fd_read_poll should be suppressed, but an API change is
4529 necessary in the character devices to suppress fd_can_read(). */
4530 int qemu_set_fd_handler2(int fd
,
4531 IOCanRWHandler
*fd_read_poll
,
4533 IOHandler
*fd_write
,
4536 IOHandlerRecord
**pioh
, *ioh
;
4538 if (!fd_read
&& !fd_write
) {
4539 pioh
= &first_io_handler
;
4544 if (ioh
->fd
== fd
) {
4551 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4555 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4558 ioh
->next
= first_io_handler
;
4559 first_io_handler
= ioh
;
4562 ioh
->fd_read_poll
= fd_read_poll
;
4563 ioh
->fd_read
= fd_read
;
4564 ioh
->fd_write
= fd_write
;
4565 ioh
->opaque
= opaque
;
4571 int qemu_set_fd_handler(int fd
,
4573 IOHandler
*fd_write
,
4576 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4579 /***********************************************************/
4580 /* Polling handling */
4582 typedef struct PollingEntry
{
4585 struct PollingEntry
*next
;
4588 static PollingEntry
*first_polling_entry
;
4590 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4592 PollingEntry
**ppe
, *pe
;
4593 pe
= qemu_mallocz(sizeof(PollingEntry
));
4597 pe
->opaque
= opaque
;
4598 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
4603 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
4605 PollingEntry
**ppe
, *pe
;
4606 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
4608 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
4617 /***********************************************************/
4618 /* Wait objects support */
4619 typedef struct WaitObjects
{
4621 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
4622 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
4623 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
4626 static WaitObjects wait_objects
= {0};
4628 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4630 WaitObjects
*w
= &wait_objects
;
4632 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
4634 w
->events
[w
->num
] = handle
;
4635 w
->func
[w
->num
] = func
;
4636 w
->opaque
[w
->num
] = opaque
;
4641 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4644 WaitObjects
*w
= &wait_objects
;
4647 for (i
= 0; i
< w
->num
; i
++) {
4648 if (w
->events
[i
] == handle
)
4651 w
->events
[i
] = w
->events
[i
+ 1];
4652 w
->func
[i
] = w
->func
[i
+ 1];
4653 w
->opaque
[i
] = w
->opaque
[i
+ 1];
4661 /***********************************************************/
4662 /* savevm/loadvm support */
4664 #define IO_BUF_SIZE 32768
4668 BlockDriverState
*bs
;
4671 int64_t base_offset
;
4672 int64_t buf_offset
; /* start of buffer when writing, end of buffer
4675 int buf_size
; /* 0 when writing */
4676 uint8_t buf
[IO_BUF_SIZE
];
4679 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
4683 f
= qemu_mallocz(sizeof(QEMUFile
));
4686 if (!strcmp(mode
, "wb")) {
4688 } else if (!strcmp(mode
, "rb")) {
4693 f
->outfile
= fopen(filename
, mode
);
4705 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
4709 f
= qemu_mallocz(sizeof(QEMUFile
));
4714 f
->is_writable
= is_writable
;
4715 f
->base_offset
= offset
;
4719 void qemu_fflush(QEMUFile
*f
)
4721 if (!f
->is_writable
)
4723 if (f
->buf_index
> 0) {
4725 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4726 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
4728 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4729 f
->buf
, f
->buf_index
);
4731 f
->buf_offset
+= f
->buf_index
;
4736 static void qemu_fill_buffer(QEMUFile
*f
)
4743 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4744 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
4748 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4749 f
->buf
, IO_BUF_SIZE
);
4755 f
->buf_offset
+= len
;
4758 void qemu_fclose(QEMUFile
*f
)
4768 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
4772 l
= IO_BUF_SIZE
- f
->buf_index
;
4775 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
4779 if (f
->buf_index
>= IO_BUF_SIZE
)
4784 void qemu_put_byte(QEMUFile
*f
, int v
)
4786 f
->buf
[f
->buf_index
++] = v
;
4787 if (f
->buf_index
>= IO_BUF_SIZE
)
4791 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
4797 l
= f
->buf_size
- f
->buf_index
;
4799 qemu_fill_buffer(f
);
4800 l
= f
->buf_size
- f
->buf_index
;
4806 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
4811 return size1
- size
;
4814 int qemu_get_byte(QEMUFile
*f
)
4816 if (f
->buf_index
>= f
->buf_size
) {
4817 qemu_fill_buffer(f
);
4818 if (f
->buf_index
>= f
->buf_size
)
4821 return f
->buf
[f
->buf_index
++];
4824 int64_t qemu_ftell(QEMUFile
*f
)
4826 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
4829 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
4831 if (whence
== SEEK_SET
) {
4833 } else if (whence
== SEEK_CUR
) {
4834 pos
+= qemu_ftell(f
);
4836 /* SEEK_END not supported */
4839 if (f
->is_writable
) {
4841 f
->buf_offset
= pos
;
4843 f
->buf_offset
= pos
;
4850 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
4852 qemu_put_byte(f
, v
>> 8);
4853 qemu_put_byte(f
, v
);
4856 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
4858 qemu_put_byte(f
, v
>> 24);
4859 qemu_put_byte(f
, v
>> 16);
4860 qemu_put_byte(f
, v
>> 8);
4861 qemu_put_byte(f
, v
);
4864 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
4866 qemu_put_be32(f
, v
>> 32);
4867 qemu_put_be32(f
, v
);
4870 unsigned int qemu_get_be16(QEMUFile
*f
)
4873 v
= qemu_get_byte(f
) << 8;
4874 v
|= qemu_get_byte(f
);
4878 unsigned int qemu_get_be32(QEMUFile
*f
)
4881 v
= qemu_get_byte(f
) << 24;
4882 v
|= qemu_get_byte(f
) << 16;
4883 v
|= qemu_get_byte(f
) << 8;
4884 v
|= qemu_get_byte(f
);
4888 uint64_t qemu_get_be64(QEMUFile
*f
)
4891 v
= (uint64_t)qemu_get_be32(f
) << 32;
4892 v
|= qemu_get_be32(f
);
4896 typedef struct SaveStateEntry
{
4900 SaveStateHandler
*save_state
;
4901 LoadStateHandler
*load_state
;
4903 struct SaveStateEntry
*next
;
4906 static SaveStateEntry
*first_se
;
4908 int register_savevm(const char *idstr
,
4911 SaveStateHandler
*save_state
,
4912 LoadStateHandler
*load_state
,
4915 SaveStateEntry
*se
, **pse
;
4917 se
= qemu_malloc(sizeof(SaveStateEntry
));
4920 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
4921 se
->instance_id
= instance_id
;
4922 se
->version_id
= version_id
;
4923 se
->save_state
= save_state
;
4924 se
->load_state
= load_state
;
4925 se
->opaque
= opaque
;
4928 /* add at the end of list */
4930 while (*pse
!= NULL
)
4931 pse
= &(*pse
)->next
;
4936 #define QEMU_VM_FILE_MAGIC 0x5145564d
4937 #define QEMU_VM_FILE_VERSION 0x00000002
4939 int qemu_savevm_state(QEMUFile
*f
)
4943 int64_t cur_pos
, len_pos
, total_len_pos
;
4945 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
4946 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
4947 total_len_pos
= qemu_ftell(f
);
4948 qemu_put_be64(f
, 0); /* total size */
4950 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4952 len
= strlen(se
->idstr
);
4953 qemu_put_byte(f
, len
);
4954 qemu_put_buffer(f
, se
->idstr
, len
);
4956 qemu_put_be32(f
, se
->instance_id
);
4957 qemu_put_be32(f
, se
->version_id
);
4959 /* record size: filled later */
4960 len_pos
= qemu_ftell(f
);
4961 qemu_put_be32(f
, 0);
4963 se
->save_state(f
, se
->opaque
);
4965 /* fill record size */
4966 cur_pos
= qemu_ftell(f
);
4967 len
= cur_pos
- len_pos
- 4;
4968 qemu_fseek(f
, len_pos
, SEEK_SET
);
4969 qemu_put_be32(f
, len
);
4970 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4972 cur_pos
= qemu_ftell(f
);
4973 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
4974 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
4975 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4981 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
4985 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4986 if (!strcmp(se
->idstr
, idstr
) &&
4987 instance_id
== se
->instance_id
)
4993 int qemu_loadvm_state(QEMUFile
*f
)
4996 int len
, ret
, instance_id
, record_len
, version_id
;
4997 int64_t total_len
, end_pos
, cur_pos
;
5001 v
= qemu_get_be32(f
);
5002 if (v
!= QEMU_VM_FILE_MAGIC
)
5004 v
= qemu_get_be32(f
);
5005 if (v
!= QEMU_VM_FILE_VERSION
) {
5010 total_len
= qemu_get_be64(f
);
5011 end_pos
= total_len
+ qemu_ftell(f
);
5013 if (qemu_ftell(f
) >= end_pos
)
5015 len
= qemu_get_byte(f
);
5016 qemu_get_buffer(f
, idstr
, len
);
5018 instance_id
= qemu_get_be32(f
);
5019 version_id
= qemu_get_be32(f
);
5020 record_len
= qemu_get_be32(f
);
5022 printf("idstr=%s instance=0x%x version=%d len=%d\n",
5023 idstr
, instance_id
, version_id
, record_len
);
5025 cur_pos
= qemu_ftell(f
);
5026 se
= find_se(idstr
, instance_id
);
5028 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
5029 instance_id
, idstr
);
5031 ret
= se
->load_state(f
, se
->opaque
, version_id
);
5033 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
5034 instance_id
, idstr
);
5037 /* always seek to exact end of record */
5038 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
5045 /* device can contain snapshots */
5046 static int bdrv_can_snapshot(BlockDriverState
*bs
)
5049 !bdrv_is_removable(bs
) &&
5050 !bdrv_is_read_only(bs
));
5053 /* device must be snapshots in order to have a reliable snapshot */
5054 static int bdrv_has_snapshot(BlockDriverState
*bs
)
5057 !bdrv_is_removable(bs
) &&
5058 !bdrv_is_read_only(bs
));
5061 static BlockDriverState
*get_bs_snapshots(void)
5063 BlockDriverState
*bs
;
5067 return bs_snapshots
;
5068 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5070 if (bdrv_can_snapshot(bs
))
5079 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
5082 QEMUSnapshotInfo
*sn_tab
, *sn
;
5086 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5089 for(i
= 0; i
< nb_sns
; i
++) {
5091 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
5101 void do_savevm(const char *name
)
5103 BlockDriverState
*bs
, *bs1
;
5104 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
5105 int must_delete
, ret
, i
;
5106 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5108 int saved_vm_running
;
5115 bs
= get_bs_snapshots();
5117 term_printf("No block device can accept snapshots\n");
5121 /* ??? Should this occur after vm_stop? */
5124 saved_vm_running
= vm_running
;
5129 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
5134 memset(sn
, 0, sizeof(*sn
));
5136 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
5137 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
5140 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
5143 /* fill auxiliary fields */
5146 sn
->date_sec
= tb
.time
;
5147 sn
->date_nsec
= tb
.millitm
* 1000000;
5149 gettimeofday(&tv
, NULL
);
5150 sn
->date_sec
= tv
.tv_sec
;
5151 sn
->date_nsec
= tv
.tv_usec
* 1000;
5153 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
5155 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5156 term_printf("Device %s does not support VM state snapshots\n",
5157 bdrv_get_device_name(bs
));
5161 /* save the VM state */
5162 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
5164 term_printf("Could not open VM state file\n");
5167 ret
= qemu_savevm_state(f
);
5168 sn
->vm_state_size
= qemu_ftell(f
);
5171 term_printf("Error %d while writing VM\n", ret
);
5175 /* create the snapshots */
5177 for(i
= 0; i
< MAX_DISKS
; i
++) {
5179 if (bdrv_has_snapshot(bs1
)) {
5181 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
5183 term_printf("Error while deleting snapshot on '%s'\n",
5184 bdrv_get_device_name(bs1
));
5187 ret
= bdrv_snapshot_create(bs1
, sn
);
5189 term_printf("Error while creating snapshot on '%s'\n",
5190 bdrv_get_device_name(bs1
));
5196 if (saved_vm_running
)
5200 void do_loadvm(const char *name
)
5202 BlockDriverState
*bs
, *bs1
;
5203 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
5206 int saved_vm_running
;
5208 bs
= get_bs_snapshots();
5210 term_printf("No block device supports snapshots\n");
5214 /* Flush all IO requests so they don't interfere with the new state. */
5217 saved_vm_running
= vm_running
;
5220 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5222 if (bdrv_has_snapshot(bs1
)) {
5223 ret
= bdrv_snapshot_goto(bs1
, name
);
5226 term_printf("Warning: ");
5229 term_printf("Snapshots not supported on device '%s'\n",
5230 bdrv_get_device_name(bs1
));
5233 term_printf("Could not find snapshot '%s' on device '%s'\n",
5234 name
, bdrv_get_device_name(bs1
));
5237 term_printf("Error %d while activating snapshot on '%s'\n",
5238 ret
, bdrv_get_device_name(bs1
));
5241 /* fatal on snapshot block device */
5248 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
5249 term_printf("Device %s does not support VM state snapshots\n",
5250 bdrv_get_device_name(bs
));
5254 /* restore the VM state */
5255 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
5257 term_printf("Could not open VM state file\n");
5260 ret
= qemu_loadvm_state(f
);
5263 term_printf("Error %d while loading VM state\n", ret
);
5266 if (saved_vm_running
)
5270 void do_delvm(const char *name
)
5272 BlockDriverState
*bs
, *bs1
;
5275 bs
= get_bs_snapshots();
5277 term_printf("No block device supports snapshots\n");
5281 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5283 if (bdrv_has_snapshot(bs1
)) {
5284 ret
= bdrv_snapshot_delete(bs1
, name
);
5286 if (ret
== -ENOTSUP
)
5287 term_printf("Snapshots not supported on device '%s'\n",
5288 bdrv_get_device_name(bs1
));
5290 term_printf("Error %d while deleting snapshot on '%s'\n",
5291 ret
, bdrv_get_device_name(bs1
));
5297 void do_info_snapshots(void)
5299 BlockDriverState
*bs
, *bs1
;
5300 QEMUSnapshotInfo
*sn_tab
, *sn
;
5304 bs
= get_bs_snapshots();
5306 term_printf("No available block device supports snapshots\n");
5309 term_printf("Snapshot devices:");
5310 for(i
= 0; i
<= MAX_DISKS
; i
++) {
5312 if (bdrv_has_snapshot(bs1
)) {
5314 term_printf(" %s", bdrv_get_device_name(bs1
));
5319 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
5321 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
5324 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
5325 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
5326 for(i
= 0; i
< nb_sns
; i
++) {
5328 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
5333 /***********************************************************/
5334 /* cpu save/restore */
5336 #if defined(TARGET_I386)
5338 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
5340 qemu_put_be32(f
, dt
->selector
);
5341 qemu_put_betl(f
, dt
->base
);
5342 qemu_put_be32(f
, dt
->limit
);
5343 qemu_put_be32(f
, dt
->flags
);
5346 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
5348 dt
->selector
= qemu_get_be32(f
);
5349 dt
->base
= qemu_get_betl(f
);
5350 dt
->limit
= qemu_get_be32(f
);
5351 dt
->flags
= qemu_get_be32(f
);
5354 void cpu_save(QEMUFile
*f
, void *opaque
)
5356 CPUState
*env
= opaque
;
5357 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
5361 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5362 qemu_put_betls(f
, &env
->regs
[i
]);
5363 qemu_put_betls(f
, &env
->eip
);
5364 qemu_put_betls(f
, &env
->eflags
);
5365 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
5366 qemu_put_be32s(f
, &hflags
);
5370 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
5372 for(i
= 0; i
< 8; i
++) {
5373 fptag
|= ((!env
->fptags
[i
]) << i
);
5376 qemu_put_be16s(f
, &fpuc
);
5377 qemu_put_be16s(f
, &fpus
);
5378 qemu_put_be16s(f
, &fptag
);
5380 #ifdef USE_X86LDOUBLE
5385 qemu_put_be16s(f
, &fpregs_format
);
5387 for(i
= 0; i
< 8; i
++) {
5388 #ifdef USE_X86LDOUBLE
5392 /* we save the real CPU data (in case of MMX usage only 'mant'
5393 contains the MMX register */
5394 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
5395 qemu_put_be64(f
, mant
);
5396 qemu_put_be16(f
, exp
);
5399 /* if we use doubles for float emulation, we save the doubles to
5400 avoid losing information in case of MMX usage. It can give
5401 problems if the image is restored on a CPU where long
5402 doubles are used instead. */
5403 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
5407 for(i
= 0; i
< 6; i
++)
5408 cpu_put_seg(f
, &env
->segs
[i
]);
5409 cpu_put_seg(f
, &env
->ldt
);
5410 cpu_put_seg(f
, &env
->tr
);
5411 cpu_put_seg(f
, &env
->gdt
);
5412 cpu_put_seg(f
, &env
->idt
);
5414 qemu_put_be32s(f
, &env
->sysenter_cs
);
5415 qemu_put_be32s(f
, &env
->sysenter_esp
);
5416 qemu_put_be32s(f
, &env
->sysenter_eip
);
5418 qemu_put_betls(f
, &env
->cr
[0]);
5419 qemu_put_betls(f
, &env
->cr
[2]);
5420 qemu_put_betls(f
, &env
->cr
[3]);
5421 qemu_put_betls(f
, &env
->cr
[4]);
5423 for(i
= 0; i
< 8; i
++)
5424 qemu_put_betls(f
, &env
->dr
[i
]);
5427 qemu_put_be32s(f
, &env
->a20_mask
);
5430 qemu_put_be32s(f
, &env
->mxcsr
);
5431 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5432 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5433 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5436 #ifdef TARGET_X86_64
5437 qemu_put_be64s(f
, &env
->efer
);
5438 qemu_put_be64s(f
, &env
->star
);
5439 qemu_put_be64s(f
, &env
->lstar
);
5440 qemu_put_be64s(f
, &env
->cstar
);
5441 qemu_put_be64s(f
, &env
->fmask
);
5442 qemu_put_be64s(f
, &env
->kernelgsbase
);
5444 qemu_put_be32s(f
, &env
->smbase
);
5447 #ifdef USE_X86LDOUBLE
5448 /* XXX: add that in a FPU generic layer */
5449 union x86_longdouble
{
5454 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
5455 #define EXPBIAS1 1023
5456 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
5457 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
5459 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
5463 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
5464 /* exponent + sign */
5465 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
5466 e
|= SIGND1(temp
) >> 16;
5471 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5473 CPUState
*env
= opaque
;
5476 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
5478 if (version_id
!= 3 && version_id
!= 4)
5480 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5481 qemu_get_betls(f
, &env
->regs
[i
]);
5482 qemu_get_betls(f
, &env
->eip
);
5483 qemu_get_betls(f
, &env
->eflags
);
5484 qemu_get_be32s(f
, &hflags
);
5486 qemu_get_be16s(f
, &fpuc
);
5487 qemu_get_be16s(f
, &fpus
);
5488 qemu_get_be16s(f
, &fptag
);
5489 qemu_get_be16s(f
, &fpregs_format
);
5491 /* NOTE: we cannot always restore the FPU state if the image come
5492 from a host with a different 'USE_X86LDOUBLE' define. We guess
5493 if we are in an MMX state to restore correctly in that case. */
5494 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
5495 for(i
= 0; i
< 8; i
++) {
5499 switch(fpregs_format
) {
5501 mant
= qemu_get_be64(f
);
5502 exp
= qemu_get_be16(f
);
5503 #ifdef USE_X86LDOUBLE
5504 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5506 /* difficult case */
5508 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5510 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5514 mant
= qemu_get_be64(f
);
5515 #ifdef USE_X86LDOUBLE
5517 union x86_longdouble
*p
;
5518 /* difficult case */
5519 p
= (void *)&env
->fpregs
[i
];
5524 fp64_to_fp80(p
, mant
);
5528 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5537 /* XXX: restore FPU round state */
5538 env
->fpstt
= (fpus
>> 11) & 7;
5539 env
->fpus
= fpus
& ~0x3800;
5541 for(i
= 0; i
< 8; i
++) {
5542 env
->fptags
[i
] = (fptag
>> i
) & 1;
5545 for(i
= 0; i
< 6; i
++)
5546 cpu_get_seg(f
, &env
->segs
[i
]);
5547 cpu_get_seg(f
, &env
->ldt
);
5548 cpu_get_seg(f
, &env
->tr
);
5549 cpu_get_seg(f
, &env
->gdt
);
5550 cpu_get_seg(f
, &env
->idt
);
5552 qemu_get_be32s(f
, &env
->sysenter_cs
);
5553 qemu_get_be32s(f
, &env
->sysenter_esp
);
5554 qemu_get_be32s(f
, &env
->sysenter_eip
);
5556 qemu_get_betls(f
, &env
->cr
[0]);
5557 qemu_get_betls(f
, &env
->cr
[2]);
5558 qemu_get_betls(f
, &env
->cr
[3]);
5559 qemu_get_betls(f
, &env
->cr
[4]);
5561 for(i
= 0; i
< 8; i
++)
5562 qemu_get_betls(f
, &env
->dr
[i
]);
5565 qemu_get_be32s(f
, &env
->a20_mask
);
5567 qemu_get_be32s(f
, &env
->mxcsr
);
5568 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5569 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5570 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5573 #ifdef TARGET_X86_64
5574 qemu_get_be64s(f
, &env
->efer
);
5575 qemu_get_be64s(f
, &env
->star
);
5576 qemu_get_be64s(f
, &env
->lstar
);
5577 qemu_get_be64s(f
, &env
->cstar
);
5578 qemu_get_be64s(f
, &env
->fmask
);
5579 qemu_get_be64s(f
, &env
->kernelgsbase
);
5581 if (version_id
>= 4)
5582 qemu_get_be32s(f
, &env
->smbase
);
5584 /* XXX: compute hflags from scratch, except for CPL and IIF */
5585 env
->hflags
= hflags
;
5590 #elif defined(TARGET_PPC)
5591 void cpu_save(QEMUFile
*f
, void *opaque
)
5595 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5600 #elif defined(TARGET_MIPS)
5601 void cpu_save(QEMUFile
*f
, void *opaque
)
5605 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5610 #elif defined(TARGET_SPARC)
5611 void cpu_save(QEMUFile
*f
, void *opaque
)
5613 CPUState
*env
= opaque
;
5617 for(i
= 0; i
< 8; i
++)
5618 qemu_put_betls(f
, &env
->gregs
[i
]);
5619 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5620 qemu_put_betls(f
, &env
->regbase
[i
]);
5623 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5629 qemu_put_be32(f
, u
.i
);
5632 qemu_put_betls(f
, &env
->pc
);
5633 qemu_put_betls(f
, &env
->npc
);
5634 qemu_put_betls(f
, &env
->y
);
5636 qemu_put_be32(f
, tmp
);
5637 qemu_put_betls(f
, &env
->fsr
);
5638 qemu_put_betls(f
, &env
->tbr
);
5639 #ifndef TARGET_SPARC64
5640 qemu_put_be32s(f
, &env
->wim
);
5642 for(i
= 0; i
< 16; i
++)
5643 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
5647 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5649 CPUState
*env
= opaque
;
5653 for(i
= 0; i
< 8; i
++)
5654 qemu_get_betls(f
, &env
->gregs
[i
]);
5655 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5656 qemu_get_betls(f
, &env
->regbase
[i
]);
5659 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5664 u
.i
= qemu_get_be32(f
);
5668 qemu_get_betls(f
, &env
->pc
);
5669 qemu_get_betls(f
, &env
->npc
);
5670 qemu_get_betls(f
, &env
->y
);
5671 tmp
= qemu_get_be32(f
);
5672 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
5673 correctly updated */
5675 qemu_get_betls(f
, &env
->fsr
);
5676 qemu_get_betls(f
, &env
->tbr
);
5677 #ifndef TARGET_SPARC64
5678 qemu_get_be32s(f
, &env
->wim
);
5680 for(i
= 0; i
< 16; i
++)
5681 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
5687 #elif defined(TARGET_ARM)
5689 void cpu_save(QEMUFile
*f
, void *opaque
)
5692 CPUARMState
*env
= (CPUARMState
*)opaque
;
5694 for (i
= 0; i
< 16; i
++) {
5695 qemu_put_be32(f
, env
->regs
[i
]);
5697 qemu_put_be32(f
, cpsr_read(env
));
5698 qemu_put_be32(f
, env
->spsr
);
5699 for (i
= 0; i
< 6; i
++) {
5700 qemu_put_be32(f
, env
->banked_spsr
[i
]);
5701 qemu_put_be32(f
, env
->banked_r13
[i
]);
5702 qemu_put_be32(f
, env
->banked_r14
[i
]);
5704 for (i
= 0; i
< 5; i
++) {
5705 qemu_put_be32(f
, env
->usr_regs
[i
]);
5706 qemu_put_be32(f
, env
->fiq_regs
[i
]);
5708 qemu_put_be32(f
, env
->cp15
.c0_cpuid
);
5709 qemu_put_be32(f
, env
->cp15
.c0_cachetype
);
5710 qemu_put_be32(f
, env
->cp15
.c1_sys
);
5711 qemu_put_be32(f
, env
->cp15
.c1_coproc
);
5712 qemu_put_be32(f
, env
->cp15
.c2_base
);
5713 qemu_put_be32(f
, env
->cp15
.c2_data
);
5714 qemu_put_be32(f
, env
->cp15
.c2_insn
);
5715 qemu_put_be32(f
, env
->cp15
.c3
);
5716 qemu_put_be32(f
, env
->cp15
.c5_insn
);
5717 qemu_put_be32(f
, env
->cp15
.c5_data
);
5718 for (i
= 0; i
< 8; i
++) {
5719 qemu_put_be32(f
, env
->cp15
.c6_region
[i
]);
5721 qemu_put_be32(f
, env
->cp15
.c6_insn
);
5722 qemu_put_be32(f
, env
->cp15
.c6_data
);
5723 qemu_put_be32(f
, env
->cp15
.c9_insn
);
5724 qemu_put_be32(f
, env
->cp15
.c9_data
);
5725 qemu_put_be32(f
, env
->cp15
.c13_fcse
);
5726 qemu_put_be32(f
, env
->cp15
.c13_context
);
5727 qemu_put_be32(f
, env
->cp15
.c15_cpar
);
5729 qemu_put_be32(f
, env
->features
);
5731 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
5732 for (i
= 0; i
< 16; i
++) {
5734 u
.d
= env
->vfp
.regs
[i
];
5735 qemu_put_be32(f
, u
.l
.upper
);
5736 qemu_put_be32(f
, u
.l
.lower
);
5738 for (i
= 0; i
< 16; i
++) {
5739 qemu_put_be32(f
, env
->vfp
.xregs
[i
]);
5742 /* TODO: Should use proper FPSCR access functions. */
5743 qemu_put_be32(f
, env
->vfp
.vec_len
);
5744 qemu_put_be32(f
, env
->vfp
.vec_stride
);
5747 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
5748 for (i
= 0; i
< 16; i
++) {
5749 qemu_put_be64(f
, env
->iwmmxt
.regs
[i
]);
5751 for (i
= 0; i
< 16; i
++) {
5752 qemu_put_be32(f
, env
->iwmmxt
.cregs
[i
]);
5757 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5759 CPUARMState
*env
= (CPUARMState
*)opaque
;
5762 if (version_id
!= 0)
5765 for (i
= 0; i
< 16; i
++) {
5766 env
->regs
[i
] = qemu_get_be32(f
);
5768 cpsr_write(env
, qemu_get_be32(f
), 0xffffffff);
5769 env
->spsr
= qemu_get_be32(f
);
5770 for (i
= 0; i
< 6; i
++) {
5771 env
->banked_spsr
[i
] = qemu_get_be32(f
);
5772 env
->banked_r13
[i
] = qemu_get_be32(f
);
5773 env
->banked_r14
[i
] = qemu_get_be32(f
);
5775 for (i
= 0; i
< 5; i
++) {
5776 env
->usr_regs
[i
] = qemu_get_be32(f
);
5777 env
->fiq_regs
[i
] = qemu_get_be32(f
);
5779 env
->cp15
.c0_cpuid
= qemu_get_be32(f
);
5780 env
->cp15
.c0_cachetype
= qemu_get_be32(f
);
5781 env
->cp15
.c1_sys
= qemu_get_be32(f
);
5782 env
->cp15
.c1_coproc
= qemu_get_be32(f
);
5783 env
->cp15
.c2_base
= qemu_get_be32(f
);
5784 env
->cp15
.c2_data
= qemu_get_be32(f
);
5785 env
->cp15
.c2_insn
= qemu_get_be32(f
);
5786 env
->cp15
.c3
= qemu_get_be32(f
);
5787 env
->cp15
.c5_insn
= qemu_get_be32(f
);
5788 env
->cp15
.c5_data
= qemu_get_be32(f
);
5789 for (i
= 0; i
< 8; i
++) {
5790 env
->cp15
.c6_region
[i
] = qemu_get_be32(f
);
5792 env
->cp15
.c6_insn
= qemu_get_be32(f
);
5793 env
->cp15
.c6_data
= qemu_get_be32(f
);
5794 env
->cp15
.c9_insn
= qemu_get_be32(f
);
5795 env
->cp15
.c9_data
= qemu_get_be32(f
);
5796 env
->cp15
.c13_fcse
= qemu_get_be32(f
);
5797 env
->cp15
.c13_context
= qemu_get_be32(f
);
5798 env
->cp15
.c15_cpar
= qemu_get_be32(f
);
5800 env
->features
= qemu_get_be32(f
);
5802 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
5803 for (i
= 0; i
< 16; i
++) {
5805 u
.l
.upper
= qemu_get_be32(f
);
5806 u
.l
.lower
= qemu_get_be32(f
);
5807 env
->vfp
.regs
[i
] = u
.d
;
5809 for (i
= 0; i
< 16; i
++) {
5810 env
->vfp
.xregs
[i
] = qemu_get_be32(f
);
5813 /* TODO: Should use proper FPSCR access functions. */
5814 env
->vfp
.vec_len
= qemu_get_be32(f
);
5815 env
->vfp
.vec_stride
= qemu_get_be32(f
);
5818 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
5819 for (i
= 0; i
< 16; i
++) {
5820 env
->iwmmxt
.regs
[i
] = qemu_get_be64(f
);
5822 for (i
= 0; i
< 16; i
++) {
5823 env
->iwmmxt
.cregs
[i
] = qemu_get_be32(f
);
5832 #warning No CPU save/restore functions
5836 /***********************************************************/
5837 /* ram save/restore */
5839 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
5843 v
= qemu_get_byte(f
);
5846 if (qemu_get_buffer(f
, buf
, len
) != len
)
5850 v
= qemu_get_byte(f
);
5851 memset(buf
, v
, len
);
5859 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
5863 if (qemu_get_be32(f
) != phys_ram_size
)
5865 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
5866 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
5873 #define BDRV_HASH_BLOCK_SIZE 1024
5874 #define IOBUF_SIZE 4096
5875 #define RAM_CBLOCK_MAGIC 0xfabe
5877 typedef struct RamCompressState
{
5880 uint8_t buf
[IOBUF_SIZE
];
5883 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
5886 memset(s
, 0, sizeof(*s
));
5888 ret
= deflateInit2(&s
->zstream
, 1,
5890 9, Z_DEFAULT_STRATEGY
);
5893 s
->zstream
.avail_out
= IOBUF_SIZE
;
5894 s
->zstream
.next_out
= s
->buf
;
5898 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
5900 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
5901 qemu_put_be16(s
->f
, len
);
5902 qemu_put_buffer(s
->f
, buf
, len
);
5905 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
5909 s
->zstream
.avail_in
= len
;
5910 s
->zstream
.next_in
= (uint8_t *)buf
;
5911 while (s
->zstream
.avail_in
> 0) {
5912 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
5915 if (s
->zstream
.avail_out
== 0) {
5916 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
5917 s
->zstream
.avail_out
= IOBUF_SIZE
;
5918 s
->zstream
.next_out
= s
->buf
;
5924 static void ram_compress_close(RamCompressState
*s
)
5928 /* compress last bytes */
5930 ret
= deflate(&s
->zstream
, Z_FINISH
);
5931 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
5932 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
5934 ram_put_cblock(s
, s
->buf
, len
);
5936 s
->zstream
.avail_out
= IOBUF_SIZE
;
5937 s
->zstream
.next_out
= s
->buf
;
5938 if (ret
== Z_STREAM_END
)
5945 deflateEnd(&s
->zstream
);
5948 typedef struct RamDecompressState
{
5951 uint8_t buf
[IOBUF_SIZE
];
5952 } RamDecompressState
;
5954 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
5957 memset(s
, 0, sizeof(*s
));
5959 ret
= inflateInit(&s
->zstream
);
5965 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
5969 s
->zstream
.avail_out
= len
;
5970 s
->zstream
.next_out
= buf
;
5971 while (s
->zstream
.avail_out
> 0) {
5972 if (s
->zstream
.avail_in
== 0) {
5973 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
5975 clen
= qemu_get_be16(s
->f
);
5976 if (clen
> IOBUF_SIZE
)
5978 qemu_get_buffer(s
->f
, s
->buf
, clen
);
5979 s
->zstream
.avail_in
= clen
;
5980 s
->zstream
.next_in
= s
->buf
;
5982 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
5983 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
5990 static void ram_decompress_close(RamDecompressState
*s
)
5992 inflateEnd(&s
->zstream
);
5995 static void ram_save(QEMUFile
*f
, void *opaque
)
5998 RamCompressState s1
, *s
= &s1
;
6001 qemu_put_be32(f
, phys_ram_size
);
6002 if (ram_compress_open(s
, f
) < 0)
6004 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6006 if (tight_savevm_enabled
) {
6010 /* find if the memory block is available on a virtual
6013 for(j
= 0; j
< MAX_DISKS
; j
++) {
6015 sector_num
= bdrv_hash_find(bs_table
[j
],
6016 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6017 if (sector_num
>= 0)
6022 goto normal_compress
;
6025 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
6026 ram_compress_buf(s
, buf
, 10);
6032 ram_compress_buf(s
, buf
, 1);
6033 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
6036 ram_compress_close(s
);
6039 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
6041 RamDecompressState s1
, *s
= &s1
;
6045 if (version_id
== 1)
6046 return ram_load_v1(f
, opaque
);
6047 if (version_id
!= 2)
6049 if (qemu_get_be32(f
) != phys_ram_size
)
6051 if (ram_decompress_open(s
, f
) < 0)
6053 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
6054 if (ram_decompress_buf(s
, buf
, 1) < 0) {
6055 fprintf(stderr
, "Error while reading ram block header\n");
6059 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
6060 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
6069 ram_decompress_buf(s
, buf
+ 1, 9);
6071 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
6072 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
6073 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
6076 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
6077 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
6078 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
6079 bs_index
, sector_num
);
6086 printf("Error block header\n");
6090 ram_decompress_close(s
);
6094 /***********************************************************/
6095 /* bottom halves (can be seen as timers which expire ASAP) */
6104 static QEMUBH
*first_bh
= NULL
;
6106 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
6109 bh
= qemu_mallocz(sizeof(QEMUBH
));
6113 bh
->opaque
= opaque
;
6117 int qemu_bh_poll(void)
6136 void qemu_bh_schedule(QEMUBH
*bh
)
6138 CPUState
*env
= cpu_single_env
;
6142 bh
->next
= first_bh
;
6145 /* stop the currently executing CPU to execute the BH ASAP */
6147 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
6151 void qemu_bh_cancel(QEMUBH
*bh
)
6154 if (bh
->scheduled
) {
6157 pbh
= &(*pbh
)->next
;
6163 void qemu_bh_delete(QEMUBH
*bh
)
6169 /***********************************************************/
6170 /* machine registration */
6172 QEMUMachine
*first_machine
= NULL
;
6174 int qemu_register_machine(QEMUMachine
*m
)
6177 pm
= &first_machine
;
6185 QEMUMachine
*find_machine(const char *name
)
6189 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6190 if (!strcmp(m
->name
, name
))
6196 /***********************************************************/
6197 /* main execution loop */
6199 void gui_update(void *opaque
)
6201 display_state
.dpy_refresh(&display_state
);
6202 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
6205 struct vm_change_state_entry
{
6206 VMChangeStateHandler
*cb
;
6208 LIST_ENTRY (vm_change_state_entry
) entries
;
6211 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
6213 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
6216 VMChangeStateEntry
*e
;
6218 e
= qemu_mallocz(sizeof (*e
));
6224 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
6228 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
6230 LIST_REMOVE (e
, entries
);
6234 static void vm_state_notify(int running
)
6236 VMChangeStateEntry
*e
;
6238 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
6239 e
->cb(e
->opaque
, running
);
6243 /* XXX: support several handlers */
6244 static VMStopHandler
*vm_stop_cb
;
6245 static void *vm_stop_opaque
;
6247 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6250 vm_stop_opaque
= opaque
;
6254 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
6268 void vm_stop(int reason
)
6271 cpu_disable_ticks();
6275 vm_stop_cb(vm_stop_opaque
, reason
);
6282 /* reset/shutdown handler */
6284 typedef struct QEMUResetEntry
{
6285 QEMUResetHandler
*func
;
6287 struct QEMUResetEntry
*next
;
6290 static QEMUResetEntry
*first_reset_entry
;
6291 static int reset_requested
;
6292 static int shutdown_requested
;
6293 static int powerdown_requested
;
6295 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
6297 QEMUResetEntry
**pre
, *re
;
6299 pre
= &first_reset_entry
;
6300 while (*pre
!= NULL
)
6301 pre
= &(*pre
)->next
;
6302 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
6304 re
->opaque
= opaque
;
6309 static void qemu_system_reset(void)
6313 /* reset all devices */
6314 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
6315 re
->func(re
->opaque
);
6319 void qemu_system_reset_request(void)
6322 shutdown_requested
= 1;
6324 reset_requested
= 1;
6327 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6330 void qemu_system_shutdown_request(void)
6332 shutdown_requested
= 1;
6334 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6337 void qemu_system_powerdown_request(void)
6339 powerdown_requested
= 1;
6341 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
6344 void main_loop_wait(int timeout
)
6346 IOHandlerRecord
*ioh
;
6347 fd_set rfds
, wfds
, xfds
;
6356 /* XXX: need to suppress polling by better using win32 events */
6358 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
6359 ret
|= pe
->func(pe
->opaque
);
6364 WaitObjects
*w
= &wait_objects
;
6366 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
6367 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
6368 if (w
->func
[ret
- WAIT_OBJECT_0
])
6369 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
6371 /* Check for additional signaled events */
6372 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
6374 /* Check if event is signaled */
6375 ret2
= WaitForSingleObject(w
->events
[i
], 0);
6376 if(ret2
== WAIT_OBJECT_0
) {
6378 w
->func
[i
](w
->opaque
[i
]);
6379 } else if (ret2
== WAIT_TIMEOUT
) {
6381 err
= GetLastError();
6382 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
6385 } else if (ret
== WAIT_TIMEOUT
) {
6387 err
= GetLastError();
6388 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
6392 /* poll any events */
6393 /* XXX: separate device handlers from system ones */
6398 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6402 (!ioh
->fd_read_poll
||
6403 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
6404 FD_SET(ioh
->fd
, &rfds
);
6408 if (ioh
->fd_write
) {
6409 FD_SET(ioh
->fd
, &wfds
);
6419 tv
.tv_usec
= timeout
* 1000;
6421 #if defined(CONFIG_SLIRP)
6423 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
6426 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
6428 IOHandlerRecord
**pioh
;
6430 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
6433 if (FD_ISSET(ioh
->fd
, &rfds
)) {
6434 ioh
->fd_read(ioh
->opaque
);
6436 if (FD_ISSET(ioh
->fd
, &wfds
)) {
6437 ioh
->fd_write(ioh
->opaque
);
6441 /* remove deleted IO handlers */
6442 pioh
= &first_io_handler
;
6452 #if defined(CONFIG_SLIRP)
6459 slirp_select_poll(&rfds
, &wfds
, &xfds
);
6465 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
6466 qemu_get_clock(vm_clock
));
6467 /* run dma transfers, if any */
6471 /* real time timers */
6472 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
6473 qemu_get_clock(rt_clock
));
6475 /* Check bottom-halves last in case any of the earlier events triggered
6481 static CPUState
*cur_cpu
;
6486 #ifdef CONFIG_PROFILER
6491 cur_cpu
= first_cpu
;
6498 env
= env
->next_cpu
;
6501 #ifdef CONFIG_PROFILER
6502 ti
= profile_getclock();
6504 ret
= cpu_exec(env
);
6505 #ifdef CONFIG_PROFILER
6506 qemu_time
+= profile_getclock() - ti
;
6508 if (ret
== EXCP_HLT
) {
6509 /* Give the next CPU a chance to run. */
6513 if (ret
!= EXCP_HALTED
)
6515 /* all CPUs are halted ? */
6521 if (shutdown_requested
) {
6522 ret
= EXCP_INTERRUPT
;
6525 if (reset_requested
) {
6526 reset_requested
= 0;
6527 qemu_system_reset();
6528 ret
= EXCP_INTERRUPT
;
6530 if (powerdown_requested
) {
6531 powerdown_requested
= 0;
6532 qemu_system_powerdown();
6533 ret
= EXCP_INTERRUPT
;
6535 if (ret
== EXCP_DEBUG
) {
6536 vm_stop(EXCP_DEBUG
);
6538 /* If all cpus are halted then wait until the next IRQ */
6539 /* XXX: use timeout computed from timers */
6540 if (ret
== EXCP_HALTED
)
6547 #ifdef CONFIG_PROFILER
6548 ti
= profile_getclock();
6550 main_loop_wait(timeout
);
6551 #ifdef CONFIG_PROFILER
6552 dev_time
+= profile_getclock() - ti
;
6555 cpu_disable_ticks();
6561 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2007 Fabrice Bellard\n"
6562 "usage: %s [options] [disk_image]\n"
6564 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
6566 "Standard options:\n"
6567 "-M machine select emulated machine (-M ? for list)\n"
6568 "-cpu cpu select CPU (-cpu ? for list)\n"
6569 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
6570 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
6571 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
6572 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
6573 "-mtdblock file use 'file' as on-board Flash memory image\n"
6574 "-sd file use 'file' as SecureDigital card image\n"
6575 "-pflash file use 'file' as a parallel flash image\n"
6576 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
6577 "-snapshot write to temporary files instead of disk image files\n"
6579 "-no-frame open SDL window without a frame and window decorations\n"
6580 "-no-quit disable SDL window close capability\n"
6583 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
6585 "-m megs set virtual RAM size to megs MB [default=%d]\n"
6586 "-smp n set the number of CPUs to 'n' [default=1]\n"
6587 "-nographic disable graphical output and redirect serial I/Os to console\n"
6588 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
6590 "-k language use keyboard layout (for example \"fr\" for French)\n"
6593 "-audio-help print list of audio drivers and their options\n"
6594 "-soundhw c1,... enable audio support\n"
6595 " and only specified sound cards (comma separated list)\n"
6596 " use -soundhw ? to get the list of supported cards\n"
6597 " use -soundhw all to enable all of them\n"
6599 "-localtime set the real time clock to local time [default=utc]\n"
6600 "-full-screen start in full screen\n"
6602 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
6604 "-usb enable the USB driver (will be the default soon)\n"
6605 "-usbdevice name add the host or guest USB device 'name'\n"
6606 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6607 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
6609 "-name string set the name of the guest\n"
6611 "Network options:\n"
6612 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
6613 " create a new Network Interface Card and connect it to VLAN 'n'\n"
6615 "-net user[,vlan=n][,hostname=host]\n"
6616 " connect the user mode network stack to VLAN 'n' and send\n"
6617 " hostname 'host' to DHCP clients\n"
6620 "-net tap[,vlan=n],ifname=name\n"
6621 " connect the host TAP network interface to VLAN 'n'\n"
6623 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
6624 " connect the host TAP network interface to VLAN 'n' and use\n"
6625 " the network script 'file' (default=%s);\n"
6626 " use 'script=no' to disable script execution;\n"
6627 " use 'fd=h' to connect to an already opened TAP interface\n"
6629 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
6630 " connect the vlan 'n' to another VLAN using a socket connection\n"
6631 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
6632 " connect the vlan 'n' to multicast maddr and port\n"
6633 "-net none use it alone to have zero network devices; if no -net option\n"
6634 " is provided, the default is '-net nic -net user'\n"
6637 "-tftp dir allow tftp access to files in dir [-net user]\n"
6638 "-bootp file advertise file in BOOTP replies\n"
6640 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
6642 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
6643 " redirect TCP or UDP connections from host to guest [-net user]\n"
6646 "Linux boot specific:\n"
6647 "-kernel bzImage use 'bzImage' as kernel image\n"
6648 "-append cmdline use 'cmdline' as kernel command line\n"
6649 "-initrd file use 'file' as initial ram disk\n"
6651 "Debug/Expert options:\n"
6652 "-monitor dev redirect the monitor to char device 'dev'\n"
6653 "-serial dev redirect the serial port to char device 'dev'\n"
6654 "-parallel dev redirect the parallel port to char device 'dev'\n"
6655 "-pidfile file Write PID to 'file'\n"
6656 "-S freeze CPU at startup (use 'c' to start execution)\n"
6657 "-s wait gdb connection to port\n"
6658 "-p port set gdb connection port [default=%s]\n"
6659 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
6660 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
6661 " translation (t=none or lba) (usually qemu can guess them)\n"
6662 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
6664 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
6665 "-no-kqemu disable KQEMU kernel module usage\n"
6667 #ifdef USE_CODE_COPY
6668 "-no-code-copy disable code copy acceleration\n"
6671 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
6672 " (default is CL-GD5446 PCI VGA)\n"
6673 "-no-acpi disable ACPI\n"
6675 "-no-reboot exit instead of rebooting\n"
6676 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
6677 "-vnc display start a VNC server on display\n"
6679 "-daemonize daemonize QEMU after initializing\n"
6681 "-option-rom rom load a file, rom, into the option ROM space\n"
6683 "-prom-env variable=value set OpenBIOS nvram variables\n"
6686 "During emulation, the following keys are useful:\n"
6687 "ctrl-alt-f toggle full screen\n"
6688 "ctrl-alt-n switch to virtual console 'n'\n"
6689 "ctrl-alt toggle mouse and keyboard grab\n"
6691 "When using -nographic, press 'ctrl-a h' to get some help.\n"
6696 DEFAULT_NETWORK_SCRIPT
,
6698 DEFAULT_GDBSTUB_PORT
,
6703 #define HAS_ARG 0x0001
6717 QEMU_OPTION_mtdblock
,
6721 QEMU_OPTION_snapshot
,
6723 QEMU_OPTION_no_fd_bootchk
,
6726 QEMU_OPTION_nographic
,
6727 QEMU_OPTION_portrait
,
6729 QEMU_OPTION_audio_help
,
6730 QEMU_OPTION_soundhw
,
6749 QEMU_OPTION_no_code_copy
,
6751 QEMU_OPTION_localtime
,
6752 QEMU_OPTION_cirrusvga
,
6755 QEMU_OPTION_std_vga
,
6757 QEMU_OPTION_monitor
,
6759 QEMU_OPTION_parallel
,
6761 QEMU_OPTION_full_screen
,
6762 QEMU_OPTION_no_frame
,
6763 QEMU_OPTION_no_quit
,
6764 QEMU_OPTION_pidfile
,
6765 QEMU_OPTION_no_kqemu
,
6766 QEMU_OPTION_kernel_kqemu
,
6767 QEMU_OPTION_win2k_hack
,
6769 QEMU_OPTION_usbdevice
,
6772 QEMU_OPTION_no_acpi
,
6773 QEMU_OPTION_no_reboot
,
6774 QEMU_OPTION_show_cursor
,
6775 QEMU_OPTION_daemonize
,
6776 QEMU_OPTION_option_rom
,
6777 QEMU_OPTION_semihosting
,
6779 QEMU_OPTION_prom_env
,
6782 typedef struct QEMUOption
{
6788 const QEMUOption qemu_options
[] = {
6789 { "h", 0, QEMU_OPTION_h
},
6790 { "help", 0, QEMU_OPTION_h
},
6792 { "M", HAS_ARG
, QEMU_OPTION_M
},
6793 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
6794 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
6795 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
6796 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
6797 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
6798 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
6799 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
6800 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
6801 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
6802 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
6803 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
6804 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
6805 { "snapshot", 0, QEMU_OPTION_snapshot
},
6807 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
6809 { "m", HAS_ARG
, QEMU_OPTION_m
},
6810 { "nographic", 0, QEMU_OPTION_nographic
},
6811 { "portrait", 0, QEMU_OPTION_portrait
},
6812 { "k", HAS_ARG
, QEMU_OPTION_k
},
6814 { "audio-help", 0, QEMU_OPTION_audio_help
},
6815 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
6818 { "net", HAS_ARG
, QEMU_OPTION_net
},
6820 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
6821 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
6823 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
6825 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
6828 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
6829 { "append", HAS_ARG
, QEMU_OPTION_append
},
6830 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
6832 { "S", 0, QEMU_OPTION_S
},
6833 { "s", 0, QEMU_OPTION_s
},
6834 { "p", HAS_ARG
, QEMU_OPTION_p
},
6835 { "d", HAS_ARG
, QEMU_OPTION_d
},
6836 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
6837 { "L", HAS_ARG
, QEMU_OPTION_L
},
6838 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
6840 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
6841 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
6843 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6844 { "g", 1, QEMU_OPTION_g
},
6846 { "localtime", 0, QEMU_OPTION_localtime
},
6847 { "std-vga", 0, QEMU_OPTION_std_vga
},
6848 { "echr", 1, QEMU_OPTION_echr
},
6849 { "monitor", 1, QEMU_OPTION_monitor
},
6850 { "serial", 1, QEMU_OPTION_serial
},
6851 { "parallel", 1, QEMU_OPTION_parallel
},
6852 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
6853 { "full-screen", 0, QEMU_OPTION_full_screen
},
6855 { "no-frame", 0, QEMU_OPTION_no_frame
},
6856 { "no-quit", 0, QEMU_OPTION_no_quit
},
6858 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
6859 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
6860 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
6861 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
6862 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
6864 /* temporary options */
6865 { "usb", 0, QEMU_OPTION_usb
},
6866 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
6867 { "vmwarevga", 0, QEMU_OPTION_vmsvga
},
6868 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
6869 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
6870 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
6871 { "daemonize", 0, QEMU_OPTION_daemonize
},
6872 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
6873 #if defined(TARGET_ARM) || defined(TARGET_M68K)
6874 { "semihosting", 0, QEMU_OPTION_semihosting
},
6876 { "name", HAS_ARG
, QEMU_OPTION_name
},
6877 #if defined(TARGET_SPARC)
6878 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
6883 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
6885 /* this stack is only used during signal handling */
6886 #define SIGNAL_STACK_SIZE 32768
6888 static uint8_t *signal_stack
;
6892 /* password input */
6894 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
6899 if (!bdrv_is_encrypted(bs
))
6902 term_printf("%s is encrypted.\n", name
);
6903 for(i
= 0; i
< 3; i
++) {
6904 monitor_readline("Password: ", 1, password
, sizeof(password
));
6905 if (bdrv_set_key(bs
, password
) == 0)
6907 term_printf("invalid password\n");
6912 static BlockDriverState
*get_bdrv(int index
)
6914 BlockDriverState
*bs
;
6917 bs
= bs_table
[index
];
6918 } else if (index
< 6) {
6919 bs
= fd_table
[index
- 4];
6926 static void read_passwords(void)
6928 BlockDriverState
*bs
;
6931 for(i
= 0; i
< 6; i
++) {
6934 qemu_key_check(bs
, bdrv_get_device_name(bs
));
6938 /* XXX: currently we cannot use simultaneously different CPUs */
6939 void register_machines(void)
6941 #if defined(TARGET_I386)
6942 qemu_register_machine(&pc_machine
);
6943 qemu_register_machine(&isapc_machine
);
6944 #elif defined(TARGET_PPC)
6945 qemu_register_machine(&heathrow_machine
);
6946 qemu_register_machine(&core99_machine
);
6947 qemu_register_machine(&prep_machine
);
6948 qemu_register_machine(&ref405ep_machine
);
6949 qemu_register_machine(&taihu_machine
);
6950 #elif defined(TARGET_MIPS)
6951 qemu_register_machine(&mips_machine
);
6952 qemu_register_machine(&mips_malta_machine
);
6953 qemu_register_machine(&mips_pica61_machine
);
6954 #elif defined(TARGET_SPARC)
6955 #ifdef TARGET_SPARC64
6956 qemu_register_machine(&sun4u_machine
);
6958 qemu_register_machine(&ss5_machine
);
6959 qemu_register_machine(&ss10_machine
);
6961 #elif defined(TARGET_ARM)
6962 qemu_register_machine(&integratorcp_machine
);
6963 qemu_register_machine(&versatilepb_machine
);
6964 qemu_register_machine(&versatileab_machine
);
6965 qemu_register_machine(&realview_machine
);
6966 qemu_register_machine(&akitapda_machine
);
6967 qemu_register_machine(&spitzpda_machine
);
6968 qemu_register_machine(&borzoipda_machine
);
6969 qemu_register_machine(&terrierpda_machine
);
6970 #elif defined(TARGET_SH4)
6971 qemu_register_machine(&shix_machine
);
6972 #elif defined(TARGET_ALPHA)
6974 #elif defined(TARGET_M68K)
6975 qemu_register_machine(&an5206_machine
);
6977 #error unsupported CPU
6982 struct soundhw soundhw
[] = {
6983 #ifdef HAS_AUDIO_CHOICE
6990 { .init_isa
= pcspk_audio_init
}
6995 "Creative Sound Blaster 16",
6998 { .init_isa
= SB16_init
}
7005 "Yamaha YMF262 (OPL3)",
7007 "Yamaha YM3812 (OPL2)",
7011 { .init_isa
= Adlib_init
}
7018 "Gravis Ultrasound GF1",
7021 { .init_isa
= GUS_init
}
7027 "ENSONIQ AudioPCI ES1370",
7030 { .init_pci
= es1370_init
}
7034 { NULL
, NULL
, 0, 0, { NULL
} }
7037 static void select_soundhw (const char *optarg
)
7041 if (*optarg
== '?') {
7044 printf ("Valid sound card names (comma separated):\n");
7045 for (c
= soundhw
; c
->name
; ++c
) {
7046 printf ("%-11s %s\n", c
->name
, c
->descr
);
7048 printf ("\n-soundhw all will enable all of the above\n");
7049 exit (*optarg
!= '?');
7057 if (!strcmp (optarg
, "all")) {
7058 for (c
= soundhw
; c
->name
; ++c
) {
7066 e
= strchr (p
, ',');
7067 l
= !e
? strlen (p
) : (size_t) (e
- p
);
7069 for (c
= soundhw
; c
->name
; ++c
) {
7070 if (!strncmp (c
->name
, p
, l
)) {
7079 "Unknown sound card name (too big to show)\n");
7082 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
7087 p
+= l
+ (e
!= NULL
);
7091 goto show_valid_cards
;
7097 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
7099 exit(STATUS_CONTROL_C_EXIT
);
7104 #define MAX_NET_CLIENTS 32
7106 int main(int argc
, char **argv
)
7108 #ifdef CONFIG_GDBSTUB
7110 const char *gdbstub_port
;
7112 int i
, cdrom_index
, pflash_index
;
7113 int snapshot
, linux_boot
;
7114 const char *initrd_filename
;
7115 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
7116 const char *pflash_filename
[MAX_PFLASH
];
7117 const char *sd_filename
;
7118 const char *mtd_filename
;
7119 const char *kernel_filename
, *kernel_cmdline
;
7120 DisplayState
*ds
= &display_state
;
7121 int cyls
, heads
, secs
, translation
;
7122 char net_clients
[MAX_NET_CLIENTS
][256];
7125 const char *r
, *optarg
;
7126 CharDriverState
*monitor_hd
;
7127 char monitor_device
[128];
7128 char serial_devices
[MAX_SERIAL_PORTS
][128];
7129 int serial_device_index
;
7130 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
7131 int parallel_device_index
;
7132 const char *loadvm
= NULL
;
7133 QEMUMachine
*machine
;
7134 const char *cpu_model
;
7135 char usb_devices
[MAX_USB_CMDLINE
][128];
7136 int usb_devices_index
;
7138 const char *pid_file
= NULL
;
7141 LIST_INIT (&vm_change_state_head
);
7144 struct sigaction act
;
7145 sigfillset(&act
.sa_mask
);
7147 act
.sa_handler
= SIG_IGN
;
7148 sigaction(SIGPIPE
, &act
, NULL
);
7151 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
7152 /* Note: cpu_interrupt() is currently not SMP safe, so we force
7153 QEMU to run on a single CPU */
7158 h
= GetCurrentProcess();
7159 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
7160 for(i
= 0; i
< 32; i
++) {
7161 if (mask
& (1 << i
))
7166 SetProcessAffinityMask(h
, mask
);
7172 register_machines();
7173 machine
= first_machine
;
7175 initrd_filename
= NULL
;
7176 for(i
= 0; i
< MAX_FD
; i
++)
7177 fd_filename
[i
] = NULL
;
7178 for(i
= 0; i
< MAX_DISKS
; i
++)
7179 hd_filename
[i
] = NULL
;
7180 for(i
= 0; i
< MAX_PFLASH
; i
++)
7181 pflash_filename
[i
] = NULL
;
7184 mtd_filename
= NULL
;
7185 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
7186 vga_ram_size
= VGA_RAM_SIZE
;
7187 #ifdef CONFIG_GDBSTUB
7189 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
7193 kernel_filename
= NULL
;
7194 kernel_cmdline
= "";
7200 cyls
= heads
= secs
= 0;
7201 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7202 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
7204 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
7205 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
7206 serial_devices
[i
][0] = '\0';
7207 serial_device_index
= 0;
7209 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
7210 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
7211 parallel_devices
[i
][0] = '\0';
7212 parallel_device_index
= 0;
7214 usb_devices_index
= 0;
7219 /* default mac address of the first network interface */
7227 hd_filename
[0] = argv
[optind
++];
7229 const QEMUOption
*popt
;
7232 /* Treat --foo the same as -foo. */
7235 popt
= qemu_options
;
7238 fprintf(stderr
, "%s: invalid option -- '%s'\n",
7242 if (!strcmp(popt
->name
, r
+ 1))
7246 if (popt
->flags
& HAS_ARG
) {
7247 if (optind
>= argc
) {
7248 fprintf(stderr
, "%s: option '%s' requires an argument\n",
7252 optarg
= argv
[optind
++];
7257 switch(popt
->index
) {
7259 machine
= find_machine(optarg
);
7262 printf("Supported machines are:\n");
7263 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
7264 printf("%-10s %s%s\n",
7266 m
== first_machine
? " (default)" : "");
7271 case QEMU_OPTION_cpu
:
7272 /* hw initialization will check this */
7273 if (optarg
[0] == '?') {
7274 #if defined(TARGET_PPC)
7275 ppc_cpu_list(stdout
, &fprintf
);
7276 #elif defined(TARGET_ARM)
7278 #elif defined(TARGET_MIPS)
7279 mips_cpu_list(stdout
, &fprintf
);
7280 #elif defined(TARGET_SPARC)
7281 sparc_cpu_list(stdout
, &fprintf
);
7288 case QEMU_OPTION_initrd
:
7289 initrd_filename
= optarg
;
7291 case QEMU_OPTION_hda
:
7292 case QEMU_OPTION_hdb
:
7293 case QEMU_OPTION_hdc
:
7294 case QEMU_OPTION_hdd
:
7297 hd_index
= popt
->index
- QEMU_OPTION_hda
;
7298 hd_filename
[hd_index
] = optarg
;
7299 if (hd_index
== cdrom_index
)
7303 case QEMU_OPTION_mtdblock
:
7304 mtd_filename
= optarg
;
7306 case QEMU_OPTION_sd
:
7307 sd_filename
= optarg
;
7309 case QEMU_OPTION_pflash
:
7310 if (pflash_index
>= MAX_PFLASH
) {
7311 fprintf(stderr
, "qemu: too many parallel flash images\n");
7314 pflash_filename
[pflash_index
++] = optarg
;
7316 case QEMU_OPTION_snapshot
:
7319 case QEMU_OPTION_hdachs
:
7323 cyls
= strtol(p
, (char **)&p
, 0);
7324 if (cyls
< 1 || cyls
> 16383)
7329 heads
= strtol(p
, (char **)&p
, 0);
7330 if (heads
< 1 || heads
> 16)
7335 secs
= strtol(p
, (char **)&p
, 0);
7336 if (secs
< 1 || secs
> 63)
7340 if (!strcmp(p
, "none"))
7341 translation
= BIOS_ATA_TRANSLATION_NONE
;
7342 else if (!strcmp(p
, "lba"))
7343 translation
= BIOS_ATA_TRANSLATION_LBA
;
7344 else if (!strcmp(p
, "auto"))
7345 translation
= BIOS_ATA_TRANSLATION_AUTO
;
7348 } else if (*p
!= '\0') {
7350 fprintf(stderr
, "qemu: invalid physical CHS format\n");
7355 case QEMU_OPTION_nographic
:
7356 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
7357 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "null");
7358 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
7361 case QEMU_OPTION_portrait
:
7364 case QEMU_OPTION_kernel
:
7365 kernel_filename
= optarg
;
7367 case QEMU_OPTION_append
:
7368 kernel_cmdline
= optarg
;
7370 case QEMU_OPTION_cdrom
:
7371 if (cdrom_index
>= 0) {
7372 hd_filename
[cdrom_index
] = optarg
;
7375 case QEMU_OPTION_boot
:
7376 boot_device
= optarg
[0];
7377 if (boot_device
!= 'a' &&
7378 #if defined(TARGET_SPARC) || defined(TARGET_I386)
7380 boot_device
!= 'n' &&
7382 boot_device
!= 'c' && boot_device
!= 'd') {
7383 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
7387 case QEMU_OPTION_fda
:
7388 fd_filename
[0] = optarg
;
7390 case QEMU_OPTION_fdb
:
7391 fd_filename
[1] = optarg
;
7394 case QEMU_OPTION_no_fd_bootchk
:
7398 case QEMU_OPTION_no_code_copy
:
7399 code_copy_enabled
= 0;
7401 case QEMU_OPTION_net
:
7402 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
7403 fprintf(stderr
, "qemu: too many network clients\n");
7406 pstrcpy(net_clients
[nb_net_clients
],
7407 sizeof(net_clients
[0]),
7412 case QEMU_OPTION_tftp
:
7413 tftp_prefix
= optarg
;
7415 case QEMU_OPTION_bootp
:
7416 bootp_filename
= optarg
;
7419 case QEMU_OPTION_smb
:
7420 net_slirp_smb(optarg
);
7423 case QEMU_OPTION_redir
:
7424 net_slirp_redir(optarg
);
7428 case QEMU_OPTION_audio_help
:
7432 case QEMU_OPTION_soundhw
:
7433 select_soundhw (optarg
);
7440 ram_size
= atoi(optarg
) * 1024 * 1024;
7443 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
7444 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
7445 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
7454 mask
= cpu_str_to_log_mask(optarg
);
7456 printf("Log items (comma separated):\n");
7457 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
7458 printf("%-10s %s\n", item
->name
, item
->help
);
7465 #ifdef CONFIG_GDBSTUB
7470 gdbstub_port
= optarg
;
7480 keyboard_layout
= optarg
;
7482 case QEMU_OPTION_localtime
:
7485 case QEMU_OPTION_cirrusvga
:
7486 cirrus_vga_enabled
= 1;
7489 case QEMU_OPTION_vmsvga
:
7490 cirrus_vga_enabled
= 0;
7493 case QEMU_OPTION_std_vga
:
7494 cirrus_vga_enabled
= 0;
7502 w
= strtol(p
, (char **)&p
, 10);
7505 fprintf(stderr
, "qemu: invalid resolution or depth\n");
7511 h
= strtol(p
, (char **)&p
, 10);
7516 depth
= strtol(p
, (char **)&p
, 10);
7517 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
7518 depth
!= 24 && depth
!= 32)
7520 } else if (*p
== '\0') {
7521 depth
= graphic_depth
;
7528 graphic_depth
= depth
;
7531 case QEMU_OPTION_echr
:
7534 term_escape_char
= strtol(optarg
, &r
, 0);
7536 printf("Bad argument to echr\n");
7539 case QEMU_OPTION_monitor
:
7540 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
7542 case QEMU_OPTION_serial
:
7543 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
7544 fprintf(stderr
, "qemu: too many serial ports\n");
7547 pstrcpy(serial_devices
[serial_device_index
],
7548 sizeof(serial_devices
[0]), optarg
);
7549 serial_device_index
++;
7551 case QEMU_OPTION_parallel
:
7552 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
7553 fprintf(stderr
, "qemu: too many parallel ports\n");
7556 pstrcpy(parallel_devices
[parallel_device_index
],
7557 sizeof(parallel_devices
[0]), optarg
);
7558 parallel_device_index
++;
7560 case QEMU_OPTION_loadvm
:
7563 case QEMU_OPTION_full_screen
:
7567 case QEMU_OPTION_no_frame
:
7570 case QEMU_OPTION_no_quit
:
7574 case QEMU_OPTION_pidfile
:
7578 case QEMU_OPTION_win2k_hack
:
7579 win2k_install_hack
= 1;
7583 case QEMU_OPTION_no_kqemu
:
7586 case QEMU_OPTION_kernel_kqemu
:
7590 case QEMU_OPTION_usb
:
7593 case QEMU_OPTION_usbdevice
:
7595 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
7596 fprintf(stderr
, "Too many USB devices\n");
7599 pstrcpy(usb_devices
[usb_devices_index
],
7600 sizeof(usb_devices
[usb_devices_index
]),
7602 usb_devices_index
++;
7604 case QEMU_OPTION_smp
:
7605 smp_cpus
= atoi(optarg
);
7606 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
7607 fprintf(stderr
, "Invalid number of CPUs\n");
7611 case QEMU_OPTION_vnc
:
7612 vnc_display
= optarg
;
7614 case QEMU_OPTION_no_acpi
:
7617 case QEMU_OPTION_no_reboot
:
7620 case QEMU_OPTION_show_cursor
:
7623 case QEMU_OPTION_daemonize
:
7626 case QEMU_OPTION_option_rom
:
7627 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
7628 fprintf(stderr
, "Too many option ROMs\n");
7631 option_rom
[nb_option_roms
] = optarg
;
7634 case QEMU_OPTION_semihosting
:
7635 semihosting_enabled
= 1;
7637 case QEMU_OPTION_name
:
7641 case QEMU_OPTION_prom_env
:
7642 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
7643 fprintf(stderr
, "Too many prom variables\n");
7646 prom_envs
[nb_prom_envs
] = optarg
;
7655 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
7656 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
7663 if (pipe(fds
) == -1)
7674 len
= read(fds
[0], &status
, 1);
7675 if (len
== -1 && (errno
== EINTR
))
7680 else if (status
== 1) {
7681 fprintf(stderr
, "Could not acquire pidfile\n");
7699 signal(SIGTSTP
, SIG_IGN
);
7700 signal(SIGTTOU
, SIG_IGN
);
7701 signal(SIGTTIN
, SIG_IGN
);
7705 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
7708 write(fds
[1], &status
, 1);
7710 fprintf(stderr
, "Could not acquire pid file\n");
7718 linux_boot
= (kernel_filename
!= NULL
);
7721 boot_device
!= 'n' &&
7722 hd_filename
[0] == '\0' &&
7723 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
7724 fd_filename
[0] == '\0')
7727 /* boot to floppy or the default cd if no hard disk defined yet */
7728 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
7729 if (fd_filename
[0] != '\0')
7735 setvbuf(stdout
, NULL
, _IOLBF
, 0);
7745 /* init network clients */
7746 if (nb_net_clients
== 0) {
7747 /* if no clients, we use a default config */
7748 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
7750 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
7755 for(i
= 0;i
< nb_net_clients
; i
++) {
7756 if (net_client_init(net_clients
[i
]) < 0)
7759 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
7760 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
7762 if (vlan
->nb_guest_devs
== 0) {
7763 fprintf(stderr
, "Invalid vlan (%d) with no nics\n", vlan
->id
);
7766 if (vlan
->nb_host_devs
== 0)
7768 "Warning: vlan %d is not connected to host network\n",
7773 if (boot_device
== 'n') {
7774 for (i
= 0; i
< nb_nics
; i
++) {
7775 const char *model
= nd_table
[i
].model
;
7779 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
7780 if (get_image_size(buf
) > 0) {
7781 option_rom
[nb_option_roms
] = strdup(buf
);
7787 fprintf(stderr
, "No valid PXE rom found for network device\n");
7790 boot_device
= 'c'; /* to prevent confusion by the BIOS */
7794 /* init the memory */
7795 phys_ram_size
= ram_size
+ vga_ram_size
+ MAX_BIOS_SIZE
;
7797 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
7798 if (!phys_ram_base
) {
7799 fprintf(stderr
, "Could not allocate physical memory\n");
7803 /* we always create the cdrom drive, even if no disk is there */
7805 if (cdrom_index
>= 0) {
7806 bs_table
[cdrom_index
] = bdrv_new("cdrom");
7807 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
7810 /* open the virtual block devices */
7811 for(i
= 0; i
< MAX_DISKS
; i
++) {
7812 if (hd_filename
[i
]) {
7815 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
7816 bs_table
[i
] = bdrv_new(buf
);
7818 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7819 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
7823 if (i
== 0 && cyls
!= 0) {
7824 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
7825 bdrv_set_translation_hint(bs_table
[i
], translation
);
7830 /* we always create at least one floppy disk */
7831 fd_table
[0] = bdrv_new("fda");
7832 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
7834 for(i
= 0; i
< MAX_FD
; i
++) {
7835 if (fd_filename
[i
]) {
7838 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
7839 fd_table
[i
] = bdrv_new(buf
);
7840 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
7842 if (fd_filename
[i
][0] != '\0') {
7843 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
7844 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7845 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
7853 /* Open the virtual parallel flash block devices */
7854 for(i
= 0; i
< MAX_PFLASH
; i
++) {
7855 if (pflash_filename
[i
]) {
7856 if (!pflash_table
[i
]) {
7858 snprintf(buf
, sizeof(buf
), "fl%c", i
+ 'a');
7859 pflash_table
[i
] = bdrv_new(buf
);
7861 if (bdrv_open(pflash_table
[i
], pflash_filename
[i
],
7862 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7863 fprintf(stderr
, "qemu: could not open flash image '%s'\n",
7864 pflash_filename
[i
]);
7870 sd_bdrv
= bdrv_new ("sd");
7871 /* FIXME: This isn't really a floppy, but it's a reasonable
7873 bdrv_set_type_hint(sd_bdrv
, BDRV_TYPE_FLOPPY
);
7875 if (bdrv_open(sd_bdrv
, sd_filename
,
7876 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7877 fprintf(stderr
, "qemu: could not open SD card image %s\n",
7880 qemu_key_check(sd_bdrv
, sd_filename
);
7884 mtd_bdrv
= bdrv_new ("mtd");
7885 if (bdrv_open(mtd_bdrv
, mtd_filename
,
7886 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0 ||
7887 qemu_key_check(mtd_bdrv
, mtd_filename
)) {
7888 fprintf(stderr
, "qemu: could not open Flash image %s\n",
7890 bdrv_delete(mtd_bdrv
);
7895 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
7896 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
7902 dumb_display_init(ds
);
7903 } else if (vnc_display
!= NULL
) {
7904 vnc_display_init(ds
, vnc_display
);
7906 #if defined(CONFIG_SDL)
7907 sdl_display_init(ds
, full_screen
, no_frame
);
7908 #elif defined(CONFIG_COCOA)
7909 cocoa_display_init(ds
, full_screen
);
7911 dumb_display_init(ds
);
7915 /* Maintain compatibility with multiple stdio monitors */
7916 if (!strcmp(monitor_device
,"stdio")) {
7917 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7918 if (!strcmp(serial_devices
[i
],"mon:stdio")) {
7919 monitor_device
[0] = '\0';
7921 } else if (!strcmp(serial_devices
[i
],"stdio")) {
7922 monitor_device
[0] = '\0';
7923 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "mon:stdio");
7928 if (monitor_device
[0] != '\0') {
7929 monitor_hd
= qemu_chr_open(monitor_device
);
7931 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
7934 monitor_init(monitor_hd
, !nographic
);
7937 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7938 const char *devname
= serial_devices
[i
];
7939 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7940 serial_hds
[i
] = qemu_chr_open(devname
);
7941 if (!serial_hds
[i
]) {
7942 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
7946 if (!strcmp(devname
, "vc"))
7947 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
7951 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
7952 const char *devname
= parallel_devices
[i
];
7953 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7954 parallel_hds
[i
] = qemu_chr_open(devname
);
7955 if (!parallel_hds
[i
]) {
7956 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
7960 if (!strcmp(devname
, "vc"))
7961 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
7965 machine
->init(ram_size
, vga_ram_size
, boot_device
,
7966 ds
, fd_filename
, snapshot
,
7967 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
7969 /* init USB devices */
7971 for(i
= 0; i
< usb_devices_index
; i
++) {
7972 if (usb_device_add(usb_devices
[i
]) < 0) {
7973 fprintf(stderr
, "Warning: could not add USB device %s\n",
7979 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
7980 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
7982 #ifdef CONFIG_GDBSTUB
7984 /* XXX: use standard host:port notation and modify options
7986 if (gdbserver_start(gdbstub_port
) < 0) {
7987 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
7997 /* XXX: simplify init */
8010 len
= write(fds
[1], &status
, 1);
8011 if (len
== -1 && (errno
== EINTR
))
8017 fd
= open("/dev/null", O_RDWR
);