From d48cbe75ef29a9c67c9d176bf58e56ea6448fb9e Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 21 Oct 2024 20:23:36 +0200 Subject: IMP: Major refactor of header and source files extensions. Signed-off-by: Amlal El Mahrouss --- dev/zka/KernelKit/CodeMgr.h | 31 ++ dev/zka/KernelKit/CodeMgr.hxx | 31 -- dev/zka/KernelKit/DebugOutput.h | 197 ++++++++++++ dev/zka/KernelKit/DebugOutput.hxx | 197 ------------ dev/zka/KernelKit/Defines.h | 11 + dev/zka/KernelKit/Defines.hxx | 11 - dev/zka/KernelKit/DeviceMgr.h | 132 ++++++++ dev/zka/KernelKit/DeviceMgr.hxx | 132 -------- dev/zka/KernelKit/DriveMgr.h | 155 ++++++++++ dev/zka/KernelKit/DriveMgr.hxx | 155 ---------- dev/zka/KernelKit/FileMgr.h | 420 ++++++++++++++++++++++++++ dev/zka/KernelKit/FileMgr.hxx | 420 -------------------------- dev/zka/KernelKit/HardwareThreadScheduler.h | 149 +++++++++ dev/zka/KernelKit/HardwareThreadScheduler.hxx | 149 --------- dev/zka/KernelKit/Heap.h | 74 +++++ dev/zka/KernelKit/Heap.hxx | 74 ----- dev/zka/KernelKit/IDLLObject.h | 45 +++ dev/zka/KernelKit/IDLLObject.hxx | 45 --- dev/zka/KernelKit/IPEFDLLObject.h | 106 +++++++ dev/zka/KernelKit/IPEFDLLObject.hxx | 106 ------- dev/zka/KernelKit/LPC.h | 61 ++++ dev/zka/KernelKit/LPC.hxx | 61 ---- dev/zka/KernelKit/LoaderInterface.h | 33 ++ dev/zka/KernelKit/LoaderInterface.hxx | 33 -- dev/zka/KernelKit/LockDelegate.h | 67 ++++ dev/zka/KernelKit/LockDelegate.hxx | 67 ---- dev/zka/KernelKit/MSDOS.h | 52 ++++ dev/zka/KernelKit/MSDOS.hxx | 52 ---- dev/zka/KernelKit/PCI/DMA.h | 81 +++++ dev/zka/KernelKit/PCI/DMA.hxx | 81 ----- dev/zka/KernelKit/PCI/Database.h | 38 +++ dev/zka/KernelKit/PCI/Database.hxx | 38 --- dev/zka/KernelKit/PCI/Device.h | 80 +++++ dev/zka/KernelKit/PCI/Device.hxx | 80 ----- dev/zka/KernelKit/PCI/Express.h | 11 + dev/zka/KernelKit/PCI/Express.hxx | 11 - dev/zka/KernelKit/PCI/IO.h | 59 ++++ dev/zka/KernelKit/PCI/IO.hxx | 59 ---- dev/zka/KernelKit/PCI/Iterator.h | 43 +++ dev/zka/KernelKit/PCI/Iterator.hxx | 43 --- dev/zka/KernelKit/PCI/PCI.h | 59 ++++ dev/zka/KernelKit/PCI/PCI.hxx | 59 ---- dev/zka/KernelKit/PE.h | 143 +++++++++ dev/zka/KernelKit/PE.hxx | 143 --------- dev/zka/KernelKit/PECodeMgr.h | 24 ++ dev/zka/KernelKit/PECodeMgr.hxx | 24 -- dev/zka/KernelKit/PEF.h | 116 +++++++ dev/zka/KernelKit/PEF.hxx | 116 ------- dev/zka/KernelKit/PEFCodeMgr.h | 67 ++++ dev/zka/KernelKit/PEFCodeMgr.hxx | 67 ---- dev/zka/KernelKit/Semaphore.h | 43 +++ dev/zka/KernelKit/Semaphore.hxx | 43 --- dev/zka/KernelKit/ThreadLocalStorage.h | 54 ++++ dev/zka/KernelKit/ThreadLocalStorage.hxx | 54 ---- dev/zka/KernelKit/ThreadLocalStorage.inl | 2 +- dev/zka/KernelKit/Timer.h | 82 +++++ dev/zka/KernelKit/Timer.hxx | 82 ----- dev/zka/KernelKit/User.h | 75 +++++ dev/zka/KernelKit/User.hxx | 75 ----- dev/zka/KernelKit/UserProcessScheduler.h | 322 ++++++++++++++++++++ dev/zka/KernelKit/UserProcessScheduler.hxx | 322 -------------------- dev/zka/KernelKit/XCOFF.h | 51 ++++ dev/zka/KernelKit/XCOFF.hxx | 51 ---- 63 files changed, 2882 insertions(+), 2882 deletions(-) create mode 100644 dev/zka/KernelKit/CodeMgr.h delete mode 100644 dev/zka/KernelKit/CodeMgr.hxx create mode 100644 dev/zka/KernelKit/DebugOutput.h delete mode 100644 dev/zka/KernelKit/DebugOutput.hxx create mode 100644 dev/zka/KernelKit/Defines.h delete mode 100644 dev/zka/KernelKit/Defines.hxx create mode 100644 dev/zka/KernelKit/DeviceMgr.h delete mode 100644 dev/zka/KernelKit/DeviceMgr.hxx create mode 100644 dev/zka/KernelKit/DriveMgr.h delete mode 100644 dev/zka/KernelKit/DriveMgr.hxx create mode 100644 dev/zka/KernelKit/FileMgr.h delete mode 100644 dev/zka/KernelKit/FileMgr.hxx create mode 100644 dev/zka/KernelKit/HardwareThreadScheduler.h delete mode 100644 dev/zka/KernelKit/HardwareThreadScheduler.hxx create mode 100644 dev/zka/KernelKit/Heap.h delete mode 100644 dev/zka/KernelKit/Heap.hxx create mode 100644 dev/zka/KernelKit/IDLLObject.h delete mode 100644 dev/zka/KernelKit/IDLLObject.hxx create mode 100644 dev/zka/KernelKit/IPEFDLLObject.h delete mode 100644 dev/zka/KernelKit/IPEFDLLObject.hxx create mode 100644 dev/zka/KernelKit/LPC.h delete mode 100644 dev/zka/KernelKit/LPC.hxx create mode 100644 dev/zka/KernelKit/LoaderInterface.h delete mode 100644 dev/zka/KernelKit/LoaderInterface.hxx create mode 100644 dev/zka/KernelKit/LockDelegate.h delete mode 100644 dev/zka/KernelKit/LockDelegate.hxx create mode 100644 dev/zka/KernelKit/MSDOS.h delete mode 100644 dev/zka/KernelKit/MSDOS.hxx create mode 100644 dev/zka/KernelKit/PCI/DMA.h delete mode 100644 dev/zka/KernelKit/PCI/DMA.hxx create mode 100644 dev/zka/KernelKit/PCI/Database.h delete mode 100644 dev/zka/KernelKit/PCI/Database.hxx create mode 100644 dev/zka/KernelKit/PCI/Device.h delete mode 100644 dev/zka/KernelKit/PCI/Device.hxx create mode 100644 dev/zka/KernelKit/PCI/Express.h delete mode 100644 dev/zka/KernelKit/PCI/Express.hxx create mode 100644 dev/zka/KernelKit/PCI/IO.h delete mode 100644 dev/zka/KernelKit/PCI/IO.hxx create mode 100644 dev/zka/KernelKit/PCI/Iterator.h delete mode 100644 dev/zka/KernelKit/PCI/Iterator.hxx create mode 100644 dev/zka/KernelKit/PCI/PCI.h delete mode 100644 dev/zka/KernelKit/PCI/PCI.hxx create mode 100644 dev/zka/KernelKit/PE.h delete mode 100644 dev/zka/KernelKit/PE.hxx create mode 100644 dev/zka/KernelKit/PECodeMgr.h delete mode 100644 dev/zka/KernelKit/PECodeMgr.hxx create mode 100644 dev/zka/KernelKit/PEF.h delete mode 100644 dev/zka/KernelKit/PEF.hxx create mode 100644 dev/zka/KernelKit/PEFCodeMgr.h delete mode 100644 dev/zka/KernelKit/PEFCodeMgr.hxx create mode 100644 dev/zka/KernelKit/Semaphore.h delete mode 100644 dev/zka/KernelKit/Semaphore.hxx create mode 100644 dev/zka/KernelKit/ThreadLocalStorage.h delete mode 100644 dev/zka/KernelKit/ThreadLocalStorage.hxx create mode 100644 dev/zka/KernelKit/Timer.h delete mode 100644 dev/zka/KernelKit/Timer.hxx create mode 100644 dev/zka/KernelKit/User.h delete mode 100644 dev/zka/KernelKit/User.hxx create mode 100644 dev/zka/KernelKit/UserProcessScheduler.h delete mode 100644 dev/zka/KernelKit/UserProcessScheduler.hxx create mode 100644 dev/zka/KernelKit/XCOFF.h delete mode 100644 dev/zka/KernelKit/XCOFF.hxx (limited to 'dev/zka/KernelKit') diff --git a/dev/zka/KernelKit/CodeMgr.h b/dev/zka/KernelKit/CodeMgr.h new file mode 100644 index 00000000..41a90ec5 --- /dev/null +++ b/dev/zka/KernelKit/CodeMgr.h @@ -0,0 +1,31 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: CodeMgr.hpp + Purpose: Code Mgr and DLL mgr. + + Revision History: + + 30/01/24: Added file (amlel) + 3/8/24: Add UPP struct. + +------------------------------------------- */ + +#pragma once + +#include +#include +#include + +namespace Kernel +{ + /// @brief Main process entrypoint. + typedef void (*MainKind)(void); + + /// @brief Executes a new process from a function. Kernel code only. + /// @note This sets up a new stack, anything on the main function that calls the Kernel will not be accessible. + /// @param main the start of the process. + /// @return if the process was started or not. + bool sched_execute_thread(MainKind main, const Char* process_name) noexcept; +} // namespace Kernel diff --git a/dev/zka/KernelKit/CodeMgr.hxx b/dev/zka/KernelKit/CodeMgr.hxx deleted file mode 100644 index 127f3b7b..00000000 --- a/dev/zka/KernelKit/CodeMgr.hxx +++ /dev/null @@ -1,31 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: CodeMgr.hpp - Purpose: Code Mgr and DLL mgr. - - Revision History: - - 30/01/24: Added file (amlel) - 3/8/24: Add UPP struct. - -------------------------------------------- */ - -#pragma once - -#include -#include -#include - -namespace Kernel -{ - /// @brief Main process entrypoint. - typedef void (*MainKind)(void); - - /// @brief Executes a new process from a function. Kernel code only. - /// @note This sets up a new stack, anything on the main function that calls the Kernel will not be accessible. - /// @param main the start of the process. - /// @return if the process was started or not. - bool sched_execute_thread(MainKind main, const Char* process_name) noexcept; -} // namespace Kernel diff --git a/dev/zka/KernelKit/DebugOutput.h b/dev/zka/KernelKit/DebugOutput.h new file mode 100644 index 00000000..e7e466e3 --- /dev/null +++ b/dev/zka/KernelKit/DebugOutput.h @@ -0,0 +1,197 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include +#include +#include +#include + +#define kDebugMaxPorts 16 + +#define kDebugUnboundPort 0x0FEED + +#define kDebugMag0 'N' +#define kDebugMag1 'D' +#define kDebugMag2 'B' +#define kDebugMag3 'G' + +#define kDebugSourceFile 0 +#define kDebugLine 33 +#define kDebugTeam 43 +#define kDebugEOP 49 + +namespace Kernel +{ + class TerminalDevice; + + inline TerminalDevice end_line(); + inline TerminalDevice number(const Long& x); + inline TerminalDevice hex_number(const Long& x); + + // @brief Emulates a VT100 terminal. + class TerminalDevice final : public DeviceInterface + { + public: + TerminalDevice(void (*print)(const Char*), void (*get)(const Char*)) + : DeviceInterface(print, get) + { + } + + virtual ~TerminalDevice() = default; + + /// @brief returns device name (terminal name) + /// @return string type (const Char*) + virtual const Char* Name() const override + { + return ("TerminalDevice"); + } + + ZKA_COPY_DEFAULT(TerminalDevice); + + STATIC TerminalDevice The() noexcept; + }; + + inline TerminalDevice end_line() + { + TerminalDevice selfTerm = TerminalDevice::The(); + + selfTerm.operator<<("\r"); + return selfTerm; + } + + inline TerminalDevice carriage_return() + { + TerminalDevice selfTerm = TerminalDevice::The(); + + selfTerm.operator<<("\r"); + return selfTerm; + } + + inline TerminalDevice tabulate() + { + TerminalDevice selfTerm = TerminalDevice::The(); + + selfTerm.operator<<("\t"); + return selfTerm; + } + + /// @brief emulate a terminal bell, like the VT100 does. + inline TerminalDevice bell() + { + TerminalDevice selfTerm = TerminalDevice::The(); + + selfTerm.operator<<("\a"); + return selfTerm; + } + + namespace Detail + { + inline TerminalDevice _write_number(const Long& x, TerminalDevice& term) + { + UInt64 y = (x > 0 ? x : -x) / 10; + UInt64 h = (x > 0 ? x : -x) % 10; + + if (y) + _write_number(y, term); + + /* fail if the number is not base-10 */ + if (h > 10) + { + _write_number('?', term); + return term; + } + + if (y < 0) + y = -y; + + const Char cNumbers[11] = "0123456789"; + + Char buf[2]; + buf[0] = cNumbers[h]; + buf[1] = 0; + + term.operator<<(buf); + return term; + } + + inline TerminalDevice _write_number_hex(const Long& x, TerminalDevice& term) + { + UInt64 y = (x > 0 ? x : -x) / 16; + UInt64 h = (x > 0 ? x : -x) % 16; + + if (y) + _write_number_hex(y, term); + + /* fail if the hex number is not base-16 */ + if (h > 15) + { + _write_number_hex('?', term); + return term; + } + + if (y < 0) + y = -y; + + const Char cNumbers[17] = "0123456789ABCDEF"; + + Char buf[2]; + buf[0] = cNumbers[h]; + buf[1] = 0; + + term.operator<<(buf); + return term; + } + } // namespace Detail + + inline TerminalDevice hex_number(const Long& x) + { + TerminalDevice selfTerm = TerminalDevice::The(); + + Detail::_write_number_hex(x, selfTerm); + selfTerm.operator<<("h"); + + return selfTerm; + } + + inline TerminalDevice number(const Long& x) + { + TerminalDevice selfTerm = TerminalDevice::The(); + + Detail::_write_number(x, selfTerm); + + return selfTerm; + } + + inline TerminalDevice get_console_in(Char* buf) + { + TerminalDevice selfTerm = TerminalDevice::The(); + + selfTerm >> buf; + + return selfTerm; + } + + typedef Char rt_debug_type[255]; + + class DebuggerPortHeader final + { + public: + Int16 fPort[kDebugMaxPorts]; + Int16 fBoundCnt; + }; +} // namespace Kernel + +#ifdef kcout +#undef kcout +#endif // ifdef kcout + +#define kcout \ + (Kernel::TerminalDevice::The() << "\e[0;31m [ " << __FILE__ << ": LINE: " << Kernel::number(__LINE__)); \ + (Kernel::TerminalDevice::The() << " ] \e[0m" \ + << ": ") +#define endl Kernel::TerminalDevice::The() << Kernel::end_line() diff --git a/dev/zka/KernelKit/DebugOutput.hxx b/dev/zka/KernelKit/DebugOutput.hxx deleted file mode 100644 index 142debc7..00000000 --- a/dev/zka/KernelKit/DebugOutput.hxx +++ /dev/null @@ -1,197 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include -#include -#include -#include - -#define kDebugMaxPorts 16 - -#define kDebugUnboundPort 0x0FEED - -#define kDebugMag0 'N' -#define kDebugMag1 'D' -#define kDebugMag2 'B' -#define kDebugMag3 'G' - -#define kDebugSourceFile 0 -#define kDebugLine 33 -#define kDebugTeam 43 -#define kDebugEOP 49 - -namespace Kernel -{ - class TerminalDevice; - - inline TerminalDevice end_line(); - inline TerminalDevice number(const Long& x); - inline TerminalDevice hex_number(const Long& x); - - // @brief Emulates a VT100 terminal. - class TerminalDevice final : public DeviceInterface - { - public: - TerminalDevice(void (*print)(const Char*), void (*get)(const Char*)) - : DeviceInterface(print, get) - { - } - - virtual ~TerminalDevice() = default; - - /// @brief returns device name (terminal name) - /// @return string type (const Char*) - virtual const Char* Name() const override - { - return ("TerminalDevice"); - } - - ZKA_COPY_DEFAULT(TerminalDevice); - - STATIC TerminalDevice The() noexcept; - }; - - inline TerminalDevice end_line() - { - TerminalDevice selfTerm = TerminalDevice::The(); - - selfTerm.operator<<("\r"); - return selfTerm; - } - - inline TerminalDevice carriage_return() - { - TerminalDevice selfTerm = TerminalDevice::The(); - - selfTerm.operator<<("\r"); - return selfTerm; - } - - inline TerminalDevice tabulate() - { - TerminalDevice selfTerm = TerminalDevice::The(); - - selfTerm.operator<<("\t"); - return selfTerm; - } - - /// @brief emulate a terminal bell, like the VT100 does. - inline TerminalDevice bell() - { - TerminalDevice selfTerm = TerminalDevice::The(); - - selfTerm.operator<<("\a"); - return selfTerm; - } - - namespace Detail - { - inline TerminalDevice _write_number(const Long& x, TerminalDevice& term) - { - UInt64 y = (x > 0 ? x : -x) / 10; - UInt64 h = (x > 0 ? x : -x) % 10; - - if (y) - _write_number(y, term); - - /* fail if the number is not base-10 */ - if (h > 10) - { - _write_number('?', term); - return term; - } - - if (y < 0) - y = -y; - - const Char cNumbers[11] = "0123456789"; - - Char buf[2]; - buf[0] = cNumbers[h]; - buf[1] = 0; - - term.operator<<(buf); - return term; - } - - inline TerminalDevice _write_number_hex(const Long& x, TerminalDevice& term) - { - UInt64 y = (x > 0 ? x : -x) / 16; - UInt64 h = (x > 0 ? x : -x) % 16; - - if (y) - _write_number_hex(y, term); - - /* fail if the hex number is not base-16 */ - if (h > 15) - { - _write_number_hex('?', term); - return term; - } - - if (y < 0) - y = -y; - - const Char cNumbers[17] = "0123456789ABCDEF"; - - Char buf[2]; - buf[0] = cNumbers[h]; - buf[1] = 0; - - term.operator<<(buf); - return term; - } - } // namespace Detail - - inline TerminalDevice hex_number(const Long& x) - { - TerminalDevice selfTerm = TerminalDevice::The(); - - Detail::_write_number_hex(x, selfTerm); - selfTerm.operator<<("h"); - - return selfTerm; - } - - inline TerminalDevice number(const Long& x) - { - TerminalDevice selfTerm = TerminalDevice::The(); - - Detail::_write_number(x, selfTerm); - - return selfTerm; - } - - inline TerminalDevice get_console_in(Char* buf) - { - TerminalDevice selfTerm = TerminalDevice::The(); - - selfTerm >> buf; - - return selfTerm; - } - - typedef Char rt_debug_type[255]; - - class DebuggerPortHeader final - { - public: - Int16 fPort[kDebugMaxPorts]; - Int16 fBoundCnt; - }; -} // namespace Kernel - -#ifdef kcout -#undef kcout -#endif // ifdef kcout - -#define kcout \ - (Kernel::TerminalDevice::The() << "\e[0;31m [ " << __FILE__ << ": LINE: " << Kernel::number(__LINE__)); \ - (Kernel::TerminalDevice::The() << " ] \e[0m" \ - << ": ") -#define endl Kernel::TerminalDevice::The() << Kernel::end_line() diff --git a/dev/zka/KernelKit/Defines.h b/dev/zka/KernelKit/Defines.h new file mode 100644 index 00000000..48befc8e --- /dev/null +++ b/dev/zka/KernelKit/Defines.h @@ -0,0 +1,11 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include + +#define KERNELKIT_VERSION "1.02" diff --git a/dev/zka/KernelKit/Defines.hxx b/dev/zka/KernelKit/Defines.hxx deleted file mode 100644 index 3e59716f..00000000 --- a/dev/zka/KernelKit/Defines.hxx +++ /dev/null @@ -1,11 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include - -#define KERNELKIT_VERSION "1.02" diff --git a/dev/zka/KernelKit/DeviceMgr.h b/dev/zka/KernelKit/DeviceMgr.h new file mode 100644 index 00000000..1f767aa6 --- /dev/null +++ b/dev/zka/KernelKit/DeviceMgr.h @@ -0,0 +1,132 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +/* ------------------------------------------- + + Revision History: + + 31/01/24: Add kDeviceCnt (amlel) + + ------------------------------------------- */ + +#pragma once + +/* Device manager. */ +/* @file KernelKit/DeviceMgr.hpp */ +/* @brief Device abstraction and I/O buffer. */ + +#include +#include + +#define cDevicePath "\\Mount\\" + +// Last Rev +// Wed, Apr 3, 2024 9:09:41 AM + +namespace Kernel +{ + template + class DeviceInterface; + + template + class DeviceInterface + { + public: + explicit DeviceInterface(void (*Out)(T), void (*In)(T)) + : fOut(Out), fIn(In) + { + } + + virtual ~DeviceInterface() = default; + + public: + DeviceInterface& operator=(const DeviceInterface&) = default; + DeviceInterface(const DeviceInterface&) = default; + + public: + virtual DeviceInterface& operator<<(T Data) + { + fOut(Data); + return *this; + } + + virtual DeviceInterface& operator>>(T Data) + { + fIn(Data); + return *this; + } + + virtual const char* Name() const + { + return "DeviceInterface"; + } + + operator bool() + { + return fOut && fIn; + } + + Bool operator!() + { + return !fOut || !fIn; + } + + private: + Void (*fOut)(T Data) = {nullptr}; + Void (*fIn)(T Data) = {nullptr}; + }; + + /// + /// @brief Input Output Buffer + /// Used mainly to communicate between hardware. + /// + template + class IOBuf final + { + public: + explicit IOBuf(T Dat) + : fData(Dat) + { + // at least pass something valid when instancating this struct. + MUST_PASS(Dat); + } + + IOBuf& operator=(const IOBuf&) = default; + IOBuf(const IOBuf&) = default; + + ~IOBuf() = default; + + public: + template + R operator->() const + { + return fData; + } + + template + R& operator[](Size index) const + { + return fData[index]; + } + + private: + T fData; + }; + + ///! @brief Device enum types. + enum + { + kDeviceTypeIDE, + kDeviceTypeEthernet, + kDeviceTypeWiFi, + kDeviceTypeRS232, + kDeviceTypeSCSI, + kDeviceTypeSHCI, + kDeviceTypeUSB, + kDeviceTypeMedia, + kDeviceTypeCount, + }; +} // namespace Kernel diff --git a/dev/zka/KernelKit/DeviceMgr.hxx b/dev/zka/KernelKit/DeviceMgr.hxx deleted file mode 100644 index 5788d1b0..00000000 --- a/dev/zka/KernelKit/DeviceMgr.hxx +++ /dev/null @@ -1,132 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -/* ------------------------------------------- - - Revision History: - - 31/01/24: Add kDeviceCnt (amlel) - - ------------------------------------------- */ - -#pragma once - -/* Device manager. */ -/* @file KernelKit/DeviceMgr.hpp */ -/* @brief Device abstraction and I/O buffer. */ - -#include -#include - -#define cDevicePath "\\Mount\\" - -// Last Rev -// Wed, Apr 3, 2024 9:09:41 AM - -namespace Kernel -{ - template - class DeviceInterface; - - template - class DeviceInterface - { - public: - explicit DeviceInterface(void (*Out)(T), void (*In)(T)) - : fOut(Out), fIn(In) - { - } - - virtual ~DeviceInterface() = default; - - public: - DeviceInterface& operator=(const DeviceInterface&) = default; - DeviceInterface(const DeviceInterface&) = default; - - public: - virtual DeviceInterface& operator<<(T Data) - { - fOut(Data); - return *this; - } - - virtual DeviceInterface& operator>>(T Data) - { - fIn(Data); - return *this; - } - - virtual const char* Name() const - { - return "DeviceInterface"; - } - - operator bool() - { - return fOut && fIn; - } - - Bool operator!() - { - return !fOut || !fIn; - } - - private: - Void (*fOut)(T Data) = {nullptr}; - Void (*fIn)(T Data) = {nullptr}; - }; - - /// - /// @brief Input Output Buffer - /// Used mainly to communicate between hardware. - /// - template - class IOBuf final - { - public: - explicit IOBuf(T Dat) - : fData(Dat) - { - // at least pass something valid when instancating this struct. - MUST_PASS(Dat); - } - - IOBuf& operator=(const IOBuf&) = default; - IOBuf(const IOBuf&) = default; - - ~IOBuf() = default; - - public: - template - R operator->() const - { - return fData; - } - - template - R& operator[](Size index) const - { - return fData[index]; - } - - private: - T fData; - }; - - ///! @brief Device enum types. - enum - { - kDeviceTypeIDE, - kDeviceTypeEthernet, - kDeviceTypeWiFi, - kDeviceTypeRS232, - kDeviceTypeSCSI, - kDeviceTypeSHCI, - kDeviceTypeUSB, - kDeviceTypeMedia, - kDeviceTypeCount, - }; -} // namespace Kernel diff --git a/dev/zka/KernelKit/DriveMgr.h b/dev/zka/KernelKit/DriveMgr.h new file mode 100644 index 00000000..850217cb --- /dev/null +++ b/dev/zka/KernelKit/DriveMgr.h @@ -0,0 +1,155 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#ifndef __INC_DRIVE_MANAGER_HXX__ +#define __INC_DRIVE_MANAGER_HXX__ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define kMaxDriveCountPerMountpoint (4U) + +#define kDriveInvalidID (-1) +#define kDriveNameLen (32) + +#define DrvSectorCnt(SIZE, SECTOR_SZ) (((SIZE) + (SECTOR_SZ)) / (SECTOR_SZ)) + +namespace Kernel +{ + enum + { + /// Storage type. + kInvalidStorage = -1, + kBlockDevice = 0xAD, + kMassStorage = 0xDA, + kFloppyDisc = 0xCD, + kOpticalDisc = 0xDC, // CD-ROM/DVD-ROM/Blu-Ray + /// Storage flags, combine with below. + kReadOnly = 0x10, // Read only drive + kEPMDrive = 0x11, // Explicit Partition Map. + kEPTDrive = 0x12, // ESP w/ EPM partition. + kMBRDrive = 0x13, // PC classic partition scheme + kGPTDrive = 0x14, // PC new partition scheme + kStorageCount = 9, + }; + + typedef Int64 rt_drive_id_type; + + /// @brief Media drive trait type. + struct DriveTrait final + { + Char fName[kDriveNameLen]; // /System, /Boot, //./Devices/USB... + Int32 fKind; // fMassStorage, fFloppy, fOpticalDisc. + rt_drive_id_type fId; // Drive id. + Int32 fFlags; // fReadOnly, fXPMDrive, fXPTDrive + + /// @brief Packet drive (StorageKit compilant.) + struct DrivePacket final + { + Ref fPacketContent; //! packet body. + Char fPacketMime[kDriveNameLen]; //! identify what we're sending. + SizeT fPacketSize; //! packet size + UInt32 fPacketCRC32; //! sanity crc, in case if good is set to false + Boolean fPacketGood; + Lba fLba; + } fPacket; + + Void (*fInput)(DrivePacket* packetPtr); + Void (*fOutput)(DrivePacket* packetPtr); + Void (*fVerify)(DrivePacket* packetPtr); + Void (*fInit)(DrivePacket* packetPtr); + const Char* (*fDriveKind)(Void); + }; + + ///! drive as a device. + typedef DriveTrait* DriveTraitPtr; + + /** + * @brief Mounted drives interface. + * @note This class has all of it's drive set to nullptr, allocate them using + * GetAddressOf(index). + */ + class MountpointInterface final + { + public: + explicit MountpointInterface() = default; + ~MountpointInterface() = default; + + ZKA_COPY_DEFAULT(MountpointInterface); + + public: + DriveTrait& A() + { + return mA; + } + DriveTrait& B() + { + return mB; + } + DriveTrait& C() + { + return mC; + } + DriveTrait& D() + { + return mD; + } + + DriveTraitPtr GetAddressOf(Int32 index) + { + ErrLocal() = kErrorSuccess; + + switch (index) + { + case 0: + return &mA; + case 1: + return &mB; + case 2: + return &mC; + case 3: + return &mD; + default: { + ErrLocal() = kErrorNoSuchDisk; + kcout << "No such disk.\n"; + + break; + } + } + + return nullptr; + } + + private: + DriveTrait mA, mB, mC, mD; + }; + + /// @brief Unimplemented drive. + /// @param pckt + /// @return + Void io_drv_unimplemented(DriveTrait::DrivePacket* pckt); + + /// @brief Gets the drive kind (ATA, SCSI, AHCI...) + /// @param + /// @return the drive kind (ATA, Flash, NVM) + const Char* io_drv_kind(Void); + + /// @brief Makes a new drive. + /// @return the new drive as a trait. + DriveTrait io_construct_drive(void) noexcept; + + /// @brief Fetches the main drive. + /// @return the new drive as a trait. + DriveTrait io_construct_main_drive(void) noexcept; +} // namespace Kernel + +#endif /* ifndef __INC_DRIVE_MANAGER_HXX__ */ diff --git a/dev/zka/KernelKit/DriveMgr.hxx b/dev/zka/KernelKit/DriveMgr.hxx deleted file mode 100644 index ddff013b..00000000 --- a/dev/zka/KernelKit/DriveMgr.hxx +++ /dev/null @@ -1,155 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#ifndef __INC_DRIVE_MANAGER_HXX__ -#define __INC_DRIVE_MANAGER_HXX__ - -#include -#include -#include -#include -#include -#include -#include -#include - -#define kMaxDriveCountPerMountpoint (4U) - -#define kDriveInvalidID (-1) -#define kDriveNameLen (32) - -#define DrvSectorCnt(SIZE, SECTOR_SZ) (((SIZE) + (SECTOR_SZ)) / (SECTOR_SZ)) - -namespace Kernel -{ - enum - { - /// Storage type. - kInvalidStorage = -1, - kBlockDevice = 0xAD, - kMassStorage = 0xDA, - kFloppyDisc = 0xCD, - kOpticalDisc = 0xDC, // CD-ROM/DVD-ROM/Blu-Ray - /// Storage flags, combine with below. - kReadOnly = 0x10, // Read only drive - kEPMDrive = 0x11, // Explicit Partition Map. - kEPTDrive = 0x12, // ESP w/ EPM partition. - kMBRDrive = 0x13, // PC classic partition scheme - kGPTDrive = 0x14, // PC new partition scheme - kStorageCount = 9, - }; - - typedef Int64 rt_drive_id_type; - - /// @brief Media drive trait type. - struct DriveTrait final - { - Char fName[kDriveNameLen]; // /System, /Boot, //./Devices/USB... - Int32 fKind; // fMassStorage, fFloppy, fOpticalDisc. - rt_drive_id_type fId; // Drive id. - Int32 fFlags; // fReadOnly, fXPMDrive, fXPTDrive - - /// @brief Packet drive (StorageKit compilant.) - struct DrivePacket final - { - Ref fPacketContent; //! packet body. - Char fPacketMime[kDriveNameLen]; //! identify what we're sending. - SizeT fPacketSize; //! packet size - UInt32 fPacketCRC32; //! sanity crc, in case if good is set to false - Boolean fPacketGood; - Lba fLba; - } fPacket; - - Void (*fInput)(DrivePacket* packetPtr); - Void (*fOutput)(DrivePacket* packetPtr); - Void (*fVerify)(DrivePacket* packetPtr); - Void (*fInit)(DrivePacket* packetPtr); - const Char* (*fDriveKind)(Void); - }; - - ///! drive as a device. - typedef DriveTrait* DriveTraitPtr; - - /** - * @brief Mounted drives interface. - * @note This class has all of it's drive set to nullptr, allocate them using - * GetAddressOf(index). - */ - class MountpointInterface final - { - public: - explicit MountpointInterface() = default; - ~MountpointInterface() = default; - - ZKA_COPY_DEFAULT(MountpointInterface); - - public: - DriveTrait& A() - { - return mA; - } - DriveTrait& B() - { - return mB; - } - DriveTrait& C() - { - return mC; - } - DriveTrait& D() - { - return mD; - } - - DriveTraitPtr GetAddressOf(Int32 index) - { - ErrLocal() = kErrorSuccess; - - switch (index) - { - case 0: - return &mA; - case 1: - return &mB; - case 2: - return &mC; - case 3: - return &mD; - default: { - ErrLocal() = kErrorNoSuchDisk; - kcout << "No such disk.\n"; - - break; - } - } - - return nullptr; - } - - private: - DriveTrait mA, mB, mC, mD; - }; - - /// @brief Unimplemented drive. - /// @param pckt - /// @return - Void io_drv_unimplemented(DriveTrait::DrivePacket* pckt); - - /// @brief Gets the drive kind (ATA, SCSI, AHCI...) - /// @param - /// @return the drive kind (ATA, Flash, NVM) - const Char* io_drv_kind(Void); - - /// @brief Makes a new drive. - /// @return the new drive as a trait. - DriveTrait io_construct_drive(void) noexcept; - - /// @brief Fetches the main drive. - /// @return the new drive as a trait. - DriveTrait io_construct_main_drive(void) noexcept; -} // namespace Kernel - -#endif /* ifndef __INC_DRIVE_MANAGER_HXX__ */ diff --git a/dev/zka/KernelKit/FileMgr.h b/dev/zka/KernelKit/FileMgr.h new file mode 100644 index 00000000..cdda89e7 --- /dev/null +++ b/dev/zka/KernelKit/FileMgr.h @@ -0,0 +1,420 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: FileMgr.h + Purpose: Kernel file manager. + +------------------------------------------- */ + +/* ------------------------------------------- + + Revision History: + + 31/01/24: Update documentation (amlel) + 05/07/24: NeFS support, and fork support, updated constants and specs + as well. + + ------------------------------------------- */ + +#pragma once + +#ifdef __FSKIT_USE_NEFS__ +#include +#endif // __FSKIT_USE_NEFS__ + +#include +#include +#include +#include +#include +#include +#include +#include + +/// @brief Filesystem manager, abstraction over mounted filesystem. +/// Works like the VFS or IFS. + +#define cRestrictR "r" +#define cRestrictRB "rb" +#define cRestrictW "w" +#define cRestrictWB "rw" +#define cRestrictRWB "rwb" + +#define cRestrictMax 5 + +#define node_cast(PTR) reinterpret_cast(PTR) + +/** + @note Refer to first enum. +*/ +#define cFileOpsCount 4 +#define cFileMimeGeneric "n-application-kind/all" + +/** @brief invalid position. (n-pos) */ +#define kNPos (SizeT)(-1); + +namespace Kernel +{ + enum + { + cFileWriteAll = 100, + cFileReadAll = 101, + cFileReadChunk = 102, + cFileWriteChunk = 103, + cFileIOCnt = (cFileWriteChunk - cFileWriteAll) + 1, + // file flags + cFileFlagRsrc = 104, + cFileFlagData = 105, + }; + + typedef VoidPtr NodePtr; + + /** + @brief Filesystem Mgr Interface class + @brief Used to provide common I/O for a specific filesystem. +*/ + class IFilesystemMgr + { + public: + explicit IFilesystemMgr() = default; + virtual ~IFilesystemMgr() = default; + + public: + ZKA_COPY_DEFAULT(IFilesystemMgr); + + public: + /// @brief Mounts a new filesystem into an active state. + /// @param interface the filesystem interface + /// @return + static bool Mount(IFilesystemMgr* interface); + + /// @brief Unmounts the active filesystem + /// @return + static IFilesystemMgr* Unmount(); + + /// @brief Getter, gets the active filesystem. + /// @return + static IFilesystemMgr* GetMounted(); + + public: + virtual NodePtr Create(_Input const Char* path) = 0; + virtual NodePtr CreateAlias(_Input const Char* path) = 0; + virtual NodePtr CreateDirectory(_Input const Char* path) = 0; + virtual NodePtr CreateSwapFile(const Char* path) = 0; + + public: + virtual bool Remove(_Input const Char* path) = 0; + + public: + virtual NodePtr Open(_Input const Char* path, _Input const Char* r) = 0; + + public: + virtual Void Write(_Input NodePtr node, _Input VoidPtr data, _Input Int32 flags, _Input SizeT size) = 0; + + virtual _Output VoidPtr Read(_Input NodePtr node, + _Input Int32 flags, + _Input SizeT sz) = 0; + + virtual Void Write(_Input const Char* name, + _Input NodePtr node, + _Input VoidPtr data, + _Input Int32 flags, + _Input SizeT size) = 0; + + virtual _Output VoidPtr Read(_Input const Char* name, + _Input NodePtr node, + _Input Int32 flags, + _Input SizeT sz) = 0; + + public: + virtual bool Seek(_Input NodePtr node, _Input SizeT off) = 0; + + public: + virtual SizeT Tell(_Input NodePtr node) = 0; + virtual bool Rewind(_Input NodePtr node) = 0; + }; + +#ifdef __FSKIT_USE_NEFS__ + /** + * @brief Based of IFilesystemMgr, takes care of managing NeFS + * disks. + */ + class NeFileSystemMgr final : public IFilesystemMgr + { + public: + explicit NeFileSystemMgr(); + ~NeFileSystemMgr() override; + + public: + ZKA_COPY_DEFAULT(NeFileSystemMgr); + + public: + NodePtr Create(const Char* path) override; + NodePtr CreateAlias(const Char* path) override; + NodePtr CreateDirectory(const Char* path) override; + NodePtr CreateSwapFile(const Char* path) override; + + public: + bool Remove(_Input const Char* path) override; + NodePtr Open(_Input const Char* path, _Input const Char* r) override; + Void Write(_Input NodePtr node, _Input VoidPtr data, _Input Int32 flags, _Input SizeT sz) override; + VoidPtr Read(_Input NodePtr node, _Input Int32 flags, _Input SizeT sz) override; + bool Seek(_Input NodePtr node, _Input SizeT off) override; + SizeT Tell(_Input NodePtr node) override; + bool Rewind(_Input NodePtr node) override; + + Void Write(_Input const Char* name, + _Input NodePtr node, + _Input VoidPtr data, + _Input Int32 flags, + _Input SizeT size) override; + + _Output VoidPtr Read(_Input const Char* name, + _Input NodePtr node, + _Input Int32 flags, + _Input SizeT sz) override; + + public: + /// @brief Get NeFS parser class. + /// @return The filesystem parser class. + NeFSParser* GetParser() noexcept; + + private: + NeFSParser* fImpl{nullptr}; + }; + +#endif // ifdef __FSKIT_USE_NEFS__ + + /** + * Usable FileStream + * @tparam Encoding file encoding (char, wchar_t...) + * @tparam FSClass Filesystem contract who takes care of it. + */ + template + class FileStream final + { + public: + explicit FileStream(const Encoding* path, const Encoding* restrict_type); + ~FileStream(); + + public: + FileStream& operator=(const FileStream&); + FileStream(const FileStream&); + + public: + ErrorOr WriteAll(const VoidPtr data) noexcept + { + if (this->fFileRestrict != eRestrictReadWrite && + this->fFileRestrict != eRestrictReadWriteBinary && + this->fFileRestrict != eRestrictWrite && + this->fFileRestrict != eRestrictWriteBinary) + return ErrorOr(kErrorInvalidData); + + if (data == nullptr) + return ErrorOr(kErrorInvalidData); + + auto man = FSClass::GetMounted(); + + if (man) + { + man->Write(fFile, data, cFileWriteAll); + return ErrorOr(0); + } + + return ErrorOr(kErrorInvalidData); + } + + VoidPtr ReadAll() noexcept + { + if (this->fFileRestrict != eRestrictReadWrite && + this->fFileRestrict != eRestrictReadWriteBinary && + this->fFileRestrict != eRestrictRead && + this->fFileRestrict != eRestrictReadBinary) + return nullptr; + + auto man = FSClass::GetMounted(); + + if (man) + { + VoidPtr ret = man->Read(fFile, cFileReadAll, 0); + return ret; + } + + return nullptr; + } + + ErrorOr WriteAll(const Char* fName, const VoidPtr data) noexcept + { + if (this->fFileRestrict != eRestrictReadWrite && + this->fFileRestrict != eRestrictReadWriteBinary && + this->fFileRestrict != eRestrictWrite && + this->fFileRestrict != eRestrictWriteBinary) + return ErrorOr(kErrorInvalidData); + + if (data == nullptr) + return ErrorOr(kErrorInvalidData); + + auto man = FSClass::GetMounted(); + + if (man) + { + man->Write(fName, fFile, data, cFileWriteAll); + return ErrorOr(0); + } + + return ErrorOr(kErrorInvalidData); + } + + VoidPtr Read(const Char* fName) noexcept + { + if (this->fFileRestrict != eRestrictReadWrite && + this->fFileRestrict != eRestrictReadWriteBinary && + this->fFileRestrict != eRestrictRead && + this->fFileRestrict != eRestrictReadBinary) + return nullptr; + + auto man = FSClass::GetMounted(); + + if (man) + { + VoidPtr ret = man->Read(fName, fFile, cFileReadAll, 0); + return ret; + } + + return nullptr; + } + + VoidPtr Read(SizeT offset, SizeT sz) + { + if (this->fFileRestrict != eRestrictReadWrite && + this->fFileRestrict != eRestrictReadWriteBinary && + this->fFileRestrict != eRestrictRead && + this->fFileRestrict != eRestrictReadBinary) + return nullptr; + + auto man = FSClass::GetMounted(); + + if (man) + { + man->Seek(fFile, offset); + auto ret = man->Read(fFile, cFileReadChunk, sz); + + return ret; + } + + return nullptr; + } + + Void Write(SizeT offset, voidPtr data, SizeT sz) + { + if (this->fFileRestrict != eRestrictReadWrite && + this->fFileRestrict != eRestrictReadWriteBinary && + this->fFileRestrict != eRestrictWrite && + this->fFileRestrict != eRestrictWriteBinary) + return; + + auto man = FSClass::GetMounted(); + + if (man) + { + man->Seek(fFile, offset); + man->Write(fFile, data, sz, cFileReadChunk); + } + } + + public: + /// @brief Leak node pointer. + /// @return The node pointer. + NodePtr Leak() + { + return fFile; + } + + /// @brief Leak MIME. + /// @return The MIME. + Char* MIME() noexcept + { + return const_cast(fMime); + } + + enum + { + eRestrictRead, + eRestrictReadBinary, + eRestrictWrite, + eRestrictWriteBinary, + eRestrictReadWrite, + eRestrictReadWriteBinary, + }; + + private: + NodePtr fFile{nullptr}; + Int32 fFileRestrict{}; + const Char* fMime{cFileMimeGeneric}; + }; + + using FileStreamUTF8 = FileStream; + using FileStreamUTF16 = FileStream; + + typedef UInt64 CursorType; + + /// @brief constructor + template + FileStream::FileStream(const Encoding* path, + const Encoding* restrict_type) + : fFile(Class::GetMounted()->Open(path, restrict_type)) + { + static const auto cLength = 255; + + /// @brief restrict information about the file descriptor. + struct RESTRICT_MAP final + { + Char fRestrict[cLength]; + Int32 fMappedTo; + }; + + const SizeT cRestrictCount = cRestrictMax; + const RESTRICT_MAP cRestrictList[] = { + { + .fRestrict = cRestrictR, + .fMappedTo = eRestrictRead, + }, + { + .fRestrict = cRestrictRB, + .fMappedTo = eRestrictReadBinary, + }, + { + .fRestrict = cRestrictRWB, + .fMappedTo = eRestrictReadWriteBinary, + }, + { + .fRestrict = cRestrictW, + .fMappedTo = eRestrictWrite, + }, + { + .fRestrict = cRestrictWB, + .fMappedTo = eRestrictReadWrite, + }}; + + for (SizeT index = 0; index < cRestrictCount; ++index) + { + if (rt_string_cmp(restrict_type, cRestrictList[index].fRestrict, + rt_string_len(cRestrictList[index].fRestrict)) == 0) + { + fFileRestrict = cRestrictList[index].fMappedTo; + break; + } + } + + kcout << "new file: " << path << ".\r"; + } + + /// @brief destructor + template + FileStream::~FileStream() + { + mm_delete_heap(fFile); + } +} // namespace Kernel diff --git a/dev/zka/KernelKit/FileMgr.hxx b/dev/zka/KernelKit/FileMgr.hxx deleted file mode 100644 index fc6e6fc1..00000000 --- a/dev/zka/KernelKit/FileMgr.hxx +++ /dev/null @@ -1,420 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: FileMgr.hxx - Purpose: Kernel file manager. - -------------------------------------------- */ - -/* ------------------------------------------- - - Revision History: - - 31/01/24: Update documentation (amlel) - 05/07/24: NeFS support, and fork support, updated constants and specs - as well. - - ------------------------------------------- */ - -#pragma once - -#ifdef __FSKIT_USE_NEFS__ -#include -#endif // __FSKIT_USE_NEFS__ - -#include -#include -#include -#include -#include -#include -#include -#include - -/// @brief Filesystem manager, abstraction over mounted filesystem. -/// Works like the VFS or IFS. - -#define cRestrictR "r" -#define cRestrictRB "rb" -#define cRestrictW "w" -#define cRestrictWB "rw" -#define cRestrictRWB "rwb" - -#define cRestrictMax 5 - -#define node_cast(PTR) reinterpret_cast(PTR) - -/** - @note Refer to first enum. -*/ -#define cFileOpsCount 4 -#define cFileMimeGeneric "n-application-kind/all" - -/** @brief invalid position. (n-pos) */ -#define kNPos (SizeT)(-1); - -namespace Kernel -{ - enum - { - cFileWriteAll = 100, - cFileReadAll = 101, - cFileReadChunk = 102, - cFileWriteChunk = 103, - cFileIOCnt = (cFileWriteChunk - cFileWriteAll) + 1, - // file flags - cFileFlagRsrc = 104, - cFileFlagData = 105, - }; - - typedef VoidPtr NodePtr; - - /** - @brief Filesystem Mgr Interface class - @brief Used to provide common I/O for a specific filesystem. -*/ - class IFilesystemMgr - { - public: - explicit IFilesystemMgr() = default; - virtual ~IFilesystemMgr() = default; - - public: - ZKA_COPY_DEFAULT(IFilesystemMgr); - - public: - /// @brief Mounts a new filesystem into an active state. - /// @param interface the filesystem interface - /// @return - static bool Mount(IFilesystemMgr* interface); - - /// @brief Unmounts the active filesystem - /// @return - static IFilesystemMgr* Unmount(); - - /// @brief Getter, gets the active filesystem. - /// @return - static IFilesystemMgr* GetMounted(); - - public: - virtual NodePtr Create(_Input const Char* path) = 0; - virtual NodePtr CreateAlias(_Input const Char* path) = 0; - virtual NodePtr CreateDirectory(_Input const Char* path) = 0; - virtual NodePtr CreateSwapFile(const Char* path) = 0; - - public: - virtual bool Remove(_Input const Char* path) = 0; - - public: - virtual NodePtr Open(_Input const Char* path, _Input const Char* r) = 0; - - public: - virtual Void Write(_Input NodePtr node, _Input VoidPtr data, _Input Int32 flags, _Input SizeT size) = 0; - - virtual _Output VoidPtr Read(_Input NodePtr node, - _Input Int32 flags, - _Input SizeT sz) = 0; - - virtual Void Write(_Input const Char* name, - _Input NodePtr node, - _Input VoidPtr data, - _Input Int32 flags, - _Input SizeT size) = 0; - - virtual _Output VoidPtr Read(_Input const Char* name, - _Input NodePtr node, - _Input Int32 flags, - _Input SizeT sz) = 0; - - public: - virtual bool Seek(_Input NodePtr node, _Input SizeT off) = 0; - - public: - virtual SizeT Tell(_Input NodePtr node) = 0; - virtual bool Rewind(_Input NodePtr node) = 0; - }; - -#ifdef __FSKIT_USE_NEFS__ - /** - * @brief Based of IFilesystemMgr, takes care of managing NeFS - * disks. - */ - class NeFileSystemMgr final : public IFilesystemMgr - { - public: - explicit NeFileSystemMgr(); - ~NeFileSystemMgr() override; - - public: - ZKA_COPY_DEFAULT(NeFileSystemMgr); - - public: - NodePtr Create(const Char* path) override; - NodePtr CreateAlias(const Char* path) override; - NodePtr CreateDirectory(const Char* path) override; - NodePtr CreateSwapFile(const Char* path) override; - - public: - bool Remove(_Input const Char* path) override; - NodePtr Open(_Input const Char* path, _Input const Char* r) override; - Void Write(_Input NodePtr node, _Input VoidPtr data, _Input Int32 flags, _Input SizeT sz) override; - VoidPtr Read(_Input NodePtr node, _Input Int32 flags, _Input SizeT sz) override; - bool Seek(_Input NodePtr node, _Input SizeT off) override; - SizeT Tell(_Input NodePtr node) override; - bool Rewind(_Input NodePtr node) override; - - Void Write(_Input const Char* name, - _Input NodePtr node, - _Input VoidPtr data, - _Input Int32 flags, - _Input SizeT size) override; - - _Output VoidPtr Read(_Input const Char* name, - _Input NodePtr node, - _Input Int32 flags, - _Input SizeT sz) override; - - public: - /// @brief Get NeFS parser class. - /// @return The filesystem parser class. - NeFSParser* GetParser() noexcept; - - private: - NeFSParser* fImpl{nullptr}; - }; - -#endif // ifdef __FSKIT_USE_NEFS__ - - /** - * Usable FileStream - * @tparam Encoding file encoding (char, wchar_t...) - * @tparam FSClass Filesystem contract who takes care of it. - */ - template - class FileStream final - { - public: - explicit FileStream(const Encoding* path, const Encoding* restrict_type); - ~FileStream(); - - public: - FileStream& operator=(const FileStream&); - FileStream(const FileStream&); - - public: - ErrorOr WriteAll(const VoidPtr data) noexcept - { - if (this->fFileRestrict != eRestrictReadWrite && - this->fFileRestrict != eRestrictReadWriteBinary && - this->fFileRestrict != eRestrictWrite && - this->fFileRestrict != eRestrictWriteBinary) - return ErrorOr(kErrorInvalidData); - - if (data == nullptr) - return ErrorOr(kErrorInvalidData); - - auto man = FSClass::GetMounted(); - - if (man) - { - man->Write(fFile, data, cFileWriteAll); - return ErrorOr(0); - } - - return ErrorOr(kErrorInvalidData); - } - - VoidPtr ReadAll() noexcept - { - if (this->fFileRestrict != eRestrictReadWrite && - this->fFileRestrict != eRestrictReadWriteBinary && - this->fFileRestrict != eRestrictRead && - this->fFileRestrict != eRestrictReadBinary) - return nullptr; - - auto man = FSClass::GetMounted(); - - if (man) - { - VoidPtr ret = man->Read(fFile, cFileReadAll, 0); - return ret; - } - - return nullptr; - } - - ErrorOr WriteAll(const Char* fName, const VoidPtr data) noexcept - { - if (this->fFileRestrict != eRestrictReadWrite && - this->fFileRestrict != eRestrictReadWriteBinary && - this->fFileRestrict != eRestrictWrite && - this->fFileRestrict != eRestrictWriteBinary) - return ErrorOr(kErrorInvalidData); - - if (data == nullptr) - return ErrorOr(kErrorInvalidData); - - auto man = FSClass::GetMounted(); - - if (man) - { - man->Write(fName, fFile, data, cFileWriteAll); - return ErrorOr(0); - } - - return ErrorOr(kErrorInvalidData); - } - - VoidPtr Read(const Char* fName) noexcept - { - if (this->fFileRestrict != eRestrictReadWrite && - this->fFileRestrict != eRestrictReadWriteBinary && - this->fFileRestrict != eRestrictRead && - this->fFileRestrict != eRestrictReadBinary) - return nullptr; - - auto man = FSClass::GetMounted(); - - if (man) - { - VoidPtr ret = man->Read(fName, fFile, cFileReadAll, 0); - return ret; - } - - return nullptr; - } - - VoidPtr Read(SizeT offset, SizeT sz) - { - if (this->fFileRestrict != eRestrictReadWrite && - this->fFileRestrict != eRestrictReadWriteBinary && - this->fFileRestrict != eRestrictRead && - this->fFileRestrict != eRestrictReadBinary) - return nullptr; - - auto man = FSClass::GetMounted(); - - if (man) - { - man->Seek(fFile, offset); - auto ret = man->Read(fFile, cFileReadChunk, sz); - - return ret; - } - - return nullptr; - } - - Void Write(SizeT offset, voidPtr data, SizeT sz) - { - if (this->fFileRestrict != eRestrictReadWrite && - this->fFileRestrict != eRestrictReadWriteBinary && - this->fFileRestrict != eRestrictWrite && - this->fFileRestrict != eRestrictWriteBinary) - return; - - auto man = FSClass::GetMounted(); - - if (man) - { - man->Seek(fFile, offset); - man->Write(fFile, data, sz, cFileReadChunk); - } - } - - public: - /// @brief Leak node pointer. - /// @return The node pointer. - NodePtr Leak() - { - return fFile; - } - - /// @brief Leak MIME. - /// @return The MIME. - Char* MIME() noexcept - { - return const_cast(fMime); - } - - enum - { - eRestrictRead, - eRestrictReadBinary, - eRestrictWrite, - eRestrictWriteBinary, - eRestrictReadWrite, - eRestrictReadWriteBinary, - }; - - private: - NodePtr fFile{nullptr}; - Int32 fFileRestrict{}; - const Char* fMime{cFileMimeGeneric}; - }; - - using FileStreamUTF8 = FileStream; - using FileStreamUTF16 = FileStream; - - typedef UInt64 CursorType; - - /// @brief constructor - template - FileStream::FileStream(const Encoding* path, - const Encoding* restrict_type) - : fFile(Class::GetMounted()->Open(path, restrict_type)) - { - static const auto cLength = 255; - - /// @brief restrict information about the file descriptor. - struct RESTRICT_MAP final - { - Char fRestrict[cLength]; - Int32 fMappedTo; - }; - - const SizeT cRestrictCount = cRestrictMax; - const RESTRICT_MAP cRestrictList[] = { - { - .fRestrict = cRestrictR, - .fMappedTo = eRestrictRead, - }, - { - .fRestrict = cRestrictRB, - .fMappedTo = eRestrictReadBinary, - }, - { - .fRestrict = cRestrictRWB, - .fMappedTo = eRestrictReadWriteBinary, - }, - { - .fRestrict = cRestrictW, - .fMappedTo = eRestrictWrite, - }, - { - .fRestrict = cRestrictWB, - .fMappedTo = eRestrictReadWrite, - }}; - - for (SizeT index = 0; index < cRestrictCount; ++index) - { - if (rt_string_cmp(restrict_type, cRestrictList[index].fRestrict, - rt_string_len(cRestrictList[index].fRestrict)) == 0) - { - fFileRestrict = cRestrictList[index].fMappedTo; - break; - } - } - - kcout << "new file: " << path << ".\r"; - } - - /// @brief destructor - template - FileStream::~FileStream() - { - mm_delete_heap(fFile); - } -} // namespace Kernel diff --git a/dev/zka/KernelKit/HardwareThreadScheduler.h b/dev/zka/KernelKit/HardwareThreadScheduler.h new file mode 100644 index 00000000..15848d97 --- /dev/null +++ b/dev/zka/KernelKit/HardwareThreadScheduler.h @@ -0,0 +1,149 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#ifndef __INC_MP_MANAGER_HPP__ +#define __INC_MP_MANAGER_HPP__ + +#include +#include +#include + +/// @note Last Rev Sun 28 Jul CET 2024 +/// @note Last Rev Thu, Aug 1, 2024 9:07:38 AM + +#define cMaxHartInsideSched (8U) + +namespace Kernel +{ + class HardwareThread; + class HardwareThreadScheduler; + + using ThreadID = UInt32; + + enum ThreadKind + { + kHartSystemReserved, // System reserved thread, well user can't use it + kHartStandard, // user thread, cannot be used by Kernel + kHartFallback, // fallback thread, cannot be used by user if not clear or + // used by Kernel. + kHartBoot, // The core we booted from, the mama. + kInvalidHart, + kHartCount, + }; + + typedef enum ThreadKind ThreadKind; + typedef ThreadID ThreadID; + + /***********************************************************************************/ + /// + /// \name HardwareThread + /// \brief Abstraction over the CPU's core, used to run processes or threads. + /// + /***********************************************************************************/ + + class HardwareThread final + { + public: + explicit HardwareThread(); + ~HardwareThread(); + + public: + ZKA_COPY_DEFAULT(HardwareThread) + + public: + operator bool(); + + public: + void Wake(const bool wakeup = false) noexcept; + void Busy(const bool busy = false) noexcept; + + public: + bool Switch(VoidPtr image, UInt8* stack_ptr, HAL::StackFramePtr frame); + bool IsWakeup() noexcept; + + public: + HAL::StackFramePtr StackFrame() noexcept; + const ThreadKind& Kind() noexcept; + bool IsBusy() noexcept; + const ThreadID& ID() noexcept; + + private: + HAL::StackFramePtr fStack{nullptr}; + ThreadKind fKind{ThreadKind::kHartStandard}; + ThreadID fID{0}; + ProcessID fSourcePID{-1}; + Bool fWakeup{false}; + Bool fBusy{false}; + UInt64 fPTime{0}; + + private: + friend class HardwareThreadScheduler; + friend class UserProcessHelper; + }; + + /// + /// \name HardwareThreadScheduler + /// \brief Class to manage the thread scheduling. + /// + + class HardwareThreadScheduler final : public ISchedulerObject + { + private: + friend class UserProcessHelper; + + public: + explicit HardwareThreadScheduler(); + ~HardwareThreadScheduler(); + ZKA_COPY_DEFAULT(HardwareThreadScheduler); + + public: + HAL::StackFramePtr Leak() noexcept; + + public: + Ref operator[](const SizeT& idx); + bool operator!() noexcept; + operator bool() noexcept; + + const Bool IsUser() override + { + return Yes; + } + + const Bool IsKernel() override + { + return No; + } + + const Bool HasMP() override + { + return kHandoverHeader->f_HardwareTables.f_MultiProcessingEnabled; + } + + public: + /// @brief Shared instance of the MP Mgr. + /// @return the reference to the mp manager class. + STATIC HardwareThreadScheduler& The(); + + public: + /// @brief Returns the amount of threads present in the system. + /// @returns SizeT the amount of cores present. + SizeT Count() noexcept; + + private: + Array fThreadList; + ThreadID fCurrentThread{0}; + }; + + /// @brief wakes up thread. + /// wakes up thread from hang. + Void mp_wakeup_thread(HAL::StackFramePtr stack); + + /// @brief makes thread sleep. + /// hooks and hangs thread to prevent code from executing. + Void mp_hang_thread(HAL::StackFramePtr stack); +} // namespace Kernel + +#endif // !__INC_MP_MANAGER_HPP__ diff --git a/dev/zka/KernelKit/HardwareThreadScheduler.hxx b/dev/zka/KernelKit/HardwareThreadScheduler.hxx deleted file mode 100644 index c026f6ac..00000000 --- a/dev/zka/KernelKit/HardwareThreadScheduler.hxx +++ /dev/null @@ -1,149 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#ifndef __INC_MP_MANAGER_HPP__ -#define __INC_MP_MANAGER_HPP__ - -#include -#include -#include - -/// @note Last Rev Sun 28 Jul CET 2024 -/// @note Last Rev Thu, Aug 1, 2024 9:07:38 AM - -#define cMaxHartInsideSched (8U) - -namespace Kernel -{ - class HardwareThread; - class HardwareThreadScheduler; - - using ThreadID = UInt32; - - enum ThreadKind - { - kHartSystemReserved, // System reserved thread, well user can't use it - kHartStandard, // user thread, cannot be used by Kernel - kHartFallback, // fallback thread, cannot be used by user if not clear or - // used by Kernel. - kHartBoot, // The core we booted from, the mama. - kInvalidHart, - kHartCount, - }; - - typedef enum ThreadKind ThreadKind; - typedef ThreadID ThreadID; - - /***********************************************************************************/ - /// - /// \name HardwareThread - /// \brief Abstraction over the CPU's core, used to run processes or threads. - /// - /***********************************************************************************/ - - class HardwareThread final - { - public: - explicit HardwareThread(); - ~HardwareThread(); - - public: - ZKA_COPY_DEFAULT(HardwareThread) - - public: - operator bool(); - - public: - void Wake(const bool wakeup = false) noexcept; - void Busy(const bool busy = false) noexcept; - - public: - bool Switch(VoidPtr image, UInt8* stack_ptr, HAL::StackFramePtr frame); - bool IsWakeup() noexcept; - - public: - HAL::StackFramePtr StackFrame() noexcept; - const ThreadKind& Kind() noexcept; - bool IsBusy() noexcept; - const ThreadID& ID() noexcept; - - private: - HAL::StackFramePtr fStack{nullptr}; - ThreadKind fKind{ThreadKind::kHartStandard}; - ThreadID fID{0}; - ProcessID fSourcePID{-1}; - Bool fWakeup{false}; - Bool fBusy{false}; - UInt64 fPTime{0}; - - private: - friend class HardwareThreadScheduler; - friend class UserProcessHelper; - }; - - /// - /// \name HardwareThreadScheduler - /// \brief Class to manage the thread scheduling. - /// - - class HardwareThreadScheduler final : public ISchedulerObject - { - private: - friend class UserProcessHelper; - - public: - explicit HardwareThreadScheduler(); - ~HardwareThreadScheduler(); - ZKA_COPY_DEFAULT(HardwareThreadScheduler); - - public: - HAL::StackFramePtr Leak() noexcept; - - public: - Ref operator[](const SizeT& idx); - bool operator!() noexcept; - operator bool() noexcept; - - const Bool IsUser() override - { - return Yes; - } - - const Bool IsKernel() override - { - return No; - } - - const Bool HasMP() override - { - return kHandoverHeader->f_HardwareTables.f_MultiProcessingEnabled; - } - - public: - /// @brief Shared instance of the MP Mgr. - /// @return the reference to the mp manager class. - STATIC HardwareThreadScheduler& The(); - - public: - /// @brief Returns the amount of threads present in the system. - /// @returns SizeT the amount of cores present. - SizeT Count() noexcept; - - private: - Array fThreadList; - ThreadID fCurrentThread{0}; - }; - - /// @brief wakes up thread. - /// wakes up thread from hang. - Void mp_wakeup_thread(HAL::StackFramePtr stack); - - /// @brief makes thread sleep. - /// hooks and hangs thread to prevent code from executing. - Void mp_hang_thread(HAL::StackFramePtr stack); -} // namespace Kernel - -#endif // !__INC_MP_MANAGER_HPP__ diff --git a/dev/zka/KernelKit/Heap.h b/dev/zka/KernelKit/Heap.h new file mode 100644 index 00000000..a136b42f --- /dev/null +++ b/dev/zka/KernelKit/Heap.h @@ -0,0 +1,74 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#ifndef INC_KERNEL_HEAP_HXX +#define INC_KERNEL_HEAP_HXX + +// last-rev 30/01/24 +// file: Heap.h +// description: heap allocation for the Kernel. + +#include +#include + +namespace Kernel +{ + /// @brief Declare pointer as free. + /// @param heap_ptr the pointer. + /// @return a status code regarding the deallocation. + Int32 mm_delete_heap(VoidPtr heap_ptr); + + /// @brief Declare a new size for heap_ptr. + /// @param heap_ptr the pointer. + /// @return unsupported always returns nullptr. + VoidPtr mm_realloc_heap(VoidPtr heap_ptr, SizeT new_sz); + + /// @brief Check if pointer is a valid Kernel pointer. + /// @param heap_ptr the pointer + /// @return if it exists it returns true. + Boolean mm_is_valid_heap(VoidPtr heap_ptr); + + /// @brief Allocate chunk of memory. + /// @param sz Size of pointer + /// @param wr Read Write bit. + /// @param user User enable bit. + /// @return The newly allocated pointer, or nullptr. + VoidPtr mm_new_heap(const SizeT sz, const Bool wr, const Bool user); + + /// @brief Protect the heap with a CRC value. + /// @param heap_ptr pointer. + /// @return if it valid: point has crc now., otherwise fail. + Boolean mm_protect_heap(VoidPtr heap_ptr); + + /// @brief Makes a Kernel page. + /// @param heap_ptr the page pointer. + /// @return status code + Int32 mm_make_ke_page(VoidPtr heap_ptr); + + /// @brief Allocate C++ class. + template + inline T* mm_new_class(Args&&... args) + { + T* cls = new T(move(args)...); + + if (cls == nullptr) + { + ke_stop(RUNTIME_CHECK_POINTER); + } + + return cls; + } + + /// @brief Free C++ class. + template + inline Void mm_delete_class(T* cls) + { + delete cls; + cls = nullptr; + } +} // namespace Kernel + +#endif // !INC_KERNEL_HEAP_HXX diff --git a/dev/zka/KernelKit/Heap.hxx b/dev/zka/KernelKit/Heap.hxx deleted file mode 100644 index cfa3fc94..00000000 --- a/dev/zka/KernelKit/Heap.hxx +++ /dev/null @@ -1,74 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#ifndef INC_KERNEL_HEAP_HXX -#define INC_KERNEL_HEAP_HXX - -// last-rev 30/01/24 -// file: Heap.hxx -// description: heap allocation for the Kernel. - -#include -#include - -namespace Kernel -{ - /// @brief Declare pointer as free. - /// @param heap_ptr the pointer. - /// @return a status code regarding the deallocation. - Int32 mm_delete_heap(VoidPtr heap_ptr); - - /// @brief Declare a new size for heap_ptr. - /// @param heap_ptr the pointer. - /// @return unsupported always returns nullptr. - VoidPtr mm_realloc_heap(VoidPtr heap_ptr, SizeT new_sz); - - /// @brief Check if pointer is a valid Kernel pointer. - /// @param heap_ptr the pointer - /// @return if it exists it returns true. - Boolean mm_is_valid_heap(VoidPtr heap_ptr); - - /// @brief Allocate chunk of memory. - /// @param sz Size of pointer - /// @param wr Read Write bit. - /// @param user User enable bit. - /// @return The newly allocated pointer, or nullptr. - VoidPtr mm_new_heap(const SizeT sz, const Bool wr, const Bool user); - - /// @brief Protect the heap with a CRC value. - /// @param heap_ptr pointer. - /// @return if it valid: point has crc now., otherwise fail. - Boolean mm_protect_heap(VoidPtr heap_ptr); - - /// @brief Makes a Kernel page. - /// @param heap_ptr the page pointer. - /// @return status code - Int32 mm_make_ke_page(VoidPtr heap_ptr); - - /// @brief Allocate C++ class. - template - inline T* mm_new_class(Args&&... args) - { - T* cls = new T(move(args)...); - - if (cls == nullptr) - { - ke_stop(RUNTIME_CHECK_POINTER); - } - - return cls; - } - - /// @brief Free C++ class. - template - inline Void mm_delete_class(T* cls) - { - delete cls; - cls = nullptr; - } -} // namespace Kernel - -#endif // !INC_KERNEL_HEAP_HXX diff --git a/dev/zka/KernelKit/IDLLObject.h b/dev/zka/KernelKit/IDLLObject.h new file mode 100644 index 00000000..e3b09ccc --- /dev/null +++ b/dev/zka/KernelKit/IDLLObject.h @@ -0,0 +1,45 @@ +/* + * ======================================================== + * + * Kernel + * Copyright ZKA Web Services Co., all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include +#include + +namespace Kernel +{ + class IDLLObject + { + public: + explicit IDLLObject() = default; + virtual ~IDLLObject() = default; + + struct DLL_TRAITS final + { + VoidPtr fImageObject{nullptr}; + VoidPtr fImageEntrypointOffset{nullptr}; + + Bool IsValid() + { + return fImageObject && fImageEntrypointOffset; + } + }; + + ZKA_COPY_DEFAULT(IDLLObject); + + virtual DLL_TRAITS** GetAddressOf() = 0; + virtual DLL_TRAITS* Get() = 0; + + virtual Void Mount(DLL_TRAITS* to_mount) = 0; + virtual Void Unmount() = 0; + }; + + /// @brief Pure implementation, missing method/function handler. + EXTERN_C void __zka_pure_call(void); +} // namespace Kernel diff --git a/dev/zka/KernelKit/IDLLObject.hxx b/dev/zka/KernelKit/IDLLObject.hxx deleted file mode 100644 index 1755b403..00000000 --- a/dev/zka/KernelKit/IDLLObject.hxx +++ /dev/null @@ -1,45 +0,0 @@ -/* - * ======================================================== - * - * Kernel - * Copyright ZKA Web Services Co., all rights reserved. - * - * ======================================================== - */ - -#pragma once - -#include -#include - -namespace Kernel -{ - class IDLLObject - { - public: - explicit IDLLObject() = default; - virtual ~IDLLObject() = default; - - struct DLL_TRAITS final - { - VoidPtr fImageObject{nullptr}; - VoidPtr fImageEntrypointOffset{nullptr}; - - Bool IsValid() - { - return fImageObject && fImageEntrypointOffset; - } - }; - - ZKA_COPY_DEFAULT(IDLLObject); - - virtual DLL_TRAITS** GetAddressOf() = 0; - virtual DLL_TRAITS* Get() = 0; - - virtual Void Mount(DLL_TRAITS* to_mount) = 0; - virtual Void Unmount() = 0; - }; - - /// @brief Pure implementation, missing method/function handler. - EXTERN_C void __zka_pure_call(void); -} // namespace Kernel diff --git a/dev/zka/KernelKit/IPEFDLLObject.h b/dev/zka/KernelKit/IPEFDLLObject.h new file mode 100644 index 00000000..8c0703ed --- /dev/null +++ b/dev/zka/KernelKit/IPEFDLLObject.h @@ -0,0 +1,106 @@ +/* + * ======================================================== + * + * Kernel + * Copyright ZKA Web Services Co., all rights reserved. + * + * ======================================================== + */ + +#ifndef __KERNELKIT_SHARED_OBJECT_HXX__ +#define __KERNELKIT_SHARED_OBJECT_HXX__ + +#include +#include +#include +#include +#include + +namespace Kernel +{ + /** + * @brief Shared Library class + * Load library from this class + */ + class IPEFDLLObject final : public IDLLObject + { + public: + explicit IPEFDLLObject() = default; + ~IPEFDLLObject() = default; + + public: + ZKA_COPY_DEFAULT(IPEFDLLObject); + + private: + DLL_TRAITS* fMounted{nullptr}; + + public: + DLL_TRAITS** GetAddressOf() + { + return &fMounted; + } + + DLL_TRAITS* Get() + { + return fMounted; + } + + public: + void Mount(DLL_TRAITS* to_mount) + { + if (!to_mount || !to_mount->fImageObject) + return; + + fMounted = to_mount; + + if (fLoader && to_mount) + { + delete fLoader; + fLoader = nullptr; + } + + if (!fLoader) + { + fLoader = new PEFLoader(fMounted->fImageObject); + } + } + + void Unmount() + { + if (fMounted) + fMounted = nullptr; + }; + + template + SymbolType Load(const Char* symbol_name, SizeT len, Int32 kind) + { + if (symbol_name == nullptr || *symbol_name == 0) + return nullptr; + if (len > kPathLen || len < 1) + return nullptr; + + auto ret = + reinterpret_cast(fLoader->FindSymbol(symbol_name, kind)); + + if (!ret) + { + if (kind == kPefCode) + return (VoidPtr)&__zka_pure_call; + + return nullptr; + } + + return ret; + } + + private: + PEFLoader* fLoader{nullptr}; + }; + + typedef IPEFDLLObject* IDLL; + + EXTERN_C IDLL rtl_init_dll(UserProcess* header); + EXTERN_C Void rtl_fini_dll(UserProcess* header, IDLL lib, Bool* successful); +} // namespace Kernel + +#endif /* ifndef __KERNELKIT_SHARED_OBJECT_HXX__ */ diff --git a/dev/zka/KernelKit/IPEFDLLObject.hxx b/dev/zka/KernelKit/IPEFDLLObject.hxx deleted file mode 100644 index 7852381d..00000000 --- a/dev/zka/KernelKit/IPEFDLLObject.hxx +++ /dev/null @@ -1,106 +0,0 @@ -/* - * ======================================================== - * - * Kernel - * Copyright ZKA Web Services Co., all rights reserved. - * - * ======================================================== - */ - -#ifndef __KERNELKIT_SHARED_OBJECT_HXX__ -#define __KERNELKIT_SHARED_OBJECT_HXX__ - -#include -#include -#include -#include -#include - -namespace Kernel -{ - /** - * @brief Shared Library class - * Load library from this class - */ - class IPEFDLLObject final : public IDLLObject - { - public: - explicit IPEFDLLObject() = default; - ~IPEFDLLObject() = default; - - public: - ZKA_COPY_DEFAULT(IPEFDLLObject); - - private: - DLL_TRAITS* fMounted{nullptr}; - - public: - DLL_TRAITS** GetAddressOf() - { - return &fMounted; - } - - DLL_TRAITS* Get() - { - return fMounted; - } - - public: - void Mount(DLL_TRAITS* to_mount) - { - if (!to_mount || !to_mount->fImageObject) - return; - - fMounted = to_mount; - - if (fLoader && to_mount) - { - delete fLoader; - fLoader = nullptr; - } - - if (!fLoader) - { - fLoader = new PEFLoader(fMounted->fImageObject); - } - } - - void Unmount() - { - if (fMounted) - fMounted = nullptr; - }; - - template - SymbolType Load(const Char* symbol_name, SizeT len, Int32 kind) - { - if (symbol_name == nullptr || *symbol_name == 0) - return nullptr; - if (len > kPathLen || len < 1) - return nullptr; - - auto ret = - reinterpret_cast(fLoader->FindSymbol(symbol_name, kind)); - - if (!ret) - { - if (kind == kPefCode) - return (VoidPtr)&__zka_pure_call; - - return nullptr; - } - - return ret; - } - - private: - PEFLoader* fLoader{nullptr}; - }; - - typedef IPEFDLLObject* IDLL; - - EXTERN_C IDLL rtl_init_dll(UserProcess* header); - EXTERN_C Void rtl_fini_dll(UserProcess* header, IDLL lib, Bool* successful); -} // namespace Kernel - -#endif /* ifndef __KERNELKIT_SHARED_OBJECT_HXX__ */ diff --git a/dev/zka/KernelKit/LPC.h b/dev/zka/KernelKit/LPC.h new file mode 100644 index 00000000..b0b0fae8 --- /dev/null +++ b/dev/zka/KernelKit/LPC.h @@ -0,0 +1,61 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include + +/// @file LPC.h +/// @brief Local Process Codes. + +#define ErrLocalIsOk() (Kernel::UserProcessScheduler::The().CurrentProcess().Leak().GetLocalCode() == Kernel::kErrorSuccess) +#define ErrLocalFailed() (Kernel::UserProcessScheduler::The().CurrentProcess().Leak().GetLocalCode() != Kernel::kErrorSuccess) +#define ErrLocal() Kernel::UserProcessScheduler::The().CurrentProcess().Leak().GetLocalCode() + +namespace Kernel +{ + typedef Int32 HError; + + inline constexpr HError kErrorSuccess = 0; + inline constexpr HError kErrorExecutable = 33; + inline constexpr HError kErrorExecutableLib = 34; + inline constexpr HError kErrorFileNotFound = 35; + inline constexpr HError kErrorDirectoryNotFound = 36; + inline constexpr HError kErrorDiskReadOnly = 37; + inline constexpr HError kErrorDiskIsFull = 38; + inline constexpr HError kErrorProcessFault = 39; + inline constexpr HError kErrorSocketHangUp = 40; + inline constexpr HError kErrorThreadLocalStorage = 41; + inline constexpr HError kErrorMath = 42; + inline constexpr HError kErrorNoNetwork = 43; + inline constexpr HError kErrorHeapOutOfMemory = 44; + inline constexpr HError kErrorNoSuchDisk = 45; + inline constexpr HError kErrorFileExists = 46; + inline constexpr HError kErrorFormatFailed = 47; + inline constexpr HError kErrorNetworkTimeout = 48; + inline constexpr HError kErrorInternal = 49; + inline constexpr HError kErrorForkAlreadyExists = 50; + inline constexpr HError kErrorOutOfTeamSlot = 51; + inline constexpr HError kErrorHeapNotPresent = 52; + inline constexpr HError kErrorNoEntrypoint = 53; + inline constexpr HError kErrorDiskIsCorrupted = 54; + inline constexpr HError kErrorDisk = 55; + inline constexpr HError kErrorInvalidData = 56; + inline constexpr HError kErrorAsync = 57; + inline constexpr HError kErrorNonBlocking = 58; + inline constexpr HError kErrorIPC = 59; + inline constexpr HError kErrorSign = 60; + inline constexpr HError kErrorInvalidCreds = 61; + inline constexpr HError kErrorUnimplemented = 0; + + /// @brief Reports a bug to system check. + Void err_bug_check_raise(void) noexcept; + + /// @brief Does a system wide bug check. + /// @param void no params. + /// @return if error-free: false, otherwise true. + Boolean err_bug_check(void) noexcept; +} // namespace Kernel diff --git a/dev/zka/KernelKit/LPC.hxx b/dev/zka/KernelKit/LPC.hxx deleted file mode 100644 index 57c38390..00000000 --- a/dev/zka/KernelKit/LPC.hxx +++ /dev/null @@ -1,61 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include - -/// @file LPC.hxx -/// @brief Local Process Codes. - -#define ErrLocalIsOk() (Kernel::UserProcessScheduler::The().CurrentProcess().Leak().GetLocalCode() == Kernel::kErrorSuccess) -#define ErrLocalFailed() (Kernel::UserProcessScheduler::The().CurrentProcess().Leak().GetLocalCode() != Kernel::kErrorSuccess) -#define ErrLocal() Kernel::UserProcessScheduler::The().CurrentProcess().Leak().GetLocalCode() - -namespace Kernel -{ - typedef Int32 HError; - - inline constexpr HError kErrorSuccess = 0; - inline constexpr HError kErrorExecutable = 33; - inline constexpr HError kErrorExecutableLib = 34; - inline constexpr HError kErrorFileNotFound = 35; - inline constexpr HError kErrorDirectoryNotFound = 36; - inline constexpr HError kErrorDiskReadOnly = 37; - inline constexpr HError kErrorDiskIsFull = 38; - inline constexpr HError kErrorProcessFault = 39; - inline constexpr HError kErrorSocketHangUp = 40; - inline constexpr HError kErrorThreadLocalStorage = 41; - inline constexpr HError kErrorMath = 42; - inline constexpr HError kErrorNoNetwork = 43; - inline constexpr HError kErrorHeapOutOfMemory = 44; - inline constexpr HError kErrorNoSuchDisk = 45; - inline constexpr HError kErrorFileExists = 46; - inline constexpr HError kErrorFormatFailed = 47; - inline constexpr HError kErrorNetworkTimeout = 48; - inline constexpr HError kErrorInternal = 49; - inline constexpr HError kErrorForkAlreadyExists = 50; - inline constexpr HError kErrorOutOfTeamSlot = 51; - inline constexpr HError kErrorHeapNotPresent = 52; - inline constexpr HError kErrorNoEntrypoint = 53; - inline constexpr HError kErrorDiskIsCorrupted = 54; - inline constexpr HError kErrorDisk = 55; - inline constexpr HError kErrorInvalidData = 56; - inline constexpr HError kErrorAsync = 57; - inline constexpr HError kErrorNonBlocking = 58; - inline constexpr HError kErrorIPC = 59; - inline constexpr HError kErrorSign = 60; - inline constexpr HError kErrorInvalidCreds = 61; - inline constexpr HError kErrorUnimplemented = 0; - - /// @brief Reports a bug to system check. - Void err_bug_check_raise(void) noexcept; - - /// @brief Does a system wide bug check. - /// @param void no params. - /// @return if error-free: false, otherwise true. - Boolean err_bug_check(void) noexcept; -} // namespace Kernel diff --git a/dev/zka/KernelKit/LoaderInterface.h b/dev/zka/KernelKit/LoaderInterface.h new file mode 100644 index 00000000..843b42d8 --- /dev/null +++ b/dev/zka/KernelKit/LoaderInterface.h @@ -0,0 +1,33 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include +#include +#include +#include + +namespace Kernel +{ + /// @brief This interface is used to make loader contracts (MSCOFF, PEF). + /// @author @Amlal-El-Mahrouss + class LoaderInterface + { + public: + explicit LoaderInterface() = default; + virtual ~LoaderInterface() = default; + + ZKA_COPY_DEFAULT(LoaderInterface); + + public: + virtual _Output const Char* AsString() = 0; + virtual _Output const Char* MIME() = 0; + virtual _Output const Char* Path() = 0; + virtual _Output ErrorOr FindStart() = 0; + virtual _Output VoidPtr FindSymbol(_Input const Char* name, _Input Int32 kind) = 0; + }; +} // namespace Kernel diff --git a/dev/zka/KernelKit/LoaderInterface.hxx b/dev/zka/KernelKit/LoaderInterface.hxx deleted file mode 100644 index 246978dd..00000000 --- a/dev/zka/KernelKit/LoaderInterface.hxx +++ /dev/null @@ -1,33 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include -#include -#include -#include - -namespace Kernel -{ - /// @brief This interface is used to make loader contracts (MSCOFF, PEF). - /// @author @Amlal-El-Mahrouss - class LoaderInterface - { - public: - explicit LoaderInterface() = default; - virtual ~LoaderInterface() = default; - - ZKA_COPY_DEFAULT(LoaderInterface); - - public: - virtual _Output const Char* AsString() = 0; - virtual _Output const Char* MIME() = 0; - virtual _Output const Char* Path() = 0; - virtual _Output ErrorOr FindStart() = 0; - virtual _Output VoidPtr FindSymbol(_Input const Char* name, _Input Int32 kind) = 0; - }; -} // namespace Kernel diff --git a/dev/zka/KernelKit/LockDelegate.h b/dev/zka/KernelKit/LockDelegate.h new file mode 100644 index 00000000..8f7e9a42 --- /dev/null +++ b/dev/zka/KernelKit/LockDelegate.h @@ -0,0 +1,67 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include +#include + +namespace Kernel +{ + enum + { + kLockDone = 200, + kLockTimedOut, + }; + + /// @brief Lock condition pointer. + typedef Boolean* LockPtr; + + /// @brief Locking delegate class, hangs until limit. + /// @tparam N the amount of cycles to wait. + template + class LockDelegate final + { + public: + LockDelegate() = delete; + + public: + explicit LockDelegate(LockPtr expr) + { + auto spin = 0U; + + while (spin != N) + { + if (*expr) + { + fLockStatus | kLockDone; + break; + } + } + + if (spin == N) + fLockStatus | kLockTimedOut; + } + + ~LockDelegate() = default; + + LockDelegate& operator=(const LockDelegate&) = delete; + LockDelegate(const LockDelegate&) = delete; + + bool Done() + { + return fLockStatus[kLockDone] == kLockDone; + } + + bool HasTimedOut() + { + return fLockStatus[kLockTimedOut] != kLockTimedOut; + } + + private: + Atom fLockStatus; + }; +} // namespace Kernel diff --git a/dev/zka/KernelKit/LockDelegate.hxx b/dev/zka/KernelKit/LockDelegate.hxx deleted file mode 100644 index 0181fc9b..00000000 --- a/dev/zka/KernelKit/LockDelegate.hxx +++ /dev/null @@ -1,67 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include -#include - -namespace Kernel -{ - enum - { - kLockDone = 200, - kLockTimedOut, - }; - - /// @brief Lock condition pointer. - typedef Boolean* LockPtr; - - /// @brief Locking delegate class, hangs until limit. - /// @tparam N the amount of cycles to wait. - template - class LockDelegate final - { - public: - LockDelegate() = delete; - - public: - explicit LockDelegate(LockPtr expr) - { - auto spin = 0U; - - while (spin != N) - { - if (*expr) - { - fLockStatus | kLockDone; - break; - } - } - - if (spin == N) - fLockStatus | kLockTimedOut; - } - - ~LockDelegate() = default; - - LockDelegate& operator=(const LockDelegate&) = delete; - LockDelegate(const LockDelegate&) = delete; - - bool Done() - { - return fLockStatus[kLockDone] == kLockDone; - } - - bool HasTimedOut() - { - return fLockStatus[kLockTimedOut] != kLockTimedOut; - } - - private: - Atom fLockStatus; - }; -} // namespace Kernel diff --git a/dev/zka/KernelKit/MSDOS.h b/dev/zka/KernelKit/MSDOS.h new file mode 100644 index 00000000..b9f8f13c --- /dev/null +++ b/dev/zka/KernelKit/MSDOS.h @@ -0,0 +1,52 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: MSDOS.hpp + Purpose: MS-DOS header for Kernel. + + Revision History: + + 30/01/24: Added file (amlel) + +------------------------------------------- */ + +#ifndef __MSDOS_EXEC__ +#define __MSDOS_EXEC__ + +#include +#include + +// Last Rev +// Sat Feb 24 CET 2024 + +#define kMagMz0 'M' +#define kMagMz1 'Z' + +typedef Kernel::UInt32 DosWord; +typedef Kernel::Long DosLong; + +typedef struct _DosHeader +{ + Kernel::UInt8 eMagic[2]; + DosWord eMagLen; + DosWord ePagesCount; + DosWord eCrlc; + DosWord eCParHdr; + DosWord eMinAlloc; + DosWord eMaxAlloc; + DosWord eStackSeg; + DosWord eStackPtr; + DosWord eChksum; + DosWord eIp; + DosWord eCs; + DosWord eLfarlc; + DosWord eOvno; + DosWord eRes[4]; + DosWord eOemid; + DosWord eOeminfo; + DosWord eRes2[10]; + DosLong eLfanew; +} DosHeader, *DosHeaderPtr; + +#endif /* ifndef __MSDOS_EXEC__ */ diff --git a/dev/zka/KernelKit/MSDOS.hxx b/dev/zka/KernelKit/MSDOS.hxx deleted file mode 100644 index 1a0698b3..00000000 --- a/dev/zka/KernelKit/MSDOS.hxx +++ /dev/null @@ -1,52 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: MSDOS.hpp - Purpose: MS-DOS header for Kernel. - - Revision History: - - 30/01/24: Added file (amlel) - -------------------------------------------- */ - -#ifndef __MSDOS_EXEC__ -#define __MSDOS_EXEC__ - -#include -#include - -// Last Rev -// Sat Feb 24 CET 2024 - -#define kMagMz0 'M' -#define kMagMz1 'Z' - -typedef Kernel::UInt32 DosWord; -typedef Kernel::Long DosLong; - -typedef struct _DosHeader -{ - Kernel::UInt8 eMagic[2]; - DosWord eMagLen; - DosWord ePagesCount; - DosWord eCrlc; - DosWord eCParHdr; - DosWord eMinAlloc; - DosWord eMaxAlloc; - DosWord eStackSeg; - DosWord eStackPtr; - DosWord eChksum; - DosWord eIp; - DosWord eCs; - DosWord eLfarlc; - DosWord eOvno; - DosWord eRes[4]; - DosWord eOemid; - DosWord eOeminfo; - DosWord eRes2[10]; - DosLong eLfanew; -} DosHeader, *DosHeaderPtr; - -#endif /* ifndef __MSDOS_EXEC__ */ diff --git a/dev/zka/KernelKit/PCI/DMA.h b/dev/zka/KernelKit/PCI/DMA.h new file mode 100644 index 00000000..d13f2c05 --- /dev/null +++ b/dev/zka/KernelKit/PCI/DMA.h @@ -0,0 +1,81 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include +#include +#include +#include +#include + +namespace Kernel +{ + enum class DmaKind + { + PCI, // Bus mastering is required to be turned on. Basiaclly a request + // control system. 64-Bit access depends on the PAE bit and the device + // (if Double Address Cycle is available) + ISA, // Four DMA channels 0-3; 8 bit transfers and only a megabyte of RAM. + Invalid, + }; + + class DMAWrapper final + { + public: + explicit DMAWrapper() = delete; + + public: + explicit DMAWrapper(nullPtr) = delete; + explicit DMAWrapper(voidPtr Ptr, DmaKind Kind = DmaKind::PCI) + : fAddress(Ptr), fKind(Kind) + { + } + + public: + DMAWrapper& operator=(voidPtr Ptr); + + public: + DMAWrapper& operator=(const DMAWrapper&) = default; + DMAWrapper(const DMAWrapper&) = default; + + public: + ~DMAWrapper() = default; + + template + T* operator->(); + + template + T* Get(const UIntPtr off = 0); + + public: + operator bool(); + bool operator!(); + + public: + bool Write(const UIntPtr& bit, const UIntPtr& offset); + UIntPtr Read(const UIntPtr& offset); + Boolean Check(UIntPtr offset) const; + + public: + UIntPtr operator[](const UIntPtr& offset); + + private: + voidPtr fAddress{nullptr}; + DmaKind fKind{DmaKind::Invalid}; + + private: + friend class DMAFactory; + }; + + class DMAFactory final + { + public: + static OwnPtr> Construct(OwnPtr& dma); + }; +} // namespace Kernel + +#include diff --git a/dev/zka/KernelKit/PCI/DMA.hxx b/dev/zka/KernelKit/PCI/DMA.hxx deleted file mode 100644 index 94dd5ad5..00000000 --- a/dev/zka/KernelKit/PCI/DMA.hxx +++ /dev/null @@ -1,81 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include -#include -#include -#include -#include - -namespace Kernel -{ - enum class DmaKind - { - PCI, // Bus mastering is required to be turned on. Basiaclly a request - // control system. 64-Bit access depends on the PAE bit and the device - // (if Double Address Cycle is available) - ISA, // Four DMA channels 0-3; 8 bit transfers and only a megabyte of RAM. - Invalid, - }; - - class DMAWrapper final - { - public: - explicit DMAWrapper() = delete; - - public: - explicit DMAWrapper(nullPtr) = delete; - explicit DMAWrapper(voidPtr Ptr, DmaKind Kind = DmaKind::PCI) - : fAddress(Ptr), fKind(Kind) - { - } - - public: - DMAWrapper& operator=(voidPtr Ptr); - - public: - DMAWrapper& operator=(const DMAWrapper&) = default; - DMAWrapper(const DMAWrapper&) = default; - - public: - ~DMAWrapper() = default; - - template - T* operator->(); - - template - T* Get(const UIntPtr off = 0); - - public: - operator bool(); - bool operator!(); - - public: - bool Write(const UIntPtr& bit, const UIntPtr& offset); - UIntPtr Read(const UIntPtr& offset); - Boolean Check(UIntPtr offset) const; - - public: - UIntPtr operator[](const UIntPtr& offset); - - private: - voidPtr fAddress{nullptr}; - DmaKind fKind{DmaKind::Invalid}; - - private: - friend class DMAFactory; - }; - - class DMAFactory final - { - public: - static OwnPtr> Construct(OwnPtr& dma); - }; -} // namespace Kernel - -#include diff --git a/dev/zka/KernelKit/PCI/Database.h b/dev/zka/KernelKit/PCI/Database.h new file mode 100644 index 00000000..4985049a --- /dev/null +++ b/dev/zka/KernelKit/PCI/Database.h @@ -0,0 +1,38 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ +#pragma once + +#include +#include + +namespace Kernel +{ + namespace Types + { + // https://wiki.osdev.org/PCI + enum class PciDeviceKind : UChar + { + MassStorageController = 0x1, + NetworkController = 0x2, + DisplayController = 0x3, + MultimediaController = 0x4, + MemoryController = 0x5, + Bridge = 0x6, + CommunicationController = 0x7, + GenericSystemPeripheral = 0x8, + InputDeviceController = 0x9, + DockingStation = 0xa, + Processor = 0xb, + SerialBusController = 0xc, + WirelessController = 0xd, + IntelligentController = 0xe, + SatelliteCommunicationsController = 0xf, + CoProcessor = 0x40, + Unassgined = 0xf, + Invalid = Unassgined, + }; + } // namespace Types +} // namespace Kernel diff --git a/dev/zka/KernelKit/PCI/Database.hxx b/dev/zka/KernelKit/PCI/Database.hxx deleted file mode 100644 index 16b5c787..00000000 --- a/dev/zka/KernelKit/PCI/Database.hxx +++ /dev/null @@ -1,38 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ -#pragma once - -#include -#include - -namespace Kernel -{ - namespace Types - { - // https://wiki.osdev.org/PCI - enum class PciDeviceKind : UChar - { - MassStorageController = 0x1, - NetworkController = 0x2, - DisplayController = 0x3, - MultimediaController = 0x4, - MemoryController = 0x5, - Bridge = 0x6, - CommunicationController = 0x7, - GenericSystemPeripheral = 0x8, - InputDeviceController = 0x9, - DockingStation = 0xa, - Processor = 0xb, - SerialBusController = 0xc, - WirelessController = 0xd, - IntelligentController = 0xe, - SatelliteCommunicationsController = 0xf, - CoProcessor = 0x40, - Unassgined = 0xf, - Invalid = Unassgined, - }; - } // namespace Types -} // namespace Kernel diff --git a/dev/zka/KernelKit/PCI/Device.h b/dev/zka/KernelKit/PCI/Device.h new file mode 100644 index 00000000..cdedb92b --- /dev/null +++ b/dev/zka/KernelKit/PCI/Device.h @@ -0,0 +1,80 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ +#pragma once + +#include + +namespace Kernel::PCI +{ + enum class PciConfigKind : UShort + { + ConfigAddress = 0xCF8, + ConfigData = 0xCFC, + Invalid = 0xFFF + }; + + class Device final + { + public: + Device() = default; + + public: + explicit Device(UShort bus, UShort device, UShort function, UInt32 bar); + + Device& operator=(const Device&) = default; + + Device(const Device&) = default; + + ~Device(); + + public: + UInt Read(UInt bar, Size szData); + void Write(UInt bar, UIntPtr data, Size szData); + + public: + operator bool(); + + public: + template + UInt Read(UInt bar) + { + static_assert(sizeof(T) <= 4, "64-bit PCI addressing is unsupported"); + return Read(bar, sizeof(T)); + } + + template + void Write(UInt bar, UIntPtr data) + { + static_assert(sizeof(T) <= 4, "64-bit PCI addressing is unsupported"); + Write(bar, data, sizeof(T)); + } + + public: + UShort DeviceId(); + UShort VendorId(); + UShort InterfaceId(); + UChar Class(); + UChar Subclass(); + UChar ProgIf(); + UChar HeaderType(); + UInt32 Bar(UInt32 bar_in); + + public: + void EnableMmio(UInt32 bar_in); + void BecomeBusMaster(UInt32 bar_in); // for PCI-DMA, PC-DMA does not need that. + + UShort Vendor(); + + private: + UShort fBus; + UShort fDevice; + UShort fFunction; + UInt32 fBar; + }; +} // namespace Kernel::PCI + +EXTERN_C void NewOSPCISetCfgTarget(Kernel::UInt bar); +EXTERN_C Kernel::UInt NewOSPCIReadRaw(Kernel::UInt bar); diff --git a/dev/zka/KernelKit/PCI/Device.hxx b/dev/zka/KernelKit/PCI/Device.hxx deleted file mode 100644 index bc1b4eb7..00000000 --- a/dev/zka/KernelKit/PCI/Device.hxx +++ /dev/null @@ -1,80 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ -#pragma once - -#include - -namespace Kernel::PCI -{ - enum class PciConfigKind : UShort - { - ConfigAddress = 0xCF8, - ConfigData = 0xCFC, - Invalid = 0xFFF - }; - - class Device final - { - public: - Device() = default; - - public: - explicit Device(UShort bus, UShort device, UShort function, UInt32 bar); - - Device& operator=(const Device&) = default; - - Device(const Device&) = default; - - ~Device(); - - public: - UInt Read(UInt bar, Size szData); - void Write(UInt bar, UIntPtr data, Size szData); - - public: - operator bool(); - - public: - template - UInt Read(UInt bar) - { - static_assert(sizeof(T) <= 4, "64-bit PCI addressing is unsupported"); - return Read(bar, sizeof(T)); - } - - template - void Write(UInt bar, UIntPtr data) - { - static_assert(sizeof(T) <= 4, "64-bit PCI addressing is unsupported"); - Write(bar, data, sizeof(T)); - } - - public: - UShort DeviceId(); - UShort VendorId(); - UShort InterfaceId(); - UChar Class(); - UChar Subclass(); - UChar ProgIf(); - UChar HeaderType(); - UInt32 Bar(UInt32 bar_in); - - public: - void EnableMmio(UInt32 bar_in); - void BecomeBusMaster(UInt32 bar_in); // for PCI-DMA, PC-DMA does not need that. - - UShort Vendor(); - - private: - UShort fBus; - UShort fDevice; - UShort fFunction; - UInt32 fBar; - }; -} // namespace Kernel::PCI - -EXTERN_C void NewOSPCISetCfgTarget(Kernel::UInt bar); -EXTERN_C Kernel::UInt NewOSPCIReadRaw(Kernel::UInt bar); diff --git a/dev/zka/KernelKit/PCI/Express.h b/dev/zka/KernelKit/PCI/Express.h new file mode 100644 index 00000000..57962631 --- /dev/null +++ b/dev/zka/KernelKit/PCI/Express.h @@ -0,0 +1,11 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include + +#define PCI_EXPRESS_BUS_COUNT (4096) diff --git a/dev/zka/KernelKit/PCI/Express.hxx b/dev/zka/KernelKit/PCI/Express.hxx deleted file mode 100644 index 1e7ce5bc..00000000 --- a/dev/zka/KernelKit/PCI/Express.hxx +++ /dev/null @@ -1,11 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include - -#define PCI_EXPRESS_BUS_COUNT (4096) diff --git a/dev/zka/KernelKit/PCI/IO.h b/dev/zka/KernelKit/PCI/IO.h new file mode 100644 index 00000000..d42119f2 --- /dev/null +++ b/dev/zka/KernelKit/PCI/IO.h @@ -0,0 +1,59 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include +#include +#include +#include + +namespace Kernel +{ + template + class IOArray final + { + public: + IOArray() = delete; + + IOArray(nullPtr) = delete; + + explicit IOArray(Array& ports) + : fPorts(ports) + { + } + ~IOArray() + { + } + + IOArray& operator=(const IOArray&) = default; + + IOArray(const IOArray&) = default; + + operator bool() + { + return !fPorts.Empty(); + } + + public: + template + T In(SizeT index); + + template + void Out(SizeT index, T value); + + private: + Array fPorts; + }; + + using IOArray16 = IOArray<16>; +} // namespace Kernel + +#ifdef __x86_64__ +#include +#else +#error Please provide platform specific code for the I/O +#endif // ifdef __x86_64__ diff --git a/dev/zka/KernelKit/PCI/IO.hxx b/dev/zka/KernelKit/PCI/IO.hxx deleted file mode 100644 index 58287a9b..00000000 --- a/dev/zka/KernelKit/PCI/IO.hxx +++ /dev/null @@ -1,59 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include -#include -#include -#include - -namespace Kernel -{ - template - class IOArray final - { - public: - IOArray() = delete; - - IOArray(nullPtr) = delete; - - explicit IOArray(Array& ports) - : fPorts(ports) - { - } - ~IOArray() - { - } - - IOArray& operator=(const IOArray&) = default; - - IOArray(const IOArray&) = default; - - operator bool() - { - return !fPorts.Empty(); - } - - public: - template - T In(SizeT index); - - template - void Out(SizeT index, T value); - - private: - Array fPorts; - }; - - using IOArray16 = IOArray<16>; -} // namespace Kernel - -#ifdef __x86_64__ -#include -#else -#error Please provide platform specific code for the I/O -#endif // ifdef __x86_64__ diff --git a/dev/zka/KernelKit/PCI/Iterator.h b/dev/zka/KernelKit/PCI/Iterator.h new file mode 100644 index 00000000..232a4671 --- /dev/null +++ b/dev/zka/KernelKit/PCI/Iterator.h @@ -0,0 +1,43 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#ifndef __PCI_ITERATOR_HPP__ +#define __PCI_ITERATOR_HPP__ + +#include +#include +#include +#include +#include + +#define ZKA_BUS_COUNT (256) +#define ZKA_DEVICE_COUNT (33) +#define ZKA_FUNCTION_COUNT (8) + +namespace Kernel::PCI +{ + class Iterator final + { + public: + Iterator() = delete; + + public: + explicit Iterator(const Types::PciDeviceKind& deviceType); + + Iterator& operator=(const Iterator&) = default; + Iterator(const Iterator&) = default; + + ~Iterator(); + + public: + Ref operator[](const Size& sz); + + private: + Array fDevices; + }; +} // namespace Kernel::PCI + +#endif // __PCI_ITERATOR_HPP__ diff --git a/dev/zka/KernelKit/PCI/Iterator.hxx b/dev/zka/KernelKit/PCI/Iterator.hxx deleted file mode 100644 index a065f9de..00000000 --- a/dev/zka/KernelKit/PCI/Iterator.hxx +++ /dev/null @@ -1,43 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#ifndef __PCI_ITERATOR_HPP__ -#define __PCI_ITERATOR_HPP__ - -#include -#include -#include -#include -#include - -#define ZKA_BUS_COUNT (256) -#define ZKA_DEVICE_COUNT (33) -#define ZKA_FUNCTION_COUNT (8) - -namespace Kernel::PCI -{ - class Iterator final - { - public: - Iterator() = delete; - - public: - explicit Iterator(const Types::PciDeviceKind& deviceType); - - Iterator& operator=(const Iterator&) = default; - Iterator(const Iterator&) = default; - - ~Iterator(); - - public: - Ref operator[](const Size& sz); - - private: - Array fDevices; - }; -} // namespace Kernel::PCI - -#endif // __PCI_ITERATOR_HPP__ diff --git a/dev/zka/KernelKit/PCI/PCI.h b/dev/zka/KernelKit/PCI/PCI.h new file mode 100644 index 00000000..b445074f --- /dev/null +++ b/dev/zka/KernelKit/PCI/PCI.h @@ -0,0 +1,59 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include + +#define cPCIConfigAddressPort (0xCF8) +#define cPCIConfigDataPort (0xCFC) + +#define cPCIDeviceCount (32) +#define cPCIFuncCount (8) +#define cPCIBusCount (255) + +namespace Kernel::PCI +{ + // model + struct DeviceHeader + { + UInt16 VendorId; + UInt16 DeviceId; + UInt8 Command; + UInt8 Status; + UInt8 RevisionId; + UInt8 ProgIf; + UInt8 SubClass; + UInt8 Class; + UInt8 CacheLineSz; + UInt8 LatencyTimer; + UInt8 HeaderType; + UInt8 Bist; + UInt8 Bus; + UInt8 Device; + UInt8 Function; + }; + + namespace Detail + { + class BAR + { + public: + UIntPtr BAR; + SizeT Size; + }; + } // namespace Detail + + class BAR + { + public: + Detail::BAR BAR1; + Detail::BAR BAR2; + Detail::BAR BAR3; + Detail::BAR BAR4; + Detail::BAR BAR5; + }; +} // namespace Kernel::PCI diff --git a/dev/zka/KernelKit/PCI/PCI.hxx b/dev/zka/KernelKit/PCI/PCI.hxx deleted file mode 100644 index 2433be43..00000000 --- a/dev/zka/KernelKit/PCI/PCI.hxx +++ /dev/null @@ -1,59 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include - -#define cPCIConfigAddressPort (0xCF8) -#define cPCIConfigDataPort (0xCFC) - -#define cPCIDeviceCount (32) -#define cPCIFuncCount (8) -#define cPCIBusCount (255) - -namespace Kernel::PCI -{ - // model - struct DeviceHeader - { - UInt16 VendorId; - UInt16 DeviceId; - UInt8 Command; - UInt8 Status; - UInt8 RevisionId; - UInt8 ProgIf; - UInt8 SubClass; - UInt8 Class; - UInt8 CacheLineSz; - UInt8 LatencyTimer; - UInt8 HeaderType; - UInt8 Bist; - UInt8 Bus; - UInt8 Device; - UInt8 Function; - }; - - namespace Detail - { - class BAR - { - public: - UIntPtr BAR; - SizeT Size; - }; - } // namespace Detail - - class BAR - { - public: - Detail::BAR BAR1; - Detail::BAR BAR2; - Detail::BAR BAR3; - Detail::BAR BAR4; - Detail::BAR BAR5; - }; -} // namespace Kernel::PCI diff --git a/dev/zka/KernelKit/PE.h b/dev/zka/KernelKit/PE.h new file mode 100644 index 00000000..b077f251 --- /dev/null +++ b/dev/zka/KernelKit/PE.h @@ -0,0 +1,143 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: PE.h + Purpose: Portable Executable for Kernel. + + Revision History: + + 30/01/24: Added file (amlel) + +------------------------------------------- */ + +#ifndef __KERNELKIT_INC_PE_HXX__ +#define __KERNELKIT_INC_PE_HXX__ + +#include + +#define kPeSignature 0x00004550 + +#define kPeDLLBase 0x4000000 +#define kPeEXEBase 0x1000000 + +#define kPeMagic32 0x010b +#define kPeMagic64 0x020b + +#define kPeMachineAMD64 0x8664 +#define kPeMachineARM64 0xaa64 + +typedef struct LDR_EXEC_HEADER final +{ + Kernel::UInt32 mSignature; + Kernel::UInt16 mMachine; + Kernel::UInt16 mNumberOfSections; + Kernel::UInt32 mTimeDateStamp; + Kernel::UInt32 mPointerToSymbolTable; + Kernel::UInt32 mNumberOfSymbols; + Kernel::UInt16 mSizeOfOptionalHeader; + Kernel::UInt16 mCharacteristics; +} LDR_EXEC_HEADER, *LDR_EXEC_HEADER_PTR; + +typedef struct LDR_OPTIONAL_HEADER final +{ + Kernel::UInt16 mMagic; // 0x010b - PE32, 0x020b - PE32+ (64 bit) + Kernel::UInt8 mMajorLinkerVersion; + Kernel::UInt8 mMinorLinkerVersion; + Kernel::UInt32 mSizeOfCode; + Kernel::UInt32 mSizeOfInitializedData; + Kernel::UInt32 mSizeOfUninitializedData; + Kernel::UInt32 mAddressOfEntryPoint; + Kernel::UInt32 mBaseOfCode; + Kernel::UInt32 mBaseOfData; + Kernel::UInt32 mImageBase; + Kernel::UInt32 mSectionAlignment; + Kernel::UInt32 mFileAlignment; + Kernel::UInt16 mMajorOperatingSystemVersion; + Kernel::UInt16 mMinorOperatingSystemVersion; + Kernel::UInt16 mMajorImageVersion; + Kernel::UInt16 mMinorImageVersion; + Kernel::UInt16 mMajorSubsystemVersion; + Kernel::UInt16 mMinorSubsystemVersion; + Kernel::UInt32 mWin32VersionValue; + Kernel::UInt32 mSizeOfImage; + Kernel::UInt32 mSizeOfHeaders; + Kernel::UInt32 mCheckSum; + Kernel::UInt16 mSubsystem; + Kernel::UInt16 mDllCharacteristics; + Kernel::UInt32 mSizeOfStackReserve; + Kernel::UInt32 mSizeOfStackCommit; + Kernel::UInt32 mSizeOfHeapReserve; + Kernel::UInt32 mSizeOfHeapCommit; + Kernel::UInt32 mLoaderFlags; + Kernel::UInt32 mNumberOfRvaAndSizes; +} LDR_OPTIONAL_HEADER, *LDR_OPTIONAL_HEADER_PTR; + +typedef struct LDR_SECTION_HEADER final +{ + Kernel::Char mName[8]; + Kernel::UInt32 mVirtualSize; + Kernel::UInt32 mVirtualAddress; + Kernel::UInt32 mSizeOfRawData; + Kernel::UInt32 mPointerToRawData; + Kernel::UInt32 mPointerToRelocations; + Kernel::UInt32 mPointerToLineNumbers; + Kernel::UInt16 mNumberOfRelocations; + Kernel::UInt16 mNumberOfLinenumbers; + Kernel::UInt32 mCharacteristics; +} LDR_SECTION_HEADER, *LDR_SECTION_HEADER_PTR; + +enum kExecDataDirParams +{ + kExecExport, + kExecImport, + kExecInvalid, + kExecCount, +}; + +typedef struct LDR_EXPORT_DIRECTORY +{ + Kernel::UInt32 mCharacteristics; + Kernel::UInt32 mTimeDateStamp; + Kernel::UInt16 mMajorVersion; + Kernel::UInt16 mMinorVersion; + Kernel::UInt32 mName; + Kernel::UInt32 mBase; + Kernel::UInt32 mNumberOfFunctions; + Kernel::UInt32 mNumberOfNames; + Kernel::UInt32 mAddressOfFunctions; // export table rva + Kernel::UInt32 mAddressOfNames; + Kernel::UInt32 mAddressOfNameOrdinal; // ordinal table rva +} LDR_EXPORT_DIRECTORY, *LDR_EXPORT_DIRECTORY_PTR; + +typedef struct LDR_IMPORT_DIRECTORY +{ + union { + Kernel::UInt32 mCharacteristics; + Kernel::UInt32 mOriginalFirstThunk; + }; + Kernel::UInt32 mTimeDateStamp; + Kernel::UInt32 mForwarderChain; + Kernel::UInt32 mNameRva; + Kernel::UInt32 mThunkTableRva; +} LDR_IMPORT_DIRECTORY, *LDR_IMPORT_DIRECTORY_PTR; + +typedef struct LDR_DATA_DIRECTORY +{ + Kernel::UInt32 VirtualAddress; + Kernel::UInt32 Size; +} LDR_DATA_DIRECTORY, *LDR_DATA_DIRECTORY_PTR; + +typedef struct LDR_IMAGE_HEADER +{ + LDR_EXEC_HEADER mHeader; + LDR_OPTIONAL_HEADER mOptHdr; +} LDR_IMAGE_HEADER, *LDR_IMAGE_HEADER_PTR; + +enum +{ + eUserSection = 0x00000020, + cPEResourceId = 0xFFaadd00, +}; + +#endif /* ifndef __KERNELKIT_INC_PE_HXX__ */ diff --git a/dev/zka/KernelKit/PE.hxx b/dev/zka/KernelKit/PE.hxx deleted file mode 100644 index 54b1b12e..00000000 --- a/dev/zka/KernelKit/PE.hxx +++ /dev/null @@ -1,143 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: PE.hxx - Purpose: Portable Executable for Kernel. - - Revision History: - - 30/01/24: Added file (amlel) - -------------------------------------------- */ - -#ifndef __KERNELKIT_INC_PE_HXX__ -#define __KERNELKIT_INC_PE_HXX__ - -#include - -#define kPeSignature 0x00004550 - -#define kPeDLLBase 0x4000000 -#define kPeEXEBase 0x1000000 - -#define kPeMagic32 0x010b -#define kPeMagic64 0x020b - -#define kPeMachineAMD64 0x8664 -#define kPeMachineARM64 0xaa64 - -typedef struct LDR_EXEC_HEADER final -{ - Kernel::UInt32 mSignature; - Kernel::UInt16 mMachine; - Kernel::UInt16 mNumberOfSections; - Kernel::UInt32 mTimeDateStamp; - Kernel::UInt32 mPointerToSymbolTable; - Kernel::UInt32 mNumberOfSymbols; - Kernel::UInt16 mSizeOfOptionalHeader; - Kernel::UInt16 mCharacteristics; -} LDR_EXEC_HEADER, *LDR_EXEC_HEADER_PTR; - -typedef struct LDR_OPTIONAL_HEADER final -{ - Kernel::UInt16 mMagic; // 0x010b - PE32, 0x020b - PE32+ (64 bit) - Kernel::UInt8 mMajorLinkerVersion; - Kernel::UInt8 mMinorLinkerVersion; - Kernel::UInt32 mSizeOfCode; - Kernel::UInt32 mSizeOfInitializedData; - Kernel::UInt32 mSizeOfUninitializedData; - Kernel::UInt32 mAddressOfEntryPoint; - Kernel::UInt32 mBaseOfCode; - Kernel::UInt32 mBaseOfData; - Kernel::UInt32 mImageBase; - Kernel::UInt32 mSectionAlignment; - Kernel::UInt32 mFileAlignment; - Kernel::UInt16 mMajorOperatingSystemVersion; - Kernel::UInt16 mMinorOperatingSystemVersion; - Kernel::UInt16 mMajorImageVersion; - Kernel::UInt16 mMinorImageVersion; - Kernel::UInt16 mMajorSubsystemVersion; - Kernel::UInt16 mMinorSubsystemVersion; - Kernel::UInt32 mWin32VersionValue; - Kernel::UInt32 mSizeOfImage; - Kernel::UInt32 mSizeOfHeaders; - Kernel::UInt32 mCheckSum; - Kernel::UInt16 mSubsystem; - Kernel::UInt16 mDllCharacteristics; - Kernel::UInt32 mSizeOfStackReserve; - Kernel::UInt32 mSizeOfStackCommit; - Kernel::UInt32 mSizeOfHeapReserve; - Kernel::UInt32 mSizeOfHeapCommit; - Kernel::UInt32 mLoaderFlags; - Kernel::UInt32 mNumberOfRvaAndSizes; -} LDR_OPTIONAL_HEADER, *LDR_OPTIONAL_HEADER_PTR; - -typedef struct LDR_SECTION_HEADER final -{ - Kernel::Char mName[8]; - Kernel::UInt32 mVirtualSize; - Kernel::UInt32 mVirtualAddress; - Kernel::UInt32 mSizeOfRawData; - Kernel::UInt32 mPointerToRawData; - Kernel::UInt32 mPointerToRelocations; - Kernel::UInt32 mPointerToLineNumbers; - Kernel::UInt16 mNumberOfRelocations; - Kernel::UInt16 mNumberOfLinenumbers; - Kernel::UInt32 mCharacteristics; -} LDR_SECTION_HEADER, *LDR_SECTION_HEADER_PTR; - -enum kExecDataDirParams -{ - kExecExport, - kExecImport, - kExecInvalid, - kExecCount, -}; - -typedef struct LDR_EXPORT_DIRECTORY -{ - Kernel::UInt32 mCharacteristics; - Kernel::UInt32 mTimeDateStamp; - Kernel::UInt16 mMajorVersion; - Kernel::UInt16 mMinorVersion; - Kernel::UInt32 mName; - Kernel::UInt32 mBase; - Kernel::UInt32 mNumberOfFunctions; - Kernel::UInt32 mNumberOfNames; - Kernel::UInt32 mAddressOfFunctions; // export table rva - Kernel::UInt32 mAddressOfNames; - Kernel::UInt32 mAddressOfNameOrdinal; // ordinal table rva -} LDR_EXPORT_DIRECTORY, *LDR_EXPORT_DIRECTORY_PTR; - -typedef struct LDR_IMPORT_DIRECTORY -{ - union { - Kernel::UInt32 mCharacteristics; - Kernel::UInt32 mOriginalFirstThunk; - }; - Kernel::UInt32 mTimeDateStamp; - Kernel::UInt32 mForwarderChain; - Kernel::UInt32 mNameRva; - Kernel::UInt32 mThunkTableRva; -} LDR_IMPORT_DIRECTORY, *LDR_IMPORT_DIRECTORY_PTR; - -typedef struct LDR_DATA_DIRECTORY -{ - Kernel::UInt32 VirtualAddress; - Kernel::UInt32 Size; -} LDR_DATA_DIRECTORY, *LDR_DATA_DIRECTORY_PTR; - -typedef struct LDR_IMAGE_HEADER -{ - LDR_EXEC_HEADER mHeader; - LDR_OPTIONAL_HEADER mOptHdr; -} LDR_IMAGE_HEADER, *LDR_IMAGE_HEADER_PTR; - -enum -{ - eUserSection = 0x00000020, - cPEResourceId = 0xFFaadd00, -}; - -#endif /* ifndef __KERNELKIT_INC_PE_HXX__ */ diff --git a/dev/zka/KernelKit/PECodeMgr.h b/dev/zka/KernelKit/PECodeMgr.h new file mode 100644 index 00000000..14dd8c1a --- /dev/null +++ b/dev/zka/KernelKit/PECodeMgr.h @@ -0,0 +1,24 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: PECodeMgr.h + Purpose: PE32+ Code Mgr and DLL mgr. + + Revision History: + + 12/02/24: Added file (amlel) + +------------------------------------------- */ + +#pragma once + +//////////////////////////////////////////////////// + +// LAST REV: Mon Feb 12 13:52:01 CET 2024 + +//////////////////////////////////////////////////// + +#include +#include +#include diff --git a/dev/zka/KernelKit/PECodeMgr.hxx b/dev/zka/KernelKit/PECodeMgr.hxx deleted file mode 100644 index 39551909..00000000 --- a/dev/zka/KernelKit/PECodeMgr.hxx +++ /dev/null @@ -1,24 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: PECodeMgr.hxx - Purpose: PE32+ Code Mgr and DLL mgr. - - Revision History: - - 12/02/24: Added file (amlel) - -------------------------------------------- */ - -#pragma once - -//////////////////////////////////////////////////// - -// LAST REV: Mon Feb 12 13:52:01 CET 2024 - -//////////////////////////////////////////////////// - -#include -#include -#include diff --git a/dev/zka/KernelKit/PEF.h b/dev/zka/KernelKit/PEF.h new file mode 100644 index 00000000..15f9c5e2 --- /dev/null +++ b/dev/zka/KernelKit/PEF.h @@ -0,0 +1,116 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: PEF.h + Purpose: Preferred Executable Format for Kernel. + + Revision History: + + ?/?/23: Added file (amlel) + +------------------------------------------- */ + +#ifndef __KERNELKIT_INC_PEF_HXX__ +#define __KERNELKIT_INC_PEF_HXX__ + +#include +#include +#include + +#define kPefMagic "Joy!" +#define kPefMagicFat "yoJ!" + +#define kPefMagicLen 5 + +#define kPefVersion 3 +#define kPefNameLen 255 + +namespace Kernel +{ + enum + { + kPefArchIntel86S, + kPefArchAMD64, + kPefArchRISCV, + kPefArch64x0, /* 64x0. ISA */ + kPefArch32x0, /* 32x0. ISA */ + kPefArchPowerPC, + kPefArchARM64, + kPefArchCount = (kPefArchPowerPC - kPefArchIntel86S) + 1, + kPefArchInvalid = 0xFF, + }; + + enum + { + kPefSubArchAMD, + kPefSubArchIntel, + kPefSubArchGeneric, + kPefSubArchIBM, + }; + + enum + { + kPefKindExec = 1, /* .exe */ + kPefKindSharedObject = 2, /* .lib */ + kPefKindObject = 4, /* .obj */ + kPefKindDebug = 5, /* .dbg */ + kPefKindDriver = 6, + kPefKindCount, + }; + + typedef struct PEFContainer final + { + Char Magic[kPefMagicLen]; + UInt32 Linker; + UInt32 Version; + UInt32 Kind; + UInt32 Abi; + UInt32 Cpu; + UInt32 SubCpu; /* Cpu specific information */ + UIntPtr Start; + SizeT HdrSz; /* Size of header */ + SizeT Count; /* container header count */ + } PACKED PEFContainer; + + /* First PEFCommandHeader starts after PEFContainer */ + + typedef struct PEFCommandHeader final + { + Char Name[kPefNameLen]; /* container name */ + UInt32 Cpu; /* container cpu */ + UInt32 SubCpu; /* container sub-cpu */ + UInt32 Flags; /* container flags */ + UInt16 Kind; /* container kind */ + UIntPtr Offset; /* content offset */ + SizeT Size; /* content Size */ + } PACKED PEFCommandHeader; + + enum + { + kPefCode = 0xC, + kPefData = 0xD, + kPefZero = 0xE, + kPefLinkerID = 0x1, + }; +} // namespace Kernel + +/* not mandatory, only for non fork based filesystems */ +#define kPefExt ".exe" +#define kPefDylibExt ".dll" +#define kPefLibExt ".lib" +#define kPefObjectExt ".obj" +#define kPefDebugExt ".dbg" +#define kPefDriverExt ".sys" + +// Kernel System Binary Interface. +#define kPefAbi (0x5046) + +#define kPefBaseOrigin (0x40000000) + +#define kPefStart "__ImageStart" + +#define kPefForkKind kPefMagic +#define kPefForkKindFAT kPefMagicFat + +#endif /* ifndef __KERNELKIT_INC_PEF_HXX__ */ diff --git a/dev/zka/KernelKit/PEF.hxx b/dev/zka/KernelKit/PEF.hxx deleted file mode 100644 index 98287693..00000000 --- a/dev/zka/KernelKit/PEF.hxx +++ /dev/null @@ -1,116 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: PEF.hxx - Purpose: Preferred Executable Format for Kernel. - - Revision History: - - ?/?/23: Added file (amlel) - -------------------------------------------- */ - -#ifndef __KERNELKIT_INC_PEF_HXX__ -#define __KERNELKIT_INC_PEF_HXX__ - -#include -#include -#include - -#define kPefMagic "Joy!" -#define kPefMagicFat "yoJ!" - -#define kPefMagicLen 5 - -#define kPefVersion 3 -#define kPefNameLen 255 - -namespace Kernel -{ - enum - { - kPefArchIntel86S, - kPefArchAMD64, - kPefArchRISCV, - kPefArch64x0, /* 64x0. ISA */ - kPefArch32x0, /* 32x0. ISA */ - kPefArchPowerPC, - kPefArchARM64, - kPefArchCount = (kPefArchPowerPC - kPefArchIntel86S) + 1, - kPefArchInvalid = 0xFF, - }; - - enum - { - kPefSubArchAMD, - kPefSubArchIntel, - kPefSubArchGeneric, - kPefSubArchIBM, - }; - - enum - { - kPefKindExec = 1, /* .exe */ - kPefKindSharedObject = 2, /* .lib */ - kPefKindObject = 4, /* .obj */ - kPefKindDebug = 5, /* .dbg */ - kPefKindDriver = 6, - kPefKindCount, - }; - - typedef struct PEFContainer final - { - Char Magic[kPefMagicLen]; - UInt32 Linker; - UInt32 Version; - UInt32 Kind; - UInt32 Abi; - UInt32 Cpu; - UInt32 SubCpu; /* Cpu specific information */ - UIntPtr Start; - SizeT HdrSz; /* Size of header */ - SizeT Count; /* container header count */ - } PACKED PEFContainer; - - /* First PEFCommandHeader starts after PEFContainer */ - - typedef struct PEFCommandHeader final - { - Char Name[kPefNameLen]; /* container name */ - UInt32 Cpu; /* container cpu */ - UInt32 SubCpu; /* container sub-cpu */ - UInt32 Flags; /* container flags */ - UInt16 Kind; /* container kind */ - UIntPtr Offset; /* content offset */ - SizeT Size; /* content Size */ - } PACKED PEFCommandHeader; - - enum - { - kPefCode = 0xC, - kPefData = 0xD, - kPefZero = 0xE, - kPefLinkerID = 0x1, - }; -} // namespace Kernel - -/* not mandatory, only for non fork based filesystems */ -#define kPefExt ".exe" -#define kPefDylibExt ".dll" -#define kPefLibExt ".lib" -#define kPefObjectExt ".obj" -#define kPefDebugExt ".dbg" -#define kPefDriverExt ".sys" - -// Kernel System Binary Interface. -#define kPefAbi (0x5046) - -#define kPefBaseOrigin (0x40000000) - -#define kPefStart "__ImageStart" - -#define kPefForkKind kPefMagic -#define kPefForkKindFAT kPefMagicFat - -#endif /* ifndef __KERNELKIT_INC_PEF_HXX__ */ diff --git a/dev/zka/KernelKit/PEFCodeMgr.h b/dev/zka/KernelKit/PEFCodeMgr.h new file mode 100644 index 00000000..8ad9eb01 --- /dev/null +++ b/dev/zka/KernelKit/PEFCodeMgr.h @@ -0,0 +1,67 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#ifndef _INC_CODE_MANAGER_PEF_HXX_ +#define _INC_CODE_MANAGER_PEF_HXX_ + +#include +#include +#include +#include + +#define kPefApplicationMime "application/vnd-zka-executable" + +namespace Kernel +{ + /// + /// \name PEFLoader + /// \brief PEF loader class. + /// + class PEFLoader : public LoaderInterface + { + private: + explicit PEFLoader() = delete; + + public: + explicit PEFLoader(const VoidPtr blob); + explicit PEFLoader(const Char* path); + ~PEFLoader() override; + + public: + ZKA_COPY_DEFAULT(PEFLoader); + + public: + const Char* Path() override; + const Char* AsString() override; + const Char* MIME() override; + + public: + ErrorOr FindStart() override; + VoidPtr FindSymbol(const Char* name, Int32 kind) override; + + public: + bool IsLoaded() noexcept; + + private: +#ifdef __FSKIT_USE_NEFS__ + OwnPtr> fFile; +#else + OwnPtr> fFile; +#endif // __FSKIT_USE_NEFS__ + + Ref fPath; + VoidPtr fCachedBlob; + bool fFatBinary; + bool fBad; + }; + + namespace Utils + { + bool execute_from_image(PEFLoader& exec, const Int32& procKind) noexcept; + } // namespace Utils +} // namespace Kernel + +#endif // ifndef _INC_CODE_MANAGER_PEF_HXX_ diff --git a/dev/zka/KernelKit/PEFCodeMgr.hxx b/dev/zka/KernelKit/PEFCodeMgr.hxx deleted file mode 100644 index 006fa6e6..00000000 --- a/dev/zka/KernelKit/PEFCodeMgr.hxx +++ /dev/null @@ -1,67 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#ifndef _INC_CODE_MANAGER_PEF_HXX_ -#define _INC_CODE_MANAGER_PEF_HXX_ - -#include -#include -#include -#include - -#define kPefApplicationMime "application/vnd-zka-executable" - -namespace Kernel -{ - /// - /// \name PEFLoader - /// \brief PEF loader class. - /// - class PEFLoader : public LoaderInterface - { - private: - explicit PEFLoader() = delete; - - public: - explicit PEFLoader(const VoidPtr blob); - explicit PEFLoader(const Char* path); - ~PEFLoader() override; - - public: - ZKA_COPY_DEFAULT(PEFLoader); - - public: - const Char* Path() override; - const Char* AsString() override; - const Char* MIME() override; - - public: - ErrorOr FindStart() override; - VoidPtr FindSymbol(const Char* name, Int32 kind) override; - - public: - bool IsLoaded() noexcept; - - private: -#ifdef __FSKIT_USE_NEFS__ - OwnPtr> fFile; -#else - OwnPtr> fFile; -#endif // __FSKIT_USE_NEFS__ - - Ref fPath; - VoidPtr fCachedBlob; - bool fFatBinary; - bool fBad; - }; - - namespace Utils - { - bool execute_from_image(PEFLoader& exec, const Int32& procKind) noexcept; - } // namespace Utils -} // namespace Kernel - -#endif // ifndef _INC_CODE_MANAGER_PEF_HXX_ diff --git a/dev/zka/KernelKit/Semaphore.h b/dev/zka/KernelKit/Semaphore.h new file mode 100644 index 00000000..0ce17e9d --- /dev/null +++ b/dev/zka/KernelKit/Semaphore.h @@ -0,0 +1,43 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include +#include +#include + +namespace Kernel +{ + class UserProcess; + + typedef UserProcess* UserProcessPtr; + + /// @brief Access control class, which locks a task until one is done. + class Semaphore final + { + public: + explicit Semaphore() = default; + ~Semaphore() = default; + + public: + bool IsLocked() const; + bool Unlock() noexcept; + + public: + void WaitForProcess() noexcept; + + public: + bool Lock(UserProcess* process); + bool LockOrWait(UserProcess* process, TimerInterface* timer); + + public: + ZKA_COPY_DEFAULT(Semaphore); + + private: + UserProcessPtr fLockingProcess{nullptr}; + }; +} // namespace Kernel diff --git a/dev/zka/KernelKit/Semaphore.hxx b/dev/zka/KernelKit/Semaphore.hxx deleted file mode 100644 index 535a683b..00000000 --- a/dev/zka/KernelKit/Semaphore.hxx +++ /dev/null @@ -1,43 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include -#include -#include - -namespace Kernel -{ - class UserProcess; - - typedef UserProcess* UserProcessPtr; - - /// @brief Access control class, which locks a task until one is done. - class Semaphore final - { - public: - explicit Semaphore() = default; - ~Semaphore() = default; - - public: - bool IsLocked() const; - bool Unlock() noexcept; - - public: - void WaitForProcess() noexcept; - - public: - bool Lock(UserProcess* process); - bool LockOrWait(UserProcess* process, TimerInterface* timer); - - public: - ZKA_COPY_DEFAULT(Semaphore); - - private: - UserProcessPtr fLockingProcess{nullptr}; - }; -} // namespace Kernel diff --git a/dev/zka/KernelKit/ThreadLocalStorage.h b/dev/zka/KernelKit/ThreadLocalStorage.h new file mode 100644 index 00000000..1e6df991 --- /dev/null +++ b/dev/zka/KernelKit/ThreadLocalStorage.h @@ -0,0 +1,54 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#ifndef _KERNELKIT_TLS_HPP +#define _KERNELKIT_TLS_HPP + +#include + +///! @brief Thread Local Storage for minoskrnl. + +#define kCookieMag0 'Z' +#define kCookieMag1 'K' +#define kCookieMag2 'A' + +#define kTLSCookieLen (3U) + +struct THREAD_INFORMATION_BLOCK; + +/// @brief Thread Information Block. +/// Located in GS on AMD64, other architectures have their own stuff. (64x0, 32x0, ARM64) +struct PACKED THREAD_INFORMATION_BLOCK final +{ + Kernel::Char f_Cookie[kTLSCookieLen]{0}; //! Thread magic number. + Kernel::VoidPtr f_ThreadRecord{nullptr}; //! Thread information record. +}; + +///! @brief Cookie Sanity check. +Kernel::Boolean tls_check_tib(THREAD_INFORMATION_BLOCK* the_tib); + +///! @brief new ptr syscall. +template +T* tls_new_ptr(void) noexcept; + +///! @brief delete ptr syscall. +template +Kernel::Boolean tls_delete_ptr(T* ptr) noexcept; + +template +T* tls_new_class(Args&&... args); + +/// @brief TLS install TIB and PIB. (syscall) +EXTERN_C Kernel::Void rt_install_tib(THREAD_INFORMATION_BLOCK* TIB, THREAD_INFORMATION_BLOCK* PIB); + +/// @brief TLS check (syscall) +EXTERN_C Kernel::Bool tls_check_syscall_impl(Kernel::VoidPtr TIB) noexcept; + +#include + +// last rev 7/7/24 + +#endif /* ifndef _KERNELKIT_TLS_HPP */ diff --git a/dev/zka/KernelKit/ThreadLocalStorage.hxx b/dev/zka/KernelKit/ThreadLocalStorage.hxx deleted file mode 100644 index 55bb2b5f..00000000 --- a/dev/zka/KernelKit/ThreadLocalStorage.hxx +++ /dev/null @@ -1,54 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#ifndef _KERNELKIT_TLS_HPP -#define _KERNELKIT_TLS_HPP - -#include - -///! @brief Thread Local Storage for minoskrnl. - -#define kCookieMag0 'Z' -#define kCookieMag1 'K' -#define kCookieMag2 'A' - -#define kTLSCookieLen (3U) - -struct THREAD_INFORMATION_BLOCK; - -/// @brief Thread Information Block. -/// Located in GS on AMD64, other architectures have their own stuff. (64x0, 32x0, ARM64) -struct PACKED THREAD_INFORMATION_BLOCK final -{ - Kernel::Char f_Cookie[kTLSCookieLen]{0}; //! Thread magic number. - Kernel::VoidPtr f_ThreadRecord{nullptr}; //! Thread information record. -}; - -///! @brief Cookie Sanity check. -Kernel::Boolean tls_check_tib(THREAD_INFORMATION_BLOCK* the_tib); - -///! @brief new ptr syscall. -template -T* tls_new_ptr(void) noexcept; - -///! @brief delete ptr syscall. -template -Kernel::Boolean tls_delete_ptr(T* ptr) noexcept; - -template -T* tls_new_class(Args&&... args); - -/// @brief TLS install TIB and PIB. (syscall) -EXTERN_C Kernel::Void rt_install_tib(THREAD_INFORMATION_BLOCK* TIB, THREAD_INFORMATION_BLOCK* PIB); - -/// @brief TLS check (syscall) -EXTERN_C Kernel::Bool tls_check_syscall_impl(Kernel::VoidPtr TIB) noexcept; - -#include - -// last rev 7/7/24 - -#endif /* ifndef _KERNELKIT_TLS_HPP */ diff --git a/dev/zka/KernelKit/ThreadLocalStorage.inl b/dev/zka/KernelKit/ThreadLocalStorage.inl index 2f0fe553..305d3966 100644 --- a/dev/zka/KernelKit/ThreadLocalStorage.inl +++ b/dev/zka/KernelKit/ThreadLocalStorage.inl @@ -8,7 +8,7 @@ //! @brief Allocate resources from the process's heap storage. #ifndef INC_PROCESS_SCHEDULER_HXX -#include +#include #endif template diff --git a/dev/zka/KernelKit/Timer.h b/dev/zka/KernelKit/Timer.h new file mode 100644 index 00000000..cd0b6c4c --- /dev/null +++ b/dev/zka/KernelKit/Timer.h @@ -0,0 +1,82 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include +#include +#include + +namespace Kernel +{ + class SoftwareTimer; + class TimerInterface; + + class TimerInterface + { + public: + /// @brief Default constructor + explicit TimerInterface() = default; + virtual ~TimerInterface() = default; + + public: + ZKA_COPY_DEFAULT(TimerInterface); + + public: + virtual Int32 Wait() noexcept; + }; + + class SoftwareTimer final : public TimerInterface + { + public: + explicit SoftwareTimer(Int64 seconds); + ~SoftwareTimer() override; + + public: + ZKA_COPY_DEFAULT(SoftwareTimer); + + public: + Int32 Wait() noexcept override; + + private: + IntPtr* fDigitalTimer{nullptr}; + Int64 fWaitFor{0}; + }; + + class HardwareTimer final : public TimerInterface + { + public: + explicit HardwareTimer(Int64 seconds); + ~HardwareTimer() override; + + public: + ZKA_COPY_DEFAULT(HardwareTimer); + + public: + Int32 Wait() noexcept override; + + private: + IntPtr* fDigitalTimer{nullptr}; + Int64 fWaitFor{0}; + }; + + inline Int64 Milliseconds(Int64 time) + { + if (time < 0) + return 0; + + // TODO: nanoseconds maybe? + return 1000 * 1000 * time; + } + + inline Int64 Seconds(Int64 time) + { + if (time < 0) + return 0; + + return 1000 * Milliseconds(time); + } +} // namespace Kernel diff --git a/dev/zka/KernelKit/Timer.hxx b/dev/zka/KernelKit/Timer.hxx deleted file mode 100644 index 345f59ca..00000000 --- a/dev/zka/KernelKit/Timer.hxx +++ /dev/null @@ -1,82 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include -#include -#include - -namespace Kernel -{ - class SoftwareTimer; - class TimerInterface; - - class TimerInterface - { - public: - /// @brief Default constructor - explicit TimerInterface() = default; - virtual ~TimerInterface() = default; - - public: - ZKA_COPY_DEFAULT(TimerInterface); - - public: - virtual Int32 Wait() noexcept; - }; - - class SoftwareTimer final : public TimerInterface - { - public: - explicit SoftwareTimer(Int64 seconds); - ~SoftwareTimer() override; - - public: - ZKA_COPY_DEFAULT(SoftwareTimer); - - public: - Int32 Wait() noexcept override; - - private: - IntPtr* fDigitalTimer{nullptr}; - Int64 fWaitFor{0}; - }; - - class HardwareTimer final : public TimerInterface - { - public: - explicit HardwareTimer(Int64 seconds); - ~HardwareTimer() override; - - public: - ZKA_COPY_DEFAULT(HardwareTimer); - - public: - Int32 Wait() noexcept override; - - private: - IntPtr* fDigitalTimer{nullptr}; - Int64 fWaitFor{0}; - }; - - inline Int64 Milliseconds(Int64 time) - { - if (time < 0) - return 0; - - // TODO: nanoseconds maybe? - return 1000 * 1000 * time; - } - - inline Int64 Seconds(Int64 time) - { - if (time < 0) - return 0; - - return 1000 * Milliseconds(time); - } -} // namespace Kernel diff --git a/dev/zka/KernelKit/User.h b/dev/zka/KernelKit/User.h new file mode 100644 index 00000000..10697f28 --- /dev/null +++ b/dev/zka/KernelKit/User.h @@ -0,0 +1,75 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#ifndef INC_USER_HXX +#define INC_USER_HXX + +#include +#include +#include +#include + +// user mode users. +#define kSuperUser "ZKA AUTHORITY\\SUPER" +#define kGuestUser "ZKA AUTHORITY\\GUEST" + +#define kUsersDir "\\Users\\" + +#define kMaxUserNameLen (255) +#define kMaxUserTokenLen (255) + +namespace Kernel +{ + class User; + + enum class RingKind + { + kRingStdUser = 1, + kRingSuperUser = 2, + kRingGuestUser = 5, + kRingCount = 3, + }; + + class User final + { + public: + explicit User() = delete; + + User(const Int32& sel, const Char* userName); + User(const RingKind& kind, const Char* userName); + + ~User(); + + public: + ZKA_COPY_DEFAULT(User) + + public: + bool operator==(const User& lhs); + bool operator!=(const User& lhs); + + public: + /// @brief Get software ring + const RingKind& Ring() noexcept; + + /// @brief Get user name + Char* Name() noexcept; + + /// @brief Is he a standard user? + Bool IsStdUser() noexcept; + + /// @brief Is she a super user? + Bool IsSuperUser() noexcept; + + Bool TrySave(const Char* password) noexcept; + + private: + RingKind fRing{RingKind::kRingStdUser}; + Char fUserName[kMaxUserNameLen] = {0}; + Char fUserToken[kMaxUserTokenLen] = {0}; + }; +} // namespace Kernel + +#endif /* ifndef INC_USER_HXX */ diff --git a/dev/zka/KernelKit/User.hxx b/dev/zka/KernelKit/User.hxx deleted file mode 100644 index d082bf93..00000000 --- a/dev/zka/KernelKit/User.hxx +++ /dev/null @@ -1,75 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#ifndef INC_USER_HXX -#define INC_USER_HXX - -#include -#include -#include -#include - -// user mode users. -#define kSuperUser "ZKA AUTHORITY\\SUPER" -#define kGuestUser "ZKA AUTHORITY\\GUEST" - -#define kUsersDir "\\Users\\" - -#define kMaxUserNameLen (255) -#define kMaxUserTokenLen (255) - -namespace Kernel -{ - class User; - - enum class RingKind - { - kRingStdUser = 1, - kRingSuperUser = 2, - kRingGuestUser = 5, - kRingCount = 3, - }; - - class User final - { - public: - explicit User() = delete; - - User(const Int32& sel, const Char* userName); - User(const RingKind& kind, const Char* userName); - - ~User(); - - public: - ZKA_COPY_DEFAULT(User) - - public: - bool operator==(const User& lhs); - bool operator!=(const User& lhs); - - public: - /// @brief Get software ring - const RingKind& Ring() noexcept; - - /// @brief Get user name - Char* Name() noexcept; - - /// @brief Is he a standard user? - Bool IsStdUser() noexcept; - - /// @brief Is she a super user? - Bool IsSuperUser() noexcept; - - Bool TrySave(const Char* password) noexcept; - - private: - RingKind fRing{RingKind::kRingStdUser}; - Char fUserName[kMaxUserNameLen] = {0}; - Char fUserToken[kMaxUserTokenLen] = {0}; - }; -} // namespace Kernel - -#endif /* ifndef INC_USER_HXX */ diff --git a/dev/zka/KernelKit/UserProcessScheduler.h b/dev/zka/KernelKit/UserProcessScheduler.h new file mode 100644 index 00000000..637f41b4 --- /dev/null +++ b/dev/zka/KernelKit/UserProcessScheduler.h @@ -0,0 +1,322 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#ifndef INC_PROCESS_SCHEDULER_HXX +#define INC_PROCESS_SCHEDULER_HXX + +#include +#include +#include +#include + +#define kSchedMinMicroTime (AffinityKind::kStandard) +#define kSchedInvalidPID (-1) +#define kSchedProcessLimitPerTeam (16U) + +#define kSchedMaxMemoryLimit gib_cast(128) +#define kSchedMaxStackSz mib_cast(8) + +//////////////////////////////////////////////////// + +// LAST REV: Mon Feb 12 13:52:01 CET 2024 + +//////////////////////////////////////////////////// + +namespace Kernel +{ + //! @note Forward declarations. + + class UserProcess; + class IPEFDLLObject; + class UserProcessTeam; + class UserProcessScheduler; + class UserProcessHelper; + + //! @brief UserProcess identifier. + typedef Int64 ProcessID; + + //! @brief UserProcess name length. + inline constexpr SizeT kProcessLen = 256U; + + //! @brief UserProcess status enum. + enum class ProcessStatusKind : Int32 + { + kStarting, + kRunning, + kKilled, + kFrozen, + kDead + }; + + //! @brief Affinity is the amount of nano-seconds this process is going + //! to run. + enum class AffinityKind : Int32 + { + kRealTime = 500, + kVeryHigh = 250, + kHigh = 200, + kStandard = 1000, + kLowUsage = 1500, + kVeryLowUsage = 2000, + }; + + // 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 : Int32 + { + kProcessSubsystemSecurity = 100, + kProcessSubsystemApplication, + kProcessSubsystemService, + kProcessSubsystemDriver, + kProcessSubsystemInvalid = 255, + kProcessSubsystemCount = 4, + }; + + using ProcessSubsystem = ProcessSubsystemEnum; + using ProcessTime = UInt64; + using PID = Int64; + + // for permission manager, tells where we run the code. + enum class ProcessLevelRing : Int32 + { + kRingStdUser = 1, + kRingSuperUser = 2, + kRingGuestUser = 5, + kRingCount = 5, + }; + + // Helper types. + using ImagePtr = VoidPtr; + using HeapPtrKind = VoidPtr; + + /// @name UserProcess + /// @brief User process block. + /// Holds information about the running process/thread. + struct UserProcess final + { + public: + explicit UserProcess(VoidPtr startImage = nullptr) + : Image(startImage) + { + } + + ~UserProcess() = default; + + ZKA_COPY_DEFAULT(UserProcess) + + public: + Void SetImageStart(VoidPtr imageStart) noexcept; + const UInt32& GetExitCode() noexcept; + + public: + Char Name[kProcessLen] = {"ZKA Process"}; + ProcessSubsystem SubSystem{ProcessSubsystem::kProcessSubsystemInvalid}; + User* Owner{nullptr}; + HAL::StackFramePtr StackFrame{nullptr}; + AffinityKind Affinity{AffinityKind::kStandard}; + ProcessStatusKind Status{ProcessStatusKind::kDead}; + UInt8* StackReserve{nullptr}; + + //! @brief Code Image. + ImagePtr Image{nullptr}; + + SizeT StackSize{kSchedMaxStackSz}; + + //! @brief Shared library handle, reserved for kExectuableDLLKind types of executables only. + IPEFDLLObject* PefDLLDelegate{nullptr}; + + // Memory usage. + SizeT MemoryCursor{0}; + SizeT MemoryLimit{kSchedMaxMemoryLimit}; + + struct PROCESS_MEMORY_ENTRY final + { + VoidPtr MemoryEntry; + + struct PROCESS_MEMORY_ENTRY* MemoryPrev; + struct PROCESS_MEMORY_ENTRY* MemoryNext; + }; + + PROCESS_MEMORY_ENTRY* MemoryEntryList{nullptr}; + + UIntPtr MemoryPD{0UL}; + + enum + { + kExectuableKind, + kExectuableDLLKind, + kExectuableKindCount, + }; + + ProcessTime PTime{0}; //! @brief Process allocated tine. + + PID ProcessId{kSchedInvalidPID}; + Int32 Kind{kExectuableKind}; + + public: + //! @brief boolean operator, check status. + operator bool(); + + ///! @brief Crashes the app, exits with code ~0. + Void Crash(); + + ///! @brief Exits the app. + Void Exit(const Int32& exit_code = 0); + + ///! @brief TLS allocate. + ///! @param sz size of new ptr. + VoidPtr New(const SizeT& sz); + + ///! @brief TLS free. + ///! @param ptr the pointer to free. + ///! @param sz the size of it. + Boolean Delete(VoidPtr ptr, const SizeT& sz); + + ///! @brief Wakes up threads. + Void Wake(const bool wakeup = false); + + // UserProcess getters. + public: + ///! @brief Get the process's name + ///! @example 'C Runtime Library' + const Char* GetProcessName() noexcept; + + //! @brief return local error code of process. + //! @return Int32 local error code. + Int32& GetLocalCode() noexcept; + + const User* GetOwner() noexcept; + const ProcessStatusKind& GetStatus() noexcept; + const AffinityKind& GetAffinity() noexcept; + + private: + UInt32 fLastExitCode{0}; + Int32 fLocalCode{0}; + + friend UserProcessScheduler; + friend UserProcessHelper; + }; + + /// \brief Processs Team (contains multiple processes inside it.) + /// Equivalent to a process batch + class UserProcessTeam final + { + public: + explicit UserProcessTeam() = default; + ~UserProcessTeam() = default; + + ZKA_COPY_DEFAULT(UserProcessTeam); + + Array& AsArray(); + Ref& AsRef(); + ProcessID& Id() noexcept; + + public: + Array mProcessList; + Ref mCurrentProcess; + SizeT mProcessAmount{0}; + ProcessID mTeamId{0}; + }; + + using UserProcessPtr = UserProcess*; + + /// @brief UserProcess scheduler class. + /// The main class which you call to schedule processes. + class UserProcessScheduler final : public ISchedulerObject + { + friend class UserProcessHelper; + + public: + explicit UserProcessScheduler() = default; + ~UserProcessScheduler() override = default; + + ZKA_COPY_DEFAULT(UserProcessScheduler) + + operator bool(); + bool operator!(); + + public: + UserProcessTeam& CurrentTeam(); + + public: + SizeT Add(UserProcess process); + Bool Remove(ProcessID process_id); + + const Bool IsUser() override; + const Bool IsKernel() override; + const Bool HasMP() override; + + public: + Ref& CurrentProcess(); + SizeT Run() noexcept; + + public: + STATIC UserProcessScheduler& The(); + + private: + UserProcessTeam mTeam; + }; + + /* + * \brief UserProcess helper class, which contains needed utilities for the scheduler. + */ + + class UserProcessHelper final + { + public: + STATIC bool Switch(VoidPtr image_ptr, UInt8* stack_ptr, HAL::StackFramePtr frame_ptr, const PID& new_pid); + STATIC bool CanBeScheduled(const UserProcess& process); + STATIC PID& TheCurrentPID(); + STATIC SizeT StartScheduling(); + STATIC Bool InitializeScheduler(); + }; + + const UInt32& sched_get_exit_code(void) noexcept; +} // namespace Kernel + +#include + +//////////////////////////////////////////////////// + +// END + +//////////////////////////////////////////////////// + +#endif /* ifndef INC_PROCESS_SCHEDULER_HXX */ diff --git a/dev/zka/KernelKit/UserProcessScheduler.hxx b/dev/zka/KernelKit/UserProcessScheduler.hxx deleted file mode 100644 index bc246683..00000000 --- a/dev/zka/KernelKit/UserProcessScheduler.hxx +++ /dev/null @@ -1,322 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#ifndef INC_PROCESS_SCHEDULER_HXX -#define INC_PROCESS_SCHEDULER_HXX - -#include -#include -#include -#include - -#define kSchedMinMicroTime (AffinityKind::kStandard) -#define kSchedInvalidPID (-1) -#define kSchedProcessLimitPerTeam (16U) - -#define kSchedMaxMemoryLimit gib_cast(128) -#define kSchedMaxStackSz mib_cast(8) - -//////////////////////////////////////////////////// - -// LAST REV: Mon Feb 12 13:52:01 CET 2024 - -//////////////////////////////////////////////////// - -namespace Kernel -{ - //! @note Forward declarations. - - class UserProcess; - class IPEFDLLObject; - class UserProcessTeam; - class UserProcessScheduler; - class UserProcessHelper; - - //! @brief UserProcess identifier. - typedef Int64 ProcessID; - - //! @brief UserProcess name length. - inline constexpr SizeT kProcessLen = 256U; - - //! @brief UserProcess status enum. - enum class ProcessStatusKind : Int32 - { - kStarting, - kRunning, - kKilled, - kFrozen, - kDead - }; - - //! @brief Affinity is the amount of nano-seconds this process is going - //! to run. - enum class AffinityKind : Int32 - { - kRealTime = 500, - kVeryHigh = 250, - kHigh = 200, - kStandard = 1000, - kLowUsage = 1500, - kVeryLowUsage = 2000, - }; - - // 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 : Int32 - { - kProcessSubsystemSecurity = 100, - kProcessSubsystemApplication, - kProcessSubsystemService, - kProcessSubsystemDriver, - kProcessSubsystemInvalid = 255, - kProcessSubsystemCount = 4, - }; - - using ProcessSubsystem = ProcessSubsystemEnum; - using ProcessTime = UInt64; - using PID = Int64; - - // for permission manager, tells where we run the code. - enum class ProcessLevelRing : Int32 - { - kRingStdUser = 1, - kRingSuperUser = 2, - kRingGuestUser = 5, - kRingCount = 5, - }; - - // Helper types. - using ImagePtr = VoidPtr; - using HeapPtrKind = VoidPtr; - - /// @name UserProcess - /// @brief User process block. - /// Holds information about the running process/thread. - struct UserProcess final - { - public: - explicit UserProcess(VoidPtr startImage = nullptr) - : Image(startImage) - { - } - - ~UserProcess() = default; - - ZKA_COPY_DEFAULT(UserProcess) - - public: - Void SetImageStart(VoidPtr imageStart) noexcept; - const UInt32& GetExitCode() noexcept; - - public: - Char Name[kProcessLen] = {"ZKA Process"}; - ProcessSubsystem SubSystem{ProcessSubsystem::kProcessSubsystemInvalid}; - User* Owner{nullptr}; - HAL::StackFramePtr StackFrame{nullptr}; - AffinityKind Affinity{AffinityKind::kStandard}; - ProcessStatusKind Status{ProcessStatusKind::kDead}; - UInt8* StackReserve{nullptr}; - - //! @brief Code Image. - ImagePtr Image{nullptr}; - - SizeT StackSize{kSchedMaxStackSz}; - - //! @brief Shared library handle, reserved for kExectuableDLLKind types of executables only. - IPEFDLLObject* PefDLLDelegate{nullptr}; - - // Memory usage. - SizeT MemoryCursor{0}; - SizeT MemoryLimit{kSchedMaxMemoryLimit}; - - struct PROCESS_MEMORY_ENTRY final - { - VoidPtr MemoryEntry; - - struct PROCESS_MEMORY_ENTRY* MemoryPrev; - struct PROCESS_MEMORY_ENTRY* MemoryNext; - }; - - PROCESS_MEMORY_ENTRY* MemoryEntryList{nullptr}; - - UIntPtr MemoryPD{0UL}; - - enum - { - kExectuableKind, - kExectuableDLLKind, - kExectuableKindCount, - }; - - ProcessTime PTime{0}; //! @brief Process allocated tine. - - PID ProcessId{kSchedInvalidPID}; - Int32 Kind{kExectuableKind}; - - public: - //! @brief boolean operator, check status. - operator bool(); - - ///! @brief Crashes the app, exits with code ~0. - Void Crash(); - - ///! @brief Exits the app. - Void Exit(const Int32& exit_code = 0); - - ///! @brief TLS allocate. - ///! @param sz size of new ptr. - VoidPtr New(const SizeT& sz); - - ///! @brief TLS free. - ///! @param ptr the pointer to free. - ///! @param sz the size of it. - Boolean Delete(VoidPtr ptr, const SizeT& sz); - - ///! @brief Wakes up threads. - Void Wake(const bool wakeup = false); - - // UserProcess getters. - public: - ///! @brief Get the process's name - ///! @example 'C Runtime Library' - const Char* GetProcessName() noexcept; - - //! @brief return local error code of process. - //! @return Int32 local error code. - Int32& GetLocalCode() noexcept; - - const User* GetOwner() noexcept; - const ProcessStatusKind& GetStatus() noexcept; - const AffinityKind& GetAffinity() noexcept; - - private: - UInt32 fLastExitCode{0}; - Int32 fLocalCode{0}; - - friend UserProcessScheduler; - friend UserProcessHelper; - }; - - /// \brief Processs Team (contains multiple processes inside it.) - /// Equivalent to a process batch - class UserProcessTeam final - { - public: - explicit UserProcessTeam() = default; - ~UserProcessTeam() = default; - - ZKA_COPY_DEFAULT(UserProcessTeam); - - Array& AsArray(); - Ref& AsRef(); - ProcessID& Id() noexcept; - - public: - Array mProcessList; - Ref mCurrentProcess; - SizeT mProcessAmount{0}; - ProcessID mTeamId{0}; - }; - - using UserProcessPtr = UserProcess*; - - /// @brief UserProcess scheduler class. - /// The main class which you call to schedule processes. - class UserProcessScheduler final : public ISchedulerObject - { - friend class UserProcessHelper; - - public: - explicit UserProcessScheduler() = default; - ~UserProcessScheduler() override = default; - - ZKA_COPY_DEFAULT(UserProcessScheduler) - - operator bool(); - bool operator!(); - - public: - UserProcessTeam& CurrentTeam(); - - public: - SizeT Add(UserProcess process); - Bool Remove(ProcessID process_id); - - const Bool IsUser() override; - const Bool IsKernel() override; - const Bool HasMP() override; - - public: - Ref& CurrentProcess(); - SizeT Run() noexcept; - - public: - STATIC UserProcessScheduler& The(); - - private: - UserProcessTeam mTeam; - }; - - /* - * \brief UserProcess helper class, which contains needed utilities for the scheduler. - */ - - class UserProcessHelper final - { - public: - STATIC bool Switch(VoidPtr image_ptr, UInt8* stack_ptr, HAL::StackFramePtr frame_ptr, const PID& new_pid); - STATIC bool CanBeScheduled(const UserProcess& process); - STATIC PID& TheCurrentPID(); - STATIC SizeT StartScheduling(); - STATIC Bool InitializeScheduler(); - }; - - const UInt32& sched_get_exit_code(void) noexcept; -} // namespace Kernel - -#include - -//////////////////////////////////////////////////// - -// END - -//////////////////////////////////////////////////// - -#endif /* ifndef INC_PROCESS_SCHEDULER_HXX */ diff --git a/dev/zka/KernelKit/XCOFF.h b/dev/zka/KernelKit/XCOFF.h new file mode 100644 index 00000000..a7cf3aff --- /dev/null +++ b/dev/zka/KernelKit/XCOFF.h @@ -0,0 +1,51 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: XCOFF.hpp + Purpose: XCOFF for Kernel. + + Revision History: + + 04/07/24: Added file (amlel) + +------------------------------------------- */ + +#ifndef __INC_XCOFF_HXX__ +#define __INC_XCOFF_HXX__ + +#include + +#define cXCOFF64Magic (0x01F7) + +#define cXCOFFRelFlg (0x0001) +#define cXCOFFExecutable (0x0002) +#define cXCOFFLnno (0x0004) +#define cXCOFFLSyms (0x0008) + +struct XCoffFileHeader; +struct XCoffForkHeader; + +/// @brief XCoff file header, meant for POWER apps. +typedef struct XCoffFileHeader +{ + Kernel::UInt16 fMagic; + Kernel::UInt16 fTarget; + Kernel::UInt16 fNumSecs; + Kernel::UInt32 fTimeDat; + Kernel::UIntPtr fSymPtr; + Kernel::UInt32 fNumSyms; + Kernel::UInt16 fOptHdr; // ?: Number of bytes in optional header +} XCoffFileHeader64; + +#define cForkNameLen (255) + +/// @brief This the executable manifest fork. +typedef struct XCoffForkHeader +{ + Kernel::Char fPropertiesXMLFork[cForkNameLen]; + Kernel::Char fDynamicLoaderFork[cForkNameLen]; + Kernel::Char fCodeSignFork[cForkNameLen]; +} XCoffForkHeader; + +#endif // ifndef __INC_XCOFF_HXX__ diff --git a/dev/zka/KernelKit/XCOFF.hxx b/dev/zka/KernelKit/XCOFF.hxx deleted file mode 100644 index 9a09a617..00000000 --- a/dev/zka/KernelKit/XCOFF.hxx +++ /dev/null @@ -1,51 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: XCOFF.hpp - Purpose: XCOFF for Kernel. - - Revision History: - - 04/07/24: Added file (amlel) - -------------------------------------------- */ - -#ifndef __INC_XCOFF_HXX__ -#define __INC_XCOFF_HXX__ - -#include - -#define cXCOFF64Magic (0x01F7) - -#define cXCOFFRelFlg (0x0001) -#define cXCOFFExecutable (0x0002) -#define cXCOFFLnno (0x0004) -#define cXCOFFLSyms (0x0008) - -struct XCoffFileHeader; -struct XCoffForkHeader; - -/// @brief XCoff file header, meant for POWER apps. -typedef struct XCoffFileHeader -{ - Kernel::UInt16 fMagic; - Kernel::UInt16 fTarget; - Kernel::UInt16 fNumSecs; - Kernel::UInt32 fTimeDat; - Kernel::UIntPtr fSymPtr; - Kernel::UInt32 fNumSyms; - Kernel::UInt16 fOptHdr; // ?: Number of bytes in optional header -} XCoffFileHeader64; - -#define cForkNameLen (255) - -/// @brief This the executable manifest fork. -typedef struct XCoffForkHeader -{ - Kernel::Char fPropertiesXMLFork[cForkNameLen]; - Kernel::Char fDynamicLoaderFork[cForkNameLen]; - Kernel::Char fCodeSignFork[cForkNameLen]; -} XCoffForkHeader; - -#endif // ifndef __INC_XCOFF_HXX__ -- cgit v1.2.3