gem5  v20.1.0.0
Public Member Functions | Private Attributes | List of all members
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 MemState:
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. 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)
 

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 62 of file mem_state.hh.

Constructor & Destructor Documentation

◆ MemState()

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.

Member Function Documentation

◆ allocateMem()

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.

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 MemState::extendMmap ( Addr  length)

◆ fixupFault()

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.

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

◆ getBrkPoint()

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

◆ getMaxStackSize()

Addr MemState::getMaxStackSize ( ) const
inline

Definition at line 83 of file mem_state.hh.

References _maxStackSize.

◆ getMmapEnd()

Addr MemState::getMmapEnd ( ) const
inline

Definition at line 86 of file mem_state.hh.

References _mmapEnd.

◆ getNextThreadStackBase()

Addr MemState::getNextThreadStackBase ( ) const
inline

Definition at line 85 of file mem_state.hh.

References _nextThreadStackBase.

◆ getStackBase()

Addr MemState::getStackBase ( ) const
inline

Definition at line 81 of file mem_state.hh.

References _stackBase.

◆ getStackMin()

Addr MemState::getStackMin ( ) const
inline

Definition at line 84 of file mem_state.hh.

References _stackMin.

◆ getStackSize()

Addr MemState::getStackSize ( ) const
inline

Definition at line 82 of file mem_state.hh.

References _stackSize.

◆ isUnmapped()

bool 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 80 of file mem_state.cc.

References _ownerProcess, _pageBytes, _vmaList, length, EmulationPageTable::lookup(), panic, and Process::pTable.

Referenced by extendMmap(), mapRegion(), and updateBrkRegion().

◆ mapRegion()

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.

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 164 of file mem_state.cc.

References _pageBytes, _vmaList, DPRINTF, isUnmapped(), length, and ArmISA::offset.

Referenced by updateBrkRegion().

◆ operator=()

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

◆ printVmaList()

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.

◆ remapRegion()

void 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 282 of file mem_state.cc.

References _ownerProcess, _pageBytes, _vmaList, EmulationPageTable::isUnmapped(), length, Process::pTable, EmulationPageTable::remap(), Process::system, and System::threads.

◆ resetOwner()

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.

◆ serialize()

void MemState::serialize ( CheckpointOut cp) const
inlineoverridevirtual

Serialize an object.

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

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

◆ setBrkPoint()

void MemState::setBrkPoint ( Addr  brk_point)
inline

Definition at line 87 of file mem_state.hh.

References _brkPoint.

◆ setMaxStackSize()

void MemState::setMaxStackSize ( Addr  max_stack)
inline

Definition at line 90 of file mem_state.hh.

References _maxStackSize.

◆ setMmapEnd()

void MemState::setMmapEnd ( Addr  mmap_end)
inline

Definition at line 93 of file mem_state.hh.

References _mmapEnd.

◆ setNextThreadStackBase()

void MemState::setNextThreadStackBase ( Addr  ntsb)
inline

Definition at line 92 of file mem_state.hh.

References _nextThreadStackBase.

◆ setStackBase()

void MemState::setStackBase ( Addr  stack_base)
inline

Definition at line 88 of file mem_state.hh.

References _stackBase.

◆ setStackMin()

void MemState::setStackMin ( Addr  stack_min)
inline

Definition at line 91 of file mem_state.hh.

References _stackMin.

◆ setStackSize()

void MemState::setStackSize ( Addr  stack_size)
inline

Definition at line 89 of file mem_state.hh.

References _stackSize.

◆ unmapRegion()

void 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 184 of file mem_state.cc.

References _ownerProcess, _pageBytes, _vmaList, DPRINTF, EmulationPageTable::isUnmapped(), length, Process::pTable, Process::system, System::threads, and EmulationPageTable::unmap().

◆ unserialize()

void MemState::unserialize ( CheckpointIn cp)
inlineoverridevirtual

Unserialize an object.

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

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

◆ updateBrkRegion()

void 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

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

Member Data Documentation

◆ _brkPoint

Addr MemState::_brkPoint
private

◆ _endBrkPoint

Addr MemState::_endBrkPoint
private

Keeps record of the furthest mapped heap location.

Definition at line 254 of file mem_state.hh.

Referenced by operator=(), and updateBrkRegion().

◆ _maxStackSize

Addr MemState::_maxStackSize
private

◆ _mmapEnd

Addr MemState::_mmapEnd
private

Definition at line 249 of file mem_state.hh.

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

◆ _nextThreadStackBase

Addr MemState::_nextThreadStackBase
private

◆ _ownerProcess

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

◆ _pageBytes

Addr MemState::_pageBytes
private

◆ _stackBase

Addr MemState::_stackBase
private

Definition at line 244 of file mem_state.hh.

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

◆ _stackMin

Addr MemState::_stackMin
private

Definition at line 247 of file mem_state.hh.

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

◆ _stackSize

Addr MemState::_stackSize
private

Definition at line 245 of file mem_state.hh.

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

◆ _vmaList

std::list<VMA> 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 268 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 Wed Sep 30 2020 14:02:27 for gem5 by doxygen 1.8.17