Commit Graph

457 Commits

Author SHA1 Message Date
James Mintram
dfe4810c3a Kernel: Add cross platform RegisterState header and Aarch64 version
A new RegisterState header includes the platform specific RegisterState
header based on the platform being compiled.

The Aarch64 RegisterState header contains stubs for Debug
2021-10-15 21:48:45 +01: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
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
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
Przemysław R. Kusiak
c6e23e45c5 Kernel: Let MouseDevice and KeyboardDevice write method return EINVAL
Currently, writing anything to `/dev/mouse0` or `/dev/keyboard0` causes
the Kernel to panic. The reason for this is that
`[Mouse,Keyboard]Device::write` always returns 0, which is explicitly
prohibited by `VERIFY` macro in `Process::sys$write`.  The fix seems
trivial; `write` should return EINVAL instead (as is the case with, for
example, `KCOVDevice`).
2021-09-18 22:57:42 +03:00
Liav A
44f5f72add Kernel/Devices: Use try_create_device helper for SB16 2021-09-17 01:02:48 +03:00
Liav A
fd4397a430 Kernel/Devices: Use try_create_device helper for ConsoleDevice 2021-09-17 01:02:48 +03:00
Liav A
5e8dcb9ca7 Kernel/Devices: Move ConsoleDevice into the Devices source directory 2021-09-17 01:02:48 +03:00
Liav A
aee4786d8e Kernel: Introduce the DeviceManagement singleton
This singleton simplifies many aspects that we struggled with before:
1. There's no need to make derived classes of Device expose the
constructor as public anymore. The singleton is a friend of them, so he
can call the constructor. This solves the issue with try_create_device
helper neatly, hopefully for good.
2. Getting a reference of the NullDevice is now being done from this
singleton, which means that NullDevice no longer needs to use its own
singleton, and we can apply the try_create_device helper on it too :)
3. We can now defer registration completely after the Device constructor
which means the Device constructor is merely assigning the major and
minor numbers of the Device, and the try_create_device helper ensures it
calls the after_inserting method immediately after construction. This
creates a great opportunity to make registration more OOM-safe.
2021-09-17 01:02:48 +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
Idan Horowitz
92a3318375 Kernel: Run clang-format on SerialDevice.h 2021-09-11 17:20:19 +03:00
Liav A
7dfecbee44 Kernel: Initialize and expose SerialDevice(s) properly
I forgot that we need to also initialize SerialDevice and also to ensure
it creates a sysfs node properly. Although I had a better fix for this,
it keeps the CI happy, so for now it's more than enough :)
2021-09-11 17:07:38 +03:00
Liav A
f5de4f24b2 Kernel/Devices: Defer creation of SysFS component after the constructor
Instead of doing so in the constructor, let's do immediately after the
constructor, so we can safely pass a reference of a Device, so the
SysFSDeviceComponent constructor can use that object to identify whether
it's a block device or a character device.
This allows to us to not hold a device in SysFSDeviceComponent with a
RefPtr.
Also, we also call the before_removing method in both SlavePTY::unref
and File::unref, so because Device has that method being overrided, it
can ensure the device is removed always cleanly.
2021-09-11 11:41:14 +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
fb7b4caa57 Kernel/Storage: Implement basic AHCI hotplug support
This is really a basic support for AHCI hotplug events, so we know how
to add a node representing the device in /sys/dev/block and removing it
according to the event type (insertion/removal).

This change doesn't take into account what happens if the device was
mounted or a read/write operation is being handled.

For this to work correctly, StorageManagement now uses the Singleton
container, as it might be accessed simultaneously from many CPUs
for hotplug events. DiskPartition holds a WeakPtr instead of a RefPtr,
to allow removal of a StorageDevice object from the heap.
StorageDevices are now stored and being referenced to via an
IntrusiveList to make it easier to remove them on hotplug event.

In future changes, all of the stated above might change, but for now,
this commit represents the least amount of changes to make everything
to work correctly.
2021-09-08 00:42:20 +02:00
Liav A
6a9c717a30 Kernel: Expose device presence in /sys/dev/char and /sys/dev/block
These files are not marked as block devices or character devices so they
are not meant to be used as device nodes. The filenames are formatted to
the pattern "major:minor", but a Userland program need to call the parse
these format and inspect the the major and minor numbers and create the
real device nodes in /dev.

Later on, it might be a good idea to ensure we don't create new
SysFSComponents on the heap for each Device, but rather generate
them only when required (and preferably to not create a SysFSComponent
at all if possible).
2021-09-08 00:42:20 +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
9669bf29f6 Kernel: Make Device request creation return KResultOr
This allows us to propagate errors in a bunch of new places.
2021-09-07 16:42:03 +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
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
4b4e1d1c90 Kernel: Remove redundant [[nodiscard]] on KResult return values
Both KResult and KResultOr are [[nodiscard]] at the class level,
so there's no need to have functions return `[[nodiscard]] KResult`.
2021-09-07 01:18:02 +02:00
Andreas Kling
bdb697f6de Kernel: Use TRY() in MemoryDevice::mmap() 2021-09-06 20:33:45 +02:00
Andreas Kling
d6fe5e1e5b Kernel/KCOV: Use TRY() in KCOVInstance::buffer_allocate() 2021-09-06 20:33:35 +02:00
Andreas Kling
f16b9a691f Kernel: Rename ProcessPagingScope => ScopedAddressSpaceSwitcher 2021-09-06 18:56:51 +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
91fe6b6552 Kernel/KCOV: Bring closer to typical SerenityOS coding style
- Remove a bunch of redundant `this->`
- Make class data members private and provide accessors instead
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
Andreas Kling
48a0b31c47 Kernel: Make copy_{from,to}_user() return KResult and use TRY()
This makes EFAULT propagation flow much more naturally. :^)
2021-09-05 17:38:37 +02:00
Andreas Kling
c9a20bcd87 Kernel: Use TRY() in the SoundBlaster16 driver 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
Brian Gianforcaro
9d1b27263f Kernel: Declare type aliases with "using" instead of "typedef"
This is the idiomatic way to declare type aliases in modern C++.
Flagged by Sonar Cloud as a "Code Smell", but I happen to agree
with this particular one. :^)
2021-09-05 09:48:43 +01:00
Liav A
d189cb6a24 Kernel/Devices: Remove unnecessary virtual method
The is_disk_device method is not being overrided by any class or called
by anyone, so let's just remove it.
2021-09-04 16:19:12 +02:00
Brian Gianforcaro
668c429900 Kernel: Convert UserOrKernelBuffer callbacks to use AK::Bytes 2021-09-01 18:06:14 +02:00
Brian Gianforcaro
f3baa5d8c9 Kernel: Convert random bytes interface to use AK::Bytes 2021-09-01 18:06:14 +02:00
LepkoQQ
25f76ed771 Kernel: Fix shift sometimes staying pressed after releasing the key
Previous implementation sometimes didn't release the key after pressing
and holding shift due to repeating key updates when holding keys. This
meant repeating updates would set/unset `m_both_shift_keys_pressed`
repeatedly, sometimes resulting in shift still being considered pressed
even after you released it.

Simplify left and right shift key pressed logic by tracking both key
states separately and always updating modifiers based on them.
2021-08-30 22:35:51 +02:00
Andreas Kling
ae197deb6b Kernel: Strongly typed user & group ID's
Prior to this change, both uid_t and gid_t were typedef'ed to `u32`.
This made it easy to use them interchangeably. Let's not allow that.

This patch adds UserID and GroupID using the AK::DistinctNumeric
mechanism we've already been employing for pid_t/ProcessID.
2021-08-29 01:09:19 +02:00
kleines Filmröllchen
d0ceaa24a6 Kernel: Implement ioctl for the SB16 to change sample rate
Two new ioctl requests are used to get and set the sample rate of the
sound card. The SB16 device keeps track of the sample rate separately,
because I don't want to figure out how to read the sample rate from the
device; it's easier that way.

