Commit Graph

8291 Commits

Author SHA1 Message Date
Liav A
ee0ccdaebe Kernel: Move Credentials.{cpp,h} to the Security subdirectory 2023-06-04 21:32:34 +02:00
Liav A
aaa1de7878 Kernel: Move {Virtual,Physical}Address classes to the Memory directory 2023-06-04 21:32:34 +02:00
Liav A
64af4953c2 Kernel: Move UBSanitizer and AddressSanitizer to Security subdirectory 2023-06-04 21:32:34 +02:00
Liav A
490856453d Kernel: Move Random.{h,cpp} code to Security subdirectory 2023-06-04 21:32:34 +02:00
Liav A
1b04726c85 Kernel: Move all tasks-related code to the Tasks subdirectory 2023-06-04 21:32:34 +02:00
Liav A
788022d5d1 Kernel: Move Jail code to a new subdirectory 2023-06-04 21:32:34 +02:00
Liav A
b40b1c8d93 Kernel+Userland: Ensure proper unveil permissions before using rm/rmdir
When deleting a directory, the rmdir syscall should fail if the path was
unveiled without the 'c' permission. This matches the same behavior that
OpenBSD enforces when doing this kind of operation.

When deleting a file, the unlink syscall should fail if the path was
unveiled without the 'w' permission, to ensure that userspace is aware
of the possibility of removing a file only when the path was unveiled as
writable.

When using the userdel utility, we now unveil that directory path with
the unveil 'c' permission so removal of an account home directory is
done properly.
2023-06-02 17:53:55 +02:00
Liav A
500b7b08d6 Kernel: Move the Storage directory to be a new directory under Devices
The Storage subsystem, like the Audio and HID subsystems, exposes Unix
device files (for example, in the /dev directory). To ensure consistency
across the repository, we should make the Storage subsystem to reside in
the Kernel/Devices directory like the two other mentioned subsystems.
2023-06-02 11:04:37 +02:00
kleines Filmröllchen
0eddee44f3 Kernel: Remove unused Platform.h include in linker script
This had only been in use for architecture detection before
the removal of 32 bit x86.
2023-06-02 10:27:29 +02:00
Timon Kruiper
dfba998d00 Kernel/aarch64: Make sure stack pointer is always 16 byte aligned
This is enforced by the hardware and an exception is generated when the
stack pointer is not properly aligned. This brings us closer to booting
the aarch64 Kernel on baremetal.
2023-05-31 22:36:44 +02:00
Daniel Bertalan
7987bf5b92 Kernel/aarch64: Add RPi/MMIO.cpp to SOURCES_RUNNING_WITHOUT_MMU
Otherwise, `MMIO::MMIO` will fault on the RPi 3 due to accessing
`__stack_chk_guard` before the kernel is mapped into high memory.
2023-05-28 05:05:09 -06:00
Daniel Bertalan
2a2787b199 Kernel/aarch64: Make Processor::capture_stack_trace stub non-crashing
This is the only kernel issue blocking us from running the test suite.
Having userspace backtraces printed to the debug console during crashes
isn't vital to the system's function, so let's just return an empty
trace and print a FIXME instead of crashing.
2023-05-28 05:05:09 -06:00
Ben Wiederhake
5fafd82927 AK+Everywhere: Don't crash on invalid months
Sadly, we don't have proper error propagation here. However, crashing
the Kernel just because a CDROM contains an invalid month seems like a
bad idea.
2023-05-27 12:17:50 +02:00
Ben Wiederhake
815ea06d2c AK: Test from_unix_time_parts intensively 2023-05-27 12:17:50 +02:00
Liav A
8142f7b196 Kernel: Mark sys$get_dir_entries as not needing the big lock
After examination of all overriden Inode::traverse_as_directory methods
it seems like proper locking is already existing everywhere, so there's
no need to take the big process lock anymore, as there's no access to
shared process structures anyway.
2023-05-27 10:58:58 +02:00
Liav A
2ab657d3b5 Kernel: Make Ext2FSInode::traverse_as_directory to take m_inode_lock
The contents of the directory inode could change if we are not taking so
we must take the m_inode_lock to prevent corruption when reading the
directory contents.
2023-05-27 10:58:58 +02:00
Liav A
46ef2f8e20 Kernel: Mark sys$fork as not needing the big lock
All shared structures are already protected by "atomic" spinlocks for
those structures, so there's no need to take the big process lock.
2023-05-27 10:58:58 +02:00
Liav A
0be79f9bc2 Kernel: Mark sys$umount as not needing the big lock
All accesses to the mount table are already serialized by the actual
spinlock of that table.
2023-05-27 10:58:58 +02:00
Liav A
902dac7f5f Kernel: Don't lock ProcFS mutex when calling traverse_as_directory
This is not needed, because when we are doing this traversing, functions
that are called from this function are using proper and more "atomic"
locking.
2023-05-27 10:58:58 +02:00
Liav A
bce17d06f5 Kernel: Don't lock SysFS filesystem mutex calling traverse_as_directory
This locking is simply not needed because the associated SysFS component
will use proper and more "atomic" locking on its own.
2023-05-27 10:58:58 +02:00
Caoimhe
360b8b166f Kernel/aarch64: Use the correct MMIO base address in the MMU 2023-05-26 08:29:26 -06:00
Daniel Bertalan
906abbdf53 Kernel/aarch64: Fix build after #17842 2023-05-25 08:26:07 -07:00
kleines Filmröllchen
fc5cab5c21 Everywhere: Use MonotonicTime instead of Duration
This is easily identifiable by anyone who uses Duration::now_monotonic,
and any downstream users of that data.
2023-05-24 23:18:07 +02:00
kleines Filmröllchen
939600d2d4 Kernel: Use UnixDateTime wherever applicable
"Wherever applicable" = most places, actually :^), especially for
networking and filesystem timestamps.

This includes changes to unzip, which uses DOSPackedTime, since that is
changed for the FAT file systems.
2023-05-24 23:18:07 +02:00
kleines Filmröllchen
213025f210 AK: Rename Time to Duration
That's what this class really is; in fact that's what the first line of
the comment says it is.

