Skip to content

Commit f6e8408

Browse files
committed
[X86] Add __get_cpuid_count to cpuid.h. Update __get_cpuid to check the maximum level support before accessing the leaf. Rename level to leaf everywhere.
This matches gcc behavior. llvm-svn: 307506
1 parent f2d571c commit f6e8408

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

clang/lib/Headers/cpuid.h

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -153,38 +153,31 @@
153153
#define bit_ENH_MOVSB 0x00000200
154154

155155
#if __i386__
156-
#define __cpuid(__level, __eax, __ebx, __ecx, __edx) \
156+
#define __cpuid(__leaf, __eax, __ebx, __ecx, __edx) \
157157
__asm("cpuid" : "=a"(__eax), "=b" (__ebx), "=c"(__ecx), "=d"(__edx) \
158-
: "0"(__level))
158+
: "0"(__leaf))
159159

160-
#define __cpuid_count(__level, __count, __eax, __ebx, __ecx, __edx) \
160+
#define __cpuid_count(__leaf, __count, __eax, __ebx, __ecx, __edx) \
161161
__asm("cpuid" : "=a"(__eax), "=b" (__ebx), "=c"(__ecx), "=d"(__edx) \
162-
: "0"(__level), "2"(__count))
162+
: "0"(__leaf), "2"(__count))
163163
#else
164164
/* x86-64 uses %rbx as the base register, so preserve it. */
165-
#define __cpuid(__level, __eax, __ebx, __ecx, __edx) \
165+
#define __cpuid(__leaf, __eax, __ebx, __ecx, __edx) \
166166
__asm(" xchgq %%rbx,%q1\n" \
167167
" cpuid\n" \
168168
" xchgq %%rbx,%q1" \
169169
: "=a"(__eax), "=r" (__ebx), "=c"(__ecx), "=d"(__edx) \
170-
: "0"(__level))
170+
: "0"(__leaf))
171171

172-
#define __cpuid_count(__level, __count, __eax, __ebx, __ecx, __edx) \
172+
#define __cpuid_count(__leaf, __count, __eax, __ebx, __ecx, __edx) \
173173
__asm(" xchgq %%rbx,%q1\n" \
174174
" cpuid\n" \
175175
" xchgq %%rbx,%q1" \
176176
: "=a"(__eax), "=r" (__ebx), "=c"(__ecx), "=d"(__edx) \
177-
: "0"(__level), "2"(__count))
177+
: "0"(__leaf), "2"(__count))
178178
#endif
179179

180-
static __inline int __get_cpuid (unsigned int __level, unsigned int *__eax,
181-
unsigned int *__ebx, unsigned int *__ecx,
182-
unsigned int *__edx) {
183-
__cpuid(__level, *__eax, *__ebx, *__ecx, *__edx);
184-
return 1;
185-
}
186-
187-
static __inline int __get_cpuid_max (unsigned int __level, unsigned int *__sig)
180+
static __inline int __get_cpuid_max (unsigned int __leaf, unsigned int *__sig)
188181
{
189182
unsigned int __eax, __ebx, __ecx, __edx;
190183
#if __i386__
@@ -208,8 +201,35 @@ static __inline int __get_cpuid_max (unsigned int __level, unsigned int *__sig)
208201
return 0;
209202
#endif
210203

211-
__cpuid(__level, __eax, __ebx, __ecx, __edx);
204+
__cpuid(__leaf, __eax, __ebx, __ecx, __edx);
212205
if (__sig)
213206
*__sig = __ebx;
214207
return __eax;
215208
}
209+
210+
static __inline int __get_cpuid (unsigned int __leaf, unsigned int *__eax,
211+
unsigned int *__ebx, unsigned int *__ecx,
212+
unsigned int *__edx)
213+
{
214+
unsigned int __max_leaf = __get_cpuid_max(__leaf & 0x80000000, 0);
215+
216+
if (__max_leaf == 0 || __max_leaf < __leaf)
217+
return 0;
218+
219+
__cpuid(__leaf, *__eax, *__ebx, *__ecx, *__edx);
220+
return 1;
221+
}
222+
223+
static __inline int __get_cpuid_count (unsigned int __leaf,
224+
unsigned int __subleaf,
225+
unsigned int *__eax, unsigned int *__ebx,
226+
unsigned int *__ecx, unsigned int *__edx)
227+
{
228+
unsigned int __max_leaf = __get_cpuid_max(__leaf & 0x80000000, 0);
229+
230+
if (__max_leaf == 0 || __max_leaf < __leaf)
231+
return 0;
232+
233+
__cpuid_count(__leaf, __subleaf, *__eax, *__ebx, *__ecx, *__edx);
234+
return 1;
235+
}

0 commit comments

Comments
 (0)