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 #define COLOUR_ALIGN(addr,pgoff) \
86 ((((addr)+SHMLBA-1)&~(SHMLBA-1)) + \
87 (((pgoff)<<PAGE_SHIFT) & (SHMLBA-1)))
89 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)
91 struct mm_struct *mm = current->mm;
92 struct vm_area_struct * vma;
93 unsigned long task_size = TASK_SIZE;
94 unsigned long start_addr;
97 if (flags & MAP_FIXED) {
98 /* We do not accept a shared mapping if it would violate
99 * cache aliasing constraints.
101 if ((flags & MAP_SHARED) &&
102 ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
107 if (test_thread_flag(TIF_32BIT))
108 task_size = 0xf0000000UL;
109 if (len > task_size || len >= VA_EXCLUDE_START)
113 if (filp || (flags & MAP_SHARED))
118 addr = COLOUR_ALIGN(addr, pgoff);
120 addr = PAGE_ALIGN(addr);
122 vma = find_vma(mm, addr);
123 if (task_size - len >= addr &&
124 (!vma || addr + len <= vma->vm_start))
128 if (len <= mm->cached_hole_size) {
129 mm->cached_hole_size = 0;
130 mm->free_area_cache = TASK_UNMAPPED_BASE;
132 start_addr = addr = mm->free_area_cache;
138 addr = COLOUR_ALIGN(addr, pgoff);
140 addr = PAGE_ALIGN(addr);
142 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
143 /* At this point: (!vma || addr < vma->vm_end). */
144 if (addr < VA_EXCLUDE_START &&
145 (addr + len) >= VA_EXCLUDE_START) {
146 addr = VA_EXCLUDE_END;
147 vma = find_vma(mm, VA_EXCLUDE_END);
149 if (task_size < addr) {
150 if (start_addr != TASK_UNMAPPED_BASE) {
151 start_addr = addr = TASK_UNMAPPED_BASE;
152 mm->cached_hole_size = 0;
157 if (!vma || addr + len <= vma->vm_start) {
159 * Remember the place where we stopped the search:
161 mm->free_area_cache = addr + len;
164 if (addr + mm->cached_hole_size < vma->vm_start)
165 mm->cached_hole_size = vma->vm_start - addr;
169 addr = COLOUR_ALIGN(addr, pgoff);
173 /* Try to align mapping such that we align it as much as possible. */
174 unsigned long get_fb_unmapped_area(struct file *filp, unsigned long orig_addr, unsigned long len, unsigned long pgoff, unsigned long flags)
176 unsigned long align_goal, addr = -ENOMEM;
178 if (flags & MAP_FIXED) {
179 /* Ok, don't mess with it. */
180 return get_unmapped_area(NULL, addr, len, pgoff, flags);
182 flags &= ~MAP_SHARED;
184 align_goal = PAGE_SIZE;
185 if (len >= (4UL * 1024 * 1024))
186 align_goal = (4UL * 1024 * 1024);
187 else if (len >= (512UL * 1024))
188 align_goal = (512UL * 1024);
189 else if (len >= (64UL * 1024))
190 align_goal = (64UL * 1024);
193 addr = get_unmapped_area(NULL, orig_addr, len + (align_goal - PAGE_SIZE), pgoff, flags);
194 if (!(addr & ~PAGE_MASK)) {
195 addr = (addr + (align_goal - 1UL)) & ~(align_goal - 1UL);
199 if (align_goal == (4UL * 1024 * 1024))
200 align_goal = (512UL * 1024);
201 else if (align_goal == (512UL * 1024))
202 align_goal = (64UL * 1024);
204 align_goal = PAGE_SIZE;
205 } while ((addr & ~PAGE_MASK) && align_goal > PAGE_SIZE);
207 /* Mapping is smaller than 64K or larger areas could not
210 if (addr & ~PAGE_MASK)
211 addr = get_unmapped_area(NULL, orig_addr, len, pgoff, flags);
216 asmlinkage unsigned long sparc_brk(unsigned long brk)
218 /* People could try to be nasty and use ta 0x6d in 32bit programs */
219 if (test_thread_flag(TIF_32BIT) && brk >= 0xf0000000UL)
220 return current->mm->brk;
222 if (unlikely(straddles_64bit_va_hole(current->mm->brk, brk)))
223 return current->mm->brk;
229 * sys_pipe() is the normal C calling standard for creating
230 * a pipe. It's not the way unix traditionally does this, though.
232 asmlinkage long sparc_pipe(struct pt_regs *regs)
240 regs->u_regs[UREG_I1] = fd[1];
247 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
249 * This is really horribly ugly.
252 asmlinkage long sys_ipc(unsigned int call, int first, unsigned long second,
253 unsigned long third, void __user *ptr, long fifth)
257 /* No need for backward compatibility. We can start fresh... */
258 if (call <= SEMCTL) {
261 err = sys_semtimedop(first, ptr,
262 (unsigned)second, NULL);
265 err = sys_semtimedop(first, ptr, (unsigned)second,
266 (const struct timespec __user *) fifth);
269 err = sys_semget(first, (int)second, (int)third);
277 if (get_user(fourth.__pad,
278 (void __user * __user *) ptr))
280 err = sys_semctl(first, (int)second | IPC_64,
289 if (call <= MSGCTL) {
292 err = sys_msgsnd(first, ptr, (size_t)second,
296 err = sys_msgrcv(first, ptr, (size_t)second, fifth,
300 err = sys_msgget((key_t)first, (int)second);
303 err = sys_msgctl(first, (int)second | IPC_64, ptr);
310 if (call <= SHMCTL) {
314 err = do_shmat(first, ptr, (int)second, &raddr);
317 (ulong __user *) third))
323 err = sys_shmdt(ptr);
326 err = sys_shmget(first, (size_t)second, (int)third);
329 err = sys_shmctl(first, (int)second | IPC_64, ptr);
342 asmlinkage long sparc64_newuname(struct new_utsname __user *name)
344 int ret = sys_newuname(name);
346 if (current->personality == PER_LINUX32 && !ret) {
347 ret = (copy_to_user(name->machine, "sparc\0\0", 8)
353 asmlinkage long sparc64_personality(unsigned long personality)
357 if (current->personality == PER_LINUX32 &&
358 personality == PER_LINUX)
359 personality = PER_LINUX32;
360 ret = sys_personality(personality);
361 if (ret == PER_LINUX32)
367 /* Linux version of mmap */
368 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
369 unsigned long prot, unsigned long flags, unsigned long fd,
372 struct file * file = NULL;
373 unsigned long retval = -EBADF;
375 if (!(flags & MAP_ANONYMOUS)) {
380 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
381 len = PAGE_ALIGN(len);
384 if (test_thread_flag(TIF_32BIT)) {
385 if (len >= 0xf0000000UL)
388 if ((flags & MAP_FIXED) && addr > 0xf0000000UL - len)
391 if (len >= VA_EXCLUDE_START)
394 if ((flags & MAP_FIXED) && invalid_64bit_range(addr, len))
398 down_write(¤t->mm->mmap_sem);
399 retval = do_mmap(file, addr, len, prot, flags, off);
400 up_write(¤t->mm->mmap_sem);
409 asmlinkage long sys64_munmap(unsigned long addr, size_t len)
413 if (invalid_64bit_range(addr, len))
416 down_write(¤t->mm->mmap_sem);
417 ret = do_munmap(current->mm, addr, len);
418 up_write(¤t->mm->mmap_sem);
422 extern unsigned long do_mremap(unsigned long addr,
423 unsigned long old_len, unsigned long new_len,
424 unsigned long flags, unsigned long new_addr);
426 asmlinkage unsigned long sys64_mremap(unsigned long addr,
427 unsigned long old_len, unsigned long new_len,
428 unsigned long flags, unsigned long new_addr)
430 struct vm_area_struct *vma;
431 unsigned long ret = -EINVAL;
433 if (test_thread_flag(TIF_32BIT))
435 if (unlikely(new_len >= VA_EXCLUDE_START))
437 if (unlikely(invalid_64bit_range(addr, old_len)))
440 down_write(¤t->mm->mmap_sem);
441 if (flags & MREMAP_FIXED) {
442 if (invalid_64bit_range(new_addr, new_len))
444 } else if (invalid_64bit_range(addr, new_len)) {
445 unsigned long map_flags = 0;
446 struct file *file = NULL;
449 if (!(flags & MREMAP_MAYMOVE))
452 vma = find_vma(current->mm, addr);
454 if (vma->vm_flags & VM_SHARED)
455 map_flags |= MAP_SHARED;
459 /* MREMAP_FIXED checked above. */
460 new_addr = get_unmapped_area(file, addr, new_len,
461 vma ? vma->vm_pgoff : 0,
464 if (new_addr & ~PAGE_MASK)
466 flags |= MREMAP_FIXED;
468 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
470 up_write(¤t->mm->mmap_sem);
475 /* we come to here via sys_nis_syscall so it can setup the regs argument */
476 asmlinkage unsigned long c_sys_nis_syscall(struct pt_regs *regs)
480 /* Don't make the system unusable, if someone goes stuck */
484 printk ("Unimplemented SPARC system call %ld\n",regs->u_regs[1]);
485 #ifdef DEBUG_UNIMP_SYSCALL
492 /* #define DEBUG_SPARC_BREAKPOINT */
494 asmlinkage void sparc_breakpoint(struct pt_regs *regs)
498 if (test_thread_flag(TIF_32BIT)) {
499 regs->tpc &= 0xffffffff;
500 regs->tnpc &= 0xffffffff;
502 #ifdef DEBUG_SPARC_BREAKPOINT
503 printk ("TRAP: Entering kernel PC=%lx, nPC=%lx\n", regs->tpc, regs->tnpc);
505 info.si_signo = SIGTRAP;
507 info.si_code = TRAP_BRKPT;
508 info.si_addr = (void __user *)regs->tpc;
510 force_sig_info(SIGTRAP, &info, current);
511 #ifdef DEBUG_SPARC_BREAKPOINT
512 printk ("TRAP: Returning to space: PC=%lx nPC=%lx\n", regs->tpc, regs->tnpc);
516 extern void check_pending(int signum);
518 asmlinkage long sys_getdomainname(char __user *name, int len)
525 nlen = strlen(system_utsname.domainname) + 1;
529 if (len > __NEW_UTS_LEN)
531 if (copy_to_user(name, system_utsname.domainname, len))
539 asmlinkage long solaris_syscall(struct pt_regs *regs)
543 regs->tpc = regs->tnpc;
545 if (test_thread_flag(TIF_32BIT)) {
546 regs->tpc &= 0xffffffff;
547 regs->tnpc &= 0xffffffff;
550 printk ("For Solaris binary emulation you need solaris module loaded\n");
553 send_sig(SIGSEGV, current, 1);
558 #ifndef CONFIG_SUNOS_EMUL
559 asmlinkage long sunos_syscall(struct pt_regs *regs)
563 regs->tpc = regs->tnpc;
565 if (test_thread_flag(TIF_32BIT)) {
566 regs->tpc &= 0xffffffff;
567 regs->tnpc &= 0xffffffff;
570 printk ("SunOS binary emulation not compiled in\n");
571 force_sig(SIGSEGV, current);
577 asmlinkage long sys_utrap_install(utrap_entry_t type,
578 utrap_handler_t new_p,
579 utrap_handler_t new_d,
580 utrap_handler_t __user *old_p,
581 utrap_handler_t __user *old_d)
583 if (type < UT_INSTRUCTION_EXCEPTION || type > UT_TRAP_INSTRUCTION_31)
585 if (new_p == (utrap_handler_t)(long)UTH_NOCHANGE) {
587 if (!current_thread_info()->utraps) {
588 if (put_user(NULL, old_p))
591 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
596 if (put_user(NULL, old_d))
601 if (!current_thread_info()->utraps) {
602 current_thread_info()->utraps =
603 kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long), GFP_KERNEL);
604 if (!current_thread_info()->utraps)
606 current_thread_info()->utraps[0] = 1;
607 memset(current_thread_info()->utraps+1, 0,
608 UT_TRAP_INSTRUCTION_31*sizeof(long));
610 if ((utrap_handler_t)current_thread_info()->utraps[type] != new_p &&
611 current_thread_info()->utraps[0] > 1) {
612 long *p = current_thread_info()->utraps;
614 current_thread_info()->utraps =
615 kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long),
617 if (!current_thread_info()->utraps) {
618 current_thread_info()->utraps = p;
622 current_thread_info()->utraps[0] = 1;
623 memcpy(current_thread_info()->utraps+1, p+1,
624 UT_TRAP_INSTRUCTION_31*sizeof(long));
628 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
632 if (put_user(NULL, old_d))
635 current_thread_info()->utraps[type] = (long)new_p;
640 long sparc_memory_ordering(unsigned long model, struct pt_regs *regs)
644 regs->tstate = (regs->tstate & ~TSTATE_MM) | (model << 14);
648 asmlinkage long sys_rt_sigaction(int sig,
649 const struct sigaction __user *act,
650 struct sigaction __user *oact,
651 void __user *restorer,
654 struct k_sigaction new_ka, old_ka;
657 /* XXX: Don't preclude handling different sized sigset_t's. */
658 if (sigsetsize != sizeof(sigset_t))
662 new_ka.ka_restorer = restorer;
663 if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
667 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
670 if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
677 /* Invoked by rtrap code to update performance counters in
680 asmlinkage void update_perfctrs(void)
682 unsigned long pic, tmp;
685 tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
686 __put_user(tmp, current_thread_info()->user_cntd0);
687 tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
688 __put_user(tmp, current_thread_info()->user_cntd1);
692 asmlinkage long sys_perfctr(int opcode, unsigned long arg0, unsigned long arg1, unsigned long arg2)
698 current_thread_info()->pcr_reg = arg2;
699 current_thread_info()->user_cntd0 = (u64 __user *) arg0;
700 current_thread_info()->user_cntd1 = (u64 __user *) arg1;
701 current_thread_info()->kernel_cntd0 =
702 current_thread_info()->kernel_cntd1 = 0;
705 set_thread_flag(TIF_PERFCTR);
710 if (test_thread_flag(TIF_PERFCTR)) {
711 current_thread_info()->user_cntd0 =
712 current_thread_info()->user_cntd1 = NULL;
713 current_thread_info()->pcr_reg = 0;
715 clear_thread_flag(TIF_PERFCTR);
721 unsigned long pic, tmp;
723 if (!test_thread_flag(TIF_PERFCTR)) {
728 tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
729 err |= __put_user(tmp, current_thread_info()->user_cntd0);
730 tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
731 err |= __put_user(tmp, current_thread_info()->user_cntd1);
737 if (!test_thread_flag(TIF_PERFCTR)) {
741 current_thread_info()->kernel_cntd0 =
742 current_thread_info()->kernel_cntd1 = 0;
746 case PERFCTR_SETPCR: {
747 u64 __user *user_pcr = (u64 __user *)arg0;
749 if (!test_thread_flag(TIF_PERFCTR)) {
753 err |= __get_user(current_thread_info()->pcr_reg, user_pcr);
754 write_pcr(current_thread_info()->pcr_reg);
755 current_thread_info()->kernel_cntd0 =
756 current_thread_info()->kernel_cntd1 = 0;
761 case PERFCTR_GETPCR: {
762 u64 __user *user_pcr = (u64 __user *)arg0;
764 if (!test_thread_flag(TIF_PERFCTR)) {
768 err |= __put_user(current_thread_info()->pcr_reg, user_pcr);