Simplify reschedule points, mail works now!
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m55s

This commit is contained in:
2026-02-19 18:25:47 +01:00
parent a1730dfdc2
commit 4472ad5bb3
13 changed files with 45 additions and 143 deletions

View File

@@ -9,7 +9,7 @@ void app_main (void) {
char ch; char ch;
mail_receive (&ch, 1); mail_receive (&ch, 1);
test (ch); /* test (ch); */
/* mail_send (e_pgid, &ch, 1); */ mail_send (e_pgid, &ch, 1);
} }
} }

View File

@@ -18,8 +18,9 @@ void app_main (void) {
mail_send (ce_pgid, (uint8_t*)&ch, 1); mail_send (ce_pgid, (uint8_t*)&ch, 1);
/* char rcv; */ char rcv;
/* mail_receive (&rcv, 1); */ mail_receive (&rcv, 1);
/* terminal_print (&rcv, 1); */ /* test (rcv); */
terminal_print (&rcv, 1);
} }
} }

View File

@@ -54,7 +54,7 @@ void bootmain (void) {
vfs_init (); vfs_init ();
struct device* ramdisk_device = device_find (RAMDISK_DEVICE); struct device* ramdisk_device = device_find (RAMDISK_DEVICE);
struct reschedule_ctx rctx = {.entries = NULL, .lock = SPIN_LOCK_INIT}; struct reschedule_ctx rctx = {.cpu = thiscpu, .reschedule = false};
int ret = vfs_create_mountpoint ("ramdisk", VFS_TARFS, ramdisk_device, NULL, &rctx); int ret = vfs_create_mountpoint ("ramdisk", VFS_TARFS, ramdisk_device, NULL, &rctx);
if (ret < 0) { if (ret < 0) {

View File

@@ -26,13 +26,9 @@ static bool amd64_debug_serial_tx_empty (void) {
/* Write a single character to serial */ /* Write a single character to serial */
static void amd64_debug_serial_write (char x) { static void amd64_debug_serial_write (char x) {
spin_lock (&serial_lock);
while (!amd64_debug_serial_tx_empty ()) while (!amd64_debug_serial_tx_empty ())
; ;
amd64_io_outb (PORT_COM1, (uint8_t)x); amd64_io_outb (PORT_COM1, (uint8_t)x);
spin_unlock (&serial_lock);
} }
/* /*
@@ -54,10 +50,14 @@ void debugprintf (const char* fmt, ...) {
const char* p = buffer; const char* p = buffer;
spin_lock (&serial_lock);
while (*p) { while (*p) {
amd64_debug_serial_write (*p); amd64_debug_serial_write (*p);
p++; p++;
} }
spin_unlock (&serial_lock);
} }
/* Initialize serial */ /* Initialize serial */

View File

@@ -157,35 +157,13 @@ static void amd64_intr_exception (struct saved_regs* regs) {
regs->rbx); regs->rbx);
if (regs->cs == (GDT_UCODE | 0x03)) { if (regs->cs == (GDT_UCODE | 0x03)) {
struct reschedule_ctx rctx = {.entries = NULL, .lock = SPIN_LOCK_INIT}; struct reschedule_ctx rctx = {.reschedule = false, .cpu = NULL};
proc_kill (thiscpu->proc_current, &rctx); proc_kill (thiscpu->proc_current, &rctx);
bool reschedule_thiscpu = false; if (rctx.reschedule)
cpu_request_sched (rctx.cpu);
spin_lock (&rctx.lock);
struct list_node_link *node, *tmp;
list_foreach (rctx.entries, node, tmp) {
struct reschedule_entry* entry = list_entry (node, struct reschedule_entry, link);
struct cpu* cpu = entry->cpu;
if (cpu != thiscpu) {
cpu_request_sched (cpu);
} else {
reschedule_thiscpu = true;
}
list_remove (rctx.entries, &entry->link);
free (entry);
}
spin_unlock (&rctx.lock);
if (reschedule_thiscpu) {
proc_sched ();
}
} else { } else {
__asm__ volatile ("cli");
spin (); spin ();
} }
} }
@@ -215,33 +193,11 @@ void amd64_intr_handler (void* stack_ptr) {
if (irq == NULL) if (irq == NULL)
return; return;
struct reschedule_ctx rctx = {.entries = NULL, .lock = SPIN_LOCK_INIT}; struct reschedule_ctx rctx = {.reschedule = false, .cpu = NULL};
irq->func (irq->arg, stack_ptr, &rctx); irq->func (irq->arg, stack_ptr, &rctx);
bool reschedule_thiscpu = false; if (rctx.reschedule)
cpu_request_sched (rctx.cpu);
spin_lock (&rctx.lock);
struct list_node_link *node, *tmp;
list_foreach (rctx.entries, node, tmp) {
struct reschedule_entry* entry = list_entry (node, struct reschedule_entry, link);
struct cpu* cpu = entry->cpu;
if (cpu != thiscpu) {
cpu_request_sched (cpu);
} else {
reschedule_thiscpu = true;
}
list_remove (rctx.entries, &entry->link);
free (entry);
}
spin_unlock (&rctx.lock);
if (reschedule_thiscpu) {
proc_sched ();
}
} }
} }

