Basic memory management for UEFI only

This commit is contained in:
Jozef Nagy 2025-01-25 21:42:26 +01:00
parent 1fc97f9c5f
commit 50093e70e4
Signed by untrusted user who does not match committer: crz
GPG key ID: 459A4811CEAC7068
9 changed files with 146 additions and 11 deletions

View file

@ -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

View file

@ -17,7 +17,7 @@
/* SOFTWARE. */
/*********************************************************************************/
#include <debug/serial.h>
//#include <debug/serial.h>
#include <arch/cpu/cpu.h>
#include <stdint.h>

98
boot/common/mem/mman.c Normal file
View file

@ -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 <mem/mman.h>
#include <print.h>
#include <stddef.h>
#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 <efi.h>
#include <efilib.h>
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

View file

@ -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
}

View file

@ -20,10 +20,6 @@
#ifndef _MM_PAGING_H
#define _MM_PAGING_H
#include <firmware/memmap.h>
#include <stdint.h>
#define PAGE_SIZE 0x1000
#endif /* _MM_PAGING_H */

30
boot/include/mem/mman.h Normal file
View file

@ -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 <stddef.h>
void *mem_alloc(size_t n);
int mem_allocat(void *addr, size_t npages);
void mem_free(void **addr);
#endif /* _MEM_MMAN_H */

View file

@ -20,10 +20,16 @@
#include <efi.h>
#include <efilib.h>
#include <mem/mman.h>
#include <lib/string.h>
#include <print.h>
#include <stddef.h>
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);

@ -1 +1 @@
Subproject commit 00ed5f5d53641d48024d8a1346edef07d1d79421
Subproject commit c6a93ef5d4f6129cc579b3376d1f2990515210f9

View file

@ -17,7 +17,6 @@
/* SOFTWARE. */
/*********************************************************************************/
#include <firmware/firmware.h>
#include <lib/string.h>
#include <efi.h>
#include <efilib.h>