s390 bits
[qemu] / vl.c
diff --git a/vl.c b/vl.c
index e32c6d0..c3894b8 100644 (file)
--- a/vl.c
+++ b/vl.c
@@ -1,7 +1,7 @@
 /*
  * QEMU System Emulator
  * 
- * Copyright (c) 2003-2004 Fabrice Bellard
+ * Copyright (c) 2003-2005 Fabrice Bellard
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -38,6 +38,7 @@
 #include <sys/mman.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
+#include <netinet/in.h>
 #include <dirent.h>
 #ifdef _BSD
 #include <sys/stat.h>
 #endif
 #endif /* CONFIG_SDL */
 
+#ifdef CONFIG_COCOA
+#undef main
+#define main qemu_main
+#endif /* CONFIG_COCOA */
+
 #include "disas.h"
 
 #include "exec-all.h"
@@ -111,6 +117,7 @@ int vga_ram_size;
 int bios_size;
 static DisplayState display_state;
 int nographic;
+const char* keyboard_layout = NULL;
 int64_t ticks_per_sec;
 int boot_device = 'c';
 int ram_size;
@@ -121,20 +128,34 @@ NetDriverState nd_table[MAX_NICS];
 QEMUTimer *gui_timer;
 int vm_running;
 int audio_enabled = 0;
+int sb16_enabled = 1;
+int adlib_enabled = 1;
+int gus_enabled = 1;
 int pci_enabled = 1;
 int prep_enabled = 0;
 int rtc_utc = 1;
 int cirrus_vga_enabled = 1;
+#ifdef TARGET_SPARC
+int graphic_width = 1024;
+int graphic_height = 768;
+#else
 int graphic_width = 800;
 int graphic_height = 600;
+#endif
 int graphic_depth = 15;
+int full_screen = 0;
 TextConsole *vga_console;
 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
+CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
+#ifdef TARGET_I386
+int win2k_install_hack = 0;
+#endif
 
 /***********************************************************/
 /* x86 ISA bus support */
 
 target_phys_addr_t isa_mem_base = 0;
+PicState2 *isa_pic;
 
 uint32_t default_ioport_readb(void *opaque, uint32_t address)
 {
@@ -262,6 +283,8 @@ void isa_unassign_ioport(int start, int length)
     }
 }
 
+/***********************************************************/
+
 void pstrcpy(char *buf, int buf_size, const char *str)
 {
     int c;
@@ -404,9 +427,9 @@ void hw_error(const char *fmt, ...)
     vfprintf(stderr, fmt, ap);
     fprintf(stderr, "\n");
 #ifdef TARGET_I386
-    cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
+    cpu_dump_state(global_env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
 #else
-    cpu_dump_state(global_env, stderr, 0);
+    cpu_dump_state(global_env, stderr, fprintf, 0);
 #endif
     va_end(ap);
     abort();
@@ -500,6 +523,24 @@ int64_t cpu_get_real_ticks(void)
     return val;
 }
 
+#elif defined(__ia64)
+
+int64_t cpu_get_real_ticks(void)
+{
+       int64_t val;
+       asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory");
+       return val;
+}
+
+#elif defined(__s390__)
+
+int64_t cpu_get_real_ticks(void)
+{
+    int64_t val;
+    asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
+    return val;
+}
+
 #else
 #error unsupported CPU
 #endif
@@ -709,7 +750,7 @@ static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
     
     for(;;) {
         ts = *ptimer_head;
-        if (ts->expire_time > current_time)
+        if (!ts || ts->expire_time > current_time)
             break;
         /* remove timer from the list before calling the callback */
         *ptimer_head = ts->next;
@@ -905,7 +946,7 @@ static void init_timers(void)
         
         /* timer signal */
         sigfillset(&act.sa_mask);
-        act.sa_flags = 0;
+       act.sa_flags = 0;
 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
         act.sa_flags |= SA_ONSTACK;
 #endif
@@ -913,7 +954,7 @@ static void init_timers(void)
         sigaction(SIGALRM, &act, NULL);
 
         itv.it_interval.tv_sec = 0;
-        itv.it_interval.tv_usec = 1000;
+        itv.it_interval.tv_usec = 999; /* for i386 kernel 2.6 to get 1 ms */
         itv.it_value.tv_sec = 0;
         itv.it_value.tv_usec = 10 * 1000;
         setitimer(ITIMER_REAL, &itv, NULL);
@@ -1029,10 +1070,30 @@ typedef struct {
 static int stdio_nb_clients;
 static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS];
 
+static int unix_write(int fd, const uint8_t *buf, int len1)
+{
+    int ret, len;
+
+    len = len1;
+    while (len > 0) {
+        ret = write(fd, buf, len);
+        if (ret < 0) {
+            if (errno != EINTR && errno != EAGAIN)
+                return -1;
+        } else if (ret == 0) {
+            break;
+        } else {
+            buf += ret;
+            len -= ret;
+        }
+    }
+    return len1 - len;
+}
+
 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
 {
     FDCharDriver *s = chr->opaque;
-    return write(s->fd_out, buf, len);
+    return unix_write(s->fd_out, buf, len);
 }
 
 static void fd_chr_add_read_handler(CharDriverState *chr, 
@@ -1455,6 +1516,8 @@ static void net_slirp_redir(const char *redir_str)
     exit(1);
 }
     
+#ifndef _WIN32
+
 char smb_dir[1024];
 
 static void smb_exit(void)
@@ -1507,6 +1570,9 @@ void net_slirp_smb(const char *exported_dir)
     }
     fprintf(f, 
             "[global]\n"
+            "private dir=%s\n"
+            "smb ports=0\n"
+            "socket address=127.0.0.1\n"
             "pid directory=%s\n"
             "lock directory=%s\n"
             "log file=%s/log.smbd\n"
@@ -1520,6 +1586,7 @@ void net_slirp_smb(const char *exported_dir)
             smb_dir,
             smb_dir,
             smb_dir,
+            smb_dir,
             exported_dir
             );
     fclose(f);
@@ -1531,6 +1598,8 @@ void net_slirp_smb(const char *exported_dir)
     slirp_add_exec(0, smb_cmdline, 4, 139);
 }
 
