From d63c88259f50f60fa7b1443ff7cf1a673e7f08b7 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 22 Apr 2026 23:56:01 -0400 Subject: [PATCH] sp1: mm: Add MI virtual memory management Signed-off-by: Ian Moffett --- usr/src/sp1/common/mm/vm_map.c | 67 ++++++++++++++++++++++++++++++++++ usr/src/sp1/head/mm/vm.h | 25 +++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 usr/src/sp1/common/mm/vm_map.c diff --git a/usr/src/sp1/common/mm/vm_map.c b/usr/src/sp1/common/mm/vm_map.c new file mode 100644 index 0000000..20485d8 --- /dev/null +++ b/usr/src/sp1/common/mm/vm_map.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2026, Mirocom Laboratories + * All rights reserved. + * + * The following sources are CONFIDENTIAL and PROPRIETARY + * property of Mirocom Laboratories. Unauthorized copying, + * use, distribution or modification of this file, in whole + * and in part, is strictly prohibited without the prior written + * consent from Mirocom Laboratories. + */ + +#include +#include +#include +#include +#include +#include + +#define pr_trace(fmt, ...) \ + printf("vm_map: " fmt, ##__VA_ARGS__) + +/* Used to safely convert pagesize constants */ +#define GRAN(ps) \ + ((ps) <= PAGESIZE_1G) \ + ? pstab[(ps)] \ + : pstab[PAGESIZE_4K] + +/* Pagesize constant to length table */ +static size_t pstab[] = { + [PAGESIZE_4K] = 0x1000, + [PAGESIZE_2M] = UNIT_MIB * 2, + [PAGESIZE_1G] = UNIT_GIB +}; + +status_t +mm_vm_map(struct mmu_vfr *vfr, struct vm_map *mapping, int prot) +{ + size_t len, gran; + uintptr_t vma, pma; + status_t status; + + if (vfr == NULL || mapping == NULL) { + return STATUS_INVALID_PARAM; + } + + gran = GRAN(mapping->ps); + len = mapping->length; + + vma = ALIGN_DOWN(mapping->vma_base, gran); + pma = ALIGN_DOWN(mapping->pma_base, gran); + len = ALIGN_UP(len + (len & (gran - 1)), gran); + + for (size_t i = 0; i < len; i += gran) { + status = mu_mmu_map(vfr, vma + i, pma + i, prot, mapping->ps); + + /* + * TODO: We need to clean up here instead of leaving a big hole + * when this fails. + */ + if (status != STATUS_SUCCESS) { + pr_trace("mapping failed, leaked %d bytes\n", len); + return status; + } + } + + return STATUS_SUCCESS; +} diff --git a/usr/src/sp1/head/mm/vm.h b/usr/src/sp1/head/mm/vm.h index 8baaa06..5cba19b 100644 --- a/usr/src/sp1/head/mm/vm.h +++ b/usr/src/sp1/head/mm/vm.h @@ -12,8 +12,25 @@ #ifndef _MM_VM_H_ #define _MM_VM_H_ 1 +#include #include #include +#include + +/* + * Represents a virtual memory mapping that can be made + * + * @ps: Pagesize + * @vma_base: Virtual memory base + * @pma_base: Physical memory base + * @length: Number of bytes to map + */ +struct vm_map { + pagesize_t ps; + uintptr_t vma_base; + uintptr_t pma_base; + size_t length; +}; /* * Macros used to convert physical to virtual addresses @@ -24,6 +41,14 @@ #define vma_to_pma(vma) \ (uintptr_t)PTR_NOFFSET(vma, bpt_kload_base()) +/* + * Create a virtual memory mapping + * + * @vfr: Virtual fuck region to map within + * @mapping: Mapping to create + */ +status_t mm_vm_map(struct mmu_vfr *vfr, struct vm_map *mapping, int prot); + /* * Initialize the virtual memory management */