Kernel: Move PhysicalRange to its own file and add VirtualRange

This commit is contained in:
Bananymous
2023-05-28 16:21:45 +03:00
parent 869de7283f
commit 15842db83e
8 changed files with 312 additions and 150 deletions

View File

@@ -3,50 +3,11 @@
#include <BAN/NoCopyMove.h>
#include <BAN/Vector.h>
#include <stdint.h>
#define PAGE_SIZE 4096
#include <kernel/Memory/PhysicalRange.h>
#include <kernel/SpinLock.h>
namespace Kernel
{
using vaddr_t = uintptr_t;
using paddr_t = uintptr_t;
class PhysicalRange
{
public:
PhysicalRange(paddr_t, size_t);
paddr_t reserve_page();
void release_page(paddr_t);
paddr_t start() const { return m_start; }
paddr_t end() const { return m_start + m_size; }
bool contains(paddr_t addr) const { return m_start <= addr && addr < m_start + m_size; }
size_t usable_memory() const { return m_reservable_pages * PAGE_SIZE; }
private:
struct node
{
node* next;
node* prev;
};
paddr_t page_address(const node*) const;
node* node_address(paddr_t) const;
private:
paddr_t m_start { 0 };
size_t m_size { 0 };
uint64_t m_total_pages { 0 };
uint64_t m_reservable_pages { 0 };
uint64_t m_list_pages { 0 };
node* m_free_list { nullptr };
node* m_used_list { nullptr };
};
class Heap
{
@@ -65,7 +26,8 @@ namespace Kernel
void initialize_impl();
private:
BAN::Vector<PhysicalRange> m_physical_ranges;
BAN::Vector<PhysicalRange> m_physical_ranges;
SpinLock m_lock;
};
}

View File

@@ -0,0 +1,46 @@
#pragma once
#include <kernel/Memory/Types.h>
#include <stddef.h>
#include <stdint.h>
namespace Kernel
{
class PhysicalRange
{
public:
PhysicalRange(paddr_t, size_t);
paddr_t reserve_page();
void release_page(paddr_t);
paddr_t start() const { return m_start; }
paddr_t end() const { return m_start + m_size; }
bool contains(paddr_t addr) const { return m_start <= addr && addr < m_start + m_size; }
size_t usable_memory() const { return m_reservable_pages * PAGE_SIZE; }
private:
struct node
{
node* next;
node* prev;
};
paddr_t page_address(const node*) const;
node* node_address(paddr_t) const;
private:
paddr_t m_start { 0 };
size_t m_size { 0 };
uint64_t m_total_pages { 0 };
uint64_t m_reservable_pages { 0 };
uint64_t m_list_pages { 0 };
node* m_free_list { nullptr };
node* m_used_list { nullptr };
};
}

View File

@@ -0,0 +1,11 @@
#pragma once
#define PAGE_SIZE 4096
namespace Kernel
{
using vaddr_t = uintptr_t;
using paddr_t = uintptr_t;
}

View File

@@ -0,0 +1,38 @@
#pragma once
#include <BAN/Vector.h>
#include <BAN/NoCopyMove.h>
#include <kernel/Memory/MMU.h>
#include <kernel/Memory/Types.h>
namespace Kernel
{
class VirtualRange
{
BAN_NON_COPYABLE(VirtualRange);
BAN_NON_MOVABLE(VirtualRange);
public:
static VirtualRange* create(MMU&, vaddr_t, size_t, uint8_t flags);
static VirtualRange* create_kmalloc(size_t);
~VirtualRange();
VirtualRange* clone(MMU& new_mmu);
vaddr_t vaddr() const { return m_vaddr; }
size_t size() const { return m_size; }
uint8_t flags() const { return m_flags; }
private:
VirtualRange(MMU&);
private:
MMU& m_mmu;
vaddr_t m_vaddr { 0 };
size_t m_size { 0 };
uint8_t m_flags { 0 };
BAN::Vector<paddr_t> m_physical_pages;
};
}