Updated to fedora-glibc-20090204T2135
[glibc.git] / sysdeps / unix / sysv / linux / i386 / sysconf.c
blobb1c93d7359cd8e4afbd37b8d4c43f4ee73cab2dc
1 /* Get file-specific information about a file. Linux version.
2 Copyright (C) 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <stdbool.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <hp-timing.h>
26 static long int linux_sysconf (int name);
29 static long int __attribute__ ((noinline))
30 handle_i486 (int name)
32 /* The processor only has a unified level 1 cache of 8k. */
33 switch (name)
35 case _SC_LEVEL1_ICACHE_SIZE:
36 case _SC_LEVEL1_DCACHE_SIZE:
37 return 8 * 1024;
39 case _SC_LEVEL1_ICACHE_ASSOC:
40 case _SC_LEVEL1_DCACHE_ASSOC:
41 // XXX Anybody know this?
42 return 0;
44 case _SC_LEVEL1_ICACHE_LINESIZE:
45 case _SC_LEVEL1_DCACHE_LINESIZE:
46 // XXX Anybody know for sure?
47 return 16;
49 case _SC_LEVEL2_CACHE_SIZE:
50 case _SC_LEVEL2_CACHE_ASSOC:
51 case _SC_LEVEL2_CACHE_LINESIZE:
52 case _SC_LEVEL3_CACHE_SIZE:
53 case _SC_LEVEL3_CACHE_ASSOC:
54 case _SC_LEVEL3_CACHE_LINESIZE:
55 case _SC_LEVEL4_CACHE_SIZE:
56 case _SC_LEVEL4_CACHE_ASSOC:
57 /* Not available. */
58 break;
60 default:
61 assert (! "cannot happen");
64 return -1;
68 static const struct intel_02_cache_info
70 unsigned int idx;
71 int name;
72 long int size;
73 long int assoc;
74 long int linesize;
75 } intel_02_known[] =
77 { 0x06, _SC_LEVEL1_ICACHE_SIZE, 8192, 4, 32 },
78 { 0x08, _SC_LEVEL1_ICACHE_SIZE, 16384, 4, 32 },
79 { 0x09, _SC_LEVEL1_ICACHE_SIZE, 32768, 4, 32 },
80 { 0x0a, _SC_LEVEL1_DCACHE_SIZE, 8192, 2, 32 },
81 { 0x0c, _SC_LEVEL1_DCACHE_SIZE, 16384, 4, 32 },
82 { 0x0d, _SC_LEVEL1_DCACHE_SIZE, 16384, 4, 64 },
83 { 0x22, _SC_LEVEL3_CACHE_SIZE, 524288, 4, 64 },
84 { 0x23, _SC_LEVEL3_CACHE_SIZE, 1048576, 8, 64 },
85 { 0x25, _SC_LEVEL3_CACHE_SIZE, 2097152, 8, 64 },
86 { 0x29, _SC_LEVEL3_CACHE_SIZE, 4194304, 8, 64 },
87 { 0x2c, _SC_LEVEL1_DCACHE_SIZE, 32768, 8, 64 },
88 { 0x30, _SC_LEVEL1_ICACHE_SIZE, 32768, 8, 64 },
89 { 0x39, _SC_LEVEL2_CACHE_SIZE, 131072, 4, 64 },
90 { 0x3a, _SC_LEVEL2_CACHE_SIZE, 196608, 6, 64 },
91 { 0x3b, _SC_LEVEL2_CACHE_SIZE, 131072, 2, 64 },
92 { 0x3c, _SC_LEVEL2_CACHE_SIZE, 262144, 4, 64 },
93 { 0x3d, _SC_LEVEL2_CACHE_SIZE, 393216, 6, 64 },
94 { 0x3e, _SC_LEVEL2_CACHE_SIZE, 524288, 4, 64 },
95 { 0x3f, _SC_LEVEL2_CACHE_SIZE, 262144, 2, 64 },
96 { 0x41, _SC_LEVEL2_CACHE_SIZE, 131072, 4, 32 },
97 { 0x42, _SC_LEVEL2_CACHE_SIZE, 262144, 4, 32 },
98 { 0x43, _SC_LEVEL2_CACHE_SIZE, 524288, 4, 32 },
99 { 0x44, _SC_LEVEL2_CACHE_SIZE, 1048576, 4, 32 },
100 { 0x45, _SC_LEVEL2_CACHE_SIZE, 2097152, 4, 32 },
101 { 0x46, _SC_LEVEL3_CACHE_SIZE, 4194304, 4, 64 },
102 { 0x47, _SC_LEVEL3_CACHE_SIZE, 8388608, 8, 64 },
103 { 0x48, _SC_LEVEL2_CACHE_SIZE, 3145728, 12, 64 },
104 { 0x49, _SC_LEVEL2_CACHE_SIZE, 4194304, 16, 64 },
105 { 0x4a, _SC_LEVEL3_CACHE_SIZE, 6291456, 12, 64 },
106 { 0x4b, _SC_LEVEL3_CACHE_SIZE, 8388608, 16, 64 },
107 { 0x4c, _SC_LEVEL3_CACHE_SIZE, 12582912, 12, 64 },
108 { 0x4d, _SC_LEVEL3_CACHE_SIZE, 16777216, 16, 64 },
109 { 0x4e, _SC_LEVEL2_CACHE_SIZE, 6291456, 24, 64 },
110 { 0x60, _SC_LEVEL1_DCACHE_SIZE, 16384, 8, 64 },
111 { 0x66, _SC_LEVEL1_DCACHE_SIZE, 8192, 4, 64 },
112 { 0x67, _SC_LEVEL1_DCACHE_SIZE, 16384, 4, 64 },
113 { 0x68, _SC_LEVEL1_DCACHE_SIZE, 32768, 4, 64 },
114 { 0x78, _SC_LEVEL2_CACHE_SIZE, 1048576, 8, 64 },
115 { 0x79, _SC_LEVEL2_CACHE_SIZE, 131072, 8, 64 },
116 { 0x7a, _SC_LEVEL2_CACHE_SIZE, 262144, 8, 64 },
117 { 0x7b, _SC_LEVEL2_CACHE_SIZE, 524288, 8, 64 },
118 { 0x7c, _SC_LEVEL2_CACHE_SIZE, 1048576, 8, 64 },
119 { 0x7d, _SC_LEVEL2_CACHE_SIZE, 2097152, 8, 64 },
120 { 0x7f, _SC_LEVEL2_CACHE_SIZE, 524288, 2, 64 },
121 { 0x82, _SC_LEVEL2_CACHE_SIZE, 262144, 8, 32 },
122 { 0x83, _SC_LEVEL2_CACHE_SIZE, 524288, 8, 32 },
123 { 0x84, _SC_LEVEL2_CACHE_SIZE, 1048576, 8, 32 },
124 { 0x85, _SC_LEVEL2_CACHE_SIZE, 2097152, 8, 32 },
125 { 0x86, _SC_LEVEL2_CACHE_SIZE, 524288, 4, 64 },
126 { 0x87, _SC_LEVEL2_CACHE_SIZE, 1048576, 8, 64 },
127 { 0xd0, _SC_LEVEL3_CACHE_SIZE, 524288, 4, 64 },
128 { 0xd1, _SC_LEVEL3_CACHE_SIZE, 1048576, 4, 64 },
129 { 0xd2, _SC_LEVEL3_CACHE_SIZE, 2097152, 4, 64 },
130 { 0xd6, _SC_LEVEL3_CACHE_SIZE, 1048576, 8, 64 },
131 { 0xd7, _SC_LEVEL3_CACHE_SIZE, 2097152, 8, 64 },
132 { 0xd8, _SC_LEVEL3_CACHE_SIZE, 4194304, 8, 64 },
133 { 0xdc, _SC_LEVEL3_CACHE_SIZE, 2097152, 12, 64 },
134 { 0xdd, _SC_LEVEL3_CACHE_SIZE, 4194304, 12, 64 },
135 { 0xde, _SC_LEVEL3_CACHE_SIZE, 8388608, 12, 64 },
136 { 0xe3, _SC_LEVEL3_CACHE_SIZE, 2097152, 16, 64 },
137 { 0xe3, _SC_LEVEL3_CACHE_SIZE, 4194304, 16, 64 },
138 { 0xe4, _SC_LEVEL3_CACHE_SIZE, 8388608, 16, 64 },
140 #define nintel_02_known (sizeof (intel_02_known) / sizeof (intel_02_known[0]))
143 static int
144 intel_02_known_compare (const void *p1, const void *p2)
146 const struct intel_02_cache_info *i1;
147 const struct intel_02_cache_info *i2;
149 i1 = (const struct intel_02_cache_info *) p1;
150 i2 = (const struct intel_02_cache_info *) p2;
152 if (i1->idx == i2->idx)
153 return 0;
155 return i1->idx < i2->idx ? -1 : 1;
159 static long int
160 __attribute__ ((noinline))
161 intel_check_word (int name, unsigned int value, bool *has_level_2,
162 bool *no_level_2_or_3)
164 if ((value & 0x80000000) != 0)
165 /* The register value is reserved. */
166 return 0;
168 /* Fold the name. The _SC_ constants are always in the order SIZE,
169 ASSOC, LINESIZE. */
170 int folded_name = (_SC_LEVEL1_ICACHE_SIZE
171 + ((name - _SC_LEVEL1_ICACHE_SIZE) / 3) * 3);
173 while (value != 0)
175 unsigned int byte = value & 0xff;
177 if (byte == 0x40)
179 *no_level_2_or_3 = true;
181 if (folded_name == _SC_LEVEL3_CACHE_SIZE)
182 /* No need to look further. */
183 break;
185 else
187 if (byte == 0x49 && folded_name == _SC_LEVEL3_CACHE_SIZE)
189 /* Intel reused this value. For family 15, model 6 it
190 specifies the 3rd level cache. Otherwise the 2nd
191 level cache. */
192 unsigned int eax;
193 unsigned int ebx;
194 unsigned int ecx;
195 unsigned int edx;
196 asm volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1"
197 : "=a" (eax), "=r" (ebx), "=c" (ecx), "=d" (edx)
198 : "0" (1));
200 unsigned int family = ((eax >> 20) & 0xff) + ((eax >> 8) & 0xf);
201 unsigned int model = ((((eax >>16) & 0xf) << 4)
202 + ((eax >> 4) & 0xf));
203 if (family == 15 && model == 6)
205 /* The level 3 cache is encoded for this model like
206 the level 2 cache is for other models. Pretend
207 the caller asked for the level 2 cache. */
208 name = (_SC_LEVEL2_CACHE_SIZE
209 + (name - _SC_LEVEL3_CACHE_SIZE));
210 folded_name = _SC_LEVEL3_CACHE_SIZE;
214 struct intel_02_cache_info *found;
215 struct intel_02_cache_info search;
217 search.idx = byte;
218 found = bsearch (&search, intel_02_known, nintel_02_known,
219 sizeof (intel_02_known[0]), intel_02_known_compare);
220 if (found != NULL)
222 if (found->name == folded_name)
224 unsigned int offset = name - folded_name;
226 if (offset == 0)
227 /* Cache size. */
228 return found->size;
229 if (offset == 1)
230 return found->assoc;
232 assert (offset == 2);
233 return found->linesize;
236 if (found->name == _SC_LEVEL2_CACHE_SIZE)
237 *has_level_2 = true;
241 /* Next byte for the next round. */
242 value >>= 8;
245 /* Nothing found. */
246 return 0;
250 static long int __attribute__ ((noinline))
251 handle_intel (int name, unsigned int maxidx)
253 if (maxidx < 2)
255 // XXX Do such processors exist? When we know we can fill in some
256 // values.
257 return 0;
260 /* OK, we can use the CPUID instruction to get all info about the
261 caches. */
262 unsigned int cnt = 0;
263 unsigned int max = 1;
264 long int result = 0;
265 bool no_level_2_or_3 = false;
266 bool has_level_2 = false;
267 while (cnt++ < max)
269 unsigned int eax;
270 unsigned int ebx;
271 unsigned int ecx;
272 unsigned int edx;
273 asm volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1"
274 : "=a" (eax), "=r" (ebx), "=c" (ecx), "=d" (edx)
275 : "0" (2));
277 /* The low byte of EAX in the first round contain the number of
278 rounds we have to make. At least one, the one we are already
279 doing. */
280 if (cnt == 1)
282 max = eax & 0xff;
283 eax &= 0xffffff00;
286 /* Process the individual registers' value. */
287 result = intel_check_word (name, eax, &has_level_2, &no_level_2_or_3);
288 if (result != 0)
289 return result;
291 result = intel_check_word (name, ebx, &has_level_2, &no_level_2_or_3);
292 if (result != 0)
293 return result;
295 result = intel_check_word (name, ecx, &has_level_2, &no_level_2_or_3);
296 if (result != 0)
297 return result;
299 result = intel_check_word (name, edx, &has_level_2, &no_level_2_or_3);
300 if (result != 0)
301 return result;
304 if (name >= _SC_LEVEL2_CACHE_SIZE && name <= _SC_LEVEL3_CACHE_LINESIZE
305 && no_level_2_or_3)
306 return -1;
308 return 0;
312 static long int __attribute__ ((noinline))
313 handle_amd (int name)
315 unsigned int eax;
316 unsigned int ebx;
317 unsigned int ecx;
318 unsigned int edx;
319 asm volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1"
320 : "=a" (eax), "=r" (ebx), "=c" (ecx), "=d" (edx)
321 : "0" (0x80000000));
323 if (name >= _SC_LEVEL3_CACHE_SIZE)
324 return 0;
326 unsigned int fn = 0x80000005 + (name >= _SC_LEVEL2_CACHE_SIZE);
327 if (eax < fn)
328 return 0;
330 asm volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1"
331 : "=a" (eax), "=r" (ebx), "=c" (ecx), "=d" (edx)
332 : "0" (fn));
334 if (name < _SC_LEVEL1_DCACHE_SIZE)
336 name += _SC_LEVEL1_DCACHE_SIZE - _SC_LEVEL1_ICACHE_SIZE;
337 ecx = edx;
340 switch (name)
342 case _SC_LEVEL1_DCACHE_SIZE:
343 return (ecx >> 14) & 0x3fc00;
344 case _SC_LEVEL1_DCACHE_ASSOC:
345 ecx >>= 16;
346 if ((ecx & 0xff) == 0xff)
347 /* Fully associative. */
348 return (ecx << 2) & 0x3fc00;
349 return ecx & 0xff;
350 case _SC_LEVEL1_DCACHE_LINESIZE:
351 return ecx & 0xff;
352 case _SC_LEVEL2_CACHE_SIZE:
353 return (ecx & 0xf000) == 0 ? 0 : (ecx >> 6) & 0x3fffc00;
354 case _SC_LEVEL2_CACHE_ASSOC:
355 ecx >>= 12;
356 switch (ecx & 0xf)
358 case 0:
359 case 1:
360 case 2:
361 case 4:
362 return ecx & 0xf;
363 case 6:
364 return 8;
365 case 8:
366 return 16;
367 case 0xf:
368 return (ecx << 6) & 0x3fffc00;
369 default:
370 return 0;
372 case _SC_LEVEL2_CACHE_LINESIZE:
373 return (ecx & 0xf000) == 0 ? 0 : ecx & 0xff;
374 default:
375 assert (! "cannot happen");
377 return -1;
381 static int
382 i386_i486_test (void)
384 int eflags;
385 int ac;
386 asm volatile ("pushfl;\n\t"
387 "popl %0;\n\t"
388 "movl $0x240000, %1;\n\t"
389 "xorl %0, %1;\n\t"
390 "pushl %1;\n\t"
391 "popfl;\n\t"
392 "pushfl;\n\t"
393 "popl %1;\n\t"
394 "xorl %0, %1;\n\t"
395 "pushl %0;\n\t"
396 "popfl"
397 : "=r" (eflags), "=r" (ac));
399 return ac;
403 /* Get the value of the system variable NAME. */
404 long int
405 __sysconf (int name)
407 /* All the remainder, except the cache information, is handled in
408 the generic code. */
409 if (name < _SC_LEVEL1_ICACHE_SIZE || name > _SC_LEVEL4_CACHE_LINESIZE)
410 return linux_sysconf (name);
412 /* Recognize i386 and compatible. These don't have any cache on
413 board. */
414 int ac = i386_i486_test ();
416 if (ac == 0)
417 /* This is an i386. */
418 // XXX Is this true for all brands?
419 return -1;
421 /* Detect i486, the last Intel processor without CPUID. */
422 if ((ac & (1 << 21)) == 0)
424 /* No CPUID. */
425 // XXX Fill in info about other brands. For now only Intel.
426 return handle_i486 (name);
429 /* Find out what brand of processor. */
430 unsigned int eax;
431 unsigned int ebx;
432 unsigned int ecx;
433 unsigned int edx;
434 asm volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1"
435 : "=a" (eax), "=r" (ebx), "=c" (ecx), "=d" (edx)
436 : "0" (0));
438 /* This spells out "GenuineIntel". */
439 if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
440 return handle_intel (name, eax);
442 /* This spells out "AuthenticAMD". */
443 if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
444 return handle_amd (name);
446 // XXX Fill in more vendors.
448 /* CPU not known, we have no information. */
449 return 0;
452 /* Now the generic Linux version. */
453 #undef __sysconf
454 #define __sysconf static linux_sysconf
455 #include "../sysconf.c"