diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-04-16 18:15:13 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-16 18:15:13 +0200 |
| commit | 0033ef41f33ce3d39b4e7ed7e1353cec916bdd5d (patch) | |
| tree | cf298a3b522fed5b829c4b250932ab13ef407838 | |
| parent | d3cf45311cf3f6de0bc25bb0ddb5c14a29a72cd6 (diff) | |
| parent | 58b3d776ab5435de352217b2a3a525aeb9316e99 (diff) | |
Merge pull request #19 from amlel-el-mahrouss/dev
urgent patches.
65 files changed, 501 insertions, 292 deletions
diff --git a/.github/workflows/kernel-ahci.yml b/.github/workflows/kernel-ahci.yml index 51cb54a3..a76865c0 100644 --- a/.github/workflows/kernel-ahci.yml +++ b/.github/workflows/kernel-ahci.yml @@ -16,5 +16,5 @@ jobs: - name: Install Packages run: sudo apt update && sudo apt install mingw-w64 qemu-utils nasm - name: Build NeKernel (AHCI) - run: ./setup_amd64.sh && cd dev/kernel && export AHCI_SUPPORT=1 && make -f amd64-ci.make all + run: ./setup_x64.sh && cd dev/kernel && export AHCI_SUPPORT=1 && make -f amd64-ci.make all diff --git a/.github/workflows/kernel-pio.yml b/.github/workflows/kernel-pio.yml index 57f6faee..e6a205b3 100644 --- a/.github/workflows/kernel-pio.yml +++ b/.github/workflows/kernel-pio.yml @@ -16,5 +16,5 @@ jobs: - name: Install Packages run: sudo apt update && sudo apt install mingw-w64 qemu-utils nasm - name: Build NeKernel (ATA PIO) - run: ./setup_amd64.sh && cd dev/kernel && export ATA_PIO_SUPPORT=1 && make -f amd64-ci.make all + run: ./setup_x64.sh && cd dev/kernel && export ATA_PIO_SUPPORT=1 && make -f amd64-ci.make all @@ -46,7 +46,7 @@ A documentation regarding building, and developing for NeKernel is available her ```sh git clone git@github.com:amlel-el-mahrouss/nekernel.git cd nekernel -./setup_amd64.sh +./setup_x64.sh ./debug_ata_x64.sh # Generic ATA PIO target ``` diff --git a/dev/boot/BootKit/Device.h b/dev/boot/BootKit/Device.h index 77c6cd32..88574bd6 100644 --- a/dev/boot/BootKit/Device.h +++ b/dev/boot/BootKit/Device.h @@ -28,7 +28,7 @@ public: virtual Trait& Leak() = 0; - virtual Device& Read(Char* Buf, SizeT SecCount) = 0; + virtual Device& Read(Char* Buf, SizeT SecCount) = 0; virtual Device& Write(Char* Buf, SizeT SecCount) = 0; }; diff --git a/dev/boot/amd64-ci.make b/dev/boot/amd64-ci.make index ae451e26..62ab9af1 100644 --- a/dev/boot/amd64-ci.make +++ b/dev/boot/amd64-ci.make @@ -56,7 +56,7 @@ EMU_FLAGS= -smp 4 -m 8G \ -bios $(BIOS) -M q35 -cdrom $(BOOT) -boot d -accel kvm endif -LD_FLAGS=-e ModuleMain --subsystem=10 +LD_FLAGS=-e BootloaderMain --subsystem=10 STANDALONE_MACRO=-D__BOOTZ_STANDALONE__ OBJ=obj/*.o @@ -88,7 +88,7 @@ all: compile-amd64 .PHONY: disk disk: - dd if=/dev/zero of=$(BOOT) bs=30M count=100 + dd if=/dev/zero of=$(BOOT) bs=7M count=100 mformat -i $(BOOT) -F -v "NeKernel" diff --git a/dev/boot/amd64-desktop.make b/dev/boot/amd64-desktop.make index 3adbfb33..21a99941 100644 --- a/dev/boot/amd64-desktop.make +++ b/dev/boot/amd64-desktop.make @@ -58,7 +58,7 @@ EMU_FLAGS= -smp 4 -m 8G \ -bios $(BIOS) -M q35 -cdrom $(BOOT) -boot d -accel kvm endif -LD_FLAGS=-e ModuleMain --subsystem=10 +LD_FLAGS=-e BootloaderMain --subsystem=10 STANDALONE_MACRO=-D__BOOTZ_STANDALONE__ OBJ=obj/*.o @@ -96,7 +96,7 @@ all: compile-amd64 .PHONY: disk disk: - dd if=/dev/zero of=$(BOOT) bs=30M count=100 + dd if=/dev/zero of=$(BOOT) bs=7M count=100 mformat -i $(BOOT) -F -v "NEKERNEL-ESP" @@ -128,7 +128,7 @@ run-efi-amd64-ata-dma: .PHONY: run-efi-amd64-ata run-efi-amd64-ata: run-efi-amd64-ata-dma -# img_2 is the rescue disk. img is the bootable disk, as provided by the Zeta specs. +# img_2 is the rescue disk. img is the bootable disk, as provided by the NeKernel specs. .PHONY: epm-img epm-img: qemu-img create -f raw $(IMG) 4G diff --git a/dev/boot/arm64-desktop.make b/dev/boot/arm64-desktop.make index a553e908..06ec84d0 100644 --- a/dev/boot/arm64-desktop.make +++ b/dev/boot/arm64-desktop.make @@ -34,7 +34,7 @@ EMU_FLAGS= -smp 4 -m 8G -cpu max -M virt \ file=fat:rw:src/root/,index=2,format=raw \ -no-shutdown -no-reboot -cpu cortex-a72 -device virtio-gpu-pci -LD_FLAGS=-subsystem:efi_application -entry:ModuleMain /nodefaultlib +LD_FLAGS=-subsystem:efi_application -entry:BootloaderMain /nodefaultlib STANDALONE_MACRO=-D__BOOTZ_STANDALONE__ OBJ=*.o diff --git a/dev/boot/gdbinit b/dev/boot/gdbinit index e5b29f38..b55fa645 100644 --- a/dev/boot/gdbinit +++ b/dev/boot/gdbinit @@ -1,3 +1,3 @@ set disassemble-next-line on -b ModuleMain +b BootloaderMain target remote localhost:1234 diff --git a/dev/boot/modules/BootNet/BootNet.cc b/dev/boot/modules/BootNet/BootNet.cc index 8ee08929..050148c4 100644 --- a/dev/boot/modules/BootNet/BootNet.cc +++ b/dev/boot/modules/BootNet/BootNet.cc @@ -7,29 +7,41 @@ * ======================================================== */ +#include <FirmwareKit/EFI/API.h> #include <modules/BootNet/BootNet.h> #include <BootKit/BootKit.h> #include <BootKit/BootThread.h> -STATIC EfiGUID kEfiIP4ProtoGUID = {}; +STATIC EfiGUID kEfiSimpleProtoGUID = EFI_SIMPLE_NETWORK_PROTOCOL_GUID; +STATIC EFI_SIMPLE_NETWORK_PROTOCOL* kEfiProtocol = nullptr; -STATIC Void bootnet_read_udp_packet(BOOTNET_INTERNET_HEADER&); +STATIC Void bootnet_read_ip_packet(BOOTNET_INTERNET_HEADER); EXTERN_C Int32 BootNetModuleMain(Kernel::HEL::BootInfoHeader* handover) { + Boot::BootTextWriter writer; + + writer.Write("BootNet: Init EFI...\r"); + + fw_init_efi((EfiSystemTable*)handover->f_FirmwareCustomTables[1]); + + if (BS->LocateProtocol(&kEfiSimpleProtoGUID, nullptr, (VoidPtr*)&kEfiProtocol) != kEfiOk) + { + writer.Write("BootNet: Not supported by firmware.\r"); + return kEfiFail; + } + BOOTNET_INTERNET_HEADER inet{}; - memset(&inet, 0, sizeof(BOOTNET_INTERNET_HEADER)); - - bootnet_read_udp_packet(inet); + SetMem(&inet, 0, sizeof(BOOTNET_INTERNET_HEADER)); + + writer.Write("BootNet: Downloading kernel...\r"); - /// TODO: Read address from JSON file 'bootnet.json' + bootnet_read_ip_packet(inet); if (inet.Length < 1) { - Boot::BootTextWriter writer; writer.Write("BootNet: No executable attached to the packet, aborting.\r"); - return kEfiFail; } @@ -38,23 +50,35 @@ EXTERN_C Int32 BootNetModuleMain(Kernel::HEL::BootInfoHeader* handover) Boot::BootThread thread(inet.Data); if (thread.IsValid()) + { + writer.Write("BootNet: Running kernel...\r"); return thread.Start(handover, YES); + } return kEfiFail; } else { - Boot::BootTextWriter writer; - writer.Write("BootNetLauncher: EEPROM flash is not available as of right now.\r"); + constexpr auto kROMSize = 0x200; + + if (inet.Length != kROMSize) + { + writer.Write("BootNet: Not within 512K.\r"); + return kEfiFail; + } + + writer.Write("BootNet: Programming the flash is not available as of right now.\r"); /// TODO: Program new firmware to EEPROM (if crc and size matches) - return kEfiFail; // TODO: Add support for EEPROM firmware update. + const UIntPtr kEEPROMAddress = 0; + const UInt16 kEEPROMSize = inet.Length; } return kEfiFail; } -STATIC Void bootnet_read_udp_packet(BOOTNET_INTERNET_HEADER&) +STATIC Void bootnet_read_ip_packet(BOOTNET_INTERNET_HEADER inet) { + NE_UNUSED(inet); }
\ No newline at end of file diff --git a/dev/boot/modules/BootNet/BootNet.h b/dev/boot/modules/BootNet/BootNet.h index aba7924f..b38ed0e9 100644 --- a/dev/boot/modules/BootNet/BootNet.h +++ b/dev/boot/modules/BootNet/BootNet.h @@ -29,5 +29,5 @@ typedef struct BOOTNET_INTERNET_HEADER Kernel::Char Target[kBootNetNameLen]; /// the target file. Kernel::Boolean ImpliesProgram : 1; /// does it imply an EEPROM reprogram? Kernel::Boolean Preflight : 1; /// is it a preflight packet. - Kernel::Char Data[]; /// non preflight packet has a patch blob for a **PatchTarget** + Kernel::Char Data[1]; /// non preflight packet has a patch blob for a **PatchTarget** } ATTRIBUTE(packed) BOOTNET_INTERNET_HEADER; diff --git a/dev/boot/modules/BootNet/amd64.json b/dev/boot/modules/BootNet/amd64.json index da4a15f0..912eeb0b 100644 --- a/dev/boot/modules/BootNet/amd64.json +++ b/dev/boot/modules/BootNet/amd64.json @@ -7,7 +7,6 @@ "compiler_flags": [ "-nostdlib", "-std=c++20", - "-fPIC", "-fno-rtti", "-fno-exceptions", "-Wl,--subsystem=17,--image-base,0x10000000,-e,BootNetModuleMain" diff --git a/dev/boot/modules/SysChk/SysChk.cc b/dev/boot/modules/SysChk/SysChk.cc index 3086cdc6..dec4e11a 100644 --- a/dev/boot/modules/SysChk/SysChk.cc +++ b/dev/boot/modules/SysChk/SysChk.cc @@ -28,6 +28,8 @@ EXTERN_C Int32 SysChkModuleMain(Kernel::HEL::BootInfoHeader* handover) { + NE_UNUSED(handover); + #ifdef __ATA_PIO__ Boot::BDiskFormatFactory<BootDeviceATA> partition_factory; diff --git a/dev/boot/modules/SysChk/amd64-ahci.json b/dev/boot/modules/SysChk/amd64-ahci.json index 527e79f9..d13e6c4a 100644 --- a/dev/boot/modules/SysChk/amd64-ahci.json +++ b/dev/boot/modules/SysChk/amd64-ahci.json @@ -7,7 +7,6 @@ "compiler_flags": [ "-nostdlib", "-std=c++20", - "-fPIC", "-fno-rtti", "-fno-exceptions", "-Wl,--subsystem=17,--image-base,0x10000000,-e,SysChkModuleMain" diff --git a/dev/boot/modules/SysChk/amd64-pio.json b/dev/boot/modules/SysChk/amd64-pio.json index de3195ce..f15a8efd 100644 --- a/dev/boot/modules/SysChk/amd64-pio.json +++ b/dev/boot/modules/SysChk/amd64-pio.json @@ -7,7 +7,6 @@ "compiler_flags": [ "-nostdlib", "-std=c++20", - "-fPIC", "-fno-rtti", "-fno-exceptions", "-Wl,--subsystem=17,--image-base,0x10000000,-e,SysChkModuleMain" diff --git a/dev/boot/modules/SysChk/arm64.json b/dev/boot/modules/SysChk/arm64.json index 1f91326e..ad5fde6e 100644 --- a/dev/boot/modules/SysChk/arm64.json +++ b/dev/boot/modules/SysChk/arm64.json @@ -11,7 +11,7 @@ "-fno-rtti", "-fno-exceptions", "-fuse-ld=lld", - "-Wl,-subsystem:efi_application,-entry:ModuleMain", + "-Wl,-subsystem:efi_application,-entry:BootloaderMain", "-target aarch64-unknown-windows" ], "cpp_macros": [ diff --git a/dev/boot/src/BootThread.cc b/dev/boot/src/BootThread.cc index ce691f96..f1866a76 100644 --- a/dev/boot/src/BootThread.cc +++ b/dev/boot/src/BootThread.cc @@ -166,40 +166,18 @@ namespace Boot /// @note handover header has to be valid! Int32 BootThread::Start(HEL::BootInfoHeader* handover, Bool own_stack) { - HEL::HandoverProc err_fn = [](HEL::BootInfoHeader* rcx) -> Int32 { - NE_UNUSED(rcx); - - fb_render_string("BootZ: Invalid Boot Image...", 50, 10, RGB(0xFF, 0xFF, 0xFF)); - ::Boot::Stop(); - - return NO; - }; - - if (!fStartAddress) - { - err_fn(handover); - } - fHandover = handover; if (own_stack) { - UInt8* aligned_stack = &fStack[mib_cast(16) - 1]; - aligned_stack = (UInt8*)((UIntPtr)aligned_stack & ~0xF); - - rt_jump_to_address(fStartAddress, fHandover, aligned_stack); + return rt_jump_to_address(fStartAddress, fHandover, &fStack[mib_cast(16) - 1]); } else { - if (fStack) - delete[] fStack; - - fStack = nullptr; - return reinterpret_cast<HEL::HandoverProc>(fStartAddress)(fHandover); } - return kEfiOk; + return kEfiFail; } const Char* BootThread::GetName() @@ -216,4 +194,4 @@ namespace Boot { return fStartAddress != nullptr; } -} // namespace Boot +} // namespace Boot
\ No newline at end of file diff --git a/dev/boot/src/HEL/AMD64/BootAPI.S b/dev/boot/src/HEL/AMD64/BootAPI.S index c4a7ad39..963ef46a 100644 --- a/dev/boot/src/HEL/AMD64/BootAPI.S +++ b/dev/boot/src/HEL/AMD64/BootAPI.S @@ -11,11 +11,16 @@ rt_jump_to_address: mov rbx, rcx mov rcx, rdx + push rbx + push rdx mov rsp, r8 - - cli + push rax jmp rbx + pop rdx + pop rbx + pop rax + ret rt_reset_hardware: diff --git a/dev/boot/src/HEL/AMD64/BootEFI.cc b/dev/boot/src/HEL/AMD64/BootEFI.cc index 4220c6cb..fa0cb74b 100644 --- a/dev/boot/src/HEL/AMD64/BootEFI.cc +++ b/dev/boot/src/HEL/AMD64/BootEFI.cc @@ -73,27 +73,32 @@ STATIC Bool boot_init_fb() noexcept EfiGUID kEfiGlobalNamespaceVarGUID = { 0x8BE4DF61, 0x93CA, 0x11D2, {0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C}}; -/// @brief ModuleMain EFI entrypoint. +/// @brief BootloaderMain EFI entrypoint. /// @param image_handle Handle of this image. /// @param sys_table The system table of it. /// @return nothing, never returns. -EFI_EXTERN_C EFI_API Int32 ModuleMain(EfiHandlePtr image_handle, - EfiSystemTable* sys_table) +EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, + EfiSystemTable* sys_table) { - InitEFI(sys_table); ///! Init the EFI library. + fw_init_efi(sys_table); ///! Init the EFI library. HEL::BootInfoHeader* handover_hdr = new HEL::BootInfoHeader(); UInt32 map_key = 0; + UInt32 size_struct_ptr = sizeof(EfiMemoryDescriptor); + EfiMemoryDescriptor* struct_ptr = nullptr; + UInt32 sz_desc = sizeof(EfiMemoryDescriptor); + UInt32 rev_desc = 0; + + Boot::BootTextWriter writer; + + writer.Write("BootZ: NeKernel Loader.\r"); -#ifdef ZBA_USE_FB if (!boot_init_fb()) { - Boot::BootTextWriter writer; writer.Write("BootZ: Invalid Framebuffer, can't boot NeKernel.\r"); - - CANT_REACH(); + Boot::Stop(); } for (SizeT index_vt = 0; index_vt < sys_table->NumberOfTableEntries; @@ -123,7 +128,6 @@ EFI_EXTERN_C EFI_API Int32 ModuleMain(EfiHandlePtr image_handle, handover_hdr->f_GOP.f_PixelPerLine = kGop->Mode->Info->PixelsPerScanLine; handover_hdr->f_GOP.f_PixelFormat = kGop->Mode->Info->PixelFormat; handover_hdr->f_GOP.f_Size = kGop->Mode->FrameBufferSize; -#endif // ZBA_USE_FB // ------------------------------------------- // // Grab MP services, extended to runtime. // @@ -138,12 +142,10 @@ EFI_EXTERN_C EFI_API Int32 ModuleMain(EfiHandlePtr image_handle, kHandoverHeader = handover_hdr; - fb_init(); - FB::fb_clear_video(); + fb_init(); FBDrawBitMapInRegion(zka_disk, NE_DISK_WIDTH, NE_DISK_HEIGHT, (kHandoverHeader->f_GOP.f_Width - NE_DISK_WIDTH) / 2, (kHandoverHeader->f_GOP.f_Height - NE_DISK_HEIGHT) / 2); - fb_clear(); UInt32 cnt_enabled = 0; @@ -161,14 +163,12 @@ EFI_EXTERN_C EFI_API Int32 ModuleMain(EfiHandlePtr image_handle, // Fill handover header now. - Boot::BootTextWriter writer; + handover_hdr->f_BitMapStart = nullptr; /* Start of bitmap. */ + handover_hdr->f_BitMapSize = kHandoverBitMapSz; /* Size of bitmap in bytes. */ - //-------------------------------------------------------------// - // Update handover file specific table and phyiscal start field. - //-------------------------------------------------------------// + Int32 trials = 5 * 10000000; - handover_hdr->f_BitMapSize = gib_cast(4); /* Size of bitmap in bytes. */ - Int32 trials = 5 * 10000000; + writer.Write("BootZ: Allocating sufficent memory, trying with 4GB...\r"); while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, &handover_hdr->f_BitMapStart) != kEfiOk) { @@ -180,7 +180,7 @@ EFI_EXTERN_C EFI_API Int32 ModuleMain(EfiHandlePtr image_handle, trials = 3 * 10000000; - handover_hdr->f_BitMapSize = gib_cast(2); /* Size of bitmap in bytes. */ + handover_hdr->f_BitMapSize = kHandoverBitMapSz / 2; /* Size of bitmap in bytes. */ while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, &handover_hdr->f_BitMapStart) != kEfiOk) { @@ -214,24 +214,12 @@ EFI_EXTERN_C EFI_API Int32 ModuleMain(EfiHandlePtr image_handle, if (syschk_thread->Start(handover_hdr, NO) != kEfiOk) { - fb_init(); - FB::fb_clear_video(); - FBDrawBitMapInRegion(zka_no_disk, NE_NO_DISK_WIDTH, NE_NO_DISK_HEIGHT, (kHandoverHeader->f_GOP.f_Width - NE_NO_DISK_WIDTH) / 2, (kHandoverHeader->f_GOP.f_Height - NE_NO_DISK_HEIGHT) / 2); - - fb_clear(); - - Boot::Stop(); } } - // ------------------------------------------ // - // null these fields, to avoid being reused later. - // ------------------------------------------ // - - handover_hdr->f_FirmwareCustomTables[0] = nullptr; - handover_hdr->f_FirmwareCustomTables[1] = nullptr; + BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); @@ -267,60 +255,45 @@ EFI_EXTERN_C EFI_API Int32 ModuleMain(EfiHandlePtr image_handle, ver = KERNEL_VERSION_BCD; ST->RuntimeServices->SetVariable(L"/props/kern_ver", kEfiGlobalNamespaceVarGUID, nullptr, &sz_ver, &ver); - writer.Write("BootZ: Kernel Version Updated: ").Write(ver).Write("\r"); + writer.Write("BootZ: version has been updated: ").Write(ver).Write("\r"); } - writer.Write("BootZ: Kernel Version: ").Write(ver).Write("\r"); + writer.Write("BootZ: version: ").Write(ver).Write("\r"); + + // boot to kernel, if not netboot this. Boot::BootFileReader reader_kernel(kernel_path, image_handle); reader_kernel.ReadAll(0); - Boot::BootThread* kernel_thread = nullptr; - // ------------------------------------------ // // If we succeed in reading the blob, then execute it. // ------------------------------------------ // if (reader_kernel.Blob()) { - kernel_thread = new Boot::BootThread(reader_kernel.Blob()); - kernel_thread->SetName("BootZ: Kernel"); + // ------------------------------------------ // + // null these fields, to avoid being reused later. + // ------------------------------------------ // + + auto kernel_thread = Boot::BootThread(reader_kernel.Blob()); + + kernel_thread.SetName("BootZ: Kernel"); handover_hdr->f_KernelImage = reader_kernel.Blob(); - } - else - { - fb_init(); - FBDrawBitMapInRegion(zka_no_disk, NE_NO_DISK_WIDTH, NE_NO_DISK_HEIGHT, (kHandoverHeader->f_GOP.f_Width - NE_NO_DISK_WIDTH) / 2, (kHandoverHeader->f_GOP.f_Height - NE_NO_DISK_HEIGHT) / 2); + handover_hdr->f_KernelSz = reader_kernel.Size(); - Boot::Stop(); + kernel_thread.Start(handover_hdr, YES); } Boot::BootFileReader reader_netboot(L"net.efi", image_handle); reader_netboot.ReadAll(0); - Boot::BootThread* netboot_thread = nullptr; + if (!reader_netboot.Blob()) + return kEfiFail; - // ---------------------------------------------------- // - // Finally load the OS kernel. - // ---------------------------------------------------- // - - Boot::ExitBootServices(map_key, image_handle); - - if (kernel_thread->Start(handover_hdr, YES) != kEfiOk) - { - // ------------------------------------------ // - // If we fail into booting the kernel, then run BootNet. - // ------------------------------------------ // - - if (reader_netboot.Blob()) - { - netboot_thread = new Boot::BootThread(reader_netboot.Blob()); - netboot_thread->SetName("BootZ: BootNet"); - netboot_thread->Start(handover_hdr, YES); - } - } + auto netboot_thread = Boot::BootThread(reader_netboot.Blob()); + netboot_thread.SetName("BootZ: BootNet"); - CANT_REACH(); + return netboot_thread.Start(handover_hdr, NO); } diff --git a/dev/boot/src/HEL/ARM64/BootEFI.cc b/dev/boot/src/HEL/ARM64/BootEFI.cc index b6f28351..8008d846 100644 --- a/dev/boot/src/HEL/ARM64/BootEFI.cc +++ b/dev/boot/src/HEL/ARM64/BootEFI.cc @@ -18,17 +18,12 @@ #include <BootKit/BootThread.h> #include <modules/CoreGfx/CoreGfx.h> -// Makes the compiler shut up. -#ifndef kMachineModel -#define kMachineModel "NeKernel" -#endif // !kMachineModel - #ifndef kExpectedWidth -#define kExpectedWidth (1920) +#define kExpectedWidth (800) #endif #ifndef kExpectedHeight -#define kExpectedHeight (1080) +#define kExpectedHeight (600) #endif /** Graphics related. */ @@ -74,23 +69,19 @@ STATIC Bool boot_init_fb() noexcept EXTERN EfiBootServices* BS; -/// @brief ModuleMain EFI entrypoint. +/// @brief BootloaderMain EFI entrypoint. /// @param image_handle Handle of this image. /// @param sys_table The system table of it. /// @return nothing, never returns. -EFI_EXTERN_C EFI_API Int32 ModuleMain(EfiHandlePtr image_handle, - EfiSystemTable* sys_table) +EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, + EfiSystemTable* sys_table) { - InitEFI(sys_table); ///! Init the EFI library. + fw_init_efi(sys_table); ///! Init the EFI library. HEL::BootInfoHeader* handover_hdr = new HEL::BootInfoHeader(); UInt32 map_key = 0; - UInt32 size_struct_ptr = sizeof(EfiMemoryDescriptor); - EfiMemoryDescriptor* struct_ptr = nullptr; - UInt32 sz_desc = sizeof(EfiMemoryDescriptor); - UInt32 rev_desc = 0; #ifdef ZBA_USE_FB if (!boot_init_fb()) @@ -133,9 +124,9 @@ EFI_EXTERN_C EFI_API Int32 ModuleMain(EfiHandlePtr image_handle, EfiMpServicesProtocol* mp = nullptr; BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast<VoidPtr*>(&mp)); - handover_hdr->f_HardwareTables.f_MpPtr = reinterpret_cast<VoidPtr>(mp); + // Assign to global 'kHandoverHeader'. kHandoverHeader = handover_hdr; fb_init(); @@ -160,18 +151,59 @@ EFI_EXTERN_C EFI_API Int32 ModuleMain(EfiHandlePtr image_handle, } //-------------------------------------------------------------// - // Update handover file specific table and phyiscal start field. + // Allocate heap. //-------------------------------------------------------------// + Boot::BootTextWriter writer; + handover_hdr->f_BitMapStart = nullptr; /* Start of bitmap. */ - handover_hdr->f_BitMapSize = kHandoverBitMapSz; /* Size of bitmap. */ + handover_hdr->f_BitMapSize = kHandoverBitMapSz; /* Size of bitmap in bytes. */ + Int32 trials = 5 * 10000000; while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, &handover_hdr->f_BitMapStart) != kEfiOk) { - if (handover_hdr->f_BitMapStart) + --trials; + + if (!trials) + { + writer.Write("BootZ: Unable to allocate sufficent memory, trying again with 2GB...\r"); + + trials = 3 * 10000000; + + handover_hdr->f_BitMapSize = kHandoverBitMapSz / 2; /* Size of bitmap in bytes. */ + + while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, &handover_hdr->f_BitMapStart) != kEfiOk) + { + --trials; + + if (!trials) + { + writer.Write("BootZ: Unable to allocate sufficent memory, aborting...\r"); + Boot::Stop(); + } + } + } + } + + Boot::BootFileReader reader_syschk(L"chk.efi", image_handle); + reader_syschk.ReadAll(0); + + Boot::BootThread* syschk_thread = nullptr; + + if (reader_syschk.Blob()) + { + syschk_thread = new Boot::BootThread(reader_syschk.Blob()); + syschk_thread->SetName("BootZ: System Check"); + + if (syschk_thread->Start(handover_hdr, NO) != kEfiOk) { - BS->FreePool(handover_hdr->f_BitMapStart); - handover_hdr->f_BitMapStart = nullptr; + fb_init(); + + FB::fb_clear_video(); + + FBDrawBitMapInRegion(zka_no_disk, NE_NO_DISK_WIDTH, NE_NO_DISK_HEIGHT, (kHandoverHeader->f_GOP.f_Width - NE_NO_DISK_WIDTH) / 2, (kHandoverHeader->f_GOP.f_Height - NE_NO_DISK_HEIGHT) / 2); + + fb_clear(); } } @@ -194,8 +226,6 @@ EFI_EXTERN_C EFI_API Int32 ModuleMain(EfiHandlePtr image_handle, handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); - // Assign to global 'kHandoverHeader'. - Boot::BootFileReader reader_kernel(L"vmkrnl.efi", image_handle); reader_kernel.ReadAll(0); @@ -207,9 +237,10 @@ EFI_EXTERN_C EFI_API Int32 ModuleMain(EfiHandlePtr image_handle, if (reader_kernel.Blob()) { auto kernel_thread = Boot::BootThread(reader_kernel.Blob()); - kernel_thread.SetName("BootZ: MicroKernel."); + kernel_thread.SetName("BootZ: Kernel."); handover_hdr->f_KernelImage = reader_kernel.Blob(); + handover_hdr->f_KernelSz = reader_kernel.Size(); Boot::ExitBootServices(map_key, image_handle); diff --git a/dev/kernel/CompilerKit/Version.h b/dev/kernel/CompilerKit/Version.h index 0d53b5ed..0f4f863d 100644 --- a/dev/kernel/CompilerKit/Version.h +++ b/dev/kernel/CompilerKit/Version.h @@ -6,5 +6,5 @@ #define BOOTLOADER_VERSION "1104.2025.110" #define KERNEL_VERSION "1104.2025.110" -#define BOOTLOADER_VERSION_BCD 0x11042025110 -#define KERNEL_VERSION_BCD 0x11042025110 +#define BOOTLOADER_VERSION_BCD 0x20250415 +#define KERNEL_VERSION_BCD 0x20250415 diff --git a/dev/kernel/FSKit/NeFS.h b/dev/kernel/FSKit/NeFS.h index 4b54afc8..3c11fdb4 100644 --- a/dev/kernel/FSKit/NeFS.h +++ b/dev/kernel/FSKit/NeFS.h @@ -336,8 +336,8 @@ namespace Kernel public: STATIC const Char* Root(); STATIC const Char* UpDir(); - STATIC Char Separator(); - STATIC Char MetaFile(); + STATIC Char Separator(); + STATIC Char MetaFile(); }; /// @brief Journal class for NeFS. diff --git a/dev/kernel/FirmwareKit/CoreBoot/BootNet.h b/dev/kernel/FirmwareKit/CoreBoot/BootNet.h index e3cda0ba..c2bfe161 100644 --- a/dev/kernel/FirmwareKit/CoreBoot/BootNet.h +++ b/dev/kernel/FirmwareKit/CoreBoot/BootNet.h @@ -26,5 +26,5 @@ typedef struct BOOTNET_INTERNET_HEADER Kernel::Char Target[kBootNetNameLen]; /// the target file. Kernel::Boolean ImpliesProgram : 1; /// does it imply an EEPROM program? Kernel::Boolean Preflight : 1; /// is it a preflight packet. - Kernel::Char Data[]; /// non preflight packet has a patch blob for a **PatchTarget** + Kernel::Char Data[1]; /// non preflight packet has a patch blob for a **PatchTarget** } ATTRIBUTE(packed) BOOTNET_INTERNET_HEADER; diff --git a/dev/kernel/FirmwareKit/CoreBoot/CoreBoot.h b/dev/kernel/FirmwareKit/CoreBoot/CoreBoot.h index f5470877..e1f3fbc6 100644 --- a/dev/kernel/FirmwareKit/CoreBoot/CoreBoot.h +++ b/dev/kernel/FirmwareKit/CoreBoot/CoreBoot.h @@ -24,12 +24,12 @@ namespace Firmware::Detail::CoreBoot const UInt32 fStartAddress; // start address (master/slave(s) thread) #ifdef NE_IS_EXTENDED_COREBOOT - UIntPtr fMasterStructure; // master structure for MP/PM and device tree and such. (ARM) + UIntPtr fMasterStructure; // master structure for MP/PM and device tree and such. (ARM) UIntPtr fMasterStructureVersion; // master structure version. #endif #ifdef NE_IS_MBCI_COREBOOT - UIntPtr fMBCIStructure; // MBCI structure for MBCI (ARM) + UIntPtr fMBCIStructure; // MBCI structure for MBCI (ARM) UIntPtr fMBCIStructureVersion; // MBCI structure version. #endif }; diff --git a/dev/kernel/FirmwareKit/EFI/API.h b/dev/kernel/FirmwareKit/EFI/API.h index 403e017e..f3b03bb5 100644 --- a/dev/kernel/FirmwareKit/EFI/API.h +++ b/dev/kernel/FirmwareKit/EFI/API.h @@ -48,7 +48,7 @@ namespace Boot @brief Exit EFI API to let the OS load correctly. Bascially frees everything we have in the EFI side. */ - inline void ExitBootServices(UInt64 MapKey, EfiHandlePtr ImageHandle) noexcept + inline Void ExitBootServices(UInt64 MapKey, EfiHandlePtr ImageHandle) noexcept { if (!ST) return; @@ -83,7 +83,7 @@ Bascially frees everything we have in the EFI side. } } // namespace Boot -inline void InitEFI(EfiSystemTable* SystemTable) noexcept +inline void fw_init_efi(EfiSystemTable* SystemTable) noexcept { if (!SystemTable) return; diff --git a/dev/kernel/FirmwareKit/EFI/EFI.h b/dev/kernel/FirmwareKit/EFI/EFI.h index 1e6de42c..24f474d1 100644 --- a/dev/kernel/FirmwareKit/EFI/EFI.h +++ b/dev/kernel/FirmwareKit/EFI/EFI.h @@ -21,10 +21,16 @@ using namespace Kernel; #define EFI_API __attribute__((ms_abi)) #endif // ifndef EPI_API +#ifndef EPIAPI +#define EFIAPI __attribute__((ms_abi)) +#endif // ifndef EPIAPI + #define IN #define OUT #define OPTIONAL +#define EFI_STATUS EfiStatusType + #define EFI_FINAL final // Forward decls @@ -285,6 +291,24 @@ typedef struct EfiTableHeader } \ } +#define EFI_SIMPLE_NETWORK_PROTOCOL_GUID \ + { \ + 0xA19832B9, 0xAC25, 0x11D3, \ + { \ + 0x9A, 0x2D, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d \ + } \ + } + +#define EFI_SIMPLE_NETWORK_PROTOCOL_REVISION 0x00010000 + +#define EFI_IP4_PROTOCOL_GUID \ + { \ + 0x41d94cd2, 0x35b6, 0x455a, \ + { \ + 0x82, 0x58, 0xd4, 0xe5, 0x13, 0x34, 0xaa, 0xdd \ + } \ + } + #define EFI_LOADED_IMAGE_PROTOCOL_REVISION 0x1000 #define EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID \ @@ -330,6 +354,91 @@ enum kPixelFormatMax }; +struct EFI_SIMPLE_NETWORK_PROTOCOL; + +typedef EFI_STATUS(EFI_API* EFI_SIMPLE_NETWORK_START)( + IN EFI_SIMPLE_NETWORK_PROTOCOL* This); + +typedef EFI_STATUS(EFI_API* EFI_SIMPLE_NETWORK_STOP)( + IN EFI_SIMPLE_NETWORK_PROTOCOL* This); + +typedef EFI_STATUS(EFI_API* EFI_SIMPLE_NETWORK_INITIALIZE)( + IN EFI_SIMPLE_NETWORK_PROTOCOL* This, + IN UInt32 ExtraRxBufferSize OPTIONAL, + IN UInt32 ExtraTxBufferSize OPTIONAL); + +typedef EFI_STATUS(EFIAPI* EFI_SIMPLE_NETWORK_RESET)( + IN EFI_SIMPLE_NETWORK_PROTOCOL* This, + IN Boolean ExtendedVerification); + +typedef EFI_STATUS(EFIAPI* EFI_SIMPLE_NETWORK_SHUTDOWN)( + IN EFI_SIMPLE_NETWORK_PROTOCOL* This); + +typedef UInt8 EfiMacAddress[32]; + +#define MAX_MCAST_FILTER_CNT 16 + +typedef struct +{ + UInt32 State; + UInt32 HwAddressSize; + UInt32 MediaHeaderSize; + UInt32 MaxPacketSize; + UInt32 NvRamSize; + UInt32 NvRamAccessSize; + UInt32 ReceiveFilterMask; + UInt32 ReceiveFilterSetting; + UInt32 MaxMCastFilterCount; + UInt32 MCastFilterCount; + EfiMacAddress MCastFilter[MAX_MCAST_FILTER_CNT]; + EfiMacAddress CurrentAddress; + EfiMacAddress BroadcastAddress; + EfiMacAddress PermanentAddress; + UInt8 IfType; + BOOL MacAddressChangeable; + BOOL MultipleTxSupported; + BOOL MediaPresentSupported; + BOOL MediaPresent; +} EFI_SIMPLE_NETWORK_MODE; + +typedef EFI_STATUS(EFIAPI* EFI_SIMPLE_NETWORK_TRANSMIT)( + IN EFI_SIMPLE_NETWORK_PROTOCOL* This, + IN UInt32 HeaderSize, + IN UInt32 BufferSize, + IN Void* Buffer, + IN EfiMacAddress* SrcAddr OPTIONAL, + IN EfiMacAddress* DestAddr OPTIONAL, + IN UInt16* Protocol OPTIONAL); + +typedef EFI_STATUS(EFIAPI* EFI_SIMPLE_NETWORK_RECEIVE)( + IN EFI_SIMPLE_NETWORK_PROTOCOL* This, + OUT UInt32* HeaderSize OPTIONAL, + IN OUT UInt32* BufferSize, + OUT Void* Buffer, + OUT EfiMacAddress* SrcAddr OPTIONAL, + OUT EfiMacAddress* DestAddr OPTIONAL, + OUT UInt16* Protocol OPTIONAL); + +typedef struct EFI_SIMPLE_NETWORK_PROTOCOL +{ + UInt64 Revision; + EFI_SIMPLE_NETWORK_START Start; + EFI_SIMPLE_NETWORK_STOP Stop; + EFI_SIMPLE_NETWORK_INITIALIZE Initialize; + EFI_SIMPLE_NETWORK_RESET Reset; + EFI_SIMPLE_NETWORK_SHUTDOWN Shutdown; + VoidPtr ReceiveFilters; + VoidPtr StationAddress; + VoidPtr Statistics; + VoidPtr MCastIpToMac; + VoidPtr NvData; + VoidPtr GetStatus; + EFI_SIMPLE_NETWORK_TRANSMIT Transmit; + EFI_SIMPLE_NETWORK_RECEIVE Receive; + VoidPtr WaitForPacket; + EFI_SIMPLE_NETWORK_MODE* Mode; +} EFI_SIMPLE_NETWORK_PROTOCOL; + typedef struct EfiBitmask { UInt32 RedMask; diff --git a/dev/kernel/FirmwareKit/GPT.h b/dev/kernel/FirmwareKit/GPT.h index c903f3ab..a8b53ead 100644 --- a/dev/kernel/FirmwareKit/GPT.h +++ b/dev/kernel/FirmwareKit/GPT.h @@ -10,7 +10,7 @@ #include <FirmwareKit/EFI/EFI.h> #define kSectorSizeGPT (512U) -#define kPartNameGPT (8U) +#define kPartNameGPT (8U) namespace Kernel { diff --git a/dev/kernel/FirmwareKit/Handover.h b/dev/kernel/FirmwareKit/Handover.h index 22d96ca9..ecdaca64 100644 --- a/dev/kernel/FirmwareKit/Handover.h +++ b/dev/kernel/FirmwareKit/Handover.h @@ -17,6 +17,7 @@ #pragma once +#include "FirmwareKit/EFI/EFI.h" #include <NewKit/Defines.h> #define kHandoverMagic 0xBADCC @@ -70,6 +71,11 @@ namespace Kernel::HEL WideChar f_FirmwareVendorName[32]; SizeT f_FirmwareVendorLen; +#ifdef __NE_AMD64__ + UInt32 f_EFIImageKey; + EfiHandlePtr f_EFIImage; +#endif + VoidPtr f_FirmwareCustomTables[2]; // On EFI 0: BS 1: ST struct diff --git a/dev/kernel/HALKit/AMD64/HalDebugOutput.cc b/dev/kernel/HALKit/AMD64/HalDebugOutput.cc index 0ee7eef0..b69614fd 100644 --- a/dev/kernel/HALKit/AMD64/HalDebugOutput.cc +++ b/dev/kernel/HALKit/AMD64/HalDebugOutput.cc @@ -181,7 +181,7 @@ namespace Kernel TerminalDevice TerminalDevice::The() noexcept { - static TerminalDevice out(Kernel::ke_io_write, Kernel::ke_io_read); + TerminalDevice out(Kernel::ke_io_write, Kernel::ke_io_read); return out; } diff --git a/dev/kernel/HALKit/AMD64/HalKernelMain.cc b/dev/kernel/HALKit/AMD64/HalKernelMain.cc index cc3c8e93..60df6a12 100644 --- a/dev/kernel/HALKit/AMD64/HalKernelMain.cc +++ b/dev/kernel/HALKit/AMD64/HalKernelMain.cc @@ -4,6 +4,7 @@ ------------------------------------------- */ +#include "modules/CoreGfx/CoreGfx.h" #include <StorageKit/AHCI.h> #include <ArchKit/ArchKit.h> #include <KernelKit/ProcessScheduler.h> @@ -13,6 +14,11 @@ #include <NetworkKit/IPC.h> #include <CFKit/Property.h> #include <modules/CoreGfx/TextGfx.h> +#include <KernelKit/Timer.h> + +#include <FirmwareKit/EFI/API.h> +#include <FirmwareKit/EFI/EFI.h> + EXTERN_C Kernel::VoidPtr kInterruptVectorTable[]; EXTERN_C Kernel::VoidPtr mp_user_switch_proc; @@ -27,31 +33,25 @@ STATIC Kernel::Void hal_init_scheduler_team() } } -STATIC Kernel::UInt64 hal_rdtsc_fn() +/// @brief Kernel init procedure. +EXTERN_C Int32 hal_init_platform( + Kernel::HEL::BootInfoHeader* handover_hdr) { - Kernel::UInt32 lo, hi; - __asm__ volatile("rdtsc" - : "=a"(lo), "=d"(hi)); + if (handover_hdr->f_Magic != kHandoverMagic && + handover_hdr->f_Version != kHandoverVersion) + { + return kEfiFail; + } - return ((Kernel::UInt64)hi << 32) | lo; -} + FB::fb_clear_video(); -STATIC Kernel::UInt64 kStartTim, kEndTim; + (Void)(Kernel::kout << "Welcome to NeKernel.\r"); -/// @brief Kernel init procedure. -EXTERN_C void hal_init_platform( - Kernel::HEL::BootInfoHeader* handover_hdr) -{ - kStartTim = hal_rdtsc_fn(); + fw_init_efi((EfiSystemTable*)handover_hdr->f_FirmwareCustomTables[1]); + Boot::ExitBootServices(handover_hdr->f_EFIImageKey, handover_hdr->f_EFIImage); kHandoverHeader = handover_hdr; - if (kHandoverHeader->f_Magic != kHandoverMagic && - kHandoverHeader->f_Version != kHandoverVersion) - { - return; - } - hal_init_scheduler_team(); /************************************** */ @@ -83,35 +83,24 @@ EXTERN_C void hal_init_platform( gdt_reg.Base = reinterpret_cast<Kernel::UIntPtr>(kGDTArray); gdt_reg.Limit = (sizeof(Kernel::HAL::Detail::NE_GDT_ENTRY) * kGDTEntriesCount) - 1; - FB::fb_clear_video(); + Kernel::NeFS::fs_init_nefs(); + + Kernel::HAL::mp_get_cores(kHandoverHeader->f_HardwareTables.f_VendorPtr); //! GDT will load hal_read_init after it successfully loads the segments. Kernel::HAL::GDTLoader gdt_loader; gdt_loader.Load(gdt_reg); - Kernel::ke_panic(RUNTIME_CHECK_BOOTSTRAP); + return kEfiFail; } EXTERN_C Kernel::Void hal_real_init(Kernel::Void) noexcept { - kEndTim = hal_rdtsc_fn(); - - (void)(Kernel::kout << "Boot Time: " << Kernel::number(kEndTim - kStartTim) << Kernel::kendl); - - Kernel::NeFS::fs_init_nefs(); - - Kernel::HAL::mp_get_cores(kHandoverHeader->f_HardwareTables.f_VendorPtr); - Kernel::HAL::Register64 idt_reg; - idt_reg.Base = (Kernel::UIntPtr)kInterruptVectorTable; Kernel::HAL::IDTLoader idt_loader; - kEndTim = hal_rdtsc_fn(); - - (void)(Kernel::kout << "Init Time: " << Kernel::number(kEndTim - kStartTim) << Kernel::kendl); - idt_loader.Load(idt_reg); dbg_break_point(); diff --git a/dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc b/dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc index 8bd98923..4b50a3f4 100644 --- a/dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc +++ b/dev/kernel/HALKit/AMD64/HalPagingMgrAMD64.cc @@ -14,6 +14,7 @@ namespace Kernel::HAL { namespace Detail { + /// @brief Page Table Entry for AMD64. struct PTE { UInt64 Present : 1; @@ -28,7 +29,7 @@ namespace Kernel::HAL UInt64 Ignored1 : 3; // Available to software UInt64 PhysicalAddress : 40; // Physical page frame address (bits 12–51) UInt64 Ignored2 : 7; // More software bits / reserved - UInt64 Protection_key : 4; // Optional (if PKU enabled) + UInt64 ProtectionKey : 4; // Optional (if PKU enabled) UInt64 Reserved : 1; // Usually reserved UInt64 Nx : 1; // No Execute }; @@ -45,6 +46,9 @@ namespace Kernel::HAL (void)(kout << (pte->Nx ? "NX" : "Not NX") << kendl); (void)(kout << (pte->User ? "User" : "Not User") << kendl); (void)(kout << (pte->Pcd ? "Not Cached" : "Cached") << kendl); + (void)(kout << (pte->Accessed ? "Accessed" : "Not Accessed") << kendl); + (void)(kout << hex_number(pte->PhysicalAddress) << kendl); + (void)(kout << (pte->ProtectionKey ? "Protected" : "Not Protected/PKU Disabled") << kendl); } /***********************************************************************************/ @@ -54,31 +58,29 @@ namespace Kernel::HAL /***********************************************************************************/ UIntPtr hal_get_phys_address(VoidPtr virt) { - const UInt64 vaddr = (UInt64)virt; + const UInt64 kVMAddr = (UInt64)virt; const UInt64 kMask9Bits = 0x1FFULL; const UInt64 kPageOffsetMask = 0xFFFULL; UInt64 cr3 = (UInt64)hal_read_cr3() & ~kPageOffsetMask; - hal_invl_tlb(virt); - // Level 4 auto pml4 = reinterpret_cast<UInt64*>(cr3); - UInt64 pml4e = pml4[(vaddr >> 39) & kMask9Bits]; + UInt64 pml4e = pml4[(kVMAddr >> 39) & kMask9Bits]; if (!(pml4e & 1)) return 0; // Level 3 auto pdpt = reinterpret_cast<UInt64*>(pml4e & ~kPageOffsetMask); - UInt64 pdpte = pdpt[(vaddr >> 30) & kMask9Bits]; + UInt64 pdpte = pdpt[(kVMAddr >> 30) & kMask9Bits]; if (!(pdpte & 1)) return 0; // Level 2 auto pd = reinterpret_cast<UInt64*>(pdpte & ~kPageOffsetMask); - UInt64 pde = pd[(vaddr >> 21) & kMask9Bits]; + UInt64 pde = pd[(kVMAddr >> 21) & kMask9Bits]; if (!(pde & 1)) return 0; @@ -86,12 +88,12 @@ namespace Kernel::HAL // 1 GiB page support if (pde & (1 << 7)) { - return (pde & ~((1ULL << 30) - 1)) | (vaddr & ((1ULL << 30) - 1)); + return (pde & ~((1ULL << 30) - 1)) | (kVMAddr & ((1ULL << 30) - 1)); } // Level 1 - auto pt = reinterpret_cast<UInt64*>(pde & ~kPageOffsetMask); - Detail::PTE* pte = (Detail::PTE*)pt[(vaddr >> 12) & kMask9Bits]; + auto pt = reinterpret_cast<UInt64*>(pde & ~kPageOffsetMask); + Detail::PTE* pte = (Detail::PTE*)pt[(kVMAddr >> 12) & kMask9Bits]; if (!pte->Present) return 0; @@ -110,46 +112,44 @@ namespace Kernel::HAL /***********************************************************************************/ EXTERN_C Int32 mm_map_page(VoidPtr virtual_address, VoidPtr physical_address, UInt32 flags) { - const UInt64 vaddr = (UInt64)virtual_address; + const UInt64 kVMAddr = (UInt64)virtual_address; constexpr UInt64 kMask9 = 0x1FF; constexpr UInt64 kPageMask = 0xFFF; UInt64 cr3 = (UIntPtr)hal_read_cr3() & ~kPageMask; auto pml4 = reinterpret_cast<UInt64*>(cr3); - UInt64 pml4e = pml4[(vaddr >> 39) & kMask9]; + UInt64 pml4e = pml4[(kVMAddr >> 39) & kMask9]; if (!(pml4e & 1)) return kErrorInvalidData; - auto pdpt = reinterpret_cast<UInt64*>(pml4e & ~kPageMask); - UInt64 pdpte = pdpt[(vaddr >> 30) & kMask9]; + UInt64* pdpt = reinterpret_cast<UInt64*>(pml4e & ~kPageMask); + UInt64 pdpte = pdpt[(kVMAddr >> 30) & kMask9]; if (!(pdpte & 1)) return kErrorInvalidData; - auto pd = reinterpret_cast<UInt64*>(pdpte & ~kPageMask); - UInt64 pde = pd[(vaddr >> 21) & kMask9]; + UInt64* pd = reinterpret_cast<UInt64*>(pdpte & ~kPageMask); + UInt64 pde = pd[(kVMAddr >> 21) & kMask9]; if (!(pde & 1)) return kErrorInvalidData; UInt64* pt = reinterpret_cast<UInt64*>(pde & ~kPageMask); - Detail::PTE* pte = (Detail::PTE*)pt[(vaddr >> 12) & kMask9]; + Detail::PTE* pte = (Detail::PTE*)pt[(kVMAddr >> 12) & kMask9]; - pte->Present = !!(flags & kMMFlagsPresent); - pte->Wr = !!(flags & kMMFlagsWr); - pte->User = !!(flags & kMMFlagsUser); - pte->Nx = !!(flags & kMMFlagsNX); - pte->Pcd = !(flags & kMMFlagsUncached); + pte->Present = !!(flags & kMMFlagsPresent); + pte->Wr = !!(flags & kMMFlagsWr); + pte->User = !!(flags & kMMFlagsUser); + pte->Nx = !!(flags & kMMFlagsNX); + pte->Pcd = !(flags & kMMFlagsUncached); + pte->PhysicalAddress = (UIntPtr)(physical_address); - if (physical_address) - pte->PhysicalAddress = (UIntPtr)physical_address; + mmi_page_status(pte); hal_invl_tlb(virtual_address); - mmi_page_status(pte); - return kErrorSuccess; } } // namespace Kernel::HAL diff --git a/dev/kernel/HALKit/AMD64/HalSchedulerCorePrimitivesAMD64.cc b/dev/kernel/HALKit/AMD64/HalSchedulerCorePrimitivesAMD64.cc index ba5710e3..edad0e97 100644 --- a/dev/kernel/HALKit/AMD64/HalSchedulerCorePrimitivesAMD64.cc +++ b/dev/kernel/HALKit/AMD64/HalSchedulerCorePrimitivesAMD64.cc @@ -46,7 +46,7 @@ namespace Kernel Void mp_hang_thread(HAL::StackFrame* stack) { NE_UNUSED(stack); - + while (Yes) { /* Nothing to do, code is spinning */ diff --git a/dev/kernel/HALKit/AMD64/HalTimerAMD64.cc b/dev/kernel/HALKit/AMD64/HalTimerAMD64.cc index 01be2be5..72de8eb0 100644 --- a/dev/kernel/HALKit/AMD64/HalTimerAMD64.cc +++ b/dev/kernel/HALKit/AMD64/HalTimerAMD64.cc @@ -45,7 +45,7 @@ namespace Kernel::Detail using namespace Kernel;
-HardwareTimer::HardwareTimer(Int64 ms)
+HardwareTimer::HardwareTimer(UInt64 ms)
: fWaitFor(ms)
{
auto power = PowerFactoryInterface(kHandoverHeader->f_HardwareTables.f_VendorPtr);
diff --git a/dev/kernel/HALKit/AMD64/Paging.h b/dev/kernel/HALKit/AMD64/Paging.h index da90a71a..ea20dece 100644 --- a/dev/kernel/HALKit/AMD64/Paging.h +++ b/dev/kernel/HALKit/AMD64/Paging.h @@ -64,7 +64,6 @@ namespace Kernel::HAL } } // namespace Detail - auto mm_alloc_bitmap(Boolean wr, Boolean user, SizeT size, Bool is_page, SizeT pad = 0) -> VoidPtr; auto mm_free_bitmap(VoidPtr page_ptr) -> Bool; } // namespace Kernel::HAL diff --git a/dev/kernel/HALKit/AMD64/Processor.h b/dev/kernel/HALKit/AMD64/Processor.h index 084bc962..e375a935 100644 --- a/dev/kernel/HALKit/AMD64/Processor.h +++ b/dev/kernel/HALKit/AMD64/Processor.h @@ -68,13 +68,13 @@ namespace Kernel::HAL /// @brief Memory Manager mapping flags. enum { - kMMFlagsInvalid = 1 << 0, - kMMFlagsPresent = 1 << 1, - kMMFlagsWr = 1 << 2, - kMMFlagsUser = 1 << 3, - kMMFlagsNX = 1 << 4, + kMMFlagsInvalid = 1 << 0, + kMMFlagsPresent = 1 << 1, + kMMFlagsWr = 1 << 2, + kMMFlagsUser = 1 << 3, + kMMFlagsNX = 1 << 4, kMMFlagsUncached = 1 << 5, - kMMFlagsCount = 4, + kMMFlagsCount = 4, }; struct PACKED Register64 final diff --git a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc index 728b20ed..2dfd89a1 100644 --- a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc @@ -152,9 +152,9 @@ STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz /// check for command header. MUST_PASS(command_header); - command_header->Struc.Cfl = sizeof(FisRegH2D) / sizeof(UInt32); + command_header->Struc.Cfl = sizeof(FisRegH2D) / sizeof(UInt32); command_header->Struc.Write = Write; - command_header->Prdtl = 1; + command_header->Prdtl = 8; auto ctba_phys = ((UInt64)command_header->Ctbau << 32) | command_header->Ctba; auto command_table = reinterpret_cast<volatile HbaCmdTbl*>(ctba_phys); diff --git a/dev/kernel/HALKit/ARM64/Processor.h b/dev/kernel/HALKit/ARM64/Processor.h index f0f09578..b108fc82 100644 --- a/dev/kernel/HALKit/ARM64/Processor.h +++ b/dev/kernel/HALKit/ARM64/Processor.h @@ -28,7 +28,8 @@ namespace Kernel::HAL kMMFlagsWr = 1 << 1, kMMFlagsUser = 1 << 2, kMMFlagsNX = 1 << 3, - kMMFlagsCount = 3, + kMMFlagsUncached = 1 << 4, + kMMFlagsCount = 4, }; /// @brief Set a PTE from pd_base. @@ -38,6 +39,8 @@ namespace Kernel::HAL /// @return Status code of page manip. EXTERN_C Int32 mm_map_page(VoidPtr virtual_address, VoidPtr physical_address, UInt32 flags); + EXTERN_C UIntPtr hal_get_phys_address(VoidPtr virtual_address); + typedef UIntPtr Reg; typedef Register64 Register; diff --git a/dev/kernel/KernelKit/DeviceMgr.h b/dev/kernel/KernelKit/DeviceMgr.h index fcafa7a5..9cfc54dd 100644 --- a/dev/kernel/KernelKit/DeviceMgr.h +++ b/dev/kernel/KernelKit/DeviceMgr.h @@ -52,13 +52,13 @@ namespace Kernel IDeviceObject(const IDeviceObject<T>&) = default; public: - virtual IDeviceObject<T>& operator<<(T Data) + virtual IDeviceObject<T>& operator<<(T Data) { fOut(this, Data); return *this; } - virtual IDeviceObject<T>& operator>>(T Data) + virtual IDeviceObject<T>& operator>>(T Data) { fIn(this, Data); return *this; diff --git a/dev/kernel/KernelKit/FileMgr.h b/dev/kernel/KernelKit/FileMgr.h index b4da6d5b..03e74ee8 100644 --- a/dev/kernel/KernelKit/FileMgr.h +++ b/dev/kernel/KernelKit/FileMgr.h @@ -347,7 +347,7 @@ namespace Kernel const Encoding* restrict_type) : fFile(Class::GetMounted()->Open(path, restrict_type)) { - SizeT kRestrictCount = kRestrictMax; + SizeT kRestrictCount = kRestrictMax; const FileRestrictKind kRestrictList[] = { { .fRestrict = kRestrictR, diff --git a/dev/kernel/KernelKit/ProcessScheduler.h b/dev/kernel/KernelKit/ProcessScheduler.h index db6f9aed..cff2ce6b 100644 --- a/dev/kernel/KernelKit/ProcessScheduler.h +++ b/dev/kernel/KernelKit/ProcessScheduler.h @@ -181,7 +181,7 @@ namespace Kernel AffinityKind Affinity{AffinityKind::kStandard}; ProcessStatusKind Status{ProcessStatusKind::kFinished}; UInt8* StackReserve{nullptr}; - ProcessImage Image{}; + ProcessImage Image{}; SizeT StackSize{kSchedMaxStackSz}; IDylibObject* DylibDelegate{nullptr}; SizeT MemoryCursor{0UL}; @@ -205,9 +205,9 @@ namespace Kernel UIntPtr SignalID; }; - ProcessSignal ProcessSignal; + ProcessSignal ProcessSignal; ProcessMemoryHeapList* ProcessMemoryHeap{nullptr}; - ProcessTeam* ProcessParentTeam; + ProcessTeam* ProcessParentTeam; VoidPtr VMRegister{0UL}; @@ -303,13 +303,13 @@ namespace Kernel Array<Process, kSchedProcessLimitPerTeam>& AsArray(); Ref<Process>& AsRef(); - ProcessID& Id() noexcept; + ProcessID& Id() noexcept; public: Array<Process, kSchedProcessLimitPerTeam> mProcessList; Ref<Process> mCurrentProcess; - ProcessID mTeamId{0}; - ProcessID mProcessCount{0}; + ProcessID mTeamId{0}; + ProcessID mProcessCount{0}; }; typedef Array<Process, kSchedProcessLimitPerTeam> UserThreadArray; @@ -337,8 +337,8 @@ namespace Kernel ProcessTeam& CurrentTeam(); public: - ProcessID Spawn(const Char* name, VoidPtr code, VoidPtr image); - Void Remove(ProcessID process_id); + ProcessID Spawn(const Char* name, VoidPtr code, VoidPtr image); + Void Remove(ProcessID process_id); Bool IsUser() override; Bool IsKernel() override; diff --git a/dev/kernel/KernelKit/Timer.h b/dev/kernel/KernelKit/Timer.h index 6c9195aa..b6d23e6b 100644 --- a/dev/kernel/KernelKit/Timer.h +++ b/dev/kernel/KernelKit/Timer.h @@ -44,13 +44,13 @@ namespace Kernel private: UIntPtr* fDigitalTimer{nullptr}; - Int64 fWaitFor{0}; + Int64 fWaitFor{0}; }; class HardwareTimer final : public TimerInterface { public: - explicit HardwareTimer(Int64 seconds); + explicit HardwareTimer(UInt64 seconds); ~HardwareTimer() override; public: @@ -61,7 +61,7 @@ namespace Kernel private: UIntPtr* fDigitalTimer{nullptr}; - Int64 fWaitFor{0}; + Int64 fWaitFor{0}; }; inline Int64 rtl_ms(Int64 time) diff --git a/dev/kernel/NetworkKit/MAC.h b/dev/kernel/NetworkKit/MAC.h index 55e444dc..0509fed4 100644 --- a/dev/kernel/NetworkKit/MAC.h +++ b/dev/kernel/NetworkKit/MAC.h @@ -10,7 +10,7 @@ #include <NewKit/Defines.h> #include <NewKit/KString.h> -#define kMACAddrLen (12) +#define kMACAddrLen (32) namespace Kernel { @@ -27,10 +27,10 @@ namespace Kernel NE_COPY_DEFAULT(MacAddressGetter) public: - Array<WideChar, kMACAddrLen>& AsBytes(); + Array<UInt8, kMACAddrLen>& AsBytes(); private: - Array<WideChar, kMACAddrLen> fMacAddress; + Array<UInt8, kMACAddrLen> fMacAddress; }; } // namespace Kernel diff --git a/dev/kernel/NewKit/KernelPanic.h b/dev/kernel/NewKit/KernelPanic.h index 60a6519c..d630e6b4 100644 --- a/dev/kernel/NewKit/KernelPanic.h +++ b/dev/kernel/NewKit/KernelPanic.h @@ -33,7 +33,7 @@ namespace Kernel #undef __MUST_PASS #endif -#define __MUST_PASS(EXPR, FILE, LINE) Kernel::ke_runtime_check(EXPR, FILE, STRINGIFY(LINE)) +#define __MUST_PASS(EXPR, FILE, LINE) Kernel::ke_runtime_check(EXPR, FILE, STRINGIFY(LINE)) #ifdef __DEBUG__ #define MUST_PASS(EXPR) __MUST_PASS((EXPR), __FILE__, __LINE__) diff --git a/dev/kernel/amd64-ci.make b/dev/kernel/amd64-ci.make index 9e881123..e9a4d4b0 100644 --- a/dev/kernel/amd64-ci.make +++ b/dev/kernel/amd64-ci.make @@ -5,7 +5,7 @@ CXX = x86_64-w64-mingw32-g++ LD = x86_64-w64-mingw32-ld -CCFLAGS = -fshort-wchar -c -D__NE_AMD64__ -Werror -Wall -Wpedantic -Wextra -mno-red-zone -fno-rtti -fno-exceptions -std=c++20 -D__NE_SUPPORT_NX__ -O0 -I../Vendor -D__FSKIT_INCLUDES_NEFS__ -D__NEOSKRNL__ -D__HAVE_NE_APIS__ -D__FREESTANDING__ -D__NE_VIRTUAL_MEMORY_SUPPORT__ -D__NE_AUTO_FORMAT__ -D__NE__ -I./ -I../ -I../zba +CCFLAGS = -fshort-wchar -c -D__NE_AMD64__ -Werror -Wall -Wpedantic -Wextra -mno-red-zone -fno-rtti -fno-exceptions -std=c++20 -D__NE_SUPPORT_NX__ -O0 -I../vendor -D__FSKIT_INCLUDES_NEFS__ -D__NEOSKRNL__ -D__HAVE_NE_APIS__ -D__FREESTANDING__ -D__NE_VIRTUAL_MEMORY_SUPPORT__ -D__NE_AUTO_FORMAT__ -D__NE__ -I./ -I../ -I../boot ASM = nasm diff --git a/dev/kernel/amd64-desktop.make b/dev/kernel/amd64-desktop.make index 905a6c91..02b0a3b4 100644 --- a/dev/kernel/amd64-desktop.make +++ b/dev/kernel/amd64-desktop.make @@ -5,7 +5,7 @@ CXX = x86_64-w64-mingw32-g++ LD = x86_64-w64-mingw32-ld -CCFLAGS = -fshort-wchar -c -D__NE_AMD64__ -Wall -Wpedantic -Wextra -mno-red-zone -fno-rtti -fno-exceptions -std=c++20 -D__NE_SUPPORT_NX__ -O0 -I../Vendor -D__FSKIT_INCLUDES_NEFS__ -D__NEOSKRNL__ -D__HAVE_NE_APIS__ -D__FREESTANDING__ -D__NE_VIRTUAL_MEMORY_SUPPORT__ -D__NE_AUTO_FORMAT__ -D__NE__ -I./ -I../ -I../zba +CCFLAGS = -fshort-wchar -c -D__NE_AMD64__ -Wall -Wpedantic -Wextra -mno-red-zone -fno-rtti -fno-exceptions -std=c++20 -D__NE_SUPPORT_NX__ -O0 -I../vendor -D__FSKIT_INCLUDES_NEFS__ -D__NEOSKRNL__ -D__HAVE_NE_APIS__ -D__FREESTANDING__ -D__NE_VIRTUAL_MEMORY_SUPPORT__ -D__NE_AUTO_FORMAT__ -D__NE__ -I./ -I../ -I../boot ASM = nasm @@ -33,7 +33,7 @@ COPY = cp ASMFLAGS = -f win64 # Kernel subsystem is 17 and entrypoint is hal_init_platform -LDFLAGS = -e hal_init_platform --subsystem=17 --image-base 0x4000000 +LDFLAGS = -e hal_init_platform --subsystem=17 --image-base 0x10000000 LDOBJ = obj/*.obj # This file is the Kernel, responsible of task, memory, driver, sci, disk and device management. diff --git a/dev/kernel/src/FS/NeFS.cc b/dev/kernel/src/FS/NeFS.cc index 369e1eca..8bbec1d2 100644 --- a/dev/kernel/src/FS/NeFS.cc +++ b/dev/kernel/src/FS/NeFS.cc @@ -145,7 +145,7 @@ _Output BOOL NeFileSystemParser::CreateFork(_Input NEFS_FORK_STRUCT& the_fork) /// log what we have now. (void)(kout << "Fork offset is at: " << hex_number(the_fork.DataOffset) - << kendl); + << kendl); (void)(kout << "Wrote fork metadata at: " << hex_number(lba) << kendl); @@ -735,7 +735,7 @@ _Output NEFS_CATALOG_STRUCT* NeFileSystemParser::FindCatalog(_Input const Char* drive.fInput(drive.fPacket); - auto start_catalog_lba = kNeFSCatalogStartAddress; + auto start_catalog_lba = kNeFSCatalogStartAddress; if (!KStringBuilder::Equals(catalog_name, NeFileSystemHelper::Root()) && local_search) { @@ -966,7 +966,7 @@ VoidPtr NeFileSystemParser::ReadCatalog(_Input _Output NEFS_CATALOG_STRUCT* cata NE_UNUSED(dataSz); - Lba dataForkLba = (!is_rsrc_fork) ? catalog->DataFork : catalog->ResourceFork; + Lba dataForkLba = (!is_rsrc_fork) ? catalog->DataFork : catalog->ResourceFork; NEFS_FORK_STRUCT* fs_buf = new NEFS_FORK_STRUCT(); auto& drive = kMountpoint.A(); @@ -1030,7 +1030,7 @@ bool NeFileSystemParser::Seek(_Input _Output NEFS_CATALOG_STRUCT* catalog, SizeT SizeT NeFileSystemParser::Tell(_Input _Output NEFS_CATALOG_STRUCT* catalog) { NE_UNUSED(catalog); - + err_global_get() = kErrorUnimplemented; return 0; } diff --git a/dev/kernel/src/GUIDWizard.cc b/dev/kernel/src/GUIDWizard.cc index 4aab3b76..b14f637f 100644 --- a/dev/kernel/src/GUIDWizard.cc +++ b/dev/kernel/src/GUIDWizard.cc @@ -26,9 +26,9 @@ namespace CF::XRN::Version1 Ref<GUIDSequence*> seq_ref{seq}; - seq_ref.Leak()->fUuid.fMs1 = uuidSeq[0]; - seq_ref.Leak()->fUuid.fMs2 = uuidSeq[1]; - seq_ref.Leak()->fUuid.fMs3 = uuidSeq[2]; + seq_ref.Leak()->fUuid.fMs1 = uuidSeq[0]; + seq_ref.Leak()->fUuid.fMs2 = uuidSeq[1]; + seq_ref.Leak()->fUuid.fMs3 = uuidSeq[2]; seq_ref.Leak()->fUuid.fMs4[0] = uuidSeq[3]; seq_ref.Leak()->fUuid.fMs4[1] = uuidSeq[4]; seq_ref.Leak()->fUuid.fMs4[2] = uuidSeq[5]; diff --git a/dev/kernel/src/Network/IPCMsg.cc b/dev/kernel/src/Network/IPCMsg.cc index 0f3e61e5..ceb0ae98 100644 --- a/dev/kernel/src/Network/IPCMsg.cc +++ b/dev/kernel/src/Network/IPCMsg.cc @@ -90,11 +90,11 @@ namespace Kernel (*pckt_in)->IpcEndianess = static_cast<UInt8>(endianess); (*pckt_in)->IpcPacketSize = sizeof(IPC_MSG); - (*pckt_in)->IpcTo.UserProcessID = 0; - (*pckt_in)->IpcTo.ProcessTeam = 0; + (*pckt_in)->IpcTo.UserProcessID = 0; + (*pckt_in)->IpcTo.ProcessTeam = 0; - (*pckt_in)->IpcFrom.UserProcessID = 0; - (*pckt_in)->IpcFrom.ProcessTeam = 0; + (*pckt_in)->IpcFrom.UserProcessID = 0; + (*pckt_in)->IpcFrom.ProcessTeam = 0; return Yes; } diff --git a/dev/kernel/src/Network/MACAddressGetter.cc b/dev/kernel/src/Network/MACAddressGetter.cc index b6659298..192dbc70 100644 --- a/dev/kernel/src/Network/MACAddressGetter.cc +++ b/dev/kernel/src/Network/MACAddressGetter.cc @@ -8,7 +8,7 @@ namespace Kernel { - Array<WideChar, kMACAddrLen>& MacAddressGetter::AsBytes() + Array<UInt8, kMACAddrLen>& MacAddressGetter::AsBytes() { return this->fMacAddress; } diff --git a/dev/kernel/src/UserProcessScheduler.cc b/dev/kernel/src/UserProcessScheduler.cc index 5f627dd5..b2052723 100644 --- a/dev/kernel/src/UserProcessScheduler.cc +++ b/dev/kernel/src/UserProcessScheduler.cc @@ -133,14 +133,14 @@ namespace Kernel } else { - ProcessMemoryHeapList* entry = this->ProcessMemoryHeap; + ProcessMemoryHeapList* entry = this->ProcessMemoryHeap; while (entry) { if (entry->MemoryEntry == nullptr) break; // chose to break here, when we get an already allocated memory entry for our needs. - entry = entry->MemoryNext; + entry = entry->MemoryNext; } entry->MemoryNext = new ProcessMemoryHeapList(); diff --git a/dev/modules/AHCI/AHCI.h b/dev/modules/AHCI/AHCI.h index 67e12c45..1c0f3b00 100644 --- a/dev/modules/AHCI/AHCI.h +++ b/dev/modules/AHCI/AHCI.h @@ -335,7 +335,7 @@ typedef struct HbaCmdTbl final Kernel::UInt8 Cfis[64]; // Command FIS Kernel::UInt8 Acmd[16]; // ATAPI command, 12 or 16 bytes Kernel::UInt8 Rsv[48]; // Reserved - struct HbaPrdtEntry Prdt[1]; // Physical region descriptor table entries, 0 ~ 65535 + struct HbaPrdtEntry Prdt[1]; // Physical region descriptor table entries, 0 ~ 65535 } HbaCmdTbl; /// @brief Initializes an AHCI disk. diff --git a/dev/user/Macros.h b/dev/user/Macros.h index 98beeb0d..556833ea 100644 --- a/dev/user/Macros.h +++ b/dev/user/Macros.h @@ -90,4 +90,36 @@ IMPORT_C void _rtl_assert(Bool expr, const Char* origin); #define ARRAY_SIZE(X) \ (((sizeof(X) / sizeof(*(X))) / \ (static_cast<SizeT>(!(sizeof(X) % sizeof(*(X))))))) -#endif
\ No newline at end of file +#endif + +#ifndef KIB +#define KIB(X) (UInt64)((X) / 1024) +#endif + +#ifndef kib_cast +#define kib_cast(X) (UInt64)((X)*1024) +#endif + +#ifndef MIB +#define MIB(X) (UInt64)((UInt64)KIB(X) / 1024) +#endif + +#ifndef mib_cast +#define mib_cast(X) (UInt64)((UInt64)kib_cast(X) * 1024) +#endif + +#ifndef GIB +#define GIB(X) (UInt64)((UInt64)MIB(X) / 1024) +#endif + +#ifndef gib_cast +#define gib_cast(X) (UInt64)((UInt64)mib_cast(X) * 1024) +#endif + +#ifndef TIB +#define TIB(X) (UInt64)((UInt64)GIB(X) / 1024) +#endif + +#ifndef tib_cast +#define tib_cast(X) ((UInt64)gib_cast(X) * 1024) +#endif diff --git a/dev/user/SystemCalls.h b/dev/user/SystemCalls.h index 97a49469..df91bc1e 100644 --- a/dev/user/SystemCalls.h +++ b/dev/user/SystemCalls.h @@ -322,14 +322,17 @@ IMPORT_C SInt32 SchedBreakPoint(Void); // @brief Filesystem API.
// ------------------------------------------------------------------------------------------ //
-IMPORT_C BOOL FsCopy(const char* path, const char* dst);
-IMPORT_C BOOL FsMove(const char* path, const char* dst);
+IMPORT_C BOOL FsCopy(const Char* path, const Char* dst);
-IMPORT_C BOOL FsExists(const char* path);
+IMPORT_C BOOL FsMove(const Char* path, const Char* dst);
-IMPORT_C BOOL FsCreateDir(const char* path);
-IMPORT_C BOOL FsCreateFile(const char* path);
-IMPORT_C BOOL FsCreateAlias(const char* path, const char* from);
+IMPORT_C BOOL FsExists(const Char* path);
+
+IMPORT_C BOOL FsCreateDir(const Char* path);
+
+IMPORT_C BOOL FsCreateFile(const Char* path);
+
+IMPORT_C BOOL FsCreateAlias(const Char* path, const Char* from);
// ------------------------------------------------------------------------------------------ //
// @brief Format API.
@@ -337,6 +340,6 @@ IMPORT_C BOOL FsCreateAlias(const char* path, const char* from); IMPORT_C Char* StrFmt(const Char* fmt, ...);
-IMPORT_C UInt64 MathToNumber(const Char* in, const Char** endp, const SInt16 base);
+IMPORT_C UInt64 StrMathToNumber(const Char* in, const Char** endp, const SInt16 base);
#endif // ifndef SCI_SCI_H
diff --git a/dev/user/src/GNUmakefile b/dev/user/src/GNUmakefile index c0ddd5b4..09993d16 100644 --- a/dev/user/src/GNUmakefile +++ b/dev/user/src/GNUmakefile @@ -1,6 +1,6 @@ ################################################## # (c) Amlal El Mahrouss, all rights reserved. -# This is the bootloader makefile. +# This file is for user.sys's syscall stubs. ################################################## ASM=nasm @@ -13,4 +13,4 @@ error: .PHONY: sci_asm_io_x64 sci_asm_io_x64: - $(ASM) $(FLAGS) SystemCall+IO.asm -o SystemCall+IO.o + $(ASM) $(FLAGS) SystemCalls+IO.asm -o SystemCalls+IO.asm.o diff --git a/dev/user/src/SystemCall+IO.asm b/dev/user/src/SystemCalls+IO.asm index 8f123ec8..58ea79e7 100644 --- a/dev/user/src/SystemCall+IO.asm +++ b/dev/user/src/SystemCalls+IO.asm @@ -9,6 +9,8 @@ [bits 64] +;; @brief Syscall dispatch, also taking note the Microsoft's calling convention to translate it to NeKernel's ABI. + section .text global sci_syscall_arg_1 diff --git a/dev/user/src/SystemCalls.cc b/dev/user/src/SystemCalls.cc index 19f1fe42..7ad0fe6f 100644 --- a/dev/user/src/SystemCalls.cc +++ b/dev/user/src/SystemCalls.cc @@ -6,9 +6,14 @@ #include <user/SystemCalls.h>
-/// @file libsci.cc
+/// @file SystemCalls.cc
/// @brief Source file for the memory functions of the libsci.
+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);
+
/// @brief Copy memory region.
IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len)
{
@@ -27,7 +32,22 @@ IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input Si return dest;
}
-/// @brief Fill memory region with **value**.
+IMPORT_C SInt64 MmStrLen(const Char* in)
+{
+ if (!in)
+ return 0;
+
+ SizeT len{0};
+
+ do
+ {
+ ++len;
+ } while (in[len] != '\0');
+
+ return len;
+}
+
+/// @brief Fill memory region **dest** with **value**.
IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt8 value)
{
if (!len ||
@@ -43,3 +63,29 @@ IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt return dest;
}
+
+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)));
+}
+
+IMPORT_C Void IoCloseFile(_Input Ref desc)
+{
+ sci_syscall_arg_2(2, desc);
+}
+
+IMPORT_C UInt64 IoSeekFile(_Input Ref desc, _Input UInt64 off)
+{
+ auto ret = (UInt64*)sci_syscall_arg_3(3, reinterpret_cast<VoidPtr>(desc),
+ reinterpret_cast<VoidPtr>(&off));
+
+ MUST_PASS((*ret) != ~0UL);
+ return *ret;
+}
+
+IMPORT_C UInt64 IoTellFile(_Input Ref desc)
+{
+ auto ret = (UInt64*)sci_syscall_arg_2(4, reinterpret_cast<VoidPtr>(desc));
+ return *ret;
+}
diff --git a/dev/user/user.json b/dev/user/user.json index 481280f8..9581d4d4 100644 --- a/dev/user/user.json +++ b/dev/user/user.json @@ -5,7 +5,6 @@ "sources_path": ["src/*.cc", "src/*.o"], "output_name": "user.sys", "compiler_flags": [ - "-fPIC", "-ffreestanding", "-shared", "-fno-rtti", diff --git a/modules_ahci_x64.sh b/modules_ahci_x64.sh new file mode 100755 index 00000000..44e3ecf5 --- /dev/null +++ b/modules_ahci_x64.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# LOG HISTORY: +# 03/25/25: Add 'disk' build step. +# 04/05/25: Improve and fix script. + +cd dev/boot/modules/SysChk +btb amd64-ahci.json +cd ../ +cd BootNet +btb amd64.json
\ No newline at end of file diff --git a/modules_pio_x64.sh b/modules_pio_x64.sh new file mode 100755 index 00000000..2b501047 --- /dev/null +++ b/modules_pio_x64.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# LOG HISTORY: +# 03/25/25: Add 'disk' build step. +# 04/05/25: Improve and fix script. + +cd dev/boot/modules/SysChk +btb amd64-pio.json +cd ../ +cd BootNet +btb amd64.json
\ No newline at end of file diff --git a/public/frameworks/CoreFoundation.fwrk/CoreFoundation.json b/public/frameworks/CoreFoundation.fwrk/CoreFoundation.json index e8ee3245..a208f57a 100644 --- a/public/frameworks/CoreFoundation.fwrk/CoreFoundation.json +++ b/public/frameworks/CoreFoundation.fwrk/CoreFoundation.json @@ -5,7 +5,6 @@ "sources_path": ["src/*.cc"], "output_name": "./dist/libCoreFoundation.dylib", "compiler_flags": [ - "-fPIC", "-ffreestanding", "-shared", "-fno-rtti", diff --git a/public/frameworks/DiskImage.fwrk/DiskImage.json b/public/frameworks/DiskImage.fwrk/DiskImage.json index 0b97dafb..aaff409c 100644 --- a/public/frameworks/DiskImage.fwrk/DiskImage.json +++ b/public/frameworks/DiskImage.fwrk/DiskImage.json @@ -5,7 +5,6 @@ "sources_path": ["src/*.cc"], "output_name": "./dist/libDiskImage.dylib", "compiler_flags": [ - "-fPIC", "-ffreestanding", "-shared", "-fno-rtti", diff --git a/public/frameworks/DiskImage.fwrk/headers/DiskImage.h b/public/frameworks/DiskImage.fwrk/headers/DiskImage.h index 1dc23c0a..c0566485 100644 --- a/public/frameworks/DiskImage.fwrk/headers/DiskImage.h +++ b/public/frameworks/DiskImage.fwrk/headers/DiskImage.h @@ -10,7 +10,6 @@ #pragma once #include <user/SystemCalls.h> -#include <NewKit/Defines.h> #define kDISectorSz (512) #define kDIMinDiskSz mib_cast(1) diff --git a/public/frameworks/KernelTest.fwrk/KernelTest.json b/public/frameworks/KernelTest.fwrk/KernelTest.json index 93207890..aa70db30 100644 --- a/public/frameworks/KernelTest.fwrk/KernelTest.json +++ b/public/frameworks/KernelTest.fwrk/KernelTest.json @@ -5,7 +5,6 @@ "sources_path": ["src/*.cc"], "output_name": "./dist/libKernelTest.dylib", "compiler_flags": [ - "-fPIC", "-ffreestanding", "-shared", "-fno-rtti", diff --git a/public/tools/diutil/diutil.json b/public/tools/diutil/diutil.json index 0805e69f..19b845c5 100644 --- a/public/tools/diutil/diutil.json +++ b/public/tools/diutil/diutil.json @@ -1,8 +1,8 @@ { "compiler_path": "g++", "compiler_std": "c++20", - "headers_path": ["./", "../../../dev/kernel", "../../../dev/", "./"], - "sources_path": ["src/CommandLine.cc"], + "headers_path": ["./", "../../../dev/kernel", "../../../public/frameworks/", "../../../dev/", "./"], + "sources_path": ["src/CommandLine.cc", "../../../public/frameworks/DiskImage.fwrk/src/*.cc", "../../../dev/user/src/*.cc"], "output_name": "./dist/diutil", "cpp_macros": [ "kDUTILVersion=0x0100", diff --git a/public/tools/diutil/src/CommandLine.cc b/public/tools/diutil/src/CommandLine.cc index 61154e7a..9f11e778 100644 --- a/public/tools/diutil/src/CommandLine.cc +++ b/public/tools/diutil/src/CommandLine.cc @@ -11,14 +11,13 @@ static const Char kDiskName[kDIDiskNameLen] = "Disk"; static SInt32 kDiskSectorSz = 512; -static const SInt32 kDiskBlockCnt = 1; static SizeT kDiskSz = gib_cast(4); static const Char kOutDisk[kDIOutNameLen] = "disk.eimg"; /// @brief Filesystem tool entrypoint. int main(int argc, char** argv) { - for (SizeT arg = 0; arg < argc; ++arg) + for (SInt32 arg = 0; arg < argc; ++arg) { const Char* arg_s = argv[arg]; @@ -33,14 +32,14 @@ int main(int argc, char** argv) { if ((arg + 1) < argc) { - kDiskSz = MathToNumber(argv[arg + 1], nullptr, 10); + kDiskSz = StrMathToNumber(argv[arg + 1], nullptr, 10); } } else if (MmCmpMemory((VoidPtr)arg_s, (VoidPtr) "--disk-sector-size", MmStrLen("--disk-sector-size") == 0)) { if ((arg + 1) < argc) { - kDiskSectorSz = MathToNumber(argv[arg + 1], nullptr, 10); + kDiskSectorSz = StrMathToNumber(argv[arg + 1], nullptr, 10); } } else if (MmCmpMemory((VoidPtr)arg_s, (VoidPtr) "--disk-part-name", MmStrLen("--disk-part-name") == 0)) diff --git a/setup_amd64.sh b/setup_x64.sh index 717f7840..35111b88 100755 --- a/setup_amd64.sh +++ b/setup_x64.sh @@ -4,13 +4,7 @@ # 03/25/25: Add 'disk' build step. # 04/05/25: Improve and fix script. -cd dev/boot/modules/SysChk -btb amd64.json -cd ../ -cd BootNet -btb amd64.json -cd ../../../ -cd user +cd dev/user btb user.json cd ../boot make -f amd64-desktop.make efi |
