ptrace.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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_GR1 && addr <= PT_GR31) ||
  153. addr == PT_IAOQ0 || addr == PT_IAOQ1 ||
  154. (addr >= PT_FR0 && addr <= PT_FR31 + 4) ||
  155. addr == PT_SAR) {
  156. *(unsigned long *) ((char *) task_regs(child) + addr) = data;
  157. ret = 0;
  158. }
  159. break;
  160. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  161. return copy_regset_to_user(child,
  162. task_user_regset_view(current),
  163. REGSET_GENERAL,
  164. 0, sizeof(struct user_regs_struct),
  165. datap);
  166. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  167. return copy_regset_from_user(child,
  168. task_user_regset_view(current),
  169. REGSET_GENERAL,
  170. 0, sizeof(struct user_regs_struct),
  171. datap);
  172. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  173. return copy_regset_to_user(child,
  174. task_user_regset_view(current),
  175. REGSET_FP,
  176. 0, sizeof(struct user_fp_struct),
  177. datap);
  178. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  179. return copy_regset_from_user(child,
  180. task_user_regset_view(current),
  181. REGSET_FP,
  182. 0, sizeof(struct user_fp_struct),
  183. datap);
  184. default:
  185. ret = ptrace_request(child, request, addr, data);
  186. break;
  187. }
  188. return ret;
  189. }
  190. #ifdef CONFIG_COMPAT
  191. /* This function is needed to translate 32 bit pt_regs offsets in to
  192. * 64 bit pt_regs offsets. For example, a 32 bit gdb under a 64 bit kernel
  193. * will request offset 12 if it wants gr3, but the lower 32 bits of
  194. * the 64 bit kernels view of gr3 will be at offset 28 (3*8 + 4).
  195. * This code relies on a 32 bit pt_regs being comprised of 32 bit values
  196. * except for the fp registers which (a) are 64 bits, and (b) follow
  197. * the gr registers at the start of pt_regs. The 32 bit pt_regs should
  198. * be half the size of the 64 bit pt_regs, plus 32*4 to allow for fr[]
  199. * being 64 bit in both cases.
  200. */
  201. static compat_ulong_t translate_usr_offset(compat_ulong_t offset)
  202. {
  203. if (offset < 0)
  204. return sizeof(struct pt_regs);
  205. else if (offset <= 32*4) /* gr[0..31] */
  206. return offset * 2 + 4;
  207. else if (offset <= 32*4+32*8) /* gr[0..31] + fr[0..31] */
  208. return offset + 32*4;
  209. else if (offset < sizeof(struct pt_regs)/2 + 32*4)
  210. return offset * 2 + 4 - 32*8;
  211. else
  212. return sizeof(struct pt_regs);
  213. }
  214. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  215. compat_ulong_t addr, compat_ulong_t data)
  216. {
  217. compat_uint_t tmp;
  218. long ret = -EIO;
  219. switch (request) {
  220. case PTRACE_PEEKUSR:
  221. if (addr & (sizeof(compat_uint_t)-1))
  222. break;
  223. addr = translate_usr_offset(addr);
  224. if (addr >= sizeof(struct pt_regs))
  225. break;
  226. tmp = *(compat_uint_t *) ((char *) task_regs(child) + addr);
  227. ret = put_user(tmp, (compat_uint_t *) (unsigned long) data);
  228. break;
  229. /* Write the word at location addr in the USER area. This will need
  230. to change when the kernel no longer saves all regs on a syscall.
  231. FIXME. There is a problem at the moment in that r3-r18 are only
  232. saved if the process is ptraced on syscall entry, and even then
  233. those values are overwritten by actual register values on syscall
  234. exit. */
  235. case PTRACE_POKEUSR:
  236. /* Some register values written here may be ignored in
  237. * entry.S:syscall_restore_rfi; e.g. iaoq is written with
  238. * r31/r31+4, and not with the values in pt_regs.
  239. */
  240. if (addr == PT_PSW) {
  241. /* Since PT_PSW==0, it is valid for 32 bit processes
  242. * under 64 bit kernels as well.
  243. */
  244. ret = arch_ptrace(child, request, addr, data);
  245. } else {
  246. if (addr & (sizeof(compat_uint_t)-1))
  247. break;
  248. addr = translate_usr_offset(addr);
  249. if (addr >= sizeof(struct pt_regs))
  250. break;
  251. if (addr >= PT_FR0 && addr <= PT_FR31 + 4) {
  252. /* Special case, fp regs are 64 bits anyway */
  253. *(__u64 *) ((char *) task_regs(child) + addr) = data;
  254. ret = 0;
  255. }
  256. else if ((addr >= PT_GR1+4 && addr <= PT_GR31+4) ||
  257. addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4 ||
  258. addr == PT_SAR+4) {
  259. /* Zero the top 32 bits */
  260. *(__u32 *) ((char *) task_regs(child) + addr - 4) = 0;
  261. *(__u32 *) ((char *) task_regs(child) + addr) = data;
  262. ret = 0;
  263. }
  264. }
  265. break;
  266. default:
  267. ret = compat_ptrace_request(child, request, addr, data);
  268. break;
  269. }
  270. return ret;
  271. }
  272. #endif
  273. long do_syscall_trace_enter(struct pt_regs *regs)
  274. {
  275. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  276. tracehook_report_syscall_entry(regs)) {
  277. /*
  278. * Tracing decided this syscall should not happen or the
  279. * debugger stored an invalid system call number. Skip
  280. * the system call and the system call restart handling.
  281. */
  282. regs->gr[20] = -1UL;
  283. goto out;
  284. }
  285. /* Do the secure computing check after ptrace. */
  286. if (secure_computing(NULL) == -1)
  287. return -1;
  288. #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
  289. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  290. trace_sys_enter(regs, regs->gr[20]);
  291. #endif
  292. #ifdef CONFIG_64BIT
  293. if (!is_compat_task())
  294. audit_syscall_entry(regs->gr[20], regs->gr[26], regs->gr[25],
  295. regs->gr[24], regs->gr[23]);
  296. else
  297. #endif
  298. audit_syscall_entry(regs->gr[20] & 0xffffffff,
  299. regs->gr[26] & 0xffffffff,
  300. regs->gr[25] & 0xffffffff,
  301. regs->gr[24] & 0xffffffff,
  302. regs->gr[23] & 0xffffffff);
  303. out:
  304. /*
  305. * Sign extend the syscall number to 64bit since it may have been
  306. * modified by a compat ptrace call
  307. */
  308. return (int) ((u32) regs->gr[20]);
  309. }
  310. void do_syscall_trace_exit(struct pt_regs *regs)
  311. {
  312. int stepping = test_thread_flag(TIF_SINGLESTEP) ||
  313. test_thread_flag(TIF_BLOCKSTEP);
  314. audit_syscall_exit(regs);
  315. #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
  316. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  317. trace_sys_exit(regs, regs->gr[20]);
  318. #endif
  319. if (stepping || test_thread_flag(TIF_SYSCALL_TRACE))
  320. tracehook_report_syscall_exit(regs, stepping);
  321. }
  322. /*
  323. * regset functions.
  324. */
  325. static int fpr_get(struct task_struct *target,
  326. const struct user_regset *regset,
  327. unsigned int pos, unsigned int count,
  328. void *kbuf, void __user *ubuf)
  329. {
  330. struct pt_regs *regs = task_regs(target);
  331. __u64 *k = kbuf;
  332. __u64 __user *u = ubuf;
  333. __u64 reg;
  334. pos /= sizeof(reg);
  335. count /= sizeof(reg);
  336. if (kbuf)
  337. for (; count > 0 && pos < ELF_NFPREG; --count)
  338. *k++ = regs->fr[pos++];
  339. else
  340. for (; count > 0 && pos < ELF_NFPREG; --count)
  341. if (__put_user(regs->fr[pos++], u++))
  342. return -EFAULT;
  343. kbuf = k;
  344. ubuf = u;
  345. pos *= sizeof(reg);
  346. count *= sizeof(reg);
  347. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  348. ELF_NFPREG * sizeof(reg), -1);
  349. }
  350. static int fpr_set(struct task_struct *target,
  351. const struct user_regset *regset,
  352. unsigned int pos, unsigned int count,
  353. const void *kbuf, const void __user *ubuf)
  354. {
  355. struct pt_regs *regs = task_regs(target);
  356. const __u64 *k = kbuf;
  357. const __u64 __user *u = ubuf;
  358. __u64 reg;
  359. pos /= sizeof(reg);
  360. count /= sizeof(reg);
  361. if (kbuf)
  362. for (; count > 0 && pos < ELF_NFPREG; --count)
  363. regs->fr[pos++] = *k++;
  364. else
  365. for (; count > 0 && pos < ELF_NFPREG; --count) {
  366. if (__get_user(reg, u++))
  367. return -EFAULT;
  368. regs->fr[pos++] = reg;
  369. }
  370. kbuf = k;
  371. ubuf = u;
  372. pos *= sizeof(reg);
  373. count *= sizeof(reg);
  374. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  375. ELF_NFPREG * sizeof(reg), -1);
  376. }
  377. #define RI(reg) (offsetof(struct user_regs_struct,reg) / sizeof(long))
  378. static unsigned long get_reg(struct pt_regs *regs, int num)
  379. {
  380. switch (num) {
  381. case RI(gr[0]) ... RI(gr[31]): return regs->gr[num - RI(gr[0])];
  382. case RI(sr[0]) ... RI(sr[7]): return regs->sr[num - RI(sr[0])];
  383. case RI(iasq[0]): return regs->iasq[0];
  384. case RI(iasq[1]): return regs->iasq[1];
  385. case RI(iaoq[0]): return regs->iaoq[0];
  386. case RI(iaoq[1]): return regs->iaoq[1];
  387. case RI(sar): return regs->sar;
  388. case RI(iir): return regs->iir;
  389. case RI(isr): return regs->isr;
  390. case RI(ior): return regs->ior;
  391. case RI(ipsw): return regs->ipsw;
  392. case RI(cr27): return regs->cr27;
  393. case RI(cr0): return mfctl(0);
  394. case RI(cr24): return mfctl(24);
  395. case RI(cr25): return mfctl(25);
  396. case RI(cr26): return mfctl(26);
  397. case RI(cr28): return mfctl(28);
  398. case RI(cr29): return mfctl(29);
  399. case RI(cr30): return mfctl(30);
  400. case RI(cr31): return mfctl(31);
  401. case RI(cr8): return mfctl(8);
  402. case RI(cr9): return mfctl(9);
  403. case RI(cr12): return mfctl(12);
  404. case RI(cr13): return mfctl(13);
  405. case RI(cr10): return mfctl(10);
  406. case RI(cr15): return mfctl(15);
  407. default: return 0;
  408. }
  409. }
  410. static void set_reg(struct pt_regs *regs, int num, unsigned long val)
  411. {
  412. switch (num) {
  413. case RI(gr[0]): /*
  414. * PSW is in gr[0].
  415. * Allow writing to Nullify, Divide-step-correction,
  416. * and carry/borrow bits.
  417. * BEWARE, if you set N, and then single step, it won't
  418. * stop on the nullified instruction.
  419. */
  420. val &= USER_PSW_BITS;
  421. regs->gr[0] &= ~USER_PSW_BITS;
  422. regs->gr[0] |= val;
  423. return;
  424. case RI(gr[1]) ... RI(gr[31]):
  425. regs->gr[num - RI(gr[0])] = val;
  426. return;
  427. case RI(iaoq[0]):
  428. case RI(iaoq[1]):
  429. regs->iaoq[num - RI(iaoq[0])] = val;
  430. return;
  431. case RI(sar): regs->sar = val;
  432. return;
  433. default: return;
  434. #if 0
  435. /* do not allow to change any of the following registers (yet) */
  436. case RI(sr[0]) ... RI(sr[7]): return regs->sr[num - RI(sr[0])];
  437. case RI(iasq[0]): return regs->iasq[0];
  438. case RI(iasq[1]): return regs->iasq[1];
  439. case RI(iir): return regs->iir;
  440. case RI(isr): return regs->isr;
  441. case RI(ior): return regs->ior;
  442. case RI(ipsw): return regs->ipsw;
  443. case RI(cr27): return regs->cr27;
  444. case cr0, cr24, cr25, cr26, cr27, cr28, cr29, cr30, cr31;
  445. case cr8, cr9, cr12, cr13, cr10, cr15;
  446. #endif
  447. }
  448. }
  449. static int gpr_get(struct task_struct *target,
  450. const struct user_regset *regset,
  451. unsigned int pos, unsigned int count,
  452. void *kbuf, void __user *ubuf)
  453. {
  454. struct pt_regs *regs = task_regs(target);
  455. unsigned long *k = kbuf;
  456. unsigned long __user *u = ubuf;
  457. unsigned long reg;
  458. pos /= sizeof(reg);
  459. count /= sizeof(reg);
  460. if (kbuf)
  461. for (; count > 0 && pos < ELF_NGREG; --count)
  462. *k++ = get_reg(regs, pos++);
  463. else
  464. for (; count > 0 && pos < ELF_NGREG; --count)
  465. if (__put_user(get_reg(regs, pos++), u++))
  466. return -EFAULT;
  467. kbuf = k;
  468. ubuf = u;
  469. pos *= sizeof(reg);
  470. count *= sizeof(reg);
  471. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  472. ELF_NGREG * sizeof(reg), -1);
  473. }
  474. static int gpr_set(struct task_struct *target,
  475. const struct user_regset *regset,
  476. unsigned int pos, unsigned int count,
  477. const void *kbuf, const void __user *ubuf)
  478. {
  479. struct pt_regs *regs = task_regs(target);
  480. const unsigned long *k = kbuf;
  481. const unsigned long __user *u = ubuf;
  482. unsigned long reg;
  483. pos /= sizeof(reg);
  484. count /= sizeof(reg);
  485. if (kbuf)
  486. for (; count > 0 && pos < ELF_NGREG; --count)
  487. set_reg(regs, pos++, *k++);
  488. else
  489. for (; count > 0 && pos < ELF_NGREG; --count) {
  490. if (__get_user(reg, u++))
  491. return -EFAULT;
  492. set_reg(regs, pos++, reg);
  493. }
  494. kbuf = k;
  495. ubuf = u;
  496. pos *= sizeof(reg);
  497. count *= sizeof(reg);
  498. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  499. ELF_NGREG * sizeof(reg), -1);
  500. }
  501. static const struct user_regset native_regsets[] = {
  502. [REGSET_GENERAL] = {
  503. .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
  504. .size = sizeof(long), .align = sizeof(long),
  505. .get = gpr_get, .set = gpr_set
  506. },
  507. [REGSET_FP] = {
  508. .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
  509. .size = sizeof(__u64), .align = sizeof(__u64),
  510. .get = fpr_get, .set = fpr_set
  511. }
  512. };
  513. static const struct user_regset_view user_parisc_native_view = {
  514. .name = "parisc", .e_machine = ELF_ARCH, .ei_osabi = ELFOSABI_LINUX,
  515. .regsets = native_regsets, .n = ARRAY_SIZE(native_regsets)
  516. };
  517. #ifdef CONFIG_64BIT
  518. #include <linux/compat.h>
  519. static int gpr32_get(struct task_struct *target,
  520. const struct user_regset *regset,
  521. unsigned int pos, unsigned int count,
  522. void *kbuf, void __user *ubuf)
  523. {
  524. struct pt_regs *regs = task_regs(target);
  525. compat_ulong_t *k = kbuf;
  526. compat_ulong_t __user *u = ubuf;
  527. compat_ulong_t reg;
  528. pos /= sizeof(reg);
  529. count /= sizeof(reg);
  530. if (kbuf)
  531. for (; count > 0 && pos < ELF_NGREG; --count)
  532. *k++ = get_reg(regs, pos++);
  533. else
  534. for (; count > 0 && pos < ELF_NGREG; --count)
  535. if (__put_user((compat_ulong_t) get_reg(regs, pos++), u++))
  536. return -EFAULT;
  537. kbuf = k;
  538. ubuf = u;
  539. pos *= sizeof(reg);
  540. count *= sizeof(reg);
  541. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  542. ELF_NGREG * sizeof(reg), -1);
  543. }
  544. static int gpr32_set(struct task_struct *target,
  545. const struct user_regset *regset,
  546. unsigned int pos, unsigned int count,
  547. const void *kbuf, const void __user *ubuf)
  548. {
  549. struct pt_regs *regs = task_regs(target);
  550. const compat_ulong_t *k = kbuf;
  551. const compat_ulong_t __user *u = ubuf;
  552. compat_ulong_t reg;
  553. pos /= sizeof(reg);
  554. count /= sizeof(reg);
  555. if (kbuf)
  556. for (; count > 0 && pos < ELF_NGREG; --count)
  557. set_reg(regs, pos++, *k++);
  558. else
  559. for (; count > 0 && pos < ELF_NGREG; --count) {
  560. if (__get_user(reg, u++))
  561. return -EFAULT;
  562. set_reg(regs, pos++, reg);
  563. }
  564. kbuf = k;
  565. ubuf = u;
  566. pos *= sizeof(reg);
  567. count *= sizeof(reg);
  568. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  569. ELF_NGREG * sizeof(reg), -1);
  570. }
  571. /*
  572. * These are the regset flavors matching the 32bit native set.
  573. */
  574. static const struct user_regset compat_regsets[] = {
  575. [REGSET_GENERAL] = {
  576. .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
  577. .size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
  578. .get = gpr32_get, .set = gpr32_set
  579. },
  580. [REGSET_FP] = {
  581. .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
  582. .size = sizeof(__u64), .align = sizeof(__u64),
  583. .get = fpr_get, .set = fpr_set
  584. }
  585. };
  586. static const struct user_regset_view user_parisc_compat_view = {
  587. .name = "parisc", .e_machine = EM_PARISC, .ei_osabi = ELFOSABI_LINUX,
  588. .regsets = compat_regsets, .n = ARRAY_SIZE(compat_regsets)
  589. };
  590. #endif /* CONFIG_64BIT */
  591. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  592. {
  593. BUILD_BUG_ON(sizeof(struct user_regs_struct)/sizeof(long) != ELF_NGREG);
  594. BUILD_BUG_ON(sizeof(struct user_fp_struct)/sizeof(__u64) != ELF_NFPREG);
  595. #ifdef CONFIG_64BIT
  596. if (is_compat_task())
  597. return &user_parisc_compat_view;
  598. #endif
  599. return &user_parisc_native_view;
  600. }
  601. /* HAVE_REGS_AND_STACK_ACCESS_API feature */
  602. struct pt_regs_offset {
  603. const char *name;
  604. int offset;
  605. };
  606. #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
  607. #define REG_OFFSET_INDEX(r,i) {.name = #r#i, .offset = offsetof(struct pt_regs, r[i])}
  608. #define REG_OFFSET_END {.name = NULL, .offset = 0}
  609. static const struct pt_regs_offset regoffset_table[] = {
  610. REG_OFFSET_INDEX(gr,0),
  611. REG_OFFSET_INDEX(gr,1),
  612. REG_OFFSET_INDEX(gr,2),
  613. REG_OFFSET_INDEX(gr,3),
  614. REG_OFFSET_INDEX(gr,4),
  615. REG_OFFSET_INDEX(gr,5),
  616. REG_OFFSET_INDEX(gr,6),
  617. REG_OFFSET_INDEX(gr,7),
  618. REG_OFFSET_INDEX(gr,8),
  619. REG_OFFSET_INDEX(gr,9),
  620. REG_OFFSET_INDEX(gr,10),
  621. REG_OFFSET_INDEX(gr,11),
  622. REG_OFFSET_INDEX(gr,12),
  623. REG_OFFSET_INDEX(gr,13),
  624. REG_OFFSET_INDEX(gr,14),
  625. REG_OFFSET_INDEX(gr,15),
  626. REG_OFFSET_INDEX(gr,16),
  627. REG_OFFSET_INDEX(gr,17),
  628. REG_OFFSET_INDEX(gr,18),
  629. REG_OFFSET_INDEX(gr,19),
  630. REG_OFFSET_INDEX(gr,20),
  631. REG_OFFSET_INDEX(gr,21),
  632. REG_OFFSET_INDEX(gr,22),
  633. REG_OFFSET_INDEX(gr,23),
  634. REG_OFFSET_INDEX(gr,24),
  635. REG_OFFSET_INDEX(gr,25),
  636. REG_OFFSET_INDEX(gr,26),
  637. REG_OFFSET_INDEX(gr,27),
  638. REG_OFFSET_INDEX(gr,28),
  639. REG_OFFSET_INDEX(gr,29),
  640. REG_OFFSET_INDEX(gr,30),
  641. REG_OFFSET_INDEX(gr,31),
  642. REG_OFFSET_INDEX(sr,0),
  643. REG_OFFSET_INDEX(sr,1),
  644. REG_OFFSET_INDEX(sr,2),
  645. REG_OFFSET_INDEX(sr,3),
  646. REG_OFFSET_INDEX(sr,4),
  647. REG_OFFSET_INDEX(sr,5),
  648. REG_OFFSET_INDEX(sr,6),
  649. REG_OFFSET_INDEX(sr,7),
  650. REG_OFFSET_INDEX(iasq,0),
  651. REG_OFFSET_INDEX(iasq,1),
  652. REG_OFFSET_INDEX(iaoq,0),
  653. REG_OFFSET_INDEX(iaoq,1),
  654. REG_OFFSET_NAME(cr27),
  655. REG_OFFSET_NAME(ksp),
  656. REG_OFFSET_NAME(kpc),
  657. REG_OFFSET_NAME(sar),
  658. REG_OFFSET_NAME(iir),
  659. REG_OFFSET_NAME(isr),
  660. REG_OFFSET_NAME(ior),
  661. REG_OFFSET_NAME(ipsw),
  662. REG_OFFSET_END,
  663. };
  664. /**
  665. * regs_query_register_offset() - query register offset from its name
  666. * @name: the name of a register
  667. *
  668. * regs_query_register_offset() returns the offset of a register in struct
  669. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  670. */
  671. int regs_query_register_offset(const char *name)
  672. {
  673. const struct pt_regs_offset *roff;
  674. for (roff = regoffset_table; roff->name != NULL; roff++)
  675. if (!strcmp(roff->name, name))
  676. return roff->offset;
  677. return -EINVAL;
  678. }
  679. /**
  680. * regs_query_register_name() - query register name from its offset
  681. * @offset: the offset of a register in struct pt_regs.
  682. *
  683. * regs_query_register_name() returns the name of a register from its
  684. * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
  685. */
  686. const char *regs_query_register_name(unsigned int offset)
  687. {
  688. const struct pt_regs_offset *roff;
  689. for (roff = regoffset_table; roff->name != NULL; roff++)
  690. if (roff->offset == offset)
  691. return roff->name;
  692. return NULL;
  693. }