From 0b866f3673eee3d79fc92fc911f8135e16a25aa6 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Sat, 4 Apr 2020 22:07:02 +0200
Subject: [PATCH 4/4] WIP libfbsdwrap

---
 etc/defaults/make.conf       |  2 +
 lib/Makefile                 |  5 +++
 lib/libfbsdwrap/Makefile     | 17 ++++++++
 lib/libfbsdwrap/Versions.def |  5 +++
 lib/libfbsdwrap/cpuset.c     | 57 +++++++++++++++++++++++++
 lib/libfbsdwrap/fbsdwrap.h   | 42 +++++++++++++++++++
 lib/libfbsdwrap/mkfbsdwrap   | 80 ++++++++++++++++++++++++++++++++++++
 7 files changed, 208 insertions(+)
 create mode 100644 lib/libfbsdwrap/Makefile
 create mode 100644 lib/libfbsdwrap/Versions.def
 create mode 100644 lib/libfbsdwrap/cpuset.c
 create mode 100644 lib/libfbsdwrap/fbsdwrap.h
 create mode 100644 lib/libfbsdwrap/mkfbsdwrap

diff --git a/etc/defaults/make.conf b/etc/defaults/make.conf
index 37f7d4220f..8fdd49318f 100644
--- a/etc/defaults/make.conf
+++ b/etc/defaults/make.conf
@@ -194,3 +194,5 @@ STATIC_LOCALES=UTF8
 # installed from DPorts together with these m4 files.
 #
 #SENDMAIL_CF_DIR=	/usr/local/share/sendmail/cf
+
+#NV_DIST=/tmp/NVIDIA-FreeBSD-x86_64-440.64_dfly
diff --git a/lib/Makefile b/lib/Makefile
index 92fb25633b..745678be9a 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -60,6 +60,7 @@ SUBDIR=	${SUBDIR_ORDERED} \
 	libefivar \
 	libevtr \
 	libexpat \
+	${_libfbsdwrap} \
 	libfsid \
 	libftpio \
 	libhammer \
@@ -102,4 +103,8 @@ _libnetgraph=	libnetgraph7
 _libnetgraph=	libnetgraph
 .endif
 
+.if defined(NV_DIST)
+_libfbsdwrap=	libfbsdwrap
+.endif
+
 .include <bsd.subdir.mk>
