Commit Graph

5481 Commits

Author SHA1 Message Date
brapru
6743170f4e Kernel: Clear SO_ERROR on successful socket connection
When TCP sockets successfully establish a connection, any SO_ERROR
should be cleared back to success. For example, SO_ERROR gets set to
EINPROGRESS on asynchronous connect calls and should be cleared when
the socket moves to the Established state.
2021-08-13 20:30:19 +04:30
brapru
1a5b3a1c3f Kernel/ProcFS: Expose TCP/UDP Socket pids
This allows for commands like netstat to reference /proc/net and
identify a connection's owning process. Process information is limited
to superusers and user owned processes.
2021-08-13 20:03:57 +04:30
Brian Gianforcaro
296452a981 Kernel: Make cloning of FileDescriptions OOM safe 2021-08-13 11:09:25 +02:00
Brian Gianforcaro
e7fb70b05c Kernel: Allow kmalloc(..) / kmalloc_aligned(..) to return nullptr
Now that we have a significant amount of code paths handling OOM, lets
enable kmalloc and friends to actually return nullptr. This way we can
start stressing these paths and validating all of they work as expected.
2021-08-13 11:09:25 +02:00
Brian Gianforcaro
ed6d842f85 Kernel: Fix OOB read in sys$dbgputstr(..) during fuzzing
The implementation uses try_copy_kstring_from_user to allocate a kernel
string using, but does not use the length of the resulting string.
The size parameter to the syscall is untrusted, as try copy kstring will
attempt to perform a `safe_strlen(..)` on the user mode string and use
that value for the allocated length of the KString instead. The bug is
that we are printing the kstring, but with the usermode size argument.

During fuzzing this resulted in us walking off the end of the allocated
KString buffer printing garbage (or any kernel data!), until we stumbled
in to the KSym region and hit a fatal page fault.

This is technically a kernel information disclosure, but (un)fortunately
the disclosure only happens to the Bochs debug port, and or the serial
port if serial debugging is enabled. As far as I can tell it's not
actually possible for an untrusted attacker to use this to do something
nefarious, as they would need access to the host. If they have host
access then they can already do much worse things :^).
2021-08-13 11:08:11 +02:00
Brian Gianforcaro
40a942d28b Kernel: Remove char* versions of path argument / kstring copy methods
The only two paths for copying strings in the kernel should be going
through the existing Userspace<char const*>, or StringArgument methods.

Lets enforce this by removing the option for using the raw cstring APIs
that were previously available.
2021-08-13 11:08:11 +02:00
Brian Gianforcaro
5121e58d4a Kernel: Fix sys$dbgputstr(...) to take a char* instead of u8*
We always attempt to print this as a string, and it's defined as such in
LibC, so fix the signature to match.
2021-08-13 11:08:11 +02:00
Brian Gianforcaro
1ee1ef5103 Kernel: Introduce a StringView overload of dbgputstr(..) 2021-08-13 11:08:11 +02:00
Brian Gianforcaro
060503ef25 Kernel: Annotate KString methods as [[nodiscard]] 2021-08-13 11:08:11 +02:00
Liav A
18eb262157 Kernel: Move VirtIO code into the Bus source folder
The VirtIO code handles functionality related to the VirtIO bus, so it
really should be in the Bus folder.
2021-08-13 08:06:47 +02:00
Liav A
7ba991dc37 Kernel: Steer away from heap allocations for ProcFS process data
Instead, use more static patterns to acquire that sort of data.
2021-08-12 20:57:32 +02:00
Liav A
bf1adc2d5d Kernel+LibC: Use 64 bit values for ino_t
Since the InodeIndex encapsulates a 64 bit value, it is correct to
ensure that the Kernel is exposing the entire value and the LibC is
aware of it.

This commit requires an entire re-compile because it's essentially a
change in the Kernel ABI, together with a corresponding change in LibC.
2021-08-12 20:57:32 +02:00
Liav A
04c2addaa8 Kernel: Fail process creating earlier if can't create AddressSpace
It makes more sense to fail the Process creation earlier if we can't
create an AddressSpace for the new Process.
2021-08-12 20:57:32 +02:00
Liav A
01b79910b3 Kernel/Process: Move protected values to the end of the object
The compiler can re-order the structure (class) members if that's
necessary, so if we make Process to inherit from ProcFSExposedComponent,
even if the declaration is to inherit first from ProcessBase, then from
ProcFSExposedComponent and last from Weakable<Process>, the members of
class ProcFSExposedComponent (including the Ref-counted parts) are the
first members of the Process class.

This problem made it impossible to safely use the current toggling
method with the write-protection bit on the ProcessBase members, so
instead of inheriting from it, we make its members the last ones in the
Process class so we can safely locate and modify the corresponding page
write protection bit of these values.