The soundcard write doesn't set the sample rate to 44100 Hz every time
anymore, as we want to change it externally.
2021-08-27 23:35:27 +04:30
kleines Filmröllchen
2c9afaf5ac Kernel: Modernize SB16.cpp
This was some old code that could use mostly some east-const :^)
2021-08-27 23:35:27 +04:30
Liav A
aacb1f0bf4 Kernel: Rename PCI::DeviceController => PCI::Device
Now that the old PCI::Device was removed, we can complete the PCI
changes by making the PCI::DeviceController to be named PCI::Device.

Really the entire purpose and the distinction between the two was about
interrupts, but since this is no longer a problem, just rename it to
simplify things further.
2021-08-23 01:07:45 +02:00
Liav A
7b9c3439ec Kernel/PCI: Delete PCI::Device in its current form
I created this class a long time ago just to be able to quickly make a
PCI device to also represent an interrupt handler (because PCI devices
have this capability for most devices).
Then after a while I introduced the PCI::DeviceController, which is
really almost the same thing (a PCI device class that has Address member
in it), but is not tied to interrupts so it can have no interrupts, or
spawn interrupt handlers however it wants to seems fit.

However I decided it's time to say goodbye for this class for
a couple of reasons:
1. It made a whole bunch of weird patterns where you had a PCI::Device
and a PCI::DeviceController being used in the topic of implementation,
where originally, they meant to be used mutually exclusively (you
can't and really don't want to use both).
2. We can really make all the classes that inherit from PCI::Device
to inherit from IRQHandler at this point. Later on, when we have MSI
interrupts support, we can go further and untie things even more.
3. It makes it possible to simplify the VirtIO implementation to a great
extent. While this commit almost doesn't change it, future changes
can untangle some complexity in the VirtIO code.

For UHCIController, E1000NetworkAdapter, NE2000NetworkAdapter,
RTL8139NetworkAdapter, RTL8168NetworkAdapter, E1000ENetworkAdapter we
are simply making them to inherit the IRQHandler. This makes some sense,
because the first 3 devices will never support anything besides IRQs.
For the last 2, they might have MSI support, so when we start to utilize
those, we might need to untie these classes from IRQHandler and spawn
IRQHandler(s) or MSIHandler(s) as needed.

The VirtIODevice class is also a case where we currently need to use
both PCI::DeviceController and IRQHandler classes as parents, but it
could also be untied from the latter.
2021-08-23 01:07:45 +02:00
Andreas Kling
d60635cb9d Kernel: Convert Processor::in_irq() to static current_in_irq()
This closes the race window between Processor::current() and a context
switch happening before in_irq().
2021-08-23 00:02:09 +02:00
Andreas Kling
c922a7da09 Kernel: Rename ScopedSpinlock => SpinlockLocker
This matches MutexLocker, and doesn't sound like it's a lock itself.
2021-08-22 03:34:10 +02:00
Andreas Kling
55adace359 Kernel: Rename SpinLock => Spinlock 2021-08-22 03:34:10 +02:00
Idan Horowitz
cf271183b4 Kernel: Make Process::current() return a Process& instead of Process*
This has several benefits:
1) We no longer just blindly derefence a null pointer in various places
2) We will get nicer runtime error messages if the current process does
turn out to be null in the call location
3) GCC no longer complains about possible nullptr dereferences when
compiling without KUBSAN
2021-08-19 23:49:53 +02:00
sin-ack
4bfd6e41b9 Kernel: Make Kernel::VMObject allocation functions return KResultOr
This makes for nicer handling of errors compared to checking whether a
RefPtr is null. Additionally, this will give way to return different
types of errors in the future.
2021-08-15 15:41:02 +02:00
Andreas Kling
c94c15d45c Everywhere: Replace AK::Singleton => Singleton 2021-08-08 00:03:45 +02:00