1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * PARISC specific syscalls
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>
12 #include <linux/uaccess.h>
14 #include <linux/file.h>
16 #include <linux/linkage.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>
28 /* we construct an artificial offset for the mapping based on the physical
29 * address of the kernel mapping variable */
30 #define GET_LAST_MMAP(filp) \
31 (filp ? ((unsigned long) filp->f_mapping) >> 8 : 0UL)
32 #define SET_LAST_MMAP(filp, val) \
35 static int get_offset(unsigned int last_mmap)
37 return (last_mmap & (SHM_COLOUR-1)) >> PAGE_SHIFT;
40 static unsigned long shared_align_offset(unsigned int last_mmap,
43 return (get_offset(last_mmap) + pgoff) << PAGE_SHIFT;
46 static inline unsigned long COLOR_ALIGN(unsigned long addr,
47 unsigned int last_mmap, unsigned long pgoff)
49 unsigned long base = (addr+SHM_COLOUR-1) & ~(SHM_COLOUR-1);
50 unsigned long off = (SHM_COLOUR-1) &
51 (shared_align_offset(last_mmap, pgoff) << PAGE_SHIFT);
57 * Top of mmap area (just below the process stack).
61 * When called from arch_get_unmapped_area(), rlim_stack will be NULL,
62 * indicating that "current" should be used instead of a passed-in
63 * value from the exec bprm as done with arch_pick_mmap_layout().
65 static unsigned long mmap_upper_limit(struct rlimit *rlim_stack)
67 unsigned long stack_base;
69 /* Limit stack size - see setup_arg_pages() in fs/exec.c */
70 stack_base = rlim_stack ? rlim_stack->rlim_max
71 : rlimit_max(RLIMIT_STACK);
72 if (stack_base > STACK_SIZE_MAX)
73 stack_base = STACK_SIZE_MAX;
75 /* Add space for stack randomization. */
76 if (current->flags & PF_RANDOMIZE)
77 stack_base += (STACK_RND_MASK << PAGE_SHIFT);
79 return PAGE_ALIGN(STACK_TOP - stack_base);
83 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
84 unsigned long len, unsigned long pgoff, unsigned long flags)
86 struct mm_struct *mm = current->mm;
87 struct vm_area_struct *vma, *prev;
88 unsigned long task_size = TASK_SIZE;
89 int do_color_align, last_mmap;
90 struct vm_unmapped_area_info info;
96 if (filp || (flags & MAP_SHARED))
98 last_mmap = GET_LAST_MMAP(filp);
100 if (flags & MAP_FIXED) {
101 if ((flags & MAP_SHARED) && last_mmap &&
102 (addr - shared_align_offset(last_mmap, pgoff))
109 if (do_color_align && last_mmap)
110 addr = COLOR_ALIGN(addr, last_mmap, pgoff);
112 addr = PAGE_ALIGN(addr);
114 vma = find_vma_prev(mm, addr, &prev);
115 if (task_size - len >= addr &&
116 (!vma || addr + len <= vm_start_gap(vma)) &&
117 (!prev || addr >= vm_end_gap(prev)))
123 info.low_limit = mm->mmap_legacy_base;
124 info.high_limit = mmap_upper_limit(NULL);
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);
130 if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
131 SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
137 arch_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)
141 struct vm_area_struct *vma, *prev;
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;
147 /* requested length too big for entire address space */
152 if (filp || (flags & MAP_SHARED))
154 last_mmap = GET_LAST_MMAP(filp);
156 if (flags & MAP_FIXED) {
157 if ((flags & MAP_SHARED) && last_mmap &&
158 (addr - shared_align_offset(last_mmap, pgoff))
164 /* requesting a specific address */
166 if (do_color_align && last_mmap)
167 addr = COLOR_ALIGN(addr, last_mmap, pgoff);
169 addr = PAGE_ALIGN(addr);
171 vma = find_vma_prev(mm, addr, &prev);
172 if (TASK_SIZE - len >= addr &&
173 (!vma || addr + len <= vm_start_gap(vma)) &&
174 (!prev || addr >= vm_end_gap(prev)))
178 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
180 info.low_limit = PAGE_SIZE;
181 info.high_limit = mm->mmap_base;
182 info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
183 info.align_offset = shared_align_offset(last_mmap, pgoff);
184 addr = vm_unmapped_area(&info);
185 if (!(addr & ~PAGE_MASK))
187 VM_BUG_ON(addr != -ENOMEM);
190 * A failed mmap() very likely causes application failure,
191 * so fall back to the bottom-up function here. This scenario
192 * can happen with large stack limits and large mmap()
195 return arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
198 if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
199 SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
204 static int mmap_is_legacy(void)
206 if (current->personality & ADDR_COMPAT_LAYOUT)
209 /* parisc stack always grows up - so a unlimited stack should
210 * not be an indicator to use the legacy memory layout.
211 * if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
215 return sysctl_legacy_va_layout;
218 static unsigned long mmap_rnd(void)
220 unsigned long rnd = 0;
222 if (current->flags & PF_RANDOMIZE)
223 rnd = get_random_int() & MMAP_RND_MASK;
225 return rnd << PAGE_SHIFT;
228 unsigned long arch_mmap_rnd(void)
230 return (get_random_int() & MMAP_RND_MASK) << PAGE_SHIFT;
233 static unsigned long mmap_legacy_base(void)
235 return TASK_UNMAPPED_BASE + mmap_rnd();
239 * This function, called very early during the creation of a new
240 * process VM image, sets up which VM layout function to use:
242 void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
244 mm->mmap_legacy_base = mmap_legacy_base();
245 mm->mmap_base = mmap_upper_limit(rlim_stack);
247 if (mmap_is_legacy()) {
248 mm->mmap_base = mm->mmap_legacy_base;
249 mm->get_unmapped_area = arch_get_unmapped_area;
251 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
256 asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
257 unsigned long prot, unsigned long flags, unsigned long fd,
260 /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
262 return ksys_mmap_pgoff(addr, len, prot, flags, fd,
263 pgoff >> (PAGE_SHIFT - 12));
266 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
267 unsigned long prot, unsigned long flags, unsigned long fd,
268 unsigned long offset)
270 if (!(offset & ~PAGE_MASK)) {
271 return ksys_mmap_pgoff(addr, len, prot, flags, fd,
272 offset >> PAGE_SHIFT);
278 /* Fucking broken ABI */
281 asmlinkage long parisc_truncate64(const char __user * path,
282 unsigned int high, unsigned int low)
284 return ksys_truncate(path, (long)high << 32 | low);
287 asmlinkage long parisc_ftruncate64(unsigned int fd,
288 unsigned int high, unsigned int low)
290 return ksys_ftruncate(fd, (long)high << 32 | low);
293 /* stubs for the benefit of the syscall_table since truncate64 and truncate
294 * are identical on LP64 */
295 asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
297 return ksys_truncate(path, length);
299 asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
301 return ksys_ftruncate(fd, length);
303 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
305 return sys_fcntl(fd, cmd, arg);
309 asmlinkage long parisc_truncate64(const char __user * path,
310 unsigned int high, unsigned int low)
312 return ksys_truncate(path, (loff_t)high << 32 | low);
315 asmlinkage long parisc_ftruncate64(unsigned int fd,
316 unsigned int high, unsigned int low)
318 return sys_ftruncate64(fd, (loff_t)high << 32 | low);
322 asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
323 unsigned int high, unsigned int low)
325 return ksys_pread64(fd, buf, count, (loff_t)high << 32 | low);
328 asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
329 size_t count, unsigned int high, unsigned int low)
331 return ksys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
334 asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
337 return ksys_readahead(fd, (loff_t)high << 32 | low, count);
340 asmlinkage long parisc_fadvise64_64(int fd,
341 unsigned int high_off, unsigned int low_off,
342 unsigned int high_len, unsigned int low_len, int advice)
344 return ksys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
345 (loff_t)high_len << 32 | low_len, advice);
348 asmlinkage long parisc_sync_file_range(int fd,
349 u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
352 return ksys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
353 (loff_t)hi_nbytes << 32 | lo_nbytes, flags);
356 asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
357 u32 lenhi, u32 lenlo)
359 return ksys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
360 ((u64)lenhi << 32) | lenlo);
363 long parisc_personality(unsigned long personality)
367 if (personality(current->personality) == PER_LINUX32
368 && personality(personality) == PER_LINUX)
369 personality = (personality & ~PER_MASK) | PER_LINUX32;
371 err = sys_personality(personality);
372 if (personality(err) == PER_LINUX32)
373 err = (err & ~PER_MASK) | PER_LINUX;
379 * Up to kernel v5.9 we defined O_NONBLOCK as 000200004,
380 * since then O_NONBLOCK is defined as 000200000.
382 * The following wrapper functions mask out the old
383 * O_NDELAY bit from calls which use O_NONBLOCK.
385 * XXX: Remove those in year 2022 (or later)?
388 #define O_NONBLOCK_OLD 000200004
389 #define O_NONBLOCK_MASK_OUT (O_NONBLOCK_OLD & ~O_NONBLOCK)
391 static int FIX_O_NONBLOCK(int flags)
393 if (flags & O_NONBLOCK_MASK_OUT) {
394 struct task_struct *tsk = current;
395 pr_warn_once("%s(%d) uses a deprecated O_NONBLOCK value.\n",
396 tsk->comm, tsk->pid);
398 return flags & ~O_NONBLOCK_MASK_OUT;
401 asmlinkage long parisc_timerfd_create(int clockid, int flags)
403 flags = FIX_O_NONBLOCK(flags);
404 return sys_timerfd_create(clockid, flags);
407 asmlinkage long parisc_signalfd4(int ufd, sigset_t __user *user_mask,
408 size_t sizemask, int flags)
410 flags = FIX_O_NONBLOCK(flags);
411 return sys_signalfd4(ufd, user_mask, sizemask, flags);
415 asmlinkage long parisc_compat_signalfd4(int ufd,
416 compat_sigset_t __user *user_mask,
417 compat_size_t sizemask, int flags)
419 flags = FIX_O_NONBLOCK(flags);
420 return compat_sys_signalfd4(ufd, user_mask, sizemask, flags);
424 asmlinkage long parisc_eventfd2(unsigned int count, int flags)
426 flags = FIX_O_NONBLOCK(flags);
427 return sys_eventfd2(count, flags);
430 asmlinkage long parisc_userfaultfd(int flags)
432 flags = FIX_O_NONBLOCK(flags);
433 return sys_userfaultfd(flags);
436 asmlinkage long parisc_pipe2(int __user *fildes, int flags)
438 flags = FIX_O_NONBLOCK(flags);
439 return sys_pipe2(fildes, flags);
442 asmlinkage long parisc_inotify_init1(int flags)
444 flags = FIX_O_NONBLOCK(flags);
445 return sys_inotify_init1(flags);