[Pixman] [PATCH 3/3] ARM: qemu related workarounds in cpu features detection code

Siarhei Siamashka siarhei.siamashka at gmail.com
Tue Jan 11 08:10:39 PST 2011


From: Siarhei Siamashka <siarhei.siamashka at nokia.com>

---
 pixman/pixman-cpu.c |   67 +++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 55 insertions(+), 12 deletions(-)

diff --git a/pixman/pixman-cpu.c b/pixman/pixman-cpu.c
index 70253d1..06ca69d 100644
--- a/pixman/pixman-cpu.c
+++ b/pixman/pixman-cpu.c
@@ -251,6 +251,7 @@ pixman_have_arm_neon (void)
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
+#include <sys/utsname.h>
 #include <fcntl.h>
 #include <string.h>
 #include <elf.h>
@@ -262,11 +263,25 @@ static pixman_bool_t arm_has_neon = FALSE;
 static pixman_bool_t arm_has_iwmmxt = FALSE;
 static pixman_bool_t arm_tests_initialized = FALSE;
 
+/*
+ * The whole CPU capabilities detection is a bit ugly: when running in
+ * userspace qemu, we see /proc/self/auxv from the host system. To make
+ * everything even worse, the size of each value is 64-bit when running
+ * on a 64-bit host system. So the data is totally bogus because we expect
+ * 32-bit values. As AT_PLATFORM value is used as a pointer, it may cause
+ * segfault (null pointer dereference on x86-64 host). So in order to be
+ * on a safe side, we require that AT_PLATFORM value is found only once,
+ * and it has non-zero value (this is still not totally reliable for a big
+ * endian 64-bit host system running qemu and may theoretically fail).
+ */
 static void
 pixman_arm_read_auxv ()
 {
     int fd;
     Elf32_auxv_t aux;
+    uint32_t hwcap = 0;
+    const char *plat = NULL;
+    int plat_cnt = 0;
 
     fd = open ("/proc/self/auxv", O_RDONLY);
     if (fd >= 0)
@@ -275,32 +290,60 @@ pixman_arm_read_auxv ()
 	{
 	    if (aux.a_type == AT_HWCAP)
 	    {
-		uint32_t hwcap = aux.a_un.a_val;
-		/* hardcode these values to avoid depending on specific
-		 * versions of the hwcap header, e.g. HWCAP_NEON
-		 */
-		arm_has_vfp = (hwcap & 64) != 0;
-		arm_has_iwmmxt = (hwcap & 512) != 0;
-		/* this flag is only present on kernel 2.6.29 */
-		arm_has_neon = (hwcap & 4096) != 0;
+		hwcap = aux.a_un.a_val;
 	    }
 	    else if (aux.a_type == AT_PLATFORM)
 	    {
-		const char *plat = (const char*) aux.a_un.a_val;
-		if (strncmp (plat, "v7l", 3) == 0)
+		plat = (const char*) aux.a_un.a_val;
+		plat_cnt++;
+	    }
+	}
+	close (fd);
+
+	if (plat == NULL || plat_cnt != 1 || *plat != 'v')
+	{
+	    /*
+	     * Something seems to be really wrong, most likely we are
+	     * running under qemu. Let's use machine type from "uname" for
+	     * CPU capabilities detection:
+	     * http://www.mail-archive.com/qemu-devel@nongnu.org/msg22212.html
+	     */
+	    struct utsname u;
+	    hwcap = 0; /* clear hwcap, because it is bogus */
+	    if (uname (&u) == 0)
+	    {
+		if (strcmp (u.machine, "armv7l") == 0)
 		{
 		    arm_has_v7 = TRUE;
 		    arm_has_v6 = TRUE;
+		    hwcap |= 64; /* qemu is supposed to emulate vfp */
+		    hwcap |= 4096; /* qemu is supposed to emulate neon */
 		}
-		else if (strncmp (plat, "v6l", 3) == 0)
+		else if (strcmp (u.machine, "armv6l") == 0)
 		{
 		    arm_has_v6 = TRUE;
+		    hwcap |= 64; /* qemu is supposed to emulate vfp */
 		}
 	    }
 	}
-	close (fd);
+	else if (strncmp (plat, "v7l", 3) == 0)
+	{
+	    arm_has_v7 = TRUE;
+	    arm_has_v6 = TRUE;
+	}
+	else if (strncmp (plat, "v6l", 3) == 0)
+	{
+	    arm_has_v6 = TRUE;
+	}
     }
 
+    /* hardcode these values to avoid depending on specific
+     * versions of the hwcap header, e.g. HWCAP_NEON
+     */
+    arm_has_vfp = (hwcap & 64) != 0;
+    arm_has_iwmmxt = (hwcap & 512) != 0;
+    arm_has_neon = (hwcap & 4096) != 0;
+
     arm_tests_initialized = TRUE;
 }
 
-- 
1.7.2.2



More information about the Pixman mailing list