The Pedigree Project 0.1
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Private Attributes | List of all members
PosixSubsystem Class Reference

#include <PosixSubsystem.h>

+ Inheritance diagram for PosixSubsystem:
+ Collaboration diagram for PosixSubsystem:

Classes

struct  ExecutableImage
 
class  PosixSyncObject
 
class  PosixThread
 
struct  PosixThreadKey
 
struct  SignalDisposition
 
struct  SignalHandler
 
struct  UserImageToken
 

Public Types

enum class  DescriptorDuplicationResult { Success , BadSource , TargetBusy }
 
enum  Abi { PosixAbi = 0 , LinuxAbi = 1 }
 
enum  UserStringResult { UserStringSuccess , UserStringBadAddress , UserStringTooLong }
 
enum class  SignalDeliveryResult {
  Unavailable , Ignored , Rejected , Queued ,
  Full
}
 
- Public Types inherited from Subsystem
enum class  UserReturnResult { Continue , Terminal }
 
enum class  UserReturnEventResult { Deliver , Consumed , Terminal }
 
enum  SubsystemType { Posix = 0 , Native = 1 , None = 255 }
 
enum  KillReason { Interrupted = 0 , Terminated = 1 , Unknown = 255 }
 
enum class  ExitCause { Normal , Signal }
 
enum  ExceptionType {
  InvalidOpcode = 0 , PageFault = 1 , GeneralProtectionFault = 2 , DivideByZero = 3 ,
  FpuError = 4 , SpecialFpuError = 5 , TerminalInput = 6 , TerminalOutput = 7 ,
  Continue = 8 , Stop = 9 , Interrupt = 10 , Quit = 11 ,
  Child = 12 , Pipe = 13 , FileMappingFault = 14 , Other = 255
}
 

Public Member Functions

 PosixSubsystem ()
 
 PosixSubsystem (PosixSubsystem &s, bool clearSignalHandlers=false)
 
 PosixSubsystem (SubsystemType type)
 
SharedPointer< PosixNamespaceContextnamespaceContext () const
 
bool executablePath (String &result) const
 
bool executablePath (FilesystemPathRef &result) const
 
bool commandLine (Vector< String > &result) const
 
PosixTraceContexttraceContext ()
 
bool tracePolicyAllowed () const
 
UserReturnResult userReturnCheckpoint (Thread &, UserReturnFrame &) override
 
UserReturnEventResult userReturnEvent (Thread &, Event &, UserReturnFrame &) override
 
bool traceException (Thread &, int &signal, InterruptState &, ExceptionType, uintptr_t, uintptr_t)
 
TraceStatus prepareTraceSignal (Thread &, int signal, int32_t pid, uint32_t uid, bool inheritReservation, UniquePointer< SignalEvent > &)
 
bool publishTraceSignal (Thread &, UniquePointer< SignalEvent > &, SignalEvent *source)
 
virtual ~PosixSubsystem ()
 
virtual void acquire ()
 Acquire full mutual exclusion for all Subsystem resources.
 
virtual void release ()
 
virtual bool kill (KillReason killReason, Thread *pThread)
 
virtual void threadException (Thread *pThread, ExceptionType eType, InterruptState *pState=nullptr, uintptr_t faultAddress=0, uintptr_t errorCode=0)
 
bool resolveUserPageFault (Thread &thread, InterruptState &state, uintptr_t faultAddress, uintptr_t errorCode) override
 
virtual void sendSignal (Thread *pThread, int signal, bool yield=true, bool processDirected=false)
 
void setSignalHandler (size_t sig, SignalHandler *handler)
 
bool admitLegacyUserSignals ()
 
bool affinityPolicyAllowed () const
 
void recordAffinityPolicyUse ()
 
void resetSignalHandlersForExec (Thread *thread, SignalHandler *const handlers[SignalDispositionCount])
 
bool getSignalDisposition (size_t sig, SignalDisposition &disposition, bool beginDelivery=false)
 
SignalDeliveryResult queueSignalDelivery (Thread *target, size_t sig, uint32_t *flags=nullptr, int32_t signalCode=0, bool processDirected=false, uint64_t signalValue=0, const SharedPointer< SignalEventState > &state=SharedPointer< SignalEventState >())
 
void setProcess (Process *process) override
 
bool snapshotUserImage (UserImageToken &token) const
 
bool matchesUserImage (const UserImageToken &token) const
 
void invalidateUserImage ()
 
bool publishUserImage (VirtualAddressSpace &space)
 
SharedPointer< PendingSignalContextpendingSignalContext ()
 
MutexpendingSignalLock ()
 
ConditionVariablependingSignalChanged ()
 
void retireDescriptor (FileDescriptor *descriptor)
 
AdvisoryOwneradvisoryOwner ()
 
PosixMemoryLockAccountmemoryLockAccount ()
 
SignalHandlergetSignalHandler (size_t sig)
 
void exit (int code, ExitCause cause=ExitCause::Normal) NORETURN
 
bool copyDescriptors (PosixSubsystem *pSubsystem)
 
size_t getFd (size_t minimum=0)
 
void allocateFd (size_t fdNum)
 
void freeFd (size_t fdNum)
 
void freeMultipleFds (bool bOnlyCloExec=false, size_t iFirst=0, size_t iLast=-1)
 
bool acquireFileDescriptor (size_t fd, DescriptorLease &descriptor)
 
bool acquireNextFileDescriptor (size_t minimum, size_t &fd, DescriptorLease &descriptor)
 
bool descriptorMatchesOpenDescription (size_t fd, const FileDescriptor::OpenFileDescriptionLease &expected)
 
bool closeFileDescriptor (size_t fd, const DescriptorLease &descriptor)
 
void addFileDescriptor (size_t fd, FileDescriptor *pFd)
 
DescriptorDuplicationResult duplicateFileDescriptor (size_t source, size_t target, bool closeOnExec)
 
size_t installFileDescriptor (FileDescriptor *descriptor, DescriptorLease &lease, size_t minimum=0)
 
PosixSyncObjectgetSyncObject (size_t n)
 
void insertSyncObject (size_t n, PosixSyncObject *sem)
 
