Commit graph

6360 commits

Author SHA1 Message Date
Brian Gianforcaro
0f7fe1eb08 Kernel: Use Process::require_no_promises instead of REQUIRE_NO_PROMISES
This change lays the foundation for making the require_promise return
an error hand handling the process abort outside of the syscall
implementations, to avoid cases where we would leak resources.

It also has the advantage that it makes removes a gs pointer read
to look up the current thread, then process for every syscall. We
can instead go through the Process this pointer in most cases.
2021-12-29 18:08:15 +01:00
Brian Gianforcaro
bad6d50b86 Kernel: Use Process::require_promise() instead of REQUIRE_PROMISE()
This change lays the foundation for making the require_promise return
an error hand handling the process abort outside of the syscall
implementations, to avoid cases where we would leak resources.

It also has the advantage that it makes removes a gs pointer read
to look up the current thread, then process for every syscall. We
can instead go through the Process this pointer in most cases.
2021-12-29 18:08:15 +01:00
Luke Wilde
c4f60844c5 Kernel: Print KUBSAN backtrace to screen if KUBSAN is deadly 2021-12-29 17:58:44 +01:00
Brian Gianforcaro
b5367bbf31 Kernel: Clarify why ftruncate() & pread() are passed off_t const*
I fell into this trap and tried to switch the syscalls to pass by
the `off_t` by register. I think it makes sense to add a clarifying
comment for future readers of the code, so they don't fall into the
same trap. :^)
2021-12-29 05:54:04 -08:00
Idan Horowitz
9d034785de Kernel: Make File::unref virtual
This is required for SlavePTY's custom unref handler to function
correctly, as otherwise a SlavePTY held in a File RefPtr would call
the base's (RefCounted<>) unref method instead of SlavePTY's version.
2021-12-29 15:46:14 +02:00
Daniel Bertalan
e37dbee017 Kernel+LibC: Add ECANCELED errno value
This is needed for clangd to compile.
2021-12-29 03:42:45 -08:00
Brian Gianforcaro
dee0c004e0 Kernel: Zero initialize winsize in TIOCGWINSZ
It looks like type types are small enough that there is no padding.
So there didn't happen to be an info leak here, but lets zero initialize
just to be on the safe side, and make auditing easier.
2021-12-29 03:41:32 -08:00
Brian Gianforcaro
737a11389c Kernel: Fix info leak from sockaddr_un in socket syscalls
In `sys$accept4()` and `get_sock_or_peer_name()` we were not
initializing the padding of the `sockaddr_un` struct, leading to
an kernel information leak if the
caller looked back at it's contents.

Before Fix:

    37.766 Clipboard(11:11): accept4 Bytes:
    2f746d702f706f7274616c2f636c6970626f61726440eac130e7fbc1e8abbfc
    19c10ffc18440eac15485bcc130e7fbc1549feaca6c9deaca549feaca1bb0bc
    03efdf62c0e056eac1b402d7acd010ffc14602000001b0bc030100000050bf0
    5c24602000001e7fbc1b402d7ac6bdc

After Fix:

    0.603 Clipboard(11:11): accept4 Bytes:
    2f746d702f706f7274616c2f636c6970626f617264000000000000000000000
    000000000000000000000000000000000000000000000000000000000000000
    000000000000000000000000000000000000000000000000000000000000000
    0000000000000000000000000000000
2021-12-29 03:41:32 -08:00
Brian Gianforcaro
98990dce53 Kernel: Fix info leak from padding in GenericFramebufferDevice::ioctl
In FB_IOCTL_GET_PROPERTIES we were not initializing the padding of the
struct, leading to the potential of an kernel information leak if the
caller looked back at it's contents.

Lets just be extra paranoid and zero initialize all these structs
in we store on the stack while handling ioctls(..).
2021-12-29 03:41:32 -08:00
Daniel Bertalan
fcdd202741 Kernel: Return the actual number of CPU cores that we have
... instead of returning the maximum number of Processor objects that we
can allocate.

Some ports (e.g. gdb) rely on this information to determine the number
of worker threads to spawn. When gdb spawned 64 threads, the kernel
could not cope with generating backtraces for it, which prevented us
from debugging it properly.

This commit also removes the confusingly named
`Processor::processor_count` function so that this mistake can't happen
again.
2021-12-29 03:17:41 -08:00
Idan Horowitz
6e2a82df13 Kernel: Port File to RefCounted
Since RefCounted automatically calls a method named `will_be_destoyed`
on classes that have one, so there's no need to have a custom
implementation of unref in File.
2021-12-29 12:04:15 +01:00
Idan Horowitz
4a3a947df3 Kernel: Rename File::{before_removing => will_be_destroyed}
This will allow File and it's descendants to use RefCounted instead of
having a custom implementation of unref. (Since RefCounted calls
will_be_destroyed automatically)