This commit does not rename the main files, since those will contain
other time-related classes in a little bit.
2023-05-24 23:18:07 +02:00
Pankaj Raghav
0c5d6c6c47 Kernel: Move NVMeInterruptQueue initialization out of its constructor
Add a helper initialize_interrupt_queue() helper to enable_irq instead
of doing it as part of its object construction as it can fail. This is
similar to how AHCI initializes its interrupt as well.
2023-05-21 18:01:29 -06:00
Pankaj Raghav
ac161f6a8d Kernel/NVMe: Add try_create() to NVMe{Poll|Interrupt}Queue
NVMe{Poll|Interrupt}Queue don't have a try_create() method. Add one to
keep it consistent with how we create objects. Also this commit is in
preparation to moving any initialization related code out of the
constructor.
2023-05-21 18:01:29 -06:00
Pankaj Raghav
b8c03d44a7 Kernel: Convert rw_dma_page to NonnullRefPtr in NVMeQueue
Propagate error if the rw_dma_page is NULL in try_create and use
relase_nonnull to convert RefPtr to NonnullRefPtr before passing it to
the NVMeQueue.
2023-05-21 18:01:29 -06:00
Pankaj Raghav
4014b06b08 Kernel: Remove the unused {cq|sq}_dma_page from NVMeQueue
{cq|sq}_dma_page are not used by the NVMeQueue class. Remove them.
2023-05-21 18:01:29 -06:00
Pankaj Raghav
d14c2a3583 Kernel: Move handle_interrupt out-of-line in PCIIRQHandler
Upgrade to GCC 13.1.0 triggered an UBSAN in PCIIRQHandler. Moving the
handle_interrupt() function out-of-line fixes this issue.
2023-05-21 18:01:29 -06:00
Daniel Bertalan
beb55f726f Kernel/aarch64: Detect if access faults come from SafeMem
This commit lets us differentiate whether access faults are caused by
accessing junk memory addresses given to us by userspace or if we hit a
kernel bug.

The stub implementations of the `safe_*` functions currently don't let
us jump back into them and return a value indicating failure, so we
panic if such a fault happens. Practically, this means that we still
crash, but if the access violation was caused by something else, we take
the usual kernel crash code path and print a register and memory dump,
rather than hitting the `TODO_AARCH64` in `handle_safe_access_fault`.
2023-05-21 12:00:22 +02:00
Daniel Bertalan
0da2d2102a Kernel/aarch64: Flatten safe_{memset,strnlen,memcpy}()
We want to detect if an access fault comes from within these operations,
so they cannot be calling out to the non-safe variants.
2023-05-21 12:00:22 +02:00
Daniel Bertalan
ab279c850b Kernel/aarch64: Stub out atomic SafeMem functions
These are used in futexes, which are needed if we want to get further in
`run-tests`.

For now, we have no way to return a non-fatal error if an access fault
is raised while executing these, so the kernel will panic. Some would
consider this a DoS vulnerability where a malicious userspace app can
crash the kernel by passing bogus pointers to it, but I prefer to call
it progress :^)
2023-05-21 12:00:22 +02:00
Andrew Kaster
28d2e26678 Kernel: Enable data and instruction cache on aarch64
Enabling these will fix the Unsupported Exclusive or Atomic access data
fault we get on bare metal Raspberry Pi 3. On A53/A57 chips (and newer),
atomic compare-exchange operations require the data cache to be enabled.
2023-05-19 20:12:25 -06:00
Andrew Kaster
f62c646c28 Kernel: Update reset value and register names of SCTLR_EL1 per Arm ARM
Referencing ARM DDI 0487J.a, update the names of previously reserved
fields, and set the reset_value() of the SCTLR_EL1 struct to reflect
the defaults we want for this register on reboot.
2023-05-19 20:12:25 -06:00
Hediadyoin1
60cddb4179 Kernel: Check only for the first equal sign in the kernel command line
... key-value decomposition

The RaspberryPi firmware will give us a value for the 'video' key that
contains multiple equal signs:
```
video=HDMI-A-1:1920x1080M@30D,margin_left=48,margin_right=48,[...]
```
Instead of asserting that this only has one equal sign, let's just split
it by the first one.
2023-05-19 20:11:53 -06:00
Pankaj Raghav
dabc6dd962 Kernel/ScatterGatherList: Add region_name as a part of try_create API
Remove the hardcoded "AHCI Scattered DMA" for region name as it is a
part of a common API. Add region_name parameter to the try_create API
so that this API can be used by other drivers with the correct Memory
region name.
2023-05-19 22:04:37 +02:00
Pankaj Raghav
e067046474 Kernel/ScatterGatherList: Move constructor init code to try_create
The constructor code of ScatterGatherList had code that can return
error. Move it to try_create for better error propagation.

This removes one TODO() and one
release_value_but_fixme_should_propagate_errors().
2023-05-19 22:04:37 +02:00
Pankaj Raghav
489e268b96 Kernel/ScatterGatherList: Return ErrorOr from try_create
This removes the TODO from the try_create API to return ErrorOr. This
is also a preparation patch to move the init code in the constructor
that can fail to this try_create function.
2023-05-19 22:04:37 +02:00
Liav A
4617c05a08 Kernel: Move a bunch of generic devices code into new subdirectory 2023-05-19 21:49:21 +02:00
Liav A
0bbd9040ef Kernel+Userland: Split bind-mounting and re-mounting from mount syscall
These 2 are an actual separate types of syscalls, so let's stop using
special flags for bind mounting or re-mounting and instead let userspace
calling directly for this kind of actions.
2023-05-17 23:39:15 -06:00
Daniel Bertalan
96f89d14a3 Kernel: Fix memory mapping size of the BootFramebufferConsole
The Multiboot header stores the framebuffer's pitch in bytes, so
multiplying it by the pixel's size is not necessary. We ended up
allocating 4 times as much memory as needed, which caused us to overlap
the MMIO reserved memory area on the Raspberry Pi.
2023-05-17 01:38:03 -06:00
Daniel Bertalan
3d383974cd Kernel: Flush data cache before passing a buffer to the VC Mailbox
Otherwise, the message's contents might be in the cache only, so
VideoCore will read stale/garbage data from main memory.

This fixes framebuffer setup on bare metal with the data cache enabled.
2023-05-17 01:38:03 -06:00
Daniel Bertalan
c460b84ebe Kernel: Add character device driver for the RPi "mini UART" (UART1)
While the PL011-based UART0 is currently reserved for the kernel
console, UART1 is free to be exposed to the userspace as `/dev/ttyS0`.
This will be used as the stdout of `run-tests-and-shutdown.sh` when
testing the AArch64 kernel.
2023-05-17 01:32:43 -06:00
Daniel Bertalan
3beb488887 Kernel: Add RPi::Timer::get_clock_rate() 2023-05-17 01:32:43 -06:00
Daniel Bertalan
d9c557d0b4 Kernel: Add RPi Watchdog and use it for system shutdown
The Raspberry Pi hardware doesn't support a proper software-initiated
shutdown, so this instead uses the watchdog to reboot to a special
partition which the firmware interprets as an immediate halt on
shutdown. When running under Qemu, this causes the emulator to exit.
2023-05-17 01:32:43 -06:00
Daniel Bertalan
555d301e3b Kernel: Unify x86-64 and AArch64 __panic implementation
We now have everything in the AArch64 kernel to be able to use the full
`__panic` implementation, so we can share the code with x86-64.

I have kept `__assertion_failed` separate for now, as the x86-64 version
directly executes inline assembly, thus `Kernel/Arch/aarch64/Panic.cpp`
could not be removed.
2023-05-17 01:32:43 -06:00
Tim Schumacher
d78bed2ffe Kernel: Alias _SC_PAGE_SIZE to _SC_PAGESIZE
Both of those are specified by POSIX.
2023-05-17 08:54:55 +02:00
Pankaj Raghav
6c7ee5344c Kernel: Add MSI support to AHCI
Add MSI support to AHCI. Prefer MSI interrupts over pin-based
interrupts.
2023-05-16 23:22:12 +02:00
Pankaj Raghav
8f62e62cfe Kernel: Add MSI support in PCI Device
Extend reserve_irqs, allocate_irq, enable_interrupt and
disable_interrupt API to add MSI support in PCI device.

The current changes only implement single MSI message support.
TODOs have been added to support Multiple MSI Message (MME) support in
the future.
2023-05-16 23:22:12 +02:00
Pankaj Raghav
35a844ac75 Kernel: Implement {enable,disable}_msi interrupts in PCI Device
Implement enabling and disabling MSI interrupts for a PCI device.

Removes two more TODO()s from PCI::Device.cpp :^)
2023-05-16 23:22:12 +02:00
Pankaj Raghav
1bc2c5c110 Kernel: Use PCIDeviceIdentifier is_msi_capable() to retrieve MSI status
Instead of iterating through the capabilities, use the is_msi_capable()
API from the PCIDeviceIdentifier class that belongs to the device.
2023-05-16 23:22:12 +02:00
Pankaj Raghav
09b5cefd43 Kernel: Add MSIInfo struct to PCI DeviceIdentifier
Add a struct named MSIInfo that stores all the relevant MSI
information as a part of PCI DeviceIdentifier struct.
Populate the MSI struct during the PCI device init.
2023-05-16 23:22:12 +02:00
Andrew Kaster
baeee0effe Kernel: Restore kernel8.img for aarch64 build
This was erroneously deleted in 420952a433
2023-05-16 09:47:37 +02:00
Timon Kruiper
55e8ffd122 Kernel/aarch64: Make REGISTER_STATE_SIZE a multiple of 16 bytes
This ensure that the stack pointer also stays 16 byte aligned. This
fixes a baremetal issue when getting an exception.
2023-05-15 17:16:06 -06:00
Timon Kruiper
65ed5419ae Kernel/aarch64: Dump registers when unknown exception occurs
This is useful when debugging baremetal issues.
2023-05-15 17:16:06 -06:00
Daniel Bertalan
2123fdd678 Kernel: Remove FIFO::{attach,detach}(Direction)
These functions would have caused a `-Woverloaded-virtual` warning with
GCC 13, as they shadow `File::{attach,detach}(OpenFileDescription&)`.

Both of these functions had a single call site. This commit inlines
`attach` into its only caller, `FIFO::open_direction`.

Instead of explicitly checking `is_fifo()` in `~OpenFileDescription`
before running the `detach(Direction)` overload, let's just override the
regular `detach(OpenFileDescription&)` for `FIFO` to perform this action
instead.
2023-05-15 07:00:29 +02:00
Ben Wiederhake
173f872cda Meta: Remove unused debug flags, add missing GENERATE_DEBUG
Commands that were helpful while investigating this:
```
grep -P '^set' Meta/CMake/all_the_debug_macros.cmake \
  | sed -Ee 's,set\((.+) ON\)$,\1,' > macros.lst
for i in $(cat macros.lst); do
  echo -n "$i "; git grep -Pn '\b'"$i"'\b' | wc -l
done | tee matches.lst
sort -k2 -n matches.lst
```
2023-05-14 16:01:57 -06:00
Liav A
420952a433 Kernel/aarch64: Remove drawing of logo on the framebuffer during init
This logo was actually used as a first sign of life in the very early
days of the aarch64 port.
Now that we boot into the graphical mode of the system just fine there's
no need to keep this.
2023-05-13 07:56:00 +02:00
Pankaj Raghav
9b3b0531e5 Kernel: Add MSIx support to NVMe
Add MSIx support to NVMe. Prefer MSIx over pin-based interrupts as they
are more efficient and all modern hardware support them.
2023-05-07 21:16:41 +02:00
Pankaj Raghav
bfcf7ab3e8 Kernel: Pass NVMeController reference to NVMequeue
This is in preparation for adding MSI(x) support to the NVMe device.
NVMeInterruptQueue needs access to the PCI device to deal with MSI(x)
interrupts. It is ok to pass the NVMeController as a reference to the
NVMeQueue as NVMeController is the one that owns the NVMeQueue.

This is very similar to how AHCIController passes its reference to its
interrupt handler.
2023-05-07 21:16:41 +02:00
Pankaj Raghav
693e3419b7 NVMe: Use an explicit Queue type instead of using an Optional irq
Add an explicit QueueType enum which could be used to create a poll or
an interrupt queue. This is better than passing an Optional<irq>.

This refactoring is in preparation for adding MSIx support to NVMe.
2023-05-07 21:16:41 +02:00
Pankaj Raghav
feb48cbc7c Kernel: Introduce PCIIRQHandler
PCIIRQHandler is a generic IRQ handler that the device driver can
inherit to use either Pin or MSI(x) based interrupt mechanism.

