summaryrefslogtreecommitdiff
path: root/sys/lib/ptrbox.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys/lib/ptrbox.c')
-rw-r--r--sys/lib/ptrbox.c41
1 files changed, 41 insertions, 0 deletions
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);