18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Based on arch/arm/mm/mmap.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2012 ARM Ltd.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/elf.h>
98c2ecf20Sopenharmony_ci#include <linux/fs.h>
108c2ecf20Sopenharmony_ci#include <linux/memblock.h>
118c2ecf20Sopenharmony_ci#include <linux/mm.h>
128c2ecf20Sopenharmony_ci#include <linux/mman.h>
138c2ecf20Sopenharmony_ci#include <linux/export.h>
148c2ecf20Sopenharmony_ci#include <linux/shm.h>
158c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
168c2ecf20Sopenharmony_ci#include <linux/sched/mm.h>
178c2ecf20Sopenharmony_ci#include <linux/io.h>
188c2ecf20Sopenharmony_ci#include <linux/personality.h>
198c2ecf20Sopenharmony_ci#include <linux/random.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include <asm/cputype.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/*
248c2ecf20Sopenharmony_ci * You really shouldn't be using read() or write() on /dev/mem.  This might go
258c2ecf20Sopenharmony_ci * away in the future.
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ciint valid_phys_addr_range(phys_addr_t addr, size_t size)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	/*
308c2ecf20Sopenharmony_ci	 * Check whether addr is covered by a memory region without the
318c2ecf20Sopenharmony_ci	 * MEMBLOCK_NOMAP attribute, and whether that region covers the
328c2ecf20Sopenharmony_ci	 * entire range. In theory, this could lead to false negatives
338c2ecf20Sopenharmony_ci	 * if the range is covered by distinct but adjacent memory regions
348c2ecf20Sopenharmony_ci	 * that only differ in other attributes. However, few of such
358c2ecf20Sopenharmony_ci	 * attributes have been defined, and it is debatable whether it
368c2ecf20Sopenharmony_ci	 * follows that /dev/mem read() calls should be able traverse
378c2ecf20Sopenharmony_ci	 * such boundaries.
388c2ecf20Sopenharmony_ci	 */
398c2ecf20Sopenharmony_ci	return memblock_is_region_memory(addr, size) &&
408c2ecf20Sopenharmony_ci	       memblock_is_map_memory(addr);
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/*
448c2ecf20Sopenharmony_ci * Do not allow /dev/mem mappings beyond the supported physical range.
458c2ecf20Sopenharmony_ci */
468c2ecf20Sopenharmony_ciint valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	return !(((pfn << PAGE_SHIFT) + size) & ~PHYS_MASK);
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#ifdef CONFIG_STRICT_DEVMEM
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#include <linux/ioport.h>
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/*
568c2ecf20Sopenharmony_ci * devmem_is_allowed() checks to see if /dev/mem access to a certain address
578c2ecf20Sopenharmony_ci * is valid. The argument is a physical page number.  We mimic x86 here by
588c2ecf20Sopenharmony_ci * disallowing access to system RAM as well as device-exclusive MMIO regions.
598c2ecf20Sopenharmony_ci * This effectively disable read()/write() on /dev/mem.
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_ciint devmem_is_allowed(unsigned long pfn)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	if (iomem_is_exclusive(pfn << PAGE_SHIFT))
648c2ecf20Sopenharmony_ci		return 0;
658c2ecf20Sopenharmony_ci	if (!page_is_ram(pfn))
668c2ecf20Sopenharmony_ci		return 1;
678c2ecf20Sopenharmony_ci	return 0;
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#endif
71