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-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) {
|
|
|
|
freed_stack_cnt_++;
|
2023-11-05 09:24:09 -08:00
|
|
|
dbgln("Freed kernel stacks using {} KiB", freed_stack_cnt_ * 12);
|
2023-05-30 21:27:20 -07:00
|
|
|
}
|