summaryrefslogtreecommitdiff
path: root/Kernel/Thread.cpp
AgeCommit message (Collapse)Author
2021-08-22Kernel: Rename ScopedSpinlock => SpinlockLockerAndreas Kling
This matches MutexLocker, and doesn't sound like it's a lock itself.
2021-08-22Kernel: Rename SpinLock => SpinlockAndreas Kling
2021-08-22Kernel: Rename SpinLockProtectedValue<T> => SpinLockProtected<T>Andreas Kling
2021-08-19Kernel: Consolidate a bunch of i386/x86_64 code pathsAndreas Kling
Add some arch-specific getters and setters that allow us to merge blocks that were previously specific to either ARCH(I386) or ARCH(X86_64).
2021-08-17Kernel: Fix compilation with ClangTimothy Flynn
Co-Authored-By: Andrew Kaster <akaster@serenityos.org>
2021-08-17Kernel: Port Thread to ListedRefCountedAndreas Kling
2021-08-16Kernel: Don't hold thread list lock while invoking ~Thread()Andreas Kling
There is no need for this, and it can cause deadlocks if ~Thread() ends up doing something else that requires a lock (e.g ~Process())
2021-08-15Kernel: Lock thread list while in Thread::unref()Andreas Kling
This patch does three things: - Convert the global thread list from a HashMap to an IntrusiveList - Combine the thread list and its lock into a SpinLockProtectedValue - Customize Thread::unref() so it locks the list while unreffing This closes the same race window for Thread as @sin-ack's recent changes did for Process. Note that the HashMap->IntrusiveList conversion means that we lose O(1) lookups, but the majority of clients of this list are doing traversal, not lookup. Once we have an intrusive hashing solution, we should port this to use that, but for now, this gets rid of heap allocations during a sensitive time.
2021-08-14Kernel+LibC: Share definitions for signal.hAndreas Kling
2021-08-13Kernel: Reduce LOCK_DEBUG ifdefs by utilizing Kernel::LockLocationBrian Gianforcaro
The LOCK_DEBUG conditional code is pretty ugly for a feature that we only use rarely. We can remove a significant amount of this code by utilizing a zero sized fake type when not building in LOCK_DEBUG mode. This lets us keep the same API, but just let the compiler optimize it away when don't actually care about the location the caller came from.
2021-08-12Kernel: Steer away from heap allocations for ProcFS process dataLiav A
Instead, use more static patterns to acquire that sort of data.
2021-08-10Kernel: Fix kernel panic when blocking on the process' big lockGunnar Beutner
Another thread might end up marking the blocking thread as holding the lock before it gets a chance to finish invoking the scheduler.
2021-08-10Kernel/SMP: Change critical sections to not disable interruptsAndreas Kling
Leave interrupts enabled so that we can still process IRQs. Critical sections should only prevent preemption by another thread. Co-authored-by: Tom <tomut@yahoo.com>
2021-08-10Kernel/SMP: Make entering/leaving critical sections multi-processor safeAndreas Kling
By making these functions static we close a window where we could get preempted after calling Processor::current() and move to another processor. Co-authored-by: Tom <tomut@yahoo.com>
2021-08-08Kernel: Rename queue_runnable_thread() => enqueue_runnable_thread()Andreas Kling
2021-08-08Kernel: Fix UB caused by taking a reference to a packed struct's memberDaniel Bertalan
Taking a reference or a pointer to a value that's not aligned properly is undefined behavior. While `[[gnu::packed]]` ensures that reads from and writes to fields of packed structs is a safe operation, the information about the reduced alignment is lost when creating pointers to these values. Weirdly enough, GCC's undefined behavior sanitizer doesn't flag these, even though the doc of `-Waddress-of-packed-member` says that it usually leads to UB. In contrast, x86_64 Clang does flag these, which renders the 64-bit kernel unable to boot. For now, the `address-of-packed-member` warning will only be enabled in the kernel, as it is absolutely crucial there because of KUBSAN, but might get excessively noisy for the userland in the future. Also note that we can't append to `CMAKE_CXX_FLAGS` like we do for other flags in the kernel, because flags added via `add_compile_options` come after these, so the `-Wno-address-of-packed-member` in the root would cancel it out.
2021-08-06Kernel: Add convenience values to the Memory::Region::Access enumAndreas Kling
Instead of `Memory::Region::Access::Read | Memory::Region::AccessWrite` you can now say `Memory::Region::Access::ReadWrite`.
2021-08-06Kernel: Rename Process::space() => Process::address_space()Andreas Kling
We commonly talk about "a process's address space" so let's nudge the code towards matching how we talk about it. :^)
2021-08-06Kernel: Move Kernel/Memory/ code into Kernel::Memory namespaceAndreas Kling
2021-08-06Kernel: Rename Kernel/VM/ to Kernel/Memory/Andreas Kling
This directory isn't just about virtual memory, it's about all kinds of memory management.
2021-08-06Kernel: Don't make a separate allocation for thread FPU stateAndreas Kling
We were allocating thread FPU state separately in order to ensure a 16-byte alignment. There should be no need to do that. This patch makes it a regular value member of Thread instead, dodging one heap allocation during thread creation.
2021-08-06Kernel: Store Thread name as a KStringAndreas Kling
2021-08-06Kernel: Make Thread::state_string() return StringViewAndreas Kling
2021-07-26Kernel: Add option to build with coverage instrumentation and KCOVPatrick Meyer
GCC and Clang allow us to inject a call to a function named __sanitizer_cov_trace_pc on every edge. This function has to be defined by us. By noting down the caller in that function we can trace the code we have encountered during execution. Such information is used by coverage guided fuzzers like AFL and LibFuzzer to determine if a new input resulted in a new code path. This makes fuzzing much more effective. Additionally this adds a basic KCOV implementation. KCOV is an API that allows user space to request the kernel to start collecting coverage information for a given user space thread. Furthermore KCOV then exposes the collected program counters to user space via a BlockDevice which can be mmaped from user space. This work is required to add effective support for fuzzing SerenityOS to the Syzkaller syscall fuzzer. :^) :^)
2021-07-22Everywhere: Prefer using {:#x} over 0x{:x}Gunnar Beutner
We have a dedicated format specifier which adds the "0x" prefix, so let's use that instead of adding it manually.
2021-07-22Everywhere: Prefix hexadecimal numbers with 0xGunnar Beutner
Depending on the values it might be difficult to figure out whether a value is decimal or hexadecimal. So let's make this more obvious. Also this allows copying and pasting those numbers into GNOME calculator and probably also other apps which auto-detect the base.
2021-07-19Kernel: Push ARCH specific ifdef's down into RegisterState functionsBrian Gianforcaro
The non CPU specific code of the kernel shouldn't need to deal with architecture specific registers, and should instead deal with an abstract view of the machine. This allows us to remove a variety of architecture specific ifdefs and helps keep the code slightly more portable. We do this by exposing the abstract representation of instruction pointer, stack pointer, base pointer, return register, etc on the RegisterState struct.
2021-07-18Everywhere: Make tracking cpu usage independent from system ticksTom
This switches tracking CPU usage to more accurately measure time in user and kernel land using either the TSC or another time source. This will also come in handy when implementing a tickless kernel mode.
2021-07-18Kernel: Support 64-bit addresses for kernel thread stacksGunnar Beutner
2021-07-18Kernel: Fix blocking relock of the big_lock while unlocking other LockTom
When a Thread is being unblocked and we need to re-lock the process big_lock and re-locking blocks again, then we may end up in Thread::block again while still servicing the original lock's Thread::block. So permit recursion as long as it's only the big_lock that we block on again. Fixes #8822
2021-07-17Kernel: Rename Lock to MutexAndreas Kling
Let's be explicit about what kind of lock this is meant to be.
2021-07-17Kernel: Remove double RedBlackTree lookup in VM/Space region removalBrian Gianforcaro
We should never request a regions removal that we don't currently own. We currently assert this everywhere else by all callers. Instead lets just push the assert down into the RedBlackTree removal and assume that we will always successfully remove the region.
2021-07-16Kernel: Implement aligned `operator new` and use itDaniel Bertalan
The compiler will use these to allocate objects that have alignment requirements greater than that of our normal `operator new` (4/8 byte aligned). This means we can now use smart pointers for over-aligned types. Fixes a FIXME.
2021-07-16Kernel: Rename functions to be less confusingTom
Thread::yield_and_release_relock_big_lock releases the big lock, yields and then relocks the big lock. Thread::yield_assuming_not_holding_big_lock yields assuming the big lock is not being held.
2021-07-16Kernel: Release big lock when blocking on another lockTom
When blocking on a Lock other than the big lock and we're holding the big lock, we need to release the big lock first. This fixes some deadlocks where a thread blocks while holding the big lock, preventing other threads from getting the big lock in order to unblock the waiting thread.
2021-07-12Kernel: Allow Lock to block from BlockConditionTom
This enables the Lock class to block a thread even while the thread is working on a BlockCondition. A thread can still only be either blocked by a Lock or a BlockCondition. This also establishes a linked list of threads that are blocked by a Lock and unblocking directly unlocks threads and wakes them directly.
2021-07-11Kernel: Rename ProcFSComponentsRegistrar => ProcFSComponentRegistryAndreas Kling
This matches the formatting used in SysFS.
2021-07-06Kernel+LibELF: Don't demangle symbols in the kernelGunnar Beutner
Instead we should just generate kernel.map in such a way that it already contains demangled symbols.
2021-07-05Kernel+LibC: Remove sys$donate()Andreas Kling
This was an old SerenityOS-specific syscall for donating the remainder of the calling thread's time-slice to another thread within the same process. Now that Threading::Lock uses a pthread_mutex_t internally, we no longer need this syscall, which allows us to get rid of a surprising amount of unnecessary scheduler logic. :^)
2021-07-05Kernel: Print region name+offset for user addresses in thread backtraceTom
This provides more crucial information to be able to do an addr2line lookup on a backtrace captured with Thread::backtrace. Also change the offset to hexadecimal as this is what is require for addr2line.
2021-07-03Userland: Respect red-zone for signal handlersHediadyoin1
We were building with red-zone before, but were not accounting for it on signal handler entries. This should fix that. Also shorten the stack alignment calculations for this.
2021-07-02Kernel: Use the GS segment for the per-CPU structGunnar Beutner
Right now we're using the FS segment for our per-CPU struct. On x86_64 there's an instruction to switch between a kernel and usermode GS segment (swapgs) which we could use. This patch doesn't update the rest of the code to use swapgs but it prepares for that by using the GS segment instead of the FS segment.
2021-07-02Kernel/ProcFS: Clean dead processes properlyLiav A
Now we use WeakPtrs to break Ref-counting cycle. Also, we call the prepare_for_deletion method to ensure deleted objects are ready for deletion. This is necessary to ensure we don't keep dead processes, which would become zombies. In addition to that, add some debug prints to aid debug in the future.
2021-07-01Kernel: Handle OOM when allocating Thread FPUStateBrian Gianforcaro
Move FPUState allocation to Thread::try_create so that allocation failure can be observed properly by the caller.
2021-06-29Kernel: Introduce the new ProcFS designLiav A
The new ProcFS design consists of two main parts: 1. The representative ProcFS class, which is derived from the FS class. The ProcFS and its inodes are much more lean - merely 3 classes to represent the common type of inodes - regular files, symbolic links and directories. They're backed by a ProcFSExposedComponent object, which is responsible for the functional operation behind the scenes. 2. The backend of the ProcFS - the ProcFSComponentsRegistrar class and all derived classes from the ProcFSExposedComponent class. These together form the entire backend and handle all the functions you can expect from the ProcFS. The ProcFSExposedComponent derived classes split to 3 types in the manner of lifetime in the kernel: 1. Persistent objects - this category includes all basic objects, like the root folder, /proc/bus folder, main blob files in the root folders, etc. These objects are persistent and cannot die ever. 2. Semi-persistent objects - this category includes all PID folders, and subdirectories to the PID folders. It also includes exposed objects like the unveil JSON'ed blob. These object are persistent as long as the the responsible process they represent is still alive. 3. Dynamic objects - this category includes files in the subdirectories of a PID folder, like /proc/PID/fd/* or /proc/PID/stacks/*. Essentially, these objects are always created dynamically and when no longer in need after being used, they're deallocated. Nevertheless, the new allocated backend objects and inodes try to use the same InodeIndex if possible - this might change only when a thread dies and a new thread is born with a new thread stack, or when a file descriptor is closed and a new one within the same file descriptor number is opened. This is needed to actually be able to do something useful with these objects. The new design assures that many ProcFS instances can be used at once, with one backend for usage for all instances.
2021-06-29Kernel: Implement signal handling for x86_64Gunnar Beutner
2021-06-27Kernel: Implement initializing threads on x86_64Gunnar Beutner
2021-06-27Kernel: Rename Thread::tss to Thread::regs and add x86_64 supportGunnar Beutner
We're using software context switches so calling this struct tss is somewhat misleading.
2021-06-27Kernel+Userland: Add x86_64 registers to RegisterState/PtraceRegistersGunnar Beutner
2021-06-24Everywhere: Use nothrow new with `adopt_{ref,own}_if_nonnull`Daniel Bertalan
This commit converts naked `new`s to `AK::try_make` and `AK::try_create` wherever possible. If the called constructor is private, this can not be done, so we instead now use the standard-defined and compiler-agnostic `new (nothrow)`.