ptrace_64.c 27 KB

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