KeblaOS

CPU Registers

System : Architecture : x86, Bit : 64

In x86-64 architecture, registers are small, fast storage locations within the CPU that are used to hold data, addresses, and control information. The x86-64 architecture extends the 32-bit x86 architecture by providing more registers and increasing their size to 64 bits. Here are the main types of registers and their uses:

1. General-Purpose Registers (GPRs)

These registers are used for a variety of purposes, including arithmetic, logic operations, and data movement. In x86-64, there are 16 general-purpose registers, each 64 bits wide. They can be accessed in different sizes:

Common Uses:

2. Segment Registers

These registers are used to segment memory and are primarily used in legacy 16-bit and 32-bit modes. In 64-bit mode, their use is limited.

Common Uses:

3. Instruction Pointer (RIP/EIP/IP)

This register holds the address of the next instruction to be executed.

Common Uses:

4. Flags Register (RFLAGS/EFLAGS/FLAGS)

This register contains status and control flags that affect the operation of the CPU.

Common Flags:

5. Floating-Point Registers (XMM, YMM, ZMM)

These registers are used for floating-point and SIMD (Single Instruction, Multiple Data) operations.

Common Uses:

6. Control Registers

These registers control the operation of the CPU and are used in system-level programming.

Common Uses:

7. Debug Registers

These registers are used for debugging purposes.

Common Uses:

8. Model-Specific Registers (MSRs)

These registers are used for CPU-specific features and are accessed using special instructions like RDMSR and WRMSR.

Common Uses:

Summary

Understanding these registers and their uses is crucial for low-level programming, such as writing assembly language code or developing operating systems.

For my KeblaOS I have define the following structure to manage cpu state

struct registers { // Total 26*8 = 208 bytes, 16 bytes aligned
    // Segment registers
    uint64_t gs;            // Offset 8*0
    uint64_t fs;            // Offset 8*1
    uint64_t es;            // Offset 8*2
    uint64_t ds;            // Offset 8*3

    // General purpose registers
    uint64_t rax;           // Offset 8*4
    uint64_t rbx;           // Offset 8*5
    uint64_t rcx;           // Offset 8*6
    uint64_t rdx;           // Offset 8*7
    uint64_t rbp;           // Offset 8*8
    uint64_t rdi;           // Offset 8*9
    uint64_t rsi;           // Offset 8*10
    uint64_t r8;            // Offset 8*11
    uint64_t r9;            // Offset 8*12
    uint64_t r10;           // Offset 8*13
    uint64_t r11;           // Offset 8*14
    uint64_t r12;           // Offset 8*15
    uint64_t r13;           // Offset 8*16
    uint64_t r14;           // Offset 8*17
    uint64_t r15;           // Offset 8*18

    uint64_t int_no;        // Offset 8*19
    uint64_t err_code;      // Offset 8*20

    // Interrupt return CPU state registers value
    uint64_t iret_rip;      // Offset 8*21
    uint64_t iret_cs;       // Offset 8*22
    uint64_t iret_rflags;   // Offset 8*23
    uint64_t iret_rsp;      // Offset 8*24
    uint64_t iret_ss;       // Offset 8*25
    
} __attribute__((packed));
typedef struct registers registers_t;