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.
This commit is contained in:
Liav A 2021-12-23 21:49:31 +02:00 committed by Idan Horowitz
parent 6feb07fe43
commit ca254699ec
Notes: sideshowbarker 2024-07-17 21:27:34 +09:00
4 changed files with 81 additions and 18 deletions

View file

@ -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;
}

View file

@ -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;

View file

@ -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&);

View file

@ -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);
}