[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gcrypt-devel
Subject:    [PATCH] Parse /proc/cpuinfo for ARM HW features
From:       Jussi Kivilinna <jussi.kivilinna () iki ! fi>
Date:       2013-12-29 17:41:15
Message-ID: 20131229174115.17873.57578.stgit () localhost6 ! localdomain6
[Download RAW message or body]

* src/hwf-arm.c [__linux__] (HAS_PROC_CPUINFO)
(detect_arm_proc_cpuinfo): New.
(_gcry_hwf_detect_arm) [HAS_PROC_CPUINFO]: Check '/proc/cpuinfo' for
HW features.
--

Some Linux platforms (read: Android) block read access to '/proc/self/auxv',
which prevents NEON HW detection. Patch adds alternative check which parses
'/proc/cpuinfo' which should be accessable by Android applications.

Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
---
 src/hwf-arm.c |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 2 deletions(-)

diff --git a/src/hwf-arm.c b/src/hwf-arm.c
index 8071894..dbbb607 100644
--- a/src/hwf-arm.c
+++ b/src/hwf-arm.c
@@ -32,6 +32,7 @@
 #endif
 
 #undef HAS_SYS_AT_HWCAP
+#undef HAS_PROC_CPUINFO
 #ifdef __linux__
 
 #define HAS_SYS_AT_HWCAP 1
@@ -94,6 +95,54 @@ detect_arm_at_hwcap(void)
   return features;
 }
 
+#define HAS_PROC_CPUINFO 1
+
+static unsigned int
+detect_arm_proc_cpuinfo(void)
+{
+  char buf[1024]; /* large enough */
+  char *str_features, *str_neon;
+  FILE *f;
+  int readlen, i;
+  static int cpuinfo_initialized = 0;
+  static unsigned int stored_cpuinfo_features;
+
+  if (cpuinfo_initialized)
+    return stored_cpuinfo_features;
+
+  f = fopen("/proc/cpuinfo", "r");
+  if (!f)
+    return 0;
+
+  memset (buf, 0, sizeof(buf));
+  readlen = fread (buf, 1, sizeof(buf), f);
+  fclose (f);
+  if (readlen <= 0 || readlen > sizeof(buf))
+    return 0;
+
+  buf[sizeof(buf) - 1] = '\0';
+
+  cpuinfo_initialized = 1;
+  stored_cpuinfo_features = 0;
+
+  /* Find features line. */
+  str_features = strstr(buf, "Features");
+  if (!str_features)
+    return stored_cpuinfo_features;
+
+  /* Lines to strings. */
+  for (i = 0; i < sizeof(buf); i++)
+    if (buf[i] == '\n')
+      buf[i] = '\0';
+
+  /* Check for NEON. */
+  str_neon = strstr(str_features, " neon");
+  if (str_neon && (str_neon[5] == ' ' || str_neon[5] == '\0'))
+    stored_cpuinfo_features |= HWF_ARM_NEON;
+
+  return stored_cpuinfo_features;
+}
+
 #endif /* __linux__ */
 
 unsigned int
@@ -103,8 +152,10 @@ _gcry_hwf_detect_arm (void)
 
 #if defined (HAS_SYS_AT_HWCAP)
   ret |= detect_arm_at_hwcap ();
-#else
-  ret |= 0;
+#endif
+
+#if defined (HAS_PROC_CPUINFO)
+  ret |= detect_arm_proc_cpuinfo ();
 #endif
 
 #if defined(__ARM_NEON__) && defined(ENABLE_NEON_SUPPORT)


_______________________________________________
Gcrypt-devel mailing list
Gcrypt-devel@gnupg.org
http://lists.gnupg.org/mailman/listinfo/gcrypt-devel
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic