summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2025-03-18 05:20:57 +0100
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2025-03-18 05:20:57 +0100
commit1311cdcfaa7fad132196a615899fd38393f54daf (patch)
tree27dbc62a874fb5ed39806475dbf7ed1828e0de40
parent8740d53b6e4a5dae1b68123cba57782c3845f924 (diff)
feat(AHCI, HAL): Improve AHCI error handling & optimize HAL initialization
- Refactored `sk_acquire_ahci_device(Int32 drv_index)` to return `ErrorOr<AHCIDeviceInterface>` for proper error handling instead of returning a raw object. - Moved `sk_init_ahci_device(BOOL atapi)` into the `NeOS` namespace for better encapsulation. - Reduced `HbaMem::Ports` array size from `Ports[32]` to `Ports[1]` to optimize memory usage for single-port AHCI controllers. - Removed manual constructor initialization (`__CTOR_LIST__`, `__DTOR_LIST__`) from HAL, simplifying kernel startup. - Updated `hal_real_init()` to initialize AHCI storage (`sk_init_ahci_device(NO)`) before userland execution. - Refactored `hal_get_phys_address(void* virtual_address)` to use `VoidPtr` for type safety. - Fixed incorrect buffer size (`kSzIdent`) in AHCI driver initialization from `kib_cast(1)` to `512`, ensuring correct sector identification. - Removed redundant drive model string parsing logic. - Refactored AHCI PRD iteration loop to use `prd_i` instead of `i`, improving readability. - Optimized `drv_std_input_output` by simplifying wait loop logic. Overall, this commit improves AHCI error handling, reduces redundant initialization, optimizes memory usage, and cleans up HAL and storage code. Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
-rw-r--r--dev/Kernel/HALKit/AMD64/HalKernelMain.cc16
-rw-r--r--dev/Kernel/HALKit/AMD64/HalPagingMgrAMD64.cc2
-rw-r--r--dev/Kernel/HALKit/AMD64/Processor.h2
-rw-r--r--dev/Kernel/HALKit/AMD64/Storage/AHCI+Generic.cc82
-rw-r--r--dev/Kernel/StorageKit/AHCI.h4
-rw-r--r--dev/Mod/AHCI/AHCI.h2
6 files changed, 45 insertions, 63 deletions
diff --git a/dev/Kernel/HALKit/AMD64/HalKernelMain.cc b/dev/Kernel/HALKit/AMD64/HalKernelMain.cc
index 88d2007c..ef5c93e3 100644
--- a/dev/Kernel/HALKit/AMD64/HalKernelMain.cc
+++ b/dev/Kernel/HALKit/AMD64/HalKernelMain.cc
@@ -4,6 +4,7 @@
------------------------------------------- */
+#include <StorageKit/AHCI.h>
#include <ArchKit/ArchKit.h>
#include <KernelKit/UserProcessScheduler.h>
#include <KernelKit/HardwareThreadScheduler.h>
@@ -17,9 +18,6 @@ EXTERN_C NeOS::VoidPtr kInterruptVectorTable[];
EXTERN_C NeOS::VoidPtr mp_user_switch_proc;
EXTERN_C NeOS::Char mp_user_switch_proc_stack_begin[];
-EXTERN_C NeOS::rtl_ctor_kind __CTOR_LIST__[];
-EXTERN_C NeOS::VoidPtr __DTOR_LIST__;
-
STATIC NeOS::Void hal_init_cxx_ctors()
{
for (NeOS::SizeT i = 0U; i < NeOS::UserProcessScheduler::The().CurrentTeam().AsArray().Count(); ++i)
@@ -27,14 +25,6 @@ STATIC NeOS::Void hal_init_cxx_ctors()
NeOS::UserProcessScheduler::The().CurrentTeam().AsArray()[i] = NeOS::UserProcess();
NeOS::UserProcessScheduler::The().CurrentTeam().AsArray()[i].Status = NeOS::ProcessStatusKind::kKilled;
}
-
- NeOS::UserProcessScheduler::The().CurrentTeam().mProcessCount = 0UL;
-
- for (NeOS::SizeT index = 0UL; __CTOR_LIST__[index] != __DTOR_LIST__; ++index)
- {
- NeOS::rtl_ctor_kind constructor_cxx = (NeOS::rtl_ctor_kind)__CTOR_LIST__[index];
- constructor_cxx();
- }
}
STATIC NeOS::UInt64 hal_rdtsc_fn()
@@ -104,7 +94,7 @@ EXTERN_C void hal_init_platform(
EXTERN_C NeOS::Void hal_real_init(NeOS::Void) noexcept
{
- NeOS::NeFS::fs_init_nefs();
+ auto dev = NeOS::sk_init_ahci_device(NO);
NeOS::HAL::mp_get_cores(kHandoverHeader->f_HardwareTables.f_VendorPtr);
@@ -116,7 +106,7 @@ EXTERN_C NeOS::Void hal_real_init(NeOS::Void) noexcept
kEnd = hal_rdtsc_fn();
- kout << "Cycles Spent: " << NeOS::number(kEnd - kStart) << kendl;
+ kout << "Cycles Spent Before Userland: " << NeOS::number(kEnd - kStart) << kendl;
idt_loader.Load(idt_reg);
diff --git a/dev/Kernel/HALKit/AMD64/HalPagingMgrAMD64.cc b/dev/Kernel/HALKit/AMD64/HalPagingMgrAMD64.cc
index 854b9057..fe9f0421 100644
--- a/dev/Kernel/HALKit/AMD64/HalPagingMgrAMD64.cc
+++ b/dev/Kernel/HALKit/AMD64/HalPagingMgrAMD64.cc
@@ -52,7 +52,7 @@ namespace NeOS::HAL
/// @brief Go over the Page structure and find the address of *virtual_address*
- UInt64 hal_get_phys_address(VoidPtr virtual_address)
+ UIntPtr hal_get_phys_address(VoidPtr virtual_address)
{
// Constants for table index bits
const UInt64 kPmlIndexMask = 0x1FFULL; // Mask for PML4, PDPT, PD, PT index (9 bits)
diff --git a/dev/Kernel/HALKit/AMD64/Processor.h b/dev/Kernel/HALKit/AMD64/Processor.h
index 1395f03b..d223c320 100644
--- a/dev/Kernel/HALKit/AMD64/Processor.h
+++ b/dev/Kernel/HALKit/AMD64/Processor.h
@@ -206,7 +206,7 @@ namespace NeOS::HAL
return edx & (1 << 5);
}
- UIntPtr hal_get_phys_address(void* virtual_address);
+ UIntPtr hal_get_phys_address(VoidPtr virtual_address);
/***********************************************************************************/
/// @brief Get Model specific register inside core.
diff --git a/dev/Kernel/HALKit/AMD64/Storage/AHCI+Generic.cc b/dev/Kernel/HALKit/AMD64/Storage/AHCI+Generic.cc
index 1f85a142..929dcac5 100644
--- a/dev/Kernel/HALKit/AMD64/Storage/AHCI+Generic.cc
+++ b/dev/Kernel/HALKit/AMD64/Storage/AHCI+Generic.cc
@@ -75,7 +75,7 @@ STATIC Void drv_compute_disk_ahci() noexcept
{
kSATASectorCount = 0UL;
- const UInt16 kSzIdent = kib_cast(1);
+ const UInt16 kSzIdent = 512;
UInt8 identify_data[kSzIdent] = {0};
@@ -83,14 +83,6 @@ STATIC Void drv_compute_disk_ahci() noexcept
drv_std_input_output<NO, YES, YES>(0, identify_data, kAHCISectorSize, kSzIdent);
- for (SizeT i = 0; i < 40; ++i)
- {
- kCurrentDiskModel[i * 2] = identify_data[27 + (i * 2)] >> 8;
- kCurrentDiskModel[i * 2 + 1] = identify_data[27 + (i * 2) + 1] & 0xFF;
- }
-
- kCurrentDiskModel[40] = '\0';
-
kSATASectorCount = (identify_data[61] << 16) | identify_data[60];
kout << "Drive Model: " << kCurrentDiskModel << kendl;
@@ -139,20 +131,20 @@ STATIC Void drv_std_input_output(UInt64 lba, UInt8* buffer, SizeT sector_sz, Siz
UIntPtr buffer_phys = HAL::hal_get_phys_address(buffer);
- Int32 i = 0;
+ UInt16 prd_i = 0;
- for (; i < (command_header->Prdtl - 1); i++)
+ for (; prd_i < (command_header->Prdtl - 1); prd_i++)
{
- command_table->Prdt[i].Dbc = ((size_buffer / command_header->Prdtl - 1) - 1);
- command_table->Prdt[i].Dba = ((UInt32)(UInt64)buffer_phys + (i * command_table->Prdt[i].Dbc));
- command_table->Prdt[i].Dbau = (((UInt64)(buffer_phys) >> 32) + (i * command_table->Prdt[i].Dbc));
- command_table->Prdt[i].Ie = YES;
+ command_table->Prdt[prd_i].Dbc = ((size_buffer / command_header->Prdtl - 1) - 1);
+ command_table->Prdt[prd_i].Dba = ((UInt32)(UInt64)buffer_phys + (prd_i * command_table->Prdt[prd_i].Dbc));
+ command_table->Prdt[prd_i].Dbau = (((UInt64)(buffer_phys) >> 32) + (prd_i * command_table->Prdt[prd_i].Dbc));
+ command_table->Prdt[prd_i].Ie = YES;
}
- command_table->Prdt[i].Dba = ((UInt32)(UInt64)buffer_phys + (i * command_table->Prdt[i].Dbc));
- command_table->Prdt[i].Dbau = (((UInt64)(buffer_phys) >> 32) + (i * command_table->Prdt[i].Dbc));
- command_table->Prdt[i].Dbc = ((size_buffer / command_header->Prdtl - 1) - 1);
- command_table->Prdt[i].Ie = YES;
+ command_table->Prdt[prd_i].Dba = ((UInt32)(UInt64)buffer_phys + (prd_i * command_table->Prdt[prd_i].Dbc));
+ command_table->Prdt[prd_i].Dbau = (((UInt64)(buffer_phys) >> 32) + (prd_i * command_table->Prdt[prd_i].Dbc));
+ command_table->Prdt[prd_i].Dbc = ((size_buffer / command_header->Prdtl - 1) - 1);
+ command_table->Prdt[prd_i].Ie = YES;
FisRegH2D* h2d_fis = (FisRegH2D*)(&command_table->Cfis);
@@ -176,16 +168,14 @@ STATIC Void drv_std_input_output(UInt64 lba, UInt8* buffer, SizeT sector_sz, Siz
h2d_fis->CountLow = (size_buffer)&0xFF;
h2d_fis->CountHigh = (size_buffer >> 8) & 0xFF;
- while ((kSATAHba->Ports[kSATAIndex].Tfd & (kSATASRBsy | kSATASRDrq)))
- {
- kout << "Waiting for TFD...\r";
- }
+ while (kSATAHba->Ports[kSATAIndex].Tfd & (kSATASRBsy | kSATASRDrq))
+ ;
kSATAHba->Ports[kSATAIndex].Ci = (1 << slot);
while (YES)
{
- if ((kSATAHba->Ports[kSATAIndex].Ci & (1 << slot)) == 0)
+ if (!(kSATAHba->Ports[kSATAIndex].Ci & (1 << slot)))
break;
if (kSATAHba->Is & kHBAErrTaskFile)
@@ -195,7 +185,6 @@ STATIC Void drv_std_input_output(UInt64 lba, UInt8* buffer, SizeT sector_sz, Siz
}
}
- // Check IS again.
if (kSATAHba->Is & kHBAErrTaskFile)
{
err_global_get() = kErrorDiskIsCorrupted;
@@ -314,37 +303,40 @@ Bool drv_std_detected_ahci()
return kSATADev.DeviceId() != (UShort)PCI::PciConfigKind::Invalid && kSATADev.Bar(kSATABar5) != 0;
}
-UInt16 sk_init_ahci_device(BOOL atapi)
+namespace NeOS
{
- UInt16 pi = 0;
- return drv_std_init_ahci(pi, atapi);
+ UInt16 sk_init_ahci_device(BOOL atapi)
+ {
+ UInt16 pi = 0;
+ return drv_std_init_ahci(pi, atapi);
- return pi;
-}
+ return pi;
+ }
-ErrorOr<AHCIDeviceInterface> sk_acquire_ahci_device(Int32 drv_index)
-{
- if (!drv_std_detected_ahci())
- return ErrorOr<AHCIDeviceInterface>(kErrorDisk);
+ ErrorOr<AHCIDeviceInterface> sk_acquire_ahci_device(Int32 drv_index)
+ {
+ if (!drv_std_detected_ahci())
+ return ErrorOr<AHCIDeviceInterface>(kErrorDisk);
- AHCIDeviceInterface device([](IDeviceObject<MountpointInterface*>* self, MountpointInterface* mnt) -> void {
+ AHCIDeviceInterface device([](IDeviceObject<MountpointInterface*>* self, MountpointInterface* mnt) -> void {
AHCIDeviceInterface* dev = (AHCIDeviceInterface*)self;
auto disk = mnt->GetAddressOf(dev->GetIndex());
drv_std_input_output<YES, YES, NO>(disk->fPacket.fPacketLba, (UInt8*)disk->fPacket.fPacketContent, kAHCISectorSize, disk->fPacket.fPacketSize); },
- [](IDeviceObject<MountpointInterface*>* self, MountpointInterface* mnt) -> void {
- AHCIDeviceInterface* dev = (AHCIDeviceInterface*)self;
+ [](IDeviceObject<MountpointInterface*>* self, MountpointInterface* mnt) -> void {
+ AHCIDeviceInterface* dev = (AHCIDeviceInterface*)self;
- auto disk = mnt->GetAddressOf(dev->GetIndex());
- drv_std_input_output<NO, YES, NO>(disk->fPacket.fPacketLba, (UInt8*)disk->fPacket.fPacketContent, kAHCISectorSize, disk->fPacket.fPacketSize);
- },
- nullptr);
+ auto disk = mnt->GetAddressOf(dev->GetIndex());
+ drv_std_input_output<NO, YES, NO>(disk->fPacket.fPacketLba, (UInt8*)disk->fPacket.fPacketContent, kAHCISectorSize, disk->fPacket.fPacketSize);
+ },
+ nullptr);
- device.SetPi(kSATAPortsImplemented);
- device.SetIndex(drv_index);
+ device.SetPi(kSATAPortsImplemented);
+ device.SetIndex(drv_index);
- return ErrorOr<AHCIDeviceInterface>(device);
-}
+ return ErrorOr<AHCIDeviceInterface>(device);
+ }
+} // namespace NeOS
#ifdef __AHCI__
diff --git a/dev/Kernel/StorageKit/AHCI.h b/dev/Kernel/StorageKit/AHCI.h
index 12ca19d9..2eb0912b 100644
--- a/dev/Kernel/StorageKit/AHCI.h
+++ b/dev/Kernel/StorageKit/AHCI.h
@@ -54,6 +54,6 @@ namespace NeOS
UInt32 fDriveIndex{0U};
};
- UInt16 sk_init_ahci_device(BOOL atapi);
- AHCIDeviceInterface sk_acquire_ahci_device(Int32 drv_index);
+ UInt16 sk_init_ahci_device(BOOL atapi);
+ ErrorOr<AHCIDeviceInterface> sk_acquire_ahci_device(Int32 drv_index);
} // namespace NeOS
diff --git a/dev/Mod/AHCI/AHCI.h b/dev/Mod/AHCI/AHCI.h
index d4f8ceb1..0b8971e2 100644
--- a/dev/Mod/AHCI/AHCI.h
+++ b/dev/Mod/AHCI/AHCI.h
@@ -265,7 +265,7 @@ typedef struct HbaMem final
NeOS::UInt8 Resv0[0xA0 - 0x2C];
NeOS::UInt8 Vendor[0x100 - 0xA0];
- HbaPort Ports[32]; // 1 ~ 32, 32 is the max ahci devices per controller.
+ HbaPort Ports[1]; // 1 ~ 32, 32 is the max ahci devices per controller.
} HbaMem;
typedef struct HbaCmdHeader final