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>
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 void *ioport_opaque[MAX_IOPORTS];
85 IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
86 IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
87 BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
89 static DisplayState display_state;
92 int64_t ticks_per_sec;
93 int boot_device = 'c';
95 static char network_script[1024];
96 int pit_min_timer_count = 0;
98 NetDriverState nd_table[MAX_NICS];
99 SerialState *serial_console;
101 /***********************************************************/
104 uint32_t default_ioport_readb(void *opaque, uint32_t address)
106 #ifdef DEBUG_UNUSED_IOPORT
107 fprintf(stderr, "inb: port=0x%04x\n", address);
112 void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
114 #ifdef DEBUG_UNUSED_IOPORT
115 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
119 /* default is to make two byte accesses */
120 uint32_t default_ioport_readw(void *opaque, uint32_t address)
123 data = ioport_read_table[0][address & (MAX_IOPORTS - 1)](opaque, address);
124 data |= ioport_read_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1) << 8;
128 void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
130 ioport_write_table[0][address & (MAX_IOPORTS - 1)](opaque, address, data & 0xff);
131 ioport_write_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1, (data >> 8) & 0xff);
134 uint32_t default_ioport_readl(void *opaque, uint32_t address)
136 #ifdef DEBUG_UNUSED_IOPORT
137 fprintf(stderr, "inl: port=0x%04x\n", address);
142 void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
144 #ifdef DEBUG_UNUSED_IOPORT
145 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
149 void init_ioports(void)
153 for(i = 0; i < MAX_IOPORTS; i++) {
154 ioport_read_table[0][i] = default_ioport_readb;
155 ioport_write_table[0][i] = default_ioport_writeb;
156 ioport_read_table[1][i] = default_ioport_readw;
157 ioport_write_table[1][i] = default_ioport_writew;
158 ioport_read_table[2][i] = default_ioport_readl;
159 ioport_write_table[2][i] = default_ioport_writel;
163 /* size is the word size in byte */
164 int register_ioport_read(int start, int length, int size,
165 IOPortReadFunc *func, void *opaque)
171 } else if (size == 2) {
173 } else if (size == 4) {
176 hw_error("register_ioport_read: invalid size");
179 for(i = start; i < start + length; i += size) {
180 ioport_read_table[bsize][i] = func;
181 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
182 hw_error("register_ioport_read: invalid opaque");
183 ioport_opaque[i] = opaque;
188 /* size is the word size in byte */
189 int register_ioport_write(int start, int length, int size,
190 IOPortWriteFunc *func, void *opaque)
196 } else if (size == 2) {
198 } else if (size == 4) {
201 hw_error("register_ioport_write: invalid size");
204 for(i = start; i < start + length; i += size) {
205 ioport_write_table[bsize][i] = func;
206 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
207 hw_error("register_ioport_read: invalid opaque");
208 ioport_opaque[i] = opaque;
213 void pstrcpy(char *buf, int buf_size, const char *str)
223 if (c == 0 || q >= buf + buf_size - 1)
230 /* strcat and truncate. */
231 char *pstrcat(char *buf, int buf_size, const char *s)
236 pstrcpy(buf + len, buf_size - len, s);
240 /* return the size or -1 if error */
241 int load_image(const char *filename, uint8_t *addr)
244 fd = open(filename, O_RDONLY);
247 size = lseek(fd, 0, SEEK_END);
248 lseek(fd, 0, SEEK_SET);
249 if (read(fd, addr, size) != size) {
257 void cpu_outb(CPUState *env, int addr, int val)
259 addr &= (MAX_IOPORTS - 1);
260 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
263 void cpu_outw(CPUState *env, int addr, int val)
265 addr &= (MAX_IOPORTS - 1);
266 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
269 void cpu_outl(CPUState *env, int addr, int val)
271 addr &= (MAX_IOPORTS - 1);
272 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
275 int cpu_inb(CPUState *env, int addr)
277 addr &= (MAX_IOPORTS - 1);
278 return ioport_read_table[0][addr](ioport_opaque[addr], addr);
281 int cpu_inw(CPUState *env, int addr)
283 addr &= (MAX_IOPORTS - 1);
284 return ioport_read_table[1][addr](ioport_opaque[addr], addr);
287 int cpu_inl(CPUState *env, int addr)
289 addr &= (MAX_IOPORTS - 1);
290 return ioport_read_table[2][addr](ioport_opaque[addr], addr);
293 /***********************************************************/
294 void hw_error(const char *fmt, ...)
299 fprintf(stderr, "qemu: hardware error: ");
300 vfprintf(stderr, fmt, ap);
301 fprintf(stderr, "\n");
303 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
305 cpu_dump_state(global_env, stderr, 0);
311 #if defined(__powerpc__)
313 static inline uint32_t get_tbl(void)
316 asm volatile("mftb %0" : "=r" (tbl));
320 static inline uint32_t get_tbu(void)
323 asm volatile("mftbu %0" : "=r" (tbl));
327 int64_t cpu_get_real_ticks(void)
330 /* NOTE: we test if wrapping has occurred */
336 return ((int64_t)h << 32) | l;
339 #elif defined(__i386__)
341 int64_t cpu_get_real_ticks(void)
344 asm("rdtsc" : "=A" (val));
349 #error unsupported CPU
352 static int64_t cpu_ticks_offset;
353 static int64_t cpu_ticks_last;
355 int64_t cpu_get_ticks(void)
357 return cpu_get_real_ticks() + cpu_ticks_offset;
360 /* enable cpu_get_ticks() */
361 void cpu_enable_ticks(void)
363 cpu_ticks_offset = cpu_ticks_last - cpu_get_real_ticks();
366 /* disable cpu_get_ticks() : the clock is stopped. You must not call
367 cpu_get_ticks() after that. */
368 void cpu_disable_ticks(void)
370 cpu_ticks_last = cpu_get_ticks();
373 int64_t get_clock(void)
376 gettimeofday(&tv, NULL);
377 return tv.tv_sec * 1000000LL + tv.tv_usec;
380 void cpu_calibrate_ticks(void)
385 ticks = cpu_get_ticks();
387 usec = get_clock() - usec;
388 ticks = cpu_get_ticks() - ticks;
389 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
392 /* compute with 96 bit intermediate result: (a*b)/c */
393 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
398 #ifdef WORDS_BIGENDIAN
408 rl = (uint64_t)u.l.low * (uint64_t)b;
409 rh = (uint64_t)u.l.high * (uint64_t)b;
412 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
416 /***********************************************************/
419 int serial_open_device(void)
421 char slave_name[1024];
422 int master_fd, slave_fd;
424 if (serial_console == NULL && nographic) {
425 /* use console for serial port */
428 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
429 fprintf(stderr, "warning: could not create pseudo terminal for serial port\n");
432 fprintf(stderr, "Serial port redirected to %s\n", slave_name);
437 /***********************************************************/
438 /* Linux network device redirector */
440 static int tun_open(char *ifname, int ifname_size)
445 fd = open("/dev/net/tun", O_RDWR);
447 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
450 memset(&ifr, 0, sizeof(ifr));
451 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
452 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
453 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
455 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
459 printf("Connected to host network interface: %s\n", ifr.ifr_name);
460 pstrcpy(ifname, ifname_size, ifr.ifr_name);
461 fcntl(fd, F_SETFL, O_NONBLOCK);
465 static int net_init(void)
467 int pid, status, launch_script, i;
469 char *args[MAX_NICS + 2];
473 for(i = 0; i < nb_nics; i++) {
476 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
483 /* try to launch network init script */
488 *parg++ = network_script;
489 for(i = 0; i < nb_nics; i++) {
492 *parg++ = nd->ifname;
496 execv(network_script, args);
499 while (waitpid(pid, &status, 0) != pid);
500 if (!WIFEXITED(status) ||
501 WEXITSTATUS(status) != 0) {
502 fprintf(stderr, "%s: could not launch network script\n",
510 void net_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
513 printf("NE2000: sending packet size=%d\n", size);
515 write(nd->fd, buf, size);
518 /***********************************************************/
521 /* init terminal so that we can grab keys */
522 static struct termios oldtty;
524 static void term_exit(void)
526 tcsetattr (0, TCSANOW, &oldtty);
529 static void term_init(void)
536 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
537 |INLCR|IGNCR|ICRNL|IXON);
538 tty.c_oflag |= OPOST;
539 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
540 /* if graphical mode, we allow Ctrl-C handling */
542 tty.c_lflag &= ~ISIG;
543 tty.c_cflag &= ~(CSIZE|PARENB);
548 tcsetattr (0, TCSANOW, &tty);
552 fcntl(0, F_SETFL, O_NONBLOCK);
555 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
559 static void dumb_resize(DisplayState *ds, int w, int h)
563 static void dumb_refresh(DisplayState *ds)
565 vga_update_display();
568 void dumb_display_init(DisplayState *ds)
573 ds->dpy_update = dumb_update;
574 ds->dpy_resize = dumb_resize;
575 ds->dpy_refresh = dumb_refresh;
578 #if !defined(CONFIG_SOFTMMU)
579 /***********************************************************/
580 /* cpu signal handler */
581 static void host_segv_handler(int host_signum, siginfo_t *info,
584 if (cpu_signal_handler(host_signum, info, puc))
591 static int timer_irq_pending;
592 static int timer_irq_count;
595 static int gui_refresh_pending, gui_refresh_count;
597 static void host_alarm_handler(int host_signum, siginfo_t *info,
600 /* NOTE: since usually the OS asks a 100 Hz clock, there can be
601 some drift between cpu_get_ticks() and the interrupt time. So
602 we queue some interrupts to avoid missing some */
603 timer_irq_count += pit_get_out_edges(&pit_channels[0]);
604 if (timer_irq_count) {
605 if (timer_irq_count > 2)
608 timer_irq_pending = 1;
610 gui_refresh_count += timer_ms;
611 if (gui_refresh_count >= GUI_REFRESH_INTERVAL) {
612 gui_refresh_count = 0;
613 gui_refresh_pending = 1;
616 if (gui_refresh_pending || timer_irq_pending) {
617 /* just exit from the cpu to have a chance to handle timers */
618 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
622 #define MAX_IO_HANDLERS 64
624 typedef struct IOHandlerRecord {
626 IOCanRWHandler *fd_can_read;
627 IOReadHandler *fd_read;
634 static IOHandlerRecord io_handlers[MAX_IO_HANDLERS];
635 static int nb_io_handlers = 0;
637 int add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
638 IOReadHandler *fd_read, void *opaque)
640 IOHandlerRecord *ioh;
642 if (nb_io_handlers >= MAX_IO_HANDLERS)
644 ioh = &io_handlers[nb_io_handlers];
646 ioh->fd_can_read = fd_can_read;
647 ioh->fd_read = fd_read;
648 ioh->opaque = opaque;
653 /* main execution loop */
655 CPUState *cpu_gdbstub_get_env(void *opaque)
660 int main_loop(void *opaque)
662 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf, *gdb_ufd;
663 int ret, n, timeout, serial_ok, max_size, i;
665 IOHandlerRecord *ioh;
666 CPUState *env = global_env;
669 /* initialize terminal only there so that the user has a
670 chance to stop QEMU with Ctrl-C before the gdb connection
679 #if defined (DO_TB_FLUSH)
683 if (reset_requested) {
684 ret = EXCP_INTERRUPT;
687 if (ret == EXCP_DEBUG) {
691 /* if hlt instruction, we wait until the next IRQ */
697 /* poll any events */
700 for(i = 0; i < nb_io_handlers; i++) {
701 max_size = ioh->fd_can_read(ioh->opaque);
703 if (max_size > sizeof(buf))
704 max_size = sizeof(buf);
712 ioh->max_size = max_size;
717 if (gdbstub_fd > 0) {
724 ret = poll(ufds, pf - ufds, timeout);
727 for(i = 0; i < nb_io_handlers; i++) {
730 n = read(ioh->fd, buf, ioh->max_size);
732 ioh->fd_read(ioh->opaque, buf, n);
737 if (gdb_ufd && (gdb_ufd->revents & POLLIN)) {
739 /* stop emulation if requested by gdb */
740 n = read(gdbstub_fd, buf, 1);
742 ret = EXCP_INTERRUPT;
749 if (timer_irq_pending) {
750 #if defined (TARGET_I386)
753 timer_irq_pending = 0;
757 /* XXX: add explicit timer */
760 /* run dma transfers, if any */
764 if (gui_refresh_pending) {
765 display_state.dpy_refresh(&display_state);
766 gui_refresh_pending = 0;
775 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
776 "usage: %s [options] [disk_image]\n"
778 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
780 "Standard options:\n"
781 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
782 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
783 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
784 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
785 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
786 "-snapshot write to temporary files instead of disk image files\n"
787 "-m megs set virtual RAM size to megs MB\n"
788 "-nographic disable graphical output and redirect serial I/Os to console\n"
791 "-n script set network init script [default=%s]\n"
792 "-nics n simulate 'n' network interfaces [default=1]\n"
793 "-tun-fd fd0[,...] use these fds as already opened tap/tun interfaces\n"
795 "Linux boot specific:\n"
796 "-kernel bzImage use 'bzImage' as kernel image\n"
797 "-append cmdline use 'cmdline' as kernel command line\n"
798 "-initrd file use 'file' as initial ram disk\n"
800 "Debug/Expert options:\n"
801 "-s wait gdb connection to port %d\n"
802 "-p port change gdb connection port\n"
803 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
804 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
805 "-L path set the directory for the BIOS and VGA BIOS\n"
807 "-no-code-copy disable code copy acceleration\n"
811 "During emulation, use C-a h to get terminal commands:\n",
812 #ifdef CONFIG_SOFTMMU
817 DEFAULT_NETWORK_SCRIPT,
818 DEFAULT_GDBSTUB_PORT,
821 #ifndef CONFIG_SOFTMMU
823 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
824 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
830 struct option long_options[] = {
831 { "initrd", 1, NULL, 0, },
832 { "hda", 1, NULL, 0, },
833 { "hdb", 1, NULL, 0, },
834 { "snapshot", 0, NULL, 0, },
835 { "hdachs", 1, NULL, 0, },
836 { "nographic", 0, NULL, 0, },
837 { "kernel", 1, NULL, 0, },
838 { "append", 1, NULL, 0, },
839 { "tun-fd", 1, NULL, 0, },
840 { "hdc", 1, NULL, 0, },
841 { "hdd", 1, NULL, 0, },
842 { "cdrom", 1, NULL, 0, },
843 { "boot", 1, NULL, 0, },
844 { "fda", 1, NULL, 0, },
845 { "fdb", 1, NULL, 0, },
846 { "no-code-copy", 0, NULL, 0 },
847 { "nics", 1, NULL, 0 },
848 { NULL, 0, NULL, 0 },
852 /* SDL use the pthreads and they modify sigaction. We don't
854 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
855 extern void __libc_sigaction();
856 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
858 extern void __sigaction();
859 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
861 #endif /* CONFIG_SDL */
863 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
865 /* this stack is only used during signal handling */
866 #define SIGNAL_STACK_SIZE 32768
868 static uint8_t *signal_stack;
872 int main(int argc, char **argv)
874 int c, i, use_gdbstub, gdbstub_port, long_index, has_cdrom;
875 int snapshot, linux_boot;
876 struct sigaction act;
877 struct itimerval itv;
879 const char *initrd_filename;
880 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
881 const char *kernel_filename, *kernel_cmdline;
882 DisplayState *ds = &display_state;
883 int cyls, heads, secs;
885 /* we never want that malloc() uses mmap() */
886 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
887 initrd_filename = NULL;
888 for(i = 0; i < MAX_FD; i++)
889 fd_filename[i] = NULL;
890 for(i = 0; i < MAX_DISKS; i++)
891 hd_filename[i] = NULL;
892 ram_size = 32 * 1024 * 1024;
893 vga_ram_size = VGA_RAM_SIZE;
894 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
896 gdbstub_port = DEFAULT_GDBSTUB_PORT;
899 kernel_filename = NULL;
902 cyls = heads = secs = 0;
905 for(i = 0; i < MAX_NICS; i++) {
906 NetDriverState *nd = &nd_table[i];
908 /* init virtual mac address */
909 nd->macaddr[0] = 0x52;
910 nd->macaddr[1] = 0x54;
911 nd->macaddr[2] = 0x00;
912 nd->macaddr[3] = 0x12;
913 nd->macaddr[4] = 0x34;
914 nd->macaddr[5] = 0x56 + i;
918 c = getopt_long_only(argc, argv, "hm:d:n:sp:L:", long_options, &long_index);
925 initrd_filename = optarg;
928 hd_filename[0] = optarg;
931 hd_filename[1] = optarg;
940 cyls = strtol(p, (char **)&p, 0);
944 heads = strtol(p, (char **)&p, 0);
948 secs = strtol(p, (char **)&p, 0);
959 kernel_filename = optarg;
962 kernel_cmdline = optarg;
971 fd = strtol(p, (char **)&p, 0);
972 nd_table[nb_nics].fd = fd;
973 snprintf(nd_table[nb_nics].ifname,
974 sizeof(nd_table[nb_nics].ifname),
979 } else if (*p != '\0') {
980 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_nics);
989 hd_filename[2] = optarg;
993 hd_filename[3] = optarg;
996 hd_filename[2] = optarg;
1000 boot_device = optarg[0];
1001 if (boot_device != 'a' && boot_device != 'b' &&
1002 boot_device != 'c' && boot_device != 'd') {
1003 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
1008 fd_filename[0] = optarg;
1011 fd_filename[1] = optarg;
1014 code_copy_enabled = 0;
1017 nb_nics = atoi(optarg);
1018 if (nb_nics < 1 || nb_nics > MAX_NICS) {
1019 fprintf(stderr, "qemu: invalid number of network interfaces\n");
1029 ram_size = atoi(optarg) * 1024 * 1024;
1032 if (ram_size > PHYS_RAM_MAX_SIZE) {
1033 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
1034 PHYS_RAM_MAX_SIZE / (1024 * 1024));
1043 mask = cpu_str_to_log_mask(optarg);
1045 printf("Log items (comma separated):\n");
1046 for(item = cpu_log_items; item->mask != 0; item++) {
1047 printf("%-10s %s\n", item->name, item->help);
1055 pstrcpy(network_script, sizeof(network_script), optarg);
1061 gdbstub_port = atoi(optarg);
1069 if (optind < argc) {
1070 hd_filename[0] = argv[optind++];
1073 linux_boot = (kernel_filename != NULL);
1075 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
1076 fd_filename[0] == '\0')
1079 /* boot to cd by default if no hard disk */
1080 if (hd_filename[0] == '\0' && boot_device == 'c') {
1081 if (fd_filename[0] != '\0')
1087 #if !defined(CONFIG_SOFTMMU)
1088 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1090 static uint8_t stdout_buf[4096];
1091 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
1094 setvbuf(stdout, NULL, _IOLBF, 0);
1097 /* init host network redirectors */
1100 /* init the memory */
1101 phys_ram_size = ram_size + vga_ram_size;
1103 #ifdef CONFIG_SOFTMMU
1104 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
1105 if (!phys_ram_base) {
1106 fprintf(stderr, "Could not allocate physical memory\n");
1110 /* as we must map the same page at several addresses, we must use
1115 tmpdir = getenv("QEMU_TMPDIR");
1118 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
1119 if (mkstemp(phys_ram_file) < 0) {
1120 fprintf(stderr, "Could not create temporary memory file '%s'\n",
1124 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
1125 if (phys_ram_fd < 0) {
1126 fprintf(stderr, "Could not open temporary memory file '%s'\n",
1130 ftruncate(phys_ram_fd, phys_ram_size);
1131 unlink(phys_ram_file);
1132 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
1134 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
1136 if (phys_ram_base == MAP_FAILED) {
1137 fprintf(stderr, "Could not map physical memory\n");
1143 /* we always create the cdrom drive, even if no disk is there */
1145 bs_table[2] = bdrv_new("cdrom");
1146 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
1149 /* open the virtual block devices */
1150 for(i = 0; i < MAX_DISKS; i++) {
1151 if (hd_filename[i]) {
1154 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
1155 bs_table[i] = bdrv_new(buf);
1157 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
1158 fprintf(stderr, "qemu: could not open hard disk image '%s\n",
1162 if (i == 0 && cyls != 0)
1163 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
1167 /* we always create at least one floppy disk */
1168 fd_table[0] = bdrv_new("fda");
1169 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
1171 for(i = 0; i < MAX_FD; i++) {
1172 if (fd_filename[i]) {
1175 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
1176 fd_table[i] = bdrv_new(buf);
1177 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
1179 if (fd_filename[i] != '\0') {
1180 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
1181 fprintf(stderr, "qemu: could not open floppy disk image '%s\n",
1189 /* init CPU state */
1192 cpu_single_env = env;
1195 cpu_calibrate_ticks();
1199 dumb_display_init(ds);
1202 sdl_display_init(ds);
1204 dumb_display_init(ds);
1208 #if defined(TARGET_I386)
1209 pc_init(ram_size, vga_ram_size, boot_device,
1210 ds, fd_filename, snapshot,
1211 kernel_filename, kernel_cmdline, initrd_filename);
1212 #elif defined(TARGET_PPC)
1216 /* launched after the device init so that it can display or not a
1220 /* setup cpu signal handlers for MMU / self modifying code handling */
1221 #if !defined(CONFIG_SOFTMMU)
1223 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1226 signal_stack = malloc(SIGNAL_STACK_SIZE);
1227 stk.ss_sp = signal_stack;
1228 stk.ss_size = SIGNAL_STACK_SIZE;
1231 if (sigaltstack(&stk, NULL) < 0) {
1232 perror("sigaltstack");
1238 sigfillset(&act.sa_mask);
1239 act.sa_flags = SA_SIGINFO;
1240 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1241 act.sa_flags |= SA_ONSTACK;
1243 act.sa_sigaction = host_segv_handler;
1244 sigaction(SIGSEGV, &act, NULL);
1245 sigaction(SIGBUS, &act, NULL);
1246 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1247 sigaction(SIGFPE, &act, NULL);
1252 sigfillset(&act.sa_mask);
1253 act.sa_flags = SA_SIGINFO;
1254 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1255 act.sa_flags |= SA_ONSTACK;
1257 act.sa_sigaction = host_alarm_handler;
1258 sigaction(SIGALRM, &act, NULL);
1260 itv.it_interval.tv_sec = 0;
1261 itv.it_interval.tv_usec = 1000;
1262 itv.it_value.tv_sec = 0;
1263 itv.it_value.tv_usec = 10 * 1000;
1264 setitimer(ITIMER_REAL, &itv, NULL);
1265 /* we probe the tick duration of the kernel to inform the user if
1266 the emulated kernel requested a too high timer frequency */
1267 getitimer(ITIMER_REAL, &itv);
1268 timer_ms = itv.it_interval.tv_usec / 1000;
1269 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) /
1273 cpu_gdbstub(NULL, main_loop, gdbstub_port);