1
0
Fork 0

feat/kernel: new vallocat API

This commit is contained in:
Kevin Alavik 2025-05-16 18:28:40 +02:00
parent 3b8639467f
commit f83961432e
Signed by: cmpsb
GPG key ID: 10D1CC0526FDC6D7
3 changed files with 65 additions and 1 deletions

View file

@ -101,6 +101,66 @@ void *valloc(vctx_t *ctx, size_t pages, uint64_t flags)
return (void *)new->start; return (void *)new->start;
} }
void *vallocat(vctx_t *ctx, size_t pages, uint64_t flags, uint64_t phys)
{
if (ctx == NULL || ctx->root == NULL || ctx->pagemap == NULL)
return NULL;
vregion_t *region = ctx->root;
vregion_t *new = NULL;
vregion_t *last = ctx->root;
while (region)
{
if (region->next == NULL || region->start + region->pages < region->next->start)
{
new = (vregion_t *)palloc(1, true);
if (!new)
return NULL;
memset(new, 0, sizeof(vregion_t));
new->pages = pages;
new->flags = VFLAGS_TO_PFLAGS(flags);
new->start = region->start + (region->pages * PAGE_SIZE);
new->next = region->next;
new->prev = region;
region->next = new;
for (uint64_t i = 0; i < pages; i++)
{
uint64_t page = phys + i * PAGE_SIZE;
if (page == 0)
return NULL;
vmap(ctx->pagemap, new->start + i * PAGE_SIZE, page, new->flags);
}
return (void *)new->start;
}
region = region->next;
}
new = (vregion_t *)palloc(1, true);
if (!new)
return NULL;
memset(new, 0, sizeof(vregion_t));
last->next = new;
new->prev = last;
new->start = last->start + (last->pages * PAGE_SIZE);
new->pages = pages;
new->flags = VFLAGS_TO_PFLAGS(flags);
new->next = NULL;
for (uint64_t i = 0; i < pages; i++)
{
uint64_t page = phys + i * PAGE_SIZE;
if (page == 0)
return NULL;
vmap(ctx->pagemap, new->start + i * PAGE_SIZE, page, new->flags);
}
return (void *)new->start;
}
void vfree(vctx_t *ctx, void *ptr) void vfree(vctx_t *ctx, void *ptr)
{ {
if (ctx == NULL) if (ctx == NULL)

View file

@ -38,6 +38,7 @@ typedef struct vctx
vctx_t *vinit(uint64_t *pm, uint64_t start); vctx_t *vinit(uint64_t *pm, uint64_t start);
void vdestroy(vctx_t *ctx); void vdestroy(vctx_t *ctx);
void *valloc(vctx_t *ctx, size_t pages, uint64_t flags); void *valloc(vctx_t *ctx, size_t pages, uint64_t flags);
void *vallocat(vctx_t *ctx, size_t pages, uint64_t flags, uint64_t phys);
void vfree(vctx_t *ctx, void *ptr); void vfree(vctx_t *ctx, void *ptr);
#endif // VMM_H #endif // VMM_H

View file

@ -4,6 +4,7 @@
#include <lib/string.h> #include <lib/string.h>
#include <util/log.h> #include <util/log.h>
#include <sys/kpanic.h> #include <sys/kpanic.h>
#include <mm/vmm.h>
static int acpi_uses_xsdt = 0; static int acpi_uses_xsdt = 0;
static void *acpi_rsdt_ptr = NULL; static void *acpi_rsdt_ptr = NULL;
@ -15,7 +16,9 @@ void acpi_init(void)
kpanic(NULL, "No RSDP provided"); kpanic(NULL, "No RSDP provided");
} }
acpi_rsdp_t *rsdp = (acpi_rsdp_t *)HIGHER_HALF(rsdp_response->address); acpi_rsdp_t *rsdp = (acpi_rsdp_t *)vallocat(kvm_ctx, 1, VALLOC_RW, rsdp_response->address);
log_early("Allocated RSDP at %p", rsdp);
if (memcmp(rsdp->signature, "RSD PTR", 7)) if (memcmp(rsdp->signature, "RSD PTR", 7))
kpanic(NULL, "Invalid RSDP signature!"); kpanic(NULL, "Invalid RSDP signature!");