We make sure that the Process class doesn't expand beyond 8192 bytes and
the protected values are always aligned on a page boundary.
2021-08-12 20:57:32 +02:00
Andreas Kling
a29788e58a Kernel: Don't record sys$perf_event() if profiling is not enabled
If you want to record perf events, just enable profiling. This allows us
to add random perf events to programs without littering the file system
with perfcore files.
2021-08-12 00:03:40 +02:00
Andreas Kling
1e90a3a542 Kernel: Make sys$perf_register_string() generate the string ID's
Making userspace provide a global string ID was silly, and made the API
extremely difficult to use correctly in a global profiling context.

Instead, simply make the kernel do the string ID allocation for us.
This also allows us to convert the string storage to a Vector in the
kernel (and an array in the JSON profile data.)
2021-08-12 00:03:39 +02:00
Andreas Kling
4657c79143 Kernel+LibC: Add sys$perf_register_string()
This syscall allows userspace to register a keyed string that appears in
a new "strings" JSON object in profile output.

This will be used to add custom strings to profile signposts. :^)
2021-08-12 00:03:39 +02:00
Andreas Kling
0d997d48ea Kernel+LibC: Add PERF_EVENT_SIGNPOST
This event will be used by userspace programs wanting to mark
interesting high-level events in the profile. :^)
2021-08-12 00:03:38 +02:00
Andreas Kling
7f50805903 Kernel/SMP: Fix RecursiveSpinLock remembering the wrong CPU when locking
We have to disable interrupts before capturing the current Processor*,
or we risk storing the wrong one if we get preempted and resume on a
different CPU.

Caught by the VERIFY in RecursiveSpinLock::unlock()
2021-08-11 12:34:55 +02:00
Andreas Kling
5e27861c2e Kernel: Don't ask RTC for current time when generating coredumps
Use kgettimeofday() like everybody else. :^)
2021-08-11 12:34:55 +02:00
Gunnar Beutner
9964c106ad Kernel: Disambiguate instruction size for mov in read_gs_ptr
Previously we allowed using immediate values here which is ambiguous
as to which specific mov instruction the compiler should choose.
2021-08-11 12:34:47 +02:00
Timothy Flynn
c16aca7abf AK+Kernel: Add StringBuilder::append overload for UTF-16 views
Currently, to append a UTF-16 view to a StringBuilder, callers must
first convert the view to UTF-8 and then append the copy. Add a UTF-16
overload so callers do not need to hold an entire copy in memory.
2021-08-10 23:07:50 +02:00
Gunnar Beutner
3322efd4cd Kernel: Fix kernel panic when blocking on the process' big lock
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-10 22:33:50 +02:00
LuK1337
e1ed1d9176 VirtualFileSystem: Return early in rename() when old_path==new_path
This change fixes disappearing symlink after trying to do the following
thing:
  cp /res/icons/16x16/add-event.png .
  ln -s add-event.png a
  mv a a
2021-08-10 21:56:10 +02:00
Jean-Baptiste Boric
0286160b62 Kernel: Add syscall performance event type
This allows tracing the syscalls made by a thread through the kernel's
performance event framework, which is similar in principle to strace.

Currently, this merely logs a stack backtrace to the current thread's
performance event buffer whenever a syscall is made, if profiling is
enabled. Future improvements could include tracing the arguments and
the return value, for example.
2021-08-10 21:55:48 +02:00
Jean-Baptiste Boric
4c4b8ea443 Kernel: Properly handle non-blocking I/O on pipes
Previously, non-blocking read operations on pipes returned EOF instead
of EAGAIN and non-blocking write operations blocked. This fixes
pkgsrc's bmake "eof on job pipe!" error message when running in
non-compatibility mode.
2021-08-10 21:55:35 +02:00
Andreas Kling
b7dae4f90e Kernel: Add CLOCK_MONOTONIC_COARSE to the kernel time page
This allows clock_gettime(CLOCK_MONOTONIC_COARSE) without syscalls.
Core::EventLoop takes advantage of this automatically. :^)
2021-08-10 21:51:05 +02:00
Andreas Kling
aaead6f332 Kernel: Only expose CLOCK_REALTIME_COARSE via the kernel time page
Non-COARSE clock sources may probably still require a syscall.
2021-08-10 21:51:05 +02:00
Andreas Kling
11456ebc00 Kernel: Close race window in timestamp update mechanism
As pointed out by 8infy, this mechanism is racy:

    WRITER:

        1. ++update1;
        2. write_data();
        3. ++update2;

    READER:

        1. do { auto saved = update1;
        2. read_data();
        3. } while (saved != update2);

The following sequence can lead to a bogus/partial read:

    R1      R2  R3
        W1  W2      W3

We close this race by incrementing the second update counter first:

    WRITER:

        1. ++update2;
        2. write_data();
        3. ++update1;
2021-08-10 21:51:05 +02:00
brapru
342e1f0a84 Kernel: Properly implement SO_ERROR option
This fixes the placeholder stub for the SO_ERROR via getsockopt. It
leverages the m_so_error value that each socket maintains. The SO_ERROR
option obtains and then clears this field, which is useful when checking
for errors that occur between socket calls. This uses an integer value
to return the SO_ERROR status.

