Commit graph

347 commits

Author SHA1 Message Date
kleines Filmröllchen 6dded99777 Kernel+LibC: Report correct scheduling priority limits
The priority range was changed several years ago, but the
userland-reported limits were just forgotten :skeleyak:. Move the thread
priority constants into an API header so that userland can use it
properly.
2022-10-27 11:30:19 +01:00
Tim Schumacher 2bf5052608 Kernel: Show more (b)locking info when dumping the process list 2022-08-26 13:07:07 +02:00
Timon Kruiper e8aff0c1c8 Kernel: Use InterruptsState in Spinlock code
This commit updates the lock function from Spinlock and
RecursiveSpinlock to return the InterruptsState of the processor,
instead of the processor flags. The unlock functions would only look at
the interrupt flag of the processor flags, so we now use the
InterruptsState enum to clarify the intent, and such that we can use the
same Spinlock code for the aarch64 build.

To not break the build, all the call sites are updated aswell.
2022-08-26 12:51:57 +02:00
Andreas Kling 0dd88fd836 Kernel: Remove unnecessary forward declaration of s_mm_lock 2022-08-24 14:57:51 +02:00
Andreas Kling 11eee67b85 Kernel: Make self-contained locking smart pointers their own classes
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-20 17:20:43 +02:00
kleines Filmröllchen 4314c25cf2 Kernel: Require lock rank for Spinlock construction
All users which relied on the default constructor use a None lock rank
for now. This will make it easier to in the future remove LockRank and
actually annotate the ranks by searching for None.
2022-08-19 20:26:47 -07:00
Andreas Kling 806ade1367 Kernel: Don't lock scheduler while updating thread scheduling times
We can use simple atomic variables with relaxed ordering for this,
and avoid locking altogether.
2022-08-19 14:05:57 +02:00
Andreas Kling e7d2696a56 Kernel: Shrink default userspace stack size from 4 MiB to 1 MiB
This knocks 70 MiB off our idle footprint, (from 350 MiB to 280 MiB.)
2022-08-15 17:18:11 +02:00
Idan Horowitz 3a80b25ed6 Kernel: Support F_SETLKW in fcntl 2022-07-21 16:39:22 +02:00
Idan Horowitz 08e88bfcad Kernel: Remove unused WriteNotOpen File BlockFlag 2022-07-10 14:24:34 +02:00
Idan Horowitz 275e5cdb64 Kernel: Report POLLNVAL events in sys$poll instead of returning EBADF
As required by Dr. Posix.
2022-07-10 14:24:34 +02:00
Tim Schumacher edbffb3c7a Kernel: Unblock SignalBlocker if a signal was just unmarked as pending
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-07-08 22:27:38 +00:00
Tim Schumacher cf7a0821b5 Kernel: Increase the default userspace stack size to 4 MiB
This makes the main thread stack size the same as the default stack
size when creating new threads.
2022-04-12 16:48:15 +02:00
Jelle Raaijmakers 7826729ab2 Kernel: Track big lock blocked threads in separate list
When we lock a mutex, eventually `Thread::block` is invoked which could
in turn invoke `Process::big_lock().restore_exclusive_lock()`. This
would then try to add the current thread to a different blocked thread
list then the one in use for the original mutex being locked, and
because it's an intrusive list, the thread is removed from its original
list during the `.append()`. When the original mutex eventually
unblocks, we no longer have the thread in the intrusive blocked threads
list and we panic.

Solve this by making the big lock mutex special and giving it its own
blocked thread list. Because the process big lock is temporary and is
being actively removed from e.g. syscalls, it's a matter of time before
we can also remove the fix introduced by this commit.

