Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * linux/arch/arm/mm/mmap.c | |
4 | */ | |
1da177e4 LT |
5 | #include <linux/fs.h> |
6 | #include <linux/mm.h> | |
7 | #include <linux/mman.h> | |
8 | #include <linux/shm.h> | |
3f07c014 | 9 | #include <linux/sched/signal.h> |
01042607 | 10 | #include <linux/sched/mm.h> |
09d9bae0 | 11 | #include <linux/io.h> |
df5419a9 | 12 | #include <linux/personality.h> |
cc92c28b | 13 | #include <linux/random.h> |
41dfaa93 | 14 | #include <asm/cachetype.h> |
1da177e4 LT |
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; | |
41dfaa93 RH |
35 | int do_align = 0; |
36 | int aliasing = cache_is_vipt_aliasing(); | |
b80fa3cb | 37 | struct vm_unmapped_area_info info = {}; |
1da177e4 LT |
38 | |
39 | /* | |
40 | * We only need to do colour alignment if either the I or D | |
41dfaa93 | 41 | * caches alias. |
1da177e4 | 42 | */ |
41dfaa93 RH |
43 | if (aliasing) |
44 | do_align = filp || (flags & MAP_SHARED); | |
1da177e4 LT |
45 | |
46 | /* | |
acec0ac0 | 47 | * We enforce the MAP_FIXED case. |
1da177e4 LT |
48 | */ |
49 | if (flags & MAP_FIXED) { | |
e77414e0 AV |
50 | if (aliasing && flags & MAP_SHARED && |
51 | (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)) | |
1da177e4 LT |
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 && | |
1be7107f | 67 | (!vma || addr + len <= vm_start_gap(vma))) |
1da177e4 LT |
68 | return addr; |
69 | } | |
1da177e4 | 70 | |
394ef640 ML |
71 | info.length = len; |
72 | info.low_limit = mm->mmap_base; | |
73 | info.high_limit = TASK_SIZE; | |
74 | info.align_mask = do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0; | |
75 | info.align_offset = pgoff << PAGE_SHIFT; | |
76 | return vm_unmapped_area(&info); | |
1da177e4 LT |
77 | } |
78 | ||
7dbaa466 RH |
79 | unsigned long |
80 | arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, | |
81 | const unsigned long len, const unsigned long pgoff, | |
82 | const unsigned long flags) | |
83 | { | |
84 | struct vm_area_struct *vma; | |
85 | struct mm_struct *mm = current->mm; | |
86 | unsigned long addr = addr0; | |
87 | int do_align = 0; | |
88 | int aliasing = cache_is_vipt_aliasing(); | |
b80fa3cb | 89 | struct vm_unmapped_area_info info = {}; |
7dbaa466 RH |
90 | |
91 | /* | |
92 | * We only need to do colour alignment if either the I or D | |
93 | * caches alias. | |
94 | */ | |
95 | if (aliasing) | |
96 | do_align = filp || (flags & MAP_SHARED); | |
97 | ||
98 | /* requested length too big for entire address space */ | |
99 | if (len > TASK_SIZE) | |
100 | return -ENOMEM; | |
101 | ||
102 | if (flags & MAP_FIXED) { | |
103 | if (aliasing && flags & MAP_SHARED && | |
104 | (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)) | |
105 | return -EINVAL; | |
106 | return addr; | |
107 | } | |
108 | ||
109 | /* requesting a specific address */ | |
110 | if (addr) { | |
111 | if (do_align) | |
112 | addr = COLOUR_ALIGN(addr, pgoff); | |
113 | else | |
114 | addr = PAGE_ALIGN(addr); | |
115 | vma = find_vma(mm, addr); | |
116 | if (TASK_SIZE - len >= addr && | |
1be7107f | 117 | (!vma || addr + len <= vm_start_gap(vma))) |
7dbaa466 RH |
118 | return addr; |
119 | } | |
120 | ||
394ef640 ML |
121 | info.flags = VM_UNMAPPED_AREA_TOPDOWN; |
122 | info.length = len; | |
d8aa712c | 123 | info.low_limit = FIRST_USER_ADDRESS; |
394ef640 ML |
124 | info.high_limit = mm->mmap_base; |
125 | info.align_mask = do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0; | |
126 | info.align_offset = pgoff << PAGE_SHIFT; | |
127 | addr = vm_unmapped_area(&info); | |
7dbaa466 | 128 | |
7dbaa466 RH |
129 | /* |
130 | * A failed mmap() very likely causes application failure, | |
131 | * so fall back to the bottom-up function here. This scenario | |
132 | * can happen with large stack limits and large mmap() | |
133 | * allocations. | |
134 | */ | |
394ef640 ML |
135 | if (addr & ~PAGE_MASK) { |
136 | VM_BUG_ON(addr != -ENOMEM); | |
137 | info.flags = 0; | |
138 | info.low_limit = mm->mmap_base; | |
139 | info.high_limit = TASK_SIZE; | |
140 | addr = vm_unmapped_area(&info); | |
141 | } | |
7dbaa466 RH |
142 | |
143 | return addr; | |
144 | } | |
145 | ||
51635ad2 LB |
146 | /* |
147 | * You really shouldn't be using read() or write() on /dev/mem. This | |
148 | * might go away in the future. | |
149 | */ | |
7e6735c3 | 150 | int valid_phys_addr_range(phys_addr_t addr, size_t size) |
51635ad2 | 151 | { |
9ae3ae0b AR |
152 | if (addr < PHYS_OFFSET) |
153 | return 0; | |
6806bfe1 | 154 | if (addr + size > __pa(high_memory - 1) + 1) |
51635ad2 LB |
155 | return 0; |
156 | ||
157 | return 1; | |
158 | } | |
159 | ||
160 | /* | |
3159f372 | 161 | * Do not allow /dev/mem mappings beyond the supported physical range. |
51635ad2 LB |
162 | */ |
163 | int valid_mmap_phys_addr_range(unsigned long pfn, size_t size) | |
164 | { | |
3159f372 | 165 | return (pfn + (size >> PAGE_SHIFT)) <= (1 + (PHYS_MASK >> PAGE_SHIFT)); |
51635ad2 | 166 | } |