Commit Graph

4447 Commits

Author SHA1 Message Date
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
Liav A
a2a7986527 Kernel/Storage: Fix sending IOGroup parameters 2021-04-26 18:44:06 +02:00
Gunnar Beutner
a85d111cd1 Kernel: Respond to packets sent to the directed broadcast address
The last IP address in an IPv4 subnet is considered the directed
broadcast address, e.g. for 192.168.3.0/24 the directed broadcast
address is 192.168.3.255. We need to consider this address as
belonging to the interface.

Here's an example with this fix applied, SerenityOS has 192.168.3.190:

[gunnar@nyx ~]$ ping -b 192.168.3.255
WARNING: pinging broadcast address
PING 192.168.3.255 (192.168.3.255) 56(84) bytes of data.
64 bytes from 192.168.3.175: icmp_seq=1 ttl=64 time=0.950 ms
64 bytes from 192.168.3.188: icmp_seq=1 ttl=64 time=2.33 ms
64 bytes from 192.168.3.46: icmp_seq=1 ttl=64 time=2.77 ms
64 bytes from 192.168.3.41: icmp_seq=1 ttl=64 time=4.15 ms
64 bytes from 192.168.3.190: icmp_seq=1 ttl=64 time=29.4 ms
64 bytes from 192.168.3.42: icmp_seq=1 ttl=64 time=30.8 ms
64 bytes from 192.168.3.55: icmp_seq=1 ttl=64 time=31.0 ms
64 bytes from 192.168.3.30: icmp_seq=1 ttl=64 time=33.2 ms
64 bytes from 192.168.3.31: icmp_seq=1 ttl=64 time=33.2 ms
64 bytes from 192.168.3.173: icmp_seq=1 ttl=64 time=41.7 ms
64 bytes from 192.168.3.43: icmp_seq=1 ttl=64 time=47.7 ms
^C
--- 192.168.3.255 ping statistics ---
1 packets transmitted, 1 received, +10 duplicates, 0% packet loss,
time 0ms, rtt min/avg/max/mdev = 0.950/23.376/47.676/16.539 ms
[gunnar@nyx ~]$
2021-04-26 18:39:59 +02:00
Gunnar Beutner
eb798d5538 Kernel+Profiler: Improve profiling subsystem
This turns the perfcore format into more a log than it was before,
which lets us properly log process, thread and region
creation/destruction. This also makes it unnecessary to dump the
process' regions every time it is scheduled like we did before.

Incidentally this also fixes 'profile -c' because we previously ended
up incorrectly dumping the parent's region map into the profile data.

Log-based mmap support enables profiling shared libraries which
are loaded at runtime, e.g. via dlopen().

This enables profiling both the parent and child process for
programs which use execve(). Previously we'd discard the profiling
data for the old process.

The Profiler tool has been updated to not treat thread IDs as
process IDs anymore. This enables support for processes with more
than one thread. Also, there's a new widget to filter which
process should be displayed.
2021-04-26 17:13:55 +02:00
Gunnar Beutner
ff8c664931 Kernel: Increase max frame count to 64
Even just profiling Piano hits the previous limit.
2021-04-26 17:13:55 +02:00
Gunnar Beutner
64639de4d2 Kernel: Stop walking the stack when we encounter return address 0 2021-04-26 17:13:55 +02:00
Linus Groh
dbe72fd962 Everywhere: Remove empty line after function body opening curly brace 2021-04-25 20:20:00 +02:00
Brian Gianforcaro
8d6e9fad40 Kernel: Remove the now defunct LOCKER(..) macro. 2021-04-25 09:38:27 +02:00
Brian Gianforcaro
0d5827f865 Kernel: Remove the now defunct RESTORE_LOCK(..) macro. 2021-04-25 09:38:27 +02:00
Brian Gianforcaro
04156d53ca Kernel: Utilize AK::SourceLocation for LOCK_DEBUG instrumentation.
The previous `LOCKER(..)` instrumentation only covered some of the
cases where a lock is actually acquired. By utilizing the new
`AK::SourceLocation` functionality we can now reliably instrument
all calls to lock automatically.

Other changes:
- Tweak the message in `Thread::finalize()` which dumps leaked lock
  so it's more readable and includes the function information that is
  now available.

- Make the `LOCKER(..)` define a no-op, it will be cleaned up in a
  follow up change.
2021-04-25 09:38:27 +02:00
Brian Gianforcaro
c248bbc7fd Kernel: Add lock_count to procfs$all when LOCK_DEBUG is enabled. 2021-04-25 09:38:27 +02:00
Brian Gianforcaro
7481789eac Kernel: Fix LOCK_DEBUG feature to work again
- UBSAN detected cases where we were calling thread->holding_lock(..)
  but current_thread was nullptr.

- Fix Lock::force_unlock_if_locked to not pass the correct ref delta to
  holding_lock(..).
2021-04-25 09:38:27 +02:00
Jelle Raaijmakers
54f5b1346c Kernel: Support null act argument for sigaction syscall
Userspace can provide a null argument for the `act` argument to the
`sigaction` syscall to not set any new behavior. This is described
here:

