1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <asm/uaccess.h>
27#include <asm/elf.h>
28#include <linux/file.h>
29#include <linux/fs.h>
30#include <linux/linkage.h>
31#include <linux/mm.h>
32#include <linux/mman.h>
33#include <linux/shm.h>
34#include <linux/syscalls.h>
35#include <linux/utsname.h>
36#include <linux/personality.h>
37#include <linux/random.h>
38
39
40
41#define GET_LAST_MMAP(filp) \
42 (filp ? ((unsigned long) filp->f_mapping) >> 8 : 0UL)
43#define SET_LAST_MMAP(filp, val) \
44 { }
45
46static int get_offset(unsigned int last_mmap)
47{
48 return (last_mmap & (SHM_COLOUR-1)) >> PAGE_SHIFT;
49}
50
51static unsigned long shared_align_offset(unsigned int last_mmap,
52 unsigned long pgoff)
53{
54 return (get_offset(last_mmap) + pgoff) << PAGE_SHIFT;
55}
56
57static inline unsigned long COLOR_ALIGN(unsigned long addr,
58 unsigned int last_mmap, unsigned long pgoff)
59{
60 unsigned long base = (addr+SHM_COLOUR-1) & ~(SHM_COLOUR-1);
61 unsigned long off = (SHM_COLOUR-1) &
62 (shared_align_offset(last_mmap, pgoff) << PAGE_SHIFT);
63
64 return base + off;
65}
66
67
68
69
70
71static unsigned long mmap_upper_limit(void)
72{
73 unsigned long stack_base;
74
75
76 stack_base = rlimit_max(RLIMIT_STACK);
77 if (stack_base > STACK_SIZE_MAX)
78 stack_base = STACK_SIZE_MAX;
79
80 return PAGE_ALIGN(STACK_TOP - stack_base);
81}
82
83
84unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
85 unsigned long len, unsigned long pgoff, unsigned long flags)
86{
87 struct mm_struct *mm = current->mm;
88 struct vm_area_struct *vma;
89 unsigned long task_size = TASK_SIZE;
90 int do_color_align, last_mmap;
91 struct vm_unmapped_area_info info;
92
93 if (len > task_size)
94 return -ENOMEM;
95
96 do_color_align = 0;
97 if (filp || (flags & MAP_SHARED))
98 do_color_align = 1;
99 last_mmap = GET_LAST_MMAP(filp);
100
101 if (flags & MAP_FIXED) {
102 if ((flags & MAP_SHARED) && last_mmap &&
103 (addr - shared_align_offset(last_mmap, pgoff))
104 & (SHM_COLOUR - 1))
105 return -EINVAL;
106 goto found_addr;
107 }
108
109 if (addr) {
110 if (do_color_align && last_mmap)
111 addr = COLOR_ALIGN(addr, last_mmap, pgoff);
112 else
113 addr = PAGE_ALIGN(addr);
114
115 vma = find_vma(mm, addr);
116 if (task_size - len >= addr &&
117 (!vma || addr + len <= vma->vm_start))
118 goto found_addr;
119 }
120
121 info.flags = 0;
122 info.length = len;
123 info.low_limit = mm->mmap_legacy_base;
124 info.high_limit = mmap_upper_limit();
125 info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
126 info.align_offset = shared_align_offset(last_mmap, pgoff);
127 addr = vm_unmapped_area(&info);
128
129found_addr:
130 if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
131 SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
132
133 return addr;
134}
135
136unsigned long
137arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
138 const unsigned long len, const unsigned long pgoff,
139 const unsigned long flags)
140{
141 struct vm_area_struct *vma;
142 struct mm_struct *mm = current->mm;
143 unsigned long addr = addr0;
144 int do_color_align, last_mmap;
145 struct vm_unmapped_area_info info;
146
147#ifdef CONFIG_64BIT
148
149 BUG_ON(!test_thread_flag(TIF_32BIT));
150#endif
151
152
153 if (len > TASK_SIZE)
154 return -ENOMEM;
155
156 do_color_align = 0;
157 if (filp || (flags & MAP_SHARED))
158 do_color_align = 1;
159 last_mmap = GET_LAST_MMAP(filp);
160
161 if (flags & MAP_FIXED) {
162 if ((flags & MAP_SHARED) && last_mmap &&
163 (addr - shared_align_offset(last_mmap, pgoff))
164 & (SHM_COLOUR - 1))
165 return -EINVAL;
166 goto found_addr;
167 }
168
169
170 if (addr) {
171 if (do_color_align && last_mmap)
172 addr = COLOR_ALIGN(addr, last_mmap, pgoff);
173 else
174 addr = PAGE_ALIGN(addr);
175 vma = find_vma(mm, addr);
176 if (TASK_SIZE - len >= addr &&
177 (!vma || addr + len <= vma->vm_start))
178 goto found_addr;
179 }
180
181 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
182 info.length = len;
183 info.low_limit = PAGE_SIZE;
184 info.high_limit = mm->mmap_base;
185 info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
186 info.align_offset = shared_align_offset(last_mmap, pgoff);
187 addr = vm_unmapped_area(&info);
188 if (!(addr & ~PAGE_MASK))
189 goto found_addr;
190 VM_BUG_ON(addr != -ENOMEM);
191
192
193
194
195
196
197
198 return arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
199
200found_addr:
201 if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
202 SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
203
204 return addr;
205}
206
207static int mmap_is_legacy(void)
208{
209 if (current->personality & ADDR_COMPAT_LAYOUT)
210 return 1;
211
212
213
214
215
216
217
218 return sysctl_legacy_va_layout;
219}
220
221static unsigned long mmap_rnd(void)
222{
223 unsigned long rnd = 0;
224
225
226
227
228
229 if (current->flags & PF_RANDOMIZE) {
230 if (is_32bit_task())
231 rnd = get_random_int() % (1<<8);
232 else
233 rnd = get_random_int() % (1<<28);
234 }
235 return rnd << PAGE_SHIFT;
236}
237
238static unsigned long mmap_legacy_base(void)
239{
240 return TASK_UNMAPPED_BASE + mmap_rnd();
241}
242
243
244
245
246
247void arch_pick_mmap_layout(struct mm_struct *mm)
248{
249 mm->mmap_legacy_base = mmap_legacy_base();
250 mm->mmap_base = mmap_upper_limit();
251
252 if (mmap_is_legacy()) {
253 mm->mmap_base = mm->mmap_legacy_base;
254 mm->get_unmapped_area = arch_get_unmapped_area;
255 } else {
256 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
257 }
258}
259
260
261asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
262 unsigned long prot, unsigned long flags, unsigned long fd,
263 unsigned long pgoff)
264{
265
266
267 return sys_mmap_pgoff(addr, len, prot, flags, fd,
268 pgoff >> (PAGE_SHIFT - 12));
269}
270
271asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
272 unsigned long prot, unsigned long flags, unsigned long fd,
273 unsigned long offset)
274{
275 if (!(offset & ~PAGE_MASK)) {
276 return sys_mmap_pgoff(addr, len, prot, flags, fd,
277 offset >> PAGE_SHIFT);
278 } else {
279 return -EINVAL;
280 }
281}
282
283
284
285#ifdef CONFIG_64BIT
286asmlinkage long parisc_truncate64(const char __user * path,
287 unsigned int high, unsigned int low)
288{
289 return sys_truncate(path, (long)high << 32 | low);
290}
291
292asmlinkage long parisc_ftruncate64(unsigned int fd,
293 unsigned int high, unsigned int low)
294{
295 return sys_ftruncate(fd, (long)high << 32 | low);
296}
297
298
299
300asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
301{
302 return sys_truncate(path, length);
303}
304asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
305{
306 return sys_ftruncate(fd, length);
307}
308asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
309{
310 return sys_fcntl(fd, cmd, arg);
311}
312#else
313
314asmlinkage long parisc_truncate64(const char __user * path,
315 unsigned int high, unsigned int low)
316{
317 return sys_truncate64(path, (loff_t)high << 32 | low);
318}
319
320asmlinkage long parisc_ftruncate64(unsigned int fd,
321 unsigned int high, unsigned int low)
322{
323 return sys_ftruncate64(fd, (loff_t)high << 32 | low);
324}
325#endif
326
327asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
328 unsigned int high, unsigned int low)
329{
330 return sys_pread64(fd, buf, count, (loff_t)high << 32 | low);
331}
332
333asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
334 size_t count, unsigned int high, unsigned int low)
335{
336 return sys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
337}
338
339asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
340 size_t count)
341{
342 return sys_readahead(fd, (loff_t)high << 32 | low, count);
343}
344
345asmlinkage long parisc_fadvise64_64(int fd,
346 unsigned int high_off, unsigned int low_off,
347 unsigned int high_len, unsigned int low_len, int advice)
348{
349 return sys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
350 (loff_t)high_len << 32 | low_len, advice);
351}
352
353asmlinkage long parisc_sync_file_range(int fd,
354 u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
355 unsigned int flags)
356{
357 return sys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
358 (loff_t)hi_nbytes << 32 | lo_nbytes, flags);
359}
360
361asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
362 u32 lenhi, u32 lenlo)
363{
364 return sys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
365 ((u64)lenhi << 32) | lenlo);
366}
367
368asmlinkage unsigned long sys_alloc_hugepages(int key, unsigned long addr, unsigned long len, int prot, int flag)
369{
370 return -ENOMEM;
371}
372
373asmlinkage int sys_free_hugepages(unsigned long addr)
374{
375 return -EINVAL;
376}
377
378long parisc_personality(unsigned long personality)
379{
380 long err;
381
382 if (personality(current->personality) == PER_LINUX32
383 && personality(personality) == PER_LINUX)
384 personality = (personality & ~PER_MASK) | PER_LINUX32;
385
386 err = sys_personality(personality);
387 if (personality(err) == PER_LINUX32)
388 err = (err & ~PER_MASK) | PER_LINUX;
389
390 return err;
391}
392