xref: /openbmc/linux/arch/parisc/kernel/sys_parisc.c (revision e68235c8)
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 
28 /*
29  * Construct an artificial page offset for the mapping based on the physical
30  * address of the kernel file mapping variable.
31  */
32 #define GET_FILP_PGOFF(filp)		\
33 	(filp ? (((unsigned long) filp->f_mapping) >> 8)	\
34 		 & ((SHM_COLOUR-1) >> PAGE_SHIFT) : 0UL)
35 
36 static unsigned long shared_align_offset(unsigned long filp_pgoff,
37 					 unsigned long pgoff)
38 {
39 	return (filp_pgoff + pgoff) << PAGE_SHIFT;
40 }
41 
42 static inline unsigned long COLOR_ALIGN(unsigned long addr,
43 			 unsigned long filp_pgoff, unsigned long pgoff)
44 {
45 	unsigned long base = (addr+SHM_COLOUR-1) & ~(SHM_COLOUR-1);
46 	unsigned long off  = (SHM_COLOUR-1) &
47 		shared_align_offset(filp_pgoff, pgoff);
48 	return base + off;
49 }
50 
51 
52 #define STACK_SIZE_DEFAULT (USER_WIDE_MODE			\
53 			? (1 << 30)	/* 1 GB */		\
54 			: (CONFIG_STACK_MAX_DEFAULT_SIZE_MB*1024*1024))
55 
56 unsigned long calc_max_stack_size(unsigned long stack_max)
57 {
58 #ifdef CONFIG_COMPAT
59 	if (!USER_WIDE_MODE && (stack_max == COMPAT_RLIM_INFINITY))
60 		stack_max = STACK_SIZE_DEFAULT;
61 	else
62 #endif
63 	if (stack_max == RLIM_INFINITY)
64 		stack_max = STACK_SIZE_DEFAULT;
65 
66 	return stack_max;
67 }
68 
69 
70 /*
71  * Top of mmap area (just below the process stack).
72  */
73 
74 /*
75  * When called from arch_get_unmapped_area(), rlim_stack will be NULL,
76  * indicating that "current" should be used instead of a passed-in
77  * value from the exec bprm as done with arch_pick_mmap_layout().
78  */
79 static unsigned long mmap_upper_limit(struct rlimit *rlim_stack)
80 {
81 	unsigned long stack_base;
82 
83 	/* Limit stack size - see setup_arg_pages() in fs/exec.c */
84 	stack_base = rlim_stack ? rlim_stack->rlim_max
85 				: rlimit_max(RLIMIT_STACK);
86 
87 	stack_base = calc_max_stack_size(stack_base);
88 
89 	/* Add space for stack randomization. */
90 	if (current->flags & PF_RANDOMIZE)
91 		stack_base += (STACK_RND_MASK << PAGE_SHIFT);
92 
93 	return PAGE_ALIGN(STACK_TOP - stack_base);
94 }
95 
96 enum mmap_allocation_direction {UP, DOWN};
97 
98 static unsigned long arch_get_unmapped_area_common(struct file *filp,
99 	unsigned long addr, unsigned long len, unsigned long pgoff,
100 	unsigned long flags, enum mmap_allocation_direction dir)
101 {
102 	struct mm_struct *mm = current->mm;
103 	struct vm_area_struct *vma, *prev;
104 	unsigned long filp_pgoff;
105 	int do_color_align;
106 	struct vm_unmapped_area_info info;
107 
108 	if (unlikely(len > TASK_SIZE))
109 		return -ENOMEM;
110 
111 	do_color_align = 0;
112 	if (filp || (flags & MAP_SHARED))
113 		do_color_align = 1;
114 	filp_pgoff = GET_FILP_PGOFF(filp);
115 
116 	if (flags & MAP_FIXED) {
117 		/* Even MAP_FIXED mappings must reside within TASK_SIZE */
118 		if (TASK_SIZE - len < addr)
119 			return -EINVAL;
120 
121 		if ((flags & MAP_SHARED) && filp &&
122 		    (addr - shared_align_offset(filp_pgoff, pgoff))
123 				& (SHM_COLOUR - 1))
124 			return -EINVAL;
125 		return addr;
126 	}
127 
128 	if (addr) {
129 		if (do_color_align)
130 			addr = COLOR_ALIGN(addr, filp_pgoff, pgoff);
131 		else
132 			addr = PAGE_ALIGN(addr);
133 
134 		vma = find_vma_prev(mm, addr, &prev);
135 		if (TASK_SIZE - len >= addr &&
136 		    (!vma || addr + len <= vm_start_gap(vma)) &&
137 		    (!prev || addr >= vm_end_gap(prev)))
138 			return addr;
139 	}
140 
141 	info.length = len;
142 	info.align_mask = do_color_align ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
143 	info.align_offset = shared_align_offset(filp_pgoff, pgoff);
144 
145 	if (dir == DOWN) {
146 		info.flags = VM_UNMAPPED_AREA_TOPDOWN;
147 		info.low_limit = PAGE_SIZE;
148 		info.high_limit = mm->mmap_base;
149 		addr = vm_unmapped_area(&info);
150 		if (!(addr & ~PAGE_MASK))
151 			return addr;
152 		VM_BUG_ON(addr != -ENOMEM);
153 
154 		/*
155 		 * A failed mmap() very likely causes application failure,
156 		 * so fall back to the bottom-up function here. This scenario
157 		 * can happen with large stack limits and large mmap()
158 		 * allocations.
159 		 */
160 	}
161 
162 	info.flags = 0;
163 	info.low_limit = mm->mmap_legacy_base;
164 	info.high_limit = mmap_upper_limit(NULL);
165 	return vm_unmapped_area(&info);
166 }
167 
168 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
169 	unsigned long len, unsigned long pgoff, unsigned long flags)
170 {
171 	return arch_get_unmapped_area_common(filp,
172 			addr, len, pgoff, flags, UP);
173 }
174 
175 unsigned long arch_get_unmapped_area_topdown(struct file *filp,
176 	unsigned long addr, unsigned long len, unsigned long pgoff,
177 	unsigned long flags)
178 {
179 	return arch_get_unmapped_area_common(filp,
180 			addr, len, pgoff, flags, DOWN);
181 }
182 
183 static int mmap_is_legacy(void)
184 {
185 	if (current->personality & ADDR_COMPAT_LAYOUT)
186 		return 1;
187 
188 	/* parisc stack always grows up - so a unlimited stack should
189 	 * not be an indicator to use the legacy memory layout.
190 	 * if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
191 	 *	return 1;
192 	 */
193 
194 	return sysctl_legacy_va_layout;
195 }
196 
197 static unsigned long mmap_rnd(void)
198 {
199 	unsigned long rnd = 0;
200 
201 	if (current->flags & PF_RANDOMIZE)
202 		rnd = get_random_u32() & MMAP_RND_MASK;
203 
204 	return rnd << PAGE_SHIFT;
205 }
206 
207 unsigned long arch_mmap_rnd(void)
208 {
209 	return (get_random_u32() & MMAP_RND_MASK) << PAGE_SHIFT;
210 }
211 
212 static unsigned long mmap_legacy_base(void)
213 {
214 	return TASK_UNMAPPED_BASE + mmap_rnd();
215 }
216 
217 /*
218  * This function, called very early during the creation of a new
219  * process VM image, sets up which VM layout function to use:
220  */
221 void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
222 {
223 	mm->mmap_legacy_base = mmap_legacy_base();
224 	mm->mmap_base = mmap_upper_limit(rlim_stack);
225 
226 	if (mmap_is_legacy()) {
227 		mm->mmap_base = mm->mmap_legacy_base;
228 		mm->get_unmapped_area = arch_get_unmapped_area;
229 	} else {
230 		mm->get_unmapped_area = arch_get_unmapped_area_topdown;
231 	}
232 }
233 
234 
235 asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
236 	unsigned long prot, unsigned long flags, unsigned long fd,
237 	unsigned long pgoff)
238 {
239 	/* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
240 	   we have. */
241 	return ksys_mmap_pgoff(addr, len, prot, flags, fd,
242 			       pgoff >> (PAGE_SHIFT - 12));
243 }
244 
245 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
246 		unsigned long prot, unsigned long flags, unsigned long fd,
247 		unsigned long offset)
248 {
249 	if (!(offset & ~PAGE_MASK)) {
250 		return ksys_mmap_pgoff(addr, len, prot, flags, fd,
251 					offset >> PAGE_SHIFT);
252 	} else {
253 		return -EINVAL;
254 	}
255 }
256 
257 /* Fucking broken ABI */
258 
259 #ifdef CONFIG_64BIT
260 asmlinkage long parisc_truncate64(const char __user * path,
261 					unsigned int high, unsigned int low)
262 {
263 	return ksys_truncate(path, (long)high << 32 | low);
264 }
265 
266 asmlinkage long parisc_ftruncate64(unsigned int fd,
267 					unsigned int high, unsigned int low)
268 {
269 	return ksys_ftruncate(fd, (long)high << 32 | low);
270 }
271 
272 /* stubs for the benefit of the syscall_table since truncate64 and truncate
273  * are identical on LP64 */
274 asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
275 {
276 	return ksys_truncate(path, length);
277 }
278 asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
279 {
280 	return ksys_ftruncate(fd, length);
281 }
282 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
283 {
284 	return sys_fcntl(fd, cmd, arg);
285 }
286 #else
287 
288 asmlinkage long parisc_truncate64(const char __user * path,
289 					unsigned int high, unsigned int low)
290 {
291 	return ksys_truncate(path, (loff_t)high << 32 | low);
292 }
293 
294 asmlinkage long parisc_ftruncate64(unsigned int fd,
295 					unsigned int high, unsigned int low)
296 {
297 	return sys_ftruncate64(fd, (loff_t)high << 32 | low);
298 }
299 #endif
300 
301 asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
302 					unsigned int high, unsigned int low)
303 {
304 	return ksys_pread64(fd, buf, count, (loff_t)high << 32 | low);
305 }
306 
307 asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
308 			size_t count, unsigned int high, unsigned int low)
309 {
310 	return ksys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
311 }
312 
313 asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
314 		                    size_t count)
315 {
316 	return ksys_readahead(fd, (loff_t)high << 32 | low, count);
317 }
318 
319 asmlinkage long parisc_fadvise64_64(int fd,
320 			unsigned int high_off, unsigned int low_off,
321 			unsigned int high_len, unsigned int low_len, int advice)
322 {
323 	return ksys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
324 			(loff_t)high_len << 32 | low_len, advice);
325 }
326 
327 asmlinkage long parisc_sync_file_range(int fd,
328 			u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
329 			unsigned int flags)
330 {
331 	return ksys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
332 			(loff_t)hi_nbytes << 32 | lo_nbytes, flags);
333 }
334 
335 asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
336 				u32 lenhi, u32 lenlo)
337 {
338 	return ksys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
339 			      ((u64)lenhi << 32) | lenlo);
340 }
341 
342 long parisc_personality(unsigned long personality)
343 {
344 	long err;
345 
346 	if (personality(current->personality) == PER_LINUX32
347 	    && personality(personality) == PER_LINUX)
348 		personality = (personality & ~PER_MASK) | PER_LINUX32;
349 
350 	err = sys_personality(personality);
351 	if (personality(err) == PER_LINUX32)
352 		err = (err & ~PER_MASK) | PER_LINUX;
353 
354 	return err;
355 }
356 
357 /*
358  * Up to kernel v5.9 we defined O_NONBLOCK as 000200004,
359  * since then O_NONBLOCK is defined as 000200000.
360  *
361  * The following wrapper functions mask out the old
362  * O_NDELAY bit from calls which use O_NONBLOCK.
363  *
364  * XXX: Remove those in year 2022 (or later)?
365  */
366 
367 #define O_NONBLOCK_OLD		000200004
368 #define O_NONBLOCK_MASK_OUT	(O_NONBLOCK_OLD & ~O_NONBLOCK)
369 
370 static int FIX_O_NONBLOCK(int flags)
371 {
372 	if ((flags & O_NONBLOCK_MASK_OUT) &&
373 			!test_thread_flag(TIF_NONBLOCK_WARNING)) {
374 		set_thread_flag(TIF_NONBLOCK_WARNING);
375 		pr_warn("%s(%d) uses a deprecated O_NONBLOCK value."
376 			" Please recompile with newer glibc.\n",
377 			current->comm, current->pid);
378 	}
379 	return flags & ~O_NONBLOCK_MASK_OUT;
380 }
381 
382 asmlinkage long parisc_timerfd_create(int clockid, int flags)
383 {
384 	flags = FIX_O_NONBLOCK(flags);
385 	return sys_timerfd_create(clockid, flags);
386 }
387 
388 asmlinkage long parisc_signalfd4(int ufd, sigset_t __user *user_mask,
389 	size_t sizemask, int flags)
390 {
391 	flags = FIX_O_NONBLOCK(flags);
392 	return sys_signalfd4(ufd, user_mask, sizemask, flags);
393 }
394 
395 #ifdef CONFIG_COMPAT
396 asmlinkage long parisc_compat_signalfd4(int ufd,
397 	compat_sigset_t __user *user_mask,
398 	compat_size_t sizemask, int flags)
399 {
400 	flags = FIX_O_NONBLOCK(flags);
401 	return compat_sys_signalfd4(ufd, user_mask, sizemask, flags);
402 }
403 #endif
404 
405 asmlinkage long parisc_eventfd2(unsigned int count, int flags)
406 {
407 	flags = FIX_O_NONBLOCK(flags);
408 	return sys_eventfd2(count, flags);
409 }
410 
411 asmlinkage long parisc_userfaultfd(int flags)
412 {
413 	flags = FIX_O_NONBLOCK(flags);
414 	return sys_userfaultfd(flags);
415 }
416 
417 asmlinkage long parisc_pipe2(int __user *fildes, int flags)
418 {
419 	flags = FIX_O_NONBLOCK(flags);
420 	return sys_pipe2(fildes, flags);
421 }
422 
423 asmlinkage long parisc_inotify_init1(int flags)
424 {
425 	flags = FIX_O_NONBLOCK(flags);
426 	return sys_inotify_init1(flags);
427 }
428 
429 /*
430  * madvise() wrapper
431  *
432  * Up to kernel v6.1 parisc has different values than all other
433  * platforms for the MADV_xxx flags listed below.
434  * To keep binary compatibility with existing userspace programs
435  * translate the former values to the new values.
436  *
437  * XXX: Remove this wrapper in year 2025 (or later)
438  */
439 
440 asmlinkage notrace long parisc_madvise(unsigned long start, size_t len_in, int behavior)
441 {
442 	switch (behavior) {
443 	case 65: behavior = MADV_MERGEABLE;	break;
444 	case 66: behavior = MADV_UNMERGEABLE;	break;
445 	case 67: behavior = MADV_HUGEPAGE;	break;
446 	case 68: behavior = MADV_NOHUGEPAGE;	break;
447 	case 69: behavior = MADV_DONTDUMP;	break;
448 	case 70: behavior = MADV_DODUMP;	break;
449 	case 71: behavior = MADV_WIPEONFORK;	break;
450 	case 72: behavior = MADV_KEEPONFORK;	break;
451 	case 73: behavior = MADV_COLLAPSE;	break;
452 	}
453 
454 	return sys_madvise(start, len_in, behavior);
455 }
456