From 26d9a3e253f74f05acf93beb8ab4eb7ce68adc28 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 30 Mar 2023 22:39:45 +0300 Subject: [PATCH] Kernel: Move DeviceManager to its own file --- kernel/Makefile | 1 + kernel/include/kernel/Device.h | 52 --------- kernel/include/kernel/DeviceManager.h | 60 ++++++++++ kernel/include/kernel/FS/Inode.h | 1 + kernel/include/kernel/Storage/ATAController.h | 2 + kernel/kernel/Device.cpp | 103 ---------------- kernel/kernel/DeviceManager.cpp | 110 ++++++++++++++++++ kernel/kernel/FS/VirtualFileSystem.cpp | 2 +- kernel/kernel/Input/PS2Controller.cpp | 3 +- kernel/kernel/kernel.cpp | 1 + 10 files changed, 178 insertions(+), 157 deletions(-) create mode 100644 kernel/include/kernel/DeviceManager.h create mode 100644 kernel/kernel/DeviceManager.cpp diff --git a/kernel/Makefile b/kernel/Makefile index e3e1af8f..6c628250 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -42,6 +42,7 @@ kernel/build_libc.o \ kernel/CPUID.o \ kernel/Debug.o \ kernel/Device.o \ +kernel/DeviceManager.o \ kernel/Font.o \ kernel/FS/Ext2.o \ kernel/FS/VirtualFileSystem.o \ diff --git a/kernel/include/kernel/Device.h b/kernel/include/kernel/Device.h index b67bd956..f6959e61 100644 --- a/kernel/include/kernel/Device.h +++ b/kernel/include/kernel/Device.h @@ -1,10 +1,7 @@ #pragma once -#include -#include #include #include -#include namespace Kernel { @@ -47,53 +44,4 @@ namespace Kernel virtual DeviceType device_type() const override { return DeviceType::CharacterDevice; } }; - class DeviceManager final : public FileSystem, public Inode - { - BAN_NON_COPYABLE(DeviceManager); - BAN_NON_MOVABLE(DeviceManager); - - public: - static void initialize(); - static DeviceManager& get(); - - void update(); - void add_device(Device*); - - virtual BAN::RefPtr root_inode() override { return this; } - - virtual InodeType inode_type() const override { return InodeType::Device; } - - virtual BAN::StringView name() const override { return "device-manager"; } - - virtual BAN::ErrorOr> read_directory_inode(BAN::StringView) override; - virtual BAN::ErrorOr> read_directory_entries(size_t) override; - - private: - DeviceManager() = default; - - private: - SpinLock m_lock; - BAN::Vector m_devices; - - friend class BAN::RefPtr; - - public: - virtual ino_t ino() const override { return 0; } - virtual Mode mode() const override { return { Mode::IFDIR | Mode::IRUSR | Mode::IWUSR | Mode::IXUSR | Mode::IRGRP | Mode::IXGRP | Mode::IROTH | Mode::IXOTH }; } - virtual nlink_t nlink() const override { return 0; } - virtual uid_t uid() const override { return 0; } - virtual gid_t gid() const override { return 0; } - virtual off_t size() const override { return 0; } - virtual timespec atime() const override { return { 0, 0 }; } - virtual timespec mtime() const override { return { 0, 0 }; } - virtual timespec ctime() const override { return { 0, 0 }; } - virtual blksize_t blksize() const override { return 0; } - virtual blkcnt_t blocks() const override { return 0; } - virtual dev_t dev() const override { return 0x4900; } - virtual dev_t rdev() const override { return 0x7854; } - - virtual BAN::ErrorOr read(size_t, void*, size_t) { ASSERT_NOT_REACHED(); } - virtual BAN::ErrorOr create_file(BAN::StringView, mode_t) { ASSERT_NOT_REACHED(); } - }; - } \ No newline at end of file diff --git a/kernel/include/kernel/DeviceManager.h b/kernel/include/kernel/DeviceManager.h new file mode 100644 index 00000000..fb6ca2a2 --- /dev/null +++ b/kernel/include/kernel/DeviceManager.h @@ -0,0 +1,60 @@ +#pragma once + +#include +#include +#include +#include + +namespace Kernel +{ + + class DeviceManager final : public FileSystem, public Inode + { + BAN_NON_COPYABLE(DeviceManager); + BAN_NON_MOVABLE(DeviceManager); + + public: + static void initialize(); + static DeviceManager& get(); + + void update(); + void add_device(Device*); + + virtual BAN::RefPtr root_inode() override { return this; } + + virtual InodeType inode_type() const override { return InodeType::Device; } + + virtual BAN::StringView name() const override { return "device-manager"; } + + virtual BAN::ErrorOr> read_directory_inode(BAN::StringView) override; + virtual BAN::ErrorOr> read_directory_entries(size_t) override; + + private: + DeviceManager() = default; + + private: + SpinLock m_lock; + BAN::Vector m_devices; + + friend class BAN::RefPtr; + + public: + virtual ino_t ino() const override { return 0; } + virtual Mode mode() const override { return { Mode::IFDIR | Mode::IRUSR | Mode::IWUSR | Mode::IXUSR | Mode::IRGRP | Mode::IXGRP | Mode::IROTH | Mode::IXOTH }; } + virtual nlink_t nlink() const override { return 1; } + virtual uid_t uid() const override { return 0; } + virtual gid_t gid() const override { return 0; } + virtual off_t size() const override { return 0; } + virtual timespec atime() const override { return { 0, 0 }; } + virtual timespec mtime() const override { return { 0, 0 }; } + virtual timespec ctime() const override { return { 0, 0 }; } + virtual blksize_t blksize() const override { return 0; } + virtual blkcnt_t blocks() const override { return 0; } + virtual dev_t dev() const override { return 0x4900; } + virtual dev_t rdev() const override { return 0x7854; } + + virtual BAN::ErrorOr read(size_t, void*, size_t) { ASSERT_NOT_REACHED(); } + virtual BAN::ErrorOr create_file(BAN::StringView, mode_t) { ASSERT_NOT_REACHED(); } + }; + +} \ No newline at end of file diff --git a/kernel/include/kernel/FS/Inode.h b/kernel/include/kernel/FS/Inode.h index eb2e23f3..2ef0721d 100644 --- a/kernel/include/kernel/FS/Inode.h +++ b/kernel/include/kernel/FS/Inode.h @@ -2,6 +2,7 @@ #include #include +#include #include #include diff --git a/kernel/include/kernel/Storage/ATAController.h b/kernel/include/kernel/Storage/ATAController.h index 2496f7b9..75c73945 100644 --- a/kernel/include/kernel/Storage/ATAController.h +++ b/kernel/include/kernel/Storage/ATAController.h @@ -1,6 +1,8 @@ #pragma once +#include #include +#include #include namespace Kernel diff --git a/kernel/kernel/Device.cpp b/kernel/kernel/Device.cpp index 3520ef6b..f82f65e1 100644 --- a/kernel/kernel/Device.cpp +++ b/kernel/kernel/Device.cpp @@ -1,115 +1,12 @@ #include #include -#include -#include -#include #include -#include namespace Kernel { - static DeviceManager* s_instance = nullptr; - Device::Device() : m_create_time({ BAN::to_unix_time(RTC::get_current_time()), 0 }) { } - void DeviceManager::initialize() - { - ASSERT(s_instance == nullptr); - - s_instance = new DeviceManager; - ASSERT(s_instance != nullptr); - - for (const auto& pci_device : PCI::get().devices()) - { - switch (pci_device.class_code()) - { - case 0x01: - { - StorageController* controller = nullptr; - switch (pci_device.subclass()) - { - case 0x01: - if (auto res = ATAController::create(pci_device); res.is_error()) - dprintln("{}", res.error()); - else - controller = res.value(); - break; - default: - dprintln("unsupported storage device (pci subclass {2H})", pci_device.subclass()); - break; - } - - if (controller) - { - s_instance->add_device(controller); - for (auto* device : controller->devices()) - { - s_instance->add_device(device); - for (auto* partition : device->partitions()) - s_instance->add_device(partition); - } - } - break; - } - default: - break; - } - } - - MUST(Process::create_kernel( - [](void*) - { - while (true) - { - DeviceManager::get().update(); - PIT::sleep(1); - } - }, nullptr) - ); - } - - DeviceManager& DeviceManager::get() - { - ASSERT(s_instance); - return *s_instance; - } - - void DeviceManager::update() - { - LockGuard _(m_lock); - for (Device* device : m_devices) - device->update(); - } - - void DeviceManager::add_device(Device* device) - { - LockGuard _(m_lock); - MUST(m_devices.push_back(device)); - } - - BAN::ErrorOr> DeviceManager::read_directory_inode(BAN::StringView name) - { - LockGuard _(m_lock); - for (Device* device : m_devices) - if (device->name() == name) - return BAN::RefPtr(device); - return BAN::Error::from_errno(ENOENT); - } - - BAN::ErrorOr> DeviceManager::read_directory_entries(size_t index) - { - BAN::Vector result; - if (index > 0) - return result; - - LockGuard _(m_lock); - TRY(result.reserve(m_devices.size())); - for (Device* device : m_devices) - TRY(result.emplace_back(device->name())); - return result; - } - } \ No newline at end of file diff --git a/kernel/kernel/DeviceManager.cpp b/kernel/kernel/DeviceManager.cpp new file mode 100644 index 00000000..b5f0d592 --- /dev/null +++ b/kernel/kernel/DeviceManager.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include + +namespace Kernel +{ + + static DeviceManager* s_instance = nullptr; + + void DeviceManager::initialize() + { + ASSERT(s_instance == nullptr); + + s_instance = new DeviceManager; + ASSERT(s_instance != nullptr); + + for (const auto& pci_device : PCI::get().devices()) + { + switch (pci_device.class_code()) + { + case 0x01: + { + StorageController* controller = nullptr; + switch (pci_device.subclass()) + { + case 0x01: + if (auto res = ATAController::create(pci_device); res.is_error()) + dprintln("{}", res.error()); + else + controller = res.value(); + break; + default: + dprintln("unsupported storage device (pci subclass {2H})", pci_device.subclass()); + break; + } + + if (controller) + { + s_instance->add_device(controller); + for (auto* device : controller->devices()) + { + s_instance->add_device(device); + for (auto* partition : device->partitions()) + s_instance->add_device(partition); + } + } + break; + } + default: + break; + } + } + + MUST(Process::create_kernel( + [](void*) + { + while (true) + { + DeviceManager::get().update(); + PIT::sleep(1); + } + }, nullptr) + ); + } + + DeviceManager& DeviceManager::get() + { + ASSERT(s_instance); + return *s_instance; + } + + void DeviceManager::update() + { + LockGuard _(m_lock); + for (Device* device : m_devices) + device->update(); + } + + void DeviceManager::add_device(Device* device) + { + LockGuard _(m_lock); + MUST(m_devices.push_back(device)); + } + + BAN::ErrorOr> DeviceManager::read_directory_inode(BAN::StringView name) + { + LockGuard _(m_lock); + for (Device* device : m_devices) + if (device->name() == name) + return BAN::RefPtr(device); + return BAN::Error::from_errno(ENOENT); + } + + BAN::ErrorOr> DeviceManager::read_directory_entries(size_t index) + { + BAN::Vector result; + if (index > 0) + return result; + + LockGuard _(m_lock); + TRY(result.reserve(m_devices.size())); + for (Device* device : m_devices) + TRY(result.emplace_back(device->name())); + return result; + } + + +} \ No newline at end of file diff --git a/kernel/kernel/FS/VirtualFileSystem.cpp b/kernel/kernel/FS/VirtualFileSystem.cpp index 95ac7ead..0ddb3d9a 100644 --- a/kernel/kernel/FS/VirtualFileSystem.cpp +++ b/kernel/kernel/FS/VirtualFileSystem.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include diff --git a/kernel/kernel/Input/PS2Controller.cpp b/kernel/kernel/Input/PS2Controller.cpp index a9d7fb4f..a0460865 100644 --- a/kernel/kernel/Input/PS2Controller.cpp +++ b/kernel/kernel/Input/PS2Controller.cpp @@ -1,6 +1,7 @@ -#include #include +#include #include +#include #include #include #include diff --git a/kernel/kernel/kernel.cpp b/kernel/kernel/kernel.cpp index c8044dee..f30e03a4 100644 --- a/kernel/kernel/kernel.cpp +++ b/kernel/kernel/kernel.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include