bsd port (Markus Niemisto)


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@800 c046a42c-6fe2-441c-8c8c-71466251a162
diff --git a/Makefile b/Makefile
index 7645c08..0f64aae 100644
--- a/Makefile
+++ b/Makefile
@@ -16,7 +16,7 @@
 
 all: dyngen$(EXESUF) $(TOOLS) qemu-doc.html qemu-tech.html qemu.1
 	for d in $(TARGET_DIRS); do \
-	make -C $$d $@ || exit 1 ; \
+	$(MAKE) -C $$d $@ || exit 1 ; \
         done
 
 qemu-mkcow: qemu-mkcow.c
@@ -29,9 +29,9 @@
 # avoid old build problems by removing potentially incorrect old files
 	rm -f config.mak config.h op-i386.h opc-i386.h gen-op-i386.h op-arm.h opc-arm.h gen-op-arm.h 
 	rm -f *.o *.a $(TOOLS) dyngen$(EXESUF) TAGS qemu.pod
-	make -C tests clean
+	$(MAKE) -C tests clean
 	for d in $(TARGET_DIRS); do \
-	make -C $$d $@ || exit 1 ; \
+	$(MAKE) -C $$d $@ || exit 1 ; \
         done
 
 distclean: clean
@@ -55,12 +55,12 @@
 	install qemu.1 qemu-mkcow.1 "$(mandir)/man1"
 endif
 	for d in $(TARGET_DIRS); do \
-	make -C $$d $@ || exit 1 ; \
+	$(MAKE) -C $$d $@ || exit 1 ; \
         done
 
 # various test targets
 test speed test2: all
-	make -C tests $@
+	$(MAKE) -C tests $@
 
 TAGS: 
 	etags *.[ch] tests/*.[ch]
diff --git a/configure b/configure
index 765b234..055f50e 100755
--- a/configure
+++ b/configure
@@ -79,9 +79,23 @@
 MINGW32*)
 mingw32="yes"
 ;;
+FreeBSD)
+bsd="yes"
+;;
+NetBSD)
+bsd="yes"
+;;
+OpenBSD)
+bsd="yes"
+;;
 *) ;;
 esac
 
+if [ "$bsd" = "yes" ] ; then
+  make="gmake"
+  target_list="i386-softmmu"
+fi
+
 # find source path
 # XXX: we assume an absolute path is given when launching configure, 
 # except in './configure' case.
@@ -373,7 +387,7 @@
 if test "$mingw32" = "yes" ; then
   echo "CONFIG_WIN32=yes" >> $config_mak
   echo "#define CONFIG_WIN32 1" >> $config_h
-else
+elif test -f "/usr/include/byteswap.h" ; then
   echo "#define HAVE_BYTESWAP_H 1" >> $config_h
 fi
 if test "$gdbstub" = "yes" ; then
@@ -402,6 +416,10 @@
 echo "SRC_PATH=$source_path" >> $config_mak
 echo "TARGET_DIRS=$target_list" >> $config_mak
 
+if [ "$bsd" = "yes" ] ; then
+  echo "#define _BSD 1" >> $config_h
+fi
+
 for target in $target_list; do 
 
 target_dir="$target"
diff --git a/dyngen-exec.h b/dyngen-exec.h
index ea20e39..2d5209b 100644
--- a/dyngen-exec.h
+++ b/dyngen-exec.h
@@ -59,7 +59,11 @@
 extern int printf(const char *, ...);
 #undef NULL
 #define NULL 0
+#ifdef _BSD
+#include <ieeefp.h>
+#else
 #include <fenv.h>
+#endif
 
 #ifdef __i386__
 #define AREG0 "ebp"
diff --git a/gdbstub.c b/gdbstub.c
index 3560b26..1962310 100644
--- a/gdbstub.c
+++ b/gdbstub.c
@@ -532,7 +532,7 @@
 
     /* set short latency */
     val = 1;
-    setsockopt(fd, SOL_TCP, TCP_NODELAY, &val, sizeof(val));
+    setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
     
     s = qemu_mallocz(sizeof(GDBState));
     if (!s) {
diff --git a/target-i386/cpu.h b/target-i386/cpu.h
index 6939a2c..19340d2 100644
--- a/target-i386/cpu.h
+++ b/target-i386/cpu.h
@@ -259,7 +259,7 @@
     CC_OP_NB,
 };
 
-#if defined(__i386__) || defined(__x86_64__)
+#if (defined(__i386__) || defined(__x86_64__)) && !defined(_BSD)
 #define USE_X86LDOUBLE
 #endif
 
diff --git a/target-i386/exec.h b/target-i386/exec.h
index fb9cc77..f5b03fb 100644
--- a/target-i386/exec.h
+++ b/target-i386/exec.h
@@ -17,6 +17,7 @@
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
+#include "config.h"
 #include "dyngen-exec.h"
 
 /* at least 4 register variables are defines */
@@ -307,8 +308,13 @@
 #define rint rintl
 #endif
 
+#if !defined(_BSD)
 extern int lrint(CPU86_LDouble x);
 extern int64_t llrint(CPU86_LDouble x);
