Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(189)

Side by Side Diff: src/cpu.cc

Issue 23401002: Fix the CPU feature detection. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Improve cpuinfo parsing. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/cpu.h ('k') | src/ia32/assembler-ia32.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "cpu.h"
29
30 #include <algorithm>
31 #include <cctype>
32 #include <cstdio>
33 #include <cstdlib>
34 #include <cstring>
35
36 #include "checks.h"
37
38 namespace v8 {
39 namespace internal {
40
41 #if V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
42
43 #if V8_CC_MSVC
44
45 #include <intrin.h> // NOLINT
46
47 #elif defined(__i386__) && defined(__pic__)
48
49 static V8_INLINE(void __cpuid(int cpu_info[4], int info_type)) {
50 __asm__ volatile (
51 "mov %%ebx, %%edi\n"
52 "cpuid\n"
53 "xchg %%edi, %%ebx\n"
54 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
55 : "a"(info_type)
56 );
57 }
58
59 #else // !V8_CC_MSVC || (!defined(__i386__) && !defined(__pic__))
60
61 static void V8_INLINE(__cpuid(int cpu_info[4], int info_type)) {
62 __asm__ volatile (
63 "cpuid \n\t"
64 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
65 : "a"(info_type)
66 );
67 }
68
69 #endif
70
71 #elif V8_HOST_ARCH_ARM || V8_HOST_ARCH_MIPS
72
73 #if V8_HOST_ARCH_ARM
74
75 // See <uapi/asm/hwcap.h> kernel header.
76 /*
77 * HWCAP flags - for elf_hwcap (in kernel) and AT_HWCAP
78 */
79 #define HWCAP_SWP (1 << 0)
80 #define HWCAP_HALF (1 << 1)
81 #define HWCAP_THUMB (1 << 2)
82 #define HWCAP_26BIT (1 << 3) /* Play it safe */
83 #define HWCAP_FAST_MULT (1 << 4)
84 #define HWCAP_FPA (1 << 5)
85 #define HWCAP_VFP (1 << 6)
86 #define HWCAP_EDSP (1 << 7)
87 #define HWCAP_JAVA (1 << 8)
88 #define HWCAP_IWMMXT (1 << 9)
89 #define HWCAP_CRUNCH (1 << 10)
90 #define HWCAP_THUMBEE (1 << 11)
91 #define HWCAP_NEON (1 << 12)
92 #define HWCAP_VFPv3 (1 << 13)
93 #define HWCAP_VFPv3D16 (1 << 14) /* also set for VFPv4-D16 */
94 #define HWCAP_TLS (1 << 15)
95 #define HWCAP_VFPv4 (1 << 16)
96 #define HWCAP_IDIVA (1 << 17)
97 #define HWCAP_IDIVT (1 << 18)
98 #define HWCAP_VFPD32 (1 << 19) /* set if VFP has 32 regs (not 16) */
99 #define HWCAP_IDIV (HWCAP_IDIVA | HWCAP_IDIVT)
100 #define HWCAP_LPAE (1 << 20)
101
102 #define AT_HWCAP 16
103
104 // Read the ELF HWCAP flags by parsing /proc/self/auxv.
105 static uint32_t ReadELFHWCaps() {
106 uint32_t result = 0;
107 FILE* fp = fopen("/proc/self/auxv", "r");
108 if (fp != NULL) {
109 struct { uint32_t tag; uint32_t value; } entry;
110 for (;;) {
111 size_t n = fread(&entry, sizeof(entry), 1, fp);
112 if (n == 0 || (entry.tag == 0 && entry.value == 0)) {
113 break;
114 }
115 if (entry.tag == AT_HWCAP) {
116 result = entry.value;
117 break;
118 }
119 }
120 fclose(fp);
121 }
122 return result;
123 }
124
125 #endif // V8_HOST_ARCH_ARM
126
127 // Extract the information exposed by the kernel via /proc/cpuinfo.
128 class CPUInfo V8_FINAL BASE_EMBEDDED {
129 public:
130 CPUInfo() : datalen_(0) {
131 // Get the size of the cpuinfo file by reading it until the end. This is
132 // required because files under /proc do not always return a valid size
133 // when using fseek(0, SEEK_END) + ftell(). Nor can the be mmap()-ed.
134 static const char PATHNAME[] = "/proc/cpuinfo";
135 FILE* fp = fopen(PATHNAME, "r");
136 if (fp != NULL) {
137 for (;;) {
138 char buffer[256];
139 size_t n = fread(buffer, 1, sizeof(buffer), fp);
140 if (n == 0) {
141 break;
142 }
143 datalen_ += n;
144 }
145 fclose(fp);
146 }
147
148 // Read the contents of the cpuinfo file.
149 data_ = new char[datalen_ + 1];
150 fp = fopen(PATHNAME, "r");
151 if (fp != NULL) {
152 for (size_t offset = 0; offset < datalen_; ) {
153 size_t n = fread(data_ + offset, 1, datalen_ - offset, fp);
154 if (n == 0) {
155 break;
156 }
157 offset += n;
158 }
159 fclose(fp);
160 }
161
162 // Zero-terminate the data.
163 data_[datalen_] = '\0';
164 }
165
166 ~CPUInfo() {
167 delete[] data_;
168 }
169
170 // Extract the content of a the first occurence of a given field in
171 // the content of the cpuinfo file and return it as a heap-allocated
172 // string that must be freed by the caller using delete[].
173 // Return NULL if not found.
174 char* ExtractField(const char* field) const {
175 ASSERT(field != NULL);
176
177 // Look for first field occurence, and ensure it starts the line.
178 size_t fieldlen = strlen(field);
179 char* p = data_;
180 for (;;) {
181 p = strstr(p, field);
182 if (p == NULL) {
183 return NULL;
184 }
185 if (p == data_ || p[-1] == '\n') {
186 break;
187 }
188 p += fieldlen;
189 }
190
191 // Skip to the first colon followed by a space.
192 p = strchr(p + fieldlen, ':');
193 if (p == NULL || !isspace(p[1])) {
194 return NULL;
195 }
196 p += 2;
197
198 // Find the end of the line.
199 char* q = strchr(p, '\n');
200 if (q == NULL) {
201 q = data_ + datalen_;
202 }
203
204 // Copy the line into a heap-allocated buffer.
205 size_t len = q - p;
206 char* result = new char[len + 1];
207 if (result != NULL) {
208 memcpy(result, p, len);
209 result[len] = '\0';
210 }
211 return result;
212 }
213
214 private:
215 char* data_;
216 size_t datalen_;
217 };
218
219
220 // Checks that a space-separated list of items contains one given 'item'.
221 static bool HasListItem(const char* list, const char* item) {
222 ssize_t item_len = strlen(item);
223 const char* p = list;
224 if (p != NULL) {
225 while (*p != '\0') {
226 // Skip whitespace.
227 while (isspace(*p)) ++p;
228
229 // Find end of current list item.
230 const char* q = p;
231 while (*q != '\0' && !isspace(*q)) ++q;
232
233 if (item_len == q - p && memcmp(p, item, item_len) == 0) {
234 return true;
235 }
236
237 // Skip to next item.
238 p = q;
239 }
240 }
241 return false;
242 }
243
244 #endif // V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
245
246 CPU::CPU() : stepping_(0),
247 model_(0),
248 ext_model_(0),
249 family_(0),
250 ext_family_(0),
251 type_(0),
252 implementer_(0),
253 architecture_(0),
254 part_(0),
255 has_fpu_(false),
256 has_cmov_(false),
257 has_sahf_(false),
258 has_mmx_(false),
259 has_sse_(false),
260 has_sse2_(false),
261 has_sse3_(false),
262 has_ssse3_(false),
263 has_sse41_(false),
264 has_sse42_(false),
265 has_idiva_(false),
266 has_neon_(false),
267 has_thumbee_(false),
268 has_vfp_(false),
269 has_vfp3_(false),
270 has_vfp3_d32_(false) {
271 memcpy(vendor_, "Unknown", 8);
272 #if V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
273 int cpu_info[4];
274
275 // __cpuid with an InfoType argument of 0 returns the number of
276 // valid Ids in CPUInfo[0] and the CPU identification string in
277 // the other three array elements. The CPU identification string is
278 // not in linear order. The code below arranges the information
279 // in a human readable form. The human readable order is CPUInfo[1] |
280 // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped
281 // before using memcpy to copy these three array elements to cpu_string.
282 __cpuid(cpu_info, 0);
283 unsigned num_ids = cpu_info[0];
284 std::swap(cpu_info[2], cpu_info[3]);
285 memcpy(vendor_, cpu_info + 1, 12);
286 vendor_[12] = '\0';
287
288 // Interpret CPU feature information.
289 if (num_ids > 0) {
290 __cpuid(cpu_info, 1);
291 stepping_ = cpu_info[0] & 0xf;
292 model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0);
293 family_ = (cpu_info[0] >> 8) & 0xf;
294 type_ = (cpu_info[0] >> 12) & 0x3;
295 ext_model_ = (cpu_info[0] >> 16) & 0xf;
296 ext_family_ = (cpu_info[0] >> 20) & 0xff;
297 has_fpu_ = (cpu_info[3] & 0x00000001) != 0;
298 has_cmov_ = (cpu_info[3] & 0x00008000) != 0;
299 has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
300 has_sse_ = (cpu_info[3] & 0x02000000) != 0;
301 has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
302 has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
303 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
304 has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
305 has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
306 }
307
308 // Query extended IDs.
309 __cpuid(cpu_info, 0x80000000);
310 unsigned num_ext_ids = cpu_info[0];
311
312 // Interpret extended CPU feature information.
313 if (num_ext_ids > 0x80000000) {
314 __cpuid(cpu_info, 0x80000001);
315 // SAHF is always available in compat/legacy mode,
316 // but must be probed in long mode.
317 #if V8_HOST_ARCH_IA32
318 has_sahf_ = true;
319 #else
320 has_sahf_ = (cpu_info[2] & 0x00000001) != 0;
321 #endif
322 }
323 #elif V8_HOST_ARCH_ARM
324 CPUInfo cpu_info;
325
326 // Extract implementor from the "CPU implementer" field.
327 char* implementer = cpu_info.ExtractField("CPU implementer");
328 if (implementer != NULL) {
329 char* end ;
330 implementer_ = strtol(implementer, &end, 0);
331 if (end == implementer) {
332 implementer_ = 0;
333 }
334 delete[] implementer;
335 }
336
337 // Extract part number from the "CPU part" field.
338 char* part = cpu_info.ExtractField("CPU part");
339 if (part != NULL) {
340 char* end ;
341 part_ = strtol(part, &end, 0);
342 if (end == part) {
343 part_ = 0;
344 }
345 delete[] part;
346 }
347
348 // Extract architecture from the "CPU Architecture" field.
349 // The list is well-known, unlike the the output of
350 // the 'Processor' field which can vary greatly.
351 // See the definition of the 'proc_arch' array in
352 // $KERNEL/arch/arm/kernel/setup.c and the 'c_show' function in
353 // same file.
354 char* architecture = cpu_info.ExtractField("CPU architecture");
355 if (architecture != NULL) {
356 char* end;
357 architecture_ = strtol(architecture, &end, 10);
358 if (end == architecture) {
359 architecture_ = 0;
360 }
361 delete[] architecture;
362
363 // Unfortunately, it seems that certain ARMv6-based CPUs
364 // report an incorrect architecture number of 7!
365 //
366 // See http://code.google.com/p/android/issues/detail?id=10812
367 //
368 // We try to correct this by looking at the 'elf_format'
369 // field reported by the 'Processor' field, which is of the
370 // form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
371 // an ARMv6-one. For example, the Raspberry Pi is one popular
372 // ARMv6 device that reports architecture 7.
373 if (architecture_ == 7) {
374 char* processor = cpu_info.ExtractField("Processor");
375 if (HasListItem(processor, "(v6l)")) {
376 architecture_ = 6;
377 }
378 delete[] processor;
379 }
380 }
381
382 // Try to extract the list of CPU features from ELF hwcaps.
383 uint32_t hwcaps = ReadELFHWCaps();
384 if (hwcaps != 0) {
385 has_idiva_ = (hwcaps & HWCAP_IDIVA) != 0;
386 has_neon_ = (hwcaps & HWCAP_NEON) != 0;
387 has_thumbee_ = (hwcaps & HWCAP_THUMBEE) != 0;
388 has_vfp_ = (hwcaps & HWCAP_VFP) != 0;
389 has_vfp3_ = (hwcaps & (HWCAP_VFPv3 | HWCAP_VFPv3D16 | HWCAP_VFPv4)) != 0;
390 has_vfp3_d32_ = (has_vfp3_ && ((hwcaps & HWCAP_VFPv3D16) == 0 ||
391 (hwcaps & HWCAP_VFPD32) != 0));
392 } else {
393 // Try to fallback to "Features" CPUInfo field.
394 char* features = cpu_info.ExtractField("Features");
395 has_idiva_ = HasListItem(features, "idiva");
396 has_neon_ = HasListItem(features, "neon");
397 has_thumbee_ = HasListItem(features, "thumbee");
398 has_vfp_ = HasListItem(features, "vfp");
399 if (HasListItem(features, "vfpv3")) {
400 has_vfp3_ = true;
401 has_vfp3_d32_ = true;
402 } else if (HasListItem(features, "vfpv3d16")) {
403 has_vfp3_ = true;
404 }
405 delete[] features;
406 }
407
408 // Some old kernels will report vfp not vfpv3. Here we make an attempt
409 // to detect vfpv3 by checking for vfp *and* neon, since neon is only
410 // available on architectures with vfpv3. Checking neon on its own is
411 // not enough as it is possible to have neon without vfp.
412 if (has_vfp_ && has_neon_) {
413 has_vfp3_ = true;
414 }
415
416 // VFPv3 implies ARMv7, see ARM DDI 0406B, page A1-6.
417 if (architecture_ < 7 && has_vfp3_) {
418 architecture_ = 7;
419 }
420
421 // ARMv7 implies ThumbEE.
422 if (architecture_ >= 7) {
423 has_thumbee_ = true;
424 }
425
426 // The earliest architecture with ThumbEE is ARMv6T2.
427 if (has_thumbee_ && architecture_ < 6) {
428 architecture_ = 6;
429 }
430
431 // We don't support any FPUs other than VFP.
432 has_fpu_ = has_vfp_;
433 #elif V8_HOST_ARCH_MIPS
434 // Simple detection of FPU at runtime for Linux.
435 // It is based on /proc/cpuinfo, which reveals hardware configuration
436 // to user-space applications. According to MIPS (early 2010), no similar
437 // facility is universally available on the MIPS architectures,
438 // so it's up to individual OSes to provide such.
439 CPUInfo cpu_info;
440 char* cpu_model = cpu_info.ExtractField("cpu model");
441 has_fpu_ = HasListItem(cpu_model, "FPU");
442 delete[] cpu_model;
443 #endif
444 }
445
446 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/cpu.h ('k') | src/ia32/assembler-ia32.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698