Commit Graph

6284 Commits

Author SHA1 Message Date
Idan Horowitz
fd3be7ffcc Kernel: Setup APIC AP cores boot environment before init_stage2
Since this range is mapped in already in the kernel page directory, we
can initialize it before jumping into the first kernel process which
lets us avoid mapping in the range into init_stage2's address space.

This brings us half-way to removing the shared bottom 2 MiB mapping in
every process, leaving only the Prekernel.
2021-12-22 00:02:36 -08:00
Idan Horowitz
7b24fc6fb8 Kernel+LibC: Stub out getifaddrs() and freeifaddrs()
These are required for some ports.
2021-12-22 00:02:36 -08:00
Idan Horowitz
468ae105d8 Kernel+LibC: Stub out if_nameindex() and if_freenameindex()
These should allow users to receive the names of network interfaces in
the system, but for now these are only stubs required to compile some
ports.
2021-12-22 00:02:36 -08:00
Idan Horowitz
3a1ff175e8 Kernel: Define and return the ARPHRD_* device type in SIOCGIFHWADDR
The sa_family field in SIOCGIFHWADDR specifies the underlying network
interface's device type, this is hardcoded to generic "Ethernet" right
now, as we don't have a nice way to query it.
2021-12-22 00:02:36 -08:00
Nick Johnson
08e4a1a4dc AK+Everywhere: Replace __builtin bit functions
In order to reduce our reliance on __builtin_{ffs, clz, ctz, popcount},
this commit removes all calls to these functions and replaces them with
the equivalent functions in AK/BuiltinWrappers.h.
2021-12-21 22:13:51 +01:00
Martin Bříza
86b249f02f Kernel: Implement sysconf(_SC_SYMLOOP_MAX)
Not much to say here, this is an implementation of this call that
accesses the actual limit constant that's used by the VirtualFileSystem
class.

As a side note, this is required for my eventual Qt port.
2021-12-21 12:54:11 -08:00
Martin Bříza
f75bab2a25 Kernel: Move symlink recursion limit to .h, increase it to 8
As pointed out by BertalanD on Discord, POSIX specifies that
_SC_SYMLOOP_MAX (implemented in the following commit) always needs to be
equal or more than _POSIX_SYMLOOP_MAX (8, defined in
LibC/bits/posix1_lim.h), hence I've increased it to that value to
comply with the standard.

The move to header is required for the following commit - to make this
constant accessible outside of the VFS class, namely in sysconf.
2021-12-21 12:54:11 -08:00
Liav A
30659040ed Kernel: Ensure SMP mode is not enabled if IOAPIC mode is disabled
We need to use the IOAPIC in SMP mode, so if the user requested to
disable it, we can't enable SMP mode either.
2021-12-20 11:00:31 -08:00
Liav A
5a649d0fd5 Kernel: Return EINVAL when specifying -1 for setuid and similar syscalls
For setreuid and setresuid syscalls, -1 means to set the current
uid/euid/gid/egid value, to be more convenient for programming.
However, for other syscalls where we pass only one argument, there's no
justification to specify -1.

This behavior is identical to how Linux handles the value -1, and is
influenced by the fact that the manual pages for the group of one
argument syscalls that handle ID operations is ambiguous about this
topic.
2021-12-20 11:32:16 +01:00
Andreas Kling
e0521cfb9d Kernel: Stop ProcFS stack walk on bogus userspace->kernel traversal
Unsurprisingly, the /proc/PID/stacks/TID stack walk had the same
arbitrary memory read problem as the perf event stack walk.

It would be nice if the kernel had a single stack walk implementation,
but that's outside the scope of this commit.
2021-12-19 18:18:38 +01:00
Andreas Kling
bc518e39bf Kernel: Make perfcore files owned by UID=0, GID=0
Since perfcore files can be generated during process finalization,
we can't just allow them to contain sensitive kernel information
if they're gonna be owned by the process's own UID+GID.

So instead, perfcores are now owned by 0:0. This is not the most
ergonomic solution, but I'm not sure what we could do to make it nicer.
We'll have to think more about that. In the meantime, this patches up
a kernel info leak. :^)
2021-12-19 18:18:38 +01:00
Andreas Kling
1cdc7aa038 Kernel: Mask kernel addresses in perf event streams for non-superuser
We now turn kernel addresses into 0xdeadc0de, same as /proc/PID/stacks/
2021-12-19 18:18:38 +01:00
Andreas Kling
369c7a7e9f Kernel: Don't include kmalloc perf events in JSON for non-superuser
These events contain sensitive kernel information and should not be
available to regular users.
2021-12-19 18:18:38 +01:00
Andreas Kling
8ef9b003ad Kernel: Stop perf event stack walk on bogus userspace->kernel traversal
When walking the stack to generate a perf_event sample, we now check
if a userspace stack frame points back into kernel memory.

