sp1: mm: Add MI virtual memory management

Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
2026-04-22 23:56:01 -04:00
parent 37fc085646
commit d63c88259f
2 changed files with 92 additions and 0 deletions
+67
View File
@@ -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 <sys/units.h>
#include <sys/param.h>
#include <lib/printf.h>
#include <mu/mmu.h>
#include <mu/param.h>
#include <mm/vm.h>
#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;
}
+25
View File
@@ -12,8 +12,25 @@
#ifndef _MM_VM_H_ #ifndef _MM_VM_H_
#define _MM_VM_H_ 1 #define _MM_VM_H_ 1
#include <sys/types.h>
#include <sys/param.h> #include <sys/param.h>
#include <os/bpt.h> #include <os/bpt.h>
#include <mu/mmu.h>
/*
* 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 * Macros used to convert physical to virtual addresses
@@ -24,6 +41,14 @@
#define vma_to_pma(vma) \ #define vma_to_pma(vma) \
(uintptr_t)PTR_NOFFSET(vma, bpt_kload_base()) (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 * Initialize the virtual memory management
*/ */