ptrace_64.c 25 KB

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