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
shared_align_offset(unsigned long filp_pgoff,unsigned long pgoff)37 static unsigned long shared_align_offset(unsigned long filp_pgoff,
38 unsigned long pgoff)
39 {
40 return (filp_pgoff + pgoff) << PAGE_SHIFT;
41 }
42
COLOR_ALIGN(unsigned long addr,unsigned long filp_pgoff,unsigned long pgoff)43 static 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
calc_max_stack_size(unsigned long stack_max)57 unsigned 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 */
mmap_upper_limit(struct rlimit * rlim_stack)80 unsigned 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
97 enum mmap_allocation_direction {UP, DOWN};
98
arch_get_unmapped_area_common(struct file * filp,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags,enum mmap_allocation_direction dir)99 static 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 .length = len
109 };
110
111 if (unlikely(len > TASK_SIZE))
112 return -ENOMEM;
113
114 do_color_align = 0;
115 if (filp || (flags & MAP_SHARED))
116 do_color_align = 1;
117 filp_pgoff = GET_FILP_PGOFF(filp);
118
119 if (flags & MAP_FIXED) {
120 /* Even MAP_FIXED mappings must reside within TASK_SIZE */
121 if (TASK_SIZE - len < addr)
122 return -EINVAL;
123
124 if ((flags & MAP_SHARED) && filp &&
125 (addr - shared_align_offset(filp_pgoff, pgoff))
126 & (SHM_COLOUR - 1))
127 return -EINVAL;
128 return addr;
129 }
130
131 if (addr) {
132 if (do_color_align)
133 addr = COLOR_ALIGN(addr, filp_pgoff, pgoff);
134 else
135 addr = PAGE_ALIGN(addr);
136
137 vma = find_vma_prev(mm, addr, &prev);
138 if (TASK_SIZE - len >= addr &&
139 (!vma || addr + len <= vm_start_gap(vma)) &&
140 (!prev || addr >= vm_end_gap(prev)))
141 return addr;
142 }
143
144 info.align_mask = do_color_align ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
145 info.align_offset = shared_align_offset(filp_pgoff, pgoff);
146
147 if (dir == DOWN) {
148 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
149 info.low_limit = PAGE_SIZE;
150 info.high_limit = mm->mmap_base;
151 addr = vm_unmapped_area(&info);
152 if (!(addr & ~PAGE_MASK))
153 return addr;
154 VM_BUG_ON(addr != -ENOMEM);
155
156 /*
157 * A failed mmap() very likely causes application failure,
158 * so fall back to the bottom-up function here. This scenario
159 * can happen with large stack limits and large mmap()
160 * allocations.
161 */
162 }
163
164 info.low_limit = mm->mmap_base;
165 info.high_limit = mmap_upper_limit(NULL);
166 return vm_unmapped_area(&info);
167 }
168
arch_get_unmapped_area(struct file * filp,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags,vm_flags_t vm_flags)169 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
170 unsigned long len, unsigned long pgoff, unsigned long flags,
171 vm_flags_t vm_flags)
172 {
173 return arch_get_unmapped_area_common(filp,
174 addr, len, pgoff, flags, UP);
175 }
176
arch_get_unmapped_area_topdown(struct file * filp,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags,vm_flags_t vm_flags)177 unsigned long arch_get_unmapped_area_topdown(struct file *filp,
178 unsigned long addr, unsigned long len, unsigned long pgoff,
179 unsigned long flags, vm_flags_t vm_flags)
180 {
181 return arch_get_unmapped_area_common(filp,
182 addr, len, pgoff, flags, DOWN);
183 }
184
sys_mmap2(unsigned long addr,unsigned long len,unsigned long prot,unsigned long flags,unsigned long fd,unsigned long pgoff)185 asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
186 unsigned long prot, unsigned long flags, unsigned long fd,
187 unsigned long pgoff)
188 {
189 /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
190 we have. */
191 return ksys_mmap_pgoff(addr, len, prot, flags, fd,
192 pgoff >> (PAGE_SHIFT - 12));
193 }
194
sys_mmap(unsigned long addr,unsigned long len,unsigned long prot,unsigned long flags,unsigned long fd,unsigned long offset)195 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
196 unsigned long prot, unsigned long flags, unsigned long fd,
197 unsigned long offset)
198 {
199 if (!(offset & ~PAGE_MASK)) {
200 return ksys_mmap_pgoff(addr, len, prot, flags, fd,
201 offset >> PAGE_SHIFT);
202 } else {
203 return -EINVAL;
204 }
205 }
206
207 /* Fucking broken ABI */
208
209 #ifdef CONFIG_64BIT
parisc_truncate64(const char __user * path,unsigned int high,unsigned int low)210 asmlinkage long parisc_truncate64(const char __user * path,
211 unsigned int high, unsigned int low)
212 {
213 return ksys_truncate(path, (long)high << 32 | low);
214 }
215
parisc_ftruncate64(unsigned int fd,unsigned int high,unsigned int low)216 asmlinkage long parisc_ftruncate64(unsigned int fd,
217 unsigned int high, unsigned int low)
218 {
219 return ksys_ftruncate(fd, (long)high << 32 | low);
220 }
221
222 /* stubs for the benefit of the syscall_table since truncate64 and truncate
223 * are identical on LP64 */
sys_truncate64(const char __user * path,unsigned long length)224 asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
225 {
226 return ksys_truncate(path, length);
227 }
sys_ftruncate64(unsigned int fd,unsigned long length)228 asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
229 {
230 return ksys_ftruncate(fd, length);
231 }
sys_fcntl64(unsigned int fd,unsigned int cmd,unsigned long arg)232 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
233 {
234 return sys_fcntl(fd, cmd, arg);
235 }
236 #else
237
parisc_truncate64(const char __user * path,unsigned int high,unsigned int low)238 asmlinkage long parisc_truncate64(const char __user * path,
239 unsigned int high, unsigned int low)
240 {
241 return ksys_truncate(path, (loff_t)high << 32 | low);
242 }
243
parisc_ftruncate64(unsigned int fd,unsigned int high,unsigned int low)244 asmlinkage long parisc_ftruncate64(unsigned int fd,
245 unsigned int high, unsigned int low)
246 {
247 return sys_ftruncate64(fd, (loff_t)high << 32 | low);
248 }
249 #endif
250
parisc_pread64(unsigned int fd,char __user * buf,size_t count,unsigned int high,unsigned int low)251 asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
252 unsigned int high, unsigned int low)
253 {
254 return ksys_pread64(fd, buf, count, (loff_t)high << 32 | low);
255 }
256
parisc_pwrite64(unsigned int fd,const char __user * buf,size_t count,unsigned int high,unsigned int low)257 asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
258 size_t count, unsigned int high, unsigned int low)
259 {
260 return ksys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
261 }
262
parisc_readahead(int fd,unsigned int high,unsigned int low,size_t count)263 asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
264 size_t count)
265 {
266 return ksys_readahead(fd, (loff_t)high << 32 | low, count);
267 }
268
parisc_fadvise64_64(int fd,unsigned int high_off,unsigned int low_off,unsigned int high_len,unsigned int low_len,int advice)269 asmlinkage long parisc_fadvise64_64(int fd,
270 unsigned int high_off, unsigned int low_off,
271 unsigned int high_len, unsigned int low_len, int advice)
272 {
273 return ksys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
274 (loff_t)high_len << 32 | low_len, advice);
275 }
276
parisc_sync_file_range(int fd,u32 hi_off,u32 lo_off,u32 hi_nbytes,u32 lo_nbytes,unsigned int flags)277 asmlinkage long parisc_sync_file_range(int fd,
278 u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
279 unsigned int flags)
280 {
281 return ksys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
282 (loff_t)hi_nbytes << 32 | lo_nbytes, flags);
283 }
284
parisc_fallocate(int fd,int mode,u32 offhi,u32 offlo,u32 lenhi,u32 lenlo)285 asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
286 u32 lenhi, u32 lenlo)
287 {
288 return ksys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
289 ((u64)lenhi << 32) | lenlo);
290 }
291
parisc_personality(unsigned long personality)292 asmlinkage long parisc_personality(unsigned long personality)
293 {
294 long err;
295
296 if (personality(current->personality) == PER_LINUX32
297 && personality(personality) == PER_LINUX)
298 personality = (personality & ~PER_MASK) | PER_LINUX32;
299
300 err = sys_personality(personality);
301 if (personality(err) == PER_LINUX32)
302 err = (err & ~PER_MASK) | PER_LINUX;
303
304 return err;
305 }
306
307 /*
308 * Up to kernel v5.9 we defined O_NONBLOCK as 000200004,
309 * since then O_NONBLOCK is defined as 000200000.
310 *
311 * The following wrapper functions mask out the old
312 * O_NDELAY bit from calls which use O_NONBLOCK.
313 *
314 * XXX: Remove those in year 2022 (or later)?
315 */
316
317 #define O_NONBLOCK_OLD 000200004
318 #define O_NONBLOCK_MASK_OUT (O_NONBLOCK_OLD & ~O_NONBLOCK)
319
FIX_O_NONBLOCK(int flags)320 static int FIX_O_NONBLOCK(int flags)
321 {
322 if ((flags & O_NONBLOCK_MASK_OUT) &&
323 !test_thread_flag(TIF_NONBLOCK_WARNING)) {
324 set_thread_flag(TIF_NONBLOCK_WARNING);
325 pr_warn("%s(%d) uses a deprecated O_NONBLOCK value."
326 " Please recompile with newer glibc.\n",
327 current->comm, current->pid);
328 }
329 return flags & ~O_NONBLOCK_MASK_OUT;
330 }
331
parisc_timerfd_create(int clockid,int flags)332 asmlinkage long parisc_timerfd_create(int clockid, int flags)
333 {
334 flags = FIX_O_NONBLOCK(flags);
335 return sys_timerfd_create(clockid, flags);
336 }
337
parisc_signalfd4(int ufd,sigset_t __user * user_mask,size_t sizemask,int flags)338 asmlinkage long parisc_signalfd4(int ufd, sigset_t __user *user_mask,
339 size_t sizemask, int flags)
340 {
341 flags = FIX_O_NONBLOCK(flags);
342 return sys_signalfd4(ufd, user_mask, sizemask, flags);
343 }
344
345 #ifdef CONFIG_COMPAT
parisc_compat_signalfd4(int ufd,compat_sigset_t __user * user_mask,compat_size_t sizemask,int flags)346 asmlinkage long parisc_compat_signalfd4(int ufd,
347 compat_sigset_t __user *user_mask,
348 compat_size_t sizemask, int flags)
349 {
350 flags = FIX_O_NONBLOCK(flags);
351 return compat_sys_signalfd4(ufd, user_mask, sizemask, flags);
352 }
353 #endif
354
parisc_eventfd2(unsigned int count,int flags)355 asmlinkage long parisc_eventfd2(unsigned int count, int flags)
356 {
357 flags = FIX_O_NONBLOCK(flags);
358 return sys_eventfd2(count, flags);
359 }
360
parisc_userfaultfd(int flags)361 asmlinkage long parisc_userfaultfd(int flags)
362 {
363 flags = FIX_O_NONBLOCK(flags);
364 return sys_userfaultfd(flags);
365 }
366
parisc_pipe2(int __user * fildes,int flags)367 asmlinkage long parisc_pipe2(int __user *fildes, int flags)
368 {
369 flags = FIX_O_NONBLOCK(flags);
370 return sys_pipe2(fildes, flags);
371 }
372
parisc_inotify_init1(int flags)373 asmlinkage long parisc_inotify_init1(int flags)
374 {
375 flags = FIX_O_NONBLOCK(flags);
376 return sys_inotify_init1(flags);
377 }
378
379 /*
380 * madvise() wrapper
381 *
382 * Up to kernel v6.1 parisc has different values than all other
383 * platforms for the MADV_xxx flags listed below.
384 * To keep binary compatibility with existing userspace programs
385 * translate the former values to the new values.
386 *
387 * XXX: Remove this wrapper in year 2025 (or later)
388 */
389
parisc_madvise(unsigned long start,size_t len_in,int behavior)390 asmlinkage notrace long parisc_madvise(unsigned long start, size_t len_in, int behavior)
391 {
392 switch (behavior) {
393 case 65: behavior = MADV_MERGEABLE; break;
394 case 66: behavior = MADV_UNMERGEABLE; break;
395 case 67: behavior = MADV_HUGEPAGE; break;
396 case 68: behavior = MADV_NOHUGEPAGE; break;
397 case 69: behavior = MADV_DONTDUMP; break;
398 case 70: behavior = MADV_DODUMP; break;
399 case 71: behavior = MADV_WIPEONFORK; break;
400 case 72: behavior = MADV_KEEPONFORK; break;
401 case 73: behavior = MADV_COLLAPSE; break;
402 }
403
404 return sys_madvise(start, len_in, behavior);
405 }
406