Browse Source

Kernel: Add initial implementation of Processor in aarch64

Instead of storing the current Processor into a core local register, we
currently just store it into a global, since we don't support SMP for
aarch64 anyway. This simplifies the initial implementation.
Timon Kruiper 3 years ago
parent
commit
c515e1341a

+ 35 - 0
Kernel/Arch/aarch64/Processor.cpp

@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2022, Timon Kruiper <timonkruiper@gmail.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/Format.h>
+
+#include <Kernel/Arch/Processor.h>
+#include <Kernel/Arch/aarch64/ASM_wrapper.h>
+#include <Kernel/Arch/aarch64/Prekernel/Aarch64_asm_utils.h>
+#include <Kernel/Arch/aarch64/Prekernel/Prekernel.h>
+
+extern "C" uintptr_t vector_table_el1;
+
+namespace Kernel {
+
+Processor* g_current_processor;
+
+void Processor::initialize(u32 cpu)
+{
+    VERIFY(g_current_processor == nullptr);
+
+    auto current_exception_level = static_cast<u64>(Kernel::Aarch64::Asm::get_current_exception_level());
+    dbgln("CPU{} started in: EL{}", cpu, current_exception_level);
+
+    dbgln("Drop CPU{} to EL1", cpu);
+    Prekernel::drop_to_exception_level_1();
+
+    // Load EL1 vector table
+    el1_vector_table_install(&vector_table_el1);
+
+    g_current_processor = this;
+}
+}

+ 11 - 2
Kernel/Arch/aarch64/Processor.h

@@ -22,6 +22,7 @@ class PageDirectory;
 };
 };
 
 
 class Thread;
 class Thread;
+class Processor;
 
 
 // FIXME This needs to go behind some sort of platform abstraction
 // FIXME This needs to go behind some sort of platform abstraction
 //       it is used between Thread and Processor.
 //       it is used between Thread and Processor.
@@ -30,8 +31,13 @@ struct [[gnu::aligned(16)]] FPUState
     u8 buffer[512];
     u8 buffer[512];
 };
 };
 
 
+// FIXME: Remove this once we support SMP in aarch64
+extern Processor* g_current_processor;
+
 class Processor {
 class Processor {
 public:
 public:
+    void initialize(u32 cpu);
+
     void set_specific(ProcessorSpecificDataID /*specific_id*/, void* /*ptr*/)
     void set_specific(ProcessorSpecificDataID /*specific_id*/, void* /*ptr*/)
     {
     {
         VERIFY_NOT_REACHED();
         VERIFY_NOT_REACHED();
@@ -79,9 +85,9 @@ public:
         VERIFY_NOT_REACHED();
         VERIFY_NOT_REACHED();
     }
     }
 
 
+    // FIXME: When aarch64 supports multiple cores, return the correct core id here.
     ALWAYS_INLINE static u32 current_id()
     ALWAYS_INLINE static u32 current_id()
     {
     {
-        VERIFY_NOT_REACHED();
         return 0;
         return 0;
     }
     }
 
 
@@ -127,7 +133,10 @@ public:
         return nullptr;
         return nullptr;
     }
     }
 
 
