Fix user CPU context saving
All checks were successful
Build documentation / build-and-deploy (push) Successful in 31s

This commit is contained in:
2026-01-25 17:39:34 +01:00
parent 95f590fb3b
commit 8650010992
9 changed files with 27 additions and 29 deletions

View File

@@ -48,7 +48,7 @@ void bootmain (void) {
amd64_hpet_init ();
mm_init2 ();
smp_init ();
proc_init ();

View File

@@ -165,13 +165,19 @@ static void amd64_intr_exception (struct saved_regs* regs) {
/* Handle incoming interrupt, dispatch IRQ handlers. */
void amd64_intr_handler (void* stack_ptr) {
spin_lock_ctx_t ctxcpu;
spin_lock_ctx_t ctxcpu, ctxpr;
amd64_load_kernel_cr3 ();
struct saved_regs* regs = stack_ptr;
spin_lock (&thiscpu->lock, &ctxcpu);
memcpy (&thiscpu->regs, regs, sizeof (struct saved_regs));
struct proc* proc_current = thiscpu->proc_current;
spin_lock (&proc_current->lock, &ctxpr);
memcpy (&proc_current->pdata.regs, regs, sizeof (struct saved_regs));
spin_unlock (&proc_current->lock, &ctxpr);
spin_unlock (&thiscpu->lock, &ctxcpu);
if (regs->trap <= 31) {

View File

@@ -3,9 +3,9 @@
#include <amd64/sched.h>
#include <libk/std.h>
#include <proc/proc.h>
#include <sync/spin_lock.h>
#include <sys/mm.h>
#include <sys/smp.h>
#include <sync/spin_lock.h>
void do_sched (struct proc* proc, spin_lock_t* cpu_lock, spin_lock_ctx_t* ctxcpu) {
spin_lock_ctx_t ctxpr;
@@ -14,7 +14,7 @@ void do_sched (struct proc* proc, spin_lock_t* cpu_lock, spin_lock_ctx_t* ctxcpu
thiscpu->tss.rsp0 = proc->pdata.kernel_stack;
thiscpu->syscall_kernel_stack = proc->pdata.kernel_stack;
spin_unlock (&proc->lock, &ctxpr);
spin_unlock (cpu_lock, ctxcpu);

View File

@@ -11,9 +11,9 @@
#include <proc/proc.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
#include <sys/sched.h>
#include <sys/smp.h>
#include <sys/syscall.h>
#include <sys/sched.h>
/// Cpu ID counter
static atomic_uint cpu_counter = 0;
@@ -83,7 +83,7 @@ static void amd64_smp_bootstrap (struct limine_mp_info* mp_info) {
DEBUG ("CPU %u is online!\n", thiscpu->id);
atomic_fetch_sub (&cpu_init_count, 1);
struct proc* spin_proc = proc_spawn_rd ("spin.exe");
proc_register (spin_proc, thiscpu);

View File

@@ -21,7 +21,6 @@ struct cpu {
volatile uint8_t irq_stack[KSTACK_SIZE] ALIGNED (16);
volatile struct gdt_extended gdt ALIGNED (16);
volatile struct tss tss;
struct saved_regs regs;
uintptr_t lapic_mmio_base;
uint64_t lapic_ticks;

View File

@@ -14,26 +14,26 @@
extern void amd64_syscall_entry (void);
int amd64_syscall_dispatch (void* stack_ptr) {
spin_lock_ctx_t ctxcpu;
spin_lock (&thiscpu->lock, &ctxcpu);
spin_lock_ctx_t ctxcpu, ctxpr;
amd64_load_kernel_cr3 ();
struct saved_regs* regs = stack_ptr;
memcpy (&thiscpu->regs, regs, sizeof (struct saved_regs));
spin_lock (&thiscpu->lock, &ctxcpu);
struct proc* caller = thiscpu->proc_current;
spin_lock (&caller->lock, &ctxpr);
memcpy (&caller->pdata.regs, regs, sizeof (struct saved_regs));
spin_unlock (&caller->lock, &ctxpr);
spin_unlock (&thiscpu->lock, &ctxcpu);
int syscall_num = regs->rax;
syscall_handler_func_t func = syscall_find_handler (syscall_num);
if (func == NULL) {
spin_unlock (&thiscpu->lock, &ctxcpu);
return -ST_SYSCALL_NOT_FOUND;
}
struct proc* caller = thiscpu->proc_current;
spin_unlock (&thiscpu->lock, &ctxcpu);
int result = func (caller, regs, regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9);
return result;

View File

@@ -18,7 +18,7 @@ bool proc_create_resource_mutex (struct proc_mutex* mutex) {
void proc_cleanup_resource_mutex (struct proc* proc, struct proc_resource* resource) {
struct proc_mutex* mutex = &resource->u.mutex;
proc_mutex_unlock (proc, mutex);
/* proc_mutex_unlock (proc, mutex); */
}
static void proc_mutex_suspend (struct proc* proc, struct proc_suspension_q* sq,

View File

@@ -294,7 +294,7 @@ static void proc_reap (void) {
}
void proc_sched (void) {
spin_lock_ctx_t ctxcpu, ctxpr;
spin_lock_ctx_t ctxcpu;
int s_cycles = atomic_fetch_add (&sched_cycles, 1);
@@ -306,15 +306,8 @@ void proc_sched (void) {
spin_lock (&cpu->lock, &ctxcpu);
struct proc* prev = cpu->proc_current;
next = proc_find_sched (cpu);
if (prev != NULL) {
spin_lock (&prev->lock, &ctxpr);
memcpy (&prev->pdata.regs, &cpu->regs, sizeof (struct saved_regs));
spin_unlock (&prev->lock, &ctxpr);
}
if (next) {
cpu->proc_current = next;