blob: 4407ac4c1d84396aa17fe7eba988844c7e790114 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
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>
Helge Deller9dabf602014-01-31 22:19:52 +01008 * Copyright (C) 1999-2014 Helge Deller <deller@gmx.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080026#include <linux/uaccess.h>
Helge Deller9dabf602014-01-31 22:19:52 +010027#include <asm/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/file.h>
29#include <linux/fs.h>
30#include <linux/linkage.h>
31#include <linux/mm.h>
32#include <linux/mman.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010033#include <linux/sched/signal.h>
Ingo Molnar01042602017-02-08 18:51:31 +010034#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/shm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/syscalls.h>
Kyle McMartin75a49582006-08-27 11:12:13 -040037#include <linux/utsname.h>
38#include <linux/personality.h>
Helge Deller9dabf602014-01-31 22:19:52 +010039#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Helge Deller9dabf602014-01-31 22:19:52 +010041/* we construct an artificial offset for the mapping based on the physical
42 * address of the kernel mapping variable */
43#define GET_LAST_MMAP(filp) \
44 (filp ? ((unsigned long) filp->f_mapping) >> 8 : 0UL)
45#define SET_LAST_MMAP(filp, val) \
46 { /* nothing */ }
47
48static int get_offset(unsigned int last_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Helge Deller0ef36bd2014-04-09 19:49:28 +020050 return (last_mmap & (SHM_COLOUR-1)) >> PAGE_SHIFT;
Helge Deller9dabf602014-01-31 22:19:52 +010051}
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Helge Deller9dabf602014-01-31 22:19:52 +010053static unsigned long shared_align_offset(unsigned int last_mmap,
54 unsigned long pgoff)
55{
56 return (get_offset(last_mmap) + pgoff) << PAGE_SHIFT;
57}
58
59static inline unsigned long COLOR_ALIGN(unsigned long addr,
60 unsigned int last_mmap, unsigned long pgoff)
61{
Helge Deller0ef36bd2014-04-09 19:49:28 +020062 unsigned long base = (addr+SHM_COLOUR-1) & ~(SHM_COLOUR-1);
63 unsigned long off = (SHM_COLOUR-1) &
Helge Deller9dabf602014-01-31 22:19:52 +010064 (shared_align_offset(last_mmap, pgoff) << PAGE_SHIFT);
65
66 return base + off;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
Helge Deller9dabf602014-01-31 22:19:52 +010070 * Top of mmap area (just below the process stack).
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
Helge Deller9dabf602014-01-31 22:19:52 +010072
Kees Cook8f2af152018-04-10 16:34:53 -070073/*
74 * When called from arch_get_unmapped_area(), rlim_stack will be NULL,
75 * indicating that "current" should be used instead of a passed-in
76 * value from the exec bprm as done with arch_pick_mmap_layout().
77 */
78static unsigned long mmap_upper_limit(struct rlimit *rlim_stack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Helge Deller9dabf602014-01-31 22:19:52 +010080 unsigned long stack_base;
81
Helge Deller042d27a2014-04-30 23:26:02 +020082 /* Limit stack size - see setup_arg_pages() in fs/exec.c */
Kees Cook8f2af152018-04-10 16:34:53 -070083 stack_base = rlim_stack ? rlim_stack->rlim_max
84 : rlimit_max(RLIMIT_STACK);
Helge Deller042d27a2014-04-30 23:26:02 +020085 if (stack_base > STACK_SIZE_MAX)
86 stack_base = STACK_SIZE_MAX;
Helge Deller9dabf602014-01-31 22:19:52 +010087
Helge Dellerd045c772015-05-11 22:01:27 +020088 /* Add space for stack randomization. */
Alexandre Ghiti17d98222019-04-04 02:26:22 -040089 if (current->flags & PF_RANDOMIZE)
90 stack_base += (STACK_RND_MASK << PAGE_SHIFT);
Helge Dellerd045c772015-05-11 22:01:27 +020091
Helge Deller9dabf602014-01-31 22:19:52 +010092 return PAGE_ALIGN(STACK_TOP - stack_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
97 unsigned long len, unsigned long pgoff, unsigned long flags)
98{
Helge Deller9dabf602014-01-31 22:19:52 +010099 struct mm_struct *mm = current->mm;
Hugh Dickins1be71072017-06-19 04:03:24 -0700100 struct vm_area_struct *vma, *prev;
Helge Deller9dabf602014-01-31 22:19:52 +0100101 unsigned long task_size = TASK_SIZE;
102 int do_color_align, last_mmap;
103 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Helge Deller9dabf602014-01-31 22:19:52 +0100105 if (len > task_size)
106 return -ENOMEM;
107
108 do_color_align = 0;
Helge Deller0576da22013-11-20 23:07:42 +0100109 if (filp || (flags & MAP_SHARED))
Helge Deller9dabf602014-01-31 22:19:52 +0100110 do_color_align = 1;
111 last_mmap = GET_LAST_MMAP(filp);
112
113 if (flags & MAP_FIXED) {
114 if ((flags & MAP_SHARED) && last_mmap &&
115 (addr - shared_align_offset(last_mmap, pgoff))
Helge Deller0ef36bd2014-04-09 19:49:28 +0200116 & (SHM_COLOUR - 1))
Helge Deller9dabf602014-01-31 22:19:52 +0100117 return -EINVAL;
118 goto found_addr;
119 }
120
121 if (addr) {
122 if (do_color_align && last_mmap)
123 addr = COLOR_ALIGN(addr, last_mmap, pgoff);
124 else
125 addr = PAGE_ALIGN(addr);
126
Hugh Dickins1be71072017-06-19 04:03:24 -0700127 vma = find_vma_prev(mm, addr, &prev);
Helge Deller9dabf602014-01-31 22:19:52 +0100128 if (task_size - len >= addr &&
Hugh Dickins1be71072017-06-19 04:03:24 -0700129 (!vma || addr + len <= vm_start_gap(vma)) &&
130 (!prev || addr >= vm_end_gap(prev)))
Helge Deller9dabf602014-01-31 22:19:52 +0100131 goto found_addr;
132 }
133
134 info.flags = 0;
135 info.length = len;
136 info.low_limit = mm->mmap_legacy_base;
Kees Cook8f2af152018-04-10 16:34:53 -0700137 info.high_limit = mmap_upper_limit(NULL);
Helge Deller0ef36bd2014-04-09 19:49:28 +0200138 info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
Helge Deller9dabf602014-01-31 22:19:52 +0100139 info.align_offset = shared_align_offset(last_mmap, pgoff);
140 addr = vm_unmapped_area(&info);
141
142found_addr:
143 if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
144 SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
Helge Deller0576da22013-11-20 23:07:42 +0100145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return addr;
147}
148
Helge Deller9dabf602014-01-31 22:19:52 +0100149unsigned long
150arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
151 const unsigned long len, const unsigned long pgoff,
152 const unsigned long flags)
153{
Hugh Dickins1be71072017-06-19 04:03:24 -0700154 struct vm_area_struct *vma, *prev;
Helge Deller9dabf602014-01-31 22:19:52 +0100155 struct mm_struct *mm = current->mm;
156 unsigned long addr = addr0;
157 int do_color_align, last_mmap;
158 struct vm_unmapped_area_info info;
159
Helge Deller9dabf602014-01-31 22:19:52 +0100160 /* requested length too big for entire address space */
161 if (len > TASK_SIZE)
162 return -ENOMEM;
163
164 do_color_align = 0;
165 if (filp || (flags & MAP_SHARED))
166 do_color_align = 1;
167 last_mmap = GET_LAST_MMAP(filp);
168
169 if (flags & MAP_FIXED) {
170 if ((flags & MAP_SHARED) && last_mmap &&
171 (addr - shared_align_offset(last_mmap, pgoff))
Helge Deller0ef36bd2014-04-09 19:49:28 +0200172 & (SHM_COLOUR - 1))
Helge Deller9dabf602014-01-31 22:19:52 +0100173 return -EINVAL;
174 goto found_addr;
175 }
176
177 /* requesting a specific address */
178 if (addr) {
179 if (do_color_align && last_mmap)
180 addr = COLOR_ALIGN(addr, last_mmap, pgoff);
181 else
182 addr = PAGE_ALIGN(addr);
Hugh Dickins1be71072017-06-19 04:03:24 -0700183
184 vma = find_vma_prev(mm, addr, &prev);
Helge Deller9dabf602014-01-31 22:19:52 +0100185 if (TASK_SIZE - len >= addr &&
Hugh Dickins1be71072017-06-19 04:03:24 -0700186 (!vma || addr + len <= vm_start_gap(vma)) &&
187 (!prev || addr >= vm_end_gap(prev)))
Helge Deller9dabf602014-01-31 22:19:52 +0100188 goto found_addr;
189 }
190
191 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
192 info.length = len;
193 info.low_limit = PAGE_SIZE;
194 info.high_limit = mm->mmap_base;
Helge Deller0ef36bd2014-04-09 19:49:28 +0200195 info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
Helge Deller9dabf602014-01-31 22:19:52 +0100196 info.align_offset = shared_align_offset(last_mmap, pgoff);
197 addr = vm_unmapped_area(&info);
198 if (!(addr & ~PAGE_MASK))
199 goto found_addr;
200 VM_BUG_ON(addr != -ENOMEM);
201
202 /*
203 * A failed mmap() very likely causes application failure,
204 * so fall back to the bottom-up function here. This scenario
205 * can happen with large stack limits and large mmap()
206 * allocations.
207 */
208 return arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
209
210found_addr:
211 if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
212 SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
213
214 return addr;
215}
216
217static int mmap_is_legacy(void)
218{
219 if (current->personality & ADDR_COMPAT_LAYOUT)
220 return 1;
221
222 /* parisc stack always grows up - so a unlimited stack should
223 * not be an indicator to use the legacy memory layout.
224 * if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
225 * return 1;
226 */
227
228 return sysctl_legacy_va_layout;
229}
230
231static unsigned long mmap_rnd(void)
232{
233 unsigned long rnd = 0;
234
Helge Deller18d98a72016-10-16 00:02:27 +0200235 if (current->flags & PF_RANDOMIZE)
236 rnd = get_random_int() & MMAP_RND_MASK;
237
Helge Deller9dabf602014-01-31 22:19:52 +0100238 return rnd << PAGE_SHIFT;
239}
240
Helge Deller18d98a72016-10-16 00:02:27 +0200241unsigned long arch_mmap_rnd(void)
242{
243 return (get_random_int() & MMAP_RND_MASK) << PAGE_SHIFT;
244}
245
Helge Deller9dabf602014-01-31 22:19:52 +0100246static unsigned long mmap_legacy_base(void)
247{
248 return TASK_UNMAPPED_BASE + mmap_rnd();
249}
250
251/*
252 * This function, called very early during the creation of a new
253 * process VM image, sets up which VM layout function to use:
254 */
Kees Cook8f2af152018-04-10 16:34:53 -0700255void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
Helge Deller9dabf602014-01-31 22:19:52 +0100256{
257 mm->mmap_legacy_base = mmap_legacy_base();
Kees Cook8f2af152018-04-10 16:34:53 -0700258 mm->mmap_base = mmap_upper_limit(rlim_stack);
Helge Deller9dabf602014-01-31 22:19:52 +0100259
260 if (mmap_is_legacy()) {
261 mm->mmap_base = mm->mmap_legacy_base;
262 mm->get_unmapped_area = arch_get_unmapped_area;
263 } else {
264 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
265 }
266}
267
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
270 unsigned long prot, unsigned long flags, unsigned long fd,
271 unsigned long pgoff)
272{
273 /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
274 we have. */
Dominik Brodowskia90f5902018-03-11 11:34:46 +0100275 return ksys_mmap_pgoff(addr, len, prot, flags, fd,
276 pgoff >> (PAGE_SHIFT - 12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
280 unsigned long prot, unsigned long flags, unsigned long fd,
281 unsigned long offset)
282{
283 if (!(offset & ~PAGE_MASK)) {
Dominik Brodowskia90f5902018-03-11 11:34:46 +0100284 return ksys_mmap_pgoff(addr, len, prot, flags, fd,
Al Virof8b72562009-11-30 17:37:04 -0500285 offset >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 } else {
287 return -EINVAL;
288 }
289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291/* Fucking broken ABI */
292
293#ifdef CONFIG_64BIT
294asmlinkage long parisc_truncate64(const char __user * path,
295 unsigned int high, unsigned int low)
296{
Dominik Brodowskidf260e22018-03-19 17:32:11 +0100297 return ksys_truncate(path, (long)high << 32 | low);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300asmlinkage long parisc_ftruncate64(unsigned int fd,
301 unsigned int high, unsigned int low)
302{
Dominik Brodowski411d9472018-03-11 11:34:54 +0100303 return ksys_ftruncate(fd, (long)high << 32 | low);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306/* stubs for the benefit of the syscall_table since truncate64 and truncate
307 * are identical on LP64 */
308asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
309{
Dominik Brodowskidf260e22018-03-19 17:32:11 +0100310 return ksys_truncate(path, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
313{
Dominik Brodowski411d9472018-03-11 11:34:54 +0100314 return ksys_ftruncate(fd, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
317{
318 return sys_fcntl(fd, cmd, arg);
319}
320#else
321
322asmlinkage long parisc_truncate64(const char __user * path,
323 unsigned int high, unsigned int low)
324{
Dominik Brodowskidf260e22018-03-19 17:32:11 +0100325 return ksys_truncate(path, (loff_t)high << 32 | low);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328asmlinkage long parisc_ftruncate64(unsigned int fd,
329 unsigned int high, unsigned int low)
330{
331 return sys_ftruncate64(fd, (loff_t)high << 32 | low);
332}
333#endif
334
335asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
336 unsigned int high, unsigned int low)
337{
Dominik Brodowski36028d52018-03-19 17:38:31 +0100338 return ksys_pread64(fd, buf, count, (loff_t)high << 32 | low);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
342 size_t count, unsigned int high, unsigned int low)
343{
Dominik Brodowski36028d52018-03-19 17:38:31 +0100344 return ksys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
348 size_t count)
349{
Dominik Brodowskic7b95d52018-03-19 17:51:36 +0100350 return ksys_readahead(fd, (loff_t)high << 32 | low, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353asmlinkage long parisc_fadvise64_64(int fd,
354 unsigned int high_off, unsigned int low_off,
355 unsigned int high_len, unsigned int low_len, int advice)
356{
Dominik Brodowski9d5b7c92018-03-11 11:34:45 +0100357 return ksys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 (loff_t)high_len << 32 | low_len, advice);
359}
360
Kyle McMartin6ca773c2006-04-20 04:44:07 +0000361asmlinkage long parisc_sync_file_range(int fd,
362 u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
363 unsigned int flags)
364{
Dominik Brodowski806cbae2018-03-11 11:34:47 +0100365 return ksys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
Kyle McMartin6ca773c2006-04-20 04:44:07 +0000366 (loff_t)hi_nbytes << 32 | lo_nbytes, flags);
367}
368
Helge Deller4474a332013-02-19 21:23:59 +0100369asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
370 u32 lenhi, u32 lenlo)
371{
Dominik Brodowskiedf292c2018-03-19 17:46:32 +0100372 return ksys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
373 ((u64)lenhi << 32) | lenlo);
Helge Deller4474a332013-02-19 21:23:59 +0100374}
375
Kyle McMartin75a49582006-08-27 11:12:13 -0400376long parisc_personality(unsigned long personality)
377{
378 long err;
379
380 if (personality(current->personality) == PER_LINUX32
Jiri Kosina5b24c422012-08-02 15:33:59 +0200381 && personality(personality) == PER_LINUX)
382 personality = (personality & ~PER_MASK) | PER_LINUX32;
Kyle McMartin75a49582006-08-27 11:12:13 -0400383
384 err = sys_personality(personality);
Jiri Kosina5b24c422012-08-02 15:33:59 +0200385 if (personality(err) == PER_LINUX32)
386 err = (err & ~PER_MASK) | PER_LINUX;
Kyle McMartin75a49582006-08-27 11:12:13 -0400387
388 return err;
389}