Optimize RNG functions

This commit is contained in:
Alex 2023-04-16 22:05:02 +03:00
parent 37f5b77421
commit b15f738e16
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
2 changed files with 68 additions and 67 deletions

View File

@ -24,7 +24,6 @@
#include <printf.h> #include <printf.h>
#include <cwalk.h> #include <cwalk.h>
#include <md5.h> #include <md5.h>
// #include <ini.h>
#include "../../kernel.h" #include "../../kernel.h"
#include "../../DAPI.hpp" #include "../../DAPI.hpp"
@ -36,16 +35,6 @@ NewLock(DriverInterruptLock);
namespace Driver namespace Driver
{ {
const char *DriverTypesName[] = {
"Unknown",
"Generic",
"Display",
"Network",
"Storage",
"FileSystem",
"Input",
"Audio"};
void Driver::Panic() void Driver::Panic()
{ {
#ifdef DEBUG #ifdef DEBUG

View File

@ -24,7 +24,12 @@ namespace Random
uint16_t rand16() uint16_t rand16()
{ {
int RDRANDFlag = 0; #if defined(a86)
static int RDRANDFlag = 0x1A1A;
if (unlikely(RDRANDFlag == 0x1A1A))
{
if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) != 0)
{
if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0) if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0)
{ {
CPU::x86::AMD::CPUID0x00000001 cpuid; CPU::x86::AMD::CPUID0x00000001 cpuid;
@ -37,11 +42,9 @@ namespace Random
cpuid.Get(); cpuid.Get();
RDRANDFlag = cpuid.ECX.RDRAND; RDRANDFlag = cpuid.ECX.RDRAND;
} }
}
}
if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) == 0)
RDRANDFlag = 0;
#if defined(a86)
if (RDRANDFlag) if (RDRANDFlag)
{ {
uint16_t RDRANDValue = 0; uint16_t RDRANDValue = 0;
@ -49,15 +52,21 @@ namespace Random
: "=r"(RDRANDValue)); : "=r"(RDRANDValue));
return RDRANDValue; return RDRANDValue;
} }
#endif
Seed = Seed * 1103515245 + 12345; Seed = Seed * 1103515245 + 12345;
return (uint16_t)(Seed / 65536) % __UINT16_MAX__; return (uint16_t)(Seed / 65536) % __UINT16_MAX__;
#endif
return 0;
} }
uint32_t rand32() uint32_t rand32()
{ {
int RDRANDFlag = 0; #if defined(a86)
static int RDRANDFlag = 0x1A1A;
if (unlikely(RDRANDFlag == 0x1A1A))
{
if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) != 0)
{
if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0) if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0)
{ {
CPU::x86::AMD::CPUID0x00000001 cpuid; CPU::x86::AMD::CPUID0x00000001 cpuid;
@ -70,11 +79,9 @@ namespace Random
cpuid.Get(); cpuid.Get();
RDRANDFlag = cpuid.ECX.RDRAND; RDRANDFlag = cpuid.ECX.RDRAND;
} }
}
}
if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) == 0)
RDRANDFlag = 0;
#if defined(a86)
if (RDRANDFlag) if (RDRANDFlag)
{ {
uint32_t RDRANDValue = 0; uint32_t RDRANDValue = 0;
@ -82,15 +89,21 @@ namespace Random
: "=r"(RDRANDValue)); : "=r"(RDRANDValue));
return RDRANDValue; return RDRANDValue;
} }
#endif
Seed = Seed * 1103515245 + 12345; Seed = Seed * 1103515245 + 12345;
return (uint32_t)(Seed / 65536) % __UINT16_MAX__; return (uint32_t)(Seed / 65536) % __UINT32_MAX__;
#endif
return 0;
} }
uint64_t rand64() uint64_t rand64()
{ {
int RDRANDFlag = 0; #if defined(a86)
static int RDRANDFlag = 0x1A1A;
if (unlikely(RDRANDFlag == 0x1A1A))
{
if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) != 0)
{
if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0) if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0)
{ {
CPU::x86::AMD::CPUID0x00000001 cpuid; CPU::x86::AMD::CPUID0x00000001 cpuid;
@ -103,11 +116,9 @@ namespace Random
cpuid.Get(); cpuid.Get();
RDRANDFlag = cpuid.ECX.RDRAND; RDRANDFlag = cpuid.ECX.RDRAND;
} }
}
}
if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) == 0)
RDRANDFlag = 0;
#if defined(a86)
if (RDRANDFlag) if (RDRANDFlag)
{ {
uint64_t RDRANDValue = 0; uint64_t RDRANDValue = 0;
@ -115,10 +126,11 @@ namespace Random
: "=r"(RDRANDValue)); : "=r"(RDRANDValue));
return RDRANDValue; return RDRANDValue;
} }
#endif
Seed = Seed * 1103515245 + 12345; Seed = Seed * 1103515245 + 12345;
return (uint64_t)(Seed / 65536) % __UINT16_MAX__; return (uint64_t)(Seed / 65536) % __UINT64_MAX__;
#endif
return 0;
} }
void ChangeSeed(uint64_t CustomSeed) { Seed = CustomSeed; } void ChangeSeed(uint64_t CustomSeed) { Seed = CustomSeed; }