void removeSyncObject (size_t n)
 
PosixThreadgetThread (size_t n)
 
void insertThread (size_t n, PosixThread *thread)
 
void removeThread (size_t n)
 
SemaphoregetThreadWaiter (void *n)
 
void * insertThreadWaiter (Semaphore *waiter)
 
void removeThreadWaiter (void *n)
 
bool checkAccess (const DescriptorLease &pFileDescriptor, bool bRead, bool bWrite, bool bExecute) const
 
virtual bool invoke (const char *name, Vector< String > &argv, Vector< String > &env)
 
virtual bool invoke (const char *name, Vector< String > &argv, Vector< String > &env, SyscallState &state)
 
virtual bool invoke (File *originalFile, const String &originalName, Vector< String > &argv, Vector< String > &env)
 
virtual bool invoke (File *originalFile, const String &originalName, Vector< String > &argv, Vector< String > &env, SyscallState &state)
 
bool invoke (File *originalFile, const String &originalName, Vector< String > &argv, Vector< String > &env, SyscallState &state, bool descriptorPathInaccessible)
 
bool invoke (const FilesystemPathRef &originalPath, const String &originalName, Vector< String > &argv, Vector< String > &env, SyscallState &state, bool descriptorPathInaccessible=false)
 
FilefindFileRetained (const String &path, ResolvedPath &result, const FilesystemPathRef &workingDir=FilesystemPathRef(), bool followFinal=false)
 
FilefollowFile (ResolvedPath &selected)
 
Abi getAbi () const
 
void setAbi (Abi which)
 
- Public Member Functions inherited from Subsystem
 Subsystem ()
 
 Subsystem (const Subsystem &s)
 
 Subsystem (SubsystemType type)
 
virtual ~Subsystem ()
 
SubsystemType getType ()
 

Static Public Member Functions

static bool checkAddress (uintptr_t addr, size_t extent, size_t flags)
 
static bool checkedUserBufferSize (size_t count, size_t elementSize, size_t &extent)
 
static bool checkUserAddressRange (uintptr_t addr, size_t count, size_t elementSize, size_t *extent=nullptr)
 
static bool checkUserBuffer (uintptr_t addr, size_t count, size_t elementSize, size_t flags, size_t *extent=nullptr)
 
static bool copyFromUser (void *destination, const void *source, size_t count, size_t elementSize=1)
 
static bool copyToUser (void *destination, const void *source, size_t count, size_t elementSize=1)
 
static UserStringResult copyUserString (const char *userString, String &copy, size_t maxLength)
 

Static Public Attributes

static const size_t SafeRegion = 0x0
 
static const size_t SafeRead = 0x1
 
static const size_t SafeWrite = 0x2
 
static const size_t SafeExecute = 0x4
 
static constexpr size_t LinuxPrivateSignalFirst = 32
 
static constexpr size_t MaximumSupportedSignal = 64
 
static constexpr size_t SignalDispositionCount = MaximumSupportedSignal + 1
 
static constexpr size_t MaximumExecArgumentBytes = 131072
 

Private Types

enum class  CallbackSchedulingDomain { Unrestricted , LegacySignals , Affinity }
 

Private Member Functions

void acquireFdLock ()
 
virtual void prepareThreadsForExec (Thread *owner)
 
virtual void preserveProcessSignalsForThreadExit (Thread *thread)
 
virtual void threadExiting (Thread *pThread)
 
void clearChildTid (Thread *thread)
 
virtual void threadRemoved (Thread *pThread)
 
bool prepareExecutable (File *pFile, ExecutableImage &image, bool isInterpreter)
 
bool loadElf (const ExecutableImage &image, uintptr_t &loadBias)
 
bool invoke (const char *name, Vector< String > &argv, Vector< String > &env, SyscallState *state)
 
bool invoke (File *originalFile, const String &originalName, Vector< String > &argv, Vector< String > &env, SyscallState *state, bool descriptorPathInaccessible=false, const FilesystemPathRef &originalPath=FilesystemPathRef())
 
bool parseShebang (File *pFile, String &interpreter, String &optionalArgument, bool &hasOptionalArgument)
 

Private Attributes

PosixTraceContext m_TraceContext
 
Tree< size_t, SignalHandler * > m_SignalHandlers
 
UnlikelyLock m_SignalHandlersLock
 
CallbackSchedulingDomain m_CallbackSchedulingDomain = CallbackSchedulingDomain::Unrestricted
 
SharedPointer< PendingSignalContextm_PendingSignals {new PendingSignalContext}
 
AdvisoryOwner m_AdvisoryOwner
 
PosixMemoryLockAccount m_MemoryLockAccount
 
SharedPointer< PosixNamespaceContextm_Namespaces
 
VirtualAddressSpacem_UserImageSpace = nullptr
 
uint64_t m_UserImageGeneration = 0
 
bool m_UserImageActive = false
 
Tree< size_t, SharedPointer< FileDescriptor > > m_FdMap
 
size_t m_NextFd
 
Mutex m_FdLock
 
ExtensibleBitmap m_FdBitmap
 
size_t m_LastFd
 
int m_FreeCount
 
Tree< size_t, PosixSyncObject * > m_SyncObjects
 
Tree< size_t, PosixThread * > m_Threads
 
Tree< void *, Semaphore * > m_ThreadWaiters
 
size_t m_NextThreadWaiter
 
Mutex m_ImageMetadataLock
 
FilesystemPathRef m_ExecutablePath
 
Vector< Stringm_CommandLine
 
Abi m_Abi
 
bool m_bAcquired
 
Threadm_pAcquiredThread
 
Spinlock m_Lock
 

Additional Inherited Members

- Protected Attributes inherited from Subsystem
SubsystemType m_Type
 
Processm_pProcess
 

Detailed Description

Defines the compatibility layer for the POSIX Subsystem

Definition at line 181 of file PosixSubsystem.h.

Member Enumeration Documentation

◆ Abi

ABI mode.

Definition at line 200 of file PosixSubsystem.h.

◆ CallbackSchedulingDomain

enum class PosixSubsystem::CallbackSchedulingDomain
strongprivate

Definition at line 789 of file PosixSubsystem.h.

◆ DescriptorDuplicationResult

enum class PosixSubsystem::DescriptorDuplicationResult
strong

Definition at line 183 of file PosixSubsystem.h.

◆ SignalDeliveryResult

enum class PosixSubsystem::SignalDeliveryResult
strong

Definition at line 421 of file PosixSubsystem.h.

◆ UserStringResult

enum PosixSubsystem::UserStringResult

Definition at line 335 of file PosixSubsystem.h.

Constructor & Destructor Documentation

◆ PosixSubsystem() [1/3]

PosixSubsystem::PosixSubsystem ( )
inline

Default constructor

Definition at line 206 of file PosixSubsystem.h.

◆ PosixSubsystem() [2/3]

PosixSubsystem::PosixSubsystem ( PosixSubsystem s,
bool  clearSignalHandlers = false 
)

◆ PosixSubsystem() [3/3]

PosixSubsystem::PosixSubsystem ( SubsystemType  type)
inline

Parameterised constructor

Definition at line 232 of file PosixSubsystem.h.

◆ ~PosixSubsystem()

PosixSubsystem::~PosixSubsystem ( )
virtual

Member Function Documentation

◆ acquire()

void PosixSubsystem::acquire ( )
virtual

Acquire full mutual exclusion for all Subsystem resources.

It is sometimes necessary to perform an operation that would require the entire Subsystem to be owned by a specific thread. For example, Subsystem termination often requires all other threads to exit the Subsystem's critical sections before it can complete.

This call allows that thread to acquire that mutual exclusion.

Reimplemented from Subsystem.

Definition at line 513 of file PosixSubsystem.cc.

References UnlikelyLock::acquire(), Spinlock::acquire(), ProcessorBase::information(), m_bAcquired, m_Lock, m_pAcquiredThread, m_SignalHandlersLock, and Spinlock::release().

Referenced by ~PosixSubsystem().

+ Here is the caller graph for this function:

◆ acquireFdLock()

void PosixSubsystem::acquireFdLock ( )
private

Definition at line 507 of file PosixSubsystem.cc.

◆ acquireFileDescriptor()

bool PosixSubsystem::acquireFileDescriptor ( size_t  fd,
DescriptorLease descriptor 
)

Pins the descriptor currently published at fd.

Callers cannot obtain a borrowed table pointer: a successful lookup retains the descriptor until the returned handle leaves scope, even if another thread closes or replaces the descriptor in the meantime.

Definition at line 1687 of file PosixSubsystem.cc.

References Tree< K, E >::lookup(), m_FdLock, m_FdMap, and Semaphore::release().

◆ acquireNextFileDescriptor()

bool PosixSubsystem::acquireNextFileDescriptor ( size_t  minimum,
size_t &  fd,
DescriptorLease descriptor 
)

Retain the lowest published descriptor at or after minimum.

Definition at line 1699 of file PosixSubsystem.cc.

References Tree< K, E >::begin(), Tree< K, E >::end(), m_FdLock, m_FdMap, and Semaphore::release().

◆ addFileDescriptor()

void PosixSubsystem::addFileDescriptor ( size_t  fd,
FileDescriptor pFd 
)

◆ admitLegacyUserSignals()

bool PosixSubsystem::admitLegacyUserSignals ( )

Definition at line 1149 of file PosixSubsystem.cc.

◆ advisoryOwner()

AdvisoryOwner & PosixSubsystem::advisoryOwner ( )
inline

Definition at line 477 of file PosixSubsystem.h.

◆ affinityPolicyAllowed()

bool PosixSubsystem::affinityPolicyAllowed ( ) const
inline

Definition at line 429 of file PosixSubsystem.h.

◆ allocateFd()

void PosixSubsystem::allocateFd ( size_t  fdNum)

Sets the given file descriptor as "in use".

Definition at line 1531 of file PosixSubsystem.cc.

References m_FdBitmap, m_FdLock, m_NextFd, Semaphore::release(), and ExtensibleBitmap::set().

◆ checkAccess()

bool PosixSubsystem::checkAccess ( const DescriptorLease pFileDescriptor,
bool  bRead,
bool  bWrite,
bool  bExecute 
) const

Definition at line 1991 of file PosixSubsystem.cc.

◆ checkAddress()

bool PosixSubsystem::checkAddress ( uintptr_t  addr,
size_t  extent,
size_t  flags 
)
static

Check whether a given region of memory is safe for the given operations.

This is important to do as we can get pointers from anywhere in the POSIX subsystem, and making sure they are sane and safe is crucial.

Todo:
This has a security flaw in that between the check and the use of the actual pointer, the pointer can become invalid due to other threads being active in the process. It may be worth having a Process-wide UnlikelyLock which has the mmap family of functions, sbrk, etc... as writers, and all other syscalls as readers. This would ensure a multithreaded application is not able to crash the kernel.

Definition at line 547 of file PosixSubsystem.cc.

References VirtualAddressSpace::CopyOnWrite, Dec, VirtualAddressSpace::Execute, VirtualAddressSpace::getMapping(), PhysicalMemoryManager::getPageSize(), Hex, ProcessorBase::information(), MemoryMapManager::instance(), VirtualAddressSpace::isMapped(), VirtualAddressSpace::KernelMode, VirtualAddressSpace::NoAccess, VirtualAddressSpace::Write, and VirtualAddressSpace::WriteProtected.

Referenced by checkUserBuffer().

+ Here is the caller graph for this function:

◆ checkedUserBufferSize()

bool PosixSubsystem::checkedUserBufferSize ( size_t  count,
size_t  elementSize,
size_t &  extent 
)
static

Calculate the byte extent of a userspace array without wrapping.

Zero-sized arrays have a zero byte extent. This makes them safe to pass through the copy helpers without special-casing a null userspace pointer.

Definition at line 659 of file PosixSubsystem.cc.

Referenced by checkUserAddressRange(), checkUserBuffer(), copyFromUser(), and copyToUser().

+ Here is the caller graph for this function:

◆ checkUserAddressRange()

bool PosixSubsystem::checkUserAddressRange ( uintptr_t  addr,
size_t  count,
size_t  elementSize,
size_t *  extent = nullptr 
)
static

Validate only the arithmetic and architectural userspace bounds of an array. Mapping residency and permissions are deliberately left to the eventual user copy.

Definition at line 673 of file PosixSubsystem.cc.

References checkedUserBufferSize().

◆ checkUserBuffer()

bool PosixSubsystem::checkUserBuffer ( uintptr_t  addr,
size_t  count,
size_t  elementSize,
size_t  flags,
size_t *  extent = nullptr 
)
static

Validate a userspace array after checking its size calculation.

Definition at line 694 of file PosixSubsystem.cc.

References checkAddress(), and checkedUserBufferSize().

◆ clearChildTid()

void PosixSubsystem::clearChildTid ( Thread thread)
private

Definition at line 1950 of file PosixSubsystem.cc.

◆ closeFileDescriptor()

bool PosixSubsystem::closeFileDescriptor ( size_t  fd,
const DescriptorLease descriptor 
)

Unpublishes fd only if it still names the generation in descriptor.

This is the close counterpart to acquireFileDescriptor: a concurrent close and reuse of the numeric fd must not allow an older close path to remove the replacement descriptor.

Definition at line 1737 of file PosixSubsystem.cc.

References ExtensibleBitmap::clear(), SharedPointer< T >::get(), Tree< K, E >::lookup(), m_FdBitmap, m_FdLock, m_FdMap, m_LastFd, Semaphore::release(), SharedPointer< T >::reset(), and Tree< K, E >::take().

◆ commandLine()

bool PosixSubsystem::commandLine ( Vector< String > &  result) const

Definition at line 334 of file PosixSubsystem.cc.

◆ copyDescriptors()

bool PosixSubsystem::copyDescriptors ( PosixSubsystem pSubsystem)

◆ copyFromUser()

bool PosixSubsystem::copyFromUser ( void *  destination,
const void *  source,
size_t  count,
size_t  elementSize = 1 
)
static

Snapshot a userspace array into kernel-owned memory.

Mapping teardown is excluded for the duration of validation and copying, so callers can safely release all userspace references before blocking.

Definition at line 711 of file PosixSubsystem.cc.

References checkedUserBufferSize(), and MemoryMapManager::instance().

Referenced by FramebufferFile::command(), and RandomFile::command().

+ Here is the caller graph for this function:

◆ copyToUser()

bool PosixSubsystem::copyToUser ( void *  destination,
const void *  source,
size_t  count,
size_t  elementSize = 1 
)
static

Copy a kernel-owned array to userspace under the mapping lifecycle gate.

Definition at line 733 of file PosixSubsystem.cc.

References checkedUserBufferSize(), and MemoryMapManager::instance().

Referenced by FramebufferFile::command().

+ Here is the caller graph for this function:

◆ copyUserString()

PosixSubsystem::UserStringResult PosixSubsystem::copyUserString ( const char *  userString,
String copy,
size_t  maxLength 
)
static

Copy a NUL-terminated user string without reading past a mapped range.

Definition at line 755 of file PosixSubsystem.cc.

References PhysicalMemoryManager::getPageSize(), and MemoryMapManager::instance().

◆ descriptorMatchesOpenDescription()

bool PosixSubsystem::descriptorMatchesOpenDescription ( size_t  fd,
const FileDescriptor::OpenFileDescriptionLease expected 
)

Compare publication identity without taking an OFD lock or retaining references.

Definition at line 1722 of file PosixSubsystem.cc.

References SharedPointer< T >::get(), Tree< K, E >::lookupRef(), m_FdLock, m_FdMap, and Semaphore::release().

◆ duplicateFileDescriptor()

PosixSubsystem::DescriptorDuplicationResult PosixSubsystem::duplicateFileDescriptor ( size_t  source,
size_t  target,
bool  closeOnExec 
)

Atomically replaces target with a duplicate of source.

The source lookup, anonymous-target owner publication, and table swap share one descriptor-table critical section. A concurrent final close therefore cannot close socket/eventfd admission between lookup and duplication, and target is never observable as an unallocated descriptor.

Definition at line 1801 of file PosixSubsystem.cc.

References SharedPointer< T >::get(), Tree< K, E >::insert(), Tree< K, E >::lookup(), m_FdBitmap, m_FdLock, m_FdMap, m_NextFd, Semaphore::release(), SharedPointer< T >::reset(), ExtensibleBitmap::set(), Tree< K, E >::take(), and ExtensibleBitmap::test().

◆ executablePath() [1/2]

bool PosixSubsystem::executablePath ( FilesystemPathRef result) const

Definition at line 328 of file PosixSubsystem.cc.

◆ executablePath() [2/2]

bool PosixSubsystem::executablePath ( String result) const

Definition at line 317 of file PosixSubsystem.cc.

◆ exit()

void PosixSubsystem::exit ( int  code,
ExitCause  cause = ExitCause::Normal 
)
virtual

◆ findFileRetained()

File * PosixSubsystem::findFileRetained ( const String path,
ResolvedPath result,
const FilesystemPathRef workingDir = FilesystemPathRef(),
bool  followFinal = false 
)

Definition at line 2238 of file PosixSubsystem.cc.

◆ followFile()

File * PosixSubsystem::followFile ( ResolvedPath selected)

Definition at line 2256 of file PosixSubsystem.cc.

◆ freeFd()

void PosixSubsystem::freeFd ( size_t  fdNum)

Sets the given file descriptor as "available" and deletes the FileDescriptor linked to it.

Definition at line 1544 of file PosixSubsystem.cc.

References ExtensibleBitmap::clear(), SharedPointer< T >::get(), m_FdBitmap, m_FdLock, m_FdMap, m_LastFd, Semaphore::release(), SharedPointer< T >::reset(), and Tree< K, E >::take().

◆ freeMultipleFds()

void PosixSubsystem::freeMultipleFds ( bool  bOnlyCloExec = false,
size_t  iFirst = 0,
size_t  iLast = -1 
)

◆ getAbi()

Abi PosixSubsystem::getAbi ( ) const
inline

Retrieves the currently-active ABI for the subsystem.

Definition at line 746 of file PosixSubsystem.h.

Referenced by threadException().

+ Here is the caller graph for this function:

◆ getFd()

size_t PosixSubsystem::getFd ( size_t  minimum = 0)

Returns the first available file descriptor at or above minimum.

Note: POSIX requires open()/accept()/etc to be safe during a signal handler, which requires us to not allow signals during these file descriptor calls. They cannot re-enter as they take process-specific locks.

Definition at line 1500 of file PosixSubsystem.cc.

References m_FdBitmap, m_FdLock, m_LastFd, m_NextFd, Semaphore::release(), ExtensibleBitmap::set(), and ExtensibleBitmap::test().

◆ getSignalDisposition()

bool PosixSubsystem::getSignalDisposition ( size_t  sig,
SignalDisposition disposition,
bool  beginDelivery = false 
)

◆ getSignalHandler()

SignalHandler * PosixSubsystem::getSignalHandler ( size_t  sig)
inline

Gets a signal handler

Definition at line 486 of file PosixSubsystem.h.

◆ getSyncObject()

PosixSyncObject * PosixSubsystem::getSyncObject ( size_t  n)
inline

Gets a synchronisation object given a descriptor

Definition at line 580 of file PosixSubsystem.h.

◆ getThread()

PosixThread * PosixSubsystem::getThread ( size_t  n)
inline

Gets a thread given a descriptor

Definition at line 677 of file PosixSubsystem.h.

◆ getThreadWaiter()

Semaphore * PosixSubsystem::getThreadWaiter ( void *  n)
inline

Gets a thread waiter object given a descriptor

Definition at line 696 of file PosixSubsystem.h.

◆ insertSyncObject()

void PosixSubsystem::insertSyncObject ( size_t  n,
PosixSyncObject sem 
)
inline

Inserts a synchronisation object given a descriptor

Definition at line 585 of file PosixSubsystem.h.

◆ insertThread()

void PosixSubsystem::insertThread ( size_t  n,
PosixThread thread 
)
inline

Inserts a thread given a descriptor and a Thread

Todo:
It might be safe to delete the pointer... We'll see.

Definition at line 682 of file PosixSubsystem.h.

◆ insertThreadWaiter()

void * PosixSubsystem::insertThreadWaiter ( Semaphore waiter)
inline

Inserts a thread waiter object, returns a descriptor

Definition at line 701 of file PosixSubsystem.h.

◆ installFileDescriptor()

size_t PosixSubsystem::installFileDescriptor ( FileDescriptor descriptor,
DescriptorLease lease,
size_t  minimum = 0 
)

Allocates and publishes a descriptor while returning a pin for that exact table generation. This lets multi-descriptor operations roll back safely if a later userspace copyout fails.

Definition at line 1856 of file PosixSubsystem.cc.

References FileDescriptor::fd, Tree< K, E >::insert(), m_FdBitmap, m_FdLock, m_FdMap, m_LastFd, m_NextFd, Semaphore::release(), ExtensibleBitmap::set(), and ExtensibleBitmap::test().

◆ invalidateUserImage()

void PosixSubsystem::invalidateUserImage ( )

Definition at line 356 of file PosixSubsystem.cc.

◆ invoke() [1/8]

bool PosixSubsystem::invoke ( const char *  name,
Vector< String > &  argv,
Vector< String > &  env 
)
virtual

Invokes the given command (thread mechanism).

Implements Subsystem.

Definition at line 2289 of file PosixSubsystem.cc.

References invoke().

Referenced by invoke(), invoke(), invoke(), and invoke().

+ Here is the caller graph for this function:

◆ invoke() [2/8]

bool PosixSubsystem::invoke ( const char *  name,
Vector< String > &  argv,
Vector< String > &  env,
SyscallState &  state 
)
virtual

Invokes the given command (SyscallState mechanism).

Implements Subsystem.

Definition at line 2293 of file PosixSubsystem.cc.

References invoke().

◆ invoke() [3/8]

bool PosixSubsystem::invoke ( const char *  name,
Vector< String > &  argv,
Vector< String > &  env,
SyscallState *  state 
)
private

Definition at line 2385 of file PosixSubsystem.cc.

◆ invoke() [4/8]

bool PosixSubsystem::invoke ( const FilesystemPathRef originalPath,
const String originalName,
Vector< String > &  argv,
Vector< String > &  env,
SyscallState &  state,
bool  descriptorPathInaccessible = false 
)

Definition at line 2418 of file PosixSubsystem.cc.

◆ invoke() [5/8]

bool PosixSubsystem::invoke ( File originalFile,
const String originalName,
Vector< String > &  argv,
Vector< String > &  env 
)
virtual

Invokes the given file (thread mechanism).

Implements Subsystem.

Definition at line 2402 of file PosixSubsystem.cc.

References invoke().

◆ invoke() [6/8]

bool PosixSubsystem::invoke ( File originalFile,
const String originalName,
Vector< String > &  argv,
Vector< String > &  env,
SyscallState &  state 
)
virtual

Invokes the given file (SyscallState mechanism).

Implements Subsystem.

Definition at line 2407 of file PosixSubsystem.cc.

References invoke().

◆ invoke() [7/8]

bool PosixSubsystem::invoke ( File originalFile,
const String originalName,
Vector< String > &  argv,
Vector< String > &  env,
SyscallState &  state,
bool  descriptorPathInaccessible 
)

Definition at line 2412 of file PosixSubsystem.cc.

◆ invoke() [8/8]

bool PosixSubsystem::invoke ( File originalFile,
const String originalName,
Vector< String > &  argv,
Vector< String > &  env,
SyscallState *  state,
bool  descriptorPathInaccessible = false,
const FilesystemPathRef originalPath = FilesystemPathRef() 
)
private

◆ kill()

bool PosixSubsystem::kill ( KillReason  killReason,
Thread pThread 
)
virtual

◆ loadElf()

bool PosixSubsystem::loadElf ( const ExecutableImage image,
uintptr_t &  loadBias 
)
private

Load a validated ELF's PT_LOAD sections into the address space.

Definition at line 2103 of file PosixSubsystem.cc.

References File::getName(), PhysicalMemoryManager::getPageSize(), Process::getParent(), Hex, ProcessorBase::information(), MemoryMapManager::instance(), and Elf::validateExecutableProgramHeaders().

Referenced by invoke().

+ Here is the caller graph for this function:

◆ matchesUserImage()

bool PosixSubsystem::matchesUserImage ( const UserImageToken token) const

Definition at line 350 of file PosixSubsystem.cc.

◆ memoryLockAccount()

PosixMemoryLockAccount & PosixSubsystem::memoryLockAccount ( )
inline

Definition at line 481 of file PosixSubsystem.h.

◆ namespaceContext()

SharedPointer< PosixNamespaceContext > PosixSubsystem::namespaceContext ( ) const
inline

Definition at line 254 of file PosixSubsystem.h.

◆ parseShebang()

bool PosixSubsystem::parseShebang ( File pFile,
String interpreter,
String optionalArgument,
bool &  hasOptionalArgument 
)
private

Parse a bounded shebang line, if present.

Definition at line 2298 of file PosixSubsystem.cc.

References File::getFullPath(), and File::read().

Referenced by invoke().

+ Here is the caller graph for this function:

◆ pendingSignalChanged()

ConditionVariable & PosixSubsystem::pendingSignalChanged ( )
inline

Definition at line 471 of file PosixSubsystem.h.

◆ pendingSignalContext()

SharedPointer< PendingSignalContext > PosixSubsystem::pendingSignalContext ( )
inline

Definition at line 465 of file PosixSubsystem.h.

◆ pendingSignalLock()

Mutex & PosixSubsystem::pendingSignalLock ( )
inline

Definition at line 468 of file PosixSubsystem.h.

◆ prepareExecutable()

bool PosixSubsystem::prepareExecutable ( File pFile,
ExecutableImage image,
bool  isInterpreter 
)
private

Validate and retain the bounded metadata needed to load an executable.

Definition at line 1996 of file PosixSubsystem.cc.

References File::acquireMappingUse(), VirtualAddressSpace::getUserReservedStart(), VirtualAddressSpace::getUserStart(), ProcessorBase::information(), File::read(), Elf::validateExecutableHeader(), Elf::validateExecutableInterpreter(), and Elf::validateExecutableProgramHeaders().

Referenced by invoke().

+ Here is the caller graph for this function:

◆ prepareThreadsForExec()

void PosixSubsystem::prepareThreadsForExec ( Thread owner)
privatevirtual

Preserves process state after exec admission closes and before peers exit.

Reimplemented from Subsystem.

Definition at line 1889 of file PosixSubsystem.cc.

References Process::acquireThread(), Process::getNumThreads(), m_SignalHandlersLock, and Thread::transferProcessSignalsTo().

◆ prepareTraceSignal()

TraceStatus PosixSubsystem::prepareTraceSignal ( Thread thread,
int  signal,
int32_t  pid,
uint32_t  uid,
bool  inheritReservation,
UniquePointer< SignalEvent > &  result 
)

Definition at line 45 of file trace-signals.cc.

◆ preserveProcessSignalsForThreadExit()

void PosixSubsystem::preserveProcessSignalsForThreadExit ( Thread thread)
privatevirtual

Preserves process-owned pending work before an exiting thread drains it.

Reimplemented from Subsystem.

Definition at line 1903 of file PosixSubsystem.cc.

References Process::acquireProcessSignalThread(), m_SignalHandlersLock, and Thread::transferProcessSignalsTo().

◆ publishTraceSignal()

bool PosixSubsystem::publishTraceSignal ( Thread thread,
UniquePointer< SignalEvent > &  prepared,
SignalEvent source 
)

Definition at line 76 of file trace-signals.cc.

◆ publishUserImage()

bool PosixSubsystem::publishUserImage ( VirtualAddressSpace space)

Definition at line 362 of file PosixSubsystem.cc.

◆ queueSignalDelivery()

PosixSubsystem::SignalDeliveryResult PosixSubsystem::queueSignalDelivery ( Thread target,
size_t  sig,
uint32_t *  flags = nullptr,
int32_t  signalCode = 0,
bool  processDirected = false,
uint64_t  signalValue = 0,
const SharedPointer< SignalEventState > &  state = SharedPointer<SignalEventState>() 
)

◆ recordAffinityPolicyUse()

void PosixSubsystem::recordAffinityPolicyUse ( )
inline

Definition at line 432 of file PosixSubsystem.h.

◆ release()

void PosixSubsystem::release ( )
virtual

Release mutual exclusion acquired via acquire().

Reimplemented from Subsystem.

Definition at line 535 of file PosixSubsystem.cc.

References Spinlock::acquire(), m_bAcquired, m_FdLock, m_Lock, m_pAcquiredThread, m_SignalHandlersLock, Spinlock::release(), UnlikelyLock::release(), and Semaphore::release().

Referenced by ~PosixSubsystem().

+ Here is the caller graph for this function:

◆ removeSyncObject()

void PosixSubsystem::removeSyncObject ( size_t  n)
inline

Removes a semaphore given a descriptor

Definition at line 596 of file PosixSubsystem.h.

◆ removeThread()

void PosixSubsystem::removeThread ( size_t  n)
inline

Removes a thread given a descriptor

Todo:
It might be safe to delete the pointer... We'll see.

Definition at line 691 of file PosixSubsystem.h.

◆ removeThreadWaiter()

void PosixSubsystem::removeThreadWaiter ( void *  n)
inline

Removes a thread waiter object given a descriptor

Definition at line 711 of file PosixSubsystem.h.

◆ resetSignalHandlersForExec()

void PosixSubsystem::resetSignalHandlersForExec ( Thread thread,
SignalHandler *const  handlers[SignalDispositionCount] 
)

Installs a complete exec-time disposition table and rebinds pending deliveries to it. Takes ownership of every entry in handlers.

Definition at line 1216 of file PosixSubsystem.cc.

References UnlikelyLock::acquire(), Process::getNumThreads(), Thread::getParent(), m_SignalHandlersLock, PosixSubsystem::SignalHandler::pEvent, UnlikelyLock::release(), and PosixSubsystem::SignalHandler::sig.

