Environment variables WIP, fix waiting scheduling issues + CE cancel proc
All checks were successful
Build documentation / build-and-deploy (push) Successful in 3m24s

This commit is contained in:
2026-03-17 00:01:15 +01:00
parent 1b1e1e4954
commit b1648a146a
11 changed files with 91 additions and 54 deletions

View File

@@ -8,6 +8,7 @@ $(eval $(call add_lib,libioutil))
$(eval $(call add_lib,libterminal))
$(eval $(call add_lib,libfat))
$(eval $(call add_lib,libmalloc))
$(eval $(call add_lib,libdebugconsole))
$(eval $(call add_include,libkb))
cflags += -DPRINTF_INCLUDE_CONFIG_H=1

View File

@@ -5,6 +5,7 @@
#include "mprintf.h"
#include "parser.h"
#include "self.h"
#include <debugconsole.h>
#include <desc.h>
#include <filereader.h>
#include <filewriter.h>
@@ -339,15 +340,12 @@ static void help (struct context* context) {
}
static void cmd_cancel_proc (void* arg) {
int pid = *(int*)arg;
int pid = (int)arg;
char ch = 0;
for (;;) {
do {
mail_receive (&ch, 1);
if (ch == KB_CTRL ('C'))
break;
}
} while (ch != KB_CTRL ('C'));
kill (pid);
}
@@ -407,12 +405,12 @@ static void execute_cmd (struct ast_cmd* cmd, struct context* context) {
return;
}
struct process_data* cancel_pdata = process_spawn (&cmd_cancel_proc, &pid);
struct process_data* cancel_pdata = process_spawn (&cmd_cancel_proc, (void*)pid);
wait_for_pid (pid);
if (kill (cancel_pdata->pid) < 0)
free (cancel_pdata);
kill (cancel_pdata->pid);
process_data_free (cancel_pdata);
}
}

View File

