Commit graph

4578 commits

Author SHA1 Message Date
Idan Horowitz 0e5aba16ef Kernel: Use unsigned instead of signed types in SerialDevice
Addresses are unsigned by definition, and the conversion from signed
to unsigned and back in SerialDevice looked a bit dubious.
2021-05-17 18:15:25 +02:00
Idan Horowitz 3ad0a0d8c3 Kernel: Initialize the PCI Bus earlier in the boot sequence
We now initialize the PCI Bus as early as possible, to allow for
early boot (PCI based) serial logging.
2021-05-17 18:15:25 +02:00
Gunnar Beutner 89956cb0d6 Kernel+Userspace: Implement the accept4() system call
Unlike accept() the new accept4() system call lets the caller specify
flags for the newly accepted socket file descriptor, such as
SOCK_CLOEXEC and SOCK_NONBLOCK.
2021-05-17 13:32:19 +02:00
Daniel Bertalan f0375e3efe Kernel+LibC: Support more termios settings in TTY
This commit adds support for the various ECHO* lflags and fixes some
POSIX conformance issues around newline handling. Also included are
error messages when setting not implemented settings.
2021-05-17 11:00:48 +02:00
Liav A e6f333ae00 Kernel: Print failed attempt to shutdown the machine
Because we don't parse ACPI AML yet, If we are not able to shut down
the machine with "hacky" emulation methods - halt and print this state
to the users so they know they can shutdown the machine by themselves.
2021-05-17 00:30:40 +01:00
Liav A 02b73cb93d Kernel/Graphics: Be more consistent about arguments passing
This fixes a bug that was reported on this discord server by
@ElectrodeYT - due to the confusion of passing arguments in different
orders, we messed up and triggered a page fault due to faulty sizes.
2021-05-17 00:30:40 +01:00
Liav A 7995e115b8 Kernel/Graphics: Round size value when mapping real framebuffer 2021-05-16 21:59:10 +01:00
Liav A 7f3080c8f2 Kernel/Graphics: Assign console to be from VGACompatibleAdapter always
If we create a VGACompatibleAdapter object with a preset framebuffer,
Always assign the console so we can use it.

This is useful for modesetting done by a Multiboot loader, like GRUB.
2021-05-16 21:59:10 +01:00
Liav A 58d7eb36ad Kernel/Graphics: Fix a method to be more accurate about its name 2021-05-16 21:59:10 +01:00
Liav A 8cbf3d88ff Kernel: Move ConsoleDevice initialization just after kmalloc init
This will ensure we will get all the kernel log on the second tty.
2021-05-16 19:58:33 +02:00
Liav A dbccfc3281 Kernel: Print all logbuffer from ConsoleDevice to debug Virtual Console 2021-05-16 19:58:33 +02:00
Liav A ab52108982 Kernel: Allow the user to specify the virtual console when booting 2021-05-16 19:58:33 +02:00
Liav A 99eab4667a Kernel: Print scheduler state to the display console 2021-05-16 19:58:33 +02:00
Liav A 20743e8aed Kernel/Graphics + SystemServer: Support text mode properly
As we removed the support of VBE modesetting that was done by GRUB early
on boot, we need to determine if we can modeset the resolution with our
drivers, and if not, we should enable text mode and ensure that
SystemServer knows about it too.

