4 * Copyright (c) 2003-2005 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
34 #include <sys/times.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
51 #include <linux/if_tun.h>
54 #include <linux/rtc.h>
55 #include <linux/ppdev.h>
59 #if defined(CONFIG_SLIRP)
65 #include <sys/timeb.h>
69 #define getopt_long_only getopt_long
70 #define memalign(align, size) malloc(size)
77 #endif /* CONFIG_SDL */
81 #define main qemu_main
82 #endif /* CONFIG_COCOA */
88 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
90 //#define DEBUG_UNUSED_IOPORT
91 //#define DEBUG_IOPORT
93 #if !defined(CONFIG_SOFTMMU)
94 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
96 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
100 #define DEFAULT_RAM_SIZE 144
102 #define DEFAULT_RAM_SIZE 128
105 #define GUI_REFRESH_INTERVAL 30
107 /* XXX: use a two level table to limit memory usage */
108 #define MAX_IOPORTS 65536
110 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
111 char phys_ram_file
[1024];
112 void *ioport_opaque
[MAX_IOPORTS
];
113 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
114 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
115 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
118 static DisplayState display_state
;
120 const char* keyboard_layout
= NULL
;
121 int64_t ticks_per_sec
;
122 int boot_device
= 'c';
124 int pit_min_timer_count
= 0;
126 NICInfo nd_table
[MAX_NICS
];
127 QEMUTimer
*gui_timer
;
130 int cirrus_vga_enabled
= 1;
132 int graphic_width
= 1024;
133 int graphic_height
= 768;
135 int graphic_width
= 800;
136 int graphic_height
= 600;
138 int graphic_depth
= 15;
140 TextConsole
*vga_console
;
141 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
142 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
144 int win2k_install_hack
= 0;
147 USBPort
*vm_usb_ports
[MAX_VM_USB_PORTS
];
148 USBDevice
*vm_usb_hub
;
149 static VLANState
*first_vlan
;
151 #if defined(TARGET_SPARC)
153 #elif defined(TARGET_I386)
159 /***********************************************************/
160 /* x86 ISA bus support */
162 target_phys_addr_t isa_mem_base
= 0;
165 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
167 #ifdef DEBUG_UNUSED_IOPORT
168 fprintf(stderr
, "inb: port=0x%04x\n", address
);
173 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
175 #ifdef DEBUG_UNUSED_IOPORT
176 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
180 /* default is to make two byte accesses */
181 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
184 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
185 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
186 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
190 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
192 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
193 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
194 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
197 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
199 #ifdef DEBUG_UNUSED_IOPORT
200 fprintf(stderr
, "inl: port=0x%04x\n", address
);
205 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
207 #ifdef DEBUG_UNUSED_IOPORT
208 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
212 void init_ioports(void)
216 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
217 ioport_read_table
[0][i
] = default_ioport_readb
;
218 ioport_write_table
[0][i
] = default_ioport_writeb
;
219 ioport_read_table
[1][i
] = default_ioport_readw
;
220 ioport_write_table
[1][i
] = default_ioport_writew
;
221 ioport_read_table
[2][i
] = default_ioport_readl
;
222 ioport_write_table
[2][i
] = default_ioport_writel
;
226 /* size is the word size in byte */
227 int register_ioport_read(int start
, int length
, int size
,
228 IOPortReadFunc
*func
, void *opaque
)
234 } else if (size
== 2) {
236 } else if (size
== 4) {
239 hw_error("register_ioport_read: invalid size");
242 for(i
= start
; i
< start
+ length
; i
+= size
) {
243 ioport_read_table
[bsize
][i
] = func
;
244 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
245 hw_error("register_ioport_read: invalid opaque");
246 ioport_opaque
[i
] = opaque
;
251 /* size is the word size in byte */
252 int register_ioport_write(int start
, int length
, int size
,
253 IOPortWriteFunc
*func
, void *opaque
)
259 } else if (size
== 2) {
261 } else if (size
== 4) {
264 hw_error("register_ioport_write: invalid size");
267 for(i
= start
; i
< start
+ length
; i
+= size
) {
268 ioport_write_table
[bsize
][i
] = func
;
269 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
270 hw_error("register_ioport_read: invalid opaque");
271 ioport_opaque
[i
] = opaque
;
276 void isa_unassign_ioport(int start
, int length
)
280 for(i
= start
; i
< start
+ length
; i
++) {
281 ioport_read_table
[0][i
] = default_ioport_readb
;
282 ioport_read_table
[1][i
] = default_ioport_readw
;
283 ioport_read_table
[2][i
] = default_ioport_readl
;
285 ioport_write_table
[0][i
] = default_ioport_writeb
;
286 ioport_write_table
[1][i
] = default_ioport_writew
;
287 ioport_write_table
[2][i
] = default_ioport_writel
;
291 /***********************************************************/
293 void pstrcpy(char *buf
, int buf_size
, const char *str
)
303 if (c
== 0 || q
>= buf
+ buf_size
- 1)
310 /* strcat and truncate. */
311 char *pstrcat(char *buf
, int buf_size
, const char *s
)
316 pstrcpy(buf
+ len
, buf_size
- len
, s
);
320 int strstart(const char *str
, const char *val
, const char **ptr
)
336 /* return the size or -1 if error */
337 int get_image_size(const char *filename
)
340 fd
= open(filename
, O_RDONLY
| O_BINARY
);
343 size
= lseek(fd
, 0, SEEK_END
);
348 /* return the size or -1 if error */
349 int load_image(const char *filename
, uint8_t *addr
)
352 fd
= open(filename
, O_RDONLY
| O_BINARY
);
355 size
= lseek(fd
, 0, SEEK_END
);
356 lseek(fd
, 0, SEEK_SET
);
357 if (read(fd
, addr
, size
) != size
) {
365 void cpu_outb(CPUState
*env
, int addr
, int val
)
368 if (loglevel
& CPU_LOG_IOPORT
)
369 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
371 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
374 void cpu_outw(CPUState
*env
, int addr
, int val
)
377 if (loglevel
& CPU_LOG_IOPORT
)
378 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
380 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
383 void cpu_outl(CPUState
*env
, int addr
, int val
)
386 if (loglevel
& CPU_LOG_IOPORT
)
387 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
389 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
392 int cpu_inb(CPUState
*env
, int addr
)
395 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
397 if (loglevel
& CPU_LOG_IOPORT
)
398 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
403 int cpu_inw(CPUState
*env
, int addr
)
406 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
408 if (loglevel
& CPU_LOG_IOPORT
)
409 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
414 int cpu_inl(CPUState
*env
, int addr
)
417 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
419 if (loglevel
& CPU_LOG_IOPORT
)
420 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
425 /***********************************************************/
426 void hw_error(const char *fmt
, ...)
432 fprintf(stderr
, "qemu: hardware error: ");
433 vfprintf(stderr
, fmt
, ap
);
434 fprintf(stderr
, "\n");
435 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
436 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
438 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
440 cpu_dump_state(env
, stderr
, fprintf
, 0);
447 /***********************************************************/
450 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
451 static void *qemu_put_kbd_event_opaque
;
452 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
453 static void *qemu_put_mouse_event_opaque
;
455 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
457 qemu_put_kbd_event_opaque
= opaque
;
458 qemu_put_kbd_event
= func
;
461 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
)
463 qemu_put_mouse_event_opaque
= opaque
;
464 qemu_put_mouse_event
= func
;
467 void kbd_put_keycode(int keycode
)
469 if (qemu_put_kbd_event
) {
470 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
474 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
476 if (qemu_put_mouse_event
) {
477 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
478 dx
, dy
, dz
, buttons_state
);
482 /***********************************************************/
485 #if defined(__powerpc__)
487 static inline uint32_t get_tbl(void)
490 asm volatile("mftb %0" : "=r" (tbl
));
494 static inline uint32_t get_tbu(void)
497 asm volatile("mftbu %0" : "=r" (tbl
));
501 int64_t cpu_get_real_ticks(void)
504 /* NOTE: we test if wrapping has occurred */
510 return ((int64_t)h
<< 32) | l
;
513 #elif defined(__i386__)
515 int64_t cpu_get_real_ticks(void)
518 asm volatile ("rdtsc" : "=A" (val
));
522 #elif defined(__x86_64__)
524 int64_t cpu_get_real_ticks(void)
528 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
535 #elif defined(__ia64)
537 int64_t cpu_get_real_ticks(void)
540 asm volatile ("mov %0 = ar.itc" : "=r"(val
) :: "memory");
544 #elif defined(__s390__)
546 int64_t cpu_get_real_ticks(void)
549 asm volatile("stck 0(%1)" : "=m" (val
) : "a" (&val
) : "cc");
554 #error unsupported CPU
557 static int64_t cpu_ticks_offset
;
558 static int cpu_ticks_enabled
;
560 static inline int64_t cpu_get_ticks(void)
562 if (!cpu_ticks_enabled
) {
563 return cpu_ticks_offset
;
565 return cpu_get_real_ticks() + cpu_ticks_offset
;
569 /* enable cpu_get_ticks() */
570 void cpu_enable_ticks(void)
572 if (!cpu_ticks_enabled
) {
573 cpu_ticks_offset
-= cpu_get_real_ticks();
574 cpu_ticks_enabled
= 1;
578 /* disable cpu_get_ticks() : the clock is stopped. You must not call
579 cpu_get_ticks() after that. */
580 void cpu_disable_ticks(void)
582 if (cpu_ticks_enabled
) {
583 cpu_ticks_offset
= cpu_get_ticks();
584 cpu_ticks_enabled
= 0;
588 static int64_t get_clock(void)
593 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
596 gettimeofday(&tv
, NULL
);
597 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
601 void cpu_calibrate_ticks(void)
606 ticks
= cpu_get_real_ticks();
612 usec
= get_clock() - usec
;
613 ticks
= cpu_get_real_ticks() - ticks
;
614 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
617 /* compute with 96 bit intermediate result: (a*b)/c */
618 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
623 #ifdef WORDS_BIGENDIAN
633 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
634 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
637 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
641 #define QEMU_TIMER_REALTIME 0
642 #define QEMU_TIMER_VIRTUAL 1
646 /* XXX: add frequency */
654 struct QEMUTimer
*next
;
660 static QEMUTimer
*active_timers
[2];
662 static MMRESULT timerID
;
664 /* frequency of the times() clock tick */
665 static int timer_freq
;
668 QEMUClock
*qemu_new_clock(int type
)
671 clock
= qemu_mallocz(sizeof(QEMUClock
));
678 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
682 ts
= qemu_mallocz(sizeof(QEMUTimer
));
689 void qemu_free_timer(QEMUTimer
*ts
)
694 /* stop a timer, but do not dealloc it */
695 void qemu_del_timer(QEMUTimer
*ts
)
699 /* NOTE: this code must be signal safe because
700 qemu_timer_expired() can be called from a signal. */
701 pt
= &active_timers
[ts
->clock
->type
];
714 /* modify the current timer so that it will be fired when current_time
715 >= expire_time. The corresponding callback will be called. */
716 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
722 /* add the timer in the sorted list */
723 /* NOTE: this code must be signal safe because
724 qemu_timer_expired() can be called from a signal. */
725 pt
= &active_timers
[ts
->clock
->type
];
730 if (t
->expire_time
> expire_time
)
734 ts
->expire_time
= expire_time
;
739 int qemu_timer_pending(QEMUTimer
*ts
)
742 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
749 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
753 return (timer_head
->expire_time
<= current_time
);
756 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
762 if (!ts
|| ts
->expire_time
> current_time
)
764 /* remove timer from the list before calling the callback */
765 *ptimer_head
= ts
->next
;
768 /* run the callback (the timer list can be modified) */
773 int64_t qemu_get_clock(QEMUClock
*clock
)
775 switch(clock
->type
) {
776 case QEMU_TIMER_REALTIME
:
778 return GetTickCount();
783 /* Note that using gettimeofday() is not a good solution
784 for timers because its value change when the date is
786 if (timer_freq
== 100) {
787 return times(&tp
) * 10;
789 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
794 case QEMU_TIMER_VIRTUAL
:
795 return cpu_get_ticks();
800 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
802 uint64_t expire_time
;
804 if (qemu_timer_pending(ts
)) {
805 expire_time
= ts
->expire_time
;
809 qemu_put_be64(f
, expire_time
);
812 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
814 uint64_t expire_time
;
816 expire_time
= qemu_get_be64(f
);
817 if (expire_time
!= -1) {
818 qemu_mod_timer(ts
, expire_time
);
824 static void timer_save(QEMUFile
*f
, void *opaque
)
826 if (cpu_ticks_enabled
) {
827 hw_error("cannot save state if virtual timers are running");
829 qemu_put_be64s(f
, &cpu_ticks_offset
);
830 qemu_put_be64s(f
, &ticks_per_sec
);
833 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
837 if (cpu_ticks_enabled
) {
840 qemu_get_be64s(f
, &cpu_ticks_offset
);
841 qemu_get_be64s(f
, &ticks_per_sec
);
846 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
847 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
849 static void host_alarm_handler(int host_signum
)
853 #define DISP_FREQ 1000
855 static int64_t delta_min
= INT64_MAX
;
856 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
858 ti
= qemu_get_clock(vm_clock
);
859 if (last_clock
!= 0) {
860 delta
= ti
- last_clock
;
861 if (delta
< delta_min
)
863 if (delta
> delta_max
)
866 if (++count
== DISP_FREQ
) {
867 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
868 muldiv64(delta_min
, 1000000, ticks_per_sec
),
869 muldiv64(delta_max
, 1000000, ticks_per_sec
),
870 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
871 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
873 delta_min
= INT64_MAX
;
881 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
882 qemu_get_clock(vm_clock
)) ||
883 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
884 qemu_get_clock(rt_clock
))) {
885 CPUState
*env
= cpu_single_env
;
887 /* stop the currently executing cpu because a timer occured */
888 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
890 if (env
->kqemu_enabled
) {
891 kqemu_cpu_interrupt(env
);
900 #if defined(__linux__)
902 #define RTC_FREQ 1024
906 static int start_rtc_timer(void)
908 rtc_fd
= open("/dev/rtc", O_RDONLY
);
911 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
912 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
913 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
914 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
917 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
922 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
928 static int start_rtc_timer(void)
933 #endif /* !defined(__linux__) */
935 #endif /* !defined(_WIN32) */
937 static void init_timers(void)
939 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
940 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
945 timerID
= timeSetEvent(1, // interval (ms)
947 host_alarm_handler
, // function
948 (DWORD
)&count
, // user parameter
949 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
951 perror("failed timer alarm");
955 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
958 struct sigaction act
;
959 struct itimerval itv
;
961 /* get times() syscall frequency */
962 timer_freq
= sysconf(_SC_CLK_TCK
);
965 sigfillset(&act
.sa_mask
);
967 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
968 act
.sa_flags
|= SA_ONSTACK
;
970 act
.sa_handler
= host_alarm_handler
;
971 sigaction(SIGALRM
, &act
, NULL
);
973 itv
.it_interval
.tv_sec
= 0;
974 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
975 itv
.it_value
.tv_sec
= 0;
976 itv
.it_value
.tv_usec
= 10 * 1000;
977 setitimer(ITIMER_REAL
, &itv
, NULL
);
978 /* we probe the tick duration of the kernel to inform the user if
979 the emulated kernel requested a too high timer frequency */
980 getitimer(ITIMER_REAL
, &itv
);
982 #if defined(__linux__)
983 if (itv
.it_interval
.tv_usec
> 1000) {
984 /* try to use /dev/rtc to have a faster timer */
985 if (start_rtc_timer() < 0)
988 itv
.it_interval
.tv_sec
= 0;
989 itv
.it_interval
.tv_usec
= 0;
990 itv
.it_value
.tv_sec
= 0;
991 itv
.it_value
.tv_usec
= 0;
992 setitimer(ITIMER_REAL
, &itv
, NULL
);
995 sigaction(SIGIO
, &act
, NULL
);
996 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
997 fcntl(rtc_fd
, F_SETOWN
, getpid());
999 #endif /* defined(__linux__) */
1002 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1003 PIT_FREQ
) / 1000000;
1009 void quit_timers(void)
1012 timeKillEvent(timerID
);
1016 /***********************************************************/
1017 /* character device */
1019 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1021 return s
->chr_write(s
, buf
, len
);
1024 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1028 return s
->chr_ioctl(s
, cmd
, arg
);
1031 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1036 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1037 qemu_chr_write(s
, buf
, strlen(buf
));
1041 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1043 if (s
->chr_send_event
)
1044 s
->chr_send_event(s
, event
);
1047 void qemu_chr_add_read_handler(CharDriverState
*s
,
1048 IOCanRWHandler
*fd_can_read
,
1049 IOReadHandler
*fd_read
, void *opaque
)
1051 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
1054 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
1056 s
->chr_event
= chr_event
;
1059 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1064 static void null_chr_add_read_handler(CharDriverState
*chr
,
1065 IOCanRWHandler
*fd_can_read
,
1066 IOReadHandler
*fd_read
, void *opaque
)
1070 CharDriverState
*qemu_chr_open_null(void)
1072 CharDriverState
*chr
;
1074 chr
= qemu_mallocz(sizeof(CharDriverState
));
1077 chr
->chr_write
= null_chr_write
;
1078 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1084 #define socket_error() WSAGetLastError()
1086 #define EWOULDBLOCK WSAEWOULDBLOCK
1087 #define EINTR WSAEINTR
1088 #define EINPROGRESS WSAEINPROGRESS
1090 static void socket_cleanup(void)
1095 static int socket_init(void)
1100 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1102 err
= WSAGetLastError();
1103 fprintf(stderr
, "WSAStartup: %d\n", err
);
1106 atexit(socket_cleanup
);
1110 static int send_all(int fd
, const uint8_t *buf
, int len1
)
1116 ret
= send(fd
, buf
, len
, 0);
1119 errno
= WSAGetLastError();
1120 if (errno
!= WSAEWOULDBLOCK
) {
1123 } else if (ret
== 0) {
1133 void socket_set_nonblock(int fd
)
1135 unsigned long opt
= 1;
1136 ioctlsocket(fd
, FIONBIO
, &opt
);
1141 #define socket_error() errno
1142 #define closesocket(s) close(s)
1144 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1150 ret
= write(fd
, buf
, len
);
1152 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1154 } else if (ret
== 0) {
1164 static inline int send_all(int fd
, const uint8_t *buf
, int len1
)
1166 return unix_write(fd
, buf
, len1
);
1169 void socket_set_nonblock(int fd
)
1171 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1173 #endif /* !_WIN32 */
1179 IOCanRWHandler
*fd_can_read
;
1180 IOReadHandler
*fd_read
;
1185 #define STDIO_MAX_CLIENTS 2
1187 static int stdio_nb_clients
;
1188 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1190 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1192 FDCharDriver
*s
= chr
->opaque
;
1193 return unix_write(s
->fd_out
, buf
, len
);
1196 static int fd_chr_read_poll(void *opaque
)
1198 CharDriverState
*chr
= opaque
;
1199 FDCharDriver
*s
= chr
->opaque
;
1201 s
->max_size
= s
->fd_can_read(s
->fd_opaque
);
1205 static void fd_chr_read(void *opaque
)
1207 CharDriverState
*chr
= opaque
;
1208 FDCharDriver
*s
= chr
->opaque
;
1213 if (len
> s
->max_size
)
1217 size
= read(s
->fd_in
, buf
, len
);
1219 s
->fd_read(s
->fd_opaque
, buf
, size
);
1223 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1224 IOCanRWHandler
*fd_can_read
,
1225 IOReadHandler
*fd_read
, void *opaque
)
1227 FDCharDriver
*s
= chr
->opaque
;
1229 if (s
->fd_in
>= 0) {
1230 s
->fd_can_read
= fd_can_read
;
1231 s
->fd_read
= fd_read
;
1232 s
->fd_opaque
= opaque
;
1233 if (nographic
&& s
->fd_in
== 0) {
1235 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
1236 fd_chr_read
, NULL
, chr
);
1241 /* open a character device to a unix fd */
1242 CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1244 CharDriverState
*chr
;
1247 chr
= qemu_mallocz(sizeof(CharDriverState
));
1250 s
= qemu_mallocz(sizeof(FDCharDriver
));
1258 chr
->chr_write
= fd_chr_write
;
1259 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1263 CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1267 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
);
1270 return qemu_chr_open_fd(-1, fd_out
);
1273 CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1277 fd
= open(filename
, O_RDWR
| O_BINARY
);
1280 return qemu_chr_open_fd(fd
, fd
);
1284 /* for STDIO, we handle the case where several clients use it
1287 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1289 #define TERM_FIFO_MAX_SIZE 1
1291 static int term_got_escape
, client_index
;
1292 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1295 void term_print_help(void)
1298 "C-a h print this help\n"
1299 "C-a x exit emulator\n"
1300 "C-a s save disk data back to file (if -snapshot)\n"
1301 "C-a b send break (magic sysrq)\n"
1302 "C-a c switch between console and monitor\n"
1303 "C-a C-a send C-a\n"
1307 /* called when a char is received */
1308 static void stdio_received_byte(int ch
)
1310 if (term_got_escape
) {
1311 term_got_escape
= 0;
1322 for (i
= 0; i
< MAX_DISKS
; i
++) {
1324 bdrv_commit(bs_table
[i
]);
1329 if (client_index
< stdio_nb_clients
) {
1330 CharDriverState
*chr
;
1333 chr
= stdio_clients
[client_index
];
1335 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1340 if (client_index
>= stdio_nb_clients
)
1342 if (client_index
== 0) {
1343 /* send a new line in the monitor to get the prompt */
1351 } else if (ch
== TERM_ESCAPE
) {
1352 term_got_escape
= 1;
1355 if (client_index
< stdio_nb_clients
) {
1357 CharDriverState
*chr
;
1360 chr
= stdio_clients
[client_index
];
1362 if (s
->fd_can_read(s
->fd_opaque
) > 0) {
1364 s
->fd_read(s
->fd_opaque
, buf
, 1);
1365 } else if (term_fifo_size
== 0) {
1366 term_fifo
[term_fifo_size
++] = ch
;
1372 static int stdio_read_poll(void *opaque
)
1374 CharDriverState
*chr
;
1377 if (client_index
< stdio_nb_clients
) {
1378 chr
= stdio_clients
[client_index
];
1380 /* try to flush the queue if needed */
1381 if (term_fifo_size
!= 0 && s
->fd_can_read(s
->fd_opaque
) > 0) {
1382 s
->fd_read(s
->fd_opaque
, term_fifo
, 1);
1385 /* see if we can absorb more chars */
1386 if (term_fifo_size
== 0)
1395 static void stdio_read(void *opaque
)
1400 size
= read(0, buf
, 1);
1402 stdio_received_byte(buf
[0]);
1405 /* init terminal so that we can grab keys */
1406 static struct termios oldtty
;
1407 static int old_fd0_flags
;
1409 static void term_exit(void)
1411 tcsetattr (0, TCSANOW
, &oldtty
);
1412 fcntl(0, F_SETFL
, old_fd0_flags
);
1415 static void term_init(void)
1419 tcgetattr (0, &tty
);
1421 old_fd0_flags
= fcntl(0, F_GETFL
);
1423 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1424 |INLCR
|IGNCR
|ICRNL
|IXON
);
1425 tty
.c_oflag
|= OPOST
;
1426 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1427 /* if graphical mode, we allow Ctrl-C handling */
1429 tty
.c_lflag
&= ~ISIG
;
1430 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1433 tty
.c_cc
[VTIME
] = 0;
1435 tcsetattr (0, TCSANOW
, &tty
);
1439 fcntl(0, F_SETFL
, O_NONBLOCK
);
1442 CharDriverState
*qemu_chr_open_stdio(void)
1444 CharDriverState
*chr
;
1447 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1449 chr
= qemu_chr_open_fd(0, 1);
1450 if (stdio_nb_clients
== 0)
1451 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, NULL
);
1452 client_index
= stdio_nb_clients
;
1454 if (stdio_nb_clients
!= 0)
1456 chr
= qemu_chr_open_fd(0, 1);
1458 stdio_clients
[stdio_nb_clients
++] = chr
;
1459 if (stdio_nb_clients
== 1) {
1460 /* set the terminal in raw mode */
1466 #if defined(__linux__)
1467 CharDriverState
*qemu_chr_open_pty(void)
1470 char slave_name
[1024];
1471 int master_fd
, slave_fd
;
1473 /* Not satisfying */
1474 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1478 /* Disabling local echo and line-buffered output */
1479 tcgetattr (master_fd
, &tty
);
1480 tty
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
1482 tty
.c_cc
[VTIME
] = 0;
1483 tcsetattr (master_fd
, TCSAFLUSH
, &tty
);
1485 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1486 return qemu_chr_open_fd(master_fd
, master_fd
);
1489 static void tty_serial_init(int fd
, int speed
,
1490 int parity
, int data_bits
, int stop_bits
)
1496 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1497 speed
, parity
, data_bits
, stop_bits
);
1499 tcgetattr (fd
, &tty
);
1541 cfsetispeed(&tty
, spd
);
1542 cfsetospeed(&tty
, spd
);
1544 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1545 |INLCR
|IGNCR
|ICRNL
|IXON
);
1546 tty
.c_oflag
|= OPOST
;
1547 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1548 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
);
1569 tty
.c_cflag
|= PARENB
;
1572 tty
.c_cflag
|= PARENB
| PARODD
;
1576 tcsetattr (fd
, TCSANOW
, &tty
);
1579 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1581 FDCharDriver
*s
= chr
->opaque
;
1584 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1586 QEMUSerialSetParams
*ssp
= arg
;
1587 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1588 ssp
->data_bits
, ssp
->stop_bits
);
1591 case CHR_IOCTL_SERIAL_SET_BREAK
:
1593 int enable
= *(int *)arg
;
1595 tcsendbreak(s
->fd_in
, 1);
1604 CharDriverState
*qemu_chr_open_tty(const char *filename
)
1606 CharDriverState
*chr
;
1609 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1612 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1613 tty_serial_init(fd
, 115200, 'N', 8, 1);
1614 chr
= qemu_chr_open_fd(fd
, fd
);
1617 chr
->chr_ioctl
= tty_serial_ioctl
;
1621 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1623 int fd
= (int)chr
->opaque
;
1627 case CHR_IOCTL_PP_READ_DATA
:
1628 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1630 *(uint8_t *)arg
= b
;
1632 case CHR_IOCTL_PP_WRITE_DATA
:
1633 b
= *(uint8_t *)arg
;
1634 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1637 case CHR_IOCTL_PP_READ_CONTROL
:
1638 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1640 *(uint8_t *)arg
= b
;
1642 case CHR_IOCTL_PP_WRITE_CONTROL
:
1643 b
= *(uint8_t *)arg
;
1644 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1647 case CHR_IOCTL_PP_READ_STATUS
:
1648 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1650 *(uint8_t *)arg
= b
;
1658 CharDriverState
*qemu_chr_open_pp(const char *filename
)
1660 CharDriverState
*chr
;
1663 fd
= open(filename
, O_RDWR
);
1667 if (ioctl(fd
, PPCLAIM
) < 0) {
1672 chr
= qemu_mallocz(sizeof(CharDriverState
));
1677 chr
->opaque
= (void *)fd
;
1678 chr
->chr_write
= null_chr_write
;
1679 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1680 chr
->chr_ioctl
= pp_ioctl
;
1685 CharDriverState
*qemu_chr_open_pty(void)
1691 #endif /* !defined(_WIN32) */
1693 CharDriverState
*qemu_chr_open(const char *filename
)
1699 if (!strcmp(filename
, "vc")) {
1700 return text_console_init(&display_state
);
1701 } else if (!strcmp(filename
, "null")) {
1702 return qemu_chr_open_null();
1705 if (strstart(filename
, "file:", &p
)) {
1706 return qemu_chr_open_file_out(p
);
1707 } else if (strstart(filename
, "pipe:", &p
)) {
1708 return qemu_chr_open_pipe(p
);
1709 } else if (!strcmp(filename
, "pty")) {
1710 return qemu_chr_open_pty();
1711 } else if (!strcmp(filename
, "stdio")) {
1712 return qemu_chr_open_stdio();
1715 #if defined(__linux__)
1716 if (strstart(filename
, "/dev/parport", NULL
)) {
1717 return qemu_chr_open_pp(filename
);
1719 if (strstart(filename
, "/dev/", NULL
)) {
1720 return qemu_chr_open_tty(filename
);
1728 /***********************************************************/
1729 /* network device redirectors */
1731 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
1735 for(i
=0;i
<size
;i
+=16) {
1739 fprintf(f
, "%08x ", i
);
1742 fprintf(f
, " %02x", buf
[i
+j
]);
1747 for(j
=0;j
<len
;j
++) {
1749 if (c
< ' ' || c
> '~')
1751 fprintf(f
, "%c", c
);
1757 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
1760 for(i
= 0; i
< 6; i
++) {
1761 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
1774 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
1779 p1
= strchr(p
, sep
);
1785 if (len
> buf_size
- 1)
1787 memcpy(buf
, p
, len
);
1794 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
1802 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
1804 saddr
->sin_family
= AF_INET
;
1805 if (buf
[0] == '\0') {
1806 saddr
->sin_addr
.s_addr
= 0;
1808 if (isdigit(buf
[0])) {
1809 if (!inet_aton(buf
, &saddr
->sin_addr
))
1812 if ((he
= gethostbyname(buf
)) == NULL
)
1814 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
1817 port
= strtol(p
, (char **)&r
, 0);
1820 saddr
->sin_port
= htons(port
);
1824 /* find or alloc a new VLAN */
1825 VLANState
*qemu_find_vlan(int id
)
1827 VLANState
**pvlan
, *vlan
;
1828 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1832 vlan
= qemu_mallocz(sizeof(VLANState
));
1837 pvlan
= &first_vlan
;
1838 while (*pvlan
!= NULL
)
1839 pvlan
= &(*pvlan
)->next
;
1844 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
1845 IOReadHandler
*fd_read
,
1846 IOCanRWHandler
*fd_can_read
,
1849 VLANClientState
*vc
, **pvc
;
1850 vc
= qemu_mallocz(sizeof(VLANClientState
));
1853 vc
->fd_read
= fd_read
;
1854 vc
->fd_can_read
= fd_can_read
;
1855 vc
->opaque
= opaque
;
1859 pvc
= &vlan
->first_client
;
1860 while (*pvc
!= NULL
)
1861 pvc
= &(*pvc
)->next
;
1866 int qemu_can_send_packet(VLANClientState
*vc1
)
1868 VLANState
*vlan
= vc1
->vlan
;
1869 VLANClientState
*vc
;
1871 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
1873 if (vc
->fd_can_read
&& !vc
->fd_can_read(vc
->opaque
))
1880 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
1882 VLANState
*vlan
= vc1
->vlan
;
1883 VLANClientState
*vc
;
1886 printf("vlan %d send:\n", vlan
->id
);
1887 hex_dump(stdout
, buf
, size
);
1889 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
1891 vc
->fd_read(vc
->opaque
, buf
, size
);
1896 #if defined(CONFIG_SLIRP)
1898 /* slirp network adapter */
1900 static int slirp_inited
;
1901 static VLANClientState
*slirp_vc
;
1903 int slirp_can_output(void)
1905 return qemu_can_send_packet(slirp_vc
);
1908 void slirp_output(const uint8_t *pkt
, int pkt_len
)
1911 printf("slirp output:\n");
1912 hex_dump(stdout
, pkt
, pkt_len
);
1914 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
1917 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
1920 printf("slirp input:\n");
1921 hex_dump(stdout
, buf
, size
);
1923 slirp_input(buf
, size
);
1926 static int net_slirp_init(VLANState
*vlan
)
1928 if (!slirp_inited
) {
1932 slirp_vc
= qemu_new_vlan_client(vlan
,
1933 slirp_receive
, NULL
, NULL
);
1934 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
1938 static void net_slirp_redir(const char *redir_str
)
1943 struct in_addr guest_addr
;
1944 int host_port
, guest_port
;
1946 if (!slirp_inited
) {
1952 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
1954 if (!strcmp(buf
, "tcp")) {
1956 } else if (!strcmp(buf
, "udp")) {
1962 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
1964 host_port
= strtol(buf
, &r
, 0);
1968 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
1970 if (buf
[0] == '\0') {
1971 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
1973 if (!inet_aton(buf
, &guest_addr
))
1976 guest_port
= strtol(p
, &r
, 0);
1980 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
1981 fprintf(stderr
, "qemu: could not set up redirection\n");
1986 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
1994 static void smb_exit(void)
1998 char filename
[1024];
2000 /* erase all the files in the directory */
2001 d
= opendir(smb_dir
);
2006 if (strcmp(de
->d_name
, ".") != 0 &&
2007 strcmp(de
->d_name
, "..") != 0) {
2008 snprintf(filename
, sizeof(filename
), "%s/%s",
2009 smb_dir
, de
->d_name
);
2017 /* automatic user mode samba server configuration */
2018 void net_slirp_smb(const char *exported_dir
)
2020 char smb_conf
[1024];
2021 char smb_cmdline
[1024];
2024 if (!slirp_inited
) {
2029 /* XXX: better tmp dir construction */
2030 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
2031 if (mkdir(smb_dir
, 0700) < 0) {
2032 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
2035 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
2037 f
= fopen(smb_conf
, "w");
2039 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
2046 "socket address=127.0.0.1\n"
2047 "pid directory=%s\n"
2048 "lock directory=%s\n"
2049 "log file=%s/log.smbd\n"
2050 "smb passwd file=%s/smbpasswd\n"
2051 "security = share\n"
2066 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "/usr/sbin/smbd -s %s",
2069 slirp_add_exec(0, smb_cmdline
, 4, 139);
2072 #endif /* !defined(_WIN32) */
2074 #endif /* CONFIG_SLIRP */
2076 #if !defined(_WIN32)
2078 typedef struct TAPState
{
2079 VLANClientState
*vc
;
2083 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
2085 TAPState
*s
= opaque
;
2088 ret
= write(s
->fd
, buf
, size
);
2089 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
2096 static void tap_send(void *opaque
)
2098 TAPState
*s
= opaque
;
2102 size
= read(s
->fd
, buf
, sizeof(buf
));
2104 qemu_send_packet(s
->vc
, buf
, size
);
2110 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
2114 s
= qemu_mallocz(sizeof(TAPState
));
2118 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
2119 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
2120 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
2125 static int tap_open(char *ifname
, int ifname_size
)
2131 fd
= open("/dev/tap", O_RDWR
);
2133 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
2138 dev
= devname(s
.st_rdev
, S_IFCHR
);
2139 pstrcpy(ifname
, ifname_size
, dev
);
2141 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2145 static int tap_open(char *ifname
, int ifname_size
)
2150 fd
= open("/dev/net/tun", O_RDWR
);
2152 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
2155 memset(&ifr
, 0, sizeof(ifr
));
2156 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
2157 if (ifname
[0] != '\0')
2158 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
2160 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
2161 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
2163 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
2167 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
2168 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2173 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
2174 const char *setup_script
)
2177 int pid
, status
, fd
;
2182 if (ifname1
!= NULL
)
2183 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
2186 fd
= tap_open(ifname
, sizeof(ifname
));
2192 if (setup_script
[0] != '\0') {
2193 /* try to launch network init script */
2198 *parg
++ = (char *)setup_script
;
2201 execv(setup_script
, args
);
2204 while (waitpid(pid
, &status
, 0) != pid
);
2205 if (!WIFEXITED(status
) ||
2206 WEXITSTATUS(status
) != 0) {
2207 fprintf(stderr
, "%s: could not launch network script\n",
2213 s
= net_tap_fd_init(vlan
, fd
);
2216 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2217 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
2221 #endif /* !_WIN32 */
2223 /* network connection */
2224 typedef struct NetSocketState
{
2225 VLANClientState
*vc
;
2227 int state
; /* 0 = getting length, 1 = getting data */
2231 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
2234 typedef struct NetSocketListenState
{
2237 } NetSocketListenState
;
2239 /* XXX: we consider we can send the whole packet without blocking */
2240 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
2242 NetSocketState
*s
= opaque
;
2246 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
2247 send_all(s
->fd
, buf
, size
);
2250 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
2252 NetSocketState
*s
= opaque
;
2253 sendto(s
->fd
, buf
, size
, 0,
2254 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
2257 static void net_socket_send(void *opaque
)
2259 NetSocketState
*s
= opaque
;
2264 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
2266 err
= socket_error();
2267 if (err
!= EWOULDBLOCK
)
2269 } else if (size
== 0) {
2270 /* end of connection */
2272 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2278 /* reassemble a packet from the network */
2284 memcpy(s
->buf
+ s
->index
, buf
, l
);
2288 if (s
->index
== 4) {
2290 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
2296 l
= s
->packet_len
- s
->index
;
2299 memcpy(s
->buf
+ s
->index
, buf
, l
);
2303 if (s
->index
>= s
->packet_len
) {
2304 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
2313 static void net_socket_send_dgram(void *opaque
)
2315 NetSocketState
*s
= opaque
;
2318 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
2322 /* end of connection */
2323 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2326 qemu_send_packet(s
->vc
, s
->buf
, size
);
2329 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
2334 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
2335 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
2336 inet_ntoa(mcastaddr
->sin_addr
),
2337 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
2341 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2343 perror("socket(PF_INET, SOCK_DGRAM)");
2348 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
2349 (const char *)&val
, sizeof(val
));
2351 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
2355 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
2361 /* Add host to multicast group */
2362 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
2363 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
2365 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
2366 (const char *)&imr
, sizeof(struct ip_mreq
));
2368 perror("setsockopt(IP_ADD_MEMBERSHIP)");
2372 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
2374 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
2375 (const char *)&val
, sizeof(val
));
2377 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
2381 socket_set_nonblock(fd
);
2384 if (fd
>=0) close(fd
);
2388 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
2391 struct sockaddr_in saddr
;
2393 socklen_t saddr_len
;
2396 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
2397 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
2398 * by ONLY ONE process: we must "clone" this dgram socket --jjo
2402 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
2404 if (saddr
.sin_addr
.s_addr
==0) {
2405 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
2409 /* clone dgram socket */
2410 newfd
= net_socket_mcast_create(&saddr
);
2412 /* error already reported by net_socket_mcast_create() */
2416 /* clone newfd to fd, close newfd */
2421 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
2422 fd
, strerror(errno
));
2427 s
= qemu_mallocz(sizeof(NetSocketState
));
2432 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
2433 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
2435 /* mcast: save bound address as dst */
2436 if (is_connected
) s
->dgram_dst
=saddr
;
2438 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2439 "socket: fd=%d (%s mcast=%s:%d)",
2440 fd
, is_connected
? "cloned" : "",
2441 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2445 static void net_socket_connect(void *opaque
)
2447 NetSocketState
*s
= opaque
;
2448 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
2451 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
2455 s
= qemu_mallocz(sizeof(NetSocketState
));
2459 s
->vc
= qemu_new_vlan_client(vlan
,
2460 net_socket_receive
, NULL
, s
);
2461 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2462 "socket: fd=%d", fd
);
2464 net_socket_connect(s
);
2466 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
2471 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
2474 int so_type
=-1, optlen
=sizeof(so_type
);
2476 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
, &optlen
)< 0) {
2477 fprintf(stderr
, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd
);
2482 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
2484 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
2486 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
2487 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
2488 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
2493 static void net_socket_accept(void *opaque
)
2495 NetSocketListenState
*s
= opaque
;
2497 struct sockaddr_in saddr
;
2502 len
= sizeof(saddr
);
2503 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
2504 if (fd
< 0 && errno
!= EINTR
) {
2506 } else if (fd
>= 0) {
2510 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
2514 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
2515 "socket: connection from %s:%d",
2516 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2520 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
2522 NetSocketListenState
*s
;
2524 struct sockaddr_in saddr
;
2526 if (parse_host_port(&saddr
, host_str
) < 0)
2529 s
= qemu_mallocz(sizeof(NetSocketListenState
));
2533 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2538 socket_set_nonblock(fd
);
2540 /* allow fast reuse */
2542 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2544 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2549 ret
= listen(fd
, 0);
2556 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
2560 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
2563 int fd
, connected
, ret
, err
;
2564 struct sockaddr_in saddr
;
2566 if (parse_host_port(&saddr
, host_str
) < 0)
2569 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2574 socket_set_nonblock(fd
);
2578 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2580 err
= socket_error();
2581 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2582 } else if (err
== EINPROGRESS
) {
2594 s
= net_socket_fd_init(vlan
, fd
, connected
);
2597 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2598 "socket: connect to %s:%d",
2599 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2603 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
2607 struct sockaddr_in saddr
;
2609 if (parse_host_port(&saddr
, host_str
) < 0)
2613 fd
= net_socket_mcast_create(&saddr
);
2617 s
= net_socket_fd_init(vlan
, fd
, 0);
2621 s
->dgram_dst
= saddr
;
2623 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2624 "socket: mcast=%s:%d",
2625 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2630 static int get_param_value(char *buf
, int buf_size
,
2631 const char *tag
, const char *str
)
2640 while (*p
!= '\0' && *p
!= '=') {
2641 if ((q
- option
) < sizeof(option
) - 1)
2649 if (!strcmp(tag
, option
)) {
2651 while (*p
!= '\0' && *p
!= ',') {
2652 if ((q
- buf
) < buf_size
- 1)
2659 while (*p
!= '\0' && *p
!= ',') {
2670 int net_client_init(const char *str
)
2681 while (*p
!= '\0' && *p
!= ',') {
2682 if ((q
- device
) < sizeof(device
) - 1)
2690 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
2691 vlan_id
= strtol(buf
, NULL
, 0);
2693 vlan
= qemu_find_vlan(vlan_id
);
2695 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
2698 if (!strcmp(device
, "nic")) {
2702 if (nb_nics
>= MAX_NICS
) {
2703 fprintf(stderr
, "Too Many NICs\n");
2706 nd
= &nd_table
[nb_nics
];
2707 macaddr
= nd
->macaddr
;
2713 macaddr
[5] = 0x56 + nb_nics
;
2715 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
2716 if (parse_macaddr(macaddr
, buf
) < 0) {
2717 fprintf(stderr
, "invalid syntax for ethernet address\n");
2721 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
2722 nd
->model
= strdup(buf
);
2728 if (!strcmp(device
, "none")) {
2729 /* does nothing. It is needed to signal that no network cards
2734 if (!strcmp(device
, "user")) {
2735 ret
= net_slirp_init(vlan
);
2739 if (!strcmp(device
, "tap")) {
2741 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
2742 fprintf(stderr
, "tap: no interface name\n");
2745 ret
= tap_win32_init(vlan
, ifname
);
2748 if (!strcmp(device
, "tap")) {
2750 char setup_script
[1024];
2752 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
2753 fd
= strtol(buf
, NULL
, 0);
2755 if (net_tap_fd_init(vlan
, fd
))
2758 get_param_value(ifname
, sizeof(ifname
), "ifname", p
);
2759 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
2760 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
2762 ret
= net_tap_init(vlan
, ifname
, setup_script
);
2766 if (!strcmp(device
, "socket")) {
2767 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
2769 fd
= strtol(buf
, NULL
, 0);
2771 if (net_socket_fd_init(vlan
, fd
, 1))
2773 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
2774 ret
= net_socket_listen_init(vlan
, buf
);
2775 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
2776 ret
= net_socket_connect_init(vlan
, buf
);
2777 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
2778 ret
= net_socket_mcast_init(vlan
, buf
);
2780 fprintf(stderr
, "Unknown socket options: %s\n", p
);
2785 fprintf(stderr
, "Unknown network device: %s\n", device
);
2789 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
2795 void do_info_network(void)
2798 VLANClientState
*vc
;
2800 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2801 term_printf("VLAN %d devices:\n", vlan
->id
);
2802 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2803 term_printf(" %s\n", vc
->info_str
);
2807 /***********************************************************/
2810 static int usb_device_add(const char *devname
)
2818 for(i
= 0;i
< MAX_VM_USB_PORTS
; i
++) {
2819 if (!vm_usb_ports
[i
]->dev
)
2822 if (i
== MAX_VM_USB_PORTS
)
2825 if (strstart(devname
, "host:", &p
)) {
2826 dev
= usb_host_device_open(p
);
2829 } else if (!strcmp(devname
, "mouse")) {
2830 dev
= usb_mouse_init();
2836 usb_attach(vm_usb_ports
[i
], dev
);
2840 static int usb_device_del(const char *devname
)
2843 int bus_num
, addr
, i
;
2849 p
= strchr(devname
, '.');
2852 bus_num
= strtoul(devname
, NULL
, 0);
2853 addr
= strtoul(p
+ 1, NULL
, 0);
2856 for(i
= 0;i
< MAX_VM_USB_PORTS
; i
++) {
2857 dev
= vm_usb_ports
[i
]->dev
;
2858 if (dev
&& dev
->addr
== addr
)
2861 if (i
== MAX_VM_USB_PORTS
)
2863 usb_attach(vm_usb_ports
[i
], NULL
);
2867 void do_usb_add(const char *devname
)
2870 ret
= usb_device_add(devname
);
2872 term_printf("Could not add USB device '%s'\n", devname
);
2875 void do_usb_del(const char *devname
)
2878 ret
= usb_device_del(devname
);
2880 term_printf("Could not remove USB device '%s'\n", devname
);
2887 const char *speed_str
;
2890 term_printf("USB support not enabled\n");
2894 for(i
= 0; i
< MAX_VM_USB_PORTS
; i
++) {
2895 dev
= vm_usb_ports
[i
]->dev
;
2897 term_printf("Hub port %d:\n", i
);
2898 switch(dev
->speed
) {
2902 case USB_SPEED_FULL
:
2905 case USB_SPEED_HIGH
:
2912 term_printf(" Device %d.%d, speed %s Mb/s\n",
2913 0, dev
->addr
, speed_str
);
2918 /***********************************************************/
2921 static char *pid_filename
;
2923 /* Remove PID file. Called on normal exit */
2925 static void remove_pidfile(void)
2927 unlink (pid_filename
);
2930 static void create_pidfile(const char *filename
)
2932 struct stat pidstat
;
2935 /* Try to write our PID to the named file */
2936 if (stat(filename
, &pidstat
) < 0) {
2937 if (errno
== ENOENT
) {
2938 if ((f
= fopen (filename
, "w")) == NULL
) {
2939 perror("Opening pidfile");
2942 fprintf(f
, "%d\n", getpid());
2944 pid_filename
= qemu_strdup(filename
);
2945 if (!pid_filename
) {
2946 fprintf(stderr
, "Could not save PID filename");
2949 atexit(remove_pidfile
);
2952 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
2958 /***********************************************************/
2961 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
2965 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
2969 static void dumb_refresh(DisplayState
*ds
)
2971 vga_update_display();
2974 void dumb_display_init(DisplayState
*ds
)
2979 ds
->dpy_update
= dumb_update
;
2980 ds
->dpy_resize
= dumb_resize
;
2981 ds
->dpy_refresh
= dumb_refresh
;
2984 #if !defined(CONFIG_SOFTMMU)
2985 /***********************************************************/
2986 /* cpu signal handler */
2987 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
2990 if (cpu_signal_handler(host_signum
, info
, puc
))
2992 if (stdio_nb_clients
> 0)
2998 /***********************************************************/
3001 #define MAX_IO_HANDLERS 64
3003 typedef struct IOHandlerRecord
{
3005 IOCanRWHandler
*fd_read_poll
;
3007 IOHandler
*fd_write
;
3009 /* temporary data */
3011 struct IOHandlerRecord
*next
;
3014 static IOHandlerRecord
*first_io_handler
;
3016 /* XXX: fd_read_poll should be suppressed, but an API change is
3017 necessary in the character devices to suppress fd_can_read(). */
3018 int qemu_set_fd_handler2(int fd
,
3019 IOCanRWHandler
*fd_read_poll
,
3021 IOHandler
*fd_write
,
3024 IOHandlerRecord
**pioh
, *ioh
;
3026 if (!fd_read
&& !fd_write
) {
3027 pioh
= &first_io_handler
;
3032 if (ioh
->fd
== fd
) {
3040 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3044 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
3047 ioh
->next
= first_io_handler
;
3048 first_io_handler
= ioh
;
3051 ioh
->fd_read_poll
= fd_read_poll
;
3052 ioh
->fd_read
= fd_read
;
3053 ioh
->fd_write
= fd_write
;
3054 ioh
->opaque
= opaque
;
3059 int qemu_set_fd_handler(int fd
,
3061 IOHandler
*fd_write
,
3064 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
3067 /***********************************************************/
3068 /* savevm/loadvm support */
3070 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
3072 fwrite(buf
, 1, size
, f
);
3075 void qemu_put_byte(QEMUFile
*f
, int v
)
3080 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
3082 qemu_put_byte(f
, v
>> 8);
3083 qemu_put_byte(f
, v
);
3086 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
3088 qemu_put_byte(f
, v
>> 24);
3089 qemu_put_byte(f
, v
>> 16);
3090 qemu_put_byte(f
, v
>> 8);
3091 qemu_put_byte(f
, v
);
3094 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
3096 qemu_put_be32(f
, v
>> 32);
3097 qemu_put_be32(f
, v
);
3100 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
3102 return fread(buf
, 1, size
, f
);
3105 int qemu_get_byte(QEMUFile
*f
)
3115 unsigned int qemu_get_be16(QEMUFile
*f
)
3118 v
= qemu_get_byte(f
) << 8;
3119 v
|= qemu_get_byte(f
);
3123 unsigned int qemu_get_be32(QEMUFile
*f
)
3126 v
= qemu_get_byte(f
) << 24;
3127 v
|= qemu_get_byte(f
) << 16;
3128 v
|= qemu_get_byte(f
) << 8;
3129 v
|= qemu_get_byte(f
);
3133 uint64_t qemu_get_be64(QEMUFile
*f
)
3136 v
= (uint64_t)qemu_get_be32(f
) << 32;
3137 v
|= qemu_get_be32(f
);
3141 int64_t qemu_ftell(QEMUFile
*f
)
3146 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
3148 if (fseek(f
, pos
, whence
) < 0)
3153 typedef struct SaveStateEntry
{
3157 SaveStateHandler
*save_state
;
3158 LoadStateHandler
*load_state
;
3160 struct SaveStateEntry
*next
;
3163 static SaveStateEntry
*first_se
;
3165 int register_savevm(const char *idstr
,
3168 SaveStateHandler
*save_state
,
3169 LoadStateHandler
*load_state
,
3172 SaveStateEntry
*se
, **pse
;
3174 se
= qemu_malloc(sizeof(SaveStateEntry
));
3177 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
3178 se
->instance_id
= instance_id
;
3179 se
->version_id
= version_id
;
3180 se
->save_state
= save_state
;
3181 se
->load_state
= load_state
;
3182 se
->opaque
= opaque
;
3185 /* add at the end of list */
3187 while (*pse
!= NULL
)
3188 pse
= &(*pse
)->next
;
3193 #define QEMU_VM_FILE_MAGIC 0x5145564d
3194 #define QEMU_VM_FILE_VERSION 0x00000001
3196 int qemu_savevm(const char *filename
)
3200 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
3202 saved_vm_running
= vm_running
;
3205 f
= fopen(filename
, "wb");
3211 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
3212 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
3214 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3216 len
= strlen(se
->idstr
);
3217 qemu_put_byte(f
, len
);
3218 qemu_put_buffer(f
, se
->idstr
, len
);
3220 qemu_put_be32(f
, se
->instance_id
);
3221 qemu_put_be32(f
, se
->version_id
);
3223 /* record size: filled later */
3225 qemu_put_be32(f
, 0);
3227 se
->save_state(f
, se
->opaque
);
3229 /* fill record size */
3231 len
= ftell(f
) - len_pos
- 4;
3232 fseek(f
, len_pos
, SEEK_SET
);
3233 qemu_put_be32(f
, len
);
3234 fseek(f
, cur_pos
, SEEK_SET
);
3240 if (saved_vm_running
)
3245 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
3249 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3250 if (!strcmp(se
->idstr
, idstr
) &&
3251 instance_id
== se
->instance_id
)
3257 int qemu_loadvm(const char *filename
)
3261 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
3262 int saved_vm_running
;
3266 saved_vm_running
= vm_running
;
3269 f
= fopen(filename
, "rb");
3275 v
= qemu_get_be32(f
);
3276 if (v
!= QEMU_VM_FILE_MAGIC
)
3278 v
= qemu_get_be32(f
);
3279 if (v
!= QEMU_VM_FILE_VERSION
) {
3286 len
= qemu_get_byte(f
);
3289 qemu_get_buffer(f
, idstr
, len
);
3291 instance_id
= qemu_get_be32(f
);
3292 version_id
= qemu_get_be32(f
);
3293 record_len
= qemu_get_be32(f
);
3295 printf("idstr=%s instance=0x%x version=%d len=%d\n",
3296 idstr
, instance_id
, version_id
, record_len
);
3299 se
= find_se(idstr
, instance_id
);
3301 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
3302 instance_id
, idstr
);
3304 ret
= se
->load_state(f
, se
->opaque
, version_id
);
3306 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
3307 instance_id
, idstr
);
3310 /* always seek to exact end of record */
3311 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
3316 if (saved_vm_running
)
3321 /***********************************************************/
3322 /* cpu save/restore */
3324 #if defined(TARGET_I386)
3326 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
3328 qemu_put_be32(f
, dt
->selector
);
3329 qemu_put_betl(f
, dt
->base
);
3330 qemu_put_be32(f
, dt
->limit
);
3331 qemu_put_be32(f
, dt
->flags
);
3334 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
3336 dt
->selector
= qemu_get_be32(f
);
3337 dt
->base
= qemu_get_betl(f
);
3338 dt
->limit
= qemu_get_be32(f
);
3339 dt
->flags
= qemu_get_be32(f
);
3342 void cpu_save(QEMUFile
*f
, void *opaque
)
3344 CPUState
*env
= opaque
;
3345 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
3349 for(i
= 0; i
< CPU_NB_REGS
; i
++)
3350 qemu_put_betls(f
, &env
->regs
[i
]);
3351 qemu_put_betls(f
, &env
->eip
);
3352 qemu_put_betls(f
, &env
->eflags
);
3353 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
3354 qemu_put_be32s(f
, &hflags
);
3358 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
3360 for(i
= 0; i
< 8; i
++) {
3361 fptag
|= ((!env
->fptags
[i
]) << i
);
3364 qemu_put_be16s(f
, &fpuc
);
3365 qemu_put_be16s(f
, &fpus
);
3366 qemu_put_be16s(f
, &fptag
);
3368 #ifdef USE_X86LDOUBLE
3373 qemu_put_be16s(f
, &fpregs_format
);
3375 for(i
= 0; i
< 8; i
++) {
3376 #ifdef USE_X86LDOUBLE
3380 /* we save the real CPU data (in case of MMX usage only 'mant'
3381 contains the MMX register */
3382 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
3383 qemu_put_be64(f
, mant
);
3384 qemu_put_be16(f
, exp
);
3387 /* if we use doubles for float emulation, we save the doubles to
3388 avoid losing information in case of MMX usage. It can give
3389 problems if the image is restored on a CPU where long
3390 doubles are used instead. */
3391 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
3395 for(i
= 0; i
< 6; i
++)
3396 cpu_put_seg(f
, &env
->segs
[i
]);
3397 cpu_put_seg(f
, &env
->ldt
);
3398 cpu_put_seg(f
, &env
->tr
);
3399 cpu_put_seg(f
, &env
->gdt
);
3400 cpu_put_seg(f
, &env
->idt
);
3402 qemu_put_be32s(f
, &env
->sysenter_cs
);
3403 qemu_put_be32s(f
, &env
->sysenter_esp
);
3404 qemu_put_be32s(f
, &env
->sysenter_eip
);
3406 qemu_put_betls(f
, &env
->cr
[0]);
3407 qemu_put_betls(f
, &env
->cr
[2]);
3408 qemu_put_betls(f
, &env
->cr
[3]);
3409 qemu_put_betls(f
, &env
->cr
[4]);
3411 for(i
= 0; i
< 8; i
++)
3412 qemu_put_betls(f
, &env
->dr
[i
]);
3415 qemu_put_be32s(f
, &env
->a20_mask
);
3418 qemu_put_be32s(f
, &env
->mxcsr
);
3419 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
3420 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
3421 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
3424 #ifdef TARGET_X86_64
3425 qemu_put_be64s(f
, &env
->efer
);
3426 qemu_put_be64s(f
, &env
->star
);
3427 qemu_put_be64s(f
, &env
->lstar
);
3428 qemu_put_be64s(f
, &env
->cstar
);
3429 qemu_put_be64s(f
, &env
->fmask
);
3430 qemu_put_be64s(f
, &env
->kernelgsbase
);
3434 #ifdef USE_X86LDOUBLE
3435 /* XXX: add that in a FPU generic layer */
3436 union x86_longdouble
{
3441 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
3442 #define EXPBIAS1 1023
3443 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
3444 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
3446 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
3450 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
3451 /* exponent + sign */
3452 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
3453 e
|= SIGND1(temp
) >> 16;
3458 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
3460 CPUState
*env
= opaque
;
3463 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
3465 if (version_id
!= 3)
3467 for(i
= 0; i
< CPU_NB_REGS
; i
++)
3468 qemu_get_betls(f
, &env
->regs
[i
]);
3469 qemu_get_betls(f
, &env
->eip
);
3470 qemu_get_betls(f
, &env
->eflags
);
3471 qemu_get_be32s(f
, &hflags
);
3473 qemu_get_be16s(f
, &fpuc
);
3474 qemu_get_be16s(f
, &fpus
);
3475 qemu_get_be16s(f
, &fptag
);
3476 qemu_get_be16s(f
, &fpregs_format
);
3478 /* NOTE: we cannot always restore the FPU state if the image come
3479 from a host with a different 'USE_X86LDOUBLE' define. We guess
3480 if we are in an MMX state to restore correctly in that case. */
3481 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
3482 for(i
= 0; i
< 8; i
++) {
3486 switch(fpregs_format
) {
3488 mant
= qemu_get_be64(f
);
3489 exp
= qemu_get_be16(f
);
3490 #ifdef USE_X86LDOUBLE
3491 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
3493 /* difficult case */
3495 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
3497 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
3501 mant
= qemu_get_be64(f
);
3502 #ifdef USE_X86LDOUBLE
3504 union x86_longdouble
*p
;
3505 /* difficult case */
3506 p
= (void *)&env
->fpregs
[i
];
3511 fp64_to_fp80(p
, mant
);
3515 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
3524 /* XXX: restore FPU round state */
3525 env
->fpstt
= (fpus
>> 11) & 7;
3526 env
->fpus
= fpus
& ~0x3800;
3528 for(i
= 0; i
< 8; i
++) {
3529 env
->fptags
[i
] = (fptag
>> i
) & 1;
3532 for(i
= 0; i
< 6; i
++)
3533 cpu_get_seg(f
, &env
->segs
[i
]);
3534 cpu_get_seg(f
, &env
->ldt
);
3535 cpu_get_seg(f
, &env
->tr
);
3536 cpu_get_seg(f
, &env
->gdt
);
3537 cpu_get_seg(f
, &env
->idt
);
3539 qemu_get_be32s(f
, &env
->sysenter_cs
);
3540 qemu_get_be32s(f
, &env
->sysenter_esp
);
3541 qemu_get_be32s(f
, &env
->sysenter_eip
);
3543 qemu_get_betls(f
, &env
->cr
[0]);
3544 qemu_get_betls(f
, &env
->cr
[2]);
3545 qemu_get_betls(f
, &env
->cr
[3]);
3546 qemu_get_betls(f
, &env
->cr
[4]);
3548 for(i
= 0; i
< 8; i
++)
3549 qemu_get_betls(f
, &env
->dr
[i
]);
3552 qemu_get_be32s(f
, &env
->a20_mask
);
3554 qemu_get_be32s(f
, &env
->mxcsr
);
3555 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
3556 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
3557 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
3560 #ifdef TARGET_X86_64
3561 qemu_get_be64s(f
, &env
->efer
);
3562 qemu_get_be64s(f
, &env
->star
);
3563 qemu_get_be64s(f
, &env
->lstar
);
3564 qemu_get_be64s(f
, &env
->cstar
);
3565 qemu_get_be64s(f
, &env
->fmask
);
3566 qemu_get_be64s(f
, &env
->kernelgsbase
);
3569 /* XXX: compute hflags from scratch, except for CPL and IIF */
3570 env
->hflags
= hflags
;
3575 #elif defined(TARGET_PPC)
3576 void cpu_save(QEMUFile
*f
, void *opaque
)
3580 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
3585 #elif defined(TARGET_MIPS)
3586 void cpu_save(QEMUFile
*f
, void *opaque
)
3590 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
3595 #elif defined(TARGET_SPARC)
3596 void cpu_save(QEMUFile
*f
, void *opaque
)
3598 CPUState
*env
= opaque
;
3602 for(i
= 0; i
< 8; i
++)
3603 qemu_put_betls(f
, &env
->gregs
[i
]);
3604 for(i
= 0; i
< NWINDOWS
* 16; i
++)
3605 qemu_put_betls(f
, &env
->regbase
[i
]);
3608 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
3614 qemu_put_betl(f
, u
.i
);
3617 qemu_put_betls(f
, &env
->pc
);
3618 qemu_put_betls(f
, &env
->npc
);
3619 qemu_put_betls(f
, &env
->y
);
3621 qemu_put_be32(f
, tmp
);
3622 qemu_put_betls(f
, &env
->fsr
);
3623 qemu_put_betls(f
, &env
->tbr
);
3624 #ifndef TARGET_SPARC64
3625 qemu_put_be32s(f
, &env
->wim
);
3627 for(i
= 0; i
< 16; i
++)
3628 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
3632 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
3634 CPUState
*env
= opaque
;
3638 for(i
= 0; i
< 8; i
++)
3639 qemu_get_betls(f
, &env
->gregs
[i
]);
3640 for(i
= 0; i
< NWINDOWS
* 16; i
++)
3641 qemu_get_betls(f
, &env
->regbase
[i
]);
3644 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
3649 u
.i
= qemu_get_betl(f
);
3653 qemu_get_betls(f
, &env
->pc
);
3654 qemu_get_betls(f
, &env
->npc
);
3655 qemu_get_betls(f
, &env
->y
);
3656 tmp
= qemu_get_be32(f
);
3657 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
3658 correctly updated */
3660 qemu_get_betls(f
, &env
->fsr
);
3661 qemu_get_betls(f
, &env
->tbr
);
3662 #ifndef TARGET_SPARC64
3663 qemu_get_be32s(f
, &env
->wim
);
3665 for(i
= 0; i
< 16; i
++)
3666 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
3672 #elif defined(TARGET_ARM)
3674 /* ??? Need to implement these. */
3675 void cpu_save(QEMUFile
*f
, void *opaque
)
3679 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
3686 #warning No CPU save/restore functions
3690 /***********************************************************/
3691 /* ram save/restore */
3693 /* we just avoid storing empty pages */
3694 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
3699 for(i
= 1; i
< len
; i
++) {
3703 qemu_put_byte(f
, 1);
3704 qemu_put_byte(f
, v
);
3707 qemu_put_byte(f
, 0);
3708 qemu_put_buffer(f
, buf
, len
);
3711 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3715 v
= qemu_get_byte(f
);
3718 if (qemu_get_buffer(f
, buf
, len
) != len
)
3722 v
= qemu_get_byte(f
);
3723 memset(buf
, v
, len
);
3731 static void ram_save(QEMUFile
*f
, void *opaque
)
3734 qemu_put_be32(f
, phys_ram_size
);
3735 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3736 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3740 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3744 if (version_id
!= 1)
3746 if (qemu_get_be32(f
) != phys_ram_size
)
3748 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3749 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3756 /***********************************************************/
3757 /* machine registration */
3759 QEMUMachine
*first_machine
= NULL
;
3761 int qemu_register_machine(QEMUMachine
*m
)
3764 pm
= &first_machine
;
3772 QEMUMachine
*find_machine(const char *name
)
3776 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3777 if (!strcmp(m
->name
, name
))
3783 /***********************************************************/
3784 /* main execution loop */
3786 void gui_update(void *opaque
)
3788 display_state
.dpy_refresh(&display_state
);
3789 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
3792 struct vm_change_state_entry
{
3793 VMChangeStateHandler
*cb
;
3795 LIST_ENTRY (vm_change_state_entry
) entries
;
3798 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3800 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3803 VMChangeStateEntry
*e
;
3805 e
= qemu_mallocz(sizeof (*e
));
3811 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3815 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3817 LIST_REMOVE (e
, entries
);
3821 static void vm_state_notify(int running
)
3823 VMChangeStateEntry
*e
;
3825 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3826 e
->cb(e
->opaque
, running
);
3830 /* XXX: support several handlers */
3831 static VMStopHandler
*vm_stop_cb
;
3832 static void *vm_stop_opaque
;
3834 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3837 vm_stop_opaque
= opaque
;
3841 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3855 void vm_stop(int reason
)
3858 cpu_disable_ticks();
3862 vm_stop_cb(vm_stop_opaque
, reason
);
3869 /* reset/shutdown handler */
3871 typedef struct QEMUResetEntry
{
3872 QEMUResetHandler
*func
;
3874 struct QEMUResetEntry
*next
;
3877 static QEMUResetEntry
*first_reset_entry
;
3878 static int reset_requested
;
3879 static int shutdown_requested
;
3880 static int powerdown_requested
;
3882 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3884 QEMUResetEntry
**pre
, *re
;
3886 pre
= &first_reset_entry
;
3887 while (*pre
!= NULL
)
3888 pre
= &(*pre
)->next
;
3889 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3891 re
->opaque
= opaque
;
3896 void qemu_system_reset(void)
3900 /* reset all devices */
3901 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3902 re
->func(re
->opaque
);
3906 void qemu_system_reset_request(void)
3908 reset_requested
= 1;
3910 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3913 void qemu_system_shutdown_request(void)
3915 shutdown_requested
= 1;
3917 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3920 void qemu_system_powerdown_request(void)
3922 powerdown_requested
= 1;
3924 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3927 void main_loop_wait(int timeout
)
3929 IOHandlerRecord
*ioh
, *ioh_next
;
3935 /* XXX: see how to merge it with the select. The constraint is
3936 that the select must be interrupted by the timer */
3940 /* poll any events */
3941 /* XXX: separate device handlers from system ones */
3945 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3947 (!ioh
->fd_read_poll
||
3948 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3949 FD_SET(ioh
->fd
, &rfds
);
3953 if (ioh
->fd_write
) {
3954 FD_SET(ioh
->fd
, &wfds
);
3964 tv
.tv_usec
= timeout
* 1000;
3966 ret
= select(nfds
+ 1, &rfds
, &wfds
, NULL
, &tv
);
3968 /* XXX: better handling of removal */
3969 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
3970 ioh_next
= ioh
->next
;
3971 if (FD_ISSET(ioh
->fd
, &rfds
)) {
3972 ioh
->fd_read(ioh
->opaque
);
3974 if (FD_ISSET(ioh
->fd
, &wfds
)) {
3975 ioh
->fd_write(ioh
->opaque
);
3983 #if defined(CONFIG_SLIRP)
3984 /* XXX: merge with the previous select() */
3986 fd_set rfds
, wfds
, xfds
;
3994 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3997 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3999 slirp_select_poll(&rfds
, &wfds
, &xfds
);
4005 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4006 qemu_get_clock(vm_clock
));
4007 /* run dma transfers, if any */
4011 /* real time timers */
4012 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4013 qemu_get_clock(rt_clock
));
4016 static CPUState
*cur_cpu
;
4023 cur_cpu
= first_cpu
;
4030 env
= env
->next_cpu
;
4033 ret
= cpu_exec(env
);
4034 if (ret
!= EXCP_HALTED
)
4036 /* all CPUs are halted ? */
4037 if (env
== cur_cpu
) {
4044 if (shutdown_requested
) {
4045 ret
= EXCP_INTERRUPT
;
4048 if (reset_requested
) {
4049 reset_requested
= 0;
4050 qemu_system_reset();
4051 ret
= EXCP_INTERRUPT
;
4053 if (powerdown_requested
) {
4054 powerdown_requested
= 0;
4055 qemu_system_powerdown();
4056 ret
= EXCP_INTERRUPT
;
4058 if (ret
== EXCP_DEBUG
) {
4059 vm_stop(EXCP_DEBUG
);
4061 /* if hlt instruction, we wait until the next IRQ */
4062 /* XXX: use timeout computed from timers */
4063 if (ret
== EXCP_HLT
)
4070 main_loop_wait(timeout
);
4072 cpu_disable_ticks();
4078 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2005 Fabrice Bellard\n"
4079 "usage: %s [options] [disk_image]\n"
4081 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4083 "Standard options:\n"
4084 "-M machine select emulated machine (-M ? for list)\n"
4085 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
4086 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
4087 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
4088 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
4089 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
4090 "-snapshot write to temporary files instead of disk image files\n"
4091 "-m megs set virtual RAM size to megs MB [default=%d]\n"
4092 "-smp n set the number of CPUs to 'n' [default=1]\n"
4093 "-nographic disable graphical output and redirect serial I/Os to console\n"
4095 "-k language use keyboard layout (for example \"fr\" for French)\n"
4098 "-audio-help print list of audio drivers and their options\n"
4099 "-soundhw c1,... enable audio support\n"
4100 " and only specified sound cards (comma separated list)\n"
4101 " use -soundhw ? to get the list of supported cards\n"
4102 " use -soundhw all to enable all of them\n"
4104 "-localtime set the real time clock to local time [default=utc]\n"
4105 "-full-screen start in full screen\n"
4107 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4109 "-usb enable the USB driver (will be the default soon)\n"
4110 "-usbdevice name add the host or guest USB device 'name'\n"
4111 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4112 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4115 "Network options:\n"
4116 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
4117 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4119 "-net user[,vlan=n]\n"
4120 " connect the user mode network stack to VLAN 'n'\n"
4123 "-net tap[,vlan=n],ifname=name\n"
4124 " connect the host TAP network interface to VLAN 'n'\n"
4126 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
4127 " connect the host TAP network interface to VLAN 'n' and use\n"
4128 " the network script 'file' (default=%s);\n"
4129 " use 'fd=h' to connect to an already opened TAP interface\n"
4131 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4132 " connect the vlan 'n' to another VLAN using a socket connection\n"
4133 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
4134 " connect the vlan 'n' to multicast maddr and port\n"
4135 "-net none use it alone to have zero network devices; if no -net option\n"
4136 " is provided, the default is '-net nic -net user'\n"
4139 "-tftp prefix allow tftp access to files starting with prefix [-net user]\n"
4141 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4143 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4144 " redirect TCP or UDP connections from host to guest [-net user]\n"
4147 "Linux boot specific:\n"
4148 "-kernel bzImage use 'bzImage' as kernel image\n"
4149 "-append cmdline use 'cmdline' as kernel command line\n"
4150 "-initrd file use 'file' as initial ram disk\n"
4152 "Debug/Expert options:\n"
4153 "-monitor dev redirect the monitor to char device 'dev'\n"
4154 "-serial dev redirect the serial port to char device 'dev'\n"
4155 "-parallel dev redirect the parallel port to char device 'dev'\n"
4156 "-pidfile file Write PID to 'file'\n"
4157 "-S freeze CPU at startup (use 'c' to start execution)\n"
4158 "-s wait gdb connection to port %d\n"
4159 "-p port change gdb connection port\n"
4160 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4161 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
4162 " translation (t=none or lba) (usually qemu can guess them)\n"
4163 "-L path set the directory for the BIOS and VGA BIOS\n"
4165 "-no-kqemu disable KQEMU kernel module usage\n"
4167 #ifdef USE_CODE_COPY
4168 "-no-code-copy disable code copy acceleration\n"
4171 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
4172 " (default is CL-GD5446 PCI VGA)\n"
4174 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
4176 "During emulation, the following keys are useful:\n"
4177 "ctrl-alt-f toggle full screen\n"
4178 "ctrl-alt-n switch to virtual console 'n'\n"
4179 "ctrl-alt toggle mouse and keyboard grab\n"
4181 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4183 #ifdef CONFIG_SOFTMMU
4190 DEFAULT_NETWORK_SCRIPT
,
4192 DEFAULT_GDBSTUB_PORT
,
4194 #ifndef CONFIG_SOFTMMU
4196 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
4197 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
4203 #define HAS_ARG 0x0001
4217 QEMU_OPTION_snapshot
,
4219 QEMU_OPTION_nographic
,
4221 QEMU_OPTION_audio_help
,
4222 QEMU_OPTION_soundhw
,
4240 QEMU_OPTION_no_code_copy
,
4242 QEMU_OPTION_localtime
,
4243 QEMU_OPTION_cirrusvga
,
4245 QEMU_OPTION_std_vga
,
4246 QEMU_OPTION_monitor
,
4248 QEMU_OPTION_parallel
,
4250 QEMU_OPTION_full_screen
,
4251 QEMU_OPTION_pidfile
,
4252 QEMU_OPTION_no_kqemu
,
4253 QEMU_OPTION_win2k_hack
,
4255 QEMU_OPTION_usbdevice
,
4259 typedef struct QEMUOption
{
4265 const QEMUOption qemu_options
[] = {
4266 { "h", 0, QEMU_OPTION_h
},
4268 { "M", HAS_ARG
, QEMU_OPTION_M
},
4269 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4270 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4271 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4272 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4273 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4274 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4275 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4276 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4277 { "snapshot", 0, QEMU_OPTION_snapshot
},
4278 { "m", HAS_ARG
, QEMU_OPTION_m
},
4279 { "nographic", 0, QEMU_OPTION_nographic
},
4280 { "k", HAS_ARG
, QEMU_OPTION_k
},
4282 { "audio-help", 0, QEMU_OPTION_audio_help
},
4283 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4286 { "net", HAS_ARG
, QEMU_OPTION_net
},
4288 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4290 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4292 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4295 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4296 { "append", HAS_ARG
, QEMU_OPTION_append
},
4297 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4299 { "S", 0, QEMU_OPTION_S
},
4300 { "s", 0, QEMU_OPTION_s
},
4301 { "p", HAS_ARG
, QEMU_OPTION_p
},
4302 { "d", HAS_ARG
, QEMU_OPTION_d
},
4303 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4304 { "L", HAS_ARG
, QEMU_OPTION_L
},
4305 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
4307 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4309 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4310 { "g", 1, QEMU_OPTION_g
},
4312 { "localtime", 0, QEMU_OPTION_localtime
},
4313 { "std-vga", 0, QEMU_OPTION_std_vga
},
4314 { "monitor", 1, QEMU_OPTION_monitor
},
4315 { "serial", 1, QEMU_OPTION_serial
},
4316 { "parallel", 1, QEMU_OPTION_parallel
},
4317 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4318 { "full-screen", 0, QEMU_OPTION_full_screen
},
4319 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4320 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4321 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4322 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4324 /* temporary options */
4325 { "usb", 0, QEMU_OPTION_usb
},
4326 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
4330 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
4332 /* this stack is only used during signal handling */
4333 #define SIGNAL_STACK_SIZE 32768
4335 static uint8_t *signal_stack
;
4339 /* password input */
4341 static BlockDriverState
*get_bdrv(int index
)
4343 BlockDriverState
*bs
;
4346 bs
= bs_table
[index
];
4347 } else if (index
< 6) {
4348 bs
= fd_table
[index
- 4];
4355 static void read_passwords(void)
4357 BlockDriverState
*bs
;
4361 for(i
= 0; i
< 6; i
++) {
4363 if (bs
&& bdrv_is_encrypted(bs
)) {
4364 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
4365 for(j
= 0; j
< 3; j
++) {
4366 monitor_readline("Password: ",
4367 1, password
, sizeof(password
));
4368 if (bdrv_set_key(bs
, password
) == 0)
4370 term_printf("invalid password\n");
4376 /* XXX: currently we cannot use simultaneously different CPUs */
4377 void register_machines(void)
4379 #if defined(TARGET_I386)
4380 qemu_register_machine(&pc_machine
);
4381 qemu_register_machine(&isapc_machine
);
4382 #elif defined(TARGET_PPC)
4383 qemu_register_machine(&heathrow_machine
);
4384 qemu_register_machine(&core99_machine
);
4385 qemu_register_machine(&prep_machine
);
4386 #elif defined(TARGET_MIPS)
4387 qemu_register_machine(&mips_machine
);
4388 #elif defined(TARGET_SPARC)
4389 #ifdef TARGET_SPARC64
4390 qemu_register_machine(&sun4u_machine
);
4392 qemu_register_machine(&sun4m_machine
);
4394 #elif defined(TARGET_ARM)
4395 qemu_register_machine(&integratorcp_machine
);
4397 #error unsupported CPU
4402 struct soundhw soundhw
[] = {
4405 "Creative Sound Blaster 16",
4408 { .init_isa
= SB16_init
}
4415 "Yamaha YMF262 (OPL3)",
4417 "Yamaha YM3812 (OPL2)",
4421 { .init_isa
= Adlib_init
}
4428 "Gravis Ultrasound GF1",
4431 { .init_isa
= GUS_init
}
4437 "ENSONIQ AudioPCI ES1370",
4440 { .init_pci
= es1370_init
}
4443 { NULL
, NULL
, 0, 0, { NULL
} }
4446 static void select_soundhw (const char *optarg
)
4450 if (*optarg
== '?') {
4453 printf ("Valid sound card names (comma separated):\n");
4454 for (c
= soundhw
; c
->name
; ++c
) {
4455 printf ("%-11s %s\n", c
->name
, c
->descr
);
4457 printf ("\n-soundhw all will enable all of the above\n");
4458 exit (*optarg
!= '?');
4466 if (!strcmp (optarg
, "all")) {
4467 for (c
= soundhw
; c
->name
; ++c
) {
4475 e
= strchr (p
, ',');
4476 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4478 for (c
= soundhw
; c
->name
; ++c
) {
4479 if (!strncmp (c
->name
, p
, l
)) {
4488 "Unknown sound card name (too big to show)\n");
4491 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4496 p
+= l
+ (e
!= NULL
);
4500 goto show_valid_cards
;
4505 #define MAX_NET_CLIENTS 32
4507 int main(int argc
, char **argv
)
4509 #ifdef CONFIG_GDBSTUB
4510 int use_gdbstub
, gdbstub_port
;
4513 int snapshot
, linux_boot
;
4514 const char *initrd_filename
;
4515 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
4516 const char *kernel_filename
, *kernel_cmdline
;
4517 DisplayState
*ds
= &display_state
;
4518 int cyls
, heads
, secs
, translation
;
4519 int start_emulation
= 1;
4520 char net_clients
[MAX_NET_CLIENTS
][256];
4523 const char *r
, *optarg
;
4524 CharDriverState
*monitor_hd
;
4525 char monitor_device
[128];
4526 char serial_devices
[MAX_SERIAL_PORTS
][128];
4527 int serial_device_index
;
4528 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
4529 int parallel_device_index
;
4530 const char *loadvm
= NULL
;
4531 QEMUMachine
*machine
;
4532 char usb_devices
[MAX_VM_USB_PORTS
][128];
4533 int usb_devices_index
;
4535 LIST_INIT (&vm_change_state_head
);
4536 #if !defined(CONFIG_SOFTMMU)
4537 /* we never want that malloc() uses mmap() */
4538 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
4540 register_machines();
4541 machine
= first_machine
;
4542 initrd_filename
= NULL
;
4543 for(i
= 0; i
< MAX_FD
; i
++)
4544 fd_filename
[i
] = NULL
;
4545 for(i
= 0; i
< MAX_DISKS
; i
++)
4546 hd_filename
[i
] = NULL
;
4547 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
4548 vga_ram_size
= VGA_RAM_SIZE
;
4549 bios_size
= BIOS_SIZE
;
4550 #ifdef CONFIG_GDBSTUB
4552 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4556 kernel_filename
= NULL
;
4557 kernel_cmdline
= "";
4563 cyls
= heads
= secs
= 0;
4564 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4565 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
4567 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
4568 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4569 serial_devices
[i
][0] = '\0';
4570 serial_device_index
= 0;
4572 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
4573 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4574 parallel_devices
[i
][0] = '\0';
4575 parallel_device_index
= 0;
4577 usb_devices_index
= 0;
4582 /* default mac address of the first network interface */
4590 hd_filename
[0] = argv
[optind
++];
4592 const QEMUOption
*popt
;
4595 popt
= qemu_options
;
4598 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4602 if (!strcmp(popt
->name
, r
+ 1))
4606 if (popt
->flags
& HAS_ARG
) {
4607 if (optind
>= argc
) {
4608 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4612 optarg
= argv
[optind
++];
4617 switch(popt
->index
) {
4619 machine
= find_machine(optarg
);
4622 printf("Supported machines are:\n");
4623 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4624 printf("%-10s %s%s\n",
4626 m
== first_machine
? " (default)" : "");
4631 case QEMU_OPTION_initrd
:
4632 initrd_filename
= optarg
;
4634 case QEMU_OPTION_hda
:
4635 case QEMU_OPTION_hdb
:
4636 case QEMU_OPTION_hdc
:
4637 case QEMU_OPTION_hdd
:
4640 hd_index
= popt
->index
- QEMU_OPTION_hda
;
4641 hd_filename
[hd_index
] = optarg
;
4642 if (hd_index
== cdrom_index
)
4646 case QEMU_OPTION_snapshot
:
4649 case QEMU_OPTION_hdachs
:
4653 cyls
= strtol(p
, (char **)&p
, 0);
4654 if (cyls
< 1 || cyls
> 16383)
4659 heads
= strtol(p
, (char **)&p
, 0);
4660 if (heads
< 1 || heads
> 16)
4665 secs
= strtol(p
, (char **)&p
, 0);
4666 if (secs
< 1 || secs
> 63)
4670 if (!strcmp(p
, "none"))
4671 translation
= BIOS_ATA_TRANSLATION_NONE
;
4672 else if (!strcmp(p
, "lba"))
4673 translation
= BIOS_ATA_TRANSLATION_LBA
;
4674 else if (!strcmp(p
, "auto"))
4675 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4678 } else if (*p
!= '\0') {
4680 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4685 case QEMU_OPTION_nographic
:
4686 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
4687 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
4690 case QEMU_OPTION_kernel
:
4691 kernel_filename
= optarg
;
4693 case QEMU_OPTION_append
:
4694 kernel_cmdline
= optarg
;
4696 case QEMU_OPTION_cdrom
:
4697 if (cdrom_index
>= 0) {
4698 hd_filename
[cdrom_index
] = optarg
;
4701 case QEMU_OPTION_boot
:
4702 boot_device
= optarg
[0];
4703 if (boot_device
!= 'a' &&
4706 boot_device
!= 'n' &&
4708 boot_device
!= 'c' && boot_device
!= 'd') {
4709 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
4713 case QEMU_OPTION_fda
:
4714 fd_filename
[0] = optarg
;
4716 case QEMU_OPTION_fdb
:
4717 fd_filename
[1] = optarg
;
4719 case QEMU_OPTION_no_code_copy
:
4720 code_copy_enabled
= 0;
4722 case QEMU_OPTION_net
:
4723 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4724 fprintf(stderr
, "qemu: too many network clients\n");
4727 pstrcpy(net_clients
[nb_net_clients
],
4728 sizeof(net_clients
[0]),
4733 case QEMU_OPTION_tftp
:
4734 tftp_prefix
= optarg
;
4737 case QEMU_OPTION_smb
:
4738 net_slirp_smb(optarg
);
4741 case QEMU_OPTION_redir
:
4742 net_slirp_redir(optarg
);
4746 case QEMU_OPTION_audio_help
:
4750 case QEMU_OPTION_soundhw
:
4751 select_soundhw (optarg
);
4758 ram_size
= atoi(optarg
) * 1024 * 1024;
4761 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
4762 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
4763 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
4772 mask
= cpu_str_to_log_mask(optarg
);
4774 printf("Log items (comma separated):\n");
4775 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4776 printf("%-10s %s\n", item
->name
, item
->help
);
4783 #ifdef CONFIG_GDBSTUB
4788 gdbstub_port
= atoi(optarg
);
4795 start_emulation
= 0;
4798 keyboard_layout
= optarg
;
4800 case QEMU_OPTION_localtime
:
4803 case QEMU_OPTION_cirrusvga
:
4804 cirrus_vga_enabled
= 1;
4806 case QEMU_OPTION_std_vga
:
4807 cirrus_vga_enabled
= 0;
4814 w
= strtol(p
, (char **)&p
, 10);
4817 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4823 h
= strtol(p
, (char **)&p
, 10);
4828 depth
= strtol(p
, (char **)&p
, 10);
4829 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4830 depth
!= 24 && depth
!= 32)
4832 } else if (*p
== '\0') {
4833 depth
= graphic_depth
;
4840 graphic_depth
= depth
;
4843 case QEMU_OPTION_monitor
:
4844 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
4846 case QEMU_OPTION_serial
:
4847 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
4848 fprintf(stderr
, "qemu: too many serial ports\n");
4851 pstrcpy(serial_devices
[serial_device_index
],
4852 sizeof(serial_devices
[0]), optarg
);
4853 serial_device_index
++;
4855 case QEMU_OPTION_parallel
:
4856 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
4857 fprintf(stderr
, "qemu: too many parallel ports\n");
4860 pstrcpy(parallel_devices
[parallel_device_index
],
4861 sizeof(parallel_devices
[0]), optarg
);
4862 parallel_device_index
++;
4864 case QEMU_OPTION_loadvm
:
4867 case QEMU_OPTION_full_screen
:
4870 case QEMU_OPTION_pidfile
:
4871 create_pidfile(optarg
);
4874 case QEMU_OPTION_win2k_hack
:
4875 win2k_install_hack
= 1;
4879 case QEMU_OPTION_no_kqemu
:
4883 case QEMU_OPTION_usb
:
4886 case QEMU_OPTION_usbdevice
:
4888 if (usb_devices_index
>= MAX_VM_USB_PORTS
) {
4889 fprintf(stderr
, "Too many USB devices\n");
4892 pstrcpy(usb_devices
[usb_devices_index
],
4893 sizeof(usb_devices
[usb_devices_index
]),
4895 usb_devices_index
++;
4897 case QEMU_OPTION_smp
:
4898 smp_cpus
= atoi(optarg
);
4899 if (smp_cpus
< 1 || smp_cpus
> MAX_CPUS
) {
4900 fprintf(stderr
, "Invalid number of CPUs\n");
4912 linux_boot
= (kernel_filename
!= NULL
);
4915 hd_filename
[0] == '\0' &&
4916 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
4917 fd_filename
[0] == '\0')
4920 /* boot to cd by default if no hard disk */
4921 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
4922 if (fd_filename
[0] != '\0')
4928 #if !defined(CONFIG_SOFTMMU)
4929 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
4931 static uint8_t stdout_buf
[4096];
4932 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
4935 setvbuf(stdout
, NULL
, _IOLBF
, 0);
4942 /* init network clients */
4943 if (nb_net_clients
== 0) {
4944 /* if no clients, we use a default config */
4945 pstrcpy(net_clients
[0], sizeof(net_clients
[0]),
4947 pstrcpy(net_clients
[1], sizeof(net_clients
[0]),
4952 for(i
= 0;i
< nb_net_clients
; i
++) {
4953 if (net_client_init(net_clients
[i
]) < 0)
4957 /* init the memory */
4958 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
4960 #ifdef CONFIG_SOFTMMU
4961 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
4962 if (!phys_ram_base
) {
4963 fprintf(stderr
, "Could not allocate physical memory\n");
4967 /* as we must map the same page at several addresses, we must use
4972 tmpdir
= getenv("QEMU_TMPDIR");
4975 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
4976 if (mkstemp(phys_ram_file
) < 0) {
4977 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
4981 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
4982 if (phys_ram_fd
< 0) {
4983 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
4987 ftruncate(phys_ram_fd
, phys_ram_size
);
4988 unlink(phys_ram_file
);
4989 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
4991 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
4993 if (phys_ram_base
== MAP_FAILED
) {
4994 fprintf(stderr
, "Could not map physical memory\n");
5000 /* we always create the cdrom drive, even if no disk is there */
5002 if (cdrom_index
>= 0) {
5003 bs_table
[cdrom_index
] = bdrv_new("cdrom");
5004 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
5007 /* open the virtual block devices */
5008 for(i
= 0; i
< MAX_DISKS
; i
++) {
5009 if (hd_filename
[i
]) {
5012 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
5013 bs_table
[i
] = bdrv_new(buf
);
5015 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
5016 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
5020 if (i
== 0 && cyls
!= 0) {
5021 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
5022 bdrv_set_translation_hint(bs_table
[i
], translation
);
5027 /* we always create at least one floppy disk */
5028 fd_table
[0] = bdrv_new("fda");
5029 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
5031 for(i
= 0; i
< MAX_FD
; i
++) {
5032 if (fd_filename
[i
]) {
5035 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
5036 fd_table
[i
] = bdrv_new(buf
);
5037 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
5039 if (fd_filename
[i
] != '\0') {
5040 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
5041 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
5049 /* init USB devices */
5051 vm_usb_hub
= usb_hub_init(vm_usb_ports
, MAX_VM_USB_PORTS
);
5052 for(i
= 0; i
< usb_devices_index
; i
++) {
5053 if (usb_device_add(usb_devices
[i
]) < 0) {
5054 fprintf(stderr
, "Warning: could not add USB device %s\n",
5060 register_savevm("timer", 0, 1, timer_save
, timer_load
, NULL
);
5061 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
5064 cpu_calibrate_ticks();
5068 dumb_display_init(ds
);
5070 #if defined(CONFIG_SDL)
5071 sdl_display_init(ds
, full_screen
);
5072 #elif defined(CONFIG_COCOA)
5073 cocoa_display_init(ds
, full_screen
);
5075 dumb_display_init(ds
);
5079 vga_console
= graphic_console_init(ds
);
5081 monitor_hd
= qemu_chr_open(monitor_device
);
5083 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5086 monitor_init(monitor_hd
, !nographic
);
5088 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5089 if (serial_devices
[i
][0] != '\0') {
5090 serial_hds
[i
] = qemu_chr_open(serial_devices
[i
]);
5091 if (!serial_hds
[i
]) {
5092 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5096 if (!strcmp(serial_devices
[i
], "vc"))
5097 qemu_chr_printf(serial_hds
[i
], "serial%d console\n", i
);
5101 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5102 if (parallel_devices
[i
][0] != '\0') {
5103 parallel_hds
[i
] = qemu_chr_open(parallel_devices
[i
]);
5104 if (!parallel_hds
[i
]) {
5105 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5106 parallel_devices
[i
]);
5109 if (!strcmp(parallel_devices
[i
], "vc"))
5110 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\n", i
);
5114 /* setup cpu signal handlers for MMU / self modifying code handling */
5115 #if !defined(CONFIG_SOFTMMU)
5117 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5120 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
5121 stk
.ss_sp
= signal_stack
;
5122 stk
.ss_size
= SIGNAL_STACK_SIZE
;
5125 if (sigaltstack(&stk
, NULL
) < 0) {
5126 perror("sigaltstack");
5132 struct sigaction act
;
5134 sigfillset(&act
.sa_mask
);
5135 act
.sa_flags
= SA_SIGINFO
;
5136 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5137 act
.sa_flags
|= SA_ONSTACK
;
5139 act
.sa_sigaction
= host_segv_handler
;
5140 sigaction(SIGSEGV
, &act
, NULL
);
5141 sigaction(SIGBUS
, &act
, NULL
);
5142 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
5143 sigaction(SIGFPE
, &act
, NULL
);
5150 struct sigaction act
;
5151 sigfillset(&act
.sa_mask
);
5153 act
.sa_handler
= SIG_IGN
;
5154 sigaction(SIGPIPE
, &act
, NULL
);
5159 machine
->init(ram_size
, vga_ram_size
, boot_device
,
5160 ds
, fd_filename
, snapshot
,
5161 kernel_filename
, kernel_cmdline
, initrd_filename
);
5163 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
5164 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
5166 #ifdef CONFIG_GDBSTUB
5168 if (gdbserver_start(gdbstub_port
) < 0) {
5169 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
5173 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
5178 qemu_loadvm(loadvm
);
5181 /* XXX: simplify init */
5183 if (start_emulation
) {