### Read I/O Port (C++) Source: https://context7.com/germanaizek/winring0/llms.txt Reads a value from a hardware I/O port address. Use Ex variants for explicit error checking. Examples show reading keyboard controller status, PC speaker port, and PCI configuration address. ```cpp // C++ — Read keyboard controller status (port 0x64) BYTE kbdStatus = ReadIoPortByte(0x64); printf("Keyboard controller status: 0x%02X\n", kbdStatus); // Read with error checking (Ex variants) BYTE val8; WORD val16; DWORD val32; if (ReadIoPortByteEx(0x61, &val8)) printf("Port 0x61 (PC speaker): 0x%02X\n", val8); if (ReadIoPortWordEx(0x0CF8, &val16)) // PCI config address (low word) printf("Port 0x0CF8 word: 0x%04X\n", val16); if (ReadIoPortDwordEx(0x0CF8, &val32)) // PCI config address register printf("Port 0x0CF8 dword: 0x%08X\n", val32); ``` -------------------------------- ### Query CPU Features with CPUID (C++) Source: https://context7.com/germanaizek/winring0/llms.txt Executes the CPUID instruction and returns the four output registers (EAX, EBX, ECX, EDX). Variants Tx/Px pin execution to specific thread/process affinity masks. This example enumerates all standard and extended CPUID leaves. ```cpp // C++ — Enumerate all standard and extended CPUID leaves DWORD eax, ebx, ecx, edx; DWORD maxLeaf, maxLeafEx; // Get max standard leaf (CPUID index 0) CpuidPx(0x00000000, &maxLeaf, &ebx, &ecx, &edx, 1); printf("Max standard CPUID leaf: 0x%08X\n", maxLeaf); // Dump all standard leaves for (DWORD idx = 0; idx <= maxLeaf; idx++) { CpuidPx(idx, &eax, &ebx, &ecx, &edx, 1); printf("%08X: EAX=%08X EBX=%08X ECX=%08X EDX=%08X\n", idx, eax, ebx, ecx, edx); } // Get max extended leaf CpuidPx(0x80000000, &maxLeafEx, &ebx, &ecx, &edx, 1); for (DWORD idx = 0x80000000; idx <= maxLeafEx; idx++) { CpuidPx(idx, &eax, &ebx, &ecx, &edx, 1); printf("%08X: EAX=%08X EBX=%08X ECX=%08X EDX=%08X\n", idx, eax, ebx, ecx, edx); } // Example output: // 00000000: EAX=00000016 EBX=756E6547 ECX=6C65746E EDX=49656E69 // 80000000: EAX=80000008 EBX=00000000 ECX=00000000 EDX=00000000 ``` -------------------------------- ### Read Performance Monitoring Counter (C++) Source: https://context7.com/germanaizek/winring0/llms.txt Reads a hardware Performance Monitoring Counter (PMC). Counters must be programmed via MSR writes before reading. Example shows reading PMC counter 0 on thread affinity 1. ```cpp // C++ — Read PMC counter 0 on thread affinity 1 DWORD eax = 0, edx = 0; if (RdpmcTx(0, &eax, &edx, 1)) { UINT64 pmc = ((UINT64)edx << 32) | eax; printf("PMC[0]: %llu\n", pmc); } else { printf("Rdpmc failed (counter may not be programmed)\n"); } ``` -------------------------------- ### Read MSR on Current Thread (C++) Source: https://context7.com/germanaizek/winring0/llms.txt Reads a 64-bit MSR register and returns the result in two 32-bit halves (eax, edx). Example shows reading CPU temperature and Time Stamp Counter. ```cpp // C++ — Read Intel IA32_THERM_STATUS (MSR 0x19C) to get CPU temperature DWORD eax = 0, edx = 0; // Pin to logical processor 0 before reading ULONG_PTR old = SetThreadAffinityMask(GetCurrentThread(), 1); if (Rdmsr(0x0000019C, &eax, &edx)) { // Bits 22:16 = Digital Readout (temp delta from TjMax, typically 100°C) int tempDelta = (eax & 0x007F0000) >> 16; int cpuTemp = 100 - tempDelta; printf("CPU Temperature: %d°C\n", cpuTemp); } else { printf("Rdmsr failed\n"); } SetThreadAffinityMask(GetCurrentThread(), old); // Read MSR 0x10 (IA32_TIME_STAMP_COUNTER) on thread affinity mask = 1 if (RdmsrTx(0x00000010, &eax, &edx, 1)) printf("TSC MSR: %08X %08X\n", edx, eax); // Read same MSR with process affinity mask if (RdmsrPx(0x00000010, &eax, &edx, 1)) printf("TSC MSR (Px): %08X %08X\n", edx, eax); ``` -------------------------------- ### Write MSR (C++) Source: https://context7.com/germanaizek/winring0/llms.txt Writes a 64-bit value to an MSR register. Requires administrator/kernel privileges. Used to configure CPU features. Examples show writing to IA32_PERF_CTL via thread and process affinity. ```cpp // C++ — Write to IA32_PERF_CTL (MSR 0x199) to request a P-state DWORD eax = 0x00000800; // P-state request value (platform-specific) DWORD edx = 0x00000000; // Write on thread affinity mask 1 (logical CPU 0) if (WrmsrTx(0x00000199, eax, edx, 1)) printf("MSR 0x199 written successfully\n"); else printf("Wrmsr failed\n"); // Write using process affinity mask if (WrmsrPx(0x00000199, eax, edx, 1)) printf("MSR written via process affinity\n"); ``` -------------------------------- ### Read Time Stamp Counter (C++/C#) Source: https://context7.com/germanaizek/winring0/llms.txt Reads the 64-bit CPU Time Stamp Counter (TSC). Variants Tx/Px execute on a pinned CPU for reliable per-core measurements. Examples provided for C++ and C#. ```cpp // C++ — Read TSC on logical processor 0 DWORD eax = 0, edx = 0; if (RdtscPx(&eax, &edx, 1)) { UINT64 tsc = ((UINT64)edx << 32) | eax; printf("TSC: %llu\n", tsc); } ``` ```csharp // C# equivalent: // uint eax = 0, edx = 0; // if (ols.RdtscPx(ref eax, ref edx, (UIntPtr)1) != 0) // Console.WriteLine($"TSC: {((ulong)edx << 32) | eax}"); ``` -------------------------------- ### Initialize WinRing0 Library (C++) Source: https://github.com/germanaizek/winring0/blob/master/README.md Use Load-Time Dynamic Linking by including OlsApi.h, calling InitializeOls(), checking status with GetDllStatus(), using library functions, and finally calling DeinitializeOls(). ```cpp #include "OlsApi.h" // ... InitializeOls(); // Check error if (GetDllStatus() != 0) { // Handle error } // Call library functions // ... DeinitializeOls(); ``` -------------------------------- ### Initialize WinRing0 Library (C++) - Run-Time Source: https://github.com/germanaizek/winring0/blob/master/README.md Use Run-Time Dynamic Linking by including OlsApiInit.h, calling InitOpenLibSys(), checking status with GetDllStatus(), using library functions, and finally calling DeinitOpenLibSys(). For other source files, include OlsApiInitExt.h. ```cpp #include "OlsApiInit.h" // ... InitOpenLibSys(); // Check error if (GetDllStatus() != 0) { // Handle error } // Call library functions // ... DeinitOpenLibSys(); ``` -------------------------------- ### Initialize WinRing0 Library (C#) Source: https://github.com/germanaizek/winring0/blob/master/README.md Include OpenLibSys.cs in your project, add 'using OpenLibSys;', call GetStatus() and GetDllStatus() to check for errors, then use the library functions. Supported platforms are x86, x64, and Any CPU (not IA64). ```csharp using OpenLibSys; // ... // Check error if (GetStatus() != 0 || GetDllStatus() != 0) { // Handle error } // Call library functions // ... ``` -------------------------------- ### Initialize WinRing0 via C# Wrapper (.NET) Source: https://context7.com/germanaizek/winring0/llms.txt Initializes the WinRing0 library using the `Ols` class from `OpenLibSys.cs`. Requires administrator privileges. Check `GetStatus()` for wrapper errors and `GetDllStatus()` for driver/DLL errors. Always dispose of the `Ols` instance when done. ```csharp using OpenLibSys; // Requires administrator privileges Ols ols = new Ols(); // Check wrapper status if (ols.GetStatus() != (uint)Ols.Status.NO_ERROR) { Console.WriteLine("Ols init error: " + ols.GetStatus()); return; } // Check driver/DLL status if (ols.GetDllStatus() != (uint)Ols.OlsDllStatus.OLS_DLL_NO_ERROR) { Console.WriteLine("DLL status error: " + ols.GetDllStatus()); return; } // Query versions byte major = 0, minor = 0, revision = 0, release = 0; olds.GetDllVersion(ref major, ref minor, ref revision, ref release); Console.WriteLine($"DLL Version: {major}.{minor}.{revision}.{release}"); olds.GetDriverVersion(ref major, ref minor, ref revision, ref release); Console.WriteLine($"Driver Version: {major}.{minor}.{revision}.{release}"); // Always dispose when done olds.Dispose(); // or use: using (Ols ols = new Ols()) { ... } ``` -------------------------------- ### Initialize WinRing0 DLL and Driver (C++) Source: https://context7.com/germanaizek/winring0/llms.txt Initializes the WinRing0 DLL and loads the kernel driver. This must be called before any hardware access functions. `DeinitializeOls` should be called to unload the driver and release resources. ```cpp #include "OlsApi.h" #include "OlsDef.h" int main() { // Initialize: loads driver, returns TRUE on success if (!InitializeOls()) { DWORD status = GetDllStatus(); switch (status) { case OLS_DLL_DRIVER_NOT_LOADED: printf("Error: Driver not loaded\n"); break; case OLS_DLL_DRIVER_NOT_FOUND: printf("Error: Driver file not found\n"); break; case OLS_DLL_UNSUPPORTED_PLATFORM: printf("Error: Unsupported platform\n"); break; default: printf("Error: DLL status = %lu\n", status); break; } return 1; } // Query version info BYTE major, minor, revision, release; GetDllVersion(&major, &minor, &revision, &release); printf("DLL Version: %d.%d.%d.%d\n", major, minor, revision, release); GetDriverVersion(&major, &minor, &revision, &release); printf("Driver Version: %d.%d.%d.%d\n", major, minor, revision, release); // Check driver type DWORD driverType = GetDriverType(); if (driverType == OLS_DRIVER_TYPE_WIN_NT_X64) printf("Driver type: NT x64\n"); // --- use library here --- DeinitializeOls(); // always clean up return 0; } // Expected output: // DLL Version: 1.3.x.x // Driver Version: 1.3.x.x // Driver type: NT x64 ``` -------------------------------- ### InitOpenLibSys / DeinitOpenLibSys (C++) Source: https://context7.com/germanaizek/winring0/llms.txt Dynamically loads WinRing0 DLL at runtime for C++ applications, resolves function pointers, and initializes the driver. Use for graceful runtime error handling. ```APIDOC ## InitOpenLibSys / DeinitOpenLibSys — Run-Time Dynamic Linking (C++) Dynamically loads `WinRing0.dll` or `WinRing0x64.dll` at runtime (based on build target), resolves all function pointers, and calls `InitializeOls`. Use when you want to handle a missing DLL gracefully at runtime rather than at load time. ```cpp #include "OlsApiInit.h" // defines InitOpenLibSys, DeinitOpenLibSys, and all function pointers HMODULE hModule = NULL; int main() { if (!InitOpenLibSys(&hModule)) { printf("Failed to load WinRing0 DLL or initialize driver\n"); return 1; } // All exported function pointers (Rdmsr, Cpuid, ReadIoPortByte, etc.) // are now usable as global function pointers — same API as load-time linking. BYTE major, minor, revision, release; GetDllVersion(&major, &minor, &revision, &release); printf("DLL Version: %d.%d.%d.%d\n", major, minor, revision, release); // --- use library here --- DeinitOpenLibSys(&hModule); // frees library and driver return 0; } ``` ``` -------------------------------- ### Ols Class Initialization (C#) Source: https://context7.com/germanaizek/winring0/llms.txt Initializes the WinRing0 library in C# using the `Ols` class wrapper via P/Invoke. Requires administrator privileges and checks for wrapper and driver/DLL status. ```APIDOC ## Ols Class — C# (.NET) Initialization The `Ols` class in `OpenLibSys.cs` wraps the DLL via P/Invoke. Instantiate it, check `GetStatus()` for wrapper-level errors and `GetDllStatus()` for driver-level errors, then use its delegate members to call hardware functions. ```csharp using OpenLibSys; // Requires administrator privileges Ols ols = new Ols(); // Check wrapper status if (ols.GetStatus() != (uint)Ols.Status.NO_ERROR) { Console.WriteLine("Ols init error: " + ols.GetStatus()); return; } // Check driver/DLL status if (ols.GetDllStatus() != (uint)Ols.OlsDllStatus.OLS_DLL_NO_ERROR) { Console.WriteLine("DLL status error: " + ols.GetDllStatus()); return; } // Query versions byte major = 0, minor = 0, revision = 0, release = 0; olds.GetDllVersion(ref major, ref minor, ref revision, ref release); Console.WriteLine($"DLL Version: {major}.{minor}.{revision}.{release}"); olds.GetDriverVersion(ref major, ref minor, ref revision, ref release); Console.WriteLine($"Driver Version: {major}.{minor}.{revision}.{release}"); // Always dispose when done olds.Dispose(); // or use: using (Ols ols = new Ols()) { ... } ``` ``` -------------------------------- ### Dynamically Load WinRing0 DLL and Initialize Driver (C++) Source: https://context7.com/germanaizek/winring0/llms.txt Dynamically loads `WinRing0.dll` or `WinRing0x64.dll` at runtime and resolves function pointers. Use this when you need to handle a missing DLL gracefully at runtime. `DeinitOpenLibSys` frees the library and driver. ```cpp #include "OlsApiInit.h" // defines InitOpenLibSys, DeinitOpenLibSys, and all function pointers HMODULE hModule = NULL; int main() { if (!InitOpenLibSys(&hModule)) { printf("Failed to load WinRing0 DLL or initialize driver\n"); return 1; } // All exported function pointers (Rdmsr, Cpuid, ReadIoPortByte, etc.) // are now usable as global function pointers — same API as load-time linking. BYTE major, minor, revision, release; GetDllVersion(&major, &minor, &revision, &release); printf("DLL Version: %d.%d.%d.%d\n", major, minor, revision, release); // --- use library here --- DeinitOpenLibSys(&hModule); // frees library and driver return 0; } ``` -------------------------------- ### InitializeOls / DeinitializeOls (C++) Source: https://context7.com/germanaizek/winring0/llms.txt Initializes the WinRing0 DLL and loads the kernel driver for C++ applications using load-time linking. `DeinitializeOls` unloads the driver and releases resources. ```APIDOC ## InitializeOls / DeinitializeOls — Load-Time Linking (C++) Initialize the WinRing0 DLL and load the kernel driver. Must be called before any hardware access function. `DeinitializeOls` unloads the driver and releases all resources. ```cpp #include "OlsApi.h" #include "OlsDef.h" int main() { // Initialize: loads driver, returns TRUE on success if (!InitializeOls()) { DWORD status = GetDllStatus(); switch (status) { case OLS_DLL_DRIVER_NOT_LOADED: printf("Error: Driver not loaded\n"); break; case OLS_DLL_DRIVER_NOT_FOUND: printf("Error: Driver file not found\n"); break; case OLS_DLL_UNSUPPORTED_PLATFORM: printf("Error: Unsupported platform\n"); break; default: printf("Error: DLL status = %lu\n", status); break; } return 1; } // Query version info BYTE major, minor, revision, release; GetDllVersion(&major, &minor, &revision, &release); printf("DLL Version: %d.%d.%d.%d\n", major, minor, revision, release); GetDriverVersion(&major, &minor, &revision, &release); printf("Driver Version: %d.%d.%d.%d\n", major, minor, revision, release); // Check driver type DWORD driverType = GetDriverType(); if (driverType == OLS_DRIVER_TYPE_WIN_NT_X64) printf("Driver type: NT x64\n"); // --- use library here --- DeinitializeOls(); // always clean up return 0; } // Expected output: // DLL Version: 1.3.x.x // Driver Version: 1.3.x.x // Driver type: NT x64 ``` ``` -------------------------------- ### Find PCI Host Bridge and Dump Configuration Space Source: https://context7.com/germanaizek/winring0/llms.txt Searches for a PCI Host Bridge and dumps its 256-byte configuration space. Ensure the WinRing0 library is initialized before use. ```cpp // C++ — Find PCI Host Bridge (Base=0x06, Sub=0x00, ProgIf=0x00) // and dump its full 256-byte configuration space DWORD addr = FindPciDeviceByClass(0x06, 0x00, 0x00, 0); if (addr != 0xFFFFFFFF) { printf("Host Bridge at Bus=%d Dev=%d Func=%d\n", PciGetBus(addr), PciGetDev(addr), PciGetFunc(addr)); printf(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n"); printf("---------------------------------------------------\n"); for (int i = 0; i < 256; i += 16) { printf("%02X|", i); for (int j = 0; j < 16; j++) printf(" %02X", ReadPciConfigByte(addr, (BYTE)(i + j))); printf("\n"); } } ``` -------------------------------- ### Write to I/O Port Byte, Word, or Dword Source: https://context7.com/germanaizek/winring0/llms.txt Writes a value to a hardware I/O port address. Use the `Ex` variants for a success/failure return code. ```cpp // C++ — Generate a PC speaker beep at 440 Hz using I/O ports DWORD freq = 1193180000 / 440000; // divisor for 440 Hz // Configure PIT channel 2 (counter mode) WriteIoPortByte(0x43, 0xB6); // Load frequency divisor (low byte, then high byte) WriteIoPortByte(0x42, (BYTE)(freq & 0xFF)); WriteIoPortByte(0x42, (BYTE)(freq >> 8)); // Enable PC speaker (bits 0 and 1 of port 0x61) WriteIoPortByte(0x61, (ReadIoPortByte(0x61) | 0x03)); Sleep(1000); // beep for 1 second // Disable PC speaker WriteIoPortByte(0x61, (ReadIoPortByte(0x61) & 0xFC)); ``` ```csharp // C# equivalent: // uint freq = 1193180000 / 440000; // ols.WriteIoPortByte(0x43, 0xB6); // ols.WriteIoPortByte(0x42, (byte)(freq & 0xFF)); // ols.WriteIoPortByte(0x42, (byte)(freq >> 8)); // ols.WriteIoPortByte(0x61, (byte)(ols.ReadIoPortByte(0x61) | 0x03)); // Thread.Sleep(1000); // ols.WriteIoPortByte(0x61, (byte)(ols.ReadIoPortByte(0x61) & 0xFC)); ``` -------------------------------- ### WriteIoPortByte / WriteIoPortWord / WriteIoPortDword Source: https://context7.com/germanaizek/winring0/llms.txt Writes a value to the specified hardware I/O port address. The `Ex` variants return a `BOOL` success/failure code. ```APIDOC ## WriteIoPortByte / WriteIoPortWord / WriteIoPortDword ### Description Writes a value to the specified hardware I/O port address. The `Ex` variants return a `BOOL` success/failure code. ### Method Not applicable (function calls) ### Endpoint Not applicable (function calls) ### Parameters Not explicitly defined in the provided text for a generic signature, but usage implies: - `port` (DWORD): The I/O port address. - `value` (BYTE/WORD/DWORD): The value to write. ### Request Example ```cpp // C++ — Generate a PC speaker beep at 440 Hz using I/O ports DWORD freq = 1193180000 / 440000; // divisor for 440 Hz // Configure PIT channel 2 (counter mode) WriteIoPortByte(0x43, 0xB6); // Load frequency divisor (low byte, then high byte) WriteIoPortByte(0x42, (BYTE)(freq & 0xFF)); WriteIoPortByte(0x42, (BYTE)(freq >> 8)); // Enable PC speaker (bits 0 and 1 of port 0x61) WriteIoPortByte(0x61, (ReadIoPortByte(0x61) | 0x03)); Sleep(1000); // beep for 1 second // Disable PC speaker WriteIoPortByte(0x61, (ReadIoPortByte(0x61) & 0xFC)); ``` ### Response Not explicitly defined for non-Ex variants, but Ex variants return a BOOL indicating success or failure. ``` -------------------------------- ### Guard Hardware Access with CPU Capability Checks Source: https://context7.com/germanaizek/winring0/llms.txt Checks for CPU support of the CPUID instruction, Model-Specific Registers (MSR), and Time Stamp Counter (TSC) before using corresponding functions. This prevents undefined behavior on older hardware. Ensure the WinRing0 library is initialized. ```cpp // C++ — Guard hardware access with capability checks if (!IsCpuid()) { printf("CPUID not supported\n"); return 1; } if (!IsMsr()) { printf("MSR not supported\n"); return 1; } if (!IsTsc()) { printf("TSC not supported\n"); return 1; } // Now safe to call Cpuid, Rdmsr/Wrmsr, Rdtsc DWORD eax, ebx, ecx, edx; Cpuid(0x00000001, &eax, &ebx, &ecx, &edx); printf("Processor Info EAX: 0x%08X\n", eax); // Family/Model/Stepping ``` -------------------------------- ### Find PCI Device by Vendor and Device ID Source: https://context7.com/germanaizek/winring0/llms.txt Searches the PCI bus for a device matching the specified Vendor ID and Device ID. Returns the encoded `pciAddress` or `0xFFFFFFFF` if not found. The `index` parameter selects among multiple matching devices. ```cpp // C++ — Find Intel HD Audio controller (Vendor=0x8086, Device=0x293E) DWORD addr = FindPciDeviceById(0x8086, 0x293E, 0); if (addr != 0xFFFFFFFF) { printf("Found at Bus=%d Dev=%d Func=%d\n", PciGetBus(addr), PciGetDev(addr), PciGetFunc(addr)); WORD status = ReadPciConfigWord(addr, 0x06); // Status register printf("Status register: 0x%04X\n", status); } else { printf("Device not found\n"); } ``` -------------------------------- ### Write to PCI Configuration Register Source: https://context7.com/germanaizek/winring0/llms.txt Writes 1, 2, or 4 bytes to a PCI device's configuration space. `Ex` variants support extended register addresses and return a success/failure `BOOL`. ```cpp // C++ — Enable PCI Bus Mastering (Command register, bit 2) DWORD pciAddr = PciBusDevFunc(0, 1, 0); // Bus 0, Dev 1, Func 0 WORD cmd = ReadPciConfigWord(pciAddr, 0x04); // Command register cmd |= 0x0004; // Set Bus Master Enable bit WritePciConfigWord(pciAddr, 0x04, cmd); printf("Bus mastering enabled on 00:01.0\n"); // Ex variant (extended register address range, with error return) if (!WritePciConfigDwordEx(pciAddr, 0x04, (DWORD)cmd)) printf("WritePciConfigDwordEx failed\n"); ``` -------------------------------- ### WritePciConfigByte / Word / Dword Source: https://context7.com/germanaizek/winring0/llms.txt Writes 1, 2, or 4 bytes to a PCI device's configuration space. `Ex` variants accept extended register addresses and return a success/failure `BOOL`. ```APIDOC ## WritePciConfigByte / Word / Dword — Write PCI Configuration Register ### Description Writes 1, 2, or 4 bytes to a PCI device's configuration space. `Ex` variants accept extended register addresses and return a success/failure `BOOL`. ### Method Not applicable (function calls) ### Endpoint Not applicable (function calls) ### Parameters - **pciAddress** (DWORD): The encoded PCI address of the device. - **regOffset** (DWORD): The register offset within the configuration space. - **value** (BYTE/WORD/DWORD): The value to write. ### Request Example ```cpp // C++ — Enable PCI Bus Mastering (Command register, bit 2) DWORD pciAddr = PciBusDevFunc(0, 1, 0); // Bus 0, Dev 1, Func 0 WORD cmd = ReadPciConfigWord(pciAddr, 0x04); // Command register cmd |= 0x0004; // Set Bus Master Enable bit WritePciConfigWord(pciAddr, 0x04, cmd); printf("Bus mastering enabled on 00:01.0\n"); // Ex variant (extended register address range, with error return) if (!WritePciConfigDwordEx(pciAddr, 0x04, (DWORD)cmd)) printf("WritePciConfigDwordEx failed\n"); ``` ### Response - **Ex variants**: Return a `BOOL` indicating success or failure. ``` -------------------------------- ### Cpuid / CpuidTx / CpuidPx — Query CPU Features Source: https://context7.com/germanaizek/winring0/llms.txt Executes the `CPUID` instruction and returns the four output registers (`EAX`, `EBX`, `ECX`, `EDX`). The `Tx` variant pins execution to a specific thread affinity mask; `Px` uses a process affinity mask. ```APIDOC ## CPUID — Query CPU Features Executes the `CPUID` instruction and returns the four output registers (`EAX`, `EBX`, `ECX`, `EDX`). The `Tx` variant pins execution to a specific thread affinity mask; `Px` uses a process affinity mask. ### Function Signature (Conceptual C++) `void Cpuid(DWORD index, DWORD* eax, DWORD* ebx, DWORD* ecx, DWORD* edx);` `bool CpuidTx(DWORD index, DWORD* eax, DWORD* ebx, DWORD* ecx, DWORD* edx, ULONG_PTR affinityMask);` `bool CpuidPx(DWORD index, DWORD* eax, DWORD* ebx, DWORD* ecx, DWORD* edx, ULONG_PTR affinityMask);` ### Parameters - **index** (DWORD) - The CPUID leaf index to query. - **eax**, **ebx**, **ecx**, **edx** (DWORD*) - Pointers to DWORDs to store the output registers. - **affinityMask** (ULONG_PTR) - The thread or process affinity mask for `Tx`/`Px` variants. ### Example Usage (C++) ```cpp // C++ — Enumerate all standard and extended CPUID leaves DWORD eax, ebx, ecx, edx; DWORD maxLeaf, maxLeafEx; // Get max standard leaf (CPUID index 0) CpuidPx(0x00000000, &maxLeaf, &ebx, &ecx, &edx, 1); printf("Max standard CPUID leaf: 0x%08X\n", maxLeaf); // Dump all standard leaves for (DWORD idx = 0; idx <= maxLeaf; idx++) { CpuidPx(idx, &eax, &ebx, &ecx, &edx, 1); printf("%08X: EAX=%08X EBX=%08X ECX=%08X EDX=%08X\n", idx, eax, ebx, ecx, edx); } // Get max extended leaf CpuidPx(0x80000000, &maxLeafEx, &ebx, &ecx, &edx, 1); for (DWORD idx = 0x80000000; idx <= maxLeafEx; idx++) { CpuidPx(idx, &eax, &ebx, &ecx, &edx, 1); printf("%08X: EAX=%08X EBX=%08X ECX=%08X EDX=%08X\n", idx, eax, ebx, ecx, edx); } // Example output: // 00000000: EAX=00000016 EBX=756E6547 ECX=6C65746E EDX=49656E69 // 80000000: EAX=80000008 EBX=00000000 ECX=00000000 EDX=00000000 ``` ``` -------------------------------- ### ReadPciConfigByte / Word / Dword Source: https://context7.com/germanaizek/winring0/llms.txt Reads 1, 2, or 4 bytes from a PCI device's configuration space at the given register offset. Returns the value directly. ```APIDOC ## ReadPciConfigByte / Word / Dword — Read PCI Configuration Register ### Description Reads 1, 2, or 4 bytes from a PCI device's configuration space at the given register offset. Returns the value directly. Standard variant takes an 8-bit register address (0–255); `Ex` variants accept a wider `DWORD` register address and return a `BOOL` error flag. ### Method Not applicable (function calls) ### Endpoint Not applicable (function calls) ### Parameters - **pciAddress** (DWORD): The encoded PCI address of the device. - **regOffset** (DWORD): The register offset within the configuration space. ### Request Example ```cpp // C++ — Read Vendor ID and Device ID from PCI device at Bus 0, Dev 0, Func 0 DWORD pciAddr = PciBusDevFunc(0, 0, 0); WORD vendorId = ReadPciConfigWord(pciAddr, 0x00); // Vendor ID WORD deviceId = ReadPciConfigWord(pciAddr, 0x02); // Device ID BYTE revId = ReadPciConfigByte(pciAddr, 0x08); // Revision ID DWORD classCode = ReadPciConfigDword(pciAddr, 0x08); // Class+Rev printf("Vendor: 0x%04X Device: 0x%04X Rev: 0x%02X\n", vendorId, deviceId, revId); // Ex variant with error checking DWORD value; if (ReadPciConfigDwordEx(pciAddr, 0x00, &value)) printf("VendorID+DeviceID: 0x%08X\n", value); else printf("ReadPciConfigDwordEx failed\n"); ``` ### Response - **value** (BYTE/WORD/DWORD): The value read from the PCI configuration register. - **Ex variants**: Return a `BOOL` indicating success or failure. ``` -------------------------------- ### Read PCI Configuration Register Source: https://context7.com/germanaizek/winring0/llms.txt Reads 1, 2, or 4 bytes from a PCI device's configuration space at a given register offset. Standard variants use an 8-bit offset, while `Ex` variants accept a `DWORD` offset and return a `BOOL` error flag. ```cpp // C++ — Read Vendor ID and Device ID from PCI device at Bus 0, Dev 0, Func 0 DWORD pciAddr = PciBusDevFunc(0, 0, 0); WORD vendorId = ReadPciConfigWord(pciAddr, 0x00); // Vendor ID WORD deviceId = ReadPciConfigWord(pciAddr, 0x02); // Device ID BYTE revId = ReadPciConfigByte(pciAddr, 0x08); // Revision ID DWORD classCode = ReadPciConfigDword(pciAddr, 0x08); // Class+Rev printf("Vendor: 0x%04X Device: 0x%04X Rev: 0x%02X\n", vendorId, deviceId, revId); // Ex variant with error checking DWORD value; if (ReadPciConfigDwordEx(pciAddr, 0x00, &value)) printf("VendorID+DeviceID: 0x%08X\n", value); else printf("ReadPciConfigDwordEx failed\n"); ``` -------------------------------- ### Wrmsr — Write MSR Source: https://context7.com/germanaizek/winring0/llms.txt Writes a 64-bit value to an MSR register. Requires administrator/kernel privileges. Used to configure CPU features such as power limits, frequency scaling hints, or performance counters. ```APIDOC ## Wrmsr — Write MSR Writes a 64-bit value to an MSR register. **Requires administrator/kernel privileges.** Used to configure CPU features such as power limits, frequency scaling hints, or performance counters. ### Function Signature (Conceptual C++) `bool WrmsrTx(DWORD index, DWORD eax, DWORD edx, ULONG_PTR affinityMask);` `bool WrmsrPx(DWORD index, DWORD eax, DWORD edx, ULONG_PTR affinityMask);` ### Parameters - **index** (DWORD) - The MSR register index to write to. - **eax** (DWORD) - The lower 32 bits of the 64-bit value to write. - **edx** (DWORD) - The upper 32 bits of the 64-bit value to write. - **affinityMask** (ULONG_PTR) - The thread or process affinity mask. ### Example Usage (C++) ```cpp // C++ — Write to IA32_PERF_CTL (MSR 0x199) to request a P-state DWORD eax = 0x00000800; // P-state request value (platform-specific) DWORD edx = 0x00000000; // Write on thread affinity mask 1 (logical CPU 0) if (WrmsrTx(0x00000199, eax, edx, 1)) printf("MSR 0x199 written successfully\n"); else printf("Wrmsr failed\n"); // Write using process affinity mask if (WrmsrPx(0x00000199, eax, edx, 1)) printf("MSR written via process affinity\n"); ``` ``` -------------------------------- ### ReadIoPortByte / ReadIoPortWord / ReadIoPortDword — Read I/O Port Source: https://context7.com/germanaizek/winring0/llms.txt Reads a value from the specified hardware I/O port address. Returns the value directly (no error code). Use the `Ex` variants for explicit error checking. ```APIDOC ## Read I/O Port Access Reads a value from the specified hardware I/O port address. Returns the value directly (no error code). Use the `Ex` variants for explicit error checking. ### Function Signatures (Conceptual C++) `BYTE ReadIoPortByte(WORD port);` `WORD ReadIoPortWord(WORD port);` `DWORD ReadIoPortDword(WORD port);` `bool ReadIoPortByteEx(WORD port, BYTE* value);` `bool ReadIoPortWordEx(WORD port, WORD* value);` `bool ReadIoPortDwordEx(WORD port, DWORD* value);` ### Parameters - **port** (WORD) - The hardware I/O port address. - **value** (BYTE*, WORD*, DWORD*) - Pointer to store the read value for `Ex` variants. ### Example Usage (C++) ```cpp // C++ — Read keyboard controller status (port 0x64) BYTE kbdStatus = ReadIoPortByte(0x64); printf("Keyboard controller status: 0x%02X\n", kbdStatus); // Read with error checking (Ex variants) BYTE val8; WORD val16; DWORD val32; if (ReadIoPortByteEx(0x61, &val8)) printf("Port 0x61 (PC speaker): 0x%02X\n", val8); if (ReadIoPortWordEx(0x0CF8, &val16)) // PCI config address (low word) printf("Port 0x0CF8 word: 0x%04X\n", val16); if (ReadIoPortDwordEx(0x0CF8, &val32)) // PCI config address register printf("Port 0x0CF8 dword: 0x%08X\n", val32); ``` ``` -------------------------------- ### PciBusDevFunc Macro Source: https://context7.com/germanaizek/winring0/llms.txt Encodes a PCI device's bus number, device number, and function number into the 32-bit `pciAddress` format used by all PCI API functions. ```APIDOC ## PCI Address Encoding — PciBusDevFunc Macro ### Description Encodes a PCI device's bus number, device number, and function number into the 32-bit `pciAddress` format used by all PCI API functions. Complementary macros `PciGetBus`, `PciGetDev`, and `PciGetFunc` decode an address back to its components. ### Method Not applicable (macro/function) ### Endpoint Not applicable (macro/function) ### Parameters - **Bus** (DWORD): The PCI bus number. - **Dev** (DWORD): The PCI device number. - **Func** (DWORD): The PCI function number. ### Request Example ```cpp // C++ macros (defined in OlsDef.h) // PciBusDevFunc(Bus, Dev, Func) => ((Bus & 0xFF) << 8) | ((Dev & 0x1F) << 3) | (Func & 7) DWORD addr = PciBusDevFunc(0, 0, 0); // Bus 0, Device 0, Function 0 (host bridge) printf("Encoded address: 0x%08X\n", addr); // 0x00000000 printf("Bus=%d Dev=%d Func=%d\n", PciGetBus(addr), PciGetDev(addr), PciGetFunc(addr)); // Bus=0 Dev=0 Func=0 ``` ### Response - **pciAddress** (DWORD): The encoded 32-bit PCI address. ``` -------------------------------- ### Encode PCI Bus, Device, and Function Source: https://context7.com/germanaizek/winring0/llms.txt Encodes PCI bus, device, and function numbers into a 32-bit address format. Use `PciGetBus`, `PciGetDev`, and `PciGetFunc` to decode. ```cpp // C++ macros (defined in OlsDef.h) // PciBusDevFunc(Bus, Dev, Func) => ((Bus & 0xFF) << 8) | ((Dev & 0x1F) << 3) | (Func & 7) DWORD addr = PciBusDevFunc(0, 0, 0); // Bus 0, Device 0, Function 0 (host bridge) printf("Encoded address: 0x%08X\n", addr); // 0x00000000 printf("Bus=%d Dev=%d Func=%d\n", PciGetBus(addr), PciGetDev(addr), PciGetFunc(addr)); // Bus=0 Dev=0 Func=0 ``` ```csharp // C# equivalent (instance methods on Ols): // uint addr = ols.PciBusDevFunc(0, 0, 0); // Console.WriteLine($"Bus={ols.PciGetBus(addr)} Dev={ols.PciGetDev(addr)} Func={ols.PciGetFunc(addr)}"); ``` -------------------------------- ### FindPciDeviceByClass Source: https://context7.com/germanaizek/winring0/llms.txt Searches the PCI bus for a device matching the given Base Class, Sub Class, and Programming Interface codes. Returns the encoded `pciAddress` or `0xFFFFFFFF`. Useful for finding standard-class devices without knowing the vendor. ```APIDOC ## FindPciDeviceByClass ### Description Searches the PCI bus for a device matching the given Base Class, Sub Class, and Programming Interface codes. Returns the encoded `pciAddress` or `0xFFFFFFFF`. ### Parameters * **Base Class** (DWORD) - Description of the Base Class code. * **Sub Class** (DWORD) - Description of the Sub Class code. * **Programming Interface** (DWORD) - Description of the Programming Interface code. * **Index** (DWORD) - Description of the index parameter. ### Return Value * `pciAddress` (DWORD) - The encoded PCI address if a device is found. * `0xFFFFFFFF` (DWORD) - If no matching device is found. ### Example ```cpp // C++ — Find PCI Host Bridge (Base=0x06, Sub=0x00, ProgIf=0x00) DWORD addr = FindPciDeviceByClass(0x06, 0x00, 0x00, 0); if (addr != 0xFFFFFFFF) { printf("Host Bridge at Bus=%d Dev=%d Func=%d\n", PciGetBus(addr), PciGetDev(addr), PciGetFunc(addr)); } ``` ```csharp // C# equivalent: uint addr = ols.FindPciDeviceByClass(0x06, 0x00, 0x00, 0); if (addr != 0xFFFFFFFF) { /* ... */ } ``` ``` -------------------------------- ### IsCpuid / IsMsr / IsTsc Source: https://context7.com/germanaizek/winring0/llms.txt Returns `TRUE` if the CPU supports the CPUID instruction, Model-Specific Registers, or the Time Stamp Counter respectively. Call these before using the corresponding read/write functions to avoid undefined behavior on older hardware. ```APIDOC ## Capability Detection ### IsCpuid #### Description Returns `TRUE` if the CPU supports the CPUID instruction. #### Return Value * `TRUE` if CPUID is supported. * `FALSE` otherwise. ### IsMsr #### Description Returns `TRUE` if the CPU supports Model-Specific Registers (MSRs). #### Return Value * `TRUE` if MSRs are supported. * `FALSE` otherwise. ### IsTsc #### Description Returns `TRUE` if the CPU supports the Time Stamp Counter (TSC). #### Return Value * `TRUE` if TSC is supported. * `FALSE` otherwise. ### Example ```cpp // C++ — Guard hardware access with capability checks if (!IsCpuid()) { printf("CPUID not supported\n"); return 1; } if (!IsMsr()) { printf("MSR not supported\n"); return 1; } if (!IsTsc()) { printf("TSC not supported\n"); return 1; } // Now safe to call Cpuid, Rdmsr/Wrmsr, Rdtsc DWORD eax, ebx, ecx, edx; Cpuid(0x00000001, &eax, &ebx, &ecx, &edx); printf("Processor Info EAX: 0x%08X\n", eax); // Family/Model/Stepping ``` ```