The PCIIRQHandler can do what the existing IRQHandler can do for pin
based interrupts but also deal with MSI based interrupts. We can
hopefully convert all the PCI based devices to use this handler so that
MSI(x) can be used.
2023-05-07 21:16:41 +02:00
Pankaj Raghav
82cf0bfb75 Kernel: Add APIs to PCI Device to use MSI(x)
Add reserve_irqs, allocate_irq, enable_interrupt and disable_interrupt
API to a PCI device.

reserve_irqs() can be used by a device driver that would like to
reserve irqs for MSI(x) interrupts. The API returns the type of IRQ
that was reserved by the PCI device. If the PCI device does not support
MSI(x), then it is a noop.

allocate_irq() API can be used to allocate an IRQ at an index. For
MSIx the driver needs to map the vector table into the memory and add
the corresponding IRQ at the given index. This API will return the
actual IRQ that was used so that the driver can use it create interrupt
handler for that IRQ.

{enable, disable}_interrupt API is used to enable or disable a
particular IRQ at the given index. It is a noop for pin-based
interrupts. This could be used by IRQHandler to enable or disable an
interrupt.
2023-05-07 21:16:41 +02:00
Pankaj Raghav
f0b6eb6932 Kernel: Implement helpers to manipulate MSI(x) data structures
MSIx table entry is used to program interrupt vectors and it is
architecture specific. Add helper functions declaration in
Arch/PCIMSI.h. The definition of the function is placed in the
respective arch specific code.
2023-05-07 21:16:41 +02:00
Pankaj Raghav
bf7ac06d7b Kernel: Implement {enable,disable}_msix interrupts in PCI Device
Implement enabling and disabling MSIx interrupts for a PCI device.

Removes two TODO()s from PCI::Device.cpp :^)
2023-05-07 21:16:41 +02:00
Pankaj Raghav
d3bb63afff Kernel: Use PCIIdentifier is_msix_capable API to retrieve MSIx status
Instead of iterating through the capabilities, use the
is_msix_capable() API from the PCIIdentifier class that belongs to the
device.
2023-05-07 21:16:41 +02:00
Pankaj Raghav
d0fbaf790a Kernel: Add MSIxInfo struct to PCI DeviceIdentifier
Add a struct named MSIxInfo that stores all the relevant MSIx
information as a part of PCI DeviceIdentifier struct.

Populate the MSIx struct during the PCI device init. As the
DeviceIdentifier struct need to populate MSIx info, don't mark
DeviceIdentifier as const in the PCI::Device class.
2023-05-07 21:16:41 +02:00
Pankaj Raghav
71c75873c9 Kernel: Add write{8,16,32} to the PCI Capability struct
MSI(x) mechanism requires the device to write to its Capability
structure. Add write{8,16,32} similar to read{8,16,32}.
2023-05-07 21:16:41 +02:00
Pankaj Raghav
91da264a4c Kernel: Add reserve_interrupt_handlers API
MSI(x) interrupts need to reserve IRQs so that it can be programmed by
the device. Add an API to reserve contiguous ranges of interrupt
handlers so that it can used by PCI devices that use MSI(x) mechanism.

This API needs to be implemented by aarch64 architecture.
2023-05-07 21:16:41 +02:00
Pankaj Raghav
a5ec5f07fa Kernel/PCI: Set IRQ as reserved for pin-based interrupts
Set pin-based interrupt handler as reserved during PCI bus init.
This is required so that MSI(x) based interrupts can avoid sharing the
IRQ which has been marked as reserved.
2023-05-07 21:16:41 +02:00
Pankaj Raghav
e5cc78e9db Kernel: Add m_reserved private variable to GenericInterruptHandler
Pin-based PCI device are allocated an IRQ, and it could be shared with
multiple devices. An interrupt handler with an IRQ for a PCI device
will get registered only during the driver initialization.

For MSI(x) interrupts, the driver has to allocate IRQs and this field
can be used to skip IRQs that have already been reserved by pin-based
interrupts so that we don't have to share IRQs, which generally will
reduce the performance.
2023-05-07 21:16:41 +02:00
Ben Wiederhake
36ff6187f6 Everywhere: Change spelling of 'behaviour' to 'behavior'
"The official project language is American English […]."
5d2e915623/CONTRIBUTING.md (L30)

Here's a short statistic of the occurrences of the word "behavio(u)r":

$ git grep -IPioh 'behaviou?r' | sort | uniq -c | sort -n
      2 BEHAVIOR
     24 Behaviour
     32 behaviour
    407 Behavior
    992 behavior

Therefore, it is clear that "behaviour" (56 occurrences) should be
regarded a typo, and "behavior" (1401 occurrences) should be preferred.

Note that The occurrences in LibJS are intentionally NOT changed,
because there are taken verbatim from the specification. Hence:

$ git grep -IPioh 'behaviou?r' | sort | uniq -c | sort -n
      2 BEHAVIOR
     10 behaviour
     24 Behaviour
    407 Behavior
   1014 behavior
2023-05-07 01:05:09 +02:00
Liav A
36bb04d792 Kernel/Memory: Fix UNMAP_AFTER_INIT page fault handling
This was discovered by me during a work on USB keyboard patches, so it
triggered this bug.

The printing format for the VirtualAddress part is incorrect, leading to
another crash when handling page fault after accessing UNMAP_AFTER_INIT
code section.
2023-05-06 08:03:34 +02:00
Pankaj Raghav
ac9d60bb13 Kernel: Promote the entry to the front during a cache hit
Whenever an entry is added to the cache, the last element is removed to
make space for the new entry(if the cache is full). To make this an LRU
cache, the entry needs to be moved to the front of the list when there
is a cache hit so that the least recently used entry moves to the end
to be evicted first.
2023-05-06 08:00:55 +02:00
Liav A
bc3eb6d65f Kernel/VirtIO: Use proper error propagation from the get_config method
This allows us to drop null-checks at call-sites, thus simplifying the
code and reducing the chance of nullptr-dereference errors.
2023-04-30 06:03:47 +02:00
Liav A
87a32ab869 Kernel/VirtIO: Improve error handling during device initialization
Rename the initialize method to initialize_virtio_resources so it's
clear what this method is intended for.

