1 /* $Id: sys_sparc.c,v 1.57 2002/02/09 19:49:30 davem Exp $
2 * linux/arch/sparc64/kernel/sys_sparc.c
4 * This file contains various random system calls that
5 * have a non-standard calling sequence on the Linux/sparc
9 #include <linux/config.h>
10 #include <linux/errno.h>
11 #include <linux/types.h>
12 #include <linux/sched.h>
14 #include <linux/file.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>
29 #include <asm/uaccess.h>
31 #include <asm/utrap.h>
32 #include <asm/perfctr.h>
34 /* #define DEBUG_UNIMP_SYSCALL */
36 asmlinkage unsigned long sys_getpagesize(void)
41 #define VA_EXCLUDE_START (0x0000080000000000UL - (1UL << 32UL))
42 #define VA_EXCLUDE_END (0xfffff80000000000UL + (1UL << 32UL))
44 /* Does addr --> addr+len fall within 4GB of the VA-space hole or
45 * overflow past the end of the 64-bit address space?
47 static inline int invalid_64bit_range(unsigned long addr, unsigned long len)
49 unsigned long va_exclude_start, va_exclude_end;
51 va_exclude_start = VA_EXCLUDE_START;
52 va_exclude_end = VA_EXCLUDE_END;
54 if (unlikely(len >= va_exclude_start))
57 if (unlikely((addr + len) < addr))
60 if (unlikely((addr >= va_exclude_start && addr < va_exclude_end) ||
61 ((addr + len) >= va_exclude_start &&
62 (addr + len) < va_exclude_end)))
68 /* Does start,end straddle the VA-space hole? */
69 static inline int straddles_64bit_va_hole(unsigned long start, unsigned long end)
71 unsigned long va_exclude_start, va_exclude_end;
73 va_exclude_start = VA_EXCLUDE_START;
74 va_exclude_end = VA_EXCLUDE_END;
76 if (likely(start < va_exclude_start && end < va_exclude_start))
79 if (likely(start >= va_exclude_end && end >= va_exclude_end))
85 /* These functions differ from the default implementations in
86 * mm/mmap.c in two ways:
88 * 1) For file backed MAP_SHARED mmap()'s we D-cache color align,
89 * for fixed such mappings we just validate what the user gave us.
90 * 2) For 64-bit tasks we avoid mapping anything within 4GB of
91 * the spitfire/niagara VA-hole.
94 static inline unsigned long COLOUR_ALIGN(unsigned long addr,
97 unsigned long base = (addr+SHMLBA-1)&~(SHMLBA-1);
98 unsigned long off = (pgoff<<PAGE_SHIFT) & (SHMLBA-1);
103 static inline unsigned long COLOUR_ALIGN_DOWN(unsigned long addr,
106 unsigned long base = addr & ~(SHMLBA-1);
107 unsigned long off = (pgoff<<PAGE_SHIFT) & (SHMLBA-1);
109 if (base + off <= addr)
114 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)
116 struct mm_struct *mm = current->mm;
117 struct vm_area_struct * vma;
118 unsigned long task_size = TASK_SIZE;
119 unsigned long start_addr;
122 if (flags & MAP_FIXED) {
123 /* We do not accept a shared mapping if it would violate
124 * cache aliasing constraints.
126 if ((flags & MAP_SHARED) &&
127 ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
132 if (test_thread_flag(TIF_32BIT))
133 task_size = 0xf0000000UL;
134 if (unlikely(len > task_size || len >= VA_EXCLUDE_START))
138 if (filp || (flags & MAP_SHARED))
143 addr = COLOUR_ALIGN(addr, pgoff);
145 addr = PAGE_ALIGN(addr);
147 vma = find_vma(mm, addr);
148 if (task_size - len >= addr &&
149 (!vma || addr + len <= vma->vm_start))
153 if (len > mm->cached_hole_size) {
154 start_addr = addr = mm->free_area_cache;
156 start_addr = addr = TASK_UNMAPPED_BASE;
157 mm->cached_hole_size = 0;
164 addr = COLOUR_ALIGN(addr, pgoff);
166 addr = PAGE_ALIGN(addr);
168 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
169 /* At this point: (!vma || addr < vma->vm_end). */
170 if (addr < VA_EXCLUDE_START &&
171 (addr + len) >= VA_EXCLUDE_START) {
172 addr = VA_EXCLUDE_END;
173 vma = find_vma(mm, VA_EXCLUDE_END);
175 if (unlikely(task_size < addr)) {
176 if (start_addr != TASK_UNMAPPED_BASE) {
177 start_addr = addr = TASK_UNMAPPED_BASE;
178 mm->cached_hole_size = 0;
183 if (likely(!vma || addr + len <= vma->vm_start)) {
185 * Remember the place where we stopped the search:
187 mm->free_area_cache = addr + len;
190 if (addr + mm->cached_hole_size < vma->vm_start)
191 mm->cached_hole_size = vma->vm_start - addr;
195 addr = COLOUR_ALIGN(addr, pgoff);
200 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
201 const unsigned long len, const unsigned long pgoff,
202 const unsigned long flags)
204 struct vm_area_struct *vma;
205 struct mm_struct *mm = current->mm;
206 unsigned long task_size = 0xf0000000UL;
207 unsigned long addr = addr0;
210 /* This should only ever run for 32-bit processes. */
211 BUG_ON(!test_thread_flag(TIF_32BIT));
213 if (flags & MAP_FIXED) {
214 /* We do not accept a shared mapping if it would violate
215 * cache aliasing constraints.
217 if ((flags & MAP_SHARED) &&
218 ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
223 if (unlikely(len > task_size))
227 if (filp || (flags & MAP_SHARED))
230 /* requesting a specific address */
233 addr = COLOUR_ALIGN(addr, pgoff);
235 addr = PAGE_ALIGN(addr);
237 vma = find_vma(mm, addr);
238 if (task_size - len >= addr &&
239 (!vma || addr + len <= vma->vm_start))
243 /* check if free_area_cache is useful for us */
244 if (len <= mm->cached_hole_size) {
245 mm->cached_hole_size = 0;
246 mm->free_area_cache = mm->mmap_base;
249 /* either no address requested or can't fit in requested address hole */
250 addr = mm->free_area_cache;
251 if (do_color_align) {
252 unsigned long base = COLOUR_ALIGN_DOWN(addr-len, pgoff);
257 /* make sure it can fit in the remaining address space */
258 if (likely(addr > len)) {
259 vma = find_vma(mm, addr-len);
260 if (!vma || addr <= vma->vm_start) {
261 /* remember the address as a hint for next time */
262 return (mm->free_area_cache = addr-len);
266 if (unlikely(mm->mmap_base < len))
269 addr = mm->mmap_base-len;
271 addr = COLOUR_ALIGN_DOWN(addr, pgoff);
275 * Lookup failure means no vma is above this address,
276 * else if new region fits below vma->vm_start,
277 * return with success:
279 vma = find_vma(mm, addr);
280 if (likely(!vma || addr+len <= vma->vm_start)) {
281 /* remember the address as a hint for next time */
282 return (mm->free_area_cache = addr);
285 /* remember the largest hole we saw so far */
286 if (addr + mm->cached_hole_size < vma->vm_start)
287 mm->cached_hole_size = vma->vm_start - addr;
289 /* try just below the current vma->vm_start */
290 addr = vma->vm_start-len;
292 addr = COLOUR_ALIGN_DOWN(addr, pgoff);
293 } while (likely(len < vma->vm_start));
297 * A failed mmap() very likely causes application failure,
298 * so fall back to the bottom-up function here. This scenario
299 * can happen with large stack limits and large mmap()
302 mm->cached_hole_size = ~0UL;
303 mm->free_area_cache = TASK_UNMAPPED_BASE;
304 addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
306 * Restore the topdown base:
308 mm->free_area_cache = mm->mmap_base;
309 mm->cached_hole_size = ~0UL;
314 /* Try to align mapping such that we align it as much as possible. */
315 unsigned long get_fb_unmapped_area(struct file *filp, unsigned long orig_addr, unsigned long len, unsigned long pgoff, unsigned long flags)
317 unsigned long align_goal, addr = -ENOMEM;
319 if (flags & MAP_FIXED) {
320 /* Ok, don't mess with it. */
321 return get_unmapped_area(NULL, addr, len, pgoff, flags);
323 flags &= ~MAP_SHARED;
325 align_goal = PAGE_SIZE;
326 if (len >= (4UL * 1024 * 1024))
327 align_goal = (4UL * 1024 * 1024);
328 else if (len >= (512UL * 1024))
329 align_goal = (512UL * 1024);
330 else if (len >= (64UL * 1024))
331 align_goal = (64UL * 1024);
334 addr = get_unmapped_area(NULL, orig_addr, len + (align_goal - PAGE_SIZE), pgoff, flags);
335 if (!(addr & ~PAGE_MASK)) {
336 addr = (addr + (align_goal - 1UL)) & ~(align_goal - 1UL);
340 if (align_goal == (4UL * 1024 * 1024))
341 align_goal = (512UL * 1024);
342 else if (align_goal == (512UL * 1024))
343 align_goal = (64UL * 1024);
345 align_goal = PAGE_SIZE;
346 } while ((addr & ~PAGE_MASK) && align_goal > PAGE_SIZE);
348 /* Mapping is smaller than 64K or larger areas could not
351 if (addr & ~PAGE_MASK)
352 addr = get_unmapped_area(NULL, orig_addr, len, pgoff, flags);
357 /* Essentially the same as PowerPC... */
358 void arch_pick_mmap_layout(struct mm_struct *mm)
361 * Fall back to the standard layout if the personality
362 * bit is set, or if the expected stack growth is unlimited:
364 if (!test_thread_flag(TIF_32BIT) ||
365 (current->personality & ADDR_COMPAT_LAYOUT) ||
366 current->signal->rlim[RLIMIT_STACK].rlim_cur == RLIM_INFINITY ||
367 sysctl_legacy_va_layout) {
368 mm->mmap_base = TASK_UNMAPPED_BASE;
369 mm->get_unmapped_area = arch_get_unmapped_area;
370 mm->unmap_area = arch_unmap_area;
372 /* We know it's 32-bit */
373 unsigned long task_size = 0xf0000000UL;
376 gap = current->signal->rlim[RLIMIT_STACK].rlim_cur;
377 if (gap < 128 * 1024 * 1024)
378 gap = 128 * 1024 * 1024;
379 if (gap > (task_size / 6 * 5))
380 gap = (task_size / 6 * 5);
382 mm->mmap_base = task_size - (gap & PAGE_MASK);
383 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
384 mm->unmap_area = arch_unmap_area_topdown;
388 asmlinkage unsigned long sparc_brk(unsigned long brk)
390 /* People could try to be nasty and use ta 0x6d in 32bit programs */
391 if (test_thread_flag(TIF_32BIT) && brk >= 0xf0000000UL)
392 return current->mm->brk;
394 if (unlikely(straddles_64bit_va_hole(current->mm->brk, brk)))
395 return current->mm->brk;
401 * sys_pipe() is the normal C calling standard for creating
402 * a pipe. It's not the way unix traditionally does this, though.
404 asmlinkage long sparc_pipe(struct pt_regs *regs)
412 regs->u_regs[UREG_I1] = fd[1];
419 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
421 * This is really horribly ugly.
424 asmlinkage long sys_ipc(unsigned int call, int first, unsigned long second,
425 unsigned long third, void __user *ptr, long fifth)
429 /* No need for backward compatibility. We can start fresh... */
430 if (call <= SEMCTL) {
433 err = sys_semtimedop(first, ptr,
434 (unsigned)second, NULL);
437 err = sys_semtimedop(first, ptr, (unsigned)second,
438 (const struct timespec __user *) fifth);
441 err = sys_semget(first, (int)second, (int)third);
449 if (get_user(fourth.__pad,
450 (void __user * __user *) ptr))
452 err = sys_semctl(first, (int)second | IPC_64,
461 if (call <= MSGCTL) {
464 err = sys_msgsnd(first, ptr, (size_t)second,
468 err = sys_msgrcv(first, ptr, (size_t)second, fifth,
472 err = sys_msgget((key_t)first, (int)second);
475 err = sys_msgctl(first, (int)second | IPC_64, ptr);
482 if (call <= SHMCTL) {
486 err = do_shmat(first, ptr, (int)second, &raddr);
489 (ulong __user *) third))
495 err = sys_shmdt(ptr);
498 err = sys_shmget(first, (size_t)second, (int)third);
501 err = sys_shmctl(first, (int)second | IPC_64, ptr);
514 asmlinkage long sparc64_newuname(struct new_utsname __user *name)
516 int ret = sys_newuname(name);
518 if (current->personality == PER_LINUX32 && !ret) {
519 ret = (copy_to_user(name->machine, "sparc\0\0", 8)
525 asmlinkage long sparc64_personality(unsigned long personality)
529 if (current->personality == PER_LINUX32 &&
530 personality == PER_LINUX)
531 personality = PER_LINUX32;
532 ret = sys_personality(personality);
533 if (ret == PER_LINUX32)
539 /* Linux version of mmap */
540 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
541 unsigned long prot, unsigned long flags, unsigned long fd,
544 struct file * file = NULL;
545 unsigned long retval = -EBADF;
547 if (!(flags & MAP_ANONYMOUS)) {
552 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
553 len = PAGE_ALIGN(len);
556 if (test_thread_flag(TIF_32BIT)) {
557 if (len >= 0xf0000000UL)
560 if ((flags & MAP_FIXED) && addr > 0xf0000000UL - len)
563 if (len >= VA_EXCLUDE_START)
566 if ((flags & MAP_FIXED) && invalid_64bit_range(addr, len))
570 down_write(¤t->mm->mmap_sem);
571 retval = do_mmap(file, addr, len, prot, flags, off);
572 up_write(¤t->mm->mmap_sem);
581 asmlinkage long sys64_munmap(unsigned long addr, size_t len)
585 if (invalid_64bit_range(addr, len))
588 down_write(¤t->mm->mmap_sem);
589 ret = do_munmap(current->mm, addr, len);
590 up_write(¤t->mm->mmap_sem);
594 extern unsigned long do_mremap(unsigned long addr,
595 unsigned long old_len, unsigned long new_len,
596 unsigned long flags, unsigned long new_addr);
598 asmlinkage unsigned long sys64_mremap(unsigned long addr,
599 unsigned long old_len, unsigned long new_len,
600 unsigned long flags, unsigned long new_addr)
602 struct vm_area_struct *vma;
603 unsigned long ret = -EINVAL;
605 if (test_thread_flag(TIF_32BIT))
607 if (unlikely(new_len >= VA_EXCLUDE_START))
609 if (unlikely(invalid_64bit_range(addr, old_len)))
612 down_write(¤t->mm->mmap_sem);
613 if (flags & MREMAP_FIXED) {
614 if (invalid_64bit_range(new_addr, new_len))
616 } else if (invalid_64bit_range(addr, new_len)) {
617 unsigned long map_flags = 0;
618 struct file *file = NULL;
621 if (!(flags & MREMAP_MAYMOVE))
624 vma = find_vma(current->mm, addr);
626 if (vma->vm_flags & VM_SHARED)
627 map_flags |= MAP_SHARED;
631 /* MREMAP_FIXED checked above. */
632 new_addr = get_unmapped_area(file, addr, new_len,
633 vma ? vma->vm_pgoff : 0,
636 if (new_addr & ~PAGE_MASK)
638 flags |= MREMAP_FIXED;
640 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
642 up_write(¤t->mm->mmap_sem);
647 /* we come to here via sys_nis_syscall so it can setup the regs argument */
648 asmlinkage unsigned long c_sys_nis_syscall(struct pt_regs *regs)
652 /* Don't make the system unusable, if someone goes stuck */
656 printk ("Unimplemented SPARC system call %ld\n",regs->u_regs[1]);
657 #ifdef DEBUG_UNIMP_SYSCALL
664 /* #define DEBUG_SPARC_BREAKPOINT */
666 asmlinkage void sparc_breakpoint(struct pt_regs *regs)
670 if (test_thread_flag(TIF_32BIT)) {
671 regs->tpc &= 0xffffffff;
672 regs->tnpc &= 0xffffffff;
674 #ifdef DEBUG_SPARC_BREAKPOINT
675 printk ("TRAP: Entering kernel PC=%lx, nPC=%lx\n", regs->tpc, regs->tnpc);
677 info.si_signo = SIGTRAP;
679 info.si_code = TRAP_BRKPT;
680 info.si_addr = (void __user *)regs->tpc;
682 force_sig_info(SIGTRAP, &info, current);
683 #ifdef DEBUG_SPARC_BREAKPOINT
684 printk ("TRAP: Returning to space: PC=%lx nPC=%lx\n", regs->tpc, regs->tnpc);
688 extern void check_pending(int signum);
690 asmlinkage long sys_getdomainname(char __user *name, int len)
697 nlen = strlen(system_utsname.domainname) + 1;
701 if (len > __NEW_UTS_LEN)
703 if (copy_to_user(name, system_utsname.domainname, len))
711 asmlinkage long solaris_syscall(struct pt_regs *regs)
715 regs->tpc = regs->tnpc;
717 if (test_thread_flag(TIF_32BIT)) {
718 regs->tpc &= 0xffffffff;
719 regs->tnpc &= 0xffffffff;
722 printk ("For Solaris binary emulation you need solaris module loaded\n");
725 send_sig(SIGSEGV, current, 1);
730 #ifndef CONFIG_SUNOS_EMUL
731 asmlinkage long sunos_syscall(struct pt_regs *regs)
735 regs->tpc = regs->tnpc;
737 if (test_thread_flag(TIF_32BIT)) {
738 regs->tpc &= 0xffffffff;
739 regs->tnpc &= 0xffffffff;
742 printk ("SunOS binary emulation not compiled in\n");
743 force_sig(SIGSEGV, current);
749 asmlinkage long sys_utrap_install(utrap_entry_t type,
750 utrap_handler_t new_p,
751 utrap_handler_t new_d,
752 utrap_handler_t __user *old_p,
753 utrap_handler_t __user *old_d)
755 if (type < UT_INSTRUCTION_EXCEPTION || type > UT_TRAP_INSTRUCTION_31)
757 if (new_p == (utrap_handler_t)(long)UTH_NOCHANGE) {
759 if (!current_thread_info()->utraps) {
760 if (put_user(NULL, old_p))
763 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
768 if (put_user(NULL, old_d))
773 if (!current_thread_info()->utraps) {
774 current_thread_info()->utraps =
775 kzalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long), GFP_KERNEL);
776 if (!current_thread_info()->utraps)
778 current_thread_info()->utraps[0] = 1;
780 if ((utrap_handler_t)current_thread_info()->utraps[type] != new_p &&
781 current_thread_info()->utraps[0] > 1) {
782 long *p = current_thread_info()->utraps;
784 current_thread_info()->utraps =
785 kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long),
787 if (!current_thread_info()->utraps) {
788 current_thread_info()->utraps = p;
792 current_thread_info()->utraps[0] = 1;
793 memcpy(current_thread_info()->utraps+1, p+1,
794 UT_TRAP_INSTRUCTION_31*sizeof(long));
798 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
802 if (put_user(NULL, old_d))
805 current_thread_info()->utraps[type] = (long)new_p;
810 long sparc_memory_ordering(unsigned long model, struct pt_regs *regs)
814 regs->tstate = (regs->tstate & ~TSTATE_MM) | (model << 14);
818 asmlinkage long sys_rt_sigaction(int sig,
819 const struct sigaction __user *act,
820 struct sigaction __user *oact,
821 void __user *restorer,
824 struct k_sigaction new_ka, old_ka;
827 /* XXX: Don't preclude handling different sized sigset_t's. */
828 if (sigsetsize != sizeof(sigset_t))
832 new_ka.ka_restorer = restorer;
833 if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
837 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
840 if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
847 /* Invoked by rtrap code to update performance counters in
850 asmlinkage void update_perfctrs(void)
852 unsigned long pic, tmp;
855 tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
856 __put_user(tmp, current_thread_info()->user_cntd0);
857 tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
858 __put_user(tmp, current_thread_info()->user_cntd1);
862 asmlinkage long sys_perfctr(int opcode, unsigned long arg0, unsigned long arg1, unsigned long arg2)
868 current_thread_info()->pcr_reg = arg2;
869 current_thread_info()->user_cntd0 = (u64 __user *) arg0;
870 current_thread_info()->user_cntd1 = (u64 __user *) arg1;
871 current_thread_info()->kernel_cntd0 =
872 current_thread_info()->kernel_cntd1 = 0;
875 set_thread_flag(TIF_PERFCTR);
880 if (test_thread_flag(TIF_PERFCTR)) {
881 current_thread_info()->user_cntd0 =
882 current_thread_info()->user_cntd1 = NULL;
883 current_thread_info()->pcr_reg = 0;
885 clear_thread_flag(TIF_PERFCTR);
891 unsigned long pic, tmp;
893 if (!test_thread_flag(TIF_PERFCTR)) {
898 tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
899 err |= __put_user(tmp, current_thread_info()->user_cntd0);
900 tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
901 err |= __put_user(tmp, current_thread_info()->user_cntd1);
907 if (!test_thread_flag(TIF_PERFCTR)) {
911 current_thread_info()->kernel_cntd0 =
912 current_thread_info()->kernel_cntd1 = 0;
916 case PERFCTR_SETPCR: {
917 u64 __user *user_pcr = (u64 __user *)arg0;
919 if (!test_thread_flag(TIF_PERFCTR)) {
923 err |= __get_user(current_thread_info()->pcr_reg, user_pcr);
924 write_pcr(current_thread_info()->pcr_reg);
925 current_thread_info()->kernel_cntd0 =
926 current_thread_info()->kernel_cntd1 = 0;
931 case PERFCTR_GETPCR: {
932 u64 __user *user_pcr = (u64 __user *)arg0;
934 if (!test_thread_flag(TIF_PERFCTR)) {
938 err |= __put_user(current_thread_info()->pcr_reg, user_pcr);