ptrace.c 22 KB

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