mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:57:35 +00:00
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:
parent
6feb07fe43
commit
ca254699ec
4 changed files with 81 additions and 18 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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&);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue