The Pedigree Project  0.1
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | 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  AlternateSignalStack
 
class  PosixSyncObject
 
class  PosixThread
 
struct  PosixThreadKey
 
struct  SignalHandler
 

Public Types

enum  Abi { PosixAbi = 0, LinuxAbi = 1 }
 
- Public Types inherited from Subsystem
enum  SubsystemType { Posix = 0, Native = 1, None = 255 }
 
enum  KillReason { Interrupted = 0, Terminated = 1, Unknown = 255 }
 
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, Other = 255
}
 

Public Member Functions

 PosixSubsystem ()
 
 PosixSubsystem (PosixSubsystem &s)
 
 PosixSubsystem (SubsystemType type)
 
virtual ~PosixSubsystem ()
 
virtual void acquire ()
 Acquire full mutual exclusion for all Subsystem resources. More...
 
virtual void release ()
 
virtual bool kill (KillReason killReason, Thread *pThread)
 
virtual void threadException (Thread *pThread, ExceptionType eType)
 
virtual void sendSignal (Thread *pThread, int signal, bool yield=true)
 
AlternateSignalStackgetAlternateSignalStack ()
 
void setAlternateSignalStack (AlternateSignalStack &s)
 
void setSignalHandler (size_t sig, SignalHandler *handler)
 
SignalHandlergetSignalHandler (size_t sig)
 
void exit (int code) NORETURN
 
bool copyDescriptors (PosixSubsystem *pSubsystem)
 
size_t getFd ()
 
void allocateFd (size_t fdNum)
 
void freeFd (size_t fdNum)
 
void freeMultipleFds (bool bOnlyCloExec=false, size_t iFirst=0, size_t iLast=-1)
 
FileDescriptorgetFileDescriptor (size_t fd)
 
void addFileDescriptor (size_t fd, FileDescriptor *pFd)
 
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 (FileDescriptor *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)
 
virtual FilefindFile (const String &path, File *workingDir)
 
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 ()
 
virtual void setProcess (Process *p)
 

Static Public Member Functions

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

Static Public Attributes

static const size_t SafeRegion = 0x0
 
static const size_t SafeRead = 0x1
 
static const size_t SafeWrite = 0x2
 

Private Member Functions

virtual void threadRemoved (Thread *pThread)
 
bool loadElf (File *pFile, uintptr_t mappedAddress, uintptr_t &newAddress, uintptr_t &finalAddress, bool &relocated)
 
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 parseShebang (File *pFile, File *&outFile, Vector< String > &argv)
 

Private Attributes

Tree< size_t, SignalHandler * > m_SignalHandlers
 
UnlikelyLock m_SignalHandlersLock
 
Tree< size_t, FileDescriptor * > m_FdMap
 
size_t m_NextFd
 
UnlikelyLock m_FdLock
 
ExtensibleBitmap m_FdBitmap
 
size_t m_LastFd
 
int m_FreeCount
 
AlternateSignalStack m_AltSigStack
 
Tree< size_t, PosixSyncObject * > m_SyncObjects
 
Tree< size_t, PosixThread * > m_Threads
 
Tree< void *, Semaphore * > m_ThreadWaiters
 
size_t m_NextThreadWaiter
 
Abi m_Abi
 
bool m_bAcquired
 
Threadm_pAcquiredThread
 
Spinlock m_Lock
 
LruCache< String, File * > m_FindFileCache
 LRU cache for file lookups. Many usage patterns involve something like a stat() immediately followed by an open() or other similar system call. Rather than have both fully complete a filesystem traversal, we can cache the result and save time.
 
Filesystemm_pRootFs = nullptr
 

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 105 of file PosixSubsystem.h.

Member Enumeration Documentation

ABI mode.

Definition at line 114 of file PosixSubsystem.h.

Constructor & Destructor Documentation

PosixSubsystem::PosixSubsystem ( )
inline

Default constructor

