Commit Graph

273 Commits

Author SHA1 Message Date
Idan Horowitz
086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Liav A
7053d7ece3 Kernel: Add a way to print the pseudo name of attached TTY of a process
Contrary to the past, we don't attempt to assume the real name of a TTY
device, but instead, we generate a pseudo name only when needed to do so
which is still OK because we don't break abstraction layer rules and we
still can provide userspace with the required information.
2022-03-26 11:01:49 +01:00
Liav A
b5ef900ccd Kernel: Don't assume paths of TTYs and pseudo terminals anymore
The obsolete ttyname and ptsname syscalls are removed.
LibC doesn't rely on these anymore, and it helps simplifying the Kernel
in many places, so it's an overall an improvement.

In addition to that, /proc/PID/tty node is removed too as it is not
needed anymore by userspace to get the attached TTY of a process, as
/dev/tty (which is already a character device) represents that as well.
2022-03-22 20:26:05 +01:00
Liav A
5ffe2f117c Kernel/TTY: Implement TIOCGPTN ioctl for MasterPTY
This ioctl operation will allow userspace to determine the index number
of a MasterPTY after opening /dev/ptmx and actually getting an internal
file descriptor of MasterPTY.
2022-03-22 20:26:05 +01:00
Brian Gianforcaro
913374163c LibVT/Kernel: Make VT::Attribute::Flags enum class, use AK EnumBits
Noticed the TODO in `Attribute.h` and realized we have as solution
to this problem already. :^)
2022-03-18 11:29:43 +01:00
Lenny Maiorani
190cf1507b Kernel: Use default constructors/destructors
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cother-other-default-operation-rules

"The compiler is more likely to get the default semantics right and
you cannot implement these functions better than the compiler."
2022-03-17 00:51:36 -07:00
Lenny Maiorani
c6acf64558 Kernel: Change static constexpr variables to constexpr where possible
Function-local `static constexpr` variables can be `constexpr`. This
can reduce memory consumption, binary size, and offer additional
compiler optimizations.

These changes result in a stripped x86_64 kernel binary size reduction
of 592 bytes.
2022-02-09 21:04:51 +00:00
Andreas Kling
0899153170 Kernel: Protect PTYMultiplexer freelist with spinlock instead of mutex 2022-02-03 16:11:26 +01:00
Idan Horowitz
664ca58746 Kernel: Use u64 instead of size_t for File::can_write offset
This ensures offsets will not be truncated on large files on i686.
2022-01-25 22:41:17 +02:00
Idan Horowitz
9ce537d703 Kernel: Use u64 instead of size_t for File::can_read offset
This ensures offsets will not be truncated on large files on i686.
2022-01-25 22:41:17 +02:00
Liav A
69f054616d Kernel: Add CommandLine option to disable or enable the PC speaker
By default, we disable the PC speaker as it's quite annoying when using
the text mode console.
2022-01-23 00:40:54 +00:00
Liav A
8dbbef9b5c Kernel: Implement beep functionality in virtual consoles 2022-01-23 00:40:54 +00:00
Andreas Kling
b86443f0e1 Kernel: Lock weak pointer revocation during listed-ref-counted unref
When doing the last unref() on a listed-ref-counted object, we keep
the list locked while mutating the ref count. The destructor itself
is invoked after unlocking the list.

This was racy with weakable classes, since their weak pointer factory
still pointed to the object after we'd decided to destroy it. That
opened a small time window where someone could try to strong-ref a weak
pointer to an object after it was removed from the list, but just before
the destructor got invoked.

This patch closes the race window by explicitly revoking all weak
pointers while the list is locked.
2022-01-08 16:31:14 +01:00
Daniel Bertalan
8e2efe78f7 Kernel: Tighten String-related includes 2021-12-30 14:16:03 +01:00
Brian Gianforcaro
54b9a4ec1e Kernel: Handle promise violations in the syscall handler
Previously we would crash the process immediately when a promise
violation was found during a syscall. This is error prone, as we
don't unwind the stack. This means that in certain cases we can
leak resources, like an OwnPtr / RefPtr tracked on the stack. Or
even leak a lock acquired in a ScopeLockLocker.

To remedy this situation we move the promise violation handling to
the syscall handler, right before we return to user space. This
allows the code to follow the normal unwind path, and grantees
there is no longer any cleanup that needs to occur.

The Process::require_promise() and Process::require_no_promises()
functions were modified to return ErrorOr<void> so we enforce that
the errors are always propagated by the caller.
2021-12-29 18:08:15 +01:00
Brian Gianforcaro
bad6d50b86 Kernel: Use Process::require_promise() instead of REQUIRE_PROMISE()
This change lays the foundation for making the require_promise return
an error hand handling the process abort outside of the syscall
implementations, to avoid cases where we would leak resources.

