From b66e8e0c9999565c78d6d207f904c8d3ec1148e4 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sun, 23 Nov 2025 13:37:23 -0500 Subject: kern: lib: Add pointerbox physmem allocation Signed-off-by: Ian Moffett --- sys/lib/ptrbox.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'sys/lib/ptrbox.c') diff --git a/sys/lib/ptrbox.c b/sys/lib/ptrbox.c index d2e9171..b2777dc 100644 --- a/sys/lib/ptrbox.c +++ b/sys/lib/ptrbox.c @@ -29,10 +29,43 @@ #include #include +#include #include #include +#include +#include #include +static void * +ptrbox_physalloc(struct ptrbox *pbox, size_t len) +{ + struct ptrbox_aldes *aldes; + uintptr_t paddr; + + len = ALIGN_UP(len, PAGESIZE); + paddr = vm_phys_alloc(len / PAGESIZE); + if (paddr == 0) { + return NULL; + } + + /* Allocate a descriptor */ + aldes = kalloc(sizeof(*aldes)); + if (aldes == NULL) { + vm_phys_free(paddr, len / PAGESIZE); + return NULL; + } + + /* Initialize our descriptor */ + aldes->type = PTRBOX_PHYSMEM; + aldes->phys = paddr; + aldes->length = len; + + /* Push it to the pointer box */ + TAILQ_INSERT_TAIL(&pbox->allist, aldes, link); + ++pbox->alcnt; + return PHYS_TO_VIRT(paddr); +} + static void * ptrbox_heapalloc(struct ptrbox *pbox, size_t len) { @@ -76,6 +109,8 @@ ptrbox_alloc(struct ptrbox *pbox, size_t len, ptrbox_altype_t type) switch (type) { case PTRBOX_HEAP: return ptrbox_heapalloc(pbox, len); + case PTRBOX_PHYSMEM: + return ptrbox_physalloc(pbox, len); default: return NULL; } @@ -106,6 +141,12 @@ ptrbox_free(struct ptrbox *pbox) case PTRBOX_HEAP: kfree(desc->base); break; + case PTRBOX_PHYSMEM: + vm_phys_free( + desc->phys, + desc->length / PAGESIZE + ); + break; } kfree(desc); -- cgit v1.2.3