+#endif /* !defined(_WIN32) */
+
 #endif /* CONFIG_SLIRP */
 
 #if !defined(_WIN32)
@@ -1638,6 +1707,46 @@ static int net_fd_init(NetDriverState *nd, int fd)
 #endif /* !_WIN32 */
 
 /***********************************************************/
+/* pid file */
+
+static char *pid_filename;
+
+/* Remove PID file. Called on normal exit */
+
+static void remove_pidfile(void) 
+{
+    unlink (pid_filename);
+}
+
+static void create_pidfile(const char *filename)
+{
+    struct stat pidstat;
+    FILE *f;
+
+    /* Try to write our PID to the named file */
+    if (stat(filename, &pidstat) < 0) {
+        if (errno == ENOENT) {
+            if ((f = fopen (filename, "w")) == NULL) {
+                perror("Opening pidfile");
+                exit(1);
+            }
+            fprintf(f, "%d\n", getpid());
+            fclose(f);
+            pid_filename = qemu_strdup(filename);
+            if (!pid_filename) {
+                fprintf(stderr, "Could not save PID filename");
+                exit(1);
+            }
+            atexit(remove_pidfile);
+        }
+    } else {
+        fprintf(stderr, "%s already exists. Remove it and try again.\n", 
+                filename);
+        exit(1);
+    }
+}
+
+/***********************************************************/
 /* dumb display */
 
 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
@@ -1994,7 +2103,7 @@ int qemu_loadvm(const char *filename)
 static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
 {
     qemu_put_be32(f, dt->selector);
-    qemu_put_be32(f, (uint32_t)dt->base);
+    qemu_put_betl(f, dt->base);
     qemu_put_be32(f, dt->limit);
     qemu_put_be32(f, dt->flags);
 }
@@ -2002,7 +2111,7 @@ static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
 static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
 {
     dt->selector = qemu_get_be32(f);
-    dt->base = (uint8_t *)qemu_get_be32(f);
+    dt->base = qemu_get_betl(f);
     dt->limit = qemu_get_be32(f);
     dt->flags = qemu_get_be32(f);
 }
@@ -2010,15 +2119,14 @@ static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
 void cpu_save(QEMUFile *f, void *opaque)
 {
     CPUState *env = opaque;
-    uint16_t fptag, fpus, fpuc;
+    uint16_t fptag, fpus, fpuc, fpregs_format;
     uint32_t hflags;
     int i;
-
-    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);
+    
+    for(i = 0; i < CPU_NB_REGS; i++)
+        qemu_put_betls(f, &env->regs[i]);
+    qemu_put_betls(f, &env->eip);
+    qemu_put_betls(f, &env->eflags);
     hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
     qemu_put_be32s(f, &hflags);
     
@@ -2026,23 +2134,39 @@ void cpu_save(QEMUFile *f, void *opaque)
     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;
-        }
+    for(i = 0; i < 8; i++) {
+        fptag |= ((!env->fptags[i]) << i);
     }
     
     qemu_put_be16s(f, &fpuc);
     qemu_put_be16s(f, &fpus);
     qemu_put_be16s(f, &fptag);
 
+#ifdef USE_X86LDOUBLE
+    fpregs_format = 0;
+#else
+    fpregs_format = 1;
+#endif
+    qemu_put_be16s(f, &fpregs_format);
+    
     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);
+#ifdef USE_X86LDOUBLE
+        {
+            uint64_t mant;
+            uint16_t exp;
+            /* we save the real CPU data (in case of MMX usage only 'mant'
+               contains the MMX register */
+            cpu_get_fp80(&mant, &exp, env->fpregs[i].d);
+            qemu_put_be64(f, mant);
+            qemu_put_be16(f, exp);
+        }
+#else
+        /* if we use doubles for float emulation, we save the doubles to
+           avoid losing information in case of MMX usage. It can give
+           problems if the image is restored on a CPU where long
+           doubles are used instead. */
+        qemu_put_be64(f, env->fpregs[i].mmx.MMX_Q(0));
+#endif
     }
 
     for(i = 0; i < 6; i++)
@@ -2056,52 +2180,130 @@ void cpu_save(QEMUFile *f, void *opaque)
     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]);
+    qemu_put_betls(f, &env->cr[0]);
+    qemu_put_betls(f, &env->cr[2]);
+    qemu_put_betls(f, &env->cr[3]);
+    qemu_put_betls(f, &env->cr[4]);
     
     for(i = 0; i < 8; i++)
