phenom 9x00 tlb fix disable
目的:由于购买到B2步进的AMD 9600 BE,导致在vista sp1,windows 7,windows xp SP3底下内存性能低下,归根到底是由于系统补丁将TLB BUG 修正原因,其实B2发生TLB BUG的几率非常低,但是性能影响却非常明显,特别是在内存读取写入速度方面降低了10-20%。此文目的就是讲解如何将修复TLB关闭,以获取最高性能。
当CPU MSR为:
0XC0010015 = 00000000 01000019 0XC0011023 = 00000000 00200022 则说明系统已将TLB BUG修复
目标将CPU MSR修改为:
0XC0010015 = 00000000 01000011 0XC0011023 = 00000000 00A00020 即恢复最大性能
修复程序语言为:C#;
依赖API:winring0.dll ,winring0.sys,winring0.vxd,winring0x64.dll,winring0x64.sys 来自openlibsys.org,网站提供多种语言的详细示例,在这并不做特别介绍,喜欢的可以自己看看。
![]()
C#调用API官方示例
//-----------------------------------------------------------------------------
// Author : hiyohiyo
// Mail : hiyohiyo@crystalmark.info
// Web : http://openlibsys.org/
// License : The modified BSD license
//
// Copyright 2007-2009 OpenLibSys.org. All rights reserved.
//-----------------------------------------------------------------------------
// This is support library for WinRing0 1.3.x.
using System;
using System.Runtime.InteropServices;
namespace OpenLibSys
{
public class Ols : IDisposable
{
const string dllNameX64 = "WinRing0x64.dll";
const string dllName = "WinRing0.dll";
// for this support library
public enum Status
{
NO_ERROR = 0,
DLL_NOT_FOUND = 1,
DLL_INCORRECT_VERSION = 2,
DLL_INITIALIZE_ERROR = 3,
}
// for WinRing0
public enum OlsDllStatus
{
OLS_DLL_NO_ERROR = 0,
OLS_DLL_UNSUPPORTED_PLATFORM = 1,
OLS_DLL_DRIVER_NOT_LOADED = 2,
OLS_DLL_DRIVER_NOT_FOUND = 3,
OLS_DLL_DRIVER_UNLOADED = 4,
OLS_DLL_DRIVER_NOT_LOADED_ON_NETWORK = 5,
OLS_DLL_UNKNOWN_ERROR = 9
}
// for WinRing0
public enum OlsDriverType
{
OLS_DRIVER_TYPE_UNKNOWN = 0,
OLS_DRIVER_TYPE_WIN_9X = 1,
OLS_DRIVER_TYPE_WIN_NT = 2,
OLS_DRIVER_TYPE_WIN_NT4 = 3, // Obsolete
OLS_DRIVER_TYPE_WIN_NT_X64 = 4,
OLS_DRIVER_TYPE_WIN_NT_IA64 = 5
}
// for WinRing0
public enum OlsErrorPci : uint
{
OLS_ERROR_PCI_BUS_NOT_EXIST = 0xE0000001,
OLS_ERROR_PCI_NO_DEVICE = 0xE0000002,
OLS_ERROR_PCI_WRITE_CONFIG = 0xE0000003,
OLS_ERROR_PCI_READ_CONFIG = 0xE0000004
}
// Bus Number, Device Number and Function Number to PCI Device Address
public uint PciBusDevFunc(uint bus, uint dev, uint func)
{
return ((bus&0xFF)<<8) | ((dev&0x1F)<<3) | (func&7);
}
// PCI Device Address to Bus Number
public uint PciGetBus(uint address)
{
return ((address>>8) & 0xFF);
}
// PCI Device Address to Device Number
public uint PciGetDev(uint address)
{
return ((address>>3) & 0x1F);
}
// PCI Device Address to Function Number
public uint PciGetFunc(uint address)
{
return (address&7);
}
[DllImport("kernel32")]
public extern static IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32", SetLastError = true)]
private static extern bool FreeLibrary(IntPtr hModule);
[DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = false)]
private static extern IntPtr GetProcAddress(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);
private IntPtr module = IntPtr.Zero;
private uint status = (uint)Status.NO_ERROR;
public Ols()
{
string fileName;
if (System.IntPtr.Size == 8)
{
fileName = dllNameX64;
}
else
{
fileName = dllName;
}
module = Ols.LoadLibrary(fileName);
if (module == IntPtr.Zero)
{
status = (uint)Status.DLL_NOT_FOUND;
}
else
{
GetDllStatus = (_GetDllStatus)GetDelegate("GetDllStatus", typeof(_GetDllStatus));
GetDllVersion = (_GetDllVersion)GetDelegate("GetDllVersion", typeof(_GetDllVersion));
GetDriverVersion = (_GetDriverVersion)GetDelegate("GetDriverVersion", typeof(_GetDriverVersion));
GetDriverType = (_GetDriverType)GetDelegate("GetDriverType", typeof(_GetDriverType));
InitializeOls = (_InitializeOls)GetDelegate("InitializeOls", typeof(_InitializeOls));
DeinitializeOls = (_DeinitializeOls)GetDelegate("DeinitializeOls", typeof(_DeinitializeOls));
IsCpuid = (_IsCpuid)GetDelegate("IsCpuid", typeof(_IsCpuid));
IsMsr = (_IsMsr)GetDelegate("IsMsr", typeof(_IsMsr));
IsTsc = (_IsTsc)GetDelegate("IsTsc", typeof(_IsTsc));
Hlt = (_Hlt)GetDelegate("Hlt", typeof(_Hlt));
HltTx = (_HltTx)GetDelegate("HltTx", typeof(_HltTx));
HltPx = (_HltPx)GetDelegate("HltPx", typeof(_HltPx));
Rdmsr = (_Rdmsr)GetDelegate("Rdmsr", typeof(_Rdmsr));
RdmsrTx = (_RdmsrTx)GetDelegate("RdmsrTx", typeof(_RdmsrTx));
RdmsrPx = (_RdmsrPx)GetDelegate("RdmsrPx", typeof(_RdmsrPx));
Wrmsr = (_Wrmsr)GetDelegate("Wrmsr", typeof(_Wrmsr));
WrmsrTx = (_WrmsrTx)GetDelegate("WrmsrTx", typeof(_WrmsrTx));
WrmsrPx = (_WrmsrPx)GetDelegate("WrmsrPx", typeof(_WrmsrPx));
Rdpmc = (_Rdpmc)GetDelegate("Rdpmc", typeof(_Rdpmc));
RdpmcTx = (_RdpmcTx)GetDelegate("RdpmcTx", typeof(_RdpmcTx));
RdpmcPx = (_RdpmcPx)GetDelegate("RdpmcPx", typeof(_RdpmcPx));
Cpuid = (_Cpuid)GetDelegate("Cpuid", typeof(_Cpuid));
CpuidTx = (_CpuidTx)GetDelegate("CpuidTx", typeof(_CpuidTx));
CpuidPx = (_CpuidPx)GetDelegate("CpuidPx", typeof(_CpuidPx));
Rdtsc = (_Rdtsc)GetDelegate("Rdtsc", typeof(_Rdtsc));
RdtscTx = (_RdtscTx)GetDelegate("RdtscTx", typeof(_RdtscTx));
RdtscPx = (_RdtscPx)GetDelegate("RdtscPx", typeof(_RdtscPx));
ReadIoPortByte = (_ReadIoPortByte)GetDelegate("ReadIoPortByte", typeof(_ReadIoPortByte));
ReadIoPortWord = (_ReadIoPortWord)GetDelegate("ReadIoPortWord", typeof(_ReadIoPortWord));
ReadIoPortDword = (_ReadIoPortDword)GetDelegate("ReadIoPortDword", typeof(_ReadIoPortDword));
ReadIoPortByteEx = (_ReadIoPortByteEx)GetDelegate("ReadIoPortByteEx", typeof(_ReadIoPortByteEx));
ReadIoPortWordEx = (_ReadIoPortWordEx)GetDelegate("ReadIoPortWordEx", typeof(_ReadIoPortWordEx));
ReadIoPortDwordEx = (_ReadIoPortDwordEx)GetDelegate("ReadIoPortDwordEx", typeof(_ReadIoPortDwordEx));
WriteIoPortByte = (_WriteIoPortByte)GetDelegate("WriteIoPortByte", typeof(_WriteIoPortByte));
WriteIoPortWord = (_WriteIoPortWord)GetDelegate("WriteIoPortWord", typeof(_WriteIoPortWord));
WriteIoPortDword = (_WriteIoPortDword)GetDelegate("WriteIoPortDword", typeof(_WriteIoPortDword));
WriteIoPortByteEx = (_WriteIoPortByteEx)GetDelegate("WriteIoPortByteEx", typeof(_WriteIoPortByteEx));
WriteIoPortWordEx = (_WriteIoPortWordEx)GetDelegate("WriteIoPortWordEx", typeof(_WriteIoPortWordEx));
WriteIoPortDwordEx = (_WriteIoPortDwordEx)GetDelegate("WriteIoPortDwordEx", typeof(_WriteIoPortDwordEx));
SetPciMaxBusIndex = (_SetPciMaxBusIndex)GetDelegate("SetPciMaxBusIndex", typeof(_SetPciMaxBusIndex));
ReadPciConfigByte = (_ReadPciConfigByte)GetDelegate("ReadPciConfigByte", typeof(_ReadPciConfigByte));
ReadPciConfigWord = (_ReadPciConfigWord)GetDelegate("ReadPciConfigWord", typeof(_ReadPciConfigWord));
ReadPciConfigDword = (_ReadPciConfigDword)GetDelegate("ReadPciConfigDword", typeof(_ReadPciConfigDword));
ReadPciConfigByteEx = (_ReadPciConfigByteEx)GetDelegate("ReadPciConfigByteEx", typeof(_ReadPciConfigByteEx));
ReadPciConfigWordEx = (_ReadPciConfigWordEx)GetDelegate("ReadPciConfigWordEx", typeof(_ReadPciConfigWordEx));
ReadPciConfigDwordEx = (_ReadPciConfigDwordEx)GetDelegate("ReadPciConfigDwordEx", typeof(_ReadPciConfigDwordEx));
WritePciConfigByte = (_WritePciConfigByte)GetDelegate("WritePciConfigByte", typeof(_WritePciConfigByte));
WritePciConfigWord = (_WritePciConfigWord)GetDelegate("WritePciConfigWord", typeof(_WritePciConfigWord));
WritePciConfigDword = (_WritePciConfigDword)GetDelegate("WritePciConfigDword", typeof(_WritePciConfigDword));
WritePciConfigByteEx = (_WritePciConfigByteEx)GetDelegate("WritePciConfigByteEx", typeof(_WritePciConfigByteEx));
WritePciConfigWordEx = (_WritePciConfigWordEx)GetDelegate("WritePciConfigWordEx", typeof(_WritePciConfigWordEx));
WritePciConfigDwordEx = (_WritePciConfigDwordEx)GetDelegate("WritePciConfigDwordEx", typeof(_WritePciConfigDwordEx));
FindPciDeviceById = (_FindPciDeviceById)GetDelegate("FindPciDeviceById", typeof(_FindPciDeviceById));
FindPciDeviceByClass = (_FindPciDeviceByClass)GetDelegate("FindPciDeviceByClass", typeof(_FindPciDeviceByClass));
#if _PHYSICAL_MEMORY_SUPPORT
ReadDmiMemory = (_ReadDmiMemory)GetDelegate("ReadDmiMemory", typeof(_ReadDmiMemory));
ReadPhysicalMemory = (_ReadPhysicalMemory)GetDelegate("ReadPhysicalMemory", typeof(_ReadPhysicalMemory));
WritePhysicalMemory = (_WritePhysicalMemory)GetDelegate("WritePhysicalMemory", typeof(_WritePhysicalMemory));
#endif
if (! (
GetDllStatus != null
&& GetDllVersion != null
&& GetDriverVersion != null
&& GetDriverType != null
&& InitializeOls != null
&& DeinitializeOls != null
&& IsCpuid != null
&& IsMsr != null
&& IsTsc != null
&& Hlt != null
&& HltTx != null
&& HltPx != null
&& Rdmsr != null
&& RdmsrTx != null
&& RdmsrPx != null
&& Wrmsr != null
&& WrmsrTx != null
&& WrmsrPx != null
&& Rdpmc != null
&& RdpmcTx != null
&& RdpmcPx != null
&& Cpuid != null
&& CpuidTx != null
&& CpuidPx != null
&& Rdtsc != null
&& RdtscTx != null
&& RdtscPx != null
&& ReadIoPortByte != null
&& ReadIoPortWord != null
&& ReadIoPortDword != null
&& ReadIoPortByteEx != null
&& ReadIoPortWordEx != null
&& ReadIoPortDwordEx != null
&& WriteIoPortByte != null
&& WriteIoPortWord != null
&& WriteIoPortDword != null
&& WriteIoPortByteEx != null
&& WriteIoPortWordEx != null
&& WriteIoPortDwordEx != null
&& SetPciMaxBusIndex != null
&& ReadPciConfigByte != null
&& ReadPciConfigWord != null
&& ReadPciConfigDword != null
&& ReadPciConfigByteEx != null
&& ReadPciConfigWordEx != null
&& ReadPciConfigDwordEx != null
&& WritePciConfigByte != null
&& WritePciConfigWord != null
&& WritePciConfigDword != null
&& WritePciConfigByteEx != null
&& WritePciConfigWordEx != null
&& WritePciConfigDwordEx != null
&& FindPciDeviceById != null
&& FindPciDeviceByClass != null
#if _PHYSICAL_MEMORY_SUPPORT
&& ReadDmiMemory != null
&& ReadPhysicalMemory != null
&& WritePhysicalMemory != null
#endif
))
{
status = (uint)Status.DLL_INCORRECT_VERSION;
}
if (InitializeOls() == 0)
{
status = (uint)Status.DLL_INITIALIZE_ERROR;
}
}
}
public uint GetStatus()
{
return status;
}
public void Dispose()
{
if (module != IntPtr.Zero)
{
DeinitializeOls();
Ols.FreeLibrary(module);
module = IntPtr.Zero;
}
}
public Delegate GetDelegate(string procName, Type delegateType)
{
IntPtr ptr = GetProcAddress(module, procName);
if (ptr != IntPtr.Zero)
{
Delegate d = Marshal.GetDelegateForFunctionPointer(ptr, delegateType);
return d;
}
int result = Marshal.GetHRForLastWin32Error();
throw Marshal.GetExceptionForHR(result);
}
//-----------------------------------------------------------------------------
// DLL Information
//-----------------------------------------------------------------------------
public delegate uint _GetDllStatus();
public delegate uint _GetDllVersion(ref byte major, ref byte minor, ref byte revision, ref byte release);
public delegate uint _GetDriverVersion(ref byte major, ref byte minor, ref byte revision, ref byte release);
public delegate uint _GetDriverType();
public delegate int _InitializeOls();
public delegate void _DeinitializeOls();
public _GetDllStatus GetDllStatus = null;
public _GetDriverType GetDriverType = null;
public _GetDllVersion GetDllVersion = null;
public _GetDriverVersion GetDriverVersion = null;
public _InitializeOls InitializeOls = null;
public _DeinitializeOls DeinitializeOls = null;
//-----------------------------------------------------------------------------
// CPU
//-----------------------------------------------------------------------------
public delegate int _IsCpuid();
public delegate int _IsMsr();
public delegate int _IsTsc();
public delegate int _Hlt();
public delegate int _HltTx(UIntPtr threadAffinityMask);
public delegate int _HltPx(UIntPtr processAffinityMask);
public delegate int _Rdmsr(uint index, ref uint eax, ref uint edx);
public delegate int _RdmsrTx(uint index, ref uint eax, ref uint edx, UIntPtr threadAffinityMask);
public delegate int _RdmsrPx(uint index, ref uint eax, ref uint edx, UIntPtr processAffinityMask);
public delegate int _Wrmsr(uint index, uint eax, uint edx);
public delegate int _WrmsrTx(uint index, uint eax, uint edx, UIntPtr threadAffinityMask);
public delegate int _WrmsrPx(uint index, uint eax, uint edx, UIntPtr processAffinityMask);
public delegate int _Rdpmc(uint index, ref uint eax, ref uint edx);
public delegate int _RdpmcTx(uint index, ref uint eax, ref uint edx, UIntPtr threadAffinityMask);
public delegate int _RdpmcPx(uint index, ref uint eax, ref uint edx, UIntPtr processAffinityMask);
public delegate int _Cpuid(uint index, ref uint eax, ref uint ebx, ref uint ecx, ref uint edx);
public delegate int _CpuidTx(uint index, ref uint eax, ref uint ebx, ref uint ecx, ref uint edx, UIntPtr threadAffinityMask);
public delegate int _CpuidPx(uint index, ref uint eax, ref uint ebx, ref uint ecx, ref uint edx, UIntPtr processAffinityMask);
public delegate int _Rdtsc(ref uint eax, ref uint edx);
public delegate int _RdtscTx(ref uint eax, ref uint edx, UIntPtr threadAffinityMask);
public delegate int _RdtscPx(ref uint eax, ref uint edx, UIntPtr processAffinityMask);
public _IsCpuid IsCpuid = null;
public _IsMsr IsMsr = null;
public _IsTsc IsTsc = null;
public _Hlt Hlt = null;
public _HltTx HltTx = null;
public _HltPx HltPx = null;
public _Rdmsr Rdmsr = null;
public _RdmsrTx RdmsrTx = null;
public _RdmsrPx RdmsrPx = null;
public _Wrmsr Wrmsr = null;
public _WrmsrTx WrmsrTx = null;
public _WrmsrPx WrmsrPx = null;
public _Rdpmc Rdpmc = null;
public _RdpmcTx RdpmcTx = null;
public _RdpmcPx RdpmcPx = null;
public _Cpuid Cpuid = null;
public _CpuidTx CpuidTx = null;
public _CpuidPx CpuidPx = null;
public _Rdtsc Rdtsc = null;
public _RdtscTx RdtscTx = null;
public _RdtscPx RdtscPx = null;
//-----------------------------------------------------------------------------
// I/O
//-----------------------------------------------------------------------------
public delegate byte _ReadIoPortByte(ushort port);
public delegate ushort _ReadIoPortWord(ushort port);
public delegate uint _ReadIoPortDword(ushort port);
public _ReadIoPortByte ReadIoPortByte;
public _ReadIoPortWord ReadIoPortWord;
public _ReadIoPortDword ReadIoPortDword;
public delegate int _ReadIoPortByteEx(ushort port, ref byte value);
public delegate int _ReadIoPortWordEx(ushort port, ref ushort value);
public delegate int _ReadIoPortDwordEx(ushort port, ref uint value);
public _ReadIoPortByteEx ReadIoPortByteEx;
public _ReadIoPortWordEx ReadIoPortWordEx;
public _ReadIoPortDwordEx ReadIoPortDwordEx;
public delegate void _WriteIoPortByte(ushort port, byte value);
public delegate void _WriteIoPortWord(ushort port, ushort value);
public delegate void _WriteIoPortDword(ushort port, uint value);
public _WriteIoPortByte WriteIoPortByte;
public _WriteIoPortWord WriteIoPortWord;
public _WriteIoPortDword WriteIoPortDword;
public delegate int _WriteIoPortByteEx(ushort port, byte value);
public delegate int _WriteIoPortWordEx(ushort port, ushort value);
public delegate int _WriteIoPortDwordEx(ushort port, uint value);
public _WriteIoPortByteEx WriteIoPortByteEx;
public _WriteIoPortWordEx WriteIoPortWordEx;
public _WriteIoPortDwordEx WriteIoPortDwordEx;
//-----------------------------------------------------------------------------
// PCI
//-----------------------------------------------------------------------------
public delegate void _SetPciMaxBusIndex(byte max);
public _SetPciMaxBusIndex SetPciMaxBusIndex;
public delegate byte _ReadPciConfigByte(uint pciAddress, byte regAddress);
public delegate ushort _ReadPciConfigWord(uint pciAddress, byte regAddress);
public delegate uint _ReadPciConfigDword(uint pciAddress, byte regAddress);
public _ReadPciConfigByte ReadPciConfigByte;
public _ReadPciConfigWord ReadPciConfigWord;
public _ReadPciConfigDword ReadPciConfigDword;
public delegate int _ReadPciConfigByteEx(uint pciAddress, uint regAddress, ref byte value);
public delegate int _ReadPciConfigWordEx(uint pciAddress, uint regAddress, ref ushort value);
public delegate int _ReadPciConfigDwordEx(uint pciAddress, uint regAddress, ref uint value);
public _ReadPciConfigByteEx ReadPciConfigByteEx;
public _ReadPciConfigWordEx ReadPciConfigWordEx;
public _ReadPciConfigDwordEx ReadPciConfigDwordEx;
public delegate void _WritePciConfigByte(uint pciAddress, byte regAddress, byte value);
public delegate void _WritePciConfigWord(uint pciAddress, byte regAddress, ushort value);
public delegate void _WritePciConfigDword(uint pciAddress, byte regAddress, uint value);
public _WritePciConfigByte WritePciConfigByte;
public _WritePciConfigWord WritePciConfigWord;
public _WritePciConfigDword WritePciConfigDword;
public delegate int _WritePciConfigByteEx(uint pciAddress, uint regAddress, byte value);
public delegate int _WritePciConfigWordEx(uint pciAddress, uint regAddress, ushort value);
public delegate int _WritePciConfigDwordEx(uint pciAddress, uint regAddress, uint value);
public _WritePciConfigByteEx WritePciConfigByteEx;
public _WritePciConfigWordEx WritePciConfigWordEx;
public _WritePciConfigDwordEx WritePciConfigDwordEx;
public delegate uint _FindPciDeviceById(ushort vendorId, ushort deviceId, byte index);
public delegate uint _FindPciDeviceByClass(byte baseClass, byte subClass, byte programIf, byte index);
public _FindPciDeviceById FindPciDeviceById;
public _FindPciDeviceByClass FindPciDeviceByClass;
//-----------------------------------------------------------------------------
// Physical Memory (unsafe)
//-----------------------------------------------------------------------------
#if _PHYSICAL_MEMORY_SUPPORT
public unsafe delegate uint _ReadDmiMemory(byte* buffer, uint count, uint unitSize);
public _ReadDmiMemory ReadDmiMemory;
public unsafe delegate uint _ReadPhysicalMemory(UIntPtr address, byte* buffer, uint count, uint unitSize);
public unsafe delegate uint _WritePhysicalMemory(UIntPtr address, byte* buffer, uint count, uint unitSize);
public _ReadPhysicalMemory ReadPhysicalMemory;
public _WritePhysicalMemory WritePhysicalMemory;
#endif
}
}
以下为具体调用方法:
当CPU MSR为:
0XC0010015 = 00000000 01000019 0XC0011023 = 00000000 00200022 则说明系统已将TLB BUG修复
目标将CPU MSR修改为:
0XC0010015 = 00000000 01000011 0XC0011023 = 00000000 00A00020 即恢复最大性能
修复程序语言为:C#;
依赖API:winring0.dll ,winring0.sys,winring0.vxd,winring0x64.dll,winring0x64.sys 来自openlibsys.org,网站提供多种语言的详细示例,在这并不做特别介绍,喜欢的可以自己看看。
//-----------------------------------------------------------------------------
// Author : hiyohiyo
// Mail : hiyohiyo@crystalmark.info
// Web : http://openlibsys.org/
// License : The modified BSD license
//
// Copyright 2007-2009 OpenLibSys.org. All rights reserved.
//-----------------------------------------------------------------------------
// This is support library for WinRing0 1.3.x.
using System;
using System.Runtime.InteropServices;
namespace OpenLibSys
{
public class Ols : IDisposable
{
const string dllNameX64 = "WinRing0x64.dll";
const string dllName = "WinRing0.dll";
// for this support library
public enum Status
{
NO_ERROR = 0,
DLL_NOT_FOUND = 1,
DLL_INCORRECT_VERSION = 2,
DLL_INITIALIZE_ERROR = 3,
}
// for WinRing0
public enum OlsDllStatus
{
OLS_DLL_NO_ERROR = 0,
OLS_DLL_UNSUPPORTED_PLATFORM = 1,
OLS_DLL_DRIVER_NOT_LOADED = 2,
OLS_DLL_DRIVER_NOT_FOUND = 3,
OLS_DLL_DRIVER_UNLOADED = 4,
OLS_DLL_DRIVER_NOT_LOADED_ON_NETWORK = 5,
OLS_DLL_UNKNOWN_ERROR = 9
}
// for WinRing0
public enum OlsDriverType
{
OLS_DRIVER_TYPE_UNKNOWN = 0,
OLS_DRIVER_TYPE_WIN_9X = 1,
OLS_DRIVER_TYPE_WIN_NT = 2,
OLS_DRIVER_TYPE_WIN_NT4 = 3, // Obsolete
OLS_DRIVER_TYPE_WIN_NT_X64 = 4,
OLS_DRIVER_TYPE_WIN_NT_IA64 = 5
}
// for WinRing0
public enum OlsErrorPci : uint
{
OLS_ERROR_PCI_BUS_NOT_EXIST = 0xE0000001,
OLS_ERROR_PCI_NO_DEVICE = 0xE0000002,
OLS_ERROR_PCI_WRITE_CONFIG = 0xE0000003,
OLS_ERROR_PCI_READ_CONFIG = 0xE0000004
}
// Bus Number, Device Number and Function Number to PCI Device Address
public uint PciBusDevFunc(uint bus, uint dev, uint func)
{
return ((bus&0xFF)<<8) | ((dev&0x1F)<<3) | (func&7);
}
// PCI Device Address to Bus Number
public uint PciGetBus(uint address)
{
return ((address>>8) & 0xFF);
}
// PCI Device Address to Device Number
public uint PciGetDev(uint address)
{
return ((address>>3) & 0x1F);
}
// PCI Device Address to Function Number
public uint PciGetFunc(uint address)
{
return (address&7);
}
[DllImport("kernel32")]
public extern static IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32", SetLastError = true)]
private static extern bool FreeLibrary(IntPtr hModule);
[DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = false)]
private static extern IntPtr GetProcAddress(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);
private IntPtr module = IntPtr.Zero;
private uint status = (uint)Status.NO_ERROR;
public Ols()
{
string fileName;
if (System.IntPtr.Size == 8)
{
fileName = dllNameX64;
}
else
{
fileName = dllName;
}
module = Ols.LoadLibrary(fileName);
if (module == IntPtr.Zero)
{
status = (uint)Status.DLL_NOT_FOUND;
}
else
{
GetDllStatus = (_GetDllStatus)GetDelegate("GetDllStatus", typeof(_GetDllStatus));
GetDllVersion = (_GetDllVersion)GetDelegate("GetDllVersion", typeof(_GetDllVersion));
GetDriverVersion = (_GetDriverVersion)GetDelegate("GetDriverVersion", typeof(_GetDriverVersion));
GetDriverType = (_GetDriverType)GetDelegate("GetDriverType", typeof(_GetDriverType));
InitializeOls = (_InitializeOls)GetDelegate("InitializeOls", typeof(_InitializeOls));
DeinitializeOls = (_DeinitializeOls)GetDelegate("DeinitializeOls", typeof(_DeinitializeOls));
IsCpuid = (_IsCpuid)GetDelegate("IsCpuid", typeof(_IsCpuid));
IsMsr = (_IsMsr)GetDelegate("IsMsr", typeof(_IsMsr));
IsTsc = (_IsTsc)GetDelegate("IsTsc", typeof(_IsTsc));
Hlt = (_Hlt)GetDelegate("Hlt", typeof(_Hlt));
HltTx = (_HltTx)GetDelegate("HltTx", typeof(_HltTx));
HltPx = (_HltPx)GetDelegate("HltPx", typeof(_HltPx));
Rdmsr = (_Rdmsr)GetDelegate("Rdmsr", typeof(_Rdmsr));
RdmsrTx = (_RdmsrTx)GetDelegate("RdmsrTx", typeof(_RdmsrTx));
RdmsrPx = (_RdmsrPx)GetDelegate("RdmsrPx", typeof(_RdmsrPx));
Wrmsr = (_Wrmsr)GetDelegate("Wrmsr", typeof(_Wrmsr));
WrmsrTx = (_WrmsrTx)GetDelegate("WrmsrTx", typeof(_WrmsrTx));
WrmsrPx = (_WrmsrPx)GetDelegate("WrmsrPx", typeof(_WrmsrPx));
Rdpmc = (_Rdpmc)GetDelegate("Rdpmc", typeof(_Rdpmc));
RdpmcTx = (_RdpmcTx)GetDelegate("RdpmcTx", typeof(_RdpmcTx));
RdpmcPx = (_RdpmcPx)GetDelegate("RdpmcPx", typeof(_RdpmcPx));
Cpuid = (_Cpuid)GetDelegate("Cpuid", typeof(_Cpuid));
CpuidTx = (_CpuidTx)GetDelegate("CpuidTx", typeof(_CpuidTx));
CpuidPx = (_CpuidPx)GetDelegate("CpuidPx", typeof(_CpuidPx));
Rdtsc = (_Rdtsc)GetDelegate("Rdtsc", typeof(_Rdtsc));
RdtscTx = (_RdtscTx)GetDelegate("RdtscTx", typeof(_RdtscTx));
RdtscPx = (_RdtscPx)GetDelegate("RdtscPx", typeof(_RdtscPx));
ReadIoPortByte = (_ReadIoPortByte)GetDelegate("ReadIoPortByte", typeof(_ReadIoPortByte));
ReadIoPortWord = (_ReadIoPortWord)GetDelegate("ReadIoPortWord", typeof(_ReadIoPortWord));
ReadIoPortDword = (_ReadIoPortDword)GetDelegate("ReadIoPortDword", typeof(_ReadIoPortDword));
ReadIoPortByteEx = (_ReadIoPortByteEx)GetDelegate("ReadIoPortByteEx", typeof(_ReadIoPortByteEx));
ReadIoPortWordEx = (_ReadIoPortWordEx)GetDelegate("ReadIoPortWordEx", typeof(_ReadIoPortWordEx));
ReadIoPortDwordEx = (_ReadIoPortDwordEx)GetDelegate("ReadIoPortDwordEx", typeof(_ReadIoPortDwordEx));
WriteIoPortByte = (_WriteIoPortByte)GetDelegate("WriteIoPortByte", typeof(_WriteIoPortByte));
WriteIoPortWord = (_WriteIoPortWord)GetDelegate("WriteIoPortWord", typeof(_WriteIoPortWord));
WriteIoPortDword = (_WriteIoPortDword)GetDelegate("WriteIoPortDword", typeof(_WriteIoPortDword));
WriteIoPortByteEx = (_WriteIoPortByteEx)GetDelegate("WriteIoPortByteEx", typeof(_WriteIoPortByteEx));
WriteIoPortWordEx = (_WriteIoPortWordEx)GetDelegate("WriteIoPortWordEx", typeof(_WriteIoPortWordEx));
WriteIoPortDwordEx = (_WriteIoPortDwordEx)GetDelegate("WriteIoPortDwordEx", typeof(_WriteIoPortDwordEx));
SetPciMaxBusIndex = (_SetPciMaxBusIndex)GetDelegate("SetPciMaxBusIndex", typeof(_SetPciMaxBusIndex));
ReadPciConfigByte = (_ReadPciConfigByte)GetDelegate("ReadPciConfigByte", typeof(_ReadPciConfigByte));
ReadPciConfigWord = (_ReadPciConfigWord)GetDelegate("ReadPciConfigWord", typeof(_ReadPciConfigWord));
ReadPciConfigDword = (_ReadPciConfigDword)GetDelegate("ReadPciConfigDword", typeof(_ReadPciConfigDword));
ReadPciConfigByteEx = (_ReadPciConfigByteEx)GetDelegate("ReadPciConfigByteEx", typeof(_ReadPciConfigByteEx));
ReadPciConfigWordEx = (_ReadPciConfigWordEx)GetDelegate("ReadPciConfigWordEx", typeof(_ReadPciConfigWordEx));
ReadPciConfigDwordEx = (_ReadPciConfigDwordEx)GetDelegate("ReadPciConfigDwordEx", typeof(_ReadPciConfigDwordEx));
WritePciConfigByte = (_WritePciConfigByte)GetDelegate("WritePciConfigByte", typeof(_WritePciConfigByte));
WritePciConfigWord = (_WritePciConfigWord)GetDelegate("WritePciConfigWord", typeof(_WritePciConfigWord));
WritePciConfigDword = (_WritePciConfigDword)GetDelegate("WritePciConfigDword", typeof(_WritePciConfigDword));
WritePciConfigByteEx = (_WritePciConfigByteEx)GetDelegate("WritePciConfigByteEx", typeof(_WritePciConfigByteEx));
WritePciConfigWordEx = (_WritePciConfigWordEx)GetDelegate("WritePciConfigWordEx", typeof(_WritePciConfigWordEx));
WritePciConfigDwordEx = (_WritePciConfigDwordEx)GetDelegate("WritePciConfigDwordEx", typeof(_WritePciConfigDwordEx));
FindPciDeviceById = (_FindPciDeviceById)GetDelegate("FindPciDeviceById", typeof(_FindPciDeviceById));
FindPciDeviceByClass = (_FindPciDeviceByClass)GetDelegate("FindPciDeviceByClass", typeof(_FindPciDeviceByClass));
#if _PHYSICAL_MEMORY_SUPPORT
ReadDmiMemory = (_ReadDmiMemory)GetDelegate("ReadDmiMemory", typeof(_ReadDmiMemory));
ReadPhysicalMemory = (_ReadPhysicalMemory)GetDelegate("ReadPhysicalMemory", typeof(_ReadPhysicalMemory));
WritePhysicalMemory = (_WritePhysicalMemory)GetDelegate("WritePhysicalMemory", typeof(_WritePhysicalMemory));
#endif
if (! (
GetDllStatus != null
&& GetDllVersion != null
&& GetDriverVersion != null
&& GetDriverType != null
&& InitializeOls != null
&& DeinitializeOls != null
&& IsCpuid != null
&& IsMsr != null
&& IsTsc != null
&& Hlt != null
&& HltTx != null
&& HltPx != null
&& Rdmsr != null
&& RdmsrTx != null
&& RdmsrPx != null
&& Wrmsr != null
&& WrmsrTx != null
&& WrmsrPx != null
&& Rdpmc != null
&& RdpmcTx != null
&& RdpmcPx != null
&& Cpuid != null
&& CpuidTx != null
&& CpuidPx != null
&& Rdtsc != null
&& RdtscTx != null
&& RdtscPx != null
&& ReadIoPortByte != null
&& ReadIoPortWord != null
&& ReadIoPortDword != null
&& ReadIoPortByteEx != null
&& ReadIoPortWordEx != null
&& ReadIoPortDwordEx != null
&& WriteIoPortByte != null
&& WriteIoPortWord != null
&& WriteIoPortDword != null
&& WriteIoPortByteEx != null
&& WriteIoPortWordEx != null
&& WriteIoPortDwordEx != null
&& SetPciMaxBusIndex != null
&& ReadPciConfigByte != null
&& ReadPciConfigWord != null
&& ReadPciConfigDword != null
&& ReadPciConfigByteEx != null
&& ReadPciConfigWordEx != null
&& ReadPciConfigDwordEx != null
&& WritePciConfigByte != null
&& WritePciConfigWord != null
&& WritePciConfigDword != null
&& WritePciConfigByteEx != null
&& WritePciConfigWordEx != null
&& WritePciConfigDwordEx != null
&& FindPciDeviceById != null
&& FindPciDeviceByClass != null
#if _PHYSICAL_MEMORY_SUPPORT
&& ReadDmiMemory != null
&& ReadPhysicalMemory != null
&& WritePhysicalMemory != null
#endif
))
{
status = (uint)Status.DLL_INCORRECT_VERSION;
}
if (InitializeOls() == 0)
{
status = (uint)Status.DLL_INITIALIZE_ERROR;
}
}
}
public uint GetStatus()
{
return status;
}
public void Dispose()
{
if (module != IntPtr.Zero)
{
DeinitializeOls();
Ols.FreeLibrary(module);
module = IntPtr.Zero;
}
}
public Delegate GetDelegate(string procName, Type delegateType)
{
IntPtr ptr = GetProcAddress(module, procName);
if (ptr != IntPtr.Zero)
{
Delegate d = Marshal.GetDelegateForFunctionPointer(ptr, delegateType);
return d;
}
int result = Marshal.GetHRForLastWin32Error();
throw Marshal.GetExceptionForHR(result);
}
//-----------------------------------------------------------------------------
// DLL Information
//-----------------------------------------------------------------------------
public delegate uint _GetDllStatus();
public delegate uint _GetDllVersion(ref byte major, ref byte minor, ref byte revision, ref byte release);
public delegate uint _GetDriverVersion(ref byte major, ref byte minor, ref byte revision, ref byte release);
public delegate uint _GetDriverType();
public delegate int _InitializeOls();
public delegate void _DeinitializeOls();
public _GetDllStatus GetDllStatus = null;
public _GetDriverType GetDriverType = null;
public _GetDllVersion GetDllVersion = null;
public _GetDriverVersion GetDriverVersion = null;
public _InitializeOls InitializeOls = null;
public _DeinitializeOls DeinitializeOls = null;
//-----------------------------------------------------------------------------
// CPU
//-----------------------------------------------------------------------------
public delegate int _IsCpuid();
public delegate int _IsMsr();
public delegate int _IsTsc();
public delegate int _Hlt();
public delegate int _HltTx(UIntPtr threadAffinityMask);
public delegate int _HltPx(UIntPtr processAffinityMask);
public delegate int _Rdmsr(uint index, ref uint eax, ref uint edx);
public delegate int _RdmsrTx(uint index, ref uint eax, ref uint edx, UIntPtr threadAffinityMask);
public delegate int _RdmsrPx(uint index, ref uint eax, ref uint edx, UIntPtr processAffinityMask);
public delegate int _Wrmsr(uint index, uint eax, uint edx);
public delegate int _WrmsrTx(uint index, uint eax, uint edx, UIntPtr threadAffinityMask);
public delegate int _WrmsrPx(uint index, uint eax, uint edx, UIntPtr processAffinityMask);
public delegate int _Rdpmc(uint index, ref uint eax, ref uint edx);
public delegate int _RdpmcTx(uint index, ref uint eax, ref uint edx, UIntPtr threadAffinityMask);
public delegate int _RdpmcPx(uint index, ref uint eax, ref uint edx, UIntPtr processAffinityMask);
public delegate int _Cpuid(uint index, ref uint eax, ref uint ebx, ref uint ecx, ref uint edx);
public delegate int _CpuidTx(uint index, ref uint eax, ref uint ebx, ref uint ecx, ref uint edx, UIntPtr threadAffinityMask);
public delegate int _CpuidPx(uint index, ref uint eax, ref uint ebx, ref uint ecx, ref uint edx, UIntPtr processAffinityMask);
public delegate int _Rdtsc(ref uint eax, ref uint edx);
public delegate int _RdtscTx(ref uint eax, ref uint edx, UIntPtr threadAffinityMask);
public delegate int _RdtscPx(ref uint eax, ref uint edx, UIntPtr processAffinityMask);
public _IsCpuid IsCpuid = null;
public _IsMsr IsMsr = null;
public _IsTsc IsTsc = null;
public _Hlt Hlt = null;
public _HltTx HltTx = null;
public _HltPx HltPx = null;
public _Rdmsr Rdmsr = null;
public _RdmsrTx RdmsrTx = null;
public _RdmsrPx RdmsrPx = null;
public _Wrmsr Wrmsr = null;
public _WrmsrTx WrmsrTx = null;
public _WrmsrPx WrmsrPx = null;
public _Rdpmc Rdpmc = null;
public _RdpmcTx RdpmcTx = null;
public _RdpmcPx RdpmcPx = null;
public _Cpuid Cpuid = null;
public _CpuidTx CpuidTx = null;
public _CpuidPx CpuidPx = null;
public _Rdtsc Rdtsc = null;
public _RdtscTx RdtscTx = null;
public _RdtscPx RdtscPx = null;
//-----------------------------------------------------------------------------
// I/O
//-----------------------------------------------------------------------------
public delegate byte _ReadIoPortByte(ushort port);
public delegate ushort _ReadIoPortWord(ushort port);
public delegate uint _ReadIoPortDword(ushort port);
public _ReadIoPortByte ReadIoPortByte;
public _ReadIoPortWord ReadIoPortWord;
public _ReadIoPortDword ReadIoPortDword;
public delegate int _ReadIoPortByteEx(ushort port, ref byte value);
public delegate int _ReadIoPortWordEx(ushort port, ref ushort value);
public delegate int _ReadIoPortDwordEx(ushort port, ref uint value);
public _ReadIoPortByteEx ReadIoPortByteEx;
public _ReadIoPortWordEx ReadIoPortWordEx;
public _ReadIoPortDwordEx ReadIoPortDwordEx;
public delegate void _WriteIoPortByte(ushort port, byte value);
public delegate void _WriteIoPortWord(ushort port, ushort value);
public delegate void _WriteIoPortDword(ushort port, uint value);
public _WriteIoPortByte WriteIoPortByte;
public _WriteIoPortWord WriteIoPortWord;
public _WriteIoPortDword WriteIoPortDword;
public delegate int _WriteIoPortByteEx(ushort port, byte value);
public delegate int _WriteIoPortWordEx(ushort port, ushort value);
public delegate int _WriteIoPortDwordEx(ushort port, uint value);
public _WriteIoPortByteEx WriteIoPortByteEx;
public _WriteIoPortWordEx WriteIoPortWordEx;
public _WriteIoPortDwordEx WriteIoPortDwordEx;
//-----------------------------------------------------------------------------
// PCI
//-----------------------------------------------------------------------------
public delegate void _SetPciMaxBusIndex(byte max);
public _SetPciMaxBusIndex SetPciMaxBusIndex;
public delegate byte _ReadPciConfigByte(uint pciAddress, byte regAddress);
public delegate ushort _ReadPciConfigWord(uint pciAddress, byte regAddress);
public delegate uint _ReadPciConfigDword(uint pciAddress, byte regAddress);
public _ReadPciConfigByte ReadPciConfigByte;
public _ReadPciConfigWord ReadPciConfigWord;
public _ReadPciConfigDword ReadPciConfigDword;
public delegate int _ReadPciConfigByteEx(uint pciAddress, uint regAddress, ref byte value);
public delegate int _ReadPciConfigWordEx(uint pciAddress, uint regAddress, ref ushort value);
public delegate int _ReadPciConfigDwordEx(uint pciAddress, uint regAddress, ref uint value);
public _ReadPciConfigByteEx ReadPciConfigByteEx;
public _ReadPciConfigWordEx ReadPciConfigWordEx;
public _ReadPciConfigDwordEx ReadPciConfigDwordEx;
public delegate void _WritePciConfigByte(uint pciAddress, byte regAddress, byte value);
public delegate void _WritePciConfigWord(uint pciAddress, byte regAddress, ushort value);
public delegate void _WritePciConfigDword(uint pciAddress, byte regAddress, uint value);
public _WritePciConfigByte WritePciConfigByte;
public _WritePciConfigWord WritePciConfigWord;
public _WritePciConfigDword WritePciConfigDword;
public delegate int _WritePciConfigByteEx(uint pciAddress, uint regAddress, byte value);
public delegate int _WritePciConfigWordEx(uint pciAddress, uint regAddress, ushort value);
public delegate int _WritePciConfigDwordEx(uint pciAddress, uint regAddress, uint value);
public _WritePciConfigByteEx WritePciConfigByteEx;
public _WritePciConfigWordEx WritePciConfigWordEx;
public _WritePciConfigDwordEx WritePciConfigDwordEx;
public delegate uint _FindPciDeviceById(ushort vendorId, ushort deviceId, byte index);
public delegate uint _FindPciDeviceByClass(byte baseClass, byte subClass, byte programIf, byte index);
public _FindPciDeviceById FindPciDeviceById;
public _FindPciDeviceByClass FindPciDeviceByClass;
//-----------------------------------------------------------------------------
// Physical Memory (unsafe)
//-----------------------------------------------------------------------------
#if _PHYSICAL_MEMORY_SUPPORT
public unsafe delegate uint _ReadDmiMemory(byte* buffer, uint count, uint unitSize);
public _ReadDmiMemory ReadDmiMemory;
public unsafe delegate uint _ReadPhysicalMemory(UIntPtr address, byte* buffer, uint count, uint unitSize);
public unsafe delegate uint _WritePhysicalMemory(UIntPtr address, byte* buffer, uint count, uint unitSize);
public _ReadPhysicalMemory ReadPhysicalMemory;
public _WritePhysicalMemory WritePhysicalMemory;
#endif
}
}
private static void WriteMSR()
{
try
{
//
//-----------------------------------------------------------------------------
// Initialize
//-----------------------------------------------------------------------------
Ols ols = new Ols();
// Check support library sutatus
switch (ols.GetStatus())
{
case (uint)Ols.Status.NO_ERROR:
break;
case (uint)Ols.Status.DLL_NOT_FOUND:
throw new Exception("Status Error!! DLL_NOT_FOUND");
case (uint)Ols.Status.DLL_INCORRECT_VERSION:
throw new Exception("Status Error!! DLL_INCORRECT_VERSION");
case (uint)Ols.Status.DLL_INITIALIZE_ERROR:
throw new Exception("Status Error!! DLL_INITIALIZE_ERROR");
}
// Check WinRing0 status
switch (ols.GetDllStatus())
{
case (uint)Ols.OlsDllStatus.OLS_DLL_NO_ERROR:
break;
case (uint)Ols.OlsDllStatus.OLS_DLL_DRIVER_NOT_LOADED:
throw new Exception("DLL Status Error!! OLS_DRIVER_NOT_LOADED");
case (uint)Ols.OlsDllStatus.OLS_DLL_UNSUPPORTED_PLATFORM:
throw new Exception("DLL Status Error!! OLS_UNSUPPORTED_PLATFORM");
case (uint)Ols.OlsDllStatus.OLS_DLL_DRIVER_NOT_FOUND:
throw new Exception("DLL Status Error!! OLS_DLL_DRIVER_NOT_FOUND");
case (uint)Ols.OlsDllStatus.OLS_DLL_DRIVER_UNLOADED:
throw new Exception("DLL Status Error!! OLS_DLL_DRIVER_UNLOADED");
case (uint)Ols.OlsDllStatus.OLS_DLL_DRIVER_NOT_LOADED_ON_NETWORK:
throw new Exception("DLL Status Error!! DRIVER_NOT_LOADED_ON_NETWORK");
case (uint)Ols.OlsDllStatus.OLS_DLL_UNKNOWN_ERROR:
throw new Exception("DLL Status Error!! OLS_DLL_UNKNOWN_ERROR");
}
uint PHENOM_MSR_HWCR = 0xc0010015, PHENOM_MSR_BU_CFG = 0xc0011023;
uint lo = 0, hi = 0;
if (ols.IsMsr() > 0)
{
for (int i = 0; i < 4; i++)//由于为四核此处需要循环调用
{
UIntPtr j = (UIntPtr)Math.Pow(2, i);//设置Process Affinity Mask
ols.RdmsrPx(PHENOM_MSR_HWCR, ref lo, ref hi, j);
ols.WrmsrPx(PHENOM_MSR_HWCR, 0x01000010, hi, j);
ols.RdmsrPx(PHENOM_MSR_BU_CFG, ref lo, ref hi, j);
ols.WrmsrPx(PHENOM_MSR_BU_CFG, 0x00A00020, hi, j);
}
}
//ols.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Environment.Exit(0);
}
浙公网安备 33010602011771号