Linux Audio

Check our new training course

Loading...
v4.6
 
  1
  2/*
  3 *    PARISC specific syscalls
  4 *
  5 *    Copyright (C) 1999-2003 Matthew Wilcox <willy at parisc-linux.org>
  6 *    Copyright (C) 2000-2003 Paul Bame <bame at parisc-linux.org>
  7 *    Copyright (C) 2001 Thomas Bogendoerfer <tsbogend at parisc-linux.org>
  8 *    Copyright (C) 1999-2014 Helge Deller <deller@gmx.de>
  9 *
 10 *
 11 *    This program is free software; you can redistribute it and/or modify
 12 *    it under the terms of the GNU General Public License as published by
 13 *    the Free Software Foundation; either version 2 of the License, or
 14 *    (at your option) any later version.
 15 *
 16 *    This program is distributed in the hope that it will be useful,
 17 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19 *    GNU General Public License for more details.
 20 *
 21 *    You should have received a copy of the GNU General Public License
 22 *    along with this program; if not, write to the Free Software
 23 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 24 */
 25
 26#include <asm/uaccess.h>
 27#include <asm/elf.h>
 28#include <linux/file.h>
 29#include <linux/fs.h>
 30#include <linux/linkage.h>
 31#include <linux/mm.h>
 32#include <linux/mman.h>
 
 
 33#include <linux/shm.h>
 34#include <linux/syscalls.h>
 35#include <linux/utsname.h>
 36#include <linux/personality.h>
 37#include <linux/random.h>
 
 38
 39/* we construct an artificial offset for the mapping based on the physical
 40 * address of the kernel mapping variable */
 41#define GET_LAST_MMAP(filp)		\
 42	(filp ? ((unsigned long) filp->f_mapping) >> 8 : 0UL)
 43#define SET_LAST_MMAP(filp, val)	\
 44	 { /* nothing */ }
 45
 46static int get_offset(unsigned int last_mmap)
 47{
 48	return (last_mmap & (SHM_COLOUR-1)) >> PAGE_SHIFT;
 49}
 50
 51static unsigned long shared_align_offset(unsigned int last_mmap,
 52					 unsigned long pgoff)
 53{
 54	return (get_offset(last_mmap) + pgoff) << PAGE_SHIFT;
 55}
 56
 57static inline unsigned long COLOR_ALIGN(unsigned long addr,
 58			 unsigned int last_mmap, unsigned long pgoff)
 59{
 60	unsigned long base = (addr+SHM_COLOUR-1) & ~(SHM_COLOUR-1);
 61	unsigned long off  = (SHM_COLOUR-1) &
 62		(shared_align_offset(last_mmap, pgoff) << PAGE_SHIFT);
 63
 64	return base + off;
 65}
 66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 67/*
 68 * Top of mmap area (just below the process stack).
 69 */
 70
 71static unsigned long mmap_upper_limit(void)
 
 
 
 
 
 72{
 73	unsigned long stack_base;
 74
 75	/* Limit stack size - see setup_arg_pages() in fs/exec.c */
 76	stack_base = rlimit_max(RLIMIT_STACK);
 77	if (stack_base > STACK_SIZE_MAX)
 78		stack_base = STACK_SIZE_MAX;
 
 79
 80	/* Add space for stack randomization. */
 81	stack_base += (STACK_RND_MASK << PAGE_SHIFT);
 
 82
 83	return PAGE_ALIGN(STACK_TOP - stack_base);
 84}
 85
 86
 87unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
 88		unsigned long len, unsigned long pgoff, unsigned long flags)
 89{
 90	struct mm_struct *mm = current->mm;
 91	struct vm_area_struct *vma;
 92	unsigned long task_size = TASK_SIZE;
 93	int do_color_align, last_mmap;
 94	struct vm_unmapped_area_info info;
 95
 96	if (len > task_size)
 97		return -ENOMEM;
 98
 99	do_color_align = 0;
100	if (filp || (flags & MAP_SHARED))
101		do_color_align = 1;
102	last_mmap = GET_LAST_MMAP(filp);
103
104	if (flags & MAP_FIXED) {
105		if ((flags & MAP_SHARED) && last_mmap &&
106		    (addr - shared_align_offset(last_mmap, pgoff))
107				& (SHM_COLOUR - 1))
108			return -EINVAL;
109		goto found_addr;
110	}
111
112	if (addr) {
113		if (do_color_align && last_mmap)
114			addr = COLOR_ALIGN(addr, last_mmap, pgoff);
115		else
116			addr = PAGE_ALIGN(addr);
117
118		vma = find_vma(mm, addr);
119		if (task_size - len >= addr &&
120		    (!vma || addr + len <= vma->vm_start))
 
121			goto found_addr;
122	}
123
124	info.flags = 0;
125	info.length = len;
126	info.low_limit = mm->mmap_legacy_base;
127	info.high_limit = mmap_upper_limit();
128	info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
129	info.align_offset = shared_align_offset(last_mmap, pgoff);
130	addr = vm_unmapped_area(&info);
131
132found_addr:
133	if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
134		SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
135
136	return addr;
137}
138
139unsigned long
140arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
141			  const unsigned long len, const unsigned long pgoff,
142			  const unsigned long flags)
143{
144	struct vm_area_struct *vma;
145	struct mm_struct *mm = current->mm;
146	unsigned long addr = addr0;
147	int do_color_align, last_mmap;
148	struct vm_unmapped_area_info info;
149
150#ifdef CONFIG_64BIT
151	/* This should only ever run for 32-bit processes.  */
152	BUG_ON(!test_thread_flag(TIF_32BIT));
153#endif
154
155	/* requested length too big for entire address space */
156	if (len > TASK_SIZE)
157		return -ENOMEM;
158
159	do_color_align = 0;
160	if (filp || (flags & MAP_SHARED))
161		do_color_align = 1;
162	last_mmap = GET_LAST_MMAP(filp);
163
164	if (flags & MAP_FIXED) {
165		if ((flags & MAP_SHARED) && last_mmap &&
166		    (addr - shared_align_offset(last_mmap, pgoff))
167			& (SHM_COLOUR - 1))
168			return -EINVAL;
169		goto found_addr;
170	}
171
172	/* requesting a specific address */
173	if (addr) {
174		if (do_color_align && last_mmap)
175			addr = COLOR_ALIGN(addr, last_mmap, pgoff);
176		else
177			addr = PAGE_ALIGN(addr);
178		vma = find_vma(mm, addr);
 
179		if (TASK_SIZE - len >= addr &&
180		    (!vma || addr + len <= vma->vm_start))
 
181			goto found_addr;
182	}
183
184	info.flags = VM_UNMAPPED_AREA_TOPDOWN;
185	info.length = len;
186	info.low_limit = PAGE_SIZE;
187	info.high_limit = mm->mmap_base;
188	info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
189	info.align_offset = shared_align_offset(last_mmap, pgoff);
190	addr = vm_unmapped_area(&info);
191	if (!(addr & ~PAGE_MASK))
192		goto found_addr;
193	VM_BUG_ON(addr != -ENOMEM);
194
195	/*
196	 * A failed mmap() very likely causes application failure,
197	 * so fall back to the bottom-up function here. This scenario
198	 * can happen with large stack limits and large mmap()
199	 * allocations.
200	 */
201	return arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
202
203found_addr:
204	if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
205		SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
206
207	return addr;
208}
209
210static int mmap_is_legacy(void)
211{
212	if (current->personality & ADDR_COMPAT_LAYOUT)
213		return 1;
214
215	/* parisc stack always grows up - so a unlimited stack should
216	 * not be an indicator to use the legacy memory layout.
217	 * if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
218	 *	return 1;
219	 */
220
221	return sysctl_legacy_va_layout;
222}
223
224static unsigned long mmap_rnd(void)
225{
226	unsigned long rnd = 0;
227
228	/*
229	*  8 bits of randomness in 32bit mmaps, 20 address space bits
230	* 28 bits of randomness in 64bit mmaps, 40 address space bits
231	*/
232	if (current->flags & PF_RANDOMIZE) {
233		if (is_32bit_task())
234			rnd = get_random_int() % (1<<8);
235		else
236			rnd = get_random_int() % (1<<28);
237	}
238	return rnd << PAGE_SHIFT;
239}
240
 
 
 
 
 
