Commit graph

8432 commits

Author SHA1 Message Date
Idan Horowitz 785c9d5c2b Kernel: Add support for TCP window size scaling
This should allow us to eventually properly saturate high-bandwidth
network links when using TCP, once other nonoptimal parts of our
network stack are improved.
2023-12-26 21:36:49 +01:00
Idan Horowitz 2c51ff763b Kernel: Properly report receive window size in sent TCP packets
Instead of lying and claiming we always have space left in our receive
buffer, actually report the available space.

While this doesn't really affect network-bound workloads, it makes a
world of difference in cpu/disk-bound ones, like git clones. Resulting
in a considerable speed-up, and in some cases making them work at all.
(instead of the sender side hanging up the connection due to timeouts)
2023-12-26 21:36:49 +01:00
Idan Horowitz 519214697b Kernel: Mark sys$getsockname as not needing the big process lock
This syscall does not access any big process lock protected resources.
2023-12-26 19:20:21 +01:00
Idan Horowitz ed5406e47d Kernel: Mark sys$getpeername as not needing the big process lock
This syscall does not access any big process lock protected resources.
2023-12-26 19:20:21 +01:00
Idan Horowitz 24a60c5a10 Kernel: Mark sys$ioctl as not needing the big process lock
This syscall does not access any big process lock protected resources.
2023-12-26 19:20:21 +01:00
Idan Horowitz d63667dbf1 Kernel: Mark sys$kill_thread as not needing the big process lock
This syscall does not access any big process lock protected resources.
2023-12-26 19:20:21 +01:00
Idan Horowitz b44628c1fb Kernel: Mark sys$join_thread as not needing the big process lock
This syscall does not access any big process lock protected resources.
2023-12-26 19:20:21 +01:00
Idan Horowitz 82e6090f47 Kernel: Mark sys$detach_thread as not needing the big process lock
This syscall does not access any big process lock protected resources.
2023-12-26 19:20:21 +01:00
Idan Horowitz b49a0e2c61 Kernel: Mark sys$create_thread as not needing the big process lock
Now that the master TLS region is spinlock protected, this syscall does
not access any big process lock protected resources.
2023-12-26 19:20:21 +01:00
Idan Horowitz 6a4b93b3e0 Kernel: Protect processes' master TLS with a fine-grained spinlock
This moves it out of the scope of the big process lock, and allows us
to wean some syscalls off it, starting with sys$allocate_tls.
2023-12-26 19:20:21 +01:00
Idan Horowitz 863e8c30ad Kernel: Ensure sockets_by_tuple table entry is up to date on connect
Previously we would incorrectly handle the (somewhat uncommon) case of
binding and then separately connecting a tcp socket to a server, as we
would register the socket during the manual bind(2) in the sockets by
tuple table, but our effective tuple would then change as the result of
the connect updating our target peer address. This would result in the
the entry not being removed from the table on destruction, which could
lead to a UAF.

We now make sure to update the table entry if needed during connects.
2023-12-26 18:36:43 +01:00
Idan Horowitz da2f33df82 Kernel: Stop modifying peer address/port in sendto on a TCP socket
POSIX (rightfully so) specifies that the sendto address argument is
ignored in connection-oriented protocols.

