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
43 #include <sys/ioctl.h>
44 #include <sys/socket.h>
46 #include <linux/if_tun.h>
53 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
55 //#define DEBUG_UNUSED_IOPORT
57 #if !defined(CONFIG_SOFTMMU)
58 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
60 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
63 #if defined (TARGET_I386)
64 #elif defined (TARGET_PPC)
65 //#define USE_OPEN_FIRMWARE
66 #if !defined (USE_OPEN_FIRMWARE)
67 #define KERNEL_LOAD_ADDR 0x01000000
68 #define KERNEL_STACK_ADDR 0x01200000
70 #define KERNEL_LOAD_ADDR 0x00000000
71 #define KERNEL_STACK_ADDR 0x00400000
75 #define GUI_REFRESH_INTERVAL 30
77 /* XXX: use a two level table to limit memory usage */
78 #define MAX_IOPORTS 65536
80 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
81 char phys_ram_file
[1024];
83 CPUState
*cpu_single_env
;
84 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
85 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
86 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
88 static DisplayState display_state
;
91 int64_t ticks_per_sec
;
92 int boot_device
= 'c';
94 static char network_script
[1024];
95 int pit_min_timer_count
= 0;
97 /***********************************************************/
100 uint32_t default_ioport_readb(CPUState
*env
, uint32_t address
)
102 #ifdef DEBUG_UNUSED_IOPORT
103 fprintf(stderr
, "inb: port=0x%04x\n", address
);
108 void default_ioport_writeb(CPUState
*env
, uint32_t address
, uint32_t data
)
110 #ifdef DEBUG_UNUSED_IOPORT
111 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
115 /* default is to make two byte accesses */
116 uint32_t default_ioport_readw(CPUState
*env
, uint32_t address
)
119 data
= ioport_read_table
[0][address
& (MAX_IOPORTS
- 1)](env
, address
);
120 data
|= ioport_read_table
[0][(address
+ 1) & (MAX_IOPORTS
- 1)](env
, address
+ 1) << 8;
124 void default_ioport_writew(CPUState
*env
, uint32_t address
, uint32_t data
)
126 ioport_write_table
[0][address
& (MAX_IOPORTS
- 1)](env
, address
, data
& 0xff);
127 ioport_write_table
[0][(address
+ 1) & (MAX_IOPORTS
- 1)](env
, address
+ 1, (data
>> 8) & 0xff);
130 uint32_t default_ioport_readl(CPUState
*env
, uint32_t address
)
132 #ifdef DEBUG_UNUSED_IOPORT
133 fprintf(stderr
, "inl: port=0x%04x\n", address
);
138 void default_ioport_writel(CPUState
*env
, uint32_t address
, uint32_t data
)
140 #ifdef DEBUG_UNUSED_IOPORT
141 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
145 void init_ioports(void)
149 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
150 ioport_read_table
[0][i
] = default_ioport_readb
;
151 ioport_write_table
[0][i
] = default_ioport_writeb
;
152 ioport_read_table
[1][i
] = default_ioport_readw
;
153 ioport_write_table
[1][i
] = default_ioport_writew
;
154 ioport_read_table
[2][i
] = default_ioport_readl
;
155 ioport_write_table
[2][i
] = default_ioport_writel
;
159 /* size is the word size in byte */
160 int register_ioport_read(int start
, int length
, IOPortReadFunc
*func
, int size
)
172 for(i
= start
; i
< start
+ length
; i
+= size
)
173 ioport_read_table
[bsize
][i
] = func
;
177 /* size is the word size in byte */
178 int register_ioport_write(int start
, int length
, IOPortWriteFunc
*func
, int size
)
190 for(i
= start
; i
< start
+ length
; i
+= size
)
191 ioport_write_table
[bsize
][i
] = func
;
195 void pstrcpy(char *buf
, int buf_size
, const char *str
)
205 if (c
== 0 || q
>= buf
+ buf_size
- 1)
212 /* strcat and truncate. */
213 char *pstrcat(char *buf
, int buf_size
, const char *s
)
218 pstrcpy(buf
+ len
, buf_size
- len
, s
);
222 /* return the size or -1 if error */
223 int load_image(const char *filename
, uint8_t *addr
)
226 fd
= open(filename
, O_RDONLY
);
229 size
= lseek(fd
, 0, SEEK_END
);
230 lseek(fd
, 0, SEEK_SET
);
231 if (read(fd
, addr
, size
) != size
) {
239 void cpu_outb(CPUState
*env
, int addr
, int val
)
241 ioport_write_table
[0][addr
& (MAX_IOPORTS
- 1)](env
, addr
, val
);
244 void cpu_outw(CPUState
*env
, int addr
, int val
)
246 ioport_write_table
[1][addr
& (MAX_IOPORTS
- 1)](env
, addr
, val
);
249 void cpu_outl(CPUState
*env
, int addr
, int val
)
251 ioport_write_table
[2][addr
& (MAX_IOPORTS
- 1)](env
, addr
, val
);
254 int cpu_inb(CPUState
*env
, int addr
)
256 return ioport_read_table
[0][addr
& (MAX_IOPORTS
- 1)](env
, addr
);
259 int cpu_inw(CPUState
*env
, int addr
)
261 return ioport_read_table
[1][addr
& (MAX_IOPORTS
- 1)](env
, addr
);
264 int cpu_inl(CPUState
*env
, int addr
)
266 return ioport_read_table
[2][addr
& (MAX_IOPORTS
- 1)](env
, addr
);
269 /***********************************************************/
270 void hw_error(const char *fmt
, ...)
275 fprintf(stderr
, "qemu: hardware error: ");
276 vfprintf(stderr
, fmt
, ap
);
277 fprintf(stderr
, "\n");
279 cpu_x86_dump_state(global_env
, stderr
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
281 cpu_dump_state(global_env
, stderr
, 0);
287 #if defined(__powerpc__)
289 static inline uint32_t get_tbl(void)
292 asm volatile("mftb %0" : "=r" (tbl
));
296 static inline uint32_t get_tbu(void)
299 asm volatile("mftbu %0" : "=r" (tbl
));
303 int64_t cpu_get_real_ticks(void)
306 /* NOTE: we test if wrapping has occurred */
312 return ((int64_t)h
<< 32) | l
;
315 #elif defined(__i386__)
317 int64_t cpu_get_real_ticks(void)
320 asm("rdtsc" : "=A" (val
));
325 #error unsupported CPU
328 static int64_t cpu_ticks_offset
;
329 static int64_t cpu_ticks_last
;
331 int64_t cpu_get_ticks(void)
333 return cpu_get_real_ticks() + cpu_ticks_offset
;
336 /* enable cpu_get_ticks() */
337 void cpu_enable_ticks(void)
339 cpu_ticks_offset
= cpu_ticks_last
- cpu_get_real_ticks();
342 /* disable cpu_get_ticks() : the clock is stopped. You must not call
343 cpu_get_ticks() after that. */
344 void cpu_disable_ticks(void)
346 cpu_ticks_last
= cpu_get_ticks();
349 int64_t get_clock(void)
352 gettimeofday(&tv
, NULL
);
353 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
356 void cpu_calibrate_ticks(void)
361 ticks
= cpu_get_ticks();
363 usec
= get_clock() - usec
;
364 ticks
= cpu_get_ticks() - ticks
;
365 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
368 /* compute with 96 bit intermediate result: (a*b)/c */
369 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
374 #ifdef WORDS_BIGENDIAN
384 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
385 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
388 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
392 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
393 static int term_got_escape
, term_command
;
394 static unsigned char term_cmd_buf
[128];
396 typedef struct term_cmd_t
{
397 const unsigned char *name
;
398 void (*handler
)(unsigned char *params
);
401 static void do_change_cdrom (unsigned char *params
);
402 static void do_change_fd0 (unsigned char *params
);
403 static void do_change_fd1 (unsigned char *params
);
405 static term_cmd_t term_cmds
[] = {
406 { "changecd", &do_change_cdrom
, },
407 { "changefd0", &do_change_fd0
, },
408 { "changefd1", &do_change_fd1
, },
412 void term_print_help(void)
415 "C-a h print this help\n"
416 "C-a x exit emulatior\n"
417 "C-a d switch on/off debug log\n"
418 "C-a s save disk data back to file (if -snapshot)\n"
419 "C-a b send break (magic sysrq)\n"
420 "C-a c send qemu internal command\n"
425 static void do_change_cdrom (unsigned char *params
)
427 /* Dunno how to do it... */
430 static void do_change_fd (int fd
, unsigned char *params
)
432 unsigned char *name_start
, *name_end
, *ros
;
435 for (name_start
= params
;
436 isspace(*name_start
); name_start
++)
438 if (*name_start
== '\0')
440 for (name_end
= name_start
;
441 !isspace(*name_end
) && *name_end
!= '\0'; name_end
++)
443 for (ros
= name_end
+ 1; isspace(*ros
); ros
++)
445 if (ros
[0] == 'r' && ros
[1] == 'o')
450 printf("Change fd %d to %s (%s)\n", fd
, name_start
, params
);
451 fdctrl_disk_change(fd
, name_start
, ro
);
454 static void do_change_fd0 (unsigned char *params
)
456 do_change_fd(0, params
);
459 static void do_change_fd1 (unsigned char *params
)
461 do_change_fd(1, params
);
464 static void term_treat_command(void)
466 unsigned char *cmd_start
, *cmd_end
;
469 for (cmd_start
= term_cmd_buf
; isspace(*cmd_start
); cmd_start
++)
471 for (cmd_end
= cmd_start
;
472 !isspace(*cmd_end
) && *cmd_end
!= '\0'; cmd_end
++)
474 for (i
= 0; term_cmds
[i
].name
!= NULL
; i
++) {
475 if (strlen(term_cmds
[i
].name
) == (cmd_end
- cmd_start
) &&
476 memcmp(term_cmds
[i
].name
, cmd_start
, cmd_end
- cmd_start
) == 0) {
477 (*term_cmds
[i
].handler
)(cmd_end
+ 1);
482 printf("Unknown term command: %s\n", cmd_start
);
485 extern FILE *logfile
;
487 /* called when a char is received */
488 void term_received_byte(int ch
)
491 if (ch
== '\n' || ch
== '\r' || term_command
== 127) {
493 term_treat_command();
496 if (ch
== 0x7F || ch
== 0x08) {
497 if (term_command
> 1) {
498 term_cmd_buf
[--term_command
- 1] = '\0';
501 printf("\r> %s", term_cmd_buf
);
503 } else if (ch
> 0x1f) {
504 term_cmd_buf
[term_command
++ - 1] = ch
;
505 term_cmd_buf
[term_command
- 1] = '\0';
506 printf("\r> %s", term_cmd_buf
);
510 } else if (term_got_escape
) {
522 for (i
= 0; i
< MAX_DISKS
; i
++) {
524 bdrv_commit(bs_table
[i
]);
529 serial_receive_break();
537 cpu_set_log(CPU_LOG_ALL
);
542 } else if (ch
== TERM_ESCAPE
) {
546 serial_receive_byte(ch
);
550 /***********************************************************/
551 /* Linux network device redirector */
556 int fd
, ret
, pid
, status
;
558 fd
= open("/dev/net/tun", O_RDWR
);
560 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
563 memset(&ifr
, 0, sizeof(ifr
));
564 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
565 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
566 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
568 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
572 printf("Connected to host network interface: %s\n", ifr
.ifr_name
);
573 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
576 /* try to launch network init script */
580 execl(network_script
, network_script
, ifr
.ifr_name
, NULL
);
583 while (waitpid(pid
, &status
, 0) != pid
);
584 if (!WIFEXITED(status
) ||
585 WEXITSTATUS(status
) != 0) {
586 fprintf(stderr
, "%s: could not launch network script for '%s'\n",
587 network_script
, ifr
.ifr_name
);
593 void net_send_packet(int net_fd
, const uint8_t *buf
, int size
)
596 printf("NE2000: sending packet size=%d\n", size
);
598 write(net_fd
, buf
, size
);
601 /***********************************************************/
604 /* init terminal so that we can grab keys */
605 static struct termios oldtty
;
607 static void term_exit(void)
609 tcsetattr (0, TCSANOW
, &oldtty
);
612 static void term_init(void)
619 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
620 |INLCR
|IGNCR
|ICRNL
|IXON
);
621 tty
.c_oflag
|= OPOST
;
622 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
623 /* if graphical mode, we allow Ctrl-C handling */
625 tty
.c_lflag
&= ~ISIG
;
626 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
631 tcsetattr (0, TCSANOW
, &tty
);
635 fcntl(0, F_SETFL
, O_NONBLOCK
);
638 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
642 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
646 static void dumb_refresh(DisplayState
*ds
)
648 vga_update_display();
651 void dumb_display_init(DisplayState
*ds
)
656 ds
->dpy_update
= dumb_update
;
657 ds
->dpy_resize
= dumb_resize
;
658 ds
->dpy_refresh
= dumb_refresh
;
661 #if !defined(CONFIG_SOFTMMU)
662 /***********************************************************/
663 /* cpu signal handler */
664 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
667 if (cpu_signal_handler(host_signum
, info
, puc
))
674 static int timer_irq_pending
;
675 static int timer_irq_count
;
678 static int gui_refresh_pending
, gui_refresh_count
;
680 static void host_alarm_handler(int host_signum
, siginfo_t
*info
,
683 /* NOTE: since usually the OS asks a 100 Hz clock, there can be
684 some drift between cpu_get_ticks() and the interrupt time. So
685 we queue some interrupts to avoid missing some */
686 timer_irq_count
+= pit_get_out_edges(&pit_channels
[0]);
687 if (timer_irq_count
) {
688 if (timer_irq_count
> 2)
691 timer_irq_pending
= 1;
693 gui_refresh_count
+= timer_ms
;
694 if (gui_refresh_count
>= GUI_REFRESH_INTERVAL
) {
695 gui_refresh_count
= 0;
696 gui_refresh_pending
= 1;
699 if (gui_refresh_pending
|| timer_irq_pending
) {
700 /* just exit from the cpu to have a chance to handle timers */
701 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
705 /* main execution loop */
707 CPUState
*cpu_gdbstub_get_env(void *opaque
)
712 int main_loop(void *opaque
)
714 struct pollfd ufds
[3], *pf
, *serial_ufd
, *gdb_ufd
;
715 #if defined (TARGET_I386)
716 struct pollfd
*net_ufd
;
718 int ret
, n
, timeout
, serial_ok
;
720 CPUState
*env
= global_env
;
723 /* initialize terminal only there so that the user has a
724 chance to stop QEMU with Ctrl-C before the gdb connection
733 #if defined (DO_TB_FLUSH)
737 if (reset_requested
) {
738 ret
= EXCP_INTERRUPT
;
741 if (ret
== EXCP_DEBUG
) {
745 /* if hlt instruction, we wait until the next IRQ */
750 /* poll any events */
753 if (serial_ok
&& serial_can_receive()) {
759 #if defined (TARGET_I386)
761 if (net_fd
> 0 && ne2000_can_receive()) {
769 if (gdbstub_fd
> 0) {
776 ret
= poll(ufds
, pf
- ufds
, timeout
);
778 if (serial_ufd
&& (serial_ufd
->revents
& POLLIN
)) {
781 term_received_byte(ch
);
783 /* Closed, stop polling. */
787 #if defined (TARGET_I386)
788 if (net_ufd
&& (net_ufd
->revents
& POLLIN
)) {
789 uint8_t buf
[MAX_ETH_FRAME_SIZE
];
791 n
= read(net_fd
, buf
, MAX_ETH_FRAME_SIZE
);
794 memset(buf
+ n
, 0, 60 - n
);
797 ne2000_receive(buf
, n
);
801 if (gdb_ufd
&& (gdb_ufd
->revents
& POLLIN
)) {
803 /* stop emulation if requested by gdb */
804 n
= read(gdbstub_fd
, buf
, 1);
806 ret
= EXCP_INTERRUPT
;
813 if (timer_irq_pending
) {
814 #if defined (TARGET_I386)
817 timer_irq_pending
= 0;
821 /* XXX: add explicit timer */
824 /* run dma transfers, if any */
828 if (gui_refresh_pending
) {
829 display_state
.dpy_refresh(&display_state
);
830 gui_refresh_pending
= 0;
839 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003 Fabrice Bellard\n"
840 "usage: %s [options] [disk_image]\n"
842 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
844 "Standard options:\n"
845 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
846 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
847 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
848 "-cdrom file use 'file' as IDE cdrom 2 image\n"
849 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
850 "-snapshot write to temporary files instead of disk image files\n"
851 "-m megs set virtual RAM size to megs MB\n"
852 "-n script set network init script [default=%s]\n"
853 "-tun-fd fd this fd talks to tap/tun, use it.\n"
854 "-nographic disable graphical output\n"
856 "Linux boot specific (does not require PC BIOS):\n"
857 "-kernel bzImage use 'bzImage' as kernel image\n"
858 "-append cmdline use 'cmdline' as kernel command line\n"
859 "-initrd file use 'file' as initial ram disk\n"
861 "Debug/Expert options:\n"
862 "-s wait gdb connection to port %d\n"
863 "-p port change gdb connection port\n"
864 "-d output log to %s\n"
865 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
866 "-L path set the directory for the BIOS and VGA BIOS\n"
868 "-no-code-copy disable code copy acceleration\n"
872 "During emulation, use C-a h to get terminal commands:\n",
873 #ifdef CONFIG_SOFTMMU
878 DEFAULT_NETWORK_SCRIPT
,
879 DEFAULT_GDBSTUB_PORT
,
882 #ifndef CONFIG_SOFTMMU
884 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
885 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
891 struct option long_options
[] = {
892 { "initrd", 1, NULL
, 0, },
893 { "hda", 1, NULL
, 0, },
894 { "hdb", 1, NULL
, 0, },
895 { "snapshot", 0, NULL
, 0, },
896 { "hdachs", 1, NULL
, 0, },
897 { "nographic", 0, NULL
, 0, },
898 { "kernel", 1, NULL
, 0, },
899 { "append", 1, NULL
, 0, },
900 { "tun-fd", 1, NULL
, 0, },
901 { "hdc", 1, NULL
, 0, },
902 { "hdd", 1, NULL
, 0, },
903 { "cdrom", 1, NULL
, 0, },
904 { "boot", 1, NULL
, 0, },
905 { "fda", 1, NULL
, 0, },
906 { "fdb", 1, NULL
, 0, },
907 { "no-code-copy", 0, NULL
, 0},
908 { NULL
, 0, NULL
, 0 },
912 /* SDL use the pthreads and they modify sigaction. We don't
914 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
915 extern void __libc_sigaction();
916 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
918 extern void __sigaction();
919 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
921 #endif /* CONFIG_SDL */
923 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
925 /* this stack is only used during signal handling */
926 #define SIGNAL_STACK_SIZE 32768
928 static uint8_t *signal_stack
;
932 int main(int argc
, char **argv
)
934 int c
, i
, use_gdbstub
, gdbstub_port
, long_index
;
935 int snapshot
, linux_boot
;
936 struct sigaction act
;
937 struct itimerval itv
;
939 const char *initrd_filename
;
940 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
941 const char *kernel_filename
, *kernel_cmdline
;
942 DisplayState
*ds
= &display_state
;
944 /* we never want that malloc() uses mmap() */
945 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
946 initrd_filename
= NULL
;
947 for(i
= 0; i
< MAX_FD
; i
++)
948 fd_filename
[i
] = NULL
;
949 for(i
= 0; i
< MAX_DISKS
; i
++)
950 hd_filename
[i
] = NULL
;
951 ram_size
= 32 * 1024 * 1024;
952 vga_ram_size
= VGA_RAM_SIZE
;
953 #if defined (TARGET_I386)
954 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
957 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
960 kernel_filename
= NULL
;
963 c
= getopt_long_only(argc
, argv
, "hm:dn:sp:L:", long_options
, &long_index
);
970 initrd_filename
= optarg
;
973 hd_filename
[0] = optarg
;
976 hd_filename
[1] = optarg
;
983 int cyls
, heads
, secs
;
986 cyls
= strtol(p
, (char **)&p
, 0);
990 heads
= strtol(p
, (char **)&p
, 0);
994 secs
= strtol(p
, (char **)&p
, 0);
997 ide_set_geometry(0, cyls
, heads
, secs
);
1005 kernel_filename
= optarg
;
1008 kernel_cmdline
= optarg
;
1010 #if defined (TARGET_I386)
1012 net_fd
= atoi(optarg
);
1016 hd_filename
[2] = optarg
;
1019 hd_filename
[3] = optarg
;
1022 hd_filename
[2] = optarg
;
1023 ide_set_cdrom(2, 1);
1026 boot_device
= optarg
[0];
1027 if (boot_device
!= 'a' && boot_device
!= 'b' &&
1028 boot_device
!= 'c' && boot_device
!= 'd') {
1029 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
1034 fd_filename
[0] = optarg
;
1037 fd_filename
[1] = optarg
;
1040 code_copy_enabled
= 0;
1048 ram_size
= atoi(optarg
) * 1024 * 1024;
1051 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
1052 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
1053 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
1058 cpu_set_log(CPU_LOG_ALL
);
1060 #if defined (TARGET_I386)
1062 pstrcpy(network_script
, sizeof(network_script
), optarg
);
1069 gdbstub_port
= atoi(optarg
);
1077 if (optind
< argc
) {
1078 hd_filename
[0] = argv
[optind
++];
1081 linux_boot
= (kernel_filename
!= NULL
);
1083 if (!linux_boot
&& hd_filename
[0] == '\0' && hd_filename
[2] == '\0' &&
1084 fd_filename
[0] == '\0')
1087 /* boot to cd by default if no hard disk */
1088 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
1089 if (fd_filename
[0] != '\0')
1095 #if !defined(CONFIG_SOFTMMU)
1096 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1098 static uint8_t stdout_buf
[4096];
1099 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
1102 setvbuf(stdout
, NULL
, _IOLBF
, 0);
1105 /* init network tun interface */
1106 #if defined (TARGET_I386)
1111 /* init the memory */
1112 phys_ram_size
= ram_size
+ vga_ram_size
;
1114 #ifdef CONFIG_SOFTMMU
1115 phys_ram_base
= memalign(TARGET_PAGE_SIZE
, phys_ram_size
);
1116 if (!phys_ram_base
) {
1117 fprintf(stderr
, "Could not allocate physical memory\n");
1121 /* as we must map the same page at several addresses, we must use
1126 tmpdir
= getenv("QEMU_TMPDIR");
1129 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
1130 if (mkstemp(phys_ram_file
) < 0) {
1131 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
1135 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
1136 if (phys_ram_fd
< 0) {
1137 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
1141 ftruncate(phys_ram_fd
, phys_ram_size
);
1142 unlink(phys_ram_file
);
1143 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
1145 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
1147 if (phys_ram_base
== MAP_FAILED
) {
1148 fprintf(stderr
, "Could not map physical memory\n");
1154 /* open the virtual block devices */
1155 for(i
= 0; i
< MAX_DISKS
; i
++) {
1156 if (hd_filename
[i
]) {
1157 bs_table
[i
] = bdrv_open(hd_filename
[i
], snapshot
);
1159 fprintf(stderr
, "qemu: could not open hard disk image '%s\n",
1166 /* init CPU state */
1169 cpu_single_env
= env
;
1172 cpu_calibrate_ticks();
1176 dumb_display_init(ds
);
1179 sdl_display_init(ds
);
1181 dumb_display_init(ds
);
1185 #if defined(TARGET_I386)
1186 pc_init(ram_size
, vga_ram_size
, boot_device
,
1187 ds
, fd_filename
, snapshot
,
1188 kernel_filename
, kernel_cmdline
, initrd_filename
);
1189 #elif defined(TARGET_PPC)
1193 /* setup cpu signal handlers for MMU / self modifying code handling */
1194 #if !defined(CONFIG_SOFTMMU)
1196 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1199 signal_stack
= malloc(SIGNAL_STACK_SIZE
);
1200 stk
.ss_sp
= signal_stack
;
1201 stk
.ss_size
= SIGNAL_STACK_SIZE
;
1204 if (sigaltstack(&stk
, NULL
) < 0) {
1205 perror("sigaltstack");
1211 sigfillset(&act
.sa_mask
);
1212 act
.sa_flags
= SA_SIGINFO
;
1213 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1214 act
.sa_flags
|= SA_ONSTACK
;
1216 act
.sa_sigaction
= host_segv_handler
;
1217 sigaction(SIGSEGV
, &act
, NULL
);
1218 sigaction(SIGBUS
, &act
, NULL
);
1219 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1220 sigaction(SIGFPE
, &act
, NULL
);
1225 sigfillset(&act
.sa_mask
);
1226 act
.sa_flags
= SA_SIGINFO
;
1227 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1228 act
.sa_flags
|= SA_ONSTACK
;
1230 act
.sa_sigaction
= host_alarm_handler
;
1231 sigaction(SIGALRM
, &act
, NULL
);
1233 itv
.it_interval
.tv_sec
= 0;
1234 itv
.it_interval
.tv_usec
= 1000;
1235 itv
.it_value
.tv_sec
= 0;
1236 itv
.it_value
.tv_usec
= 10 * 1000;
1237 setitimer(ITIMER_REAL
, &itv
, NULL
);
1238 /* we probe the tick duration of the kernel to inform the user if
1239 the emulated kernel requested a too high timer frequency */
1240 getitimer(ITIMER_REAL
, &itv
);
1241 timer_ms
= itv
.it_interval
.tv_usec
/ 1000;
1242 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
* PIT_FREQ
) /
1246 cpu_gdbstub(NULL
, main_loop
, gdbstub_port
);