diff --git a/lib/libfbsdwrap/Makefile b/lib/libfbsdwrap/Makefile
new file mode 100644
index 0000000000..65f9024325
--- /dev/null
+++ b/lib/libfbsdwrap/Makefile
@@ -0,0 +1,17 @@
+LIB=		fbsdwrap
+SRCS=		fbsdwrap.s
+SRCS+=		cpuset.c
+SHLIB_MAJOR=	1
+SYMBOL_MAPS=	Symbol.map
+VERSION_DEF=	${.CURDIR}/Versions.def
+LDADD+=		-lm -lpthread
+DPADD+=		${LIBM} ${LIBPTHREAD}
+CFLAGS+=	-I${.CURDIR}
+
+fbsdwrap.s!
+	sh ${.CURDIR}/mkfbsdwrap ${NV_DIST} wrap > ${.TARGET}
+
+Symbol.map!
+	sh ${.CURDIR}/mkfbsdwrap ${NV_DIST} map > ${.TARGET}
+
+.include <bsd.lib.mk>
diff --git a/lib/libfbsdwrap/Versions.def b/lib/libfbsdwrap/Versions.def
new file mode 100644
index 0000000000..a404cd64d9
--- /dev/null
+++ b/lib/libfbsdwrap/Versions.def
@@ -0,0 +1,5 @@
+FBSD_1.0 {
+};
+
+FBSD_1.1 {
+} FBSD_1.0;
diff --git a/lib/libfbsdwrap/cpuset.c b/lib/libfbsdwrap/cpuset.c
new file mode 100644
index 0000000000..6272a9196a
--- /dev/null
+++ b/lib/libfbsdwrap/cpuset.c
@@ -0,0 +1,57 @@
+/*-
+ * Copyright (c) 2020 The DragonFly Project.  All rights reserved.
+ *
+ * This code is derived from software contributed to The DragonFly Project
+ * by Sascha Wildner
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name of The DragonFly Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific, prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/lwp.h>
+#include <sys/sched.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <unistd.h>
+
+int _fbsdwrap_cpuset_getaffinity(int, int, id_t, size_t, cpuset_t *);
+int _fbsdwrap_cpuset_setaffinity(int, int, id_t, size_t, const cpuset_t *);
+
+int
+_fbsdwrap_cpuset_getaffinity(__unused int level, __unused int which, id_t id,
+    __unused size_t cpusetsize, cpuset_t *mask)
+{
+	return lwp_getaffinity(getpid(), id, mask);
+}
+
+int
+_fbsdwrap_cpuset_setaffinity(__unused int level, __unused int which, id_t id,
+    __unused size_t cpusetsize, const cpuset_t *mask)
+{
+	return lwp_setaffinity(getpid(), id, mask);
+}
diff --git a/lib/libfbsdwrap/fbsdwrap.h b/lib/libfbsdwrap/fbsdwrap.h
new file mode 100644
index 0000000000..df4ebddb0d
--- /dev/null
+++ b/lib/libfbsdwrap/fbsdwrap.h
@@ -0,0 +1,42 @@
+/*-
+ * Copyright (c) 2020 The DragonFly Project.  All rights reserved.
+ *
+ * This code is derived from software contributed to The DragonFly Project
+ * by Sascha Wildner
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name of The DragonFly Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific, prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#define	__fbsdsym_compat(sym, impl, verid)		\
+	.symver impl, sym@verid
+
+#define	__fbsdsym_wrap(sym, impl, verid)		\
+	__fbsdsym_compat(sym, impl, verid);		\
+	ENTRY(_fbsdwrap_##sym)				\
+	jmp PIC_PLT(##sym);				\
+	END(_fbsdwrap_##sym)
diff --git a/lib/libfbsdwrap/mkfbsdwrap b/lib/libfbsdwrap/mkfbsdwrap
new file mode 100644
index 0000000000..d2e21bbe21
--- /dev/null
+++ b/lib/libfbsdwrap/mkfbsdwrap
@@ -0,0 +1,80 @@
+#!/bin/sh
+#
+# Copyright (c) 2020 The DragonFly Project.  All rights reserved.
+#
+# This code is derived from software contributed to The DragonFly Project
+# by Antonio Huete Jimenez and Sascha Wildner
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in
+#    the documentation and/or other materials provided with the
+#    distribution.
+# 3. Neither the name of The DragonFly Project nor the names of its
+#    contributors may be used to endorse or promote products derived
+#    from this software without specific, prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
+# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+
+# MISSING __divdi3 __moddi3 __udivdi3 __umoddi3 __xuname cpuset_getid cpuset_setid malloc_usable_size
+
+LOCALFUNCS="cpuset_getaffinity cpuset_setaffinity"
+
+set -e
+
+NV_DIST=$1
+FILETYPE=$2
+
+printf "/* DO NOT EDIT -- this file is automatically generated by mkfbsdwrap. */\n\n"
+
+if [ "${FILETYPE}" = "wrap" ]; then
+	echo "#include <machine/asm.h>"
+	echo "#include \"fbsdwrap.h\""
+	echo ""
+elif [ "${FILETYPE}" = "map" ]; then
+	echo "FBSD_1.0 {"
+fi
+
+find ${NV_DIST}/* -name "*.so.*" | grep -v -e /32/ -e /linux/ | \
+	awk -F: '{print $1;}' | xargs readelf -s | grep FBSD | \
+	grep -w UND | awk '{print $4, $8;}' | sort -u | \
+	while read SYMBOL
+do
+	SYMTYPE=`echo ${SYMBOL} | awk '{print $1;}'`
+	SYMNAME=`echo ${SYMBOL} | awk '{print $2;}' | awk -F@ '{print $1;}'`
+	SYMVER=`echo ${SYMBOL} | awk '{print $2;}' | awk -F@ '{print $2;}'`
+
+	if [ "${SYMTYPE}" = "FUNC" ]; then
+		if [ "${FILETYPE}" = "wrap" ]; then
+			if [ "`echo ${LOCALFUNCS} | grep -w ${SYMNAME}`" = "" ]
+			then
+				echo "__fbsdsym_wrap(${SYMNAME}, _fbsdwrap_${SYMNAME}, ${SYMVER})"
+			else
+				echo "__fbsdsym_compat(${SYMNAME}, _fbsdwrap_${SYMNAME}, ${SYMVER})"
+			fi
+		elif [ "${FILETYPE}" = "map" ]; then
+			echo "	${SYMNAME};"
+		fi
+	fi
+done
+
+if [ "${FILETYPE}" = "map" ]; then
+	echo "};"
+fi
-- 
2.25.0