The TCPSocket also assumed the peer address may not change post-connect
and would trigger a UAF in sockets_by_tuple() when it did.
2023-12-26 18:36:43 +01:00
Idan Horowitz dca5c71e53 Kernel: Stub out getsockopt for the SO_REUSEADDR option
We currently discard setsockopt for SO_REUSEADDR, so to ensure
consistency, support getsockopt as well.
2023-12-24 22:22:58 +01:00
Idan Horowitz 545f4b6cc1 Kernel: Properly support the SO_BROADCAST socket option
POSIX requires that broadcast sends will only be allowed if the
SO_BROADCAST socket option was set on the socket.
Also, broadcast sends to protocols that do not support broadcast (like
TCP), should always fail.
2023-12-24 22:22:58 +01:00
Idan Horowitz a49b7e92eb Kernel: Shrink instead of expand sigaltstack range to page boundaries
Since the POSIX sigaltstack manpage suggests allocating the stack
region using malloc(), and many heap implementations (including ours)
store heap chunk metadata in memory just before the vended pointer,
we would end up zeroing the metadata, leading to various crashes.
2023-12-24 16:11:35 +01:00
Idan Horowitz 4c6fd454d0 Kernel: Add MM helper for shrinking a virtual range to page boundaries 2023-12-24 16:11:35 +01:00
Shannon Booth e2e7c4d574 Everywhere: Use to_number<T> instead of to_{int,uint,float,double}
In a bunch of cases, this actually ends up simplifying the code as
to_number will handle something such as:

```
Optional<I> opt;
if constexpr (IsSigned<I>)
    opt = view.to_int<I>();
else
    opt = view.to_uint<I>();
```

For us.

The main goal here however is to have a single generic number conversion
API between all of the String classes.
2023-12-23 20:41:07 +01:00
Idan Horowitz f972eda7ed Kernel: Mark cloned volatile purgeable AnonymousVMOjects as purged
Our existing AnonymousVMObject cloning flow contains an optimization
wherein purgeable VMObjects which are marked volatile during the clone
are created as a new zero-filled VMObject (as if it was purged), which
lets us skip the expensive COW process.

Unfortunately, one crucial part was missing: Marking the cloned region
as purged, (which is the value returned from madvise when unmarking the
region as volatile) so the userland logic was left unaware of the
effective zero-ing of their memory region, resulting in odd behaviour
and crashes in places like our malloc's large allocation support.
2023-12-22 10:57:59 +01:00
Sönke Holz ac79ab0b45 Kernel/riscv64: Specify correct alignment for FPUState struct
The signal handling code (and possibly other code as well) expects this
struct to have an alignment of 16 bytes, as it pushes this struct on the
stack.
2023-12-16 18:21:03 +01:00
Idan Horowitz 662143e0a9 Kernel: Resolve deadlock in MasterPTY due to mutex in spinlock scope
MasterPTY::read called DoubleBuffer::read which takes a mutex (which
may block) while holding m_slave's spinlock. If it did block, and was
later rescheduled on a different physical CPU, we would deadlock on
re-locking m_slave inside the unblock callback. (Since our recursive
spinlock implementation is processor based and not process based)
2023-12-16 12:55:41 +01:00
Idan Horowitz ab06a76920 Kernel: Resolve lock-inversion based deadlock in MasterPTY & BlockSet
MasterPTY's double buffer unblock callback would take m_slave's
spinlock and then call evaluate_block_conditions() which would take
BlockerSet's spinlock, while on the other hand, BlockerSet's
add_blocker would take BlockerSet's spinlock, and then call
should_add_blocker, which would call unblock_if_conditions_are_met,
which would then call should_unblock, which will finally call
MasterPTY::can_read() which will take m_slave's spinlock.
Resolve this by moving the call to evaluate_block_conditions() out of
the scope of m_slave's spinlock, as there's no need to hold the lock
while calling it anyways.
2023-12-16 12:55:41 +01:00
Idan Horowitz 1bea780a7f Kernel: Reject loading ELF files with no loadable segments
If there's no loadable segments then there can't be any code to execute
either. This resolves a crash these kinds of ELF files would cause from
the directly following VERIFY statement.
2023-12-15 21:36:25 +01:00
Idan Horowitz 2a6b492c7f Kernel: Copy over TLS region size and alignment when forking
Previously we would unintentionally leave them zero-initialized,
resulting in any threads created post fork (but without execve) having
invalid thread local storage pointers stored in their FS register.
2023-12-15 21:36:03 +01:00
Idan Horowitz b35ebd31a4 Kernel: Include FS & GS in process crash register dump
This is helpful when debugging thread-local-storage related crashes.
2023-12-15 21:36:03 +01:00
Sönke Holz 2b44c4c3f7 Kernel/riscv64: Make the kernel compilable using GCC
This commit adds all necessary includes, so all functions are properly
declared.
PCI.cpp is moved to PCI/Initializer.cpp, as that matches the header
path.
2023-12-14 09:02:15 -07:00
Sönke Holz 4cd1e4d69e Kernel/riscv64: Use ALWAYS_INLINE instead of inline in CSR header
This fixes building the kernel with `-Og` when using GCC.
2023-12-14 09:02:15 -07:00
Sönke Holz 78419e858d Kernel/Graphics: Don't use [[gnu::packed]] on Bochs VGA structs
The `[[gnu::packed]]` attribute apparently lowered the required
alignment of the structs, which caused the compiler to generate two
1 byte loads/stores on RISC-V. This caused the kernel to read/write
incorrect values, as the device only seems to accept 2 byte operations.
2023-12-11 14:46:06 -07:00
Sönke Holz e4019ba9dc Kernel: Make CrashHandler more useful before init_stage2
Display some helpful information about crashes even before the first
process is started.
2023-12-09 22:36:28 +01:00
Sönke Holz 8e5f201e59 Kernel/riscv64: Add .{ro,unmap}_after_init sections to linker script
`MM.protect_kernel_image` would otherwise make the contents of these
sections read-only, as they were for some reason placed before `.data`
and after the start of `.text`.
2023-12-08 12:36:38 -07:00
Andreas Kling 34ae39478a Kernel/SysFS: Fix bizarre mode bits for directories in SysFS
Not sure what led to 0445 being used here, but let's use 0755.
2023-12-01 20:46:21 +01:00
Ali Mohammad Pur b545427d53 Kernel: Make RAMFS pass along the inode type when traversing as a dir
RAMFS was passing 0, which lead to the userspace seeing all entries as
DT_UNKNOWN when iterating over the directory contents.
To repro prior to this commit, simply check `echo /tmp/*/`.
2023-12-01 20:46:16 +01:00
Daniel Bertalan 45d81dceed Everywhere: Replace ElfW(type) macro usage with Elf_type
This works around a `clang-format-17` bug which caused certain usages to
be misformatted and fail to compile.

