[ccan] [PATCH] cpuid: new module
Ahmed Samy
f.fallen45 at gmail.com
Sat Sep 21 19:48:16 EST 2013
This module parses data provided by the cpuid instruction.
It still needs more work, however, it works for most important
stuff.
Signed-off-by: Ahmed Samy <f.fallen45 at gmail.com>
---
Makefile-ccan | 2 +
ccan/cpuid/BSD-MIT | 1 +
ccan/cpuid/_info | 38 ++++++++++
ccan/cpuid/cpuid.c | 186 ++++++++++++++++++++++++++++++++++++++++++++++++
ccan/cpuid/cpuid.h | 147 ++++++++++++++++++++++++++++++++++++++
ccan/cpuid/issupprted.S | 29 ++++++++
ccan/cpuid/test/run.c | 47 ++++++++++++
7 files changed, 450 insertions(+)
create mode 120000 ccan/cpuid/BSD-MIT
create mode 100644 ccan/cpuid/_info
create mode 100644 ccan/cpuid/cpuid.c
create mode 100644 ccan/cpuid/cpuid.h
create mode 100644 ccan/cpuid/issupprted.S
create mode 100644 ccan/cpuid/test/run.c
diff --git a/Makefile-ccan b/Makefile-ccan
index d81031e..fd959ff 100644
--- a/Makefile-ccan
+++ b/Makefile-ccan
@@ -42,6 +42,7 @@ MODS_WITH_SRC := antithread \
ciniparser \
crc \
crcsync \
+ cpuid \
daemonize \
daemon_with_notify \
dgraph \
@@ -112,6 +113,7 @@ DIRS=$(patsubst %/, %, $(sort $(foreach m, $(filter-out $(MODS_EXCLUDE), $(MODS_
ccan/%-Makefile:
@echo $@: $(wildcard ccan/$*/*.[ch]) ccan/$*/_info > $@
@echo ccan/$*.o: $(patsubst %.c, %.o, $(wildcard ccan/$*/*.c)) >> $@
+ @echo ccan/$*.o: $(patsubst %.S, %.o, $(wildcard ccan/$*/*.S)) >> $@
# We compile all the ccan/foo/*.o files together into ccan/foo.o
OBJFILES=$(DIRS:=.o)
diff --git a/ccan/cpuid/BSD-MIT b/ccan/cpuid/BSD-MIT
new file mode 120000
index 0000000..2354d12
--- /dev/null
+++ b/ccan/cpuid/BSD-MIT
@@ -0,0 +1 @@
+../../licenses/BSD-MIT
\ No newline at end of file
diff --git a/ccan/cpuid/_info b/ccan/cpuid/_info
new file mode 100644
index 0000000..b6e0caf
--- /dev/null
+++ b/ccan/cpuid/_info
@@ -0,0 +1,38 @@
+#include "cpuid.h"
+
+/**
+ * cpuid - a CPUID instruction parser for x86/x86_64 CPUs.
+ *
+ * This module tries to keep-it-simple to get information about the CPU
+ * from the CPU.
+ *
+ * Example:
+ * #include <ccan/cpuid/cpuid.h>
+ *
+ * int main()
+ * {
+ * int highest;
+ * cpuid(CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED, &highest);
+ * printf ("Highest extended function supported: %d\n", highest);
+ *
+ * return 0;
+ * }
+ *
+ * Author: Ahmed Samy <f.fallen45 at gmail.com>
+ * License: MIT
+ * Version: 0.1
+ */
+
+int main(int argc, char *argv[])
+{
+ if (argc != 2)
+ return 1;
+
+ if (strcmp(argv[1], "depends") == 0) {
+ /* Nothing. */
+ return 0;
+ }
+
+ return 1;
+}
+
diff --git a/ccan/cpuid/cpuid.c b/ccan/cpuid/cpuid.c
new file mode 100644
index 0000000..f25688e
--- /dev/null
+++ b/ccan/cpuid/cpuid.c
@@ -0,0 +1,186 @@
+/*
+ * Copyright (c) 2013 Ahmed Samy <f.fallen45 at gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file has been written with some help from wikipedia:
+ * http://en.wikipedia.org/wiki/CPUID
+ */
+#include <stdint.h>
+
+#include "cpuid.h"
+
+enum {
+ CPU_PROC_BRAND_STRING_INTERNAL0 = 0x80000003,
+ CPU_PROC_BRAND_STRING_INTERNAL1 = 0x80000004
+};
+
+#ifndef _MSC_VER
+static void ___cpuid(cpuid_t info, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
+{
+ __asm__(
+ "xchg %%ebx, %%edi\n\t" /* 32bit PIC: Don't clobber ebx. */
+ "cpuid\n\t"
+ "xchg %%ebx, %%edi\n\t"
+ : "=a"(*eax), "=D"(*ebx), "=c"(*ecx), "=d"(*edx)
+ : "0" (info)
+ );
+}
+#else
+#include <intrin.h>
+
+static void ___cpuid(cpuid_t info, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
+{
+ uint32_t registers[4];
+ __cpuid(registers, info);
+
+ *eax = registers[0];
+ *ebx = registers[1];
+ *ecx = registers[2];
+ *edx = registers[3];
+}
+#endif
+
+int highest_ext_func_supported(void)
+{
+ static int highest;
+
+ if (!highest) {
+ asm volatile(
+ "cpuid\n\t"
+ : "=a" (highest)
+ : "a" (CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED)
+ );
+ }
+
+ return highest;
+}
+
+int cpuid_test_feature(cpuid_t feature)
+{
+ if (feature > CPU_VIRT_PHYS_ADDR_SIZES || feature < CPU_EXTENDED_PROC_INFO_FEATURE_BITS)
+ return 0;
+
+ return (feature <= highest_ext_func_supported());
+}
+
+int cpuid_has_feature(cpufeature_t feature)
+{
+ uint32_t eax, ebx, ecx, edx;
+
+ ___cpuid(CPU_PROCINFO_AND_FEATUREBITS, &eax, &ebx, &ecx, &edx);
+ switch (feature) {
+ case CF_MMX:
+ case CF_SSE:
+ case CF_SSE2:
+ return (edx & ((int)feature)) != 0;
+ case CF_SSE3:
+ case CF_SSSE3:
+ case CF_SSE41:
+ case CF_SSE42:
+ case CF_AVX:
+ case CF_FMA:
+ return (ecx & ((int)feature)) != 0;
+ }
+
+ return 0;
+}
+
+int cpuid_has_ext_feature(cpuextfeature_t extfeature)
+{
+ uint32_t eax, ebx, ecx, edx;
+ if (!cpuid_test_feature(CPU_EXTENDED_PROC_INFO_FEATURE_BITS))
+ return 0;
+
+ ___cpuid(CPU_EXTENDED_PROC_INFO_FEATURE_BITS, &eax, &ebx, &ecx, &edx);
+ switch (extfeature) {
+ case CEF_x64:
+ return (edx & ((int)extfeature)) != 0;
+ case CEF_SSE4a:
+ case CEF_FMA4:
+ case CEF_XOP:
+ return (ecx & ((int)extfeature)) != 0;
+ }
+
+ return 0;
+}
+
+void cpuid(cpuid_t info, void *buf)
+{
+ /* Sanity checks, make sure we're not trying to do something
+ * invalid or we are trying to get information that isn't supported
+ * by the CPU. */
+ if (info > CPU_VIRT_PHYS_ADDR_SIZES || (info > CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED
+ && !cpuid_test_feature(info)))
+ return;
+
+ uint32_t *ubuf = buf;
+ if (info == CPU_PROC_BRAND_STRING) {
+ ___cpuid(CPU_PROC_BRAND_STRING, &ubuf[0], &ubuf[1], &ubuf[2], &ubuf[3]);
+ ___cpuid(CPU_PROC_BRAND_STRING_INTERNAL0, &ubuf[4], &ubuf[5], &ubuf[6], &ubuf[7]);
+ ___cpuid(CPU_PROC_BRAND_STRING_INTERNAL1, &ubuf[8], &ubuf[9], &ubuf[10], &ubuf[11]);
+ return;
+ } else if (info == CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED) {
+ *ubuf = highest_ext_func_supported();
+ return;
+ }
+
+ uint32_t eax, ebx, ecx, edx;
+ ___cpuid(info, &eax, &ebx, &ecx, &edx);
+
+ switch (info) {
+ case CPU_VENDORID:
+ ubuf[0] = ebx;
+ ubuf[1] = edx;
+ ubuf[2] = ecx;
+ break;
+ case CPU_PROCINFO_AND_FEATUREBITS:
+ ubuf[0] = eax; /* The so called "signature" of the CPU. */
+ ubuf[1] = edx; /* Feature flags #1. */
+ ubuf[2] = ecx; /* Feature flags #2. */
+ ubuf[3] = ebx; /* Additional feature information. */
+ break;
+ case CPU_CACHE_AND_TLBD_INFO:
+ ubuf[0] = eax;
+ ubuf[1] = ebx;
+ ubuf[2] = ecx;
+ ubuf[3] = edx;
+ break;
+ case CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
+ ubuf[0] = edx;
+ ubuf[1] = ecx;
+ break;
+ case CPU_L1_CACHE_AND_TLB_IDS:
+ break;
+ case CPU_EXTENDED_L2_CACHE_FEATURES:
+ ubuf[0] = (ecx & 0xFF); /* Cache size */
+ ubuf[1] = (ecx >> 12) & 0xF; /* Line size */
+ ubuf[2] = (ecx >> 16) & 0xFFFF; /* Associativity */
+ break;
+ case CPU_ADV_POWER_MGT_INFO:
+ break;
+ case CPU_VIRT_PHYS_ADDR_SIZES:
+ *ubuf = eax;
+ break;
+ default:
+ *ubuf = 0xbaadf00d;
+ break;
+ }
+}
+
diff --git a/ccan/cpuid/cpuid.h b/ccan/cpuid/cpuid.h
new file mode 100644
index 0000000..d119e4f
--- /dev/null
+++ b/ccan/cpuid/cpuid.h
@@ -0,0 +1,147 @@
+/*
+ * Copyright (c) 2013 Ahmed Samy <f.fallen45 at gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#ifndef CCAN_CPUID_H
+#define CCAN_CPUID_H
+
+typedef enum cpuid {
+ CPU_VENDORID = 0,
+ CPU_PROCINFO_AND_FEATUREBITS = 1,
+ CPU_CACHE_AND_TLBD_INFO = 2,
+
+ CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED = 0x80000000,
+ CPU_EXTENDED_PROC_INFO_FEATURE_BITS = 0x80000001,
+ CPU_PROC_BRAND_STRING = 0x80000002,
+ CPU_L1_CACHE_AND_TLB_IDS = 0x80000005,
+ CPU_EXTENDED_L2_CACHE_FEATURES = 0x80000006,
+ CPU_ADV_POWER_MGT_INFO = 0x80000007,
+ CPU_VIRT_PHYS_ADDR_SIZES = 0x80000008
+} cpuid_t;
+
+typedef enum cpufeature {
+ CF_MMX = 1 << 23,
+ CF_SSE = 1 << 25,
+ CF_SSE2 = 1 << 26,
+ CF_SSE3 = 1 << 0,
+
+ CF_SSSE3 = 1 << 9,
+ CF_SSE41 = 1 << 19,
+ CF_SSE42 = 1 << 20,
+
+ CF_AVX = 1 << 28,
+ CF_FMA = 1 << 12
+} cpufeature_t;
+
+typedef enum cpuextfeature {
+ CEF_x64 = 1 << 29,
+ CEF_SSE4a = 1 << 6,
+ CEF_FMA4 = 1 << 16,
+ CEF_XOP = 1 << 11
+} cpuextfeature_t;
+
+/* returns 1 if the cpuid instruction is supported, 0 otherwise.
+ *
+ * CPUID isn't supported on very old Intel CPUs.
+ * Defined in issupprted.S
+ */
+int cpuid_is_supported(void);
+
+/* returns the highest extended function supported.
+ *
+ * This is the same as calling:
+ * cpuid(CPU_HIGHEST_EEXTENDED_FUNCTION_SUPPORTED, &highest);
+ *
+ * This is made visible to the linker because it's easier to call it
+ * instead of calling cpuid with less type-checking. cpuid calls this.
+ */
+int highest_ext_func_supported(void);
+
+/* Get Some information from the CPU.
+ * This function expects buf to be a valid pointer to a string/int/...
+ * depending on the requested information.
+ *
+ * For CPU_VENDOR_ID:
+ * Returns a string into buf.
+ *
+ * For CPU_PROCINFO_AND_FEATUREBITS:
+ * buf[0]:
+ * - 3:0 - Stepping
+ * - 7:4 - Model
+ * - 11:8 - Family
+ * - 13:12 - Processor Type
+ * - 19:16 - Extended Model
+ * - 27:20 - Extended family
+ * buf[1] and buf[2]:
+ * Feature flags
+ * buf[3]:
+ * Additional feature information.
+ *
+ * For CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
+ * Returns the highest supported function in *buf (expects an integer ofc)
+ *
+ * For CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
+ * Returns them in buf[0] and buf[1].
+ *
+ * For CPU_VIRT_PHYS_ADDR_SIZES:
+ * Returns it as an integer in *buf.
+ *
+ * For CPU_PROC_BRAND_STRING:
+ * Have a char array with at least 48 bytes assigned to it.
+ *
+ * If an invalid flag has been passed a 0xbaadf00d is returned in *buf.
+ */
+void cpuid(cpuid_t info, void *buf);
+
+/*
+ * Returns 1 if feature is supported, 0 otherwise.
+ *
+ * The feature parameter must be >= CPU_EXTENDED_PROC_INFO_FEATURE_BITS
+ * and <= CPU_VIRT_PHYS_ADDR_SIZES.
+ */
+int cpuid_test_feature(cpuid_t feature);
+
+/* Test if the CPU supports MMX/SSE*
+ *
+ * Returns 1 if the feature is available, 0 otherwise.
+ */
+#define cpuid_has_mmx() cpuid_has_feature(CF_MMX)
+#define cpuid_has_sse() cpuid_has_feature(CF_SSE)
+#define cpuid_has_sse2() cpuid_has_feature(CF_SSE2)
+#define cpuid_has_sse3() cpuid_has_feature(CF_SSE3)
+#define cpuid_has_ssse3() cpuid_has_feature(CF_SSSE3)
+#define cpuid_has_sse41() cpuid_has_feature(CF_SSE41)
+#define cpuid_has_sse42() cpuid_has_feature(CF_SSE42)
+#define cpuid_has_avx() cpuid_has_feature(CF_AVX)
+#define cpuid_has_fma() cpuid_has_feature(CF_FMA)
+int cpuid_has_feature(cpufeature_t feature);
+
+/* Test if the CPU supports an extended feature.
+ *
+ * Returns 1 if available, 0 otherwise.
+ */
+#define cpuid_has_x64() cpuid_has_ext_feature(CEF_x64)
+#define cpuid_has_sse4a() cpuid_has_ext_feature(CEF_SSE4a)
+#define cpuid_has_fma4() cpuid_has_ext_feature(CEF_FMA4)
+#define cpuid_has_xop() cpuid_has_ext_feature(CEF_XOP)
+int cpuid_has_ext_feature(cpuextfeature_t extfeature);
+
+#endif
+
diff --git a/ccan/cpuid/issupprted.S b/ccan/cpuid/issupprted.S
new file mode 100644
index 0000000..8fe8596
--- /dev/null
+++ b/ccan/cpuid/issupprted.S
@@ -0,0 +1,29 @@
+/*
+ Test if the CPUID instruction is available.
+ returns 1 if so, 0 otherwise. */
+
+.section .text
+.global cpuid_is_supported
+.type cpuid_is_supported, @function
+cpuid_is_supported:
+ nop
+
+ pushfl
+ popl %eax
+ movl %eax, %ecx
+ xorl $0x200000, %eax
+ pushl %eax
+ popfl
+
+ pushfl
+ popl %eax
+ xorl %ecx, %eax
+ shrl $21, %eax
+ andl $1, %eax
+ pushl %ecx
+ popfl
+
+ ret
+
+ .size cpuid_is_supported, .-cpuid_is_supported
+
diff --git a/ccan/cpuid/test/run.c b/ccan/cpuid/test/run.c
new file mode 100644
index 0000000..65741f1
--- /dev/null
+++ b/ccan/cpuid/test/run.c
@@ -0,0 +1,47 @@
+#include "cpuid.h"
+
+#include <stdio.h>
+
+int main()
+{
+ if (!cpuid_is_supported()) {
+ printf ("CPUID instruction is not supported by this CPU\n");
+ return 1;
+ }
+
+ printf ("MMX: %s\n", cpuid_has_mmx() ? "Yes" : "No");
+ printf ("SSE: %s\n", cpuid_has_sse() ? "Yes" : "No");
+ printf ("SSE2: %s\n", cpuid_has_sse2() ? "Yes" : "No");
+ printf ("SSE3: %s\n", cpuid_has_sse3() ? "Yes" : "No");
+ printf ("x64: %s\n", cpuid_has_x64() ? "Yes" : "No");
+
+ char buf[128];
+ cpuid(CPU_VENDORID, buf);
+ printf ("Vendor ID: %s\n", buf);
+
+ cpuid(CPU_PROC_BRAND_STRING, buf);
+ printf ("Processor Brand: %s\n", buf);
+
+ int addr;
+ cpuid(CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED, &addr);
+ printf ("Highest extended function supported: %#010x\n", addr);
+
+ int virtphys_size;
+ cpuid(CPU_VIRT_PHYS_ADDR_SIZES, &virtphys_size);
+ printf ("Virtual and physical address sizes: %d\n", virtphys_size);
+
+ int extfeatures[2];
+ cpuid(CPU_EXTENDED_PROC_INFO_FEATURE_BITS, extfeatures);
+ printf ("Extended processor info and feature bits: %d %d\n", extfeatures[0], extfeatures[1]);
+
+ int l2features[3];
+ cpuid(CPU_EXTENDED_L2_CACHE_FEATURES, l2features);
+ printf ("L2 Cache Size: %u KB\tLine Size: %u bytes\tAssociativity: %02xh\n",
+ l2features[0], l2features[1], l2features[2]);
+
+ int invalid;
+ cpuid(0x0ffffffUL, &invalid);
+ printf ("Testing invalid: %#010x\n", invalid);
+ return 0;
+}
+
--
1.8.4
More information about the ccan
mailing list