diff options
| author | Amlal <amlal@nekernel.org> | 2025-04-27 19:51:43 +0200 |
|---|---|---|
| committer | Amlal <amlal@nekernel.org> | 2025-04-27 19:51:43 +0200 |
| commit | 11dd858e1d9da71a182bc707ca3a481dafbccbe4 (patch) | |
| tree | 0b2ce000054b257e005199f31ac6a21a0eb5ffe5 | |
| parent | cb2f383f45dda8d1cdcef0b87fe4c70243659701 (diff) | |
dev, kernel: refactor and improve parts of the kernel, related to I/O
Signed-off-by: Amlal <amlal@nekernel.org>
| -rw-r--r-- | dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc | 10 | ||||
| -rw-r--r-- | dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc | 4 | ||||
| -rw-r--r-- | dev/kernel/KernelKit/FileMgr.h | 10 | ||||
| -rw-r--r-- | dev/kernel/StorageKit/ATA.h | 4 | ||||
| -rw-r--r-- | dev/kernel/StorageKit/DmaPool.h (renamed from dev/kernel/StorageKit/DMA.h) | 25 | ||||
| -rw-r--r-- | dev/kernel/StorageKit/StorageKit.h | 2 | ||||
| -rw-r--r-- | dev/kernel/SwapKit/DiskSwap.h | 2 | ||||
| -rw-r--r-- | dev/kernel/src/FS/HeFS+FileMgr.cc | 4 | ||||
| -rw-r--r-- | dev/kernel/src/FS/NeFS+FileMgr.cc | 14 |
9 files changed, 43 insertions, 32 deletions
diff --git a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc index 1591fa56..deea30e1 100644 --- a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc @@ -24,7 +24,7 @@ #include <KernelKit/ProcessScheduler.h> #include <NewKit/Utils.h> #include <StorageKit/AHCI.h> -#include <StorageKit/DMA.h> +#include <StorageKit/DmaPool.h> #include <modules/AHCI/AHCI.h> #include <modules/ATA/ATA.h> @@ -151,10 +151,10 @@ STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz (volatile HbaCmdTbl*) (((UInt64) command_header->Ctbau << 32) | command_header->Ctba); rt_set_memory((VoidPtr) command_table, 0, sizeof(HbaCmdTbl)); - VoidPtr ptr = Kernel::rtl_dma_alloc(size_buffer, 4096); + VoidPtr ptr = rtl_dma_alloc(size_buffer, 4096); if (Write) { - Kernel::rt_copy_memory(buffer, ptr, size_buffer); + rt_copy_memory(buffer, ptr, size_buffer); } // Build the PRDT @@ -293,7 +293,7 @@ STATIC BOOL ahci_enable_and_probe() { STATIC Bool drv_init_command_structures_ahci() { // Allocate 4KiB for Command List (32 headers) - VoidPtr clb_mem = Kernel::rtl_dma_alloc(4096, 1024); + VoidPtr clb_mem = rtl_dma_alloc(4096, 1024); if (!clb_mem) { kout << "Failed to allocate CLB memory!\r"; return NO; @@ -312,7 +312,7 @@ STATIC Bool drv_init_command_structures_ahci() { for (Int32 i = 0; i < 32; ++i) { // Allocate 4KiB for Command Table - VoidPtr ct_mem = Kernel::rtl_dma_alloc(4096, 128); + VoidPtr ct_mem = rtl_dma_alloc(4096, 128); if (!ct_mem) { (Void)(kout << "Failed to allocate CTB memory for slot " << hex_number(i)); kout << "!\r"; diff --git a/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc index db9a1747..a024d2fe 100644 --- a/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc @@ -173,7 +173,7 @@ namespace Kernel { /// @brief Initialize an PIO device (StorageKit function) /// @param is_master is the current PIO master? /// @return [io:master] for PIO device. -BOOL sk_init_pio_device(BOOL is_master, UInt16& io, UInt8& master) { +BOOL sk_init_ata_device(BOOL is_master, UInt16& io, UInt8& master) { return drv_pio_std_init(ATA_SECONDARY_IO, is_master, io, master); } @@ -225,7 +225,7 @@ namespace Detail { /// @brief Acquires a new PIO device with drv_index in mind. /// @param drv_index The drive index to assign. /// @return A wrapped device interface if successful, or error code. -ErrorOr<ATADeviceInterface> sk_acquire_pio_device(Int32 drv_index) { +ErrorOr<ATADeviceInterface> sk_acquire_ata_device(Int32 drv_index) { /// here we don't check if we probed ATA, since we'd need to grab IO after that. ATADeviceInterface device(Detail::sk_io_read_pio, Detail::sk_io_write_pio); diff --git a/dev/kernel/KernelKit/FileMgr.h b/dev/kernel/KernelKit/FileMgr.h index 465d0afb..3dff61b0 100644 --- a/dev/kernel/KernelKit/FileMgr.h +++ b/dev/kernel/KernelKit/FileMgr.h @@ -21,6 +21,7 @@ #ifndef INC_FILEMGR_H #define INC_FILEMGR_H +//! Include filesystems that neoskrnl supports. #include <FSKit/Ext2.h> #include <FSKit/HeFS.h> #include <FSKit/NeFS.h> @@ -35,7 +36,7 @@ #include <hint/CompilerHint.h> /// @brief Filesystem manager, abstraction over mounted filesystem. -/// Works like the VFS or IFS. +/// Works like the VFS or IFS subsystem. #define kRestrictR "r" #define kRestrictRB "rb" @@ -45,7 +46,7 @@ #define kRestrictMax (5U) -#define node_cast(PTR) reinterpret_cast<Kernel::NodePtr>(PTR) +#define rtl_node_cast(PTR) reinterpret_cast<Kernel::NodePtr>(PTR) /** @note Refer to first enum. @@ -54,16 +55,17 @@ #define kFileMimeGeneric "ne-application-kind/all" /** @brief invalid position. (n-pos) */ -#define kNPos (SizeT)(-1); +#define kFileMgrNPos (~0UL) namespace Kernel { enum { + kFileIOInvalid = 0, kFileWriteAll = 100, kFileReadAll = 101, kFileReadChunk = 102, kFileWriteChunk = 103, kFileIOCnt = (kFileWriteChunk - kFileWriteAll) + 1, - // file flags + // File flags (HFS+, NeFS specific) kFileFlagRsrc = 104, kFileFlagData = 105, }; diff --git a/dev/kernel/StorageKit/ATA.h b/dev/kernel/StorageKit/ATA.h index d2950228..5887c579 100644 --- a/dev/kernel/StorageKit/ATA.h +++ b/dev/kernel/StorageKit/ATA.h @@ -47,10 +47,10 @@ class ATADeviceInterface : public IDeviceObject<MountpointInterface*> { /// @brief Initialize an PIO device (StorageKit function) /// @param is_master is the current PIO master? /// @return [io:master] for PIO device. -BOOL sk_init_pio_device(BOOL is_master, UInt16& io, UInt8& master); +BOOL sk_init_ata_device(BOOL is_master, UInt16& io, UInt8& master); /// @brief Acquires a new PIO device with drv_index in mind. /// @param drv_index The drive index to assign. /// @return A wrapped device interface if successful, or error code. -ErrorOr<ATADeviceInterface> sk_acquire_pio_device(Int32 drv_index); +ErrorOr<ATADeviceInterface> sk_acquire_ata_device(Int32 drv_index); } // namespace Kernel diff --git a/dev/kernel/StorageKit/DMA.h b/dev/kernel/StorageKit/DmaPool.h index c572ffa3..9e59910a 100644 --- a/dev/kernel/StorageKit/DMA.h +++ b/dev/kernel/StorageKit/DmaPool.h @@ -23,15 +23,18 @@ #pragma once #include <KernelKit/DebugOutput.h> -#include <NewKit/Defines.h> #ifdef __NE_AMD64__ -#define DMA_POOL_START (0x1000000) -#define DMA_POOL_SIZE (0x1000000) +#define NE_DMA_POOL_START (0x1000000) +#define NE_DMA_POOL_SIZE (0x1000000) namespace Kernel { -inline UInt8* kDmaPoolPtr = (UInt8*) DMA_POOL_START; +/// @brief DMA pool base pointer, here we're sure that AHCI or whatever tricky standard sees it. +inline UInt8* kDmaPoolPtr = (UInt8*) NE_DMA_POOL_START; +/// @brief allocate from the rtl_dma_alloc system. +/// @param size the size of the chunk to allocate. +/// @param align alignement of pointer. inline VoidPtr rtl_dma_alloc(SizeT size, SizeT align) { if (!size) { return nullptr; @@ -39,9 +42,10 @@ inline VoidPtr rtl_dma_alloc(SizeT size, SizeT align) { UIntPtr addr = (UIntPtr) kDmaPoolPtr; - addr = (addr + (align - 1)) & ~(align - 1); // Align up + /// here we just align the address according to a `align` variable, i'd rather be a power of two really. + addr = (addr + (align - 1)) & ~(align - 1); - if (addr + size >= DMA_POOL_START + DMA_POOL_SIZE) { + if ((addr + size) >= (NE_DMA_POOL_START + NE_DMA_POOL_SIZE)) { kout << "DMA Pool exhausted!\r"; return nullptr; @@ -57,14 +61,19 @@ inline Void rtl_dma_free(SizeT size) { kDmaPoolPtr = (UInt8*) (kDmaPoolPtr - size); } -inline Void rtl_dma_flush(Void* ptr, SizeT size_buffer) { - if (ptr > (Void*) (DMA_POOL_START + DMA_POOL_SIZE)) { +inline Void rtl_dma_flush(VoidPtr ptr, SizeT size_buffer) { + if (ptr > (Void*) (NE_DMA_POOL_START + NE_DMA_POOL_SIZE)) { + return; + } + + if (!ptr) { return; } for (SizeT i = 0; i < size_buffer; ++i) { asm volatile("clflush (%0)" : : "r"((UInt8*) ptr + i) : "memory"); } + asm volatile("mfence" ::: "memory"); } } // namespace Kernel diff --git a/dev/kernel/StorageKit/StorageKit.h b/dev/kernel/StorageKit/StorageKit.h index 9dbb014d..adb9f5dc 100644 --- a/dev/kernel/StorageKit/StorageKit.h +++ b/dev/kernel/StorageKit/StorageKit.h @@ -8,7 +8,7 @@ #define kDriveSectorSizeHDD (512U) #define kDriveSectorSizeSSD (512U) -#define kDriveSectorSizeOptical (2048) +#define kDriveSectorSizeOptical (2048U) namespace Kernel { template <typename T> diff --git a/dev/kernel/SwapKit/DiskSwap.h b/dev/kernel/SwapKit/DiskSwap.h index b7e403aa..1832923f 100644 --- a/dev/kernel/SwapKit/DiskSwap.h +++ b/dev/kernel/SwapKit/DiskSwap.h @@ -68,5 +68,5 @@ typedef struct SWAP_DISK_HEADER { UInt64 fVirtualAddress; SizeT fBlobSz; UInt8 fBlob[1]; -} PACKED SWAP_DISK_HEADER; +} PACKED ALIGN(8) SWAP_DISK_HEADER; } // namespace Kernel diff --git a/dev/kernel/src/FS/HeFS+FileMgr.cc b/dev/kernel/src/FS/HeFS+FileMgr.cc index e0b92a8d..d7530d8f 100644 --- a/dev/kernel/src/FS/HeFS+FileMgr.cc +++ b/dev/kernel/src/FS/HeFS+FileMgr.cc @@ -10,5 +10,5 @@ #include <KernelKit/FileMgr.h> #include <KernelKit/MemoryMgr.h> -#endif // ifdef __FSKIT_INCLUDES_HEFS__ -#endif // ifndef __NE_MINIMAL_OS__ +#endif // ifdef __FSKIT_INCLUDES_HEFS__ +#endif // ifndef __NE_MINIMAL_OS__ diff --git a/dev/kernel/src/FS/NeFS+FileMgr.cc b/dev/kernel/src/FS/NeFS+FileMgr.cc index 56fc2dbb..c92d6727 100644 --- a/dev/kernel/src/FS/NeFS+FileMgr.cc +++ b/dev/kernel/src/FS/NeFS+FileMgr.cc @@ -10,7 +10,7 @@ #include <KernelKit/FileMgr.h> #include <KernelKit/MemoryMgr.h> -/// @brief NeFS File manager. +/// @brief NeFS File System Manager. /// BUGS: 0 namespace Kernel { @@ -42,28 +42,28 @@ bool NeFileSystemMgr::Remove(_Input const Char* path) { /// @param path The filename path. /// @return The Node pointer. NodePtr NeFileSystemMgr::Create(_Input const Char* path) { - return node_cast(mParser->CreateCatalog(path)); + return rtl_node_cast(mParser->CreateCatalog(path)); } /// @brief Creates a node with is a directory. /// @param path The filename path. /// @return The Node pointer. NodePtr NeFileSystemMgr::CreateDirectory(const Char* path) { - return node_cast(mParser->CreateCatalog(path, 0, kNeFSCatalogKindDir)); + return rtl_node_cast(mParser->CreateCatalog(path, 0, kNeFSCatalogKindDir)); } /// @brief Creates a node with is a alias. /// @param path The filename path. /// @return The Node pointer. NodePtr NeFileSystemMgr::CreateAlias(const Char* path) { - return node_cast(mParser->CreateCatalog(path, 0, kNeFSCatalogKindAlias)); + return rtl_node_cast(mParser->CreateCatalog(path, 0, kNeFSCatalogKindAlias)); } /// @brief Creates a node with is a page file. /// @param path The filename path. /// @return The Node pointer. NodePtr NeFileSystemMgr::CreateSwapFile(const Char* path) { - return node_cast(mParser->CreateCatalog(path, 0, kNeFSCatalogKindPage)); + return rtl_node_cast(mParser->CreateCatalog(path, 0, kNeFSCatalogKindPage)); } /// @brief Gets the root directory. @@ -101,7 +101,7 @@ _Output NodePtr NeFileSystemMgr::Open(_Input const Char* path, _Input const Char auto catalog = mParser->GetCatalog(path); - return node_cast(catalog); + return rtl_node_cast(catalog); } /// @brief Writes to a catalog's fork. @@ -177,7 +177,7 @@ _Output Bool NeFileSystemMgr::Seek(NodePtr node, SizeT off) { /// @retval false always returns this, it is unimplemented. _Output SizeT NeFileSystemMgr::Tell(NodePtr node) { - if (!node) return kNPos; + if (!node) return kFileMgrNPos; return mParser->Tell(reinterpret_cast<NEFS_CATALOG_STRUCT*>(node)); } |
