Kernel: Device dev and rdev number is done more properly

Also hd* partitions are now 1 indexed instead of 0
This commit is contained in:
Bananymous 2023-04-03 11:43:16 +03:00
parent 914f718767
commit 461a5774f8
15 changed files with 100 additions and 78 deletions

View File

@ -1,15 +1,15 @@
menuentry "banan-os" { menuentry "banan-os" {
multiboot /boot/banan-os.kernel root=/dev/hda1 multiboot /boot/banan-os.kernel root=/dev/hda2
} }
menuentry "banan-os (no serial)" { menuentry "banan-os (no serial)" {
multiboot /boot/banan-os.kernel root=/dev/hda1 noserial multiboot /boot/banan-os.kernel root=/dev/hda2 noserial
} }
menuentry "banan-os (no apic)" { menuentry "banan-os (no apic)" {
multiboot /boot/banan-os.kernel root=/dev/hda1 noapic multiboot /boot/banan-os.kernel root=/dev/hda2 noapic
} }
menuentry "banan-os (no apic, no serial)" { menuentry "banan-os (no apic, no serial)" {
multiboot /boot/banan-os.kernel root=/dev/hda1 noapic noserial multiboot /boot/banan-os.kernel root=/dev/hda2 noapic noserial
} }

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <kernel/FS/Inode.h> #include <kernel/DeviceManager.h>
namespace Kernel namespace Kernel
{ {
@ -23,12 +23,28 @@ namespace Kernel
virtual InodeType inode_type() const override { return InodeType::Device; } virtual InodeType inode_type() const override { return InodeType::Device; }
virtual ino_t ino() const override { return m_ino_t; }
virtual nlink_t nlink() const override { return 1; }
virtual off_t size() const override { return 0; }
virtual timespec atime() const override { return m_create_time; } virtual timespec atime() const override { return m_create_time; }
virtual timespec mtime() const override { return m_create_time; } virtual timespec mtime() const override { return m_create_time; }
virtual timespec ctime() const override { return m_create_time; } virtual timespec ctime() const override { return m_create_time; }
virtual blksize_t blksize() const override { return DeviceManager::get().blksize(); }
virtual blkcnt_t blocks() const override { return DeviceManager::get().blocks(); }
virtual dev_t dev() const override { return DeviceManager::get().dev(); }
/*
a device has to overload
virtual Mode mode() const;
virtual uid_t uid() const;
virtual gid_t gid() const;
virutal dev_t rdev() const;
virtual BAN::StringView name() const;
*/
private: private:
timespec m_create_time; const timespec m_create_time;
const ino_t m_ino_t;
}; };
class BlockDevice : public Device class BlockDevice : public Device

View File

@ -2,12 +2,15 @@
#include <BAN/StringView.h> #include <BAN/StringView.h>
#include <kernel/FS/FileSystem.h> #include <kernel/FS/FileSystem.h>
#include <kernel/Device.h>
#include <kernel/SpinLock.h> #include <kernel/SpinLock.h>
#include <sys/sysmacros.h>
namespace Kernel namespace Kernel
{ {
class Device;
class DeviceManager final : public FileSystem, public Inode class DeviceManager final : public FileSystem, public Inode
{ {
BAN_NON_COPYABLE(DeviceManager); BAN_NON_COPYABLE(DeviceManager);
@ -17,6 +20,10 @@ namespace Kernel
static void initialize(); static void initialize();
static DeviceManager& get(); static DeviceManager& get();
ino_t get_next_ino() const;
dev_t get_next_rdev() const;
uint8_t get_next_input_dev() const;
void update(); void update();
void add_device(Device*); void add_device(Device*);
@ -48,13 +55,18 @@ namespace Kernel
virtual timespec atime() const override { return { 0, 0 }; } virtual timespec atime() const override { return { 0, 0 }; }
virtual timespec mtime() const override { return { 0, 0 }; } virtual timespec mtime() const override { return { 0, 0 }; }
virtual timespec ctime() const override { return { 0, 0 }; } virtual timespec ctime() const override { return { 0, 0 }; }
virtual blksize_t blksize() const override { return 0; } virtual blksize_t blksize() const override { ASSERT(m_blksize); return m_blksize; }
virtual blkcnt_t blocks() const override { return 0; } virtual blkcnt_t blocks() const override { return 0; }
virtual dev_t dev() const override { return 0x4900; } virtual dev_t dev() const override { return makedev(0, 5); }
virtual dev_t rdev() const override { return 0x7854; } virtual dev_t rdev() const override { return 0; }
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) { ASSERT_NOT_REACHED(); } virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) { return BAN::Error::from_errno(EISDIR); }
virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) { ASSERT_NOT_REACHED(); } virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) { return BAN::Error::from_errno(ENOTSUP); }
void set_blksize(blksize_t blksize) { m_blksize = blksize; }
private:
blksize_t m_blksize = 0;
}; };
} }