◆ resolveUserPageFault()

bool PosixSubsystem::resolveUserPageFault ( Thread ,
InterruptState &  ,
uintptr_t  ,
uintptr_t   
)
overridevirtual

Retry a userspace page fault after raw interrupt scopes have retired.

Reimplemented from Subsystem.

Definition at line 946 of file PosixSubsystem.cc.

References Process::getAddressSpace(), ProcessorBase::getInterrupts(), Thread::getParent(), ProcessorBase::information(), MemoryMapManager::instance(), and threadException().

◆ retireDescriptor()

void PosixSubsystem::retireDescriptor ( FileDescriptor descriptor)

Definition at line 1916 of file PosixSubsystem.cc.

◆ sendSignal()

void PosixSubsystem::sendSignal ( Thread pThread,
int  signal,
bool  yield = true,
bool  processDirected = false 
)
virtual

Send a POSIX signal to a thread or its process.

Definition at line 1115 of file PosixSubsystem.cc.

References PerProcessorScheduler::checkEventState(), Dec, Process::getId(), Thread::getId(), Thread::getParent(), Thread::getScheduler(), ProcessorBase::information(), Scheduler::instance(), queueSignalDelivery(), and Scheduler::yield().

Referenced by IntervalTimer::signal(), and threadException().

+ Here is the caller graph for this function:

◆ setAbi()

void PosixSubsystem::setAbi ( Abi  which)
inline

Switch the ABI of the subsystem to the specified choice.

Definition at line 751 of file PosixSubsystem.h.

◆ setProcess()

void PosixSubsystem::setProcess ( Process p)
overridevirtual

Sets the process that this subsystem is linked to.

Reimplemented from Subsystem.

Definition at line 302 of file PosixSubsystem.cc.

References Process::getAddressSpace(), MemoryMapManager::instance(), m_TraceContext, and Subsystem::setProcess().

◆ setSignalHandler()

void PosixSubsystem::setSignalHandler ( size_t  sig,
SignalHandler handler 
)

◆ snapshotUserImage()

bool PosixSubsystem::snapshotUserImage ( UserImageToken token) const

Definition at line 340 of file PosixSubsystem.cc.

◆ threadException()

void PosixSubsystem::threadException ( Thread pThread,
ExceptionType  eType,
InterruptState *  pState = nullptr,
uintptr_t  faultAddress = 0,
uintptr_t  errorCode = 0 
)
virtual

A thread has thrown an exception!

Reimplemented from Subsystem.

Definition at line 973 of file PosixSubsystem.cc.

References Dec, Thread::deferSignalExit(), getAbi(), Process::getId(), Thread::getId(), Thread::getParent(), getSignalDisposition(), Thread::getTlsBase(), Hex, ProcessorBase::id(), and sendSignal().

Referenced by resolveUserPageFault().

+ Here is the caller graph for this function:

◆ threadExiting()

void PosixSubsystem::threadExiting ( Thread pThread)
privatevirtual

Notifies the subsystem immediately before a thread announces exit.

Reimplemented from Subsystem.

Definition at line 1931 of file PosixSubsystem.cc.

References Thread::getParent(), Thread::getTaskId(), Process::getUserspaceId(), and m_TraceContext.

◆ threadRemoved()

void PosixSubsystem::threadRemoved ( Thread pThread)
privatevirtual

Notifies the subsystem that the given thread has been removed.

Reimplemented from Subsystem.

Definition at line 1976 of file PosixSubsystem.cc.

References Completion::complete(), and m_Threads.

◆ traceContext()

PosixTraceContext & PosixSubsystem::traceContext ( )
inline

Definition at line 262 of file PosixSubsystem.h.

◆ traceException()

bool PosixSubsystem::traceException ( Thread thread,
int &  signal,
InterruptState &  state,
ExceptionType  exception,
uintptr_t  address,
uintptr_t  errorCode 
)

Definition at line 180 of file trace-signals.cc.

◆ tracePolicyAllowed()

bool PosixSubsystem::tracePolicyAllowed ( ) const
inline

Definition at line 265 of file PosixSubsystem.h.

◆ userReturnCheckpoint()

Subsystem::UserReturnResult PosixSubsystem::userReturnCheckpoint ( Thread thread,
UserReturnFrame frame 
)
overridevirtual

Reimplemented from Subsystem.

Definition at line 156 of file trace-signals.cc.

◆ userReturnEvent()

Subsystem::UserReturnEventResult PosixSubsystem::userReturnEvent ( Thread thread,
Event event,
UserReturnFrame frame 
)
overridevirtual

Reimplemented from Subsystem.

Definition at line 103 of file trace-signals.cc.

Member Data Documentation

◆ LinuxPrivateSignalFirst

constexpr size_t PosixSubsystem::LinuxPrivateSignalFirst = 32
staticconstexpr

Bundled musl reserves these signals for its timer, cancellation, and synchronous-call machinery. Public realtime signals occupy 35..64.

Definition at line 195 of file PosixSubsystem.h.

Referenced by queueSignalDelivery().

◆ m_Abi

Abi PosixSubsystem::m_Abi
private

ABI for the subsystem This affects syscall parameters and the behaviors of some syscalls.

Definition at line 844 of file PosixSubsystem.h.

◆ m_AdvisoryOwner

AdvisoryOwner PosixSubsystem::m_AdvisoryOwner
private

Definition at line 792 of file PosixSubsystem.h.

◆ m_bAcquired

bool PosixSubsystem::m_bAcquired
private

Are we acquired?

Definition at line 849 of file PosixSubsystem.h.

Referenced by acquire(), and release().

◆ m_CallbackSchedulingDomain

CallbackSchedulingDomain PosixSubsystem::m_CallbackSchedulingDomain = CallbackSchedulingDomain::Unrestricted
private

Definition at line 790 of file PosixSubsystem.h.

◆ m_CommandLine

Vector<String> PosixSubsystem::m_CommandLine
private

Definition at line 838 of file PosixSubsystem.h.

◆ m_ExecutablePath

