Skip to content

Instantly share code, notes, and snippets.

@HoShiMin
Created June 14, 2015 19:01
Show Gist options
  • Save HoShiMin/d8cb2a709d60172f8c74 to your computer and use it in GitHub Desktop.
Save HoShiMin/d8cb2a709d60172f8c74 to your computer and use it in GitHub Desktop.
Заголовочный файл для WinRing0
unit WinRing0;
interface
{$DEFINE _PHYSICAL_MEMORY_SUPPORT}
const
WinRing0DLL = {$IFDEF CPUX64}'WinRing0x64.dll'{$ELSE}'WinRing0.dll'{$ENDIF};
// GetDLLStatus Constants:
OLS_DLL_NO_ERROR = 0; // No error
OLS_DLL_UNSUPPORTED_PLATFORM = 1; // Unsupported Platform
OLS_DLL_DRIVER_NOT_LOADED = 2; // Driver not loaded
OLS_DLL_DRIVER_NOT_FOUND = 3; // Driver not found
OLS_DLL_DRIVER_UNLOADED = 4; // Driver unloaded by other process
OLS_DLL_DRIVER_NOT_LOADED_ON_NETWORK = 5; // Driver not loaded because of executing on Network Drive (1.0.8 or later)
OLS_DLL_UNKNOWN_ERROR = 9;
// GetDriverType Constants:
OLS_DRIVER_TYPE_UNKNOWN = 0; // Unknown Type = Error
OLS_DRIVER_TYPE_WIN_9X = 1; // Windows 9x
OLS_DRIVER_TYPE_WIN_NT = 2; // Windows NT4/2000/XP/Vista or later [x86]
OLS_DRIVER_TYPE_WIN_NT4 = 3; // Windows NT4 [x86] (Not supported Kernel mode SEH)
OLS_DRIVER_TYPE_WIN_NT_X64 = 4; // Windows XP/Vista or later [x64]
OLS_DRIVER_TYPE_WIN_NT_IA64 = 5; // Reserved for IA64
// DLL Functions:
function InitializeOls: LongBool; stdcall; external WinRing0DLL name 'InitializeOls';
function DeinitializeOls: LongBool; stdcall; external WinRing0DLL name 'DeinitializeOls';
function GetDLLStatus: LongWord; stdcall; external WinRing0DLL name 'GetDllStatus';
function GetDLLVersion(out Major, Minor, Revision, Release: byte): LongWord; stdcall; external WinRing0DLL name 'GetDllVersion';
function GetDriverVersion(out Major, Minor, Revision, Release: byte): LongWord; stdcall; external WinRing0DLL name 'GetDriverVersion';
function GetDriverType: LongWord; stdcall; external WinRing0DLL name 'GetDriverType';
// CPU Functions:
function IsCPUID: LongBool; stdcall; external WinRing0DLL name 'IsCpuid';
function IsMSR: LongBool; stdcall; external WinRing0DLL name 'IsMsr';
function IsTsc: LongBool; stdcall; external WinRing0DLL name 'IsTsc';
function Hlt: LongBool; stdcall; external WinRing0DLL name 'Hlt';
function RDMSR(Index: LongWord; out EAX, EDX: LongWord): LongBool; stdcall; external WinRing0DLL name 'Rdmsr';
function WRMSR(Index, EAX, EDX: LongWord): LongBool; stdcall; external WinRing0DLL name 'Wrmsr';
function RDPMC(Index: LongWord; out EAX, EDX: LongWord): LongBool; stdcall; external WinRing0DLL name 'Rdpmc';
function CPUID(Index: LongWord; out EAX, EBX, ECX, EDX: LongWord): LongBool; stdcall; external WinRing0DLL name 'Cpuid';
function RDTSC(out EAX, EDX: LongWord): LongBool; stdcall; external WinRing0DLL name 'Rdtsc';
function HltTx(ThreadAffinityMask: LongWord): LongBool; stdcall; external WinRing0DLL name 'HltTx';
function RDMSRTX(Index: LongWord; out EAX, EDX: LongWord; ThreadAffinityMask: LongWord): LongBool; stdcall; external WinRing0DLL name 'RdmsrTx';
function WRMSRTX(Index, EAX, EDX, ThreadAffinityMask: LongWord): LongBool; stdcall; external WinRing0DLL name 'WrmsrTx';
function RDPMCTX(Index: LongWord; out EAX, EDX: LongWord; ThreadAffinityMask: LongWord): LongBool; stdcall; external WinRing0DLL name 'RdpmcTx';
function CPUIDTX(Index: LongWord; out EAX, EBX, ECX, EDX: LongWord; ThreadAffinityMask: LongWord): LongBool; stdcall; external WinRing0DLL name 'CpuidTx';
function RDTSCTX(out EAX, EDX: LongWord; ThreadAffinityMask: LongWord): LongBool; stdcall; external WinRing0DLL name 'RdtscTx';
function HltPx(ProcessAffinityMask: LongWord): LongBool; stdcall; external WinRing0DLL name 'HltPx';
function RDMSRPX(Index: LongWord; out EAX, EDX: LongWord; ProcessAffinityMask: LongWord): LongBool; stdcall; external WinRing0DLL name 'RdmsrPx';
function WRMSRPX(Index, EAX, EDX, ProcessAffinityMask: LongWord): LongBool; stdcall; external WinRing0DLL name 'WrmsrPx';
function RDPMCPX(Index: LongWord; out EAX, EDX: LongWord; ProcessAffinityMask: LongWord): LongBool; stdcall; external WinRing0DLL name 'RdpmcPx';
function CPUIDPX(Index: LongWord; out EAX, EBX, ECX, EDX: LongWord; ProcessAffinityMask: LongWord): LongBool; stdcall; external WinRing0DLL name 'CpuidPx';
function RDTSCPX(out EAX, EDX: LongWord; ProcessAffinityMask: LongWord): LongBool; stdcall; external WinRing0DLL name 'RdtscPx';
// Input-Output Functions:
function ReadIOPortByte(Port: Word): Byte; stdcall; external WinRing0DLL name 'ReadIoPortByte';
function ReadIOPortWord(Port: Word): Word; stdcall; external WinRing0DLL name 'ReadIoPortWord';
function ReadIOPortDword(Port: Word): LongWord; stdcall; external WinRing0DLL name 'ReadIoPortDword';
function ReadIOPortByteEx(Port: Word; out Value: Byte): LongBool; stdcall; external WinRing0DLL name 'ReadIoPortByteEx';
function ReadIOPortWordEx(Port: Word; out Value: Word): LongBool; stdcall; external WinRing0DLL name 'ReadIoPortWordEx';
function ReadIOPortDwordEx(Port: Word; out Value: LongWord): LongBool; stdcall; external WinRing0DLL name 'ReadIoPortDwordEx';
procedure WriteIOPortByte(Port: Word; Value: Byte); stdcall; external WinRing0DLL name 'WriteIoPortByte';
procedure WriteIOPortWord(Port: Word; Value: Word); stdcall; external WinRing0DLL name 'WriteIoPortWord';
procedure WriteIOPortDword(Port: Word; Value: LongWord); stdcall; external WinRing0DLL name 'WriteIoPortDword';
function WriteIOPortByteEx(Port: Word; Value: Byte): LongBool; stdcall; external WinRing0DLL name 'WriteIoPortByteEx';
function WriteIOPortWordEx(Port: Word; Value: Word): LongBool; stdcall; external WinRing0DLL name 'WriteIoPortWordEx';
function WriteIOPortDwordEx(Port: Word; Value: LongWord): LongBool; stdcall; external WinRing0DLL name 'WriteIoPortDwordEx';
// PCI Functions:
{
PCIAddress: PCI Device Address
+-------+------------------+
| 0-2 | Function Number |
| 3-7 | Device Number |
| 8-15 | PCI Bus Number |
| 16-31 | Reserved |
+-------+------------------+
RegAddress: Configuration Register Address (0, 1 ... 0xFFFFFFFF);
}
procedure SetPCIMaxBusNo(Max: byte); stdcall; external WinRing0DLL name 'SetPciMaxBusNo';
function ReadPCIConfigByte(PCIAddress: LongWord; RegAddress: Byte): Byte; stdcall; external WinRing0DLL name 'ReadPciConfigByte';
function ReadPCIConfigWord(PCIAddress: LongWord; RegAddress: Byte): Word; stdcall; external WinRing0DLL name 'ReadPciConfigWord';
function ReadPCIConfigDword(PCIAddress: LongWord; RegAddress: Byte): LongWord; stdcall; external WinRing0DLL name 'ReadPciConfigDword';
function ReadPCIConfigByteEx(PCIAddress, RegAddress: LongWord; out Value: Byte): LongBool; stdcall; external WinRing0DLL name 'ReadPciConfigByteEx';
function ReadPCIConfigWordEx(PCIAddress, RegAddress: LongWord; out Value: Word): LongBool; stdcall; external WinRing0DLL name 'ReadPciConfigWordEx';
function ReadPCIConfigDwordEx(PCIAddress, RegAddress: LongWord; out Value: LongWord): LongBool; stdcall; external WinRing0DLL name 'ReadPciConfigDwordEx';
procedure WritePCIConfigByte(PCIAddress: LongWord; RegAddress: Byte; Value: Byte); stdcall; external WinRing0DLL name 'WritePciConfigByte';
procedure WritePCIConfigWord(PCIAddress: LongWord; RegAddress: Byte; Value: Word); stdcall; external WinRing0DLL name 'WritePciConfigWord';
procedure WritePCIConfigDword(PCIAddress: LongWord; RegAddress: Byte; Value: LongWord); stdcall; external WinRing0DLL name 'WritePciConfigDword';
function WritePCIConfigByteEx(PCIAddress, RegAddress: LongWord; Value: Byte): LongBool; stdcall; external WinRing0DLL name 'WritePciConfigByteEx';
function WritePCIConfigWordEx(PCIAddress, RegAddress: LongWord; Value: Word): LongBool; stdcall; external WinRing0DLL name 'WritePciConfigWordEx';
function WritePCIConfigDwordEx(PCIAddress, RegAddress: LongWord; Value: LongWord): LongBool; stdcall; external WinRing0DLL name 'WritePciConfigDwordEx';
// Returns 0xFFFFFFFF if device doesn't find:
function FindPCIDeviceById(VendorID, DeviceID: Word; Index: Byte): LongWord; stdcall; external WinRing0DLL name 'FindPciDeviceById';
function FindPCIDeviceByClass(BaseClass, SubClass, ProgramIf, Index: Byte): LongWord; stdcall; external WinRing0DLL name 'FindPciDeviceByClass';
// Support PCI Functions:
function PCIBusDeviceFunction(Bus, Device, Func: LongWord): LongWord;
function PCIGetBusNumber(DeviceAddress: LongWord): LongWord;
function PCIGetDeviceNumber(DeviceAddress: LongWord): LongWord;
function PCIGetFunctionNumber(DeviceAddress: LongWord): LongWord;
{$IFDEF _PHYSICAL_MEMORY_SUPPORT}
// Physical Memory & DMI Support (returns 0 if fails):
function ReadDMIMemory(Buffer: Pointer; Count, UnitSize: LongWord): LongWord; stdcall; external WinRing0DLL name 'ReadDmiMemory';
function ReadPhysicalMemory(Address: LongWord; Buffer: Pointer; Count, UnitSize: LongWord): LongWord; stdcall; external WinRing0DLL name 'ReadPhysicalMemory';
function WritePhysicalMemory(Address: LongWord; Buffer: Pointer; Count, UnitSize: LongWord): LongWord; stdcall; external WinRing0DLL name 'WritePhysicalMemory';
{$ENDIF}
implementation
// Bus Number, Device Number and Function Number to PCI Device Address:
function PCIBusDeviceFunction(Bus, Device, Func: LongWord): LongWord;
begin
Result := ((Bus and $FF) shl 8) or ((Device and $1F) shl 3) or (Func and 7);
end;
// PCI Device Address to Bus Number:
function PCIGetBusNumber(DeviceAddress: LongWord): LongWord;
begin
Result := (DeviceAddress shr 8) and $FF;
end;
// PCI Device Address to Device Number:
function PCIGetDeviceNumber(DeviceAddress: LongWord): LongWord;
begin
Result := (DeviceAddress shr 3) and $1F;
end;
// PCI Device Address to Function Number:
function PCIGetFunctionNumber(DeviceAddress: LongWord): LongWord;
begin
Result := DeviceAddress and 7;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment