1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:48:12 +00:00

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.
This commit is contained in:
Liav A 2021-09-11 09:19:20 +03:00 committed by Idan Horowitz
parent 9aa6dd6b78
commit aee4786d8e
43 changed files with 244 additions and 123 deletions

View file

@ -48,6 +48,7 @@ set(KERNEL_SOURCES
Devices/BlockDevice.cpp
Devices/CharacterDevice.cpp
Devices/Device.cpp
Devices/DeviceManagement.cpp
Devices/FullDevice.cpp
Devices/KCOVDevice.cpp
Devices/KCOVInstance.cpp

View file

@ -6,20 +6,13 @@
#include <AK/Singleton.h>
#include <Kernel/Devices/Device.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/FileSystem/InodeMetadata.h>
#include <Kernel/FileSystem/SysFS.h>
#include <Kernel/Locking/MutexProtected.h>
#include <Kernel/Sections.h>
namespace Kernel {
static Singleton<MutexProtected<HashMap<u32, Device*>>> s_all_devices;
MutexProtected<HashMap<u32, Device*>>& Device::all_devices()
{
return *s_all_devices;
}
NonnullRefPtr<SysFSDeviceComponent> SysFSDeviceComponent::must_create(Device const& device)
{
return adopt_ref_if_nonnull(new SysFSDeviceComponent(device)).release_nonnull();
@ -115,41 +108,15 @@ RefPtr<SysFSComponent> SysFSCharacterDevicesDirectory::lookup(StringView name)
});
}
void Device::for_each(Function<void(Device&)> callback)
{
all_devices().with_exclusive([&](auto& map) -> void {
for (auto& entry : map)
callback(*entry.value);
});
}
Device* Device::get_device(unsigned major, unsigned minor)
{
return all_devices().with_exclusive([&](auto& map) -> Device* {
auto it = map.find(encoded_device(major, minor));
if (it == map.end())
return nullptr;
return it->value;
});
}
Device::Device(unsigned major, unsigned minor)
: m_major(major)
, m_minor(minor)
{
u32 device_id = encoded_device(major, minor);
all_devices().with_exclusive([&](auto& map) -> void {
auto it = map.find(device_id);
if (it != map.end()) {
dbgln("Already registered {},{}: {}", major, minor, it->value->class_name());
}
VERIFY(!map.contains(device_id));
map.set(device_id, this);
});
}
void Device::after_inserting()
{
DeviceManagement::the().after_inserting_device({}, *this);
VERIFY(!m_sysfs_component);
auto sys_fs_component = SysFSDeviceComponent::must_create(*this);
m_sysfs_component = sys_fs_component;
@ -160,21 +127,17 @@ void Device::after_inserting()
void Device::before_removing()
{
m_state = State::BeingRemoved;
VERIFY(m_sysfs_component);
SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> void {
list.remove(*m_sysfs_component);
});
DeviceManagement::the().before_device_removal({}, *this);
m_state = State::BeingRemoved;
}
Device::~Device()
{
VERIFY(m_state == State::BeingRemoved);
u32 device_id = encoded_device(m_major, m_minor);
all_devices().with_exclusive([&](auto& map) -> void {
VERIFY(map.contains(device_id));
map.remove(encoded_device(m_major, m_minor));
});
}
String Device::absolute_path() const

View file

