1
0
Fork 0

feat/kernel: I dont remember but i did stuff regarding SMP

This commit is contained in:
Kevin Alavik 2025-05-26 21:04:18 +02:00
parent 7ad2167e9d
commit d0e4149de5
No known key found for this signature in database
GPG key ID: 47AAEA397DB76AD0
8 changed files with 47 additions and 29 deletions

View file

@ -22,7 +22,8 @@
"smp.h": "c",
"string_view": "c",
"lapic.h": "c",
"spinlock.h": "c"
"spinlock.h": "c",
"fb.h": "c"
},
"editor.formatOnPaste": true,
"editor.formatOnSave": true,

View file

@ -51,6 +51,7 @@ else
endif
CPPFLAGS := -I../external -I$(SRCDIR) -MMD -MP -DLIMINE_API_REVISION=3
CFLAGS += -std=c99
IMPLICIT_SRCS :=
ifneq ($(MAKECMDGOALS),menuconfig)

View file

@ -21,13 +21,13 @@ static inline void wrmsr(uint64_t msr, uint64_t value)
{
uint32_t low = value & 0xFFFFFFFF;
uint32_t high = value >> 32;
asm volatile("wrmsr" : : "c"(msr), "a"(low), "d"(high));
__asm__ volatile("wrmsr" : : "c"(msr), "a"(low), "d"(high));
}
static inline uint64_t rdmsr(uint64_t msr)
{
uint32_t low, high;
asm volatile("rdmsr" : "=a"(low), "=d"(high) : "c"(msr));
__asm__ volatile("rdmsr" : "=a"(low), "=d"(high) : "c"(msr));
return ((uint64_t)high << 32) | low;
}

View file

@ -49,6 +49,9 @@ void smp_entry(struct limine_mp_info *smp_info)
set_cpu_local(cpu);
atomic_fetch_add(&started_cpus, 1);
log_early("CPU %d is up", cpu->cpu_index);
cpu->ready = true;
while (1)
__asm__ volatile("hlt");
}
@ -67,21 +70,37 @@ void smp_init(void)
memset(&cpu_locals[i], 0, sizeof(cpu_local_t));
cpu_locals[i].lapic_id = info->lapic_id;
cpu_locals[i].cpu_index = i;
cpu_locals[i].ready = false;
if (info->lapic_id == bootstrap_lapic_id)
{
set_cpu_local(&cpu_locals[i]);
log_early("CPU %u (LAPIC %u) is the bootstrap processor", i, info->lapic_id);
log_early("CPU %u is the bootstrap processor", i);
atomic_fetch_add(&started_cpus, 1);
cpu_locals[i].ready = true;
}
else
{
atomic_store((_Atomic(void **))&info->goto_address, smp_entry);
__atomic_store_n(&info->goto_address, smp_entry, __ATOMIC_SEQ_CST);
while (atomic_load(&started_cpus) < (i + 1))
__asm__ volatile("pause");
}
}
while (atomic_load(&started_cpus) < cpu_count)
bool all_ready = false;
while (!all_ready)
{
all_ready = true;
for (uint32_t i = 0; i < cpu_count; i++)
{
if (!cpu_locals[i].ready)
{
all_ready = false;
__asm__ volatile("pause");
break;
}
}
}
log_early("All CPUs are ready");
}

View file

@ -3,6 +3,7 @@
#define SMP_H
#include <stdint.h>
#include <stdbool.h>
extern uint32_t bootstrap_lapic_id;
@ -10,6 +11,7 @@ typedef struct
{
uint32_t lapic_id;
uint32_t cpu_index;
bool ready;
} cpu_local_t;
void smp_init();

View file

@ -35,4 +35,6 @@ extern struct limine_mp_response *mp_response;
extern struct flanterm_context *ft_ctx;
#endif // FLANTERM_SUPPORT
#define LOG_SEPARATOR "----------------------------------------"
#endif // EMK_H

View file

@ -86,6 +86,7 @@ void emk_entry(void)
log_early("Experimental Micro Kernel (EMK) 1.0 Copytright (c) 2025 Piraterna");
log_early("Compiled at %s %s, emk1.0-%s, flanterm support: %s", __TIME__, __DATE__, BUILD_MODE, FLANTERM_SUPPORT ? "yes" : "no");
log_early("%s", LOG_SEPARATOR);
if (!LIMINE_BASE_REVISION_SUPPORTED)
{
@ -94,9 +95,16 @@ void emk_entry(void)
}
gdt_init();
log_early("Initialized GDT");
idt_init();
log_early("Initialized IDT");
/* Setup SMP */
if (!mp_request.response)
{
kpanic(NULL, "Failed to get MP request");
}
mp_response = mp_request.response;
smp_init();
/* Setup physical memory*/
if (!hhdm_request.response)
@ -111,7 +119,6 @@ void emk_entry(void)
memmap = memmap_request.response;
hhdm_offset = hhdm_request.response->offset;
log_early("HHDM Offset: %llx", hhdm_offset);
pmm_init();
/* Test allocate a single physical page */
@ -120,9 +127,7 @@ void emk_entry(void)
kpanic(NULL, "Failed to allocate single physical page");
*a = 32;
log_early("Allocated 1 physical page @ %llx", (uint64_t)a);
pfree(a, 1);
log_early("Initialized physical page manager");
/* Setup virtual memory */
if (!kernel_address_request.response)
@ -133,7 +138,6 @@ void emk_entry(void)
kvirt = kernel_address_request.response->virtual_base;
kphys = kernel_address_request.response->physical_base;
paging_init();
log_early("Initialized paging");
/* Kernel Virtual Memory Context, not to be confused with KVM */
kvm_ctx = vinit(kernel_pagemap, 0x1000);
@ -149,9 +153,7 @@ void emk_entry(void)
}
*b = 32;
log_early("Allocated 1 virtual page @ %llx", (uint64_t)b);
vfree(kvm_ctx, b);
log_early("Initialized virtual page manager");
/* Setup kernel heap */
heap_init();
@ -162,21 +164,12 @@ void emk_entry(void)
}
*c = 32;
log_early("Allocated 1 byte @ %llx", (uint64_t)c);
kfree(c);
log_early("Initialized kernel heap");
/* Setup SMP */
if (!mp_request.response)
{
kpanic(NULL, "Failed to get MP request");
}
mp_response = mp_request.response;
smp_init();
log_early("Initialized SMP");
__asm__ volatile("int $0x01");
/* Finished */
log_early("%s", LOG_SEPARATOR);
uint32_t uptime = 0;
log_early("Finished initializing EMK v1.0, took %d seconds", uptime); /* Still not usermode, so keep using log_early */
hlt();
}

View file

@ -19,7 +19,7 @@ static inline void spinlock_acquire(spinlock_t *lock)
{
while (__atomic_test_and_set(&lock->lock, __ATOMIC_ACQUIRE))
{
asm volatile("pause" ::: "memory");
__asm__ volatile("pause" ::: "memory");
}
}