It also has the advantage that it makes removes a gs pointer read
to look up the current thread, then process for every syscall. We
can instead go through the Process this pointer in most cases.
2021-12-29 18:08:15 +01:00
Idan Horowitz
9d034785de Kernel: Make File::unref virtual
This is required for SlavePTY's custom unref handler to function
correctly, as otherwise a SlavePTY held in a File RefPtr would call
the base's (RefCounted<>) unref method instead of SlavePTY's version.
2021-12-29 15:46:14 +02:00
Brian Gianforcaro
dee0c004e0 Kernel: Zero initialize winsize in TIOCGWINSZ
It looks like type types are small enough that there is no padding.
So there didn't happen to be an info leak here, but lets zero initialize
just to be on the safe side, and make auditing easier.
2021-12-29 03:41:32 -08:00
Idan Horowitz
6e2a82df13 Kernel: Port File to RefCounted
Since RefCounted automatically calls a method named `will_be_destoyed`
on classes that have one, so there's no need to have a custom
implementation of unref in File.
2021-12-29 12:04:15 +01:00
Idan Horowitz
4a3a947df3 Kernel: Rename File::{before_removing => will_be_destroyed}
This will allow File and it's descendants to use RefCounted instead of
having a custom implementation of unref. (Since RefCounted calls
will_be_destroyed automatically)

This commit also removes an erroneous call to `before_removing` in
AHCIPort, this is a duplicate call, as the only reference to the device
is immediately dropped following the call, which in turns calls
`before_removing` via File::unref.
2021-12-29 12:04:15 +01:00
Guilherme Goncalves
33b78915d3 Kernel: Propagate overflow errors from Memory::page_round_up
Fixes #11402.
2021-12-28 23:08:50 +01:00
Andreas Kling
ac7ce12123 Kernel: Remove the kmalloc_eternal heap :^)
This was a premature optimization from the early days of SerenityOS.
The eternal heap was a simple bump pointer allocator over a static
byte array. My original idea was to avoid heap fragmentation and improve
data locality, but both ideas were rooted in cargo culting, not data.

We would reserve 4 MiB at boot and only ended up using ~256 KiB, wasting
the rest.

This patch replaces all kmalloc_eternal() usage by regular kmalloc().
2021-12-28 21:02:38 +01:00
Daniel Bertalan
52beeebe70 Kernel: Remove the KString::try_create(String::formatted(...)) pattern
We can now directly create formatted KStrings with KString::formatted.

:^)
2021-12-28 01:55:22 -08:00
Brian Gianforcaro
e88e4967d1 Kernel: Mark PTYMultiplexer init & parse_hex_digit as UNMAP_AFTER_INIT
Noticed these boot only functions are not currently UNMAP_AFTER_INIT.
Lets fix that :^)
2021-12-24 14:28:59 -08:00
Liav A
9eb08bdb0f Kernel: Make major and minor numbers to be DistinctNumerics
This helps avoid confusion in general, and make constructors, methods
and code patterns much more clean and understandable.
2021-12-23 23:02:39 +01:00
Andreas Kling
abf2204402 Kernel: Use copy_typed_from_user() in more places :^) 2021-12-18 11:30:10 +01:00
sin-ack
69ef211925 Kernel+LibC: Move errno definitions to Kernel/API/POSIX
This fixes at least half of our LibC includes in the kernel. The source
of truth for errno codes and their description strings now lives in
Kernel/API/POSIX/errno.h as an enumeration, which LibC includes.
2021-12-16 22:21:35 +03:30
Hendiadyoin1
e34eb3e36d Kernel: Remove unused String.h includes
This makes searching for not yet OOM safe interfaces a bit easier.
2021-12-11 13:15:26 -08: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
8b1108e485 Everywhere: Pass AK::StringView by value 2021-11-11 01:27:46 +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
Brian Gianforcaro
9f6eabd73a Kernel: Move TTY subsystem to use KString instead of AK::String
This is minor progress on removing the `AK::String` API from the Kernel
in the interest of improving OOM safety.
2021-11-02 11:34:31 +01:00
Brian Gianforcaro
71f05c70b4 Kernel: Remove duplicate constructor from TTY/VirtualConsole
This removes some code dupe from the constructors.

By removing this duplicate constructor we can utilize the main
VirtualConsole::create factory implementation and call that from the
VirtualConsole::create_with_preset_log factory method.
2021-11-02 11:34:31 +01:00
Ben Wiederhake
c05c5a7ff4 Kernel: Clarify ambiguous {File,Description}::absolute_path
Found due to smelly code in InodeFile::absolute_path.

