GNU Linux-libre 4.9.328-gnu1
[releases.git] / arch / parisc / kernel / sys_parisc.c
1
2 /*
3  *    PARISC specific syscalls
4  *
5  *    Copyright (C) 1999-2003 Matthew Wilcox <willy at parisc-linux.org>
6  *    Copyright (C) 2000-2003 Paul Bame <bame at parisc-linux.org>
7  *    Copyright (C) 2001 Thomas Bogendoerfer <tsbogend at parisc-linux.org>
8  *    Copyright (C) 1999-2014 Helge Deller <deller@gmx.de>
9  *
10  *
11  *    This program is free software; you can redistribute it and/or modify
12  *    it under the terms of the GNU General Public License as published by
13  *    the Free Software Foundation; either version 2 of the License, or
14  *    (at your option) any later version.
15  *
16  *    This program is distributed in the hope that it will be useful,
17  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *    GNU General Public License for more details.
20  *
21  *    You should have received a copy of the GNU General Public License
22  *    along with this program; if not, write to the Free Software
23  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 /* we construct an artificial offset for the mapping based on the physical
40  * address of the kernel mapping variable */
41 #define GET_LAST_MMAP(filp)             \
42         (filp ? ((unsigned long) filp->f_mapping) >> 8 : 0UL)
43 #define SET_LAST_MMAP(filp, val)        \
44          { /* nothing */ }
45
46 static int get_offset(unsigned int last_mmap)
47 {
48         return (last_mmap & (SHM_COLOUR-1)) >> PAGE_SHIFT;
49 }
50
51 static 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
57 static 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  * Top of mmap area (just below the process stack).
69  */
70
71 static unsigned long mmap_upper_limit(void)
72 {
73         unsigned long stack_base;
74
75         /* Limit stack size - see setup_arg_pages() in fs/exec.c */
76         stack_base = rlimit_max(RLIMIT_STACK);
77         if (stack_base > STACK_SIZE_MAX)
78                 stack_base = STACK_SIZE_MAX;
79
80         /* Add space for stack randomization. */
81         stack_base += (STACK_RND_MASK << PAGE_SHIFT);
82
83         return PAGE_ALIGN(STACK_TOP - stack_base);
84 }
85
86
87 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
88                 unsigned long len, unsigned long pgoff, unsigned long flags)
89 {
90         struct mm_struct *mm = current->mm;
91         struct vm_area_struct *vma, *prev;
92         unsigned long task_size = TASK_SIZE;
93         int do_color_align, last_mmap;
94         struct vm_unmapped_area_info info;
95
96         if (len > task_size)
97                 return -ENOMEM;
98
99         do_color_align = 0;
100         if (filp || (flags & MAP_SHARED))
101                 do_color_align = 1;
102         last_mmap = GET_LAST_MMAP(filp);
103
104         if (flags & MAP_FIXED) {
105                 if ((flags & MAP_SHARED) && last_mmap &&
106                     (addr - shared_align_offset(last_mmap, pgoff))
107                                 & (SHM_COLOUR - 1))
108                         return -EINVAL;
109                 goto found_addr;
110         }
111
112         if (addr) {
113                 if (do_color_align && last_mmap)
114                         addr = COLOR_ALIGN(addr, last_mmap, pgoff);
115                 else
116                         addr = PAGE_ALIGN(addr);
117
118                 vma = find_vma_prev(mm, addr, &prev);
119                 if (task_size - len >= addr &&
120                     (!vma || addr + len <= vm_start_gap(vma)) &&
121                     (!prev || addr >= vm_end_gap(prev)))
122                         goto found_addr;
123         }
124
125         info.flags = 0;
126         info.length = len;
127         info.low_limit = mm->mmap_legacy_base;
128         info.high_limit = mmap_upper_limit();
129         info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
130         info.align_offset = shared_align_offset(last_mmap, pgoff);
131         addr = vm_unmapped_area(&info);
132
133 found_addr:
134         if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
135                 SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
136
137         return addr;
138 }
139
140 unsigned long
141 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
142                           const unsigned long len, const unsigned long pgoff,
143                           const unsigned long flags)
144 {
145         struct vm_area_struct *vma, *prev;
146         struct mm_struct *mm = current->mm;
147         unsigned long addr = addr0;
148         int do_color_align, last_mmap;
149         struct vm_unmapped_area_info info;
150
151 #ifdef CONFIG_64BIT
152         /* This should only ever run for 32-bit processes.  */
153         BUG_ON(!test_thread_flag(TIF_32BIT));
154 #endif
155
156         /* requested length too big for entire address space */
157         if (len > TASK_SIZE)
158                 return -ENOMEM;
159
160         do_color_align = 0;
161         if (filp || (flags & MAP_SHARED))
162                 do_color_align = 1;
163         last_mmap = GET_LAST_MMAP(filp);
164
165         if (flags & MAP_FIXED) {
166                 if ((flags & MAP_SHARED) && last_mmap &&
167                     (addr - shared_align_offset(last_mmap, pgoff))
168                         & (SHM_COLOUR - 1))
169                         return -EINVAL;
170                 goto found_addr;
171         }
172
173         /* requesting a specific address */
174         if (addr) {
175                 if (do_color_align && last_mmap)
176                         addr = COLOR_ALIGN(addr, last_mmap, pgoff);
177                 else
178                         addr = PAGE_ALIGN(addr);
179
180                 vma = find_vma_prev(mm, addr, &prev);
181                 if (TASK_SIZE - len >= addr &&
182                     (!vma || addr + len <= vm_start_gap(vma)) &&
183                     (!prev || addr >= vm_end_gap(prev)))
184                         goto found_addr;
185         }
186
187         info.flags = VM_UNMAPPED_AREA_TOPDOWN;
188         info.length = len;
189         info.low_limit = PAGE_SIZE;
190         info.high_limit = mm->mmap_base;
191         info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
192         info.align_offset = shared_align_offset(last_mmap, pgoff);
193         addr = vm_unmapped_area(&info);
194         if (!(addr & ~PAGE_MASK))
195                 goto found_addr;
196         VM_BUG_ON(addr != -ENOMEM);
197
198         /*
199          * A failed mmap() very likely causes application failure,
200          * so fall back to the bottom-up function here. This scenario
201          * can happen with large stack limits and large mmap()
202          * allocations.
203          */
204         return arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
205
206 found_addr:
207         if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
208                 SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
209
210         return addr;
211 }
212
213 static int mmap_is_legacy(void)
214 {
215         if (current->personality & ADDR_COMPAT_LAYOUT)
216                 return 1;
217
218         /* parisc stack always grows up - so a unlimited stack should
219          * not be an indicator to use the legacy memory layout.
220          * if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
221          *      return 1;
222          */
223
224         return sysctl_legacy_va_layout;
225 }
226
227 static unsigned long mmap_rnd(void)
228 {
229         unsigned long rnd = 0;
230
231         /*
232         *  8 bits of randomness in 32bit mmaps, 20 address space bits
233         * 28 bits of randomness in 64bit mmaps, 40 address space bits
234         */
235         if (current->flags & PF_RANDOMIZE) {
236                 if (is_32bit_task())
237                         rnd = get_random_int() % (1<<8);
238                 else
239                         rnd = get_random_int() % (1<<28);
240         }
241         return rnd << PAGE_SHIFT;
242 }
243
244 static unsigned long mmap_legacy_base(void)
245 {
246         return TASK_UNMAPPED_BASE + mmap_rnd();
247 }
248
249 /*
250  * This function, called very early during the creation of a new
251  * process VM image, sets up which VM layout function to use:
252  */
253 void arch_pick_mmap_layout(struct mm_struct *mm)
254 {
255         mm->mmap_legacy_base = mmap_legacy_base();
256         mm->mmap_base = mmap_upper_limit();
257
258         if (mmap_is_legacy()) {
259                 mm->mmap_base = mm->mmap_legacy_base;
260                 mm->get_unmapped_area = arch_get_unmapped_area;
261         } else {
262                 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
263         }
264 }
265
266
267 asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
268         unsigned long prot, unsigned long flags, unsigned long fd,
269         unsigned long pgoff)
270 {
271         /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
272            we have. */
273         return sys_mmap_pgoff(addr, len, prot, flags, fd,
274                               pgoff >> (PAGE_SHIFT - 12));
275 }
276
277 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
278                 unsigned long prot, unsigned long flags, unsigned long fd,
279                 unsigned long offset)
280 {
281         if (!(offset & ~PAGE_MASK)) {
282                 return sys_mmap_pgoff(addr, len, prot, flags, fd,
283                                         offset >> PAGE_SHIFT);
284         } else {
285                 return -EINVAL;
286         }
287 }
288
289 /* Fucking broken ABI */
290
291 #ifdef CONFIG_64BIT
292 asmlinkage long parisc_truncate64(const char __user * path,
293                                         unsigned int high, unsigned int low)
294 {
295         return sys_truncate(path, (long)high << 32 | low);
296 }
297
298 asmlinkage long parisc_ftruncate64(unsigned int fd,
299                                         unsigned int high, unsigned int low)
300 {
301         return sys_ftruncate(fd, (long)high << 32 | low);
302 }
303
304 /* stubs for the benefit of the syscall_table since truncate64 and truncate 
305  * are identical on LP64 */
306 asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
307 {
308         return sys_truncate(path, length);
309 }
310 asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
311 {
312         return sys_ftruncate(fd, length);
313 }
314 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
315 {
316         return sys_fcntl(fd, cmd, arg);
317 }
318 #else
319
320 asmlinkage long parisc_truncate64(const char __user * path,
321                                         unsigned int high, unsigned int low)
322 {
323         return sys_truncate64(path, (loff_t)high << 32 | low);
324 }
325
326 asmlinkage long parisc_ftruncate64(unsigned int fd,
327                                         unsigned int high, unsigned int low)
328 {
329         return sys_ftruncate64(fd, (loff_t)high << 32 | low);
330 }
331 #endif
332
333 asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
334                                         unsigned int high, unsigned int low)
335 {
336         return sys_pread64(fd, buf, count, (loff_t)high << 32 | low);
337 }
338
339 asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
340                         size_t count, unsigned int high, unsigned int low)
341 {
342         return sys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
343 }
344
345 asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
346                                     size_t count)
347 {
348         return sys_readahead(fd, (loff_t)high << 32 | low, count);
349 }
350
351 asmlinkage long parisc_fadvise64_64(int fd,
352                         unsigned int high_off, unsigned int low_off,
353                         unsigned int high_len, unsigned int low_len, int advice)
354 {
355         return sys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
356                         (loff_t)high_len << 32 | low_len, advice);
357 }
358
359 asmlinkage long parisc_sync_file_range(int fd,
360                         u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
361                         unsigned int flags)
362 {
363         return sys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
364                         (loff_t)hi_nbytes << 32 | lo_nbytes, flags);
365 }
366
367 asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
368                                 u32 lenhi, u32 lenlo)
369 {
370         return sys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
371                              ((u64)lenhi << 32) | lenlo);
372 }
373
374 long parisc_personality(unsigned long personality)
375 {
376         long err;
377
378         if (personality(current->personality) == PER_LINUX32
379             && personality(personality) == PER_LINUX)
380                 personality = (personality & ~PER_MASK) | PER_LINUX32;
381
382         err = sys_personality(personality);
383         if (personality(err) == PER_LINUX32)
384                 err = (err & ~PER_MASK) | PER_LINUX;
385
386         return err;
387 }