gem5
v20.1.0.0
|
This class holds the memory state for the Process class and all of its derived, architecture-specific children. More...
#include <mem_state.hh>
Public Member Functions | |
MemState (Process *owner, Addr brk_point, Addr stack_base, Addr max_stack_size, Addr next_thread_stack_base, Addr mmap_end) | |
MemState & | operator= (const MemState &in) |
void | resetOwner (Process *owner) |
Change the Process owner in case this MemState is copied. More... | |
Addr | getBrkPoint () const |
Get/set base addresses and sizes for the stack and data segments of the process' memory. More... | |
Addr | getStackBase () const |
Addr | getStackSize () const |
Addr | getMaxStackSize () const |
Addr | getStackMin () const |
Addr | getNextThreadStackBase () const |
Addr | getMmapEnd () const |
void | setBrkPoint (Addr brk_point) |
void | setStackBase (Addr stack_base) |
void | setStackSize (Addr stack_size) |
void | setMaxStackSize (Addr max_stack) |
void | setStackMin (Addr stack_min) |
void | setNextThreadStackBase (Addr ntsb) |
void | setMmapEnd (Addr mmap_end) |
Addr | extendMmap (Addr length) |
bool | isUnmapped (Addr start_addr, Addr length) |
Check if any page in the virtual address range from start_addr to start_addr + length is already mapped in the page table. More... | |
void | mapRegion (Addr start_addr, Addr length, const std::string &name="anon", int sim_fd=-1, Addr offset=0) |
Add a new memory region. More... | |
void | unmapRegion (Addr start_addr, Addr length) |
Unmap a pre-existing region. More... | |
void | remapRegion (Addr start_addr, Addr new_start_addr, Addr length) |
Remap a pre-existing region. More... | |
void | updateBrkRegion (Addr old_brk, Addr new_brk) |
Change the end of a process' program break. More... | |
bool | fixupFault (Addr vaddr) |
Attempt to fix up a fault at vaddr by allocating a page. More... | |
void | allocateMem (Addr vaddr, int64_t size, bool clobber=false) |
Given the vaddr and size, this method will chunk the allocation into page granularity and then request physical pages (frames) from the system object. More... | |
void | serialize (CheckpointOut &cp) const override |
Serialize an object. More... | |
void | unserialize (CheckpointIn &cp) override |
Unserialize an object. More... | |
std::string | printVmaList () |
Print the list of VMAs in a format similar to /proc/self/maps. More... | |
Public Member Functions inherited from Serializable | |
Serializable () | |
virtual | ~Serializable () |
void | serializeSection (CheckpointOut &cp, const char *name) const |
Serialize an object into a new section. More... | |
void | serializeSection (CheckpointOut &cp, const std::string &name) const |
void | unserializeSection (CheckpointIn &cp, const char *name) |
Unserialize an a child object. More... | |
void | unserializeSection (CheckpointIn &cp, const std::string &name) |
Private Attributes | |
Process * | _ownerProcess |
Owner process of MemState. More... | |
Addr | _pageBytes |
Addr | _brkPoint |
Addr | _stackBase |
Addr | _stackSize |
Addr | _maxStackSize |
Addr | _stackMin |
Addr | _nextThreadStackBase |
Addr | _mmapEnd |
Addr | _endBrkPoint |
Keeps record of the furthest mapped heap location. More... | |
std::list< VMA > | _vmaList |
The _vmaList member is a list of virtual memory areas in the target application space that have been allocated by the target. More... | |
Additional Inherited Members | |
Static Public Member Functions inherited from Serializable | |
static const std::string & | currentSection () |
Gets the fully-qualified name of the active section. More... | |
static void | serializeAll (const std::string &cpt_dir) |
Serializes all the SimObjects. More... | |
static void | unserializeGlobals (CheckpointIn &cp) |
This class holds the memory state for the Process class and all of its derived, architecture-specific children.
The class represents the Process' address space which may change dynamically while the simulation is running. They are updated by system calls and faults. Each change represents a modification to the process address space.
The class is meant to be allocated dynamically and shared through a pointer interface. Multiple process can potentially share portions of their virtual address space if specific options are passed into the clone(2) system call.
Definition at line 62 of file mem_state.hh.
MemState::MemState | ( | Process * | owner, |
Addr | brk_point, | ||
Addr | stack_base, | ||
Addr | max_stack_size, | ||
Addr | next_thread_stack_base, | ||
Addr | mmap_end | ||
) |
Definition at line 41 of file mem_state.cc.
void MemState::allocateMem | ( | Addr | vaddr, |
int64_t | size, | ||
bool | clobber = false |
||
) |
Given the vaddr and size, this method will chunk the allocation into page granularity and then request physical pages (frames) from the system object.
After retrieving a frame, the method updates the page table mappings.
vaddr | The virtual address in need of a frame allocation. |
size | The size in bytes of the requested mapping. |
clobber | This flag specifies whether mappings in the page tables can be overwritten and replaced with the new mapping. |
Definition at line 451 of file mem_state.cc.
References _mmapEnd, _ownerProcess, _pageBytes, DPRINTF, isUnmapped(), length, and Process::mmapGrowsDown().
bool MemState::fixupFault | ( | Addr | vaddr | ) |
Attempt to fix up a fault at vaddr by allocating a page.
The fault likely occurred because a virtual page which does not have physical page assignment is being accessed.
vaddr | The virtual address which is causing the fault. |
Check if we are accessing a mapped virtual address. If so then we just haven't allocated it a physical page yet and can do so here.
We are assuming that fresh pages are zero-filled, so there is no need to zero them out when there is no backing file. This assumption will not hold true if/when physical pages are recycled.
Write the memory for the host buffer contents for all ThreadContexts associated with this process.
Check if the stack needs to be grown in the case where the ISAs process argsInit does not explicitly map the entire stack.
Check if this is already on the stack and there's just no page there yet.
We've accessed the next page of the stack, so extend it to include this address.
Definition at line 386 of file mem_state.cc.
References _maxStackSize, _ownerProcess, _pageBytes, _stackBase, _stackMin, _vmaList, Process::allocateMem(), SETranslatingPortProxy::Always, Process::contextIds, fatal, inform, roundDown(), Process::system, System::threads, and MipsISA::vaddr.
|
inline |
Get/set base addresses and sizes for the stack and data segments of the process' memory.
Definition at line 80 of file mem_state.hh.
References _brkPoint.
|
inline |
Definition at line 83 of file mem_state.hh.
References _maxStackSize.
|
inline |
Definition at line 86 of file mem_state.hh.
References _mmapEnd.
|
inline |
Definition at line 85 of file mem_state.hh.
References _nextThreadStackBase.
|
inline |
Definition at line 81 of file mem_state.hh.
References _stackBase.
|
inline |
Definition at line 84 of file mem_state.hh.
References _stackMin.
|
inline |
Definition at line 82 of file mem_state.hh.
References _stackSize.
Check if any page in the virtual address range from start_addr to start_addr + length is already mapped in the page table.
start_addr | Starting address of region to check. |
length | Length of the range to check. |
In case someone skips the VMA interface and just directly maps memory also consult the page tables to make sure that this memory isnt mapped.
Definition at line 80 of file mem_state.cc.
References _ownerProcess, _pageBytes, _vmaList, length, EmulationPageTable::lookup(), panic, and Process::pTable.
Referenced by extendMmap(), mapRegion(), and updateBrkRegion().
void MemState::mapRegion | ( | Addr | start_addr, |
Addr | length, | ||
const std::string & | name = "anon" , |
||
int | sim_fd = -1 , |
||
Addr | offset = 0 |
||
) |
Add a new memory region.
The region represents a contiguous virtual address range which can map to physical memory or a host-backed file. Regions which are not file-backed should use -1 for sim_fd and 0 for offset.
start_addr | Starting address of the region. |
length | Size of the region. |
name | Name of region. Optional. |
sim_fd | File descriptor for file-backed regions or -1. |
offset | Offset in file in which region starts. |
Avoid creating a region that has preexisting mappings. This should not happen under normal circumstances so consider this to be a bug.
Record the region in our list structure.
Definition at line 164 of file mem_state.cc.
References _pageBytes, _vmaList, DPRINTF, isUnmapped(), length, and ArmISA::offset.
Referenced by updateBrkRegion().
Definition at line 54 of file mem_state.cc.
References _brkPoint, _endBrkPoint, _maxStackSize, _mmapEnd, _nextThreadStackBase, _pageBytes, _stackBase, _stackMin, _stackSize, and _vmaList.
std::string MemState::printVmaList | ( | ) |
Print the list of VMAs in a format similar to /proc/self/maps.
Definition at line 479 of file mem_state.cc.
References _vmaList.
Remap a pre-existing region.
This changes the virtual address range of the region. This will result in regions being expanded if there is overlap with another region or simply moving the range otherwise.
start_addr | Start address of region being remapped. |
new_start_addr | New start address of the region. |
length | Length of the newly remapped region. |
Create clone of the old VMA and slice right.
Create clone of the old VMA and slice it left.
Slice the old VMA left and right to adjust the file backing, then overwrite the virtual addresses!
The region cannot be in any more VMAs, because it is completely contained in this one!
Just go ahead and remap it!
Create a clone of the old VMA.
Overlaps from the right.
Remap the old region.
Overlaps from the left.
Remap the old region.
TLBs need to be flushed to remove any stale mappings from regions which were remapped. Currently the entire TLB is flushed. This results in functionally correct execution, but real systems do not flush all entries when a single mapping changes since it degrades performance. There is currently no general method across all TLB implementations that can flush just part of the address space.
The regions need to always be page-aligned otherwise the while condition will loop indefinitely. (The Addr type is currently defined to be uint64_t in src/base/types.hh; it can underflow since it is unsigned.)
Definition at line 282 of file mem_state.cc.
References _ownerProcess, _pageBytes, _vmaList, EmulationPageTable::isUnmapped(), length, Process::pTable, EmulationPageTable::remap(), Process::system, and System::threads.
void MemState::resetOwner | ( | Process * | owner | ) |
Change the Process owner in case this MemState is copied.
Definition at line 74 of file mem_state.cc.
References _ownerProcess.
|
inlineoverridevirtual |
Serialize an object.
Output an object's state into the current checkpoint section.
cp | Checkpoint state |
Implements Serializable.
Definition at line 184 of file mem_state.hh.
References _brkPoint, _maxStackSize, _mmapEnd, _nextThreadStackBase, _stackBase, _stackMin, _stackSize, _vmaList, X86ISA::count, csprintf(), and paramOut().
|
inline |
Definition at line 87 of file mem_state.hh.
References _brkPoint.
|
inline |
Definition at line 90 of file mem_state.hh.
References _maxStackSize.
|
inline |
Definition at line 93 of file mem_state.hh.
References _mmapEnd.
|
inline |
Definition at line 92 of file mem_state.hh.
References _nextThreadStackBase.
|
inline |
Definition at line 88 of file mem_state.hh.
References _stackBase.
|
inline |
Definition at line 91 of file mem_state.hh.
References _stackMin.
|
inline |
Definition at line 89 of file mem_state.hh.
References _stackSize.
Unmap a pre-existing region.
Depending on the range being unmapped the resulting new regions will either be split, resized, or removed completely.
start_addr | Starting address of region to unmap. |
length | Size of region to unmap. |
Need to split into two smaller regions. Create a clone of the old VMA and slice it to the right.
Slice old VMA to encapsulate the left region.
Region cannot be in any more VMA, because it is completely contained in this one!
Need to nuke the existing VMA.
Trim up the existing VMA.
Overlaps from the right.
Overlaps from the left.
TLBs need to be flushed to remove any stale mappings from regions which were unmapped. Currently the entire TLB is flushed. This results in functionally correct execution, but real systems do not flush all entries when a single mapping changes since it degrades performance. There is currently no general method across all TLB implementations that can flush just part of the address space.
The regions need to always be page-aligned otherwise the while condition will loop indefinitely. (The Addr type is currently defined to be uint64_t in src/base/types.hh; it can underflow since it is unsigned.)
Definition at line 184 of file mem_state.cc.
References _ownerProcess, _pageBytes, _vmaList, DPRINTF, EmulationPageTable::isUnmapped(), length, Process::pTable, Process::system, System::threads, and EmulationPageTable::unmap().
|
inlineoverridevirtual |
Unserialize an object.
Read an object's state from the current checkpoint section.
cp | Checkpoint state |
Implements Serializable.
Definition at line 206 of file mem_state.hh.
References _brkPoint, _maxStackSize, _mmapEnd, _nextThreadStackBase, _pageBytes, _stackBase, _stackMin, _stackSize, _vmaList, X86ISA::count, csprintf(), ArmISA::i, name(), and paramIn().
Change the end of a process' program break.
This represents the end of the heap segment of a process.
old_brk | Old program break address |
new_brk | New program break address |
To make this simple, avoid reducing the heap memory area if the new_brk point is less than the old_brk; this occurs when the heap is receding because the application has given back memory. The brk point is still tracked in the MemState class as an independent field so that it can be returned to the application; we just do not update the region unless we expand it out.
The regions must be page aligned but the break point can be set on byte boundaries. Ensure that the restriction is maintained here by extending the request out to the end of the page. (The roundUp function will not round up an already aligned page.)
Create a new mapping for the heap region. We only create a mapping for the extra memory that is requested so we do not create a situation where there can be overlapping mappings in the regions.
Since we do not track the type of the region and we also do not coalesce the regions together, we can create a fragmented set of heap regions. To resolve this, we keep the furthest point ever mapped by the _endBrkPoint field.
Check if existing mappings impede the expansion of brk expansion. If brk cannot expand, it must return the original, unmodified brk address and should not modify the mappings here.
Note that the heap regions are always contiguous but there is no mechanism right now to coalesce together memory that belongs to the same region with similar access permissions. This could be implemented if it actually becomes necessary; probably only necessary if the list becomes too long to walk.
Definition at line 105 of file mem_state.cc.
References _brkPoint, _endBrkPoint, _pageBytes, isUnmapped(), length, mapRegion(), and roundUp().
|
private |
Definition at line 243 of file mem_state.hh.
Referenced by getBrkPoint(), operator=(), serialize(), setBrkPoint(), unserialize(), and updateBrkRegion().
|
private |
Keeps record of the furthest mapped heap location.
Definition at line 254 of file mem_state.hh.
Referenced by operator=(), and updateBrkRegion().
|
private |
Definition at line 246 of file mem_state.hh.
Referenced by fixupFault(), getMaxStackSize(), operator=(), serialize(), setMaxStackSize(), and unserialize().
|
private |
Definition at line 249 of file mem_state.hh.
Referenced by extendMmap(), getMmapEnd(), operator=(), serialize(), setMmapEnd(), and unserialize().
|
private |
Definition at line 248 of file mem_state.hh.
Referenced by getNextThreadStackBase(), operator=(), serialize(), setNextThreadStackBase(), and unserialize().
|
private |
Owner process of MemState.
Used to manipulate page tables.
Definition at line 240 of file mem_state.hh.
Referenced by extendMmap(), fixupFault(), isUnmapped(), remapRegion(), resetOwner(), and unmapRegion().
|
private |
Definition at line 242 of file mem_state.hh.
Referenced by extendMmap(), fixupFault(), isUnmapped(), mapRegion(), operator=(), remapRegion(), unmapRegion(), unserialize(), and updateBrkRegion().
|
private |
Definition at line 244 of file mem_state.hh.
Referenced by fixupFault(), getStackBase(), operator=(), serialize(), setStackBase(), and unserialize().
|
private |
Definition at line 247 of file mem_state.hh.
Referenced by fixupFault(), getStackMin(), operator=(), serialize(), setStackMin(), and unserialize().
|
private |
Definition at line 245 of file mem_state.hh.
Referenced by getStackSize(), operator=(), serialize(), setStackSize(), and unserialize().
The _vmaList member is a list of virtual memory areas in the target application space that have been allocated by the target.
In most operating systems, lazy allocation is used and these structures (or equivalent ones) are used to track the valid address ranges.
This could use a more efficient data structure like an interval tree, but it is unclear whether the vmas will be modified often enough for the improvement in lookup time to matter. Unmapping VMAs currently modifies the list while iterating so the STL container must either support this or the unmapping method must be changed.
Definition at line 268 of file mem_state.hh.
Referenced by fixupFault(), isUnmapped(), mapRegion(), operator=(), printVmaList(), remapRegion(), serialize(), unmapRegion(), and unserialize().