-        qemu_put_be32s(f, &env->dr[i]);
+        qemu_put_betls(f, &env->dr[i]);
 
     /* MMU */
     qemu_put_be32s(f, &env->a20_mask);
+
+    /* XMM */
+    qemu_put_be32s(f, &env->mxcsr);
+    for(i = 0; i < CPU_NB_REGS; i++) {
+        qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(0));
+        qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(1));
+    }
+
+#ifdef TARGET_X86_64
+    qemu_put_be64s(f, &env->efer);
+    qemu_put_be64s(f, &env->star);
+    qemu_put_be64s(f, &env->lstar);
+    qemu_put_be64s(f, &env->cstar);
+    qemu_put_be64s(f, &env->fmask);
+    qemu_put_be64s(f, &env->kernelgsbase);
+#endif
 }
 
+#ifdef USE_X86LDOUBLE
+/* XXX: add that in a FPU generic layer */
+union x86_longdouble {
+    uint64_t mant;
+    uint16_t exp;
+};
+
+#define MANTD1(fp)     (fp & ((1LL << 52) - 1))
+#define EXPBIAS1 1023
+#define EXPD1(fp)      ((fp >> 52) & 0x7FF)
+#define SIGND1(fp)     ((fp >> 32) & 0x80000000)
+
+static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp)
+{
+    int e;
+    /* mantissa */
+    p->mant = (MANTD1(temp) << 11) | (1LL << 63);
+    /* exponent + sign */
+    e = EXPD1(temp) - EXPBIAS1 + 16383;
+    e |= SIGND1(temp) >> 16;
+    p->exp = e;
+}
+#endif
+
 int cpu_load(QEMUFile *f, void *opaque, int version_id)
 {
     CPUState *env = opaque;
-    int i;
+    int i, guess_mmx;
     uint32_t hflags;
-    uint16_t fpus, fpuc, fptag;
+    uint16_t fpus, fpuc, fptag, fpregs_format;
 
-    if (version_id != 2)
+    if (version_id != 3)
         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);
+    for(i = 0; i < CPU_NB_REGS; i++)
+        qemu_get_betls(f, &env->regs[i]);
+    qemu_get_betls(f, &env->eip);
+    qemu_get_betls(f, &env->eflags);
     qemu_get_be32s(f, &hflags);
 
     qemu_get_be16s(f, &fpuc);
     qemu_get_be16s(f, &fpus);
     qemu_get_be16s(f, &fptag);
-
+    qemu_get_be16s(f, &fpregs_format);
+    
+    /* NOTE: we cannot always restore the FPU state if the image come
+       from a host with a different 'USE_X86LDOUBLE' define. We guess
+       if we are in an MMX state to restore correctly in that case. */
+    guess_mmx = ((fptag == 0xff) && (fpus & 0x3800) == 0);
     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);
+        
+        switch(fpregs_format) {
+        case 0:
+            mant = qemu_get_be64(f);
+            exp = qemu_get_be16(f);
+#ifdef USE_X86LDOUBLE
+            env->fpregs[i].d = cpu_set_fp80(mant, exp);
+#else
+            /* difficult case */
+            if (guess_mmx)
+                env->fpregs[i].mmx.MMX_Q(0) = mant;
+            else
+                env->fpregs[i].d = cpu_set_fp80(mant, exp);
+#endif
+            break;
+        case 1:
+            mant = qemu_get_be64(f);
+#ifdef USE_X86LDOUBLE
+            {
+                union x86_longdouble *p;
+                /* difficult case */
+                p = (void *)&env->fpregs[i];
+                if (guess_mmx) {
+                    p->mant = mant;
+                    p->exp = 0xffff;
+                } else {
+                    fp64_to_fp80(p, mant);
+                }
+            }
+#else
+            env->fpregs[i].mmx.MMX_Q(0) = mant;
+#endif            
+            break;
+        default:
+            return -EINVAL;
+        }
     }
 
     env->fpuc = fpuc;
+    /* XXX: restore FPU round state */
     env->fpstt = (fpus >> 11) & 7;
     env->fpus = fpus & ~0x3800;
+    fptag ^= 0xff;
     for(i = 0; i < 8; i++) {
-        env->fptags[i] = ((fptag & 3) == 3);
-        fptag >>= 2;
+        env->fptags[i] = (fptag >> i) & 1;
     }
     
     for(i = 0; i < 6; i++)
@@ -2115,17 +2317,32 @@ int cpu_load(QEMUFile *f, void *opaque, int version_id)
     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]);
+    qemu_get_betls(f, &env->cr[0]);
+    qemu_get_betls(f, &env->cr[2]);
+    qemu_get_betls(f, &env->cr[3]);
+    qemu_get_betls(f, &env->cr[4]);
     
     for(i = 0; i < 8; i++)
-        qemu_get_be32s(f, &env->dr[i]);
+        qemu_get_betls(f, &env->dr[i]);
 
     /* MMU */
     qemu_get_be32s(f, &env->a20_mask);
 
