-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
198 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include "cpu.h" | ||
#include "../../lib/iolib.h" | ||
|
||
void check_cpu_features() { | ||
check_cpu_id(); | ||
check_cpu_vendor(); | ||
get_cpu_features(); | ||
} | ||
|
||
// Letters are stored as int | ||
// Example: | ||
// ebx might look this: 1970169159, which is actually translated to 1110101 01101110 01100101 01000111 | ||
// Each 8 bytes group can represent an Ascii letter. | ||
// Once extracted, we need to put them in the right order to get the cpu vendor | ||
// See here for more information: | ||
// https://wiki.osdev.org/CPUID | ||
void display_cpu_vendor(int ebx, int edx, int ecx) { | ||
unsigned char ebx_l1 = ebx & 0xFF; | ||
unsigned char ebx_l2 = (ebx >> 8) & 0xFF; | ||
unsigned char ebx_l3 = (ebx >> 16) & 0xFF; | ||
unsigned char ebx_l4 = (ebx >> 24) & 0xFF; | ||
|
||
unsigned char edx_l1 = edx & 0xFF; | ||
unsigned char edx_l2 = (edx >> 8) & 0xFF; | ||
unsigned char edx_l3 = (edx >> 16) & 0xFF; | ||
unsigned char edx_l4 = (edx >> 24) & 0xFF; | ||
|
||
unsigned char ecx_l1 = ecx & 0xFF; | ||
unsigned char ecx_l2 = (ecx >> 8) & 0xFF; | ||
unsigned char ecx_l3 = (ecx >> 16) & 0xFF; | ||
unsigned char ecx_l4 = (ecx >> 24) & 0xFF; | ||
|
||
printd(KERN_INFO, "CPU is %c%c%c%c%c%c%c%c%c%c%c%c", | ||
ebx_l1, ebx_l2, ebx_l3, ebx_l4, | ||
edx_l1, edx_l2, edx_l3, edx_l4, | ||
ecx_l1, ecx_l2, ecx_l3, ecx_l4 | ||
); | ||
} | ||
|
||
void display_cpu_features(int s1, int s2) { | ||
printd(KERN_INFO, "CPU features are: %d %d", s1, s2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* cpu.h :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: luca </var/spool/mail/luca> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/02/02 14:11:48 by luca #+# #+# */ | ||
/* Updated: 2024/02/02 14:11:48 by luca ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#ifndef KFS_CPU_H | ||
#define KFS_CPU_H | ||
|
||
// Vendor strings from CPUs. | ||
#define CPUID_VENDOR_AMD "AuthenticAMD" | ||
#define CPUID_VENDOR_AMD_OLD "AMDisbetter!" // Early engineering samples of AMD K5 processor | ||
#define CPUID_VENDOR_INTEL "GenuineIntel" | ||
#define CPUID_VENDOR_VIA "VIA VIA VIA " | ||
#define CPUID_VENDOR_TRANSMETA "GenuineTMx86" | ||
#define CPUID_VENDOR_TRANSMETA_OLD "TransmetaCPU" | ||
#define CPUID_VENDOR_CYRIX "CyrixInstead" | ||
#define CPUID_VENDOR_CENTAUR "CentaurHauls" | ||
#define CPUID_VENDOR_NEXGEN "NexGenDriven" | ||
#define CPUID_VENDOR_UMC "UMC UMC UMC " | ||
#define CPUID_VENDOR_SIS "SiS SiS SiS " | ||
#define CPUID_VENDOR_NSC "Geode by NSC" | ||
#define CPUID_VENDOR_RISE "RiseRiseRise" | ||
#define CPUID_VENDOR_VORTEX "Vortex86 SoC" | ||
#define CPUID_VENDOR_AO486 "MiSTer AO486" | ||
#define CPUID_VENDOR_AO486_OLD "GenuineAO486" | ||
#define CPUID_VENDOR_ZHAOXIN " Shanghai " | ||
#define CPUID_VENDOR_HYGON "HygonGenuine" | ||
#define CPUID_VENDOR_ELBRUS "E2K MACHINE " | ||
|
||
// Vendor strings from hypervisors. | ||
#define CPUID_VENDOR_QEMU "TCGTCGTCGTCG" | ||
#define CPUID_VENDOR_KVM " KVMKVMKVM " | ||
#define CPUID_VENDOR_VMWARE "VMwareVMware" | ||
#define CPUID_VENDOR_VIRTUALBOX "VBoxVBoxVBox" | ||
#define CPUID_VENDOR_XEN "XenVMMXenVMM" | ||
#define CPUID_VENDOR_HYPERV "Microsoft Hv" | ||
#define CPUID_VENDOR_PARALLELS " prl hyperv " | ||
#define CPUID_VENDOR_PARALLELS_ALT " lrpepyh vr " // Sometimes Parallels incorrectly encodes "prl hyperv" as "lrpepyh vr" due to an endianness mismatch. | ||
#define CPUID_VENDOR_BHYVE "bhyve bhyve " | ||
#define CPUID_VENDOR_QNX " QNXQVMBSQG " | ||
|
||
void check_cpu_features(); | ||
void display_cpu_vendor(int s1, int s2, int s3); | ||
void display_cpu_features(int s1, int s2); | ||
|
||
// ASM functions | ||
extern void check_cpu_id(); | ||
extern void check_cpu_vendor(); | ||
extern void get_cpu_features(); | ||
|
||
#endif //KFS_CPU_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
|
||
[GLOBAL check_cpu_id] | ||
[GLOBAL check_cpu_vendor] | ||
[GLOBAL get_cpu_features] | ||
|
||
extern printd | ||
extern display_cpu_vendor | ||
extern display_cpu_features | ||
|
||
cpuid_not_supported db 'CPUID is not supported', 0 | ||
cpuid_supported db 'CPUID is supported', 0 | ||
|
||
check_cpu_id: | ||
push ebp ; Stack & Frame Setup and Save | ||
mov ebp, esp | ||
|
||
pushfd ; Save EFLAGS | ||
pushfd ; Store EFLAGS | ||
xor dword [esp],0x00200000 ; Invert the ID bit in stored EFLAGS | ||
popfd ; Load stored EFLAGS (with ID bit inverted) | ||
pushfd ; Store EFLAGS again (ID bit may or may not be inverted) | ||
pop eax ; eax = modified EFLAGS (ID bit may or may not be inverted) | ||
xor eax,[esp] ; eax = whichever bits were changed | ||
popfd ; Restore original EFLAGS | ||
and eax, 0x00200000 ; eax = zero if ID bit can't be changed, else non-zero | ||
cmp eax, 0 ; Compare eax to 0 | ||
je id_not_supported ; if equal, jump to id_not_supported | ||
jmp id_supported ; Else, jump to id_supported | ||
ret | ||
|
||
id_not_supported: | ||
push 0x0 ; Push flags | ||
push cpuid_not_supported ; Push String | ||
push 0x3 ; Push Info Type | ||
call printd ; Call printf | ||
mov esp, ebp ; Stack & Frame Restore | ||
pop ebp | ||
ret | ||
|
||
id_supported: | ||
push 0x0 ; Push flags | ||
push cpuid_supported ; Push String | ||
push 0x0 ; Push Info Type | ||
call printd ; Call printf | ||
mov esp, ebp ; Stack & Frame Restore | ||
pop ebp | ||
ret | ||
|
||
check_cpu_vendor: | ||
push ebp ; Stack & Frame Setup and Save | ||
mov ebp, esp | ||
|
||
mov eax, 0x0 ; Get CPU Vendor ID String | ||
cpuid ; Get Infos | ||
push ecx ; Infos are returned in ebx, edx, ecx | ||
push edx ; We push thoses on the stack to pass them to C function | ||
push ebx | ||
call display_cpu_vendor ; Call our C function | ||
|
||
mov esp, ebp ; Stack & Frame Restore | ||
pop ebp | ||
ret | ||
|
||
get_cpu_features: | ||
push ebp ; Stack & Frame Setup and Save | ||
mov ebp, esp | ||
|
||
mov eax, 0x1 ; Get CPU Vendor ID String | ||
cpuid | ||
push ecx ; Infos are returned in edx, ecx | ||
push edx ; We push thoses on the stack to pass them to C function | ||
call display_cpu_features | ||
|
||
mov esp, ebp ; Stack & Frame Restore | ||
pop ebp | ||
ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters