ptrace_64.c 25 KB

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