diff --git a/Architecture/amd64/acpi.hpp b/Architecture/amd64/acpi.hpp index 6a2f25c..9e9d135 100644 --- a/Architecture/amd64/acpi.hpp +++ b/Architecture/amd64/acpi.hpp @@ -261,14 +261,7 @@ namespace ACPI uint8_t PM1_CNT_LEN = 0; ACPI *acpi; - -#if defined(__amd64__) void OnInterruptReceived(CPU::x64::TrapFrame *Frame); -#elif defined(__i386__) - void OnInterruptReceived(void *Frame); -#elif defined(__aarch64__) - void OnInterruptReceived(void *Frame); -#endif public: bool ACPIShutdownSupported = false; diff --git a/Architecture/amd64/cpu/InterruptDescriptorTable.cpp b/Architecture/amd64/cpu/InterruptDescriptorTable.cpp index 320417a..790e34f 100644 --- a/Architecture/amd64/cpu/InterruptDescriptorTable.cpp +++ b/Architecture/amd64/cpu/InterruptDescriptorTable.cpp @@ -212,10 +212,7 @@ namespace InterruptDescriptorTable /* Reserved by OS */ - __attribute__((naked, used, no_stack_protector)) void InterruptHandler_0x30() { asm("pushq $0\npushq $" - "0x30" - "\n" - "jmp SchedulerInterruptStub\n"); } + INTERRUPT_HANDLER(0x30) INTERRUPT_HANDLER(0x31) INTERRUPT_HANDLER(0x32) INTERRUPT_HANDLER(0x33) diff --git a/Core/CrashHandler.cpp b/Core/CrashHandler.cpp index 9ad0af4..a04217c 100644 --- a/Core/CrashHandler.cpp +++ b/Core/CrashHandler.cpp @@ -232,7 +232,7 @@ namespace CrashHandler cr0.PE ? "True " : "False", cr0.MP ? "True " : "False", cr0.EM ? "True " : "False", cr0.TS ? "True " : "False", cr0.ET ? "True " : "False", cr0.NE ? "True " : "False", cr0.WP ? "True " : "False", cr0.AM ? "True " : "False", cr0.NW ? "True " : "False", cr0.CD ? "True " : "False", cr0.PG ? "True " : "False", - cr0._reserved0, cr0._reserved1, cr0._reserved2); + cr0.Reserved0, cr0.Reserved1, cr0.Reserved2); EHPrint("\eFCBD79CR2: PFLA: %#llx\n", cr2.PFLA); @@ -246,7 +246,7 @@ namespace CrashHandler cr4.PCE ? "True " : "False", cr4.UMIP ? "True " : "False", cr4.OSFXSR ? "True " : "False", cr4.OSXMMEXCPT ? "True " : "False", cr4.LA57 ? "True " : "False", cr4.VMXE ? "True " : "False", cr4.SMXE ? "True " : "False", cr4.PCIDE ? "True " : "False", cr4.OSXSAVE ? "True " : "False", cr4.SMEP ? "True " : "False", cr4.SMAP ? "True " : "False", cr4.PKE ? "True " : "False", - cr4._reserved0, cr4._reserved1, cr4._reserved2); + cr4.Reserved0, cr4.Reserved1, cr4.Reserved2); EHPrint("\e79FCF5CR8: TPL:%d\n", cr8.TPL); @@ -255,8 +255,8 @@ namespace CrashHandler Frame->rflags.SF ? "True " : "False", Frame->rflags.TF ? "True " : "False", Frame->rflags.IF ? "True " : "False", Frame->rflags.DF ? "True " : "False", Frame->rflags.OF ? "True " : "False", Frame->rflags.IOPL ? "True " : "False", Frame->rflags.NT ? "True " : "False", Frame->rflags.RF ? "True " : "False", Frame->rflags.VM ? "True " : "False", Frame->rflags.AC ? "True " : "False", Frame->rflags.VIF ? "True " : "False", Frame->rflags.VIP ? "True " : "False", - Frame->rflags.ID ? "True " : "False", Frame->rflags.always_one, - Frame->rflags._reserved0, Frame->rflags._reserved1, Frame->rflags._reserved2, Frame->rflags._reserved3); + Frame->rflags.ID ? "True " : "False", Frame->rflags.AlwaysOne, + Frame->rflags.Reserved0, Frame->rflags.Reserved1, Frame->rflags.Reserved2, Frame->rflags.Reserved3); EHPrint("\eA0F0F0DR7: LDR0:%s GDR0:%s LDR1:%s GDR1:%s\n LDR2:%s GDR2:%s LDR3:%s GDR3:%s\n CDR0:%s SDR0:%s CDR1:%s SDR1:%s\n CDR2:%s SDR2:%s CDR3:%s SDR3:%s\n R:%#x\n", dr7.LocalDR0 ? "True " : "False", dr7.GlobalDR0 ? "True " : "False", dr7.LocalDR1 ? "True " : "False", dr7.GlobalDR1 ? "True " : "False", @@ -470,7 +470,7 @@ void PageFaultExceptionHandler(CPU::x64::TrapFrame *Frame) staticbuffer(page_present); staticbuffer(page_write); staticbuffer(page_user); - staticbuffer(page_reserved); + staticbuffer(pageReserved); staticbuffer(page_fetch); staticbuffer(page_protection); staticbuffer(page_shadow); @@ -485,8 +485,8 @@ void PageFaultExceptionHandler(CPU::x64::TrapFrame *Frame) CrashHandler::EHPrint(page_write); sprintf_(page_user, "Processor Mode: %s\n", params.U ? "User-Mode" : "Kernel-Mode"); CrashHandler::EHPrint(page_user); - sprintf_(page_reserved, "CPU Reserved Bits: %s\n", params.R ? "Reserved" : "Unreserved"); - CrashHandler::EHPrint(page_reserved); + sprintf_(pageReserved, "CPU Reserved Bits: %s\n", params.R ? "Reserved" : "Unreserved"); + CrashHandler::EHPrint(pageReserved); sprintf_(page_fetch, "Caused By An Instruction Fetch: %s\n", params.I ? "Yes" : "No"); CrashHandler::EHPrint(page_fetch); sprintf_(page_protection, "Caused By A Protection-Key Violation: %s\n", params.PK ? "Yes" : "No"); diff --git a/Tasking/Security.cpp b/Tasking/Security.cpp new file mode 100644 index 0000000..22c280b --- /dev/null +++ b/Tasking/Security.cpp @@ -0,0 +1,39 @@ +#include +#include + +namespace Tasking +{ + Token Security::CreateToken() + { + fixme("CreateToken->0"); + return 0; + } + + bool Security::TrustToken(Token token, + TokenTrustLevel TrustLevel) + { + fixme("TrustToken->false"); + return false; + } + + bool Security::UntrustToken(Token token) + { + fixme("UntrustToken->false"); + return false; + } + + bool Security::DestroyToken(Token token) + { + fixme("DestroyToken->false"); + return false; + } + + Security::Security() + { + trace("Initializing Tasking Security"); + } + + Security::~Security() + { + } +} diff --git a/Tasking/Task.cpp b/Tasking/Task.cpp index 18776dc..517e29d 100644 --- a/Tasking/Task.cpp +++ b/Tasking/Task.cpp @@ -1,6 +1,5 @@ #include -#include #include #include #include @@ -14,6 +13,14 @@ #elif defined(__aarch64__) #endif +#define DEBUG_SCHEDULER 1 + +#ifdef DEBUG_SCHEDULER +#define schedbg(m, ...) debug(m, ##__VA_ARGS__) +#else +#define schedbg(m, ...) +#endif + NewLock(TaskingLock); namespace Tasking @@ -27,7 +34,7 @@ namespace Tasking #endif } - extern "C" __attribute__((naked, used, no_stack_protector)) void IdleProcessLoop() + __attribute__((naked, used, no_stack_protector)) void IdleProcessLoop() { #if defined(__amd64__) || defined(__i386__) asmv("IdleLoop:\n" @@ -41,61 +48,36 @@ namespace Tasking #endif } - /** @brief Called by the IDT (IRQ16 for x64 and x32) */ - extern "C" __attribute__((naked, used, no_stack_protector)) void SchedulerInterruptStub() - { -#if defined(__amd64__) - asm("cld\n" - "pushq %rax\n" - "pushq %rbx\n" - "pushq %rcx\n" - "pushq %rdx\n" - "pushq %rsi\n" - "pushq %rdi\n" - "pushq %rbp\n" - "pushq %r8\n" - "pushq %r9\n" - "pushq %r10\n" - "pushq %r11\n" - "pushq %r12\n" - "pushq %r13\n" - "pushq %r14\n" - "pushq %r15\n" - "movq %ds, %rax\n" - "pushq %rax\n" - "movq %rsp, %rdi\n" - "call SchedulerInterruptHandler\n" - "popq %rax\n" // Pop the DS register - "popq %r15\n" - "popq %r14\n" - "popq %r13\n" - "popq %r12\n" - "popq %r11\n" - "popq %r10\n" - "popq %r9\n" - "popq %r8\n" - "popq %rbp\n" - "popq %rdi\n" - "popq %rsi\n" - "popq %rdx\n" - "popq %rcx\n" - "popq %rbx\n" - "popq %rax\n" - "addq $16, %rsp\n" - "iretq"); -#elif defined(__i386__) -#elif defined(__aarch64__) -#endif - } + Vector ListProcess; + PCB *IdleProcess = nullptr; + TCB *IdleThread = nullptr; - extern "C" __attribute__((no_stack_protector)) void SchedulerInterruptHandler(ThreadFrame *Frame) - { - fixme("SchedulerInterruptHandler: %d", GetCurrentCPU()->ID); #if defined(__amd64__) - ((APIC::APIC *)Interrupts::apic[GetCurrentCPU()->ID])->EOI(); + __attribute__((no_stack_protector)) void Task::OnInterruptReceived(CPU::x64::TrapFrame *Frame) + { + fixme("unimplemented"); + } #elif defined(__i386__) + __attribute__((no_stack_protector)) void Task::OnInterruptReceived(void *Frame) + { + fixme("unimplemented"); + } #elif defined(__aarch64__) + __attribute__((no_stack_protector)) void Task::OnInterruptReceived(void *Frame) + { + fixme("unimplemented"); + } #endif + + void ThreadDoExit(int Code) + { + SmartCriticalSection(TaskingLock); + CPUData *CPUData = GetCurrentCPU(); + CPUData->CurrentThread->Status = TaskStatus::Terminated; + CPUData->CurrentThread->ExitCode = Code; + debug("parent:%s tid:%d, code:%016p", CPUData->CurrentProcess->Name, CPUData->CurrentThread->ID, Code); + trace("Exiting thread %d(%s)...", CPUData->CurrentThread->ID, CPUData->CurrentThread->Name); + CPU::Stop(); } PCB *Task::GetCurrentProcess() @@ -110,46 +92,208 @@ namespace Tasking return GetCurrentCPU()->CurrentThread; } - PCB *Task::CreateProcess(PCB *Parent, - const char *Name, - TaskElevation Elevation) - { - SmartCriticalSection(TaskingLock); - PCB *Process = new PCB; -#if defined(__amd64__) -#elif defined(__i386__) -#elif defined(__aarch64__) -#endif - return Process; - } - TCB *Task::CreateThread(PCB *Parent, - IP EntryPoint) + IP EntryPoint, + IPOffset Offset, + TaskArchitecture Architecture, + TaskCompatibility Compatibility) { SmartCriticalSection(TaskingLock); TCB *Thread = new TCB; + Thread->ID = this->NextTID++; + strcpy(Thread->Name, Parent->Name); + Thread->Parent = Parent; + Thread->EntryPoint = EntryPoint; + Thread->Offset = Offset; + Thread->ExitCode = 0xdeadbeef; + Thread->Stack = (void *)((uint64_t)KernelAllocator.RequestPages(TO_PAGES(STACK_SIZE)) + STACK_SIZE); + Thread->Status = TaskStatus::Ready; + + memset(&Thread->Registers, 0, sizeof(ThreadFrame)); // Just in case + switch (Parent->Security.TrustLevel) + { + case TaskTrustLevel::System: + warn("Trust level not supported."); + [[fallthrough]]; + case TaskTrustLevel::Idle: + case TaskTrustLevel::Kernel: + { #if defined(__amd64__) + SecurityManager.TrustToken(Thread->Security.UniqueToken, TokenTrustLevel::TrustedByKernel); + Thread->Registers.cs = GDT_KERNEL_CODE; + Thread->Registers.ds = GDT_KERNEL_DATA; + Thread->Registers.ss = GDT_KERNEL_DATA; + Thread->Registers.rflags.AlwaysOne = 1; + Thread->Registers.rflags.IF = 1; + Thread->Registers.rflags.ID = 1; + Thread->Registers.rsp = (uint64_t)Thread->Stack; + POKE(uint64_t, Thread->Registers.rsp) = (uint64_t)ThreadDoExit; #elif defined(__i386__) #elif defined(__aarch64__) #endif + break; + } + case TaskTrustLevel::User: + { +#if defined(__amd64__) + SecurityManager.TrustToken(Thread->Security.UniqueToken, TokenTrustLevel::Untrusted); + Thread->Registers.cs = GDT_USER_CODE; + Thread->Registers.ds = GDT_USER_DATA; + Thread->Registers.ss = GDT_USER_DATA; + Thread->Registers.rflags.AlwaysOne = 1; + Thread->Registers.rflags.IF = 1; + Thread->Registers.rflags.ID = 1; + Thread->Registers.rsp = (uint64_t)Thread->Stack; + /* We need to leave the libc's crt to make a syscall when the Thread is exited or we are going to get GPF or PF exception. */ + for (uint64_t i = 0; i < TO_PAGES(STACK_SIZE); i++) + Memory::Virtual().Map((void *)((uint64_t)Thread->Stack + (i * PAGE_SIZE)), + (void *)((uint64_t)Thread->Stack + (i * PAGE_SIZE)), + Memory::PTFlag::US); +#elif defined(__i386__) +#elif defined(__aarch64__) +#endif + break; + } + default: + { + error("Unknown elevation."); + KernelAllocator.FreePages((void *)((uint64_t)Thread->Stack - STACK_SIZE), TO_PAGES(STACK_SIZE)); + delete Thread; + return nullptr; + } + } + + Thread->Security.TrustLevel = Parent->Security.TrustLevel; + Thread->Security.UniqueToken = SecurityManager.CreateToken(); + + Thread->Info.SpawnTime = 0; + Thread->Info.UsedTime = 0; + Thread->Info.OldUsedTime = 0; + Thread->Info.OldSystemTime = 0; + Thread->Info.CurrentSystemTime = 0; + Thread->Info.Year = 0; + Thread->Info.Month = 0; + Thread->Info.Day = 0; + Thread->Info.Hour = 0; + Thread->Info.Minute = 0; + Thread->Info.Second = 0; + for (int i = 0; i < MAX_CPU; i++) + { + Thread->Info.Usage[i] = 0; + Thread->Info.Affinity[i] = 0; + } + Thread->Info.Priority = 0; + Thread->Info.Architecture = Architecture; + Thread->Info.Compatibility = Compatibility; + Parent->Threads.push_back(Thread); return Thread; } - Task::Task(const IP EntryPoint) + PCB *Task::CreateProcess(PCB *Parent, + const char *Name, + TaskTrustLevel TrustLevel) + { + SmartCriticalSection(TaskingLock); + PCB *Process = new PCB; + Process->ID = this->NextPID++; + strcpy(Process->Name, Name); + Process->Parent = Parent; + Process->ExitCode = 0xdeadbeef; + Process->Status = TaskStatus::Ready; + + Process->Security.TrustLevel = TrustLevel; + Process->Security.UniqueToken = SecurityManager.CreateToken(); + + switch (TrustLevel) + { + case TaskTrustLevel::System: + warn("Trust level not supported."); + [[fallthrough]]; + case TaskTrustLevel::Idle: + case TaskTrustLevel::Kernel: + { + SecurityManager.TrustToken(Process->Security.UniqueToken, TokenTrustLevel::TrustedByKernel); +#if defined(__amd64__) + Process->PageTable = (Memory::PageTable *)KernelAllocator.RequestPages(TO_PAGES(PAGE_SIZE)); + memset(Process->PageTable, 0, PAGE_SIZE); + CPU::x64::CR3 cr3 = CPU::x64::readcr3(); + memcpy(Process->PageTable, (void *)cr3.raw, PAGE_SIZE); +#elif defined(__i386__) +#elif defined(__aarch64__) +#endif + break; + } + case TaskTrustLevel::User: + { + SecurityManager.TrustToken(Process->Security.UniqueToken, TokenTrustLevel::Untrusted); +#if defined(__amd64__) + Process->PageTable = (Memory::PageTable *)KernelAllocator.RequestPages(TO_PAGES(PAGE_SIZE)); + // TODO: Do mapping for page table + fixme("User process page table mapping not implemented."); +#elif defined(__i386__) +#elif defined(__aarch64__) +#endif + break; + } + default: + { + error("Unknown elevation."); + delete Process; + return nullptr; + } + } + + Process->Info.SpawnTime = 0; + Process->Info.UsedTime = 0; + Process->Info.OldUsedTime = 0; + Process->Info.OldSystemTime = 0; + Process->Info.CurrentSystemTime = 0; + Process->Info.Year = 0; + Process->Info.Month = 0; + Process->Info.Day = 0; + Process->Info.Hour = 0; + Process->Info.Minute = 0; + Process->Info.Second = 0; + for (int i = 0; i < MAX_CPU; i++) + { + Process->Info.Usage[i] = 0; + Process->Info.Affinity[i] = 0; + } + Process->Info.Priority = 0; + + Parent->Children.push_back(Process); + return Process; + } + + Task::Task(const IP EntryPoint) : Interrupts::Handler(CPU::x64::IRQ16) { SmartCriticalSection(TaskingLock); - KPrint("Starting Tasking With Instruction Pointer: %p (\e666666%s\eCCCCCC)", EntryPoint, KernelSymbolTable->GetSymbolFromAddress(EntryPoint)); -#if defined(__amd64__) || defined(__i386__) for (int i = 0; i < SMP::CPUCores; i++) ((APIC::APIC *)Interrupts::apic[i])->RedirectIRQ(i, CPU::x64::IRQ16 - CPU::x64::IRQ0, 1); + + KPrint("Starting Tasking With Instruction Pointer: %p (\e666666%s\eCCCCCC)", EntryPoint, KernelSymbolTable->GetSymbolFromAddress(EntryPoint)); + TaskingLock.Unlock(); + +#if defined(__amd64__) + TaskArchitecture Arch = TaskArchitecture::x64; +#elif defined(__i386__) + TaskArchitecture Arch = TaskArchitecture::x32; +#elif defined(__aarch64__) + TaskArchitecture Arch = TaskArchitecture::ARM64; #endif - TaskingLock.Unlock(); - PCB *kproc = CreateProcess(nullptr, "Kernel", TaskElevation::Kernel); - TCB *kthrd = CreateThread(kproc, EntryPoint); + PCB *kproc = CreateProcess(nullptr, "Kernel", TaskTrustLevel::Kernel); + TCB *kthrd = CreateThread(kproc, EntryPoint, 0, Arch); kthrd->Rename("Main Thread"); + debug("Created Kernel Process: %s and Thread: %s", kproc->Name, kthrd->Name); TaskingLock.Lock(); - OneShot(100); +#if defined(__amd64__) || defined(__i386__) + for (int i = 0; i < SMP::CPUCores; i++) + { + /* do stuff i guess */ + ((APIC::Timer *)Interrupts::apicTimer[i])->OneShot(CPU::x64::IRQ16, 100); + } +#endif debug("Tasking Started"); } diff --git a/include/cpu.hpp b/include/cpu.hpp index 8e3863b..eee5efd 100644 --- a/include/cpu.hpp +++ b/include/cpu.hpp @@ -1004,15 +1004,15 @@ namespace CPU /** @brief Carry Flag */ uint64_t CF : 1; /** @brief Reserved */ - uint64_t always_one : 1; + uint64_t AlwaysOne : 1; /** @brief Parity Flag */ uint64_t PF : 1; /** @brief Reserved */ - uint64_t _reserved0 : 1; + uint64_t Reserved0 : 1; /** @brief Auxiliary Carry Flag */ uint64_t AF : 1; /** @brief Reserved */ - uint64_t _reserved1 : 1; + uint64_t Reserved1 : 1; /** @brief Zero Flag */ uint64_t ZF : 1; /** @brief Sign Flag */ @@ -1030,7 +1030,7 @@ namespace CPU /** @brief Nested Task */ uint64_t NT : 1; /** @brief Reserved */ - uint64_t _reserved2 : 1; + uint64_t Reserved2 : 1; /** @brief Resume Flag */ uint64_t RF : 1; /** @brief Virtual 8086 Mode */ @@ -1044,7 +1044,7 @@ namespace CPU /** @brief ID Flag */ uint64_t ID : 1; /** @brief Reserved */ - uint64_t _reserved3 : 10; + uint64_t Reserved3 : 10; }; uint64_t raw; } RFLAGS; @@ -1099,15 +1099,15 @@ namespace CPU /** @brief Numeric Error */ uint64_t NE : 1; /** @brief Reserved */ - uint64_t _reserved0 : 10; + uint64_t Reserved0 : 10; /** @brief Write Protect */ uint64_t WP : 1; /** @brief Reserved */ - uint64_t _reserved1 : 1; + uint64_t Reserved1 : 1; /** @brief Alignment Mask */ uint64_t AM : 1; /** @brief Reserved */ - uint64_t _reserved2 : 10; + uint64_t Reserved2 : 10; /** @brief Mot Write-through */ uint64_t NW : 1; /** @brief Cache Disable */ @@ -1177,7 +1177,7 @@ namespace CPU /** @brief SMX Enable */ uint64_t SMXE : 1; /** @brief Reserved */ - uint64_t _reserved0 : 1; + uint64_t Reserved0 : 1; /** @brief FSGSBASE Enable */ uint64_t FSGSBASE : 1; /** @brief PCID Enable */ @@ -1185,7 +1185,7 @@ namespace CPU /** @brief XSAVE and Processor Extended States Enable */ uint64_t OSXSAVE : 1; /** @brief Reserved */ - uint64_t _reserved1 : 1; + uint64_t Reserved1 : 1; /** @brief SMEP Enable */ uint64_t SMEP : 1; /** @brief SMAP Enable */ @@ -1193,7 +1193,7 @@ namespace CPU /** @brief Protection-Key Enable */ uint64_t PKE : 1; /** @brief Reserved */ - uint64_t _reserved2 : 9; + uint64_t Reserved2 : 9; }; uint64_t raw; } CR4; @@ -1300,11 +1300,11 @@ namespace CPU /** @brief When set, the page fault was caused by a shadow stack access. */ uint64_t SS : 1; /** @brief Reserved */ - uint64_t _reserved0 : 8; + uint64_t Reserved0 : 8; /** @brief When set, the fault was due to an SGX violation. The fault is unrelated to ordinary paging. */ uint64_t SGX : 1; /** @brief Reserved */ - uint64_t _reserved1 : 16; + uint64_t Reserved1 : 16; }; uint64_t raw; } PageFaultErrorCode; diff --git a/include/interrupts.hpp b/include/interrupts.hpp index d51b634..a71acff 100644 --- a/include/interrupts.hpp +++ b/include/interrupts.hpp @@ -3,15 +3,14 @@ #include #include -#include namespace Interrupts { #if defined(__amd64__) - /* APIC::APIC */ extern void *apic[MAX_CPU]; - /* APIC::Timer */ extern void *apicTimer[MAX_CPU]; + /* APIC::APIC */ extern void *apic[256]; // MAX_CPU + /* APIC::Timer */ extern void *apicTimer[256]; // MAX_CPU #elif defined(__i386__) - extern void *apic[MAX_CPU]; + extern void *apic[256]; // MAX_CPU #elif defined(__aarch64__) #endif void Initialize(int Core); diff --git a/include/task.hpp b/include/task.hpp index b169cc2..a0fc804 100644 --- a/include/task.hpp +++ b/include/task.hpp @@ -3,6 +3,7 @@ #include +#include #include #include @@ -47,15 +48,15 @@ namespace Tasking /** @brief Carry Flag */ uint64_t CF : 1; /** @brief Reserved */ - uint64_t always_one : 1; + uint64_t AlwaysOne : 1; /** @brief Parity Flag */ uint64_t PF : 1; /** @brief Reserved */ - uint64_t _reserved0 : 1; + uint64_t Reserved0 : 1; /** @brief Auxiliary Carry Flag */ uint64_t AF : 1; /** @brief Reserved */ - uint64_t _reserved1 : 1; + uint64_t Reserved1 : 1; /** @brief Zero Flag */ uint64_t ZF : 1; /** @brief Sign Flag */ @@ -73,7 +74,7 @@ namespace Tasking /** @brief Nested Task */ uint64_t NT : 1; /** @brief Reserved */ - uint64_t _reserved2 : 1; + uint64_t Reserved2 : 1; /** @brief Resume Flag */ uint64_t RF : 1; /** @brief Virtual 8086 Mode */ @@ -87,7 +88,7 @@ namespace Tasking /** @brief ID Flag */ uint64_t ID : 1; /** @brief Reserved */ - uint64_t _reserved3 : 10; + uint64_t Reserved3 : 10; }; uint64_t raw; } rflags; // Register Flags @@ -107,7 +108,7 @@ namespace Tasking ARM64 }; - enum TaskPlatform + enum TaskCompatibility { UnknownPlatform, Native, @@ -115,7 +116,7 @@ namespace Tasking Windows }; - enum TaskElevation + enum TaskTrustLevel { UnknownElevation, Kernel, @@ -127,6 +128,7 @@ namespace Tasking enum TaskStatus { UnknownStatus, + Ready, Running, Sleeping, Waiting, @@ -136,7 +138,7 @@ namespace Tasking struct TaskSecurity { - TaskElevation Elevation; + TaskTrustLevel TrustLevel; Token UniqueToken; }; @@ -146,10 +148,10 @@ namespace Tasking uint64_t OldSystemTime = 0, CurrentSystemTime = 0; uint64_t Year, Month, Day, Hour, Minute, Second; uint64_t Usage[256]; // MAX_CPU - TaskArchitecture Architecture; - TaskPlatform Platform; + bool Affinity[256]; // MAX_CPU int Priority; - bool Affinity[256]; // MAX_CPU + TaskArchitecture Architecture; + TaskCompatibility Compatibility; }; struct TCB @@ -161,11 +163,10 @@ namespace Tasking IPOffset Offset; int ExitCode; void *Stack; + TaskStatus Status; ThreadFrame Registers; TaskSecurity Security; TaskInfo Info; - TaskStatus Status; - TaskElevation Elevation; void Rename(const char *name) { @@ -184,17 +185,49 @@ namespace Tasking char Name[256]; PCB *Parent; int ExitCode; + TaskStatus Status; TaskSecurity Security; TaskInfo Info; - TaskStatus Status; - TaskElevation Elevation; Vector Threads; Vector Children; - // Memory::PageTable PageTable; + Memory::PageTable *PageTable; }; - class Task + enum TokenTrustLevel { + UnknownTrustLevel, + Untrusted, + Trusted, + TrustedByKernel + }; + + class Security + { + public: + Token CreateToken(); + bool TrustToken(Token token, + TokenTrustLevel TrustLevel); + bool UntrustToken(Token token); + bool DestroyToken(Token token); + Security(); + ~Security(); + }; + + class Task : public Interrupts::Handler + { + private: + Security SecurityManager; + UPID NextPID = 0; + UTID NextTID = 0; + +#if defined(__amd64__) + void OnInterruptReceived(CPU::x64::TrapFrame *Frame); +#elif defined(__i386__) + void OnInterruptReceived(void *Frame); +#elif defined(__aarch64__) + void OnInterruptReceived(void *Frame); +#endif + public: /** * @brief Get the Current Process object @@ -210,10 +243,13 @@ namespace Tasking PCB *CreateProcess(PCB *Parent, const char *Name, - TaskElevation Elevation); + TaskTrustLevel TrustLevel); TCB *CreateThread(PCB *Parent, - IP EntryPoint); + IP EntryPoint, + IPOffset Offset = 0, + TaskArchitecture Architecture = TaskArchitecture::x64, + TaskCompatibility Compatibility = TaskCompatibility::Native); Task(const IP EntryPoint); ~Task();