20 #ifndef POSIX_SUBSYSTEM_H 21 #define POSIX_SUBSYSTEM_H 23 #include "pedigree/kernel/Subsystem.h" 24 #include "pedigree/kernel/compiler.h" 25 #include "pedigree/kernel/process/Mutex.h" 26 #include "pedigree/kernel/process/Semaphore.h" 27 #include "pedigree/kernel/process/SignalEvent.h" 28 #include "pedigree/kernel/processor/types.h" 30 #include "pedigree/kernel/LockGuard.h" 31 #include "pedigree/kernel/utilities/ExtensibleBitmap.h" 32 #include "pedigree/kernel/utilities/LruCache.h" 34 #include "pedigree/kernel/utilities/Tree.h" 36 #include "pedigree/kernel/utilities/Vector.h" 38 #include "modules/subsys/posix/logging.h" 50 extern size_t getAvailableDescriptor();
53 #define GRAB_POSIX_SUBSYSTEM(returnValue) \ 54 PosixSubsystem *pSubsystem = getSubsystem(); \ 57 return (returnValue); \ 59 #define GRAB_POSIX_SUBSYSTEM_NORET \ 60 PosixSubsystem *pSubsystem = getSubsystem(); \ 109 static const size_t SafeRegion = 0x0;
110 static const size_t SafeRead = 0x1;
111 static const size_t SafeWrite = 0x2;
122 :
Subsystem(Posix), m_SignalHandlers(), m_SignalHandlersLock(),
123 m_FdMap(), m_NextFd(0), m_FdLock(), m_FdBitmap(), m_LastFd(0),
124 m_FreeCount(1), m_AltSigStack(), m_SyncObjects(), m_Threads(),
125 m_ThreadWaiters(), m_NextThreadWaiter(0), m_Abi(PosixAbi),
126 m_bAcquired(false), m_pAcquiredThread(nullptr)
135 :
Subsystem(type), m_SignalHandlers(), m_SignalHandlersLock(),
136 m_FdMap(), m_NextFd(0), m_FdLock(), m_FdBitmap(), m_LastFd(0),
137 m_FreeCount(1), m_AltSigStack(), m_SyncObjects(), m_Threads(),
138 m_ThreadWaiters(), m_NextThreadWaiter(0), m_Abi(PosixAbi),
139 m_bAcquired(false), m_pAcquiredThread(nullptr)
147 virtual void acquire();
150 virtual void release();
166 static bool checkAddress(uintptr_t addr,
size_t extent,
size_t flags);
175 virtual void sendSignal(
Thread *pThread,
int signal,
bool yield =
true);
205 return m_AltSigStack;
217 SignalHandler() : sig(255), pEvent(0), sigMask(), flags(0), type(0)
231 pEvent->waitForDeliveries();
278 while (!m_SignalHandlersLock.enter())
281 m_SignalHandlersLock.leave();
294 void allocateFd(
size_t fdNum);
298 void freeFd(
size_t fdNum);
301 void freeMultipleFds(
302 bool bOnlyCloExec =
false,
size_t iFirst = 0,
size_t iLast = -1);
336 return m_SyncObjects.lookup(n);
345 m_SyncObjects.remove(n);
349 m_SyncObjects.insert(n, sem);
358 m_SyncObjects.remove(n);
367 void (*destructor)(
void *);
378 : pThread(0), isRunning(
true), returnValue(0), canReclaim(
false),
379 isDetached(
false), m_ThreadData(), m_ThreadKeys(), lastDataKey(0),
403 return m_ThreadData.
lookup(key);
421 if (m_ThreadData.
lookup(key))
423 m_ThreadData.
insert(key, info);
441 return m_Threads.lookup(n);
450 return m_Threads.insert(n, thread);
464 return m_ThreadWaiters.lookup(n);
470 void *descriptor =
reinterpret_cast<void *
>(m_NextThreadWaiter++);
471 Semaphore *t = m_ThreadWaiters.lookup(descriptor);
473 m_ThreadWaiters.remove(descriptor);
474 m_ThreadWaiters.insert(descriptor, waiter);
481 m_ThreadWaiters.remove(n);
486 bool bExecute)
const;
495 SyscallState &state);
522 virtual void threadRemoved(
Thread *pThread);
526 File *pFile, uintptr_t mappedAddress, uintptr_t &newAddress,
527 uintptr_t &finalAddress,
bool &relocated);
531 SyscallState *state);
589 size_t m_NextThreadWaiter;
Tree< size_t, PosixThreadKey * > m_ThreadData
A key/value dictionary for string keys.
Thread * m_pAcquiredThread
A vector / dynamic array.
ExtensibleBitmap m_GroupIds
Tree< size_t, FileDescriptor * > m_FdMap
void removeThreadWaiter(void *n)
void * insertThreadWaiter(Semaphore *waiter)
ExtensibleBitmap m_FdBitmap
size_t size
Size of the stack.
Tree< size_t, PosixThread * > m_Threads
Tree< void *, Semaphore * > m_ThreadWaiters
void setGroupId(size_t gid)
Tree< size_t, SignalHandler * > m_SignalHandlers
Tree< size_t, PosixSyncObject * > m_SyncObjects
PosixSyncObject * getSyncObject(size_t n)
void removeThreadData(size_t key)
AlternateSignalStack m_AltSigStack
void * buffer
Buffer pointer.
void setAlternateSignalStack(AlternateSignalStack &s)
PosixThreadKey * getThreadData(size_t key)
uint32_t flags
Signal handler flags.
void insert(const K &key, const E &value)
SignalHandler * getSignalHandler(size_t sig)
PosixSubsystem(SubsystemType type)
LruCache< String, File * > m_FindFileCache
LRU cache for file lookups. Many usage patterns involve something like a stat() immediately followed ...
void removeThread(size_t n)
bool addThreadData(size_t key, PosixThreadKey *info)
void insertSyncObject(size_t n, PosixSyncObject *sem)
int type
Type - 0 = normal, 1 = SIG_DFL, 2 = SIG_IGN.
SignalEvent * pEvent
Event for the signal handler.
void removeSyncObject(size_t n)
uintptr_t base
The location of this stack.
bool inUse
Are we to use this alternate stack rather than a normal stack?
PosixThread * getThread(size_t n)
UnlikelyLock m_SignalHandlersLock
void returnGroupId(size_t gid)
Semaphore * getThreadWaiter(void *n)
size_t lastDataKey
Last data key that was allocated (for the bitmap)
void remove(const K &key)
void insertThread(size_t n, PosixThread *thread)
size_t nextDataKey
Next data key available.
AlternateSignalStack()
Default constructor.
uint32_t sigMask
Signal mask to set when this signal handler is called.
E lookup(const K &key) const
bool isGroupIdValid(size_t gid) const
AlternateSignalStack & getAlternateSignalStack()
Implements a Radix Tree, a kind of Trie with compressed keys.