1From a0f53e1dbb3851bb0f0efcfdbd565b05e4be9cac Mon Sep 17 00:00:00 2001
2From: =?UTF-8?q?Andreas=20M=C3=BCller?= <schnitzeltony@googlemail.com>
3Date: Thu, 23 Aug 2012 18:10:57 +0200
4Subject: [PATCH 1/2] ARM: qemu related workarounds in cpu features detection
5 code
6MIME-Version: 1.0
7Content-Type: text/plain; charset=UTF-8
8Content-Transfer-Encoding: 8bit
9
10This was ported from meta-oe's patch [1]. The original pixman patch is found
11at [2].
12
13[1] http://cgit.openembedded.org/meta-openembedded/tree/meta-oe/recipes-graphics/xorg-lib/pixman-0.26.2/0001-ARM-qemu-related-workarounds-in-cpu-features-detecti.patch
14[2] http://lists.freedesktop.org/archives/pixman/2011-January/000906.html
15
16Upstream-Status: Inappropriate [other] qemu fix
17
18Signed-off-by: Andreas Müller <schnitzeltony@googlemail.com>
19---
20 pixman/pixman-arm.c |   82 ++++++++++++++++++++++++++++++++++++++++----------
21 1 files changed, 65 insertions(+), 17 deletions(-)
22
23diff --git a/pixman/pixman-arm.c b/pixman/pixman-arm.c
24index 23374e4..d98bda6 100644
25--- a/pixman/pixman-arm.c
26+++ b/pixman/pixman-arm.c
27@@ -129,16 +129,35 @@ detect_cpu_features (void)
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/mman.h>
31+#include <sys/utsname.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <elf.h>
35
36+/*
37+ * The whole CPU capabilities detection is a bit ugly: when running in
38+ * userspace qemu, we see /proc/self/auxv from the host system. To make
39+ * everything even worse, the size of each value is 64-bit when running
40+ * on a 64-bit host system. So the data is totally bogus because we expect
41+ * 32-bit values. As AT_PLATFORM value is used as a pointer, it may cause
42+ * segfault (null pointer dereference on x86-64 host). So in order to be
43+ * on a safe side, we require that AT_PLATFORM value is found only once,
44+ * and it has non-zero value (this is still not totally reliable for a big
45+ * endian 64-bit host system running qemu and may theoretically fail).
46+ */
47+#define ARM_HWCAP_VFP 64
48+#define ARM_HWCAP_IWMMXT 512
49+#define ARM_HWCAP_NEON 4096
50+
51 static arm_cpu_features_t
52 detect_cpu_features (void)
53 {
54     arm_cpu_features_t features = 0;
55     Elf32_auxv_t aux;
56     int fd;
57+    uint32_t hwcap = 0;
58+    const char *plat = NULL;
59+    int plat_cnt = 0;
60
61     fd = open ("/proc/self/auxv", O_RDONLY);
62     if (fd >= 0)
63@@ -147,32 +166,61 @@ detect_cpu_features (void)
64 	{
65 	    if (aux.a_type == AT_HWCAP)
66 	    {
67-		uint32_t hwcap = aux.a_un.a_val;
68-
69-		/* hardcode these values to avoid depending on specific
70-		 * versions of the hwcap header, e.g. HWCAP_NEON
71-		 */
72-		if ((hwcap & 64) != 0)
73-		    features |= ARM_VFP;
74-		if ((hwcap & 512) != 0)
75-		    features |= ARM_IWMMXT;
76-		/* this flag is only present on kernel 2.6.29 */
77-		if ((hwcap & 4096) != 0)
78-		    features |= ARM_NEON;
79+		hwcap = aux.a_un.a_val;
80 	    }
81 	    else if (aux.a_type == AT_PLATFORM)
82 	    {
83-		const char *plat = (const char*) aux.a_un.a_val;
84-
85-		if (strncmp (plat, "v7l", 3) == 0)
86+		plat = (const char*) aux.a_un.a_val;
87+		plat_cnt++;
88+	    }
89+	}
90+	close (fd);
91+	if (plat == NULL || plat_cnt != 1 || *plat != 'v')
92+	{
93+	    /*
94+	     * Something seems to be really wrong, most likely we are
95+	     * running under qemu. Let's use machine type from "uname" for
96+	     * CPU capabilities detection:
97+	     * http://www.mail-archive.com/qemu-devel at nongnu.org/msg22212.html
98+	     */
99+	    struct utsname u;
100+	    hwcap = 0; /* clear hwcap, because it is bogus */
101+	    if (uname (&u) == 0)
102+	    {
103+		if (strcmp (u.machine, "armv7l") == 0)
104+		{
105 		    features |= (ARM_V7 | ARM_V6);
106-		else if (strncmp (plat, "v6l", 3) == 0)
107+		    hwcap |= ARM_HWCAP_VFP;  /* qemu is supposed to emulate vfp */
108+		    hwcap |= ARM_HWCAP_NEON; /* qemu is supposed to emulate neon */
109+		}
110+		else if (strcmp (u.machine, "armv6l") == 0)
111+		{
112 		    features |= ARM_V6;
113+		    hwcap |= ARM_HWCAP_VFP;  /* qemu is supposed to emulate vfp */
114+		}
115 	    }
116 	}
117-	close (fd);
118+	else if (strncmp (plat, "v7l", 3) == 0)
119+	{
120+	    features |= (ARM_V7 | ARM_V6);
121+	}
122+	else if (strncmp (plat, "v6l", 3) == 0)
123+	{
124+	    features |= ARM_V6;
125+	}
126     }
127
128+    /* hardcode these values to avoid depending on specific
129+     * versions of the hwcap header, e.g. HWCAP_NEON
130+     */
131+    if ((hwcap & ARM_HWCAP_VFP) != 0)
132+        features |= ARM_VFP;
133+    if ((hwcap & ARM_HWCAP_IWMMXT) != 0)
134+        features |= ARM_IWMMXT;
135+    /* this flag is only present on kernel 2.6.29 */
136+    if ((hwcap & ARM_HWCAP_NEON) != 0)
137+        features |= ARM_NEON;
138+
139     return features;
140 }
141
142--
1431.7.6.5
144
145