Loading...
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 *
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include <asm/uaccess.h>
26#include <linux/file.h>
27#include <linux/fs.h>
28#include <linux/linkage.h>
29#include <linux/mm.h>
30#include <linux/mman.h>
31#include <linux/shm.h>
32#include <linux/syscalls.h>
33#include <linux/utsname.h>
34#include <linux/personality.h>
35
36static unsigned long get_unshared_area(unsigned long addr, unsigned long len)
37{
38 struct vm_area_struct *vma;
39
40 addr = PAGE_ALIGN(addr);
41
42 for (vma = find_vma(current->mm, addr); ; vma = vma->vm_next) {
43 /* At this point: (!vma || addr < vma->vm_end). */
44 if (TASK_SIZE - len < addr)
45 return -ENOMEM;
46 if (!vma || addr + len <= vma->vm_start)
47 return addr;
48 addr = vma->vm_end;
49 }
50}
51
52#define DCACHE_ALIGN(addr) (((addr) + (SHMLBA - 1)) &~ (SHMLBA - 1))
53
54/*
55 * We need to know the offset to use. Old scheme was to look for
56 * existing mapping and use the same offset. New scheme is to use the
57 * address of the kernel data structure as the seed for the offset.
58 * We'll see how that works...
59 *
60 * The mapping is cacheline aligned, so there's no information in the bottom
61 * few bits of the address. We're looking for 10 bits (4MB / 4k), so let's
62 * drop the bottom 8 bits and use bits 8-17.
63 */
64static int get_offset(struct address_space *mapping)
65{
66 int offset = (unsigned long) mapping << (PAGE_SHIFT - 8);
67 return offset & 0x3FF000;
68}
69
70static unsigned long get_shared_area(struct address_space *mapping,
71 unsigned long addr, unsigned long len, unsigned long pgoff)
72{
73 struct vm_area_struct *vma;
74 int offset = mapping ? get_offset(mapping) : 0;
75
76 addr = DCACHE_ALIGN(addr - offset) + offset;
77
78 for (vma = find_vma(current->mm, addr); ; vma = vma->vm_next) {
79 /* At this point: (!vma || addr < vma->vm_end). */
80 if (TASK_SIZE - len < addr)
81 return -ENOMEM;
82 if (!vma || addr + len <= vma->vm_start)
83 return addr;
84 addr = DCACHE_ALIGN(vma->vm_end - offset) + offset;
85 if (addr < vma->vm_end) /* handle wraparound */
86 return -ENOMEM;
87 }
88}
89
90unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
91 unsigned long len, unsigned long pgoff, unsigned long flags)
92{
93 if (len > TASK_SIZE)
94 return -ENOMEM;
95 /* Might want to check for cache aliasing issues for MAP_FIXED case
96 * like ARM or MIPS ??? --BenH.
97 */
98 if (flags & MAP_FIXED)
99 return addr;
100 if (!addr)
101 addr = TASK_UNMAPPED_BASE;
102
103 if (filp) {
104 addr = get_shared_area(filp->f_mapping, addr, len, pgoff);
105 } else if(flags & MAP_SHARED) {
106 addr = get_shared_area(NULL, addr, len, pgoff);
107 } else {
108 addr = get_unshared_area(addr, len);
109 }
110 return addr;
111}
112
113asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
114 unsigned long prot, unsigned long flags, unsigned long fd,
115 unsigned long pgoff)
116{
117 /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
118 we have. */
119 return sys_mmap_pgoff(addr, len, prot, flags, fd,
120 pgoff >> (PAGE_SHIFT - 12));
121}
122
123asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
124 unsigned long prot, unsigned long flags, unsigned long fd,
125 unsigned long offset)
126{
127 if (!(offset & ~PAGE_MASK)) {
128 return sys_mmap_pgoff(addr, len, prot, flags, fd,
129 offset >> PAGE_SHIFT);
130 } else {
131 return -EINVAL;
132 }
133}
134
135/* Fucking broken ABI */
136
137#ifdef CONFIG_64BIT
138asmlinkage long parisc_truncate64(const char __user * path,
139 unsigned int high, unsigned int low)
140{
141 return sys_truncate(path, (long)high << 32 | low);
142}
143
144asmlinkage long parisc_ftruncate64(unsigned int fd,
145 unsigned int high, unsigned int low)
146{
147 return sys_ftruncate(fd, (long)high << 32 | low);
148}
149
150/* stubs for the benefit of the syscall_table since truncate64 and truncate
151 * are identical on LP64 */
152asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
153{
154 return sys_truncate(path, length);
155}
156asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
157{
158 return sys_ftruncate(fd, length);
159}
160asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
161{
162 return sys_fcntl(fd, cmd, arg);
163}
164#else
165
166asmlinkage long parisc_truncate64(const char __user * path,
167 unsigned int high, unsigned int low)
168{
169 return sys_truncate64(path, (loff_t)high << 32 | low);
170}
171
172asmlinkage long parisc_ftruncate64(unsigned int fd,
173 unsigned int high, unsigned int low)
174{
175 return sys_ftruncate64(fd, (loff_t)high << 32 | low);
176}
177#endif
178
179asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
180 unsigned int high, unsigned int low)
181{
182 return sys_pread64(fd, buf, count, (loff_t)high << 32 | low);
183}
184
185asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
186 size_t count, unsigned int high, unsigned int low)
187{
188 return sys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
189}
190
191asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
192 size_t count)
193{
194 return sys_readahead(fd, (loff_t)high << 32 | low, count);
195}
196
197asmlinkage long parisc_fadvise64_64(int fd,
198 unsigned int high_off, unsigned int low_off,
199 unsigned int high_len, unsigned int low_len, int advice)
200{
201 return sys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
202 (loff_t)high_len << 32 | low_len, advice);
203}
204
205asmlinkage long parisc_sync_file_range(int fd,
206 u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
207 unsigned int flags)
208{
209 return sys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
210 (loff_t)hi_nbytes << 32 | lo_nbytes, flags);
211}
212
213asmlinkage unsigned long sys_alloc_hugepages(int key, unsigned long addr, unsigned long len, int prot, int flag)
214{
215 return -ENOMEM;
216}
217
218asmlinkage int sys_free_hugepages(unsigned long addr)
219{
220 return -EINVAL;
221}
222
223long parisc_personality(unsigned long personality)
224{
225 long err;
226
227 if (personality(current->personality) == PER_LINUX32
228 && personality == PER_LINUX)
229 personality = PER_LINUX32;
230
231 err = sys_personality(personality);
232 if (err == PER_LINUX32)
233 err = PER_LINUX;
234
235 return err;
236}
1// SPDX-License-Identifier: GPL-2.0-or-later
2
3/*
4 * PARISC specific syscalls
5 *
6 * Copyright (C) 1999-2003 Matthew Wilcox <willy at parisc-linux.org>
7 * Copyright (C) 2000-2003 Paul Bame <bame at parisc-linux.org>
8 * Copyright (C) 2001 Thomas Bogendoerfer <tsbogend at parisc-linux.org>
9 * Copyright (C) 1999-2020 Helge Deller <deller@gmx.de>
10 */
11
12#include <linux/uaccess.h>
13#include <asm/elf.h>
14#include <linux/file.h>
15#include <linux/fs.h>
16#include <linux/linkage.h>
17#include <linux/mm.h>
18#include <linux/mman.h>
19#include <linux/sched/signal.h>
20#include <linux/sched/mm.h>
21#include <linux/shm.h>
22#include <linux/syscalls.h>
23#include <linux/utsname.h>
24#include <linux/personality.h>
25#include <linux/random.h>
26#include <linux/compat.h>
27#include <linux/elf-randomize.h>
28
29/*
30 * Construct an artificial page offset for the mapping based on the physical
31 * address of the kernel file mapping variable.
32 */
33#define GET_FILP_PGOFF(filp) \
34 (filp ? (((unsigned long) filp->f_mapping) >> 8) \
35 & ((SHM_COLOUR-1) >> PAGE_SHIFT) : 0UL)
36
37static unsigned long shared_align_offset(unsigned long filp_pgoff,
38 unsigned long pgoff)
39{
40 return (filp_pgoff + pgoff) << PAGE_SHIFT;
41}
42
43static inline unsigned long COLOR_ALIGN(unsigned long addr,
44 unsigned long filp_pgoff, unsigned long pgoff)
45{
46 unsigned long base = (addr+SHM_COLOUR-1) & ~(SHM_COLOUR-1);
47 unsigned long off = (SHM_COLOUR-1) &
48 shared_align_offset(filp_pgoff, pgoff);
49 return base + off;
50}
51
52
53#define STACK_SIZE_DEFAULT (USER_WIDE_MODE \
54 ? (1 << 30) /* 1 GB */ \
55 : (CONFIG_STACK_MAX_DEFAULT_SIZE_MB*1024*1024))
56
57unsigned long calc_max_stack_size(unsigned long stack_max)
58{
59#ifdef CONFIG_COMPAT
60 if (!USER_WIDE_MODE && (stack_max == COMPAT_RLIM_INFINITY))
61 stack_max = STACK_SIZE_DEFAULT;
62 else
63#endif
64 if (stack_max == RLIM_INFINITY)
65 stack_max = STACK_SIZE_DEFAULT;
66
67 return stack_max;
68}
69
70
71/*
72 * Top of mmap area (just below the process stack).
73 */
74
75/*
76 * When called from arch_get_unmapped_area(), rlim_stack will be NULL,
77 * indicating that "current" should be used instead of a passed-in
78 * value from the exec bprm as done with arch_pick_mmap_layout().
79 */
80unsigned long mmap_upper_limit(struct rlimit *rlim_stack)
81{
82 unsigned long stack_base;
83
84 /* Limit stack size - see setup_arg_pages() in fs/exec.c */
85 stack_base = rlim_stack ? rlim_stack->rlim_max
86 : rlimit_max(RLIMIT_STACK);
87
88 stack_base = calc_max_stack_size(stack_base);
89
90 /* Add space for stack randomization. */
91 if (current->flags & PF_RANDOMIZE)
92 stack_base += (STACK_RND_MASK << PAGE_SHIFT);
93
94 return PAGE_ALIGN(STACK_TOP - stack_base);
95}
96
97enum mmap_allocation_direction {UP, DOWN};
98
99static unsigned long arch_get_unmapped_area_common(struct file *filp,
100 unsigned long addr, unsigned long len, unsigned long pgoff,
101 unsigned long flags, enum mmap_allocation_direction dir)
102{
103 struct mm_struct *mm = current->mm;
104 struct vm_area_struct *vma, *prev;
105 unsigned long filp_pgoff;
106 int do_color_align;
107 struct vm_unmapped_area_info info;
108
109 if (unlikely(len > TASK_SIZE))
110 return -ENOMEM;
111
112 do_color_align = 0;
113 if (filp || (flags & MAP_SHARED))
114 do_color_align = 1;
115 filp_pgoff = GET_FILP_PGOFF(filp);
116
117 if (flags & MAP_FIXED) {
118 /* Even MAP_FIXED mappings must reside within TASK_SIZE */
119 if (TASK_SIZE - len < addr)
120 return -EINVAL;
121
122 if ((flags & MAP_SHARED) && filp &&
123 (addr - shared_align_offset(filp_pgoff, pgoff))
124 & (SHM_COLOUR - 1))
125 return -EINVAL;
126 return addr;
127 }
128
129 if (addr) {
130 if (do_color_align)
131 addr = COLOR_ALIGN(addr, filp_pgoff, pgoff);
132 else
133 addr = PAGE_ALIGN(addr);
134
135 vma = find_vma_prev(mm, addr, &prev);
136 if (TASK_SIZE - len >= addr &&
137 (!vma || addr + len <= vm_start_gap(vma)) &&
138 (!prev || addr >= vm_end_gap(prev)))
139 return addr;
140 }
141
142 info.length = len;
143 info.align_mask = do_color_align ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
144 info.align_offset = shared_align_offset(filp_pgoff, pgoff);
145
146 if (dir == DOWN) {
147 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
148 info.low_limit = PAGE_SIZE;
149 info.high_limit = mm->mmap_base;
150 addr = vm_unmapped_area(&info);
151 if (!(addr & ~PAGE_MASK))
152 return addr;
153 VM_BUG_ON(addr != -ENOMEM);
154
155 /*
156 * A failed mmap() very likely causes application failure,
157 * so fall back to the bottom-up function here. This scenario
158 * can happen with large stack limits and large mmap()
159 * allocations.
160 */
161 }
162
163 info.flags = 0;
164 info.low_limit = mm->mmap_base;
165 info.high_limit = mmap_upper_limit(NULL);
166 return vm_unmapped_area(&info);
167}
168
169unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
170 unsigned long len, unsigned long pgoff, unsigned long flags)
171{
172 return arch_get_unmapped_area_common(filp,
173 addr, len, pgoff, flags, UP);
174}
175
176unsigned long arch_get_unmapped_area_topdown(struct file *filp,
177 unsigned long addr, unsigned long len, unsigned long pgoff,
178 unsigned long flags)
179{
180 return arch_get_unmapped_area_common(filp,
181 addr, len, pgoff, flags, DOWN);
182}
183
184asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
185 unsigned long prot, unsigned long flags, unsigned long fd,
186 unsigned long pgoff)
187{
188 /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
189 we have. */
190 return ksys_mmap_pgoff(addr, len, prot, flags, fd,
191 pgoff >> (PAGE_SHIFT - 12));
192}
193
194asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
195 unsigned long prot, unsigned long flags, unsigned long fd,
196 unsigned long offset)
197{
198 if (!(offset & ~PAGE_MASK)) {
199 return ksys_mmap_pgoff(addr, len, prot, flags, fd,
200 offset >> PAGE_SHIFT);
201 } else {
202 return -EINVAL;
203 }
204}
205
206/* Fucking broken ABI */
207
208#ifdef CONFIG_64BIT
209asmlinkage long parisc_truncate64(const char __user * path,
210 unsigned int high, unsigned int low)
211{
212 return ksys_truncate(path, (long)high << 32 | low);
213}
214
215asmlinkage long parisc_ftruncate64(unsigned int fd,
216 unsigned int high, unsigned int low)
217{
218 return ksys_ftruncate(fd, (long)high << 32 | low);
219}
220
221/* stubs for the benefit of the syscall_table since truncate64 and truncate
222 * are identical on LP64 */
223asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
224{
225 return ksys_truncate(path, length);
226}
227asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
228{
229 return ksys_ftruncate(fd, length);
230}
231asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
232{
233 return sys_fcntl(fd, cmd, arg);
234}
235#else
236
237asmlinkage long parisc_truncate64(const char __user * path,
238 unsigned int high, unsigned int low)
239{
240 return ksys_truncate(path, (loff_t)high << 32 | low);
241}
242
243asmlinkage long parisc_ftruncate64(unsigned int fd,
244 unsigned int high, unsigned int low)
245{
246 return sys_ftruncate64(fd, (loff_t)high << 32 | low);
247}
248#endif
249
250asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
251 unsigned int high, unsigned int low)
252{
253 return ksys_pread64(fd, buf, count, (loff_t)high << 32 | low);
254}
255
256asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
257 size_t count, unsigned int high, unsigned int low)
258{
259 return ksys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
260}
261
262asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
263 size_t count)
264{
265 return ksys_readahead(fd, (loff_t)high << 32 | low, count);
266}
267
268asmlinkage long parisc_fadvise64_64(int fd,
269 unsigned int high_off, unsigned int low_off,
270 unsigned int high_len, unsigned int low_len, int advice)
271{
272 return ksys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
273 (loff_t)high_len << 32 | low_len, advice);
274}
275
276asmlinkage long parisc_sync_file_range(int fd,
277 u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
278 unsigned int flags)
279{
280 return ksys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
281 (loff_t)hi_nbytes << 32 | lo_nbytes, flags);
282}
283
284asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
285 u32 lenhi, u32 lenlo)
286{
287 return ksys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
288 ((u64)lenhi << 32) | lenlo);
289}
290
291asmlinkage long parisc_personality(unsigned long personality)
292{
293 long err;
294
295 if (personality(current->personality) == PER_LINUX32
296 && personality(personality) == PER_LINUX)
297 personality = (personality & ~PER_MASK) | PER_LINUX32;
298
299 err = sys_personality(personality);
300 if (personality(err) == PER_LINUX32)
301 err = (err & ~PER_MASK) | PER_LINUX;
302
303 return err;
304}
305
306/*
307 * Up to kernel v5.9 we defined O_NONBLOCK as 000200004,
308 * since then O_NONBLOCK is defined as 000200000.
309 *
310 * The following wrapper functions mask out the old
311 * O_NDELAY bit from calls which use O_NONBLOCK.
312 *
313 * XXX: Remove those in year 2022 (or later)?
314 */
315
316#define O_NONBLOCK_OLD 000200004
317#define O_NONBLOCK_MASK_OUT (O_NONBLOCK_OLD & ~O_NONBLOCK)
318
319static int FIX_O_NONBLOCK(int flags)
320{
321 if ((flags & O_NONBLOCK_MASK_OUT) &&
322 !test_thread_flag(TIF_NONBLOCK_WARNING)) {
323 set_thread_flag(TIF_NONBLOCK_WARNING);
324 pr_warn("%s(%d) uses a deprecated O_NONBLOCK value."
325 " Please recompile with newer glibc.\n",
326 current->comm, current->pid);
327 }
328 return flags & ~O_NONBLOCK_MASK_OUT;
329}
330
331asmlinkage long parisc_timerfd_create(int clockid, int flags)
332{
333 flags = FIX_O_NONBLOCK(flags);
334 return sys_timerfd_create(clockid, flags);
335}
336
337asmlinkage long parisc_signalfd4(int ufd, sigset_t __user *user_mask,
338 size_t sizemask, int flags)
339{
340 flags = FIX_O_NONBLOCK(flags);
341 return sys_signalfd4(ufd, user_mask, sizemask, flags);
342}
343
344#ifdef CONFIG_COMPAT
345asmlinkage long parisc_compat_signalfd4(int ufd,
346 compat_sigset_t __user *user_mask,
347 compat_size_t sizemask, int flags)
348{
349 flags = FIX_O_NONBLOCK(flags);
350 return compat_sys_signalfd4(ufd, user_mask, sizemask, flags);
351}
352#endif
353
354asmlinkage long parisc_eventfd2(unsigned int count, int flags)
355{
356 flags = FIX_O_NONBLOCK(flags);
357 return sys_eventfd2(count, flags);
358}
359
360asmlinkage long parisc_userfaultfd(int flags)
361{
362 flags = FIX_O_NONBLOCK(flags);
363 return sys_userfaultfd(flags);
364}
365
366asmlinkage long parisc_pipe2(int __user *fildes, int flags)
367{
368 flags = FIX_O_NONBLOCK(flags);
369 return sys_pipe2(fildes, flags);
370}
371
372asmlinkage long parisc_inotify_init1(int flags)
373{
374 flags = FIX_O_NONBLOCK(flags);
375 return sys_inotify_init1(flags);
376}
377
378/*
379 * madvise() wrapper
380 *
381 * Up to kernel v6.1 parisc has different values than all other
382 * platforms for the MADV_xxx flags listed below.
383 * To keep binary compatibility with existing userspace programs
384 * translate the former values to the new values.
385 *
386 * XXX: Remove this wrapper in year 2025 (or later)
387 */
388
389asmlinkage notrace long parisc_madvise(unsigned long start, size_t len_in, int behavior)
390{
391 switch (behavior) {
392 case 65: behavior = MADV_MERGEABLE; break;
393 case 66: behavior = MADV_UNMERGEABLE; break;
394 case 67: behavior = MADV_HUGEPAGE; break;
395 case 68: behavior = MADV_NOHUGEPAGE; break;
396 case 69: behavior = MADV_DONTDUMP; break;
397 case 70: behavior = MADV_DODUMP; break;
398 case 71: behavior = MADV_WIPEONFORK; break;
399 case 72: behavior = MADV_KEEPONFORK; break;
400 case 73: behavior = MADV_COLLAPSE; break;
401 }
402
403 return sys_madvise(start, len_in, behavior);
404}