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