CLOSE

Sometimes we do need to write the OS according to the features of the CPU. Like some CPU might have extra capabilities which help us in any task to do efficiently and precisely. While some other lacks this functionality, in that case we should not not use those capabilities that is un-available in the CPU.

The cpuid instruction in x86 architecture provides a reliable way to gather detailed information about the processor, allowing developers to tailor their OS to make the most efficient use of the available hardware. This is particularly important because some CPUs have extra capabilities that can significantly enhance performance and precision for certain tasks, while others may lack these functionalities. By using the cpuid instruction, you can ensure that your OS does not rely on features that are unavailable on certain CPUs, thus maintaining compatibility and efficiency.

Let's first understand the cpuid instruction.

Understanding the cpuid Instruction

The cpuid instruction retrieves processor-specific information such as the vendor ID, feature flags, cache details, and more. The information is retrieved by setting specific values in the EAX register and executing the cpuid instruction, with results returned in the EAX, EBX, ECX, and EDX registers.

Basic Syntax:

mov eax, <input_value>
cpuid

The value placed in EAX determines the type of information returned by the cpuid instruction.

Common cpuid Requests:

  1. Vendor ID and Feature Information (EAX=0):
    • Input: EAX = 0
    • Output:
      • EAX: Maximum input value for basic CPUID information.
      • EBX, EDX, ECX: CPU vendor ID string (e.g., "GenuineIntel", "AuthenticAMD").
  2. Processor Info and Feature Bits (EAX=1):
    • Input: EAX = 1
    • Output:
      • EAX: Processor Info (stepping, model, family).
      • EBX: Additional feature information (e.g., brand index, CLFLUSH cache line size).
      • ECX: Feature flags.
      • EDX: Feature flags.
  3. Cache and TLB Information (EAX=2):
    • Input: EAX = 2
    • Output:
      • EAX, EBX, ECX, EDX: Cache and TLB descriptor information.
  4. Processor Serial Number (EAX=3):
    • Input: EAX = 3
    • Output:
      • EAX, EBX, ECX, EDX: Processor serial number (if supported and enabled).
  5. Extended Function Information (EAX=0x80000000 and above):
    • Input: EAX = 0x80000000 (Get the maximum input value for extended functions).
    • Output:
      • EAX: Maximum input value for extended functions.
    • Input: EAX = 0x80000001 to 0x80000008 for extended function details like processor brand string, cache details, etc.

 

Step-by-Step Process