[SPARC64]: Top-down address space allocation for 32-bit tasks.
[powerpc.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 asmlinkage unsigned long sys_getpagesize(void)
37 {
38         return PAGE_SIZE;
39 }
40
41 #define VA_EXCLUDE_START (0x0000080000000000UL - (1UL << 32UL))
42 #define VA_EXCLUDE_END   (0xfffff80000000000UL + (1UL << 32UL))
43
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?
46  */
47 static inline int invalid_64bit_range(unsigned long addr, unsigned long len)
48 {
49         unsigned long va_exclude_start, va_exclude_end;
50
51         va_exclude_start = VA_EXCLUDE_START;
52         va_exclude_end   = VA_EXCLUDE_END;
53
54         if (unlikely(len >= va_exclude_start))
55                 return 1;
56
57         if (unlikely((addr + len) < addr))
58                 return 1;
59
60         if (unlikely((addr >= va_exclude_start && addr < va_exclude_end) ||
61                      ((addr + len) >= va_exclude_start &&
62                       (addr + len) < va_exclude_end)))
63                 return 1;
64
65         return 0;
66 }
67
68 /* Does start,end straddle the VA-space hole?  */
69 static inline int straddles_64bit_va_hole(unsigned long start, unsigned long end)
70 {
71         unsigned long va_exclude_start, va_exclude_end;
72
73         va_exclude_start = VA_EXCLUDE_START;
74         va_exclude_end   = VA_EXCLUDE_END;
75
76         if (likely(start < va_exclude_start && end < va_exclude_start))
77                 return 0;
78
79         if (likely(start >= va_exclude_end && end >= va_exclude_end))
80                 return 0;
81
82         return 1;
83 }
84
85 /* These functions differ from the default implementations in
86  * mm/mmap.c in two ways:
87  *
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.
92  */
93
94 static inline unsigned long COLOUR_ALIGN(unsigned long addr,
95                                          unsigned long pgoff)
96 {
97         unsigned long base = (addr+SHMLBA-1)&~(SHMLBA-1);
98         unsigned long off = (pgoff<<PAGE_SHIFT) & (SHMLBA-1);
99
100         return base + off;
101 }
102
103 static inline unsigned long COLOUR_ALIGN_DOWN(unsigned long addr,
104                                               unsigned long pgoff)
105 {
106         unsigned long base = addr & ~(SHMLBA-1);
107         unsigned long off = (pgoff<<PAGE_SHIFT) & (SHMLBA-1);
108
109         if (base + off <= addr)
110                 return base + off;
111         return base - off;
112 }
113
114 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)
115 {
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;
120         int do_color_align;
121
122         if (flags & MAP_FIXED) {
123                 /* We do not accept a shared mapping if it would violate
124                  * cache aliasing constraints.
125                  */
126                 if ((flags & MAP_SHARED) &&
127                     ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
128                         return -EINVAL;
129                 return addr;
130         }
131
132         if (test_thread_flag(TIF_32BIT))
133                 task_size = 0xf0000000UL;
134         if (unlikely(len > task_size || len >= VA_EXCLUDE_START))
135                 return -ENOMEM;
136
137         do_color_align = 0;
138         if (filp || (flags & MAP_SHARED))
139                 do_color_align = 1;
140
141         if (addr) {
142                 if (do_color_align)
143                         addr = COLOUR_ALIGN(addr, pgoff);
144                 else
145                         addr = PAGE_ALIGN(addr);
146
147                 vma = find_vma(mm, addr);
148                 if (task_size - len >= addr &&
149                     (!vma || addr + len <= vma->vm_start))
150                         return addr;
151         }
152
153         if (len > mm->cached_hole_size) {
154                 start_addr = addr = mm->free_area_cache;
155         } else {
156                 start_addr = addr = TASK_UNMAPPED_BASE;
157                 mm->cached_hole_size = 0;
158         }
159
160         task_size -= len;
161
162 full_search:
163         if (do_color_align)
164                 addr = COLOUR_ALIGN(addr, pgoff);
165         else
166                 addr = PAGE_ALIGN(addr);
167
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);
174                 }
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;
179                                 goto full_search;
180                         }
181                         return -ENOMEM;
182                 }
183                 if (likely(!vma || addr + len <= vma->vm_start)) {
184                         /*
185                          * Remember the place where we stopped the search:
186                          */
187                         mm->free_area_cache = addr + len;
188                         return addr;
189                 }
190                 if (addr + mm->cached_hole_size < vma->vm_start)
191                         mm->cached_hole_size = vma->vm_start - addr;
192
193                 addr = vma->vm_end;
194                 if (do_color_align)
195                         addr = COLOUR_ALIGN(addr, pgoff);
196         }
197 }
198
199 unsigned long
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)
203 {
204         struct vm_area_struct *vma;
205         struct mm_struct *mm = current->mm;
206         unsigned long task_size = 0xf0000000UL;
207         unsigned long addr = addr0;
208         int do_color_align;
209
210         /* This should only ever run for 32-bit processes.  */
211         BUG_ON(!test_thread_flag(TIF_32BIT));
212
213         if (flags & MAP_FIXED) {
214                 /* We do not accept a shared mapping if it would violate
215                  * cache aliasing constraints.
216                  */
217                 if ((flags & MAP_SHARED) &&
218                     ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
219                         return -EINVAL;
220                 return addr;
221         }
222
223         if (unlikely(len > task_size))
224                 return -ENOMEM;
225
226         do_color_align = 0;
227         if (filp || (flags & MAP_SHARED))
228                 do_color_align = 1;
229
230         /* requesting a specific address */
231         if (addr) {
232                 if (do_color_align)
233                         addr = COLOUR_ALIGN(addr, pgoff);
234                 else
235                         addr = PAGE_ALIGN(addr);
236
237                 vma = find_vma(mm, addr);
238                 if (task_size - len >= addr &&
239                     (!vma || addr + len <= vma->vm_start))
240                         return addr;
241         }
242
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;
247         }
248
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);
253
254                 addr = base + len;
255         }
256
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);
263                 }
264         }
265
266         if (unlikely(mm->mmap_base < len))
267                 goto bottomup;
268
269         addr = mm->mmap_base-len;
270         if (do_color_align)
271                 addr = COLOUR_ALIGN_DOWN(addr, pgoff);
272
273         do {
274                 /*
275                  * Lookup failure means no vma is above this address,
276                  * else if new region fits below vma->vm_start,
277                  * return with success:
278                  */
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);
283                 }
284
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;
288
289                 /* try just below the current vma->vm_start */
290                 addr = vma->vm_start-len;
291                 if (do_color_align)
292                         addr = COLOUR_ALIGN_DOWN(addr, pgoff);
293         } while (likely(len < vma->vm_start));
294
295 bottomup:
296         /*
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()
300          * allocations.
301          */
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);
305         /*
306          * Restore the topdown base:
307          */
308         mm->free_area_cache = mm->mmap_base;
309         mm->cached_hole_size = ~0UL;
310
311         return addr;
312 }
313
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)
316 {
317         unsigned long align_goal, addr = -ENOMEM;
318
319         if (flags & MAP_FIXED) {
320                 /* Ok, don't mess with it. */
321                 return get_unmapped_area(NULL, addr, len, pgoff, flags);
322         }
323         flags &= ~MAP_SHARED;
324
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);
332
333         do {
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);
337                         break;
338                 }
339
340                 if (align_goal == (4UL * 1024 * 1024))
341                         align_goal = (512UL * 1024);
342                 else if (align_goal == (512UL * 1024))
343                         align_goal = (64UL * 1024);
344                 else
345                         align_goal = PAGE_SIZE;
346         } while ((addr & ~PAGE_MASK) && align_goal > PAGE_SIZE);
347
348         /* Mapping is smaller than 64K or larger areas could not
349          * be obtained.
350          */
351         if (addr & ~PAGE_MASK)
352                 addr = get_unmapped_area(NULL, orig_addr, len, pgoff, flags);
353
354         return addr;
355 }
356
357 /* Essentially the same as PowerPC... */
358 void arch_pick_mmap_layout(struct mm_struct *mm)
359 {
360         /*
361          * Fall back to the standard layout if the personality
362          * bit is set, or if the expected stack growth is unlimited:
363          */
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;
371         } else {
372                 /* We know it's 32-bit */
373                 unsigned long task_size = 0xf0000000UL;
374                 unsigned long gap;
375
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);
381
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;
385         }
386 }
387
388 asmlinkage unsigned long sparc_brk(unsigned long brk)
389 {
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;
393
394         if (unlikely(straddles_64bit_va_hole(current->mm->brk, brk)))
395                 return current->mm->brk;
396
397         return sys_brk(brk);
398 }
399                                                                 
400 /*
401  * sys_pipe() is the normal C calling standard for creating
402  * a pipe. It's not the way unix traditionally does this, though.
403  */
404 asmlinkage long sparc_pipe(struct pt_regs *regs)
405 {
406         int fd[2];
407         int error;
408
409         error = do_pipe(fd);
410         if (error)
411                 goto out;
412         regs->u_regs[UREG_I1] = fd[1];
413         error = fd[0];
414 out:
415         return error;
416 }
417
418 /*
419  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
420  *
421  * This is really horribly ugly.
422  */
423
424 asmlinkage long sys_ipc(unsigned int call, int first, unsigned long second,
425                         unsigned long third, void __user *ptr, long fifth)
426 {
427         int err;
428
429         /* No need for backward compatibility. We can start fresh... */
430         if (call <= SEMCTL) {
431                 switch (call) {
432                 case SEMOP:
433                         err = sys_semtimedop(first, ptr,
434                                              (unsigned)second, NULL);
435                         goto out;
436                 case SEMTIMEDOP:
437                         err = sys_semtimedop(first, ptr, (unsigned)second,
438                                 (const struct timespec __user *) fifth);
439                         goto out;
440                 case SEMGET:
441                         err = sys_semget(first, (int)second, (int)third);
442                         goto out;
443                 case SEMCTL: {
444                         union semun fourth;
445                         err = -EINVAL;
446                         if (!ptr)
447                                 goto out;
448                         err = -EFAULT;
449                         if (get_user(fourth.__pad,
450                                      (void __user * __user *) ptr))
451                                 goto out;
452                         err = sys_semctl(first, (int)second | IPC_64,
453                                          (int)third, fourth);
454                         goto out;
455                 }
456                 default:
457                         err = -ENOSYS;
458                         goto out;
459                 };
460         }
461         if (call <= MSGCTL) {
462                 switch (call) {
463                 case MSGSND:
464                         err = sys_msgsnd(first, ptr, (size_t)second,
465                                          (int)third);
466                         goto out;
467                 case MSGRCV:
468                         err = sys_msgrcv(first, ptr, (size_t)second, fifth,
469                                          (int)third);
470                         goto out;
471                 case MSGGET:
472                         err = sys_msgget((key_t)first, (int)second);
473                         goto out;
474                 case MSGCTL:
475                         err = sys_msgctl(first, (int)second | IPC_64, ptr);
476                         goto out;
477                 default:
478                         err = -ENOSYS;
479                         goto out;
480                 };
481         }
482         if (call <= SHMCTL) {
483                 switch (call) {
484                 case SHMAT: {
485                         ulong raddr;
486                         err = do_shmat(first, ptr, (int)second, &raddr);
487                         if (!err) {
488                                 if (put_user(raddr,
489                                              (ulong __user *) third))
490                                         err = -EFAULT;
491                         }
492                         goto out;
493                 }
494                 case SHMDT:
495                         err = sys_shmdt(ptr);
496                         goto out;
497                 case SHMGET:
498                         err = sys_shmget(first, (size_t)second, (int)third);
499                         goto out;
500                 case SHMCTL:
501                         err = sys_shmctl(first, (int)second | IPC_64, ptr);
502                         goto out;
503                 default:
504                         err = -ENOSYS;
505                         goto out;
506                 };
507         } else {
508                 err = -ENOSYS;
509         }
510 out:
511         return err;
512 }
513
514 asmlinkage long sparc64_newuname(struct new_utsname __user *name)
515 {
516         int ret = sys_newuname(name);
517         
518         if (current->personality == PER_LINUX32 && !ret) {
519                 ret = (copy_to_user(name->machine, "sparc\0\0", 8)
520                        ? -EFAULT : 0);
521         }
522         return ret;
523 }
524
525 asmlinkage long sparc64_personality(unsigned long personality)
526 {
527         int ret;
528
529         if (current->personality == PER_LINUX32 &&
530             personality == PER_LINUX)
531                 personality = PER_LINUX32;
532         ret = sys_personality(personality);
533         if (ret == PER_LINUX32)
534                 ret = PER_LINUX;
535
536         return ret;
537 }
538
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,
542         unsigned long off)
543 {
544         struct file * file = NULL;
545         unsigned long retval = -EBADF;
546
547         if (!(flags & MAP_ANONYMOUS)) {
548                 file = fget(fd);
549                 if (!file)
550                         goto out;
551         }
552         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
553         len = PAGE_ALIGN(len);
554         retval = -EINVAL;
555
556         if (test_thread_flag(TIF_32BIT)) {
557                 if (len >= 0xf0000000UL)
558                         goto out_putf;
559
560                 if ((flags & MAP_FIXED) && addr > 0xf0000000UL - len)
561                         goto out_putf;
562         } else {
563                 if (len >= VA_EXCLUDE_START)
564                         goto out_putf;
565
566                 if ((flags & MAP_FIXED) && invalid_64bit_range(addr, len))
567                         goto out_putf;
568         }
569
570         down_write(&current->mm->mmap_sem);
571         retval = do_mmap(file, addr, len, prot, flags, off);
572         up_write(&current->mm->mmap_sem);
573
574 out_putf:
575         if (file)
576                 fput(file);
577 out:
578         return retval;
579 }
580
581 asmlinkage long sys64_munmap(unsigned long addr, size_t len)
582 {
583         long ret;
584
585         if (invalid_64bit_range(addr, len))
586                 return -EINVAL;
587
588         down_write(&current->mm->mmap_sem);
589         ret = do_munmap(current->mm, addr, len);
590         up_write(&current->mm->mmap_sem);
591         return ret;
592 }
593
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);
597                 
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)
601 {
602         struct vm_area_struct *vma;
603         unsigned long ret = -EINVAL;
604
605         if (test_thread_flag(TIF_32BIT))
606                 goto out;
607         if (unlikely(new_len >= VA_EXCLUDE_START))
608                 goto out;
609         if (unlikely(invalid_64bit_range(addr, old_len)))
610                 goto out;
611
612         down_write(&current->mm->mmap_sem);
613         if (flags & MREMAP_FIXED) {
614                 if (invalid_64bit_range(new_addr, new_len))
615                         goto out_sem;
616         } else if (invalid_64bit_range(addr, new_len)) {
617                 unsigned long map_flags = 0;
618                 struct file *file = NULL;
619
620                 ret = -ENOMEM;
621                 if (!(flags & MREMAP_MAYMOVE))
622                         goto out_sem;
623
624                 vma = find_vma(current->mm, addr);
625                 if (vma) {
626                         if (vma->vm_flags & VM_SHARED)
627                                 map_flags |= MAP_SHARED;
628                         file = vma->vm_file;
629                 }
630
631                 /* MREMAP_FIXED checked above. */
632                 new_addr = get_unmapped_area(file, addr, new_len,
633                                     vma ? vma->vm_pgoff : 0,
634                                     map_flags);
635                 ret = new_addr;
636                 if (new_addr & ~PAGE_MASK)
637                         goto out_sem;
638                 flags |= MREMAP_FIXED;
639         }
640         ret = do_mremap(addr, old_len, new_len, flags, new_addr);
641 out_sem:
642         up_write(&current->mm->mmap_sem);
643 out:
644         return ret;       
645 }
646
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)
649 {
650         static int count;
651         
652         /* Don't make the system unusable, if someone goes stuck */
653         if (count++ > 5)
654                 return -ENOSYS;
655
656         printk ("Unimplemented SPARC system call %ld\n",regs->u_regs[1]);
657 #ifdef DEBUG_UNIMP_SYSCALL      
658         show_regs (regs);
659 #endif
660
661         return -ENOSYS;
662 }
663
664 /* #define DEBUG_SPARC_BREAKPOINT */
665
666 asmlinkage void sparc_breakpoint(struct pt_regs *regs)
667 {
668         siginfo_t info;
669
670         if (test_thread_flag(TIF_32BIT)) {
671                 regs->tpc &= 0xffffffff;
672                 regs->tnpc &= 0xffffffff;
673         }
674 #ifdef DEBUG_SPARC_BREAKPOINT
675         printk ("TRAP: Entering kernel PC=%lx, nPC=%lx\n", regs->tpc, regs->tnpc);
676 #endif
677         info.si_signo = SIGTRAP;
678         info.si_errno = 0;
679         info.si_code = TRAP_BRKPT;
680         info.si_addr = (void __user *)regs->tpc;
681         info.si_trapno = 0;
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);
685 #endif
686 }
687
688 extern void check_pending(int signum);
689
690 asmlinkage long sys_getdomainname(char __user *name, int len)
691 {
692         int nlen;
693         int err = -EFAULT;
694
695         down_read(&uts_sem);
696         
697         nlen = strlen(system_utsname.domainname) + 1;
698
699         if (nlen < len)
700                 len = nlen;
701         if (len > __NEW_UTS_LEN)
702                 goto done;
703         if (copy_to_user(name, system_utsname.domainname, len))
704                 goto done;
705         err = 0;
706 done:
707         up_read(&uts_sem);
708         return err;
709 }
710
711 asmlinkage long solaris_syscall(struct pt_regs *regs)
712 {
713         static int count;
714
715         regs->tpc = regs->tnpc;
716         regs->tnpc += 4;
717         if (test_thread_flag(TIF_32BIT)) {
718                 regs->tpc &= 0xffffffff;
719                 regs->tnpc &= 0xffffffff;
720         }
721         if (++count <= 5) {
722                 printk ("For Solaris binary emulation you need solaris module loaded\n");
723                 show_regs (regs);
724         }
725         send_sig(SIGSEGV, current, 1);
726
727         return -ENOSYS;
728 }
729
730 #ifndef CONFIG_SUNOS_EMUL
731 asmlinkage long sunos_syscall(struct pt_regs *regs)
732 {
733         static int count;
734
735         regs->tpc = regs->tnpc;
736         regs->tnpc += 4;
737         if (test_thread_flag(TIF_32BIT)) {
738                 regs->tpc &= 0xffffffff;
739                 regs->tnpc &= 0xffffffff;
740         }
741         if (++count <= 20)
742                 printk ("SunOS binary emulation not compiled in\n");
743         force_sig(SIGSEGV, current);
744
745         return -ENOSYS;
746 }
747 #endif
748
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)
754 {
755         if (type < UT_INSTRUCTION_EXCEPTION || type > UT_TRAP_INSTRUCTION_31)
756                 return -EINVAL;
757         if (new_p == (utrap_handler_t)(long)UTH_NOCHANGE) {
758                 if (old_p) {
759                         if (!current_thread_info()->utraps) {
760                                 if (put_user(NULL, old_p))
761                                         return -EFAULT;
762                         } else {
763                                 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
764                                         return -EFAULT;
765                         }
766                 }
767                 if (old_d) {
768                         if (put_user(NULL, old_d))
769                                 return -EFAULT;
770                 }
771                 return 0;
772         }
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)
777                         return -ENOMEM;
778                 current_thread_info()->utraps[0] = 1;
779         } else {
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;
783
784                         current_thread_info()->utraps =
785                                 kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long),
786                                         GFP_KERNEL);
787                         if (!current_thread_info()->utraps) {
788                                 current_thread_info()->utraps = p;
789                                 return -ENOMEM;
790                         }
791                         p[0]--;
792                         current_thread_info()->utraps[0] = 1;
793                         memcpy(current_thread_info()->utraps+1, p+1,
794                                UT_TRAP_INSTRUCTION_31*sizeof(long));
795                 }
796         }
797         if (old_p) {
798                 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
799                         return -EFAULT;
800         }
801         if (old_d) {
802                 if (put_user(NULL, old_d))
803                         return -EFAULT;
804         }
805         current_thread_info()->utraps[type] = (long)new_p;
806
807         return 0;
808 }
809
810 long sparc_memory_ordering(unsigned long model, struct pt_regs *regs)
811 {
812         if (model >= 3)
813                 return -EINVAL;
814         regs->tstate = (regs->tstate & ~TSTATE_MM) | (model << 14);
815         return 0;
816 }
817
818 asmlinkage long sys_rt_sigaction(int sig,
819                                  const struct sigaction __user *act,
820                                  struct sigaction __user *oact,
821                                  void __user *restorer,
822                                  size_t sigsetsize)
823 {
824         struct k_sigaction new_ka, old_ka;
825         int ret;
826
827         /* XXX: Don't preclude handling different sized sigset_t's.  */
828         if (sigsetsize != sizeof(sigset_t))
829                 return -EINVAL;
830
831         if (act) {
832                 new_ka.ka_restorer = restorer;
833                 if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
834                         return -EFAULT;
835         }
836
837         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
838
839         if (!ret && oact) {
840                 if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
841                         return -EFAULT;
842         }
843
844         return ret;
845 }
846
847 /* Invoked by rtrap code to update performance counters in
848  * user space.
849  */
850 asmlinkage void update_perfctrs(void)
851 {
852         unsigned long pic, tmp;
853
854         read_pic(pic);
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);
859         reset_pic();
860 }
861
862 asmlinkage long sys_perfctr(int opcode, unsigned long arg0, unsigned long arg1, unsigned long arg2)
863 {
864         int err = 0;
865
866         switch(opcode) {
867         case PERFCTR_ON:
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;
873                 write_pcr(arg2);
874                 reset_pic();
875                 set_thread_flag(TIF_PERFCTR);
876                 break;
877
878         case PERFCTR_OFF:
879                 err = -EINVAL;
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;
884                         write_pcr(0);
885                         clear_thread_flag(TIF_PERFCTR);
886                         err = 0;
887                 }
888                 break;
889
890         case PERFCTR_READ: {
891                 unsigned long pic, tmp;
892
893                 if (!test_thread_flag(TIF_PERFCTR)) {
894                         err = -EINVAL;
895                         break;
896                 }
897                 read_pic(pic);
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);
902                 reset_pic();
903                 break;
904         }
905
906         case PERFCTR_CLRPIC:
907                 if (!test_thread_flag(TIF_PERFCTR)) {
908                         err = -EINVAL;
909                         break;
910                 }
911                 current_thread_info()->kernel_cntd0 =
912                         current_thread_info()->kernel_cntd1 = 0;
913                 reset_pic();
914                 break;
915
916         case PERFCTR_SETPCR: {
917                 u64 __user *user_pcr = (u64 __user *)arg0;
918
919                 if (!test_thread_flag(TIF_PERFCTR)) {
920                         err = -EINVAL;
921                         break;
922                 }
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;
927                 reset_pic();
928                 break;
929         }
930
931         case PERFCTR_GETPCR: {
932                 u64 __user *user_pcr = (u64 __user *)arg0;
933
934                 if (!test_thread_flag(TIF_PERFCTR)) {
935                         err = -EINVAL;
936                         break;
937                 }
938                 err |= __put_user(current_thread_info()->pcr_reg, user_pcr);
939                 break;
940         }
941
942         default:
943                 err = -EINVAL;
944                 break;
945         };
946         return err;
947 }