![]() |
Max OS 0.3
|
Manages the CPU and its cores. More...
#include <cpu.h>
Public Member Functions | |
| CPU (GlobalDescriptorTable *gdt, Multiboot *multiboot) | |
| Constructor for the CPU class. | |
| void | find_cores () const |
| Search the madt for cores and store them. | |
| void | init_cores () |
| Wake up all the cores. | |
Static Public Member Functions | |
| 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 | 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. | |
| static void | halt () |
| Halts the CPU indefinitely. | |
| static Core * | executing_core () |
| Gets the core that is currently executing. | |
| static bool | check_nx () |
| Checks if the No Execute page flag is supported. | |
| static void | get_status (cpu_status_t *status) |
| Gets the current CPU status into the provided structure. | |
| static void | set_status (cpu_status_t *status) |
| Sets the CPU registers from the provided structure. | |
| static void | print_registers (cpu_status_t *status) |
| Prints the CPU registers from the provided structure. | |
| static uint64_t | read_msr (uint32_t msr) |
| Reads the value of the specified MSR. | |
| static void | write_msr (uint32_t msr, uint64_t value) |
| Writes the specified value to the specified MSR. | |
| 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 pointers. | |
| static bool | check_cpu_feature (CPU_FEATURE_ECX feature) |
| Checks if a CPU feature is supported (ECX Register) | |
| static bool | check_cpu_feature (CPU_FEATURE_EDX feature) |
| Checks if a CPU feature is supported (EDX Register) | |
| static void | stack_trace (size_t, cpu_status_t *status=nullptr) |
| Prints a stack trace up to the specified level. | |
| static void | enable_write_protect () |
| static void | disable_write_protect () |
Public Attributes | |
| hardwarecommunication::AdvancedConfigurationAndPowerInterface | acpi |
| The ACPI interface for the CPU. | |
| hardwarecommunication::AdvancedProgrammableInterruptController | apic |
| The APIC interface for the CPU. | |
Static Public Attributes | |
| static Core * | panic_core = nullptr |
| The core that triggered the panic. | |
| static common::Spinlock | panic_lock |
| Lock to prevent multiple panics at once. | |
| static common::Vector< Core * > | cores |
| The list of CPU cores in the system (populated during initialization, includes the BSP and cores that failed to start) | |
| CPU::CPU | ( | GlobalDescriptorTable * | gdt, |
| Multiboot * | multiboot | ||
| ) |
Constructor for the CPU class.
| gdt | The global descriptor table |
| multiboot | The multiboot information struct |
Definition at line 251 of file cpu.cpp.
References apic, cores, find_cores(), MaxOS::Logger::INFO(), and MaxOS::hardwarecommunication::AdvancedProgrammableInterruptController::local_apic().
|
static |
Checks if a CPU feature is supported (ECX Register)
| feature | The feature to check |
Definition at line 612 of file cpu.cpp.
References cpuid().
Referenced by MaxOS::system::Core::init_sse().
|
static |
Checks if a CPU feature is supported (EDX Register)
| feature | The feature to check |
Definition at line 628 of file cpu.cpp.
References cpuid().
|
static |
Checks if the No Execute page flag is supported.
Definition at line 642 of file cpu.cpp.
References MaxOS::Logger::DEBUG(), and read_msr().
Referenced by MaxOS::memory::PhysicalMemoryManager::PhysicalMemoryManager().
|
static |
Executes the CPUID instruction with the specified leaf and returns the results in the provided pointers.
| leaf | The CPUID leaf to query |
| eax | The pointer to store the EAX result |
| ebx | The pointer to store the EBX result |
| ecx | The pointer to store the ECX result |
| edx | The pointer to store the EDX result |
Definition at line 402 of file cpu.cpp.
Referenced by check_cpu_feature(), check_cpu_feature(), executing_core(), and MaxOS::hardwarecommunication::LocalAPIC::LocalAPIC().
|
static |
|
static |
|
static |
Gets the core that is currently executing.
Definition at line 660 of file cpu.cpp.
References cores, and cpuid().
Referenced by core_main(), MaxOS::processes::GlobalScheduler::core_scheduler(), MaxOS::hardwarecommunication::GeneralProtectionHandler::get_message(), MaxOS::hardwarecommunication::InterruptManager::handle_interrupt_request(), MaxOS::processes::Thread::load_port_bitmap(), prepare_for_panic(), MaxOS::processes::GlobalScheduler::print_running_header(), MaxOS::processes::Thread::restore_sse_state(), MaxOS::processes::Thread::save_sse_state(), and MaxOS::processes::Thread::Thread().
| void CPU::find_cores | ( | ) | const |
Search the madt for cores and store them.
Definition at line 564 of file cpu.cpp.
References apic, cores, MaxOS::hardwarecommunication::IOAPIC::get_madt_item(), and MaxOS::hardwarecommunication::AdvancedProgrammableInterruptController::io_apic().
Referenced by CPU().
|
static |
Gets the current CPU status into the provided structure.
| status | The structure to fill with the CPU status |
Definition at line 286 of file cpu.cpp.
Referenced by PANIC().
|
static |
Halts the CPU indefinitely.
Definition at line 275 of file cpu.cpp.
Referenced by core_main(), MaxOS::hardwarecommunication::CPUStopHandler::handle_interrupt(), and PANIC().
| void CPU::init_cores | ( | ) |
Wake up all the cores.
Definition at line 588 of file cpu.cpp.
References core_boot_info, core_start(), cores, MaxOS::memory::PhysicalMemoryManager::identity_map(), MaxOS::Logger::INFO(), p4_table, MaxOS::memory::PRESENT, MaxOS::memory::PhysicalMemoryManager::s_current_manager, MaxOS::memory::PhysicalMemoryManager::to_lower_region(), and MaxOS::memory::WRITE.
Referenced by kernel_main().
|
static |
Puts the CPU into a panic state and halts it. Dumps the stack trace and registers.
| message | The panic message |
| status | The CPU status at the time of the panic (if available, otherwise will be fetched) |
Definition at line 463 of file cpu.cpp.
References MaxOS::processes::GlobalScheduler::core_scheduler(), MaxOS::processes::GlobalScheduler::current_process(), MaxOS::Logger::ERROR(), get_status(), halt(), MaxOS::system::Core::id, MaxOS::processes::Process::name, MaxOS::Logger::Out(), panic_core, panic_lock, prepare_for_panic(), print_registers(), stack_trace(), MaxOS::processes::Scheduler::ticks(), and MaxOS::processes::Process::total_ticks().
Referenced by MaxOS::Logger::assert(), and MaxOS::hardwarecommunication::ExceptionHandler::handle_interrupt().
|
static |
Ensure the CPU must panic and prepare for it if so.
| status | The status of the CPU (if available) |
| msg | The message to print to diagnose the cause of the panic (prints when close program) |
Definition at line 521 of file cpu.cpp.
References MaxOS::Logger::active_logger(), MaxOS::processes::GlobalScheduler::current_process(), MaxOS::processes::GlobalScheduler::deactivate(), MaxOS::Logger::ERROR(), executing_core(), MaxOS::processes::GlobalScheduler::force_remove_process(), MaxOS::memory::PhysicalMemoryManager::in_higher_region(), MaxOS::processes::GlobalScheduler::is_active(), MaxOS::system::Core::local_apic, MaxOS::processes::Process::name, MaxOS::Logger::Out(), panic_core, panic_lock, MaxOS::console::FramebufferConsole::print_logo(), MaxOS::hardwarecommunication::LocalAPIC::send_interrupt(), and MaxOS::processes::GlobalScheduler::system_scheduler().
Referenced by MaxOS::hardwarecommunication::ExceptionHandler::handle_interrupt(), and PANIC().
|
static |
|
static |
Reads the value of the specified MSR.
| msr | The MSR to read |
Definition at line 371 of file cpu.cpp.
Referenced by check_nx(), MaxOS::hardwarecommunication::LocalAPIC::LocalAPIC(), and MaxOS::hardwarecommunication::LocalAPIC::read().
|
static |
Sets the CPU registers from the provided structure.
| status | The structure containing the CPU status |
Definition at line 311 of file cpu.cpp.
|
static |
Prints a stack trace up to the specified level.
| level | The number of stack frames to print |
| status | The execution state to trace (optional, null will be interpreted as use the current state) |
Definition at line 414 of file cpu.cpp.
References MaxOS::Logger::ERROR(), and MaxOS::common::resolve_symbol().
Referenced by PANIC().
|
static |
Writes the specified value to the specified MSR.
| msr | The MSR to write to |
| value | The value to write |
Definition at line 387 of file cpu.cpp.
Referenced by MaxOS::hardwarecommunication::LocalAPIC::LocalAPIC(), MaxOS::hardwarecommunication::LocalAPIC::send_icr(), and MaxOS::hardwarecommunication::LocalAPIC::write().
| hardwarecommunication::AdvancedConfigurationAndPowerInterface MaxOS::system::CPU::acpi |
| hardwarecommunication::AdvancedProgrammableInterruptController MaxOS::system::CPU::apic |
The APIC interface for the CPU.
Definition at line 264 of file cpu.h.
Referenced by CPU(), find_cores(), kernel_main(), and MaxOS::system::Core::wake_up().
|
inlinestatic |
The list of CPU cores in the system (populated during initialization, includes the BSP and cores that failed to start)
Definition at line 273 of file cpu.h.
Referenced by MaxOS::processes::GlobalScheduler::activate(), MaxOS::processes::GlobalScheduler::add_process(), MaxOS::processes::GlobalScheduler::add_thread(), CPU(), MaxOS::processes::GlobalScheduler::deactivate(), executing_core(), find_cores(), MaxOS::processes::GlobalScheduler::force_remove_process(), MaxOS::processes::GlobalScheduler::get_process(), MaxOS::processes::GlobalScheduler::get_thread(), MaxOS::processes::GlobalScheduler::GlobalScheduler(), init_cores(), MaxOS::processes::GlobalScheduler::least_busy_core(), MaxOS::processes::GlobalScheduler::remove_process(), MaxOS::processes::Scheduler::Scheduler(), and MaxOS::processes::GlobalScheduler::~GlobalScheduler().
|
inlinestatic |
The core that triggered the panic.
Definition at line 266 of file cpu.h.
Referenced by PANIC(), and prepare_for_panic().
|
inlinestatic |
Lock to prevent multiple panics at once.
Definition at line 267 of file cpu.h.
Referenced by MaxOS::console::FramebufferConsole::get_background_color(), MaxOS::console::FramebufferConsole::get_foreground_color(), PANIC(), and prepare_for_panic().