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>
28 #include <linux/vs_cvirt.h>
30 #include <asm/uaccess.h>
32 #include <asm/utrap.h>
33 #include <asm/perfctr.h>
35 /* #define DEBUG_UNIMP_SYSCALL */
37 /* XXX Make this per-binary type, this way we can detect the type of
38 * XXX a binary. Every Sparc executable calls this very early on.
40 asmlinkage unsigned long sys_getpagesize(void)
45 #define COLOUR_ALIGN(addr,pgoff) \
46 ((((addr)+SHMLBA-1)&~(SHMLBA-1)) + \
47 (((pgoff)<<PAGE_SHIFT) & (SHMLBA-1)))
49 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)
51 struct mm_struct *mm = current->mm;
52 struct vm_area_struct * vma;
53 unsigned long task_size = TASK_SIZE;
54 unsigned long start_addr;
57 if (flags & MAP_FIXED) {
58 /* We do not accept a shared mapping if it would violate
59 * cache aliasing constraints.
61 if ((flags & MAP_SHARED) &&
62 ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
67 if (test_thread_flag(TIF_32BIT))
68 task_size = 0xf0000000UL;
69 if (len > task_size || len > -PAGE_OFFSET)
73 if (filp || (flags & MAP_SHARED))
78 addr = COLOUR_ALIGN(addr, pgoff);
80 addr = PAGE_ALIGN(addr);
82 vma = find_vma(mm, addr);
83 if (task_size - len >= addr &&
84 (!vma || addr + len <= vma->vm_start))
88 start_addr = addr = mm->free_area_cache;
94 addr = COLOUR_ALIGN(addr, pgoff);
96 addr = PAGE_ALIGN(addr);
98 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
99 /* At this point: (!vma || addr < vma->vm_end). */
100 if (addr < PAGE_OFFSET && -PAGE_OFFSET - len < addr) {
102 vma = find_vma(mm, PAGE_OFFSET);
104 if (task_size < addr) {
105 if (start_addr != TASK_UNMAPPED_BASE) {
106 start_addr = addr = TASK_UNMAPPED_BASE;
111 if (!vma || addr + len <= vma->vm_start) {
113 * Remember the place where we stopped the search:
115 mm->free_area_cache = addr + len;
120 addr = COLOUR_ALIGN(addr, pgoff);
124 /* Try to align mapping such that we align it as much as possible. */
125 unsigned long get_fb_unmapped_area(struct file *filp, unsigned long orig_addr, unsigned long len, unsigned long pgoff, unsigned long flags)
127 unsigned long align_goal, addr = -ENOMEM;
129 if (flags & MAP_FIXED) {
130 /* Ok, don't mess with it. */
131 return get_unmapped_area(NULL, addr, len, pgoff, flags);
133 flags &= ~MAP_SHARED;
135 align_goal = PAGE_SIZE;
136 if (len >= (4UL * 1024 * 1024))
137 align_goal = (4UL * 1024 * 1024);
138 else if (len >= (512UL * 1024))
139 align_goal = (512UL * 1024);
140 else if (len >= (64UL * 1024))
141 align_goal = (64UL * 1024);
144 addr = get_unmapped_area(NULL, orig_addr, len + (align_goal - PAGE_SIZE), pgoff, flags);
145 if (!(addr & ~PAGE_MASK)) {
146 addr = (addr + (align_goal - 1UL)) & ~(align_goal - 1UL);
150 if (align_goal == (4UL * 1024 * 1024))
151 align_goal = (512UL * 1024);
152 else if (align_goal == (512UL * 1024))
153 align_goal = (64UL * 1024);
155 align_goal = PAGE_SIZE;
156 } while ((addr & ~PAGE_MASK) && align_goal > PAGE_SIZE);
158 /* Mapping is smaller than 64K or larger areas could not
161 if (addr & ~PAGE_MASK)
162 addr = get_unmapped_area(NULL, orig_addr, len, pgoff, flags);
167 asmlinkage unsigned long sparc_brk(unsigned long brk)
169 /* People could try to be nasty and use ta 0x6d in 32bit programs */
170 if (test_thread_flag(TIF_32BIT) &&
172 return current->mm->brk;
174 if ((current->mm->brk & PAGE_OFFSET) != (brk & PAGE_OFFSET))
175 return current->mm->brk;
180 * sys_pipe() is the normal C calling standard for creating
181 * a pipe. It's not the way unix traditionally does this, though.
183 asmlinkage long sparc_pipe(struct pt_regs *regs)
191 regs->u_regs[UREG_I1] = fd[1];
198 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
200 * This is really horribly ugly.
203 asmlinkage long sys_ipc(unsigned int call, int first, unsigned long second,
204 unsigned long third, void __user *ptr, long fifth)
208 /* No need for backward compatibility. We can start fresh... */
209 if (call <= SEMCTL) {
212 err = sys_semtimedop(first, ptr,
213 (unsigned)second, NULL);
216 err = sys_semtimedop(first, ptr, (unsigned)second,
217 (const struct timespec __user *) fifth);
220 err = sys_semget(first, (int)second, (int)third);
228 if (get_user(fourth.__pad,
229 (void __user * __user *) ptr))
231 err = sys_semctl(first, (int)second | IPC_64,
240 if (call <= MSGCTL) {
243 err = sys_msgsnd(first, ptr, (size_t)second,
247 err = sys_msgrcv(first, ptr, (size_t)second, fifth,
251 err = sys_msgget((key_t)first, (int)second);
254 err = sys_msgctl(first, (int)second | IPC_64, ptr);
261 if (call <= SHMCTL) {
265 err = do_shmat(first, ptr, (int)second, &raddr);
268 (ulong __user *) third))
274 err = sys_shmdt(ptr);
277 err = sys_shmget(first, (size_t)second, (int)third);
280 err = sys_shmctl(first, (int)second | IPC_64, ptr);
293 asmlinkage long sparc64_newuname(struct new_utsname __user *name)
295 int ret = sys_newuname(name);
297 if (current->personality == PER_LINUX32 && !ret) {
298 ret = (copy_to_user(name->machine, "sparc\0\0", 8)
304 asmlinkage long sparc64_personality(unsigned long personality)
308 if (current->personality == PER_LINUX32 &&
309 personality == PER_LINUX)
310 personality = PER_LINUX32;
311 ret = sys_personality(personality);
312 if (ret == PER_LINUX32)
318 /* Linux version of mmap */
319 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
320 unsigned long prot, unsigned long flags, unsigned long fd,
323 struct file * file = NULL;
324 unsigned long retval = -EBADF;
326 if (!(flags & MAP_ANONYMOUS)) {
331 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
332 len = PAGE_ALIGN(len);
335 if (test_thread_flag(TIF_32BIT)) {
336 if (len > 0xf0000000UL ||
337 ((flags & MAP_FIXED) && addr > 0xf0000000UL - len))
340 if (len > -PAGE_OFFSET ||
341 ((flags & MAP_FIXED) &&
342 addr < PAGE_OFFSET && addr + len > -PAGE_OFFSET))
346 down_write(¤t->mm->mmap_sem);
347 retval = do_mmap(file, addr, len, prot, flags, off);
348 up_write(¤t->mm->mmap_sem);
357 asmlinkage long sys64_munmap(unsigned long addr, size_t len)
361 if (len > -PAGE_OFFSET ||
362 (addr < PAGE_OFFSET && addr + len > -PAGE_OFFSET))
364 down_write(¤t->mm->mmap_sem);
365 ret = do_munmap(current->mm, addr, len);
366 up_write(¤t->mm->mmap_sem);
370 extern unsigned long do_mremap(unsigned long addr,
371 unsigned long old_len, unsigned long new_len,
372 unsigned long flags, unsigned long new_addr);
374 asmlinkage unsigned long sys64_mremap(unsigned long addr,
375 unsigned long old_len, unsigned long new_len,
376 unsigned long flags, unsigned long new_addr)
378 struct vm_area_struct *vma;
379 unsigned long ret = -EINVAL;
380 if (test_thread_flag(TIF_32BIT))
382 if (old_len > -PAGE_OFFSET || new_len > -PAGE_OFFSET)
384 if (addr < PAGE_OFFSET && addr + old_len > -PAGE_OFFSET)
386 down_write(¤t->mm->mmap_sem);
387 if (flags & MREMAP_FIXED) {
388 if (new_addr < PAGE_OFFSET &&
389 new_addr + new_len > -PAGE_OFFSET)
391 } else if (addr < PAGE_OFFSET && addr + new_len > -PAGE_OFFSET) {
392 unsigned long map_flags = 0;
393 struct file *file = NULL;
396 if (!(flags & MREMAP_MAYMOVE))
399 vma = find_vma(current->mm, addr);
401 if (vma->vm_flags & VM_SHARED)
402 map_flags |= MAP_SHARED;
406 /* MREMAP_FIXED checked above. */
407 new_addr = get_unmapped_area(file, addr, new_len,
408 vma ? vma->vm_pgoff : 0,
411 if (new_addr & ~PAGE_MASK)
413 flags |= MREMAP_FIXED;
415 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
417 up_write(¤t->mm->mmap_sem);
422 /* we come to here via sys_nis_syscall so it can setup the regs argument */
423 asmlinkage unsigned long c_sys_nis_syscall(struct pt_regs *regs)
427 /* Don't make the system unusable, if someone goes stuck */
431 printk ("Unimplemented SPARC system call %ld\n",regs->u_regs[1]);
432 #ifdef DEBUG_UNIMP_SYSCALL
439 /* #define DEBUG_SPARC_BREAKPOINT */
441 asmlinkage void sparc_breakpoint(struct pt_regs *regs)
445 if (test_thread_flag(TIF_32BIT)) {
446 regs->tpc &= 0xffffffff;
447 regs->tnpc &= 0xffffffff;
449 #ifdef DEBUG_SPARC_BREAKPOINT
450 printk ("TRAP: Entering kernel PC=%lx, nPC=%lx\n", regs->tpc, regs->tnpc);
452 info.si_signo = SIGTRAP;
454 info.si_code = TRAP_BRKPT;
455 info.si_addr = (void __user *)regs->tpc;
457 force_sig_info(SIGTRAP, &info, current);
458 #ifdef DEBUG_SPARC_BREAKPOINT
459 printk ("TRAP: Returning to space: PC=%lx nPC=%lx\n", regs->tpc, regs->tnpc);
463 extern void check_pending(int signum);
465 asmlinkage long sys_getdomainname(char __user *name, int len)
472 nlen = strlen(vx_new_uts(domainname)) + 1;
476 if (len > __NEW_UTS_LEN)
478 if (copy_to_user(name, vx_new_uts(domainname), len))
486 asmlinkage long solaris_syscall(struct pt_regs *regs)
490 regs->tpc = regs->tnpc;
492 if (test_thread_flag(TIF_32BIT)) {
493 regs->tpc &= 0xffffffff;
494 regs->tnpc &= 0xffffffff;
497 printk ("For Solaris binary emulation you need solaris module loaded\n");
500 send_sig(SIGSEGV, current, 1);
505 #ifndef CONFIG_SUNOS_EMUL
506 asmlinkage long sunos_syscall(struct pt_regs *regs)
510 regs->tpc = regs->tnpc;
512 if (test_thread_flag(TIF_32BIT)) {
513 regs->tpc &= 0xffffffff;
514 regs->tnpc &= 0xffffffff;
517 printk ("SunOS binary emulation not compiled in\n");
518 force_sig(SIGSEGV, current);
524 asmlinkage long sys_utrap_install(utrap_entry_t type,
525 utrap_handler_t new_p,
526 utrap_handler_t new_d,
527 utrap_handler_t __user *old_p,
528 utrap_handler_t __user *old_d)
530 if (type < UT_INSTRUCTION_EXCEPTION || type > UT_TRAP_INSTRUCTION_31)
532 if (new_p == (utrap_handler_t)(long)UTH_NOCHANGE) {
534 if (!current_thread_info()->utraps) {
535 if (put_user(NULL, old_p))
538 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
543 if (put_user(NULL, old_d))
548 if (!current_thread_info()->utraps) {
549 current_thread_info()->utraps =
550 kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long), GFP_KERNEL);
551 if (!current_thread_info()->utraps)
553 current_thread_info()->utraps[0] = 1;
554 memset(current_thread_info()->utraps+1, 0,
555 UT_TRAP_INSTRUCTION_31*sizeof(long));
557 if ((utrap_handler_t)current_thread_info()->utraps[type] != new_p &&
558 current_thread_info()->utraps[0] > 1) {
559 long *p = current_thread_info()->utraps;
561 current_thread_info()->utraps =
562 kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long),
564 if (!current_thread_info()->utraps) {
565 current_thread_info()->utraps = p;
569 current_thread_info()->utraps[0] = 1;
570 memcpy(current_thread_info()->utraps+1, p+1,
571 UT_TRAP_INSTRUCTION_31*sizeof(long));
575 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
579 if (put_user(NULL, old_d))
582 current_thread_info()->utraps[type] = (long)new_p;
587 long sparc_memory_ordering(unsigned long model, struct pt_regs *regs)
591 regs->tstate = (regs->tstate & ~TSTATE_MM) | (model << 14);
595 asmlinkage long sys_rt_sigaction(int sig,
596 const struct sigaction __user *act,
597 struct sigaction __user *oact,
598 void __user *restorer,
601 struct k_sigaction new_ka, old_ka;
604 /* XXX: Don't preclude handling different sized sigset_t's. */
605 if (sigsetsize != sizeof(sigset_t))
609 new_ka.ka_restorer = restorer;
610 if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
614 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
617 if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
624 /* Invoked by rtrap code to update performance counters in
627 asmlinkage void update_perfctrs(void)
629 unsigned long pic, tmp;
632 tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
633 __put_user(tmp, current_thread_info()->user_cntd0);
634 tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
635 __put_user(tmp, current_thread_info()->user_cntd1);
639 asmlinkage long sys_perfctr(int opcode, unsigned long arg0, unsigned long arg1, unsigned long arg2)
645 current_thread_info()->pcr_reg = arg2;
646 current_thread_info()->user_cntd0 = (u64 __user *) arg0;
647 current_thread_info()->user_cntd1 = (u64 __user *) arg1;
648 current_thread_info()->kernel_cntd0 =
649 current_thread_info()->kernel_cntd1 = 0;
652 set_thread_flag(TIF_PERFCTR);
657 if (test_thread_flag(TIF_PERFCTR)) {
658 current_thread_info()->user_cntd0 =
659 current_thread_info()->user_cntd1 = NULL;
660 current_thread_info()->pcr_reg = 0;
662 clear_thread_flag(TIF_PERFCTR);
668 unsigned long pic, tmp;
670 if (!test_thread_flag(TIF_PERFCTR)) {
675 tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
676 err |= __put_user(tmp, current_thread_info()->user_cntd0);
677 tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
678 err |= __put_user(tmp, current_thread_info()->user_cntd1);
684 if (!test_thread_flag(TIF_PERFCTR)) {
688 current_thread_info()->kernel_cntd0 =
689 current_thread_info()->kernel_cntd1 = 0;
693 case PERFCTR_SETPCR: {
694 u64 __user *user_pcr = (u64 __user *)arg0;
696 if (!test_thread_flag(TIF_PERFCTR)) {
700 err |= __get_user(current_thread_info()->pcr_reg, user_pcr);
701 write_pcr(current_thread_info()->pcr_reg);
702 current_thread_info()->kernel_cntd0 =
703 current_thread_info()->kernel_cntd1 = 0;
708 case PERFCTR_GETPCR: {
709 u64 __user *user_pcr = (u64 __user *)arg0;
711 if (!test_thread_flag(TIF_PERFCTR)) {
715 err |= __put_user(current_thread_info()->pcr_reg, user_pcr);