+#else
+#define lrint(d)		((int)rint(d))
+#define llrint(d)		((int)rint(d))
+#endif
 extern CPU86_LDouble fabs(CPU86_LDouble x);
 extern CPU86_LDouble sin(CPU86_LDouble x);
 extern CPU86_LDouble cos(CPU86_LDouble x);
diff --git a/target-i386/op.c b/target-i386/op.c
index 3782331..fad8a73 100644
--- a/target-i386/op.c
+++ b/target-i386/op.c
@@ -1938,6 +1938,24 @@
     int rnd_type;
     env->fpuc = lduw((void *)A0);
     /* set rounding mode */
+#ifdef _BSD
+    switch(env->fpuc & RC_MASK) {
+    default:
+    case RC_NEAR:
+        rnd_type = FP_RN;
+        break;
+    case RC_DOWN:
+        rnd_type = FP_RM;
+        break;
+    case RC_UP:
+        rnd_type = FP_RP;
+        break;
+    case RC_CHOP:
+        rnd_type = FP_RZ;
+        break;
+    }
+    fpsetround(rnd_type);
+#else
     switch(env->fpuc & RC_MASK) {
     default:
     case RC_NEAR:
@@ -1954,6 +1972,7 @@
         break;
     }
     fesetround(rnd_type);
+#endif
 }
 
 void OPPROTO op_fclex(void)
diff --git a/vl.c b/vl.c
index 05c6f0f..4489056 100644
--- a/vl.c
+++ b/vl.c
@@ -28,29 +28,35 @@
 #include <fcntl.h>
 #include <signal.h>
 #include <time.h>
-#include <malloc.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>
+#ifdef _BSD
+#include <sys/stat.h>
+#include <libutil.h>
+#else
 #include <linux/if.h>
 #include <linux/if_tun.h>
+#include <pty.h>
+#include <malloc.h>
 #include <linux/rtc.h>
 #endif
+#endif
 
 #if defined(CONFIG_SLIRP)
 #include "libslirp.h"
 #endif
 
 #ifdef _WIN32
+#include <malloc.h>
 #include <sys/timeb.h>
 #include <windows.h>
 #define getopt_long_only getopt_long
@@ -58,15 +64,17 @@
 #endif
 
 #ifdef CONFIG_SDL
+#if defined(__linux__)
 /* SDL use the pthreads and they modify sigaction. We don't
    want that. */
-#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
+#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 /* __linux__ */
 #endif /* CONFIG_SDL */
 
 #include "disas.h"
@@ -637,11 +645,17 @@
 #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;
+        {
+            struct tms tp;
+
+            /* Note that using gettimeofday() is not a good solution
+               for timers because its value change when the date is
+               modified. */
+            if (timer_freq == 100) {
+                return times(&tp) * 10;
+            } else {
+                return ((int64_t)times(&tp) * 1000) / timer_freq;
+            }
         }
 #endif
     default:
@@ -964,7 +978,27 @@
 #endif /* CONFIG_SLIRP */
 
 #if !defined(_WIN32)
+#ifdef _BSD
+static int tun_open(char *ifname, int ifname_size)
+{
+    int fd;
+    char *dev;
+    struct stat s;
 
+    fd = open("/dev/tap", O_RDWR);
+    if (fd < 0) {
+        fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
+        return -1;
+    }
+
+    fstat(fd, &s);
+    dev = devname(s.st_rdev, S_IFCHR);
+    pstrcpy(ifname, ifname_size, dev);
+
+    fcntl(fd, F_SETFL, O_NONBLOCK);
+    return fd;
+}
+#else
 static int tun_open(char *ifname, int ifname_size)
 {
     struct ifreq ifr;
@@ -989,6 +1023,7 @@
     fcntl(fd, F_SETFL, O_NONBLOCK);
     return fd;
 }
+#endif
 
 static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
 {
@@ -2248,7 +2283,12 @@
     phys_ram_size = ram_size + vga_ram_size;
 
 #ifdef CONFIG_SOFTMMU
+#ifdef _BSD
+    /* mallocs are always aligned on BSD. */
+    phys_ram_base = malloc(phys_ram_size);
+#else
     phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
+#endif
     if (!phys_ram_base) {
         fprintf(stderr, "Could not allocate physical memory\n");
         exit(1);
diff --git a/vl.h b/vl.h
index 639b473..b640ad4 100644
--- a/vl.h
+++ b/vl.h
@@ -35,6 +35,7 @@
 #include <errno.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <sys/stat.h>
 
 #ifndef O_LARGEFILE
 #define O_LARGEFILE 0
@@ -49,6 +50,13 @@
 
 #include "cpu.h"
 
+#ifdef _BSD
+#define lseek64 lseek
+#define ftruncate64 ftruncate
+#define mkstemp64 mkstemp
+#define MAP_ANONYMOUS MAP_ANON
+#endif
+
 #ifndef glue
 #define xglue(x, y) x ## y
 #define glue(x, y) xglue(x, y)