-    ALWAYS_INLINE static Processor& current() { VERIFY_NOT_REACHED(); }
+    ALWAYS_INLINE static Processor& current()
+    {
+        return *g_current_processor;
+    }
 
 
     static void deferred_call_queue(Function<void()> /* callback */)
     static void deferred_call_queue(Function<void()> /* callback */)
     {
     {

+ 10 - 12
Kernel/Arch/aarch64/init.cpp

@@ -10,9 +10,8 @@
 #include <AK/Format.h>
 #include <AK/Format.h>
 #include <AK/Types.h>
 #include <AK/Types.h>
 
 
-#include <Kernel/Arch/aarch64/ASM_wrapper.h>
+#include <Kernel/Arch/Processor.h>
 #include <Kernel/Arch/aarch64/BootPPMParser.h>
 #include <Kernel/Arch/aarch64/BootPPMParser.h>
-#include <Kernel/Arch/aarch64/Prekernel/Aarch64_asm_utils.h>
 #include <Kernel/Arch/aarch64/Prekernel/Prekernel.h>
 #include <Kernel/Arch/aarch64/Prekernel/Prekernel.h>
 #include <Kernel/Arch/aarch64/RPi/Framebuffer.h>
 #include <Kernel/Arch/aarch64/RPi/Framebuffer.h>
 #include <Kernel/Arch/aarch64/RPi/Mailbox.h>
 #include <Kernel/Arch/aarch64/RPi/Mailbox.h>
@@ -44,6 +43,12 @@ extern ctor_func_t end_heap_ctors[];
 extern ctor_func_t start_ctors[];
 extern ctor_func_t start_ctors[];
 extern ctor_func_t end_ctors[];
 extern ctor_func_t end_ctors[];
 
 
+ALWAYS_INLINE static Kernel::Processor& bootstrap_processor()
+{
+    alignas(Kernel::Processor) static u8 bootstrap_processor_storage[sizeof(Kernel::Processor)];
+    return (Kernel::Processor&)bootstrap_processor_storage;
+}
+
 extern "C" [[noreturn]] void init()
 extern "C" [[noreturn]] void init()
 {
 {
     dbgln("Welcome to Serenity OS!");
     dbgln("Welcome to Serenity OS!");
@@ -51,6 +56,9 @@ extern "C" [[noreturn]] void init()
     dbgln("Observed deviations from that ideal are shortcomings of your imagination.");
     dbgln("Observed deviations from that ideal are shortcomings of your imagination.");
     dbgln();
     dbgln();
 
 
+    new (&bootstrap_processor()) Kernel::Processor();
+    bootstrap_processor().initialize(0);
+
     // We call the constructors of kmalloc.cpp separately, because other constructors in the Kernel
     // We call the constructors of kmalloc.cpp separately, because other constructors in the Kernel
     // might rely on being able to call new/kmalloc in the constructor. We do have to run the
     // might rely on being able to call new/kmalloc in the constructor. We do have to run the
     // kmalloc constructors, because kmalloc_init relies on that.
     // kmalloc constructors, because kmalloc_init relies on that.
@@ -66,16 +74,6 @@ extern "C" [[noreturn]] void init()
     auto firmware_version = query_firmware_version();
     auto firmware_version = query_firmware_version();
     dbgln("Firmware version: {}", firmware_version);
     dbgln("Firmware version: {}", firmware_version);
 
 
-    auto current_exception_level = static_cast<u64>(Kernel::Aarch64::Asm::get_current_exception_level());
-    dbgln("CPU started in: EL{}", current_exception_level);
-
-    dbgln("Drop CPU to EL1");
-    Prekernel::drop_to_exception_level_1();
-
-    // Load EL1 vector table
-    extern uintptr_t vector_table_el1;
-    el1_vector_table_install(&vector_table_el1);
-
     dbgln("Initialize MMU");
     dbgln("Initialize MMU");
     Prekernel::init_prekernel_page_tables();
     Prekernel::init_prekernel_page_tables();
 
 

+ 1 - 0
Kernel/CMakeLists.txt

@@ -426,6 +426,7 @@ else()
         Arch/aarch64/MainIdRegister.cpp
         Arch/aarch64/MainIdRegister.cpp
         Arch/aarch64/PageDirectory.cpp
         Arch/aarch64/PageDirectory.cpp
         Arch/aarch64/Panic.cpp
         Arch/aarch64/Panic.cpp
+        Arch/aarch64/Processor.cpp
         Arch/aarch64/SafeMem.cpp
         Arch/aarch64/SafeMem.cpp
         Arch/aarch64/ScopedCritical.cpp
         Arch/aarch64/ScopedCritical.cpp
         Arch/aarch64/SmapDisabler.cpp
         Arch/aarch64/SmapDisabler.cpp