TestMemoryDeviceMmap.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Types.h>
  7. #include <LibTest/TestCase.h>
  8. #include <assert.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <inttypes.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <sys/mman.h>
  15. #include <sys/stat.h>
  16. #include <unistd.h>
  17. static u8 read_buffer[0x100000];
  18. static ALWAYS_INLINE bool mem_chunk(int fd, u64 base, u64 length)
  19. {
  20. u64 mmoffset = base % sysconf(_SC_PAGESIZE);
  21. void* mmp = mmap(NULL, mmoffset + length, PROT_READ, MAP_SHARED, fd, base - mmoffset);
  22. if (mmp == MAP_FAILED)
  23. return false;
  24. if (munmap(mmp, mmoffset + length) < 0)
  25. perror("munmap");
  26. return true;
  27. }
  28. enum class ReadResult {
  29. SeekFailure,
  30. ReadFailure,
  31. ReadSuccess,
  32. };
  33. static ALWAYS_INLINE ReadResult read_chunk(int fd, u64 base, u64 length)
  34. {
  35. VERIFY(length <= sizeof(read_buffer));
  36. auto rs = lseek(fd, base, SEEK_SET);
  37. if (rs < 0) {
  38. fprintf(stderr, "Couldn't seek to offset %" PRIi64 " while verifying: %s\n", base, strerror(errno));
  39. return ReadResult::SeekFailure;
  40. }
  41. if (read(fd, read_buffer, length) < 0)
  42. return ReadResult::ReadFailure;
  43. return ReadResult::ReadSuccess;
  44. }
  45. TEST_CASE(test_memory_access_device_read)
  46. {
  47. int rc = geteuid();
  48. EXPECT_EQ(rc, 0);
  49. int fd = open("/dev/mem", O_RDONLY);
  50. EXPECT(fd >= 0);
  51. // FIXME: This is expected to work on QEMU machines (both 440FX and Q35),
  52. // however, it will be much nicer to have some sort of a node in the ProcFS
  53. // to expose physical memory ranges (e820 memory map).
  54. auto read_result = read_chunk(fd, 0x0, 0x100000);
  55. EXPECT_EQ(read_result, ReadResult::ReadFailure);
  56. read_result = read_chunk(fd, 0xe0000, 0x100000 - 0xe0000);
  57. EXPECT_EQ(read_result, ReadResult::ReadSuccess);
  58. read_result = read_chunk(fd, 0x100000, 0x200000 - 0x100000);
  59. EXPECT_EQ(read_result, ReadResult::ReadFailure);
  60. read_result = read_chunk(fd, 0xf0000, 70000);
  61. EXPECT_EQ(read_result, ReadResult::ReadFailure);
  62. read_result = read_chunk(fd, 0xfffc0000, 16384);
  63. EXPECT_EQ(read_result, ReadResult::ReadSuccess);
  64. read_result = read_chunk(fd, 0xfffc0000, 0x100000);
  65. EXPECT_EQ(read_result, ReadResult::ReadFailure);
  66. }
  67. TEST_CASE(test_memory_access_device_mmap)
  68. {
  69. int rc = geteuid();
  70. EXPECT_EQ(rc, 0);
  71. int fd = open("/dev/mem", O_RDONLY);
  72. EXPECT(fd >= 0);
  73. // FIXME: This is expected to work on QEMU machines (both 440FX and Q35),
  74. // however, it will be much nicer to have some sort of a node in the ProcFS
  75. // to expose physical memory ranges (e820 memory map).
  76. auto mmap_result = mem_chunk(fd, 0xe0000, 0x100000 - 0xe0000);
  77. EXPECT_EQ(mmap_result, true);
  78. mmap_result = mem_chunk(fd, 0x100000, 0x200000 - 0x100000);
  79. EXPECT_EQ(mmap_result, false);
  80. mmap_result = mem_chunk(fd, 0xf0000, 70000);
  81. EXPECT_EQ(mmap_result, false);
  82. mmap_result = mem_chunk(fd, 0xfffc0000, 16384);
  83. EXPECT_EQ(mmap_result, true);
  84. mmap_result = mem_chunk(fd, 0xfffc0000, 0x100000);
  85. EXPECT_EQ(mmap_result, false);
  86. }