Commit graph

8750 commits

Author SHA1 Message Date
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
Idan Horowitz
c5956663f7 Kernel: Move USB device initialization to controllers
The current USB::Device::enumerate_device() implementation is UHCI
specific, and is not relevant for xHCI controllers.

Move it to a USBController virtual method to allow different
implementations for the other controller types.
2024-07-26 14:25:49 -04:00
Idan Horowitz
1e2919b5c1 Kernel: Add create_kernel_thread overload that accepts a lambda
Also fixes the entry function argument initialization to be
architecture-independent.
2024-07-26 14:25:49 -04:00
Sönke Holz
0f18fb6c03 Kernel/x86: Stop the APIC timer before configuring it
This avoids spurious interrupts during APIC timer calibration, as the
timer might otherwise immediately generate an interrupt when enabling
interrupts if the initial count was at a low enough value.
2024-07-24 08:08:47 -04:00
Sönke Holz
87f194b3f5 Kernel: Don't truncate the pc value in the scheduler state dump
This is likely a leftover from the i686-only days.
Also rename the get_eip function to get_pc to be more arch-agnostic.
2024-07-23 09:03:31 -04:00
Liav A.
4aec3f4ef9 Kernel+Userland: Simplify loading of an ELF interpreter path
The LibELF validate_program_headers method tried to do too many things
at once, and as a result, we had an awkward return type from it.

To be able to simplify it, we no longer allow passing a StringBuilder*
but instead we require to pass an Optional<Elf_Phdr> by reference so
it could be filled with actual ELF program header that corresponds to
an INTERP header if such found.

As a result, we ensure that only certain implementations that actually
care about the ELF interpreter path will actually try to load it on
their own and if they fail, they can have better diagnostics for an
invalid INTERP header.

This change also fixes a bug that on which we failed to execute an ELF
program if the INTERP header is located outside the first 4KiB page of
the ELF file, as the kernel previously didn't have support for looking
beyond that for that header.
2024-07-21 15:38:52 +02:00
Liav A.
c0f55d4b11 Kernel: Add a check on ELF interpreter to verify we open a regular file
While extremely unlikely, it's possible to change the dynamic loader
to a non regular file, which will result in a kernel panic upon VERIFY
of the `interpreter_description->inode()` statement.
2024-07-21 15:38:52 +02:00
Liav A.
98160e33c4 Kernel: Add a conversion method from KBuffer to UserOrKernelBuffer 2024-07-21 15:38:52 +02:00
Liav A.
03ae9fdb0a Kernel: Check condition earlier for ELF file type
It makes no sense to do all of the loading work just to figure out that
the ELF file is an object file that is a result of compiling and not
an actual executable.

In addition to that, we should disallow running coredumps as well, so
the condition is changed now to only allow ET_DYN or ET_EXEC ELF files.
2024-07-21 15:38:52 +02:00
Liav A.
3699c2e8ac Kernel/GPU: Remove legacy VGA text mode support
Plain old VGA text mode functionality was introduced in 1987, and is
obviously still used on some (even modern) x86 machines.

However, it's very limited in what it gives to us, because by using a
80x25 text mode console, it's guaranteed that no desktop functionality
is available during such OS runtime session.

It's also quite complicated to handle access arbitration on the VGA ISA
ports which means that only one VGA card can work in VGA mode, which
makes it very cumbersome to manage multiple cards at once.

Since we never relied on the VGA text mode console for anything serious,
as booting on a QEMU machine always gives a proper framebuffer to work
with, VGA text mode console was used in bare metal sessions due to lack
of drivers.
However, since we "force" multiboot-compatible bootloaders to provide us
a framebuffer, it's basically a non-issue to have a functional console
on bare metal machines even if we don't have the required drivers.
2024-07-21 12:21:02 +02:00
Liav A.
efb8063c76 Kernel: Add the BootDummyConsole class
This class will be used in a situation where we simply don't have a
working framebuffer console, but we still want to boot (without having a
screen being attached, or an actual proper GPU driver on bare metal, for
example).
2024-07-21 12:21:02 +02:00
Liav A.
0e6624dc86 Kernel: Introduce the unshare syscall family
These 2 syscalls are responsible for unsharing resources in the system,
such as hostname, VFS root contexts and process lists.

