Kernel: Start making device numbers unique for each device

This commit is contained in:
Bananymous 2024-02-22 15:53:48 +02:00
parent 7a4ec7f7a3
commit 9594ee8e47
22 changed files with 92 additions and 57 deletions

View File

@ -0,0 +1,24 @@
#pragma once
#include <sys/types.h>
namespace Kernel
{
enum class DeviceNumber : dev_t
{
Framebuffer = 1,
TTY,
Serial,
Null,
Zero,
Debug,
Input,
SCSI,
NVMeController,
NVMeNamespace,
Ethernet,
TmpFS,
};
}

View File

@ -19,7 +19,6 @@ namespace Kernel
void add_device(BAN::RefPtr<Device>); void add_device(BAN::RefPtr<Device>);
void add_inode(BAN::StringView path, BAN::RefPtr<TmpInode>); void add_inode(BAN::StringView path, BAN::RefPtr<TmpInode>);
dev_t get_next_dev() const;
int get_next_input_device() const; int get_next_input_device() const;
void initiate_sync(bool should_block); void initiate_sync(bool should_block);

View File

@ -49,6 +49,8 @@ namespace Kernel
virtual BAN::RefPtr<Inode> root_inode() override { return m_root_inode; } virtual BAN::RefPtr<Inode> root_inode() override { return m_root_inode; }
virtual dev_t dev() const override { return m_block_device->rdev(); };
private: private:
Ext2FS(BAN::RefPtr<BlockDevice> block_device) Ext2FS(BAN::RefPtr<BlockDevice> block_device)
: m_block_device(block_device) : m_block_device(block_device)

View File

@ -10,6 +10,8 @@ namespace Kernel
public: public:
virtual ~FileSystem() {} virtual ~FileSystem() {}
virtual BAN::RefPtr<Inode> root_inode() = 0; virtual BAN::RefPtr<Inode> root_inode() = 0;
virtual dev_t dev() const = 0;
}; };
} }

View File

@ -46,6 +46,8 @@ namespace Kernel
virtual BAN::RefPtr<Inode> root_inode() override { return m_root_inode; } virtual BAN::RefPtr<Inode> root_inode() override { return m_root_inode; }
virtual dev_t dev() const override { return m_rdev; }
BAN::ErrorOr<BAN::RefPtr<TmpInode>> open_inode(ino_t ino); BAN::ErrorOr<BAN::RefPtr<TmpInode>> open_inode(ino_t ino);
BAN::ErrorOr<void> add_to_cache(BAN::RefPtr<TmpInode>); BAN::ErrorOr<void> add_to_cache(BAN::RefPtr<TmpInode>);
@ -115,6 +117,8 @@ namespace Kernel
paddr_t find_indirect(PageInfo root, size_t index, size_t depth); paddr_t find_indirect(PageInfo root, size_t index, size_t depth);
private: private:
const dev_t m_rdev;
RecursiveSpinLock m_lock; RecursiveSpinLock m_lock;
BAN::HashMap<ino_t, BAN::RefPtr<TmpInode>> m_inode_cache; BAN::HashMap<ino_t, BAN::RefPtr<TmpInode>> m_inode_cache;

View File

@ -35,8 +35,8 @@ namespace Kernel
virtual timespec ctime() const override { return m_inode_info.ctime; } virtual timespec ctime() const override { return m_inode_info.ctime; }
virtual blksize_t blksize() const override { return PAGE_SIZE; } virtual blksize_t blksize() const override { return PAGE_SIZE; }
virtual blkcnt_t blocks() const override { return m_inode_info.blocks; } virtual blkcnt_t blocks() const override { return m_inode_info.blocks; }
virtual dev_t dev() const override { return 0; } // TODO virtual dev_t dev() const override;
virtual dev_t rdev() const override { return 0; } // TODO virtual dev_t rdev() const override { return 0; }
public: public:
static BAN::ErrorOr<BAN::RefPtr<TmpInode>> create_from_existing(TmpFileSystem&, ino_t, const TmpInodeInfo&); static BAN::ErrorOr<BAN::RefPtr<TmpInode>> create_from_existing(TmpFileSystem&, ino_t, const TmpInodeInfo&);

View File

