1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2025-05-16 20:19:13 +05:30
gists/c-programming/sys/cpuid_vendor_id.mod.c

88 lines
2.3 KiB
C

/*
* cpuid_vendor_id.mod.c
*
* The file is a modified version of a gist "cpuid_vendor_id.c" by leiless:
* https://gist.github.com/leiless/8b8603ae31c00fe38d2e97d94462a5a5
* (revision 73830d4b10090dcf6748cc60a0aacee252cd9e9d2dd54549aa7b995321380091)
*
* The following license information applies for the modifications:
* Author: Intel A80486DX2-66
* License: Creative Commons Zero 1.0 Universal or Unlicense
* SPDX-License-Identifier: CC0-1.0 OR Unlicense
*/
#if !(defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || \
defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64) || \
defined(__i386) || defined(__i386__) || defined(__IA32__) || \
defined(_M_I86) || defined(_M_IX86) || defined(__X86__) || defined(_X86_))
# error "This code isn't supposed to work on non-x86 CPUs."
#endif
#include <stdint.h>
#include <stdio.h>
#ifdef _WIN32
# include <intrin.h> // __cpuid()
#endif
typedef uint32_t cpuid_t[4];
enum CPU_registers {
EAX = 0,
EBX,
ECX,
EDX
};
// https://elixir.bootlin.com/linux/latest/source/arch/x86/include/asm/
// processor.h#L216
//
// https://stackoverflow.com/questions/6491566/getting-the-machine-serial
// -number-and-cpu-id-using-c-c-in-linux
//
// https://stackoverflow.com/questions/1666093/cpuid-implementations-in-c
static inline void native_cpuid(uint32_t function_id, cpuid_t r) {
#ifdef _WIN32
__cpuid((int32_t*) r, (int32_t) function_id);
#else
r[EAX] = function_id;
r[ECX] = 0;
// NOTE:XXX: Register ECX is often an input as well as an output
asm volatile("cpuid"
: "=a" (r[EAX]),
"=b" (r[EBX]),
"=c" (r[ECX]),
"=d" (r[EDX])
: "0" (r[EAX]), "2" (r[ECX])
: "memory");
#endif
}
#define VENDOR_ID_LEN 12
/*
* FIXME: you have to make sure the vendor argument is at least lengthed
* VENDOR_ID_LEN
*/
static inline void cpuid_vendor_id(char vendor[VENDOR_ID_LEN]) {
// Always initialize the result in case of buggy CPU (like ES/QS CPUs)
cpuid_t v;
native_cpuid(0, v);
// https://learn.microsoft.com/en-us/cpp/intrinsics/cpuid-cpuidex
// ?view=msvc-170#example
((uint32_t*) vendor)[0] = v[EBX];
((uint32_t*) vendor)[1] = v[EDX];
((uint32_t*) vendor)[2] = v[ECX];
}
int main(void) {
char vendor_string[VENDOR_ID_LEN];
cpuid_vendor_id(vendor_string);
fputs("CPU Vendor ID: '", stdout);
fwrite(vendor_string, sizeof(char), 12, stdout);
fputs("'\n", stdout);
return 0;
}