Resolves #146
2021-08-10 20:59:53 +02:00
brapru
0095c7cb7d Kernel: Add so_error to keep track of the socket's error state
This sets the m_so_error variable every time the socket returns an
error.
2021-08-10 20:59:53 +02:00
Andreas Kling
fdfc66db61 Kernel+LibC: Allow clock_gettime() to run without syscalls
This patch adds a vDSO-like mechanism for exposing the current time as
an array of per-clock-source timestamps.

LibC's clock_gettime() calls sys$map_time_page() to map the kernel's
"time page" into the process address space (at a random address, ofc.)
This is only done on first call, and from then on the timestamps are
fetched from the time page.

This first patch only adds support for CLOCK_REALTIME, but eventually
we should be able to support all clock sources this way and get rid of
sys$clock_gettime() in the kernel entirely. :^)

Accesses are synchronized using two atomic integers that are incremented
at the start and finish of the kernel's time page update cycle.
2021-08-10 19:21:16 +02:00
Andreas Kling
f02d73db4d LibC+Kernel: Use an enum for clockid_t values 2021-08-10 13:01:39 +02:00
Andreas Kling
595ed59eb7 Kernel: Alphabetize the syscall list 2021-08-10 13:01:39 +02:00
Andreas Kling
fa64ab26a4 Kernel+UserspaceEmulator: Remove unused sys$gettimeofday()
Now that LibC uses clock_gettime() to implement gettimeofday(), we can
get rid of this entire syscall. :^)
2021-08-10 13:01:39 +02:00
Andreas Kling
0a02496f04 Kernel/SMP: Change critical sections to not disable interrupts
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-10 02:49:37 +02:00
Andreas Kling
364134ad4b Kernel/SMP: Skip thread registers in core dump if there is no trap frame
We can only get thread registers if there's a trap frame.
2021-08-10 02:49:37 +02:00
Andreas Kling
9babb92a4b Kernel/SMP: Make entering/leaving critical sections multi-processor safe
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-10 02:49:37 +02:00
Liav A
d16d116b65 Kernel/USB: Tighten up USBManagement enumeration code a bit 2021-08-09 22:52:09 +02:00
Luke
7a86a8df90 Kernel/USB: Create controller base class and introduce USBManagement
This removes Pipes dependency on the UHCIController by introducing a
controller base class. This will be used to implement other controllers
such as OHCI.

Additionally, there can be multiple instances of a UHCI controller.
For example, multiple UHCI instances can be required for systems with
EHCI controllers. EHCI relies on using multiple of either UHCI or OHCI
controllers to drive USB 1.x devices.

This means UHCIController can no longer be a singleton. Multiple
instances of it can now be created and passed to the device and then to
the pipe.

To handle finding and creating these instances, USBManagement has been
introduced. It has the same pattern as the other management classes
such as NetworkManagement.
2021-08-09 21:05:25 +02:00
Jesse Buhagiar
c5974d3518 USB: Fix wrong port read/write in portscan daemon
Port2 logic was errantly using `portsc1` registers, meaning that
the port wouldn't be reset properly. In effect, this puts devices
connected to Port2 in an undefined state.
2021-08-09 21:04:55 +02:00
Andreas Kling
74e6a70958 Kernel/SMP: Don't process SMP messages in non-SMP mode
Processing SMP messages outside of non-SMP mode is a waste of time,
and now that we don't rely on the side effects of calling the message
processing function, let's stop calling it entirely. :^)
2021-08-09 13:39:08 +02:00
Andreas Kling
a971de89d3 Kernel/SMP: Process the deferred call queue in exit_trap()
We were previously relying on a side effect of the critical section in
smp_process_pending_messages(): when exiting that section, it would
process any pending deferred calls.

Instead of relying on that, make the deferred invocations explicit by
calling deferred_call_execute_pending() in exit_trap().

This ensures that deferred calls get processed before entering the
scheduler at the end of exit_trap(). Since thread unblocking happens
via deferred calls, the threads don't have to wait until the next
scheduling opportunity when they could be ready *now*. :^)

This was the main reason Tom's SMP branch ran slowly in non-SMP mode.
2021-08-09 13:35:52 +02:00
Andreas Kling
57a7dfbd28 Kernel/SMP: Don't process SMP messages in exit_trap() in non-SMP mode 2021-08-09 13:23:42 +02:00
Andreas Kling
f27e7bbbf4 Kernel/SMP: Don't enable interrupts in Processor::exit_trap
Enter a critical section in Processor::exit_trap so that processing
SMP messages doesn't enable interrupts upon leaving. We need to delay
this until the end where we call into the Scheduler if exiting the
trap results in being outside of a critical section and irq handler.

