4 * Copyright (c) 2003-2004 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>
57 #if defined(CONFIG_SLIRP)
63 #include <sys/timeb.h>
65 #define getopt_long_only getopt_long
66 #define memalign(align, size) malloc(size)
73 #endif /* CONFIG_SDL */
81 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
83 //#define DEBUG_UNUSED_IOPORT
84 //#define DEBUG_IOPORT
86 #if !defined(CONFIG_SOFTMMU)
87 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
89 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
93 #define DEFAULT_RAM_SIZE 144
95 #define DEFAULT_RAM_SIZE 128
98 #define GUI_REFRESH_INTERVAL 30
100 /* XXX: use a two level table to limit memory usage */
101 #define MAX_IOPORTS 65536
103 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
104 char phys_ram_file
[1024];
105 CPUState
*global_env
;
106 CPUState
*cpu_single_env
;
107 void *ioport_opaque
[MAX_IOPORTS
];
108 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
109 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
110 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
113 static DisplayState display_state
;
115 int64_t ticks_per_sec
;
116 int boot_device
= 'c';
118 static char network_script
[1024];
119 int pit_min_timer_count
= 0;
121 NetDriverState nd_table
[MAX_NICS
];
122 QEMUTimer
*gui_timer
;
124 int audio_enabled
= 0;
126 int prep_enabled
= 0;
128 int cirrus_vga_enabled
= 1;
129 int graphic_width
= 800;
130 int graphic_height
= 600;
131 int graphic_depth
= 15;
132 TextConsole
*vga_console
;
133 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
135 /***********************************************************/
136 /* x86 ISA bus support */
138 target_phys_addr_t isa_mem_base
= 0;
140 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
142 #ifdef DEBUG_UNUSED_IOPORT
143 fprintf(stderr
, "inb: port=0x%04x\n", address
);
148 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
150 #ifdef DEBUG_UNUSED_IOPORT
151 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
155 /* default is to make two byte accesses */
156 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
159 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
160 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
161 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
165 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
167 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
168 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
169 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
172 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
174 #ifdef DEBUG_UNUSED_IOPORT
175 fprintf(stderr
, "inl: port=0x%04x\n", address
);
180 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
182 #ifdef DEBUG_UNUSED_IOPORT
183 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
187 void init_ioports(void)
191 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
192 ioport_read_table
[0][i
] = default_ioport_readb
;
193 ioport_write_table
[0][i
] = default_ioport_writeb
;
194 ioport_read_table
[1][i
] = default_ioport_readw
;
195 ioport_write_table
[1][i
] = default_ioport_writew
;
196 ioport_read_table
[2][i
] = default_ioport_readl
;
197 ioport_write_table
[2][i
] = default_ioport_writel
;
201 /* size is the word size in byte */
202 int register_ioport_read(int start
, int length
, int size
,
203 IOPortReadFunc
*func
, void *opaque
)
209 } else if (size
== 2) {
211 } else if (size
== 4) {
214 hw_error("register_ioport_read: invalid size");
217 for(i
= start
; i
< start
+ length
; i
+= size
) {
218 ioport_read_table
[bsize
][i
] = func
;
219 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
220 hw_error("register_ioport_read: invalid opaque");
221 ioport_opaque
[i
] = opaque
;
226 /* size is the word size in byte */
227 int register_ioport_write(int start
, int length
, int size
,
228 IOPortWriteFunc
*func
, void *opaque
)
234 } else if (size
== 2) {
236 } else if (size
== 4) {
239 hw_error("register_ioport_write: invalid size");
242 for(i
= start
; i
< start
+ length
; i
+= size
) {
243 ioport_write_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 void isa_unassign_ioport(int start
, int length
)
255 for(i
= start
; i
< start
+ length
; i
++) {
256 ioport_read_table
[0][i
] = default_ioport_readb
;
257 ioport_read_table
[1][i
] = default_ioport_readw
;
258 ioport_read_table
[2][i
] = default_ioport_readl
;
260 ioport_write_table
[0][i
] = default_ioport_writeb
;
261 ioport_write_table
[1][i
] = default_ioport_writew
;
262 ioport_write_table
[2][i
] = default_ioport_writel
;
266 void pstrcpy(char *buf
, int buf_size
, const char *str
)
276 if (c
== 0 || q
>= buf
+ buf_size
- 1)
283 /* strcat and truncate. */
284 char *pstrcat(char *buf
, int buf_size
, const char *s
)
289 pstrcpy(buf
+ len
, buf_size
- len
, s
);
293 int strstart(const char *str
, const char *val
, const char **ptr
)
309 /* return the size or -1 if error */
310 int get_image_size(const char *filename
)
313 fd
= open(filename
, O_RDONLY
| O_BINARY
);
316 size
= lseek(fd
, 0, SEEK_END
);
321 /* return the size or -1 if error */
322 int load_image(const char *filename
, uint8_t *addr
)
325 fd
= open(filename
, O_RDONLY
| O_BINARY
);
328 size
= lseek(fd
, 0, SEEK_END
);
329 lseek(fd
, 0, SEEK_SET
);
330 if (read(fd
, addr
, size
) != size
) {
338 void cpu_outb(CPUState
*env
, int addr
, int val
)
341 if (loglevel
& CPU_LOG_IOPORT
)
342 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
344 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
347 void cpu_outw(CPUState
*env
, int addr
, int val
)
350 if (loglevel
& CPU_LOG_IOPORT
)
351 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
353 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
356 void cpu_outl(CPUState
*env
, int addr
, int val
)
359 if (loglevel
& CPU_LOG_IOPORT
)
360 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
362 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
365 int cpu_inb(CPUState
*env
, int addr
)
368 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
370 if (loglevel
& CPU_LOG_IOPORT
)
371 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
376 int cpu_inw(CPUState
*env
, int addr
)
379 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
381 if (loglevel
& CPU_LOG_IOPORT
)
382 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
387 int cpu_inl(CPUState
*env
, int addr
)
390 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
392 if (loglevel
& CPU_LOG_IOPORT
)
393 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
398 /***********************************************************/
399 void hw_error(const char *fmt
, ...)
404 fprintf(stderr
, "qemu: hardware error: ");
405 vfprintf(stderr
, fmt
, ap
);
406 fprintf(stderr
, "\n");
408 cpu_x86_dump_state(global_env
, stderr
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
410 cpu_dump_state(global_env
, stderr
, 0);
416 /***********************************************************/
419 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
420 static void *qemu_put_kbd_event_opaque
;
421 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
422 static void *qemu_put_mouse_event_opaque
;
424 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
426 qemu_put_kbd_event_opaque
= opaque
;
427 qemu_put_kbd_event
= func
;
430 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
)
432 qemu_put_mouse_event_opaque
= opaque
;
433 qemu_put_mouse_event
= func
;
436 void kbd_put_keycode(int keycode
)
438 if (qemu_put_kbd_event
) {
439 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
443 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
445 if (qemu_put_mouse_event
) {
446 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
447 dx
, dy
, dz
, buttons_state
);
451 /***********************************************************/
454 #if defined(__powerpc__)
456 static inline uint32_t get_tbl(void)
459 asm volatile("mftb %0" : "=r" (tbl
));
463 static inline uint32_t get_tbu(void)
466 asm volatile("mftbu %0" : "=r" (tbl
));
470 int64_t cpu_get_real_ticks(void)
473 /* NOTE: we test if wrapping has occurred */
479 return ((int64_t)h
<< 32) | l
;
482 #elif defined(__i386__)
484 int64_t cpu_get_real_ticks(void)
487 asm volatile ("rdtsc" : "=A" (val
));
491 #elif defined(__x86_64__)
493 int64_t cpu_get_real_ticks(void)
497 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
505 #error unsupported CPU
508 static int64_t cpu_ticks_offset
;
509 static int cpu_ticks_enabled
;
511 static inline int64_t cpu_get_ticks(void)
513 if (!cpu_ticks_enabled
) {
514 return cpu_ticks_offset
;
516 return cpu_get_real_ticks() + cpu_ticks_offset
;
520 /* enable cpu_get_ticks() */
521 void cpu_enable_ticks(void)
523 if (!cpu_ticks_enabled
) {
524 cpu_ticks_offset
-= cpu_get_real_ticks();
525 cpu_ticks_enabled
= 1;
529 /* disable cpu_get_ticks() : the clock is stopped. You must not call
530 cpu_get_ticks() after that. */
531 void cpu_disable_ticks(void)
533 if (cpu_ticks_enabled
) {
534 cpu_ticks_offset
= cpu_get_ticks();
535 cpu_ticks_enabled
= 0;
539 static int64_t get_clock(void)
544 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
547 gettimeofday(&tv
, NULL
);
548 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
552 void cpu_calibrate_ticks(void)
557 ticks
= cpu_get_real_ticks();
563 usec
= get_clock() - usec
;
564 ticks
= cpu_get_real_ticks() - ticks
;
565 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
568 /* compute with 96 bit intermediate result: (a*b)/c */
569 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
574 #ifdef WORDS_BIGENDIAN
584 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
585 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
588 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
592 #define QEMU_TIMER_REALTIME 0
593 #define QEMU_TIMER_VIRTUAL 1
597 /* XXX: add frequency */
605 struct QEMUTimer
*next
;
611 static QEMUTimer
*active_timers
[2];
613 static MMRESULT timerID
;
615 /* frequency of the times() clock tick */
616 static int timer_freq
;
619 QEMUClock
*qemu_new_clock(int type
)
622 clock
= qemu_mallocz(sizeof(QEMUClock
));
629 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
633 ts
= qemu_mallocz(sizeof(QEMUTimer
));
640 void qemu_free_timer(QEMUTimer
*ts
)
645 /* stop a timer, but do not dealloc it */
646 void qemu_del_timer(QEMUTimer
*ts
)
650 /* NOTE: this code must be signal safe because
651 qemu_timer_expired() can be called from a signal. */
652 pt
= &active_timers
[ts
->clock
->type
];
665 /* modify the current timer so that it will be fired when current_time
666 >= expire_time. The corresponding callback will be called. */
667 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
673 /* add the timer in the sorted list */
674 /* NOTE: this code must be signal safe because
675 qemu_timer_expired() can be called from a signal. */
676 pt
= &active_timers
[ts
->clock
->type
];
681 if (t
->expire_time
> expire_time
)
685 ts
->expire_time
= expire_time
;
690 int qemu_timer_pending(QEMUTimer
*ts
)
693 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
700 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
704 return (timer_head
->expire_time
<= current_time
);
707 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
713 if (!ts
|| ts
->expire_time
> current_time
)
715 /* remove timer from the list before calling the callback */
716 *ptimer_head
= ts
->next
;
719 /* run the callback (the timer list can be modified) */
724 int64_t qemu_get_clock(QEMUClock
*clock
)
726 switch(clock
->type
) {
727 case QEMU_TIMER_REALTIME
:
729 return GetTickCount();
734 /* Note that using gettimeofday() is not a good solution
735 for timers because its value change when the date is
737 if (timer_freq
== 100) {
738 return times(&tp
) * 10;
740 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
745 case QEMU_TIMER_VIRTUAL
:
746 return cpu_get_ticks();
751 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
753 uint64_t expire_time
;
755 if (qemu_timer_pending(ts
)) {
756 expire_time
= ts
->expire_time
;
760 qemu_put_be64(f
, expire_time
);
763 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
765 uint64_t expire_time
;
767 expire_time
= qemu_get_be64(f
);
768 if (expire_time
!= -1) {
769 qemu_mod_timer(ts
, expire_time
);
775 static void timer_save(QEMUFile
*f
, void *opaque
)
777 if (cpu_ticks_enabled
) {
778 hw_error("cannot save state if virtual timers are running");
780 qemu_put_be64s(f
, &cpu_ticks_offset
);
781 qemu_put_be64s(f
, &ticks_per_sec
);
784 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
788 if (cpu_ticks_enabled
) {
791 qemu_get_be64s(f
, &cpu_ticks_offset
);
792 qemu_get_be64s(f
, &ticks_per_sec
);
797 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
798 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
800 static void host_alarm_handler(int host_signum
)
804 #define DISP_FREQ 1000
806 static int64_t delta_min
= INT64_MAX
;
807 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
809 ti
= qemu_get_clock(vm_clock
);
810 if (last_clock
!= 0) {
811 delta
= ti
- last_clock
;
812 if (delta
< delta_min
)
814 if (delta
> delta_max
)
817 if (++count
== DISP_FREQ
) {
818 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
819 muldiv64(delta_min
, 1000000, ticks_per_sec
),
820 muldiv64(delta_max
, 1000000, ticks_per_sec
),
821 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
822 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
824 delta_min
= INT64_MAX
;
832 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
833 qemu_get_clock(vm_clock
)) ||
834 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
835 qemu_get_clock(rt_clock
))) {
836 /* stop the cpu because a timer occured */
837 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
843 #if defined(__linux__)
845 #define RTC_FREQ 1024
849 static int start_rtc_timer(void)
851 rtc_fd
= open("/dev/rtc", O_RDONLY
);
854 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
855 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
856 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
857 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
860 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
865 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
871 static int start_rtc_timer(void)
876 #endif /* !defined(__linux__) */
878 #endif /* !defined(_WIN32) */
880 static void init_timers(void)
882 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
883 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
888 timerID
= timeSetEvent(10, // interval (ms)
890 host_alarm_handler
, // function
891 (DWORD
)&count
, // user parameter
892 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
894 perror("failed timer alarm");
898 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
901 struct sigaction act
;
902 struct itimerval itv
;
904 /* get times() syscall frequency */
905 timer_freq
= sysconf(_SC_CLK_TCK
);
908 sigfillset(&act
.sa_mask
);
910 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
911 act
.sa_flags
|= SA_ONSTACK
;
913 act
.sa_handler
= host_alarm_handler
;
914 sigaction(SIGALRM
, &act
, NULL
);
916 itv
.it_interval
.tv_sec
= 0;
917 itv
.it_interval
.tv_usec
= 1000;
918 itv
.it_value
.tv_sec
= 0;
919 itv
.it_value
.tv_usec
= 10 * 1000;
920 setitimer(ITIMER_REAL
, &itv
, NULL
);
921 /* we probe the tick duration of the kernel to inform the user if
922 the emulated kernel requested a too high timer frequency */
923 getitimer(ITIMER_REAL
, &itv
);
925 #if defined(__linux__)
926 if (itv
.it_interval
.tv_usec
> 1000) {
927 /* try to use /dev/rtc to have a faster timer */
928 if (start_rtc_timer() < 0)
931 itv
.it_interval
.tv_sec
= 0;
932 itv
.it_interval
.tv_usec
= 0;
933 itv
.it_value
.tv_sec
= 0;
934 itv
.it_value
.tv_usec
= 0;
935 setitimer(ITIMER_REAL
, &itv
, NULL
);
938 sigaction(SIGIO
, &act
, NULL
);
939 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
940 fcntl(rtc_fd
, F_SETOWN
, getpid());
942 #endif /* defined(__linux__) */
945 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
952 void quit_timers(void)
955 timeKillEvent(timerID
);
959 /***********************************************************/
960 /* character device */
962 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
964 return s
->chr_write(s
, buf
, len
);
967 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
972 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
973 qemu_chr_write(s
, buf
, strlen(buf
));
977 void qemu_chr_send_event(CharDriverState
*s
, int event
)
979 if (s
->chr_send_event
)
980 s
->chr_send_event(s
, event
);
983 void qemu_chr_add_read_handler(CharDriverState
*s
,
984 IOCanRWHandler
*fd_can_read
,
985 IOReadHandler
*fd_read
, void *opaque
)
987 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
990 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
992 s
->chr_event
= chr_event
;
995 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1000 static void null_chr_add_read_handler(CharDriverState
*chr
,
1001 IOCanRWHandler
*fd_can_read
,
1002 IOReadHandler
*fd_read
, void *opaque
)
1006 CharDriverState
*qemu_chr_open_null(void)
1008 CharDriverState
*chr
;
1010 chr
= qemu_mallocz(sizeof(CharDriverState
));
1013 chr
->chr_write
= null_chr_write
;
1014 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1022 /* for nographic stdio only */
1023 IOCanRWHandler
*fd_can_read
;
1024 IOReadHandler
*fd_read
;
1028 #define STDIO_MAX_CLIENTS 2
1030 static int stdio_nb_clients
;
1031 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1033 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1039 ret
= write(fd
, buf
, len
);
1041 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1043 } else if (ret
== 0) {
1053 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1055 FDCharDriver
*s
= chr
->opaque
;
1056 return unix_write(s
->fd_out
, buf
, len
);
1059 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1060 IOCanRWHandler
*fd_can_read
,
1061 IOReadHandler
*fd_read
, void *opaque
)
1063 FDCharDriver
*s
= chr
->opaque
;
1065 if (nographic
&& s
->fd_in
== 0) {
1066 s
->fd_can_read
= fd_can_read
;
1067 s
->fd_read
= fd_read
;
1068 s
->fd_opaque
= opaque
;
1070 qemu_add_fd_read_handler(s
->fd_in
, fd_can_read
, fd_read
, opaque
);
1074 /* open a character device to a unix fd */
1075 CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1077 CharDriverState
*chr
;
1080 chr
= qemu_mallocz(sizeof(CharDriverState
));
1083 s
= qemu_mallocz(sizeof(FDCharDriver
));
1091 chr
->chr_write
= fd_chr_write
;
1092 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1096 /* for STDIO, we handle the case where several clients use it
1099 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1101 static int term_got_escape
, client_index
;
1103 void term_print_help(void)
1106 "C-a h print this help\n"
1107 "C-a x exit emulator\n"
1108 "C-a s save disk data back to file (if -snapshot)\n"
1109 "C-a b send break (magic sysrq)\n"
1110 "C-a c switch between console and monitor\n"
1111 "C-a C-a send C-a\n"
1115 /* called when a char is received */
1116 static void stdio_received_byte(int ch
)
1118 if (term_got_escape
) {
1119 term_got_escape
= 0;
1130 for (i
= 0; i
< MAX_DISKS
; i
++) {
1132 bdrv_commit(bs_table
[i
]);
1137 if (client_index
< stdio_nb_clients
) {
1138 CharDriverState
*chr
;
1141 chr
= stdio_clients
[client_index
];
1143 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1148 if (client_index
>= stdio_nb_clients
)
1150 if (client_index
== 0) {
1151 /* send a new line in the monitor to get the prompt */
1159 } else if (ch
== TERM_ESCAPE
) {
1160 term_got_escape
= 1;
1163 if (client_index
< stdio_nb_clients
) {
1165 CharDriverState
*chr
;
1168 chr
= stdio_clients
[client_index
];
1171 /* XXX: should queue the char if the device is not
1173 if (s
->fd_can_read(s
->fd_opaque
) > 0)
1174 s
->fd_read(s
->fd_opaque
, buf
, 1);
1179 static int stdio_can_read(void *opaque
)
1181 /* XXX: not strictly correct */
1185 static void stdio_read(void *opaque
, const uint8_t *buf
, int size
)
1188 for(i
= 0; i
< size
; i
++)
1189 stdio_received_byte(buf
[i
]);
1192 /* init terminal so that we can grab keys */
1193 static struct termios oldtty
;
1194 static int old_fd0_flags
;
1196 static void term_exit(void)
1198 tcsetattr (0, TCSANOW
, &oldtty
);
1199 fcntl(0, F_SETFL
, old_fd0_flags
);
1202 static void term_init(void)
1206 tcgetattr (0, &tty
);
1208 old_fd0_flags
= fcntl(0, F_GETFL
);
1210 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1211 |INLCR
|IGNCR
|ICRNL
|IXON
);
1212 tty
.c_oflag
|= OPOST
;
1213 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1214 /* if graphical mode, we allow Ctrl-C handling */
1216 tty
.c_lflag
&= ~ISIG
;
1217 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1220 tty
.c_cc
[VTIME
] = 0;
1222 tcsetattr (0, TCSANOW
, &tty
);
1226 fcntl(0, F_SETFL
, O_NONBLOCK
);
1229 CharDriverState
*qemu_chr_open_stdio(void)
1231 CharDriverState
*chr
;
1234 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1236 chr
= qemu_chr_open_fd(0, 1);
1237 if (stdio_nb_clients
== 0)
1238 qemu_add_fd_read_handler(0, stdio_can_read
, stdio_read
, NULL
);
1239 client_index
= stdio_nb_clients
;
1241 if (stdio_nb_clients
!= 0)
1243 chr
= qemu_chr_open_fd(0, 1);
1245 stdio_clients
[stdio_nb_clients
++] = chr
;
1246 if (stdio_nb_clients
== 1) {
1247 /* set the terminal in raw mode */
1253 #if defined(__linux__)
1254 CharDriverState
*qemu_chr_open_pty(void)
1256 char slave_name
[1024];
1257 int master_fd
, slave_fd
;
1259 /* Not satisfying */
1260 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1263 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1264 return qemu_chr_open_fd(master_fd
, master_fd
);
1267 CharDriverState
*qemu_chr_open_pty(void)
1273 #endif /* !defined(_WIN32) */
1275 CharDriverState
*qemu_chr_open(const char *filename
)
1277 if (!strcmp(filename
, "vc")) {
1278 return text_console_init(&display_state
);
1279 } else if (!strcmp(filename
, "null")) {
1280 return qemu_chr_open_null();
1283 if (!strcmp(filename
, "pty")) {
1284 return qemu_chr_open_pty();
1285 } else if (!strcmp(filename
, "stdio")) {
1286 return qemu_chr_open_stdio();
1294 /***********************************************************/
1295 /* Linux network device redirectors */
1297 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
1301 for(i
=0;i
<size
;i
+=16) {
1305 fprintf(f
, "%08x ", i
);
1308 fprintf(f
, " %02x", buf
[i
+j
]);
1313 for(j
=0;j
<len
;j
++) {
1315 if (c
< ' ' || c
> '~')
1317 fprintf(f
, "%c", c
);
1323 void qemu_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1325 nd
->send_packet(nd
, buf
, size
);
1328 void qemu_add_read_packet(NetDriverState
*nd
, IOCanRWHandler
*fd_can_read
,
1329 IOReadHandler
*fd_read
, void *opaque
)
1331 nd
->add_read_packet(nd
, fd_can_read
, fd_read
, opaque
);
1334 /* dummy network adapter */
1336 static void dummy_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1340 static void dummy_add_read_packet(NetDriverState
*nd
,
1341 IOCanRWHandler
*fd_can_read
,
1342 IOReadHandler
*fd_read
, void *opaque
)
1346 static int net_dummy_init(NetDriverState
*nd
)
1348 nd
->send_packet
= dummy_send_packet
;
1349 nd
->add_read_packet
= dummy_add_read_packet
;
1350 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "dummy");
1354 #if defined(CONFIG_SLIRP)
1356 /* slirp network adapter */
1358 static void *slirp_fd_opaque
;
1359 static IOCanRWHandler
*slirp_fd_can_read
;
1360 static IOReadHandler
*slirp_fd_read
;
1361 static int slirp_inited
;
1363 int slirp_can_output(void)
1365 return slirp_fd_can_read(slirp_fd_opaque
);
1368 void slirp_output(const uint8_t *pkt
, int pkt_len
)
1371 printf("output:\n");
1372 hex_dump(stdout
, pkt
, pkt_len
);
1374 slirp_fd_read(slirp_fd_opaque
, pkt
, pkt_len
);
1377 static void slirp_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1381 hex_dump(stdout
, buf
, size
);
1383 slirp_input(buf
, size
);
1386 static void slirp_add_read_packet(NetDriverState
*nd
,
1387 IOCanRWHandler
*fd_can_read
,
1388 IOReadHandler
*fd_read
, void *opaque
)
1390 slirp_fd_opaque
= opaque
;
1391 slirp_fd_can_read
= fd_can_read
;
1392 slirp_fd_read
= fd_read
;
1395 static int net_slirp_init(NetDriverState
*nd
)
1397 if (!slirp_inited
) {
1401 nd
->send_packet
= slirp_send_packet
;
1402 nd
->add_read_packet
= slirp_add_read_packet
;
1403 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "slirp");
1407 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
1412 p1
= strchr(p
, sep
);
1418 if (len
> buf_size
- 1)
1420 memcpy(buf
, p
, len
);
1427 static void net_slirp_redir(const char *redir_str
)
1432 struct in_addr guest_addr
;
1433 int host_port
, guest_port
;
1435 if (!slirp_inited
) {
1441 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
1443 if (!strcmp(buf
, "tcp")) {
1445 } else if (!strcmp(buf
, "udp")) {
1451 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
1453 host_port
= strtol(buf
, &r
, 0);
1457 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
1459 if (buf
[0] == '\0') {
1460 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
1462 if (!inet_aton(buf
, &guest_addr
))
1465 guest_port
= strtol(p
, &r
, 0);
1469 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
1470 fprintf(stderr
, "qemu: could not set up redirection\n");
1475 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
1483 static void smb_exit(void)
1487 char filename
[1024];
1489 /* erase all the files in the directory */
1490 d
= opendir(smb_dir
);
1495 if (strcmp(de
->d_name
, ".") != 0 &&
1496 strcmp(de
->d_name
, "..") != 0) {
1497 snprintf(filename
, sizeof(filename
), "%s/%s",
1498 smb_dir
, de
->d_name
);
1506 /* automatic user mode samba server configuration */
1507 void net_slirp_smb(const char *exported_dir
)
1509 char smb_conf
[1024];
1510 char smb_cmdline
[1024];
1513 if (!slirp_inited
) {
1518 /* XXX: better tmp dir construction */
1519 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
1520 if (mkdir(smb_dir
, 0700) < 0) {
1521 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
1524 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
1526 f
= fopen(smb_conf
, "w");
1528 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
1533 "pid directory=%s\n"
1534 "lock directory=%s\n"
1535 "log file=%s/log.smbd\n"
1536 "smb passwd file=%s/smbpasswd\n"
1537 "security = share\n"
1551 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "/usr/sbin/smbd -s %s",
1554 slirp_add_exec(0, smb_cmdline
, 4, 139);
1557 #endif /* !defined(_WIN32) */
1559 #endif /* CONFIG_SLIRP */
1561 #if !defined(_WIN32)
1563 static int tun_open(char *ifname
, int ifname_size
)
1569 fd
= open("/dev/tap", O_RDWR
);
1571 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
1576 dev
= devname(s
.st_rdev
, S_IFCHR
);
1577 pstrcpy(ifname
, ifname_size
, dev
);
1579 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1583 static int tun_open(char *ifname
, int ifname_size
)
1588 fd
= open("/dev/net/tun", O_RDWR
);
1590 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1593 memset(&ifr
, 0, sizeof(ifr
));
1594 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1595 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
1596 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1598 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1602 printf("Connected to host network interface: %s\n", ifr
.ifr_name
);
1603 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1604 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1609 static void tun_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1611 write(nd
->fd
, buf
, size
);
1614 static void tun_add_read_packet(NetDriverState
*nd
,
1615 IOCanRWHandler
*fd_can_read
,
1616 IOReadHandler
*fd_read
, void *opaque
)
1618 qemu_add_fd_read_handler(nd
->fd
, fd_can_read
, fd_read
, opaque
);
1621 static int net_tun_init(NetDriverState
*nd
)
1627 nd
->fd
= tun_open(nd
->ifname
, sizeof(nd
->ifname
));
1631 /* try to launch network init script */
1636 *parg
++ = network_script
;
1637 *parg
++ = nd
->ifname
;
1639 execv(network_script
, args
);
1642 while (waitpid(pid
, &status
, 0) != pid
);
1643 if (!WIFEXITED(status
) ||
1644 WEXITSTATUS(status
) != 0) {
1645 fprintf(stderr
, "%s: could not launch network script\n",
1649 nd
->send_packet
= tun_send_packet
;
1650 nd
->add_read_packet
= tun_add_read_packet
;
1654 static int net_fd_init(NetDriverState
*nd
, int fd
)
1657 nd
->send_packet
= tun_send_packet
;
1658 nd
->add_read_packet
= tun_add_read_packet
;
1659 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "tunfd");
1663 #endif /* !_WIN32 */
1665 /***********************************************************/
1668 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
1672 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
1676 static void dumb_refresh(DisplayState
*ds
)
1678 vga_update_display();
1681 void dumb_display_init(DisplayState
*ds
)
1686 ds
->dpy_update
= dumb_update
;
1687 ds
->dpy_resize
= dumb_resize
;
1688 ds
->dpy_refresh
= dumb_refresh
;
1691 #if !defined(CONFIG_SOFTMMU)
1692 /***********************************************************/
1693 /* cpu signal handler */
1694 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
1697 if (cpu_signal_handler(host_signum
, info
, puc
))
1699 if (stdio_nb_clients
> 0)
1705 /***********************************************************/
1708 #define MAX_IO_HANDLERS 64
1710 typedef struct IOHandlerRecord
{
1712 IOCanRWHandler
*fd_can_read
;
1713 IOReadHandler
*fd_read
;
1715 /* temporary data */
1718 struct IOHandlerRecord
*next
;
1721 static IOHandlerRecord
*first_io_handler
;
1723 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
1724 IOReadHandler
*fd_read
, void *opaque
)
1726 IOHandlerRecord
*ioh
;
1728 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1732 ioh
->fd_can_read
= fd_can_read
;
1733 ioh
->fd_read
= fd_read
;
1734 ioh
->opaque
= opaque
;
1735 ioh
->next
= first_io_handler
;
1736 first_io_handler
= ioh
;
1740 void qemu_del_fd_read_handler(int fd
)
1742 IOHandlerRecord
**pioh
, *ioh
;
1744 pioh
= &first_io_handler
;
1749 if (ioh
->fd
== fd
) {
1757 /***********************************************************/
1758 /* savevm/loadvm support */
1760 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
1762 fwrite(buf
, 1, size
, f
);
1765 void qemu_put_byte(QEMUFile
*f
, int v
)
1770 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
1772 qemu_put_byte(f
, v
>> 8);
1773 qemu_put_byte(f
, v
);
1776 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
1778 qemu_put_byte(f
, v
>> 24);
1779 qemu_put_byte(f
, v
>> 16);
1780 qemu_put_byte(f
, v
>> 8);
1781 qemu_put_byte(f
, v
);
1784 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
1786 qemu_put_be32(f
, v
>> 32);
1787 qemu_put_be32(f
, v
);
1790 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
1792 return fread(buf
, 1, size
, f
);
1795 int qemu_get_byte(QEMUFile
*f
)
1805 unsigned int qemu_get_be16(QEMUFile
*f
)
1808 v
= qemu_get_byte(f
) << 8;
1809 v
|= qemu_get_byte(f
);
1813 unsigned int qemu_get_be32(QEMUFile
*f
)
1816 v
= qemu_get_byte(f
) << 24;
1817 v
|= qemu_get_byte(f
) << 16;
1818 v
|= qemu_get_byte(f
) << 8;
1819 v
|= qemu_get_byte(f
);
1823 uint64_t qemu_get_be64(QEMUFile
*f
)
1826 v
= (uint64_t)qemu_get_be32(f
) << 32;
1827 v
|= qemu_get_be32(f
);
1831 int64_t qemu_ftell(QEMUFile
*f
)
1836 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
1838 if (fseek(f
, pos
, whence
) < 0)
1843 typedef struct SaveStateEntry
{
1847 SaveStateHandler
*save_state
;
1848 LoadStateHandler
*load_state
;
1850 struct SaveStateEntry
*next
;
1853 static SaveStateEntry
*first_se
;
1855 int register_savevm(const char *idstr
,
1858 SaveStateHandler
*save_state
,
1859 LoadStateHandler
*load_state
,
1862 SaveStateEntry
*se
, **pse
;
1864 se
= qemu_malloc(sizeof(SaveStateEntry
));
1867 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1868 se
->instance_id
= instance_id
;
1869 se
->version_id
= version_id
;
1870 se
->save_state
= save_state
;
1871 se
->load_state
= load_state
;
1872 se
->opaque
= opaque
;
1875 /* add at the end of list */
1877 while (*pse
!= NULL
)
1878 pse
= &(*pse
)->next
;
1883 #define QEMU_VM_FILE_MAGIC 0x5145564d
1884 #define QEMU_VM_FILE_VERSION 0x00000001
1886 int qemu_savevm(const char *filename
)
1890 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
1892 saved_vm_running
= vm_running
;
1895 f
= fopen(filename
, "wb");
1901 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1902 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1904 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1906 len
= strlen(se
->idstr
);
1907 qemu_put_byte(f
, len
);
1908 qemu_put_buffer(f
, se
->idstr
, len
);
1910 qemu_put_be32(f
, se
->instance_id
);
1911 qemu_put_be32(f
, se
->version_id
);
1913 /* record size: filled later */
1915 qemu_put_be32(f
, 0);
1917 se
->save_state(f
, se
->opaque
);
1919 /* fill record size */
1921 len
= ftell(f
) - len_pos
- 4;
1922 fseek(f
, len_pos
, SEEK_SET
);
1923 qemu_put_be32(f
, len
);
1924 fseek(f
, cur_pos
, SEEK_SET
);
1930 if (saved_vm_running
)
1935 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1939 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1940 if (!strcmp(se
->idstr
, idstr
) &&
1941 instance_id
== se
->instance_id
)
1947 int qemu_loadvm(const char *filename
)
1951 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
1952 int saved_vm_running
;
1956 saved_vm_running
= vm_running
;
1959 f
= fopen(filename
, "rb");
1965 v
= qemu_get_be32(f
);
1966 if (v
!= QEMU_VM_FILE_MAGIC
)
1968 v
= qemu_get_be32(f
);
1969 if (v
!= QEMU_VM_FILE_VERSION
) {
1976 #if defined (DO_TB_FLUSH)
1977 tb_flush(global_env
);
1979 len
= qemu_get_byte(f
);
1982 qemu_get_buffer(f
, idstr
, len
);
1984 instance_id
= qemu_get_be32(f
);
1985 version_id
= qemu_get_be32(f
);
1986 record_len
= qemu_get_be32(f
);
1988 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1989 idstr
, instance_id
, version_id
, record_len
);
1992 se
= find_se(idstr
, instance_id
);
1994 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1995 instance_id
, idstr
);
1997 ret
= se
->load_state(f
, se
->opaque
, version_id
);
1999 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2000 instance_id
, idstr
);
2003 /* always seek to exact end of record */
2004 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
2009 if (saved_vm_running
)
2014 /***********************************************************/
2015 /* cpu save/restore */
2017 #if defined(TARGET_I386)
2019 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
2021 qemu_put_be32(f
, dt
->selector
);
2022 qemu_put_be32(f
, (uint32_t)dt
->base
);
2023 qemu_put_be32(f
, dt
->limit
);
2024 qemu_put_be32(f
, dt
->flags
);
2027 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
2029 dt
->selector
= qemu_get_be32(f
);
2030 dt
->base
= (uint8_t *)qemu_get_be32(f
);
2031 dt
->limit
= qemu_get_be32(f
);
2032 dt
->flags
= qemu_get_be32(f
);
2035 void cpu_save(QEMUFile
*f
, void *opaque
)
2037 CPUState
*env
= opaque
;
2038 uint16_t fptag
, fpus
, fpuc
;
2042 for(i
= 0; i
< 8; i
++)
2043 qemu_put_be32s(f
, &env
->regs
[i
]);
2044 qemu_put_be32s(f
, &env
->eip
);
2045 qemu_put_be32s(f
, &env
->eflags
);
2046 qemu_put_be32s(f
, &env
->eflags
);
2047 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
2048 qemu_put_be32s(f
, &hflags
);
2052 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
2054 for (i
=7; i
>=0; i
--) {
2056 if (env
->fptags
[i
]) {
2061 qemu_put_be16s(f
, &fpuc
);
2062 qemu_put_be16s(f
, &fpus
);
2063 qemu_put_be16s(f
, &fptag
);
2065 for(i
= 0; i
< 8; i
++) {
2068 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
]);
2069 qemu_put_be64(f
, mant
);
2070 qemu_put_be16(f
, exp
);
2073 for(i
= 0; i
< 6; i
++)
2074 cpu_put_seg(f
, &env
->segs
[i
]);
2075 cpu_put_seg(f
, &env
->ldt
);
2076 cpu_put_seg(f
, &env
->tr
);
2077 cpu_put_seg(f
, &env
->gdt
);
2078 cpu_put_seg(f
, &env
->idt
);
2080 qemu_put_be32s(f
, &env
->sysenter_cs
);
2081 qemu_put_be32s(f
, &env
->sysenter_esp
);
2082 qemu_put_be32s(f
, &env
->sysenter_eip
);
2084 qemu_put_be32s(f
, &env
->cr
[0]);
2085 qemu_put_be32s(f
, &env
->cr
[2]);
2086 qemu_put_be32s(f
, &env
->cr
[3]);
2087 qemu_put_be32s(f
, &env
->cr
[4]);
2089 for(i
= 0; i
< 8; i
++)
2090 qemu_put_be32s(f
, &env
->dr
[i
]);
2093 qemu_put_be32s(f
, &env
->a20_mask
);
2096 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
2098 CPUState
*env
= opaque
;
2101 uint16_t fpus
, fpuc
, fptag
;
2103 if (version_id
!= 2)
2105 for(i
= 0; i
< 8; i
++)
2106 qemu_get_be32s(f
, &env
->regs
[i
]);
2107 qemu_get_be32s(f
, &env
->eip
);
2108 qemu_get_be32s(f
, &env
->eflags
);
2109 qemu_get_be32s(f
, &env
->eflags
);
2110 qemu_get_be32s(f
, &hflags
);
2112 qemu_get_be16s(f
, &fpuc
);
2113 qemu_get_be16s(f
, &fpus
);
2114 qemu_get_be16s(f
, &fptag
);
2116 for(i
= 0; i
< 8; i
++) {
2119 mant
= qemu_get_be64(f
);
2120 exp
= qemu_get_be16(f
);
2121 env
->fpregs
[i
] = cpu_set_fp80(mant
, exp
);
2125 env
->fpstt
= (fpus
>> 11) & 7;
2126 env
->fpus
= fpus
& ~0x3800;
2127 for(i
= 0; i
< 8; i
++) {
2128 env
->fptags
[i
] = ((fptag
& 3) == 3);
2132 for(i
= 0; i
< 6; i
++)
2133 cpu_get_seg(f
, &env
->segs
[i
]);
2134 cpu_get_seg(f
, &env
->ldt
);
2135 cpu_get_seg(f
, &env
->tr
);
2136 cpu_get_seg(f
, &env
->gdt
);
2137 cpu_get_seg(f
, &env
->idt
);
2139 qemu_get_be32s(f
, &env
->sysenter_cs
);
2140 qemu_get_be32s(f
, &env
->sysenter_esp
);
2141 qemu_get_be32s(f
, &env
->sysenter_eip
);
2143 qemu_get_be32s(f
, &env
->cr
[0]);
2144 qemu_get_be32s(f
, &env
->cr
[2]);
2145 qemu_get_be32s(f
, &env
->cr
[3]);
2146 qemu_get_be32s(f
, &env
->cr
[4]);
2148 for(i
= 0; i
< 8; i
++)
2149 qemu_get_be32s(f
, &env
->dr
[i
]);
2152 qemu_get_be32s(f
, &env
->a20_mask
);
2154 /* XXX: compute hflags from scratch, except for CPL and IIF */
2155 env
->hflags
= hflags
;
2160 #elif defined(TARGET_PPC)
2161 void cpu_save(QEMUFile
*f
, void *opaque
)
2165 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
2169 #elif defined(TARGET_SPARC)
2170 void cpu_save(QEMUFile
*f
, void *opaque
)
2174 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
2180 #warning No CPU save/restore functions
2184 /***********************************************************/
2185 /* ram save/restore */
2187 /* we just avoid storing empty pages */
2188 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
2193 for(i
= 1; i
< len
; i
++) {
2197 qemu_put_byte(f
, 1);
2198 qemu_put_byte(f
, v
);
2201 qemu_put_byte(f
, 0);
2202 qemu_put_buffer(f
, buf
, len
);
2205 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2209 v
= qemu_get_byte(f
);
2212 if (qemu_get_buffer(f
, buf
, len
) != len
)
2216 v
= qemu_get_byte(f
);
2217 memset(buf
, v
, len
);
2225 static void ram_save(QEMUFile
*f
, void *opaque
)
2228 qemu_put_be32(f
, phys_ram_size
);
2229 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
2230 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
2234 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2238 if (version_id
!= 1)
2240 if (qemu_get_be32(f
) != phys_ram_size
)
2242 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
2243 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
2250 /***********************************************************/
2251 /* main execution loop */
2253 void gui_update(void *opaque
)
2255 display_state
.dpy_refresh(&display_state
);
2256 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
2259 /* XXX: support several handlers */
2260 VMStopHandler
*vm_stop_cb
;
2261 VMStopHandler
*vm_stop_opaque
;
2263 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
2266 vm_stop_opaque
= opaque
;
2270 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
2283 void vm_stop(int reason
)
2286 cpu_disable_ticks();
2290 vm_stop_cb(vm_stop_opaque
, reason
);
2296 /* reset/shutdown handler */
2298 typedef struct QEMUResetEntry
{
2299 QEMUResetHandler
*func
;
2301 struct QEMUResetEntry
*next
;
2304 static QEMUResetEntry
*first_reset_entry
;
2305 static int reset_requested
;
2306 static int shutdown_requested
;
2308 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
2310 QEMUResetEntry
**pre
, *re
;
2312 pre
= &first_reset_entry
;
2313 while (*pre
!= NULL
)
2314 pre
= &(*pre
)->next
;
2315 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
2317 re
->opaque
= opaque
;
2322 void qemu_system_reset(void)
2326 /* reset all devices */
2327 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
2328 re
->func(re
->opaque
);
2332 void qemu_system_reset_request(void)
2334 reset_requested
= 1;
2335 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
2338 void qemu_system_shutdown_request(void)
2340 shutdown_requested
= 1;
2341 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
2344 static void main_cpu_reset(void *opaque
)
2347 CPUState
*env
= opaque
;
2352 void main_loop_wait(int timeout
)
2355 struct pollfd ufds
[MAX_IO_HANDLERS
+ 1], *pf
;
2356 IOHandlerRecord
*ioh
, *ioh_next
;
2366 /* poll any events */
2367 /* XXX: separate device handlers from system ones */
2369 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2370 if (!ioh
->fd_can_read
) {
2373 pf
->events
= POLLIN
;
2377 max_size
= ioh
->fd_can_read(ioh
->opaque
);
2379 if (max_size
> sizeof(buf
))
2380 max_size
= sizeof(buf
);
2382 pf
->events
= POLLIN
;
2389 ioh
->max_size
= max_size
;
2392 ret
= poll(ufds
, pf
- ufds
, timeout
);
2394 /* XXX: better handling of removal */
2395 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
2396 ioh_next
= ioh
->next
;
2399 if (pf
->revents
& POLLIN
) {
2400 if (ioh
->max_size
== 0) {
2401 /* just a read event */
2402 ioh
->fd_read(ioh
->opaque
, NULL
, 0);
2404 n
= read(ioh
->fd
, buf
, ioh
->max_size
);
2406 ioh
->fd_read(ioh
->opaque
, buf
, n
);
2407 } else if (errno
!= EAGAIN
) {
2408 ioh
->fd_read(ioh
->opaque
, NULL
, -errno
);
2415 #endif /* !defined(_WIN32) */
2416 #if defined(CONFIG_SLIRP)
2417 /* XXX: merge with poll() */
2419 fd_set rfds
, wfds
, xfds
;
2427 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
2430 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
2432 slirp_select_poll(&rfds
, &wfds
, &xfds
);
2438 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
2439 qemu_get_clock(vm_clock
));
2441 if (audio_enabled
) {
2442 /* XXX: add explicit timer */
2446 /* run dma transfers, if any */
2450 /* real time timers */
2451 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
2452 qemu_get_clock(rt_clock
));
2458 CPUState
*env
= global_env
;
2462 ret
= cpu_exec(env
);
2463 if (shutdown_requested
) {
2464 ret
= EXCP_INTERRUPT
;
2467 if (reset_requested
) {
2468 reset_requested
= 0;
2469 qemu_system_reset();
2470 ret
= EXCP_INTERRUPT
;
2472 if (ret
== EXCP_DEBUG
) {
2473 vm_stop(EXCP_DEBUG
);
2475 /* if hlt instruction, we wait until the next IRQ */
2476 /* XXX: use timeout computed from timers */
2477 if (ret
== EXCP_HLT
)
2484 main_loop_wait(timeout
);
2486 cpu_disable_ticks();
2492 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2004 Fabrice Bellard\n"
2493 "usage: %s [options] [disk_image]\n"
2495 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2497 "Standard options:\n"
2498 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
2499 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
2500 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
2501 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
2502 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
2503 "-snapshot write to temporary files instead of disk image files\n"
2504 "-m megs set virtual RAM size to megs MB [default=%d]\n"
2505 "-nographic disable graphical output and redirect serial I/Os to console\n"
2506 "-enable-audio enable audio support\n"
2507 "-localtime set the real time clock to local time [default=utc]\n"
2509 "-prep Simulate a PREP system (default is PowerMAC)\n"
2510 "-g WxH[xDEPTH] Set the initial VGA graphic mode\n"
2513 "Network options:\n"
2514 "-nics n simulate 'n' network cards [default=1]\n"
2515 "-macaddr addr set the mac address of the first interface\n"
2516 "-n script set tap/tun network init script [default=%s]\n"
2517 "-tun-fd fd use this fd as already opened tap/tun interface\n"
2519 "-user-net use user mode network stack [default if no tap/tun script]\n"
2520 "-tftp prefix allow tftp access to files starting with prefix [-user-net]\n"
2522 "-smb dir allow SMB access to files in 'dir' [-user-net]\n"
2524 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
2525 " redirect TCP or UDP connections from host to guest [-user-net]\n"
2527 "-dummy-net use dummy network stack\n"
2529 "Linux boot specific:\n"
2530 "-kernel bzImage use 'bzImage' as kernel image\n"
2531 "-append cmdline use 'cmdline' as kernel command line\n"
2532 "-initrd file use 'file' as initial ram disk\n"
2534 "Debug/Expert options:\n"
2535 "-monitor dev redirect the monitor to char device 'dev'\n"
2536 "-serial dev redirect the serial port to char device 'dev'\n"
2537 "-S freeze CPU at startup (use 'c' to start execution)\n"
2538 "-s wait gdb connection to port %d\n"
2539 "-p port change gdb connection port\n"
2540 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
2541 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
2542 "-L path set the directory for the BIOS and VGA BIOS\n"
2543 #ifdef USE_CODE_COPY
2544 "-no-code-copy disable code copy acceleration\n"
2547 "-isa simulate an ISA-only system (default is PCI system)\n"
2548 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
2549 " (default is CL-GD5446 PCI VGA)\n"
2552 "During emulation, the following keys are useful:\n"
2553 "ctrl-shift-f toggle full screen\n"
2554 "ctrl-shift-Fn switch to virtual console 'n'\n"
2555 "ctrl-shift toggle mouse and keyboard grab\n"
2557 "When using -nographic, press 'ctrl-a h' to get some help.\n"
2559 #ifdef CONFIG_SOFTMMU
2565 DEFAULT_NETWORK_SCRIPT
,
2566 DEFAULT_GDBSTUB_PORT
,
2568 #ifndef CONFIG_SOFTMMU
2570 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
2571 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
2577 #define HAS_ARG 0x0001
2590 QEMU_OPTION_snapshot
,
2592 QEMU_OPTION_nographic
,
2593 QEMU_OPTION_enable_audio
,
2596 QEMU_OPTION_macaddr
,
2599 QEMU_OPTION_user_net
,
2603 QEMU_OPTION_dummy_net
,
2615 QEMU_OPTION_no_code_copy
,
2619 QEMU_OPTION_localtime
,
2620 QEMU_OPTION_cirrusvga
,
2622 QEMU_OPTION_std_vga
,
2623 QEMU_OPTION_monitor
,
2627 typedef struct QEMUOption
{
2633 const QEMUOption qemu_options
[] = {
2634 { "h", 0, QEMU_OPTION_h
},
2636 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
2637 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
2638 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
2639 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
2640 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
2641 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
2642 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
2643 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
2644 { "snapshot", 0, QEMU_OPTION_snapshot
},
2645 { "m", HAS_ARG
, QEMU_OPTION_m
},
2646 { "nographic", 0, QEMU_OPTION_nographic
},
2647 { "enable-audio", 0, QEMU_OPTION_enable_audio
},
2649 { "nics", HAS_ARG
, QEMU_OPTION_nics
},
2650 { "macaddr", HAS_ARG
, QEMU_OPTION_macaddr
},
2651 { "n", HAS_ARG
, QEMU_OPTION_n
},
2652 { "tun-fd", HAS_ARG
, QEMU_OPTION_tun_fd
},
2654 { "user-net", 0, QEMU_OPTION_user_net
},
2655 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
2657 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
2659 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
2661 { "dummy-net", 0, QEMU_OPTION_dummy_net
},
2663 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
2664 { "append", HAS_ARG
, QEMU_OPTION_append
},
2665 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
2667 { "S", 0, QEMU_OPTION_S
},
2668 { "s", 0, QEMU_OPTION_s
},
2669 { "p", HAS_ARG
, QEMU_OPTION_p
},
2670 { "d", HAS_ARG
, QEMU_OPTION_d
},
2671 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
2672 { "L", HAS_ARG
, QEMU_OPTION_L
},
2673 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
2675 { "prep", 0, QEMU_OPTION_prep
},
2676 { "g", 1, QEMU_OPTION_g
},
2678 { "localtime", 0, QEMU_OPTION_localtime
},
2679 { "isa", 0, QEMU_OPTION_isa
},
2680 { "std-vga", 0, QEMU_OPTION_std_vga
},
2681 { "monitor", 1, QEMU_OPTION_monitor
},
2682 { "serial", 1, QEMU_OPTION_serial
},
2684 /* temporary options */
2685 { "pci", 0, QEMU_OPTION_pci
},
2686 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
2690 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2692 /* this stack is only used during signal handling */
2693 #define SIGNAL_STACK_SIZE 32768
2695 static uint8_t *signal_stack
;
2699 /* password input */
2701 static BlockDriverState
*get_bdrv(int index
)
2703 BlockDriverState
*bs
;
2706 bs
= bs_table
[index
];
2707 } else if (index
< 6) {
2708 bs
= fd_table
[index
- 4];
2715 static void read_passwords(void)
2717 BlockDriverState
*bs
;
2721 for(i
= 0; i
< 6; i
++) {
2723 if (bs
&& bdrv_is_encrypted(bs
)) {
2724 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
2725 for(j
= 0; j
< 3; j
++) {
2726 monitor_readline("Password: ",
2727 1, password
, sizeof(password
));
2728 if (bdrv_set_key(bs
, password
) == 0)
2730 term_printf("invalid password\n");
2736 #define NET_IF_TUN 0
2737 #define NET_IF_USER 1
2738 #define NET_IF_DUMMY 2
2740 int main(int argc
, char **argv
)
2742 #ifdef CONFIG_GDBSTUB
2743 int use_gdbstub
, gdbstub_port
;
2746 int snapshot
, linux_boot
;
2748 const char *initrd_filename
;
2749 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
2750 const char *kernel_filename
, *kernel_cmdline
;
2751 DisplayState
*ds
= &display_state
;
2752 int cyls
, heads
, secs
;
2753 int start_emulation
= 1;
2755 int net_if_type
, nb_tun_fds
, tun_fds
[MAX_NICS
];
2757 const char *r
, *optarg
;
2758 CharDriverState
*monitor_hd
;
2759 char monitor_device
[128];
2760 char serial_devices
[MAX_SERIAL_PORTS
][128];
2761 int serial_device_index
;
2763 #if !defined(CONFIG_SOFTMMU)
2764 /* we never want that malloc() uses mmap() */
2765 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
2767 initrd_filename
= NULL
;
2768 for(i
= 0; i
< MAX_FD
; i
++)
2769 fd_filename
[i
] = NULL
;
2770 for(i
= 0; i
< MAX_DISKS
; i
++)
2771 hd_filename
[i
] = NULL
;
2772 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
2773 vga_ram_size
= VGA_RAM_SIZE
;
2774 bios_size
= BIOS_SIZE
;
2775 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
2776 #ifdef CONFIG_GDBSTUB
2778 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
2782 kernel_filename
= NULL
;
2783 kernel_cmdline
= "";
2785 cyls
= heads
= secs
= 0;
2786 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
2788 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
2789 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
2790 serial_devices
[i
][0] = '\0';
2791 serial_device_index
= 0;
2796 /* default mac address of the first network interface */
2810 hd_filename
[0] = argv
[optind
++];
2812 const QEMUOption
*popt
;
2815 popt
= qemu_options
;
2818 fprintf(stderr
, "%s: invalid option -- '%s'\n",
2822 if (!strcmp(popt
->name
, r
+ 1))
2826 if (popt
->flags
& HAS_ARG
) {
2827 if (optind
>= argc
) {
2828 fprintf(stderr
, "%s: option '%s' requires an argument\n",
2832 optarg
= argv
[optind
++];
2837 switch(popt
->index
) {
2838 case QEMU_OPTION_initrd
:
2839 initrd_filename
= optarg
;
2841 case QEMU_OPTION_hda
:
2842 hd_filename
[0] = optarg
;
2844 case QEMU_OPTION_hdb
:
2845 hd_filename
[1] = optarg
;
2847 case QEMU_OPTION_snapshot
:
2850 case QEMU_OPTION_hdachs
:
2854 cyls
= strtol(p
, (char **)&p
, 0);
2858 heads
= strtol(p
, (char **)&p
, 0);
2862 secs
= strtol(p
, (char **)&p
, 0);
2869 case QEMU_OPTION_nographic
:
2870 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
2871 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
2874 case QEMU_OPTION_kernel
:
2875 kernel_filename
= optarg
;
2877 case QEMU_OPTION_append
:
2878 kernel_cmdline
= optarg
;
2880 case QEMU_OPTION_tun_fd
:
2884 net_if_type
= NET_IF_TUN
;
2885 if (nb_tun_fds
< MAX_NICS
) {
2886 fd
= strtol(optarg
, (char **)&p
, 0);
2888 fprintf(stderr
, "qemu: invalid fd for network interface %d\n", nb_tun_fds
);
2891 tun_fds
[nb_tun_fds
++] = fd
;
2895 case QEMU_OPTION_hdc
:
2896 hd_filename
[2] = optarg
;
2899 case QEMU_OPTION_hdd
:
2900 hd_filename
[3] = optarg
;
2902 case QEMU_OPTION_cdrom
:
2903 hd_filename
[2] = optarg
;
2906 case QEMU_OPTION_boot
:
2907 boot_device
= optarg
[0];
2908 if (boot_device
!= 'a' && boot_device
!= 'b' &&
2909 boot_device
!= 'c' && boot_device
!= 'd') {
2910 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
2914 case QEMU_OPTION_fda
:
2915 fd_filename
[0] = optarg
;
2917 case QEMU_OPTION_fdb
:
2918 fd_filename
[1] = optarg
;
2920 case QEMU_OPTION_no_code_copy
:
2921 code_copy_enabled
= 0;
2923 case QEMU_OPTION_nics
:
2924 nb_nics
= atoi(optarg
);
2925 if (nb_nics
< 0 || nb_nics
> MAX_NICS
) {
2926 fprintf(stderr
, "qemu: invalid number of network interfaces\n");
2930 case QEMU_OPTION_macaddr
:
2935 for(i
= 0; i
< 6; i
++) {
2936 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2943 fprintf(stderr
, "qemu: invalid syntax for ethernet address\n");
2952 case QEMU_OPTION_tftp
:
2953 tftp_prefix
= optarg
;
2956 case QEMU_OPTION_smb
:
2957 net_slirp_smb(optarg
);
2960 case QEMU_OPTION_user_net
:
2961 net_if_type
= NET_IF_USER
;
2963 case QEMU_OPTION_redir
:
2964 net_slirp_redir(optarg
);
2967 case QEMU_OPTION_dummy_net
:
2968 net_if_type
= NET_IF_DUMMY
;
2970 case QEMU_OPTION_enable_audio
:
2977 ram_size
= atoi(optarg
) * 1024 * 1024;
2980 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
2981 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
2982 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
2991 mask
= cpu_str_to_log_mask(optarg
);
2993 printf("Log items (comma separated):\n");
2994 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
2995 printf("%-10s %s\n", item
->name
, item
->help
);
3003 pstrcpy(network_script
, sizeof(network_script
), optarg
);
3005 #ifdef CONFIG_GDBSTUB
3010 gdbstub_port
= atoi(optarg
);
3017 start_emulation
= 0;
3019 case QEMU_OPTION_pci
:
3022 case QEMU_OPTION_isa
:
3025 case QEMU_OPTION_prep
:
3028 case QEMU_OPTION_localtime
:
3031 case QEMU_OPTION_cirrusvga
:
3032 cirrus_vga_enabled
= 1;
3034 case QEMU_OPTION_std_vga
:
3035 cirrus_vga_enabled
= 0;
3042 w
= strtol(p
, (char **)&p
, 10);
3045 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3051 h
= strtol(p
, (char **)&p
, 10);
3056 depth
= strtol(p
, (char **)&p
, 10);
3057 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3058 depth
!= 24 && depth
!= 32)
3060 } else if (*p
== '\0') {
3061 depth
= graphic_depth
;
3068 graphic_depth
= depth
;
3071 case QEMU_OPTION_monitor
:
3072 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
3074 case QEMU_OPTION_serial
:
3075 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
3076 fprintf(stderr
, "qemu: too many serial ports\n");
3079 pstrcpy(serial_devices
[serial_device_index
],
3080 sizeof(serial_devices
[0]), optarg
);
3081 serial_device_index
++;
3087 linux_boot
= (kernel_filename
!= NULL
);
3089 if (!linux_boot
&& hd_filename
[0] == '\0' && hd_filename
[2] == '\0' &&
3090 fd_filename
[0] == '\0')
3093 /* boot to cd by default if no hard disk */
3094 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
3095 if (fd_filename
[0] != '\0')
3101 #if !defined(CONFIG_SOFTMMU)
3102 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
3104 static uint8_t stdout_buf
[4096];
3105 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
3108 setvbuf(stdout
, NULL
, _IOLBF
, 0);
3111 /* init host network redirectors */
3112 if (net_if_type
== -1) {
3113 net_if_type
= NET_IF_TUN
;
3114 #if defined(CONFIG_SLIRP)
3115 if (access(network_script
, R_OK
) < 0) {
3116 net_if_type
= NET_IF_USER
;
3121 for(i
= 0; i
< nb_nics
; i
++) {
3122 NetDriverState
*nd
= &nd_table
[i
];
3124 /* init virtual mac address */
3125 nd
->macaddr
[0] = macaddr
[0];
3126 nd
->macaddr
[1] = macaddr
[1];
3127 nd
->macaddr
[2] = macaddr
[2];
3128 nd
->macaddr
[3] = macaddr
[3];
3129 nd
->macaddr
[4] = macaddr
[4];
3130 nd
->macaddr
[5] = macaddr
[5] + i
;
3131 switch(net_if_type
) {
3132 #if defined(CONFIG_SLIRP)
3137 #if !defined(_WIN32)
3139 if (i
< nb_tun_fds
) {
3140 net_fd_init(nd
, tun_fds
[i
]);
3142 if (net_tun_init(nd
) < 0)
3154 /* init the memory */
3155 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
3157 #ifdef CONFIG_SOFTMMU
3159 /* mallocs are always aligned on BSD. valloc is better for correctness */
3160 phys_ram_base
= valloc(phys_ram_size
);
3162 phys_ram_base
= memalign(TARGET_PAGE_SIZE
, phys_ram_size
);
3164 if (!phys_ram_base
) {
3165 fprintf(stderr
, "Could not allocate physical memory\n");
3169 /* as we must map the same page at several addresses, we must use
3174 tmpdir
= getenv("QEMU_TMPDIR");
3177 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
3178 if (mkstemp(phys_ram_file
) < 0) {
3179 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
3183 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
3184 if (phys_ram_fd
< 0) {
3185 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
3189 ftruncate(phys_ram_fd
, phys_ram_size
);
3190 unlink(phys_ram_file
);
3191 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
3193 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
3195 if (phys_ram_base
== MAP_FAILED
) {
3196 fprintf(stderr
, "Could not map physical memory\n");
3202 /* we always create the cdrom drive, even if no disk is there */
3205 bs_table
[2] = bdrv_new("cdrom");
3206 bdrv_set_type_hint(bs_table
[2], BDRV_TYPE_CDROM
);
3209 /* open the virtual block devices */
3210 for(i
= 0; i
< MAX_DISKS
; i
++) {
3211 if (hd_filename
[i
]) {
3214 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
3215 bs_table
[i
] = bdrv_new(buf
);
3217 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
3218 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
3222 if (i
== 0 && cyls
!= 0)
3223 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
3227 /* we always create at least one floppy disk */
3228 fd_table
[0] = bdrv_new("fda");
3229 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
3231 for(i
= 0; i
< MAX_FD
; i
++) {
3232 if (fd_filename
[i
]) {
3235 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
3236 fd_table
[i
] = bdrv_new(buf
);
3237 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
3239 if (fd_filename
[i
] != '\0') {
3240 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
3241 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
3249 /* init CPU state */
3252 cpu_single_env
= env
;
3254 register_savevm("timer", 0, 1, timer_save
, timer_load
, env
);
3255 register_savevm("cpu", 0, 2, cpu_save
, cpu_load
, env
);
3256 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
3257 qemu_register_reset(main_cpu_reset
, global_env
);
3260 cpu_calibrate_ticks();
3264 dumb_display_init(ds
);
3267 sdl_display_init(ds
);
3269 dumb_display_init(ds
);
3273 vga_console
= graphic_console_init(ds
);
3275 monitor_hd
= qemu_chr_open(monitor_device
);
3277 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
3280 monitor_init(monitor_hd
, !nographic
);
3282 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
3283 if (serial_devices
[i
][0] != '\0') {
3284 serial_hds
[i
] = qemu_chr_open(serial_devices
[i
]);
3285 if (!serial_hds
[i
]) {
3286 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
3290 if (!strcmp(serial_devices
[i
], "vc"))
3291 qemu_chr_printf(serial_hds
[i
], "serial%d console\n", i
);
3295 /* setup cpu signal handlers for MMU / self modifying code handling */
3296 #if !defined(CONFIG_SOFTMMU)
3298 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3301 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
3302 stk
.ss_sp
= signal_stack
;
3303 stk
.ss_size
= SIGNAL_STACK_SIZE
;
3306 if (sigaltstack(&stk
, NULL
) < 0) {
3307 perror("sigaltstack");
3313 struct sigaction act
;
3315 sigfillset(&act
.sa_mask
);
3316 act
.sa_flags
= SA_SIGINFO
;
3317 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3318 act
.sa_flags
|= SA_ONSTACK
;
3320 act
.sa_sigaction
= host_segv_handler
;
3321 sigaction(SIGSEGV
, &act
, NULL
);
3322 sigaction(SIGBUS
, &act
, NULL
);
3323 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3324 sigaction(SIGFPE
, &act
, NULL
);
3331 struct sigaction act
;
3332 sigfillset(&act
.sa_mask
);
3334 act
.sa_handler
= SIG_IGN
;
3335 sigaction(SIGPIPE
, &act
, NULL
);
3340 #if defined(TARGET_I386)
3341 pc_init(ram_size
, vga_ram_size
, boot_device
,
3342 ds
, fd_filename
, snapshot
,
3343 kernel_filename
, kernel_cmdline
, initrd_filename
);
3344 #elif defined(TARGET_PPC)
3345 ppc_init(ram_size
, vga_ram_size
, boot_device
,
3346 ds
, fd_filename
, snapshot
,
3347 kernel_filename
, kernel_cmdline
, initrd_filename
);
3348 #elif defined(TARGET_SPARC)
3349 sun4m_init(ram_size
, vga_ram_size
, boot_device
,
3350 ds
, fd_filename
, snapshot
,
3351 kernel_filename
, kernel_cmdline
, initrd_filename
);
3354 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
3355 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
3357 #ifdef CONFIG_GDBSTUB
3359 if (gdbserver_start(gdbstub_port
) < 0) {
3360 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
3364 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
3369 /* XXX: simplify init */
3371 if (start_emulation
) {