ptrace_64.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /* ptrace.c: Sparc process tracing support.
  2. *
  3. * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
  4. * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  5. *
  6. * Based upon code written by Ross Biro, Linus Torvalds, Bob Manson,
  7. * and David Mosberger.
  8. *
  9. * Added Linux support -miguel (weird, eh?, the original code was meant
  10. * to emulate SunOS).
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/errno.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/user.h>
  18. #include <linux/smp.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/security.h>
  21. #include <linux/seccomp.h>
  22. #include <linux/audit.h>
  23. #include <linux/signal.h>
  24. #include <linux/regset.h>
  25. #include <linux/tracehook.h>
  26. #include <linux/compat.h>
  27. #include <linux/elf.h>
  28. #include <asm/asi.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/system.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/psrcompat.h>
  33. #include <asm/visasm.h>
  34. #include <asm/spitfire.h>
  35. #include <asm/page.h>
  36. #include <asm/cpudata.h>
  37. #include <asm/cacheflush.h>
  38. #include "entry.h"
  39. /* #define ALLOW_INIT_TRACING */
  40. /*
  41. * Called by kernel/ptrace.c when detaching..
  42. *
  43. * Make sure single step bits etc are not set.
  44. */
  45. void ptrace_disable(struct task_struct *child)
  46. {
  47. /* nothing to do */
  48. }
  49. /* To get the necessary page struct, access_process_vm() first calls
  50. * get_user_pages(). This has done a flush_dcache_page() on the
  51. * accessed page. Then our caller (copy_{to,from}_user_page()) did
  52. * to memcpy to read/write the data from that page.
  53. *
  54. * Now, the only thing we have to do is:
  55. * 1) flush the D-cache if it's possible than an illegal alias
  56. * has been created
  57. * 2) flush the I-cache if this is pre-cheetah and we did a write
  58. */
  59. void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
  60. unsigned long uaddr, void *kaddr,
  61. unsigned long len, int write)
  62. {
  63. BUG_ON(len > PAGE_SIZE);
  64. if (tlb_type == hypervisor)
  65. return;
  66. preempt_disable();
  67. #ifdef DCACHE_ALIASING_POSSIBLE
  68. /* If bit 13 of the kernel address we used to access the
  69. * user page is the same as the virtual address that page
  70. * is mapped to in the user's address space, we can skip the
  71. * D-cache flush.
  72. */
  73. if ((uaddr ^ (unsigned long) kaddr) & (1UL << 13)) {
  74. unsigned long start = __pa(kaddr);
  75. unsigned long end = start + len;
  76. unsigned long dcache_line_size;
  77. dcache_line_size = local_cpu_data().dcache_line_size;
  78. if (tlb_type == spitfire) {
  79. for (; start < end; start += dcache_line_size)
  80. spitfire_put_dcache_tag(start & 0x3fe0, 0x0);
  81. } else {
  82. start &= ~(dcache_line_size - 1);
  83. for (; start < end; start += dcache_line_size)
  84. __asm__ __volatile__(
  85. "stxa %%g0, [%0] %1\n\t"
  86. "membar #Sync"
  87. : /* no outputs */
  88. : "r" (start),
  89. "i" (ASI_DCACHE_INVALIDATE));
  90. }
  91. }
  92. #endif
  93. if (write && tlb_type == spitfire) {
  94. unsigned long start = (unsigned long) kaddr;
  95. unsigned long end = start + len;
  96. unsigned long icache_line_size;
  97. icache_line_size = local_cpu_data().icache_line_size;
  98. for (; start < end; start += icache_line_size)
  99. flushi(start);
  100. }
  101. preempt_enable();
  102. }
  103. static int get_from_target(struct task_struct *target, unsigned long uaddr,
  104. void *kbuf, int len)
  105. {
  106. if (target == current) {
  107. if (copy_from_user(kbuf, (void __user *) uaddr, len))
  108. return -EFAULT;
  109. } else {
  110. int len2 = access_process_vm(target, uaddr, kbuf, len, 0);
  111. if (len2 != len)
  112. return -EFAULT;
  113. }
  114. return 0;
  115. }
  116. static int set_to_target(struct task_struct *target, unsigned long uaddr,
  117. void *kbuf, int len)
  118. {
  119. if (target == current) {
  120. if (copy_to_user((void __user *) uaddr, kbuf, len))
  121. return -EFAULT;
  122. } else {
  123. int len2 = access_process_vm(target, uaddr, kbuf, len, 1);
  124. if (len2 != len)
  125. return -EFAULT;
  126. }
  127. return 0;
  128. }
  129. static int regwindow64_get(struct task_struct *target,
  130. const struct pt_regs *regs,
  131. struct reg_window *wbuf)
  132. {
  133. unsigned long rw_addr = regs->u_regs[UREG_I6];
  134. if (test_tsk_thread_flag(current, TIF_32BIT)) {
  135. struct reg_window32 win32;
  136. int i;
  137. if (get_from_target(target, rw_addr, &win32, sizeof(win32)))
  138. return -EFAULT;
  139. for (i = 0; i < 8; i++)
  140. wbuf->locals[i] = win32.locals[i];
  141. for (i = 0; i < 8; i++)
  142. wbuf->ins[i] = win32.ins[i];
  143. } else {
  144. rw_addr += STACK_BIAS;
  145. if (get_from_target(target, rw_addr, wbuf, sizeof(*wbuf)))
  146. return -EFAULT;
  147. }
  148. return 0;
  149. }
  150. static int regwindow64_set(struct task_struct *target,
  151. const struct pt_regs *regs,
  152. struct reg_window *wbuf)
  153. {
  154. unsigned long rw_addr = regs->u_regs[UREG_I6];
  155. if (test_tsk_thread_flag(current, TIF_32BIT)) {
  156. struct reg_window32 win32;
  157. int i;
  158. for (i = 0; i < 8; i++)
  159. win32.locals[i] = wbuf->locals[i];
  160. for (i = 0; i < 8; i++)
  161. win32.ins[i] = wbuf->ins[i];
  162. if (set_to_target(target, rw_addr, &win32, sizeof(win32)))
  163. return -EFAULT;
  164. } else {
  165. rw_addr += STACK_BIAS;
  166. if (set_to_target(target, rw_addr, wbuf, sizeof(*wbuf)))
  167. return -EFAULT;
  168. }
  169. return 0;
  170. }
  171. enum sparc_regset {
  172. REGSET_GENERAL,
  173. REGSET_FP,
  174. };
  175. static int genregs64_get(struct task_struct *target,
  176. const struct user_regset *regset,
  177. unsigned int pos, unsigned int count,
  178. void *kbuf, void __user *ubuf)
  179. {
  180. const struct pt_regs *regs = task_pt_regs(target);
  181. int ret;
  182. if (target == current)
  183. flushw_user();
  184. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  185. regs->u_regs,
  186. 0, 16 * sizeof(u64));
  187. if (!ret && count && pos < (32 * sizeof(u64))) {
  188. struct reg_window window;
  189. if (regwindow64_get(target, regs, &window))
  190. return -EFAULT;
  191. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  192. &window,
  193. 16 * sizeof(u64),
  194. 32 * sizeof(u64));
  195. }
  196. if (!ret) {
  197. /* TSTATE, TPC, TNPC */
  198. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  199. &regs->tstate,
  200. 32 * sizeof(u64),
  201. 35 * sizeof(u64));
  202. }
  203. if (!ret) {
  204. unsigned long y = regs->y;
  205. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  206. &y,
  207. 35 * sizeof(u64),
  208. 36 * sizeof(u64));
  209. }
  210. if (!ret) {
  211. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  212. 36 * sizeof(u64), -1);
  213. }
  214. return ret;
  215. }
  216. static int genregs64_set(struct task_struct *target,
  217. const struct user_regset *regset,
  218. unsigned int pos, unsigned int count,
  219. const void *kbuf, const void __user *ubuf)
  220. {
  221. struct pt_regs *regs = task_pt_regs(target);
  222. int ret;
  223. if (target == current)
  224. flushw_user();
  225. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  226. regs->u_regs,
  227. 0, 16 * sizeof(u64));
  228. if (!ret && count && pos < (32 * sizeof(u64))) {
  229. struct reg_window window;
  230. if (regwindow64_get(target, regs, &window))
  231. return -EFAULT;
  232. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  233. &window,
  234. 16 * sizeof(u64),
  235. 32 * sizeof(u64));
  236. if (!ret &&
  237. regwindow64_set(target, regs, &window))
  238. return -EFAULT;
  239. }
  240. if (!ret && count > 0) {
  241. unsigned long tstate;
  242. /* TSTATE */
  243. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  244. &tstate,
  245. 32 * sizeof(u64),
  246. 33 * sizeof(u64));
  247. if (!ret) {
  248. /* Only the condition codes and the "in syscall"
  249. * state can be modified in the %tstate register.
  250. */
  251. tstate &= (TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
  252. regs->tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
  253. regs->tstate |= tstate;
  254. }
  255. }
  256. if (!ret) {
  257. /* TPC, TNPC */
  258. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  259. &regs->tpc,
  260. 33 * sizeof(u64),
  261. 35 * sizeof(u64));
  262. }
  263. if (!ret) {
  264. unsigned long y;
  265. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  266. &y,
  267. 35 * sizeof(u64),
  268. 36 * sizeof(u64));
  269. if (!ret)
  270. regs->y = y;
  271. }
  272. if (!ret)
  273. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  274. 36 * sizeof(u64), -1);
  275. return ret;
  276. }
  277. static int fpregs64_get(struct task_struct *target,
  278. const struct user_regset *regset,
  279. unsigned int pos, unsigned int count,
  280. void *kbuf, void __user *ubuf)
  281. {
  282. const unsigned long *fpregs = task_thread_info(target)->fpregs;
  283. unsigned long fprs, fsr, gsr;
  284. int ret;
  285. if (target == current)
  286. save_and_clear_fpu();
  287. fprs = task_thread_info(target)->fpsaved[0];
  288. if (fprs & FPRS_DL)
  289. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  290. fpregs,
  291. 0, 16 * sizeof(u64));
  292. else
  293. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  294. 0,
  295. 16 * sizeof(u64));
  296. if (!ret) {
  297. if (fprs & FPRS_DU)
  298. ret = user_regset_copyout(&pos, &count,
  299. &kbuf, &ubuf,
  300. fpregs + 16,
  301. 16 * sizeof(u64),
  302. 32 * sizeof(u64));
  303. else
  304. ret = user_regset_copyout_zero(&pos, &count,
  305. &kbuf, &ubuf,
  306. 16 * sizeof(u64),
  307. 32 * sizeof(u64));
  308. }
  309. if (fprs & FPRS_FEF) {
  310. fsr = task_thread_info(target)->xfsr[0];
  311. gsr = task_thread_info(target)->gsr[0];
  312. } else {
  313. fsr = gsr = 0;
  314. }
  315. if (!ret)
  316. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  317. &fsr,
  318. 32 * sizeof(u64),
  319. 33 * sizeof(u64));
  320. if (!ret)
  321. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  322. &gsr,
  323. 33 * sizeof(u64),
  324. 34 * sizeof(u64));
  325. if (!ret)
  326. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  327. &fprs,
  328. 34 * sizeof(u64),
  329. 35 * sizeof(u64));
  330. if (!ret)
  331. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  332. 35 * sizeof(u64), -1);
  333. return ret;
  334. }
  335. static int fpregs64_set(struct task_struct *target,
  336. const struct user_regset *regset,
  337. unsigned int pos, unsigned int count,
  338. const void *kbuf, const void __user *ubuf)
  339. {
  340. unsigned long *fpregs = task_thread_info(target)->fpregs;
  341. unsigned long fprs;
  342. int ret;
  343. if (target == current)
  344. save_and_clear_fpu();
  345. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  346. fpregs,
  347. 0, 32 * sizeof(u64));
  348. if (!ret)
  349. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  350. task_thread_info(target)->xfsr,
  351. 32 * sizeof(u64),
  352. 33 * sizeof(u64));
  353. if (!ret)
  354. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  355. task_thread_info(target)->gsr,
  356. 33 * sizeof(u64),
  357. 34 * sizeof(u64));
  358. fprs = task_thread_info(target)->fpsaved[0];
  359. if (!ret && count > 0) {
  360. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  361. &fprs,
  362. 34 * sizeof(u64),
  363. 35 * sizeof(u64));
  364. }
  365. fprs |= (FPRS_FEF | FPRS_DL | FPRS_DU);
  366. task_thread_info(target)->fpsaved[0] = fprs;
  367. if (!ret)
  368. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  369. 35 * sizeof(u64), -1);
  370. return ret;
  371. }
  372. static const struct user_regset sparc64_regsets[] = {
  373. /* Format is:
  374. * G0 --> G7
  375. * O0 --> O7
  376. * L0 --> L7
  377. * I0 --> I7
  378. * TSTATE, TPC, TNPC, Y
  379. */
  380. [REGSET_GENERAL] = {
  381. .core_note_type = NT_PRSTATUS,
  382. .n = 36,
  383. .size = sizeof(u64), .align = sizeof(u64),
  384. .get = genregs64_get, .set = genregs64_set
  385. },
  386. /* Format is:
  387. * F0 --> F63
  388. * FSR
  389. * GSR
  390. * FPRS
  391. */
  392. [REGSET_FP] = {
  393. .core_note_type = NT_PRFPREG,
  394. .n = 35,
  395. .size = sizeof(u64), .align = sizeof(u64),
  396. .get = fpregs64_get, .set = fpregs64_set
  397. },
  398. };
  399. static const struct user_regset_view user_sparc64_view = {
  400. .name = "sparc64", .e_machine = EM_SPARCV9,
  401. .regsets = sparc64_regsets, .n = ARRAY_SIZE(sparc64_regsets)
  402. };
  403. #ifdef CONFIG_COMPAT
  404. static int genregs32_get(struct task_struct *target,
  405. const struct user_regset *regset,
  406. unsigned int pos, unsigned int count,
  407. void *kbuf, void __user *ubuf)
  408. {
  409. const struct pt_regs *regs = task_pt_regs(target);
  410. compat_ulong_t __user *reg_window;
  411. compat_ulong_t *k = kbuf;
  412. compat_ulong_t __user *u = ubuf;
  413. compat_ulong_t reg;
  414. if (target == current)
  415. flushw_user();
  416. pos /= sizeof(reg);
  417. count /= sizeof(reg);
  418. if (kbuf) {
  419. for (; count > 0 && pos < 16; count--)
  420. *k++ = regs->u_regs[pos++];
  421. reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
  422. if (target == current) {
  423. for (; count > 0 && pos < 32; count--) {
  424. if (get_user(*k++, &reg_window[pos++]))
  425. return -EFAULT;
  426. }
  427. } else {
  428. for (; count > 0 && pos < 32; count--) {
  429. if (access_process_vm(target,
  430. (unsigned long)
  431. &reg_window[pos],
  432. k, sizeof(*k), 0)
  433. != sizeof(*k))
  434. return -EFAULT;
  435. k++;
  436. pos++;
  437. }
  438. }
  439. } else {
  440. for (; count > 0 && pos < 16; count--) {
  441. if (put_user((compat_ulong_t) regs->u_regs[pos++], u++))
  442. return -EFAULT;
  443. }
  444. reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
  445. if (target == current) {
  446. for (; count > 0 && pos < 32; count--) {
  447. if (get_user(reg, &reg_window[pos++]) ||
  448. put_user(reg, u++))
  449. return -EFAULT;
  450. }
  451. } else {
  452. for (; count > 0 && pos < 32; count--) {
  453. if (access_process_vm(target,
  454. (unsigned long)
  455. &reg_window[pos],
  456. &reg, sizeof(reg), 0)
  457. != sizeof(reg))
  458. return -EFAULT;
  459. if (access_process_vm(target,
  460. (unsigned long) u,
  461. &reg, sizeof(reg), 1)
  462. != sizeof(reg))
  463. return -EFAULT;
  464. pos++;
  465. u++;
  466. }
  467. }
  468. }
  469. while (count > 0) {
  470. switch (pos) {
  471. case 32: /* PSR */
  472. reg = tstate_to_psr(regs->tstate);
  473. break;
  474. case 33: /* PC */
  475. reg = regs->tpc;
  476. break;
  477. case 34: /* NPC */
  478. reg = regs->tnpc;
  479. break;
  480. case 35: /* Y */
  481. reg = regs->y;
  482. break;
  483. case 36: /* WIM */
  484. case 37: /* TBR */
  485. reg = 0;
  486. break;
  487. default:
  488. goto finish;
  489. }
  490. if (kbuf)
  491. *k++ = reg;
  492. else if (put_user(reg, u++))
  493. return -EFAULT;
  494. pos++;
  495. count--;
  496. }
  497. finish:
  498. pos *= sizeof(reg);
  499. count *= sizeof(reg);
  500. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  501. 38 * sizeof(reg), -1);
  502. }
  503. static int genregs32_set(struct task_struct *target,
  504. const struct user_regset *regset,
  505. unsigned int pos, unsigned int count,
  506. const void *kbuf, const void __user *ubuf)
  507. {
  508. struct pt_regs *regs = task_pt_regs(target);
  509. compat_ulong_t __user *reg_window;
  510. const compat_ulong_t *k = kbuf;
  511. const compat_ulong_t __user *u = ubuf;
  512. compat_ulong_t reg;
  513. if (target == current)
  514. flushw_user();
  515. pos /= sizeof(reg);
  516. count /= sizeof(reg);
  517. if (kbuf) {
  518. for (; count > 0 && pos < 16; count--)
  519. regs->u_regs[pos++] = *k++;
  520. reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
  521. if (target == current) {
  522. for (; count > 0 && pos < 32; count--) {
  523. if (put_user(*k++, &reg_window[pos++]))
  524. return -EFAULT;
  525. }
  526. } else {
  527. for (; count > 0 && pos < 32; count--) {
  528. if (access_process_vm(target,
  529. (unsigned long)
  530. &reg_window[pos],
  531. (void *) k,
  532. sizeof(*k), 1)
  533. != sizeof(*k))
  534. return -EFAULT;
  535. k++;
  536. pos++;
  537. }
  538. }
  539. } else {
  540. for (; count > 0 && pos < 16; count--) {
  541. if (get_user(reg, u++))
  542. return -EFAULT;
  543. regs->u_regs[pos++] = reg;
  544. }
  545. reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
  546. if (target == current) {
  547. for (; count > 0 && pos < 32; count--) {
  548. if (get_user(reg, u++) ||
  549. put_user(reg, &reg_window[pos++]))
  550. return -EFAULT;
  551. }
  552. } else {
  553. for (; count > 0 && pos < 32; count--) {
  554. if (access_process_vm(target,
  555. (unsigned long)
  556. u,
  557. &reg, sizeof(reg), 0)
  558. != sizeof(reg))
  559. return -EFAULT;
  560. if (access_process_vm(target,
  561. (unsigned long)
  562. &reg_window[pos],
  563. &reg, sizeof(reg), 1)
  564. != sizeof(reg))
  565. return -EFAULT;
  566. pos++;
  567. u++;
  568. }
  569. }
  570. }
  571. while (count > 0) {
  572. unsigned long tstate;
  573. if (kbuf)
  574. reg = *k++;
  575. else if (get_user(reg, u++))
  576. return -EFAULT;
  577. switch (pos) {
  578. case 32: /* PSR */
  579. tstate = regs->tstate;
  580. tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
  581. tstate |= psr_to_tstate_icc(reg);
  582. if (reg & PSR_SYSCALL)
  583. tstate |= TSTATE_SYSCALL;
  584. regs->tstate = tstate;
  585. break;
  586. case 33: /* PC */
  587. regs->tpc = reg;
  588. break;
  589. case 34: /* NPC */
  590. regs->tnpc = reg;
  591. break;
  592. case 35: /* Y */
  593. regs->y = reg;
  594. break;
  595. case 36: /* WIM */
  596. case 37: /* TBR */
  597. break;
  598. default:
  599. goto finish;
  600. }
  601. pos++;
  602. count--;
  603. }
  604. finish:
  605. pos *= sizeof(reg);
  606. count *= sizeof(reg);
  607. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  608. 38 * sizeof(reg), -1);
  609. }
  610. static int fpregs32_get(struct task_struct *target,
  611. const struct user_regset *regset,
  612. unsigned int pos, unsigned int count,
  613. void *kbuf, void __user *ubuf)
  614. {
  615. const unsigned long *fpregs = task_thread_info(target)->fpregs;
  616. compat_ulong_t enabled;
  617. unsigned long fprs;
  618. compat_ulong_t fsr;
  619. int ret = 0;
  620. if (target == current)
  621. save_and_clear_fpu();
  622. fprs = task_thread_info(target)->fpsaved[0];
  623. if (fprs & FPRS_FEF) {
  624. fsr = task_thread_info(target)->xfsr[0];
  625. enabled = 1;
  626. } else {
  627. fsr = 0;
  628. enabled = 0;
  629. }
  630. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  631. fpregs,
  632. 0, 32 * sizeof(u32));
  633. if (!ret)
  634. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  635. 32 * sizeof(u32),
  636. 33 * sizeof(u32));
  637. if (!ret)
  638. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  639. &fsr,
  640. 33 * sizeof(u32),
  641. 34 * sizeof(u32));
  642. if (!ret) {
  643. compat_ulong_t val;
  644. val = (enabled << 8) | (8 << 16);
  645. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  646. &val,
  647. 34 * sizeof(u32),
  648. 35 * sizeof(u32));
  649. }
  650. if (!ret)
  651. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  652. 35 * sizeof(u32), -1);
  653. return ret;
  654. }
  655. static int fpregs32_set(struct task_struct *target,
  656. const struct user_regset *regset,
  657. unsigned int pos, unsigned int count,
  658. const void *kbuf, const void __user *ubuf)
  659. {
  660. unsigned long *fpregs = task_thread_info(target)->fpregs;
  661. unsigned long fprs;
  662. int ret;
  663. if (target == current)
  664. save_and_clear_fpu();
  665. fprs = task_thread_info(target)->fpsaved[0];
  666. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  667. fpregs,
  668. 0, 32 * sizeof(u32));
  669. if (!ret)
  670. user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  671. 32 * sizeof(u32),
  672. 33 * sizeof(u32));
  673. if (!ret && count > 0) {
  674. compat_ulong_t fsr;
  675. unsigned long val;
  676. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  677. &fsr,
  678. 33 * sizeof(u32),
  679. 34 * sizeof(u32));
  680. if (!ret) {
  681. val = task_thread_info(target)->xfsr[0];
  682. val &= 0xffffffff00000000UL;
  683. val |= fsr;
  684. task_thread_info(target)->xfsr[0] = val;
  685. }
  686. }
  687. fprs |= (FPRS_FEF | FPRS_DL);
  688. task_thread_info(target)->fpsaved[0] = fprs;
  689. if (!ret)
  690. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  691. 34 * sizeof(u32), -1);
  692. return ret;
  693. }
  694. static const struct user_regset sparc32_regsets[] = {
  695. /* Format is:
  696. * G0 --> G7
  697. * O0 --> O7
  698. * L0 --> L7
  699. * I0 --> I7
  700. * PSR, PC, nPC, Y, WIM, TBR
  701. */
  702. [REGSET_GENERAL] = {
  703. .core_note_type = NT_PRSTATUS,
  704. .n = 38,
  705. .size = sizeof(u32), .align = sizeof(u32),
  706. .get = genregs32_get, .set = genregs32_set
  707. },
  708. /* Format is:
  709. * F0 --> F31
  710. * empty 32-bit word
  711. * FSR (32--bit word)
  712. * FPU QUEUE COUNT (8-bit char)
  713. * FPU QUEUE ENTRYSIZE (8-bit char)
  714. * FPU ENABLED (8-bit char)
  715. * empty 8-bit char
  716. * FPU QUEUE (64 32-bit ints)
  717. */
  718. [REGSET_FP] = {
  719. .core_note_type = NT_PRFPREG,
  720. .n = 99,
  721. .size = sizeof(u32), .align = sizeof(u32),
  722. .get = fpregs32_get, .set = fpregs32_set
  723. },
  724. };
  725. static const struct user_regset_view user_sparc32_view = {
  726. .name = "sparc", .e_machine = EM_SPARC,
  727. .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
  728. };
  729. #endif /* CONFIG_COMPAT */
  730. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  731. {
  732. #ifdef CONFIG_COMPAT
  733. if (test_tsk_thread_flag(task, TIF_32BIT))
  734. return &user_sparc32_view;
  735. #endif
  736. return &user_sparc64_view;
  737. }
  738. #ifdef CONFIG_COMPAT
  739. struct compat_fps {
  740. unsigned int regs[32];
  741. unsigned int fsr;
  742. unsigned int flags;
  743. unsigned int extra;
  744. unsigned int fpqd;
  745. struct compat_fq {
  746. unsigned int insnaddr;
  747. unsigned int insn;
  748. } fpq[16];
  749. };
  750. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  751. compat_ulong_t caddr, compat_ulong_t cdata)
  752. {
  753. const struct user_regset_view *view = task_user_regset_view(current);
  754. compat_ulong_t caddr2 = task_pt_regs(current)->u_regs[UREG_I4];
  755. struct pt_regs32 __user *pregs;
  756. struct compat_fps __user *fps;
  757. unsigned long addr2 = caddr2;
  758. unsigned long addr = caddr;
  759. unsigned long data = cdata;
  760. int ret;
  761. pregs = (struct pt_regs32 __user *) addr;
  762. fps = (struct compat_fps __user *) addr;
  763. switch (request) {
  764. case PTRACE_PEEKUSR:
  765. ret = (addr != 0) ? -EIO : 0;
  766. break;
  767. case PTRACE_GETREGS:
  768. ret = copy_regset_to_user(child, view, REGSET_GENERAL,
  769. 32 * sizeof(u32),
  770. 4 * sizeof(u32),
  771. &pregs->psr);
  772. if (!ret)
  773. ret = copy_regset_to_user(child, view, REGSET_GENERAL,
  774. 1 * sizeof(u32),
  775. 15 * sizeof(u32),
  776. &pregs->u_regs[0]);
  777. break;
  778. case PTRACE_SETREGS:
  779. ret = copy_regset_from_user(child, view, REGSET_GENERAL,
  780. 32 * sizeof(u32),
  781. 4 * sizeof(u32),
  782. &pregs->psr);
  783. if (!ret)
  784. ret = copy_regset_from_user(child, view, REGSET_GENERAL,
  785. 1 * sizeof(u32),
  786. 15 * sizeof(u32),
  787. &pregs->u_regs[0]);
  788. break;
  789. case PTRACE_GETFPREGS:
  790. ret = copy_regset_to_user(child, view, REGSET_FP,
  791. 0 * sizeof(u32),
  792. 32 * sizeof(u32),
  793. &fps->regs[0]);
  794. if (!ret)
  795. ret = copy_regset_to_user(child, view, REGSET_FP,
  796. 33 * sizeof(u32),
  797. 1 * sizeof(u32),
  798. &fps->fsr);
  799. if (!ret) {
  800. if (__put_user(0, &fps->flags) ||
  801. __put_user(0, &fps->extra) ||
  802. __put_user(0, &fps->fpqd) ||
  803. clear_user(&fps->fpq[0], 32 * sizeof(unsigned int)))
  804. ret = -EFAULT;
  805. }
  806. break;
  807. case PTRACE_SETFPREGS:
  808. ret = copy_regset_from_user(child, view, REGSET_FP,
  809. 0 * sizeof(u32),
  810. 32 * sizeof(u32),
  811. &fps->regs[0]);
  812. if (!ret)
  813. ret = copy_regset_from_user(child, view, REGSET_FP,
  814. 33 * sizeof(u32),
  815. 1 * sizeof(u32),
  816. &fps->fsr);
  817. break;
  818. case PTRACE_READTEXT:
  819. case PTRACE_READDATA:
  820. ret = ptrace_readdata(child, addr,
  821. (char __user *)addr2, data);
  822. if (ret == data)
  823. ret = 0;
  824. else if (ret >= 0)
  825. ret = -EIO;
  826. break;
  827. case PTRACE_WRITETEXT:
  828. case PTRACE_WRITEDATA:
  829. ret = ptrace_writedata(child, (char __user *) addr2,
  830. addr, data);
  831. if (ret == data)
  832. ret = 0;
  833. else if (ret >= 0)
  834. ret = -EIO;
  835. break;
  836. default:
  837. if (request == PTRACE_SPARC_DETACH)
  838. request = PTRACE_DETACH;
  839. ret = compat_ptrace_request(child, request, addr, data);
  840. break;
  841. }
  842. return ret;
  843. }
  844. #endif /* CONFIG_COMPAT */
  845. struct fps {
  846. unsigned int regs[64];
  847. unsigned long fsr;
  848. };
  849. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  850. {
  851. const struct user_regset_view *view = task_user_regset_view(current);
  852. unsigned long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
  853. struct pt_regs __user *pregs;
  854. struct fps __user *fps;
  855. int ret;
  856. pregs = (struct pt_regs __user *) (unsigned long) addr;
  857. fps = (struct fps __user *) (unsigned long) addr;
  858. switch (request) {
  859. case PTRACE_PEEKUSR:
  860. ret = (addr != 0) ? -EIO : 0;
  861. break;
  862. case PTRACE_GETREGS64:
  863. ret = copy_regset_to_user(child, view, REGSET_GENERAL,
  864. 1 * sizeof(u64),
  865. 15 * sizeof(u64),
  866. &pregs->u_regs[0]);
  867. if (!ret) {
  868. /* XXX doesn't handle 'y' register correctly XXX */
  869. ret = copy_regset_to_user(child, view, REGSET_GENERAL,
  870. 32 * sizeof(u64),
  871. 4 * sizeof(u64),
  872. &pregs->tstate);
  873. }
  874. break;
  875. case PTRACE_SETREGS64:
  876. ret = copy_regset_from_user(child, view, REGSET_GENERAL,
  877. 1 * sizeof(u64),
  878. 15 * sizeof(u64),
  879. &pregs->u_regs[0]);
  880. if (!ret) {
  881. /* XXX doesn't handle 'y' register correctly XXX */
  882. ret = copy_regset_from_user(child, view, REGSET_GENERAL,
  883. 32 * sizeof(u64),
  884. 4 * sizeof(u64),
  885. &pregs->tstate);
  886. }
  887. break;
  888. case PTRACE_GETFPREGS64:
  889. ret = copy_regset_to_user(child, view, REGSET_FP,
  890. 0 * sizeof(u64),
  891. 33 * sizeof(u64),
  892. fps);
  893. break;
  894. case PTRACE_SETFPREGS64:
  895. ret = copy_regset_from_user(child, view, REGSET_FP,
  896. 0 * sizeof(u64),
  897. 33 * sizeof(u64),
  898. fps);
  899. break;
  900. case PTRACE_READTEXT:
  901. case PTRACE_READDATA:
  902. ret = ptrace_readdata(child, addr,
  903. (char __user *)addr2, data);
  904. if (ret == data)
  905. ret = 0;
  906. else if (ret >= 0)
  907. ret = -EIO;
  908. break;
  909. case PTRACE_WRITETEXT:
  910. case PTRACE_WRITEDATA:
  911. ret = ptrace_writedata(child, (char __user *) addr2,
  912. addr, data);
  913. if (ret == data)
  914. ret = 0;
  915. else if (ret >= 0)
  916. ret = -EIO;
  917. break;
  918. default:
  919. if (request == PTRACE_SPARC_DETACH)
  920. request = PTRACE_DETACH;
  921. ret = ptrace_request(child, request, addr, data);
  922. break;
  923. }
  924. return ret;
  925. }
  926. asmlinkage int syscall_trace_enter(struct pt_regs *regs)
  927. {
  928. int ret = 0;
  929. /* do the secure computing check first */
  930. secure_computing(regs->u_regs[UREG_G1]);
  931. if (test_thread_flag(TIF_SYSCALL_TRACE))
  932. ret = tracehook_report_syscall_entry(regs);
  933. if (unlikely(current->audit_context) && !ret)
  934. audit_syscall_entry((test_thread_flag(TIF_32BIT) ?
  935. AUDIT_ARCH_SPARC :
  936. AUDIT_ARCH_SPARC64),
  937. regs->u_regs[UREG_G1],
  938. regs->u_regs[UREG_I0],
  939. regs->u_regs[UREG_I1],
  940. regs->u_regs[UREG_I2],
  941. regs->u_regs[UREG_I3]);
  942. return ret;
  943. }
  944. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  945. {
  946. if (unlikely(current->audit_context)) {
  947. unsigned long tstate = regs->tstate;
  948. int result = AUDITSC_SUCCESS;
  949. if (unlikely(tstate & (TSTATE_XCARRY | TSTATE_ICARRY)))
  950. result = AUDITSC_FAILURE;
  951. audit_syscall_exit(result, regs->u_regs[UREG_I0]);
  952. }
  953. if (test_thread_flag(TIF_SYSCALL_TRACE))
  954. tracehook_report_syscall_exit(regs, 0);
  955. }