Co-authored-by: Tom <tomut@yahoo.com>
2021-08-09 13:22:22 +02:00
Andreas Kling
cd0fc7f52c Kernel/SMP: Mark s_smp_enabled READONLY_AFTER_INIT
We can't enter/leave SMP mode once the kernel is up and running.
2021-08-09 13:19:26 +02:00
Andreas Kling
f357fd91da Kernel: Don't hog MM lock in Memory::Region::clone()
We don't need to hold the MM lock across all of Region::clone().
2021-08-09 11:46:31 +02:00
Andreas Kling
08b4d8f0de Kernel: Fix deadlock in ~Memory::Region()
First off: unregister the region from MemoryManager before unmapping it.
The order of operations here was a bit strange, presumably to avoid a
situation where a fault would happen while unmapping, and the fault
handler would find the MemoryManager region list in an invalid state.

Unregistering it before unmapping sidesteps the whole problem, and
allows us to easily fix another problem: a deadlock could occur due
to inconsistent acquisition order (PageDirectory must come before MM.)
2021-08-09 11:46:31 +02:00
Andreas Kling
95c8e421ae Kernel: Don't hog MM lock in find_region_from_vaddr()
We don't want to be holding the MM lock if it's a user region and we
have to consult the page directory, since that can lead to a deadlock
if we don't already have the page directory lock.
2021-08-09 11:46:31 +02:00
Andreas Kling
96c7b70de3 Kernel: Remove unused list of user regions in MemoryManager
We were putting all the user Region objects on an intrusive list
but not using it for anything.
2021-08-09 11:46:31 +02:00
Andreas Kling
ab5c422a29 Kernel/SMP: Make SMP message queueing work correctly
- Use the receiver's per-CPU entry in the message, instead of the
  sender's. (Using the sender's entry wasn't safe for broadcast
  messages since the same entry ended up on multiple message queues.)

- Retry the CAS until it *succeeds* instead of *fails*. This closes a
  race window, and also ensures a correct return value. The return value
  is used by the caller to decide whether to broadcast an IPI.
  This was the main reason smp=on was so slow. We had CPUs busy-waiting
  until someone else triggered an IPI and moved things along.

- Add a CPU pause hint to the spin loop. :^)
2021-08-09 11:46:31 +02:00
Andreas Kling
6283acb6b7 Kernel/SMP: Don't panic in inode fault if other CPU already paged it in
It may happen that CPU A manages to page in from the same inode
while we're just entering the same page fault handler on CPU B.

Handle it gracefully by checking if the data has already been paged in
(instead of VERIFY'ing that it hasn't) and then remap the page if that's
the case.
2021-08-09 11:46:31 +02:00
Andreas Kling
00bbbdeda6 Kernel/SMP: Always take PageDirectory lock before the MemoryManager lock
This prevents deadlocking due to inconsistent acquisition order.
2021-08-09 11:46:31 +02:00
Andreas Kling
d21b8f9013 Kernel/SMP: Fix ProcessorMessage deallocation bug
Due to a boolean mistake in smp_return_to_pool(), we didn't retry
pushing the message onto the freelist after a failed attempt.

This caused the message pool to eventually become completely empty
after enough contentious access attempts.

This patch also adds a pause hint to the CPU in the failed attempt
code path.
2021-08-09 11:46:30 +02:00
Andreas Kling
f3fed411d4 Kernel: Rename Processor::smp_queue_message() => smp_enqueue_message() 2021-08-09 11:46:30 +02:00
Andreas Kling
46215a8183 Kernel: Add Processor::pause() and use it to give the CPU a rest
On x86, the "pause" instruction is a "spin loop hint".
2021-08-09 11:46:30 +02:00
Andreas Kling
b7129c57df Kernel: Only get register dump when we have a trap
Co-authored-by: Tom <tomut@yahoo.com>
2021-08-09 11:46:30 +02:00
Andreas Kling
cebdb0b575 Kernel: Fix logic typo in ConsoleManagement::is_initialized()
Regressed in 2ff3c54153.
2021-08-09 11:46:30 +02:00
Brian Gianforcaro
8e93815846 Kernel: Fix panic loop when encountering an unknown boot_mode
The kernel panic handler now parses the kernels boot_mode to decide how
to handle the panic. So the previous logic could end up in an panic loop
until we blew out the kernel stack.

