From ce84bea215424ff36a2c18cab67835edfa8f71b5 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 27 May 2024 13:50:07 +0200 Subject: MHR-23: :boom: Change ::Shared() to ::The(). Signed-off-by: Amlal El Mahrouss --- Kernel/Sources/ProcessScheduler.cxx | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'Kernel/Sources/ProcessScheduler.cxx') diff --git a/Kernel/Sources/ProcessScheduler.cxx b/Kernel/Sources/ProcessScheduler.cxx index 1e4537f1..63d566b7 100644 --- a/Kernel/Sources/ProcessScheduler.cxx +++ b/Kernel/Sources/ProcessScheduler.cxx @@ -163,11 +163,11 @@ namespace NewOS void ProcessHeader::Exit(Int32 exit_code) { if (this->ProcessId != - ProcessScheduler::Shared().Leak().GetCurrent().Leak().ProcessId) + ProcessScheduler::The().Leak().GetCurrent().Leak().ProcessId) ke_stop(RUNTIME_CHECK_PROCESS); if (this->Ring == (Int32)ProcessSelector::kRingKernel && - ProcessScheduler::Shared().Leak().GetCurrent().Leak().Ring > 0) + ProcessScheduler::The().Leak().GetCurrent().Leak().Ring > 0) ke_stop(RUNTIME_CHECK_PROCESS); kLastExitCode = exit_code; @@ -193,7 +193,7 @@ namespace NewOS this->Image = nullptr; this->StackFrame = nullptr; - ProcessScheduler::Shared().Leak().Remove(this->ProcessId); + ProcessScheduler::The().Leak().Remove(this->ProcessId); } /// @brief Add process to list. @@ -301,7 +301,7 @@ namespace NewOS /// @brief Shared instance of the process scheduler. /// @return - Ref ProcessScheduler::Shared() + Ref ProcessScheduler::The() { static ProcessScheduler ref; return {ref}; @@ -319,7 +319,7 @@ namespace NewOS PID& ProcessHelper::GetCurrentPID() { kcout << "ProcessHelper::GetCurrentPID: Leaking ProcessId...\r"; - return ProcessScheduler::Shared().Leak().GetCurrent().Leak().ProcessId; + return ProcessScheduler::The().Leak().GetCurrent().Leak().ProcessId; } /// @brief Check if process can be schedulded. @@ -355,13 +355,13 @@ namespace NewOS bool ProcessHelper::StartScheduling() { if (ProcessHelper::CanBeScheduled( - ProcessScheduler::Shared().Leak().GetCurrent())) + ProcessScheduler::The().Leak().GetCurrent())) { - --ProcessScheduler::Shared().Leak().GetCurrent().Leak().PTime; + --ProcessScheduler::The().Leak().GetCurrent().Leak().PTime; return false; } - auto processRef = ProcessScheduler::Shared().Leak(); + auto processRef = ProcessScheduler::The().Leak(); if (!processRef) return false; // we have nothing to schedule. simply return. @@ -385,29 +385,29 @@ namespace NewOS if (!the_stack || new_pid < 0) return false; - for (SizeT index = 0UL; index < SMPManager::Shared().Leak().Count(); ++index) + for (SizeT index = 0UL; index < SMPManager::The().Leak().Count(); ++index) { - if (SMPManager::Shared().Leak()[index].Leak().Kind() == kInvalidHart) + if (SMPManager::The().Leak()[index].Leak().Kind() == kInvalidHart) continue; - if (SMPManager::Shared().Leak()[index].Leak().StackFrame() == the_stack) + if (SMPManager::The().Leak()[index].Leak().StackFrame() == the_stack) { - SMPManager::Shared().Leak()[index].Leak().Busy(false); + SMPManager::The().Leak()[index].Leak().Busy(false); continue; } - if (SMPManager::Shared().Leak()[index].Leak().IsBusy()) + if (SMPManager::The().Leak()[index].Leak().IsBusy()) continue; - if (SMPManager::Shared().Leak()[index].Leak().Kind() != + if (SMPManager::The().Leak()[index].Leak().Kind() != ThreadKind::kHartBoot && - SMPManager::Shared().Leak()[index].Leak().Kind() != + SMPManager::The().Leak()[index].Leak().Kind() != ThreadKind::kHartSystemReserved) { - SMPManager::Shared().Leak()[index].Leak().Busy(true); + SMPManager::The().Leak()[index].Leak().Busy(true); ProcessHelper::GetCurrentPID() = new_pid; - return SMPManager::Shared().Leak()[index].Leak().Switch(the_stack); + return SMPManager::The().Leak()[index].Leak().Switch(the_stack); } } -- cgit v1.2.3 From 8f262d685126a9a9f68beb6d4002ba30bebae401 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Tue, 28 May 2024 11:52:48 +0200 Subject: MHR-23: Improve scheduler code: Dont use 39, write kErrorProcessFault instead. MHR-23: Improve filesystem structure. Signed-off-by: Amlal El Mahrouss --- .vscode/c_cpp_properties.json | 4 +-- DDK/KernelCall.c | 25 +++++++++++++++++ DDK/KernelDev.c | 37 +++++++++++++++++++++++++ DDK/KernelDev.h | 33 +++++++++++++++++++++++ DDK/KernelDispatchCall.S | 21 +++++++++++++++ DDK/KernelPrint.c | 33 +++++++++++++++++++++++ DDK/KernelPrint.h | 18 +++++++++++++ DDK/KernelStd.h | 22 +++++++++++++++ DDK/KernelStdCxx.cc | 24 +++++++++++++++++ DDK/KernelString.c | 34 +++++++++++++++++++++++ DDK/KernelString.h | 16 +++++++++++ DDK/ReadMe.txt | 4 +++ DriverKit/KernelCall.c | 25 ----------------- DriverKit/KernelDev.c | 37 ------------------------- DriverKit/KernelDev.h | 33 ----------------------- DriverKit/KernelDispatchCall.S | 21 --------------- DriverKit/KernelPrint.c | 33 ----------------------- DriverKit/KernelPrint.h | 18 ------------- DriverKit/KernelStd.h | 22 --------------- DriverKit/KernelStdCxx.cc | 24 ----------------- DriverKit/KernelString.c | 34 ----------------------- DriverKit/KernelString.h | 16 ----------- DriverKit/ReadMe.txt | 4 --- Drivers/.gitkeep | 0 Drivers/Bonjour/Bonjour.c | 20 -------------- Drivers/Bonjour/DriverRsrc.rsrc | 25 ----------------- Drivers/Bonjour/x86_64.mk | 51 ----------------------------------- Drivers/DynamicLoader/DriverRsrc.rsrc | 25 ----------------- Drivers/DynamicLoader/DynamicLoader.c | 26 ------------------ Drivers/DynamicLoader/x86_64.mk | 51 ----------------------------------- Drivers/SampleDriver/DriverRsrc.rsrc | 25 ----------------- Drivers/SampleDriver/SampleDriver.c | 26 ------------------ Drivers/SampleDriver/x86_64.mk | 51 ----------------------------------- Drv/.gitkeep | 0 Drv/Bonjour/Bonjour.c | 20 ++++++++++++++ Drv/Bonjour/DriverRsrc.rsrc | 25 +++++++++++++++++ Drv/Bonjour/x86_64.mk | 51 +++++++++++++++++++++++++++++++++++ Drv/DynamicLoader/DriverRsrc.rsrc | 25 +++++++++++++++++ Drv/DynamicLoader/DynamicLoader.c | 26 ++++++++++++++++++ Drv/DynamicLoader/x86_64.mk | 51 +++++++++++++++++++++++++++++++++++ Drv/SampleDriver/DriverRsrc.rsrc | 25 +++++++++++++++++ Drv/SampleDriver/SampleDriver.c | 26 ++++++++++++++++++ Drv/SampleDriver/x86_64.mk | 51 +++++++++++++++++++++++++++++++++++ Kernel/Sources/ProcessScheduler.cxx | 2 +- 44 files changed, 570 insertions(+), 570 deletions(-) create mode 100644 DDK/KernelCall.c create mode 100644 DDK/KernelDev.c create mode 100644 DDK/KernelDev.h create mode 100644 DDK/KernelDispatchCall.S create mode 100644 DDK/KernelPrint.c create mode 100644 DDK/KernelPrint.h create mode 100644 DDK/KernelStd.h create mode 100644 DDK/KernelStdCxx.cc create mode 100644 DDK/KernelString.c create mode 100644 DDK/KernelString.h create mode 100644 DDK/ReadMe.txt delete mode 100644 DriverKit/KernelCall.c delete mode 100644 DriverKit/KernelDev.c delete mode 100644 DriverKit/KernelDev.h delete mode 100644 DriverKit/KernelDispatchCall.S delete mode 100644 DriverKit/KernelPrint.c delete mode 100644 DriverKit/KernelPrint.h delete mode 100644 DriverKit/KernelStd.h delete mode 100644 DriverKit/KernelStdCxx.cc delete mode 100644 DriverKit/KernelString.c delete mode 100644 DriverKit/KernelString.h delete mode 100644 DriverKit/ReadMe.txt delete mode 100644 Drivers/.gitkeep delete mode 100644 Drivers/Bonjour/Bonjour.c delete mode 100644 Drivers/Bonjour/DriverRsrc.rsrc delete mode 100644 Drivers/Bonjour/x86_64.mk delete mode 100644 Drivers/DynamicLoader/DriverRsrc.rsrc delete mode 100644 Drivers/DynamicLoader/DynamicLoader.c delete mode 100644 Drivers/DynamicLoader/x86_64.mk delete mode 100644 Drivers/SampleDriver/DriverRsrc.rsrc delete mode 100644 Drivers/SampleDriver/SampleDriver.c delete mode 100644 Drivers/SampleDriver/x86_64.mk create mode 100644 Drv/.gitkeep create mode 100644 Drv/Bonjour/Bonjour.c create mode 100644 Drv/Bonjour/DriverRsrc.rsrc create mode 100644 Drv/Bonjour/x86_64.mk create mode 100644 Drv/DynamicLoader/DriverRsrc.rsrc create mode 100644 Drv/DynamicLoader/DynamicLoader.c create mode 100644 Drv/DynamicLoader/x86_64.mk create mode 100644 Drv/SampleDriver/DriverRsrc.rsrc create mode 100644 Drv/SampleDriver/SampleDriver.c create mode 100644 Drv/SampleDriver/x86_64.mk (limited to 'Kernel/Sources/ProcessScheduler.cxx') diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index d9d86a41..ff4a5912 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -4,7 +4,7 @@ "name": "MicroKernel (Macintosh)", "includePath": [ "${workspaceFolder}/Kernel/**", - "${workspaceFolder}/Drivers/**", + "${workspaceFolder}/Drv/**", "${workspaceFolder}/Boot/**", "${workspaceFolder}/**", "${workspaceFolder}/SDK/Libraries/CoreSystem/**", @@ -38,7 +38,7 @@ { "name": "MicroKernel (Windows)", "includePath": [ - "${workspaceFolder}/Drivers/**", + "${workspaceFolder}/Drv/**", "${workspaceFolder}/Kernel/**", "${workspaceFolder}/Boot/**", "${workspaceFolder}/**", diff --git a/DDK/KernelCall.c b/DDK/KernelCall.c new file mode 100644 index 00000000..17e0b5d5 --- /dev/null +++ b/DDK/KernelCall.c @@ -0,0 +1,25 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + + Purpose: Kernel Definitions. + +------------------------------------------- */ + +#include +#include + +DK_EXTERN __attribute__((naked)) void __kernelDispatchCall(int32_t cnt, ...); + +DK_EXTERN void* kernelCall(const char* kernelRpcName, int32_t cnt, ...) +{ + if (!kernelRpcName || cnt == 0) + return NIL; + + va_list arg; + va_start(arg, cnt); + + __kernelDispatchCall(cnt, arg); + + va_end(arg); +} diff --git a/DDK/KernelDev.c b/DDK/KernelDev.c new file mode 100644 index 00000000..ee7aa84b --- /dev/null +++ b/DDK/KernelDev.c @@ -0,0 +1,37 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + + Purpose: Kernel Text I/O. + +------------------------------------------- */ + +#include + +/// @brief Open a new binary device from path. +DK_EXTERN kernelDeviceRef kernelOpenBinaryDevice(const char* devicePath) +{ + if (!devicePath) + return NIL; + + return kernelCall("OpenBinaryDevice", 1, devicePath); +} + +/// @brief Open a new character device from path. +DK_EXTERN kernelDeviceRef kernelOpenCharDevice(const char* devicePath) +{ + if (!devicePath) + return NIL; + + return kernelCall("OpenCharDevice", 1, devicePath); +} + +/// @brief Close any device. +/// @param device valid device. +DK_EXTERN void kernelCloseDevice(kernelDeviceRef device) +{ + if (!device) + return; + + kernelCall("CloseDevice", 1, device); +} diff --git a/DDK/KernelDev.h b/DDK/KernelDev.h new file mode 100644 index 00000000..4710023a --- /dev/null +++ b/DDK/KernelDev.h @@ -0,0 +1,33 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + + Purpose: Kernel Devices. + +------------------------------------------- */ + +#pragma once + +#include + +struct _kernelDevice; + +/// @brief Kernel Device driver. +typedef struct _kernelDevice +{ + char name[255]; // the device name. Could be /./DEVICE_NAME/ + int32_t (*read)(); // read from device. + int32_t (*write)(); // write to device. + struct _kernelDevice* (*open)(const char* path); // open device. + void (*close)(struct _kernelDevice* dev); // close device. +} kernelDevice, *kernelDeviceRef; + +/// @brief Open a new binary device from path. +DK_EXTERN kernelDeviceRef kernelOpenBinaryDevice(const char* devicePath); + +/// @brief Open a new character device from path. +DK_EXTERN kernelDeviceRef kernelOpenCharDevice(const char* devicePath); + +/// @brief Close any device. +/// @param device valid device. +DK_EXTERN void kernelCloseDevice(kernelDeviceRef device); diff --git a/DDK/KernelDispatchCall.S b/DDK/KernelDispatchCall.S new file mode 100644 index 00000000..64b6663e --- /dev/null +++ b/DDK/KernelDispatchCall.S @@ -0,0 +1,21 @@ +.globl __kernelDispatchCall + +.section .text + +/* Really simple function, takes our va-list, + and brings it to the trap handler in the kernel. */ + +#ifdef __x86_64__ + +__kernelDispatchCall: + int $0x33 + ret + +#elif defined(__powerpc64__) + +__kernelDispatchCall: + /* There is no specific interrupt request id for a system call in POWER. */ + sc + blr + +#endif diff --git a/DDK/KernelPrint.c b/DDK/KernelPrint.c new file mode 100644 index 00000000..9e25d31b --- /dev/null +++ b/DDK/KernelPrint.c @@ -0,0 +1,33 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + + Purpose: Kernel Text I/O. + +------------------------------------------- */ + +#include + +DK_EXTERN void kernelPrintChar(const char ch) +{ + kernelCall("WriteCharacter", 1, ch); +} + +/// @brief print string to UART. +/// @param message UART to transmit. +DK_EXTERN void kernelPrintStr(const char* message) +{ + if (!message) + return; + if (*message == 0) + return; + + size_t index = 0; + size_t len = kernelStringLength(message); + + while (index < len) + { + kernelPrintChar(message[index]); + ++index; + } +} diff --git a/DDK/KernelPrint.h b/DDK/KernelPrint.h new file mode 100644 index 00000000..135abcc0 --- /dev/null +++ b/DDK/KernelPrint.h @@ -0,0 +1,18 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + + Purpose: Kernel Text I/O. + +------------------------------------------- */ + +#pragma once + +#include + +/// @brief print character into UART. +DK_EXTERN void kernelPrintChar(const char ch); + +/// @brief print string to UART. +/// @param message UART to transmit. +DK_EXTERN void kernelPrintStr(const char* message); diff --git a/DDK/KernelStd.h b/DDK/KernelStd.h new file mode 100644 index 00000000..b0dba70e --- /dev/null +++ b/DDK/KernelStd.h @@ -0,0 +1,22 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + + Purpose: Kernel Definitions. + +------------------------------------------- */ + +#pragma once + +#if defined(__cplusplus) +#define DK_EXTERN extern "C" +#define NIL nullptr +#else +#define DK_EXTERN extern +#define NIL NULL +#endif // defined(__cplusplus) + +#include +#include + +DK_EXTERN void* kernelCall(const char* kernelRpcName, int32_t cnt, ...); diff --git a/DDK/KernelStdCxx.cc b/DDK/KernelStdCxx.cc new file mode 100644 index 00000000..7d8c9ff3 --- /dev/null +++ b/DDK/KernelStdCxx.cc @@ -0,0 +1,24 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + + Purpose: Driver C++ Definitions. + +------------------------------------------- */ + +#include + +void* operator new(size_t sz) { + if (!sz) ++sz; + + auto ptr = kernelCall("NewKernelHeap", 1, sz); + kernelCall("ProtectKernelHeap", 1, ptr); + + return ptr; +} + +void operator delete(void* ptr) noexcept { + if (!ptr) return; + + kernelCall("DeleteKernelHeap", 1,ptr); +} diff --git a/DDK/KernelString.c b/DDK/KernelString.c new file mode 100644 index 00000000..1bd4c8c6 --- /dev/null +++ b/DDK/KernelString.c @@ -0,0 +1,34 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + + Purpose: Kernel Strings. + +------------------------------------------- */ + +#include + +DK_EXTERN size_t kernelStringLength(const char* str) +{ + size_t index = 0; + + while (str[index] != 0) + { + ++index; + } + + return index; +} + +DK_EXTERN int kernelStringCopy(char* dst, const char* src, size_t len) +{ + size_t index = 0; + + while (index != len) + { + dst[index] = src[index]; + ++index; + } + + return index; +} diff --git a/DDK/KernelString.h b/DDK/KernelString.h new file mode 100644 index 00000000..d39aeea1 --- /dev/null +++ b/DDK/KernelString.h @@ -0,0 +1,16 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + + Purpose: Kernel Strings. + +------------------------------------------- */ + +#pragma once + +#include + +/// @brief DDK equivalent of POSIX's string.h. + +DK_EXTERN size_t kernelStringLength(const char* str); +DK_EXTERN int kernelStringCopy(char* dst, const char* src, size_t len); diff --git a/DDK/ReadMe.txt b/DDK/ReadMe.txt new file mode 100644 index 00000000..e33c88ec --- /dev/null +++ b/DDK/ReadMe.txt @@ -0,0 +1,4 @@ +== The Driver Kit == + +-> A kit used to write user level drivers. +-> Use MPCC to compile the code, it can work on a patched GNU compiler. diff --git a/DriverKit/KernelCall.c b/DriverKit/KernelCall.c deleted file mode 100644 index d61f8439..00000000 --- a/DriverKit/KernelCall.c +++ /dev/null @@ -1,25 +0,0 @@ -/* ------------------------------------------- - - Copyright SoftwareLabs - - Purpose: Kernel Definitions. - -------------------------------------------- */ - -#include -#include - -DK_EXTERN __attribute__((naked)) void __kernelDispatchCall(int32_t cnt, ...); - -DK_EXTERN void* kernelCall(const char* kernelRpcName, int32_t cnt, ...) -{ - if (!kernelRpcName || cnt == 0) - return NIL; - - va_list arg; - va_start(arg, cnt); - - __kernelDispatchCall(cnt, arg); - - va_end(arg); -} diff --git a/DriverKit/KernelDev.c b/DriverKit/KernelDev.c deleted file mode 100644 index 704f17da..00000000 --- a/DriverKit/KernelDev.c +++ /dev/null @@ -1,37 +0,0 @@ -/* ------------------------------------------- - - Copyright SoftwareLabs - - Purpose: Kernel Text I/O. - -------------------------------------------- */ - -#include - -/// @brief Open a new binary device from path. -DK_EXTERN kernelDeviceRef kernelOpenBinaryDevice(const char* devicePath) -{ - if (!devicePath) - return NIL; - - return kernelCall("OpenBinaryDevice", 1, devicePath); -} - -/// @brief Open a new character device from path. -DK_EXTERN kernelDeviceRef kernelOpenCharDevice(const char* devicePath) -{ - if (!devicePath) - return NIL; - - return kernelCall("OpenCharDevice", 1, devicePath); -} - -/// @brief Close any device. -/// @param device valid device. -DK_EXTERN void kernelCloseDevice(kernelDeviceRef device) -{ - if (!device) - return; - - kernelCall("CloseDevice", 1, device); -} diff --git a/DriverKit/KernelDev.h b/DriverKit/KernelDev.h deleted file mode 100644 index c3435e8d..00000000 --- a/DriverKit/KernelDev.h +++ /dev/null @@ -1,33 +0,0 @@ -/* ------------------------------------------- - - Copyright SoftwareLabs - - Purpose: Kernel Devices. - -------------------------------------------- */ - -#pragma once - -#include - -struct _kernelDevice; - -/// @brief Kernel Device driver. -typedef struct _kernelDevice -{ - char name[255]; // the device name. Could be /./DEVICE_NAME/ - int32_t (*read)(); // read from device. - int32_t (*write)(); // write to device. - struct _kernelDevice* (*open)(const char* path); // open device. - void (*close)(struct _kernelDevice* dev); // close device. -} kernelDevice, *kernelDeviceRef; - -/// @brief Open a new binary device from path. -DK_EXTERN kernelDeviceRef kernelOpenBinaryDevice(const char* devicePath); - -/// @brief Open a new character device from path. -DK_EXTERN kernelDeviceRef kernelOpenCharDevice(const char* devicePath); - -/// @brief Close any device. -/// @param device valid device. -DK_EXTERN void kernelCloseDevice(kernelDeviceRef device); diff --git a/DriverKit/KernelDispatchCall.S b/DriverKit/KernelDispatchCall.S deleted file mode 100644 index 64b6663e..00000000 --- a/DriverKit/KernelDispatchCall.S +++ /dev/null @@ -1,21 +0,0 @@ -.globl __kernelDispatchCall - -.section .text - -/* Really simple function, takes our va-list, - and brings it to the trap handler in the kernel. */ - -#ifdef __x86_64__ - -__kernelDispatchCall: - int $0x33 - ret - -#elif defined(__powerpc64__) - -__kernelDispatchCall: - /* There is no specific interrupt request id for a system call in POWER. */ - sc - blr - -#endif diff --git a/DriverKit/KernelPrint.c b/DriverKit/KernelPrint.c deleted file mode 100644 index 533bf34a..00000000 --- a/DriverKit/KernelPrint.c +++ /dev/null @@ -1,33 +0,0 @@ -/* ------------------------------------------- - - Copyright SoftwareLabs - - Purpose: Kernel Text I/O. - -------------------------------------------- */ - -#include - -DK_EXTERN void kernelPrintChar(const char ch) -{ - kernelCall("WriteCharacter", 1, ch); -} - -/// @brief print string to UART. -/// @param message UART to transmit. -DK_EXTERN void kernelPrintStr(const char* message) -{ - if (!message) - return; - if (*message == 0) - return; - - size_t index = 0; - size_t len = kernelStringLength(message); - - while (index < len) - { - kernelPrintChar(message[index]); - ++index; - } -} diff --git a/DriverKit/KernelPrint.h b/DriverKit/KernelPrint.h deleted file mode 100644 index 7bf0dda9..00000000 --- a/DriverKit/KernelPrint.h +++ /dev/null @@ -1,18 +0,0 @@ -/* ------------------------------------------- - - Copyright SoftwareLabs - - Purpose: Kernel Text I/O. - -------------------------------------------- */ - -#pragma once - -#include - -/// @brief print character into UART. -DK_EXTERN void kernelPrintChar(const char ch); - -/// @brief print string to UART. -/// @param message UART to transmit. -DK_EXTERN void kernelPrintStr(const char* message); diff --git a/DriverKit/KernelStd.h b/DriverKit/KernelStd.h deleted file mode 100644 index b0dba70e..00000000 --- a/DriverKit/KernelStd.h +++ /dev/null @@ -1,22 +0,0 @@ -/* ------------------------------------------- - - Copyright SoftwareLabs - - Purpose: Kernel Definitions. - -------------------------------------------- */ - -#pragma once - -#if defined(__cplusplus) -#define DK_EXTERN extern "C" -#define NIL nullptr -#else -#define DK_EXTERN extern -#define NIL NULL -#endif // defined(__cplusplus) - -#include -#include - -DK_EXTERN void* kernelCall(const char* kernelRpcName, int32_t cnt, ...); diff --git a/DriverKit/KernelStdCxx.cc b/DriverKit/KernelStdCxx.cc deleted file mode 100644 index 633437dc..00000000 --- a/DriverKit/KernelStdCxx.cc +++ /dev/null @@ -1,24 +0,0 @@ -/* ------------------------------------------- - - Copyright SoftwareLabs - - Purpose: Driver C++ Definitions. - -------------------------------------------- */ - -#include - -void* operator new(size_t sz) { - if (!sz) ++sz; - - auto ptr = kernelCall("NewKernelHeap", 1, sz); - kernelCall("ProtectKernelHeap", 1, ptr); - - return ptr; -} - -void operator delete(void* ptr) noexcept { - if (!ptr) return; - - kernelCall("DeleteKernelHeap", 1,ptr); -} diff --git a/DriverKit/KernelString.c b/DriverKit/KernelString.c deleted file mode 100644 index 6f28f4d8..00000000 --- a/DriverKit/KernelString.c +++ /dev/null @@ -1,34 +0,0 @@ -/* ------------------------------------------- - - Copyright SoftwareLabs - - Purpose: Kernel Strings. - -------------------------------------------- */ - -#include - -DK_EXTERN size_t kernelStringLength(const char* str) -{ - size_t index = 0; - - while (str[index] != 0) - { - ++index; - } - - return index; -} - -DK_EXTERN int kernelStringCopy(char* dst, const char* src, size_t len) -{ - size_t index = 0; - - while (index != len) - { - dst[index] = src[index]; - ++index; - } - - return index; -} diff --git a/DriverKit/KernelString.h b/DriverKit/KernelString.h deleted file mode 100644 index db063a1b..00000000 --- a/DriverKit/KernelString.h +++ /dev/null @@ -1,16 +0,0 @@ -/* ------------------------------------------- - - Copyright SoftwareLabs - - Purpose: Kernel Strings. - -------------------------------------------- */ - -#pragma once - -#include - -/// @brief DriverKit equivalent of POSIX's string.h. - -DK_EXTERN size_t kernelStringLength(const char* str); -DK_EXTERN int kernelStringCopy(char* dst, const char* src, size_t len); diff --git a/DriverKit/ReadMe.txt b/DriverKit/ReadMe.txt deleted file mode 100644 index e33c88ec..00000000 --- a/DriverKit/ReadMe.txt +++ /dev/null @@ -1,4 +0,0 @@ -== The Driver Kit == - --> A kit used to write user level drivers. --> Use MPCC to compile the code, it can work on a patched GNU compiler. diff --git a/Drivers/.gitkeep b/Drivers/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/Drivers/Bonjour/Bonjour.c b/Drivers/Bonjour/Bonjour.c deleted file mode 100644 index 52187ac1..00000000 --- a/Drivers/Bonjour/Bonjour.c +++ /dev/null @@ -1,20 +0,0 @@ -/* ------------------------------------------- - - Copyright SoftwareLabs - -------------------------------------------- */ - -#include -#include - -int __ImageStart(void) -{ - kernelPrintStr("Bonjour: Starting up zeroconf...\r"); - return 0; -} - -int __ImageEnd(void) -{ - kernelPrintStr("Bonjour: Shutting down zeroconf...\r"); - return 0; -} diff --git a/Drivers/Bonjour/DriverRsrc.rsrc b/Drivers/Bonjour/DriverRsrc.rsrc deleted file mode 100644 index 494e85ee..00000000 --- a/Drivers/Bonjour/DriverRsrc.rsrc +++ /dev/null @@ -1,25 +0,0 @@ -1 ICON "../../Root/Boot/Icons/bonjour-logo.ico" - -1 VERSIONINFO -FILEVERSION 1,0,0,0 -PRODUCTVERSION 1,0,0,0 -BEGIN - BLOCK "StringFileInfo" - BEGIN - BLOCK "080904E4" - BEGIN - VALUE "CompanyName", "SoftwareLabs" - VALUE "FileDescription", "New OS Bonjour driver." - VALUE "FileVersion", "1.00" - VALUE "InternalName", "Bonjour." - VALUE "LegalCopyright", "Copyright SoftwareLabs, all rights reserved." - VALUE "OriginalFilename", "Bonjour.exe" - VALUE "ProductName", "Bonjour." - VALUE "ProductVersion", "1.00" - END - END - BLOCK "VarFileInfo" - BEGIN - VALUE "Translation", 0x809, 1252 - END -END diff --git a/Drivers/Bonjour/x86_64.mk b/Drivers/Bonjour/x86_64.mk deleted file mode 100644 index afbc70ad..00000000 --- a/Drivers/Bonjour/x86_64.mk +++ /dev/null @@ -1,51 +0,0 @@ -################################################## -# (C) SoftwareLabs, all rights reserved. -# This is the sample driver makefile. -################################################## - -CC_GNU=x86_64-w64-mingw32-gcc -LD_GNU=x86_64-w64-mingw32-ld - -WINDRES=x86_64-w64-mingw32-windres - -ADD_FILE=touch -COPY=cp -HTTP_GET=wget - -LD_FLAGS=-e __ImageStart --subsystem=17 - -OBJ=*.o - - -REM=rm -REM_FLAG=-f - -FLAG_ASM=-f win64 -FLAG_GNU=-fshort-wchar -D__EFI_x86_64__ -mgeneral-regs-only -mno-red-zone -D__KERNEL__ -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -std=c17 -D__HAVE_MAHROUSS_APIS__ -D__MAHROUSS__ -D__BOOTLOADER__ -I./ - -.PHONY: invalid-recipe -invalid-recipe: - @echo "invalid-recipe: Use make all instead." - -.PHONY: all -all: compile-amd64 - $(LD_GNU) $(OBJ) $(LD_FLAGS) -o Bonjour.exe - -ifneq ($(DEBUG_SUPPORT), ) -DEBUG = -D__DEBUG__ -endif - -.PHONY: compile-amd64 -compile-amd64: - $(WINDRES) DriverRsrc.rsrc -O coff -o DriverRsrc.o - $(CC_GNU) $(FLAG_GNU) $(DEBUG) $(wildcard *.c) $(wildcard ../../DriverKit/*.c) $(wildcard ../../DriverKit/*.S) - -.PHONY: clean -clean: - $(REM) $(REM_FLAG) $(OBJ) Bonjour.exe - -.PHONY: help -help: - @echo "=== HELP ===" - @echo "clean: Clean driver." - @echo "compile-amd64: Build driver." diff --git a/Drivers/DynamicLoader/DriverRsrc.rsrc b/Drivers/DynamicLoader/DriverRsrc.rsrc deleted file mode 100644 index 3a3b8774..00000000 --- a/Drivers/DynamicLoader/DriverRsrc.rsrc +++ /dev/null @@ -1,25 +0,0 @@ -1 ICON "../../Root/Boot/Icons/driver-logo.ico" - -1 VERSIONINFO -FILEVERSION 1,0,0,0 -PRODUCTVERSION 1,0,0,0 -BEGIN - BLOCK "StringFileInfo" - BEGIN - BLOCK "080904E4" - BEGIN - VALUE "CompanyName", "SoftwareLabs" - VALUE "FileDescription", "New OS driver." - VALUE "FileVersion", "1.00" - VALUE "InternalName", "SampleDriver" - VALUE "LegalCopyright", "Copyright SoftwareLabs, all rights reserved." - VALUE "OriginalFilename", "SampleDriver.exe" - VALUE "ProductName", "SampleDriver" - VALUE "ProductVersion", "1.00" - END - END - BLOCK "VarFileInfo" - BEGIN - VALUE "Translation", 0x809, 1252 - END -END diff --git a/Drivers/DynamicLoader/DynamicLoader.c b/Drivers/DynamicLoader/DynamicLoader.c deleted file mode 100644 index 16ffc06e..00000000 --- a/Drivers/DynamicLoader/DynamicLoader.c +++ /dev/null @@ -1,26 +0,0 @@ -/* ------------------------------------------- - - Copyright SoftwareLabs - -------------------------------------------- */ - -#include -#include - -int __ImageStart(void) -{ - kernelPrintStr("DynamicLoader: Starting up...\r"); - return 0; -} - -int __ImageEnd(void) -{ - kernelPrintStr("DynamicLoader: Shutting down...\r"); - return 0; -} - -///! @brief Use this to check your stack, if using MinGW/MSVC. -void ___chkstk_ms(void) -{ - (void)0; -} diff --git a/Drivers/DynamicLoader/x86_64.mk b/Drivers/DynamicLoader/x86_64.mk deleted file mode 100644 index 1d46948b..00000000 --- a/Drivers/DynamicLoader/x86_64.mk +++ /dev/null @@ -1,51 +0,0 @@ -################################################## -# (C) SoftwareLabs, all rights reserved. -# This is the sample driver makefile. -################################################## - -CC_GNU=x86_64-w64-mingw32-gcc -LD_GNU=x86_64-w64-mingw32-ld - -WINDRES=x86_64-w64-mingw32-windres - -ADD_FILE=touch -COPY=cp -HTTP_GET=wget - -LD_FLAGS=-e __ImageStart --subsystem=17 - -OBJ=*.o - - -REM=rm -REM_FLAG=-f - -FLAG_ASM=-f win64 -FLAG_GNU=-fshort-wchar -D__EFI_x86_64__ -mgeneral-regs-only -mno-red-zone -D__KERNEL__ -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -std=c17 -D__HAVE_MAHROUSS_APIS__ -D__MAHROUSS__ -D__BOOTLOADER__ -I./ - -.PHONY: invalid-recipe -invalid-recipe: - @echo "invalid-recipe: Use make all instead." - -.PHONY: all -all: compile-amd64 - $(LD_GNU) $(OBJ) $(LD_FLAGS) -o SampleDriver.exe - -ifneq ($(DEBUG_SUPPORT), ) -DEBUG = -D__DEBUG__ -endif - -.PHONY: compile-amd64 -compile-amd64: - $(WINDRES) DriverRsrc.rsrc -O coff -o DriverRsrc.o - $(CC_GNU) $(FLAG_GNU) $(DEBUG) $(wildcard *.c) $(wildcard ../../DriverKit/*.c) $(wildcard ../../DriverKit/*.S) - -.PHONY: clean -clean: - $(REM) $(REM_FLAG) $(OBJ) SampleDriver.exe - -.PHONY: help -help: - @echo "=== HELP ===" - @echo "clean: Clean driver." - @echo "compile-amd64: Build driver." diff --git a/Drivers/SampleDriver/DriverRsrc.rsrc b/Drivers/SampleDriver/DriverRsrc.rsrc deleted file mode 100644 index 3a3b8774..00000000 --- a/Drivers/SampleDriver/DriverRsrc.rsrc +++ /dev/null @@ -1,25 +0,0 @@ -1 ICON "../../Root/Boot/Icons/driver-logo.ico" - -1 VERSIONINFO -FILEVERSION 1,0,0,0 -PRODUCTVERSION 1,0,0,0 -BEGIN - BLOCK "StringFileInfo" - BEGIN - BLOCK "080904E4" - BEGIN - VALUE "CompanyName", "SoftwareLabs" - VALUE "FileDescription", "New OS driver." - VALUE "FileVersion", "1.00" - VALUE "InternalName", "SampleDriver" - VALUE "LegalCopyright", "Copyright SoftwareLabs, all rights reserved." - VALUE "OriginalFilename", "SampleDriver.exe" - VALUE "ProductName", "SampleDriver" - VALUE "ProductVersion", "1.00" - END - END - BLOCK "VarFileInfo" - BEGIN - VALUE "Translation", 0x809, 1252 - END -END diff --git a/Drivers/SampleDriver/SampleDriver.c b/Drivers/SampleDriver/SampleDriver.c deleted file mode 100644 index 4085bf4f..00000000 --- a/Drivers/SampleDriver/SampleDriver.c +++ /dev/null @@ -1,26 +0,0 @@ -/* ------------------------------------------- - - Copyright SoftwareLabs - -------------------------------------------- */ - -#include -#include - -int __ImageStart(void) -{ - kernelPrintStr("SampleDriver: Starting up...\r"); - return 0; -} - -int __ImageEnd(void) -{ - kernelPrintStr("SampleDriver: Shutting down...\r"); - return 0; -} - -///! @brief Use this to check your stack, if using MinGW/MSVC. -void ___chkstk_ms(void) -{ - (void)0; -} diff --git a/Drivers/SampleDriver/x86_64.mk b/Drivers/SampleDriver/x86_64.mk deleted file mode 100644 index 1d46948b..00000000 --- a/Drivers/SampleDriver/x86_64.mk +++ /dev/null @@ -1,51 +0,0 @@ -################################################## -# (C) SoftwareLabs, all rights reserved. -# This is the sample driver makefile. -################################################## - -CC_GNU=x86_64-w64-mingw32-gcc -LD_GNU=x86_64-w64-mingw32-ld - -WINDRES=x86_64-w64-mingw32-windres - -ADD_FILE=touch -COPY=cp -HTTP_GET=wget - -LD_FLAGS=-e __ImageStart --subsystem=17 - -OBJ=*.o - - -REM=rm -REM_FLAG=-f - -FLAG_ASM=-f win64 -FLAG_GNU=-fshort-wchar -D__EFI_x86_64__ -mgeneral-regs-only -mno-red-zone -D__KERNEL__ -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -std=c17 -D__HAVE_MAHROUSS_APIS__ -D__MAHROUSS__ -D__BOOTLOADER__ -I./ - -.PHONY: invalid-recipe -invalid-recipe: - @echo "invalid-recipe: Use make all instead." - -.PHONY: all -all: compile-amd64 - $(LD_GNU) $(OBJ) $(LD_FLAGS) -o SampleDriver.exe - -ifneq ($(DEBUG_SUPPORT), ) -DEBUG = -D__DEBUG__ -endif - -.PHONY: compile-amd64 -compile-amd64: - $(WINDRES) DriverRsrc.rsrc -O coff -o DriverRsrc.o - $(CC_GNU) $(FLAG_GNU) $(DEBUG) $(wildcard *.c) $(wildcard ../../DriverKit/*.c) $(wildcard ../../DriverKit/*.S) - -.PHONY: clean -clean: - $(REM) $(REM_FLAG) $(OBJ) SampleDriver.exe - -.PHONY: help -help: - @echo "=== HELP ===" - @echo "clean: Clean driver." - @echo "compile-amd64: Build driver." diff --git a/Drv/.gitkeep b/Drv/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Drv/Bonjour/Bonjour.c b/Drv/Bonjour/Bonjour.c new file mode 100644 index 00000000..93411e1f --- /dev/null +++ b/Drv/Bonjour/Bonjour.c @@ -0,0 +1,20 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + +------------------------------------------- */ + +#include +#include + +int __ImageStart(void) +{ + kernelPrintStr("Bonjour: Starting up zeroconf...\r"); + return 0; +} + +int __ImageEnd(void) +{ + kernelPrintStr("Bonjour: Shutting down zeroconf...\r"); + return 0; +} diff --git a/Drv/Bonjour/DriverRsrc.rsrc b/Drv/Bonjour/DriverRsrc.rsrc new file mode 100644 index 00000000..494e85ee --- /dev/null +++ b/Drv/Bonjour/DriverRsrc.rsrc @@ -0,0 +1,25 @@ +1 ICON "../../Root/Boot/Icons/bonjour-logo.ico" + +1 VERSIONINFO +FILEVERSION 1,0,0,0 +PRODUCTVERSION 1,0,0,0 +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "080904E4" + BEGIN + VALUE "CompanyName", "SoftwareLabs" + VALUE "FileDescription", "New OS Bonjour driver." + VALUE "FileVersion", "1.00" + VALUE "InternalName", "Bonjour." + VALUE "LegalCopyright", "Copyright SoftwareLabs, all rights reserved." + VALUE "OriginalFilename", "Bonjour.exe" + VALUE "ProductName", "Bonjour." + VALUE "ProductVersion", "1.00" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x809, 1252 + END +END diff --git a/Drv/Bonjour/x86_64.mk b/Drv/Bonjour/x86_64.mk new file mode 100644 index 00000000..6a020d28 --- /dev/null +++ b/Drv/Bonjour/x86_64.mk @@ -0,0 +1,51 @@ +################################################## +# (C) SoftwareLabs, all rights reserved. +# This is the sample driver makefile. +################################################## + +CC_GNU=x86_64-w64-mingw32-gcc +LD_GNU=x86_64-w64-mingw32-ld + +WINDRES=x86_64-w64-mingw32-windres + +ADD_FILE=touch +COPY=cp +HTTP_GET=wget + +LD_FLAGS=-e __ImageStart --subsystem=17 + +OBJ=*.o + + +REM=rm +REM_FLAG=-f + +FLAG_ASM=-f win64 +FLAG_GNU=-fshort-wchar -D__EFI_x86_64__ -mgeneral-regs-only -mno-red-zone -D__KERNEL__ -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -std=c17 -D__HAVE_MAHROUSS_APIS__ -D__MAHROUSS__ -D__BOOTLOADER__ -I./ + +.PHONY: invalid-recipe +invalid-recipe: + @echo "invalid-recipe: Use make all instead." + +.PHONY: all +all: compile-amd64 + $(LD_GNU) $(OBJ) $(LD_FLAGS) -o Bonjour.exe + +ifneq ($(DEBUG_SUPPORT), ) +DEBUG = -D__DEBUG__ +endif + +.PHONY: compile-amd64 +compile-amd64: + $(WINDRES) DriverRsrc.rsrc -O coff -o DriverRsrc.o + $(CC_GNU) $(FLAG_GNU) $(DEBUG) $(wildcard *.c) $(wildcard ../../DDK/*.c) $(wildcard ../../DDK/*.S) + +.PHONY: clean +clean: + $(REM) $(REM_FLAG) $(OBJ) Bonjour.exe + +.PHONY: help +help: + @echo "=== HELP ===" + @echo "clean: Clean driver." + @echo "compile-amd64: Build driver." diff --git a/Drv/DynamicLoader/DriverRsrc.rsrc b/Drv/DynamicLoader/DriverRsrc.rsrc new file mode 100644 index 00000000..3a3b8774 --- /dev/null +++ b/Drv/DynamicLoader/DriverRsrc.rsrc @@ -0,0 +1,25 @@ +1 ICON "../../Root/Boot/Icons/driver-logo.ico" + +1 VERSIONINFO +FILEVERSION 1,0,0,0 +PRODUCTVERSION 1,0,0,0 +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "080904E4" + BEGIN + VALUE "CompanyName", "SoftwareLabs" + VALUE "FileDescription", "New OS driver." + VALUE "FileVersion", "1.00" + VALUE "InternalName", "SampleDriver" + VALUE "LegalCopyright", "Copyright SoftwareLabs, all rights reserved." + VALUE "OriginalFilename", "SampleDriver.exe" + VALUE "ProductName", "SampleDriver" + VALUE "ProductVersion", "1.00" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x809, 1252 + END +END diff --git a/Drv/DynamicLoader/DynamicLoader.c b/Drv/DynamicLoader/DynamicLoader.c new file mode 100644 index 00000000..83ca555e --- /dev/null +++ b/Drv/DynamicLoader/DynamicLoader.c @@ -0,0 +1,26 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + +------------------------------------------- */ + +#include +#include + +int __ImageStart(void) +{ + kernelPrintStr("DynamicLoader: Starting up...\r"); + return 0; +} + +int __ImageEnd(void) +{ + kernelPrintStr("DynamicLoader: Shutting down...\r"); + return 0; +} + +///! @brief Use this to check your stack, if using MinGW/MSVC. +void ___chkstk_ms(void) +{ + (void)0; +} diff --git a/Drv/DynamicLoader/x86_64.mk b/Drv/DynamicLoader/x86_64.mk new file mode 100644 index 00000000..54ee54f0 --- /dev/null +++ b/Drv/DynamicLoader/x86_64.mk @@ -0,0 +1,51 @@ +################################################## +# (C) SoftwareLabs, all rights reserved. +# This is the sample driver makefile. +################################################## + +CC_GNU=x86_64-w64-mingw32-gcc +LD_GNU=x86_64-w64-mingw32-ld + +WINDRES=x86_64-w64-mingw32-windres + +ADD_FILE=touch +COPY=cp +HTTP_GET=wget + +LD_FLAGS=-e __ImageStart --subsystem=17 + +OBJ=*.o + + +REM=rm +REM_FLAG=-f + +FLAG_ASM=-f win64 +FLAG_GNU=-fshort-wchar -D__EFI_x86_64__ -mgeneral-regs-only -mno-red-zone -D__KERNEL__ -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -std=c17 -D__HAVE_MAHROUSS_APIS__ -D__MAHROUSS__ -D__BOOTLOADER__ -I./ + +.PHONY: invalid-recipe +invalid-recipe: + @echo "invalid-recipe: Use make all instead." + +.PHONY: all +all: compile-amd64 + $(LD_GNU) $(OBJ) $(LD_FLAGS) -o SampleDriver.exe + +ifneq ($(DEBUG_SUPPORT), ) +DEBUG = -D__DEBUG__ +endif + +.PHONY: compile-amd64 +compile-amd64: + $(WINDRES) DriverRsrc.rsrc -O coff -o DriverRsrc.o + $(CC_GNU) $(FLAG_GNU) $(DEBUG) $(wildcard *.c) $(wildcard ../../DDK/*.c) $(wildcard ../../DDK/*.S) + +.PHONY: clean +clean: + $(REM) $(REM_FLAG) $(OBJ) SampleDriver.exe + +.PHONY: help +help: + @echo "=== HELP ===" + @echo "clean: Clean driver." + @echo "compile-amd64: Build driver." diff --git a/Drv/SampleDriver/DriverRsrc.rsrc b/Drv/SampleDriver/DriverRsrc.rsrc new file mode 100644 index 00000000..3a3b8774 --- /dev/null +++ b/Drv/SampleDriver/DriverRsrc.rsrc @@ -0,0 +1,25 @@ +1 ICON "../../Root/Boot/Icons/driver-logo.ico" + +1 VERSIONINFO +FILEVERSION 1,0,0,0 +PRODUCTVERSION 1,0,0,0 +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "080904E4" + BEGIN + VALUE "CompanyName", "SoftwareLabs" + VALUE "FileDescription", "New OS driver." + VALUE "FileVersion", "1.00" + VALUE "InternalName", "SampleDriver" + VALUE "LegalCopyright", "Copyright SoftwareLabs, all rights reserved." + VALUE "OriginalFilename", "SampleDriver.exe" + VALUE "ProductName", "SampleDriver" + VALUE "ProductVersion", "1.00" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x809, 1252 + END +END diff --git a/Drv/SampleDriver/SampleDriver.c b/Drv/SampleDriver/SampleDriver.c new file mode 100644 index 00000000..821927be --- /dev/null +++ b/Drv/SampleDriver/SampleDriver.c @@ -0,0 +1,26 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + +------------------------------------------- */ + +#include +#include + +int __ImageStart(void) +{ + kernelPrintStr("SampleDriver: Starting up...\r"); + return 0; +} + +int __ImageEnd(void) +{ + kernelPrintStr("SampleDriver: Shutting down...\r"); + return 0; +} + +///! @brief Use this to check your stack, if using MinGW/MSVC. +void ___chkstk_ms(void) +{ + (void)0; +} diff --git a/Drv/SampleDriver/x86_64.mk b/Drv/SampleDriver/x86_64.mk new file mode 100644 index 00000000..54ee54f0 --- /dev/null +++ b/Drv/SampleDriver/x86_64.mk @@ -0,0 +1,51 @@ +################################################## +# (C) SoftwareLabs, all rights reserved. +# This is the sample driver makefile. +################################################## + +CC_GNU=x86_64-w64-mingw32-gcc +LD_GNU=x86_64-w64-mingw32-ld + +WINDRES=x86_64-w64-mingw32-windres + +ADD_FILE=touch +COPY=cp +HTTP_GET=wget + +LD_FLAGS=-e __ImageStart --subsystem=17 + +OBJ=*.o + + +REM=rm +REM_FLAG=-f + +FLAG_ASM=-f win64 +FLAG_GNU=-fshort-wchar -D__EFI_x86_64__ -mgeneral-regs-only -mno-red-zone -D__KERNEL__ -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -std=c17 -D__HAVE_MAHROUSS_APIS__ -D__MAHROUSS__ -D__BOOTLOADER__ -I./ + +.PHONY: invalid-recipe +invalid-recipe: + @echo "invalid-recipe: Use make all instead." + +.PHONY: all +all: compile-amd64 + $(LD_GNU) $(OBJ) $(LD_FLAGS) -o SampleDriver.exe + +ifneq ($(DEBUG_SUPPORT), ) +DEBUG = -D__DEBUG__ +endif + +.PHONY: compile-amd64 +compile-amd64: + $(WINDRES) DriverRsrc.rsrc -O coff -o DriverRsrc.o + $(CC_GNU) $(FLAG_GNU) $(DEBUG) $(wildcard *.c) $(wildcard ../../DDK/*.c) $(wildcard ../../DDK/*.S) + +.PHONY: clean +clean: + $(REM) $(REM_FLAG) $(OBJ) SampleDriver.exe + +.PHONY: help +help: + @echo "=== HELP ===" + @echo "clean: Clean driver." + @echo "compile-amd64: Build driver." diff --git a/Kernel/Sources/ProcessScheduler.cxx b/Kernel/Sources/ProcessScheduler.cxx index 63d566b7..32e1fbde 100644 --- a/Kernel/Sources/ProcessScheduler.cxx +++ b/Kernel/Sources/ProcessScheduler.cxx @@ -42,7 +42,7 @@ namespace NewOS void ProcessHeader::Crash() { - kcout << this->Name << ": crashed. (id = " << number(39); + kcout << this->Name << ": crashed. (id = " << number(kErrorProcessFault); kcout << ")\r"; if (this->Ring != kRingUserKind) -- cgit v1.2.3 From fc0d38259fd6670966b916b1f28a11f3cb2a4c45 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sat, 1 Jun 2024 09:29:31 +0200 Subject: MHR-23: SMP: Add hal_send_start_ipi and hal_send_end_ipi, as well as DMA utilities. Signed-off-by: Amlal El Mahrouss --- Boot/makefile | 2 +- Drv/DynamicLoader/DynamicLoader.c | 6 +- Kernel/ArchKit/ArchKit.hpp | 41 +++ Kernel/Builtins/ACPI/ACPI.hxx | 23 +- Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx | 29 +- .../HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp | 2 +- .../HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp | 67 ++++- Kernel/HALKit/AMD64/HalKernelMain.cxx | 2 +- Kernel/HALKit/AMD64/HalSMPCore.cxx | 2 +- Kernel/HALKit/AMD64/Processor.hpp | 65 +++++ Kernel/KernelKit/KernelHeap.hpp | 5 + Kernel/KernelKit/ProcessScheduler.hpp | 303 --------------------- Kernel/KernelKit/ProcessScheduler.hxx | 303 +++++++++++++++++++++ Kernel/KernelKit/ThreadLocalStorage.inl | 2 +- Kernel/Sources/AppMain.cxx | 26 +- Kernel/Sources/CodeManager.cxx | 2 +- Kernel/Sources/KernelHeap.cxx | 27 +- Kernel/Sources/PEFCodeManager.cxx | 2 +- Kernel/Sources/PEFSharedObjectRT.cxx | 2 +- Kernel/Sources/ProcessScheduler.cxx | 6 +- Kernel/Sources/ProcessTeam.cxx | 2 +- Kernel/Sources/SMPManager.cxx | 6 +- Kernel/Sources/Semaphore.cxx | 2 +- Kernel/Sources/ThreadLocalStorage.cxx | 2 +- Kernel/Sources/UserHeap.cxx | 2 +- Kernel/makefile | 2 +- 26 files changed, 579 insertions(+), 354 deletions(-) delete mode 100644 Kernel/KernelKit/ProcessScheduler.hpp create mode 100644 Kernel/KernelKit/ProcessScheduler.hxx (limited to 'Kernel/Sources/ProcessScheduler.cxx') diff --git a/Boot/makefile b/Boot/makefile index 6c38208d..5b1ab16b 100644 --- a/Boot/makefile +++ b/Boot/makefile @@ -26,7 +26,7 @@ BIOS=OVMF.fd IMG=epm.img IMG_2=epm-slave.img -EMU_FLAGS=-net none -smp 4,sockets=1,cores=4,threads=1 -m 4G -M q35 \ +EMU_FLAGS=-net none -smp 2 -m 4G -M q35 \ -bios $(BIOS) -device piix3-ide,id=ide \ -drive id=disk,file=$(IMG),format=raw,if=none \ -device ide-hd,drive=disk,bus=ide.0 -drive \ diff --git a/Drv/DynamicLoader/DynamicLoader.c b/Drv/DynamicLoader/DynamicLoader.c index 45083a5c..f7eb9458 100644 --- a/Drv/DynamicLoader/DynamicLoader.c +++ b/Drv/DynamicLoader/DynamicLoader.c @@ -7,19 +7,23 @@ #include #include +/// @brief Start function +/// @return status code int __ImageStart(void) { kernelPrintStr("SDLD: Starting up...\r"); return 0; } +/// @brief End function. +/// @return status code int __ImageEnd(void) { kernelPrintStr("SDLD: Shutting down...\r"); return 0; } -///! @brief Use this to check your stack, if using MinGW/MSVC. +///! @brief Check if stack has enough space. void ___chkstk_ms(void) { (void)0; diff --git a/Kernel/ArchKit/ArchKit.hpp b/Kernel/ArchKit/ArchKit.hpp index 83770272..fa899984 100644 --- a/Kernel/ArchKit/ArchKit.hpp +++ b/Kernel/ArchKit/ArchKit.hpp @@ -34,6 +34,47 @@ namespace NewOS return hash; } + + /// @brief write to mapped memory register + /// @param base the base address. + /// @param reg the register. + /// @param value the write to write on it. + inline void ke_dma_write(UInt32 base, UInt32 reg, UInt32 value) noexcept + { + *(volatile UInt32*)((UInt64)base + reg) = value; + } + + /// @brief read from mapped memory register. + /// @param base base address + /// @param reg the register. + /// @return the value inside the register. + inline UInt32 ke_dma_read(UInt32 base, UInt32 reg) noexcept + { + return *(volatile UInt32*)((UInt64)base + reg); + } + + /// @brief Print a region of memory. + /// @param start + /// @param length + inline void ke_print_raw_memory(const void* start, Size length) + { + const UInt8* ptr = (const UInt8*)start; + for (Size i = 0; i < length; i++) + { + if (i % 16 == 0) + { + kcout << hex_number((UIntPtr)ptr + i); + } + else + { + kcout << hex_number(ptr[i]); + } + + kcout << " "; + } + + kcout << "\r"; + } } // namespace NewOS #define kKernelMaxSystemCalls (256) diff --git a/Kernel/Builtins/ACPI/ACPI.hxx b/Kernel/Builtins/ACPI/ACPI.hxx index 13ea6ecc..d9bfeaca 100644 --- a/Kernel/Builtins/ACPI/ACPI.hxx +++ b/Kernel/Builtins/ACPI/ACPI.hxx @@ -15,7 +15,7 @@ namespace NewOS { - class SDT + class PACKED SDT { public: Char Signature[4]; @@ -29,7 +29,7 @@ namespace NewOS UInt32 CreatorRevision; }; - class RSDP : public SDT + class PACKED RSDP : public SDT { public: UInt32 RsdtAddress; @@ -38,7 +38,7 @@ namespace NewOS UInt8 Reserved0[3]; }; - class ConfigHeader + class PACKED ConfigHeader { public: UInt64 BaseAddress; @@ -59,7 +59,7 @@ namespace NewOS Invalid = 0xFF, }; - class Address + class PACKED Address { public: AddressSpace AddressSpaceId; @@ -68,6 +68,21 @@ namespace NewOS UInt8 Reserved; UIntPtr Address; }; + + class PACKED RSDT + { + public: + Char Signature[4]; + UInt32 Length; + UInt8 Revision; + Char Checksum; + Char OemId[6]; + Char OemTableId[8]; + UInt32 OemRev; + UInt32 CreatorID; + UInt32 CreatorRevision; + UInt64 AddressArr[]; + }; } // namespace NewOS #endif // !__ACPI__ diff --git a/Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx b/Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx index ff5372f4..f92554f8 100644 --- a/Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx +++ b/Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx @@ -7,6 +7,8 @@ #include #include #include +#include +#include namespace NewOS { @@ -73,28 +75,37 @@ namespace NewOS return ErrorOr{-4}; } - SDT* xsdt = (SDT*)(rsdPtr->XsdtAddress >> (rsdPtr->XsdtAddress & 0xFFF)); + /// FIXME + RSDT* xsdt = (RSDT*)rsdPtr->XsdtAddress; - SizeT num = -(xsdt->Length - sizeof(SDT)) / 8; + if (NewOS::HAL::ke_map_address((PDE*)hal_read_cr3(), rsdPtr->XsdtAddress, (UIntPtr)xsdt, NewOS::HAL::eFlagsRw)) + return ErrorOr{-5}; + + Int64 num = (xsdt->Length - sizeof(SDT)) / sizeof(UInt64); + + if (num < 1) + { + return ErrorOr{-6}; + } this->fEntries = num; - kcout << "ACPI: Number of entries: " << number(num) << endl; + kcout << "ACPI: Number of entries: " << number(this->fEntries) << endl; kcout << "ACPI: Address of XSDT: " << hex_number((UIntPtr)xsdt) << endl; - constexpr short ACPI_SIGNATURE_LENGTH = 4; + const short cAcpiSignatureLength = 4; for (Size index = 0; index < this->fEntries; ++index) { - SDT& sdt = xsdt[index]; + SDT* sdt = (SDT*)(xsdt->AddressArr[index]); - for (short signature_index = 0; signature_index < ACPI_SIGNATURE_LENGTH; ++signature_index) + for (short signature_index = 0; signature_index < cAcpiSignatureLength; ++signature_index) { - if (sdt.Signature[signature_index] != signature[signature_index]) + if (sdt->Signature[signature_index] != signature[signature_index]) break; - if (signature_index == 4) - return ErrorOr(reinterpret_cast(&sdt)); + if (signature_index == (cAcpiSignatureLength - 1)) + return ErrorOr(reinterpret_cast(sdt)); } } diff --git a/Kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp b/Kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp index 0ac7a50e..d06dc6a6 100644 --- a/Kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp +++ b/Kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp @@ -5,7 +5,7 @@ ------------------------------------------- */ #include -#include +#include #include /// @brief Handle GPF fault. diff --git a/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp b/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp index abd19586..287b1882 100644 --- a/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp +++ b/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp @@ -7,6 +7,12 @@ #include #include #include +#include + +#define kAPIC_ICR_Low 0x300 +#define kAPIC_ICR_High 0x310 +#define kAPIC_SIPI_Vector 0x00500 +#define kAPIC_EIPI_Vector 0x00400 /////////////////////////////////////////////////////////////////////////////////////// @@ -62,7 +68,7 @@ namespace NewOS::HAL struct MadtProcessorLocalApic final { Char AcpiProcessorId; - Char Reserved; + Char ApicId; UInt32 Flags; }; @@ -102,15 +108,66 @@ namespace NewOS::HAL /////////////////////////////////////////////////////////////////////////////////////// - void hal_system_get_cores(voidPtr rsdPtr) + /// @brief Send start IPI for CPU. + /// @param apicId + /// @param vector + /// @param targetAddress + /// @return + Void hal_send_start_ipi(UInt32 apicId, UInt8 vector, UInt32 targetAddress) + { + NewOS::ke_dma_write(targetAddress, kAPIC_ICR_High, apicId << 24); + NewOS::ke_dma_write(targetAddress, kAPIC_ICR_Low, kAPIC_SIPI_Vector | vector); + } + + /// @brief Send end IPI for CPU. + /// @param apicId + /// @param vector + /// @param targetAddress + /// @return + Void hal_send_end_ipi(UInt32 apicId, UInt8 vector, UInt32 targetAddress) + { + NewOS::ke_dma_write(targetAddress, kAPIC_ICR_High, apicId << 24); + NewOS::ke_dma_write(targetAddress, kAPIC_ICR_Low, kAPIC_EIPI_Vector | vector); + } + + Void hal_system_get_cores(voidPtr rsdPtr) { auto acpi = ACPIFactoryInterface(rsdPtr); kApicMadt = acpi.Find(kApicSignature).Leak().Leak(); - if (kApicMadt) + if (kApicMadt != nullptr) { - kcout << "New OS: APIC is present...\r"; - kApicInfoBlock = (MadtType*)kApicMadt; + auto madt = (SDT*)kApicMadt; + + const UInt8* madt_end = (const UInt8*)madt + madt->Length; + const UInt8* entry_ptr = (const UInt8*)(madt + 1); + + while (entry_ptr < madt_end) + { + const MadtType::MadtAddress* entry_header = (const MadtType::MadtAddress*)entry_ptr; + + switch (entry_header->Flags) + { + case 0: { + const MadtProcessorLocalApic* local_apic = (const MadtProcessorLocalApic*)entry_ptr; + if (local_apic->Flags & 1) + { + // Processor is enabled + kcout << "Processor ID: %d, APIC ID: %d\n" + << number(local_apic->AcpiProcessorId) << number(local_apic->ApicId); + } + break; + } + default: + break; + } + + entry_ptr += entry_header->RecordLen; + } + + while (true) + { + } } else { diff --git a/Kernel/HALKit/AMD64/HalKernelMain.cxx b/Kernel/HALKit/AMD64/HalKernelMain.cxx index 07656060..3136bf76 100644 --- a/Kernel/HALKit/AMD64/HalKernelMain.cxx +++ b/Kernel/HALKit/AMD64/HalKernelMain.cxx @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include diff --git a/Kernel/HALKit/AMD64/HalSMPCore.cxx b/Kernel/HALKit/AMD64/HalSMPCore.cxx index 90703e13..7aa13068 100644 --- a/Kernel/HALKit/AMD64/HalSMPCore.cxx +++ b/Kernel/HALKit/AMD64/HalSMPCore.cxx @@ -4,7 +4,7 @@ ------------------------------------------- */ -#include +#include using namespace NewOS; Void ProcessHeader::SetEntrypoint(UIntPtr& imageStart) noexcept diff --git a/Kernel/HALKit/AMD64/Processor.hpp b/Kernel/HALKit/AMD64/Processor.hpp index 235e425d..608a502f 100644 --- a/Kernel/HALKit/AMD64/Processor.hpp +++ b/Kernel/HALKit/AMD64/Processor.hpp @@ -17,6 +17,7 @@ #include #include #include +#include #ifdef kCPUBackendName #undef kCPUBackendName @@ -53,6 +54,68 @@ namespace NewOS namespace NewOS::HAL { + + enum + { + eFlagsUser, + eFlagsRw, + eFlagsExecDisable + }; + + /// @brief Map address to PDE. + /// @param pde a valid page directory. + /// @param phys_addr a valid phyiscal address. + /// @param virt_addr a valid virtual address. + /// @param flags the flags to put on the page. + inline Int32 ke_map_address(PDE* pde, UIntPtr phys_addr, UIntPtr virt_addr, UInt32 flags) + { + UInt16 pml4_index = (virt_addr >> 39) & 0x1FF; + + if (!pde->Pte[pml4_index].Present) + { + pde->Pte[pml4_index].Present = true; + kcout << "PM: It is present now.\r"; + } + else + { + kcout << "PM: It is already present.\r"; + kcout << "Address? " << hex_number(pde->Pte[pml4_index].PhysicalAddress) << endl; + kcout << "User? " << (pde->Pte[pml4_index].User ? "yes" : "no") << "\r"; + kcout << "RW? " << (pde->Pte[pml4_index].Rw ? "yes" : "no") << "\r"; + + return 1; + } + + pde->Pte[pml4_index].PhysicalAddress = phys_addr; + pde->Pte[pml4_index].Rw = flags & eFlagsRw; + pde->Pte[pml4_index].User = flags & eFlagsUser; + pde->Pte[pml4_index].ExecDisable = flags & eFlagsExecDisable; + + return 0; + } + + /// @brief Map address to PDE. + /// @param pde + /// @param phys_addr + /// @param virt_addr + /// @param flags + inline void ke_unmap_address(PDE* pde, UIntPtr phys_addr, UIntPtr virt_addr, UInt32 flags) + { + UInt16 pml4_index = (virt_addr >> 39) & 0x1FF; + UInt16 pdpt_index = (virt_addr >> 30) & 0x1FF; + UInt16 pd_index = (virt_addr >> 21) & 0x1FF; + UInt16 pt_index = (virt_addr >> 12) & 0x1FF; + + if (pde->Pte[pml4_index].Present) + { + pde->Pte[pml4_index].Present = false; + pde->Pte[pml4_index].PhysicalAddress = 0; + pde->Pte[pml4_index].Rw = 0; + pde->Pte[pml4_index].User = 0; + pde->Pte[pml4_index].ExecDisable = 0; + } + } + EXTERN_C UChar In8(UInt16 port); EXTERN_C UShort In16(UInt16 port); EXTERN_C UInt In32(UInt16 port); @@ -162,6 +225,8 @@ namespace NewOS::HAL }; Void hal_system_get_cores(VoidPtr rsdPtr); + Void hal_send_start_ipi(UInt32 apicId, UInt8 vector, UInt32 targetAddress); + Void hal_send_end_ipi(UInt32 apicId, UInt8 vector, UInt32 targetAddress); /// @brief Processor specific structures. namespace Detail diff --git a/Kernel/KernelKit/KernelHeap.hpp b/Kernel/KernelKit/KernelHeap.hpp index c7b898ed..e2ab19b5 100644 --- a/Kernel/KernelKit/KernelHeap.hpp +++ b/Kernel/KernelKit/KernelHeap.hpp @@ -36,4 +36,9 @@ namespace NewOS /// @param heapPtr HIB pointer. /// @return if it valid: point has crc now., otherwise fail. Boolean ke_protect_ke_heap(VoidPtr heapPtr); + + /// @brief Makes a kernel heap page. + /// @param heapPtr + /// @return + Int32 ke_make_ke_page(VoidPtr heapPtr); } // namespace NewOS diff --git a/Kernel/KernelKit/ProcessScheduler.hpp b/Kernel/KernelKit/ProcessScheduler.hpp deleted file mode 100644 index 9adaac0d..00000000 --- a/Kernel/KernelKit/ProcessScheduler.hpp +++ /dev/null @@ -1,303 +0,0 @@ -/* ------------------------------------------- - - Copyright SoftwareLabs - -------------------------------------------- */ - -#ifndef __PROCESS_SCHEDULER__ -#define __PROCESS_SCHEDULER__ - -#include -#include -#include -#include -#include -#include - -#define kSchedMinMicroTime AffinityKind::kHartStandard -#define kSchedInvalidPID (-1) - -#define kSchedProcessLimitPerTeam (100U) - -//////////////////////////////////////////////////// - -// LAST REV: Mon Feb 12 13:52:01 CET 2024 - -//////////////////////////////////////////////////// - -namespace NewOS -{ - class ProcessHeader; - class ProcessTeam; - class ProcessScheduler; - - //! @brief Process identifier. - typedef Int64 ProcessID; - - //! @brief Process name length. - inline constexpr SizeT kProcessLen = 256U; - - //! @brief Forward declaration. - class ProcessHeader; - class ProcessScheduler; - class ProcessHelper; - - //! @brief Process status enum. - enum class ProcessStatus : Int32 - { - kStarting, - kRunning, - kKilled, - kFrozen, - kDead - }; - - //! @brief Affinity is the amount of nano-seconds this process is going - //! to run. - enum class AffinityKind : Int32 - { - kInvalid = 300, - kVeryHigh = 250, - kHigh = 200, - kHartStandard = 150, - kLowUsage = 100, - kVeryLowUsage = 50, - }; - - // operator overloading. - - inline bool operator<(AffinityKind lhs, AffinityKind rhs) - { - Int32 lhs_int = static_cast(lhs); - Int32 rhs_int = static_cast(rhs); - - return lhs_int < rhs_int; - } - - inline bool operator>(AffinityKind lhs, AffinityKind rhs) - { - Int32 lhs_int = static_cast(lhs); - Int32 rhs_int = static_cast(rhs); - - return lhs_int > rhs_int; - } - - inline bool operator<=(AffinityKind lhs, AffinityKind rhs) - { - Int32 lhs_int = static_cast(lhs); - Int32 rhs_int = static_cast(rhs); - - return lhs_int <= rhs_int; - } - - inline bool operator>=(AffinityKind lhs, AffinityKind rhs) - { - Int32 lhs_int = static_cast(lhs); - Int32 rhs_int = static_cast(rhs); - - return lhs_int >= rhs_int; - } - - // end of operator overloading. - - enum ProcessSubsystemEnum - { - eProcessSubsystemLogin, - eProcessSubsystemNative, - eProcessSubsystemInvalid, - eProcessSubsystemCount, - }; - - using ProcessSubsystem = ProcessSubsystemEnum; - using ProcessTime = UInt64; - using PID = Int64; - - // for permission manager, tells where we run the code. - enum class ProcessSelector : Int - { - kRingUser, /* user ring (or ring 3 in x86) */ - kRingDriver, /* ring 2 in x86, hypervisor privileges in other archs */ - kRingKernel, /* machine privileges */ - }; - - // Helper types. - using ImagePtr = VoidPtr; - using HeapPtr = VoidPtr; - - // @name ProcessHeader - // @brief Process Header (PH) - // Holds information about the running process. - // Thread execution is being abstracted away. - class ProcessHeader final - { - public: - explicit ProcessHeader(VoidPtr startImage = nullptr) - : Image(startImage) - { - MUST_PASS(startImage); - } - - ~ProcessHeader() = default; - - NEWOS_COPY_DEFAULT(ProcessHeader) - - public: - void SetEntrypoint(UIntPtr& imageStart) noexcept; - - public: - Char Name[kProcessLen] = {"NewOS Process"}; - ProcessSubsystem SubSystem{ProcessSubsystem::eProcessSubsystemInvalid}; - ProcessSelector Selector{ProcessSelector::kRingUser}; - HAL::StackFramePtr StackFrame{nullptr}; - AffinityKind Affinity; - ProcessStatus Status; - - // Memory, images. - HeapPtr HeapCursor{nullptr}; - ImagePtr Image{nullptr}; - HeapPtr HeapPtr{nullptr}; - - // memory usage - SizeT UsedMemory{0}; - SizeT FreeMemory{0}; - - enum - { - kAppKind = 3, - kLibKind = 3, - kDriverKind = 0, - kKindCount, - }; - - enum - { - kRingUserKind = 3, - kRingDriverKind = 0, - }; - - ProcessTime PTime; - PID ProcessId{kSchedInvalidPID}; - Int32 Ring{kRingDriverKind}; - Int32 Kind{kAppKind}; - - public: - //! @brief boolean operator, check status. - operator bool() - { - return Status != ProcessStatus::kDead; - } - - //! @brief Crash the app, exits with code ~0. - void Crash(); - - //! @brief Exits app. - void Exit(Int32 exitCode = 0); - - //! @brief TLS Allocate - VoidPtr New(const SizeT& sz); - - //! @brief TLS Free. - Boolean Delete(VoidPtr ptr, const SizeT& sz); - - //! @brief Wakes up threads. - void Wake(const bool wakeup = false); - - // ProcessHeader getters. - public: - //! @brief ProcessHeader name getter, example: "C RunTime" - const Char* GetName(); - - const ProcessSelector& GetSelector(); - const ProcessStatus& GetStatus(); - const AffinityKind& GetAffinity(); - - private: - friend ProcessScheduler; - friend ProcessHelper; - }; - - /// \brief Processs Team (contains multiple processes inside it.) - /// Equivalent to a process batch - class ProcessTeam final - { - public: - explicit ProcessTeam() = default; - ~ProcessTeam() = default; - - NEWOS_COPY_DEFAULT(ProcessTeam); - - MutableArray>& AsArray(); - Ref& AsRef(); - - public: - MutableArray> mProcessList; - Ref mCurrentProcess; - }; - - using ProcessHeaderRef = ProcessHeader*; - - /// @brief ProcessHeader manager class. - /// The main class which you call to schedule an app. - class ProcessScheduler final - { - private: - explicit ProcessScheduler() = default; - - public: - ~ProcessScheduler() = default; - - NEWOS_COPY_DEFAULT(ProcessScheduler) - - operator bool() - { - return mTeam.AsArray().Count() > 0; - } - - bool operator!() - { - return mTeam.AsArray().Count() == 0; - } - - public: - ProcessTeam& CurrentTeam(); - - public: - SizeT Add(Ref& headerRef); - bool Remove(SizeT headerIndex); - - public: - Ref& GetCurrent(); - SizeT Run() noexcept; - - public: - static Ref The(); - - private: - ProcessTeam mTeam; - }; - - /* - * Just a helper class, which contains some utilities for the scheduler. - */ - - class ProcessHelper final - { - public: - static bool Switch(HAL::StackFrame* newStack, const PID& newPid); - static bool CanBeScheduled(Ref& process); - static PID& GetCurrentPID(); - static bool StartScheduling(); - }; - - const Int32& rt_get_exit_code() noexcept; -} // namespace NewOS - -#include - -//////////////////////////////////////////////////// - -// END - -//////////////////////////////////////////////////// - -#endif /* ifndef __PROCESS_SCHEDULER__ */ diff --git a/Kernel/KernelKit/ProcessScheduler.hxx b/Kernel/KernelKit/ProcessScheduler.hxx new file mode 100644 index 00000000..33c9f313 --- /dev/null +++ b/Kernel/KernelKit/ProcessScheduler.hxx @@ -0,0 +1,303 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + +------------------------------------------- */ + +#ifndef __PROCESS_SCHEDULER__ +#define __PROCESS_SCHEDULER__ + +#include +#include +#include +#include +#include +#include + +#define kSchedMinMicroTime AffinityKind::kHartStandard +#define kSchedInvalidPID (-1) + +#define kSchedProcessLimitPerTeam (100U) + +//////////////////////////////////////////////////// + +// LAST REV: Mon Feb 12 13:52:01 CET 2024 + +//////////////////////////////////////////////////// + +namespace NewOS +{ + class ProcessHeader; + class ProcessTeam; + class ProcessScheduler; + + //! @brief Process identifier. + typedef Int64 ProcessID; + + //! @brief Process name length. + inline constexpr SizeT kProcessLen = 256U; + + //! @brief Forward declaration. + class ProcessHeader; + class ProcessScheduler; + class ProcessHelper; + + //! @brief Process status enum. + enum class ProcessStatus : Int32 + { + kStarting, + kRunning, + kKilled, + kFrozen, + kDead + }; + + //! @brief Affinity is the amount of nano-seconds this process is going + //! to run. + enum class AffinityKind : Int32 + { + kInvalid = 300, + kVeryHigh = 250, + kHigh = 200, + kHartStandard = 150, + kLowUsage = 100, + kVeryLowUsage = 50, + }; + + // operator overloading. + + inline bool operator<(AffinityKind lhs, AffinityKind rhs) + { + Int32 lhs_int = static_cast(lhs); + Int32 rhs_int = static_cast(rhs); + + return lhs_int < rhs_int; + } + + inline bool operator>(AffinityKind lhs, AffinityKind rhs) + { + Int32 lhs_int = static_cast(lhs); + Int32 rhs_int = static_cast(rhs); + + return lhs_int > rhs_int; + } + + inline bool operator<=(AffinityKind lhs, AffinityKind rhs) + { + Int32 lhs_int = static_cast(lhs); + Int32 rhs_int = static_cast(rhs); + + return lhs_int <= rhs_int; + } + + inline bool operator>=(AffinityKind lhs, AffinityKind rhs) + { + Int32 lhs_int = static_cast(lhs); + Int32 rhs_int = static_cast(rhs); + + return lhs_int >= rhs_int; + } + + // end of operator overloading. + + enum ProcessSubsystemEnum + { + eProcessSubsystemLogin, + eProcessSubsystemNative, + eProcessSubsystemInvalid, + eProcessSubsystemCount, + }; + + using ProcessSubsystem = ProcessSubsystemEnum; + using ProcessTime = UInt64; + using PID = Int64; + + // for permission manager, tells where we run the code. + enum class ProcessSelector : Int + { + kRingUser, /* user ring (or ring 3 in x86) */ + kRingDriver, /* ring 2 in x86, hypervisor privileges in other archs */ + kRingKernel, /* machine privileges */ + }; + + // Helper types. + using ImagePtr = VoidPtr; + using HeapPtr = VoidPtr; + + // @name ProcessHeader + // @brief Process Header (PH) + // Holds information about the running process. + // Thread execution is being abstracted away. + class ProcessHeader final + { + public: + explicit ProcessHeader(VoidPtr startImage = nullptr) + : Image(startImage) + { + MUST_PASS(startImage); + } + + ~ProcessHeader() = default; + + NEWOS_COPY_DEFAULT(ProcessHeader) + + public: + void SetEntrypoint(UIntPtr& imageStart) noexcept; + + public: + Char Name[kProcessLen] = {"NewOS Process"}; + ProcessSubsystem SubSystem{ProcessSubsystem::eProcessSubsystemInvalid}; + ProcessSelector Selector{ProcessSelector::kRingUser}; + HAL::StackFramePtr StackFrame{nullptr}; + AffinityKind Affinity; + ProcessStatus Status; + + // Memory, images. + HeapPtr HeapCursor{nullptr}; + ImagePtr Image{nullptr}; + HeapPtr HeapPtr{nullptr}; + + // memory usage + SizeT UsedMemory{0}; + SizeT FreeMemory{0}; + + enum + { + kAppKind = 1, + kShLibKind = 2, + kDriverKind = 3, + kKindCount, + }; + + enum + { + kRingUserKind = 3, + kRingDriverKind = 0, + }; + + ProcessTime PTime; + PID ProcessId{kSchedInvalidPID}; + Int32 Ring{kRingDriverKind}; + Int32 Kind{kAppKind}; + + public: + //! @brief boolean operator, check status. + operator bool() + { + return Status != ProcessStatus::kDead; + } + + //! @brief Crash the app, exits with code ~0. + void Crash(); + + //! @brief Exits app. + void Exit(Int32 exitCode = 0); + + //! @brief TLS Allocate + VoidPtr New(const SizeT& sz); + + //! @brief TLS Free. + Boolean Delete(VoidPtr ptr, const SizeT& sz); + + //! @brief Wakes up threads. + void Wake(const bool wakeup = false); + + // ProcessHeader getters. + public: + //! @brief ProcessHeader name getter, example: "C RunTime" + const Char* GetName(); + + const ProcessSelector& GetSelector(); + const ProcessStatus& GetStatus(); + const AffinityKind& GetAffinity(); + + private: + friend ProcessScheduler; + friend ProcessHelper; + }; + + /// \brief Processs Team (contains multiple processes inside it.) + /// Equivalent to a process batch + class ProcessTeam final + { + public: + explicit ProcessTeam() = default; + ~ProcessTeam() = default; + + NEWOS_COPY_DEFAULT(ProcessTeam); + + MutableArray>& AsArray(); + Ref& AsRef(); + + public: + MutableArray> mProcessList; + Ref mCurrentProcess; + }; + + using ProcessHeaderRef = ProcessHeader*; + + /// @brief ProcessHeader manager class. + /// The main class which you call to schedule an app. + class ProcessScheduler final + { + private: + explicit ProcessScheduler() = default; + + public: + ~ProcessScheduler() = default; + + NEWOS_COPY_DEFAULT(ProcessScheduler) + + operator bool() + { + return mTeam.AsArray().Count() > 0; + } + + bool operator!() + { + return mTeam.AsArray().Count() == 0; + } + + public: + ProcessTeam& CurrentTeam(); + + public: + SizeT Add(Ref& headerRef); + bool Remove(SizeT headerIndex); + + public: + Ref& GetCurrent(); + SizeT Run() noexcept; + + public: + static Ref The(); + + private: + ProcessTeam mTeam; + }; + + /* + * Just a helper class, which contains some utilities for the scheduler. + */ + + class ProcessHelper final + { + public: + static bool Switch(HAL::StackFrame* newStack, const PID& newPid); + static bool CanBeScheduled(Ref& process); + static PID& GetCurrentPID(); + static bool StartScheduling(); + }; + + const Int32& rt_get_exit_code() noexcept; +} // namespace NewOS + +#include + +//////////////////////////////////////////////////// + +// END + +//////////////////////////////////////////////////// + +#endif /* ifndef __PROCESS_SCHEDULER__ */ diff --git a/Kernel/KernelKit/ThreadLocalStorage.inl b/Kernel/KernelKit/ThreadLocalStorage.inl index e2137ed6..a8269a14 100644 --- a/Kernel/KernelKit/ThreadLocalStorage.inl +++ b/Kernel/KernelKit/ThreadLocalStorage.inl @@ -7,7 +7,7 @@ //! @brief Allocates a pointer from the process's tls. #ifndef __PROCESS_MANAGER__ -#include +#include #endif template diff --git a/Kernel/Sources/AppMain.cxx b/Kernel/Sources/AppMain.cxx index 692d4376..62795893 100644 --- a/Kernel/Sources/AppMain.cxx +++ b/Kernel/Sources/AppMain.cxx @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include @@ -182,31 +182,31 @@ namespace NewOS::Detail /// @brief System loader entrypoint. /// @param void no parameters. /// @return void no return value. - STATIC NewOS::Void AppSystemLoader(NewOS::Void) + STATIC NewOS::Void AppSystem(NewOS::Void) { - NewOS::PEFLoader coreGraphicsShLib("/System/WindowServer"); + NewOS::PEFLoader wndServer("/System/WindowServer"); - if (!coreGraphicsShLib.IsLoaded()) + if (!wndServer.IsLoaded()) { NewOS::ke_stop(RUNTIME_CHECK_FAILED); } - NewOS::Utils::execute_from_image(coreGraphicsShLib, - NewOS::ProcessHeader::kLibKind); + NewOS::Utils::execute_from_image(wndServer, + NewOS::ProcessHeader::kAppKind); - NewOS::PEFLoader logonService("/System/Login"); + NewOS::PEFLoader launchServer("/System/Launcher"); - if (!logonService.IsLoaded()) + if (!launchServer.IsLoaded()) { NewOS::ke_stop(RUNTIME_CHECK_FAILED); } - NewOS::Utils::execute_from_image(logonService, + NewOS::Utils::execute_from_image(launchServer, NewOS::ProcessHeader::kAppKind); - NewOS::kcout << "SystemLoader: Exiting process, we're done initializing stuff..."; + NewOS::kcout << "System: done, sleeping..."; - NewOS::ProcessScheduler::The().Leak().GetCurrent().Leak().Exit(0); + while (true) {} } } // namespace NewOS::Detail @@ -218,8 +218,8 @@ EXTERN_C NewOS::Void AppMain(NewOS::Void) /// Now run kernel loop, until no process are running. NewOS::Detail::FilesystemWizard wizard; // automatic. - auto cLoaderName = "SystemLoader"; - NewOS::execute_from_image(NewOS::Detail::AppSystemLoader, cLoaderName); + auto cLoaderName = "System"; + NewOS::execute_from_image(NewOS::Detail::AppSystem, cLoaderName); while (NewOS::ProcessScheduler::The().Leak().Run() > 0) {} } diff --git a/Kernel/Sources/CodeManager.cxx b/Kernel/Sources/CodeManager.cxx index 7483b32f..10de3512 100644 --- a/Kernel/Sources/CodeManager.cxx +++ b/Kernel/Sources/CodeManager.cxx @@ -6,7 +6,7 @@ #include #include -#include +#include namespace NewOS { diff --git a/Kernel/Sources/KernelHeap.cxx b/Kernel/Sources/KernelHeap.cxx index a8ca467e..93f0c164 100644 --- a/Kernel/Sources/KernelHeap.cxx +++ b/Kernel/Sources/KernelHeap.cxx @@ -38,7 +38,10 @@ namespace NewOS SizeT fTargetPtrSize; /// @brief 64-bit target pointer. UIntPtr fTargetPtr; - UInt8 fPadding[kKernelHeapHeaderPaddingSz]; + /// @brief Is this a page pointer? + Boolean fPagePtr; + /// @brief Padding bytes for header. + UInt8 fPadding[kKernelHeapHeaderPaddingSz]; }; typedef HeapInformationBlock* HeapInformationBlockPtr; @@ -64,6 +67,7 @@ namespace NewOS heapInfo->fMagic = kKernelHeapMagic; heapInfo->fCRC32 = 0; // dont fill it for now. heapInfo->fTargetPtr = wrapper.VirtualAddress(); + heapInfo->fPagePtr = 0; ++kHeapCount; @@ -71,6 +75,27 @@ namespace NewOS sizeof(Detail::HeapInformationBlock)); } + /// @brief Makes a page heap. + /// @param heapPtr + /// @return + Int32 ke_make_ke_page(VoidPtr heapPtr) + { + if (kHeapCount < 1) + return -kErrorInternal; + if (((IntPtr)heapPtr - sizeof(Detail::HeapInformationBlock)) <= 0) + return -kErrorInternal; + if (((IntPtr)heapPtr - kBadPtr) < 0) + return -kErrorInternal; + + Detail::HeapInformationBlockPtr virtualAddress = + reinterpret_cast( + (UIntPtr)heapPtr - sizeof(Detail::HeapInformationBlock)); + + virtualAddress->fPagePtr = 1; + + return 0; + } + /// @brief Declare pointer as free. /// @param heapPtr the pointer. /// @return diff --git a/Kernel/Sources/PEFCodeManager.cxx b/Kernel/Sources/PEFCodeManager.cxx index c0997dff..a84730e9 100644 --- a/Kernel/Sources/PEFCodeManager.cxx +++ b/Kernel/Sources/PEFCodeManager.cxx @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Kernel/Sources/PEFSharedObjectRT.cxx b/Kernel/Sources/PEFSharedObjectRT.cxx index 33df8857..3ddef245 100644 --- a/Kernel/Sources/PEFSharedObjectRT.cxx +++ b/Kernel/Sources/PEFSharedObjectRT.cxx @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include diff --git a/Kernel/Sources/ProcessScheduler.cxx b/Kernel/Sources/ProcessScheduler.cxx index 32e1fbde..bf088cc6 100644 --- a/Kernel/Sources/ProcessScheduler.cxx +++ b/Kernel/Sources/ProcessScheduler.cxx @@ -9,7 +9,7 @@ /// @brief MicroKernel process scheduler. /***********************************************************************************/ -#include +#include #include #include #include @@ -206,7 +206,7 @@ namespace NewOS if (!process.Leak().Image) { - if (process.Leak().Kind != ProcessHeader::kLibKind) + if (process.Leak().Kind != ProcessHeader::kShLibKind) { return -kErrorNoEntrypoint; } @@ -223,7 +223,7 @@ namespace NewOS /// Create heap according to type of process. if (process.Leak().Kind == ProcessHeader::kAppKind) process.Leak().HeapPtr = rt_new_heap(kUserHeapUser | kUserHeapRw); - else if (process.Leak().Kind == ProcessHeader::kLibKind) + else if (process.Leak().Kind == ProcessHeader::kShLibKind) process.Leak().HeapPtr = rt_new_heap(kUserHeapUser | kUserHeapRw | kUserHeapShared); else process.Leak().HeapPtr = rt_new_heap(kUserHeapDriver | kUserHeapRw); diff --git a/Kernel/Sources/ProcessTeam.cxx b/Kernel/Sources/ProcessTeam.cxx index 7e311399..068e0dbb 100644 --- a/Kernel/Sources/ProcessTeam.cxx +++ b/Kernel/Sources/ProcessTeam.cxx @@ -9,7 +9,7 @@ /// @brief Process teams implementation. /***********************************************************************************/ -#include +#include namespace NewOS { diff --git a/Kernel/Sources/SMPManager.cxx b/Kernel/Sources/SMPManager.cxx index b2f239e3..29e0fa26 100644 --- a/Kernel/Sources/SMPManager.cxx +++ b/Kernel/Sources/SMPManager.cxx @@ -5,7 +5,7 @@ ------------------------------------------- */ #include -#include +#include #include ///! BUGS: 0 @@ -95,7 +95,6 @@ namespace NewOS { /// Keep the arguments, switch the base pointer, stack pointer /// fs and gs registers. - fStack->Rbp = stack->Rbp; fStack->Rsp = stack->Rsp; fStack->Fs = stack->Fs; @@ -103,6 +102,7 @@ namespace NewOS } rt_do_context_switch(fStack); + return true; } @@ -172,6 +172,8 @@ namespace NewOS rt_copy_memory(stack, fThreadList[idx].Leak().Leak().fStack, sizeof(HAL::StackFrame)); + fThreadList[idx].Leak().Leak().Switch(fThreadList[idx].Leak().Leak().fStack); + fThreadList[idx].Leak().Leak().fPID = ProcessHelper::GetCurrentPID(); fThreadList[idx].Leak().Leak().Busy(false); diff --git a/Kernel/Sources/Semaphore.cxx b/Kernel/Sources/Semaphore.cxx index 7bd1d513..e2e135d0 100644 --- a/Kernel/Sources/Semaphore.cxx +++ b/Kernel/Sources/Semaphore.cxx @@ -4,7 +4,7 @@ ------------------------------------------- */ -#include +#include #include #include diff --git a/Kernel/Sources/ThreadLocalStorage.cxx b/Kernel/Sources/ThreadLocalStorage.cxx index 2d7e61eb..00305006 100644 --- a/Kernel/Sources/ThreadLocalStorage.cxx +++ b/Kernel/Sources/ThreadLocalStorage.cxx @@ -7,7 +7,7 @@ * ======================================================== */ -#include +#include #include ///! BUGS: 0 diff --git a/Kernel/Sources/UserHeap.cxx b/Kernel/Sources/UserHeap.cxx index da5d7b5a..64b75641 100644 --- a/Kernel/Sources/UserHeap.cxx +++ b/Kernel/Sources/UserHeap.cxx @@ -4,7 +4,7 @@ ------------------------------------------- */ -#include +#include #include #include diff --git a/Kernel/makefile b/Kernel/makefile index b7749100..14bd1cae 100644 --- a/Kernel/makefile +++ b/Kernel/makefile @@ -69,7 +69,7 @@ link-amd64-epm: .PHONY: all all: newos-amd64-epm link-amd64-epm - @echo "NewKernel => OK." + @echo "NewOSKrnl => OK." .PHONY: help help: -- cgit v1.2.3