From 80abd907a81fe301ccebc3c8ff2b156978db13c5 Mon Sep 17 00:00:00 2001 From: Matheus Date: Mon, 16 Sep 2024 21:36:16 -0300 Subject: [PATCH] smp: smp_cpus --- kernel-src/arch/x86-64/smp.c | 24 ++++++++++++++++-------- kernel-src/include/x86-64/arch/smp.h | 3 +++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/kernel-src/arch/x86-64/smp.c b/kernel-src/arch/x86-64/smp.c index 5d7ac79f..69f67b10 100644 --- a/kernel-src/arch/x86-64/smp.c +++ b/kernel-src/arch/x86-64/smp.c @@ -6,6 +6,7 @@ #include #include #include +#include static volatile struct limine_smp_request smprequest = { .id = LIMINE_SMP_REQUEST @@ -20,7 +21,6 @@ static void cpuwakeuphalt(struct limine_smp_info *info) { for (;;) CPU_HALT(); } - static void cpuwakeup(struct limine_smp_info *info) { cpu_set((cpu_t *)info->extra_argument); @@ -47,6 +47,8 @@ void arch_smp_haltallothers() { arch_smp_sendipi(NULL, ¤t_cpu()->isr[0xfd], ARCH_SMP_IPI_OTHERCPUS, true); } +cpu_t **smp_cpus; + void arch_smp_wakeup() { interrupt_register(0xfd, (void *)cpuwakeuphalt, NULL, IPL_IGNORE); struct limine_smp_response *response = smprequest.response; @@ -59,21 +61,27 @@ void arch_smp_wakeup() { printf("smp: %d processor%s\n", response->cpu_count, response->cpu_count > 1 ? "s" : ""); // use physical pages so the other cpus have it on the hhdm - size_t apcpusize = ROUND_UP(sizeof(cpu_t) * response->cpu_count, PAGE_SIZE); - cpu_t *apcpu = pmm_alloc(apcpusize / PAGE_SIZE, PMM_SECTION_DEFAULT); - __assert(apcpu); - apcpu = MAKE_HHDM(apcpu); - memset(apcpu, 0, apcpusize); + size_t cpu_size = ROUND_UP(sizeof(cpu_t) * response->cpu_count, PAGE_SIZE); + cpu_t *cpus = pmm_alloc(cpu_size / PAGE_SIZE, PMM_SECTION_DEFAULT); + __assert(cpus); + cpus = MAKE_HHDM(cpus); + memset(cpus, 0, cpu_size); + + smp_cpus = alloc(response->cpu_count * sizeof(cpu_t *)); + __assert(smp_cpus); void (*wakeupfn)(struct limine_smp_info *) = cmdline_get("nosmp") ? cpuwakeuphalt : cpuwakeup; // make the other processors jump to cpuwakeup() for (int i = 0; i < response->cpu_count; ++i) { // skip the bootstrap processor - if (response->cpus[i]->lapic_id == response->bsp_lapic_id) + if (response->cpus[i]->lapic_id == response->bsp_lapic_id) { + smp_cpus[i] = get_bsp(); continue; + } - response->cpus[i]->extra_argument = (uint64_t)&apcpu[i]; + smp_cpus[i] = &cpus[i]; + response->cpus[i]->extra_argument = (uint64_t)&cpus[i]; __atomic_store_n(&response->cpus[i]->goto_address, wakeupfn, __ATOMIC_SEQ_CST); } diff --git a/kernel-src/include/x86-64/arch/smp.h b/kernel-src/include/x86-64/arch/smp.h index 43367192..9b149e75 100644 --- a/kernel-src/include/x86-64/arch/smp.h +++ b/kernel-src/include/x86-64/arch/smp.h @@ -6,7 +6,10 @@ #define ARCH_SMP_IPI_ALL 2 #define ARCH_SMP_IPI_OTHERCPUS 3 +#include + extern size_t arch_smp_cpusawake; +extern cpu_t **smp_cpus; void arch_smp_wakeup(); void arch_smp_sendipi(cpu_t *targcpu, isr_t *isr, int target, bool nmi);