MemoryDevice.cpp 2.9 KB

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