diff --git a/src/kernel/boot/boot.asm b/src/kernel/boot/boot.asm index df975de..e9443dc 100644 --- a/src/kernel/boot/boot.asm +++ b/src/kernel/boot/boot.asm @@ -91,7 +91,7 @@ pop eax ret -earlyGDT: +earlyGDT: ; using this GDT before activating paging .start: dq 0 .code: diff --git a/src/kernel/boot/boot.asm b/src/kernel/boot/boot.asm index df975de..e9443dc 100644 --- a/src/kernel/boot/boot.asm +++ b/src/kernel/boot/boot.asm @@ -91,7 +91,7 @@ pop eax ret -earlyGDT: +earlyGDT: ; using this GDT before activating paging .start: dq 0 .code: diff --git a/src/kernel/include/memory.h b/src/kernel/include/memory.h index 2129d04..2bbb8fe 100644 --- a/src/kernel/include/memory.h +++ b/src/kernel/include/memory.h @@ -31,7 +31,6 @@ extern PagingInfo *kernelPhysicalPages; -extern void setupMemory(); extern void reservePagesUntilPhysical(uint32_t endPageId); extern void *findTarFile(void *fileStart, uint32_t fileSize, char *filename); diff --git a/src/kernel/boot/boot.asm b/src/kernel/boot/boot.asm index df975de..e9443dc 100644 --- a/src/kernel/boot/boot.asm +++ b/src/kernel/boot/boot.asm @@ -91,7 +91,7 @@ pop eax ret -earlyGDT: +earlyGDT: ; using this GDT before activating paging .start: dq 0 .code: diff --git a/src/kernel/include/memory.h b/src/kernel/include/memory.h index 2129d04..2bbb8fe 100644 --- a/src/kernel/include/memory.h +++ b/src/kernel/include/memory.h @@ -31,7 +31,6 @@ extern PagingInfo *kernelPhysicalPages; -extern void setupMemory(); extern void reservePagesUntilPhysical(uint32_t endPageId); extern void *findTarFile(void *fileStart, uint32_t fileSize, char *filename); diff --git a/src/kernel/main.c b/src/kernel/main.c index ae6b0d1..d3bd8a9 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -1,3 +1,4 @@ +#include #define ALLOC_MAIN #include "memory/malloc.h" @@ -40,7 +41,7 @@ } void kernelMain(void *multibootInfo) { - setupMemory(); + reservePagesUntilPhysical(0x900); loadAndScheduleSystemServices(multibootInfo); setupSyscalls(); registerInterrupts(); diff --git a/src/kernel/boot/boot.asm b/src/kernel/boot/boot.asm index df975de..e9443dc 100644 --- a/src/kernel/boot/boot.asm +++ b/src/kernel/boot/boot.asm @@ -91,7 +91,7 @@ pop eax ret -earlyGDT: +earlyGDT: ; using this GDT before activating paging .start: dq 0 .code: diff --git a/src/kernel/include/memory.h b/src/kernel/include/memory.h index 2129d04..2bbb8fe 100644 --- a/src/kernel/include/memory.h +++ b/src/kernel/include/memory.h @@ -31,7 +31,6 @@ extern PagingInfo *kernelPhysicalPages; -extern void setupMemory(); extern void reservePagesUntilPhysical(uint32_t endPageId); extern void *findTarFile(void *fileStart, uint32_t fileSize, char *filename); diff --git a/src/kernel/main.c b/src/kernel/main.c index ae6b0d1..d3bd8a9 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -1,3 +1,4 @@ +#include #define ALLOC_MAIN #include "memory/malloc.h" @@ -40,7 +41,7 @@ } void kernelMain(void *multibootInfo) { - setupMemory(); + reservePagesUntilPhysical(0x900); loadAndScheduleSystemServices(multibootInfo); setupSyscalls(); registerInterrupts(); diff --git a/src/kernel/memory/memory.c b/src/kernel/memory/memory.c deleted file mode 100644 index 0be4063..0000000 --- a/src/kernel/memory/memory.c +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include - -void setupMemory() { - reservePagesUntilPhysical(0x900); // address 0x900000, until the end of - // where the kernel data was mapped -} diff --git a/src/kernel/boot/boot.asm b/src/kernel/boot/boot.asm index df975de..e9443dc 100644 --- a/src/kernel/boot/boot.asm +++ b/src/kernel/boot/boot.asm @@ -91,7 +91,7 @@ pop eax ret -earlyGDT: +earlyGDT: ; using this GDT before activating paging .start: dq 0 .code: diff --git a/src/kernel/include/memory.h b/src/kernel/include/memory.h index 2129d04..2bbb8fe 100644 --- a/src/kernel/include/memory.h +++ b/src/kernel/include/memory.h @@ -31,7 +31,6 @@ extern PagingInfo *kernelPhysicalPages; -extern void setupMemory(); extern void reservePagesUntilPhysical(uint32_t endPageId); extern void *findTarFile(void *fileStart, uint32_t fileSize, char *filename); diff --git a/src/kernel/main.c b/src/kernel/main.c index ae6b0d1..d3bd8a9 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -1,3 +1,4 @@ +#include #define ALLOC_MAIN #include "memory/malloc.h" @@ -40,7 +41,7 @@ } void kernelMain(void *multibootInfo) { - setupMemory(); + reservePagesUntilPhysical(0x900); loadAndScheduleSystemServices(multibootInfo); setupSyscalls(); registerInterrupts(); diff --git a/src/kernel/memory/memory.c b/src/kernel/memory/memory.c deleted file mode 100644 index 0be4063..0000000 --- a/src/kernel/memory/memory.c +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include - -void setupMemory() { - reservePagesUntilPhysical(0x900); // address 0x900000, until the end of - // where the kernel data was mapped -} diff --git a/src/kernel/syscalls/syscall.c b/src/kernel/syscalls/syscall.c index a0acca3..61ab6d8 100644 --- a/src/kernel/syscalls/syscall.c +++ b/src/kernel/syscalls/syscall.c @@ -8,72 +8,77 @@ extern void(syscallStub)(); extern Syscall *currentSyscall; -void wrmsr(uint32_t msr, uint32_t low, uint32_t high) { - asm("wrmsr" ::"a"(low), "d"(high), "c"(msr)); +void writeMsrRegister(uint32_t reg, void *value) { + // when transitioning to 64 bit: U64(value) >> 32); + asm("wrmsr" ::"a"(U32(value)), "d"(0), "c"(reg)); } -void writeMsrRegister(uint32_t reg, void *value) { - wrmsr(reg, U32(value), - 0); // when transitioning to 64 bit: U32(value) >> 32); +void setupSyscalls() { + writeMsrRegister(0x174, PTR(0x08)); // code segment register + writeMsrRegister(0x175, malloc(0x1000) + 0x1000); // handler stack + writeMsrRegister(0x176, syscallStub); // the handler +} + +void finalizeCall(uint32_t returnCode) { + // if a function has finished all of its work, free the stack and resume the + // super call (if it exists) + Service *service = currentSyscall->service; + void *espPhysical = getPhysicalAddress(service->pagingInfo.pageDirectory, + currentSyscall->esp); + freePage(currentSyscall->esp); + + if (currentSyscall->respondingTo) { + if (currentSyscall->respondingTo->function == 2) { + currentSyscall->respondingTo->returnValue = returnCode; + } + listAdd(&callsToProcess, currentSyscall->respondingTo); + } } void handleSyscall(void *esp, uint32_t function, uint32_t parameter0, uint32_t parameter1, uint32_t parameter2, uint32_t parameter3) { if (!function) { - Service *service = currentSyscall->service; - void *espPhysical = getPhysicalAddress( - service->pagingInfo.pageDirectory, currentSyscall->esp); - freePage(currentSyscall->esp); - - if (currentSyscall->respondingTo) { - if (currentSyscall->respondingTo->function == 2) { - currentSyscall->respondingTo->returnValue = parameter0; - } - listAdd(&callsToProcess, currentSyscall->respondingTo); - } - return; + return finalizeCall(parameter0); } Syscall *call = malloc(sizeof(Syscall)); + Service *currentService = currentSyscall->service; call->function = function; + call->service = currentService; + call->esp = esp; + call->respondingTo = currentSyscall->respondingTo; + // todo: remove the cr3 parameter from the syscall struct + call->cr3 = + getPhysicalAddressKernel(currentService->pagingInfo.pageDirectory); call->parameters[0] = parameter0; call->parameters[1] = parameter1; call->parameters[2] = parameter2; call->parameters[3] = parameter3; - call->service = currentSyscall->service; - call->esp = esp; - call->respondingTo = currentSyscall->respondingTo; - Service *currentService = currentSyscall->service; - call->cr3 = - getPhysicalAddressKernel(currentService->pagingInfo.pageDirectory); - if (function == -1) { - call->resume = true; - call->function = 0; - } listAdd(&callsToProcess, call); } -void *syscallStubPtr = syscallStub; - -void setupSyscalls() { - writeMsrRegister(0x174, PTR(0x08)); // code segment register - writeMsrRegister(0x175, malloc(0x1000) + 0x1000); // handler stack - writeMsrRegister(0x176, syscallStubPtr); // the handler -} - +extern uintptr_t handleCreateFunctionSyscall; +extern uintptr_t handleRequestSyscall; +extern uintptr_t handleIOInSyscall; +extern uintptr_t handleIOOutSyscall; extern uintptr_t handleLoadFromInitrdSyscall; -extern uintptr_t handleIOInSyscall, handleIOOutSyscall; -extern uintptr_t handleGetServiceIdSyscall, handleGetFunctionSyscall, - handleGetServiceSyscall, handleCreateFunctionSyscall, handleRequestSyscall; -extern uintptr_t handleCreateEventSyscall, handleGetEventSyscall, - handleFireEventSyscall, handleSubscribeEventSyscall; +extern uintptr_t handleGetServiceSyscall; +extern uintptr_t handleGetFunctionSyscall; extern uintptr_t handleSubscribeInterruptSyscall; -extern uintptr_t handleInsertStringSyscall, handleReadStringLengthSyscall, - handleReadStringSyscall, handleDiscardStringSyscall; -extern uintptr_t handleRequestMemorySyscall, handleGetPhysicalSyscall; +extern uintptr_t handleCreateEventSyscall; +extern uintptr_t handleGetEventSyscall; +extern uintptr_t handleFireEventSyscall; +extern uintptr_t handleSubscribeEventSyscall; +extern uintptr_t handleGetServiceIdSyscall; +extern uintptr_t handleInsertStringSyscall; +extern uintptr_t handleReadStringLengthSyscall; +extern uintptr_t handleReadStringSyscall; +extern uintptr_t handleDiscardStringSyscall; +extern uintptr_t handleRequestMemorySyscall; extern uintptr_t handleLookupSymbolSyscall; extern uintptr_t handleStackContainsSyscall; extern uintptr_t handleAwaitSyscall; +extern uintptr_t handleGetPhysicalSyscall; void (*syscallHandlers[])(Syscall *) = { 0, @@ -99,9 +104,6 @@ (void *)&handleStackContainsSyscall, (void *)&handleAwaitSyscall, (void *)&handleGetPhysicalSyscall, - 0, - 0, - 0, }; void processSyscall(Syscall *call) { @@ -110,8 +112,10 @@ free(call); return; } - void (*handler)(Syscall *) = syscallHandlers[call->function]; - if (handler) { + // call->function is never 0 (see handleSyscall) + // if the call handler could not be resolved, ignore the syscall + if (call->function < sizeof(syscallHandlers) / sizeof(void *)) { + void (*handler)(Syscall *) = syscallHandlers[call->function]; handler(call); } call->resume = true;