The patchset below allows buildiung the CVS version of qemu as of
20050501 on NetBSD 2.0 userland. qemu(-i386) works OK to boot NetBSD,
but when starting X, the mouse cursor still jumps around, and the
(host) X server is then killed with a sig11. This works 

The patchset includes patches from NetBSD's
pkgsrc/emulators/qemu/patches plus more from me. The patch in op.c is
esp. nasty - defining isunordered() in exec.h is not enough.


 - Hubert Feyrer <hubertf@NetBSD.org>

Index: Makefile
===================================================================
RCS file: /cvsroot/qemu/qemu/Makefile,v
retrieving revision 1.87
diff -u -r1.87 Makefile
--- Makefile	28 Apr 2005 21:15:08 -0000	1.87
+++ Makefile	1 May 2005 18:27:51 -0000
@@ -1,10 +1,10 @@
 -include config-host.mak
 
-CFLAGS=-Wall -O2 -g -fno-strict-aliasing 
+#CFLAGS=-Wall -O2 -g -fno-strict-aliasing 
 ifdef CONFIG_DARWIN
 CFLAGS+= -mdynamic-no-pic
 endif
-LDFLAGS=-g
+#LDFLAGS=-g
 LIBS=
 DEFINES+=-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
 TOOLS=qemu-img$(EXESUF)
@@ -89,11 +89,11 @@
 	texi2html -monolithic -number $<
 
 qemu.1: qemu-doc.texi
-	./texi2pod.pl $< qemu.pod
+	perl texi2pod.pl $< qemu.pod
 	pod2man --section=1 --center=" " --release=" " qemu.pod > $@
 
 qemu-img.1: qemu-img.texi
-	./texi2pod.pl $< qemu-img.pod
+	perl texi2pod.pl $< qemu-img.pod
 	pod2man --section=1 --center=" " --release=" " qemu-img.pod > $@
 
 FILE=qemu-$(shell cat VERSION)
Index: Makefile.target
===================================================================
RCS file: /cvsroot/qemu/qemu/Makefile.target,v
retrieving revision 1.69
diff -u -r1.69 Makefile.target
--- Makefile.target	28 Apr 2005 21:15:08 -0000	1.69
+++ Makefile.target	1 May 2005 18:27:51 -0000
@@ -14,9 +14,9 @@
 VPATH+=:$(SRC_PATH)/linux-user
 DEFINES+=-I$(SRC_PATH)/linux-user -I$(SRC_PATH)/linux-user/$(TARGET_ARCH)
 endif
-CFLAGS=-Wall -O2 -g -fno-strict-aliasing
+#CFLAGS=-Wall -O2 -g -fno-strict-aliasing
 #CFLAGS+=-Werror
-LDFLAGS=-g
+#LDFLAGS=-g
 LIBS=
 HELPER_CFLAGS=$(CFLAGS)
 DYNGEN=../dyngen$(EXESUF)
@@ -213,6 +213,9 @@
 
 DEFINES+=-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
 LIBS+=-lm
+ifdef CONFIG_DARWIN
+LIBS+=-lmx
+endif
 ifndef CONFIG_USER_ONLY
 LIBS+=-lz
 endif
@@ -463,7 +466,7 @@
 
 install: all 
 ifneq ($(PROGS),)
-	install -m 755 -s $(PROGS) "$(bindir)"
+	$(BSD_INSTALL_PROGRAM) $(PROGS) "$(bindir)"
 endif
 
 ifneq ($(wildcard .depend),)
Index: bswap.h
===================================================================
RCS file: /cvsroot/qemu/qemu/bswap.h,v
retrieving revision 1.5
diff -u -r1.5 bswap.h
--- bswap.h	10 Oct 2004 15:44:19 -0000	1.5
+++ bswap.h	1 May 2005 18:27:51 -0000
@@ -5,6 +5,12 @@
 
 #include <inttypes.h>
 
+#ifdef HAVE_MACHINE_BSWAP_H
+#include <sys/endian.h>
+#include <sys/types.h>
+#include <machine/bswap.h>
+#else
+
 #ifdef HAVE_BYTESWAP_H
 #include <byteswap.h>
 #else
