Commit graph

8685 commits

Author SHA1 Message Date
Space Meyer bba94804c2 Kernel: Deduplicate backtrace printing 2024-04-15 21:16:22 -06:00
Space Meyer a721e4d507 Kernel: Track KCOVInstance via Process instead of HashMap
While this clutters Process.cpp a tiny bit, I feel that it's worth it:
- 2x speed on the kcov_loop benchmark. Likely more during fuzzing.
- Overall code complexity is going down with this change.
- By reducing the code reachable from __sanitizer_cov_trace_pc code,
  we can now instrument more code.
2024-04-15 21:16:22 -06:00
Space Meyer fdc0328ce3 Kernel: Exclude individual functions from coverage instrumentation
Sticking this to the function source has multiple benefits:
- We instrument more code, by not excluding entire files.
- NO_SANITIZE_COVERAGE can be used in Header files.
- Keeping the info with the source code, means if a function or
  file is moved around, the NO_SANITIZE_COVERAGE moves with it.
2024-04-15 21:16:22 -06:00
Space Meyer ca89116a46 Kernel: Only build kcov object files, if feature is enabled 2024-04-15 21:16:22 -06:00
Space Meyer 106d4636a4 Revert "Kernel+SystemServer: Make KCOVDevice a character device"
This reverts commit 9dbec601b0.

For KCOV to be performant (or at least not even slower) we need to
mmap the PC buffer from both user and kernel space at the same time.
You can't mmap a character device, so this change didn't make sense.

Plus even if we did invent a new method to exfiltrate the coverage
information out of the kernel, it would be incompatible with existing
kernel fuzzers. That would be kind of annoying. 🙃
2024-04-15 21:16:22 -06:00
Sönke Holz ec5cfc031e Kernel/riscv64: Add Linux boot header
This allows us to boot via U-Boot's booti command.
2024-03-25 14:30:39 -06:00
Sönke Holz 1a312f4265 Kernel/riscv64: Only enable interrupts in trap handler if they were on
Always enabling interrupts is in hindsight obviously a bug, as trapping
code that has interrupts disabled very likely expects that they stay
disabled.
2024-03-25 14:21:41 -06:00
Sönke Holz 6a223c6210 Kernel/riscv64: Set g_total_processors to a hard-coded value of 1
This value is used by the NVMe driver to determine the number of queues
to create.
2024-03-25 14:20:39 -06:00
Sönke Holz 58a2e6412c Kernel/riscv64: Implement Processor::pause 2024-03-25 14:20:39 -06:00
Sönke Holz 040e0fe88c Kernel/riscv64: Implement microseconds_delay
This simple delay loop uses the time CSR to wait for the given amount
of time. The tick frequency of the CSR is read from the
/cpus/timebase-frequency devicetree property.
2024-03-25 14:20:39 -06:00
Sönke Holz 6654021655 Kernel/riscv64: Don't hard-code the page fault reason on RISC-V
Instead, rewrite the region page fault handling code to not use
PageFault::type() on RISC-V.

I split Region::handle_fault into having a RISC-V-specific
implementation, as I am not sure if I cover all page fault handling edge
cases by solely relying on MM's own region metadata.
We should probably also take the processor-provided page fault reason
into account, if we decide to merge these two implementations in the
future.
2024-03-25 14:18:38 -06:00
Sönke Holz 496a7541a2 Kernel/riscv64: Implement the signal trampoline 2024-03-25 14:17:32 -06:00
Sönke Holz 66f8d0f031 Kernel/riscv64: Add support for handling traps from userspace
This commit also removes the unnecessary user_sp RegisterState member.
We never use the kernel stack pointer on entry, so we can simply always
store the stack pointer of the previous privilege mode in sp.

Also remove the sp member from mcontext, as RISC-V doesn't have a
dedicated stack pointer register.
sp is defined to be x2 (x[1] in our case) by the ABI.

I probably accidentally included sp while copying the struct from
aarch64.
2024-03-25 14:14:43 -06:00
Sönke Holz afe9a12412 Kernel/riscv64: Handle syscalls
sepc has to be incremented before the call to syscall_handler,
as we otherwise would return to the ecall instruction, resulting in an
infinite trap loop.
We can't increment it after syscall_handler, as sepc might get changed
while handling the syscall.
2024-03-25 14:11:43 -06:00
Sönke Holz 04ca9f393f Kernel/riscv64: Implement create_thread 2024-03-25 14:10:05 -06:00
Sönke Holz 65724efac3 Kernel/riscv64: Implement fork 2024-03-25 14:10:05 -06:00
Sönke Holz faede8c93a Kernel/riscv64: Implement execve 2024-03-25 14:10:05 -06:00
Sönke Holz 6daa0da3c6 Kernel/NVMe: Fix calculation of "Maximum Queue Entries Supported" field
The value of this field is incremented by one, as a value of 0 for this
field means 1 entry supported.

A value of 0xffff for CAP.MQES would incorrectly by truncated to 0x0000,
if we don't increase the bit width of the return type.
2024-03-25 14:08:28 -06:00
Sönke Holz 378fa09a5a Kernel/riscv64: Fix typo (CSR::SATP::Mode::{Sv67 => Sv57}) 2024-03-20 10:36:10 -06:00
Tom Finet b9cfb50f71 Kernel/Net: Add TCPSocket timer for TimeWait moving to Closed
RFC9293 states that from the TimeWait state the TCPSocket
should wait the MSL (2mins) for delayed segments to expire
so that their sequence numbers do not clash with a new
connection's sequence numbers using the same ip address
and port number. The wait also ensures the remote TCP peer
has received the ACK to their FIN segment.
2024-03-14 18:33:19 -06:00
mrkubax10 eb0d56a4ed Kernel/Net: Implement support for RTL8168C
Please be aware that I only have NIC with chip version 6 so
this is the only one that I have tested. Rest was implemented
via looking at Linux rtl8169 driver. Also thanks to IdanHo
for some initial work.
2024-03-13 22:09:54 -06:00
Liav A 0734de9f9a Kernel+Userland: Add mount MS_SRCHIDDEN option
Either we mount from a loop device or other source, the user might want
to obfuscate the given source for security reasons, so this option will
ensure this will happen.
If passed during a mount, the source will be hidden when reading from
the /sys/kernel/df node.
2024-03-13 15:33:47 -06:00
Liav A 0d2e4a7e67 Kernel/FileSystem: Add the DevLoopFS filesystem
Similarly to DevPtsFS, this filesystem is about exposing loop device
nodes easily in /dev/loop, so userspace doesn't need to do anything in
order to use new devices immediately.
2024-03-13 15:33:47 -06:00
Liav A 11ead5c84f Kernel: Get RefPtr<Device> from the DeviceManagement::get_device method
Instead of returning a raw pointer, which could be technically invalid
when using it in the caller function, we return a valid RefPtr of such
device.

This ensures that the code in DevPtsFS is now safe from a rare race
condition in which the SlavePTY device is gone but we still have a
pointer to it.
2024-03-13 15:33:47 -06:00
Liav A 5dcf03ad9a Kernel/Devices: Introduce the LoopDevice device
This device is a block device that allows a user to effectively treat an
Inode as a block device.