It was possible to use this as an arbitrary kernel memory read. :^)
2021-12-19 18:18:38 +01:00
Hendiadyoin1
18013f3c06 Kernel: Remove a redundant check in Process::remap_range_as_stack
We already VERIFY that we have carved something out, so we don't need to
check that again.
2021-12-18 10:31:18 -08:00
Hendiadyoin1
2d28b441bf Kernel: Collapse a redundant boolean conditional return statement in …
validate_mmap_prot
2021-12-18 10:31:18 -08:00
Hendiadyoin1
f38d32535c Kernel: Access OpenFileDescriptions::max_open() statically in Syscalls 2021-12-18 10:31:18 -08:00
Hendiadyoin1
c860e0ab95 Kernel: Add implicit auto qualifiers in Syscalls 2021-12-18 10:31:18 -08:00
Hendiadyoin1
f5b495d92c Kernel: Remove else after return in Process::do_write 2021-12-18 10:31:18 -08:00
Andreas Kling
32aa623eff Kernel: Fix 4-byte uninitialized memory leak in sys$sigaltstack()
It was possible to extract 4 bytes of uninitialized kernel stack memory
on x86_64 by looking in the padding of stack_t.
2021-12-18 11:30:10 +01:00
Andreas Kling
ed839450c8 Kernel: Enable SMAP protection earlier during syscall entry
There's no reason to delay this for as long as we did.
2021-12-18 11:30:10 +01:00
Andreas Kling
0ae8702692 Kernel: Make File::stat() & friends return Error<struct stat>
Instead of making the caller provide a stat buffer, let's just return
one as a value.
2021-12-18 11:30:10 +01:00
Andreas Kling
1f2d0d0ad4 Kernel: Use ksyms in-place instead of duplicating them into eternal heap
We can leave the .ksyms section mapped-but-read-only and then have the
symbols index simply point into it.

Note that we manually insert null-terminators into the symbols section
while parsing it.

This gets rid of ~950 KiB of kmalloc_eternal() at startup. :^)
2021-12-18 11:30:10 +01:00
Andreas Kling
abf2204402 Kernel: Use copy_typed_from_user() in more places :^) 2021-12-18 11:30:10 +01:00
Andreas Kling
39d9337db5 Kernel: Make sys${ftruncate,pread} take off_t as const pointer
These syscalls don't write back to the off_t value (unlike sys$lseek)
so let's take Userspace<off_t const*> instead of Userspace<off_t*>.
2021-12-18 11:30:10 +01:00
Andreas Kling
9c7659306a Kernel: Fix getsockopt(SO_RCVTIMEO) returning wrong timeout
We were returning the send timeout for both SO_RCVTIMEO and SO_SNDTIMEO.
2021-12-18 11:30:10 +01:00
Andreas Kling
5c344f4aee Kernel: Start perf event stack capture at RBP, not EBP on x86_64 2021-12-18 11:16:24 +01:00
Andreas Kling
13680ae038 Kernel: Build with -O2 by default
We used to build with -Os in order to fit within a certain size, but
there isn't really a good reason for that kind of restriction.

Switching to -O2 yields a significant improvement in throughput,
for example `test-js` is roughly 20% faster on my machine. :^)
2021-12-16 22:48:16 +01:00
sin-ack
69ef211925 Kernel+LibC: Move errno definitions to Kernel/API/POSIX
This fixes at least half of our LibC includes in the kernel. The source
of truth for errno codes and their description strings now lives in
Kernel/API/POSIX/errno.h as an enumeration, which LibC includes.
2021-12-16 22:21:35 +03:30
sin-ack
3da0c072f4 Kernel: Return the correct result for FIONREAD on datagram sockets
Before this commit, we only checked the receive buffer on the socket,
which is unused on datagram streams. Now we return the actual size of
the datagram without the protocol headers, which required the protocol
to tell us what the size of the payload is.
2021-12-16 22:21:35 +03:30
Hendiadyoin1
e5cf395a54 Kernel: Collapse blocking logic for exclusive Mutex' restore_lock()
Clang-tidy pointed out that the `need_to_block = true;` block was
duplicate, and if we collapse these if statements, we should do so
fully.
2021-12-15 23:34:11 -08:00
Hendiadyoin1
1ad4a190b5 Kernel: Add implied auto-specifiers in Locking
As per clang-tidy.
2021-12-15 23:34:11 -08:00
Hendiadyoin1
a7209ca0f9 Kernel: Add missing includes in Locking 2021-12-15 23:34:11 -08:00
Hendiadyoin1
30458a6600 Kernel: Remove duplicate access specifier in DevTmpFSInode
As per clang-tidy.
2021-12-15 23:34:11 -08:00
Hendiadyoin1
47a39e425c Kernel: Remove else statements after return in Plan9FileSystem.cpp
As per clang-tidy.
2021-12-15 23:34:11 -08:00
Hendiadyoin1
4cec16a713 Kernel: Add implied auto-specifiers in FileSystem
As per clang-tidy.
2021-12-15 23:34:11 -08:00
Hendiadyoin1
fe2cf774c3 Kernel: Fix missing include in FileSystem/Mount.h 2021-12-15 23:34:11 -08:00
Liav A
ac7953f945 Kernel: Allow switching to IOAPIC mode even without enabling SMP
This small change allows to use the IOAPIC by default without to enable
SMP mode, which emulates Uni-Processor setup with IOAPIC instead of
using the PIC.

