4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 #include <sys/times.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
50 #include <linux/if_tun.h>
53 #include <linux/rtc.h>
57 #if defined(CONFIG_SLIRP)
63 #include <sys/timeb.h>
65 #define getopt_long_only getopt_long
66 #define memalign(align, size) malloc(size)
73 #endif /* CONFIG_SDL */
81 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
83 //#define DEBUG_UNUSED_IOPORT
84 //#define DEBUG_IOPORT
86 #if !defined(CONFIG_SOFTMMU)
87 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
89 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
93 #define DEFAULT_RAM_SIZE 144
95 #define DEFAULT_RAM_SIZE 128
98 #define GUI_REFRESH_INTERVAL 30
100 /* XXX: use a two level table to limit memory usage */
101 #define MAX_IOPORTS 65536
103 const char *bios_dir = CONFIG_QEMU_SHAREDIR;
104 char phys_ram_file[1024];
105 CPUState *global_env;
106 CPUState *cpu_single_env;
107 void *ioport_opaque[MAX_IOPORTS];
108 IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
109 IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
110 BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
113 static DisplayState display_state;
115 int64_t ticks_per_sec;
116 int boot_device = 'c';
118 static char network_script[1024];
119 int pit_min_timer_count = 0;
121 NetDriverState nd_table[MAX_NICS];
122 QEMUTimer *gui_timer;
124 int audio_enabled = 0;
126 int prep_enabled = 0;
128 int cirrus_vga_enabled = 1;
129 int graphic_width = 800;
130 int graphic_height = 600;
131 int graphic_depth = 15;
132 TextConsole *vga_console;
133 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
135 /***********************************************************/
136 /* x86 ISA bus support */
138 target_phys_addr_t isa_mem_base = 0;
140 uint32_t default_ioport_readb(void *opaque, uint32_t address)
142 #ifdef DEBUG_UNUSED_IOPORT
143 fprintf(stderr, "inb: port=0x%04x\n", address);
148 void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
150 #ifdef DEBUG_UNUSED_IOPORT
151 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
155 /* default is to make two byte accesses */
156 uint32_t default_ioport_readw(void *opaque, uint32_t address)
159 data = ioport_read_table[0][address](ioport_opaque[address], address);
160 address = (address + 1) & (MAX_IOPORTS - 1);
161 data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;
165 void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
167 ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);
168 address = (address + 1) & (MAX_IOPORTS - 1);
169 ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);
172 uint32_t default_ioport_readl(void *opaque, uint32_t address)
174 #ifdef DEBUG_UNUSED_IOPORT
175 fprintf(stderr, "inl: port=0x%04x\n", address);
180 void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
182 #ifdef DEBUG_UNUSED_IOPORT
183 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
187 void init_ioports(void)
191 for(i = 0; i < MAX_IOPORTS; i++) {
192 ioport_read_table[0][i] = default_ioport_readb;
193 ioport_write_table[0][i] = default_ioport_writeb;
194 ioport_read_table[1][i] = default_ioport_readw;
195 ioport_write_table[1][i] = default_ioport_writew;
196 ioport_read_table[2][i] = default_ioport_readl;
197 ioport_write_table[2][i] = default_ioport_writel;
201 /* size is the word size in byte */
202 int register_ioport_read(int start, int length, int size,
203 IOPortReadFunc *func, void *opaque)
209 } else if (size == 2) {
211 } else if (size == 4) {
214 hw_error("register_ioport_read: invalid size");
217 for(i = start; i < start + length; i += size) {
218 ioport_read_table[bsize][i] = func;
219 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
220 hw_error("register_ioport_read: invalid opaque");
221 ioport_opaque[i] = opaque;
226 /* size is the word size in byte */
227 int register_ioport_write(int start, int length, int size,
228 IOPortWriteFunc *func, void *opaque)
234 } else if (size == 2) {
236 } else if (size == 4) {
239 hw_error("register_ioport_write: invalid size");
242 for(i = start; i < start + length; i += size) {
243 ioport_write_table[bsize][i] = func;
244 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
245 hw_error("register_ioport_read: invalid opaque");
246 ioport_opaque[i] = opaque;
251 void isa_unassign_ioport(int start, int length)
255 for(i = start; i < start + length; i++) {
256 ioport_read_table[0][i] = default_ioport_readb;
257 ioport_read_table[1][i] = default_ioport_readw;
258 ioport_read_table[2][i] = default_ioport_readl;
260 ioport_write_table[0][i] = default_ioport_writeb;
261 ioport_write_table[1][i] = default_ioport_writew;
262 ioport_write_table[2][i] = default_ioport_writel;
266 void pstrcpy(char *buf, int buf_size, const char *str)
276 if (c == 0 || q >= buf + buf_size - 1)
283 /* strcat and truncate. */
284 char *pstrcat(char *buf, int buf_size, const char *s)
289 pstrcpy(buf + len, buf_size - len, s);
293 int strstart(const char *str, const char *val, const char **ptr)
309 /* return the size or -1 if error */
310 int get_image_size(const char *filename)
313 fd = open(filename, O_RDONLY | O_BINARY);
316 size = lseek(fd, 0, SEEK_END);
321 /* return the size or -1 if error */
322 int load_image(const char *filename, uint8_t *addr)
325 fd = open(filename, O_RDONLY | O_BINARY);
328 size = lseek(fd, 0, SEEK_END);
329 lseek(fd, 0, SEEK_SET);
330 if (read(fd, addr, size) != size) {
338 void cpu_outb(CPUState *env, int addr, int val)
341 if (loglevel & CPU_LOG_IOPORT)
342 fprintf(logfile, "outb: %04x %02x\n", addr, val);
344 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
347 void cpu_outw(CPUState *env, int addr, int val)
350 if (loglevel & CPU_LOG_IOPORT)
351 fprintf(logfile, "outw: %04x %04x\n", addr, val);
353 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
356 void cpu_outl(CPUState *env, int addr, int val)
359 if (loglevel & CPU_LOG_IOPORT)
360 fprintf(logfile, "outl: %04x %08x\n", addr, val);
362 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
365 int cpu_inb(CPUState *env, int addr)
368 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
370 if (loglevel & CPU_LOG_IOPORT)
371 fprintf(logfile, "inb : %04x %02x\n", addr, val);
376 int cpu_inw(CPUState *env, int addr)
379 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
381 if (loglevel & CPU_LOG_IOPORT)
382 fprintf(logfile, "inw : %04x %04x\n", addr, val);
387 int cpu_inl(CPUState *env, int addr)
390 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
392 if (loglevel & CPU_LOG_IOPORT)
393 fprintf(logfile, "inl : %04x %08x\n", addr, val);
398 /***********************************************************/
399 void hw_error(const char *fmt, ...)
404 fprintf(stderr, "qemu: hardware error: ");
405 vfprintf(stderr, fmt, ap);
406 fprintf(stderr, "\n");
408 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
410 cpu_dump_state(global_env, stderr, 0);
416 /***********************************************************/
419 static QEMUPutKBDEvent *qemu_put_kbd_event;
420 static void *qemu_put_kbd_event_opaque;
421 static QEMUPutMouseEvent *qemu_put_mouse_event;
422 static void *qemu_put_mouse_event_opaque;
424 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
426 qemu_put_kbd_event_opaque = opaque;
427 qemu_put_kbd_event = func;
430 void qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, void *opaque)
432 qemu_put_mouse_event_opaque = opaque;
433 qemu_put_mouse_event = func;
436 void kbd_put_keycode(int keycode)
438 if (qemu_put_kbd_event) {
439 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
443 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
445 if (qemu_put_mouse_event) {
446 qemu_put_mouse_event(qemu_put_mouse_event_opaque,
447 dx, dy, dz, buttons_state);
451 /***********************************************************/
454 #if defined(__powerpc__)
456 static inline uint32_t get_tbl(void)
459 asm volatile("mftb %0" : "=r" (tbl));
463 static inline uint32_t get_tbu(void)
466 asm volatile("mftbu %0" : "=r" (tbl));
470 int64_t cpu_get_real_ticks(void)
473 /* NOTE: we test if wrapping has occurred */
479 return ((int64_t)h << 32) | l;
482 #elif defined(__i386__)
484 int64_t cpu_get_real_ticks(void)
487 asm volatile ("rdtsc" : "=A" (val));
491 #elif defined(__x86_64__)
493 int64_t cpu_get_real_ticks(void)
497 asm volatile("rdtsc" : "=a" (low), "=d" (high));
505 #error unsupported CPU
508 static int64_t cpu_ticks_offset;
509 static int cpu_ticks_enabled;
511 static inline int64_t cpu_get_ticks(void)
513 if (!cpu_ticks_enabled) {
514 return cpu_ticks_offset;
516 return cpu_get_real_ticks() + cpu_ticks_offset;
520 /* enable cpu_get_ticks() */
521 void cpu_enable_ticks(void)
523 if (!cpu_ticks_enabled) {
524 cpu_ticks_offset -= cpu_get_real_ticks();
525 cpu_ticks_enabled = 1;
529 /* disable cpu_get_ticks() : the clock is stopped. You must not call
530 cpu_get_ticks() after that. */
531 void cpu_disable_ticks(void)
533 if (cpu_ticks_enabled) {
534 cpu_ticks_offset = cpu_get_ticks();
535 cpu_ticks_enabled = 0;
539 static int64_t get_clock(void)
544 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
547 gettimeofday(&tv, NULL);
548 return tv.tv_sec * 1000000LL + tv.tv_usec;
552 void cpu_calibrate_ticks(void)
557 ticks = cpu_get_real_ticks();
563 usec = get_clock() - usec;
564 ticks = cpu_get_real_ticks() - ticks;
565 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
568 /* compute with 96 bit intermediate result: (a*b)/c */
569 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
574 #ifdef WORDS_BIGENDIAN
584 rl = (uint64_t)u.l.low * (uint64_t)b;
585 rh = (uint64_t)u.l.high * (uint64_t)b;
588 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
592 #define QEMU_TIMER_REALTIME 0
593 #define QEMU_TIMER_VIRTUAL 1
597 /* XXX: add frequency */
605 struct QEMUTimer *next;
611 static QEMUTimer *active_timers[2];
613 static MMRESULT timerID;
615 /* frequency of the times() clock tick */
616 static int timer_freq;
619 QEMUClock *qemu_new_clock(int type)
622 clock = qemu_mallocz(sizeof(QEMUClock));
629 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
633 ts = qemu_mallocz(sizeof(QEMUTimer));
640 void qemu_free_timer(QEMUTimer *ts)
645 /* stop a timer, but do not dealloc it */
646 void qemu_del_timer(QEMUTimer *ts)
650 /* NOTE: this code must be signal safe because
651 qemu_timer_expired() can be called from a signal. */
652 pt = &active_timers[ts->clock->type];
665 /* modify the current timer so that it will be fired when current_time
666 >= expire_time. The corresponding callback will be called. */
667 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
673 /* add the timer in the sorted list */
674 /* NOTE: this code must be signal safe because
675 qemu_timer_expired() can be called from a signal. */
676 pt = &active_timers[ts->clock->type];
681 if (t->expire_time > expire_time)
685 ts->expire_time = expire_time;
690 int qemu_timer_pending(QEMUTimer *ts)
693 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
700 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
704 return (timer_head->expire_time <= current_time);
707 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
713 if (ts->expire_time > current_time)
715 /* remove timer from the list before calling the callback */
716 *ptimer_head = ts->next;
719 /* run the callback (the timer list can be modified) */
724 int64_t qemu_get_clock(QEMUClock *clock)
726 switch(clock->type) {
727 case QEMU_TIMER_REALTIME:
729 return GetTickCount();
734 /* Note that using gettimeofday() is not a good solution
735 for timers because its value change when the date is
737 if (timer_freq == 100) {
738 return times(&tp) * 10;
740 return ((int64_t)times(&tp) * 1000) / timer_freq;
745 case QEMU_TIMER_VIRTUAL:
746 return cpu_get_ticks();
751 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
753 uint64_t expire_time;
755 if (qemu_timer_pending(ts)) {
756 expire_time = ts->expire_time;
760 qemu_put_be64(f, expire_time);
763 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
765 uint64_t expire_time;
767 expire_time = qemu_get_be64(f);
768 if (expire_time != -1) {
769 qemu_mod_timer(ts, expire_time);
775 static void timer_save(QEMUFile *f, void *opaque)
777 if (cpu_ticks_enabled) {
778 hw_error("cannot save state if virtual timers are running");
780 qemu_put_be64s(f, &cpu_ticks_offset);
781 qemu_put_be64s(f, &ticks_per_sec);
784 static int timer_load(QEMUFile *f, void *opaque, int version_id)
788 if (cpu_ticks_enabled) {
791 qemu_get_be64s(f, &cpu_ticks_offset);
792 qemu_get_be64s(f, &ticks_per_sec);
797 void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
798 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
800 static void host_alarm_handler(int host_signum)
804 #define DISP_FREQ 1000
806 static int64_t delta_min = INT64_MAX;
807 static int64_t delta_max, delta_cum, last_clock, delta, ti;
809 ti = qemu_get_clock(vm_clock);
810 if (last_clock != 0) {
811 delta = ti - last_clock;
812 if (delta < delta_min)
814 if (delta > delta_max)
817 if (++count == DISP_FREQ) {
818 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
819 muldiv64(delta_min, 1000000, ticks_per_sec),
820 muldiv64(delta_max, 1000000, ticks_per_sec),
821 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
822 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
824 delta_min = INT64_MAX;
832 if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
833 qemu_get_clock(vm_clock)) ||
834 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
835 qemu_get_clock(rt_clock))) {
836 /* stop the cpu because a timer occured */
837 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
843 #if defined(__linux__)
845 #define RTC_FREQ 1024
849 static int start_rtc_timer(void)
851 rtc_fd = open("/dev/rtc", O_RDONLY);
854 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
855 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
856 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
857 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
860 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
865 pit_min_timer_count = PIT_FREQ / RTC_FREQ;
871 static int start_rtc_timer(void)
876 #endif /* !defined(__linux__) */
878 #endif /* !defined(_WIN32) */
880 static void init_timers(void)
882 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
883 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
888 timerID = timeSetEvent(10, // interval (ms)
890 host_alarm_handler, // function
891 (DWORD)&count, // user parameter
892 TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
894 perror("failed timer alarm");
898 pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
901 struct sigaction act;
902 struct itimerval itv;
904 /* get times() syscall frequency */
905 timer_freq = sysconf(_SC_CLK_TCK);
908 sigfillset(&act.sa_mask);
910 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
911 act.sa_flags |= SA_ONSTACK;
913 act.sa_handler = host_alarm_handler;
914 sigaction(SIGALRM, &act, NULL);
916 itv.it_interval.tv_sec = 0;
917 itv.it_interval.tv_usec = 1000;
918 itv.it_value.tv_sec = 0;
919 itv.it_value.tv_usec = 10 * 1000;
920 setitimer(ITIMER_REAL, &itv, NULL);
921 /* we probe the tick duration of the kernel to inform the user if
922 the emulated kernel requested a too high timer frequency */
923 getitimer(ITIMER_REAL, &itv);
925 #if defined(__linux__)
926 if (itv.it_interval.tv_usec > 1000) {
927 /* try to use /dev/rtc to have a faster timer */
928 if (start_rtc_timer() < 0)
931 itv.it_interval.tv_sec = 0;
932 itv.it_interval.tv_usec = 0;
933 itv.it_value.tv_sec = 0;
934 itv.it_value.tv_usec = 0;
935 setitimer(ITIMER_REAL, &itv, NULL);
938 sigaction(SIGIO, &act, NULL);
939 fcntl(rtc_fd, F_SETFL, O_ASYNC);
940 fcntl(rtc_fd, F_SETOWN, getpid());
942 #endif /* defined(__linux__) */
945 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec *
952 void quit_timers(void)
955 timeKillEvent(timerID);
959 /***********************************************************/
960 /* character device */
962 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
964 return s->chr_write(s, buf, len);
967 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
972 vsnprintf(buf, sizeof(buf), fmt, ap);
973 qemu_chr_write(s, buf, strlen(buf));
977 void qemu_chr_send_event(CharDriverState *s, int event)
979 if (s->chr_send_event)
980 s->chr_send_event(s, event);
983 void qemu_chr_add_read_handler(CharDriverState *s,
984 IOCanRWHandler *fd_can_read,
985 IOReadHandler *fd_read, void *opaque)
987 s->chr_add_read_handler(s, fd_can_read, fd_read, opaque);
990 void qemu_chr_add_event_handler(CharDriverState *s, IOEventHandler *chr_event)
992 s->chr_event = chr_event;
995 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1000 static void null_chr_add_read_handler(CharDriverState *chr,
1001 IOCanRWHandler *fd_can_read,
1002 IOReadHandler *fd_read, void *opaque)
1006 CharDriverState *qemu_chr_open_null(void)
1008 CharDriverState *chr;
1010 chr = qemu_mallocz(sizeof(CharDriverState));
1013 chr->chr_write = null_chr_write;
1014 chr->chr_add_read_handler = null_chr_add_read_handler;
1022 /* for nographic stdio only */
1023 IOCanRWHandler *fd_can_read;
1024 IOReadHandler *fd_read;
1028 #define STDIO_MAX_CLIENTS 2
1030 static int stdio_nb_clients;
1031 static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS];
1033 static int unix_write(int fd, const uint8_t *buf, int len1)
1039 ret = write(fd, buf, len);
1041 if (errno != EINTR && errno != EAGAIN)
1043 } else if (ret == 0) {
1053 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1055 FDCharDriver *s = chr->opaque;
1056 return unix_write(s->fd_out, buf, len);
1059 static void fd_chr_add_read_handler(CharDriverState *chr,
1060 IOCanRWHandler *fd_can_read,
1061 IOReadHandler *fd_read, void *opaque)
1063 FDCharDriver *s = chr->opaque;
1065 if (nographic && s->fd_in == 0) {
1066 s->fd_can_read = fd_can_read;
1067 s->fd_read = fd_read;
1068 s->fd_opaque = opaque;
1070 qemu_add_fd_read_handler(s->fd_in, fd_can_read, fd_read, opaque);
1074 /* open a character device to a unix fd */
1075 CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
1077 CharDriverState *chr;
1080 chr = qemu_mallocz(sizeof(CharDriverState));
1083 s = qemu_mallocz(sizeof(FDCharDriver));
1091 chr->chr_write = fd_chr_write;
1092 chr->chr_add_read_handler = fd_chr_add_read_handler;
1096 /* for STDIO, we handle the case where several clients use it
1099 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1101 static int term_got_escape, client_index;
1103 void term_print_help(void)
1106 "C-a h print this help\n"
1107 "C-a x exit emulator\n"
1108 "C-a s save disk data back to file (if -snapshot)\n"
1109 "C-a b send break (magic sysrq)\n"
1110 "C-a c switch between console and monitor\n"
1111 "C-a C-a send C-a\n"
1115 /* called when a char is received */
1116 static void stdio_received_byte(int ch)
1118 if (term_got_escape) {
1119 term_got_escape = 0;
1130 for (i = 0; i < MAX_DISKS; i++) {
1132 bdrv_commit(bs_table[i]);
1137 if (client_index < stdio_nb_clients) {
1138 CharDriverState *chr;
1141 chr = stdio_clients[client_index];
1143 chr->chr_event(s->fd_opaque, CHR_EVENT_BREAK);
1148 if (client_index >= stdio_nb_clients)
1150 if (client_index == 0) {
1151 /* send a new line in the monitor to get the prompt */
1159 } else if (ch == TERM_ESCAPE) {
1160 term_got_escape = 1;
1163 if (client_index < stdio_nb_clients) {
1165 CharDriverState *chr;
1168 chr = stdio_clients[client_index];
1171 /* XXX: should queue the char if the device is not
1173 if (s->fd_can_read(s->fd_opaque) > 0)
1174 s->fd_read(s->fd_opaque, buf, 1);
1179 static int stdio_can_read(void *opaque)
1181 /* XXX: not strictly correct */
1185 static void stdio_read(void *opaque, const uint8_t *buf, int size)
1188 for(i = 0; i < size; i++)
1189 stdio_received_byte(buf[i]);
1192 /* init terminal so that we can grab keys */
1193 static struct termios oldtty;
1194 static int old_fd0_flags;
1196 static void term_exit(void)
1198 tcsetattr (0, TCSANOW, &oldtty);
1199 fcntl(0, F_SETFL, old_fd0_flags);
1202 static void term_init(void)
1206 tcgetattr (0, &tty);
1208 old_fd0_flags = fcntl(0, F_GETFL);
1210 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1211 |INLCR|IGNCR|ICRNL|IXON);
1212 tty.c_oflag |= OPOST;
1213 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1214 /* if graphical mode, we allow Ctrl-C handling */
1216 tty.c_lflag &= ~ISIG;
1217 tty.c_cflag &= ~(CSIZE|PARENB);
1220 tty.c_cc[VTIME] = 0;
1222 tcsetattr (0, TCSANOW, &tty);
1226 fcntl(0, F_SETFL, O_NONBLOCK);
1229 CharDriverState *qemu_chr_open_stdio(void)
1231 CharDriverState *chr;
1234 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
1236 chr = qemu_chr_open_fd(0, 1);
1237 if (stdio_nb_clients == 0)
1238 qemu_add_fd_read_handler(0, stdio_can_read, stdio_read, NULL);
1239 client_index = stdio_nb_clients;
1241 if (stdio_nb_clients != 0)
1243 chr = qemu_chr_open_fd(0, 1);
1245 stdio_clients[stdio_nb_clients++] = chr;
1246 if (stdio_nb_clients == 1) {
1247 /* set the terminal in raw mode */
1253 #if defined(__linux__)
1254 CharDriverState *qemu_chr_open_pty(void)
1256 char slave_name[1024];
1257 int master_fd, slave_fd;
1259 /* Not satisfying */
1260 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
1263 fprintf(stderr, "char device redirected to %s\n", slave_name);
1264 return qemu_chr_open_fd(master_fd, master_fd);
1267 CharDriverState *qemu_chr_open_pty(void)
1273 #endif /* !defined(_WIN32) */
1275 CharDriverState *qemu_chr_open(const char *filename)
1277 if (!strcmp(filename, "vc")) {
1278 return text_console_init(&display_state);
1279 } else if (!strcmp(filename, "null")) {
1280 return qemu_chr_open_null();
1283 if (!strcmp(filename, "pty")) {
1284 return qemu_chr_open_pty();
1285 } else if (!strcmp(filename, "stdio")) {
1286 return qemu_chr_open_stdio();
1294 /***********************************************************/
1295 /* Linux network device redirectors */
1297 void hex_dump(FILE *f, const uint8_t *buf, int size)
1301 for(i=0;i<size;i+=16) {
1305 fprintf(f, "%08x ", i);
1308 fprintf(f, " %02x", buf[i+j]);
1313 for(j=0;j<len;j++) {
1315 if (c < ' ' || c > '~')
1317 fprintf(f, "%c", c);
1323 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1325 nd->send_packet(nd, buf, size);
1328 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
1329 IOReadHandler *fd_read, void *opaque)
1331 nd->add_read_packet(nd, fd_can_read, fd_read, opaque);
1334 /* dummy network adapter */
1336 static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1340 static void dummy_add_read_packet(NetDriverState *nd,
1341 IOCanRWHandler *fd_can_read,
1342 IOReadHandler *fd_read, void *opaque)
1346 static int net_dummy_init(NetDriverState *nd)
1348 nd->send_packet = dummy_send_packet;
1349 nd->add_read_packet = dummy_add_read_packet;
1350 pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy");
1354 #if defined(CONFIG_SLIRP)
1356 /* slirp network adapter */
1358 static void *slirp_fd_opaque;
1359 static IOCanRWHandler *slirp_fd_can_read;
1360 static IOReadHandler *slirp_fd_read;
1361 static int slirp_inited;
1363 int slirp_can_output(void)
1365 return slirp_fd_can_read(slirp_fd_opaque);
1368 void slirp_output(const uint8_t *pkt, int pkt_len)
1371 printf("output:\n");
1372 hex_dump(stdout, pkt, pkt_len);
1374 slirp_fd_read(slirp_fd_opaque, pkt, pkt_len);
1377 static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1381 hex_dump(stdout, buf, size);
1383 slirp_input(buf, size);
1386 static void slirp_add_read_packet(NetDriverState *nd,
1387 IOCanRWHandler *fd_can_read,
1388 IOReadHandler *fd_read, void *opaque)
1390 slirp_fd_opaque = opaque;
1391 slirp_fd_can_read = fd_can_read;
1392 slirp_fd_read = fd_read;
1395 static int net_slirp_init(NetDriverState *nd)
1397 if (!slirp_inited) {
1401 nd->send_packet = slirp_send_packet;
1402 nd->add_read_packet = slirp_add_read_packet;
1403 pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp");
1407 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
1412 p1 = strchr(p, sep);
1418 if (len > buf_size - 1)
1420 memcpy(buf, p, len);
1427 static void net_slirp_redir(const char *redir_str)
1432 struct in_addr guest_addr;
1433 int host_port, guest_port;
1435 if (!slirp_inited) {
1441 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1443 if (!strcmp(buf, "tcp")) {
1445 } else if (!strcmp(buf, "udp")) {
1451 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1453 host_port = strtol(buf, &r, 0);
1457 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1459 if (buf[0] == '\0') {
1460 pstrcpy(buf, sizeof(buf), "10.0.2.15");
1462 if (!inet_aton(buf, &guest_addr))
1465 guest_port = strtol(p, &r, 0);
1469 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
1470 fprintf(stderr, "qemu: could not set up redirection\n");
1475 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
1483 static void smb_exit(void)
1487 char filename[1024];
1489 /* erase all the files in the directory */
1490 d = opendir(smb_dir);
1495 if (strcmp(de->d_name, ".") != 0 &&
1496 strcmp(de->d_name, "..") != 0) {
1497 snprintf(filename, sizeof(filename), "%s/%s",
1498 smb_dir, de->d_name);
1506 /* automatic user mode samba server configuration */
1507 void net_slirp_smb(const char *exported_dir)
1509 char smb_conf[1024];
1510 char smb_cmdline[1024];
1513 if (!slirp_inited) {
1518 /* XXX: better tmp dir construction */
1519 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
1520 if (mkdir(smb_dir, 0700) < 0) {
1521 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
1524 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
1526 f = fopen(smb_conf, "w");
1528 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
1533 "pid directory=%s\n"
1534 "lock directory=%s\n"
1535 "log file=%s/log.smbd\n"
1536 "smb passwd file=%s/smbpasswd\n"
1537 "security = share\n"
1551 snprintf(smb_cmdline, sizeof(smb_cmdline), "/usr/sbin/smbd -s %s",
1554 slirp_add_exec(0, smb_cmdline, 4, 139);
1557 #endif /* !defined(_WIN32) */
1559 #endif /* CONFIG_SLIRP */
1561 #if !defined(_WIN32)
1563 static int tun_open(char *ifname, int ifname_size)
1569 fd = open("/dev/tap", O_RDWR);
1571 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1576 dev = devname(s.st_rdev, S_IFCHR);
1577 pstrcpy(ifname, ifname_size, dev);
1579 fcntl(fd, F_SETFL, O_NONBLOCK);
1583 static int tun_open(char *ifname, int ifname_size)
1588 fd = open("/dev/net/tun", O_RDWR);
1590 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1593 memset(&ifr, 0, sizeof(ifr));
1594 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1595 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
1596 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1598 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1602 printf("Connected to host network interface: %s\n", ifr.ifr_name);
1603 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1604 fcntl(fd, F_SETFL, O_NONBLOCK);
1609 static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1611 write(nd->fd, buf, size);
1614 static void tun_add_read_packet(NetDriverState *nd,
1615 IOCanRWHandler *fd_can_read,
1616 IOReadHandler *fd_read, void *opaque)
1618 qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque);
1621 static int net_tun_init(NetDriverState *nd)
1627 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
1631 /* try to launch network init script */
1636 *parg++ = network_script;
1637 *parg++ = nd->ifname;
1639 execv(network_script, args);
1642 while (waitpid(pid, &status, 0) != pid);
1643 if (!WIFEXITED(status) ||
1644 WEXITSTATUS(status) != 0) {
1645 fprintf(stderr, "%s: could not launch network script\n",
1649 nd->send_packet = tun_send_packet;
1650 nd->add_read_packet = tun_add_read_packet;
1654 static int net_fd_init(NetDriverState *nd, int fd)
1657 nd->send_packet = tun_send_packet;
1658 nd->add_read_packet = tun_add_read_packet;
1659 pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
1663 #endif /* !_WIN32 */
1665 /***********************************************************/
1668 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
1672 static void dumb_resize(DisplayState *ds, int w, int h)
1676 static void dumb_refresh(DisplayState *ds)
1678 vga_update_display();
1681 void dumb_display_init(DisplayState *ds)
1686 ds->dpy_update = dumb_update;
1687 ds->dpy_resize = dumb_resize;
1688 ds->dpy_refresh = dumb_refresh;
1691 #if !defined(CONFIG_SOFTMMU)
1692 /***********************************************************/
1693 /* cpu signal handler */
1694 static void host_segv_handler(int host_signum, siginfo_t *info,
1697 if (cpu_signal_handler(host_signum, info, puc))
1699 if (stdio_nb_clients > 0)
1705 /***********************************************************/
1708 #define MAX_IO_HANDLERS 64
1710 typedef struct IOHandlerRecord {
1712 IOCanRWHandler *fd_can_read;
1713 IOReadHandler *fd_read;
1715 /* temporary data */
1718 struct IOHandlerRecord *next;
1721 static IOHandlerRecord *first_io_handler;
1723 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
1724 IOReadHandler *fd_read, void *opaque)
1726 IOHandlerRecord *ioh;
1728 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1732 ioh->fd_can_read = fd_can_read;
1733 ioh->fd_read = fd_read;
1734 ioh->opaque = opaque;
1735 ioh->next = first_io_handler;
1736 first_io_handler = ioh;
1740 void qemu_del_fd_read_handler(int fd)
1742 IOHandlerRecord **pioh, *ioh;
1744 pioh = &first_io_handler;
1749 if (ioh->fd == fd) {
1757 /***********************************************************/
1758 /* savevm/loadvm support */
1760 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
1762 fwrite(buf, 1, size, f);
1765 void qemu_put_byte(QEMUFile *f, int v)
1770 void qemu_put_be16(QEMUFile *f, unsigned int v)
1772 qemu_put_byte(f, v >> 8);
1773 qemu_put_byte(f, v);
1776 void qemu_put_be32(QEMUFile *f, unsigned int v)
1778 qemu_put_byte(f, v >> 24);
1779 qemu_put_byte(f, v >> 16);
1780 qemu_put_byte(f, v >> 8);
1781 qemu_put_byte(f, v);
1784 void qemu_put_be64(QEMUFile *f, uint64_t v)
1786 qemu_put_be32(f, v >> 32);
1787 qemu_put_be32(f, v);
1790 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
1792 return fread(buf, 1, size, f);
1795 int qemu_get_byte(QEMUFile *f)
1805 unsigned int qemu_get_be16(QEMUFile *f)
1808 v = qemu_get_byte(f) << 8;
1809 v |= qemu_get_byte(f);
1813 unsigned int qemu_get_be32(QEMUFile *f)
1816 v = qemu_get_byte(f) << 24;
1817 v |= qemu_get_byte(f) << 16;
1818 v |= qemu_get_byte(f) << 8;
1819 v |= qemu_get_byte(f);
1823 uint64_t qemu_get_be64(QEMUFile *f)
1826 v = (uint64_t)qemu_get_be32(f) << 32;
1827 v |= qemu_get_be32(f);
1831 int64_t qemu_ftell(QEMUFile *f)
1836 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
1838 if (fseek(f, pos, whence) < 0)
1843 typedef struct SaveStateEntry {
1847 SaveStateHandler *save_state;
1848 LoadStateHandler *load_state;
1850 struct SaveStateEntry *next;
1853 static SaveStateEntry *first_se;
1855 int register_savevm(const char *idstr,
1858 SaveStateHandler *save_state,
1859 LoadStateHandler *load_state,
1862 SaveStateEntry *se, **pse;
1864 se = qemu_malloc(sizeof(SaveStateEntry));
1867 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1868 se->instance_id = instance_id;
1869 se->version_id = version_id;
1870 se->save_state = save_state;
1871 se->load_state = load_state;
1872 se->opaque = opaque;
1875 /* add at the end of list */
1877 while (*pse != NULL)
1878 pse = &(*pse)->next;
1883 #define QEMU_VM_FILE_MAGIC 0x5145564d
1884 #define QEMU_VM_FILE_VERSION 0x00000001
1886 int qemu_savevm(const char *filename)
1890 int len, len_pos, cur_pos, saved_vm_running, ret;
1892 saved_vm_running = vm_running;
1895 f = fopen(filename, "wb");
1901 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1902 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1904 for(se = first_se; se != NULL; se = se->next) {
1906 len = strlen(se->idstr);
1907 qemu_put_byte(f, len);
1908 qemu_put_buffer(f, se->idstr, len);
1910 qemu_put_be32(f, se->instance_id);
1911 qemu_put_be32(f, se->version_id);
1913 /* record size: filled later */
1915 qemu_put_be32(f, 0);
1917 se->save_state(f, se->opaque);
1919 /* fill record size */
1921 len = ftell(f) - len_pos - 4;
1922 fseek(f, len_pos, SEEK_SET);
1923 qemu_put_be32(f, len);
1924 fseek(f, cur_pos, SEEK_SET);
1930 if (saved_vm_running)
1935 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1939 for(se = first_se; se != NULL; se = se->next) {
1940 if (!strcmp(se->idstr, idstr) &&
1941 instance_id == se->instance_id)
1947 int qemu_loadvm(const char *filename)
1951 int len, cur_pos, ret, instance_id, record_len, version_id;
1952 int saved_vm_running;
1956 saved_vm_running = vm_running;
1959 f = fopen(filename, "rb");
1965 v = qemu_get_be32(f);
1966 if (v != QEMU_VM_FILE_MAGIC)
1968 v = qemu_get_be32(f);
1969 if (v != QEMU_VM_FILE_VERSION) {
1976 #if defined (DO_TB_FLUSH)
1977 tb_flush(global_env);
1979 len = qemu_get_byte(f);
1982 qemu_get_buffer(f, idstr, len);
1984 instance_id = qemu_get_be32(f);
1985 version_id = qemu_get_be32(f);
1986 record_len = qemu_get_be32(f);
1988 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1989 idstr, instance_id, version_id, record_len);
1992 se = find_se(idstr, instance_id);
1994 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1995 instance_id, idstr);
1997 ret = se->load_state(f, se->opaque, version_id);
1999 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2000 instance_id, idstr);
2003 /* always seek to exact end of record */
2004 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
2009 if (saved_vm_running)
2014 /***********************************************************/
2015 /* cpu save/restore */
2017 #if defined(TARGET_I386)
2019 static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
2021 qemu_put_be32(f, dt->selector);
2022 qemu_put_be32(f, (uint32_t)dt->base);
2023 qemu_put_be32(f, dt->limit);
2024 qemu_put_be32(f, dt->flags);
2027 static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
2029 dt->selector = qemu_get_be32(f);
2030 dt->base = (uint8_t *)qemu_get_be32(f);
2031 dt->limit = qemu_get_be32(f);
2032 dt->flags = qemu_get_be32(f);
2035 void cpu_save(QEMUFile *f, void *opaque)
2037 CPUState *env = opaque;
2038 uint16_t fptag, fpus, fpuc;
2042 for(i = 0; i < 8; i++)
2043 qemu_put_be32s(f, &env->regs[i]);
2044 qemu_put_be32s(f, &env->eip);
2045 qemu_put_be32s(f, &env->eflags);
2046 qemu_put_be32s(f, &env->eflags);
2047 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
2048 qemu_put_be32s(f, &hflags);
2052 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2054 for (i=7; i>=0; i--) {
2056 if (env->fptags[i]) {
2061 qemu_put_be16s(f, &fpuc);
2062 qemu_put_be16s(f, &fpus);
2063 qemu_put_be16s(f, &fptag);
2065 for(i = 0; i < 8; i++) {
2068 cpu_get_fp80(&mant, &exp, env->fpregs[i]);
2069 qemu_put_be64(f, mant);
2070 qemu_put_be16(f, exp);
2073 for(i = 0; i < 6; i++)
2074 cpu_put_seg(f, &env->segs[i]);
2075 cpu_put_seg(f, &env->ldt);
2076 cpu_put_seg(f, &env->tr);
2077 cpu_put_seg(f, &env->gdt);
2078 cpu_put_seg(f, &env->idt);
2080 qemu_put_be32s(f, &env->sysenter_cs);
2081 qemu_put_be32s(f, &env->sysenter_esp);
2082 qemu_put_be32s(f, &env->sysenter_eip);
2084 qemu_put_be32s(f, &env->cr[0]);
2085 qemu_put_be32s(f, &env->cr[2]);
2086 qemu_put_be32s(f, &env->cr[3]);
2087 qemu_put_be32s(f, &env->cr[4]);
2089 for(i = 0; i < 8; i++)
2090 qemu_put_be32s(f, &env->dr[i]);
2093 qemu_put_be32s(f, &env->a20_mask);
2096 int cpu_load(QEMUFile *f, void *opaque, int version_id)
2098 CPUState *env = opaque;
2101 uint16_t fpus, fpuc, fptag;
2103 if (version_id != 2)
2105 for(i = 0; i < 8; i++)
2106 qemu_get_be32s(f, &env->regs[i]);
2107 qemu_get_be32s(f, &env->eip);
2108 qemu_get_be32s(f, &env->eflags);
2109 qemu_get_be32s(f, &env->eflags);
2110 qemu_get_be32s(f, &hflags);
2112 qemu_get_be16s(f, &fpuc);
2113 qemu_get_be16s(f, &fpus);
2114 qemu_get_be16s(f, &fptag);
2116 for(i = 0; i < 8; i++) {
2119 mant = qemu_get_be64(f);
2120 exp = qemu_get_be16(f);
2121 env->fpregs[i] = cpu_set_fp80(mant, exp);
2125 env->fpstt = (fpus >> 11) & 7;
2126 env->fpus = fpus & ~0x3800;
2127 for(i = 0; i < 8; i++) {
2128 env->fptags[i] = ((fptag & 3) == 3);
2132 for(i = 0; i < 6; i++)
2133 cpu_get_seg(f, &env->segs[i]);
2134 cpu_get_seg(f, &env->ldt);
2135 cpu_get_seg(f, &env->tr);
2136 cpu_get_seg(f, &env->gdt);
2137 cpu_get_seg(f, &env->idt);
2139 qemu_get_be32s(f, &env->sysenter_cs);
2140 qemu_get_be32s(f, &env->sysenter_esp);
2141 qemu_get_be32s(f, &env->sysenter_eip);
2143 qemu_get_be32s(f, &env->cr[0]);
2144 qemu_get_be32s(f, &env->cr[2]);
2145 qemu_get_be32s(f, &env->cr[3]);
2146 qemu_get_be32s(f, &env->cr[4]);
2148 for(i = 0; i < 8; i++)
2149 qemu_get_be32s(f, &env->dr[i]);
2152 qemu_get_be32s(f, &env->a20_mask);
2154 /* XXX: compute hflags from scratch, except for CPL and IIF */
2155 env->hflags = hflags;
2160 #elif defined(TARGET_PPC)
2161 void cpu_save(QEMUFile *f, void *opaque)
2165 int cpu_load(QEMUFile *f, void *opaque, int version_id)
2171 #warning No CPU save/restore functions
2175 /***********************************************************/
2176 /* ram save/restore */
2178 /* we just avoid storing empty pages */
2179 static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
2184 for(i = 1; i < len; i++) {
2188 qemu_put_byte(f, 1);
2189 qemu_put_byte(f, v);
2192 qemu_put_byte(f, 0);
2193 qemu_put_buffer(f, buf, len);
2196 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
2200 v = qemu_get_byte(f);
2203 if (qemu_get_buffer(f, buf, len) != len)
2207 v = qemu_get_byte(f);
2208 memset(buf, v, len);
2216 static void ram_save(QEMUFile *f, void *opaque)
2219 qemu_put_be32(f, phys_ram_size);
2220 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
2221 ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
2225 static int ram_load(QEMUFile *f, void *opaque, int version_id)
2229 if (version_id != 1)
2231 if (qemu_get_be32(f) != phys_ram_size)
2233 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
2234 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
2241 /***********************************************************/
2242 /* main execution loop */
2244 void gui_update(void *opaque)
2246 display_state.dpy_refresh(&display_state);
2247 qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
2250 /* XXX: support several handlers */
2251 VMStopHandler *vm_stop_cb;
2252 VMStopHandler *vm_stop_opaque;
2254 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
2257 vm_stop_opaque = opaque;
2261 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
2274 void vm_stop(int reason)
2277 cpu_disable_ticks();
2281 vm_stop_cb(vm_stop_opaque, reason);
2287 /* reset/shutdown handler */
2289 typedef struct QEMUResetEntry {
2290 QEMUResetHandler *func;
2292 struct QEMUResetEntry *next;
2295 static QEMUResetEntry *first_reset_entry;
2296 static int reset_requested;
2297 static int shutdown_requested;
2299 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
2301 QEMUResetEntry **pre, *re;
2303 pre = &first_reset_entry;
2304 while (*pre != NULL)
2305 pre = &(*pre)->next;
2306 re = qemu_mallocz(sizeof(QEMUResetEntry));
2308 re->opaque = opaque;
2313 void qemu_system_reset(void)
2317 /* reset all devices */
2318 for(re = first_reset_entry; re != NULL; re = re->next) {
2319 re->func(re->opaque);
2323 void qemu_system_reset_request(void)
2325 reset_requested = 1;
2326 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
2329 void qemu_system_shutdown_request(void)
2331 shutdown_requested = 1;
2332 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
2335 static void main_cpu_reset(void *opaque)
2338 CPUState *env = opaque;
2343 void main_loop_wait(int timeout)
2346 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
2347 IOHandlerRecord *ioh, *ioh_next;
2357 /* poll any events */
2358 /* XXX: separate device handlers from system ones */
2360 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2361 if (!ioh->fd_can_read) {
2364 pf->events = POLLIN;
2368 max_size = ioh->fd_can_read(ioh->opaque);
2370 if (max_size > sizeof(buf))
2371 max_size = sizeof(buf);
2373 pf->events = POLLIN;
2380 ioh->max_size = max_size;
2383 ret = poll(ufds, pf - ufds, timeout);
2385 /* XXX: better handling of removal */
2386 for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
2387 ioh_next = ioh->next;
2390 if (pf->revents & POLLIN) {
2391 if (ioh->max_size == 0) {
2392 /* just a read event */
2393 ioh->fd_read(ioh->opaque, NULL, 0);
2395 n = read(ioh->fd, buf, ioh->max_size);
2397 ioh->fd_read(ioh->opaque, buf, n);
2398 } else if (errno != EAGAIN) {
2399 ioh->fd_read(ioh->opaque, NULL, -errno);
2406 #endif /* !defined(_WIN32) */
2407 #if defined(CONFIG_SLIRP)
2408 /* XXX: merge with poll() */
2410 fd_set rfds, wfds, xfds;
2418 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2421 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
2423 slirp_select_poll(&rfds, &wfds, &xfds);
2429 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
2430 qemu_get_clock(vm_clock));
2432 if (audio_enabled) {
2433 /* XXX: add explicit timer */
2437 /* run dma transfers, if any */
2441 /* real time timers */
2442 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
2443 qemu_get_clock(rt_clock));
2449 CPUState *env = global_env;
2453 ret = cpu_exec(env);
2454 if (shutdown_requested) {
2455 ret = EXCP_INTERRUPT;
2458 if (reset_requested) {
2459 reset_requested = 0;
2460 qemu_system_reset();
2461 ret = EXCP_INTERRUPT;
2463 if (ret == EXCP_DEBUG) {
2464 vm_stop(EXCP_DEBUG);
2466 /* if hlt instruction, we wait until the next IRQ */
2467 /* XXX: use timeout computed from timers */
2468 if (ret == EXCP_HLT)
2475 main_loop_wait(timeout);
2477 cpu_disable_ticks();
2483 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n"
2484 "usage: %s [options] [disk_image]\n"
2486 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2488 "Standard options:\n"
2489 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
2490 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
2491 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
2492 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
2493 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
2494 "-snapshot write to temporary files instead of disk image files\n"
2495 "-m megs set virtual RAM size to megs MB [default=%d]\n"
2496 "-nographic disable graphical output and redirect serial I/Os to console\n"
2497 "-enable-audio enable audio support\n"
2498 "-localtime set the real time clock to local time [default=utc]\n"
2500 "-prep Simulate a PREP system (default is PowerMAC)\n"
2501 "-g WxH[xDEPTH] Set the initial VGA graphic mode\n"
2504 "Network options:\n"
2505 "-nics n simulate 'n' network cards [default=1]\n"
2506 "-macaddr addr set the mac address of the first interface\n"
2507 "-n script set tap/tun network init script [default=%s]\n"
2508 "-tun-fd fd use this fd as already opened tap/tun interface\n"
2510 "-user-net use user mode network stack [default if no tap/tun script]\n"
2511 "-tftp prefix allow tftp access to files starting with prefix [-user-net]\n"
2513 "-smb dir allow SMB access to files in 'dir' [-user-net]\n"
2515 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
2516 " redirect TCP or UDP connections from host to guest [-user-net]\n"
2518 "-dummy-net use dummy network stack\n"
2520 "Linux boot specific:\n"
2521 "-kernel bzImage use 'bzImage' as kernel image\n"
2522 "-append cmdline use 'cmdline' as kernel command line\n"
2523 "-initrd file use 'file' as initial ram disk\n"
2525 "Debug/Expert options:\n"
2526 "-monitor dev redirect the monitor to char device 'dev'\n"
2527 "-serial dev redirect the serial port to char device 'dev'\n"
2528 "-S freeze CPU at startup (use 'c' to start execution)\n"
2529 "-s wait gdb connection to port %d\n"
2530 "-p port change gdb connection port\n"
2531 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
2532 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
2533 "-L path set the directory for the BIOS and VGA BIOS\n"
2534 #ifdef USE_CODE_COPY
2535 "-no-code-copy disable code copy acceleration\n"
2538 "-isa simulate an ISA-only system (default is PCI system)\n"
2539 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
2540 " (default is CL-GD5446 PCI VGA)\n"
2543 "During emulation, the following keys are useful:\n"
2544 "ctrl-shift-f toggle full screen\n"
2545 "ctrl-shift-Fn switch to virtual console 'n'\n"
2546 "ctrl-shift toggle mouse and keyboard grab\n"
2548 "When using -nographic, press 'ctrl-a h' to get some help.\n"
2550 #ifdef CONFIG_SOFTMMU
2556 DEFAULT_NETWORK_SCRIPT,
2557 DEFAULT_GDBSTUB_PORT,
2559 #ifndef CONFIG_SOFTMMU
2561 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
2562 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
2568 #define HAS_ARG 0x0001
2581 QEMU_OPTION_snapshot,
2583 QEMU_OPTION_nographic,
2584 QEMU_OPTION_enable_audio,
2587 QEMU_OPTION_macaddr,
2590 QEMU_OPTION_user_net,
2594 QEMU_OPTION_dummy_net,
2606 QEMU_OPTION_no_code_copy,
2610 QEMU_OPTION_localtime,
2611 QEMU_OPTION_cirrusvga,
2613 QEMU_OPTION_std_vga,
2614 QEMU_OPTION_monitor,
2618 typedef struct QEMUOption {
2624 const QEMUOption qemu_options[] = {
2625 { "h", 0, QEMU_OPTION_h },
2627 { "fda", HAS_ARG, QEMU_OPTION_fda },
2628 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
2629 { "hda", HAS_ARG, QEMU_OPTION_hda },
2630 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
2631 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
2632 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
2633 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
2634 { "boot", HAS_ARG, QEMU_OPTION_boot },
2635 { "snapshot", 0, QEMU_OPTION_snapshot },
2636 { "m", HAS_ARG, QEMU_OPTION_m },
2637 { "nographic", 0, QEMU_OPTION_nographic },
2638 { "enable-audio", 0, QEMU_OPTION_enable_audio },
2640 { "nics", HAS_ARG, QEMU_OPTION_nics},
2641 { "macaddr", HAS_ARG, QEMU_OPTION_macaddr},
2642 { "n", HAS_ARG, QEMU_OPTION_n },
2643 { "tun-fd", HAS_ARG, QEMU_OPTION_tun_fd },
2645 { "user-net", 0, QEMU_OPTION_user_net },
2646 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
2648 { "smb", HAS_ARG, QEMU_OPTION_smb },
2650 { "redir", HAS_ARG, QEMU_OPTION_redir },
2652 { "dummy-net", 0, QEMU_OPTION_dummy_net },
2654 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
2655 { "append", HAS_ARG, QEMU_OPTION_append },
2656 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
2658 { "S", 0, QEMU_OPTION_S },
2659 { "s", 0, QEMU_OPTION_s },
2660 { "p", HAS_ARG, QEMU_OPTION_p },
2661 { "d", HAS_ARG, QEMU_OPTION_d },
2662 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
2663 { "L", HAS_ARG, QEMU_OPTION_L },
2664 { "no-code-copy", 0, QEMU_OPTION_no_code_copy },
2666 { "prep", 0, QEMU_OPTION_prep },
2667 { "g", 1, QEMU_OPTION_g },
2669 { "localtime", 0, QEMU_OPTION_localtime },
2670 { "isa", 0, QEMU_OPTION_isa },
2671 { "std-vga", 0, QEMU_OPTION_std_vga },
2672 { "monitor", 1, QEMU_OPTION_monitor },
2673 { "serial", 1, QEMU_OPTION_serial },
2675 /* temporary options */
2676 { "pci", 0, QEMU_OPTION_pci },
2677 { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
2681 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2683 /* this stack is only used during signal handling */
2684 #define SIGNAL_STACK_SIZE 32768
2686 static uint8_t *signal_stack;
2690 /* password input */
2692 static BlockDriverState *get_bdrv(int index)
2694 BlockDriverState *bs;
2697 bs = bs_table[index];
2698 } else if (index < 6) {
2699 bs = fd_table[index - 4];
2706 static void read_passwords(void)
2708 BlockDriverState *bs;
2712 for(i = 0; i < 6; i++) {
2714 if (bs && bdrv_is_encrypted(bs)) {
2715 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs));
2716 for(j = 0; j < 3; j++) {
2717 monitor_readline("Password: ",
2718 1, password, sizeof(password));
2719 if (bdrv_set_key(bs, password) == 0)
2721 term_printf("invalid password\n");
2727 #define NET_IF_TUN 0
2728 #define NET_IF_USER 1
2729 #define NET_IF_DUMMY 2
2731 int main(int argc, char **argv)
2733 #ifdef CONFIG_GDBSTUB
2734 int use_gdbstub, gdbstub_port;
2737 int snapshot, linux_boot;
2739 const char *initrd_filename;
2740 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
2741 const char *kernel_filename, *kernel_cmdline;
2742 DisplayState *ds = &display_state;
2743 int cyls, heads, secs;
2744 int start_emulation = 1;
2746 int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
2748 const char *r, *optarg;
2749 CharDriverState *monitor_hd;
2750 char monitor_device[128];
2751 char serial_devices[MAX_SERIAL_PORTS][128];
2752 int serial_device_index;
2754 #if !defined(CONFIG_SOFTMMU)
2755 /* we never want that malloc() uses mmap() */
2756 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
2758 initrd_filename = NULL;
2759 for(i = 0; i < MAX_FD; i++)
2760 fd_filename[i] = NULL;
2761 for(i = 0; i < MAX_DISKS; i++)
2762 hd_filename[i] = NULL;
2763 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2764 vga_ram_size = VGA_RAM_SIZE;
2765 bios_size = BIOS_SIZE;
2766 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
2767 #ifdef CONFIG_GDBSTUB
2769 gdbstub_port = DEFAULT_GDBSTUB_PORT;
2773 kernel_filename = NULL;
2774 kernel_cmdline = "";
2776 cyls = heads = secs = 0;
2777 pstrcpy(monitor_device, sizeof(monitor_device), "vc");
2779 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc");
2780 for(i = 1; i < MAX_SERIAL_PORTS; i++)
2781 serial_devices[i][0] = '\0';
2782 serial_device_index = 0;
2787 /* default mac address of the first network interface */
2801 hd_filename[0] = argv[optind++];
2803 const QEMUOption *popt;
2806 popt = qemu_options;
2809 fprintf(stderr, "%s: invalid option -- '%s'\n",
2813 if (!strcmp(popt->name, r + 1))
2817 if (popt->flags & HAS_ARG) {
2818 if (optind >= argc) {
2819 fprintf(stderr, "%s: option '%s' requires an argument\n",
2823 optarg = argv[optind++];
2828 switch(popt->index) {
2829 case QEMU_OPTION_initrd:
2830 initrd_filename = optarg;
2832 case QEMU_OPTION_hda:
2833 hd_filename[0] = optarg;
2835 case QEMU_OPTION_hdb:
2836 hd_filename[1] = optarg;
2838 case QEMU_OPTION_snapshot:
2841 case QEMU_OPTION_hdachs:
2845 cyls = strtol(p, (char **)&p, 0);
2849 heads = strtol(p, (char **)&p, 0);
2853 secs = strtol(p, (char **)&p, 0);
2860 case QEMU_OPTION_nographic:
2861 pstrcpy(monitor_device, sizeof(monitor_device), "stdio");
2862 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio");
2865 case QEMU_OPTION_kernel:
2866 kernel_filename = optarg;
2868 case QEMU_OPTION_append:
2869 kernel_cmdline = optarg;
2871 case QEMU_OPTION_tun_fd:
2875 net_if_type = NET_IF_TUN;
2876 if (nb_tun_fds < MAX_NICS) {
2877 fd = strtol(optarg, (char **)&p, 0);
2879 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
2882 tun_fds[nb_tun_fds++] = fd;
2886 case QEMU_OPTION_hdc:
2887 hd_filename[2] = optarg;
2890 case QEMU_OPTION_hdd:
2891 hd_filename[3] = optarg;
2893 case QEMU_OPTION_cdrom:
2894 hd_filename[2] = optarg;
2897 case QEMU_OPTION_boot:
2898 boot_device = optarg[0];
2899 if (boot_device != 'a' && boot_device != 'b' &&
2900 boot_device != 'c' && boot_device != 'd') {
2901 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
2905 case QEMU_OPTION_fda:
2906 fd_filename[0] = optarg;
2908 case QEMU_OPTION_fdb:
2909 fd_filename[1] = optarg;
2911 case QEMU_OPTION_no_code_copy:
2912 code_copy_enabled = 0;
2914 case QEMU_OPTION_nics:
2915 nb_nics = atoi(optarg);
2916 if (nb_nics < 0 || nb_nics > MAX_NICS) {
2917 fprintf(stderr, "qemu: invalid number of network interfaces\n");
2921 case QEMU_OPTION_macaddr:
2926 for(i = 0; i < 6; i++) {
2927 macaddr[i] = strtol(p, (char **)&p, 16);
2934 fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
2943 case QEMU_OPTION_tftp:
2944 tftp_prefix = optarg;
2947 case QEMU_OPTION_smb:
2948 net_slirp_smb(optarg);
2951 case QEMU_OPTION_user_net:
2952 net_if_type = NET_IF_USER;
2954 case QEMU_OPTION_redir:
2955 net_slirp_redir(optarg);
2958 case QEMU_OPTION_dummy_net:
2959 net_if_type = NET_IF_DUMMY;
2961 case QEMU_OPTION_enable_audio:
2968 ram_size = atoi(optarg) * 1024 * 1024;
2971 if (ram_size > PHYS_RAM_MAX_SIZE) {
2972 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
2973 PHYS_RAM_MAX_SIZE / (1024 * 1024));
2982 mask = cpu_str_to_log_mask(optarg);
2984 printf("Log items (comma separated):\n");
2985 for(item = cpu_log_items; item->mask != 0; item++) {
2986 printf("%-10s %s\n", item->name, item->help);
2994 pstrcpy(network_script, sizeof(network_script), optarg);
2996 #ifdef CONFIG_GDBSTUB
3001 gdbstub_port = atoi(optarg);
3008 start_emulation = 0;
3010 case QEMU_OPTION_pci:
3013 case QEMU_OPTION_isa:
3016 case QEMU_OPTION_prep:
3019 case QEMU_OPTION_localtime:
3022 case QEMU_OPTION_cirrusvga:
3023 cirrus_vga_enabled = 1;
3025 case QEMU_OPTION_std_vga:
3026 cirrus_vga_enabled = 0;
3033 w = strtol(p, (char **)&p, 10);
3036 fprintf(stderr, "qemu: invalid resolution or depth\n");
3042 h = strtol(p, (char **)&p, 10);
3047 depth = strtol(p, (char **)&p, 10);
3048 if (depth != 8 && depth != 15 && depth != 16 &&
3049 depth != 24 && depth != 32)
3051 } else if (*p == '\0') {
3052 depth = graphic_depth;
3059 graphic_depth = depth;
3062 case QEMU_OPTION_monitor:
3063 pstrcpy(monitor_device, sizeof(monitor_device), optarg);
3065 case QEMU_OPTION_serial:
3066 if (serial_device_index >= MAX_SERIAL_PORTS) {
3067 fprintf(stderr, "qemu: too many serial ports\n");
3070 pstrcpy(serial_devices[serial_device_index],
3071 sizeof(serial_devices[0]), optarg);
3072 serial_device_index++;
3078 linux_boot = (kernel_filename != NULL);
3080 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
3081 fd_filename[0] == '\0')
3084 /* boot to cd by default if no hard disk */
3085 if (hd_filename[0] == '\0' && boot_device == 'c') {
3086 if (fd_filename[0] != '\0')
3092 #if !defined(CONFIG_SOFTMMU)
3093 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
3095 static uint8_t stdout_buf[4096];
3096 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
3099 setvbuf(stdout, NULL, _IOLBF, 0);
3102 /* init host network redirectors */
3103 if (net_if_type == -1) {
3104 net_if_type = NET_IF_TUN;
3105 #if defined(CONFIG_SLIRP)
3106 if (access(network_script, R_OK) < 0) {
3107 net_if_type = NET_IF_USER;
3112 for(i = 0; i < nb_nics; i++) {
3113 NetDriverState *nd = &nd_table[i];
3115 /* init virtual mac address */
3116 nd->macaddr[0] = macaddr[0];
3117 nd->macaddr[1] = macaddr[1];
3118 nd->macaddr[2] = macaddr[2];
3119 nd->macaddr[3] = macaddr[3];
3120 nd->macaddr[4] = macaddr[4];
3121 nd->macaddr[5] = macaddr[5] + i;
3122 switch(net_if_type) {
3123 #if defined(CONFIG_SLIRP)
3128 #if !defined(_WIN32)
3130 if (i < nb_tun_fds) {
3131 net_fd_init(nd, tun_fds[i]);
3133 if (net_tun_init(nd) < 0)
3145 /* init the memory */
3146 phys_ram_size = ram_size + vga_ram_size + bios_size;
3148 #ifdef CONFIG_SOFTMMU
3150 /* mallocs are always aligned on BSD. valloc is better for correctness */
3151 phys_ram_base = valloc(phys_ram_size);
3153 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
3155 if (!phys_ram_base) {
3156 fprintf(stderr, "Could not allocate physical memory\n");
3160 /* as we must map the same page at several addresses, we must use
3165 tmpdir = getenv("QEMU_TMPDIR");
3168 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
3169 if (mkstemp(phys_ram_file) < 0) {
3170 fprintf(stderr, "Could not create temporary memory file '%s'\n",
3174 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
3175 if (phys_ram_fd < 0) {
3176 fprintf(stderr, "Could not open temporary memory file '%s'\n",
3180 ftruncate(phys_ram_fd, phys_ram_size);
3181 unlink(phys_ram_file);
3182 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
3184 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
3186 if (phys_ram_base == MAP_FAILED) {
3187 fprintf(stderr, "Could not map physical memory\n");
3193 /* we always create the cdrom drive, even if no disk is there */
3196 bs_table[2] = bdrv_new("cdrom");
3197 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
3200 /* open the virtual block devices */
3201 for(i = 0; i < MAX_DISKS; i++) {
3202 if (hd_filename[i]) {
3205 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
3206 bs_table[i] = bdrv_new(buf);
3208 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
3209 fprintf(stderr, "qemu: could not open hard disk image '%s'\n",
3213 if (i == 0 && cyls != 0)
3214 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
3218 /* we always create at least one floppy disk */
3219 fd_table[0] = bdrv_new("fda");
3220 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
3222 for(i = 0; i < MAX_FD; i++) {
3223 if (fd_filename[i]) {
3226 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
3227 fd_table[i] = bdrv_new(buf);
3228 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
3230 if (fd_filename[i] != '\0') {
3231 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
3232 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
3240 /* init CPU state */
3243 cpu_single_env = env;
3245 register_savevm("timer", 0, 1, timer_save, timer_load, env);
3246 register_savevm("cpu", 0, 2, cpu_save, cpu_load, env);
3247 register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
3248 qemu_register_reset(main_cpu_reset, global_env);
3251 cpu_calibrate_ticks();
3255 dumb_display_init(ds);
3258 sdl_display_init(ds);
3260 dumb_display_init(ds);
3264 vga_console = graphic_console_init(ds);
3266 monitor_hd = qemu_chr_open(monitor_device);
3268 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
3271 monitor_init(monitor_hd, !nographic);
3273 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
3274 if (serial_devices[i][0] != '\0') {
3275 serial_hds[i] = qemu_chr_open(serial_devices[i]);
3276 if (!serial_hds[i]) {
3277 fprintf(stderr, "qemu: could not open serial device '%s'\n",
3281 if (!strcmp(serial_devices[i], "vc"))
3282 qemu_chr_printf(serial_hds[i], "serial%d console\n", i);
3286 /* setup cpu signal handlers for MMU / self modifying code handling */
3287 #if !defined(CONFIG_SOFTMMU)
3289 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3292 signal_stack = memalign(16, SIGNAL_STACK_SIZE);
3293 stk.ss_sp = signal_stack;
3294 stk.ss_size = SIGNAL_STACK_SIZE;
3297 if (sigaltstack(&stk, NULL) < 0) {
3298 perror("sigaltstack");
3304 struct sigaction act;
3306 sigfillset(&act.sa_mask);
3307 act.sa_flags = SA_SIGINFO;
3308 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3309 act.sa_flags |= SA_ONSTACK;
3311 act.sa_sigaction = host_segv_handler;
3312 sigaction(SIGSEGV, &act, NULL);
3313 sigaction(SIGBUS, &act, NULL);
3314 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3315 sigaction(SIGFPE, &act, NULL);
3322 struct sigaction act;
3323 sigfillset(&act.sa_mask);
3325 act.sa_handler = SIG_IGN;
3326 sigaction(SIGPIPE, &act, NULL);
3331 #if defined(TARGET_I386)
3332 pc_init(ram_size, vga_ram_size, boot_device,
3333 ds, fd_filename, snapshot,
3334 kernel_filename, kernel_cmdline, initrd_filename);
3335 #elif defined(TARGET_PPC)
3336 ppc_init(ram_size, vga_ram_size, boot_device,
3337 ds, fd_filename, snapshot,
3338 kernel_filename, kernel_cmdline, initrd_filename);
3341 gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
3342 qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
3344 #ifdef CONFIG_GDBSTUB
3346 if (gdbserver_start(gdbstub_port) < 0) {
3347 fprintf(stderr, "Could not open gdbserver socket on port %d\n",
3351 printf("Waiting gdb connection on port %d\n", gdbstub_port);
3356 /* XXX: simplify init */
3358 if (start_emulation) {