Fixes #8315
2023-12-01 10:02:39 +02:00
Maja Kądziołka 3ed48e6008 Kernel/HID: Don't refer to a USB mouse as PS2
Following 77441079dd, the code in Kernel/Devices/HID/MouseDevice.cpp
is used by both USB and PS2 rodents. Make sure not to emit misleading
debug messages that could suggest that a USB mouse is a PS/2 one.
2023-11-30 13:30:12 -07:00
Sönke Holz 173a085e48 Kernel: Only unmap prekernel on x86_64
Other arches don't use the prekernel, so don't try to unmap it on
non-x86 platforms.
For some reason, this didn't cause aarch64 to crash, but on riscv64 this
would cause a panic.
2023-11-30 13:14:18 -07:00
Sönke Holz 3852f4f136 Kernel: Only try to use VGA text mode on x86_64 2023-11-30 13:13:54 -07:00
Sönke Holz 93b6111f47 Kernel/aarch64: Unbreak RPi framebuffer boot console
342c707 introduced an additional check whether this flag is set
before trying to use the multiboot-provided framebuffer console.
2023-11-30 13:13:54 -07:00
Liav A c3d28d7f5a Kernel: Switch init boot argument to "/init"
This doesn't affect system functionality, but somewhat reduces the
reliance on complicated hardcoded paths. It also allows the user to
simply link /init (which is normally a symbolic link) to another program
to run it instead of SystemServer as the default option.
2023-11-27 09:29:05 -07:00
Liav A 4a50c01c50 Kernel: Print to the log what is the first user process & its arguments 2023-11-27 09:29:05 -07:00
Liav A 5dba1dedb7 Kernel: Don't warn when running dynamically-linked ELF without PT_INTERP
We could technically copy the dynamic loader to other path and run it
from there, so let's not assume paths.
If the user is so determined to do such thing, then a warning is quite
meaningless.
2023-11-27 09:27:34 -07:00
Liav A 93e172895a Kernel: Add /sys/kernel/request_panic node to simulate a kernel panic
When writing to /sys/kernel/request_panic it will do a kernel panic.
Trying to truncate the node will result in kernel panic with a slightly
different message.
2023-11-27 09:24:52 -07:00
Idan Horowitz 743a9e9ebf Kernel: Stop including the ethernet header in LoopbackAdapter's mtu
The networking subsystem currently assumes all adapters are Ethernet
adapters, including the LoopbackAdapter, so all packets are pre-pended
with an Ethernet Frame header. Since the MTU must not include any
overhead added by the data-link (Ethernet in this case) or physical
layers, we need to subtract it from the MTU.

