3 * PARISC specific syscalls
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>
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.
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.
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
26 #include <asm/uaccess.h>
28 #include <linux/file.h>
30 #include <linux/linkage.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>
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) \
46 static int get_offset(unsigned int last_mmap
)
48 return (last_mmap
& (SHM_COLOUR
-1)) >> PAGE_SHIFT
;
51 static unsigned long shared_align_offset(unsigned int last_mmap
,
54 return (get_offset(last_mmap
) + pgoff
) << PAGE_SHIFT
;
57 static inline unsigned long COLOR_ALIGN(unsigned long addr
,
58 unsigned int last_mmap
, unsigned long pgoff
)
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
);
68 * Top of mmap area (just below the process stack).
71 static unsigned long mmap_upper_limit(void)
73 unsigned long stack_base
;
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
;
80 return PAGE_ALIGN(STACK_TOP
- stack_base
);
84 unsigned long arch_get_unmapped_area(struct file
*filp
, unsigned long addr
,
85 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
87 struct mm_struct
*mm
= current
->mm
;
88 struct vm_area_struct
*vma
;
89 unsigned long task_size
= TASK_SIZE
;
90 int do_color_align
, last_mmap
;
91 struct vm_unmapped_area_info info
;
97 if (filp
|| (flags
& MAP_SHARED
))
99 last_mmap
= GET_LAST_MMAP(filp
);
101 if (flags
& MAP_FIXED
) {
102 if ((flags
& MAP_SHARED
) && last_mmap
&&
103 (addr
- shared_align_offset(last_mmap
, pgoff
))
110 if (do_color_align
&& last_mmap
)
111 addr
= COLOR_ALIGN(addr
, last_mmap
, pgoff
);
113 addr
= PAGE_ALIGN(addr
);
115 vma
= find_vma(mm
, addr
);
116 if (task_size
- len
>= addr
&&
117 (!vma
|| addr
+ len
<= vma
->vm_start
))
123 info
.low_limit
= mm
->mmap_legacy_base
;
124 info
.high_limit
= mmap_upper_limit();
125 info
.align_mask
= last_mmap
? (PAGE_MASK
& (SHM_COLOUR
- 1)) : 0;
126 info
.align_offset
= shared_align_offset(last_mmap
, pgoff
);
127 addr
= vm_unmapped_area(&info
);
130 if (do_color_align
&& !last_mmap
&& !(addr
& ~PAGE_MASK
))
131 SET_LAST_MMAP(filp
, addr
- (pgoff
<< PAGE_SHIFT
));
137 arch_get_unmapped_area_topdown(struct file
*filp
, const unsigned long addr0
,
138 const unsigned long len
, const unsigned long pgoff
,
139 const unsigned long flags
)
141 struct vm_area_struct
*vma
;
142 struct mm_struct
*mm
= current
->mm
;
143 unsigned long addr
= addr0
;
144 int do_color_align
, last_mmap
;
145 struct vm_unmapped_area_info info
;
148 /* This should only ever run for 32-bit processes. */
149 BUG_ON(!test_thread_flag(TIF_32BIT
));
152 /* requested length too big for entire address space */
157 if (filp
|| (flags
& MAP_SHARED
))
159 last_mmap
= GET_LAST_MMAP(filp
);
161 if (flags
& MAP_FIXED
) {
162 if ((flags
& MAP_SHARED
) && last_mmap
&&
163 (addr
- shared_align_offset(last_mmap
, pgoff
))
169 /* requesting a specific address */
171 if (do_color_align
&& last_mmap
)
172 addr
= COLOR_ALIGN(addr
, last_mmap
, pgoff
);
174 addr
= PAGE_ALIGN(addr
);
175 vma
= find_vma(mm
, addr
);
176 if (TASK_SIZE
- len
>= addr
&&
177 (!vma
|| addr
+ len
<= vma
->vm_start
))
181 info
.flags
= VM_UNMAPPED_AREA_TOPDOWN
;
183 info
.low_limit
= PAGE_SIZE
;
184 info
.high_limit
= mm
->mmap_base
;
185 info
.align_mask
= last_mmap
? (PAGE_MASK
& (SHM_COLOUR
- 1)) : 0;
186 info
.align_offset
= shared_align_offset(last_mmap
, pgoff
);
187 addr
= vm_unmapped_area(&info
);
188 if (!(addr
& ~PAGE_MASK
))
190 VM_BUG_ON(addr
!= -ENOMEM
);
193 * A failed mmap() very likely causes application failure,
194 * so fall back to the bottom-up function here. This scenario
195 * can happen with large stack limits and large mmap()
198 return arch_get_unmapped_area(filp
, addr0
, len
, pgoff
, flags
);
201 if (do_color_align
&& !last_mmap
&& !(addr
& ~PAGE_MASK
))
202 SET_LAST_MMAP(filp
, addr
- (pgoff
<< PAGE_SHIFT
));
207 static int mmap_is_legacy(void)
209 if (current
->personality
& ADDR_COMPAT_LAYOUT
)
212 /* parisc stack always grows up - so a unlimited stack should
213 * not be an indicator to use the legacy memory layout.
214 * if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
218 return sysctl_legacy_va_layout
;
221 static unsigned long mmap_rnd(void)
223 unsigned long rnd
= 0;
226 * 8 bits of randomness in 32bit mmaps, 20 address space bits
227 * 28 bits of randomness in 64bit mmaps, 40 address space bits
229 if (current
->flags
& PF_RANDOMIZE
) {
231 rnd
= get_random_int() % (1<<8);
233 rnd
= get_random_int() % (1<<28);
235 return rnd
<< PAGE_SHIFT
;
238 static unsigned long mmap_legacy_base(void)
240 return TASK_UNMAPPED_BASE
+ mmap_rnd();
244 * This function, called very early during the creation of a new
245 * process VM image, sets up which VM layout function to use:
247 void arch_pick_mmap_layout(struct mm_struct
*mm
)
249 mm
->mmap_legacy_base
= mmap_legacy_base();
250 mm
->mmap_base
= mmap_upper_limit();
252 if (mmap_is_legacy()) {
253 mm
->mmap_base
= mm
->mmap_legacy_base
;
254 mm
->get_unmapped_area
= arch_get_unmapped_area
;
256 mm
->get_unmapped_area
= arch_get_unmapped_area_topdown
;
261 asmlinkage
unsigned long sys_mmap2(unsigned long addr
, unsigned long len
,
262 unsigned long prot
, unsigned long flags
, unsigned long fd
,
265 /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
267 return sys_mmap_pgoff(addr
, len
, prot
, flags
, fd
,
268 pgoff
>> (PAGE_SHIFT
- 12));
271 asmlinkage
unsigned long sys_mmap(unsigned long addr
, unsigned long len
,
272 unsigned long prot
, unsigned long flags
, unsigned long fd
,
273 unsigned long offset
)
275 if (!(offset
& ~PAGE_MASK
)) {
276 return sys_mmap_pgoff(addr
, len
, prot
, flags
, fd
,
277 offset
>> PAGE_SHIFT
);
283 /* Fucking broken ABI */
286 asmlinkage
long parisc_truncate64(const char __user
* path
,
287 unsigned int high
, unsigned int low
)
289 return sys_truncate(path
, (long)high
<< 32 | low
);
292 asmlinkage
long parisc_ftruncate64(unsigned int fd
,
293 unsigned int high
, unsigned int low
)
295 return sys_ftruncate(fd
, (long)high
<< 32 | low
);
298 /* stubs for the benefit of the syscall_table since truncate64 and truncate
299 * are identical on LP64 */
300 asmlinkage
long sys_truncate64(const char __user
* path
, unsigned long length
)
302 return sys_truncate(path
, length
);
304 asmlinkage
long sys_ftruncate64(unsigned int fd
, unsigned long length
)
306 return sys_ftruncate(fd
, length
);
308 asmlinkage
long sys_fcntl64(unsigned int fd
, unsigned int cmd
, unsigned long arg
)
310 return sys_fcntl(fd
, cmd
, arg
);
314 asmlinkage
long parisc_truncate64(const char __user
* path
,
315 unsigned int high
, unsigned int low
)
317 return sys_truncate64(path
, (loff_t
)high
<< 32 | low
);
320 asmlinkage
long parisc_ftruncate64(unsigned int fd
,
321 unsigned int high
, unsigned int low
)
323 return sys_ftruncate64(fd
, (loff_t
)high
<< 32 | low
);
327 asmlinkage ssize_t
parisc_pread64(unsigned int fd
, char __user
*buf
, size_t count
,
328 unsigned int high
, unsigned int low
)
330 return sys_pread64(fd
, buf
, count
, (loff_t
)high
<< 32 | low
);
333 asmlinkage ssize_t
parisc_pwrite64(unsigned int fd
, const char __user
*buf
,
334 size_t count
, unsigned int high
, unsigned int low
)
336 return sys_pwrite64(fd
, buf
, count
, (loff_t
)high
<< 32 | low
);
339 asmlinkage ssize_t
parisc_readahead(int fd
, unsigned int high
, unsigned int low
,
342 return sys_readahead(fd
, (loff_t
)high
<< 32 | low
, count
);
345 asmlinkage
long parisc_fadvise64_64(int fd
,
346 unsigned int high_off
, unsigned int low_off
,
347 unsigned int high_len
, unsigned int low_len
, int advice
)
349 return sys_fadvise64_64(fd
, (loff_t
)high_off
<< 32 | low_off
,
350 (loff_t
)high_len
<< 32 | low_len
, advice
);
353 asmlinkage
long parisc_sync_file_range(int fd
,
354 u32 hi_off
, u32 lo_off
, u32 hi_nbytes
, u32 lo_nbytes
,
357 return sys_sync_file_range(fd
, (loff_t
)hi_off
<< 32 | lo_off
,
358 (loff_t
)hi_nbytes
<< 32 | lo_nbytes
, flags
);
361 asmlinkage
long parisc_fallocate(int fd
, int mode
, u32 offhi
, u32 offlo
,
362 u32 lenhi
, u32 lenlo
)
364 return sys_fallocate(fd
, mode
, ((u64
)offhi
<< 32) | offlo
,
365 ((u64
)lenhi
<< 32) | lenlo
);
368 asmlinkage
unsigned long sys_alloc_hugepages(int key
, unsigned long addr
, unsigned long len
, int prot
, int flag
)
373 asmlinkage
int sys_free_hugepages(unsigned long addr
)
378 long parisc_personality(unsigned long personality
)
382 if (personality(current
->personality
) == PER_LINUX32
383 && personality(personality
) == PER_LINUX
)
384 personality
= (personality
& ~PER_MASK
) | PER_LINUX32
;
386 err
= sys_personality(personality
);
387 if (personality(err
) == PER_LINUX32
)
388 err
= (err
& ~PER_MASK
) | PER_LINUX
;