aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2024-07-04 19:25:12 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2024-07-04 19:25:12 +0300
commit07c2376702fb3d508d6ffad6b0ce93b83972ad6e (patch)
tree9ce46b000f8411fa86118645063b4df05d018dbf /include
parent66e7f184a925247bac51aae02d01483faa5454fc (diff)
downloadkmi-07c2376702fb3d508d6ffad6b0ce93b83972ad6e.tar.gz
kmi-07c2376702fb3d508d6ffad6b0ce93b83972ad6e.zip
add zombie and orphan threads
+ Should write this down somewhere but the idea is that when a process gets killed, it frees all the memory it can, making all threads within that process orphans. Orphaned threads are assigned to the init process, which will generally call exit() on each one. Zombie threads are threads that own some bit of shared data, and whose reference count is above zero. They may not be swapped to or called, even though they take up space in thread map and reserve their thread ID.
Diffstat (limited to 'include')
-rw-r--r--include/arch/tcb.h7
-rw-r--r--include/kmi/ipi.h6
-rw-r--r--include/kmi/orphanage.h35
-rw-r--r--include/kmi/queue.h20
-rw-r--r--include/kmi/syscalls.h12
-rw-r--r--include/kmi/tcb.h21
-rw-r--r--include/kmi/uapi.h27
7 files changed, 108 insertions, 20 deletions
diff --git a/include/arch/tcb.h b/include/arch/tcb.h
index 4e82ab1..115f49d 100644
--- a/include/arch/tcb.h
+++ b/include/arch/tcb.h
@@ -33,6 +33,13 @@ void tcb_assign(struct tcb *t);
void setup_rpc_stack(struct tcb *t);
/**
+ * Free memory backing rpc stack.
+ *
+ * @param t Thred whose RPC stack should be destroyed.
+ */
+void destroy_rpc_stack(struct tcb *t);
+
+/**
* Maximum size of one individual RPC stack instance.
*
* @return Max size of one individual RPC stack instance.
diff --git a/include/kmi/ipi.h b/include/kmi/ipi.h
index 7a0760a..9c2963f 100644
--- a/include/kmi/ipi.h
+++ b/include/kmi/ipi.h
@@ -21,6 +21,12 @@
*/
void send_ipi(struct tcb *t);
+/**
+ * Remove thread from IPI queue if it is on it.
+ * @param t Thread to unqueue.
+ */
+void unqueue_ipi(struct tcb *t);
+
/** Handle IPI. */
void handle_ipi();
diff --git a/include/kmi/orphanage.h b/include/kmi/orphanage.h
new file mode 100644
index 0000000..530d10c
--- /dev/null
+++ b/include/kmi/orphanage.h
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: copyleft-next-0.3.1 */
+/* Copyright 2024, Kim Kuparinen < kimi.h.kuparinen@gmail.com > */
+
+#ifndef KMI_ORPHANAGE_H
+#define KMI_OPRHANAGE_H
+
+/**
+ * @file orphanage.h
+ *
+ * Stuff related to orphaned threads.
+ *
+ * Oprhaned threads are threads whose owning process has been destroyed.
+ * They are always assigned to the init process, which will generally destroy
+ * them whenever it gets a chance.
+ */
+
+#include <kmi/tcb.h>
+
+/**
+ * @param t Thread suspected of being an orphan.
+ * @return \ref true if \p t is an orphan, \ref false otherwise.
+ */
+bool orphan(struct tcb *t);
+
+/**
+ * Assign \p t to the init process and jump to it.
+ * \p t must be an orphan!
+ * \p t must be in the process of swapping to its root process, either by
+ * returning from an rpc or being swapped to.
+ *
+ * @param t Orphaned thread.
+ */
+__noreturn void orphanize(struct tcb *t);
+
+#endif /* KMI_OPRHANAGE_H */
diff --git a/include/kmi/queue.h b/include/kmi/queue.h
index 534318a..c00bf6f 100644
--- a/include/kmi/queue.h
+++ b/include/kmi/queue.h
@@ -53,4 +53,24 @@ static inline struct queue_head *queue_pop(struct queue_head *head)
return container_of(e, struct queue_head, l);
}
+/**
+ * @param head Entry to check for queueing status.
+ * @return \ref true if in queue, \ref false otherwise.
+ */
+static inline bool in_queue(struct queue_head *head)
+{
+ return in_list(&head->l);
+}
+
+/**
+ * Remove entry from queue.
+ *
+ * @param head Entry to remove.
+ */
+static inline void queue_del(struct queue_head *head)
+{
+ if (in_queue(head))
+ list_del(&head->l);
+}
+
#endif /* QUEUE_H */
diff --git a/include/kmi/syscalls.h b/include/kmi/syscalls.h
index c78ddae..3619469 100644
--- a/include/kmi/syscalls.h
+++ b/include/kmi/syscalls.h
@@ -99,8 +99,8 @@ enum sys_code {
/** IPC return without visible side effects. */
SYS_IPC_GHOST,
- /** IPC notify thread, essentially interrupt or signal. */
- SYS_IPC_NOTIFY,
+ /** Notify thread, essentially interrupt or signal. */
+ SYS_NOTIFY,
/** @} */
/** @name Process management. */
@@ -154,6 +154,9 @@ enum sys_code {
/** Request a notification handler. */
SYS_REQ_NOTIFICATION,
+ /** Request a thread exits. */
+ SYS_EXIT,
+
/** @} */
SYS_NUM,
@@ -165,11 +168,14 @@ enum sys_user {
/** Thread has received one or several notifications, please handle
* them. */
SYS_USER_NOTIFY,
+
+ /** Thread has been orphaned. */
+ SYS_USER_ORPHANED,
};
/** Which notifications have arrived. */
enum notify_flag {
- /** A signal (\ref ipc_notify()). */
+ /** A signal (\ref sys_notify()). */
NOTIFY_SIGNAL = (1 << 0),
/** A timer has expired. */
diff --git a/include/kmi/tcb.h b/include/kmi/tcb.h
index b52f5e3..fac9df7 100644
--- a/include/kmi/tcb.h
+++ b/include/kmi/tcb.h
@@ -98,7 +98,7 @@ struct tcb {
/** Arch-specific data. */
struct arch_tcbd arch;
- /** Memory mapping data. */
+ /** Memory mapping data. Only relevant in root thread. */
struct mem_region_root sp_r;
/** Address of callback function in servers. */
@@ -128,16 +128,6 @@ struct tcb {
struct tcb_ctx rpc;
/**
- * RPC server context of thread. When a thread attaches itself to this
- * process, its \ref rpc member is added to the list maintained in this
- * variable. This allows the original thread to do rpc calls without
- * messing up other threads' rpc status.
- *
- * I think, more testing required.
- */
- struct tcb_ctx server;
-
- /**
* Effective process ID.
*
* This is the ID on which globally visible stuff should occur, such as
@@ -335,6 +325,15 @@ void set_return(struct tcb *t, vm_t r);
bool running(struct tcb *t);
/**
+ * Check whether \p t is actually dead but just kept around for resource
+ * management.
+ *
+ * @param t \ref tcb to check.
+ * @return \c true if \p t is a zombie, \c false otherwise.
+ */
+bool zombie(struct tcb *t);
+
+/**
* Add a reference to a process.
* Instead of lists of threads that belong to a process, we give the process'
* owning thread a reference counter. When a process is killed, a 'dead' bit is
diff --git a/include/kmi/uapi.h b/include/kmi/uapi.h
index 00fb45a..684f0da 100644
--- a/include/kmi/uapi.h
+++ b/include/kmi/uapi.h
@@ -584,7 +584,7 @@ SYSCALL_DECLARE0(ipc_ghost);
*
* Returns \ref OK and 0.
*/
-SYSCALL_DECLARE1(ipc_notify, tid);
+SYSCALL_DECLARE1(notify, tid);
/** @} */
/** @name Process handling syscalls. */
@@ -657,18 +657,17 @@ SYSCALL_DECLARE2(exec, bin, interp);
SYSCALL_DECLARE2(spawn, bin, interp);
/**
- * Kill syscall.
+ * Kill syscall. Requests that all memory in process \p pid is freed and all
+ * threads are orphaned. If \p pid is not a process, nothing is done.
*
* @param t Current tcb.
- * @param tid Thread ID to kill. 0 if self.
+ * @param pid Thread ID to kill. 0 if self.
* @param b Unused.
* @param c Unused.
* @param d Unused.
* @param e Unused.
- *
- * Returns \ref OK if not called on itself, otherwise doesn't return.
*/
-SYSCALL_DECLARE1(kill, tid);
+SYSCALL_DECLARE1(kill, pid);
/**
* Swap syscall.
@@ -826,6 +825,22 @@ SYSCALL_DECLARE0(sleep);
*/
SYSCALL_DECLARE1(irq_req, id);
+/**
+ * Request that a thread exits, i.e. removes itself from the thread list and
+ * frees all kernel data associated with thread.
+ * Note that similarly to \ref kill(), some shared resources may cause the
+ * thread to stay around until their reference counts go to zero.
+ *
+ * @param t Current tcb.
+ * @param tid Thread to swap to once the current thread no longer exists. 0 for
+ * sleeping.
+ * @param b Unused.
+ * @param c Unused.
+ * @param d Unused.
+ * @param e Unused.
+ */
+SYSCALL_DECLARE1(exit, tid);
+
/** @} */
/**