sys_sparc.c 17 KB

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