From 50093e70e4a45178744ae6ba9dfac4126dcb70f0 Mon Sep 17 00:00:00 2001 From: Jozef Nagy Date: Sat, 25 Jan 2025 21:42:26 +0100 Subject: [PATCH] Basic memory management for UEFI only --- Makefile | 2 +- boot/arch/x86_64/common/debug/serial.c | 2 +- boot/common/mem/mman.c | 98 +++++++++++++++++++++++ boot/common/print.c | 9 ++- boot/include/arch/x86_64/arch/mm/paging.h | 4 - boot/include/mem/mman.h | 30 +++++++ boot/platform/uefi/entry.c | 9 ++- boot/platform/uefi/libefi | 2 +- boot/platform/uefi/print.c | 1 - 9 files changed, 146 insertions(+), 11 deletions(-) create mode 100644 boot/common/mem/mman.c create mode 100644 boot/include/mem/mman.h diff --git a/Makefile b/Makefile index 844c246..eb93a78 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ export BUILD_DIR ?= $(ROOT_DIR)/build export SYSROOT_DIR ?= $(ROOT_DIR)/sysroot export RELEASE_DIR ?= $(ROOT_DIR)/release -NOUEFI ?= y +NOUEFI ?= n ## # Image generation and running diff --git a/boot/arch/x86_64/common/debug/serial.c b/boot/arch/x86_64/common/debug/serial.c index 690fa5c..e80bf12 100644 --- a/boot/arch/x86_64/common/debug/serial.c +++ b/boot/arch/x86_64/common/debug/serial.c @@ -17,7 +17,7 @@ /* SOFTWARE. */ /*********************************************************************************/ -#include +//#include #include #include diff --git a/boot/common/mem/mman.c b/boot/common/mem/mman.c new file mode 100644 index 0000000..8b3651f --- /dev/null +++ b/boot/common/mem/mman.c @@ -0,0 +1,98 @@ +/*********************************************************************************/ +/* Module Name: mman.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 + +#ifndef AXBOOT_UEFI + +#warning "Memory management is not implemented yet, expect runtime errors." + +void *mem_alloc(size_t n) +{ + (void)n; + + return NULL; +} + +int mem_allocat(void *addr, size_t npages) +{ + (void)addr; + (void)npages; + + return 0; +} + +void mem_free(void **addr) +{ + (void)addr; +} + +#else + +#include +#include + +void *mem_alloc(size_t n) +{ + EFI_STATUS status; + void *alloc = NULL; + + status = gBootServices->AllocatePool(EfiLoaderData, (EFI_UINTN)n, &alloc); + if (EFI_ERROR(status)) { + debug("mem_alloc(): Couldn't allocate %u bytes: %s (%lx)\n", n, efi_status_to_str(status), status); + return NULL; + } + + return alloc; +} + +int mem_allocat(void *addr, size_t npages) +{ + EFI_STATUS status; + void *alloc; + + status = gBootServices->AllocatePages(AllocateAddress, EfiLoaderData, (EFI_UINTN)npages, addr); + if (EFI_ERROR(status)) { + debug("mem_allocat(): Couldn't allocate %u bytes at 0x%lx: %s (%lx)\n", npages, addr, efi_status_to_str(status), status); + return 0; + } + + return 1; +} + +void mem_free(void **addr) +{ + EFI_STATUS status; + + if (addr == NULL) { + return; + } + + status = gBootServices->FreePool(*addr); + if (EFI_ERROR(status)) { + debug("mem_free(): Couldn't free 0x%lx: %s (%lx)\n", *addr, efi_status_to_str(status), status); + return; + } + + *addr = NULL; +} + +#endif \ No newline at end of file diff --git a/boot/common/print.c b/boot/common/print.c index fcfd1fa..58b2bbe 100644 --- a/boot/common/print.c +++ b/boot/common/print.c @@ -46,7 +46,10 @@ void log(const char *fmt, ...) npf_vsnprintf(buf, sizeof(buf), fmt, args); va_end(args); - //printstr(buf); +// TODO: Get rid of this +#ifdef AXBOOT_UEFI + printstr(buf); +#endif } void debug(const char *fmt, ...) @@ -58,6 +61,8 @@ void debug(const char *fmt, ...) npf_vsnprintf(buf, sizeof(buf), fmt, args); va_end(args); - //serial_sendstr(buf); +#ifdef AXBOOT_UEFI + printstr(buf); +#endif } diff --git a/boot/include/arch/x86_64/arch/mm/paging.h b/boot/include/arch/x86_64/arch/mm/paging.h index c6e9c23..d9c8698 100644 --- a/boot/include/arch/x86_64/arch/mm/paging.h +++ b/boot/include/arch/x86_64/arch/mm/paging.h @@ -20,10 +20,6 @@ #ifndef _MM_PAGING_H #define _MM_PAGING_H -#include - -#include - #define PAGE_SIZE 0x1000 #endif /* _MM_PAGING_H */ \ No newline at end of file diff --git a/boot/include/mem/mman.h b/boot/include/mem/mman.h new file mode 100644 index 0000000..4879d92 --- /dev/null +++ b/boot/include/mem/mman.h @@ -0,0 +1,30 @@ +/*********************************************************************************/ +/* Module Name: mman.h */ +/* 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. */ +/*********************************************************************************/ + +#ifndef _MEM_MMAN_H +#define _MEM_MMAN_H + +#include + +void *mem_alloc(size_t n); +int mem_allocat(void *addr, size_t npages); + +void mem_free(void **addr); + +#endif /* _MEM_MMAN_H */ diff --git a/boot/platform/uefi/entry.c b/boot/platform/uefi/entry.c index 90be214..b468b0a 100644 --- a/boot/platform/uefi/entry.c +++ b/boot/platform/uefi/entry.c @@ -20,10 +20,16 @@ #include #include +#include +#include #include #include +EFI_HANDLE gImageHandle; +EFI_SYSTEM_TABLE *gSystemTable; +EFI_BOOT_SERVICES *gBootServices; + EFI_STATUS uefi_entry(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) { @@ -31,6 +37,7 @@ EFI_STATUS uefi_entry(EFI_HANDLE ImageHandle, gImageHandle = ImageHandle; gSystemTable = SystemTable; + gBootServices = SystemTable->BootServices; // clear the screen gSystemTable->ConOut->ClearScreen(gSystemTable->ConOut); @@ -38,7 +45,7 @@ EFI_STATUS uefi_entry(EFI_HANDLE ImageHandle, // disable UEFI watchdog Status = gSystemTable->BootServices->SetWatchdogTimer(0, 0, 0, NULL); if (EFI_ERROR(Status)) { - debug("Couldn't disable UEFI watchdog!\n"); + debug("Couldn't disable UEFI watchdog: %s (%x)\n", efi_status_to_str(Status), Status); } while(1); diff --git a/boot/platform/uefi/libefi b/boot/platform/uefi/libefi index 00ed5f5..c6a93ef 160000 --- a/boot/platform/uefi/libefi +++ b/boot/platform/uefi/libefi @@ -1 +1 @@ -Subproject commit 00ed5f5d53641d48024d8a1346edef07d1d79421 +Subproject commit c6a93ef5d4f6129cc579b3376d1f2990515210f9 diff --git a/boot/platform/uefi/print.c b/boot/platform/uefi/print.c index 908f61e..2f4817c 100644 --- a/boot/platform/uefi/print.c +++ b/boot/platform/uefi/print.c @@ -17,7 +17,6 @@ /* SOFTWARE. */ /*********************************************************************************/ -#include #include #include #include