Fixes issue #9401.
2022-04-06 18:27:19 +02:00
Idan Horowitz 086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Ali Mohammad Pur e14e919b78 Kernel: Fill some siginfo and ucontext fields on SA_SIGINFO
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-04 20:07:05 +01:00
Ali Mohammad Pur cf63447044 Kernel: Move signal handlers from being thread state to process state
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-03-04 20:07:05 +01:00
Andreas Kling b0e5406ae2 Kernel: Update terminology around Thread's "blocking mutex"
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-30 16:21:59 +01:00
Andreas Kling dca5fe69eb Kernel: Make Thread::State an enum class and use it consistently
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-30 16:21:59 +01:00
Andreas Kling 677da0288c Kernel: Don't dispatch signals in Processor::enter_current()
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-30 16:21:59 +01:00
Andreas Kling 3845c90e08 Kernel: Remove unnecessary includes from Thread.h
...and deal with the fallout by adding missing includes everywhere.
2022-01-30 16:21:59 +01:00
Andreas Kling f469fb47b8 Kernel: Move Thread::block<BlockerType>() out of the Thread.h header
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-30 16:21:59 +01:00
Idan Horowitz 7e9df6ddba Kernel: Remove always-false Thread::drop_thread_count boolean parameter 2022-01-26 22:05:34 +00:00
Idan Horowitz 9a7cd8fef8 Kernel: Make Thread::backtrace() fallible using KString 2022-01-12 16:09:09 +02:00
kleines Filmröllchen e2c9578390 Kernel: Allow preventing kmalloc and kfree
For "destructive" disallowance of allocations throughout the system,
Thread gains a member that controls whether allocations are currently
allowed or not. kmalloc checks this member on both allocations and
deallocations (with the exception of early boot) and panics the kernel
if allocations are disabled. This will allow for critical sections that
can't be allowed to allocate to fail-fast, making for easier debugging.

PS: My first proper Kernel commit :^)
2022-01-11 00:08:58 +01:00
Idan Horowitz 91b80da4b4 Kernel: Make Thread::m_kernel_stack_region a NonnullOwnPtr
This OwnPtr is always non-null, so let's enforce it statically.
2022-01-04 19:08:07 +02:00
Brian Gianforcaro 018dc4bb5c Kernel: Add verification promise violations are propagated properly
This change adds a thread member variable to track if we have a pending
promise violation on a kernel thread. This ensures that all code
properly propagates promise violations up to the syscall handler.

Suggested-by: Andreas Kling <kling@serenityos.org>
2021-12-29 18:08:15 +01:00
Idan Horowitz be91b4fe3e Kernel: Support Mutex Protected lists in ListedRefCounted
This will allow us to support Mutex Protected lists like the custodies
list as well.
2021-12-29 12:04:15 +01:00
Andreas Kling 601a9321d9 Kernel: Don't honor userspace SIGSTOP requests in Thread::block()
Instead, wait until we transition back to userspace. This stops
userspace from being able to suspend a thread indefinitely while it's
running in kernelspace (potentially holding some blocking mutex.)
2021-12-23 00:57:36 +01:00
Idan Horowitz 762e047ec9 Kernel+LibC: Implement sigtimedwait()
This includes a new Thread::Blocker called SignalBlocker which blocks
until a signal of a matching type is pending. The current Blocker
implementation in the Kernel is very complicated, but cleaning it up is
a different yak for a different day.
2021-12-12 08:34:19 +02:00
Idan Horowitz 81a76a30a1 Kernel: Preserve pending signals across execve(2)s
As required by posix. Also rename Thread::clear_signals to
Thread::reset_signals_for_exec since it doesn't actually clear any
pending signals, but rather does execve related signal book-keeping.
2021-12-12 08:34:19 +02:00
Idan Horowitz 265764ff2f Kernel: Add support for the POLLWRBAND poll event 2021-12-05 12:53:29 +01:00
Idan Horowitz 40f64d7379 Kernel: Dispatch handle-able signals instead of crashing if possible
This matches the behaviour of the other *nixs and allows processes to
try and recover from such signals in userland.
2021-12-01 21:44:11 +02:00
Idan Horowitz f415218afe Kernel+LibC: Implement sigaltstack()
This is required for compiling wine for serenity
2021-12-01 21:44:11 +02:00
Itamar 38ddf301f6 Kernel+LibC: Fix ptrace for 64-bit
This makes the types used in the PT_PEEK and PT_POKE actions
suitable for 64-bit platforms as well.
2021-11-20 21:22:24 +00:00
Andreas Kling 216e21a1fa AK: Convert AK::Format formatting helpers to returning ErrorOr<void>
This isn't a complete conversion to ErrorOr<void>, but a good chunk.
The end goal here is to propagate buffer allocation failures to the
caller, and allow the use of TRY() with formatting functions.
2021-11-17 00:21:13 +01:00
Andrew Kaster b1d5d3cc34 Kernel: Avoid redundant bool comparisons in Kernel::Thread
Two instances of comparing a bool with == true or == false, and one
instance where we can just return an expression instead of checking it
to return true on succeess and false on failure.
2021-11-14 22:52:35 +01:00
Andrew Kaster a92132e44a Kernel: Resolve clang-tidy readability-implicit-bool-conversion warnings
... In files included from Kernel/Process.cpp and Kernel/Thread.cpp
2021-11-14 22:52:35 +01:00
Andreas Kling 79fa9765ca Kernel: Replace KResult and KResultOr<T> with Error and ErrorOr<T>
We now use AK::Error and AK::ErrorOr<T> in both kernel and userspace!
This was a slightly tedious refactoring that took a long time, so it's
not unlikely that some bugs crept in.