Together with an appropriate userspace implementation, these syscalls
could be used for creating a sandbox environment (containers) for user
programs.
2024-07-21 11:44:23 +02:00
Liav A.
e52abd4c09 Kernel: Introduce the HostnameContext class
Similarly to VFSRootContext and ScopedProcessList, this class intends
to form resource isolation as well.
We add this class as an infrastructure preparation of hostname contexts
which should allow processes to obtain different hostnames on the same
machine.
2024-07-21 11:44:23 +02:00
Liav A.
3692af528e Kernel: Move most of VirtualFileSystem code to be in a namespace
There's no point in constructing an object just for the sake of keeping
a state that can be touched by anything in the kernel code.

Let's reduce everything to be in a C++ namespace called with the
previous name "VirtualFileSystem" and keep a smaller textual-footprint
struct called "VirtualFileSystemDetails".

This change also cleans up old "friend class" statements that were no
longer needed, and move methods from the VirtualFileSystem code to more
appropriate places as well.
Please note that the method of locking all filesystems during shutdown
is removed, as in that place there's no meaning to actually locking all
filesystems because of running in kernel mode entirely.
2024-07-21 11:44:23 +02:00
Liav A.
4370bbb3ad Kernel+Userland: Introduce the copy_mount syscall
This new syscall will be used by the upcoming runc (run-container)
utility.

In addition to that, this syscall allows userspace to neatly copy RAMFS
instances to other places, which was not possible in the past.
2024-07-21 11:44:23 +02:00
Liav A.
dd59fe35c7 Kernel+Userland: Reduce jails to be a simple boolean flag
The whole concept of Jails was far more complicated than I actually want
it to be, so let's reduce the complexity of how it works from now on.
Please note that we always leaked the attach count of a Jail object in
the fork syscall if it failed midway.
Instead, we should have attach to the jail just before registering the
new Process, so we don't need to worry about unsuccessful Process
creation.

The reduction of complexity in regard to jails means that instead of
relying on jails to provide PID isolation, we could simplify the whole
idea of them to be a simple SetOnce, and let the ProcessList (now called
ScopedProcessList) to be responsible for this type of isolation.

Therefore, we apply the following changes to do so:
- We make the Jail concept no longer a class of its own. Instead, we
  simplify the idea of being jailed to a simple ProtectedValues boolean
  flag. This means that we no longer check of matching jail pointers
  anywhere in the Kernel code.
  To set a process as jailed, a new prctl option was added to set a
  Kernel SetOnce boolean flag (so it cannot change ever again).
- We provide Process & Thread methods to iterate over process lists.
  A process can either iterate on the global process list, or if it's
  attached to a scoped process list, then only over that list.
  This essentially replaces the need of checking the Jail pointer of a
  process when iterating over process lists.
2024-07-21 11:44:23 +02:00
Liav A.
91c87c5b77 Kernel+Userland: Prepare for considering VFSRootContext when mounting
Expose some initial interfaces in the mount-related syscalls to select
the desired VFSRootContext, by specifying the VFSRootContext index
number.

For now there's still no way to create a different VFSRootContext, so
the only valid IDs are -1 (for currently attached VFSRootContext) or 1
for the first userspace VFSRootContext.
2024-07-21 11:44:23 +02:00
Liav A.
01e1af732b Kernel/FileSystem: Introduce the VFSRootContext class
The VFSRootContext class, as its name suggests, holds a context for a
root directory with its mount table and the root custody/inode in the
same class.

The idea is derived from the Linux mount namespace mechanism.
It mimicks the concept of the ProcessList object, but it is adjusted for
a root directory tree context.
In contrast to the ProcessList concept, processes that share the default
VFSRootContext can't see other VFSRootContext related properties such as
as the mount table and root custody/inode.

To accommodate to this change progressively, we internally create 2 main
VFS root contexts for now - one for kernel processes (as they don't need
to care about VFS root contexts for the most part), and another for all
userspace programs.
This separation allows us to continue pretending for userspace that
everything is "normal" as it is used to be, until we introduce proper
interfaces in the mount-related syscalls as well as in the SysFS.

We make VFSRootContext objects being listed, as another preparation
before we could expose interfaces to userspace.
As a result, the PowerStateSwitchTask now iterates on all contexts
and tear them down one by one.
2024-07-21 11:44:23 +02:00
brody-qq
6340658005 Kernel/FileSystem: Clean up duplicated if statements
This commit merges 2 if statements that check the same condition in
OpenFileDescription::~OpenFileDescription().
2024-07-15 20:26:45 -04:00
brody-qq
2a164dc923 Kernel/Memory: Fix overcommit when cloning anonymous mmap objects
AnonymousVMObject::try_clone() computed how many shared cow pages to
commit by counting all VMObject pages that were not shared_zero_pages.

