From 8b1114ea205ef6560fb7bf5713577d1ab7deb5be Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 4 Sep 2024 17:29:07 +0200 Subject: [ MR ] BUMP commit. Signed-off-by: Amlal El Mahrouss --- dev/ZKA/Sources/CodeManager.cxx | 32 ---- dev/ZKA/Sources/CodeMgr.cxx | 32 ++++ dev/ZKA/Sources/DLLMain.cxx | 22 +-- dev/ZKA/Sources/DeviceManager.cxx | 7 - dev/ZKA/Sources/DeviceMgr.cxx | 7 + dev/ZKA/Sources/DriveManager.cxx | 146 -------------- dev/ZKA/Sources/DriveMgr.cxx | 146 ++++++++++++++ dev/ZKA/Sources/FileManager.cxx | 190 ------------------- dev/ZKA/Sources/FileMgr.cxx | 190 +++++++++++++++++++ dev/ZKA/Sources/Heap.cxx | 73 ++++--- dev/ZKA/Sources/MP.cxx | 30 +-- dev/ZKA/Sources/NewFS+FileManager.cxx | 109 ----------- dev/ZKA/Sources/NewFS+FileMgr.cxx | 109 +++++++++++ dev/ZKA/Sources/NewFS+IO.cxx | 4 +- dev/ZKA/Sources/NewFS+Journal.cxx | 2 +- dev/ZKA/Sources/PEFCodeManager.cxx | 252 ------------------------- dev/ZKA/Sources/PEFCodeMgr.cxx | 252 +++++++++++++++++++++++++ dev/ZKA/Sources/PageManager.cxx | 111 ----------- dev/ZKA/Sources/PageMgr.cxx | 111 +++++++++++ dev/ZKA/Sources/Pmm.cxx | 14 +- dev/ZKA/Sources/Storage/ATADeviceInterface.cxx | 4 +- dev/ZKA/Sources/User.cxx | 2 +- dev/ZKA/Sources/UserProcessScheduler.cxx | 2 +- 23 files changed, 915 insertions(+), 932 deletions(-) delete mode 100644 dev/ZKA/Sources/CodeManager.cxx create mode 100644 dev/ZKA/Sources/CodeMgr.cxx delete mode 100644 dev/ZKA/Sources/DeviceManager.cxx create mode 100644 dev/ZKA/Sources/DeviceMgr.cxx delete mode 100644 dev/ZKA/Sources/DriveManager.cxx create mode 100644 dev/ZKA/Sources/DriveMgr.cxx delete mode 100644 dev/ZKA/Sources/FileManager.cxx create mode 100644 dev/ZKA/Sources/FileMgr.cxx delete mode 100644 dev/ZKA/Sources/NewFS+FileManager.cxx create mode 100644 dev/ZKA/Sources/NewFS+FileMgr.cxx delete mode 100644 dev/ZKA/Sources/PEFCodeManager.cxx create mode 100644 dev/ZKA/Sources/PEFCodeMgr.cxx delete mode 100644 dev/ZKA/Sources/PageManager.cxx create mode 100644 dev/ZKA/Sources/PageMgr.cxx (limited to 'dev/ZKA/Sources') diff --git a/dev/ZKA/Sources/CodeManager.cxx b/dev/ZKA/Sources/CodeManager.cxx deleted file mode 100644 index 7e180ac0..00000000 --- a/dev/ZKA/Sources/CodeManager.cxx +++ /dev/null @@ -1,32 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include -#include -#include - -namespace Kernel -{ - /// @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* processName) noexcept - { - if (!main) - return false; - - UserProcess proc; - proc.SetImageStart(reinterpret_cast(main)); - - proc.Kind = UserProcess::kExeKind; - proc.StackSize = kib_cast(8); - - rt_copy_memory((VoidPtr)processName, proc.Name, rt_string_len(processName)); - - return UserProcessScheduler::The().Add(proc) == kErrorSuccess; - } -} // namespace Kernel diff --git a/dev/ZKA/Sources/CodeMgr.cxx b/dev/ZKA/Sources/CodeMgr.cxx new file mode 100644 index 00000000..df5b0917 --- /dev/null +++ b/dev/ZKA/Sources/CodeMgr.cxx @@ -0,0 +1,32 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include +#include +#include + +namespace Kernel +{ + /// @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* processName) noexcept + { + if (!main) + return false; + + UserProcess proc; + proc.SetImageStart(reinterpret_cast(main)); + + proc.Kind = UserProcess::kExeKind; + proc.StackSize = kib_cast(8); + + rt_copy_memory((VoidPtr)processName, proc.Name, rt_string_len(processName)); + + return UserProcessScheduler::The().Add(proc) == kErrorSuccess; + } +} // namespace Kernel diff --git a/dev/ZKA/Sources/DLLMain.cxx b/dev/ZKA/Sources/DLLMain.cxx index 84c58b3a..88887ec3 100644 --- a/dev/ZKA/Sources/DLLMain.cxx +++ b/dev/ZKA/Sources/DLLMain.cxx @@ -11,18 +11,18 @@ #include #include #include -#include +#include #include #include #include -#include +#include #include #include #include #include #include -#include -#include +#include +#include #include #include #include @@ -44,23 +44,23 @@ namespace Kernel::Detail /// @brief Filesystem auto formatter, additional checks are also done by the class. class FilesystemInstaller final { - Kernel::NewFilesystemManager* fNewFS{nullptr}; + Kernel::NewFilesystemMgr* fNewFS{nullptr}; public: /// @brief wizard constructor. explicit FilesystemInstaller() { - if (Kernel::FilesystemManagerInterface::GetMounted()) + if (Kernel::FilesystemMgrInterface::GetMounted()) { CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.exe: NewFS IFS already mounted by HAL (A:)", 10, 10, RGB(0, 0, 0)); - fNewFS = reinterpret_cast(Kernel::FilesystemManagerInterface::GetMounted()); + fNewFS = reinterpret_cast(Kernel::FilesystemMgrInterface::GetMounted()); } else { // Mounts a NewFS from main drive. - fNewFS = new Kernel::NewFilesystemManager(); + fNewFS = new Kernel::NewFilesystemMgr(); - Kernel::FilesystemManagerInterface::Mount(fNewFS); + Kernel::FilesystemMgrInterface::Mount(fNewFS); CG::CGDrawStringToWnd(cKernelWnd, "newoskrnl.exe: Mounted NewFS IFS (A:)", 10, 10, RGB(0, 0, 0)); } @@ -142,8 +142,8 @@ namespace Kernel::Detail ZKA_COPY_DEFAULT(FilesystemInstaller); /// @brief Grab the disk's NewFS reference. - /// @return NewFilesystemManager the filesystem interface - Kernel::NewFilesystemManager* Leak() + /// @return NewFilesystemMgr the filesystem interface + Kernel::NewFilesystemMgr* Leak() { return fNewFS; } diff --git a/dev/ZKA/Sources/DeviceManager.cxx b/dev/ZKA/Sources/DeviceManager.cxx deleted file mode 100644 index 229e0fb7..00000000 --- a/dev/ZKA/Sources/DeviceManager.cxx +++ /dev/null @@ -1,7 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include diff --git a/dev/ZKA/Sources/DeviceMgr.cxx b/dev/ZKA/Sources/DeviceMgr.cxx new file mode 100644 index 00000000..70d01984 --- /dev/null +++ b/dev/ZKA/Sources/DeviceMgr.cxx @@ -0,0 +1,7 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include diff --git a/dev/ZKA/Sources/DriveManager.cxx b/dev/ZKA/Sources/DriveManager.cxx deleted file mode 100644 index 84c9870f..00000000 --- a/dev/ZKA/Sources/DriveManager.cxx +++ /dev/null @@ -1,146 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include -#include -#include -#include -#include - -/// @file DriveManager.cxx -/// @brief Kernel drive manager. - -namespace Kernel -{ - static UInt16 kATAIO = 0U; - static UInt8 kATAMaster = 0U; - - /// @brief reads from an ATA drive. - /// @param pckt - /// @return - Void ke_drv_input(DriveTrait::DrivePacket* pckt) - { - if (!pckt) - { - return; - } - -#ifdef __AHCI__ - drv_std_read(pckt->fLba, (Char*)pckt->fPacketContent, kAHCISectorSize, pckt->fPacketSize); -#elif defined(__ATA_PIO__) || defined(__ATA_DMA__) - drv_std_read(pckt->fLba, kATAIO, kATAMaster, (Char*)pckt->fPacketContent, kATASectorSize, pckt->fPacketSize); -#endif - } - - /// @brief Writes to an ATA drive. - /// @param pckt - /// @return - Void ke_drv_output(DriveTrait::DrivePacket* pckt) - { - if (!pckt) - { - return; - } - -#ifdef __AHCI__ - drv_std_write(pckt->fLba, (Char*)pckt->fPacketContent, kAHCISectorSize, pckt->fPacketSize); -#elif defined(__ATA_PIO__) || defined(__ATA_DMA__) - drv_std_write(pckt->fLba, kATAIO, kATAMaster, (Char*)pckt->fPacketContent, kATASectorSize, pckt->fPacketSize); -#endif - } - - /// @brief Executes a disk check on the ATA drive. - /// @param pckt - /// @return - Void ke_drv_check_disk(DriveTrait::DrivePacket* pckt) - { - if (!pckt) - { - return; - } - - pckt->fPacketGood = false; - -#if defined(__ATA_PIO__) || defined(__ATA_DMA__) - kATAMaster = true; - kATAIO = ATA_PRIMARY_IO; - - MUST_PASS(drv_std_init(kATAIO, kATAMaster, kATAIO, kATAMaster)); -#elif defined(__AHCI__) - UInt16 pi = 0; - MUST_PASS(drv_std_init(pi)); -#endif // if defined(__ATA_PIO__) || defined (__ATA_DMA__) - - pckt->fPacketGood = true; - } - -/// @brief Gets the drive kind (ATA, SCSI, AHCI...) -/// @param -/// @return -#ifdef __ATA_PIO__ - const Char* io_drive_kind(Void) - { - return "ATA-PIO"; - } -#endif - -#ifdef __ATA_DMA__ - const Char* io_drive_kind(Void) - { - return "ATA-DMA"; - } -#endif - -#ifdef __AHCI__ - const Char* io_drive_kind(Void) - { - return "AHCI"; - } -#endif - - /// @brief Unimplemented drive. - /// @param pckt - /// @return - Void io_drv_unimplemented(DriveTrait::DrivePacket* pckt) - { - } - - /// @brief Makes a new drive. - /// @return the new drive. - DriveTrait io_construct_drive() noexcept - { - DriveTrait trait; - - rt_copy_memory((VoidPtr) "\\Mount\\NUL:", trait.fName, rt_string_len("\\Mount\\NUL:")); - trait.fKind = kInvalidDrive; - - trait.fInput = io_drv_unimplemented; - trait.fOutput = io_drv_unimplemented; - trait.fVerify = io_drv_unimplemented; - trait.fDriveKind = io_drive_kind; - - return trait; - } - - /// @brief Fetches the main drive. - /// @return the new drive. - DriveTrait io_construct_main_drive() noexcept - { - DriveTrait trait; - - rt_copy_memory((VoidPtr) "\\Mount\\MainDisk:", trait.fName, rt_string_len("\\Mount\\MainDisk:")); - trait.fKind = kMassStorage; - - trait.fInput = ke_drv_input; - trait.fOutput = ke_drv_output; - trait.fVerify = ke_drv_check_disk; - trait.fDriveKind = io_drive_kind; - - kcout << "newoskrnl.exe: Construct drive with success.\r"; - - return trait; - } -} // namespace Kernel diff --git a/dev/ZKA/Sources/DriveMgr.cxx b/dev/ZKA/Sources/DriveMgr.cxx new file mode 100644 index 00000000..376eb158 --- /dev/null +++ b/dev/ZKA/Sources/DriveMgr.cxx @@ -0,0 +1,146 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include +#include +#include +#include +#include + +/// @file DriveMgr.cxx +/// @brief Kernel drive manager. + +namespace Kernel +{ + static UInt16 kATAIO = 0U; + static UInt8 kATAMaster = 0U; + + /// @brief reads from an ATA drive. + /// @param pckt + /// @return + Void ke_drv_input(DriveTrait::DrivePacket* pckt) + { + if (!pckt) + { + return; + } + +#ifdef __AHCI__ + drv_std_read(pckt->fLba, (Char*)pckt->fPacketContent, kAHCISectorSize, pckt->fPacketSize); +#elif defined(__ATA_PIO__) || defined(__ATA_DMA__) + drv_std_read(pckt->fLba, kATAIO, kATAMaster, (Char*)pckt->fPacketContent, kATASectorSize, pckt->fPacketSize); +#endif + } + + /// @brief Writes to an ATA drive. + /// @param pckt + /// @return + Void ke_drv_output(DriveTrait::DrivePacket* pckt) + { + if (!pckt) + { + return; + } + +#ifdef __AHCI__ + drv_std_write(pckt->fLba, (Char*)pckt->fPacketContent, kAHCISectorSize, pckt->fPacketSize); +#elif defined(__ATA_PIO__) || defined(__ATA_DMA__) + drv_std_write(pckt->fLba, kATAIO, kATAMaster, (Char*)pckt->fPacketContent, kATASectorSize, pckt->fPacketSize); +#endif + } + + /// @brief Executes a disk check on the ATA drive. + /// @param pckt + /// @return + Void ke_drv_check_disk(DriveTrait::DrivePacket* pckt) + { + if (!pckt) + { + return; + } + + pckt->fPacketGood = false; + +#if defined(__ATA_PIO__) || defined(__ATA_DMA__) + kATAMaster = true; + kATAIO = ATA_PRIMARY_IO; + + MUST_PASS(drv_std_init(kATAIO, kATAMaster, kATAIO, kATAMaster)); +#elif defined(__AHCI__) + UInt16 pi = 0; + MUST_PASS(drv_std_init(pi)); +#endif // if defined(__ATA_PIO__) || defined (__ATA_DMA__) + + pckt->fPacketGood = true; + } + +/// @brief Gets the drive kind (ATA, SCSI, AHCI...) +/// @param +/// @return +#ifdef __ATA_PIO__ + const Char* io_drive_kind(Void) + { + return "ATA-PIO"; + } +#endif + +#ifdef __ATA_DMA__ + const Char* io_drive_kind(Void) + { + return "ATA-DMA"; + } +#endif + +#ifdef __AHCI__ + const Char* io_drive_kind(Void) + { + return "AHCI"; + } +#endif + + /// @brief Unimplemented drive. + /// @param pckt + /// @return + Void io_drv_unimplemented(DriveTrait::DrivePacket* pckt) + { + } + + /// @brief Makes a new drive. + /// @return the new drive. + DriveTrait io_construct_drive() noexcept + { + DriveTrait trait; + + rt_copy_memory((VoidPtr) "\\Mount\\NUL:", trait.fName, rt_string_len("\\Mount\\NUL:")); + trait.fKind = kInvalidDrive; + + trait.fInput = io_drv_unimplemented; + trait.fOutput = io_drv_unimplemented; + trait.fVerify = io_drv_unimplemented; + trait.fDriveKind = io_drive_kind; + + return trait; + } + + /// @brief Fetches the main drive. + /// @return the new drive. + DriveTrait io_construct_main_drive() noexcept + { + DriveTrait trait; + + rt_copy_memory((VoidPtr) "\\Mount\\MainDisk:", trait.fName, rt_string_len("\\Mount\\MainDisk:")); + trait.fKind = kMassStorage; + + trait.fInput = ke_drv_input; + trait.fOutput = ke_drv_output; + trait.fVerify = ke_drv_check_disk; + trait.fDriveKind = io_drive_kind; + + kcout << "newoskrnl.exe: Construct drive with success.\r"; + + return trait; + } +} // namespace Kernel diff --git a/dev/ZKA/Sources/FileManager.cxx b/dev/ZKA/Sources/FileManager.cxx deleted file mode 100644 index 8c8fb83a..00000000 --- a/dev/ZKA/Sources/FileManager.cxx +++ /dev/null @@ -1,190 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include -#include - -/// BUGS: 0 -//! @brief File manager for Kernel. - -namespace Kernel -{ - STATIC FilesystemManagerInterface* kMounted = nullptr; - - /// @brief FilesystemManager getter. - /// @return The mounted filesystem. - _Output FilesystemManagerInterface* FilesystemManagerInterface::GetMounted() - { - return kMounted; - } - - /// @brief Unmount filesystem. - /// @return The unmounted filesystem. - _Output FilesystemManagerInterface* FilesystemManagerInterface::Unmount() - { - if (kMounted) - { - auto mount = kMounted; - kMounted = nullptr; - - return mount; - } - - return nullptr; - } - - /// @brief Mount filesystem. - /// @param mount_ptr The filesystem to mount. - /// @return if it succeeded true, otherwise false. - bool FilesystemManagerInterface::Mount(_Input FilesystemManagerInterface* mount_ptr) - { - if (mount_ptr != nullptr) - { - kMounted = mount_ptr; - return true; - } - - return false; - } - -#ifdef __FSKIT_USE_NEWFS__ - /// @brief Opens a new file. - /// @param path - /// @param r - /// @return - _Output NodePtr NewFilesystemManager::Open(_Input const Char* path, _Input const Char* r) - { - if (!path || *path == 0) - return nullptr; - - if (!r || *r == 0) - return nullptr; - - auto catalog = fImpl->GetCatalog(path); - - return node_cast(catalog); - } - - /// @brief Writes to a catalog's fork. - /// @param node the node ptr. - /// @param data the data. - /// @param flags the size. - /// @return - Void NewFilesystemManager::Write(_Input NodePtr node, _Input VoidPtr data, _Input Int32 flags, _Input SizeT size) - { - if (!node) - return; - if (!size) - return; - - constexpr auto cDataForkName = kNewFSDataFork; - this->Write(cDataForkName, node, data, flags, size); - } - - /// @brief Read from filesystem fork. - /// @param node the catalog node. - /// @param flags the flags with it. - /// @param sz the size to read. - /// @return - _Output VoidPtr NewFilesystemManager::Read(_Input NodePtr node, _Input Int32 flags, _Input SizeT size) - { - if (!node) - return nullptr; - if (!size) - return nullptr; - - constexpr auto cDataForkName = kNewFSDataFork; - return this->Read(cDataForkName, node, flags, size); - } - - Void NewFilesystemManager::Write(_Input const Char* name, - _Input NodePtr node, - _Input VoidPtr data, - _Input Int32 flags, - _Input SizeT size) - { - if (!size || - size > kNewFSForkSize) - return; - - if (!data) - return; - - ZKA_UNUSED(flags); - - if ((reinterpret_cast(node))->Kind == kNewFSCatalogKindFile) - fImpl->WriteCatalog(reinterpret_cast(node), (flags & cFileFlagRsrc ? true : false), data, size, - name); - } - - _Output VoidPtr NewFilesystemManager::Read(_Input const Char* name, - _Input NodePtr node, - _Input Int32 flags, - _Input SizeT sz) - { - if (sz > kNewFSForkSize) - return nullptr; - - if (!sz) - return nullptr; - - ZKA_UNUSED(flags); - - if ((reinterpret_cast(node))->Kind == kNewFSCatalogKindFile) - return fImpl->ReadCatalog(reinterpret_cast(node), (flags & cFileFlagRsrc ? true : false), sz, - name); - - return nullptr; - } - - /// @brief Seek from Catalog. - /// @param node - /// @param off - /// @retval true always returns false, this is unimplemented. - /// @retval false always returns this, it is unimplemented. - - _Output Bool NewFilesystemManager::Seek(NodePtr node, SizeT off) - { - if (!node || off == 0) - return false; - - return fImpl->Seek(reinterpret_cast(node), off); - } - - /// @brief Tell where the catalog is. - /// @param node - /// @retval true always returns false, this is unimplemented. - /// @retval false always returns this, it is unimplemented. - - _Output SizeT NewFilesystemManager::Tell(NodePtr node) - { - if (!node) - return kNPos; - - return fImpl->Tell(reinterpret_cast(node)); - } - - /// @brief Rewinds the catalog. - /// @param node - /// @retval true always returns false, this is unimplemented. - /// @retval false always returns this, it is unimplemented. - - _Output Bool NewFilesystemManager::Rewind(NodePtr node) - { - if (!node) - return false; - - return this->Seek(node, 0); - } - - /// @brief Returns the filesystem parser. - /// @return the Filesystem parser class. - _Output NewFSParser* NewFilesystemManager::GetParser() noexcept - { - return fImpl; - } -#endif // __FSKIT_USE_NEWFS__ -} // namespace Kernel diff --git a/dev/ZKA/Sources/FileMgr.cxx b/dev/ZKA/Sources/FileMgr.cxx new file mode 100644 index 00000000..270c13f2 --- /dev/null +++ b/dev/ZKA/Sources/FileMgr.cxx @@ -0,0 +1,190 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include +#include + +/// BUGS: 0 +//! @brief File manager for Kernel. + +namespace Kernel +{ + STATIC FilesystemMgrInterface* kMounted = nullptr; + + /// @brief FilesystemMgr getter. + /// @return The mounted filesystem. + _Output FilesystemMgrInterface* FilesystemMgrInterface::GetMounted() + { + return kMounted; + } + + /// @brief Unmount filesystem. + /// @return The unmounted filesystem. + _Output FilesystemMgrInterface* FilesystemMgrInterface::Unmount() + { + if (kMounted) + { + auto mount = kMounted; + kMounted = nullptr; + + return mount; + } + + return nullptr; + } + + /// @brief Mount filesystem. + /// @param mount_ptr The filesystem to mount. + /// @return if it succeeded true, otherwise false. + bool FilesystemMgrInterface::Mount(_Input FilesystemMgrInterface* mount_ptr) + { + if (mount_ptr != nullptr) + { + kMounted = mount_ptr; + return true; + } + + return false; + } + +#ifdef __FSKIT_USE_NEWFS__ + /// @brief Opens a new file. + /// @param path + /// @param r + /// @return + _Output NodePtr NewFilesystemMgr::Open(_Input const Char* path, _Input const Char* r) + { + if (!path || *path == 0) + return nullptr; + + if (!r || *r == 0) + return nullptr; + + auto catalog = fImpl->GetCatalog(path); + + return node_cast(catalog); + } + + /// @brief Writes to a catalog's fork. + /// @param node the node ptr. + /// @param data the data. + /// @param flags the size. + /// @return + Void NewFilesystemMgr::Write(_Input NodePtr node, _Input VoidPtr data, _Input Int32 flags, _Input SizeT size) + { + if (!node) + return; + if (!size) + return; + + constexpr auto cDataForkName = kNewFSDataFork; + this->Write(cDataForkName, node, data, flags, size); + } + + /// @brief Read from filesystem fork. + /// @param node the catalog node. + /// @param flags the flags with it. + /// @param sz the size to read. + /// @return + _Output VoidPtr NewFilesystemMgr::Read(_Input NodePtr node, _Input Int32 flags, _Input SizeT size) + { + if (!node) + return nullptr; + if (!size) + return nullptr; + + constexpr auto cDataForkName = kNewFSDataFork; + return this->Read(cDataForkName, node, flags, size); + } + + Void NewFilesystemMgr::Write(_Input const Char* name, + _Input NodePtr node, + _Input VoidPtr data, + _Input Int32 flags, + _Input SizeT size) + { + if (!size || + size > kNewFSForkSize) + return; + + if (!data) + return; + + ZKA_UNUSED(flags); + + if ((reinterpret_cast(node))->Kind == kNewFSCatalogKindFile) + fImpl->WriteCatalog(reinterpret_cast(node), (flags & cFileFlagRsrc ? true : false), data, size, + name); + } + + _Output VoidPtr NewFilesystemMgr::Read(_Input const Char* name, + _Input NodePtr node, + _Input Int32 flags, + _Input SizeT sz) + { + if (sz > kNewFSForkSize) + return nullptr; + + if (!sz) + return nullptr; + + ZKA_UNUSED(flags); + + if ((reinterpret_cast(node))->Kind == kNewFSCatalogKindFile) + return fImpl->ReadCatalog(reinterpret_cast(node), (flags & cFileFlagRsrc ? true : false), sz, + name); + + return nullptr; + } + + /// @brief Seek from Catalog. + /// @param node + /// @param off + /// @retval true always returns false, this is unimplemented. + /// @retval false always returns this, it is unimplemented. + + _Output Bool NewFilesystemMgr::Seek(NodePtr node, SizeT off) + { + if (!node || off == 0) + return false; + + return fImpl->Seek(reinterpret_cast(node), off); + } + + /// @brief Tell where the catalog is. + /// @param node + /// @retval true always returns false, this is unimplemented. + /// @retval false always returns this, it is unimplemented. + + _Output SizeT NewFilesystemMgr::Tell(NodePtr node) + { + if (!node) + return kNPos; + + return fImpl->Tell(reinterpret_cast(node)); + } + + /// @brief Rewinds the catalog. + /// @param node + /// @retval true always returns false, this is unimplemented. + /// @retval false always returns this, it is unimplemented. + + _Output Bool NewFilesystemMgr::Rewind(NodePtr node) + { + if (!node) + return false; + + return this->Seek(node, 0); + } + + /// @brief Returns the filesystem parser. + /// @return the Filesystem parser class. + _Output NewFSParser* NewFilesystemMgr::GetParser() noexcept + { + return fImpl; + } +#endif // __FSKIT_USE_NEWFS__ +} // namespace Kernel diff --git a/dev/ZKA/Sources/Heap.cxx b/dev/ZKA/Sources/Heap.cxx index e036badb..615f5664 100644 --- a/dev/ZKA/Sources/Heap.cxx +++ b/dev/ZKA/Sources/Heap.cxx @@ -8,7 +8,7 @@ #include #include #include -#include +#include //! @file KernelHeap.cxx //! @brief Kernel heap allocator. @@ -19,7 +19,7 @@ namespace Kernel { SizeT kHeapCount = 0UL; - PageManager kHeapPageManager; + PageMgr kHeapPageMgr; Bool kOperationInProgress = No; /// @brief Contains data structures and algorithms for the heap. @@ -71,13 +71,13 @@ namespace Kernel if (!allocatedPtr || newSz < 1) return nullptr; - Detail::HEAP_INFORMATION_BLOCK_PTR heapInfoBlk = + Detail::HEAP_INFORMATION_BLOCK_PTR heap_blk = reinterpret_cast( (UIntPtr)allocatedPtr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); - heapInfoBlk->fHeapSize = newSz; + heap_blk->fHeapSize = newSz; - if (heapInfoBlk->fCRC32 > 0) + if (heap_blk->fCRC32 > 0) { MUST_PASS(mm_protect_ke_heap(allocatedPtr)); } @@ -97,9 +97,9 @@ namespace Kernel auto szFix = sz; if (szFix == 0) - ++szFix; + return nullptr; - auto wrapper = kHeapPageManager.Request(rw, user, No, szFix); + auto wrapper = kHeapPageMgr.Request(rw, user, No, szFix); Detail::HEAP_INFORMATION_BLOCK_PTR heap_info_ptr = reinterpret_cast( @@ -107,11 +107,11 @@ namespace Kernel heap_info_ptr->fHeapSize = szFix; heap_info_ptr->fMagic = kKernelHeapMagic; - heap_info_ptr->fCRC32 = 0U; // dont fill it for now. - heap_info_ptr->fHeapPtr = wrapper.VirtualAddress() + sizeof(Detail::HEAP_INFORMATION_BLOCK); - heap_info_ptr->fPage = 0UL; + heap_info_ptr->fCRC32 = No; // dont fill it for now. + heap_info_ptr->fHeapPtr = reinterpret_cast(heap_info_ptr) + sizeof(Detail::HEAP_INFORMATION_BLOCK); + heap_info_ptr->fPage = No; heap_info_ptr->fUser = user; - heap_info_ptr->fPresent = true; + heap_info_ptr->fPresent = No; ++kHeapCount; @@ -138,11 +138,11 @@ namespace Kernel Detail::mm_alloc_init_timeout(); - Detail::HEAP_INFORMATION_BLOCK_PTR heapInfoBlk = + Detail::HEAP_INFORMATION_BLOCK_PTR heap_blk = reinterpret_cast( (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); - heapInfoBlk->fPage = true; + heap_blk->fPage = true; Detail::mm_alloc_fini_timeout(); @@ -156,48 +156,47 @@ namespace Kernel { if (kHeapCount < 1) return -kErrorInternal; - if (((IntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK)) <= 0) - return -kErrorInternal; - if (((IntPtr)heap_ptr - kInvalidAddress) < 0) - return -kErrorInternal; + + if (!heap_ptr) + return -kErrorInvalidData; Detail::mm_alloc_init_timeout(); - Detail::HEAP_INFORMATION_BLOCK_PTR heapInfoBlk = + Detail::HEAP_INFORMATION_BLOCK_PTR heap_blk = reinterpret_cast( (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); - if (heapInfoBlk && heapInfoBlk->fMagic == kKernelHeapMagic) + if (heap_blk && heap_blk->fMagic == kKernelHeapMagic) { - if (!heapInfoBlk->fPresent) + if (!heap_blk->fPresent) { Detail::mm_alloc_fini_timeout(); return -kErrorHeapNotPresent; } - if (heapInfoBlk->fCRC32 != 0) + if (heap_blk->fCRC32 != 0) { - if (heapInfoBlk->fCRC32 != - ke_calculate_crc32((Char*)heapInfoBlk->fHeapPtr, - heapInfoBlk->fHeapSize)) + if (heap_blk->fCRC32 != + ke_calculate_crc32((Char*)heap_blk->fHeapPtr, + heap_blk->fHeapSize)) { - if (!heapInfoBlk->fUser) + if (!heap_blk->fUser) { ke_stop(RUNTIME_CHECK_POINTER); } } } - heapInfoBlk->fHeapSize = 0UL; - heapInfoBlk->fPresent = false; - heapInfoBlk->fHeapPtr = 0; - heapInfoBlk->fCRC32 = 0; - heapInfoBlk->fMagic = 0; + heap_blk->fHeapSize = 0UL; + heap_blk->fPresent = No; + heap_blk->fHeapPtr = 0; + heap_blk->fCRC32 = 0; + heap_blk->fMagic = 0; - PTEWrapper pageWrapper(false, false, false, reinterpret_cast(heapInfoBlk) - sizeof(Detail::HEAP_INFORMATION_BLOCK)); - Ref pteAddress{&pageWrapper}; + PTEWrapper pageWrapper(false, false, false, reinterpret_cast(heap_blk) - sizeof(Detail::HEAP_INFORMATION_BLOCK)); + Ref pteAddress{pageWrapper}; - kHeapPageManager.Free(pteAddress); + kHeapPageMgr.Free(pteAddress); --kHeapCount; @@ -239,14 +238,14 @@ namespace Kernel { if (heap_ptr) { - Detail::HEAP_INFORMATION_BLOCK_PTR heapInfoBlk = + Detail::HEAP_INFORMATION_BLOCK_PTR heap_blk = reinterpret_cast( (UIntPtr)heap_ptr - sizeof(Detail::HEAP_INFORMATION_BLOCK)); - if (heapInfoBlk->fPresent && kKernelHeapMagic == heapInfoBlk->fMagic) + if (heap_blk->fPresent && kKernelHeapMagic == heap_blk->fMagic) { - heapInfoBlk->fCRC32 = - ke_calculate_crc32((Char*)heapInfoBlk->fHeapPtr, heapInfoBlk->fHeapSize); + heap_blk->fCRC32 = + ke_calculate_crc32((Char*)heap_blk->fHeapPtr, heap_blk->fHeapSize); return true; } diff --git a/dev/ZKA/Sources/MP.cxx b/dev/ZKA/Sources/MP.cxx index 7429751f..c5cab5dc 100644 --- a/dev/ZKA/Sources/MP.cxx +++ b/dev/ZKA/Sources/MP.cxx @@ -17,12 +17,6 @@ namespace Kernel { - /***********************************************************************************/ - /// @brief MP object container property. - /***********************************************************************************/ - - Property cSMPCoreName; - ///! A HardwareThread class takes care of it's owned hardware thread. ///! It has a stack for it's core. @@ -102,13 +96,13 @@ namespace Kernel { return mp_register_process(fStack); } - - //! SMP is disabled here. - - mp_do_context_switch_pre(); - mp_do_context_switch(image, stack_ptr, fStack); - - return true; + else + { + mp_do_context_switch_pre(); + mp_do_context_switch(image, stack_ptr, fStack); + + return false; + } } ///! @brief Tells if processor is waked up. @@ -123,15 +117,7 @@ namespace Kernel ///! @brief Constructor and destructors. ///! @brief Default constructor. - HardwareThreadScheduler::HardwareThreadScheduler() - { - kcout << "newoskrnl.exe: Initializing HardwareThreadScheduler." << endl; - - cSMPCoreName.GetKey() += "Property\\MPClass"; - cSMPCoreName.GetValue() = (PropertyId)this; - - kcout << "newoskrnl.exe: Initialized HardwareThreadScheduler." << endl; - } + HardwareThreadScheduler::HardwareThreadScheduler() = default; ///! @brief Default destructor. HardwareThreadScheduler::~HardwareThreadScheduler() = default; diff --git a/dev/ZKA/Sources/NewFS+FileManager.cxx b/dev/ZKA/Sources/NewFS+FileManager.cxx deleted file mode 100644 index 20fb623d..00000000 --- a/dev/ZKA/Sources/NewFS+FileManager.cxx +++ /dev/null @@ -1,109 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include -#include - -#ifdef __FSKIT_USE_NEWFS__ - -/// @brief NewFS File manager. -/// BUGS: 0 - -namespace Kernel -{ - /// @brief C++ constructor - NewFilesystemManager::NewFilesystemManager() - { - MUST_PASS(Detail::fs_init_newfs()); - fImpl = new NewFSParser(); - MUST_PASS(fImpl); - - kcout << "newoskrnl.exe: We are done here... (NewFilesystemManager).\r"; - } - - NewFilesystemManager::~NewFilesystemManager() - { - kcout << "newoskrnl.exe: Destroying it...\r"; - - if (fImpl) - { - delete fImpl; - } - } - - /// @brief Removes a node from the filesystem. - /// @param fileName The filename - /// @return If it was deleted or not. - bool NewFilesystemManager::Remove(const Char* fileName) - { - if (fileName == nullptr || *fileName == 0) - return false; - - return fImpl->RemoveCatalog(fileName); - } - - /// @brief Creates a node with the specified. - /// @param path The filename path. - /// @return The Node pointer. - NodePtr NewFilesystemManager::Create(const Char* path) - { - return node_cast(fImpl->CreateCatalog(path)); - } - - /// @brief Creates a node with is a directory. - /// @param path The filename path. - /// @return The Node pointer. - NodePtr NewFilesystemManager::CreateDirectory(const Char* path) - { - return node_cast(fImpl->CreateCatalog(path, 0, kNewFSCatalogKindDir)); - } - - /// @brief Creates a node with is a alias. - /// @param path The filename path. - /// @return The Node pointer. - NodePtr NewFilesystemManager::CreateAlias(const Char* path) - { - return node_cast(fImpl->CreateCatalog(path, 0, kNewFSCatalogKindAlias)); - } - - /// @brief Creates a node with is a page file. - /// @param path The filename path. - /// @return The Node pointer. - NodePtr NewFilesystemManager::CreateSwapFile(const Char* path) - { - return node_cast(fImpl->CreateCatalog(path, 0, kNewFSCatalogKindPage)); - } - - /// @brief Gets the root directory. - /// @return - const Char* NewFilesystemHelper::Root() - { - return kNewFSRoot; - } - - /// @brief Gets the up-dir directory. - /// @return - const Char* NewFilesystemHelper::UpDir() - { - return kNewFSUpDir; - } - - /// @brief Gets the separator character. - /// @return - const Char NewFilesystemHelper::Separator() - { - return kNewFSSeparator; - } - - /// @brief Gets the metafile character. - /// @return - const Char NewFilesystemHelper::MetaFile() - { - return kNewFSMetaFilePrefix; - } -} // namespace Kernel - -#endif // ifdef __FSKIT_USE_NEWFS__ diff --git a/dev/ZKA/Sources/NewFS+FileMgr.cxx b/dev/ZKA/Sources/NewFS+FileMgr.cxx new file mode 100644 index 00000000..dd0d4a21 --- /dev/null +++ b/dev/ZKA/Sources/NewFS+FileMgr.cxx @@ -0,0 +1,109 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include +#include + +#ifdef __FSKIT_USE_NEWFS__ + +/// @brief NewFS File manager. +/// BUGS: 0 + +namespace Kernel +{ + /// @brief C++ constructor + NewFilesystemMgr::NewFilesystemMgr() + { + MUST_PASS(Detail::fs_init_newfs()); + fImpl = new NewFSParser(); + MUST_PASS(fImpl); + + kcout << "newoskrnl.exe: We are done here... (NewFilesystemMgr).\r"; + } + + NewFilesystemMgr::~NewFilesystemMgr() + { + kcout << "newoskrnl.exe: Destroying it...\r"; + + if (fImpl) + { + delete fImpl; + } + } + + /// @brief Removes a node from the filesystem. + /// @param fileName The filename + /// @return If it was deleted or not. + bool NewFilesystemMgr::Remove(const Char* fileName) + { + if (fileName == nullptr || *fileName == 0) + return false; + + return fImpl->RemoveCatalog(fileName); + } + + /// @brief Creates a node with the specified. + /// @param path The filename path. + /// @return The Node pointer. + NodePtr NewFilesystemMgr::Create(const Char* path) + { + return node_cast(fImpl->CreateCatalog(path)); + } + + /// @brief Creates a node with is a directory. + /// @param path The filename path. + /// @return The Node pointer. + NodePtr NewFilesystemMgr::CreateDirectory(const Char* path) + { + return node_cast(fImpl->CreateCatalog(path, 0, kNewFSCatalogKindDir)); + } + + /// @brief Creates a node with is a alias. + /// @param path The filename path. + /// @return The Node pointer. + NodePtr NewFilesystemMgr::CreateAlias(const Char* path) + { + return node_cast(fImpl->CreateCatalog(path, 0, kNewFSCatalogKindAlias)); + } + + /// @brief Creates a node with is a page file. + /// @param path The filename path. + /// @return The Node pointer. + NodePtr NewFilesystemMgr::CreateSwapFile(const Char* path) + { + return node_cast(fImpl->CreateCatalog(path, 0, kNewFSCatalogKindPage)); + } + + /// @brief Gets the root directory. + /// @return + const Char* NewFilesystemHelper::Root() + { + return kNewFSRoot; + } + + /// @brief Gets the up-dir directory. + /// @return + const Char* NewFilesystemHelper::UpDir() + { + return kNewFSUpDir; + } + + /// @brief Gets the separator character. + /// @return + const Char NewFilesystemHelper::Separator() + { + return kNewFSSeparator; + } + + /// @brief Gets the metafile character. + /// @return + const Char NewFilesystemHelper::MetaFile() + { + return kNewFSMetaFilePrefix; + } +} // namespace Kernel + +#endif // ifdef __FSKIT_USE_NEWFS__ diff --git a/dev/ZKA/Sources/NewFS+IO.cxx b/dev/ZKA/Sources/NewFS+IO.cxx index ddcecde4..e1a6ac95 100644 --- a/dev/ZKA/Sources/NewFS+IO.cxx +++ b/dev/ZKA/Sources/NewFS+IO.cxx @@ -4,8 +4,8 @@ ------------------------------------------- */ -#include -#include +#include +#include /************************************************************* * diff --git a/dev/ZKA/Sources/NewFS+Journal.cxx b/dev/ZKA/Sources/NewFS+Journal.cxx index 6504b2bc..2f63cc9a 100644 --- a/dev/ZKA/Sources/NewFS+Journal.cxx +++ b/dev/ZKA/Sources/NewFS+Journal.cxx @@ -5,7 +5,7 @@ ------------------------------------------- */ #include -#include +#include #ifdef __FSKIT_USE_NEWFS__ diff --git a/dev/ZKA/Sources/PEFCodeManager.cxx b/dev/ZKA/Sources/PEFCodeManager.cxx deleted file mode 100644 index 526312e0..00000000 --- a/dev/ZKA/Sources/PEFCodeManager.cxx +++ /dev/null @@ -1,252 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include -#include -#include -#include -#include -#include -#include -#include - -/// @brief PEF stack size symbol. -#define cPefStackSizeSymbol "SizeOfReserveStack" - -namespace Kernel -{ - namespace Detail - { - /// @brief Get the PEF platform signature according to the compiled backebnd - UInt32 rt_get_pef_platform(void) noexcept - { -#ifdef __ZKA_32X0__ - return kPefArch32x0; -#elif defined(__ZKA_64X0__) - return kPefArch64x0; -#elif defined(__ZKA_AMD64__) - return kPefArchAMD64; -#elif defined(__ZKA_PPC64__) - return kPefArchPowerPC; -#elif defined(__ZKA_ARM64__) - return kPefArchARM64; -#else - return kPefArchInvalid; -#endif // __32x0__ || __64x0__ || __x86_64__ - } - } // namespace Detail - - /// @brief PEF loader constructor w/ blob. - /// @param blob - PEFLoader::PEFLoader(const VoidPtr blob) - : fCachedBlob(blob) - { - MUST_PASS(fCachedBlob); - fBad = false; - } - - /// @brief PEF loader constructor. - /// @param path the filesystem path. - PEFLoader::PEFLoader(const Char* path) - : fCachedBlob(nullptr), fBad(false), fFatBinary(false) - { - fFile.New(const_cast(path), cRestrictRB); - fPath = StringBuilder::Construct(path).Leak(); - - auto cPefHeader = "PEF_CONTAINER"; - - fCachedBlob = fFile->Read(cPefHeader); - - PEFContainer* container = reinterpret_cast(fCachedBlob); - - if (container->Cpu == Detail::rt_get_pef_platform() && - container->Magic[0] == kPefMagic[0] && - container->Magic[1] == kPefMagic[1] && - container->Magic[2] == kPefMagic[2] && - container->Magic[3] == kPefMagic[3] && - container->Magic[4] == kPefMagic[4] && container->Abi == kPefAbi) - { - return; - } - else if (container->Magic[4] == kPefMagic[0] && - container->Magic[3] == kPefMagic[1] && - container->Magic[2] == kPefMagic[2] && - container->Magic[1] == kPefMagic[3] && - container->Magic[0] == kPefMagic[0] && container->Abi == kPefAbi) - { - /// This is a fat binary. - this->fFatBinary = true; - return; - } - - fBad = true; - - if (fCachedBlob) - mm_delete_ke_heap(fCachedBlob); - - kcout << "CodeManagerPEF: Warning: Executable format error!\r"; - fCachedBlob = nullptr; - } - - /// @brief PEF destructor. - PEFLoader::~PEFLoader() - { - if (fCachedBlob) - mm_delete_ke_heap(fCachedBlob); - - fFile.Delete(); - } - - VoidPtr PEFLoader::FindSymbol(const Char* name, Int32 kind) - { - if (!fCachedBlob || fBad || !name) - return nullptr; - - PEFContainer* container = reinterpret_cast(fCachedBlob); - - auto blob = fFile->Read(name); - - PEFCommandHeader* container_header = reinterpret_cast(blob); - - constexpr auto cMangleCharacter = '$'; - const Char* cContainerKinds[] = {".code64", ".data64", ".zero64", nullptr}; - - ErrorOr errOrSym; - - switch (kind) - { - case kPefCode: { - errOrSym = StringBuilder::Construct(cContainerKinds[0]); // code symbol. - break; - } - case kPefData: { - errOrSym = StringBuilder::Construct(cContainerKinds[1]); // data symbol. - break; - } - case kPefZero: { - errOrSym = StringBuilder::Construct(cContainerKinds[2]); // block starting symbol. - break; - } - default: - return nullptr; - } - - char* unconstSymbol = const_cast(name); - - for (SizeT i = 0UL; i < rt_string_len(unconstSymbol, kPefNameLen); ++i) - { - if (unconstSymbol[i] == ' ') - { - unconstSymbol[i] = cMangleCharacter; - } - } - - errOrSym.Leak().Leak() += name; - - for (SizeT index = 0; index < container->Count; ++index) - { - if (StringBuilder::Equals(container_header->Name, - errOrSym.Leak().Leak().CData())) - { - if (container_header->Kind == kind) - { - if (container_header->Cpu != Detail::rt_get_pef_platform()) - { - if (!this->fFatBinary) - { - mm_delete_ke_heap(blob); - return nullptr; - } - } - - Char* blobRet = new Char[container_header->Size]; - - HAL::mm_update_pte(blobRet, HAL::eFlagsPresent); - HAL::mm_update_pte(blobRet, HAL::eFlagsUser); - - if (container_header->Kind != kPefCode) - HAL::mm_update_pte(blobRet, HAL::eFlagsRw); - - rt_copy_memory((VoidPtr)((Char*)blob + sizeof(PEFCommandHeader)), blobRet, container_header->Size); - - mm_delete_ke_heap(blob); - return blobRet; - } - } - } - - mm_delete_ke_heap(blob); - return nullptr; - } - - /// @brief Finds the executable entrypoint. - /// @return - ErrorOr PEFLoader::FindStart() - { - if (auto sym = this->FindSymbol(kPefStart, kPefCode); sym) - return ErrorOr(sym); - - return ErrorOr(kErrorExecutable); - } - - /// @brief Tells if the executable is loaded or not. - /// @return - bool PEFLoader::IsLoaded() noexcept - { - return !fBad && fCachedBlob; - } - - namespace Utils - { - Bool execute_from_image(PEFLoader& exec, const Int32& procKind) noexcept - { - auto errOrStart = exec.FindStart(); - - if (errOrStart.Error() != kErrorSuccess) - return false; - - UserProcess proc; - - proc.SetImageStart(errOrStart.Leak().Leak()); - proc.Kind = procKind; - proc.StackSize = *(UIntPtr*)exec.FindSymbol(cPefStackSizeSymbol, kPefData); - - if (!proc.StackSize) - { - const auto cDefaultStackSizeMib = 8; - - proc.StackSize = mib_cast(cDefaultStackSizeMib); - } - - return UserProcessScheduler::The().Add(proc); - } - } // namespace Utils - - const Char* PEFLoader::Path() - { - return fPath.Leak().CData(); - } - - const Char* PEFLoader::AsString() - { -#ifdef __32x0__ - return "32x0 PEF format."; -#elif defined(__64x0__) - return "64x0 PEF format."; -#elif defined(__x86_64__) - return "x86_64 PEF format."; -#elif defined(__powerpc64__) - return "POWER PEF format."; -#else - return "Unknown PEF format."; -#endif // __32x0__ || __64x0__ || __x86_64__ || __powerpc64__ - } - - const Char* PEFLoader::MIME() - { - return kPefApplicationMime; - } -} // namespace Kernel diff --git a/dev/ZKA/Sources/PEFCodeMgr.cxx b/dev/ZKA/Sources/PEFCodeMgr.cxx new file mode 100644 index 00000000..feafc35c --- /dev/null +++ b/dev/ZKA/Sources/PEFCodeMgr.cxx @@ -0,0 +1,252 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/// @brief PEF stack size symbol. +#define cPefStackSizeSymbol "SizeOfReserveStack" + +namespace Kernel +{ + namespace Detail + { + /// @brief Get the PEF platform signature according to the compiled backebnd + UInt32 rt_get_pef_platform(void) noexcept + { +#ifdef __ZKA_32X0__ + return kPefArch32x0; +#elif defined(__ZKA_64X0__) + return kPefArch64x0; +#elif defined(__ZKA_AMD64__) + return kPefArchAMD64; +#elif defined(__ZKA_PPC64__) + return kPefArchPowerPC; +#elif defined(__ZKA_ARM64__) + return kPefArchARM64; +#else + return kPefArchInvalid; +#endif // __32x0__ || __64x0__ || __x86_64__ + } + } // namespace Detail + + /// @brief PEF loader constructor w/ blob. + /// @param blob + PEFLoader::PEFLoader(const VoidPtr blob) + : fCachedBlob(blob) + { + MUST_PASS(fCachedBlob); + fBad = false; + } + + /// @brief PEF loader constructor. + /// @param path the filesystem path. + PEFLoader::PEFLoader(const Char* path) + : fCachedBlob(nullptr), fBad(false), fFatBinary(false) + { + fFile.New(const_cast(path), cRestrictRB); + fPath = StringBuilder::Construct(path).Leak(); + + auto cPefHeader = "PEF_CONTAINER"; + + fCachedBlob = fFile->Read(cPefHeader); + + PEFContainer* container = reinterpret_cast(fCachedBlob); + + if (container->Cpu == Detail::rt_get_pef_platform() && + container->Magic[0] == kPefMagic[0] && + container->Magic[1] == kPefMagic[1] && + container->Magic[2] == kPefMagic[2] && + container->Magic[3] == kPefMagic[3] && + container->Magic[4] == kPefMagic[4] && container->Abi == kPefAbi) + { + return; + } + else if (container->Magic[4] == kPefMagic[0] && + container->Magic[3] == kPefMagic[1] && + container->Magic[2] == kPefMagic[2] && + container->Magic[1] == kPefMagic[3] && + container->Magic[0] == kPefMagic[0] && container->Abi == kPefAbi) + { + /// This is a fat binary. + this->fFatBinary = true; + return; + } + + fBad = true; + + if (fCachedBlob) + mm_delete_ke_heap(fCachedBlob); + + kcout << "CodeMgrPEF: Warning: Executable format error!\r"; + fCachedBlob = nullptr; + } + + /// @brief PEF destructor. + PEFLoader::~PEFLoader() + { + if (fCachedBlob) + mm_delete_ke_heap(fCachedBlob); + + fFile.Delete(); + } + + VoidPtr PEFLoader::FindSymbol(const Char* name, Int32 kind) + { + if (!fCachedBlob || fBad || !name) + return nullptr; + + PEFContainer* container = reinterpret_cast(fCachedBlob); + + auto blob = fFile->Read(name); + + PEFCommandHeader* container_header = reinterpret_cast(blob); + + constexpr auto cMangleCharacter = '$'; + const Char* cContainerKinds[] = {".code64", ".data64", ".zero64", nullptr}; + + ErrorOr errOrSym; + + switch (kind) + { + case kPefCode: { + errOrSym = StringBuilder::Construct(cContainerKinds[0]); // code symbol. + break; + } + case kPefData: { + errOrSym = StringBuilder::Construct(cContainerKinds[1]); // data symbol. + break; + } + case kPefZero: { + errOrSym = StringBuilder::Construct(cContainerKinds[2]); // block starting symbol. + break; + } + default: + return nullptr; + } + + char* unconstSymbol = const_cast(name); + + for (SizeT i = 0UL; i < rt_string_len(unconstSymbol, kPefNameLen); ++i) + { + if (unconstSymbol[i] == ' ') + { + unconstSymbol[i] = cMangleCharacter; + } + } + + errOrSym.Leak().Leak() += name; + + for (SizeT index = 0; index < container->Count; ++index) + { + if (StringBuilder::Equals(container_header->Name, + errOrSym.Leak().Leak().CData())) + { + if (container_header->Kind == kind) + { + if (container_header->Cpu != Detail::rt_get_pef_platform()) + { + if (!this->fFatBinary) + { + mm_delete_ke_heap(blob); + return nullptr; + } + } + + Char* blobRet = new Char[container_header->Size]; + + HAL::mm_update_pte(blobRet, HAL::eFlagsPresent); + HAL::mm_update_pte(blobRet, HAL::eFlagsUser); + + if (container_header->Kind != kPefCode) + HAL::mm_update_pte(blobRet, HAL::eFlagsRw); + + rt_copy_memory((VoidPtr)((Char*)blob + sizeof(PEFCommandHeader)), blobRet, container_header->Size); + + mm_delete_ke_heap(blob); + return blobRet; + } + } + } + + mm_delete_ke_heap(blob); + return nullptr; + } + + /// @brief Finds the executable entrypoint. + /// @return + ErrorOr PEFLoader::FindStart() + { + if (auto sym = this->FindSymbol(kPefStart, kPefCode); sym) + return ErrorOr(sym); + + return ErrorOr(kErrorExecutable); + } + + /// @brief Tells if the executable is loaded or not. + /// @return + bool PEFLoader::IsLoaded() noexcept + { + return !fBad && fCachedBlob; + } + + namespace Utils + { + Bool execute_from_image(PEFLoader& exec, const Int32& procKind) noexcept + { + auto errOrStart = exec.FindStart(); + + if (errOrStart.Error() != kErrorSuccess) + return false; + + UserProcess proc; + + proc.SetImageStart(errOrStart.Leak().Leak()); + proc.Kind = procKind; + proc.StackSize = *(UIntPtr*)exec.FindSymbol(cPefStackSizeSymbol, kPefData); + + if (!proc.StackSize) + { + const auto cDefaultStackSizeMib = 8; + + proc.StackSize = mib_cast(cDefaultStackSizeMib); + } + + return UserProcessScheduler::The().Add(proc); + } + } // namespace Utils + + const Char* PEFLoader::Path() + { + return fPath.Leak().CData(); + } + + const Char* PEFLoader::AsString() + { +#ifdef __32x0__ + return "32x0 PEF format."; +#elif defined(__64x0__) + return "64x0 PEF format."; +#elif defined(__x86_64__) + return "x86_64 PEF format."; +#elif defined(__powerpc64__) + return "POWER PEF format."; +#else + return "Unknown PEF format."; +#endif // __32x0__ || __64x0__ || __x86_64__ || __powerpc64__ + } + + const Char* PEFLoader::MIME() + { + return kPefApplicationMime; + } +} // namespace Kernel diff --git a/dev/ZKA/Sources/PageManager.cxx b/dev/ZKA/Sources/PageManager.cxx deleted file mode 100644 index dd18709f..00000000 --- a/dev/ZKA/Sources/PageManager.cxx +++ /dev/null @@ -1,111 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include -#include - -#ifdef __ZKA_AMD64__ -#include -#elif defined(__ZKA_ARM64__) -#include -#endif // ifdef __ZKA_AMD64__ || defined(__ZKA_ARM64__) - -//! null deref will throw (Page Zero detected, aborting app!) -#define kProtectedRegionEnd (512) - -namespace Kernel -{ - PTEWrapper::PTEWrapper(Boolean Rw, Boolean User, Boolean ExecDisable, UIntPtr VirtAddr) - : fRw(Rw), - fUser(User), - fExecDisable(ExecDisable), - fVirtAddr(VirtAddr), - fCache(false), - fShareable(false), - fWt(false), - fPresent(true), - fAccessed(false) - { - } - - PTEWrapper::~PTEWrapper() = default; - - /// @brief Flush virtual address. - /// @param VirtAddr - Void PageManager::FlushTLB() - { - hal_flush_tlb(); - } - - /// @brief Reclaim freed page. - /// @return - Bool PTEWrapper::Reclaim() - { - if (!this->fPresent) - { - this->fPresent = true; - return true; - } - - return false; - } - - /// @brief Request a PTE. - /// @param Rw r/w? - /// @param User user mode? - /// @param ExecDisable disable execution on page? - /// @return - PTEWrapper PageManager::Request(Boolean Rw, Boolean User, Boolean ExecDisable, SizeT Sz) - { - // Store PTE wrapper right after PTE. - VoidPtr ptr = Kernel::HAL::mm_alloc_bitmap(Rw, User, Sz); - - return PTEWrapper{Rw, User, ExecDisable, reinterpret_cast(ptr)}; - } - - /// @brief Disable BitMap. - /// @param wrapper the wrapper. - /// @return If the page bitmap was cleared or not. - Bool PageManager::Free(Ref& wrapper) - { - if (!Kernel::HAL::mm_free_bitmap((VoidPtr)wrapper->VirtualAddress())) - return false; - - return true; - } - - /// @brief Virtual PTE address. - /// @return The virtual address of the page. - const UIntPtr PTEWrapper::VirtualAddress() - { - return (fVirtAddr); - } - - bool PTEWrapper::Shareable() - { - return fShareable; - } - - bool PTEWrapper::Present() - { - return fPresent; - } - - bool PTEWrapper::Access() - { - return fAccessed; - } - - void PTEWrapper::NoExecute(const bool enable) - { - this->fExecDisable = enable; - } - - const bool& PTEWrapper::NoExecute() - { - return this->fExecDisable; - } -} // namespace Kernel diff --git a/dev/ZKA/Sources/PageMgr.cxx b/dev/ZKA/Sources/PageMgr.cxx new file mode 100644 index 00000000..78b279f3 --- /dev/null +++ b/dev/ZKA/Sources/PageMgr.cxx @@ -0,0 +1,111 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include +#include + +#ifdef __ZKA_AMD64__ +#include +#elif defined(__ZKA_ARM64__) +#include +#endif // ifdef __ZKA_AMD64__ || defined(__ZKA_ARM64__) + +//! null deref will throw (Page Zero detected, aborting app!) +#define kProtectedRegionEnd (512) + +namespace Kernel +{ + PTEWrapper::PTEWrapper(Boolean Rw, Boolean User, Boolean ExecDisable, UIntPtr VirtAddr) + : fRw(Rw), + fUser(User), + fExecDisable(ExecDisable), + fVirtAddr(VirtAddr), + fCache(false), + fShareable(false), + fWt(false), + fPresent(true), + fAccessed(false) + { + } + + PTEWrapper::~PTEWrapper() = default; + + /// @brief Flush virtual address. + /// @param VirtAddr + Void PageMgr::FlushTLB() + { + hal_flush_tlb(); + } + + /// @brief Reclaim freed page. + /// @return + Bool PTEWrapper::Reclaim() + { + if (!this->fPresent) + { + this->fPresent = true; + return true; + } + + return false; + } + + /// @brief Request a PTE. + /// @param Rw r/w? + /// @param User user mode? + /// @param ExecDisable disable execution on page? + /// @return + PTEWrapper PageMgr::Request(Boolean Rw, Boolean User, Boolean ExecDisable, SizeT Sz) + { + // Store PTE wrapper right after PTE. + VoidPtr ptr = Kernel::HAL::mm_alloc_bitmap(Rw, User, Sz); + + return PTEWrapper{Rw, User, ExecDisable, reinterpret_cast(ptr)}; + } + + /// @brief Disable BitMap. + /// @param wrapper the wrapper. + /// @return If the page bitmap was cleared or not. + Bool PageMgr::Free(Ref& wrapper) + { + if (!Kernel::HAL::mm_free_bitmap((VoidPtr)wrapper.Leak().VirtualAddress())) + return false; + + return true; + } + + /// @brief Virtual PTE address. + /// @return The virtual address of the page. + const UIntPtr PTEWrapper::VirtualAddress() + { + return (fVirtAddr); + } + + bool PTEWrapper::Shareable() + { + return fShareable; + } + + bool PTEWrapper::Present() + { + return fPresent; + } + + bool PTEWrapper::Access() + { + return fAccessed; + } + + void PTEWrapper::NoExecute(const bool enable) + { + this->fExecDisable = enable; + } + + const bool& PTEWrapper::NoExecute() + { + return this->fExecDisable; + } +} // namespace Kernel diff --git a/dev/ZKA/Sources/Pmm.cxx b/dev/ZKA/Sources/Pmm.cxx index b0855281..58335a2b 100644 --- a/dev/ZKA/Sources/Pmm.cxx +++ b/dev/ZKA/Sources/Pmm.cxx @@ -19,9 +19,9 @@ namespace Kernel { /// @brief Pmm constructor. Pmm::Pmm() - : fPageManager() + : fPageMgr() { - kcout << "[PMM] Allocate PageMemoryManager"; + kcout << "[PMM] Allocate PageMemoryMgr"; } Pmm::~Pmm() = default; @@ -31,17 +31,15 @@ namespace Kernel /// @param readWrite is it r/w? Ref Pmm::RequestPage(Boolean user, Boolean readWrite) { - PTEWrapper pt = fPageManager.Leak().Request(user, readWrite, false, kPTESize); + PTEWrapper pt = fPageMgr.Leak().Request(user, readWrite, false, kPTESize); if (pt.fPresent) { - kcout << "[PMM]: Allocation was successful.\r"; - return Ref(pt); + kcout << "[PMM]: Allocation failed.\r"; + return {}; } - kcout << "[PMM]: Allocation failed.\r"; - - return {}; + return Ref(pt); } Boolean Pmm::FreePage(Ref PageRef) diff --git a/dev/ZKA/Sources/Storage/ATADeviceInterface.cxx b/dev/ZKA/Sources/Storage/ATADeviceInterface.cxx index 183481e1..97d2e2e4 100644 --- a/dev/ZKA/Sources/Storage/ATADeviceInterface.cxx +++ b/dev/ZKA/Sources/Storage/ATADeviceInterface.cxx @@ -43,7 +43,7 @@ ATADeviceInterface& ATADeviceInterface::operator<<(MountpointInterface* Data) if (!Data) return *this; - for (SizeT driveCount = 0; driveCount < kDriveManagerCount; ++driveCount) + for (SizeT driveCount = 0; driveCount < kDriveMgrCount; ++driveCount) { auto interface = Data->GetAddressOf(driveCount); if ((interface) && rt_string_cmp((interface)->fDriveKind(), "ATA-", 5) == 0) @@ -69,7 +69,7 @@ ATADeviceInterface& ATADeviceInterface::operator>>(MountpointInterface* Data) if (!Data) return *this; - for (SizeT driveCount = 0; driveCount < kDriveManagerCount; ++driveCount) + for (SizeT driveCount = 0; driveCount < kDriveMgrCount; ++driveCount) { auto interface = Data->GetAddressOf(driveCount); if ((interface) && rt_string_cmp((interface)->fDriveKind(), "ATA-", 5) == 0) diff --git a/dev/ZKA/Sources/User.cxx b/dev/ZKA/Sources/User.cxx index 871d011f..5aa688be 100644 --- a/dev/ZKA/Sources/User.cxx +++ b/dev/ZKA/Sources/User.cxx @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include diff --git a/dev/ZKA/Sources/UserProcessScheduler.cxx b/dev/ZKA/Sources/UserProcessScheduler.cxx index 26d8be79..48988e57 100644 --- a/dev/ZKA/Sources/UserProcessScheduler.cxx +++ b/dev/ZKA/Sources/UserProcessScheduler.cxx @@ -34,7 +34,7 @@ namespace Kernel /// @brief UserProcess scheduler instance. /***********************************************************************************/ - STATIC UserProcessScheduler* cProcessScheduler; + STATIC UserProcessScheduler* cProcessScheduler = nullptr; /// @brief Gets the last exit code. /// @note Not thread-safe. -- cgit v1.2.3