@@ -31,15 +31,21 @@ int id_alloc (struct id_alloc* ida) {
spin_lock (&ida->lock, &fid);
for (size_t bit = 0; bit < ida->bm.nbits; bit++) {
if (!bm_test (&ida->bm, bit)) {
bm_set (&ida->bm, bit);
size_t start = ida->next_id;
size_t current = start;
do {
if (!bm_test (&ida->bm, current)) {
bm_set (&ida->bm, current);
ida->next_id = (current + 1) % ida->bm.nbits;
spin_unlock (&ida->lock, fid);
return (int)bit;
}
return (int)current;
}
current = (current + 1) % ida->bm.nbits;
} while (current != start);
spin_unlock (&ida->lock, fid);
return -ST_OOM_ERROR;
}

View File

@@ -8,6 +8,7 @@
struct id_alloc {
struct bm bm;
spin_lock_t lock;
int next_id;
};
int id_alloc (struct id_alloc* ida);

View File

@@ -221,7 +221,7 @@ static struct proc* proc_find_sched (struct cpu* cpu) {
struct list_node_link *current, *start;
if (cpu->proc_current)
if (cpu->proc_current && cpu->proc_current->cpu_run_q_link.next)
current = cpu->proc_current->cpu_run_q_link.next;
else
current = cpu->proc_run_q;
@@ -231,31 +231,75 @@ static struct proc* proc_find_sched (struct cpu* cpu) {
start = current;
do {
bool wrap = false;
while (current) {
struct proc* proc = list_entry (current, struct proc, cpu_run_q_link);
spin_lock (&proc->lock, &fp);
int state = proc->state;
if (state == PROC_READY && !(proc->flags & PROC_KPROC)) {
if (!proc->dead && state == PROC_READY && !(proc->flags & PROC_KPROC)) {
spin_unlock (&proc->lock, fp);
return proc;
}
spin_unlock (&proc->lock, fp);
current = current->next ? current->next : cpu->proc_run_q;
} while (current != start);
current = current->next;
if (!current && !wrap) {
current = cpu->proc_run_q;
wrap = true;
}
if (wrap && current == start)
break;
}
return NULL;
}
static void proc_reaper (struct reschedule_ctx* rctx) {
uint64_t fpt, fp, fc;
struct list_node_link* reaper_list = NULL;
spin_lock (&proc_tree_lock, &fpt);
spin_lock (&thiscpu->lock, &fc);
struct list_node_link *run_link, *tmp_run_link;
list_foreach (thiscpu->proc_run_q, run_link, tmp_run_link) {
struct proc* proc = list_entry (run_link, struct proc, cpu_run_q_link);
if (!proc->dead)
continue;
spin_lock (&proc->lock, &fp);
list_remove (thiscpu->proc_run_q, &proc->cpu_run_q_link);
rbtree_delete (&proc_tree, &proc->proc_tree_link);
list_append (reaper_list, &proc->reaper_list_link);
spin_unlock (&proc->lock, fp);
}
spin_unlock (&thiscpu->lock, fc);
spin_unlock (&proc_tree_lock, fpt);
struct list_node_link *rlink, *tmp_rlink;
list_foreach (reaper_list, rlink, tmp_rlink) {
struct proc* proc = list_entry (rlink, struct proc, reaper_list_link);
list_remove (reaper_list, &proc->reaper_list_link);
proc_cleanup (proc, rctx);
}
}
void proc_sched (void) {
struct proc* next = NULL;
struct cpu* cpu = thiscpu;
uint64_t fc;
retry:
spin_lock (&cpu->lock, &fc);
next = proc_find_sched (cpu);
@@ -268,7 +312,9 @@ void proc_sched (void) {
cpu->proc_current = NULL;
spin_unlock (&cpu->lock, fc);
spin ();
spin_lock_relax ();
goto retry;
}
}
@@ -288,10 +334,9 @@ void proc_kill (struct proc* proc, struct reschedule_ctx* rctx) {
spin_lock (&cpu->lock, &fc);
spin_lock (&proc->lock, &fp);
proc->state = PROC_DEAD;
proc->cpu = NULL;
proc->dead = true;
list_remove (cpu->proc_run_q, &proc->cpu_run_q_link);
cpu->proc_run_q_count--;
if (cpu->proc_current == proc)
cpu->proc_current = NULL;
@@ -299,16 +344,6 @@ void proc_kill (struct proc* proc, struct reschedule_ctx* rctx) {
spin_unlock (&proc->lock, fp);
spin_unlock (&cpu->lock, fc);
spin_lock (&proc_tree_lock, &fpt);
spin_lock (&proc->lock, &fp);
rbtree_delete (&proc_tree, &proc->proc_tree_link);
spin_unlock (&proc->lock, fp);
spin_unlock (&proc_tree_lock, fpt);
proc_cleanup (proc, rctx);
rctx_insert_cpu (rctx, cpu);
DEBUG ("killed PID %d\n", proc->pid);
@@ -321,11 +356,13 @@ void proc_wait_for (struct proc* proc, struct reschedule_ctx* rctx, struct proc*
static void proc_irq_sched (void* arg, void* regs, bool user, struct reschedule_ctx* rctx) {
(void)arg, (void)regs, (void)rctx;
proc_reaper (rctx);
if (!user) {
return;
}
proc_sched ();
rctx_insert_cpu (rctx, thiscpu);
}
void proc_init (void) {

View File

@@ -21,8 +21,7 @@
/* process states */
#define PROC_READY 0
#define PROC_DEAD 1
#define PROC_SUSPENDED 2
#define PROC_SUSPENDED 1
/* process flags */
#define PROC_USTK_PREALLOC (1 << 0)
@@ -34,6 +33,7 @@ struct reschedule_ctx;
struct proc {
int pid;
int exec_pid;
struct list_node_link reaper_list_link;
struct rb_node_link proc_tree_link;
struct rb_node_link procgroup_memb_tree_link;
struct list_node_link cpu_run_q_link;
@@ -44,6 +44,7 @@ struct proc {
spin_lock_t lock;
struct cpu* cpu;
int state;
bool dead;
uintptr_t uvaddr_argument;
void* mail_recv_buffer;
size_t mail_recv_size;

View File

@@ -47,7 +47,6 @@ int proc_sq_suspend (struct proc* proc, struct proc_suspension_q* sq, spin_lock_
if (cpu->proc_current == proc)
cpu->proc_current = NULL;
proc->cpu = NULL;
int state = proc->state;
spin_unlock (&sq->lock, fsq);

View File

@@ -537,14 +537,7 @@ DEFINE_SYSCALL (sys_get_procgroup) {
return SYSRESULT (-ST_NOT_FOUND);
spin_lock (&target_proc->lock, &fp);
if (target_proc->state == PROC_DEAD) {
spin_unlock (&target_proc->lock, fp);
return SYSRESULT (-ST_NOT_FOUND);
}
int pgid = target_proc->procgroup->pgid;
spin_unlock (&target_proc->lock, fp);
return SYSRESULT (pgid);
@@ -681,7 +674,7 @@ DEFINE_SYSCALL (sys_wait_for_pid) {
spin_lock (&wait_proc->lock, &fp);
if (wait_proc->state == PROC_DEAD) {
if (wait_proc->dead) {
spin_unlock (&wait_proc->lock, fp);
return SYSRESULT (-ST_NOT_FOUND);
}
@@ -694,7 +687,6 @@ DEFINE_SYSCALL (sys_wait_for_pid) {
}
/* int kill (int pid) */
DEFINE_SYSCALL (sys_kill) {
uint64_t fp;
@@ -705,15 +697,6 @@ DEFINE_SYSCALL (sys_kill) {
if (target_proc == NULL)
return SYSRESULT (-ST_NOT_FOUND);
spin_lock (&target_proc->lock, &fp);
if (target_proc->state == PROC_DEAD) {
spin_unlock (&target_proc->lock, fp);
return SYSRESULT (-ST_NOT_FOUND);
}
spin_unlock (&target_proc->lock, fp);
proc_kill (target_proc, rctx);
return ST_OK;

View File

@@ -2,6 +2,7 @@ include ../make/ufuncs.mk
$(eval $(call add_include,libsystem))
$(eval $(call add_include,libmalloc))
$(eval $(call add_include,libdebugconsole))
libname := libprocess

View File

@@ -1,3 +1,4 @@
#include <debugconsole.h>
#include <malloc.h>
#include <map.h>
#include <page_size.h>
@@ -21,6 +22,7 @@ struct process_data* process_spawn (process_func_t func, void* argument_ptr) {
return NULL;
}
pdata->stack = stack;
pdata->arg_ptr = argument_ptr;
pdata->fn = func;
@@ -36,3 +38,8 @@ struct process_data* process_spawn (process_func_t func, void* argument_ptr) {
pdata->pid = pid;
return pdata;
}
void process_data_free (struct process_data* pdata) {
free (pdata->stack);
free (pdata);
}

View File

@@ -14,9 +14,12 @@ struct process_data {
void* arg_ptr;
process_func_t fn;
int pid;
void* stack;
};
/* Spawn a new process within the same procgroup with argument */
struct process_data* process_spawn (process_func_t func, void* argument_ptr);
void process_data_free (struct process_data* pdata);
#endif // _LIBPROCESS_PROCESS_PROCESS_H