file ->get_unmapped_area() shouldn't duplicate work of get_unmapped_area()
[linux-2.6.git] / arch / sparc / kernel / sys_sparc_64.c
1 /* linux/arch/sparc64/kernel/sys_sparc.c
2  *
3  * This file contains various random system calls that
4  * have a non-standard calling sequence on the Linux/sparc
5  * platform.
6  */
7
8 #include <linux/errno.h>
9 #include <linux/types.h>
10 #include <linux/sched.h>
11 #include <linux/fs.h>
12 #include <linux/file.h>
13 #include <linux/mm.h>
14 #include <linux/sem.h>
15 #include <linux/msg.h>
16 #include <linux/shm.h>
17 #include <linux/stat.h>
18 #include <linux/mman.h>
19 #include <linux/utsname.h>
20 #include <linux/smp.h>
21 #include <linux/slab.h>
22 #include <linux/syscalls.h>
23 #include <linux/ipc.h>
24 #include <linux/personality.h>
25 #include <linux/random.h>
26 #include <linux/module.h>
27
28 #include <asm/uaccess.h>
29 #include <asm/utrap.h>
30 #include <asm/perfctr.h>
31 #include <asm/unistd.h>
32
33 #include "entry.h"
34 #include "systbls.h"
35
36 /* #define DEBUG_UNIMP_SYSCALL */
37
38 asmlinkage unsigned long sys_getpagesize(void)
39 {
40         return PAGE_SIZE;
41 }
42
43 #define VA_EXCLUDE_START (0x0000080000000000UL - (1UL << 32UL))
44 #define VA_EXCLUDE_END   (0xfffff80000000000UL + (1UL << 32UL))
45
46 /* Does addr --> addr+len fall within 4GB of the VA-space hole or
47  * overflow past the end of the 64-bit address space?
48  */
49 static inline int invalid_64bit_range(unsigned long addr, unsigned long len)
50 {
51         unsigned long va_exclude_start, va_exclude_end;
52
53         va_exclude_start = VA_EXCLUDE_START;
54         va_exclude_end   = VA_EXCLUDE_END;
55
56         if (unlikely(len >= va_exclude_start))
57                 return 1;
58
59         if (unlikely((addr + len) < addr))
60                 return 1;
61
62         if (unlikely((addr >= va_exclude_start && addr < va_exclude_end) ||
63                      ((addr + len) >= va_exclude_start &&
64                       (addr + len) < va_exclude_end)))
65                 return 1;
66
67         return 0;
68 }
69
70 /* Does start,end straddle the VA-space hole?  */
71 static inline int straddles_64bit_va_hole(unsigned long start, unsigned long end)
72 {
73         unsigned long va_exclude_start, va_exclude_end;
74
75         va_exclude_start = VA_EXCLUDE_START;
76         va_exclude_end   = VA_EXCLUDE_END;
77
78         if (likely(start < va_exclude_start && end < va_exclude_start))
79                 return 0;
80
81         if (likely(start >= va_exclude_end && end >= va_exclude_end))
82                 return 0;
83
84         return 1;
85 }
86
87 /* These functions differ from the default implementations in
88  * mm/mmap.c in two ways:
89  *
90  * 1) For file backed MAP_SHARED mmap()'s we D-cache color align,
91  *    for fixed such mappings we just validate what the user gave us.
92  * 2) For 64-bit tasks we avoid mapping anything within 4GB of
93  *    the spitfire/niagara VA-hole.
94  */
95
96 static inline unsigned long COLOUR_ALIGN(unsigned long addr,
97                                          unsigned long pgoff)
98 {
99         unsigned long base = (addr+SHMLBA-1)&~(SHMLBA-1);
100         unsigned long off = (pgoff<<PAGE_SHIFT) & (SHMLBA-1);
101
102         return base + off;
103 }
104
105 static inline unsigned long COLOUR_ALIGN_DOWN(unsigned long addr,
106                                               unsigned long pgoff)
107 {
108         unsigned long base = addr & ~(SHMLBA-1);
109         unsigned long off = (pgoff<<PAGE_SHIFT) & (SHMLBA-1);
110
111         if (base + off <= addr)
112                 return base + off;
113         return base - off;
114 }
115
116 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)
117 {
118         struct mm_struct *mm = current->mm;
119         struct vm_area_struct * vma;
120         unsigned long task_size = TASK_SIZE;
121         unsigned long start_addr;
122         int do_color_align;
123
124         if (flags & MAP_FIXED) {
125                 /* We do not accept a shared mapping if it would violate
126                  * cache aliasing constraints.
127                  */
128                 if ((flags & MAP_SHARED) &&
129                     ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
130                         return -EINVAL;
131                 return addr;
132         }
133
134         if (test_thread_flag(TIF_32BIT))
135                 task_size = STACK_TOP32;
136         if (unlikely(len > task_size || len >= VA_EXCLUDE_START))
137                 return -ENOMEM;
138
139         do_color_align = 0;
140         if (filp || (flags & MAP_SHARED))
141                 do_color_align = 1;
142
143         if (addr) {
144                 if (do_color_align)
145                         addr = COLOUR_ALIGN(addr, pgoff);
146                 else
147                         addr = PAGE_ALIGN(addr);
148
149                 vma = find_vma(mm, addr);
150                 if (task_size - len >= addr &&
151                     (!vma || addr + len <= vma->vm_start))
152                         return addr;
153         }
154
155         if (len > mm->cached_hole_size) {
156                 start_addr = addr = mm->free_area_cache;
157         } else {
158                 start_addr = addr = TASK_UNMAPPED_BASE;
159                 mm->cached_hole_size = 0;
160         }
161
162         task_size -= len;
163
164 full_search:
165         if (do_color_align)
166                 addr = COLOUR_ALIGN(addr, pgoff);
167         else
168                 addr = PAGE_ALIGN(addr);
169
170         for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
171                 /* At this point:  (!vma || addr < vma->vm_end). */
172                 if (addr < VA_EXCLUDE_START &&
173                     (addr + len) >= VA_EXCLUDE_START) {
174                         addr = VA_EXCLUDE_END;
175                         vma = find_vma(mm, VA_EXCLUDE_END);
176                 }
177                 if (unlikely(task_size < addr)) {
178                         if (start_addr != TASK_UNMAPPED_BASE) {
179                                 start_addr = addr = TASK_UNMAPPED_BASE;
180                                 mm->cached_hole_size = 0;
181                                 goto full_search;
182                         }
183                         return -ENOMEM;
184                 }
185                 if (likely(!vma || addr + len <= vma->vm_start)) {
186                         /*
187                          * Remember the place where we stopped the search:
188                          */
189                         mm->free_area_cache = addr + len;
190                         return addr;
191                 }
192                 if (addr + mm->cached_hole_size < vma->vm_start)
193                         mm->cached_hole_size = vma->vm_start - addr;
194
195                 addr = vma->vm_end;
196                 if (do_color_align)
197                         addr = COLOUR_ALIGN(addr, pgoff);
198         }
199 }
200
201 unsigned long
202 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
203                           const unsigned long len, const unsigned long pgoff,
204                           const unsigned long flags)
205 {
206         struct vm_area_struct *vma;
207         struct mm_struct *mm = current->mm;
208         unsigned long task_size = STACK_TOP32;
209         unsigned long addr = addr0;
210         int do_color_align;
211
212         /* This should only ever run for 32-bit processes.  */
213         BUG_ON(!test_thread_flag(TIF_32BIT));
214
215         if (flags & MAP_FIXED) {
216                 /* We do not accept a shared mapping if it would violate
217                  * cache aliasing constraints.
218                  */
219                 if ((flags & MAP_SHARED) &&
220                     ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
221                         return -EINVAL;
222                 return addr;
223         }
224
225         if (unlikely(len > task_size))
226                 return -ENOMEM;
227
228         do_color_align = 0;
229         if (filp || (flags & MAP_SHARED))
230                 do_color_align = 1;
231
232         /* requesting a specific address */
233         if (addr) {
234                 if (do_color_align)
235                         addr = COLOUR_ALIGN(addr, pgoff);
236                 else
237                         addr = PAGE_ALIGN(addr);
238
239                 vma = find_vma(mm, addr);
240                 if (task_size - len >= addr &&
241                     (!vma || addr + len <= vma->vm_start))
242                         return addr;
243         }
244
245         /* check if free_area_cache is useful for us */
246         if (len <= mm->cached_hole_size) {
247                 mm->cached_hole_size = 0;
248                 mm->free_area_cache = mm->mmap_base;
249         }
250
251         /* either no address requested or can't fit in requested address hole */
252         addr = mm->free_area_cache;
253         if (do_color_align) {
254                 unsigned long base = COLOUR_ALIGN_DOWN(addr-len, pgoff);
255
256                 addr = base + len;
257         }
258
259         /* make sure it can fit in the remaining address space */
260         if (likely(addr > len)) {
261                 vma = find_vma(mm, addr-len);
262                 if (!vma || addr <= vma->vm_start) {
263                         /* remember the address as a hint for next time */
264                         return (mm->free_area_cache = addr-len);
265                 }
266         }
267
268         if (unlikely(mm->mmap_base < len))
269                 goto bottomup;
270
271         addr = mm->mmap_base-len;
272         if (do_color_align)
273                 addr = COLOUR_ALIGN_DOWN(addr, pgoff);
274
275         do {
276                 /*
277                  * Lookup failure means no vma is above this address,
278                  * else if new region fits below vma->vm_start,
279                  * return with success:
280                  */
281                 vma = find_vma(mm, addr);
282                 if (likely(!vma || addr+len <= vma->vm_start)) {
283                         /* remember the address as a hint for next time */
284                         return (mm->free_area_cache = addr);
285                 }
286
287                 /* remember the largest hole we saw so far */
288                 if (addr + mm->cached_hole_size < vma->vm_start)
289                         mm->cached_hole_size = vma->vm_start - addr;
290
291                 /* try just below the current vma->vm_start */
292                 addr = vma->vm_start-len;
293                 if (do_color_align)
294                         addr = COLOUR_ALIGN_DOWN(addr, pgoff);
295         } while (likely(len < vma->vm_start));
296
297 bottomup:
298         /*
299          * A failed mmap() very likely causes application failure,
300          * so fall back to the bottom-up function here. This scenario
301          * can happen with large stack limits and large mmap()
302          * allocations.
303          */
304         mm->cached_hole_size = ~0UL;
305         mm->free_area_cache = TASK_UNMAPPED_BASE;
306         addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
307         /*
308          * Restore the topdown base:
309          */
310         mm->free_area_cache = mm->mmap_base;
311         mm->cached_hole_size = ~0UL;
312
313         return addr;
314 }
315
316 /* Try to align mapping such that we align it as much as possible. */
317 unsigned long get_fb_unmapped_area(struct file *filp, unsigned long orig_addr, unsigned long len, unsigned long pgoff, unsigned long flags)
318 {
319         unsigned long align_goal, addr = -ENOMEM;
320         unsigned long (*get_area)(struct file *, unsigned long,
321                                   unsigned long, unsigned long, unsigned long);
322
323         get_area = current->mm->get_unmapped_area;
324
325         if (flags & MAP_FIXED) {
326                 /* Ok, don't mess with it. */
327                 return get_area(NULL, orig_addr, len, pgoff, flags);
328         }
329         flags &= ~MAP_SHARED;
330
331         align_goal = PAGE_SIZE;
332         if (len >= (4UL * 1024 * 1024))
333                 align_goal = (4UL * 1024 * 1024);
334         else if (len >= (512UL * 1024))
335                 align_goal = (512UL * 1024);
336         else if (len >= (64UL * 1024))
337                 align_goal = (64UL * 1024);
338
339         do {
340                 addr = get_area(NULL, orig_addr, len + (align_goal - PAGE_SIZE), pgoff, flags);
341                 if (!(addr & ~PAGE_MASK)) {
342                         addr = (addr + (align_goal - 1UL)) & ~(align_goal - 1UL);
343                         break;
344                 }
345
346                 if (align_goal == (4UL * 1024 * 1024))
347                         align_goal = (512UL * 1024);
348                 else if (align_goal == (512UL * 1024))
349                         align_goal = (64UL * 1024);
350                 else
351                         align_goal = PAGE_SIZE;
352         } while ((addr & ~PAGE_MASK) && align_goal > PAGE_SIZE);
353
354         /* Mapping is smaller than 64K or larger areas could not
355          * be obtained.
356          */
357         if (addr & ~PAGE_MASK)
358                 addr = get_area(NULL, orig_addr, len, pgoff, flags);
359
360         return addr;
361 }
362 EXPORT_SYMBOL(get_fb_unmapped_area);
363
364 /* Essentially the same as PowerPC... */
365 void arch_pick_mmap_layout(struct mm_struct *mm)
366 {
367         unsigned long random_factor = 0UL;
368
369         if (current->flags & PF_RANDOMIZE) {
370                 random_factor = get_random_int();
371                 if (test_thread_flag(TIF_32BIT))
372                         random_factor &= ((1 * 1024 * 1024) - 1);
373                 else
374                         random_factor = ((random_factor << PAGE_SHIFT) &
375                                          0xffffffffUL);
376         }
377
378         /*
379          * Fall back to the standard layout if the personality
380          * bit is set, or if the expected stack growth is unlimited:
381          */
382         if (!test_thread_flag(TIF_32BIT) ||
383             (current->personality & ADDR_COMPAT_LAYOUT) ||
384             current->signal->rlim[RLIMIT_STACK].rlim_cur == RLIM_INFINITY ||
385             sysctl_legacy_va_layout) {
386                 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
387                 mm->get_unmapped_area = arch_get_unmapped_area;
388                 mm->unmap_area = arch_unmap_area;
389         } else {
390                 /* We know it's 32-bit */
391                 unsigned long task_size = STACK_TOP32;
392                 unsigned long gap;
393
394                 gap = current->signal->rlim[RLIMIT_STACK].rlim_cur;
395                 if (gap < 128 * 1024 * 1024)
396                         gap = 128 * 1024 * 1024;
397                 if (gap > (task_size / 6 * 5))
398                         gap = (task_size / 6 * 5);
399
400                 mm->mmap_base = PAGE_ALIGN(task_size - gap - random_factor);
401                 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
402                 mm->unmap_area = arch_unmap_area_topdown;
403         }
404 }
405
406 SYSCALL_DEFINE1(sparc_brk, unsigned long, brk)
407 {
408         /* People could try to be nasty and use ta 0x6d in 32bit programs */
409         if (test_thread_flag(TIF_32BIT) && brk >= STACK_TOP32)
410                 return current->mm->brk;
411
412         if (unlikely(straddles_64bit_va_hole(current->mm->brk, brk)))
413                 return current->mm->brk;
414
415         return sys_brk(brk);
416 }
417                                                                 
418 /*
419  * sys_pipe() is the normal C calling standard for creating
420  * a pipe. It's not the way unix traditionally does this, though.
421  */
422 SYSCALL_DEFINE1(sparc_pipe_real, struct pt_regs *, regs)
423 {
424         int fd[2];
425         int error;
426
427         error = do_pipe_flags(fd, 0);
428         if (error)
429                 goto out;
430         regs->u_regs[UREG_I1] = fd[1];
431         error = fd[0];
432 out:
433         return error;
434 }
435
436 /*
437  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
438  *
439  * This is really horribly ugly.
440  */
441
442 SYSCALL_DEFINE6(ipc, unsigned int, call, int, first, unsigned long, second,
443                 unsigned long, third, void __user *, ptr, long, fifth)
444 {
445         long err;
446
447         /* No need for backward compatibility. We can start fresh... */
448         if (call <= SEMCTL) {
449                 switch (call) {
450                 case SEMOP:
451                         err = sys_semtimedop(first, ptr,
452                                              (unsigned)second, NULL);
453                         goto out;
454                 case SEMTIMEDOP:
455                         err = sys_semtimedop(first, ptr, (unsigned)second,
456                                 (const struct timespec __user *)
457                                              (unsigned long) fifth);
458                         goto out;
459                 case SEMGET:
460                         err = sys_semget(first, (int)second, (int)third);
461                         goto out;
462                 case SEMCTL: {
463                         err = sys_semctl(first, second,
464                                          (int)third | IPC_64,
465                                          (union semun) ptr);
466                         goto out;
467                 }
468                 default:
469                         err = -ENOSYS;
470                         goto out;
471                 };
472         }
473         if (call <= MSGCTL) {
474                 switch (call) {
475                 case MSGSND:
476                         err = sys_msgsnd(first, ptr, (size_t)second,
477                                          (int)third);
478                         goto out;
479                 case MSGRCV:
480                         err = sys_msgrcv(first, ptr, (size_t)second, fifth,
481                                          (int)third);
482                         goto out;
483                 case MSGGET:
484                         err = sys_msgget((key_t)first, (int)second);
485                         goto out;
486                 case MSGCTL:
487                         err = sys_msgctl(first, (int)second | IPC_64, ptr);
488                         goto out;
489                 default:
490                         err = -ENOSYS;
491                         goto out;
492                 };
493         }
494         if (call <= SHMCTL) {
495                 switch (call) {
496                 case SHMAT: {
497                         ulong raddr;
498                         err = do_shmat(first, ptr, (int)second, &raddr);
499                         if (!err) {
500                                 if (put_user(raddr,
501                                              (ulong __user *) third))
502                                         err = -EFAULT;
503                         }
504                         goto out;
505                 }
506                 case SHMDT:
507                         err = sys_shmdt(ptr);
508                         goto out;
509                 case SHMGET:
510                         err = sys_shmget(first, (size_t)second, (int)third);
511                         goto out;
512                 case SHMCTL:
513                         err = sys_shmctl(first, (int)second | IPC_64, ptr);
514                         goto out;
515                 default:
516                         err = -ENOSYS;
517                         goto out;
518                 };
519         } else {
520                 err = -ENOSYS;
521         }
522 out:
523         return err;
524 }
525
526 SYSCALL_DEFINE1(sparc64_newuname, struct new_utsname __user *, name)
527 {
528         int ret = sys_newuname(name);
529         
530         if (current->personality == PER_LINUX32 && !ret) {
531                 ret = (copy_to_user(name->machine, "sparc\0\0", 8)
532                        ? -EFAULT : 0);
533         }
534         return ret;
535 }
536
537 SYSCALL_DEFINE1(sparc64_personality, unsigned long, personality)
538 {
539         int ret;
540
541         if (current->personality == PER_LINUX32 &&
542             personality == PER_LINUX)
543                 personality = PER_LINUX32;
544         ret = sys_personality(personality);
545         if (ret == PER_LINUX32)
546                 ret = PER_LINUX;
547
548         return ret;
549 }
550
551 int sparc_mmap_check(unsigned long addr, unsigned long len)
552 {
553         if (test_thread_flag(TIF_32BIT)) {
554                 if (len >= STACK_TOP32)
555                         return -EINVAL;
556
557                 if (addr > STACK_TOP32 - len)
558                         return -EINVAL;
559         } else {
560                 if (len >= VA_EXCLUDE_START)
561                         return -EINVAL;
562
563                 if (invalid_64bit_range(addr, len))
564                         return -EINVAL;
565         }
566
567         return 0;
568 }
569
570 /* Linux version of mmap */
571 SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len,
572                 unsigned long, prot, unsigned long, flags, unsigned long, fd,
573                 unsigned long, off)
574 {
575         struct file * file = NULL;
576         unsigned long retval = -EBADF;
577
578         if (!(flags & MAP_ANONYMOUS)) {
579                 file = fget(fd);
580                 if (!file)
581                         goto out;
582         }
583         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
584         len = PAGE_ALIGN(len);
585
586         down_write(&current->mm->mmap_sem);
587         retval = do_mmap(file, addr, len, prot, flags, off);
588         up_write(&current->mm->mmap_sem);
589
590         if (file)
591                 fput(file);
592 out:
593         return retval;
594 }
595
596 SYSCALL_DEFINE2(64_munmap, unsigned long, addr, size_t, len)
597 {
598         long ret;
599
600         if (invalid_64bit_range(addr, len))
601                 return -EINVAL;
602
603         down_write(&current->mm->mmap_sem);
604         ret = do_munmap(current->mm, addr, len);
605         up_write(&current->mm->mmap_sem);
606         return ret;
607 }
608
609 extern unsigned long do_mremap(unsigned long addr,
610         unsigned long old_len, unsigned long new_len,
611         unsigned long flags, unsigned long new_addr);
612                 
613 SYSCALL_DEFINE5(64_mremap, unsigned long, addr, unsigned long, old_len,
614                 unsigned long, new_len, unsigned long, flags,
615                 unsigned long, new_addr)
616 {
617         unsigned long ret = -EINVAL;
618
619         if (test_thread_flag(TIF_32BIT))
620                 goto out;
621
622         down_write(&current->mm->mmap_sem);
623         ret = do_mremap(addr, old_len, new_len, flags, new_addr);
624         up_write(&current->mm->mmap_sem);
625 out:
626         return ret;       
627 }
628
629 /* we come to here via sys_nis_syscall so it can setup the regs argument */
630 asmlinkage unsigned long c_sys_nis_syscall(struct pt_regs *regs)
631 {
632         static int count;
633         
634         /* Don't make the system unusable, if someone goes stuck */
635         if (count++ > 5)
636                 return -ENOSYS;
637
638         printk ("Unimplemented SPARC system call %ld\n",regs->u_regs[1]);
639 #ifdef DEBUG_UNIMP_SYSCALL      
640         show_regs (regs);
641 #endif
642
643         return -ENOSYS;
644 }
645
646 /* #define DEBUG_SPARC_BREAKPOINT */
647
648 asmlinkage void sparc_breakpoint(struct pt_regs *regs)
649 {
650         siginfo_t info;
651
652         if (test_thread_flag(TIF_32BIT)) {
653                 regs->tpc &= 0xffffffff;
654                 regs->tnpc &= 0xffffffff;
655         }
656 #ifdef DEBUG_SPARC_BREAKPOINT
657         printk ("TRAP: Entering kernel PC=%lx, nPC=%lx\n", regs->tpc, regs->tnpc);
658 #endif
659         info.si_signo = SIGTRAP;
660         info.si_errno = 0;
661         info.si_code = TRAP_BRKPT;
662         info.si_addr = (void __user *)regs->tpc;
663         info.si_trapno = 0;
664         force_sig_info(SIGTRAP, &info, current);
665 #ifdef DEBUG_SPARC_BREAKPOINT
666         printk ("TRAP: Returning to space: PC=%lx nPC=%lx\n", regs->tpc, regs->tnpc);
667 #endif
668 }
669
670 extern void check_pending(int signum);
671
672 SYSCALL_DEFINE2(getdomainname, char __user *, name, int, len)
673 {
674         int nlen, err;
675
676         if (len < 0)
677                 return -EINVAL;
678
679         down_read(&uts_sem);
680         
681         nlen = strlen(utsname()->domainname) + 1;
682         err = -EINVAL;
683         if (nlen > len)
684                 goto out;
685
686         err = -EFAULT;
687         if (!copy_to_user(name, utsname()->domainname, nlen))
688                 err = 0;
689
690 out:
691         up_read(&uts_sem);
692         return err;
693 }
694
695 SYSCALL_DEFINE5(utrap_install, utrap_entry_t, type,
696                 utrap_handler_t, new_p, utrap_handler_t, new_d,
697                 utrap_handler_t __user *, old_p,
698                 utrap_handler_t __user *, old_d)
699 {
700         if (type < UT_INSTRUCTION_EXCEPTION || type > UT_TRAP_INSTRUCTION_31)
701                 return -EINVAL;
702         if (new_p == (utrap_handler_t)(long)UTH_NOCHANGE) {
703                 if (old_p) {
704                         if (!current_thread_info()->utraps) {
705                                 if (put_user(NULL, old_p))
706                                         return -EFAULT;
707                         } else {
708                                 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
709                                         return -EFAULT;
710                         }
711                 }
712                 if (old_d) {
713                         if (put_user(NULL, old_d))
714                                 return -EFAULT;
715                 }
716                 return 0;
717         }
718         if (!current_thread_info()->utraps) {
719                 current_thread_info()->utraps =
720                         kzalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long), GFP_KERNEL);
721                 if (!current_thread_info()->utraps)
722                         return -ENOMEM;
723                 current_thread_info()->utraps[0] = 1;
724         } else {
725                 if ((utrap_handler_t)current_thread_info()->utraps[type] != new_p &&
726                     current_thread_info()->utraps[0] > 1) {
727                         unsigned long *p = current_thread_info()->utraps;
728
729                         current_thread_info()->utraps =
730                                 kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long),
731                                         GFP_KERNEL);
732                         if (!current_thread_info()->utraps) {
733                                 current_thread_info()->utraps = p;
734                                 return -ENOMEM;
735                         }
736                         p[0]--;
737                         current_thread_info()->utraps[0] = 1;
738                         memcpy(current_thread_info()->utraps+1, p+1,
739                                UT_TRAP_INSTRUCTION_31*sizeof(long));
740                 }
741         }
742         if (old_p) {
743                 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
744                         return -EFAULT;
745         }
746         if (old_d) {
747                 if (put_user(NULL, old_d))
748                         return -EFAULT;
749         }
750         current_thread_info()->utraps[type] = (long)new_p;
751
752         return 0;
753 }
754
755 asmlinkage long sparc_memory_ordering(unsigned long model,
756                                       struct pt_regs *regs)
757 {
758         if (model >= 3)
759                 return -EINVAL;
760         regs->tstate = (regs->tstate & ~TSTATE_MM) | (model << 14);
761         return 0;
762 }
763
764 SYSCALL_DEFINE5(rt_sigaction, int, sig, const struct sigaction __user *, act,
765                 struct sigaction __user *, oact, void __user *, restorer,
766                 size_t, sigsetsize)
767 {
768         struct k_sigaction new_ka, old_ka;
769         int ret;
770
771         /* XXX: Don't preclude handling different sized sigset_t's.  */
772         if (sigsetsize != sizeof(sigset_t))
773                 return -EINVAL;
774
775         if (act) {
776                 new_ka.ka_restorer = restorer;
777                 if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
778                         return -EFAULT;
779         }
780
781         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
782
783         if (!ret && oact) {
784                 if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
785                         return -EFAULT;
786         }
787
788         return ret;
789 }
790
791 /* Invoked by rtrap code to update performance counters in
792  * user space.
793  */
794 asmlinkage void update_perfctrs(void)
795 {
796         unsigned long pic, tmp;
797
798         read_pic(pic);
799         tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
800         __put_user(tmp, current_thread_info()->user_cntd0);
801         tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
802         __put_user(tmp, current_thread_info()->user_cntd1);
803         reset_pic();
804 }
805
806 SYSCALL_DEFINE4(perfctr, int, opcode, unsigned long, arg0,
807                 unsigned long, arg1, unsigned long, arg2)
808 {
809         int err = 0;
810
811         switch(opcode) {
812         case PERFCTR_ON:
813                 current_thread_info()->pcr_reg = arg2;
814                 current_thread_info()->user_cntd0 = (u64 __user *) arg0;
815                 current_thread_info()->user_cntd1 = (u64 __user *) arg1;
816                 current_thread_info()->kernel_cntd0 =
817                         current_thread_info()->kernel_cntd1 = 0;
818                 write_pcr(arg2);
819                 reset_pic();
820                 set_thread_flag(TIF_PERFCTR);
821                 break;
822
823         case PERFCTR_OFF:
824                 err = -EINVAL;
825                 if (test_thread_flag(TIF_PERFCTR)) {
826                         current_thread_info()->user_cntd0 =
827                                 current_thread_info()->user_cntd1 = NULL;
828                         current_thread_info()->pcr_reg = 0;
829                         write_pcr(0);
830                         clear_thread_flag(TIF_PERFCTR);
831                         err = 0;
832                 }
833                 break;
834
835         case PERFCTR_READ: {
836                 unsigned long pic, tmp;
837
838                 if (!test_thread_flag(TIF_PERFCTR)) {
839                         err = -EINVAL;
840                         break;
841                 }
842                 read_pic(pic);
843                 tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
844                 err |= __put_user(tmp, current_thread_info()->user_cntd0);
845                 tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
846                 err |= __put_user(tmp, current_thread_info()->user_cntd1);
847                 reset_pic();
848                 break;
849         }
850
851         case PERFCTR_CLRPIC:
852                 if (!test_thread_flag(TIF_PERFCTR)) {
853                         err = -EINVAL;
854                         break;
855                 }
856                 current_thread_info()->kernel_cntd0 =
857                         current_thread_info()->kernel_cntd1 = 0;
858                 reset_pic();
859                 break;
860
861         case PERFCTR_SETPCR: {
862                 u64 __user *user_pcr = (u64 __user *)arg0;
863
864                 if (!test_thread_flag(TIF_PERFCTR)) {
865                         err = -EINVAL;
866                         break;
867                 }
868                 err |= __get_user(current_thread_info()->pcr_reg, user_pcr);
869                 write_pcr(current_thread_info()->pcr_reg);
870                 current_thread_info()->kernel_cntd0 =
871                         current_thread_info()->kernel_cntd1 = 0;
872                 reset_pic();
873                 break;
874         }
875
876         case PERFCTR_GETPCR: {
877                 u64 __user *user_pcr = (u64 __user *)arg0;
878
879                 if (!test_thread_flag(TIF_PERFCTR)) {
880                         err = -EINVAL;
881                         break;
882                 }
883                 err |= __put_user(current_thread_info()->pcr_reg, user_pcr);
884                 break;
885         }
886
887         default:
888                 err = -EINVAL;
889                 break;
890         };
891         return err;
892 }
893
894 /*
895  * Do a system call from kernel instead of calling sys_execve so we
896  * end up with proper pt_regs.
897  */
898 int kernel_execve(const char *filename, char *const argv[], char *const envp[])
899 {
900         long __res;
901         register long __g1 __asm__ ("g1") = __NR_execve;
902         register long __o0 __asm__ ("o0") = (long)(filename);
903         register long __o1 __asm__ ("o1") = (long)(argv);
904         register long __o2 __asm__ ("o2") = (long)(envp);
905         asm volatile ("t 0x6d\n\t"
906                       "sub %%g0, %%o0, %0\n\t"
907                       "movcc %%xcc, %%o0, %0\n\t"
908                       : "=r" (__res), "=&r" (__o0)
909                       : "1" (__o0), "r" (__o1), "r" (__o2), "r" (__g1)
910                       : "cc");
911         return __res;
912 }