@ -17,6 +17,9 @@ namespace Kernel
virtual BAN::RefPtr<Inode> root_inode() override { return m_root_fs->root_inode(); } virtual BAN::RefPtr<Inode> root_inode() override { return m_root_fs->root_inode(); }
// FIXME:
virtual dev_t dev() const override { return 0; }
BAN::ErrorOr<void> mount(const Credentials&, BAN::StringView, BAN::StringView); BAN::ErrorOr<void> mount(const Credentials&, BAN::StringView, BAN::StringView);
BAN::ErrorOr<void> mount(const Credentials&, FileSystem*, BAN::StringView); BAN::ErrorOr<void> mount(const Credentials&, FileSystem*, BAN::StringView);

View File

@ -9,7 +9,6 @@ namespace Kernel::Input
class PS2Device : public CharacterDevice, public Interruptable class PS2Device : public CharacterDevice, public Interruptable
{ {
public: public:
PS2Device(PS2Controller&);
virtual ~PS2Device() {} virtual ~PS2Device() {}
virtual void send_initialize() = 0; virtual void send_initialize() = 0;
@ -27,9 +26,12 @@ namespace Kernel::Input
virtual void update() final override { m_controller.update_command_queue(); } virtual void update() final override { m_controller.update_command_queue(); }
protected:
PS2Device(PS2Controller&);
private: private:
const BAN::String m_name;
const dev_t m_rdev; const dev_t m_rdev;
const BAN::String m_name;
PS2Controller& m_controller; PS2Controller& m_controller;
}; };

View File

