From 769539698050d9c14064c3830d728f5fc7226a5e Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Fri, 24 Nov 2023 16:16:25 -0800 Subject: [PATCH] [Zion] Add released User Stacks to pool for reuse. --- zion/memory/constants.h | 5 +++++ zion/memory/user_stack_manager.cpp | 15 +++++++++------ zion/memory/user_stack_manager.h | 10 +++++----- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/zion/memory/constants.h b/zion/memory/constants.h index 335746f..2c17ba3 100644 --- a/zion/memory/constants.h +++ b/zion/memory/constants.h @@ -3,6 +3,7 @@ #include const uint64_t KiB = 0x400; +const uint64_t MiB = KiB * KiB; const uint64_t kPageSize = 4 * KiB; const uint64_t kKernelSlabHeapStart = 0xFFFF'FFFF'4000'0000; @@ -20,3 +21,7 @@ const uint64_t kKernelStackEnd = 0xFFFF'FFFF'A000'0000; const uint64_t kKernelStackSize = 3 * kPageSize; const uint64_t kKernelStackOffset = 4 * kPageSize; + +const uint64_t kUserStackMin = 0x00007FF0'00000000; +const uint64_t kUserStackMax = 0x00008000'00000000; +const uint64_t kUserStackSize = MiB; diff --git a/zion/memory/user_stack_manager.cpp b/zion/memory/user_stack_manager.cpp index d714b8a..ffb91ae 100644 --- a/zion/memory/user_stack_manager.cpp +++ b/zion/memory/user_stack_manager.cpp @@ -4,12 +4,16 @@ #include "memory/paging_util.h" uint64_t UserStackManager::NewUserStack() { + if (!freed_stacks_.empty()) { + return freed_stacks_.PopFront(); + } + uint64_t stack = next_stack_; - next_stack_ -= kStackSize; - if (stack <= kStackMin) { + next_stack_ -= kUserStackSize; + if (stack <= kUserStackMin) { panic("Out of user stacks!"); } - if (stack == kStackMax) { + if (stack == kUserStackMax) { // Add a additional page boudary between kernel and user space. stack -= 0x1000; } @@ -18,12 +22,11 @@ uint64_t UserStackManager::NewUserStack() { } void UserStackManager::FreeUserStack(uint64_t stack_ptr) { - freed_stacks_++; - dbgln("{} freed user stacks", freed_stacks_); + freed_stacks_.PushBack(stack_ptr); } bool UserStackManager::IsValidStack(uint64_t vaddr) { - if (vaddr < next_stack_ || vaddr > (kStackMax - 0x1000)) { + if (vaddr < next_stack_ || vaddr > (kUserStackMax - 0x1000)) { return false; } // Checks if the address is in the first page of the stack. diff --git a/zion/memory/user_stack_manager.h b/zion/memory/user_stack_manager.h index 5ed7a0f..fa549c4 100644 --- a/zion/memory/user_stack_manager.h +++ b/zion/memory/user_stack_manager.h @@ -1,7 +1,10 @@ #pragma once +#include #include +#include "memory/constants.h" + // Per-process class to manage user stacks. // // User stacks live at @@ -24,10 +27,7 @@ class UserStackManager { bool IsValidStack(uint64_t vaddr); private: - const uint64_t kStackMax = 0x00008000'00000000; - const uint64_t kStackMin = 0x00007FF0'00000000; - const uint64_t kStackSize = 0x100000; + uint64_t next_stack_ = kUserStackMax; - uint64_t next_stack_ = kStackMax; - uint64_t freed_stacks_ = 0; + glcr::LinkedList freed_stacks_; };