In particular, this replaces the following misleading methods:

File::absolute_path
This method *never* returns an actual path, and if called on an
InodeFile (which is impossible), it would VERIFY_NOT_REACHED().

OpenFileDescription::try_serialize_absolute_path
OpenFileDescription::absolute_path
These methods do not guarantee to return an actual path (just like the
other method), and just like Custody::absolute_path they do not
guarantee accuracy. In particular, just renaming the method made a
TOCTOU bug obvious.

The new method signatures use KResultOr, just like
try_serialize_absolute_path() already did.
2021-10-31 12:06:28 +01:00
Daniel Bertalan
5f6030b13c Kernel+LibC: Default to 8-bit characters in TTY
Some ports (like `bc` with history enabled) sensibly set the termios
character size to 8 bits.

Previously, we left the character size value (given by the bitmask
CSIZE) as zero by default (meaning 5 bits per character), and returned
ENOTIMPL whenever someone modified it. This was dumb.
2021-10-25 21:42:26 +02:00
Peter Elliott
669b23ac0a Kernel: Handle backspace for tab character in TTY cooked mode
Before, serenity would only backspace one character for a tab.
This is the only feature that my OS has and serenity doesn't.
2021-10-06 12:31:13 +02:00
Brian Gianforcaro
5f1c98e576 Kernel: Use operator ""sv in all class_name() implementations
Previously there was a mix of returning plain strings and returning
explicit string views using `operator ""sv`. This change switches them
all to standardized on `operator ""sv` as it avoids a call to strlen.
2021-10-03 13:36:10 +02:00
Liav A
3e9dfd2e74 Kernel: Remove IO.h include from a bunch of places 2021-10-01 12:27:20 +02:00
Liav A
fd4397a430 Kernel/Devices: Use try_create_device helper for ConsoleDevice 2021-09-17 01:02:48 +03:00
Liav A
5e8dcb9ca7 Kernel/Devices: Move ConsoleDevice into the Devices source directory 2021-09-17 01:02:48 +03:00
Liav A
aee4786d8e Kernel: Introduce the DeviceManagement singleton
This singleton simplifies many aspects that we struggled with before:
1. There's no need to make derived classes of Device expose the
constructor as public anymore. The singleton is a friend of them, so he
can call the constructor. This solves the issue with try_create_device
helper neatly, hopefully for good.
2. Getting a reference of the NullDevice is now being done from this
singleton, which means that NullDevice no longer needs to use its own
singleton, and we can apply the try_create_device helper on it too :)
3. We can now defer registration completely after the Device constructor
which means the Device constructor is merely assigning the major and
minor numbers of the Device, and the try_create_device helper ensures it
calls the after_inserting method immediately after construction. This
creates a great opportunity to make registration more OOM-safe.
2021-09-17 01:02:48 +03:00
Liav A
f5de4f24b2 Kernel/Devices: Defer creation of SysFS component after the constructor
Instead of doing so in the constructor, let's do immediately after the
constructor, so we can safely pass a reference of a Device, so the
SysFSDeviceComponent constructor can use that object to identify whether
it's a block device or a character device.
This allows to us to not hold a device in SysFSDeviceComponent with a
RefPtr.
Also, we also call the before_removing method in both SlavePTY::unref
and File::unref, so because Device has that method being overrided, it
can ensure the device is removed always cleanly.
2021-09-11 11:41:14 +02: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
Liav A
21b6d84ff0 Kernel/Devices: Remove required_mode and device_name methods
These methods are no longer needed because SystemServer is able to
populate the DevFS on its own.

Device absolute_path no longer assume a path to the /dev location,
because it really should not assume any path to a Device node.

Because StorageManagement still needs to know the storage name, we
declare a virtual method only for StorageDevices to override, but this
technique should really be removed later on.
2021-09-08 00:42:20 +02:00
Andreas Kling
631b8e90cd Kernel: Use KResultOr and TRY() for MasterPTY 2021-09-07 13:58:16 +02:00
Andreas Kling
01993d0af3 Kernel: Make DoubleBuffer::try() return KResultOr
This tidies up error propagation in a number of places.
2021-09-07 13:53:14 +02: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
Andreas Kling
75564b4a5f Kernel: Make kernel region allocators return KResultOr<NOP<Region>>
This expands the reach of error propagation greatly throughout the
kernel. Sadly, it also exposes the fact that we're allocating (and
doing other fallible things) in constructors all over the place.

This patch doesn't attempt to address that of course. That's work for
our future selves.
2021-09-06 01:55:27 +02:00
Andreas Kling
5e2bae700d Kernel: Use TRY() in PTYMultiplexer::open() 2021-09-06 01:55:27 +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