9#ifndef MAXOS_SYSTEM_CPU_H
10#define MAXOS_SYSTEM_CPU_H
22namespace MaxOS::processes {
27namespace MaxOS::system {
131 HYPERVISOR = 1 << 31,
268 [[noreturn]]
static void halt();
281 static uint64_t
read_msr(uint32_t msr);
282 static void write_msr(uint32_t msr, uint64_t value);
284 static void cpuid(uint32_t leaf, uint32_t* eax, uint32_t* ebx, uint32_t* ecx, uint32_t* edx);
Defines an Advanced Configuration And Power Interface (ACPI class for handling ACPI table parsing and...
Defines classes and structures for handling the Advanced Programmable Interrupt Controller (APIC) in ...
struct PACKED MaxOS::hardwarecommunication::MADT_PROCESSOR_APIC madt_processor_apic_t
Alias for MADT_PROCESSOR_APIC struct.
Stores the left, top, width and height of a rectangle.
Enables a resource to be used by only one instance at a time through locking and unlocking.
Handles ACPI table parsing and retrieval.
Handles both the Local APIC (boot core) and IO APIC.
Handles the local APIC for the current core.
Schedules processes to run on the core via their threads.
Manages the CPU and its cores.
static void print_registers(cpu_status_t *status)
Prints the CPU registers from the provided structure.
static void PANIC(const char *message, cpu_status_t *status=nullptr)
Puts the CPU into a panic state and halts it. Dumps the stack trace and registers.
hardwarecommunication::AdvancedConfigurationAndPowerInterface acpi
The ACPI interface for the CPU.
static void halt()
Halts the CPU indefinitely.
static void get_status(cpu_status_t *status)
Gets the current CPU status into the provided structure.
void init_cores()
Wake up all the cores.
static bool check_cpu_feature(CPU_FEATURE_ECX feature)
Checks if a CPU feature is supported (ECX Register)
hardwarecommunication::AdvancedProgrammableInterruptController apic
The APIC interface for the CPU.
static void cpuid(uint32_t leaf, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
Executes the CPUID instruction with the specified leaf and returns the results in the provided pointe...
static cpu_status_t * prepare_for_panic(cpu_status_t *status=nullptr, const string &msg="")
Ensure the CPU must panic and prepare for it if so.
static void set_status(cpu_status_t *status)
Sets the CPU registers from the provided structure.
void find_cores() const
Search the madt for cores and store them.
static uint64_t read_msr(uint32_t msr)
Reads the value of the specified MSR.
static common::Vector< Core * > cores
The list of CPU cores in the system (populated during initialization, includes the BSP and cores that...
static Core * executing_core()
Gets the core that is currently executing.
static void write_msr(uint32_t msr, uint64_t value)
Writes the specified value to the specified MSR.
static Core * panic_core
The core that triggered the panic.
static void stack_trace(size_t)
Prints a stack trace up to the specified level.
static common::Spinlock panic_lock
Lock to prevent multiple panics at once.
static bool check_nx()
Checks if the No Execute page flag is supported.
Represents a CPU core in the system.
void wake_up(CPU *cpu)
Wakes up the core by sending the appropriate IPIs. (see core_loader.s for startup code)
void init_tss()
Initialises the task state segment for this core.
uint64_t m_stack
The stack pointer for this core.
bool m_bsp
Whether this core is the bootstrap processor.
void init_sse()
Initialises the SSE instructions.
GlobalDescriptorTable * gdt
The GDT for this core.
void init()
Initialises the core by setting up the GDT, IDT, TSS, SSE and APIC.
uint8_t id
The ID of this core.
bool active
Whether this core is active.
hardwarecommunication::LocalAPIC * local_apic
The local APIC for this core.
processes::Scheduler * scheduler
The scheduler for this core.
uint8_t m_apic_id
The ID of the apic for this core.
bool xsave_enabled
Whether XSAVE is enabled.
bool m_enabled
Whether the core is enabled.
bool avx_enabled
Whether AVX is enabled.
bool m_can_enable
Whether the core can be enabled.
hardwarecommunication::madt_processor_apic_t * m_madt
The MADT entry for this core.
tss_t tss
The Task State Segment for this core.
Sets up the GDT in the CPU.
Parses and provides access to Multiboot 2 information.
constexpr size_t BOOT_STACK_SIZE
The size of the stack allocated for booting a core (should align with the startup assembly code for t...
struct PACKED MaxOS::system::CPUStatus cpu_status_t
Alias for CPUStatus struct.
CPU_FEATURE_ECX
CPU features indicated by the ECX register after calling CPUID with EAX = 1.
CPU_FEATURE_EDX
CPU features indicated by the EDX register after calling CPUID with EAX = 1.
struct PACKED MaxOS::system::TaskStateSegment tss_t
Alias for TaskStateSegment struct.
Defines a Global Descriptor Table (GDT) for setting up memory segments in protected mode.
Defines a PhysicalMemoryManager class for managing physical memory allocation and deallocation of pag...
Structure representing the CPU state during an interrupt.
uint64_t error_code
The error code (if applicable)
uint64_t r15
Register r15.
uint64_t rdi
Register rdi (used for first argument)
uint64_t rip
Instruction pointer.
uint64_t r10
Register r10.
uint64_t rflags
Flags register.
uint64_t rcx
Register rcx (used for fourth argument)
uint64_t rdx
Register rdx (used for third argument)
uint64_t ss
Stack segment.
uint64_t interrupt_number
The interrupt number.
uint64_t rsi
Register rsi (used for second argument)
uint64_t r14
Register r14.
uint64_t rbp
Register rbp (base pointer)
uint64_t r11
Register r11.
uint64_t rsp
Stack pointer.
uint64_t r13
Register r13.
uint64_t rbx
Register rbx (base register)
uint64_t rax
Register rax (accumulator)
uint64_t r12
Register r12.
Information needed when booting a core.
uint64_t stack
The stack pointer for the core.
uint64_t p4_table
The physical address of the P4 page table.
uint8_t id
The ID of the core.
bool activated
Whether the core has been activated.
void * gdt_64_base
The base of the 64-bit GDT.
A snapshot of the a frame in the call stack.
uintptr_t rip
The instruction pointer at this frame.
StackFrame * next
Pointer to the next stack frame (up the call stack)
Structure representing the Task State Segment (TSS)
uint64_t reserved1
Unused, must be zero.
uint64_t rsp2
Stack pointer for ring 2.
uint64_t reserved2
Unused, must be zero.
uint32_t reserved0
Unused, must be zero.
uint16_t io_bitmap_offset
Offset to the I/O bitmap.
uint64_t reserved3
Unused, must be zero.
uint64_t ist5
Interrupt Stack Table entry 5.
uint64_t ist6
Interrupt Stack Table entry 6.
uint64_t ist1
Interrupt Stack Table entry 1.
uint64_t ist4
Interrupt Stack Table entry 4.
uint64_t rsp1
Stack pointer for ring 1.
uint16_t reserved4
Unused, must be zero.
uint64_t ist7
Interrupt Stack Table entry 7.
uint64_t rsp0
Stack pointer for ring 0.
uint64_t ist3
Interrupt Stack Table entry 3.
uint64_t ist2
Interrupt Stack Table entry 2.