From 44771b28cd4bacebdb3cb67d2aff723308831b85 Mon Sep 17 00:00:00 2001 From: rsahwe Date: Wed, 14 May 2025 21:30:35 +0000 Subject: [PATCH 1/8] Update boot/common/loader/elf.c --- boot/common/loader/elf.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/boot/common/loader/elf.c b/boot/common/loader/elf.c index bd17021..ba8e313 100644 --- a/boot/common/loader/elf.c +++ b/boot/common/loader/elf.c @@ -52,13 +52,13 @@ uintptr_t elf64_load(char *data, pagetable *pagemap) } } + uintptr_t kernel_address = 0;// OR OTHER INVALID + for (uint16_t i = 0; i < header->e_phnum; i++) { if (ph[i].p_type != PT_LOAD) continue; - if ((ph[i].p_vaddr & (~(max_align - 1))) < lowest) { - lowest = ph[i].p_vaddr & ~(max_align - 1); - } + lowest = ph[i].p_vaddr & ~(max_align - 1); uint64_t flags = VMM_PRESENT; if (ph[i].p_flags & PF_W) @@ -66,7 +66,7 @@ uintptr_t elf64_load(char *data, pagetable *pagemap) if (!(ph[i].p_flags & PF_X)) flags |= VMM_NX; - uint64_t phys = (uint64_t)mem_alloc(ph[i].p_memsz); + uint64_t phys = ((uint64_t)mem_alloc(ph[i].p_memsz + ph[i].p_vaddr - lowest + 4096) + 4096) & ~0xFFF; if (!phys) { debug("elf64_load(): Out of memory\n"); return 0; @@ -74,12 +74,25 @@ uintptr_t elf64_load(char *data, pagetable *pagemap) debug("elf64_load(): phys=0x%llx, virt=0x%llx, size=%lu\n", phys, ph[i].p_vaddr, ph[i].p_filesz); - map_page(pagemap, ph[i].p_vaddr, phys, flags); - memcpy((void*)ph[i].p_vaddr - lowest, data + ph[i].p_offset, ph[i].p_filesz); + if (ph[i].p_vaddr <= header->e_entry && ph[i].p_vaddr + ph[i].p_memsz >= header->e_entry) { + kernel_address = header->e_entry; + + debug("elf64_load(): found kernel entry at 0x%llx\n", kernel_address); + debug("elf64_load(): comparison to old at 0x%llx\n", (uintptr_t)data + header->e_entry - 0xffffffff80000000 + 0x1000); + } + + map_page(pagemap, lowest, phys, flags); + debug("elf64_load(): memcpy(0x%llx, 0x%llx, 0x%llx)\n", (void*)(phys + ph[i].p_vaddr - lowest), data + ph[i].p_offset, ph[i].p_filesz); + debug("\n"); + memcpy((void*)(phys + ph[i].p_vaddr - lowest), data + ph[i].p_offset, ph[i].p_filesz); + } + + if (kernel_address == 0) { + //SOMETHING } debug("elf64_load(): ELF loaded successfully, entry: 0x%llx\n", header->e_entry); - return (uintptr_t)((uint8_t *)data + header->e_entry); + return (uintptr_t)kernel_address; } uintptr_t elf_load(char *data, pagetable *pagemap) From 03802f538c510efa5221bc477203bf855b18c077 Mon Sep 17 00:00:00 2001 From: rsahwe Date: Wed, 21 May 2025 15:13:01 +0200 Subject: [PATCH 2/8] Cleanup --- boot/common/loader/elf.c | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/boot/common/loader/elf.c b/boot/common/loader/elf.c index ba8e313..e79601f 100644 --- a/boot/common/loader/elf.c +++ b/boot/common/loader/elf.c @@ -40,7 +40,6 @@ uintptr_t elf64_load(char *data, pagetable *pagemap) struct elf_header *header = (struct elf_header *)data; struct elf_program_header *ph = (struct elf_program_header *)((uint8_t *)data + header->e_phoff); - uint64_t lowest = UINT64_MAX; uint64_t max_align = 0; for (uint16_t i = 0; i < header->e_phnum; i++) { @@ -52,13 +51,11 @@ uintptr_t elf64_load(char *data, pagetable *pagemap) } } - uintptr_t kernel_address = 0;// OR OTHER INVALID - for (uint16_t i = 0; i < header->e_phnum; i++) { if (ph[i].p_type != PT_LOAD) continue; - lowest = ph[i].p_vaddr & ~(max_align - 1); + uint64_t aligned_vaddr = ph[i].p_vaddr & ~(max_align - 1); uint64_t flags = VMM_PRESENT; if (ph[i].p_flags & PF_W) @@ -66,7 +63,7 @@ uintptr_t elf64_load(char *data, pagetable *pagemap) if (!(ph[i].p_flags & PF_X)) flags |= VMM_NX; - uint64_t phys = ((uint64_t)mem_alloc(ph[i].p_memsz + ph[i].p_vaddr - lowest + 4096) + 4096) & ~0xFFF; + uint64_t phys = ((uint64_t)mem_alloc(ph[i].p_memsz + ph[i].p_vaddr - aligned_vaddr + 4096) + 4096) & ~0xFFF; if (!phys) { debug("elf64_load(): Out of memory\n"); return 0; @@ -74,25 +71,12 @@ uintptr_t elf64_load(char *data, pagetable *pagemap) debug("elf64_load(): phys=0x%llx, virt=0x%llx, size=%lu\n", phys, ph[i].p_vaddr, ph[i].p_filesz); - if (ph[i].p_vaddr <= header->e_entry && ph[i].p_vaddr + ph[i].p_memsz >= header->e_entry) { - kernel_address = header->e_entry; - - debug("elf64_load(): found kernel entry at 0x%llx\n", kernel_address); - debug("elf64_load(): comparison to old at 0x%llx\n", (uintptr_t)data + header->e_entry - 0xffffffff80000000 + 0x1000); - } - - map_page(pagemap, lowest, phys, flags); - debug("elf64_load(): memcpy(0x%llx, 0x%llx, 0x%llx)\n", (void*)(phys + ph[i].p_vaddr - lowest), data + ph[i].p_offset, ph[i].p_filesz); - debug("\n"); - memcpy((void*)(phys + ph[i].p_vaddr - lowest), data + ph[i].p_offset, ph[i].p_filesz); - } - - if (kernel_address == 0) { - //SOMETHING + map_page(pagemap, aligned_vaddr, phys, flags); + memcpy((void*)(phys + ph[i].p_vaddr - aligned_vaddr), data + ph[i].p_offset, ph[i].p_filesz); } debug("elf64_load(): ELF loaded successfully, entry: 0x%llx\n", header->e_entry); - return (uintptr_t)kernel_address; + return (uintptr_t)header->e_entry; } uintptr_t elf_load(char *data, pagetable *pagemap) From 4f39a7564e84035546c8274266a65ee46c9e777a Mon Sep 17 00:00:00 2001 From: Jozef Nagy Date: Wed, 21 May 2025 22:46:02 +0200 Subject: [PATCH 3/8] Higher half, fixed warnings --- boot/Makefile | 2 +- boot/platform/uefi/driver.c | 2 +- kernel/arch/x86_64/linker.ld | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/boot/Makefile b/boot/Makefile index e1fb122..10a6bd9 100644 --- a/boot/Makefile +++ b/boot/Makefile @@ -28,7 +28,7 @@ export BUILD_DIR ?= build export SYSROOT_DIR ?= sysroot export ASFLAGS := $(foreach d, $(DEFINES), -D$d) -export CFLAGS := $(foreach d, $(DEFINES), -D$d) -Wall -Wextra -ffreestanding -fno-stack-protector -fno-stack-check -MMD -MP +export CFLAGS := $(foreach d, $(DEFINES), -D$d) -Wall -Wextra -Wno-unused-local-typedef -ffreestanding -fno-stack-protector -fno-stack-check -MMD -MP export LDFLAGS := -nostdlib export BOOT_ROOT := $(ROOT_DIR)/boot diff --git a/boot/platform/uefi/driver.c b/boot/platform/uefi/driver.c index 25642ab..cd7283f 100644 --- a/boot/platform/uefi/driver.c +++ b/boot/platform/uefi/driver.c @@ -49,7 +49,7 @@ bool verify_secure_boot() void load_drivers() { - EFI_STATUS status; + // EFI_STATUS status; if (!verify_secure_boot()) { debug("load_drivers(): Secure boot is enabled! Won't load drivers...\n"); return; diff --git a/kernel/arch/x86_64/linker.ld b/kernel/arch/x86_64/linker.ld index 7314b13..758e8b6 100644 --- a/kernel/arch/x86_64/linker.ld +++ b/kernel/arch/x86_64/linker.ld @@ -31,8 +31,7 @@ PHDRS SECTIONS { - /* TODO: 0xffffffff80000000 */ - . = 0x1000; + . = 0xffffffff80000000; _linker_start_text = .; From b1d59e02ebc73dbb51ff7d160e1ada312334a849 Mon Sep 17 00:00:00 2001 From: Jozef Nagy Date: Thu, 22 May 2025 11:48:51 +0200 Subject: [PATCH 4/8] Tweaked kernel CFLAGS --- kernel/Makefile | 5 +---- kernel/arch/x86_64/config.mk | 1 + 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/kernel/Makefile b/kernel/Makefile index a7027f1..4890579 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -47,15 +47,12 @@ KERNEL_CFLAGS := $(foreach d, $(INCLUDE_DIRS), -I$d) \ -fno-omit-frame-pointer \ -fno-stack-protector \ -fno-stack-check \ - -fpic \ - -fpie \ -MMD \ -MP KERNEL_LDFLAGS := -Tarch/$(ARCH)/linker.ld \ -nostdlib \ - -static \ - -no-pie + -static ifeq ($(BUILD_TYPE),debug) KERNEL_CFLAGS += -O0 -g3 diff --git a/kernel/arch/x86_64/config.mk b/kernel/arch/x86_64/config.mk index 7be3279..738c9ca 100644 --- a/kernel/arch/x86_64/config.mk +++ b/kernel/arch/x86_64/config.mk @@ -20,6 +20,7 @@ KERNEL_CFLAGS += -m64 \ -march=x86-64 \ -mabi=sysv \ + -mcmodel=kernel \ -mno-red-zone \ -mno-80387 \ -mno-mmx \ From d1a5d7d43d009937d442768f8f1eff7c17d699d3 Mon Sep 17 00:00:00 2001 From: Jozef Nagy Date: Sat, 24 May 2025 21:12:40 +0200 Subject: [PATCH 5/8] Finished Aurix Protocol, AxBoot logs to file now --- Makefile | 17 +- boot/Makefile | 2 + boot/arch/aarch64/config.mk | 0 boot/arch/i686/config.mk | 1 + boot/arch/x86_64/common/mm/paging.c | 4 - boot/arch/x86_64/common/proto/aurix/handoff.c | 95 ++++++++++ boot/arch/x86_64/common/proto/aurix_handoff.S | 24 --- boot/arch/x86_64/config.mk | 1 + boot/common/config/config.c | 23 ++- boot/common/fs/uefi_sfs.c | 49 ++++- boot/common/init.c | 18 +- boot/common/loader/elf.c | 27 +-- boot/common/loader/loader.c | 4 +- boot/common/mm/mman.c | 9 +- boot/common/print.c | 15 +- boot/common/proto/aurix.c | 177 +++++++++++++++--- boot/common/vfs/vfs.c | 15 +- boot/include/acpi/acpi.h | 28 +++ .../ini.h => arch/x86_64/arch/cpu/idt.h} | 71 +------ boot/include/axboot.h | 5 + boot/include/config/config.h | 1 + boot/include/driver.h | 4 +- boot/include/fs/uefi_sfs.h | 1 + boot/include/loader/elf.h | 2 +- boot/include/mm/memmap.h | 2 +- boot/include/proto/aurix.h | 63 ++++++- boot/include/ui/framebuffer.h | 4 +- boot/platform/uefi/acpi.c | 77 ++++++++ boot/platform/uefi/driver.c | 10 +- boot/platform/uefi/entry.c | 52 ++++- boot/platform/uefi/mm/memmap.c | 46 ++--- boot/platform/uefi/power.c | 6 +- boot/platform/uefi/time/datetime.c | 2 +- boot/platform/uefi/ui/framebuffer.c | 10 +- kernel/include/boot/aurix.h | 84 +++++++++ kernel/kinit.c | 13 +- utils/arch/x86_64/generate-hdd.sh | 3 + 37 files changed, 744 insertions(+), 221 deletions(-) create mode 100644 boot/arch/aarch64/config.mk create mode 100644 boot/arch/i686/config.mk create mode 100644 boot/arch/x86_64/common/proto/aurix/handoff.c delete mode 100644 boot/arch/x86_64/common/proto/aurix_handoff.S create mode 100644 boot/arch/x86_64/config.mk create mode 100644 boot/include/acpi/acpi.h rename boot/include/{config/ini.h => arch/x86_64/arch/cpu/idt.h} (56%) create mode 100644 boot/platform/uefi/acpi.c create mode 100644 kernel/include/boot/aurix.h diff --git a/Makefile b/Makefile index 2582f48..5ba13c8 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,8 @@ .DEFAULT_GOAL := all +GITREV := $(shell git rev-parse --short HEAD) + ## # Build configuration # @@ -43,7 +45,7 @@ LIVECD := $(RELEASE_DIR)/aurix-$(GITREV)-livecd_$(ARCH)-$(PLATFORM).iso LIVEHDD := $(RELEASE_DIR)/aurix-$(GITREV)-livehdd_$(ARCH)-$(PLATFORM).img LIVESD := $(RELEASE_DIR)/aurix-$(GITREV)-livesd_$(ARCH)-$(PLATFORM).img -QEMU_FLAGS := -m 2G -smp 4 -rtc base=localtime -serial stdio +QEMU_FLAGS := -m 512m -smp 4 -rtc base=localtime -serial stdio # QEMU Audio support #QEMU_FLAGS += -audiodev coreaudio,id=coreaudio0 -device ich9-intel-hda -device hda-output,audiodev=coreaudio0 @@ -52,7 +54,10 @@ QEMU_FLAGS := -m 2G -smp 4 -rtc base=localtime -serial stdio QEMU_FLAGS += -usb -device usb-mouse # x86_64 +# TODO: Move this elsewhere +ifeq ($(ARCH),x86_64) QEMU_FLAGS += -machine q35 +endif ## # General info @@ -130,15 +135,17 @@ livesd: install @mkdir -p $(RELEASE_DIR) @utils/arch/$(ARCH)/generate-sd.sh $(LIVESD) +# TODO: Maybe don't run with -hda but -drive? .PHONY: run -run: livecd +run: livehdd @printf ">>> Running QEMU...\n" - @qemu-system-$(ARCH) $(QEMU_FLAGS) $(QEMU_MACHINE_FLAGS) -cdrom $(LIVECD) + @qemu-system-$(ARCH) $(QEMU_FLAGS) $(QEMU_MACHINE_FLAGS) -hda $(LIVEHDD) +# TODO: Maybe don't run with -hda but -drive? .PHONY: run-uefi -run-uefi: livecd ovmf +run-uefi: livehdd ovmf @printf ">>> Running QEMU (UEFI)...\n" - @qemu-system-$(ARCH) $(QEMU_FLAGS) $(QEMU_MACHINE_FLAGS) -bios ovmf/ovmf-$(ARCH).fd -cdrom $(LIVECD) + @qemu-system-$(ARCH) $(QEMU_FLAGS) $(QEMU_MACHINE_FLAGS) -bios ovmf/ovmf-$(ARCH).fd -hda $(LIVEHDD) .PHONY: clean clean: diff --git a/boot/Makefile b/boot/Makefile index 10a6bd9..79e063c 100644 --- a/boot/Makefile +++ b/boot/Makefile @@ -31,6 +31,8 @@ export ASFLAGS := $(foreach d, $(DEFINES), -D$d) export CFLAGS := $(foreach d, $(DEFINES), -D$d) -Wall -Wextra -Wno-unused-local-typedef -ffreestanding -fno-stack-protector -fno-stack-check -MMD -MP export LDFLAGS := -nostdlib +include arch/$(ARCH)/config.mk + export BOOT_ROOT := $(ROOT_DIR)/boot ifeq ($(BUILD_TYPE),debug) diff --git a/boot/arch/aarch64/config.mk b/boot/arch/aarch64/config.mk new file mode 100644 index 0000000..e69de29 diff --git a/boot/arch/i686/config.mk b/boot/arch/i686/config.mk new file mode 100644 index 0000000..65b82a4 --- /dev/null +++ b/boot/arch/i686/config.mk @@ -0,0 +1 @@ +CFLAGS += -DARCH_ACPI_AVAILABLE -DARCH_SMBIOS_AVAILABLE \ No newline at end of file diff --git a/boot/arch/x86_64/common/mm/paging.c b/boot/arch/x86_64/common/mm/paging.c index 40826fd..827201e 100644 --- a/boot/arch/x86_64/common/mm/paging.c +++ b/boot/arch/x86_64/common/mm/paging.c @@ -58,10 +58,6 @@ static void _map(pagetable *pm, uintptr_t virt, uintptr_t phys, uint64_t flags) } pagetable *pml1_table = (pagetable *)(pml2_table->entries[pml2_idx] & 0x000FFFFFFFFFF000); - if ((pml1_table->entries[pml1_idx] & 1)) { - // debug("_map(): Remapping present page\n"); - } - pml1_table->entries[pml1_idx] = (phys & 0x000FFFFFFFFFF000) | flags; } diff --git a/boot/arch/x86_64/common/proto/aurix/handoff.c b/boot/arch/x86_64/common/proto/aurix/handoff.c new file mode 100644 index 0000000..20f0a31 --- /dev/null +++ b/boot/arch/x86_64/common/proto/aurix/handoff.c @@ -0,0 +1,95 @@ +/*********************************************************************************/ +/* Module Name: handoff.c */ +/* Project: AurixOS */ +/* */ +/* Copyright (c) 2024-2025 Jozef Nagy */ +/* */ +/* This source is subject to the MIT License. */ +/* See License.txt in the root of this repository. */ +/* All other rights reserved. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */ +/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */ +/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */ +/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */ +/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */ +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ +/* SOFTWARE. */ +/*********************************************************************************/ + +#include +#include +#include +#include +#include + +struct gdt { + struct gdt_descriptor null; + struct gdt_descriptor kcode; + struct gdt_descriptor kdata; + struct gdt_descriptor ucode; + struct gdt_descriptor udata; + struct tss_descriptor tss; +}; + +void aurix_arch_handoff(void *kernel_entry, pagetable *pm, void *stack, uint32_t stack_size, struct aurix_parameters *parameters) +{ + struct tss tss = { + .iomap_base = sizeof(struct tss), + }; + + struct gdt gdt = { + .tss = { + .gdt = { + .base_low = ((uintptr_t)&tss) & 0xFFFF, + .base_mid = (((uintptr_t)&tss) >> 16) & 0xFF, + .base_high = (((uintptr_t)&tss) >> 24) & 0xFF, + .access = 0b10001001 + }, + .base_high = (((uintptr_t)&tss) >> 32) & 0xFFFF + }, + }; + + struct gdtr gdtr = { + .base = (uint64_t)&gdt, + .limit = sizeof(gdt) - 1 + }; + + gdt_set_entry(&gdt.null, 0, 0, 0, 0); + gdt_set_entry(&gdt.kcode, 0, 0, 0x9a, 0xaf); + gdt_set_entry(&gdt.kdata, 0, 0, 0x92, 0xcf); + gdt_set_entry(&gdt.ucode, 0, 0, 0xfa, 0xaf); + gdt_set_entry(&gdt.udata, 0, 0, 0xf2, 0xcf); + + struct idtr idtr = { + .base = (uint64_t)0, + .limit = 0 + }; + + __asm__ volatile( + "cli\n" + "lgdt %[gdtr]\n" + "ltr %[tss]\n" + "pushq $0x08\n" + "lea 1f(%%rip), %%rax\n" + "pushq %%rax\n" + "lretq\n" + "1:\n" + "movq $0x10, %%rax\n" + "movq %%rax, %%ds\n" + "movq %%rax, %%es\n" + "movq %%rax, %%ss\n" + "movq %%rax, %%fs\n" + "movq %%rax, %%gs\n" + + "lidt %[idt]\n" + + "movq %[pml4], %%cr3\n" + "movq %[stack], %%rsp\n" + "movq %[params], %%rdi\n" + "callq *%[entry]\n" + :: [gdtr]"g"(gdtr), [tss]"r"((uint16_t)__builtin_offsetof(struct gdt, tss)), + [idt]"g"(idtr), + [pml4]"r"(pm), [stack]"r"(stack + stack_size), + [entry]"r"(kernel_entry), [params]"d"(parameters) : "rax", "memory"); +} \ No newline at end of file diff --git a/boot/arch/x86_64/common/proto/aurix_handoff.S b/boot/arch/x86_64/common/proto/aurix_handoff.S deleted file mode 100644 index 7c8f896..0000000 --- a/boot/arch/x86_64/common/proto/aurix_handoff.S +++ /dev/null @@ -1,24 +0,0 @@ -.globl _aurix_handoff_start -.globl _aurix_handoff_end -.globl aurix_handoff - -_aurix_handoff_start: -aurix_handoff: - cli - movq %rsi, %rsp -.section _aurix_handoff - movq %rdi, %cr3 - xor %rax, %rax - xor %rbx, %rbx - xor %rcx, %rcx - xor %rdi, %rdi - xor %r8, %r8 - xor %r9, %r9 - xor %r10, %r10 - xor %r11, %r11 - xor %r12, %r12 - xor %r13, %r13 - xor %r14, %r14 - xor %r15, %r15 - callq *%rdx -_aurix_handoff_end: \ No newline at end of file diff --git a/boot/arch/x86_64/config.mk b/boot/arch/x86_64/config.mk new file mode 100644 index 0000000..65b82a4 --- /dev/null +++ b/boot/arch/x86_64/config.mk @@ -0,0 +1 @@ +CFLAGS += -DARCH_ACPI_AVAILABLE -DARCH_SMBIOS_AVAILABLE \ No newline at end of file diff --git a/boot/common/config/config.c b/boot/common/config/config.c index c0d5a21..57352e0 100644 --- a/boot/common/config/config.c +++ b/boot/common/config/config.c @@ -18,10 +18,10 @@ /*********************************************************************************/ #include -#include #include #include #include +#include