1/* x86 cache info.
2 Copyright (C) 2003-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#if IS_IN (libc)
20
21#include <unistd.h>
22#include <ldsodefs.h>
23
24/* Get the value of the system variable NAME. */
25long int
26attribute_hidden
27__cache_sysconf (int name)
28{
29 const struct cpu_features *cpu_features = __get_cpu_features ();
30 switch (name)
31 {
32 case _SC_LEVEL1_ICACHE_SIZE:
33 return cpu_features->level1_icache_size;
34
35 case _SC_LEVEL1_ICACHE_LINESIZE:
36 return cpu_features->level1_icache_linesize;
37
38 case _SC_LEVEL1_DCACHE_SIZE:
39 return cpu_features->level1_dcache_size;
40
41 case _SC_LEVEL1_DCACHE_ASSOC:
42 return cpu_features->level1_dcache_assoc;
43
44 case _SC_LEVEL1_DCACHE_LINESIZE:
45 return cpu_features->level1_dcache_linesize;
46
47 case _SC_LEVEL2_CACHE_SIZE:
48 return cpu_features->level2_cache_size;
49
50 case _SC_LEVEL2_CACHE_ASSOC:
51 return cpu_features->level2_cache_assoc;
52
53 case _SC_LEVEL2_CACHE_LINESIZE:
54 return cpu_features->level2_cache_linesize;
55
56 case _SC_LEVEL3_CACHE_SIZE:
57 return cpu_features->level3_cache_size;
58
59 case _SC_LEVEL3_CACHE_ASSOC:
60 return cpu_features->level3_cache_assoc;
61
62 case _SC_LEVEL3_CACHE_LINESIZE:
63 return cpu_features->level3_cache_linesize;
64
65 case _SC_LEVEL4_CACHE_SIZE:
66 return cpu_features->level4_cache_size;
67
68 default:
69 break;
70 }
71 return -1;
72}
73
74# ifdef SHARED
75/* NB: In libc.a, cacheinfo.h is included in libc-start.c. In libc.so,
76 cacheinfo.h is included here and call init_cacheinfo by initializing
77 a dummy function pointer via IFUNC relocation after CPU features in
78 ld.so have been initialized by DL_PLATFORM_INIT or IFUNC relocation. */
79# include <cacheinfo.h>
80# include <ifunc-init.h>
81
82extern void __x86_cacheinfo (void) attribute_hidden;
83void (*const __x86_cacheinfo_p) (void) attribute_hidden
84 = __x86_cacheinfo;
85
86__ifunc (__x86_cacheinfo, __x86_cacheinfo, NULL, void, init_cacheinfo);
87# endif
88#endif
89

source code of glibc/sysdeps/x86/cacheinfo.c