2023-05-30 21:27:20 -07:00
|
|
|
#include "memory/kernel_stack_manager.h"
|
|
|
|
|
2023-08-01 23:11:12 -07:00
|
|
|
#include "common/gdt.h"
|
2023-05-30 21:27:20 -07:00
|
|
|
#include "debug/debug.h"
|
2023-11-15 15:38:25 -08:00
|
|
|
#include "interrupt/interrupt.h"
|
2023-11-15 15:55:24 -08:00
|
|
|
#include "memory/constants.h"
|
2023-05-30 21:27:20 -07:00
|
|
|
#include "memory/paging_util.h"
|
|
|
|
|
|
|
|
KernelStackManager::KernelStackManager()
|
2023-11-15 15:55:24 -08:00
|
|
|
: next_stack_addr_(kKernelStackStart) {}
|
2023-05-30 21:27:20 -07:00
|
|
|
|
2023-11-15 15:38:25 -08:00
|
|
|
void KernelStackManager::SetupInterruptStack() {
|
|
|
|
SetIst1(AllocateKernelStack());
|
|
|
|
UpdateFaultHandlersToIst1();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t KernelStackManager::AllocateKernelStack() {
|
2023-11-24 16:04:40 -08:00
|
|
|
if (!free_stacks_.empty()) {
|
|
|
|
return free_stacks_.PopFront();
|
|
|
|
}
|
2023-11-15 15:55:24 -08:00
|
|
|
next_stack_addr_ += kKernelStackOffset;
|
|
|
|
if (next_stack_addr_ >= kKernelStackEnd) {
|
|
|
|
panic("No more kernel stack space");
|
2023-05-30 21:27:20 -07:00
|
|
|
}
|
2023-11-15 15:55:24 -08:00
|
|
|
EnsureResident(next_stack_addr_ - kKernelStackSize, kKernelStackSize);
|
2023-11-15 15:38:25 -08:00
|
|
|
return next_stack_addr_ - 8;
|
2023-05-30 21:27:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void KernelStackManager::FreeKernelStack(uint64_t stack_base) {
|
2023-11-24 16:04:40 -08:00
|
|
|
// TODO: Validate this value.
|
|
|
|
if ((stack_base & 0x3FF8) != 0x3FF8) {
|
|
|
|
dbgln("Odd kernel stack free {x}", stack_base);
|
|
|
|
}
|
|
|
|
free_stacks_.PushFront(stack_base);
|
2023-05-30 21:27:20 -07:00
|
|
|
}
|