This fixes a kernel panic which occurs when sending a packet that is at
least 65523 bytes long through the loopback adapter, which results in
the kernel "receiving" a packet which is larger than the support MTU
out the other end. (As the actual final size was increased by the
addition of the ethernet frame header)
2023-11-25 16:34:38 +01:00
Idan Horowitz 16a53c811e Kernel: Treat a backlog argument of 0 to listen() as if it was 1
As per POSIX, the behavior of listen() with a backlog value of 0 is
implementation defined: "A backlog argument of 0 may allow the socket
to accept connections, in which case the length of the listen queue may
be set to an implementation-defined minimum value."
Since creating a socket that can't accept any connections seems
relatively useless, and as other platforms (Linux, FreeBSD, etc) chose
to support accepting connections with this backlog value, support it as
well by normalizing it to 1.
2023-11-25 16:34:38 +01:00
Sönke Holz 185715d832 Kernel/riscv64: Generate Kernel.bin from kernel ELF
This is necessary for being able to use the qemu `-kernel` option.
The QEMU virt machine uses OpenSBI's FW_DYNAMIC feature to pass
the kernel entry address, which is the virtual entry point address
specified in the kernel ELF. If we instead `objcopy` the kernel into a
raw binary, OpenSBI will jump to the physical kernel load address, which
is what we want it to do.
2023-11-24 08:52:02 -07:00
Sönke Holz fa39a57474 Kernel/riscv64: Add missing input sections to linker script
The linker would otherwise put those sections after
`end_of_kernel_image`.
2023-11-24 08:50:19 -07:00
Blake Smith e346331424 Kernel/FS: Fix check-then-act concurrency bug in FileSystem/Inode
When the FileSystem does a sync, it gathers up all the inodes with
dirty metadata into a vector. The inode mutex is not held while
checking the inode dirty bit, which can lead to a kernel panic
due to concurrent inode modifications.

Fixes: #21796
2023-11-20 09:44:29 +01:00
Dan Klishch c0ffff7e88 AK: Ban JsonValue from the kernel and remove ifdef guards
JsonValue can store JsonObject which uses DS for keys, so it is not safe
to use it in the kernel even with the double/String guards.
2023-11-14 10:06:54 +01:00
Sönke Holz da88d766b2 Kernel/riscv64: Make the kernel compile
This commits inserts TODOs into all necessary places to make the kernel
compile on riscv64!
2023-11-10 15:51:31 -07:00
Sönke Holz b6ac2ed34d Kernel/riscv64: Implement RISC-V SmapDisabler 2023-11-10 15:51:31 -07:00
Sönke Holz cb1b0c4101 Kernel/riscv64: Add initial PageDirectory header 2023-11-10 15:51:31 -07:00
Sönke Holz fd8858ace2 Kernel/riscv64: Add RISC-V Syscall API 2023-11-10 15:51:31 -07:00