diff --git a/arch/x86/mmuContext.c b/arch/x86/mmuContext.c index 164bb71..9a8438f 100644 --- a/arch/x86/mmuContext.c +++ b/arch/x86/mmuContext.c @@ -1,10 +1,11 @@ -#include "mmuContext.h" #include "alloc.h" #include "allocArea.h" #include "errno.h" #include "irq.h" #include "klibc.h" #include "list.h" +#include "mem.h" +#include "mmuContext.h" #include "paging.h" #include "stdarg.h" #include "types.h" @@ -29,6 +30,8 @@ int mmuContextSetup() struct mmu_context *initialCtx; int ret = 0; + allocBookSlab(sizeof(struct mmu_context), PAGE_SIZE * 3, 0, 0); + initialCtx = malloc(sizeof(struct mmu_context)); if (initialCtx == NULL) diff --git a/core/process.c b/core/process.c new file mode 100644 index 0000000..182c33b --- /dev/null +++ b/core/process.c @@ -0,0 +1,163 @@ +#include "process.h" +#include "alloc.h" +#include "irq.h" +#include "klibc.h" +#include "list.h" +#include "mmuContext.h" + +struct process { + char name[PROCESS_NAME_MAX_LENGTH]; + int ref; + int pid; + struct mmu_context *context; + struct thread *thList; + + struct process *prev, *next; +}; + +static struct process *processList; +static int nextPid; + +int processSetup() +{ + list_init(processList); + allocBookSlab(sizeof(struct process), PAGE_SIZE * 3, 0, 0); + nextPid = 0; + + return 0; +} + +struct process *processCreate(char *name) +{ + uint32_t flags; + + struct process *new = (struct process *)malloc(sizeof(struct process)); + if (new == NULL) + return NULL; + + new->context = mmuContextCreate(); + if (new->context == NULL) { + free(new); + + return NULL; + } + + strzcpy(new->name, name, PROCESS_NAME_MAX_LENGTH); + new->ref = 1; + + disable_IRQs(flags); + new->pid = nextPid++; + list_add_tail(processList, new); + restore_IRQs(flags); + + return new; +} + +void processListPrint() +{ + struct process *proc; + int nbProcs; + struct thread *cur = getCurrentThread(); + + printf("PID NAME NBTHREAD REF\n"); + list_foreach(processList, proc, nbProcs) + { + struct thread *th; + int nbTh; + + printf("%d %s %d %d\n", proc->pid, proc->name, processCountThread(proc), proc->ref); + list_foreach_named(proc->thList, th, nbTh, prevInProcess, nextInProcess) + { + if (th == cur) { + printf(" th: 0x%x Current\n", th); + } else { + printf(" th: 0x%x in 0x%x\n", th, cpu_context_get_PC(th->cpuState)); + } + } + } +} + +int processCountThread(struct process *proc) +{ + int count; + struct thread *th; + + list_foreach_named(proc->thList, th, count, prevInProcess, nextInProcess) {} + + return count; +} + +int processRef(struct process *proc) +{ + uint32_t flags; + // ref == 0 -> delete + assert(proc->ref > 0); + + disable_IRQs(flags); + proc->ref++; + restore_IRQs(flags); + + return 0; +} + +int processUnref(struct process *proc) +{ + uint32_t flags; + + assert(proc->ref > 0); + + disable_IRQs(flags); + + proc->ref--; + if (proc->ref > 0) { + restore_IRQs(flags); + return -EBUSY; + } + list_delete(processList, proc); + restore_IRQs(flags); + + mmuContextUnref(proc->context); + free(proc); + + return 0; +} + +int processAddThread(struct process *proc, struct thread *th) +{ + uint32_t flags; + + assert(proc->ref > 0); + + th->process = proc; + + disable_IRQs(flags); + processRef(proc); + list_add_tail_named(proc->thList, th, prevInProcess, nextInProcess); + + restore_IRQs(flags); + + return 0; +} + +int processRemoveThread(struct thread *th) +{ + uint32_t flags; + struct process *proc; + + disable_IRQs(flags); + proc = th->process; + + list_delete_named(proc->thList, th, prevInProcess, nextInProcess); + restore_IRQs(flags); + processUnref(proc); + + return 0; +} + +int processSetName(struct process *proc, char *name) +{ + assert(name != NULL); + strzcpy(proc->name, name, PROCESS_NAME_MAX_LENGTH); + + return 0; +} diff --git a/core/process.h b/core/process.h new file mode 100644 index 0000000..4c6c9d2 --- /dev/null +++ b/core/process.h @@ -0,0 +1,15 @@ +#pragma once +#include "thread.h" + +#define PROCESS_NAME_MAX_LENGTH 32 + +struct process; + +int processSetup(); +struct process *processCreate(char *name); +int processCountThread(struct process *proc); +void processListPrint(); +int processRef(struct process *proc); +int processUnref(struct process *proc); +int processSetName(struct process *proc, char *name); +int processAddThread(struct process *proc, struct thread *th); diff --git a/core/thread.h b/core/thread.h index 2bbb065..a8d6cbd 100644 --- a/core/thread.h +++ b/core/thread.h @@ -1,7 +1,9 @@ #pragma once +struct thread; #include "cpu_context.h" #include "mem.h" +#include "process.h" #define THREAD_NAME_MAX_LENGTH 32 #define THREAD_DEFAULT_STACK_SIZE PAGE_SIZE @@ -21,12 +23,15 @@ struct thread { thread_state state; vaddr_t stackAddr; size_t stackSize; + unsigned long jiffiesSleeping; int sleepHaveTimeouted; - struct thread *next; - struct thread *prev; - struct thread*timeNext; - struct thread *timePrev; + + struct thread *next, *prev; + struct thread *timeNext, *timePrev; + // For User thread only + struct thread *nextInProcess, *prevInProcess; + struct process *process; }; int threadSetup(vaddr_t mainStack, size_t mainStackSize);