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

Side by Side Diff: base/cpu.cc

Issue 79283002: crypto: disable NSS AES-NI support when AVX is disabled by OS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ... Created 7 years 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 | « base/cpu.h ('k') | crypto/nss_util.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/cpu.h" 5 #include "base/cpu.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
11 #include "base/basictypes.h"
11 #include "build/build_config.h" 12 #include "build/build_config.h"
12 13
13 #if defined(ARCH_CPU_X86_FAMILY) 14 #if defined(ARCH_CPU_X86_FAMILY)
14 #if defined(_MSC_VER) 15 #if defined(_MSC_VER)
15 #include <intrin.h> 16 #include <intrin.h>
17 #include <immintrin.h> // For _xgetbv()
16 #endif 18 #endif
17 #endif 19 #endif
18 20
19 namespace base { 21 namespace base {
20 22
21 CPU::CPU() 23 CPU::CPU()
22 : signature_(0), 24 : signature_(0),
23 type_(0), 25 type_(0),
24 family_(0), 26 family_(0),
25 model_(0), 27 model_(0),
26 stepping_(0), 28 stepping_(0),
27 ext_model_(0), 29 ext_model_(0),
28 ext_family_(0), 30 ext_family_(0),
29 has_mmx_(false), 31 has_mmx_(false),
30 has_sse_(false), 32 has_sse_(false),
31 has_sse2_(false), 33 has_sse2_(false),
32 has_sse3_(false), 34 has_sse3_(false),
33 has_ssse3_(false), 35 has_ssse3_(false),
34 has_sse41_(false), 36 has_sse41_(false),
35 has_sse42_(false), 37 has_sse42_(false),
38 has_avx_(false),
39 has_avx_hardware_(false),
36 has_non_stop_time_stamp_counter_(false), 40 has_non_stop_time_stamp_counter_(false),
37 cpu_vendor_("unknown") { 41 cpu_vendor_("unknown") {
38 Initialize(); 42 Initialize();
39 } 43 }
40 44
45 namespace {
46
41 #if defined(ARCH_CPU_X86_FAMILY) 47 #if defined(ARCH_CPU_X86_FAMILY)
42 #ifndef _MSC_VER 48 #ifndef _MSC_VER
43 49
44 #if defined(__pic__) && defined(__i386__) 50 #if defined(__pic__) && defined(__i386__)
45 51
46 void __cpuid(int cpu_info[4], int info_type) { 52 void __cpuid(int cpu_info[4], int info_type) {
47 __asm__ volatile ( 53 __asm__ volatile (
48 "mov %%ebx, %%edi\n" 54 "mov %%ebx, %%edi\n"
49 "cpuid\n" 55 "cpuid\n"
50 "xchg %%edi, %%ebx\n" 56 "xchg %%edi, %%ebx\n"
51 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3]) 57 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
52 : "a"(info_type) 58 : "a"(info_type)
53 ); 59 );
54 } 60 }
55 61
56 void __cpuidex(int cpu_info[4], int info_type, int info_index) {
57 __asm__ volatile (
58 "mov %%ebx, %%edi\n"
59 "cpuid\n"
60 "xchg %%edi, %%ebx\n"
61 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
62 : "a"(info_type), "c"(info_index)
63 );
64 }
65
66 #else 62 #else
67 63
68 void __cpuid(int cpu_info[4], int info_type) { 64 void __cpuid(int cpu_info[4], int info_type) {
69 __asm__ volatile ( 65 __asm__ volatile (
70 "cpuid \n\t" 66 "cpuid \n\t"
71 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3]) 67 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
72 : "a"(info_type) 68 : "a"(info_type)
73 ); 69 );
74 } 70 }
75 71
76 void __cpuidex(int cpu_info[4], int info_type, int info_index) {
77 __asm__ volatile (
78 "cpuid \n\t"
79 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
80 : "a"(info_type), "c"(info_index)
81 );
82 }
83
84 #endif 72 #endif
85 #endif // _MSC_VER 73
74 // _xgetbv returns the value of an Intel Extended Control Register (XCR).
75 // Currently only XCR0 is defined by Intel so |xcr| should always be zero.
76 uint64 _xgetbv(uint32 xcr) {
77 uint32 eax, edx;
78
79 __asm__ volatile ("xgetbv" : "=a" (eax), "=d" (edx) : "c" (xcr));
80 return (static_cast<uint64>(edx) << 32) | eax;
81 }
82
83 #endif // !_MSC_VER
86 #endif // ARCH_CPU_X86_FAMILY 84 #endif // ARCH_CPU_X86_FAMILY
87 85
86 } // anonymous namespace
87
88 void CPU::Initialize() { 88 void CPU::Initialize() {
89 #if defined(ARCH_CPU_X86_FAMILY) 89 #if defined(ARCH_CPU_X86_FAMILY)
90 int cpu_info[4] = {-1}; 90 int cpu_info[4] = {-1};
91 char cpu_string[48]; 91 char cpu_string[48];
92 92
93 // __cpuid with an InfoType argument of 0 returns the number of 93 // __cpuid with an InfoType argument of 0 returns the number of
94 // valid Ids in CPUInfo[0] and the CPU identification string in 94 // valid Ids in CPUInfo[0] and the CPU identification string in
95 // the other three array elements. The CPU identification string is 95 // the other three array elements. The CPU identification string is
96 // not in linear order. The code below arranges the information 96 // not in linear order. The code below arranges the information
97 // in a human readable form. The human readable order is CPUInfo[1] | 97 // in a human readable form. The human readable order is CPUInfo[1] |
98 // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped 98 // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped
99 // before using memcpy to copy these three array elements to cpu_string. 99 // before using memcpy to copy these three array elements to cpu_string.
100 __cpuid(cpu_info, 0); 100 __cpuid(cpu_info, 0);
101 int num_ids = cpu_info[0]; 101 int num_ids = cpu_info[0];
102 std::swap(cpu_info[2], cpu_info[3]); 102 std::swap(cpu_info[2], cpu_info[3]);
103 memcpy(cpu_string, &cpu_info[1], 3 * sizeof(cpu_info[1])); 103 memcpy(cpu_string, &cpu_info[1], 3 * sizeof(cpu_info[1]));
104 cpu_vendor_.assign(cpu_string, 3 * sizeof(cpu_info[1])); 104 cpu_vendor_.assign(cpu_string, 3 * sizeof(cpu_info[1]));
105 105
106 // Interpret CPU feature information. 106 // Interpret CPU feature information.
107 if (num_ids > 0) { 107 if (num_ids > 0) {
108 __cpuid(cpu_info, 1); 108 __cpuid(cpu_info, 1);
109 signature_ = cpu_info[0]; 109 signature_ = cpu_info[0];
110 stepping_ = cpu_info[0] & 0xf; 110 stepping_ = cpu_info[0] & 0xf;
111 model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0); 111 model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0);
112 family_ = (cpu_info[0] >> 8) & 0xf; 112 family_ = (cpu_info[0] >> 8) & 0xf;
113 type_ = (cpu_info[0] >> 12) & 0x3; 113 type_ = (cpu_info[0] >> 12) & 0x3;
114 ext_model_ = (cpu_info[0] >> 16) & 0xf; 114 ext_model_ = (cpu_info[0] >> 16) & 0xf;
115 ext_family_ = (cpu_info[0] >> 20) & 0xff; 115 ext_family_ = (cpu_info[0] >> 20) & 0xff;
116 has_mmx_ = (cpu_info[3] & 0x00800000) != 0; 116 has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
117 has_sse_ = (cpu_info[3] & 0x02000000) != 0; 117 has_sse_ = (cpu_info[3] & 0x02000000) != 0;
118 has_sse2_ = (cpu_info[3] & 0x04000000) != 0; 118 has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
119 has_sse3_ = (cpu_info[2] & 0x00000001) != 0; 119 has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
120 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0; 120 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
121 has_sse41_ = (cpu_info[2] & 0x00080000) != 0; 121 has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
122 has_sse42_ = (cpu_info[2] & 0x00100000) != 0; 122 has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
123 has_avx_ = (cpu_info[2] & 0x10000000) != 0; 123 has_avx_hardware_ =
124 (cpu_info[2] & 0x10000000) != 0;
125 // AVX instructions will generate an illegal instruction exception unless
126 // a) they are supported by the CPU,
127 // b) XSAVE is supported by the CPU and
128 // c) XSAVE is enabled by the kernel.
129 // See http://software.intel.com/en-us/blogs/2011/04/14/is-avx-enabled
130 has_avx_ =
131 has_avx_hardware_ &&
132 (cpu_info[2] & 0x08000000) != 0 /* OSXSAVE */ &&
133 (_xgetbv(0) & 6) == 6 /* XSAVE enabled by kernel */;
124 } 134 }
125 135
126 // Get the brand string of the cpu. 136 // Get the brand string of the cpu.
127 __cpuid(cpu_info, 0x80000000); 137 __cpuid(cpu_info, 0x80000000);
128 const int parameter_end = 0x80000004; 138 const int parameter_end = 0x80000004;
129 int max_parameter = cpu_info[0]; 139 int max_parameter = cpu_info[0];
130 140
131 if (cpu_info[0] >= parameter_end) { 141 if (cpu_info[0] >= parameter_end) {
132 char* cpu_string_ptr = cpu_string; 142 char* cpu_string_ptr = cpu_string;
133 143
(...skipping 26 matching lines...) Expand all
160 if (has_sse42()) return SSE42; 170 if (has_sse42()) return SSE42;
161 if (has_sse41()) return SSE41; 171 if (has_sse41()) return SSE41;
162 if (has_ssse3()) return SSSE3; 172 if (has_ssse3()) return SSSE3;
163 if (has_sse3()) return SSE3; 173 if (has_sse3()) return SSE3;
164 if (has_sse2()) return SSE2; 174 if (has_sse2()) return SSE2;
165 if (has_sse()) return SSE; 175 if (has_sse()) return SSE;
166 return PENTIUM; 176 return PENTIUM;
167 } 177 }
168 178
169 } // namespace base 179 } // namespace base
OLDNEW
« no previous file with comments | « base/cpu.h ('k') | crypto/nss_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698