Commit Graph

122 Commits

Author SHA1 Message Date
Liav A
8e55c4bfaf Kernel/PCI: Remove InterruptDisabler usage
Instead, just ensure we pick the m_access_lock and then m_scan_lock when
doing a scan/re-scan of the PCI configuration space so we know nobody
can actually access the PCI configuration space during the scan.

The m_scan_lock is now a Spinlock, to ensure we cannot yield to other
process while we do the PCI configuration space scanning.
2021-10-23 19:17:44 +02:00
Liav A
72e831e9e9 Kernel/PCI: Ensure m_domains is not empty if scanning with memory access 2021-10-23 19:17:44 +02:00
Liav A
40b1e6376b Kernel/PCI: Split Access::rescan_hardware method
To ensure clarity, this method is essentially splitted to two methods to
be called according to the access type being determined beforehand.
2021-10-23 19:17:44 +02:00
Liav A
d395ac8f59 Kernel/PCI: Rename Access::scan_pci_domains method
We rename it to scan_pci_domains_from_acpi_mcfg_table to ensure clarity,
because this method relies on the ACPI MCFG table to work.
2021-10-23 19:17:44 +02:00
Liav A
ca32ee5714 Kernel/PCI: Simplify detect_optimal_access_type function
Instead of getting the kernel commandline argument as function parameter
we just take internally in the function.
2021-10-23 19:17:44 +02:00
James Mintram
545ce5b595 Kernel: Add per platform Processor.h headers
The platform independent Processor.h file includes the shared processor
code and includes the specific platform header file.

All references to the Arch/x86/Processor.h file have been replaced with
a reference to Arch/Processor.h.
2021-10-14 01:23:08 +01:00
James Mintram
26d7c25076 Kernel: Add header includes closer to their use 2021-10-14 01:23:08 +01:00
Brian Gianforcaro
9d17070047 Kernel: Fix copy paste in VirtIO::RNG::class_name()
Ben noticed this copy paste error during code review.

