ptrace_64.c 25 KB

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