This means that lazy_committed_pages were also being included in the
count. This is a problem because the page fault handling code for
lazy_committed_pages does not allocate from
m_shared_committed_cow_pages. So more pages than necessary were being
committed.

This fixes this overcommitting problem by skipping lazy_committed_pages
when counting how many pages to commit.
2024-07-12 08:52:06 -04:00
brody-qq
faa6395a11 Kernel/Memory: Add more efficient method for remapping single page
This commit introduces VMObject::remap_regions_single_page(). This
method remaps a single page in all regions associated with a VMObject.
This is intended to be a more efficient replacement for remap_regions()
in cases where only a single page needs to be remapped.

This commit also updates the cow page fault handling code to use this
new method.
2024-07-12 08:52:06 -04:00
brody-qq
e14f954988 Kernel/Memory: Fix shared anonymous mmap changes not being shared
Writes to a MAP_SHARED | MAP_ANONYMOUS mmap region were not visible to
other processes sharing the mmap region. This was happening because the
page fault handler was not remapping the VMObject's m_regions after
allocating a new page.

This commit fixes the problem by calling remap_regions() after assigning
a new page to the VMObject in the page fault handler. This remapping
only occurs for shared Regions.
2024-07-12 08:52:06 -04:00
brody-qq
781ded408b Kernel/Memory: Small refactor of handle_zero_fault()
This commit makes the following minor changes to handle_zero_fault():
* cleans up a call to static_cast(), replacing it with a reference (a
  future commit will also use this reference).
* replaces a call to vmobject() with the new reference mentioned above.
* moves the definition of already_handled to inside the block where
  already_handled is used.
2024-07-12 08:52:06 -04:00
brody-qq
8812410617 Kernel/Memory: Fix redundant page faults on anonymous mmaps after fork
After a fork(), page faults on anonymous mmaps can cause a redundant
page fault to occur.

This happens because VMObjects for anonymous mmaps are initially filled
with references to the lazy_committed_page or shared_zero_page. If there
is a fork, VMObject::try_clone() is called and all pages of the VMObject
are marked as cow (via the m_cow_map).

Page faults on a zero/lazy page are handled by handle_zero_fault().
handle_zero_fault() does not update m_cow_map, so if the page was marked
cow before the fault, it will still be marked cow after the fault. This
causes a second (redundant) page fault when the CPU retries the write.

This commit removes the redundant page fault by not marking zero/lazy
pages as cow in m_cow_map.
2024-07-12 08:52:06 -04:00
brody-qq
2278b17c42 Kernel/Memory: Remove cow map updates from try_allocate_split_region()
AddressSpace::try_allocate_split_region() was updating the cow map of
new_region based on the cow map of source_region.

The problem is that both new_region and source_region reference the
same vmobject and the same cow map, so these cow map updates didn't
actually change anything.

This commit:
* removes the cow map updates from try_allocate_split_region()
* removes Region::set_should_cow() since it is no longer used
2024-07-12 08:52:06 -04:00
brody-qq
3e9b269bcd Kernel/Memory: Make mmap objects track dirty pages
InodeVMObjects now track dirty and clean pages. This tracking of
dirty and clean pages is used by the msync and purge syscalls.

dirty page tracking works using the following rules:
* when a new InodeVMObject is made, all pages are marked clean.
* writes to clean InodeVMObject pages will cause a page fault,
  the fault handler will mark the page as dirty.
* writes to dirty InodeVMObject pages do not cause page faults.
* if msync is called, only dirty pages are flushed to storage (and
  marked clean).
* if purge syscall is called, only clean pages are discarded.
2024-07-07 18:25:32 +02:00
Liav A.
e89726562b Kernel: Remove the AllMiceDevice class
This device was a short-lived solution to allow userspace (WindowServer)
to easily support hotplugging mouse devices with presumably very small
modifications on userspace side.

Now that we have a proper mechanism to propagate hotplug events from the
DeviceMapper program to any program that needs to get such events, we no
longer need this device, so let's remove it.
2024-07-06 21:42:32 +02:00
Liav A.
a1a2470c1e Documentation: Add a small guide on how to allocate new major numbers
Contributors that want to add new types of character or block devices
must follow these guidelines in order to keep everything intact.
2024-07-06 21:42:32 +02:00
Liav A.
24c1e93afb Kernel/SysFS: Expose device major number allocations to userspace
After the previous commit, we are able to create a comprehensive list of
all devices' major number allocations.

To help userspace to distinguish between character and block devices, we
expose 2 sysfs nodes so userspace can decide which list it needs to open
in order to iterate on it.
2024-07-06 21:42:32 +02:00