[Zion] Add released User Stacks to pool for reuse.

This commit is contained in:
Drew Galbraith 2023-11-24 16:16:25 -08:00
parent d1ace374b6
commit 7695396980
3 changed files with 19 additions and 11 deletions

View File

@ -3,6 +3,7 @@
#include <stdint.h> #include <stdint.h>
const uint64_t KiB = 0x400; const uint64_t KiB = 0x400;
const uint64_t MiB = KiB * KiB;
const uint64_t kPageSize = 4 * KiB; const uint64_t kPageSize = 4 * KiB;
const uint64_t kKernelSlabHeapStart = 0xFFFF'FFFF'4000'0000; 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 kKernelStackSize = 3 * kPageSize;
const uint64_t kKernelStackOffset = 4 * kPageSize; const uint64_t kKernelStackOffset = 4 * kPageSize;
const uint64_t kUserStackMin = 0x00007FF0'00000000;
const uint64_t kUserStackMax = 0x00008000'00000000;
const uint64_t kUserStackSize = MiB;

View File

@ -4,12 +4,16 @@
#include "memory/paging_util.h" #include "memory/paging_util.h"
uint64_t UserStackManager::NewUserStack() { uint64_t UserStackManager::NewUserStack() {
if (!freed_stacks_.empty()) {
return freed_stacks_.PopFront();
}
uint64_t stack = next_stack_; uint64_t stack = next_stack_;
next_stack_ -= kStackSize; next_stack_ -= kUserStackSize;
if (stack <= kStackMin) { if (stack <= kUserStackMin) {
panic("Out of user stacks!"); panic("Out of user stacks!");
} }
if (stack == kStackMax) { if (stack == kUserStackMax) {
// Add a additional page boudary between kernel and user space. // Add a additional page boudary between kernel and user space.
stack -= 0x1000; stack -= 0x1000;
} }
@ -18,12 +22,11 @@ uint64_t UserStackManager::NewUserStack() {
} }
void UserStackManager::FreeUserStack(uint64_t stack_ptr) { void UserStackManager::FreeUserStack(uint64_t stack_ptr) {
freed_stacks_++; freed_stacks_.PushBack(stack_ptr);
dbgln("{} freed user stacks", freed_stacks_);
} }
bool UserStackManager::IsValidStack(uint64_t vaddr) { bool UserStackManager::IsValidStack(uint64_t vaddr) {
if (vaddr < next_stack_ || vaddr > (kStackMax - 0x1000)) { if (vaddr < next_stack_ || vaddr > (kUserStackMax - 0x1000)) {
return false; return false;
} }
// Checks if the address is in the first page of the stack. // Checks if the address is in the first page of the stack.

View File

@ -1,7 +1,10 @@
#pragma once #pragma once
#include <glacier/container/linked_list.h>
#include <stdint.h> #include <stdint.h>
#include "memory/constants.h"
// Per-process class to manage user stacks. // Per-process class to manage user stacks.
// //
// User stacks live at // User stacks live at
@ -24,10 +27,7 @@ class UserStackManager {
bool IsValidStack(uint64_t vaddr); bool IsValidStack(uint64_t vaddr);
private: private:
const uint64_t kStackMax = 0x00008000'00000000; uint64_t next_stack_ = kUserStackMax;
const uint64_t kStackMin = 0x00007FF0'00000000;
const uint64_t kStackSize = 0x100000;
uint64_t next_stack_ = kStackMax; glcr::LinkedList<uint64_t> freed_stacks_;
uint64_t freed_stacks_ = 0;
}; };