ptrace.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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. if (request == PTRACE_SPARC_DETACH)
  835. request = PTRACE_DETACH;
  836. ret = compat_ptrace_request(child, request, addr, data);
  837. break;
  838. }
  839. return ret;
  840. }
  841. #endif /* CONFIG_COMPAT */
  842. struct fps {
  843. unsigned int regs[64];
  844. unsigned long fsr;
  845. };
  846. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  847. {
  848. const struct user_regset_view *view = task_user_regset_view(current);
  849. unsigned long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
  850. struct pt_regs __user *pregs;
  851. struct fps __user *fps;
  852. int ret;
  853. pregs = (struct pt_regs __user *) (unsigned long) addr;
  854. fps = (struct fps __user *) (unsigned long) addr;
  855. switch (request) {
  856. case PTRACE_PEEKUSR:
  857. ret = (addr != 0) ? -EIO : 0;
  858. break;
  859. case PTRACE_GETREGS64:
  860. ret = copy_regset_to_user(child, view, REGSET_GENERAL,
  861. 1 * sizeof(u64),
  862. 15 * sizeof(u64),
  863. &pregs->u_regs[0]);
  864. if (!ret) {
  865. /* XXX doesn't handle 'y' register correctly XXX */
  866. ret = copy_regset_to_user(child, view, REGSET_GENERAL,
  867. 32 * sizeof(u64),
  868. 4 * sizeof(u64),
  869. &pregs->tstate);
  870. }
  871. break;
  872. case PTRACE_SETREGS64:
  873. ret = copy_regset_from_user(child, view, REGSET_GENERAL,
  874. 1 * sizeof(u64),
  875. 15 * sizeof(u64),
  876. &pregs->u_regs[0]);
  877. if (!ret) {
  878. /* XXX doesn't handle 'y' register correctly XXX */
  879. ret = copy_regset_from_user(child, view, REGSET_GENERAL,
  880. 32 * sizeof(u64),
  881. 4 * sizeof(u64),
  882. &pregs->tstate);
  883. }
  884. break;
  885. case PTRACE_GETFPREGS64:
  886. ret = copy_regset_to_user(child, view, REGSET_FP,
  887. 0 * sizeof(u64),
  888. 33 * sizeof(u64),
  889. fps);
  890. break;
  891. case PTRACE_SETFPREGS64:
  892. ret = copy_regset_to_user(child, view, REGSET_FP,
  893. 0 * sizeof(u64),
  894. 33 * sizeof(u64),
  895. fps);
  896. break;
  897. case PTRACE_READTEXT:
  898. case PTRACE_READDATA:
  899. ret = ptrace_readdata(child, addr,
  900. (char __user *)addr2, data);
  901. if (ret == data)
  902. ret = 0;
  903. else if (ret >= 0)
  904. ret = -EIO;
  905. break;
  906. case PTRACE_WRITETEXT:
  907. case PTRACE_WRITEDATA:
  908. ret = ptrace_writedata(child, (char __user *) addr2,
  909. addr, data);
  910. if (ret == data)
  911. ret = 0;
  912. else if (ret >= 0)
  913. ret = -EIO;
  914. break;
  915. default:
  916. if (request == PTRACE_SPARC_DETACH)
  917. request = PTRACE_DETACH;
  918. ret = ptrace_request(child, request, addr, data);
  919. break;
  920. }
  921. return ret;
  922. }
  923. asmlinkage void syscall_trace(struct pt_regs *regs, int syscall_exit_p)
  924. {
  925. /* do the secure computing check first */
  926. secure_computing(regs->u_regs[UREG_G1]);
  927. if (unlikely(current->audit_context) && syscall_exit_p) {
  928. unsigned long tstate = regs->tstate;
  929. int result = AUDITSC_SUCCESS;
  930. if (unlikely(tstate & (TSTATE_XCARRY | TSTATE_ICARRY)))
  931. result = AUDITSC_FAILURE;
  932. audit_syscall_exit(result, regs->u_regs[UREG_I0]);
  933. }
  934. if (!(current->ptrace & PT_PTRACED))
  935. goto out;
  936. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  937. goto out;
  938. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  939. ? 0x80 : 0));
  940. /*
  941. * this isn't the same as continuing with a signal, but it will do
  942. * for normal use. strace only continues with a signal if the
  943. * stopping signal is not SIGTRAP. -brl
  944. */
  945. if (current->exit_code) {
  946. send_sig(current->exit_code, current, 1);
  947. current->exit_code = 0;
  948. }
  949. out:
  950. if (unlikely(current->audit_context) && !syscall_exit_p)
  951. audit_syscall_entry((test_thread_flag(TIF_32BIT) ?
  952. AUDIT_ARCH_SPARC :
  953. AUDIT_ARCH_SPARC64),
  954. regs->u_regs[UREG_G1],
  955. regs->u_regs[UREG_I0],
  956. regs->u_regs[UREG_I1],
  957. regs->u_regs[UREG_I2],
  958. regs->u_regs[UREG_I3]);
  959. }