diff --git a/LibELF/LibELF/LoadableELF.cpp b/LibELF/LibELF/LoadableELF.cpp index 9ca3a8bb..c5475bef 100644 --- a/LibELF/LibELF/LoadableELF.cpp +++ b/LibELF/LibELF/LoadableELF.cpp @@ -62,7 +62,7 @@ namespace LibELF return BAN::Error::from_errno(ENOEXEC); } - size_t nread = TRY(m_inode->read(0, &m_file_header, sizeof(m_file_header))); + size_t nread = TRY(m_inode->read(0, BAN::ByteSpan::from(m_file_header))); ASSERT(nread == sizeof(m_file_header)); if (m_file_header.e_ident[EI_MAG0] != ELFMAG0 || @@ -113,7 +113,7 @@ namespace LibELF TRY(m_program_headers.resize(m_file_header.e_phnum)); for (size_t i = 0; i < m_file_header.e_phnum; i++) { - TRY(m_inode->read(m_file_header.e_phoff + m_file_header.e_phentsize * i, &m_program_headers[i], m_file_header.e_phentsize)); + TRY(m_inode->read(m_file_header.e_phoff + m_file_header.e_phentsize * i, BAN::ByteSpan::from(m_program_headers[i]))); const auto& pheader = m_program_headers[i]; if (pheader.p_type != PT_NULL && pheader.p_type != PT_LOAD) @@ -242,7 +242,7 @@ namespace LibELF file_offset = vaddr - program_header.p_vaddr; size_t bytes = BAN::Math::min(PAGE_SIZE - vaddr_offset, program_header.p_filesz - file_offset); - TRY(m_inode->read(program_header.p_offset + file_offset, (void*)(vaddr + vaddr_offset), bytes)); + TRY(m_inode->read(program_header.p_offset + file_offset, { (uint8_t*)vaddr + vaddr_offset, bytes })); } return {}; diff --git a/kernel/include/kernel/Device/NullDevice.h b/kernel/include/kernel/Device/NullDevice.h index a5e09932..ec748f92 100644 --- a/kernel/include/kernel/Device/NullDevice.h +++ b/kernel/include/kernel/Device/NullDevice.h @@ -20,8 +20,8 @@ namespace Kernel , m_rdev(rdev) { } - virtual BAN::ErrorOr read_impl(off_t, void*, size_t) override { return 0; } - virtual BAN::ErrorOr write_impl(off_t, const void*, size_t size) override { return size; }; + virtual BAN::ErrorOr read_impl(off_t, BAN::ByteSpan) override { return 0; } + virtual BAN::ErrorOr write_impl(off_t, BAN::ConstByteSpan buffer) override { return buffer.size(); }; private: const dev_t m_rdev; diff --git a/kernel/include/kernel/Device/ZeroDevice.h b/kernel/include/kernel/Device/ZeroDevice.h index 86fb781f..90aa3f8a 100644 --- a/kernel/include/kernel/Device/ZeroDevice.h +++ b/kernel/include/kernel/Device/ZeroDevice.h @@ -18,8 +18,8 @@ namespace Kernel , m_rdev(rdev) { } - virtual BAN::ErrorOr read_impl(off_t, void*, size_t) override; - virtual BAN::ErrorOr write_impl(off_t, const void*, size_t size) override { return size; }; + virtual BAN::ErrorOr read_impl(off_t, BAN::ByteSpan) override; + virtual BAN::ErrorOr write_impl(off_t, BAN::ConstByteSpan buffer) override { return buffer.size(); }; private: const dev_t m_rdev; diff --git a/kernel/include/kernel/FS/Ext2/FileSystem.h b/kernel/include/kernel/FS/Ext2/FileSystem.h index 1d162271..717394c3 100644 --- a/kernel/include/kernel/FS/Ext2/FileSystem.h +++ b/kernel/include/kernel/FS/Ext2/FileSystem.h @@ -33,6 +33,9 @@ namespace Kernel uint8_t* data() { return m_buffer.data(); } const uint8_t* data() const { return m_buffer.data(); } + BAN::ByteSpan span() { return m_buffer; } + BAN::ConstByteSpan span() const { return m_buffer.as_const(); } + uint8_t& operator[](size_t index) { return m_buffer[index]; } uint8_t operator[](size_t index) const { return m_buffer[index]; } diff --git a/kernel/include/kernel/FS/Ext2/Inode.h b/kernel/include/kernel/FS/Ext2/Inode.h index e5cc01fe..dfcd24fd 100644 --- a/kernel/include/kernel/FS/Ext2/Inode.h +++ b/kernel/include/kernel/FS/Ext2/Inode.h @@ -34,8 +34,8 @@ namespace Kernel virtual BAN::ErrorOr link_target_impl() override; - virtual BAN::ErrorOr read_impl(off_t, void*, size_t) override; - virtual BAN::ErrorOr write_impl(off_t, const void*, size_t) override; + virtual BAN::ErrorOr read_impl(off_t, BAN::ByteSpan) override; + virtual BAN::ErrorOr write_impl(off_t, BAN::ConstByteSpan) override; virtual BAN::ErrorOr truncate_impl(size_t) override; private: diff --git a/kernel/include/kernel/FS/Inode.h b/kernel/include/kernel/FS/Inode.h index 9c4991d2..b13486bf 100644 --- a/kernel/include/kernel/FS/Inode.h +++ b/kernel/include/kernel/FS/Inode.h @@ -1,10 +1,11 @@ #pragma once +#include #include #include #include -#include #include +#include #include #include @@ -95,8 +96,8 @@ namespace Kernel BAN::ErrorOr link_target(); // General API - BAN::ErrorOr read(off_t, void*, size_t); - BAN::ErrorOr write(off_t, const void*, size_t); + BAN::ErrorOr read(off_t, BAN::ByteSpan buffer); + BAN::ErrorOr write(off_t, BAN::ConstByteSpan buffer); BAN::ErrorOr truncate(size_t); bool has_data() const; @@ -111,8 +112,8 @@ namespace Kernel virtual BAN::ErrorOr link_target_impl() { return BAN::Error::from_errno(ENOTSUP); } // General API - virtual BAN::ErrorOr read_impl(off_t, void*, size_t) { return BAN::Error::from_errno(ENOTSUP); } - virtual BAN::ErrorOr write_impl(off_t, const void*, size_t) { return BAN::Error::from_errno(ENOTSUP); } + virtual BAN::ErrorOr read_impl(off_t, BAN::ByteSpan) { return BAN::Error::from_errno(ENOTSUP); } + virtual BAN::ErrorOr write_impl(off_t, BAN::ConstByteSpan) { return BAN::Error::from_errno(ENOTSUP); } virtual BAN::ErrorOr truncate_impl(size_t) { return BAN::Error::from_errno(ENOTSUP); } virtual bool has_data_impl() const { dwarnln("nonblock not supported"); return true; } diff --git a/kernel/include/kernel/FS/Pipe.h b/kernel/include/kernel/FS/Pipe.h index 8617c9ec..6d90c572 100644 --- a/kernel/include/kernel/FS/Pipe.h +++ b/kernel/include/kernel/FS/Pipe.h @@ -31,8 +31,8 @@ namespace Kernel virtual dev_t rdev() const override { return 0; } // FIXME protected: - virtual BAN::ErrorOr read_impl(off_t, void*, size_t) override; - virtual BAN::ErrorOr write_impl(off_t, const void*, size_t) override; + virtual BAN::ErrorOr read_impl(off_t, BAN::ByteSpan) override; + virtual BAN::ErrorOr write_impl(off_t, BAN::ConstByteSpan) override; private: Pipe(const Credentials&); diff --git a/kernel/include/kernel/FS/ProcFS/Inode.h b/kernel/include/kernel/FS/ProcFS/Inode.h index 4300721e..628b9c71 100644 --- a/kernel/include/kernel/FS/ProcFS/Inode.h +++ b/kernel/include/kernel/FS/ProcFS/Inode.h @@ -23,23 +23,23 @@ namespace Kernel class ProcROInode final : public RamInode { public: - static BAN::ErrorOr> create(Process&, size_t (Process::*callback)(off_t, void*, size_t) const, RamFileSystem&, mode_t, uid_t, gid_t); + static BAN::ErrorOr> create(Process&, size_t (Process::*callback)(off_t, BAN::ByteSpan) const, RamFileSystem&, mode_t, uid_t, gid_t); ~ProcROInode() = default; protected: - virtual BAN::ErrorOr read_impl(off_t, void*, size_t) override; + virtual BAN::ErrorOr read_impl(off_t, BAN::ByteSpan) override; // You may not write here and this is always non blocking - virtual BAN::ErrorOr write_impl(off_t, const void*, size_t) override { return BAN::Error::from_errno(EINVAL); } + virtual BAN::ErrorOr write_impl(off_t, BAN::ConstByteSpan) override { return BAN::Error::from_errno(EINVAL); } virtual BAN::ErrorOr truncate_impl(size_t) override { return BAN::Error::from_errno(EINVAL); } virtual bool has_data_impl() const override { return true; } private: - ProcROInode(Process&, size_t (Process::*)(off_t, void*, size_t) const, RamFileSystem&, const FullInodeInfo&); + ProcROInode(Process&, size_t (Process::*)(off_t, BAN::ByteSpan) const, RamFileSystem&, const FullInodeInfo&); private: Process& m_process; - size_t (Process::*m_callback)(off_t, void*, size_t) const; + size_t (Process::*m_callback)(off_t, BAN::ByteSpan) const; }; } diff --git a/kernel/include/kernel/FS/RamFS/Inode.h b/kernel/include/kernel/FS/RamFS/Inode.h index c3ac0e0e..75bd4a54 100644 --- a/kernel/include/kernel/FS/RamFS/Inode.h +++ b/kernel/include/kernel/FS/RamFS/Inode.h @@ -70,8 +70,8 @@ namespace Kernel protected: RamFileInode(RamFileSystem&, const FullInodeInfo&); - virtual BAN::ErrorOr read_impl(off_t, void*, size_t) override; - virtual BAN::ErrorOr write_impl(off_t, const void*, size_t) override; + virtual BAN::ErrorOr read_impl(off_t, BAN::ByteSpan) override; + virtual BAN::ErrorOr write_impl(off_t, BAN::ConstByteSpan) override; virtual BAN::ErrorOr truncate_impl(size_t) override; private: diff --git a/kernel/include/kernel/Input/PS2Keyboard.h b/kernel/include/kernel/Input/PS2Keyboard.h index 62e8a57e..4ee18922 100644 --- a/kernel/include/kernel/Input/PS2Keyboard.h +++ b/kernel/include/kernel/Input/PS2Keyboard.h @@ -63,7 +63,7 @@ namespace Kernel::Input virtual dev_t rdev() const override { return m_rdev; } protected: - virtual BAN::ErrorOr read_impl(off_t, void*, size_t) override; + virtual BAN::ErrorOr read_impl(off_t, BAN::ByteSpan) override; private: const dev_t m_rdev; diff --git a/kernel/include/kernel/OpenFileDescriptorSet.h b/kernel/include/kernel/OpenFileDescriptorSet.h index 6996bc96..378b791b 100644 --- a/kernel/include/kernel/OpenFileDescriptorSet.h +++ b/kernel/include/kernel/OpenFileDescriptorSet.h @@ -41,8 +41,8 @@ namespace Kernel void close_all(); void close_cloexec(); - BAN::ErrorOr read(int fd, void* buffer, size_t count); - BAN::ErrorOr write(int fd, const void* buffer, size_t count); + BAN::ErrorOr read(int fd, BAN::ByteSpan); + BAN::ErrorOr write(int fd, BAN::ConstByteSpan); BAN::ErrorOr read_dir_entries(int fd, DirectoryEntryList* list, size_t list_size); diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index a0b23e96..3421a72e 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -139,9 +139,9 @@ namespace Kernel PageTable& page_table() { return m_page_table ? *m_page_table : PageTable::kernel(); } - size_t proc_meminfo(off_t offset, void* buffer, size_t buffer_size) const; - size_t proc_cmdline(off_t offset, void* buffer, size_t buffer_size) const; - size_t proc_environ(off_t offset, void* buffer, size_t buffer_size) const; + size_t proc_meminfo(off_t offset, BAN::ByteSpan) const; + size_t proc_cmdline(off_t offset, BAN::ByteSpan) const; + size_t proc_environ(off_t offset, BAN::ByteSpan) const; bool is_userspace() const { return m_is_userspace; } const userspace_info_t& userspace_info() const { return m_userspace_info; } diff --git a/kernel/include/kernel/Storage/ATA/AHCI/Device.h b/kernel/include/kernel/Storage/ATA/AHCI/Device.h index 4e4c29fa..42d39335 100644 --- a/kernel/include/kernel/Storage/ATA/AHCI/Device.h +++ b/kernel/include/kernel/Storage/ATA/AHCI/Device.h @@ -23,8 +23,8 @@ namespace Kernel BAN::ErrorOr rebase(); BAN::ErrorOr read_identify_data(); - virtual BAN::ErrorOr read_sectors_impl(uint64_t lba, uint64_t sector_count, uint8_t* buffer) override; - virtual BAN::ErrorOr write_sectors_impl(uint64_t lba, uint64_t sector_count, const uint8_t* buffer) override; + virtual BAN::ErrorOr read_sectors_impl(uint64_t lba, uint64_t sector_count, BAN::ByteSpan) override; + virtual BAN::ErrorOr write_sectors_impl(uint64_t lba, uint64_t sector_count, BAN::ConstByteSpan) override; BAN::ErrorOr send_command_and_block(uint64_t lba, uint64_t sector_count, Command command); BAN::Optional find_free_command_slot(); diff --git a/kernel/include/kernel/Storage/ATA/ATABus.h b/kernel/include/kernel/Storage/ATA/ATABus.h index 3c781d6b..95bdda8e 100644 --- a/kernel/include/kernel/Storage/ATA/ATABus.h +++ b/kernel/include/kernel/Storage/ATA/ATABus.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -22,8 +23,8 @@ namespace Kernel public: static BAN::ErrorOr> create(uint16_t base, uint16_t ctrl, uint8_t irq); - BAN::ErrorOr read(ATADevice&, uint64_t, uint8_t, uint8_t*); - BAN::ErrorOr write(ATADevice&, uint64_t, uint8_t, const uint8_t*); + BAN::ErrorOr read(ATADevice&, uint64_t lba, uint64_t sector_count, BAN::ByteSpan); + BAN::ErrorOr write(ATADevice&, uint64_t lba, uint64_t sector_count, BAN::ConstByteSpan); virtual void handle_irq() override; diff --git a/kernel/include/kernel/Storage/ATA/ATADevice.h b/kernel/include/kernel/Storage/ATA/ATADevice.h index 38a67bd7..b91e27b5 100644 --- a/kernel/include/kernel/Storage/ATA/ATADevice.h +++ b/kernel/include/kernel/Storage/ATA/ATADevice.h @@ -32,8 +32,8 @@ namespace Kernel virtual dev_t rdev() const override { return m_rdev; } - virtual BAN::ErrorOr read_impl(off_t, void*, size_t) override; - virtual BAN::ErrorOr write_impl(off_t, const void*, size_t) override; + virtual BAN::ErrorOr read_impl(off_t, BAN::ByteSpan) override; + virtual BAN::ErrorOr write_impl(off_t, BAN::ConstByteSpan) override; protected: ATABaseDevice(); @@ -63,8 +63,8 @@ namespace Kernel private: ATADevice(BAN::RefPtr, ATABus::DeviceType, bool is_secodary); - virtual BAN::ErrorOr read_sectors_impl(uint64_t, uint64_t, uint8_t*) override; - virtual BAN::ErrorOr write_sectors_impl(uint64_t, uint64_t, const uint8_t*) override; + virtual BAN::ErrorOr read_sectors_impl(uint64_t, uint64_t, BAN::ByteSpan) override; + virtual BAN::ErrorOr write_sectors_impl(uint64_t, uint64_t, BAN::ConstByteSpan) override; private: BAN::RefPtr m_bus; diff --git a/kernel/include/kernel/Storage/DiskCache.h b/kernel/include/kernel/Storage/DiskCache.h index 99b88ce4..c0b082df 100644 --- a/kernel/include/kernel/Storage/DiskCache.h +++ b/kernel/include/kernel/Storage/DiskCache.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -15,8 +16,8 @@ namespace Kernel DiskCache(size_t sector_size, StorageDevice&); ~DiskCache(); - bool read_from_cache(uint64_t sector, uint8_t* buffer); - BAN::ErrorOr write_to_cache(uint64_t sector, const uint8_t* buffer, bool dirty); + bool read_from_cache(uint64_t sector, BAN::ByteSpan); + BAN::ErrorOr write_to_cache(uint64_t sector, BAN::ConstByteSpan, bool dirty); BAN::ErrorOr sync(); size_t release_clean_pages(size_t); diff --git a/kernel/include/kernel/Storage/StorageDevice.h b/kernel/include/kernel/Storage/StorageDevice.h index 15a32903..4f433fb0 100644 --- a/kernel/include/kernel/Storage/StorageDevice.h +++ b/kernel/include/kernel/Storage/StorageDevice.h @@ -30,8 +30,8 @@ namespace Kernel const char* label() const { return m_label; } const StorageDevice& device() const { return m_device; } - BAN::ErrorOr read_sectors(uint64_t lba, uint8_t sector_count, uint8_t* buffer); - BAN::ErrorOr write_sectors(uint64_t lba, uint8_t sector_count, const uint8_t* buffer); + BAN::ErrorOr read_sectors(uint64_t lba, uint8_t sector_count, BAN::ByteSpan); + BAN::ErrorOr write_sectors(uint64_t lba, uint8_t sector_count, BAN::ConstByteSpan); virtual BAN::StringView name() const override { return m_name; } @@ -57,7 +57,7 @@ namespace Kernel virtual dev_t rdev() const override { return m_rdev; } protected: - virtual BAN::ErrorOr read_impl(off_t, void*, size_t) override; + virtual BAN::ErrorOr read_impl(off_t, BAN::ByteSpan) override; private: const dev_t m_rdev; @@ -73,8 +73,8 @@ namespace Kernel BAN::ErrorOr initialize_partitions(); - BAN::ErrorOr read_sectors(uint64_t lba, uint64_t sector_count, uint8_t* buffer); - BAN::ErrorOr write_sectors(uint64_t lba, uint64_t sector_count, const uint8_t* buffer); + BAN::ErrorOr read_sectors(uint64_t lba, uint64_t sector_count, BAN::ByteSpan); + BAN::ErrorOr write_sectors(uint64_t lba, uint64_t sector_count, BAN::ConstByteSpan); virtual uint32_t sector_size() const = 0; virtual uint64_t total_size() const = 0; @@ -86,8 +86,8 @@ namespace Kernel virtual bool is_storage_device() const override { return true; } protected: - virtual BAN::ErrorOr read_sectors_impl(uint64_t lba, uint64_t sector_count, uint8_t* buffer) = 0; - virtual BAN::ErrorOr write_sectors_impl(uint64_t lba, uint64_t sector_count, const uint8_t* buffer) = 0; + virtual BAN::ErrorOr read_sectors_impl(uint64_t lba, uint64_t sector_count, BAN::ByteSpan) = 0; + virtual BAN::ErrorOr write_sectors_impl(uint64_t lba, uint64_t sector_count, BAN::ConstByteSpan) = 0; void add_disk_cache(); private: diff --git a/kernel/include/kernel/Terminal/TTY.h b/kernel/include/kernel/Terminal/TTY.h index b5c87749..9c776167 100644 --- a/kernel/include/kernel/Terminal/TTY.h +++ b/kernel/include/kernel/Terminal/TTY.h @@ -49,8 +49,8 @@ namespace Kernel { } virtual void putchar_impl(uint8_t ch) = 0; - virtual BAN::ErrorOr read_impl(off_t, void*, size_t) override; - virtual BAN::ErrorOr write_impl(off_t, const void*, size_t) override; + virtual BAN::ErrorOr read_impl(off_t, BAN::ByteSpan) override; + virtual BAN::ErrorOr write_impl(off_t, BAN::ConstByteSpan) override; private: void do_backspace(); diff --git a/kernel/kernel/Device/ZeroDevice.cpp b/kernel/kernel/Device/ZeroDevice.cpp index 4a026c32..cf6d98fb 100644 --- a/kernel/kernel/Device/ZeroDevice.cpp +++ b/kernel/kernel/Device/ZeroDevice.cpp @@ -12,10 +12,10 @@ namespace Kernel return BAN::RefPtr::adopt(result); } - BAN::ErrorOr ZeroDevice::read_impl(off_t, void* buffer, size_t bytes) + BAN::ErrorOr ZeroDevice::read_impl(off_t, BAN::ByteSpan buffer) { - memset(buffer, 0, bytes); - return bytes; + memset(buffer.data(), 0, buffer.size()); + return buffer.size(); } } \ No newline at end of file diff --git a/kernel/kernel/FS/Ext2/FileSystem.cpp b/kernel/kernel/FS/Ext2/FileSystem.cpp index 47473965..37b1b55c 100644 --- a/kernel/kernel/FS/Ext2/FileSystem.cpp +++ b/kernel/kernel/FS/Ext2/FileSystem.cpp @@ -33,7 +33,7 @@ namespace Kernel BAN::Vector superblock_buffer; TRY(superblock_buffer.resize(sector_count * sector_size)); - TRY(m_partition.read_sectors(lba, sector_count, superblock_buffer.data())); + TRY(m_partition.read_sectors(lba, sector_count, superblock_buffer.span())); memcpy(&m_superblock, superblock_buffer.data(), sizeof(Ext2::Superblock)); } @@ -223,7 +223,7 @@ namespace Kernel ASSERT(block >= 2); ASSERT(buffer.size() >= block_size); - MUST(m_partition.read_sectors(sectors_before + (block - 2) * sectors_per_block, sectors_per_block, buffer.data())); + MUST(m_partition.read_sectors(sectors_before + (block - 2) * sectors_per_block, sectors_per_block, buffer.span())); } void Ext2FS::write_block(uint32_t block, const BlockBufferWrapper& buffer) @@ -237,7 +237,7 @@ namespace Kernel ASSERT(block >= 2); ASSERT(buffer.size() >= block_size); - MUST(m_partition.write_sectors(sectors_before + (block - 2) * sectors_per_block, sectors_per_block, buffer.data())); + MUST(m_partition.write_sectors(sectors_before + (block - 2) * sectors_per_block, sectors_per_block, buffer.span())); } void Ext2FS::sync_superblock() @@ -258,11 +258,11 @@ namespace Kernel BAN::Vector superblock_buffer; MUST(superblock_buffer.resize(sector_count * sector_size)); - MUST(m_partition.read_sectors(lba, sector_count, superblock_buffer.data())); + MUST(m_partition.read_sectors(lba, sector_count, superblock_buffer.span())); if (memcmp(superblock_buffer.data(), &m_superblock, superblock_bytes)) { memcpy(superblock_buffer.data(), &m_superblock, superblock_bytes); - MUST(m_partition.write_sectors(lba, sector_count, superblock_buffer.data())); + MUST(m_partition.write_sectors(lba, sector_count, superblock_buffer.span())); } } diff --git a/kernel/kernel/FS/Ext2/Inode.cpp b/kernel/kernel/FS/Ext2/Inode.cpp index b489a9ae..2781e36e 100644 --- a/kernel/kernel/FS/Ext2/Inode.cpp +++ b/kernel/kernel/FS/Ext2/Inode.cpp @@ -100,19 +100,21 @@ namespace Kernel return BAN::Error::from_errno(ENOTSUP); } - BAN::ErrorOr Ext2Inode::read_impl(off_t offset, void* buffer, size_t count) + BAN::ErrorOr Ext2Inode::read_impl(off_t offset, BAN::ByteSpan buffer) { // FIXME: update atime if needed ASSERT(!mode().ifdir()); ASSERT(offset >= 0); - if (offset >= UINT32_MAX || count >= UINT32_MAX || offset + count >= UINT32_MAX) + if (offset >= UINT32_MAX || buffer.size() >= UINT32_MAX || buffer.size() >= (size_t)(UINT32_MAX - offset)) return BAN::Error::from_errno(EOVERFLOW); if (offset >= m_inode.size) return 0; - if (offset + count > m_inode.size) + + uint32_t count = buffer.size(); + if (offset + buffer.size() > m_inode.size) count = m_inode.size - offset; const uint32_t block_size = blksize(); @@ -131,7 +133,7 @@ namespace Kernel uint32_t copy_offset = (offset + n_read) % block_size; uint32_t to_copy = BAN::Math::min(block_size - copy_offset, count - n_read); - memcpy((uint8_t*)buffer + n_read, block_buffer.data() + copy_offset, to_copy); + memcpy(buffer.data() + n_read, block_buffer.data() + copy_offset, to_copy); n_read += to_copy; } @@ -139,26 +141,25 @@ namespace Kernel return n_read; } - BAN::ErrorOr Ext2Inode::write_impl(off_t offset, const void* buffer, size_t count) + BAN::ErrorOr Ext2Inode::write_impl(off_t offset, BAN::ConstByteSpan buffer) { // FIXME: update atime if needed ASSERT(!mode().ifdir()); ASSERT(offset >= 0); - if (offset >= UINT32_MAX || count >= UINT32_MAX || offset + count >= UINT32_MAX) + if (offset >= UINT32_MAX || buffer.size() >= UINT32_MAX || buffer.size() >= (size_t)(UINT32_MAX - offset)) return BAN::Error::from_errno(EOVERFLOW); - if (m_inode.size < offset + count) - TRY(truncate_impl(offset + count)); + if (m_inode.size < offset + buffer.size()) + TRY(truncate_impl(offset + buffer.size())); const uint32_t block_size = blksize(); auto block_buffer = m_fs.get_block_buffer(); - const uint8_t* u8buffer = (const uint8_t*)buffer; - - size_t to_write = count; + size_t written = 0; + size_t to_write = buffer.size(); // Write partial block if (offset % block_size) @@ -169,10 +170,10 @@ namespace Kernel uint32_t to_copy = BAN::Math::min(block_size - block_offset, to_write); m_fs.read_block(block_index, block_buffer); - memcpy(block_buffer.data() + block_offset, u8buffer, to_copy); + memcpy(block_buffer.data() + block_offset, buffer.data(), to_copy); m_fs.write_block(block_index, block_buffer); - u8buffer += to_copy; + written += to_copy; offset += to_copy; to_write -= to_copy; } @@ -181,10 +182,10 @@ namespace Kernel { uint32_t block_index = fs_block_of_data_block_index(offset / block_size); - memcpy(block_buffer.data(), u8buffer, block_buffer.size()); + memcpy(block_buffer.data(), buffer.data() + written, block_buffer.size()); m_fs.write_block(block_index, block_buffer); - u8buffer += block_size; + written += block_size; offset += block_size; to_write -= block_size; } @@ -194,11 +195,11 @@ namespace Kernel uint32_t block_index = fs_block_of_data_block_index(offset / block_size); m_fs.read_block(block_index, block_buffer); - memcpy(block_buffer.data(), u8buffer, to_write); + memcpy(block_buffer.data(), buffer.data() + written, to_write); m_fs.write_block(block_index, block_buffer); } - return count; + return buffer.size(); } BAN::ErrorOr Ext2Inode::truncate_impl(size_t new_size) diff --git a/kernel/kernel/FS/Inode.cpp b/kernel/kernel/FS/Inode.cpp index 1e68a4d9..b66369b4 100644 --- a/kernel/kernel/FS/Inode.cpp +++ b/kernel/kernel/FS/Inode.cpp @@ -102,22 +102,22 @@ namespace Kernel return link_target_impl(); } - BAN::ErrorOr Inode::read(off_t offset, void* buffer, size_t bytes) + BAN::ErrorOr Inode::read(off_t offset, BAN::ByteSpan buffer) { LockGuard _(m_lock); Thread::TerminateBlocker blocker(Thread::current()); if (mode().ifdir()) return BAN::Error::from_errno(EISDIR); - return read_impl(offset, buffer, bytes); + return read_impl(offset, buffer); } - BAN::ErrorOr Inode::write(off_t offset, const void* buffer, size_t bytes) + BAN::ErrorOr Inode::write(off_t offset, BAN::ConstByteSpan buffer) { LockGuard _(m_lock); Thread::TerminateBlocker blocker(Thread::current()); if (mode().ifdir()) return BAN::Error::from_errno(EISDIR); - return write_impl(offset, buffer, bytes); + return write_impl(offset, buffer); } BAN::ErrorOr Inode::truncate(size_t size) diff --git a/kernel/kernel/FS/Pipe.cpp b/kernel/kernel/FS/Pipe.cpp index 3331cbe3..5893b7a9 100644 --- a/kernel/kernel/FS/Pipe.cpp +++ b/kernel/kernel/FS/Pipe.cpp @@ -39,7 +39,7 @@ namespace Kernel m_semaphore.unblock(); } - BAN::ErrorOr Pipe::read_impl(off_t, void* buffer, size_t count) + BAN::ErrorOr Pipe::read_impl(off_t, BAN::ByteSpan buffer) { LockGuard _(m_lock); while (m_buffer.empty()) @@ -51,8 +51,8 @@ namespace Kernel m_lock.lock(); } - size_t to_copy = BAN::Math::min(count, m_buffer.size()); - memcpy(buffer, m_buffer.data(), to_copy); + size_t to_copy = BAN::Math::min(buffer.size(), m_buffer.size()); + memcpy(buffer.data(), m_buffer.data(), to_copy); memmove(m_buffer.data(), m_buffer.data() + to_copy, m_buffer.size() - to_copy); MUST(m_buffer.resize(m_buffer.size() - to_copy)); @@ -64,14 +64,14 @@ namespace Kernel return to_copy; } - BAN::ErrorOr Pipe::write_impl(off_t, const void* buffer, size_t count) + BAN::ErrorOr Pipe::write_impl(off_t, BAN::ConstByteSpan buffer) { LockGuard _(m_lock); size_t old_size = m_buffer.size(); - TRY(m_buffer.resize(old_size + count)); - memcpy(m_buffer.data() + old_size, buffer, count); + TRY(m_buffer.resize(old_size + buffer.size())); + memcpy(m_buffer.data() + old_size, buffer.data(), buffer.size()); timespec current_time = SystemTimer::get().real_time(); m_mtime = current_time; @@ -79,7 +79,7 @@ namespace Kernel m_semaphore.unblock(); - return count; + return buffer.size(); } } \ No newline at end of file diff --git a/kernel/kernel/FS/ProcFS/Inode.cpp b/kernel/kernel/FS/ProcFS/Inode.cpp index 7bc0a392..e74e6237 100644 --- a/kernel/kernel/FS/ProcFS/Inode.cpp +++ b/kernel/kernel/FS/ProcFS/Inode.cpp @@ -25,7 +25,7 @@ namespace Kernel { } - BAN::ErrorOr> ProcROInode::create(Process& process, size_t (Process::*callback)(off_t, void*, size_t) const, RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid) + BAN::ErrorOr> ProcROInode::create(Process& process, size_t (Process::*callback)(off_t, BAN::ByteSpan) const, RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid) { FullInodeInfo inode_info(fs, mode, uid, gid); @@ -35,7 +35,7 @@ namespace Kernel return BAN::RefPtr::adopt(inode_ptr); } - ProcROInode::ProcROInode(Process& process, size_t (Process::*callback)(off_t, void*, size_t) const, RamFileSystem& fs, const FullInodeInfo& inode_info) + ProcROInode::ProcROInode(Process& process, size_t (Process::*callback)(off_t, BAN::ByteSpan) const, RamFileSystem& fs, const FullInodeInfo& inode_info) : RamInode(fs, inode_info) , m_process(process) , m_callback(callback) @@ -43,12 +43,12 @@ namespace Kernel m_inode_info.mode |= Inode::Mode::IFREG; } - BAN::ErrorOr ProcROInode::read_impl(off_t offset, void* buffer, size_t buffer_size) + BAN::ErrorOr ProcROInode::read_impl(off_t offset, BAN::ByteSpan buffer) { ASSERT(offset >= 0); if ((size_t)offset >= sizeof(proc_meminfo_t)) return 0; - return (m_process.*m_callback)(offset, buffer, buffer_size); + return (m_process.*m_callback)(offset, buffer); } } diff --git a/kernel/kernel/FS/RamFS/Inode.cpp b/kernel/kernel/FS/RamFS/Inode.cpp index f030ec12..2b7f7ffe 100644 --- a/kernel/kernel/FS/RamFS/Inode.cpp +++ b/kernel/kernel/FS/RamFS/Inode.cpp @@ -48,23 +48,23 @@ namespace Kernel m_inode_info.mode |= Inode::Mode::IFREG; } - BAN::ErrorOr RamFileInode::read_impl(off_t offset, void* buffer, size_t bytes) + BAN::ErrorOr RamFileInode::read_impl(off_t offset, BAN::ByteSpan buffer) { ASSERT(offset >= 0); if (offset >= size()) return 0; - size_t to_copy = BAN::Math::min(m_inode_info.size - offset, bytes); - memcpy(buffer, m_data.data(), to_copy); + size_t to_copy = BAN::Math::min(m_inode_info.size - offset, buffer.size()); + memcpy(buffer.data(), m_data.data(), to_copy); return to_copy; } - BAN::ErrorOr RamFileInode::write_impl(off_t offset, const void* buffer, size_t bytes) + BAN::ErrorOr RamFileInode::write_impl(off_t offset, BAN::ConstByteSpan buffer) { ASSERT(offset >= 0); - if (offset + bytes > (size_t)size()) - TRY(truncate_impl(offset + bytes)); - memcpy(m_data.data() + offset, buffer, bytes); - return bytes; + if (offset + buffer.size() > (size_t)size()) + TRY(truncate_impl(offset + buffer.size())); + memcpy(m_data.data() + offset, buffer.data(), buffer.size()); + return buffer.size(); } BAN::ErrorOr RamFileInode::truncate_impl(size_t new_size) diff --git a/kernel/kernel/Font.cpp b/kernel/kernel/Font.cpp index 93750a84..1d7d3822 100644 --- a/kernel/kernel/Font.cpp +++ b/kernel/kernel/Font.cpp @@ -43,7 +43,7 @@ namespace Kernel BAN::Vector file_data; TRY(file_data.resize(inode->size())); - TRY(inode->read(0, file_data.data(), file_data.size())); + TRY(inode->read(0, file_data.span())); if (file_data.size() < 4) return BAN::Error::from_error_code(ErrorCode::Font_FileTooSmall); diff --git a/kernel/kernel/Input/PS2Keyboard.cpp b/kernel/kernel/Input/PS2Keyboard.cpp index aba441f1..e4f7a3d5 100644 --- a/kernel/kernel/Input/PS2Keyboard.cpp +++ b/kernel/kernel/Input/PS2Keyboard.cpp @@ -217,9 +217,9 @@ namespace Kernel::Input append_command_queue(Command::SET_LEDS, new_leds); } - BAN::ErrorOr PS2Keyboard::read_impl(off_t, void* buffer, size_t size) + BAN::ErrorOr PS2Keyboard::read_impl(off_t, BAN::ByteSpan buffer) { - if (size < sizeof(KeyEvent)) + if (buffer.size() < sizeof(KeyEvent)) return BAN::Error::from_errno(ENOBUFS); while (true) @@ -231,7 +231,7 @@ namespace Kernel::Input if (m_event_queue.empty()) continue; - *(KeyEvent*)buffer = m_event_queue.front(); + buffer.as() = m_event_queue.front(); m_event_queue.pop(); return sizeof(KeyEvent); diff --git a/kernel/kernel/Memory/FileBackedRegion.cpp b/kernel/kernel/Memory/FileBackedRegion.cpp index b4314f6f..0592be5b 100644 --- a/kernel/kernel/Memory/FileBackedRegion.cpp +++ b/kernel/kernel/Memory/FileBackedRegion.cpp @@ -80,7 +80,7 @@ namespace Kernel page_table.unmap_page(0); } - if (auto ret = inode->write(i * PAGE_SIZE, page_buffer, PAGE_SIZE); ret.is_error()) + if (auto ret = inode->write(i * PAGE_SIZE, BAN::ConstByteSpan::from(page_buffer)); ret.is_error()) dwarnln("{}", ret.error()); } } @@ -109,7 +109,7 @@ namespace Kernel // Zero out the new page if (&PageTable::current() == &m_page_table) - read_ret = m_inode->read(file_offset, (void*)vaddr, bytes); + read_ret = m_inode->read(file_offset, BAN::ByteSpan((uint8_t*)vaddr, bytes)); else { auto& page_table = PageTable::current(); @@ -118,7 +118,7 @@ namespace Kernel ASSERT(page_table.is_page_free(0)); page_table.map_page_at(paddr, 0, PageTable::Flags::ReadWrite | PageTable::Flags::Present); - read_ret = m_inode->read(file_offset, (void*)0, bytes); + read_ret = m_inode->read(file_offset, BAN::ByteSpan((uint8_t*)0, bytes)); memset((void*)0, 0x00, PAGE_SIZE); page_table.unmap_page(0); } @@ -156,7 +156,7 @@ namespace Kernel size_t offset = vaddr - m_vaddr; size_t bytes = BAN::Math::min(m_size - offset, PAGE_SIZE); - TRY(m_inode->read(offset, m_shared_data->page_buffer, bytes)); + TRY(m_inode->read(offset, BAN::ByteSpan(m_shared_data->page_buffer, bytes))); auto& page_table = PageTable::current(); diff --git a/kernel/kernel/OpenFileDescriptorSet.cpp b/kernel/kernel/OpenFileDescriptorSet.cpp index f160f8ac..24ab36aa 100644 --- a/kernel/kernel/OpenFileDescriptorSet.cpp +++ b/kernel/kernel/OpenFileDescriptorSet.cpp @@ -264,24 +264,24 @@ namespace Kernel } } - BAN::ErrorOr OpenFileDescriptorSet::read(int fd, void* buffer, size_t count) + BAN::ErrorOr OpenFileDescriptorSet::read(int fd, BAN::ByteSpan buffer) { TRY(validate_fd(fd)); auto& open_file = m_open_files[fd]; if ((open_file->flags & O_NONBLOCK) && !open_file->inode->has_data()) return 0; - size_t nread = TRY(open_file->inode->read(open_file->offset, buffer, count)); + size_t nread = TRY(open_file->inode->read(open_file->offset, buffer)); open_file->offset += nread; return nread; } - BAN::ErrorOr OpenFileDescriptorSet::write(int fd, const void* buffer, size_t count) + BAN::ErrorOr OpenFileDescriptorSet::write(int fd, BAN::ConstByteSpan buffer) { TRY(validate_fd(fd)); auto& open_file = m_open_files[fd]; if (open_file->flags & O_APPEND) open_file->offset = open_file->inode->size(); - size_t nwrite = TRY(open_file->inode->write(open_file->offset, buffer, count)); + size_t nwrite = TRY(open_file->inode->write(open_file->offset, buffer)); open_file->offset += nwrite; return nwrite; } diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 18ca6048..57180db2 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -260,7 +260,7 @@ namespace Kernel thread->set_terminating(); } - size_t Process::proc_meminfo(off_t offset, void* buffer, size_t buffer_size) const + size_t Process::proc_meminfo(off_t offset, BAN::ByteSpan buffer) const { ASSERT(offset >= 0); if ((size_t)offset >= sizeof(proc_meminfo_t)) @@ -290,12 +290,12 @@ namespace Kernel } } - size_t bytes = BAN::Math::min(sizeof(proc_meminfo_t) - offset, buffer_size); - memcpy(buffer, (uint8_t*)&meminfo + offset, bytes); + size_t bytes = BAN::Math::min(sizeof(proc_meminfo_t) - offset, buffer.size()); + memcpy(buffer.data(), (uint8_t*)&meminfo + offset, bytes); return bytes; } - static size_t read_from_vec_of_str(const BAN::Vector& container, size_t start, void* buffer, size_t buffer_size) + static size_t read_from_vec_of_str(const BAN::Vector& container, size_t start, BAN::ByteSpan buffer) { size_t offset = 0; size_t written = 0; @@ -307,11 +307,11 @@ namespace Kernel if (offset < start) elem_offset = start - offset; - size_t bytes = BAN::Math::min(elem.size() + 1 - elem_offset, buffer_size - written); - memcpy((uint8_t*)buffer + written, elem.data() + elem_offset, bytes); + size_t bytes = BAN::Math::min(elem.size() + 1 - elem_offset, buffer.size() - written); + memcpy(buffer.data() + written, elem.data() + elem_offset, bytes); written += bytes; - if (written >= buffer_size) + if (written >= buffer.size()) break; } offset += elem.size() + 1; @@ -319,16 +319,16 @@ namespace Kernel return written; } - size_t Process::proc_cmdline(off_t offset, void* buffer, size_t buffer_size) const + size_t Process::proc_cmdline(off_t offset, BAN::ByteSpan buffer) const { LockGuard _(m_lock); - return read_from_vec_of_str(m_cmdline, offset, buffer, buffer_size); + return read_from_vec_of_str(m_cmdline, offset, buffer); } - size_t Process::proc_environ(off_t offset, void* buffer, size_t buffer_size) const + size_t Process::proc_environ(off_t offset, BAN::ByteSpan buffer) const { LockGuard _(m_lock); - return read_from_vec_of_str(m_environ, offset, buffer, buffer_size); + return read_from_vec_of_str(m_environ, offset, buffer); } BAN::ErrorOr Process::sys_exit(int status) @@ -723,14 +723,14 @@ namespace Kernel { LockGuard _(m_lock); validate_pointer_access(buffer, count); - return TRY(m_open_file_descriptors.read(fd, buffer, count)); + return TRY(m_open_file_descriptors.read(fd, BAN::ByteSpan((uint8_t*)buffer, count))); } BAN::ErrorOr Process::sys_write(int fd, const void* buffer, size_t count) { LockGuard _(m_lock); validate_pointer_access(buffer, count); - return TRY(m_open_file_descriptors.write(fd, buffer, count)); + return TRY(m_open_file_descriptors.write(fd, BAN::ByteSpan((uint8_t*)buffer, count))); } BAN::ErrorOr Process::sys_pipe(int fildes[2]) diff --git a/kernel/kernel/Storage/ATA/AHCI/Device.cpp b/kernel/kernel/Storage/ATA/AHCI/Device.cpp index 8b0c2521..a087bd56 100644 --- a/kernel/kernel/Storage/ATA/AHCI/Device.cpp +++ b/kernel/kernel/Storage/ATA/AHCI/Device.cpp @@ -158,28 +158,30 @@ namespace Kernel __builtin_ia32_pause(); } - BAN::ErrorOr AHCIDevice::read_sectors_impl(uint64_t lba, uint64_t sector_count, uint8_t* buffer) + BAN::ErrorOr AHCIDevice::read_sectors_impl(uint64_t lba, uint64_t sector_count, BAN::ByteSpan buffer) { + ASSERT(buffer.size() >= sector_count * sector_size()); const size_t sectors_per_page = PAGE_SIZE / sector_size(); for (uint64_t sector_off = 0; sector_off < sector_count; sector_off += sectors_per_page) { uint64_t to_read = BAN::Math::min(sector_count - sector_off, sectors_per_page); TRY(send_command_and_block(lba + sector_off, to_read, Command::Read)); - memcpy(buffer + sector_off * sector_size(), (void*)m_data_dma_region->vaddr(), to_read * sector_size()); + memcpy(buffer.data() + sector_off * sector_size(), (void*)m_data_dma_region->vaddr(), to_read * sector_size()); } return {}; } - BAN::ErrorOr AHCIDevice::write_sectors_impl(uint64_t lba, uint64_t sector_count, const uint8_t* buffer) + BAN::ErrorOr AHCIDevice::write_sectors_impl(uint64_t lba, uint64_t sector_count, BAN::ConstByteSpan buffer) { + ASSERT(buffer.size() >= sector_count * sector_size()); const size_t sectors_per_page = PAGE_SIZE / sector_size(); for (uint64_t sector_off = 0; sector_off < sector_count; sector_off += sectors_per_page) { uint64_t to_read = BAN::Math::min(sector_count - sector_off, sectors_per_page); - memcpy((void*)m_data_dma_region->vaddr(), buffer + sector_off * sector_size(), to_read * sector_size()); + memcpy((void*)m_data_dma_region->vaddr(), buffer.data() + sector_off * sector_size(), to_read * sector_size()); TRY(send_command_and_block(lba + sector_off, to_read, Command::Write)); } diff --git a/kernel/kernel/Storage/ATA/ATABus.cpp b/kernel/kernel/Storage/ATA/ATABus.cpp index cd499312..bb0b0f48 100644 --- a/kernel/kernel/Storage/ATA/ATABus.cpp +++ b/kernel/kernel/Storage/ATA/ATABus.cpp @@ -228,8 +228,10 @@ namespace Kernel return BAN::Error::from_error_code(ErrorCode::None); } - BAN::ErrorOr ATABus::read(ATADevice& device, uint64_t lba, uint8_t sector_count, uint8_t* buffer) + BAN::ErrorOr ATABus::read(ATADevice& device, uint64_t lba, uint64_t sector_count, BAN::ByteSpan buffer) { + ASSERT(sector_count <= 0xFF); + ASSERT(buffer.size() >= sector_count * device.sector_size()); if (lba + sector_count > device.sector_count()) return BAN::Error::from_error_code(ErrorCode::Storage_Boundaries); @@ -251,7 +253,7 @@ namespace Kernel for (uint32_t sector = 0; sector < sector_count; sector++) { block_until_irq(); - read_buffer(ATA_PORT_DATA, (uint16_t*)buffer + sector * device.words_per_sector(), device.words_per_sector()); + read_buffer(ATA_PORT_DATA, (uint16_t*)buffer.data() + sector * device.words_per_sector(), device.words_per_sector()); } } else @@ -263,8 +265,10 @@ namespace Kernel return {}; } - BAN::ErrorOr ATABus::write(ATADevice& device, uint64_t lba, uint8_t sector_count, const uint8_t* buffer) + BAN::ErrorOr ATABus::write(ATADevice& device, uint64_t lba, uint64_t sector_count, BAN::ConstByteSpan buffer) { + ASSERT(sector_count <= 0xFF); + ASSERT(buffer.size() >= sector_count * device.sector_size()); if (lba + sector_count > device.sector_count()) return BAN::Error::from_error_code(ErrorCode::Storage_Boundaries); @@ -285,7 +289,7 @@ namespace Kernel for (uint32_t sector = 0; sector < sector_count; sector++) { - write_buffer(ATA_PORT_DATA, (uint16_t*)buffer + sector * device.words_per_sector(), device.words_per_sector()); + write_buffer(ATA_PORT_DATA, (uint16_t*)buffer.data() + sector * device.words_per_sector(), device.words_per_sector()); block_until_irq(); } } diff --git a/kernel/kernel/Storage/ATA/ATADevice.cpp b/kernel/kernel/Storage/ATA/ATADevice.cpp index 0eacead6..bb959054 100644 --- a/kernel/kernel/Storage/ATA/ATADevice.cpp +++ b/kernel/kernel/Storage/ATA/ATADevice.cpp @@ -82,24 +82,24 @@ namespace Kernel return {}; } - BAN::ErrorOr detail::ATABaseDevice::read_impl(off_t offset, void* buffer, size_t bytes) + BAN::ErrorOr detail::ATABaseDevice::read_impl(off_t offset, BAN::ByteSpan buffer) { if (offset % sector_size()) return BAN::Error::from_errno(EINVAL); - if (bytes % sector_size()) + if (buffer.size() % sector_size()) return BAN::Error::from_errno(EINVAL); - TRY(read_sectors(offset / sector_size(), bytes / sector_size(), (uint8_t*)buffer)); - return bytes; + TRY(read_sectors(offset / sector_size(), buffer.size() / sector_size(), buffer)); + return buffer.size(); } - BAN::ErrorOr detail::ATABaseDevice::write_impl(off_t offset, const void* buffer, size_t bytes) + BAN::ErrorOr detail::ATABaseDevice::write_impl(off_t offset, BAN::ConstByteSpan buffer) { if (offset % sector_size()) return BAN::Error::from_errno(EINVAL); - if (bytes % sector_size()) + if (buffer.size() % sector_size()) return BAN::Error::from_errno(EINVAL); - TRY(write_sectors(offset / sector_size(), bytes / sector_size(), (const uint8_t*)buffer)); - return bytes; + TRY(write_sectors(offset / sector_size(), buffer.size() / sector_size(), buffer)); + return buffer.size(); } BAN::ErrorOr> ATADevice::create(BAN::RefPtr bus, ATABus::DeviceType type, bool is_secondary, BAN::Span identify_data) @@ -118,14 +118,16 @@ namespace Kernel , m_is_secondary(is_secondary) { } - BAN::ErrorOr ATADevice::read_sectors_impl(uint64_t lba, uint64_t sector_count, uint8_t* buffer) + BAN::ErrorOr ATADevice::read_sectors_impl(uint64_t lba, uint64_t sector_count, BAN::ByteSpan buffer) { + ASSERT(buffer.size() >= sector_count * sector_size()); TRY(m_bus->read(*this, lba, sector_count, buffer)); return {}; } - BAN::ErrorOr ATADevice::write_sectors_impl(uint64_t lba, uint64_t sector_count, const uint8_t* buffer) + BAN::ErrorOr ATADevice::write_sectors_impl(uint64_t lba, uint64_t sector_count, BAN::ConstByteSpan buffer) { + ASSERT(buffer.size() >= sector_count * sector_size()); TRY(m_bus->write(*this, lba, sector_count, buffer)); return {}; } diff --git a/kernel/kernel/Storage/DiskCache.cpp b/kernel/kernel/Storage/DiskCache.cpp index afcc5675..05803ad7 100644 --- a/kernel/kernel/Storage/DiskCache.cpp +++ b/kernel/kernel/Storage/DiskCache.cpp @@ -24,8 +24,10 @@ namespace Kernel release_all_pages(); } - bool DiskCache::read_from_cache(uint64_t sector, uint8_t* buffer) + bool DiskCache::read_from_cache(uint64_t sector, BAN::ByteSpan buffer) { + ASSERT(buffer.size() >= m_sector_size); + uint64_t sectors_per_page = PAGE_SIZE / m_sector_size; uint64_t page_cache_offset = sector % sectors_per_page; uint64_t page_cache_start = sector - page_cache_offset; @@ -46,7 +48,7 @@ namespace Kernel CriticalScope _; page_table.map_page_at(cache.paddr, 0, PageTable::Flags::Present); - memcpy(buffer, (void*)(page_cache_offset * m_sector_size), m_sector_size); + memcpy(buffer.data(), (void*)(page_cache_offset * m_sector_size), m_sector_size); page_table.unmap_page(0); return true; @@ -55,8 +57,9 @@ namespace Kernel return false; }; - BAN::ErrorOr DiskCache::write_to_cache(uint64_t sector, const uint8_t* buffer, bool dirty) + BAN::ErrorOr DiskCache::write_to_cache(uint64_t sector, BAN::ConstByteSpan buffer, bool dirty) { + ASSERT(buffer.size() >= m_sector_size); uint64_t sectors_per_page = PAGE_SIZE / m_sector_size; uint64_t page_cache_offset = sector % sectors_per_page; uint64_t page_cache_start = sector - page_cache_offset; @@ -80,7 +83,7 @@ namespace Kernel { CriticalScope _; page_table.map_page_at(cache.paddr, 0, PageTable::Flags::ReadWrite | PageTable::Flags::Present); - memcpy((void*)(page_cache_offset * m_sector_size), buffer, m_sector_size); + memcpy((void*)(page_cache_offset * m_sector_size), buffer.data(), m_sector_size); page_table.unmap_page(0); } @@ -110,8 +113,8 @@ namespace Kernel { CriticalScope _; - page_table.map_page_at(cache.paddr, 0, PageTable::Flags::Present); - memcpy((void*)(page_cache_offset * m_sector_size), buffer, m_sector_size); + page_table.map_page_at(cache.paddr, 0, PageTable::Flags::ReadWrite | PageTable::Flags::Present); + memcpy((void*)(page_cache_offset * m_sector_size), buffer.data(), m_sector_size); page_table.unmap_page(0); } @@ -149,7 +152,8 @@ namespace Kernel else { dprintln_if(DEBUG_SYNC, "syncing {}->{}", cache.first_sector + sector_start, cache.first_sector + sector_start + sector_count); - TRY(m_device.write_sectors_impl(cache.first_sector + sector_start, sector_count, m_sync_cache.data() + sector_start * m_sector_size)); + auto data_slice = m_sync_cache.span().slice(sector_start * m_sector_size, sector_count * m_sector_size); + TRY(m_device.write_sectors_impl(cache.first_sector + sector_start, sector_count, data_slice)); sector_start += sector_count + 1; sector_count = 0; } @@ -158,7 +162,8 @@ namespace Kernel if (sector_count > 0) { dprintln_if(DEBUG_SYNC, "syncing {}->{}", cache.first_sector + sector_start, cache.first_sector + sector_start + sector_count); - TRY(m_device.write_sectors_impl(cache.first_sector + sector_start, sector_count, m_sync_cache.data() + sector_start * m_sector_size)); + auto data_slice = m_sync_cache.span().slice(sector_start * m_sector_size, sector_count * m_sector_size); + TRY(m_device.write_sectors_impl(cache.first_sector + sector_start, sector_count, data_slice)); } cache.dirty_mask = 0; diff --git a/kernel/kernel/Storage/StorageDevice.cpp b/kernel/kernel/Storage/StorageDevice.cpp index a64b656a..583a3a0b 100644 --- a/kernel/kernel/Storage/StorageDevice.cpp +++ b/kernel/kernel/Storage/StorageDevice.cpp @@ -153,8 +153,10 @@ namespace Kernel if (total_size() < sizeof(GPTHeader)) return BAN::Error::from_error_code(ErrorCode::Storage_GPTHeader); - BAN::Vector lba1(sector_size()); - TRY(read_sectors(1, 1, lba1.data())); + BAN::Vector lba1; + TRY(lba1.resize(sector_size())); + + TRY(read_sectors(1, 1, lba1.span())); const GPTHeader& header = *(const GPTHeader*)lba1.data(); if (!is_valid_gpt_header(header, sector_size())) @@ -169,7 +171,7 @@ namespace Kernel BAN::Vector entry_array; TRY(entry_array.resize(size)); - TRY(read_sectors(header.partition_entry_lba, size / sector_size(), entry_array.data())); + TRY(read_sectors(header.partition_entry_lba, size / sector_size(), entry_array.span())); if (!is_valid_gpt_crc32(header, lba1, entry_array)) return BAN::Error::from_error_code(ErrorCode::Storage_GPTHeader); @@ -226,8 +228,9 @@ namespace Kernel memcpy(m_label, label, sizeof(m_label)); } - BAN::ErrorOr Partition::read_sectors(uint64_t lba, uint8_t sector_count, uint8_t* buffer) + BAN::ErrorOr Partition::read_sectors(uint64_t lba, uint8_t sector_count, BAN::ByteSpan buffer) { + ASSERT(buffer.size() >= sector_count * m_device.sector_size()); const uint32_t sectors_in_partition = m_lba_end - m_lba_start; if (lba + sector_count > sectors_in_partition) return BAN::Error::from_error_code(ErrorCode::Storage_Boundaries); @@ -235,8 +238,9 @@ namespace Kernel return {}; } - BAN::ErrorOr Partition::write_sectors(uint64_t lba, uint8_t sector_count, const uint8_t* buffer) + BAN::ErrorOr Partition::write_sectors(uint64_t lba, uint8_t sector_count, BAN::ConstByteSpan buffer) { + ASSERT(buffer.size() >= sector_count * m_device.sector_size()); const uint32_t sectors_in_partition = m_lba_end - m_lba_start; if (lba + sector_count > sectors_in_partition) return BAN::Error::from_error_code(ErrorCode::Storage_Boundaries); @@ -244,23 +248,23 @@ namespace Kernel return {}; } - BAN::ErrorOr Partition::read_impl(off_t offset, void* buffer, size_t bytes) + BAN::ErrorOr Partition::read_impl(off_t offset, BAN::ByteSpan buffer) { ASSERT(offset >= 0); - if (offset % m_device.sector_size() || bytes % m_device.sector_size()) + if (offset % m_device.sector_size() || buffer.size() % m_device.sector_size()) return BAN::Error::from_errno(ENOTSUP); const uint32_t sectors_in_partition = m_lba_end - m_lba_start; uint32_t lba = offset / m_device.sector_size(); - uint32_t sector_count = bytes / m_device.sector_size(); + uint32_t sector_count = buffer.size() / m_device.sector_size(); if (lba == sectors_in_partition) return 0; if (lba + sector_count > sectors_in_partition) sector_count = sectors_in_partition - lba; - TRY(read_sectors(lba, sector_count, (uint8_t*)buffer)); + TRY(read_sectors(lba, sector_count, buffer)); return sector_count * m_device.sector_size(); } @@ -283,36 +287,51 @@ namespace Kernel return {}; } - BAN::ErrorOr StorageDevice::read_sectors(uint64_t lba, uint64_t sector_count, uint8_t* buffer) + BAN::ErrorOr StorageDevice::read_sectors(uint64_t lba, uint64_t sector_count, BAN::ByteSpan buffer) { + ASSERT(buffer.size() >= sector_count * sector_size()); + + { + LockGuard _(m_lock); + Thread::TerminateBlocker blocker(Thread::current()); + if (!m_disk_cache.has_value()) + return read_sectors_impl(lba, sector_count, buffer); + } + for (uint64_t offset = 0; offset < sector_count; offset++) { LockGuard _(m_lock); Thread::TerminateBlocker blocker(Thread::current()); - uint8_t* buffer_ptr = buffer + offset * sector_size(); - if (m_disk_cache.has_value()) - if (m_disk_cache->read_from_cache(lba + offset, buffer_ptr)) - continue; - TRY(read_sectors_impl(lba + offset, 1, buffer_ptr)); - if (m_disk_cache.has_value()) - (void)m_disk_cache->write_to_cache(lba + offset, buffer_ptr, false); + auto sector_buffer = buffer.slice(offset * sector_size(), sector_size()); + if (m_disk_cache->read_from_cache(lba + offset, sector_buffer)) + continue; + TRY(read_sectors_impl(lba + offset, 1, sector_buffer)); + (void)m_disk_cache->write_to_cache(lba + offset, sector_buffer, false); } return {}; } - BAN::ErrorOr StorageDevice::write_sectors(uint64_t lba, uint64_t sector_count, const uint8_t* buffer) + BAN::ErrorOr StorageDevice::write_sectors(uint64_t lba, uint64_t sector_count, BAN::ConstByteSpan buffer) { - // TODO: use disk cache for dirty pages. I don't wanna think about how to do it safely now + ASSERT(buffer.size() >= sector_count * sector_size()); + + { + LockGuard _(m_lock); + Thread::TerminateBlocker blocker(Thread::current()); + if (!m_disk_cache.has_value()) + return write_sectors_impl(lba, sector_count, buffer); + } + for (uint8_t offset = 0; offset < sector_count; offset++) { LockGuard _(m_lock); Thread::TerminateBlocker blocker(Thread::current()); - const uint8_t* buffer_ptr = buffer + offset * sector_size(); - if (!m_disk_cache.has_value() || m_disk_cache->write_to_cache(lba + offset, buffer_ptr, true).is_error()) - TRY(write_sectors_impl(lba + offset, 1, buffer_ptr)); + auto sector_buffer = buffer.slice(offset * sector_size(), sector_size()); + if (m_disk_cache->write_to_cache(lba + offset, sector_buffer, true).is_error()) + TRY(write_sectors_impl(lba + offset, 1, sector_buffer)); } return {}; diff --git a/kernel/kernel/Terminal/TTY.cpp b/kernel/kernel/Terminal/TTY.cpp index fd370573..77024d24 100644 --- a/kernel/kernel/Terminal/TTY.cpp +++ b/kernel/kernel/Terminal/TTY.cpp @@ -95,7 +95,7 @@ namespace Kernel TTY::current()->m_tty_ctrl.semaphore.block(); Input::KeyEvent event; - size_t read = MUST(inode->read(0, &event, sizeof(event))); + size_t read = MUST(inode->read(0, BAN::ByteSpan::from(event))); ASSERT(read == sizeof(event)); TTY::current()->on_key_event(event); } @@ -298,7 +298,7 @@ namespace Kernel putchar_impl(ch); } - BAN::ErrorOr TTY::read_impl(off_t, void* buffer, size_t count) + BAN::ErrorOr TTY::read_impl(off_t, BAN::ByteSpan buffer) { LockGuard _(m_lock); while (!m_output.flush) @@ -314,8 +314,8 @@ namespace Kernel return 0; } - size_t to_copy = BAN::Math::min(count, m_output.bytes); - memcpy(buffer, m_output.buffer.data(), to_copy); + size_t to_copy = BAN::Math::min(buffer.size(), m_output.bytes); + memcpy(buffer.data(), m_output.buffer.data(), to_copy); memmove(m_output.buffer.data(), m_output.buffer.data() + to_copy, m_output.bytes - to_copy); m_output.bytes -= to_copy; @@ -328,12 +328,12 @@ namespace Kernel return to_copy; } - BAN::ErrorOr TTY::write_impl(off_t, const void* buffer, size_t count) + BAN::ErrorOr TTY::write_impl(off_t, BAN::ConstByteSpan buffer) { LockGuard _(m_lock); - for (size_t i = 0; i < count; i++) - putchar(((uint8_t*)buffer)[i]); - return count; + for (size_t i = 0; i < buffer.size(); i++) + putchar(buffer[i]); + return buffer.size(); } bool TTY::has_data_impl() const