Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/arch/arm/mm/mmap.c |
| 4 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/fs.h> |
| 6 | #include <linux/mm.h> |
| 7 | #include <linux/mman.h> |
| 8 | #include <linux/shm.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 9 | #include <linux/sched/signal.h> |
Ingo Molnar | 0104260 | 2017-02-08 18:51:31 +0100 | [diff] [blame] | 10 | #include <linux/sched/mm.h> |
Russell King | 09d9bae | 2008-09-05 14:08:44 +0100 | [diff] [blame] | 11 | #include <linux/io.h> |
Nicolas Pitre | df5419a | 2011-04-13 04:57:17 +0100 | [diff] [blame] | 12 | #include <linux/personality.h> |
Nicolas Pitre | cc92c28 | 2010-06-14 21:16:19 -0400 | [diff] [blame] | 13 | #include <linux/random.h> |
Rob Herring | 41dfaa9 | 2011-11-22 04:01:06 +0100 | [diff] [blame] | 14 | #include <asm/cachetype.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | #define COLOUR_ALIGN(addr,pgoff) \ |
| 17 | ((((addr)+SHMLBA-1)&~(SHMLBA-1)) + \ |
| 18 | (((pgoff)<<PAGE_SHIFT) & (SHMLBA-1))) |
| 19 | |
| 20 | /* |
| 21 | * We need to ensure that shared mappings are correctly aligned to |
| 22 | * avoid aliasing issues with VIPT caches. We need to ensure that |
| 23 | * a specific page of an object is always mapped at a multiple of |
| 24 | * SHMLBA bytes. |
| 25 | * |
| 26 | * We unconditionally provide this function for all cases, however |
| 27 | * in the VIVT case, we optimise out the alignment rules. |
| 28 | */ |
| 29 | unsigned long |
| 30 | arch_get_unmapped_area(struct file *filp, unsigned long addr, |
| 31 | unsigned long len, unsigned long pgoff, unsigned long flags) |
| 32 | { |
| 33 | struct mm_struct *mm = current->mm; |
| 34 | struct vm_area_struct *vma; |
Rob Herring | 41dfaa9 | 2011-11-22 04:01:06 +0100 | [diff] [blame] | 35 | int do_align = 0; |
| 36 | int aliasing = cache_is_vipt_aliasing(); |
Michel Lespinasse | 394ef64 | 2012-12-11 16:02:10 -0800 | [diff] [blame] | 37 | struct vm_unmapped_area_info info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * We only need to do colour alignment if either the I or D |
Rob Herring | 41dfaa9 | 2011-11-22 04:01:06 +0100 | [diff] [blame] | 41 | * caches alias. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | */ |
Rob Herring | 41dfaa9 | 2011-11-22 04:01:06 +0100 | [diff] [blame] | 43 | if (aliasing) |
| 44 | do_align = filp || (flags & MAP_SHARED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | /* |
Benjamin Herrenschmidt | acec0ac | 2007-05-06 14:50:07 -0700 | [diff] [blame] | 47 | * We enforce the MAP_FIXED case. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | */ |
| 49 | if (flags & MAP_FIXED) { |
Al Viro | e77414e | 2009-12-05 15:10:44 -0500 | [diff] [blame] | 50 | if (aliasing && flags & MAP_SHARED && |
| 51 | (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | return -EINVAL; |
| 53 | return addr; |
| 54 | } |
| 55 | |
| 56 | if (len > TASK_SIZE) |
| 57 | return -ENOMEM; |
| 58 | |
| 59 | if (addr) { |
| 60 | if (do_align) |
| 61 | addr = COLOUR_ALIGN(addr, pgoff); |
| 62 | else |
| 63 | addr = PAGE_ALIGN(addr); |
| 64 | |
| 65 | vma = find_vma(mm, addr); |
| 66 | if (TASK_SIZE - len >= addr && |
Hugh Dickins | 1be7107 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 67 | (!vma || addr + len <= vm_start_gap(vma))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | return addr; |
| 69 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Michel Lespinasse | 394ef64 | 2012-12-11 16:02:10 -0800 | [diff] [blame] | 71 | info.flags = 0; |
| 72 | info.length = len; |
| 73 | info.low_limit = mm->mmap_base; |
| 74 | info.high_limit = TASK_SIZE; |
| 75 | info.align_mask = do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0; |
| 76 | info.align_offset = pgoff << PAGE_SHIFT; |
| 77 | return vm_unmapped_area(&info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 80 | unsigned long |
| 81 | arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, |
| 82 | const unsigned long len, const unsigned long pgoff, |
| 83 | const unsigned long flags) |
| 84 | { |
| 85 | struct vm_area_struct *vma; |
| 86 | struct mm_struct *mm = current->mm; |
| 87 | unsigned long addr = addr0; |
| 88 | int do_align = 0; |
| 89 | int aliasing = cache_is_vipt_aliasing(); |
Michel Lespinasse | 394ef64 | 2012-12-11 16:02:10 -0800 | [diff] [blame] | 90 | struct vm_unmapped_area_info info; |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 91 | |
| 92 | /* |
| 93 | * We only need to do colour alignment if either the I or D |
| 94 | * caches alias. |
| 95 | */ |
| 96 | if (aliasing) |
| 97 | do_align = filp || (flags & MAP_SHARED); |
| 98 | |
| 99 | /* requested length too big for entire address space */ |
| 100 | if (len > TASK_SIZE) |
| 101 | return -ENOMEM; |
| 102 | |
| 103 | if (flags & MAP_FIXED) { |
| 104 | if (aliasing && flags & MAP_SHARED && |
| 105 | (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)) |
| 106 | return -EINVAL; |
| 107 | return addr; |
| 108 | } |
| 109 | |
| 110 | /* requesting a specific address */ |
| 111 | if (addr) { |
| 112 | if (do_align) |
| 113 | addr = COLOUR_ALIGN(addr, pgoff); |
| 114 | else |
| 115 | addr = PAGE_ALIGN(addr); |
| 116 | vma = find_vma(mm, addr); |
| 117 | if (TASK_SIZE - len >= addr && |
Hugh Dickins | 1be7107 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 118 | (!vma || addr + len <= vm_start_gap(vma))) |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 119 | return addr; |
| 120 | } |
| 121 | |
Michel Lespinasse | 394ef64 | 2012-12-11 16:02:10 -0800 | [diff] [blame] | 122 | info.flags = VM_UNMAPPED_AREA_TOPDOWN; |
| 123 | info.length = len; |
Russell King | d8aa712 | 2013-11-28 21:43:40 +0000 | [diff] [blame] | 124 | info.low_limit = FIRST_USER_ADDRESS; |
Michel Lespinasse | 394ef64 | 2012-12-11 16:02:10 -0800 | [diff] [blame] | 125 | info.high_limit = mm->mmap_base; |
| 126 | info.align_mask = do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0; |
| 127 | info.align_offset = pgoff << PAGE_SHIFT; |
| 128 | addr = vm_unmapped_area(&info); |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 129 | |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 130 | /* |
| 131 | * A failed mmap() very likely causes application failure, |
| 132 | * so fall back to the bottom-up function here. This scenario |
| 133 | * can happen with large stack limits and large mmap() |
| 134 | * allocations. |
| 135 | */ |
Michel Lespinasse | 394ef64 | 2012-12-11 16:02:10 -0800 | [diff] [blame] | 136 | if (addr & ~PAGE_MASK) { |
| 137 | VM_BUG_ON(addr != -ENOMEM); |
| 138 | info.flags = 0; |
| 139 | info.low_limit = mm->mmap_base; |
| 140 | info.high_limit = TASK_SIZE; |
| 141 | addr = vm_unmapped_area(&info); |
| 142 | } |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 143 | |
| 144 | return addr; |
| 145 | } |
| 146 | |
Lennert Buytenhek | 51635ad | 2006-09-16 10:50:22 +0100 | [diff] [blame] | 147 | /* |
| 148 | * You really shouldn't be using read() or write() on /dev/mem. This |
| 149 | * might go away in the future. |
| 150 | */ |
Cyril Chemparathy | 7e6735c | 2012-09-12 14:05:58 -0400 | [diff] [blame] | 151 | int valid_phys_addr_range(phys_addr_t addr, size_t size) |
Lennert Buytenhek | 51635ad | 2006-09-16 10:50:22 +0100 | [diff] [blame] | 152 | { |
Alexandre Rusev | 9ae3ae0 | 2008-02-26 18:42:10 +0100 | [diff] [blame] | 153 | if (addr < PHYS_OFFSET) |
| 154 | return 0; |
Greg Ungerer | 6806bfe | 2009-10-02 00:45:28 +0100 | [diff] [blame] | 155 | if (addr + size > __pa(high_memory - 1) + 1) |
Lennert Buytenhek | 51635ad | 2006-09-16 10:50:22 +0100 | [diff] [blame] | 156 | return 0; |
| 157 | |
| 158 | return 1; |
| 159 | } |
| 160 | |
| 161 | /* |
Sergey Dyasly | 3159f37 | 2013-09-24 16:38:00 +0100 | [diff] [blame] | 162 | * Do not allow /dev/mem mappings beyond the supported physical range. |
Lennert Buytenhek | 51635ad | 2006-09-16 10:50:22 +0100 | [diff] [blame] | 163 | */ |
| 164 | int valid_mmap_phys_addr_range(unsigned long pfn, size_t size) |
| 165 | { |
Sergey Dyasly | 3159f37 | 2013-09-24 16:38:00 +0100 | [diff] [blame] | 166 | return (pfn + (size >> PAGE_SHIFT)) <= (1 + (PHYS_MASK >> PAGE_SHIFT)); |
Lennert Buytenhek | 51635ad | 2006-09-16 10:50:22 +0100 | [diff] [blame] | 167 | } |