View File

@@ -37,7 +37,7 @@ uintptr_t amd64_syscall_dispatch (void* stack_ptr) {
return -ST_SYSCALL_NOT_FOUND; return -ST_SYSCALL_NOT_FOUND;
} }
struct reschedule_ctx rctx = {.entries = NULL, .lock = SPIN_LOCK_INIT}; struct reschedule_ctx rctx = {.reschedule = false, .cpu = NULL};
uintptr_t r = uintptr_t r =
func (caller, regs, &rctx, regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9); func (caller, regs, &rctx, regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9);
@@ -50,30 +50,8 @@ uintptr_t amd64_syscall_dispatch (void* stack_ptr) {
spin_unlock (&caller->lock); spin_unlock (&caller->lock);
} }
bool reschedule_thiscpu = false; if (rctx.reschedule)
cpu_request_sched (rctx.cpu);
spin_lock (&rctx.lock);
struct list_node_link *node, *tmp;
list_foreach (rctx.entries, node, tmp) {
struct reschedule_entry* entry = list_entry (node, struct reschedule_entry, link);
struct cpu* cpu = entry->cpu;
if (cpu != thiscpu) {
cpu_request_sched (cpu);
} else {
reschedule_thiscpu = true;
}
list_remove (rctx.entries, &entry->link);
free (entry);
}
spin_unlock (&rctx.lock);
if (reschedule_thiscpu) {
proc_sched ();
}
return r; return r;
} }

View File

