summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-11-23 13:37:23 -0500
committerIan Moffett <ian@osmora.org>2025-11-23 13:37:23 -0500
commitb66e8e0c9999565c78d6d207f904c8d3ec1148e4 (patch)
treed2bf632b9e4660a78637f09c4cc5110d67b79d47
parentd9e933046e49221836cae35769c5d53ac721e715 (diff)
kern: lib: Add pointerbox physmem allocationdev
Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r--sys/inc/lib/ptrbox.h9
-rw-r--r--sys/lib/ptrbox.c41
2 files changed, 48 insertions, 2 deletions
diff --git a/sys/inc/lib/ptrbox.h b/sys/inc/lib/ptrbox.h
index 94f718c..38f6265 100644
--- a/sys/inc/lib/ptrbox.h
+++ b/sys/inc/lib/ptrbox.h
@@ -37,9 +37,11 @@
* Represents allocation types in a pointerbox
*
* @PTRBOX_HEAP: kalloc() and friends
+ * @PTRBOX_PHYSMEM: Physical memory
*/
typedef enum {
- PTRBOX_HEAP
+ PTRBOX_HEAP,
+ PTRBOX_PHYSMEM
} ptrbox_altype_t;
/*
@@ -53,7 +55,10 @@ typedef enum {
*/
struct ptrbox_aldes {
ptrbox_altype_t type;
- void *base;
+ union {
+ void *base;
+ uintptr_t phys;
+ };
size_t length;
TAILQ_ENTRY(ptrbox_aldes) link;
};
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,11 +29,44 @@
#include <sys/types.h>
#include <sys/cdefs.h>
+#include <sys/param.h>
#include <sys/errno.h>
#include <vm/kalloc.h>
+#include <vm/phys.h>
+#include <vm/vm.h>
#include <lib/ptrbox.h>
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)
{
struct ptrbox_aldes *aldes;
@@ -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);