Nevertheless, it does pass basic functionality testing, and it's just
real nice to finally see the same pattern in all contexts. :^)
2021-11-08 01:10:53 +01:00
James Mintram baa7925c0c Kernel: Remove unused includes 2021-10-14 01:23:08 +01:00
Ali Mohammad Pur 5a0cdb15b0 AK+Everywhere: Reduce the number of template parameters of IntrusiveList
This makes the user-facing type only take the node member pointer, and
lets the compiler figure out the other needed types from that.
2021-09-10 18:05:46 +03:00
Andreas Kling 4a9c18afb9 Kernel: Rename FileDescription => OpenFileDescription
Dr. POSIX really calls these "open file description", not just
"file description", so let's call them exactly that. :^)
2021-09-07 13:53:14 +02:00
Brian Gianforcaro 88c27fc38e Kernel: Specify a lock rank for Thread::m_lock 2021-09-07 13:16:01 +02:00
Brian Gianforcaro 066b0590ec Kernel/Locking: Add lock rank tracking per thread to find deadlocks
This change adds a static lock hierarchy / ranking to the Kernel with
the goal of reducing / finding deadlocks when running with SMP enabled.

We have seen quite a few lock ordering deadlocks (locks taken in a
different order, on two different code paths). As we properly annotate
locks in the system, then these facilities will find these locking
protocol violations automatically

The `LockRank` enum documents the various locks in the system and their
rank. The implementation guarantees that a thread holding one or more
locks of a lower rank cannot acquire an additional lock with rank that
is greater or equal to any of the currently held locks.
2021-09-07 13:16:01 +02:00
Brian Gianforcaro 0718afa773 Kernel: Track when a thread is in the middle of crashing
There are certain checks that we should skip if the system is crashing.
The system can avoid stack overflow during crash, or even triple
faulting while while handling issues that can causes recursive panics
or aborts.
2021-09-07 13:16:01 +02:00
Andreas Kling 7981422500 Kernel: Make Threads always have a name
We previously allowed Thread to exist in a state where its m_name was
null, and had to work around that in various places.

This patch removes that possibility and forces those who would create a
thread (or change the name of one) to provide a NonnullOwnPtr<KString>
with the name.
2021-09-06 13:06:05 +02:00
Brian Gianforcaro bb58a4d943 Kernel: Make all Spinlocks use u8 for storage, remove template
The default template argument is only used in one place, and it
looks like it was probably just an oversight. The rest of the Kernel
code all uses u8 as the type. So lets make that the default and remove
the unused template argument, as there doesn't seem to be a reason to
allow the size to be customizable.
2021-09-05 20:46:02 +02:00
sin-ack 566c5d1e99 AK+Kernel: Move KResult.h to Kernel/API for userspace access
This commit moves the KResult and KResultOr objects to Kernel/API to
signify that they may now be freely used by userspace code at points
where a syscall-related error result is to be expected. It also exposes
KResult and KResultOr to the global namespace to make it nicer to use
for userspace code.
2021-09-05 12:54:48 +02:00
Brian Gianforcaro 9d1b27263f Kernel: Declare type aliases with "using" instead of "typedef"
This is the idiomatic way to declare type aliases in modern C++.
Flagged by Sonar Cloud as a "Code Smell", but I happen to agree
with this particular one. :^)
2021-09-05 09:48:43 +01:00
Andreas Kling e851a77346 Kernel: Rename FileBlocker::unblock() => unblock_if_conditions_are_met()
Since this may not actually unblock, the old name was very confusing.
2021-09-05 01:10:56 +02:00