summaryrefslogtreecommitdiff
path: root/Kernel/Thread.cpp
AgeCommit message (Collapse)Author
2023-01-02Kernel: Turn lock ranks into template parameterskleines Filmröllchen
This step would ideally not have been necessary (increases amount of refactoring and templates necessary, which in turn increases build times), but it gives us a couple of nice properties: - SpinlockProtected inside Singleton (a very common combination) can now obtain any lock rank just via the template parameter. It was not previously possible to do this with SingletonInstanceCreator magic. - SpinlockProtected's lock rank is now mandatory; this is the majority of cases and allows us to see where we're still missing proper ranks. - The type already informs us what lock rank a lock has, which aids code readability and (possibly, if gdb cooperates) lock mismatch debugging. - The rank of a lock can no longer be dynamic, which is not something we wanted in the first place (or made use of). Locks randomly changing their rank sounds like a disaster waiting to happen. - In some places, we might be able to statically check that locks are taken in the right order (with the right lock rank checking implementation) as rank information is fully statically known. This refactoring even more exposes the fact that Mutex has no lock rank capabilites, which is not fixed here.
2022-12-29Kernel: Move ThreadRegisters into arch-specific directoryTimon Kruiper
These are architecture-specific anyway, so they belong in the Arch directory. This commit also adds ThreadRegisters::set_initial_state to factor out the logic in Thread.cpp.
2022-12-29Kernel: Remove debug printing of code segmentTimon Kruiper
This allows us to use the same code for aarch64.
2022-12-28Kernel: Remove the two remaining ARCH(I386) checksAndreas Kling
2022-12-28Kernel: Remove i686 supportLiav A
2022-12-10Kernel: Set EFLAGS/RFLAGS to a known-good value on signal entrySergey Lisov
2022-11-05Kernel: Add support for jailsLiav A
Our implementation for Jails resembles much of how FreeBSD jails are working - it's essentially only a matter of using a RefPtr in the Process class to a Jail object. Then, when we iterate over all processes in various cases, we could ensure if either the current process is in jail and therefore should be restricted what is visible in terms of PID isolation, and also to be able to expose metadata about Jails in /sys/kernel/jails node (which does not reveal anything to a process which is in jail). A lifetime model for the Jail object is currently plain simple - there's simpy no way to manually delete a Jail object once it was created. Such feature should be carefully designed to allow safe destruction of a Jail without the possibility of releasing a process which is in Jail from the actual jail. Each process which is attached into a Jail cannot leave it until the end of a Process (i.e. when finalizing a Process). All jails are kept being referenced in the JailManagement. When a last attached process is finalized, the Jail is automatically destroyed.
2022-10-17Kernel: Move InterruptDisabler out of Arch directoryTimon Kruiper
The code in this file is not architecture specific, so it can be moved to the base Kernel directory.
2022-10-16Kernel: Add more stubs needed for Thread.cppGunnar Beutner
2022-10-16Kernel: Don't directly include <Kernel/Arch/x86/TrapFrame.h>Gunnar Beutner
This adds a new arch-independent header which in turn includes the correct header for the build architecture.
2022-08-26Kernel: Remove global MM lock in favor of SpinlockProtectedAndreas Kling
Globally shared MemoryManager state is now kept in a GlobalData struct and wrapped in SpinlockProtected. A small set of members are left outside the GlobalData struct as they are only set during boot initialization, and then remain constant. This allows us to access those members without taking any locks.
2022-08-24Kernel: Wrap process address spaces in SpinlockProtectedAndreas Kling
This forces anyone who wants to look into and/or manipulate an address space to lock it. And this replaces the previous, more flimsy, manual spinlock use. Note that pointers *into* the address space are not safe to use after you unlock the space. We've got many issues like this, and we'll have to track those down as wlel.
2022-08-22Kernel: Use Process::credentials() and remove user ID/group ID helpersAnthony Iacono
Move away from using the group ID/user ID helpers in the process to allow for us to take advantage of the immutable credentials instead.
2022-08-20Kernel: Make self-contained locking smart pointers their own classesAndreas Kling
Until now, our kernel has reimplemented a number of AK classes to provide automatic internal locking: - RefPtr - NonnullRefPtr - WeakPtr - Weakable This patch renames the Kernel classes so that they can coexist with the original AK classes: - RefPtr => LockRefPtr - NonnullRefPtr => NonnullLockRefPtr - WeakPtr => LockWeakPtr - Weakable => LockWeakable The goal here is to eventually get rid of the Lock* classes in favor of using external locking.
2022-08-19Kernel: Don't take thread lock for signal dispatchAndreas Kling
Signal dispatch is already protected by the global scheduler lock, but in some cases we also took Thread::m_lock for some reason. This led to a number of different deadlocks that started showing up with 4+ CPU's attached to the system. As a first step towards solving this, simply don't take the thread lock and let the scheduler lock cover it. Eventually, we should work in the other direction and break the scheduler lock into much finer-grained locks, but let's get out of the deadlock swamp first.
2022-08-19Kernel: Don't lock scheduler in ~Thread()Andreas Kling
This is not necessary, and is a leftover from before Thread started using the ListedRefCounted pattern to be safely removed from lists on the last call to unref().
2022-08-19Kernel: Don't lock scheduler while updating thread scheduling timesAndreas Kling
We can use simple atomic variables with relaxed ordering for this, and avoid locking altogether.
2022-08-18Kernel: Use consistent lock acquisition order in Thread::block*()Andreas Kling
We want to grab g_scheduler_lock *before* Thread::m_block_lock. This appears to have fixed a deadlock that I encountered while building DOOM with make -j2.
2022-08-10Kernel: Fix a typo and a grammar issue in code commentsBrian Gianforcaro
2022-07-27Everywhere: Make the codebase more architecture awareUndefine
2022-07-22LibC: Implement `pthread_cancel`Tim Schumacher
2022-07-22Kernel+LibC: Don't hardcode the maximum signal number everywhereTim Schumacher
2022-07-12Everywhere: Add sv suffix to strings relying on StringView(char const*)sin-ack
Each of these strings would previously rely on StringView's char const* constructor overload, which would call __builtin_strlen on the string. Since we now have operator ""sv, we can replace these with much simpler versions. This opens the door to being able to remove StringView(char const*). No functional changes.
2022-07-08Kernel: Unblock SignalBlocker if a signal was just unmarked as pendingTim Schumacher
When updating the signal mask, there is a small frame where we might set up the receiving process for handing the signal and therefore remove that signal from the list of pending signals before SignalBlocker has a chance to block. In turn, this might cause SignalBlocker to never notice that the signal arrives and it will never unblock once blocked. Track the currently handled signal separately and include it when determining if SignalBlocker should be unblocking.
2022-06-02Kernel: Implement InterruptDisabler using generic Processor functionsTimon Kruiper
Now that the code does not use architectural specific code, it is moved to the generic Arch directory and the paths are modified accordingly.
2022-04-03Kernel: Unbreak ASLR in the new RegionTree worldAndreas Kling
Functions that allocate and/or place a Region now take a parameter that tells it whether to randomize unspecified addresses.
2022-04-03Kernel: Make VM allocation atomic for userspace regionsAndreas Kling
This patch move AddressSpace (the per-process memory manager) to using the new atomic "place" APIs in RegionTree as well, just like we did for MemoryManager in the previous commit. This required updating quite a few places where VM allocation and actually committing a Region object to the AddressSpace were separated by other code. All you have to do now is call into AddressSpace once and it'll take care of everything for you.
2022-04-01Everywhere: Run clang-formatIdan Horowitz
2022-03-04Kernel: Save and restore FPU state on signal dispatch on i386/x86_64Ali Mohammad Pur
2022-03-04Kernel: Fill some siginfo and ucontext fields on SA_SIGINFOAli Mohammad Pur
There's no reason to fill in any of these fields if SA_SIGINFO is not given, as the signal handler won't be reading from them at all.
2022-03-04Kernel: Add support for SA_SIGINFOAli Mohammad Pur
We currently don't really populate most of the fields, but that can wait :^)
2022-03-04Kernel: Make the signal trampoline stack alignment a bit more readableAli Mohammad Pur
The comments were confusing, and had a mathematical error, stop trying to be clever and just let the computer do the math. Also assert that we're pushing exactly as many stack elements as we're using for the alignment calculations.
2022-03-04Kernel: Move signal handlers from being thread state to process stateAli Mohammad Pur
POSIX requires that sigaction() and friends set a _process-wide_ signal handler, so move signal handlers and flags inside Process. This also fixes a "pid/tid confusion" FIXME, as we can now send the signal to the process and let that decide which thread should get the signal (which is the thread with tid==pid, but that's now the Process's problem). Note that each thread still retains its signal mask, as that is local to each thread.
2022-02-27Kernel: Defer signal handling without a register capture earlierIdan Horowitz
We were deferring the signal handling after already marking the signal as handling, which led to some failures in the Shell tests.
2022-02-21Kernel: Try to dispatch pending signals on context switchIdan Horowitz
This ensures that processes that don't perform any syscalls will also eventually receive signals.
2022-02-21Kernel: VERIFY that signals are not sent to Kernel processesIdan Horowitz
Kernel processes can't handle signals, nor should they ever receive any
2022-02-13Kernel: Use try_make_weak_ptr() instead of make_weak_ptr()Idan Horowitz
2022-02-03Kernel: Convert try_make_ref_counted to use ErrorOrIdan Horowitz
This allows more ergonomic memory allocation failure related error checking using the TRY macro.
2022-01-30Kernel: Take scheduler lock before block lock in unblock_from_mutex()Andreas Kling
This matches the acquisition order used elsewhere.
2022-01-30Kernel: Enforce that Thread::unblock_from_mutex() doesn't happen in IRQAndreas Kling
Mutexes are not usable from IRQ handlers, so unblock_from_mutex() can simply VERIFY() that the current processor is not in an IRQ.
2022-01-30Kernel: Update terminology around Thread's "blocking mutex"Andreas Kling
It's more accurate to say that we're blocking on a mutex, rather than blocking on a lock. The previous terminology made sense when this code was using something called Kernel::Lock, but since it was renamed to Kernel::Mutex, this updates brings the language back in sync.
2022-01-30Kernel: Make Thread::State an `enum class` and use it consistentlyAndreas Kling
It was annoyingly hard to spot these when we were using them with different amounts of qualification everywhere. This patch uses Thread::State::Foo everywhere instead of Thread::Foo or just Foo.
2022-01-30Kernel: Don't dispatch signals in Thread::block_impl()Andreas Kling
If the blocker is interrupted by a signal, that signal will be delivered to the process when returning to userspace (at the syscall exit point.) We don't have to perform the dispatch manually in Thread::block_impl().
2022-01-30Kernel: Don't dispatch signals in Processor::enter_current()Andreas Kling
Signal dispatch is already taken care of elsewhere, so there appears to be no need for the hack in enter_current(). This also allows us to remove the Thread::m_in_block flag, simplifying thread blocking logic somewhat. Verified with the original repro for #4336 which this was meant to fix.
2022-01-30Kernel: Remove unnecessary includes from Thread.hAndreas Kling
...and deal with the fallout by adding missing includes everywhere.
2022-01-30Kernel: Move Thread::block<BlockerType>() out of the Thread.h headerAndreas Kling
This function is large and unwieldy and forces Thread.h to #include a bunch of things. The only reason it was in the header is because we need to instantiate a blocker based on the templated BlockerType. We actually keep block<BlockerType>() in the header, but move the bulk of the function body out of line into Thread::block_impl(). To preserve destructor ordering, we add Blocker::finalize() which is called where we'd previously destroy the Blocker.
2022-01-29Kernel: Stop using HashMap in MutexIdan Horowitz
This commit removes the usage of HashMap in Mutex, thereby making Mutex be allocation-free. In order to achieve this several simplifications were made to Mutex, removing unused code-paths and extra VERIFYs: * We no longer support 'upgrading' a shared lock holder to an exclusive holder when it is the only shared holder and it did not unlock the lock before relocking it as exclusive. NOTE: Unlike the rest of these changes, this scenario is not VERIFY-able in an allocation-free way, as a result the new LOCK_SHARED_UPGRADE_DEBUG debug flag was added, this flag lets Mutex allocate in order to detect such cases when debugging a deadlock. * We no longer support checking if a Mutex is locked by the current thread when the Mutex was not locked exclusively, the shared version of this check was not used anywhere. * We no longer support force unlocking/relocking a Mutex if the Mutex was not locked exclusively, the shared version of these functions was not used anywhere.
2022-01-26Kernel: Ignore allocation failures during thread finalizationIdan Horowitz
We ignore allocation failures above the first 32 guaranteed thread slots, and just flag our future-selves to finalize these threads at a later point.
2022-01-26Kernel: Remove always-false Thread::drop_thread_count boolean parameterIdan Horowitz
2022-01-26Kernel: Add tracing to help catch thread blocking with incorrect stateBrian Gianforcaro
A number of crashes in this `VERIFY_NOT_REACHED` case have been reported on discord. Lets add some tracing to gather more information and help diagnose what is the cause of these crashes.