241static unsigned long mmap_legacy_base(void)
242{
243	return TASK_UNMAPPED_BASE + mmap_rnd();
244}
245
246/*
247 * This function, called very early during the creation of a new
248 * process VM image, sets up which VM layout function to use:
249 */
250void arch_pick_mmap_layout(struct mm_struct *mm)
251{
252	mm->mmap_legacy_base = mmap_legacy_base();
253	mm->mmap_base = mmap_upper_limit();
254
255	if (mmap_is_legacy()) {
256		mm->mmap_base = mm->mmap_legacy_base;
257		mm->get_unmapped_area = arch_get_unmapped_area;
258	} else {
259		mm->get_unmapped_area = arch_get_unmapped_area_topdown;
260	}
261}
262
263
264asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
265	unsigned long prot, unsigned long flags, unsigned long fd,
266	unsigned long pgoff)
267{
268	/* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
269	   we have. */
270	return sys_mmap_pgoff(addr, len, prot, flags, fd,
271			      pgoff >> (PAGE_SHIFT - 12));
272}
273
274asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
275		unsigned long prot, unsigned long flags, unsigned long fd,
276		unsigned long offset)
277{
278	if (!(offset & ~PAGE_MASK)) {
279		return sys_mmap_pgoff(addr, len, prot, flags, fd,
280					offset >> PAGE_SHIFT);
281	} else {
282		return -EINVAL;
283	}
284}
285
286/* Fucking broken ABI */
287
288#ifdef CONFIG_64BIT
289asmlinkage long parisc_truncate64(const char __user * path,
290					unsigned int high, unsigned int low)
291{
292	return sys_truncate(path, (long)high << 32 | low);
293}
294
295asmlinkage long parisc_ftruncate64(unsigned int fd,
296					unsigned int high, unsigned int low)
297{
298	return sys_ftruncate(fd, (long)high << 32 | low);
299}
300
301/* stubs for the benefit of the syscall_table since truncate64 and truncate 
302 * are identical on LP64 */
303asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
304{
305	return sys_truncate(path, length);
306}
307asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
308{
309	return sys_ftruncate(fd, length);
310}
311asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
312{
313	return sys_fcntl(fd, cmd, arg);
314}
315#else
316
317asmlinkage long parisc_truncate64(const char __user * path,
318					unsigned int high, unsigned int low)
319{
320	return sys_truncate64(path, (loff_t)high << 32 | low);
321}
322
323asmlinkage long parisc_ftruncate64(unsigned int fd,
324					unsigned int high, unsigned int low)
325{
326	return sys_ftruncate64(fd, (loff_t)high << 32 | low);
327}
328#endif
329
330asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
331					unsigned int high, unsigned int low)
332{
333	return sys_pread64(fd, buf, count, (loff_t)high << 32 | low);
334}
335
336asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
337			size_t count, unsigned int high, unsigned int low)
338{
339	return sys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
340}
341
342asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
343		                    size_t count)
344{
345	return sys_readahead(fd, (loff_t)high << 32 | low, count);
346}
347
348asmlinkage long parisc_fadvise64_64(int fd,
349			unsigned int high_off, unsigned int low_off,
350			unsigned int high_len, unsigned int low_len, int advice)
351{
352	return sys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
353			(loff_t)high_len << 32 | low_len, advice);
354}
355
356asmlinkage long parisc_sync_file_range(int fd,
357			u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
358			unsigned int flags)
359{
360	return sys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
361			(loff_t)hi_nbytes << 32 | lo_nbytes, flags);
362}
363
364asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
365				u32 lenhi, u32 lenlo)
366{
367        return sys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
368                             ((u64)lenhi << 32) | lenlo);
369}
370
371long parisc_personality(unsigned long personality)
372{
373	long err;
374
375	if (personality(current->personality) == PER_LINUX32
376	    && personality(personality) == PER_LINUX)
377		personality = (personality & ~PER_MASK) | PER_LINUX32;
378
379	err = sys_personality(personality);
380	if (personality(err) == PER_LINUX32)
381		err = (err & ~PER_MASK) | PER_LINUX;
382
383	return err;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
384}
v6.2
  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/* 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)	\
 33	 { /* nothing */ }
 34
 35static int get_offset(unsigned int last_mmap)
 36{
 37	return (last_mmap & (SHM_COLOUR-1)) >> PAGE_SHIFT;
 38}
 39
 40static unsigned long shared_align_offset(unsigned int last_mmap,
 41					 unsigned long pgoff)
 42{
 43	return (get_offset(last_mmap) + pgoff) << PAGE_SHIFT;
 44}
 45
 46static inline unsigned long COLOR_ALIGN(unsigned long addr,
 47			 unsigned int last_mmap, unsigned long pgoff)
 48{
 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);
 52
 53	return base + off;
 54}
 55
 56
 57#define STACK_SIZE_DEFAULT (USER_WIDE_MODE			\
 58			? (1 << 30)	/* 1 GB */		\
 59			: (CONFIG_STACK_MAX_DEFAULT_SIZE_MB*1024*1024))
 60
 61unsigned long calc_max_stack_size(unsigned long stack_max)
 62{
 63#ifdef CONFIG_COMPAT
 64	if (!USER_WIDE_MODE && (stack_max == COMPAT_RLIM_INFINITY))
 65		stack_max = STACK_SIZE_DEFAULT;
 66	else
 67#endif
 68	if (stack_max == RLIM_INFINITY)
 69		stack_max = STACK_SIZE_DEFAULT;
 70
 71	return stack_max;
 72}
 73
 74
 75/*
 76 * Top of mmap area (just below the process stack).
 77 */
 78
 79/*
 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().
 83 */
 84static unsigned long mmap_upper_limit(struct rlimit *rlim_stack)
 85{
 86	unsigned long stack_base;
 87
 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);
 91
 92	stack_base = calc_max_stack_size(stack_base);
 93
 94	/* Add space for stack randomization. */
 95	if (current->flags & PF_RANDOMIZE)
 96		stack_base += (STACK_RND_MASK << PAGE_SHIFT);
 97
 98	return PAGE_ALIGN(STACK_TOP - stack_base);
 99}
