ptrace.c 29 KB

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