Co-authored-by: Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
2021-10-03 13:36:10 +02:00
Brian Gianforcaro
3a945051fc Kernel: Use operator ""sv in all purpose() implementations
Previously there was a mix of returning plain strings and returning
explicit string views using `operator ""sv`. This change switches them
all to standardized on `operator ""sv` as it avoids a call to strlen.
2021-10-03 13:36:10 +02:00
Brian Gianforcaro
5f1c98e576 Kernel: Use operator ""sv in all class_name() implementations
Previously there was a mix of returning plain strings and returning
explicit string views using `operator ""sv`. This change switches them
all to standardized on `operator ""sv` as it avoids a call to strlen.
2021-10-03 13:36:10 +02:00
Liav A
4974727dbb Kernel: Move x86 IO instructions code into the x86 specific folder 2021-10-01 12:27:20 +02:00
Liav A
3e9dfd2e74 Kernel: Remove IO.h include from a bunch of places 2021-10-01 12:27:20 +02:00
Liav A
ef9b8ff0c7 Kernel/PCI: Remove all macros and replace them with enum classes 2021-09-29 11:24:33 +02:00
Liav A
9d9d57056e Kernel/PCI: Remove Address from enumeration callback
If we need that address, we can always get it from the DeviceIdentifier.
2021-09-29 11:24:33 +02:00
Liav A
a411a44fda Kernel/PCI: Cache interrupt line and interrupt pin of a device
This allows us to remove the PCI::get_interrupt_line API function. As a
result, this removes a bunch of not so great patterns that we used to
cache PCI interrupt line in many IRQHandler derived classes instead of
just using interrupt_number method of IRQHandler class.
2021-09-29 11:24:33 +02:00
Liav A
057f5a12c2 Kernel/PCI: Propagate usage of DeviceIdentifier everywhere
This allows us to remove a bunch of PCI API functions, and instead to
leverage the cached data from DeviceIdentifier object in many places.
2021-09-29 11:24:33 +02:00
Liav A
da327746a2 Kernel: Rename two PCI components
Rename ID => HardwareID, and PhysicalID => DeviceIdentifier.
This change merely does that to clarify what these objects really are.
2021-09-29 11:24:33 +02:00
Liav A
82bb08a15c Kernel/PCI: Cache more details about PCI devices when enumerating them
There's no good reason to fetch these values each time we need them.
2021-09-29 11:24:33 +02:00
Ben Wiederhake
de15a6a657 Kernel: Clean up PCI::Device namespace formatting 2021-09-20 16:00:43 +03:00
Idan Horowitz
574a1c522d Kernel: Store device class name as member of VirtIO devices
This ensures we dont try to hold the PCI Access mutex under IRQ when
printing VirtIO debug logs (which is not allowed and results in an
assertion). This is also relatively free, as it requires no allocations
(we're just storing a pointer to the rodata section).
2021-09-19 21:51:03 +02:00
Idan Horowitz
53cf28c47c Kernel: Use StringView literals in VirtIO::determine_device_class()
Since the return type is StringView we can just create them at compile
time and avoid the run-time construction.
2021-09-19 21:51:03 +02:00
Sahan Fernando
2e079c6d69 Kernel/VirtIO: Remove lazy allocation of VirtIO::Device BAR regions
This fixes a Kernel Panic where the lazy allocation triggers inside an
ISR and grabs a mutex, which isn't allowed when interrupts are
disabled. This also fixes a bug where the mapping for VirtIO device
BARs is never allocated. #9876
2021-09-19 11:46:09 +03:00
Liav A
9132596b8e Kernel: Move ACPI and BIOS code into the new Firmware directory
This will somwhat help unify them also under the same SysFS directory in
the commit.
Also, it feels much more like this change reflects the reality that both
ACPI and the BIOS are part of the firmware on x86 computers.
2021-09-12 11:52:16 +02:00
Liav A
c181494b19 Kernel/SysFS: Move the PCI bus directory to the /sys/bus directory
The USB bus directory is already in /sys/bus directory, so I don't see a
reason why the PCI bus directory shouldn't be in that directory too.
2021-09-12 01:44:19 +02:00
Ali Mohammad Pur
5a0cdb15b0 AK+Everywhere: Reduce the number of template parameters of IntrusiveList
This makes the user-facing type only take the node member pointer, and
lets the compiler figure out the other needed types from that.
2021-09-10 18:05:46 +03:00
Liav A
666c0c5a08 Kernel/VirtIO: Determine names without PCI access in IRQ context
This is a fix so the VirtIO code doesn't lead to assertion because we
try to determine the name based on the PCI values of the VirtIO device,
because trying to read from the PCI configuration space requires to
acquire a Mutex, which fails in an IRQ context.

To ensure we never encounter a situation when we call a pure virtual
function in an IRQ context, let's make class_name() method to be a
non-pure virtual function, so it can be still called at anytime.
2021-09-08 16:47:01 +02:00
Liav A
bde3c7301e Kernel/ACPI: Return Optional container after table search
This is a better pattern than returning a PhysicalAddress with a zero
value, so the code is more understandable now.
2021-09-08 10:27:02 +02:00
Liav A
009feefee0 Kernel/Devices: Ensure appropriate locking on the Device map singleton
Devices might be removed and inserted at anytime, so let's ensure we
always do these kind of operations with a good known state of the
HashMap.

The VirtIO code was modified to create devices outside the IRQ handler,
so now it works with the new locking of the devices singleton, but a
better approach might be needed later on.
2021-09-08 00:42:20 +02:00
Liav A
21b6d84ff0 Kernel/Devices: Remove required_mode and device_name methods
These methods are no longer needed because SystemServer is able to
populate the DevFS on its own.

Device absolute_path no longer assume a path to the /dev location,
because it really should not assume any path to a Device node.

Because StorageManagement still needs to know the storage name, we
declare a virtual method only for StorageDevices to override, but this
technique should really be removed later on.
2021-09-08 00:42:20 +02:00
Andreas Kling
300402cc14 Kernel: Make it possible for KBufferBuilder creation to fail
This patch adds KBufferBuilder::try_create() and treats it like anything
else that can fail. And so, failure to allocate the initial internal
buffer of the builder will now propagate an ENOMEM to the caller. :^)
2021-09-07 15:54:23 +02:00
Andreas Kling
250b52d6e5 Kernel: Make KBuffer::try_create_with_bytes() return KResultOr 2021-09-07 15:22:24 +02:00
Andreas Kling
4a9c18afb9 Kernel: Rename FileDescription => OpenFileDescription
Dr. POSIX really calls these "open file description", not just
"file description", so let's call them exactly that. :^)
2021-09-07 13:53:14 +02:00
Andreas Kling
6ad427993a Everywhere: Behaviour => Behavior 2021-09-07 13:53:14 +02:00
Andreas Kling
55b0b06897 Kernel: Store process names as KString 2021-09-07 13:53:14 +02:00
Andreas Kling
b481132418 Kernel: Make UserOrKernelBuffer return KResult from read/write/memset
This allows us to simplify a whole bunch of call sites with TRY(). :^)
2021-09-07 13:53:14 +02:00
Liav A
25ea7461a0 Kernel/PCI: Simplify the entire subsystem
A couple of things were changed:
1. Semantic changes - PCI segments are now called PCI domains, to better
match what they are really. It's also the name that Linux gave, and it
seems that Wikipedia also uses this name.
We also remove PCI::ChangeableAddress, because it was used in the past
but now it's no longer being used.
2. There are no WindowedMMIOAccess or MMIOAccess classes anymore, as
they made a bunch of unnecessary complexity. Instead, Windowed access is
removed entirely (this was tested, but never was benchmarked), so we are
left with IO access and memory access options. The memory access option
is essentially mapping the PCI bus (from the chosen PCI domain), to
virtual memory as-is. This means that unless needed, at any time, there
is only one PCI bus being mapped, and this is changed if access to
another PCI bus in the same PCI domain is needed. For now, we don't
support mapping of different PCI buses from different PCI domains at the
same time, because basically it's still a non-issue for most machines
out there.
2. OOM-safety is increased, especially when constructing the Access
object. It means that we pre-allocating any needed resources, and we try
to find PCI domains (if requested to initialize memory access) after we
attempt to construct the Access object, so it's possible to fail at this
point "gracefully".
3. All PCI API functions are now separated into a different header file,
which means only "clients" of the PCI subsystem API will need to include
that header file.
4. Functional changes - we only allow now to enumerate the bus after
a hardware scan. This means that the old method "enumerate_hardware"
is removed, so, when initializing an Access object, the initializing
function must call rescan on it to force it to find devices. This makes
it possible to fail rescan, and also to defer it after construction from
both OOM-safety terms and hotplug capabilities.
2021-09-07 13:47:37 +02:00
Andreas Kling
9db8a14264 Kernel: Make SysFS and ProcFS generator functions return KResult
This allows us to propagate a whole bunch of KBufferBuilder errors.
2021-09-06 18:56:51 +02:00
Andreas Kling
f173f73f10 Kernel/USB: Use TRY() and adopt_nonnull_own_or_enomem() some more 2021-09-06 13:06:05 +02:00
Andreas Kling
75564b4a5f Kernel: Make kernel region allocators return KResultOr<NOP<Region>>
This expands the reach of error propagation greatly throughout the
kernel. Sadly, it also exposes the fact that we're allocating (and
doing other fallible things) in constructors all over the place.

