Commit Graph

4370 Commits

Author SHA1 Message Date
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