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

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, 1 month 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
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"
(...skipping 24 matching lines...) Expand all
75 81
76 void __cpuidex(int cpu_info[4], int info_type, int info_index) { 82 void __cpuidex(int cpu_info[4], int info_type, int info_index) {
77 __asm__ volatile ( 83 __asm__ volatile (
78 "cpuid \n\t" 84 "cpuid \n\t"
79 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3]) 85 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
80 : "a"(info_type), "c"(info_index) 86 : "a"(info_type), "c"(info_index)
81 ); 87 );
82 } 88 }
83 89
84 #endif 90 #endif
85 #endif // _MSC_VER 91
92 // _xgetbv returns the value of an Intel Extended Control Register (XCR).
93 // Currently only XCR0 is defined by Intel so |xcr| should always be zero.
94 uint64 _xgetbv(uint32 xcr) {
95 uint32 eax, edx;
96
97 __asm__ volatile ("xgetbv" : "=a" (eax), "=d" (edx) : "c" (xcr));
98 return (static_cast<uint64>(edx) << 32) | eax;
99 }
100
101 #endif // !_MSC_VER
86 #endif // ARCH_CPU_X86_FAMILY 102 #endif // ARCH_CPU_X86_FAMILY
87 103
104 } // anonymous namespace
105
88 void CPU::Initialize() { 106 void CPU::Initialize() {
89 #if defined(ARCH_CPU_X86_FAMILY) 107 #if defined(ARCH_CPU_X86_FAMILY)
90 int cpu_info[4] = {-1}; 108 int cpu_info[4] = {-1};
91 char cpu_string[48]; 109 char cpu_string[48];
92 110
93 // __cpuid with an InfoType argument of 0 returns the number of 111 // __cpuid with an InfoType argument of 0 returns the number of
94 // valid Ids in CPUInfo[0] and the CPU identification string in 112 // valid Ids in CPUInfo[0] and the CPU identification string in
95 // the other three array elements. The CPU identification string is 113 // the other three array elements. The CPU identification string is
96 // not in linear order. The code below arranges the information 114 // not in linear order. The code below arranges the information
97 // in a human readable form. The human readable order is CPUInfo[1] | 115 // in a human readable form. The human readable order is CPUInfo[1] |
98 // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped 116 // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped
99 // before using memcpy to copy these three array elements to cpu_string. 117 // before using memcpy to copy these three array elements to cpu_string.
100 __cpuid(cpu_info, 0); 118 __cpuid(cpu_info, 0);
101 int num_ids = cpu_info[0]; 119 int num_ids = cpu_info[0];
102 std::swap(cpu_info[2], cpu_info[3]); 120 std::swap(cpu_info[2], cpu_info[3]);
103 memcpy(cpu_string, &cpu_info[1], 3 * sizeof(cpu_info[1])); 121 memcpy(cpu_string, &cpu_info[1], 3 * sizeof(cpu_info[1]));
104 cpu_vendor_.assign(cpu_string, 3 * sizeof(cpu_info[1])); 122 cpu_vendor_.assign(cpu_string, 3 * sizeof(cpu_info[1]));
105 123
106 // Interpret CPU feature information. 124 // Interpret CPU feature information.
107 if (num_ids > 0) { 125 if (num_ids > 0) {
108 __cpuid(cpu_info, 1); 126 __cpuid(cpu_info, 1);
109 signature_ = cpu_info[0]; 127 signature_ = cpu_info[0];
110 stepping_ = cpu_info[0] & 0xf; 128 stepping_ = cpu_info[0] & 0xf;
111 model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0); 129 model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0);
112 family_ = (cpu_info[0] >> 8) & 0xf; 130 family_ = (cpu_info[0] >> 8) & 0xf;
113 type_ = (cpu_info[0] >> 12) & 0x3; 131 type_ = (cpu_info[0] >> 12) & 0x3;
114 ext_model_ = (cpu_info[0] >> 16) & 0xf; 132 ext_model_ = (cpu_info[0] >> 16) & 0xf;
115 ext_family_ = (cpu_info[0] >> 20) & 0xff; 133 ext_family_ = (cpu_info[0] >> 20) & 0xff;
116 has_mmx_ = (cpu_info[3] & 0x00800000) != 0; 134 has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
117 has_sse_ = (cpu_info[3] & 0x02000000) != 0; 135 has_sse_ = (cpu_info[3] & 0x02000000) != 0;
118 has_sse2_ = (cpu_info[3] & 0x04000000) != 0; 136 has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
119 has_sse3_ = (cpu_info[2] & 0x00000001) != 0; 137 has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
120 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0; 138 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
121 has_sse41_ = (cpu_info[2] & 0x00080000) != 0; 139 has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
122 has_sse42_ = (cpu_info[2] & 0x00100000) != 0; 140 has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
123 has_avx_ = (cpu_info[2] & 0x10000000) != 0; 141 has_avx_hardware_ =
142 (cpu_info[2] & 0x10000000) != 0;
143 /* AVX instructions will generate an illegal instruction exception unless
144 * a) they are supported by the CPU,
145 * b) XSAVE is supported by the CPU and
146 * c) XSAVE is enabled by the kernel.
147 * See http://software.intel.com/en-us/blogs/2011/04/14/is-avx-enabled */
148 has_avx_ =
149 has_avx_hardware_ &&
150 (cpu_info[2] & 0x08000000) == 0x08000000 /* OSXSAVE */ &&
151 (_xgetbv(0) & 6) == 6 /* XSAVE enabled by kernel */;
124 } 152 }
125 153
126 // Get the brand string of the cpu. 154 // Get the brand string of the cpu.
127 __cpuid(cpu_info, 0x80000000); 155 __cpuid(cpu_info, 0x80000000);
128 const int parameter_end = 0x80000004; 156 const int parameter_end = 0x80000004;
129 int max_parameter = cpu_info[0]; 157 int max_parameter = cpu_info[0];
130 158
131 if (cpu_info[0] >= parameter_end) { 159 if (cpu_info[0] >= parameter_end) {
132 char* cpu_string_ptr = cpu_string; 160 char* cpu_string_ptr = cpu_string;
133 161
(...skipping 26 matching lines...) Expand all
160 if (has_sse42()) return SSE42; 188 if (has_sse42()) return SSE42;
161 if (has_sse41()) return SSE41; 189 if (has_sse41()) return SSE41;
162 if (has_ssse3()) return SSSE3; 190 if (has_ssse3()) return SSSE3;
163 if (has_sse3()) return SSE3; 191 if (has_sse3()) return SSE3;
164 if (has_sse2()) return SSE2; 192 if (has_sse2()) return SSE2;
165 if (has_sse()) return SSE; 193 if (has_sse()) return SSE;
166 return PENTIUM; 194 return PENTIUM;
167 } 195 }
168 196
169 } // namespace base 197 } // namespace base
OLDNEW
« no previous file with comments | « base/cpu.h ('k') | crypto/nss_util.cc » ('j') | crypto/nss_util.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698