mmap.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Based on arch/arm/mm/mmap.c
  4. *
  5. * Copyright (C) 2012 ARM Ltd.
  6. */
  7. #include <linux/elf.h>
  8. #include <linux/fs.h>
  9. #include <linux/memblock.h>
  10. #include <linux/mm.h>
  11. #include <linux/mman.h>
  12. #include <linux/export.h>
  13. #include <linux/shm.h>
  14. #include <linux/sched/signal.h>
  15. #include <linux/sched/mm.h>
  16. #include <linux/io.h>
  17. #include <linux/personality.h>
  18. #include <linux/random.h>
  19. #include <asm/cputype.h>
  20. /*
  21. * You really shouldn't be using read() or write() on /dev/mem. This might go
  22. * away in the future.
  23. */
  24. int valid_phys_addr_range(phys_addr_t addr, size_t size)
  25. {
  26. /*
  27. * Check whether addr is covered by a memory region without the
  28. * MEMBLOCK_NOMAP attribute, and whether that region covers the
  29. * entire range. In theory, this could lead to false negatives
  30. * if the range is covered by distinct but adjacent memory regions
  31. * that only differ in other attributes. However, few of such
  32. * attributes have been defined, and it is debatable whether it
  33. * follows that /dev/mem read() calls should be able traverse
  34. * such boundaries.
  35. */
  36. return memblock_is_region_memory(addr, size) &&
  37. memblock_is_map_memory(addr);
  38. }
  39. /*
  40. * Do not allow /dev/mem mappings beyond the supported physical range.
  41. */
  42. int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
  43. {
  44. return !(((pfn << PAGE_SHIFT) + size) & ~PHYS_MASK);
  45. }
  46. #ifdef CONFIG_STRICT_DEVMEM
  47. #include <linux/ioport.h>
  48. /*
  49. * devmem_is_allowed() checks to see if /dev/mem access to a certain address
  50. * is valid. The argument is a physical page number. We mimic x86 here by
  51. * disallowing access to system RAM as well as device-exclusive MMIO regions.
  52. * This effectively disable read()/write() on /dev/mem.
  53. */
  54. int devmem_is_allowed(unsigned long pfn)
  55. {
  56. if (iomem_is_exclusive(pfn << PAGE_SHIFT))
  57. return 0;
  58. if (!page_is_ram(pfn))
  59. return 1;
  60. return 0;
  61. }
  62. #endif