@@ -37,7 +37,6 @@ void proc_cleanup_resource_mail (struct proc_resource* resource, struct reschedu
} }
spin_unlock (&mail->send_sq.lock); spin_unlock (&mail->send_sq.lock);
spin_unlock (&mail->resource->lock); spin_unlock (&mail->resource->lock);
} }
@@ -82,9 +81,12 @@ void proc_mail_send (struct proc* proc, struct proc_mail* mail, struct reschedul
spin_unlock (&mail->recv_sq.lock); spin_unlock (&mail->recv_sq.lock);
/* mail is empty and nobody is waiting */ /* mail is empty and nobody is waiting */
mail->pending_mesg = malloc (data_size); void* mesg = malloc (data_size);
if (mesg != NULL) {
mail->pending_mesg = mesg;
memcpy (mail->pending_mesg, data, data_size); memcpy (mail->pending_mesg, data, data_size);
mail->pending_mesg_size = data_size; mail->pending_mesg_size = data_size;
}
spin_unlock (&mail->resource->lock); spin_unlock (&mail->resource->lock);
} }
@@ -100,7 +102,7 @@ void proc_mail_receive (struct proc* proc, struct proc_mail* mail, struct resche
/* consume mesg if available */ /* consume mesg if available */
if (mail->pending_mesg != NULL) { if (mail->pending_mesg != NULL) {
memcpy (recv_buffer, mail->pending_mesg, recv_size); memcpy (recv_buffer, mail->pending_mesg, min (recv_size, mail->pending_mesg_size));
free (mail->pending_mesg); free (mail->pending_mesg);
mail->pending_mesg = NULL; mail->pending_mesg = NULL;
mail->pending_mesg_size = 0; mail->pending_mesg_size = 0;

View File

@@ -181,8 +181,10 @@ void proc_register (struct proc* proc, struct cpu* register_cpu, struct reschedu
spin_unlock (&cpu->lock); spin_unlock (&cpu->lock);
spin_unlock (&proc_tree_lock); spin_unlock (&proc_tree_lock);
if (rctx != NULL) if (rctx != NULL) {
reschedule_list_append (rctx, cpu); rctx->reschedule = true;
rctx->cpu = cpu;
}
} }
/* caller holds cpu->lock */ /* caller holds cpu->lock */
@@ -252,7 +254,7 @@ static void proc_reap (struct reschedule_ctx* rctx) {
void proc_sched (void) { void proc_sched (void) {
int s_cycles = atomic_fetch_add (&sched_cycles, 1); int s_cycles = atomic_fetch_add (&sched_cycles, 1);
struct reschedule_ctx rctx = {.entries = NULL, .lock = SPIN_LOCK_INIT}; struct reschedule_ctx rctx = {.reschedule = false, .cpu = NULL};
if (s_cycles % SCHED_REAP_FREQ == 0) if (s_cycles % SCHED_REAP_FREQ == 0)
proc_reap (&rctx); proc_reap (&rctx);
@@ -295,7 +297,8 @@ void proc_kill (struct proc* proc, struct reschedule_ctx* rctx) {
spin_unlock (&proc->lock); spin_unlock (&proc->lock);
spin_unlock (&cpu->lock); spin_unlock (&cpu->lock);
reschedule_list_append (rctx, cpu); rctx->reschedule = true;
rctx->cpu = cpu;
DEBUG ("killed PID %d\n", proc->pid); DEBUG ("killed PID %d\n", proc->pid);
} }

View File

@@ -1,32 +0,0 @@
#include <libk/list.h>
#include <mm/liballoc.h>
#include <proc/reschedule.h>
#include <sync/spin_lock.h>
#include <sys/smp.h>
void reschedule_list_append (struct reschedule_ctx* rctx, struct cpu* cpu) {
spin_lock (&rctx->lock);
struct list_node_link *node, *tmp;
list_foreach (rctx->entries, node, tmp) {
struct reschedule_entry* entry = list_entry (node, struct reschedule_entry, link);
if (entry->cpu == cpu) {
spin_unlock (&rctx->lock);
return;
}
}
struct reschedule_entry* entry = malloc (sizeof (*entry));
if (entry == NULL) {
spin_unlock (&rctx->lock);
return;
}
entry->cpu = cpu;
list_append (rctx->entries, &entry->link);
spin_unlock (&rctx->lock);
}

View File

@@ -5,16 +5,9 @@
#include <sync/spin_lock.h> #include <sync/spin_lock.h>
#include <sys/smp.h> #include <sys/smp.h>
struct reschedule_entry {
struct cpu* cpu;
struct list_node_link link;
};
struct reschedule_ctx { struct reschedule_ctx {
struct list_node_link* entries; bool reschedule;
spin_lock_t lock; struct cpu* cpu;
}; };
void reschedule_list_append (struct reschedule_ctx* rctx, struct cpu* cpu);
#endif // _KERNEL_PROC_RESCHEDULE_H #endif // _KERNEL_PROC_RESCHEDULE_H

View File

@@ -3,13 +3,11 @@ c += proc/proc.c \
proc/mutex.c \ proc/mutex.c \
proc/procgroup.c \ proc/procgroup.c \
proc/suspension_q.c \ proc/suspension_q.c \
proc/mail.c \ proc/mail.c
proc/reschedule.c
o += proc/proc.o \ o += proc/proc.o \
proc/resource.o \ proc/resource.o \
proc/mutex.o \ proc/mutex.o \
proc/procgroup.o \ proc/procgroup.o \
proc/suspension_q.o \ proc/suspension_q.o \
proc/mail.o \ proc/mail.o
proc/reschedule.o

View File

@@ -48,7 +48,8 @@ void proc_sq_suspend (struct proc* proc, struct proc_suspension_q* sq, spin_lock
spin_unlock (&proc->lock); spin_unlock (&proc->lock);
spin_unlock (&cpu->lock); spin_unlock (&cpu->lock);
reschedule_list_append (rctx, cpu); rctx->reschedule = true;
rctx->cpu = cpu;
} }
void proc_sq_resume (struct proc* proc, struct proc_sq_entry* sq_entry, void proc_sq_resume (struct proc* proc, struct proc_sq_entry* sq_entry,
@@ -80,7 +81,8 @@ void proc_sq_resume (struct proc* proc, struct proc_sq_entry* sq_entry,
free (sq_entry); free (sq_entry);
reschedule_list_append (rctx, cpu); rctx->reschedule = true;
rctx->cpu = cpu;
} }
void proc_sqs_cleanup (struct proc* proc) { void proc_sqs_cleanup (struct proc* proc) {

View File

@@ -108,7 +108,8 @@ DEFINE_SYSCALL (sys_argument_ptr) { return proc->uvaddr_argument; }
/* int sched (void) */ /* int sched (void) */
DEFINE_SYSCALL (sys_sched) { DEFINE_SYSCALL (sys_sched) {
reschedule_list_append (rctx, thiscpu); rctx->reschedule = true;
rctx->cpu = thiscpu;
return SYSRESULT (ST_OK); return SYSRESULT (ST_OK);
} }