aboutsummaryrefslogtreecommitdiff
path: root/kernel/include/proc/context.h
diff options
context:
space:
mode:
authornthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
committernthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
commitc63f340d90800895f007de64b7d2d14624263331 (patch)
tree2c0849fa597dd6da831c8707b6f2603403778d7b /kernel/include/proc/context.h
Created student weenix repository
Diffstat (limited to 'kernel/include/proc/context.h')
-rw-r--r--kernel/include/proc/context.h62
1 files changed, 62 insertions, 0 deletions
diff --git a/kernel/include/proc/context.h b/kernel/include/proc/context.h
new file mode 100644
index 0000000..63c692e
--- /dev/null
+++ b/kernel/include/proc/context.h
@@ -0,0 +1,62 @@
+#pragma once
+
+#include "types.h"
+
+#include "mm/pagetable.h"
+
+/*
+ * The function pointer to be implemented by functions which are entry
+ * points for new threads.
+ */
+typedef void *(*context_func_t)(long, void *);
+
+typedef struct context
+{
+ uintptr_t c_rip; /* instruction pointer (RIP) */
+ uintptr_t c_rsp; /* stack pointer (RSP) */
+ uintptr_t c_rbp; /* frame pointer (RBP) */
+
+ pml4_t
+ *c_pml4; /* pointer to the top level page table (PML4) for this proc.
+ It's the 'root' of the page table where virtual address -> physical address
+ lookup starts. */
+
+ uintptr_t c_kstack;
+ size_t c_kstacksz;
+} context_t;
+
+/**
+ * Initialize the given context such that when it begins execution it
+ * will execute func(arg1,arg2). A kernel stack and page directory
+ * exclusive to this context must also be provided.
+ *
+ * @param c the context to initialize
+ * @param func the function which will begin executing when this
+ * context is first made active
+ * @param arg1 the first argument to func
+ * @param arg2 the second argument to func
+ * @param kstack a pointer to the kernel stack this context will use
+ * @param kstacksz the size of the kernel stack
+ * @param pdptr the pagetable this context will use
+ */
+void context_setup(context_t *c, context_func_t func, long arg1, void *arg2,
+ void *kstack, size_t kstacksz, pml4_t *pml4);
+
+void context_setup_raw(context_t *c, void (*func)(), void *kstack,
+ size_t kstacksz, pml4_t *pml4);
+/**
+ * Makes the given context the one currently running on the CPU. Use
+ * this mainly for the initial context.
+ *
+ * @param c the context to make active
+ */
+void context_make_active(context_t *c);
+
+/**
+ * Save the current state of the machine into the old context, and begin
+ * executing the new context. Used primarily by the scheduler.
+ *
+ * @param oldc the context to switch from
+ * @param newc the context to switch to
+ */
+void context_switch(context_t *oldc, context_t *newc);