View File

@ -8,23 +8,13 @@ namespace Kernel::Input
class PS2Device : public CharacterDevice class PS2Device : public CharacterDevice
{ {
public: public:
PS2Device(dev_t);
virtual ~PS2Device() {} virtual ~PS2Device() {}
virtual void on_byte(uint8_t) = 0; virtual void on_byte(uint8_t) = 0;
public: public:
virtual ino_t ino() const override { return m_ino; }
virtual Mode mode() const override { return { Mode::IFCHR | Mode::IRUSR | Mode::IRGRP }; } virtual Mode mode() const override { return { Mode::IFCHR | Mode::IRUSR | Mode::IRGRP }; }
virtual nlink_t nlink() const override { return 1; }
virtual uid_t uid() const override { return 0; } virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; } virtual gid_t gid() const override { return 0; }
virtual off_t size() const override { return 0; }
virtual blkcnt_t blocks() const override { return 0; }
virtual dev_t dev() const override { return m_dev; }
private:
ino_t m_ino;
dev_t m_dev;
}; };
class PS2Controller class PS2Controller

View File

@ -27,17 +27,13 @@ namespace Kernel::Input
}; };
public: public:
static BAN::ErrorOr<PS2Keyboard*> create(PS2Controller&, dev_t); static BAN::ErrorOr<PS2Keyboard*> create(PS2Controller&);
virtual void on_byte(uint8_t) override; virtual void on_byte(uint8_t) override;
virtual void update() override; virtual void update() override;
private: private:
PS2Keyboard(PS2Controller& controller, dev_t device) PS2Keyboard(PS2Controller& controller);
: PS2Device(device)
, m_controller(controller)
, m_name(BAN::String::formatted("input{}", device))
{}
BAN::ErrorOr<void> initialize(); BAN::ErrorOr<void> initialize();
void append_command_queue(uint8_t); void append_command_queue(uint8_t);
@ -63,13 +59,15 @@ namespace Kernel::Input
Semaphore m_semaphore; Semaphore m_semaphore;
BAN::String m_name;
public: public:
virtual BAN::StringView name() const override { return m_name; } virtual BAN::StringView name() const override { return m_name; }
virtual blksize_t blksize() const override { return sizeof(KeyEvent); } virtual dev_t rdev() const override { return m_rdev; }
virtual dev_t rdev() const override { return 0x8594; }
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override; virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override;
private:
const BAN::String m_name;
const dev_t m_rdev;
}; };
} }

View File

@ -20,7 +20,9 @@ namespace Kernel
uint8_t next_device_index() const; uint8_t next_device_index() const;
private: private:
ATAController() = default; ATAController()
: m_rdev(makedev(DeviceManager::get().get_next_rdev(), 0))
{ }
BAN::ErrorOr<void> initialize(const PCIDevice& device); BAN::ErrorOr<void> initialize(const PCIDevice& device);
private: private:
@ -28,20 +30,17 @@ namespace Kernel
friend class ATABus; friend class ATABus;
public: public:
virtual ino_t ino() const override { return 0; }
virtual Mode mode() const override { return { Mode::IFCHR }; } virtual Mode mode() const override { return { Mode::IFCHR }; }
virtual nlink_t nlink() const override { return 1; }
virtual uid_t uid() const override { return 0; } virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; } virtual gid_t gid() const override { return 0; }
virtual off_t size() const override { return 0; } virtual dev_t rdev() const override { return m_rdev; }
virtual blksize_t blksize() const override { return 0; }
virtual blkcnt_t blocks() const override { return 0; }
virtual dev_t dev() const override { return DeviceManager::get().dev(); }
virtual dev_t rdev() const override { return 0x8594; }
virtual BAN::StringView name() const override { return "hd"sv; } virtual BAN::StringView name() const override { return "hd"sv; }
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) { return BAN::Error::from_errno(ENOTSUP); } virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) { return BAN::Error::from_errno(ENOTSUP); }
private:
const dev_t m_rdev;
}; };
} }

View File