The static construction method is given an OpenFileDescription reference
but validates that:
- The description has a valid custody (so it's not some arbitrary file).
  Failing this requirement will yield EINVAL.
- The description custody points to an Inode which is a regular file, as
  we only support (seekable) regular files. Failing this requirement
  will yield ENOTSUP.

LoopDevice can be used to mount a regular file on the filesystem like
other supported types of (physical) block devices.
2024-03-13 15:33:47 -06:00
Timothy Flynn 4b777397b5 Kernel: Define bitwise operations for KeyModifier
This type is designed to be use as a flag. Define bitwise operations for
convenience.
2024-03-06 07:46:18 +01:00
Timothy Flynn 836d93f7e3 Kernel: Replace C-idioms with AK types in RTL8168 network adapter
Instead of using C-arrays, and manually counting their lengths, use
AK::Array. And pass these arrays around as spans, instead of as pointer-
and-length pairs.
2024-03-04 20:00:52 +01:00
Idan Horowitz 209c588ed1 Kernel: Switch a couple of signal dispatch dbglns to dbgln_if
These are pretty spammy when using strace.
2024-03-02 09:10:14 +01:00
Hendiadyoin1 b0fc5bea91 Kernel/PCI: Prefer structured bindings when iterating over HashMaps
This makes `Access::rescan_hardware` look a bit nicer.
2024-03-01 14:05:53 -07:00
Hendiadyoin1 3e3b34ab8a Kernel/NVMe: Use a struct for the namespace features, instead of a Tuple 2024-03-01 14:05:53 -07:00
Sönke Holz cdc0c9f094 Kernel/Storage: Don't allocate IRQs in NVMeCntlr when nvme_poll passed 2024-02-25 17:20:40 -07:00
Liav A fff49ab6d3 Kernel/FileSystem: Avoid double locking m_inode_lock in the Ext2 driver 2024-02-24 16:45:26 -07:00
Liav A b63a1dda63 Kernel/FileSystem: Enforce locking of m_inode_lock when truncating Inode
Such operation is almost equivalent to writing on an Inode, so lock the
Inode m_inode_lock exclusively.
All FileSystem Inode implementations then override a new method called
truncate_locked which should implement the actual truncating.
2024-02-24 16:45:26 -07:00
Hendiadyoin1 53dd04e219 Kernel/riscv64: Read the timebase-frequency from the device tree 2024-02-24 16:43:44 -07:00
Hendiadyoin1 3941277940 Kernel/riscv64: Get the kernel command line from the device tree
This also hides the fdt dump by default now,
it can be activated by adding `dump_fdt` to the kernel command line
2024-02-24 16:43:44 -07:00
Hendiadyoin1 a99bd8eda6 Kernel/riscv64: Initialize PCI controllers using the device tree
Currently the search path is limited to `/soc/pci*` but this is enough
to get it to work on qemu.
2024-02-24 16:43:44 -07:00
Hediadyoin1 7309427d2f Kernel/riscv64: Unflatten the DeviceTree 2024-02-24 16:43:44 -07:00
Hendiadyoin1 d3f6b03733 Kernel/riscv64: Take the memory map from the FDT and dump it
For this the BootInfo struct was made architecture specific
2024-02-24 16:43:44 -07:00
Sönke Holz 1fc0c84017 Kernel/riscv64: Implement Processor::assume_context
This code is based on the aarch64 implementation.
2024-02-24 16:42:58 -07:00
Sönke Holz 726865592c Kernel/riscv64: Implement Processor::switch_context
This code is based on the aarch64 implementation.
2024-02-24 16:42:58 -07:00
Sönke Holz 494e026ca7 Kernel/riscv64: Implement Processor::init_context
This code is based on the aarch64 implementation.
2024-02-24 16:42:58 -07:00
Sönke Holz efdc433ebc Kernel/riscv64: Implement thread_context_first_enter
thread_context_first_enter reuses the context restoring code in the
trap handler, just like other arches already do.

The `ld x2, 1*8(sp)` is unnecessary in the trap handler, as the stack
pointer should be equal to the stack pointer slot in the RegisterState
if the trap is from supervisor mode (and we currently don't support
user traps).
This load will however make us unable to reuse that code for
thread_context_first_enter.
2024-02-24 16:42:58 -07:00
Sönke Holz 7fbcceb657 Kernel/riscv64: Implement enter_thread_context
This code is based on the aarch64 implementation.
2024-02-24 16:42:58 -07:00
Sönke Holz 2f33e7a964 Kernel/riscv64: Stub out Processor::set_thread_specific_data 2024-02-24 16:42:58 -07:00
Sönke Holz 7fff6eef09 Kernel/riscv64: Implement Processor::initialize_context_switching
This code is based on the aarch64 implementation.
2024-02-24 16:42:58 -07:00
Sönke Holz 1429c83942 Kernel/riscv64: Implement {load,store}_fpu_state
This commit adds two functions which save/restore the entire FPU state.
On RISC-V, you only need to save the floating pointer registers
themselves and the fcsr CSR, which contains the entire state of the F/D
extensions.
2024-02-24 16:42:58 -07:00
Sönke Holz cd389833d4 Kernel/riscv64: Use TimeManagement as an entropy source for now
Better entropy will require us to parse the riscv,isa devicetree
property and then use the Zkr extension, if present.
2024-02-24 16:37:20 -07:00
Sönke Holz f6151b3c9e Kernel/Storage: Add AssertSizes to AHCI structs 2024-02-24 16:36:47 -07:00
Sönke Holz f85099ea47 Kernel/Storage: Make AHCI::HBA::port_regs a flexible array member
Some real hardware apparently uses smaller BAR sizes than sizeof(HBA)
with a completely filled port_regs member.
Change the port_regs array to a flexible array member, so we don't panic
while verifying that the BAR size is large enough to map this struct.
Accesses to this array are already bounds checked against
AHCI::Limits::MaxPorts.
2024-02-24 16:36:47 -07:00
Sönke Holz 6fd02d2a04 Kernel: Add PCI::map_bar overloads that don't take an explicit size
These overloads map the entire BAR space instead.
2024-02-24 16:36:47 -07:00
Liav A c216e7439f Kernel: Require StorageDevice reference for StorageDevicePartition
Allowing creation of StorageDevicePartition objects for any arbitrary
BlockDevice objects means that we could technically create a
StorageDevicePartition for another StorageDevicePartition which is
obviously not the intention for this code. Instead, require to pass a
StorageDevice reference to ensure this cannot happen.
2024-02-24 16:01:26 -07:00
Liav A f08f339e9a Kernel: Return EIO if attached device is gone in StorageDevicePartition
The previous code will just do a nullptr-dereference if the underlying
device is gone, so instead gracefully return EIO error.
2024-02-24 16:01:26 -07:00
Liav A 67e028e4f4 Kernel: Declare proper blocking support for StorageDevicePartitions 2024-02-24 16:01:26 -07:00
Liav A 42ed0a6c94 Kernel/Storage: Rename DiskPartition => StorageDevicePartition
We deal with partitions for storage devices, not only for disk devices,
originally harddrives.
2024-02-24 16:01:26 -07:00
Liav A 4e651d0e4f Kernel/Storage: Convert StorageDevice class members to be const
It is expected that these class members will be set when the object is
created (so they're set in the class constructor method) and never
change again, as its the driver responsibility to find these values
before creating a StorageDevice object.

This makes it easier to rely on these values later on as we don't expect
them to ever change for a StorageDevice object during its lifetime.
2024-02-24 16:01:26 -07:00
Liav A c8bdcd90a8 Kernel/Storage: Fix StorageDevice STORAGE_DEVICE_GET_SIZE ioctl option
It calculated the disk size with the zero-based max addressable block
value.
For example, for a disk device with a block size of 512 bytes that has 2
LBAs so it can address LBA 0 and LBA 1 (so m_max_addressable_block is 1)
the calculated disk size will be 512 instead of 1024 bytes.
2024-02-24 16:01:26 -07:00
Liav A 763ef690c6 Kernel/Storage: Declare proper blocking support for StorageDevices
We remove can_read() and can_write(), as both of these methods should be
implemented for proper blocking support.
For our case, the previous code will simply block the user if they tries
to read beyond the max addressable offset, which is not a correct
behavior.

Instead, just do proper EOF guarding when calling read() and write() on
such objects.
2024-02-24 16:01:26 -07:00
Liav A c33246235a Kernel/Storage: Change semantics for addressing block methods
Add a method for matehmatical operations when verifying IO operation
boundaries.
Also, make max_addressable_block method non-virtual, since no other
derived class actually has ever overrided this method.
2024-02-24 16:01:26 -07:00
implicitfield a69e113d49 Kernel: Add a definition for the FAT32 FSInfo structure 2024-02-24 15:54:52 -07:00
implicitfield 8b77737f8e Kernel: Move FAT structure definitions to Kernel/API
We need to be able to include these definitions from userspace as the
upcoming mkfs.fat utility will depend on these.
2024-02-24 15:54:52 -07:00
Tom Finet a7db718ffb Kernel/Net: Send RST packet when socket receives in closed state
RFC9293 states that a closed socket should reply to all non-RST
packets with an RST. This change implements this behaviour as
specified in section 3.5.2 in bullet point 1.
2024-02-24 15:49:46 -07:00
Fabian Dellwing 5180e41918 Kernel: Call did_modify_contents() after truncate()
Truncating a file should absolutly mark it as modified. With this
change our `tail` implementation can correctly handle it.
2024-02-24 15:48:36 -07:00
Thomas Queiroz 2de9ffa632 Kernel+Tests: Don't panic when LocalSocket is already bound 2024-02-23 09:49:23 +01:00
Sönke Holz 06a32b6e16 Kernel/USB: Handle non-page aligned EHCI controller BARs correctly
BARs don't have to be page-aligned. This patch correctly calculates the
needed memory range and register base address in that region.
2024-02-22 07:36:45 +01:00
Sönke Holz ecf41e13ad Kernel/Graphics: Probe PCI graphics adapters on all architectures 2024-02-21 22:36:00 +01:00
Nico Weber 24a469f521 Everywhere: Prefer {:#x} over 0x{:x} in format strings
The former automatically adapts the prefix to binary and octal
output, and is what we already use in the majority of cases.

Patch generated by:

    rg -l '0x\{' | xargs sed -i '' -e 's/0x{:/{:#/'

I ran it 4 times (until it stopped changing things) since each
invocation only converted one instance per line.

No behavior change.
2024-02-21 17:54:38 +01:00
Sönke Holz 227818de9b Kernel/riscv64: Don't use the memory before .text as the initial stack
This new stack has the same size as the x86_64 prekernel stack (32K).
2024-02-19 00:41:17 +01:00
Tom Finet 3b6a300ed7 Kernel: Resolve FIXME relating to the gate type arg for x86 Interrupts
According to the Intel Software Developer's Manual Volume 3A section
6.12.1.3, the interrupt gate type means the IF flag is cleared to
prevent nested interruption. The trap gate type does not modify the
IF flag. Thus the gate type argument is important when constructing
an IDTEntry.
2024-02-19 00:16:05 +01:00
Tom Finet 9e8e93d91d Kernel: Remove storage_segment from IDTEntry constructor
On x86_64 and x86, storage_segment (bit 12 counting from 0)
is always 0 according to the Intel Software Developer's Manual,
volume 3A, section 6.11 and section 6.14.1. It has therefore
been removed as a parameter from IDTEntry's constructor and
hardwired to 0.
2024-02-19 00:16:05 +01:00
John te Bokkel 9d62dfabbb Kernel/HID: Modify index for shifted scan codes of non-printing chars
Several non-printing keys were assigned key map index values when they
should have been ignored using index 0xFF. This resolves issue 23185.
2024-02-19 00:03:50 +01:00
Tim Ledbetter 532f2b644c Kernel: Propagate errors with TRY() where possible 2024-02-14 17:46:06 -05:00
Sönke Holz 570480c7d5 Kernel/riscv64: Add missing include in Delay.cpp
This include was missed in 4409b33145.
RISC-V should build again now.
2024-02-14 10:32:45 -07:00
Jelle Raaijmakers 96675e61cd Kernel/TTY: Support non-blocking reads with termios VMIN == 0
In a TTY's non-canonical mode, data availability can be configured by
setting VMIN and VTIME to determine the minimum amount of bytes to read
and the timeout between bytes, respectively. Some ports (such as SRB2)
set VMIN to 0 which effectively makes reading a TTY such as stdin a
non-blocking read. We didn't support this, causing ports to hang as soon
as they try to read stdin without any data available.

Add a very duct-tapey implementation for the case where VMIN == 0 by
overwriting the TTY's description's blocking status; 3 FIXMEs are
included to make sure we clean this up some day.
2024-02-13 19:48:39 +01:00
Fabian Dellwing 32b07f7057 Kernel: Make ftruncate change st_ctime as per spec
Previously we only modified `st_mtime` but the spec tells
us to do both: https://pubs.opengroup.org/onlinepubs/007908799/xsh/ftruncate.html
2024-02-12 16:27:19 +01:00
Nico Weber 4409b33145 AK: Make IndexSequence use size_t
This makes it possible to use MakeIndexSequqnce in functions like:

    template<typename T, size_t N>
    constexpr auto foo(T (&a)[N])

This means AK/StdLibExtraDetails.h must now include AK/Types.h
for size_t, which means AK/Types.h can no longer include
AK/StdLibExtras.h (which arguably it shouldn't do anyways),
which requires rejiggering some things.

(IMHO Types.h shouldn't use AK::Details metaprogramming at all.
FlatPtr doesn't necessarily have to use Conditional<> and ssize_t could
maybe be in its own header or something. But since it's tangential to
this PR, going with the tried and true "lift things that cause the
cycle up to the top" approach.)
2024-02-11 18:53:00 +01:00
Idan Horowitz e38ccebfc8 Kernel: Stop swallowing thread unblocks while process is stopped
This easily led to kernel deadlocks if the stopped thread held an
important global mutex (like the disk cache lock) while blocking.
Resolve this by ensuring stopped threads have a chance to return to the
userland boundary before actually stopping.
2024-02-10 08:42:53 +01:00
Idan Horowitz 458e990b7b Kernel: Stop locking the scheduler spinlock before the ptrace mutex
Locking a mutex while holding a spinlock is always wrong, but in the
case of the scheduler lock, it also causes an assertion failure. (Which
would be triggered by 2 separate threads trying to ptrace at the same
time).
2024-02-10 08:42:53 +01:00
Idan Horowitz 03cb3e5370 Kernel: Move NVMeQueue's m_cq_lock to NVMePollQueue
It's only used by that subclass, so there's no reason for it to be
defined as part of the parent class.
2024-02-10 08:42:53 +01:00
Idan Horowitz 45aee20ea9 Kernel: Switch to SpinlockProtected to protect NVMeQueue's requests map
This helps ensure no one accidentally accesses m_requests without first
locking it's spinlock. In fact this change fixed such a case, since
process_cq() implicitly assumed the caller locked the lock, which was
not the case for NVMePollQueue::submit_sqe().
2024-02-10 08:42:53 +01:00
Idan Horowitz 263127f21a Kernel: Actually report nvme request status
Due to an incorrect lambda scope capture declaration, we would copy the
result status at the start of the function, before it actually got
updated with the final status. Capture it by reference instead to
ensure we report the updated result.
2024-02-10 08:42:53 +01:00
Idan Horowitz 38dad2e27f Kernel: Replace bespoke & rickety NVMeIO synchronization mechanism
Instead of assuming data races won't occur and trying to somehow verify
it with manual un-atomic tracking, we can just use a recursive spinlock
instead of a normal one, to resolve the original deadlock.
2024-02-10 08:42:53 +01:00
Idan Horowitz a957907f4b Kernel: Merge NVME Queue complete_current_request implementations
Most of the actual logic is identical, with the only real difference
being that one wraps it with an async work item.
Merge the implementations to reduce duplications (which will also
require the fixes in the next commits to only be done once).
2024-02-10 08:42:53 +01:00
Tim Ledbetter a580118e53 Kernel/HID: Set Caps Lock state before doing code point remapping
This restores Caps Lock functionality, while still ensuring that
shortcut keys are remapped correctly.
2024-02-09 12:28:08 -07:00
Sönke Holz d0e3065ced Kernel/riscv64: Don't touch reserved fields of the sstatus CSR in boot.S
Multiple fields in sstatus are defined as WPRI "Reserved Writes Preserve
Values, Reads Ignore Values", which means we have to preserve their
values when writing to other fields in the same CSR.

We don't really need to touch any fields except SIE.
Interrupts are probably already disabled, but just to be safe,
disable them explicitly.
2024-02-08 08:13:47 -07:00
John te Bokkel d70424bb0f Kernel/HID: Move code_point assignment before its use key in assignment
We need to handle the character map to set the code point before we can
reassign the correct key to the queued_event.key. This fixes keyboard
shortcuts using the incorrect keys based on the keyboard layout.
2024-02-08 08:06:41 -07:00
Sönke Holz c0d7749654 Kernel/riscv64: Add QEMU virt machine memory map
We can remove this temporary memory map once we support parsing the
device tree provided to us by the firmware.
2024-02-08 07:16:20 -07:00
Tom Finet 227aa8f2e4 Kernel/Net: E1000ENetworkAdapter detect EEPROM
The presence of EEPROM is detected by reading the
EE_PRES bit from the EECD register.
2024-02-08 07:05:46 -07:00
Ali Mohammad Pur 54ab6fe5b9 LibVT+Everywhere: Introduce 'automarks' and 'clear previous command'
Automarks are similar to bookmarks placed by the terminal, allowing the
user to selectively remove a single command and its output from the
terminal scrollback.
This commit implements a single way to add marks: automatically placing
them when the shell becomes interactive.

To make sure the shell behaves correctly after its expected prompt
position changes, the terminal layer forces a resize event to be passed
to the shell on such (possibly) partial clears; this also has the nice
side effect of fixing the disappearing prompt on the preexisting "clear
including history" action: Fixes #4192.
2024-02-07 00:43:11 +01:00
Sönke Holz 8878e0c815 Kernel/riscv64: Implement everything in PageDirectory.cpp
This code is heavily based on aarch64's PageDirectory.cpp.
2024-01-29 05:17:03 -07:00
Sönke Holz 0a4ef6f3b7 Kernel/riscv64: Stub out InterruptManagement::find_controllers
We don't support any IRQControllers for RISC-V (like the PLIC) yet,
so just do nothing here for now.
2024-01-23 13:13:18 -07:00
Sönke Holz cec20908a5 Kernel/riscv64: Add assembly trap handler
This trap handler can't handle traps from userspace yet.
2024-01-23 13:13:18 -07:00
Sönke Holz df21d435eb Kernel/riscv64: Add C++ trap handler
This is a basic trap handler which can handle interrupts and some
exceptions. Syscalls aren't handled yet.
2024-01-23 13:13:18 -07:00
Sönke Holz 0111fe0d24 Kernel/riscv64: Implement Processor::exit_trap
This function is copy-pasted from aarch64's Processor.cpp
2024-01-23 13:13:18 -07:00
Sönke Holz 0e6659d1eb Kernel/riscv64: Implement dump_registers() 2024-01-23 13:13:18 -07:00
Sönke Holz 9bbf513c27 Kernel/riscv64: Implement initialize_interrupts() 2024-01-23 13:13:18 -07:00
Sönke Holz 7b7578bc1b Kernel/riscv64: Add AK::Formatter for scause CSR 2024-01-23 13:13:18 -07:00
Sönke Holz 6d8378735b Kernel/riscv64: Add AK::Formatter for sstatus CSR 2024-01-23 13:13:18 -07:00
Sönke Holz d061da4cf5 Kernel/riscv64: Clean up Timer class
I just copy-pasted microseconds_since_boot and
set_interrupt_interval_usec from aarch64.
However, on RISC-V, they are not in microseconds.
The TimerRegisters struct is also unused.

current_time and set_compare can also be private and static.
2024-01-23 13:13:18 -07:00
Sönke Holz 8582f0720f Kernel/riscv64: Make RISC-V Timer inherit from GenericInterruptHandler
IRQHandler is not the correct class to inherit from, as the timer
is not connected to an IRQController.
Each hart has one of these Timers directly connected to it.
2024-01-23 13:13:18 -07:00
Sönke Holz 8c017c3078 Kernel/riscv64: Remove unnecessary InterruptController header
I originally added this header because I misunderstood how
IRQControllers are supposed to be used.
I thought that I would need a IRQController class for the hart-local
interrupt controller, but apparently, this class is supposed to be used
for non-local interrupt controllers like the IOAPIC or RISC-V PLIC.
x86 LAPICs don't use this class either.
2024-01-23 13:13:18 -07:00
Sönke Holz 0e6d87fe83 Kernel/riscv64: Don't disable stack protector and sanitizers
I am not sure why 096cecb95e disabled the stack protector and sanitizers
for all files, but this is not necessary.
Only the pre_init code needs to run without them, as that code runs
identity mapped.
2024-01-22 20:07:36 -07:00
Jelle Raaijmakers f5dec55fd6 Kernel: Correct mapping of PS/2 keyboard exclamation point
Pressing Shift+1 resulted in `Key_Escape` being sent as the event's key.
2024-01-14 15:06:37 -07:00
Jelle Raaijmakers 015622bc22 Kernel: Set correct KeyCode count
The underlying value of the `KeyCode::Key_*` enum values starts at 0,
so we should add 1 to `Key_Menu` to get the correct count.
2024-01-14 15:06:37 -07:00
Liav A a10e63f08e Kernel/FileSystem: Send proper filetypes when traversing RAM-backed FSes
SysFS, ProcFS and DevPtsFS were all sending filetype 0 when traversing
their directories, but it is actually very easy to send proper filetypes
in these filesystems.
This patch binds all RAM backed filesystems to use only one enum for
their internal filetype, to simplify the implementation and allow
sharing of code.
Please note that the Plan9FS case is currently not solved as I am not
familiar with this filesystem and its constructs.

The ProcFS mostly keeps track of the filetype, and a fix was needed for
the /proc root directory - all processes exhibit a directory inside it
which makes it very easy to hardcode the directory filetype for them.
There's also the `self` symlink inode which is now exposed as DT_LNK.

As for SysFS, we could leverage the fact everything inherits from the
SysFSComponent class, so we could have a virtual const method to return
the proper filetype.
Most of the files in SysFS are "regular" files though, so the base class
has a non-pure virtual method.

Lastly, the DevPtsFS simply hardcodes '.' and '..' as directory file
type, and everything else is hardcoded to send the character device file
type, as this filesystem is only exposing character pts device files.
2024-01-13 19:01:07 -07:00
Sönke Holz 959f2c0342 Kernel/riscv64: Implement debug_output and change cmdline to use it
Just implementing `debug_output` (and adding "serial_debug" to the
cmdline) makes the kernel print its first assertion failure!
2024-01-12 16:46:46 -07:00
Sönke Holz dfce2f6341 Kernel/riscv64: Explicitly discard temporary local symbols
This is for some reason needed for riscv64 clang, as otherwise the
kernel.map file would grow too big to fit in its section inside the
kernel image.
None of our other architectures have temporary locals in their
kernel.map.
2024-01-12 16:46:46 -07:00
Sönke Holz da33e2a564 Kernel/riscv64: Add MMU initialization code
We initialize the MMU by first setting up the page tables for the
kernel image and the initial kernel stack.
Then we jump to a identity mapped page which makes the newly created
kernel root page table active by setting `satp` and then jumps to
`init`.
2024-01-12 16:46:46 -07:00
Sönke Holz 27860cfaa2 Kernel/riscv64: Add a basic trap handler to pre_init
This trap handler uses the SBI to print an error message via a newly
introduced panic function, which is necessary as `pre_init` is running
identity mapped.
Also add a header file for `pre_init.cpp` as we wan't to use the panic
and `dbgln` function in the MMU init code as well.
2024-01-12 16:46:46 -07:00
Jared dd53f64d2f Kernel: Properly ack segment
Fixed a mistake where we did not acknowledge a segment in the FinWait2
state.
2024-01-12 16:29:08 -07:00
Sönke Holz e4244f7469 Kernel/riscv64: Implement support for "panic=shutdown" cmdline option 2024-01-12 16:25:46 -07:00
Sönke Holz 8547813b6d Kernel/riscv64: Implement arch_specific_{reboot,shutdown} using the SBI
We first try to use the newer "SRST" extension for rebooting and
shutting down and if that fails, we try to shutdown using the legacy
"System Shutdown" extension (which can't reboot, so we always shutdown).
The kernel will halt, if we return from here due to all attempts at
rebooting / shutting down failing.
2024-01-12 16:25:46 -07:00
Sönke Holz 21b2d1de65 Kernel/riscv64: Add AK::Formatter for SBI errors
This allows us to print errors returned to us via the SBI.
The error messages are taken from the SBI spec.
2024-01-12 16:25:46 -07:00
Sönke Holz 27087318bc Kernel/riscv64: Add support for SRST "System Reset" SBI extension
This extension will be used for rebooting and shutting down.
2024-01-12 16:25:46 -07:00
Sönke Holz cac7dc8d71 Kernel/riscv64: Add support for legacy "System Shutdown" SBI extension 2024-01-12 16:25:46 -07:00
Liav A 8a0a3638f0 Kernel/HID: Introduce the all-mice device
This device will be used by userspace to read mouse packets from all
mouse devices that are attached to the machine.

This change is a preparation before we can enable seamless hotplug
capabilities in WindowServer for mouse devices, without any major change
on the userspace side.
2024-01-12 16:08:08 -07:00
Liav A b634792022 Kernel: Enable i8042 first port translation by default
Without this, we have many issues with the keyboard, so enable this for
now until this is figured out.
2024-01-12 16:02:13 -07:00
Liav A 2a5f66e4d8 Kernel/HID: Fix PS2 keyboard scan code tables
We do this by implementing the following fixes:
- The Key_Plus is assigned to a proper map entry index now which is 0x4e
  both on the keypad and non-keypad keys.
- Shift+Q now prints out "Q" properly on scan code set 2.
- Key BackSlash (or Pipe on shift key being pressed down) is now working
  properly as well.
- Key_Pipe (which is "|" for en-US layout) is now working in scan code
  set 2.
- Numpad keys as well as the decimal separator key are working again.
2024-01-12 16:02:13 -07:00
Hendiadyoin1 23d6c88027 Kernel/MM: Don't allocate a temporary Vector when parsing the memory map
Instead we can achieve the same by just using an optional.
2024-01-12 15:59:47 -07:00
Hendiadyoin1 adac43ab1c Kernel: Use new PCI BAR API in IOWindow and correct IO bounds checks
The IO bounds checks were on 32 bit instead of the actual limit of
16 bit, which would have caused problems later down the line.
2024-01-12 15:59:47 -07:00
Hendiadyoin1 bd118f4eb0 Kernel: Use the new BAR address API for the NVMe stack
This includes changing the cached `m_bar` value to a `PhysicalAddress`
2024-01-12 15:59:47 -07:00
Hendiadyoin1 2dc20f9e39 Kernel: Use the new API to query and map BAR spaces in most places
This might be a bit overkill in some instances, but it's nice to be
consistent
2024-01-12 15:59:47 -07:00
Hendiadyoin1 c65455e122 Kernel: Expand BAR address mask up to 64 bit
Otherwise we would end up truncating the address when applying the mask

Co-Authored-By: Sönke Holz <sholz8530@gmail.com>
2024-01-12 15:59:47 -07:00
Hendiadyoin1 2f98c7d470 Kernel: Add convenience helpers for mapping PCI BAR spaces 2024-01-12 15:59:47 -07:00
Taj Morton 55cd89aea8 Kernel/FileSystem/FATFS: Use AssertSize to enforce FAT structure sizes 2024-01-12 15:54:46 -07:00
Taj Morton d6a519e9af Kernel/FileSystem/FATFS: Restrict reads to the size of the file
Resolves issue where empty portions of a sector (or empty sectors in
a cluster) would be included with the returned data in a read().
2024-01-12 15:54:46 -07:00
Taj Morton 2995ee5858 Kernel/FileSystem/FATFS: Support FAT12 file system clusters 2024-01-12 15:54:46 -07:00
Taj Morton 1f70a728f0 Kernel/FileSystem/FATFS: Support FAT16 file system clusters 2024-01-12 15:54:46 -07:00
Taj Morton 67f567348f Kernel/FileSystem/FATFS: Support for FAT12/16 DOS BIOS Parameter Blocks 2024-01-12 15:54:46 -07:00
hanaa12G 7abda6a36f Kernel: Add new sysconf option _SC_GETGR_R_SIZE_MAX 2024-01-06 04:59:50 -07:00
jared 3dfd8defa9 Kernel: Properly ack segments in the half-close state
We didn't proberly ack incoming packets while in the FinWait2. This fix
addresses that.
2024-01-06 03:13:01 -07:00
jared 7244369aff Kernel: Add unhandled cases for the FinWait2 state
According to RFC 9293 Section 3.6.1. Half-Closed Connections, we should
still accept incoming packets in the FinWait2 state. Additionally, we
didn't handle the FIN+ACK case. We should handle this the same we
handle the FIN flag. The ACK is only added to signify successful
reception of the last packet.
2024-01-06 03:13:01 -07:00
Idan Horowitz c5187c6bb3 Kernel: Replace incorrect RTL8168 multicast config registers address
The specification uses awkward numbering, marking the first byte as 7,
and the last one as 0, which caused me to misunderstand their ordering,
and use the last byte's address as the first one, and so on.
2024-01-05 10:52:43 -07:00
implicitfield 48e848a9fd Kernel/Ext2: Only handle extended attributes when they are supported 2024-01-05 04:00:11 +03:30
implicitfield 280d5feac9 Kernel/Ext2: Allow checking for the presence of extended attributes 2024-01-05 04:00:11 +03:30
Liav A 475ef6965a Kernel: Add boot parameter to determine i8042 first port translation
This can be used mainly for bare metal hardware, if the user experiences
problems with output from the PS2 keyboard.
2024-01-04 10:38:03 -07:00
Liav A c8f27d7cb8 Kernel+Userland: Implement support for PS2 scan code set 2
This scan code set is more advanced than the basic scan code set 1, and
is required to be supported for some bare metal hardware that might not
properly enable the PS2 first port translation in the i8042 controller.

LibWeb can now also generate bindings for keyboard events like the Pause
key, as well as other function keys (such as Right Alt, etc).

The logic for handling scan code sets is implemented by the PS2 keyboard
driver and is abstracted from the main HID KeyboardDevice code which
only handles "standard" KeyEvent(s).
2024-01-04 10:38:03 -07:00
Sönke Holz 6bc16ad62e Kernel/riscv64: Add RISC-V support to TimeManagement 2024-01-02 06:50:59 -07:00
Sönke Holz 9e4286d782 Kernel/riscv64: Stub out optional_current_time
This function is used in TimeManagement.cpp
2024-01-02 06:50:59 -07:00
Sönke Holz 4292b0ead7 Kernel/riscv64: Add a Timer class for RISC-V
This is a basic Timer class based on the aarch64 RPi Timer.
It uses the hart-local timer, as defined by the privileged ISA.
2024-01-02 06:50:59 -07:00
Sönke Holz 3286a05de1 Kernel/riscv64: Add enum for scause CSR 2024-01-02 06:50:59 -07:00
Sönke Holz 26752ee8df Kernel: Only write text to serial console if no ConsoleDevice attached
Otherwise we write everything twice on the serial port
2024-01-02 06:16:53 -07:00
Sönke Holz b060643941 Kernel: Remove outdated comment from console_out 2024-01-02 06:16:53 -07:00
Sönke Holz 0c8c0ff412 Kernel/riscv64: Fix backtrace generation on RISC-V
RISC-V uses a different convention for storing stack frame information
described here: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc#frame-pointer-convention
This part of the psABI is not yet in a ratified version, but both GCC
and Clang seem to use this convention.

Note that the backtrace dumping code still won't work for the initial
stack, as it is located before `kernel_mapping_base`.
2023-12-30 23:24:18 +01:00
Idan Horowitz bbceb155ce Kernel: Restrict KASLR randomization range when KASAN is enabled
To allow for easy mapping between the kernel virtual addresses and
KASAN shadow memory, we map shadow memory at the very end of the
virtual range, so that we can index into it using just an offset.
To ensure this range is free when needed, we restrict the possible
KASLR range when KASAN is enabled to make sure we don't use the end of
the virtual range.

This fixes the random kernel panics that could occur when KASAN is
enabled, if the kernel was randomly placed at the very end of the
virtual range.
2023-12-30 23:18:49 +01:00
Idan Horowitz f7a1f28d7f Kernel: Add initial basic support for KASAN
This commit adds minimal support for compiler-instrumentation based
memory access sanitization.
Currently we only support detection of kmalloc redzone accesses, and
kmalloc use-after-free accesses.

Support for inline checks (for improved performance), and for stack
use-after-return and use-after-return detection is left for future PRs.
2023-12-30 13:57:10 +01:00
Andrew Kaster d3025668a4 Revert "Kernel+Userland: Implement support for PS2 scan code set 2"
This reverts commit 61a385fc01.

The commit broke the shift and caps lock key from working.
2023-12-29 22:02:19 +01:00
Andrew Kaster 68b0826107 Revert "Kernel: Add boot parameter to determine i8042 first port translation"
This reverts commit 0379742d7e.

Commit 61a385fc01 breaks the shift and
caps lock key, but depends on this one.
2023-12-29 22:02:19 +01:00
Sönke Holz 28a3089dc3 Kernel/riscv64: Return correct range in kernel_virtual_range on RISC-V
riscv64 doesn't use a prekernel, so use the same code as aarch64 for
determining the kernel virtual address range.
2023-12-29 16:45:08 +01:00
Liav A 0379742d7e Kernel: Add boot parameter to determine i8042 first port translation
This can be used mainly for bare metal hardware, if the user experiences
problems with output from the PS2 keyboard.
2023-12-29 16:40:59 +01:00
Liav A 61a385fc01 Kernel+Userland: Implement support for PS2 scan code set 2
This scan code set is more advanced than the basic scan code set 1, and
is required to be supported for some bare metal hardware that might not
properly enable the PS2 first port translation in the i8042 controller.

LibWeb can now also generate bindings for keyboard events like the Pause
key, as well as other function keys (such as Right Alt, etc).

The logic for handling scan code sets is implemented by the PS2 keyboard
driver and is abstracted from the main HID KeyboardDevice code which
only handles "standard" KeyEvent(s).
2023-12-29 16:40:59 +01:00