]> git.corax.cc Git - corax/commitdiff
Implement functions to spawn and run processes
authorMatthias Kruk <m@m10k.eu>
Tue, 24 Sep 2019 08:42:12 +0000 (17:42 +0900)
committerMatthias Kruk <m@m10k.eu>
Tue, 24 Sep 2019 08:42:27 +0000 (17:42 +0900)
kernel/arch/Makefile
kernel/arch/process.c [new file with mode: 0644]
kernel/include/arch.h

index e7e4cceed87b98fc69b0ce7310ec5dafb317dda7..8097994f4ef10cc7752e42c964aeb356256f3c10 100644 (file)
@@ -1,5 +1,5 @@
 OBJECTS = boot.o debug.o init.o cpu.o cpu32.o entry.o interrupt.o paging.o apic.o i8259.o task.o \
-                 heap.o
+                 heap.o process.o
 OUTPUT = arch.o
 LDFLAGS += -r
 INCLUDES = -I../../include -I../include -I../..
diff --git a/kernel/arch/process.c b/kernel/arch/process.c
new file mode 100644 (file)
index 0000000..264894b
--- /dev/null
@@ -0,0 +1,115 @@
+#include <corax/types.h>
+#include <corax/errno.h>
+#include <corax/heap.h>
+#include <arch.h>
+#include <debug.h>
+#include "paging.h"
+#include "cpu.h"
+
+struct process {
+       struct pagedir *p_pgdir;
+       struct task *p_tasks;
+};
+
+void foo(void)
+{
+       dbg_printf("Running...\n");
+
+       for(;;) {
+               asm volatile("hlt");
+       }
+
+       return;
+}
+
+extern int _pg_dir_vpxlate(pg_dir_t*, u32_t, u32_t*);
+
+int process_create(process_t **dst)
+{
+       int ret_val;
+       process_t *proc;
+
+       ret_val = -ENOMEM;
+       proc = kmalloc(sizeof(*proc));
+
+       if(proc) {
+               void *pdbr;
+               void *kstack;
+               void *ustack;
+
+               ret_val = pg_dir_create(&(proc->p_pgdir));
+
+               if(ret_val < 0) {
+                       goto cleanup;
+               }
+
+               pdbr = proc->p_pgdir->pd_base;
+               kstack = pg_dir_get_kstack(proc->p_pgdir);
+               ustack = pg_dir_get_ustack(proc->p_pgdir);
+
+               proc->p_tasks = kmalloc(sizeof(*(proc->p_tasks)));
+
+               if(!proc->p_tasks) {
+                       ret_val = -ENOMEM;
+                       goto cleanup;
+               }
+
+               task_prepare(proc->p_tasks, (u32_t)pdbr, (u32_t)foo, (u32_t)kstack, (u32_t)ustack + PAGE_SIZE, 0);
+
+               proc->p_tasks->t_sp = (u32_t)(CONFIG_KERNEL_STACK_BASE + ((u32_t)proc->p_tasks->t_sp - (u32_t)kstack));
+               dbg_printf("ESP0 = 0x%08x\n", proc->p_tasks->t_sp);
+       }
+
+cleanup:
+       if(ret_val < 0) {
+               if(proc) {
+                       if(proc->p_pgdir) {
+                               /* cleanup pagedir */
+                               /* pg_dir_free(proc->p_pgdir): */
+                               proc->p_pgdir = NULL;
+                       }
+
+                       if(proc->p_tasks) {
+                               /* cleanup task */
+
+                               kfree(proc->p_tasks);
+                               proc->p_tasks = NULL;
+                       }
+
+                       kfree(proc);
+                       proc = NULL;
+               }
+       }
+
+       *dst = proc;
+       return(ret_val);
+}
+
+int process_switch(process_t *proc)
+{
+//     struct stack_frame *sfrm;
+       int ret_val;
+/*
+       sfrm = (void*)proc->p_tasks->t_sp;
+
+       dbg_printf("SS  = %08x\n"
+                          "ESP = %08x\n"
+                          "EFL = %08x\n"
+                          "CS  = %08x\n"
+                          "EIP = %08x\n"
+                          "DS  = %08x\n",
+                          sfrm->ss,
+                          sfrm->prevesp,
+                          sfrm->eflags,
+                          sfrm->cs,
+                          sfrm->eip,
+                          sfrm->ds);
+
+       for(;;) {
+               asm volatile("hlt");
+       }
+*/
+       ret_val = task_switch(proc->p_tasks);
+
+       return(ret_val);
+}
index 787cdf898f5aaa4d43eaf795c6c2c9aad0d27edb..43064fd566178ae3f45c3d3fe9a8db6c050bee76 100644 (file)
@@ -21,6 +21,8 @@
 
 #include <corax/types.h>
 
+typedef struct process process_t;
+
 struct task {
        u32_t t_sp;
        u32_t t_pgdir;
@@ -33,7 +35,9 @@ u64_t   cpu_timestamp(void);
 
 u32_t   cpu_set_pstate(int pstate);
 
-int     task_prepare(struct task*, u32_t cr3, u32_t eip, u32_t esp, u32_t priv);
+int     process_create(process_t**);
+
+int     task_prepare(struct task*, u32_t cr3, u32_t eip, u32_t esp0, u32_t esp, u32_t priv);
 int     task_switch(struct task*);
 
 #endif /* __ARCH_H */