Kernel: Remove VirtualRanges created in kmalloc memory

These were only used as stacks of kernel threads, but they can just
as well be allocated as full pages from Heap
This commit is contained in:
Bananymous 2024-02-25 21:56:37 +02:00
parent 264eff3ad0
commit 21dc64dc21
3 changed files with 18 additions and 39 deletions

View File

@ -18,8 +18,6 @@ namespace Kernel
static BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> create_to_vaddr(PageTable&, vaddr_t, size_t, PageTable::flags_t flags, bool preallocate_pages);
// Create virtual range to virtual address range
static BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> create_to_vaddr_range(PageTable&, vaddr_t vaddr_start, vaddr_t vaddr_end, size_t, PageTable::flags_t flags, bool preallocate_pages);
// Create virtual range in kernel memory with kmalloc
static BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> create_kmalloc(size_t);
~VirtualRange();
BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> clone(PageTable&);
@ -35,14 +33,13 @@ namespace Kernel
void copy_from(size_t offset, const uint8_t* buffer, size_t bytes);
private:
VirtualRange(PageTable&, bool preallocated, bool kmalloc);
VirtualRange(PageTable&, bool preallocated);
void set_zero();
private:
PageTable& m_page_table;
const bool m_preallocated;
const bool m_kmalloc;
vaddr_t m_vaddr { 0 };
size_t m_size { 0 };
PageTable::flags_t m_flags { 0 };

View File

@ -11,7 +11,7 @@ namespace Kernel
ASSERT(vaddr % PAGE_SIZE == 0);
ASSERT(vaddr > 0);
VirtualRange* result_ptr = new VirtualRange(page_table, preallocate_pages, false);
VirtualRange* result_ptr = new VirtualRange(page_table, preallocate_pages);
if (result_ptr == nullptr)
return BAN::Error::from_errno(ENOMEM);
@ -72,26 +72,9 @@ namespace Kernel
return create_to_vaddr(page_table, vaddr, size, flags, preallocate_pages);
}
BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> VirtualRange::create_kmalloc(size_t size)
{
auto* result_ptr = new VirtualRange(PageTable::kernel(), false, true);
if (!result_ptr)
return BAN::Error::from_errno(ENOMEM);
auto result = BAN::UniqPtr<VirtualRange>::adopt(result_ptr);
result->m_size = size;
result->m_flags = PageTable::Flags::ReadWrite | PageTable::Flags::Present;
result->m_vaddr = (vaddr_t)kmalloc(size);
if (result->m_vaddr == 0)
return BAN::Error::from_errno(ENOMEM);
result->set_zero();
return result;
}
VirtualRange::VirtualRange(PageTable& page_table, bool preallocated, bool kmalloc)
VirtualRange::VirtualRange(PageTable& page_table, bool preallocated)
: m_page_table(page_table)
, m_preallocated(preallocated)
, m_kmalloc(kmalloc)
{ }
VirtualRange::~VirtualRange()
@ -99,18 +82,13 @@ namespace Kernel
if (m_vaddr == 0)
return;
if (m_kmalloc)
kfree((void*)m_vaddr);
else
for (size_t offset = 0; offset < size(); offset += PAGE_SIZE)
{
for (size_t offset = 0; offset < size(); offset += PAGE_SIZE)
{
paddr_t paddr = m_page_table.physical_address_of(vaddr() + offset);
if (paddr)
Heap::get().release_page(paddr);
}
m_page_table.unmap_range(vaddr(), size());
paddr_t paddr = m_page_table.physical_address_of(vaddr() + offset);
if (paddr)
Heap::get().release_page(paddr);
}
m_page_table.unmap_range(vaddr(), size());
}
BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> VirtualRange::clone(PageTable& page_table)
@ -141,7 +119,6 @@ namespace Kernel
BAN::ErrorOr<void> VirtualRange::allocate_page_for_demand_paging(vaddr_t address)
{
ASSERT(!m_kmalloc);
ASSERT(!m_preallocated);
ASSERT(contains(address));
ASSERT(&PageTable::current() == &m_page_table);
@ -161,9 +138,7 @@ namespace Kernel
void VirtualRange::set_zero()
{
PageTable& page_table = PageTable::current();
if (m_kmalloc || &page_table == &m_page_table)
if (&PageTable::current() == &m_page_table || &PageTable::kernel() == &m_page_table)
{
memset((void*)vaddr(), 0, size());
return;
@ -187,7 +162,7 @@ namespace Kernel
ASSERT_LTE(offset, size());
ASSERT_LTE(offset, size() - bytes);
if (m_kmalloc || &PageTable::current() == &m_page_table)
if (&PageTable::current() == &m_page_table || &PageTable::kernel() == &m_page_table)
{
memcpy((void*)(vaddr() + offset), buffer, bytes);
return;

View File

@ -47,7 +47,14 @@ namespace Kernel
BAN::ScopeGuard thread_deleter([thread] { delete thread; });
// Initialize stack and registers
thread->m_stack = TRY(VirtualRange::create_kmalloc(m_kernel_stack_size));
thread->m_stack = TRY(VirtualRange::create_to_vaddr_range(
PageTable::kernel(),
KERNEL_OFFSET,
~(uintptr_t)0,
m_kernel_stack_size,
PageTable::Flags::ReadWrite | PageTable::Flags::Present,
true
));
thread->m_rsp = thread->stack_base() + thread->stack_size();
thread->m_rip = (uintptr_t)entry;