summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-10-13 14:53:04 -0400
committerIan Moffett <ian@osmora.org>2025-10-13 14:53:04 -0400
commit44bbb86a75043b38bdfb9ed6ff4be0764e3fc905 (patch)
treed64c2a0baa08c85c9fb284be150204f10d84307b /src
parent959dd1c5dd5b2a369a33946a081d8764bcc51e2f (diff)
kern: proc: Implement process sleeping and waking
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src')
-rw-r--r--src/sys/arch/amd64/os/os_proc.c8
-rw-r--r--src/sys/include/sys/proc.h28
-rw-r--r--src/sys/os/os_proc.c49
-rw-r--r--src/sys/os/os_sched.c23
4 files changed, 106 insertions, 2 deletions
diff --git a/src/sys/arch/amd64/os/os_proc.c b/src/sys/arch/amd64/os/os_proc.c
index befeb34..93e5d73 100644
--- a/src/sys/arch/amd64/os/os_proc.c
+++ b/src/sys/arch/amd64/os/os_proc.c
@@ -82,6 +82,14 @@ md_proc_kick(struct proc *procp)
__builtin_unreachable();
}
+void
+md_proc_sleep(void)
+{
+ /* Wait for the timer to go off */
+ lapic_timer_oneshot_us(4000);
+ __ASMV("sti; hlt; cli");
+}
+
/*
* MD proc init code
*/
diff --git a/src/sys/include/sys/proc.h b/src/sys/include/sys/proc.h
index 9f5cd73..bb34066 100644
--- a/src/sys/include/sys/proc.h
+++ b/src/sys/include/sys/proc.h
@@ -90,6 +90,7 @@ struct proc {
struct filedesc *fdtab[FD_MAX];
struct penv_blk *envblk;
struct ptrbox *envblk_box;
+ struct proc *parent;
mac_level_t level;
struct spinlock maplist_lock;
sigtab_t sigtab;
@@ -99,6 +100,7 @@ struct proc {
};
#define PROC_EXITING BIT(0) /* Process is exiting */
+#define PROC_SLEEPING BIT(1) /* Process is sleeping */
/*
* Initialize a process into a basic minimal
@@ -204,6 +206,26 @@ int md_set_ip(struct proc *procp, uintptr_t ip);
int proc_check_addr(struct proc *proc, uintptr_t addr, size_t len);
/*
+ * Put a process to sleep
+ *
+ * @proc: Process to put to sleep
+ *
+ * Returns zero if the address is within the process bounds,
+ * otherwise a less than zero value on failure.
+ */
+int proc_sleep(struct proc *proc);
+
+/*
+ * Wake up a process
+ *
+ * @proc: Process to wake up
+ *
+ * Returns zero if the address is within the process bounds,
+ * otherwise a less than zero value on failure.
+ */
+int proc_wake(struct proc *proc);
+
+/*
* Lookup a process using its PID
*
* @pid: PID of process to lookup
@@ -213,6 +235,12 @@ int proc_check_addr(struct proc *proc, uintptr_t addr, size_t len);
struct proc *proc_lookup(pid_t pid);
/*
+ * Put the current process to sleep until woken
+ * up
+ */
+void md_proc_sleep(void);
+
+/*
* Put the current process into a halt loop
* until the next one runs.
*/
diff --git a/src/sys/os/os_proc.c b/src/sys/os/os_proc.c
index 971a6c0..4ee4fd8 100644
--- a/src/sys/os/os_proc.c
+++ b/src/sys/os/os_proc.c
@@ -169,6 +169,43 @@ proc_clear_ranges(struct proc *proc)
}
/*
+ * Put a process to sleep
+ */
+int
+proc_sleep(struct proc *proc)
+{
+ struct pcore *core = this_core();
+
+ if (proc == NULL || core == NULL) {
+ return -EINVAL;
+ }
+
+ proc->flags |= PROC_SLEEPING;
+ md_proc_sleep();
+ return 0;
+}
+
+/*
+ * Wake up a process
+ */
+int
+proc_wake(struct proc *proc)
+{
+ struct pcore *core = cpu_sched();
+
+ if (core == NULL || proc == NULL) {
+ return -1;
+ }
+
+ if (!ISSET(proc->flags, PROC_SLEEPING)) {
+ return -1;
+ }
+
+ proc->flags &= ~PROC_SLEEPING;
+ return 0;
+}
+
+/*
* MI proc init code
*/
int
@@ -273,10 +310,21 @@ proc_add_range(struct proc *procp, vaddr_t va, paddr_t pa, size_t len)
int
proc_kill(struct proc *procp, int status)
{
+ struct proc *self = proc_self();
+
if (procp == NULL) {
return -EINVAL;
}
+ /*
+ * Try to wake up our parent if they are sleeping
+ * at all.
+ */
+ if (self->pid == procp->pid) {
+ if (self->parent != NULL)
+ proc_wake(self->parent);
+ }
+
procp->flags |= PROC_EXITING;
proc_clear_ranges(procp);
TAILQ_REMOVE(&procq, procp, lup_link);
@@ -337,6 +385,7 @@ proc_spawn(const char *path, struct penv_blk *envbp)
}
proc->envblk = envbp;
+ proc->parent = proc_self();
md_set_ip(proc, elf.entrypoint);
sched_enq(&core->scq, proc);
diff --git a/src/sys/os/os_sched.c b/src/sys/os/os_sched.c
index d5824aa..e636f0a 100644
--- a/src/sys/os/os_sched.c
+++ b/src/sys/os/os_sched.c
@@ -111,9 +111,28 @@ sched_deq(struct sched_queue *q, struct proc **procp)
}
spinlock_acquire(&q->lock);
- proc = TAILQ_FIRST(&q->q);
- TAILQ_REMOVE(&q->q, proc, link);
+ if ((proc = TAILQ_FIRST(&q->q)) == NULL) {
+ spinlock_release(&q->lock);
+ return -EAGAIN;
+ }
+
+ /* Find a process that is not sleeping */
+ while (proc != NULL) {
+ if (ISSET(proc->flags, PROC_SLEEPING)) {
+ proc = TAILQ_NEXT(proc, link);
+ continue;
+ }
+
+ break;
+ }
+
+ /* Is there anything? */
+ if (proc == NULL) {
+ spinlock_release(&q->lock);
+ return -EAGAIN;
+ }
+ TAILQ_REMOVE(&q->q, proc, link);
*procp = proc;
--q->nproc;
spinlock_release(&q->lock);