summaryrefslogtreecommitdiffhomepage
path: root/Private/HALKit
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-04-27 22:10:15 +0200
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-04-27 22:10:15 +0200
commitdd6568c64e440fe9d8c75539165377ddbbca3e2c (patch)
treea37e126238a4908100b3aaf701c27d183fe68a63 /Private/HALKit
parent86a2d7327f84519f525d66a7745554b41dddeb93 (diff)
MHR-18: A lot of patches and fixes, big improvements as well.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'Private/HALKit')
-rw-r--r--Private/HALKit/AMD64/HalACPIFactoryInterface.cxx9
-rw-r--r--Private/HALKit/AMD64/HalKernelMain.cxx2
-rw-r--r--Private/HALKit/AMD64/HalPageAlloc.cpp36
-rw-r--r--Private/HALKit/AMD64/HalPageAlloc.hpp2
-rw-r--r--Private/HALKit/AMD64/Processor.hpp8
-rw-r--r--Private/HALKit/AMD64/Storage/AHCI.cxx2
-rw-r--r--Private/HALKit/AMD64/Storage/ATA-PIO.cxx112
7 files changed, 75 insertions, 96 deletions
diff --git a/Private/HALKit/AMD64/HalACPIFactoryInterface.cxx b/Private/HALKit/AMD64/HalACPIFactoryInterface.cxx
index d20bf222..91e0eeb6 100644
--- a/Private/HALKit/AMD64/HalACPIFactoryInterface.cxx
+++ b/Private/HALKit/AMD64/HalACPIFactoryInterface.cxx
@@ -22,10 +22,6 @@ void rt_shutdown_acpi_virtualbox(void) { HAL::Out16(0x4004, 0x3400); }
ACPIFactoryInterface::ACPIFactoryInterface(voidPtr rsdPtr)
: fRsdp(rsdPtr), fEntries(0) {
- volatile RSDP *_rsdPtr = reinterpret_cast<volatile RSDP *>(this->fRsdp);
-
- MUST_PASS(_rsdPtr);
- MUST_PASS(_rsdPtr->Revision >= 2);
}
Void ACPIFactoryInterface::Shutdown() {
@@ -60,9 +56,12 @@ ErrorOr<voidPtr> ACPIFactoryInterface::Find(const char *signature) {
return ErrorOr<voidPtr>{-4};
}
- SDT* xsdt = (SDT*)(rsdPtr->RsdtAddress + rsdPtr->XsdtAddress);
+ SDT* xsdt = (SDT*)(rsdPtr->XsdtAddress >> (rsdPtr->XsdtAddress & 0xFFF));
+
SizeT num = xsdt->Length + sizeof(SDT) / 8;
+ this->fEntries = num;
+
kcout << "ACPI: Number of entries: " << number(num) << endl;
kcout << "ACPI: Address of XSDT: " << hex_number((UIntPtr)xsdt) << endl;
diff --git a/Private/HALKit/AMD64/HalKernelMain.cxx b/Private/HALKit/AMD64/HalKernelMain.cxx
index 82b80d34..d6b4ab76 100644
--- a/Private/HALKit/AMD64/HalKernelMain.cxx
+++ b/Private/HALKit/AMD64/HalKernelMain.cxx
@@ -74,7 +74,5 @@ EXTERN_C void hal_init_platform(
AppMain();
- hal_flush_tlb();
-
NewOS::ke_stop(RUNTIME_CHECK_BOOTSTRAP);
}
diff --git a/Private/HALKit/AMD64/HalPageAlloc.cpp b/Private/HALKit/AMD64/HalPageAlloc.cpp
index 3a8d3047..9ba0ea4d 100644
--- a/Private/HALKit/AMD64/HalPageAlloc.cpp
+++ b/Private/HALKit/AMD64/HalPageAlloc.cpp
@@ -10,28 +10,31 @@
#include <NewKit/KernelCheck.hpp>
STATIC NewOS::Boolean kAllocationInProgress = false;
+
namespace NewOS {
namespace HAL {
namespace Detail {
struct VirtualMemoryHeader {
- Boolean Present : 1;
- Boolean ReadWrite : 1;
- Boolean User : 1;
+ UInt32 Magic;
+ Boolean Present;
+ Boolean ReadWrite;
+ Boolean User;
+ SizeT PageSize;
};
struct VirtualMemoryHeaderTraits {
/// @brief Get next header.
- /// @param current
- /// @return
+ /// @param current
+ /// @return
VirtualMemoryHeader* Next(VirtualMemoryHeader* current) {
- return current + sizeof(PTE);
+ return current + sizeof(PTE) + current->PageSize;
}
/// @brief Get previous header.
- /// @param current
- /// @return
+ /// @param current
+ /// @return
VirtualMemoryHeader* Prev(VirtualMemoryHeader* current) {
- return current - sizeof(PTE);
+ return current - sizeof(PTE) - current->PageSize;
}
};
}
@@ -41,22 +44,27 @@ struct VirtualMemoryHeaderTraits {
/// @param rw read/write flag.
/// @param user user flag.
/// @return the page table of it.
-STATIC auto hal_try_alloc_new_page(Boolean rw, Boolean user) -> VoidPtr {
+STATIC auto hal_try_alloc_new_page(Boolean rw, Boolean user, SizeT size) -> VoidPtr {
if (kAllocationInProgress) return nullptr;
kAllocationInProgress = true;
+ constexpr auto cVMTMagic = 0xDEEFD00D;
+
///! fetch from the start.
Detail::VirtualMemoryHeader* vmHeader = reinterpret_cast<Detail::VirtualMemoryHeader*>(kKernelVirtualStart);
Detail::VirtualMemoryHeaderTraits traits;
- while (vmHeader->Present) {
+ while (vmHeader->Present &&
+ vmHeader->Magic != cVMTMagic) {
vmHeader = traits.Next(vmHeader);
}
+ vmHeader->Magic = cVMTMagic;
vmHeader->Present = true;
vmHeader->ReadWrite = rw;
vmHeader->User = user;
+ vmHeader->PageSize = size;
kAllocationInProgress = false;
@@ -67,14 +75,16 @@ STATIC auto hal_try_alloc_new_page(Boolean rw, Boolean user) -> VoidPtr {
/// @param rw read/write bit.
/// @param user user bit.
/// @return
-auto hal_alloc_page(Boolean rw, Boolean user) -> VoidPtr {
+auto hal_alloc_page(Boolean rw, Boolean user, SizeT size) -> VoidPtr {
/// Wait for a ongoing allocation to complete.
while (kAllocationInProgress) {
;
}
+ if (size == 0) ++size;
+
/// allocate new page.
- return hal_try_alloc_new_page(rw, user);
+ return hal_try_alloc_new_page(rw, user, size);
}
} // namespace HAL
} // namespace NewOS
diff --git a/Private/HALKit/AMD64/HalPageAlloc.hpp b/Private/HALKit/AMD64/HalPageAlloc.hpp
index 553f136e..94956329 100644
--- a/Private/HALKit/AMD64/HalPageAlloc.hpp
+++ b/Private/HALKit/AMD64/HalPageAlloc.hpp
@@ -72,7 +72,7 @@ struct PageDirectory64 final {
PageTable64 ALIGN(kPTEAlign) Pte[kPTEMax];
};
-VoidPtr hal_alloc_page(Boolean rw, Boolean user);
+VoidPtr hal_alloc_page(Boolean rw, Boolean user, SizeT size);
} // namespace NewOS::HAL
namespace NewOS {
diff --git a/Private/HALKit/AMD64/Processor.hpp b/Private/HALKit/AMD64/Processor.hpp
index 70452d1d..efe773da 100644
--- a/Private/HALKit/AMD64/Processor.hpp
+++ b/Private/HALKit/AMD64/Processor.hpp
@@ -16,6 +16,7 @@
#include <NewKit/Array.hpp>
#include <NewKit/Defines.hpp>
#include <NewKit/Utils.hpp>
+#include <FirmwareKit/Handover.hxx>
#ifdef kCPUBackendName
#undef kCPUBackendName
@@ -30,7 +31,7 @@
#define kTrapGate (0xEF)
#define kTaskGate (0b10001100)
#define kGdtCodeSelector (0x08)
-#define kVirtualAddressStartOffset (0x100)
+#define kVirtualAddressStartOffset (0x10000000)
namespace NewOS {
namespace Detail::AMD64 {
@@ -183,3 +184,8 @@ EXTERN_C void hal_load_gdt(NewOS::HAL::RegisterGDT ptr);
/// @brief Maximum size of the IDT.
#define kKernelIdtSize 0x100
#define kKernelInterruptId 0x32
+
+inline NewOS::VoidPtr kKernelVirtualStart = (NewOS::VoidPtr)kVirtualAddressStartOffset;
+inline NewOS::UIntPtr kKernelVirtualSize = 0UL;
+
+inline NewOS::VoidPtr kKernelPhysicalStart = nullptr;
diff --git a/Private/HALKit/AMD64/Storage/AHCI.cxx b/Private/HALKit/AMD64/Storage/AHCI.cxx
index c9db540a..80224d89 100644
--- a/Private/HALKit/AMD64/Storage/AHCI.cxx
+++ b/Private/HALKit/AMD64/Storage/AHCI.cxx
@@ -36,7 +36,7 @@ NewOS::Boolean drv_std_init(NewOS::UInt16& PortsImplemented) {
iterator[devIndex].Leak().EnableMmio(); /// enable the memory i/o for this ahci device.
kAhciDevice = iterator[devIndex].Leak(); /// and then leak the reference.
- kcout << "NewKernel: [PCI] Found AHCI controller.\r\n";
+ kcout << "New Kernel: [PCI] Found AHCI controller.\r\n";
return true;
}
diff --git a/Private/HALKit/AMD64/Storage/ATA-PIO.cxx b/Private/HALKit/AMD64/Storage/ATA-PIO.cxx
index 7f1cc11a..e72999ca 100644
--- a/Private/HALKit/AMD64/Storage/ATA-PIO.cxx
+++ b/Private/HALKit/AMD64/Storage/ATA-PIO.cxx
@@ -32,21 +32,21 @@ static Int32 kATADeviceType = kATADeviceCount;
static Char kATAData[kATADataLen] = {0};
Boolean drv_std_wait_io(UInt16 IO) {
- for (int i = 0; i < 4; i++) In8(IO + ATA_REG_STATUS);
+ for (int i = 0; i < 4; i++) In8(IO + ATA_REG_STATUS);
-ATAWaitForIO_Retry:
- auto statRdy = In8(IO + ATA_REG_STATUS);
+ ATAWaitForIO_Retry:
+ auto statRdy = In8(IO + ATA_REG_STATUS);
- if ((statRdy & ATA_SR_BSY)) goto ATAWaitForIO_Retry;
+ if ((statRdy & ATA_SR_BSY)) goto ATAWaitForIO_Retry;
-ATAWaitForIO_Retry2:
- statRdy = In8(IO + ATA_REG_STATUS);
+ ATAWaitForIO_Retry2:
+ statRdy = In8(IO + ATA_REG_STATUS);
- if (statRdy & ATA_SR_ERR) return false;
+ if (statRdy & ATA_SR_ERR) return false;
- if (!(statRdy & ATA_SR_DRDY)) goto ATAWaitForIO_Retry2;
+ if (!(statRdy & ATA_SR_DRDY)) goto ATAWaitForIO_Retry2;
- return true;
+ return true;
}
Void drv_std_select(UInt16 Bus) {
@@ -58,76 +58,41 @@ Void drv_std_select(UInt16 Bus) {
Boolean drv_std_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus,
UInt8& OutMaster) {
- if (drv_std_detected()) return true;
+ if (drv_std_detected()) return true;
- UInt16 IO = Bus;
+ UInt16 IO = Bus;
- drv_std_select(IO);
+ drv_std_select(IO);
- // Bus init, NEIN bit.
- Out8(IO + ATA_REG_NEIN, 1);
+ // Bus init, NEIN bit.
+ Out8(IO + ATA_REG_NEIN, 1);
- // identify until it's good.
+ // identify until it's good.
ATAInit_Retry:
- auto statRdy = In8(IO + ATA_REG_STATUS);
-
- if (statRdy & ATA_SR_ERR) {
- kcout << "New OS: ATA: Select error, not an IDE based hard-drive.\r\n";
-
- return false;
- }
-
- if ((statRdy & ATA_SR_BSY)) goto ATAInit_Retry;
-
- Out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY);
-
- rt_set_memory(kATAData, 0, kATADataLen);
+ auto statRdy = In8(IO + ATA_REG_STATUS);
- /// fetch serial info
- /// model, speed, number of sectors...
+ if (statRdy & ATA_SR_ERR) {
+ return false;
+ }
- drv_std_wait_io(IO);
-
- for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) {
- kATAData[indexData] = In16(IO + ATA_REG_DATA);
- }
-
- OutBus = Bus;
- OutMaster = (OutBus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE;
+ if ((statRdy & ATA_SR_BSY)) goto ATAInit_Retry;
- Out8(Bus + ATA_REG_HDDEVSEL, 0xA0 | ATA_MASTER << 4);
+ Out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY);
- In8(Bus + ATA_REG_CONTROL);
- In8(Bus + ATA_REG_CONTROL);
- In8(Bus + ATA_REG_CONTROL);
- In8(Bus + ATA_REG_CONTROL);
+ /// fetch serial info
+ /// model, speed, number of sectors...
- unsigned cl = In8(Bus + ATA_CYL_LOW); /* get the "signature bytes" */
- unsigned ch = In8(Bus + ATA_CYL_HIGH);
-
- /* differentiate ATA, ATAPI, SATA and SATAPI */
- if (cl == 0x14 && ch == 0xEB) {
- kcout << "New OS: PATAPI drive detected.\r\n";
- kATADeviceType = kATADevicePATA_PI;
- }
- if (cl == 0x69 && ch == 0x96) {
- kcout << "New OS: SATAPI drive detected.\r\n";
- kATADeviceType = kATADeviceSATA_PI;
- }
+ drv_std_wait_io(IO);
- if (cl == 0x0 && ch == 0x0) {
- kcout << "New OS: PATA drive detected.\r\n";
- kATADeviceType = kATADevicePATA;
- }
+ for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) {
+ kATAData[indexData] = In16(IO + ATA_REG_DATA);
+ }
- if (cl == 0x3c && ch == 0xc3) {
- kcout << "New OS: SATA drive detected.\r\n";
- kATADeviceType = kATADeviceSATA;
- }
+ OutBus = (Bus == ATA_PRIMARY_IO) ? ATA_PRIMARY_IO : ATA_SECONDARY_IO;
- Out8(IO + ATA_REG_CONTROL, 0x02);
+ OutMaster = (Bus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE;
- return true;
+ return true;
}
Void drv_std_read(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf,
@@ -136,6 +101,9 @@ Void drv_std_read(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf,
Lba /= SectorSz;
+ drv_std_wait_io(IO);
+ drv_std_select(IO);
+
Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F));
Out8(IO + ATA_REG_SEC_COUNT0, 1);
@@ -146,12 +114,9 @@ Void drv_std_read(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf,
Out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO);
- drv_std_wait_io(IO);
-
for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff) {
- WideChar chr = In16(IO + ATA_REG_DATA);
-
- Buf[IndexOff] = chr;
+ drv_std_wait_io(IO);
+ Buf[IndexOff] = In16(IO + ATA_REG_DATA);
}
}
@@ -161,6 +126,9 @@ Void drv_std_write(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf,
Lba /= SectorSz;
+ drv_std_wait_io(IO);
+ drv_std_select(IO);
+
Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F));
Out8(IO + ATA_REG_SEC_COUNT0, 1);
@@ -171,11 +139,9 @@ Void drv_std_write(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf,
Out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO);
- drv_std_wait_io(IO);
-
for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff) {
Out16(IO + ATA_REG_DATA, Buf[IndexOff]);
-
+ drv_std_wait_io(IO);
}
}