100
101
102unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
103		unsigned long len, unsigned long pgoff, unsigned long flags)
104{
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;
110
111	if (len > task_size)
112		return -ENOMEM;
113
114	do_color_align = 0;
115	if (filp || (flags & MAP_SHARED))
116		do_color_align = 1;
117	last_mmap = GET_LAST_MMAP(filp);
118
119	if (flags & MAP_FIXED) {
120		if ((flags & MAP_SHARED) && last_mmap &&
121		    (addr - shared_align_offset(last_mmap, pgoff))
122				& (SHM_COLOUR - 1))
123			return -EINVAL;
124		goto found_addr;
125	}
126
127	if (addr) {
128		if (do_color_align && last_mmap)
129			addr = COLOR_ALIGN(addr, last_mmap, pgoff);
130		else
131			addr = PAGE_ALIGN(addr);
132
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)))
137			goto found_addr;
138	}
139
140	info.flags = 0;
141	info.length = len;
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);
147
148found_addr:
149	if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
150		SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
151
152	return addr;
153}
154
155unsigned long
156arch_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)
159{
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;
165
 
 
 
 
 
166	/* requested length too big for entire address space */
167	if (len > TASK_SIZE)
168		return -ENOMEM;
169
170	do_color_align = 0;
171	if (filp || (flags & MAP_SHARED))
172		do_color_align = 1;
173	last_mmap = GET_LAST_MMAP(filp);
174
175	if (flags & MAP_FIXED) {
176		if ((flags & MAP_SHARED) && last_mmap &&
177		    (addr - shared_align_offset(last_mmap, pgoff))
178			& (SHM_COLOUR - 1))
179			return -EINVAL;
180		goto found_addr;
181	}
182
183	/* requesting a specific address */
184	if (addr) {
185		if (do_color_align && last_mmap)
186			addr = COLOR_ALIGN(addr, last_mmap, pgoff);
187		else
188			addr = PAGE_ALIGN(addr);
189
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)))
194			goto found_addr;
195	}
196
197	info.flags = VM_UNMAPPED_AREA_TOPDOWN;
198	info.length = len;
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))
205		goto found_addr;
206	VM_BUG_ON(addr != -ENOMEM);
207
208	/*
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()
212	 * allocations.
213	 */
214	return arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
215
216found_addr:
217	if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
218		SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
219
220	return addr;
221}
222
223static int mmap_is_legacy(void)
224{
225	if (current->personality & ADDR_COMPAT_LAYOUT)
226		return 1;
227
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)
231	 *	return 1;
232	 */
233
234	return sysctl_legacy_va_layout;
235}
236
237static unsigned long mmap_rnd(void)
238{
239	unsigned long rnd = 0;
240
241	if (current->flags & PF_RANDOMIZE)
242		rnd = get_random_u32() & MMAP_RND_MASK;
243
 
 
 
 
 
 
 
244	return rnd << PAGE_SHIFT;
245}
246
247unsigned long arch_mmap_rnd(void)
248{
249	return (get_random_u32() & MMAP_RND_MASK) << PAGE_SHIFT;
250}
251
252static unsigned long mmap_legacy_base(void)
253{
254	return TASK_UNMAPPED_BASE + mmap_rnd();
255}
256
257/*
258 * This function, called very early during the creation of a new
259 * process VM image, sets up which VM layout function to use:
260 */
261void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
262{
263	mm->mmap_legacy_base = mmap_legacy_base();
264	mm->mmap_base = mmap_upper_limit(rlim_stack);
265
266	if (mmap_is_legacy()) {
267		mm->mmap_base = mm->mmap_legacy_base;
268		mm->get_unmapped_area = arch_get_unmapped_area;
269	} else {
270		mm->get_unmapped_area = arch_get_unmapped_area_topdown;
271	}
272}
273
274
275asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
276	unsigned long prot, unsigned long flags, unsigned long fd,
277	unsigned long pgoff)
278{
279	/* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
280	   we have. */
281	return ksys_mmap_pgoff(addr, len, prot, flags, fd,
282			       pgoff >> (PAGE_SHIFT - 12));
283}
284
285asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
286		unsigned long prot, unsigned long flags, unsigned long fd,
287		unsigned long offset)
288{
289	if (!(offset & ~PAGE_MASK)) {
290		return ksys_mmap_pgoff(addr, len, prot, flags, fd,
291					offset >> PAGE_SHIFT);
292	} else {
293		return -EINVAL;
294	}
295}
296
297/* Fucking broken ABI */
298
299#ifdef CONFIG_64BIT
300asmlinkage long parisc_truncate64(const char __user * path,
301					unsigned int high, unsigned int low)
302{
303	return ksys_truncate(path, (long)high << 32 | low);
304}
305
306asmlinkage long parisc_ftruncate64(unsigned int fd,
307					unsigned int high, unsigned int low)
308{
309	return ksys_ftruncate(fd, (long)high << 32 | low);
310}
311
312/* stubs for the benefit of the syscall_table since truncate64 and truncate 
313 * are identical on LP64 */
314asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
315{
316	return ksys_truncate(path, length);
317}
318asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
319{
320	return ksys_ftruncate(fd, length);
321}
322asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
323{
324	return sys_fcntl(fd, cmd, arg);
325}
326#else
327
328asmlinkage long parisc_truncate64(const char __user * path,
329					unsigned int high, unsigned int low)
330{
331	return ksys_truncate(path, (loff_t)high << 32 | low);
332}
333
334asmlinkage long parisc_ftruncate64(unsigned int fd,
335					unsigned int high, unsigned int low)
336{
337	return sys_ftruncate64(fd, (loff_t)high << 32 | low);
338}
339#endif
340
341asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
342					unsigned int high, unsigned int low)
343{
344	return ksys_pread64(fd, buf, count, (loff_t)high << 32 | low);
345}
346
347asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
348			size_t count, unsigned int high, unsigned int low)
349{
350	return ksys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
351}
352
353asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
354		                    size_t count)
355{
356	return ksys_readahead(fd, (loff_t)high << 32 | low, count);
357}
358
359asmlinkage 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)
362{
363	return ksys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
364			(loff_t)high_len << 32 | low_len, advice);
365}
366
367asmlinkage long parisc_sync_file_range(int fd,
368			u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
369			unsigned int flags)
370{
371	return ksys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
372			(loff_t)hi_nbytes << 32 | lo_nbytes, flags);
373}
374
375asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
376				u32 lenhi, u32 lenlo)
377{
378	return ksys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
379			      ((u64)lenhi << 32) | lenlo);
380}
381
382long parisc_personality(unsigned long personality)
383{
384	long err;
385
386	if (personality(current->personality) == PER_LINUX32
387	    && personality(personality) == PER_LINUX)
388		personality = (personality & ~PER_MASK) | PER_LINUX32;
389
390	err = sys_personality(personality);
391	if (personality(err) == PER_LINUX32)
392		err = (err & ~PER_MASK) | PER_LINUX;
393
394	return err;
395}
396
397/*
398 * Up to kernel v5.9 we defined O_NONBLOCK as 000200004,
399 * since then O_NONBLOCK is defined as 000200000.
400 *
401 * The following wrapper functions mask out the old
402 * O_NDELAY bit from calls which use O_NONBLOCK.
403 *
404 * XXX: Remove those in year 2022 (or later)?
405 */
406
407#define O_NONBLOCK_OLD		000200004
408#define O_NONBLOCK_MASK_OUT	(O_NONBLOCK_OLD & ~O_NONBLOCK)
409
410static int FIX_O_NONBLOCK(int flags)
411{
412	if ((flags & O_NONBLOCK_MASK_OUT) &&
413			!test_thread_flag(TIF_NONBLOCK_WARNING)) {
414		set_thread_flag(TIF_NONBLOCK_WARNING);
415		pr_warn("%s(%d) uses a deprecated O_NONBLOCK value."
416			" Please recompile with newer glibc.\n",
417			current->comm, current->pid);
418	}
419	return flags & ~O_NONBLOCK_MASK_OUT;
420}
421
422asmlinkage long parisc_timerfd_create(int clockid, int flags)
423{
424	flags = FIX_O_NONBLOCK(flags);
425	return sys_timerfd_create(clockid, flags);
426}
427
428asmlinkage long parisc_signalfd4(int ufd, sigset_t __user *user_mask,
429	size_t sizemask, int flags)
430{
431	flags = FIX_O_NONBLOCK(flags);
432	return sys_signalfd4(ufd, user_mask, sizemask, flags);
433}
434
435#ifdef CONFIG_COMPAT
436asmlinkage long parisc_compat_signalfd4(int ufd,
437	compat_sigset_t __user *user_mask,
438	compat_size_t sizemask, int flags)
439{
440	flags = FIX_O_NONBLOCK(flags);
441	return compat_sys_signalfd4(ufd, user_mask, sizemask, flags);
442}
443#endif
444
445asmlinkage long parisc_eventfd2(unsigned int count, int flags)
446{
447	flags = FIX_O_NONBLOCK(flags);
448	return sys_eventfd2(count, flags);
449}
450
451asmlinkage long parisc_userfaultfd(int flags)
452{
453	flags = FIX_O_NONBLOCK(flags);
454	return sys_userfaultfd(flags);
455}
456
457asmlinkage long parisc_pipe2(int __user *fildes, int flags)
458{
459	flags = FIX_O_NONBLOCK(flags);
460	return sys_pipe2(fildes, flags);
461}
462
463asmlinkage long parisc_inotify_init1(int flags)
464{
465	flags = FIX_O_NONBLOCK(flags);
466	return sys_inotify_init1(flags);
467}
468
469/*
470 * madvise() wrapper
471 *
472 * Up to kernel v6.1 parisc has different values than all other
473 * platforms for the MADV_xxx flags listed below.
474 * To keep binary compatibility with existing userspace programs
475 * translate the former values to the new values.
476 *
477 * XXX: Remove this wrapper in year 2025 (or later)
478 */
479
480asmlinkage notrace long parisc_madvise(unsigned long start, size_t len_in, int behavior)
481{
482	switch (behavior) {
483	case 65: behavior = MADV_MERGEABLE;	break;
484	case 66: behavior = MADV_UNMERGEABLE;	break;
485	case 67: behavior = MADV_HUGEPAGE;	break;
486	case 68: behavior = MADV_NOHUGEPAGE;	break;
487	case 69: behavior = MADV_DONTDUMP;	break;
488	case 70: behavior = MADV_DODUMP;	break;
489	case 71: behavior = MADV_WIPEONFORK;	break;
490	case 72: behavior = MADV_KEEPONFORK;	break;
491	case 73: behavior = MADV_COLLAPSE;	break;
492	}
493
494	return sys_madvise(start, len_in, behavior);
495}