From 9c50a1df92bacf7376a6520fe1e073a4351d22a3 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Wed, 12 Aug 2026 14:59:34 -0400 Subject: [PATCH] kernel: add thread_wait, thread_block and mtx Signed-off-by: Michael Shaw --- amd64/arch/entry.S | 25 ++++++++++++++++++++++--- amd64/include/arch/proc.h | 3 +++ amd64/include/kernel/mtx.h | 10 ++++++++++ amd64/include/kernel/spinlock.h | 1 + amd64/kernel/spinlock.c | 12 ++++++++++++ include/kernel/proc.h | 1 + 6 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 amd64/include/kernel/mtx.h diff --git a/amd64/arch/entry.S b/amd64/arch/entry.S index de27da7..53fcde4 100644 --- a/amd64/arch/entry.S +++ b/amd64/arch/entry.S @@ -138,9 +138,28 @@ _timer_handler: .global sched_restore_state sched_restore_state: - mov rsp, [rdi + 8] /* load RSP from thread->kstack_ptr */ - pop_all - ret + mov rsp, [rdi + 8] /* load RSP from thread->kstack_ptr */ + pop_all + ret + +.global thread_wait +thread_wait: + push_all + mov rax, qword ptr gs:[24] /* cur_thread */ + mov [rax + 8], rsp /* save rsp to kstack_ptr */ + mov qword ptr [rax + 32], 1 /* state = PROC_STATE_WAITING */ + call sched_add_thread /* re-add to run queue */ + call schedule + /* not reached */ + +.global thread_block +thread_block: + push_all + mov rax, qword ptr gs:[24] /* cur_thread */ + mov [rax + 8], rsp /* save rsp to kstack_ptr */ + mov qword ptr [rax + 32], 3 /* state = PROC_STATE_BLOCKED */ + call schedule + /* not reached */ .global _syscall_enter _syscall_enter: diff --git a/amd64/include/arch/proc.h b/amd64/include/arch/proc.h index 54a352b..dbe4640 100644 --- a/amd64/include/arch/proc.h +++ b/amd64/include/arch/proc.h @@ -46,4 +46,7 @@ void enter_userspace_init(struct thread *thread, uaddr_t entry); struct pt_regs *current_pt_regs(); u32 cpu_id(); +void thread_wait(); +void thread_block(); + #endif /* _ARCH_PROC_H_ */ diff --git a/amd64/include/kernel/mtx.h b/amd64/include/kernel/mtx.h new file mode 100644 index 0000000..43adaa3 --- /dev/null +++ b/amd64/include/kernel/mtx.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _MTX_H_ +#define _MTX_H_ + +#include + +void mtx_lock(mtx_t *l); +void mtx_unlock(mtx_t *l); + +#endif /* _MTX_H_ */ diff --git a/amd64/include/kernel/spinlock.h b/amd64/include/kernel/spinlock.h index 06ad9c2..a41d463 100644 --- a/amd64/include/kernel/spinlock.h +++ b/amd64/include/kernel/spinlock.h @@ -8,6 +8,7 @@ #define CLEANUP_SLOCK CLEANUP(cleanup_spinlock) typedef u32 spinlock_t; +typedef spinlock_t mtx_t; void spinlock_acquire(spinlock_t *lock); void spinlock_release(spinlock_t *lock); diff --git a/amd64/kernel/spinlock.c b/amd64/kernel/spinlock.c index 809380b..e6f1e28 100644 --- a/amd64/kernel/spinlock.c +++ b/amd64/kernel/spinlock.c @@ -2,6 +2,7 @@ #include #include #include +#include static uint64_t spinlock_attempt_acquire(spinlock_t *lock) { @@ -24,3 +25,14 @@ void cleanup_spinlock(spinlock_t **lock_ptr) if (lock_ptr && *lock_ptr) spinlock_release(*lock_ptr); } + +void mtx_lock(mtx_t *l) +{ + while (spinlock_attempt_acquire(l)) + thread_wait(); +} + +void mtx_unlock(mtx_t *l) +{ + spinlock_release(l); +} diff --git a/include/kernel/proc.h b/include/kernel/proc.h index 17b50df..a3e285a 100644 --- a/include/kernel/proc.h +++ b/include/kernel/proc.h @@ -13,6 +13,7 @@ #define PROC_STATE_RUNNING 0 #define PROC_STATE_WAITING 1 #define PROC_STATE_DNS 2 /* do not schedule */ +#define PROC_STATE_BLOCKED 3 typedef u64 pid_t; typedef u64 tid_t;