kernel: rework init

This commit is contained in:
Matheus
2025-08-14 20:25:43 -03:00
parent fb6cbe49d7
commit 607566531d
41 changed files with 268 additions and 164 deletions
+3
View File
@@ -1,5 +1,6 @@
#include <kernel/auth.h>
#include <logging.h>
#include <kernel/init.h>
// default listeners for auth
@@ -285,3 +286,5 @@ void defaultauth_init() {
auth_registerlistener(AUTH_SCOPE_PROCESS, process);
auth_registerlistener(AUTH_SCOPE_NETWORK, network);
}
INIT_ROUTINE_DEFINE(default_auth, INIT_ROUTINE_FLAGS_NONE, defaultauth_init, bsp_early);
+27
View File
@@ -0,0 +1,27 @@
#include <kernel/init.h>
#include <arch/cpu.h>
#include <logging.h>
void init_run_routine(init_routine_t *routine) {
if (routine->flags & INIT_ROUTINE_FLAGS_ALREADY_DONE)
return;
for (int i = 0; i < routine->dependency_count; ++i)
init_run_routine(*routine->dependencies[i]);
if ((routine->flags & INIT_ROUTINE_FLAGS_QUIET) == 0)
printf("kernel_init: running routine %s\n", routine->name);
if ((routine->flags & INIT_ROUTINE_FLAGS_PHONY) == 0)
routine->fn();
routine->flags |= INIT_ROUTINE_FLAGS_ALREADY_DONE;
}
extern init_routine_t *init_routines;
extern init_routine_t *init_routines_end;
void init_run_all_routines(void) {
for (init_routine_t **routine_ptr = &init_routines; routine_ptr < &init_routines_end; ++routine_ptr)
init_run_routine(*routine_ptr);
}
+5
View File
@@ -13,6 +13,7 @@
#include <kernel/cmdline.h>
#include <kernel/auth.h>
#include <arch/smp.h>
#include <kernel/init.h>
#define QUANTUM_US 100000
#define SCHEDULER_STACK_SIZE PAGE_SIZE * 16
@@ -458,7 +459,11 @@ void sched_init() {
current_cpu()->reschedule_isr = interrupt_allocate(reschedule_ipi, ARCH_EOI, IPL_MAX);
__assert(current_cpu()->reschedule_isr);
sched_target_cpu(current_cpu());
timer_insert(current_cpu()->timer, &current_cpu()->schedtimerentry, reschedule_timer_dpc, NULL, QUANTUM_US, true);
// XXX move this resume to a more appropriate place
timer_resume(current_cpu()->timer);
}
INIT_ROUTINE_DEFINE(scheduler, INIT_ROUTINE_FLAGS_NONE, sched_init, arch_timer);
+8 -5
View File
@@ -4,6 +4,7 @@
#include <mutex.h>
#include <limine.h>
#include <logging.h>
#include <kernel/init.h>
static volatile struct limine_boot_time_request timereq = {
.id = LIMINE_BOOT_TIME_REQUEST,
@@ -106,6 +107,8 @@ void timekeeper_sync(void) {
MUTEX_RELEASE(&sync_mutex);
}
INIT_ROUTINE_DEFINE(sync_cpu_time, INIT_ROUTINE_FLAGS_NONE, timekeeper_sync, smp, timekeeper);
extern timekeeper_source_t *timekeeper_sources;
extern timekeeper_source_t *timekeeper_sources_end;
@@ -130,7 +133,7 @@ void timekeeper_wait_us(time_t us) {
CPU_PAUSE();
}
void timekeeper_early_init(time_t us_offset) {
void timekeeper_early_init(void) {
__assert(timereq.response);
boot_unix = timereq.response->boot_time;
@@ -151,14 +154,12 @@ void timekeeper_early_init(time_t us_offset) {
current_cpu()->timekeeper_source_info = early_source_info;
current_cpu()->timekeeper_source = early_source;
// this tick offset is to account for any possible time before early_init.
// for example, on SMP init the early init will have the time passed on the ap as an offset
current_cpu()->timekeeper_source_tick_offset = us_offset * early_source_info->hz / 1000000;
printf("cpu%d: timekeeper: \"%s\" selected as early source. %lu ticks at early init (%lu hz)\n",
current_cpu_id(), early_source->name, current_cpu()->timekeeper_source_base_ticks, early_source_info->hz);
}
INIT_ROUTINE_DEFINE(timekeeper_early, INIT_ROUTINE_FLAGS_NONE, timekeeper_early_init, arch_irq);
// this will do a re-init per cpu
// the initialization threads will not switch cpus so we dont have to worry about it here
void timekeeper_init(void) {
@@ -197,3 +198,5 @@ void timekeeper_init(void) {
printf("cpu%d: timekeeper: \"%s\" selected as main source. %lu ticks at init (%lu hz)\n",
current_cpu_id(), new_source->name, current_cpu()->timekeeper_source_base_ticks, new_source_info->hz);
}
INIT_ROUTINE_DEFINE(timekeeper, INIT_ROUTINE_FLAGS_NONE, timekeeper_init, scheduler);