This commit also removes an erroneous call to `before_removing` in
AHCIPort, this is a duplicate call, as the only reference to the device
is immediately dropped following the call, which in turns calls
`before_removing` via File::unref.
2021-12-29 12:04:15 +01:00
Idan Horowitz
d7ec5d042f Kernel: Port Process to ListedRefCounted 2021-12-29 12:04:15 +01:00
Idan Horowitz
3d0b5efcfc Kernel: Remove Process::all_processes()
This was only used in ProcFS, which can use the `processes()` list just
as well, so let's remove it.
2021-12-29 12:04:15 +01:00
Idan Horowitz
81e23617d6 Kernel: Port Custody to ListedRefCounted
Custody's unref is one of many implementions of ListedRefCounted's
behaviour in the Kernel, which results in avoidable bugs caused by
the fragmentation of the implementations. This commit starts the work
of replacing all custom implementations with ListedRefCounted by
porting Custody to it.
2021-12-29 12:04:15 +01:00
Idan Horowitz
be91b4fe3e Kernel: Support Mutex Protected lists in ListedRefCounted
This will allow us to support Mutex Protected lists like the custodies
list as well.
2021-12-29 12:04:15 +01:00
Owen Smith
e6df1c9988 Kernel: Implement and use the syscall/sysret instruction pair on x86_64 2021-12-28 23:15:38 +01:00
Owen Smith
d36c84c331 Kernel: Reorder the 64-bit GDT a bit
Add a kernel data segment and make the user code segment come after
the data segment. We need the GDT to be in a certain order to support
the syscall and sysret instruction pair.
2021-12-28 23:15:38 +01:00
Guilherme Goncalves
33b78915d3 Kernel: Propagate overflow errors from Memory::page_round_up
Fixes #11402.
2021-12-28 23:08:50 +01:00
Andreas Kling
987b5adf2a Kernel: Remove old comment about kmalloc() being Q&D :^)
We've finally gotten kmalloc to a point where it feels decent enough
to drop this comment.

There's still a lot of room for improvement, and we'll continue working
on it.
2021-12-28 21:02:38 +01:00
Andreas Kling
9dffcc9752 Kernel: VERIFY that addresses passed to kfree_sized() look valid
Let's do some simple pointer arithmetic to verify that the address being
freed is at least within one of the two valid kmalloc VM ranges.
2021-12-28 21:02:38 +01:00
Andreas Kling
9111376d70 Kernel: Rename kmalloc_pool_heap => initial_kmalloc_memory 2021-12-28 21:02:38 +01:00
Andreas Kling
ac7ce12123 Kernel: Remove the kmalloc_eternal heap :^)
This was a premature optimization from the early days of SerenityOS.
The eternal heap was a simple bump pointer allocator over a static
byte array. My original idea was to avoid heap fragmentation and improve
data locality, but both ideas were rooted in cargo culting, not data.

We would reserve 4 MiB at boot and only ended up using ~256 KiB, wasting
the rest.

This patch replaces all kmalloc_eternal() usage by regular kmalloc().
2021-12-28 21:02:38 +01:00
Andreas Kling
a1be135891 Kernel: Lock socket mutex across {get,set}sockopt() and SO_ERROR updates
Since a socket can be accessed by multiple threads concurrently, we need
to protect shared data behind the socket mutex.

There's very likely more places where we need to fix this, the purpose
of this patch is to fix a VERIFY() failure in getsockopt() seen on CI.
2021-12-28 18:52:38 +01:00
Andreas Kling
416b0374fb Kernel: Fix race condition in TmpFSInode::notify_watchers()
We were doing this dance in notify_watchers():

    set_metadata_dirty(true);
    set_metadata_dirty(false);

This was done in order to force out inode watcher events immediately.
Unfortunately, this was racy, as if SyncTask got scheduled at the wrong
moment, it would try to flush metadata for a clean inode. This then got
trapped by the VERIFY() statement in Inode::sync_all():

    VERIFY(inode.is_metadata_dirty());

This patch fixes the issue by replacing notify_watchers() with lazy
metadata notifications like all other filesystems.
2021-12-28 13:00:28 +01:00
Brian Gianforcaro
904ea56956 Kernel: Add _SC_MAPPED_FILES sysconf API
This is mandated by POSIX, it's fine that we don't actually implement
it, just as long as it's present during compilation. :^)
2021-12-28 11:00:51 +01:00
Brian Gianforcaro
4fdff1ba63 LibC: Add in6addr_loopback and IN6ADDR_LOOPBACK_INIT constant
Much like the existing in6addr_any global and the IN6ADDR_ANY_INIT
macro, our LibC is also expected to export the in6addr_loopback global
and the IN6ADDR_LOOPBACK_INIT constant.

These were found by the stress-ng port.
2021-12-28 11:00:51 +01:00
Daniel Bertalan
52beeebe70 Kernel: Remove the KString::try_create(String::formatted(...)) pattern
We can now directly create formatted KStrings with KString::formatted.

