gem5 v24.0.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. | |
Addr | getBrkPoint () const |
Get/set base addresses and sizes for the stack and data segments of the process' memory. | |
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. | |
void | mapRegion (Addr start_addr, Addr length, const std::string &name="anon", int sim_fd=-1, Addr offset=0) |
Add a new memory region. | |
void | unmapRegion (Addr start_addr, Addr length) |
Unmap a pre-existing region. | |
void | remapRegion (Addr start_addr, Addr new_start_addr, Addr length) |
Remap a pre-existing region. | |
void | updateBrkRegion (Addr old_brk, Addr new_brk) |
Change the end of a process' program break. | |
bool | fixupFault (Addr vaddr) |
Attempt to fix up a fault at vaddr by allocating a page. | |
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. | |
void | serialize (CheckpointOut &cp) const override |
Serialize an object. | |
void | unserialize (CheckpointIn &cp) override |
Unserialize an object. | |
std::string | printVmaList () |
Print the list of VMAs in a format similar to /proc/self/maps. | |
Public Member Functions inherited from gem5::Serializable | |
Serializable () | |
virtual | ~Serializable () |
void | serializeSection (CheckpointOut &cp, const char *name) const |
Serialize an object into a new section. | |
void | serializeSection (CheckpointOut &cp, const std::string &name) const |
void | unserializeSection (CheckpointIn &cp, const char *name) |
Unserialize an a child object. | |
void | unserializeSection (CheckpointIn &cp, const std::string &name) |
Private Member Functions | |
void | replicatePage (const MemState &in, Addr vaddr, Addr new_paddr, bool alloc_page) |
System * | system () const |
Private Attributes | |
Process * | _ownerProcess |
Owner process of MemState. | |
Addr | _pageBytes |
Addr | _brkPoint |
Addr | _stackBase |
Addr | _stackSize |
Addr | _maxStackSize |
Addr | _stackMin |
Addr | _nextThreadStackBase |
Addr | _mmapEnd |
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. | |
Additional Inherited Members | |
Static Public Member Functions inherited from gem5::Serializable | |
static const std::string & | currentSection () |
Gets the fully-qualified name of the active section. | |
static void | generateCheckpointOut (const std::string &cpt_dir, std::ofstream &outstream) |
Generate a checkpoint file so that the serialization can be routed to it. | |
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 67 of file mem_state.hh.
gem5::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 44 of file mem_state.cc.
void gem5::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 457 of file mem_state.cc.
References _mmapEnd, _ownerProcess, _pageBytes, DPRINTF, isUnmapped(), and gem5::Process::mmapGrowsDown().
bool gem5::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 392 of file mem_state.cc.
References _maxStackSize, _ownerProcess, _pageBytes, _stackBase, _stackMin, _vmaList, gem5::Process::allocateMem(), gem5::SETranslatingPortProxy::Always, gem5::Process::contextIds, fatal, inform, gem5::roundDown(), gem5::Process::system, gem5::System::threads, gem5::MipsISA::vaddr, and gem5::RiscvISA::vma.
|
inline |
Get/set base addresses and sizes for the stack and data segments of the process' memory.
Definition at line 85 of file mem_state.hh.
References _brkPoint.
|
inline |
Definition at line 88 of file mem_state.hh.
References _maxStackSize.
|
inline |
Definition at line 91 of file mem_state.hh.
References _mmapEnd.
|
inline |
Definition at line 90 of file mem_state.hh.
References _nextThreadStackBase.
|
inline |
Definition at line 86 of file mem_state.hh.
References _stackBase.
|
inline |
Definition at line 89 of file mem_state.hh.
References _stackMin.
|
inline |
Definition at line 87 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 82 of file mem_state.cc.
References _ownerProcess, _pageBytes, _vmaList, gem5::EmulationPageTable::lookup(), panic, gem5::Process::pTable, and gem5::RiscvISA::vma.
Referenced by extendMmap(), mapRegion(), and updateBrkRegion().
void gem5::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 172 of file mem_state.cc.
References _pageBytes, _vmaList, DPRINTF, isUnmapped(), and gem5::ArmISA::offset.
Referenced by updateBrkRegion().
Definition at line 57 of file mem_state.cc.
References _brkPoint, _maxStackSize, _mmapEnd, _nextThreadStackBase, _pageBytes, _stackBase, _stackMin, _stackSize, and _vmaList.
std::string gem5::MemState::printVmaList | ( | ) |
Print the list of VMAs in a format similar to /proc/self/maps.
Definition at line 485 of file mem_state.cc.
References _vmaList, and gem5::RiscvISA::vma.
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 289 of file mem_state.cc.
References _ownerProcess, _pageBytes, _vmaList, gem5::EmulationPageTable::isUnmapped(), gem5::Process::pTable, gem5::EmulationPageTable::remap(), gem5::Process::system, gem5::System::threads, and gem5::RiscvISA::vma.
|
private |
void gem5::MemState::resetOwner | ( | Process * | owner | ) |
Change the Process owner in case this MemState is copied.
Definition at line 76 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 gem5::Serializable.
Definition at line 189 of file mem_state.hh.
References _brkPoint, _maxStackSize, _mmapEnd, _nextThreadStackBase, _stackBase, _stackMin, _stackSize, _vmaList, gem5::X86ISA::count, gem5::csprintf(), gem5::paramOut(), and gem5::RiscvISA::vma.
|
inline |
Definition at line 92 of file mem_state.hh.
References _brkPoint.
|
inline |
Definition at line 95 of file mem_state.hh.
References _maxStackSize.
|
inline |
Definition at line 98 of file mem_state.hh.
References _mmapEnd.
|
inline |
Definition at line 97 of file mem_state.hh.
References _nextThreadStackBase.
|
inline |
Definition at line 93 of file mem_state.hh.
References _stackBase.
|
inline |
Definition at line 96 of file mem_state.hh.
References _stackMin.
|
inline |
Definition at line 94 of file mem_state.hh.
References _stackSize.
|
private |
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 192 of file mem_state.cc.
References _ownerProcess, _pageBytes, _vmaList, DPRINTF, gem5::EmulationPageTable::isUnmapped(), gem5::Process::pTable, gem5::Process::system, gem5::System::threads, gem5::EmulationPageTable::unmap(), and gem5::RiscvISA::vma.
Referenced by updateBrkRegion().
|
inlineoverridevirtual |
Unserialize an object.
Read an object's state from the current checkpoint section.
cp | Checkpoint state |
Implements gem5::Serializable.
Definition at line 214 of file mem_state.hh.
References _brkPoint, _maxStackSize, _mmapEnd, _nextThreadStackBase, _pageBytes, _stackBase, _stackMin, _stackSize, _vmaList, gem5::X86ISA::count, gem5::csprintf(), fatal_if, gem5::ArmISA::i, name(), gem5::ArmISA::offset, gem5::optParamIn(), and gem5::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 |
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.)
Reduce 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. This may involve unmapping heap pages, if new_brk rounds to a lower-address page. The previous behavior was to leave such pages mapped for simplicity; however, that was not what Linux does in practice and may violate the assumptions of applications like glibc malloc, whose default configuration for Linux requires all pages allocated via brk(2) to be zero-filled (specifically, by setting MORECORE_CLEARS to 2).
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.
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 107 of file mem_state.cc.
References _brkPoint, _pageBytes, isUnmapped(), mapRegion(), gem5::roundUp(), and unmapRegion().
|
private |
Definition at line 272 of file mem_state.hh.
Referenced by getBrkPoint(), operator=(), serialize(), setBrkPoint(), unserialize(), and updateBrkRegion().
|
private |
Definition at line 275 of file mem_state.hh.
Referenced by fixupFault(), getMaxStackSize(), operator=(), serialize(), setMaxStackSize(), and unserialize().
|
private |
Definition at line 278 of file mem_state.hh.
Referenced by extendMmap(), getMmapEnd(), operator=(), serialize(), setMmapEnd(), and unserialize().
|
private |
Definition at line 277 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 269 of file mem_state.hh.
Referenced by extendMmap(), fixupFault(), isUnmapped(), remapRegion(), resetOwner(), and unmapRegion().
|
private |
Definition at line 271 of file mem_state.hh.
Referenced by extendMmap(), fixupFault(), isUnmapped(), mapRegion(), operator=(), remapRegion(), unmapRegion(), unserialize(), and updateBrkRegion().
|
private |
Definition at line 273 of file mem_state.hh.
Referenced by fixupFault(), getStackBase(), operator=(), serialize(), setStackBase(), and unserialize().
|
private |
Definition at line 276 of file mem_state.hh.
Referenced by fixupFault(), getStackMin(), operator=(), serialize(), setStackMin(), and unserialize().
|
private |
Definition at line 274 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 292 of file mem_state.hh.
Referenced by fixupFault(), isUnmapped(), mapRegion(), operator=(), printVmaList(), remapRegion(), serialize(), unmapRegion(), and unserialize().