Merge master.kernel.org:/pub/scm/linux/kernel/git/lethal/sh-2.6
[linux-2.6] / 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/errno.h>
10 #include <linux/types.h>
11 #include <linux/sched.h>
12 #include <linux/fs.h>
13 #include <linux/file.h>
14 #include <linux/mm.h>
15 #include <linux/sem.h>
16 #include <linux/msg.h>
17 #include <linux/shm.h>
18 #include <linux/stat.h>
19 #include <linux/mman.h>
20 #include <linux/utsname.h>
21 #include <linux/smp.h>
22 #include <linux/slab.h>
23 #include <linux/syscalls.h>
24 #include <linux/ipc.h>
25 #include <linux/personality.h>
26 #include <linux/random.h>
27
28 #include <asm/uaccess.h>
29 #include <asm/utrap.h>
30 #include <asm/perfctr.h>
31 #include <asm/a.out.h>
32 #include <asm/unistd.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 = STACK_TOP32;
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 = STACK_TOP32;
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, orig_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         unsigned long random_factor = 0UL;
361
362         if (current->flags & PF_RANDOMIZE) {
363                 random_factor = get_random_int();
364                 if (test_thread_flag(TIF_32BIT))
365                         random_factor &= ((1 * 1024 * 1024) - 1);
366                 else
367                         random_factor = ((random_factor << PAGE_SHIFT) &
368                                          0xffffffffUL);
369         }
370
371         /*
372          * Fall back to the standard layout if the personality
373          * bit is set, or if the expected stack growth is unlimited:
374          */
375         if (!test_thread_flag(TIF_32BIT) ||
376             (current->personality & ADDR_COMPAT_LAYOUT) ||
377             current->signal->rlim[RLIMIT_STACK].rlim_cur == RLIM_INFINITY ||
378             sysctl_legacy_va_layout) {
379                 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
380                 mm->get_unmapped_area = arch_get_unmapped_area;
381                 mm->unmap_area = arch_unmap_area;
382         } else {
383                 /* We know it's 32-bit */
384                 unsigned long task_size = STACK_TOP32;
385                 unsigned long gap;
386
387                 gap = current->signal->rlim[RLIMIT_STACK].rlim_cur;
388                 if (gap < 128 * 1024 * 1024)
389                         gap = 128 * 1024 * 1024;
390                 if (gap > (task_size / 6 * 5))
391                         gap = (task_size / 6 * 5);
392
393                 mm->mmap_base = PAGE_ALIGN(task_size - gap - random_factor);
394                 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
395                 mm->unmap_area = arch_unmap_area_topdown;
396         }
397 }
398
399 asmlinkage unsigned long sparc_brk(unsigned long brk)
400 {
401         /* People could try to be nasty and use ta 0x6d in 32bit programs */
402         if (test_thread_flag(TIF_32BIT) && brk >= STACK_TOP32)
403                 return current->mm->brk;
404
405         if (unlikely(straddles_64bit_va_hole(current->mm->brk, brk)))
406                 return current->mm->brk;
407
408         return sys_brk(brk);
409 }
410                                                                 
411 /*
412  * sys_pipe() is the normal C calling standard for creating
413  * a pipe. It's not the way unix traditionally does this, though.
414  */
415 asmlinkage long sparc_pipe(struct pt_regs *regs)
416 {
417         int fd[2];
418         int error;
419
420         error = do_pipe(fd);
421         if (error)
422                 goto out;
423         regs->u_regs[UREG_I1] = fd[1];
424         error = fd[0];
425 out:
426         return error;
427 }
428
429 /*
430  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
431  *
432  * This is really horribly ugly.
433  */
434
435 asmlinkage long sys_ipc(unsigned int call, int first, unsigned long second,
436                         unsigned long third, void __user *ptr, long fifth)
437 {
438         long err;
439
440         /* No need for backward compatibility. We can start fresh... */
441         if (call <= SEMCTL) {
442                 switch (call) {
443                 case SEMOP:
444                         err = sys_semtimedop(first, ptr,
445                                              (unsigned)second, NULL);
446                         goto out;
447                 case SEMTIMEDOP:
448                         err = sys_semtimedop(first, ptr, (unsigned)second,
449                                 (const struct timespec __user *) fifth);
450                         goto out;
451                 case SEMGET:
452                         err = sys_semget(first, (int)second, (int)third);
453                         goto out;
454                 case SEMCTL: {
455                         err = sys_semctl(first, third,
456                                          (int)second | IPC_64,
457                                          (union semun) ptr);
458                         goto out;
459                 }
460                 default:
461                         err = -ENOSYS;
462                         goto out;
463                 };
464         }
465         if (call <= MSGCTL) {
466                 switch (call) {
467                 case MSGSND:
468                         err = sys_msgsnd(first, ptr, (size_t)second,
469                                          (int)third);
470                         goto out;
471                 case MSGRCV:
472                         err = sys_msgrcv(first, ptr, (size_t)second, fifth,
473                                          (int)third);
474                         goto out;
475                 case MSGGET:
476                         err = sys_msgget((key_t)first, (int)second);
477                         goto out;
478                 case MSGCTL:
479                         err = sys_msgctl(first, (int)second | IPC_64, ptr);
480                         goto out;
481                 default:
482                         err = -ENOSYS;
483                         goto out;
484                 };
485         }
486         if (call <= SHMCTL) {
487                 switch (call) {
488                 case SHMAT: {
489                         ulong raddr;
490                         err = do_shmat(first, ptr, (int)second, &raddr);
491                         if (!err) {
492                                 if (put_user(raddr,
493                                              (ulong __user *) third))
494                                         err = -EFAULT;
495                         }
496                         goto out;
497                 }
498                 case SHMDT:
499                         err = sys_shmdt(ptr);
500                         goto out;
501                 case SHMGET:
502                         err = sys_shmget(first, (size_t)second, (int)third);
503                         goto out;
504                 case SHMCTL:
505                         err = sys_shmctl(first, (int)second | IPC_64, ptr);
506                         goto out;
507                 default:
508                         err = -ENOSYS;
509                         goto out;
510                 };
511         } else {
512                 err = -ENOSYS;
513         }
514 out:
515         return err;
516 }
517
518 asmlinkage long sparc64_newuname(struct new_utsname __user *name)
519 {
520         int ret = sys_newuname(name);
521         
522         if (current->personality == PER_LINUX32 && !ret) {
523                 ret = (copy_to_user(name->machine, "sparc\0\0", 8)
524                        ? -EFAULT : 0);
525         }
526         return ret;
527 }
528
529 asmlinkage long sparc64_personality(unsigned long personality)
530 {
531         int ret;
532
533         if (current->personality == PER_LINUX32 &&
534             personality == PER_LINUX)
535                 personality = PER_LINUX32;
536         ret = sys_personality(personality);
537         if (ret == PER_LINUX32)
538                 ret = PER_LINUX;
539
540         return ret;
541 }
542
543 int sparc64_mmap_check(unsigned long addr, unsigned long len,
544                 unsigned long flags)
545 {
546         if (test_thread_flag(TIF_32BIT)) {
547                 if (len >= STACK_TOP32)
548                         return -EINVAL;
549
550                 if ((flags & MAP_FIXED) && addr > STACK_TOP32 - len)
551                         return -EINVAL;
552         } else {
553                 if (len >= VA_EXCLUDE_START)
554                         return -EINVAL;
555
556                 if ((flags & MAP_FIXED) && invalid_64bit_range(addr, len))
557                         return -EINVAL;
558         }
559
560         return 0;
561 }
562
563 /* Linux version of mmap */
564 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
565         unsigned long prot, unsigned long flags, unsigned long fd,
566         unsigned long off)
567 {
568         struct file * file = NULL;
569         unsigned long retval = -EBADF;
570
571         if (!(flags & MAP_ANONYMOUS)) {
572                 file = fget(fd);
573                 if (!file)
574                         goto out;
575         }
576         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
577         len = PAGE_ALIGN(len);
578
579         down_write(&current->mm->mmap_sem);
580         retval = do_mmap(file, addr, len, prot, flags, off);
581         up_write(&current->mm->mmap_sem);
582
583         if (file)
584                 fput(file);
585 out:
586         return retval;
587 }
588
589 asmlinkage long sys64_munmap(unsigned long addr, size_t len)
590 {
591         long ret;
592
593         if (invalid_64bit_range(addr, len))
594                 return -EINVAL;
595
596         down_write(&current->mm->mmap_sem);
597         ret = do_munmap(current->mm, addr, len);
598         up_write(&current->mm->mmap_sem);
599         return ret;
600 }
601
602 extern unsigned long do_mremap(unsigned long addr,
603         unsigned long old_len, unsigned long new_len,
604         unsigned long flags, unsigned long new_addr);
605                 
606 asmlinkage unsigned long sys64_mremap(unsigned long addr,
607         unsigned long old_len, unsigned long new_len,
608         unsigned long flags, unsigned long new_addr)
609 {
610         struct vm_area_struct *vma;
611         unsigned long ret = -EINVAL;
612
613         if (test_thread_flag(TIF_32BIT))
614                 goto out;
615         if (unlikely(new_len >= VA_EXCLUDE_START))
616                 goto out;
617         if (unlikely(invalid_64bit_range(addr, old_len)))
618                 goto out;
619
620         down_write(&current->mm->mmap_sem);
621         if (flags & MREMAP_FIXED) {
622                 if (invalid_64bit_range(new_addr, new_len))
623                         goto out_sem;
624         } else if (invalid_64bit_range(addr, new_len)) {
625                 unsigned long map_flags = 0;
626                 struct file *file = NULL;
627
628                 ret = -ENOMEM;
629                 if (!(flags & MREMAP_MAYMOVE))
630                         goto out_sem;
631
632                 vma = find_vma(current->mm, addr);
633                 if (vma) {
634                         if (vma->vm_flags & VM_SHARED)
635                                 map_flags |= MAP_SHARED;
636                         file = vma->vm_file;
637                 }
638
639                 /* MREMAP_FIXED checked above. */
640                 new_addr = get_unmapped_area(file, addr, new_len,
641                                     vma ? vma->vm_pgoff : 0,
642                                     map_flags);
643                 ret = new_addr;
644                 if (new_addr & ~PAGE_MASK)
645                         goto out_sem;
646                 flags |= MREMAP_FIXED;
647         }
648         ret = do_mremap(addr, old_len, new_len, flags, new_addr);
649 out_sem:
650         up_write(&current->mm->mmap_sem);
651 out:
652         return ret;       
653 }
654
655 /* we come to here via sys_nis_syscall so it can setup the regs argument */
656 asmlinkage unsigned long c_sys_nis_syscall(struct pt_regs *regs)
657 {
658         static int count;
659         
660         /* Don't make the system unusable, if someone goes stuck */
661         if (count++ > 5)
662                 return -ENOSYS;
663
664         printk ("Unimplemented SPARC system call %ld\n",regs->u_regs[1]);
665 #ifdef DEBUG_UNIMP_SYSCALL      
666         show_regs (regs);
667 #endif
668
669         return -ENOSYS;
670 }
671
672 /* #define DEBUG_SPARC_BREAKPOINT */
673
674 asmlinkage void sparc_breakpoint(struct pt_regs *regs)
675 {
676         siginfo_t info;
677
678         if (test_thread_flag(TIF_32BIT)) {
679                 regs->tpc &= 0xffffffff;
680                 regs->tnpc &= 0xffffffff;
681         }
682 #ifdef DEBUG_SPARC_BREAKPOINT
683         printk ("TRAP: Entering kernel PC=%lx, nPC=%lx\n", regs->tpc, regs->tnpc);
684 #endif
685         info.si_signo = SIGTRAP;
686         info.si_errno = 0;
687         info.si_code = TRAP_BRKPT;
688         info.si_addr = (void __user *)regs->tpc;
689         info.si_trapno = 0;
690         force_sig_info(SIGTRAP, &info, current);
691 #ifdef DEBUG_SPARC_BREAKPOINT
692         printk ("TRAP: Returning to space: PC=%lx nPC=%lx\n", regs->tpc, regs->tnpc);
693 #endif
694 }
695
696 extern void check_pending(int signum);
697
698 asmlinkage long sys_getdomainname(char __user *name, int len)
699 {
700         int nlen, err;
701
702         if (len < 0)
703                 return -EINVAL;
704
705         down_read(&uts_sem);
706         
707         nlen = strlen(utsname()->domainname) + 1;
708         err = -EINVAL;
709         if (nlen > len)
710                 goto out;
711
712         err = -EFAULT;
713         if (!copy_to_user(name, utsname()->domainname, nlen))
714                 err = 0;
715
716 out:
717         up_read(&uts_sem);
718         return err;
719 }
720
721 asmlinkage long solaris_syscall(struct pt_regs *regs)
722 {
723         static int count;
724
725         regs->tpc = regs->tnpc;
726         regs->tnpc += 4;
727         if (test_thread_flag(TIF_32BIT)) {
728                 regs->tpc &= 0xffffffff;
729                 regs->tnpc &= 0xffffffff;
730         }
731         if (++count <= 5) {
732                 printk ("For Solaris binary emulation you need solaris module loaded\n");
733                 show_regs (regs);
734         }
735         send_sig(SIGSEGV, current, 1);
736
737         return -ENOSYS;
738 }
739
740 #ifndef CONFIG_SUNOS_EMUL
741 asmlinkage long sunos_syscall(struct pt_regs *regs)
742 {
743         static int count;
744
745         regs->tpc = regs->tnpc;
746         regs->tnpc += 4;
747         if (test_thread_flag(TIF_32BIT)) {
748                 regs->tpc &= 0xffffffff;
749                 regs->tnpc &= 0xffffffff;
750         }
751         if (++count <= 20)
752                 printk ("SunOS binary emulation not compiled in\n");
753         force_sig(SIGSEGV, current);
754
755         return -ENOSYS;
756 }
757 #endif
758
759 asmlinkage long sys_utrap_install(utrap_entry_t type,
760                                   utrap_handler_t new_p,
761                                   utrap_handler_t new_d,
762                                   utrap_handler_t __user *old_p,
763                                   utrap_handler_t __user *old_d)
764 {
765         if (type < UT_INSTRUCTION_EXCEPTION || type > UT_TRAP_INSTRUCTION_31)
766                 return -EINVAL;
767         if (new_p == (utrap_handler_t)(long)UTH_NOCHANGE) {
768                 if (old_p) {
769                         if (!current_thread_info()->utraps) {
770                                 if (put_user(NULL, old_p))
771                                         return -EFAULT;
772                         } else {
773                                 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
774                                         return -EFAULT;
775                         }
776                 }
777                 if (old_d) {
778                         if (put_user(NULL, old_d))
779                                 return -EFAULT;
780                 }
781                 return 0;
782         }
783         if (!current_thread_info()->utraps) {
784                 current_thread_info()->utraps =
785                         kzalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long), GFP_KERNEL);
786                 if (!current_thread_info()->utraps)
787                         return -ENOMEM;
788                 current_thread_info()->utraps[0] = 1;
789         } else {
790                 if ((utrap_handler_t)current_thread_info()->utraps[type] != new_p &&
791                     current_thread_info()->utraps[0] > 1) {
792                         long *p = current_thread_info()->utraps;
793
794                         current_thread_info()->utraps =
795                                 kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long),
796                                         GFP_KERNEL);
797                         if (!current_thread_info()->utraps) {
798                                 current_thread_info()->utraps = p;
799                                 return -ENOMEM;
800                         }
801                         p[0]--;
802                         current_thread_info()->utraps[0] = 1;
803                         memcpy(current_thread_info()->utraps+1, p+1,
804                                UT_TRAP_INSTRUCTION_31*sizeof(long));
805                 }
806         }
807         if (old_p) {
808                 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
809                         return -EFAULT;
810         }
811         if (old_d) {
812                 if (put_user(NULL, old_d))
813                         return -EFAULT;
814         }
815         current_thread_info()->utraps[type] = (long)new_p;
816
817         return 0;
818 }
819
820 long sparc_memory_ordering(unsigned long model, struct pt_regs *regs)
821 {
822         if (model >= 3)
823                 return -EINVAL;
824         regs->tstate = (regs->tstate & ~TSTATE_MM) | (model << 14);
825         return 0;
826 }
827
828 asmlinkage long sys_rt_sigaction(int sig,
829                                  const struct sigaction __user *act,
830                                  struct sigaction __user *oact,
831                                  void __user *restorer,
832                                  size_t sigsetsize)
833 {
834         struct k_sigaction new_ka, old_ka;
835         int ret;
836
837         /* XXX: Don't preclude handling different sized sigset_t's.  */
838         if (sigsetsize != sizeof(sigset_t))
839                 return -EINVAL;
840
841         if (act) {
842                 new_ka.ka_restorer = restorer;
843                 if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
844                         return -EFAULT;
845         }
846
847         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
848
849         if (!ret && oact) {
850                 if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
851                         return -EFAULT;
852         }
853
854         return ret;
855 }
856
857 /* Invoked by rtrap code to update performance counters in
858  * user space.
859  */
860 asmlinkage void update_perfctrs(void)
861 {
862         unsigned long pic, tmp;
863
864         read_pic(pic);
865         tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
866         __put_user(tmp, current_thread_info()->user_cntd0);
867         tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
868         __put_user(tmp, current_thread_info()->user_cntd1);
869         reset_pic();
870 }
871
872 asmlinkage long sys_perfctr(int opcode, unsigned long arg0, unsigned long arg1, unsigned long arg2)
873 {
874         int err = 0;
875
876         switch(opcode) {
877         case PERFCTR_ON:
878                 current_thread_info()->pcr_reg = arg2;
879                 current_thread_info()->user_cntd0 = (u64 __user *) arg0;
880                 current_thread_info()->user_cntd1 = (u64 __user *) arg1;
881                 current_thread_info()->kernel_cntd0 =
882                         current_thread_info()->kernel_cntd1 = 0;
883                 write_pcr(arg2);
884                 reset_pic();
885                 set_thread_flag(TIF_PERFCTR);
886                 break;
887
888         case PERFCTR_OFF:
889                 err = -EINVAL;
890                 if (test_thread_flag(TIF_PERFCTR)) {
891                         current_thread_info()->user_cntd0 =
892                                 current_thread_info()->user_cntd1 = NULL;
893                         current_thread_info()->pcr_reg = 0;
894                         write_pcr(0);
895                         clear_thread_flag(TIF_PERFCTR);
896                         err = 0;
897                 }
898                 break;
899
900         case PERFCTR_READ: {
901                 unsigned long pic, tmp;
902
903                 if (!test_thread_flag(TIF_PERFCTR)) {
904                         err = -EINVAL;
905                         break;
906                 }
907                 read_pic(pic);
908                 tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
909                 err |= __put_user(tmp, current_thread_info()->user_cntd0);
910                 tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
911                 err |= __put_user(tmp, current_thread_info()->user_cntd1);
912                 reset_pic();
913                 break;
914         }
915
916         case PERFCTR_CLRPIC:
917                 if (!test_thread_flag(TIF_PERFCTR)) {
918                         err = -EINVAL;
919                         break;
920                 }
921                 current_thread_info()->kernel_cntd0 =
922                         current_thread_info()->kernel_cntd1 = 0;
923                 reset_pic();
924                 break;
925
926         case PERFCTR_SETPCR: {
927                 u64 __user *user_pcr = (u64 __user *)arg0;
928
929                 if (!test_thread_flag(TIF_PERFCTR)) {
930                         err = -EINVAL;
931                         break;
932                 }
933                 err |= __get_user(current_thread_info()->pcr_reg, user_pcr);
934                 write_pcr(current_thread_info()->pcr_reg);
935                 current_thread_info()->kernel_cntd0 =
936                         current_thread_info()->kernel_cntd1 = 0;
937                 reset_pic();
938                 break;
939         }
940
941         case PERFCTR_GETPCR: {
942                 u64 __user *user_pcr = (u64 __user *)arg0;
943
944                 if (!test_thread_flag(TIF_PERFCTR)) {
945                         err = -EINVAL;
946                         break;
947                 }
948                 err |= __put_user(current_thread_info()->pcr_reg, user_pcr);
949                 break;
950         }
951
952         default:
953                 err = -EINVAL;
954                 break;
955         };
956         return err;
957 }
958
959 /*
960  * Do a system call from kernel instead of calling sys_execve so we
961  * end up with proper pt_regs.
962  */
963 int kernel_execve(const char *filename, char *const argv[], char *const envp[])
964 {
965         long __res;
966         register long __g1 __asm__ ("g1") = __NR_execve;
967         register long __o0 __asm__ ("o0") = (long)(filename);
968         register long __o1 __asm__ ("o1") = (long)(argv);
969         register long __o2 __asm__ ("o2") = (long)(envp);
970         asm volatile ("t 0x6d\n\t"
971                       "sub %%g0, %%o0, %0\n\t"
972                       "movcc %%xcc, %%o0, %0\n\t"
973                       : "=r" (__res), "=&r" (__o0)
974                       : "1" (__o0), "r" (__o1), "r" (__o2), "r" (__g1)
975                       : "cc");
976         return __res;
977 }