@ -1,14 +1,17 @@
#include <kernel/Device/DebugDevice.h> #include <kernel/Device/DebugDevice.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/Device/DeviceNumbers.h>
#include <kernel/Process.h> #include <kernel/Process.h>
#include <kernel/Timer/Timer.h> #include <kernel/Timer/Timer.h>
#include <sys/sysmacros.h>
namespace Kernel namespace Kernel
{ {
BAN::ErrorOr<BAN::RefPtr<DebugDevice>> DebugDevice::create(mode_t mode, uid_t uid, gid_t gid) BAN::ErrorOr<BAN::RefPtr<DebugDevice>> DebugDevice::create(mode_t mode, uid_t uid, gid_t gid)
{ {
auto* result = new DebugDevice(mode, uid, gid, DevFileSystem::get().get_next_dev()); static uint32_t minor = 0;
auto* result = new DebugDevice(mode, uid, gid, makedev(DeviceNumber::Debug, minor++));
if (result == nullptr) if (result == nullptr)
return BAN::Error::from_errno(ENOMEM); return BAN::Error::from_errno(ENOMEM);
return BAN::RefPtr<DebugDevice>::adopt(result); return BAN::RefPtr<DebugDevice>::adopt(result);

View File

@ -1,10 +1,12 @@
#include <kernel/BootInfo.h> #include <kernel/BootInfo.h>
#include <kernel/Device/DeviceNumbers.h>
#include <kernel/Device/FramebufferDevice.h> #include <kernel/Device/FramebufferDevice.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/Memory/Heap.h> #include <kernel/Memory/Heap.h>
#include <sys/framebuffer.h> #include <sys/framebuffer.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/sysmacros.h>
namespace Kernel namespace Kernel
{ {
@ -23,7 +25,7 @@ namespace Kernel
return BAN::Error::from_errno(ENOTSUP); return BAN::Error::from_errno(ENOTSUP);
auto* device_ptr = new FramebufferDevice( auto* device_ptr = new FramebufferDevice(
0660, 0, 900, 0660, 0, 900,
DevFileSystem::get().get_next_dev(), makedev(DeviceNumber::Framebuffer, get_framebuffer_device_index()),
g_boot_info.framebuffer.address, g_boot_info.framebuffer.address,
g_boot_info.framebuffer.width, g_boot_info.framebuffer.width,
g_boot_info.framebuffer.height, g_boot_info.framebuffer.height,
@ -39,7 +41,7 @@ namespace Kernel
FramebufferDevice::FramebufferDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev, paddr_t paddr, uint32_t width, uint32_t height, uint32_t pitch, uint8_t bpp) FramebufferDevice::FramebufferDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev, paddr_t paddr, uint32_t width, uint32_t height, uint32_t pitch, uint8_t bpp)
: CharacterDevice(mode, uid, gid) : CharacterDevice(mode, uid, gid)
, m_name(BAN::String::formatted("fb{}", get_framebuffer_device_index())) , m_name(BAN::String::formatted("fb{}", minor(rdev)))
, m_rdev(rdev) , m_rdev(rdev)
, m_video_memory_paddr(paddr) , m_video_memory_paddr(paddr)
, m_width(width) , m_width(width)

View File

@ -1,12 +1,15 @@
#include <kernel/Device/DeviceNumbers.h>
#include <kernel/Device/NullDevice.h> #include <kernel/Device/NullDevice.h>
#include <kernel/FS/DevFS/FileSystem.h>
#include <sys/sysmacros.h>
namespace Kernel namespace Kernel
{ {
BAN::ErrorOr<BAN::RefPtr<NullDevice>> NullDevice::create(mode_t mode, uid_t uid, gid_t gid) BAN::ErrorOr<BAN::RefPtr<NullDevice>> NullDevice::create(mode_t mode, uid_t uid, gid_t gid)
{ {
auto* result = new NullDevice(mode, uid, gid, DevFileSystem::get().get_next_dev()); static uint32_t minor = 0;
auto* result = new NullDevice(mode, uid, gid, makedev(DeviceNumber::Null, minor++));
if (result == nullptr) if (result == nullptr)
return BAN::Error::from_errno(ENOMEM); return BAN::Error::from_errno(ENOMEM);
return BAN::RefPtr<NullDevice>::adopt(result); return BAN::RefPtr<NullDevice>::adopt(result);

View File

@ -1,12 +1,15 @@
#include <kernel/Device/DeviceNumbers.h>
#include <kernel/Device/ZeroDevice.h> #include <kernel/Device/ZeroDevice.h>
#include <kernel/FS/DevFS/FileSystem.h>
#include <sys/sysmacros.h>
namespace Kernel namespace Kernel
{ {
BAN::ErrorOr<BAN::RefPtr<ZeroDevice>> ZeroDevice::create(mode_t mode, uid_t uid, gid_t gid) BAN::ErrorOr<BAN::RefPtr<ZeroDevice>> ZeroDevice::create(mode_t mode, uid_t uid, gid_t gid)
{ {
auto* result = new ZeroDevice(mode, uid, gid, DevFileSystem::get().get_next_dev()); static uint32_t minor = 0;
auto* result = new ZeroDevice(mode, uid, gid, makedev(DeviceNumber::Zero, minor++));
if (result == nullptr) if (result == nullptr)
return BAN::Error::from_errno(ENOMEM); return BAN::Error::from_errno(ENOMEM);
return BAN::RefPtr<ZeroDevice>::adopt(result); return BAN::RefPtr<ZeroDevice>::adopt(result);

View File

@ -122,17 +122,9 @@ namespace Kernel
MUST(static_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*inode, path)); MUST(static_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*inode, path));
} }
dev_t DevFileSystem::get_next_dev() const
{
LockGuard _(m_device_lock);
static dev_t next_dev = 1;
return next_dev++;
}
int DevFileSystem::get_next_input_device() const int DevFileSystem::get_next_input_device() const
{ {
LockGuard _(m_device_lock); static BAN::Atomic<dev_t> next_dev = 0;
static dev_t next_dev = 0;
return next_dev++; return next_dev++;
} }

View File

@ -1,9 +1,18 @@
#include <kernel/Device/DeviceNumbers.h>
#include <kernel/FS/TmpFS/FileSystem.h> #include <kernel/FS/TmpFS/FileSystem.h>
#include <kernel/Memory/Heap.h> #include <kernel/Memory/Heap.h>
#include <sys/sysmacros.h>
namespace Kernel namespace Kernel
{ {
static dev_t get_next_rdev()
{
static dev_t minor = 0;
return makedev(DeviceNumber::TmpFS, minor++);
}
BAN::ErrorOr<TmpFileSystem*> TmpFileSystem::create(size_t max_pages, mode_t mode, uid_t uid, gid_t gid) BAN::ErrorOr<TmpFileSystem*> TmpFileSystem::create(size_t max_pages, mode_t mode, uid_t uid, gid_t gid)
{ {
if (max_pages < 2) if (max_pages < 2)
@ -17,7 +26,8 @@ namespace Kernel
} }
TmpFileSystem::TmpFileSystem(size_t max_pages) TmpFileSystem::TmpFileSystem(size_t max_pages)
: m_max_pages(max_pages) : m_rdev(get_next_rdev())
, m_max_pages(max_pages)
{ } { }
BAN::ErrorOr<void> TmpFileSystem::initialize(mode_t mode, uid_t uid, gid_t gid) BAN::ErrorOr<void> TmpFileSystem::initialize(mode_t mode, uid_t uid, gid_t gid)

View File

@ -41,6 +41,11 @@ namespace Kernel
/* GENERAL INODE */ /* GENERAL INODE */
dev_t TmpInode::dev() const
{
return m_fs.dev();
}
BAN::ErrorOr<BAN::RefPtr<TmpInode>> TmpInode::create_from_existing(TmpFileSystem& fs, ino_t ino, const TmpInodeInfo& info) BAN::ErrorOr<BAN::RefPtr<TmpInode>> TmpInode::create_from_existing(TmpFileSystem& fs, ino_t ino, const TmpInodeInfo& info)
{ {
TmpInode* inode_ptr = nullptr; TmpInode* inode_ptr = nullptr;

View File

@ -1,3 +1,4 @@
#include <kernel/Device/DeviceNumbers.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/Input/PS2/Config.h> #include <kernel/Input/PS2/Config.h>
#include <kernel/Input/PS2/Device.h> #include <kernel/Input/PS2/Device.h>
@ -10,8 +11,8 @@ namespace Kernel::Input
PS2Device::PS2Device(PS2Controller& controller) PS2Device::PS2Device(PS2Controller& controller)
: CharacterDevice(0440, 0, 901) : CharacterDevice(0440, 0, 901)
, m_name(BAN::String::formatted("input{}", DevFileSystem::get().get_next_input_device())) , m_rdev(makedev(DeviceNumber::Input, DevFileSystem::get().get_next_input_device()))
, m_rdev(makedev(DevFileSystem::get().get_next_dev(), 0)) , m_name(BAN::String::formatted("input{}", minor(m_rdev)))
, m_controller(controller) , m_controller(controller)
{ } { }

View File

@ -1,4 +1,5 @@
#include <BAN/Endianness.h> #include <BAN/Endianness.h>
#include <kernel/Device/DeviceNumbers.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/Networking/NetworkInterface.h> #include <kernel/Networking/NetworkInterface.h>
@ -8,12 +9,6 @@
namespace Kernel namespace Kernel
{ {
static dev_t get_network_rdev_major()
{
static dev_t major = DevFileSystem::get().get_next_dev();
return major;
}
static dev_t get_network_rdev_minor() static dev_t get_network_rdev_minor()
{ {
static dev_t minor = 0; static dev_t minor = 0;
@ -23,7 +18,7 @@ namespace Kernel
NetworkInterface::NetworkInterface() NetworkInterface::NetworkInterface()
: CharacterDevice(0400, 0, 0) : CharacterDevice(0400, 0, 0)
, m_type(Type::Ethernet) , m_type(Type::Ethernet)
, m_rdev(makedev(get_network_rdev_major(), get_network_rdev_minor())) , m_rdev(makedev(DeviceNumber::Ethernet, get_network_rdev_minor()))
{ {
ASSERT(minor(m_rdev) < 10); ASSERT(minor(m_rdev) < 10);
ASSERT(m_type == Type::Ethernet); ASSERT(m_type == Type::Ethernet);

View File

@ -1,3 +1,4 @@
#include <kernel/Device/DeviceNumbers.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/IO.h> #include <kernel/IO.h>
#include <kernel/Storage/ATA/ATABus.h> #include <kernel/Storage/ATA/ATABus.h>
@ -9,12 +10,6 @@
namespace Kernel namespace Kernel
{ {
static dev_t get_ata_dev_major()
{
static dev_t major = DevFileSystem::get().get_next_dev();
return major;
}
static dev_t get_ata_dev_minor() static dev_t get_ata_dev_minor()
{ {
static dev_t minor = 0; static dev_t minor = 0;
@ -22,7 +17,7 @@ namespace Kernel
} }
detail::ATABaseDevice::ATABaseDevice() detail::ATABaseDevice::ATABaseDevice()
: m_rdev(makedev(get_ata_dev_major(), get_ata_dev_minor())) : m_rdev(makedev(DeviceNumber::SCSI, get_ata_dev_minor()))
{ {
strcpy(m_name, "sda"); strcpy(m_name, "sda");
m_name[2] += minor(m_rdev); m_name[2] += minor(m_rdev);

View File

@ -1,4 +1,5 @@
#include <BAN/Array.h> #include <BAN/Array.h>
#include <kernel/Device/DeviceNumbers.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/Memory/DMARegion.h> #include <kernel/Memory/DMARegion.h>
#include <kernel/Storage/NVMe/Controller.h> #include <kernel/Storage/NVMe/Controller.h>
@ -11,12 +12,6 @@
namespace Kernel namespace Kernel
{ {
static dev_t get_ctrl_dev_major()
{
static dev_t major = DevFileSystem::get().get_next_dev();
return major;
}
static dev_t get_ctrl_dev_minor() static dev_t get_ctrl_dev_minor()
{ {
static dev_t minor = 0; static dev_t minor = 0;
@ -36,7 +31,7 @@ namespace Kernel
NVMeController::NVMeController(PCI::Device& pci_device) NVMeController::NVMeController(PCI::Device& pci_device)
: CharacterDevice(0600, 0, 0) : CharacterDevice(0600, 0, 0)
, m_pci_device(pci_device) , m_pci_device(pci_device)
, m_rdev(makedev(get_ctrl_dev_major(), get_ctrl_dev_minor())) , m_rdev(makedev(DeviceNumber::NVMeController, get_ctrl_dev_minor()))
{ {
ASSERT(minor(m_rdev) < 10); ASSERT(minor(m_rdev) < 10);
strcpy(m_name, "nvmeX"); strcpy(m_name, "nvmeX");

View File

@ -1,3 +1,4 @@
#include <kernel/Device/DeviceNumbers.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/Storage/NVMe/Controller.h> #include <kernel/Storage/NVMe/Controller.h>
#include <kernel/Storage/NVMe/Namespace.h> #include <kernel/Storage/NVMe/Namespace.h>
@ -7,12 +8,6 @@
namespace Kernel namespace Kernel
{ {
static dev_t get_ns_dev_major()
{
static dev_t major = DevFileSystem::get().get_next_dev();
return major;
}
static dev_t get_ns_dev_minor() static dev_t get_ns_dev_minor()
{ {
static dev_t minor = 0; static dev_t minor = 0;
@ -34,7 +29,7 @@ namespace Kernel
, m_nsid(nsid) , m_nsid(nsid)
, m_block_size(block_size) , m_block_size(block_size)
, m_block_count(block_count) , m_block_count(block_count)
, m_rdev(makedev(get_ns_dev_major(), get_ns_dev_minor())) , m_rdev(makedev(DeviceNumber::NVMeNamespace, get_ns_dev_minor()))
{ {
ASSERT(minor(m_rdev) < 10); ASSERT(minor(m_rdev) < 10);
ASSERT(m_controller.name().size() + 2 < sizeof(m_name)); ASSERT(m_controller.name().size() + 2 < sizeof(m_name));

View File

@ -1,5 +1,6 @@
#include <BAN/Array.h> #include <BAN/Array.h>
#include <kernel/CriticalScope.h> #include <kernel/CriticalScope.h>
#include <kernel/Device/DeviceNumbers.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/IDT.h> #include <kernel/IDT.h>
#include <kernel/InterruptController.h> #include <kernel/InterruptController.h>
@ -42,9 +43,8 @@ namespace Kernel
static dev_t next_rdev() static dev_t next_rdev()
{ {
static dev_t major = DevFileSystem::get().get_next_dev();
static dev_t minor = 0; static dev_t minor = 0;
return makedev(major, minor++); return makedev(DeviceNumber::Serial, minor++);
} }
void Serial::initialize() void Serial::initialize()

View File

@ -2,6 +2,7 @@
#include <BAN/ScopeGuard.h> #include <BAN/ScopeGuard.h>
#include <BAN/UTF8.h> #include <BAN/UTF8.h>
#include <kernel/Debug.h> #include <kernel/Debug.h>
#include <kernel/Device/DeviceNumbers.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/LockGuard.h> #include <kernel/LockGuard.h>
#include <kernel/Process.h> #include <kernel/Process.h>
@ -26,9 +27,8 @@ namespace Kernel
static dev_t next_rdev() static dev_t next_rdev()
{ {
static dev_t major = DevFileSystem::get().get_next_dev();
static dev_t minor = 0; static dev_t minor = 0;
return makedev(major, minor++); return makedev(DeviceNumber::TTY, minor++);
} }
BAN::ErrorOr<BAN::RefPtr<VirtualTTY>> VirtualTTY::create(TerminalDriver* driver) BAN::ErrorOr<BAN::RefPtr<VirtualTTY>> VirtualTTY::create(TerminalDriver* driver)