From 39c3d4e2f2c391e3861c0ba792ec2b3cb8e9b831 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 5 May 2023 17:10:44 +0300 Subject: [PATCH] Add syscalls test --- KernelThread.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++-- Tests/TestSyscalls.c | 47 +++++++++++++++++++++++++++++++++++ Tests/t.h | 1 + 3 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 Tests/TestSyscalls.c diff --git a/KernelThread.cpp b/KernelThread.cpp index 6cf6bda..0772752 100644 --- a/KernelThread.cpp +++ b/KernelThread.cpp @@ -16,6 +16,9 @@ */ #include "kernel.h" +#ifdef DEBUG +#include "Tests/t.h" +#endif #include #include @@ -127,15 +130,24 @@ uint64_t GetUsage(uint64_t OldSystemTime, Tasking::TaskInfo *Info) return 0; } +static int ShowTaskManager = 0; + void TaskMgr() { TaskManager->GetCurrentThread()->Rename("Debug Task Manager"); TaskManager->GetCurrentThread()->SetPriority(Tasking::Low); + + while (ShowTaskManager == 0) + CPU::Pause(); + TaskManager->CreateThread(TaskManager->GetCurrentProcess(), (Tasking::IP)TaskMgr_Dummy100Usage)->Rename("Dummy 100% Usage"); TaskManager->CreateThread(TaskManager->GetCurrentProcess(), (Tasking::IP)TaskMgr_Dummy0Usage)->Rename("Dummy 0% Usage"); - while (1) + while (true) { + while (ShowTaskManager == 0) + CPU::Pause(); + static int sanity = 0; Video::ScreenBuffer *sb = Display->GetBuffer(0); for (short i = 0; i < 1000; i++) @@ -191,6 +203,48 @@ void TaskMgr() TaskManager->Sleep(100); } } + +void TestSyscallsKernel() +{ + return; + KPrint("Testing syscalls..."); + Tasking::PCB *SyscallsTestProcess = TaskManager->CreateProcess(TaskManager->GetCurrentProcess(), + "Syscalls Test", + Tasking::TaskTrustLevel::User, + KernelSymbolTable); + + Tasking::TCB *SyscallsTestThread = TaskManager->CreateThread(SyscallsTestProcess, + (Tasking::IP)TestSyscalls, + nullptr, + nullptr, + std::vector(), + 0, + Tasking::TaskArchitecture::x64, + Tasking::TaskCompatibility::Native, + true); + SyscallsTestThread->SetCritical(true); + TaskManager->GetSecurityManager()->TrustToken(SyscallsTestThread->Security.UniqueToken, Tasking::TTL::FullTrust); + + Memory::Virtual va = Memory::Virtual(SyscallsTestProcess->PageTable); + + // va.Remap((void *)TestSyscalls, va.GetPhysical((void *)TestSyscalls), Memory::P | Memory::RW | Memory::US); + + // for (uintptr_t k = (uintptr_t)&_kernel_start; k < (uintptr_t)&_kernel_end; k += PAGE_SIZE) + // { + // va.Remap((void *)k, (void *)va.GetPhysical((void *)k), Memory::P | Memory::RW | Memory::US); + // debug("Remapped %#lx %#lx", k, va.GetPhysical((void *)k)); + // } + + for (uintptr_t k = (uintptr_t)TestSyscalls - PAGE_SIZE; k < (uintptr_t)TestSyscalls + FROM_PAGES(5); k += PAGE_SIZE) + { + va.Remap((void *)k, (void *)va.GetPhysical((void *)k), Memory::P | Memory::RW | Memory::US); + debug("Remapped %#lx %#lx", k, va.GetPhysical((void *)k)); + } + + SyscallsTestThread->Status = Tasking::TaskStatus::Ready; + TaskManager->WaitForThread(SyscallsTestThread); + KPrint("Test complete"); +} #endif Execute::SpawnData SpawnInit() @@ -363,8 +417,9 @@ void KernelMainThread() } #ifdef DEBUG - // Tasking::TCB *tskMgr = TaskManager->CreateThread(TaskManager->GetCurrentProcess(), (Tasking::IP)TaskMgr); + Tasking::TCB *tskMgr = TaskManager->CreateThread(TaskManager->GetCurrentProcess(), (Tasking::IP)TaskMgr); TreeFS(vfs->GetRootNode(), 0); + TestSyscallsKernel(); #endif KPrint("Kernel Compiled at: %s %s with C++ Standard: %d", __DATE__, __TIME__, CPP_LANGUAGE_STANDARD); diff --git a/Tests/TestSyscalls.c b/Tests/TestSyscalls.c new file mode 100644 index 0000000..3eb2c94 --- /dev/null +++ b/Tests/TestSyscalls.c @@ -0,0 +1,47 @@ +/* + This file is part of Fennix Kernel. + + Fennix Kernel is free software: you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of + the License, or (at your option) any later version. + + Fennix Kernel is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Fennix Kernel. If not, see . +*/ + +#include + +#include "../syscalls.h" + +#ifdef DEBUG + +__aligned(0x1000) __no_stack_protector void TestSyscalls() +{ + __asm__ __volatile__("syscall" + : + : "a"(_Print), "D"('H'), "S"(0) + : "rcx", "r11", "memory"); + + int fork_id = -0xda; + + __asm__ __volatile__("syscall" + : "=a"(fork_id) + : "a"(_Fork) + : "rcx", "r11", "memory"); + + __asm__ __volatile__("syscall" + : + : "a"(_Exit), "D"(fork_id) + : "rcx", "r11", "memory"); + + while (1) + ; +} + +#endif diff --git a/Tests/t.h b/Tests/t.h index b7326d4..e6f841c 100644 --- a/Tests/t.h +++ b/Tests/t.h @@ -23,6 +23,7 @@ void TestString(); void TestMemoryAllocation(); +EXTERNC void TestSyscalls(); #endif // DEBUG #endif // !__FENNIX_KERNEL_non_constructor_tests_H__