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>
50 #include <linux/if_tun.h>
53 #include <linux/rtc.h>
54 #include <linux/ppdev.h>
58 #if defined(CONFIG_SLIRP)
64 #include <sys/timeb.h>
66 #define getopt_long_only getopt_long
67 #define memalign(align, size) malloc(size)
74 #endif /* CONFIG_SDL */
78 #define main qemu_main
79 #endif /* CONFIG_COCOA */
87 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
89 //#define DEBUG_UNUSED_IOPORT
90 //#define DEBUG_IOPORT
92 #if !defined(CONFIG_SOFTMMU)
93 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
95 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
99 #define DEFAULT_RAM_SIZE 144
101 #define DEFAULT_RAM_SIZE 128
104 #define GUI_REFRESH_INTERVAL 30
106 /* XXX: use a two level table to limit memory usage */
107 #define MAX_IOPORTS 65536
109 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
110 char phys_ram_file
[1024];
111 CPUState
*global_env
;
112 CPUState
*cpu_single_env
;
113 void *ioport_opaque
[MAX_IOPORTS
];
114 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
115 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
116 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
119 static DisplayState display_state
;
121 const char* keyboard_layout
= NULL
;
122 int64_t ticks_per_sec
;
123 int boot_device
= 'c';
125 static char network_script
[1024];
126 int pit_min_timer_count
= 0;
128 NetDriverState nd_table
[MAX_NICS
];
129 QEMUTimer
*gui_timer
;
132 int audio_enabled
= 0;
133 int sb16_enabled
= 0;
134 int adlib_enabled
= 0;
136 int es1370_enabled
= 0;
139 int cirrus_vga_enabled
= 1;
141 int graphic_width
= 1024;
142 int graphic_height
= 768;
144 int graphic_width
= 800;
145 int graphic_height
= 600;
147 int graphic_depth
= 15;
149 TextConsole
*vga_console
;
150 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
151 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
153 int win2k_install_hack
= 0;
156 USBPort
*vm_usb_ports
[MAX_VM_USB_PORTS
];
157 USBDevice
*vm_usb_hub
;
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
, ...)
431 fprintf(stderr
, "qemu: hardware error: ");
432 vfprintf(stderr
, fmt
, ap
);
433 fprintf(stderr
, "\n");
435 cpu_dump_state(global_env
, stderr
, fprintf
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
437 cpu_dump_state(global_env
, stderr
, fprintf
, 0);
443 /***********************************************************/
446 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
447 static void *qemu_put_kbd_event_opaque
;
448 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
449 static void *qemu_put_mouse_event_opaque
;
451 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
453 qemu_put_kbd_event_opaque
= opaque
;
454 qemu_put_kbd_event
= func
;
457 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
)
459 qemu_put_mouse_event_opaque
= opaque
;
460 qemu_put_mouse_event
= func
;
463 void kbd_put_keycode(int keycode
)
465 if (qemu_put_kbd_event
) {
466 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
470 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
472 if (qemu_put_mouse_event
) {
473 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
474 dx
, dy
, dz
, buttons_state
);
478 /***********************************************************/
481 #if defined(__powerpc__)
483 static inline uint32_t get_tbl(void)
486 asm volatile("mftb %0" : "=r" (tbl
));
490 static inline uint32_t get_tbu(void)
493 asm volatile("mftbu %0" : "=r" (tbl
));
497 int64_t cpu_get_real_ticks(void)
500 /* NOTE: we test if wrapping has occurred */
506 return ((int64_t)h
<< 32) | l
;
509 #elif defined(__i386__)
511 int64_t cpu_get_real_ticks(void)
514 asm volatile ("rdtsc" : "=A" (val
));
518 #elif defined(__x86_64__)
520 int64_t cpu_get_real_ticks(void)
524 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
531 #elif defined(__ia64)
533 int64_t cpu_get_real_ticks(void)
536 asm volatile ("mov %0 = ar.itc" : "=r"(val
) :: "memory");
540 #elif defined(__s390__)
542 int64_t cpu_get_real_ticks(void)
545 asm volatile("stck 0(%1)" : "=m" (val
) : "a" (&val
) : "cc");
550 #error unsupported CPU
553 static int64_t cpu_ticks_offset
;
554 static int cpu_ticks_enabled
;
556 static inline int64_t cpu_get_ticks(void)
558 if (!cpu_ticks_enabled
) {
559 return cpu_ticks_offset
;
561 return cpu_get_real_ticks() + cpu_ticks_offset
;
565 /* enable cpu_get_ticks() */
566 void cpu_enable_ticks(void)
568 if (!cpu_ticks_enabled
) {
569 cpu_ticks_offset
-= cpu_get_real_ticks();
570 cpu_ticks_enabled
= 1;
574 /* disable cpu_get_ticks() : the clock is stopped. You must not call
575 cpu_get_ticks() after that. */
576 void cpu_disable_ticks(void)
578 if (cpu_ticks_enabled
) {
579 cpu_ticks_offset
= cpu_get_ticks();
580 cpu_ticks_enabled
= 0;
584 static int64_t get_clock(void)
589 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
592 gettimeofday(&tv
, NULL
);
593 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
597 void cpu_calibrate_ticks(void)
602 ticks
= cpu_get_real_ticks();
608 usec
= get_clock() - usec
;
609 ticks
= cpu_get_real_ticks() - ticks
;
610 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
613 /* compute with 96 bit intermediate result: (a*b)/c */
614 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
619 #ifdef WORDS_BIGENDIAN
629 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
630 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
633 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
637 #define QEMU_TIMER_REALTIME 0
638 #define QEMU_TIMER_VIRTUAL 1
642 /* XXX: add frequency */
650 struct QEMUTimer
*next
;
656 static QEMUTimer
*active_timers
[2];
658 static MMRESULT timerID
;
660 /* frequency of the times() clock tick */
661 static int timer_freq
;
664 QEMUClock
*qemu_new_clock(int type
)
667 clock
= qemu_mallocz(sizeof(QEMUClock
));
674 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
678 ts
= qemu_mallocz(sizeof(QEMUTimer
));
685 void qemu_free_timer(QEMUTimer
*ts
)
690 /* stop a timer, but do not dealloc it */
691 void qemu_del_timer(QEMUTimer
*ts
)
695 /* NOTE: this code must be signal safe because
696 qemu_timer_expired() can be called from a signal. */
697 pt
= &active_timers
[ts
->clock
->type
];
710 /* modify the current timer so that it will be fired when current_time
711 >= expire_time. The corresponding callback will be called. */
712 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
718 /* add the timer in the sorted list */
719 /* NOTE: this code must be signal safe because
720 qemu_timer_expired() can be called from a signal. */
721 pt
= &active_timers
[ts
->clock
->type
];
726 if (t
->expire_time
> expire_time
)
730 ts
->expire_time
= expire_time
;
735 int qemu_timer_pending(QEMUTimer
*ts
)
738 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
745 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
749 return (timer_head
->expire_time
<= current_time
);
752 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
758 if (!ts
|| ts
->expire_time
> current_time
)
760 /* remove timer from the list before calling the callback */
761 *ptimer_head
= ts
->next
;
764 /* run the callback (the timer list can be modified) */
769 int64_t qemu_get_clock(QEMUClock
*clock
)
771 switch(clock
->type
) {
772 case QEMU_TIMER_REALTIME
:
774 return GetTickCount();
779 /* Note that using gettimeofday() is not a good solution
780 for timers because its value change when the date is
782 if (timer_freq
== 100) {
783 return times(&tp
) * 10;
785 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
790 case QEMU_TIMER_VIRTUAL
:
791 return cpu_get_ticks();
796 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
798 uint64_t expire_time
;
800 if (qemu_timer_pending(ts
)) {
801 expire_time
= ts
->expire_time
;
805 qemu_put_be64(f
, expire_time
);
808 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
810 uint64_t expire_time
;
812 expire_time
= qemu_get_be64(f
);
813 if (expire_time
!= -1) {
814 qemu_mod_timer(ts
, expire_time
);
820 static void timer_save(QEMUFile
*f
, void *opaque
)
822 if (cpu_ticks_enabled
) {
823 hw_error("cannot save state if virtual timers are running");
825 qemu_put_be64s(f
, &cpu_ticks_offset
);
826 qemu_put_be64s(f
, &ticks_per_sec
);
829 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
833 if (cpu_ticks_enabled
) {
836 qemu_get_be64s(f
, &cpu_ticks_offset
);
837 qemu_get_be64s(f
, &ticks_per_sec
);
842 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
843 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
845 static void host_alarm_handler(int host_signum
)
849 #define DISP_FREQ 1000
851 static int64_t delta_min
= INT64_MAX
;
852 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
854 ti
= qemu_get_clock(vm_clock
);
855 if (last_clock
!= 0) {
856 delta
= ti
- last_clock
;
857 if (delta
< delta_min
)
859 if (delta
> delta_max
)
862 if (++count
== DISP_FREQ
) {
863 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
864 muldiv64(delta_min
, 1000000, ticks_per_sec
),
865 muldiv64(delta_max
, 1000000, ticks_per_sec
),
866 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
867 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
869 delta_min
= INT64_MAX
;
877 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
878 qemu_get_clock(vm_clock
)) ||
879 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
880 qemu_get_clock(rt_clock
))) {
881 /* stop the cpu because a timer occured */
882 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
884 if (global_env
->kqemu_enabled
) {
885 kqemu_cpu_interrupt(global_env
);
893 #if defined(__linux__)
895 #define RTC_FREQ 1024
899 static int start_rtc_timer(void)
901 rtc_fd
= open("/dev/rtc", O_RDONLY
);
904 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
905 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
906 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
907 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
910 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
915 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
921 static int start_rtc_timer(void)
926 #endif /* !defined(__linux__) */
928 #endif /* !defined(_WIN32) */
930 static void init_timers(void)
932 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
933 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
938 timerID
= timeSetEvent(1, // interval (ms)
940 host_alarm_handler
, // function
941 (DWORD
)&count
, // user parameter
942 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
944 perror("failed timer alarm");
948 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
951 struct sigaction act
;
952 struct itimerval itv
;
954 /* get times() syscall frequency */
955 timer_freq
= sysconf(_SC_CLK_TCK
);
958 sigfillset(&act
.sa_mask
);
960 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
961 act
.sa_flags
|= SA_ONSTACK
;
963 act
.sa_handler
= host_alarm_handler
;
964 sigaction(SIGALRM
, &act
, NULL
);
966 itv
.it_interval
.tv_sec
= 0;
967 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
968 itv
.it_value
.tv_sec
= 0;
969 itv
.it_value
.tv_usec
= 10 * 1000;
970 setitimer(ITIMER_REAL
, &itv
, NULL
);
971 /* we probe the tick duration of the kernel to inform the user if
972 the emulated kernel requested a too high timer frequency */
973 getitimer(ITIMER_REAL
, &itv
);
975 #if defined(__linux__)
976 if (itv
.it_interval
.tv_usec
> 1000) {
977 /* try to use /dev/rtc to have a faster timer */
978 if (start_rtc_timer() < 0)
981 itv
.it_interval
.tv_sec
= 0;
982 itv
.it_interval
.tv_usec
= 0;
983 itv
.it_value
.tv_sec
= 0;
984 itv
.it_value
.tv_usec
= 0;
985 setitimer(ITIMER_REAL
, &itv
, NULL
);
988 sigaction(SIGIO
, &act
, NULL
);
989 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
990 fcntl(rtc_fd
, F_SETOWN
, getpid());
992 #endif /* defined(__linux__) */
995 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
1002 void quit_timers(void)
1005 timeKillEvent(timerID
);
1009 /***********************************************************/
1010 /* character device */
1012 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
1014 return s
->chr_write(s
, buf
, len
);
1017 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
1021 return s
->chr_ioctl(s
, cmd
, arg
);
1024 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
1029 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1030 qemu_chr_write(s
, buf
, strlen(buf
));
1034 void qemu_chr_send_event(CharDriverState
*s
, int event
)
1036 if (s
->chr_send_event
)
1037 s
->chr_send_event(s
, event
);
1040 void qemu_chr_add_read_handler(CharDriverState
*s
,
1041 IOCanRWHandler
*fd_can_read
,
1042 IOReadHandler
*fd_read
, void *opaque
)
1044 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
1047 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
1049 s
->chr_event
= chr_event
;
1052 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1057 static void null_chr_add_read_handler(CharDriverState
*chr
,
1058 IOCanRWHandler
*fd_can_read
,
1059 IOReadHandler
*fd_read
, void *opaque
)
1063 CharDriverState
*qemu_chr_open_null(void)
1065 CharDriverState
*chr
;
1067 chr
= qemu_mallocz(sizeof(CharDriverState
));
1070 chr
->chr_write
= null_chr_write
;
1071 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1079 /* for nographic stdio only */
1080 IOCanRWHandler
*fd_can_read
;
1081 IOReadHandler
*fd_read
;
1085 #define STDIO_MAX_CLIENTS 2
1087 static int stdio_nb_clients
;
1088 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1090 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1096 ret
= write(fd
, buf
, len
);
1098 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1100 } else if (ret
== 0) {
1110 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1112 FDCharDriver
*s
= chr
->opaque
;
1113 return unix_write(s
->fd_out
, buf
, len
);
1116 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1117 IOCanRWHandler
*fd_can_read
,
1118 IOReadHandler
*fd_read
, void *opaque
)
1120 FDCharDriver
*s
= chr
->opaque
;
1122 if (s
->fd_in
>= 0) {
1123 if (nographic
&& s
->fd_in
== 0) {
1124 s
->fd_can_read
= fd_can_read
;
1125 s
->fd_read
= fd_read
;
1126 s
->fd_opaque
= opaque
;
1128 qemu_add_fd_read_handler(s
->fd_in
, fd_can_read
, fd_read
, opaque
);
1133 /* open a character device to a unix fd */
1134 CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1136 CharDriverState
*chr
;
1139 chr
= qemu_mallocz(sizeof(CharDriverState
));
1142 s
= qemu_mallocz(sizeof(FDCharDriver
));
1150 chr
->chr_write
= fd_chr_write
;
1151 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1155 CharDriverState
*qemu_chr_open_file_out(const char *file_out
)
1159 fd_out
= open(file_out
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
);
1162 return qemu_chr_open_fd(-1, fd_out
);
1165 CharDriverState
*qemu_chr_open_pipe(const char *filename
)
1169 fd
= open(filename
, O_RDWR
| O_BINARY
);
1172 return qemu_chr_open_fd(fd
, fd
);
1176 /* for STDIO, we handle the case where several clients use it
1179 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1181 #define TERM_FIFO_MAX_SIZE 1
1183 static int term_got_escape
, client_index
;
1184 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
1187 void term_print_help(void)
1190 "C-a h print this help\n"
1191 "C-a x exit emulator\n"
1192 "C-a s save disk data back to file (if -snapshot)\n"
1193 "C-a b send break (magic sysrq)\n"
1194 "C-a c switch between console and monitor\n"
1195 "C-a C-a send C-a\n"
1199 /* called when a char is received */
1200 static void stdio_received_byte(int ch
)
1202 if (term_got_escape
) {
1203 term_got_escape
= 0;
1214 for (i
= 0; i
< MAX_DISKS
; i
++) {
1216 bdrv_commit(bs_table
[i
]);
1221 if (client_index
< stdio_nb_clients
) {
1222 CharDriverState
*chr
;
1225 chr
= stdio_clients
[client_index
];
1227 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1232 if (client_index
>= stdio_nb_clients
)
1234 if (client_index
== 0) {
1235 /* send a new line in the monitor to get the prompt */
1243 } else if (ch
== TERM_ESCAPE
) {
1244 term_got_escape
= 1;
1247 if (client_index
< stdio_nb_clients
) {
1249 CharDriverState
*chr
;
1252 chr
= stdio_clients
[client_index
];
1254 if (s
->fd_can_read(s
->fd_opaque
) > 0) {
1256 s
->fd_read(s
->fd_opaque
, buf
, 1);
1257 } else if (term_fifo_size
== 0) {
1258 term_fifo
[term_fifo_size
++] = ch
;
1264 static int stdio_can_read(void *opaque
)
1266 CharDriverState
*chr
;
1269 if (client_index
< stdio_nb_clients
) {
1270 chr
= stdio_clients
[client_index
];
1272 /* try to flush the queue if needed */
1273 if (term_fifo_size
!= 0 && s
->fd_can_read(s
->fd_opaque
) > 0) {
1274 s
->fd_read(s
->fd_opaque
, term_fifo
, 1);
1277 /* see if we can absorb more chars */
1278 if (term_fifo_size
== 0)
1287 static void stdio_read(void *opaque
, const uint8_t *buf
, int size
)
1290 for(i
= 0; i
< size
; i
++)
1291 stdio_received_byte(buf
[i
]);
1294 /* init terminal so that we can grab keys */
1295 static struct termios oldtty
;
1296 static int old_fd0_flags
;
1298 static void term_exit(void)
1300 tcsetattr (0, TCSANOW
, &oldtty
);
1301 fcntl(0, F_SETFL
, old_fd0_flags
);
1304 static void term_init(void)
1308 tcgetattr (0, &tty
);
1310 old_fd0_flags
= fcntl(0, F_GETFL
);
1312 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1313 |INLCR
|IGNCR
|ICRNL
|IXON
);
1314 tty
.c_oflag
|= OPOST
;
1315 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1316 /* if graphical mode, we allow Ctrl-C handling */
1318 tty
.c_lflag
&= ~ISIG
;
1319 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1322 tty
.c_cc
[VTIME
] = 0;
1324 tcsetattr (0, TCSANOW
, &tty
);
1328 fcntl(0, F_SETFL
, O_NONBLOCK
);
1331 CharDriverState
*qemu_chr_open_stdio(void)
1333 CharDriverState
*chr
;
1336 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1338 chr
= qemu_chr_open_fd(0, 1);
1339 if (stdio_nb_clients
== 0)
1340 qemu_add_fd_read_handler(0, stdio_can_read
, stdio_read
, NULL
);
1341 client_index
= stdio_nb_clients
;
1343 if (stdio_nb_clients
!= 0)
1345 chr
= qemu_chr_open_fd(0, 1);
1347 stdio_clients
[stdio_nb_clients
++] = chr
;
1348 if (stdio_nb_clients
== 1) {
1349 /* set the terminal in raw mode */
1355 #if defined(__linux__)
1356 CharDriverState
*qemu_chr_open_pty(void)
1358 char slave_name
[1024];
1359 int master_fd
, slave_fd
;
1361 /* Not satisfying */
1362 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1365 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1366 return qemu_chr_open_fd(master_fd
, master_fd
);
1369 static void tty_serial_init(int fd
, int speed
,
1370 int parity
, int data_bits
, int stop_bits
)
1376 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1377 speed
, parity
, data_bits
, stop_bits
);
1379 tcgetattr (fd
, &tty
);
1421 cfsetispeed(&tty
, spd
);
1422 cfsetospeed(&tty
, spd
);
1424 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1425 |INLCR
|IGNCR
|ICRNL
|IXON
);
1426 tty
.c_oflag
|= OPOST
;
1427 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1428 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
);
1449 tty
.c_cflag
|= PARENB
;
1452 tty
.c_cflag
|= PARENB
| PARODD
;
1456 tcsetattr (fd
, TCSANOW
, &tty
);
1459 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1461 FDCharDriver
*s
= chr
->opaque
;
1464 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1466 QEMUSerialSetParams
*ssp
= arg
;
1467 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1468 ssp
->data_bits
, ssp
->stop_bits
);
1471 case CHR_IOCTL_SERIAL_SET_BREAK
:
1473 int enable
= *(int *)arg
;
1475 tcsendbreak(s
->fd_in
, 1);
1484 CharDriverState
*qemu_chr_open_tty(const char *filename
)
1486 CharDriverState
*chr
;
1489 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
1492 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1493 tty_serial_init(fd
, 115200, 'N', 8, 1);
1494 chr
= qemu_chr_open_fd(fd
, fd
);
1497 chr
->chr_ioctl
= tty_serial_ioctl
;
1501 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1503 int fd
= (int)chr
->opaque
;
1507 case CHR_IOCTL_PP_READ_DATA
:
1508 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1510 *(uint8_t *)arg
= b
;
1512 case CHR_IOCTL_PP_WRITE_DATA
:
1513 b
= *(uint8_t *)arg
;
1514 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1517 case CHR_IOCTL_PP_READ_CONTROL
:
1518 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1520 *(uint8_t *)arg
= b
;
1522 case CHR_IOCTL_PP_WRITE_CONTROL
:
1523 b
= *(uint8_t *)arg
;
1524 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1527 case CHR_IOCTL_PP_READ_STATUS
:
1528 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1530 *(uint8_t *)arg
= b
;
1538 CharDriverState
*qemu_chr_open_pp(const char *filename
)
1540 CharDriverState
*chr
;
1543 fd
= open(filename
, O_RDWR
);
1547 if (ioctl(fd
, PPCLAIM
) < 0) {
1552 chr
= qemu_mallocz(sizeof(CharDriverState
));
1557 chr
->opaque
= (void *)fd
;
1558 chr
->chr_write
= null_chr_write
;
1559 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1560 chr
->chr_ioctl
= pp_ioctl
;
1565 CharDriverState
*qemu_chr_open_pty(void)
1571 #endif /* !defined(_WIN32) */
1573 CharDriverState
*qemu_chr_open(const char *filename
)
1576 if (!strcmp(filename
, "vc")) {
1577 return text_console_init(&display_state
);
1578 } else if (!strcmp(filename
, "null")) {
1579 return qemu_chr_open_null();
1580 } else if (strstart(filename
, "file:", &p
)) {
1581 return qemu_chr_open_file_out(p
);
1582 } else if (strstart(filename
, "pipe:", &p
)) {
1583 return qemu_chr_open_pipe(p
);
1586 if (!strcmp(filename
, "pty")) {
1587 return qemu_chr_open_pty();
1588 } else if (!strcmp(filename
, "stdio")) {
1589 return qemu_chr_open_stdio();
1592 #if defined(__linux__)
1593 if (strstart(filename
, "/dev/parport", NULL
)) {
1594 return qemu_chr_open_pp(filename
);
1596 if (strstart(filename
, "/dev/", NULL
)) {
1597 return qemu_chr_open_tty(filename
);
1605 /***********************************************************/
1606 /* Linux network device redirectors */
1608 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
1612 for(i
=0;i
<size
;i
+=16) {
1616 fprintf(f
, "%08x ", i
);
1619 fprintf(f
, " %02x", buf
[i
+j
]);
1624 for(j
=0;j
<len
;j
++) {
1626 if (c
< ' ' || c
> '~')
1628 fprintf(f
, "%c", c
);
1634 void qemu_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1636 nd
->send_packet(nd
, buf
, size
);
1639 void qemu_add_read_packet(NetDriverState
*nd
, IOCanRWHandler
*fd_can_read
,
1640 IOReadHandler
*fd_read
, void *opaque
)
1642 nd
->add_read_packet(nd
, fd_can_read
, fd_read
, opaque
);
1645 /* dummy network adapter */
1647 static void dummy_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1651 static void dummy_add_read_packet(NetDriverState
*nd
,
1652 IOCanRWHandler
*fd_can_read
,
1653 IOReadHandler
*fd_read
, void *opaque
)
1657 static int net_dummy_init(NetDriverState
*nd
)
1659 nd
->send_packet
= dummy_send_packet
;
1660 nd
->add_read_packet
= dummy_add_read_packet
;
1661 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "dummy");
1665 #if defined(CONFIG_SLIRP)
1667 /* slirp network adapter */
1669 static void *slirp_fd_opaque
;
1670 static IOCanRWHandler
*slirp_fd_can_read
;
1671 static IOReadHandler
*slirp_fd_read
;
1672 static int slirp_inited
;
1674 int slirp_can_output(void)
1676 return slirp_fd_can_read(slirp_fd_opaque
);
1679 void slirp_output(const uint8_t *pkt
, int pkt_len
)
1682 printf("output:\n");
1683 hex_dump(stdout
, pkt
, pkt_len
);
1685 slirp_fd_read(slirp_fd_opaque
, pkt
, pkt_len
);
1688 static void slirp_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1692 hex_dump(stdout
, buf
, size
);
1694 slirp_input(buf
, size
);
1697 static void slirp_add_read_packet(NetDriverState
*nd
,
1698 IOCanRWHandler
*fd_can_read
,
1699 IOReadHandler
*fd_read
, void *opaque
)
1701 slirp_fd_opaque
= opaque
;
1702 slirp_fd_can_read
= fd_can_read
;
1703 slirp_fd_read
= fd_read
;
1706 static int net_slirp_init(NetDriverState
*nd
)
1708 if (!slirp_inited
) {
1712 nd
->send_packet
= slirp_send_packet
;
1713 nd
->add_read_packet
= slirp_add_read_packet
;
1714 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "slirp");
1718 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
1723 p1
= strchr(p
, sep
);
1729 if (len
> buf_size
- 1)
1731 memcpy(buf
, p
, len
);
1738 static void net_slirp_redir(const char *redir_str
)
1743 struct in_addr guest_addr
;
1744 int host_port
, guest_port
;
1746 if (!slirp_inited
) {
1752 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
1754 if (!strcmp(buf
, "tcp")) {
1756 } else if (!strcmp(buf
, "udp")) {
1762 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
1764 host_port
= strtol(buf
, &r
, 0);
1768 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
1770 if (buf
[0] == '\0') {
1771 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
1773 if (!inet_aton(buf
, &guest_addr
))
1776 guest_port
= strtol(p
, &r
, 0);
1780 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
1781 fprintf(stderr
, "qemu: could not set up redirection\n");
1786 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
1794 static void smb_exit(void)
1798 char filename
[1024];
1800 /* erase all the files in the directory */
1801 d
= opendir(smb_dir
);
1806 if (strcmp(de
->d_name
, ".") != 0 &&
1807 strcmp(de
->d_name
, "..") != 0) {
1808 snprintf(filename
, sizeof(filename
), "%s/%s",
1809 smb_dir
, de
->d_name
);
1817 /* automatic user mode samba server configuration */
1818 void net_slirp_smb(const char *exported_dir
)
1820 char smb_conf
[1024];
1821 char smb_cmdline
[1024];
1824 if (!slirp_inited
) {
1829 /* XXX: better tmp dir construction */
1830 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
1831 if (mkdir(smb_dir
, 0700) < 0) {
1832 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
1835 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
1837 f
= fopen(smb_conf
, "w");
1839 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
1846 "socket address=127.0.0.1\n"
1847 "pid directory=%s\n"
1848 "lock directory=%s\n"
1849 "log file=%s/log.smbd\n"
1850 "smb passwd file=%s/smbpasswd\n"
1851 "security = share\n"
1866 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "/usr/sbin/smbd -s %s",
1869 slirp_add_exec(0, smb_cmdline
, 4, 139);
1872 #endif /* !defined(_WIN32) */
1874 #endif /* CONFIG_SLIRP */
1876 #if !defined(_WIN32)
1878 static int tun_open(char *ifname
, int ifname_size
)
1884 fd
= open("/dev/tap", O_RDWR
);
1886 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
1891 dev
= devname(s
.st_rdev
, S_IFCHR
);
1892 pstrcpy(ifname
, ifname_size
, dev
);
1894 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1898 static int tun_open(char *ifname
, int ifname_size
)
1903 fd
= open("/dev/net/tun", O_RDWR
);
1905 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1908 memset(&ifr
, 0, sizeof(ifr
));
1909 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1910 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
1911 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1913 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1917 printf("Connected to host network interface: %s\n", ifr
.ifr_name
);
1918 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1919 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1924 static void tun_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1926 write(nd
->fd
, buf
, size
);
1929 static void tun_add_read_packet(NetDriverState
*nd
,
1930 IOCanRWHandler
*fd_can_read
,
1931 IOReadHandler
*fd_read
, void *opaque
)
1933 qemu_add_fd_read_handler(nd
->fd
, fd_can_read
, fd_read
, opaque
);
1936 static int net_tun_init(NetDriverState
*nd
)
1942 nd
->fd
= tun_open(nd
->ifname
, sizeof(nd
->ifname
));
1946 /* try to launch network init script */
1951 *parg
++ = network_script
;
1952 *parg
++ = nd
->ifname
;
1954 execv(network_script
, args
);
1957 while (waitpid(pid
, &status
, 0) != pid
);
1958 if (!WIFEXITED(status
) ||
1959 WEXITSTATUS(status
) != 0) {
1960 fprintf(stderr
, "%s: could not launch network script\n",
1964 nd
->send_packet
= tun_send_packet
;
1965 nd
->add_read_packet
= tun_add_read_packet
;
1969 static int net_fd_init(NetDriverState
*nd
, int fd
)
1972 nd
->send_packet
= tun_send_packet
;
1973 nd
->add_read_packet
= tun_add_read_packet
;
1974 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "tunfd");
1978 #endif /* !_WIN32 */
1980 /***********************************************************/
1983 static int usb_device_add(const char *devname
)
1991 for(i
= 0;i
< MAX_VM_USB_PORTS
; i
++) {
1992 if (!vm_usb_ports
[i
]->dev
)
1995 if (i
== MAX_VM_USB_PORTS
)
1998 if (strstart(devname
, "host:", &p
)) {
1999 dev
= usb_host_device_open(p
);
2002 } else if (!strcmp(devname
, "mouse")) {
2003 dev
= usb_mouse_init();
2009 usb_attach(vm_usb_ports
[i
], dev
);
2013 static int usb_device_del(const char *devname
)
2016 int bus_num
, addr
, i
;
2022 p
= strchr(devname
, '.');
2025 bus_num
= strtoul(devname
, NULL
, 0);
2026 addr
= strtoul(p
+ 1, NULL
, 0);
2029 for(i
= 0;i
< MAX_VM_USB_PORTS
; i
++) {
2030 dev
= vm_usb_ports
[i
]->dev
;
2031 if (dev
&& dev
->addr
== addr
)
2034 if (i
== MAX_VM_USB_PORTS
)
2036 usb_attach(vm_usb_ports
[i
], NULL
);
2040 void do_usb_add(const char *devname
)
2043 ret
= usb_device_add(devname
);
2045 term_printf("Could not add USB device '%s'\n", devname
);
2048 void do_usb_del(const char *devname
)
2051 ret
= usb_device_del(devname
);
2053 term_printf("Could not remove USB device '%s'\n", devname
);
2060 const char *speed_str
;
2063 term_printf("USB support not enabled\n");
2067 for(i
= 0; i
< MAX_VM_USB_PORTS
; i
++) {
2068 dev
= vm_usb_ports
[i
]->dev
;
2070 term_printf("Hub port %d:\n", i
);
2071 switch(dev
->speed
) {
2075 case USB_SPEED_FULL
:
2078 case USB_SPEED_HIGH
:
2085 term_printf(" Device %d.%d, speed %s Mb/s\n",
2086 0, dev
->addr
, speed_str
);
2091 /***********************************************************/
2094 static char *pid_filename
;
2096 /* Remove PID file. Called on normal exit */
2098 static void remove_pidfile(void)
2100 unlink (pid_filename
);
2103 static void create_pidfile(const char *filename
)
2105 struct stat pidstat
;
2108 /* Try to write our PID to the named file */
2109 if (stat(filename
, &pidstat
) < 0) {
2110 if (errno
== ENOENT
) {
2111 if ((f
= fopen (filename
, "w")) == NULL
) {
2112 perror("Opening pidfile");
2115 fprintf(f
, "%d\n", getpid());
2117 pid_filename
= qemu_strdup(filename
);
2118 if (!pid_filename
) {
2119 fprintf(stderr
, "Could not save PID filename");
2122 atexit(remove_pidfile
);
2125 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
2131 /***********************************************************/
2134 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
2138 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
2142 static void dumb_refresh(DisplayState
*ds
)
2144 vga_update_display();
2147 void dumb_display_init(DisplayState
*ds
)
2152 ds
->dpy_update
= dumb_update
;
2153 ds
->dpy_resize
= dumb_resize
;
2154 ds
->dpy_refresh
= dumb_refresh
;
2157 #if !defined(CONFIG_SOFTMMU)
2158 /***********************************************************/
2159 /* cpu signal handler */
2160 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
2163 if (cpu_signal_handler(host_signum
, info
, puc
))
2165 if (stdio_nb_clients
> 0)
2171 /***********************************************************/
2174 #define MAX_IO_HANDLERS 64
2176 typedef struct IOHandlerRecord
{
2178 IOCanRWHandler
*fd_can_read
;
2179 IOReadHandler
*fd_read
;
2181 /* temporary data */
2184 struct IOHandlerRecord
*next
;
2187 static IOHandlerRecord
*first_io_handler
;
2189 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
2190 IOReadHandler
*fd_read
, void *opaque
)
2192 IOHandlerRecord
*ioh
;
2194 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2198 ioh
->fd_can_read
= fd_can_read
;
2199 ioh
->fd_read
= fd_read
;
2200 ioh
->opaque
= opaque
;
2201 ioh
->next
= first_io_handler
;
2202 first_io_handler
= ioh
;
2206 void qemu_del_fd_read_handler(int fd
)
2208 IOHandlerRecord
**pioh
, *ioh
;
2210 pioh
= &first_io_handler
;
2215 if (ioh
->fd
== fd
) {
2223 /***********************************************************/
2224 /* savevm/loadvm support */
2226 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
2228 fwrite(buf
, 1, size
, f
);
2231 void qemu_put_byte(QEMUFile
*f
, int v
)
2236 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
2238 qemu_put_byte(f
, v
>> 8);
2239 qemu_put_byte(f
, v
);
2242 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
2244 qemu_put_byte(f
, v
>> 24);
2245 qemu_put_byte(f
, v
>> 16);
2246 qemu_put_byte(f
, v
>> 8);
2247 qemu_put_byte(f
, v
);
2250 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
2252 qemu_put_be32(f
, v
>> 32);
2253 qemu_put_be32(f
, v
);
2256 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
2258 return fread(buf
, 1, size
, f
);
2261 int qemu_get_byte(QEMUFile
*f
)
2271 unsigned int qemu_get_be16(QEMUFile
*f
)
2274 v
= qemu_get_byte(f
) << 8;
2275 v
|= qemu_get_byte(f
);
2279 unsigned int qemu_get_be32(QEMUFile
*f
)
2282 v
= qemu_get_byte(f
) << 24;
2283 v
|= qemu_get_byte(f
) << 16;
2284 v
|= qemu_get_byte(f
) << 8;
2285 v
|= qemu_get_byte(f
);
2289 uint64_t qemu_get_be64(QEMUFile
*f
)
2292 v
= (uint64_t)qemu_get_be32(f
) << 32;
2293 v
|= qemu_get_be32(f
);
2297 int64_t qemu_ftell(QEMUFile
*f
)
2302 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
2304 if (fseek(f
, pos
, whence
) < 0)
2309 typedef struct SaveStateEntry
{
2313 SaveStateHandler
*save_state
;
2314 LoadStateHandler
*load_state
;
2316 struct SaveStateEntry
*next
;
2319 static SaveStateEntry
*first_se
;
2321 int register_savevm(const char *idstr
,
2324 SaveStateHandler
*save_state
,
2325 LoadStateHandler
*load_state
,
2328 SaveStateEntry
*se
, **pse
;
2330 se
= qemu_malloc(sizeof(SaveStateEntry
));
2333 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
2334 se
->instance_id
= instance_id
;
2335 se
->version_id
= version_id
;
2336 se
->save_state
= save_state
;
2337 se
->load_state
= load_state
;
2338 se
->opaque
= opaque
;
2341 /* add at the end of list */
2343 while (*pse
!= NULL
)
2344 pse
= &(*pse
)->next
;
2349 #define QEMU_VM_FILE_MAGIC 0x5145564d
2350 #define QEMU_VM_FILE_VERSION 0x00000001
2352 int qemu_savevm(const char *filename
)
2356 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
2358 saved_vm_running
= vm_running
;
2361 f
= fopen(filename
, "wb");
2367 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
2368 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
2370 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
2372 len
= strlen(se
->idstr
);
2373 qemu_put_byte(f
, len
);
2374 qemu_put_buffer(f
, se
->idstr
, len
);
2376 qemu_put_be32(f
, se
->instance_id
);
2377 qemu_put_be32(f
, se
->version_id
);
2379 /* record size: filled later */
2381 qemu_put_be32(f
, 0);
2383 se
->save_state(f
, se
->opaque
);
2385 /* fill record size */
2387 len
= ftell(f
) - len_pos
- 4;
2388 fseek(f
, len_pos
, SEEK_SET
);
2389 qemu_put_be32(f
, len
);
2390 fseek(f
, cur_pos
, SEEK_SET
);
2396 if (saved_vm_running
)
2401 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
2405 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
2406 if (!strcmp(se
->idstr
, idstr
) &&
2407 instance_id
== se
->instance_id
)
2413 int qemu_loadvm(const char *filename
)
2417 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
2418 int saved_vm_running
;
2422 saved_vm_running
= vm_running
;
2425 f
= fopen(filename
, "rb");
2431 v
= qemu_get_be32(f
);
2432 if (v
!= QEMU_VM_FILE_MAGIC
)
2434 v
= qemu_get_be32(f
);
2435 if (v
!= QEMU_VM_FILE_VERSION
) {
2442 #if defined (DO_TB_FLUSH)
2443 tb_flush(global_env
);
2445 len
= qemu_get_byte(f
);
2448 qemu_get_buffer(f
, idstr
, len
);
2450 instance_id
= qemu_get_be32(f
);
2451 version_id
= qemu_get_be32(f
);
2452 record_len
= qemu_get_be32(f
);
2454 printf("idstr=%s instance=0x%x version=%d len=%d\n",
2455 idstr
, instance_id
, version_id
, record_len
);
2458 se
= find_se(idstr
, instance_id
);
2460 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
2461 instance_id
, idstr
);
2463 ret
= se
->load_state(f
, se
->opaque
, version_id
);
2465 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2466 instance_id
, idstr
);
2469 /* always seek to exact end of record */
2470 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
2475 if (saved_vm_running
)
2480 /***********************************************************/
2481 /* cpu save/restore */
2483 #if defined(TARGET_I386)
2485 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
2487 qemu_put_be32(f
, dt
->selector
);
2488 qemu_put_betl(f
, dt
->base
);
2489 qemu_put_be32(f
, dt
->limit
);
2490 qemu_put_be32(f
, dt
->flags
);
2493 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
2495 dt
->selector
= qemu_get_be32(f
);
2496 dt
->base
= qemu_get_betl(f
);
2497 dt
->limit
= qemu_get_be32(f
);
2498 dt
->flags
= qemu_get_be32(f
);
2501 void cpu_save(QEMUFile
*f
, void *opaque
)
2503 CPUState
*env
= opaque
;
2504 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
2508 for(i
= 0; i
< CPU_NB_REGS
; i
++)
2509 qemu_put_betls(f
, &env
->regs
[i
]);
2510 qemu_put_betls(f
, &env
->eip
);
2511 qemu_put_betls(f
, &env
->eflags
);
2512 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
2513 qemu_put_be32s(f
, &hflags
);
2517 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
2519 for(i
= 0; i
< 8; i
++) {
2520 fptag
|= ((!env
->fptags
[i
]) << i
);
2523 qemu_put_be16s(f
, &fpuc
);
2524 qemu_put_be16s(f
, &fpus
);
2525 qemu_put_be16s(f
, &fptag
);
2527 #ifdef USE_X86LDOUBLE
2532 qemu_put_be16s(f
, &fpregs_format
);
2534 for(i
= 0; i
< 8; i
++) {
2535 #ifdef USE_X86LDOUBLE
2539 /* we save the real CPU data (in case of MMX usage only 'mant'
2540 contains the MMX register */
2541 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
2542 qemu_put_be64(f
, mant
);
2543 qemu_put_be16(f
, exp
);
2546 /* if we use doubles for float emulation, we save the doubles to
2547 avoid losing information in case of MMX usage. It can give
2548 problems if the image is restored on a CPU where long
2549 doubles are used instead. */
2550 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
2554 for(i
= 0; i
< 6; i
++)
2555 cpu_put_seg(f
, &env
->segs
[i
]);
2556 cpu_put_seg(f
, &env
->ldt
);
2557 cpu_put_seg(f
, &env
->tr
);
2558 cpu_put_seg(f
, &env
->gdt
);
2559 cpu_put_seg(f
, &env
->idt
);
2561 qemu_put_be32s(f
, &env
->sysenter_cs
);
2562 qemu_put_be32s(f
, &env
->sysenter_esp
);
2563 qemu_put_be32s(f
, &env
->sysenter_eip
);
2565 qemu_put_betls(f
, &env
->cr
[0]);
2566 qemu_put_betls(f
, &env
->cr
[2]);
2567 qemu_put_betls(f
, &env
->cr
[3]);
2568 qemu_put_betls(f
, &env
->cr
[4]);
2570 for(i
= 0; i
< 8; i
++)
2571 qemu_put_betls(f
, &env
->dr
[i
]);
2574 qemu_put_be32s(f
, &env
->a20_mask
);
2577 qemu_put_be32s(f
, &env
->mxcsr
);
2578 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
2579 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
2580 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
2583 #ifdef TARGET_X86_64
2584 qemu_put_be64s(f
, &env
->efer
);
2585 qemu_put_be64s(f
, &env
->star
);
2586 qemu_put_be64s(f
, &env
->lstar
);
2587 qemu_put_be64s(f
, &env
->cstar
);
2588 qemu_put_be64s(f
, &env
->fmask
);
2589 qemu_put_be64s(f
, &env
->kernelgsbase
);
2593 #ifdef USE_X86LDOUBLE
2594 /* XXX: add that in a FPU generic layer */
2595 union x86_longdouble
{
2600 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
2601 #define EXPBIAS1 1023
2602 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
2603 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
2605 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
2609 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
2610 /* exponent + sign */
2611 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
2612 e
|= SIGND1(temp
) >> 16;
2617 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
2619 CPUState
*env
= opaque
;
2622 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
2624 if (version_id
!= 3)
2626 for(i
= 0; i
< CPU_NB_REGS
; i
++)
2627 qemu_get_betls(f
, &env
->regs
[i
]);
2628 qemu_get_betls(f
, &env
->eip
);
2629 qemu_get_betls(f
, &env
->eflags
);
2630 qemu_get_be32s(f
, &hflags
);
2632 qemu_get_be16s(f
, &fpuc
);
2633 qemu_get_be16s(f
, &fpus
);
2634 qemu_get_be16s(f
, &fptag
);
2635 qemu_get_be16s(f
, &fpregs_format
);
2637 /* NOTE: we cannot always restore the FPU state if the image come
2638 from a host with a different 'USE_X86LDOUBLE' define. We guess
2639 if we are in an MMX state to restore correctly in that case. */
2640 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
2641 for(i
= 0; i
< 8; i
++) {
2645 switch(fpregs_format
) {
2647 mant
= qemu_get_be64(f
);
2648 exp
= qemu_get_be16(f
);
2649 #ifdef USE_X86LDOUBLE
2650 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
2652 /* difficult case */
2654 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
2656 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
2660 mant
= qemu_get_be64(f
);
2661 #ifdef USE_X86LDOUBLE
2663 union x86_longdouble
*p
;
2664 /* difficult case */
2665 p
= (void *)&env
->fpregs
[i
];
2670 fp64_to_fp80(p
, mant
);
2674 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
2683 /* XXX: restore FPU round state */
2684 env
->fpstt
= (fpus
>> 11) & 7;
2685 env
->fpus
= fpus
& ~0x3800;
2687 for(i
= 0; i
< 8; i
++) {
2688 env
->fptags
[i
] = (fptag
>> i
) & 1;
2691 for(i
= 0; i
< 6; i
++)
2692 cpu_get_seg(f
, &env
->segs
[i
]);
2693 cpu_get_seg(f
, &env
->ldt
);
2694 cpu_get_seg(f
, &env
->tr
);
2695 cpu_get_seg(f
, &env
->gdt
);
2696 cpu_get_seg(f
, &env
->idt
);
2698 qemu_get_be32s(f
, &env
->sysenter_cs
);
2699 qemu_get_be32s(f
, &env
->sysenter_esp
);
2700 qemu_get_be32s(f
, &env
->sysenter_eip
);
2702 qemu_get_betls(f
, &env
->cr
[0]);
2703 qemu_get_betls(f
, &env
->cr
[2]);
2704 qemu_get_betls(f
, &env
->cr
[3]);
2705 qemu_get_betls(f
, &env
->cr
[4]);
2707 for(i
= 0; i
< 8; i
++)
2708 qemu_get_betls(f
, &env
->dr
[i
]);
2711 qemu_get_be32s(f
, &env
->a20_mask
);
2713 qemu_get_be32s(f
, &env
->mxcsr
);
2714 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
2715 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
2716 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
2719 #ifdef TARGET_X86_64
2720 qemu_get_be64s(f
, &env
->efer
);
2721 qemu_get_be64s(f
, &env
->star
);
2722 qemu_get_be64s(f
, &env
->lstar
);
2723 qemu_get_be64s(f
, &env
->cstar
);
2724 qemu_get_be64s(f
, &env
->fmask
);
2725 qemu_get_be64s(f
, &env
->kernelgsbase
);
2728 /* XXX: compute hflags from scratch, except for CPL and IIF */
2729 env
->hflags
= hflags
;
2734 #elif defined(TARGET_PPC)
2735 void cpu_save(QEMUFile
*f
, void *opaque
)
2739 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
2744 #elif defined(TARGET_MIPS)
2745 void cpu_save(QEMUFile
*f
, void *opaque
)
2749 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
2754 #elif defined(TARGET_SPARC)
2755 void cpu_save(QEMUFile
*f
, void *opaque
)
2757 CPUState
*env
= opaque
;
2761 for(i
= 0; i
< 8; i
++)
2762 qemu_put_betls(f
, &env
->gregs
[i
]);
2763 for(i
= 0; i
< NWINDOWS
* 16; i
++)
2764 qemu_put_betls(f
, &env
->regbase
[i
]);
2767 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
2773 qemu_put_betl(f
, u
.i
);
2776 qemu_put_betls(f
, &env
->pc
);
2777 qemu_put_betls(f
, &env
->npc
);
2778 qemu_put_betls(f
, &env
->y
);
2780 qemu_put_be32(f
, tmp
);
2781 qemu_put_betls(f
, &env
->fsr
);
2782 qemu_put_betls(f
, &env
->tbr
);
2783 #ifndef TARGET_SPARC64
2784 qemu_put_be32s(f
, &env
->wim
);
2786 for(i
= 0; i
< 16; i
++)
2787 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
2791 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
2793 CPUState
*env
= opaque
;
2797 for(i
= 0; i
< 8; i
++)
2798 qemu_get_betls(f
, &env
->gregs
[i
]);
2799 for(i
= 0; i
< NWINDOWS
* 16; i
++)
2800 qemu_get_betls(f
, &env
->regbase
[i
]);
2803 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
2808 u
.i
= qemu_get_betl(f
);
2812 qemu_get_betls(f
, &env
->pc
);
2813 qemu_get_betls(f
, &env
->npc
);
2814 qemu_get_betls(f
, &env
->y
);
2815 tmp
= qemu_get_be32(f
);
2816 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
2817 correctly updated */
2819 qemu_get_betls(f
, &env
->fsr
);
2820 qemu_get_betls(f
, &env
->tbr
);
2821 #ifndef TARGET_SPARC64
2822 qemu_get_be32s(f
, &env
->wim
);
2824 for(i
= 0; i
< 16; i
++)
2825 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
2832 #warning No CPU save/restore functions
2836 /***********************************************************/
2837 /* ram save/restore */
2839 /* we just avoid storing empty pages */
2840 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
2845 for(i
= 1; i
< len
; i
++) {
2849 qemu_put_byte(f
, 1);
2850 qemu_put_byte(f
, v
);
2853 qemu_put_byte(f
, 0);
2854 qemu_put_buffer(f
, buf
, len
);
2857 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2861 v
= qemu_get_byte(f
);
2864 if (qemu_get_buffer(f
, buf
, len
) != len
)
2868 v
= qemu_get_byte(f
);
2869 memset(buf
, v
, len
);
2877 static void ram_save(QEMUFile
*f
, void *opaque
)
2880 qemu_put_be32(f
, phys_ram_size
);
2881 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
2882 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
2886 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2890 if (version_id
!= 1)
2892 if (qemu_get_be32(f
) != phys_ram_size
)
2894 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
2895 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
2902 /***********************************************************/
2903 /* machine registration */
2905 QEMUMachine
*first_machine
= NULL
;
2907 int qemu_register_machine(QEMUMachine
*m
)
2910 pm
= &first_machine
;
2918 QEMUMachine
*find_machine(const char *name
)
2922 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2923 if (!strcmp(m
->name
, name
))
2929 /***********************************************************/
2930 /* main execution loop */
2932 void gui_update(void *opaque
)
2934 display_state
.dpy_refresh(&display_state
);
2935 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
2938 /* XXX: support several handlers */
2939 VMStopHandler
*vm_stop_cb
;
2940 VMStopHandler
*vm_stop_opaque
;
2942 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
2945 vm_stop_opaque
= opaque
;
2949 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
2962 void vm_stop(int reason
)
2965 cpu_disable_ticks();
2969 vm_stop_cb(vm_stop_opaque
, reason
);
2975 /* reset/shutdown handler */
2977 typedef struct QEMUResetEntry
{
2978 QEMUResetHandler
*func
;
2980 struct QEMUResetEntry
*next
;
2983 static QEMUResetEntry
*first_reset_entry
;
2984 static int reset_requested
;
2985 static int shutdown_requested
;
2986 static int powerdown_requested
;
2988 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
2990 QEMUResetEntry
**pre
, *re
;
2992 pre
= &first_reset_entry
;
2993 while (*pre
!= NULL
)
2994 pre
= &(*pre
)->next
;
2995 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
2997 re
->opaque
= opaque
;
3002 void qemu_system_reset(void)
3006 /* reset all devices */
3007 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3008 re
->func(re
->opaque
);
3012 void qemu_system_reset_request(void)
3014 reset_requested
= 1;
3015 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3018 void qemu_system_shutdown_request(void)
3020 shutdown_requested
= 1;
3021 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3024 void qemu_system_powerdown_request(void)
3026 powerdown_requested
= 1;
3027 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3030 static void main_cpu_reset(void *opaque
)
3032 #if defined(TARGET_I386) || defined(TARGET_SPARC)
3033 CPUState
*env
= opaque
;
3038 void main_loop_wait(int timeout
)
3041 struct pollfd ufds
[MAX_IO_HANDLERS
+ 1], *pf
;
3042 IOHandlerRecord
*ioh
, *ioh_next
;
3052 /* poll any events */
3053 /* XXX: separate device handlers from system ones */
3055 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3056 if (!ioh
->fd_can_read
) {
3059 pf
->events
= POLLIN
;
3063 max_size
= ioh
->fd_can_read(ioh
->opaque
);
3065 if (max_size
> sizeof(buf
))
3066 max_size
= sizeof(buf
);
3068 pf
->events
= POLLIN
;
3075 ioh
->max_size
= max_size
;
3078 ret
= poll(ufds
, pf
- ufds
, timeout
);
3080 /* XXX: better handling of removal */
3081 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
3082 ioh_next
= ioh
->next
;
3085 if (pf
->revents
& POLLIN
) {
3086 if (ioh
->max_size
== 0) {
3087 /* just a read event */
3088 ioh
->fd_read(ioh
->opaque
, NULL
, 0);
3090 n
= read(ioh
->fd
, buf
, ioh
->max_size
);
3092 ioh
->fd_read(ioh
->opaque
, buf
, n
);
3093 } else if (errno
!= EAGAIN
) {
3094 ioh
->fd_read(ioh
->opaque
, NULL
, -errno
);
3101 #endif /* !defined(_WIN32) */
3102 #if defined(CONFIG_SLIRP)
3103 /* XXX: merge with poll() */
3105 fd_set rfds
, wfds
, xfds
;
3113 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3116 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3118 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3124 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3125 qemu_get_clock(vm_clock
));
3126 /* run dma transfers, if any */
3130 /* real time timers */
3131 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3132 qemu_get_clock(rt_clock
));
3138 CPUState
*env
= global_env
;
3142 ret
= cpu_exec(env
);
3143 if (shutdown_requested
) {
3144 ret
= EXCP_INTERRUPT
;
3147 if (reset_requested
) {
3148 reset_requested
= 0;
3149 qemu_system_reset();
3150 ret
= EXCP_INTERRUPT
;
3152 if (powerdown_requested
) {
3153 powerdown_requested
= 0;
3154 qemu_system_powerdown();
3155 ret
= EXCP_INTERRUPT
;
3157 if (ret
== EXCP_DEBUG
) {
3158 vm_stop(EXCP_DEBUG
);
3160 /* if hlt instruction, we wait until the next IRQ */
3161 /* XXX: use timeout computed from timers */
3162 if (ret
== EXCP_HLT
)
3169 main_loop_wait(timeout
);
3171 cpu_disable_ticks();
3177 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2005 Fabrice Bellard\n"
3178 "usage: %s [options] [disk_image]\n"
3180 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3182 "Standard options:\n"
3183 "-M machine select emulated machine (-M ? for list)\n"
3184 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3185 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3186 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3187 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3188 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
3189 "-snapshot write to temporary files instead of disk image files\n"
3190 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3191 "-nographic disable graphical output and redirect serial I/Os to console\n"
3193 "-k language use keyboard layout (for example \"fr\" for French)\n"
3196 "-enable-audio enable audio support, and all the sound cars\n"
3197 "-audio-help print list of audio drivers and their options\n"
3198 "-soundhw c1,... enable audio support\n"
3199 " and only specified sound cards (comma separated list)\n"
3200 " use -soundhw ? to get the list of supported cards\n"
3202 "-localtime set the real time clock to local time [default=utc]\n"
3203 "-full-screen start in full screen\n"
3205 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
3207 "-usb enable the USB driver (will be the default soon)\n"
3208 "-usbdevice name add the host or guest USB device 'name'\n"
3209 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3210 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
3213 "Network options:\n"
3214 "-nics n simulate 'n' network cards [default=1]\n"
3215 "-macaddr addr set the mac address of the first interface\n"
3216 "-n script set tap/tun network init script [default=%s]\n"
3217 "-tun-fd fd use this fd as already opened tap/tun interface\n"
3219 "-user-net use user mode network stack [default if no tap/tun script]\n"
3220 "-tftp prefix allow tftp access to files starting with prefix [-user-net]\n"
3222 "-smb dir allow SMB access to files in 'dir' [-user-net]\n"
3224 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
3225 " redirect TCP or UDP connections from host to guest [-user-net]\n"
3227 "-dummy-net use dummy network stack\n"
3229 "Linux boot specific:\n"
3230 "-kernel bzImage use 'bzImage' as kernel image\n"
3231 "-append cmdline use 'cmdline' as kernel command line\n"
3232 "-initrd file use 'file' as initial ram disk\n"
3234 "Debug/Expert options:\n"
3235 "-monitor dev redirect the monitor to char device 'dev'\n"
3236 "-serial dev redirect the serial port to char device 'dev'\n"
3237 "-parallel dev redirect the parallel port to char device 'dev'\n"
3238 "-pidfile file Write PID to 'file'\n"
3239 "-S freeze CPU at startup (use 'c' to start execution)\n"
3240 "-s wait gdb connection to port %d\n"
3241 "-p port change gdb connection port\n"
3242 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
3243 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
3244 " translation (t=none or lba) (usually qemu can guess them)\n"
3245 "-L path set the directory for the BIOS and VGA BIOS\n"
3247 "-no-kqemu disable KQEMU kernel module usage\n"
3249 #ifdef USE_CODE_COPY
3250 "-no-code-copy disable code copy acceleration\n"
3253 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
3254 " (default is CL-GD5446 PCI VGA)\n"
3256 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
3258 "During emulation, the following keys are useful:\n"
3259 "ctrl-alt-f toggle full screen\n"
3260 "ctrl-alt-n switch to virtual console 'n'\n"
3261 "ctrl-alt toggle mouse and keyboard grab\n"
3263 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3265 #ifdef CONFIG_SOFTMMU
3271 DEFAULT_NETWORK_SCRIPT
,
3272 DEFAULT_GDBSTUB_PORT
,
3274 #ifndef CONFIG_SOFTMMU
3276 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
3277 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
3283 #define HAS_ARG 0x0001
3297 QEMU_OPTION_snapshot
,
3299 QEMU_OPTION_nographic
,
3301 QEMU_OPTION_enable_audio
,
3302 QEMU_OPTION_audio_help
,
3303 QEMU_OPTION_soundhw
,
3307 QEMU_OPTION_macaddr
,
3310 QEMU_OPTION_user_net
,
3314 QEMU_OPTION_dummy_net
,
3326 QEMU_OPTION_no_code_copy
,
3328 QEMU_OPTION_localtime
,
3329 QEMU_OPTION_cirrusvga
,
3331 QEMU_OPTION_std_vga
,
3332 QEMU_OPTION_monitor
,
3334 QEMU_OPTION_parallel
,
3336 QEMU_OPTION_full_screen
,
3337 QEMU_OPTION_pidfile
,
3338 QEMU_OPTION_no_kqemu
,
3339 QEMU_OPTION_win2k_hack
,
3341 QEMU_OPTION_usbdevice
,
3344 typedef struct QEMUOption
{
3350 const QEMUOption qemu_options
[] = {
3351 { "h", 0, QEMU_OPTION_h
},
3353 { "M", HAS_ARG
, QEMU_OPTION_M
},
3354 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
3355 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
3356 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
3357 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
3358 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
3359 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
3360 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
3361 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
3362 { "snapshot", 0, QEMU_OPTION_snapshot
},
3363 { "m", HAS_ARG
, QEMU_OPTION_m
},
3364 { "nographic", 0, QEMU_OPTION_nographic
},
3365 { "k", HAS_ARG
, QEMU_OPTION_k
},
3367 { "enable-audio", 0, QEMU_OPTION_enable_audio
},
3368 { "audio-help", 0, QEMU_OPTION_audio_help
},
3369 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
3372 { "nics", HAS_ARG
, QEMU_OPTION_nics
},
3373 { "macaddr", HAS_ARG
, QEMU_OPTION_macaddr
},
3374 { "n", HAS_ARG
, QEMU_OPTION_n
},
3375 { "tun-fd", HAS_ARG
, QEMU_OPTION_tun_fd
},
3377 { "user-net", 0, QEMU_OPTION_user_net
},
3378 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
3380 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
3382 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
3384 { "dummy-net", 0, QEMU_OPTION_dummy_net
},
3386 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
3387 { "append", HAS_ARG
, QEMU_OPTION_append
},
3388 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
3390 { "S", 0, QEMU_OPTION_S
},
3391 { "s", 0, QEMU_OPTION_s
},
3392 { "p", HAS_ARG
, QEMU_OPTION_p
},
3393 { "d", HAS_ARG
, QEMU_OPTION_d
},
3394 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
3395 { "L", HAS_ARG
, QEMU_OPTION_L
},
3396 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
3398 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
3400 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3401 { "g", 1, QEMU_OPTION_g
},
3403 { "localtime", 0, QEMU_OPTION_localtime
},
3404 { "std-vga", 0, QEMU_OPTION_std_vga
},
3405 { "monitor", 1, QEMU_OPTION_monitor
},
3406 { "serial", 1, QEMU_OPTION_serial
},
3407 { "parallel", 1, QEMU_OPTION_parallel
},
3408 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
3409 { "full-screen", 0, QEMU_OPTION_full_screen
},
3410 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
3411 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
3412 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
3414 /* temporary options */
3415 { "usb", 0, QEMU_OPTION_usb
},
3416 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
3420 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3422 /* this stack is only used during signal handling */
3423 #define SIGNAL_STACK_SIZE 32768
3425 static uint8_t *signal_stack
;
3429 /* password input */
3431 static BlockDriverState
*get_bdrv(int index
)
3433 BlockDriverState
*bs
;
3436 bs
= bs_table
[index
];
3437 } else if (index
< 6) {
3438 bs
= fd_table
[index
- 4];
3445 static void read_passwords(void)
3447 BlockDriverState
*bs
;
3451 for(i
= 0; i
< 6; i
++) {
3453 if (bs
&& bdrv_is_encrypted(bs
)) {
3454 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
3455 for(j
= 0; j
< 3; j
++) {
3456 monitor_readline("Password: ",
3457 1, password
, sizeof(password
));
3458 if (bdrv_set_key(bs
, password
) == 0)
3460 term_printf("invalid password\n");
3466 /* XXX: currently we cannot use simultaneously different CPUs */
3467 void register_machines(void)
3469 #if defined(TARGET_I386)
3470 qemu_register_machine(&pc_machine
);
3471 qemu_register_machine(&isapc_machine
);
3472 #elif defined(TARGET_PPC)
3473 qemu_register_machine(&heathrow_machine
);
3474 qemu_register_machine(&core99_machine
);
3475 qemu_register_machine(&prep_machine
);
3476 #elif defined(TARGET_MIPS)
3477 qemu_register_machine(&mips_machine
);
3478 #elif defined(TARGET_SPARC)
3479 #ifdef TARGET_SPARC64
3480 qemu_register_machine(&sun4u_machine
);
3482 qemu_register_machine(&sun4m_machine
);
3488 static void select_soundhw (const char *optarg
)
3490 if (*optarg
== '?') {
3492 printf ("Valid sound card names (comma separated):\n");
3493 printf ("sb16 Creative Sound Blaster 16\n");
3496 printf ("adlib Yamaha YMF262 (OPL3)\n");
3498 printf ("adlib Yamaha YM3812 (OPL2)\n");
3502 printf ("gus Gravis Ultrasound GF1\n");
3504 printf ("es1370 ENSONIQ AudioPCI ES1370\n");
3505 exit (*optarg
!= '?');
3512 { "sb16", &sb16_enabled
},
3514 { "adlib", &adlib_enabled
},
3517 { "gus", &gus_enabled
},
3519 { "es1370", &es1370_enabled
},
3521 size_t tablen
, l
, i
;
3527 tablen
= sizeof (soundhw_tab
) / sizeof (soundhw_tab
[0]);
3530 e
= strchr (p
, ',');
3531 l
= !e
? strlen (p
) : (size_t) (e
- p
);
3532 for (i
= 0; i
< tablen
; ++i
) {
3533 if (!strncmp (soundhw_tab
[i
].name
, p
, l
)) {
3535 *soundhw_tab
[i
].enabledp
= 1;
3542 "Unknown sound card name (too big to show)\n");
3545 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
3550 p
+= l
+ (e
!= NULL
);
3554 goto show_valid_cards
;
3559 #define NET_IF_TUN 0
3560 #define NET_IF_USER 1
3561 #define NET_IF_DUMMY 2
3563 int main(int argc
, char **argv
)
3565 #ifdef CONFIG_GDBSTUB
3566 int use_gdbstub
, gdbstub_port
;
3569 int snapshot
, linux_boot
;
3571 const char *initrd_filename
;
3572 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
3573 const char *kernel_filename
, *kernel_cmdline
;
3574 DisplayState
*ds
= &display_state
;
3575 int cyls
, heads
, secs
, translation
;
3576 int start_emulation
= 1;
3578 int net_if_type
, nb_tun_fds
, tun_fds
[MAX_NICS
];
3580 const char *r
, *optarg
;
3581 CharDriverState
*monitor_hd
;
3582 char monitor_device
[128];
3583 char serial_devices
[MAX_SERIAL_PORTS
][128];
3584 int serial_device_index
;
3585 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
3586 int parallel_device_index
;
3587 const char *loadvm
= NULL
;
3588 QEMUMachine
*machine
;
3589 char usb_devices
[MAX_VM_USB_PORTS
][128];
3590 int usb_devices_index
;
3592 #if !defined(CONFIG_SOFTMMU)
3593 /* we never want that malloc() uses mmap() */
3594 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
3596 register_machines();
3597 machine
= first_machine
;
3598 initrd_filename
= NULL
;
3599 for(i
= 0; i
< MAX_FD
; i
++)
3600 fd_filename
[i
] = NULL
;
3601 for(i
= 0; i
< MAX_DISKS
; i
++)
3602 hd_filename
[i
] = NULL
;
3603 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3604 vga_ram_size
= VGA_RAM_SIZE
;
3605 bios_size
= BIOS_SIZE
;
3606 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
3607 #ifdef CONFIG_GDBSTUB
3609 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
3613 kernel_filename
= NULL
;
3614 kernel_cmdline
= "";
3620 cyls
= heads
= secs
= 0;
3621 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3622 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
3624 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
3625 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
3626 serial_devices
[i
][0] = '\0';
3627 serial_device_index
= 0;
3629 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
3630 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
3631 parallel_devices
[i
][0] = '\0';
3632 parallel_device_index
= 0;
3634 usb_devices_index
= 0;
3639 /* default mac address of the first network interface */
3653 hd_filename
[0] = argv
[optind
++];
3655 const QEMUOption
*popt
;
3658 popt
= qemu_options
;
3661 fprintf(stderr
, "%s: invalid option -- '%s'\n",
3665 if (!strcmp(popt
->name
, r
+ 1))
3669 if (popt
->flags
& HAS_ARG
) {
3670 if (optind
>= argc
) {
3671 fprintf(stderr
, "%s: option '%s' requires an argument\n",
3675 optarg
= argv
[optind
++];
3680 switch(popt
->index
) {
3682 machine
= find_machine(optarg
);
3685 printf("Supported machines are:\n");
3686 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3687 printf("%-10s %s%s\n",
3689 m
== first_machine
? " (default)" : "");
3694 case QEMU_OPTION_initrd
:
3695 initrd_filename
= optarg
;
3697 case QEMU_OPTION_hda
:
3698 case QEMU_OPTION_hdb
:
3699 case QEMU_OPTION_hdc
:
3700 case QEMU_OPTION_hdd
:
3703 hd_index
= popt
->index
- QEMU_OPTION_hda
;
3704 hd_filename
[hd_index
] = optarg
;
3705 if (hd_index
== cdrom_index
)
3709 case QEMU_OPTION_snapshot
:
3712 case QEMU_OPTION_hdachs
:
3716 cyls
= strtol(p
, (char **)&p
, 0);
3717 if (cyls
< 1 || cyls
> 16383)
3722 heads
= strtol(p
, (char **)&p
, 0);
3723 if (heads
< 1 || heads
> 16)
3728 secs
= strtol(p
, (char **)&p
, 0);
3729 if (secs
< 1 || secs
> 63)
3733 if (!strcmp(p
, "none"))
3734 translation
= BIOS_ATA_TRANSLATION_NONE
;
3735 else if (!strcmp(p
, "lba"))
3736 translation
= BIOS_ATA_TRANSLATION_LBA
;
3737 else if (!strcmp(p
, "auto"))
3738 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3741 } else if (*p
!= '\0') {
3743 fprintf(stderr
, "qemu: invalid physical CHS format\n");
3748 case QEMU_OPTION_nographic
:
3749 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
3750 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
3753 case QEMU_OPTION_kernel
:
3754 kernel_filename
= optarg
;
3756 case QEMU_OPTION_append
:
3757 kernel_cmdline
= optarg
;
3759 case QEMU_OPTION_tun_fd
:
3763 net_if_type
= NET_IF_TUN
;
3764 if (nb_tun_fds
< MAX_NICS
) {
3765 fd
= strtol(optarg
, (char **)&p
, 0);
3767 fprintf(stderr
, "qemu: invalid fd for network interface %d\n", nb_tun_fds
);
3770 tun_fds
[nb_tun_fds
++] = fd
;
3774 case QEMU_OPTION_cdrom
:
3775 if (cdrom_index
>= 0) {
3776 hd_filename
[cdrom_index
] = optarg
;
3779 case QEMU_OPTION_boot
:
3780 boot_device
= optarg
[0];
3781 if (boot_device
!= 'a' &&
3784 boot_device
!= 'n' &&
3786 boot_device
!= 'c' && boot_device
!= 'd') {
3787 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
3791 case QEMU_OPTION_fda
:
3792 fd_filename
[0] = optarg
;
3794 case QEMU_OPTION_fdb
:
3795 fd_filename
[1] = optarg
;
3797 case QEMU_OPTION_no_code_copy
:
3798 code_copy_enabled
= 0;
3800 case QEMU_OPTION_nics
:
3801 nb_nics
= atoi(optarg
);
3802 if (nb_nics
< 0 || nb_nics
> MAX_NICS
) {
3803 fprintf(stderr
, "qemu: invalid number of network interfaces\n");
3807 case QEMU_OPTION_macaddr
:
3812 for(i
= 0; i
< 6; i
++) {
3813 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3820 fprintf(stderr
, "qemu: invalid syntax for ethernet address\n");
3829 case QEMU_OPTION_tftp
:
3830 tftp_prefix
= optarg
;
3833 case QEMU_OPTION_smb
:
3834 net_slirp_smb(optarg
);
3837 case QEMU_OPTION_user_net
:
3838 net_if_type
= NET_IF_USER
;
3840 case QEMU_OPTION_redir
:
3841 net_slirp_redir(optarg
);
3844 case QEMU_OPTION_dummy_net
:
3845 net_if_type
= NET_IF_DUMMY
;
3848 case QEMU_OPTION_enable_audio
:
3855 case QEMU_OPTION_audio_help
:
3859 case QEMU_OPTION_soundhw
:
3860 select_soundhw (optarg
);
3867 ram_size
= atoi(optarg
) * 1024 * 1024;
3870 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
3871 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
3872 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
3881 mask
= cpu_str_to_log_mask(optarg
);
3883 printf("Log items (comma separated):\n");
3884 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
3885 printf("%-10s %s\n", item
->name
, item
->help
);
3893 pstrcpy(network_script
, sizeof(network_script
), optarg
);
3895 #ifdef CONFIG_GDBSTUB
3900 gdbstub_port
= atoi(optarg
);
3907 start_emulation
= 0;
3910 keyboard_layout
= optarg
;
3912 case QEMU_OPTION_localtime
:
3915 case QEMU_OPTION_cirrusvga
:
3916 cirrus_vga_enabled
= 1;
3918 case QEMU_OPTION_std_vga
:
3919 cirrus_vga_enabled
= 0;
3926 w
= strtol(p
, (char **)&p
, 10);
3929 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3935 h
= strtol(p
, (char **)&p
, 10);
3940 depth
= strtol(p
, (char **)&p
, 10);
3941 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3942 depth
!= 24 && depth
!= 32)
3944 } else if (*p
== '\0') {
3945 depth
= graphic_depth
;
3952 graphic_depth
= depth
;
3955 case QEMU_OPTION_monitor
:
3956 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
3958 case QEMU_OPTION_serial
:
3959 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
3960 fprintf(stderr
, "qemu: too many serial ports\n");
3963 pstrcpy(serial_devices
[serial_device_index
],
3964 sizeof(serial_devices
[0]), optarg
);
3965 serial_device_index
++;
3967 case QEMU_OPTION_parallel
:
3968 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
3969 fprintf(stderr
, "qemu: too many parallel ports\n");
3972 pstrcpy(parallel_devices
[parallel_device_index
],
3973 sizeof(parallel_devices
[0]), optarg
);
3974 parallel_device_index
++;
3976 case QEMU_OPTION_loadvm
:
3979 case QEMU_OPTION_full_screen
:
3982 case QEMU_OPTION_pidfile
:
3983 create_pidfile(optarg
);
3986 case QEMU_OPTION_win2k_hack
:
3987 win2k_install_hack
= 1;
3991 case QEMU_OPTION_no_kqemu
:
3995 case QEMU_OPTION_usb
:
3998 case QEMU_OPTION_usbdevice
:
4000 if (usb_devices_index
>= MAX_VM_USB_PORTS
) {
4001 fprintf(stderr
, "Too many USB devices\n");
4004 pstrcpy(usb_devices
[usb_devices_index
],
4005 sizeof(usb_devices
[usb_devices_index
]),
4007 usb_devices_index
++;
4013 linux_boot
= (kernel_filename
!= NULL
);
4016 hd_filename
[0] == '\0' &&
4017 (cdrom_index
>= 0 && hd_filename
[cdrom_index
] == '\0') &&
4018 fd_filename
[0] == '\0')
4021 /* boot to cd by default if no hard disk */
4022 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
4023 if (fd_filename
[0] != '\0')
4029 #if !defined(CONFIG_SOFTMMU)
4030 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
4032 static uint8_t stdout_buf
[4096];
4033 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
4036 setvbuf(stdout
, NULL
, _IOLBF
, 0);
4039 /* init host network redirectors */
4040 if (net_if_type
== -1) {
4041 net_if_type
= NET_IF_TUN
;
4042 #if defined(CONFIG_SLIRP)
4043 if (access(network_script
, R_OK
) < 0) {
4044 net_if_type
= NET_IF_USER
;
4049 for(i
= 0; i
< nb_nics
; i
++) {
4050 NetDriverState
*nd
= &nd_table
[i
];
4052 /* init virtual mac address */
4053 nd
->macaddr
[0] = macaddr
[0];
4054 nd
->macaddr
[1] = macaddr
[1];
4055 nd
->macaddr
[2] = macaddr
[2];
4056 nd
->macaddr
[3] = macaddr
[3];
4057 nd
->macaddr
[4] = macaddr
[4];
4058 nd
->macaddr
[5] = macaddr
[5] + i
;
4059 switch(net_if_type
) {
4060 #if defined(CONFIG_SLIRP)
4065 #if !defined(_WIN32)
4067 if (i
< nb_tun_fds
) {
4068 net_fd_init(nd
, tun_fds
[i
]);
4070 if (net_tun_init(nd
) < 0)
4082 /* init the memory */
4083 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
4085 #ifdef CONFIG_SOFTMMU
4086 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
4087 if (!phys_ram_base
) {
4088 fprintf(stderr
, "Could not allocate physical memory\n");
4092 /* as we must map the same page at several addresses, we must use
4097 tmpdir
= getenv("QEMU_TMPDIR");
4100 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
4101 if (mkstemp(phys_ram_file
) < 0) {
4102 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
4106 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
4107 if (phys_ram_fd
< 0) {
4108 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
4112 ftruncate(phys_ram_fd
, phys_ram_size
);
4113 unlink(phys_ram_file
);
4114 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
4116 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
4118 if (phys_ram_base
== MAP_FAILED
) {
4119 fprintf(stderr
, "Could not map physical memory\n");
4125 /* we always create the cdrom drive, even if no disk is there */
4127 if (cdrom_index
>= 0) {
4128 bs_table
[cdrom_index
] = bdrv_new("cdrom");
4129 bdrv_set_type_hint(bs_table
[cdrom_index
], BDRV_TYPE_CDROM
);
4132 /* open the virtual block devices */
4133 for(i
= 0; i
< MAX_DISKS
; i
++) {
4134 if (hd_filename
[i
]) {
4137 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
4138 bs_table
[i
] = bdrv_new(buf
);
4140 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
4141 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
4145 if (i
== 0 && cyls
!= 0) {
4146 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
4147 bdrv_set_translation_hint(bs_table
[i
], translation
);
4152 /* we always create at least one floppy disk */
4153 fd_table
[0] = bdrv_new("fda");
4154 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
4156 for(i
= 0; i
< MAX_FD
; i
++) {
4157 if (fd_filename
[i
]) {
4160 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
4161 fd_table
[i
] = bdrv_new(buf
);
4162 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
4164 if (fd_filename
[i
] != '\0') {
4165 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
4166 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
4174 /* init USB devices */
4176 vm_usb_hub
= usb_hub_init(vm_usb_ports
, MAX_VM_USB_PORTS
);
4177 for(i
= 0; i
< usb_devices_index
; i
++) {
4178 if (usb_device_add(usb_devices
[i
]) < 0) {
4179 fprintf(stderr
, "Warning: could not add USB device %s\n",
4185 /* init CPU state */
4188 cpu_single_env
= env
;
4190 register_savevm("timer", 0, 1, timer_save
, timer_load
, env
);
4191 register_savevm("cpu", 0, 3, cpu_save
, cpu_load
, env
);
4192 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
4193 qemu_register_reset(main_cpu_reset
, global_env
);
4196 cpu_calibrate_ticks();
4200 dumb_display_init(ds
);
4202 #if defined(CONFIG_SDL)
4203 sdl_display_init(ds
, full_screen
);
4204 #elif defined(CONFIG_COCOA)
4205 cocoa_display_init(ds
, full_screen
);
4207 dumb_display_init(ds
);
4211 vga_console
= graphic_console_init(ds
);
4213 monitor_hd
= qemu_chr_open(monitor_device
);
4215 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
4218 monitor_init(monitor_hd
, !nographic
);
4220 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
4221 if (serial_devices
[i
][0] != '\0') {
4222 serial_hds
[i
] = qemu_chr_open(serial_devices
[i
]);
4223 if (!serial_hds
[i
]) {
4224 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
4228 if (!strcmp(serial_devices
[i
], "vc"))
4229 qemu_chr_printf(serial_hds
[i
], "serial%d console\n", i
);
4233 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
4234 if (parallel_devices
[i
][0] != '\0') {
4235 parallel_hds
[i
] = qemu_chr_open(parallel_devices
[i
]);
4236 if (!parallel_hds
[i
]) {
4237 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
4238 parallel_devices
[i
]);
4241 if (!strcmp(parallel_devices
[i
], "vc"))
4242 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\n", i
);
4246 /* setup cpu signal handlers for MMU / self modifying code handling */
4247 #if !defined(CONFIG_SOFTMMU)
4249 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
4252 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
4253 stk
.ss_sp
= signal_stack
;
4254 stk
.ss_size
= SIGNAL_STACK_SIZE
;
4257 if (sigaltstack(&stk
, NULL
) < 0) {
4258 perror("sigaltstack");
4264 struct sigaction act
;
4266 sigfillset(&act
.sa_mask
);
4267 act
.sa_flags
= SA_SIGINFO
;
4268 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
4269 act
.sa_flags
|= SA_ONSTACK
;
4271 act
.sa_sigaction
= host_segv_handler
;
4272 sigaction(SIGSEGV
, &act
, NULL
);
4273 sigaction(SIGBUS
, &act
, NULL
);
4274 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
4275 sigaction(SIGFPE
, &act
, NULL
);
4282 struct sigaction act
;
4283 sigfillset(&act
.sa_mask
);
4285 act
.sa_handler
= SIG_IGN
;
4286 sigaction(SIGPIPE
, &act
, NULL
);
4291 machine
->init(ram_size
, vga_ram_size
, boot_device
,
4292 ds
, fd_filename
, snapshot
,
4293 kernel_filename
, kernel_cmdline
, initrd_filename
);
4295 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
4296 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
4298 #ifdef CONFIG_GDBSTUB
4300 if (gdbserver_start(gdbstub_port
) < 0) {
4301 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
4305 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
4310 qemu_loadvm(loadvm
);
4313 /* XXX: simplify init */
4315 if (start_emulation
) {