TestMemoryDeviceMmap.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/mman.h>
  15. #include <unistd.h>
  16. static ALWAYS_INLINE bool mem_chunk(int fd, u64 base, u64 length)
  17. {
  18. u64 mmoffset = base % sysconf(_SC_PAGESIZE);
  19. void* mmp = mmap(NULL, mmoffset + length, PROT_READ, MAP_SHARED, fd, base - mmoffset);
  20. return mmp != MAP_FAILED;
  21. }
  22. TEST_CASE(test_memory_access_device_mmap)
  23. {
  24. int rc = geteuid();
  25. EXPECT_EQ(rc, 0);
  26. int fd = open("/dev/mem", O_RDONLY);
  27. EXPECT_EQ(fd < 0, false);
  28. // FIXME: This is expected to work on QEMU machines (both 440FX and Q35),
  29. // however, it will be much nicer to have some sort of a node in the ProcFS
  30. // to expose physical memory ranges (e820 memory map).
  31. auto result = mem_chunk(fd, 0xe0000, 0x100000 - 0xe0000);
  32. EXPECT_EQ(result, true);
  33. result = mem_chunk(fd, 0x100000, 0x200000 - 0x100000);
  34. EXPECT_EQ(result, false);
  35. result = mem_chunk(fd, 0xf0000, 70000);
  36. EXPECT_EQ(result, false);
  37. result = mem_chunk(fd, 0xfffc0000, 16384);
  38. EXPECT_EQ(result, true);
  39. result = mem_chunk(fd, 0xfffc0000, 0x100000);
  40. EXPECT_EQ(result, false);
  41. }