To ensure healthier device initialization, we could also return the type
of ErrorOr<void> from this method, so in all overriden instances and in
the original method code, we could leverage TRY() pattern which also
does simplify the code a bit.
2023-04-30 06:03:47 +02:00
Liav A
aa985a0570 Kernel/VirtIO: Move declarations and definitions to a separate file 2023-04-30 06:03:47 +02:00
Liav A
d430ee8bec Kernel/aarch64: Don't set multiboot_modules to an empty array on-stack
Since multiboot_modules_count is set to 0, we can safely set the
multiboot_modules pointer to 0 (null pointer), as we don't use multiboot
on aarch64 anyway.
2023-04-29 08:53:29 -06:00
Daniel Bertalan
81dd29f713 Kernel/aarch64: Support reading the command line via the RPi Mailbox
This reuses the existing `RPi::Mailbox` interface to read the command
line via a VideoCore-specific mailbox message. This will have to be
replaced if that interface starts being smarter, as this is needed very
early, and nothing guarantees that a smarter Mailbox interface wouldn't
need to allocate or log, which is a no-no during early boot.

As the response string can be arbitrarily long, it's the caller's job to
provide a long enough buffer for `Mailbox::query_kernel_command_line`.
This commit chose 512 bytes, as it provides a large enough headroom over
the 150-200 characters implicitly added by the VC firmware.

The portable way would be to parse the `/chosen/bootargs` property of
the device tree, but we currently lack the scaffolding for doing that.

Support for this in QEMU relies on a patch that has not yet been
accepted upstream, but is available via our `Toolchain/BuildQEMU.sh`
script. It should, however, work on bare metal.

Tested-By: Timon Kruiper <timonkruiper@gmail.com>
2023-04-29 08:24:18 +02:00
Daniel Bertalan
6aa392f6e4 Kernel: Store the kernel command line in a StringView
The Raspberry Pi's mailbox interface does not guarantee that the
returned command line is null-terminated. This commit removes that
assumption from the current code, allowing the next commit to add
support for reading it on the Pi.

This also lets us eliminate a few manual `strlen()` calls :^)
2023-04-29 08:24:18 +02:00
Tim Schumacher
9ab598af49 Revert "Kernel/x86: Bake the Prekernel and the Kernel into one image"
Some hardware/software configurations crash KVM as soon as we try to
start Serenity. The exact cause is currently unknown, so just fully
revert it for now.

This reverts commit 897c4e5145.
2023-04-28 23:24:19 +02:00
Daniel Bertalan
a0356a0302 Kernel/aarch64: Fix build after is_sharing_with_others API removal
This commit fixes the build after the removal of
`GenericInterruptHandler::is_sharing_with_others` in 8944ca830f.
2023-04-28 15:00:06 +02:00
Samuel Bowman
fc1fd907b4 Kernel: Create all kernel processes before enabling boot profiling
Process created performance events for kernel processes are only ever
emitted for the kernel processes that exist when profiling is enabled.
Any new kernel processes created after profiling is enabled will not
have corresponding process created performance events, so all kernel
processes should be created before enabling profiling.

NetworkTask was the only kernel process being created after enabling
profiling, so we now just create it before enabling profiling. This
fixes an issue where Profiler was failing to parse boot profiles as a
result of NetworkTask not having a process created event.
2023-04-28 09:27:55 +02:00
Liav A
897c4e5145 Kernel/x86: Bake the Prekernel and the Kernel into one image
The new baked image is a Prekernel and a Kernel baked together now, so
essentially we no longer need to pass the Prekernel as -kernel and the
actual  kernel image as -initrd to QEMU, leaving the option to pass an
actual initrd or initramfs module later on with multiboot.
2023-04-28 09:23:30 +02:00
Timothy Flynn
3d4d0a1243 Kernel: Colorize log message for paths which haven't been unveiled
The log message can be hard to spot in a sea of debug messages. Colorize
it to make the message more immediately pop out.
2023-04-25 18:04:15 +02:00
Pankaj Raghav
8944ca830f Kernel: Remove is_sharing_with_others API from GenericInterruptHandler
is_sharing_with_others API was never really put to use properly since
it was introduced. The only place where it is used in Interrupts.cpp is
in conjuction with is_shared_handler() which is only true for
SharedIRQHandler and is_sharing_with_others will always return false.

Remove that API.
2023-04-25 10:18:39 +02:00
Pankaj Raghav
756a73471e Kernel: Use SpinlockProtected list in SharedIRQHandler
Adding handlers to the SharedIRQHandler without any lock is not thread
safe. Use SpinlockProtected list instead.
2023-04-25 10:18:39 +02:00
Pankaj Raghav
fd8a154f8c Kernel: Set IRQHandler m_shared_with_others when the irq is shared
If IRQHandler's IRQ is shared, then disable_irq() should not call the
controller to disable that IRQ as some other device might be using it.
IRQHandler had a private variable to indicate if it is being shared:
m_shared_with_others but it was never modified even if the IRQ was
shared.

Add a new member function set_shared_with_others() to enable/disable
m_shared_with_others member of IRQHandler class. This function is
called when an IRQHandler is being added/removed as a part of
SharedIRQHandler.
2023-04-25 10:18:39 +02:00
Pankaj Raghav
83b87a5ade Kernel: Add bar_address_mask to mask the last 4 bits of a BAR address
Create a bar_address_mask constant to mask the last 4 bits of a BAR
address instead of hand coding the mask all over the kernel.
2023-04-24 21:41:54 +02:00
Liav A
7b6cea9ef4 Kernel: Improve context state keeping in the VirtIOGPU3DDevice class
This is done mainly by implementing safe locking on the data structure
keeping the pointers to the PerContextState objects. Therefore, this now
eliminates the need for using LockRefPtr, as SpinlockProtected is enough
for the whole list.

The usage of HashMap in this class was questionable, and according to
Sahan Fernando (the original contributor to the VirGL work also known as
ccapitalK) there was no deep research on which data structure to use for
keeping all pointers to PerContextState objects.
Therefore, this structure is changed to IntrusiveList as the main reason
and advantage to use it is that handling OOM conditions is much more
simple, because if we succeeded to create a PerContextState object, we
can be sure now that inserting it to the list will not cause OOM error
condition.
2023-04-24 13:09:22 +02:00
Liav A
8289759f1d Kernel: Allow configuring a Jail to not impose PID isolation restriction
This is quite useful for userspace applications that can't cope with the
restriction, but it's still useful to impose other non-configurable
restrictions by using jails.
2023-04-24 12:15:29 +02:00
Daniel Bertalan
d205814da6 Kernel+LibC: Implement pthread_create for AArch64
Instead of storing x86_64 register names in `SC_create_thread_params`,
let the Kernel figure out how to pass the parameters to
`pthread_create_helper`.
2023-04-23 14:30:59 +02:00
Liav A
ee4e9b807a Kernel: Protect internal structures in InodeWatcher with spinlocks
This was the last change that was needed to be able boot with the flag
of LOCK_IN_CRITICAL_DEBUG. That flag is not always enabled because there
are still other issues in which we hold a spinlock and still try to lock
a mutex.

Instead of using one global mutex we can protect internal structures of
the InodeWatcher class with SpinlockProtected wrappers. This in turn
allows the InodeWatcher code to be called from other parts in the kernel
while holding a prior spinlock properly.
2023-04-22 07:16:41 +02:00
Tim Schumacher
12ce6ef3d7 Kernel+Userland: Remove the nfds entry from /sys/kernel/processes
`process.fds()` is protected by a Mutex, which causes issues when we try
to acquire it while holding a Spinlock. Since nothing seems to use this
value, let's just remove it entirely for now.
2023-04-21 13:55:23 +02:00
Hendiadyoin1
d10e838efd Kernel: Add a FIXME about SD card synchronicity
This is a major bottleneck when booting the system, especially in
non-smp mode.
2023-04-19 10:53:33 +01:00
Hendiadyoin1
863d0ac260 Kernel: Add High Speed support to the SDHC driver
This should not affect boot times on qemu, as that does not use
dynamic transfer delays in its adma code path.
On real hardware this could potentially double the data throughput,
decreasing load times.
2023-04-19 10:53:33 +01:00
Hendiadyoin1
857c9b4558 Kernel: Add basic ADMA2 support to the SD card driver
It only takes ~10s to fully boot with smp enabled!
2023-04-19 10:53:33 +01:00
Hendiadyoin1
daf85732bc Kernel: Make SDHC InterruptStatus a bitfield
A raw accessor was left as a means to use already existing codepaths.
2023-04-19 10:53:33 +01:00
Hendiadyoin1
35ec96fd28 Kernel: Add V4.10 fields to SDHC Command struct
They are not used yet but might become helpful in the future
2023-04-19 10:53:33 +01:00
Hendiadyoin1
fb79b09688 Kernel: Turn SD CapabilitiesRegister into a bit-field 2023-04-19 10:53:33 +01:00
Tim Schumacher
aefd6e9ee1 Kernel: Don't get rbp from internal context switch structures
This has been broken on x86_64 since its introduction, as it features
more registers to be saved, and we never held up the "rbp has to be the
last pushed register" there.

Instead, just copy rbp from the thread structure, which is now properly
updated since the last commit.
2023-04-17 20:30:17 +02:00
Tim Schumacher
0ee476948b Kernel: Properly update the stored thread rbp when switching contexts 2023-04-17 20:30:17 +02:00
Tim Schumacher
50b7183bd1 Kernel: Mark the idle thread as active before switching it in
Otherwise, our code for dumping all thread stacks gets confused if it
finds a thread that is in a 'running' state, but that isn't marked
active.
2023-04-17 20:30:17 +02:00
Tim Schumacher
d4e114a31e Kernel: Remove unused functions related to reading full inodes 2023-04-17 01:20:23 +02:00
Tim Schumacher
f5010f7263 Kernel: Use purpose-sized buffers for holding readlink results 2023-04-17 01:20:23 +02:00
Tim Schumacher
6f524e35a7 Kernel: Use purpose-sized buffers when resolving inodes as links 2023-04-17 01:20:23 +02:00
Tim Schumacher
acd8c8dba4 Kernel: Add Inode::read_until_filled_or_end
The existing `read_entire` is quite slow due to allocating and copying
multiple times, but it is simultaneously quite hard to get rid of in a
single step. As a replacement, add a new function that reads as much as
possible directly into a user-provided buffer.
2023-04-17 01:20:23 +02:00
Liav A
32557be930 Kernel/HID: Don't use *LockRefPtrs in the I8042Controller code 2023-04-15 12:53:31 +02:00
Liav A
747efc5265 Kernel: Re-organize header includes in the HID subsystem code
Just a small clean-up in the I8042Controller and HIDManagement code so
it will be easier to modify this code in future changes.
2023-04-15 12:53:31 +02:00
Marco Cutecchia
75cc670bcb Kernel: Disable interrupt signaling for the SD driver
Currently we do not use interrupts for the SD driver, yet we
had enabled the signaling of all of them.
Since we were never acknowledging them, we were getting spammed by
unnecessary interrupts, causing the system to slow down to a crawl.

This commit makes the system boot in less than 1 minute with PIO,
compared to the old 30+ minute boot.
2023-04-15 11:16:22 +01:00
Liav A
7c1f645e27 Kernel/Net: Iron out the locking mechanism across the subsystem
There is a big mix of LockRefPtrs all over the Networking subsystem, as
well as lots of room for improvements with our locking patterns, which
this commit will not pursue, but will give a good start for such work.

To deal with this situation, we change the following things:
- Creating instances of NetworkAdapter should always yield a non-locking
  NonnullRefPtr. Acquiring an instance from the NetworkingManagement
  should give a simple RefPtr,as giving LockRefPtr does not really
  protect from concurrency problems in such case.
- Since NetworkingManagement works with normal RefPtrs we should
  protect all instances of RefPtr<NetworkAdapter> with SpinlockProtected
  to ensure references are gone unexpectedly.
- Protect the so_error class member with a proper spinlock. This happens
  to be important because the clear_so_error() method lacked any proper
  locking measures. It also helps preventing a possible TOCTOU when we
  might do a more fine-grained locking in the Socket code, so this could
  be definitely a start for this.
- Change unnecessary LockRefPtr<PacketWithTimestamp> in the structure
  of OutgoingPacket to a simple RefPtr<PacketWithTimestamp> as the whole
  list should be MutexProtected.
2023-04-14 19:27:56 +02:00
Liav A
bd7d4513bf Kernel/Net: Make the LoopbackAdapter initializer to use ErrorOr pattern
This looks much more nice, and also matches our pattern for other types
of network adapters' initializers.
2023-04-14 19:27:56 +02:00
Liav A
9f011592be Kernel/Net: Convert initializers to return NonnullRefPtr<NetworkAdapter>
There's no need for using NonnullLockRefPtr here.
2023-04-14 19:27:56 +02:00
Liav A
b02ee664e7 Kernel: Get rid of *LockRefPtr in the SysFS filesystem code
To do this we also need to get rid of LockRefPtrs in the USB code as
well.
Most of the SysFS nodes are statically generated during boot and are not
mutated afterwards.

