Kernel: Make userspace stack on-demand allocated

Also bump the hardlimit of stack size from 512 KiB->32 MiB. This still
feels quite low but is much better than before :D
This commit is contained in:
Bananymous 2025-08-07 02:49:02 +03:00
parent f5bbcc017c
commit 72f85dce2b
3 changed files with 14 additions and 4 deletions

View File

@ -35,7 +35,9 @@ namespace Kernel
// FIXME: kernel stack does NOT have to be this big, but my recursive AML interpreter
// stack overflows on some machines with 8 page stack
static constexpr size_t kernel_stack_size { PAGE_SIZE * 16 };
static constexpr size_t userspace_stack_size { PAGE_SIZE * 128 };
// TODO: userspace stack is hard limited to 32 MiB, maybe make this dynamic?
static constexpr size_t userspace_stack_size { 32 << 20 };
public:
static BAN::ErrorOr<Thread*> create_kernel(entry_t, void*);

View File

@ -143,7 +143,7 @@ namespace Kernel
BAN::ErrorOr<bool> VirtualRange::allocate_page_for_demand_paging(vaddr_t vaddr)
{
ASSERT(contains(vaddr));
ASSERT(&PageTable::current() == &m_page_table);
vaddr &= PAGE_ADDR_MASK;
if (m_preallocated)
return false;
@ -158,8 +158,11 @@ namespace Kernel
if (m_paddrs[index] == 0)
return BAN::Error::from_errno(ENOMEM);
PageTable::with_fast_page(m_paddrs[index], []{
memset(PageTable::fast_page_as_ptr(), 0x00, PAGE_SIZE);
});
m_page_table.map_page_at(m_paddrs[index], vaddr, m_flags);
memset(reinterpret_cast<void*>(vaddr), 0, PAGE_SIZE);
return true;
}

View File

@ -138,7 +138,7 @@ namespace Kernel
stack_addr_start, USERSPACE_END,
userspace_stack_size,
PageTable::Flags::UserSupervisor | PageTable::Flags::ReadWrite | PageTable::Flags::Present,
true, true
false, true
));
thread_deleter.disable();
@ -217,6 +217,7 @@ namespace Kernel
save_sse();
memcpy(thread->m_sse_storage, m_sse_storage, sizeof(m_sse_storage));
TRY(thread->userspace_stack().allocate_page_for_demand_paging(thread->userspace_stack_top() - PAGE_SIZE));
PageTable::with_fast_page(thread->userspace_stack().paddr_of(thread->userspace_stack_top() - PAGE_SIZE), [=] {
PageTable::fast_page_as<void*>(PAGE_SIZE - sizeof(uintptr_t)) = arg;
});
@ -299,6 +300,10 @@ namespace Kernel
vaddr_t vaddr = userspace_stack_top() - needed_size;
const size_t page_count = BAN::Math::div_round_up(needed_size, PAGE_SIZE);
for (size_t i = 0; i < page_count; i++)
TRY(m_userspace_stack->allocate_page_for_demand_paging(vaddr + i * PAGE_SIZE));
const auto stack_copy_buf =
[this](BAN::ConstByteSpan buffer, vaddr_t vaddr) -> void
{