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 const char* keyboard_layout = NULL;
116 int64_t ticks_per_sec;
117 int boot_device = 'c';
119 static char network_script[1024];
120 int pit_min_timer_count = 0;
122 NetDriverState nd_table[MAX_NICS];
123 QEMUTimer *gui_timer;
125 int audio_enabled = 0;
126 int sb16_enabled = 1;
127 int adlib_enabled = 1;
130 int prep_enabled = 0;
132 int cirrus_vga_enabled = 1;
133 int graphic_width = 800;
134 int graphic_height = 600;
135 int graphic_depth = 15;
137 TextConsole *vga_console;
138 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
140 /***********************************************************/
141 /* x86 ISA bus support */
143 target_phys_addr_t isa_mem_base = 0;
145 uint32_t default_ioport_readb(void *opaque, uint32_t address)
147 #ifdef DEBUG_UNUSED_IOPORT
148 fprintf(stderr, "inb: port=0x%04x\n", address);
153 void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
155 #ifdef DEBUG_UNUSED_IOPORT
156 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
160 /* default is to make two byte accesses */
161 uint32_t default_ioport_readw(void *opaque, uint32_t address)
164 data = ioport_read_table[0][address](ioport_opaque[address], address);
165 address = (address + 1) & (MAX_IOPORTS - 1);
166 data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;
170 void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
172 ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);
173 address = (address + 1) & (MAX_IOPORTS - 1);
174 ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);
177 uint32_t default_ioport_readl(void *opaque, uint32_t address)
179 #ifdef DEBUG_UNUSED_IOPORT
180 fprintf(stderr, "inl: port=0x%04x\n", address);
185 void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
187 #ifdef DEBUG_UNUSED_IOPORT
188 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
192 void init_ioports(void)
196 for(i = 0; i < MAX_IOPORTS; i++) {
197 ioport_read_table[0][i] = default_ioport_readb;
198 ioport_write_table[0][i] = default_ioport_writeb;
199 ioport_read_table[1][i] = default_ioport_readw;
200 ioport_write_table[1][i] = default_ioport_writew;
201 ioport_read_table[2][i] = default_ioport_readl;
202 ioport_write_table[2][i] = default_ioport_writel;
206 /* size is the word size in byte */
207 int register_ioport_read(int start, int length, int size,
208 IOPortReadFunc *func, void *opaque)
214 } else if (size == 2) {
216 } else if (size == 4) {
219 hw_error("register_ioport_read: invalid size");
222 for(i = start; i < start + length; i += size) {
223 ioport_read_table[bsize][i] = func;
224 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
225 hw_error("register_ioport_read: invalid opaque");
226 ioport_opaque[i] = opaque;
231 /* size is the word size in byte */
232 int register_ioport_write(int start, int length, int size,
233 IOPortWriteFunc *func, void *opaque)
239 } else if (size == 2) {
241 } else if (size == 4) {
244 hw_error("register_ioport_write: invalid size");
247 for(i = start; i < start + length; i += size) {
248 ioport_write_table[bsize][i] = func;
249 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
250 hw_error("register_ioport_read: invalid opaque");
251 ioport_opaque[i] = opaque;
256 void isa_unassign_ioport(int start, int length)
260 for(i = start; i < start + length; i++) {
261 ioport_read_table[0][i] = default_ioport_readb;
262 ioport_read_table[1][i] = default_ioport_readw;
263 ioport_read_table[2][i] = default_ioport_readl;
265 ioport_write_table[0][i] = default_ioport_writeb;
266 ioport_write_table[1][i] = default_ioport_writew;
267 ioport_write_table[2][i] = default_ioport_writel;
271 /***********************************************************/
273 void pstrcpy(char *buf, int buf_size, const char *str)
283 if (c == 0 || q >= buf + buf_size - 1)
290 /* strcat and truncate. */
291 char *pstrcat(char *buf, int buf_size, const char *s)
296 pstrcpy(buf + len, buf_size - len, s);
300 int strstart(const char *str, const char *val, const char **ptr)
316 /* return the size or -1 if error */
317 int get_image_size(const char *filename)
320 fd = open(filename, O_RDONLY | O_BINARY);
323 size = lseek(fd, 0, SEEK_END);
328 /* return the size or -1 if error */
329 int load_image(const char *filename, uint8_t *addr)
332 fd = open(filename, O_RDONLY | O_BINARY);
335 size = lseek(fd, 0, SEEK_END);
336 lseek(fd, 0, SEEK_SET);
337 if (read(fd, addr, size) != size) {
345 void cpu_outb(CPUState *env, int addr, int val)
348 if (loglevel & CPU_LOG_IOPORT)
349 fprintf(logfile, "outb: %04x %02x\n", addr, val);
351 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
354 void cpu_outw(CPUState *env, int addr, int val)
357 if (loglevel & CPU_LOG_IOPORT)
358 fprintf(logfile, "outw: %04x %04x\n", addr, val);
360 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
363 void cpu_outl(CPUState *env, int addr, int val)
366 if (loglevel & CPU_LOG_IOPORT)
367 fprintf(logfile, "outl: %04x %08x\n", addr, val);
369 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
372 int cpu_inb(CPUState *env, int addr)
375 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
377 if (loglevel & CPU_LOG_IOPORT)
378 fprintf(logfile, "inb : %04x %02x\n", addr, val);
383 int cpu_inw(CPUState *env, int addr)
386 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
388 if (loglevel & CPU_LOG_IOPORT)
389 fprintf(logfile, "inw : %04x %04x\n", addr, val);
394 int cpu_inl(CPUState *env, int addr)
397 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
399 if (loglevel & CPU_LOG_IOPORT)
400 fprintf(logfile, "inl : %04x %08x\n", addr, val);
405 /***********************************************************/
406 void hw_error(const char *fmt, ...)
411 fprintf(stderr, "qemu: hardware error: ");
412 vfprintf(stderr, fmt, ap);
413 fprintf(stderr, "\n");
415 cpu_dump_state(global_env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
417 cpu_dump_state(global_env, stderr, fprintf, 0);
423 /***********************************************************/
426 static QEMUPutKBDEvent *qemu_put_kbd_event;
427 static void *qemu_put_kbd_event_opaque;
428 static QEMUPutMouseEvent *qemu_put_mouse_event;
429 static void *qemu_put_mouse_event_opaque;
431 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
433 qemu_put_kbd_event_opaque = opaque;
434 qemu_put_kbd_event = func;
437 void qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, void *opaque)
439 qemu_put_mouse_event_opaque = opaque;
440 qemu_put_mouse_event = func;
443 void kbd_put_keycode(int keycode)
445 if (qemu_put_kbd_event) {
446 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
450 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
452 if (qemu_put_mouse_event) {
453 qemu_put_mouse_event(qemu_put_mouse_event_opaque,
454 dx, dy, dz, buttons_state);
458 /***********************************************************/
461 #if defined(__powerpc__)
463 static inline uint32_t get_tbl(void)
466 asm volatile("mftb %0" : "=r" (tbl));
470 static inline uint32_t get_tbu(void)
473 asm volatile("mftbu %0" : "=r" (tbl));
477 int64_t cpu_get_real_ticks(void)
480 /* NOTE: we test if wrapping has occurred */
486 return ((int64_t)h << 32) | l;
489 #elif defined(__i386__)
491 int64_t cpu_get_real_ticks(void)
494 asm volatile ("rdtsc" : "=A" (val));
498 #elif defined(__x86_64__)
500 int64_t cpu_get_real_ticks(void)
504 asm volatile("rdtsc" : "=a" (low), "=d" (high));
512 #error unsupported CPU
515 static int64_t cpu_ticks_offset;
516 static int cpu_ticks_enabled;
518 static inline int64_t cpu_get_ticks(void)
520 if (!cpu_ticks_enabled) {
521 return cpu_ticks_offset;
523 return cpu_get_real_ticks() + cpu_ticks_offset;
527 /* enable cpu_get_ticks() */
528 void cpu_enable_ticks(void)
530 if (!cpu_ticks_enabled) {
531 cpu_ticks_offset -= cpu_get_real_ticks();
532 cpu_ticks_enabled = 1;
536 /* disable cpu_get_ticks() : the clock is stopped. You must not call
537 cpu_get_ticks() after that. */
538 void cpu_disable_ticks(void)
540 if (cpu_ticks_enabled) {
541 cpu_ticks_offset = cpu_get_ticks();
542 cpu_ticks_enabled = 0;
546 static int64_t get_clock(void)
551 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
554 gettimeofday(&tv, NULL);
555 return tv.tv_sec * 1000000LL + tv.tv_usec;
559 void cpu_calibrate_ticks(void)
564 ticks = cpu_get_real_ticks();
570 usec = get_clock() - usec;
571 ticks = cpu_get_real_ticks() - ticks;
572 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
575 /* compute with 96 bit intermediate result: (a*b)/c */
576 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
581 #ifdef WORDS_BIGENDIAN
591 rl = (uint64_t)u.l.low * (uint64_t)b;
592 rh = (uint64_t)u.l.high * (uint64_t)b;
595 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
599 #define QEMU_TIMER_REALTIME 0
600 #define QEMU_TIMER_VIRTUAL 1
604 /* XXX: add frequency */
612 struct QEMUTimer *next;
618 static QEMUTimer *active_timers[2];
620 static MMRESULT timerID;
622 /* frequency of the times() clock tick */
623 static int timer_freq;
626 QEMUClock *qemu_new_clock(int type)
629 clock = qemu_mallocz(sizeof(QEMUClock));
636 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
640 ts = qemu_mallocz(sizeof(QEMUTimer));
647 void qemu_free_timer(QEMUTimer *ts)
652 /* stop a timer, but do not dealloc it */
653 void qemu_del_timer(QEMUTimer *ts)
657 /* NOTE: this code must be signal safe because
658 qemu_timer_expired() can be called from a signal. */
659 pt = &active_timers[ts->clock->type];
672 /* modify the current timer so that it will be fired when current_time
673 >= expire_time. The corresponding callback will be called. */
674 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
680 /* add the timer in the sorted list */
681 /* NOTE: this code must be signal safe because
682 qemu_timer_expired() can be called from a signal. */
683 pt = &active_timers[ts->clock->type];
688 if (t->expire_time > expire_time)
692 ts->expire_time = expire_time;
697 int qemu_timer_pending(QEMUTimer *ts)
700 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
707 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
711 return (timer_head->expire_time <= current_time);
714 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
720 if (!ts || ts->expire_time > current_time)
722 /* remove timer from the list before calling the callback */
723 *ptimer_head = ts->next;
726 /* run the callback (the timer list can be modified) */
731 int64_t qemu_get_clock(QEMUClock *clock)
733 switch(clock->type) {
734 case QEMU_TIMER_REALTIME:
736 return GetTickCount();
741 /* Note that using gettimeofday() is not a good solution
742 for timers because its value change when the date is
744 if (timer_freq == 100) {
745 return times(&tp) * 10;
747 return ((int64_t)times(&tp) * 1000) / timer_freq;
752 case QEMU_TIMER_VIRTUAL:
753 return cpu_get_ticks();
758 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
760 uint64_t expire_time;
762 if (qemu_timer_pending(ts)) {
763 expire_time = ts->expire_time;
767 qemu_put_be64(f, expire_time);
770 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
772 uint64_t expire_time;
774 expire_time = qemu_get_be64(f);
775 if (expire_time != -1) {
776 qemu_mod_timer(ts, expire_time);
782 static void timer_save(QEMUFile *f, void *opaque)
784 if (cpu_ticks_enabled) {
785 hw_error("cannot save state if virtual timers are running");
787 qemu_put_be64s(f, &cpu_ticks_offset);
788 qemu_put_be64s(f, &ticks_per_sec);
791 static int timer_load(QEMUFile *f, void *opaque, int version_id)
795 if (cpu_ticks_enabled) {
798 qemu_get_be64s(f, &cpu_ticks_offset);
799 qemu_get_be64s(f, &ticks_per_sec);
804 void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
805 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
807 static void host_alarm_handler(int host_signum)
811 #define DISP_FREQ 1000
813 static int64_t delta_min = INT64_MAX;
814 static int64_t delta_max, delta_cum, last_clock, delta, ti;
816 ti = qemu_get_clock(vm_clock);
817 if (last_clock != 0) {
818 delta = ti - last_clock;
819 if (delta < delta_min)
821 if (delta > delta_max)
824 if (++count == DISP_FREQ) {
825 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
826 muldiv64(delta_min, 1000000, ticks_per_sec),
827 muldiv64(delta_max, 1000000, ticks_per_sec),
828 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
829 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
831 delta_min = INT64_MAX;
839 if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
840 qemu_get_clock(vm_clock)) ||
841 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
842 qemu_get_clock(rt_clock))) {
843 /* stop the cpu because a timer occured */
844 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
850 #if defined(__linux__)
852 #define RTC_FREQ 1024
856 static int start_rtc_timer(void)
858 rtc_fd = open("/dev/rtc", O_RDONLY);
861 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
862 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
863 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
864 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
867 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
872 pit_min_timer_count = PIT_FREQ / RTC_FREQ;
878 static int start_rtc_timer(void)
883 #endif /* !defined(__linux__) */
885 #endif /* !defined(_WIN32) */
887 static void init_timers(void)
889 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
890 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
895 timerID = timeSetEvent(10, // interval (ms)
897 host_alarm_handler, // function
898 (DWORD)&count, // user parameter
899 TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
901 perror("failed timer alarm");
905 pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
908 struct sigaction act;
909 struct itimerval itv;
911 /* get times() syscall frequency */
912 timer_freq = sysconf(_SC_CLK_TCK);
915 sigfillset(&act.sa_mask);
917 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
918 act.sa_flags |= SA_ONSTACK;
920 act.sa_handler = host_alarm_handler;
921 sigaction(SIGALRM, &act, NULL);
923 itv.it_interval.tv_sec = 0;
924 itv.it_interval.tv_usec = 1000;
925 itv.it_value.tv_sec = 0;
926 itv.it_value.tv_usec = 10 * 1000;
927 setitimer(ITIMER_REAL, &itv, NULL);
928 /* we probe the tick duration of the kernel to inform the user if
929 the emulated kernel requested a too high timer frequency */
930 getitimer(ITIMER_REAL, &itv);
932 #if defined(__linux__)
933 if (itv.it_interval.tv_usec > 1000) {
934 /* try to use /dev/rtc to have a faster timer */
935 if (start_rtc_timer() < 0)
938 itv.it_interval.tv_sec = 0;
939 itv.it_interval.tv_usec = 0;
940 itv.it_value.tv_sec = 0;
941 itv.it_value.tv_usec = 0;
942 setitimer(ITIMER_REAL, &itv, NULL);
945 sigaction(SIGIO, &act, NULL);
946 fcntl(rtc_fd, F_SETFL, O_ASYNC);
947 fcntl(rtc_fd, F_SETOWN, getpid());
949 #endif /* defined(__linux__) */
952 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec *
959 void quit_timers(void)
962 timeKillEvent(timerID);
966 /***********************************************************/
967 /* character device */
969 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
971 return s->chr_write(s, buf, len);
974 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
979 vsnprintf(buf, sizeof(buf), fmt, ap);
980 qemu_chr_write(s, buf, strlen(buf));
984 void qemu_chr_send_event(CharDriverState *s, int event)
986 if (s->chr_send_event)
987 s->chr_send_event(s, event);
990 void qemu_chr_add_read_handler(CharDriverState *s,
991 IOCanRWHandler *fd_can_read,
992 IOReadHandler *fd_read, void *opaque)
994 s->chr_add_read_handler(s, fd_can_read, fd_read, opaque);
997 void qemu_chr_add_event_handler(CharDriverState *s, IOEventHandler *chr_event)
999 s->chr_event = chr_event;
1002 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1007 static void null_chr_add_read_handler(CharDriverState *chr,
1008 IOCanRWHandler *fd_can_read,
1009 IOReadHandler *fd_read, void *opaque)
1013 CharDriverState *qemu_chr_open_null(void)
1015 CharDriverState *chr;
1017 chr = qemu_mallocz(sizeof(CharDriverState));
1020 chr->chr_write = null_chr_write;
1021 chr->chr_add_read_handler = null_chr_add_read_handler;
1029 /* for nographic stdio only */
1030 IOCanRWHandler *fd_can_read;
1031 IOReadHandler *fd_read;
1035 #define STDIO_MAX_CLIENTS 2
1037 static int stdio_nb_clients;
1038 static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS];
1040 static int unix_write(int fd, const uint8_t *buf, int len1)
1046 ret = write(fd, buf, len);
1048 if (errno != EINTR && errno != EAGAIN)
1050 } else if (ret == 0) {
1060 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1062 FDCharDriver *s = chr->opaque;
1063 return unix_write(s->fd_out, buf, len);
1066 static void fd_chr_add_read_handler(CharDriverState *chr,
1067 IOCanRWHandler *fd_can_read,
1068 IOReadHandler *fd_read, void *opaque)
1070 FDCharDriver *s = chr->opaque;
1072 if (nographic && s->fd_in == 0) {
1073 s->fd_can_read = fd_can_read;
1074 s->fd_read = fd_read;
1075 s->fd_opaque = opaque;
1077 qemu_add_fd_read_handler(s->fd_in, fd_can_read, fd_read, opaque);
1081 /* open a character device to a unix fd */
1082 CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
1084 CharDriverState *chr;
1087 chr = qemu_mallocz(sizeof(CharDriverState));
1090 s = qemu_mallocz(sizeof(FDCharDriver));
1098 chr->chr_write = fd_chr_write;
1099 chr->chr_add_read_handler = fd_chr_add_read_handler;
1103 /* for STDIO, we handle the case where several clients use it
1106 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1108 static int term_got_escape, client_index;
1110 void term_print_help(void)
1113 "C-a h print this help\n"
1114 "C-a x exit emulator\n"
1115 "C-a s save disk data back to file (if -snapshot)\n"
1116 "C-a b send break (magic sysrq)\n"
1117 "C-a c switch between console and monitor\n"
1118 "C-a C-a send C-a\n"
1122 /* called when a char is received */
1123 static void stdio_received_byte(int ch)
1125 if (term_got_escape) {
1126 term_got_escape = 0;
1137 for (i = 0; i < MAX_DISKS; i++) {
1139 bdrv_commit(bs_table[i]);
1144 if (client_index < stdio_nb_clients) {
1145 CharDriverState *chr;
1148 chr = stdio_clients[client_index];
1150 chr->chr_event(s->fd_opaque, CHR_EVENT_BREAK);
1155 if (client_index >= stdio_nb_clients)
1157 if (client_index == 0) {
1158 /* send a new line in the monitor to get the prompt */
1166 } else if (ch == TERM_ESCAPE) {
1167 term_got_escape = 1;
1170 if (client_index < stdio_nb_clients) {
1172 CharDriverState *chr;
1175 chr = stdio_clients[client_index];
1178 /* XXX: should queue the char if the device is not
1180 if (s->fd_can_read(s->fd_opaque) > 0)
1181 s->fd_read(s->fd_opaque, buf, 1);
1186 static int stdio_can_read(void *opaque)
1188 /* XXX: not strictly correct */
1192 static void stdio_read(void *opaque, const uint8_t *buf, int size)
1195 for(i = 0; i < size; i++)
1196 stdio_received_byte(buf[i]);
1199 /* init terminal so that we can grab keys */
1200 static struct termios oldtty;
1201 static int old_fd0_flags;
1203 static void term_exit(void)
1205 tcsetattr (0, TCSANOW, &oldtty);
1206 fcntl(0, F_SETFL, old_fd0_flags);
1209 static void term_init(void)
1213 tcgetattr (0, &tty);
1215 old_fd0_flags = fcntl(0, F_GETFL);
1217 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1218 |INLCR|IGNCR|ICRNL|IXON);
1219 tty.c_oflag |= OPOST;
1220 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1221 /* if graphical mode, we allow Ctrl-C handling */
1223 tty.c_lflag &= ~ISIG;
1224 tty.c_cflag &= ~(CSIZE|PARENB);
1227 tty.c_cc[VTIME] = 0;
1229 tcsetattr (0, TCSANOW, &tty);
1233 fcntl(0, F_SETFL, O_NONBLOCK);
1236 CharDriverState *qemu_chr_open_stdio(void)
1238 CharDriverState *chr;
1241 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
1243 chr = qemu_chr_open_fd(0, 1);
1244 if (stdio_nb_clients == 0)
1245 qemu_add_fd_read_handler(0, stdio_can_read, stdio_read, NULL);
1246 client_index = stdio_nb_clients;
1248 if (stdio_nb_clients != 0)
1250 chr = qemu_chr_open_fd(0, 1);
1252 stdio_clients[stdio_nb_clients++] = chr;
1253 if (stdio_nb_clients == 1) {
1254 /* set the terminal in raw mode */
1260 #if defined(__linux__)
1261 CharDriverState *qemu_chr_open_pty(void)
1263 char slave_name[1024];
1264 int master_fd, slave_fd;
1266 /* Not satisfying */
1267 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
1270 fprintf(stderr, "char device redirected to %s\n", slave_name);
1271 return qemu_chr_open_fd(master_fd, master_fd);
1274 CharDriverState *qemu_chr_open_pty(void)
1280 #endif /* !defined(_WIN32) */
1282 CharDriverState *qemu_chr_open(const char *filename)
1284 if (!strcmp(filename, "vc")) {
1285 return text_console_init(&display_state);
1286 } else if (!strcmp(filename, "null")) {
1287 return qemu_chr_open_null();
1290 if (!strcmp(filename, "pty")) {
1291 return qemu_chr_open_pty();
1292 } else if (!strcmp(filename, "stdio")) {
1293 return qemu_chr_open_stdio();
1301 /***********************************************************/
1302 /* Linux network device redirectors */
1304 void hex_dump(FILE *f, const uint8_t *buf, int size)
1308 for(i=0;i<size;i+=16) {
1312 fprintf(f, "%08x ", i);
1315 fprintf(f, " %02x", buf[i+j]);
1320 for(j=0;j<len;j++) {
1322 if (c < ' ' || c > '~')
1324 fprintf(f, "%c", c);
1330 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1332 nd->send_packet(nd, buf, size);
1335 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
1336 IOReadHandler *fd_read, void *opaque)
1338 nd->add_read_packet(nd, fd_can_read, fd_read, opaque);
1341 /* dummy network adapter */
1343 static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1347 static void dummy_add_read_packet(NetDriverState *nd,
1348 IOCanRWHandler *fd_can_read,
1349 IOReadHandler *fd_read, void *opaque)
1353 static int net_dummy_init(NetDriverState *nd)
1355 nd->send_packet = dummy_send_packet;
1356 nd->add_read_packet = dummy_add_read_packet;
1357 pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy");
1361 #if defined(CONFIG_SLIRP)
1363 /* slirp network adapter */
1365 static void *slirp_fd_opaque;
1366 static IOCanRWHandler *slirp_fd_can_read;
1367 static IOReadHandler *slirp_fd_read;
1368 static int slirp_inited;
1370 int slirp_can_output(void)
1372 return slirp_fd_can_read(slirp_fd_opaque);
1375 void slirp_output(const uint8_t *pkt, int pkt_len)
1378 printf("output:\n");
1379 hex_dump(stdout, pkt, pkt_len);
1381 slirp_fd_read(slirp_fd_opaque, pkt, pkt_len);
1384 static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1388 hex_dump(stdout, buf, size);
1390 slirp_input(buf, size);
1393 static void slirp_add_read_packet(NetDriverState *nd,
1394 IOCanRWHandler *fd_can_read,
1395 IOReadHandler *fd_read, void *opaque)
1397 slirp_fd_opaque = opaque;
1398 slirp_fd_can_read = fd_can_read;
1399 slirp_fd_read = fd_read;
1402 static int net_slirp_init(NetDriverState *nd)
1404 if (!slirp_inited) {
1408 nd->send_packet = slirp_send_packet;
1409 nd->add_read_packet = slirp_add_read_packet;
1410 pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp");
1414 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
1419 p1 = strchr(p, sep);
1425 if (len > buf_size - 1)
1427 memcpy(buf, p, len);
1434 static void net_slirp_redir(const char *redir_str)
1439 struct in_addr guest_addr;
1440 int host_port, guest_port;
1442 if (!slirp_inited) {
1448 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1450 if (!strcmp(buf, "tcp")) {
1452 } else if (!strcmp(buf, "udp")) {
1458 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1460 host_port = strtol(buf, &r, 0);
1464 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1466 if (buf[0] == '\0') {
1467 pstrcpy(buf, sizeof(buf), "10.0.2.15");
1469 if (!inet_aton(buf, &guest_addr))
1472 guest_port = strtol(p, &r, 0);
1476 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
1477 fprintf(stderr, "qemu: could not set up redirection\n");
1482 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
1490 static void smb_exit(void)
1494 char filename[1024];
1496 /* erase all the files in the directory */
1497 d = opendir(smb_dir);
1502 if (strcmp(de->d_name, ".") != 0 &&
1503 strcmp(de->d_name, "..") != 0) {
1504 snprintf(filename, sizeof(filename), "%s/%s",
1505 smb_dir, de->d_name);
1513 /* automatic user mode samba server configuration */
1514 void net_slirp_smb(const char *exported_dir)
1516 char smb_conf[1024];
1517 char smb_cmdline[1024];
1520 if (!slirp_inited) {
1525 /* XXX: better tmp dir construction */
1526 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
1527 if (mkdir(smb_dir, 0700) < 0) {
1528 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
1531 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
1533 f = fopen(smb_conf, "w");
1535 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
1540 "pid directory=%s\n"
1541 "lock directory=%s\n"
1542 "log file=%s/log.smbd\n"
1543 "smb passwd file=%s/smbpasswd\n"
1544 "security = share\n"
1558 snprintf(smb_cmdline, sizeof(smb_cmdline), "/usr/sbin/smbd -s %s",
1561 slirp_add_exec(0, smb_cmdline, 4, 139);
1564 #endif /* !defined(_WIN32) */
1566 #endif /* CONFIG_SLIRP */
1568 #if !defined(_WIN32)
1570 static int tun_open(char *ifname, int ifname_size)
1576 fd = open("/dev/tap", O_RDWR);
1578 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1583 dev = devname(s.st_rdev, S_IFCHR);
1584 pstrcpy(ifname, ifname_size, dev);
1586 fcntl(fd, F_SETFL, O_NONBLOCK);
1590 static int tun_open(char *ifname, int ifname_size)
1595 fd = open("/dev/net/tun", O_RDWR);
1597 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1600 memset(&ifr, 0, sizeof(ifr));
1601 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1602 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
1603 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1605 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1609 printf("Connected to host network interface: %s\n", ifr.ifr_name);
1610 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1611 fcntl(fd, F_SETFL, O_NONBLOCK);
1616 static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1618 write(nd->fd, buf, size);
1621 static void tun_add_read_packet(NetDriverState *nd,
1622 IOCanRWHandler *fd_can_read,
1623 IOReadHandler *fd_read, void *opaque)
1625 qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque);
1628 static int net_tun_init(NetDriverState *nd)
1634 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
1638 /* try to launch network init script */
1643 *parg++ = network_script;
1644 *parg++ = nd->ifname;
1646 execv(network_script, args);
1649 while (waitpid(pid, &status, 0) != pid);
1650 if (!WIFEXITED(status) ||
1651 WEXITSTATUS(status) != 0) {
1652 fprintf(stderr, "%s: could not launch network script\n",
1656 nd->send_packet = tun_send_packet;
1657 nd->add_read_packet = tun_add_read_packet;
1661 static int net_fd_init(NetDriverState *nd, int fd)
1664 nd->send_packet = tun_send_packet;
1665 nd->add_read_packet = tun_add_read_packet;
1666 pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
1670 #endif /* !_WIN32 */
1672 /***********************************************************/
1675 static char *pid_filename;
1677 /* Remove PID file. Called on normal exit */
1679 static void remove_pidfile(void)
1681 unlink (pid_filename);
1684 static void create_pidfile(const char *filename)
1686 struct stat pidstat;
1689 /* Try to write our PID to the named file */
1690 if (stat(filename, &pidstat) < 0) {
1691 if (errno == ENOENT) {
1692 if ((f = fopen (filename, "w")) == NULL) {
1693 perror("Opening pidfile");
1696 fprintf(f, "%d\n", getpid());
1698 pid_filename = qemu_strdup(filename);
1699 if (!pid_filename) {
1700 fprintf(stderr, "Could not save PID filename");
1703 atexit(remove_pidfile);
1706 fprintf(stderr, "%s already exists. Remove it and try again.\n",
1712 /***********************************************************/
1715 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
1719 static void dumb_resize(DisplayState *ds, int w, int h)
1723 static void dumb_refresh(DisplayState *ds)
1725 vga_update_display();
1728 void dumb_display_init(DisplayState *ds)
1733 ds->dpy_update = dumb_update;
1734 ds->dpy_resize = dumb_resize;
1735 ds->dpy_refresh = dumb_refresh;
1738 #if !defined(CONFIG_SOFTMMU)
1739 /***********************************************************/
1740 /* cpu signal handler */
1741 static void host_segv_handler(int host_signum, siginfo_t *info,
1744 if (cpu_signal_handler(host_signum, info, puc))
1746 if (stdio_nb_clients > 0)
1752 /***********************************************************/
1755 #define MAX_IO_HANDLERS 64
1757 typedef struct IOHandlerRecord {
1759 IOCanRWHandler *fd_can_read;
1760 IOReadHandler *fd_read;
1762 /* temporary data */
1765 struct IOHandlerRecord *next;
1768 static IOHandlerRecord *first_io_handler;
1770 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
1771 IOReadHandler *fd_read, void *opaque)
1773 IOHandlerRecord *ioh;
1775 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1779 ioh->fd_can_read = fd_can_read;
1780 ioh->fd_read = fd_read;
1781 ioh->opaque = opaque;
1782 ioh->next = first_io_handler;
1783 first_io_handler = ioh;
1787 void qemu_del_fd_read_handler(int fd)
1789 IOHandlerRecord **pioh, *ioh;
1791 pioh = &first_io_handler;
1796 if (ioh->fd == fd) {
1804 /***********************************************************/
1805 /* savevm/loadvm support */
1807 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
1809 fwrite(buf, 1, size, f);
1812 void qemu_put_byte(QEMUFile *f, int v)
1817 void qemu_put_be16(QEMUFile *f, unsigned int v)
1819 qemu_put_byte(f, v >> 8);
1820 qemu_put_byte(f, v);
1823 void qemu_put_be32(QEMUFile *f, unsigned int v)
1825 qemu_put_byte(f, v >> 24);
1826 qemu_put_byte(f, v >> 16);
1827 qemu_put_byte(f, v >> 8);
1828 qemu_put_byte(f, v);
1831 void qemu_put_be64(QEMUFile *f, uint64_t v)
1833 qemu_put_be32(f, v >> 32);
1834 qemu_put_be32(f, v);
1837 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
1839 return fread(buf, 1, size, f);
1842 int qemu_get_byte(QEMUFile *f)
1852 unsigned int qemu_get_be16(QEMUFile *f)
1855 v = qemu_get_byte(f) << 8;
1856 v |= qemu_get_byte(f);
1860 unsigned int qemu_get_be32(QEMUFile *f)
1863 v = qemu_get_byte(f) << 24;
1864 v |= qemu_get_byte(f) << 16;
1865 v |= qemu_get_byte(f) << 8;
1866 v |= qemu_get_byte(f);
1870 uint64_t qemu_get_be64(QEMUFile *f)
1873 v = (uint64_t)qemu_get_be32(f) << 32;
1874 v |= qemu_get_be32(f);
1878 int64_t qemu_ftell(QEMUFile *f)
1883 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
1885 if (fseek(f, pos, whence) < 0)
1890 typedef struct SaveStateEntry {
1894 SaveStateHandler *save_state;
1895 LoadStateHandler *load_state;
1897 struct SaveStateEntry *next;
1900 static SaveStateEntry *first_se;
1902 int register_savevm(const char *idstr,
1905 SaveStateHandler *save_state,
1906 LoadStateHandler *load_state,
1909 SaveStateEntry *se, **pse;
1911 se = qemu_malloc(sizeof(SaveStateEntry));
1914 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1915 se->instance_id = instance_id;
1916 se->version_id = version_id;
1917 se->save_state = save_state;
1918 se->load_state = load_state;
1919 se->opaque = opaque;
1922 /* add at the end of list */
1924 while (*pse != NULL)
1925 pse = &(*pse)->next;
1930 #define QEMU_VM_FILE_MAGIC 0x5145564d
1931 #define QEMU_VM_FILE_VERSION 0x00000001
1933 int qemu_savevm(const char *filename)
1937 int len, len_pos, cur_pos, saved_vm_running, ret;
1939 saved_vm_running = vm_running;
1942 f = fopen(filename, "wb");
1948 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1949 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1951 for(se = first_se; se != NULL; se = se->next) {
1953 len = strlen(se->idstr);
1954 qemu_put_byte(f, len);
1955 qemu_put_buffer(f, se->idstr, len);
1957 qemu_put_be32(f, se->instance_id);
1958 qemu_put_be32(f, se->version_id);
1960 /* record size: filled later */
1962 qemu_put_be32(f, 0);
1964 se->save_state(f, se->opaque);
1966 /* fill record size */
1968 len = ftell(f) - len_pos - 4;
1969 fseek(f, len_pos, SEEK_SET);
1970 qemu_put_be32(f, len);
1971 fseek(f, cur_pos, SEEK_SET);
1977 if (saved_vm_running)
1982 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1986 for(se = first_se; se != NULL; se = se->next) {
1987 if (!strcmp(se->idstr, idstr) &&
1988 instance_id == se->instance_id)
1994 int qemu_loadvm(const char *filename)
1998 int len, cur_pos, ret, instance_id, record_len, version_id;
1999 int saved_vm_running;
2003 saved_vm_running = vm_running;
2006 f = fopen(filename, "rb");
2012 v = qemu_get_be32(f);
2013 if (v != QEMU_VM_FILE_MAGIC)
2015 v = qemu_get_be32(f);
2016 if (v != QEMU_VM_FILE_VERSION) {
2023 #if defined (DO_TB_FLUSH)
2024 tb_flush(global_env);
2026 len = qemu_get_byte(f);
2029 qemu_get_buffer(f, idstr, len);
2031 instance_id = qemu_get_be32(f);
2032 version_id = qemu_get_be32(f);
2033 record_len = qemu_get_be32(f);
2035 printf("idstr=%s instance=0x%x version=%d len=%d\n",
2036 idstr, instance_id, version_id, record_len);
2039 se = find_se(idstr, instance_id);
2041 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
2042 instance_id, idstr);
2044 ret = se->load_state(f, se->opaque, version_id);
2046 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2047 instance_id, idstr);
2050 /* always seek to exact end of record */
2051 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
2056 if (saved_vm_running)
2061 /***********************************************************/
2062 /* cpu save/restore */
2064 #if defined(TARGET_I386)
2066 static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
2068 qemu_put_be32(f, dt->selector);
2069 qemu_put_betl(f, dt->base);
2070 qemu_put_be32(f, dt->limit);
2071 qemu_put_be32(f, dt->flags);
2074 static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
2076 dt->selector = qemu_get_be32(f);
2077 dt->base = qemu_get_betl(f);
2078 dt->limit = qemu_get_be32(f);
2079 dt->flags = qemu_get_be32(f);
2082 void cpu_save(QEMUFile *f, void *opaque)
2084 CPUState *env = opaque;
2085 uint16_t fptag, fpus, fpuc;
2089 for(i = 0; i < CPU_NB_REGS; i++)
2090 qemu_put_betls(f, &env->regs[i]);
2091 qemu_put_betls(f, &env->eip);
2092 qemu_put_betls(f, &env->eflags);
2093 qemu_put_betl(f, 0); /* XXX: suppress that */
2094 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
2095 qemu_put_be32s(f, &hflags);
2099 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2101 for (i=7; i>=0; i--) {
2103 if (env->fptags[i]) {
2108 qemu_put_be16s(f, &fpuc);
2109 qemu_put_be16s(f, &fpus);
2110 qemu_put_be16s(f, &fptag);
2112 for(i = 0; i < 8; i++) {
2115 cpu_get_fp80(&mant, &exp, env->fpregs[i]);
2116 qemu_put_be64(f, mant);
2117 qemu_put_be16(f, exp);
2120 for(i = 0; i < 6; i++)
2121 cpu_put_seg(f, &env->segs[i]);
2122 cpu_put_seg(f, &env->ldt);
2123 cpu_put_seg(f, &env->tr);
2124 cpu_put_seg(f, &env->gdt);
2125 cpu_put_seg(f, &env->idt);
2127 qemu_put_be32s(f, &env->sysenter_cs);
2128 qemu_put_be32s(f, &env->sysenter_esp);
2129 qemu_put_be32s(f, &env->sysenter_eip);
2131 qemu_put_betls(f, &env->cr[0]);
2132 qemu_put_betls(f, &env->cr[2]);
2133 qemu_put_betls(f, &env->cr[3]);
2134 qemu_put_betls(f, &env->cr[4]);
2136 for(i = 0; i < 8; i++)
2137 qemu_put_betls(f, &env->dr[i]);
2140 qemu_put_be32s(f, &env->a20_mask);
2143 int cpu_load(QEMUFile *f, void *opaque, int version_id)
2145 CPUState *env = opaque;
2148 uint16_t fpus, fpuc, fptag;
2150 if (version_id != 2)
2152 for(i = 0; i < CPU_NB_REGS; i++)
2153 qemu_get_betls(f, &env->regs[i]);
2154 qemu_get_betls(f, &env->eip);
2155 qemu_get_betls(f, &env->eflags);
2156 qemu_get_betl(f); /* XXX: suppress that */
2157 qemu_get_be32s(f, &hflags);
2159 qemu_get_be16s(f, &fpuc);
2160 qemu_get_be16s(f, &fpus);
2161 qemu_get_be16s(f, &fptag);
2163 for(i = 0; i < 8; i++) {
2166 mant = qemu_get_be64(f);
2167 exp = qemu_get_be16(f);
2168 env->fpregs[i] = cpu_set_fp80(mant, exp);
2172 env->fpstt = (fpus >> 11) & 7;
2173 env->fpus = fpus & ~0x3800;
2174 for(i = 0; i < 8; i++) {
2175 env->fptags[i] = ((fptag & 3) == 3);
2179 for(i = 0; i < 6; i++)
2180 cpu_get_seg(f, &env->segs[i]);
2181 cpu_get_seg(f, &env->ldt);
2182 cpu_get_seg(f, &env->tr);
2183 cpu_get_seg(f, &env->gdt);
2184 cpu_get_seg(f, &env->idt);
2186 qemu_get_be32s(f, &env->sysenter_cs);
2187 qemu_get_be32s(f, &env->sysenter_esp);
2188 qemu_get_be32s(f, &env->sysenter_eip);
2190 qemu_get_betls(f, &env->cr[0]);
2191 qemu_get_betls(f, &env->cr[2]);
2192 qemu_get_betls(f, &env->cr[3]);
2193 qemu_get_betls(f, &env->cr[4]);
2195 for(i = 0; i < 8; i++)
2196 qemu_get_betls(f, &env->dr[i]);
2199 qemu_get_be32s(f, &env->a20_mask);
2201 /* XXX: compute hflags from scratch, except for CPL and IIF */
2202 env->hflags = hflags;
2207 #elif defined(TARGET_PPC)
2208 void cpu_save(QEMUFile *f, void *opaque)
2212 int cpu_load(QEMUFile *f, void *opaque, int version_id)
2216 #elif defined(TARGET_SPARC)
2217 void cpu_save(QEMUFile *f, void *opaque)
2219 CPUState *env = opaque;
2223 for(i = 1; i < 8; i++)
2224 qemu_put_be32s(f, &env->gregs[i]);
2225 tmp = env->regwptr - env->regbase;
2226 qemu_put_be32s(f, &tmp);
2227 for(i = 1; i < NWINDOWS * 16 + 8; i++)
2228 qemu_put_be32s(f, &env->regbase[i]);
2231 for(i = 0; i < 32; i++) {
2234 cpu_get_fp64(&mant, &exp, env->fpr[i]);
2235 qemu_put_be64(f, mant);
2236 qemu_put_be16(f, exp);
2238 qemu_put_be32s(f, &env->pc);
2239 qemu_put_be32s(f, &env->npc);
2240 qemu_put_be32s(f, &env->y);
2242 qemu_put_be32s(f, &tmp);
2243 qemu_put_be32s(f, &env->fsr);
2244 qemu_put_be32s(f, &env->cwp);
2245 qemu_put_be32s(f, &env->wim);
2246 qemu_put_be32s(f, &env->tbr);
2248 for(i = 0; i < 16; i++)
2249 qemu_put_be32s(f, &env->mmuregs[i]);
2252 int cpu_load(QEMUFile *f, void *opaque, int version_id)
2254 CPUState *env = opaque;
2258 for(i = 1; i < 8; i++)
2259 qemu_get_be32s(f, &env->gregs[i]);
2260 qemu_get_be32s(f, &tmp);
2261 env->regwptr = env->regbase + tmp;
2262 for(i = 1; i < NWINDOWS * 16 + 8; i++)
2263 qemu_get_be32s(f, &env->regbase[i]);
2266 for(i = 0; i < 32; i++) {
2270 qemu_get_be64s(f, &mant);
2271 qemu_get_be16s(f, &exp);
2272 env->fpr[i] = cpu_put_fp64(mant, exp);
2274 qemu_get_be32s(f, &env->pc);
2275 qemu_get_be32s(f, &env->npc);
2276 qemu_get_be32s(f, &env->y);
2277 qemu_get_be32s(f, &tmp);
2279 qemu_get_be32s(f, &env->fsr);
2280 qemu_get_be32s(f, &env->cwp);
2281 qemu_get_be32s(f, &env->wim);
2282 qemu_get_be32s(f, &env->tbr);
2284 for(i = 0; i < 16; i++)
2285 qemu_get_be32s(f, &env->mmuregs[i]);
2291 #warning No CPU save/restore functions
2295 /***********************************************************/
2296 /* ram save/restore */
2298 /* we just avoid storing empty pages */
2299 static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
2304 for(i = 1; i < len; i++) {
2308 qemu_put_byte(f, 1);
2309 qemu_put_byte(f, v);
2312 qemu_put_byte(f, 0);
2313 qemu_put_buffer(f, buf, len);
2316 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
2320 v = qemu_get_byte(f);
2323 if (qemu_get_buffer(f, buf, len) != len)
2327 v = qemu_get_byte(f);
2328 memset(buf, v, len);
2336 static void ram_save(QEMUFile *f, void *opaque)
2339 qemu_put_be32(f, phys_ram_size);
2340 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
2341 ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
2345 static int ram_load(QEMUFile *f, void *opaque, int version_id)
2349 if (version_id != 1)
2351 if (qemu_get_be32(f) != phys_ram_size)
2353 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
2354 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
2361 /***********************************************************/
2362 /* main execution loop */
2364 void gui_update(void *opaque)
2366 display_state.dpy_refresh(&display_state);
2367 qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
2370 /* XXX: support several handlers */
2371 VMStopHandler *vm_stop_cb;
2372 VMStopHandler *vm_stop_opaque;
2374 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
2377 vm_stop_opaque = opaque;
2381 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
2394 void vm_stop(int reason)
2397 cpu_disable_ticks();
2401 vm_stop_cb(vm_stop_opaque, reason);
2407 /* reset/shutdown handler */
2409 typedef struct QEMUResetEntry {
2410 QEMUResetHandler *func;
2412 struct QEMUResetEntry *next;
2415 static QEMUResetEntry *first_reset_entry;
2416 static int reset_requested;
2417 static int shutdown_requested;
2419 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
2421 QEMUResetEntry **pre, *re;
2423 pre = &first_reset_entry;
2424 while (*pre != NULL)
2425 pre = &(*pre)->next;
2426 re = qemu_mallocz(sizeof(QEMUResetEntry));
2428 re->opaque = opaque;
2433 void qemu_system_reset(void)
2437 /* reset all devices */
2438 for(re = first_reset_entry; re != NULL; re = re->next) {
2439 re->func(re->opaque);
2443 void qemu_system_reset_request(void)
2445 reset_requested = 1;
2446 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
2449 void qemu_system_shutdown_request(void)
2451 shutdown_requested = 1;
2452 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
2455 static void main_cpu_reset(void *opaque)
2457 #if defined(TARGET_I386) || defined(TARGET_SPARC)
2458 CPUState *env = opaque;
2463 void main_loop_wait(int timeout)
2466 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
2467 IOHandlerRecord *ioh, *ioh_next;
2477 /* poll any events */
2478 /* XXX: separate device handlers from system ones */
2480 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2481 if (!ioh->fd_can_read) {
2484 pf->events = POLLIN;
2488 max_size = ioh->fd_can_read(ioh->opaque);
2490 if (max_size > sizeof(buf))
2491 max_size = sizeof(buf);
2493 pf->events = POLLIN;
2500 ioh->max_size = max_size;
2503 ret = poll(ufds, pf - ufds, timeout);
2505 /* XXX: better handling of removal */
2506 for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
2507 ioh_next = ioh->next;
2510 if (pf->revents & POLLIN) {
2511 if (ioh->max_size == 0) {
2512 /* just a read event */
2513 ioh->fd_read(ioh->opaque, NULL, 0);
2515 n = read(ioh->fd, buf, ioh->max_size);
2517 ioh->fd_read(ioh->opaque, buf, n);
2518 } else if (errno != EAGAIN) {
2519 ioh->fd_read(ioh->opaque, NULL, -errno);
2526 #endif /* !defined(_WIN32) */
2527 #if defined(CONFIG_SLIRP)
2528 /* XXX: merge with poll() */
2530 fd_set rfds, wfds, xfds;
2538 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2541 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
2543 slirp_select_poll(&rfds, &wfds, &xfds);
2549 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
2550 qemu_get_clock(vm_clock));
2551 /* run dma transfers, if any */
2555 /* real time timers */
2556 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
2557 qemu_get_clock(rt_clock));
2563 CPUState *env = global_env;
2567 ret = cpu_exec(env);
2568 if (shutdown_requested) {
2569 ret = EXCP_INTERRUPT;
2572 if (reset_requested) {
2573 reset_requested = 0;
2574 qemu_system_reset();
2575 ret = EXCP_INTERRUPT;
2577 if (ret == EXCP_DEBUG) {
2578 vm_stop(EXCP_DEBUG);
2580 /* if hlt instruction, we wait until the next IRQ */
2581 /* XXX: use timeout computed from timers */
2582 if (ret == EXCP_HLT)
2589 main_loop_wait(timeout);
2591 cpu_disable_ticks();
2597 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n"
2598 "usage: %s [options] [disk_image]\n"
2600 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2602 "Standard options:\n"
2603 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
2604 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
2605 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
2606 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
2607 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
2608 "-snapshot write to temporary files instead of disk image files\n"
2609 "-m megs set virtual RAM size to megs MB [default=%d]\n"
2610 "-nographic disable graphical output and redirect serial I/Os to console\n"
2612 "-k language use keyboard layout (for example \"fr\" for French)\n"
2614 "-enable-audio enable audio support\n"
2615 "-localtime set the real time clock to local time [default=utc]\n"
2616 "-full-screen start in full screen\n"
2618 "-prep Simulate a PREP system (default is PowerMAC)\n"
2619 "-g WxH[xDEPTH] Set the initial VGA graphic mode\n"
2622 "Network options:\n"
2623 "-nics n simulate 'n' network cards [default=1]\n"
2624 "-macaddr addr set the mac address of the first interface\n"
2625 "-n script set tap/tun network init script [default=%s]\n"
2626 "-tun-fd fd use this fd as already opened tap/tun interface\n"
2628 "-user-net use user mode network stack [default if no tap/tun script]\n"
2629 "-tftp prefix allow tftp access to files starting with prefix [-user-net]\n"
2631 "-smb dir allow SMB access to files in 'dir' [-user-net]\n"
2633 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
2634 " redirect TCP or UDP connections from host to guest [-user-net]\n"
2636 "-dummy-net use dummy network stack\n"
2638 "Linux boot specific:\n"
2639 "-kernel bzImage use 'bzImage' as kernel image\n"
2640 "-append cmdline use 'cmdline' as kernel command line\n"
2641 "-initrd file use 'file' as initial ram disk\n"
2643 "Debug/Expert options:\n"
2644 "-monitor dev redirect the monitor to char device 'dev'\n"
2645 "-serial dev redirect the serial port to char device 'dev'\n"
2646 "-pidfile file Write PID to 'file'\n"
2647 "-S freeze CPU at startup (use 'c' to start execution)\n"
2648 "-s wait gdb connection to port %d\n"
2649 "-p port change gdb connection port\n"
2650 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
2651 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
2652 " translation (t=none or lba) (usually qemu can guess them)\n"
2653 "-L path set the directory for the BIOS and VGA BIOS\n"
2654 #ifdef USE_CODE_COPY
2655 "-no-code-copy disable code copy acceleration\n"
2658 "-isa simulate an ISA-only system (default is PCI system)\n"
2659 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
2660 " (default is CL-GD5446 PCI VGA)\n"
2662 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
2664 "During emulation, the following keys are useful:\n"
2665 "ctrl-alt-f toggle full screen\n"
2666 "ctrl-alt-n switch to virtual console 'n'\n"
2667 "ctrl-alt toggle mouse and keyboard grab\n"
2669 "When using -nographic, press 'ctrl-a h' to get some help.\n"
2671 #ifdef CONFIG_SOFTMMU
2677 DEFAULT_NETWORK_SCRIPT,
2678 DEFAULT_GDBSTUB_PORT,
2680 #ifndef CONFIG_SOFTMMU
2682 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
2683 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
2689 #define HAS_ARG 0x0001
2702 QEMU_OPTION_snapshot,
2704 QEMU_OPTION_nographic,
2705 QEMU_OPTION_enable_audio,
2708 QEMU_OPTION_macaddr,
2711 QEMU_OPTION_user_net,
2715 QEMU_OPTION_dummy_net,
2727 QEMU_OPTION_no_code_copy,
2732 QEMU_OPTION_localtime,
2733 QEMU_OPTION_cirrusvga,
2735 QEMU_OPTION_std_vga,
2736 QEMU_OPTION_monitor,
2739 QEMU_OPTION_full_screen,
2740 QEMU_OPTION_pidfile,
2743 typedef struct QEMUOption {
2749 const QEMUOption qemu_options[] = {
2750 { "h", 0, QEMU_OPTION_h },
2752 { "fda", HAS_ARG, QEMU_OPTION_fda },
2753 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
2754 { "hda", HAS_ARG, QEMU_OPTION_hda },
2755 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
2756 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
2757 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
2758 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
2759 { "boot", HAS_ARG, QEMU_OPTION_boot },
2760 { "snapshot", 0, QEMU_OPTION_snapshot },
2761 { "m", HAS_ARG, QEMU_OPTION_m },
2762 { "nographic", 0, QEMU_OPTION_nographic },
2763 { "k", HAS_ARG, QEMU_OPTION_k },
2764 { "enable-audio", 0, QEMU_OPTION_enable_audio },
2766 { "nics", HAS_ARG, QEMU_OPTION_nics},
2767 { "macaddr", HAS_ARG, QEMU_OPTION_macaddr},
2768 { "n", HAS_ARG, QEMU_OPTION_n },
2769 { "tun-fd", HAS_ARG, QEMU_OPTION_tun_fd },
2771 { "user-net", 0, QEMU_OPTION_user_net },
2772 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
2774 { "smb", HAS_ARG, QEMU_OPTION_smb },
2776 { "redir", HAS_ARG, QEMU_OPTION_redir },
2778 { "dummy-net", 0, QEMU_OPTION_dummy_net },
2780 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
2781 { "append", HAS_ARG, QEMU_OPTION_append },
2782 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
2784 { "S", 0, QEMU_OPTION_S },
2785 { "s", 0, QEMU_OPTION_s },
2786 { "p", HAS_ARG, QEMU_OPTION_p },
2787 { "d", HAS_ARG, QEMU_OPTION_d },
2788 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
2789 { "L", HAS_ARG, QEMU_OPTION_L },
2790 { "no-code-copy", 0, QEMU_OPTION_no_code_copy },
2792 { "prep", 0, QEMU_OPTION_prep },
2793 { "g", 1, QEMU_OPTION_g },
2795 { "localtime", 0, QEMU_OPTION_localtime },
2796 { "isa", 0, QEMU_OPTION_isa },
2797 { "std-vga", 0, QEMU_OPTION_std_vga },
2798 { "monitor", 1, QEMU_OPTION_monitor },
2799 { "serial", 1, QEMU_OPTION_serial },
2800 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
2801 { "full-screen", 0, QEMU_OPTION_full_screen },
2802 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
2804 /* temporary options */
2805 { "pci", 0, QEMU_OPTION_pci },
2806 { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
2810 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2812 /* this stack is only used during signal handling */
2813 #define SIGNAL_STACK_SIZE 32768
2815 static uint8_t *signal_stack;
2819 /* password input */
2821 static BlockDriverState *get_bdrv(int index)
2823 BlockDriverState *bs;
2826 bs = bs_table[index];
2827 } else if (index < 6) {
2828 bs = fd_table[index - 4];
2835 static void read_passwords(void)
2837 BlockDriverState *bs;
2841 for(i = 0; i < 6; i++) {
2843 if (bs && bdrv_is_encrypted(bs)) {
2844 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs));
2845 for(j = 0; j < 3; j++) {
2846 monitor_readline("Password: ",
2847 1, password, sizeof(password));
2848 if (bdrv_set_key(bs, password) == 0)
2850 term_printf("invalid password\n");
2856 #define NET_IF_TUN 0
2857 #define NET_IF_USER 1
2858 #define NET_IF_DUMMY 2
2860 int main(int argc, char **argv)
2862 #ifdef CONFIG_GDBSTUB
2863 int use_gdbstub, gdbstub_port;
2866 int snapshot, linux_boot;
2868 const char *initrd_filename;
2869 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
2870 const char *kernel_filename, *kernel_cmdline;
2871 DisplayState *ds = &display_state;
2872 int cyls, heads, secs, translation;
2873 int start_emulation = 1;
2875 int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
2877 const char *r, *optarg;
2878 CharDriverState *monitor_hd;
2879 char monitor_device[128];
2880 char serial_devices[MAX_SERIAL_PORTS][128];
2881 int serial_device_index;
2882 const char *loadvm = NULL;
2884 #if !defined(CONFIG_SOFTMMU)
2885 /* we never want that malloc() uses mmap() */
2886 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
2888 initrd_filename = NULL;
2889 for(i = 0; i < MAX_FD; i++)
2890 fd_filename[i] = NULL;
2891 for(i = 0; i < MAX_DISKS; i++)
2892 hd_filename[i] = NULL;
2893 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2894 vga_ram_size = VGA_RAM_SIZE;
2895 bios_size = BIOS_SIZE;
2896 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
2897 #ifdef CONFIG_GDBSTUB
2899 gdbstub_port = DEFAULT_GDBSTUB_PORT;
2903 kernel_filename = NULL;
2904 kernel_cmdline = "";
2906 cyls = heads = secs = 0;
2907 translation = BIOS_ATA_TRANSLATION_AUTO;
2908 pstrcpy(monitor_device, sizeof(monitor_device), "vc");
2910 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc");
2911 for(i = 1; i < MAX_SERIAL_PORTS; i++)
2912 serial_devices[i][0] = '\0';
2913 serial_device_index = 0;
2918 /* default mac address of the first network interface */
2932 hd_filename[0] = argv[optind++];
2934 const QEMUOption *popt;
2937 popt = qemu_options;
2940 fprintf(stderr, "%s: invalid option -- '%s'\n",
2944 if (!strcmp(popt->name, r + 1))
2948 if (popt->flags & HAS_ARG) {
2949 if (optind >= argc) {
2950 fprintf(stderr, "%s: option '%s' requires an argument\n",
2954 optarg = argv[optind++];
2959 switch(popt->index) {
2960 case QEMU_OPTION_initrd:
2961 initrd_filename = optarg;
2963 case QEMU_OPTION_hda:
2964 hd_filename[0] = optarg;
2966 case QEMU_OPTION_hdb:
2967 hd_filename[1] = optarg;
2969 case QEMU_OPTION_snapshot:
2972 case QEMU_OPTION_hdachs:
2976 cyls = strtol(p, (char **)&p, 0);
2977 if (cyls < 1 || cyls > 16383)
2982 heads = strtol(p, (char **)&p, 0);
2983 if (heads < 1 || heads > 16)
2988 secs = strtol(p, (char **)&p, 0);
2989 if (secs < 1 || secs > 63)
2993 if (!strcmp(p, "none"))
2994 translation = BIOS_ATA_TRANSLATION_NONE;
2995 else if (!strcmp(p, "lba"))
2996 translation = BIOS_ATA_TRANSLATION_LBA;
2997 else if (!strcmp(p, "auto"))
2998 translation = BIOS_ATA_TRANSLATION_AUTO;
3001 } else if (*p != '\0') {
3003 fprintf(stderr, "qemu: invalid physical CHS format\n");
3008 case QEMU_OPTION_nographic:
3009 pstrcpy(monitor_device, sizeof(monitor_device), "stdio");
3010 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio");
3013 case QEMU_OPTION_kernel:
3014 kernel_filename = optarg;
3016 case QEMU_OPTION_append:
3017 kernel_cmdline = optarg;
3019 case QEMU_OPTION_tun_fd:
3023 net_if_type = NET_IF_TUN;
3024 if (nb_tun_fds < MAX_NICS) {
3025 fd = strtol(optarg, (char **)&p, 0);
3027 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
3030 tun_fds[nb_tun_fds++] = fd;
3034 case QEMU_OPTION_hdc:
3035 hd_filename[2] = optarg;
3038 case QEMU_OPTION_hdd:
3039 hd_filename[3] = optarg;
3041 case QEMU_OPTION_cdrom:
3042 hd_filename[2] = optarg;
3045 case QEMU_OPTION_boot:
3046 boot_device = optarg[0];
3047 if (boot_device != 'a' &&
3048 boot_device != 'c' && boot_device != 'd') {
3049 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
3053 case QEMU_OPTION_fda:
3054 fd_filename[0] = optarg;
3056 case QEMU_OPTION_fdb:
3057 fd_filename[1] = optarg;
3059 case QEMU_OPTION_no_code_copy:
3060 code_copy_enabled = 0;
3062 case QEMU_OPTION_nics:
3063 nb_nics = atoi(optarg);
3064 if (nb_nics < 0 || nb_nics > MAX_NICS) {
3065 fprintf(stderr, "qemu: invalid number of network interfaces\n");
3069 case QEMU_OPTION_macaddr:
3074 for(i = 0; i < 6; i++) {
3075 macaddr[i] = strtol(p, (char **)&p, 16);
3082 fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
3091 case QEMU_OPTION_tftp:
3092 tftp_prefix = optarg;
3095 case QEMU_OPTION_smb:
3096 net_slirp_smb(optarg);
3099 case QEMU_OPTION_user_net:
3100 net_if_type = NET_IF_USER;
3102 case QEMU_OPTION_redir:
3103 net_slirp_redir(optarg);
3106 case QEMU_OPTION_dummy_net:
3107 net_if_type = NET_IF_DUMMY;
3109 case QEMU_OPTION_enable_audio:
3116 ram_size = atoi(optarg) * 1024 * 1024;
3119 if (ram_size > PHYS_RAM_MAX_SIZE) {
3120 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
3121 PHYS_RAM_MAX_SIZE / (1024 * 1024));
3130 mask = cpu_str_to_log_mask(optarg);
3132 printf("Log items (comma separated):\n");
3133 for(item = cpu_log_items; item->mask != 0; item++) {
3134 printf("%-10s %s\n", item->name, item->help);
3142 pstrcpy(network_script, sizeof(network_script), optarg);
3144 #ifdef CONFIG_GDBSTUB
3149 gdbstub_port = atoi(optarg);
3156 start_emulation = 0;
3158 case QEMU_OPTION_pci:
3161 case QEMU_OPTION_isa:
3164 case QEMU_OPTION_prep:
3168 keyboard_layout = optarg;
3170 case QEMU_OPTION_localtime:
3173 case QEMU_OPTION_cirrusvga:
3174 cirrus_vga_enabled = 1;
3176 case QEMU_OPTION_std_vga:
3177 cirrus_vga_enabled = 0;
3184 w = strtol(p, (char **)&p, 10);
3187 fprintf(stderr, "qemu: invalid resolution or depth\n");
3193 h = strtol(p, (char **)&p, 10);
3198 depth = strtol(p, (char **)&p, 10);
3199 if (depth != 8 && depth != 15 && depth != 16 &&
3200 depth != 24 && depth != 32)
3202 } else if (*p == '\0') {
3203 depth = graphic_depth;
3210 graphic_depth = depth;
3213 case QEMU_OPTION_monitor:
3214 pstrcpy(monitor_device, sizeof(monitor_device), optarg);
3216 case QEMU_OPTION_serial:
3217 if (serial_device_index >= MAX_SERIAL_PORTS) {
3218 fprintf(stderr, "qemu: too many serial ports\n");
3221 pstrcpy(serial_devices[serial_device_index],
3222 sizeof(serial_devices[0]), optarg);
3223 serial_device_index++;
3225 case QEMU_OPTION_loadvm:
3228 case QEMU_OPTION_full_screen:
3231 case QEMU_OPTION_pidfile:
3232 create_pidfile(optarg);
3238 linux_boot = (kernel_filename != NULL);
3240 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
3241 fd_filename[0] == '\0')
3244 /* boot to cd by default if no hard disk */
3245 if (hd_filename[0] == '\0' && boot_device == 'c') {
3246 if (fd_filename[0] != '\0')
3252 #if !defined(CONFIG_SOFTMMU)
3253 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
3255 static uint8_t stdout_buf[4096];
3256 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
3259 setvbuf(stdout, NULL, _IOLBF, 0);
3262 /* init host network redirectors */
3263 if (net_if_type == -1) {
3264 net_if_type = NET_IF_TUN;
3265 #if defined(CONFIG_SLIRP)
3266 if (access(network_script, R_OK) < 0) {
3267 net_if_type = NET_IF_USER;
3272 for(i = 0; i < nb_nics; i++) {
3273 NetDriverState *nd = &nd_table[i];
3275 /* init virtual mac address */
3276 nd->macaddr[0] = macaddr[0];
3277 nd->macaddr[1] = macaddr[1];
3278 nd->macaddr[2] = macaddr[2];
3279 nd->macaddr[3] = macaddr[3];
3280 nd->macaddr[4] = macaddr[4];
3281 nd->macaddr[5] = macaddr[5] + i;
3282 switch(net_if_type) {
3283 #if defined(CONFIG_SLIRP)
3288 #if !defined(_WIN32)
3290 if (i < nb_tun_fds) {
3291 net_fd_init(nd, tun_fds[i]);
3293 if (net_tun_init(nd) < 0)
3305 /* init the memory */
3306 phys_ram_size = ram_size + vga_ram_size + bios_size;
3308 #ifdef CONFIG_SOFTMMU
3310 /* mallocs are always aligned on BSD. valloc is better for correctness */
3311 phys_ram_base = valloc(phys_ram_size);
3313 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
3315 if (!phys_ram_base) {
3316 fprintf(stderr, "Could not allocate physical memory\n");
3320 /* as we must map the same page at several addresses, we must use
3325 tmpdir = getenv("QEMU_TMPDIR");
3328 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
3329 if (mkstemp(phys_ram_file) < 0) {
3330 fprintf(stderr, "Could not create temporary memory file '%s'\n",
3334 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
3335 if (phys_ram_fd < 0) {
3336 fprintf(stderr, "Could not open temporary memory file '%s'\n",
3340 ftruncate(phys_ram_fd, phys_ram_size);
3341 unlink(phys_ram_file);
3342 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
3344 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
3346 if (phys_ram_base == MAP_FAILED) {
3347 fprintf(stderr, "Could not map physical memory\n");
3353 /* we always create the cdrom drive, even if no disk is there */
3356 bs_table[2] = bdrv_new("cdrom");
3357 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
3360 /* open the virtual block devices */
3361 for(i = 0; i < MAX_DISKS; i++) {
3362 if (hd_filename[i]) {
3365 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
3366 bs_table[i] = bdrv_new(buf);
3368 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
3369 fprintf(stderr, "qemu: could not open hard disk image '%s'\n",
3373 if (i == 0 && cyls != 0) {
3374 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
3375 bdrv_set_translation_hint(bs_table[i], translation);
3380 /* we always create at least one floppy disk */
3381 fd_table[0] = bdrv_new("fda");
3382 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
3384 for(i = 0; i < MAX_FD; i++) {
3385 if (fd_filename[i]) {
3388 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
3389 fd_table[i] = bdrv_new(buf);
3390 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
3392 if (fd_filename[i] != '\0') {
3393 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
3394 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
3402 /* init CPU state */
3405 cpu_single_env = env;
3407 register_savevm("timer", 0, 1, timer_save, timer_load, env);
3408 register_savevm("cpu", 0, 2, cpu_save, cpu_load, env);
3409 register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
3410 qemu_register_reset(main_cpu_reset, global_env);
3413 cpu_calibrate_ticks();
3417 dumb_display_init(ds);
3420 sdl_display_init(ds, full_screen);
3422 dumb_display_init(ds);
3426 vga_console = graphic_console_init(ds);
3428 monitor_hd = qemu_chr_open(monitor_device);
3430 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
3433 monitor_init(monitor_hd, !nographic);
3435 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
3436 if (serial_devices[i][0] != '\0') {
3437 serial_hds[i] = qemu_chr_open(serial_devices[i]);
3438 if (!serial_hds[i]) {
3439 fprintf(stderr, "qemu: could not open serial device '%s'\n",
3443 if (!strcmp(serial_devices[i], "vc"))
3444 qemu_chr_printf(serial_hds[i], "serial%d console\n", i);
3448 /* setup cpu signal handlers for MMU / self modifying code handling */
3449 #if !defined(CONFIG_SOFTMMU)
3451 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3454 signal_stack = memalign(16, SIGNAL_STACK_SIZE);
3455 stk.ss_sp = signal_stack;
3456 stk.ss_size = SIGNAL_STACK_SIZE;
3459 if (sigaltstack(&stk, NULL) < 0) {
3460 perror("sigaltstack");
3466 struct sigaction act;
3468 sigfillset(&act.sa_mask);
3469 act.sa_flags = SA_SIGINFO;
3470 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3471 act.sa_flags |= SA_ONSTACK;
3473 act.sa_sigaction = host_segv_handler;
3474 sigaction(SIGSEGV, &act, NULL);
3475 sigaction(SIGBUS, &act, NULL);
3476 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3477 sigaction(SIGFPE, &act, NULL);
3484 struct sigaction act;
3485 sigfillset(&act.sa_mask);
3487 act.sa_handler = SIG_IGN;
3488 sigaction(SIGPIPE, &act, NULL);
3493 #if defined(TARGET_I386)
3494 pc_init(ram_size, vga_ram_size, boot_device,
3495 ds, fd_filename, snapshot,
3496 kernel_filename, kernel_cmdline, initrd_filename);
3497 #elif defined(TARGET_PPC)
3498 ppc_init(ram_size, vga_ram_size, boot_device,
3499 ds, fd_filename, snapshot,
3500 kernel_filename, kernel_cmdline, initrd_filename);
3501 #elif defined(TARGET_SPARC)
3502 sun4m_init(ram_size, vga_ram_size, boot_device,
3503 ds, fd_filename, snapshot,
3504 kernel_filename, kernel_cmdline, initrd_filename);
3507 gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
3508 qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
3510 #ifdef CONFIG_GDBSTUB
3512 if (gdbserver_start(gdbstub_port) < 0) {
3513 fprintf(stderr, "Could not open gdbserver socket on port %d\n",
3517 printf("Waiting gdb connection on port %d\n", gdbstub_port);
3522 qemu_loadvm(loadvm);
3525 /* XXX: simplify init */
3527 if (start_emulation) {