gem5 v24.0.0.0
Loading...
Searching...
No Matches
gem5::MemState Class Reference

This class holds the memory state for the Process class and all of its derived, architecture-specific children. More...

#include <mem_state.hh>

Inheritance diagram for gem5::MemState:
gem5::Serializable

Public Member Functions

 MemState (Process *owner, Addr brk_point, Addr stack_base, Addr max_stack_size, Addr next_thread_stack_base, Addr mmap_end)
 
MemStateoperator= (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)
 
Systemsystem () 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.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ MemState()

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.

Member Function Documentation

◆ allocateMem()

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.

Parameters
vaddrThe virtual address in need of a frame allocation.
sizeThe size in bytes of the requested mapping.
clobberThis flag specifies whether mappings in the page tables can be overwritten and replaced with the new mapping.

◆ extendMmap()

Addr gem5::MemState::extendMmap ( Addr length)

◆ fixupFault()

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.

Parameters
vaddrThe virtual address which is causing the fault.
Returns
Whether the fault has been fixed.

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.

◆ getBrkPoint()

Addr gem5::MemState::getBrkPoint ( ) const
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.

◆ getMaxStackSize()

Addr gem5::MemState::getMaxStackSize ( ) const
inline

Definition at line 88 of file mem_state.hh.

References _maxStackSize.

◆ getMmapEnd()

Addr gem5::MemState::getMmapEnd ( ) const
inline

Definition at line 91 of file mem_state.hh.

References _mmapEnd.

◆ getNextThreadStackBase()

Addr gem5::MemState::getNextThreadStackBase ( ) const
inline

Definition at line 90 of file mem_state.hh.

References _nextThreadStackBase.

◆ getStackBase()

Addr gem5::MemState::getStackBase ( ) const
inline

Definition at line 86 of file mem_state.hh.

References _stackBase.

◆ getStackMin()

Addr gem5::MemState::getStackMin ( ) const
inline

Definition at line 89 of file mem_state.hh.

References _stackMin.

◆ getStackSize()

Addr gem5::MemState::getStackSize ( ) const
inline

Definition at line 87 of file mem_state.hh.

References _stackSize.

◆ isUnmapped()

bool gem5::MemState::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.

Parameters
start_addrStarting address of region to check.
lengthLength of the range to check.
Returns
true if all pages in the range are unmapped in page table

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().

◆ mapRegion()

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.

Parameters
start_addrStarting address of the region.
lengthSize of the region.
nameName of region. Optional.
sim_fdFile descriptor for file-backed regions or -1.
offsetOffset 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().

◆ operator=()

MemState & gem5::MemState::operator= ( const MemState & in)

◆ printVmaList()

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.

◆ remapRegion()

void gem5::MemState::remapRegion ( Addr start_addr,
Addr new_start_addr,
Addr length )

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.

Parameters
start_addrStart address of region being remapped.
new_start_addrNew start address of the region.
lengthLength 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.

◆ replicatePage()

void gem5::MemState::replicatePage ( const MemState & in,
Addr vaddr,
Addr new_paddr,
bool alloc_page )
private
Parameters

◆ resetOwner()

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.

◆ serialize()

void gem5::MemState::serialize ( CheckpointOut & cp) const
inlineoverridevirtual

Serialize an object.

Output an object's state into the current checkpoint section.

Parameters
cpCheckpoint 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.

◆ setBrkPoint()

void gem5::MemState::setBrkPoint ( Addr brk_point)
inline

Definition at line 92 of file mem_state.hh.

References _brkPoint.

◆ setMaxStackSize()

void gem5::MemState::setMaxStackSize ( Addr max_stack)
inline

Definition at line 95 of file mem_state.hh.

References _maxStackSize.

◆ setMmapEnd()

void gem5::MemState::setMmapEnd ( Addr mmap_end)
inline

Definition at line 98 of file mem_state.hh.

References _mmapEnd.

◆ setNextThreadStackBase()

void gem5::MemState::setNextThreadStackBase ( Addr ntsb)
inline

Definition at line 97 of file mem_state.hh.

References _nextThreadStackBase.

◆ setStackBase()

void gem5::MemState::setStackBase ( Addr stack_base)
inline

Definition at line 93 of file mem_state.hh.

References _stackBase.

◆ setStackMin()

void gem5::MemState::setStackMin ( Addr stack_min)
inline

Definition at line 96 of file mem_state.hh.

References _stackMin.

◆ setStackSize()

void gem5::MemState::setStackSize ( Addr stack_size)
inline

Definition at line 94 of file mem_state.hh.

References _stackSize.

◆ system()

System * gem5::MemState::system ( ) const
private
Parameters

◆ unmapRegion()

void gem5::MemState::unmapRegion ( Addr start_addr,
Addr length )

Unmap a pre-existing region.

Depending on the range being unmapped the resulting new regions will either be split, resized, or removed completely.

Parameters
start_addrStarting address of region to unmap.
lengthSize 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().

◆ unserialize()

void gem5::MemState::unserialize ( CheckpointIn & cp)
inlineoverridevirtual

Unserialize an object.

Read an object's state from the current checkpoint section.

Parameters
cpCheckpoint 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().

◆ updateBrkRegion()

void gem5::MemState::updateBrkRegion ( Addr old_brk,
Addr new_brk )

Change the end of a process' program break.

This represents the end of the heap segment of a process.

Parameters
old_brkOld program break address
new_brkNew 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().

Member Data Documentation

◆ _brkPoint

Addr gem5::MemState::_brkPoint
private

◆ _maxStackSize

Addr gem5::MemState::_maxStackSize
private

◆ _mmapEnd

Addr gem5::MemState::_mmapEnd
private

Definition at line 278 of file mem_state.hh.

Referenced by extendMmap(), getMmapEnd(), operator=(), serialize(), setMmapEnd(), and unserialize().

◆ _nextThreadStackBase

Addr gem5::MemState::_nextThreadStackBase
private

◆ _ownerProcess

Process* gem5::MemState::_ownerProcess
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().

◆ _pageBytes

Addr gem5::MemState::_pageBytes
private

◆ _stackBase

Addr gem5::MemState::_stackBase
private

Definition at line 273 of file mem_state.hh.

Referenced by fixupFault(), getStackBase(), operator=(), serialize(), setStackBase(), and unserialize().

◆ _stackMin

Addr gem5::MemState::_stackMin
private

Definition at line 276 of file mem_state.hh.

Referenced by fixupFault(), getStackMin(), operator=(), serialize(), setStackMin(), and unserialize().

◆ _stackSize

Addr gem5::MemState::_stackSize
private

Definition at line 274 of file mem_state.hh.

Referenced by getStackSize(), operator=(), serialize(), setStackSize(), and unserialize().

◆ _vmaList

std::list<VMA> gem5::MemState::_vmaList
private

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().


The documentation for this class was generated from the following files:

Generated on Tue Jun 18 2024 16:24:12 for gem5 by doxygen 1.11.0