[SPARC64]: Do not allow mapping pages within 4GB of 64-bit VA hole.
[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/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 #define COLOUR_ALIGN(addr,pgoff)                \
86         ((((addr)+SHMLBA-1)&~(SHMLBA-1)) +      \
87          (((pgoff)<<PAGE_SHIFT) & (SHMLBA-1)))
88
89 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)
90 {
91         struct mm_struct *mm = current->mm;
92         struct vm_area_struct * vma;
93         unsigned long task_size = TASK_SIZE;
94         unsigned long start_addr;
95         int do_color_align;
96
97         if (flags & MAP_FIXED) {
98                 /* We do not accept a shared mapping if it would violate
99                  * cache aliasing constraints.
100                  */
101                 if ((flags & MAP_SHARED) &&
102                     ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
103                         return -EINVAL;
104                 return addr;
105         }
106
107         if (test_thread_flag(TIF_32BIT))
108                 task_size = 0xf0000000UL;
109         if (len > task_size || len >= VA_EXCLUDE_START)
110                 return -ENOMEM;
111
112         do_color_align = 0;
113         if (filp || (flags & MAP_SHARED))
114                 do_color_align = 1;
115
116         if (addr) {
117                 if (do_color_align)
118                         addr = COLOUR_ALIGN(addr, pgoff);
119                 else
120                         addr = PAGE_ALIGN(addr);
121
122                 vma = find_vma(mm, addr);
123                 if (task_size - len >= addr &&
124                     (!vma || addr + len <= vma->vm_start))
125                         return addr;
126         }
127
128         if (len <= mm->cached_hole_size) {
129                 mm->cached_hole_size = 0;
130                 mm->free_area_cache = TASK_UNMAPPED_BASE;
131         }
132         start_addr = addr = mm->free_area_cache;
133
134         task_size -= len;
135
136 full_search:
137         if (do_color_align)
138                 addr = COLOUR_ALIGN(addr, pgoff);
139         else
140                 addr = PAGE_ALIGN(addr);
141
142         for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
143                 /* At this point:  (!vma || addr < vma->vm_end). */
144                 if (addr < VA_EXCLUDE_START &&
145                     (addr + len) >= VA_EXCLUDE_START) {
146                         addr = VA_EXCLUDE_END;
147                         vma = find_vma(mm, VA_EXCLUDE_END);
148                 }
149                 if (task_size < addr) {
150                         if (start_addr != TASK_UNMAPPED_BASE) {
151                                 start_addr = addr = TASK_UNMAPPED_BASE;
152                                 mm->cached_hole_size = 0;
153                                 goto full_search;
154                         }
155                         return -ENOMEM;
156                 }
157                 if (!vma || addr + len <= vma->vm_start) {
158                         /*
159                          * Remember the place where we stopped the search:
160                          */
161                         mm->free_area_cache = addr + len;
162                         return addr;
163                 }
164                 if (addr + mm->cached_hole_size < vma->vm_start)
165                         mm->cached_hole_size = vma->vm_start - addr;
166
167                 addr = vma->vm_end;
168                 if (do_color_align)
169                         addr = COLOUR_ALIGN(addr, pgoff);
170         }
171 }
172
173 /* Try to align mapping such that we align it as much as possible. */
174 unsigned long get_fb_unmapped_area(struct file *filp, unsigned long orig_addr, unsigned long len, unsigned long pgoff, unsigned long flags)
175 {
176         unsigned long align_goal, addr = -ENOMEM;
177
178         if (flags & MAP_FIXED) {
179                 /* Ok, don't mess with it. */
180                 return get_unmapped_area(NULL, addr, len, pgoff, flags);
181         }
182         flags &= ~MAP_SHARED;
183
184         align_goal = PAGE_SIZE;
185         if (len >= (4UL * 1024 * 1024))
186                 align_goal = (4UL * 1024 * 1024);
187         else if (len >= (512UL * 1024))
188                 align_goal = (512UL * 1024);
189         else if (len >= (64UL * 1024))
190                 align_goal = (64UL * 1024);
191
192         do {
193                 addr = get_unmapped_area(NULL, orig_addr, len + (align_goal - PAGE_SIZE), pgoff, flags);
194                 if (!(addr & ~PAGE_MASK)) {
195                         addr = (addr + (align_goal - 1UL)) & ~(align_goal - 1UL);
196                         break;
197                 }
198
199                 if (align_goal == (4UL * 1024 * 1024))
200                         align_goal = (512UL * 1024);
201                 else if (align_goal == (512UL * 1024))
202                         align_goal = (64UL * 1024);
203                 else
204                         align_goal = PAGE_SIZE;
205         } while ((addr & ~PAGE_MASK) && align_goal > PAGE_SIZE);
206
207         /* Mapping is smaller than 64K or larger areas could not
208          * be obtained.
209          */
210         if (addr & ~PAGE_MASK)
211                 addr = get_unmapped_area(NULL, orig_addr, len, pgoff, flags);
212
213         return addr;
214 }
215
216 asmlinkage unsigned long sparc_brk(unsigned long brk)
217 {
218         /* People could try to be nasty and use ta 0x6d in 32bit programs */
219         if (test_thread_flag(TIF_32BIT) && brk >= 0xf0000000UL)
220                 return current->mm->brk;
221
222         if (unlikely(straddles_64bit_va_hole(current->mm->brk, brk)))
223                 return current->mm->brk;
224
225         return sys_brk(brk);
226 }
227                                                                 
228 /*
229  * sys_pipe() is the normal C calling standard for creating
230  * a pipe. It's not the way unix traditionally does this, though.
231  */
232 asmlinkage long sparc_pipe(struct pt_regs *regs)
233 {
234         int fd[2];
235         int error;
236
237         error = do_pipe(fd);
238         if (error)
239                 goto out;
240         regs->u_regs[UREG_I1] = fd[1];
241         error = fd[0];
242 out:
243         return error;
244 }
245
246 /*
247  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
248  *
249  * This is really horribly ugly.
250  */
251
252 asmlinkage long sys_ipc(unsigned int call, int first, unsigned long second,
253                         unsigned long third, void __user *ptr, long fifth)
254 {
255         int err;
256
257         /* No need for backward compatibility. We can start fresh... */
258         if (call <= SEMCTL) {
259                 switch (call) {
260                 case SEMOP:
261                         err = sys_semtimedop(first, ptr,
262                                              (unsigned)second, NULL);
263                         goto out;
264                 case SEMTIMEDOP:
265                         err = sys_semtimedop(first, ptr, (unsigned)second,
266                                 (const struct timespec __user *) fifth);
267                         goto out;
268                 case SEMGET:
269                         err = sys_semget(first, (int)second, (int)third);
270                         goto out;
271                 case SEMCTL: {
272                         union semun fourth;
273                         err = -EINVAL;
274                         if (!ptr)
275                                 goto out;
276                         err = -EFAULT;
277                         if (get_user(fourth.__pad,
278                                      (void __user * __user *) ptr))
279                                 goto out;
280                         err = sys_semctl(first, (int)second | IPC_64,
281                                          (int)third, fourth);
282                         goto out;
283                 }
284                 default:
285                         err = -ENOSYS;
286                         goto out;
287                 };
288         }
289         if (call <= MSGCTL) {
290                 switch (call) {
291                 case MSGSND:
292                         err = sys_msgsnd(first, ptr, (size_t)second,
293                                          (int)third);
294                         goto out;
295                 case MSGRCV:
296                         err = sys_msgrcv(first, ptr, (size_t)second, fifth,
297                                          (int)third);
298                         goto out;
299                 case MSGGET:
300                         err = sys_msgget((key_t)first, (int)second);
301                         goto out;
302                 case MSGCTL:
303                         err = sys_msgctl(first, (int)second | IPC_64, ptr);
304                         goto out;
305                 default:
306                         err = -ENOSYS;
307                         goto out;
308                 };
309         }
310         if (call <= SHMCTL) {
311                 switch (call) {
312                 case SHMAT: {
313                         ulong raddr;
314                         err = do_shmat(first, ptr, (int)second, &raddr);
315                         if (!err) {
316                                 if (put_user(raddr,
317                                              (ulong __user *) third))
318                                         err = -EFAULT;
319                         }
320                         goto out;
321                 }
322                 case SHMDT:
323                         err = sys_shmdt(ptr);
324                         goto out;
325                 case SHMGET:
326                         err = sys_shmget(first, (size_t)second, (int)third);
327                         goto out;
328                 case SHMCTL:
329                         err = sys_shmctl(first, (int)second | IPC_64, ptr);
330                         goto out;
331                 default:
332                         err = -ENOSYS;
333                         goto out;
334                 };
335         } else {
336                 err = -ENOSYS;
337         }
338 out:
339         return err;
340 }
341
342 asmlinkage long sparc64_newuname(struct new_utsname __user *name)
343 {
344         int ret = sys_newuname(name);
345         
346         if (current->personality == PER_LINUX32 && !ret) {
347                 ret = (copy_to_user(name->machine, "sparc\0\0", 8)
348                        ? -EFAULT : 0);
349         }
350         return ret;
351 }
352
353 asmlinkage long sparc64_personality(unsigned long personality)
354 {
355         int ret;
356
357         if (current->personality == PER_LINUX32 &&
358             personality == PER_LINUX)
359                 personality = PER_LINUX32;
360         ret = sys_personality(personality);
361         if (ret == PER_LINUX32)
362                 ret = PER_LINUX;
363
364         return ret;
365 }
366
367 /* Linux version of mmap */
368 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
369         unsigned long prot, unsigned long flags, unsigned long fd,
370         unsigned long off)
371 {
372         struct file * file = NULL;
373         unsigned long retval = -EBADF;
374
375         if (!(flags & MAP_ANONYMOUS)) {
376                 file = fget(fd);
377                 if (!file)
378                         goto out;
379         }
380         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
381         len = PAGE_ALIGN(len);
382         retval = -EINVAL;
383
384         if (test_thread_flag(TIF_32BIT)) {
385                 if (len >= 0xf0000000UL)
386                         goto out_putf;
387
388                 if ((flags & MAP_FIXED) && addr > 0xf0000000UL - len)
389                         goto out_putf;
390         } else {
391                 if (len >= VA_EXCLUDE_START)
392                         goto out_putf;
393
394                 if ((flags & MAP_FIXED) && invalid_64bit_range(addr, len))
395                         goto out_putf;
396         }
397
398         down_write(&current->mm->mmap_sem);
399         retval = do_mmap(file, addr, len, prot, flags, off);
400         up_write(&current->mm->mmap_sem);
401
402 out_putf:
403         if (file)
404                 fput(file);
405 out:
406         return retval;
407 }
408
409 asmlinkage long sys64_munmap(unsigned long addr, size_t len)
410 {
411         long ret;
412
413         if (invalid_64bit_range(addr, len))
414                 return -EINVAL;
415
416         down_write(&current->mm->mmap_sem);
417         ret = do_munmap(current->mm, addr, len);
418         up_write(&current->mm->mmap_sem);
419         return ret;
420 }
421
422 extern unsigned long do_mremap(unsigned long addr,
423         unsigned long old_len, unsigned long new_len,
424         unsigned long flags, unsigned long new_addr);
425                 
426 asmlinkage unsigned long sys64_mremap(unsigned long addr,
427         unsigned long old_len, unsigned long new_len,
428         unsigned long flags, unsigned long new_addr)
429 {
430         struct vm_area_struct *vma;
431         unsigned long ret = -EINVAL;
432
433         if (test_thread_flag(TIF_32BIT))
434                 goto out;
435         if (unlikely(new_len >= VA_EXCLUDE_START))
436                 goto out;
437         if (unlikely(invalid_64bit_range(addr, old_len)))
438                 goto out;
439
440         down_write(&current->mm->mmap_sem);
441         if (flags & MREMAP_FIXED) {
442                 if (invalid_64bit_range(new_addr, new_len))
443                         goto out_sem;
444         } else if (invalid_64bit_range(addr, new_len)) {
445                 unsigned long map_flags = 0;
446                 struct file *file = NULL;
447
448                 ret = -ENOMEM;
449                 if (!(flags & MREMAP_MAYMOVE))
450                         goto out_sem;
451
452                 vma = find_vma(current->mm, addr);
453                 if (vma) {
454                         if (vma->vm_flags & VM_SHARED)
455                                 map_flags |= MAP_SHARED;
456                         file = vma->vm_file;
457                 }
458
459                 /* MREMAP_FIXED checked above. */
460                 new_addr = get_unmapped_area(file, addr, new_len,
461                                     vma ? vma->vm_pgoff : 0,
462                                     map_flags);
463                 ret = new_addr;
464                 if (new_addr & ~PAGE_MASK)
465                         goto out_sem;
466                 flags |= MREMAP_FIXED;
467         }
468         ret = do_mremap(addr, old_len, new_len, flags, new_addr);
469 out_sem:
470         up_write(&current->mm->mmap_sem);
471 out:
472         return ret;       
473 }
474
475 /* we come to here via sys_nis_syscall so it can setup the regs argument */
476 asmlinkage unsigned long c_sys_nis_syscall(struct pt_regs *regs)
477 {
478         static int count;
479         
480         /* Don't make the system unusable, if someone goes stuck */
481         if (count++ > 5)
482                 return -ENOSYS;
483
484         printk ("Unimplemented SPARC system call %ld\n",regs->u_regs[1]);
485 #ifdef DEBUG_UNIMP_SYSCALL      
486         show_regs (regs);
487 #endif
488
489         return -ENOSYS;
490 }
491
492 /* #define DEBUG_SPARC_BREAKPOINT */
493
494 asmlinkage void sparc_breakpoint(struct pt_regs *regs)
495 {
496         siginfo_t info;
497
498         if (test_thread_flag(TIF_32BIT)) {
499                 regs->tpc &= 0xffffffff;
500                 regs->tnpc &= 0xffffffff;
501         }
502 #ifdef DEBUG_SPARC_BREAKPOINT
503         printk ("TRAP: Entering kernel PC=%lx, nPC=%lx\n", regs->tpc, regs->tnpc);
504 #endif
505         info.si_signo = SIGTRAP;
506         info.si_errno = 0;
507         info.si_code = TRAP_BRKPT;
508         info.si_addr = (void __user *)regs->tpc;
509         info.si_trapno = 0;
510         force_sig_info(SIGTRAP, &info, current);
511 #ifdef DEBUG_SPARC_BREAKPOINT
512         printk ("TRAP: Returning to space: PC=%lx nPC=%lx\n", regs->tpc, regs->tnpc);
513 #endif
514 }
515
516 extern void check_pending(int signum);
517
518 asmlinkage long sys_getdomainname(char __user *name, int len)
519 {
520         int nlen;
521         int err = -EFAULT;
522
523         down_read(&uts_sem);
524         
525         nlen = strlen(system_utsname.domainname) + 1;
526
527         if (nlen < len)
528                 len = nlen;
529         if (len > __NEW_UTS_LEN)
530                 goto done;
531         if (copy_to_user(name, system_utsname.domainname, len))
532                 goto done;
533         err = 0;
534 done:
535         up_read(&uts_sem);
536         return err;
537 }
538
539 asmlinkage long solaris_syscall(struct pt_regs *regs)
540 {
541         static int count;
542
543         regs->tpc = regs->tnpc;
544         regs->tnpc += 4;
545         if (test_thread_flag(TIF_32BIT)) {
546                 regs->tpc &= 0xffffffff;
547                 regs->tnpc &= 0xffffffff;
548         }
549         if (++count <= 5) {
550                 printk ("For Solaris binary emulation you need solaris module loaded\n");
551                 show_regs (regs);
552         }
553         send_sig(SIGSEGV, current, 1);
554
555         return -ENOSYS;
556 }
557
558 #ifndef CONFIG_SUNOS_EMUL
559 asmlinkage long sunos_syscall(struct pt_regs *regs)
560 {
561         static int count;
562
563         regs->tpc = regs->tnpc;
564         regs->tnpc += 4;
565         if (test_thread_flag(TIF_32BIT)) {
566                 regs->tpc &= 0xffffffff;
567                 regs->tnpc &= 0xffffffff;
568         }
569         if (++count <= 20)
570                 printk ("SunOS binary emulation not compiled in\n");
571         force_sig(SIGSEGV, current);
572
573         return -ENOSYS;
574 }
575 #endif
576
577 asmlinkage long sys_utrap_install(utrap_entry_t type,
578                                   utrap_handler_t new_p,
579                                   utrap_handler_t new_d,
580                                   utrap_handler_t __user *old_p,
581                                   utrap_handler_t __user *old_d)
582 {
583         if (type < UT_INSTRUCTION_EXCEPTION || type > UT_TRAP_INSTRUCTION_31)
584                 return -EINVAL;
585         if (new_p == (utrap_handler_t)(long)UTH_NOCHANGE) {
586                 if (old_p) {
587                         if (!current_thread_info()->utraps) {
588                                 if (put_user(NULL, old_p))
589                                         return -EFAULT;
590                         } else {
591                                 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
592                                         return -EFAULT;
593                         }
594                 }
595                 if (old_d) {
596                         if (put_user(NULL, old_d))
597                                 return -EFAULT;
598                 }
599                 return 0;
600         }
601         if (!current_thread_info()->utraps) {
602                 current_thread_info()->utraps =
603                         kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long), GFP_KERNEL);
604                 if (!current_thread_info()->utraps)
605                         return -ENOMEM;
606                 current_thread_info()->utraps[0] = 1;
607                 memset(current_thread_info()->utraps+1, 0,
608                        UT_TRAP_INSTRUCTION_31*sizeof(long));
609         } else {
610                 if ((utrap_handler_t)current_thread_info()->utraps[type] != new_p &&
611                     current_thread_info()->utraps[0] > 1) {
612                         long *p = current_thread_info()->utraps;
613
614                         current_thread_info()->utraps =
615                                 kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long),
616                                         GFP_KERNEL);
617                         if (!current_thread_info()->utraps) {
618                                 current_thread_info()->utraps = p;
619                                 return -ENOMEM;
620                         }
621                         p[0]--;
622                         current_thread_info()->utraps[0] = 1;
623                         memcpy(current_thread_info()->utraps+1, p+1,
624                                UT_TRAP_INSTRUCTION_31*sizeof(long));
625                 }
626         }
627         if (old_p) {
628                 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
629                         return -EFAULT;
630         }
631         if (old_d) {
632                 if (put_user(NULL, old_d))
633                         return -EFAULT;
634         }
635         current_thread_info()->utraps[type] = (long)new_p;
636
637         return 0;
638 }
639
640 long sparc_memory_ordering(unsigned long model, struct pt_regs *regs)
641 {
642         if (model >= 3)
643                 return -EINVAL;
644         regs->tstate = (regs->tstate & ~TSTATE_MM) | (model << 14);
645         return 0;
646 }
647
648 asmlinkage long sys_rt_sigaction(int sig,
649                                  const struct sigaction __user *act,
650                                  struct sigaction __user *oact,
651                                  void __user *restorer,
652                                  size_t sigsetsize)
653 {
654         struct k_sigaction new_ka, old_ka;
655         int ret;
656
657         /* XXX: Don't preclude handling different sized sigset_t's.  */
658         if (sigsetsize != sizeof(sigset_t))
659                 return -EINVAL;
660
661         if (act) {
662                 new_ka.ka_restorer = restorer;
663                 if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
664                         return -EFAULT;
665         }
666
667         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
668
669         if (!ret && oact) {
670                 if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
671                         return -EFAULT;
672         }
673
674         return ret;
675 }
676
677 /* Invoked by rtrap code to update performance counters in
678  * user space.
679  */
680 asmlinkage void update_perfctrs(void)
681 {
682         unsigned long pic, tmp;
683
684         read_pic(pic);
685         tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
686         __put_user(tmp, current_thread_info()->user_cntd0);
687         tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
688         __put_user(tmp, current_thread_info()->user_cntd1);
689         reset_pic();
690 }
691
692 asmlinkage long sys_perfctr(int opcode, unsigned long arg0, unsigned long arg1, unsigned long arg2)
693 {
694         int err = 0;
695
696         switch(opcode) {
697         case PERFCTR_ON:
698                 current_thread_info()->pcr_reg = arg2;
699                 current_thread_info()->user_cntd0 = (u64 __user *) arg0;
700                 current_thread_info()->user_cntd1 = (u64 __user *) arg1;
701                 current_thread_info()->kernel_cntd0 =
702                         current_thread_info()->kernel_cntd1 = 0;
703                 write_pcr(arg2);
704                 reset_pic();
705                 set_thread_flag(TIF_PERFCTR);
706                 break;
707
708         case PERFCTR_OFF:
709                 err = -EINVAL;
710                 if (test_thread_flag(TIF_PERFCTR)) {
711                         current_thread_info()->user_cntd0 =
712                                 current_thread_info()->user_cntd1 = NULL;
713                         current_thread_info()->pcr_reg = 0;
714                         write_pcr(0);
715                         clear_thread_flag(TIF_PERFCTR);
716                         err = 0;
717                 }
718                 break;
719
720         case PERFCTR_READ: {
721                 unsigned long pic, tmp;
722
723                 if (!test_thread_flag(TIF_PERFCTR)) {
724                         err = -EINVAL;
725                         break;
726                 }
727                 read_pic(pic);
728                 tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
729                 err |= __put_user(tmp, current_thread_info()->user_cntd0);
730                 tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
731                 err |= __put_user(tmp, current_thread_info()->user_cntd1);
732                 reset_pic();
733                 break;
734         }
735
736         case PERFCTR_CLRPIC:
737                 if (!test_thread_flag(TIF_PERFCTR)) {
738                         err = -EINVAL;
739                         break;
740                 }
741                 current_thread_info()->kernel_cntd0 =
742                         current_thread_info()->kernel_cntd1 = 0;
743                 reset_pic();
744                 break;
745
746         case PERFCTR_SETPCR: {
747                 u64 __user *user_pcr = (u64 __user *)arg0;
748
749                 if (!test_thread_flag(TIF_PERFCTR)) {
750                         err = -EINVAL;
751                         break;
752                 }
753                 err |= __get_user(current_thread_info()->pcr_reg, user_pcr);
754                 write_pcr(current_thread_info()->pcr_reg);
755                 current_thread_info()->kernel_cntd0 =
756                         current_thread_info()->kernel_cntd1 = 0;
757                 reset_pic();
758                 break;
759         }
760
761         case PERFCTR_GETPCR: {
762                 u64 __user *user_pcr = (u64 __user *)arg0;
763
764                 if (!test_thread_flag(TIF_PERFCTR)) {
765                         err = -EINVAL;
766                         break;
767                 }
768                 err |= __put_user(current_thread_info()->pcr_reg, user_pcr);
769                 break;
770         }
771
772         default:
773                 err = -EINVAL;
774                 break;
775         };
776         return err;
777 }