Instead only validate the kernel's boot mode once per boot, after
initializing the kernel command line.
2021-08-08 23:11:50 +02:00
Gunnar Beutner
aabbfa78e2 DynamicLoader: Make sure we don't link against libgcc_s
This bug was reintroduced by the removal of -fbuilding-gcc.
2021-08-08 16:41:51 +02:00
Andreas Kling
2be368e4c4 Kernel: Rename queue_runnable_thread() => enqueue_runnable_thread() 2021-08-08 14:24:55 +02:00
Andreas Kling
0910979dec Kernel: Remove unused Process::FileDescriptions::fd_flags() 2021-08-08 14:24:55 +02:00
Andreas Kling
9ccc77092d Kernel: Remove unused cruft from FileDescriptionAndFlags 2021-08-08 14:24:55 +02:00
Andreas Kling
d959b5d9dd Kernel: Remove unused template variant of create_kernel_thread() 2021-08-08 14:24:55 +02:00
Andreas Kling
db14092d8f Kernel: Remove unused Process::all_pids() 2021-08-08 14:24:54 +02:00
Andreas Kling
374972578d Kernel: Port the scheduler's time tracking to SpinLockProtectedValue 2021-08-08 14:24:54 +02:00
Andreas Kling
a425f421ac Kernel: Port the scheduler's ready queues to SpinLockProtectedValue 2021-08-08 14:24:54 +02:00
Andreas Kling
a1c82041a6 Kernel: Simplify the per-CPU SchedulerData struct 2021-08-08 14:24:54 +02:00
Andreas Kling
5712e46ddc Kernel: Remove unused "VGA font" memory region in GraphicsManagement 2021-08-08 14:24:54 +02:00
Daniel Bertalan
fa8507d1ce Kernel: Fix UB caused by taking a reference to a packed struct's member
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-08 10:55:36 +02:00
Daniel Bertalan
13e3df41de Meta: Add Clang support to the CMake build scripts 2021-08-08 10:55:36 +02:00
Daniel Bertalan
5d617be462 Kernel: Bump eternal kmalloc range to 4 MiB
Kernels built with Clang seem to be quite allocation-heavy compared to
their GCC counterparts. We would sometimes end up crashing during boot
because the eternal ranges had no free capacity.
2021-08-08 10:55:36 +02:00
Daniel Bertalan
347c3361ee Kernel: Fix deprecated array comparison
The Clang error message reads like this (`-Wdeprecated-array-compare`):

> error: comparison between two arrays is deprecated; to compare
> array addresses, use unary '+' to decay operands to pointers.
2021-08-08 10:55:36 +02:00
Andreas Kling
2ff3c54153 Kernel: Remove unnecessary churn in ConsoleManagement
The maximum number of virtual consoles is determined at compile time,
so we can pre-allocate that many slots, dodging some heap allocations.

Furthermore, virtual consoles are never destroyed, so it's fine to
simply store a raw pointer to the currently active one.
2021-08-08 00:44:25 +02:00
Andreas Kling
552dd7abd3 Kernel: Port BlockBasedFileSystem to ProtectedValue :^) 2021-08-08 00:28:10 +02:00
Andreas Kling
e2bfdd74bc Kernel: Remove unused mutex PATADiskDevice::m_lock 2021-08-08 00:08:24 +02:00
Andreas Kling
c94c15d45c Everywhere: Replace AK::Singleton => Singleton 2021-08-08 00:03:45 +02:00
Andreas Kling
15d033b486 Kernel: Remove unused Process pointer in Memory::AddressSpace
Nobody was using the back-pointer to the process, so let's lose it.
2021-08-08 00:03:45 +02:00
Andreas Kling
0cb6c3c831 Kernel/TCP: Port TCP retransmit queue to ProtectedValue
I had to switch to exclusive locking since ProtectedValue rightly
doesn't allow you to mutate protected data with only a shared lock.
2021-08-07 18:49:27 +02:00
Andreas Kling
4c582b57e9 Kernel: Port PTYMultiplexer to ProtectedValue 2021-08-07 18:49:27 +02:00
Andreas Kling
7f2791f02e Kernel: Increase maximum PTY count from 8 to 64
Let's allow users to allocate more pseudo-terminals if they want.
2021-08-07 18:49:27 +02:00
Idan Horowitz
9d21c79671 Kernel: Disable big process lock for sys$sync
This syscall doesn't touch any intra-process shared resources and only
calls VirtualFileSystem::sync, which is self-locking.
2021-08-07 15:30:26 +02:00
sin-ack
0d468f2282 Kernel: Implement a ISO 9660 filesystem reader :^)
This commit implements the ISO 9660 filesystem as specified in ECMA 119.
Currently, it only supports the base specification and Joliet or Rock
Ridge support is not present. The filesystem will normalize all
filenames to be lowercase (same as Linux).

The filesystem can be mounted directly from a file. Loop devices are
currently not supported by SerenityOS.

Special thanks to Lubrsi for testing on real hardware and providing
profiling help.

Co-Authored-By: Luke <luke.wilde@live.co.uk>
2021-08-07 15:21:58 +02:00
Andreas Kling
b197fc40a7 Kernel: Port process thread lists to SpinLockProtectedValue
I had to move the thread list out of the protected base area of Process
so that it could live with its lock (which needs to be mutable).
Ideally it would live in the protected area, so maybe we can figure out
a way to do that later.
2021-08-07 13:46:16 +02:00
Andreas Kling
d6667e4cb8 Kernel: Port process groups to SpinLockProtectedValue 2021-08-07 13:30:59 +02:00
Brian Gianforcaro
d496eb48e3 Kernel: Fix boot profiling after big process lock separation regression
When I laid down the foundation for the start of the big process lock
separation, I added asserts to all system call implementations to
validate we hold the big process lock in the locations we think we
should be.