@ -9,7 +9,10 @@ namespace Kernel
class ATADevice final : public StorageDevice class ATADevice final : public StorageDevice
{ {
public: public:
ATADevice(ATABus* bus) : m_bus(bus) { } ATADevice(ATABus* bus)
: m_bus(bus)
, m_rdev(makedev(DeviceManager::get().get_next_rdev(), 0))
{ }
BAN::ErrorOr<void> initialize(ATABus::DeviceType, const uint16_t*); BAN::ErrorOr<void> initialize(ATABus::DeviceType, const uint16_t*);
virtual BAN::ErrorOr<void> read_sectors(uint64_t, uint8_t, uint8_t*) override; virtual BAN::ErrorOr<void> read_sectors(uint64_t, uint8_t, uint8_t*) override;
@ -34,22 +37,17 @@ namespace Kernel
friend class ATABus; friend class ATABus;
public: public:
virtual ino_t ino() const override { return m_index; } virtual Mode mode() const override { return { Mode::IFBLK | Mode::IRUSR | Mode::IRGRP }; }
virtual Mode mode() const override { return { Mode::IFBLK }; }
virtual nlink_t nlink() const override { return 1; }
virtual uid_t uid() const override { return 0; } virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; } virtual gid_t gid() const override { return 0; }
virtual off_t size() const override { return 0; } virtual dev_t rdev() const override { return m_rdev; }
virtual blksize_t blksize() const override { return sector_size(); }
virtual blkcnt_t blocks() const override { return 0; }
virtual dev_t dev() const override { return m_bus->controller()->dev(); }
virtual dev_t rdev() const override { return 0x5429; }
virtual BAN::StringView name() const override { return BAN::StringView(m_device_name, sizeof(m_device_name) - 1); } virtual BAN::StringView name() const override { return BAN::StringView(m_device_name, sizeof(m_device_name) - 1); }
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override; virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override;
public: public:
const dev_t m_rdev;
char m_device_name[4] {}; char m_device_name[4] {};
}; };

View File

@ -44,16 +44,10 @@ namespace Kernel
public: public:
virtual DeviceType device_type() const override { return DeviceType::Partition; } virtual DeviceType device_type() const override { return DeviceType::Partition; }
virtual ino_t ino() const override { return m_index; }
virtual Mode mode() const override { return { Mode::IFBLK | Mode::IRUSR | Mode::IRGRP }; } virtual Mode mode() const override { return { Mode::IFBLK | Mode::IRUSR | Mode::IRGRP }; }
virtual nlink_t nlink() const override { return 1; }
virtual uid_t uid() const override { return 0; } virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; } virtual gid_t gid() const override { return 0; }
virtual off_t size() const override { return 0; } virtual dev_t rdev() const override;
virtual blksize_t blksize() const;
virtual blkcnt_t blocks() const override { return 0; }
virtual dev_t dev() const override;
virtual dev_t rdev() const { return 0x7459; }
virtual BAN::StringView name() const override { return m_device_name; } virtual BAN::StringView name() const override { return m_device_name; }

View File

@ -7,6 +7,7 @@ namespace Kernel
Device::Device() Device::Device()
: m_create_time({ BAN::to_unix_time(RTC::get_current_time()), 0 }) : m_create_time({ BAN::to_unix_time(RTC::get_current_time()), 0 })
, m_ino_t(DeviceManager::get().get_next_ino())
{ } { }
} }

View File

@ -76,6 +76,24 @@ namespace Kernel
return *s_instance; return *s_instance;
} }
ino_t DeviceManager::get_next_ino() const
{
static ino_t next_ino = 1;
return next_ino++;
}
dev_t DeviceManager::get_next_rdev() const
{
static dev_t next_dev = 1;
return next_dev++;
}
uint8_t DeviceManager::get_next_input_dev() const
{
static uint8_t next_dev = 0;
return next_dev++;
}
void DeviceManager::update() void DeviceManager::update()
{ {
LockGuard _(m_lock); LockGuard _(m_lock);

View File

@ -25,6 +25,7 @@ namespace Kernel
auto partition_inode = TRY(DeviceManager::get().read_directory_inode(root)); auto partition_inode = TRY(DeviceManager::get().read_directory_inode(root));
s_instance->m_root_fs = TRY(Ext2FS::create(*(Partition*)partition_inode.ptr())); s_instance->m_root_fs = TRY(Ext2FS::create(*(Partition*)partition_inode.ptr()));
DeviceManager::get().set_blksize(s_instance->m_root_fs->root_inode()->blksize());
TRY(s_instance->mount(&DeviceManager::get(), "/dev")); TRY(s_instance->mount(&DeviceManager::get(), "/dev"));
guard.disable(); guard.disable();

View File

@ -302,7 +302,7 @@ namespace Kernel::Input
// MF2 Keyboard // MF2 Keyboard
if (index == 2 && (bytes[0] == 0xAB && bytes[1] == 0x83)) if (index == 2 && (bytes[0] == 0xAB && bytes[1] == 0x83))
m_devices[device] = TRY(PS2Keyboard::create(*this, device)); m_devices[device] = TRY(PS2Keyboard::create(*this));
if (m_devices[device]) if (m_devices[device])
return {}; return {};
@ -332,10 +332,4 @@ namespace Kernel::Input
return {}; return {};
} }
PS2Device::PS2Device(dev_t dev)
{
m_dev = dev;
m_ino = dev & 1;
}
} }

