summaryrefslogtreecommitdiffstats
path: root/task
diff options
context:
space:
mode:
authorJon Santmyer <jon@jonsantmyer.com>2024-05-22 13:00:41 -0400
committerJon Santmyer <jon@jonsantmyer.com>2024-05-22 13:00:41 -0400
commitace65b453151845bc361f21f3e5b651c35f9f126 (patch)
tree262ebd29b0ca1d8584f0b6f1efa7a00d9f4f3e43 /task
parentf004c1ade8d617a82cea2fe249434cccb47a2358 (diff)
downloadjove-kernel-ace65b453151845bc361f21f3e5b651c35f9f126.tar.gz
jove-kernel-ace65b453151845bc361f21f3e5b651c35f9f126.tar.bz2
jove-kernel-ace65b453151845bc361f21f3e5b651c35f9f126.zip
massive refactor for mp and organizationHEADmaster
Diffstat (limited to 'task')
-rw-r--r--task/exrtab.c19
-rw-r--r--task/init.c22
-rw-r--r--task/stack.c14
-rw-r--r--task/tasking.c45
-rw-r--r--task/umode_vma.c37
5 files changed, 137 insertions, 0 deletions
diff --git a/task/exrtab.c b/task/exrtab.c
new file mode 100644
index 0000000..82b9e29
--- /dev/null
+++ b/task/exrtab.c
@@ -0,0 +1,19 @@
+#include "arch/processor.h"
+#include "assert.h"
+
+void
+_exrtab_push(void *v)
+{
+ processor_t *proc = processor_current();
+ proc->exrtab[proc->ert_i++] = (uintptr_t)v;
+}
+
+void*
+_exrtab_pop(void)
+{
+ processor_t *proc = processor_current();
+
+ assert(proc->ert_i != 0);
+ uintptr_t r = proc->exrtab[--proc->ert_i];
+ return (void*)r;
+}
diff --git a/task/init.c b/task/init.c
new file mode 100644
index 0000000..c4c8bad
--- /dev/null
+++ b/task/init.c
@@ -0,0 +1,22 @@
+#include "commandline.h"
+#include "jove.h"
+#include "initrd.h"
+#include "tasking.h"
+#include "arch/elf.h"
+
+__attribute__((noreturn)) void
+kinit(void)
+{
+ const char *init_filepath = cmdline_get("init");
+ if(init_filepath == NULL)
+ kpanic("init filepath not found in kernel commandline\n");
+
+ initrd_file_t *init_file = ird_getfile(init_filepath);
+ if(init_file == NULL)
+ kpanic("init file not found in initrd [filepath=\"%s\"]\n", init_filepath);
+
+ uintptr_t ip = elf_load(init_file->data, init_file->size);
+
+ kexec((void*)ip, 0, NULL, 0, NULL);
+ kpanic("Reached end of kinit\n");
+}
diff --git a/task/stack.c b/task/stack.c
new file mode 100644
index 0000000..396ab3e
--- /dev/null
+++ b/task/stack.c
@@ -0,0 +1,14 @@
+#include "tasking.h"
+#include "string.h"
+#include "assert.h"
+
+void
+tcb_stack_push(tcb_t *tcb, void *data, size_t len)
+{
+ assert(tcb != NULL);
+ void *spo = (void*)(tcb->ksp - len);
+ assert(spo >= tcb->stack);
+
+ memcpy(spo, data, len);
+ tcb->ksp = (uintptr_t)spo;
+}
diff --git a/task/tasking.c b/task/tasking.c
new file mode 100644
index 0000000..ee57a13
--- /dev/null
+++ b/task/tasking.c
@@ -0,0 +1,45 @@
+#include "tasking.h"
+#include "slab.h"
+#include "klib/rbtree.h"
+#include "arch/processor.h"
+
+rbtree_t s_tcbs;
+tcbid_t s_tcb_nextid = 0;
+slab_cache_t s_kstack_slab;
+
+void
+tasking_setup(void)
+{
+ rbtree_new(&s_tcbs, tcb_t);
+ slabcache_new(&s_kstack_slab, "kernel stack", KERNEL_STACKW);
+
+ extern void kernel_stage2(void);
+ tcb_t *init_tcb = tcb_new((void*)kernel_stage2, pd_current());
+
+ tcb_switch(init_tcb);
+}
+
+tcb_t*
+tcb_new(void *ip, page_directory_t *pd)
+{
+ void *stack = slab_alloc(&s_kstack_slab);
+ tcb_t *tcb = rbtree_reserve(&s_tcbs, s_tcb_nextid);
+ *tcb = (tcb_t) {
+ .id = s_tcb_nextid++,
+ .stack = stack,
+ .ksp = ((uintptr_t)stack) + (KERNEL_STACKW - 1),
+ .pd = pd,
+ .state = TCB_RUNNING
+ };
+
+ tcb_prepare(tcb, ip);
+ return tcb;
+}
+
+void
+tcb_kill(tcb_t *tcb, int code)
+{
+ if(tcb->id == 0) kpanic("init thread killed!\n");
+ tcb->state = TCB_DEAD;
+ tcb->exit_code = code;
+}
diff --git a/task/umode_vma.c b/task/umode_vma.c
new file mode 100644
index 0000000..880e047
--- /dev/null
+++ b/task/umode_vma.c
@@ -0,0 +1,37 @@
+#include "umode_vma.h"
+#include "arch/processor.h"
+#include "api/errno.h"
+#include "print.h"
+
+__attribute__((naked))
+int
+_umode_vma_fail(void)
+{
+ __asm__ volatile(" \
+ movq %%rbp, %%rsp; \
+ popq %%rbp; \
+ mov $1, %%eax; \
+ retq":::);
+}
+
+int
+user_vma_read(void *dest, const void *src, size_t w)
+{
+ if((uintptr_t)src > USERLAND_MEMORY_LIMIT) return -EACCESS;
+
+ _exrtab_push((void*)_umode_vma_fail);
+ for(size_t i = 0; i < w; i++) ((char*)dest)[i] = ((const char*)src)[i];
+ _exrtab_pop();
+ return 0;
+}
+
+int
+user_vma_write(void *dest, const void *src, size_t w)
+{
+ if((uintptr_t)dest > USERLAND_MEMORY_LIMIT) return -EACCESS;
+
+ _exrtab_push((void*)_umode_vma_fail);
+ for(size_t i = 0; i < w; i++) ((char*)dest)[i] = ((const char*)src)[i];
+ _exrtab_pop();
+ return 0;
+}