2 * Copyright (c) 2013 Ahmed Samy <f.fallen45@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * This file has been written with some help from wikipedia:
23 * http://en.wikipedia.org/wiki/CPUID
31 CPU_PROC_BRAND_STRING_INTERNAL0 = 0x80000003,
32 CPU_PROC_BRAND_STRING_INTERNAL1 = 0x80000004
36 static void ___cpuid(cpuid_t info, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
39 "xchg %%ebx, %%edi\n\t" /* 32bit PIC: Don't clobber ebx. */
41 "xchg %%ebx, %%edi\n\t"
42 : "=a"(*eax), "=D"(*ebx), "=c"(*ecx), "=d"(*edx)
49 static void ___cpuid(cpuid_t info, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
51 uint32_t registers[4];
52 __cpuid(registers, info);
64 int instruction; /* 0 = ecx, 1 = edx. */
66 { CF_MMX, 1 << 23, 1 },
67 { CF_SSE, 1 << 25, 1 },
68 { CF_SSE2, 1 << 26, 1 },
69 { CF_SSE3, 1 << 9, 0 },
70 { CF_FPU, 1 << 0, 1 },
72 { CF_TSC, 1 << 4, 1 },
73 { CF_MSR, 1 << 5, 1 },
75 { CF_SSSE3, 1 << 9, 0 },
76 { CF_AVX, 1 << 28, 0 },
79 { CEF_x64, 1 << 30, 1 },
80 { CEF_FPU, 1 << 0, 1 },
81 { CEF_DE, 1 << 2, 1 },
82 { CEF_SYSCALLRET, 1 << 11, 1 },
83 { CEF_CMOV, 1 << 15, 1 },
85 { CEF_SSE4a, 1 << 6, 0 },
86 { CEF_FMA4, 1 << 16, 0 },
87 { CEF_XOP, 1 << 11, 0 }
90 static int has_feature(int feature, uint32_t ecx, uint32_t edx)
94 for (i = 0; i < sizeof(features) / sizeof(features[0]); ++i) {
95 if (features[i].feature == feature) {
96 if (features[i].instruction == 0)
97 return (ecx & features[i].mask);
99 return (edx & features[i].mask);
106 int cpuid_test_feature(cpuid_t feature)
108 if (feature > CPU_VIRT_PHYS_ADDR_SIZES || feature < CPU_EXTENDED_PROC_INFO_FEATURE_BITS)
111 return (feature <= cpuid_highest_ext_func_supported());
114 int cpuid_has_feature(int feature, int extended)
116 uint32_t eax, ebx, ecx, edx;
119 ___cpuid(CPU_PROCINFO_AND_FEATUREBITS, &eax, &ebx, &ecx, &edx);
121 ___cpuid(CPU_EXTENDED_PROC_INFO_FEATURE_BITS, &eax, &ebx, &ecx, &edx);
123 return has_feature(feature, ecx, edx);
126 static const char *cpuids[] = {
145 cputype_t cpuid_get_cpu_type(void)
147 static cputype_t cputype;
148 if (cputype == CT_NONE) {
155 ___cpuid(CPU_VENDORID, &i, &u.bufu32[0], &u.bufu32[2], &u.bufu32[1]);
158 for (i = 0; i < sizeof(cpuids) / sizeof(cpuids[0]); ++i) {
159 if (strncmp(cpuids[i], u.buf, 12) == 0) {
160 cputype = (cputype_t)i;
169 const char *cpuid_get_cpu_type_string(const cputype_t cputype)
171 return cpuids[(int)cputype];
174 int cpuid_highest_ext_func_supported(void)
182 : "a" (CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED)
189 void cpuid(cpuid_t info, void *buf)
191 /* Sanity checks, make sure we're not trying to do something
192 * invalid or we are trying to get information that isn't supported
194 if (info > CPU_VIRT_PHYS_ADDR_SIZES || (info > CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED
195 && !cpuid_test_feature(info)))
198 uint32_t *ubuf = buf;
199 if (info == CPU_PROC_BRAND_STRING) {
200 ___cpuid(CPU_PROC_BRAND_STRING, &ubuf[0], &ubuf[1], &ubuf[2], &ubuf[3]);
201 ___cpuid(CPU_PROC_BRAND_STRING_INTERNAL0, &ubuf[4], &ubuf[5], &ubuf[6], &ubuf[7]);
202 ___cpuid(CPU_PROC_BRAND_STRING_INTERNAL1, &ubuf[8], &ubuf[9], &ubuf[10], &ubuf[11]);
204 } else if (info == CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED) {
205 *ubuf = cpuid_highest_ext_func_supported();
209 uint32_t eax, ebx, ecx, edx;
210 ___cpuid(info, &eax, &ebx, &ecx, &edx);
218 case CPU_PROCINFO_AND_FEATUREBITS:
219 ubuf[0] = eax; /* The so called "signature" of the CPU. */
220 ubuf[1] = edx; /* Feature flags #1. */
221 ubuf[2] = ecx; /* Feature flags #2. */
222 ubuf[3] = ebx; /* Additional feature information. */
224 case CPU_CACHE_AND_TLBD_INFO:
230 case CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
234 case CPU_L1_CACHE_AND_TLB_IDS:
236 case CPU_EXTENDED_L2_CACHE_FEATURES:
239 case CPU_ADV_POWER_MGT_INFO:
242 case CPU_VIRT_PHYS_ADDR_SIZES: