summaryrefslogtreecommitdiff
path: root/src/kernel
diff options
context:
space:
mode:
authordzwdz2021-07-26 19:39:57 +0200
committerdzwdz2021-07-26 19:39:57 +0200
commit3c8cc57cd1d7b04ab92dc80bc0e0c9a481d905f8 (patch)
tree481ae0421ed8d0046f1735ad143576ae4c8f21bd /src/kernel
parent2ae4e31b74efa29cc92554647d5c9dce077971c4 (diff)
exit() now switches to the first running process
i used a linked list because it's the simplest way to implement this
Diffstat (limited to 'src/kernel')
-rw-r--r--src/kernel/proc.c15
-rw-r--r--src/kernel/proc.h13
-rw-r--r--src/kernel/syscalls.c6
3 files changed, 32 insertions, 2 deletions
diff --git a/src/kernel/proc.c b/src/kernel/proc.c
index b165878..36c88b5 100644
--- a/src/kernel/proc.c
+++ b/src/kernel/proc.c
@@ -4,11 +4,16 @@
#include <kernel/util.h>
#include <stdint.h>
+struct process *process_first;
struct process *process_current;
struct process *process_new() {
struct process *proc = page_alloc(1); // TODO kmalloc
proc->pages = pagedir_new();
+ proc->state = PS_RUNNING;
+ proc->next = NULL;
+
+ process_first = proc;
// map the stack to the last page in memory
pagedir_map(proc->pages, (void*)~PAGE_MASK, page_alloc(1), true, true);
@@ -24,10 +29,11 @@ struct process *process_new() {
return proc;
}
-struct process *process_clone(const struct process *orig) {
+struct process *process_clone(struct process *orig) {
struct process *clone = page_alloc(1);
memcpy(clone, orig, sizeof(struct process));
clone->pages = pagedir_copy(orig->pages);
+ orig->next = clone;
return clone;
}
@@ -37,3 +43,10 @@ void process_switch(struct process *proc) {
pagedir_switch(proc->pages);
sysexit(proc->regs);
}
+
+struct process *process_find(enum process_state target) {
+ struct process *iter = process_first;
+ while (iter && (iter->state != target))
+ iter = iter->next;
+ return iter;
+}
diff --git a/src/kernel/proc.h b/src/kernel/proc.h
index 71880e6..6d1ed8c 100644
--- a/src/kernel/proc.h
+++ b/src/kernel/proc.h
@@ -1,14 +1,25 @@
#pragma once
#include <kernel/arch/generic.h>
+enum process_state {
+ PS_RUNNING,
+ PS_DEAD,
+};
+
struct process {
void *stack_top;
struct pagedir *pages;
struct registers regs;
+ enum process_state state;
+
+ struct process *next;
};
+extern struct process *process_first;
extern struct process *process_current;
struct process *process_new();
-struct process *process_clone(const struct process *orig);
+struct process *process_clone(struct process *orig);
_Noreturn void process_switch(struct process *proc);
+
+struct process *process_find(enum process_state);
diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c
index eff670e..3d35a4a 100644
--- a/src/kernel/syscalls.c
+++ b/src/kernel/syscalls.c
@@ -5,6 +5,12 @@
#include <stdint.h>
_Noreturn void sc_exit(const char *msg, size_t len) {
+ process_current->state = PS_DEAD;
+
+ process_current = process_find(PS_RUNNING);
+ if (process_current)
+ process_switch(process_current);
+
log_const("last process returned. ");
panic();
}