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