diff options
author | Liav A <liavalb@gmail.com> | 2021-12-23 21:49:31 +0200 |
---|---|---|
committer | Idan Horowitz <idan.horowitz@gmail.com> | 2022-01-08 13:21:16 +0200 |
commit | ca254699ec31b26441b5bbf5e2894a4f107bd7c9 (patch) | |
tree | e43b46bc75b839bf3dd67a1ed1db17a5552cb5fd | |
parent | 6feb07fe43fdd17739faf3704202d8c601f79796 (diff) | |
download | serenity-ca254699ec31b26441b5bbf5e2894a4f107bd7c9.zip |
Kernel: Implement read functionality for MemoryDevice
So far we only had mmap(2) functionality on the /dev/mem device, but now
we can also do read(2) on it.
The test unit was updated to check we are doing it safely.
-rw-r--r-- | Kernel/Devices/MemoryDevice.cpp | 15 | ||||
-rw-r--r-- | Kernel/Memory/MemoryManager.cpp | 6 | ||||
-rw-r--r-- | Kernel/Memory/MemoryManager.h | 2 | ||||
-rw-r--r-- | Tests/Kernel/TestMemoryDeviceMmap.cpp | 76 |
4 files changed, 81 insertions, 18 deletions
diff --git a/Kernel/Devices/MemoryDevice.cpp b/Kernel/Devices/MemoryDevice.cpp index d17bce0a9a..c94667a601 100644 --- a/Kernel/Devices/MemoryDevice.cpp +++ b/Kernel/Devices/MemoryDevice.cpp @@ -10,6 +10,7 @@ #include <Kernel/Devices/MemoryDevice.h> #include <Kernel/Firmware/BIOS.h> #include <Kernel/Memory/AnonymousVMObject.h> +#include <Kernel/Memory/TypedMapping.h> #include <Kernel/Sections.h> namespace Kernel { @@ -31,9 +32,17 @@ UNMAP_AFTER_INIT MemoryDevice::~MemoryDevice() { } -ErrorOr<size_t> MemoryDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) +ErrorOr<size_t> MemoryDevice::read(OpenFileDescription&, u64 offset, UserOrKernelBuffer& buffer, size_t length) { - TODO(); + if (!MM.is_allowed_to_read_physical_memory_for_userspace(PhysicalAddress(offset), length)) { + dbgln("MemoryDevice: Trying to read physical memory at {} for range of {} bytes failed due to violation of access", PhysicalAddress(offset), length); + return EINVAL; + } + auto mapping = Memory::map_typed<u8>(PhysicalAddress(offset), length); + + auto bytes = ReadonlyBytes { mapping.ptr(), length }; + TRY(buffer.write(bytes)); + return length; } ErrorOr<Memory::Region*> MemoryDevice::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared) @@ -41,7 +50,7 @@ ErrorOr<Memory::Region*> MemoryDevice::mmap(Process& process, OpenFileDescriptio auto viewed_address = PhysicalAddress(offset); dbgln("MemoryDevice: Trying to mmap physical memory at {} for range of {} bytes", viewed_address, range.size()); - if (!MM.is_allowed_to_mmap_physical_memory_to_userspace(viewed_address, range)) { + if (!MM.is_allowed_to_read_physical_memory_for_userspace(viewed_address, range.size())) { dbgln("MemoryDevice: Trying to mmap physical memory at {} for range of {} bytes failed due to violation of access", viewed_address, range.size()); return EINVAL; } diff --git a/Kernel/Memory/MemoryManager.cpp b/Kernel/Memory/MemoryManager.cpp index 3e14f503b7..8b06e7732d 100644 --- a/Kernel/Memory/MemoryManager.cpp +++ b/Kernel/Memory/MemoryManager.cpp @@ -209,13 +209,13 @@ UNMAP_AFTER_INIT void MemoryManager::register_reserved_ranges() m_reserved_memory_ranges.append(ContiguousReservedMemoryRange { range.start, m_physical_memory_ranges.last().start.get() + m_physical_memory_ranges.last().length - range.start.get() }); } -bool MemoryManager::is_allowed_to_mmap_physical_memory_to_userspace(PhysicalAddress start_address, VirtualRange const& range) const +bool MemoryManager::is_allowed_to_read_physical_memory_for_userspace(PhysicalAddress start_address, size_t read_length) const { // Note: Guard against overflow in case someone tries to mmap on the edge of // the RAM - if (start_address.offset_addition_would_overflow(range.size())) + if (start_address.offset_addition_would_overflow(read_length)) return false; - auto end_address = start_address.offset(range.size()); + auto end_address = start_address.offset(read_length); for (auto& current_range : m_reserved_memory_ranges) { if (current_range.start > start_address) continue; diff --git a/Kernel/Memory/MemoryManager.h b/Kernel/Memory/MemoryManager.h index 761419c461..fb0892d833 100644 --- a/Kernel/Memory/MemoryManager.h +++ b/Kernel/Memory/MemoryManager.h @@ -230,7 +230,7 @@ public: PageDirectory& kernel_page_directory() { return *m_kernel_page_directory; } Vector<UsedMemoryRange> const& used_memory_ranges() { return m_used_memory_ranges; } - bool is_allowed_to_mmap_physical_memory_to_userspace(PhysicalAddress, VirtualRange const&) const; + bool is_allowed_to_read_physical_memory_for_userspace(PhysicalAddress, size_t read_length) const; PhysicalPageEntry& get_physical_page_entry(PhysicalAddress); PhysicalAddress get_physical_address(PhysicalPage const&); diff --git a/Tests/Kernel/TestMemoryDeviceMmap.cpp b/Tests/Kernel/TestMemoryDeviceMmap.cpp index 64a8605cb0..ef4a21ee80 100644 --- a/Tests/Kernel/TestMemoryDeviceMmap.cpp +++ b/Tests/Kernel/TestMemoryDeviceMmap.cpp @@ -9,12 +9,16 @@ #include <assert.h> #include <errno.h> #include <fcntl.h> +#include <inttypes.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/mman.h> +#include <sys/stat.h> #include <unistd.h> +static u8 read_buffer[0x100000]; + static ALWAYS_INLINE bool mem_chunk(int fd, u64 base, u64 length) { u64 mmoffset = base % sysconf(_SC_PAGESIZE); @@ -22,30 +26,80 @@ static ALWAYS_INLINE bool mem_chunk(int fd, u64 base, u64 length) return mmp != MAP_FAILED; } +enum class ReadResult { + SeekFailure, + ReadFailure, + ReadSuccess, +}; + +static ALWAYS_INLINE ReadResult read_chunk(int fd, u64 base, u64 length) +{ + VERIFY(length <= sizeof(read_buffer)); + auto rs = lseek(fd, base, SEEK_SET); + if (rs < 0) { + fprintf(stderr, "Couldn't seek to offset %" PRIi64 " while verifying: %s\n", base, strerror(errno)); + return ReadResult::SeekFailure; + } + if (read(fd, read_buffer, length) < 0) + return ReadResult::ReadFailure; + return ReadResult::ReadSuccess; +} + +TEST_CASE(test_memory_access_device_read) +{ + int rc = geteuid(); + EXPECT_EQ(rc, 0); + + int fd = open("/dev/mem", O_RDONLY); + EXPECT(fd >= 0); + + // FIXME: This is expected to work on QEMU machines (both 440FX and Q35), + // however, it will be much nicer to have some sort of a node in the ProcFS + // to expose physical memory ranges (e820 memory map). + + auto read_result = read_chunk(fd, 0x0, 0x100000); + EXPECT_EQ(read_result, ReadResult::ReadFailure); + + read_result = read_chunk(fd, 0xe0000, 0x100000 - 0xe0000); + EXPECT_EQ(read_result, ReadResult::ReadSuccess); + + read_result = read_chunk(fd, 0x100000, 0x200000 - 0x100000); + EXPECT_EQ(read_result, ReadResult::ReadFailure); + + read_result = read_chunk(fd, 0xf0000, 70000); + EXPECT_EQ(read_result, ReadResult::ReadFailure); + + read_result = read_chunk(fd, 0xfffc0000, 16384); + EXPECT_EQ(read_result, ReadResult::ReadSuccess); + + read_result = read_chunk(fd, 0xfffc0000, 0x100000); + EXPECT_EQ(read_result, ReadResult::ReadFailure); +} + TEST_CASE(test_memory_access_device_mmap) { int rc = geteuid(); EXPECT_EQ(rc, 0); int fd = open("/dev/mem", O_RDONLY); - EXPECT_EQ(fd < 0, false); + EXPECT(fd >= 0); // FIXME: This is expected to work on QEMU machines (both 440FX and Q35), // however, it will be much nicer to have some sort of a node in the ProcFS // to expose physical memory ranges (e820 memory map). - auto result = mem_chunk(fd, 0xe0000, 0x100000 - 0xe0000); - EXPECT_EQ(result, true); + auto mmap_result = mem_chunk(fd, 0xe0000, 0x100000 - 0xe0000); + EXPECT_EQ(mmap_result, true); - result = mem_chunk(fd, 0x100000, 0x200000 - 0x100000); - EXPECT_EQ(result, false); + mmap_result = mem_chunk(fd, 0x100000, 0x200000 - 0x100000); + EXPECT_EQ(mmap_result, false); - result = mem_chunk(fd, 0xf0000, 70000); - EXPECT_EQ(result, false); + mmap_result = mem_chunk(fd, 0xf0000, 70000); + EXPECT_EQ(mmap_result, false); - result = mem_chunk(fd, 0xfffc0000, 16384); - EXPECT_EQ(result, true); + mmap_result = mem_chunk(fd, 0xfffc0000, 16384); + EXPECT_EQ(mmap_result, true); - result = mem_chunk(fd, 0xfffc0000, 0x100000); - EXPECT_EQ(result, false); + mmap_result = mem_chunk(fd, 0xfffc0000, 0x100000); + EXPECT_EQ(mmap_result, false); } |