:^)
2021-12-28 01:55:22 -08:00
Liav A
7e8beadd57 Kernel/Net: Move Realtek network adapters code to a separate directory 2021-12-28 00:56:47 -08:00
Liav A
7991a92388 Kernel/Net: Move NE2000 network adapter code to a separate directory 2021-12-28 00:56:47 -08:00
Liav A
059a47afb6 Kernel: Run clang-format on E1000NetworkAdapter.cpp 2021-12-28 00:56:47 -08:00
Liav A
39d40afa93 Kernel/Net: Move Intel network adapters code to a separate directory 2021-12-28 00:56:47 -08:00
Brian Gianforcaro
8b99fb26d9 Kernel: Use type alias for Kmalloc SubHeap and SlabBlock list types
We've moved to this pattern for the majority of usages of IntrusiveList
in the Kernel, might as well be consistent. :^)
2021-12-28 09:17:06 +01:00
Andreas Kling
63e1b904a4 Kernel: Scrub kmalloc slabs when allocated and deallocated
This matches the behavior of the generic subheaps (and the old slab
allocator implementation.)
2021-12-26 21:22:59 +01:00
Andreas Kling
3399b6c57f Kernel: Remove old SlabAllocator :^)
This is no longer useful since kmalloc() does automatic slab allocation
without any of the limitations of the old SlabAllocator. :^)
2021-12-26 21:22:59 +01:00
Andreas Kling
66d35f2936 Kernel: Add FIXME about allocation waste in kmalloc slabheap 2021-12-26 21:22:59 +01:00
Andreas Kling
43099fb387 Kernel: Remove all uses of MAKE_SLAB_ALLOCATED()
Objects that were previously allocated via slab_alloc()/slab_dealloc()
now go through kmalloc()/kfree_sized() instead.
2021-12-26 21:22:59 +01:00
Andreas Kling
2a5cff232b Kernel: Use slab allocation automagically for small kmalloc() requests
This patch adds generic slab allocators to kmalloc. In this initial
version, the slab sizes are 16, 32, 64, 128, 256 and 512 bytes.

Slabheaps are backed by 64 KiB block-aligned blocks with freelists,
similar to what we do in LibC malloc and LibJS Heap.
2021-12-26 21:22:59 +01:00
Andreas Kling
f6c594fa29 Kernel: Remove arbitrary alignment requirement from kmalloc_aligned()
We were not allowing alignments greater than PAGE_SIZE for some reason.
2021-12-26 21:22:59 +01:00
Andreas Kling
9182653a0f Kernel: Log purported size of bogus kfree_sized() requests 2021-12-26 21:22:59 +01:00
Andreas Kling
c6c786c992 Kernel: Remove kfree(), leaving only kfree_sized() :^)
There are no more users of the C-style kfree() API in the kernel,
so let's get rid of it and enjoy the new world where we always know
how much memory we are freeing. :^)
2021-12-26 21:22:59 +01:00
Andreas Kling
6eb48f7df6 Kernel: Consolidate kmalloc_aligned() and use kfree_sized() within
This patch does two things:

- Combines kmalloc_aligned() and kmalloc_aligned_cxx(). Templatizing
  the alignment parameter doesn't seem like a valuable enough
  optimization to justify having two almost-identical implementations.

- Stores the real allocation size of an aligned allocation along with
  the other alignment metadata, and uses it to call kfree_sized()
  instead of kfree().
2021-12-26 21:22:59 +01:00
Andreas Kling
83dd93ff13 Kernel: Use kfree_sized() in SlabAllocator 2021-12-26 21:22:59 +01:00
Andreas Kling
8f3b3af5ea Kernel: Remove no-longer-used Lockable template 2021-12-26 21:22:59 +01:00
Andreas Kling
fcf6ccd771 Kernel: Make KernelRng not inherit from Lockable
This class was misusing the outdate Lockable template and didn't take
advantage of the lock/resource separation mechanism fully anyway.

Since the underlying PRNG has its own SpinLock, and we already use that
for synchronization everywhere anyway, we can simply remove the Lockable
inheritance from this class.
2021-12-26 21:22:59 +01:00
Pankaj Raghav
1a27220bca Kernel: Encapsulate APIC initialization inside InterruptManagement
Currently the APIC class is constructed irrespective of whether it
is used or not.

So, move APIC initialization from init to the InterruptManagement
class and construct the APIC class only when it is needed.
2021-12-26 16:22:09 +02:00
Idan Horowitz
7757d874ad Kernel: Assert that a KmallocSubheap fits inside a page
Since we allocate the subheap in the first page of the given storage
let's assert that the subheap can actually fit in a single page, to
prevent the possible future headache of trying to debug the cause of
random kernel memory corruption :^)
2021-12-26 11:26:39 +01:00
Andreas Kling
1c99f99e99 Kernel: Make kmalloc expansions scale to incoming allocation request
This allows kmalloc() to satisfy arbitrary allocation requests instead
of being limited to a static subheap expansion size.
2021-12-26 10:43:07 +01:00
Andreas Kling
f49649645c Kernel: Allocate page tables for the entire kmalloc VM range up front
This avoids getting caught with our pants down when heap expansion fails
due to missing page tables. It also avoids a circular dependency on
kmalloc() by way of HashMap::set() in MemoryManager::ensure_pte().
2021-12-26 02:42:49 +01:00
Andreas Kling
d58880b5b0 Kernel: Write to debug log when creating new kmalloc subheaps 2021-12-26 01:25:02 +01:00