+    qemu_get_be32s(f, &env->mxcsr);
+    for(i = 0; i < CPU_NB_REGS; i++) {
+        qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(0));
+        qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(1));
+    }
+
+#ifdef TARGET_X86_64
+    qemu_get_be64s(f, &env->efer);
+    qemu_get_be64s(f, &env->star);
+    qemu_get_be64s(f, &env->lstar);
+    qemu_get_be64s(f, &env->cstar);
+    qemu_get_be64s(f, &env->fmask);
+    qemu_get_be64s(f, &env->kernelgsbase);
+#endif
+
     /* XXX: compute hflags from scratch, except for CPL and IIF */
     env->hflags = hflags;
     tlb_flush(env, 1);
@@ -2141,6 +2358,93 @@ int cpu_load(QEMUFile *f, void *opaque, int version_id)
 {
     return 0;
 }
+
+#elif defined(TARGET_MIPS)
+void cpu_save(QEMUFile *f, void *opaque)
+{
+}
+
+int cpu_load(QEMUFile *f, void *opaque, int version_id)
+{
+    return 0;
+}
+
+#elif defined(TARGET_SPARC)
+void cpu_save(QEMUFile *f, void *opaque)
+{
+    CPUState *env = opaque;
+    int i;
+    uint32_t tmp;
+
+    for(i = 0; i < 8; i++)
+        qemu_put_betls(f, &env->gregs[i]);
+    for(i = 0; i < NWINDOWS * 16; i++)
+        qemu_put_betls(f, &env->regbase[i]);
+
+    /* FPU */
+    for(i = 0; i < TARGET_FPREGS; i++) {
+        union {
+            TARGET_FPREG_T f;
+            target_ulong i;
+        } u;
+        u.f = env->fpr[i];
+        qemu_put_betl(f, u.i);
+    }
+
+    qemu_put_betls(f, &env->pc);
+    qemu_put_betls(f, &env->npc);
+    qemu_put_betls(f, &env->y);
+    tmp = GET_PSR(env);
+    qemu_put_be32(f, tmp);
+    qemu_put_betls(f, &env->fsr);
+    qemu_put_betls(f, &env->tbr);
+#ifndef TARGET_SPARC64
+    qemu_put_be32s(f, &env->wim);
+    /* MMU */
+    for(i = 0; i < 16; i++)
+        qemu_put_be32s(f, &env->mmuregs[i]);
+#endif
+}
+
+int cpu_load(QEMUFile *f, void *opaque, int version_id)
+{
+    CPUState *env = opaque;
+    int i;
+    uint32_t tmp;
+
+    for(i = 0; i < 8; i++)
+        qemu_get_betls(f, &env->gregs[i]);
+    for(i = 0; i < NWINDOWS * 16; i++)
+        qemu_get_betls(f, &env->regbase[i]);
+
+    /* FPU */
+    for(i = 0; i < TARGET_FPREGS; i++) {
+        union {
+            TARGET_FPREG_T f;
+            target_ulong i;
+        } u;
+        u.i = qemu_get_betl(f);
+        env->fpr[i] = u.f;
+    }
+
+    qemu_get_betls(f, &env->pc);
+    qemu_get_betls(f, &env->npc);
+    qemu_get_betls(f, &env->y);
+    tmp = qemu_get_be32(f);
+    env->cwp = 0; /* needed to ensure that the wrapping registers are
+                     correctly updated */
+    PUT_PSR(env, tmp);
+    qemu_get_betls(f, &env->fsr);
+    qemu_get_betls(f, &env->tbr);
+#ifndef TARGET_SPARC64
+    qemu_get_be32s(f, &env->wim);
+    /* MMU */
+    for(i = 0; i < 16; i++)
+        qemu_get_be32s(f, &env->mmuregs[i]);
+#endif
+    tlb_flush(env, 1);
+    return 0;
+}
 #else
 
 #warning No CPU save/restore functions
@@ -2214,6 +2518,33 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
 }
 
 /***********************************************************/
+/* machine registration */
+
+QEMUMachine *first_machine = NULL;
+
+int qemu_register_machine(QEMUMachine *m)
+{
+    QEMUMachine **pm;
+    pm = &first_machine;
+    while (*pm != NULL)
+        pm = &(*pm)->next;
+    m->next = NULL;
+    *pm = m;
+    return 0;
+}
+
+QEMUMachine *find_machine(const char *name)
+{
+    QEMUMachine *m;
+
+    for(m = first_machine; m != NULL; m = m->next) {
+        if (!strcmp(m->name, name))
+            return m;
+    }
+    return NULL;
+}
+
+/***********************************************************/
 /* main execution loop */
 
 void gui_update(void *opaque)
