Blame SOURCES/0001-eal-abstract-away-the-auxiliary-vector.patch

a6040a
From 2ed9bf330709e75c2f066f6ec13ece1ecdb4e9db Mon Sep 17 00:00:00 2001
a6040a
From: Aaron Conole <aconole@redhat.com>
a6040a
Date: Mon, 2 Apr 2018 14:24:34 -0400
a6040a
Subject: [PATCH] eal: abstract away the auxiliary vector
a6040a
a6040a
Rather than attempting to load the contents of the auxv directly,
a6040a
prefer to use an exposed API - and if that doesn't exist then attempt
a6040a
to load the vector.  This is because on some systems, when a user
a6040a
is downgraded, the /proc/self/auxv file retains the old ownership
a6040a
and permissions.  The original method of /proc/self/auxv is retained.
a6040a
a6040a
This also removes a potential abort() in the code when compiled with
a6040a
NDEBUG.  A quick parse of the code shows that many (if not all) of
a6040a
the CPU flag parsing isn't used internally, so it should be okay.
a6040a
a6040a
Signed-off-by: Aaron Conole <aconole@redhat.com>
a6040a
Signed-off-by: Timothy Redaelli <tredaelli@redhat.com>
a6040a
---
a6040a
 lib/librte_eal/common/arch/arm/rte_cpuflags.c | 20 +----
a6040a
 .../common/arch/ppc_64/rte_cpuflags.c         | 15 +---
a6040a
 lib/librte_eal/common/eal_common_cpuflags.c   | 79 +++++++++++++++++++
a6040a
 .../common/include/generic/rte_cpuflags.h     | 21 +++++
a6040a
 4 files changed, 106 insertions(+), 29 deletions(-)
