1 /* 2 * This file contains various system calls that have different calling 3 * conventions on different platforms. 4 * 5 * Copyright (C) 1999-2000, 2002-2003, 2005 Hewlett-Packard Co 6 * David Mosberger-Tang <davidm@hpl.hp.com> 7 */ 8 #include <linux/config.h> 9 #include <linux/errno.h> 10 #include <linux/fs.h> 11 #include <linux/mm.h> 12 #include <linux/mman.h> 13 #include <linux/sched.h> 14 #include <linux/shm.h> 15 #include <linux/file.h> /* doh, must come after sched.h... */ 16 #include <linux/smp.h> 17 #include <linux/smp_lock.h> 18 #include <linux/syscalls.h> 19 #include <linux/highuid.h> 20 #include <linux/hugetlb.h> 21 22 #include <asm/shmparam.h> 23 #include <asm/uaccess.h> 24 25 unsigned long 26 arch_get_unmapped_area (struct file *filp, unsigned long addr, unsigned long len, 27 unsigned long pgoff, unsigned long flags) 28 { 29 long map_shared = (flags & MAP_SHARED); 30 unsigned long start_addr, align_mask = PAGE_SIZE - 1; 31 struct mm_struct *mm = current->mm; 32 struct vm_area_struct *vma; 33 34 if (len > RGN_MAP_LIMIT) 35 return -ENOMEM; 36 37 #ifdef CONFIG_HUGETLB_PAGE 38 if (REGION_NUMBER(addr) == REGION_HPAGE) 39 addr = 0; 40 #endif 41 if (!addr) 42 addr = mm->free_area_cache; 43 44 if (map_shared && (TASK_SIZE > 0xfffffffful)) 45 /* 46 * For 64-bit tasks, align shared segments to 1MB to avoid potential 47 * performance penalty due to virtual aliasing (see ASDM). For 32-bit 48 * tasks, we prefer to avoid exhausting the address space too quickly by 49 * limiting alignment to a single page. 50 */ 51 align_mask = SHMLBA - 1; 52 53 full_search: 54 start_addr = addr = (addr + align_mask) & ~align_mask; 55 56 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) { 57 /* At this point: (!vma || addr < vma->vm_end). */ 58 if (TASK_SIZE - len < addr || RGN_MAP_LIMIT - len < REGION_OFFSET(addr)) { 59 if (start_addr != TASK_UNMAPPED_BASE) { 60 /* Start a new search --- just in case we missed some holes. */ 61 addr = TASK_UNMAPPED_BASE; 62 goto full_search; 63 } 64 return -ENOMEM; 65 } 66 if (!vma || addr + len <= vma->vm_start) { 67 /* Remember the address where we stopped this search: */ 68 mm->free_area_cache = addr + len; 69 return addr; 70 } 71 addr = (vma->vm_end + align_mask) & ~align_mask; 72 } 73 } 74 75 asmlinkage long 76 ia64_getpriority (int which, int who) 77 { 78 long prio; 79 80 prio = sys_getpriority(which, who); 81 if (prio >= 0) { 82 force_successful_syscall_return(); 83 prio = 20 - prio; 84 } 85 return prio; 86 } 87 88 /* XXX obsolete, but leave it here until the old libc is gone... */ 89 asmlinkage unsigned long 90 sys_getpagesize (void) 91 { 92 return PAGE_SIZE; 93 } 94 95 asmlinkage unsigned long 96 ia64_shmat (int shmid, void __user *shmaddr, int shmflg) 97 { 98 unsigned long raddr; 99 int retval; 100 101 retval = do_shmat(shmid, shmaddr, shmflg, &raddr); 102 if (retval < 0) 103 return retval; 104 105 force_successful_syscall_return(); 106 return raddr; 107 } 108 109 asmlinkage unsigned long 110 ia64_brk (unsigned long brk) 111 { 112 unsigned long rlim, retval, newbrk, oldbrk; 113 struct mm_struct *mm = current->mm; 114 115 /* 116 * Most of this replicates the code in sys_brk() except for an additional safety 117 * check and the clearing of r8. However, we can't call sys_brk() because we need 118 * to acquire the mmap_sem before we can do the test... 119 */ 120 down_write(&mm->mmap_sem); 121 122 if (brk < mm->end_code) 123 goto out; 124 newbrk = PAGE_ALIGN(brk); 125 oldbrk = PAGE_ALIGN(mm->brk); 126 if (oldbrk == newbrk) 127 goto set_brk; 128 129 /* Always allow shrinking brk. */ 130 if (brk <= mm->brk) { 131 if (!do_munmap(mm, newbrk, oldbrk-newbrk)) 132 goto set_brk; 133 goto out; 134 } 135 136 /* Check against unimplemented/unmapped addresses: */ 137 if ((newbrk - oldbrk) > RGN_MAP_LIMIT || REGION_OFFSET(newbrk) > RGN_MAP_LIMIT) 138 goto out; 139 140 /* Check against rlimit.. */ 141 rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur; 142 if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim) 143 goto out; 144 145 /* Check against existing mmap mappings. */ 146 if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE)) 147 goto out; 148 149 /* Ok, looks good - let it rip. */ 150 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk) 151 goto out; 152 set_brk: 153 mm->brk = brk; 154 out: 155 retval = mm->brk; 156 up_write(&mm->mmap_sem); 157 force_successful_syscall_return(); 158 return retval; 159 } 160 161 /* 162 * On IA-64, we return the two file descriptors in ret0 and ret1 (r8 163 * and r9) as this is faster than doing a copy_to_user(). 164 */ 165 asmlinkage long 166 sys_pipe (void) 167 { 168 struct pt_regs *regs = ia64_task_regs(current); 169 int fd[2]; 170 int retval; 171 172 retval = do_pipe(fd); 173 if (retval) 174 goto out; 175 retval = fd[0]; 176 regs->r9 = fd[1]; 177 out: 178 return retval; 179 } 180 181 static inline unsigned long 182 do_mmap2 (unsigned long addr, unsigned long len, int prot, int flags, int fd, unsigned long pgoff) 183 { 184 unsigned long roff; 185 struct file *file = NULL; 186 187 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE); 188 if (!(flags & MAP_ANONYMOUS)) { 189 file = fget(fd); 190 if (!file) 191 return -EBADF; 192 193 if (!file->f_op || !file->f_op->mmap) { 194 addr = -ENODEV; 195 goto out; 196 } 197 } 198 199 /* 200 * A zero mmap always succeeds in Linux, independent of whether or not the 201 * remaining arguments are valid. 202 */ 203 if (len == 0) 204 goto out; 205 206 /* Careful about overflows.. */ 207 len = PAGE_ALIGN(len); 208 if (!len || len > TASK_SIZE) { 209 addr = -EINVAL; 210 goto out; 211 } 212 213 /* 214 * Don't permit mappings into unmapped space, the virtual page table of a region, 215 * or across a region boundary. Note: RGN_MAP_LIMIT is equal to 2^n-PAGE_SIZE 216 * (for some integer n <= 61) and len > 0. 217 */ 218 roff = REGION_OFFSET(addr); 219 if ((len > RGN_MAP_LIMIT) || (roff > (RGN_MAP_LIMIT - len))) { 220 addr = -EINVAL; 221 goto out; 222 } 223 224 down_write(¤t->mm->mmap_sem); 225 addr = do_mmap_pgoff(file, addr, len, prot, flags, pgoff); 226 up_write(¤t->mm->mmap_sem); 227 228 out: if (file) 229 fput(file); 230 return addr; 231 } 232 233 /* 234 * mmap2() is like mmap() except that the offset is expressed in units 235 * of PAGE_SIZE (instead of bytes). This allows to mmap2() (pieces 236 * of) files that are larger than the address space of the CPU. 237 */ 238 asmlinkage unsigned long 239 sys_mmap2 (unsigned long addr, unsigned long len, int prot, int flags, int fd, long pgoff) 240 { 241 addr = do_mmap2(addr, len, prot, flags, fd, pgoff); 242 if (!IS_ERR((void *) addr)) 243 force_successful_syscall_return(); 244 return addr; 245 } 246 247 asmlinkage unsigned long 248 sys_mmap (unsigned long addr, unsigned long len, int prot, int flags, int fd, long off) 249 { 250 if (offset_in_page(off) != 0) 251 return -EINVAL; 252 253 addr = do_mmap2(addr, len, prot, flags, fd, off >> PAGE_SHIFT); 254 if (!IS_ERR((void *) addr)) 255 force_successful_syscall_return(); 256 return addr; 257 } 258 259 asmlinkage unsigned long 260 ia64_mremap (unsigned long addr, unsigned long old_len, unsigned long new_len, unsigned long flags, 261 unsigned long new_addr) 262 { 263 extern unsigned long do_mremap (unsigned long addr, 264 unsigned long old_len, 265 unsigned long new_len, 266 unsigned long flags, 267 unsigned long new_addr); 268 269 down_write(¤t->mm->mmap_sem); 270 { 271 addr = do_mremap(addr, old_len, new_len, flags, new_addr); 272 } 273 up_write(¤t->mm->mmap_sem); 274 275 if (IS_ERR((void *) addr)) 276 return addr; 277 278 force_successful_syscall_return(); 279 return addr; 280 } 281 282 #ifndef CONFIG_PCI 283 284 asmlinkage long 285 sys_pciconfig_read (unsigned long bus, unsigned long dfn, unsigned long off, unsigned long len, 286 void *buf) 287 { 288 return -ENOSYS; 289 } 290 291 asmlinkage long 292 sys_pciconfig_write (unsigned long bus, unsigned long dfn, unsigned long off, unsigned long len, 293 void *buf) 294 { 295 return -ENOSYS; 296 } 297 298 #endif /* CONFIG_PCI */ 299