Also, SystemServer should first check if there's a framebuffer device
node, which is an indication that text mode was not even if it was
requested. Then, if it doesn't find it, it should check what boot_mode
argument the user specified (in case it's self-test). This way if we
try to use bochs-display device (which is not VGA compatible) and
request a text mode, it will not honor the request and will continue
with graphical mode.

Also try to print critical messages with mininum memory allocations
possible.

In LibVT, We make the implementation flexible for kernel-specific
methods that are implemented in ConsoleImpl class.
2021-05-16 19:58:33 +02:00
Liav A dac129e10b Kernel: Expand the kernel memory slot from 16 MiB to 32 MiB
Like in 8cd5477e54, we need to expand the
kernel slot again to be able to boot again.
2021-05-16 19:58:33 +02:00
Liav A 7078119c58 Kernel: Don't ref-count UsedMemoryRange during iteration 2021-05-16 19:58:33 +02:00
Liav A 8f2ddde4cb Kernel: Rename Console => ConsoleDevice
This change will help to distinguish between the console
device and the Console abstraction layer in the Graphics
subsystem later.
2021-05-16 19:58:33 +02:00
Liav A 0669dd82e2 Kernel/PCI: Add helper to determine if device can access IO space 2021-05-16 19:58:33 +02:00
Liav A 2cd1f928e1 Kernel: Drop support of GRUB VBE modesetting
We used GRUB to modeset the resolution for a long time, but for good
reasons I see no point with keeping it supported in our kernel. We
support bochs-display device on QEMU (both the VGA compatible and
non-VGA compatible variants), so for QEMU we can still boot the system
in graphical mode even without GRUB help.

Also, we now have a native driver for Intel graphics and although it
doesn't support most Intel graphics cards out there yet, it's a good
starting point to support more cards. If a user wants to boot on
bare-metal in graphical mode, all he needs to do is to add the removed
flag back again, as the kernel still supports pre-set framebuffers.
2021-05-16 19:58:33 +02:00
Liav A cc92538d49 Kernel/Graphics: Add basic support for Intel native accelerator
We simply modeset the resolution after determining the preferred
resolution after getting the EDID from the attached display.
2021-05-16 19:58:33 +02:00
Liav A 6a728e2d76 Kernel: Introduce a new graphics subsystem
This new subsystem is replacing the old code that was used to
create device nodes of framebuffer devices in /dev.

This subsystem includes for now 3 roles:
1. GraphicsManagement singleton object that is used in the boot
process to enumerate and initialize display devices.
2. GraphicsDevice(s) that are used to control the display adapter.
3. FramebufferDevice(s) that are used to control the device node in
/dev.

For now, we support the Bochs display adapter and any other
generic VGA compatible adapter that was configured by the boot
loader to a known and fixed resolution.

Two improvements in the Bochs display adapter code are that
we can support native bochs-display device (this device doesn't
expose any VGA capabilities) and also that we use the MMIO region,
to configure the device, instead of setting IO ports for such tasks.
2021-05-16 19:58:33 +02:00
Liav A 86be477da0 Kernel: Support the bochs-display device
This device is a graphics display device that is not supporting
VGA functionality.
Therefore, it exposes a MMIO region to configure it, so we use that
region to set the framebuffer resolution.
2021-05-16 19:58:33 +02:00
Gunnar Beutner 006f11f23d Kernel: Avoid allocations when handling network packets 2021-05-16 17:49:42 +02:00
Gunnar Beutner 4b5dbc15df Kernel: Fix incorrect argument when constructing DiskPartitionMetadata
The existing code invokes operator bool for the partition_type
variable.
2021-05-16 17:49:42 +02:00
Gunnar Beutner fcaf98361f AK: Turn ByteBuffer into a value type
Previously ByteBuffer would internally hold a RefPtr to the byte
buffer and would behave like a reference type, i.e. copying a
ByteBuffer would not create a duplicate byte buffer, but rather
two objects which refer to the same internal buffer.

This also changes ByteBuffer so that it has some internal capacity
much like the Vector<T> type. Unlike Vector<T> however a byte
buffer's data may be uninitialized.

With this commit ByteBuffer makes use of the kmalloc_good_size()
API to pick an optimal allocation size for its internal buffer.
2021-05-16 17:49:42 +02:00
Daniel Bertalan d5ea04cdfb LibC+Kernel: Add sys/ttydefaults.h
This non-POSIX header is used in Linux/BSD systems for storing the
default termios settings. This lets us setup new TTYs' `m_termios.c_cc`
in a nicer way than using a magic string.
2021-05-16 16:31:30 +02:00
Daniel Bertalan be519022c3 LibVT: Implement new ANSI escape sequence parser
This commit replaces the former, hand-written parser with a new one that
can be generated automatically according to a state change diagram.

The new `EscapeSequenceParser` class provides a more ergonomic interface
to dealing with escape sequences. This interface has been inspired by
Alacritty's [vte library](https://github.com/alacritty/vte/).

I tried to avoid changing the application logic inside the `Terminal`
class. While this code has not been thoroughly tested, I can't find
regressions in the basic command line utilities or `vttest`.

`Terminal` now displays nicer debug messages when it encounters an
unknown escape sequence. Defensive programming and bounds checks have
been added where we access parameters, and as a result, we can now
endure 4-5 seconds of `cat /dev/urandom`. :D

We generate EscapeSequenceStateMachine.h when building the in-kernel
LibVT, and we assume that the file is already in place when the userland
library is being built. This will probably cause problems later on, but
I can't find a way to do it nicely.
2021-05-16 11:50:56 +02:00
Nicholas Baron aa4d41fe2c
AK+Kernel+LibELF: Remove the need for IteratorDecision::Continue
By constraining two implementations, the compiler will select the best
fitting one. All this will require is duplicating the implementation and
simplifying for the `void` case.

This constraining also informs both the caller and compiler by passing
the callback parameter types as part of the constraint
(e.g.: `IterationFunction<int>`).

Some `for_each` functions in LibELF only take functions which return
`void`. This is a minimal correctness check, as it removes one way for a
function to incompletely do something.

There seems to be a possible idiom where inside a lambda, a `return;` is
the same as `continue;` in a for-loop.
2021-05-16 10:36:52 +01:00
Sahan Fernando cbc845c8a8 Kernel: Reorder VirtIODevice PCI initialization steps
We can't reset the device before we've read the PCI configuration
space, because we read the reset register location from the
configuration space.
2021-05-15 23:29:03 +01:00
Sahan Fernando 7cf34b5549 Kernel: Rename VirtIODevice::clear_status_bit to mask_status_bits 2021-05-15 23:29:03 +01:00
Andreas Kling 4d429ba9ea Kernel: Unbreak profiling all processes
Regressed in 8a4cc735b9.
We stopped generating "process created" when enabling profiling,
which led to Profiler getting confused about the missing events.
2021-05-15 21:25:54 +02:00
Luke 57277d8a5c Kernel/AHCI: Fix "received" => "recovered" typo in communication error
The error is actually "Recovered communications error" instead of
"Received communications error".
2021-05-15 19:45:44 +02:00
Luke 174fdddc2b Kernel/AHCI: Get BOH and NVMP from extended capabilities
It was accidentally getting it from the regular capabilities.
2021-05-15 19:45:44 +02:00
Alexander Richards 88a997871e
AHCIController: Fix off-by-one mistake (#7144)
Fixes off-by-one caused by reading the register directly
without adding a 1 to it, because AHCI reports 1 less port than
the actual number of ports supported.
2021-05-15 19:45:23 +02:00
Sahan Fernando e4f61c6f28 Kernel: Fix return values of BXVGADevice::read/write 2021-05-15 17:43:45 +01:00
Liav A 8a4cc735b9 Kernel: Don't use the profile timer if we don't have a timer to assign 2021-05-15 18:08:41 +02:00
Gunnar Beutner f89e8fb71a AK+LibC: Implement malloc_good_size() and use it for Vector/HashTable
This implements the macOS API malloc_good_size() which returns the
true allocation size for a given requested allocation size. This
allows us to make use of all the available memory in a malloc chunk.

For example, for a malloc request of 35 bytes our malloc would
internally use a chunk of size 64, however the remaining 29 bytes
would be unused.

Knowing the true allocation size allows us to request more usable
memory that would otherwise be wasted and make that available for
Vector, HashTable and potentially other callers in the future.
2021-05-15 16:30:14 +02:00
Gunnar Beutner 4ab9d8736b Kernel: Make perf_event() work for global profiles
Previously calls to perf_event() would end up in a process-specific
perfcore file even though global profiling was enabled. This changes
the behavior for perf_event() so that these events are stored into
the global profile instead.
2021-05-15 16:28:18 +02:00
Luke 0f35dfc694 Kernel/AHCI: Don't check for PCC during initialization
On my machine, it only sets PRC and not PCC.

Confirmed to happen on:
- 8086:9ca2 (Intel Corporation Wildcat Point-LP SATA Controller
[AHCI Mode] (rev 03))
2021-05-15 10:14:16 +02:00
Luke 84fc498b9f Kernel/AHCI: Don't enable interrupts in the AHCIPort constructor
On my bare metal machine, enabling it as this point causes it to
instantly send an interrupt, and we're too early in the process
to be able to handle AHCI interrupts. The interrupts were being
enabled in the initialize function anyway.

Confirmed to happen on:
- 8086:9ca2 (Intel Corporation Wildcat Point-LP SATA Controller
[AHCI Mode] (rev 03))
- 8086:3b22 (Intel Corporation 5 Series/3400 Series Chipset
6 port SATA AHCI Controller (rev 06))
2021-05-15 10:14:16 +02:00
Brian Gianforcaro f255993349 Kernel: Log unexpected TCP packet flags in NetworkTask handle_tcp()
Occasionally we'll see messages in the serial console like:

    handle_tcp: unexpected flags in FinWait1 state

In these cases it would be nice to know what flags we are receiving that
we aren't expecting.
2021-05-15 09:46:50 +02:00
Brian Gianforcaro d76dedb381 Kernel: Fix UHCIController singleton startup null-deref race condition.
The following KUBSAN crash on startup was reported on discord:

```
UHCI: Started
KUBSAN: reference binding to null pointer of type struct UHCIController
KUBSAN: at ../../Kernel/Devices/USB/UHCIController.cpp, line 67
```

After inspecting the code, it became clear that there's a window of time
where the kernel task which monitors the UHCI port can startup and start
executing before the UHCIController constructor completes. This leaves
the singleton pointing to nullptr, thus in the duration of this race
window the "UHCI port proc" thread will go an and de-reference the null
pointer when trying to read for status changes on the UHCI root ports.

Reported-by: @stelar7
Reported-by: @bcoles

Fixes: #6154
2021-05-15 09:46:41 +02:00
Brian Gianforcaro a324d4d6a3 Kernel: Make AnonymousVMObject physical page APIs OOM safe
AnonymousVMObject::create_with_physical_page(s) can't be NonnullRefPtr
as it allocates internally. Fixing the API then surfaced an issue in
ScatterGatherList, where the code was attempting to create an
AnonymousVMObject in the constructor which will not be observable
during OOM.

Fix all of these issues and start propagating errors at the callers
of the AnonymousVMObject and ScatterGatherList APis.
2021-05-15 09:01:32 +02:00
Brian Gianforcaro d45db06826 Kernel: Make AnonymousVMObject::clone/create APIs OOM safe 2021-05-15 09:01:32 +02:00
Brian Gianforcaro ede1483e48 Kernel: Make Process creation APIs OOM safe
This change looks more involved than it actually is. This simply
reshuffles the previous Process constructor and splits out the
parts which can fail (resource allocation) into separate methods
which can be called from a factory method. The factory is then
used everywhere instead of the constructor.
2021-05-15 09:01:32 +02:00
Brian Gianforcaro 77868abe6a Kernel: Make PTYMultiplexer::open API OOM safe 2021-05-15 09:01:32 +02:00
Brian Gianforcaro f3f5a225b9 Kernel: Fix lock state corruption in AHCIPORT::start_request
This code was unlocking the lock directly, but the Locker is still
attached, causing the lock to be unlocked an extra time, hence
corrupting the internal lock state.

This is extra confusing though, as complete_current_request() runs
without a lock which also looks like a bug. But that's a task for
another day.
2021-05-15 09:01:13 +02:00
Brian Gianforcaro f982ef0ef0 Kernel: Halt CPU on deadly UBSAN instead of calling PANIC
The separate backtrace that the PANIC emits isn't useful and just adds
more data to visually filter from the output when debugging an issue.
Instead of calling PANIC, just emit the message with dbgln() and
manually halt the system.
2021-05-15 09:00:29 +02:00
Brian Gianforcaro db78331741 Kernel: Don't crash in page_fault_handler if current_thread is null
If we are attempting to emit debugging information about an unhandleable
page fault, don't crash trying to kill threads or dump processes if the
current_thread isn't set in TLS. Attempt to keep proceeding in order to
dump as much useful information as possible.

Related: #6948
2021-05-15 09:00:29 +02:00
Andreas Kling 16221305ad LibELF: Remove sketchy use of "undefined" ELF::Image::Section
We were using ELF::Image::section(0) to indicate the "undefined"
section, when what we really wanted was just Optional<Section>.

So let's use Optional instead. :^)
2021-05-15 00:17:55 +02:00
Mart G e7310ba45a Kernel+LibC: Add fstatat
The function fstatat can do the same thing as the stat and lstat
functions. However, it can be passed the file descriptor of a directory
which will be used when as the starting point for relative paths. This
is contrary to stat and lstat which use the current working directory as
the starting for relative paths.
2021-05-14 23:32:10 +02:00
Gunnar Beutner c22296505c Kernel: Merge do_retransmit_packets() into retransmit_packets() 2021-05-14 16:50:00 +02:00
Gunnar Beutner 08aa3a91e3 Kernel: Try to retransmit lost TCP packets
Previously we didn't retransmit lost TCP packets which would cause
connections to hang if packets were lost. Also we now time out
TCP connections after a number of retransmission attempts.
2021-05-14 16:50:00 +02:00
Gunnar Beutner 9daec809b7 Kernel: Wake up NetworkTask every 500 milliseconds
This wakes up NetworkTask every 500 milliseconds so that it can send
pending delayed TCP ACKs and isn't forced to send all of them early
when it goes to sleep like it did before.
2021-05-14 16:50:00 +02:00
Gunnar Beutner 990b2d0989 Kernel: Don't use delayed ACKs when establishing the connection
When establishing the connection we should send ACKs right away so
as to not delay the connection process. This didn't previously
matter because we'd flush all delayed ACKs when NetworkTask waits
for incoming packets.
2021-05-14 16:50:00 +02:00
Brian Gianforcaro 9c38475608 Kernel: Add the ability to verify we don't kmalloc under spinlock.
Ideally we would never allocate under a spinlock, as it has many
performance and potentially functionality (deadlock) pitfalls.

We violate that rule in many places today, but we need a tool to track
them all down and fix them. This change introduces a new macro option
named `KMALLOC_VERIFY_NO_SPINLOCK_HELD` which can catch these
situations at runtime via an assert.
2021-05-14 13:28:21 +02:00
Brian Gianforcaro 4728f2af80 Kernel: Avoid unnecessary time under lock in TCPSocket::create
Avoid holding the sockets_by_tuple lock while allocating the TCPSocket.
While checking if the list contains the item we can also hold the lock
in shared mode, as we are only reading the hash table.

In addition the call to from_tuple appears to be superfluous, as we
created the socket, so we should be able to just return it directly.
This avoids the recursive lock acquisition, as well as the unnecessary
hash table lookups.
2021-05-14 11:32:50 +02:00
Brian Gianforcaro 879eec6aa8 Kernel: Remove dead TCPSocket::from_endpoints method 2021-05-14 11:32:50 +02:00
Gunnar Beutner 53664787fb Kernel: Correctly set the lost_samples field for the first sample
This ensures that the lost_samples field is set to zero for the
first sample. We didn't lose any samples before the first sample
so this is the correct value. Without this Profiler gets confused
and draws the graph for the process which contains the first CPU
sample incorrectly (all zeroes usually).
2021-05-14 00:46:10 +02:00
Gunnar Beutner c41f13f10b Kernel+Profiler: Track lost time between profiler timer ticks
We can lose profiling timer events for a few reasons, for example
disabled interrupts or system slowness. This accounts for lost
time between CPU samples by adding a field lost_samples to each
profiling event which tracks how many samples were lost immediately
preceding the event.
2021-05-14 00:35:57 +02:00
Gunnar Beutner 8614d18956 Kernel: Use a separate timer for profiling the system
This updates the profiling subsystem to use a separate timer to
trigger CPU sampling. This timer has a higher resolution (1000Hz)
and is independent from the scheduler. At a later time the
resolution could even be made configurable with an argument for
sys$profiling_enable() - but not today.
2021-05-14 00:35:57 +02:00
Andreas Kling e46343bf9a Kernel: Make UserOrKernelBuffer R/W helpers return KResultOr<size_t>
This makes error propagation less cumbersome (and also exposed some
places where we were not doing it.)
2021-05-13 23:28:40 +02:00
Alexander Richards 28a6a9a08f
IDEChannel: Fix wait_until_not_busy() (#7084)
The time_elapsed variable would count until milliseconds_timeout + 1,
so a != comparision won't work.
2021-05-13 19:11:14 +02:00
Brian Gianforcaro 8693c925a0 CMake: Fix message levels for error conditions during configuration
Make messages which should be fatal, actually fail the build.

- FATAL is not a valid mode keyword. The full list is available in the
  docs: https://cmake.org/cmake/help/v3.19/command/message.html

- SEND_ERROR doesn't immediately stop processing, FATAL_ERROR does.
  We should immediately stop if the Toolchain is not present.

- The app icon size validation was just a WARNING that is easy to
  overlook. We should promote it to a FATAL_ERROR so that people will
  not overlook the issue when adding a new application. We can only make
  the small icon message FATAL_ERROR, as there is currently one
  violation of the medium app icon validation.
2021-05-13 18:52:48 +02:00
Brian Gianforcaro 0d50d3ed1e Kernel: Make InodeWatcher::crate API OOM safe 2021-05-13 16:21:53 +02:00
Brian Gianforcaro c8758d4faa Kernel: Make KBuffer::try_create_with* APIs OOM safe 2021-05-13 16:21:53 +02:00
Brian Gianforcaro 2e34714ba1 Kernel: Make UDPSocket::create() API OOM safe 2021-05-13 16:21:53 +02:00
Brian Gianforcaro 858fff979a Kernel: Make IPv4Socket::create API for SOCK_RAW OOM safe 2021-05-13 16:21:53 +02:00
Brian Gianforcaro 46ce7adf7b Kernel: Make TCPSocket::create API OOM safe
Note that the changes to IPv4Socket::create are unfortunately needed as
the return type of TCPSocket::create and IPv4Socket::create don't match.

 - KResultOr<NonnullRefPtr<TcpSocket>>>
   vs
 - KResultOr<NonnullRefPtr<Socket>>>

To handle this we are forced to manually decompose the KResultOr<T> and
return the value() and error() separately.
2021-05-13 16:21:53 +02:00
Brian Gianforcaro 9375f3dc09 Kernel: Make LocalSocket factory APIs OOM safe 2021-05-13 16:21:53 +02:00
Brian Gianforcaro 51ceb172b9 Kernel: Replace make<T>() with adopt_own_if_nonnull() in sys$module_load 2021-05-13 16:21:53 +02:00
Brian Gianforcaro 9ca8f0afaa Kernel: Replace make<T>() with adopt_own_if_nonnull() in KBufferBuilder
The make<T> factory function allocates internally and immediately
dereferences the pointer, and always returns a NonnullOwnPtr<T> making
it impossible to propagate an error on OOM.
2021-05-13 16:21:53 +02:00
Brian Gianforcaro fb40da0429 Kernel: Replace make<T>() with adopt_own_if_nonnull() in Ext2FileSystem
The make<T> factory function allocates internally and immediately
dereferences the pointer, and always returns a NonnullOwnPtr<T> making
it impossible to propagate an error on OOM.
2021-05-13 16:21:53 +02:00
Brian Gianforcaro 5dc5f31f76 Kernel: Replace bare new in Custody::create() with adopt_ref_if_nonnull 2021-05-13 16:21:53 +02:00
Brian Gianforcaro 956314f0a1 Kernel: Make Process::start_tracing_from API OOM safe
Modify the API so it's possible to propagate error on OOM failure.
NonnullOwnPtr<T> is not appropriate for the ThreadTracer::create() API,
so switch to OwnPtr<T>, use adopt_own_if_nonnull() to handle creation.
2021-05-13 16:21:53 +02:00
Sahan Fernando ed0e7b53a5 Kernel: Move VirtIO code away from using a scatter gather list
Currently, when passing buffers into VirtIOQueues, we use scatter-gather
lists, which contain an internal vector of buffers. This vector is
allocated, filled and the destroy whenever we try to provide buffers
into a virtqueue, which would happen a lot in performance cricital code
(the main transport mechanism for certain paravirtualized devices).

This commit moves it over to using VirtIOQueueChains and building the
chain in place in the VirtIOQueue. Also included are a bunch of fixups
for the VirtIO Console device, making it use an internal VM::RingBuffer
instead.
2021-05-13 10:00:42 +02:00
Sahan Fernando 13d5cdcd08 Kernel: Create VM::RingBuffer class 2021-05-13 10:00:42 +02:00
Sahan Fernando 8131c0de8c Kernel: Move AHCIPort::ScatterList to VM::ScatterGatherList
We want to move this out of the AHCI subsystem into the VM system,
since other parts of the kernel may need to perform scatter-gather IO.

We rename the current VM::ScatterGatherList impl that's used in the
virtio subsystem to VM::ScatterGatherRefList, since its distinguishing
feature from the AHCI scatter-gather list is that it doesn't own its
buffers.
2021-05-13 10:00:42 +02:00
Brian Gianforcaro b8fd52fad6 Kernel: Remove unused header from FileDescription.cpp 2021-05-13 08:29:01 +02:00
Brian Gianforcaro a643ee5759 Kernel: Move FileDescription::get_dir_entries to KResultOr<ssize_t>
This normalizes the error handling with the rest of the subsystem.
2021-05-13 08:29:01 +02:00
Brian Gianforcaro 12ab0dcee0 Kernel: Make FileDescription::create() APIs OOM safe 2021-05-13 08:29:01 +02:00
Brian Gianforcaro 11bd2002bb Kernel: Make InodeFile::create() API OOM safe 2021-05-13 08:29:01 +02:00
Brian Gianforcaro 112393b38a Kernel: Make Thread::try_create API OOM safe 2021-05-13 08:29:01 +02:00
Brian Gianforcaro 6d39b792f0 Kernel: Declare operator new/delete noexcept for MAKE_SLAB_ALLOCATED 2021-05-13 08:29:01 +02:00
Brian Gianforcaro 788075c58b Kernel: Declare operator new/delete noexcept for MAKE_ALIGNED_ALLOCATED 2021-05-13 08:29:01 +02:00
Brian Gianforcaro 97adaaf933 Kernel: Declare operator new/delete as noexcept for the Kernel
For Kernel OOM hardening to work correctly, we need to be able to
call a "nothrow" version of operator new. Unfortunately the default
"throwing" version of operator new assumes that the allocation will
never return on failure and will always throw an exception. This isn't
true in the Kernel, as we don't have exceptions. So if we call the
normal/throwing new and kmalloc returns NULL, the generated code will
happily go and dereference that NULL pointer by invoking the constructor
before we have a chance to handle the failure.

To fix this we declare operator new as noexcept in the Kernel headers,
which will allow the caller to actually handle allocation failure.

The delete implementations need to match the prototype of the new which
allocated them, so we need define delete as noexcept as well. GCC then
errors out declaring that you should implement sized delete as well, so
this change provides those stubs in order to compile cleanly.

Finally the new operator definitions have been standardized as being
declared with [[nodiscard]] to avoid potential memory leaks. So lets
declares the kernel versions that way as well.
2021-05-13 08:29:01 +02:00
Gunnar Beutner 8b079a6b0d Kernel: Avoid unnecessary allocations for TTY::tty_name() 2021-05-13 08:27:42 +02:00
Gunnar Beutner 93c3b6bdd2 Kernel: Avoid allocations in KBufferBuilder::appendff
This avoids some of the the shortest-lived allocations in the kernel:

StringImpl::create_uninitialized(unsigned long, char*&)
StringImpl::create(char const*, unsigned long, ShouldChomp)
StringBuilder::to_string() const
String::vformatted(StringView, TypeErasedFormatParams)
void Kernel::KBufferBuilder::appendff<unsigned int>(...)
JsonObjectSerializer<Kernel::KBufferBuilder>::add(..., unsigned int)
Kernel::procfs$all(Kernel::InodeIdentifier, ...) const
Kernel::procfs$all(Kernel::InodeIdentifier, Kernel::KBufferBuilder&)
2021-05-13 08:27:42 +02:00
Gunnar Beutner 1bb20a255f Kernel: Avoid unnecessary allocations in NetworkAdapter::for_each
This avoids allocations for initializing the Function<T>
for the NetworkAdapter::for_each callback argument.

Applying this patch decreases CPU utilization for NetworkTask
from 40% to 28% when receiving TCP packets at a rate of 100Mbit/s.
2021-05-13 08:27:42 +02:00
Gunnar Beutner 76deda802d Kernel: Avoid allocating and then freeing packet buffers
We already have another limit for the total number of packet buffers
allowed (max_packet_buffers). This second limit caused us to
repeatedly allocate and then free buffers.
2021-05-13 08:27:42 +02:00
sin-ack fe5ca6ca27 Kernel: Implement multi-watch InodeWatcher :^)
This patch modifies InodeWatcher to switch to a one watcher, multiple
watches architecture.  The following changes have been made:

- The watch_file syscall is removed, and in its place the
  create_iwatcher, iwatcher_add_watch and iwatcher_remove_watch calls
  have been added.
- InodeWatcher now holds multiple WatchDescriptions for each file that
  is being watched.
- The InodeWatcher file descriptor can be read from to receive events on
  all watched files.

Co-authored-by: Gunnar Beutner <gunnar@beutner.name>
2021-05-12 22:38:20 +02:00
Tom 3f9927b0c3 Kernel: Fix issues supporting HPETs with 32-bit-only main counter
If the HPET main counter does not support full 64 bits, we should
not expect the upper 32 bit to work. This is a problem when writing
to the upper 32 bit of the comparator value, which requires the
TimerConfiguration::ValueSet bit to be set, but if it's not 64 bit
capable then the bit will not be cleared and leave it in a bad state.

Fixes #6990
2021-05-12 21:44:16 +02:00
Gunnar Beutner 790d68ac5e Kernel: Route packets destined for us through the loopback adapter
Without this patch we'd send packets through the physical adapter
and they'd incorrectly end up on the network.
2021-05-12 16:31:29 +02:00
Gunnar Beutner 2b6aa571d1 Kernel: Outbound packets should use the source address from the socket
Previously we'd use the adapter's address as the source address
when sending packets. Instead we should use the socket's bound local
address.
2021-05-12 16:31:29 +02:00
Gunnar Beutner 532db9f768 Kernel: Treat 0.0.0.0 as a loopback address
This matches what other operating systems like Linux do:

$ ip route get 0.0.0.0
local 0.0.0.0 dev lo src 127.0.0.1 uid 1000
    cache <local>

$ ssh 0.0.0.0
gunnar@0.0.0.0's password:

$ ss -na | grep :22 | grep ESTAB
tcp   ESTAB      0      0   127.0.0.1:43118   127.0.0.1:22
tcp   ESTAB      0      0   127.0.0.1:22      127.0.0.1:43118
2021-05-12 13:47:07 +02:00
Gunnar Beutner af59f64bc0 Kernel: Coalesce TCP ACKs
Previously we'd send a TCP ACK for each TCP packet we received. This
changes NetworkTask so that we send fewer TCP ACKs.
2021-05-12 13:47:07 +02:00
Gunnar Beutner ffc6b714b0 Kernel: Trigger TCP fast retransmission when we encounter lost packets
When we receive a TCP packet with a sequence number that is not what
we expected we have lost one or more packets. We can signal this to
the sender by sending a TCP ACK with the previous ack number so that
they can resend the missing TCP fragments.
2021-05-12 13:47:07 +02:00
Gunnar Beutner 7272127927 Kernel: Don't process TCP packets out of order
Previously we'd process TCP packets in whatever order we received
them in. In the case where packets arrived out of order we'd end
up passing garbage to the userspace process.

This was most evident for TLS connections:

courage:~ $ git clone https://github.com/SerenityOS/serenity
Cloning into 'serenity'...
remote: Enumerating objects: 178826, done.
remote: Counting objects: 100% (1880/1880), done.
remote: Compressing objects: 100% (907/907), done.
error: RPC failed; curl 56 OpenSSL SSL_read: error:1408F119:SSL
routines:SSL3_GET_RECORD:decryption failed or bad record mac, errno 0
error: 1918 bytes of body are still expected
fetch-pack: unexpected disconnect while reading sideband packet
fatal: early EOF
fatal: fetch-pack: invalid index-pack output
2021-05-12 13:47:07 +02:00
Gunnar Beutner aff4d22de9 Kernel: Set MSS option for outbound TCP SYN packets
When the MSS option header is missing the default maximum segment
size is 536 which results in lots of very small TCP packets that
NetworkTask has to handle.

This adds the MSS option header to outbound TCP SYN packets and
sets it to an appropriate value depending on the interface's MTU.

Note that we do not currently do path MTU discovery so this could
cause problems when hops don't fragment packets properly.
2021-05-12 13:47:07 +02:00
Gunnar Beutner 5feeb62843 Kernel: Avoid allocating KBuffers for TCP packets
This avoids allocating a KBuffer for each incoming TCP packet.
2021-05-12 13:47:07 +02:00
Gunnar Beutner c0800ab898 Kernel: Increase the default TCP window size
This increases the default TCP window size to a more reasonable
value of 64k. This allows TCP peers to send us more packets before
waiting for corresponding ACKs.
2021-05-12 13:47:07 +02:00
Gunnar Beutner b83a110174 Kernel: Increase IPv4 buffer size to 256kB
This increases the buffer size for connection-oriented sockets
to 256kB. In combination with the other patches in this series
I was able to receive TCP packets at a rate of about 120Mbps.
2021-05-12 13:47:07 +02:00
Mart G b00cdf8ed8 Kernel+LibC: Make get_dir_entries syscall retriable
The get_dir_entries syscall failed if the serialized form of all the
directory entries together was too large to fit in its temporary buffer.

Now the kernel uses a fixed size buffer, that is flushed to an output
buffer when it is full. If this flushing operation fails because there
is not enough space available, the syscall will return -EINVAL. That
error code is then used in userspace as a signal to allocate a larger
buffer and retry the syscall.
2021-05-12 12:50:23 +02:00
Gunnar Beutner 22ebd754d3 Kernel: Fix loading ELF images without PT_INTERP
Previously we'd try to load ELF images which did not have
an interpreter set with an incorrect load offset of 0, i.e. way
outside of the part of the address space where we'd expect either
the dynamic loader or the user's executable to reside.

This fixes the problem by using get_load_offset for both executables
which have an interpreter set and those which don't. Notably this
allows us to actually successfully execute the Loader.so binary:

courage:~ $ /usr/lib/Loader.so
You have invoked `Loader.so'. This is the helper program for programs
that use shared libraries. Special directives embedded in executables
tell the kernel to load this program.

This helper program loads the shared libraries needed by the program,
prepares the program to run, and runs it. You do not need to invoke
this helper program directly.
courage:~ $
2021-05-10 20:39:08 +02:00
Gunnar Beutner c160c6b035 Kernel: Use correct destination MAC address for multicast packets
Previously we'd incorrectly use the default gateway's MAC address.
Instead we must use destination MAC addresses that are derived from
the multicast IPv4 address.

With this patch applied I can query mDNS on a real network.
2021-05-10 17:26:17 +02:00
Brian Gianforcaro 0b7395848a Kernel: Plumb OOM propagation through Custody factory
Modify the Custody::create(..) API so it has the ability to propagate
OOM back to the caller.
2021-05-10 11:55:52 +02:00
Brian Gianforcaro 0f03960c1b Meta: Remove obsolete Kernel/.gitignore
The Kernel/.gitignore file is a remnant of the prior build system,
where the kernel.map was written directly to to the Kernel folder.
The run.sh was also under Kernel so pcap files and others would get
dropped there when running the system under qemu.

None of these situations are possible now, so lets get rid of it.
2021-05-10 10:34:10 +02:00
Mart G e0deb46723 Kernel: Traverse ext2 directories blockwise.
Instead of reading in the entire contents of a directory into a large
buffer, we can iterate block by block. This only requires a small
buffer.

Because directory entries are guaranteed to never span multiple blocks
we do not have to handle any edge cases related to that.
2021-05-08 20:01:08 +02:00
Liav A 49b132a92d Kernel/ACPI: Map two pages when reading the FADT
On some cases, the FADT could be on the end of a page, so if we don't
have two pages being mapped, we could easily read from a non-mapped
virtual address, which will trigger the UB sanitizer.

Also, we need to treat the FADT structure as volatile and const, as it
may change at any time, but we should not touch (write) it anyhow.
2021-05-08 19:15:54 +02:00
Liav A f7b5352af0 Kernel/HID: Don't assume that ACPI is initialized 2021-05-08 19:15:54 +02:00
Mart G 25a5e59f79 Kernel: Place ext2 dir entries so they don't span multiple blocks
Ext2 dir entries spanning multiple blocks are not allowed.
If they do occur they are flagged as corrupt by e2fsck for example.
2021-05-08 15:25:50 +02:00
r-paiva 293a5c2b49 Kernel-VFS: Fixed kernel crash if parent custody is null
In VFS::rename, if new_path is equal to '/', then, parent custody is
set to null.
VFS::rename would then use parent custody without checking it first.

Fixed VFS::rename to check both old and new path parent custody
before actually using them.
2021-05-08 15:22:47 +02:00
Mart G cab6155254 Kernel: Allow Ext2FSInode::write_bytes calls with a byte count of zero
write_bytes is called with a count of 0 bytes if a directory is being
deleted, because in that case even the . and .. pseudo directories are
getting removed. In this case write_bytes is now a no-op.

Before write_bytes would fail because it would check to see if there
were any blocks available to write in (even though it wasn't going to
write in them anyway).

This behaviour was uncovered because of a recent change where
directories are correctly reduced in size. Which in this case results in
all the blocks being removed from the inode, whereas previously there
would be some stale blocks around to pass the check.
2021-05-07 21:11:55 +02:00
Mart G bfce328ade Kernel: Set unused block pointers in ext2 inodes to zero
e2fsck considers all blocks reachable through any of the pointers in
m_raw_inode.i_block as part of this inode regardless of the value in
m_raw_inode.i_size. When it finds more blocks than the amount that
is indicated by i_size or i_blocks it offers to repair the filesystem
by changing those values. That will actually cause further corruption.
So we must zero all pointers to blocks that are now unused.
2021-05-07 20:13:00 +02:00
Gunnar Beutner f999d5a91a Kernel: Limit the number of in-flight packet buffers
This fixes an OOM when hitting the VM with lots of UDP packets.

fixes #6907
2021-05-07 16:00:07 +02:00
Brian Gianforcaro 7463cbdbdb Kernel: Move cpu sample perf event to PerformanceManager 2021-05-07 15:35:23 +02:00
Brian Gianforcaro d7089a0417 Kernel: Move process exit perf events to PerformanceManager 2021-05-07 15:35:23 +02:00
Brian Gianforcaro 8bf4201f50 Kernel: Move process creation perf events to PerformanceManager 2021-05-07 15:35:23 +02:00
Brian Gianforcaro ccdcb6a635 Kernel: Add PerformanceManager static class, move perf event APIs there
The current method of emitting performance events requires a bit of
boiler plate at every invocation, as well as having to ignore the
return code which isn't used outside of the perf event syscall. This
change attempts to clean that up by exposing high level API's that
can be used around the code base.
2021-05-07 15:35:23 +02:00
Mart G 6e641fadfa
Kernel: Resize Ext2FSInode when writing directory contents (#6897)
Ext2 directory contents are stored in a linked list of ext2_dir_entry
structs. There is no sentinel value to determine where the list ends.
Instead the list fills the entirety of the allocated space for the
inode.

Previously the inode was not correctly resized when it became smaller.
This resulted in stale data being interpreted as part of the linked list
of directory entries.
2021-05-06 17:53:59 +02:00
Gunnar Beutner 9213d1e926 Kernel: Truncate UDP packets on read
When reading UDP packets from userspace with recvmsg()/recv() we
would hit a VERIFY() if the supplied buffer is smaller than the
received UDP packet. Instead we should just return truncated data
to the caller.

This can be reproduced with:

    $ dd if=/dev/zero bs=1k count=1 | nc -u 192.168.3.190 68
2021-05-06 08:49:35 +02:00
Spencer Dixon 0f89e47a1a
Kernel: Allow remapping Caps Lock to Control (#6883)
We use a global setting to determine if Caps Lock should be remapped to
Control because we don't care how keyboard events come in, just that they
should be massaged into different scan codes.

The `proc` filesystem is able to manipulate this global variable using
the `sysctl` utility like so:

```
# sysctl caps_lock_to_ctrl=1
```
2021-05-05 23:10:56 +02:00
Sergey Bugaev 78459b92d5 Kernel: Implement IP multicast support
An IP socket can now join a multicast group by using the
IP_ADD_MEMBERSHIP sockopt, which will cause it to start receiving
packets sent to the multicast address, even though this address does
not belong to this host.
2021-05-05 21:16:17 +02:00
Spencer Dixon 2156c728cd
Kernel: Fix writes to ProcFS (#6879)
When using `sysctl` you can enable/disable values by writing to the
ProcFS. Some drift must have occured where writing was failing due to
a missing `set_mtime` call. Whenever one `write`'s a file the modified
time (mtime) will be updated so we need to implement this interface in
ProcFS.
2021-05-05 21:07:13 +02:00
Brian Gianforcaro 11306d7121
Kernel: Modify TimeManagement::current_time(..) API so it can't fail. (#6869)
The fact that current_time can "fail" makes its use a bit awkward.
All callers in the Kernel are trusted besides syscalls, so assert
that they never get there, and make sure all current callers perform
validation of the clock_id with TimeManagement::is_valid_clock_id().

I have fuzzed this change locally for a bit to make sure I didn't
miss any obvious regression.
2021-05-05 18:51:06 +02:00
Brian Gianforcaro 64b4e3f34b
Kernel: Add Processor::is_bootstrap_processor() function, and use it. (#6871)
The variety of checks for Processor::id() == 0 could use some assistance
in the readability department. This change adds a new function to
represent this check, and replaces the comparison everywhere it's used.
2021-05-05 18:48:26 +02:00
Tom 72a61fe137 Kernel: Remove shadowing member variable from FileDescriptionBlocker
FileDescriptionBlocker::m_should_block was shadowing the parent's
FileBlocker::m_should_block variable, which would cause should_block()
to return the wrong value.

Found by @gunnarbeutner
2021-05-05 18:44:40 +02:00
Tom ec27cbbb2a Kernel: Store whether a thread is the idle thread in Thread directly
This solves a problem where checking whether a thread is an idle
thread may require iterating all processors if it is not the idle
thread of the current processor.
2021-05-04 16:44:02 +02:00
Brian Gianforcaro 35bb8ab4db Kernel: Return one kernel frame from procfs$tid_stack for normal users.
Previously we would return a 0xdeadc0de frame for every kernel frame
in the real kernel stack when an non super-user issued the request.
This isn't useful, and just produces visual clutter in tools which
attempt to symbolize stacks.
2021-05-04 10:57:39 +02:00
Brian Gianforcaro 869becc944 Kernel: Remove unused function ProcFS::add_sys_string 2021-05-04 10:57:39 +02:00
Brian Gianforcaro 9b5c137f46 Kernel: Remove unused header includes from ProcFS.cpp 2021-05-04 10:57:39 +02:00
Brian Gianforcaro 4bf9b399f7 Kernel: Remove unused header includes from various files.
Found while browsing code with CLion.
2021-05-03 16:03:17 +02:00
Brian Gianforcaro 65138171f9 Kernel: Mark AsyncBlockDeviceRequest + AnonymousVMObject as final
Mark final to aid in de-virtualization since they are not currently
derived from.
2021-05-03 16:03:17 +02:00
Gunnar Beutner 6990ab41c8 Kernel: Fix some 64-bit portability issues 2021-05-03 08:42:39 +02:00
Spencer Dixon 27bfb01f25 Kernel: Fix ProcFS for non-process backed sub dirs
While hacking on `sysctl` an issue in ProcFS was making me unable to
read/write from `/proc/sys/XXX`. Some directories in the ProcFS are not
actually backed by a process and need to return `nullptr` so callbacks
get properly set. We now do an explicit check for the parent to ensure
it's one that is PID-based.
2021-05-02 19:21:42 +02:00
Hendiadyoin1 effdd76bb2 Kernel: Remove outdated UBSan comments
The triple-fault issue has long been fixed
2021-05-02 16:18:44 +02:00
Brian Gianforcaro 234c6ae32d Kernel: Change Inode::{read/write}_bytes interface to KResultOr<ssize_t>
The error handling in all these cases was still using the old style
negative values to indicate errors. We have a nicer solution for this
now with KResultOr<T>. This change switches the interface and then all
implementers to use the new style.
2021-05-02 13:27:37 +02:00
Gunnar Beutner 0a94b4233f Kernel: Don't use ref_count() in MasterPTY::close()
With the recent fixes to how close() gets called this is not
necessary anymore.
2021-05-01 23:04:40 +02:00
Gunnar Beutner 6cf59b6ae9 Everywhere: Turn #if *_DEBUG into dbgln_if/if constexpr 2021-05-01 21:25:06 +02:00
Idan Horowitz 8293b22361 Kernel: Handle both shift keys being pressed and then released
Our current implementation does not work in the special case in which
both shift keys are pressed, and then only one of the keys is released,
as this would result in writing lower case letters, instead of the
expected upper case letters.

This commit fixes that by keeping track of the amount of shift keys
that are pressed (instead of if any are at all), and only switching to
the unshifted keymap once all of them are released.
2021-05-01 21:08:23 +02:00
Sahan Fernando bd563f0b3c Kernel: Make processes start with a 16-byte-aligned stack 2021-05-01 20:08:35 +02:00
Gunnar Beutner cf7df418ed Kernel: Make sure we read all packets
The previous patch already helped with this, however my idea of only
reading a few packets didn't work and we'd still sometimes end up not
receiving any more packets from the E1000 interface.

With this patch applied my NIC seems to receive packets just fine, at
least for now.
2021-05-01 20:08:08 +02:00
Valtteri Koskivuori 370231c05c Kernel: Expose minor device numbers for keyboard and mouse
A fix for two FIXMEs, and paving the way for multi-keyboard/mouse
support, I guess.
2021-05-01 20:07:11 +02:00
Brian Gianforcaro f05086a5d2 Kernel: Harden Ext2FileSystem Vector usage against OOM. 2021-05-01 09:10:30 +02:00
Brian Gianforcaro 2ee1731966 Kernel: Harden Process Vector usage against OOM. 2021-05-01 09:10:30 +02:00
Brian Gianforcaro ee84b8a845 Kernel: Harden DevFS Vector usage against OOM.
The dance here is not complicated, but it is something that should
be taken note of. Since we append to both lists, we don't want to
orphan the new Inode in the m_links/m_subfolders Vector in the event
that the append to m_parent_fs.m_nodes fails.
2021-05-01 09:10:30 +02:00
Brian Gianforcaro a678851b41 Kernel: Harden sys$setgroups Vector usage against OOM 2021-05-01 09:10:30 +02:00
Brian Gianforcaro f0568bff9b Kernel: Harden Socket Vector usage against OOM 2021-05-01 09:10:30 +02:00
Brian Gianforcaro e8d6d478c4 Kernel: Harden LocalSocket Vector usage against OOM. 2021-05-01 09:10:30 +02:00
Andreas Kling 51f88cb00d Kernel/IPv4: Unbreak raw socket (port allocation failing is OK)
Raw sockets don't need a local port, so we shouldn't fail operations
if allocation yields an ENOPROTOOPT.

I'm not in love with the factoring here, just patching up the bug.
2021-05-01 00:03:33 +02:00
Gunnar Beutner e0ac611a08 Kernel: Tear down connections when we receive an RST packet 2021-04-30 23:11:56 +02:00
Gunnar Beutner c03cbf83ab Kernel: Record MAC addresses for incoming IPv4 packets
This way we don't have to do ARP just to send packets back to
an address which just sent us a packet.
2021-04-30 23:11:56 +02:00
Gunnar Beutner fb2ad94195 Kernel: Remove socket from the listener's accept list when it is closed
Without this patch we end up with sockets in the listener's accept
queue with state 'closed' when doing stealth SYN scans:

Client -> Server: SYN for port 22
Server -> Client: SYN/ACK
Client -> Server: RST (i.e. don't complete the TCP handshake)
2021-04-30 23:11:56 +02:00
Gunnar Beutner 866e577f1d Kernel: Don't put closed/listener sockets into the closing_sockets list 2021-04-30 23:11:56 +02:00
Gunnar Beutner d8f92bdf96 Kernel: Avoid deadlock when trying to send packets from the NetworkTask
fixes #6758
2021-04-30 23:11:56 +02:00
Gunnar Beutner 488ee56cf7 Kernel: chmod()/chown() for PTYs should return EROFS
All the other methods already do this and this is also what OpenSSH
expects when trying to change modes/ownership for devpts files.
2021-04-30 23:10:22 +02:00
Gunnar Beutner 71f90695c2 Kernel: Implement support for PTY flags ICRNL, OPOST and ONLCR
These are used by OpenSSH. Without those flags new-lines are
all jumbled in the output.
2021-04-30 23:10:22 +02:00
Itamar 6bbd2ebf83 Kernel+LibELF: Support initializing values of TLS data
Previously, TLS data was always zero-initialized.

To support initializing the values of TLS data, sys$allocate_tls now
receives a buffer with the desired initial data, and copies it to the
master TLS region of the process.

The DynamicLinker gathers the initial TLS image and passes it to
sys$allocate_tls.

We also now require the size passed to sys$allocate_tls to be
page-aligned, to make things easier. Note that this doesn't waste memory
as the TLS data has to be allocated in separate pages anyway.
2021-04-30 18:47:39 +02:00
Itamar 373e8bcbc7 Kernel: Give a name to the Master TLS region allocation 2021-04-30 18:47:39 +02:00
Andreas Kling cd9be1733c Kernel: Make Inode::set_{a,c,m}time return KResult
This exposed some missing error propagation, which this patch also
takes care of.
2021-04-30 15:51:06 +02:00
Andreas Kling a5f385f052 Kernel: Fix bogus error codes from raw socket protocol_{send,receive}
Since these return KResultOr, we should not negate the error code.
2021-04-30 15:27:41 +02:00
Andreas Kling 71a10eb8e7 Kernel/IPv4: Propagate errors from local port allocation
Remove hacks and assumptions and make the EADDRINUSE propagate all
the way from the point of failure to the syscall layer.
2021-04-30 15:27:41 +02:00
Gunnar Beutner 3c0355a398 Kernel: Accepted socket file descriptors should not inherit flags
For example Linux accepts an additional argument for flags in accept4()
that let the user specify what flags they want. However, by default
accept() should not inherit those flags from the listener socket.
2021-04-30 11:43:19 +02:00
Gunnar Beutner 7a1d09ef1a Kernel: Closing a file descriptor should not always close the file
When there is more than one file descriptor for a file closing
one of them should not close the underlying file.

Previously this relied on the file's ref_count() but at least
for sockets this didn't work reliably.
2021-04-30 11:42:35 +02:00
Jesse Buhagiar 60cdbc9397 Kernel/LibC: Implement setreuid 2021-04-30 11:35:17 +02:00
Andreas Kling 3d4afe7614 Everywhere: "indexes" => "indices"
I've wasted a silly amount of time in the past fretting over which
of these words to use. Let's just choose one and use it everywhere. :^)
2021-04-29 22:23:52 +02:00
Andreas Kling 7ae7170d61 Everywhere: "file name" => "filename" 2021-04-29 22:16:18 +02:00
Brian Gianforcaro a8765fa673 Kernel: Harden sys$select Vector usage against OOM.
Theoretically the append should never fail as we have in-line storage
of FD_SETSIZE, which should always be enough. However I'm planning on
removing the non-try variants of AK::Vector when compiling in kernel
mode in the future, so this will need to go eventually. I suppose it
also protects against some unforeseen bug where we we can append more
than FD_SETSIZE items.
2021-04-29 20:31:15 +02:00
Brian Gianforcaro 0ca668f59c Kernel: Harden sys$munmap Vector usage against OOM.
Theoretically the append should never fail as we have in-line storage
of 2, which should be enough. However I'm planning on removing the
non-try variants of AK::Vector when compiling in kernel mode in the
future, so this will need to go eventually. I suppose it also protects
against some unforeseen bug where we we can append more than 2 items.
2021-04-29 20:31:15 +02:00
Brian Gianforcaro 569c5a8922 Kernel: Harden sys$purge Vector usage against OOM.
sys$purge() is a bit unique, in that it is probably in the systems
advantage to attempt to limp along if we hit OOM while processing
the vmobjects to purge. This change modifies the algorithm to observe
OOM and continue trying to purge any previously visited VMObjects.
2021-04-29 20:31:15 +02:00
Brian Gianforcaro b3096276bb Kernel: Harden sys$poll Vector usage against OOM. 2021-04-29 20:31:15 +02:00
Brian Gianforcaro 119b7be249 Kernel: Harden sys$execve Vector usage against OOM. 2021-04-29 20:31:15 +02:00
Brian Gianforcaro 454d2fd42a Kernel: Harden sys$readv / sys$writev Vector usage against OOM. 2021-04-29 20:31:15 +02:00
Brian Gianforcaro cd29eb7867 Kernel: Harden sys$sendmsg / sys$recvmsg Vector usage against OOM. 2021-04-29 20:31:15 +02:00
Gunnar Beutner 6288ae2c37 Kernel: Add a CMake flag to enable LTO for the kernel 2021-04-29 20:26:36 +02:00
Gunnar Beutner 55ae52fdf8 Kernel: Enable building the kernel with -flto
GCC with -flto is more aggressive when it comes to inlining and
discarding functions which is why we must mark some of the functions
as NEVER_INLINE (because they contain asm labels which would be
duplicated in the object files if the compiler decides to inline
the function elsewhere) and __attribute__((used)) for others so
that GCC doesn't discard them.
2021-04-29 20:26:36 +02:00
Justin e6401d65bd Kernel: Add MSG_PEEK support for the IPv4Socket
This commit will add MSG_PEEK support, which allows a package to be
seen without taking it from the buffer, so that a subsequent recv()
without the MSG_PEEK flag can pick it up.
2021-04-29 08:09:53 +02:00
Justin 2d098c88dc Kernel: Implement peek() function for DoubleBuffer
This allows us to "peek" into a DoubleBuffer without incrementing
the m_read_buffer_index, which is needed to implement MSG_PEEK.
2021-04-29 08:09:53 +02:00
Linus Groh 649d2faeab Everywhere: Use "the SerenityOS developers." in copyright headers
We had some inconsistencies before:

- Sometimes "The", sometimes "the"
- Sometimes trailing ".", sometimes no trailing "."

I picked the most common one (lowecase "the", trailing ".") and applied
it to all copyright headers.

By using the exact same string everywhere we can ensure nothing gets
missed during a global search (and replace), and that these
inconsistencies are not spread any further (as copyright headers are
commonly copied to new files).
2021-04-29 00:59:26 +02:00
Linus Groh 5459c744f1 Everywhere: Add missing comma between copyright year and name 2021-04-29 00:59:26 +02:00
Gunnar Beutner d9ee2c6a89 Kernel: Avoid overrunning the user-specified buffers in select() 2021-04-28 23:05:10 +02:00
Idan Horowitz d9f7b29273 Kernel: Check kernel symbol's name length matches searched name
The current implementation would only check the first name.length()
characters match, which means any kernel symbol that the provided name
is a prefix of would match, instead of the actual matching symbol.

This commit fixes that by using StringView::operator==() for the
comparison, which already checks the equality correctly.
2021-04-28 22:14:32 +02:00
Gunnar Beutner aa792062cb Kernel+LibC: Implement the socketpair() syscall 2021-04-28 14:19:45 +02:00
Brian Gianforcaro b970dbf2af Build: Provide the user with a nice message after Toolchain upgrade
Lots of people are confused by the error message you get when the
Toolchain is behind/messed up:

    'initializer-list: No such file or directory'

Before this error can happen, catch the problem at CMake configure time,
and provide them with an actionable error message.
2021-04-27 13:07:04 +02:00
Brian Gianforcaro 2ef93a3c07 Build: Use variables when concatenating Toolchain paths.
Make this stuff a bit easier to maintain by using the
root level variables to build up the Toolchain paths.

Also leave a note for future editors of BuildIt.sh to
give them warning about the other changes they'll need
to make.
2021-04-27 13:07:04 +02:00
Gunnar Beutner bf703ee553 Kernel: Move PCI vendor and device IDs into Kernel/PCI/IDs.h 2021-04-27 11:36:24 +02:00
Gunnar Beutner eaf8fc90e7 Kernel: Avoid resetting the IRQ mask for E1000 on each interrupt 2021-04-27 11:36:24 +02:00
Gunnar Beutner 897f001076 Kernel: Add logging for E1000 RX buffer overrun 2021-04-27 11:36:24 +02:00
Gunnar Beutner fa434305a7 Kernel: Use macros instead of hard-coded magic values 2021-04-27 11:36:24 +02:00
Gunnar Beutner addddb4880 Kernel: Make sure the E1000 network adapter keeps receiving packets
Previously the E1000 network adapter would stop receiving further
packets when an RX buffer overrun occurred. This was the case
when connecting the adapter to a real network where enough broadcast
traffic caused the buffer to be full before the kernel had a chance
to clear the RX buffer.
2021-04-27 11:36:24 +02:00
Jelle Raaijmakers b630e39fbb Kernel: Check futex command if realtime clock is used 2021-04-27 09:19:55 +02:00
Jelle Raaijmakers c3cff7d70a Kernel: Simplify BlockTimeout constructor 2021-04-27 09:19:55 +02:00
Gunnar Beutner 4e6a26cbd2 Kernel: Silence a few more network dbgln()s 2021-04-27 08:59:02 +02:00
Gunnar Beutner 659507696c Kernel: Fix incorrect argument for thread_exit events 2021-04-26 23:26:58 +02:00
Gunnar Beutner 1c02848e54 Kernel: Log thread exits for global profiles 2021-04-26 23:26:58 +02:00
Gunnar Beutner afeee35cbf Kernel: Avoid calling characters() where not necessary 2021-04-26 23:26:58 +02:00
Gunnar Beutner 4a40caa020 Kernel: Use the correct union member for unmap events 2021-04-26 23:26:58 +02:00
Liav A 161a8ea062 Kernel: Ensure IO and memory accesses are allowed for IDE channels 2021-04-26 18:44:06 +02:00
Liav A 05510e3994 Kernel/Storage: Make the IDEChannel design more robust
The overall design is the same, but we change a few things,
like decreasing the amount of blocking forever loops. The goal
is to ensure the kernel won't hang forever when dealing with
buggy hardware.
Also, we reset the channel when initializing it, just in case the
hardware was in bad state before we start use it.
2021-04-26 18:44:06 +02:00
Liav A ecf897f7c4 Kernel/PCI: Add helpers to enable IO and memory accesses 2021-04-26 18:44:06 +02:00