xref: /openbmc/linux/arch/parisc/kernel/sys_parisc.c (revision da2ef666)
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 <linux/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/sched/signal.h>
34 #include <linux/sched/mm.h>
35 #include <linux/shm.h>
36 #include <linux/syscalls.h>
37 #include <linux/utsname.h>
38 #include <linux/personality.h>
39 #include <linux/random.h>
40 
41 /* we construct an artificial offset for the mapping based on the physical
42  * address of the kernel mapping variable */
43 #define GET_LAST_MMAP(filp)		\
44 	(filp ? ((unsigned long) filp->f_mapping) >> 8 : 0UL)
45 #define SET_LAST_MMAP(filp, val)	\
46 	 { /* nothing */ }
47 
48 static int get_offset(unsigned int last_mmap)
49 {
50 	return (last_mmap & (SHM_COLOUR-1)) >> PAGE_SHIFT;
51 }
52 
53 static unsigned long shared_align_offset(unsigned int last_mmap,
54 					 unsigned long pgoff)
55 {
56 	return (get_offset(last_mmap) + pgoff) << PAGE_SHIFT;
57 }
58 
59 static inline unsigned long COLOR_ALIGN(unsigned long addr,
60 			 unsigned int last_mmap, unsigned long pgoff)
61 {
62 	unsigned long base = (addr+SHM_COLOUR-1) & ~(SHM_COLOUR-1);
63 	unsigned long off  = (SHM_COLOUR-1) &
64 		(shared_align_offset(last_mmap, pgoff) << PAGE_SHIFT);
65 
66 	return base + off;
67 }
68 
69 /*
70  * Top of mmap area (just below the process stack).
71  */
72 
73 /*
74  * When called from arch_get_unmapped_area(), rlim_stack will be NULL,
75  * indicating that "current" should be used instead of a passed-in
76  * value from the exec bprm as done with arch_pick_mmap_layout().
77  */
78 static unsigned long mmap_upper_limit(struct rlimit *rlim_stack)
79 {
80 	unsigned long stack_base;
81 
82 	/* Limit stack size - see setup_arg_pages() in fs/exec.c */
83 	stack_base = rlim_stack ? rlim_stack->rlim_max
84 				: rlimit_max(RLIMIT_STACK);
85 	if (stack_base > STACK_SIZE_MAX)
86 		stack_base = STACK_SIZE_MAX;
87 
88 	/* Add space for stack randomization. */
89 	stack_base += (STACK_RND_MASK << PAGE_SHIFT);
90 
91 	return PAGE_ALIGN(STACK_TOP - stack_base);
92 }
93 
94 
95 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
96 		unsigned long len, unsigned long pgoff, unsigned long flags)
97 {
98 	struct mm_struct *mm = current->mm;
99 	struct vm_area_struct *vma, *prev;
100 	unsigned long task_size = TASK_SIZE;
101 	int do_color_align, last_mmap;
102 	struct vm_unmapped_area_info info;
103 
104 	if (len > task_size)
105 		return -ENOMEM;
106 
107 	do_color_align = 0;
108 	if (filp || (flags & MAP_SHARED))
109 		do_color_align = 1;
110 	last_mmap = GET_LAST_MMAP(filp);
111 
112 	if (flags & MAP_FIXED) {
113 		if ((flags & MAP_SHARED) && last_mmap &&
114 		    (addr - shared_align_offset(last_mmap, pgoff))
115 				& (SHM_COLOUR - 1))
116 			return -EINVAL;
117 		goto found_addr;
118 	}
119 
120 	if (addr) {
121 		if (do_color_align && last_mmap)
122 			addr = COLOR_ALIGN(addr, last_mmap, pgoff);
123 		else
124 			addr = PAGE_ALIGN(addr);
125 
126 		vma = find_vma_prev(mm, addr, &prev);
127 		if (task_size - len >= addr &&
128 		    (!vma || addr + len <= vm_start_gap(vma)) &&
129 		    (!prev || addr >= vm_end_gap(prev)))
130 			goto found_addr;
131 	}
132 
133 	info.flags = 0;
134 	info.length = len;
135 	info.low_limit = mm->mmap_legacy_base;
136 	info.high_limit = mmap_upper_limit(NULL);
137 	info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
138 	info.align_offset = shared_align_offset(last_mmap, pgoff);
139 	addr = vm_unmapped_area(&info);
140 
141 found_addr:
142 	if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
143 		SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
144 
145 	return addr;
146 }
147 
148 unsigned long
149 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
150 			  const unsigned long len, const unsigned long pgoff,
151 			  const unsigned long flags)
152 {
153 	struct vm_area_struct *vma, *prev;
154 	struct mm_struct *mm = current->mm;
155 	unsigned long addr = addr0;
156 	int do_color_align, last_mmap;
157 	struct vm_unmapped_area_info info;
158 
159 	/* requested length too big for entire address space */
160 	if (len > TASK_SIZE)
161 		return -ENOMEM;
162 
163 	do_color_align = 0;
164 	if (filp || (flags & MAP_SHARED))
165 		do_color_align = 1;
166 	last_mmap = GET_LAST_MMAP(filp);
167 
168 	if (flags & MAP_FIXED) {
169 		if ((flags & MAP_SHARED) && last_mmap &&
170 		    (addr - shared_align_offset(last_mmap, pgoff))
171 			& (SHM_COLOUR - 1))
172 			return -EINVAL;
173 		goto found_addr;
174 	}
175 
176 	/* requesting a specific address */
177 	if (addr) {
178 		if (do_color_align && last_mmap)
179 			addr = COLOR_ALIGN(addr, last_mmap, pgoff);
180 		else
181 			addr = PAGE_ALIGN(addr);
182 
183 		vma = find_vma_prev(mm, addr, &prev);
184 		if (TASK_SIZE - len >= addr &&
185 		    (!vma || addr + len <= vm_start_gap(vma)) &&
186 		    (!prev || addr >= vm_end_gap(prev)))
187 			goto found_addr;
188 	}
189 
190 	info.flags = VM_UNMAPPED_AREA_TOPDOWN;
191 	info.length = len;
192 	info.low_limit = PAGE_SIZE;
193 	info.high_limit = mm->mmap_base;
194 	info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
195 	info.align_offset = shared_align_offset(last_mmap, pgoff);
196 	addr = vm_unmapped_area(&info);
197 	if (!(addr & ~PAGE_MASK))
198 		goto found_addr;
199 	VM_BUG_ON(addr != -ENOMEM);
200 
201 	/*
202 	 * A failed mmap() very likely causes application failure,
203 	 * so fall back to the bottom-up function here. This scenario
204 	 * can happen with large stack limits and large mmap()
205 	 * allocations.
206 	 */
207 	return arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
208 
209 found_addr:
210 	if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
211 		SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
212 
213 	return addr;
214 }
215 
216 static int mmap_is_legacy(void)
217 {
218 	if (current->personality & ADDR_COMPAT_LAYOUT)
219 		return 1;
220 
221 	/* parisc stack always grows up - so a unlimited stack should
222 	 * not be an indicator to use the legacy memory layout.
223 	 * if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
224 	 *	return 1;
225 	 */
226 
227 	return sysctl_legacy_va_layout;
228 }
229 
230 static unsigned long mmap_rnd(void)
231 {
232 	unsigned long rnd = 0;
233 
234 	if (current->flags & PF_RANDOMIZE)
235 		rnd = get_random_int() & MMAP_RND_MASK;
236 
237 	return rnd << PAGE_SHIFT;
238 }
239 
240 unsigned long arch_mmap_rnd(void)
241 {
242 	return (get_random_int() & MMAP_RND_MASK) << PAGE_SHIFT;
243 }
244 
245 static unsigned long mmap_legacy_base(void)
246 {
247 	return TASK_UNMAPPED_BASE + mmap_rnd();
248 }
249 
250 /*
251  * This function, called very early during the creation of a new
252  * process VM image, sets up which VM layout function to use:
253  */
254 void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
255 {
256 	mm->mmap_legacy_base = mmap_legacy_base();
257 	mm->mmap_base = mmap_upper_limit(rlim_stack);
258 
259 	if (mmap_is_legacy()) {
260 		mm->mmap_base = mm->mmap_legacy_base;
261 		mm->get_unmapped_area = arch_get_unmapped_area;
262 	} else {
263 		mm->get_unmapped_area = arch_get_unmapped_area_topdown;
264 	}
265 }
266 
267 
268 asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
269 	unsigned long prot, unsigned long flags, unsigned long fd,
270 	unsigned long pgoff)
271 {
272 	/* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
273 	   we have. */
274 	return ksys_mmap_pgoff(addr, len, prot, flags, fd,
275 			       pgoff >> (PAGE_SHIFT - 12));
276 }
277 
278 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
279 		unsigned long prot, unsigned long flags, unsigned long fd,
280 		unsigned long offset)
281 {
282 	if (!(offset & ~PAGE_MASK)) {
283 		return ksys_mmap_pgoff(addr, len, prot, flags, fd,
284 					offset >> PAGE_SHIFT);
285 	} else {
286 		return -EINVAL;
287 	}
288 }
289 
290 /* Fucking broken ABI */
291 
292 #ifdef CONFIG_64BIT
293 asmlinkage long parisc_truncate64(const char __user * path,
294 					unsigned int high, unsigned int low)
295 {
296 	return ksys_truncate(path, (long)high << 32 | low);
297 }
298 
299 asmlinkage long parisc_ftruncate64(unsigned int fd,
300 					unsigned int high, unsigned int low)
301 {
302 	return ksys_ftruncate(fd, (long)high << 32 | low);
303 }
304 
305 /* stubs for the benefit of the syscall_table since truncate64 and truncate
306  * are identical on LP64 */
307 asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
308 {
309 	return ksys_truncate(path, length);
310 }
311 asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
312 {
313 	return ksys_ftruncate(fd, length);
314 }
315 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
316 {
317 	return sys_fcntl(fd, cmd, arg);
318 }
319 #else
320 
321 asmlinkage long parisc_truncate64(const char __user * path,
322 					unsigned int high, unsigned int low)
323 {
324 	return ksys_truncate(path, (loff_t)high << 32 | low);
325 }
326 
327 asmlinkage long parisc_ftruncate64(unsigned int fd,
328 					unsigned int high, unsigned int low)
329 {
330 	return sys_ftruncate64(fd, (loff_t)high << 32 | low);
331 }
332 #endif
333 
334 asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
335 					unsigned int high, unsigned int low)
336 {
337 	return ksys_pread64(fd, buf, count, (loff_t)high << 32 | low);
338 }
339 
340 asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
341 			size_t count, unsigned int high, unsigned int low)
342 {
343 	return ksys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
344 }
345 
346 asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
347 		                    size_t count)
348 {
349 	return ksys_readahead(fd, (loff_t)high << 32 | low, count);
350 }
351 
352 asmlinkage long parisc_fadvise64_64(int fd,
353 			unsigned int high_off, unsigned int low_off,
354 			unsigned int high_len, unsigned int low_len, int advice)
355 {
356 	return ksys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
357 			(loff_t)high_len << 32 | low_len, advice);
358 }
359 
360 asmlinkage long parisc_sync_file_range(int fd,
361 			u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
362 			unsigned int flags)
363 {
364 	return ksys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
365 			(loff_t)hi_nbytes << 32 | lo_nbytes, flags);
366 }
367 
368 asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
369 				u32 lenhi, u32 lenlo)
370 {
371 	return ksys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
372 			      ((u64)lenhi << 32) | lenlo);
373 }
374 
375 long parisc_personality(unsigned long personality)
376 {
377 	long err;
378 
379 	if (personality(current->personality) == PER_LINUX32
380 	    && personality(personality) == PER_LINUX)
381 		personality = (personality & ~PER_MASK) | PER_LINUX32;
382 
383 	err = sys_personality(personality);
384 	if (personality(err) == PER_LINUX32)
385 		err = (err & ~PER_MASK) | PER_LINUX;
386 
387 	return err;
388 }
389