FreeNOS
MemoryContext.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __LIBARCH_MEMORYCONTEXT_H
19 #define __LIBARCH_MEMORYCONTEXT_H
20 
21 #include <Types.h>
22 #include <Macros.h>
23 #include <BitOperations.h>
24 #include <Callback.h>
25 #include "Memory.h"
26 #include "MemoryMap.h"
27 
29 class SplitAllocator;
30 
43 {
44  public:
45 
49  typedef enum Result
50  {
56  }
57  Result;
58 
66 
70  virtual ~MemoryContext();
71 
77  static MemoryContext * getCurrent();
78 
84  virtual Result initialize() = 0;
85 
95  virtual Result activate(bool initializeMMU = false) = 0;
96 
106  virtual Result map(Address virt, Address phys, Memory::Access access) = 0;
107 
118  virtual Result unmap(Address virt) = 0;
119 
128  virtual Result lookup(Address virt, Address *phys) const = 0;
129 
138  virtual Result access(Address virt, Memory::Access *access) const = 0;
139 
147  virtual Result mapRangeContiguous(Memory::Range *range);
148 
156  virtual Result mapRangeSparse(Memory::Range *range);
157 
165  virtual Result unmapRange(Memory::Range *range);
166 
174  virtual Result release(Address virt);
175 
183  virtual Result releaseRange(Memory::Range *range) = 0;
184 
197  virtual Result releaseSection(const Memory::Range & range,
198  const bool tablesOnly = false) = 0;
199 
213  virtual Result findFree(Size size, MemoryMap::Region region, Address *virt) const;
214 
222  virtual void mapRangeSparseCallback(Address *phys);
223 
224  protected:
225 
228 
231 
234 
237 
240 
243 };
244 
250 #endif /* __LIBARCH_MEMORYCONTEXT_H */
MemoryContext::initialize
virtual Result initialize()=0
Initialize the MemoryContext.
MemoryContext::m_savedRange
Memory::Range * m_savedRange
Saved range input for use in the mapRangeSparse Callback.
Definition: MemoryContext.h:239
Memory::Range
Memory range.
Definition: Memory.h:55
MemoryContext
Virtual memory abstract interface.
Definition: MemoryContext.h:42
Macros.h
MemoryContext::unmapRange
virtual Result unmapRange(Memory::Range *range)
Unmaps a range of virtual memory.
Definition: MemoryContext.cpp:95
MemoryContext::access
virtual Result access(Address virt, Memory::Access *access) const =0
Get Access flags for a virtual address.
MemoryContext::releaseRange
virtual Result releaseRange(Memory::Range *range)=0
Release a range of physical memory by its virtual memory pages.
Types.h
MemoryContext::MemoryContext
MemoryContext(MemoryMap *map, SplitAllocator *alloc)
Constructor.
Definition: MemoryContext.cpp:28
MemoryContext::release
virtual Result release(Address virt)
Release a memory page mapping.
Definition: MemoryContext.cpp:106
MemoryContext::map
virtual Result map(Address virt, Address phys, Memory::Access access)=0
Map a physical page to a virtual address.
MemoryContext::AlreadyExists
@ AlreadyExists
Definition: MemoryContext.h:54
Address
unsigned long Address
A memory address.
Definition: Types.h:131
BitOperations.h
MemoryContext::unmap
virtual Result unmap(Address virt)=0
Unmap a virtual address.
MemoryContext::InvalidAddress
@ InvalidAddress
Definition: MemoryContext.h:52
Callback.h
SplitAllocator
Allocator which separates kernel mapped memory at virtual and physical addresses.
Definition: SplitAllocator.h:37
MemoryContext::lookup
virtual Result lookup(Address virt, Address *phys) const =0
Translate virtual address to physical address.
MemoryContext::activate
virtual Result activate(bool initializeMMU=false)=0
Activate the MemoryContext.
MemoryContext::m_mapRangeSparseCallback
Callback< MemoryContext, Address > m_mapRangeSparseCallback
Callback object for mapRangeSparseCallback function.
Definition: MemoryContext.h:236
MemoryContext::Result
Result
Result codes.
Definition: MemoryContext.h:49
MemoryContext::InvalidSize
@ InvalidSize
Definition: MemoryContext.h:53
MemoryContext::mapRangeSparseCallback
virtual void mapRangeSparseCallback(Address *phys)
Callback to provide intermediate Range object during mapRangeSparse()
Definition: MemoryContext.cpp:146
MemoryContext::Success
@ Success
Definition: MemoryContext.h:51
MemoryContext::findFree
virtual Result findFree(Size size, MemoryMap::Region region, Address *virt) const
Find unused memory.
Definition: MemoryContext.cpp:117
MemoryMap
Describes virtual memory map layout.
Definition: MemoryMap.h:38
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
MemoryMap.h
MemoryMap::Region
Region
Memory regions.
Definition: MemoryMap.h:52
Callback< MemoryContext, Address >
Memory::Access
Access
Memory access flags.
Definition: Memory.h:38
MemoryContext::OutOfMemory
@ OutOfMemory
Definition: MemoryContext.h:55
MemoryContext::getCurrent
static MemoryContext * getCurrent()
Get currently active MemoryContext.
Definition: MemoryContext.cpp:41
Memory.h
MemoryContext::m_numSparsePages
Size m_numSparsePages
Number of pages allocated via mapRangeSparse Callback.
Definition: MemoryContext.h:242
MemoryContext::m_current
static MemoryContext * m_current
The currently active MemoryContext.
Definition: MemoryContext.h:233
MemoryContext::~MemoryContext
virtual ~MemoryContext()
Destructor.
Definition: MemoryContext.cpp:37
MemoryContext::releaseSection
virtual Result releaseSection(const Memory::Range &range, const bool tablesOnly=false)=0
Release memory sections.
MemoryContext::m_alloc
SplitAllocator * m_alloc
Physical memory allocator.
Definition: MemoryContext.h:227
MemoryContext::mapRangeContiguous
virtual Result mapRangeContiguous(Memory::Range *range)
Map a range of contiguous physical pages to virtual addresses.
Definition: MemoryContext.cpp:46
MemoryContext::mapRangeSparse
virtual Result mapRangeSparse(Memory::Range *range)
Map and allocate a range of sparse (non-contiguous) physical pages to virtual addresses.
Definition: MemoryContext.cpp:76
MemoryContext::m_map
MemoryMap * m_map
Virtual memory layout.
Definition: MemoryContext.h:230