@@ -58,6 +64,8 @@
     return bswap_64(x);
 }
 
+#endif /* ! HAVE_MACHINE_BSWAP_H */
+
 static inline void bswap16s(uint16_t *s)
 {
     *s = bswap16(*s);
Index: configure
===================================================================
RCS file: /cvsroot/qemu/qemu/configure,v
retrieving revision 1.66
diff -u -r1.66 configure
--- configure	28 Apr 2005 20:41:53 -0000	1.66
+++ configure	1 May 2005 18:27:51 -0000
@@ -292,7 +292,7 @@
 `$sdl_config --static-libs | grep \\\-laa > /dev/null` && aa="yes"
 sdl_static_libs=`$sdl_config --static-libs`
 if [ "$aa" = "yes" ] ; then
-  sdl_static_libs="$sdl_static_libs `aalib-config --static-libs`"
+  sdl_static_libs="$sdl_static_libs `aalib-x11-config --static-libs`"
 fi
 
 if $cc -o $TMPE `$sdl_config --cflags 2> /dev/null` $TMPC $sdl_static_libs 2> /dev/null; then
@@ -352,7 +352,7 @@
 if test -z "$prefix" ; then
     prefix="/usr/local"
 fi
-mandir="$prefix/share/man"
+mandir="$prefix/man"
 datadir="$prefix/share/qemu"
 docdir="$prefix/share/doc/qemu"
 bindir="$prefix/bin"
@@ -523,6 +523,8 @@
   echo "#define CONFIG_WIN32 1" >> $config_h
 elif test -f "/usr/include/byteswap.h" ; then
   echo "#define HAVE_BYTESWAP_H 1" >> $config_h
+elif test -f "/usr/include/machine/bswap.h" ; then
+  echo "#define HAVE_MACHINE_BSWAP_H 1" >> $config_h
 fi
 if test "$darwin" = "yes" ; then
   echo "CONFIG_DARWIN=yes" >> $config_mak
@@ -698,7 +700,7 @@
         fi
         echo -n "SDL_CFLAGS=`$sdl_config --cflags`" >> $config_mak
         if [ "${aa}" = "yes" ] ; then
-            echo -n " `aalib-config --cflags`" >> $config_mak ;
+            echo -n " `aalib-x11-config --cflags`" >> $config_mak ;
         fi
         echo "" >> $config_mak
     fi
Index: dyngen-exec.h
===================================================================
RCS file: /cvsroot/qemu/qemu/dyngen-exec.h,v
retrieving revision 1.25
diff -u -r1.25 dyngen-exec.h
--- dyngen-exec.h	24 Apr 2005 18:01:56 -0000	1.25
+++ dyngen-exec.h	1 May 2005 18:27:51 -0000
@@ -23,7 +23,9 @@
 /* NOTE: standard headers should be used with special care at this
    point because host CPU registers are used as global variables. Some
    host headers do not allow that. */
+#ifndef __APPLE__
 #include <stddef.h>
+#endif
 
 typedef unsigned char uint8_t;
 typedef unsigned short uint16_t;
@@ -43,7 +45,7 @@
 #else
 typedef signed long long int64_t;
 #endif
-
+/*
 #define INT8_MIN		(-128)
 #define INT16_MIN		(-32767-1)
 #define INT32_MIN		(-2147483647-1)
@@ -56,12 +58,28 @@
 #define UINT16_MAX		(65535)
 #define UINT32_MAX		(4294967295U)
 #define UINT64_MAX		((uint64_t)(18446744073709551615))
-
+*/
+#ifdef __NetBSD__
+typedef	struct __sFILE FILE;
+#else
 typedef struct FILE FILE;
+#endif
 extern int fprintf(FILE *, const char *, ...);
 extern int printf(const char *, ...);
 #undef NULL
 #define NULL 0
+#if defined(_BSD) && !defined(__APPLE__)
+#include <ieeefp.h>
+
+#define FE_TONEAREST   FP_RN
+#define FE_DOWNWARD    FP_RM
+#define FE_UPWARD      FP_RP
+#define FE_TOWARDZERO  FP_RZ
+#define fesetround(x)  fpsetround(x)
+#define fegetround()  fpgetround()
+#else
+#include <fenv.h>
+#endif
 
 #ifdef __i386__
 #define AREG0 "ebp"
Index: osdep.c
===================================================================
RCS file: /cvsroot/qemu/qemu/osdep.c,v
retrieving revision 1.10
diff -u -r1.10 osdep.c
--- osdep.c	23 Apr 2005 20:44:25 -0000	1.10
+++ osdep.c	1 May 2005 18:27:52 -0000
@@ -39,7 +39,9 @@
    the CPU core because it needs to use alternates stacks and
    libc/thread incompatibles settings */
 
+#ifdef __linux
 #include <linux/unistd.h>
+#endif
 
 #define QEMU_SYSCALL0(name) \
 { \
Index: osdep.h
===================================================================
RCS file: /cvsroot/qemu/qemu/osdep.h,v
retrieving revision 1.5
diff -u -r1.5 osdep.h
--- osdep.h	10 Feb 2005 21:59:25 -0000	1.5
+++ osdep.h	1 May 2005 18:27:52 -0000
@@ -2,6 +2,8 @@
 #define QEMU_OSDEP_H
 
 #include <stdarg.h>
+#include <sys/types.h>
+#include <sys/signal.h>
 
 int qemu_vsnprintf(char *buf, int buflen, const char *fmt, va_list args);
 void qemu_vprintf(const char *fmt, va_list ap);
@@ -31,7 +33,7 @@
 struct qemu_sigaction {
     union {
         void (*_sa_handler)(int);
-        void (*_sa_sigaction)(int, struct siginfo *, void *);
+        void (*_sa_sigaction)(int, siginfo_t *, void *);
     } _u;
     unsigned long sa_flags;
     void (*sa_restorer)(void);
Index: vl.c
===================================================================
RCS file: /cvsroot/qemu/qemu/vl.c,v
retrieving revision 1.127
diff -u -r1.127 vl.c
--- vl.c	30 Apr 2005 16:10:35 -0000	1.127
+++ vl.c	1 May 2005 18:27:53 -0000
@@ -43,8 +43,10 @@
 #ifdef _BSD
 #include <sys/stat.h>
 #ifndef __APPLE__
+#ifndef _BSD
 #include <libutil.h>
 #endif
+#endif
 #else
 #include <linux/if.h>
 #include <linux/if_tun.h>
@@ -3038,9 +3040,11 @@
     const char *loadvm = NULL;
     
 #if !defined(CONFIG_SOFTMMU)
+#ifdef __linux
     /* we never want that malloc() uses mmap() */
     mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
 #endif
+#endif
     initrd_filename = NULL;
     for(i = 0; i < MAX_FD; i++)
         fd_filename[i] = NULL;
Index: audio/mixeng.c
===================================================================
RCS file: /cvsroot/qemu/qemu/audio/mixeng.c,v
retrieving revision 1.1
diff -u -r1.1 mixeng.c
--- audio/mixeng.c	7 Nov 2004 18:04:02 -0000	1.1
+++ audio/mixeng.c	1 May 2005 18:27:53 -0000
@@ -27,6 +27,8 @@
 #include "audio/mixeng.h"
 
 #define IN_T int8_t
+#define CONV_IN_T conv_int8_t
+#define CLIP_IN_T clip_int8_t
 #define IN_MIN CHAR_MIN
 #define IN_MAX CHAR_MAX
 #define SIGNED
@@ -34,17 +36,25 @@
 #undef SIGNED
 #undef IN_MAX
 #undef IN_MIN
+#undef CLIP_IN_T
+#undef CONV_IN_T
 #undef IN_T
 
 #define IN_T uint8_t
+#define CONV_IN_T conv_uint8_t
+#define CLIP_IN_T clip_uint8_t
 #define IN_MIN 0
 #define IN_MAX UCHAR_MAX
 #include "mixeng_template.h"
 #undef IN_MAX
 #undef IN_MIN
+#undef CLIP_IN_T
+#undef CONV_IN_T
 #undef IN_T
 
 #define IN_T int16_t
+#define CONV_IN_T conv_int16_t
+#define CLIP_IN_T clip_int16_t
 #define IN_MIN SHRT_MIN
 #define IN_MAX SHRT_MAX
 #define SIGNED
@@ -52,17 +62,24 @@
 #undef SIGNED
 #undef IN_MAX
 #undef IN_MIN
+#undef CLIP_IN_T
+#undef CONV_IN_T
 #undef IN_T
 
 #define IN_T uint16_t
+#define CONV_IN_T conv_uint16_t
+#define CLIP_IN_T clip_uint16_t
 #define IN_MIN 0
 #define IN_MAX USHRT_MAX
 #include "mixeng_template.h"
 #undef IN_MAX
 #undef IN_MIN
+#undef CLIP_IN_T
+#undef CONV_IN_T
 #undef IN_T
 
 t_sample *mixeng_conv[2][2][2] = {
+#if !defined(__NetBSD__)
     {
         {
             conv_uint8_t_to_mono,
@@ -83,9 +100,11 @@
             conv_int16_t_to_stereo
         }
     }
+#endif /* !__NetBSD__ */
 };
 
 f_sample *mixeng_clip[2][2][2] = {
+#if !defined(__NetBSD__)
     {
         {
             clip_uint8_t_from_mono,
@@ -106,6 +125,7 @@
             clip_int16_t_from_stereo
         }
     }
+#endif /* !__NetBSD__ */
 };
 
 /*
Index: audio/ossaudio.c
===================================================================
RCS file: /cvsroot/qemu/qemu/audio/ossaudio.c,v
retrieving revision 1.4
diff -u -r1.4 ossaudio.c
--- audio/ossaudio.c	14 Nov 2004 16:02:51 -0000	1.4
+++ audio/ossaudio.c	1 May 2005 18:27:55 -0000
@@ -164,7 +164,7 @@
         goto err;
     }
 
-    if (ioctl (fd, SNDCTL_DSP_NONBLOCK)) {
+    if (ioctl (fd, SNDCTL_DSP_NONBLOCK, NULL)) {
         dolog ("Could not initialize audio hardware\n"
                "Failed to set non-blocking mode\n"
                "Reason: %s\n",
Index: fpu/softfloat-native.c
===================================================================
RCS file: /cvsroot/qemu/qemu/fpu/softfloat-native.c,v
retrieving revision 1.2
diff -u -r1.2 softfloat-native.c
--- fpu/softfloat-native.c	20 Mar 2005 10:33:58 -0000	1.2
+++ fpu/softfloat-native.c	1 May 2005 18:27:55 -0000
@@ -24,7 +24,16 @@
 
 #if defined(_BSD)
 #define lrint(d)		((int32_t)rint(d))
+#define lrintf(d)		((int32_t)rint(d))
 #define llrint(d)		((int64_t)rint(d))
+#define llrintf(d)		((int64_t)rint(d))
+
+/* SUSv3 / C99 define these: - HF */
+#ifdef __NetBSD__
+#define isgreater(x, y)		(x > y)
+#define isless(x, y)		(x < y)
+#define isunordered(x, y)	(isnan(x)?1:isnan(y)1:0)
+#endif /* __NetBSD__ */
 #endif
 
 #if defined(__powerpc__)
Index: fpu/softfloat-native.h
===================================================================
RCS file: /cvsroot/qemu/qemu/fpu/softfloat-native.h,v
retrieving revision 1.2
diff -u -r1.2 softfloat-native.h
--- fpu/softfloat-native.h	20 Mar 2005 10:33:58 -0000	1.2
+++ fpu/softfloat-native.h	1 May 2005 18:27:55 -0000
@@ -34,12 +34,21 @@
 | Software IEC/IEEE floating-point rounding mode.
 *----------------------------------------------------------------------------*/
 #if defined(_BSD) && !defined(__APPLE__)
+#  ifdef __NetBSD__
+enum {
+    float_round_nearest_even = FP_RN,
+    float_round_down         = FP_RM,
+    float_round_up           = FP_RP,
+    float_round_to_zero      = FP_RZ
+};
+#  else /* __NetBSD__ */
 enum {
     float_round_nearest_even = FP_RN,
     float_round_down         = FE_RM,
     float_round_up           = FE_RP,
     float_round_to_zero      = FE_RZ
 };
+#  endif /* __NetBSD__ */
 #elif defined(__arm__)
 enum {
     float_round_nearest_even = 0,
Index: hw/ide.c
===================================================================
RCS file: /cvsroot/qemu/qemu/hw/ide.c,v
retrieving revision 1.34
diff -u -r1.34 ide.c
--- hw/ide.c	30 Apr 2005 16:10:35 -0000	1.34
+++ hw/ide.c	1 May 2005 18:27:55 -0000
@@ -1952,7 +1952,9 @@
                     s->heads = 16;
                     s->sectors = 63;
                 }
+#if 0
                 bdrv_set_geometry_hint(s->bs, s->cylinders, s->heads, s->sectors);
+#endif
             }
             if (bdrv_get_type_hint(s->bs) == BDRV_TYPE_CDROM) {
                 s->is_cdrom = 1;
Index: target-i386/cpu.h
===================================================================
RCS file: /cvsroot/qemu/qemu/target-i386/cpu.h,v
retrieving revision 1.30
diff -u -r1.30 cpu.h
--- target-i386/cpu.h	23 Apr 2005 17:46:55 -0000	1.30
+++ target-i386/cpu.h	1 May 2005 18:27:55 -0000
@@ -620,9 +620,15 @@
 /* you can call this signal handler from your SIGBUS and SIGSEGV
    signal handlers to inform the virtual CPU of exceptions. non zero
    is returned if the signal was handled by the virtual CPU.  */
+#ifdef __NetBSD__
+union siginfo;
+int cpu_x86_signal_handler(int host_signum, union siginfo *info, 
+                           void *puc);
+#else
 struct siginfo;
 int cpu_x86_signal_handler(int host_signum, struct siginfo *info, 
                            void *puc);
+#endif
 void cpu_x86_set_a20(CPUX86State *env, int a20_state);
 
 uint64_t cpu_get_tsc(CPUX86State *env);
Index: target-i386/exec.h
===================================================================
RCS file: /cvsroot/qemu/qemu/target-i386/exec.h,v
retrieving revision 1.24
diff -u -r1.24 exec.h
--- target-i386/exec.h	20 Mar 2005 10:39:24 -0000	1.24
+++ target-i386/exec.h	1 May 2005 18:27:56 -0000
@@ -346,6 +346,23 @@
 #define floatx_compare_quiet float64_compare_quiet
 #endif
 
+#if !defined(_BSD)
+extern int lrint(CPU86_LDouble x);
+extern int64_t llrint(CPU86_LDouble x);
+#else
+#define lrint(d)		((long)rint(d))
+#define llrint(d)		((long long)rint(d))
+
+/* SUSv3 / C99 define these: - HF */
+#ifdef __NetBSD__
+#define isgreater(x, y)		(x > y)
+#define isless(x, y)		(x < y)
+#define isunordered(x, y)	((isnan(x))?1:(isnan(y))?1:0)
+#warning HF: isunordered defined (2)
+#endif /* __NetBSD__ */
+#endif
+extern CPU86_LDouble fabs(CPU86_LDouble x);
+
 extern CPU86_LDouble sin(CPU86_LDouble x);
 extern CPU86_LDouble cos(CPU86_LDouble x);
 extern CPU86_LDouble sqrt(CPU86_LDouble x);
Index: target-i386/op.c
===================================================================
RCS file: /cvsroot/qemu/qemu/target-i386/op.c,v
retrieving revision 1.37
diff -u -r1.37 op.c
--- target-i386/op.c	26 Apr 2005 20:38:17 -0000	1.37
+++ target-i386/op.c	1 May 2005 18:27:56 -0000
@@ -1,3 +1,9 @@
+/* NetBSD 2.x does not have isunordered(); NetBSD 3.x may have it - HF */
+#ifdef __NetBSD__
+#  define isunordered(x, y)	((isnan(x))?1:(isnan(y))?1:0)
+#endif /* __NetBSD__ */
+
+
 /*
  *  i386 micro operations
  * 
Index: target-ppc/cpu.h
===================================================================
RCS file: /cvsroot/qemu/qemu/target-ppc/cpu.h,v
retrieving revision 1.17
diff -u -r1.17 cpu.h
--- target-ppc/cpu.h	17 Apr 2005 19:16:13 -0000	1.17
+++ target-ppc/cpu.h	1 May 2005 18:27:56 -0000
@@ -169,9 +169,15 @@
 /* you can call this signal handler from your SIGBUS and SIGSEGV
    signal handlers to inform the virtual CPU of exceptions. non zero
    is returned if the signal was handled by the virtual CPU.  */
+#ifdef __NetBSD__
+union siginfo;
+int cpu_ppc_signal_handler(int host_signum, union siginfo *info, 
+                           void *puc);
+#else
 struct siginfo;
 int cpu_ppc_signal_handler(int host_signum, struct siginfo *info, 
                            void *puc);
+#endif
 
 void do_interrupt (CPUPPCState *env);
 void cpu_loop_exit(void);
Index: target-ppc/exec.h
===================================================================
RCS file: /cvsroot/qemu/qemu/target-ppc/exec.h,v
retrieving revision 1.10
diff -u -r1.10 exec.h
--- target-ppc/exec.h	13 Mar 2005 17:01:22 -0000	1.10
+++ target-ppc/exec.h	1 May 2005 18:27:56 -0000
@@ -20,6 +20,7 @@
 #if !defined (__PPC_H__)
 #define __PPC_H__
 
+#include "config.h"
 #include "dyngen-exec.h"
 
 register struct CPUPPCState *env asm(AREG0);
Index: target-sparc/cpu.h
===================================================================
RCS file: /cvsroot/qemu/qemu/target-sparc/cpu.h,v
retrieving revision 1.17
diff -u -r1.17 cpu.h
--- target-sparc/cpu.h	17 Apr 2005 19:16:13 -0000	1.17
+++ target-sparc/cpu.h	1 May 2005 18:27:56 -0000
@@ -194,8 +194,7 @@
 	cpu_set_cwp(env, _tmp & PSR_CWP & (NWINDOWS - 1));		\
     } while (0)
 
-struct siginfo;
-int cpu_sparc_signal_handler(int hostsignum, struct siginfo *info, void *puc);
+int cpu_sparc_signal_handler(int hostsignum, siginfo_t *info, void *puc);
 
 #define TARGET_PAGE_BITS 12 /* 4k */
 #include "cpu-all.h"
Index: target-sparc/exec.h
===================================================================
RCS file: /cvsroot/qemu/qemu/target-sparc/exec.h,v
retrieving revision 1.10
diff -u -r1.10 exec.h
--- target-sparc/exec.h	13 Feb 2005 19:02:42 -0000	1.10
+++ target-sparc/exec.h	1 May 2005 18:27:56 -0000
@@ -1,5 +1,6 @@
 #ifndef EXEC_SPARC_H
 #define EXEC_SPARC_H 1
+#include "config.h"
 #include "dyngen-exec.h"
 
 register struct CPUSPARCState *env asm(AREG0);
Index: target-sparc/op_helper.c
===================================================================
RCS file: /cvsroot/qemu/qemu/target-sparc/op_helper.c,v
retrieving revision 1.12
diff -u -r1.12 op_helper.c
--- target-sparc/op_helper.c	6 Apr 2005 20:44:48 -0000	1.12
+++ target-sparc/op_helper.c	1 May 2005 18:27:56 -0000
@@ -1,3 +1,4 @@
+#include <math.h>
 #include "exec.h"
 
 //#define DEBUG_MMU
