MemoryDevice.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Devices/DeviceManagement.h>
  7. #include <Kernel/Devices/Generic/MemoryDevice.h>
  8. #include <Kernel/Memory/AnonymousVMObject.h>
  9. #include <Kernel/Memory/TypedMapping.h>
  10. #include <Kernel/Sections.h>
  11. namespace Kernel {
  12. UNMAP_AFTER_INIT NonnullLockRefPtr<MemoryDevice> MemoryDevice::must_create()
  13. {
  14. auto memory_device_or_error = DeviceManagement::try_create_device<MemoryDevice>();
  15. // FIXME: Find a way to propagate errors
  16. VERIFY(!memory_device_or_error.is_error());
  17. return memory_device_or_error.release_value();
  18. }
  19. UNMAP_AFTER_INIT MemoryDevice::MemoryDevice()
  20. : CharacterDevice(1, 1)
  21. {
  22. }
  23. UNMAP_AFTER_INIT MemoryDevice::~MemoryDevice() = default;
  24. ErrorOr<size_t> MemoryDevice::read(OpenFileDescription&, u64 offset, UserOrKernelBuffer& buffer, size_t length)
  25. {
  26. if (!MM.is_allowed_to_read_physical_memory_for_userspace(PhysicalAddress(offset), length)) {
  27. dbgln_if(MEMORY_DEVICE_DEBUG, "MemoryDevice: Trying to read physical memory at {} for range of {} bytes failed due to violation of access", PhysicalAddress(offset), length);
  28. return EINVAL;
  29. }
  30. auto mapping = TRY(Memory::map_typed<u8>(PhysicalAddress(offset), length));
  31. auto bytes = ReadonlyBytes { mapping.ptr(), length };
  32. TRY(buffer.write(bytes));
  33. return length;
  34. }
  35. ErrorOr<NonnullLockRefPtr<Memory::VMObject>> MemoryDevice::vmobject_for_mmap(Process&, Memory::VirtualRange const& range, u64& offset, bool)
  36. {
  37. auto viewed_address = PhysicalAddress(offset);
  38. // Note: This check happens to guard against possible memory leak.
  39. // For example, if we try to mmap physical memory from 0x1000 to 0x2000 and you
  40. // can actually mmap only from 0x1001, then we would fail as usual.
  41. // However, in such case if we mmap from 0x1002, we are technically not violating
  42. // any rules, besides the fact that we mapped an entire page with two bytes which we
  43. // were not supposed to see. To prevent that, if we use mmap(2) syscall, we should
  44. // always consider the start page to be aligned on PAGE_SIZE, or to be more precise
  45. // is to be set to the page base of that start address.
  46. VERIFY(viewed_address == viewed_address.page_base());
  47. if (!MM.is_allowed_to_read_physical_memory_for_userspace(viewed_address, range.size())) {
  48. dbgln_if(MEMORY_DEVICE_DEBUG, "MemoryDevice: Trying to mmap physical memory at {} for range of {} bytes failed due to violation of access", viewed_address, range.size());
  49. return EINVAL;
  50. }
  51. offset = 0;
  52. return TRY(Memory::AnonymousVMObject::try_create_for_physical_range(viewed_address, range.size()));
  53. }
  54. }