Commit graph

8778 commits

Author SHA1 Message Date
Sönke Holz
1d83f73ec5 Kernel/PCI: Stop using a dangling pointer in dmesgln_pci
VariadicFormatParams only stores pointers to the parameters, so
the device.device_name() parameter will dangle.

This fixes broken dmesgln_pci output on riscv64 GCC.
2024-09-07 12:52:52 -04:00
Sönke Holz
b628ab0ae3 Kernel/riscv64: Add comment about the bss to the linker script 2024-09-06 16:02:08 -04:00
Sönke Holz
ed3fa378c5 Kernel/riscv64: Add FLAGS to the PHDRS linker script command
Otherwise validate_program_headers will complain about the kernel text
segment being executable and writable.
2024-09-06 16:02:08 -04:00
Liav A.
0482f4e117 Kernel: Remove passing of register state to IRQ handlers
Linux did the same thing 18 years ago and their reasons for the change
are similar to ours - https://github.com/torvalds/linux/commit/7d12e78

Most interrupt handlers (i.e. IRQ handlers) never used the register
state reference anywhere so there's simply no need of passing it around.
I didn't measure the performance boost but surely this change can't make
things worse anyway.
2024-09-01 21:00:18 +02:00
Liav A.
fdf3e0aca1 Kernel: Don't assume sizes of needed buffers early in the execve syscall
Instead, start by trying to read a buffer with size of Elf_Ehdr, and
check it for the shebang sign. If it's indeed an executable with shebang
then read again from the file, now with PAGE_SIZE size, which should
suffice for finding the interpreter path.

However, if the executable is an ELF, we quickly validate it and then
pass the preliminary buffer to the find_elf_interpreter_for_executable
method.

That method calculates the last byte offset which is needed to read all
of the program headers, so we don't just assume 4096 bytes is sufficient
anymore. The same pattern is applied when loading the interpreter ELF
main header and its program headers.
2024-09-01 20:52:55 +02:00
brody-qq
44a2157e2e Kernel/Ext2FS: Reduce allocations in compute_block_list_impl()
In Ext2FSInode::compute_block_list_impl(), each call to
process_block_array() creates a new ByteBuffer, which leads to a
kmalloc() call. The ByteBuffer is then discarded when
process_block_array() exits, leading to a kfree() call.

This leads to repeated kmalloc() and kfree() calls as ByteBuffers are
created and destroyed each time process_block_array() is called.

This commit makes it so that only 1 ByteBuffer is created for each level
of inode indirect block (so only 3 ByteBuffers are created at most).
These ByteBuffers are re-used on each call to process_block_array().

This reduces the number of kmalloc() and kfree() calls during
compute_block_list_impl(), especially for larger files.
2024-08-30 22:22:13 +02:00
Sönke Holz
75ccee81be Kernel: Use the correct ip in the dispatch_signal debug output
The signal handler RegisterState is in 'regs', not 'm_regs'.
2024-08-21 08:17:17 -04:00
Sönke Holz
7b0553eb13 Kernel/x86: Add missing rcx + r11 clobber to Syscall::invoke with 4 args
The 4-arg version was missing those.
2024-08-21 08:17:17 -04:00
Sönke Holz
d9c098f103 Kernel/x86: Use iretq instead of sysretq for sys$sigreturn
Using sysretq clobbers rcx and r11 as this instruction loads the rip and
rflags from those registers. This is fine for normal syscalls.

Signal dispatching works like this:
The kernel makes userspace jump to the signal trampoline when a signal
is dispatched. That trampoline then executes the sigreturn syscall after
calling the signal handler to continue executing the code before the
signal was dispatched.

Since e71c320154 the sigreturn syscall is done via the syscall
instruction (and int 0x82 support was removed in the next commit),
which causes the kernel to currently use sysretq to return to userspace.
But signals can happen at any time, not just during syscalls, so the
sigreturn syscall shouldn't clobber the contents of those registers when
returning to userspace.
2024-08-21 08:17:17 -04:00
Hendiadyoin1
f5d2671718 Kernel/USBMS: Query and use the Block Limits VPD page
This allows us to properly limit our block requests to the device's
capabilities, and choose more optimal block counts for I/O operations.
In theory, as Qemu  only advertises a block limit above our current
internal block size limit of u16::max and does not advertise any optimal
transfer lengths.
2024-08-20 22:06:51 -04:00
Hendiadyoin1
0d2e8ec96f Kernel/USBMS: Inquire and wait for the storage device to become ready
This is apparently what bootloaders do before using a USB storage device
so we should likely do so as well, especially when no BIOS is present,
like on riscv.