FilesystemPathRef PosixSubsystem::m_ExecutablePath
private

Definition at line 837 of file PosixSubsystem.h.

◆ m_FdBitmap

ExtensibleBitmap PosixSubsystem::m_FdBitmap
private

◆ m_FdLock

Mutex PosixSubsystem::m_FdLock
private

◆ m_FdMap

Tree<size_t, SharedPointer<FileDescriptor> > PosixSubsystem::m_FdMap
private

The file descriptor map. Maps number to pointers, the type of which is decided by the subsystem.

Definition at line 804 of file PosixSubsystem.h.

Referenced by acquireFileDescriptor(), acquireNextFileDescriptor(), addFileDescriptor(), closeFileDescriptor(), copyDescriptors(), descriptorMatchesOpenDescription(), duplicateFileDescriptor(), freeFd(), freeMultipleFds(), and installFileDescriptor().

◆ m_FreeCount

int PosixSubsystem::m_FreeCount
private

Number of times freed

Definition at line 824 of file PosixSubsystem.h.

Referenced by ~PosixSubsystem().

◆ m_ImageMetadataLock

Mutex PosixSubsystem::m_ImageMetadataLock
mutableprivate

Definition at line 836 of file PosixSubsystem.h.

◆ m_LastFd

size_t PosixSubsystem::m_LastFd
private

Last known unallocated descriptor

Definition at line 820 of file PosixSubsystem.h.

Referenced by closeFileDescriptor(), freeFd(), freeMultipleFds(), getFd(), and installFileDescriptor().

◆ m_Lock

Spinlock PosixSubsystem::m_Lock
private

Safety spinlock for mutual exclusion in acquire().

Definition at line 859 of file PosixSubsystem.h.

Referenced by acquire(), and release().

◆ m_MemoryLockAccount

PosixMemoryLockAccount PosixSubsystem::m_MemoryLockAccount
private

Definition at line 793 of file PosixSubsystem.h.

◆ m_Namespaces

SharedPointer<PosixNamespaceContext> PosixSubsystem::m_Namespaces
private
Initial value:
{
static SharedPointer< T > tryAllocate(Args...)

Definition at line 794 of file PosixSubsystem.h.

◆ m_NextFd

size_t PosixSubsystem::m_NextFd
private

The next available file descriptor.

Definition at line 808 of file PosixSubsystem.h.

Referenced by addFileDescriptor(), allocateFd(), copyDescriptors(), duplicateFileDescriptor(), getFd(), and installFileDescriptor().

◆ m_NextThreadWaiter

size_t PosixSubsystem::m_NextThreadWaiter
private

Definition at line 835 of file PosixSubsystem.h.

◆ m_pAcquiredThread

Thread* PosixSubsystem::m_pAcquiredThread
private

Which thread acquired?

Definition at line 854 of file PosixSubsystem.h.

Referenced by acquire(), and release().

◆ m_PendingSignals

SharedPointer<PendingSignalContext> PosixSubsystem::m_PendingSignals {new PendingSignalContext}
private

Definition at line 791 of file PosixSubsystem.h.

◆ m_SignalHandlers

Tree<size_t, SignalHandler*> PosixSubsystem::m_SignalHandlers
private

Definition at line 785 of file PosixSubsystem.h.

◆ m_SignalHandlersLock

UnlikelyLock PosixSubsystem::m_SignalHandlersLock
private

◆ m_SyncObjects

Tree<size_t, PosixSyncObject*> PosixSubsystem::m_SyncObjects
private

Links some file descriptors to PosixSyncObjects.

Definition at line 826 of file PosixSubsystem.h.

Referenced by ~PosixSubsystem().

◆ m_Threads

Tree<size_t, PosixThread*> PosixSubsystem::m_Threads
private

Links some thread handles to Threads.

Definition at line 830 of file PosixSubsystem.h.

Referenced by threadRemoved(), and ~PosixSubsystem().

◆ m_ThreadWaiters

Tree<void*, Semaphore*> PosixSubsystem::m_ThreadWaiters
private

Links waiter objects to Semaphores.

Definition at line 834 of file PosixSubsystem.h.

Referenced by PosixSubsystem(), and ~PosixSubsystem().

◆ m_TraceContext

PosixTraceContext PosixSubsystem::m_TraceContext
private

Signal handlers

Definition at line 784 of file PosixSubsystem.h.

Referenced by exit(), invoke(), queueSignalDelivery(), setProcess(), threadExiting(), and ~PosixSubsystem().

◆ m_UserImageActive

bool PosixSubsystem::m_UserImageActive = false
private

Definition at line 798 of file PosixSubsystem.h.

◆ m_UserImageGeneration

uint64_t PosixSubsystem::m_UserImageGeneration = 0
private

Definition at line 797 of file PosixSubsystem.h.

◆ m_UserImageSpace

VirtualAddressSpace* PosixSubsystem::m_UserImageSpace = nullptr
private

Definition at line 796 of file PosixSubsystem.h.

◆ MaximumExecArgumentBytes

constexpr size_t PosixSubsystem::MaximumExecArgumentBytes = 131072
staticconstexpr

Definition at line 308 of file PosixSubsystem.h.

◆ MaximumSupportedSignal

constexpr size_t PosixSubsystem::MaximumSupportedSignal = 64
staticconstexpr

Definition at line 196 of file PosixSubsystem.h.

◆ SafeExecute

const size_t PosixSubsystem::SafeExecute = 0x4
static

Definition at line 189 of file PosixSubsystem.h.

◆ SafeRead

const size_t PosixSubsystem::SafeRead = 0x1
static

Definition at line 187 of file PosixSubsystem.h.

◆ SafeRegion

const size_t PosixSubsystem::SafeRegion = 0x0
static

Sanitise flags.

Definition at line 186 of file PosixSubsystem.h.

◆ SafeWrite

const size_t PosixSubsystem::SafeWrite = 0x2
static

Definition at line 188 of file PosixSubsystem.h.

◆ SignalDispositionCount

constexpr size_t PosixSubsystem::SignalDispositionCount = MaximumSupportedSignal + 1
staticconstexpr

Definition at line 197 of file PosixSubsystem.h.


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