* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdarg.h>
-#include <string.h>
-#include <ctype.h>
+#include "vl.h"
+
#include <getopt.h>
-#include <inttypes.h>
#include <unistd.h>
-#include <sys/mman.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
-#include <sys/time.h>
#include <malloc.h>
-#include <termios.h>
-#include <sys/poll.h>
#include <errno.h>
+#include <sys/time.h>
+
+#ifndef _WIN32
+#include <sys/times.h>
#include <sys/wait.h>
#include <pty.h>
-
+#include <termios.h>
+#include <sys/poll.h>
+#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_tun.h>
+#endif
+
+#if defined(CONFIG_SLIRP)
+#include "libslirp.h"
+#endif
+
+#ifdef _WIN32
+#include <sys/timeb.h>
+#include <windows.h>
+#define getopt_long_only getopt_long
+#define memalign(align, size) malloc(size)
+#endif
+
+#ifdef CONFIG_SDL
+/* SDL use the pthreads and they modify sigaction. We don't
+ want that. */
+#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
+extern void __libc_sigaction();
+#define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
+#else
+extern void __sigaction();
+#define sigaction(sig, act, oact) __sigaction(sig, act, oact)
+#endif
+#endif /* CONFIG_SDL */
#include "disas.h"
-#include "vl.h"
+#include "exec-all.h"
+
+//#define DO_TB_FLUSH
#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
#define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
#endif
-#if defined (TARGET_I386)
-#elif defined (TARGET_PPC)
-//#define USE_OPEN_FIRMWARE
-#if !defined (USE_OPEN_FIRMWARE)
-#define KERNEL_LOAD_ADDR 0x01000000
-#define KERNEL_STACK_ADDR 0x01200000
-#else
-#define KERNEL_LOAD_ADDR 0x00000000
-#define KERNEL_STACK_ADDR 0x00400000
-#endif
-#endif
-
-#define GUI_REFRESH_INTERVAL 30
+/* in ms */
+#define GUI_REFRESH_INTERVAL 30
/* XXX: use a two level table to limit memory usage */
#define MAX_IOPORTS 65536
int vga_ram_size;
static DisplayState display_state;
int nographic;
-int term_inited;
int64_t ticks_per_sec;
int boot_device = 'c';
static int ram_size;
int nb_nics;
NetDriverState nd_table[MAX_NICS];
SerialState *serial_console;
+QEMUTimer *gui_timer;
+int vm_running;
+int audio_enabled = 0;
/***********************************************************/
-/* x86 io ports */
+/* x86 ISA bus support */
+
+target_phys_addr_t isa_mem_base = 0;
uint32_t default_ioport_readb(void *opaque, uint32_t address)
{
int load_image(const char *filename, uint8_t *addr)
{
int fd, size;
- fd = open(filename, O_RDONLY);
+ fd = open(filename, O_RDONLY | O_BINARY);
if (fd < 0)
return -1;
size = lseek(fd, 0, SEEK_END);
abort();
}
+/***********************************************************/
+/* timers */
+
#if defined(__powerpc__)
static inline uint32_t get_tbl(void)
int64_t cpu_get_real_ticks(void)
{
int64_t val;
- asm("rdtsc" : "=A" (val));
+ asm volatile ("rdtsc" : "=A" (val));
+ return val;
+}
+
+#elif defined(__x86_64__)
+
+int64_t cpu_get_real_ticks(void)
+{
+ uint32_t low,high;
+ int64_t val;
+ asm volatile("rdtsc" : "=a" (low), "=d" (high));
+ val = high;
+ val <<= 32;
+ val |= low;
return val;
}
#endif
static int64_t cpu_ticks_offset;
-static int64_t cpu_ticks_last;
+static int cpu_ticks_enabled;
-int64_t cpu_get_ticks(void)
+static inline int64_t cpu_get_ticks(void)
{
- return cpu_get_real_ticks() + cpu_ticks_offset;
+ if (!cpu_ticks_enabled) {
+ return cpu_ticks_offset;
+ } else {
+ return cpu_get_real_ticks() + cpu_ticks_offset;
+ }
}
/* enable cpu_get_ticks() */
void cpu_enable_ticks(void)
{
- cpu_ticks_offset = cpu_ticks_last - cpu_get_real_ticks();
+ if (!cpu_ticks_enabled) {
+ cpu_ticks_offset -= cpu_get_real_ticks();
+ cpu_ticks_enabled = 1;
+ }
}
/* disable cpu_get_ticks() : the clock is stopped. You must not call
cpu_get_ticks() after that. */
void cpu_disable_ticks(void)
{
- cpu_ticks_last = cpu_get_ticks();
+ if (cpu_ticks_enabled) {
+ cpu_ticks_offset = cpu_get_ticks();
+ cpu_ticks_enabled = 0;
+ }
}
-int64_t get_clock(void)
+static int64_t get_clock(void)
{
+#ifdef _WIN32
+ struct _timeb tb;
+ _ftime(&tb);
+ return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
+#else
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000LL + tv.tv_usec;
+#endif
}
void cpu_calibrate_ticks(void)
int64_t usec, ticks;
usec = get_clock();
- ticks = cpu_get_ticks();
+ ticks = cpu_get_real_ticks();
+#ifdef _WIN32
+ Sleep(50);
+#else
usleep(50 * 1000);
+#endif
usec = get_clock() - usec;
- ticks = cpu_get_ticks() - ticks;
+ ticks = cpu_get_real_ticks() - ticks;
ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
}
return res.ll;
}
+#define QEMU_TIMER_REALTIME 0
+#define QEMU_TIMER_VIRTUAL 1
+
+struct QEMUClock {
+ int type;
+ /* XXX: add frequency */
+};
+
+struct QEMUTimer {
+ QEMUClock *clock;
+ int64_t expire_time;
+ QEMUTimerCB *cb;
+ void *opaque;
+ struct QEMUTimer *next;
+};
+
+QEMUClock *rt_clock;
+QEMUClock *vm_clock;
+
+static QEMUTimer *active_timers[2];
+#ifdef _WIN32
+static MMRESULT timerID;
+#else
+/* frequency of the times() clock tick */
+static int timer_freq;
+#endif
+
+QEMUClock *qemu_new_clock(int type)
+{
+ QEMUClock *clock;
+ clock = qemu_mallocz(sizeof(QEMUClock));
+ if (!clock)
+ return NULL;
+ clock->type = type;
+ return clock;
+}
+
+QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
+{
+ QEMUTimer *ts;
+
+ ts = qemu_mallocz(sizeof(QEMUTimer));
+ ts->clock = clock;
+ ts->cb = cb;
+ ts->opaque = opaque;
+ return ts;
+}
+
+void qemu_free_timer(QEMUTimer *ts)
+{
+ qemu_free(ts);
+}
+
+/* stop a timer, but do not dealloc it */
+void qemu_del_timer(QEMUTimer *ts)
+{
+ QEMUTimer **pt, *t;
+
+ /* NOTE: this code must be signal safe because
+ qemu_timer_expired() can be called from a signal. */
+ pt = &active_timers[ts->clock->type];
+ for(;;) {
+ t = *pt;
+ if (!t)
+ break;
+ if (t == ts) {
+ *pt = t->next;
+ break;
+ }
+ pt = &t->next;
+ }
+}
+
+/* modify the current timer so that it will be fired when current_time
+ >= expire_time. The corresponding callback will be called. */
+void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
+{
+ QEMUTimer **pt, *t;
+
+ qemu_del_timer(ts);
+
+ /* add the timer in the sorted list */
+ /* NOTE: this code must be signal safe because
+ qemu_timer_expired() can be called from a signal. */
+ pt = &active_timers[ts->clock->type];
+ for(;;) {
+ t = *pt;
+ if (!t)
+ break;
+ if (t->expire_time > expire_time)
+ break;
+ pt = &t->next;
+ }
+ ts->expire_time = expire_time;
+ ts->next = *pt;
+ *pt = ts;
+}
+
+int qemu_timer_pending(QEMUTimer *ts)
+{
+ QEMUTimer *t;
+ for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
+ if (t == ts)
+ return 1;
+ }
+ return 0;
+}
+
+static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
+{
+ if (!timer_head)
+ return 0;
+ return (timer_head->expire_time <= current_time);
+}
+
+static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
+{
+ QEMUTimer *ts;
+
+ for(;;) {
+ ts = *ptimer_head;
+ if (ts->expire_time > current_time)
+ break;
+ /* remove timer from the list before calling the callback */
+ *ptimer_head = ts->next;
+ ts->next = NULL;
+
+ /* run the callback (the timer list can be modified) */
+ ts->cb(ts->opaque);
+ }
+}
+
+int64_t qemu_get_clock(QEMUClock *clock)
+{
+ switch(clock->type) {
+ case QEMU_TIMER_REALTIME:
+#ifdef _WIN32
+ return GetTickCount();
+#else
+ /* XXX: portability among Linux hosts */
+ if (timer_freq == 100) {
+ return times(NULL) * 10;
+ } else {
+ return ((int64_t)times(NULL) * 1000) / timer_freq;
+ }
+#endif
+ default:
+ case QEMU_TIMER_VIRTUAL:
+ return cpu_get_ticks();
+ }
+}
+
+/* save a timer */
+void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
+{
+ uint64_t expire_time;
+
+ if (qemu_timer_pending(ts)) {
+ expire_time = ts->expire_time;
+ } else {
+ expire_time = -1;
+ }
+ qemu_put_be64(f, expire_time);
+}
+
+void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
+{
+ uint64_t expire_time;
+
+ expire_time = qemu_get_be64(f);
+ if (expire_time != -1) {
+ qemu_mod_timer(ts, expire_time);
+ } else {
+ qemu_del_timer(ts);
+ }
+}
+
+static void timer_save(QEMUFile *f, void *opaque)
+{
+ if (cpu_ticks_enabled) {
+ hw_error("cannot save state if virtual timers are running");
+ }
+ qemu_put_be64s(f, &cpu_ticks_offset);
+ qemu_put_be64s(f, &ticks_per_sec);
+}
+
+static int timer_load(QEMUFile *f, void *opaque, int version_id)
+{
+ if (version_id != 1)
+ return -EINVAL;
+ if (cpu_ticks_enabled) {
+ return -EINVAL;
+ }
+ qemu_get_be64s(f, &cpu_ticks_offset);
+ qemu_get_be64s(f, &ticks_per_sec);
+ return 0;
+}
+
+#ifdef _WIN32
+void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
+ DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
+#else
+static void host_alarm_handler(int host_signum)
+#endif
+{
+ if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
+ qemu_get_clock(vm_clock)) ||
+ qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
+ qemu_get_clock(rt_clock))) {
+ /* stop the cpu because a timer occured */
+ cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
+ }
+}
+
+static void init_timers(void)
+{
+ rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
+ vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
+
+#ifdef _WIN32
+ {
+ int count=0;
+ timerID = timeSetEvent(10, // interval (ms)
+ 0, // resolution
+ host_alarm_handler, // function
+ (DWORD)&count, // user parameter
+ TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
+ if( !timerID ) {
+ perror("failed timer alarm");
+ exit(1);
+ }
+ }
+ pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
+#else
+ {
+ struct sigaction act;
+ struct itimerval itv;
+
+ /* get times() syscall frequency */
+ timer_freq = sysconf(_SC_CLK_TCK);
+
+ /* timer signal */
+ sigfillset(&act.sa_mask);
+ act.sa_flags = 0;
+#if defined (TARGET_I386) && defined(USE_CODE_COPY)
+ act.sa_flags |= SA_ONSTACK;
+#endif
+ act.sa_handler = host_alarm_handler;
+ sigaction(SIGALRM, &act, NULL);
+
+ itv.it_interval.tv_sec = 0;
+ itv.it_interval.tv_usec = 1000;
+ itv.it_value.tv_sec = 0;
+ itv.it_value.tv_usec = 10 * 1000;
+ setitimer(ITIMER_REAL, &itv, NULL);
+ /* we probe the tick duration of the kernel to inform the user if
+ the emulated kernel requested a too high timer frequency */
+ getitimer(ITIMER_REAL, &itv);
+ pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) /
+ 1000000;
+ }
+#endif
+}
+
+void quit_timers(void)
+{
+#ifdef _WIN32
+ timeKillEvent(timerID);
+#endif
+}
+
/***********************************************************/
/* serial device */
+#ifdef _WIN32
+
+int serial_open_device(void)
+{
+ return -1;
+}
+
+#else
+
int serial_open_device(void)
{
char slave_name[1024];
}
}
+#endif
+
/***********************************************************/
-/* Linux network device redirector */
+/* Linux network device redirectors */
+
+void hex_dump(FILE *f, const uint8_t *buf, int size)
+{
+ int len, i, j, c;
+
+ for(i=0;i<size;i+=16) {
+ len = size - i;
+ if (len > 16)
+ len = 16;
+ fprintf(f, "%08x ", i);
+ for(j=0;j<16;j++) {
+ if (j < len)
+ fprintf(f, " %02x", buf[i+j]);
+ else
+ fprintf(f, " ");
+ }
+ fprintf(f, " ");
+ for(j=0;j<len;j++) {
+ c = buf[i+j];
+ if (c < ' ' || c > '~')
+ c = '.';
+ fprintf(f, "%c", c);
+ }
+ fprintf(f, "\n");
+ }
+}
+
+void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
+{
+ nd->send_packet(nd, buf, size);
+}
+
+void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
+ IOReadHandler *fd_read, void *opaque)
+{
+ nd->add_read_packet(nd, fd_can_read, fd_read, opaque);
+}
+
+/* dummy network adapter */
+
+static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
+{
+}
+
+static void dummy_add_read_packet(NetDriverState *nd,
+ IOCanRWHandler *fd_can_read,
+ IOReadHandler *fd_read, void *opaque)
+{
+}
+
+static int net_dummy_init(NetDriverState *nd)
+{
+ nd->send_packet = dummy_send_packet;
+ nd->add_read_packet = dummy_add_read_packet;
+ pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy");
+ return 0;
+}
+
+#if defined(CONFIG_SLIRP)
+
+/* slirp network adapter */
+
+static void *slirp_fd_opaque;
+static IOCanRWHandler *slirp_fd_can_read;
+static IOReadHandler *slirp_fd_read;
+static int slirp_inited;
+
+int slirp_can_output(void)
+{
+ return slirp_fd_can_read(slirp_fd_opaque);
+}
+
+void slirp_output(const uint8_t *pkt, int pkt_len)
+{
+#if 0
+ printf("output:\n");
+ hex_dump(stdout, pkt, pkt_len);
+#endif
+ slirp_fd_read(slirp_fd_opaque, pkt, pkt_len);
+}
+
+static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
+{
+#if 0
+ printf("input:\n");
+ hex_dump(stdout, buf, size);
+#endif
+ slirp_input(buf, size);
+}
+
+static void slirp_add_read_packet(NetDriverState *nd,
+ IOCanRWHandler *fd_can_read,
+ IOReadHandler *fd_read, void *opaque)
+{
+ slirp_fd_opaque = opaque;
+ slirp_fd_can_read = fd_can_read;
+ slirp_fd_read = fd_read;
+}
+
+static int net_slirp_init(NetDriverState *nd)
+{
+ if (!slirp_inited) {
+ slirp_inited = 1;
+ slirp_init();
+ }
+ nd->send_packet = slirp_send_packet;
+ nd->add_read_packet = slirp_add_read_packet;
+ pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp");
+ return 0;
+}
+
+#endif /* CONFIG_SLIRP */
+
+#if !defined(_WIN32)
static int tun_open(char *ifname, int ifname_size)
{
return fd;
}
-static int net_init(void)
+static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
+{
+ write(nd->fd, buf, size);
+}
+
+static void tun_add_read_packet(NetDriverState *nd,
+ IOCanRWHandler *fd_can_read,
+ IOReadHandler *fd_read, void *opaque)
+{
+ qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque);
+}
+
+static int net_tun_init(NetDriverState *nd)
{
- int pid, status, launch_script, i;
- NetDriverState *nd;
- char *args[MAX_NICS + 2];
+ int pid, status;
+ char *args[3];
char **parg;
- launch_script = 0;
- for(i = 0; i < nb_nics; i++) {
- nd = &nd_table[i];
- if (nd->fd < 0) {
- nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
- if (nd->fd >= 0)
- launch_script = 1;
+ nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
+ if (nd->fd < 0)
+ return -1;
+
+ /* try to launch network init script */
+ pid = fork();
+ if (pid >= 0) {
+ if (pid == 0) {
+ parg = args;
+ *parg++ = network_script;
+ *parg++ = nd->ifname;
+ *parg++ = NULL;
+ execv(network_script, args);
+ exit(1);
+ }
+ while (waitpid(pid, &status, 0) != pid);
+ if (!WIFEXITED(status) ||
+ WEXITSTATUS(status) != 0) {
+ fprintf(stderr, "%s: could not launch network script\n",
+ network_script);
}
}
+ nd->send_packet = tun_send_packet;
+ nd->add_read_packet = tun_add_read_packet;
+ return 0;
+}
- if (launch_script) {
- /* try to launch network init script */
- pid = fork();
- if (pid >= 0) {
- if (pid == 0) {
- parg = args;
- *parg++ = network_script;
- for(i = 0; i < nb_nics; i++) {
- nd = &nd_table[i];
- if (nd->fd >= 0) {
- *parg++ = nd->ifname;
- }
- }
- *parg++ = NULL;
- execv(network_script, args);
- exit(1);
- }
- while (waitpid(pid, &status, 0) != pid);
- if (!WIFEXITED(status) ||
- WEXITSTATUS(status) != 0) {
- fprintf(stderr, "%s: could not launch network script\n",
- network_script);
- }
+static int net_fd_init(NetDriverState *nd, int fd)
+{
+ nd->fd = fd;
+ nd->send_packet = tun_send_packet;
+ nd->add_read_packet = tun_add_read_packet;
+ pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
+ return 0;
+}
+
+#endif /* !_WIN32 */
+
+/***********************************************************/
+/* dumb display */
+
+#ifdef _WIN32
+
+static void term_exit(void)
+{
+}
+
+static void term_init(void)
+{
+}
+
+#else
+
+/* init terminal so that we can grab keys */
+static struct termios oldtty;
+
+static void term_exit(void)
+{
+ tcsetattr (0, TCSANOW, &oldtty);
+}
+
+static void term_init(void)
+{
+ struct termios tty;
+
+ tcgetattr (0, &tty);
+ oldtty = tty;
+
+ tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
+ |INLCR|IGNCR|ICRNL|IXON);
+ tty.c_oflag |= OPOST;
+ tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
+ /* if graphical mode, we allow Ctrl-C handling */
+ if (nographic)
+ tty.c_lflag &= ~ISIG;
+ tty.c_cflag &= ~(CSIZE|PARENB);
+ tty.c_cflag |= CS8;
+ tty.c_cc[VMIN] = 1;
+ tty.c_cc[VTIME] = 0;
+
+ tcsetattr (0, TCSANOW, &tty);
+
+ atexit(term_exit);
+
+ fcntl(0, F_SETFL, O_NONBLOCK);
+}
+
+#endif
+
+static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
+{
+}
+
+static void dumb_resize(DisplayState *ds, int w, int h)
+{
+}
+
+static void dumb_refresh(DisplayState *ds)
+{
+ vga_update_display();
+}
+
+void dumb_display_init(DisplayState *ds)
+{
+ ds->data = NULL;
+ ds->linesize = 0;
+ ds->depth = 0;
+ ds->dpy_update = dumb_update;
+ ds->dpy_resize = dumb_resize;
+ ds->dpy_refresh = dumb_refresh;
+}
+
+#if !defined(CONFIG_SOFTMMU)
+/***********************************************************/
+/* cpu signal handler */
+static void host_segv_handler(int host_signum, siginfo_t *info,
+ void *puc)
+{
+ if (cpu_signal_handler(host_signum, info, puc))
+ return;
+ term_exit();
+ abort();
+}
+#endif
+
+/***********************************************************/
+/* I/O handling */
+
+#define MAX_IO_HANDLERS 64
+
+typedef struct IOHandlerRecord {
+ int fd;
+ IOCanRWHandler *fd_can_read;
+ IOReadHandler *fd_read;
+ void *opaque;
+ /* temporary data */
+ struct pollfd *ufd;
+ int max_size;
+ struct IOHandlerRecord *next;
+} IOHandlerRecord;
+
+static IOHandlerRecord *first_io_handler;
+
+int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
+ IOReadHandler *fd_read, void *opaque)
+{
+ IOHandlerRecord *ioh;
+
+ ioh = qemu_mallocz(sizeof(IOHandlerRecord));
+ if (!ioh)
+ return -1;
+ ioh->fd = fd;
+ ioh->fd_can_read = fd_can_read;
+ ioh->fd_read = fd_read;
+ ioh->opaque = opaque;
+ ioh->next = first_io_handler;
+ first_io_handler = ioh;
+ return 0;
+}
+
+void qemu_del_fd_read_handler(int fd)
+{
+ IOHandlerRecord **pioh, *ioh;
+
+ pioh = &first_io_handler;
+ for(;;) {
+ ioh = *pioh;
+ if (ioh == NULL)
+ break;
+ if (ioh->fd == fd) {
+ *pioh = ioh->next;
+ break;
}
+ pioh = &ioh->next;
+ }
+}
+
+/***********************************************************/
+/* savevm/loadvm support */
+
+void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
+{
+ fwrite(buf, 1, size, f);
+}
+
+void qemu_put_byte(QEMUFile *f, int v)
+{
+ fputc(v, f);
+}
+
+void qemu_put_be16(QEMUFile *f, unsigned int v)
+{
+ qemu_put_byte(f, v >> 8);
+ qemu_put_byte(f, v);
+}
+
+void qemu_put_be32(QEMUFile *f, unsigned int v)
+{
+ qemu_put_byte(f, v >> 24);
+ qemu_put_byte(f, v >> 16);
+ qemu_put_byte(f, v >> 8);
+ qemu_put_byte(f, v);
+}
+
+void qemu_put_be64(QEMUFile *f, uint64_t v)
+{
+ qemu_put_be32(f, v >> 32);
+ qemu_put_be32(f, v);
+}
+
+int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
+{
+ return fread(buf, 1, size, f);
+}
+
+int qemu_get_byte(QEMUFile *f)
+{
+ int v;
+ v = fgetc(f);
+ if (v == EOF)
+ return 0;
+ else
+ return v;
+}
+
+unsigned int qemu_get_be16(QEMUFile *f)
+{
+ unsigned int v;
+ v = qemu_get_byte(f) << 8;
+ v |= qemu_get_byte(f);
+ return v;
+}
+
+unsigned int qemu_get_be32(QEMUFile *f)
+{
+ unsigned int v;
+ v = qemu_get_byte(f) << 24;
+ v |= qemu_get_byte(f) << 16;
+ v |= qemu_get_byte(f) << 8;
+ v |= qemu_get_byte(f);
+ return v;
+}
+
+uint64_t qemu_get_be64(QEMUFile *f)
+{
+ uint64_t v;
+ v = (uint64_t)qemu_get_be32(f) << 32;
+ v |= qemu_get_be32(f);
+ return v;
+}
+
+int64_t qemu_ftell(QEMUFile *f)
+{
+ return ftell(f);
+}
+
+int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
+{
+ if (fseek(f, pos, whence) < 0)
+ return -1;
+ return ftell(f);
+}
+
+typedef struct SaveStateEntry {
+ char idstr[256];
+ int instance_id;
+ int version_id;
+ SaveStateHandler *save_state;
+ LoadStateHandler *load_state;
+ void *opaque;
+ struct SaveStateEntry *next;
+} SaveStateEntry;
+
+static SaveStateEntry *first_se;
+
+int register_savevm(const char *idstr,
+ int instance_id,
+ int version_id,
+ SaveStateHandler *save_state,
+ LoadStateHandler *load_state,
+ void *opaque)
+{
+ SaveStateEntry *se, **pse;
+
+ se = qemu_malloc(sizeof(SaveStateEntry));
+ if (!se)
+ return -1;
+ pstrcpy(se->idstr, sizeof(se->idstr), idstr);
+ se->instance_id = instance_id;
+ se->version_id = version_id;
+ se->save_state = save_state;
+ se->load_state = load_state;
+ se->opaque = opaque;
+ se->next = NULL;
+
+ /* add at the end of list */
+ pse = &first_se;
+ while (*pse != NULL)
+ pse = &(*pse)->next;
+ *pse = se;
+ return 0;
+}
+
+#define QEMU_VM_FILE_MAGIC 0x5145564d
+#define QEMU_VM_FILE_VERSION 0x00000001
+
+int qemu_savevm(const char *filename)
+{
+ SaveStateEntry *se;
+ QEMUFile *f;
+ int len, len_pos, cur_pos, saved_vm_running, ret;
+
+ saved_vm_running = vm_running;
+ vm_stop(0);
+
+ f = fopen(filename, "wb");
+ if (!f) {
+ ret = -1;
+ goto the_end;
+ }
+
+ qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
+ qemu_put_be32(f, QEMU_VM_FILE_VERSION);
+
+ for(se = first_se; se != NULL; se = se->next) {
+ /* ID string */
+ len = strlen(se->idstr);
+ qemu_put_byte(f, len);
+ qemu_put_buffer(f, se->idstr, len);
+
+ qemu_put_be32(f, se->instance_id);
+ qemu_put_be32(f, se->version_id);
+
+ /* record size: filled later */
+ len_pos = ftell(f);
+ qemu_put_be32(f, 0);
+
+ se->save_state(f, se->opaque);
+
+ /* fill record size */
+ cur_pos = ftell(f);
+ len = ftell(f) - len_pos - 4;
+ fseek(f, len_pos, SEEK_SET);
+ qemu_put_be32(f, len);
+ fseek(f, cur_pos, SEEK_SET);
+ }
+
+ fclose(f);
+ ret = 0;
+ the_end:
+ if (saved_vm_running)
+ vm_start();
+ return ret;
+}
+
+static SaveStateEntry *find_se(const char *idstr, int instance_id)
+{
+ SaveStateEntry *se;
+
+ for(se = first_se; se != NULL; se = se->next) {
+ if (!strcmp(se->idstr, idstr) &&
+ instance_id == se->instance_id)
+ return se;
+ }
+ return NULL;
+}
+
+int qemu_loadvm(const char *filename)
+{
+ SaveStateEntry *se;
+ QEMUFile *f;
+ int len, cur_pos, ret, instance_id, record_len, version_id;
+ int saved_vm_running;
+ unsigned int v;
+ char idstr[256];
+
+ saved_vm_running = vm_running;
+ vm_stop(0);
+
+ f = fopen(filename, "rb");
+ if (!f) {
+ ret = -1;
+ goto the_end;
}
- return 0;
-}
-void net_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
-{
-#ifdef DEBUG_NE2000
- printf("NE2000: sending packet size=%d\n", size);
+ v = qemu_get_be32(f);
+ if (v != QEMU_VM_FILE_MAGIC)
+ goto fail;
+ v = qemu_get_be32(f);
+ if (v != QEMU_VM_FILE_VERSION) {
+ fail:
+ fclose(f);
+ ret = -1;
+ goto the_end;
+ }
+ for(;;) {
+#if defined (DO_TB_FLUSH)
+ tb_flush(global_env);
#endif
- write(nd->fd, buf, size);
+ len = qemu_get_byte(f);
+ if (feof(f))
+ break;
+ qemu_get_buffer(f, idstr, len);
+ idstr[len] = '\0';
+ instance_id = qemu_get_be32(f);
+ version_id = qemu_get_be32(f);
+ record_len = qemu_get_be32(f);
+#if 0
+ printf("idstr=%s instance=0x%x version=%d len=%d\n",
+ idstr, instance_id, version_id, record_len);
+#endif
+ cur_pos = ftell(f);
+ se = find_se(idstr, instance_id);
+ if (!se) {
+ fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
+ instance_id, idstr);
+ } else {
+ ret = se->load_state(f, se->opaque, version_id);
+ if (ret < 0) {
+ fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
+ instance_id, idstr);
+ }
+ }
+ /* always seek to exact end of record */
+ qemu_fseek(f, cur_pos + record_len, SEEK_SET);
+ }
+ fclose(f);
+ ret = 0;
+ the_end:
+ if (saved_vm_running)
+ vm_start();
+ return ret;
}
/***********************************************************/
-/* dumb display */
+/* cpu save/restore */
-/* init terminal so that we can grab keys */
-static struct termios oldtty;
+#if defined(TARGET_I386)
-static void term_exit(void)
+static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
{
- tcsetattr (0, TCSANOW, &oldtty);
+ qemu_put_be32(f, (uint32_t)dt->base);
+ qemu_put_be32(f, dt->limit);
+ qemu_put_be32(f, dt->flags);
}
-static void term_init(void)
+static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
{
- struct termios tty;
+ dt->base = (uint8_t *)qemu_get_be32(f);
+ dt->limit = qemu_get_be32(f);
+ dt->flags = qemu_get_be32(f);
+}
- tcgetattr (0, &tty);
- oldtty = tty;
+void cpu_save(QEMUFile *f, void *opaque)
+{
+ CPUState *env = opaque;
+ uint16_t fptag, fpus, fpuc;
+ uint32_t hflags;
+ int i;
- tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
- |INLCR|IGNCR|ICRNL|IXON);
- tty.c_oflag |= OPOST;
- tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
- /* if graphical mode, we allow Ctrl-C handling */
- if (nographic)
- tty.c_lflag &= ~ISIG;
- tty.c_cflag &= ~(CSIZE|PARENB);
- tty.c_cflag |= CS8;
- tty.c_cc[VMIN] = 1;
- tty.c_cc[VTIME] = 0;
+ for(i = 0; i < 8; i++)
+ qemu_put_be32s(f, &env->regs[i]);
+ qemu_put_be32s(f, &env->eip);
+ qemu_put_be32s(f, &env->eflags);
+ qemu_put_be32s(f, &env->eflags);
+ hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
+ qemu_put_be32s(f, &hflags);
- tcsetattr (0, TCSANOW, &tty);
+ /* FPU */
+ fpuc = env->fpuc;
+ fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
+ fptag = 0;
+ for (i=7; i>=0; i--) {
+ fptag <<= 2;
+ if (env->fptags[i]) {
+ fptag |= 3;
+ }
+ }
+
+ qemu_put_be16s(f, &fpuc);
+ qemu_put_be16s(f, &fpus);
+ qemu_put_be16s(f, &fptag);
+
+ for(i = 0; i < 8; i++) {
+ uint64_t mant;
+ uint16_t exp;
+ cpu_get_fp80(&mant, &exp, env->fpregs[i]);
+ qemu_put_be64(f, mant);
+ qemu_put_be16(f, exp);
+ }
- atexit(term_exit);
+ for(i = 0; i < 6; i++)
+ cpu_put_seg(f, &env->segs[i]);
+ cpu_put_seg(f, &env->ldt);
+ cpu_put_seg(f, &env->tr);
+ cpu_put_seg(f, &env->gdt);
+ cpu_put_seg(f, &env->idt);
+
+ qemu_put_be32s(f, &env->sysenter_cs);
+ qemu_put_be32s(f, &env->sysenter_esp);
+ qemu_put_be32s(f, &env->sysenter_eip);
+
+ qemu_put_be32s(f, &env->cr[0]);
+ qemu_put_be32s(f, &env->cr[2]);
+ qemu_put_be32s(f, &env->cr[3]);
+ qemu_put_be32s(f, &env->cr[4]);
+
+ for(i = 0; i < 8; i++)
+ qemu_put_be32s(f, &env->dr[i]);
- fcntl(0, F_SETFL, O_NONBLOCK);
+ /* MMU */
+ qemu_put_be32s(f, &env->a20_mask);
}
-static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
+int cpu_load(QEMUFile *f, void *opaque, int version_id)
{
-}
+ CPUState *env = opaque;
+ int i;
+ uint32_t hflags;
+ uint16_t fpus, fpuc, fptag;
+
+ if (version_id != 1)
+ return -EINVAL;
+ for(i = 0; i < 8; i++)
+ qemu_get_be32s(f, &env->regs[i]);
+ qemu_get_be32s(f, &env->eip);
+ qemu_get_be32s(f, &env->eflags);
+ qemu_get_be32s(f, &env->eflags);
+ qemu_get_be32s(f, &hflags);
+
+ qemu_get_be16s(f, &fpuc);
+ qemu_get_be16s(f, &fpus);
+ qemu_get_be16s(f, &fptag);
+
+ for(i = 0; i < 8; i++) {
+ uint64_t mant;
+ uint16_t exp;
+ mant = qemu_get_be64(f);
+ exp = qemu_get_be16(f);
+ env->fpregs[i] = cpu_set_fp80(mant, exp);
+ }
-static void dumb_resize(DisplayState *ds, int w, int h)
-{
-}
+ env->fpuc = fpuc;
+ env->fpstt = (fpus >> 11) & 7;
+ env->fpus = fpus & ~0x3800;
+ for(i = 0; i < 8; i++) {
+ env->fptags[i] = ((fptag & 3) == 3);
+ fptag >>= 2;
+ }
+
+ for(i = 0; i < 6; i++)
+ cpu_get_seg(f, &env->segs[i]);
+ cpu_get_seg(f, &env->ldt);
+ cpu_get_seg(f, &env->tr);
+ cpu_get_seg(f, &env->gdt);
+ cpu_get_seg(f, &env->idt);
+
+ qemu_get_be32s(f, &env->sysenter_cs);
+ qemu_get_be32s(f, &env->sysenter_esp);
+ qemu_get_be32s(f, &env->sysenter_eip);
+
+ qemu_get_be32s(f, &env->cr[0]);
+ qemu_get_be32s(f, &env->cr[2]);
+ qemu_get_be32s(f, &env->cr[3]);
+ qemu_get_be32s(f, &env->cr[4]);
+
+ for(i = 0; i < 8; i++)
+ qemu_get_be32s(f, &env->dr[i]);
-static void dumb_refresh(DisplayState *ds)
-{
- vga_update_display();
+ /* MMU */
+ qemu_get_be32s(f, &env->a20_mask);
+
+ /* XXX: compute hflags from scratch, except for CPL and IIF */
+ env->hflags = hflags;
+ tlb_flush(env, 1);
+ return 0;
}
-void dumb_display_init(DisplayState *ds)
+#elif defined(TARGET_PPC)
+void cpu_save(QEMUFile *f, void *opaque)
{
- ds->data = NULL;
- ds->linesize = 0;
- ds->depth = 0;
- ds->dpy_update = dumb_update;
- ds->dpy_resize = dumb_resize;
- ds->dpy_refresh = dumb_refresh;
}
-#if !defined(CONFIG_SOFTMMU)
-/***********************************************************/
-/* cpu signal handler */
-static void host_segv_handler(int host_signum, siginfo_t *info,
- void *puc)
+int cpu_load(QEMUFile *f, void *opaque, int version_id)
{
- if (cpu_signal_handler(host_signum, info, puc))
- return;
- term_exit();
- abort();
+ return 0;
}
+#else
+
+#warning No CPU save/restore functions
+
#endif
-static int timer_irq_pending;
-static int timer_irq_count;
+/***********************************************************/
+/* ram save/restore */
+
+/* we just avoid storing empty pages */
+static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
+{
+ int i, v;
-static int timer_ms;
-static int gui_refresh_pending, gui_refresh_count;
+ v = buf[0];
+ for(i = 1; i < len; i++) {
+ if (buf[i] != v)
+ goto normal_save;
+ }
+ qemu_put_byte(f, 1);
+ qemu_put_byte(f, v);
+ return;
+ normal_save:
+ qemu_put_byte(f, 0);
+ qemu_put_buffer(f, buf, len);
+}
-static void host_alarm_handler(int host_signum, siginfo_t *info,
- void *puc)
+static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
{
- /* NOTE: since usually the OS asks a 100 Hz clock, there can be
- some drift between cpu_get_ticks() and the interrupt time. So
- we queue some interrupts to avoid missing some */
- timer_irq_count += pit_get_out_edges(&pit_channels[0]);
- if (timer_irq_count) {
- if (timer_irq_count > 2)
- timer_irq_count = 2;
- timer_irq_count--;
- timer_irq_pending = 1;
+ int v;
+
+ v = qemu_get_byte(f);
+ switch(v) {
+ case 0:
+ if (qemu_get_buffer(f, buf, len) != len)
+ return -EIO;
+ break;
+ case 1:
+ v = qemu_get_byte(f);
+ memset(buf, v, len);
+ break;
+ default:
+ return -EINVAL;
}
- gui_refresh_count += timer_ms;
- if (gui_refresh_count >= GUI_REFRESH_INTERVAL) {
- gui_refresh_count = 0;
- gui_refresh_pending = 1;
+ return 0;
+}
+
+static void ram_save(QEMUFile *f, void *opaque)
+{
+ int i;
+ qemu_put_be32(f, phys_ram_size);
+ for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
+ ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
}
+}
- if (gui_refresh_pending || timer_irq_pending) {
- /* just exit from the cpu to have a chance to handle timers */
- cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
+static int ram_load(QEMUFile *f, void *opaque, int version_id)
+{
+ int i, ret;
+
+ if (version_id != 1)
+ return -EINVAL;
+ if (qemu_get_be32(f) != phys_ram_size)
+ return -EINVAL;
+ for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
+ ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
+ if (ret)
+ return ret;
}
+ return 0;
}
-#define MAX_IO_HANDLERS 64
+/***********************************************************/
+/* main execution loop */
-typedef struct IOHandlerRecord {
- int fd;
- IOCanRWHandler *fd_can_read;
- IOReadHandler *fd_read;
- void *opaque;
- /* temporary data */
- struct pollfd *ufd;
- int max_size;
-} IOHandlerRecord;
+void gui_update(void *opaque)
+{
+ display_state.dpy_refresh(&display_state);
+ qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
+}
-static IOHandlerRecord io_handlers[MAX_IO_HANDLERS];
-static int nb_io_handlers = 0;
+/* XXX: support several handlers */
+VMStopHandler *vm_stop_cb;
+VMStopHandler *vm_stop_opaque;
-int add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
- IOReadHandler *fd_read, void *opaque)
+int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
{
- IOHandlerRecord *ioh;
-
- if (nb_io_handlers >= MAX_IO_HANDLERS)
- return -1;
- ioh = &io_handlers[nb_io_handlers];
- ioh->fd = fd;
- ioh->fd_can_read = fd_can_read;
- ioh->fd_read = fd_read;
- ioh->opaque = opaque;
- nb_io_handlers++;
+ vm_stop_cb = cb;
+ vm_stop_opaque = opaque;
return 0;
}
-/* main execution loop */
+void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
+{
+ vm_stop_cb = NULL;
+}
+
+void vm_start(void)
+{
+ if (!vm_running) {
+ cpu_enable_ticks();
+ vm_running = 1;
+ }
+}
-CPUState *cpu_gdbstub_get_env(void *opaque)
+void vm_stop(int reason)
{
- return global_env;
+ if (vm_running) {
+ cpu_disable_ticks();
+ vm_running = 0;
+ if (reason != 0) {
+ if (vm_stop_cb) {
+ vm_stop_cb(vm_stop_opaque, reason);
+ }
+ }
+ }
}
-int main_loop(void *opaque)
+int main_loop(void)
{
- struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf, *gdb_ufd;
- int ret, n, timeout, serial_ok, max_size, i;
+#ifndef _WIN32
+ struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
+ IOHandlerRecord *ioh, *ioh_next;
uint8_t buf[4096];
- IOHandlerRecord *ioh;
+ int n, max_size;
+#endif
+ int ret, timeout;
CPUState *env = global_env;
- if (!term_inited) {
- /* initialize terminal only there so that the user has a
- chance to stop QEMU with Ctrl-C before the gdb connection
- is launched */
- term_inited = 1;
- term_init();
- }
-
- serial_ok = 1;
- cpu_enable_ticks();
for(;;) {
-#if defined (DO_TB_FLUSH)
- tb_flush();
-#endif
- ret = cpu_exec(env);
- if (reset_requested) {
- ret = EXCP_INTERRUPT;
- break;
- }
- if (ret == EXCP_DEBUG) {
- ret = EXCP_DEBUG;
- break;
- }
- /* if hlt instruction, we wait until the next IRQ */
- if (ret == EXCP_HLT)
+ if (vm_running) {
+ ret = cpu_exec(env);
+ if (reset_requested) {
+ ret = EXCP_INTERRUPT;
+ break;
+ }
+ if (ret == EXCP_DEBUG) {
+ vm_stop(EXCP_DEBUG);
+ }
+ /* if hlt instruction, we wait until the next IRQ */
+ /* XXX: use timeout computed from timers */
+ if (ret == EXCP_HLT)
+ timeout = 10;
+ else
+ timeout = 0;
+ } else {
timeout = 10;
- else
- timeout = 0;
+ }
+
+#ifdef _WIN32
+ if (timeout > 0)
+ Sleep(timeout);
+#else
/* poll any events */
+ /* XXX: separate device handlers from system ones */
pf = ufds;
- ioh = io_handlers;
- for(i = 0; i < nb_io_handlers; i++) {
- max_size = ioh->fd_can_read(ioh->opaque);
- if (max_size > 0) {
- if (max_size > sizeof(buf))
- max_size = sizeof(buf);
+ for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
+ if (!ioh->fd_can_read) {
+ max_size = 0;
pf->fd = ioh->fd;
pf->events = POLLIN;
ioh->ufd = pf;
pf++;
} else {
- ioh->ufd = NULL;
+ max_size = ioh->fd_can_read(ioh->opaque);
+ if (max_size > 0) {
+ if (max_size > sizeof(buf))
+ max_size = sizeof(buf);
+ pf->fd = ioh->fd;
+ pf->events = POLLIN;
+ ioh->ufd = pf;
+ pf++;
+ } else {
+ ioh->ufd = NULL;
+ }
}
ioh->max_size = max_size;
- ioh++;
}
-
- gdb_ufd = NULL;
- if (gdbstub_fd > 0) {
- gdb_ufd = pf;
- pf->fd = gdbstub_fd;
- pf->events = POLLIN;
- pf++;
- }
-
+
ret = poll(ufds, pf - ufds, timeout);
if (ret > 0) {
- ioh = io_handlers;
- for(i = 0; i < nb_io_handlers; i++) {
+ /* XXX: better handling of removal */
+ for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
+ ioh_next = ioh->next;
pf = ioh->ufd;
if (pf) {
- n = read(ioh->fd, buf, ioh->max_size);
- if (n > 0) {
- ioh->fd_read(ioh->opaque, buf, n);
+ if (pf->revents & POLLIN) {
+ if (ioh->max_size == 0) {
+ /* just a read event */
+ ioh->fd_read(ioh->opaque, NULL, 0);
+ } else {
+ n = read(ioh->fd, buf, ioh->max_size);
+ if (n >= 0) {
+ ioh->fd_read(ioh->opaque, buf, n);
+ } else if (errno != -EAGAIN) {
+ ioh->fd_read(ioh->opaque, NULL, -errno);
+ }
+ }
}
}
- ioh++;
- }
- if (gdb_ufd && (gdb_ufd->revents & POLLIN)) {
- uint8_t buf[1];
- /* stop emulation if requested by gdb */
- n = read(gdbstub_fd, buf, 1);
- if (n == 1) {
- ret = EXCP_INTERRUPT;
- break;
- }
}
}
- /* timer IRQ */
- if (timer_irq_pending) {
-#if defined (TARGET_I386)
- pic_set_irq(0, 1);
- pic_set_irq(0, 0);
- timer_irq_pending = 0;
- rtc_timer();
-#endif
+#if defined(CONFIG_SLIRP)
+ /* XXX: merge with poll() */
+ if (slirp_inited) {
+ fd_set rfds, wfds, xfds;
+ int nfds;
+ struct timeval tv;
+
+ nfds = -1;
+ FD_ZERO(&rfds);
+ FD_ZERO(&wfds);
+ FD_ZERO(&xfds);
+ slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
+ if (ret >= 0) {
+ slirp_select_poll(&rfds, &wfds, &xfds);
+ }
}
- /* XXX: add explicit timer */
- SB16_run();
+#endif
- /* run dma transfers, if any */
- DMA_run();
+#endif
- /* VGA */
- if (gui_refresh_pending) {
- display_state.dpy_refresh(&display_state);
- gui_refresh_pending = 0;
+ if (vm_running) {
+ qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
+ qemu_get_clock(vm_clock));
+
+ if (audio_enabled) {
+ /* XXX: add explicit timer */
+ SB16_run();
+ }
+
+ /* run dma transfers, if any */
+ DMA_run();
}
+
+ /* real time timers */
+ qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
+ qemu_get_clock(rt_clock));
}
cpu_disable_ticks();
return ret;
void help(void)
{
- printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
+ printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n"
"usage: %s [options] [disk_image]\n"
"\n"
"'disk_image' is a raw hard image image for IDE hard disk 0\n"
"-snapshot write to temporary files instead of disk image files\n"
"-m megs set virtual RAM size to megs MB\n"
"-nographic disable graphical output and redirect serial I/Os to console\n"
+ "-enable-audio enable audio support\n"
"\n"
"Network options:\n"
- "-n script set network init script [default=%s]\n"
- "-nics n simulate 'n' network interfaces [default=1]\n"
- "-tun-fd fd0[,...] use these fds as already opened tap/tun interfaces\n"
+ "-nics n simulate 'n' network cards [default=1]\n"
+ "-macaddr addr set the mac address of the first interface\n"
+ "-n script set tap/tun network init script [default=%s]\n"
+ "-tun-fd fd use this fd as already opened tap/tun interface\n"
+#ifdef CONFIG_SLIRP
+ "-user-net use user mode network stack [default if no tap/tun script]\n"
+#endif
+ "-dummy-net use dummy network stack\n"
"\n"
"Linux boot specific:\n"
"-kernel bzImage use 'bzImage' as kernel image\n"
{ "fdb", 1, NULL, 0, },
{ "no-code-copy", 0, NULL, 0 },
{ "nics", 1, NULL, 0 },
+ { "macaddr", 1, NULL, 0 },
+ { "user-net", 0, NULL, 0 },
+ { "dummy-net", 0, NULL, 0 },
+ { "enable-audio", 0, NULL, 0 },
{ NULL, 0, NULL, 0 },
};
-#ifdef CONFIG_SDL
-/* SDL use the pthreads and they modify sigaction. We don't
- want that. */
-#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
-extern void __libc_sigaction();
-#define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
-#else
-extern void __sigaction();
-#define sigaction(sig, act, oact) __sigaction(sig, act, oact)
-#endif
-#endif /* CONFIG_SDL */
-
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
/* this stack is only used during signal handling */
#endif
+#define NET_IF_TUN 0
+#define NET_IF_USER 1
+#define NET_IF_DUMMY 2
+
int main(int argc, char **argv)
{
- int c, i, use_gdbstub, gdbstub_port, long_index, has_cdrom;
+#ifdef CONFIG_GDBSTUB
+ int use_gdbstub, gdbstub_port;
+#endif
+ int c, i, long_index, has_cdrom;
int snapshot, linux_boot;
- struct sigaction act;
- struct itimerval itv;
CPUState *env;
const char *initrd_filename;
const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
const char *kernel_filename, *kernel_cmdline;
DisplayState *ds = &display_state;
int cyls, heads, secs;
-
+ int start_emulation = 1;
+ uint8_t macaddr[6];
+ int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
+
+#if !defined(CONFIG_SOFTMMU)
/* we never want that malloc() uses mmap() */
mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
+#endif
initrd_filename = NULL;
for(i = 0; i < MAX_FD; i++)
fd_filename[i] = NULL;
ram_size = 32 * 1024 * 1024;
vga_ram_size = VGA_RAM_SIZE;
pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
+#ifdef CONFIG_GDBSTUB
use_gdbstub = 0;
gdbstub_port = DEFAULT_GDBSTUB_PORT;
+#endif
snapshot = 0;
nographic = 0;
kernel_filename = NULL;
has_cdrom = 1;
cyls = heads = secs = 0;
+ nb_tun_fds = 0;
+ net_if_type = -1;
nb_nics = 1;
- for(i = 0; i < MAX_NICS; i++) {
- NetDriverState *nd = &nd_table[i];
- nd->fd = -1;
- /* init virtual mac address */
- nd->macaddr[0] = 0x52;
- nd->macaddr[1] = 0x54;
- nd->macaddr[2] = 0x00;
- nd->macaddr[3] = 0x12;
- nd->macaddr[4] = 0x34;
- nd->macaddr[5] = 0x56 + i;
- }
-
+ /* default mac address of the first network interface */
+ macaddr[0] = 0x52;
+ macaddr[1] = 0x54;
+ macaddr[2] = 0x00;
+ macaddr[3] = 0x12;
+ macaddr[4] = 0x34;
+ macaddr[5] = 0x56;
+
+
for(;;) {
- c = getopt_long_only(argc, argv, "hm:d:n:sp:L:", long_options, &long_index);
+ c = getopt_long_only(argc, argv, "hm:d:n:sp:L:S", long_options, &long_index);
if (c == -1)
break;
switch(c) {
{
const char *p;
int fd;
- p = optarg;
- nb_nics = 0;
- for(;;) {
- fd = strtol(p, (char **)&p, 0);
- nd_table[nb_nics].fd = fd;
- snprintf(nd_table[nb_nics].ifname,
- sizeof(nd_table[nb_nics].ifname),
- "fd%d", nb_nics);
- nb_nics++;
- if (*p == ',') {
- p++;
- } else if (*p != '\0') {
- fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_nics);
+ if (nb_tun_fds < MAX_NICS) {
+ fd = strtol(optarg, (char **)&p, 0);
+ if (*p != '\0') {
+ fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
exit(1);
- } else {
- break;
}
+ tun_fds[nb_tun_fds++] = fd;
}
}
break;
exit(1);
}
break;
+ case 17:
+ {
+ const char *p;
+ int i;
+ p = optarg;
+ for(i = 0; i < 6; i++) {
+ macaddr[i] = strtol(p, (char **)&p, 16);
+ if (i == 5) {
+ if (*p != '\0')
+ goto macaddr_error;
+ } else {
+ if (*p != ':') {
+ macaddr_error:
+ fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
+ exit(1);
+ }
+ p++;
+ }
+ }
+ }
+ break;
+ case 18:
+ net_if_type = NET_IF_USER;
+ break;
+ case 19:
+ net_if_type = NET_IF_DUMMY;
+ break;
+ case 20:
+ audio_enabled = 1;
+ break;
}
break;
case 'h':
case 'n':
pstrcpy(network_script, sizeof(network_script), optarg);
break;
+#ifdef CONFIG_GDBSTUB
case 's':
use_gdbstub = 1;
break;
case 'p':
gdbstub_port = atoi(optarg);
break;
+#endif
case 'L':
bios_dir = optarg;
break;
+ case 'S':
+ start_emulation = 0;
+ break;
}
}
#endif
/* init host network redirectors */
- net_init();
+ if (net_if_type == -1) {
+ net_if_type = NET_IF_TUN;
+#if defined(CONFIG_SLIRP)
+ if (access(network_script, R_OK) < 0) {
+ net_if_type = NET_IF_USER;
+ }
+#endif
+ }
+
+ for(i = 0; i < nb_nics; i++) {
+ NetDriverState *nd = &nd_table[i];
+ nd->index = i;
+ /* init virtual mac address */
+ nd->macaddr[0] = macaddr[0];
+ nd->macaddr[1] = macaddr[1];
+ nd->macaddr[2] = macaddr[2];
+ nd->macaddr[3] = macaddr[3];
+ nd->macaddr[4] = macaddr[4];
+ nd->macaddr[5] = macaddr[5] + i;
+ switch(net_if_type) {
+#if defined(CONFIG_SLIRP)
+ case NET_IF_USER:
+ net_slirp_init(nd);
+ break;
+#endif
+#if !defined(_WIN32)
+ case NET_IF_TUN:
+ if (i < nb_tun_fds) {
+ net_fd_init(nd, tun_fds[i]);
+ } else {
+ if (net_tun_init(nd) < 0)
+ net_dummy_init(nd);
+ }
+ break;
+#endif
+ case NET_IF_DUMMY:
+ default:
+ net_dummy_init(nd);
+ break;
+ }
+ }
/* init the memory */
phys_ram_size = ram_size + vga_ram_size;
}
if (fd_filename[i] != '\0') {
if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
- fprintf(stderr, "qemu: could not open floppy disk image '%s\n",
+ fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
fd_filename[i]);
exit(1);
}
global_env = env;
cpu_single_env = env;
+ register_savevm("timer", 0, 1, timer_save, timer_load, env);
+ register_savevm("cpu", 0, 1, cpu_save, cpu_load, env);
+ register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
+
init_ioports();
cpu_calibrate_ticks();
#endif
}
-#if defined(TARGET_I386)
- pc_init(ram_size, vga_ram_size, boot_device,
- ds, fd_filename, snapshot,
- kernel_filename, kernel_cmdline, initrd_filename);
-#elif defined(TARGET_PPC)
- ppc_init();
-#endif
-
- /* launched after the device init so that it can display or not a
- banner */
- monitor_init();
-
/* setup cpu signal handlers for MMU / self modifying code handling */
#if !defined(CONFIG_SOFTMMU)
-
+
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
{
stack_t stk;
- signal_stack = malloc(SIGNAL_STACK_SIZE);
+ signal_stack = memalign(16, SIGNAL_STACK_SIZE);
stk.ss_sp = signal_stack;
stk.ss_size = SIGNAL_STACK_SIZE;
stk.ss_flags = 0;
}
}
#endif
+ {
+ struct sigaction act;
- sigfillset(&act.sa_mask);
- act.sa_flags = SA_SIGINFO;
+ sigfillset(&act.sa_mask);
+ act.sa_flags = SA_SIGINFO;
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
- act.sa_flags |= SA_ONSTACK;
+ act.sa_flags |= SA_ONSTACK;
#endif
- act.sa_sigaction = host_segv_handler;
- sigaction(SIGSEGV, &act, NULL);
- sigaction(SIGBUS, &act, NULL);
+ act.sa_sigaction = host_segv_handler;
+ sigaction(SIGSEGV, &act, NULL);
+ sigaction(SIGBUS, &act, NULL);
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
- sigaction(SIGFPE, &act, NULL);
+ sigaction(SIGFPE, &act, NULL);
#endif
+ }
#endif
- /* timer signal */
- sigfillset(&act.sa_mask);
- act.sa_flags = SA_SIGINFO;
-#if defined (TARGET_I386) && defined(USE_CODE_COPY)
- act.sa_flags |= SA_ONSTACK;
+#ifndef _WIN32
+ {
+ struct sigaction act;
+ sigfillset(&act.sa_mask);
+ act.sa_flags = 0;
+ act.sa_handler = SIG_IGN;
+ sigaction(SIGPIPE, &act, NULL);
+ }
+#endif
+ init_timers();
+
+#if defined(TARGET_I386)
+ pc_init(ram_size, vga_ram_size, boot_device,
+ ds, fd_filename, snapshot,
+ kernel_filename, kernel_cmdline, initrd_filename);
+#elif defined(TARGET_PPC)
+ ppc_init(ram_size, vga_ram_size, boot_device,
+ ds, fd_filename, snapshot,
+ kernel_filename, kernel_cmdline, initrd_filename);
#endif
- act.sa_sigaction = host_alarm_handler;
- sigaction(SIGALRM, &act, NULL);
-
- itv.it_interval.tv_sec = 0;
- itv.it_interval.tv_usec = 1000;
- itv.it_value.tv_sec = 0;
- itv.it_value.tv_usec = 10 * 1000;
- setitimer(ITIMER_REAL, &itv, NULL);
- /* we probe the tick duration of the kernel to inform the user if
- the emulated kernel requested a too high timer frequency */
- getitimer(ITIMER_REAL, &itv);
- timer_ms = itv.it_interval.tv_usec / 1000;
- pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) /
- 1000000;
+ /* launched after the device init so that it can display or not a
+ banner */
+ monitor_init();
+
+ gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
+ qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
+
+#ifdef CONFIG_GDBSTUB
if (use_gdbstub) {
- cpu_gdbstub(NULL, main_loop, gdbstub_port);
- } else {
- main_loop(NULL);
+ if (gdbserver_start(gdbstub_port) < 0) {
+ fprintf(stderr, "Could not open gdbserver socket on port %d\n",
+ gdbstub_port);
+ exit(1);
+ } else {
+ printf("Waiting gdb connection on port %d\n", gdbstub_port);
+ }
+ } else
+#endif
+ if (start_emulation)
+ {
+ vm_start();
}
+ term_init();
+ main_loop();
+ quit_timers();
return 0;
}