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