]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
kernel: make process/scheduler arch-agnostic (sp/address space)
authorTulio A M Mendes <[email protected]>
Thu, 5 Feb 2026 22:40:26 +0000 (19:40 -0300)
committerTulio A M Mendes <[email protected]>
Thu, 5 Feb 2026 22:40:26 +0000 (19:40 -0300)
include/process.h
src/kernel/scheduler.c

index 94e14a73789af3591c9e99e2226e51ca063beb95..8a813e7c3400e528e9868c58c305cb9e8b48db44 100644 (file)
@@ -23,8 +23,8 @@ typedef enum {
 
 struct process {
     uint32_t pid;
-    uint32_t esp;
-    uint32_t cr3;
+    uintptr_t sp;
+    uintptr_t addr_space;
     uint32_t* kernel_stack;
     process_state_t state;
     uint32_t wake_at_tick;      // New: When to wake up (global tick count)
@@ -50,7 +50,7 @@ void process_wake_check(uint32_t current_tick);
 // The magic function that switches stacks (Implemented in Assembly)
 // old_esp_ptr: Address where we save the OLD process's ESP
 // new_esp: The NEW process's ESP to load
-extern void context_switch(uint32_t* old_esp_ptr, uint32_t new_esp);
+extern void context_switch(uintptr_t* old_sp_ptr, uintptr_t new_sp);
 
 // Yield the CPU to the next process voluntarily
 void schedule(void);
index 28293b846c2937d74b7713696376d17cd5f0da92..5c13a3055c08b1521f77ea2da5c48f11410022cc 100644 (file)
@@ -13,7 +13,7 @@
 #include "uart_console.h"
 #include "timer.h" // Need access to current tick usually, but we pass it in wake_check
 #include "spinlock.h"
-#include "gdt.h"
+#include "hal/cpu.h"
 #include <stddef.h>
 
 struct process* current_process = NULL;
@@ -49,7 +49,7 @@ void process_init(void) {
     kernel_proc->pid = 0;
     kernel_proc->state = PROCESS_RUNNING;
     kernel_proc->wake_at_tick = 0;
-    __asm__ volatile("mov %%cr3, %0" : "=r"(kernel_proc->cr3));
+    kernel_proc->addr_space = hal_cpu_get_address_space();
     
     current_process = kernel_proc;
     ready_queue_head = kernel_proc;
@@ -57,18 +57,17 @@ void process_init(void) {
     kernel_proc->next = kernel_proc;
 
     // Best effort: set esp0 to current stack until we have a dedicated kernel stack for PID 0
-    uintptr_t cur_esp;
-    __asm__ volatile("mov %%esp, %0" : "=r"(cur_esp));
-    tss_set_kernel_stack(cur_esp);
+    uintptr_t cur_esp = hal_cpu_get_stack_pointer();
+    hal_cpu_set_kernel_stack(cur_esp);
 
     spin_unlock_irqrestore(&sched_lock, flags);
 }
 
 void thread_wrapper(void (*fn)(void)) {
-    __asm__ volatile("sti"); 
+    hal_cpu_enable_interrupts();
     fn();
     uart_print("[SCHED] Thread exited.\n");
-    for(;;) __asm__("hlt");
+    for(;;) hal_cpu_idle();
 }
 
 struct process* process_create_kernel(void (*entry_point)(void)) {
@@ -81,7 +80,7 @@ struct process* process_create_kernel(void (*entry_point)(void)) {
 
     proc->pid = next_pid++;
     proc->state = PROCESS_READY;
-    proc->cr3 = current_process->cr3; 
+    proc->addr_space = current_process->addr_space;
     proc->wake_at_tick = 0;
     
     void* stack_phys = pmm_alloc_page_low();
@@ -101,7 +100,7 @@ struct process* process_create_kernel(void (*entry_point)(void)) {
     *--sp = (uint32_t)thread_wrapper;
     *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0;
     
-    proc->esp = (uint32_t)sp;
+    proc->sp = (uintptr_t)sp;
 
     proc->next = ready_queue_head;
     ready_queue_tail->next = proc;
@@ -170,12 +169,12 @@ void schedule(void) {
 
     // For ring3->ring0 transitions, esp0 must point to the top of the kernel stack.
     if (current_process->kernel_stack) {
-        tss_set_kernel_stack((uintptr_t)current_process->kernel_stack + 4096);
+        hal_cpu_set_kernel_stack((uintptr_t)current_process->kernel_stack + 4096);
     }
 
     spin_unlock(&sched_lock);
     
-    context_switch(&prev->esp, current_process->esp);
+    context_switch(&prev->sp, current_process->sp);
 
     irq_restore(irq_flags);
 }