sys_sparc.c 17 KB

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