Kernel: Move PhysicalRange to its own file and add VirtualRange
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
46
kernel/include/kernel/Memory/PhysicalRange.h
Normal file
46
kernel/include/kernel/Memory/PhysicalRange.h
Normal 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 };
|
||||
};
|
||||
|
||||
}
|
||||
11
kernel/include/kernel/Memory/Types.h
Normal file
11
kernel/include/kernel/Memory/Types.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#define PAGE_SIZE 4096
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
using vaddr_t = uintptr_t;
|
||||
using paddr_t = uintptr_t;
|
||||
|
||||
}
|
||||
38
kernel/include/kernel/Memory/VirtualRange.h
Normal file
38
kernel/include/kernel/Memory/VirtualRange.h
Normal 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;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user