4 * Copyright (c) 2003-2005 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 */
77 #define main qemu_main
78 #endif /* CONFIG_COCOA */
86 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
88 //#define DEBUG_UNUSED_IOPORT
89 //#define DEBUG_IOPORT
91 #if !defined(CONFIG_SOFTMMU)
92 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
94 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
98 #define DEFAULT_RAM_SIZE 144
100 #define DEFAULT_RAM_SIZE 128
103 #define GUI_REFRESH_INTERVAL 30
105 /* XXX: use a two level table to limit memory usage */
106 #define MAX_IOPORTS 65536
108 const char *bios_dir = CONFIG_QEMU_SHAREDIR;
109 char phys_ram_file[1024];
110 CPUState *global_env;
111 CPUState *cpu_single_env;
112 void *ioport_opaque[MAX_IOPORTS];
113 IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
114 IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
115 BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
118 static DisplayState display_state;
120 const char* keyboard_layout = NULL;
121 int64_t ticks_per_sec;
122 int boot_device = 'c';
124 static char network_script[1024];
125 int pit_min_timer_count = 0;
127 NetDriverState nd_table[MAX_NICS];
128 QEMUTimer *gui_timer;
131 int audio_enabled = 0;
132 int sb16_enabled = 0;
133 int adlib_enabled = 0;
135 int es1370_enabled = 0;
138 int cirrus_vga_enabled = 1;
140 int graphic_width = 1024;
141 int graphic_height = 768;
143 int graphic_width = 800;
144 int graphic_height = 600;
146 int graphic_depth = 15;
148 TextConsole *vga_console;
149 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
150 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
152 int win2k_install_hack = 0;
155 USBPort *vm_usb_ports[MAX_VM_USB_PORTS];
156 USBDevice *vm_usb_hub;
158 /***********************************************************/
159 /* x86 ISA bus support */
161 target_phys_addr_t isa_mem_base = 0;
164 uint32_t default_ioport_readb(void *opaque, uint32_t address)
166 #ifdef DEBUG_UNUSED_IOPORT
167 fprintf(stderr, "inb: port=0x%04x\n", address);
172 void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
174 #ifdef DEBUG_UNUSED_IOPORT
175 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
179 /* default is to make two byte accesses */
180 uint32_t default_ioport_readw(void *opaque, uint32_t address)
183 data = ioport_read_table[0][address](ioport_opaque[address], address);
184 address = (address + 1) & (MAX_IOPORTS - 1);
185 data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;
189 void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
191 ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);
192 address = (address + 1) & (MAX_IOPORTS - 1);
193 ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);
196 uint32_t default_ioport_readl(void *opaque, uint32_t address)
198 #ifdef DEBUG_UNUSED_IOPORT
199 fprintf(stderr, "inl: port=0x%04x\n", address);
204 void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
206 #ifdef DEBUG_UNUSED_IOPORT
207 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
211 void init_ioports(void)
215 for(i = 0; i < MAX_IOPORTS; i++) {
216 ioport_read_table[0][i] = default_ioport_readb;
217 ioport_write_table[0][i] = default_ioport_writeb;
218 ioport_read_table[1][i] = default_ioport_readw;
219 ioport_write_table[1][i] = default_ioport_writew;
220 ioport_read_table[2][i] = default_ioport_readl;
221 ioport_write_table[2][i] = default_ioport_writel;
225 /* size is the word size in byte */
226 int register_ioport_read(int start, int length, int size,
227 IOPortReadFunc *func, void *opaque)
233 } else if (size == 2) {
235 } else if (size == 4) {
238 hw_error("register_ioport_read: invalid size");
241 for(i = start; i < start + length; i += size) {
242 ioport_read_table[bsize][i] = func;
243 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
244 hw_error("register_ioport_read: invalid opaque");
245 ioport_opaque[i] = opaque;
250 /* size is the word size in byte */
251 int register_ioport_write(int start, int length, int size,
252 IOPortWriteFunc *func, void *opaque)
258 } else if (size == 2) {
260 } else if (size == 4) {
263 hw_error("register_ioport_write: invalid size");
266 for(i = start; i < start + length; i += size) {
267 ioport_write_table[bsize][i] = func;
268 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
269 hw_error("register_ioport_read: invalid opaque");
270 ioport_opaque[i] = opaque;
275 void isa_unassign_ioport(int start, int length)
279 for(i = start; i < start + length; i++) {
280 ioport_read_table[0][i] = default_ioport_readb;
281 ioport_read_table[1][i] = default_ioport_readw;
282 ioport_read_table[2][i] = default_ioport_readl;
284 ioport_write_table[0][i] = default_ioport_writeb;
285 ioport_write_table[1][i] = default_ioport_writew;
286 ioport_write_table[2][i] = default_ioport_writel;
290 /***********************************************************/
292 void pstrcpy(char *buf, int buf_size, const char *str)
302 if (c == 0 || q >= buf + buf_size - 1)
309 /* strcat and truncate. */
310 char *pstrcat(char *buf, int buf_size, const char *s)
315 pstrcpy(buf + len, buf_size - len, s);
319 int strstart(const char *str, const char *val, const char **ptr)
335 /* return the size or -1 if error */
336 int get_image_size(const char *filename)
339 fd = open(filename, O_RDONLY | O_BINARY);
342 size = lseek(fd, 0, SEEK_END);
347 /* return the size or -1 if error */
348 int load_image(const char *filename, uint8_t *addr)
351 fd = open(filename, O_RDONLY | O_BINARY);
354 size = lseek(fd, 0, SEEK_END);
355 lseek(fd, 0, SEEK_SET);
356 if (read(fd, addr, size) != size) {
364 void cpu_outb(CPUState *env, int addr, int val)
367 if (loglevel & CPU_LOG_IOPORT)
368 fprintf(logfile, "outb: %04x %02x\n", addr, val);
370 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
373 void cpu_outw(CPUState *env, int addr, int val)
376 if (loglevel & CPU_LOG_IOPORT)
377 fprintf(logfile, "outw: %04x %04x\n", addr, val);
379 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
382 void cpu_outl(CPUState *env, int addr, int val)
385 if (loglevel & CPU_LOG_IOPORT)
386 fprintf(logfile, "outl: %04x %08x\n", addr, val);
388 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
391 int cpu_inb(CPUState *env, int addr)
394 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
396 if (loglevel & CPU_LOG_IOPORT)
397 fprintf(logfile, "inb : %04x %02x\n", addr, val);
402 int cpu_inw(CPUState *env, int addr)
405 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
407 if (loglevel & CPU_LOG_IOPORT)
408 fprintf(logfile, "inw : %04x %04x\n", addr, val);
413 int cpu_inl(CPUState *env, int addr)
416 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
418 if (loglevel & CPU_LOG_IOPORT)
419 fprintf(logfile, "inl : %04x %08x\n", addr, val);
424 /***********************************************************/
425 void hw_error(const char *fmt, ...)
430 fprintf(stderr, "qemu: hardware error: ");
431 vfprintf(stderr, fmt, ap);
432 fprintf(stderr, "\n");
434 cpu_dump_state(global_env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
436 cpu_dump_state(global_env, stderr, fprintf, 0);
442 /***********************************************************/
445 static QEMUPutKBDEvent *qemu_put_kbd_event;
446 static void *qemu_put_kbd_event_opaque;
447 static QEMUPutMouseEvent *qemu_put_mouse_event;
448 static void *qemu_put_mouse_event_opaque;
450 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
452 qemu_put_kbd_event_opaque = opaque;
453 qemu_put_kbd_event = func;
456 void qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, void *opaque)
458 qemu_put_mouse_event_opaque = opaque;
459 qemu_put_mouse_event = func;
462 void kbd_put_keycode(int keycode)
464 if (qemu_put_kbd_event) {
465 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
469 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
471 if (qemu_put_mouse_event) {
472 qemu_put_mouse_event(qemu_put_mouse_event_opaque,
473 dx, dy, dz, buttons_state);
477 /***********************************************************/
480 #if defined(__powerpc__)
482 static inline uint32_t get_tbl(void)
485 asm volatile("mftb %0" : "=r" (tbl));
489 static inline uint32_t get_tbu(void)
492 asm volatile("mftbu %0" : "=r" (tbl));
496 int64_t cpu_get_real_ticks(void)
499 /* NOTE: we test if wrapping has occurred */
505 return ((int64_t)h << 32) | l;
508 #elif defined(__i386__)
510 int64_t cpu_get_real_ticks(void)
513 asm volatile ("rdtsc" : "=A" (val));
517 #elif defined(__x86_64__)
519 int64_t cpu_get_real_ticks(void)
523 asm volatile("rdtsc" : "=a" (low), "=d" (high));
530 #elif defined(__ia64)
532 int64_t cpu_get_real_ticks(void)
535 asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory");
539 #elif defined(__s390__)
541 int64_t cpu_get_real_ticks(void)
544 asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
549 #error unsupported CPU
552 static int64_t cpu_ticks_offset;
553 static int cpu_ticks_enabled;
555 static inline int64_t cpu_get_ticks(void)
557 if (!cpu_ticks_enabled) {
558 return cpu_ticks_offset;
560 return cpu_get_real_ticks() + cpu_ticks_offset;
564 /* enable cpu_get_ticks() */
565 void cpu_enable_ticks(void)
567 if (!cpu_ticks_enabled) {
568 cpu_ticks_offset -= cpu_get_real_ticks();
569 cpu_ticks_enabled = 1;
573 /* disable cpu_get_ticks() : the clock is stopped. You must not call
574 cpu_get_ticks() after that. */
575 void cpu_disable_ticks(void)
577 if (cpu_ticks_enabled) {
578 cpu_ticks_offset = cpu_get_ticks();
579 cpu_ticks_enabled = 0;
583 static int64_t get_clock(void)
588 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
591 gettimeofday(&tv, NULL);
592 return tv.tv_sec * 1000000LL + tv.tv_usec;
596 void cpu_calibrate_ticks(void)
601 ticks = cpu_get_real_ticks();
607 usec = get_clock() - usec;
608 ticks = cpu_get_real_ticks() - ticks;
609 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
612 /* compute with 96 bit intermediate result: (a*b)/c */
613 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
618 #ifdef WORDS_BIGENDIAN
628 rl = (uint64_t)u.l.low * (uint64_t)b;
629 rh = (uint64_t)u.l.high * (uint64_t)b;
632 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
636 #define QEMU_TIMER_REALTIME 0
637 #define QEMU_TIMER_VIRTUAL 1
641 /* XXX: add frequency */
649 struct QEMUTimer *next;
655 static QEMUTimer *active_timers[2];
657 static MMRESULT timerID;
659 /* frequency of the times() clock tick */
660 static int timer_freq;
663 QEMUClock *qemu_new_clock(int type)
666 clock = qemu_mallocz(sizeof(QEMUClock));
673 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
677 ts = qemu_mallocz(sizeof(QEMUTimer));
684 void qemu_free_timer(QEMUTimer *ts)
689 /* stop a timer, but do not dealloc it */
690 void qemu_del_timer(QEMUTimer *ts)
694 /* NOTE: this code must be signal safe because
695 qemu_timer_expired() can be called from a signal. */
696 pt = &active_timers[ts->clock->type];
709 /* modify the current timer so that it will be fired when current_time
710 >= expire_time. The corresponding callback will be called. */
711 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
717 /* add the timer in the sorted list */
718 /* NOTE: this code must be signal safe because
719 qemu_timer_expired() can be called from a signal. */
720 pt = &active_timers[ts->clock->type];
725 if (t->expire_time > expire_time)
729 ts->expire_time = expire_time;
734 int qemu_timer_pending(QEMUTimer *ts)
737 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
744 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
748 return (timer_head->expire_time <= current_time);
751 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
757 if (!ts || ts->expire_time > current_time)
759 /* remove timer from the list before calling the callback */
760 *ptimer_head = ts->next;
763 /* run the callback (the timer list can be modified) */
768 int64_t qemu_get_clock(QEMUClock *clock)
770 switch(clock->type) {
771 case QEMU_TIMER_REALTIME:
773 return GetTickCount();
778 /* Note that using gettimeofday() is not a good solution
779 for timers because its value change when the date is
781 if (timer_freq == 100) {
782 return times(&tp) * 10;
784 return ((int64_t)times(&tp) * 1000) / timer_freq;
789 case QEMU_TIMER_VIRTUAL:
790 return cpu_get_ticks();
795 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
797 uint64_t expire_time;
799 if (qemu_timer_pending(ts)) {
800 expire_time = ts->expire_time;
804 qemu_put_be64(f, expire_time);
807 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
809 uint64_t expire_time;
811 expire_time = qemu_get_be64(f);
812 if (expire_time != -1) {
813 qemu_mod_timer(ts, expire_time);
819 static void timer_save(QEMUFile *f, void *opaque)
821 if (cpu_ticks_enabled) {
822 hw_error("cannot save state if virtual timers are running");
824 qemu_put_be64s(f, &cpu_ticks_offset);
825 qemu_put_be64s(f, &ticks_per_sec);
828 static int timer_load(QEMUFile *f, void *opaque, int version_id)
832 if (cpu_ticks_enabled) {
835 qemu_get_be64s(f, &cpu_ticks_offset);
836 qemu_get_be64s(f, &ticks_per_sec);
841 void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
842 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
844 static void host_alarm_handler(int host_signum)
848 #define DISP_FREQ 1000
850 static int64_t delta_min = INT64_MAX;
851 static int64_t delta_max, delta_cum, last_clock, delta, ti;
853 ti = qemu_get_clock(vm_clock);
854 if (last_clock != 0) {
855 delta = ti - last_clock;
856 if (delta < delta_min)
858 if (delta > delta_max)
861 if (++count == DISP_FREQ) {
862 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
863 muldiv64(delta_min, 1000000, ticks_per_sec),
864 muldiv64(delta_max, 1000000, ticks_per_sec),
865 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
866 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
868 delta_min = INT64_MAX;
876 if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
877 qemu_get_clock(vm_clock)) ||
878 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
879 qemu_get_clock(rt_clock))) {
880 /* stop the cpu because a timer occured */
881 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
883 if (global_env->kqemu_enabled) {
884 kqemu_cpu_interrupt(global_env);
892 #if defined(__linux__)
894 #define RTC_FREQ 1024
898 static int start_rtc_timer(void)
900 rtc_fd = open("/dev/rtc", O_RDONLY);
903 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
904 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
905 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
906 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
909 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
914 pit_min_timer_count = PIT_FREQ / RTC_FREQ;
920 static int start_rtc_timer(void)
925 #endif /* !defined(__linux__) */
927 #endif /* !defined(_WIN32) */
929 static void init_timers(void)
931 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
932 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
937 timerID = timeSetEvent(1, // interval (ms)
939 host_alarm_handler, // function
940 (DWORD)&count, // user parameter
941 TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
943 perror("failed timer alarm");
947 pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
950 struct sigaction act;
951 struct itimerval itv;
953 /* get times() syscall frequency */
954 timer_freq = sysconf(_SC_CLK_TCK);
957 sigfillset(&act.sa_mask);
959 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
960 act.sa_flags |= SA_ONSTACK;
962 act.sa_handler = host_alarm_handler;
963 sigaction(SIGALRM, &act, NULL);
965 itv.it_interval.tv_sec = 0;
966 itv.it_interval.tv_usec = 999; /* for i386 kernel 2.6 to get 1 ms */
967 itv.it_value.tv_sec = 0;
968 itv.it_value.tv_usec = 10 * 1000;
969 setitimer(ITIMER_REAL, &itv, NULL);
970 /* we probe the tick duration of the kernel to inform the user if
971 the emulated kernel requested a too high timer frequency */
972 getitimer(ITIMER_REAL, &itv);
974 #if defined(__linux__)
975 if (itv.it_interval.tv_usec > 1000) {
976 /* try to use /dev/rtc to have a faster timer */
977 if (start_rtc_timer() < 0)
980 itv.it_interval.tv_sec = 0;
981 itv.it_interval.tv_usec = 0;
982 itv.it_value.tv_sec = 0;
983 itv.it_value.tv_usec = 0;
984 setitimer(ITIMER_REAL, &itv, NULL);
987 sigaction(SIGIO, &act, NULL);
988 fcntl(rtc_fd, F_SETFL, O_ASYNC);
989 fcntl(rtc_fd, F_SETOWN, getpid());
991 #endif /* defined(__linux__) */
994 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec *
1001 void quit_timers(void)
1004 timeKillEvent(timerID);
1008 /***********************************************************/
1009 /* character device */
1011 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
1013 return s->chr_write(s, buf, len);
1016 void qemu_chr_set_serial_parameters(CharDriverState *s,
1017 int speed, int parity,
1018 int data_bits, int stop_bits)
1020 if (s->chr_set_serial_parameters)
1021 s->chr_set_serial_parameters(s, speed, parity, data_bits, stop_bits);
1024 void qemu_chr_set_serial_break(CharDriverState *s, int enable)
1026 if (s->chr_set_serial_break)
1027 s->chr_set_serial_break(s, enable);
1031 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
1036 vsnprintf(buf, sizeof(buf), fmt, ap);
1037 qemu_chr_write(s, buf, strlen(buf));
1041 void qemu_chr_send_event(CharDriverState *s, int event)
1043 if (s->chr_send_event)
1044 s->chr_send_event(s, event);
1047 void qemu_chr_add_read_handler(CharDriverState *s,
1048 IOCanRWHandler *fd_can_read,
1049 IOReadHandler *fd_read, void *opaque)
1051 s->chr_add_read_handler(s, fd_can_read, fd_read, opaque);
1054 void qemu_chr_add_event_handler(CharDriverState *s, IOEventHandler *chr_event)
1056 s->chr_event = chr_event;
1059 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1064 static void null_chr_add_read_handler(CharDriverState *chr,
1065 IOCanRWHandler *fd_can_read,
1066 IOReadHandler *fd_read, void *opaque)
1070 CharDriverState *qemu_chr_open_null(void)
1072 CharDriverState *chr;
1074 chr = qemu_mallocz(sizeof(CharDriverState));
1077 chr->chr_write = null_chr_write;
1078 chr->chr_add_read_handler = null_chr_add_read_handler;
1086 /* for nographic stdio only */
1087 IOCanRWHandler *fd_can_read;
1088 IOReadHandler *fd_read;
1092 #define STDIO_MAX_CLIENTS 2
1094 static int stdio_nb_clients;
1095 static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS];
1097 static int unix_write(int fd, const uint8_t *buf, int len1)
1103 ret = write(fd, buf, len);
1105 if (errno != EINTR && errno != EAGAIN)
1107 } else if (ret == 0) {
1117 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1119 FDCharDriver *s = chr->opaque;
1120 return unix_write(s->fd_out, buf, len);
1123 static void fd_chr_add_read_handler(CharDriverState *chr,
1124 IOCanRWHandler *fd_can_read,
1125 IOReadHandler *fd_read, void *opaque)
1127 FDCharDriver *s = chr->opaque;
1129 if (s->fd_in >= 0) {
1130 if (nographic && s->fd_in == 0) {
1131 s->fd_can_read = fd_can_read;
1132 s->fd_read = fd_read;
1133 s->fd_opaque = opaque;
1135 qemu_add_fd_read_handler(s->fd_in, fd_can_read, fd_read, opaque);
1140 /* open a character device to a unix fd */
1141 CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
1143 CharDriverState *chr;
1146 chr = qemu_mallocz(sizeof(CharDriverState));
1149 s = qemu_mallocz(sizeof(FDCharDriver));
1157 chr->chr_write = fd_chr_write;
1158 chr->chr_add_read_handler = fd_chr_add_read_handler;
1162 CharDriverState *qemu_chr_open_file_out(const char *file_out)
1166 fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY);
1169 return qemu_chr_open_fd(-1, fd_out);
1172 CharDriverState *qemu_chr_open_pipe(const char *filename)
1176 fd = open(filename, O_RDWR | O_BINARY);
1179 return qemu_chr_open_fd(fd, fd);
1183 /* for STDIO, we handle the case where several clients use it
1186 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1188 #define TERM_FIFO_MAX_SIZE 1
1190 static int term_got_escape, client_index;
1191 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
1194 void term_print_help(void)
1197 "C-a h print this help\n"
1198 "C-a x exit emulator\n"
1199 "C-a s save disk data back to file (if -snapshot)\n"
1200 "C-a b send break (magic sysrq)\n"
1201 "C-a c switch between console and monitor\n"
1202 "C-a C-a send C-a\n"
1206 /* called when a char is received */
1207 static void stdio_received_byte(int ch)
1209 if (term_got_escape) {
1210 term_got_escape = 0;
1221 for (i = 0; i < MAX_DISKS; i++) {
1223 bdrv_commit(bs_table[i]);
1228 if (client_index < stdio_nb_clients) {
1229 CharDriverState *chr;
1232 chr = stdio_clients[client_index];
1234 chr->chr_event(s->fd_opaque, CHR_EVENT_BREAK);
1239 if (client_index >= stdio_nb_clients)
1241 if (client_index == 0) {
1242 /* send a new line in the monitor to get the prompt */
1250 } else if (ch == TERM_ESCAPE) {
1251 term_got_escape = 1;
1254 if (client_index < stdio_nb_clients) {
1256 CharDriverState *chr;
1259 chr = stdio_clients[client_index];
1261 if (s->fd_can_read(s->fd_opaque) > 0) {
1263 s->fd_read(s->fd_opaque, buf, 1);
1264 } else if (term_fifo_size == 0) {
1265 term_fifo[term_fifo_size++] = ch;
1271 static int stdio_can_read(void *opaque)
1273 CharDriverState *chr;
1276 if (client_index < stdio_nb_clients) {
1277 chr = stdio_clients[client_index];
1279 /* try to flush the queue if needed */
1280 if (term_fifo_size != 0 && s->fd_can_read(s->fd_opaque) > 0) {
1281 s->fd_read(s->fd_opaque, term_fifo, 1);
1284 /* see if we can absorb more chars */
1285 if (term_fifo_size == 0)
1294 static void stdio_read(void *opaque, const uint8_t *buf, int size)
1297 for(i = 0; i < size; i++)
1298 stdio_received_byte(buf[i]);
1301 /* init terminal so that we can grab keys */
1302 static struct termios oldtty;
1303 static int old_fd0_flags;
1305 static void term_exit(void)
1307 tcsetattr (0, TCSANOW, &oldtty);
1308 fcntl(0, F_SETFL, old_fd0_flags);
1311 static void term_init(void)
1315 tcgetattr (0, &tty);
1317 old_fd0_flags = fcntl(0, F_GETFL);
1319 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1320 |INLCR|IGNCR|ICRNL|IXON);
1321 tty.c_oflag |= OPOST;
1322 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1323 /* if graphical mode, we allow Ctrl-C handling */
1325 tty.c_lflag &= ~ISIG;
1326 tty.c_cflag &= ~(CSIZE|PARENB);
1329 tty.c_cc[VTIME] = 0;
1331 tcsetattr (0, TCSANOW, &tty);
1335 fcntl(0, F_SETFL, O_NONBLOCK);
1338 CharDriverState *qemu_chr_open_stdio(void)
1340 CharDriverState *chr;
1343 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
1345 chr = qemu_chr_open_fd(0, 1);
1346 if (stdio_nb_clients == 0)
1347 qemu_add_fd_read_handler(0, stdio_can_read, stdio_read, NULL);
1348 client_index = stdio_nb_clients;
1350 if (stdio_nb_clients != 0)
1352 chr = qemu_chr_open_fd(0, 1);
1354 stdio_clients[stdio_nb_clients++] = chr;
1355 if (stdio_nb_clients == 1) {
1356 /* set the terminal in raw mode */
1362 #if defined(__linux__)
1363 CharDriverState *qemu_chr_open_pty(void)
1365 char slave_name[1024];
1366 int master_fd, slave_fd;
1368 /* Not satisfying */
1369 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
1372 fprintf(stderr, "char device redirected to %s\n", slave_name);
1373 return qemu_chr_open_fd(master_fd, master_fd);
1376 static void tty_serial_init(int fd, int speed,
1377 int parity, int data_bits, int stop_bits)
1382 tcgetattr (0, &tty);
1424 cfsetispeed(&tty, spd);
1425 cfsetospeed(&tty, spd);
1427 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1428 |INLCR|IGNCR|ICRNL|IXON);
1429 tty.c_oflag |= OPOST;
1430 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1431 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS);
1452 tty.c_cflag |= PARENB;
1455 tty.c_cflag |= PARENB | PARODD;
1459 tcsetattr (fd, TCSANOW, &tty);
1462 static void tty_set_serial_parameters(CharDriverState *chr,
1463 int speed, int parity,
1464 int data_bits, int stop_bits)
1466 FDCharDriver *s = chr->opaque;
1467 tty_serial_init(s->fd_in, speed, parity, data_bits, stop_bits);
1470 static void tty_set_serial_break(CharDriverState *chr, int enable)
1472 FDCharDriver *s = chr->opaque;
1473 /* XXX: find a better solution */
1475 tcsendbreak(s->fd_in, 1);
1478 CharDriverState *qemu_chr_open_tty(const char *filename)
1480 CharDriverState *chr;
1483 fd = open(filename, O_RDWR);
1486 fcntl(fd, F_SETFL, O_NONBLOCK);
1487 tty_serial_init(fd, 115200, 'N', 8, 1);
1488 chr = qemu_chr_open_fd(fd, fd);
1491 chr->chr_set_serial_parameters = tty_set_serial_parameters;
1492 chr->chr_set_serial_break = tty_set_serial_break;
1497 CharDriverState *qemu_chr_open_pty(void)
1503 #endif /* !defined(_WIN32) */
1505 CharDriverState *qemu_chr_open(const char *filename)
1508 if (!strcmp(filename, "vc")) {
1509 return text_console_init(&display_state);
1510 } else if (!strcmp(filename, "null")) {
1511 return qemu_chr_open_null();
1512 } else if (strstart(filename, "file:", &p)) {
1513 return qemu_chr_open_file_out(p);
1514 } else if (strstart(filename, "pipe:", &p)) {
1515 return qemu_chr_open_pipe(p);
1518 if (!strcmp(filename, "pty")) {
1519 return qemu_chr_open_pty();
1520 } else if (!strcmp(filename, "stdio")) {
1521 return qemu_chr_open_stdio();
1524 #if defined(__linux__)
1525 if (strstart(filename, "/dev/", NULL)) {
1526 return qemu_chr_open_tty(filename);
1534 /***********************************************************/
1535 /* Linux network device redirectors */
1537 void hex_dump(FILE *f, const uint8_t *buf, int size)
1541 for(i=0;i<size;i+=16) {
1545 fprintf(f, "%08x ", i);
1548 fprintf(f, " %02x", buf[i+j]);
1553 for(j=0;j<len;j++) {
1555 if (c < ' ' || c > '~')
1557 fprintf(f, "%c", c);
1563 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1565 nd->send_packet(nd, buf, size);
1568 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
1569 IOReadHandler *fd_read, void *opaque)
1571 nd->add_read_packet(nd, fd_can_read, fd_read, opaque);
1574 /* dummy network adapter */
1576 static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1580 static void dummy_add_read_packet(NetDriverState *nd,
1581 IOCanRWHandler *fd_can_read,
1582 IOReadHandler *fd_read, void *opaque)
1586 static int net_dummy_init(NetDriverState *nd)
1588 nd->send_packet = dummy_send_packet;
1589 nd->add_read_packet = dummy_add_read_packet;
1590 pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy");
1594 #if defined(CONFIG_SLIRP)
1596 /* slirp network adapter */
1598 static void *slirp_fd_opaque;
1599 static IOCanRWHandler *slirp_fd_can_read;
1600 static IOReadHandler *slirp_fd_read;
1601 static int slirp_inited;
1603 int slirp_can_output(void)
1605 return slirp_fd_can_read(slirp_fd_opaque);
1608 void slirp_output(const uint8_t *pkt, int pkt_len)
1611 printf("output:\n");
1612 hex_dump(stdout, pkt, pkt_len);
1614 slirp_fd_read(slirp_fd_opaque, pkt, pkt_len);
1617 static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1621 hex_dump(stdout, buf, size);
1623 slirp_input(buf, size);
1626 static void slirp_add_read_packet(NetDriverState *nd,
1627 IOCanRWHandler *fd_can_read,
1628 IOReadHandler *fd_read, void *opaque)
1630 slirp_fd_opaque = opaque;
1631 slirp_fd_can_read = fd_can_read;
1632 slirp_fd_read = fd_read;
1635 static int net_slirp_init(NetDriverState *nd)
1637 if (!slirp_inited) {
1641 nd->send_packet = slirp_send_packet;
1642 nd->add_read_packet = slirp_add_read_packet;
1643 pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp");
1647 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
1652 p1 = strchr(p, sep);
1658 if (len > buf_size - 1)
1660 memcpy(buf, p, len);
1667 static void net_slirp_redir(const char *redir_str)
1672 struct in_addr guest_addr;
1673 int host_port, guest_port;
1675 if (!slirp_inited) {
1681 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1683 if (!strcmp(buf, "tcp")) {
1685 } else if (!strcmp(buf, "udp")) {
1691 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1693 host_port = strtol(buf, &r, 0);
1697 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1699 if (buf[0] == '\0') {
1700 pstrcpy(buf, sizeof(buf), "10.0.2.15");
1702 if (!inet_aton(buf, &guest_addr))
1705 guest_port = strtol(p, &r, 0);
1709 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
1710 fprintf(stderr, "qemu: could not set up redirection\n");
1715 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
1723 static void smb_exit(void)
1727 char filename[1024];
1729 /* erase all the files in the directory */
1730 d = opendir(smb_dir);
1735 if (strcmp(de->d_name, ".") != 0 &&
1736 strcmp(de->d_name, "..") != 0) {
1737 snprintf(filename, sizeof(filename), "%s/%s",
1738 smb_dir, de->d_name);
1746 /* automatic user mode samba server configuration */
1747 void net_slirp_smb(const char *exported_dir)
1749 char smb_conf[1024];
1750 char smb_cmdline[1024];
1753 if (!slirp_inited) {
1758 /* XXX: better tmp dir construction */
1759 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
1760 if (mkdir(smb_dir, 0700) < 0) {
1761 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
1764 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
1766 f = fopen(smb_conf, "w");
1768 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
1775 "socket address=127.0.0.1\n"
1776 "pid directory=%s\n"
1777 "lock directory=%s\n"
1778 "log file=%s/log.smbd\n"
1779 "smb passwd file=%s/smbpasswd\n"
1780 "security = share\n"
1795 snprintf(smb_cmdline, sizeof(smb_cmdline), "/usr/sbin/smbd -s %s",
1798 slirp_add_exec(0, smb_cmdline, 4, 139);
1801 #endif /* !defined(_WIN32) */
1803 #endif /* CONFIG_SLIRP */
1805 #if !defined(_WIN32)
1807 static int tun_open(char *ifname, int ifname_size)
1813 fd = open("/dev/tap", O_RDWR);
1815 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1820 dev = devname(s.st_rdev, S_IFCHR);
1821 pstrcpy(ifname, ifname_size, dev);
1823 fcntl(fd, F_SETFL, O_NONBLOCK);
1827 static int tun_open(char *ifname, int ifname_size)
1832 fd = open("/dev/net/tun", O_RDWR);
1834 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1837 memset(&ifr, 0, sizeof(ifr));
1838 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1839 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
1840 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1842 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1846 printf("Connected to host network interface: %s\n", ifr.ifr_name);
1847 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1848 fcntl(fd, F_SETFL, O_NONBLOCK);
1853 static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1855 write(nd->fd, buf, size);
1858 static void tun_add_read_packet(NetDriverState *nd,
1859 IOCanRWHandler *fd_can_read,
1860 IOReadHandler *fd_read, void *opaque)
1862 qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque);
1865 static int net_tun_init(NetDriverState *nd)
1871 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
1875 /* try to launch network init script */
1880 *parg++ = network_script;
1881 *parg++ = nd->ifname;
1883 execv(network_script, args);
1886 while (waitpid(pid, &status, 0) != pid);
1887 if (!WIFEXITED(status) ||
1888 WEXITSTATUS(status) != 0) {
1889 fprintf(stderr, "%s: could not launch network script\n",
1893 nd->send_packet = tun_send_packet;
1894 nd->add_read_packet = tun_add_read_packet;
1898 static int net_fd_init(NetDriverState *nd, int fd)
1901 nd->send_packet = tun_send_packet;
1902 nd->add_read_packet = tun_add_read_packet;
1903 pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
1907 #endif /* !_WIN32 */
1909 /***********************************************************/
1912 static int usb_device_add(const char *devname)
1920 for(i = 0;i < MAX_VM_USB_PORTS; i++) {
1921 if (!vm_usb_ports[i]->dev)
1924 if (i == MAX_VM_USB_PORTS)
1927 if (strstart(devname, "host:", &p)) {
1928 dev = usb_host_device_open(p);
1931 } else if (!strcmp(devname, "mouse")) {
1932 dev = usb_mouse_init();
1938 usb_attach(vm_usb_ports[i], dev);
1942 static int usb_device_del(const char *devname)
1945 int bus_num, addr, i;
1951 p = strchr(devname, '.');
1954 bus_num = strtoul(devname, NULL, 0);
1955 addr = strtoul(p + 1, NULL, 0);
1958 for(i = 0;i < MAX_VM_USB_PORTS; i++) {
1959 dev = vm_usb_ports[i]->dev;
1960 if (dev && dev->addr == addr)
1963 if (i == MAX_VM_USB_PORTS)
1965 usb_attach(vm_usb_ports[i], NULL);
1969 void do_usb_add(const char *devname)
1972 ret = usb_device_add(devname);
1974 term_printf("Could not add USB device '%s'\n", devname);
1977 void do_usb_del(const char *devname)
1980 ret = usb_device_del(devname);
1982 term_printf("Could not remove USB device '%s'\n", devname);
1989 const char *speed_str;
1992 term_printf("USB support not enabled\n");
1996 for(i = 0; i < MAX_VM_USB_PORTS; i++) {
1997 dev = vm_usb_ports[i]->dev;
1999 term_printf("Hub port %d:\n", i);
2000 switch(dev->speed) {
2004 case USB_SPEED_FULL:
2007 case USB_SPEED_HIGH:
2014 term_printf(" Device %d.%d, speed %s Mb/s\n",
2015 0, dev->addr, speed_str);
2020 /***********************************************************/
2023 static char *pid_filename;
2025 /* Remove PID file. Called on normal exit */
2027 static void remove_pidfile(void)
2029 unlink (pid_filename);
2032 static void create_pidfile(const char *filename)
2034 struct stat pidstat;
2037 /* Try to write our PID to the named file */
2038 if (stat(filename, &pidstat) < 0) {
2039 if (errno == ENOENT) {
2040 if ((f = fopen (filename, "w")) == NULL) {
2041 perror("Opening pidfile");
2044 fprintf(f, "%d\n", getpid());
2046 pid_filename = qemu_strdup(filename);
2047 if (!pid_filename) {
2048 fprintf(stderr, "Could not save PID filename");
2051 atexit(remove_pidfile);
2054 fprintf(stderr, "%s already exists. Remove it and try again.\n",
2060 /***********************************************************/
2063 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
2067 static void dumb_resize(DisplayState *ds, int w, int h)
2071 static void dumb_refresh(DisplayState *ds)
2073 vga_update_display();
2076 void dumb_display_init(DisplayState *ds)
2081 ds->dpy_update = dumb_update;
2082 ds->dpy_resize = dumb_resize;
2083 ds->dpy_refresh = dumb_refresh;
2086 #if !defined(CONFIG_SOFTMMU)
2087 /***********************************************************/
2088 /* cpu signal handler */
2089 static void host_segv_handler(int host_signum, siginfo_t *info,
2092 if (cpu_signal_handler(host_signum, info, puc))
2094 if (stdio_nb_clients > 0)
2100 /***********************************************************/
2103 #define MAX_IO_HANDLERS 64
2105 typedef struct IOHandlerRecord {
2107 IOCanRWHandler *fd_can_read;
2108 IOReadHandler *fd_read;
2110 /* temporary data */
2113 struct IOHandlerRecord *next;
2116 static IOHandlerRecord *first_io_handler;
2118 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
2119 IOReadHandler *fd_read, void *opaque)
2121 IOHandlerRecord *ioh;
2123 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
2127 ioh->fd_can_read = fd_can_read;
2128 ioh->fd_read = fd_read;
2129 ioh->opaque = opaque;
2130 ioh->next = first_io_handler;
2131 first_io_handler = ioh;
2135 void qemu_del_fd_read_handler(int fd)
2137 IOHandlerRecord **pioh, *ioh;
2139 pioh = &first_io_handler;
2144 if (ioh->fd == fd) {
2152 /***********************************************************/
2153 /* savevm/loadvm support */
2155 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
2157 fwrite(buf, 1, size, f);
2160 void qemu_put_byte(QEMUFile *f, int v)
2165 void qemu_put_be16(QEMUFile *f, unsigned int v)
2167 qemu_put_byte(f, v >> 8);
2168 qemu_put_byte(f, v);
2171 void qemu_put_be32(QEMUFile *f, unsigned int v)
2173 qemu_put_byte(f, v >> 24);
2174 qemu_put_byte(f, v >> 16);
2175 qemu_put_byte(f, v >> 8);
2176 qemu_put_byte(f, v);
2179 void qemu_put_be64(QEMUFile *f, uint64_t v)
2181 qemu_put_be32(f, v >> 32);
2182 qemu_put_be32(f, v);
2185 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
2187 return fread(buf, 1, size, f);
2190 int qemu_get_byte(QEMUFile *f)
2200 unsigned int qemu_get_be16(QEMUFile *f)
2203 v = qemu_get_byte(f) << 8;
2204 v |= qemu_get_byte(f);
2208 unsigned int qemu_get_be32(QEMUFile *f)
2211 v = qemu_get_byte(f) << 24;
2212 v |= qemu_get_byte(f) << 16;
2213 v |= qemu_get_byte(f) << 8;
2214 v |= qemu_get_byte(f);
2218 uint64_t qemu_get_be64(QEMUFile *f)
2221 v = (uint64_t)qemu_get_be32(f) << 32;
2222 v |= qemu_get_be32(f);
2226 int64_t qemu_ftell(QEMUFile *f)
2231 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
2233 if (fseek(f, pos, whence) < 0)
2238 typedef struct SaveStateEntry {
2242 SaveStateHandler *save_state;
2243 LoadStateHandler *load_state;
2245 struct SaveStateEntry *next;
2248 static SaveStateEntry *first_se;
2250 int register_savevm(const char *idstr,
2253 SaveStateHandler *save_state,
2254 LoadStateHandler *load_state,
2257 SaveStateEntry *se, **pse;
2259 se = qemu_malloc(sizeof(SaveStateEntry));
2262 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
2263 se->instance_id = instance_id;
2264 se->version_id = version_id;
2265 se->save_state = save_state;
2266 se->load_state = load_state;
2267 se->opaque = opaque;
2270 /* add at the end of list */
2272 while (*pse != NULL)
2273 pse = &(*pse)->next;
2278 #define QEMU_VM_FILE_MAGIC 0x5145564d
2279 #define QEMU_VM_FILE_VERSION 0x00000001
2281 int qemu_savevm(const char *filename)
2285 int len, len_pos, cur_pos, saved_vm_running, ret;
2287 saved_vm_running = vm_running;
2290 f = fopen(filename, "wb");
2296 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
2297 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
2299 for(se = first_se; se != NULL; se = se->next) {
2301 len = strlen(se->idstr);
2302 qemu_put_byte(f, len);
2303 qemu_put_buffer(f, se->idstr, len);
2305 qemu_put_be32(f, se->instance_id);
2306 qemu_put_be32(f, se->version_id);
2308 /* record size: filled later */
2310 qemu_put_be32(f, 0);
2312 se->save_state(f, se->opaque);
2314 /* fill record size */
2316 len = ftell(f) - len_pos - 4;
2317 fseek(f, len_pos, SEEK_SET);
2318 qemu_put_be32(f, len);
2319 fseek(f, cur_pos, SEEK_SET);
2325 if (saved_vm_running)
2330 static SaveStateEntry *find_se(const char *idstr, int instance_id)
2334 for(se = first_se; se != NULL; se = se->next) {
2335 if (!strcmp(se->idstr, idstr) &&
2336 instance_id == se->instance_id)
2342 int qemu_loadvm(const char *filename)
2346 int len, cur_pos, ret, instance_id, record_len, version_id;
2347 int saved_vm_running;
2351 saved_vm_running = vm_running;
2354 f = fopen(filename, "rb");
2360 v = qemu_get_be32(f);
2361 if (v != QEMU_VM_FILE_MAGIC)
2363 v = qemu_get_be32(f);
2364 if (v != QEMU_VM_FILE_VERSION) {
2371 #if defined (DO_TB_FLUSH)
2372 tb_flush(global_env);
2374 len = qemu_get_byte(f);
2377 qemu_get_buffer(f, idstr, len);
2379 instance_id = qemu_get_be32(f);
2380 version_id = qemu_get_be32(f);
2381 record_len = qemu_get_be32(f);
2383 printf("idstr=%s instance=0x%x version=%d len=%d\n",
2384 idstr, instance_id, version_id, record_len);
2387 se = find_se(idstr, instance_id);
2389 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
2390 instance_id, idstr);
2392 ret = se->load_state(f, se->opaque, version_id);
2394 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2395 instance_id, idstr);
2398 /* always seek to exact end of record */
2399 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
2404 if (saved_vm_running)
2409 /***********************************************************/
2410 /* cpu save/restore */
2412 #if defined(TARGET_I386)
2414 static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
2416 qemu_put_be32(f, dt->selector);
2417 qemu_put_betl(f, dt->base);
2418 qemu_put_be32(f, dt->limit);
2419 qemu_put_be32(f, dt->flags);
2422 static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
2424 dt->selector = qemu_get_be32(f);
2425 dt->base = qemu_get_betl(f);
2426 dt->limit = qemu_get_be32(f);
2427 dt->flags = qemu_get_be32(f);
2430 void cpu_save(QEMUFile *f, void *opaque)
2432 CPUState *env = opaque;
2433 uint16_t fptag, fpus, fpuc, fpregs_format;
2437 for(i = 0; i < CPU_NB_REGS; i++)
2438 qemu_put_betls(f, &env->regs[i]);
2439 qemu_put_betls(f, &env->eip);
2440 qemu_put_betls(f, &env->eflags);
2441 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
2442 qemu_put_be32s(f, &hflags);
2446 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2448 for(i = 0; i < 8; i++) {
2449 fptag |= ((!env->fptags[i]) << i);
2452 qemu_put_be16s(f, &fpuc);
2453 qemu_put_be16s(f, &fpus);
2454 qemu_put_be16s(f, &fptag);
2456 #ifdef USE_X86LDOUBLE
2461 qemu_put_be16s(f, &fpregs_format);
2463 for(i = 0; i < 8; i++) {
2464 #ifdef USE_X86LDOUBLE
2468 /* we save the real CPU data (in case of MMX usage only 'mant'
2469 contains the MMX register */
2470 cpu_get_fp80(&mant, &exp, env->fpregs[i].d);
2471 qemu_put_be64(f, mant);
2472 qemu_put_be16(f, exp);
2475 /* if we use doubles for float emulation, we save the doubles to
2476 avoid losing information in case of MMX usage. It can give
2477 problems if the image is restored on a CPU where long
2478 doubles are used instead. */
2479 qemu_put_be64(f, env->fpregs[i].mmx.MMX_Q(0));
2483 for(i = 0; i < 6; i++)
2484 cpu_put_seg(f, &env->segs[i]);
2485 cpu_put_seg(f, &env->ldt);
2486 cpu_put_seg(f, &env->tr);
2487 cpu_put_seg(f, &env->gdt);
2488 cpu_put_seg(f, &env->idt);
2490 qemu_put_be32s(f, &env->sysenter_cs);
2491 qemu_put_be32s(f, &env->sysenter_esp);
2492 qemu_put_be32s(f, &env->sysenter_eip);
2494 qemu_put_betls(f, &env->cr[0]);
2495 qemu_put_betls(f, &env->cr[2]);
2496 qemu_put_betls(f, &env->cr[3]);
2497 qemu_put_betls(f, &env->cr[4]);
2499 for(i = 0; i < 8; i++)
2500 qemu_put_betls(f, &env->dr[i]);
2503 qemu_put_be32s(f, &env->a20_mask);
2506 qemu_put_be32s(f, &env->mxcsr);
2507 for(i = 0; i < CPU_NB_REGS; i++) {
2508 qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(0));
2509 qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(1));
2512 #ifdef TARGET_X86_64
2513 qemu_put_be64s(f, &env->efer);
2514 qemu_put_be64s(f, &env->star);
2515 qemu_put_be64s(f, &env->lstar);
2516 qemu_put_be64s(f, &env->cstar);
2517 qemu_put_be64s(f, &env->fmask);
2518 qemu_put_be64s(f, &env->kernelgsbase);
2522 #ifdef USE_X86LDOUBLE
2523 /* XXX: add that in a FPU generic layer */
2524 union x86_longdouble {
2529 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
2530 #define EXPBIAS1 1023
2531 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
2532 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
2534 static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp)
2538 p->mant = (MANTD1(temp) << 11) | (1LL << 63);
2539 /* exponent + sign */
2540 e = EXPD1(temp) - EXPBIAS1 + 16383;
2541 e |= SIGND1(temp) >> 16;
2546 int cpu_load(QEMUFile *f, void *opaque, int version_id)
2548 CPUState *env = opaque;
2551 uint16_t fpus, fpuc, fptag, fpregs_format;
2553 if (version_id != 3)
2555 for(i = 0; i < CPU_NB_REGS; i++)
2556 qemu_get_betls(f, &env->regs[i]);
2557 qemu_get_betls(f, &env->eip);
2558 qemu_get_betls(f, &env->eflags);
2559 qemu_get_be32s(f, &hflags);
2561 qemu_get_be16s(f, &fpuc);
2562 qemu_get_be16s(f, &fpus);
2563 qemu_get_be16s(f, &fptag);
2564 qemu_get_be16s(f, &fpregs_format);
2566 /* NOTE: we cannot always restore the FPU state if the image come
2567 from a host with a different 'USE_X86LDOUBLE' define. We guess
2568 if we are in an MMX state to restore correctly in that case. */
2569 guess_mmx = ((fptag == 0xff) && (fpus & 0x3800) == 0);
2570 for(i = 0; i < 8; i++) {
2574 switch(fpregs_format) {
2576 mant = qemu_get_be64(f);
2577 exp = qemu_get_be16(f);
2578 #ifdef USE_X86LDOUBLE
2579 env->fpregs[i].d = cpu_set_fp80(mant, exp);
2581 /* difficult case */
2583 env->fpregs[i].mmx.MMX_Q(0) = mant;
2585 env->fpregs[i].d = cpu_set_fp80(mant, exp);
2589 mant = qemu_get_be64(f);
2590 #ifdef USE_X86LDOUBLE
2592 union x86_longdouble *p;
2593 /* difficult case */
2594 p = (void *)&env->fpregs[i];
2599 fp64_to_fp80(p, mant);
2603 env->fpregs[i].mmx.MMX_Q(0) = mant;
2612 /* XXX: restore FPU round state */
2613 env->fpstt = (fpus >> 11) & 7;
2614 env->fpus = fpus & ~0x3800;
2616 for(i = 0; i < 8; i++) {
2617 env->fptags[i] = (fptag >> i) & 1;
2620 for(i = 0; i < 6; i++)
2621 cpu_get_seg(f, &env->segs[i]);
2622 cpu_get_seg(f, &env->ldt);
2623 cpu_get_seg(f, &env->tr);
2624 cpu_get_seg(f, &env->gdt);
2625 cpu_get_seg(f, &env->idt);
2627 qemu_get_be32s(f, &env->sysenter_cs);
2628 qemu_get_be32s(f, &env->sysenter_esp);
2629 qemu_get_be32s(f, &env->sysenter_eip);
2631 qemu_get_betls(f, &env->cr[0]);
2632 qemu_get_betls(f, &env->cr[2]);
2633 qemu_get_betls(f, &env->cr[3]);
2634 qemu_get_betls(f, &env->cr[4]);
2636 for(i = 0; i < 8; i++)
2637 qemu_get_betls(f, &env->dr[i]);
2640 qemu_get_be32s(f, &env->a20_mask);
2642 qemu_get_be32s(f, &env->mxcsr);
2643 for(i = 0; i < CPU_NB_REGS; i++) {
2644 qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(0));
2645 qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(1));
2648 #ifdef TARGET_X86_64
2649 qemu_get_be64s(f, &env->efer);
2650 qemu_get_be64s(f, &env->star);
2651 qemu_get_be64s(f, &env->lstar);
2652 qemu_get_be64s(f, &env->cstar);
2653 qemu_get_be64s(f, &env->fmask);
2654 qemu_get_be64s(f, &env->kernelgsbase);
2657 /* XXX: compute hflags from scratch, except for CPL and IIF */
2658 env->hflags = hflags;
2663 #elif defined(TARGET_PPC)
2664 void cpu_save(QEMUFile *f, void *opaque)
2668 int cpu_load(QEMUFile *f, void *opaque, int version_id)
2673 #elif defined(TARGET_MIPS)
2674 void cpu_save(QEMUFile *f, void *opaque)
2678 int cpu_load(QEMUFile *f, void *opaque, int version_id)
2683 #elif defined(TARGET_SPARC)
2684 void cpu_save(QEMUFile *f, void *opaque)
2686 CPUState *env = opaque;
2690 for(i = 0; i < 8; i++)
2691 qemu_put_betls(f, &env->gregs[i]);
2692 for(i = 0; i < NWINDOWS * 16; i++)
2693 qemu_put_betls(f, &env->regbase[i]);
2696 for(i = 0; i < TARGET_FPREGS; i++) {
2702 qemu_put_betl(f, u.i);
2705 qemu_put_betls(f, &env->pc);
2706 qemu_put_betls(f, &env->npc);
2707 qemu_put_betls(f, &env->y);
2709 qemu_put_be32(f, tmp);
2710 qemu_put_betls(f, &env->fsr);
2711 qemu_put_betls(f, &env->tbr);
2712 #ifndef TARGET_SPARC64
2713 qemu_put_be32s(f, &env->wim);
2715 for(i = 0; i < 16; i++)
2716 qemu_put_be32s(f, &env->mmuregs[i]);
2720 int cpu_load(QEMUFile *f, void *opaque, int version_id)
2722 CPUState *env = opaque;
2726 for(i = 0; i < 8; i++)
2727 qemu_get_betls(f, &env->gregs[i]);
2728 for(i = 0; i < NWINDOWS * 16; i++)
2729 qemu_get_betls(f, &env->regbase[i]);
2732 for(i = 0; i < TARGET_FPREGS; i++) {
2737 u.i = qemu_get_betl(f);
2741 qemu_get_betls(f, &env->pc);
2742 qemu_get_betls(f, &env->npc);
2743 qemu_get_betls(f, &env->y);
2744 tmp = qemu_get_be32(f);
2745 env->cwp = 0; /* needed to ensure that the wrapping registers are
2746 correctly updated */
2748 qemu_get_betls(f, &env->fsr);
2749 qemu_get_betls(f, &env->tbr);
2750 #ifndef TARGET_SPARC64
2751 qemu_get_be32s(f, &env->wim);
2753 for(i = 0; i < 16; i++)
2754 qemu_get_be32s(f, &env->mmuregs[i]);
2761 #warning No CPU save/restore functions
2765 /***********************************************************/
2766 /* ram save/restore */
2768 /* we just avoid storing empty pages */
2769 static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
2774 for(i = 1; i < len; i++) {
2778 qemu_put_byte(f, 1);
2779 qemu_put_byte(f, v);
2782 qemu_put_byte(f, 0);
2783 qemu_put_buffer(f, buf, len);
2786 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
2790 v = qemu_get_byte(f);
2793 if (qemu_get_buffer(f, buf, len) != len)
2797 v = qemu_get_byte(f);
2798 memset(buf, v, len);
2806 static void ram_save(QEMUFile *f, void *opaque)
2809 qemu_put_be32(f, phys_ram_size);
2810 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
2811 ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
2815 static int ram_load(QEMUFile *f, void *opaque, int version_id)
2819 if (version_id != 1)
2821 if (qemu_get_be32(f) != phys_ram_size)
2823 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
2824 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
2831 /***********************************************************/
2832 /* machine registration */
2834 QEMUMachine *first_machine = NULL;
2836 int qemu_register_machine(QEMUMachine *m)
2839 pm = &first_machine;
2847 QEMUMachine *find_machine(const char *name)
2851 for(m = first_machine; m != NULL; m = m->next) {
2852 if (!strcmp(m->name, name))
2858 /***********************************************************/
2859 /* main execution loop */
2861 void gui_update(void *opaque)
2863 display_state.dpy_refresh(&display_state);
2864 qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
2867 /* XXX: support several handlers */
2868 VMStopHandler *vm_stop_cb;
2869 VMStopHandler *vm_stop_opaque;
2871 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
2874 vm_stop_opaque = opaque;
2878 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
2891 void vm_stop(int reason)
2894 cpu_disable_ticks();
2898 vm_stop_cb(vm_stop_opaque, reason);
2904 /* reset/shutdown handler */
2906 typedef struct QEMUResetEntry {
2907 QEMUResetHandler *func;
2909 struct QEMUResetEntry *next;
2912 static QEMUResetEntry *first_reset_entry;
2913 static int reset_requested;
2914 static int shutdown_requested;
2915 static int powerdown_requested;
2917 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
2919 QEMUResetEntry **pre, *re;
2921 pre = &first_reset_entry;
2922 while (*pre != NULL)
2923 pre = &(*pre)->next;
2924 re = qemu_mallocz(sizeof(QEMUResetEntry));
2926 re->opaque = opaque;
2931 void qemu_system_reset(void)
2935 /* reset all devices */
2936 for(re = first_reset_entry; re != NULL; re = re->next) {
2937 re->func(re->opaque);
2941 void qemu_system_reset_request(void)
2943 reset_requested = 1;
2944 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
2947 void qemu_system_shutdown_request(void)
2949 shutdown_requested = 1;
2950 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
2953 void qemu_system_powerdown_request(void)
2955 powerdown_requested = 1;
2956 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
2959 static void main_cpu_reset(void *opaque)
2961 #if defined(TARGET_I386) || defined(TARGET_SPARC)
2962 CPUState *env = opaque;
2967 void main_loop_wait(int timeout)
2970 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
2971 IOHandlerRecord *ioh, *ioh_next;
2981 /* poll any events */
2982 /* XXX: separate device handlers from system ones */
2984 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2985 if (!ioh->fd_can_read) {
2988 pf->events = POLLIN;
2992 max_size = ioh->fd_can_read(ioh->opaque);
2994 if (max_size > sizeof(buf))
2995 max_size = sizeof(buf);
2997 pf->events = POLLIN;
3004 ioh->max_size = max_size;
3007 ret = poll(ufds, pf - ufds, timeout);
3009 /* XXX: better handling of removal */
3010 for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
3011 ioh_next = ioh->next;
3014 if (pf->revents & POLLIN) {
3015 if (ioh->max_size == 0) {
3016 /* just a read event */
3017 ioh->fd_read(ioh->opaque, NULL, 0);
3019 n = read(ioh->fd, buf, ioh->max_size);
3021 ioh->fd_read(ioh->opaque, buf, n);
3022 } else if (errno != EAGAIN) {
3023 ioh->fd_read(ioh->opaque, NULL, -errno);
3030 #endif /* !defined(_WIN32) */
3031 #if defined(CONFIG_SLIRP)
3032 /* XXX: merge with poll() */
3034 fd_set rfds, wfds, xfds;
3042 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3045 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
3047 slirp_select_poll(&rfds, &wfds, &xfds);
3053 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
3054 qemu_get_clock(vm_clock));
3055 /* run dma transfers, if any */
3059 /* real time timers */
3060 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
3061 qemu_get_clock(rt_clock));
3067 CPUState *env = global_env;
3071 ret = cpu_exec(env);
3072 if (shutdown_requested) {
3073 ret = EXCP_INTERRUPT;
3076 if (reset_requested) {
3077 reset_requested = 0;
3078 qemu_system_reset();
3079 ret = EXCP_INTERRUPT;
3081 if (powerdown_requested) {
3082 powerdown_requested = 0;
3083 qemu_system_powerdown();
3084 ret = EXCP_INTERRUPT;
3086 if (ret == EXCP_DEBUG) {
3087 vm_stop(EXCP_DEBUG);
3089 /* if hlt instruction, we wait until the next IRQ */
3090 /* XXX: use timeout computed from timers */
3091 if (ret == EXCP_HLT)
3098 main_loop_wait(timeout);
3100 cpu_disable_ticks();
3106 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2005 Fabrice Bellard\n"
3107 "usage: %s [options] [disk_image]\n"
3109 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3111 "Standard options:\n"
3112 "-M machine select emulated machine (-M ? for list)\n"
3113 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3114 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3115 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3116 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3117 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
3118 "-snapshot write to temporary files instead of disk image files\n"
3119 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3120 "-nographic disable graphical output and redirect serial I/Os to console\n"
3122 "-k language use keyboard layout (for example \"fr\" for French)\n"
3125 "-enable-audio enable audio support, and all the sound cars\n"
3126 "-audio-help print list of audio drivers and their options\n"
3127 "-soundhw c1,... enable audio support\n"
3128 " and only specified sound cards (comma separated list)\n"
3129 " use -soundhw ? to get the list of supported cards\n"
3131 "-localtime set the real time clock to local time [default=utc]\n"
3132 "-full-screen start in full screen\n"
3134 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
3136 "-usb enable the USB driver (will be the default soon)\n"
3137 "-usbdevice name add the host or guest USB device 'name'\n"
3138 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3139 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
3142 "Network options:\n"
3143 "-nics n simulate 'n' network cards [default=1]\n"
3144 "-macaddr addr set the mac address of the first interface\n"
3145 "-n script set tap/tun network init script [default=%s]\n"
3146 "-tun-fd fd use this fd as already opened tap/tun interface\n"
3148 "-user-net use user mode network stack [default if no tap/tun script]\n"
3149 "-tftp prefix allow tftp access to files starting with prefix [-user-net]\n"
3151 "-smb dir allow SMB access to files in 'dir' [-user-net]\n"
3153 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
3154 " redirect TCP or UDP connections from host to guest [-user-net]\n"
3156 "-dummy-net use dummy network stack\n"
3158 "Linux boot specific:\n"
3159 "-kernel bzImage use 'bzImage' as kernel image\n"
3160 "-append cmdline use 'cmdline' as kernel command line\n"
3161 "-initrd file use 'file' as initial ram disk\n"
3163 "Debug/Expert options:\n"
3164 "-monitor dev redirect the monitor to char device 'dev'\n"
3165 "-serial dev redirect the serial port to char device 'dev'\n"
3166 "-parallel dev redirect the parallel port to char device 'dev'\n"
3167 "-pidfile file Write PID to 'file'\n"
3168 "-S freeze CPU at startup (use 'c' to start execution)\n"
3169 "-s wait gdb connection to port %d\n"
3170 "-p port change gdb connection port\n"
3171 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
3172 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
3173 " translation (t=none or lba) (usually qemu can guess them)\n"
3174 "-L path set the directory for the BIOS and VGA BIOS\n"
3176 "-no-kqemu disable KQEMU kernel module usage\n"
3178 #ifdef USE_CODE_COPY
3179 "-no-code-copy disable code copy acceleration\n"
3182 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
3183 " (default is CL-GD5446 PCI VGA)\n"
3185 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
3187 "During emulation, the following keys are useful:\n"
3188 "ctrl-alt-f toggle full screen\n"
3189 "ctrl-alt-n switch to virtual console 'n'\n"
3190 "ctrl-alt toggle mouse and keyboard grab\n"
3192 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3194 #ifdef CONFIG_SOFTMMU
3200 DEFAULT_NETWORK_SCRIPT,
3201 DEFAULT_GDBSTUB_PORT,
3203 #ifndef CONFIG_SOFTMMU
3205 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
3206 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
3212 #define HAS_ARG 0x0001
3226 QEMU_OPTION_snapshot,
3228 QEMU_OPTION_nographic,
3230 QEMU_OPTION_enable_audio,
3231 QEMU_OPTION_audio_help,
3232 QEMU_OPTION_soundhw,
3236 QEMU_OPTION_macaddr,
3239 QEMU_OPTION_user_net,
3243 QEMU_OPTION_dummy_net,
3255 QEMU_OPTION_no_code_copy,
3257 QEMU_OPTION_localtime,
3258 QEMU_OPTION_cirrusvga,
3260 QEMU_OPTION_std_vga,
3261 QEMU_OPTION_monitor,
3263 QEMU_OPTION_parallel,
3265 QEMU_OPTION_full_screen,
3266 QEMU_OPTION_pidfile,
3267 QEMU_OPTION_no_kqemu,
3268 QEMU_OPTION_win2k_hack,
3270 QEMU_OPTION_usbdevice,
3273 typedef struct QEMUOption {
3279 const QEMUOption qemu_options[] = {
3280 { "h", 0, QEMU_OPTION_h },
3282 { "M", HAS_ARG, QEMU_OPTION_M },
3283 { "fda", HAS_ARG, QEMU_OPTION_fda },
3284 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
3285 { "hda", HAS_ARG, QEMU_OPTION_hda },
3286 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
3287 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
3288 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
3289 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
3290 { "boot", HAS_ARG, QEMU_OPTION_boot },
3291 { "snapshot", 0, QEMU_OPTION_snapshot },
3292 { "m", HAS_ARG, QEMU_OPTION_m },
3293 { "nographic", 0, QEMU_OPTION_nographic },
3294 { "k", HAS_ARG, QEMU_OPTION_k },
3296 { "enable-audio", 0, QEMU_OPTION_enable_audio },
3297 { "audio-help", 0, QEMU_OPTION_audio_help },
3298 { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
3301 { "nics", HAS_ARG, QEMU_OPTION_nics},
3302 { "macaddr", HAS_ARG, QEMU_OPTION_macaddr},
3303 { "n", HAS_ARG, QEMU_OPTION_n },
3304 { "tun-fd", HAS_ARG, QEMU_OPTION_tun_fd },
3306 { "user-net", 0, QEMU_OPTION_user_net },
3307 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
3309 { "smb", HAS_ARG, QEMU_OPTION_smb },
3311 { "redir", HAS_ARG, QEMU_OPTION_redir },
3313 { "dummy-net", 0, QEMU_OPTION_dummy_net },
3315 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
3316 { "append", HAS_ARG, QEMU_OPTION_append },
3317 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
3319 { "S", 0, QEMU_OPTION_S },
3320 { "s", 0, QEMU_OPTION_s },
3321 { "p", HAS_ARG, QEMU_OPTION_p },
3322 { "d", HAS_ARG, QEMU_OPTION_d },
3323 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
3324 { "L", HAS_ARG, QEMU_OPTION_L },
3325 { "no-code-copy", 0, QEMU_OPTION_no_code_copy },
3327 { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
3329 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3330 { "g", 1, QEMU_OPTION_g },
3332 { "localtime", 0, QEMU_OPTION_localtime },
3333 { "std-vga", 0, QEMU_OPTION_std_vga },
3334 { "monitor", 1, QEMU_OPTION_monitor },
3335 { "serial", 1, QEMU_OPTION_serial },
3336 { "parallel", 1, QEMU_OPTION_parallel },
3337 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
3338 { "full-screen", 0, QEMU_OPTION_full_screen },
3339 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
3340 { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
3341 { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
3343 /* temporary options */
3344 { "usb", 0, QEMU_OPTION_usb },
3345 { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
3349 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3351 /* this stack is only used during signal handling */
3352 #define SIGNAL_STACK_SIZE 32768
3354 static uint8_t *signal_stack;
3358 /* password input */
3360 static BlockDriverState *get_bdrv(int index)
3362 BlockDriverState *bs;
3365 bs = bs_table[index];
3366 } else if (index < 6) {
3367 bs = fd_table[index - 4];
3374 static void read_passwords(void)
3376 BlockDriverState *bs;
3380 for(i = 0; i < 6; i++) {
3382 if (bs && bdrv_is_encrypted(bs)) {
3383 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs));
3384 for(j = 0; j < 3; j++) {
3385 monitor_readline("Password: ",
3386 1, password, sizeof(password));
3387 if (bdrv_set_key(bs, password) == 0)
3389 term_printf("invalid password\n");
3395 /* XXX: currently we cannot use simultaneously different CPUs */
3396 void register_machines(void)
3398 #if defined(TARGET_I386)
3399 qemu_register_machine(&pc_machine);
3400 qemu_register_machine(&isapc_machine);
3401 #elif defined(TARGET_PPC)
3402 qemu_register_machine(&heathrow_machine);
3403 qemu_register_machine(&core99_machine);
3404 qemu_register_machine(&prep_machine);
3405 #elif defined(TARGET_MIPS)
3406 qemu_register_machine(&mips_machine);
3407 #elif defined(TARGET_SPARC)
3408 #ifdef TARGET_SPARC64
3409 qemu_register_machine(&sun4u_machine);
3411 qemu_register_machine(&sun4m_machine);
3417 static void select_soundhw (const char *optarg)
3419 if (*optarg == '?') {
3421 printf ("Valid sound card names (comma separated):\n");
3422 printf ("sb16 Creative Sound Blaster 16\n");
3425 printf ("adlib Yamaha YMF262 (OPL3)\n");
3427 printf ("adlib Yamaha YM3812 (OPL2)\n");
3431 printf ("gus Gravis Ultrasound GF1\n");
3433 printf ("es1370 ENSONIQ AudioPCI ES1370\n");
3434 exit (*optarg != '?');
3441 { "sb16", &sb16_enabled },
3443 { "adlib", &adlib_enabled },
3446 { "gus", &gus_enabled },
3448 { "es1370", &es1370_enabled },
3450 size_t tablen, l, i;
3456 tablen = sizeof (soundhw_tab) / sizeof (soundhw_tab[0]);
3459 e = strchr (p, ',');
3460 l = !e ? strlen (p) : (size_t) (e - p);
3461 for (i = 0; i < tablen; ++i) {
3462 if (!strncmp (soundhw_tab[i].name, p, l)) {
3464 *soundhw_tab[i].enabledp = 1;
3471 "Unknown sound card name (too big to show)\n");
3474 fprintf (stderr, "Unknown sound card name `%.*s'\n",
3479 p += l + (e != NULL);
3483 goto show_valid_cards;
3488 #define NET_IF_TUN 0
3489 #define NET_IF_USER 1
3490 #define NET_IF_DUMMY 2
3492 int main(int argc, char **argv)
3494 #ifdef CONFIG_GDBSTUB
3495 int use_gdbstub, gdbstub_port;
3498 int snapshot, linux_boot;
3500 const char *initrd_filename;
3501 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
3502 const char *kernel_filename, *kernel_cmdline;
3503 DisplayState *ds = &display_state;
3504 int cyls, heads, secs, translation;
3505 int start_emulation = 1;
3507 int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
3509 const char *r, *optarg;
3510 CharDriverState *monitor_hd;
3511 char monitor_device[128];
3512 char serial_devices[MAX_SERIAL_PORTS][128];
3513 int serial_device_index;
3514 char parallel_devices[MAX_PARALLEL_PORTS][128];
3515 int parallel_device_index;
3516 const char *loadvm = NULL;
3517 QEMUMachine *machine;
3518 char usb_devices[MAX_VM_USB_PORTS][128];
3519 int usb_devices_index;
3521 #if !defined(CONFIG_SOFTMMU)
3522 /* we never want that malloc() uses mmap() */
3523 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
3525 register_machines();
3526 machine = first_machine;
3527 initrd_filename = NULL;
3528 for(i = 0; i < MAX_FD; i++)
3529 fd_filename[i] = NULL;
3530 for(i = 0; i < MAX_DISKS; i++)
3531 hd_filename[i] = NULL;
3532 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3533 vga_ram_size = VGA_RAM_SIZE;
3534 bios_size = BIOS_SIZE;
3535 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
3536 #ifdef CONFIG_GDBSTUB
3538 gdbstub_port = DEFAULT_GDBSTUB_PORT;
3542 kernel_filename = NULL;
3543 kernel_cmdline = "";
3549 cyls = heads = secs = 0;
3550 translation = BIOS_ATA_TRANSLATION_AUTO;
3551 pstrcpy(monitor_device, sizeof(monitor_device), "vc");
3553 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc");
3554 for(i = 1; i < MAX_SERIAL_PORTS; i++)
3555 serial_devices[i][0] = '\0';
3556 serial_device_index = 0;
3558 pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "vc");
3559 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
3560 parallel_devices[i][0] = '\0';
3561 parallel_device_index = 0;
3563 usb_devices_index = 0;
3568 /* default mac address of the first network interface */
3582 hd_filename[0] = argv[optind++];
3584 const QEMUOption *popt;
3587 popt = qemu_options;
3590 fprintf(stderr, "%s: invalid option -- '%s'\n",
3594 if (!strcmp(popt->name, r + 1))
3598 if (popt->flags & HAS_ARG) {
3599 if (optind >= argc) {
3600 fprintf(stderr, "%s: option '%s' requires an argument\n",
3604 optarg = argv[optind++];
3609 switch(popt->index) {
3611 machine = find_machine(optarg);
3614 printf("Supported machines are:\n");
3615 for(m = first_machine; m != NULL; m = m->next) {
3616 printf("%-10s %s%s\n",
3618 m == first_machine ? " (default)" : "");
3623 case QEMU_OPTION_initrd:
3624 initrd_filename = optarg;
3626 case QEMU_OPTION_hda:
3627 case QEMU_OPTION_hdb:
3628 case QEMU_OPTION_hdc:
3629 case QEMU_OPTION_hdd:
3632 hd_index = popt->index - QEMU_OPTION_hda;
3633 hd_filename[hd_index] = optarg;
3634 if (hd_index == cdrom_index)
3638 case QEMU_OPTION_snapshot:
3641 case QEMU_OPTION_hdachs:
3645 cyls = strtol(p, (char **)&p, 0);
3646 if (cyls < 1 || cyls > 16383)
3651 heads = strtol(p, (char **)&p, 0);
3652 if (heads < 1 || heads > 16)
3657 secs = strtol(p, (char **)&p, 0);
3658 if (secs < 1 || secs > 63)
3662 if (!strcmp(p, "none"))
3663 translation = BIOS_ATA_TRANSLATION_NONE;
3664 else if (!strcmp(p, "lba"))
3665 translation = BIOS_ATA_TRANSLATION_LBA;
3666 else if (!strcmp(p, "auto"))
3667 translation = BIOS_ATA_TRANSLATION_AUTO;
3670 } else if (*p != '\0') {
3672 fprintf(stderr, "qemu: invalid physical CHS format\n");
3677 case QEMU_OPTION_nographic:
3678 pstrcpy(monitor_device, sizeof(monitor_device), "stdio");
3679 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio");
3682 case QEMU_OPTION_kernel:
3683 kernel_filename = optarg;
3685 case QEMU_OPTION_append:
3686 kernel_cmdline = optarg;
3688 case QEMU_OPTION_tun_fd:
3692 net_if_type = NET_IF_TUN;
3693 if (nb_tun_fds < MAX_NICS) {
3694 fd = strtol(optarg, (char **)&p, 0);
3696 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
3699 tun_fds[nb_tun_fds++] = fd;
3703 case QEMU_OPTION_cdrom:
3704 if (cdrom_index >= 0) {
3705 hd_filename[cdrom_index] = optarg;
3708 case QEMU_OPTION_boot:
3709 boot_device = optarg[0];
3710 if (boot_device != 'a' &&
3713 boot_device != 'n' &&
3715 boot_device != 'c' && boot_device != 'd') {
3716 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
3720 case QEMU_OPTION_fda:
3721 fd_filename[0] = optarg;
3723 case QEMU_OPTION_fdb:
3724 fd_filename[1] = optarg;
3726 case QEMU_OPTION_no_code_copy:
3727 code_copy_enabled = 0;
3729 case QEMU_OPTION_nics:
3730 nb_nics = atoi(optarg);
3731 if (nb_nics < 0 || nb_nics > MAX_NICS) {
3732 fprintf(stderr, "qemu: invalid number of network interfaces\n");
3736 case QEMU_OPTION_macaddr:
3741 for(i = 0; i < 6; i++) {
3742 macaddr[i] = strtol(p, (char **)&p, 16);
3749 fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
3758 case QEMU_OPTION_tftp:
3759 tftp_prefix = optarg;
3762 case QEMU_OPTION_smb:
3763 net_slirp_smb(optarg);
3766 case QEMU_OPTION_user_net:
3767 net_if_type = NET_IF_USER;
3769 case QEMU_OPTION_redir:
3770 net_slirp_redir(optarg);
3773 case QEMU_OPTION_dummy_net:
3774 net_if_type = NET_IF_DUMMY;
3777 case QEMU_OPTION_enable_audio:
3784 case QEMU_OPTION_audio_help:
3788 case QEMU_OPTION_soundhw:
3789 select_soundhw (optarg);
3796 ram_size = atoi(optarg) * 1024 * 1024;
3799 if (ram_size > PHYS_RAM_MAX_SIZE) {
3800 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
3801 PHYS_RAM_MAX_SIZE / (1024 * 1024));
3810 mask = cpu_str_to_log_mask(optarg);
3812 printf("Log items (comma separated):\n");
3813 for(item = cpu_log_items; item->mask != 0; item++) {
3814 printf("%-10s %s\n", item->name, item->help);
3822 pstrcpy(network_script, sizeof(network_script), optarg);
3824 #ifdef CONFIG_GDBSTUB
3829 gdbstub_port = atoi(optarg);
3836 start_emulation = 0;
3839 keyboard_layout = optarg;
3841 case QEMU_OPTION_localtime:
3844 case QEMU_OPTION_cirrusvga:
3845 cirrus_vga_enabled = 1;
3847 case QEMU_OPTION_std_vga:
3848 cirrus_vga_enabled = 0;
3855 w = strtol(p, (char **)&p, 10);
3858 fprintf(stderr, "qemu: invalid resolution or depth\n");
3864 h = strtol(p, (char **)&p, 10);
3869 depth = strtol(p, (char **)&p, 10);
3870 if (depth != 8 && depth != 15 && depth != 16 &&
3871 depth != 24 && depth != 32)
3873 } else if (*p == '\0') {
3874 depth = graphic_depth;
3881 graphic_depth = depth;
3884 case QEMU_OPTION_monitor:
3885 pstrcpy(monitor_device, sizeof(monitor_device), optarg);
3887 case QEMU_OPTION_serial:
3888 if (serial_device_index >= MAX_SERIAL_PORTS) {
3889 fprintf(stderr, "qemu: too many serial ports\n");
3892 pstrcpy(serial_devices[serial_device_index],
3893 sizeof(serial_devices[0]), optarg);
3894 serial_device_index++;
3896 case QEMU_OPTION_parallel:
3897 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
3898 fprintf(stderr, "qemu: too many parallel ports\n");
3901 pstrcpy(parallel_devices[parallel_device_index],
3902 sizeof(parallel_devices[0]), optarg);
3903 parallel_device_index++;
3905 case QEMU_OPTION_loadvm:
3908 case QEMU_OPTION_full_screen:
3911 case QEMU_OPTION_pidfile:
3912 create_pidfile(optarg);
3915 case QEMU_OPTION_win2k_hack:
3916 win2k_install_hack = 1;
3920 case QEMU_OPTION_no_kqemu:
3924 case QEMU_OPTION_usb:
3927 case QEMU_OPTION_usbdevice:
3929 if (usb_devices_index >= MAX_VM_USB_PORTS) {
3930 fprintf(stderr, "Too many USB devices\n");
3933 pstrcpy(usb_devices[usb_devices_index],
3934 sizeof(usb_devices[usb_devices_index]),
3936 usb_devices_index++;
3942 linux_boot = (kernel_filename != NULL);
3945 hd_filename[0] == '\0' &&
3946 (cdrom_index >= 0 && hd_filename[cdrom_index] == '\0') &&
3947 fd_filename[0] == '\0')
3950 /* boot to cd by default if no hard disk */
3951 if (hd_filename[0] == '\0' && boot_device == 'c') {
3952 if (fd_filename[0] != '\0')
3958 #if !defined(CONFIG_SOFTMMU)
3959 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
3961 static uint8_t stdout_buf[4096];
3962 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
3965 setvbuf(stdout, NULL, _IOLBF, 0);
3968 /* init host network redirectors */
3969 if (net_if_type == -1) {
3970 net_if_type = NET_IF_TUN;
3971 #if defined(CONFIG_SLIRP)
3972 if (access(network_script, R_OK) < 0) {
3973 net_if_type = NET_IF_USER;
3978 for(i = 0; i < nb_nics; i++) {
3979 NetDriverState *nd = &nd_table[i];
3981 /* init virtual mac address */
3982 nd->macaddr[0] = macaddr[0];
3983 nd->macaddr[1] = macaddr[1];
3984 nd->macaddr[2] = macaddr[2];
3985 nd->macaddr[3] = macaddr[3];
3986 nd->macaddr[4] = macaddr[4];
3987 nd->macaddr[5] = macaddr[5] + i;
3988 switch(net_if_type) {
3989 #if defined(CONFIG_SLIRP)
3994 #if !defined(_WIN32)
3996 if (i < nb_tun_fds) {
3997 net_fd_init(nd, tun_fds[i]);
3999 if (net_tun_init(nd) < 0)
4011 /* init the memory */
4012 phys_ram_size = ram_size + vga_ram_size + bios_size;
4014 #ifdef CONFIG_SOFTMMU
4015 phys_ram_base = qemu_vmalloc(phys_ram_size);
4016 if (!phys_ram_base) {
4017 fprintf(stderr, "Could not allocate physical memory\n");
4021 /* as we must map the same page at several addresses, we must use
4026 tmpdir = getenv("QEMU_TMPDIR");
4029 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
4030 if (mkstemp(phys_ram_file) < 0) {
4031 fprintf(stderr, "Could not create temporary memory file '%s'\n",
4035 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
4036 if (phys_ram_fd < 0) {
4037 fprintf(stderr, "Could not open temporary memory file '%s'\n",
4041 ftruncate(phys_ram_fd, phys_ram_size);
4042 unlink(phys_ram_file);
4043 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
4045 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
4047 if (phys_ram_base == MAP_FAILED) {
4048 fprintf(stderr, "Could not map physical memory\n");
4054 /* we always create the cdrom drive, even if no disk is there */
4056 if (cdrom_index >= 0) {
4057 bs_table[cdrom_index] = bdrv_new("cdrom");
4058 bdrv_set_type_hint(bs_table[cdrom_index], BDRV_TYPE_CDROM);
4061 /* open the virtual block devices */
4062 for(i = 0; i < MAX_DISKS; i++) {
4063 if (hd_filename[i]) {
4066 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
4067 bs_table[i] = bdrv_new(buf);
4069 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
4070 fprintf(stderr, "qemu: could not open hard disk image '%s'\n",
4074 if (i == 0 && cyls != 0) {
4075 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
4076 bdrv_set_translation_hint(bs_table[i], translation);
4081 /* we always create at least one floppy disk */
4082 fd_table[0] = bdrv_new("fda");
4083 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
4085 for(i = 0; i < MAX_FD; i++) {
4086 if (fd_filename[i]) {
4089 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
4090 fd_table[i] = bdrv_new(buf);
4091 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
4093 if (fd_filename[i] != '\0') {
4094 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
4095 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
4103 /* init USB devices */
4105 vm_usb_hub = usb_hub_init(vm_usb_ports, MAX_VM_USB_PORTS);
4106 for(i = 0; i < usb_devices_index; i++) {
4107 if (usb_device_add(usb_devices[i]) < 0) {
4108 fprintf(stderr, "Warning: could not add USB device %s\n",
4114 /* init CPU state */
4117 cpu_single_env = env;
4119 register_savevm("timer", 0, 1, timer_save, timer_load, env);
4120 register_savevm("cpu", 0, 3, cpu_save, cpu_load, env);
4121 register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
4122 qemu_register_reset(main_cpu_reset, global_env);
4125 cpu_calibrate_ticks();
4129 dumb_display_init(ds);
4131 #if defined(CONFIG_SDL)
4132 sdl_display_init(ds, full_screen);
4133 #elif defined(CONFIG_COCOA)
4134 cocoa_display_init(ds, full_screen);
4136 dumb_display_init(ds);
4140 vga_console = graphic_console_init(ds);
4142 monitor_hd = qemu_chr_open(monitor_device);
4144 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
4147 monitor_init(monitor_hd, !nographic);
4149 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
4150 if (serial_devices[i][0] != '\0') {
4151 serial_hds[i] = qemu_chr_open(serial_devices[i]);
4152 if (!serial_hds[i]) {
4153 fprintf(stderr, "qemu: could not open serial device '%s'\n",
4157 if (!strcmp(serial_devices[i], "vc"))
4158 qemu_chr_printf(serial_hds[i], "serial%d console\n", i);
4162 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
4163 if (parallel_devices[i][0] != '\0') {
4164 parallel_hds[i] = qemu_chr_open(parallel_devices[i]);
4165 if (!parallel_hds[i]) {
4166 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
4167 parallel_devices[i]);
4170 if (!strcmp(parallel_devices[i], "vc"))
4171 qemu_chr_printf(parallel_hds[i], "parallel%d console\n", i);
4175 /* setup cpu signal handlers for MMU / self modifying code handling */
4176 #if !defined(CONFIG_SOFTMMU)
4178 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
4181 signal_stack = memalign(16, SIGNAL_STACK_SIZE);
4182 stk.ss_sp = signal_stack;
4183 stk.ss_size = SIGNAL_STACK_SIZE;
4186 if (sigaltstack(&stk, NULL) < 0) {
4187 perror("sigaltstack");
4193 struct sigaction act;
4195 sigfillset(&act.sa_mask);
4196 act.sa_flags = SA_SIGINFO;
4197 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
4198 act.sa_flags |= SA_ONSTACK;
4200 act.sa_sigaction = host_segv_handler;
4201 sigaction(SIGSEGV, &act, NULL);
4202 sigaction(SIGBUS, &act, NULL);
4203 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
4204 sigaction(SIGFPE, &act, NULL);
4211 struct sigaction act;
4212 sigfillset(&act.sa_mask);
4214 act.sa_handler = SIG_IGN;
4215 sigaction(SIGPIPE, &act, NULL);
4220 machine->init(ram_size, vga_ram_size, boot_device,
4221 ds, fd_filename, snapshot,
4222 kernel_filename, kernel_cmdline, initrd_filename);
4224 gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
4225 qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
4227 #ifdef CONFIG_GDBSTUB
4229 if (gdbserver_start(gdbstub_port) < 0) {
4230 fprintf(stderr, "Could not open gdbserver socket on port %d\n",
4234 printf("Waiting gdb connection on port %d\n", gdbstub_port);
4239 qemu_loadvm(loadvm);
4242 /* XXX: simplify init */
4244 if (start_emulation) {