From 6d72600dd63f7974be433256e6beebe1c90bf403 Mon Sep 17 00:00:00 2001
From: Ian Moffett <ian@osmora.org>
Date: Thu, 22 Feb 2024 22:40:57 -0500
Subject: kernel/amd64: Create per-processor context storage

This commit introduces per-processor context storage within the
IA32_KERNEL_GS_BASE MSR

Signed-off-by: Ian Moffett <ian@osmora.org>
---
 sys/arch/amd64/amd64/cpu.c     | 22 +---------------------
 sys/arch/amd64/amd64/cpu_mp.c  | 28 +++++-----------------------
 sys/arch/amd64/amd64/machdep.c | 12 +++++++++++-
 sys/include/arch/amd64/cpu.h   |  1 -
 4 files changed, 17 insertions(+), 46 deletions(-)

diff --git a/sys/arch/amd64/amd64/cpu.c b/sys/arch/amd64/amd64/cpu.c
index d8617d8..e608535 100644
--- a/sys/arch/amd64/amd64/cpu.c
+++ b/sys/arch/amd64/amd64/cpu.c
@@ -33,28 +33,8 @@
 __KERNEL_META("$Hyra$: cpu.c, Ian Marco Moffett, "
               "AMD64 CPU abstraction module");
 
-/* XXX: Must be zero'd!! */
-static struct cpu_info bsp_info = {0};
-
-struct cpu_info *
-amd64_get_bsp(void)
-{
-    return &bsp_info;
-}
-
-/*
- * TODO: Update this when adding SMP
- *       support.
- */
 struct cpu_info *
 amd64_this_cpu(void)
 {
-    struct cpu_ctx *cctx;
-
-    if (!mp_supported()) {
-        return amd64_get_bsp();
-    }
-
-    cctx = (void *)amd64_read_gs_base();
-    return cctx->ci;
+    return (void *)amd64_read_gs_base();
 }
diff --git a/sys/arch/amd64/amd64/cpu_mp.c b/sys/arch/amd64/amd64/cpu_mp.c
index c382439..b00e34e 100644
--- a/sys/arch/amd64/amd64/cpu_mp.c
+++ b/sys/arch/amd64/amd64/cpu_mp.c
@@ -48,35 +48,20 @@ static volatile struct limine_smp_request g_smp_req = {
 
 static bool is_mp_supported = false;
 
-static void
-ap_create_cctx(struct cpu_info *ci)
-{
-    struct cpu_ctx *cctx;
-
-    cctx = dynalloc(sizeof(struct cpu_ctx));
-    __assert(cctx != NULL);
-
-    cctx->ci = ci;
-
-    /* Set *our* %GS now... */
-    wrmsr(IA32_KERNEL_GS_BASE, (uintptr_t)cctx);
-
-    if (!is_mp_supported) {
-        is_mp_supported = true;
-    }
-}
-
 static void
 ap_trampoline(struct limine_smp_info *si)
 {
     struct cpu_info *ci;
+    static struct spinlock lock = {0};
 
-    ci = (void *)si->extra_argument;
+    spinlock_acquire(&lock);
 
+    ci = (void *)si->extra_argument;
     pre_init();
-    ap_create_cctx(ci);
     processor_init();
 
+    spinlock_release(&lock);
+
     sched_init_processor(ci);
 
     /* Should not be reached */
@@ -112,9 +97,6 @@ ap_bootstrap(struct cpu_info *ci)
         panic("System only has 1 core!\n");
     }
 
-    /* Create processor context */
-    ap_create_cctx(ci);
-
     KINFO("Bootstrapping %d cores...\n", cpu_init_counter);
     for (size_t i = 0; i < resp->cpu_count; ++i) {
         if (ci->id == cpus[i]->lapic_id) {
diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c
index 8e7adce..9795dbe 100644
--- a/sys/arch/amd64/amd64/machdep.c
+++ b/sys/arch/amd64/amd64/machdep.c
@@ -41,9 +41,12 @@
 #include <machine/cpu.h>
 #include <machine/uart.h>
 #include <vm/vm.h>
+#include <vm/dynalloc.h>
 #include <vm/physseg.h>
 #include <firmware/acpi/acpi.h>
 #include <sys/syslog.h>
+#include <assert.h>
+#include <string.h>
 
 __MODULE_NAME("machdep");
 __KERNEL_META("$Hyra$: machdep.c, Ian Marco Moffett, "
@@ -135,8 +138,15 @@ processor_init(void)
 {
     /* Indicates what doesn't need to be init anymore */
     static uint8_t init_flags = 0;
+    struct cpu_info *cur_cpu;
 
-    struct cpu_info *cur_cpu = this_cpu();
+    /* Create our cpu_info structure */
+    cur_cpu = dynalloc(sizeof(struct cpu_info));
+    __assert(cur_cpu != NULL);
+    memset(cur_cpu, 0, sizeof(struct cpu_info));
+
+    /* Set %GS to cpu_info */
+    amd64_write_gs_base((uintptr_t)cur_cpu);
 
     CPU_INFO_LOCK(cur_cpu);
     init_tss(cur_cpu);
diff --git a/sys/include/arch/amd64/cpu.h b/sys/include/arch/amd64/cpu.h
index cff86f5..7186a5f 100644
--- a/sys/include/arch/amd64/cpu.h
+++ b/sys/include/arch/amd64/cpu.h
@@ -111,6 +111,5 @@ amd64_read_gs_base(void)
 }
 
 struct cpu_info *amd64_this_cpu(void);
-struct cpu_info *amd64_get_bsp(void);
 
 #endif  /* !_AMD64_CPU_H_ */
-- 
cgit v1.2.3