Loading...
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 *
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include <asm/uaccess.h>
26#include <linux/file.h>
27#include <linux/fs.h>
28#include <linux/linkage.h>
29#include <linux/mm.h>
30#include <linux/mman.h>
31#include <linux/shm.h>
32#include <linux/syscalls.h>
33#include <linux/utsname.h>
34#include <linux/personality.h>
35
36static unsigned long get_unshared_area(unsigned long addr, unsigned long len)
37{
38 struct vm_area_struct *vma;
39
40 addr = PAGE_ALIGN(addr);
41
42 for (vma = find_vma(current->mm, addr); ; vma = vma->vm_next) {
43 /* At this point: (!vma || addr < vma->vm_end). */
44 if (TASK_SIZE - len < addr)
45 return -ENOMEM;
46 if (!vma || addr + len <= vma->vm_start)
47 return addr;
48 addr = vma->vm_end;
49 }
50}
51
52#define DCACHE_ALIGN(addr) (((addr) + (SHMLBA - 1)) &~ (SHMLBA - 1))
53
54/*
55 * We need to know the offset to use. Old scheme was to look for
56 * existing mapping and use the same offset. New scheme is to use the
57 * address of the kernel data structure as the seed for the offset.
58 * We'll see how that works...
59 *
60 * The mapping is cacheline aligned, so there's no information in the bottom
61 * few bits of the address. We're looking for 10 bits (4MB / 4k), so let's
62 * drop the bottom 8 bits and use bits 8-17.
63 */
64static int get_offset(struct address_space *mapping)
65{
66 int offset = (unsigned long) mapping << (PAGE_SHIFT - 8);
67 return offset & 0x3FF000;
68}
69
70static unsigned long get_shared_area(struct address_space *mapping,
71 unsigned long addr, unsigned long len, unsigned long pgoff)
72{
73 struct vm_area_struct *vma;
74 int offset = mapping ? get_offset(mapping) : 0;
75
76 addr = DCACHE_ALIGN(addr - offset) + offset;
77
78 for (vma = find_vma(current->mm, addr); ; vma = vma->vm_next) {
79 /* At this point: (!vma || addr < vma->vm_end). */
80 if (TASK_SIZE - len < addr)
81 return -ENOMEM;
82 if (!vma || addr + len <= vma->vm_start)
83 return addr;
84 addr = DCACHE_ALIGN(vma->vm_end - offset) + offset;
85 if (addr < vma->vm_end) /* handle wraparound */
86 return -ENOMEM;
87 }
88}
89
90unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
91 unsigned long len, unsigned long pgoff, unsigned long flags)
92{
93 if (len > TASK_SIZE)
94 return -ENOMEM;
95 /* Might want to check for cache aliasing issues for MAP_FIXED case
96 * like ARM or MIPS ??? --BenH.
97 */
98 if (flags & MAP_FIXED)
99 return addr;
100 if (!addr)
101 addr = TASK_UNMAPPED_BASE;
102
103 if (filp) {
104 addr = get_shared_area(filp->f_mapping, addr, len, pgoff);
105 } else if(flags & MAP_SHARED) {
106 addr = get_shared_area(NULL, addr, len, pgoff);
107 } else {
108 addr = get_unshared_area(addr, len);
109 }
110 return addr;
111}
112
113asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
114 unsigned long prot, unsigned long flags, unsigned long fd,
115 unsigned long pgoff)
116{
117 /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
118 we have. */
119 return sys_mmap_pgoff(addr, len, prot, flags, fd,
120 pgoff >> (PAGE_SHIFT - 12));
121}
122
123asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
124 unsigned long prot, unsigned long flags, unsigned long fd,
125 unsigned long offset)
126{
127 if (!(offset & ~PAGE_MASK)) {
128 return sys_mmap_pgoff(addr, len, prot, flags, fd,
129 offset >> PAGE_SHIFT);
130 } else {
131 return -EINVAL;
132 }
133}
134
135/* Fucking broken ABI */
136
137#ifdef CONFIG_64BIT
138asmlinkage long parisc_truncate64(const char __user * path,
139 unsigned int high, unsigned int low)
140{
141 return sys_truncate(path, (long)high << 32 | low);
142}
143
144asmlinkage long parisc_ftruncate64(unsigned int fd,
145 unsigned int high, unsigned int low)
146{
147 return sys_ftruncate(fd, (long)high << 32 | low);
148}
149
150/* stubs for the benefit of the syscall_table since truncate64 and truncate
151 * are identical on LP64 */
152asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
153{
154 return sys_truncate(path, length);
155}
156asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
157{
158 return sys_ftruncate(fd, length);
159}
160asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
161{
162 return sys_fcntl(fd, cmd, arg);
163}
164#else
165
166asmlinkage long parisc_truncate64(const char __user * path,
167 unsigned int high, unsigned int low)
168{
169 return sys_truncate64(path, (loff_t)high << 32 | low);
170}
171
172asmlinkage long parisc_ftruncate64(unsigned int fd,
173 unsigned int high, unsigned int low)
174{
175 return sys_ftruncate64(fd, (loff_t)high << 32 | low);
176}
177#endif
178
179asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
180 unsigned int high, unsigned int low)
181{
182 return sys_pread64(fd, buf, count, (loff_t)high << 32 | low);
183}
184
185asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
186 size_t count, unsigned int high, unsigned int low)
187{
188 return sys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
189}
190
191asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
192 size_t count)
193{
194 return sys_readahead(fd, (loff_t)high << 32 | low, count);
195}
196
197asmlinkage long parisc_fadvise64_64(int fd,
198 unsigned int high_off, unsigned int low_off,
199 unsigned int high_len, unsigned int low_len, int advice)
200{
201 return sys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
202 (loff_t)high_len << 32 | low_len, advice);
203}
204
205asmlinkage long parisc_sync_file_range(int fd,
206 u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
207 unsigned int flags)
208{
209 return sys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
210 (loff_t)hi_nbytes << 32 | lo_nbytes, flags);
211}
212
213asmlinkage unsigned long sys_alloc_hugepages(int key, unsigned long addr, unsigned long len, int prot, int flag)
214{
215 return -ENOMEM;
216}
217
218asmlinkage int sys_free_hugepages(unsigned long addr)
219{
220 return -EINVAL;
221}
222
223long parisc_personality(unsigned long personality)
224{
225 long err;
226
227 if (personality(current->personality) == PER_LINUX32
228 && personality == PER_LINUX)
229 personality = PER_LINUX32;
230
231 err = sys_personality(personality);
232 if (err == PER_LINUX32)
233 err = PER_LINUX;
234
235 return err;
236}
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
48static int get_offset(unsigned int last_mmap)
49{
50 return (last_mmap & (SHM_COLOUR-1)) >> PAGE_SHIFT;
51}
52
53static 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
59static 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 */
78static 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
95unsigned 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
141found_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
148unsigned long
149arch_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#ifdef CONFIG_64BIT
160 /* This should only ever run for 32-bit processes. */
161 BUG_ON(!test_thread_flag(TIF_32BIT));
162#endif
163
164 /* requested length too big for entire address space */
165 if (len > TASK_SIZE)
166 return -ENOMEM;
167
168 do_color_align = 0;
169 if (filp || (flags & MAP_SHARED))
170 do_color_align = 1;
171 last_mmap = GET_LAST_MMAP(filp);
172
173 if (flags & MAP_FIXED) {
174 if ((flags & MAP_SHARED) && last_mmap &&
175 (addr - shared_align_offset(last_mmap, pgoff))
176 & (SHM_COLOUR - 1))
177 return -EINVAL;
178 goto found_addr;
179 }
180
181 /* requesting a specific address */
182 if (addr) {
183 if (do_color_align && last_mmap)
184 addr = COLOR_ALIGN(addr, last_mmap, pgoff);
185 else
186 addr = PAGE_ALIGN(addr);
187
188 vma = find_vma_prev(mm, addr, &prev);
189 if (TASK_SIZE - len >= addr &&
190 (!vma || addr + len <= vm_start_gap(vma)) &&
191 (!prev || addr >= vm_end_gap(prev)))
192 goto found_addr;
193 }
194
195 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
196 info.length = len;
197 info.low_limit = PAGE_SIZE;
198 info.high_limit = mm->mmap_base;
199 info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
200 info.align_offset = shared_align_offset(last_mmap, pgoff);
201 addr = vm_unmapped_area(&info);
202 if (!(addr & ~PAGE_MASK))
203 goto found_addr;
204 VM_BUG_ON(addr != -ENOMEM);
205
206 /*
207 * A failed mmap() very likely causes application failure,
208 * so fall back to the bottom-up function here. This scenario
209 * can happen with large stack limits and large mmap()
210 * allocations.
211 */
212 return arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
213
214found_addr:
215 if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
216 SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
217
218 return addr;
219}
220
221static int mmap_is_legacy(void)
222{
223 if (current->personality & ADDR_COMPAT_LAYOUT)
224 return 1;
225
226 /* parisc stack always grows up - so a unlimited stack should
227 * not be an indicator to use the legacy memory layout.
228 * if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
229 * return 1;
230 */
231
232 return sysctl_legacy_va_layout;
233}
234
235static unsigned long mmap_rnd(void)
236{
237 unsigned long rnd = 0;
238
239 if (current->flags & PF_RANDOMIZE)
240 rnd = get_random_int() & MMAP_RND_MASK;
241
242 return rnd << PAGE_SHIFT;
243}
244
245unsigned long arch_mmap_rnd(void)
246{
247 return (get_random_int() & MMAP_RND_MASK) << PAGE_SHIFT;
248}
249
250static unsigned long mmap_legacy_base(void)
251{
252 return TASK_UNMAPPED_BASE + mmap_rnd();
253}
254
255/*
256 * This function, called very early during the creation of a new
257 * process VM image, sets up which VM layout function to use:
258 */
259void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
260{
261 mm->mmap_legacy_base = mmap_legacy_base();
262 mm->mmap_base = mmap_upper_limit(rlim_stack);
263
264 if (mmap_is_legacy()) {
265 mm->mmap_base = mm->mmap_legacy_base;
266 mm->get_unmapped_area = arch_get_unmapped_area;
267 } else {
268 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
269 }
270}
271
272
273asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
274 unsigned long prot, unsigned long flags, unsigned long fd,
275 unsigned long pgoff)
276{
277 /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
278 we have. */
279 return ksys_mmap_pgoff(addr, len, prot, flags, fd,
280 pgoff >> (PAGE_SHIFT - 12));
281}
282
283asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
284 unsigned long prot, unsigned long flags, unsigned long fd,
285 unsigned long offset)
286{
287 if (!(offset & ~PAGE_MASK)) {
288 return ksys_mmap_pgoff(addr, len, prot, flags, fd,
289 offset >> PAGE_SHIFT);
290 } else {
291 return -EINVAL;
292 }
293}
294
295/* Fucking broken ABI */
296
297#ifdef CONFIG_64BIT
298asmlinkage long parisc_truncate64(const char __user * path,
299 unsigned int high, unsigned int low)
300{
301 return ksys_truncate(path, (long)high << 32 | low);
302}
303
304asmlinkage long parisc_ftruncate64(unsigned int fd,
305 unsigned int high, unsigned int low)
306{
307 return ksys_ftruncate(fd, (long)high << 32 | low);
308}
309
310/* stubs for the benefit of the syscall_table since truncate64 and truncate
311 * are identical on LP64 */
312asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
313{
314 return ksys_truncate(path, length);
315}
316asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
317{
318 return ksys_ftruncate(fd, length);
319}
320asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
321{
322 return sys_fcntl(fd, cmd, arg);
323}
324#else
325
326asmlinkage long parisc_truncate64(const char __user * path,
327 unsigned int high, unsigned int low)
328{
329 return ksys_truncate(path, (loff_t)high << 32 | low);
330}
331
332asmlinkage long parisc_ftruncate64(unsigned int fd,
333 unsigned int high, unsigned int low)
334{
335 return sys_ftruncate64(fd, (loff_t)high << 32 | low);
336}
337#endif
338
339asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
340 unsigned int high, unsigned int low)
341{
342 return ksys_pread64(fd, buf, count, (loff_t)high << 32 | low);
343}
344
345asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
346 size_t count, unsigned int high, unsigned int low)
347{
348 return ksys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
349}
350
351asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
352 size_t count)
353{
354 return ksys_readahead(fd, (loff_t)high << 32 | low, count);
355}
356
357asmlinkage long parisc_fadvise64_64(int fd,
358 unsigned int high_off, unsigned int low_off,
359 unsigned int high_len, unsigned int low_len, int advice)
360{
361 return ksys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
362 (loff_t)high_len << 32 | low_len, advice);
363}
364
365asmlinkage long parisc_sync_file_range(int fd,
366 u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
367 unsigned int flags)
368{
369 return ksys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
370 (loff_t)hi_nbytes << 32 | lo_nbytes, flags);
371}
372
373asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
374 u32 lenhi, u32 lenlo)
375{
376 return ksys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
377 ((u64)lenhi << 32) | lenlo);
378}
379
380long parisc_personality(unsigned long personality)
381{
382 long err;
383
384 if (personality(current->personality) == PER_LINUX32
385 && personality(personality) == PER_LINUX)
386 personality = (personality & ~PER_MASK) | PER_LINUX32;
387
388 err = sys_personality(personality);
389 if (personality(err) == PER_LINUX32)
390 err = (err & ~PER_MASK) | PER_LINUX;
391
392 return err;
393}