cpufreq: intel_pstate: Fix policy data management in passive mode
[linux-2.6/btrfs-unstable.git] / arch / parisc / kernel / sys_parisc.c
blobe5288638a1d9ad713c03fa4fc0d439efd012c470
2 /*
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 <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>
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 */ }
48 static int get_offset(unsigned int last_mmap)
50 return (last_mmap & (SHM_COLOUR-1)) >> PAGE_SHIFT;
53 static unsigned long shared_align_offset(unsigned int last_mmap,
54 unsigned long pgoff)
56 return (get_offset(last_mmap) + pgoff) << PAGE_SHIFT;
59 static inline unsigned long COLOR_ALIGN(unsigned long addr,
60 unsigned int last_mmap, unsigned long pgoff)
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);
66 return base + off;
70 * Top of mmap area (just below the process stack).
73 static unsigned long mmap_upper_limit(void)
75 unsigned long stack_base;
77 /* Limit stack size - see setup_arg_pages() in fs/exec.c */
78 stack_base = rlimit_max(RLIMIT_STACK);
79 if (stack_base > STACK_SIZE_MAX)
80 stack_base = STACK_SIZE_MAX;
82 /* Add space for stack randomization. */
83 stack_base += (STACK_RND_MASK << PAGE_SHIFT);
85 return PAGE_ALIGN(STACK_TOP - stack_base);
89 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
90 unsigned long len, unsigned long pgoff, unsigned long flags)
92 struct mm_struct *mm = current->mm;
93 struct vm_area_struct *vma;
94 unsigned long task_size = TASK_SIZE;
95 int do_color_align, last_mmap;
96 struct vm_unmapped_area_info info;
98 if (len > task_size)
99 return -ENOMEM;
101 do_color_align = 0;
102 if (filp || (flags & MAP_SHARED))
103 do_color_align = 1;
104 last_mmap = GET_LAST_MMAP(filp);
106 if (flags & MAP_FIXED) {
107 if ((flags & MAP_SHARED) && last_mmap &&
108 (addr - shared_align_offset(last_mmap, pgoff))
109 & (SHM_COLOUR - 1))
110 return -EINVAL;
111 goto found_addr;
114 if (addr) {
115 if (do_color_align && last_mmap)
116 addr = COLOR_ALIGN(addr, last_mmap, pgoff);
117 else
118 addr = PAGE_ALIGN(addr);
120 vma = find_vma(mm, addr);
121 if (task_size - len >= addr &&
122 (!vma || addr + len <= vma->vm_start))
123 goto found_addr;
126 info.flags = 0;
127 info.length = len;
128 info.low_limit = mm->mmap_legacy_base;
129 info.high_limit = mmap_upper_limit();
130 info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
131 info.align_offset = shared_align_offset(last_mmap, pgoff);
132 addr = vm_unmapped_area(&info);
134 found_addr:
135 if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
136 SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
138 return addr;
141 unsigned long
142 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
143 const unsigned long len, const unsigned long pgoff,
144 const unsigned long flags)
146 struct vm_area_struct *vma;
147 struct mm_struct *mm = current->mm;
148 unsigned long addr = addr0;
149 int do_color_align, last_mmap;
150 struct vm_unmapped_area_info info;
152 #ifdef CONFIG_64BIT
153 /* This should only ever run for 32-bit processes. */
154 BUG_ON(!test_thread_flag(TIF_32BIT));
155 #endif
157 /* requested length too big for entire address space */
158 if (len > TASK_SIZE)
159 return -ENOMEM;
161 do_color_align = 0;
162 if (filp || (flags & MAP_SHARED))
163 do_color_align = 1;
164 last_mmap = GET_LAST_MMAP(filp);
166 if (flags & MAP_FIXED) {
167 if ((flags & MAP_SHARED) && last_mmap &&
168 (addr - shared_align_offset(last_mmap, pgoff))
169 & (SHM_COLOUR - 1))
170 return -EINVAL;
171 goto found_addr;
174 /* requesting a specific address */
175 if (addr) {
176 if (do_color_align && last_mmap)
177 addr = COLOR_ALIGN(addr, last_mmap, pgoff);
178 else
179 addr = PAGE_ALIGN(addr);
180 vma = find_vma(mm, addr);
181 if (TASK_SIZE - len >= addr &&
182 (!vma || addr + len <= vma->vm_start))
183 goto found_addr;
186 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
187 info.length = len;
188 info.low_limit = PAGE_SIZE;
189 info.high_limit = mm->mmap_base;
190 info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
191 info.align_offset = shared_align_offset(last_mmap, pgoff);
192 addr = vm_unmapped_area(&info);
193 if (!(addr & ~PAGE_MASK))
194 goto found_addr;
195 VM_BUG_ON(addr != -ENOMEM);
198 * A failed mmap() very likely causes application failure,
199 * so fall back to the bottom-up function here. This scenario
200 * can happen with large stack limits and large mmap()
201 * allocations.
203 return arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
205 found_addr:
206 if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
207 SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
209 return addr;
212 static int mmap_is_legacy(void)
214 if (current->personality & ADDR_COMPAT_LAYOUT)
215 return 1;
217 /* parisc stack always grows up - so a unlimited stack should
218 * not be an indicator to use the legacy memory layout.
219 * if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
220 * return 1;
223 return sysctl_legacy_va_layout;
226 static unsigned long mmap_rnd(void)
228 unsigned long rnd = 0;
230 if (current->flags & PF_RANDOMIZE)
231 rnd = get_random_int() & MMAP_RND_MASK;
233 return rnd << PAGE_SHIFT;
236 unsigned long arch_mmap_rnd(void)
238 return (get_random_int() & MMAP_RND_MASK) << PAGE_SHIFT;
241 static unsigned long mmap_legacy_base(void)
243 return TASK_UNMAPPED_BASE + mmap_rnd();
247 * This function, called very early during the creation of a new
248 * process VM image, sets up which VM layout function to use:
250 void arch_pick_mmap_layout(struct mm_struct *mm)
252 mm->mmap_legacy_base = mmap_legacy_base();
253 mm->mmap_base = mmap_upper_limit();
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;
264 asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
265 unsigned long prot, unsigned long flags, unsigned long fd,
266 unsigned long pgoff)
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));
274 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
275 unsigned long prot, unsigned long flags, unsigned long fd,
276 unsigned long offset)
278 if (!(offset & ~PAGE_MASK)) {
279 return sys_mmap_pgoff(addr, len, prot, flags, fd,
280 offset >> PAGE_SHIFT);
281 } else {
282 return -EINVAL;
286 /* Fucking broken ABI */
288 #ifdef CONFIG_64BIT
289 asmlinkage long parisc_truncate64(const char __user * path,
290 unsigned int high, unsigned int low)
292 return sys_truncate(path, (long)high << 32 | low);
295 asmlinkage long parisc_ftruncate64(unsigned int fd,
296 unsigned int high, unsigned int low)
298 return sys_ftruncate(fd, (long)high << 32 | low);
301 /* stubs for the benefit of the syscall_table since truncate64 and truncate
302 * are identical on LP64 */
303 asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
305 return sys_truncate(path, length);
307 asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
309 return sys_ftruncate(fd, length);
311 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
313 return sys_fcntl(fd, cmd, arg);
315 #else
317 asmlinkage long parisc_truncate64(const char __user * path,
318 unsigned int high, unsigned int low)
320 return sys_truncate64(path, (loff_t)high << 32 | low);
323 asmlinkage long parisc_ftruncate64(unsigned int fd,
324 unsigned int high, unsigned int low)
326 return sys_ftruncate64(fd, (loff_t)high << 32 | low);
328 #endif
330 asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
331 unsigned int high, unsigned int low)
333 return sys_pread64(fd, buf, count, (loff_t)high << 32 | low);
336 asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
337 size_t count, unsigned int high, unsigned int low)
339 return sys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
342 asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
343 size_t count)
345 return sys_readahead(fd, (loff_t)high << 32 | low, count);
348 asmlinkage 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)
352 return sys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
353 (loff_t)high_len << 32 | low_len, advice);
356 asmlinkage long parisc_sync_file_range(int fd,
357 u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
358 unsigned int flags)
360 return sys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
361 (loff_t)hi_nbytes << 32 | lo_nbytes, flags);
364 asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
365 u32 lenhi, u32 lenlo)
367 return sys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
368 ((u64)lenhi << 32) | lenlo);
371 long parisc_personality(unsigned long personality)
373 long err;
375 if (personality(current->personality) == PER_LINUX32
376 && personality(personality) == PER_LINUX)
377 personality = (personality & ~PER_MASK) | PER_LINUX32;
379 err = sys_personality(personality);
380 if (personality(err) == PER_LINUX32)
381 err = (err & ~PER_MASK) | PER_LINUX;
383 return err;