Adding that assert to sys$profiling_enable broke boot time profiling as
we were never holding the lock on boot. Even though it's not technically
required, lets make sure to hold the lock while enabling to appease the
assert.
2021-08-07 12:38:59 +02:00
Andreas Kling
58fb38551c Kernel/TTY: Remove redundant SpinLock from VirtualConsole
VirtualConsole::m_lock was only used in a single place: on_tty_write()
That function is already protected by a global lock, so this second
lock served no purpose whatsoever.
2021-08-07 12:38:14 +02:00
Andreas Kling
5acb7e4eba Kernel: Remove outdated FIXME about ProcessHandle
ProcessHandle hasn't been a thing since Process became ref-counted.
2021-08-07 12:29:26 +02:00
Jean-Baptiste Boric
08891e82a5 Kernel: Migrate process list locking to ProtectedValue
The existing recursive spinlock is repurposed for profiling only, as it
was shared with the process list.
2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
8554b66d09 Kernel: Make process list a singleton 2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
626b99ce1c Kernel: Migrate hostname locking to ProtectedValue 2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
9517100672 Kernel: Migrate UDP socket table locking to ProtectedValue 2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
9216c72bfe Kernel: Migrate TCP socket tables locking to ProtectedValue
Note: TCPSocket::create_client() has a dubious locking process where
the sockets by tuple table is first shared lock to check if the socket
exists and bail out if it does, then unlocks, then exclusively locks to
add the tuple. There could be a race condition where two client
creation requests for the same tuple happen at the same time and both
cleared the shared lock check. When in doubt, lock exclusively the
whole time.
2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
583abc27d8 Kernel: Migrate IPv4 socket table locking to ProtectedValue 2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
edd6c04024 Kernel: Migrate local socket table locking to ProtectedValue 2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
6d83b2d8f0 Kernel: Migrate FIFO table locking to ProtectedValue 2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
25d7beec6b Kernel: Use atomic integer for next FIFO id 2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
738e604bfc Kernel: Migrate ARP table locking to ProtectedValue 2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
75260bff92 Kernel: Introduce ProtectedValue
A protected value is a variable with enforced locking semantics. The
value is protected with a Mutex and can only be accessed through a
Locked object that holds a MutexLocker to said Mutex. Therefore, the
value itself cannot be accessed except through the proper locking
mechanism, which enforces correct locking semantics.

The Locked object has knowledge of shared and exclusive lock types and
will only return const-correct references and pointers. This should
help catch incorrect locking usage where a shared lock is acquired but
the user then modifies the locked value.

