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];
139 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
141 /***********************************************************/
142 /* x86 ISA bus support */
144 target_phys_addr_t isa_mem_base = 0;
146 uint32_t default_ioport_readb(void *opaque, uint32_t address)
148 #ifdef DEBUG_UNUSED_IOPORT
149 fprintf(stderr, "inb: port=0x%04x\n", address);
154 void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
156 #ifdef DEBUG_UNUSED_IOPORT
157 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
161 /* default is to make two byte accesses */
162 uint32_t default_ioport_readw(void *opaque, uint32_t address)
165 data = ioport_read_table[0][address](ioport_opaque[address], address);
166 address = (address + 1) & (MAX_IOPORTS - 1);
167 data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;
171 void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
173 ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);
174 address = (address + 1) & (MAX_IOPORTS - 1);
175 ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);
178 uint32_t default_ioport_readl(void *opaque, uint32_t address)
180 #ifdef DEBUG_UNUSED_IOPORT
181 fprintf(stderr, "inl: port=0x%04x\n", address);
186 void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
188 #ifdef DEBUG_UNUSED_IOPORT
189 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
193 void init_ioports(void)
197 for(i = 0; i < MAX_IOPORTS; i++) {
198 ioport_read_table[0][i] = default_ioport_readb;
199 ioport_write_table[0][i] = default_ioport_writeb;
200 ioport_read_table[1][i] = default_ioport_readw;
201 ioport_write_table[1][i] = default_ioport_writew;
202 ioport_read_table[2][i] = default_ioport_readl;
203 ioport_write_table[2][i] = default_ioport_writel;
207 /* size is the word size in byte */
208 int register_ioport_read(int start, int length, int size,
209 IOPortReadFunc *func, void *opaque)
215 } else if (size == 2) {
217 } else if (size == 4) {
220 hw_error("register_ioport_read: invalid size");
223 for(i = start; i < start + length; i += size) {
224 ioport_read_table[bsize][i] = func;
225 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
226 hw_error("register_ioport_read: invalid opaque");
227 ioport_opaque[i] = opaque;
232 /* size is the word size in byte */
233 int register_ioport_write(int start, int length, int size,
234 IOPortWriteFunc *func, void *opaque)
240 } else if (size == 2) {
242 } else if (size == 4) {
245 hw_error("register_ioport_write: invalid size");
248 for(i = start; i < start + length; i += size) {
249 ioport_write_table[bsize][i] = func;
250 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
251 hw_error("register_ioport_read: invalid opaque");
252 ioport_opaque[i] = opaque;
257 void isa_unassign_ioport(int start, int length)
261 for(i = start; i < start + length; i++) {
262 ioport_read_table[0][i] = default_ioport_readb;
263 ioport_read_table[1][i] = default_ioport_readw;
264 ioport_read_table[2][i] = default_ioport_readl;
266 ioport_write_table[0][i] = default_ioport_writeb;
267 ioport_write_table[1][i] = default_ioport_writew;
268 ioport_write_table[2][i] = default_ioport_writel;
272 /***********************************************************/
274 void pstrcpy(char *buf, int buf_size, const char *str)
284 if (c == 0 || q >= buf + buf_size - 1)
291 /* strcat and truncate. */
292 char *pstrcat(char *buf, int buf_size, const char *s)
297 pstrcpy(buf + len, buf_size - len, s);
301 int strstart(const char *str, const char *val, const char **ptr)
317 /* return the size or -1 if error */
318 int get_image_size(const char *filename)
321 fd = open(filename, O_RDONLY | O_BINARY);
324 size = lseek(fd, 0, SEEK_END);
329 /* return the size or -1 if error */
330 int load_image(const char *filename, uint8_t *addr)
333 fd = open(filename, O_RDONLY | O_BINARY);
336 size = lseek(fd, 0, SEEK_END);
337 lseek(fd, 0, SEEK_SET);
338 if (read(fd, addr, size) != size) {
346 void cpu_outb(CPUState *env, int addr, int val)
349 if (loglevel & CPU_LOG_IOPORT)
350 fprintf(logfile, "outb: %04x %02x\n", addr, val);
352 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
355 void cpu_outw(CPUState *env, int addr, int val)
358 if (loglevel & CPU_LOG_IOPORT)
359 fprintf(logfile, "outw: %04x %04x\n", addr, val);
361 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
364 void cpu_outl(CPUState *env, int addr, int val)
367 if (loglevel & CPU_LOG_IOPORT)
368 fprintf(logfile, "outl: %04x %08x\n", addr, val);
370 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
373 int cpu_inb(CPUState *env, int addr)
376 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
378 if (loglevel & CPU_LOG_IOPORT)
379 fprintf(logfile, "inb : %04x %02x\n", addr, val);
384 int cpu_inw(CPUState *env, int addr)
387 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
389 if (loglevel & CPU_LOG_IOPORT)
390 fprintf(logfile, "inw : %04x %04x\n", addr, val);
395 int cpu_inl(CPUState *env, int addr)
398 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
400 if (loglevel & CPU_LOG_IOPORT)
401 fprintf(logfile, "inl : %04x %08x\n", addr, val);
406 /***********************************************************/
407 void hw_error(const char *fmt, ...)
412 fprintf(stderr, "qemu: hardware error: ");
413 vfprintf(stderr, fmt, ap);
414 fprintf(stderr, "\n");
416 cpu_dump_state(global_env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
418 cpu_dump_state(global_env, stderr, fprintf, 0);
424 /***********************************************************/
427 static QEMUPutKBDEvent *qemu_put_kbd_event;
428 static void *qemu_put_kbd_event_opaque;
429 static QEMUPutMouseEvent *qemu_put_mouse_event;
430 static void *qemu_put_mouse_event_opaque;
432 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
434 qemu_put_kbd_event_opaque = opaque;
435 qemu_put_kbd_event = func;
438 void qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, void *opaque)
440 qemu_put_mouse_event_opaque = opaque;
441 qemu_put_mouse_event = func;
444 void kbd_put_keycode(int keycode)
446 if (qemu_put_kbd_event) {
447 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
451 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
453 if (qemu_put_mouse_event) {
454 qemu_put_mouse_event(qemu_put_mouse_event_opaque,
455 dx, dy, dz, buttons_state);
459 /***********************************************************/
462 #if defined(__powerpc__)
464 static inline uint32_t get_tbl(void)
467 asm volatile("mftb %0" : "=r" (tbl));
471 static inline uint32_t get_tbu(void)
474 asm volatile("mftbu %0" : "=r" (tbl));
478 int64_t cpu_get_real_ticks(void)
481 /* NOTE: we test if wrapping has occurred */
487 return ((int64_t)h << 32) | l;
490 #elif defined(__i386__)
492 int64_t cpu_get_real_ticks(void)
495 asm volatile ("rdtsc" : "=A" (val));
499 #elif defined(__x86_64__)
501 int64_t cpu_get_real_ticks(void)
505 asm volatile("rdtsc" : "=a" (low), "=d" (high));
513 #error unsupported CPU
516 static int64_t cpu_ticks_offset;
517 static int cpu_ticks_enabled;
519 static inline int64_t cpu_get_ticks(void)
521 if (!cpu_ticks_enabled) {
522 return cpu_ticks_offset;
524 return cpu_get_real_ticks() + cpu_ticks_offset;
528 /* enable cpu_get_ticks() */
529 void cpu_enable_ticks(void)
531 if (!cpu_ticks_enabled) {
532 cpu_ticks_offset -= cpu_get_real_ticks();
533 cpu_ticks_enabled = 1;
537 /* disable cpu_get_ticks() : the clock is stopped. You must not call
538 cpu_get_ticks() after that. */
539 void cpu_disable_ticks(void)
541 if (cpu_ticks_enabled) {
542 cpu_ticks_offset = cpu_get_ticks();
543 cpu_ticks_enabled = 0;
547 static int64_t get_clock(void)
552 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
555 gettimeofday(&tv, NULL);
556 return tv.tv_sec * 1000000LL + tv.tv_usec;
560 void cpu_calibrate_ticks(void)
565 ticks = cpu_get_real_ticks();
571 usec = get_clock() - usec;
572 ticks = cpu_get_real_ticks() - ticks;
573 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
576 /* compute with 96 bit intermediate result: (a*b)/c */
577 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
582 #ifdef WORDS_BIGENDIAN
592 rl = (uint64_t)u.l.low * (uint64_t)b;
593 rh = (uint64_t)u.l.high * (uint64_t)b;
596 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
600 #define QEMU_TIMER_REALTIME 0
601 #define QEMU_TIMER_VIRTUAL 1
605 /* XXX: add frequency */
613 struct QEMUTimer *next;
619 static QEMUTimer *active_timers[2];
621 static MMRESULT timerID;
623 /* frequency of the times() clock tick */
624 static int timer_freq;
627 QEMUClock *qemu_new_clock(int type)
630 clock = qemu_mallocz(sizeof(QEMUClock));
637 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
641 ts = qemu_mallocz(sizeof(QEMUTimer));
648 void qemu_free_timer(QEMUTimer *ts)
653 /* stop a timer, but do not dealloc it */
654 void qemu_del_timer(QEMUTimer *ts)
658 /* NOTE: this code must be signal safe because
659 qemu_timer_expired() can be called from a signal. */
660 pt = &active_timers[ts->clock->type];
673 /* modify the current timer so that it will be fired when current_time
674 >= expire_time. The corresponding callback will be called. */
675 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
681 /* add the timer in the sorted list */
682 /* NOTE: this code must be signal safe because
683 qemu_timer_expired() can be called from a signal. */
684 pt = &active_timers[ts->clock->type];
689 if (t->expire_time > expire_time)
693 ts->expire_time = expire_time;
698 int qemu_timer_pending(QEMUTimer *ts)
701 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
708 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
712 return (timer_head->expire_time <= current_time);
715 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
721 if (!ts || ts->expire_time > current_time)
723 /* remove timer from the list before calling the callback */
724 *ptimer_head = ts->next;
727 /* run the callback (the timer list can be modified) */
732 int64_t qemu_get_clock(QEMUClock *clock)
734 switch(clock->type) {
735 case QEMU_TIMER_REALTIME:
737 return GetTickCount();
742 /* Note that using gettimeofday() is not a good solution
743 for timers because its value change when the date is
745 if (timer_freq == 100) {
746 return times(&tp) * 10;
748 return ((int64_t)times(&tp) * 1000) / timer_freq;
753 case QEMU_TIMER_VIRTUAL:
754 return cpu_get_ticks();
759 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
761 uint64_t expire_time;
763 if (qemu_timer_pending(ts)) {
764 expire_time = ts->expire_time;
768 qemu_put_be64(f, expire_time);
771 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
773 uint64_t expire_time;
775 expire_time = qemu_get_be64(f);
776 if (expire_time != -1) {
777 qemu_mod_timer(ts, expire_time);
783 static void timer_save(QEMUFile *f, void *opaque)
785 if (cpu_ticks_enabled) {
786 hw_error("cannot save state if virtual timers are running");
788 qemu_put_be64s(f, &cpu_ticks_offset);
789 qemu_put_be64s(f, &ticks_per_sec);
792 static int timer_load(QEMUFile *f, void *opaque, int version_id)
796 if (cpu_ticks_enabled) {
799 qemu_get_be64s(f, &cpu_ticks_offset);
800 qemu_get_be64s(f, &ticks_per_sec);
805 void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
806 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
808 static void host_alarm_handler(int host_signum)
812 #define DISP_FREQ 1000
814 static int64_t delta_min = INT64_MAX;
815 static int64_t delta_max, delta_cum, last_clock, delta, ti;
817 ti = qemu_get_clock(vm_clock);
818 if (last_clock != 0) {
819 delta = ti - last_clock;
820 if (delta < delta_min)
822 if (delta > delta_max)
825 if (++count == DISP_FREQ) {
826 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
827 muldiv64(delta_min, 1000000, ticks_per_sec),
828 muldiv64(delta_max, 1000000, ticks_per_sec),
829 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
830 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
832 delta_min = INT64_MAX;
840 if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
841 qemu_get_clock(vm_clock)) ||
842 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
843 qemu_get_clock(rt_clock))) {
844 /* stop the cpu because a timer occured */
845 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
851 #if defined(__linux__)
853 #define RTC_FREQ 1024
857 static int start_rtc_timer(void)
859 rtc_fd = open("/dev/rtc", O_RDONLY);
862 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
863 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
864 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
865 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
868 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
873 pit_min_timer_count = PIT_FREQ / RTC_FREQ;
879 static int start_rtc_timer(void)
884 #endif /* !defined(__linux__) */
886 #endif /* !defined(_WIN32) */
888 static void init_timers(void)
890 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
891 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
896 timerID = timeSetEvent(10, // interval (ms)
898 host_alarm_handler, // function
899 (DWORD)&count, // user parameter
900 TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
902 perror("failed timer alarm");
906 pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
909 struct sigaction act;
910 struct itimerval itv;
912 /* get times() syscall frequency */
913 timer_freq = sysconf(_SC_CLK_TCK);
916 sigfillset(&act.sa_mask);
918 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
919 act.sa_flags |= SA_ONSTACK;
921 act.sa_handler = host_alarm_handler;
922 sigaction(SIGALRM, &act, NULL);
924 itv.it_interval.tv_sec = 0;
925 itv.it_interval.tv_usec = 999; /* for i386 kernel 2.6 to get 1 ms */
926 itv.it_value.tv_sec = 0;
927 itv.it_value.tv_usec = 10 * 1000;
928 setitimer(ITIMER_REAL, &itv, NULL);
929 /* we probe the tick duration of the kernel to inform the user if
930 the emulated kernel requested a too high timer frequency */
931 getitimer(ITIMER_REAL, &itv);
933 #if defined(__linux__)
934 if (itv.it_interval.tv_usec > 1000) {
935 /* try to use /dev/rtc to have a faster timer */
936 if (start_rtc_timer() < 0)
939 itv.it_interval.tv_sec = 0;
940 itv.it_interval.tv_usec = 0;
941 itv.it_value.tv_sec = 0;
942 itv.it_value.tv_usec = 0;
943 setitimer(ITIMER_REAL, &itv, NULL);
946 sigaction(SIGIO, &act, NULL);
947 fcntl(rtc_fd, F_SETFL, O_ASYNC);
948 fcntl(rtc_fd, F_SETOWN, getpid());
950 #endif /* defined(__linux__) */
953 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec *
960 void quit_timers(void)
963 timeKillEvent(timerID);
967 /***********************************************************/
968 /* character device */
970 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
972 return s->chr_write(s, buf, len);
975 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
980 vsnprintf(buf, sizeof(buf), fmt, ap);
981 qemu_chr_write(s, buf, strlen(buf));
985 void qemu_chr_send_event(CharDriverState *s, int event)
987 if (s->chr_send_event)
988 s->chr_send_event(s, event);
991 void qemu_chr_add_read_handler(CharDriverState *s,
992 IOCanRWHandler *fd_can_read,
993 IOReadHandler *fd_read, void *opaque)
995 s->chr_add_read_handler(s, fd_can_read, fd_read, opaque);
998 void qemu_chr_add_event_handler(CharDriverState *s, IOEventHandler *chr_event)
1000 s->chr_event = chr_event;
1003 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1008 static void null_chr_add_read_handler(CharDriverState *chr,
1009 IOCanRWHandler *fd_can_read,
1010 IOReadHandler *fd_read, void *opaque)
1014 CharDriverState *qemu_chr_open_null(void)
1016 CharDriverState *chr;
1018 chr = qemu_mallocz(sizeof(CharDriverState));
1021 chr->chr_write = null_chr_write;
1022 chr->chr_add_read_handler = null_chr_add_read_handler;
1030 /* for nographic stdio only */
1031 IOCanRWHandler *fd_can_read;
1032 IOReadHandler *fd_read;
1036 #define STDIO_MAX_CLIENTS 2
1038 static int stdio_nb_clients;
1039 static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS];
1041 static int unix_write(int fd, const uint8_t *buf, int len1)
1047 ret = write(fd, buf, len);
1049 if (errno != EINTR && errno != EAGAIN)
1051 } else if (ret == 0) {
1061 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1063 FDCharDriver *s = chr->opaque;
1064 return unix_write(s->fd_out, buf, len);
1067 static void fd_chr_add_read_handler(CharDriverState *chr,
1068 IOCanRWHandler *fd_can_read,
1069 IOReadHandler *fd_read, void *opaque)
1071 FDCharDriver *s = chr->opaque;
1073 if (nographic && s->fd_in == 0) {
1074 s->fd_can_read = fd_can_read;
1075 s->fd_read = fd_read;
1076 s->fd_opaque = opaque;
1078 qemu_add_fd_read_handler(s->fd_in, fd_can_read, fd_read, opaque);
1082 /* open a character device to a unix fd */
1083 CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
1085 CharDriverState *chr;
1088 chr = qemu_mallocz(sizeof(CharDriverState));
1091 s = qemu_mallocz(sizeof(FDCharDriver));
1099 chr->chr_write = fd_chr_write;
1100 chr->chr_add_read_handler = fd_chr_add_read_handler;
1104 /* for STDIO, we handle the case where several clients use it
1107 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1109 static int term_got_escape, client_index;
1111 void term_print_help(void)
1114 "C-a h print this help\n"
1115 "C-a x exit emulator\n"
1116 "C-a s save disk data back to file (if -snapshot)\n"
1117 "C-a b send break (magic sysrq)\n"
1118 "C-a c switch between console and monitor\n"
1119 "C-a C-a send C-a\n"
1123 /* called when a char is received */
1124 static void stdio_received_byte(int ch)
1126 if (term_got_escape) {
1127 term_got_escape = 0;
1138 for (i = 0; i < MAX_DISKS; i++) {
1140 bdrv_commit(bs_table[i]);
1145 if (client_index < stdio_nb_clients) {
1146 CharDriverState *chr;
1149 chr = stdio_clients[client_index];
1151 chr->chr_event(s->fd_opaque, CHR_EVENT_BREAK);
1156 if (client_index >= stdio_nb_clients)
1158 if (client_index == 0) {
1159 /* send a new line in the monitor to get the prompt */
1167 } else if (ch == TERM_ESCAPE) {
1168 term_got_escape = 1;
1171 if (client_index < stdio_nb_clients) {
1173 CharDriverState *chr;
1176 chr = stdio_clients[client_index];
1179 /* XXX: should queue the char if the device is not
1181 if (s->fd_can_read(s->fd_opaque) > 0)
1182 s->fd_read(s->fd_opaque, buf, 1);
1187 static int stdio_can_read(void *opaque)
1189 /* XXX: not strictly correct */
1193 static void stdio_read(void *opaque, const uint8_t *buf, int size)
1196 for(i = 0; i < size; i++)
1197 stdio_received_byte(buf[i]);
1200 /* init terminal so that we can grab keys */
1201 static struct termios oldtty;
1202 static int old_fd0_flags;
1204 static void term_exit(void)
1206 tcsetattr (0, TCSANOW, &oldtty);
1207 fcntl(0, F_SETFL, old_fd0_flags);
1210 static void term_init(void)
1214 tcgetattr (0, &tty);
1216 old_fd0_flags = fcntl(0, F_GETFL);
1218 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1219 |INLCR|IGNCR|ICRNL|IXON);
1220 tty.c_oflag |= OPOST;
1221 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1222 /* if graphical mode, we allow Ctrl-C handling */
1224 tty.c_lflag &= ~ISIG;
1225 tty.c_cflag &= ~(CSIZE|PARENB);
1228 tty.c_cc[VTIME] = 0;
1230 tcsetattr (0, TCSANOW, &tty);
1234 fcntl(0, F_SETFL, O_NONBLOCK);
1237 CharDriverState *qemu_chr_open_stdio(void)
1239 CharDriverState *chr;
1242 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
1244 chr = qemu_chr_open_fd(0, 1);
1245 if (stdio_nb_clients == 0)
1246 qemu_add_fd_read_handler(0, stdio_can_read, stdio_read, NULL);
1247 client_index = stdio_nb_clients;
1249 if (stdio_nb_clients != 0)
1251 chr = qemu_chr_open_fd(0, 1);
1253 stdio_clients[stdio_nb_clients++] = chr;
1254 if (stdio_nb_clients == 1) {
1255 /* set the terminal in raw mode */
1261 #if defined(__linux__)
1262 CharDriverState *qemu_chr_open_pty(void)
1264 char slave_name[1024];
1265 int master_fd, slave_fd;
1267 /* Not satisfying */
1268 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
1271 fprintf(stderr, "char device redirected to %s\n", slave_name);
1272 return qemu_chr_open_fd(master_fd, master_fd);
1275 CharDriverState *qemu_chr_open_pty(void)
1281 #endif /* !defined(_WIN32) */
1283 CharDriverState *qemu_chr_open(const char *filename)
1285 if (!strcmp(filename, "vc")) {
1286 return text_console_init(&display_state);
1287 } else if (!strcmp(filename, "null")) {
1288 return qemu_chr_open_null();
1291 if (!strcmp(filename, "pty")) {
1292 return qemu_chr_open_pty();
1293 } else if (!strcmp(filename, "stdio")) {
1294 return qemu_chr_open_stdio();
1302 /***********************************************************/
1303 /* Linux network device redirectors */
1305 void hex_dump(FILE *f, const uint8_t *buf, int size)
1309 for(i=0;i<size;i+=16) {
1313 fprintf(f, "%08x ", i);
1316 fprintf(f, " %02x", buf[i+j]);
1321 for(j=0;j<len;j++) {
1323 if (c < ' ' || c > '~')
1325 fprintf(f, "%c", c);
1331 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1333 nd->send_packet(nd, buf, size);
1336 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
1337 IOReadHandler *fd_read, void *opaque)
1339 nd->add_read_packet(nd, fd_can_read, fd_read, opaque);
1342 /* dummy network adapter */
1344 static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1348 static void dummy_add_read_packet(NetDriverState *nd,
1349 IOCanRWHandler *fd_can_read,
1350 IOReadHandler *fd_read, void *opaque)
1354 static int net_dummy_init(NetDriverState *nd)
1356 nd->send_packet = dummy_send_packet;
1357 nd->add_read_packet = dummy_add_read_packet;
1358 pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy");
1362 #if defined(CONFIG_SLIRP)
1364 /* slirp network adapter */
1366 static void *slirp_fd_opaque;
1367 static IOCanRWHandler *slirp_fd_can_read;
1368 static IOReadHandler *slirp_fd_read;
1369 static int slirp_inited;
1371 int slirp_can_output(void)
1373 return slirp_fd_can_read(slirp_fd_opaque);
1376 void slirp_output(const uint8_t *pkt, int pkt_len)
1379 printf("output:\n");
1380 hex_dump(stdout, pkt, pkt_len);
1382 slirp_fd_read(slirp_fd_opaque, pkt, pkt_len);
1385 static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1389 hex_dump(stdout, buf, size);
1391 slirp_input(buf, size);
1394 static void slirp_add_read_packet(NetDriverState *nd,
1395 IOCanRWHandler *fd_can_read,
1396 IOReadHandler *fd_read, void *opaque)
1398 slirp_fd_opaque = opaque;
1399 slirp_fd_can_read = fd_can_read;
1400 slirp_fd_read = fd_read;
1403 static int net_slirp_init(NetDriverState *nd)
1405 if (!slirp_inited) {
1409 nd->send_packet = slirp_send_packet;
1410 nd->add_read_packet = slirp_add_read_packet;
1411 pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp");
1415 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
1420 p1 = strchr(p, sep);
1426 if (len > buf_size - 1)
1428 memcpy(buf, p, len);
1435 static void net_slirp_redir(const char *redir_str)
1440 struct in_addr guest_addr;
1441 int host_port, guest_port;
1443 if (!slirp_inited) {
1449 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1451 if (!strcmp(buf, "tcp")) {
1453 } else if (!strcmp(buf, "udp")) {
1459 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1461 host_port = strtol(buf, &r, 0);
1465 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1467 if (buf[0] == '\0') {
1468 pstrcpy(buf, sizeof(buf), "10.0.2.15");
1470 if (!inet_aton(buf, &guest_addr))
1473 guest_port = strtol(p, &r, 0);
1477 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
1478 fprintf(stderr, "qemu: could not set up redirection\n");
1483 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
1491 static void smb_exit(void)
1495 char filename[1024];
1497 /* erase all the files in the directory */
1498 d = opendir(smb_dir);
1503 if (strcmp(de->d_name, ".") != 0 &&
1504 strcmp(de->d_name, "..") != 0) {
1505 snprintf(filename, sizeof(filename), "%s/%s",
1506 smb_dir, de->d_name);
1514 /* automatic user mode samba server configuration */
1515 void net_slirp_smb(const char *exported_dir)
1517 char smb_conf[1024];
1518 char smb_cmdline[1024];
1521 if (!slirp_inited) {
1526 /* XXX: better tmp dir construction */
1527 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
1528 if (mkdir(smb_dir, 0700) < 0) {
1529 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
1532 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
1534 f = fopen(smb_conf, "w");
1536 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
1541 "pid directory=%s\n"
1542 "lock directory=%s\n"
1543 "log file=%s/log.smbd\n"
1544 "smb passwd file=%s/smbpasswd\n"
1545 "security = share\n"
1559 snprintf(smb_cmdline, sizeof(smb_cmdline), "/usr/sbin/smbd -s %s",
1562 slirp_add_exec(0, smb_cmdline, 4, 139);
1565 #endif /* !defined(_WIN32) */
1567 #endif /* CONFIG_SLIRP */
1569 #if !defined(_WIN32)
1571 static int tun_open(char *ifname, int ifname_size)
1577 fd = open("/dev/tap", O_RDWR);
1579 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1584 dev = devname(s.st_rdev, S_IFCHR);
1585 pstrcpy(ifname, ifname_size, dev);
1587 fcntl(fd, F_SETFL, O_NONBLOCK);
1591 static int tun_open(char *ifname, int ifname_size)
1596 fd = open("/dev/net/tun", O_RDWR);
1598 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1601 memset(&ifr, 0, sizeof(ifr));
1602 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1603 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
1604 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1606 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1610 printf("Connected to host network interface: %s\n", ifr.ifr_name);
1611 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1612 fcntl(fd, F_SETFL, O_NONBLOCK);
1617 static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1619 write(nd->fd, buf, size);
1622 static void tun_add_read_packet(NetDriverState *nd,
1623 IOCanRWHandler *fd_can_read,
1624 IOReadHandler *fd_read, void *opaque)
1626 qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque);
1629 static int net_tun_init(NetDriverState *nd)
1635 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
1639 /* try to launch network init script */
1644 *parg++ = network_script;
1645 *parg++ = nd->ifname;
1647 execv(network_script, args);
1650 while (waitpid(pid, &status, 0) != pid);
1651 if (!WIFEXITED(status) ||
1652 WEXITSTATUS(status) != 0) {
1653 fprintf(stderr, "%s: could not launch network script\n",
1657 nd->send_packet = tun_send_packet;
1658 nd->add_read_packet = tun_add_read_packet;
1662 static int net_fd_init(NetDriverState *nd, int fd)
1665 nd->send_packet = tun_send_packet;
1666 nd->add_read_packet = tun_add_read_packet;
1667 pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
1671 #endif /* !_WIN32 */
1673 /***********************************************************/
1676 static char *pid_filename;
1678 /* Remove PID file. Called on normal exit */
1680 static void remove_pidfile(void)
1682 unlink (pid_filename);
1685 static void create_pidfile(const char *filename)
1687 struct stat pidstat;
1690 /* Try to write our PID to the named file */
1691 if (stat(filename, &pidstat) < 0) {
1692 if (errno == ENOENT) {
1693 if ((f = fopen (filename, "w")) == NULL) {
1694 perror("Opening pidfile");
1697 fprintf(f, "%d\n", getpid());
1699 pid_filename = qemu_strdup(filename);
1700 if (!pid_filename) {
1701 fprintf(stderr, "Could not save PID filename");
1704 atexit(remove_pidfile);
1707 fprintf(stderr, "%s already exists. Remove it and try again.\n",
1713 /***********************************************************/
1716 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
1720 static void dumb_resize(DisplayState *ds, int w, int h)
1724 static void dumb_refresh(DisplayState *ds)
1726 vga_update_display();
1729 void dumb_display_init(DisplayState *ds)
1734 ds->dpy_update = dumb_update;
1735 ds->dpy_resize = dumb_resize;
1736 ds->dpy_refresh = dumb_refresh;
1739 #if !defined(CONFIG_SOFTMMU)
1740 /***********************************************************/
1741 /* cpu signal handler */
1742 static void host_segv_handler(int host_signum, siginfo_t *info,
1745 if (cpu_signal_handler(host_signum, info, puc))
1747 if (stdio_nb_clients > 0)
1753 /***********************************************************/
1756 #define MAX_IO_HANDLERS 64
1758 typedef struct IOHandlerRecord {
1760 IOCanRWHandler *fd_can_read;
1761 IOReadHandler *fd_read;
1763 /* temporary data */
1766 struct IOHandlerRecord *next;
1769 static IOHandlerRecord *first_io_handler;
1771 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
1772 IOReadHandler *fd_read, void *opaque)
1774 IOHandlerRecord *ioh;
1776 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1780 ioh->fd_can_read = fd_can_read;
1781 ioh->fd_read = fd_read;
1782 ioh->opaque = opaque;
1783 ioh->next = first_io_handler;
1784 first_io_handler = ioh;
1788 void qemu_del_fd_read_handler(int fd)
1790 IOHandlerRecord **pioh, *ioh;
1792 pioh = &first_io_handler;
1797 if (ioh->fd == fd) {
1805 /***********************************************************/
1806 /* savevm/loadvm support */
1808 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
1810 fwrite(buf, 1, size, f);
1813 void qemu_put_byte(QEMUFile *f, int v)
1818 void qemu_put_be16(QEMUFile *f, unsigned int v)
1820 qemu_put_byte(f, v >> 8);
1821 qemu_put_byte(f, v);
1824 void qemu_put_be32(QEMUFile *f, unsigned int v)
1826 qemu_put_byte(f, v >> 24);
1827 qemu_put_byte(f, v >> 16);
1828 qemu_put_byte(f, v >> 8);
1829 qemu_put_byte(f, v);
1832 void qemu_put_be64(QEMUFile *f, uint64_t v)
1834 qemu_put_be32(f, v >> 32);
1835 qemu_put_be32(f, v);
1838 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
1840 return fread(buf, 1, size, f);
1843 int qemu_get_byte(QEMUFile *f)
1853 unsigned int qemu_get_be16(QEMUFile *f)
1856 v = qemu_get_byte(f) << 8;
1857 v |= qemu_get_byte(f);
1861 unsigned int qemu_get_be32(QEMUFile *f)
1864 v = qemu_get_byte(f) << 24;
1865 v |= qemu_get_byte(f) << 16;
1866 v |= qemu_get_byte(f) << 8;
1867 v |= qemu_get_byte(f);
1871 uint64_t qemu_get_be64(QEMUFile *f)
1874 v = (uint64_t)qemu_get_be32(f) << 32;
1875 v |= qemu_get_be32(f);
1879 int64_t qemu_ftell(QEMUFile *f)
1884 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
1886 if (fseek(f, pos, whence) < 0)
1891 typedef struct SaveStateEntry {
1895 SaveStateHandler *save_state;
1896 LoadStateHandler *load_state;
1898 struct SaveStateEntry *next;
1901 static SaveStateEntry *first_se;
1903 int register_savevm(const char *idstr,
1906 SaveStateHandler *save_state,
1907 LoadStateHandler *load_state,
1910 SaveStateEntry *se, **pse;
1912 se = qemu_malloc(sizeof(SaveStateEntry));
1915 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1916 se->instance_id = instance_id;
1917 se->version_id = version_id;
1918 se->save_state = save_state;
1919 se->load_state = load_state;
1920 se->opaque = opaque;
1923 /* add at the end of list */
1925 while (*pse != NULL)
1926 pse = &(*pse)->next;
1931 #define QEMU_VM_FILE_MAGIC 0x5145564d
1932 #define QEMU_VM_FILE_VERSION 0x00000001
1934 int qemu_savevm(const char *filename)
1938 int len, len_pos, cur_pos, saved_vm_running, ret;
1940 saved_vm_running = vm_running;
1943 f = fopen(filename, "wb");
1949 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1950 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1952 for(se = first_se; se != NULL; se = se->next) {
1954 len = strlen(se->idstr);
1955 qemu_put_byte(f, len);
1956 qemu_put_buffer(f, se->idstr, len);
1958 qemu_put_be32(f, se->instance_id);
1959 qemu_put_be32(f, se->version_id);
1961 /* record size: filled later */
1963 qemu_put_be32(f, 0);
1965 se->save_state(f, se->opaque);
1967 /* fill record size */
1969 len = ftell(f) - len_pos - 4;
1970 fseek(f, len_pos, SEEK_SET);
1971 qemu_put_be32(f, len);
1972 fseek(f, cur_pos, SEEK_SET);
1978 if (saved_vm_running)
1983 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1987 for(se = first_se; se != NULL; se = se->next) {
1988 if (!strcmp(se->idstr, idstr) &&
1989 instance_id == se->instance_id)
1995 int qemu_loadvm(const char *filename)
1999 int len, cur_pos, ret, instance_id, record_len, version_id;
2000 int saved_vm_running;
2004 saved_vm_running = vm_running;
2007 f = fopen(filename, "rb");
2013 v = qemu_get_be32(f);
2014 if (v != QEMU_VM_FILE_MAGIC)
2016 v = qemu_get_be32(f);
2017 if (v != QEMU_VM_FILE_VERSION) {
2024 #if defined (DO_TB_FLUSH)
2025 tb_flush(global_env);
2027 len = qemu_get_byte(f);
2030 qemu_get_buffer(f, idstr, len);
2032 instance_id = qemu_get_be32(f);
2033 version_id = qemu_get_be32(f);
2034 record_len = qemu_get_be32(f);
2036 printf("idstr=%s instance=0x%x version=%d len=%d\n",
2037 idstr, instance_id, version_id, record_len);
2040 se = find_se(idstr, instance_id);
2042 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
2043 instance_id, idstr);
2045 ret = se->load_state(f, se->opaque, version_id);
2047 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2048 instance_id, idstr);
2051 /* always seek to exact end of record */
2052 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
2057 if (saved_vm_running)
2062 /***********************************************************/
2063 /* cpu save/restore */
2065 #if defined(TARGET_I386)
2067 static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
2069 qemu_put_be32(f, dt->selector);
2070 qemu_put_betl(f, dt->base);
2071 qemu_put_be32(f, dt->limit);
2072 qemu_put_be32(f, dt->flags);
2075 static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
2077 dt->selector = qemu_get_be32(f);
2078 dt->base = qemu_get_betl(f);
2079 dt->limit = qemu_get_be32(f);
2080 dt->flags = qemu_get_be32(f);
2083 void cpu_save(QEMUFile *f, void *opaque)
2085 CPUState *env = opaque;
2086 uint16_t fptag, fpus, fpuc, fpregs_format;
2090 for(i = 0; i < CPU_NB_REGS; i++)
2091 qemu_put_betls(f, &env->regs[i]);
2092 qemu_put_betls(f, &env->eip);
2093 qemu_put_betls(f, &env->eflags);
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 = 0; i < 8; i++) {
2102 fptag |= ((!env->fptags[i]) << i);
2105 qemu_put_be16s(f, &fpuc);
2106 qemu_put_be16s(f, &fpus);
2107 qemu_put_be16s(f, &fptag);
2109 #ifdef USE_X86LDOUBLE
2114 qemu_put_be16s(f, &fpregs_format);
2116 for(i = 0; i < 8; i++) {
2117 #ifdef USE_X86LDOUBLE
2121 /* we save the real CPU data (in case of MMX usage only 'mant'
2122 contains the MMX register */
2123 cpu_get_fp80(&mant, &exp, env->fpregs[i].d);
2124 qemu_put_be64(f, mant);
2125 qemu_put_be16(f, exp);
2128 /* if we use doubles for float emulation, we save the doubles to
2129 avoid losing information in case of MMX usage. It can give
2130 problems if the image is restored on a CPU where long
2131 doubles are used instead. */
2132 qemu_put_be64(f, env->fpregs[i].mmx.MMX_Q(0));
2136 for(i = 0; i < 6; i++)
2137 cpu_put_seg(f, &env->segs[i]);
2138 cpu_put_seg(f, &env->ldt);
2139 cpu_put_seg(f, &env->tr);
2140 cpu_put_seg(f, &env->gdt);
2141 cpu_put_seg(f, &env->idt);
2143 qemu_put_be32s(f, &env->sysenter_cs);
2144 qemu_put_be32s(f, &env->sysenter_esp);
2145 qemu_put_be32s(f, &env->sysenter_eip);
2147 qemu_put_betls(f, &env->cr[0]);
2148 qemu_put_betls(f, &env->cr[2]);
2149 qemu_put_betls(f, &env->cr[3]);
2150 qemu_put_betls(f, &env->cr[4]);
2152 for(i = 0; i < 8; i++)
2153 qemu_put_betls(f, &env->dr[i]);
2156 qemu_put_be32s(f, &env->a20_mask);
2159 qemu_put_be32s(f, &env->mxcsr);
2160 for(i = 0; i < CPU_NB_REGS; i++) {
2161 qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(0));
2162 qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(1));
2165 #ifdef TARGET_X86_64
2166 qemu_put_be64s(f, &env->efer);
2167 qemu_put_be64s(f, &env->star);
2168 qemu_put_be64s(f, &env->lstar);
2169 qemu_put_be64s(f, &env->cstar);
2170 qemu_put_be64s(f, &env->fmask);
2171 qemu_put_be64s(f, &env->kernelgsbase);
2175 #ifdef USE_X86LDOUBLE
2176 /* XXX: add that in a FPU generic layer */
2177 union x86_longdouble {
2182 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
2183 #define EXPBIAS1 1023
2184 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
2185 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
2187 static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp)
2191 p->mant = (MANTD1(temp) << 11) | (1LL << 63);
2192 /* exponent + sign */
2193 e = EXPD1(temp) - EXPBIAS1 + 16383;
2194 e |= SIGND1(temp) >> 16;
2199 int cpu_load(QEMUFile *f, void *opaque, int version_id)
2201 CPUState *env = opaque;
2204 uint16_t fpus, fpuc, fptag, fpregs_format;
2206 if (version_id != 3)
2208 for(i = 0; i < CPU_NB_REGS; i++)
2209 qemu_get_betls(f, &env->regs[i]);
2210 qemu_get_betls(f, &env->eip);
2211 qemu_get_betls(f, &env->eflags);
2212 qemu_get_be32s(f, &hflags);
2214 qemu_get_be16s(f, &fpuc);
2215 qemu_get_be16s(f, &fpus);
2216 qemu_get_be16s(f, &fptag);
2217 qemu_get_be16s(f, &fpregs_format);
2219 /* NOTE: we cannot always restore the FPU state if the image come
2220 from a host with a different 'USE_X86LDOUBLE' define. We guess
2221 if we are in an MMX state to restore correctly in that case. */
2222 guess_mmx = ((fptag == 0xff) && (fpus & 0x3800) == 0);
2223 for(i = 0; i < 8; i++) {
2227 switch(fpregs_format) {
2229 mant = qemu_get_be64(f);
2230 exp = qemu_get_be16(f);
2231 #ifdef USE_X86LDOUBLE
2232 env->fpregs[i].d = cpu_set_fp80(mant, exp);
2234 /* difficult case */
2236 env->fpregs[i].mmx.MMX_Q(0) = mant;
2238 env->fpregs[i].d = cpu_set_fp80(mant, exp);
2242 mant = qemu_get_be64(f);
2243 #ifdef USE_X86LDOUBLE
2245 union x86_longdouble *p;
2246 /* difficult case */
2247 p = (void *)&env->fpregs[i];
2252 fp64_to_fp80(p, mant);
2256 env->fpregs[i].mmx.MMX_Q(0) = mant;
2265 env->fpstt = (fpus >> 11) & 7;
2266 env->fpus = fpus & ~0x3800;
2268 for(i = 0; i < 8; i++) {
2269 env->fptags[i] = (fptag >> i) & 1;
2272 for(i = 0; i < 6; i++)
2273 cpu_get_seg(f, &env->segs[i]);
2274 cpu_get_seg(f, &env->ldt);
2275 cpu_get_seg(f, &env->tr);
2276 cpu_get_seg(f, &env->gdt);
2277 cpu_get_seg(f, &env->idt);
2279 qemu_get_be32s(f, &env->sysenter_cs);
2280 qemu_get_be32s(f, &env->sysenter_esp);
2281 qemu_get_be32s(f, &env->sysenter_eip);
2283 qemu_get_betls(f, &env->cr[0]);
2284 qemu_get_betls(f, &env->cr[2]);
2285 qemu_get_betls(f, &env->cr[3]);
2286 qemu_get_betls(f, &env->cr[4]);
2288 for(i = 0; i < 8; i++)
2289 qemu_get_betls(f, &env->dr[i]);
2292 qemu_get_be32s(f, &env->a20_mask);
2294 qemu_get_be32s(f, &env->mxcsr);
2295 for(i = 0; i < CPU_NB_REGS; i++) {
2296 qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(0));
2297 qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(1));
2300 #ifdef TARGET_X86_64
2301 qemu_get_be64s(f, &env->efer);
2302 qemu_get_be64s(f, &env->star);
2303 qemu_get_be64s(f, &env->lstar);
2304 qemu_get_be64s(f, &env->cstar);
2305 qemu_get_be64s(f, &env->fmask);
2306 qemu_get_be64s(f, &env->kernelgsbase);
2309 /* XXX: compute hflags from scratch, except for CPL and IIF */
2310 env->hflags = hflags;
2315 #elif defined(TARGET_PPC)
2316 void cpu_save(QEMUFile *f, void *opaque)
2320 int cpu_load(QEMUFile *f, void *opaque, int version_id)
2324 #elif defined(TARGET_SPARC)
2325 void cpu_save(QEMUFile *f, void *opaque)
2327 CPUState *env = opaque;
2331 for(i = 0; i < 8; i++)
2332 qemu_put_betls(f, &env->gregs[i]);
2333 for(i = 0; i < NWINDOWS * 16; i++)
2334 qemu_put_betls(f, &env->regbase[i]);
2337 for(i = 0; i < TARGET_FPREGS; i++) {
2343 qemu_put_betl(f, u.i);
2346 qemu_put_betls(f, &env->pc);
2347 qemu_put_betls(f, &env->npc);
2348 qemu_put_betls(f, &env->y);
2350 qemu_put_be32(f, tmp);
2351 qemu_put_be32s(f, &env->fsr);
2352 qemu_put_be32s(f, &env->wim);
2353 qemu_put_be32s(f, &env->tbr);
2355 for(i = 0; i < 16; i++)
2356 qemu_put_be32s(f, &env->mmuregs[i]);
2359 int cpu_load(QEMUFile *f, void *opaque, int version_id)
2361 CPUState *env = opaque;
2365 for(i = 0; i < 8; i++)
2366 qemu_get_betls(f, &env->gregs[i]);
2367 for(i = 0; i < NWINDOWS * 16; i++)
2368 qemu_get_betls(f, &env->regbase[i]);
2371 for(i = 0; i < TARGET_FPREGS; i++) {
2376 u.i = qemu_get_betl(f);
2380 qemu_get_betls(f, &env->pc);
2381 qemu_get_betls(f, &env->npc);
2382 qemu_get_betls(f, &env->y);
2383 tmp = qemu_get_be32(f);
2384 env->cwp = 0; /* needed to ensure that the wrapping registers are
2385 correctly updated */
2387 qemu_get_be32s(f, &env->fsr);
2388 qemu_get_be32s(f, &env->wim);
2389 qemu_get_be32s(f, &env->tbr);
2391 for(i = 0; i < 16; i++)
2392 qemu_get_be32s(f, &env->mmuregs[i]);
2399 #warning No CPU save/restore functions
2403 /***********************************************************/
2404 /* ram save/restore */
2406 /* we just avoid storing empty pages */
2407 static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
2412 for(i = 1; i < len; i++) {
2416 qemu_put_byte(f, 1);
2417 qemu_put_byte(f, v);
2420 qemu_put_byte(f, 0);
2421 qemu_put_buffer(f, buf, len);
2424 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
2428 v = qemu_get_byte(f);
2431 if (qemu_get_buffer(f, buf, len) != len)
2435 v = qemu_get_byte(f);
2436 memset(buf, v, len);
2444 static void ram_save(QEMUFile *f, void *opaque)
2447 qemu_put_be32(f, phys_ram_size);
2448 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
2449 ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
2453 static int ram_load(QEMUFile *f, void *opaque, int version_id)
2457 if (version_id != 1)
2459 if (qemu_get_be32(f) != phys_ram_size)
2461 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
2462 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
2469 /***********************************************************/
2470 /* main execution loop */
2472 void gui_update(void *opaque)
2474 display_state.dpy_refresh(&display_state);
2475 qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
2478 /* XXX: support several handlers */
2479 VMStopHandler *vm_stop_cb;
2480 VMStopHandler *vm_stop_opaque;
2482 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
2485 vm_stop_opaque = opaque;
2489 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
2502 void vm_stop(int reason)
2505 cpu_disable_ticks();
2509 vm_stop_cb(vm_stop_opaque, reason);
2515 /* reset/shutdown handler */
2517 typedef struct QEMUResetEntry {
2518 QEMUResetHandler *func;
2520 struct QEMUResetEntry *next;
2523 static QEMUResetEntry *first_reset_entry;
2524 static int reset_requested;
2525 static int shutdown_requested;
2527 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
2529 QEMUResetEntry **pre, *re;
2531 pre = &first_reset_entry;
2532 while (*pre != NULL)
2533 pre = &(*pre)->next;
2534 re = qemu_mallocz(sizeof(QEMUResetEntry));
2536 re->opaque = opaque;
2541 void qemu_system_reset(void)
2545 /* reset all devices */
2546 for(re = first_reset_entry; re != NULL; re = re->next) {
2547 re->func(re->opaque);
2551 void qemu_system_reset_request(void)
2553 reset_requested = 1;
2554 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
2557 void qemu_system_shutdown_request(void)
2559 shutdown_requested = 1;
2560 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
2563 static void main_cpu_reset(void *opaque)
2565 #if defined(TARGET_I386) || defined(TARGET_SPARC)
2566 CPUState *env = opaque;
2571 void main_loop_wait(int timeout)
2574 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
2575 IOHandlerRecord *ioh, *ioh_next;
2585 /* poll any events */
2586 /* XXX: separate device handlers from system ones */
2588 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2589 if (!ioh->fd_can_read) {
2592 pf->events = POLLIN;
2596 max_size = ioh->fd_can_read(ioh->opaque);
2598 if (max_size > sizeof(buf))
2599 max_size = sizeof(buf);
2601 pf->events = POLLIN;
2608 ioh->max_size = max_size;
2611 ret = poll(ufds, pf - ufds, timeout);
2613 /* XXX: better handling of removal */
2614 for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
2615 ioh_next = ioh->next;
2618 if (pf->revents & POLLIN) {
2619 if (ioh->max_size == 0) {
2620 /* just a read event */
2621 ioh->fd_read(ioh->opaque, NULL, 0);
2623 n = read(ioh->fd, buf, ioh->max_size);
2625 ioh->fd_read(ioh->opaque, buf, n);
2626 } else if (errno != EAGAIN) {
2627 ioh->fd_read(ioh->opaque, NULL, -errno);
2634 #endif /* !defined(_WIN32) */
2635 #if defined(CONFIG_SLIRP)
2636 /* XXX: merge with poll() */
2638 fd_set rfds, wfds, xfds;
2646 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2649 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
2651 slirp_select_poll(&rfds, &wfds, &xfds);
2657 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
2658 qemu_get_clock(vm_clock));
2659 /* run dma transfers, if any */
2663 /* real time timers */
2664 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
2665 qemu_get_clock(rt_clock));
2671 CPUState *env = global_env;
2675 ret = cpu_exec(env);
2676 if (shutdown_requested) {
2677 ret = EXCP_INTERRUPT;
2680 if (reset_requested) {
2681 reset_requested = 0;
2682 qemu_system_reset();
2683 ret = EXCP_INTERRUPT;
2685 if (ret == EXCP_DEBUG) {
2686 vm_stop(EXCP_DEBUG);
2688 /* if hlt instruction, we wait until the next IRQ */
2689 /* XXX: use timeout computed from timers */
2690 if (ret == EXCP_HLT)
2697 main_loop_wait(timeout);
2699 cpu_disable_ticks();
2705 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n"
2706 "usage: %s [options] [disk_image]\n"
2708 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2710 "Standard options:\n"
2711 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
2712 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
2713 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
2714 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
2715 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
2716 "-snapshot write to temporary files instead of disk image files\n"
2717 "-m megs set virtual RAM size to megs MB [default=%d]\n"
2718 "-nographic disable graphical output and redirect serial I/Os to console\n"
2720 "-k language use keyboard layout (for example \"fr\" for French)\n"
2722 "-enable-audio enable audio support\n"
2723 "-localtime set the real time clock to local time [default=utc]\n"
2724 "-full-screen start in full screen\n"
2726 "-prep Simulate a PREP system (default is PowerMAC)\n"
2727 "-g WxH[xDEPTH] Set the initial VGA graphic mode\n"
2730 "Network options:\n"
2731 "-nics n simulate 'n' network cards [default=1]\n"
2732 "-macaddr addr set the mac address of the first interface\n"
2733 "-n script set tap/tun network init script [default=%s]\n"
2734 "-tun-fd fd use this fd as already opened tap/tun interface\n"
2736 "-user-net use user mode network stack [default if no tap/tun script]\n"
2737 "-tftp prefix allow tftp access to files starting with prefix [-user-net]\n"
2739 "-smb dir allow SMB access to files in 'dir' [-user-net]\n"
2741 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
2742 " redirect TCP or UDP connections from host to guest [-user-net]\n"
2744 "-dummy-net use dummy network stack\n"
2746 "Linux boot specific:\n"
2747 "-kernel bzImage use 'bzImage' as kernel image\n"
2748 "-append cmdline use 'cmdline' as kernel command line\n"
2749 "-initrd file use 'file' as initial ram disk\n"
2751 "Debug/Expert options:\n"
2752 "-monitor dev redirect the monitor to char device 'dev'\n"
2753 "-serial dev redirect the serial port to char device 'dev'\n"
2754 "-parallel dev redirect the parallel port to char device 'dev'\n"
2755 "-pidfile file Write PID to 'file'\n"
2756 "-S freeze CPU at startup (use 'c' to start execution)\n"
2757 "-s wait gdb connection to port %d\n"
2758 "-p port change gdb connection port\n"
2759 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
2760 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
2761 " translation (t=none or lba) (usually qemu can guess them)\n"
2762 "-L path set the directory for the BIOS and VGA BIOS\n"
2764 "-no-kqemu disable KQEMU kernel module usage\n"
2766 #ifdef USE_CODE_COPY
2767 "-no-code-copy disable code copy acceleration\n"
2770 "-isa simulate an ISA-only system (default is PCI system)\n"
2771 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
2772 " (default is CL-GD5446 PCI VGA)\n"
2774 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
2776 "During emulation, the following keys are useful:\n"
2777 "ctrl-alt-f toggle full screen\n"
2778 "ctrl-alt-n switch to virtual console 'n'\n"
2779 "ctrl-alt toggle mouse and keyboard grab\n"
2781 "When using -nographic, press 'ctrl-a h' to get some help.\n"
2783 #ifdef CONFIG_SOFTMMU
2789 DEFAULT_NETWORK_SCRIPT,
2790 DEFAULT_GDBSTUB_PORT,
2792 #ifndef CONFIG_SOFTMMU
2794 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
2795 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
2801 #define HAS_ARG 0x0001
2814 QEMU_OPTION_snapshot,
2816 QEMU_OPTION_nographic,
2817 QEMU_OPTION_enable_audio,
2820 QEMU_OPTION_macaddr,
2823 QEMU_OPTION_user_net,
2827 QEMU_OPTION_dummy_net,
2839 QEMU_OPTION_no_code_copy,
2844 QEMU_OPTION_localtime,
2845 QEMU_OPTION_cirrusvga,
2847 QEMU_OPTION_std_vga,
2848 QEMU_OPTION_monitor,
2850 QEMU_OPTION_parallel,
2852 QEMU_OPTION_full_screen,
2853 QEMU_OPTION_pidfile,
2854 QEMU_OPTION_no_kqemu,
2857 typedef struct QEMUOption {
2863 const QEMUOption qemu_options[] = {
2864 { "h", 0, QEMU_OPTION_h },
2866 { "fda", HAS_ARG, QEMU_OPTION_fda },
2867 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
2868 { "hda", HAS_ARG, QEMU_OPTION_hda },
2869 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
2870 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
2871 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
2872 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
2873 { "boot", HAS_ARG, QEMU_OPTION_boot },
2874 { "snapshot", 0, QEMU_OPTION_snapshot },
2875 { "m", HAS_ARG, QEMU_OPTION_m },
2876 { "nographic", 0, QEMU_OPTION_nographic },
2877 { "k", HAS_ARG, QEMU_OPTION_k },
2878 { "enable-audio", 0, QEMU_OPTION_enable_audio },
2880 { "nics", HAS_ARG, QEMU_OPTION_nics},
2881 { "macaddr", HAS_ARG, QEMU_OPTION_macaddr},
2882 { "n", HAS_ARG, QEMU_OPTION_n },
2883 { "tun-fd", HAS_ARG, QEMU_OPTION_tun_fd },
2885 { "user-net", 0, QEMU_OPTION_user_net },
2886 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
2888 { "smb", HAS_ARG, QEMU_OPTION_smb },
2890 { "redir", HAS_ARG, QEMU_OPTION_redir },
2892 { "dummy-net", 0, QEMU_OPTION_dummy_net },
2894 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
2895 { "append", HAS_ARG, QEMU_OPTION_append },
2896 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
2898 { "S", 0, QEMU_OPTION_S },
2899 { "s", 0, QEMU_OPTION_s },
2900 { "p", HAS_ARG, QEMU_OPTION_p },
2901 { "d", HAS_ARG, QEMU_OPTION_d },
2902 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
2903 { "L", HAS_ARG, QEMU_OPTION_L },
2904 { "no-code-copy", 0, QEMU_OPTION_no_code_copy },
2906 { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
2909 { "prep", 0, QEMU_OPTION_prep },
2910 { "g", 1, QEMU_OPTION_g },
2912 { "localtime", 0, QEMU_OPTION_localtime },
2913 { "isa", 0, QEMU_OPTION_isa },
2914 { "std-vga", 0, QEMU_OPTION_std_vga },
2915 { "monitor", 1, QEMU_OPTION_monitor },
2916 { "serial", 1, QEMU_OPTION_serial },
2917 { "parallel", 1, QEMU_OPTION_parallel },
2918 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
2919 { "full-screen", 0, QEMU_OPTION_full_screen },
2920 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
2922 /* temporary options */
2923 { "pci", 0, QEMU_OPTION_pci },
2924 { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
2928 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2930 /* this stack is only used during signal handling */
2931 #define SIGNAL_STACK_SIZE 32768
2933 static uint8_t *signal_stack;
2937 /* password input */
2939 static BlockDriverState *get_bdrv(int index)
2941 BlockDriverState *bs;
2944 bs = bs_table[index];
2945 } else if (index < 6) {
2946 bs = fd_table[index - 4];
2953 static void read_passwords(void)
2955 BlockDriverState *bs;
2959 for(i = 0; i < 6; i++) {
2961 if (bs && bdrv_is_encrypted(bs)) {
2962 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs));
2963 for(j = 0; j < 3; j++) {
2964 monitor_readline("Password: ",
2965 1, password, sizeof(password));
2966 if (bdrv_set_key(bs, password) == 0)
2968 term_printf("invalid password\n");
2974 #define NET_IF_TUN 0
2975 #define NET_IF_USER 1
2976 #define NET_IF_DUMMY 2
2978 int main(int argc, char **argv)
2980 #ifdef CONFIG_GDBSTUB
2981 int use_gdbstub, gdbstub_port;
2984 int snapshot, linux_boot;
2986 const char *initrd_filename;
2987 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
2988 const char *kernel_filename, *kernel_cmdline;
2989 DisplayState *ds = &display_state;
2990 int cyls, heads, secs, translation;
2991 int start_emulation = 1;
2993 int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
2995 const char *r, *optarg;
2996 CharDriverState *monitor_hd;
2997 char monitor_device[128];
2998 char serial_devices[MAX_SERIAL_PORTS][128];
2999 int serial_device_index;
3000 char parallel_devices[MAX_PARALLEL_PORTS][128];
3001 int parallel_device_index;
3002 const char *loadvm = NULL;
3004 #if !defined(CONFIG_SOFTMMU)
3005 /* we never want that malloc() uses mmap() */
3006 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
3008 initrd_filename = NULL;
3009 for(i = 0; i < MAX_FD; i++)
3010 fd_filename[i] = NULL;
3011 for(i = 0; i < MAX_DISKS; i++)
3012 hd_filename[i] = NULL;
3013 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3014 vga_ram_size = VGA_RAM_SIZE;
3015 bios_size = BIOS_SIZE;
3016 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
3017 #ifdef CONFIG_GDBSTUB
3019 gdbstub_port = DEFAULT_GDBSTUB_PORT;
3023 kernel_filename = NULL;
3024 kernel_cmdline = "";
3026 cyls = heads = secs = 0;
3027 translation = BIOS_ATA_TRANSLATION_AUTO;
3028 pstrcpy(monitor_device, sizeof(monitor_device), "vc");
3030 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc");
3031 for(i = 1; i < MAX_SERIAL_PORTS; i++)
3032 serial_devices[i][0] = '\0';
3033 serial_device_index = 0;
3035 pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "vc");
3036 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
3037 parallel_devices[i][0] = '\0';
3038 parallel_device_index = 0;
3043 /* default mac address of the first network interface */
3057 hd_filename[0] = argv[optind++];
3059 const QEMUOption *popt;
3062 popt = qemu_options;
3065 fprintf(stderr, "%s: invalid option -- '%s'\n",
3069 if (!strcmp(popt->name, r + 1))
3073 if (popt->flags & HAS_ARG) {
3074 if (optind >= argc) {
3075 fprintf(stderr, "%s: option '%s' requires an argument\n",
3079 optarg = argv[optind++];
3084 switch(popt->index) {
3085 case QEMU_OPTION_initrd:
3086 initrd_filename = optarg;
3088 case QEMU_OPTION_hda:
3089 hd_filename[0] = optarg;
3091 case QEMU_OPTION_hdb:
3092 hd_filename[1] = optarg;
3094 case QEMU_OPTION_snapshot:
3097 case QEMU_OPTION_hdachs:
3101 cyls = strtol(p, (char **)&p, 0);
3102 if (cyls < 1 || cyls > 16383)
3107 heads = strtol(p, (char **)&p, 0);
3108 if (heads < 1 || heads > 16)
3113 secs = strtol(p, (char **)&p, 0);
3114 if (secs < 1 || secs > 63)
3118 if (!strcmp(p, "none"))
3119 translation = BIOS_ATA_TRANSLATION_NONE;
3120 else if (!strcmp(p, "lba"))
3121 translation = BIOS_ATA_TRANSLATION_LBA;
3122 else if (!strcmp(p, "auto"))
3123 translation = BIOS_ATA_TRANSLATION_AUTO;
3126 } else if (*p != '\0') {
3128 fprintf(stderr, "qemu: invalid physical CHS format\n");
3133 case QEMU_OPTION_nographic:
3134 pstrcpy(monitor_device, sizeof(monitor_device), "stdio");
3135 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio");
3138 case QEMU_OPTION_kernel:
3139 kernel_filename = optarg;
3141 case QEMU_OPTION_append:
3142 kernel_cmdline = optarg;
3144 case QEMU_OPTION_tun_fd:
3148 net_if_type = NET_IF_TUN;
3149 if (nb_tun_fds < MAX_NICS) {
3150 fd = strtol(optarg, (char **)&p, 0);
3152 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
3155 tun_fds[nb_tun_fds++] = fd;
3159 case QEMU_OPTION_hdc:
3160 hd_filename[2] = optarg;
3163 case QEMU_OPTION_hdd:
3164 hd_filename[3] = optarg;
3166 case QEMU_OPTION_cdrom:
3167 hd_filename[2] = optarg;
3170 case QEMU_OPTION_boot:
3171 boot_device = optarg[0];
3172 if (boot_device != 'a' &&
3173 boot_device != 'c' && boot_device != 'd') {
3174 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
3178 case QEMU_OPTION_fda:
3179 fd_filename[0] = optarg;
3181 case QEMU_OPTION_fdb:
3182 fd_filename[1] = optarg;
3184 case QEMU_OPTION_no_code_copy:
3185 code_copy_enabled = 0;
3187 case QEMU_OPTION_nics:
3188 nb_nics = atoi(optarg);
3189 if (nb_nics < 0 || nb_nics > MAX_NICS) {
3190 fprintf(stderr, "qemu: invalid number of network interfaces\n");
3194 case QEMU_OPTION_macaddr:
3199 for(i = 0; i < 6; i++) {
3200 macaddr[i] = strtol(p, (char **)&p, 16);
3207 fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
3216 case QEMU_OPTION_tftp:
3217 tftp_prefix = optarg;
3220 case QEMU_OPTION_smb:
3221 net_slirp_smb(optarg);
3224 case QEMU_OPTION_user_net:
3225 net_if_type = NET_IF_USER;
3227 case QEMU_OPTION_redir:
3228 net_slirp_redir(optarg);
3231 case QEMU_OPTION_dummy_net:
3232 net_if_type = NET_IF_DUMMY;
3234 case QEMU_OPTION_enable_audio:
3241 ram_size = atoi(optarg) * 1024 * 1024;
3244 if (ram_size > PHYS_RAM_MAX_SIZE) {
3245 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
3246 PHYS_RAM_MAX_SIZE / (1024 * 1024));
3255 mask = cpu_str_to_log_mask(optarg);
3257 printf("Log items (comma separated):\n");
3258 for(item = cpu_log_items; item->mask != 0; item++) {
3259 printf("%-10s %s\n", item->name, item->help);
3267 pstrcpy(network_script, sizeof(network_script), optarg);
3269 #ifdef CONFIG_GDBSTUB
3274 gdbstub_port = atoi(optarg);
3281 start_emulation = 0;
3283 case QEMU_OPTION_pci:
3286 case QEMU_OPTION_isa:
3289 case QEMU_OPTION_prep:
3293 keyboard_layout = optarg;
3295 case QEMU_OPTION_localtime:
3298 case QEMU_OPTION_cirrusvga:
3299 cirrus_vga_enabled = 1;
3301 case QEMU_OPTION_std_vga:
3302 cirrus_vga_enabled = 0;
3309 w = strtol(p, (char **)&p, 10);
3312 fprintf(stderr, "qemu: invalid resolution or depth\n");
3318 h = strtol(p, (char **)&p, 10);
3323 depth = strtol(p, (char **)&p, 10);
3324 if (depth != 8 && depth != 15 && depth != 16 &&
3325 depth != 24 && depth != 32)
3327 } else if (*p == '\0') {
3328 depth = graphic_depth;
3335 graphic_depth = depth;
3338 case QEMU_OPTION_monitor:
3339 pstrcpy(monitor_device, sizeof(monitor_device), optarg);
3341 case QEMU_OPTION_serial:
3342 if (serial_device_index >= MAX_SERIAL_PORTS) {
3343 fprintf(stderr, "qemu: too many serial ports\n");
3346 pstrcpy(serial_devices[serial_device_index],
3347 sizeof(serial_devices[0]), optarg);
3348 serial_device_index++;
3350 case QEMU_OPTION_parallel:
3351 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
3352 fprintf(stderr, "qemu: too many parallel ports\n");
3355 pstrcpy(parallel_devices[parallel_device_index],
3356 sizeof(parallel_devices[0]), optarg);
3357 parallel_device_index++;
3359 case QEMU_OPTION_loadvm:
3362 case QEMU_OPTION_full_screen:
3365 case QEMU_OPTION_pidfile:
3366 create_pidfile(optarg);
3369 case QEMU_OPTION_no_kqemu:
3377 linux_boot = (kernel_filename != NULL);
3379 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
3380 fd_filename[0] == '\0')
3383 /* boot to cd by default if no hard disk */
3384 if (hd_filename[0] == '\0' && boot_device == 'c') {
3385 if (fd_filename[0] != '\0')
3391 #if !defined(CONFIG_SOFTMMU)
3392 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
3394 static uint8_t stdout_buf[4096];
3395 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
3398 setvbuf(stdout, NULL, _IOLBF, 0);
3401 /* init host network redirectors */
3402 if (net_if_type == -1) {
3403 net_if_type = NET_IF_TUN;
3404 #if defined(CONFIG_SLIRP)
3405 if (access(network_script, R_OK) < 0) {
3406 net_if_type = NET_IF_USER;
3411 for(i = 0; i < nb_nics; i++) {
3412 NetDriverState *nd = &nd_table[i];
3414 /* init virtual mac address */
3415 nd->macaddr[0] = macaddr[0];
3416 nd->macaddr[1] = macaddr[1];
3417 nd->macaddr[2] = macaddr[2];
3418 nd->macaddr[3] = macaddr[3];
3419 nd->macaddr[4] = macaddr[4];
3420 nd->macaddr[5] = macaddr[5] + i;
3421 switch(net_if_type) {
3422 #if defined(CONFIG_SLIRP)
3427 #if !defined(_WIN32)
3429 if (i < nb_tun_fds) {
3430 net_fd_init(nd, tun_fds[i]);
3432 if (net_tun_init(nd) < 0)
3444 /* init the memory */
3445 phys_ram_size = ram_size + vga_ram_size + bios_size;
3447 #ifdef CONFIG_SOFTMMU
3448 phys_ram_base = qemu_vmalloc(phys_ram_size);
3449 if (!phys_ram_base) {
3450 fprintf(stderr, "Could not allocate physical memory\n");
3454 /* as we must map the same page at several addresses, we must use
3459 tmpdir = getenv("QEMU_TMPDIR");
3462 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
3463 if (mkstemp(phys_ram_file) < 0) {
3464 fprintf(stderr, "Could not create temporary memory file '%s'\n",
3468 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
3469 if (phys_ram_fd < 0) {
3470 fprintf(stderr, "Could not open temporary memory file '%s'\n",
3474 ftruncate(phys_ram_fd, phys_ram_size);
3475 unlink(phys_ram_file);
3476 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
3478 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
3480 if (phys_ram_base == MAP_FAILED) {
3481 fprintf(stderr, "Could not map physical memory\n");
3487 /* we always create the cdrom drive, even if no disk is there */
3490 bs_table[2] = bdrv_new("cdrom");
3491 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
3494 /* open the virtual block devices */
3495 for(i = 0; i < MAX_DISKS; i++) {
3496 if (hd_filename[i]) {
3499 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
3500 bs_table[i] = bdrv_new(buf);
3502 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
3503 fprintf(stderr, "qemu: could not open hard disk image '%s'\n",
3507 if (i == 0 && cyls != 0) {
3508 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
3509 bdrv_set_translation_hint(bs_table[i], translation);
3514 /* we always create at least one floppy disk */
3515 fd_table[0] = bdrv_new("fda");
3516 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
3518 for(i = 0; i < MAX_FD; i++) {
3519 if (fd_filename[i]) {
3522 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
3523 fd_table[i] = bdrv_new(buf);
3524 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
3526 if (fd_filename[i] != '\0') {
3527 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
3528 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
3536 /* init CPU state */
3539 cpu_single_env = env;
3541 register_savevm("timer", 0, 1, timer_save, timer_load, env);
3542 register_savevm("cpu", 0, 3, cpu_save, cpu_load, env);
3543 register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
3544 qemu_register_reset(main_cpu_reset, global_env);
3547 cpu_calibrate_ticks();
3551 dumb_display_init(ds);
3554 sdl_display_init(ds, full_screen);
3556 dumb_display_init(ds);
3560 vga_console = graphic_console_init(ds);
3562 monitor_hd = qemu_chr_open(monitor_device);
3564 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
3567 monitor_init(monitor_hd, !nographic);
3569 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
3570 if (serial_devices[i][0] != '\0') {
3571 serial_hds[i] = qemu_chr_open(serial_devices[i]);
3572 if (!serial_hds[i]) {
3573 fprintf(stderr, "qemu: could not open serial device '%s'\n",
3577 if (!strcmp(serial_devices[i], "vc"))
3578 qemu_chr_printf(serial_hds[i], "serial%d console\n", i);
3582 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
3583 if (parallel_devices[i][0] != '\0') {
3584 parallel_hds[i] = qemu_chr_open(parallel_devices[i]);
3585 if (!parallel_hds[i]) {
3586 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
3587 parallel_devices[i]);
3590 if (!strcmp(parallel_devices[i], "vc"))
3591 qemu_chr_printf(parallel_hds[i], "parallel%d console\n", i);
3595 /* setup cpu signal handlers for MMU / self modifying code handling */
3596 #if !defined(CONFIG_SOFTMMU)
3598 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3601 signal_stack = memalign(16, SIGNAL_STACK_SIZE);
3602 stk.ss_sp = signal_stack;
3603 stk.ss_size = SIGNAL_STACK_SIZE;
3606 if (sigaltstack(&stk, NULL) < 0) {
3607 perror("sigaltstack");
3613 struct sigaction act;
3615 sigfillset(&act.sa_mask);
3616 act.sa_flags = SA_SIGINFO;
3617 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3618 act.sa_flags |= SA_ONSTACK;
3620 act.sa_sigaction = host_segv_handler;
3621 sigaction(SIGSEGV, &act, NULL);
3622 sigaction(SIGBUS, &act, NULL);
3623 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3624 sigaction(SIGFPE, &act, NULL);
3631 struct sigaction act;
3632 sigfillset(&act.sa_mask);
3634 act.sa_handler = SIG_IGN;
3635 sigaction(SIGPIPE, &act, NULL);
3640 #if defined(TARGET_I386)
3641 pc_init(ram_size, vga_ram_size, boot_device,
3642 ds, fd_filename, snapshot,
3643 kernel_filename, kernel_cmdline, initrd_filename);
3644 #elif defined(TARGET_PPC)
3645 ppc_init(ram_size, vga_ram_size, boot_device,
3646 ds, fd_filename, snapshot,
3647 kernel_filename, kernel_cmdline, initrd_filename);
3648 #elif defined(TARGET_SPARC)
3649 sun4m_init(ram_size, vga_ram_size, boot_device,
3650 ds, fd_filename, snapshot,
3651 kernel_filename, kernel_cmdline, initrd_filename);
3654 gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
3655 qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
3657 #ifdef CONFIG_GDBSTUB
3659 if (gdbserver_start(gdbstub_port) < 0) {
3660 fprintf(stderr, "Could not open gdbserver socket on port %d\n",
3664 printf("Waiting gdb connection on port %d\n", gdbstub_port);
3669 qemu_loadvm(loadvm);
3672 /* XXX: simplify init */
3674 if (start_emulation) {