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

@@ -47,8 +47,8 @@ void app_main (void) {
spawn (&app_thread1); spawn (&app_thread1);
/* for (volatile int i = 0; i < 1000*1000; i++) */ /* for (volatile int i = 0; i < 1000*1000; i++) */
/* ; */ /* ; */
for (;;) { for (;;) {
lock_mutex (MUTEX, RV_PRIVATE); lock_mutex (MUTEX, RV_PRIVATE);

View File

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

View File

@@ -3,9 +3,9 @@
#include <amd64/sched.h> #include <amd64/sched.h>
#include <libk/std.h> #include <libk/std.h>
#include <proc/proc.h> #include <proc/proc.h>
#include <sync/spin_lock.h>
#include <sys/mm.h> #include <sys/mm.h>
#include <sys/smp.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) { void do_sched (struct proc* proc, spin_lock_t* cpu_lock, spin_lock_ctx_t* ctxcpu) {
spin_lock_ctx_t ctxpr; spin_lock_ctx_t ctxpr;

View File

@@ -11,9 +11,9 @@
#include <proc/proc.h> #include <proc/proc.h>
#include <sync/spin_lock.h> #include <sync/spin_lock.h>
#include <sys/debug.h> #include <sys/debug.h>
#include <sys/sched.h>
#include <sys/smp.h> #include <sys/smp.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <sys/sched.h>
/// Cpu ID counter /// Cpu ID counter
static atomic_uint cpu_counter = 0; static atomic_uint cpu_counter = 0;

View File

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

View File

@@ -14,27 +14,27 @@
extern void amd64_syscall_entry (void); extern void amd64_syscall_entry (void);
int amd64_syscall_dispatch (void* stack_ptr) { int amd64_syscall_dispatch (void* stack_ptr) {
spin_lock_ctx_t ctxcpu; spin_lock_ctx_t ctxcpu, ctxpr;
spin_lock (&thiscpu->lock, &ctxcpu);
amd64_load_kernel_cr3 (); amd64_load_kernel_cr3 ();
struct saved_regs* regs = stack_ptr; 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; int syscall_num = regs->rax;
syscall_handler_func_t func = syscall_find_handler (syscall_num); syscall_handler_func_t func = syscall_find_handler (syscall_num);
if (func == NULL) { if (func == NULL) {
spin_unlock (&thiscpu->lock, &ctxcpu);
return -ST_SYSCALL_NOT_FOUND; 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); int result = func (caller, regs, regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9);
return result; 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) { void proc_cleanup_resource_mutex (struct proc* proc, struct proc_resource* resource) {
struct proc_mutex* mutex = &resource->u.mutex; 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, 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) { void proc_sched (void) {
spin_lock_ctx_t ctxcpu, ctxpr; spin_lock_ctx_t ctxcpu;
int s_cycles = atomic_fetch_add (&sched_cycles, 1); int s_cycles = atomic_fetch_add (&sched_cycles, 1);
@@ -306,15 +306,8 @@ void proc_sched (void) {
spin_lock (&cpu->lock, &ctxcpu); spin_lock (&cpu->lock, &ctxcpu);
struct proc* prev = cpu->proc_current;
next = proc_find_sched (cpu); 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) { if (next) {
cpu->proc_current = next; cpu->proc_current = next;