Xyris  0.5
Physical.hpp
Go to the documentation of this file.
1 /**
2  * @file Physical.hpp
3  * @author Keeton Feavel ([email protected])
4  * @brief Physical memory mapping
5  * @version 0.1
6  * @date 2021-10-28
7  *
8  * @copyright Copyright the Xyris Contributors (c) 2021
9  *
10  */
11 #pragma once
12 
13 #include <Arch/Memory.hpp>
14 #include <Library/Bitset.hpp>
15 #include <Memory/MemorySection.hpp>
16 #include <Logger.hpp>
17 #include <stddef.h>
18 #include <stdint.h>
19 
20 #define KADDR_TO_PHYS(addr) ((addr) - KERNEL_BASE)
21 
22 namespace Memory::Physical {
23 
25 public:
26 /*
27  PhysicalManager()
28  : m_memory(SIZE_MAX)
29  {
30  // Always assume memory is reserved until proven otherwise
31  }
32 */
33  [[gnu::always_inline]] void setFree(Section& sect)
34  {
35  for (size_t i = 0; i < sect.pages(); i++) {
36  setFree(ADDRESS_TO_PAGE_IDX(sect.base()) + i);
37  }
38  }
39 
40  [[gnu::always_inline]] void setUsed(Section& sect)
41  {
43  __func__,
44  "0x%08zX-0x%08zX 0x%08zX [%zu] [%s]",
45  sect.base(),
46  sect.end(),
47  sect.size(),
48  sect.pages(),
49  sect.typeString());
50  // TODO: Optimize bitmap library to take number of bits to set
51  for (size_t i = 0; i < sect.pages(); i++) {
52  setUsed(ADDRESS_TO_PAGE_IDX(sect.base()) + i);
53  }
54  }
55 
56  [[gnu::always_inline]] void setFree(Arch::Memory::Address addr)
57  {
58  if (!isFree(addr)) {
60  }
61  }
62 
63  [[gnu::always_inline]] void setUsed(Arch::Memory::Address addr)
64  {
65  if (isFree(addr)) {
67  }
68  }
69 
70  [[gnu::always_inline]] void setFree(uintptr_t addr)
71  {
72  if (!isFree(addr)) {
74  }
75  }
76 
77  [[gnu::always_inline]] void setUsed(uintptr_t addr)
78  {
79  if (isFree(addr)) {
81  }
82  }
83 
84  [[gnu::always_inline]] bool isFree(uintptr_t addr)
85  {
86  return m_memory.Test(ADDRESS_TO_PAGE_IDX(addr)) == 0;
87  }
88 
89  [[gnu::always_inline]] bool isFree(Section& sect)
90  {
91  // TODO: Optimize bitmap library to take number of bits to set
92  for (size_t i = 0; i < sect.pages(); i++) {
93  if (!isFree(ADDRESS_TO_PAGE_IDX(sect.base()) + i)) {
94  return false;
95  }
96  }
97  return true;
98  }
99 
100  [[gnu::always_inline]] uintptr_t findNextFreePhysicalAddress()
101  {
102  return m_memory.FindFirstBit(false);
103  }
104 
105 private:
107 };
108 
109 }
Memory::Physical::PhysicalManager::isFree
bool isFree(uintptr_t addr)
Definition: Physical.hpp:84
Memory::Physical::PhysicalManager::setUsed
void setUsed(uintptr_t addr)
Definition: Physical.hpp:77
Memory::Physical::PhysicalManager::setFree
void setFree(uintptr_t addr)
Definition: Physical.hpp:70
Bitset::Test
bool Test(size_t pos)
Return the value of the bit at the given position.
Definition: Bitset.hpp:91
Memory::Physical::PhysicalManager
Definition: Physical.hpp:24
Memory::Physical::PhysicalManager::setFree
void setFree(Arch::Memory::Address addr)
Definition: Physical.hpp:56
Bitset::Set
void Set(size_t pos)
Set the bit for a given position.
Definition: Bitset.hpp:60
MemorySection.hpp
Bitset.hpp
A basic bitmap implementation.
Memory::Section::pages
uintptr_t pages()
Definition: MemorySection.hpp:72
Memory::Physical::PhysicalManager::setUsed
void setUsed(Arch::Memory::Address addr)
Definition: Physical.hpp:63
Memory::Physical::PhysicalManager::m_memory
Bitset< MEM_BITMAP_SIZE > m_memory
Definition: Physical.hpp:106
Memory::Section::size
uintptr_t size()
Definition: MemorySection.hpp:67
Bitset::FindFirstBit
size_t FindFirstBit(bool isSet)
Finds and returns the position of the first clear bit.
Definition: Bitset.hpp:116
Memory::Physical::PhysicalManager::setFree
void setFree(Section &sect)
Definition: Physical.hpp:33
Memory::Physical
Definition: Physical.hpp:22
Memory::Physical::PhysicalManager::isFree
bool isFree(Section &sect)
Definition: Physical.hpp:89
Memory::Section
Definition: MemorySection.hpp:29
Memory::Section::typeString
const char * typeString()
Definition: MemorySection.hpp:82
Logger::Debug
static void Debug(const char *tag, const char *fmt,...)
Definition: Logger.cpp:71
Memory.hpp
Architecture memory management & paging API.
Memory::Section::base
uintptr_t base()
Definition: MemorySection.hpp:57
Memory::Section::end
uintptr_t end()
Definition: MemorySection.hpp:62
Memory::Physical::PhysicalManager::setUsed
void setUsed(Section &sect)
Definition: Physical.hpp:40
ADDRESS_TO_PAGE_IDX
#define ADDRESS_TO_PAGE_IDX(addr)
Definition: Memory.hpp:28
Bitset< MEM_BITMAP_SIZE >
Memory::Physical::PhysicalManager::findNextFreePhysicalAddress
uintptr_t findNextFreePhysicalAddress()
Definition: Physical.hpp:100
Logger.hpp
Bitset::Reset
void Reset(size_t pos)
Reset (clear) the bit at the given position.
Definition: Bitset.hpp:70