[PATCH] Avoiding mmap fragmentation
[deliverable/linux.git] / arch / sparc64 / kernel / sys_sparc.c
1 /* $Id: sys_sparc.c,v 1.57 2002/02/09 19:49:30 davem Exp $
2 * linux/arch/sparc64/kernel/sys_sparc.c
3 *
4 * This file contains various random system calls that
5 * have a non-standard calling sequence on the Linux/sparc
6 * platform.
7 */
8
9 #include <linux/config.h>
10 #include <linux/errno.h>
11 #include <linux/types.h>
12 #include <linux/sched.h>
13 #include <linux/fs.h>
14 #include <linux/file.h>
15 #include <linux/mm.h>
16 #include <linux/sem.h>
17 #include <linux/msg.h>
18 #include <linux/shm.h>
19 #include <linux/stat.h>
20 #include <linux/mman.h>
21 #include <linux/utsname.h>
22 #include <linux/smp.h>
23 #include <linux/smp_lock.h>
24 #include <linux/slab.h>
25 #include <linux/syscalls.h>
26 #include <linux/ipc.h>
27 #include <linux/personality.h>
28
29 #include <asm/uaccess.h>
30 #include <asm/ipc.h>
31 #include <asm/utrap.h>
32 #include <asm/perfctr.h>
33
34 /* #define DEBUG_UNIMP_SYSCALL */
35
36 /* XXX Make this per-binary type, this way we can detect the type of
37 * XXX a binary. Every Sparc executable calls this very early on.
38 */
39 asmlinkage unsigned long sys_getpagesize(void)
40 {
41 return PAGE_SIZE;
42 }
43
44 #define COLOUR_ALIGN(addr,pgoff) \
45 ((((addr)+SHMLBA-1)&~(SHMLBA-1)) + \
46 (((pgoff)<<PAGE_SHIFT) & (SHMLBA-1)))
47
48 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)
49 {
50 struct mm_struct *mm = current->mm;
51 struct vm_area_struct * vma;
52 unsigned long task_size = TASK_SIZE;
53 unsigned long start_addr;
54 int do_color_align;
55
56 if (flags & MAP_FIXED) {
57 /* We do not accept a shared mapping if it would violate
58 * cache aliasing constraints.
59 */
60 if ((flags & MAP_SHARED) &&
61 ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
62 return -EINVAL;
63 return addr;
64 }
65
66 if (test_thread_flag(TIF_32BIT))
67 task_size = 0xf0000000UL;
68 if (len > task_size || len > -PAGE_OFFSET)
69 return -ENOMEM;
70
71 do_color_align = 0;
72 if (filp || (flags & MAP_SHARED))
73 do_color_align = 1;
74
75 if (addr) {
76 if (do_color_align)
77 addr = COLOUR_ALIGN(addr, pgoff);
78 else
79 addr = PAGE_ALIGN(addr);
80
81 vma = find_vma(mm, addr);
82 if (task_size - len >= addr &&
83 (!vma || addr + len <= vma->vm_start))
84 return addr;
85 }
86
87 if (len <= mm->cached_hole_size) {
88 mm->cached_hole_size = 0;
89 mm->free_area_cache = TASK_UNMAPPED_BASE;
90 }
91 start_addr = addr = mm->free_area_cache;
92
93 task_size -= len;
94
95 full_search:
96 if (do_color_align)
97 addr = COLOUR_ALIGN(addr, pgoff);
98 else
99 addr = PAGE_ALIGN(addr);
100
101 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
102 /* At this point: (!vma || addr < vma->vm_end). */
103 if (addr < PAGE_OFFSET && -PAGE_OFFSET - len < addr) {
104 addr = PAGE_OFFSET;
105 vma = find_vma(mm, PAGE_OFFSET);
106 }
107 if (task_size < addr) {
108 if (start_addr != TASK_UNMAPPED_BASE) {
109 start_addr = addr = TASK_UNMAPPED_BASE;
110 mm->cached_hole_size = 0;
111 goto full_search;
112 }
113 return -ENOMEM;
114 }
115 if (!vma || addr + len <= vma->vm_start) {
116 /*
117 * Remember the place where we stopped the search:
118 */
119 mm->free_area_cache = addr + len;
120 return addr;
121 }
122 if (addr + mm->cached_hole_size < vma->vm_start)
123 mm->cached_hole_size = vma->vm_start - addr;
124
125 addr = vma->vm_end;
126 if (do_color_align)
127 addr = COLOUR_ALIGN(addr, pgoff);
128 }
129 }
130
131 /* Try to align mapping such that we align it as much as possible. */
132 unsigned long get_fb_unmapped_area(struct file *filp, unsigned long orig_addr, unsigned long len, unsigned long pgoff, unsigned long flags)
133 {
134 unsigned long align_goal, addr = -ENOMEM;
135
136 if (flags & MAP_FIXED) {
137 /* Ok, don't mess with it. */
138 return get_unmapped_area(NULL, addr, len, pgoff, flags);
139 }
140 flags &= ~MAP_SHARED;
141
142 align_goal = PAGE_SIZE;
143 if (len >= (4UL * 1024 * 1024))
144 align_goal = (4UL * 1024 * 1024);
145 else if (len >= (512UL * 1024))
146 align_goal = (512UL * 1024);
147 else if (len >= (64UL * 1024))
148 align_goal = (64UL * 1024);
149
150 do {
151 addr = get_unmapped_area(NULL, orig_addr, len + (align_goal - PAGE_SIZE), pgoff, flags);
152 if (!(addr & ~PAGE_MASK)) {
153 addr = (addr + (align_goal - 1UL)) & ~(align_goal - 1UL);
154 break;
155 }
156
157 if (align_goal == (4UL * 1024 * 1024))
158 align_goal = (512UL * 1024);
159 else if (align_goal == (512UL * 1024))
160 align_goal = (64UL * 1024);
161 else
162 align_goal = PAGE_SIZE;
163 } while ((addr & ~PAGE_MASK) && align_goal > PAGE_SIZE);
164
165 /* Mapping is smaller than 64K or larger areas could not
166 * be obtained.
167 */
168 if (addr & ~PAGE_MASK)
169 addr = get_unmapped_area(NULL, orig_addr, len, pgoff, flags);
170
171 return addr;
172 }
173
174 asmlinkage unsigned long sparc_brk(unsigned long brk)
175 {
176 /* People could try to be nasty and use ta 0x6d in 32bit programs */
177 if (test_thread_flag(TIF_32BIT) &&
178 brk >= 0xf0000000UL)
179 return current->mm->brk;
180
181 if ((current->mm->brk & PAGE_OFFSET) != (brk & PAGE_OFFSET))
182 return current->mm->brk;
183 return sys_brk(brk);
184 }
185
186 /*
187 * sys_pipe() is the normal C calling standard for creating
188 * a pipe. It's not the way unix traditionally does this, though.
189 */
190 asmlinkage long sparc_pipe(struct pt_regs *regs)
191 {
192 int fd[2];
193 int error;
194
195 error = do_pipe(fd);
196 if (error)
197 goto out;
198 regs->u_regs[UREG_I1] = fd[1];
199 error = fd[0];
200 out:
201 return error;
202 }
203
204 /*
205 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
206 *
207 * This is really horribly ugly.
208 */
209
210 asmlinkage long sys_ipc(unsigned int call, int first, unsigned long second,
211 unsigned long third, void __user *ptr, long fifth)
212 {
213 int err;
214
215 /* No need for backward compatibility. We can start fresh... */
216 if (call <= SEMCTL) {
217 switch (call) {
218 case SEMOP:
219 err = sys_semtimedop(first, ptr,
220 (unsigned)second, NULL);
221 goto out;
222 case SEMTIMEDOP:
223 err = sys_semtimedop(first, ptr, (unsigned)second,
224 (const struct timespec __user *) fifth);
225 goto out;
226 case SEMGET:
227 err = sys_semget(first, (int)second, (int)third);
228 goto out;
229 case SEMCTL: {
230 union semun fourth;
231 err = -EINVAL;
232 if (!ptr)
233 goto out;
234 err = -EFAULT;
235 if (get_user(fourth.__pad,
236 (void __user * __user *) ptr))
237 goto out;
238 err = sys_semctl(first, (int)second | IPC_64,
239 (int)third, fourth);
240 goto out;
241 }
242 default:
243 err = -ENOSYS;
244 goto out;
245 };
246 }
247 if (call <= MSGCTL) {
248 switch (call) {
249 case MSGSND:
250 err = sys_msgsnd(first, ptr, (size_t)second,
251 (int)third);
252 goto out;
253 case MSGRCV:
254 err = sys_msgrcv(first, ptr, (size_t)second, fifth,
255 (int)third);
256 goto out;
257 case MSGGET:
258 err = sys_msgget((key_t)first, (int)second);
259 goto out;
260 case MSGCTL:
261 err = sys_msgctl(first, (int)second | IPC_64, ptr);
262 goto out;
263 default:
264 err = -ENOSYS;
265 goto out;
266 };
267 }
268 if (call <= SHMCTL) {
269 switch (call) {
270 case SHMAT: {
271 ulong raddr;
272 err = do_shmat(first, ptr, (int)second, &raddr);
273 if (!err) {
274 if (put_user(raddr,
275 (ulong __user *) third))
276 err = -EFAULT;
277 }
278 goto out;
279 }
280 case SHMDT:
281 err = sys_shmdt(ptr);
282 goto out;
283 case SHMGET:
284 err = sys_shmget(first, (size_t)second, (int)third);
285 goto out;
286 case SHMCTL:
287 err = sys_shmctl(first, (int)second | IPC_64, ptr);
288 goto out;
289 default:
290 err = -ENOSYS;
291 goto out;
292 };
293 } else {
294 err = -ENOSYS;
295 }
296 out:
297 return err;
298 }
299
300 asmlinkage long sparc64_newuname(struct new_utsname __user *name)
301 {
302 int ret = sys_newuname(name);
303
304 if (current->personality == PER_LINUX32 && !ret) {
305 ret = (copy_to_user(name->machine, "sparc\0\0", 8)
306 ? -EFAULT : 0);
307 }
308 return ret;
309 }
310
311 asmlinkage long sparc64_personality(unsigned long personality)
312 {
313 int ret;
314
315 if (current->personality == PER_LINUX32 &&
316 personality == PER_LINUX)
317 personality = PER_LINUX32;
318 ret = sys_personality(personality);
319 if (ret == PER_LINUX32)
320 ret = PER_LINUX;
321
322 return ret;
323 }
324
325 /* Linux version of mmap */
326 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
327 unsigned long prot, unsigned long flags, unsigned long fd,
328 unsigned long off)
329 {
330 struct file * file = NULL;
331 unsigned long retval = -EBADF;
332
333 if (!(flags & MAP_ANONYMOUS)) {
334 file = fget(fd);
335 if (!file)
336 goto out;
337 }
338 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
339 len = PAGE_ALIGN(len);
340 retval = -EINVAL;
341
342 if (test_thread_flag(TIF_32BIT)) {
343 if (len > 0xf0000000UL ||
344 ((flags & MAP_FIXED) && addr > 0xf0000000UL - len))
345 goto out_putf;
346 } else {
347 if (len > -PAGE_OFFSET ||
348 ((flags & MAP_FIXED) &&
349 addr < PAGE_OFFSET && addr + len > -PAGE_OFFSET))
350 goto out_putf;
351 }
352
353 down_write(&current->mm->mmap_sem);
354 retval = do_mmap(file, addr, len, prot, flags, off);
355 up_write(&current->mm->mmap_sem);
356
357 out_putf:
358 if (file)
359 fput(file);
360 out:
361 return retval;
362 }
363
364 asmlinkage long sys64_munmap(unsigned long addr, size_t len)
365 {
366 long ret;
367
368 if (len > -PAGE_OFFSET ||
369 (addr < PAGE_OFFSET && addr + len > -PAGE_OFFSET))
370 return -EINVAL;
371 down_write(&current->mm->mmap_sem);
372 ret = do_munmap(current->mm, addr, len);
373 up_write(&current->mm->mmap_sem);
374 return ret;
375 }
376
377 extern unsigned long do_mremap(unsigned long addr,
378 unsigned long old_len, unsigned long new_len,
379 unsigned long flags, unsigned long new_addr);
380
381 asmlinkage unsigned long sys64_mremap(unsigned long addr,
382 unsigned long old_len, unsigned long new_len,
383 unsigned long flags, unsigned long new_addr)
384 {
385 struct vm_area_struct *vma;
386 unsigned long ret = -EINVAL;
387 if (test_thread_flag(TIF_32BIT))
388 goto out;
389 if (old_len > -PAGE_OFFSET || new_len > -PAGE_OFFSET)
390 goto out;
391 if (addr < PAGE_OFFSET && addr + old_len > -PAGE_OFFSET)
392 goto out;
393 down_write(&current->mm->mmap_sem);
394 if (flags & MREMAP_FIXED) {
395 if (new_addr < PAGE_OFFSET &&
396 new_addr + new_len > -PAGE_OFFSET)
397 goto out_sem;
398 } else if (addr < PAGE_OFFSET && addr + new_len > -PAGE_OFFSET) {
399 unsigned long map_flags = 0;
400 struct file *file = NULL;
401
402 ret = -ENOMEM;
403 if (!(flags & MREMAP_MAYMOVE))
404 goto out_sem;
405
406 vma = find_vma(current->mm, addr);
407 if (vma) {
408 if (vma->vm_flags & VM_SHARED)
409 map_flags |= MAP_SHARED;
410 file = vma->vm_file;
411 }
412
413 /* MREMAP_FIXED checked above. */
414 new_addr = get_unmapped_area(file, addr, new_len,
415 vma ? vma->vm_pgoff : 0,
416 map_flags);
417 ret = new_addr;
418 if (new_addr & ~PAGE_MASK)
419 goto out_sem;
420 flags |= MREMAP_FIXED;
421 }
422 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
423 out_sem:
424 up_write(&current->mm->mmap_sem);
425 out:
426 return ret;
427 }
428
429 /* we come to here via sys_nis_syscall so it can setup the regs argument */
430 asmlinkage unsigned long c_sys_nis_syscall(struct pt_regs *regs)
431 {
432 static int count;
433
434 /* Don't make the system unusable, if someone goes stuck */
435 if (count++ > 5)
436 return -ENOSYS;
437
438 printk ("Unimplemented SPARC system call %ld\n",regs->u_regs[1]);
439 #ifdef DEBUG_UNIMP_SYSCALL
440 show_regs (regs);
441 #endif
442
443 return -ENOSYS;
444 }
445
446 /* #define DEBUG_SPARC_BREAKPOINT */
447
448 asmlinkage void sparc_breakpoint(struct pt_regs *regs)
449 {
450 siginfo_t info;
451
452 if (test_thread_flag(TIF_32BIT)) {
453 regs->tpc &= 0xffffffff;
454 regs->tnpc &= 0xffffffff;
455 }
456 #ifdef DEBUG_SPARC_BREAKPOINT
457 printk ("TRAP: Entering kernel PC=%lx, nPC=%lx\n", regs->tpc, regs->tnpc);
458 #endif
459 info.si_signo = SIGTRAP;
460 info.si_errno = 0;
461 info.si_code = TRAP_BRKPT;
462 info.si_addr = (void __user *)regs->tpc;
463 info.si_trapno = 0;
464 force_sig_info(SIGTRAP, &info, current);
465 #ifdef DEBUG_SPARC_BREAKPOINT
466 printk ("TRAP: Returning to space: PC=%lx nPC=%lx\n", regs->tpc, regs->tnpc);
467 #endif
468 }
469
470 extern void check_pending(int signum);
471
472 asmlinkage long sys_getdomainname(char __user *name, int len)
473 {
474 int nlen;
475 int err = -EFAULT;
476
477 down_read(&uts_sem);
478
479 nlen = strlen(system_utsname.domainname) + 1;
480
481 if (nlen < len)
482 len = nlen;
483 if (len > __NEW_UTS_LEN)
484 goto done;
485 if (copy_to_user(name, system_utsname.domainname, len))
486 goto done;
487 err = 0;
488 done:
489 up_read(&uts_sem);
490 return err;
491 }
492
493 asmlinkage long solaris_syscall(struct pt_regs *regs)
494 {
495 static int count;
496
497 regs->tpc = regs->tnpc;
498 regs->tnpc += 4;
499 if (test_thread_flag(TIF_32BIT)) {
500 regs->tpc &= 0xffffffff;
501 regs->tnpc &= 0xffffffff;
502 }
503 if (++count <= 5) {
504 printk ("For Solaris binary emulation you need solaris module loaded\n");
505 show_regs (regs);
506 }
507 send_sig(SIGSEGV, current, 1);
508
509 return -ENOSYS;
510 }
511
512 #ifndef CONFIG_SUNOS_EMUL
513 asmlinkage long sunos_syscall(struct pt_regs *regs)
514 {
515 static int count;
516
517 regs->tpc = regs->tnpc;
518 regs->tnpc += 4;
519 if (test_thread_flag(TIF_32BIT)) {
520 regs->tpc &= 0xffffffff;
521 regs->tnpc &= 0xffffffff;
522 }
523 if (++count <= 20)
524 printk ("SunOS binary emulation not compiled in\n");
525 force_sig(SIGSEGV, current);
526
527 return -ENOSYS;
528 }
529 #endif
530
531 asmlinkage long sys_utrap_install(utrap_entry_t type,
532 utrap_handler_t new_p,
533 utrap_handler_t new_d,
534 utrap_handler_t __user *old_p,
535 utrap_handler_t __user *old_d)
536 {
537 if (type < UT_INSTRUCTION_EXCEPTION || type > UT_TRAP_INSTRUCTION_31)
538 return -EINVAL;
539 if (new_p == (utrap_handler_t)(long)UTH_NOCHANGE) {
540 if (old_p) {
541 if (!current_thread_info()->utraps) {
542 if (put_user(NULL, old_p))
543 return -EFAULT;
544 } else {
545 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
546 return -EFAULT;
547 }
548 }
549 if (old_d) {
550 if (put_user(NULL, old_d))
551 return -EFAULT;
552 }
553 return 0;
554 }
555 if (!current_thread_info()->utraps) {
556 current_thread_info()->utraps =
557 kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long), GFP_KERNEL);
558 if (!current_thread_info()->utraps)
559 return -ENOMEM;
560 current_thread_info()->utraps[0] = 1;
561 memset(current_thread_info()->utraps+1, 0,
562 UT_TRAP_INSTRUCTION_31*sizeof(long));
563 } else {
564 if ((utrap_handler_t)current_thread_info()->utraps[type] != new_p &&
565 current_thread_info()->utraps[0] > 1) {
566 long *p = current_thread_info()->utraps;
567
568 current_thread_info()->utraps =
569 kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long),
570 GFP_KERNEL);
571 if (!current_thread_info()->utraps) {
572 current_thread_info()->utraps = p;
573 return -ENOMEM;
574 }
575 p[0]--;
576 current_thread_info()->utraps[0] = 1;
577 memcpy(current_thread_info()->utraps+1, p+1,
578 UT_TRAP_INSTRUCTION_31*sizeof(long));
579 }
580 }
581 if (old_p) {
582 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
583 return -EFAULT;
584 }
585 if (old_d) {
586 if (put_user(NULL, old_d))
587 return -EFAULT;
588 }
589 current_thread_info()->utraps[type] = (long)new_p;
590
591 return 0;
592 }
593
594 long sparc_memory_ordering(unsigned long model, struct pt_regs *regs)
595 {
596 if (model >= 3)
597 return -EINVAL;
598 regs->tstate = (regs->tstate & ~TSTATE_MM) | (model << 14);
599 return 0;
600 }
601
602 asmlinkage long sys_rt_sigaction(int sig,
603 const struct sigaction __user *act,
604 struct sigaction __user *oact,
605 void __user *restorer,
606 size_t sigsetsize)
607 {
608 struct k_sigaction new_ka, old_ka;
609 int ret;
610
611 /* XXX: Don't preclude handling different sized sigset_t's. */
612 if (sigsetsize != sizeof(sigset_t))
613 return -EINVAL;
614
615 if (act) {
616 new_ka.ka_restorer = restorer;
617 if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
618 return -EFAULT;
619 }
620
621 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
622
623 if (!ret && oact) {
624 if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
625 return -EFAULT;
626 }
627
628 return ret;
629 }
630
631 /* Invoked by rtrap code to update performance counters in
632 * user space.
633 */
634 asmlinkage void update_perfctrs(void)
635 {
636 unsigned long pic, tmp;
637
638 read_pic(pic);
639 tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
640 __put_user(tmp, current_thread_info()->user_cntd0);
641 tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
642 __put_user(tmp, current_thread_info()->user_cntd1);
643 reset_pic();
644 }
645
646 asmlinkage long sys_perfctr(int opcode, unsigned long arg0, unsigned long arg1, unsigned long arg2)
647 {
648 int err = 0;
649
650 switch(opcode) {
651 case PERFCTR_ON:
652 current_thread_info()->pcr_reg = arg2;
653 current_thread_info()->user_cntd0 = (u64 __user *) arg0;
654 current_thread_info()->user_cntd1 = (u64 __user *) arg1;
655 current_thread_info()->kernel_cntd0 =
656 current_thread_info()->kernel_cntd1 = 0;
657 write_pcr(arg2);
658 reset_pic();
659 set_thread_flag(TIF_PERFCTR);
660 break;
661
662 case PERFCTR_OFF:
663 err = -EINVAL;
664 if (test_thread_flag(TIF_PERFCTR)) {
665 current_thread_info()->user_cntd0 =
666 current_thread_info()->user_cntd1 = NULL;
667 current_thread_info()->pcr_reg = 0;
668 write_pcr(0);
669 clear_thread_flag(TIF_PERFCTR);
670 err = 0;
671 }
672 break;
673
674 case PERFCTR_READ: {
675 unsigned long pic, tmp;
676
677 if (!test_thread_flag(TIF_PERFCTR)) {
678 err = -EINVAL;
679 break;
680 }
681 read_pic(pic);
682 tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
683 err |= __put_user(tmp, current_thread_info()->user_cntd0);
684 tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
685 err |= __put_user(tmp, current_thread_info()->user_cntd1);
686 reset_pic();
687 break;
688 }
689
690 case PERFCTR_CLRPIC:
691 if (!test_thread_flag(TIF_PERFCTR)) {
692 err = -EINVAL;
693 break;
694 }
695 current_thread_info()->kernel_cntd0 =
696 current_thread_info()->kernel_cntd1 = 0;
697 reset_pic();
698 break;
699
700 case PERFCTR_SETPCR: {
701 u64 __user *user_pcr = (u64 __user *)arg0;
702
703 if (!test_thread_flag(TIF_PERFCTR)) {
704 err = -EINVAL;
705 break;
706 }
707 err |= __get_user(current_thread_info()->pcr_reg, user_pcr);
708 write_pcr(current_thread_info()->pcr_reg);
709 current_thread_info()->kernel_cntd0 =
710 current_thread_info()->kernel_cntd1 = 0;
711 reset_pic();
712 break;
713 }
714
715 case PERFCTR_GETPCR: {
716 u64 __user *user_pcr = (u64 __user *)arg0;
717
718 if (!test_thread_flag(TIF_PERFCTR)) {
719 err = -EINVAL;
720 break;
721 }
722 err |= __put_user(current_thread_info()->pcr_reg, user_pcr);
723 break;
724 }
725
726 default:
727 err = -EINVAL;
728 break;
729 };
730 return err;
731 }
This page took 0.060215 seconds and 5 git commands to generate.