aboutsummaryrefslogtreecommitdiff
path: root/src/features
diff options
context:
space:
mode:
authorMarvin Borner2020-08-01 18:37:15 +0200
committerMarvin Borner2020-08-01 18:37:15 +0200
commit1686173757af4e453e7a32d152ec4bd20d789652 (patch)
tree539d90855fa59c85c043976fcd9cd3075ebc8d8d /src/features
parent928bf3f29d7a8b2163a3c1d5c15554d5b42c606b (diff)
Started multitasking
Diffstat (limited to 'src/features')
-rw-r--r--src/features/load.c13
-rw-r--r--src/features/proc.c64
2 files changed, 71 insertions, 6 deletions
diff --git a/src/features/load.c b/src/features/load.c
index 958acd7..2cfa546 100644
--- a/src/features/load.c
+++ b/src/features/load.c
@@ -2,15 +2,16 @@
#include <def.h>
#include <fs.h>
-#include <load.h>
+#include <mem.h>
#include <print.h>
+#include <proc.h>
-void bin_load(char *path)
+void bin_load(char *path, struct proc *proc)
{
char *data = read_file(path);
+ u32 stack = (u32)malloc(0x1000) + 0x1000;
- void (*entry)();
- *(void **)(&entry) = data;
-
- entry();
+ proc->regs.ebp = stack;
+ proc->regs.esp = stack;
+ proc->regs.eip = (u32)&data;
}
diff --git a/src/features/proc.c b/src/features/proc.c
new file mode 100644
index 0000000..92e17f1
--- /dev/null
+++ b/src/features/proc.c
@@ -0,0 +1,64 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#include <interrupts.h>
+#include <load.h>
+#include <mem.h>
+#include <print.h>
+#include <proc.h>
+#include <timer.h>
+
+u32 pid = 0;
+struct proc *root;
+struct proc *current;
+struct proc *last;
+
+void scheduler(struct regs *regs)
+{
+ printf(".");
+ memcpy(&current->regs, regs, sizeof(struct regs));
+
+ timer_handler();
+
+ if (!current->next)
+ current = root;
+ else
+ current = current->next;
+
+ while (current->state == PROC_ASLEEP)
+ if (!current->next)
+ current = root;
+ else
+ current = current->next;
+
+ memcpy(regs, &current->regs, sizeof(struct regs));
+}
+
+void proc_attach(struct proc *proc)
+{
+ if (!last->next) {
+ last->next = proc;
+ } else {
+ struct proc *save = last;
+ while (save->next)
+ save = save->next;
+ save->next = proc;
+ }
+}
+
+struct proc *proc_make()
+{
+ struct proc *proc = (struct proc *)malloc(sizeof(*proc));
+ proc->pid = pid++;
+ proc->state = PROC_RUNNING;
+ if (current)
+ proc_attach(proc);
+ last = proc;
+ return proc;
+}
+
+void proc_init()
+{
+ current = root = proc_make();
+ bin_load("/root", root);
+ irq_install_handler(0, scheduler);
+}