Definition at line 121 of file PosixSubsystem.h.

PosixSubsystem::PosixSubsystem ( PosixSubsystem s)
PosixSubsystem::PosixSubsystem ( SubsystemType  type)
inline

Parameterised constructor

Definition at line 134 of file PosixSubsystem.h.

PosixSubsystem::~PosixSubsystem ( )
virtual

Member Function Documentation

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 278 of file PosixSubsystem.cc.

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

Referenced by ~PosixSubsystem().

+ Here is the caller graph for this function:

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

Inserts a file descriptor

Todo:
this is possibly racy

Definition at line 946 of file PosixSubsystem.cc.

References UnlikelyLock::acquire(), allocateFd(), freeFd(), Tree< K, E >::insert(), m_FdLock, m_FdMap, and UnlikelyLock::release().

void PosixSubsystem::allocateFd ( size_t  fdNum)

Sets the given file descriptor as "in use".

Definition at line 781 of file PosixSubsystem.cc.

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

Referenced by addFileDescriptor().

+ Here is the caller graph for this function:

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 317 of file PosixSubsystem.cc.

References VirtualAddressSpace::CopyOnWrite, Dec, VirtualAddressSpace::getKernelStart(), VirtualAddressSpace::getMapping(), PhysicalMemoryManager::getPageSize(), VirtualAddressSpace::getUserStart(), Hex, Processor::information(), MemoryMapManager::instance(), VirtualAddressSpace::isMapped(), and VirtualAddressSpace::Write.

Referenced by WaitCleanup::terminated().

+ Here is the caller graph for this function:

bool PosixSubsystem::copyDescriptors ( PosixSubsystem pSubsystem)
void PosixSubsystem::exit ( int  code)
virtual
File * PosixSubsystem::findFile ( const String path,
File workingDir 
)
virtual

Finds a file, performing any subsystem-specific logic as needed.

Implements Subsystem.

Definition at line 1169 of file PosixSubsystem.cc.

References assert, VFS::find(), LruCache< K, T, Slots >::get(), getAbi(), Process::getCwd(), Filesystem::getRoot(), VFS::instance(), VFS::lookupFilesystem(), m_FindFileCache, m_pRootFs, and LruCache< K, T, Slots >::store().

void PosixSubsystem::freeFd ( size_t  fdNum)

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

Definition at line 796 of file PosixSubsystem.cc.

References UnlikelyLock::acquire(), ExtensibleBitmap::clear(), Tree< K, E >::lookup(), m_FdBitmap, m_FdLock, m_FdMap, m_LastFd, UnlikelyLock::release(), and Tree< K, E >::remove().

Referenced by addFileDescriptor().

+ Here is the caller graph for this function:

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

Retrieves the currently-active ABI for the subsystem.

Definition at line 510 of file PosixSubsystem.h.

Referenced by findFile(), and WaitCleanup::terminated().

+ Here is the caller graph for this function:

AlternateSignalStack& PosixSubsystem::getAlternateSignalStack ( )
inline

Grabs the alternate signal stack

Definition at line 203 of file PosixSubsystem.h.

size_t PosixSubsystem::getFd ( )

Returns the first available file descriptor.

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 753 of file PosixSubsystem.cc.

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

FileDescriptor * PosixSubsystem::getFileDescriptor ( size_t  fd)

Gets a pointer to a FileDescriptor object from an fd number

Definition at line 931 of file PosixSubsystem.cc.

References UnlikelyLock::enter(), UnlikelyLock::leave(), Tree< K, E >::lookup(), m_FdLock, and m_FdMap.

SignalHandler* PosixSubsystem::getSignalHandler ( size_t  sig)
inline

Gets a signal handler

Definition at line 276 of file PosixSubsystem.h.

References NORETURN.

Referenced by kill(), and sendSignal().

+ Here is the caller graph for this function:

PosixSyncObject* PosixSubsystem::getSyncObject ( size_t  n)
inline