Co-Authored-By: Sönke Holz <sholz8530@gmail.com>
2024-08-20 22:06:51 -04:00
Hendiadyoin1
e3567969d4 Kernel/USBMS: Add and use a send_scsi_command helper
This makes sending commands less repetitive
2024-08-20 22:06:51 -04:00
Hendiadyoin1
0866adde12 Kernel/USB: Be a bit more const-correct with USB transfers 2024-08-20 22:06:51 -04:00
Sönke Holz
13e789a261 Kernel/riscv64: Verify the FDT magic before copying it 2024-08-20 21:52:12 -04:00
Hendiadyoin1
f74f80e13b Kernel/aarch64: Use the FDT to get the device/boot info
This removes the old hacky multiboot memory map and instead uses the
FDT to get the actual memory map.
2024-08-20 21:52:12 -04:00
Hendiadyoin1
55a22ad393 Kernel/aarch64: Save the device tree 2024-08-20 21:52:12 -04:00
Hendiadyoin1
e0a177061e Kernel/Firmware+riscv64: Move devicetree handling to Firmware directory
This also adds a `verify_fdt` method which will be used in later commits
2024-08-20 21:52:12 -04:00
Sönke Holz
1ffb7afba7 Kernel/aarch64: Make the kernel bootable from any physical load address
Instead calculate the load offset at runtime.

The mapping of the initial stack is now done explicitly. It was
previously included in the 2 MiB-aligned kernel range.

We need to use kernel8.img now, as we no longer hard-code the physical
addresses of all sections in the linker script. QEMU would otherwise
try to load us at KERNEL_MAPPING_BASE.
2024-08-20 21:52:12 -04:00
Sönke Holz
ba27989da1 Kernel/aarch64: Use MM's virtual_to_low_physical for Mailbox::send_queue
This is slightly better than assuming that cutting off the high 32 bits
results in the physical address. This worked for now because the kernel
(and stack) is mapped at KERNEL_MAPPING_BASE + PHYSICAL_LOAD_ADDR and
KERNEL_MAPPING_BASE & 0xffff'ffff == 0.

The next commit will move the kernel to KERNEL_MAPPINGS_BASE + 0, so
we need to get the physical address in a slightly less hacky way.
2024-08-20 21:52:12 -04:00
Sönke Holz
194d9df34f Kernel/Memory: Handle devicetree memory nodes with more than one region 2024-08-20 21:52:12 -04:00
Sönke Holz
ab44530304 Kernel/Memory: Remove x86 LAPIC address hack
MM is now able to handle MMIO after our "highest_phyisical_address".
2024-08-20 21:52:12 -04:00
Sönke Holz
810f2e7f29 Kernel/aarch64: Include .bss* sections in the linker script 2024-08-20 21:52:12 -04:00
Sönke Holz
e7b8eed005 Kernel: Share Processor::capture_stack_trace() between architectures
This makes Processor::capture_stack_trace() work on all our
architectures. For this function to work on AArch64 and RISC-V, the
frame pointer has to be saved during context switches.

AArch64 and RISC-V don't support SMP yet, so the code for getting a
backtrace for processes running on other cores is guarded behind a
'#if ARCH(X86_64)'.
2024-08-16 16:01:19 -04:00
Sönke Holz
fa39086922 Kernel: Add ThreadRegisters::frame_pointer() accessors 2024-08-16 16:01:19 -04:00
Sönke Holz
c1dc55ec7c Kernel: Remove Processor::is_kernel_mode
This function was only used to verify that we are running in kernel
mode. But it is pretty much impossible that we will ever end up in
kernel code and actually are able to execute it in user mode. A lot of
stuff must go completely wrong to end up in such a situation.

Getting the current privilege level is also impossible on RISC-V by
design.
2024-08-16 15:58:27 -04:00
Sönke Holz
c3c5378c82 Kernel/USB: Always check for the Intel xHCI port routing quirk
The quirk previously only worked when USBLEGSUP was present and the BIOS
owned the host controller.
2024-08-12 10:54:00 -04:00
brody-qq
a0b021cbcf Kernel/Memory: Fix crash on writes to shared file mmaps
Writes to SharedInodeVMObjects could cause a Protection Violation if a
page was marked as dirty by a different process.

This happened due to a combination of 2 things:
* handle_dirty_on_write_fault() was skipped if a page was already marked
  as dirty
* when a page was marked as dirty, only the Region that caused the page
  fault was remapped

This commit:
* fixes the crash by making handle_fault() stop checking if a page was
  marked dirty before running handle_dirty_on_write_fault()
* modifies handle_dirty_on_write_fault() so that it always marks the
  page as dirty and remaps the page (this avoids a 2nd bug that was
  never hit due to the 1st bug)
2024-08-10 16:19:12 +02:00
Liav A.
79d9abd3cc Kernel: Create coredump by using the crashed process VFS root context
Resolve a regression caused by 01e1af732b.

This unbreaks coredump generation, because we need to use the VFS root
context of the crashed process and not of the FinalizerTask, as it will
hold an empty VFS root context that is assigned to kernel processes.
2024-08-10 10:14:37 -04:00
Sönke Holz
6a195b6616 Kernel/USB: Add support for xHCI bulk transfers
Bulk transfers also use Normal TRBs, so move the reusable normal TRB
setup code from submit_async_interrupt_transfer into a new function
prepare_normal_transfer.

submit_bulk_transfer and submit_async_interrupt_transfer use this
function and then either block on the completion or submit it
asynchronously and wrap it into a PeriodicPendingTransfer.
2024-08-07 18:30:06 -04:00
Sönke Holz
d82148aae1 Kernel/USB: Don't truncate the max packet size in the MassStorage driver 2024-08-07 18:30:06 -04:00
Liav A.
cb10f70394 Kernel: Change internal handling of filesystem-specific options
Instead of using a raw `KBuffer` and letting each implementation to
populating the specific flags on its own, we change things so we only
let each FileSystem implementation to validate the flag and its value
but then store it in a HashMap which its key is the flag name and
the value is a special new class called `FileSystemSpecificOption`
which wraps around `AK::Variant<...>`.

This approach has multiple advantages over the previous:
- It allows runtime inspection of what the user has set on a `MountFile`
  description for a specific filesystem.
- It ensures accidental overriding of filesystem specific option that
  was already set is not possible
- It removes ugly casting of a `KBuffer` contents to a strongly-typed
  values. Instead, a strongly-typed `AK::Variant` is used which ensures
  we always get a value without doing any casting.

Please note that we have removed support for ASCII string-oriented flags
as there were no actual use cases, and supporting such type would make
`FileSystemSpecificOption` more complicated unnecessarily for now.
2024-08-03 20:35:06 +02:00
Sönke Holz
ad64243bb9 Kernel/x86: Properly restore rflags in the syscall handler
The sysret instruction restores the rflags value from the r11 register.
Before, we expected that the value in RegisterState::r11 is still the
rflags value saved by syscall and therefore didn't copy
RegisterState::rflags to r11 before the sysret.

But signal handlers and ptrace can change the value in
RegisterState::r11 while we are handling a syscall, so we shouldn't
assume that it still contains the saved rflags.
While handling a syscall the contents of RegisterState::rflags may also
have been updated by e.g. ptrace in which case we should restore the
updated rflags, not the original state on syscall entry.
2024-08-01 22:08:08 +02:00
Sönke Holz
640530dbed Kernel/PCI: Handle 64-bit MSI-X BARs
PCI::get_BAR just returns the raw BAR value and doesn't handle 64-bit
BARs.
2024-08-01 12:05:56 -07:00
Sönke Holz
26555baf53 Kernel/USB: Make USBHubDescriptor::hub_characteristics union packed
AArch64 Clang otherwise complains that this anonymous union is more
aligned than the struct.
2024-07-30 09:07:32 -04:00
Logkos
ad73adef5d Kernel/Net: Rename IPv4-specific files and structs 2024-07-29 09:49:56 -04:00
Sönke Holz
137cb0029b Kernel/USB: Use a Stream to parse USB descriptors
This fixes incorrect assumptions about the layout of descriptors and
gets rid of all the pointer arithmetic.
The USB spec doesn't define a strict order for all descriptors to appear
in.
It just says that endpoint descriptors follow its interface descriptor.
We also have to skip all unknown descriptors (which also can appear
anywhere), not just HID descriptors.
2024-07-27 13:28:15 -04:00
Sönke Holz
594664d9b5 Kernel/USB: Add AssertSizes to USB descriptor definitions 2024-07-27 13:28:15 -04:00
Sönke Holz
8799524b00 Kernel/USB: Don't copy the USBConfiguration during device initialization 2024-07-27 09:40:09 -04:00
Sönke Holz
d8cfe3b4a8 Kernel/USB: Pass the configuration value in the GET_DESCRIPTOR request
The low byte of wValue is the configuration value for
GetDescriptor(Configuration) requests.
2024-07-27 09:40:09 -04:00
Sönke Holz
a5b33734f8 Kernel/USB: Explicitly request the boot protocol in the USB mouse driver
By default the report protocol is used.
2024-07-27 09:40:09 -04:00
Sönke Holz
4f573ebdc3 Kernel/USB: Add spec references to HID/Codes.h 2024-07-27 09:40:09 -04:00
Sönke Holz
345117e404 Kernel/USB: Handle USB mice with more than one interface
This simply looks for the first interface with the appropriate class,
subclass and protocol which we can initialize now.
2024-07-27 09:40:09 -04:00
Sönke Holz
ed73aa3364 Kernel/USB: Don't prepend "0x" to the logged VID/PID in the mouse driver
This just looks silly and USB VID/PIDs are typically formatted without
the prefix.
2024-07-27 09:40:09 -04:00
Sönke Holz
64f827dbc8 Kernel/USB: Correct the xHCI version check
0x102 would be 1.0.2, not 1.2.0.
2024-07-27 09:40:09 -04:00
brody-qq
f3f681ae45 Kernel/Ext2FS: Replace hardcoded offset with a constexpr
The superblock of an ext2 filesystem is always found on the storage
device at offset 1024. This 1024 number was hardcoded in the Ext2FS
code.

This commit:
* adds a constexpr to replace the hardcoded 1024 values
* removes a comment about one of the the hardcoded 1024 values which is
  now umnecessary
2024-07-27 07:50:07 -04:00
brody-qq
416875f952 Kernel/FileSystem: Change const placement to match style guide
This moves 3 const keywords in OpenFileDescription::tty() to make them
consistent with the 'const placement' rule in the style guide.
2024-07-27 07:50:07 -04:00
Sönke Holz
c04f240398 Kernel: Set the configuration during USB Hub initialization
All USB Devices (including hubs) need to have a configuration set before
you can use them. We already do this in other USB drivers, but forgot to
do it for USB hubs as well.
2024-07-26 14:25:49 -04:00
Idan Horowitz
3dec9aa54e Kernel: Add a minimal xHCI driver implementation
This adds a minimal (that is, just enough to make USB mouse/keyboard
work) implementation of an xHCI driver, to let us use serenity on
modern baremetal machines.
2024-07-26 14:25:49 -04:00
Idan Horowitz
ccd614212f Kernel: Correct pointer math in USBConfiguration::enumerate_interfaces
Both the calculation for the interface descriptor address and the
endpoint descriptors addresses (for second interface and above) were
incorrect, and would read the wrong data (and go out-of-bounds as well)
2024-07-26 14:25:49 -04:00
Idan Horowitz
c35446b232 Kernel: Give USB::Pipe a reference to its owning USB::Device
Previously USB::Pipe would just try to poorly maintain copies of some
of the relevant properties of USB::Device. (address & speed)
Now it just holds a reference to it's owning device and can query them
when needed.
2024-07-26 14:25:49 -04:00