ptrace_64.c 28 KB

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