From: Matthias Kruk Date: Tue, 24 Sep 2019 08:42:12 +0000 (+0900) Subject: Implement functions to spawn and run processes X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=1f783c3f448a95ade65b8bf0e3627ef123c99ce5;p=corax Implement functions to spawn and run processes --- diff --git a/kernel/arch/Makefile b/kernel/arch/Makefile index e7e4cce..8097994 100644 --- a/kernel/arch/Makefile +++ b/kernel/arch/Makefile @@ -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 index 0000000..264894b --- /dev/null +++ b/kernel/arch/process.c @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#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); +} diff --git a/kernel/include/arch.h b/kernel/include/arch.h index 787cdf8..43064fd 100644 --- a/kernel/include/arch.h +++ b/kernel/include/arch.h @@ -21,6 +21,8 @@ #include +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 */