ptrace_64.c 25 KB

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