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
36 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/socket.h>
45 #include <linux/if_tun.h>
48 #if defined(CONFIG_SLIRP)
53 #include <sys/timeb.h>
55 #define getopt_long_only getopt_long
56 #define memalign(align, size) malloc(size)
60 /* SDL use the pthreads and they modify sigaction. We don't
62 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
63 extern void __libc_sigaction();
64 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
66 extern void __sigaction();
67 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
69 #endif /* CONFIG_SDL */
77 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
79 //#define DEBUG_UNUSED_IOPORT
81 #if !defined(CONFIG_SOFTMMU)
82 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
84 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
88 #define GUI_REFRESH_INTERVAL 30
90 /* XXX: use a two level table to limit memory usage */
91 #define MAX_IOPORTS 65536
93 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
94 char phys_ram_file
[1024];
96 CPUState
*cpu_single_env
;
97 void *ioport_opaque
[MAX_IOPORTS
];
98 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
99 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
100 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
102 static DisplayState display_state
;
104 int64_t ticks_per_sec
;
105 int boot_device
= 'c';
107 static char network_script
[1024];
108 int pit_min_timer_count
= 0;
110 NetDriverState nd_table
[MAX_NICS
];
111 SerialState
*serial_console
;
112 QEMUTimer
*gui_timer
;
115 /***********************************************************/
118 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
120 #ifdef DEBUG_UNUSED_IOPORT
121 fprintf(stderr
, "inb: port=0x%04x\n", address
);
126 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
128 #ifdef DEBUG_UNUSED_IOPORT
129 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
133 /* default is to make two byte accesses */
134 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
137 data
= ioport_read_table
[0][address
& (MAX_IOPORTS
- 1)](opaque
, address
);
138 data
|= ioport_read_table
[0][(address
+ 1) & (MAX_IOPORTS
- 1)](opaque
, address
+ 1) << 8;
142 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
144 ioport_write_table
[0][address
& (MAX_IOPORTS
- 1)](opaque
, address
, data
& 0xff);
145 ioport_write_table
[0][(address
+ 1) & (MAX_IOPORTS
- 1)](opaque
, address
+ 1, (data
>> 8) & 0xff);
148 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
150 #ifdef DEBUG_UNUSED_IOPORT
151 fprintf(stderr
, "inl: port=0x%04x\n", address
);
156 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
158 #ifdef DEBUG_UNUSED_IOPORT
159 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
163 void init_ioports(void)
167 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
168 ioport_read_table
[0][i
] = default_ioport_readb
;
169 ioport_write_table
[0][i
] = default_ioport_writeb
;
170 ioport_read_table
[1][i
] = default_ioport_readw
;
171 ioport_write_table
[1][i
] = default_ioport_writew
;
172 ioport_read_table
[2][i
] = default_ioport_readl
;
173 ioport_write_table
[2][i
] = default_ioport_writel
;
177 /* size is the word size in byte */
178 int register_ioport_read(int start
, int length
, int size
,
179 IOPortReadFunc
*func
, void *opaque
)
185 } else if (size
== 2) {
187 } else if (size
== 4) {
190 hw_error("register_ioport_read: invalid size");
193 for(i
= start
; i
< start
+ length
; i
+= size
) {
194 ioport_read_table
[bsize
][i
] = func
;
195 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
196 hw_error("register_ioport_read: invalid opaque");
197 ioport_opaque
[i
] = opaque
;
202 /* size is the word size in byte */
203 int register_ioport_write(int start
, int length
, int size
,
204 IOPortWriteFunc
*func
, void *opaque
)
210 } else if (size
== 2) {
212 } else if (size
== 4) {
215 hw_error("register_ioport_write: invalid size");
218 for(i
= start
; i
< start
+ length
; i
+= size
) {
219 ioport_write_table
[bsize
][i
] = func
;
220 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
221 hw_error("register_ioport_read: invalid opaque");
222 ioport_opaque
[i
] = opaque
;
227 void pstrcpy(char *buf
, int buf_size
, const char *str
)
237 if (c
== 0 || q
>= buf
+ buf_size
- 1)
244 /* strcat and truncate. */
245 char *pstrcat(char *buf
, int buf_size
, const char *s
)
250 pstrcpy(buf
+ len
, buf_size
- len
, s
);
254 /* return the size or -1 if error */
255 int load_image(const char *filename
, uint8_t *addr
)
258 fd
= open(filename
, O_RDONLY
| O_BINARY
);
261 size
= lseek(fd
, 0, SEEK_END
);
262 lseek(fd
, 0, SEEK_SET
);
263 if (read(fd
, addr
, size
) != size
) {
271 void cpu_outb(CPUState
*env
, int addr
, int val
)
273 addr
&= (MAX_IOPORTS
- 1);
274 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
277 void cpu_outw(CPUState
*env
, int addr
, int val
)
279 addr
&= (MAX_IOPORTS
- 1);
280 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
283 void cpu_outl(CPUState
*env
, int addr
, int val
)
285 addr
&= (MAX_IOPORTS
- 1);
286 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
289 int cpu_inb(CPUState
*env
, int addr
)
291 addr
&= (MAX_IOPORTS
- 1);
292 return ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
295 int cpu_inw(CPUState
*env
, int addr
)
297 addr
&= (MAX_IOPORTS
- 1);
298 return ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
301 int cpu_inl(CPUState
*env
, int addr
)
303 addr
&= (MAX_IOPORTS
- 1);
304 return ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
307 /***********************************************************/
308 void hw_error(const char *fmt
, ...)
313 fprintf(stderr
, "qemu: hardware error: ");
314 vfprintf(stderr
, fmt
, ap
);
315 fprintf(stderr
, "\n");
317 cpu_x86_dump_state(global_env
, stderr
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
319 cpu_dump_state(global_env
, stderr
, 0);
325 /***********************************************************/
328 #if defined(__powerpc__)
330 static inline uint32_t get_tbl(void)
333 asm volatile("mftb %0" : "=r" (tbl
));
337 static inline uint32_t get_tbu(void)
340 asm volatile("mftbu %0" : "=r" (tbl
));
344 int64_t cpu_get_real_ticks(void)
347 /* NOTE: we test if wrapping has occurred */
353 return ((int64_t)h
<< 32) | l
;
356 #elif defined(__i386__)
358 int64_t cpu_get_real_ticks(void)
361 asm volatile ("rdtsc" : "=A" (val
));
366 #error unsupported CPU
369 static int64_t cpu_ticks_offset
;
370 static int cpu_ticks_enabled
;
372 static inline int64_t cpu_get_ticks(void)
374 if (!cpu_ticks_enabled
) {
375 return cpu_ticks_offset
;
377 return cpu_get_real_ticks() + cpu_ticks_offset
;
381 /* enable cpu_get_ticks() */
382 void cpu_enable_ticks(void)
384 if (!cpu_ticks_enabled
) {
385 cpu_ticks_offset
-= cpu_get_real_ticks();
386 cpu_ticks_enabled
= 1;
390 /* disable cpu_get_ticks() : the clock is stopped. You must not call
391 cpu_get_ticks() after that. */
392 void cpu_disable_ticks(void)
394 if (cpu_ticks_enabled
) {
395 cpu_ticks_offset
= cpu_get_ticks();
396 cpu_ticks_enabled
= 0;
400 static int64_t get_clock(void)
405 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
408 gettimeofday(&tv
, NULL
);
409 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
413 void cpu_calibrate_ticks(void)
418 ticks
= cpu_get_real_ticks();
424 usec
= get_clock() - usec
;
425 ticks
= cpu_get_real_ticks() - ticks
;
426 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
429 /* compute with 96 bit intermediate result: (a*b)/c */
430 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
435 #ifdef WORDS_BIGENDIAN
445 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
446 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
449 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
453 #define QEMU_TIMER_REALTIME 0
454 #define QEMU_TIMER_VIRTUAL 1
458 /* XXX: add frequency */
466 struct QEMUTimer
*next
;
472 static QEMUTimer
*active_timers
[2];
474 static MMRESULT timerID
;
476 /* frequency of the times() clock tick */
477 static int timer_freq
;
480 QEMUClock
*qemu_new_clock(int type
)
483 clock
= qemu_mallocz(sizeof(QEMUClock
));
490 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
494 ts
= qemu_mallocz(sizeof(QEMUTimer
));
501 void qemu_free_timer(QEMUTimer
*ts
)
506 /* stop a timer, but do not dealloc it */
507 void qemu_del_timer(QEMUTimer
*ts
)
511 /* NOTE: this code must be signal safe because
512 qemu_timer_expired() can be called from a signal. */
513 pt
= &active_timers
[ts
->clock
->type
];
526 /* modify the current timer so that it will be fired when current_time
527 >= expire_time. The corresponding callback will be called. */
528 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
534 /* add the timer in the sorted list */
535 /* NOTE: this code must be signal safe because
536 qemu_timer_expired() can be called from a signal. */
537 pt
= &active_timers
[ts
->clock
->type
];
542 if (t
->expire_time
> expire_time
)
546 ts
->expire_time
= expire_time
;
551 int qemu_timer_pending(QEMUTimer
*ts
)
554 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
561 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
565 return (timer_head
->expire_time
<= current_time
);
568 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
574 if (ts
->expire_time
> current_time
)
576 /* remove timer from the list before calling the callback */
577 *ptimer_head
= ts
->next
;
580 /* run the callback (the timer list can be modified) */
585 int64_t qemu_get_clock(QEMUClock
*clock
)
587 switch(clock
->type
) {
588 case QEMU_TIMER_REALTIME
:
590 return GetTickCount();
592 /* XXX: portability among Linux hosts */
593 if (timer_freq
== 100) {
594 return times(NULL
) * 10;
596 return ((int64_t)times(NULL
) * 1000) / timer_freq
;
600 case QEMU_TIMER_VIRTUAL
:
601 return cpu_get_ticks();
606 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
608 uint64_t expire_time
;
610 if (qemu_timer_pending(ts
)) {
611 expire_time
= ts
->expire_time
;
615 qemu_put_be64(f
, expire_time
);
618 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
620 uint64_t expire_time
;
622 expire_time
= qemu_get_be64(f
);
623 if (expire_time
!= -1) {
624 qemu_mod_timer(ts
, expire_time
);
630 static void timer_save(QEMUFile
*f
, void *opaque
)
632 if (cpu_ticks_enabled
) {
633 hw_error("cannot save state if virtual timers are running");
635 qemu_put_be64s(f
, &cpu_ticks_offset
);
636 qemu_put_be64s(f
, &ticks_per_sec
);
639 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
643 if (cpu_ticks_enabled
) {
646 qemu_get_be64s(f
, &cpu_ticks_offset
);
647 qemu_get_be64s(f
, &ticks_per_sec
);
652 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
653 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
655 static void host_alarm_handler(int host_signum
)
658 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
659 qemu_get_clock(vm_clock
)) ||
660 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
661 qemu_get_clock(rt_clock
))) {
662 /* stop the cpu because a timer occured */
663 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
667 static void init_timers(void)
669 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
670 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
675 timerID
= timeSetEvent(10, // interval (ms)
677 host_alarm_handler
, // function
678 (DWORD
)&count
, // user parameter
679 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
681 perror("failed timer alarm");
685 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
688 struct sigaction act
;
689 struct itimerval itv
;
691 /* get times() syscall frequency */
692 timer_freq
= sysconf(_SC_CLK_TCK
);
695 sigfillset(&act
.sa_mask
);
697 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
698 act
.sa_flags
|= SA_ONSTACK
;
700 act
.sa_handler
= host_alarm_handler
;
701 sigaction(SIGALRM
, &act
, NULL
);
703 itv
.it_interval
.tv_sec
= 0;
704 itv
.it_interval
.tv_usec
= 1000;
705 itv
.it_value
.tv_sec
= 0;
706 itv
.it_value
.tv_usec
= 10 * 1000;
707 setitimer(ITIMER_REAL
, &itv
, NULL
);
708 /* we probe the tick duration of the kernel to inform the user if
709 the emulated kernel requested a too high timer frequency */
710 getitimer(ITIMER_REAL
, &itv
);
711 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
* PIT_FREQ
) /
717 void quit_timers(void)
720 timeKillEvent(timerID
);
724 /***********************************************************/
729 int serial_open_device(void)
736 int serial_open_device(void)
738 char slave_name
[1024];
739 int master_fd
, slave_fd
;
741 if (serial_console
== NULL
&& nographic
) {
742 /* use console for serial port */
745 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
746 fprintf(stderr
, "warning: could not create pseudo terminal for serial port\n");
749 fprintf(stderr
, "Serial port redirected to %s\n", slave_name
);
756 /***********************************************************/
757 /* Linux network device redirectors */
759 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
763 for(i
=0;i
<size
;i
+=16) {
767 fprintf(f
, "%08x ", i
);
770 fprintf(f
, " %02x", buf
[i
+j
]);
777 if (c
< ' ' || c
> '~')
785 void qemu_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
787 nd
->send_packet(nd
, buf
, size
);
790 void qemu_add_read_packet(NetDriverState
*nd
, IOCanRWHandler
*fd_can_read
,
791 IOReadHandler
*fd_read
, void *opaque
)
793 nd
->add_read_packet(nd
, fd_can_read
, fd_read
, opaque
);
796 /* dummy network adapter */
798 static void dummy_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
802 static void dummy_add_read_packet(NetDriverState
*nd
,
803 IOCanRWHandler
*fd_can_read
,
804 IOReadHandler
*fd_read
, void *opaque
)
808 static int net_dummy_init(NetDriverState
*nd
)
810 nd
->send_packet
= dummy_send_packet
;
811 nd
->add_read_packet
= dummy_add_read_packet
;
812 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "dummy");
816 #if defined(CONFIG_SLIRP)
818 /* slirp network adapter */
820 static void *slirp_fd_opaque
;
821 static IOCanRWHandler
*slirp_fd_can_read
;
822 static IOReadHandler
*slirp_fd_read
;
823 static int slirp_inited
;
825 int slirp_can_output(void)
827 return slirp_fd_can_read(slirp_fd_opaque
);
830 void slirp_output(const uint8_t *pkt
, int pkt_len
)
834 hex_dump(stdout
, pkt
, pkt_len
);
836 slirp_fd_read(slirp_fd_opaque
, pkt
, pkt_len
);
839 static void slirp_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
843 hex_dump(stdout
, buf
, size
);
845 slirp_input(buf
, size
);
848 static void slirp_add_read_packet(NetDriverState
*nd
,
849 IOCanRWHandler
*fd_can_read
,
850 IOReadHandler
*fd_read
, void *opaque
)
852 slirp_fd_opaque
= opaque
;
853 slirp_fd_can_read
= fd_can_read
;
854 slirp_fd_read
= fd_read
;
857 static int net_slirp_init(NetDriverState
*nd
)
863 nd
->send_packet
= slirp_send_packet
;
864 nd
->add_read_packet
= slirp_add_read_packet
;
865 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "slirp");
869 #endif /* CONFIG_SLIRP */
873 static int tun_open(char *ifname
, int ifname_size
)
878 fd
= open("/dev/net/tun", O_RDWR
);
880 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
883 memset(&ifr
, 0, sizeof(ifr
));
884 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
885 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
886 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
888 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
892 printf("Connected to host network interface: %s\n", ifr
.ifr_name
);
893 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
894 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
898 static void tun_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
900 write(nd
->fd
, buf
, size
);
903 static void tun_add_read_packet(NetDriverState
*nd
,
904 IOCanRWHandler
*fd_can_read
,
905 IOReadHandler
*fd_read
, void *opaque
)
907 qemu_add_fd_read_handler(nd
->fd
, fd_can_read
, fd_read
, opaque
);
910 static int net_tun_init(NetDriverState
*nd
)
916 nd
->fd
= tun_open(nd
->ifname
, sizeof(nd
->ifname
));
920 /* try to launch network init script */
925 *parg
++ = network_script
;
926 *parg
++ = nd
->ifname
;
928 execv(network_script
, args
);
931 while (waitpid(pid
, &status
, 0) != pid
);
932 if (!WIFEXITED(status
) ||
933 WEXITSTATUS(status
) != 0) {
934 fprintf(stderr
, "%s: could not launch network script\n",
938 nd
->send_packet
= tun_send_packet
;
939 nd
->add_read_packet
= tun_add_read_packet
;
943 static int net_fd_init(NetDriverState
*nd
, int fd
)
946 nd
->send_packet
= tun_send_packet
;
947 nd
->add_read_packet
= tun_add_read_packet
;
948 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "tunfd");
954 /***********************************************************/
959 static void term_exit(void)
963 static void term_init(void)
969 /* init terminal so that we can grab keys */
970 static struct termios oldtty
;
972 static void term_exit(void)
974 tcsetattr (0, TCSANOW
, &oldtty
);
977 static void term_init(void)
984 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
985 |INLCR
|IGNCR
|ICRNL
|IXON
);
986 tty
.c_oflag
|= OPOST
;
987 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
988 /* if graphical mode, we allow Ctrl-C handling */
990 tty
.c_lflag
&= ~ISIG
;
991 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
996 tcsetattr (0, TCSANOW
, &tty
);
1000 fcntl(0, F_SETFL
, O_NONBLOCK
);
1005 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
1009 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
1013 static void dumb_refresh(DisplayState
*ds
)
1015 vga_update_display();
1018 void dumb_display_init(DisplayState
*ds
)
1023 ds
->dpy_update
= dumb_update
;
1024 ds
->dpy_resize
= dumb_resize
;
1025 ds
->dpy_refresh
= dumb_refresh
;
1028 #if !defined(CONFIG_SOFTMMU)
1029 /***********************************************************/
1030 /* cpu signal handler */
1031 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
1034 if (cpu_signal_handler(host_signum
, info
, puc
))
1041 /***********************************************************/
1044 #define MAX_IO_HANDLERS 64
1046 typedef struct IOHandlerRecord
{
1048 IOCanRWHandler
*fd_can_read
;
1049 IOReadHandler
*fd_read
;
1051 /* temporary data */
1054 struct IOHandlerRecord
*next
;
1057 static IOHandlerRecord
*first_io_handler
;
1059 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
1060 IOReadHandler
*fd_read
, void *opaque
)
1062 IOHandlerRecord
*ioh
;
1064 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1068 ioh
->fd_can_read
= fd_can_read
;
1069 ioh
->fd_read
= fd_read
;
1070 ioh
->opaque
= opaque
;
1071 ioh
->next
= first_io_handler
;
1072 first_io_handler
= ioh
;
1076 void qemu_del_fd_read_handler(int fd
)
1078 IOHandlerRecord
**pioh
, *ioh
;
1080 pioh
= &first_io_handler
;
1085 if (ioh
->fd
== fd
) {
1093 /***********************************************************/
1094 /* savevm/loadvm support */
1096 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
1098 fwrite(buf
, 1, size
, f
);
1101 void qemu_put_byte(QEMUFile
*f
, int v
)
1106 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
1108 qemu_put_byte(f
, v
>> 8);
1109 qemu_put_byte(f
, v
);
1112 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
1114 qemu_put_byte(f
, v
>> 24);
1115 qemu_put_byte(f
, v
>> 16);
1116 qemu_put_byte(f
, v
>> 8);
1117 qemu_put_byte(f
, v
);
1120 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
1122 qemu_put_be32(f
, v
>> 32);
1123 qemu_put_be32(f
, v
);
1126 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
1128 return fread(buf
, 1, size
, f
);
1131 int qemu_get_byte(QEMUFile
*f
)
1141 unsigned int qemu_get_be16(QEMUFile
*f
)
1144 v
= qemu_get_byte(f
) << 8;
1145 v
|= qemu_get_byte(f
);
1149 unsigned int qemu_get_be32(QEMUFile
*f
)
1152 v
= qemu_get_byte(f
) << 24;
1153 v
|= qemu_get_byte(f
) << 16;
1154 v
|= qemu_get_byte(f
) << 8;
1155 v
|= qemu_get_byte(f
);
1159 uint64_t qemu_get_be64(QEMUFile
*f
)
1162 v
= (uint64_t)qemu_get_be32(f
) << 32;
1163 v
|= qemu_get_be32(f
);
1167 int64_t qemu_ftell(QEMUFile
*f
)
1172 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
1174 if (fseek(f
, pos
, whence
) < 0)
1179 typedef struct SaveStateEntry
{
1183 SaveStateHandler
*save_state
;
1184 LoadStateHandler
*load_state
;
1186 struct SaveStateEntry
*next
;
1189 static SaveStateEntry
*first_se
;
1191 int register_savevm(const char *idstr
,
1194 SaveStateHandler
*save_state
,
1195 LoadStateHandler
*load_state
,
1198 SaveStateEntry
*se
, **pse
;
1200 se
= qemu_malloc(sizeof(SaveStateEntry
));
1203 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1204 se
->instance_id
= instance_id
;
1205 se
->version_id
= version_id
;
1206 se
->save_state
= save_state
;
1207 se
->load_state
= load_state
;
1208 se
->opaque
= opaque
;
1211 /* add at the end of list */
1213 while (*pse
!= NULL
)
1214 pse
= &(*pse
)->next
;
1219 #define QEMU_VM_FILE_MAGIC 0x5145564d
1220 #define QEMU_VM_FILE_VERSION 0x00000001
1222 int qemu_savevm(const char *filename
)
1226 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
1228 saved_vm_running
= vm_running
;
1231 f
= fopen(filename
, "wb");
1237 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1238 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1240 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1242 len
= strlen(se
->idstr
);
1243 qemu_put_byte(f
, len
);
1244 qemu_put_buffer(f
, se
->idstr
, len
);
1246 qemu_put_be32(f
, se
->instance_id
);
1247 qemu_put_be32(f
, se
->version_id
);
1249 /* record size: filled later */
1251 qemu_put_be32(f
, 0);
1253 se
->save_state(f
, se
->opaque
);
1255 /* fill record size */
1257 len
= ftell(f
) - len_pos
- 4;
1258 fseek(f
, len_pos
, SEEK_SET
);
1259 qemu_put_be32(f
, len
);
1260 fseek(f
, cur_pos
, SEEK_SET
);
1266 if (saved_vm_running
)
1271 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1275 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1276 if (!strcmp(se
->idstr
, idstr
) &&
1277 instance_id
== se
->instance_id
)
1283 int qemu_loadvm(const char *filename
)
1287 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
1288 int saved_vm_running
;
1292 saved_vm_running
= vm_running
;
1295 f
= fopen(filename
, "rb");
1301 v
= qemu_get_be32(f
);
1302 if (v
!= QEMU_VM_FILE_MAGIC
)
1304 v
= qemu_get_be32(f
);
1305 if (v
!= QEMU_VM_FILE_VERSION
) {
1312 #if defined (DO_TB_FLUSH)
1315 len
= qemu_get_byte(f
);
1318 qemu_get_buffer(f
, idstr
, len
);
1320 instance_id
= qemu_get_be32(f
);
1321 version_id
= qemu_get_be32(f
);
1322 record_len
= qemu_get_be32(f
);
1324 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1325 idstr
, instance_id
, version_id
, record_len
);
1328 se
= find_se(idstr
, instance_id
);
1330 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1331 instance_id
, idstr
);
1333 ret
= se
->load_state(f
, se
->opaque
, version_id
);
1335 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1336 instance_id
, idstr
);
1339 /* always seek to exact end of record */
1340 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
1345 if (saved_vm_running
)
1350 /***********************************************************/
1351 /* cpu save/restore */
1353 #if defined(TARGET_I386)
1355 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
1357 qemu_put_be32(f
, (uint32_t)dt
->base
);
1358 qemu_put_be32(f
, dt
->limit
);
1359 qemu_put_be32(f
, dt
->flags
);
1362 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
1364 dt
->base
= (uint8_t *)qemu_get_be32(f
);
1365 dt
->limit
= qemu_get_be32(f
);
1366 dt
->flags
= qemu_get_be32(f
);
1369 void cpu_save(QEMUFile
*f
, void *opaque
)
1371 CPUState
*env
= opaque
;
1372 uint16_t fptag
, fpus
, fpuc
;
1376 for(i
= 0; i
< 8; i
++)
1377 qemu_put_be32s(f
, &env
->regs
[i
]);
1378 qemu_put_be32s(f
, &env
->eip
);
1379 qemu_put_be32s(f
, &env
->eflags
);
1380 qemu_put_be32s(f
, &env
->eflags
);
1381 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
1382 qemu_put_be32s(f
, &hflags
);
1386 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
1388 for (i
=7; i
>=0; i
--) {
1390 if (env
->fptags
[i
]) {
1395 qemu_put_be16s(f
, &fpuc
);
1396 qemu_put_be16s(f
, &fpus
);
1397 qemu_put_be16s(f
, &fptag
);
1399 for(i
= 0; i
< 8; i
++) {
1402 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
]);
1403 qemu_put_be64(f
, mant
);
1404 qemu_put_be16(f
, exp
);
1407 for(i
= 0; i
< 6; i
++)
1408 cpu_put_seg(f
, &env
->segs
[i
]);
1409 cpu_put_seg(f
, &env
->ldt
);
1410 cpu_put_seg(f
, &env
->tr
);
1411 cpu_put_seg(f
, &env
->gdt
);
1412 cpu_put_seg(f
, &env
->idt
);
1414 qemu_put_be32s(f
, &env
->sysenter_cs
);
1415 qemu_put_be32s(f
, &env
->sysenter_esp
);
1416 qemu_put_be32s(f
, &env
->sysenter_eip
);
1418 qemu_put_be32s(f
, &env
->cr
[0]);
1419 qemu_put_be32s(f
, &env
->cr
[2]);
1420 qemu_put_be32s(f
, &env
->cr
[3]);
1421 qemu_put_be32s(f
, &env
->cr
[4]);
1423 for(i
= 0; i
< 8; i
++)
1424 qemu_put_be32s(f
, &env
->dr
[i
]);
1427 qemu_put_be32s(f
, &env
->a20_mask
);
1430 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1432 CPUState
*env
= opaque
;
1435 uint16_t fpus
, fpuc
, fptag
;
1437 if (version_id
!= 1)
1439 for(i
= 0; i
< 8; i
++)
1440 qemu_get_be32s(f
, &env
->regs
[i
]);
1441 qemu_get_be32s(f
, &env
->eip
);
1442 qemu_get_be32s(f
, &env
->eflags
);
1443 qemu_get_be32s(f
, &env
->eflags
);
1444 qemu_get_be32s(f
, &hflags
);
1446 qemu_get_be16s(f
, &fpuc
);
1447 qemu_get_be16s(f
, &fpus
);
1448 qemu_get_be16s(f
, &fptag
);
1450 for(i
= 0; i
< 8; i
++) {
1453 mant
= qemu_get_be64(f
);
1454 exp
= qemu_get_be16(f
);
1455 env
->fpregs
[i
] = cpu_set_fp80(mant
, exp
);
1459 env
->fpstt
= (fpus
>> 11) & 7;
1460 env
->fpus
= fpus
& ~0x3800;
1461 for(i
= 0; i
< 8; i
++) {
1462 env
->fptags
[i
] = ((fptag
& 3) == 3);
1466 for(i
= 0; i
< 6; i
++)
1467 cpu_get_seg(f
, &env
->segs
[i
]);
1468 cpu_get_seg(f
, &env
->ldt
);
1469 cpu_get_seg(f
, &env
->tr
);
1470 cpu_get_seg(f
, &env
->gdt
);
1471 cpu_get_seg(f
, &env
->idt
);
1473 qemu_get_be32s(f
, &env
->sysenter_cs
);
1474 qemu_get_be32s(f
, &env
->sysenter_esp
);
1475 qemu_get_be32s(f
, &env
->sysenter_eip
);
1477 qemu_get_be32s(f
, &env
->cr
[0]);
1478 qemu_get_be32s(f
, &env
->cr
[2]);
1479 qemu_get_be32s(f
, &env
->cr
[3]);
1480 qemu_get_be32s(f
, &env
->cr
[4]);
1482 for(i
= 0; i
< 8; i
++)
1483 qemu_get_be32s(f
, &env
->dr
[i
]);
1486 qemu_get_be32s(f
, &env
->a20_mask
);
1488 /* XXX: compute hflags from scratch, except for CPL and IIF */
1489 env
->hflags
= hflags
;
1494 #elif defined(TARGET_PPC)
1495 void cpu_save(QEMUFile
*f
, void *opaque
)
1499 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1505 #warning No CPU save/restore functions
1509 /***********************************************************/
1510 /* ram save/restore */
1512 /* we just avoid storing empty pages */
1513 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
1518 for(i
= 1; i
< len
; i
++) {
1522 qemu_put_byte(f
, 1);
1523 qemu_put_byte(f
, v
);
1526 qemu_put_byte(f
, 0);
1527 qemu_put_buffer(f
, buf
, len
);
1530 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
1534 v
= qemu_get_byte(f
);
1537 if (qemu_get_buffer(f
, buf
, len
) != len
)
1541 v
= qemu_get_byte(f
);
1542 memset(buf
, v
, len
);
1550 static void ram_save(QEMUFile
*f
, void *opaque
)
1553 qemu_put_be32(f
, phys_ram_size
);
1554 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1555 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1559 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1563 if (version_id
!= 1)
1565 if (qemu_get_be32(f
) != phys_ram_size
)
1567 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1568 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1575 /***********************************************************/
1576 /* main execution loop */
1578 void gui_update(void *opaque
)
1580 display_state
.dpy_refresh(&display_state
);
1581 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
1584 /* XXX: support several handlers */
1585 VMStopHandler
*vm_stop_cb
;
1586 VMStopHandler
*vm_stop_opaque
;
1588 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1591 vm_stop_opaque
= opaque
;
1595 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1608 void vm_stop(int reason
)
1611 cpu_disable_ticks();
1615 vm_stop_cb(vm_stop_opaque
, reason
);
1624 struct pollfd ufds
[MAX_IO_HANDLERS
+ 1], *pf
;
1625 IOHandlerRecord
*ioh
, *ioh_next
;
1630 CPUState
*env
= global_env
;
1634 ret
= cpu_exec(env
);
1635 if (reset_requested
) {
1636 ret
= EXCP_INTERRUPT
;
1639 if (ret
== EXCP_DEBUG
) {
1640 vm_stop(EXCP_DEBUG
);
1642 /* if hlt instruction, we wait until the next IRQ */
1643 /* XXX: use timeout computed from timers */
1644 if (ret
== EXCP_HLT
)
1657 /* poll any events */
1658 /* XXX: separate device handlers from system ones */
1660 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
1661 if (!ioh
->fd_can_read
) {
1664 pf
->events
= POLLIN
;
1668 max_size
= ioh
->fd_can_read(ioh
->opaque
);
1670 if (max_size
> sizeof(buf
))
1671 max_size
= sizeof(buf
);
1673 pf
->events
= POLLIN
;
1680 ioh
->max_size
= max_size
;
1683 ret
= poll(ufds
, pf
- ufds
, timeout
);
1685 /* XXX: better handling of removal */
1686 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
1687 ioh_next
= ioh
->next
;
1690 if (pf
->revents
& POLLIN
) {
1691 if (ioh
->max_size
== 0) {
1692 /* just a read event */
1693 ioh
->fd_read(ioh
->opaque
, NULL
, 0);
1695 n
= read(ioh
->fd
, buf
, ioh
->max_size
);
1697 ioh
->fd_read(ioh
->opaque
, buf
, n
);
1698 } else if (errno
!= -EAGAIN
) {
1699 ioh
->fd_read(ioh
->opaque
, NULL
, -errno
);
1707 #if defined(CONFIG_SLIRP)
1708 /* XXX: merge with poll() */
1710 fd_set rfds
, wfds
, xfds
;
1718 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1721 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1723 slirp_select_poll(&rfds
, &wfds
, &xfds
);
1731 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
1732 qemu_get_clock(vm_clock
));
1734 /* XXX: add explicit timer */
1737 /* run dma transfers, if any */
1741 /* real time timers */
1742 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
1743 qemu_get_clock(rt_clock
));
1745 cpu_disable_ticks();
1751 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003 Fabrice Bellard\n"
1752 "usage: %s [options] [disk_image]\n"
1754 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1756 "Standard options:\n"
1757 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1758 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1759 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1760 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1761 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1762 "-snapshot write to temporary files instead of disk image files\n"
1763 "-m megs set virtual RAM size to megs MB\n"
1764 "-nographic disable graphical output and redirect serial I/Os to console\n"
1766 "Network options:\n"
1767 "-nics n simulate 'n' network cards [default=1]\n"
1768 "-macaddr addr set the mac address of the first interface\n"
1769 "-n script set tap/tun network init script [default=%s]\n"
1770 "-tun-fd fd use this fd as already opened tap/tun interface\n"
1772 "-user-net use user mode network stack [default if no tap/tun script]\n"
1774 "-dummy-net use dummy network stack\n"
1776 "Linux boot specific:\n"
1777 "-kernel bzImage use 'bzImage' as kernel image\n"
1778 "-append cmdline use 'cmdline' as kernel command line\n"
1779 "-initrd file use 'file' as initial ram disk\n"
1781 "Debug/Expert options:\n"
1782 "-s wait gdb connection to port %d\n"
1783 "-p port change gdb connection port\n"
1784 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1785 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1786 "-L path set the directory for the BIOS and VGA BIOS\n"
1787 #ifdef USE_CODE_COPY
1788 "-no-code-copy disable code copy acceleration\n"
1792 "During emulation, use C-a h to get terminal commands:\n",
1793 #ifdef CONFIG_SOFTMMU
1798 DEFAULT_NETWORK_SCRIPT
,
1799 DEFAULT_GDBSTUB_PORT
,
1802 #ifndef CONFIG_SOFTMMU
1804 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1805 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1811 struct option long_options
[] = {
1812 { "initrd", 1, NULL
, 0, },
1813 { "hda", 1, NULL
, 0, },
1814 { "hdb", 1, NULL
, 0, },
1815 { "snapshot", 0, NULL
, 0, },
1816 { "hdachs", 1, NULL
, 0, },
1817 { "nographic", 0, NULL
, 0, },
1818 { "kernel", 1, NULL
, 0, },
1819 { "append", 1, NULL
, 0, },
1820 { "tun-fd", 1, NULL
, 0, },
1821 { "hdc", 1, NULL
, 0, },
1822 { "hdd", 1, NULL
, 0, },
1823 { "cdrom", 1, NULL
, 0, },
1824 { "boot", 1, NULL
, 0, },
1825 { "fda", 1, NULL
, 0, },
1826 { "fdb", 1, NULL
, 0, },
1827 { "no-code-copy", 0, NULL
, 0 },
1828 { "nics", 1, NULL
, 0 },
1829 { "macaddr", 1, NULL
, 0 },
1830 { "user-net", 1, NULL
, 0 },
1831 { "dummy-net", 1, NULL
, 0 },
1832 { NULL
, 0, NULL
, 0 },
1835 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1837 /* this stack is only used during signal handling */
1838 #define SIGNAL_STACK_SIZE 32768
1840 static uint8_t *signal_stack
;
1844 #define NET_IF_TUN 0
1845 #define NET_IF_USER 1
1846 #define NET_IF_DUMMY 2
1848 int main(int argc
, char **argv
)
1850 #ifdef CONFIG_GDBSTUB
1851 int use_gdbstub
, gdbstub_port
;
1853 int c
, i
, long_index
, has_cdrom
;
1854 int snapshot
, linux_boot
;
1856 const char *initrd_filename
;
1857 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
1858 const char *kernel_filename
, *kernel_cmdline
;
1859 DisplayState
*ds
= &display_state
;
1860 int cyls
, heads
, secs
;
1861 int start_emulation
= 1;
1863 int net_if_type
, nb_tun_fds
, tun_fds
[MAX_NICS
];
1865 #if !defined(CONFIG_SOFTMMU)
1866 /* we never want that malloc() uses mmap() */
1867 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
1869 initrd_filename
= NULL
;
1870 for(i
= 0; i
< MAX_FD
; i
++)
1871 fd_filename
[i
] = NULL
;
1872 for(i
= 0; i
< MAX_DISKS
; i
++)
1873 hd_filename
[i
] = NULL
;
1874 ram_size
= 32 * 1024 * 1024;
1875 vga_ram_size
= VGA_RAM_SIZE
;
1876 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
1877 #ifdef CONFIG_GDBSTUB
1879 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
1883 kernel_filename
= NULL
;
1884 kernel_cmdline
= "";
1886 cyls
= heads
= secs
= 0;
1891 /* default mac address of the first network interface */
1901 c
= getopt_long_only(argc
, argv
, "hm:d:n:sp:L:S", long_options
, &long_index
);
1906 switch(long_index
) {
1908 initrd_filename
= optarg
;
1911 hd_filename
[0] = optarg
;
1914 hd_filename
[1] = optarg
;
1923 cyls
= strtol(p
, (char **)&p
, 0);
1927 heads
= strtol(p
, (char **)&p
, 0);
1931 secs
= strtol(p
, (char **)&p
, 0);
1942 kernel_filename
= optarg
;
1945 kernel_cmdline
= optarg
;
1951 if (nb_tun_fds
< MAX_NICS
) {
1952 fd
= strtol(optarg
, (char **)&p
, 0);
1954 fprintf(stderr
, "qemu: invalid fd for network interface %d\n", nb_tun_fds
);
1957 tun_fds
[nb_tun_fds
++] = fd
;
1962 hd_filename
[2] = optarg
;
1966 hd_filename
[3] = optarg
;
1969 hd_filename
[2] = optarg
;
1973 boot_device
= optarg
[0];
1974 if (boot_device
!= 'a' && boot_device
!= 'b' &&
1975 boot_device
!= 'c' && boot_device
!= 'd') {
1976 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
1981 fd_filename
[0] = optarg
;
1984 fd_filename
[1] = optarg
;
1987 code_copy_enabled
= 0;
1990 nb_nics
= atoi(optarg
);
1991 if (nb_nics
< 1 || nb_nics
> MAX_NICS
) {
1992 fprintf(stderr
, "qemu: invalid number of network interfaces\n");
2001 for(i
= 0; i
< 6; i
++) {
2002 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2009 fprintf(stderr
, "qemu: invalid syntax for ethernet address\n");
2018 net_if_type
= NET_IF_USER
;
2021 net_if_type
= NET_IF_DUMMY
;
2029 ram_size
= atoi(optarg
) * 1024 * 1024;
2032 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
2033 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
2034 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
2043 mask
= cpu_str_to_log_mask(optarg
);
2045 printf("Log items (comma separated):\n");
2046 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
2047 printf("%-10s %s\n", item
->name
, item
->help
);
2055 pstrcpy(network_script
, sizeof(network_script
), optarg
);
2057 #ifdef CONFIG_GDBSTUB
2062 gdbstub_port
= atoi(optarg
);
2069 start_emulation
= 0;
2074 if (optind
< argc
) {
2075 hd_filename
[0] = argv
[optind
++];
2078 linux_boot
= (kernel_filename
!= NULL
);
2080 if (!linux_boot
&& hd_filename
[0] == '\0' && hd_filename
[2] == '\0' &&
2081 fd_filename
[0] == '\0')
2084 /* boot to cd by default if no hard disk */
2085 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
2086 if (fd_filename
[0] != '\0')
2092 #if !defined(CONFIG_SOFTMMU)
2093 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2095 static uint8_t stdout_buf
[4096];
2096 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
2099 setvbuf(stdout
, NULL
, _IOLBF
, 0);
2102 /* init host network redirectors */
2103 if (net_if_type
== -1) {
2104 net_if_type
= NET_IF_TUN
;
2105 #if defined(CONFIG_SLIRP)
2106 if (access(network_script
, R_OK
) < 0) {
2107 net_if_type
= NET_IF_USER
;
2112 for(i
= 0; i
< nb_nics
; i
++) {
2113 NetDriverState
*nd
= &nd_table
[i
];
2115 /* init virtual mac address */
2116 nd
->macaddr
[0] = macaddr
[0];
2117 nd
->macaddr
[1] = macaddr
[1];
2118 nd
->macaddr
[2] = macaddr
[2];
2119 nd
->macaddr
[3] = macaddr
[3];
2120 nd
->macaddr
[4] = macaddr
[4];
2121 nd
->macaddr
[5] = macaddr
[5] + i
;
2122 switch(net_if_type
) {
2123 #if defined(CONFIG_SLIRP)
2128 #if !defined(_WIN32)
2130 if (i
< nb_tun_fds
) {
2131 net_fd_init(nd
, tun_fds
[i
]);
2144 /* init the memory */
2145 phys_ram_size
= ram_size
+ vga_ram_size
;
2147 #ifdef CONFIG_SOFTMMU
2148 phys_ram_base
= memalign(TARGET_PAGE_SIZE
, phys_ram_size
);
2149 if (!phys_ram_base
) {
2150 fprintf(stderr
, "Could not allocate physical memory\n");
2154 /* as we must map the same page at several addresses, we must use
2159 tmpdir
= getenv("QEMU_TMPDIR");
2162 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
2163 if (mkstemp(phys_ram_file
) < 0) {
2164 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
2168 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
2169 if (phys_ram_fd
< 0) {
2170 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
2174 ftruncate(phys_ram_fd
, phys_ram_size
);
2175 unlink(phys_ram_file
);
2176 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
2178 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
2180 if (phys_ram_base
== MAP_FAILED
) {
2181 fprintf(stderr
, "Could not map physical memory\n");
2187 /* we always create the cdrom drive, even if no disk is there */
2189 bs_table
[2] = bdrv_new("cdrom");
2190 bdrv_set_type_hint(bs_table
[2], BDRV_TYPE_CDROM
);
2193 /* open the virtual block devices */
2194 for(i
= 0; i
< MAX_DISKS
; i
++) {
2195 if (hd_filename
[i
]) {
2198 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
2199 bs_table
[i
] = bdrv_new(buf
);
2201 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
2202 fprintf(stderr
, "qemu: could not open hard disk image '%s\n",
2206 if (i
== 0 && cyls
!= 0)
2207 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
2211 /* we always create at least one floppy disk */
2212 fd_table
[0] = bdrv_new("fda");
2213 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
2215 for(i
= 0; i
< MAX_FD
; i
++) {
2216 if (fd_filename
[i
]) {
2219 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
2220 fd_table
[i
] = bdrv_new(buf
);
2221 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
2223 if (fd_filename
[i
] != '\0') {
2224 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
2225 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
2233 /* init CPU state */
2236 cpu_single_env
= env
;
2238 register_savevm("timer", 0, 1, timer_save
, timer_load
, env
);
2239 register_savevm("cpu", 0, 1, cpu_save
, cpu_load
, env
);
2240 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
2243 cpu_calibrate_ticks();
2247 dumb_display_init(ds
);
2250 sdl_display_init(ds
);
2252 dumb_display_init(ds
);
2256 /* setup cpu signal handlers for MMU / self modifying code handling */
2257 #if !defined(CONFIG_SOFTMMU)
2259 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2262 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
2263 stk
.ss_sp
= signal_stack
;
2264 stk
.ss_size
= SIGNAL_STACK_SIZE
;
2267 if (sigaltstack(&stk
, NULL
) < 0) {
2268 perror("sigaltstack");
2274 struct sigaction act
;
2276 sigfillset(&act
.sa_mask
);
2277 act
.sa_flags
= SA_SIGINFO
;
2278 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2279 act
.sa_flags
|= SA_ONSTACK
;
2281 act
.sa_sigaction
= host_segv_handler
;
2282 sigaction(SIGSEGV
, &act
, NULL
);
2283 sigaction(SIGBUS
, &act
, NULL
);
2284 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2285 sigaction(SIGFPE
, &act
, NULL
);
2292 struct sigaction act
;
2293 sigfillset(&act
.sa_mask
);
2295 act
.sa_handler
= SIG_IGN
;
2296 sigaction(SIGPIPE
, &act
, NULL
);
2301 #if defined(TARGET_I386)
2302 pc_init(ram_size
, vga_ram_size
, boot_device
,
2303 ds
, fd_filename
, snapshot
,
2304 kernel_filename
, kernel_cmdline
, initrd_filename
);
2305 #elif defined(TARGET_PPC)
2306 ppc_init(ram_size
, vga_ram_size
, boot_device
,
2307 ds
, fd_filename
, snapshot
,
2308 kernel_filename
, kernel_cmdline
, initrd_filename
);
2311 /* launched after the device init so that it can display or not a
2315 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
2316 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
2318 #ifdef CONFIG_GDBSTUB
2320 if (gdbserver_start(gdbstub_port
) < 0) {
2321 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
2325 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
2329 if (start_emulation
)