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