The same goes for general device code - once we generate the appropriate
SysFS nodes, we almost never mutate the node pointers afterwards, making
locking unnecessary.
2023-04-14 19:24:54 +02:00
Liav A
dd7633c5f4 Kernel/Audio: Propagate errors when creating AudioChannels
While doing this, we can also just return a normal RefPtr instead of a
LockRefPtr, because we create these channels when initializing an audio
controller, and never change the pointer in AudioController instances
after their initialization, hence no locking is necessary.
2023-04-14 19:23:12 +02:00
Liav A
4921561687 Kernel/Audio: Simplify initialization sequence for drivers
Instead of enumerating all available controllers and then ask each to
find its audio channels, we change the initialization sequence to match
what happens in the Networking subsystem and Graphics subsystem - we
essentially probe for a matching driver on a PCI device, create a device
instance, and immediately initialize it.

This in fact allows us to immediately find any hardware initialization
issues and report it, and then dropping the created instance, as usually
being done in other initialization paths in the Kernel.

This also opens the opportunity to propagate errors when failed to
initialize an AudioChannel instance, and it will be addressed in a
future commit.
2023-04-14 19:23:12 +02:00
Liav A
0050358cd3 Kernel/Storage: Modernize ATA IDE controller initialization code
This is done by 2 ways which both fit very well together:
- We stop use LockRefPtrs. We also don't allow expansion of the
  m_channels member, by setting it to be a fixed Array of 2
  IDEChannels.
- More error propagation through the code, in the construction point of
  IDEChannel(s). This means that in the future we could technically do
  something meaningful with OOM conditions when initializing an IDE
  controller.
2023-04-14 19:20:43 +02:00
Liav A
dac7e911e6 Kernel/Storage: Remove unused IDEController::initialize() method 2023-04-14 19:20:43 +02:00
Liav A
93fceb1890 Kernel: Stop using LockRefPtrs in the Jail code
Each Jail object within the list is already protected by the global list
spinlock, therefore there's no need for using LockRefPtrs at all.
2023-04-14 19:17:49 +02:00
Liav A
e8510b6415 Kernel: Make Jail class to be AtomicRefCounted instead of RefCounted
This will help ensuring that taking and dropping a reference, hence
changing the ref-count, will be done in a safe manner in terms of
concurrency.
2023-04-14 19:17:49 +02:00
Tim Schumacher
9be5dcfd89 Kernel: Also search the main program for stack size requests 2023-04-14 16:12:04 +01:00
Tim Schumacher
ed74f792e2 Kernel: Pick the maximum out of the requested stack sizes 2023-04-14 16:12:04 +01:00
Arda Cinar
38dc54317c Kernel/Net: Implement SIOCGIFINDEX and SIOCGIFNAME for sockets
These ioctls exist on Linux and can be used to implement libc functions
if_indextoname and if_nametoindex (without needing to parse any JSON).
2023-04-14 12:29:03 +01:00
Timon Kruiper
9ed04bdb33 Kernel/aarch64: Add implementation of Processor::for_each 2023-04-13 20:24:25 +02:00
Timon Kruiper
4927eb5396 Kernel/aarch64: Change RPi::Framebuffer::PixelOrder to BGR
This is what the WindowServer expects. Confusingly the pixel format for
MULTIBOOT_FRAMEBUFFER_TYPE_RGB is actually BGRx8888.
2023-04-13 20:22:08 +02:00
Timon Kruiper
baa5cb9e30 Kernel/aarch64: Add volatile modifier to various asm statements
This prevents the optimizer from reordering them, which hopefully
prevents future bugs.
2023-04-13 20:22:08 +02:00
Timon Kruiper
10030038e9 Kernel/aarch64: Make sure no reordering of DAIF::read is possible
We were crashing on the VERIFY_INTERRUPTS_DISABLED() in
RecursiveSpinlock::unlock, which was caused by the compiler reordering
instructions in `sys$get_root_session_id`. In this function, a SpinLock
is locked and quickly unlocked again, and since the lock and unlock
functions were inlined into `sys$get_root_session_id` and the DAIF::read
was missing the `volatile` keyword, the compiler was free to reorder the
reads from the DAIF register to the top of this function. This caused
the CPU to read the interrupts state at the beginning of the function,
and storing the result on the stack, which in turn caused the
VERIFY_INTERRUPTS_DISABLED() assertion to fail. By adding the `volatile`
modifier to the inline assembly, the compiler will not reorder the
instructions.

In aa40cef2b7, I mistakenly assumed that the crash was related to the
initial interrupts state of the kernel threads, but it turns out that
the missing `volatile` keyword was the actual problem. This commit also
removes that code again.
2023-04-13 20:22:08 +02:00
Liav A
cbf78975f1 Kernel: Add the futimens syscall
We have a problem with the original utimensat syscall because when we
do call LibC futimens function, internally we provide an empty path,
and the Kernel get_syscall_path_argument method will detect this as an
invalid path.

This happens to spit an error for example in the touch utility, so if a
user is running "touch non_existing_file", it will create that file, but
the user will still see an error coming from LibC futimens function.

This new syscall gets an open file description and it provides the same
functionality as utimensat, on the specified open file description.
The new syscall will be used later by LibC to properly implement LibC
futimens function so the situation described with relation to the
"touch" utility could be fixed.
2023-04-10 10:21:28 +02:00
Liav A
fb8d4b7032 Kernel/Memory: Explain better why we don't use the first 1 MiB on x86_64 2023-04-09 19:40:45 -06:00
Liav A
5a94e8dfd0 Kernel: Ensure jailed processes can be reaped by a jailed parent process
We were detaching from the jail process list too early. To ensure we
detach properly, leverage the remove_from_secondary_lists method
so the possibly jailed parent process can still see the dying process
and therefore clean it properly.
2023-04-09 18:49:01 +02:00
Liav A
bfffe88de5 Kernel/HID: Untie the PS2 protocol, i8042 hardware and generic devices
For a very long time, the kernel had only support for basic PS/2 devices
such as the PS2 AT keyboard and regular PS2 mouse (with a scroll wheel).

To adapt to this, we had very simple abstractions in place, essentially,
the PS2 devices were registered as IRQ handlers (IRQ 1 and 12), and when
an interrupt was triggered, we simply had to tell the I8042Controller to
fetch a byte for us, then send it back to the appropriate device for
further processing and queueing of either a key event, or a mouse packet
so userspace can do something meaningful about it.

