From d62920dcd07e947e9dc4228d10fa471bd6a6cb9c Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Tue, 16 Sep 2025 16:42:35 -0400 Subject: kern: vm: Implement lazy allocation for vm_map() Introduce lazy allocation in the vm_map() function. When passing a zero value to any of the base address fields of the map spec, allocate our own memory. Signed-off-by: Ian Moffett --- src/sys/vm/vm_map.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/sys/vm/vm_map.c') diff --git a/src/sys/vm/vm_map.c b/src/sys/vm/vm_map.c index 1b27a21..d8cb4b7 100644 --- a/src/sys/vm/vm_map.c +++ b/src/sys/vm/vm_map.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -57,6 +58,22 @@ __vm_map(struct vm_vas *vas, struct mmu_map *spec, size_t len, int prot) /* Must be 4K aligned */ len = ALIGN_UP(len, PSIZE); + + /* + * If we encounter any address that is zero, we + * must assign our own. + */ + if (spec->pa == 0 || spec->va == 0) { + spec->pa = vm_alloc_frame(len / PSIZE); + if (spec->va == 0) + spec->va = spec->pa; + } + + if (spec->pa == 0) { + return -ENOMEM; + } + + /* Must be on a 4K boundary */ spec->va = ALIGN_DOWN(spec->va, PSIZE); spec->pa = ALIGN_DOWN(spec->pa, PSIZE); -- cgit v1.2.3