Gets a synchronisation object given a descriptor

Definition at line 334 of file PosixSubsystem.h.

PosixThread* PosixSubsystem::getThread ( size_t  n)
inline

Gets a thread given a descriptor

Definition at line 439 of file PosixSubsystem.h.

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

Gets a thread waiter object given a descriptor

Definition at line 462 of file PosixSubsystem.h.

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

Inserts a synchronisation object given a descriptor

Definition at line 340 of file PosixSubsystem.h.

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 445 of file PosixSubsystem.h.

void* PosixSubsystem::insertThreadWaiter ( Semaphore waiter)
inline

Inserts a thread waiter object, returns a descriptor

Definition at line 468 of file PosixSubsystem.h.

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

Invokes the given command (thread mechanism).

Implements Subsystem.

Definition at line 1231 of file PosixSubsystem.cc.

Referenced by invoke(), and parseShebang().

+ Here is the caller graph for this function:

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 1237 of file PosixSubsystem.cc.

References invoke().

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 1378 of file PosixSubsystem.cc.

References invoke().

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 1385 of file PosixSubsystem.cc.

References invoke().

bool PosixSubsystem::invoke ( File originalFile,
const String originalName,
Vector< String > &  argv,
Vector< String > &  env,
SyscallState *  state 
)
private
bool PosixSubsystem::kill ( KillReason  killReason,
Thread pThread 
)
virtual
bool PosixSubsystem::loadElf ( File pFile,
uintptr_t  mappedAddress,
uintptr_t &  newAddress,
uintptr_t &  finalAddress,
bool &  relocated 
)
private

Load an ELF's PT_LOAD sections into the address space.

Definition at line 990 of file PosixSubsystem.cc.

References ERROR, File::getName(), PhysicalMemoryManager::getPageSize(), Hex, Processor::information(), MemoryMapManager::instance(), MemoryMapManager::mapAnon(), and MemoryMapManager::mapFile().

Referenced by invoke().

+ Here is the caller graph for this function:

bool PosixSubsystem::parseShebang ( File pFile,
File *&  outFile,
Vector< String > &  argv 
)
private

Parse a file for a possible shebang line.

Todo:
this loop could terminate MUCH faster

Definition at line 1244 of file PosixSubsystem.cc.

References Vector< T >::begin(), Vector< T >::count(), Symlink::followLink(), Symlink::fromFile(), File::getFullPath(), invoke(), File::isDirectory(), File::isSymlink(), Vector< T >::popBack(), Vector< T >::pushFront(), and File::read().

Referenced by invoke().

+ Here is the caller graph for this function:

void PosixSubsystem::release ( )
virtual

Release mutual exclusion acquired via acquire().

Reimplemented from Subsystem.

Definition at line 304 of file PosixSubsystem.cc.

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

Referenced by ~PosixSubsystem().

+ Here is the caller graph for this function:

void PosixSubsystem::removeSyncObject ( size_t  n)
inline

Removes a semaphore given a descriptor

Definition at line 353 of file PosixSubsystem.h.

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 455 of file PosixSubsystem.h.

void PosixSubsystem::removeThreadWaiter ( void *  n)
inline

Removes a thread waiter object given a descriptor

Definition at line 479 of file PosixSubsystem.h.

void PosixSubsystem::sendSignal ( Thread pThread,
int  signal,
bool  yield = true 
)
virtual
void PosixSubsystem::setAbi ( Abi  which)
inline

Switch the ABI of the subsystem to the specified choice.

Definition at line 516 of file PosixSubsystem.h.

Referenced by PosixSyscallManager::syscall().

+ Here is the caller graph for this function:

void PosixSubsystem::setAlternateSignalStack ( AlternateSignalStack s)
inline

Sets the alternate signal stack, if possible

Definition at line 209 of file PosixSubsystem.h.

