ptrace.c 22 KB

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