4 * Copyright (c) 2003-2006 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>
62 #if defined(CONFIG_SLIRP)
68 #include <sys/timeb.h>
70 #define getopt_long_only getopt_long
71 #define memalign(align, size) malloc(size)
74 #include "qemu_socket.h"
80 #endif /* CONFIG_SDL */
84 #define main qemu_main
85 #endif /* CONFIG_COCOA */
91 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
93 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
95 #define SMBD_COMMAND "/usr/sbin/smbd"
98 //#define DEBUG_UNUSED_IOPORT
99 //#define DEBUG_IOPORT
101 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
104 #define DEFAULT_RAM_SIZE 144
106 #define DEFAULT_RAM_SIZE 128
109 #define GUI_REFRESH_INTERVAL 30
111 /* Max number of USB devices that can be specified on the commandline. */
112 #define MAX_USB_CMDLINE 8
114 /* XXX: use a two level table to limit memory usage */
115 #define MAX_IOPORTS 65536
117 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
118 char phys_ram_file
[1024];
119 void *ioport_opaque
[MAX_IOPORTS
];
120 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
121 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
122 /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available
123 to store the VM snapshots */
124 BlockDriverState
*bs_table
[MAX_DISKS
+ 1], *fd_table
[MAX_FD
];
125 /* point to the block driver where the snapshots are managed */
126 BlockDriverState
*bs_snapshots
;
129 static DisplayState display_state
;
131 const char* keyboard_layout
= NULL
;
132 int64_t ticks_per_sec
;
133 int boot_device
= 'c';
135 int pit_min_timer_count
= 0;
137 NICInfo nd_table
[MAX_NICS
];
138 QEMUTimer
*gui_timer
;
141 int cirrus_vga_enabled
= 1;
143 int graphic_width
= 1024;
144 int graphic_height
= 768;
146 int graphic_width
= 800;
147 int graphic_height
= 600;
149 int graphic_depth
= 15;
152 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
153 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
155 int win2k_install_hack
= 0;
158 static VLANState
*first_vlan
;
160 const char *vnc_display
;
161 #if defined(TARGET_SPARC)
163 #elif defined(TARGET_I386)
168 int acpi_enabled
= 1;
172 const char *option_rom
[MAX_OPTION_ROMS
];
174 int semihosting_enabled
= 0;
176 /***********************************************************/
177 /* x86 ISA bus support */
179 target_phys_addr_t isa_mem_base
= 0;
182 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
184 #ifdef DEBUG_UNUSED_IOPORT
185 fprintf(stderr
, "inb: port=0x%04x\n", address
);
190 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
192 #ifdef DEBUG_UNUSED_IOPORT
193 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
197 /* default is to make two byte accesses */
198 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
201 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
202 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
203 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
207 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
209 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
210 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
211 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
214 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
216 #ifdef DEBUG_UNUSED_IOPORT
217 fprintf(stderr
, "inl: port=0x%04x\n", address
);
222 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
224 #ifdef DEBUG_UNUSED_IOPORT
225 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
229 void init_ioports(void)
233 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
234 ioport_read_table
[0][i
] = default_ioport_readb
;
235 ioport_write_table
[0][i
] = default_ioport_writeb
;
236 ioport_read_table
[1][i
] = default_ioport_readw
;
237 ioport_write_table
[1][i
] = default_ioport_writew
;
238 ioport_read_table
[2][i
] = default_ioport_readl
;
239 ioport_write_table
[2][i
] = default_ioport_writel
;
243 /* size is the word size in byte */
244 int register_ioport_read(int start
, int length
, int size
,
245 IOPortReadFunc
*func
, void *opaque
)
251 } else if (size
== 2) {
253 } else if (size
== 4) {
256 hw_error("register_ioport_read: invalid size");
259 for(i
= start
; i
< start
+ length
; i
+= size
) {
260 ioport_read_table
[bsize
][i
] = func
;
261 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
262 hw_error("register_ioport_read: invalid opaque");
263 ioport_opaque
[i
] = opaque
;
268 /* size is the word size in byte */
269 int register_ioport_write(int start
, int length
, int size
,
270 IOPortWriteFunc
*func
, void *opaque
)
276 } else if (size
== 2) {
278 } else if (size
== 4) {
281 hw_error("register_ioport_write: invalid size");
284 for(i
= start
; i
< start
+ length
; i
+= size
) {
285 ioport_write_table
[bsize
][i
] = func
;
286 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
287 hw_error("register_ioport_write: invalid opaque");
288 ioport_opaque
[i
] = opaque
;
293 void isa_unassign_ioport(int start
, int length
)
297 for(i
= start
; i
< start
+ length
; i
++) {
298 ioport_read_table
[0][i
] = default_ioport_readb
;
299 ioport_read_table
[1][i
] = default_ioport_readw
;
300 ioport_read_table
[2][i
] = default_ioport_readl
;
302 ioport_write_table
[0][i
] = default_ioport_writeb
;
303 ioport_write_table
[1][i
] = default_ioport_writew
;
304 ioport_write_table
[2][i
] = default_ioport_writel
;
308 /***********************************************************/
310 void cpu_outb(CPUState
*env
, int addr
, int val
)
313 if (loglevel
& CPU_LOG_IOPORT
)
314 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
316 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
319 env
->last_io_time
= cpu_get_time_fast();
323 void cpu_outw(CPUState
*env
, int addr
, int val
)
326 if (loglevel
& CPU_LOG_IOPORT
)
327 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
329 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
332 env
->last_io_time
= cpu_get_time_fast();
336 void cpu_outl(CPUState
*env
, int addr
, int val
)
339 if (loglevel
& CPU_LOG_IOPORT
)
340 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
342 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
345 env
->last_io_time
= cpu_get_time_fast();
349 int cpu_inb(CPUState
*env
, int addr
)
352 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
354 if (loglevel
& CPU_LOG_IOPORT
)
355 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
359 env
->last_io_time
= cpu_get_time_fast();
364 int cpu_inw(CPUState
*env
, int addr
)
367 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
369 if (loglevel
& CPU_LOG_IOPORT
)
370 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
374 env
->last_io_time
= cpu_get_time_fast();
379 int cpu_inl(CPUState
*env
, int addr
)
382 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
384 if (loglevel
& CPU_LOG_IOPORT
)
385 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
389 env
->last_io_time
= cpu_get_time_fast();
394 /***********************************************************/
395 void hw_error(const char *fmt
, ...)
401 fprintf(stderr
, "qemu: hardware error: ");
402 vfprintf(stderr
, fmt
, ap
);
403 fprintf(stderr
, "\n");
404 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
405 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
407 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
409 cpu_dump_state(env
, stderr
, fprintf
, 0);
416 /***********************************************************/
419 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
420 static void *qemu_put_kbd_event_opaque
;
421 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
422 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
424 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
426 qemu_put_kbd_event_opaque
= opaque
;
427 qemu_put_kbd_event
= func
;
430 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
431 void *opaque
, int absolute
,
434 QEMUPutMouseEntry
*s
, *cursor
;
436 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
440 s
->qemu_put_mouse_event
= func
;
441 s
->qemu_put_mouse_event_opaque
= opaque
;
442 s
->qemu_put_mouse_event_absolute
= absolute
;
443 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
446 if (!qemu_put_mouse_event_head
) {
447 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
451 cursor
= qemu_put_mouse_event_head
;
452 while (cursor
->next
!= NULL
)
453 cursor
= cursor
->next
;
456 qemu_put_mouse_event_current
= s
;
461 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
463 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
465 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
468 cursor
= qemu_put_mouse_event_head
;
469 while (cursor
!= NULL
&& cursor
!= entry
) {
471 cursor
= cursor
->next
;
474 if (cursor
== NULL
) // does not exist or list empty
476 else if (prev
== NULL
) { // entry is head
477 qemu_put_mouse_event_head
= cursor
->next
;
478 if (qemu_put_mouse_event_current
== entry
)
479 qemu_put_mouse_event_current
= cursor
->next
;
480 qemu_free(entry
->qemu_put_mouse_event_name
);
485 prev
->next
= entry
->next
;
487 if (qemu_put_mouse_event_current
== entry
)
488 qemu_put_mouse_event_current
= prev
;
490 qemu_free(entry
->qemu_put_mouse_event_name
);
494 void kbd_put_keycode(int keycode
)
496 if (qemu_put_kbd_event
) {
497 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
501 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
503 QEMUPutMouseEvent
*mouse_event
;
504 void *mouse_event_opaque
;
506 if (!qemu_put_mouse_event_current
) {
511 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
513 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
516 mouse_event(mouse_event_opaque
, dx
, dy
, dz
, buttons_state
);
520 int kbd_mouse_is_absolute(void)
522 if (!qemu_put_mouse_event_current
)
525 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
528 void do_info_mice(void)
530 QEMUPutMouseEntry
*cursor
;
533 if (!qemu_put_mouse_event_head
) {
534 term_printf("No mouse devices connected\n");
538 term_printf("Mouse devices available:\n");
539 cursor
= qemu_put_mouse_event_head
;
540 while (cursor
!= NULL
) {
541 term_printf("%c Mouse #%d: %s\n",
542 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
543 index
, cursor
->qemu_put_mouse_event_name
);
545 cursor
= cursor
->next
;
549 void do_mouse_set(int index
)
551 QEMUPutMouseEntry
*cursor
;
554 if (!qemu_put_mouse_event_head
) {
555 term_printf("No mouse devices connected\n");
559 cursor
= qemu_put_mouse_event_head
;
560 while (cursor
!= NULL
&& index
!= i
) {
562 cursor
= cursor
->next
;
566 qemu_put_mouse_event_current
= cursor
;
568 term_printf("Mouse at given index not found\n");
571 /* compute with 96 bit intermediate result: (a*b)/c */
572 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
577 #ifdef WORDS_BIGENDIAN
587 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
588 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
591 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
595 /***********************************************************/
596 /* real time host monotonic timer */
598 #define QEMU_TIMER_BASE 1000000000LL
602 static int64_t clock_freq
;
604 static void init_get_clock(void)
608 ret
= QueryPerformanceFrequency(&freq
);
610 fprintf(stderr
, "Could not calibrate ticks\n");
613 clock_freq
= freq
.QuadPart
;
616 static int64_t get_clock(void)
619 QueryPerformanceCounter(&ti
);
620 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
625 static int use_rt_clock
;
627 static void init_get_clock(void)
630 #if defined(__linux__)
633 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
640 static int64_t get_clock(void)
642 #if defined(__linux__)
645 clock_gettime(CLOCK_MONOTONIC
, &ts
);
646 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
650 /* XXX: using gettimeofday leads to problems if the date
651 changes, so it should be avoided. */
653 gettimeofday(&tv
, NULL
);
654 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
660 /***********************************************************/
661 /* guest cycle counter */
663 static int64_t cpu_ticks_prev
;
664 static int64_t cpu_ticks_offset
;
665 static int64_t cpu_clock_offset
;
666 static int cpu_ticks_enabled
;
668 /* return the host CPU cycle counter and handle stop/restart */
669 int64_t cpu_get_ticks(void)
671 if (!cpu_ticks_enabled
) {
672 return cpu_ticks_offset
;
675 ticks
= cpu_get_real_ticks();
676 if (cpu_ticks_prev
> ticks
) {
677 /* Note: non increasing ticks may happen if the host uses
679 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
681 cpu_ticks_prev
= ticks
;
682 return ticks
+ cpu_ticks_offset
;
686 /* return the host CPU monotonic timer and handle stop/restart */
687 static int64_t cpu_get_clock(void)
690 if (!cpu_ticks_enabled
) {
691 return cpu_clock_offset
;
694 return ti
+ cpu_clock_offset
;
698 /* enable cpu_get_ticks() */
699 void cpu_enable_ticks(void)
701 if (!cpu_ticks_enabled
) {
702 cpu_ticks_offset
-= cpu_get_real_ticks();
703 cpu_clock_offset
-= get_clock();
704 cpu_ticks_enabled
= 1;
708 /* disable cpu_get_ticks() : the clock is stopped. You must not call
709 cpu_get_ticks() after that. */
710 void cpu_disable_ticks(void)
712 if (cpu_ticks_enabled
) {
713 cpu_ticks_offset
= cpu_get_ticks();
714 cpu_clock_offset
= cpu_get_clock();
715 cpu_ticks_enabled
= 0;
719 /***********************************************************/
722 #define QEMU_TIMER_REALTIME 0
723 #define QEMU_TIMER_VIRTUAL 1
727 /* XXX: add frequency */
735 struct QEMUTimer
*next
;
741 static QEMUTimer
*active_timers
[2];
743 static MMRESULT timerID
;
744 static HANDLE host_alarm
= NULL
;
745 static unsigned int period
= 1;
747 /* frequency of the times() clock tick */
748 static int timer_freq
;
751 QEMUClock
*qemu_new_clock(int type
)
754 clock
= qemu_mallocz(sizeof(QEMUClock
));
761 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
765 ts
= qemu_mallocz(sizeof(QEMUTimer
));
772 void qemu_free_timer(QEMUTimer
*ts
)
777 /* stop a timer, but do not dealloc it */
778 void qemu_del_timer(QEMUTimer
*ts
)
782 /* NOTE: this code must be signal safe because
783 qemu_timer_expired() can be called from a signal. */
784 pt
= &active_timers
[ts
->clock
->type
];
797 /* modify the current timer so that it will be fired when current_time
798 >= expire_time. The corresponding callback will be called. */
799 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
805 /* add the timer in the sorted list */
806 /* NOTE: this code must be signal safe because
807 qemu_timer_expired() can be called from a signal. */
808 pt
= &active_timers
[ts
->clock
->type
];
813 if (t
->expire_time
> expire_time
)
817 ts
->expire_time
= expire_time
;
822 int qemu_timer_pending(QEMUTimer
*ts
)
825 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
832 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
836 return (timer_head
->expire_time
<= current_time
);
839 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
845 if (!ts
|| ts
->expire_time
> current_time
)
847 /* remove timer from the list before calling the callback */
848 *ptimer_head
= ts
->next
;
851 /* run the callback (the timer list can be modified) */
856 int64_t qemu_get_clock(QEMUClock
*clock
)
858 switch(clock
->type
) {
859 case QEMU_TIMER_REALTIME
:
860 return get_clock() / 1000000;
862 case QEMU_TIMER_VIRTUAL
:
863 return cpu_get_clock();
867 static void init_timers(void)
870 ticks_per_sec
= QEMU_TIMER_BASE
;
871 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
872 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
876 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
878 uint64_t expire_time
;
880 if (qemu_timer_pending(ts
)) {
881 expire_time
= ts
->expire_time
;
885 qemu_put_be64(f
, expire_time
);
888 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
890 uint64_t expire_time
;
892 expire_time
= qemu_get_be64(f
);
893 if (expire_time
!= -1) {
894 qemu_mod_timer(ts
, expire_time
);
900 static void timer_save(QEMUFile
*f
, void *opaque
)
902 if (cpu_ticks_enabled
) {
903 hw_error("cannot save state if virtual timers are running");
905 qemu_put_be64s(f
, &cpu_ticks_offset
);
906 qemu_put_be64s(f
, &ticks_per_sec
);
907 qemu_put_be64s(f
, &cpu_clock_offset
);
910 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
912 if (version_id
!= 1 && version_id
!= 2)
914 if (cpu_ticks_enabled
) {
917 qemu_get_be64s(f
, &cpu_ticks_offset
);
918 qemu_get_be64s(f
, &ticks_per_sec
);
919 if (version_id
== 2) {
920 qemu_get_be64s(f
, &cpu_clock_offset
);
926 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
927 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
929 static void host_alarm_handler(int host_signum
)
933 #define DISP_FREQ 1000
935 static int64_t delta_min
= INT64_MAX
;
936 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
938 ti
= qemu_get_clock(vm_clock
);
939 if (last_clock
!= 0) {
940 delta
= ti
- last_clock
;
941 if (delta
< delta_min
)
943 if (delta
> delta_max
)
946 if (++count
== DISP_FREQ
) {
947 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
948 muldiv64(delta_min
, 1000000, ticks_per_sec
),
949 muldiv64(delta_max
, 1000000, ticks_per_sec
),
950 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
951 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
953 delta_min
= INT64_MAX
;
961 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
962 qemu_get_clock(vm_clock
)) ||
963 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
964 qemu_get_clock(rt_clock
))) {
966 SetEvent(host_alarm
);
968 CPUState
*env
= cpu_single_env
;
970 /* stop the currently executing cpu because a timer occured */
971 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
973 if (env
->kqemu_enabled
) {
974 kqemu_cpu_interrupt(env
);
983 #if defined(__linux__)
985 #define RTC_FREQ 1024
989 static int start_rtc_timer(void)
991 rtc_fd
= open("/dev/rtc", O_RDONLY
);
994 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
995 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
996 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
997 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1000 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1005 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
1011 static int start_rtc_timer(void)
1016 #endif /* !defined(__linux__) */
1018 #endif /* !defined(_WIN32) */
1020 static void init_timer_alarm(void)
1027 ZeroMemory(&tc
, sizeof(TIMECAPS
));
1028 timeGetDevCaps(&tc
, sizeof(TIMECAPS
));
1029 if (period
< tc
.wPeriodMin
)
1030 period
= tc
.wPeriodMin
;
1031 timeBeginPeriod(period
);
1032 timerID
= timeSetEvent(1, // interval (ms)
1033 period
, // resolution
1034 host_alarm_handler
, // function
1035 (DWORD
)&count
, // user parameter
1036 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
1038 perror("failed timer alarm");
1041 host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1043 perror("failed CreateEvent");
1046 qemu_add_wait_object(host_alarm
, NULL
, NULL
);
1048 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
1051 struct sigaction act
;
1052 struct itimerval itv
;
1054 /* get times() syscall frequency */
1055 timer_freq
= sysconf(_SC_CLK_TCK
);
1058 sigfillset(&act
.sa_mask
);
1060 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1061 act
.sa_flags
|= SA_ONSTACK
;
1063 act
.sa_handler
= host_alarm_handler
;
1064 sigaction(SIGALRM
, &act
, NULL
);
1066 itv
.it_interval
.tv_sec
= 0;
1067 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
1068 itv
.it_value
.tv_sec
= 0;
1069 itv
.it_value
.tv_usec
= 10 * 1000;
1070 setitimer(ITIMER_REAL
, &itv
, NULL
);
1071 /* we probe the tick duration of the kernel to inform the user if
1072 the emulated kernel requested a too high timer frequency */
1073 getitimer(ITIMER_REAL
, &itv
);
1075 #if defined(__linux__)
1076 /* XXX: force /dev/rtc usage because even 2.6 kernels may not
1077 have timers with 1 ms resolution. The correct solution will
1078 be to use the POSIX real time timers available in recent
1080 if (itv
.it_interval
.tv_usec
> 1000 || 1) {
1081 /* try to use /dev/rtc to have a faster timer */
1082 if (start_rtc_timer() < 0)
1084 /* disable itimer */
1085 itv
.it_interval
.tv_sec
= 0;
1086 itv
.it_interval
.tv_usec
= 0;
1087 itv
.it_value
.tv_sec
= 0;
1088 itv
.it_value
.tv_usec
= 0;
1089 setitimer(ITIMER_REAL
, &itv
, NULL
);
1092 sigaction(SIGIO
, &act
, NULL
);
1093 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
1094 fcntl(rtc_fd
, F_SETOWN
, getpid());
1096 #endif /* defined(__linux__) */
1099 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1100 PIT_FREQ
) / 1000000;
1106 void quit_timers(void)
1109 timeKillEvent(timerID
);
1110 timeEndPeriod(period
);
1112 CloseHandle(host_alarm
);
1118 /***********************************************************/
1119 /* character device */
1121 static void qemu_chr_reset_bh(void *opaque
)
1123 CharDriverState
*s
= opaque
;
1125 s
->chr_event(s
, CHR_EVENT_RESET
);
1126 qemu_bh_delete(s
->bh
);
1130 void qemu_chr_reset(CharDriverState
*s
)
1132 if (s
->bh
== NULL
) {
1133 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
1134 qemu_bh_schedule(s
->bh
);
1138 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1140 return s
->chr_write(s
, buf
, len
);
1143 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1147 return s
->chr_ioctl(s
, cmd
, arg
);
1150 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1155 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1156 qemu_chr_write(s
, buf
, strlen(buf
));
1160 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1162 if (s
->chr_send_event
)
1163 s
->chr_send_event(s
, event
);
1166 void qemu_chr_add_read_handler(CharDriverState
*s
,
1167 IOCanRWHandler
*fd_can_read
,
1168 IOReadHandler
*fd_read
, void *opaque
)
1170 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
1173 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
1175 s
->chr_event
= chr_event
;
1178 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1183 static void null_chr_add_read_handler(CharDriverState
*chr
,
1184 IOCanRWHandler
*fd_can_read
,
1185 IOReadHandler
*fd_read
, void *opaque
)
1189 static CharDriverState
*qemu_chr_open_null(void)
1191 CharDriverState
*chr
;
1193 chr
= qemu_mallocz(sizeof(CharDriverState
));
1196 chr
->chr_write
= null_chr_write
;
1197 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1203 static void socket_cleanup(void)
1208 static int socket_init(void)
1213 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1215 err
= WSAGetLastError();
1216 fprintf(stderr
, "WSAStartup: %d\n", err
);
1219 atexit(socket_cleanup
);
1223 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1229 ret
= send(fd
, buf
, len
, 0);
1232 errno
= WSAGetLastError();
1233 if (errno
!= WSAEWOULDBLOCK
) {
1236 } else if (ret
== 0) {
1246 void socket_set_nonblock(int fd
)
1248 unsigned long opt
= 1;
1249 ioctlsocket(fd
, FIONBIO
, &opt
);
1254 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1260 ret
= write(fd
, buf
, len
);
1262 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1264 } else if (ret
== 0) {
1274 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1276 return unix_write(fd
, buf
, len1
);
1279 void socket_set_nonblock(int fd
)
1281 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1283 #endif /* !_WIN32 */
1289 IOCanRWHandler
*fd_can_read
;
1290 IOReadHandler
*fd_read
;
1295 #define STDIO_MAX_CLIENTS 2
1297 static int stdio_nb_clients
;
1298 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1300 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1302 FDCharDriver
*s
= chr
->opaque
;
1303 return unix_write(s
->fd_out
, buf
, len
);
1306 static int fd_chr_read_poll(void *opaque
)
1308 CharDriverState
*chr
= opaque
;
1309 FDCharDriver
*s
= chr
->opaque
;
1311 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
1315 static void fd_chr_read(void *opaque
)
1317 CharDriverState
*chr
= opaque
;
1318 FDCharDriver
*s
= chr
->opaque
;
1323 if (len
> s
->max_size
)
1327 size
= read(s
->fd_in
, buf
, len
);
1329 /* FD has been closed. Remove it from the active list. */
1330 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
1334 s
->fd_read(s
->fd_opaque
, buf
, size
);
1338 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1339 IOCanRWHandler
*fd_can_read
,
1340 IOReadHandler
*fd_read
, void *opaque
)
1342 FDCharDriver
*s
= chr
->opaque
;
1344 if (s
->fd_in
>= 0) {
1345 s
->fd_can_read
= fd_can_read
;
1346 s
->fd_read
= fd_read
;
1347 s
->fd_opaque
= opaque
;
1348 if (nographic
&& s
->fd_in
== 0) {
1350 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1351 fd_chr_read
, NULL
, chr
);
1356 /* open a character device to a unix fd */
1357 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1359 CharDriverState
*chr
;
1362 chr
= qemu_mallocz(sizeof(CharDriverState
));
1365 s
= qemu_mallocz(sizeof(FDCharDriver
));
1373 chr
->chr_write
= fd_chr_write
;
1374 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1376 qemu_chr_reset(chr
);
1381 static CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1385 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666);
1388 return qemu_chr_open_fd(-1, fd_out
);
1391 static CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1394 char filename_in
[256], filename_out
[256];
1396 snprintf(filename_in
, 256, "%s.in", filename
);
1397 snprintf(filename_out
, 256, "%s.out", filename
);
1398 fd_in
= open(filename_in
, O_RDWR
| O_BINARY
);
1399 fd_out
= open(filename_out
, O_RDWR
| O_BINARY
);
1400 if (fd_in
< 0 || fd_out
< 0) {
1405 fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
);
1409 return qemu_chr_open_fd(fd_in
, fd_out
);
1413 /* for STDIO, we handle the case where several clients use it
1416 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1418 #define TERM_FIFO_MAX_SIZE 1
1420 static int term_got_escape
, client_index
;
1421 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1422 static int term_fifo_size
;
1423 static int term_timestamps
;
1424 static int64_t term_timestamps_start
;
1426 void term_print_help(void)
1429 "C-a h print this help\n"
1430 "C-a x exit emulator\n"
1431 "C-a s save disk data back to file (if -snapshot)\n"
1432 "C-a b send break (magic sysrq)\n"
1433 "C-a t toggle console timestamps\n"
1434 "C-a c switch between console and monitor\n"
1435 "C-a C-a send C-a\n"
1439 /* called when a char is received */
1440 static void stdio_received_byte(int ch
)
1442 if (term_got_escape
) {
1443 term_got_escape
= 0;
1454 for (i
= 0; i
< MAX_DISKS
; i
++) {
1456 bdrv_commit(bs_table
[i
]);
1461 if (client_index
< stdio_nb_clients
) {
1462 CharDriverState
*chr
;
1465 chr
= stdio_clients
[client_index
];
1467 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1472 if (client_index
>= stdio_nb_clients
)
1474 if (client_index
== 0) {
1475 /* send a new line in the monitor to get the prompt */
1481 term_timestamps
= !term_timestamps
;
1482 term_timestamps_start
= -1;
1487 } else if (ch
== TERM_ESCAPE
) {
1488 term_got_escape
= 1;
1491 if (client_index
< stdio_nb_clients
) {
1493 CharDriverState
*chr
;
1496 chr
= stdio_clients
[client_index
];
1498 if (s
->fd_can_read(s
->fd_opaque
) > 0) {
1500 s
->fd_read(s
->fd_opaque
, buf
, 1);
1501 } else if (term_fifo_size
== 0) {
1502 term_fifo
[term_fifo_size
++] = ch
;
1508 static int stdio_read_poll(void *opaque
)
1510 CharDriverState
*chr
;
1513 if (client_index
< stdio_nb_clients
) {
1514 chr
= stdio_clients
[client_index
];
1516 /* try to flush the queue if needed */
1517 if (term_fifo_size
!= 0 && s
->fd_can_read(s
->fd_opaque
) > 0) {
1518 s
->fd_read(s
->fd_opaque
, term_fifo
, 1);
1521 /* see if we can absorb more chars */
1522 if (term_fifo_size
== 0)
1531 static void stdio_read(void *opaque
)
1536 size
= read(0, buf
, 1);
1538 /* stdin has been closed. Remove it from the active list. */
1539 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
1543 stdio_received_byte(buf
[0]);
1546 static int stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1548 FDCharDriver
*s
= chr
->opaque
;
1549 if (!term_timestamps
) {
1550 return unix_write(s
->fd_out
, buf
, len
);
1555 for(i
= 0; i
< len
; i
++) {
1556 unix_write(s
->fd_out
, buf
+ i
, 1);
1557 if (buf
[i
] == '\n') {
1562 if (term_timestamps_start
== -1)
1563 term_timestamps_start
= ti
;
1564 ti
-= term_timestamps_start
;
1565 secs
= ti
/ 1000000000;
1566 snprintf(buf1
, sizeof(buf1
),
1567 "[%02d:%02d:%02d.%03d] ",
1571 (int)((ti
/ 1000000) % 1000));
1572 unix_write(s
->fd_out
, buf1
, strlen(buf1
));
1579 /* init terminal so that we can grab keys */
1580 static struct termios oldtty
;
1581 static int old_fd0_flags
;
1583 static void term_exit(void)
1585 tcsetattr (0, TCSANOW
, &oldtty
);
1586 fcntl(0, F_SETFL
, old_fd0_flags
);
1589 static void term_init(void)
1593 tcgetattr (0, &tty
);
1595 old_fd0_flags
= fcntl(0, F_GETFL
);
1597 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1598 |INLCR
|IGNCR
|ICRNL
|IXON
);
1599 tty
.c_oflag
|= OPOST
;
1600 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1601 /* if graphical mode, we allow Ctrl-C handling */
1603 tty
.c_lflag
&= ~ISIG
;
1604 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1607 tty
.c_cc
[VTIME
] = 0;
1609 tcsetattr (0, TCSANOW
, &tty
);
1613 fcntl(0, F_SETFL
, O_NONBLOCK
);
1616 static CharDriverState
*qemu_chr_open_stdio(void)
1618 CharDriverState
*chr
;
1621 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1623 chr
= qemu_chr_open_fd(0, 1);
1624 chr
->chr_write
= stdio_write
;
1625 if (stdio_nb_clients
== 0)
1626 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, NULL
);
1627 client_index
= stdio_nb_clients
;
1629 if (stdio_nb_clients
!= 0)
1631 chr
= qemu_chr_open_fd(0, 1);
1633 stdio_clients
[stdio_nb_clients
++] = chr
;
1634 if (stdio_nb_clients
== 1) {
1635 /* set the terminal in raw mode */
1641 #if defined(__linux__)
1642 static CharDriverState
*qemu_chr_open_pty(void)
1645 char slave_name
[1024];
1646 int master_fd
, slave_fd
;
1648 /* Not satisfying */
1649 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1653 /* Disabling local echo and line-buffered output */
1654 tcgetattr (master_fd
, &tty
);
1655 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1657 tty
.c_cc
[VTIME
] = 0;
1658 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1660 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1661 return qemu_chr_open_fd(master_fd
, master_fd
);
1664 static void tty_serial_init(int fd
, int speed
,
1665 int parity
, int data_bits
, int stop_bits
)
1671 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1672 speed
, parity
, data_bits
, stop_bits
);
1674 tcgetattr (fd
, &tty
);
1716 cfsetispeed(&tty
, spd
);
1717 cfsetospeed(&tty
, spd
);
1719 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1720 |INLCR
|IGNCR
|ICRNL
|IXON
);
1721 tty
.c_oflag
|= OPOST
;
1722 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1723 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1744 tty
.c_cflag
|= PARENB
;
1747 tty
.c_cflag
|= PARENB
| PARODD
;
1751 tty
.c_cflag
|= CSTOPB
;
1753 tcsetattr (fd
, TCSANOW
, &tty
);
1756 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1758 FDCharDriver
*s
= chr
->opaque
;
1761 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1763 QEMUSerialSetParams
*ssp
= arg
;
1764 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1765 ssp
->data_bits
, ssp
->stop_bits
);
1768 case CHR_IOCTL_SERIAL_SET_BREAK
:
1770 int enable
= *(int *)arg
;
1772 tcsendbreak(s
->fd_in
, 1);
1781 static CharDriverState
*qemu_chr_open_tty(const char *filename
)
1783 CharDriverState
*chr
;
1786 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1789 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1790 tty_serial_init(fd
, 115200, 'N', 8, 1);
1791 chr
= qemu_chr_open_fd(fd
, fd
);
1794 chr
->chr_ioctl
= tty_serial_ioctl
;
1795 qemu_chr_reset(chr
);
1799 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1801 int fd
= (int)chr
->opaque
;
1805 case CHR_IOCTL_PP_READ_DATA
:
1806 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1808 *(uint8_t *)arg
= b
;
1810 case CHR_IOCTL_PP_WRITE_DATA
:
1811 b
= *(uint8_t *)arg
;
1812 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1815 case CHR_IOCTL_PP_READ_CONTROL
:
1816 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1818 *(uint8_t *)arg
= b
;
1820 case CHR_IOCTL_PP_WRITE_CONTROL
:
1821 b
= *(uint8_t *)arg
;
1822 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1825 case CHR_IOCTL_PP_READ_STATUS
:
1826 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1828 *(uint8_t *)arg
= b
;
1836 static CharDriverState
*qemu_chr_open_pp(const char *filename
)
1838 CharDriverState
*chr
;
1841 fd
= open(filename
, O_RDWR
);
1845 if (ioctl(fd
, PPCLAIM
) < 0) {
1850 chr
= qemu_mallocz(sizeof(CharDriverState
));
1855 chr
->opaque
= (void *)fd
;
1856 chr
->chr_write
= null_chr_write
;
1857 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1858 chr
->chr_ioctl
= pp_ioctl
;
1860 qemu_chr_reset(chr
);
1866 static CharDriverState
*qemu_chr_open_pty(void)
1872 #endif /* !defined(_WIN32) */
1876 IOCanRWHandler
*fd_can_read
;
1877 IOReadHandler
*fd_read
;
1880 HANDLE hcom
, hrecv
, hsend
;
1881 OVERLAPPED orecv
, osend
;
1886 #define NSENDBUF 2048
1887 #define NRECVBUF 2048
1888 #define MAXCONNECT 1
1889 #define NTIMEOUT 5000
1891 static int win_chr_poll(void *opaque
);
1892 static int win_chr_pipe_poll(void *opaque
);
1894 static void win_chr_close2(WinCharState
*s
)
1897 CloseHandle(s
->hsend
);
1901 CloseHandle(s
->hrecv
);
1905 CloseHandle(s
->hcom
);
1909 qemu_del_polling_cb(win_chr_pipe_poll
, s
);
1911 qemu_del_polling_cb(win_chr_poll
, s
);
1914 static void win_chr_close(CharDriverState
*chr
)
1916 WinCharState
*s
= chr
->opaque
;
1920 static int win_chr_init(WinCharState
*s
, const char *filename
)
1923 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1928 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1930 fprintf(stderr
, "Failed CreateEvent\n");
1933 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1935 fprintf(stderr
, "Failed CreateEvent\n");
1939 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1940 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1941 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1942 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1947 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1948 fprintf(stderr
, "Failed SetupComm\n");
1952 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1953 size
= sizeof(COMMCONFIG
);
1954 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1955 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1956 CommConfigDialog(filename
, NULL
, &comcfg
);
1958 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1959 fprintf(stderr
, "Failed SetCommState\n");
1963 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1964 fprintf(stderr
, "Failed SetCommMask\n");
1968 cto
.ReadIntervalTimeout
= MAXDWORD
;
1969 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1970 fprintf(stderr
, "Failed SetCommTimeouts\n");
1974 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1975 fprintf(stderr
, "Failed ClearCommError\n");
1978 qemu_add_polling_cb(win_chr_poll
, s
);
1986 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1988 WinCharState
*s
= chr
->opaque
;
1989 DWORD len
, ret
, size
, err
;
1992 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1993 s
->osend
.hEvent
= s
->hsend
;
1996 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1998 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2000 err
= GetLastError();
2001 if (err
== ERROR_IO_PENDING
) {
2002 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2020 static int win_chr_read_poll(WinCharState
*s
)
2022 s
->max_size
= s
->fd_can_read(s
->win_opaque
);
2026 static void win_chr_readfile(WinCharState
*s
)
2032 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2033 s
->orecv
.hEvent
= s
->hrecv
;
2034 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2036 err
= GetLastError();
2037 if (err
== ERROR_IO_PENDING
) {
2038 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2043 s
->fd_read(s
->win_opaque
, buf
, size
);
2047 static void win_chr_read(WinCharState
*s
)
2049 if (s
->len
> s
->max_size
)
2050 s
->len
= s
->max_size
;
2054 win_chr_readfile(s
);
2057 static int win_chr_poll(void *opaque
)
2059 WinCharState
*s
= opaque
;
2063 ClearCommError(s
->hcom
, &comerr
, &status
);
2064 if (status
.cbInQue
> 0) {
2065 s
->len
= status
.cbInQue
;
2066 win_chr_read_poll(s
);
2073 static void win_chr_add_read_handler(CharDriverState
*chr
,
2074 IOCanRWHandler
*fd_can_read
,
2075 IOReadHandler
*fd_read
, void *opaque
)
2077 WinCharState
*s
= chr
->opaque
;
2079 s
->fd_can_read
= fd_can_read
;
2080 s
->fd_read
= fd_read
;
2081 s
->win_opaque
= opaque
;
2084 static CharDriverState
*qemu_chr_open_win(const char *filename
)
2086 CharDriverState
*chr
;
2089 chr
= qemu_mallocz(sizeof(CharDriverState
));
2092 s
= qemu_mallocz(sizeof(WinCharState
));
2098 chr
->chr_write
= win_chr_write
;
2099 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2100 chr
->chr_close
= win_chr_close
;
2102 if (win_chr_init(s
, filename
) < 0) {
2107 qemu_chr_reset(chr
);
2111 static int win_chr_pipe_poll(void *opaque
)
2113 WinCharState
*s
= opaque
;
2116 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2119 win_chr_read_poll(s
);
2126 static int win_chr_pipe_init(WinCharState
*s
, const char *filename
)
2135 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2137 fprintf(stderr
, "Failed CreateEvent\n");
2140 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2142 fprintf(stderr
, "Failed CreateEvent\n");
2146 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2147 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2148 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2150 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2151 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2152 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2157 ZeroMemory(&ov
, sizeof(ov
));
2158 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2159 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2161 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2165 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2167 fprintf(stderr
, "Failed GetOverlappedResult\n");
2169 CloseHandle(ov
.hEvent
);
2176 CloseHandle(ov
.hEvent
);
2179 qemu_add_polling_cb(win_chr_pipe_poll
, s
);
2188 static CharDriverState
*qemu_chr_open_win_pipe(const char *filename
)
2190 CharDriverState
*chr
;
2193 chr
= qemu_mallocz(sizeof(CharDriverState
));
2196 s
= qemu_mallocz(sizeof(WinCharState
));
2202 chr
->chr_write
= win_chr_write
;
2203 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2204 chr
->chr_close
= win_chr_close
;
2206 if (win_chr_pipe_init(s
, filename
) < 0) {
2211 qemu_chr_reset(chr
);
2215 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2217 CharDriverState
*chr
;
2220 chr
= qemu_mallocz(sizeof(CharDriverState
));
2223 s
= qemu_mallocz(sizeof(WinCharState
));
2230 chr
->chr_write
= win_chr_write
;
2231 chr
->chr_add_read_handler
= win_chr_add_read_handler
;
2232 qemu_chr_reset(chr
);
2236 static CharDriverState
*qemu_chr_open_win_file_out(const char *file_out
)
2240 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
2241 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2242 if (fd_out
== INVALID_HANDLE_VALUE
)
2245 return qemu_chr_open_win_file(fd_out
);
2249 /***********************************************************/
2250 /* UDP Net console */
2253 IOCanRWHandler
*fd_can_read
;
2254 IOReadHandler
*fd_read
;
2257 struct sockaddr_in daddr
;
2264 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2266 NetCharDriver
*s
= chr
->opaque
;
2268 return sendto(s
->fd
, buf
, len
, 0,
2269 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
2272 static int udp_chr_read_poll(void *opaque
)
2274 CharDriverState
*chr
= opaque
;
2275 NetCharDriver
*s
= chr
->opaque
;
2277 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2279 /* If there were any stray characters in the queue process them
2282 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2283 s
->fd_read(s
->fd_opaque
, &s
->buf
[s
->bufptr
], 1);
2285 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2290 static void udp_chr_read(void *opaque
)
2292 CharDriverState
*chr
= opaque
;
2293 NetCharDriver
*s
= chr
->opaque
;
2295 if (s
->max_size
== 0)
2297 s
->bufcnt
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2298 s
->bufptr
= s
->bufcnt
;
2303 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2304 s
->fd_read(s
->fd_opaque
, &s
->buf
[s
->bufptr
], 1);
2306 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2310 static void udp_chr_add_read_handler(CharDriverState
*chr
,
2311 IOCanRWHandler
*fd_can_read
,
2312 IOReadHandler
*fd_read
, void *opaque
)
2314 NetCharDriver
*s
= chr
->opaque
;
2317 s
->fd_can_read
= fd_can_read
;
2318 s
->fd_read
= fd_read
;
2319 s
->fd_opaque
= opaque
;
2320 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
2321 udp_chr_read
, NULL
, chr
);
2325 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
2327 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
);
2329 int parse_host_src_port(struct sockaddr_in
*haddr
,
2330 struct sockaddr_in
*saddr
,
2333 static CharDriverState
*qemu_chr_open_udp(const char *def
)
2335 CharDriverState
*chr
= NULL
;
2336 NetCharDriver
*s
= NULL
;
2338 struct sockaddr_in saddr
;
2340 chr
= qemu_mallocz(sizeof(CharDriverState
));
2343 s
= qemu_mallocz(sizeof(NetCharDriver
));
2347 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2349 perror("socket(PF_INET, SOCK_DGRAM)");
2353 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
2354 printf("Could not parse: %s\n", def
);
2358 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
2368 chr
->chr_write
= udp_chr_write
;
2369 chr
->chr_add_read_handler
= udp_chr_add_read_handler
;
2382 /***********************************************************/
2383 /* TCP Net console */
2386 IOCanRWHandler
*fd_can_read
;
2387 IOReadHandler
*fd_read
;
2396 static void tcp_chr_accept(void *opaque
);
2398 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2400 TCPCharDriver
*s
= chr
->opaque
;
2402 return send_all(s
->fd
, buf
, len
);
2404 /* XXX: indicate an error ? */
2409 static int tcp_chr_read_poll(void *opaque
)
2411 CharDriverState
*chr
= opaque
;
2412 TCPCharDriver
*s
= chr
->opaque
;
2415 if (!s
->fd_can_read
)
2417 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
2422 #define IAC_BREAK 243
2423 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2425 char *buf
, int *size
)
2427 /* Handle any telnet client's basic IAC options to satisfy char by
2428 * char mode with no echo. All IAC options will be removed from
2429 * the buf and the do_telnetopt variable will be used to track the
2430 * state of the width of the IAC information.
2432 * IAC commands come in sets of 3 bytes with the exception of the
2433 * "IAC BREAK" command and the double IAC.
2439 for (i
= 0; i
< *size
; i
++) {
2440 if (s
->do_telnetopt
> 1) {
2441 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2442 /* Double IAC means send an IAC */
2446 s
->do_telnetopt
= 1;
2448 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2449 /* Handle IAC break commands by sending a serial break */
2450 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
2455 if (s
->do_telnetopt
>= 4) {
2456 s
->do_telnetopt
= 1;
2459 if ((unsigned char)buf
[i
] == IAC
) {
2460 s
->do_telnetopt
= 2;
2471 static void tcp_chr_read(void *opaque
)
2473 CharDriverState
*chr
= opaque
;
2474 TCPCharDriver
*s
= chr
->opaque
;
2478 if (!s
->connected
|| s
->max_size
<= 0)
2481 if (len
> s
->max_size
)
2483 size
= recv(s
->fd
, buf
, len
, 0);
2485 /* connection closed */
2487 if (s
->listen_fd
>= 0) {
2488 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2490 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2493 } else if (size
> 0) {
2494 if (s
->do_telnetopt
)
2495 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2497 s
->fd_read(s
->fd_opaque
, buf
, size
);
2501 static void tcp_chr_add_read_handler(CharDriverState
*chr
,
2502 IOCanRWHandler
*fd_can_read
,
2503 IOReadHandler
*fd_read
, void *opaque
)
2505 TCPCharDriver
*s
= chr
->opaque
;
2507 s
->fd_can_read
= fd_can_read
;
2508 s
->fd_read
= fd_read
;
2509 s
->fd_opaque
= opaque
;
2512 static void tcp_chr_connect(void *opaque
)
2514 CharDriverState
*chr
= opaque
;
2515 TCPCharDriver
*s
= chr
->opaque
;
2518 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2519 tcp_chr_read
, NULL
, chr
);
2520 qemu_chr_reset(chr
);
2523 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2524 static void tcp_chr_telnet_init(int fd
)
2527 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2528 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2529 send(fd
, (char *)buf
, 3, 0);
2530 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2531 send(fd
, (char *)buf
, 3, 0);
2532 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2533 send(fd
, (char *)buf
, 3, 0);
2534 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2535 send(fd
, (char *)buf
, 3, 0);
2538 static void tcp_chr_accept(void *opaque
)
2540 CharDriverState
*chr
= opaque
;
2541 TCPCharDriver
*s
= chr
->opaque
;
2542 struct sockaddr_in saddr
;
2544 struct sockaddr_un uaddr
;
2546 struct sockaddr
*addr
;
2553 len
= sizeof(uaddr
);
2554 addr
= (struct sockaddr
*)&uaddr
;
2558 len
= sizeof(saddr
);
2559 addr
= (struct sockaddr
*)&saddr
;
2561 fd
= accept(s
->listen_fd
, addr
, &len
);
2562 if (fd
< 0 && errno
!= EINTR
) {
2564 } else if (fd
>= 0) {
2565 if (s
->do_telnetopt
)
2566 tcp_chr_telnet_init(fd
);
2570 socket_set_nonblock(fd
);
2572 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2573 tcp_chr_connect(chr
);
2576 static void tcp_chr_close(CharDriverState
*chr
)
2578 TCPCharDriver
*s
= chr
->opaque
;
2581 if (s
->listen_fd
>= 0)
2582 closesocket(s
->listen_fd
);
2586 static CharDriverState
*qemu_chr_open_tcp(const char *host_str
,
2590 CharDriverState
*chr
= NULL
;
2591 TCPCharDriver
*s
= NULL
;
2592 int fd
= -1, ret
, err
, val
;
2594 int is_waitconnect
= 1;
2596 struct sockaddr_in saddr
;
2598 struct sockaddr_un uaddr
;
2600 struct sockaddr
*addr
;
2605 addr
= (struct sockaddr
*)&uaddr
;
2606 addrlen
= sizeof(uaddr
);
2607 if (parse_unix_path(&uaddr
, host_str
) < 0)
2612 addr
= (struct sockaddr
*)&saddr
;
2613 addrlen
= sizeof(saddr
);
2614 if (parse_host_port(&saddr
, host_str
) < 0)
2619 while((ptr
= strchr(ptr
,','))) {
2621 if (!strncmp(ptr
,"server",6)) {
2623 } else if (!strncmp(ptr
,"nowait",6)) {
2626 printf("Unknown option: %s\n", ptr
);
2633 chr
= qemu_mallocz(sizeof(CharDriverState
));
2636 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2642 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2645 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2650 if (!is_waitconnect
)
2651 socket_set_nonblock(fd
);
2656 s
->is_unix
= is_unix
;
2659 chr
->chr_write
= tcp_chr_write
;
2660 chr
->chr_add_read_handler
= tcp_chr_add_read_handler
;
2661 chr
->chr_close
= tcp_chr_close
;
2664 /* allow fast reuse */
2668 strncpy(path
, uaddr
.sun_path
, 108);
2675 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2678 ret
= bind(fd
, addr
, addrlen
);
2682 ret
= listen(fd
, 0);
2687 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2689 s
->do_telnetopt
= 1;
2692 ret
= connect(fd
, addr
, addrlen
);
2694 err
= socket_error();
2695 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2696 } else if (err
== EINPROGRESS
) {
2708 tcp_chr_connect(chr
);
2710 qemu_set_fd_handler(s
->fd
, NULL
, tcp_chr_connect
, chr
);
2713 if (is_listen
&& is_waitconnect
) {
2714 printf("QEMU waiting for connection on: %s\n", host_str
);
2715 tcp_chr_accept(chr
);
2716 socket_set_nonblock(s
->listen_fd
);
2728 CharDriverState
*qemu_chr_open(const char *filename
)
2732 if (!strcmp(filename
, "vc")) {
2733 return text_console_init(&display_state
);
2734 } else if (!strcmp(filename
, "null")) {
2735 return qemu_chr_open_null();
2737 if (strstart(filename
, "tcp:", &p
)) {
2738 return qemu_chr_open_tcp(p
, 0, 0);
2740 if (strstart(filename
, "telnet:", &p
)) {
2741 return qemu_chr_open_tcp(p
, 1, 0);
2743 if (strstart(filename
, "udp:", &p
)) {
2744 return qemu_chr_open_udp(p
);
2747 if (strstart(filename
, "unix:", &p
)) {
2748 return qemu_chr_open_tcp(p
, 0, 1);
2749 } else if (strstart(filename
, "file:", &p
)) {
2750 return qemu_chr_open_file_out(p
);
2751 } else if (strstart(filename
, "pipe:", &p
)) {
2752 return qemu_chr_open_pipe(p
);
2753 } else if (!strcmp(filename
, "pty")) {
2754 return qemu_chr_open_pty();
2755 } else if (!strcmp(filename
, "stdio")) {
2756 return qemu_chr_open_stdio();
2759 #if defined(__linux__)
2760 if (strstart(filename
, "/dev/parport", NULL
)) {
2761 return qemu_chr_open_pp(filename
);
2763 if (strstart(filename
, "/dev/", NULL
)) {
2764 return qemu_chr_open_tty(filename
);
2768 if (strstart(filename
, "COM", NULL
)) {
2769 return qemu_chr_open_win(filename
);
2771 if (strstart(filename
, "pipe:", &p
)) {
2772 return qemu_chr_open_win_pipe(p
);
2774 if (strstart(filename
, "file:", &p
)) {
2775 return qemu_chr_open_win_file_out(p
);
2783 void qemu_chr_close(CharDriverState
*chr
)
2786 chr
->chr_close(chr
);
2789 /***********************************************************/
2790 /* network device redirectors */
2792 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
2796 for(i
=0;i
<size
;i
+=16) {
2800 fprintf(f
, "%08x ", i
);
2803 fprintf(f
, " %02x", buf
[i
+j
]);
2808 for(j
=0;j
<len
;j
++) {
2810 if (c
< ' ' || c
> '~')
2812 fprintf(f
, "%c", c
);
2818 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
2821 for(i
= 0; i
< 6; i
++) {
2822 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2835 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
2840 p1
= strchr(p
, sep
);
2846 if (len
> buf_size
- 1)
2848 memcpy(buf
, p
, len
);
2855 int parse_host_src_port(struct sockaddr_in
*haddr
,
2856 struct sockaddr_in
*saddr
,
2857 const char *input_str
)
2859 char *str
= strdup(input_str
);
2860 char *host_str
= str
;
2865 * Chop off any extra arguments at the end of the string which
2866 * would start with a comma, then fill in the src port information
2867 * if it was provided else use the "any address" and "any port".
2869 if ((ptr
= strchr(str
,',')))
2872 if ((src_str
= strchr(input_str
,'@'))) {
2877 if (parse_host_port(haddr
, host_str
) < 0)
2880 if (!src_str
|| *src_str
== '\0')
2883 if (parse_host_port(saddr
, src_str
) < 0)
2894 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
2902 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
2904 saddr
->sin_family
= AF_INET
;
2905 if (buf
[0] == '\0') {
2906 saddr
->sin_addr
.s_addr
= 0;
2908 if (isdigit(buf
[0])) {
2909 if (!inet_aton(buf
, &saddr
->sin_addr
))
2912 if ((he
= gethostbyname(buf
)) == NULL
)
2914 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
2917 port
= strtol(p
, (char **)&r
, 0);
2920 saddr
->sin_port
= htons(port
);
2925 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
2930 len
= MIN(108, strlen(str
));
2931 p
= strchr(str
, ',');
2933 len
= MIN(len
, p
- str
);
2935 memset(uaddr
, 0, sizeof(*uaddr
));
2937 uaddr
->sun_family
= AF_UNIX
;
2938 memcpy(uaddr
->sun_path
, str
, len
);
2944 /* find or alloc a new VLAN */
2945 VLANState
*qemu_find_vlan(int id
)
2947 VLANState
**pvlan
, *vlan
;
2948 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2952 vlan
= qemu_mallocz(sizeof(VLANState
));
2957 pvlan
= &first_vlan
;
2958 while (*pvlan
!= NULL
)
2959 pvlan
= &(*pvlan
)->next
;
2964 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
2965 IOReadHandler
*fd_read
,
2966 IOCanRWHandler
*fd_can_read
,
2969 VLANClientState
*vc
, **pvc
;
2970 vc
= qemu_mallocz(sizeof(VLANClientState
));
2973 vc
->fd_read
= fd_read
;
2974 vc
->fd_can_read
= fd_can_read
;
2975 vc
->opaque
= opaque
;
2979 pvc
= &vlan
->first_client
;
2980 while (*pvc
!= NULL
)
2981 pvc
= &(*pvc
)->next
;
2986 int qemu_can_send_packet(VLANClientState
*vc1
)
2988 VLANState
*vlan
= vc1
->vlan
;
2989 VLANClientState
*vc
;
2991 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2993 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
3000 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
3002 VLANState
*vlan
= vc1
->vlan
;
3003 VLANClientState
*vc
;
3006 printf("vlan %d send:\n", vlan
->id
);
3007 hex_dump(stdout
, buf
, size
);
3009 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
3011 vc
->fd_read(vc
->opaque
, buf
, size
);
3016 #if defined(CONFIG_SLIRP)
3018 /* slirp network adapter */
3020 static int slirp_inited
;
3021 static VLANClientState
*slirp_vc
;
3023 int slirp_can_output(void)
3025 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
3028 void slirp_output(const uint8_t *pkt
, int pkt_len
)
3031 printf("slirp output:\n");
3032 hex_dump(stdout
, pkt
, pkt_len
);
3036 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
3039 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
3042 printf("slirp input:\n");
3043 hex_dump(stdout
, buf
, size
);
3045 slirp_input(buf
, size
);
3048 static int net_slirp_init(VLANState
*vlan
)
3050 if (!slirp_inited
) {
3054 slirp_vc
= qemu_new_vlan_client(vlan
,
3055 slirp_receive
, NULL
, NULL
);
3056 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
3060 static void net_slirp_redir(const char *redir_str
)
3065 struct in_addr guest_addr
;
3066 int host_port
, guest_port
;
3068 if (!slirp_inited
) {
3074 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3076 if (!strcmp(buf
, "tcp")) {
3078 } else if (!strcmp(buf
, "udp")) {
3084 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3086 host_port
= strtol(buf
, &r
, 0);
3090 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
3092 if (buf
[0] == '\0') {
3093 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
3095 if (!inet_aton(buf
, &guest_addr
))
3098 guest_port
= strtol(p
, &r
, 0);
3102 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
3103 fprintf(stderr
, "qemu: could not set up redirection\n");
3108 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3116 static void smb_exit(void)
3120 char filename
[1024];
3122 /* erase all the files in the directory */
3123 d
= opendir(smb_dir
);
3128 if (strcmp(de
->d_name
, ".") != 0 &&
3129 strcmp(de
->d_name
, "..") != 0) {
3130 snprintf(filename
, sizeof(filename
), "%s/%s",
3131 smb_dir
, de
->d_name
);
3139 /* automatic user mode samba server configuration */
3140 void net_slirp_smb(const char *exported_dir
)
3142 char smb_conf
[1024];
3143 char smb_cmdline
[1024];
3146 if (!slirp_inited
) {
3151 /* XXX: better tmp dir construction */
3152 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
3153 if (mkdir(smb_dir
, 0700) < 0) {
3154 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
3157 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
3159 f
= fopen(smb_conf
, "w");
3161 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
3168 "socket address=127.0.0.1\n"
3169 "pid directory=%s\n"
3170 "lock directory=%s\n"
3171 "log file=%s/log.smbd\n"
3172 "smb passwd file=%s/smbpasswd\n"
3173 "security = share\n"
3188 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
3189 SMBD_COMMAND
, smb_conf
);
3191 slirp_add_exec(0, smb_cmdline
, 4, 139);
3194 #endif /* !defined(_WIN32) */
3196 #endif /* CONFIG_SLIRP */
3198 #if !defined(_WIN32)
3200 typedef struct TAPState
{
3201 VLANClientState
*vc
;
3205 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
3207 TAPState
*s
= opaque
;
3210 ret
= write(s
->fd
, buf
, size
);
3211 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
3218 static void tap_send(void *opaque
)
3220 TAPState
*s
= opaque
;
3224 size
= read(s
->fd
, buf
, sizeof(buf
));
3226 qemu_send_packet(s
->vc
, buf
, size
);
3232 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
3236 s
= qemu_mallocz(sizeof(TAPState
));
3240 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
3241 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
3242 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
3247 static int tap_open(char *ifname
, int ifname_size
)
3253 fd
= open("/dev/tap", O_RDWR
);
3255 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
3260 dev
= devname(s
.st_rdev
, S_IFCHR
);
3261 pstrcpy(ifname
, ifname_size
, dev
);
3263 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3266 #elif defined(__sun__)
3267 static int tap_open(char *ifname
, int ifname_size
)
3269 fprintf(stderr
, "warning: tap_open not yet implemented\n");
3273 static int tap_open(char *ifname
, int ifname_size
)
3278 fd
= open("/dev/net/tun", O_RDWR
);
3280 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
3283 memset(&ifr
, 0, sizeof(ifr
));
3284 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
3285 if (ifname
[0] != '\0')
3286 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
3288 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
3289 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
3291 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
3295 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
3296 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
3301 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
3302 const char *setup_script
)
3305 int pid
, status
, fd
;
3310 if (ifname1
!= NULL
)
3311 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
3314 fd
= tap_open(ifname
, sizeof(ifname
));
3320 if (setup_script
[0] != '\0') {
3321 /* try to launch network init script */
3326 *parg
++ = (char *)setup_script
;
3329 execv(setup_script
, args
);
3332 while (waitpid(pid
, &status
, 0) != pid
);
3333 if (!WIFEXITED(status
) ||
3334 WEXITSTATUS(status
) != 0) {
3335 fprintf(stderr
, "%s: could not launch network script\n",
3341 s
= net_tap_fd_init(vlan
, fd
);
3344 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3345 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
3349 #endif /* !_WIN32 */
3351 /* network connection */
3352 typedef struct NetSocketState
{
3353 VLANClientState
*vc
;
3355 int state
; /* 0 = getting length, 1 = getting data */
3359 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
3362 typedef struct NetSocketListenState
{
3365 } NetSocketListenState
;
3367 /* XXX: we consider we can send the whole packet without blocking */
3368 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
3370 NetSocketState
*s
= opaque
;
3374 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
3375 send_all(s
->fd
, buf
, size
);
3378 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
3380 NetSocketState
*s
= opaque
;
3381 sendto(s
->fd
, buf
, size
, 0,
3382 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
3385 static void net_socket_send(void *opaque
)
3387 NetSocketState
*s
= opaque
;
3392 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
3394 err
= socket_error();
3395 if (err
!= EWOULDBLOCK
)
3397 } else if (size
== 0) {
3398 /* end of connection */
3400 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3406 /* reassemble a packet from the network */
3412 memcpy(s
->buf
+ s
->index
, buf
, l
);
3416 if (s
->index
== 4) {
3418 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
3424 l
= s
->packet_len
- s
->index
;
3427 memcpy(s
->buf
+ s
->index
, buf
, l
);
3431 if (s
->index
>= s
->packet_len
) {
3432 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
3441 static void net_socket_send_dgram(void *opaque
)
3443 NetSocketState
*s
= opaque
;
3446 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
3450 /* end of connection */
3451 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
3454 qemu_send_packet(s
->vc
, s
->buf
, size
);
3457 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
3462 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
3463 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
3464 inet_ntoa(mcastaddr
->sin_addr
),
3465 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
3469 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
3471 perror("socket(PF_INET, SOCK_DGRAM)");
3476 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
3477 (const char *)&val
, sizeof(val
));
3479 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
3483 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
3489 /* Add host to multicast group */
3490 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
3491 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
3493 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
3494 (const char *)&imr
, sizeof(struct ip_mreq
));
3496 perror("setsockopt(IP_ADD_MEMBERSHIP)");
3500 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
3502 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
3503 (const char *)&val
, sizeof(val
));
3505 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
3509 socket_set_nonblock(fd
);
3517 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
3520 struct sockaddr_in saddr
;
3522 socklen_t saddr_len
;
3525 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
3526 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
3527 * by ONLY ONE process: we must "clone" this dgram socket --jjo
3531 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
3533 if (saddr
.sin_addr
.s_addr
==0) {
3534 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
3538 /* clone dgram socket */
3539 newfd
= net_socket_mcast_create(&saddr
);
3541 /* error already reported by net_socket_mcast_create() */
3545 /* clone newfd to fd, close newfd */
3550 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
3551 fd
, strerror(errno
));
3556 s
= qemu_mallocz(sizeof(NetSocketState
));
3561 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
3562 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
3564 /* mcast: save bound address as dst */
3565 if (is_connected
) s
->dgram_dst
=saddr
;
3567 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3568 "socket: fd=%d (%s mcast=%s:%d)",
3569 fd
, is_connected
? "cloned" : "",
3570 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3574 static void net_socket_connect(void *opaque
)
3576 NetSocketState
*s
= opaque
;
3577 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
3580 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
3584 s
= qemu_mallocz(sizeof(NetSocketState
));
3588 s
->vc
= qemu_new_vlan_client(vlan
,
3589 net_socket_receive
, NULL
, s
);
3590 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3591 "socket: fd=%d", fd
);
3593 net_socket_connect(s
);
3595 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
3600 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
3603 int so_type
=-1, optlen
=sizeof(so_type
);
3605 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
3606 fprintf(stderr
, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd
);
3611 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
3613 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3615 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
3616 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
3617 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
3622 static void net_socket_accept(void *opaque
)
3624 NetSocketListenState
*s
= opaque
;
3626 struct sockaddr_in saddr
;
3631 len
= sizeof(saddr
);
3632 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
3633 if (fd
< 0 && errno
!= EINTR
) {
3635 } else if (fd
>= 0) {
3639 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
3643 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
3644 "socket: connection from %s:%d",
3645 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3649 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
3651 NetSocketListenState
*s
;
3653 struct sockaddr_in saddr
;
3655 if (parse_host_port(&saddr
, host_str
) < 0)
3658 s
= qemu_mallocz(sizeof(NetSocketListenState
));
3662 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3667 socket_set_nonblock(fd
);
3669 /* allow fast reuse */
3671 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
3673 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3678 ret
= listen(fd
, 0);
3685 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
3689 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
3692 int fd
, connected
, ret
, err
;
3693 struct sockaddr_in saddr
;
3695 if (parse_host_port(&saddr
, host_str
) < 0)
3698 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3703 socket_set_nonblock(fd
);
3707 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
3709 err
= socket_error();
3710 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
3711 } else if (err
== EINPROGRESS
) {
3723 s
= net_socket_fd_init(vlan
, fd
, connected
);
3726 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3727 "socket: connect to %s:%d",
3728 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3732 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
3736 struct sockaddr_in saddr
;
3738 if (parse_host_port(&saddr
, host_str
) < 0)
3742 fd
= net_socket_mcast_create(&saddr
);
3746 s
= net_socket_fd_init(vlan
, fd
, 0);
3750 s
->dgram_dst
= saddr
;
3752 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3753 "socket: mcast=%s:%d",
3754 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
3759 static int get_param_value(char *buf
, int buf_size
,
3760 const char *tag
, const char *str
)
3769 while (*p
!= '\0' && *p
!= '=') {
3770 if ((q
- option
) < sizeof(option
) - 1)
3778 if (!strcmp(tag
, option
)) {
3780 while (*p
!= '\0' && *p
!= ',') {
3781 if ((q
- buf
) < buf_size
- 1)
3788 while (*p
!= '\0' && *p
!= ',') {
3799 static int net_client_init(const char *str
)
3810 while (*p
!= '\0' && *p
!= ',') {
3811 if ((q
- device
) < sizeof(device
) - 1)
3819 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
3820 vlan_id
= strtol(buf
, NULL
, 0);
3822 vlan
= qemu_find_vlan(vlan_id
);
3824 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
3827 if (!strcmp(device
, "nic")) {
3831 if (nb_nics
>= MAX_NICS
) {
3832 fprintf(stderr
, "Too Many NICs\n");
3835 nd
= &nd_table
[nb_nics
];
3836 macaddr
= nd
->macaddr
;
3842 macaddr
[5] = 0x56 + nb_nics
;
3844 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
3845 if (parse_macaddr(macaddr
, buf
) < 0) {
3846 fprintf(stderr
, "invalid syntax for ethernet address\n");
3850 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
3851 nd
->model
= strdup(buf
);
3857 if (!strcmp(device
, "none")) {
3858 /* does nothing. It is needed to signal that no network cards
3863 if (!strcmp(device
, "user")) {
3864 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
3865 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
3867 ret
= net_slirp_init(vlan
);
3871 if (!strcmp(device
, "tap")) {
3873 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
3874 fprintf(stderr
, "tap: no interface name\n");
3877 ret
= tap_win32_init(vlan
, ifname
);
3880 if (!strcmp(device
, "tap")) {
3882 char setup_script
[1024];
3884 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3885 fd
= strtol(buf
, NULL
, 0);
3887 if (net_tap_fd_init(vlan
, fd
))
3890 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
3893 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
3894 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
3896 ret
= net_tap_init(vlan
, ifname
, setup_script
);
3900 if (!strcmp(device
, "socket")) {
3901 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
3903 fd
= strtol(buf
, NULL
, 0);
3905 if (net_socket_fd_init(vlan
, fd
, 1))
3907 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
3908 ret
= net_socket_listen_init(vlan
, buf
);
3909 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
3910 ret
= net_socket_connect_init(vlan
, buf
);
3911 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
3912 ret
= net_socket_mcast_init(vlan
, buf
);
3914 fprintf(stderr
, "Unknown socket options: %s\n", p
);
3919 fprintf(stderr
, "Unknown network device: %s\n", device
);
3923 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
3929 void do_info_network(void)
3932 VLANClientState
*vc
;
3934 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3935 term_printf("VLAN %d devices:\n", vlan
->id
);
3936 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3937 term_printf(" %s\n", vc
->info_str
);
3941 /***********************************************************/
3944 static USBPort
*used_usb_ports
;
3945 static USBPort
*free_usb_ports
;
3947 /* ??? Maybe change this to register a hub to keep track of the topology. */
3948 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
3949 usb_attachfn attach
)
3951 port
->opaque
= opaque
;
3952 port
->index
= index
;
3953 port
->attach
= attach
;
3954 port
->next
= free_usb_ports
;
3955 free_usb_ports
= port
;
3958 static int usb_device_add(const char *devname
)
3964 if (!free_usb_ports
)
3967 if (strstart(devname
, "host:", &p
)) {
3968 dev
= usb_host_device_open(p
);
3969 } else if (!strcmp(devname
, "mouse")) {
3970 dev
= usb_mouse_init();
3971 } else if (!strcmp(devname
, "tablet")) {
3972 dev
= usb_tablet_init();
3973 } else if (strstart(devname
, "disk:", &p
)) {
3974 dev
= usb_msd_init(p
);
3981 /* Find a USB port to add the device to. */
3982 port
= free_usb_ports
;
3986 /* Create a new hub and chain it on. */
3987 free_usb_ports
= NULL
;
3988 port
->next
= used_usb_ports
;
3989 used_usb_ports
= port
;
3991 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
3992 usb_attach(port
, hub
);
3993 port
= free_usb_ports
;
3996 free_usb_ports
= port
->next
;
3997 port
->next
= used_usb_ports
;
3998 used_usb_ports
= port
;
3999 usb_attach(port
, dev
);
4003 static int usb_device_del(const char *devname
)
4011 if (!used_usb_ports
)
4014 p
= strchr(devname
, '.');
4017 bus_num
= strtoul(devname
, NULL
, 0);
4018 addr
= strtoul(p
+ 1, NULL
, 0);
4022 lastp
= &used_usb_ports
;
4023 port
= used_usb_ports
;
4024 while (port
&& port
->dev
->addr
!= addr
) {
4025 lastp
= &port
->next
;
4033 *lastp
= port
->next
;
4034 usb_attach(port
, NULL
);
4035 dev
->handle_destroy(dev
);
4036 port
->next
= free_usb_ports
;
4037 free_usb_ports
= port
;
4041 void do_usb_add(const char *devname
)
4044 ret
= usb_device_add(devname
);
4046 term_printf("Could not add USB device '%s'\n", devname
);
4049 void do_usb_del(const char *devname
)
4052 ret
= usb_device_del(devname
);
4054 term_printf("Could not remove USB device '%s'\n", devname
);
4061 const char *speed_str
;
4064 term_printf("USB support not enabled\n");
4068 for (port
= used_usb_ports
; port
; port
= port
->next
) {
4072 switch(dev
->speed
) {
4076 case USB_SPEED_FULL
:
4079 case USB_SPEED_HIGH
:
4086 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
4087 0, dev
->addr
, speed_str
, dev
->devname
);
4091 /***********************************************************/
4094 static char *pid_filename
;
4096 /* Remove PID file. Called on normal exit */
4098 static void remove_pidfile(void)
4100 unlink (pid_filename
);
4103 static void create_pidfile(const char *filename
)
4105 struct stat pidstat
;
4108 /* Try to write our PID to the named file */
4109 if (stat(filename
, &pidstat
) < 0) {
4110 if (errno
== ENOENT
) {
4111 if ((f
= fopen (filename
, "w")) == NULL
) {
4112 perror("Opening pidfile");
4115 fprintf(f
, "%d\n", getpid());
4117 pid_filename
= qemu_strdup(filename
);
4118 if (!pid_filename
) {
4119 fprintf(stderr
, "Could not save PID filename");
4122 atexit(remove_pidfile
);
4125 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
4131 /***********************************************************/
4134 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
4138 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
4142 static void dumb_refresh(DisplayState
*ds
)
4147 void dumb_display_init(DisplayState
*ds
)
4152 ds
->dpy_update
= dumb_update
;
4153 ds
->dpy_resize
= dumb_resize
;
4154 ds
->dpy_refresh
= dumb_refresh
;
4157 /***********************************************************/
4160 #define MAX_IO_HANDLERS 64
4162 typedef struct IOHandlerRecord
{
4164 IOCanRWHandler
*fd_read_poll
;
4166 IOHandler
*fd_write
;
4168 /* temporary data */
4170 struct IOHandlerRecord
*next
;
4173 static IOHandlerRecord
*first_io_handler
;
4175 /* XXX: fd_read_poll should be suppressed, but an API change is
4176 necessary in the character devices to suppress fd_can_read(). */
4177 int qemu_set_fd_handler2(int fd
,
4178 IOCanRWHandler
*fd_read_poll
,
4180 IOHandler
*fd_write
,
4183 IOHandlerRecord
**pioh
, *ioh
;
4185 if (!fd_read
&& !fd_write
) {
4186 pioh
= &first_io_handler
;
4191 if (ioh
->fd
== fd
) {
4199 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4203 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
4206 ioh
->next
= first_io_handler
;
4207 first_io_handler
= ioh
;
4210 ioh
->fd_read_poll
= fd_read_poll
;
4211 ioh
->fd_read
= fd_read
;
4212 ioh
->fd_write
= fd_write
;
4213 ioh
->opaque
= opaque
;
4218 int qemu_set_fd_handler(int fd
,
4220 IOHandler
*fd_write
,
4223 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
4226 /***********************************************************/
4227 /* Polling handling */
4229 typedef struct PollingEntry
{
4232 struct PollingEntry
*next
;
4235 static PollingEntry
*first_polling_entry
;
4237 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
4239 PollingEntry
**ppe
, *pe
;
4240 pe
= qemu_mallocz(sizeof(PollingEntry
));
4244 pe
->opaque
= opaque
;
4245 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
4250 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
4252 PollingEntry
**ppe
, *pe
;
4253 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
4255 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
4264 /***********************************************************/
4265 /* Wait objects support */
4266 typedef struct WaitObjects
{
4268 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
4269 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
4270 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
4273 static WaitObjects wait_objects
= {0};
4275 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4277 WaitObjects
*w
= &wait_objects
;
4279 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
4281 w
->events
[w
->num
] = handle
;
4282 w
->func
[w
->num
] = func
;
4283 w
->opaque
[w
->num
] = opaque
;
4288 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
4291 WaitObjects
*w
= &wait_objects
;
4294 for (i
= 0; i
< w
->num
; i
++) {
4295 if (w
->events
[i
] == handle
)
4298 w
->events
[i
] = w
->events
[i
+ 1];
4299 w
->func
[i
] = w
->func
[i
+ 1];
4300 w
->opaque
[i
] = w
->opaque
[i
+ 1];
4308 /***********************************************************/
4309 /* savevm/loadvm support */
4311 #define IO_BUF_SIZE 32768
4315 BlockDriverState
*bs
;
4318 int64_t base_offset
;
4319 int64_t buf_offset
; /* start of buffer when writing, end of buffer
4322 int buf_size
; /* 0 when writing */
4323 uint8_t buf
[IO_BUF_SIZE
];
4326 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
4330 f
= qemu_mallocz(sizeof(QEMUFile
));
4333 if (!strcmp(mode
, "wb")) {
4335 } else if (!strcmp(mode
, "rb")) {
4340 f
->outfile
= fopen(filename
, mode
);
4352 QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
4356 f
= qemu_mallocz(sizeof(QEMUFile
));
4361 f
->is_writable
= is_writable
;
4362 f
->base_offset
= offset
;
4366 void qemu_fflush(QEMUFile
*f
)
4368 if (!f
->is_writable
)
4370 if (f
->buf_index
> 0) {
4372 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4373 fwrite(f
->buf
, 1, f
->buf_index
, f
->outfile
);
4375 bdrv_pwrite(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4376 f
->buf
, f
->buf_index
);
4378 f
->buf_offset
+= f
->buf_index
;
4383 static void qemu_fill_buffer(QEMUFile
*f
)
4390 fseek(f
->outfile
, f
->buf_offset
, SEEK_SET
);
4391 len
= fread(f
->buf
, 1, IO_BUF_SIZE
, f
->outfile
);
4395 len
= bdrv_pread(f
->bs
, f
->base_offset
+ f
->buf_offset
,
4396 f
->buf
, IO_BUF_SIZE
);
4402 f
->buf_offset
+= len
;
4405 void qemu_fclose(QEMUFile
*f
)
4415 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
4419 l
= IO_BUF_SIZE
- f
->buf_index
;
4422 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
4426 if (f
->buf_index
>= IO_BUF_SIZE
)
4431 void qemu_put_byte(QEMUFile
*f
, int v
)
4433 f
->buf
[f
->buf_index
++] = v
;
4434 if (f
->buf_index
>= IO_BUF_SIZE
)
4438 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
4444 l
= f
->buf_size
- f
->buf_index
;
4446 qemu_fill_buffer(f
);
4447 l
= f
->buf_size
- f
->buf_index
;
4453 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
4458 return size1
- size
;
4461 int qemu_get_byte(QEMUFile
*f
)
4463 if (f
->buf_index
>= f
->buf_size
) {
4464 qemu_fill_buffer(f
);
4465 if (f
->buf_index
>= f
->buf_size
)
4468 return f
->buf
[f
->buf_index
++];
4471 int64_t qemu_ftell(QEMUFile
*f
)
4473 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
4476 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
4478 if (whence
== SEEK_SET
) {
4480 } else if (whence
== SEEK_CUR
) {
4481 pos
+= qemu_ftell(f
);
4483 /* SEEK_END not supported */
4486 if (f
->is_writable
) {
4488 f
->buf_offset
= pos
;
4490 f
->buf_offset
= pos
;
4497 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
4499 qemu_put_byte(f
, v
>> 8);
4500 qemu_put_byte(f
, v
);
4503 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
4505 qemu_put_byte(f
, v
>> 24);
4506 qemu_put_byte(f
, v
>> 16);
4507 qemu_put_byte(f
, v
>> 8);
4508 qemu_put_byte(f
, v
);
4511 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
4513 qemu_put_be32(f
, v
>> 32);
4514 qemu_put_be32(f
, v
);
4517 unsigned int qemu_get_be16(QEMUFile
*f
)
4520 v
= qemu_get_byte(f
) << 8;
4521 v
|= qemu_get_byte(f
);
4525 unsigned int qemu_get_be32(QEMUFile
*f
)
4528 v
= qemu_get_byte(f
) << 24;
4529 v
|= qemu_get_byte(f
) << 16;
4530 v
|= qemu_get_byte(f
) << 8;
4531 v
|= qemu_get_byte(f
);
4535 uint64_t qemu_get_be64(QEMUFile
*f
)
4538 v
= (uint64_t)qemu_get_be32(f
) << 32;
4539 v
|= qemu_get_be32(f
);
4543 typedef struct SaveStateEntry
{
4547 SaveStateHandler
*save_state
;
4548 LoadStateHandler
*load_state
;
4550 struct SaveStateEntry
*next
;
4553 static SaveStateEntry
*first_se
;
4555 int register_savevm(const char *idstr
,
4558 SaveStateHandler
*save_state
,
4559 LoadStateHandler
*load_state
,
4562 SaveStateEntry
*se
, **pse
;
4564 se
= qemu_malloc(sizeof(SaveStateEntry
));
4567 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
4568 se
->instance_id
= instance_id
;
4569 se
->version_id
= version_id
;
4570 se
->save_state
= save_state
;
4571 se
->load_state
= load_state
;
4572 se
->opaque
= opaque
;
4575 /* add at the end of list */
4577 while (*pse
!= NULL
)
4578 pse
= &(*pse
)->next
;
4583 #define QEMU_VM_FILE_MAGIC 0x5145564d
4584 #define QEMU_VM_FILE_VERSION 0x00000002
4586 int qemu_savevm_state(QEMUFile
*f
)
4590 int64_t cur_pos
, len_pos
, total_len_pos
;
4592 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
4593 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
4594 total_len_pos
= qemu_ftell(f
);
4595 qemu_put_be64(f
, 0); /* total size */
4597 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4599 len
= strlen(se
->idstr
);
4600 qemu_put_byte(f
, len
);
4601 qemu_put_buffer(f
, se
->idstr
, len
);
4603 qemu_put_be32(f
, se
->instance_id
);
4604 qemu_put_be32(f
, se
->version_id
);
4606 /* record size: filled later */
4607 len_pos
= qemu_ftell(f
);
4608 qemu_put_be32(f
, 0);
4610 se
->save_state(f
, se
->opaque
);
4612 /* fill record size */
4613 cur_pos
= qemu_ftell(f
);
4614 len
= cur_pos
- len_pos
- 4;
4615 qemu_fseek(f
, len_pos
, SEEK_SET
);
4616 qemu_put_be32(f
, len
);
4617 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4619 cur_pos
= qemu_ftell(f
);
4620 qemu_fseek(f
, total_len_pos
, SEEK_SET
);
4621 qemu_put_be64(f
, cur_pos
- total_len_pos
- 8);
4622 qemu_fseek(f
, cur_pos
, SEEK_SET
);
4628 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
4632 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
4633 if (!strcmp(se
->idstr
, idstr
) &&
4634 instance_id
== se
->instance_id
)
4640 int qemu_loadvm_state(QEMUFile
*f
)
4643 int len
, ret
, instance_id
, record_len
, version_id
;
4644 int64_t total_len
, end_pos
, cur_pos
;
4648 v
= qemu_get_be32(f
);
4649 if (v
!= QEMU_VM_FILE_MAGIC
)
4651 v
= qemu_get_be32(f
);
4652 if (v
!= QEMU_VM_FILE_VERSION
) {
4657 total_len
= qemu_get_be64(f
);
4658 end_pos
= total_len
+ qemu_ftell(f
);
4660 if (qemu_ftell(f
) >= end_pos
)
4662 len
= qemu_get_byte(f
);
4663 qemu_get_buffer(f
, idstr
, len
);
4665 instance_id
= qemu_get_be32(f
);
4666 version_id
= qemu_get_be32(f
);
4667 record_len
= qemu_get_be32(f
);
4669 printf("idstr=%s instance=0x%x version=%d len=%d\n",
4670 idstr
, instance_id
, version_id
, record_len
);
4672 cur_pos
= qemu_ftell(f
);
4673 se
= find_se(idstr
, instance_id
);
4675 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
4676 instance_id
, idstr
);
4678 ret
= se
->load_state(f
, se
->opaque
, version_id
);
4680 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
4681 instance_id
, idstr
);
4684 /* always seek to exact end of record */
4685 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
4692 /* device can contain snapshots */
4693 static int bdrv_can_snapshot(BlockDriverState
*bs
)
4696 !bdrv_is_removable(bs
) &&
4697 !bdrv_is_read_only(bs
));
4700 /* device must be snapshots in order to have a reliable snapshot */
4701 static int bdrv_has_snapshot(BlockDriverState
*bs
)
4704 !bdrv_is_removable(bs
) &&
4705 !bdrv_is_read_only(bs
));
4708 static BlockDriverState
*get_bs_snapshots(void)
4710 BlockDriverState
*bs
;
4714 return bs_snapshots
;
4715 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4717 if (bdrv_can_snapshot(bs
))
4726 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
4729 QEMUSnapshotInfo
*sn_tab
, *sn
;
4733 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
4736 for(i
= 0; i
< nb_sns
; i
++) {
4738 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
4748 void do_savevm(const char *name
)
4750 BlockDriverState
*bs
, *bs1
;
4751 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
4752 int must_delete
, ret
, i
;
4753 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
4755 int saved_vm_running
;
4762 bs
= get_bs_snapshots();
4764 term_printf("No block device can accept snapshots\n");
4768 /* ??? Should this occur after vm_stop? */
4771 saved_vm_running
= vm_running
;
4776 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
4781 memset(sn
, 0, sizeof(*sn
));
4783 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
4784 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
4787 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
4790 /* fill auxiliary fields */
4793 sn
->date_sec
= tb
.time
;
4794 sn
->date_nsec
= tb
.millitm
* 1000000;
4796 gettimeofday(&tv
, NULL
);
4797 sn
->date_sec
= tv
.tv_sec
;
4798 sn
->date_nsec
= tv
.tv_usec
* 1000;
4800 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
4802 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
4803 term_printf("Device %s does not support VM state snapshots\n",
4804 bdrv_get_device_name(bs
));
4808 /* save the VM state */
4809 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
4811 term_printf("Could not open VM state file\n");
4814 ret
= qemu_savevm_state(f
);
4815 sn
->vm_state_size
= qemu_ftell(f
);
4818 term_printf("Error %d while writing VM\n", ret
);
4822 /* create the snapshots */
4824 for(i
= 0; i
< MAX_DISKS
; i
++) {
4826 if (bdrv_has_snapshot(bs1
)) {
4828 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
4830 term_printf("Error while deleting snapshot on '%s'\n",
4831 bdrv_get_device_name(bs1
));
4834 ret
= bdrv_snapshot_create(bs1
, sn
);
4836 term_printf("Error while creating snapshot on '%s'\n",
4837 bdrv_get_device_name(bs1
));
4843 if (saved_vm_running
)
4847 void do_loadvm(const char *name
)
4849 BlockDriverState
*bs
, *bs1
;
4850 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
4853 int saved_vm_running
;
4855 bs
= get_bs_snapshots();
4857 term_printf("No block device supports snapshots\n");
4861 /* Flush all IO requests so they don't interfere with the new state. */
4864 saved_vm_running
= vm_running
;
4867 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4869 if (bdrv_has_snapshot(bs1
)) {
4870 ret
= bdrv_snapshot_goto(bs1
, name
);
4873 term_printf("Warning: ");
4876 term_printf("Snapshots not supported on device '%s'\n",
4877 bdrv_get_device_name(bs1
));
4880 term_printf("Could not find snapshot '%s' on device '%s'\n",
4881 name
, bdrv_get_device_name(bs1
));
4884 term_printf("Error %d while activating snapshot on '%s'\n",
4885 ret
, bdrv_get_device_name(bs1
));
4888 /* fatal on snapshot block device */
4895 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
4896 term_printf("Device %s does not support VM state snapshots\n",
4897 bdrv_get_device_name(bs
));
4901 /* restore the VM state */
4902 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
4904 term_printf("Could not open VM state file\n");
4907 ret
= qemu_loadvm_state(f
);
4910 term_printf("Error %d while loading VM state\n", ret
);
4913 if (saved_vm_running
)
4917 void do_delvm(const char *name
)
4919 BlockDriverState
*bs
, *bs1
;
4922 bs
= get_bs_snapshots();
4924 term_printf("No block device supports snapshots\n");
4928 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4930 if (bdrv_has_snapshot(bs1
)) {
4931 ret
= bdrv_snapshot_delete(bs1
, name
);
4933 if (ret
== -ENOTSUP
)
4934 term_printf("Snapshots not supported on device '%s'\n",
4935 bdrv_get_device_name(bs1
));
4937 term_printf("Error %d while deleting snapshot on '%s'\n",
4938 ret
, bdrv_get_device_name(bs1
));
4944 void do_info_snapshots(void)
4946 BlockDriverState
*bs
, *bs1
;
4947 QEMUSnapshotInfo
*sn_tab
, *sn
;
4951 bs
= get_bs_snapshots();
4953 term_printf("No available block device supports snapshots\n");
4956 term_printf("Snapshot devices:");
4957 for(i
= 0; i
<= MAX_DISKS
; i
++) {
4959 if (bdrv_has_snapshot(bs1
)) {
4961 term_printf(" %s", bdrv_get_device_name(bs1
));
4966 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
4968 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
4971 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
4972 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
4973 for(i
= 0; i
< nb_sns
; i
++) {
4975 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
4980 /***********************************************************/
4981 /* cpu save/restore */
4983 #if defined(TARGET_I386)
4985 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
4987 qemu_put_be32(f
, dt
->selector
);
4988 qemu_put_betl(f
, dt
->base
);
4989 qemu_put_be32(f
, dt
->limit
);
4990 qemu_put_be32(f
, dt
->flags
);
4993 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
4995 dt
->selector
= qemu_get_be32(f
);
4996 dt
->base
= qemu_get_betl(f
);
4997 dt
->limit
= qemu_get_be32(f
);
4998 dt
->flags
= qemu_get_be32(f
);
5001 void cpu_save(QEMUFile
*f
, void *opaque
)
5003 CPUState
*env
= opaque
;
5004 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
5008 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5009 qemu_put_betls(f
, &env
->regs
[i
]);
5010 qemu_put_betls(f
, &env
->eip
);
5011 qemu_put_betls(f
, &env
->eflags
);
5012 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
5013 qemu_put_be32s(f
, &hflags
);
5017 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
5019 for(i
= 0; i
< 8; i
++) {
5020 fptag
|= ((!env
->fptags
[i
]) << i
);
5023 qemu_put_be16s(f
, &fpuc
);
5024 qemu_put_be16s(f
, &fpus
);
5025 qemu_put_be16s(f
, &fptag
);
5027 #ifdef USE_X86LDOUBLE
5032 qemu_put_be16s(f
, &fpregs_format
);
5034 for(i
= 0; i
< 8; i
++) {
5035 #ifdef USE_X86LDOUBLE
5039 /* we save the real CPU data (in case of MMX usage only 'mant'
5040 contains the MMX register */
5041 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
5042 qemu_put_be64(f
, mant
);
5043 qemu_put_be16(f
, exp
);
5046 /* if we use doubles for float emulation, we save the doubles to
5047 avoid losing information in case of MMX usage. It can give
5048 problems if the image is restored on a CPU where long
5049 doubles are used instead. */
5050 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
5054 for(i
= 0; i
< 6; i
++)
5055 cpu_put_seg(f
, &env
->segs
[i
]);
5056 cpu_put_seg(f
, &env
->ldt
);
5057 cpu_put_seg(f
, &env
->tr
);
5058 cpu_put_seg(f
, &env
->gdt
);
5059 cpu_put_seg(f
, &env
->idt
);
5061 qemu_put_be32s(f
, &env
->sysenter_cs
);
5062 qemu_put_be32s(f
, &env
->sysenter_esp
);
5063 qemu_put_be32s(f
, &env
->sysenter_eip
);
5065 qemu_put_betls(f
, &env
->cr
[0]);
5066 qemu_put_betls(f
, &env
->cr
[2]);
5067 qemu_put_betls(f
, &env
->cr
[3]);
5068 qemu_put_betls(f
, &env
->cr
[4]);
5070 for(i
= 0; i
< 8; i
++)
5071 qemu_put_betls(f
, &env
->dr
[i
]);
5074 qemu_put_be32s(f
, &env
->a20_mask
);
5077 qemu_put_be32s(f
, &env
->mxcsr
);
5078 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5079 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5080 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5083 #ifdef TARGET_X86_64
5084 qemu_put_be64s(f
, &env
->efer
);
5085 qemu_put_be64s(f
, &env
->star
);
5086 qemu_put_be64s(f
, &env
->lstar
);
5087 qemu_put_be64s(f
, &env
->cstar
);
5088 qemu_put_be64s(f
, &env
->fmask
);
5089 qemu_put_be64s(f
, &env
->kernelgsbase
);
5091 qemu_put_be32s(f
, &env
->smbase
);
5094 #ifdef USE_X86LDOUBLE
5095 /* XXX: add that in a FPU generic layer */
5096 union x86_longdouble
{
5101 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
5102 #define EXPBIAS1 1023
5103 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
5104 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
5106 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
5110 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
5111 /* exponent + sign */
5112 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
5113 e
|= SIGND1(temp
) >> 16;
5118 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5120 CPUState
*env
= opaque
;
5123 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
5125 if (version_id
!= 3 && version_id
!= 4)
5127 for(i
= 0; i
< CPU_NB_REGS
; i
++)
5128 qemu_get_betls(f
, &env
->regs
[i
]);
5129 qemu_get_betls(f
, &env
->eip
);
5130 qemu_get_betls(f
, &env
->eflags
);
5131 qemu_get_be32s(f
, &hflags
);
5133 qemu_get_be16s(f
, &fpuc
);
5134 qemu_get_be16s(f
, &fpus
);
5135 qemu_get_be16s(f
, &fptag
);
5136 qemu_get_be16s(f
, &fpregs_format
);
5138 /* NOTE: we cannot always restore the FPU state if the image come
5139 from a host with a different 'USE_X86LDOUBLE' define. We guess
5140 if we are in an MMX state to restore correctly in that case. */
5141 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
5142 for(i
= 0; i
< 8; i
++) {
5146 switch(fpregs_format
) {
5148 mant
= qemu_get_be64(f
);
5149 exp
= qemu_get_be16(f
);
5150 #ifdef USE_X86LDOUBLE
5151 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5153 /* difficult case */
5155 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5157 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
5161 mant
= qemu_get_be64(f
);
5162 #ifdef USE_X86LDOUBLE
5164 union x86_longdouble
*p
;
5165 /* difficult case */
5166 p
= (void *)&env
->fpregs
[i
];
5171 fp64_to_fp80(p
, mant
);
5175 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
5184 /* XXX: restore FPU round state */
5185 env
->fpstt
= (fpus
>> 11) & 7;
5186 env
->fpus
= fpus
& ~0x3800;
5188 for(i
= 0; i
< 8; i
++) {
5189 env
->fptags
[i
] = (fptag
>> i
) & 1;
5192 for(i
= 0; i
< 6; i
++)
5193 cpu_get_seg(f
, &env
->segs
[i
]);
5194 cpu_get_seg(f
, &env
->ldt
);
5195 cpu_get_seg(f
, &env
->tr
);
5196 cpu_get_seg(f
, &env
->gdt
);
5197 cpu_get_seg(f
, &env
->idt
);
5199 qemu_get_be32s(f
, &env
->sysenter_cs
);
5200 qemu_get_be32s(f
, &env
->sysenter_esp
);
5201 qemu_get_be32s(f
, &env
->sysenter_eip
);
5203 qemu_get_betls(f
, &env
->cr
[0]);
5204 qemu_get_betls(f
, &env
->cr
[2]);
5205 qemu_get_betls(f
, &env
->cr
[3]);
5206 qemu_get_betls(f
, &env
->cr
[4]);
5208 for(i
= 0; i
< 8; i
++)
5209 qemu_get_betls(f
, &env
->dr
[i
]);
5212 qemu_get_be32s(f
, &env
->a20_mask
);
5214 qemu_get_be32s(f
, &env
->mxcsr
);
5215 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
5216 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
5217 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
5220 #ifdef TARGET_X86_64
5221 qemu_get_be64s(f
, &env
->efer
);
5222 qemu_get_be64s(f
, &env
->star
);
5223 qemu_get_be64s(f
, &env
->lstar
);
5224 qemu_get_be64s(f
, &env
->cstar
);
5225 qemu_get_be64s(f
, &env
->fmask
);
5226 qemu_get_be64s(f
, &env
->kernelgsbase
);
5228 if (version_id
>= 4)
5229 qemu_get_be32s(f
, &env
->smbase
);
5231 /* XXX: compute hflags from scratch, except for CPL and IIF */
5232 env
->hflags
= hflags
;
5237 #elif defined(TARGET_PPC)
5238 void cpu_save(QEMUFile
*f
, void *opaque
)
5242 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5247 #elif defined(TARGET_MIPS)
5248 void cpu_save(QEMUFile
*f
, void *opaque
)
5252 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5257 #elif defined(TARGET_SPARC)
5258 void cpu_save(QEMUFile
*f
, void *opaque
)
5260 CPUState
*env
= opaque
;
5264 for(i
= 0; i
< 8; i
++)
5265 qemu_put_betls(f
, &env
->gregs
[i
]);
5266 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5267 qemu_put_betls(f
, &env
->regbase
[i
]);
5270 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5276 qemu_put_be32(f
, u
.i
);
5279 qemu_put_betls(f
, &env
->pc
);
5280 qemu_put_betls(f
, &env
->npc
);
5281 qemu_put_betls(f
, &env
->y
);
5283 qemu_put_be32(f
, tmp
);
5284 qemu_put_betls(f
, &env
->fsr
);
5285 qemu_put_betls(f
, &env
->tbr
);
5286 #ifndef TARGET_SPARC64
5287 qemu_put_be32s(f
, &env
->wim
);
5289 for(i
= 0; i
< 16; i
++)
5290 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
5294 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5296 CPUState
*env
= opaque
;
5300 for(i
= 0; i
< 8; i
++)
5301 qemu_get_betls(f
, &env
->gregs
[i
]);
5302 for(i
= 0; i
< NWINDOWS
* 16; i
++)
5303 qemu_get_betls(f
, &env
->regbase
[i
]);
5306 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
5311 u
.i
= qemu_get_be32(f
);
5315 qemu_get_betls(f
, &env
->pc
);
5316 qemu_get_betls(f
, &env
->npc
);
5317 qemu_get_betls(f
, &env
->y
);
5318 tmp
= qemu_get_be32(f
);
5319 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
5320 correctly updated */
5322 qemu_get_betls(f
, &env
->fsr
);
5323 qemu_get_betls(f
, &env
->tbr
);
5324 #ifndef TARGET_SPARC64
5325 qemu_get_be32s(f
, &env
->wim
);
5327 for(i
= 0; i
< 16; i
++)
5328 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
5334 #elif defined(TARGET_ARM)
5336 /* ??? Need to implement these. */
5337 void cpu_save(QEMUFile
*f
, void *opaque
)
5341 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
5348 #warning No CPU save/restore functions
5352 /***********************************************************/
5353 /* ram save/restore */
5355 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
5359 v
= qemu_get_byte(f
);
5362 if (qemu_get_buffer(f
, buf
, len
) != len
)
5366 v
= qemu_get_byte(f
);
5367 memset(buf
, v
, len
);
5375 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
5379 if (qemu_get_be32(f
) != phys_ram_size
)
5381 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
5382 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
5389 #define BDRV_HASH_BLOCK_SIZE 1024
5390 #define IOBUF_SIZE 4096
5391 #define RAM_CBLOCK_MAGIC 0xfabe
5393 typedef struct RamCompressState
{
5396 uint8_t buf
[IOBUF_SIZE
];
5399 static int ram_compress_open(RamCompressState
*s
, QEMUFile
*f
)
5402 memset(s
, 0, sizeof(*s
));
5404 ret
= deflateInit2(&s
->zstream
, 1,
5406 9, Z_DEFAULT_STRATEGY
);
5409 s
->zstream
.avail_out
= IOBUF_SIZE
;
5410 s
->zstream
.next_out
= s
->buf
;
5414 static void ram_put_cblock(RamCompressState
*s
, const uint8_t *buf
, int len
)
5416 qemu_put_be16(s
->f
, RAM_CBLOCK_MAGIC
);
5417 qemu_put_be16(s
->f
, len
);
5418 qemu_put_buffer(s
->f
, buf
, len
);
5421 static int ram_compress_buf(RamCompressState
*s
, const uint8_t *buf
, int len
)
5425 s
->zstream
.avail_in
= len
;
5426 s
->zstream
.next_in
= (uint8_t *)buf
;
5427 while (s
->zstream
.avail_in
> 0) {
5428 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
5431 if (s
->zstream
.avail_out
== 0) {
5432 ram_put_cblock(s
, s
->buf
, IOBUF_SIZE
);
5433 s
->zstream
.avail_out
= IOBUF_SIZE
;
5434 s
->zstream
.next_out
= s
->buf
;
5440 static void ram_compress_close(RamCompressState
*s
)
5444 /* compress last bytes */
5446 ret
= deflate(&s
->zstream
, Z_FINISH
);
5447 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
5448 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
5450 ram_put_cblock(s
, s
->buf
, len
);
5452 s
->zstream
.avail_out
= IOBUF_SIZE
;
5453 s
->zstream
.next_out
= s
->buf
;
5454 if (ret
== Z_STREAM_END
)
5461 deflateEnd(&s
->zstream
);
5464 typedef struct RamDecompressState
{
5467 uint8_t buf
[IOBUF_SIZE
];
5468 } RamDecompressState
;
5470 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
5473 memset(s
, 0, sizeof(*s
));
5475 ret
= inflateInit(&s
->zstream
);
5481 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
5485 s
->zstream
.avail_out
= len
;
5486 s
->zstream
.next_out
= buf
;
5487 while (s
->zstream
.avail_out
> 0) {
5488 if (s
->zstream
.avail_in
== 0) {
5489 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
5491 clen
= qemu_get_be16(s
->f
);
5492 if (clen
> IOBUF_SIZE
)
5494 qemu_get_buffer(s
->f
, s
->buf
, clen
);
5495 s
->zstream
.avail_in
= clen
;
5496 s
->zstream
.next_in
= s
->buf
;
5498 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
5499 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
5506 static void ram_decompress_close(RamDecompressState
*s
)
5508 inflateEnd(&s
->zstream
);
5511 static void ram_save(QEMUFile
*f
, void *opaque
)
5514 RamCompressState s1
, *s
= &s1
;
5517 qemu_put_be32(f
, phys_ram_size
);
5518 if (ram_compress_open(s
, f
) < 0)
5520 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5522 if (tight_savevm_enabled
) {
5526 /* find if the memory block is available on a virtual
5529 for(j
= 0; j
< MAX_DISKS
; j
++) {
5531 sector_num
= bdrv_hash_find(bs_table
[j
],
5532 phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5533 if (sector_num
>= 0)
5538 goto normal_compress
;
5541 cpu_to_be64wu((uint64_t *)(buf
+ 2), sector_num
);
5542 ram_compress_buf(s
, buf
, 10);
5548 ram_compress_buf(s
, buf
, 1);
5549 ram_compress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
);
5552 ram_compress_close(s
);
5555 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
5557 RamDecompressState s1
, *s
= &s1
;
5561 if (version_id
== 1)
5562 return ram_load_v1(f
, opaque
);
5563 if (version_id
!= 2)
5565 if (qemu_get_be32(f
) != phys_ram_size
)
5567 if (ram_decompress_open(s
, f
) < 0)
5569 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
5570 if (ram_decompress_buf(s
, buf
, 1) < 0) {
5571 fprintf(stderr
, "Error while reading ram block header\n");
5575 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
5576 fprintf(stderr
, "Error while reading ram block address=0x%08x", i
);
5585 ram_decompress_buf(s
, buf
+ 1, 9);
5587 sector_num
= be64_to_cpupu((const uint64_t *)(buf
+ 2));
5588 if (bs_index
>= MAX_DISKS
|| bs_table
[bs_index
] == NULL
) {
5589 fprintf(stderr
, "Invalid block device index %d\n", bs_index
);
5592 if (bdrv_read(bs_table
[bs_index
], sector_num
, phys_ram_base
+ i
,
5593 BDRV_HASH_BLOCK_SIZE
/ 512) < 0) {
5594 fprintf(stderr
, "Error while reading sector %d:%" PRId64
"\n",
5595 bs_index
, sector_num
);
5602 printf("Error block header\n");
5606 ram_decompress_close(s
);
5610 /***********************************************************/
5611 /* bottom halves (can be seen as timers which expire ASAP) */
5620 static QEMUBH
*first_bh
= NULL
;
5622 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
5625 bh
= qemu_mallocz(sizeof(QEMUBH
));
5629 bh
->opaque
= opaque
;
5633 int qemu_bh_poll(void)
5652 void qemu_bh_schedule(QEMUBH
*bh
)
5654 CPUState
*env
= cpu_single_env
;
5658 bh
->next
= first_bh
;
5661 /* stop the currently executing CPU to execute the BH ASAP */
5663 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
5667 void qemu_bh_cancel(QEMUBH
*bh
)
5670 if (bh
->scheduled
) {
5673 pbh
= &(*pbh
)->next
;
5679 void qemu_bh_delete(QEMUBH
*bh
)
5685 /***********************************************************/
5686 /* machine registration */
5688 QEMUMachine
*first_machine
= NULL
;
5690 int qemu_register_machine(QEMUMachine
*m
)
5693 pm
= &first_machine
;
5701 QEMUMachine
*find_machine(const char *name
)
5705 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5706 if (!strcmp(m
->name
, name
))
5712 /***********************************************************/
5713 /* main execution loop */
5715 void gui_update(void *opaque
)
5717 display_state
.dpy_refresh(&display_state
);
5718 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
5721 struct vm_change_state_entry
{
5722 VMChangeStateHandler
*cb
;
5724 LIST_ENTRY (vm_change_state_entry
) entries
;
5727 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
5729 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
5732 VMChangeStateEntry
*e
;
5734 e
= qemu_mallocz(sizeof (*e
));
5740 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
5744 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
5746 LIST_REMOVE (e
, entries
);
5750 static void vm_state_notify(int running
)
5752 VMChangeStateEntry
*e
;
5754 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
5755 e
->cb(e
->opaque
, running
);
5759 /* XXX: support several handlers */
5760 static VMStopHandler
*vm_stop_cb
;
5761 static void *vm_stop_opaque
;
5763 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
5766 vm_stop_opaque
= opaque
;
5770 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
5784 void vm_stop(int reason
)
5787 cpu_disable_ticks();
5791 vm_stop_cb(vm_stop_opaque
, reason
);
5798 /* reset/shutdown handler */
5800 typedef struct QEMUResetEntry
{
5801 QEMUResetHandler
*func
;
5803 struct QEMUResetEntry
*next
;
5806 static QEMUResetEntry
*first_reset_entry
;
5807 static int reset_requested
;
5808 static int shutdown_requested
;
5809 static int powerdown_requested
;
5811 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
5813 QEMUResetEntry
**pre
, *re
;
5815 pre
= &first_reset_entry
;
5816 while (*pre
!= NULL
)
5817 pre
= &(*pre
)->next
;
5818 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
5820 re
->opaque
= opaque
;
5825 static void qemu_system_reset(void)
5829 /* reset all devices */
5830 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
5831 re
->func(re
->opaque
);
5835 void qemu_system_reset_request(void)
5838 shutdown_requested
= 1;
5840 reset_requested
= 1;
5843 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5846 void qemu_system_shutdown_request(void)
5848 shutdown_requested
= 1;
5850 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5853 void qemu_system_powerdown_request(void)
5855 powerdown_requested
= 1;
5857 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
5860 void main_loop_wait(int timeout
)
5862 IOHandlerRecord
*ioh
, *ioh_next
;
5863 fd_set rfds
, wfds
, xfds
;
5869 /* XXX: need to suppress polling by better using win32 events */
5871 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
5872 ret
|= pe
->func(pe
->opaque
);
5875 if (ret
== 0 && timeout
> 0) {
5877 WaitObjects
*w
= &wait_objects
;
5879 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, timeout
);
5880 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
5881 if (w
->func
[ret
- WAIT_OBJECT_0
])
5882 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
5883 } else if (ret
== WAIT_TIMEOUT
) {
5885 err
= GetLastError();
5886 fprintf(stderr
, "Wait error %d %d\n", ret
, err
);
5890 /* poll any events */
5891 /* XXX: separate device handlers from system ones */
5896 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
5898 (!ioh
->fd_read_poll
||
5899 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
5900 FD_SET(ioh
->fd
, &rfds
);
5904 if (ioh
->fd_write
) {
5905 FD_SET(ioh
->fd
, &wfds
);
5915 tv
.tv_usec
= timeout
* 1000;
5917 #if defined(CONFIG_SLIRP)
5919 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
5922 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
5924 /* XXX: better handling of removal */
5925 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
5926 ioh_next
= ioh
->next
;
5927 if (FD_ISSET(ioh
->fd
, &rfds
)) {
5928 ioh
->fd_read(ioh
->opaque
);
5930 if (FD_ISSET(ioh
->fd
, &wfds
)) {
5931 ioh
->fd_write(ioh
->opaque
);
5935 #if defined(CONFIG_SLIRP)
5942 slirp_select_poll(&rfds
, &wfds
, &xfds
);
5949 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
5950 qemu_get_clock(vm_clock
));
5951 /* run dma transfers, if any */
5955 /* real time timers */
5956 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
5957 qemu_get_clock(rt_clock
));
5960 static CPUState
*cur_cpu
;
5965 #ifdef CONFIG_PROFILER
5970 cur_cpu
= first_cpu
;
5977 env
= env
->next_cpu
;
5980 #ifdef CONFIG_PROFILER
5981 ti
= profile_getclock();
5983 ret
= cpu_exec(env
);
5984 #ifdef CONFIG_PROFILER
5985 qemu_time
+= profile_getclock() - ti
;
5987 if (ret
!= EXCP_HALTED
)
5989 /* all CPUs are halted ? */
5990 if (env
== cur_cpu
) {
5997 if (shutdown_requested
) {
5998 ret
= EXCP_INTERRUPT
;
6001 if (reset_requested
) {
6002 reset_requested
= 0;
6003 qemu_system_reset();
6004 ret
= EXCP_INTERRUPT
;
6006 if (powerdown_requested
) {
6007 powerdown_requested
= 0;
6008 qemu_system_powerdown();
6009 ret
= EXCP_INTERRUPT
;
6011 if (ret
== EXCP_DEBUG
) {
6012 vm_stop(EXCP_DEBUG
);
6014 /* if hlt instruction, we wait until the next IRQ */
6015 /* XXX: use timeout computed from timers */
6016 if (ret
== EXCP_HLT
)
6023 #ifdef CONFIG_PROFILER
6024 ti
= profile_getclock();
6026 main_loop_wait(timeout
);
6027 #ifdef CONFIG_PROFILER
6028 dev_time
+= profile_getclock() - ti
;
6031 cpu_disable_ticks();
6037 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2006 Fabrice Bellard\n"
6038 "usage: %s [options] [disk_image]\n"
6040 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
6042 "Standard options:\n"
6043 "-M machine select emulated machine (-M ? for list)\n"
6044 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
6045 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
6046 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
6047 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
6048 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
6049 "-snapshot write to temporary files instead of disk image files\n"
6051 "-no-quit disable SDL window close capability\n"
6054 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
6056 "-m megs set virtual RAM size to megs MB [default=%d]\n"
6057 "-smp n set the number of CPUs to 'n' [default=1]\n"
6058 "-nographic disable graphical output and redirect serial I/Os to console\n"
6060 "-k language use keyboard layout (for example \"fr\" for French)\n"
6063 "-audio-help print list of audio drivers and their options\n"
6064 "-soundhw c1,... enable audio support\n"
6065 " and only specified sound cards (comma separated list)\n"
6066 " use -soundhw ? to get the list of supported cards\n"
6067 " use -soundhw all to enable all of them\n"
6069 "-localtime set the real time clock to local time [default=utc]\n"
6070 "-full-screen start in full screen\n"
6072 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
6074 "-usb enable the USB driver (will be the default soon)\n"
6075 "-usbdevice name add the host or guest USB device 'name'\n"
6076 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6077 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
6080 "Network options:\n"
6081 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
6082 " create a new Network Interface Card and connect it to VLAN 'n'\n"
6084 "-net user[,vlan=n][,hostname=host]\n"
6085 " connect the user mode network stack to VLAN 'n' and send\n"
6086 " hostname 'host' to DHCP clients\n"
6089 "-net tap[,vlan=n],ifname=name\n"
6090 " connect the host TAP network interface to VLAN 'n'\n"
6092 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
6093 " connect the host TAP network interface to VLAN 'n' and use\n"
6094 " the network script 'file' (default=%s);\n"
6095 " use 'fd=h' to connect to an already opened TAP interface\n"
6097 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
6098 " connect the vlan 'n' to another VLAN using a socket connection\n"
6099 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
6100 " connect the vlan 'n' to multicast maddr and port\n"
6101 "-net none use it alone to have zero network devices; if no -net option\n"
6102 " is provided, the default is '-net nic -net user'\n"
6105 "-tftp prefix allow tftp access to files starting with prefix [-net user]\n"
6107 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
6109 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
6110 " redirect TCP or UDP connections from host to guest [-net user]\n"
6113 "Linux boot specific:\n"
6114 "-kernel bzImage use 'bzImage' as kernel image\n"
6115 "-append cmdline use 'cmdline' as kernel command line\n"
6116 "-initrd file use 'file' as initial ram disk\n"
6118 "Debug/Expert options:\n"
6119 "-monitor dev redirect the monitor to char device 'dev'\n"
6120 "-serial dev redirect the serial port to char device 'dev'\n"
6121 "-parallel dev redirect the parallel port to char device 'dev'\n"
6122 "-pidfile file Write PID to 'file'\n"
6123 "-S freeze CPU at startup (use 'c' to start execution)\n"
6124 "-s wait gdb connection to port %d\n"
6125 "-p port change gdb connection port\n"
6126 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
6127 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
6128 " translation (t=none or lba) (usually qemu can guess them)\n"
6129 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
6131 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
6132 "-no-kqemu disable KQEMU kernel module usage\n"
6134 #ifdef USE_CODE_COPY
6135 "-no-code-copy disable code copy acceleration\n"
6138 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
6139 " (default is CL-GD5446 PCI VGA)\n"
6140 "-no-acpi disable ACPI\n"
6142 "-no-reboot exit instead of rebooting\n"
6143 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
6144 "-vnc display start a VNC server on display\n"
6146 "-daemonize daemonize QEMU after initializing\n"
6148 "-option-rom rom load a file, rom, into the option ROM space\n"
6150 "During emulation, the following keys are useful:\n"
6151 "ctrl-alt-f toggle full screen\n"
6152 "ctrl-alt-n switch to virtual console 'n'\n"
6153 "ctrl-alt toggle mouse and keyboard grab\n"
6155 "When using -nographic, press 'ctrl-a h' to get some help.\n"
6160 DEFAULT_NETWORK_SCRIPT
,
6162 DEFAULT_GDBSTUB_PORT
,
6167 #define HAS_ARG 0x0001
6181 QEMU_OPTION_snapshot
,
6183 QEMU_OPTION_no_fd_bootchk
,
6186 QEMU_OPTION_nographic
,
6188 QEMU_OPTION_audio_help
,
6189 QEMU_OPTION_soundhw
,
6207 QEMU_OPTION_no_code_copy
,
6209 QEMU_OPTION_localtime
,
6210 QEMU_OPTION_cirrusvga
,
6212 QEMU_OPTION_std_vga
,
6213 QEMU_OPTION_monitor
,
6215 QEMU_OPTION_parallel
,
6217 QEMU_OPTION_full_screen
,
6218 QEMU_OPTION_no_quit
,
6219 QEMU_OPTION_pidfile
,
6220 QEMU_OPTION_no_kqemu
,
6221 QEMU_OPTION_kernel_kqemu
,
6222 QEMU_OPTION_win2k_hack
,
6224 QEMU_OPTION_usbdevice
,
6227 QEMU_OPTION_no_acpi
,
6228 QEMU_OPTION_no_reboot
,
6229 QEMU_OPTION_daemonize
,
6230 QEMU_OPTION_option_rom
,
6231 QEMU_OPTION_semihosting
6234 typedef struct QEMUOption
{
6240 const QEMUOption qemu_options
[] = {
6241 { "h", 0, QEMU_OPTION_h
},
6243 { "M", HAS_ARG
, QEMU_OPTION_M
},
6244 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
6245 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
6246 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
6247 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
6248 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
6249 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
6250 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
6251 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
6252 { "snapshot", 0, QEMU_OPTION_snapshot
},
6254 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
6256 { "m", HAS_ARG
, QEMU_OPTION_m
},
6257 { "nographic", 0, QEMU_OPTION_nographic
},
6258 { "k", HAS_ARG
, QEMU_OPTION_k
},
6260 { "audio-help", 0, QEMU_OPTION_audio_help
},
6261 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
6264 { "net", HAS_ARG
, QEMU_OPTION_net
},
6266 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
6268 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
6270 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
6273 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
6274 { "append", HAS_ARG
, QEMU_OPTION_append
},
6275 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
6277 { "S", 0, QEMU_OPTION_S
},
6278 { "s", 0, QEMU_OPTION_s
},
6279 { "p", HAS_ARG
, QEMU_OPTION_p
},
6280 { "d", HAS_ARG
, QEMU_OPTION_d
},
6281 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
6282 { "L", HAS_ARG
, QEMU_OPTION_L
},
6283 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
6285 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
6286 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
6288 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
6289 { "g", 1, QEMU_OPTION_g
},
6291 { "localtime", 0, QEMU_OPTION_localtime
},
6292 { "std-vga", 0, QEMU_OPTION_std_vga
},
6293 { "monitor", 1, QEMU_OPTION_monitor
},
6294 { "serial", 1, QEMU_OPTION_serial
},
6295 { "parallel", 1, QEMU_OPTION_parallel
},
6296 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
6297 { "full-screen", 0, QEMU_OPTION_full_screen
},
6299 { "no-quit", 0, QEMU_OPTION_no_quit
},
6301 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
6302 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
6303 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
6304 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
6305 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
6307 /* temporary options */
6308 { "usb", 0, QEMU_OPTION_usb
},
6309 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
6310 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
6311 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
6312 { "daemonize", 0, QEMU_OPTION_daemonize
},
6313 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
6314 #if defined(TARGET_ARM)
6315 { "semihosting", 0, QEMU_OPTION_semihosting
},
6320 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
6322 /* this stack is only used during signal handling */
6323 #define SIGNAL_STACK_SIZE 32768
6325 static uint8_t *signal_stack
;
6329 /* password input */
6331 static BlockDriverState
*get_bdrv(int index
)
6333 BlockDriverState
*bs
;
6336 bs
= bs_table
[index
];
6337 } else if (index
< 6) {
6338 bs
= fd_table
[index
- 4];
6345 static void read_passwords(void)
6347 BlockDriverState
*bs
;
6351 for(i
= 0; i
< 6; i
++) {
6353 if (bs
&& bdrv_is_encrypted(bs
)) {
6354 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
6355 for(j
= 0; j
< 3; j
++) {
6356 monitor_readline("Password: ",
6357 1, password
, sizeof(password
));
6358 if (bdrv_set_key(bs
, password
) == 0)
6360 term_printf("invalid password\n");
6366 /* XXX: currently we cannot use simultaneously different CPUs */
6367 void register_machines(void)
6369 #if defined(TARGET_I386)
6370 qemu_register_machine(&pc_machine
);
6371 qemu_register_machine(&isapc_machine
);
6372 #elif defined(TARGET_PPC)
6373 qemu_register_machine(&heathrow_machine
);
6374 qemu_register_machine(&core99_machine
);
6375 qemu_register_machine(&prep_machine
);
6376 #elif defined(TARGET_MIPS)
6377 qemu_register_machine(&mips_machine
);
6378 qemu_register_machine(&mips_malta_machine
);
6379 #elif defined(TARGET_SPARC)
6380 #ifdef TARGET_SPARC64
6381 qemu_register_machine(&sun4u_machine
);
6383 qemu_register_machine(&sun4m_machine
);
6385 #elif defined(TARGET_ARM)
6386 qemu_register_machine(&integratorcp926_machine
);
6387 qemu_register_machine(&integratorcp1026_machine
);
6388 qemu_register_machine(&versatilepb_machine
);
6389 qemu_register_machine(&versatileab_machine
);
6390 qemu_register_machine(&realview_machine
);
6391 #elif defined(TARGET_SH4)
6392 qemu_register_machine(&shix_machine
);
6394 #error unsupported CPU
6399 struct soundhw soundhw
[] = {
6406 { .init_isa
= pcspk_audio_init
}
6411 "Creative Sound Blaster 16",
6414 { .init_isa
= SB16_init
}
6421 "Yamaha YMF262 (OPL3)",
6423 "Yamaha YM3812 (OPL2)",
6427 { .init_isa
= Adlib_init
}
6434 "Gravis Ultrasound GF1",
6437 { .init_isa
= GUS_init
}
6443 "ENSONIQ AudioPCI ES1370",
6446 { .init_pci
= es1370_init
}
6449 { NULL
, NULL
, 0, 0, { NULL
} }
6452 static void select_soundhw (const char *optarg
)
6456 if (*optarg
== '?') {
6459 printf ("Valid sound card names (comma separated):\n");
6460 for (c
= soundhw
; c
->name
; ++c
) {
6461 printf ("%-11s %s\n", c
->name
, c
->descr
);
6463 printf ("\n-soundhw all will enable all of the above\n");
6464 exit (*optarg
!= '?');
6472 if (!strcmp (optarg
, "all")) {
6473 for (c
= soundhw
; c
->name
; ++c
) {
6481 e
= strchr (p
, ',');
6482 l
= !e
? strlen (p
) : (size_t) (e
- p
);
6484 for (c
= soundhw
; c
->name
; ++c
) {
6485 if (!strncmp (c
->name
, p
, l
)) {
6494 "Unknown sound card name (too big to show)\n");
6497 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
6502 p
+= l
+ (e
!= NULL
);
6506 goto show_valid_cards
;
6512 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
6514 exit(STATUS_CONTROL_C_EXIT
);
6519 #define MAX_NET_CLIENTS 32
6521 int main(int argc
, char **argv
)
6523 #ifdef CONFIG_GDBSTUB
6524 int use_gdbstub
, gdbstub_port
;
6527 int snapshot
, linux_boot
;
6528 const char *initrd_filename
;
6529 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
6530 const char *kernel_filename
, *kernel_cmdline
;
6531 DisplayState
*ds
= &display_state
;
6532 int cyls
, heads
, secs
, translation
;
6533 int start_emulation
= 1;
6534 char net_clients
[MAX_NET_CLIENTS
][256];
6537 const char *r
, *optarg
;
6538 CharDriverState
*monitor_hd
;
6539 char monitor_device
[128];
6540 char serial_devices
[MAX_SERIAL_PORTS
][128];
6541 int serial_device_index
;
6542 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
6543 int parallel_device_index
;
6544 const char *loadvm
= NULL
;
6545 QEMUMachine
*machine
;
6546 char usb_devices
[MAX_USB_CMDLINE
][128];
6547 int usb_devices_index
;
6550 LIST_INIT (&vm_change_state_head
);
6553 struct sigaction act
;
6554 sigfillset(&act
.sa_mask
);
6556 act
.sa_handler
= SIG_IGN
;
6557 sigaction(SIGPIPE
, &act
, NULL
);
6560 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
6561 /* Note: cpu_interrupt() is currently not SMP safe, so we force
6562 QEMU to run on a single CPU */
6567 h
= GetCurrentProcess();
6568 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
6569 for(i
= 0; i
< 32; i
++) {
6570 if (mask
& (1 << i
))
6575 SetProcessAffinityMask(h
, mask
);
6581 register_machines();
6582 machine
= first_machine
;
6583 initrd_filename
= NULL
;
6584 for(i
= 0; i
< MAX_FD
; i
++)
6585 fd_filename
[i
] = NULL
;
6586 for(i
= 0; i
< MAX_DISKS
; i
++)
6587 hd_filename
[i
] = NULL
;
6588 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
6589 vga_ram_size
= VGA_RAM_SIZE
;
6590 bios_size
= BIOS_SIZE
;
6591 #ifdef CONFIG_GDBSTUB
6593 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
6597 kernel_filename
= NULL
;
6598 kernel_cmdline
= "";
6604 cyls
= heads
= secs
= 0;
6605 translation
= BIOS_ATA_TRANSLATION_AUTO
;
6606 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
6608 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
6609 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
6610 serial_devices
[i
][0] = '\0';
6611 serial_device_index
= 0;
6613 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
6614 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
6615 parallel_devices
[i
][0] = '\0';
6616 parallel_device_index
= 0;
6618 usb_devices_index
= 0;
6623 /* default mac address of the first network interface */
6631 hd_filename
[0] = argv
[optind
++];
6633 const QEMUOption
*popt
;
6636 popt
= qemu_options
;
6639 fprintf(stderr
, "%s: invalid option -- '%s'\n",
6643 if (!strcmp(popt
->name
, r
+ 1))
6647 if (popt
->flags
& HAS_ARG
) {
6648 if (optind
>= argc
) {
6649 fprintf(stderr
, "%s: option '%s' requires an argument\n",
6653 optarg
= argv
[optind
++];
6658 switch(popt
->index
) {
6660 machine
= find_machine(optarg
);
6663 printf("Supported machines are:\n");
6664 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
6665 printf("%-10s %s%s\n",
6667 m
== first_machine
? " (default)" : "");
6672 case QEMU_OPTION_initrd
:
6673 initrd_filename
= optarg
;
6675 case QEMU_OPTION_hda
:
6676 case QEMU_OPTION_hdb
:
6677 case QEMU_OPTION_hdc
:
6678 case QEMU_OPTION_hdd
:
6681 hd_index
= popt
->index
- QEMU_OPTION_hda
;
6682 hd_filename
[hd_index
] = optarg
;
6683 if (hd_index
== cdrom_index
)
6687 case QEMU_OPTION_snapshot
:
6690 case QEMU_OPTION_hdachs
:
6694 cyls
= strtol(p
, (char **)&p
, 0);
6695 if (cyls
< 1 || cyls
> 16383)
6700 heads
= strtol(p
, (char **)&p
, 0);
6701 if (heads
< 1 || heads
> 16)
6706 secs
= strtol(p
, (char **)&p
, 0);
6707 if (secs
< 1 || secs
> 63)
6711 if (!strcmp(p
, "none"))
6712 translation
= BIOS_ATA_TRANSLATION_NONE
;
6713 else if (!strcmp(p
, "lba"))
6714 translation
= BIOS_ATA_TRANSLATION_LBA
;
6715 else if (!strcmp(p
, "auto"))
6716 translation
= BIOS_ATA_TRANSLATION_AUTO
;
6719 } else if (*p
!= '\0') {
6721 fprintf(stderr
, "qemu: invalid physical CHS format\n");
6726 case QEMU_OPTION_nographic
:
6727 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
6728 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
6731 case QEMU_OPTION_kernel
:
6732 kernel_filename
= optarg
;
6734 case QEMU_OPTION_append
:
6735 kernel_cmdline
= optarg
;
6737 case QEMU_OPTION_cdrom
:
6738 if (cdrom_index
>= 0) {
6739 hd_filename
[cdrom_index
] = optarg
;
6742 case QEMU_OPTION_boot
:
6743 boot_device
= optarg
[0];
6744 if (boot_device
!= 'a' &&
6745 #if defined(TARGET_SPARC) || defined(TARGET_I386)
6747 boot_device
!= 'n' &&
6749 boot_device
!= 'c' && boot_device
!= 'd') {
6750 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
6754 case QEMU_OPTION_fda
:
6755 fd_filename
[0] = optarg
;
6757 case QEMU_OPTION_fdb
:
6758 fd_filename
[1] = optarg
;
6761 case QEMU_OPTION_no_fd_bootchk
:
6765 case QEMU_OPTION_no_code_copy
:
6766 code_copy_enabled
= 0;
6768 case QEMU_OPTION_net
:
6769 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
6770 fprintf(stderr
, "qemu: too many network clients\n");
6773 pstrcpy(net_clients
[nb_net_clients
],
6774 sizeof(net_clients
[0]),
6779 case QEMU_OPTION_tftp
:
6780 tftp_prefix
= optarg
;
6783 case QEMU_OPTION_smb
:
6784 net_slirp_smb(optarg
);
6787 case QEMU_OPTION_redir
:
6788 net_slirp_redir(optarg
);
6792 case QEMU_OPTION_audio_help
:
6796 case QEMU_OPTION_soundhw
:
6797 select_soundhw (optarg
);
6804 ram_size
= atoi(optarg
) * 1024 * 1024;
6807 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
6808 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
6809 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
6818 mask
= cpu_str_to_log_mask(optarg
);
6820 printf("Log items (comma separated):\n");
6821 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
6822 printf("%-10s %s\n", item
->name
, item
->help
);
6829 #ifdef CONFIG_GDBSTUB
6834 gdbstub_port
= atoi(optarg
);
6841 start_emulation
= 0;
6844 keyboard_layout
= optarg
;
6846 case QEMU_OPTION_localtime
:
6849 case QEMU_OPTION_cirrusvga
:
6850 cirrus_vga_enabled
= 1;
6852 case QEMU_OPTION_std_vga
:
6853 cirrus_vga_enabled
= 0;
6860 w
= strtol(p
, (char **)&p
, 10);
6863 fprintf(stderr
, "qemu: invalid resolution or depth\n");
6869 h
= strtol(p
, (char **)&p
, 10);
6874 depth
= strtol(p
, (char **)&p
, 10);
6875 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
6876 depth
!= 24 && depth
!= 32)
6878 } else if (*p
== '\0') {
6879 depth
= graphic_depth
;
6886 graphic_depth
= depth
;
6889 case QEMU_OPTION_monitor
:
6890 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
6892 case QEMU_OPTION_serial
:
6893 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
6894 fprintf(stderr
, "qemu: too many serial ports\n");
6897 pstrcpy(serial_devices
[serial_device_index
],
6898 sizeof(serial_devices
[0]), optarg
);
6899 serial_device_index
++;
6901 case QEMU_OPTION_parallel
:
6902 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
6903 fprintf(stderr
, "qemu: too many parallel ports\n");
6906 pstrcpy(parallel_devices
[parallel_device_index
],
6907 sizeof(parallel_devices
[0]), optarg
);
6908 parallel_device_index
++;
6910 case QEMU_OPTION_loadvm
:
6913 case QEMU_OPTION_full_screen
:
6917 case QEMU_OPTION_no_quit
:
6921 case QEMU_OPTION_pidfile
:
6922 create_pidfile(optarg
);
6925 case QEMU_OPTION_win2k_hack
:
6926 win2k_install_hack
= 1;
6930 case QEMU_OPTION_no_kqemu
:
6933 case QEMU_OPTION_kernel_kqemu
:
6937 case QEMU_OPTION_usb
:
6940 case QEMU_OPTION_usbdevice
:
6942 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
6943 fprintf(stderr
, "Too many USB devices\n");
6946 pstrcpy(usb_devices
[usb_devices_index
],
6947 sizeof(usb_devices
[usb_devices_index
]),
6949 usb_devices_index
++;
6951 case QEMU_OPTION_smp
:
6952 smp_cpus
= atoi(optarg
);
6953 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
6954 fprintf(stderr
, "Invalid number of CPUs\n");
6958 case QEMU_OPTION_vnc
:
6959 vnc_display
= optarg
;
6961 case QEMU_OPTION_no_acpi
:
6964 case QEMU_OPTION_no_reboot
:
6967 case QEMU_OPTION_daemonize
:
6970 case QEMU_OPTION_option_rom
:
6971 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
6972 fprintf(stderr
, "Too many option ROMs\n");
6975 option_rom
[nb_option_roms
] = optarg
;
6978 case QEMU_OPTION_semihosting
:
6979 semihosting_enabled
= 1;
6986 if (daemonize
&& !nographic
&& vnc_display
== NULL
) {
6987 fprintf(stderr
, "Can only daemonize if using -nographic or -vnc\n");
6994 if (pipe(fds
) == -1)
7005 len
= read(fds
[0], &status
, 1);
7006 if (len
== -1 && (errno
== EINTR
))
7009 if (len
!= 1 || status
!= 0)
7027 signal(SIGTSTP
, SIG_IGN
);
7028 signal(SIGTTOU
, SIG_IGN
);
7029 signal(SIGTTIN
, SIG_IGN
);
7037 linux_boot
= (kernel_filename
!= NULL
);
7040 hd_filename
[0] == '\0' &&
7041 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
7042 fd_filename
[0] == '\0')
7045 /* boot to floppy or the default cd if no hard disk defined yet */
7046 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
7047 if (fd_filename
[0] != '\0')
7053 setvbuf(stdout
, NULL
, _IOLBF
, 0);
7063 /* init network clients */
7064 if (nb_net_clients
== 0) {
7065 /* if no clients, we use a default config */
7066 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
7068 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
7073 for(i
= 0;i
< nb_net_clients
; i
++) {
7074 if (net_client_init(net_clients
[i
]) < 0)
7079 if (boot_device
== 'n') {
7080 for (i
= 0; i
< nb_nics
; i
++) {
7081 const char *model
= nd_table
[i
].model
;
7085 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
7086 if (get_image_size(buf
) > 0) {
7087 option_rom
[nb_option_roms
] = strdup(buf
);
7093 fprintf(stderr
, "No valid PXE rom found for network device\n");
7096 boot_device
= 'c'; /* to prevent confusion by the BIOS */
7100 /* init the memory */
7101 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
7103 for (i
= 0; i
< nb_option_roms
; i
++) {
7104 int ret
= get_image_size(option_rom
[i
]);
7106 fprintf(stderr
, "Could not load option rom '%s'\n", option_rom
[i
]);
7109 phys_ram_size
+= ret
;
7112 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
7113 if (!phys_ram_base
) {
7114 fprintf(stderr
, "Could not allocate physical memory\n");
7118 /* we always create the cdrom drive, even if no disk is there */
7120 if (cdrom_index
>= 0) {
7121 bs_table
[cdrom_index
] = bdrv_new("cdrom");
7122 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
7125 /* open the virtual block devices */
7126 for(i
= 0; i
< MAX_DISKS
; i
++) {
7127 if (hd_filename
[i
]) {
7130 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
7131 bs_table
[i
] = bdrv_new(buf
);
7133 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7134 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
7138 if (i
== 0 && cyls
!= 0) {
7139 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
7140 bdrv_set_translation_hint(bs_table
[i
], translation
);
7145 /* we always create at least one floppy disk */
7146 fd_table
[0] = bdrv_new("fda");
7147 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
7149 for(i
= 0; i
< MAX_FD
; i
++) {
7150 if (fd_filename
[i
]) {
7153 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
7154 fd_table
[i
] = bdrv_new(buf
);
7155 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
7157 if (fd_filename
[i
] != '\0') {
7158 if (bdrv_open(fd_table
[i
], fd_filename
[i
],
7159 snapshot
? BDRV_O_SNAPSHOT
: 0) < 0) {
7160 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
7168 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
7169 register_savevm("ram", 0, 2, ram_save
, ram_load
, NULL
);
7175 dumb_display_init(ds
);
7176 } else if (vnc_display
!= NULL
) {
7177 vnc_display_init(ds
, vnc_display
);
7179 #if defined(CONFIG_SDL)
7180 sdl_display_init(ds
, full_screen
);
7181 #elif defined(CONFIG_COCOA)
7182 cocoa_display_init(ds
, full_screen
);
7184 dumb_display_init(ds
);
7188 monitor_hd
= qemu_chr_open(monitor_device
);
7190 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
7193 monitor_init(monitor_hd
, !nographic
);
7195 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
7196 const char *devname
= serial_devices
[i
];
7197 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7198 serial_hds
[i
] = qemu_chr_open(devname
);
7199 if (!serial_hds
[i
]) {
7200 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
7204 if (!strcmp(devname
, "vc"))
7205 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
7209 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
7210 const char *devname
= parallel_devices
[i
];
7211 if (devname
[0] != '\0' && strcmp(devname
, "none")) {
7212 parallel_hds
[i
] = qemu_chr_open(devname
);
7213 if (!parallel_hds
[i
]) {
7214 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
7218 if (!strcmp(devname
, "vc"))
7219 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
7223 machine
->init(ram_size
, vga_ram_size
, boot_device
,
7224 ds
, fd_filename
, snapshot
,
7225 kernel_filename
, kernel_cmdline
, initrd_filename
);
7227 /* init USB devices */
7229 for(i
= 0; i
< usb_devices_index
; i
++) {
7230 if (usb_device_add(usb_devices
[i
]) < 0) {
7231 fprintf(stderr
, "Warning: could not add USB device %s\n",
7237 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
7238 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
7240 #ifdef CONFIG_GDBSTUB
7242 if (gdbserver_start(gdbstub_port
) < 0) {
7243 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
7247 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
7255 /* XXX: simplify init */
7257 if (start_emulation
) {
7268 len
= write(fds
[1], &status
, 1);
7269 if (len
== -1 && (errno
== EINTR
))
7275 fd
= open("/dev/null", O_RDWR
);