void PosixSubsystem::setSignalHandler ( size_t  sig,
SignalHandler handler 
)
void PosixSubsystem::threadException ( Thread pThread,
ExceptionType  eType 
)
virtual

A thread has thrown an exception!

Reimplemented from Subsystem.

Definition at line 556 of file PosixSubsystem.cc.

References Dec, ERROR, Process::getId(), Thread::getId(), Thread::getParent(), and sendSignal().

void PosixSubsystem::threadRemoved ( Thread pThread)
privatevirtual

Notifies the subsystem that the given thread has been removed.

Reimplemented from Subsystem.

Definition at line 965 of file PosixSubsystem.cc.

References VFS::checkAccess(), FileDescriptor::file, m_Threads, and Semaphore::release().

Member Data Documentation

Abi PosixSubsystem::m_Abi
private

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

Definition at line 595 of file PosixSubsystem.h.

AlternateSignalStack PosixSubsystem::m_AltSigStack
private

Alternate signal stack - if defined, used instead of a system-defined stack

Definition at line 576 of file PosixSubsystem.h.

bool PosixSubsystem::m_bAcquired
private

Are we acquired?

Definition at line 600 of file PosixSubsystem.h.

Referenced by acquire(), and release().

ExtensibleBitmap PosixSubsystem::m_FdBitmap
private

File descriptors used by this process

Definition at line 563 of file PosixSubsystem.h.

Referenced by allocateFd(), copyDescriptors(), freeFd(), freeMultipleFds(), and getFd().

UnlikelyLock PosixSubsystem::m_FdLock
private

Lock to guard the next file descriptor while it is being changed.

Definition at line 559 of file PosixSubsystem.h.

Referenced by acquire(), addFileDescriptor(), allocateFd(), copyDescriptors(), freeFd(), freeMultipleFds(), getFd(), getFileDescriptor(), and release().

Tree<size_t, 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 551 of file PosixSubsystem.h.

Referenced by addFileDescriptor(), copyDescriptors(), freeFd(), freeMultipleFds(), and getFileDescriptor().

int PosixSubsystem::m_FreeCount
private

Number of times freed

Definition at line 571 of file PosixSubsystem.h.

Referenced by ~PosixSubsystem().

size_t PosixSubsystem::m_LastFd
private

Last known unallocated descriptor

Definition at line 567 of file PosixSubsystem.h.

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

Spinlock PosixSubsystem::m_Lock
private

Safety spinlock for mutual exclusion in acquire().

Definition at line 610 of file PosixSubsystem.h.

Referenced by acquire(), and release().

size_t PosixSubsystem::m_NextFd
private

The next available file descriptor.

Definition at line 555 of file PosixSubsystem.h.

Referenced by allocateFd(), copyDescriptors(), and getFd().

Thread* PosixSubsystem::m_pAcquiredThread
private

Which thread acquired?

Definition at line 605 of file PosixSubsystem.h.

Referenced by acquire(), and release().

Filesystem* PosixSubsystem::m_pRootFs = nullptr
private

Cached lookup of the root filesystem.

Definition at line 621 of file PosixSubsystem.h.

Referenced by findFile().

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

Signal handlers

Definition at line 542 of file PosixSubsystem.h.

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

UnlikelyLock PosixSubsystem::m_SignalHandlersLock
private

A lock for access to the signal handlers tree

Definition at line 545 of file PosixSubsystem.h.

Referenced by acquire(), PosixSubsystem(), release(), and setSignalHandler().

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

Links some file descriptors to PosixSyncObjects.

Definition at line 580 of file PosixSubsystem.h.

Referenced by ~PosixSubsystem().

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

Links some thread handles to Threads.

Definition at line 584 of file PosixSubsystem.h.

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

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

Links waiter objects to Semaphores.

Definition at line 588 of file PosixSubsystem.h.

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

const size_t PosixSubsystem::SafeRegion = 0x0
static

Sanitise flags.

Definition at line 109 of file PosixSubsystem.h.


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