From e2fc63ba3e0f33018a2b0623e50feca92d234556 Mon Sep 17 00:00:00 2001
From: Alexander von Gluck IV <kallisti5@unixzen.com>
Date: Tue, 18 May 2021 16:49:20 -0500
Subject: [PATCH] haiku: fixes and patches, rebased from qemu 3.x

---
 disas/nanomips.h         |  4 ++++
 os-posix.c               | 10 ++++++++++
 slirp/src/tftp.c         |  5 +++++
 target/mips/mips-semi.c  |  4 ++++
 util/main-loop.c         |  2 ++
 util/notify.c            |  4 ++++
 util/qemu-thread-posix.c |  4 ++++
 7 files changed, 33 insertions(+)

diff --git a/disas/nanomips.h b/disas/nanomips.h
index a0a222530..8b8add814 100644
--- a/disas/nanomips.h
+++ b/disas/nanomips.h
@@ -25,10 +25,14 @@
 
 #include <string>
 
+#ifndef __HAIKU__
 typedef int64_t int64;
 typedef uint64_t uint64;
 typedef uint32_t uint32;
 typedef uint16_t uint16;
+#else
+#include <SupportDefs.h>
+#endif
 
 namespace img
 {
diff --git a/os-posix.c b/os-posix.c
index a6846f51c..cc0c80e8c 100644
--- a/os-posix.c
+++ b/os-posix.c
@@ -42,6 +42,16 @@
 #include <sys/prctl.h>
 #endif
 
+#ifdef __HAIKU__
+#ifndef MCL_CURRENT
+#define MCL_CURRENT 8192
+#endif
+#ifndef MCL_FUTURE
+#define MCL_FUTURE 16384
+#endif
+#endif
+
+
 /*
  * Must set all three of these at once.
  * Legal combinations are              unset   by name   by uid
diff --git a/slirp/src/tftp.c b/slirp/src/tftp.c
index c6950ee10..5e1cad3ec 100644
--- a/slirp/src/tftp.c
+++ b/slirp/src/tftp.c
@@ -29,6 +29,11 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+
 static inline int tftp_session_in_use(struct tftp_session *spt)
 {
     return (spt->slirp != NULL);
diff --git a/target/mips/mips-semi.c b/target/mips/mips-semi.c
index 6de60fa6d..717a24787 100644
--- a/target/mips/mips-semi.c
+++ b/target/mips/mips-semi.c
@@ -76,6 +76,7 @@ enum UHIOpenFlags {
 
 /* Errno values taken from asm-mips/errno.h */
 static uint16_t host_to_mips_errno[] = {
+#ifndef __HAIKU__
     [ENAMETOOLONG] = 78,
 #ifdef EOVERFLOW
     [EOVERFLOW]    = 79,
@@ -83,6 +84,9 @@ static uint16_t host_to_mips_errno[] = {
 #ifdef ELOOP
     [ELOOP]        = 90,
 #endif
+#else
+    NULL
+#endif
 };
 
 static int errno_mips(int err)
diff --git a/util/main-loop.c b/util/main-loop.c
index 5188ff654..62cdbf6dd 100644
--- a/util/main-loop.c
+++ b/util/main-loop.c
@@ -97,7 +97,9 @@ static int qemu_signal_init(Error **errp)
      */
     sigemptyset(&set);
     sigaddset(&set, SIG_IPI);
+    #ifdef SIGIO
     sigaddset(&set, SIGIO);
+    #endif
     sigaddset(&set, SIGALRM);
     sigaddset(&set, SIGBUS);
     /* SIGINT cannot be handled via signalfd, so that ^C can be used
diff --git a/util/notify.c b/util/notify.c
index 76bab212a..7c1ea8405 100644
--- a/util/notify.c
+++ b/util/notify.c
@@ -67,6 +67,10 @@ int notifier_with_return_list_notify(NotifierWithReturnList *list, void *data)
     int ret = 0;
 
     QLIST_FOREACH_SAFE(notifier, &list->notifiers, node, next) {
+        #ifdef __HAIKU__
+        if(notifier->notify == NULL) break;
+        #endif
+
         ret = notifier->notify(notifier, data);
         if (ret != 0) {
             break;
diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
index dcff5e7c5..2e8271be5 100644
--- a/util/qemu-thread-posix.c
+++ b/util/qemu-thread-posix.c
@@ -311,7 +311,11 @@ int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
         }
     }
     if (rc < 0) {
+        #ifndef __HAIKU__
         error_exit(errno, __func__);
+        #else
+        return -1;
+        #endif
     }
     return 0;
 #endif
-- 
2.30.2