View File

@ -37,9 +37,9 @@ namespace Kernel::Input
} }
BAN::ErrorOr<PS2Keyboard*> PS2Keyboard::create(PS2Controller& controller, dev_t device) BAN::ErrorOr<PS2Keyboard*> PS2Keyboard::create(PS2Controller& controller)
{ {
PS2Keyboard* keyboard = new PS2Keyboard(controller, device); PS2Keyboard* keyboard = new PS2Keyboard(controller);
if (keyboard == nullptr) if (keyboard == nullptr)
return BAN::Error::from_errno(ENOMEM); return BAN::Error::from_errno(ENOMEM);
BAN::ScopeGuard guard([keyboard] { delete keyboard; }); BAN::ScopeGuard guard([keyboard] { delete keyboard; });
@ -48,6 +48,12 @@ namespace Kernel::Input
return keyboard; return keyboard;
} }
PS2Keyboard::PS2Keyboard(PS2Controller& controller)
: m_controller(controller)
, m_name(BAN::String::formatted("input{}", DeviceManager::get().get_next_input_dev()))
, m_rdev(makedev(DeviceManager::get().get_next_rdev(), 0))
{ }
void PS2Keyboard::on_byte(uint8_t byte) void PS2Keyboard::on_byte(uint8_t byte)
{ {
// NOTE: This implementation does not allow using commands // NOTE: This implementation does not allow using commands

View File

@ -455,9 +455,9 @@ argument_done:
TTY_PRINTLN(" File: {}", arguments[1]); TTY_PRINTLN(" File: {}", arguments[1]);
TTY_PRINTLN(" Size: {}\tBlocks: {}\tIO Block: {}\t {}", st.st_size, st.st_blocks, st.st_blksize, type); TTY_PRINTLN(" Size: {}\tBlocks: {}\tIO Block: {}\t {}", st.st_size, st.st_blocks, st.st_blksize, type);
TTY_PRINT("Device: {},{}\tInode: {}\tLinks: {}", st.st_dev >> 8, st.st_dev & 0xFF, st.st_ino, st.st_nlink); TTY_PRINT("Device: {},{}\tInode: {}\tLinks: {}", major(st.st_dev), minor(st.st_dev), st.st_ino, st.st_nlink);
if (st.st_rdev) if (st.st_rdev)
TTY_PRINT("\tDevice type: {},{}", st.st_rdev >> 8, st.st_rdev & 0xFF); TTY_PRINT("\tDevice type: {},{}", major(st.st_rdev), minor(st.st_rdev));
TTY_PRINTLN(""); TTY_PRINTLN("");
TTY_PRINTLN("Access: ({4O}/{})\tUid: {}\tGid: {}", mode.mode & 0777, mode_string(mode), st.st_uid, st.st_gid); TTY_PRINTLN("Access: ({4O}/{})\tUid: {}\tGid: {}", mode.mode & 0777, mode_string(mode), st.st_uid, st.st_gid);
TTY_PRINTLN("Access: {}", BAN::from_unix_time(st.st_atime)); TTY_PRINTLN("Access: {}", BAN::from_unix_time(st.st_atime));

View File

@ -189,7 +189,7 @@ namespace Kernel
entry.ending_lba, entry.ending_lba,
entry.attributes, entry.attributes,
utf8_name, utf8_name,
i i + 1
); );
ASSERT(partition != nullptr); ASSERT(partition != nullptr);
MUST(m_partitions.push_back(partition)); MUST(m_partitions.push_back(partition));
@ -211,6 +211,11 @@ namespace Kernel
memcpy(m_label, label, sizeof(m_label)); memcpy(m_label, label, sizeof(m_label));
} }
dev_t Partition::rdev() const
{
return makedev(major(m_device.rdev()), m_index);
}
BAN::ErrorOr<void> Partition::read_sectors(uint64_t lba, uint8_t sector_count, uint8_t* buffer) BAN::ErrorOr<void> Partition::read_sectors(uint64_t lba, uint8_t sector_count, uint8_t* buffer)
{ {
const uint32_t sectors_in_partition = m_lba_end - m_lba_start; const uint32_t sectors_in_partition = m_lba_end - m_lba_start;
@ -229,16 +234,6 @@ namespace Kernel
return {}; return {};
} }
blksize_t Partition::blksize() const
{
return m_device.blksize();
}
dev_t Partition::dev() const
{
return m_device.dev();
}
BAN::ErrorOr<size_t> Partition::read(size_t offset, void* buffer, size_t bytes) BAN::ErrorOr<size_t> Partition::read(size_t offset, void* buffer, size_t bytes)
{ {
if (offset % m_device.sector_size() || bytes % m_device.sector_size()) if (offset % m_device.sector_size() || bytes % m_device.sector_size())