From 0476248f648ccddea9bdf9bae9095c3231e6643d Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 26 Jan 2024 15:37:37 +0100 Subject: Kernel: Retarget AMD64 according to spec. Kernel/Toolchain: Now move hKernel to /System/ when building the system. MPT: Fix API. newBoot: This is the priority, get a standard file layout to load the kernel at BK_START_KERNEL (0x00080000) Signed-off-by: Amlal El Mahrouss --- Linker/AMD64.ld | 44 ++++++++++++++++++++++ Linker/PowerPC.ld | 21 ----------- Root/Boot/manifest.json | 4 ++ SPECS | 2 +- Source/KMain.cxx | 2 +- Source/String.cxx | 3 -- makefile | 24 ++++++------ newBoot/BootKit/Boot.hpp | 19 +++++++--- newBoot/Source/Arch/AMD64/BootAMD64.cxx | 4 +- newBoot/Source/Arch/AMD64/Processor.cxx | 7 ++++ newBoot/Source/MPT/API.cxx | 67 ++++++++++++++++++--------------- newBoot/Source/MPT/API.hxx | 2 +- newBoot/Source/MPT/MPT.hxx | 6 +-- newBoot/Source/Start.cxx | 2 +- 14 files changed, 125 insertions(+), 82 deletions(-) create mode 100644 Linker/AMD64.ld delete mode 100644 Linker/PowerPC.ld create mode 100644 Root/Boot/manifest.json diff --git a/Linker/AMD64.ld b/Linker/AMD64.ld new file mode 100644 index 00000000..0b083ca0 --- /dev/null +++ b/Linker/AMD64.ld @@ -0,0 +1,44 @@ +OUTPUT_FORMAT("elf64-x86-64") + +ENTRY(Main) + +PHDRS { + null PT_NULL FLAGS(0) ; + text PT_LOAD FLAGS((1 << 0) | (1 << 2) | (1 << 3)); + rodata PT_LOAD FLAGS((1 << 2)) ; + data PT_LOAD FLAGS((1 << 1) | (1 << 2)) ; +} + +SECTIONS { + . = 0x00080000; + + .text : { + KEEP(*(.multiboot)) + *(.text .text.*) + } :text + + . += CONSTANT(MAXPAGESIZE); + + .init : { + *(.initl) + } + + . += CONSTANT(MAXPAGESIZE); + + .rodata : { + *(.rodata .rodata.*) + } :rodata + + . += CONSTANT(MAXPAGESIZE); + + .data : { + *(.data .data.*) + } :data + + . += CONSTANT(MAXPAGESIZE); + + .bss : { + *(COMMON) + *(.bss .bss.*) + } :data +} \ No newline at end of file diff --git a/Linker/PowerPC.ld b/Linker/PowerPC.ld deleted file mode 100644 index 722a86b8..00000000 --- a/Linker/PowerPC.ld +++ /dev/null @@ -1,21 +0,0 @@ -ENTRY(_start) - -SECTIONS -{ - . = 0x02000000; - .text BLOCK(4K) : ALIGN(4K) - { - *(.text) - } - . = 0x02100000; - .data BLOCK(4K) : ALIGN(4K) - { - - *(.rodata) - *(.data) - } - .bss BLOCK(4K) (NOLOAD) : ALIGN(4K) - { - *(.bss) - } -} \ No newline at end of file diff --git a/Root/Boot/manifest.json b/Root/Boot/manifest.json new file mode 100644 index 00000000..60f27421 --- /dev/null +++ b/Root/Boot/manifest.json @@ -0,0 +1,4 @@ +{ + "pluginName": "h-core", + "kernelPath": "/System/hKernel.elf" +} \ No newline at end of file diff --git a/SPECS b/SPECS index 245e37c3..6cae3058 100644 --- a/SPECS +++ b/SPECS @@ -4,7 +4,7 @@ - ABI/Format: Portable Executable Format. - Architecture: Microkernel. -- Language: C++/C/Assembly (AMD64, ARC, X86S, ARM64) +- Language: C++/C/Assembly (AMD64, X64000, X86S, ARM64) =================================== 1: The hCore Microkernel diff --git a/Source/KMain.cxx b/Source/KMain.cxx index 0a614171..8a96aee0 100644 --- a/Source/KMain.cxx +++ b/Source/KMain.cxx @@ -11,7 +11,7 @@ #include #include -extern "C" void __KernelMain(hCore::VoidPtr this_image) +extern "C" void Main(hCore::VoidPtr this_image) { MUST_PASS(hCore::initialize_hardware_components()); diff --git a/Source/String.cxx b/Source/String.cxx index 86194812..3ad2a0cd 100644 --- a/Source/String.cxx +++ b/Source/String.cxx @@ -7,7 +7,6 @@ * ======================================================== */ -#include #include #include @@ -103,7 +102,6 @@ namespace hCore if (!to_str(result, sizeof(int), i)) { - delete[] ret; return ("-1"); } @@ -211,7 +209,6 @@ namespace hCore if (lhs && rhs && cur < string_length(lhs)) { - SizeT sz_lhs = string_length(lhs); SizeT sz_rhs = string_length(rhs); rt_copy_memory(rhs, lhs + cur, sz_rhs); diff --git a/makefile b/makefile index ee57f2c0..558811a2 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,6 @@ -CC = clang -LD = ld.lld -CCFLAGS = --target=ppc64le -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__hCore__ -I./ -I$(HOME)/ +CC = x86_64-elf-gcc +LD = x86_64-elf-ld +CCFLAGS = -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__hCore__ -I./ -I$(HOME)/ ASM = nasm ASMFLAGS = -f elf64 @@ -8,27 +8,27 @@ ASMFLAGS = -f elf64 KERNEL = hKernel.elf # The kernel entrypoint -ENTRY = --script=Linker/PowerPC.ld - -# Where the text segment is. -TEXT = 0xc0000000 +SCRIPT = --script=Linker/AMD64.ld # we want a flat binary FMT = elf64 .PHONY: kernel-build kernel-build: - $(CC) $(CCFLAGS) Source/*.cxx HALKit/PowerPC/PCI/*.cxx Source/Network/*.cpp\ - Source/Storage/*.cxx HALKit/PowerPC/*.cpp HALKit/PowerPC/*.s - mv *.o Obj/ + $(CC) $(CCFLAGS) Source/*.cxx HALKit/AMD64/PCI/*.cpp Source/Network/*.cpp\ + Source/Storage/*.cxx HALKit/AMD64/*.cxx HALKit/AMD64/*.cpp HALKit/AMD64/*.s + $(ASM) -f elf64 HALKit/AMD64/DebugManager.asm + $(ASM) -f elf64 HALKit/AMD64/SMPCoreManager.asm + mv *.o HALKit/AMD64/*.o Obj/ .PHONY: kernel-link kernel-link: - $(LD) -e $(ENTRY) Obj/*.o -o $(KERNEL) + $(LD) $(SCRIPT) Obj/*.o -o $(KERNEL) + cp $(KERNEL) Root/System .PHONY: all all: kernel-build kernel-link - echo "[hKernel] you can now link it!" + @echo "[hKernel] JOB DONE." .PHONY: kernel-clean kernel-clean: diff --git a/newBoot/BootKit/Boot.hpp b/newBoot/BootKit/Boot.hpp index fabc6186..af86ce4c 100644 --- a/newBoot/BootKit/Boot.hpp +++ b/newBoot/BootKit/Boot.hpp @@ -22,9 +22,13 @@ enum kSegmentBss = 6, }; -class BTextWriter final +/** + * @brief BootKit Text Writer class + * Writes to VGA. + */ +class BKTextWriter final { - volatile UInt16* fWhere; + volatile UInt16* fWhere{ nullptr }; public: void WriteString(const char* c, @@ -40,12 +44,12 @@ public: int y); public: - BTextWriter() = default; - ~BTextWriter() = default; + BKTextWriter() = default; + ~BKTextWriter() = default; public: - BTextWriter& operator=(const BTextWriter&) = default; - BTextWriter(const BTextWriter&) = default; + BKTextWriter& operator=(const BKTextWriter&) = default; + BKTextWriter(const BKTextWriter&) = default; }; @@ -68,3 +72,6 @@ enum kYellow, kWhite, }; + +#define BK_START_KERNEL (0x00080000) + diff --git a/newBoot/Source/Arch/AMD64/BootAMD64.cxx b/newBoot/Source/Arch/AMD64/BootAMD64.cxx index 5543a34f..0ed59ff0 100644 --- a/newBoot/Source/Arch/AMD64/BootAMD64.cxx +++ b/newBoot/Source/Arch/AMD64/BootAMD64.cxx @@ -23,7 +23,7 @@ long long int BStrLen(const char* ptr) return cnt; } -void BTextWriter::WriteString( +void BKTextWriter::WriteString( const char* str, unsigned char forecolour, unsigned char backcolour, @@ -41,7 +41,7 @@ void BTextWriter::WriteString( } } -void BTextWriter::WriteCharacter( +void BKTextWriter::WriteCharacter( char c, unsigned char forecolour, unsigned char backcolour, diff --git a/newBoot/Source/Arch/AMD64/Processor.cxx b/newBoot/Source/Arch/AMD64/Processor.cxx index 18b5e8e7..50330e47 100644 --- a/newBoot/Source/Arch/AMD64/Processor.cxx +++ b/newBoot/Source/Arch/AMD64/Processor.cxx @@ -7,6 +7,13 @@ * ======================================================== */ +/* + * + * @file Processor.cxx + * @brief Processor Specific Functions. + * + */ + extern "C" void rt_halt(void) { asm volatile("hlt"); } extern "C" void rt_cli(void) { asm volatile("cli"); } diff --git a/newBoot/Source/MPT/API.cxx b/newBoot/Source/MPT/API.cxx index c5e31d7f..e607eecd 100644 --- a/newBoot/Source/MPT/API.cxx +++ b/newBoot/Source/MPT/API.cxx @@ -10,22 +10,6 @@ #include "API.hxx" #include "Detail.hxx" -struct Files32FileHdr final -{ - char Filename[32]; - char Ext[3]; - char Attr; - char Case; - char CreateMs; - unsigned short Create; - unsigned short CreateDate; - unsigned short LastAccess; - unsigned short Timestamp; - unsigned short Datestamp; - unsigned short StartLba; - unsigned int SizeFile; -}; - #define kFilesR 0x01 /* read-only */ #define kFilesH 0x02 /* hidden */ #define kFilesS 0x04 /* system */ @@ -36,29 +20,50 @@ struct Files32FileHdr final // @brief Array of unused bits. #define kFilesU { 0x40, 0x80 } -struct Files32FileGroup final +namespace mpt::detail { - Files32FileHdr* fHdr{ nullptr }; + struct Files32FileHdr final + { + char Filename[32]; + char Ext[3]; + char Attr; + char Case; + char CreateMs; + unsigned short Create; + unsigned short CreateDate; + unsigned short LastAccess; + unsigned short Timestamp; + unsigned short Datestamp; + unsigned short StartLba; + unsigned int SizeFile; + }; + + struct Files32FileGroup final + { + Files32FileHdr* fHdr{ nullptr }; + + Files32FileGroup* fUpper{ nullptr }; + Files32FileGroup* fLower{ nullptr }; + Files32FileGroup* fPrev{ nullptr }; + Files32FileGroup* fNext{ nullptr }; + }; - Files32FileGroup* fUpper{ nullptr }; - Files32FileGroup* fLower{ nullptr }; - Files32FileGroup* fPrev{ nullptr }; - Files32FileGroup* fNext{ nullptr }; -} kRootGroup = nullptr; + /* @brief external inits */ + extern "C" int init_ata_mpt(void); + extern "C" int init_mpt(void); -/* @brief external inits */ -extern "C" int init_ata_mpt(void); -extern "C" int init_mpt(void); + Files32FileGroup* kRootGroup = nullptr; +} namespace mpt { - bool filesystem_init(void) noexcept + bool init_mpt() noexcept { - kRootGroup = detail::new_class(); + detail::kRootGroup = detail::new_class(); - assert(kRootGroup != nullptr); - assert(init_ata_mpt() == detail::okay); - assert(init_mpt() == detail::okay); + assert(detail::kRootGroup != nullptr); + assert(detail::init_ata_mpt() == detail::okay); + assert(detail::init_mpt() == detail::okay); return true; } diff --git a/newBoot/Source/MPT/API.hxx b/newBoot/Source/MPT/API.hxx index 1c5123b1..3ce689e7 100644 --- a/newBoot/Source/MPT/API.hxx +++ b/newBoot/Source/MPT/API.hxx @@ -13,5 +13,5 @@ namespace mpt { /// initializes the Master Partition Table and the Files32 filesystem. /// \return status, assert(fail) is also triggered, use filesystem_hook_error if you want to catch it. - bool filesystem_init(void) noexcept; + bool init_mpt() noexcept; } \ No newline at end of file diff --git a/newBoot/Source/MPT/MPT.hxx b/newBoot/Source/MPT/MPT.hxx index cd97b113..e8bbe7cd 100644 --- a/newBoot/Source/MPT/MPT.hxx +++ b/newBoot/Source/MPT/MPT.hxx @@ -23,7 +23,7 @@ struct MasterPartitionTable final enum { - kPartEfi = 'efi', - kPartEpm = 'epm', - kPartEbr = 'ebr', + kPartEfi = 0x10, + kPartEpm = 0x11, + kPartEbr = 0x12, }; \ No newline at end of file diff --git a/newBoot/Source/Start.cxx b/newBoot/Source/Start.cxx index 0dbe1de0..5f320ac7 100644 --- a/newBoot/Source/Start.cxx +++ b/newBoot/Source/Start.cxx @@ -11,7 +11,7 @@ extern "C" void __AppMain(void) { - BTextWriter writer; + BKTextWriter writer; writer.WriteString("Starting hCore...", kBlack, kWhite, 0, 0); const char* args[] = { "/hCore.bin" }; -- cgit v1.2.3