This is not a perfect solution because dereferencing the Locked object
returns the value, so the caller could defeat the protected value
semantics once it acquires a lock by keeping a pointer or a reference
to the value around. Then again, this is C++ and we can't protect
against malicious users from within the kernel anyways, but we can
raise the threshold above "didn't pay attention".
2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
39ceefa5dd Kernel: Implement contended, ref-counted resource framework
This is some syntaxic sugar to use a ContendedResource object with
reference counting. This effectively dissociates merely holding a
reference to an object and actually using the object in a way that
requires locking it against concurrent use.
2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
019ad8a507 Kernel: Introduce spin-locked contended and locked resource concepts 2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
3d684316c2 Kernel: Introduce contended and locked resource concepts 2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
aea98a85d1 Kernel: Move Lockable into its own header 2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
2c3b0baf76 Kernel: Move SpinLock.h into Locking/ 2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
f7f794e74a Kernel: Move Mutex into Locking/ 2021-08-07 11:48:00 +02:00
Jean-Baptiste Boric
479b07339c Kernel: Move LockMode into Locking/ 2021-08-07 11:48:00 +02:00
Andreas Kling
f770b9d430 Kernel: Fix bad search-and-replace renames
Oops, I didn't mean to change every *Range* to *VirtualRange*!
2021-08-07 00:39:06 +02:00
Idan Horowitz
ad419a669d Kernel: Disable big process lock for sys$sysconf
This syscall only reads constant kernel globals, and as such does not
need to hold the big lock.
2021-08-06 23:36:12 +02:00
Idan Horowitz
efeb01e35f Kernel: Disable big process lock for sys$get_stack_bounds
This syscall only reads from the shared m_space field, but that field
is only over written to by Process::attach_resources, before the
process was initialized (aka, before syscalls can happen), by
Process::finalize which is only called after all the process' threads
have exited (aka, syscalls can not happen anymore), and by
Process::do_exec which calls all other syscall-capable threads before
doing so. Space's find_region_containing already holds its own lock,
and as such there's no need to hold the big lock.
2021-08-06 23:36:12 +02:00
Idan Horowitz
d40038a04f Kernel: Disable big process lock for sys$gettimeofday
This syscall doesn't touch any intra-process shared resources and only
accesses the time via the atomic TimeManagement::now so there's no need
to hold the big lock.
2021-08-06 23:36:12 +02:00
Idan Horowitz
3ba2449058 Kernel: Disable big process lock for sys$clock_nanosleep
This syscall doesn't touch any intra-process shared resources and only
accesses the time via the atomic TimeManagement::current_time so there's
no need to hold the big lock.
2021-08-06 23:36:12 +02:00
Idan Horowitz
fbd848e6eb Kernel: Disable big process lock for sys$clock_gettime()
This syscall doesn't touch any intra-process shared resources and
reads the time via the atomic TimeManagement::current_time, so it
doesn't need to hold any lock.
2021-08-06 23:36:12 +02:00
Idan Horowitz
1a08694dfc Kernel: Disable big process lock for sys$getkeymap
This syscall only reads non process-related global values, and as such
doesn't need to hold the big lock.
2021-08-06 23:36:12 +02:00
Idan Horowitz
48325e2959 Kernel: Disable big process lock for sys$getrandom
This syscall doesn't touch any intra-process shared resources and
already holds the global kernel RNG lock so there's no reason to hold
the big lock.
2021-08-06 23:36:12 +02:00
Idan Horowitz
b1f4f6ee15 Kernel: Disable big process lock for sys$dbgputch
This syscall doesn't touch any intra-process shared resources and
already holds the global logging lock so there's no reason to hold
the big lock.
2021-08-06 23:36:12 +02:00
Idan Horowitz
72331168be Kernel: Hold the global logging lock in dbgputch
This was not an active issue as sys$dbgputch (the only user of this
function) is not actually used anywhere.
2021-08-06 23:36:12 +02:00
Idan Horowitz
c7ad4c6c32 Kernel: Disable big process lock for sys$dbgputstr
This syscall doesn't touch any intra-process shared resources and
already holds the global logging lock so there's no reason to hold
the big lock.
2021-08-06 23:36:12 +02:00
Idan Horowitz
00818b8447 Kernel: Disable big process lock for sys$dump_backtrace()
This syscall only dumps the current thread's backtrace and as such
doesn't touch any shared intra-process resources.
2021-08-06 23:36:12 +02:00
Idan Horowitz
da0b7d1737 Kernel: Disable big process lock for sys$beep()
The PCSpeaker is global and not locked anyways, so there's no need for
mutual exclusion between threads in the same process.
2021-08-06 23:36:12 +02:00
Idan Horowitz
c3f668a758 Kernel: Make Process's m_promises & m_execpromises fields atomic
This is essentially free on x86 and allows us to not hold the big
process lock just to check the required promises for a syscall.
2021-08-06 23:36:12 +02:00
Andreas Kling
2cd8b21974 Kernel: Add convenience values to the Memory::Region::Access enum
Instead of `Memory::Region::Access::Read | Memory::Region::AccessWrite`
you can now say `Memory::Region::Access::ReadWrite`.
2021-08-06 22:25:00 +02:00
Andreas Kling
47bdd7c3a0 Kernel: Rename a very long enum to ShouldDeallocateVirtualRange
ShouldDeallocateVirtualMemoryVirtualRange was a bit on the long side.
2021-08-06 21:45:05 +02:00
Andreas Kling
cdab5b2091 Kernel: Make identity mapping mechanism used during AP boot non-generic
When booting AP's, we identity map a region at 0x8000 while doing the
initial bringup sequence. This is the only thing in the kernel that
requires an identity mapping, yet we had a bunch of generic API's and a
dedicated VirtualRangeAllocator in every PageDirectory for this purpose.

This patch simplifies the situation by moving the identity mapping logic
to the AP boot code and removing the generic API's.
2021-08-06 21:35:56 +02:00
Andreas Kling
44da58c0b2 Kernel: Move UnveilNode.h into Kernel/FileSystem/ 2021-08-06 14:11:45 +02:00
Andreas Kling
208147c77c Kernel: Rename Process::space() => Process::address_space()
We commonly talk about "a process's address space" so let's nudge the
code towards matching how we talk about it. :^)
2021-08-06 14:05:58 +02:00
Andreas Kling
b7476d7a1b Kernel: Rename Memory::Space => Memory::AddressSpace 2021-08-06 14:05:58 +02:00
Andreas Kling
cd5faf4e42 Kernel: Rename Range => VirtualRange
...and also RangeAllocator => VirtualRangeAllocator.

