ptrace.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Kernel support for the ptrace() and syscall tracing interfaces.
  4. *
  5. * Copyright (C) 2000 Hewlett-Packard Co, Linuxcare Inc.
  6. * Copyright (C) 2000 Matthew Wilcox <matthew@wil.cx>
  7. * Copyright (C) 2000 David Huggins-Daines <dhd@debian.org>
  8. * Copyright (C) 2008-2016 Helge Deller <deller@gmx.de>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/sched.h>
  12. #include <linux/mm.h>
  13. #include <linux/smp.h>
  14. #include <linux/elf.h>
  15. #include <linux/errno.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/tracehook.h>
  18. #include <linux/user.h>
  19. #include <linux/personality.h>
  20. #include <linux/regset.h>
  21. #include <linux/security.h>
  22. #include <linux/seccomp.h>
  23. #include <linux/compat.h>
  24. #include <linux/signal.h>
  25. #include <linux/audit.h>
  26. #include <linux/uaccess.h>
  27. #include <asm/processor.h>
  28. #include <asm/asm-offsets.h>
  29. /* PSW bits we allow the debugger to modify */
  30. #define USER_PSW_BITS (PSW_N | PSW_B | PSW_V | PSW_CB)
  31. #define CREATE_TRACE_POINTS
  32. #include <trace/events/syscalls.h>
  33. /*
  34. * These are our native regset flavors.
  35. */
  36. enum parisc_regset {
  37. REGSET_GENERAL,
  38. REGSET_FP
  39. };
  40. /*
  41. * Called by kernel/ptrace.c when detaching..
  42. *
  43. * Make sure single step bits etc are not set.
  44. */
  45. void ptrace_disable(struct task_struct *task)
  46. {
  47. clear_tsk_thread_flag(task, TIF_SINGLESTEP);
  48. clear_tsk_thread_flag(task, TIF_BLOCKSTEP);
  49. /* make sure the trap bits are not set */
  50. pa_psw(task)->r = 0;
  51. pa_psw(task)->t = 0;
  52. pa_psw(task)->h = 0;
  53. pa_psw(task)->l = 0;
  54. }
  55. /*
  56. * The following functions are called by ptrace_resume() when
  57. * enabling or disabling single/block tracing.
  58. */
  59. void user_disable_single_step(struct task_struct *task)
  60. {
  61. ptrace_disable(task);
  62. }
  63. void user_enable_single_step(struct task_struct *task)
  64. {
  65. clear_tsk_thread_flag(task, TIF_BLOCKSTEP);
  66. set_tsk_thread_flag(task, TIF_SINGLESTEP);
  67. if (pa_psw(task)->n) {
  68. /* Nullified, just crank over the queue. */
  69. task_regs(task)->iaoq[0] = task_regs(task)->iaoq[1];
  70. task_regs(task)->iasq[0] = task_regs(task)->iasq[1];
  71. task_regs(task)->iaoq[1] = task_regs(task)->iaoq[0] + 4;
  72. pa_psw(task)->n = 0;
  73. pa_psw(task)->x = 0;
  74. pa_psw(task)->y = 0;
  75. pa_psw(task)->z = 0;
  76. pa_psw(task)->b = 0;
  77. ptrace_disable(task);
  78. /* Don't wake up the task, but let the
  79. parent know something happened. */
  80. force_sig_fault_to_task(SIGTRAP, TRAP_TRACE,
  81. (void __user *) (task_regs(task)->iaoq[0] & ~3),
  82. task);
  83. /* notify_parent(task, SIGCHLD); */
  84. return;
  85. }
  86. /* Enable recovery counter traps. The recovery counter
  87. * itself will be set to zero on a task switch. If the
  88. * task is suspended on a syscall then the syscall return
  89. * path will overwrite the recovery counter with a suitable
  90. * value such that it traps once back in user space. We
  91. * disable interrupts in the tasks PSW here also, to avoid
  92. * interrupts while the recovery counter is decrementing.
  93. */
  94. pa_psw(task)->r = 1;
  95. pa_psw(task)->t = 0;
  96. pa_psw(task)->h = 0;
  97. pa_psw(task)->l = 0;
  98. }
  99. void user_enable_block_step(struct task_struct *task)
  100. {
  101. clear_tsk_thread_flag(task, TIF_SINGLESTEP);
  102. set_tsk_thread_flag(task, TIF_BLOCKSTEP);
  103. /* Enable taken branch trap. */
  104. pa_psw(task)->r = 0;
  105. pa_psw(task)->t = 1;
  106. pa_psw(task)->h = 0;
  107. pa_psw(task)->l = 0;
  108. }
  109. long arch_ptrace(struct task_struct *child, long request,
  110. unsigned long addr, unsigned long data)
  111. {
  112. unsigned long __user *datap = (unsigned long __user *)data;
  113. unsigned long tmp;
  114. long ret = -EIO;
  115. switch (request) {
  116. /* Read the word at location addr in the USER area. For ptraced
  117. processes, the kernel saves all regs on a syscall. */
  118. case PTRACE_PEEKUSR:
  119. if ((addr & (sizeof(unsigned long)-1)) ||
  120. addr >= sizeof(struct pt_regs))
  121. break;
  122. tmp = *(unsigned long *) ((char *) task_regs(child) + addr);
  123. ret = put_user(tmp, datap);
  124. break;
  125. /* Write the word at location addr in the USER area. This will need
  126. to change when the kernel no longer saves all regs on a syscall.
  127. FIXME. There is a problem at the moment in that r3-r18 are only
  128. saved if the process is ptraced on syscall entry, and even then
  129. those values are overwritten by actual register values on syscall
  130. exit. */
  131. case PTRACE_POKEUSR:
  132. /* Some register values written here may be ignored in
  133. * entry.S:syscall_restore_rfi; e.g. iaoq is written with
  134. * r31/r31+4, and not with the values in pt_regs.
  135. */
  136. if (addr == PT_PSW) {
  137. /* Allow writing to Nullify, Divide-step-correction,
  138. * and carry/borrow bits.
  139. * BEWARE, if you set N, and then single step, it won't
  140. * stop on the nullified instruction.
  141. */
  142. data &= USER_PSW_BITS;
  143. task_regs(child)->gr[0] &= ~USER_PSW_BITS;
  144. task_regs(child)->gr[0] |= data;
  145. ret = 0;
  146. break;
  147. }
  148. if ((addr & (sizeof(unsigned long)-1)) ||
  149. addr >= sizeof(struct pt_regs))
  150. break;
  151. if (addr == PT_IAOQ0 || addr == PT_IAOQ1) {
  152. data |= 3; /* ensure userspace privilege */
  153. }
  154. if ((addr >= PT_GR1 && addr <= PT_GR31) ||
  155. addr == PT_IAOQ0 || addr == PT_IAOQ1 ||
  156. (addr >= PT_FR0 && addr <= PT_FR31 + 4) ||
  157. addr == PT_SAR) {
  158. *(unsigned long *) ((char *) task_regs(child) + addr) = data;
  159. ret = 0;
  160. }
  161. break;
  162. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  163. return copy_regset_to_user(child,
  164. task_user_regset_view(current),
  165. REGSET_GENERAL,
  166. 0, sizeof(struct user_regs_struct),
  167. datap);
  168. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  169. return copy_regset_from_user(child,
  170. task_user_regset_view(current),
  171. REGSET_GENERAL,
  172. 0, sizeof(struct user_regs_struct),
  173. datap);
  174. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  175. return copy_regset_to_user(child,
  176. task_user_regset_view(current),
  177. REGSET_FP,
  178. 0, sizeof(struct user_fp_struct),
  179. datap);
  180. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  181. return copy_regset_from_user(child,
  182. task_user_regset_view(current),
  183. REGSET_FP,
  184. 0, sizeof(struct user_fp_struct),
  185. datap);
  186. default:
  187. ret = ptrace_request(child, request, addr, data);
  188. break;
  189. }
  190. return ret;
  191. }
  192. #ifdef CONFIG_COMPAT
  193. /* This function is needed to translate 32 bit pt_regs offsets in to
  194. * 64 bit pt_regs offsets. For example, a 32 bit gdb under a 64 bit kernel
  195. * will request offset 12 if it wants gr3, but the lower 32 bits of
  196. * the 64 bit kernels view of gr3 will be at offset 28 (3*8 + 4).
  197. * This code relies on a 32 bit pt_regs being comprised of 32 bit values
  198. * except for the fp registers which (a) are 64 bits, and (b) follow
  199. * the gr registers at the start of pt_regs. The 32 bit pt_regs should
  200. * be half the size of the 64 bit pt_regs, plus 32*4 to allow for fr[]
  201. * being 64 bit in both cases.
  202. */
  203. static compat_ulong_t translate_usr_offset(compat_ulong_t offset)
  204. {
  205. compat_ulong_t pos;
  206. if (offset < 32*4) /* gr[0..31] */
  207. pos = offset * 2 + 4;
  208. else if (offset < 32*4+32*8) /* fr[0] ... fr[31] */
  209. pos = (offset - 32*4) + PT_FR0;
  210. else if (offset < sizeof(struct pt_regs)/2 + 32*4) /* sr[0] ... ipsw */
  211. pos = (offset - 32*4 - 32*8) * 2 + PT_SR0 + 4;
  212. else
  213. pos = sizeof(struct pt_regs);
  214. return pos;
  215. }
  216. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  217. compat_ulong_t addr, compat_ulong_t data)
  218. {
  219. compat_uint_t tmp;
  220. long ret = -EIO;
  221. switch (request) {
  222. case PTRACE_PEEKUSR:
  223. if (addr & (sizeof(compat_uint_t)-1))
  224. break;
  225. addr = translate_usr_offset(addr);
  226. if (addr >= sizeof(struct pt_regs))
  227. break;
  228. tmp = *(compat_uint_t *) ((char *) task_regs(child) + addr);
  229. ret = put_user(tmp, (compat_uint_t *) (unsigned long) data);
  230. break;
  231. /* Write the word at location addr in the USER area. This will need
  232. to change when the kernel no longer saves all regs on a syscall.
  233. FIXME. There is a problem at the moment in that r3-r18 are only
  234. saved if the process is ptraced on syscall entry, and even then
  235. those values are overwritten by actual register values on syscall
  236. exit. */
  237. case PTRACE_POKEUSR:
  238. /* Some register values written here may be ignored in
  239. * entry.S:syscall_restore_rfi; e.g. iaoq is written with
  240. * r31/r31+4, and not with the values in pt_regs.
  241. */
  242. if (addr == PT_PSW) {
  243. /* Since PT_PSW==0, it is valid for 32 bit processes
  244. * under 64 bit kernels as well.
  245. */
  246. ret = arch_ptrace(child, request, addr, data);
  247. } else {
  248. if (addr & (sizeof(compat_uint_t)-1))
  249. break;
  250. addr = translate_usr_offset(addr);
  251. if (addr >= sizeof(struct pt_regs))
  252. break;
  253. if (addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4) {
  254. data |= 3; /* ensure userspace privilege */
  255. }
  256. if (addr >= PT_FR0 && addr <= PT_FR31 + 4) {
  257. /* Special case, fp regs are 64 bits anyway */
  258. *(__u32 *) ((char *) task_regs(child) + addr) = data;
  259. ret = 0;
  260. }
  261. else if ((addr >= PT_GR1+4 && addr <= PT_GR31+4) ||
  262. addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4 ||
  263. addr == PT_SAR+4) {
  264. /* Zero the top 32 bits */
  265. *(__u32 *) ((char *) task_regs(child) + addr - 4) = 0;
  266. *(__u32 *) ((char *) task_regs(child) + addr) = data;
  267. ret = 0;
  268. }
  269. }
  270. break;
  271. default:
  272. ret = compat_ptrace_request(child, request, addr, data);
  273. break;
  274. }
  275. return ret;
  276. }
  277. #endif
  278. long do_syscall_trace_enter(struct pt_regs *regs)
  279. {
  280. if (test_thread_flag(TIF_SYSCALL_TRACE)) {
  281. int rc = tracehook_report_syscall_entry(regs);
  282. /*
  283. * As tracesys_next does not set %r28 to -ENOSYS
  284. * when %r20 is set to -1, initialize it here.
  285. */
  286. regs->gr[28] = -ENOSYS;
  287. if (rc) {
  288. /*
  289. * A nonzero return code from
  290. * tracehook_report_syscall_entry() tells us
  291. * to prevent the syscall execution. Skip
  292. * the syscall call and the syscall restart handling.
  293. *
  294. * Note that the tracer may also just change
  295. * regs->gr[20] to an invalid syscall number,
  296. * that is handled by tracesys_next.
  297. */
  298. regs->gr[20] = -1UL;
  299. return -1;
  300. }
  301. }
  302. /* Do the secure computing check after ptrace. */
  303. if (secure_computing() == -1)
  304. return -1;
  305. #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
  306. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  307. trace_sys_enter(regs, regs->gr[20]);
  308. #endif
  309. #ifdef CONFIG_64BIT
  310. if (!is_compat_task())
  311. audit_syscall_entry(regs->gr[20], regs->gr[26], regs->gr[25],
  312. regs->gr[24], regs->gr[23]);
  313. else
  314. #endif
  315. audit_syscall_entry(regs->gr[20] & 0xffffffff,
  316. regs->gr[26] & 0xffffffff,
  317. regs->gr[25] & 0xffffffff,
  318. regs->gr[24] & 0xffffffff,
  319. regs->gr[23] & 0xffffffff);
  320. /*
  321. * Sign extend the syscall number to 64bit since it may have been
  322. * modified by a compat ptrace call
  323. */
  324. return (int) ((u32) regs->gr[20]);
  325. }
  326. void do_syscall_trace_exit(struct pt_regs *regs)
  327. {
  328. int stepping = test_thread_flag(TIF_SINGLESTEP) ||
  329. test_thread_flag(TIF_BLOCKSTEP);
  330. audit_syscall_exit(regs);
  331. #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
  332. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  333. trace_sys_exit(regs, regs->gr[20]);
  334. #endif
  335. if (stepping || test_thread_flag(TIF_SYSCALL_TRACE))
  336. tracehook_report_syscall_exit(regs, stepping);
  337. }
  338. /*
  339. * regset functions.
  340. */
  341. static int fpr_get(struct task_struct *target,
  342. const struct user_regset *regset,
  343. struct membuf to)
  344. {
  345. struct pt_regs *regs = task_regs(target);
  346. return membuf_write(&to, regs->fr, ELF_NFPREG * sizeof(__u64));
  347. }
  348. static int fpr_set(struct task_struct *target,
  349. const struct user_regset *regset,
  350. unsigned int pos, unsigned int count,
  351. const void *kbuf, const void __user *ubuf)
  352. {
  353. struct pt_regs *regs = task_regs(target);
  354. const __u64 *k = kbuf;
  355. const __u64 __user *u = ubuf;
  356. __u64 reg;
  357. pos /= sizeof(reg);
  358. count /= sizeof(reg);
  359. if (kbuf)
  360. for (; count > 0 && pos < ELF_NFPREG; --count)
  361. regs->fr[pos++] = *k++;
  362. else
  363. for (; count > 0 && pos < ELF_NFPREG; --count) {
  364. if (__get_user(reg, u++))
  365. return -EFAULT;
  366. regs->fr[pos++] = reg;
  367. }
  368. kbuf = k;
  369. ubuf = u;
  370. pos *= sizeof(reg);
  371. count *= sizeof(reg);
  372. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  373. ELF_NFPREG * sizeof(reg), -1);
  374. }
  375. #define RI(reg) (offsetof(struct user_regs_struct,reg) / sizeof(long))
  376. static unsigned long get_reg(struct pt_regs *regs, int num)
  377. {
  378. switch (num) {
  379. case RI(gr[0]) ... RI(gr[31]): return regs->gr[num - RI(gr[0])];
  380. case RI(sr[0]) ... RI(sr[7]): return regs->sr[num - RI(sr[0])];
  381. case RI(iasq[0]): return regs->iasq[0];
  382. case RI(iasq[1]): return regs->iasq[1];
  383. case RI(iaoq[0]): return regs->iaoq[0];
  384. case RI(iaoq[1]): return regs->iaoq[1];
  385. case RI(sar): return regs->sar;
  386. case RI(iir): return regs->iir;
  387. case RI(isr): return regs->isr;
  388. case RI(ior): return regs->ior;
  389. case RI(ipsw): return regs->ipsw;
  390. case RI(cr27): return regs->cr27;
  391. case RI(cr0): return mfctl(0);
  392. case RI(cr24): return mfctl(24);
  393. case RI(cr25): return mfctl(25);
  394. case RI(cr26): return mfctl(26);
  395. case RI(cr28): return mfctl(28);
  396. case RI(cr29): return mfctl(29);
  397. case RI(cr30): return mfctl(30);
  398. case RI(cr31): return mfctl(31);
  399. case RI(cr8): return mfctl(8);
  400. case RI(cr9): return mfctl(9);
  401. case RI(cr12): return mfctl(12);
  402. case RI(cr13): return mfctl(13);
  403. case RI(cr10): return mfctl(10);
  404. case RI(cr15): return mfctl(15);
  405. default: return 0;
  406. }
  407. }
  408. static void set_reg(struct pt_regs *regs, int num, unsigned long val)
  409. {
  410. switch (num) {
  411. case RI(gr[0]): /*
  412. * PSW is in gr[0].
  413. * Allow writing to Nullify, Divide-step-correction,
  414. * and carry/borrow bits.
  415. * BEWARE, if you set N, and then single step, it won't
  416. * stop on the nullified instruction.
  417. */
  418. val &= USER_PSW_BITS;
  419. regs->gr[0] &= ~USER_PSW_BITS;
  420. regs->gr[0] |= val;
  421. return;
  422. case RI(gr[1]) ... RI(gr[31]):
  423. regs->gr[num - RI(gr[0])] = val;
  424. return;
  425. case RI(iaoq[0]):
  426. case RI(iaoq[1]):
  427. /* set 2 lowest bits to ensure userspace privilege: */
  428. regs->iaoq[num - RI(iaoq[0])] = val | 3;
  429. return;
  430. case RI(sar): regs->sar = val;
  431. return;
  432. default: return;
  433. #if 0
  434. /* do not allow to change any of the following registers (yet) */
  435. case RI(sr[0]) ... RI(sr[7]): return regs->sr[num - RI(sr[0])];
  436. case RI(iasq[0]): return regs->iasq[0];
  437. case RI(iasq[1]): return regs->iasq[1];
  438. case RI(iir): return regs->iir;
  439. case RI(isr): return regs->isr;
  440. case RI(ior): return regs->ior;
  441. case RI(ipsw): return regs->ipsw;
  442. case RI(cr27): return regs->cr27;
  443. case cr0, cr24, cr25, cr26, cr27, cr28, cr29, cr30, cr31;
  444. case cr8, cr9, cr12, cr13, cr10, cr15;
  445. #endif
  446. }
  447. }
  448. static int gpr_get(struct task_struct *target,
  449. const struct user_regset *regset,
  450. struct membuf to)
  451. {
  452. struct pt_regs *regs = task_regs(target);
  453. unsigned int pos;
  454. for (pos = 0; pos < ELF_NGREG; pos++)
  455. membuf_store(&to, get_reg(regs, pos));
  456. return 0;
  457. }
  458. static int gpr_set(struct task_struct *target,
  459. const struct user_regset *regset,
  460. unsigned int pos, unsigned int count,
  461. const void *kbuf, const void __user *ubuf)
  462. {
  463. struct pt_regs *regs = task_regs(target);
  464. const unsigned long *k = kbuf;
  465. const unsigned long __user *u = ubuf;
  466. unsigned long reg;
  467. pos /= sizeof(reg);
  468. count /= sizeof(reg);
  469. if (kbuf)
  470. for (; count > 0 && pos < ELF_NGREG; --count)
  471. set_reg(regs, pos++, *k++);
  472. else
  473. for (; count > 0 && pos < ELF_NGREG; --count) {
  474. if (__get_user(reg, u++))
  475. return -EFAULT;
  476. set_reg(regs, pos++, reg);
  477. }
  478. kbuf = k;
  479. ubuf = u;
  480. pos *= sizeof(reg);
  481. count *= sizeof(reg);
  482. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  483. ELF_NGREG * sizeof(reg), -1);
  484. }
  485. static const struct user_regset native_regsets[] = {
  486. [REGSET_GENERAL] = {
  487. .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
  488. .size = sizeof(long), .align = sizeof(long),
  489. .regset_get = gpr_get, .set = gpr_set
  490. },
  491. [REGSET_FP] = {
  492. .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
  493. .size = sizeof(__u64), .align = sizeof(__u64),
  494. .regset_get = fpr_get, .set = fpr_set
  495. }
  496. };
  497. static const struct user_regset_view user_parisc_native_view = {
  498. .name = "parisc", .e_machine = ELF_ARCH, .ei_osabi = ELFOSABI_LINUX,
  499. .regsets = native_regsets, .n = ARRAY_SIZE(native_regsets)
  500. };
  501. #ifdef CONFIG_64BIT
  502. #include <linux/compat.h>
  503. static int gpr32_get(struct task_struct *target,
  504. const struct user_regset *regset,
  505. struct membuf to)
  506. {
  507. struct pt_regs *regs = task_regs(target);
  508. unsigned int pos;
  509. for (pos = 0; pos < ELF_NGREG; pos++)
  510. membuf_store(&to, (compat_ulong_t)get_reg(regs, pos));
  511. return 0;
  512. }
  513. static int gpr32_set(struct task_struct *target,
  514. const struct user_regset *regset,
  515. unsigned int pos, unsigned int count,
  516. const void *kbuf, const void __user *ubuf)
  517. {
  518. struct pt_regs *regs = task_regs(target);
  519. const compat_ulong_t *k = kbuf;
  520. const compat_ulong_t __user *u = ubuf;
  521. compat_ulong_t reg;
  522. pos /= sizeof(reg);
  523. count /= sizeof(reg);
  524. if (kbuf)
  525. for (; count > 0 && pos < ELF_NGREG; --count)
  526. set_reg(regs, pos++, *k++);
  527. else
  528. for (; count > 0 && pos < ELF_NGREG; --count) {
  529. if (__get_user(reg, u++))
  530. return -EFAULT;
  531. set_reg(regs, pos++, reg);
  532. }
  533. kbuf = k;
  534. ubuf = u;
  535. pos *= sizeof(reg);
  536. count *= sizeof(reg);
  537. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  538. ELF_NGREG * sizeof(reg), -1);
  539. }
  540. /*
  541. * These are the regset flavors matching the 32bit native set.
  542. */
  543. static const struct user_regset compat_regsets[] = {
  544. [REGSET_GENERAL] = {
  545. .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
  546. .size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
  547. .regset_get = gpr32_get, .set = gpr32_set
  548. },
  549. [REGSET_FP] = {
  550. .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
  551. .size = sizeof(__u64), .align = sizeof(__u64),
  552. .regset_get = fpr_get, .set = fpr_set
  553. }
  554. };
  555. static const struct user_regset_view user_parisc_compat_view = {
  556. .name = "parisc", .e_machine = EM_PARISC, .ei_osabi = ELFOSABI_LINUX,
  557. .regsets = compat_regsets, .n = ARRAY_SIZE(compat_regsets)
  558. };
  559. #endif /* CONFIG_64BIT */
  560. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  561. {
  562. BUILD_BUG_ON(sizeof(struct user_regs_struct)/sizeof(long) != ELF_NGREG);
  563. BUILD_BUG_ON(sizeof(struct user_fp_struct)/sizeof(__u64) != ELF_NFPREG);
  564. #ifdef CONFIG_64BIT
  565. if (is_compat_task())
  566. return &user_parisc_compat_view;
  567. #endif
  568. return &user_parisc_native_view;
  569. }
  570. /* HAVE_REGS_AND_STACK_ACCESS_API feature */
  571. struct pt_regs_offset {
  572. const char *name;
  573. int offset;
  574. };
  575. #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
  576. #define REG_OFFSET_INDEX(r,i) {.name = #r#i, .offset = offsetof(struct pt_regs, r[i])}
  577. #define REG_OFFSET_END {.name = NULL, .offset = 0}
  578. static const struct pt_regs_offset regoffset_table[] = {
  579. REG_OFFSET_INDEX(gr,0),
  580. REG_OFFSET_INDEX(gr,1),
  581. REG_OFFSET_INDEX(gr,2),
  582. REG_OFFSET_INDEX(gr,3),
  583. REG_OFFSET_INDEX(gr,4),
  584. REG_OFFSET_INDEX(gr,5),
  585. REG_OFFSET_INDEX(gr,6),
  586. REG_OFFSET_INDEX(gr,7),
  587. REG_OFFSET_INDEX(gr,8),
  588. REG_OFFSET_INDEX(gr,9),
  589. REG_OFFSET_INDEX(gr,10),
  590. REG_OFFSET_INDEX(gr,11),
  591. REG_OFFSET_INDEX(gr,12),
  592. REG_OFFSET_INDEX(gr,13),
  593. REG_OFFSET_INDEX(gr,14),
  594. REG_OFFSET_INDEX(gr,15),
  595. REG_OFFSET_INDEX(gr,16),
  596. REG_OFFSET_INDEX(gr,17),
  597. REG_OFFSET_INDEX(gr,18),
  598. REG_OFFSET_INDEX(gr,19),
  599. REG_OFFSET_INDEX(gr,20),
  600. REG_OFFSET_INDEX(gr,21),
  601. REG_OFFSET_INDEX(gr,22),
  602. REG_OFFSET_INDEX(gr,23),
  603. REG_OFFSET_INDEX(gr,24),
  604. REG_OFFSET_INDEX(gr,25),
  605. REG_OFFSET_INDEX(gr,26),
  606. REG_OFFSET_INDEX(gr,27),
  607. REG_OFFSET_INDEX(gr,28),
  608. REG_OFFSET_INDEX(gr,29),
  609. REG_OFFSET_INDEX(gr,30),
  610. REG_OFFSET_INDEX(gr,31),
  611. REG_OFFSET_INDEX(sr,0),
  612. REG_OFFSET_INDEX(sr,1),
  613. REG_OFFSET_INDEX(sr,2),
  614. REG_OFFSET_INDEX(sr,3),
  615. REG_OFFSET_INDEX(sr,4),
  616. REG_OFFSET_INDEX(sr,5),
  617. REG_OFFSET_INDEX(sr,6),
  618. REG_OFFSET_INDEX(sr,7),
  619. REG_OFFSET_INDEX(iasq,0),
  620. REG_OFFSET_INDEX(iasq,1),
  621. REG_OFFSET_INDEX(iaoq,0),
  622. REG_OFFSET_INDEX(iaoq,1),
  623. REG_OFFSET_NAME(cr27),
  624. REG_OFFSET_NAME(ksp),
  625. REG_OFFSET_NAME(kpc),
  626. REG_OFFSET_NAME(sar),
  627. REG_OFFSET_NAME(iir),
  628. REG_OFFSET_NAME(isr),
  629. REG_OFFSET_NAME(ior),
  630. REG_OFFSET_NAME(ipsw),
  631. REG_OFFSET_END,
  632. };
  633. /**
  634. * regs_query_register_offset() - query register offset from its name
  635. * @name: the name of a register
  636. *
  637. * regs_query_register_offset() returns the offset of a register in struct
  638. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  639. */
  640. int regs_query_register_offset(const char *name)
  641. {
  642. const struct pt_regs_offset *roff;
  643. for (roff = regoffset_table; roff->name != NULL; roff++)
  644. if (!strcmp(roff->name, name))
  645. return roff->offset;
  646. return -EINVAL;
  647. }
  648. /**
  649. * regs_query_register_name() - query register name from its offset
  650. * @offset: the offset of a register in struct pt_regs.
  651. *
  652. * regs_query_register_name() returns the name of a register from its
  653. * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
  654. */
  655. const char *regs_query_register_name(unsigned int offset)
  656. {
  657. const struct pt_regs_offset *roff;
  658. for (roff = regoffset_table; roff->name != NULL; roff++)
  659. if (roff->offset == offset)
  660. return roff->name;
  661. return NULL;
  662. }
  663. /**
  664. * regs_within_kernel_stack() - check the address in the stack
  665. * @regs: pt_regs which contains kernel stack pointer.
  666. * @addr: address which is checked.
  667. *
  668. * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
  669. * If @addr is within the kernel stack, it returns true. If not, returns false.
  670. */
  671. int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
  672. {
  673. return ((addr & ~(THREAD_SIZE - 1)) ==
  674. (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
  675. }
  676. /**
  677. * regs_get_kernel_stack_nth() - get Nth entry of the stack
  678. * @regs: pt_regs which contains kernel stack pointer.
  679. * @n: stack entry number.
  680. *
  681. * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
  682. * is specified by @regs. If the @n th entry is NOT in the kernel stack,
  683. * this returns 0.
  684. */
  685. unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
  686. {
  687. unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
  688. addr -= n;
  689. if (!regs_within_kernel_stack(regs, (unsigned long)addr))
  690. return 0;
  691. return *addr;
  692. }