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
30 CPU_PROC_BRAND_STRING_INTERNAL0 = 0x80000003,
31 CPU_PROC_BRAND_STRING_INTERNAL1 = 0x80000004
35 static void ___cpuid(cpuid_t info, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
38 "xchg %%ebx, %%edi\n\t" /* 32bit PIC: Don't clobber ebx. */
40 "xchg %%ebx, %%edi\n\t"
41 : "=a"(*eax), "=D"(*ebx), "=c"(*ecx), "=d"(*edx)
48 static void ___cpuid(cpuid_t info, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
50 uint32_t registers[4];
51 __cpuid(registers, info);
60 int highest_ext_func_supported(void)
68 : "a" (CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED)
75 int cpuid_test_feature(cpuid_t feature)
77 if (feature > CPU_VIRT_PHYS_ADDR_SIZES || feature < CPU_EXTENDED_PROC_INFO_FEATURE_BITS)
80 return (feature <= highest_ext_func_supported());
83 int cpuid_has_feature(cpufeature_t feature)
85 uint32_t eax, ebx, ecx, edx;
87 ___cpuid(CPU_PROCINFO_AND_FEATUREBITS, &eax, &ebx, &ecx, &edx);
92 return (edx & ((int)feature)) != 0;
99 return (ecx & ((int)feature)) != 0;
105 int cpuid_has_ext_feature(cpuextfeature_t extfeature)
107 uint32_t eax, ebx, ecx, edx;
108 if (!cpuid_test_feature(CPU_EXTENDED_PROC_INFO_FEATURE_BITS))
111 ___cpuid(CPU_EXTENDED_PROC_INFO_FEATURE_BITS, &eax, &ebx, &ecx, &edx);
112 switch (extfeature) {
114 return (edx & ((int)extfeature)) != 0;
118 return (ecx & ((int)extfeature)) != 0;
124 void cpuid(cpuid_t info, void *buf)
126 /* Sanity checks, make sure we're not trying to do something
127 * invalid or we are trying to get information that isn't supported
129 if (info > CPU_VIRT_PHYS_ADDR_SIZES || (info > CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED
130 && !cpuid_test_feature(info)))
133 uint32_t *ubuf = buf;
134 if (info == CPU_PROC_BRAND_STRING) {
135 ___cpuid(CPU_PROC_BRAND_STRING, &ubuf[0], &ubuf[1], &ubuf[2], &ubuf[3]);
136 ___cpuid(CPU_PROC_BRAND_STRING_INTERNAL0, &ubuf[4], &ubuf[5], &ubuf[6], &ubuf[7]);
137 ___cpuid(CPU_PROC_BRAND_STRING_INTERNAL1, &ubuf[8], &ubuf[9], &ubuf[10], &ubuf[11]);
139 } else if (info == CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED) {
140 *ubuf = highest_ext_func_supported();
144 uint32_t eax, ebx, ecx, edx;
145 ___cpuid(info, &eax, &ebx, &ecx, &edx);
153 case CPU_PROCINFO_AND_FEATUREBITS:
154 ubuf[0] = eax; /* The so called "signature" of the CPU. */
155 ubuf[1] = edx; /* Feature flags #1. */
156 ubuf[2] = ecx; /* Feature flags #2. */
157 ubuf[3] = ebx; /* Additional feature information. */
159 case CPU_CACHE_AND_TLBD_INFO:
165 case CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
169 case CPU_L1_CACHE_AND_TLB_IDS:
171 case CPU_EXTENDED_L2_CACHE_FEATURES:
172 ubuf[0] = (ecx & 0xFF); /* Cache size */
173 ubuf[1] = (ecx >> 12) & 0xF; /* Line size */
174 ubuf[2] = (ecx >> 16) & 0xFFFF; /* Associativity */
176 case CPU_ADV_POWER_MGT_INFO:
178 case CPU_VIRT_PHYS_ADDR_SIZES: