ptrace_64.c 27 KB

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