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
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
47 #include <linux/if_tun.h>
54 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
56 //#define DEBUG_UNUSED_IOPORT
58 #if !defined(CONFIG_SOFTMMU)
59 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
61 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
64 #if defined (TARGET_I386)
65 #elif defined (TARGET_PPC)
66 //#define USE_OPEN_FIRMWARE
67 #if !defined (USE_OPEN_FIRMWARE)
68 #define KERNEL_LOAD_ADDR 0x01000000
69 #define KERNEL_STACK_ADDR 0x01200000
71 #define KERNEL_LOAD_ADDR 0x00000000
72 #define KERNEL_STACK_ADDR 0x00400000
76 #define GUI_REFRESH_INTERVAL 30
78 /* XXX: use a two level table to limit memory usage */
79 #define MAX_IOPORTS 65536
81 const char *bios_dir = CONFIG_QEMU_SHAREDIR;
82 char phys_ram_file[1024];
84 CPUState *cpu_single_env;
85 void *ioport_opaque[MAX_IOPORTS];
86 IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
87 IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
88 BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
90 static DisplayState display_state;
93 int64_t ticks_per_sec;
94 int boot_device = 'c';
96 static char network_script[1024];
97 int pit_min_timer_count = 0;
99 NetDriverState nd_table[MAX_NICS];
100 SerialState *serial_console;
102 /***********************************************************/
105 uint32_t default_ioport_readb(void *opaque, uint32_t address)
107 #ifdef DEBUG_UNUSED_IOPORT
108 fprintf(stderr, "inb: port=0x%04x\n", address);
113 void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
115 #ifdef DEBUG_UNUSED_IOPORT
116 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
120 /* default is to make two byte accesses */
121 uint32_t default_ioport_readw(void *opaque, uint32_t address)
124 data = ioport_read_table[0][address & (MAX_IOPORTS - 1)](opaque, address);
125 data |= ioport_read_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1) << 8;
129 void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
131 ioport_write_table[0][address & (MAX_IOPORTS - 1)](opaque, address, data & 0xff);
132 ioport_write_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1, (data >> 8) & 0xff);
135 uint32_t default_ioport_readl(void *opaque, uint32_t address)
137 #ifdef DEBUG_UNUSED_IOPORT
138 fprintf(stderr, "inl: port=0x%04x\n", address);
143 void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
145 #ifdef DEBUG_UNUSED_IOPORT
146 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
150 void init_ioports(void)
154 for(i = 0; i < MAX_IOPORTS; i++) {
155 ioport_read_table[0][i] = default_ioport_readb;
156 ioport_write_table[0][i] = default_ioport_writeb;
157 ioport_read_table[1][i] = default_ioport_readw;
158 ioport_write_table[1][i] = default_ioport_writew;
159 ioport_read_table[2][i] = default_ioport_readl;
160 ioport_write_table[2][i] = default_ioport_writel;
164 /* size is the word size in byte */
165 int register_ioport_read(int start, int length, int size,
166 IOPortReadFunc *func, void *opaque)
172 } else if (size == 2) {
174 } else if (size == 4) {
177 hw_error("register_ioport_read: invalid size");
180 for(i = start; i < start + length; i += size) {
181 ioport_read_table[bsize][i] = func;
182 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
183 hw_error("register_ioport_read: invalid opaque");
184 ioport_opaque[i] = opaque;
189 /* size is the word size in byte */
190 int register_ioport_write(int start, int length, int size,
191 IOPortWriteFunc *func, void *opaque)
197 } else if (size == 2) {
199 } else if (size == 4) {
202 hw_error("register_ioport_write: invalid size");
205 for(i = start; i < start + length; i += size) {
206 ioport_write_table[bsize][i] = func;
207 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
208 hw_error("register_ioport_read: invalid opaque");
209 ioport_opaque[i] = opaque;
214 void pstrcpy(char *buf, int buf_size, const char *str)
224 if (c == 0 || q >= buf + buf_size - 1)
231 /* strcat and truncate. */
232 char *pstrcat(char *buf, int buf_size, const char *s)
237 pstrcpy(buf + len, buf_size - len, s);
241 /* return the size or -1 if error */
242 int load_image(const char *filename, uint8_t *addr)
245 fd = open(filename, O_RDONLY);
248 size = lseek(fd, 0, SEEK_END);
249 lseek(fd, 0, SEEK_SET);
250 if (read(fd, addr, size) != size) {
258 void cpu_outb(CPUState *env, int addr, int val)
260 addr &= (MAX_IOPORTS - 1);
261 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
264 void cpu_outw(CPUState *env, int addr, int val)
266 addr &= (MAX_IOPORTS - 1);
267 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
270 void cpu_outl(CPUState *env, int addr, int val)
272 addr &= (MAX_IOPORTS - 1);
273 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
276 int cpu_inb(CPUState *env, int addr)
278 addr &= (MAX_IOPORTS - 1);
279 return ioport_read_table[0][addr](ioport_opaque[addr], addr);
282 int cpu_inw(CPUState *env, int addr)
284 addr &= (MAX_IOPORTS - 1);
285 return ioport_read_table[1][addr](ioport_opaque[addr], addr);
288 int cpu_inl(CPUState *env, int addr)
290 addr &= (MAX_IOPORTS - 1);
291 return ioport_read_table[2][addr](ioport_opaque[addr], addr);
294 /***********************************************************/
295 void hw_error(const char *fmt, ...)
300 fprintf(stderr, "qemu: hardware error: ");
301 vfprintf(stderr, fmt, ap);
302 fprintf(stderr, "\n");
304 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
306 cpu_dump_state(global_env, stderr, 0);
312 #if defined(__powerpc__)
314 static inline uint32_t get_tbl(void)
317 asm volatile("mftb %0" : "=r" (tbl));
321 static inline uint32_t get_tbu(void)
324 asm volatile("mftbu %0" : "=r" (tbl));
328 int64_t cpu_get_real_ticks(void)
331 /* NOTE: we test if wrapping has occurred */
337 return ((int64_t)h << 32) | l;
340 #elif defined(__i386__)
342 int64_t cpu_get_real_ticks(void)
345 asm("rdtsc" : "=A" (val));
350 #error unsupported CPU
353 static int64_t cpu_ticks_offset;
354 static int64_t cpu_ticks_last;
356 int64_t cpu_get_ticks(void)
358 return cpu_get_real_ticks() + cpu_ticks_offset;
361 /* enable cpu_get_ticks() */
362 void cpu_enable_ticks(void)
364 cpu_ticks_offset = cpu_ticks_last - cpu_get_real_ticks();
367 /* disable cpu_get_ticks() : the clock is stopped. You must not call
368 cpu_get_ticks() after that. */
369 void cpu_disable_ticks(void)
371 cpu_ticks_last = cpu_get_ticks();
374 int64_t get_clock(void)
377 gettimeofday(&tv, NULL);
378 return tv.tv_sec * 1000000LL + tv.tv_usec;
381 void cpu_calibrate_ticks(void)
386 ticks = cpu_get_ticks();
388 usec = get_clock() - usec;
389 ticks = cpu_get_ticks() - ticks;
390 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
393 /* compute with 96 bit intermediate result: (a*b)/c */
394 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
399 #ifdef WORDS_BIGENDIAN
409 rl = (uint64_t)u.l.low * (uint64_t)b;
410 rh = (uint64_t)u.l.high * (uint64_t)b;
413 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
417 /***********************************************************/
420 int serial_open_device(void)
422 char slave_name[1024];
423 int master_fd, slave_fd;
425 if (serial_console == NULL && nographic) {
426 /* use console for serial port */
429 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
430 fprintf(stderr, "warning: could not create pseudo terminal for serial port\n");
433 fprintf(stderr, "Serial port redirected to %s\n", slave_name);
438 /***********************************************************/
439 /* Linux network device redirector */
441 static int tun_open(char *ifname, int ifname_size)
446 fd = open("/dev/net/tun", O_RDWR);
448 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
451 memset(&ifr, 0, sizeof(ifr));
452 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
453 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
454 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
456 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
460 printf("Connected to host network interface: %s\n", ifr.ifr_name);
461 pstrcpy(ifname, ifname_size, ifr.ifr_name);
462 fcntl(fd, F_SETFL, O_NONBLOCK);
466 static int net_init(void)
468 int pid, status, launch_script, i;
470 char *args[MAX_NICS + 2];
474 for(i = 0; i < nb_nics; i++) {
477 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
484 /* try to launch network init script */
489 *parg++ = network_script;
490 for(i = 0; i < nb_nics; i++) {
493 *parg++ = nd->ifname;
497 execv(network_script, args);
500 while (waitpid(pid, &status, 0) != pid);
501 if (!WIFEXITED(status) ||
502 WEXITSTATUS(status) != 0) {
503 fprintf(stderr, "%s: could not launch network script\n",
511 void net_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
514 printf("NE2000: sending packet size=%d\n", size);
516 write(nd->fd, buf, size);
519 /***********************************************************/
522 /* init terminal so that we can grab keys */
523 static struct termios oldtty;
525 static void term_exit(void)
527 tcsetattr (0, TCSANOW, &oldtty);
530 static void term_init(void)
537 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
538 |INLCR|IGNCR|ICRNL|IXON);
539 tty.c_oflag |= OPOST;
540 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
541 /* if graphical mode, we allow Ctrl-C handling */
543 tty.c_lflag &= ~ISIG;
544 tty.c_cflag &= ~(CSIZE|PARENB);
549 tcsetattr (0, TCSANOW, &tty);
553 fcntl(0, F_SETFL, O_NONBLOCK);
556 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
560 static void dumb_resize(DisplayState *ds, int w, int h)
564 static void dumb_refresh(DisplayState *ds)
566 vga_update_display();
569 void dumb_display_init(DisplayState *ds)
574 ds->dpy_update = dumb_update;
575 ds->dpy_resize = dumb_resize;
576 ds->dpy_refresh = dumb_refresh;
579 #if !defined(CONFIG_SOFTMMU)
580 /***********************************************************/
581 /* cpu signal handler */
582 static void host_segv_handler(int host_signum, siginfo_t *info,
585 if (cpu_signal_handler(host_signum, info, puc))
592 static int timer_irq_pending;
593 static int timer_irq_count;
596 static int gui_refresh_pending, gui_refresh_count;
598 static void host_alarm_handler(int host_signum, siginfo_t *info,
601 /* NOTE: since usually the OS asks a 100 Hz clock, there can be
602 some drift between cpu_get_ticks() and the interrupt time. So
603 we queue some interrupts to avoid missing some */
604 timer_irq_count += pit_get_out_edges(&pit_channels[0]);
605 if (timer_irq_count) {
606 if (timer_irq_count > 2)
609 timer_irq_pending = 1;
611 gui_refresh_count += timer_ms;
612 if (gui_refresh_count >= GUI_REFRESH_INTERVAL) {
613 gui_refresh_count = 0;
614 gui_refresh_pending = 1;
617 if (gui_refresh_pending || timer_irq_pending) {
618 /* just exit from the cpu to have a chance to handle timers */
619 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
623 #define MAX_IO_HANDLERS 64
625 typedef struct IOHandlerRecord {
627 IOCanRWHandler *fd_can_read;
628 IOReadHandler *fd_read;
635 static IOHandlerRecord io_handlers[MAX_IO_HANDLERS];
636 static int nb_io_handlers = 0;
638 int add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
639 IOReadHandler *fd_read, void *opaque)
641 IOHandlerRecord *ioh;
643 if (nb_io_handlers >= MAX_IO_HANDLERS)
645 ioh = &io_handlers[nb_io_handlers];
647 ioh->fd_can_read = fd_can_read;
648 ioh->fd_read = fd_read;
649 ioh->opaque = opaque;
654 /* main execution loop */
656 CPUState *cpu_gdbstub_get_env(void *opaque)
661 int main_loop(void *opaque)
663 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf, *gdb_ufd;
664 int ret, n, timeout, serial_ok, max_size, i;
666 IOHandlerRecord *ioh;
667 CPUState *env = global_env;
670 /* initialize terminal only there so that the user has a
671 chance to stop QEMU with Ctrl-C before the gdb connection
680 #if defined (DO_TB_FLUSH)
684 if (reset_requested) {
685 ret = EXCP_INTERRUPT;
688 if (ret == EXCP_DEBUG) {
692 /* if hlt instruction, we wait until the next IRQ */
698 /* poll any events */
701 for(i = 0; i < nb_io_handlers; i++) {
702 max_size = ioh->fd_can_read(ioh->opaque);
704 if (max_size > sizeof(buf))
705 max_size = sizeof(buf);
713 ioh->max_size = max_size;
718 if (gdbstub_fd > 0) {
725 ret = poll(ufds, pf - ufds, timeout);
728 for(i = 0; i < nb_io_handlers; i++) {
731 n = read(ioh->fd, buf, ioh->max_size);
733 ioh->fd_read(ioh->opaque, buf, n);
738 if (gdb_ufd && (gdb_ufd->revents & POLLIN)) {
740 /* stop emulation if requested by gdb */
741 n = read(gdbstub_fd, buf, 1);
743 ret = EXCP_INTERRUPT;
750 if (timer_irq_pending) {
751 #if defined (TARGET_I386)
754 timer_irq_pending = 0;
758 /* XXX: add explicit timer */
761 /* run dma transfers, if any */
765 if (gui_refresh_pending) {
766 display_state.dpy_refresh(&display_state);
767 gui_refresh_pending = 0;
776 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
777 "usage: %s [options] [disk_image]\n"
779 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
781 "Standard options:\n"
782 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
783 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
784 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
785 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
786 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
787 "-snapshot write to temporary files instead of disk image files\n"
788 "-m megs set virtual RAM size to megs MB\n"
789 "-nographic disable graphical output and redirect serial I/Os to console\n"
792 "-n script set network init script [default=%s]\n"
793 "-nics n simulate 'n' network interfaces [default=1]\n"
794 "-tun-fd fd0[,...] use these fds as already opened tap/tun interfaces\n"
796 "Linux boot specific:\n"
797 "-kernel bzImage use 'bzImage' as kernel image\n"
798 "-append cmdline use 'cmdline' as kernel command line\n"
799 "-initrd file use 'file' as initial ram disk\n"
801 "Debug/Expert options:\n"
802 "-s wait gdb connection to port %d\n"
803 "-p port change gdb connection port\n"
804 "-d output log to %s\n"
805 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
806 "-L path set the directory for the BIOS and VGA BIOS\n"
808 "-no-code-copy disable code copy acceleration\n"
812 "During emulation, use C-a h to get terminal commands:\n",
813 #ifdef CONFIG_SOFTMMU
818 DEFAULT_NETWORK_SCRIPT,
819 DEFAULT_GDBSTUB_PORT,
822 #ifndef CONFIG_SOFTMMU
824 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
825 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
831 struct option long_options[] = {
832 { "initrd", 1, NULL, 0, },
833 { "hda", 1, NULL, 0, },
834 { "hdb", 1, NULL, 0, },
835 { "snapshot", 0, NULL, 0, },
836 { "hdachs", 1, NULL, 0, },
837 { "nographic", 0, NULL, 0, },
838 { "kernel", 1, NULL, 0, },
839 { "append", 1, NULL, 0, },
840 { "tun-fd", 1, NULL, 0, },
841 { "hdc", 1, NULL, 0, },
842 { "hdd", 1, NULL, 0, },
843 { "cdrom", 1, NULL, 0, },
844 { "boot", 1, NULL, 0, },
845 { "fda", 1, NULL, 0, },
846 { "fdb", 1, NULL, 0, },
847 { "no-code-copy", 0, NULL, 0 },
848 { "nics", 1, NULL, 0 },
849 { NULL, 0, NULL, 0 },
853 /* SDL use the pthreads and they modify sigaction. We don't
855 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
856 extern void __libc_sigaction();
857 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
859 extern void __sigaction();
860 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
862 #endif /* CONFIG_SDL */
864 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
866 /* this stack is only used during signal handling */
867 #define SIGNAL_STACK_SIZE 32768
869 static uint8_t *signal_stack;
873 int main(int argc, char **argv)
875 int c, i, use_gdbstub, gdbstub_port, long_index, has_cdrom;
876 int snapshot, linux_boot;
877 struct sigaction act;
878 struct itimerval itv;
880 const char *initrd_filename;
881 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
882 const char *kernel_filename, *kernel_cmdline;
883 DisplayState *ds = &display_state;
884 int cyls, heads, secs;
886 /* we never want that malloc() uses mmap() */
887 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
888 initrd_filename = NULL;
889 for(i = 0; i < MAX_FD; i++)
890 fd_filename[i] = NULL;
891 for(i = 0; i < MAX_DISKS; i++)
892 hd_filename[i] = NULL;
893 ram_size = 32 * 1024 * 1024;
894 vga_ram_size = VGA_RAM_SIZE;
895 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
897 gdbstub_port = DEFAULT_GDBSTUB_PORT;
900 kernel_filename = NULL;
903 cyls = heads = secs = 0;
906 for(i = 0; i < MAX_NICS; i++) {
907 NetDriverState *nd = &nd_table[i];
909 /* init virtual mac address */
910 nd->macaddr[0] = 0x52;
911 nd->macaddr[1] = 0x54;
912 nd->macaddr[2] = 0x00;
913 nd->macaddr[3] = 0x12;
914 nd->macaddr[4] = 0x34;
915 nd->macaddr[5] = 0x56 + i;
919 c = getopt_long_only(argc, argv, "hm:dn:sp:L:", long_options, &long_index);
926 initrd_filename = optarg;
929 hd_filename[0] = optarg;
932 hd_filename[1] = optarg;
941 cyls = strtol(p, (char **)&p, 0);
945 heads = strtol(p, (char **)&p, 0);
949 secs = strtol(p, (char **)&p, 0);
960 kernel_filename = optarg;
963 kernel_cmdline = optarg;
972 fd = strtol(p, (char **)&p, 0);
973 nd_table[nb_nics].fd = fd;
974 snprintf(nd_table[nb_nics].ifname,
975 sizeof(nd_table[nb_nics].ifname),
980 } else if (*p != '\0') {
981 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_nics);
990 hd_filename[2] = optarg;
994 hd_filename[3] = optarg;
997 hd_filename[2] = optarg;
1001 boot_device = optarg[0];
1002 if (boot_device != 'a' && boot_device != 'b' &&
1003 boot_device != 'c' && boot_device != 'd') {
1004 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
1009 fd_filename[0] = optarg;
1012 fd_filename[1] = optarg;
1015 code_copy_enabled = 0;
1018 nb_nics = atoi(optarg);
1019 if (nb_nics < 1 || nb_nics > MAX_NICS) {
1020 fprintf(stderr, "qemu: invalid number of network interfaces\n");
1030 ram_size = atoi(optarg) * 1024 * 1024;
1033 if (ram_size > PHYS_RAM_MAX_SIZE) {
1034 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
1035 PHYS_RAM_MAX_SIZE / (1024 * 1024));
1040 cpu_set_log(CPU_LOG_ALL);
1043 pstrcpy(network_script, sizeof(network_script), optarg);
1049 gdbstub_port = atoi(optarg);
1057 if (optind < argc) {
1058 hd_filename[0] = argv[optind++];
1061 linux_boot = (kernel_filename != NULL);
1063 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
1064 fd_filename[0] == '\0')
1067 /* boot to cd by default if no hard disk */
1068 if (hd_filename[0] == '\0' && boot_device == 'c') {
1069 if (fd_filename[0] != '\0')
1075 #if !defined(CONFIG_SOFTMMU)
1076 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1078 static uint8_t stdout_buf[4096];
1079 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
1082 setvbuf(stdout, NULL, _IOLBF, 0);
1085 /* init host network redirectors */
1088 /* init the memory */
1089 phys_ram_size = ram_size + vga_ram_size;
1091 #ifdef CONFIG_SOFTMMU
1092 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
1093 if (!phys_ram_base) {
1094 fprintf(stderr, "Could not allocate physical memory\n");
1098 /* as we must map the same page at several addresses, we must use
1103 tmpdir = getenv("QEMU_TMPDIR");
1106 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
1107 if (mkstemp(phys_ram_file) < 0) {
1108 fprintf(stderr, "Could not create temporary memory file '%s'\n",
1112 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
1113 if (phys_ram_fd < 0) {
1114 fprintf(stderr, "Could not open temporary memory file '%s'\n",
1118 ftruncate(phys_ram_fd, phys_ram_size);
1119 unlink(phys_ram_file);
1120 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
1122 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
1124 if (phys_ram_base == MAP_FAILED) {
1125 fprintf(stderr, "Could not map physical memory\n");
1131 /* we always create the cdrom drive, even if no disk is there */
1133 bs_table[2] = bdrv_new("cdrom");
1134 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
1137 /* open the virtual block devices */
1138 for(i = 0; i < MAX_DISKS; i++) {
1139 if (hd_filename[i]) {
1142 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
1143 bs_table[i] = bdrv_new(buf);
1145 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
1146 fprintf(stderr, "qemu: could not open hard disk image '%s\n",
1150 if (i == 0 && cyls != 0)
1151 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
1155 /* we always create at least one floppy disk */
1156 fd_table[0] = bdrv_new("fda");
1157 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
1159 for(i = 0; i < MAX_FD; i++) {
1160 if (fd_filename[i]) {
1163 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
1164 fd_table[i] = bdrv_new(buf);
1165 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
1167 if (fd_filename[i] != '\0') {
1168 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
1169 fprintf(stderr, "qemu: could not open floppy disk image '%s\n",
1177 /* init CPU state */
1180 cpu_single_env = env;
1183 cpu_calibrate_ticks();
1187 dumb_display_init(ds);
1190 sdl_display_init(ds);
1192 dumb_display_init(ds);
1196 #if defined(TARGET_I386)
1197 pc_init(ram_size, vga_ram_size, boot_device,
1198 ds, fd_filename, snapshot,
1199 kernel_filename, kernel_cmdline, initrd_filename);
1200 #elif defined(TARGET_PPC)
1204 /* launched after the device init so that it can display or not a
1208 /* setup cpu signal handlers for MMU / self modifying code handling */
1209 #if !defined(CONFIG_SOFTMMU)
1211 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1214 signal_stack = malloc(SIGNAL_STACK_SIZE);
1215 stk.ss_sp = signal_stack;
1216 stk.ss_size = SIGNAL_STACK_SIZE;
1219 if (sigaltstack(&stk, NULL) < 0) {
1220 perror("sigaltstack");
1226 sigfillset(&act.sa_mask);
1227 act.sa_flags = SA_SIGINFO;
1228 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1229 act.sa_flags |= SA_ONSTACK;
1231 act.sa_sigaction = host_segv_handler;
1232 sigaction(SIGSEGV, &act, NULL);
1233 sigaction(SIGBUS, &act, NULL);
1234 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1235 sigaction(SIGFPE, &act, NULL);
1240 sigfillset(&act.sa_mask);
1241 act.sa_flags = SA_SIGINFO;
1242 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1243 act.sa_flags |= SA_ONSTACK;
1245 act.sa_sigaction = host_alarm_handler;
1246 sigaction(SIGALRM, &act, NULL);
1248 itv.it_interval.tv_sec = 0;
1249 itv.it_interval.tv_usec = 1000;
1250 itv.it_value.tv_sec = 0;
1251 itv.it_value.tv_usec = 10 * 1000;
1252 setitimer(ITIMER_REAL, &itv, NULL);
1253 /* we probe the tick duration of the kernel to inform the user if
1254 the emulated kernel requested a too high timer frequency */
1255 getitimer(ITIMER_REAL, &itv);
1256 timer_ms = itv.it_interval.tv_usec / 1000;
1257 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) /
1261 cpu_gdbstub(NULL, main_loop, gdbstub_port);