This opens the opportunity to utilize other types of interrupts like MSI
and MSI-X interrupts.
2021-12-14 09:02:06 +01:00
Liav A
f57900a41b Kernel/Interrupts: Add ByteReaders to read possible unaligned MADT data
The MADT data could be on unaligned boundary - for example, a GSI number
(u32) on unaligned address which leads to a KUBSAN error and halting the
system.
2021-12-14 09:02:06 +01:00
Liav A
bbdb55126c Kernel/SysFS: Don't allocate ACPISysFS components in constructors
Instead defer it to a method to be called after the construction of
ACPISysFSDirectory.
2021-12-14 09:01:33 +01:00
Liav A
381fdaa163 Kernel/SysFS: Make it clear that some components must be created in boot
Using the phrase "create" doesn't give information on whether the object
must be allocated or a failure to do so can be handled gracefully.
Therefore, we must use better phrase for such purpose, so "must_create"
for the allocate-and-construct static methods is definitely good choice.
2021-12-14 09:01:33 +01:00
Liav A
478f543899 Kernel/SysFS: Prevent allocation for component name during construction
Instead, allocate before constructing the object and pass NonnullOwnPtr
of KString to the object if needed. Some classes can determine their
names as they have a known attribute to look for or have a static name.
2021-12-14 09:01:33 +01:00
Luke Wilde
62af3acf56 Kernel: Fix backtrace dumping on x86_64
- dump_backtrace was using ebp instead of rbp on x86_64, only using the
  lower 32-bits of rbp.
- The symbol loader was only fetching half of the pointer from the
  symbol table. (8 chars instead of 16 chars)
2021-12-13 09:17:32 +01:00
Clay Freeman
e3e40cca65 Kernel: Replace final loop in PhysicalRegion::return_page() with math
Since it's possible to determine where the small zones will start to
occur for each PhysicalRegion, we can use arithmetic so that the call
time for both large and small zones is identical.
2021-12-13 00:13:43 +01:00
Daniel Bertalan
8dd11ae717 Kernel+SystemServer: Add /dev/tty
This file refers to the controlling terminal associated with the current
process. It's specified by POSIX, and is used by ports like openssh to
interface with the terminal even if the standard input/output is
redirected to somewhere else.

Our implementation leverages ProcFS's existing facilities to create
process-specific symbolic links. In our setup, `/dev/tty` is a symbolic
link to `/proc/self/tty`, which itself is a symlink to the appropriate
`/dev/pts` entry. If no TTY is attached, `/dev/tty` is left dangling.
2021-12-12 22:32:35 +01:00
Jean-Baptiste Boric
23257cac52 Kernel: Remove sys$select() syscall
Now that the userland has a compatiblity wrapper for select(), the
kernel doesn't need to implement this syscall natively. The poll()
interface been around since 1987, any code still using select()
should be slapped silly.

Note: the SerenityOS source tree mostly uses select() and not poll()
despite SerenityOS having support for poll() since early 2019...
2021-12-12 21:48:50 +01:00
Jean-Baptiste Boric
2177c2a30b Kernel: Split off sys$poll() into Syscalls/poll.cpp 2021-12-12 21:48:50 +01:00
Idan Horowitz
762e047ec9 Kernel+LibC: Implement sigtimedwait()
This includes a new Thread::Blocker called SignalBlocker which blocks
until a signal of a matching type is pending. The current Blocker
implementation in the Kernel is very complicated, but cleaning it up is
a different yak for a different day.
2021-12-12 08:34:19 +02:00
Idan Horowitz
13d98999b7 Kernel: Flip incorrect bitwise set/clear of signal mask 2021-12-12 08:34:19 +02:00
Idan Horowitz
1397a5ab02 Kernel: Unblock threads only on unmasked signals
Signals that were explicitly masked should not unblock threads.
2021-12-12 08:34:19 +02:00