4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 #include <sys/times.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
50 #include <linux/if_tun.h>
53 #include <linux/rtc.h>
57 #if defined(CONFIG_SLIRP)
63 #include <sys/timeb.h>
65 #define getopt_long_only getopt_long
66 #define memalign(align, size) malloc(size)
73 #endif /* CONFIG_SDL */
81 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
83 //#define DEBUG_UNUSED_IOPORT
84 //#define DEBUG_IOPORT
86 #if !defined(CONFIG_SOFTMMU)
87 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
89 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
93 #define DEFAULT_RAM_SIZE 144
95 #define DEFAULT_RAM_SIZE 128
98 #define GUI_REFRESH_INTERVAL 30
100 /* XXX: use a two level table to limit memory usage */
101 #define MAX_IOPORTS 65536
103 const char *bios_dir = CONFIG_QEMU_SHAREDIR;
104 char phys_ram_file[1024];
105 CPUState *global_env;
106 CPUState *cpu_single_env;
107 void *ioport_opaque[MAX_IOPORTS];
108 IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
109 IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
110 BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
113 static DisplayState display_state;
115 int64_t ticks_per_sec;
116 int boot_device = 'c';
118 static char network_script[1024];
119 int pit_min_timer_count = 0;
121 NetDriverState nd_table[MAX_NICS];
122 QEMUTimer *gui_timer;
124 int audio_enabled = 0;
125 int sb16_enabled = 1;
126 int adlib_enabled = 1;
129 int prep_enabled = 0;
131 int cirrus_vga_enabled = 1;
132 int graphic_width = 800;
133 int graphic_height = 600;
134 int graphic_depth = 15;
136 TextConsole *vga_console;
137 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
139 /***********************************************************/
140 /* x86 ISA bus support */
142 target_phys_addr_t isa_mem_base = 0;
144 uint32_t default_ioport_readb(void *opaque, uint32_t address)
146 #ifdef DEBUG_UNUSED_IOPORT
147 fprintf(stderr, "inb: port=0x%04x\n", address);
152 void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
154 #ifdef DEBUG_UNUSED_IOPORT
155 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
159 /* default is to make two byte accesses */
160 uint32_t default_ioport_readw(void *opaque, uint32_t address)
163 data = ioport_read_table[0][address](ioport_opaque[address], address);
164 address = (address + 1) & (MAX_IOPORTS - 1);
165 data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;
169 void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
171 ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);
172 address = (address + 1) & (MAX_IOPORTS - 1);
173 ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);
176 uint32_t default_ioport_readl(void *opaque, uint32_t address)
178 #ifdef DEBUG_UNUSED_IOPORT
179 fprintf(stderr, "inl: port=0x%04x\n", address);
184 void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
186 #ifdef DEBUG_UNUSED_IOPORT
187 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
191 void init_ioports(void)
195 for(i = 0; i < MAX_IOPORTS; i++) {
196 ioport_read_table[0][i] = default_ioport_readb;
197 ioport_write_table[0][i] = default_ioport_writeb;
198 ioport_read_table[1][i] = default_ioport_readw;
199 ioport_write_table[1][i] = default_ioport_writew;
200 ioport_read_table[2][i] = default_ioport_readl;
201 ioport_write_table[2][i] = default_ioport_writel;
205 /* size is the word size in byte */
206 int register_ioport_read(int start, int length, int size,
207 IOPortReadFunc *func, void *opaque)
213 } else if (size == 2) {
215 } else if (size == 4) {
218 hw_error("register_ioport_read: invalid size");
221 for(i = start; i < start + length; i += size) {
222 ioport_read_table[bsize][i] = func;
223 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
224 hw_error("register_ioport_read: invalid opaque");
225 ioport_opaque[i] = opaque;
230 /* size is the word size in byte */
231 int register_ioport_write(int start, int length, int size,
232 IOPortWriteFunc *func, void *opaque)
238 } else if (size == 2) {
240 } else if (size == 4) {
243 hw_error("register_ioport_write: invalid size");
246 for(i = start; i < start + length; i += size) {
247 ioport_write_table[bsize][i] = func;
248 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
249 hw_error("register_ioport_read: invalid opaque");
250 ioport_opaque[i] = opaque;
255 void isa_unassign_ioport(int start, int length)
259 for(i = start; i < start + length; i++) {
260 ioport_read_table[0][i] = default_ioport_readb;
261 ioport_read_table[1][i] = default_ioport_readw;
262 ioport_read_table[2][i] = default_ioport_readl;
264 ioport_write_table[0][i] = default_ioport_writeb;
265 ioport_write_table[1][i] = default_ioport_writew;
266 ioport_write_table[2][i] = default_ioport_writel;
270 void pstrcpy(char *buf, int buf_size, const char *str)
280 if (c == 0 || q >= buf + buf_size - 1)
287 /* strcat and truncate. */
288 char *pstrcat(char *buf, int buf_size, const char *s)
293 pstrcpy(buf + len, buf_size - len, s);
297 int strstart(const char *str, const char *val, const char **ptr)
313 /* return the size or -1 if error */
314 int get_image_size(const char *filename)
317 fd = open(filename, O_RDONLY | O_BINARY);
320 size = lseek(fd, 0, SEEK_END);
325 /* return the size or -1 if error */
326 int load_image(const char *filename, uint8_t *addr)
329 fd = open(filename, O_RDONLY | O_BINARY);
332 size = lseek(fd, 0, SEEK_END);
333 lseek(fd, 0, SEEK_SET);
334 if (read(fd, addr, size) != size) {
342 void cpu_outb(CPUState *env, int addr, int val)
345 if (loglevel & CPU_LOG_IOPORT)
346 fprintf(logfile, "outb: %04x %02x\n", addr, val);
348 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
351 void cpu_outw(CPUState *env, int addr, int val)
354 if (loglevel & CPU_LOG_IOPORT)
355 fprintf(logfile, "outw: %04x %04x\n", addr, val);
357 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
360 void cpu_outl(CPUState *env, int addr, int val)
363 if (loglevel & CPU_LOG_IOPORT)
364 fprintf(logfile, "outl: %04x %08x\n", addr, val);
366 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
369 int cpu_inb(CPUState *env, int addr)
372 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
374 if (loglevel & CPU_LOG_IOPORT)
375 fprintf(logfile, "inb : %04x %02x\n", addr, val);
380 int cpu_inw(CPUState *env, int addr)
383 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
385 if (loglevel & CPU_LOG_IOPORT)
386 fprintf(logfile, "inw : %04x %04x\n", addr, val);
391 int cpu_inl(CPUState *env, int addr)
394 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
396 if (loglevel & CPU_LOG_IOPORT)
397 fprintf(logfile, "inl : %04x %08x\n", addr, val);
402 /***********************************************************/
403 void hw_error(const char *fmt, ...)
408 fprintf(stderr, "qemu: hardware error: ");
409 vfprintf(stderr, fmt, ap);
410 fprintf(stderr, "\n");
412 cpu_dump_state(global_env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
414 cpu_dump_state(global_env, stderr, fprintf, 0);
420 /***********************************************************/
423 static QEMUPutKBDEvent *qemu_put_kbd_event;
424 static void *qemu_put_kbd_event_opaque;
425 static QEMUPutMouseEvent *qemu_put_mouse_event;
426 static void *qemu_put_mouse_event_opaque;
428 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
430 qemu_put_kbd_event_opaque = opaque;
431 qemu_put_kbd_event = func;
434 void qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, void *opaque)
436 qemu_put_mouse_event_opaque = opaque;
437 qemu_put_mouse_event = func;
440 void kbd_put_keycode(int keycode)
442 if (qemu_put_kbd_event) {
443 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
447 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
449 if (qemu_put_mouse_event) {
450 qemu_put_mouse_event(qemu_put_mouse_event_opaque,
451 dx, dy, dz, buttons_state);
455 /***********************************************************/
458 #if defined(__powerpc__)
460 static inline uint32_t get_tbl(void)
463 asm volatile("mftb %0" : "=r" (tbl));
467 static inline uint32_t get_tbu(void)
470 asm volatile("mftbu %0" : "=r" (tbl));
474 int64_t cpu_get_real_ticks(void)
477 /* NOTE: we test if wrapping has occurred */
483 return ((int64_t)h << 32) | l;
486 #elif defined(__i386__)
488 int64_t cpu_get_real_ticks(void)
491 asm volatile ("rdtsc" : "=A" (val));
495 #elif defined(__x86_64__)
497 int64_t cpu_get_real_ticks(void)
501 asm volatile("rdtsc" : "=a" (low), "=d" (high));
509 #error unsupported CPU
512 static int64_t cpu_ticks_offset;
513 static int cpu_ticks_enabled;
515 static inline int64_t cpu_get_ticks(void)
517 if (!cpu_ticks_enabled) {
518 return cpu_ticks_offset;
520 return cpu_get_real_ticks() + cpu_ticks_offset;
524 /* enable cpu_get_ticks() */
525 void cpu_enable_ticks(void)
527 if (!cpu_ticks_enabled) {
528 cpu_ticks_offset -= cpu_get_real_ticks();
529 cpu_ticks_enabled = 1;
533 /* disable cpu_get_ticks() : the clock is stopped. You must not call
534 cpu_get_ticks() after that. */
535 void cpu_disable_ticks(void)
537 if (cpu_ticks_enabled) {
538 cpu_ticks_offset = cpu_get_ticks();
539 cpu_ticks_enabled = 0;
543 static int64_t get_clock(void)
548 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
551 gettimeofday(&tv, NULL);
552 return tv.tv_sec * 1000000LL + tv.tv_usec;
556 void cpu_calibrate_ticks(void)
561 ticks = cpu_get_real_ticks();
567 usec = get_clock() - usec;
568 ticks = cpu_get_real_ticks() - ticks;
569 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
572 /* compute with 96 bit intermediate result: (a*b)/c */
573 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
578 #ifdef WORDS_BIGENDIAN
588 rl = (uint64_t)u.l.low * (uint64_t)b;
589 rh = (uint64_t)u.l.high * (uint64_t)b;
592 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
596 #define QEMU_TIMER_REALTIME 0
597 #define QEMU_TIMER_VIRTUAL 1
601 /* XXX: add frequency */
609 struct QEMUTimer *next;
615 static QEMUTimer *active_timers[2];
617 static MMRESULT timerID;
619 /* frequency of the times() clock tick */
620 static int timer_freq;
623 QEMUClock *qemu_new_clock(int type)
626 clock = qemu_mallocz(sizeof(QEMUClock));
633 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
637 ts = qemu_mallocz(sizeof(QEMUTimer));
644 void qemu_free_timer(QEMUTimer *ts)
649 /* stop a timer, but do not dealloc it */
650 void qemu_del_timer(QEMUTimer *ts)
654 /* NOTE: this code must be signal safe because
655 qemu_timer_expired() can be called from a signal. */
656 pt = &active_timers[ts->clock->type];
669 /* modify the current timer so that it will be fired when current_time
670 >= expire_time. The corresponding callback will be called. */
671 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
677 /* add the timer in the sorted list */
678 /* NOTE: this code must be signal safe because
679 qemu_timer_expired() can be called from a signal. */
680 pt = &active_timers[ts->clock->type];
685 if (t->expire_time > expire_time)
689 ts->expire_time = expire_time;
694 int qemu_timer_pending(QEMUTimer *ts)
697 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
704 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
708 return (timer_head->expire_time <= current_time);
711 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
717 if (!ts || ts->expire_time > current_time)
719 /* remove timer from the list before calling the callback */
720 *ptimer_head = ts->next;
723 /* run the callback (the timer list can be modified) */
728 int64_t qemu_get_clock(QEMUClock *clock)
730 switch(clock->type) {
731 case QEMU_TIMER_REALTIME:
733 return GetTickCount();
738 /* Note that using gettimeofday() is not a good solution
739 for timers because its value change when the date is
741 if (timer_freq == 100) {
742 return times(&tp) * 10;
744 return ((int64_t)times(&tp) * 1000) / timer_freq;
749 case QEMU_TIMER_VIRTUAL:
750 return cpu_get_ticks();
755 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
757 uint64_t expire_time;
759 if (qemu_timer_pending(ts)) {
760 expire_time = ts->expire_time;
764 qemu_put_be64(f, expire_time);
767 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
769 uint64_t expire_time;
771 expire_time = qemu_get_be64(f);
772 if (expire_time != -1) {
773 qemu_mod_timer(ts, expire_time);
779 static void timer_save(QEMUFile *f, void *opaque)
781 if (cpu_ticks_enabled) {
782 hw_error("cannot save state if virtual timers are running");
784 qemu_put_be64s(f, &cpu_ticks_offset);
785 qemu_put_be64s(f, &ticks_per_sec);
788 static int timer_load(QEMUFile *f, void *opaque, int version_id)
792 if (cpu_ticks_enabled) {
795 qemu_get_be64s(f, &cpu_ticks_offset);
796 qemu_get_be64s(f, &ticks_per_sec);
801 void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
802 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
804 static void host_alarm_handler(int host_signum)
808 #define DISP_FREQ 1000
810 static int64_t delta_min = INT64_MAX;
811 static int64_t delta_max, delta_cum, last_clock, delta, ti;
813 ti = qemu_get_clock(vm_clock);
814 if (last_clock != 0) {
815 delta = ti - last_clock;
816 if (delta < delta_min)
818 if (delta > delta_max)
821 if (++count == DISP_FREQ) {
822 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
823 muldiv64(delta_min, 1000000, ticks_per_sec),
824 muldiv64(delta_max, 1000000, ticks_per_sec),
825 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
826 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
828 delta_min = INT64_MAX;
836 if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
837 qemu_get_clock(vm_clock)) ||
838 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
839 qemu_get_clock(rt_clock))) {
840 /* stop the cpu because a timer occured */
841 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
847 #if defined(__linux__)
849 #define RTC_FREQ 1024
853 static int start_rtc_timer(void)
855 rtc_fd = open("/dev/rtc", O_RDONLY);
858 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
859 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
860 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
861 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
864 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
869 pit_min_timer_count = PIT_FREQ / RTC_FREQ;
875 static int start_rtc_timer(void)
880 #endif /* !defined(__linux__) */
882 #endif /* !defined(_WIN32) */
884 static void init_timers(void)
886 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
887 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
892 timerID = timeSetEvent(10, // interval (ms)
894 host_alarm_handler, // function
895 (DWORD)&count, // user parameter
896 TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
898 perror("failed timer alarm");
902 pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
905 struct sigaction act;
906 struct itimerval itv;
908 /* get times() syscall frequency */
909 timer_freq = sysconf(_SC_CLK_TCK);
912 sigfillset(&act.sa_mask);
914 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
915 act.sa_flags |= SA_ONSTACK;
917 act.sa_handler = host_alarm_handler;
918 sigaction(SIGALRM, &act, NULL);
920 itv.it_interval.tv_sec = 0;
921 itv.it_interval.tv_usec = 1000;
922 itv.it_value.tv_sec = 0;
923 itv.it_value.tv_usec = 10 * 1000;
924 setitimer(ITIMER_REAL, &itv, NULL);
925 /* we probe the tick duration of the kernel to inform the user if
926 the emulated kernel requested a too high timer frequency */
927 getitimer(ITIMER_REAL, &itv);
929 #if defined(__linux__)
930 if (itv.it_interval.tv_usec > 1000) {
931 /* try to use /dev/rtc to have a faster timer */
932 if (start_rtc_timer() < 0)
935 itv.it_interval.tv_sec = 0;
936 itv.it_interval.tv_usec = 0;
937 itv.it_value.tv_sec = 0;
938 itv.it_value.tv_usec = 0;
939 setitimer(ITIMER_REAL, &itv, NULL);
942 sigaction(SIGIO, &act, NULL);
943 fcntl(rtc_fd, F_SETFL, O_ASYNC);
944 fcntl(rtc_fd, F_SETOWN, getpid());
946 #endif /* defined(__linux__) */
949 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec *
956 void quit_timers(void)
959 timeKillEvent(timerID);
963 /***********************************************************/
964 /* character device */
966 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
968 return s->chr_write(s, buf, len);
971 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
976 vsnprintf(buf, sizeof(buf), fmt, ap);
977 qemu_chr_write(s, buf, strlen(buf));
981 void qemu_chr_send_event(CharDriverState *s, int event)
983 if (s->chr_send_event)
984 s->chr_send_event(s, event);
987 void qemu_chr_add_read_handler(CharDriverState *s,
988 IOCanRWHandler *fd_can_read,
989 IOReadHandler *fd_read, void *opaque)
991 s->chr_add_read_handler(s, fd_can_read, fd_read, opaque);
994 void qemu_chr_add_event_handler(CharDriverState *s, IOEventHandler *chr_event)
996 s->chr_event = chr_event;
999 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1004 static void null_chr_add_read_handler(CharDriverState *chr,
1005 IOCanRWHandler *fd_can_read,
1006 IOReadHandler *fd_read, void *opaque)
1010 CharDriverState *qemu_chr_open_null(void)
1012 CharDriverState *chr;
1014 chr = qemu_mallocz(sizeof(CharDriverState));
1017 chr->chr_write = null_chr_write;
1018 chr->chr_add_read_handler = null_chr_add_read_handler;
1026 /* for nographic stdio only */
1027 IOCanRWHandler *fd_can_read;
1028 IOReadHandler *fd_read;
1032 #define STDIO_MAX_CLIENTS 2
1034 static int stdio_nb_clients;
1035 static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS];
1037 static int unix_write(int fd, const uint8_t *buf, int len1)
1043 ret = write(fd, buf, len);
1045 if (errno != EINTR && errno != EAGAIN)
1047 } else if (ret == 0) {
1057 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1059 FDCharDriver *s = chr->opaque;
1060 return unix_write(s->fd_out, buf, len);
1063 static void fd_chr_add_read_handler(CharDriverState *chr,
1064 IOCanRWHandler *fd_can_read,
1065 IOReadHandler *fd_read, void *opaque)
1067 FDCharDriver *s = chr->opaque;
1069 if (nographic && s->fd_in == 0) {
1070 s->fd_can_read = fd_can_read;
1071 s->fd_read = fd_read;
1072 s->fd_opaque = opaque;
1074 qemu_add_fd_read_handler(s->fd_in, fd_can_read, fd_read, opaque);
1078 /* open a character device to a unix fd */
1079 CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
1081 CharDriverState *chr;
1084 chr = qemu_mallocz(sizeof(CharDriverState));
1087 s = qemu_mallocz(sizeof(FDCharDriver));
1095 chr->chr_write = fd_chr_write;
1096 chr->chr_add_read_handler = fd_chr_add_read_handler;
1100 /* for STDIO, we handle the case where several clients use it
1103 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1105 static int term_got_escape, client_index;
1107 void term_print_help(void)
1110 "C-a h print this help\n"
1111 "C-a x exit emulator\n"
1112 "C-a s save disk data back to file (if -snapshot)\n"
1113 "C-a b send break (magic sysrq)\n"
1114 "C-a c switch between console and monitor\n"
1115 "C-a C-a send C-a\n"
1119 /* called when a char is received */
1120 static void stdio_received_byte(int ch)
1122 if (term_got_escape) {
1123 term_got_escape = 0;
1134 for (i = 0; i < MAX_DISKS; i++) {
1136 bdrv_commit(bs_table[i]);
1141 if (client_index < stdio_nb_clients) {
1142 CharDriverState *chr;
1145 chr = stdio_clients[client_index];
1147 chr->chr_event(s->fd_opaque, CHR_EVENT_BREAK);
1152 if (client_index >= stdio_nb_clients)
1154 if (client_index == 0) {
1155 /* send a new line in the monitor to get the prompt */
1163 } else if (ch == TERM_ESCAPE) {
1164 term_got_escape = 1;
1167 if (client_index < stdio_nb_clients) {
1169 CharDriverState *chr;
1172 chr = stdio_clients[client_index];
1175 /* XXX: should queue the char if the device is not
1177 if (s->fd_can_read(s->fd_opaque) > 0)
1178 s->fd_read(s->fd_opaque, buf, 1);
1183 static int stdio_can_read(void *opaque)
1185 /* XXX: not strictly correct */
1189 static void stdio_read(void *opaque, const uint8_t *buf, int size)
1192 for(i = 0; i < size; i++)
1193 stdio_received_byte(buf[i]);
1196 /* init terminal so that we can grab keys */
1197 static struct termios oldtty;
1198 static int old_fd0_flags;
1200 static void term_exit(void)
1202 tcsetattr (0, TCSANOW, &oldtty);
1203 fcntl(0, F_SETFL, old_fd0_flags);
1206 static void term_init(void)
1210 tcgetattr (0, &tty);
1212 old_fd0_flags = fcntl(0, F_GETFL);
1214 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1215 |INLCR|IGNCR|ICRNL|IXON);
1216 tty.c_oflag |= OPOST;
1217 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1218 /* if graphical mode, we allow Ctrl-C handling */
1220 tty.c_lflag &= ~ISIG;
1221 tty.c_cflag &= ~(CSIZE|PARENB);
1224 tty.c_cc[VTIME] = 0;
1226 tcsetattr (0, TCSANOW, &tty);
1230 fcntl(0, F_SETFL, O_NONBLOCK);
1233 CharDriverState *qemu_chr_open_stdio(void)
1235 CharDriverState *chr;
1238 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
1240 chr = qemu_chr_open_fd(0, 1);
1241 if (stdio_nb_clients == 0)
1242 qemu_add_fd_read_handler(0, stdio_can_read, stdio_read, NULL);
1243 client_index = stdio_nb_clients;
1245 if (stdio_nb_clients != 0)
1247 chr = qemu_chr_open_fd(0, 1);
1249 stdio_clients[stdio_nb_clients++] = chr;
1250 if (stdio_nb_clients == 1) {
1251 /* set the terminal in raw mode */
1257 #if defined(__linux__)
1258 CharDriverState *qemu_chr_open_pty(void)
1260 char slave_name[1024];
1261 int master_fd, slave_fd;
1263 /* Not satisfying */
1264 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
1267 fprintf(stderr, "char device redirected to %s\n", slave_name);
1268 return qemu_chr_open_fd(master_fd, master_fd);
1271 CharDriverState *qemu_chr_open_pty(void)
1277 #endif /* !defined(_WIN32) */
1279 CharDriverState *qemu_chr_open(const char *filename)
1281 if (!strcmp(filename, "vc")) {
1282 return text_console_init(&display_state);
1283 } else if (!strcmp(filename, "null")) {
1284 return qemu_chr_open_null();
1287 if (!strcmp(filename, "pty")) {
1288 return qemu_chr_open_pty();
1289 } else if (!strcmp(filename, "stdio")) {
1290 return qemu_chr_open_stdio();
1298 /***********************************************************/
1299 /* Linux network device redirectors */
1301 void hex_dump(FILE *f, const uint8_t *buf, int size)
1305 for(i=0;i<size;i+=16) {
1309 fprintf(f, "%08x ", i);
1312 fprintf(f, " %02x", buf[i+j]);
1317 for(j=0;j<len;j++) {
1319 if (c < ' ' || c > '~')
1321 fprintf(f, "%c", c);
1327 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1329 nd->send_packet(nd, buf, size);
1332 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
1333 IOReadHandler *fd_read, void *opaque)
1335 nd->add_read_packet(nd, fd_can_read, fd_read, opaque);
1338 /* dummy network adapter */
1340 static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1344 static void dummy_add_read_packet(NetDriverState *nd,
1345 IOCanRWHandler *fd_can_read,
1346 IOReadHandler *fd_read, void *opaque)
1350 static int net_dummy_init(NetDriverState *nd)
1352 nd->send_packet = dummy_send_packet;
1353 nd->add_read_packet = dummy_add_read_packet;
1354 pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy");
1358 #if defined(CONFIG_SLIRP)
1360 /* slirp network adapter */
1362 static void *slirp_fd_opaque;
1363 static IOCanRWHandler *slirp_fd_can_read;
1364 static IOReadHandler *slirp_fd_read;
1365 static int slirp_inited;
1367 int slirp_can_output(void)
1369 return slirp_fd_can_read(slirp_fd_opaque);
1372 void slirp_output(const uint8_t *pkt, int pkt_len)
1375 printf("output:\n");
1376 hex_dump(stdout, pkt, pkt_len);
1378 slirp_fd_read(slirp_fd_opaque, pkt, pkt_len);
1381 static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1385 hex_dump(stdout, buf, size);
1387 slirp_input(buf, size);
1390 static void slirp_add_read_packet(NetDriverState *nd,
1391 IOCanRWHandler *fd_can_read,
1392 IOReadHandler *fd_read, void *opaque)
1394 slirp_fd_opaque = opaque;
1395 slirp_fd_can_read = fd_can_read;
1396 slirp_fd_read = fd_read;
1399 static int net_slirp_init(NetDriverState *nd)
1401 if (!slirp_inited) {
1405 nd->send_packet = slirp_send_packet;
1406 nd->add_read_packet = slirp_add_read_packet;
1407 pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp");
1411 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
1416 p1 = strchr(p, sep);
1422 if (len > buf_size - 1)
1424 memcpy(buf, p, len);
1431 static void net_slirp_redir(const char *redir_str)
1436 struct in_addr guest_addr;
1437 int host_port, guest_port;
1439 if (!slirp_inited) {
1445 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1447 if (!strcmp(buf, "tcp")) {
1449 } else if (!strcmp(buf, "udp")) {
1455 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1457 host_port = strtol(buf, &r, 0);
1461 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1463 if (buf[0] == '\0') {
1464 pstrcpy(buf, sizeof(buf), "10.0.2.15");
1466 if (!inet_aton(buf, &guest_addr))
1469 guest_port = strtol(p, &r, 0);
1473 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
1474 fprintf(stderr, "qemu: could not set up redirection\n");
1479 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
1487 static void smb_exit(void)
1491 char filename[1024];
1493 /* erase all the files in the directory */
1494 d = opendir(smb_dir);
1499 if (strcmp(de->d_name, ".") != 0 &&
1500 strcmp(de->d_name, "..") != 0) {
1501 snprintf(filename, sizeof(filename), "%s/%s",
1502 smb_dir, de->d_name);
1510 /* automatic user mode samba server configuration */
1511 void net_slirp_smb(const char *exported_dir)
1513 char smb_conf[1024];
1514 char smb_cmdline[1024];
1517 if (!slirp_inited) {
1522 /* XXX: better tmp dir construction */
1523 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
1524 if (mkdir(smb_dir, 0700) < 0) {
1525 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
1528 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
1530 f = fopen(smb_conf, "w");
1532 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
1537 "pid directory=%s\n"
1538 "lock directory=%s\n"
1539 "log file=%s/log.smbd\n"
1540 "smb passwd file=%s/smbpasswd\n"
1541 "security = share\n"
1555 snprintf(smb_cmdline, sizeof(smb_cmdline), "/usr/sbin/smbd -s %s",
1558 slirp_add_exec(0, smb_cmdline, 4, 139);
1561 #endif /* !defined(_WIN32) */
1563 #endif /* CONFIG_SLIRP */
1565 #if !defined(_WIN32)
1567 static int tun_open(char *ifname, int ifname_size)
1573 fd = open("/dev/tap", O_RDWR);
1575 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1580 dev = devname(s.st_rdev, S_IFCHR);
1581 pstrcpy(ifname, ifname_size, dev);
1583 fcntl(fd, F_SETFL, O_NONBLOCK);
1587 static int tun_open(char *ifname, int ifname_size)
1592 fd = open("/dev/net/tun", O_RDWR);
1594 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1597 memset(&ifr, 0, sizeof(ifr));
1598 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1599 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
1600 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1602 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1606 printf("Connected to host network interface: %s\n", ifr.ifr_name);
1607 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1608 fcntl(fd, F_SETFL, O_NONBLOCK);
1613 static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1615 write(nd->fd, buf, size);
1618 static void tun_add_read_packet(NetDriverState *nd,
1619 IOCanRWHandler *fd_can_read,
1620 IOReadHandler *fd_read, void *opaque)
1622 qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque);
1625 static int net_tun_init(NetDriverState *nd)
1631 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
1635 /* try to launch network init script */
1640 *parg++ = network_script;
1641 *parg++ = nd->ifname;
1643 execv(network_script, args);
1646 while (waitpid(pid, &status, 0) != pid);
1647 if (!WIFEXITED(status) ||
1648 WEXITSTATUS(status) != 0) {
1649 fprintf(stderr, "%s: could not launch network script\n",
1653 nd->send_packet = tun_send_packet;
1654 nd->add_read_packet = tun_add_read_packet;
1658 static int net_fd_init(NetDriverState *nd, int fd)
1661 nd->send_packet = tun_send_packet;
1662 nd->add_read_packet = tun_add_read_packet;
1663 pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
1667 #endif /* !_WIN32 */
1669 /***********************************************************/
1672 static char *pid_filename;
1674 /* Remove PID file. Called on normal exit */
1676 static void remove_pidfile(void)
1678 unlink (pid_filename);
1681 static void create_pidfile(const char *filename)
1683 struct stat pidstat;
1686 /* Try to write our PID to the named file */
1687 if (stat(filename, &pidstat) < 0) {
1688 if (errno == ENOENT) {
1689 if ((f = fopen (filename, "w")) == NULL) {
1690 perror("Opening pidfile");
1693 fprintf(f, "%d\n", getpid());
1695 pid_filename = qemu_strdup(filename);
1696 if (!pid_filename) {
1697 fprintf(stderr, "Could not save PID filename");
1700 atexit(remove_pidfile);
1703 fprintf(stderr, "%s already exists. Remove it and try again.\n",
1709 /***********************************************************/
1712 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
1716 static void dumb_resize(DisplayState *ds, int w, int h)
1720 static void dumb_refresh(DisplayState *ds)
1722 vga_update_display();
1725 void dumb_display_init(DisplayState *ds)
1730 ds->dpy_update = dumb_update;
1731 ds->dpy_resize = dumb_resize;
1732 ds->dpy_refresh = dumb_refresh;
1735 #if !defined(CONFIG_SOFTMMU)
1736 /***********************************************************/
1737 /* cpu signal handler */
1738 static void host_segv_handler(int host_signum, siginfo_t *info,
1741 if (cpu_signal_handler(host_signum, info, puc))
1743 if (stdio_nb_clients > 0)
1749 /***********************************************************/
1752 #define MAX_IO_HANDLERS 64
1754 typedef struct IOHandlerRecord {
1756 IOCanRWHandler *fd_can_read;
1757 IOReadHandler *fd_read;
1759 /* temporary data */
1762 struct IOHandlerRecord *next;
1765 static IOHandlerRecord *first_io_handler;
1767 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
1768 IOReadHandler *fd_read, void *opaque)
1770 IOHandlerRecord *ioh;
1772 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1776 ioh->fd_can_read = fd_can_read;
1777 ioh->fd_read = fd_read;
1778 ioh->opaque = opaque;
1779 ioh->next = first_io_handler;
1780 first_io_handler = ioh;
1784 void qemu_del_fd_read_handler(int fd)
1786 IOHandlerRecord **pioh, *ioh;
1788 pioh = &first_io_handler;
1793 if (ioh->fd == fd) {
1801 /***********************************************************/
1802 /* savevm/loadvm support */
1804 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
1806 fwrite(buf, 1, size, f);
1809 void qemu_put_byte(QEMUFile *f, int v)
1814 void qemu_put_be16(QEMUFile *f, unsigned int v)
1816 qemu_put_byte(f, v >> 8);
1817 qemu_put_byte(f, v);
1820 void qemu_put_be32(QEMUFile *f, unsigned int v)
1822 qemu_put_byte(f, v >> 24);
1823 qemu_put_byte(f, v >> 16);
1824 qemu_put_byte(f, v >> 8);
1825 qemu_put_byte(f, v);
1828 void qemu_put_be64(QEMUFile *f, uint64_t v)
1830 qemu_put_be32(f, v >> 32);
1831 qemu_put_be32(f, v);
1834 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
1836 return fread(buf, 1, size, f);
1839 int qemu_get_byte(QEMUFile *f)
1849 unsigned int qemu_get_be16(QEMUFile *f)
1852 v = qemu_get_byte(f) << 8;
1853 v |= qemu_get_byte(f);
1857 unsigned int qemu_get_be32(QEMUFile *f)
1860 v = qemu_get_byte(f) << 24;
1861 v |= qemu_get_byte(f) << 16;
1862 v |= qemu_get_byte(f) << 8;
1863 v |= qemu_get_byte(f);
1867 uint64_t qemu_get_be64(QEMUFile *f)
1870 v = (uint64_t)qemu_get_be32(f) << 32;
1871 v |= qemu_get_be32(f);
1875 int64_t qemu_ftell(QEMUFile *f)
1880 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
1882 if (fseek(f, pos, whence) < 0)
1887 typedef struct SaveStateEntry {
1891 SaveStateHandler *save_state;
1892 LoadStateHandler *load_state;
1894 struct SaveStateEntry *next;
1897 static SaveStateEntry *first_se;
1899 int register_savevm(const char *idstr,
1902 SaveStateHandler *save_state,
1903 LoadStateHandler *load_state,
1906 SaveStateEntry *se, **pse;
1908 se = qemu_malloc(sizeof(SaveStateEntry));
1911 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1912 se->instance_id = instance_id;
1913 se->version_id = version_id;
1914 se->save_state = save_state;
1915 se->load_state = load_state;
1916 se->opaque = opaque;
1919 /* add at the end of list */
1921 while (*pse != NULL)
1922 pse = &(*pse)->next;
1927 #define QEMU_VM_FILE_MAGIC 0x5145564d
1928 #define QEMU_VM_FILE_VERSION 0x00000001
1930 int qemu_savevm(const char *filename)
1934 int len, len_pos, cur_pos, saved_vm_running, ret;
1936 saved_vm_running = vm_running;
1939 f = fopen(filename, "wb");
1945 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1946 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1948 for(se = first_se; se != NULL; se = se->next) {
1950 len = strlen(se->idstr);
1951 qemu_put_byte(f, len);
1952 qemu_put_buffer(f, se->idstr, len);
1954 qemu_put_be32(f, se->instance_id);
1955 qemu_put_be32(f, se->version_id);
1957 /* record size: filled later */
1959 qemu_put_be32(f, 0);
1961 se->save_state(f, se->opaque);
1963 /* fill record size */
1965 len = ftell(f) - len_pos - 4;
1966 fseek(f, len_pos, SEEK_SET);
1967 qemu_put_be32(f, len);
1968 fseek(f, cur_pos, SEEK_SET);
1974 if (saved_vm_running)
1979 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1983 for(se = first_se; se != NULL; se = se->next) {
1984 if (!strcmp(se->idstr, idstr) &&
1985 instance_id == se->instance_id)
1991 int qemu_loadvm(const char *filename)
1995 int len, cur_pos, ret, instance_id, record_len, version_id;
1996 int saved_vm_running;
2000 saved_vm_running = vm_running;
2003 f = fopen(filename, "rb");
2009 v = qemu_get_be32(f);
2010 if (v != QEMU_VM_FILE_MAGIC)
2012 v = qemu_get_be32(f);
2013 if (v != QEMU_VM_FILE_VERSION) {
2020 #if defined (DO_TB_FLUSH)
2021 tb_flush(global_env);
2023 len = qemu_get_byte(f);
2026 qemu_get_buffer(f, idstr, len);
2028 instance_id = qemu_get_be32(f);
2029 version_id = qemu_get_be32(f);
2030 record_len = qemu_get_be32(f);
2032 printf("idstr=%s instance=0x%x version=%d len=%d\n",
2033 idstr, instance_id, version_id, record_len);
2036 se = find_se(idstr, instance_id);
2038 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
2039 instance_id, idstr);
2041 ret = se->load_state(f, se->opaque, version_id);
2043 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2044 instance_id, idstr);
2047 /* always seek to exact end of record */
2048 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
2053 if (saved_vm_running)
2058 /***********************************************************/
2059 /* cpu save/restore */
2061 #if defined(TARGET_I386)
2063 static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
2065 qemu_put_be32(f, dt->selector);
2066 qemu_put_be32(f, (uint32_t)dt->base);
2067 qemu_put_be32(f, dt->limit);
2068 qemu_put_be32(f, dt->flags);
2071 static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
2073 dt->selector = qemu_get_be32(f);
2074 dt->base = (uint8_t *)qemu_get_be32(f);
2075 dt->limit = qemu_get_be32(f);
2076 dt->flags = qemu_get_be32(f);
2079 void cpu_save(QEMUFile *f, void *opaque)
2081 CPUState *env = opaque;
2082 uint16_t fptag, fpus, fpuc;
2086 for(i = 0; i < 8; i++)
2087 qemu_put_be32s(f, &env->regs[i]);
2088 qemu_put_be32s(f, &env->eip);
2089 qemu_put_be32s(f, &env->eflags);
2090 qemu_put_be32s(f, &env->eflags);
2091 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
2092 qemu_put_be32s(f, &hflags);
2096 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2098 for (i=7; i>=0; i--) {
2100 if (env->fptags[i]) {
2105 qemu_put_be16s(f, &fpuc);
2106 qemu_put_be16s(f, &fpus);
2107 qemu_put_be16s(f, &fptag);
2109 for(i = 0; i < 8; i++) {
2112 cpu_get_fp80(&mant, &exp, env->fpregs[i]);
2113 qemu_put_be64(f, mant);
2114 qemu_put_be16(f, exp);
2117 for(i = 0; i < 6; i++)
2118 cpu_put_seg(f, &env->segs[i]);
2119 cpu_put_seg(f, &env->ldt);
2120 cpu_put_seg(f, &env->tr);
2121 cpu_put_seg(f, &env->gdt);
2122 cpu_put_seg(f, &env->idt);
2124 qemu_put_be32s(f, &env->sysenter_cs);
2125 qemu_put_be32s(f, &env->sysenter_esp);
2126 qemu_put_be32s(f, &env->sysenter_eip);
2128 qemu_put_be32s(f, &env->cr[0]);
2129 qemu_put_be32s(f, &env->cr[2]);
2130 qemu_put_be32s(f, &env->cr[3]);
2131 qemu_put_be32s(f, &env->cr[4]);
2133 for(i = 0; i < 8; i++)
2134 qemu_put_be32s(f, &env->dr[i]);
2137 qemu_put_be32s(f, &env->a20_mask);
2140 int cpu_load(QEMUFile *f, void *opaque, int version_id)
2142 CPUState *env = opaque;
2145 uint16_t fpus, fpuc, fptag;
2147 if (version_id != 2)
2149 for(i = 0; i < 8; i++)
2150 qemu_get_be32s(f, &env->regs[i]);
2151 qemu_get_be32s(f, &env->eip);
2152 qemu_get_be32s(f, &env->eflags);
2153 qemu_get_be32s(f, &env->eflags);
2154 qemu_get_be32s(f, &hflags);
2156 qemu_get_be16s(f, &fpuc);
2157 qemu_get_be16s(f, &fpus);
2158 qemu_get_be16s(f, &fptag);
2160 for(i = 0; i < 8; i++) {
2163 mant = qemu_get_be64(f);
2164 exp = qemu_get_be16(f);
2165 env->fpregs[i] = cpu_set_fp80(mant, exp);
2169 env->fpstt = (fpus >> 11) & 7;
2170 env->fpus = fpus & ~0x3800;
2171 for(i = 0; i < 8; i++) {
2172 env->fptags[i] = ((fptag & 3) == 3);
2176 for(i = 0; i < 6; i++)
2177 cpu_get_seg(f, &env->segs[i]);
2178 cpu_get_seg(f, &env->ldt);
2179 cpu_get_seg(f, &env->tr);
2180 cpu_get_seg(f, &env->gdt);
2181 cpu_get_seg(f, &env->idt);
2183 qemu_get_be32s(f, &env->sysenter_cs);
2184 qemu_get_be32s(f, &env->sysenter_esp);
2185 qemu_get_be32s(f, &env->sysenter_eip);
2187 qemu_get_be32s(f, &env->cr[0]);
2188 qemu_get_be32s(f, &env->cr[2]);
2189 qemu_get_be32s(f, &env->cr[3]);
2190 qemu_get_be32s(f, &env->cr[4]);
2192 for(i = 0; i < 8; i++)
2193 qemu_get_be32s(f, &env->dr[i]);
2196 qemu_get_be32s(f, &env->a20_mask);
2198 /* XXX: compute hflags from scratch, except for CPL and IIF */
2199 env->hflags = hflags;
2204 #elif defined(TARGET_PPC)
2205 void cpu_save(QEMUFile *f, void *opaque)
2209 int cpu_load(QEMUFile *f, void *opaque, int version_id)
2213 #elif defined(TARGET_SPARC)
2214 void cpu_save(QEMUFile *f, void *opaque)
2218 int cpu_load(QEMUFile *f, void *opaque, int version_id)
2224 #warning No CPU save/restore functions
2228 /***********************************************************/
2229 /* ram save/restore */
2231 /* we just avoid storing empty pages */
2232 static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
2237 for(i = 1; i < len; i++) {
2241 qemu_put_byte(f, 1);
2242 qemu_put_byte(f, v);
2245 qemu_put_byte(f, 0);
2246 qemu_put_buffer(f, buf, len);
2249 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
2253 v = qemu_get_byte(f);
2256 if (qemu_get_buffer(f, buf, len) != len)
2260 v = qemu_get_byte(f);
2261 memset(buf, v, len);
2269 static void ram_save(QEMUFile *f, void *opaque)
2272 qemu_put_be32(f, phys_ram_size);
2273 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
2274 ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
2278 static int ram_load(QEMUFile *f, void *opaque, int version_id)
2282 if (version_id != 1)
2284 if (qemu_get_be32(f) != phys_ram_size)
2286 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
2287 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
2294 /***********************************************************/
2295 /* main execution loop */
2297 void gui_update(void *opaque)
2299 display_state.dpy_refresh(&display_state);
2300 qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
2303 /* XXX: support several handlers */
2304 VMStopHandler *vm_stop_cb;
2305 VMStopHandler *vm_stop_opaque;
2307 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
2310 vm_stop_opaque = opaque;
2314 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
2327 void vm_stop(int reason)
2330 cpu_disable_ticks();
2334 vm_stop_cb(vm_stop_opaque, reason);
2340 /* reset/shutdown handler */
2342 typedef struct QEMUResetEntry {
2343 QEMUResetHandler *func;
2345 struct QEMUResetEntry *next;
2348 static QEMUResetEntry *first_reset_entry;
2349 static int reset_requested;
2350 static int shutdown_requested;
2352 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
2354 QEMUResetEntry **pre, *re;
2356 pre = &first_reset_entry;
2357 while (*pre != NULL)
2358 pre = &(*pre)->next;
2359 re = qemu_mallocz(sizeof(QEMUResetEntry));
2361 re->opaque = opaque;
2366 void qemu_system_reset(void)
2370 /* reset all devices */
2371 for(re = first_reset_entry; re != NULL; re = re->next) {
2372 re->func(re->opaque);
2376 void qemu_system_reset_request(void)
2378 reset_requested = 1;
2379 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
2382 void qemu_system_shutdown_request(void)
2384 shutdown_requested = 1;
2385 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
2388 static void main_cpu_reset(void *opaque)
2391 CPUState *env = opaque;
2396 void main_loop_wait(int timeout)
2399 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
2400 IOHandlerRecord *ioh, *ioh_next;
2410 /* poll any events */
2411 /* XXX: separate device handlers from system ones */
2413 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2414 if (!ioh->fd_can_read) {
2417 pf->events = POLLIN;
2421 max_size = ioh->fd_can_read(ioh->opaque);
2423 if (max_size > sizeof(buf))
2424 max_size = sizeof(buf);
2426 pf->events = POLLIN;
2433 ioh->max_size = max_size;
2436 ret = poll(ufds, pf - ufds, timeout);
2438 /* XXX: better handling of removal */
2439 for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
2440 ioh_next = ioh->next;
2443 if (pf->revents & POLLIN) {
2444 if (ioh->max_size == 0) {
2445 /* just a read event */
2446 ioh->fd_read(ioh->opaque, NULL, 0);
2448 n = read(ioh->fd, buf, ioh->max_size);
2450 ioh->fd_read(ioh->opaque, buf, n);
2451 } else if (errno != EAGAIN) {
2452 ioh->fd_read(ioh->opaque, NULL, -errno);
2459 #endif /* !defined(_WIN32) */
2460 #if defined(CONFIG_SLIRP)
2461 /* XXX: merge with poll() */
2463 fd_set rfds, wfds, xfds;
2471 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2474 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
2476 slirp_select_poll(&rfds, &wfds, &xfds);
2482 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
2483 qemu_get_clock(vm_clock));
2484 /* run dma transfers, if any */
2488 /* real time timers */
2489 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
2490 qemu_get_clock(rt_clock));
2496 CPUState *env = global_env;
2500 ret = cpu_exec(env);
2501 if (shutdown_requested) {
2502 ret = EXCP_INTERRUPT;
2505 if (reset_requested) {
2506 reset_requested = 0;
2507 qemu_system_reset();
2508 ret = EXCP_INTERRUPT;
2510 if (ret == EXCP_DEBUG) {
2511 vm_stop(EXCP_DEBUG);
2513 /* if hlt instruction, we wait until the next IRQ */
2514 /* XXX: use timeout computed from timers */
2515 if (ret == EXCP_HLT)
2522 main_loop_wait(timeout);
2524 cpu_disable_ticks();
2530 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n"
2531 "usage: %s [options] [disk_image]\n"
2533 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2535 "Standard options:\n"
2536 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
2537 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
2538 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
2539 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
2540 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
2541 "-snapshot write to temporary files instead of disk image files\n"
2542 "-m megs set virtual RAM size to megs MB [default=%d]\n"
2543 "-nographic disable graphical output and redirect serial I/Os to console\n"
2544 "-enable-audio enable audio support\n"
2545 "-localtime set the real time clock to local time [default=utc]\n"
2546 "-full-screen start in full screen\n"
2548 "-prep Simulate a PREP system (default is PowerMAC)\n"
2549 "-g WxH[xDEPTH] Set the initial VGA graphic mode\n"
2552 "Network options:\n"
2553 "-nics n simulate 'n' network cards [default=1]\n"
2554 "-macaddr addr set the mac address of the first interface\n"
2555 "-n script set tap/tun network init script [default=%s]\n"
2556 "-tun-fd fd use this fd as already opened tap/tun interface\n"
2558 "-user-net use user mode network stack [default if no tap/tun script]\n"
2559 "-tftp prefix allow tftp access to files starting with prefix [-user-net]\n"
2561 "-smb dir allow SMB access to files in 'dir' [-user-net]\n"
2563 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
2564 " redirect TCP or UDP connections from host to guest [-user-net]\n"
2566 "-dummy-net use dummy network stack\n"
2568 "Linux boot specific:\n"
2569 "-kernel bzImage use 'bzImage' as kernel image\n"
2570 "-append cmdline use 'cmdline' as kernel command line\n"
2571 "-initrd file use 'file' as initial ram disk\n"
2573 "Debug/Expert options:\n"
2574 "-monitor dev redirect the monitor to char device 'dev'\n"
2575 "-serial dev redirect the serial port to char device 'dev'\n"
2576 "-pidfile file Write PID to 'file'\n"
2577 "-S freeze CPU at startup (use 'c' to start execution)\n"
2578 "-s wait gdb connection to port %d\n"
2579 "-p port change gdb connection port\n"
2580 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
2581 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
2582 " translation (t=none or lba) (usually qemu can guess them)\n"
2583 "-L path set the directory for the BIOS and VGA BIOS\n"
2584 #ifdef USE_CODE_COPY
2585 "-no-code-copy disable code copy acceleration\n"
2588 "-isa simulate an ISA-only system (default is PCI system)\n"
2589 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
2590 " (default is CL-GD5446 PCI VGA)\n"
2592 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
2594 "During emulation, the following keys are useful:\n"
2595 "ctrl-alt-f toggle full screen\n"
2596 "ctrl-alt-n switch to virtual console 'n'\n"
2597 "ctrl-alt toggle mouse and keyboard grab\n"
2599 "When using -nographic, press 'ctrl-a h' to get some help.\n"
2601 #ifdef CONFIG_SOFTMMU
2607 DEFAULT_NETWORK_SCRIPT,
2608 DEFAULT_GDBSTUB_PORT,
2610 #ifndef CONFIG_SOFTMMU
2612 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
2613 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
2619 #define HAS_ARG 0x0001
2632 QEMU_OPTION_snapshot,
2634 QEMU_OPTION_nographic,
2635 QEMU_OPTION_enable_audio,
2638 QEMU_OPTION_macaddr,
2641 QEMU_OPTION_user_net,
2645 QEMU_OPTION_dummy_net,
2657 QEMU_OPTION_no_code_copy,
2661 QEMU_OPTION_localtime,
2662 QEMU_OPTION_cirrusvga,
2664 QEMU_OPTION_std_vga,
2665 QEMU_OPTION_monitor,
2668 QEMU_OPTION_full_screen,
2669 QEMU_OPTION_pidfile,
2672 typedef struct QEMUOption {
2678 const QEMUOption qemu_options[] = {
2679 { "h", 0, QEMU_OPTION_h },
2681 { "fda", HAS_ARG, QEMU_OPTION_fda },
2682 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
2683 { "hda", HAS_ARG, QEMU_OPTION_hda },
2684 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
2685 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
2686 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
2687 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
2688 { "boot", HAS_ARG, QEMU_OPTION_boot },
2689 { "snapshot", 0, QEMU_OPTION_snapshot },
2690 { "m", HAS_ARG, QEMU_OPTION_m },
2691 { "nographic", 0, QEMU_OPTION_nographic },
2692 { "enable-audio", 0, QEMU_OPTION_enable_audio },
2694 { "nics", HAS_ARG, QEMU_OPTION_nics},
2695 { "macaddr", HAS_ARG, QEMU_OPTION_macaddr},
2696 { "n", HAS_ARG, QEMU_OPTION_n },
2697 { "tun-fd", HAS_ARG, QEMU_OPTION_tun_fd },
2699 { "user-net", 0, QEMU_OPTION_user_net },
2700 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
2702 { "smb", HAS_ARG, QEMU_OPTION_smb },
2704 { "redir", HAS_ARG, QEMU_OPTION_redir },
2706 { "dummy-net", 0, QEMU_OPTION_dummy_net },
2708 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
2709 { "append", HAS_ARG, QEMU_OPTION_append },
2710 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
2712 { "S", 0, QEMU_OPTION_S },
2713 { "s", 0, QEMU_OPTION_s },
2714 { "p", HAS_ARG, QEMU_OPTION_p },
2715 { "d", HAS_ARG, QEMU_OPTION_d },
2716 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
2717 { "L", HAS_ARG, QEMU_OPTION_L },
2718 { "no-code-copy", 0, QEMU_OPTION_no_code_copy },
2720 { "prep", 0, QEMU_OPTION_prep },
2721 { "g", 1, QEMU_OPTION_g },
2723 { "localtime", 0, QEMU_OPTION_localtime },
2724 { "isa", 0, QEMU_OPTION_isa },
2725 { "std-vga", 0, QEMU_OPTION_std_vga },
2726 { "monitor", 1, QEMU_OPTION_monitor },
2727 { "serial", 1, QEMU_OPTION_serial },
2728 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
2729 { "full-screen", 0, QEMU_OPTION_full_screen },
2730 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
2732 /* temporary options */
2733 { "pci", 0, QEMU_OPTION_pci },
2734 { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
2738 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2740 /* this stack is only used during signal handling */
2741 #define SIGNAL_STACK_SIZE 32768
2743 static uint8_t *signal_stack;
2747 /* password input */
2749 static BlockDriverState *get_bdrv(int index)
2751 BlockDriverState *bs;
2754 bs = bs_table[index];
2755 } else if (index < 6) {
2756 bs = fd_table[index - 4];
2763 static void read_passwords(void)
2765 BlockDriverState *bs;
2769 for(i = 0; i < 6; i++) {
2771 if (bs && bdrv_is_encrypted(bs)) {
2772 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs));
2773 for(j = 0; j < 3; j++) {
2774 monitor_readline("Password: ",
2775 1, password, sizeof(password));
2776 if (bdrv_set_key(bs, password) == 0)
2778 term_printf("invalid password\n");
2784 #define NET_IF_TUN 0
2785 #define NET_IF_USER 1
2786 #define NET_IF_DUMMY 2
2788 int main(int argc, char **argv)
2790 #ifdef CONFIG_GDBSTUB
2791 int use_gdbstub, gdbstub_port;
2794 int snapshot, linux_boot;
2796 const char *initrd_filename;
2797 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
2798 const char *kernel_filename, *kernel_cmdline;
2799 DisplayState *ds = &display_state;
2800 int cyls, heads, secs, translation;
2801 int start_emulation = 1;
2803 int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
2805 const char *r, *optarg;
2806 CharDriverState *monitor_hd;
2807 char monitor_device[128];
2808 char serial_devices[MAX_SERIAL_PORTS][128];
2809 int serial_device_index;
2810 const char *loadvm = NULL;
2812 #if !defined(CONFIG_SOFTMMU)
2813 /* we never want that malloc() uses mmap() */
2814 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
2816 initrd_filename = NULL;
2817 for(i = 0; i < MAX_FD; i++)
2818 fd_filename[i] = NULL;
2819 for(i = 0; i < MAX_DISKS; i++)
2820 hd_filename[i] = NULL;
2821 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2822 vga_ram_size = VGA_RAM_SIZE;
2823 bios_size = BIOS_SIZE;
2824 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
2825 #ifdef CONFIG_GDBSTUB
2827 gdbstub_port = DEFAULT_GDBSTUB_PORT;
2831 kernel_filename = NULL;
2832 kernel_cmdline = "";
2834 cyls = heads = secs = 0;
2835 translation = BIOS_ATA_TRANSLATION_AUTO;
2836 pstrcpy(monitor_device, sizeof(monitor_device), "vc");
2838 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc");
2839 for(i = 1; i < MAX_SERIAL_PORTS; i++)
2840 serial_devices[i][0] = '\0';
2841 serial_device_index = 0;
2846 /* default mac address of the first network interface */
2860 hd_filename[0] = argv[optind++];
2862 const QEMUOption *popt;
2865 popt = qemu_options;
2868 fprintf(stderr, "%s: invalid option -- '%s'\n",
2872 if (!strcmp(popt->name, r + 1))
2876 if (popt->flags & HAS_ARG) {
2877 if (optind >= argc) {
2878 fprintf(stderr, "%s: option '%s' requires an argument\n",
2882 optarg = argv[optind++];
2887 switch(popt->index) {
2888 case QEMU_OPTION_initrd:
2889 initrd_filename = optarg;
2891 case QEMU_OPTION_hda:
2892 hd_filename[0] = optarg;
2894 case QEMU_OPTION_hdb:
2895 hd_filename[1] = optarg;
2897 case QEMU_OPTION_snapshot:
2900 case QEMU_OPTION_hdachs:
2904 cyls = strtol(p, (char **)&p, 0);
2905 if (cyls < 1 || cyls > 16383)
2910 heads = strtol(p, (char **)&p, 0);
2911 if (heads < 1 || heads > 16)
2916 secs = strtol(p, (char **)&p, 0);
2917 if (secs < 1 || secs > 63)
2921 if (!strcmp(p, "none"))
2922 translation = BIOS_ATA_TRANSLATION_NONE;
2923 else if (!strcmp(p, "lba"))
2924 translation = BIOS_ATA_TRANSLATION_LBA;
2925 else if (!strcmp(p, "auto"))
2926 translation = BIOS_ATA_TRANSLATION_AUTO;
2929 } else if (*p != '\0') {
2931 fprintf(stderr, "qemu: invalid physical CHS format\n");
2936 case QEMU_OPTION_nographic:
2937 pstrcpy(monitor_device, sizeof(monitor_device), "stdio");
2938 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio");
2941 case QEMU_OPTION_kernel:
2942 kernel_filename = optarg;
2944 case QEMU_OPTION_append:
2945 kernel_cmdline = optarg;
2947 case QEMU_OPTION_tun_fd:
2951 net_if_type = NET_IF_TUN;
2952 if (nb_tun_fds < MAX_NICS) {
2953 fd = strtol(optarg, (char **)&p, 0);
2955 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
2958 tun_fds[nb_tun_fds++] = fd;
2962 case QEMU_OPTION_hdc:
2963 hd_filename[2] = optarg;
2966 case QEMU_OPTION_hdd:
2967 hd_filename[3] = optarg;
2969 case QEMU_OPTION_cdrom:
2970 hd_filename[2] = optarg;
2973 case QEMU_OPTION_boot:
2974 boot_device = optarg[0];
2975 if (boot_device != 'a' &&
2976 boot_device != 'c' && boot_device != 'd') {
2977 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
2981 case QEMU_OPTION_fda:
2982 fd_filename[0] = optarg;
2984 case QEMU_OPTION_fdb:
2985 fd_filename[1] = optarg;
2987 case QEMU_OPTION_no_code_copy:
2988 code_copy_enabled = 0;
2990 case QEMU_OPTION_nics:
2991 nb_nics = atoi(optarg);
2992 if (nb_nics < 0 || nb_nics > MAX_NICS) {
2993 fprintf(stderr, "qemu: invalid number of network interfaces\n");
2997 case QEMU_OPTION_macaddr:
3002 for(i = 0; i < 6; i++) {
3003 macaddr[i] = strtol(p, (char **)&p, 16);
3010 fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
3019 case QEMU_OPTION_tftp:
3020 tftp_prefix = optarg;
3023 case QEMU_OPTION_smb:
3024 net_slirp_smb(optarg);
3027 case QEMU_OPTION_user_net:
3028 net_if_type = NET_IF_USER;
3030 case QEMU_OPTION_redir:
3031 net_slirp_redir(optarg);
3034 case QEMU_OPTION_dummy_net:
3035 net_if_type = NET_IF_DUMMY;
3037 case QEMU_OPTION_enable_audio:
3044 ram_size = atoi(optarg) * 1024 * 1024;
3047 if (ram_size > PHYS_RAM_MAX_SIZE) {
3048 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
3049 PHYS_RAM_MAX_SIZE / (1024 * 1024));
3058 mask = cpu_str_to_log_mask(optarg);
3060 printf("Log items (comma separated):\n");
3061 for(item = cpu_log_items; item->mask != 0; item++) {
3062 printf("%-10s %s\n", item->name, item->help);
3070 pstrcpy(network_script, sizeof(network_script), optarg);
3072 #ifdef CONFIG_GDBSTUB
3077 gdbstub_port = atoi(optarg);
3084 start_emulation = 0;
3086 case QEMU_OPTION_pci:
3089 case QEMU_OPTION_isa:
3092 case QEMU_OPTION_prep:
3095 case QEMU_OPTION_localtime:
3098 case QEMU_OPTION_cirrusvga:
3099 cirrus_vga_enabled = 1;
3101 case QEMU_OPTION_std_vga:
3102 cirrus_vga_enabled = 0;
3109 w = strtol(p, (char **)&p, 10);
3112 fprintf(stderr, "qemu: invalid resolution or depth\n");
3118 h = strtol(p, (char **)&p, 10);
3123 depth = strtol(p, (char **)&p, 10);
3124 if (depth != 8 && depth != 15 && depth != 16 &&
3125 depth != 24 && depth != 32)
3127 } else if (*p == '\0') {
3128 depth = graphic_depth;
3135 graphic_depth = depth;
3138 case QEMU_OPTION_monitor:
3139 pstrcpy(monitor_device, sizeof(monitor_device), optarg);
3141 case QEMU_OPTION_serial:
3142 if (serial_device_index >= MAX_SERIAL_PORTS) {
3143 fprintf(stderr, "qemu: too many serial ports\n");
3146 pstrcpy(serial_devices[serial_device_index],
3147 sizeof(serial_devices[0]), optarg);
3148 serial_device_index++;
3150 case QEMU_OPTION_loadvm:
3153 case QEMU_OPTION_full_screen:
3156 case QEMU_OPTION_pidfile:
3157 create_pidfile(optarg);
3163 linux_boot = (kernel_filename != NULL);
3165 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
3166 fd_filename[0] == '\0')
3169 /* boot to cd by default if no hard disk */
3170 if (hd_filename[0] == '\0' && boot_device == 'c') {
3171 if (fd_filename[0] != '\0')
3177 #if !defined(CONFIG_SOFTMMU)
3178 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
3180 static uint8_t stdout_buf[4096];
3181 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
3184 setvbuf(stdout, NULL, _IOLBF, 0);
3187 /* init host network redirectors */
3188 if (net_if_type == -1) {
3189 net_if_type = NET_IF_TUN;
3190 #if defined(CONFIG_SLIRP)
3191 if (access(network_script, R_OK) < 0) {
3192 net_if_type = NET_IF_USER;
3197 for(i = 0; i < nb_nics; i++) {
3198 NetDriverState *nd = &nd_table[i];
3200 /* init virtual mac address */
3201 nd->macaddr[0] = macaddr[0];
3202 nd->macaddr[1] = macaddr[1];
3203 nd->macaddr[2] = macaddr[2];
3204 nd->macaddr[3] = macaddr[3];
3205 nd->macaddr[4] = macaddr[4];
3206 nd->macaddr[5] = macaddr[5] + i;
3207 switch(net_if_type) {
3208 #if defined(CONFIG_SLIRP)
3213 #if !defined(_WIN32)
3215 if (i < nb_tun_fds) {
3216 net_fd_init(nd, tun_fds[i]);
3218 if (net_tun_init(nd) < 0)
3230 /* init the memory */
3231 phys_ram_size = ram_size + vga_ram_size + bios_size;
3233 #ifdef CONFIG_SOFTMMU
3235 /* mallocs are always aligned on BSD. valloc is better for correctness */
3236 phys_ram_base = valloc(phys_ram_size);
3238 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
3240 if (!phys_ram_base) {
3241 fprintf(stderr, "Could not allocate physical memory\n");
3245 /* as we must map the same page at several addresses, we must use
3250 tmpdir = getenv("QEMU_TMPDIR");
3253 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
3254 if (mkstemp(phys_ram_file) < 0) {
3255 fprintf(stderr, "Could not create temporary memory file '%s'\n",
3259 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
3260 if (phys_ram_fd < 0) {
3261 fprintf(stderr, "Could not open temporary memory file '%s'\n",
3265 ftruncate(phys_ram_fd, phys_ram_size);
3266 unlink(phys_ram_file);
3267 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
3269 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
3271 if (phys_ram_base == MAP_FAILED) {
3272 fprintf(stderr, "Could not map physical memory\n");
3278 /* we always create the cdrom drive, even if no disk is there */
3281 bs_table[2] = bdrv_new("cdrom");
3282 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
3285 /* open the virtual block devices */
3286 for(i = 0; i < MAX_DISKS; i++) {
3287 if (hd_filename[i]) {
3290 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
3291 bs_table[i] = bdrv_new(buf);
3293 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
3294 fprintf(stderr, "qemu: could not open hard disk image '%s'\n",
3298 if (i == 0 && cyls != 0) {
3299 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
3300 bdrv_set_translation_hint(bs_table[i], translation);
3305 /* we always create at least one floppy disk */
3306 fd_table[0] = bdrv_new("fda");
3307 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
3309 for(i = 0; i < MAX_FD; i++) {
3310 if (fd_filename[i]) {
3313 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
3314 fd_table[i] = bdrv_new(buf);
3315 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
3317 if (fd_filename[i] != '\0') {
3318 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
3319 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
3327 /* init CPU state */
3330 cpu_single_env = env;
3332 register_savevm("timer", 0, 1, timer_save, timer_load, env);
3333 register_savevm("cpu", 0, 2, cpu_save, cpu_load, env);
3334 register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
3335 qemu_register_reset(main_cpu_reset, global_env);
3338 cpu_calibrate_ticks();
3342 dumb_display_init(ds);
3345 sdl_display_init(ds, full_screen);
3347 dumb_display_init(ds);
3351 vga_console = graphic_console_init(ds);
3353 monitor_hd = qemu_chr_open(monitor_device);
3355 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
3358 monitor_init(monitor_hd, !nographic);
3360 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
3361 if (serial_devices[i][0] != '\0') {
3362 serial_hds[i] = qemu_chr_open(serial_devices[i]);
3363 if (!serial_hds[i]) {
3364 fprintf(stderr, "qemu: could not open serial device '%s'\n",
3368 if (!strcmp(serial_devices[i], "vc"))
3369 qemu_chr_printf(serial_hds[i], "serial%d console\n", i);
3373 /* setup cpu signal handlers for MMU / self modifying code handling */
3374 #if !defined(CONFIG_SOFTMMU)
3376 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3379 signal_stack = memalign(16, SIGNAL_STACK_SIZE);
3380 stk.ss_sp = signal_stack;
3381 stk.ss_size = SIGNAL_STACK_SIZE;
3384 if (sigaltstack(&stk, NULL) < 0) {
3385 perror("sigaltstack");
3391 struct sigaction act;
3393 sigfillset(&act.sa_mask);
3394 act.sa_flags = SA_SIGINFO;
3395 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3396 act.sa_flags |= SA_ONSTACK;
3398 act.sa_sigaction = host_segv_handler;
3399 sigaction(SIGSEGV, &act, NULL);
3400 sigaction(SIGBUS, &act, NULL);
3401 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3402 sigaction(SIGFPE, &act, NULL);
3409 struct sigaction act;
3410 sigfillset(&act.sa_mask);
3412 act.sa_handler = SIG_IGN;
3413 sigaction(SIGPIPE, &act, NULL);
3418 #if defined(TARGET_I386)
3419 pc_init(ram_size, vga_ram_size, boot_device,
3420 ds, fd_filename, snapshot,
3421 kernel_filename, kernel_cmdline, initrd_filename);
3422 #elif defined(TARGET_PPC)
3423 ppc_init(ram_size, vga_ram_size, boot_device,
3424 ds, fd_filename, snapshot,
3425 kernel_filename, kernel_cmdline, initrd_filename);
3426 #elif defined(TARGET_SPARC)
3427 sun4m_init(ram_size, vga_ram_size, boot_device,
3428 ds, fd_filename, snapshot,
3429 kernel_filename, kernel_cmdline, initrd_filename);
3432 gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
3433 qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
3435 #ifdef CONFIG_GDBSTUB
3437 if (gdbserver_start(gdbstub_port) < 0) {
3438 fprintf(stderr, "Could not open gdbserver socket on port %d\n",
3442 printf("Waiting gdb connection on port %d\n", gdbstub_port);
3447 qemu_loadvm(loadvm);
3450 /* XXX: simplify init */
3452 if (start_emulation) {