a6040a
a6040a
diff --git a/lib/librte_eal/common/arch/arm/rte_cpuflags.c b/lib/librte_eal/common/arch/arm/rte_cpuflags.c
a6040a
index 390a19a26..caf3dc83a 100644
a6040a
--- a/lib/librte_eal/common/arch/arm/rte_cpuflags.c
a6040a
+++ b/lib/librte_eal/common/arch/arm/rte_cpuflags.c
a6040a
@@ -105,22 +105,10 @@ const struct feature_entry rte_cpu_feature_table[] = {
a6040a
 static void
a6040a
 rte_cpu_get_features(hwcap_registers_t out)
a6040a
 {
a6040a
-	int auxv_fd;
a6040a
-	_Elfx_auxv_t auxv;
a6040a
-
a6040a
-	auxv_fd = open("/proc/self/auxv", O_RDONLY);
a6040a
-	assert(auxv_fd != -1);
a6040a
-	while (read(auxv_fd, &auxv, sizeof(auxv)) == sizeof(auxv)) {
a6040a
-		if (auxv.a_type == AT_HWCAP) {
a6040a
-			out[REG_HWCAP] = auxv.a_un.a_val;
a6040a
-		} else if (auxv.a_type == AT_HWCAP2) {
a6040a
-			out[REG_HWCAP2] = auxv.a_un.a_val;
a6040a
-		} else if (auxv.a_type == AT_PLATFORM) {
a6040a
-			if (!strcmp((const char *)auxv.a_un.a_val, PLATFORM_STR))
a6040a
-				out[REG_PLATFORM] = 0x0001;
a6040a
-		}
a6040a
-	}
a6040a
-	close(auxv_fd);
a6040a
+	out[REG_HWCAP] = rte_cpu_getauxval(AT_HWCAP);
a6040a
+	out[REG_HWCAP2] = rte_cpu_getauxval(AT_HWCAP2);
a6040a
+	if (!rte_cpu_strcmp_auxval(AT_PLATFORM, PLATFORM_STR))
a6040a
+		out[REG_PLATFORM] = 0x0001;
a6040a
 }
a6040a
 
a6040a
 /*
a6040a
diff --git a/lib/librte_eal/common/arch/ppc_64/rte_cpuflags.c b/lib/librte_eal/common/arch/ppc_64/rte_cpuflags.c
a6040a
index 970a61c5e..e7a82452b 100644
a6040a
--- a/lib/librte_eal/common/arch/ppc_64/rte_cpuflags.c
a6040a
+++ b/lib/librte_eal/common/arch/ppc_64/rte_cpuflags.c
a6040a
@@ -104,19 +104,8 @@ const struct feature_entry rte_cpu_feature_table[] = {
a6040a
 static void
a6040a
 rte_cpu_get_features(hwcap_registers_t out)
a6040a
 {
a6040a
-	int auxv_fd;
a6040a
-	Elf64_auxv_t auxv;
a6040a
-
a6040a
-	auxv_fd = open("/proc/self/auxv", O_RDONLY);
a6040a
-	assert(auxv_fd != -1);
a6040a
-	while (read(auxv_fd, &auxv,
a6040a
-		sizeof(Elf64_auxv_t)) == sizeof(Elf64_auxv_t)) {
a6040a
-		if (auxv.a_type == AT_HWCAP)
a6040a
-			out[REG_HWCAP] = auxv.a_un.a_val;
a6040a
-		else if (auxv.a_type == AT_HWCAP2)
a6040a
-			out[REG_HWCAP2] = auxv.a_un.a_val;
a6040a
-	}
a6040a
-	close(auxv_fd);
a6040a
+	out[REG_HWCAP] = rte_cpu_getauxval(AT_HWCAP);
a6040a
+	out[REG_HWCAP2] = rte_cpu_getauxval(AT_HWCAP2);
a6040a
 }
a6040a
 
a6040a
 /*
a6040a
diff --git a/lib/librte_eal/common/eal_common_cpuflags.c b/lib/librte_eal/common/eal_common_cpuflags.c
a6040a
index 3a055f7c7..a09667563 100644
a6040a
--- a/lib/librte_eal/common/eal_common_cpuflags.c
a6040a
+++ b/lib/librte_eal/common/eal_common_cpuflags.c
a6040a
@@ -2,11 +2,90 @@
a6040a
  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
a6040a
  */
a6040a
 
a6040a
+#include <elf.h>
a6040a
+#include <fcntl.h>
a6040a
 #include <stdio.h>
a6040a
+#include <string.h>
a6040a
+#include <sys/stat.h>
a6040a
+#include <sys/types.h>
a6040a
+#include <unistd.h>
a6040a
+
a6040a
+#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
a6040a
+#if __GLIBC_PREREQ(2, 16)
a6040a
+#include <sys/auxv.h>
a6040a
+#define HAS_AUXV 1
a6040a
+#endif
a6040a
+#endif
a6040a
 
a6040a
 #include <rte_common.h>
a6040a
 #include <rte_cpuflags.h>
a6040a
 
a6040a
+#ifndef HAS_AUXV
a6040a
+static unsigned long
a6040a
+getauxval(unsigned long type)
a6040a
+{
a6040a
+	errno = ENOTSUP;
a6040a
+	return 0;
a6040a
+}
a6040a
+#endif
a6040a
+
a6040a
+#ifdef RTE_ARCH_64
a6040a
+typedef Elf64_auxv_t Internal_Elfx_auxv_t;
a6040a
+#else
a6040a
+typedef Elf32_auxv_t Internal_Elfx_auxv_t;
a6040a
+#endif
a6040a
+
a6040a
+
a6040a
+/**
a6040a
+ * Provides a method for retrieving values from the auxiliary vector and
a6040a
+ * possibly running a string comparison.
a6040a
+ *
a6040a
+ * @return Always returns a result.  When the result is 0, check errno
a6040a
+ * to see if an error occurred during processing.
a6040a
+ */
a6040a
+static unsigned long
a6040a
+_rte_cpu_getauxval(unsigned long type, const char *str)
a6040a
+{
a6040a
+	unsigned long val;
a6040a
+
a6040a
+	errno = 0;
a6040a
+	val = getauxval(type);
a6040a
+
a6040a
+	if (!val && (errno == ENOTSUP || errno == ENOENT)) {
a6040a
+		int auxv_fd = open("/proc/self/auxv", O_RDONLY);
a6040a
+		Internal_Elfx_auxv_t auxv;
a6040a
+
a6040a
+		if (auxv_fd == -1)
a6040a
+			return 0;
a6040a
+
a6040a
+		errno = ENOENT;
a6040a
+		while (read(auxv_fd, &auxv, sizeof(auxv)) == sizeof(auxv)) {
a6040a
+			if (auxv.a_type == type) {
a6040a
+				errno = 0;
a6040a
+				val = auxv.a_un.a_val;
a6040a
+				if (str)
a6040a
+					val = strcmp((const char *)val, str);
a6040a
+				break;
a6040a
+			}
a6040a
+		}
a6040a
+		close(auxv_fd);
a6040a
+	}
a6040a
+
a6040a
+	return val;
a6040a
+}
a6040a
+
a6040a
+unsigned long
a6040a
+rte_cpu_getauxval(unsigned long type)
a6040a
+{
a6040a
+	return _rte_cpu_getauxval(type, NULL);
a6040a
+}
a6040a
+
a6040a
+int
a6040a
+rte_cpu_strcmp_auxval(unsigned long type, const char *str)
a6040a
+{
a6040a
+	return _rte_cpu_getauxval(type, str);
a6040a
+}
a6040a
+
a6040a
 /**
a6040a
  * Checks if the machine is adequate for running the binary. If it is not, the
a6040a
  * program exits with status 1.
a6040a
diff --git a/lib/librte_eal/common/include/generic/rte_cpuflags.h b/lib/librte_eal/common/include/generic/rte_cpuflags.h
a6040a
index 8d31687d8..156ea0029 100644
a6040a
--- a/lib/librte_eal/common/include/generic/rte_cpuflags.h
a6040a
+++ b/lib/librte_eal/common/include/generic/rte_cpuflags.h
a6040a
@@ -64,4 +64,25 @@ rte_cpu_check_supported(void);
a6040a
 int
a6040a
 rte_cpu_is_supported(void);
a6040a
 
a6040a
+/**
a6040a
+ * This function attempts to retrieve a value from the auxiliary vector.
a6040a
+ * If it is unsuccessful, the result will be 0, and errno will be set.
a6040a
+ *
a6040a
+ * @return A value from the auxiliary vector.  When the value is 0, check
a6040a
+ * errno to determine if an error occurred.
a6040a
+ */
a6040a
+unsigned long
a6040a
+rte_cpu_getauxval(unsigned long type);
a6040a
+
a6040a
+/**
a6040a
+ * This function retrieves a value from the auxiliary vector, and compares it
a6040a
+ * as a string against the value retrieved.
a6040a
+ *
a6040a
+ * @return The result of calling strcmp() against the value retrieved from
a6040a
+ * the auxiliary vector.  When the value is 0 (meaning a match is found),
a6040a
+ * check errno to determine if an error occurred.
a6040a
+ */
a6040a
+int
a6040a
+rte_cpu_strcmp_auxval(unsigned long type, const char *str);
a6040a
+
a6040a
 #endif /* _RTE_CPUFLAGS_H_ */
a6040a
-- 
a6040a
2.17.0
a6040a