devmem_is_allowed.c 683 B

123456789101112131415161718192021222324252627
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * A generic version of devmem_is_allowed.
  4. *
  5. * Based on arch/arm64/mm/mmap.c
  6. *
  7. * Copyright (C) 2020 Google, Inc.
  8. * Copyright (C) 2012 ARM Ltd.
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/ioport.h>
  12. /*
  13. * devmem_is_allowed() checks to see if /dev/mem access to a certain address
  14. * is valid. The argument is a physical page number. We mimic x86 here by
  15. * disallowing access to system RAM as well as device-exclusive MMIO regions.
  16. * This effectively disable read()/write() on /dev/mem.
  17. */
  18. int devmem_is_allowed(unsigned long pfn)
  19. {
  20. if (iomem_is_exclusive(PFN_PHYS(pfn)))
  21. return 0;
  22. if (!page_is_ram(pfn))
  23. return 1;
  24. return 0;
  25. }