Compare commits

...

2 Commits

4 changed files with 78 additions and 38 deletions

View File

@ -16,6 +16,23 @@ class LinkedList {
bool empty() const { return size_ == 0; }
uint64_t size() const { return size_; }
void PushFront(const T& item) {
ListItem* new_item = new ListItem{
.item = item,
.next = front_,
};
front_ = new_item;
size_++;
}
void PushFront(T&& item) {
ListItem* new_item = new ListItem{
.item = glcr::Move(item),
.next = front_,
};
front_ = new_item;
size_++;
}
void PushBack(const T& item) {
ListItem* new_item = new ListItem{
.item = item,
@ -42,7 +59,8 @@ class LinkedList {
return ret;
}
T& PeekFront() const { return front_->item; }
T& PeekFront() { return front_->item; }
const T& PeekFront() const { return front_->item; }
struct ListItem {
T item;

View File

@ -7,6 +7,7 @@
#include "interrupt/apic.h"
#include "interrupt/apic_timer.h"
#include "memory/kernel_heap.h"
#include "memory/physical_memory.h"
#include "scheduler/scheduler.h"
#define IDT_INTERRUPT_GATE 0x8E
@ -142,6 +143,7 @@ extern "C" void interrupt_apic_timer(InterruptFrame*) {
if (cnt % 20 == 0) {
if (cnt == 20) {
KernelHeap::DumpDebugData();
phys_mem::DumpPhysicalMemoryUsage();
}
dbgln("timer: {}s", cnt * 50 / 1000);
}

View File

@ -1,5 +1,7 @@
#include "memory/physical_memory.h"
#include <glacier/container/linked_list.h>
#include "boot/boot_info.h"
#include "debug/debug.h"
@ -53,78 +55,89 @@ class PhysicalMemoryManager {
}
uint64_t AllocatePage() {
if (front_ == nullptr) {
if (memory_blocks.size() == 0) {
panic("No available memory regions.");
}
if (front_->num_pages == 0) {
panic("Bad state, empty memory block.");
while (memory_blocks.PeekFront().num_pages == 0) {
memory_blocks.PopFront();
}
uint64_t page = front_->base;
front_->base += 0x1000;
front_->num_pages--;
if (front_->num_pages == 0) {
MemBlock* temp = front_;
front_ = front_->next;
delete temp;
MemBlock& block = memory_blocks.PeekFront();
uint64_t page = block.base;
block.base += 0x1000;
block.num_pages--;
if (block.num_pages == 0) {
memory_blocks.PopFront();
}
#if K_PHYS_DEBUG
dbgln("Single {x}", page);
#endif
allocated_pages_ += 1;
return page;
}
uint64_t AllocateContinuous(uint64_t num_pages) {
if (front_ == nullptr) {
if (memory_blocks.size() == 0) {
panic("No available memory regions.");
}
if (front_->num_pages == 0) {
MemBlock& block = memory_blocks.PeekFront();
if (block.num_pages == 0) {
panic("Bad state, empty memory block.");
}
MemBlock* block = front_;
while (block != nullptr && block->num_pages < num_pages) {
dbgln("Skipping block of size {} seeking {}", block->num_pages,
num_pages);
block = block->next;
auto iter = memory_blocks.begin();
while (iter != memory_blocks.end() && iter->num_pages < num_pages) {
dbgln("Skipping block of size {} seeking {}", iter->num_pages, num_pages);
iter = iter.next();
}
if (block == nullptr) {
if (iter == memory_blocks.end()) {
panic("No memory regions to allocate");
}
uint64_t page = front_->base;
front_->base += num_pages * 0x1000;
front_->num_pages -= num_pages;
if (front_->num_pages == 0) {
MemBlock* temp = front_;
front_ = front_->next;
delete temp;
}
uint64_t page = iter->base;
iter->base += num_pages * 0x1000;
iter->num_pages -= num_pages;
#if K_PHYS_DEBUG
dbgln("Continuous {x}:{}", page, num_pages);
#endif
allocated_pages_ += num_pages;
return page;
}
void FreePage(uint64_t page) { AddMemoryRegion(page, 0x1000); }
void FreePage(uint64_t page) {
AddMemoryRegion(page, 0x1000);
allocated_pages_--;
}
uint64_t AllocatedPages() { return allocated_pages_; }
uint64_t AvailablePages() {
uint64_t available = 0;
for (auto iter = memory_blocks.begin(); iter != memory_blocks.end();
iter = iter.next()) {
available += iter->num_pages;
}
return available;
}
private:
void AddMemoryRegion(uint64_t base, uint64_t size) {
MemBlock* block = new MemBlock{
.next = front_,
.base = base,
.num_pages = size >> 12,
};
front_ = block;
}
struct MemBlock {
MemBlock* next = nullptr;
uint64_t base = 0;
uint64_t num_pages = 0;
};
MemBlock* front_ = nullptr;
glcr::LinkedList<MemBlock> memory_blocks;
uint64_t allocated_pages_ = 0;
void AddMemoryRegion(uint64_t base, uint64_t size) {
MemBlock block{
.base = base,
.num_pages = size >> 12,
};
memory_blocks.PushFront(block);
}
};
static PhysicalMemoryManager* gPmm = nullptr;
@ -220,4 +233,9 @@ uint64_t AllocateContinuous(uint64_t num_continuous) {
return gPmm->AllocateContinuous(num_continuous);
}
void DumpPhysicalMemoryUsage() {
dbgln("Pages used: {} MiB, avail: {} MiB", gPmm->AllocatedPages() / 256,
gPmm->AvailablePages() / 256);
}
} // namespace phys_mem

View File

@ -21,4 +21,6 @@ uint64_t AllocateAndZeroPage();
uint64_t AllocateContinuous(uint64_t num_pages);
void FreePage(uint64_t page);
void DumpPhysicalMemoryUsage();
} // namespace phys_mem