Moved to asm cpuid call. Support for CPU name string. Output skecth on main

This commit is contained in:
Dr-Noob
2018-03-24 23:57:49 +01:00
parent 7afefdb672
commit 97e21a64ba
7 changed files with 132 additions and 16 deletions

15
02h.c
View File

@@ -1,6 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "02h.h" #include "02h.h"
#include "cpuid.h"
#define MASK1 0xFF; #define MASK1 0xFF;
#define MASK2 0xFF00; #define MASK2 0xFF00;
@@ -23,18 +24,6 @@ struct TLB {
//http://www.sandpile.org/x86/cpuid.htm //http://www.sandpile.org/x86/cpuid.htm
//http://www.hugi.scene.org/online/coding/hugi%2016%20-%20corawhd4.htm //http://www.hugi.scene.org/online/coding/hugi%2016%20-%20corawhd4.htm
/*** EXEC CPUID INSTRUCTION ***/
static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
unsigned int *ecx, unsigned int *edx)
{
asm volatile("cpuid"
: "=a" (*eax),
"=b" (*ebx),
"=c" (*ecx),
"=d" (*edx)
: "0" (*eax), "2" (*ecx));
}
/*** ACCORDING TO CPUID TABLE ***/ /*** ACCORDING TO CPUID TABLE ***/
//TO BE IMPLEMENTED //TO BE IMPLEMENTED
void fillWithDescriptor(unsigned int desc, struct level2* data) { void fillWithDescriptor(unsigned int desc, struct level2* data) {
@@ -328,7 +317,7 @@ struct level2* fillLevel2(struct level2* data) {
unsigned eax, ebx, ecx, edx; unsigned eax, ebx, ecx, edx;
eax = 2; eax = 2;
native_cpuid(&eax, &ebx, &ecx, &edx); cpuid(&eax, &ebx, &ecx, &edx);
unsigned int desc[16]; unsigned int desc[16];

View File

@@ -2,12 +2,13 @@ CXX=gcc
CXXFLAGS=-g CXXFLAGS=-g
MAIN=main.c SOURCE=main.c 02h.c extended.c cpuid.c
HEADERS=02h.h extended.h cpuid.h
OUTPUT=cpufetch OUTPUT=cpufetch
$(OUTPUT): Makefile $(MAIN) 02h.c 02h.h $(OUTPUT): Makefile $(SOURCE) $(HEADERS)
$(CXX) $(CXXFLAGS) $(MAIN) 02h.c -o $(OUTPUT) $(CXX) $(CXXFLAGS) $(SOURCE) -o $(OUTPUT)
clean: clean:
@rm $(OUTPUT1) $(OUTPUT2) @rm $(OUTPUT1) $(OUTPUT2)

12
cpuid.c Normal file
View File

@@ -0,0 +1,12 @@
#include "cpuid.h"
void cpuid(unsigned int *eax, unsigned int *ebx,
unsigned int *ecx, unsigned int *edx)
{
asm volatile("cpuid"
: "=a" (*eax),
"=b" (*ebx),
"=c" (*ecx),
"=d" (*edx)
: "0" (*eax), "2" (*ecx));
}

6
cpuid.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef __CPUID__
#define __CPUID__
void cpuid(unsigned int *eax, unsigned int *ebx,unsigned int *ecx, unsigned int *edx);
#endif

77
extended.c Normal file
View File

@@ -0,0 +1,77 @@
#include "extended.h"
char* getCPUName() {
unsigned eax, ebx, ecx, edx;
//First, check we can use extended
eax = 0x80000000;
cpuid(&eax, &ebx, &ecx, &edx);
if(eax < 0x80000001)
return NULL;
//We can, fetch name
char* name = malloc(sizeof(char)*64);
eax = 0x80000002;
cpuid(&eax, &ebx, &ecx, &edx);
name[__COUNTER__] = eax & MASK;
name[__COUNTER__] = (eax>>8) & MASK;
name[__COUNTER__] = (eax>>16) & MASK;
name[__COUNTER__] = (eax>>24) & MASK;
name[__COUNTER__] = ebx & MASK;
name[__COUNTER__] = (ebx>>8) & MASK;
name[__COUNTER__] = (ebx>>16) & MASK;
name[__COUNTER__] = (ebx>>24) & MASK;
name[__COUNTER__] = ecx & MASK;
name[__COUNTER__] = (ecx>>8) & MASK;
name[__COUNTER__] = (ecx>>16) & MASK;
name[__COUNTER__] = (ecx>>24) & MASK;
name[__COUNTER__] = edx & MASK;
name[__COUNTER__] = (edx>>8) & MASK;
name[__COUNTER__] = (edx>>16) & MASK;
name[__COUNTER__] = (edx>>24) & MASK;
eax = 0x80000003;
cpuid(&eax, &ebx, &ecx, &edx);
name[__COUNTER__] = eax & MASK;
name[__COUNTER__] = (eax>>8) & MASK;
name[__COUNTER__] = (eax>>16) & MASK;
name[__COUNTER__] = (eax>>24) & MASK;
name[__COUNTER__] = ebx & MASK;
name[__COUNTER__] = (ebx>>8) & MASK;
name[__COUNTER__] = (ebx>>16) & MASK;
name[__COUNTER__] = (ebx>>24) & MASK;
name[__COUNTER__] = ecx & MASK;
name[__COUNTER__] = (ecx>>8) & MASK;
name[__COUNTER__] = (ecx>>16) & MASK;
name[__COUNTER__] = (ecx>>24) & MASK;
name[__COUNTER__] = edx & MASK;
name[__COUNTER__] = (edx>>8) & MASK;
name[__COUNTER__] = (edx>>16) & MASK;
name[__COUNTER__] = (edx>>24) & MASK;
eax = 0x80000004;
cpuid(&eax, &ebx, &ecx, &edx);
name[__COUNTER__] = eax & MASK;
name[__COUNTER__] = (eax>>8) & MASK;
name[__COUNTER__] = (eax>>16) & MASK;
name[__COUNTER__] = (eax>>24) & MASK;
name[__COUNTER__] = ebx & MASK;
name[__COUNTER__] = (ebx>>8) & MASK;
name[__COUNTER__] = (ebx>>16) & MASK;
name[__COUNTER__] = (ebx>>24) & MASK;
name[__COUNTER__] = ecx & MASK;
name[__COUNTER__] = (ecx>>8) & MASK;
name[__COUNTER__] = (ecx>>16) & MASK;
name[__COUNTER__] = (ecx>>24) & MASK;
name[__COUNTER__] = edx & MASK;
name[__COUNTER__] = (edx>>8) & MASK;
name[__COUNTER__] = (edx>>16) & MASK;
name[__COUNTER__] = (edx>>24) & MASK;
name[__COUNTER__] = '\0';
return name;
}

10
extended.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef __EXTENDED__
#define __EXTENDED__
#define MASK 0xFF
#include "cpuid.h"
#include <stdlib.h>
char* getCPUName();
#endif

21
main.c
View File

@@ -1,9 +1,30 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "02h.h" #include "02h.h"
#include "extended.h"
/***
SAMPLE OUTPUT
Name: Intel Core i7-4790K
Arch: 64 Bits
Frecuency: 4.0 GHz
NºCores: 4 cores(8 threads)
AXV: AVX,AVX2
SSE: SSE,SSE2,SSE4.1,SSE4.2
FMA: FMA3
AES: Yes
SHA: No
L1 Size: 32KB(Data)32KB(Instructions)
L2 Size: 512KB
L3 Size: 8MB
Peak FLOPS: 512 GFLOP/s(in simple precision)
***/
int main() { int main() {
struct level2* level2 = fillLevel2(level2); struct level2* level2 = fillLevel2(level2);
debugLevel2(level2); debugLevel2(level2);
freeLevel2(level2); freeLevel2(level2);
printf("%s\n",getCPUName());
} }