This clarifies that the ranges we're dealing with are *virtual* memory
ranges and not anything else.
2021-08-06 14:05:58 +02:00
Andreas Kling
93d98d4976 Kernel: Move Kernel/Memory/ code into Kernel::Memory namespace 2021-08-06 14:05:58 +02:00
Andreas Kling
a1d7ebf85a Kernel: Rename Kernel/VM/ to Kernel/Memory/
This directory isn't just about virtual memory, it's about all kinds
of memory management.
2021-08-06 14:05:58 +02:00
Andreas Kling
ad3ae7e0e8 Kernel: Fix handful of remaining "return -EFOO" mistakes
Now that all KResult and KResultOr are used consistently throughout the
kernel, it's no longer necessary to return negative error codes.
However, we were still doing that in some places, so let's fix all those
(bugs) by removing the minuses. :^)
2021-08-06 00:37:47 +02:00
Andreas Kling
3377cc74df Kernel: Use try_copy_kstring_from_user() in sys$mount() 2021-08-06 00:37:47 +02:00
Andreas Kling
33adc3a42d Kernel: Store coredump metadata properties as KStrings
This patch also replaces the HashMap previously used to store coredump
properties with a plain AK::Array.
2021-08-06 00:37:47 +02:00
Andreas Kling
95669fa861 Kernel: Use try_copy_kstring_from_user() in sys$link() 2021-08-06 00:37:47 +02:00
Andreas Kling
5b13af0edd Kernel: Use try_copy_kstring_from_user() in Socket::setsockopt() 2021-08-06 00:37:47 +02:00
Andreas Kling
b96ad76cba Kernel: Use try_copy_kstring_from_user() in IPv4Socket::ioctl() 2021-08-06 00:37:47 +02:00
Andreas Kling
584fa525eb Kernel: Don't make a separate allocation for thread FPU state
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-06 00:37:47 +02:00
Andreas Kling
d5d8fba579 Kernel: Store Thread name as a KString 2021-08-06 00:37:47 +02:00
Andreas Kling
07599b48de Kernel: Make a helper in the Intel graphics driver return StringView 2021-08-06 00:37:47 +02:00
Andreas Kling
066d3281b5 Kernel: Make AsyncDeviceRequest::name() return StringView 2021-08-06 00:37:47 +02:00
Andreas Kling
beeed90a3f Kernel: Remove unused PCI::Access::access_type() 2021-08-06 00:37:47 +02:00
Andreas Kling
f3f0b80b83 Kernel: Make IRQController::model() return StringView 2021-08-06 00:37:47 +02:00
Andreas Kling
f572d96539 Kernel: Make HardwareTimer::model() return StringView 2021-08-06 00:37:47 +02:00
Andreas Kling
32a150f2b4 Kernel: Make Thread::state_string() return StringView 2021-08-06 00:37:47 +02:00
Andreas Kling
af46f2214c Kernel: Make a bunch of "char const* to_string()" return StringView 2021-08-06 00:37:47 +02:00
Andreas Kling
f35108fc31 Kernel: Simplify PageDirectory allocation failure
This patch gets rid of the "valid" bit in PageDirectory since it was
only used to communicate an allocation failure during construction.

We now do all the work in the static factory functions instead of in the
constructor, which allows us to simply return nullptr instead of an
"invalid" PageDirectory.
2021-08-06 00:37:47 +02:00
Andreas Kling
27100126c0 Kernel: Fix logic typo in AnonymousVMObject::handle_cow_fault()
Introduced in dd58d0f650
2021-08-06 00:37:47 +02:00
Idan Horowitz
3e909c0c49 Kernel: Remove double-counting of allocated pages in AnonymousVMObject
When constructing an AnonymousVMObject with the AllocateNow allocation
strategy we accidentally allocated the committed pages directly through
MemoryManager instead of taking them from our m_unused_physical_pages
CommittedPhysicalPageSet, which meant they were counted as allocated in
MemoryManager, but were still counted as unallocated in the PageSet,
who would then try to uncommit them on destruction, resulting in a
failed assertion.

To help prevent similar issues in the future a Badge<T> was added to
MM::allocate_committed_user_physical_page to prevent allocation of
commited pages not via a CommittedPhysicalPageSet.
2021-08-05 20:26:47 +02:00
Andreas Kling
dd58d0f650 Kernel: Uncommit a shared COW page when discovering it was unshared
When we hit a COW fault and discover than no other process is sharing
the physical page, we simply remap it r/w and save ourselves the
trouble. When this happens, we can also give back (uncommit) one of our
shared committed COW pages, since we won't be needing it.

We had this optimization before, but I mistakenly removed it in
50472fd69f since I had misunderstood
it to be the reason for a panic.
2021-08-05 17:41:58 +02:00
Andreas Kling
843d0d0d15 Kernel: Detach AnonymousVMObject from shared COW pages set once emptied
We currently overcommit for COW when forking a process and cloning its
memory regions. Both the parent and child process share a set of.
committed COW pages.

If there's COW sharing across more than two processeses within a lineage
(e.g parent, child & grandchild), it's possible to exhaust these pages.
When the shared set is emptied, the next COW fault in each process must
detach from the shared set and fall back to on demand allocation.

This patch makes sure that we detach from the shared set once we
discover it to be empty (during COW fault handling). This fixes an issue
where we'd try to allocate from an exhausted shared set while building
GNU binutils inside SerenityOS.
2021-08-05 17:41:58 +02:00
Andreas Kling
89a9ae7d0c Kernel: Handle AnonymousVMObject allocation failure when forking
Thanks to all the RAII, AnonymousVMObject::try_clone() can now
gracefully handle allocation failure.
2021-08-05 17:41:58 +02:00