From 1e43292c3bee9fe9160cece8260f5c46daa54cd6 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 27 Jul 2021 14:30:26 +0200 Subject: Kernel: Introduce ProcessorSpecific for per-CPU data structures To add a new per-CPU data structure, add an ID for it to the ProcessorSpecificDataID enum. Then call ProcessorSpecific::initialize() when you are ready to construct the per-CPU data structure on the current CPU. It can then be accessed via ProcessorSpecific::get(). This patch replaces the existing hard-coded mechanisms for Scheduler and MemoryManager per-CPU data structure. --- Kernel/Arch/x86/Processor.h | 44 +++++++++++++++++++++--------------- Kernel/Arch/x86/common/Processor.cpp | 2 -- 2 files changed, 26 insertions(+), 20 deletions(-) (limited to 'Kernel/Arch') diff --git a/Kernel/Arch/x86/Processor.h b/Kernel/Arch/x86/Processor.h index 478ab0c2f7..1a27e071c7 100644 --- a/Kernel/Arch/x86/Processor.h +++ b/Kernel/Arch/x86/Processor.h @@ -21,10 +21,14 @@ namespace Kernel { class ProcessorInfo; -class SchedulerPerProcessorData; -struct MemoryManagerData; struct ProcessorMessageEntry; +enum class ProcessorSpecificDataID { + MemoryManager, + Scheduler, + __Count, +}; + #if ARCH(X86_64) # define MSR_FS_BASE 0xc0000100 # define MSR_GS_BASE 0xc0000101 @@ -127,8 +131,6 @@ class Processor { u8 m_physical_address_bit_width; ProcessorInfo* m_info; - MemoryManagerData* m_mm_data; - SchedulerPerProcessorData* m_scheduler_data; Thread* m_current_thread; Thread* m_idle_thread; @@ -142,6 +144,8 @@ class Processor { DeferredCallEntry* m_free_deferred_call_pool_entry; DeferredCallEntry m_deferred_call_pool[5]; + void* m_processor_specific_data[(size_t)ProcessorSpecificDataID::__Count]; + void gdt_init(); void write_raw_gdt_entry(u16 selector, u32 low, u32 high); void write_gdt_entry(u16 selector, Descriptor& descriptor); @@ -259,24 +263,15 @@ public: read_gs_ptr(__builtin_offsetof(Processor, m_self)) != 0; } - ALWAYS_INLINE void set_scheduler_data(SchedulerPerProcessorData& scheduler_data) - { - m_scheduler_data = &scheduler_data; - } - - ALWAYS_INLINE SchedulerPerProcessorData& get_scheduler_data() const - { - return *m_scheduler_data; - } - - ALWAYS_INLINE void set_mm_data(MemoryManagerData& mm_data) + template + T* get_specific() { - m_mm_data = &mm_data; + return static_cast(m_processor_specific_data[static_cast(T::processor_specific_data_id())]); } - ALWAYS_INLINE MemoryManagerData& get_mm_data() const + void set_specific(ProcessorSpecificDataID specific_id, void* ptr) { - return *m_mm_data; + m_processor_specific_data[static_cast(specific_id)] = ptr; } ALWAYS_INLINE void set_idle_thread(Thread& idle_thread) @@ -449,4 +444,17 @@ public: String platform_string() const; }; +template +class ProcessorSpecific { +public: + static void initialize() + { + Processor::current().set_specific(T::processor_specific_data_id(), new T); + } + static T& get() + { + return *Processor::current().get_specific(); + } +}; + } diff --git a/Kernel/Arch/x86/common/Processor.cpp b/Kernel/Arch/x86/common/Processor.cpp index 2a3ba5e907..29eed55ae4 100644 --- a/Kernel/Arch/x86/common/Processor.cpp +++ b/Kernel/Arch/x86/common/Processor.cpp @@ -304,8 +304,6 @@ UNMAP_AFTER_INIT void Processor::early_initialize(u32 cpu) m_message_queue = nullptr; m_idle_thread = nullptr; m_current_thread = nullptr; - m_scheduler_data = nullptr; - m_mm_data = nullptr; m_info = nullptr; m_halt_requested = false; -- cgit v1.2.3