Kernel: Add kmalloc helper APIs

This commit is contained in:
Bananymous 2024-10-14 11:39:04 +03:00
parent 5f63ea8f8a
commit f0b18da881
2 changed files with 23 additions and 5 deletions

View File

@ -13,3 +13,4 @@ void* kmalloc(size_t size, size_t align, bool force_identity_map = false);
void kfree(void*); void kfree(void*);
BAN::Optional<Kernel::paddr_t> kmalloc_paddr_of(Kernel::vaddr_t); BAN::Optional<Kernel::paddr_t> kmalloc_paddr_of(Kernel::vaddr_t);
BAN::Optional<Kernel::vaddr_t> kmalloc_vaddr_of(Kernel::paddr_t);

View File

@ -418,12 +418,29 @@ void kfree(void* address)
} }
static bool is_kmalloc_vaddr(Kernel::vaddr_t vaddr)
{
using namespace Kernel;
if (vaddr < reinterpret_cast<vaddr_t>(s_kmalloc_storage))
return false;
if (vaddr >= reinterpret_cast<vaddr_t>(s_kmalloc_storage) + sizeof(s_kmalloc_storage))
return false;
return true;
}
BAN::Optional<Kernel::paddr_t> kmalloc_paddr_of(Kernel::vaddr_t vaddr) BAN::Optional<Kernel::paddr_t> kmalloc_paddr_of(Kernel::vaddr_t vaddr)
{ {
using namespace Kernel; using namespace Kernel;
if (!is_kmalloc_vaddr(vaddr))
if ((vaddr_t)s_kmalloc_storage <= vaddr && vaddr < (vaddr_t)s_kmalloc_storage + sizeof(s_kmalloc_storage))
return vaddr - KERNEL_OFFSET + g_boot_info.kernel_paddr;
return {}; return {};
return vaddr - KERNEL_OFFSET + g_boot_info.kernel_paddr;
}
BAN::Optional<Kernel::vaddr_t> kmalloc_vaddr_of(Kernel::paddr_t paddr)
{
using namespace Kernel;
const vaddr_t vaddr = paddr + KERNEL_OFFSET - g_boot_info.kernel_paddr;
if (!is_kmalloc_vaddr(vaddr))
return {};
return vaddr;
} }