This patch doesn't attempt to address that of course. That's work for
our future selves.
2021-09-06 01:55:27 +02:00
Andreas Kling
cb71a73708 Kernel/USB: Tidy up UHCIDescriptorPool construction
- Use KResultOr<NonnullRefPtr<UHCIDescriptorPool<T>>
- Make the constructor private
- Use TRY() at call sites
2021-09-06 01:55:27 +02:00
Andreas Kling
79fbad6df9 Kernel/USB: Tidy up USB::Transfer construction 2021-09-06 01:55:27 +02:00
Andreas Kling
4d961387c1 Kernel: Use TRY() some more in USB::Hub 2021-09-06 01:55:27 +02:00
Brian Gianforcaro
bb58a4d943 Kernel: Make all Spinlocks use u8 for storage, remove template
The default template argument is only used in one place, and it
looks like it was probably just an oversight. The rest of the Kernel
code all uses u8 as the type. So lets make that the default and remove
the unused template argument, as there doesn't seem to be a reason to
allow the size to be customizable.
2021-09-05 20:46:02 +02:00
Brian Gianforcaro
472454cded Kernel: Switch static_asserts of a type size to AK::AssertSize
This will provide better debug ability when the size comparison fails.
2021-09-05 20:08:57 +02:00
Andreas Kling
29a9f80ecf Kernel/USB: Use TRY() in the various USB classes 2021-09-05 16:25:40 +02:00
sin-ack
566c5d1e99 AK+Kernel: Move KResult.h to Kernel/API for userspace access
This commit moves the KResult and KResultOr objects to Kernel/API to
signify that they may now be freely used by userspace code at points
where a syscall-related error result is to be expected. It also exposes
KResult and KResultOr to the global namespace to make it nicer to use
for userspace code.
2021-09-05 12:54:48 +02:00
Liav A
e606ff3751 Kernel/VirtIO: Determine VirtIO device class also with the PCI device ID
According to the VirtIO 1.0 specification:
"Non-transitional devices SHOULD have a PCI Device ID in the range
0x1040 to 0x107f. Non-transitional devices SHOULD have a PCI Revision ID
of 1 or higher. Non-transitional devices SHOULD have a PCI Subsystem
Device ID of 0x40 or higher."

