diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-06-03 09:31:01 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-03 09:31:01 +0200 |
| commit | 6511afbf405c31513bc88ab06bca58218610a994 (patch) | |
| tree | e2509b7f9b59643b2a97773604aa383a2fd2e2f3 | |
| parent | 5c0bb7ee7b1b0fee02cc179fb21f4c57a61d6c2d (diff) | |
| parent | bebcbe04c2b47b3b4fcdc093b1736cc0295109fe (diff) | |
Merge pull request #36 from nekernel-org/dev
0.0.3 — NeKernel
97 files changed, 717 insertions, 511 deletions
diff --git a/.github/workflows/boot-pio.yml b/.github/workflows/boot-pio.yml index 80c5b932..3a8c087b 100644 --- a/.github/workflows/boot-pio.yml +++ b/.github/workflows/boot-pio.yml @@ -15,6 +15,5 @@ jobs: - uses: actions/checkout@v4 - name: Install Packages run: sudo apt update && sudo apt install mingw-w64 qemu-utils nasm - - name: Build BootZ (ATA PIO) - run: cd dev/boot && export ATA_PIO_SUPPORT=1 && make -f amd64-ci.make all + diff --git a/.github/workflows/kernel-ahci-dev.yml b/.github/workflows/kernel-ahci-dev.yml new file mode 100644 index 00000000..a1a47fa3 --- /dev/null +++ b/.github/workflows/kernel-ahci-dev.yml @@ -0,0 +1,18 @@ +name: NeKernel CI (AHCI) + +on: + push: + branches: [ "dev" ] + pull_request: + branches: [ "dev" ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Install Packages + run: sudo apt update && sudo apt install mingw-w64 qemu-utils nasm + diff --git a/.github/workflows/kernel-ahci.yml b/.github/workflows/kernel-ahci.yml index a328a0f7..17a68a30 100644 --- a/.github/workflows/kernel-ahci.yml +++ b/.github/workflows/kernel-ahci.yml @@ -15,6 +15,4 @@ jobs: - uses: actions/checkout@v4 - name: Install Packages run: sudo apt update && sudo apt install mingw-w64 qemu-utils nasm - - name: Build NeKernel (AHCI) - run: ./setup_x64_project.sh && cd dev/kernel && export AHCI_SUPPORT=1 && make -f amd64-ci.make all - + diff --git a/.github/workflows/kernel-pio-dev.yml b/.github/workflows/kernel-pio-dev.yml new file mode 100644 index 00000000..dd6cd56f --- /dev/null +++ b/.github/workflows/kernel-pio-dev.yml @@ -0,0 +1,18 @@ +name: NeKernel CI (ATA PIO) + +on: + push: + branches: [ "dev" ] + pull_request: + branches: [ "dev" ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Install Packages + run: sudo apt update && sudo apt install mingw-w64 qemu-utils nasm +
\ No newline at end of file diff --git a/.github/workflows/kernel-pio.yml b/.github/workflows/kernel-pio.yml index 69f14a83..6c4fbb6a 100644 --- a/.github/workflows/kernel-pio.yml +++ b/.github/workflows/kernel-pio.yml @@ -15,6 +15,4 @@ jobs: - uses: actions/checkout@v4 - name: Install Packages run: sudo apt update && sudo apt install mingw-w64 qemu-utils nasm - - name: Build NeKernel (ATA PIO) - run: ./setup_x64_project.sh && cd dev/kernel && export ATA_PIO_SUPPORT=1 && make -f amd64-ci.make all - +
\ No newline at end of file diff --git a/dev/boot/amd64-desktop.make b/dev/boot/amd64-desktop.make index d39c3bcd..d7515367 100644 --- a/dev/boot/amd64-desktop.make +++ b/dev/boot/amd64-desktop.make @@ -128,7 +128,7 @@ run-efi-amd64-ata-dma: $(EMU) $(EMU_FLAGS) -device piix4-ide,id=ide -drive id=disk,file=$(IMG),format=raw,if=none -device ide-hd,drive=disk,bus=ide.0 -s -S -boot menu=on .PHONY: run-efi-amd64-ata -run-efi-amd64-ata: run-efi-amd64-ata-dma +run-efi-amd64-ata: run-efi-amd64-ata-pio # img_2 is the rescue disk. img is the bootable disk, as provided by the NeKernel specs. .PHONY: epm-img diff --git a/dev/boot/modules/BootNet/BootNet.cc b/dev/boot/modules/BootNet/BootNet.cc index 2fbd7edc..ba9beef4 100644 --- a/dev/boot/modules/BootNet/BootNet.cc +++ b/dev/boot/modules/BootNet/BootNet.cc @@ -12,7 +12,7 @@ #include <FirmwareKit/EFI/API.h> #include <modules/BootNet/BootNet.h> -STATIC EFI_GUID kEfiSimpleProtoGUID = EFI_SIMPLE_NETWORK_PROTOCOL_GUID; +STATIC EFI_GUID kEfiSimpleProtoGUID = EFI_SIMPLE_NETWORK_PROTOCOL_GUID; STATIC EFI_SIMPLE_NETWORK_PROTOCOL* kEfiProtocol = nullptr; STATIC Void bootnet_read_ip_packet(BOOTNET_INTERNET_HEADER inet, diff --git a/dev/boot/src/BootFileReader.cc b/dev/boot/src/BootFileReader.cc index 2f409848..5aadd608 100644 --- a/dev/boot/src/BootFileReader.cc +++ b/dev/boot/src/BootFileReader.cc @@ -48,7 +48,7 @@ Boot::BootFileReader::BootFileReader(const CharacterTypeUTF16* path, EfiHandlePt EfiSimpleFilesystemProtocol* efp = nullptr; EfiLoadImageProtocol* img = nullptr; - EFI_GUID guidImg = EFI_GUID(EFI_LOADED_IMAGE_PROTOCOL_GUID); + EFI_GUID guidImg = EFI_GUID(EFI_LOADED_IMAGE_PROTOCOL_GUID); if (BS->HandleProtocol(ImageHandle, &guidImg, (void**) &img) != kEfiOk) { mWriter.Write(L"BootZ: Handle-Protocol: No-Such-Protocol").Write(L"\r"); diff --git a/dev/boot/src/HEL/AMD64/BootATAcc b/dev/boot/src/HEL/AMD64/BootATA.cc index 4fd6dc16..e5e0d8c2 100644 --- a/dev/boot/src/HEL/AMD64/BootATAcc +++ b/dev/boot/src/HEL/AMD64/BootATA.cc @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss/Symphony Corp, all rights reserved. ------------------------------------------- */ @@ -74,7 +74,7 @@ ATAInit_Retry: auto status_rdy = rt_in8(IO + ATA_REG_STATUS); if (status_rdy & ATA_SR_ERR) { - writer.Write(L"BootZ: ATA: Not an IDE based drive.\r"); + writer.Write(L"VMBoot: ATA: Not an IDE based drive.\r"); return false; } diff --git a/dev/boot/src/HEL/AMD64/BootEFI.cc b/dev/boot/src/HEL/AMD64/BootEFI.cc index 3685add5..166e0169 100644 --- a/dev/boot/src/HEL/AMD64/BootEFI.cc +++ b/dev/boot/src/HEL/AMD64/BootEFI.cc @@ -21,7 +21,7 @@ STATIC EfiGraphicsOutputProtocol* kGop = nullptr; STATIC UInt16 kGopStride = 0U; -STATIC EFI_GUID kGopGuid; +STATIC EFI_GUID kGopGuid; /** Related to jumping to the reset vector. */ @@ -102,7 +102,7 @@ EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTa // Grab MP services, extended to runtime. // // ------------------------------------------- // - EFI_GUID guid_mp = EFI_GUID(EFI_MP_SERVICES_PROTOCOL_GUID); + EFI_GUID guid_mp = EFI_GUID(EFI_MP_SERVICES_PROTOCOL_GUID); EfiMpServicesProtocol* mp = nullptr; BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast<VoidPtr*>(&mp)); diff --git a/dev/boot/src/HEL/ARM64/BootEFI.cc b/dev/boot/src/HEL/ARM64/BootEFI.cc index 1633cb39..66f91352 100644 --- a/dev/boot/src/HEL/ARM64/BootEFI.cc +++ b/dev/boot/src/HEL/ARM64/BootEFI.cc @@ -29,7 +29,7 @@ STATIC EfiGraphicsOutputProtocol* kGop = nullptr; STATIC UInt16 kGopStride = 0U; -STATIC EFI_GUID kGopGuid; +STATIC EFI_GUID kGopGuid; EXTERN_C Void rt_reset_hardware(); @@ -107,7 +107,7 @@ EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTa // Grab MP services, extended to runtime. // // ------------------------------------------- // - EFI_GUID guid_mp = EFI_GUID(EFI_MP_SERVICES_PROTOCOL_GUID); + EFI_GUID guid_mp = EFI_GUID(EFI_MP_SERVICES_PROTOCOL_GUID); EfiMpServicesProtocol* mp = nullptr; BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast<VoidPtr*>(&mp)); diff --git a/dev/generic_kits/BenchKit/X64Chrono.h b/dev/generic_kits/BenchKit/X64Chrono.h index 1578814f..229146cb 100644 --- a/dev/generic_kits/BenchKit/X64Chrono.h +++ b/dev/generic_kits/BenchKit/X64Chrono.h @@ -15,7 +15,7 @@ class X64Chrono; struct X64ChronoTraits; struct X64ChronoTraits { -private: + private: STATIC UInt64 TickImpl_(void) { UInt64 a = 0, d = 0; __asm__ volatile("rdtsc" : "=a"(a), "=d"(d)); diff --git a/dev/kernel/DmaKit/DmaPool.h b/dev/kernel/DmaKit/DmaPool.h index ec98745d..9decb3f1 100644 --- a/dev/kernel/DmaKit/DmaPool.h +++ b/dev/kernel/DmaKit/DmaPool.h @@ -44,7 +44,7 @@ inline VoidPtr rtl_dma_alloc(SizeT size, SizeT align) { kDmaPoolPtr = (UInt8*) (addr + size); - HAL::mm_memory_fence((VoidPtr)addr); + HAL::mm_memory_fence((VoidPtr) addr); return (VoidPtr) addr; } diff --git a/dev/kernel/FirmwareKit/EFI/EFI.h b/dev/kernel/FirmwareKit/EFI/EFI.h index ed22f1e9..97e3ad01 100644 --- a/dev/kernel/FirmwareKit/EFI/EFI.h +++ b/dev/kernel/FirmwareKit/EFI/EFI.h @@ -664,7 +664,7 @@ typedef struct EfiSystemTable { /// The configuration table (contains the RSD PTR entry.) struct { EFI_GUID VendorGUID; - VoidPtr VendorTable; + VoidPtr VendorTable; } * ConfigurationTable; } EfiSystemTable; diff --git a/dev/kernel/FirmwareKit/GPT.h b/dev/kernel/FirmwareKit/GPT.h index df0ed286..f361c4a2 100644 --- a/dev/kernel/FirmwareKit/GPT.h +++ b/dev/kernel/FirmwareKit/GPT.h @@ -20,29 +20,29 @@ struct GPT_PARTITION_TABLE; struct GPT_PARTITION_ENTRY; struct PACKED GPT_PARTITION_TABLE final { - Char Signature[kMagicLenGPT]; - UInt32 Revision; - UInt32 HeaderSize; - UInt32 CRC32; - UInt32 Reserved1; - UInt64 LBAHeader; - UInt64 LBAAltHeader; - UInt64 FirstGPTEntry; - UInt64 LastGPTEntry; + Char Signature[kMagicLenGPT]; + UInt32 Revision; + UInt32 HeaderSize; + UInt32 CRC32; + UInt32 Reserved1; + UInt64 LBAHeader; + UInt64 LBAAltHeader; + UInt64 FirstGPTEntry; + UInt64 LastGPTEntry; EFI_GUID Guid; - UInt64 StartingLBA; - UInt32 NumPartitionEntries; - UInt32 SizeOfEntries; - UInt32 CRC32PartEntry; - UInt8 Reserved2[kSectorAlignGPT_PartTbl]; + UInt64 StartingLBA; + UInt32 NumPartitionEntries; + UInt32 SizeOfEntries; + UInt32 CRC32PartEntry; + UInt8 Reserved2[kSectorAlignGPT_PartTbl]; }; struct PACKED GPT_PARTITION_ENTRY { EFI_GUID PartitionTypeGUID; EFI_GUID UniquePartitionGUID; - UInt64 StartLBA; - UInt64 EndLBA; - UInt64 Attributes; - UInt8 Name[kSectorAlignGPT_PartEntry]; + UInt64 StartLBA; + UInt64 EndLBA; + UInt64 Attributes; + UInt8 Name[kSectorAlignGPT_PartEntry]; }; } // namespace Kernel diff --git a/dev/kernel/GfxKit/FB.h b/dev/kernel/GfxKit/FB.h index e30f1800..14f0393c 100644 --- a/dev/kernel/GfxKit/FB.h +++ b/dev/kernel/GfxKit/FB.h @@ -32,11 +32,11 @@ struct PACKED FBDevicePacket final { /// @brief Framebuffer device interface. /// @details This class is used to send and receive data from the framebuffer device. -/// @note The class is derived from the IDeviceObject class. +/// @note The class is derived from the DeviceInterface class. class FBDeviceInterface NE_DEVICE<FBDevicePacket*> { public: - explicit FBDeviceInterface(void (*out)(IDeviceObject* self, FBDevicePacket* out), - void (*in)(IDeviceObject* self, FBDevicePacket* in)); + explicit FBDeviceInterface(void (*out)(DeviceInterface* self, FBDevicePacket* out), + void (*in)(DeviceInterface* self, FBDevicePacket* in)); virtual ~FBDeviceInterface() override; diff --git a/dev/kernel/HALKit/AMD64/HalApplicationProcessor.cc b/dev/kernel/HALKit/AMD64/HalApplicationProcessor.cc index 3e10d577..24a81af9 100644 --- a/dev/kernel/HALKit/AMD64/HalApplicationProcessor.cc +++ b/dev/kernel/HALKit/AMD64/HalApplicationProcessor.cc @@ -120,7 +120,9 @@ EXTERN_C BOOL mp_register_task(HAL::StackFramePtr stack_frame, ThreadID thrdid) HardwareThreadScheduler::The()[thrdid].Leak()->Busy(NO); - sched_jump_to_task(kHWThread[thrdid].mFramePtr); + if (!kSMPAware) { + sched_jump_to_task(kHWThread[thrdid].mFramePtr); + } return YES; } @@ -201,9 +203,9 @@ Void mp_init_cores(VoidPtr vendor_ptr) noexcept { volatile LAPIC* entry_struct = (volatile LAPIC*) entry_ptr; if (entry_struct->Flags & 0x1) { - kAPICLocales[kSMPCount] = entry_struct->ProcessorID; + kAPICLocales[kSMPCount] = entry_struct->ProcessorID; kHWThread[kSMPCount].mThreadID = kAPICLocales[kSMPCount]; - + ++kSMPCount; kout << "Kind: LAPIC: ON\r"; diff --git a/dev/kernel/HALKit/AMD64/HalDebugOutput.cc b/dev/kernel/HALKit/AMD64/HalDebugOutput.cc index b0463eb5..1e9fbab3 100644 --- a/dev/kernel/HALKit/AMD64/HalDebugOutput.cc +++ b/dev/kernel/HALKit/AMD64/HalDebugOutput.cc @@ -59,7 +59,7 @@ TerminalDevice::~TerminalDevice() = default; STATIC SizeT kX = kFontSizeX, kY = kFontSizeY; -EXTERN_C void ke_utf_io_write(IDeviceObject<const Utf8Char*>* obj, const Utf8Char* bytes) { +EXTERN_C void ke_utf_io_write(DeviceInterface<const Utf8Char*>* obj, const Utf8Char* bytes) { NE_UNUSED(bytes); NE_UNUSED(obj); @@ -115,7 +115,7 @@ EXTERN_C void ke_utf_io_write(IDeviceObject<const Utf8Char*>* obj, const Utf8Cha #endif // __DEBUG__ } -EXTERN_C void ke_io_write(IDeviceObject<const Char*>* obj, const Char* bytes) { +EXTERN_C void ke_io_write(DeviceInterface<const Char*>* obj, const Char* bytes) { NE_UNUSED(bytes); NE_UNUSED(obj); @@ -171,7 +171,7 @@ EXTERN_C void ke_io_write(IDeviceObject<const Char*>* obj, const Char* bytes) { #endif // __DEBUG__ } -EXTERN_C void ke_io_read(IDeviceObject<const Char*>*, const Char* bytes) { +EXTERN_C void ke_io_read(DeviceInterface<const Char*>*, const Char* bytes) { NE_UNUSED(bytes); #ifdef __DEBUG__ @@ -218,7 +218,7 @@ Utf8TerminalDevice::~Utf8TerminalDevice() = default; Utf8TerminalDevice Utf8TerminalDevice::The() noexcept { Utf8TerminalDevice out(Kernel::ke_utf_io_write, - [](IDeviceObject<const Utf8Char*>*, const Utf8Char*) -> Void {}); + [](DeviceInterface<const Utf8Char*>*, const Utf8Char*) -> Void {}); return out; } diff --git a/dev/kernel/HALKit/AMD64/HalSchedulerCorePrimitives.cc b/dev/kernel/HALKit/AMD64/HalSchedulerCorePrimitives.cc index 0c468e14..1dbce4ac 100644 --- a/dev/kernel/HALKit/AMD64/HalSchedulerCorePrimitives.cc +++ b/dev/kernel/HALKit/AMD64/HalSchedulerCorePrimitives.cc @@ -22,7 +22,7 @@ EXTERN_C Void __zka_pure_call(USER_PROCESS* process) { /// @param stack_ptr the frame pointer. /***********************************************************************************/ -EXTERN_C Bool hal_check_stack(HAL::StackFramePtr stack_ptr) { +EXTERN_C Bool hal_check_task(HAL::StackFramePtr stack_ptr) { if (!stack_ptr) return No; return stack_ptr->SP != 0 && stack_ptr->IP != 0; diff --git a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc index b30bfc32..cd41480a 100644 --- a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc @@ -546,7 +546,8 @@ namespace Detail { /// @brief Read AHCI device. /// @param self device /// @param mnt mounted disk. - STATIC Void sk_io_read_ahci(IDeviceObject<MountpointInterface*>* self, MountpointInterface* mnt) { + STATIC Void sk_io_read_ahci(DeviceInterface<MountpointInterface*>* self, + MountpointInterface* mnt) { AHCIDeviceInterface* dev = (AHCIDeviceInterface*) self; err_global_get() = kErrorDisk; @@ -567,8 +568,8 @@ namespace Detail { /// @brief Write AHCI device. /// @param self device /// @param mnt mounted disk. - STATIC Void sk_io_write_ahci(IDeviceObject<MountpointInterface*>* self, - MountpointInterface* mnt) { + STATIC Void sk_io_write_ahci(DeviceInterface<MountpointInterface*>* self, + MountpointInterface* mnt) { AHCIDeviceInterface* dev = (AHCIDeviceInterface*) self; err_global_get() = kErrorDisk; diff --git a/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc index 99e1c619..6fccbdfa 100644 --- a/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc @@ -200,7 +200,8 @@ namespace Detail { /// @brief Read PIO device. /// @param self device /// @param mnt mounted disk. - STATIC Void sk_io_read_pio(IDeviceObject<MountpointInterface*>* self, MountpointInterface* mnt) { + STATIC Void sk_io_read_pio(DeviceInterface<MountpointInterface*>* self, + MountpointInterface* mnt) { ATADeviceInterface* dev = (ATADeviceInterface*) self; err_global_get() = kErrorDisk; @@ -221,7 +222,8 @@ namespace Detail { /// @brief Write PIO device. /// @param self device /// @param mnt mounted disk. - STATIC Void sk_io_write_pio(IDeviceObject<MountpointInterface*>* self, MountpointInterface* mnt) { + STATIC Void sk_io_write_pio(DeviceInterface<MountpointInterface*>* self, + MountpointInterface* mnt) { ATADeviceInterface* dev = (ATADeviceInterface*) self; err_global_get() = kErrorDisk; diff --git a/dev/kernel/HALKit/ARM64/HalCommonAPI.s b/dev/kernel/HALKit/ARM64/HalCommonAPI.s index e76b6e3f..f0c69368 100644 --- a/dev/kernel/HALKit/ARM64/HalCommonAPI.s +++ b/dev/kernel/HALKit/ARM64/HalCommonAPI.s @@ -5,5 +5,5 @@ .global hal_flush_tlb hal_flush_tlb: - tlbi + tlbi vmalle1 ret diff --git a/dev/kernel/HALKit/ARM64/HalDebugOutput.cc b/dev/kernel/HALKit/ARM64/HalDebugOutput.cc index 3c518e2f..64004ac3 100644 --- a/dev/kernel/HALKit/ARM64/HalDebugOutput.cc +++ b/dev/kernel/HALKit/ARM64/HalDebugOutput.cc @@ -10,7 +10,7 @@ #include <NeKit/Utils.h> namespace Kernel { -EXTERN_C void ke_io_write(IDeviceObject<const Char*>* self, const Char* bytes) { +EXTERN_C void ke_io_write(DeviceInterface<const Char*>* self, const Char* bytes) { #ifdef __DEBUG__ if (*bytes == 0) return; @@ -33,7 +33,7 @@ EXTERN_C void ke_io_write(IDeviceObject<const Char*>* self, const Char* bytes) { TerminalDevice::~TerminalDevice() = default; -EXTERN_C void ke_io_read(IDeviceObject<const Char*>* self, const Char* bytes) { +EXTERN_C void ke_io_read(DeviceInterface<const Char*>* self, const Char* bytes) { #ifdef __DEBUG__ SizeT index = 0; diff --git a/dev/kernel/HALKit/ARM64/HalKernelPanic.cc b/dev/kernel/HALKit/ARM64/HalKernelPanic.cc index 5feb6c75..d70fef00 100644 --- a/dev/kernel/HALKit/ARM64/HalKernelPanic.cc +++ b/dev/kernel/HALKit/ARM64/HalKernelPanic.cc @@ -33,7 +33,6 @@ Void ke_panic(const Kernel::Int32& id, const Char* message) { (Void)(kout << "*** STOP ***\r"); (Void)(kout << "Kernel_Panic_MSG: " << message << kendl); (Void)(kout << "Kernel_Panic_ID: " << hex_number(id) << kendl); - (Void)(kout << "Kernel_Panic_CR2: " << hex_number((UIntPtr) hal_read_cr2()) << kendl); RecoveryFactory::Recover(); } diff --git a/dev/kernel/HALKit/ARM64/HalSchedulerCorePrimitives.cc b/dev/kernel/HALKit/ARM64/HalSchedulerCorePrimitives.cc index ee286639..10f95e29 100644 --- a/dev/kernel/HALKit/ARM64/HalSchedulerCorePrimitives.cc +++ b/dev/kernel/HALKit/ARM64/HalSchedulerCorePrimitives.cc @@ -22,7 +22,7 @@ EXTERN_C Void __zka_pure_call(USER_PROCESS* process) { /// @param stack_ptr the frame pointer.
/***********************************************************************************/
-EXTERN_C Bool hal_check_stack(HAL::StackFramePtr stack_ptr) {
+EXTERN_C Bool hal_check_task(HAL::StackFramePtr stack_ptr) {
if (!stack_ptr) return No;
return stack_ptr->SP != 0 && stack_ptr->IP != 0;
diff --git a/dev/kernel/KernelKit/CoreProcessScheduler.h b/dev/kernel/KernelKit/CoreProcessScheduler.h index 5ba29d3a..c06ef92c 100644 --- a/dev/kernel/KernelKit/CoreProcessScheduler.h +++ b/dev/kernel/KernelKit/CoreProcessScheduler.h @@ -130,7 +130,7 @@ enum class ProcessStatusKind : Int32 { //! @brief Affinity is the amount of nano-seconds this process is going to run. /***********************************************************************************/ enum class AffinityKind : Int32 { - kRealTime = 50, + kRealTime = 100, kVeryHigh = 150, kHigh = 200, kStandard = 1000, @@ -208,7 +208,7 @@ struct PROCESS_IMAGE final { Bool HasImage() const { return this->fBlob != nullptr; } - ErrorOr<ImagePtr> Leak() { + ErrorOr<ImagePtr> LeakImage() { if (this->fCode) { return ErrorOr<ImagePtr>{this->fCode}; } diff --git a/dev/kernel/KernelKit/DebugOutput.h b/dev/kernel/KernelKit/DebugOutput.h index 9598f590..de3bc997 100644 --- a/dev/kernel/KernelKit/DebugOutput.h +++ b/dev/kernel/KernelKit/DebugOutput.h @@ -37,9 +37,9 @@ inline TerminalDevice hex_number(const Long& x); // @brief Emulates a VT100 terminal. class TerminalDevice final NE_DEVICE<const Char*> { public: - TerminalDevice(void (*print)(IDeviceObject*, const Char*), - void (*gets)(IDeviceObject*, const Char*)) - : IDeviceObject<const Char*>(print, gets) {} + TerminalDevice(void (*print)(DeviceInterface*, const Char*), + void (*gets)(DeviceInterface*, const Char*)) + : DeviceInterface<const Char*>(print, gets) {} ~TerminalDevice() override; @@ -54,9 +54,9 @@ class TerminalDevice final NE_DEVICE<const Char*> { class Utf8TerminalDevice final NE_DEVICE<const Utf8Char*> { public: - Utf8TerminalDevice(void (*print)(IDeviceObject*, const Utf8Char*), - void (*gets)(IDeviceObject*, const Utf8Char*)) - : IDeviceObject<const Utf8Char*>(print, gets) {} + Utf8TerminalDevice(void (*print)(DeviceInterface*, const Utf8Char*), + void (*gets)(DeviceInterface*, const Utf8Char*)) + : DeviceInterface<const Utf8Char*>(print, gets) {} ~Utf8TerminalDevice() override; diff --git a/dev/kernel/KernelKit/DeviceMgr.h b/dev/kernel/KernelKit/DeviceMgr.h index 7c7b9da3..c6467190 100644 --- a/dev/kernel/KernelKit/DeviceMgr.h +++ b/dev/kernel/KernelKit/DeviceMgr.h @@ -24,13 +24,13 @@ #define kDeviceMgrRootDirPath "/devices/" -#define NE_DEVICE : public ::Kernel::IDeviceObject +#define NE_DEVICE : public ::Kernel::DeviceInterface // Last Rev: Wed, May 27, 2025 6:22 PM namespace Kernel { template <typename T> -class IDeviceObject; +class DeviceInterface; template <typename T> class IOBuf; @@ -39,24 +39,26 @@ class IOBuf; /// @brief Device contract interface, represents an HW device. /***********************************************************************************/ template <typename T> -class IDeviceObject { +class DeviceInterface { public: - explicit IDeviceObject(void (*Out)(IDeviceObject<T>*, T), void (*In)(IDeviceObject<T>*, T)) + DeviceInterface() = default; + + explicit DeviceInterface(void (*Out)(DeviceInterface<T>*, T), void (*In)(DeviceInterface<T>*, T)) : fOut(Out), fIn(In) {} - virtual ~IDeviceObject() = default; + virtual ~DeviceInterface() = default; public: - IDeviceObject& operator=(const IDeviceObject<T>&) = default; - IDeviceObject(const IDeviceObject<T>&) = default; + DeviceInterface& operator=(const DeviceInterface<T>&) = default; + DeviceInterface(const DeviceInterface<T>&) = default; public: - virtual IDeviceObject<T>& operator<<(T Data) { + virtual DeviceInterface<T>& operator<<(T Data) { fOut(this, Data); return *this; } - virtual IDeviceObject<T>& operator>>(T Data) { + virtual DeviceInterface<T>& operator>>(T Data) { fIn(this, Data); return *this; } @@ -68,8 +70,8 @@ class IDeviceObject { Bool operator!() { return !fOut || !fIn; } protected: - Void (*fOut)(IDeviceObject<T>*, T Data) = {nullptr}; - Void (*fIn)(IDeviceObject<T>*, T Data) = {nullptr}; + Void (*fOut)(DeviceInterface<T>*, T Data) = {nullptr}; + Void (*fIn)(DeviceInterface<T>*, T Data) = {nullptr}; }; /// diff --git a/dev/kernel/KernelKit/UserMgr.h b/dev/kernel/KernelKit/UserMgr.h index 82f8ca66..ef1cc659 100644 --- a/dev/kernel/KernelKit/UserMgr.h +++ b/dev/kernel/KernelKit/UserMgr.h @@ -20,8 +20,8 @@ #include <NeKit/Defines.h> #include <NeKit/KString.h> -///! We got the Super, Standard (%s format) and Guest user, -///! all are used to make authorization operations on the OS. +///! We got the MGMT, STD (%s format) and GUEST users, +///! all are used to make authorized operations. #define kSuperUser "OS AUTHORITY/MGMT/%s" #define kGuestUser "OS AUTHORITY/GUEST/%s" #define kStdUser "OS AUTHORITY/STD/%s" @@ -34,11 +34,11 @@ namespace Kernel { class User; -enum class UserRingKind { +enum class UserRingKind : Int32 { kRingInvalid = 0, - kRingStdUser = 1, - kRingSuperUser = 2, - kRingGuestUser = 5, + kRingStdUser = 444, + kRingSuperUser = 666, + kRingGuestUser = 777, kRingCount = 3, }; diff --git a/dev/kernel/KernelKit/UserProcessScheduler.h b/dev/kernel/KernelKit/UserProcessScheduler.h index 14986ab6..7c994726 100644 --- a/dev/kernel/KernelKit/UserProcessScheduler.h +++ b/dev/kernel/KernelKit/UserProcessScheduler.h @@ -125,7 +125,7 @@ class USER_PROCESS final { /***********************************************************************************/ //! @brief Gets the local exit code. /***********************************************************************************/ - const UInt32& GetExitCode() noexcept; + KPCError& GetExitCode() noexcept; /***********************************************************************************/ ///! @brief Get the process's name @@ -137,21 +137,22 @@ class USER_PROCESS final { //! @brief return local error code of process. //! @return Int32 local error code. /***********************************************************************************/ - Int32& GetLocalCode() noexcept; + KPCError& GetLocalCode() noexcept; const User* GetOwner() noexcept; const ProcessStatusKind& GetStatus() noexcept; const AffinityKind& GetAffinity() noexcept; private: - UInt32 fLastExitCode{0}; - Int32 fLocalCode{0}; + KPCError LastExitCode{0}; + KPCError LocalCode{0}; friend UserProcessScheduler; friend UserProcessHelper; }; typedef Array<USER_PROCESS, kSchedProcessLimitPerTeam> USER_PROCESS_ARRAY; +typedef Ref<USER_PROCESS> USER_PROCESS_REF; /// \brief Processs Team (contains multiple processes inside it.) /// Equivalent to a process batch @@ -168,9 +169,9 @@ class UserProcessTeam final { public: USER_PROCESS_ARRAY mProcessList; - Ref<USER_PROCESS> mCurrentProcess; + USER_PROCESS_REF mCurrentProcess; ProcessID mTeamId{0}; - ProcessID mProcessCount{0}; + ProcessID mProcessCur{0}; }; /***********************************************************************************/ @@ -204,8 +205,8 @@ class UserProcessScheduler final : public ISchedulable { Bool HasMP() override; public: - Ref<USER_PROCESS>& TheCurrentProcess(); - SizeT Run() noexcept; + USER_PROCESS_REF& TheCurrentProcess(); + SizeT Run() noexcept; public: STATIC UserProcessScheduler& The(); diff --git a/dev/kernel/NeKit/ErrorOr.h b/dev/kernel/NeKit/ErrorOr.h index d7751b7e..b653e0ee 100644 --- a/dev/kernel/NeKit/ErrorOr.h +++ b/dev/kernel/NeKit/ErrorOr.h @@ -18,13 +18,13 @@ using ErrorT = UInt; template <typename T> class ErrorOr final { public: - ErrorOr() = default; - ~ErrorOr() = default; + explicit ErrorOr() = default; + ~ErrorOr() = default; public: - explicit ErrorOr(Int32 err) : mId(err) {} + explicit ErrorOr(Int32 err) : mRef((T*) RTL_ALLOCA(sizeof(T))), mId(err) {} - explicit ErrorOr(nullPtr Null) {} + explicit ErrorOr(nullPtr) {} explicit ErrorOr(T* Class) : mRef(Class) {} @@ -48,7 +48,7 @@ class ErrorOr final { private: Ref<T> mRef; - UInt32 mId{0}; + Int32 mId{0}; }; using ErrorOrAny = ErrorOr<voidPtr>; diff --git a/dev/kernel/NeKit/Ref.h b/dev/kernel/NeKit/Ref.h index 9920aa6f..4d343bc5 100644 --- a/dev/kernel/NeKit/Ref.h +++ b/dev/kernel/NeKit/Ref.h @@ -8,6 +8,7 @@ #ifndef _NEWKIT_REF_H_ #define _NEWKIT_REF_H_ +#include <CompilerKit/CompilerKit.h> #include <KernelKit/HeapMgr.h> #include <NeKit/Defines.h> #include <NeKit/KernelPanic.h> @@ -16,41 +17,33 @@ namespace Kernel { template <typename T> class Ref final { public: - Ref() = default; - - ~Ref() { - if (mm_is_valid_ptr(fClass)) delete fClass; - } + explicit Ref() = default; + ~Ref() = default; public: - Ref(T* cls) : fClass(cls) {} - - Ref(T cls) : fClass(nullptr) { fClass = &cls; } + Ref(T* cls) : fClass(*cls) {} + Ref(T cls) : fClass(cls) {} Ref& operator=(T ref) { - fClass = &ref; + fClass = ref; return *this; } + NE_COPY_DEFAULT(Ref); + public: - T operator->() const { - MUST_PASS(*fClass); - return *fClass; - } + T operator->() const { return fClass; } - T& Leak() noexcept { return *fClass; } + T& Leak() noexcept { return fClass; } - T& TryLeak() const noexcept { - MUST_PASS(*fClass); - return *fClass; - } + T& TryLeak() const noexcept { return fClass; } - T operator*() { return *fClass; } + T operator*() { return fClass; } - operator bool() noexcept { return fClass; } + operator bool() noexcept { return true; } private: - T* fClass{nullptr}; + T fClass; }; template <typename T> @@ -70,7 +63,7 @@ class NonNullRef final { NonNullRef(const NonNullRef<T>& ref) = default; private: - Ref<T> fRef{nullptr}; + Ref<T> fRef{}; }; } // namespace Kernel diff --git a/dev/kernel/NetworkKit/NetworkDevice.h b/dev/kernel/NetworkKit/NetworkDevice.h index c37d8504..04d208c5 100644 --- a/dev/kernel/NetworkKit/NetworkDevice.h +++ b/dev/kernel/NetworkKit/NetworkDevice.h @@ -21,8 +21,8 @@ class NetworkDevice; */ class NetworkDevice final NE_DEVICE<NetworkDeviceCommand> { public: - NetworkDevice(void (*out)(IDeviceObject<NetworkDeviceCommand>*, NetworkDeviceCommand), - void (*in)(IDeviceObject<NetworkDeviceCommand>*, NetworkDeviceCommand), + NetworkDevice(void (*out)(DeviceInterface<NetworkDeviceCommand>*, NetworkDeviceCommand), + void (*in)(DeviceInterface<NetworkDeviceCommand>*, NetworkDeviceCommand), void (*cleanup)(void) = nullptr); ~NetworkDevice() override; diff --git a/dev/kernel/NetworkKit/NetworkDevice.inl b/dev/kernel/NetworkKit/NetworkDevice.inl index 84ec9edc..6ef480a9 100644 --- a/dev/kernel/NetworkKit/NetworkDevice.inl +++ b/dev/kernel/NetworkKit/NetworkDevice.inl @@ -13,12 +13,12 @@ #endif // __INC_NETWORK_DEVICE_H__ namespace Kernel { -inline NetworkDevice::NetworkDevice(void (*out)(IDeviceObject<NetworkDeviceCommand>*, +inline NetworkDevice::NetworkDevice(void (*out)(DeviceInterface<NetworkDeviceCommand>*, NetworkDeviceCommand), - void (*in)(IDeviceObject<NetworkDeviceCommand>*, + void (*in)(DeviceInterface<NetworkDeviceCommand>*, NetworkDeviceCommand), void (*on_cleanup)(void)) - : IDeviceObject<NetworkDeviceCommand>(out, in), fCleanup(on_cleanup) { + : DeviceInterface<NetworkDeviceCommand>(out, in), fCleanup(on_cleanup) { kout << "NetworkDevice initialized.\r"; MUST_PASS(out && in && on_cleanup); diff --git a/dev/kernel/StorageKit/AHCI.h b/dev/kernel/StorageKit/AHCI.h index e2220719..3b9dac67 100644 --- a/dev/kernel/StorageKit/AHCI.h +++ b/dev/kernel/StorageKit/AHCI.h @@ -13,11 +13,11 @@ namespace Kernel { /// @brief AHCIDeviceInterface class /// @details This class is used to send and receive data from the AHCI device. -/// @note The class is derived from the IDeviceObject class. +/// @note The class is derived from the DeviceInterface class. class AHCIDeviceInterface NE_DEVICE<MountpointInterface*> { public: - explicit AHCIDeviceInterface(void (*out)(IDeviceObject* self, MountpointInterface* out), - void (*in)(IDeviceObject* self, MountpointInterface* in)); + explicit AHCIDeviceInterface(void (*out)(DeviceInterface* self, MountpointInterface* out), + void (*in)(DeviceInterface* self, MountpointInterface* in)); virtual ~AHCIDeviceInterface() override; diff --git a/dev/kernel/StorageKit/ATA.h b/dev/kernel/StorageKit/ATA.h index abf255a5..d4c894a3 100644 --- a/dev/kernel/StorageKit/ATA.h +++ b/dev/kernel/StorageKit/ATA.h @@ -13,10 +13,10 @@ namespace Kernel { /// @brief ATA device interface class. -class ATADeviceInterface : public IDeviceObject<MountpointInterface*> { +class ATADeviceInterface : public DeviceInterface<MountpointInterface*> { public: - explicit ATADeviceInterface(void (*Out)(IDeviceObject*, MountpointInterface* outpacket), - void (*In)(IDeviceObject*, MountpointInterface* inpacket)); + explicit ATADeviceInterface(void (*Out)(DeviceInterface*, MountpointInterface* outpacket), + void (*In)(DeviceInterface*, MountpointInterface* inpacket)); virtual ~ATADeviceInterface(); diff --git a/dev/kernel/StorageKit/NVME.h b/dev/kernel/StorageKit/NVME.h index aae36a94..1b2b6358 100644 --- a/dev/kernel/StorageKit/NVME.h +++ b/dev/kernel/StorageKit/NVME.h @@ -12,8 +12,8 @@ namespace Kernel { class NVMEDeviceInterface final NE_DEVICE<MountpointInterface*> { public: - explicit NVMEDeviceInterface(Void (*out)(IDeviceObject*, MountpointInterface* out_packet), - Void (*in)(IDeviceObject*, MountpointInterface* in_packet), + explicit NVMEDeviceInterface(Void (*out)(DeviceInterface*, MountpointInterface* out_packet), + Void (*in)(DeviceInterface*, MountpointInterface* in_packet), Void (*cleanup)(Void)); ~NVMEDeviceInterface() override; diff --git a/dev/kernel/StorageKit/StorageKit.h b/dev/kernel/StorageKit/StorageKit.h index adb9f5dc..411aa6ae 100644 --- a/dev/kernel/StorageKit/StorageKit.h +++ b/dev/kernel/StorageKit/StorageKit.h @@ -12,7 +12,7 @@ namespace Kernel { template <typename T> -class IDeviceObject; +class DeviceInterface; class NVMEDeviceInterface; class AHCIDeviceInterface; diff --git a/dev/kernel/src/CxxAbi-ARM64.cc b/dev/kernel/src/CxxAbi-ARM64.cc index 02f3dbcf..d351b024 100644 --- a/dev/kernel/src/CxxAbi-ARM64.cc +++ b/dev/kernel/src/CxxAbi-ARM64.cc @@ -19,7 +19,7 @@ Kernel::UIntPtr __dso_handle; EXTERN_C void __chkstk(void) {} -EXTERN_C int atexit(void (*f)(void*), void* arg, void* dso) { +EXTERN_C int atexit(void (*f)(), void* arg, void* dso) { if (__atexit_func_count >= kAtExitMacDestructors) return 1; __atexit_funcs[__atexit_func_count].destructor_func = f; @@ -36,7 +36,8 @@ EXTERN_C void __cxa_finalize(void* f) { if (!f) { while (i--) { if (__atexit_funcs[i].destructor_func) { - (*__atexit_funcs[i].destructor_func)(__atexit_funcs[i].obj_ptr); + (*__atexit_funcs[i].destructor_func)(); + __atexit_funcs[i].destructor_func = 0; }; } @@ -45,7 +46,7 @@ EXTERN_C void __cxa_finalize(void* f) { while (i--) { if (__atexit_funcs[i].destructor_func) { - (*__atexit_funcs[i].destructor_func)(__atexit_funcs[i].obj_ptr); + (*__atexit_funcs[i].destructor_func)(); __atexit_funcs[i].destructor_func = 0; }; } diff --git a/dev/kernel/src/Gfx/FBDeviceInterface.cc b/dev/kernel/src/Gfx/FBDeviceInterface.cc index d588e8c8..f1867b45 100644 --- a/dev/kernel/src/Gfx/FBDeviceInterface.cc +++ b/dev/kernel/src/Gfx/FBDeviceInterface.cc @@ -12,9 +12,9 @@ using namespace Kernel; /// @param Out Drive output /// @param In Drive input /// @param Cleanup Drive cleanup. -FBDeviceInterface::FBDeviceInterface(void (*out)(IDeviceObject* self, FBDevicePacket* outpacket), - void (*in)(IDeviceObject* self, FBDevicePacket* inpacket)) - : IDeviceObject(out, in) {} +FBDeviceInterface::FBDeviceInterface(void (*out)(DeviceInterface* self, FBDevicePacket* outpacket), + void (*in)(DeviceInterface* self, FBDevicePacket* inpacket)) + : DeviceInterface(out, in) {} /// @brief Class desctructor FBDeviceInterface::~FBDeviceInterface() = default; diff --git a/dev/kernel/src/HardwareThreadScheduler.cc b/dev/kernel/src/HardwareThreadScheduler.cc index 78bad9d6..86f46718 100644 --- a/dev/kernel/src/HardwareThreadScheduler.cc +++ b/dev/kernel/src/HardwareThreadScheduler.cc @@ -20,7 +20,7 @@ namespace Kernel { /// @note Those symbols are needed in order to switch and validate the stack. /***********************************************************************************/ -EXTERN_C Bool hal_check_stack(HAL::StackFramePtr frame); +EXTERN_C Bool hal_check_task(HAL::StackFramePtr frame); EXTERN_C Bool mp_register_task(HAL::StackFramePtr frame, ProcessID pid); STATIC HardwareThreadScheduler kHardwareThreadScheduler; @@ -96,7 +96,7 @@ Bool HardwareThread::Switch(HAL::StackFramePtr frame) { return NO; } - if (!hal_check_stack(frame)) { + if (!hal_check_task(frame)) { return NO; } @@ -145,7 +145,7 @@ HAL::StackFramePtr HardwareThreadScheduler::Leak() noexcept { */ /***********************************************************************************/ Ref<HardwareThread*> HardwareThreadScheduler::operator[](SizeT idx) { - if (idx >= kMaxAPInsideSched) { + if (idx > kMaxAPInsideSched) { HardwareThread* kFakeThread = nullptr; return {kFakeThread}; } diff --git a/dev/kernel/src/KString.cc b/dev/kernel/src/KString.cc index 9f332cdf..f5732280 100644 --- a/dev/kernel/src/KString.cc +++ b/dev/kernel/src/KString.cc @@ -64,7 +64,7 @@ bool KString::operator!=(const Char* rhs) const { } ErrorOr<KString> KStringBuilder::Construct(const Char* data) { - if (!data || *data == 0) return {}; + if (!data || *data == 0) return ErrorOr<KString>(new KString(0)); KString* view = new KString(rt_string_len(data)); (*view) += data; diff --git a/dev/kernel/src/Pmm.cc b/dev/kernel/src/Pmm.cc index b9fba20e..7f5050f9 100644 --- a/dev/kernel/src/Pmm.cc +++ b/dev/kernel/src/Pmm.cc @@ -35,7 +35,7 @@ Ref<PTEWrapper> Pmm::RequestPage(Boolean user, Boolean readWrite) { if (pt.fPresent) { kout << "[PMM]: Allocation failed.\r"; - return {}; + return {pt}; } return Ref<PTEWrapper>(pt); diff --git a/dev/kernel/src/Storage/AHCIDeviceInterface.cc b/dev/kernel/src/Storage/AHCIDeviceInterface.cc index 2d97eee7..39570665 100644 --- a/dev/kernel/src/Storage/AHCIDeviceInterface.cc +++ b/dev/kernel/src/Storage/AHCIDeviceInterface.cc @@ -12,11 +12,11 @@ using namespace Kernel; /// @param Out Drive output /// @param In Drive input /// @param Cleanup Drive cleanup. -AHCIDeviceInterface::AHCIDeviceInterface(void (*out)(IDeviceObject* self, +AHCIDeviceInterface::AHCIDeviceInterface(void (*out)(DeviceInterface* self, MountpointInterface* outpacket), - void (*in)(IDeviceObject* self, + void (*in)(DeviceInterface* self, MountpointInterface* inpacket)) - : IDeviceObject(out, in) {} + : DeviceInterface(out, in) {} /// @brief Class desctructor AHCIDeviceInterface::~AHCIDeviceInterface() = default; @@ -45,7 +45,7 @@ AHCIDeviceInterface& AHCIDeviceInterface::operator<<(MountpointInterface* mnt) { } } - return (AHCIDeviceInterface&) IDeviceObject<MountpointInterface*>::operator<<(mnt); + return (AHCIDeviceInterface&) DeviceInterface<MountpointInterface*>::operator<<(mnt); } /// @brief Input operator. @@ -67,7 +67,7 @@ AHCIDeviceInterface& AHCIDeviceInterface::operator>>(MountpointInterface* mnt) { } } - return (AHCIDeviceInterface&) IDeviceObject<MountpointInterface*>::operator>>(mnt); + return (AHCIDeviceInterface&) DeviceInterface<MountpointInterface*>::operator>>(mnt); } const UInt16& AHCIDeviceInterface::GetPortsImplemented() { diff --git a/dev/kernel/src/Storage/ATADeviceInterface.cc b/dev/kernel/src/Storage/ATADeviceInterface.cc index a66d812b..609837af 100644 --- a/dev/kernel/src/Storage/ATADeviceInterface.cc +++ b/dev/kernel/src/Storage/ATADeviceInterface.cc @@ -12,9 +12,10 @@ using namespace Kernel; /// @param Out Drive output /// @param In Drive input /// @param Cleanup Drive cleanup. -ATADeviceInterface::ATADeviceInterface(void (*Out)(IDeviceObject*, MountpointInterface* outpacket), - void (*In)(IDeviceObject*, MountpointInterface* inpacket)) - : IDeviceObject(Out, In) {} +ATADeviceInterface::ATADeviceInterface(void (*Out)(DeviceInterface*, + MountpointInterface* outpacket), + void (*In)(DeviceInterface*, MountpointInterface* inpacket)) + : DeviceInterface(Out, In) {} /// @brief Class desctructor ATADeviceInterface::~ATADeviceInterface() = default; @@ -43,7 +44,7 @@ ATADeviceInterface& ATADeviceInterface::operator<<(MountpointInterface* Data) { } } - return (ATADeviceInterface&) IDeviceObject<MountpointInterface*>::operator<<(Data); + return (ATADeviceInterface&) DeviceInterface<MountpointInterface*>::operator<<(Data); } /// @brief Input operator. @@ -65,7 +66,7 @@ ATADeviceInterface& ATADeviceInterface::operator>>(MountpointInterface* Data) { } } - return (ATADeviceInterface&) IDeviceObject<MountpointInterface*>::operator>>(Data); + return (ATADeviceInterface&) DeviceInterface<MountpointInterface*>::operator>>(Data); } const UInt32& ATADeviceInterface::GetIndex() { diff --git a/dev/kernel/src/Storage/NVMEDeviceInterface.cc b/dev/kernel/src/Storage/NVMEDeviceInterface.cc index cff776c9..077595cf 100644 --- a/dev/kernel/src/Storage/NVMEDeviceInterface.cc +++ b/dev/kernel/src/Storage/NVMEDeviceInterface.cc @@ -7,11 +7,10 @@ #include <StorageKit/NVME.h> namespace Kernel { -NVMEDeviceInterface::NVMEDeviceInterface(void (*out)(IDeviceObject*, - MountpointInterface* outpacket), - void (*in)(IDeviceObject*, MountpointInterface* inpacket), - void (*cleanup)(void)) - : IDeviceObject(out, in), fCleanup(cleanup) {} +NVMEDeviceInterface::NVMEDeviceInterface( + void (*out)(DeviceInterface*, MountpointInterface* outpacket), + void (*in)(DeviceInterface*, MountpointInterface* inpacket), void (*cleanup)(void)) + : DeviceInterface(out, in), fCleanup(cleanup) {} NVMEDeviceInterface::~NVMEDeviceInterface() { if (fCleanup) fCleanup(); diff --git a/dev/kernel/src/UserMgr.cc b/dev/kernel/src/UserMgr.cc index c41b445b..8eade85e 100644 --- a/dev/kernel/src/UserMgr.cc +++ b/dev/kernel/src/UserMgr.cc @@ -4,8 +4,8 @@ * NeKernel * Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. * - * File: User.cc - * Purpose: User class, used to provide authentication and security. + * File: UserMgr.cc + * Purpose: User Manager, used to provide authentication and security. * * ======================================================== */ diff --git a/dev/kernel/src/UserProcessScheduler.cc b/dev/kernel/src/UserProcessScheduler.cc index 4bbd085a..e417cc8d 100644 --- a/dev/kernel/src/UserProcessScheduler.cc +++ b/dev/kernel/src/UserProcessScheduler.cc @@ -31,10 +31,6 @@ namespace Kernel { STATIC UInt32 kLastExitCode = 0U; -/***********************************************************************************/ -/// @brief Scheduler itself. -/***********************************************************************************/ - USER_PROCESS::USER_PROCESS() = default; USER_PROCESS::~USER_PROCESS() = default; @@ -71,16 +67,16 @@ USER_PROCESS::operator bool() { /// @return Int32 the last exit code. /***********************************************************************************/ -const UInt32& USER_PROCESS::GetExitCode() noexcept { - return this->fLastExitCode; +KPCError& USER_PROCESS::GetExitCode() noexcept { + return this->LastExitCode; } /***********************************************************************************/ /// @brief Error code variable getter. /***********************************************************************************/ -Int32& USER_PROCESS::GetLocalCode() noexcept { - return this->fLocalCode; +KPCError& USER_PROCESS::GetLocalCode() noexcept { + return this->LocalCode; } /***********************************************************************************/ @@ -262,12 +258,13 @@ STATIC Void sched_free_ptr_tree(PROCESS_HEAP_TREE<VoidPtr>* memory_ptr_list) { /***********************************************************************************/ Void USER_PROCESS::Exit(const Int32& exit_code) { - this->Status = exit_code > 0 ? ProcessStatusKind::kKilled : ProcessStatusKind::kFrozen; - this->fLastExitCode = exit_code; + this->Status = exit_code > 0 ? ProcessStatusKind::kKilled : ProcessStatusKind::kFrozen; + this->LastExitCode = exit_code; + this->UTime = 0; kLastExitCode = exit_code; - --this->ParentTeam->mProcessCount; + --this->ParentTeam->mProcessCur; auto memory_ptr_list = this->HeapTree; @@ -317,7 +314,7 @@ Void USER_PROCESS::Exit(const Int32& exit_code) { this->ProcessId = 0UL; this->Status = ProcessStatusKind::kFinished; - --this->ParentTeam->mProcessCount; + --this->ParentTeam->mProcessCur; } /***********************************************************************************/ @@ -363,13 +360,13 @@ ProcessID UserProcessScheduler::Spawn(const Char* name, VoidPtr code, VoidPtr im return -kErrorProcessFault; } - ProcessID pid = this->mTeam.mProcessCount; + ProcessID pid = this->mTeam.mProcessCur; if (pid > kSchedProcessLimitPerTeam) { return -kErrorProcessFault; } - ++this->mTeam.mProcessCount; + ++this->mTeam.mProcessCur; USER_PROCESS& process = this->mTeam.mProcessList[pid]; @@ -447,7 +444,7 @@ UserProcessScheduler& UserProcessScheduler::The() { /***********************************************************************************/ Void UserProcessScheduler::Remove(ProcessID process_id) { - if (process_id < 0 || process_id >= kSchedProcessLimitPerTeam) { + if (process_id < 0 || process_id > kSchedProcessLimitPerTeam) { return; } @@ -483,7 +480,7 @@ Bool UserProcessScheduler::HasMP() { /***********************************************************************************/ SizeT UserProcessScheduler::Run() noexcept { - if (mTeam.mProcessCount < 1) { + if (mTeam.mProcessCur < 1) { return 0UL; } @@ -507,6 +504,7 @@ SizeT UserProcessScheduler::Run() noexcept { if (UserProcessHelper::Switch(process.StackFrame, process.ProcessId)) { process.PTime = static_cast<Int32>(process.Affinity); + // We add a bigger cooldown according to the RTime and affinity here. if (process.PTime < process.RTime && AffinityKind::kRealTime != process.Affinity) { if (process.RTime < (Int32) AffinityKind::kVeryHigh) process.RTime = (Int32) AffinityKind::kLowUsage / 2; diff --git a/dev/kernel/src/UserProcessTeam.cc b/dev/kernel/src/UserProcessTeam.cc index 8ef9a013..dd21ac49 100644 --- a/dev/kernel/src/UserProcessTeam.cc +++ b/dev/kernel/src/UserProcessTeam.cc @@ -21,7 +21,7 @@ UserProcessTeam::UserProcessTeam() { this->mProcessList[i].ParentTeam = this; } - this->mProcessCount = 0UL; + this->mProcessCur = 0UL; } /***********************************************************************************/ diff --git a/dev/kernel/src/Utils.cc b/dev/kernel/src/Utils.cc index d7cc08af..087b6d5f 100644 --- a/dev/kernel/src/Utils.cc +++ b/dev/kernel/src/Utils.cc @@ -7,189 +7,188 @@ #include <NeKit/Utils.h> namespace Kernel { -Int32 rt_string_cmp(const Char* src, const Char* cmp, Size size) { - Int32 counter = 0; - - for (Size index = 0; index < size; ++index) { - if (src[index] != cmp[index]) ++counter; - } - return counter; -} +STATIC Int rt_copy_memory_safe(const voidPtr src, voidPtr dst, Size len, Size dst_size); +STATIC voidPtr rt_set_memory_safe(voidPtr dst, UInt32 value, Size len, Size dst_size); -Void rt_zero_memory(voidPtr pointer, Size len) { - rt_set_memory(pointer, 0, len); +Int32 rt_string_cmp(const Char* src, const Char* cmp, Size size) { + for (Size i = 0; i < size; ++i) { + if (src[i] != cmp[i]) + return static_cast<Int32>(src[i]) - static_cast<Int32>(cmp[i]); + } + return 0; } -SizeT rt_string_len(const Char* str, SizeT _len) { - SizeT len{0}; - - do { - if (len > _len) { - return _len; - } - +SizeT rt_string_len(const Char* str, SizeT max_len) { + SizeT len = 0; + while (len < max_len && str[len] != '\0') ++len; - } while (str[len] != '\0'); - return len; } Size rt_string_len(const Char* ptr) { - SizeT cnt{0}; - - while (ptr[cnt] != 0) ++cnt; - + Size cnt = 0; + while (ptr[cnt] != '\0') + ++cnt; return cnt; } -voidPtr rt_set_memory(voidPtr src, UInt32 value, Size len) { - UInt32* start = reinterpret_cast<UInt32*>(src); - - while (len) { - *start = value; - ++start; - --len; +const Char* rt_alloc_string(const Char* src) { + SizeT slen = rt_string_len(src); + Char* buffer = new Char[slen + 1]; + if (!buffer) return nullptr; + + if (rt_copy_memory_safe(reinterpret_cast<voidPtr>(const_cast<Char*>(src)), + reinterpret_cast<voidPtr>(buffer), + slen, + slen + 1) < 0) { + delete[] buffer; + return nullptr; } - return (voidPtr) start; + buffer[slen] = '\0'; + return buffer; } -Int rt_move_memory(const voidPtr src, voidPtr dst, Size len) { - Char* srcChr = reinterpret_cast<Char*>(src); - Char* dstChar = reinterpret_cast<Char*>(dst); - SizeT index = 0; - - while (index < len) { - dstChar[index] = srcChr[index]; - srcChr[index] = 0; - - ++index; +STATIC Int rt_copy_memory_safe(const voidPtr src, voidPtr dst, Size len, Size dst_size) { + if (!src || !dst || len > dst_size) { + if (dst && dst_size) { + rt_set_memory_safe(dst, 0, dst_size, dst_size); + } + return -1; } - - return 0; + auto s = reinterpret_cast<const UInt8*>(src); + auto d = reinterpret_cast<UInt8*>(dst); + for (Size i = 0; i < len; ++i) + d[i] = s[i]; + return static_cast<Int>(len); +} + +STATIC voidPtr rt_set_memory_safe(voidPtr dst, UInt32 value, Size len, Size dst_size) { + if (!dst || len > dst_size) return nullptr; + auto p = reinterpret_cast<UInt8*>(dst); + UInt8 v = static_cast<UInt8>(value & 0xFF); + for (Size i = 0; i < len; ++i) + p[i] = v; + return dst; } -Int rt_copy_memory(const voidPtr src, voidPtr dst, Size len) { - char* srcChr = reinterpret_cast<char*>(src); - char* dstChar = reinterpret_cast<char*>(dst); - Size index = 0; - - while (index < len) { - dstChar[index] = srcChr[index]; - ++index; - } - - dstChar[index] = 0; - - return index; +Void rt_zero_memory(voidPtr pointer, Size len) { + rt_set_memory_safe(pointer, 0, len, len); } -const Char* rt_alloc_string(const Char* src) { - const Char* string = new Char[rt_string_len(src) + 1]; - - if (!string) return nullptr; - - voidPtr v_src = reinterpret_cast<voidPtr>(const_cast<char*>(src)); - voidPtr v_dst = reinterpret_cast<voidPtr>(const_cast<char*>(string)); - - rt_copy_memory(v_src, v_dst, rt_string_len(src) + 1); - - return string; +#ifdef __NE_ENFORCE_DEPRECATED_WARNINGS +[[deprecated("Use rt_set_memory_safe instead")]] +#endif +voidPtr rt_set_memory(voidPtr src, UInt32 value, Size len) { + if (!src) return nullptr; + auto p = reinterpret_cast<UInt8*>(src); + UInt8 v = static_cast<UInt8>(value & 0xFF); + for (Size i = 0; i < len; ++i) + p[i] = v; + return src; } -Int32 rt_to_uppercase(Int32 character) { - if (character >= 'a' && character <= 'z') return character - 0x20; +#ifdef __NE_ENFORCE_DEPRECATED_WARNINGS +[[deprecated("Use rt_copy_memory_safe instead")]] +#endif +Int rt_copy_memory(const voidPtr src, voidPtr dst, Size len) { + if (!src || !dst) return -1; + auto s = reinterpret_cast<const UInt8*>(src); + auto d = reinterpret_cast<UInt8*>(dst); - return character; -} + for (Size i = 0; i < len; ++i) + d[i] = s[i]; -Int32 rt_is_alnum(Int32 character) { - return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || - (character >= '0' && character <= '9'); + return static_cast<Int>(len); } -Int32 rt_to_lower(Int32 character) { - if (character >= 'A' && character <= 'Z') return character + 0x20; - return character; +Int32 rt_to_uppercase(Int32 ch) { + return (ch >= 'a' && ch <= 'z') ? ch - 0x20 : ch; } -Boolean rt_is_space(Char chr) { - return chr == ' '; +Int32 rt_to_lower(Int32 ch) { + return (ch >= 'A' && ch <= 'Z') ? ch + 0x20 : ch; } -Boolean rt_is_newln(Char chr) { - return chr == '\n'; +Int32 rt_is_alnum(Int32 ch) { + return (ch >= 'a' && ch <= 'z') || + (ch >= 'A' && ch <= 'Z') || + (ch >= '0' && ch <= '9'); } -VoidPtr rt_string_in_string(const Char* in, const Char* needle) { - for (SizeT i = 0; i < rt_string_len(in); ++i) { - if (rt_string_cmp(in + i, needle, rt_string_len(needle)) == 0) - return reinterpret_cast<voidPtr>(const_cast<char*>(in + i)); - } +Boolean rt_is_space(Char ch) { + return ch == ' '; +} - return nullptr; +Boolean rt_is_newln(Char ch) { + return ch == '\n'; } -Char rt_to_char(UInt64 base, Int32 limit) { - Char kNumbers[17] = "0123456789ABCDEF"; - return kNumbers[base % limit]; +Char rt_to_char(UInt64 value, Int32 base) { + static constexpr Char kDigits[] = "0123456789ABCDEF"; + return kDigits[value % base]; } -Bool rt_to_string(Char* str, UInt64 base, Int32 limit) { +Bool rt_to_string(Char* str, UInt64 value, Int32 base) { #ifdef __NE_AMD64__ - auto i = 0; - - auto final_number = base; - - auto mult = 1; - auto elems = 0L; - - base /= 10; - - while (base > 0) { - elems++; - mult *= 10; - base /= 10; - } - - while (elems > -1) { - final_number = (final_number % mult) * 10 + final_number / mult; - str[i] = rt_to_char(final_number, limit); - - --elems; - ++i; + Int i = 0; + do { + str[i++] = rt_to_char(value, base); + value /= base; + } while (value); + str[i] = '\0'; + // in-place + for (Int j = 0; j < i / 2; ++j) { + Char tmp = str[j]; + str[j] = str[i - j - 1]; + str[i - j - 1] = tmp; } #endif - - return YES; + return true; } -/// @brief Checks for a string start at the character. -Char* rt_string_has_char(Char* str, Char chr) { - while (*str != chr) { - ++str; +VoidPtr rt_string_in_string(const Char* haystack, const Char* needle) { + SizeT needle_len = rt_string_len(needle); + SizeT hay_len = rt_string_len(haystack); - if (*str == 0) return nullptr; + if (needle_len > hay_len) return nullptr; + for (SizeT i = 0; i <= hay_len - needle_len; ++i) { + if (rt_string_cmp(haystack + i, needle, needle_len) == 0) { + return reinterpret_cast<voidPtr>(const_cast<Char*>(haystack + i)); + } } + return nullptr; +} - return str; +Char* rt_string_has_char(Char* str, Char ch) { + while (*str && *str != ch) ++str; + return (*str == ch) ? str : nullptr; } -} // namespace Kernel -////// @note These symbols were written to satisfy gcc, clang and other compiler complaints. +Int32 rt_strcmp(const Char* a, const Char* b) { + Size i = 0; + while (a[i] != '\0' && b[i] != '\0' && a[i] == b[i]) { + ++i; + } + return static_cast<Int32>(static_cast<UInt8>(a[i]) - + static_cast<UInt8>(b[i])); +} -EXTERN_C void* memset(void* dst, int c, long long unsigned int len) { - return Kernel::rt_set_memory(dst, c, len); + // @uses the deprecated version callers should ensure 'len' is valid. +extern "C" void* memset(void* dst, int c, long long unsigned int len) { + return Kernel::rt_set_memory(dst, c, static_cast<Size>(len)); } -EXTERN_C void* memcpy(void* dst, const void* src, long long unsigned int len) { - Kernel::rt_copy_memory(const_cast<void*>(src), dst, len); +extern "C" void* memcpy(void* dst, const void* src, long long unsigned int len) { + Kernel::rt_copy_memory(const_cast<void*>(src), dst, static_cast<Size>(len)); return dst; } -EXTERN_C Kernel::Int32 strcmp(const char* dst, const char* src) { - return Kernel::rt_string_cmp(dst, src, Kernel::rt_string_len(dst)); +extern "C" Kernel::Int32 strcmp(const char* a, const char* b) { + return Kernel::rt_strcmp(a, b); } + +} diff --git a/dev/libSystem/AsmProc.h b/dev/libSystem/AsmProc.h deleted file mode 100644 index b707d533..00000000 --- a/dev/libSystem/AsmProc.h +++ /dev/null @@ -1,15 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. - -------------------------------------------- */ - -#pragma once - -#include <libSystem/System.h> -#include <cstdarg> - -IMPORT_C VoidPtr sci_syscall_arg_1(SizeT id); -IMPORT_C VoidPtr sci_syscall_arg_2(SizeT id, VoidPtr arg1); -IMPORT_C VoidPtr sci_syscall_arg_3(SizeT id, VoidPtr arg1, VoidPtr arg3); -IMPORT_C VoidPtr sci_syscall_arg_4(SizeT id, VoidPtr arg1, VoidPtr arg3, VoidPtr arg4); diff --git a/dev/libSystem/SecurityPolicy.h b/dev/libSystem/SecurityPolicy.h deleted file mode 100644 index 812f52e2..00000000 --- a/dev/libSystem/SecurityPolicy.h +++ /dev/null @@ -1,12 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. - -------------------------------------------- */ - -#pragma once - -#include <libSystem/System.h> - -/// @file SecurityPolicy.h -/// @brief Hardened Security Policy, used to restrict access to certain system calls.
\ No newline at end of file diff --git a/dev/libSystem/Codes.h b/dev/libSystem/SystemKit/Err.h index 0451df64..c0a2282b 100644 --- a/dev/libSystem/Codes.h +++ b/dev/libSystem/SystemKit/Err.h @@ -6,9 +6,9 @@ #pragma once -#include <libSystem/Macros.h> +#include <libSystem/SystemKit/Macros.h> -/// @file Codes.h +/// @file Err.h /// @brief Process Codes type and values. /// @author Amlal El Mahrouss (amlal@nekernel.org) diff --git a/dev/libSystem/SystemKit/Jail.h b/dev/libSystem/SystemKit/Jail.h new file mode 100644 index 00000000..998173f9 --- /dev/null +++ b/dev/libSystem/SystemKit/Jail.h @@ -0,0 +1,22 @@ +/* ------------------------------------------- + + Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include <libSystem/SystemKit/System.h> + +/// @file Jail.h +/// @brief NeKernel Jail System + +struct JAIL_INFO; +struct JAIL; + +/// @brief Jail information (client side struct) +struct JAIL_INFO { + SInt32 fParentID; + SInt32 fJailHash; + SInt64 fACL; +};
\ No newline at end of file diff --git a/dev/libSystem/Macros.h b/dev/libSystem/SystemKit/Macros.h index 52dc904c..2bdeff9d 100644 --- a/dev/libSystem/Macros.h +++ b/dev/libSystem/SystemKit/Macros.h @@ -64,26 +64,24 @@ typedef char Char; typedef decltype(nullptr) nullPtr; typedef nullPtr NullPtr; -#define SCI_COPY_DELETE(KLASS) \ +#define LIBSYS_COPY_DELETE(KLASS) \ KLASS& operator=(const KLASS&) = delete; \ KLASS(const KLASS&) = delete; -#define SCI_COPY_DEFAULT(KLASS) \ +#define LIBSYS_COPY_DEFAULT(KLASS) \ KLASS& operator=(const KLASS&) = default; \ KLASS(const KLASS&) = default; -#define SCI_MOVE_DELETE(KLASS) \ +#define LIBSYS_MOVE_DELETE(KLASS) \ KLASS& operator=(KLASS&&) = delete; \ KLASS(KLASS&&) = delete; -#define SCI_MOVE_DEFAULT(KLASS) \ +#define LIBSYS_MOVE_DEFAULT(KLASS) \ KLASS& operator=(KLASS&&) = default; \ KLASS(KLASS&&) = default; #endif -IMPORT_C void _rtl_assert(Bool expr, const Char* origin); - #define MUST_PASS(X) _rtl_assert(X, __FILE__) #ifndef ARRAY_SIZE @@ -123,4 +121,6 @@ IMPORT_C void _rtl_assert(Bool expr, const Char* origin); #define tib_cast(X) ((UInt64) gib_cast(X) * 1024) #endif -#define SCI_UNUSED(X) ((void) X) +#define LIBSYS_UNUSED(X) ((void) X) + +IMPORT_C void _rtl_assert(Bool expr, const Char* origin); diff --git a/dev/libSystem/SystemKit/Syscall.h b/dev/libSystem/SystemKit/Syscall.h new file mode 100644 index 00000000..436665ae --- /dev/null +++ b/dev/libSystem/SystemKit/Syscall.h @@ -0,0 +1,19 @@ +/* ------------------------------------------- + + Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include <libSystem/SystemKit/System.h> +#include <cstdarg> + +#ifndef SYSCALL_HASH +#define SYSCALL_HASH(str) (UInt64) str +#endif // !SYSCALL_HASH + +IMPORT_C VoidPtr libsys_syscall_arg_1(SizeT id); +IMPORT_C VoidPtr libsys_syscall_arg_2(SizeT id, VoidPtr arg1); +IMPORT_C VoidPtr libsys_syscall_arg_3(SizeT id, VoidPtr arg1, VoidPtr arg3); +IMPORT_C VoidPtr libsys_syscall_arg_4(SizeT id, VoidPtr arg1, VoidPtr arg3, VoidPtr arg4); diff --git a/dev/libSystem/System.h b/dev/libSystem/SystemKit/System.h index 920ea2b2..91899efe 100644 --- a/dev/libSystem/System.h +++ b/dev/libSystem/SystemKit/System.h @@ -7,21 +7,21 @@ Purpose: System Call Interface. ------------------------------------------- */
-#ifndef SCI_SYSTEM_CALLS_H
-#define SCI_SYSTEM_CALLS_H
+#ifndef LIBSYS_SYSTEM_CALLS_H
+#define LIBSYS_SYSTEM_CALLS_H
-#include <libSystem/Macros.h>
+#include <libSystem/SystemKit/Macros.h>
// ------------------------------------------------------------------------------------------ //
/// @brief Types API.
// ------------------------------------------------------------------------------------------ //
-struct RefType {
- UInt32 __hash;
- VoidPtr __self;
+struct REF_TYPE {
+ UInt64 __hash; /// @brief Hash of the syscall
+ VoidPtr __self; /// @brief Syscall self value.
};
-typedef RefType* Ref;
+typedef REF_TYPE* Ref;
typedef Ref IORef;
typedef Ref FSRef;
@@ -382,4 +382,4 @@ IMPORT_C Char* StrFmt(const Char* fmt, ...); IMPORT_C UInt64 StrMathToNumber(const Char* in, const Char** endp, const SInt16 base);
-#endif // ifndef SCI_SYSTEM_CALLS_H
+#endif // ifndef LIBSYS_SYSTEM_CALLS_H
diff --git a/dev/libSystem/src/GNUmakefile b/dev/libSystem/src/Makefile index 9b901f9f..39af446b 100644 --- a/dev/libSystem/src/GNUmakefile +++ b/dev/libSystem/src/Makefile @@ -9,8 +9,8 @@ FLAGS=-f win64 .PHONY: error error: @echo "==> Invalid rule." - @echo "==> Use sci_asm_io_<arch> instead." + @echo "==> Use libsys_asm_io_<arch> instead." -.PHONY: sci_asm_io_x64 -sci_asm_io_x64: - $(ASM) $(FLAGS) SystemCalls+IO.asm -o SystemCalls+IO.stub.obj +.PHONY: libsys_asm_io_x64 +libsys_asm_io_x64: + $(ASM) $(FLAGS) SystemProc.asm -o SystemProc.stub.obj diff --git a/dev/libSystem/src/System.cc b/dev/libSystem/src/SystemImpl.cc index 1c28303d..d0682830 100644 --- a/dev/libSystem/src/System.cc +++ b/dev/libSystem/src/SystemImpl.cc @@ -4,11 +4,11 @@ ------------------------------------------- */
-#include <libSystem/AsmProc.h>
-#include <libSystem/System.h>
+#include <libSystem/SystemKit/Syscall.h>
+#include <libSystem/SystemKit/System.h>
-/// @file SystemCalls.cc
-/// @brief Source file for the memory functions/syscalls for libSystem.sys
+/// @file SystemAPI.cc
+/// @brief System wide API for NeKernel.
IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len) {
if (!len || !dest || !src) {
@@ -46,30 +46,27 @@ IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt return dest;
}
-//-----------------------------------------------------------------------------------------------------------//
-/// @brief Systems Calls implementation.
-/// @internal
-//-----------------------------------------------------------------------------------------------------------//
-
IMPORT_C Ref IoOpenFile(_Input const Char* path, _Input const Char* drv_letter) {
- return sci_syscall_arg_3(1, reinterpret_cast<VoidPtr>(const_cast<Char*>(path)),
- reinterpret_cast<VoidPtr>(const_cast<Char*>(drv_letter)));
+ return (Ref) libsys_syscall_arg_3(SYSCALL_HASH('IoOpenFile'),
+ reinterpret_cast<VoidPtr>(const_cast<Char*>(path)),
+ reinterpret_cast<VoidPtr>(const_cast<Char*>(drv_letter)));
}
IMPORT_C Void IoCloseFile(_Input Ref desc) {
- sci_syscall_arg_2(2, desc);
+ libsys_syscall_arg_2(2, desc);
}
IMPORT_C UInt64 IoSeekFile(_Input Ref desc, _Input UInt64 off) {
- auto ret = (volatile UInt64*) sci_syscall_arg_3(3, reinterpret_cast<VoidPtr>(desc),
- reinterpret_cast<VoidPtr>(&off));
+ auto ret = (volatile UInt64*) libsys_syscall_arg_3(
+ SYSCALL_HASH('IoSeekFile'), reinterpret_cast<VoidPtr>(desc), reinterpret_cast<VoidPtr>(&off));
MUST_PASS((*ret) != ~0UL);
return *ret;
}
IMPORT_C UInt64 IoTellFile(_Input Ref desc) {
- auto ret = (volatile UInt64*) sci_syscall_arg_2(4, reinterpret_cast<VoidPtr>(desc));
+ auto ret = (volatile UInt64*) libsys_syscall_arg_2(SYSCALL_HASH('IoTellFile'),
+ reinterpret_cast<VoidPtr>(desc));
return *ret;
}
@@ -78,8 +75,9 @@ IMPORT_C SInt32 PrintOut(_Input IORef desc, const char* fmt, ...) { va_start(args, fmt);
- auto ret = (volatile UInt64*) sci_syscall_arg_4(
- 5, reinterpret_cast<VoidPtr>(desc), reinterpret_cast<VoidPtr>(const_cast<Char*>(fmt)), args);
+ auto ret = (volatile UInt64*) libsys_syscall_arg_4(
+ SYSCALL_HASH('PrintOut'), reinterpret_cast<VoidPtr>(desc),
+ reinterpret_cast<VoidPtr>(const_cast<Char*>(fmt)), args);
va_end(args);
@@ -90,5 +88,7 @@ IMPORT_C Void _rtl_assert(Bool expr, const Char* origin) { if (!expr) {
PrintOut(nullptr, "Assertion failed: %s\r", origin);
PrintOut(nullptr, "Origin: %s\r", origin);
+
+ libsys_syscall_arg_1(SYSCALL_HASH('_rtl_debug_break'));
}
}
diff --git a/dev/libSystem/src/SystemCalls+IO.asm b/dev/libSystem/src/SystemProc.asm index 097046af..299b59f9 100644 --- a/dev/libSystem/src/SystemCalls+IO.asm +++ b/dev/libSystem/src/SystemProc.asm @@ -11,34 +11,37 @@ section .text -global sci_syscall_arg_1 -global sci_syscall_arg_2 -global sci_syscall_arg_3 -global sci_syscall_arg_4 +global libsys_syscall_arg_1 +global libsys_syscall_arg_2 +global libsys_syscall_arg_3 +global libsys_syscall_arg_4 -sci_syscall_arg_1: +libsys_syscall_arg_1: push rbp mov rbp, rsp mov r8, rcx - syscall + + int 50 pop rbp ret -sci_syscall_arg_2: +libsys_syscall_arg_2: push rbp mov rbp, rsp mov r8, rcx mov r9, rdx - syscall + + int 50 + pop rbp ret -sci_syscall_arg_3: +libsys_syscall_arg_3: push rbp mov rbp, rsp @@ -46,12 +49,13 @@ sci_syscall_arg_3: mov r9, rdx mov r10, rbx - syscall + int 50 + pop rbp ret -sci_syscall_arg_4: +libsys_syscall_arg_4: push rbp mov rbp, rsp @@ -60,7 +64,8 @@ sci_syscall_arg_4: mov r10, rbx mov r11, rax - syscall + int 50 + pop rbp ret diff --git a/dev/system_sdk/.keep b/dev/open_msg/.keep index e69de29b..e69de29b 100644 --- a/dev/system_sdk/.keep +++ b/dev/open_msg/.keep diff --git a/dev/open_msg/MsgKit/Server.h b/dev/open_msg/MsgKit/Server.h new file mode 100644 index 00000000..54183472 --- /dev/null +++ b/dev/open_msg/MsgKit/Server.h @@ -0,0 +1,22 @@ +/* ------------------------------------------- + + Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include <NeKit/KString.h> +#include <NeKit/MutableArray.h> + +struct OPENMSG_EXPR; + +struct OPENMSG_EXPR { + Kernel::KString* l_head; + Kernel::MutableArray<OPENMSG_EXPR> l_args; +}; + +typedef Kernel::Void (*openmsg_func_t)(OPENMSG_EXPR* arg); + +EXTERN_C Kernel::Void openmsg_init_library(openmsg_func_t* funcs, Kernel::SizeT cnt); +EXTERN_C Kernel::UInt32 openmsg_close_library(Kernel::Void); diff --git a/dev/open_msg/obj/.keep b/dev/open_msg/obj/.keep new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/dev/open_msg/obj/.keep diff --git a/dev/open_msg/script/window.msg b/dev/open_msg/script/window.msg new file mode 100644 index 00000000..77d6ef69 --- /dev/null +++ b/dev/open_msg/script/window.msg @@ -0,0 +1,6 @@ +(window + :id 1 + :pos (x 100 y 100) + :size (w 300 h 200) + :title "My App" + :on-click (lambda () (log "Clicked!")))
\ No newline at end of file diff --git a/dev/open_msg/src/.keep b/dev/open_msg/src/.keep new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/dev/open_msg/src/.keep diff --git a/docs/drawio/OS_DESIGN.drawio b/docs/drawio/SYSTEM_DESIGN.drawio index c428d137..c428d137 100644 --- a/docs/drawio/OS_DESIGN.drawio +++ b/docs/drawio/SYSTEM_DESIGN.drawio diff --git a/public/frameworks/CoreFoundation.fwrk/CoreFoundation.json b/public/frameworks/CoreFoundation.fwrk/CoreFoundation.json index a208f57a..076b35ae 100644 --- a/public/frameworks/CoreFoundation.fwrk/CoreFoundation.json +++ b/public/frameworks/CoreFoundation.fwrk/CoreFoundation.json @@ -3,7 +3,7 @@ "compiler_std": "c++20", "headers_path": ["../", "./", "../../../dev", "../../../dev/kernel"], "sources_path": ["src/*.cc"], - "output_name": "./dist/libCoreFoundation.dylib", + "output_name": "./dist/libCoreFoundation.fwrk.dylib", "compiler_flags": [ "-ffreestanding", "-shared", diff --git a/public/frameworks/CoreFoundation.fwrk/headers/Array.h b/public/frameworks/CoreFoundation.fwrk/headers/Array.h index 55e75e5e..d9c528d9 100644 --- a/public/frameworks/CoreFoundation.fwrk/headers/Array.h +++ b/public/frameworks/CoreFoundation.fwrk/headers/Array.h @@ -6,7 +6,7 @@ #pragma once -#include <libSystem/System.h> +#include <libSystem/SystemKit/System.h> namespace CF { template <typename T, SizeT N> diff --git a/public/frameworks/CoreFoundation.fwrk/headers/Foundation.h b/public/frameworks/CoreFoundation.fwrk/headers/Foundation.h index 1f295ea5..500ad544 100644 --- a/public/frameworks/CoreFoundation.fwrk/headers/Foundation.h +++ b/public/frameworks/CoreFoundation.fwrk/headers/Foundation.h @@ -10,7 +10,7 @@ #pragma once -#include <libSystem/System.h> +#include <libSystem/SystemKit/System.h> namespace CF { class CFString; diff --git a/public/frameworks/CoreFoundation.fwrk/headers/Object.h b/public/frameworks/CoreFoundation.fwrk/headers/Object.h index 1692f20e..5cb2050a 100644 --- a/public/frameworks/CoreFoundation.fwrk/headers/Object.h +++ b/public/frameworks/CoreFoundation.fwrk/headers/Object.h @@ -19,6 +19,6 @@ class CFObject { explicit CFObject() = default; virtual ~CFObject() = default; - SCI_COPY_DEFAULT(CFObject); + LIBSYS_COPY_DEFAULT(CFObject); }; } // namespace CF
\ No newline at end of file diff --git a/public/frameworks/CoreFoundation.fwrk/headers/Property.h b/public/frameworks/CoreFoundation.fwrk/headers/Property.h index 4da173c7..5210bd95 100644 --- a/public/frameworks/CoreFoundation.fwrk/headers/Property.h +++ b/public/frameworks/CoreFoundation.fwrk/headers/Property.h @@ -8,7 +8,7 @@ #define _PROPS_H #include <CoreFoundation.fwrk/headers/Ref.h> -#include <libSystem/System.h> +#include <libSystem/SystemKit/System.h> #define kMaxPropLen (256U) diff --git a/public/frameworks/CoreFoundation.fwrk/headers/Ref.h b/public/frameworks/CoreFoundation.fwrk/headers/Ref.h index cb72a034..87005db1 100644 --- a/public/frameworks/CoreFoundation.fwrk/headers/Ref.h +++ b/public/frameworks/CoreFoundation.fwrk/headers/Ref.h @@ -9,7 +9,7 @@ #define _REF_H_ #include <CoreFoundation.fwrk/headers/Object.h> -#include <libSystem/System.h> +#include <libSystem/SystemKit/System.h> namespace CF { template <typename T> diff --git a/public/frameworks/DiskImage.fwrk/DiskImage.json b/public/frameworks/DiskImage.fwrk/DiskImage.json index aaff409c..16c538e5 100644 --- a/public/frameworks/DiskImage.fwrk/DiskImage.json +++ b/public/frameworks/DiskImage.fwrk/DiskImage.json @@ -3,7 +3,7 @@ "compiler_std": "c++20", "headers_path": ["../", "./", "../../../dev", "../../../dev/kernel"], "sources_path": ["src/*.cc"], - "output_name": "./dist/libDiskImage.dylib", + "output_name": "./dist/libDiskImage.fwrk.dylib", "compiler_flags": [ "-ffreestanding", "-shared", diff --git a/public/frameworks/DiskImage.fwrk/headers/DiskImage.h b/public/frameworks/DiskImage.fwrk/headers/DiskImage.h index efc21253..53348f3e 100644 --- a/public/frameworks/DiskImage.fwrk/headers/DiskImage.h +++ b/public/frameworks/DiskImage.fwrk/headers/DiskImage.h @@ -9,7 +9,7 @@ #pragma once -#include <libSystem/System.h> +#include <libSystem/SystemKit/System.h> #ifndef __DISK_IMAGE_CDROM__ #define kDISectorSz (512) diff --git a/public/frameworks/KernelTest.fwrk/KernelTest.json b/public/frameworks/KernelTest.fwrk/KernelTest.json index aa70db30..b8c322b6 100644 --- a/public/frameworks/KernelTest.fwrk/KernelTest.json +++ b/public/frameworks/KernelTest.fwrk/KernelTest.json @@ -3,7 +3,7 @@ "compiler_std": "c++20", "headers_path": ["../", "./", "../../../dev", "../../../dev/kernel"], "sources_path": ["src/*.cc"], - "output_name": "./dist/libKernelTest.dylib", + "output_name": "./dist/libKernelTest.fwrk.dylib", "compiler_flags": [ "-ffreestanding", "-shared", diff --git a/public/frameworks/OpenMSG.fwrk/.keep b/public/frameworks/OpenMSG.fwrk/.keep new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/public/frameworks/OpenMSG.fwrk/.keep diff --git a/public/frameworks/OpenMSG.fwrk/OpenMSG.json b/public/frameworks/OpenMSG.fwrk/OpenMSG.json new file mode 100644 index 00000000..4bda8798 --- /dev/null +++ b/public/frameworks/OpenMSG.fwrk/OpenMSG.json @@ -0,0 +1,19 @@ +{ + "compiler_path": "clang++", + "compiler_std": "c++20", + "headers_path": [ + "./", + "../../../dev/kernel", + "../../../public/frameworks/", + "../../../dev/", + "./" + ], + "sources_path": [], + "output_name": "./dist/libOpenMSG.fwrk.dylib", + "cpp_macros": [ + "kSampleFWVersion=0x0100", + "kSampleFWVersionHighest=0x0100", + "kSampleFWVersionLowest=0x0100", + "__NE_SDK__" + ] +}
\ No newline at end of file diff --git a/public/frameworks/OpenMSG.fwrk/headers/.keep b/public/frameworks/OpenMSG.fwrk/headers/.keep new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/public/frameworks/OpenMSG.fwrk/headers/.keep diff --git a/public/frameworks/OpenMSG.fwrk/src/.keep b/public/frameworks/OpenMSG.fwrk/src/.keep new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/public/frameworks/OpenMSG.fwrk/src/.keep diff --git a/public/frameworks/OpenMSG.fwrk/src/DylibMain.cc b/public/frameworks/OpenMSG.fwrk/src/DylibMain.cc new file mode 100644 index 00000000..99eebd26 --- /dev/null +++ b/public/frameworks/OpenMSG.fwrk/src/DylibMain.cc @@ -0,0 +1,5 @@ +#include <libSystem/SystemKit/System.h> + +SInt32 _DylibAttach(SInt32 argc, Char* argv[]) { + return EXIT_FAILURE; +}
\ No newline at end of file diff --git a/public/frameworks/OpenMSG.fwrk/xml/.keep b/public/frameworks/OpenMSG.fwrk/xml/.keep new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/public/frameworks/OpenMSG.fwrk/xml/.keep diff --git a/public/frameworks/OpenMSG.fwrk/xml/app.xml b/public/frameworks/OpenMSG.fwrk/xml/app.xml new file mode 100644 index 00000000..6a46e598 --- /dev/null +++ b/public/frameworks/OpenMSG.fwrk/xml/app.xml @@ -0,0 +1,2 @@ +<PropertyList> +<PLEntry Type="CFString" Name="LibraryName" Len="12" Value="OpenMSG.fwrk" /></PropertyList>
\ No newline at end of file diff --git a/public/tools/cc/src/CommandLine.cc b/public/tools/cc/src/CommandLine.cc index f1c72b64..62757c43 100644 --- a/public/tools/cc/src/CommandLine.cc +++ b/public/tools/cc/src/CommandLine.cc @@ -4,7 +4,7 @@ * Copyright (c) 2024-2025 Amlal El Mahrouss */ -#include <libSystem/System.h> +#include <libSystem/SystemKit/System.h> /// @brief Placeholder program. diff --git a/public/tools/ld.dyn/src/CommandLine.cc b/public/tools/ld.dyn/src/CommandLine.cc index e76c34a7..e64e7837 100644 --- a/public/tools/ld.dyn/src/CommandLine.cc +++ b/public/tools/ld.dyn/src/CommandLine.cc @@ -4,15 +4,15 @@ * Copyright (c) 2024-2025 Amlal El Mahrouss */ -#include <libSystem/System.h> +#include <libSystem/SystemKit/System.h> /// @brief Library loader. #define DYNLIB_FLAG "-dyn" SInt32 _NeMain(SInt32 argc, Char* argv[]) { - SCI_UNUSED(argc); - SCI_UNUSED(argv); + LIBSYS_UNUSED(argc); + LIBSYS_UNUSED(argv); PrintOut(nullptr, "%s", "ld.dyn: Dynamic Loader.\n"); PrintOut(nullptr, "%s", "ld.dyn: © 2024-2025 Amlal El Mahrouss, All rights reserved.\n"); diff --git a/public/tools/ld.fwrk/src/CommandLine.cc b/public/tools/ld.fwrk/src/CommandLine.cc index e7a6e5ca..323e58c7 100644 --- a/public/tools/ld.fwrk/src/CommandLine.cc +++ b/public/tools/ld.fwrk/src/CommandLine.cc @@ -4,13 +4,13 @@ * Copyright (c) 2024-2025 Amlal El Mahrouss */ -#include <libSystem/System.h> +#include <libSystem/SystemKit/System.h> /// @brief This program loads a code framework into Kernel's memory. SInt32 _NeMain(SInt32 argc, Char* argv[]) { - SCI_UNUSED(argc); - SCI_UNUSED(argv); + LIBSYS_UNUSED(argc); + LIBSYS_UNUSED(argv); PrintOut(nullptr, "%s", "ld.fwrk: Framework Loader.\n"); PrintOut(nullptr, "%s", "ld.fwrk: © 2024-2025 Amlal El Mahrouss, All rights reserved.\n"); diff --git a/public/tools/manual/src/CommandLine.cc b/public/tools/manual/src/CommandLine.cc index 0704384a..8c75d565 100644 --- a/public/tools/manual/src/CommandLine.cc +++ b/public/tools/manual/src/CommandLine.cc @@ -1,8 +1,8 @@ -#include <libSystem/System.h> +#include <libSystem/SystemKit/System.h> SInt32 _NeMain(SInt32 argc, Char* argv[]) { - SCI_UNUSED(argc); - SCI_UNUSED(argv); + LIBSYS_UNUSED(argc); + LIBSYS_UNUSED(argv); if (argc < 2) { PrintOut(nullptr, "HELP: manual <tutorial_name>\n"); diff --git a/public/tools/mgmt/src/CommandLine.cc b/public/tools/mgmt/src/CommandLine.cc index f3840d01..6015988e 100644 --- a/public/tools/mgmt/src/CommandLine.cc +++ b/public/tools/mgmt/src/CommandLine.cc @@ -1,4 +1,4 @@ -#include <libSystem/System.h> +#include <libSystem/SystemKit/System.h> SInt32 _NeMain(SInt32 argc, Char* argv[]) { return EXIT_FAILURE; diff --git a/public/tools/mk.fwrk/Common.h b/public/tools/mk.fwrk/Common.h index 89e52471..8e434043 100644 --- a/public/tools/mk.fwrk/Common.h +++ b/public/tools/mk.fwrk/Common.h @@ -7,6 +7,6 @@ #define APPS_COMMON_H #include <CoreFoundation.fwrk/headers/Foundation.h> -#include <libSystem/System.h> +#include <libSystem/SystemKit/System.h> #endif // APPS_COMMON_H diff --git a/public/tools/mk.fwrk/src/CommandLine.cc b/public/tools/mk.fwrk/src/CommandLine.cc index 4ef7240a..87945ddc 100644 --- a/public/tools/mk.fwrk/src/CommandLine.cc +++ b/public/tools/mk.fwrk/src/CommandLine.cc @@ -7,7 +7,7 @@ #include <Framework.h> #include <Steps.h> -#include <libSystem/ProcessCodes.h> +#include <libSystem/SystemKit/Err.h> #include <CoreFoundation.fwrk/headers/Array.h> diff --git a/public/tools/mk.hefs/src/CommandLine.cc b/public/tools/mk.hefs/src/CommandLine.cc index 711009d5..91a3527d 100644 --- a/public/tools/mk.hefs/src/CommandLine.cc +++ b/public/tools/mk.hefs/src/CommandLine.cc @@ -4,13 +4,13 @@ * Copyright (c) 2025 Amlal El Mahrouss */ -#include <libSystem/System.h> +#include <libSystem/SystemKit/System.h> /// @brief Placeholder program. SInt32 _NeMain(SInt32 argc, Char* argv[]) { - SCI_UNUSED(argc); - SCI_UNUSED(argv); + LIBSYS_UNUSED(argc); + LIBSYS_UNUSED(argv); return EXIT_FAILURE; } diff --git a/public/tools/mk.nefs/src/CommandLine.cc b/public/tools/mk.nefs/src/CommandLine.cc index 711009d5..91a3527d 100644 --- a/public/tools/mk.nefs/src/CommandLine.cc +++ b/public/tools/mk.nefs/src/CommandLine.cc @@ -4,13 +4,13 @@ * Copyright (c) 2025 Amlal El Mahrouss */ -#include <libSystem/System.h> +#include <libSystem/SystemKit/System.h> /// @brief Placeholder program. SInt32 _NeMain(SInt32 argc, Char* argv[]) { - SCI_UNUSED(argc); - SCI_UNUSED(argv); + LIBSYS_UNUSED(argc); + LIBSYS_UNUSED(argv); return EXIT_FAILURE; } diff --git a/public/tools/open/src/CommandLine.cc b/public/tools/open/src/CommandLine.cc index 6fa4e2f2..e02fcebc 100644 --- a/public/tools/open/src/CommandLine.cc +++ b/public/tools/open/src/CommandLine.cc @@ -4,7 +4,7 @@ * Copyright (c) 2024-2025 Amlal El Mahrouss */ -#include <libSystem/System.h> +#include <libSystem/SystemKit/System.h> /// @brief This program opens an application from **OPEN_APP_BASE_PATH** /// @file CommandLine.cc diff --git a/public/tools/ping/src/CommandLine.cc b/public/tools/ping/src/CommandLine.cc index f5c82b80..a9ba177a 100644 --- a/public/tools/ping/src/CommandLine.cc +++ b/public/tools/ping/src/CommandLine.cc @@ -1,7 +1,7 @@ -#include <libSystem/System.h> +#include <libSystem/SystemKit/System.h> SInt32 _NeMain(SInt32 argc, Char* argv[]) { - SCI_UNUSED(argc); + LIBSYS_UNUSED(argc); if (argc < 2) { PrintOut(nullptr, "HELP: ping <hostname>\n"); diff --git a/setup_x64_project.sh b/setup_x64_project.sh index ba7d4b3f..fd5edf7c 100755 --- a/setup_x64_project.sh +++ b/setup_x64_project.sh @@ -4,9 +4,9 @@ # 03/25/25: Add 'disk' build step. # 04/05/25: Improve and fix script. -cd dev/user +cd dev/libSystem cd src -make sci_asm_io_x64 +make libsys_asm_io_x64 cd .. btb libSystem.json cd ../ddk diff --git a/tooling/mk_app.py b/tooling/mk_app.py index 5ee6d5b7..7f7cef17 100755 --- a/tooling/mk_app.py +++ b/tooling/mk_app.py @@ -64,7 +64,7 @@ def create_directory_structure(base_path, project_name): proj_cpp_path = os.path.join(base_path, project_name, f"src/CommandLine.cc") - cpp_file = "#include <libSystem/System.h>\n\nSInt32 _NeMain(SInt32 argc, Char* argv[]) {\n\treturn EXIT_FAILURE;\n}" + cpp_file = "#include <libSystem/SystemKit/System.h>\n\nSInt32 _NeMain(SInt32 argc, Char* argv[]) {\n\treturn EXIT_FAILURE;\n}" with open(proj_cpp_path, 'w') as cpp_file_io: cpp_file_io.write(cpp_file) diff --git a/tooling/mk_fwrk.py b/tooling/mk_fwrk.py index e4e5d7de..b2ef99ff 100755 --- a/tooling/mk_fwrk.py +++ b/tooling/mk_fwrk.py @@ -56,7 +56,7 @@ def create_directory_structure(base_path_fwrk, project_file_name, project_name): "sources_path": [ ], - "output_name": f"./dist/{project_name}", + "output_name": f"./dist/lib{project_name}.dylib", "cpp_macros": [ "kSampleFWVersion=0x0100", "kSampleFWVersionHighest=0x0100", @@ -70,7 +70,7 @@ def create_directory_structure(base_path_fwrk, project_file_name, project_name): proj_cpp_path = os.path.join(base_path_fwrk, project_name, f"src/DylibMain.cc") - cpp_file = "#include <libSystem/System.h>\n\nSInt32 _DylibAttach(SInt32 argc, Char* argv[]) {\n\treturn EXIT_FAILURE;\n}" + cpp_file = "#include <libSystem/SystemKit/System.h>\n\nSInt32 _DylibAttach(SInt32 argc, Char* argv[]) {\n\treturn EXIT_FAILURE;\n}" with open(proj_cpp_path, 'w') as cpp_file_io: cpp_file_io.write(cpp_file) diff --git a/tooling/mkfs.hefs.cc b/tooling/mkfs.hefs.cc index fcb0c006..ef6ceb32 100644 --- a/tooling/mkfs.hefs.cc +++ b/tooling/mkfs.hefs.cc @@ -7,123 +7,227 @@ #include <tooling/hefs.h> #include <tooling/mkfs.h> #include <cstdlib> +#include <cstring> #include <fstream> +#include <limits> +#include <algorithm> namespace detail { -/// @interal -/// @brief GB equation formula. +/// @internal +/// @brief GB‐to‐byte conversion (use multiplication, not XOR). static constexpr size_t gib_cast(uint32_t gb) { - return ((1024 ^ 3) * gb); + return static_cast<size_t>(gb) * 1024ULL * 1024ULL * 1024ULL; } } // namespace detail static size_t kDiskSize = detail::gib_cast(4UL); static uint16_t kVersion = kHeFSVersion; -static std::u8string kLabel = kHeFSDefaultVolumeName; +static std::u8string kLabel; static size_t kSectorSize = 512; -/// @brief Entrypoint of tool. -int main(int argc, char** argv) { - if (argc < 2) { - mkfs::console_out() - << "hefs: usage: mkfs.hefs -L <label> -s <sector_size> -b <ind_start> -e " - << "<ind_end> -bs <block_start> -be <block_end> -is <in_start> -ie <in_end> " - "-S <disk_size> -o <output_device>" - << "\n"; - return EXIT_FAILURE; - } - - std::string args; - std::u8string args_wide; - - for (int i = 1; i < argc; ++i) { - args += argv[i]; - args += " "; +static bool parse_decimal(const std::string& opt, unsigned long long& out) { + if (opt.empty()) return false; + char* endptr = nullptr; + unsigned long long val = std::strtoull(opt.c_str(), &endptr, 10); + if (endptr == opt.c_str() || *endptr != '\0') return false; + out = val; + return true; +} - std::string str = argv[i]; +static bool parse_signed(const std::string& opt, long& out, int base = 10) { + if (opt.empty()) return false; + char* endptr = nullptr; + long val = std::strtol(opt.c_str(), &endptr, base); + if (endptr == opt.c_str() || *endptr != '\0' || val < 0) return false; + out = val; + return true; +} - for (auto& ch : str) { - args_wide.push_back(ch); +static std::string build_args(int argc, char** argv) { + std::string combined; + for (int i = 1; i < argc; ++i) { + combined += argv[i]; + combined += ' '; } + return combined; +} - args_wide += u8" "; - } - - auto output_path = mkfs::get_option<char>(args, "-o"); - - kSectorSize = std::strtol(mkfs::get_option<char>(args, "-s").data(), nullptr, 10); - kLabel = mkfs::get_option<char8_t>(args_wide, u8"-L"); - - if (!kSectorSize) { - mkfs::console_out() << "hefs: error: Sector size size is set to zero.\n"; - return EXIT_FAILURE; - } - - if (kLabel.empty()) kLabel = kHeFSDefaultVolumeName; - - kDiskSize = - std::strtol(mkfs::get_option<char>(args, "-S").data(), nullptr, 10) * 1024 * 1024 * 1024; - - if (!kDiskSize) { - mkfs::console_out() << "hefs: error: Disk size is set to zero.\n"; - return EXIT_FAILURE; - } - - // Open the output_device - std::ofstream output_device(output_path, std::ios::binary); - - if (!output_device.good()) { - mkfs::console_out() << "hefs: error: Unable to open output_device: " << output_path << "\n"; - return EXIT_FAILURE; - } +int main(int argc, char** argv) { + if (argc < 2) { + mkfs::console_out() + << "hefs: usage: mkfs.hefs -L=<label> -s=<sector_size> -b=<ind_start> -e=<ind_end> " + "-bs=<block_start> -be=<block_end> -is=<in_start> -ie=<in_end> " + "-S=<disk_size_GB> -o=<output_device>\n"; - // create a boot node, and then allocate a index node directory tree. - mkfs::hefs::BootNode boot_node{{}, {}, 0, 0, 0, 0, 0, 0, 0, 0}; + return EXIT_FAILURE; + } - auto start_ind = std::strtol(mkfs::get_option<char>(args, "-b").data(), nullptr, 16); + std::string args = build_args(argc, argv); - start_ind += sizeof(mkfs::hefs::BootNode); + auto output_path = mkfs::get_option<char>(args, "-o"); + if (output_path.empty()) { + mkfs::console_out() << "hefs: error: Missing -o <output_device> argument.\n"; + return EXIT_FAILURE; + } - auto end_ind = std::strtol(mkfs::get_option<char>(args, "-e").data(), nullptr, 16); + auto opt_s = mkfs::get_option<char>(args, "-s"); + long parsed_s = 0; + if (!parse_signed(opt_s, parsed_s, 10) || parsed_s == 0) { + mkfs::console_out() + << "hefs: error: Invalid sector size \"" << opt_s + << "\". Must be a positive integer.\n"; + return EXIT_FAILURE; + } - auto start_block = std::strtol(mkfs::get_option<char>(args, "-bs").data(), nullptr, 16); - auto end_block = std::strtol(mkfs::get_option<char>(args, "-be").data(), nullptr, 16); + if ((parsed_s & (parsed_s - 1)) != 0) { + mkfs::console_out() + << "hefs: error: Sector size \"" << parsed_s + << "\" is not a power of two.\n"; + return EXIT_FAILURE; + } + kSectorSize = static_cast<size_t>(parsed_s); + + auto opt_L = mkfs::get_option<char>(args, "-L"); + if (!opt_L.empty()) { + kLabel.clear(); + for (char c : opt_L) kLabel.push_back(static_cast<char8_t>(c)); + } else { + kLabel.clear(); + for (size_t i = 0; i < kHeFSPartNameLen && kHeFSDefaultVolumeName[i] != u'\0'; ++i) { + kLabel.push_back(static_cast<char8_t>(kHeFSDefaultVolumeName[i])); + } + } - auto start_in = std::strtol(mkfs::get_option<char>(args, "-is").data(), nullptr, 16); - auto end_in = std::strtol(mkfs::get_option<char>(args, "-ie").data(), nullptr, 16); + auto opt_S = mkfs::get_option<char>(args, "-S"); + unsigned long long gb = 0; + if (!parse_decimal(opt_S, gb) || gb == 0ULL) { + mkfs::console_out() + << "hefs: error: Invalid disk size \"" << opt_S + << "\". Must be a positive integer.\n"; + return EXIT_FAILURE; + } + unsigned long long max_gb = + std::numeric_limits<uint64_t>::max() / (1024ULL * 1024ULL * 1024ULL); + if (gb > max_gb) { + mkfs::console_out() + << "hefs: error: Disk size \"" << gb << "GB\" is too large.\n"; + return EXIT_FAILURE; + } + kDiskSize = static_cast<size_t>(gb * 1024ULL * 1024ULL * 1024ULL); + + auto opt_b = mkfs::get_option<char>(args, "-b"); + auto opt_e = mkfs::get_option<char>(args, "-e"); + auto opt_bs = mkfs::get_option<char>(args, "-bs"); + auto opt_be = mkfs::get_option<char>(args, "-be"); + auto opt_is = mkfs::get_option<char>(args, "-is"); + auto opt_ie = mkfs::get_option<char>(args, "-ie"); + + long start_ind = 0, end_ind = 0; + long start_block = 0, end_block = 0; + long start_in = 0, end_in = 0; + + if (!parse_signed(opt_b, start_ind, 16)) { + mkfs::console_out() << "hefs: error: Invalid -b <hex> argument.\n"; + return EXIT_FAILURE; + } + if (!parse_signed(opt_e, end_ind, 16) || end_ind <= start_ind) { + mkfs::console_out() + << "hefs: error: Invalid or out-of-range -e <hex> argument.\n"; + return EXIT_FAILURE; + } + if (!parse_signed(opt_bs, start_block, 16)) { + mkfs::console_out() << "hefs: error: Invalid -bs <hex> argument.\n"; + return EXIT_FAILURE; + } + if (!parse_signed(opt_be, end_block, 16) || end_block <= start_block) { + mkfs::console_out() + << "hefs: error: Invalid or out-of-range -be <hex> argument.\n"; + return EXIT_FAILURE; + } + if (!parse_signed(opt_is, start_in, 16)) { + mkfs::console_out() << "hefs: error: Invalid -is <hex> argument.\n"; + return EXIT_FAILURE; + } + if (!parse_signed(opt_ie, end_in, 16) || end_in <= start_in) { + mkfs::console_out() + << "hefs: error: Invalid or out-of-range -ie <hex> argument.\n"; + return EXIT_FAILURE; + } - boot_node.version = kVersion; - boot_node.diskKind = mkfs::hefs::kHeFSHardDrive; - boot_node.encoding = mkfs::hefs::kHeFSEncodingFlagsUTF8; - boot_node.diskSize = kDiskSize; - boot_node.sectorSize = kSectorSize; - boot_node.startIND = start_ind; - boot_node.endIND = end_ind; - boot_node.startIN = start_in; - boot_node.endIN = end_in; - boot_node.startBlock = start_block; - boot_node.endBlock = end_block; - boot_node.indCount = 0UL; - boot_node.diskStatus = mkfs::hefs::kHeFSStatusUnlocked; + if (static_cast<size_t>(end_block) * kSectorSize > kDiskSize || + static_cast<size_t>(end_ind) > kDiskSize || + static_cast<size_t>(end_in) > kDiskSize) { + mkfs::console_out() << "hefs: error: One or more ranges exceed disk size.\n"; + return EXIT_FAILURE; + } - std::memcpy(boot_node.magic, kHeFSMagic, kHeFSMagicLen - 1); - std::memcpy(boot_node.volumeName, kLabel.data(), kLabel.size() * sizeof(char16_t)); + std::ofstream output_device(output_path, std::ios::binary); + if (!output_device.good()) { + mkfs::console_out() + << "hefs: error: Unable to open output device: " << output_path << "\n"; + return EXIT_FAILURE; + } - output_device.seekp(std::strtol(mkfs::get_option<char>(args, "-b").data(), nullptr, 16)); - output_device.write(reinterpret_cast<const char*>(&boot_node), sizeof(mkfs::hefs::BootNode)); + mkfs::hefs::BootNode boot_node; + std::memset(&boot_node, 0, sizeof(boot_node)); + + boot_node.version = kVersion; + boot_node.diskKind = mkfs::hefs::kHeFSHardDrive; + boot_node.encoding = mkfs::hefs::kHeFSEncodingFlagsUTF8; + boot_node.diskSize = kDiskSize; + boot_node.sectorSize = kSectorSize; + boot_node.startIND = static_cast<size_t>(start_ind) + sizeof(mkfs::hefs::BootNode); + boot_node.endIND = static_cast<size_t>(end_ind); + boot_node.startIN = static_cast<size_t>(start_in); + boot_node.endIN = static_cast<size_t>(end_in); + boot_node.startBlock = static_cast<size_t>(start_block); + boot_node.endBlock = static_cast<size_t>(end_block); + boot_node.indCount = 0UL; + boot_node.diskStatus = mkfs::hefs::kHeFSStatusUnlocked; + + static_assert(sizeof(boot_node.magic) >= kHeFSMagicLen, + "BootNode::magic too small to hold kHeFSMagicLen"); + std::memset(boot_node.magic, 0, sizeof(boot_node.magic)); + size_t magic_copy = + (sizeof(boot_node.magic) < kHeFSMagicLen - 1) + ? sizeof(boot_node.magic) + : (kHeFSMagicLen - 1); + std::memcpy(boot_node.magic, kHeFSMagic, magic_copy); + boot_node.magic[magic_copy] = 0; + + constexpr size_t vol_slots = kHeFSPartNameLen; + std::memset(boot_node.volumeName, 0, sizeof(boot_node.volumeName)); + size_t label_units = std::min(kLabel.size(), vol_slots - 1); + for (size_t i = 0; i < label_units; ++i) { + boot_node.volumeName[i] = static_cast<char16_t>(kLabel[i]); + } + boot_node.volumeName[label_units] = 0; - if (!output_device.good()) { - mkfs::console_out() << "hefs: error: Unable to write filesystem to output_device: " - << output_path << "\n"; - return EXIT_FAILURE; - } + output_device.seekp(static_cast<std::streamoff>(start_ind)); + if (!output_device.good()) { + mkfs::console_out() << "hefs: error: Failed seek to index start.\n"; + return EXIT_FAILURE; + } - output_device.seekp(boot_node.startIND); + output_device.write(reinterpret_cast<const char*>(&boot_node), + sizeof(boot_node)); + if (!output_device.good()) { + mkfs::console_out() + << "hefs: error: Unable to write BootNode to output device: " + << output_path << "\n"; + return EXIT_FAILURE; + } - output_device.flush(); - output_device.close(); + output_device.seekp(static_cast<std::streamoff>(boot_node.startIND)); + if (!output_device.good()) { + mkfs::console_out() << "hefs: error: Failed seek to startIND.\n"; + return EXIT_FAILURE; + } - mkfs::console_out() << "hefs: info: Wrote filesystem to output_device: " << output_path << "\n"; + output_device.flush(); + output_device.close(); - return EXIT_SUCCESS; -}
\ No newline at end of file + mkfs::console_out() + << "hefs: info: Wrote filesystem to output device: " << output_path << "\n"; + return EXIT_SUCCESS; +} |
