From 6346e288ad54416481972f22b67dc5274968934a Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 2 Dec 2024 03:54:11 +0200 Subject: [PATCH] LibC: Implement getrlimit --- kernel/include/kernel/Thread.h | 5 +-- kernel/kernel/Thread.cpp | 6 ++-- userspace/libraries/LibC/sys/resource.cpp | 43 +++++++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/kernel/include/kernel/Thread.h b/kernel/include/kernel/Thread.h index af38dcc1d3..02f3567525 100644 --- a/kernel/include/kernel/Thread.h +++ b/kernel/include/kernel/Thread.h @@ -30,6 +30,9 @@ namespace Kernel Terminated, }; + static constexpr size_t kernel_stack_size { PAGE_SIZE * 8 }; + static constexpr size_t userspace_stack_size { PAGE_SIZE * 128 }; + public: static BAN::ErrorOr create_kernel(entry_t, void*, Process*); static BAN::ErrorOr create_userspace(Process*, PageTable&); @@ -103,8 +106,6 @@ namespace Kernel // {kernel,userspace}_stack has to be destroyed before page table BAN::UniqPtr m_keep_alive_page_table; - static constexpr size_t m_kernel_stack_size { PAGE_SIZE * 8 }; - static constexpr size_t m_userspace_stack_size { PAGE_SIZE * 128 }; BAN::UniqPtr m_kernel_stack; BAN::UniqPtr m_userspace_stack; const pid_t m_tid { 0 }; diff --git a/kernel/kernel/Thread.cpp b/kernel/kernel/Thread.cpp index ef63b34ddb..38ed8a93b4 100644 --- a/kernel/kernel/Thread.cpp +++ b/kernel/kernel/Thread.cpp @@ -64,7 +64,7 @@ namespace Kernel PageTable::kernel(), KERNEL_OFFSET, ~(uintptr_t)0, - m_kernel_stack_size, + kernel_stack_size, PageTable::Flags::ReadWrite | PageTable::Flags::Present, true )); @@ -104,7 +104,7 @@ namespace Kernel thread->m_kernel_stack = TRY(VirtualRange::create_to_vaddr_range( page_table, 0x300000, KERNEL_OFFSET, - m_kernel_stack_size, + kernel_stack_size, PageTable::Flags::ReadWrite | PageTable::Flags::Present, true )); @@ -112,7 +112,7 @@ namespace Kernel thread->m_userspace_stack = TRY(VirtualRange::create_to_vaddr_range( page_table, 0x300000, KERNEL_OFFSET, - m_userspace_stack_size, + userspace_stack_size, PageTable::Flags::UserSupervisor | PageTable::Flags::ReadWrite | PageTable::Flags::Present, true )); diff --git a/userspace/libraries/LibC/sys/resource.cpp b/userspace/libraries/LibC/sys/resource.cpp index aca16eae2c..1ab4b1f56a 100644 --- a/userspace/libraries/LibC/sys/resource.cpp +++ b/userspace/libraries/LibC/sys/resource.cpp @@ -1,7 +1,50 @@ #include +#include + +#include + #include +#include #include +int getrlimit(int resource, struct rlimit* rlp) +{ + switch (resource) + { + case RLIMIT_CORE: + rlp->rlim_cur = 0; + rlp->rlim_max = 0; + return 0; + case RLIMIT_CPU: + rlp->rlim_cur = BAN::numeric_limits::max(); + rlp->rlim_max = BAN::numeric_limits::max(); + return 0; + case RLIMIT_DATA: + rlp->rlim_cur = BAN::numeric_limits::max(); + rlp->rlim_max = BAN::numeric_limits::max(); + return 0; + case RLIMIT_FSIZE: + rlp->rlim_cur = BAN::numeric_limits::max(); + rlp->rlim_max = BAN::numeric_limits::max(); + return 0; + case RLIMIT_NOFILE: + rlp->rlim_cur = OPEN_MAX; + rlp->rlim_max = OPEN_MAX; + return 0; + case RLIMIT_STACK: + rlp->rlim_cur = Kernel::Thread::userspace_stack_size; + rlp->rlim_max = Kernel::Thread::userspace_stack_size; + return 0; + case RLIMIT_AS: + rlp->rlim_cur = BAN::numeric_limits::max(); + rlp->rlim_max = BAN::numeric_limits::max(); + return 0; + } + + errno = EINVAL; + return -1; +} + int getrusage(int who, struct rusage* r_usage) { if (who != RUSAGE_CHILDREN && who != RUSAGE_SELF)