It also says that:
"Transitional devices MUST have a PCI Revision ID of 0. Transitional
devices MUST have the PCI Subsystem Device ID matching the Virtio
Device ID, as indicated in section 5. Transitional devices MUST have the
Transitional PCI Device ID in the range 0x1000 to 0x103f."

So, for legacy devices, we know that revision ID in the PCI header won't
be 1, so we probe for PCI_SUBSYSTEM_ID value.
Instead of using the subsystem device ID, we can probe the DEVICE_ID
value directly in case it's not a legacy device.
This should cover all possibilities for identifying VirtIO devices, both
per the specification of 0.9.5, and future revisions from 1.0 onwards.
2021-09-04 20:53:26 +02:00
Liav A
ed6c1f53af Kernel/VirtIO: Defer initialization of device out of the constructor
This ensures we safely handle interrupts (which can call virtual
functions), so they don't happen in the constructor - this pattern can
lead to a crash, if we are still in the constructor context because
not all methods are available for usage (some are pure virtual,
so it's possible to call __cxa_pure_virtual).

Also, under some conditions like adding a PCI device via PCI-passthrough
mechanism in QEMU, it became exposed to the eye that the code asserts on
RNG::handle_device_config_change(). That device has no configuration but
if the hypervisor still misbehaves and tries to configure it, we should
simply return false to indicate nothing happened.
2021-09-04 16:36:02 +02:00
Liav A
e490c17bde Kernel/SysFS: Ensure data stability when reading from Inodes
Like with the ProcFS, description data can change at anytime, so it's
wise to ensure that when the userland reads from an Inode, data is
consistent unless the userland indicated it wants to refresh the data
(by seeking to offset 0, or re-attaching the Inode).
Otherwise, if the data changes in the middle of the reading, it can
cause silent corruption in output which can lead to random crashes.
2021-09-04 16:33:56 +02:00
Andreas Kling
b02eb8224c Kernel/VirtIO: Add two missing error checks in VirtIO::ConsolePort 2021-09-04 13:08:37 +02:00