https://pubs.opengroup.org/onlinepubs/007904875/functions/sigaction.html

Without this fix, the `copy_from_user(...)` invocation on `user_act`
fails and makes the syscall return early.
2021-04-24 23:00:28 +02:00
Andreas Kling
b91c49364d AK: Rename adopt() to adopt_ref()
This makes it more symmetrical with adopt_own() (which is used to
create a NonnullOwnPtr from the result of a naked new.)
2021-04-23 16:46:57 +02:00
Maciej Zygmanowski
6efcc2fc99 Kernel: Don't allow to kill kernel processes
The protection was only for SIGKILL before.
2021-04-23 13:26:02 +02:00
Linus Groh
ebdeed087c Everywhere: Use linusg@serenityos.org for my copyright headers 2021-04-22 22:51:19 +02:00
Brian Gianforcaro
dc0fc16981 Everywhere: Use bgianf@serenityos.org for my copyright attribution 2021-04-22 21:15:54 +02:00
Brian Gianforcaro
1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00
Andreas Kling
f4eff7df8f Kernel: Convert String::format() => String::formatted() 2021-04-21 23:49:02 +02:00
Brian Gianforcaro
ce5a308f24 Kernel: Annotate more AsyncDeviceRequest API's with [[nodiscard]] 2021-04-21 19:31:49 +02:00
Brian Gianforcaro
033b287635 Kernel: Make AsyncDeviceRequest sub-req management alloc free
The previous implementation could allocate on insertion into the completed / pending
sub request vectors. There's no reason these can't be intrusive lists instead.

This is a very minor step towards improving the ability to handle OOM, as tracked by #6369
It might also help improve performance on the IO path in certain situations.
I'll benchmark that later.
2021-04-21 19:31:49 +02:00
Ali Mohammad Pur
468ac11f29 Meta: Add an option to precompile some very common AK headers
Until we get the goodness that C++ modules are supposed to be, let's try
to shave off some parse time using precompiled headers.
This commit only adds some very common AK headers, only to binaries,
libraries and the kernel (tests are not covered due to incompatibility
with AK/TestSuite.h).
This option is on by default, but can be disabled by passing
`-DPRECOMPILE_COMMON_HEADERS=OFF` to cmake, which will disable all
header precompilations.
This makes the build about 30 seconds faster on my machine (about 7%).
2021-04-21 14:29:46 +02:00
Andreas Kling
c6b7b98b64 Kernel: Don't consider kernel memory regions for syscall origin check
We should never enter the syscall handler from a kernel address.
2021-04-20 23:38:27 +02:00
Gunnar Beutner
db3fd11646 Kernel: Remove requirement for the thread entitlement for the futex syscall
GCC inserts calls to pthread_mutex_lock when compiling C++ code with
threads enabled.
2021-04-20 21:08:17 +02:00
Andreas Kling
3f5c934ea6 Ext2FS: Put bg_used_dirs_count debug logging behind EXT2_DEBUG 2021-04-20 15:08:56 +02:00
Idan Horowitz
dbc13e1ea2 Kernel: Stop treating port 0 (ephemeral auto bind) as a privileged port
Binding to port 0 is used to signal to listen() to bind to any port
that is available. (in serenity's case, to the port range of 32768 to
60999, which are not privileged ports)
2021-04-19 23:28:02 +02:00
Brian Gianforcaro
4ed682aebc Kernel: Add a syscall to clear the profiling buffer
While profiling all processes the profile buffer lives forever.
Once you have copied the profile to disk, there's no need to keep it
in memory. This syscall surfaces the ability to clear that buffer.
2021-04-19 18:30:37 +02:00
Idan Horowitz
aaf3d26dae Kernel: Add kernel command line flag to disable VirtIO support
This command line flag can be used to disable VirtIO support on
certain configurations (native windows) where interfacing with
virtio devices can cause qemu to freeze.
2021-04-18 22:06:42 +02:00
Andreas Kling
24dcd99e4b Kernel: Add magic key combo (Alt+Shift+F12) to dump scheduler state
Pressing this combo will dump a list of all threads and their state
to the debug console.

This might be useful to figure out why the system is not responding.
2021-04-18 20:00:10 +02:00
FalseHonesty
3123ffb19d Kernel: Add ptrace commands for reading/writing the debug registers
This adds PT_PEEKDEBUG and PT_POKEDEBUG to allow for reading/writing
the debug registers, and updates the Kernel's debug handler to read the
new information from the debug status register.
2021-04-18 17:02:40 +02:00
FalseHonesty
97a4c627cb Kernel: Add debug register handling
This patch adds functions to read/write from the debug registers,
and implements storing/loading them across context switches.
2021-04-18 17:02:40 +02:00
Luke
c84107a1ab Kernel: Add boot argument to disable the UHCI Controller
Helps with bare metal debugging, as we can't be sure our implementation
will work with a given machine.

As reported by someone on Discord, their machine hangs when we attempt
the dummy transfer.
2021-04-18 17:01:22 +02:00