@@ -2270,6 +2601,7 @@ typedef struct QEMUResetEntry {
 static QEMUResetEntry *first_reset_entry;
 static int reset_requested;
 static int shutdown_requested;
+static int powerdown_requested;
 
 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
 {
@@ -2307,9 +2639,15 @@ void qemu_system_shutdown_request(void)
     cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
 }
 
+void qemu_system_powerdown_request(void)
+{
+    powerdown_requested = 1;
+    cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
+}
+
 static void main_cpu_reset(void *opaque)
 {
-#ifdef TARGET_I386
+#if defined(TARGET_I386) || defined(TARGET_SPARC)
     CPUState *env = opaque;
     cpu_reset(env);
 #endif
@@ -2403,12 +2741,6 @@ void main_loop_wait(int timeout)
         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();
         }
@@ -2427,20 +2759,25 @@ int main_loop(void)
         if (vm_running) {
             ret = cpu_exec(env);
             if (shutdown_requested) {
-                ret = EXCP_INTERRUPT; 
+                ret = EXCP_INTERRUPT;
                 break;
             }
             if (reset_requested) {
                 reset_requested = 0;
                 qemu_system_reset();
-                ret = EXCP_INTERRUPT; 
+                ret = EXCP_INTERRUPT;
+            }
+            if (powerdown_requested) {
+                powerdown_requested = 0;
+               qemu_system_powerdown();
+                ret = EXCP_INTERRUPT;
             }
             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) 
+            if (ret == EXCP_HLT)
                 timeout = 10;
             else
                 timeout = 0;
@@ -2461,19 +2798,26 @@ void help(void)
            "'disk_image' is a raw hard image image for IDE hard disk 0\n"
            "\n"
            "Standard options:\n"
+           "-M machine      select emulated machine (-M ? for list)\n"
            "-fda/-fdb file  use 'file' as floppy disk 0/1 image\n"
            "-hda/-hdb file  use 'file' as IDE hard disk 0/1 image\n"
            "-hdc/-hdd file  use 'file' as IDE hard disk 2/3 image\n"
            "-cdrom file     use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
-           "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
+           "-boot [a|c|d]   boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
           "-snapshot       write to temporary files instead of disk image files\n"
            "-m megs         set virtual RAM size to megs MB [default=%d]\n"
            "-nographic      disable graphical output and redirect serial I/Os to console\n"
+#ifndef _WIN32
+          "-k language     use keyboard layout (for example \"fr\" for French)\n"
+#endif
            "-enable-audio   enable audio support\n"
            "-localtime      set the real time clock to local time [default=utc]\n"
-#ifdef TARGET_PPC
-           "-prep           Simulate a PREP system (default is PowerMAC)\n"
-           "-g WxH[xDEPTH]  Set the initial VGA graphic mode\n"
+           "-full-screen    start in full screen\n"
+#ifdef TARGET_I386
+           "-win2k-hack     use it when installing Windows 2000 to avoid a disk full bug\n"
+#endif
+#if defined(TARGET_PPC) || defined(TARGET_SPARC)
+           "-g WxH[xDEPTH]  Set the initial graphical resolution and depth\n"
 #endif
            "\n"
            "Network options:\n"
@@ -2484,7 +2828,9 @@ void help(void)
 #ifdef CONFIG_SLIRP
            "-user-net       use user mode network stack [default if no tap/tun script]\n"
            "-tftp prefix    allow tftp access to files starting with prefix [-user-net]\n"
+#ifndef _WIN32
            "-smb dir        allow SMB access to files in 'dir' [-user-net]\n"
+#endif
            "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
            "                redirect TCP or UDP connections from host to guest [-user-net]\n"
 #endif
@@ -2498,12 +2844,18 @@ void help(void)
            "Debug/Expert options:\n"
            "-monitor dev    redirect the monitor to char device 'dev'\n"
            "-serial dev     redirect the serial port to char device 'dev'\n"
+           "-parallel dev   redirect the parallel port to char device 'dev'\n"
+           "-pidfile file   Write PID to 'file'\n"
            "-S              freeze CPU at startup (use 'c' to start execution)\n"
            "-s              wait gdb connection to port %d\n"
            "-p port         change gdb connection port\n"
            "-d item1,...    output log to %s (use -d ? for a list of log items)\n"
-           "-hdachs c,h,s   force hard disk 0 geometry (usually qemu can guess it)\n"
+           "-hdachs c,h,s[,t]  force hard disk 0 physical geometry and the optional BIOS\n"
+           "                translation (t=none or lba) (usually qemu can guess them)\n"
            "-L path         set the directory for the BIOS and VGA BIOS\n"
+#ifdef USE_KQEMU
+           "-no-kqemu       disable KQEMU kernel module usage\n"
+#endif
 #ifdef USE_CODE_COPY
            "-no-code-copy   disable code copy acceleration\n"
 #endif
@@ -2512,11 +2864,12 @@ void help(void)
            "-std-vga        simulate a standard VGA card with VESA Bochs Extensions\n"
            "                (default is CL-GD5446 PCI VGA)\n"
 #endif
+           "-loadvm file    start right away with a saved state (loadvm in monitor)\n"
            "\n"
            "During emulation, the following keys are useful:\n"
-           "ctrl-shift-f    toggle full screen\n"
-           "ctrl-shift-Fn   switch to virtual console 'n'\n"
-           "ctrl-shift      toggle mouse and keyboard grab\n"
+           "ctrl-alt-f      toggle full screen\n"
+           "ctrl-alt-n      switch to virtual console 'n'\n"
+           "ctrl-alt        toggle mouse and keyboard grab\n"
            "\n"
            "When using -nographic, press 'ctrl-a h' to get some help.\n"
            ,
@@ -2543,6 +2896,7 @@ void help(void)
 enum {
     QEMU_OPTION_h,
 
+    QEMU_OPTION_M,
     QEMU_OPTION_fda,
     QEMU_OPTION_fdb,
     QEMU_OPTION_hda,
@@ -2580,12 +2934,19 @@ enum {
     QEMU_OPTION_pci,
     QEMU_OPTION_isa,
     QEMU_OPTION_prep,
+    QEMU_OPTION_k,
     QEMU_OPTION_localtime,
     QEMU_OPTION_cirrusvga,
     QEMU_OPTION_g,
     QEMU_OPTION_std_vga,
     QEMU_OPTION_monitor,
     QEMU_OPTION_serial,
+    QEMU_OPTION_parallel,
+    QEMU_OPTION_loadvm,
+    QEMU_OPTION_full_screen,
+    QEMU_OPTION_pidfile,
+    QEMU_OPTION_no_kqemu,
+    QEMU_OPTION_win2k_hack,
 };
 
 typedef struct QEMUOption {
@@ -2597,6 +2958,7 @@ typedef struct QEMUOption {
 const QEMUOption qemu_options[] = {
     { "h", 0, QEMU_OPTION_h },
 
+    { "M", HAS_ARG, QEMU_OPTION_M },
     { "fda", HAS_ARG, QEMU_OPTION_fda },
     { "fdb", HAS_ARG, QEMU_OPTION_fdb },
     { "hda", HAS_ARG, QEMU_OPTION_hda },
@@ -2608,6 +2970,7 @@ const QEMUOption qemu_options[] = {
     { "snapshot", 0, QEMU_OPTION_snapshot },
     { "m", HAS_ARG, QEMU_OPTION_m },
     { "nographic", 0, QEMU_OPTION_nographic },
+    { "k", HAS_ARG, QEMU_OPTION_k },
     { "enable-audio", 0, QEMU_OPTION_enable_audio },
 
     { "nics", HAS_ARG, QEMU_OPTION_nics},
@@ -2617,7 +2980,9 @@ const QEMUOption qemu_options[] = {
 #ifdef CONFIG_SLIRP
     { "user-net", 0, QEMU_OPTION_user_net },
     { "tftp", HAS_ARG, QEMU_OPTION_tftp },
+#ifndef _WIN32
     { "smb", HAS_ARG, QEMU_OPTION_smb },
+#endif
     { "redir", HAS_ARG, QEMU_OPTION_redir },
 #endif
     { "dummy-net", 0, QEMU_OPTION_dummy_net },
@@ -2633,8 +2998,13 @@ const QEMUOption qemu_options[] = {
     { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
     { "L", HAS_ARG, QEMU_OPTION_L },
     { "no-code-copy", 0, QEMU_OPTION_no_code_copy },
+#ifdef USE_KQEMU
+    { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
+#endif
 #ifdef TARGET_PPC
     { "prep", 0, QEMU_OPTION_prep },
+#endif
+#if defined(TARGET_PPC) || defined(TARGET_SPARC)
     { "g", 1, QEMU_OPTION_g },
 #endif
     { "localtime", 0, QEMU_OPTION_localtime },
@@ -2642,6 +3012,11 @@ const QEMUOption qemu_options[] = {
     { "std-vga", 0, QEMU_OPTION_std_vga },
     { "monitor", 1, QEMU_OPTION_monitor },
     { "serial", 1, QEMU_OPTION_serial },
+    { "parallel", 1, QEMU_OPTION_parallel },
+    { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
+    { "full-screen", 0, QEMU_OPTION_full_screen },
+    { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
+    { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
     
     /* temporary options */
     { "pci", 0, QEMU_OPTION_pci },
@@ -2695,6 +3070,26 @@ static void read_passwords(void)
     }
 }
 
+/* XXX: currently we cannot use simultaneously different CPUs */
+void register_machines(void)
+{
+#if defined(TARGET_I386)
+    qemu_register_machine(&pc_machine);
+#elif defined(TARGET_PPC)
+    qemu_register_machine(&heathrow_machine);
+    qemu_register_machine(&core99_machine);
+    qemu_register_machine(&prep_machine);
+#elif defined(TARGET_MIPS)
+    qemu_register_machine(&mips_machine);
+#elif defined(TARGET_SPARC)
+#ifdef TARGET_SPARC64
+    qemu_register_machine(&sun4u_machine);
+#else
+    qemu_register_machine(&sun4m_machine);
+#endif
+#endif
+}
+
 #define NET_IF_TUN   0
 #define NET_IF_USER  1
 #define NET_IF_DUMMY 2
@@ -2704,14 +3099,14 @@ int main(int argc, char **argv)
 #ifdef CONFIG_GDBSTUB
     int use_gdbstub, gdbstub_port;
 #endif
-    int i, has_cdrom;
+    int i, cdrom_index;
     int snapshot, linux_boot;
     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 cyls, heads, secs, translation;
     int start_emulation = 1;
     uint8_t macaddr[6];
     int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
@@ -2721,11 +3116,17 @@ int main(int argc, char **argv)
     char monitor_device[128];
     char serial_devices[MAX_SERIAL_PORTS][128];
     int serial_device_index;
-    
+    char parallel_devices[MAX_PARALLEL_PORTS][128];
+    int parallel_device_index;
+    const char *loadvm = NULL;
+    QEMUMachine *machine;
+
 #if !defined(CONFIG_SOFTMMU)
     /* we never want that malloc() uses mmap() */
     mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
 #endif
+    register_machines();
+    machine = first_machine;
     initrd_filename = NULL;
     for(i = 0; i < MAX_FD; i++)
         fd_filename[i] = NULL;
@@ -2743,8 +3144,13 @@ int main(int argc, char **argv)
     nographic = 0;
     kernel_filename = NULL;
     kernel_cmdline = "";
-    has_cdrom = 1;
+#ifdef TARGET_PPC
+    cdrom_index = 1;
+#else
+    cdrom_index = 2;
+#endif
     cyls = heads = secs = 0;
+    translation = BIOS_ATA_TRANSLATION_AUTO;
     pstrcpy(monitor_device, sizeof(monitor_device), "vc");
 
     pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc");
@@ -2752,6 +3158,11 @@ int main(int argc, char **argv)
         serial_devices[i][0] = '\0';
     serial_device_index = 0;
     
+    pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "vc");
+    for(i = 1; i < MAX_PARALLEL_PORTS; i++)
+        parallel_devices[i][0] = '\0';
+    parallel_device_index = 0;
+    
     nb_tun_fds = 0;
     net_if_type = -1;
     nb_nics = 1;
@@ -2797,14 +3208,33 @@ int main(int argc, char **argv)
             }
 
             switch(popt->index) {
+            case QEMU_OPTION_M:
+                machine = find_machine(optarg);
+                if (!machine) {
+                    QEMUMachine *m;
+                    printf("Supported machines are:\n");
+                    for(m = first_machine; m != NULL; m = m->next) {
+                        printf("%-10s %s%s\n",
+                               m->name, m->desc, 
+                               m == first_machine ? " (default)" : "");
+                    }
+                    exit(1);
+                }
+                break;
             case QEMU_OPTION_initrd:
                 initrd_filename = optarg;
                 break;
             case QEMU_OPTION_hda:
-                hd_filename[0] = optarg;
-                break;
             case QEMU_OPTION_hdb:
-                hd_filename[1] = optarg;
+            case QEMU_OPTION_hdc:
+            case QEMU_OPTION_hdd:
+                {
+                    int hd_index;
+                    hd_index = popt->index - QEMU_OPTION_hda;
+                    hd_filename[hd_index] = optarg;
+                    if (hd_index == cdrom_index)
+                        cdrom_index = -1;
+                }
                 break;
             case QEMU_OPTION_snapshot:
                 snapshot = 1;
@@ -2814,17 +3244,34 @@ int main(int argc, char **argv)
                     const char *p;
                     p = optarg;
                     cyls = strtol(p, (char **)&p, 0);
+                    if (cyls < 1 || cyls > 16383)
+                        goto chs_fail;
                     if (*p != ',')
                         goto chs_fail;
                     p++;
                     heads = strtol(p, (char **)&p, 0);
+                    if (heads < 1 || heads > 16)
+                        goto chs_fail;
                     if (*p != ',')
                         goto chs_fail;
                     p++;
                     secs = strtol(p, (char **)&p, 0);
-                    if (*p != '\0') {
+                    if (secs < 1 || secs > 63)
+                        goto chs_fail;
+                    if (*p == ',') {
+                        p++;
+                        if (!strcmp(p, "none"))
+                            translation = BIOS_ATA_TRANSLATION_NONE;
+                        else if (!strcmp(p, "lba"))
+                            translation = BIOS_ATA_TRANSLATION_LBA;
+                        else if (!strcmp(p, "auto"))
+                            translation = BIOS_ATA_TRANSLATION_AUTO;
+                        else
+                            goto chs_fail;
+                    } else if (*p != '\0') {
                     chs_fail:
-                        cyls = 0;
+                        fprintf(stderr, "qemu: invalid physical CHS format\n");
+                        exit(1);
                     }
                 }
                 break;
@@ -2854,20 +3301,18 @@ int main(int argc, char **argv)
                     }
                 }
                break;
-            case QEMU_OPTION_hdc:
-                hd_filename[2] = optarg;
-                has_cdrom = 0;
-                break;
-            case QEMU_OPTION_hdd:
-                hd_filename[3] = optarg;
-                break;
             case QEMU_OPTION_cdrom:
-                hd_filename[2] = optarg;
-                has_cdrom = 1;
+                if (cdrom_index >= 0) {
+                    hd_filename[cdrom_index] = optarg;
+                }
                 break;
             case QEMU_OPTION_boot:
                 boot_device = optarg[0];
-                if (boot_device != 'a' && boot_device != 'b' &&
+                if (boot_device != 'a' && 
+#ifdef TARGET_SPARC
+                   // Network boot
+                   boot_device != 'n' &&
+#endif
                     boot_device != 'c' && boot_device != 'd') {
                     fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
                     exit(1);
@@ -2914,9 +3359,11 @@ int main(int argc, char **argv)
             case QEMU_OPTION_tftp:
                tftp_prefix = optarg;
                 break;
+#ifndef _WIN32
             case QEMU_OPTION_smb:
                net_slirp_smb(optarg);
                 break;
+#endif
             case QEMU_OPTION_user_net:
                 net_if_type = NET_IF_USER;
                 break;
@@ -2985,6 +3432,9 @@ int main(int argc, char **argv)
             case QEMU_OPTION_prep:
                 prep_enabled = 1;
                 break;
+           case QEMU_OPTION_k:
+               keyboard_layout = optarg;
+               break;
             case QEMU_OPTION_localtime:
                 rtc_utc = 0;
                 break;
@@ -3040,13 +3490,43 @@ int main(int argc, char **argv)
                         sizeof(serial_devices[0]), optarg);
                 serial_device_index++;
                 break;
+            case QEMU_OPTION_parallel:
+                if (parallel_device_index >= MAX_PARALLEL_PORTS) {
+                    fprintf(stderr, "qemu: too many parallel ports\n");
+                    exit(1);
+                }
+                pstrcpy(parallel_devices[parallel_device_index], 
+                        sizeof(parallel_devices[0]), optarg);
+                parallel_device_index++;
+                break;
+           case QEMU_OPTION_loadvm:
+               loadvm = optarg;
+               break;
+            case QEMU_OPTION_full_screen:
+                full_screen = 1;
+                break;
+            case QEMU_OPTION_pidfile:
+                create_pidfile(optarg);
+                break;
+#ifdef TARGET_I386
+            case QEMU_OPTION_win2k_hack:
+                win2k_install_hack = 1;
+                break;
+#endif
+#ifdef USE_KQEMU
+            case QEMU_OPTION_no_kqemu:
+                kqemu_allowed = 0;
+                break;
+#endif
             }
         }
     }
 
     linux_boot = (kernel_filename != NULL);
         
-    if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
+    if (!linux_boot && 
+        hd_filename[0] == '\0' && 
+        (cdrom_index >= 0 && hd_filename[cdrom_index] == '\0') &&
         fd_filename[0] == '\0')
         help();
     
@@ -3115,12 +3595,7 @@ int main(int argc, char **argv)
     phys_ram_size = ram_size + vga_ram_size + bios_size;
 
 #ifdef CONFIG_SOFTMMU
-#ifdef _BSD
-    /* mallocs are always aligned on BSD. valloc is better for correctness */
-    phys_ram_base = valloc(phys_ram_size);
-#else
-    phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
-#endif
+    phys_ram_base = qemu_vmalloc(phys_ram_size);
     if (!phys_ram_base) {
         fprintf(stderr, "Could not allocate physical memory\n");
         exit(1);
@@ -3161,9 +3636,9 @@ int main(int argc, char **argv)
 
     /* we always create the cdrom drive, even if no disk is there */
     bdrv_init();
-    if (has_cdrom) {
-        bs_table[2] = bdrv_new("cdrom");
-        bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
+    if (cdrom_index >= 0) {
+        bs_table[cdrom_index] = bdrv_new("cdrom");
+        bdrv_set_type_hint(bs_table[cdrom_index], BDRV_TYPE_CDROM);
     }
 
     /* open the virtual block devices */
@@ -3179,8 +3654,10 @@ int main(int argc, char **argv)
                         hd_filename[i]);
                 exit(1);
             }
-            if (i == 0 && cyls != 0) 
+            if (i == 0 && cyls != 0) {
                 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
+                bdrv_set_translation_hint(bs_table[i], translation);
+            }
         }
     }
 
@@ -3212,7 +3689,7 @@ int main(int argc, char **argv)
     cpu_single_env = env;
 
     register_savevm("timer", 0, 1, timer_save, timer_load, env);
-    register_savevm("cpu", 0, 2, cpu_save, cpu_load, env);
+    register_savevm("cpu", 0, 3, cpu_save, cpu_load, env);
     register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
     qemu_register_reset(main_cpu_reset, global_env);
 
@@ -3223,8 +3700,10 @@ int main(int argc, char **argv)
     if (nographic) {
         dumb_display_init(ds);
     } else {
-#ifdef CONFIG_SDL
-        sdl_display_init(ds);
+#if defined(CONFIG_SDL)
+        sdl_display_init(ds, full_screen);
+#elif defined(CONFIG_COCOA)
+        cocoa_display_init(ds, full_screen);
 #else
         dumb_display_init(ds);
 #endif
@@ -3252,6 +3731,19 @@ int main(int argc, char **argv)
         }
     }
 
+    for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
+        if (parallel_devices[i][0] != '\0') {
+            parallel_hds[i] = qemu_chr_open(parallel_devices[i]);
+            if (!parallel_hds[i]) {
+                fprintf(stderr, "qemu: could not open parallel device '%s'\n", 
+                        parallel_devices[i]);
+                exit(1);
+            }
+            if (!strcmp(parallel_devices[i], "vc"))
+                qemu_chr_printf(parallel_hds[i], "parallel%d console\n", i);
+        }
+    }
+
     /* setup cpu signal handlers for MMU / self modifying code handling */
 #if !defined(CONFIG_SOFTMMU)
     
@@ -3297,15 +3789,9 @@ int main(int argc, char **argv)
 #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
+    machine->init(ram_size, vga_ram_size, boot_device,
+                  ds, fd_filename, snapshot,
+                  kernel_filename, kernel_cmdline, initrd_filename);
 
     gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
     qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
@@ -3321,6 +3807,9 @@ int main(int argc, char **argv)
         }
     } else 
 #endif
+    if (loadvm)
+        qemu_loadvm(loadvm);
+
     {
         /* XXX: simplify init */
         read_passwords();