When we added the VMWare mouse integration feature it was easily adapted
to this paradigm, requiring small changes across the handling code for
these devices.

This patch is a major cleanup for any future advancements in the HID
subsystem.
It ensures we do things in a much more sane manner:
- We stop using LockRefPtrs. Currently, after the initialization of the
  i8042 controller, we never have to change RefPtrs in that class, as we
  simply don't support PS2 hotplugging currently.
  Also, we remove the unnecessary getters for keyboard and mouse devices
  which also returned a LockRefPtr.
- There's a clear separation between PS2 devices and the actual device
  nodes that normally exist in /dev. PS2 devices are not polled, because
  when the user uses these devices, they will trigger an IRQ which when
  is handled, could produce either a MousePacket or KeyEvent, depending
  on the device state.
  The separation is crucial for buses that are polled, for example - USB
  is a polled bus and will not generate an IRQ for HID devices.
- There's a clear separation in roles of each structure. The PS2 devices
  which are attached to a I8042Controller object are managing the device
  state, while the generic MouseDevice and KeyboardDevice manage all
  related tasks of a CharacterDevice, as well as interpreting scan code
  events and mouse relative/absolute coordinates.
2023-04-09 18:11:37 +02:00
Liav A
3a261f5ee8 Kernel: Stop lock & unlock repeatedly while reading from a mouse device
This was a thing we needed to do in the days where we didn't have
safe_memcpy and some wrappers around it to handle possible page faults
safely.
2023-04-09 18:11:37 +02:00
Liav A
d8cbda6950 Kernel: Move HIDDevice::enable_interrupts method to I8042Device class
It happens to be that only PS/2 devices that are connected via the i8042
controller can generate interrupt events, so it makes much more sense to
have those devices to implement the enable_interrupts method because of
the I8042Device class and not the HIDDevice class.
2023-04-09 18:11:37 +02:00
Liav A
d76c08c9b0 Kernel: Introduce a new super class called HIDController
Use the new class in HID code, because all other HID device controllers
will be using this class as their parent class.

Hence, we no longer keep a reference to any PS/2 device in HIDManagement
and rely on HIDController derived classes to do this for us.

It also means that we removed another instance of a LockRefPtr, which
is designated to be removed and is replaced by the better pattern of
SpinlockProtected<RefPtr<>> instead.
2023-04-09 18:11:37 +02:00
Liav A
6c4a47d916 Kernel: Remove redundant HID name from all associated files 2023-04-09 18:11:37 +02:00
Idan Horowitz
01318d8f9b Kernel: Check flags for MAP_FIXED instead of prot in sys$mmap
We were accidentally not enforcing the map_fixed pledge
2023-04-09 11:10:37 +03:00
Idan Horowitz
6b08b18a9a Kernel: Crash process instead of panicking on KSYMS access
Also do the same for READONLY_AFTER_INIT and UNMAP_AFTER_INIT.
2023-04-09 11:10:37 +03:00
Brian Gianforcaro
e891f13c84 Kernel: Fix compilation of aarch64/RPi/Framebuffer.cpp
The definitions were being defined already by `BootInfo.h` and that was
being included here via transitive includes. The extern definitions of
the variables do not have the `READONLY_AFTER_INIT` attribute in
`BootInfo.h`. This causes conflicting definitions of the same variable.

The `READONLY_AFTER_INIT` specifier is not needed for extern variables
as it only effects their linkage, not their actual use, so just use the
versions in `BootInfo.h` instead of re-declaring.
2023-04-08 19:16:35 -07:00
Liav A
7b745a20f1 Kernel: Mark a bunch of NonnullRefPtrs also const to ensure immutability
These were easy to pick-up as these pointers are assigned during the
construction point and are never changed afterwards.

This small change to these pointers will ensure that our code will not
accidentally assign these pointers with a new object which is always a
kind of bug we will want to prevent.
2023-04-08 13:44:21 +02:00
Andreas Kling
9264303f5d Kernel: Don't reuse old master TLS region data in sys$execve()
When switching to the new address space, we also have to switch the
Process::m_master_tls_* variables as they may refer to a region in
the old address space.

This was causing `su` to not run correctly.

Regression from 65641187ff.
2023-04-08 07:28:27 +02:00
Liav A
b87747fa64 Kernel: Fix typo in the FramebufferGetPitchMboxMessage name 2023-04-07 10:43:45 +02:00
Timon Kruiper
2b6c44852c Kernel/aarch64: Actually remove Arch/aarch64/init.cpp
The idea was to remove this file in bd2011406, but that did not actually
happen. Let's actually remove it.
2023-04-06 22:06:03 +03:00
Timon Kruiper
e9b40863ab Kernel: Make dispatch_signal work for aarch64 2023-04-06 21:19:58 +03:00
Timon Kruiper
00df1fc060 Kernel: Store FPU state when dispatching signal on aarch64
And make sure to also restore it in sys$sigreturn.
2023-04-06 21:19:58 +03:00
Timon Kruiper
4e00c63897 Kernel: Implement signal trampoline for aarch64
With this change, we are now able to successfully boot into the text
mode! :^)
2023-04-06 21:19:58 +03:00
Timon Kruiper
ec765544a5 Kernel/aarch64: Add getters/setters in RegisterState and ThreadRegisters
Specifically this commit implements two setters set_userspace_sp and
set_ip in RegisterState.h, and also adds a stack pointer getter (sp) in
ThreadRegisters.h. Contributed by konrad, thanks for that.
2023-04-06 21:19:58 +03:00
Timon Kruiper
36362b9679 Kernel/aarch64: Implement copying of kernel regs into ptrace regs
And also vice versa. Contributed by konrad, thanks for that.
2023-04-06 21:19:58 +03:00
Timon Kruiper
200e91cd7f Kernel+LibC: Modify aarch64's __mcontext to store registers in an array
This commit also removes the unnecessary ifdefs from
sys/arch/aarch64/regs.h. Contributed by konrad, thanks for that.
2023-04-06 21:19:58 +03:00
Timon Kruiper
7440112cd9 Kernel: Implement ScopedAddressSpaceSwitcher using PageDirectory
This makes the code architecture independent, and thus makes it work for
aarch64.
2023-04-06 21:19:58 +03:00