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