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 #define STACK_SIZE_DEFAULT (USER_WIDE_MODE \
58 ? (1 << 30) /* 1 GB */ \
59 : (CONFIG_STACK_MAX_DEFAULT_SIZE_MB*1024*1024))
61 unsigned long calc_max_stack_size(unsigned long stack_max
)
64 if (!USER_WIDE_MODE
&& (stack_max
== COMPAT_RLIM_INFINITY
))
65 stack_max
= STACK_SIZE_DEFAULT
;
68 if (stack_max
== RLIM_INFINITY
)
69 stack_max
= STACK_SIZE_DEFAULT
;
76 * Top of mmap area (just below the process stack).
80 * When called from arch_get_unmapped_area(), rlim_stack will be NULL,
81 * indicating that "current" should be used instead of a passed-in
82 * value from the exec bprm as done with arch_pick_mmap_layout().
84 static unsigned long mmap_upper_limit(struct rlimit
*rlim_stack
)
86 unsigned long stack_base
;
88 /* Limit stack size - see setup_arg_pages() in fs/exec.c */
89 stack_base
= rlim_stack
? rlim_stack
->rlim_max
90 : rlimit_max(RLIMIT_STACK
);
92 stack_base
= calc_max_stack_size(stack_base
);
94 /* Add space for stack randomization. */
95 if (current
->flags
& PF_RANDOMIZE
)
96 stack_base
+= (STACK_RND_MASK
<< PAGE_SHIFT
);
98 return PAGE_ALIGN(STACK_TOP
- stack_base
);
102 unsigned long arch_get_unmapped_area(struct file
*filp
, unsigned long addr
,
103 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
105 struct mm_struct
*mm
= current
->mm
;
106 struct vm_area_struct
*vma
, *prev
;
107 unsigned long task_size
= TASK_SIZE
;
108 int do_color_align
, last_mmap
;
109 struct vm_unmapped_area_info info
;
115 if (filp
|| (flags
& MAP_SHARED
))
117 last_mmap
= GET_LAST_MMAP(filp
);
119 if (flags
& MAP_FIXED
) {
120 if ((flags
& MAP_SHARED
) && last_mmap
&&
121 (addr
- shared_align_offset(last_mmap
, pgoff
))
128 if (do_color_align
&& last_mmap
)
129 addr
= COLOR_ALIGN(addr
, last_mmap
, pgoff
);
131 addr
= PAGE_ALIGN(addr
);
133 vma
= find_vma_prev(mm
, addr
, &prev
);
134 if (task_size
- len
>= addr
&&
135 (!vma
|| addr
+ len
<= vm_start_gap(vma
)) &&
136 (!prev
|| addr
>= vm_end_gap(prev
)))
142 info
.low_limit
= mm
->mmap_legacy_base
;
143 info
.high_limit
= mmap_upper_limit(NULL
);
144 info
.align_mask
= last_mmap
? (PAGE_MASK
& (SHM_COLOUR
- 1)) : 0;
145 info
.align_offset
= shared_align_offset(last_mmap
, pgoff
);
146 addr
= vm_unmapped_area(&info
);
149 if (do_color_align
&& !last_mmap
&& !(addr
& ~PAGE_MASK
))
150 SET_LAST_MMAP(filp
, addr
- (pgoff
<< PAGE_SHIFT
));
156 arch_get_unmapped_area_topdown(struct file
*filp
, const unsigned long addr0
,
157 const unsigned long len
, const unsigned long pgoff
,
158 const unsigned long flags
)
160 struct vm_area_struct
*vma
, *prev
;
161 struct mm_struct
*mm
= current
->mm
;
162 unsigned long addr
= addr0
;
163 int do_color_align
, last_mmap
;
164 struct vm_unmapped_area_info info
;
166 /* requested length too big for entire address space */
171 if (filp
|| (flags
& MAP_SHARED
))
173 last_mmap
= GET_LAST_MMAP(filp
);
175 if (flags
& MAP_FIXED
) {
176 if ((flags
& MAP_SHARED
) && last_mmap
&&
177 (addr
- shared_align_offset(last_mmap
, pgoff
))
183 /* requesting a specific address */
185 if (do_color_align
&& last_mmap
)
186 addr
= COLOR_ALIGN(addr
, last_mmap
, pgoff
);
188 addr
= PAGE_ALIGN(addr
);
190 vma
= find_vma_prev(mm
, addr
, &prev
);
191 if (TASK_SIZE
- len
>= addr
&&
192 (!vma
|| addr
+ len
<= vm_start_gap(vma
)) &&
193 (!prev
|| addr
>= vm_end_gap(prev
)))
197 info
.flags
= VM_UNMAPPED_AREA_TOPDOWN
;
199 info
.low_limit
= PAGE_SIZE
;
200 info
.high_limit
= mm
->mmap_base
;
201 info
.align_mask
= last_mmap
? (PAGE_MASK
& (SHM_COLOUR
- 1)) : 0;
202 info
.align_offset
= shared_align_offset(last_mmap
, pgoff
);
203 addr
= vm_unmapped_area(&info
);
204 if (!(addr
& ~PAGE_MASK
))
206 VM_BUG_ON(addr
!= -ENOMEM
);
209 * A failed mmap() very likely causes application failure,
210 * so fall back to the bottom-up function here. This scenario
211 * can happen with large stack limits and large mmap()
214 return arch_get_unmapped_area(filp
, addr0
, len
, pgoff
, flags
);
217 if (do_color_align
&& !last_mmap
&& !(addr
& ~PAGE_MASK
))
218 SET_LAST_MMAP(filp
, addr
- (pgoff
<< PAGE_SHIFT
));
223 static int mmap_is_legacy(void)
225 if (current
->personality
& ADDR_COMPAT_LAYOUT
)
228 /* parisc stack always grows up - so a unlimited stack should
229 * not be an indicator to use the legacy memory layout.
230 * if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
234 return sysctl_legacy_va_layout
;
237 static unsigned long mmap_rnd(void)
239 unsigned long rnd
= 0;
241 if (current
->flags
& PF_RANDOMIZE
)
242 rnd
= get_random_int() & MMAP_RND_MASK
;
244 return rnd
<< PAGE_SHIFT
;
247 unsigned long arch_mmap_rnd(void)
249 return (get_random_int() & MMAP_RND_MASK
) << PAGE_SHIFT
;
252 static unsigned long mmap_legacy_base(void)
254 return TASK_UNMAPPED_BASE
+ mmap_rnd();
258 * This function, called very early during the creation of a new
259 * process VM image, sets up which VM layout function to use:
261 void arch_pick_mmap_layout(struct mm_struct
*mm
, struct rlimit
*rlim_stack
)
263 mm
->mmap_legacy_base
= mmap_legacy_base();
264 mm
->mmap_base
= mmap_upper_limit(rlim_stack
);
266 if (mmap_is_legacy()) {
267 mm
->mmap_base
= mm
->mmap_legacy_base
;
268 mm
->get_unmapped_area
= arch_get_unmapped_area
;
270 mm
->get_unmapped_area
= arch_get_unmapped_area_topdown
;
275 asmlinkage
unsigned long sys_mmap2(unsigned long addr
, unsigned long len
,
276 unsigned long prot
, unsigned long flags
, unsigned long fd
,
279 /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
281 return ksys_mmap_pgoff(addr
, len
, prot
, flags
, fd
,
282 pgoff
>> (PAGE_SHIFT
- 12));
285 asmlinkage
unsigned long sys_mmap(unsigned long addr
, unsigned long len
,
286 unsigned long prot
, unsigned long flags
, unsigned long fd
,
287 unsigned long offset
)
289 if (!(offset
& ~PAGE_MASK
)) {
290 return ksys_mmap_pgoff(addr
, len
, prot
, flags
, fd
,
291 offset
>> PAGE_SHIFT
);
297 /* Fucking broken ABI */
300 asmlinkage
long parisc_truncate64(const char __user
* path
,
301 unsigned int high
, unsigned int low
)
303 return ksys_truncate(path
, (long)high
<< 32 | low
);
306 asmlinkage
long parisc_ftruncate64(unsigned int fd
,
307 unsigned int high
, unsigned int low
)
309 return ksys_ftruncate(fd
, (long)high
<< 32 | low
);
312 /* stubs for the benefit of the syscall_table since truncate64 and truncate
313 * are identical on LP64 */
314 asmlinkage
long sys_truncate64(const char __user
* path
, unsigned long length
)
316 return ksys_truncate(path
, length
);
318 asmlinkage
long sys_ftruncate64(unsigned int fd
, unsigned long length
)
320 return ksys_ftruncate(fd
, length
);
322 asmlinkage
long sys_fcntl64(unsigned int fd
, unsigned int cmd
, unsigned long arg
)
324 return sys_fcntl(fd
, cmd
, arg
);
328 asmlinkage
long parisc_truncate64(const char __user
* path
,
329 unsigned int high
, unsigned int low
)
331 return ksys_truncate(path
, (loff_t
)high
<< 32 | low
);
334 asmlinkage
long parisc_ftruncate64(unsigned int fd
,
335 unsigned int high
, unsigned int low
)
337 return sys_ftruncate64(fd
, (loff_t
)high
<< 32 | low
);
341 asmlinkage ssize_t
parisc_pread64(unsigned int fd
, char __user
*buf
, size_t count
,
342 unsigned int high
, unsigned int low
)
344 return ksys_pread64(fd
, buf
, count
, (loff_t
)high
<< 32 | low
);
347 asmlinkage ssize_t
parisc_pwrite64(unsigned int fd
, const char __user
*buf
,
348 size_t count
, unsigned int high
, unsigned int low
)
350 return ksys_pwrite64(fd
, buf
, count
, (loff_t
)high
<< 32 | low
);
353 asmlinkage ssize_t
parisc_readahead(int fd
, unsigned int high
, unsigned int low
,
356 return ksys_readahead(fd
, (loff_t
)high
<< 32 | low
, count
);
359 asmlinkage
long parisc_fadvise64_64(int fd
,
360 unsigned int high_off
, unsigned int low_off
,
361 unsigned int high_len
, unsigned int low_len
, int advice
)
363 return ksys_fadvise64_64(fd
, (loff_t
)high_off
<< 32 | low_off
,
364 (loff_t
)high_len
<< 32 | low_len
, advice
);
367 asmlinkage
long parisc_sync_file_range(int fd
,
368 u32 hi_off
, u32 lo_off
, u32 hi_nbytes
, u32 lo_nbytes
,
371 return ksys_sync_file_range(fd
, (loff_t
)hi_off
<< 32 | lo_off
,
372 (loff_t
)hi_nbytes
<< 32 | lo_nbytes
, flags
);
375 asmlinkage
long parisc_fallocate(int fd
, int mode
, u32 offhi
, u32 offlo
,
376 u32 lenhi
, u32 lenlo
)
378 return ksys_fallocate(fd
, mode
, ((u64
)offhi
<< 32) | offlo
,
379 ((u64
)lenhi
<< 32) | lenlo
);
382 long parisc_personality(unsigned long personality
)
386 if (personality(current
->personality
) == PER_LINUX32
387 && personality(personality
) == PER_LINUX
)
388 personality
= (personality
& ~PER_MASK
) | PER_LINUX32
;
390 err
= sys_personality(personality
);
391 if (personality(err
) == PER_LINUX32
)
392 err
= (err
& ~PER_MASK
) | PER_LINUX
;
398 * Up to kernel v5.9 we defined O_NONBLOCK as 000200004,
399 * since then O_NONBLOCK is defined as 000200000.
401 * The following wrapper functions mask out the old
402 * O_NDELAY bit from calls which use O_NONBLOCK.
404 * XXX: Remove those in year 2022 (or later)?
407 #define O_NONBLOCK_OLD 000200004
408 #define O_NONBLOCK_MASK_OUT (O_NONBLOCK_OLD & ~O_NONBLOCK)
410 static int FIX_O_NONBLOCK(int flags
)
412 if (flags
& O_NONBLOCK_MASK_OUT
) {
413 struct task_struct
*tsk
= current
;
414 pr_warn_once("%s(%d) uses a deprecated O_NONBLOCK value.\n",
415 tsk
->comm
, tsk
->pid
);
417 return flags
& ~O_NONBLOCK_MASK_OUT
;
420 asmlinkage
long parisc_timerfd_create(int clockid
, int flags
)
422 flags
= FIX_O_NONBLOCK(flags
);
423 return sys_timerfd_create(clockid
, flags
);
426 asmlinkage
long parisc_signalfd4(int ufd
, sigset_t __user
*user_mask
,
427 size_t sizemask
, int flags
)
429 flags
= FIX_O_NONBLOCK(flags
);
430 return sys_signalfd4(ufd
, user_mask
, sizemask
, flags
);
434 asmlinkage
long parisc_compat_signalfd4(int ufd
,
435 compat_sigset_t __user
*user_mask
,
436 compat_size_t sizemask
, int flags
)
438 flags
= FIX_O_NONBLOCK(flags
);
439 return compat_sys_signalfd4(ufd
, user_mask
, sizemask
, flags
);
443 asmlinkage
long parisc_eventfd2(unsigned int count
, int flags
)
445 flags
= FIX_O_NONBLOCK(flags
);
446 return sys_eventfd2(count
, flags
);
449 asmlinkage
long parisc_userfaultfd(int flags
)
451 flags
= FIX_O_NONBLOCK(flags
);
452 return sys_userfaultfd(flags
);
455 asmlinkage
long parisc_pipe2(int __user
*fildes
, int flags
)
457 flags
= FIX_O_NONBLOCK(flags
);
458 return sys_pipe2(fildes
, flags
);
461 asmlinkage
long parisc_inotify_init1(int flags
)
463 flags
= FIX_O_NONBLOCK(flags
);
464 return sys_inotify_init1(flags
);