@ -27,14 +27,6 @@
namespace Kernel {
template<typename DeviceType, typename... Args>
inline KResultOr<NonnullRefPtr<DeviceType>> try_create_device(Args&&... args)
{
auto device = TRY(adopt_nonnull_ref_or_enomem(new DeviceType(forward<Args>(args)...)));
device->after_inserting();
return device;
}
class Device : public File {
protected:
enum class State {
@ -57,10 +49,6 @@ public:
virtual bool is_device() const override { return true; }
virtual void before_removing() override;
virtual void after_inserting();
static void for_each(Function<void(Device&)>);
static Device* get_device(unsigned major, unsigned minor);
void process_next_queued_request(Badge<AsyncDeviceRequest>, const AsyncDeviceRequest&);
template<typename AsyncRequestType, typename... Args>
@ -80,8 +68,6 @@ protected:
void set_uid(UserID uid) { m_uid = uid; }
void set_gid(GroupID gid) { m_gid = gid; }
static MutexProtected<HashMap<u32, Device*>>& all_devices();
private:
unsigned m_major { 0 };
unsigned m_minor { 0 };

View file

@ -0,0 +1,88 @@
/*
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/HashTable.h>
#include <AK/Singleton.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/FileSystem/InodeMetadata.h>
#include <Kernel/Sections.h>
namespace Kernel {
static Singleton<DeviceManagement> s_the;
UNMAP_AFTER_INIT DeviceManagement::DeviceManagement()
{
}
UNMAP_AFTER_INIT void DeviceManagement::initialize()
{
s_the.ensure_instance();
}
UNMAP_AFTER_INIT void DeviceManagement::attach_null_device(NullDevice const& device)
{
m_null_device = device;
}
DeviceManagement& DeviceManagement::the()
{
return *s_the;
}
Device* DeviceManagement::get_device(unsigned major, unsigned minor)
{
return m_devices.with_exclusive([&](auto& map) -> Device* {
auto it = map.find(encoded_device(major, minor));
if (it == map.end())
return nullptr;
return it->value;
});
}
void DeviceManagement::before_device_removal(Badge<Device>, Device& device)
{
u32 device_id = encoded_device(device.major(), device.minor());
m_devices.with_exclusive([&](auto& map) -> void {
VERIFY(map.contains(device_id));
map.remove(encoded_device(device.major(), device.minor()));
});
}
void DeviceManagement::after_inserting_device(Badge<Device>, Device& device)
{
u32 device_id = encoded_device(device.major(), device.minor());
m_devices.with_exclusive([&](auto& map) -> void {
if (map.contains(device_id)) {
dbgln("Already registered {},{}: {}", device.major(), device.minor(), device.class_name());
VERIFY_NOT_REACHED();
}
auto result = map.set(device_id, &device);
if (result != AK::HashSetResult::InsertedNewEntry) {
dbgln("Failed to register {},{}: {}", device.major(), device.minor(), device.class_name());
VERIFY_NOT_REACHED();
}
});
}
void DeviceManagement::for_each(Function<void(Device&)> callback)
{
m_devices.with_exclusive([&](auto& map) -> void {
for (auto& entry : map)
callback(*entry.value);
});
}
NullDevice& DeviceManagement::null_device()
{
return *m_null_device;
}
NullDevice const& DeviceManagement::null_device() const
{
return *m_null_device;
}
}

View file

@ -0,0 +1,54 @@
/*
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Badge.h>
#include <AK/NonnullRefPtrVector.h>
#include <AK/OwnPtr.h>
#include <AK/RefPtr.h>
#include <AK/Time.h>
#include <AK/Types.h>
#include <Kernel/API/KResult.h>
#include <Kernel/API/TimePage.h>
#include <Kernel/Arch/x86/RegisterState.h>
#include <Kernel/Devices/Device.h>
#include <Kernel/Devices/NullDevice.h>
#include <Kernel/UnixTypes.h>
namespace Kernel {
class DeviceManagement {
AK_MAKE_ETERNAL;
public:
DeviceManagement();
static void initialize();
static DeviceManagement& the();
void attach_null_device(NullDevice const&);
void after_inserting_device(Badge<Device>, Device&);
void before_device_removal(Badge<Device>, Device&);
void for_each(Function<void(Device&)>);
Device* get_device(unsigned major, unsigned minor);
NullDevice const& null_device() const;
NullDevice& null_device();
template<typename DeviceType, typename... Args>
static inline KResultOr<NonnullRefPtr<DeviceType>> try_create_device(Args&&... args)
{
auto device = TRY(adopt_nonnull_ref_or_enomem(new DeviceType(forward<Args>(args)...)));
device->after_inserting();
return device;
}
private:
RefPtr<NullDevice> m_null_device;
MutexProtected<HashMap<u32, Device*>> m_devices;
};
}

View file

@ -5,6 +5,7 @@
*/
#include <AK/Memory.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/FullDevice.h>
#include <Kernel/Sections.h>
#include <LibC/errno_numbers.h>
@ -13,7 +14,7 @@ namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<FullDevice> FullDevice::must_create()
{
auto full_device_or_error = try_create_device<FullDevice>();
auto full_device_or_error = DeviceManagement::try_create_device<FullDevice>();
// FIXME: Find a way to propagate errors
VERIFY(!full_device_or_error.is_error());
return full_device_or_error.release_value();

View file

@ -12,14 +12,15 @@ namespace Kernel {
class FullDevice final : public CharacterDevice {
AK_MAKE_ETERNAL
friend class DeviceManagement;
public:
static NonnullRefPtr<FullDevice> must_create();
virtual ~FullDevice() override;
// FIXME: We expose this constructor to make try_create_device helper to work
private:
FullDevice();
private:
// ^CharacterDevice
virtual KResultOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
virtual KResultOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override;

View file

@ -9,6 +9,7 @@
#include <AK/Singleton.h>
#include <AK/Types.h>
#include <Kernel/Debug.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/HID/HIDManagement.h>
#include <Kernel/Devices/HID/PS2KeyboardDevice.h>
#include <Kernel/IO.h>
@ -83,7 +84,7 @@ bool PS2KeyboardDevice::handle_irq(const RegisterState&)
UNMAP_AFTER_INIT RefPtr<PS2KeyboardDevice> PS2KeyboardDevice::try_to_initialize(const I8042Controller& ps2_controller)
{
auto keyboard_device_or_error = try_create_device<PS2KeyboardDevice>(ps2_controller);
auto keyboard_device_or_error = DeviceManagement::try_create_device<PS2KeyboardDevice>(ps2_controller);
// FIXME: Find a way to propagate errors
VERIFY(!keyboard_device_or_error.is_error());
if (keyboard_device_or_error.value()->initialize())

View file

@ -21,6 +21,8 @@ namespace Kernel {
class PS2KeyboardDevice final : public IRQHandler
, public KeyboardDevice
, public I8042Device {
friend class DeviceManagement;
public:
static RefPtr<PS2KeyboardDevice> try_to_initialize(const I8042Controller&);
virtual ~PS2KeyboardDevice() override;
@ -35,10 +37,9 @@ public:
enable_irq();
}
// FIXME: We expose this constructor to make try_create_device helper to work
private:
explicit PS2KeyboardDevice(const I8042Controller&);
private:
// ^IRQHandler
virtual bool handle_irq(const RegisterState&) override;

View file

@ -6,6 +6,7 @@
#include <AK/Memory.h>
#include <Kernel/Debug.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/HID/PS2MouseDevice.h>
#include <Kernel/Devices/VMWareBackdoor.h>
#include <Kernel/IO.h>
@ -174,7 +175,7 @@ void PS2MouseDevice::set_sample_rate(u8 rate)
UNMAP_AFTER_INIT RefPtr<PS2MouseDevice> PS2MouseDevice::try_to_initialize(const I8042Controller& ps2_controller)
{
auto mouse_device_or_error = try_create_device<PS2MouseDevice>(ps2_controller);
auto mouse_device_or_error = DeviceManagement::try_create_device<PS2MouseDevice>(ps2_controller);
// FIXME: Find a way to propagate errors
VERIFY(!mouse_device_or_error.is_error());
if (mouse_device_or_error.value()->initialize())

View file

@ -17,6 +17,8 @@ namespace Kernel {
class PS2MouseDevice : public IRQHandler
, public MouseDevice
, public I8042Device {
friend class DeviceManagement;
public:
static RefPtr<PS2MouseDevice> try_to_initialize(const I8042Controller&);
bool initialize();
@ -32,10 +34,9 @@ public:
enable_irq();
}
// FIXME: We expose this constructor to make try_create_device helper to work
protected:
explicit PS2MouseDevice(const I8042Controller&);
protected:
// ^IRQHandler
virtual bool handle_irq(const RegisterState&) override;

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/HID/VMWareMouseDevice.h>
#include <Kernel/Devices/VMWareBackdoor.h>
#include <Kernel/Sections.h>
@ -16,7 +17,7 @@ UNMAP_AFTER_INIT RefPtr<VMWareMouseDevice> VMWareMouseDevice::try_to_initialize(
return {};
if (!VMWareBackdoor::the()->vmmouse_is_absolute())
return {};
auto mouse_device_or_error = try_create_device<VMWareMouseDevice>(ps2_controller);
auto mouse_device_or_error = DeviceManagement::try_create_device<VMWareMouseDevice>(ps2_controller);
// FIXME: Find a way to propagate errors
VERIFY(!mouse_device_or_error.is_error());
if (mouse_device_or_error.value()->initialize())

View file

@ -17,13 +17,14 @@ namespace Kernel {
class VMWareMouseDevice final : public PS2MouseDevice {
public:
friend class DeviceManagement;
static RefPtr<VMWareMouseDevice> try_to_initialize(const I8042Controller&);
virtual ~VMWareMouseDevice() override;
// ^I8042Device
virtual void irq_handle_byte_read(u8 byte) override;
// FIXME: We expose this constructor to make try_create_device helper to work
private:
explicit VMWareMouseDevice(const I8042Controller&);
};

View file

@ -6,6 +6,7 @@
#include <AK/Assertions.h>
#include <AK/NonnullOwnPtr.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/KCOVDevice.h>
#include <Kernel/Devices/KCOVInstance.h>
#include <Kernel/FileSystem/OpenFileDescription.h>
@ -20,7 +21,7 @@ HashMap<ThreadID, KCOVInstance*>* KCOVDevice::thread_instance;
UNMAP_AFTER_INIT NonnullRefPtr<KCOVDevice> KCOVDevice::must_create()
{
auto kcov_device_or_error = try_create_device<KCOVDevice>();
auto kcov_device_or_error = DeviceManagement::try_create_device<KCOVDevice>();
// FIXME: Find a way to propagate errors
VERIFY(!kcov_device_or_error.is_error());
return kcov_device_or_error.release_value();

View file

@ -12,6 +12,7 @@
namespace Kernel {
class KCOVDevice final : public BlockDevice {
AK_MAKE_ETERNAL
friend class DeviceManagement;
public:
static HashMap<ProcessID, KCOVInstance*>* proc_instance;
@ -25,10 +26,9 @@ public:
KResultOr<Memory::Region*> mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override;
KResultOr<NonnullRefPtr<OpenFileDescription>> open(int options) override;
// FIXME: We expose this constructor to make try_create_device helper to work
protected:
KCOVDevice();
protected:
virtual StringView class_name() const override { return "KCOVDevice"; }
virtual bool can_read(const OpenFileDescription&, size_t) const override final { return true; }

View file

@ -6,6 +6,7 @@
#include <AK/Memory.h>
#include <AK/StdLibExtras.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/MemoryDevice.h>
#include <Kernel/Firmware/BIOS.h>
#include <Kernel/Memory/AnonymousVMObject.h>
@ -15,7 +16,7 @@ namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<MemoryDevice> MemoryDevice::must_create()
{
auto memory_device_or_error = try_create_device<MemoryDevice>();
auto memory_device_or_error = DeviceManagement::try_create_device<MemoryDevice>();
// FIXME: Find a way to propagate errors
VERIFY(!memory_device_or_error.is_error());
return memory_device_or_error.release_value();

View file

@ -15,16 +15,17 @@ namespace Kernel {
class MemoryDevice final : public CharacterDevice {
AK_MAKE_ETERNAL
friend class DeviceManagement;
public:
static NonnullRefPtr<MemoryDevice> must_create();
~MemoryDevice();
virtual KResultOr<Memory::Region*> mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override;
// FIXME: We expose this constructor to make try_create_device helper to work
private:
MemoryDevice();
private:
virtual StringView class_name() const override { return "MemoryDevice"; }
virtual bool can_read(const OpenFileDescription&, size_t) const override { return true; }
virtual bool can_write(const OpenFileDescription&, size_t) const override { return false; }

View file

@ -5,22 +5,18 @@
*/
#include <AK/Singleton.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/NullDevice.h>
#include <Kernel/Sections.h>
namespace Kernel {
static Singleton<NullDevice> s_the;
UNMAP_AFTER_INIT void NullDevice::initialize()
UNMAP_AFTER_INIT NonnullRefPtr<NullDevice> NullDevice::must_initialize()
{
s_the.ensure_instance();
s_the->after_inserting();
}
NullDevice& NullDevice::the()
{
return *s_the;
auto null_device_or_error = DeviceManagement::try_create_device<NullDevice>();
// FIXME: Find a way to propagate errors
VERIFY(!null_device_or_error.is_error());
return null_device_or_error.release_value();
}
UNMAP_AFTER_INIT NullDevice::NullDevice()

View file

@ -12,14 +12,15 @@ namespace Kernel {
class NullDevice final : public CharacterDevice {
AK_MAKE_ETERNAL
friend class DeviceManagement;
public:
NullDevice();
virtual ~NullDevice() override;
static void initialize();
static NullDevice& the();
static NonnullRefPtr<NullDevice> must_initialize();
private:
NullDevice();
// ^CharacterDevice
virtual KResultOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
virtual KResultOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override;

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/RandomDevice.h>
#include <Kernel/Random.h>
#include <Kernel/Sections.h>
@ -12,7 +13,7 @@ namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<RandomDevice> RandomDevice::must_create()
{
auto random_device_or_error = try_create_device<RandomDevice>();
auto random_device_or_error = DeviceManagement::try_create_device<RandomDevice>();
// FIXME: Find a way to propagate errors
VERIFY(!random_device_or_error.is_error());
return random_device_or_error.release_value();

View file

@ -12,14 +12,15 @@ namespace Kernel {
class RandomDevice final : public CharacterDevice {
AK_MAKE_ETERNAL
friend class DeviceManagement;
public:
static NonnullRefPtr<RandomDevice> must_create();
virtual ~RandomDevice() override;
// FIXME: We expose this constructor to make try_create_device helper to work
private:
RandomDevice();
private:
// ^CharacterDevice
virtual KResultOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
virtual KResultOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override;

View file

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/SerialDevice.h>
#include <Kernel/IO.h>
#include <Kernel/Sections.h>
@ -23,19 +24,19 @@ UNMAP_AFTER_INIT NonnullRefPtr<SerialDevice> SerialDevice::must_create(size_t co
RefPtr<SerialDevice> serial_device;
switch (com_number) {
case 0: {
serial_device = try_create_device<SerialDevice>(IOAddress(SERIAL_COM1_ADDR), 64).release_value();
serial_device = DeviceManagement::try_create_device<SerialDevice>(IOAddress(SERIAL_COM1_ADDR), 64).release_value();
break;
}
case 1: {
serial_device = try_create_device<SerialDevice>(IOAddress(SERIAL_COM2_ADDR), 65).release_value();
serial_device = DeviceManagement::try_create_device<SerialDevice>(IOAddress(SERIAL_COM2_ADDR), 65).release_value();
break;
}
case 2: {
serial_device = try_create_device<SerialDevice>(IOAddress(SERIAL_COM3_ADDR), 66).release_value();
serial_device = DeviceManagement::try_create_device<SerialDevice>(IOAddress(SERIAL_COM3_ADDR), 66).release_value();
break;
}
case 3: {
serial_device = try_create_device<SerialDevice>(IOAddress(SERIAL_COM4_ADDR), 67).release_value();
serial_device = DeviceManagement::try_create_device<SerialDevice>(IOAddress(SERIAL_COM4_ADDR), 67).release_value();
break;
}
default:

View file

@ -13,6 +13,8 @@ namespace Kernel {
class SerialDevice final : public CharacterDevice {
AK_MAKE_ETERNAL
friend class DeviceManagement;
public:
static NonnullRefPtr<SerialDevice> must_create(size_t com_number);
@ -102,10 +104,9 @@ public:
DataReady = 0x01 << 0
};
// FIXME: We expose this constructor to make try_create_device helper to work
private:
SerialDevice(IOAddress base_addr, unsigned minor);
private:
friend class PCISerialDevice;
// ^CharacterDevice

View file

@ -5,6 +5,7 @@
*/
#include <AK/Memory.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/ZeroDevice.h>
#include <Kernel/Sections.h>
@ -12,7 +13,7 @@ namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<ZeroDevice> ZeroDevice::must_create()
{
auto zero_device_or_error = try_create_device<ZeroDevice>();
auto zero_device_or_error = DeviceManagement::try_create_device<ZeroDevice>();
// FIXME: Find a way to propagate errors
VERIFY(!zero_device_or_error.is_error());
return zero_device_or_error.release_value();

View file

@ -12,14 +12,15 @@ namespace Kernel {
class ZeroDevice final : public CharacterDevice {
AK_MAKE_ETERNAL
friend class DeviceManagement;
public:
static NonnullRefPtr<ZeroDevice> must_create();
virtual ~ZeroDevice() override;
// FIXME: We expose this constructor to make try_create_device helper to work
private:
ZeroDevice();
private:
// ^CharacterDevice
virtual KResultOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
virtual KResultOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override;

View file

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/FileSystem/DevPtsFS.h>
#include <Kernel/FileSystem/VirtualFileSystem.h>
#include <Kernel/TTY/SlavePTY.h>
@ -58,7 +59,7 @@ KResultOr<NonnullRefPtr<Inode>> DevPtsFS::get_inode(InodeIdentifier inode_id) co
return *m_root_inode;
unsigned pty_index = inode_index_to_pty_index(inode_id.index());
auto* device = Device::get_device(201, pty_index);
auto* device = DeviceManagement::the().get_device(201, pty_index);
VERIFY(device);
auto inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevPtsFSInode(const_cast<DevPtsFS&>(*this), inode_id.index(), static_cast<SlavePTY*>(device))));

View file

@ -5,6 +5,7 @@
*/
#include <AK/StringView.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/FileSystem/DevTmpFS.h>
#include <Kernel/FileSystem/VirtualFileSystem.h>
@ -321,7 +322,7 @@ KResultOr<size_t> DevTmpFSDeviceInode::read_bytes(off_t offset, size_t count, Us
{
MutexLocker locker(m_inode_lock);
VERIFY(!!description);
RefPtr<Device> device = Device::get_device(m_major_number, m_minor_number);
RefPtr<Device> device = DeviceManagement::the().get_device(m_major_number, m_minor_number);
if (!device)
return KResult(ENODEV);
if (!device->can_read(*description, offset))
@ -336,7 +337,7 @@ KResultOr<size_t> DevTmpFSDeviceInode::write_bytes(off_t offset, size_t count, c
{
MutexLocker locker(m_inode_lock);
VERIFY(!!description);
RefPtr<Device> device = Device::get_device(m_major_number, m_minor_number);
RefPtr<Device> device = DeviceManagement::the().get_device(m_major_number, m_minor_number);
if (!device)
return KResult(ENODEV);
if (!device->can_write(*description, offset))

View file

@ -9,6 +9,7 @@
#include <AK/StringBuilder.h>
#include <Kernel/Debug.h>
#include <Kernel/Devices/BlockDevice.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/FileBackedFileSystem.h>
#include <Kernel/FileSystem/FileSystem.h>
@ -270,7 +271,7 @@ KResultOr<NonnullRefPtr<OpenFileDescription>> VirtualFileSystem::open(StringView
if (metadata.is_device()) {
if (custody.mount_flags() & MS_NODEV)
return EACCES;
auto device = Device::get_device(metadata.major_device, metadata.minor_device);
auto device = DeviceManagement::the().get_device(metadata.major_device, metadata.minor_device);
if (device == nullptr) {
return ENODEV;
}

View file

@ -12,6 +12,7 @@
#include <Kernel/Bus/PCI/API.h>
#include <Kernel/CommandLine.h>
#include <Kernel/ConsoleDevice.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/HID/HIDManagement.h>
#include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/FileBackedFileSystem.h>
@ -640,7 +641,7 @@ private:
virtual KResult try_generate(KBufferBuilder& builder) override
{
JsonArraySerializer array { builder };
Device::for_each([&array](auto& device) {
DeviceManagement::the().for_each([&array](auto& device) {
auto obj = array.add_object();
obj.add("major", device.major());
obj.add("minor", device.minor());

View file

@ -6,6 +6,7 @@
#include <AK/Checked.h>
#include <Kernel/Debug.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Graphics/FramebufferDevice.h>
#include <Kernel/Graphics/GraphicsManagement.h>
#include <Kernel/Memory/AnonymousVMObject.h>
@ -22,7 +23,7 @@ namespace Kernel {
NonnullRefPtr<FramebufferDevice> FramebufferDevice::create(const GraphicsDevice& adapter, size_t output_port_index, PhysicalAddress paddr, size_t width, size_t height, size_t pitch)
{
auto framebuffer_device_or_error = try_create_device<FramebufferDevice>(adapter, output_port_index, paddr, width, height, pitch);
auto framebuffer_device_or_error = DeviceManagement::try_create_device<FramebufferDevice>(adapter, output_port_index, paddr, width, height, pitch);
// FIXME: Find a way to propagate errors
VERIFY(!framebuffer_device_or_error.is_error());
return framebuffer_device_or_error.release_value();

View file

@ -19,6 +19,8 @@ namespace Kernel {
class FramebufferDevice : public BlockDevice {
AK_MAKE_ETERNAL
friend class DeviceManagement;
public:
static NonnullRefPtr<FramebufferDevice> create(const GraphicsDevice&, size_t, PhysicalAddress, size_t, size_t, size_t);
@ -32,10 +34,9 @@ public:
virtual ~FramebufferDevice() {};
KResult initialize();
// FIXME: We expose this constructor to make try_create_device helper to work
private:
FramebufferDevice(const GraphicsDevice&, size_t, PhysicalAddress, size_t, size_t, size_t);
private:
// ^File
virtual StringView class_name() const override { return "FramebufferDevice"; }

View file

@ -13,6 +13,7 @@
#include <Kernel/Arch/x86/InterruptDisabler.h>
#include <Kernel/Coredump.h>
#include <Kernel/Debug.h>
#include <Kernel/Devices/DeviceManagement.h>
#ifdef ENABLE_KERNEL_COVERAGE_COLLECTION
# include <Kernel/Devices/KCOVDevice.h>
#endif
@ -156,7 +157,7 @@ KResultOr<NonnullRefPtr<Process>> Process::try_create_user_process(RefPtr<Thread
first_thread = nullptr;
return ENOMEM;
}
auto& device_to_use_as_tty = tty ? (CharacterDevice&)*tty : NullDevice::the();
auto& device_to_use_as_tty = tty ? (CharacterDevice&)*tty : DeviceManagement::the().null_device();
auto description = TRY(device_to_use_as_tty.open(O_RDWR));
auto setup_description = [&process, &description](int fd) {
process->m_fds.m_fds_metadatas[fd].allocate();

View file

@ -5,6 +5,7 @@
*/
#include <AK/StringView.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/FileSystem/OpenFileDescription.h>
#include <Kernel/Sections.h>
#include <Kernel/Storage/IDEChannel.h>
@ -15,7 +16,7 @@ namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<PATADiskDevice> PATADiskDevice::create(const IDEController& controller, IDEChannel& channel, DriveType type, InterfaceType interface_type, u16 capabilities, u64 max_addressable_block)
{
auto device_or_error = try_create_device<PATADiskDevice>(controller, channel, type, interface_type, capabilities, max_addressable_block);
auto device_or_error = DeviceManagement::try_create_device<PATADiskDevice>(controller, channel, type, interface_type, capabilities, max_addressable_block);
// FIXME: Find a way to propagate errors
VERIFY(!device_or_error.is_error());
return device_or_error.release_value();

View file

@ -20,6 +20,7 @@ class IDEController;
class IDEChannel;
class PATADiskDevice final : public StorageDevice {
friend class IDEController;
friend class DeviceManagement;
AK_MAKE_ETERNAL
public:
// Type of drive this IDEDiskDevice is on the ATA channel.
@ -44,10 +45,9 @@ public:
virtual void start_request(AsyncBlockDeviceRequest&) override;
virtual String storage_name() const override;
// FIXME: We expose this constructor to make try_create_device helper to work
private:
PATADiskDevice(const IDEController&, IDEChannel&, DriveType, InterfaceType, u16, u64);
private:
// ^DiskDevice
virtual StringView class_name() const override;

View file

@ -5,6 +5,7 @@
*/
#include <Kernel/Debug.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/FileSystem/OpenFileDescription.h>
#include <Kernel/Storage/Partition/DiskPartition.h>
@ -12,7 +13,7 @@ namespace Kernel {
NonnullRefPtr<DiskPartition> DiskPartition::create(BlockDevice& device, unsigned minor_number, DiskPartitionMetadata metadata)
{
auto partition_or_error = try_create_device<DiskPartition>(device, minor_number, metadata);
auto partition_or_error = DeviceManagement::try_create_device<DiskPartition>(device, minor_number, metadata);
// FIXME: Find a way to propagate errors
VERIFY(!partition_or_error.is_error());
return partition_or_error.release_value();

View file

@ -14,6 +14,8 @@
namespace Kernel {
class DiskPartition final : public BlockDevice {
friend class DeviceManagement;
public:
static NonnullRefPtr<DiskPartition> create(BlockDevice&, unsigned, DiskPartitionMetadata);
virtual ~DiskPartition();
@ -28,10 +30,8 @@ public:
const DiskPartitionMetadata& metadata() const;
// FIXME: We expose this constructor to make try_create_device helper to work
DiskPartition(BlockDevice&, unsigned, DiskPartitionMetadata);
private:
DiskPartition(BlockDevice&, unsigned, DiskPartitionMetadata);
virtual StringView class_name() const override;
WeakPtr<BlockDevice> m_device;

View file

@ -6,6 +6,7 @@
#include <AK/Memory.h>
#include <AK/StringView.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/FileSystem/OpenFileDescription.h>
#include <Kernel/Storage/RamdiskController.h>
#include <Kernel/Storage/RamdiskDevice.h>
@ -14,7 +15,7 @@ namespace Kernel {
NonnullRefPtr<RamdiskDevice> RamdiskDevice::create(const RamdiskController& controller, NonnullOwnPtr<Memory::Region>&& region, int major, int minor)
{
auto device_or_error = try_create_device<RamdiskDevice>(controller, move(region), major, minor);
auto device_or_error = DeviceManagement::try_create_device<RamdiskDevice>(controller, move(region), major, minor);
// FIXME: Find a way to propagate errors
VERIFY(!device_or_error.is_error());
return device_or_error.release_value();

View file

@ -15,6 +15,7 @@ class RamdiskController;
class RamdiskDevice final : public StorageDevice {
friend class RamdiskController;
friend class DeviceManagement;
AK_MAKE_ETERNAL
public:
static NonnullRefPtr<RamdiskDevice> create(const RamdiskController&, NonnullOwnPtr<Memory::Region>&& region, int major, int minor);

View file

@ -5,6 +5,7 @@
*/
#include <AK/StringView.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/FileSystem/OpenFileDescription.h>
#include <Kernel/Storage/AHCIController.h>
#include <Kernel/Storage/IDEChannel.h>
@ -14,7 +15,7 @@ namespace Kernel {
NonnullRefPtr<SATADiskDevice> SATADiskDevice::create(const AHCIController& controller, const AHCIPort& port, size_t sector_size, u64 max_addressable_block)
{
auto device_or_error = try_create_device<SATADiskDevice>(controller, port, sector_size, max_addressable_block);
auto device_or_error = DeviceManagement::try_create_device<SATADiskDevice>(controller, port, sector_size, max_addressable_block);
// FIXME: Find a way to propagate errors
VERIFY(!device_or_error.is_error());
return device_or_error.release_value();

View file

@ -16,6 +16,7 @@ namespace Kernel {
class AHCIController;
class SATADiskDevice final : public StorageDevice {
friend class AHCIController;
friend class DeviceManagement;
public:
enum class InterfaceType : u8 {
@ -32,10 +33,9 @@ public:
virtual void start_request(AsyncBlockDeviceRequest&) override;
virtual String storage_name() const override;
// FIXME: We expose this constructor to make try_create_device helper to work
private:
SATADiskDevice(const AHCIController&, const AHCIPort&, size_t sector_size, u64 max_addressable_block);
private:
// ^DiskDevice
virtual StringView class_name() const override;
WeakPtr<AHCIPort> m_port;

View file

@ -9,6 +9,7 @@
#include <AK/StdLibExtras.h>
#include <AK/String.h>
#include <Kernel/Debug.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/HID/HIDManagement.h>
#include <Kernel/Graphics/GraphicsManagement.h>
#include <Kernel/Heap/kmalloc.h>
@ -103,7 +104,7 @@ void VirtualConsole::set_graphical(bool graphical)
UNMAP_AFTER_INIT NonnullRefPtr<VirtualConsole> VirtualConsole::create(size_t index)
{
auto virtual_console_or_error = try_create_device<VirtualConsole>(index);
auto virtual_console_or_error = DeviceManagement::try_create_device<VirtualConsole>(index);
// FIXME: Find a way to propagate errors
VERIFY(!virtual_console_or_error.is_error());
return virtual_console_or_error.release_value();
@ -111,7 +112,7 @@ UNMAP_AFTER_INIT NonnullRefPtr<VirtualConsole> VirtualConsole::create(size_t ind
UNMAP_AFTER_INIT NonnullRefPtr<VirtualConsole> VirtualConsole::create_with_preset_log(size_t index, const CircularQueue<char, 16384>& log)
{
auto virtual_console_or_error = try_create_device<VirtualConsole>(index, log);
auto virtual_console_or_error = DeviceManagement::try_create_device<VirtualConsole>(index, log);
// FIXME: Find a way to propagate errors
VERIFY(!virtual_console_or_error.is_error());
return virtual_console_or_error.release_value();

View file

@ -50,6 +50,7 @@ class VirtualConsole final : public TTY
, public VT::TerminalClient {
AK_MAKE_ETERNAL
friend class ConsoleManagement;
friend class DeviceManagement;
friend class ConsoleImpl;
friend class VT::Terminal;
@ -84,11 +85,9 @@ public:
void emit_char(char);
// FIXME: We expose these constructors to make try_create_device helper to work
private:
explicit VirtualConsole(const unsigned index);
VirtualConsole(const unsigned index, const CircularQueue<char, 16384>&);
private:
// ^KeyboardClient
virtual void on_key_pressed(KeyEvent) override;

View file

@ -13,6 +13,7 @@
#include <Kernel/Bus/VirtIO/Device.h>
#include <Kernel/CMOS.h>
#include <Kernel/CommandLine.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/FullDevice.h>
#include <Kernel/Devices/HID/HIDManagement.h>
#include <Kernel/Devices/KCOVDevice.h>
@ -182,7 +183,10 @@ extern "C" [[noreturn]] UNMAP_AFTER_INIT void init(BootInfo const& boot_info)
load_kernel_symbol_table();
DeviceManagement::initialize();
SysFSComponentRegistry::initialize();
DeviceManagement::the().attach_null_device(*NullDevice::must_initialize());
ConsoleDevice::initialize();
s_bsp_processor.initialize(0);
@ -281,7 +285,6 @@ void init_stage2(void*)
VirtualFileSystem::initialize();
NullDevice::initialize();
if (!get_serial_debug())
(void)SerialDevice::must_create(0).leak_ref();
(void)SerialDevice::must_create(1).leak_ref();