From fdbc5adf83741d7e3af0c3f0ffdb59bdf42493b6 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Thu, 4 Jul 2024 22:52:47 +0300 Subject: generalize orphants + Allow threads to make themselves become orphants --- include/arch/tcb.h | 8 ++++++-- include/kmi/orphanage.h | 9 ++++++++- include/kmi/syscalls.h | 3 +++ include/kmi/tcb.h | 20 ++++++++------------ include/kmi/uapi.h | 15 +++++++++++++++ 5 files changed, 40 insertions(+), 15 deletions(-) (limited to 'include') diff --git a/include/arch/tcb.h b/include/arch/tcb.h index 115f49d..22d3b59 100644 --- a/include/arch/tcb.h +++ b/include/arch/tcb.h @@ -40,12 +40,16 @@ void setup_rpc_stack(struct tcb *t); void destroy_rpc_stack(struct tcb *t); /** - * Maximum size of one individual RPC stack instance. - * * @return Max size of one individual RPC stack instance. */ size_t max_rpc_size(); +/** + * @param addr Address of current top of stack, generally from `ctx->rpc_stack`. + * @return \ref true if next rpc return would be into the root process. + */ +bool rpc_stack_empty(pm_t addr); + /** * Current highest address in RPC stack. Allowed to be inaccurate to one base page. * diff --git a/include/kmi/orphanage.h b/include/kmi/orphanage.h index 530d10c..09e36b3 100644 --- a/include/kmi/orphanage.h +++ b/include/kmi/orphanage.h @@ -22,6 +22,13 @@ */ bool orphan(struct tcb *t); +/** + * Mark \p t orphaned. + * + * @param t Thread to orphanize. + */ +void orphanize(struct tcb *t); + /** * Assign \p t to the init process and jump to it. * \p t must be an orphan! @@ -30,6 +37,6 @@ bool orphan(struct tcb *t); * * @param t Orphaned thread. */ -__noreturn void orphanize(struct tcb *t); +__noreturn void unorphanize(struct tcb *t); #endif /* KMI_OPRHANAGE_H */ diff --git a/include/kmi/syscalls.h b/include/kmi/syscalls.h index 3619469..93d0ac9 100644 --- a/include/kmi/syscalls.h +++ b/include/kmi/syscalls.h @@ -157,6 +157,9 @@ enum sys_code { /** Request a thread exits. */ SYS_EXIT, + /** Detach a thread from its root process, becoming an orphant. */ + SYS_DETACH, + /** @} */ SYS_NUM, diff --git a/include/kmi/tcb.h b/include/kmi/tcb.h index fac9df7..df161b3 100644 --- a/include/kmi/tcb.h +++ b/include/kmi/tcb.h @@ -72,16 +72,13 @@ struct tcb_ctx { struct vmem *vmem; }; -/** Enum for notification states. */ -enum tcb_notify { - /** Thread is free to be notified. */ - NOTIFY_WAITING = 0, +/** State of thread. */ +enum tcb_state { + /** Thread is a zombie. */ + TCB_ZOMBIE = (1 << 0), - /** Thread has notifcations queued. */ - NOTIFY_QUEUED, - - /** Thread is running notification handler. */ - NOTIFY_RUNNING, + /** Thread is an orphan. */ + TCB_ORPHAN = (1 << 1), }; /** Thread control block. Main way to handle threads. */ @@ -180,9 +177,8 @@ struct tcb { /** Queue that connects together threads waiting for an ipi */ struct queue_head ipi_queue; - /** Whether thread is dead. If thread is process, then corresponds to - * whole process. */ - bool dead; + /** Current state of thread. */ + enum tcb_state state; }; /** diff --git a/include/kmi/uapi.h b/include/kmi/uapi.h index 684f0da..76a759f 100644 --- a/include/kmi/uapi.h +++ b/include/kmi/uapi.h @@ -841,6 +841,21 @@ SYSCALL_DECLARE1(irq_req, id); */ SYSCALL_DECLARE1(exit, tid); +/** + * Request that a thread becomes orphant, i.e. eventually attached to the init + * process. Can be used to stop threads within a process by sending an + * appropriate signal to the troublesome thread which then detaches itself from + * its root process. + * + * @param t Current tcb. + * @param a Unused. + * @param b Unused. + * @param c Unused. + * @param d Unused. + * @param e Unused. + */ +SYSCALL_DECLARE0(detach); + /** @} */ /** -- cgit v1.3