ptrace.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. *
  5. * Derived from "arch/m68k/kernel/ptrace.c"
  6. * Copyright (C) 1994 by Hamish Macdonald
  7. * Taken from linux/kernel/ptrace.c and modified for M680x0.
  8. * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
  9. *
  10. * Modified by Cort Dougan (cort@hq.fsmlabs.com)
  11. * and Paul Mackerras (paulus@samba.org).
  12. *
  13. * This file is subject to the terms and conditions of the GNU General
  14. * Public License. See the file README.legal in the main directory of
  15. * this archive for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/mm.h>
  20. #include <linux/smp.h>
  21. #include <linux/errno.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/regset.h>
  24. #include <linux/elf.h>
  25. #include <linux/user.h>
  26. #include <linux/security.h>
  27. #include <linux/signal.h>
  28. #include <linux/seccomp.h>
  29. #include <linux/audit.h>
  30. #ifdef CONFIG_PPC32
  31. #include <linux/module.h>
  32. #endif
  33. #include <asm/uaccess.h>
  34. #include <asm/page.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/system.h>
  37. /*
  38. * does not yet catch signals sent when the child dies.
  39. * in exit.c or in signal.c.
  40. */
  41. /*
  42. * Set of msr bits that gdb can change on behalf of a process.
  43. */
  44. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  45. #define MSR_DEBUGCHANGE 0
  46. #else
  47. #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
  48. #endif
  49. /*
  50. * Max register writeable via put_reg
  51. */
  52. #ifdef CONFIG_PPC32
  53. #define PT_MAX_PUT_REG PT_MQ
  54. #else
  55. #define PT_MAX_PUT_REG PT_CCR
  56. #endif
  57. static unsigned long get_user_msr(struct task_struct *task)
  58. {
  59. return task->thread.regs->msr | task->thread.fpexc_mode;
  60. }
  61. static int set_user_msr(struct task_struct *task, unsigned long msr)
  62. {
  63. task->thread.regs->msr &= ~MSR_DEBUGCHANGE;
  64. task->thread.regs->msr |= msr & MSR_DEBUGCHANGE;
  65. return 0;
  66. }
  67. /*
  68. * We prevent mucking around with the reserved area of trap
  69. * which are used internally by the kernel.
  70. */
  71. static int set_user_trap(struct task_struct *task, unsigned long trap)
  72. {
  73. task->thread.regs->trap = trap & 0xfff0;
  74. return 0;
  75. }
  76. /*
  77. * Get contents of register REGNO in task TASK.
  78. */
  79. unsigned long ptrace_get_reg(struct task_struct *task, int regno)
  80. {
  81. if (task->thread.regs == NULL)
  82. return -EIO;
  83. if (regno == PT_MSR)
  84. return get_user_msr(task);
  85. if (regno < (sizeof(struct pt_regs) / sizeof(unsigned long)))
  86. return ((unsigned long *)task->thread.regs)[regno];
  87. return -EIO;
  88. }
  89. /*
  90. * Write contents of register REGNO in task TASK.
  91. */
  92. int ptrace_put_reg(struct task_struct *task, int regno, unsigned long data)
  93. {
  94. if (task->thread.regs == NULL)
  95. return -EIO;
  96. if (regno == PT_MSR)
  97. return set_user_msr(task, data);
  98. if (regno == PT_TRAP)
  99. return set_user_trap(task, data);
  100. if (regno <= PT_MAX_PUT_REG) {
  101. ((unsigned long *)task->thread.regs)[regno] = data;
  102. return 0;
  103. }
  104. return -EIO;
  105. }
  106. static int gpr_get(struct task_struct *target, const struct user_regset *regset,
  107. unsigned int pos, unsigned int count,
  108. void *kbuf, void __user *ubuf)
  109. {
  110. int ret;
  111. if (target->thread.regs == NULL)
  112. return -EIO;
  113. CHECK_FULL_REGS(target->thread.regs);
  114. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  115. target->thread.regs,
  116. 0, offsetof(struct pt_regs, msr));
  117. if (!ret) {
  118. unsigned long msr = get_user_msr(target);
  119. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &msr,
  120. offsetof(struct pt_regs, msr),
  121. offsetof(struct pt_regs, msr) +
  122. sizeof(msr));
  123. }
  124. BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
  125. offsetof(struct pt_regs, msr) + sizeof(long));
  126. if (!ret)
  127. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  128. &target->thread.regs->orig_gpr3,
  129. offsetof(struct pt_regs, orig_gpr3),
  130. sizeof(struct pt_regs));
  131. if (!ret)
  132. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  133. sizeof(struct pt_regs), -1);
  134. return ret;
  135. }
  136. static int gpr_set(struct task_struct *target, const struct user_regset *regset,
  137. unsigned int pos, unsigned int count,
  138. const void *kbuf, const void __user *ubuf)
  139. {
  140. unsigned long reg;
  141. int ret;
  142. if (target->thread.regs == NULL)
  143. return -EIO;
  144. CHECK_FULL_REGS(target->thread.regs);
  145. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  146. target->thread.regs,
  147. 0, PT_MSR * sizeof(reg));
  148. if (!ret && count > 0) {
  149. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &reg,
  150. PT_MSR * sizeof(reg),
  151. (PT_MSR + 1) * sizeof(reg));
  152. if (!ret)
  153. ret = set_user_msr(target, reg);
  154. }
  155. BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
  156. offsetof(struct pt_regs, msr) + sizeof(long));
  157. if (!ret)
  158. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  159. &target->thread.regs->orig_gpr3,
  160. PT_ORIG_R3 * sizeof(reg),
  161. (PT_MAX_PUT_REG + 1) * sizeof(reg));
  162. if (PT_MAX_PUT_REG + 1 < PT_TRAP && !ret)
  163. ret = user_regset_copyin_ignore(
  164. &pos, &count, &kbuf, &ubuf,
  165. (PT_MAX_PUT_REG + 1) * sizeof(reg),
  166. PT_TRAP * sizeof(reg));
  167. if (!ret && count > 0) {
  168. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &reg,
  169. PT_TRAP * sizeof(reg),
  170. (PT_TRAP + 1) * sizeof(reg));
  171. if (!ret)
  172. ret = set_user_trap(target, reg);
  173. }
  174. if (!ret)
  175. ret = user_regset_copyin_ignore(
  176. &pos, &count, &kbuf, &ubuf,
  177. (PT_TRAP + 1) * sizeof(reg), -1);
  178. return ret;
  179. }
  180. static int fpr_get(struct task_struct *target, const struct user_regset *regset,
  181. unsigned int pos, unsigned int count,
  182. void *kbuf, void __user *ubuf)
  183. {
  184. #ifdef CONFIG_VSX
  185. double buf[33];
  186. int i;
  187. #endif
  188. flush_fp_to_thread(target);
  189. #ifdef CONFIG_VSX
  190. /* copy to local buffer then write that out */
  191. for (i = 0; i < 32 ; i++)
  192. buf[i] = target->thread.TS_FPR(i);
  193. memcpy(&buf[32], &target->thread.fpscr, sizeof(double));
  194. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, buf, 0, -1);
  195. #else
  196. BUILD_BUG_ON(offsetof(struct thread_struct, fpscr) !=
  197. offsetof(struct thread_struct, TS_FPR(32)));
  198. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  199. &target->thread.fpr, 0, -1);
  200. #endif
  201. }
  202. static int fpr_set(struct task_struct *target, const struct user_regset *regset,
  203. unsigned int pos, unsigned int count,
  204. const void *kbuf, const void __user *ubuf)
  205. {
  206. #ifdef CONFIG_VSX
  207. double buf[33];
  208. int i;
  209. #endif
  210. flush_fp_to_thread(target);
  211. #ifdef CONFIG_VSX
  212. /* copy to local buffer then write that out */
  213. i = user_regset_copyin(&pos, &count, &kbuf, &ubuf, buf, 0, -1);
  214. if (i)
  215. return i;
  216. for (i = 0; i < 32 ; i++)
  217. target->thread.TS_FPR(i) = buf[i];
  218. memcpy(&target->thread.fpscr, &buf[32], sizeof(double));
  219. return 0;
  220. #else
  221. BUILD_BUG_ON(offsetof(struct thread_struct, fpscr) !=
  222. offsetof(struct thread_struct, TS_FPR(32)));
  223. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  224. &target->thread.fpr, 0, -1);
  225. #endif
  226. }
  227. #ifdef CONFIG_ALTIVEC
  228. /*
  229. * Get/set all the altivec registers vr0..vr31, vscr, vrsave, in one go.
  230. * The transfer totals 34 quadword. Quadwords 0-31 contain the
  231. * corresponding vector registers. Quadword 32 contains the vscr as the
  232. * last word (offset 12) within that quadword. Quadword 33 contains the
  233. * vrsave as the first word (offset 0) within the quadword.
  234. *
  235. * This definition of the VMX state is compatible with the current PPC32
  236. * ptrace interface. This allows signal handling and ptrace to use the
  237. * same structures. This also simplifies the implementation of a bi-arch
  238. * (combined (32- and 64-bit) gdb.
  239. */
  240. static int vr_active(struct task_struct *target,
  241. const struct user_regset *regset)
  242. {
  243. flush_altivec_to_thread(target);
  244. return target->thread.used_vr ? regset->n : 0;
  245. }
  246. static int vr_get(struct task_struct *target, const struct user_regset *regset,
  247. unsigned int pos, unsigned int count,
  248. void *kbuf, void __user *ubuf)
  249. {
  250. int ret;
  251. flush_altivec_to_thread(target);
  252. BUILD_BUG_ON(offsetof(struct thread_struct, vscr) !=
  253. offsetof(struct thread_struct, vr[32]));
  254. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  255. &target->thread.vr, 0,
  256. 33 * sizeof(vector128));
  257. if (!ret) {
  258. /*
  259. * Copy out only the low-order word of vrsave.
  260. */
  261. union {
  262. elf_vrreg_t reg;
  263. u32 word;
  264. } vrsave;
  265. memset(&vrsave, 0, sizeof(vrsave));
  266. vrsave.word = target->thread.vrsave;
  267. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &vrsave,
  268. 33 * sizeof(vector128), -1);
  269. }
  270. return ret;
  271. }
  272. static int vr_set(struct task_struct *target, const struct user_regset *regset,
  273. unsigned int pos, unsigned int count,
  274. const void *kbuf, const void __user *ubuf)
  275. {
  276. int ret;
  277. flush_altivec_to_thread(target);
  278. BUILD_BUG_ON(offsetof(struct thread_struct, vscr) !=
  279. offsetof(struct thread_struct, vr[32]));
  280. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  281. &target->thread.vr, 0, 33 * sizeof(vector128));
  282. if (!ret && count > 0) {
  283. /*
  284. * We use only the first word of vrsave.
  285. */
  286. union {
  287. elf_vrreg_t reg;
  288. u32 word;
  289. } vrsave;
  290. memset(&vrsave, 0, sizeof(vrsave));
  291. vrsave.word = target->thread.vrsave;
  292. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &vrsave,
  293. 33 * sizeof(vector128), -1);
  294. if (!ret)
  295. target->thread.vrsave = vrsave.word;
  296. }
  297. return ret;
  298. }
  299. #endif /* CONFIG_ALTIVEC */
  300. #ifdef CONFIG_SPE
  301. /*
  302. * For get_evrregs/set_evrregs functions 'data' has the following layout:
  303. *
  304. * struct {
  305. * u32 evr[32];
  306. * u64 acc;
  307. * u32 spefscr;
  308. * }
  309. */
  310. static int evr_active(struct task_struct *target,
  311. const struct user_regset *regset)
  312. {
  313. flush_spe_to_thread(target);
  314. return target->thread.used_spe ? regset->n : 0;
  315. }
  316. static int evr_get(struct task_struct *target, const struct user_regset *regset,
  317. unsigned int pos, unsigned int count,
  318. void *kbuf, void __user *ubuf)
  319. {
  320. int ret;
  321. flush_spe_to_thread(target);
  322. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  323. &target->thread.evr,
  324. 0, sizeof(target->thread.evr));
  325. BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
  326. offsetof(struct thread_struct, spefscr));
  327. if (!ret)
  328. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  329. &target->thread.acc,
  330. sizeof(target->thread.evr), -1);
  331. return ret;
  332. }
  333. static int evr_set(struct task_struct *target, const struct user_regset *regset,
  334. unsigned int pos, unsigned int count,
  335. const void *kbuf, const void __user *ubuf)
  336. {
  337. int ret;
  338. flush_spe_to_thread(target);
  339. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  340. &target->thread.evr,
  341. 0, sizeof(target->thread.evr));
  342. BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
  343. offsetof(struct thread_struct, spefscr));
  344. if (!ret)
  345. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  346. &target->thread.acc,
  347. sizeof(target->thread.evr), -1);
  348. return ret;
  349. }
  350. #endif /* CONFIG_SPE */
  351. /*
  352. * These are our native regset flavors.
  353. */
  354. enum powerpc_regset {
  355. REGSET_GPR,
  356. REGSET_FPR,
  357. #ifdef CONFIG_ALTIVEC
  358. REGSET_VMX,
  359. #endif
  360. #ifdef CONFIG_SPE
  361. REGSET_SPE,
  362. #endif
  363. };
  364. static const struct user_regset native_regsets[] = {
  365. [REGSET_GPR] = {
  366. .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
  367. .size = sizeof(long), .align = sizeof(long),
  368. .get = gpr_get, .set = gpr_set
  369. },
  370. [REGSET_FPR] = {
  371. .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
  372. .size = sizeof(double), .align = sizeof(double),
  373. .get = fpr_get, .set = fpr_set
  374. },
  375. #ifdef CONFIG_ALTIVEC
  376. [REGSET_VMX] = {
  377. .core_note_type = NT_PPC_VMX, .n = 34,
  378. .size = sizeof(vector128), .align = sizeof(vector128),
  379. .active = vr_active, .get = vr_get, .set = vr_set
  380. },
  381. #endif
  382. #ifdef CONFIG_SPE
  383. [REGSET_SPE] = {
  384. .n = 35,
  385. .size = sizeof(u32), .align = sizeof(u32),
  386. .active = evr_active, .get = evr_get, .set = evr_set
  387. },
  388. #endif
  389. };
  390. static const struct user_regset_view user_ppc_native_view = {
  391. .name = UTS_MACHINE, .e_machine = ELF_ARCH, .ei_osabi = ELF_OSABI,
  392. .regsets = native_regsets, .n = ARRAY_SIZE(native_regsets)
  393. };
  394. #ifdef CONFIG_PPC64
  395. #include <linux/compat.h>
  396. static int gpr32_get(struct task_struct *target,
  397. const struct user_regset *regset,
  398. unsigned int pos, unsigned int count,
  399. void *kbuf, void __user *ubuf)
  400. {
  401. const unsigned long *regs = &target->thread.regs->gpr[0];
  402. compat_ulong_t *k = kbuf;
  403. compat_ulong_t __user *u = ubuf;
  404. compat_ulong_t reg;
  405. if (target->thread.regs == NULL)
  406. return -EIO;
  407. CHECK_FULL_REGS(target->thread.regs);
  408. pos /= sizeof(reg);
  409. count /= sizeof(reg);
  410. if (kbuf)
  411. for (; count > 0 && pos < PT_MSR; --count)
  412. *k++ = regs[pos++];
  413. else
  414. for (; count > 0 && pos < PT_MSR; --count)
  415. if (__put_user((compat_ulong_t) regs[pos++], u++))
  416. return -EFAULT;
  417. if (count > 0 && pos == PT_MSR) {
  418. reg = get_user_msr(target);
  419. if (kbuf)
  420. *k++ = reg;
  421. else if (__put_user(reg, u++))
  422. return -EFAULT;
  423. ++pos;
  424. --count;
  425. }
  426. if (kbuf)
  427. for (; count > 0 && pos < PT_REGS_COUNT; --count)
  428. *k++ = regs[pos++];
  429. else
  430. for (; count > 0 && pos < PT_REGS_COUNT; --count)
  431. if (__put_user((compat_ulong_t) regs[pos++], u++))
  432. return -EFAULT;
  433. kbuf = k;
  434. ubuf = u;
  435. pos *= sizeof(reg);
  436. count *= sizeof(reg);
  437. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  438. PT_REGS_COUNT * sizeof(reg), -1);
  439. }
  440. static int gpr32_set(struct task_struct *target,
  441. const struct user_regset *regset,
  442. unsigned int pos, unsigned int count,
  443. const void *kbuf, const void __user *ubuf)
  444. {
  445. unsigned long *regs = &target->thread.regs->gpr[0];
  446. const compat_ulong_t *k = kbuf;
  447. const compat_ulong_t __user *u = ubuf;
  448. compat_ulong_t reg;
  449. if (target->thread.regs == NULL)
  450. return -EIO;
  451. CHECK_FULL_REGS(target->thread.regs);
  452. pos /= sizeof(reg);
  453. count /= sizeof(reg);
  454. if (kbuf)
  455. for (; count > 0 && pos < PT_MSR; --count)
  456. regs[pos++] = *k++;
  457. else
  458. for (; count > 0 && pos < PT_MSR; --count) {
  459. if (__get_user(reg, u++))
  460. return -EFAULT;
  461. regs[pos++] = reg;
  462. }
  463. if (count > 0 && pos == PT_MSR) {
  464. if (kbuf)
  465. reg = *k++;
  466. else if (__get_user(reg, u++))
  467. return -EFAULT;
  468. set_user_msr(target, reg);
  469. ++pos;
  470. --count;
  471. }
  472. if (kbuf) {
  473. for (; count > 0 && pos <= PT_MAX_PUT_REG; --count)
  474. regs[pos++] = *k++;
  475. for (; count > 0 && pos < PT_TRAP; --count, ++pos)
  476. ++k;
  477. } else {
  478. for (; count > 0 && pos <= PT_MAX_PUT_REG; --count) {
  479. if (__get_user(reg, u++))
  480. return -EFAULT;
  481. regs[pos++] = reg;
  482. }
  483. for (; count > 0 && pos < PT_TRAP; --count, ++pos)
  484. if (__get_user(reg, u++))
  485. return -EFAULT;
  486. }
  487. if (count > 0 && pos == PT_TRAP) {
  488. if (kbuf)
  489. reg = *k++;
  490. else if (__get_user(reg, u++))
  491. return -EFAULT;
  492. set_user_trap(target, reg);
  493. ++pos;
  494. --count;
  495. }
  496. kbuf = k;
  497. ubuf = u;
  498. pos *= sizeof(reg);
  499. count *= sizeof(reg);
  500. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  501. (PT_TRAP + 1) * sizeof(reg), -1);
  502. }
  503. /*
  504. * These are the regset flavors matching the CONFIG_PPC32 native set.
  505. */
  506. static const struct user_regset compat_regsets[] = {
  507. [REGSET_GPR] = {
  508. .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
  509. .size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
  510. .get = gpr32_get, .set = gpr32_set
  511. },
  512. [REGSET_FPR] = {
  513. .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
  514. .size = sizeof(double), .align = sizeof(double),
  515. .get = fpr_get, .set = fpr_set
  516. },
  517. #ifdef CONFIG_ALTIVEC
  518. [REGSET_VMX] = {
  519. .core_note_type = NT_PPC_VMX, .n = 34,
  520. .size = sizeof(vector128), .align = sizeof(vector128),
  521. .active = vr_active, .get = vr_get, .set = vr_set
  522. },
  523. #endif
  524. #ifdef CONFIG_SPE
  525. [REGSET_SPE] = {
  526. .core_note_type = NT_PPC_SPE, .n = 35,
  527. .size = sizeof(u32), .align = sizeof(u32),
  528. .active = evr_active, .get = evr_get, .set = evr_set
  529. },
  530. #endif
  531. };
  532. static const struct user_regset_view user_ppc_compat_view = {
  533. .name = "ppc", .e_machine = EM_PPC, .ei_osabi = ELF_OSABI,
  534. .regsets = compat_regsets, .n = ARRAY_SIZE(compat_regsets)
  535. };
  536. #endif /* CONFIG_PPC64 */
  537. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  538. {
  539. #ifdef CONFIG_PPC64
  540. if (test_tsk_thread_flag(task, TIF_32BIT))
  541. return &user_ppc_compat_view;
  542. #endif
  543. return &user_ppc_native_view;
  544. }
  545. void user_enable_single_step(struct task_struct *task)
  546. {
  547. struct pt_regs *regs = task->thread.regs;
  548. if (regs != NULL) {
  549. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  550. task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
  551. regs->msr |= MSR_DE;
  552. #else
  553. regs->msr |= MSR_SE;
  554. #endif
  555. }
  556. set_tsk_thread_flag(task, TIF_SINGLESTEP);
  557. }
  558. void user_disable_single_step(struct task_struct *task)
  559. {
  560. struct pt_regs *regs = task->thread.regs;
  561. if (regs != NULL) {
  562. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  563. task->thread.dbcr0 = 0;
  564. regs->msr &= ~MSR_DE;
  565. #else
  566. regs->msr &= ~MSR_SE;
  567. #endif
  568. }
  569. clear_tsk_thread_flag(task, TIF_SINGLESTEP);
  570. }
  571. static int ptrace_set_debugreg(struct task_struct *task, unsigned long addr,
  572. unsigned long data)
  573. {
  574. /* We only support one DABR and no IABRS at the moment */
  575. if (addr > 0)
  576. return -EINVAL;
  577. /* The bottom 3 bits are flags */
  578. if ((data & ~0x7UL) >= TASK_SIZE)
  579. return -EIO;
  580. /* Ensure translation is on */
  581. if (data && !(data & DABR_TRANSLATION))
  582. return -EIO;
  583. task->thread.dabr = data;
  584. return 0;
  585. }
  586. /*
  587. * Called by kernel/ptrace.c when detaching..
  588. *
  589. * Make sure single step bits etc are not set.
  590. */
  591. void ptrace_disable(struct task_struct *child)
  592. {
  593. /* make sure the single step bit is not set. */
  594. user_disable_single_step(child);
  595. }
  596. /*
  597. * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
  598. * we mark them as obsolete now, they will be removed in a future version
  599. */
  600. static long arch_ptrace_old(struct task_struct *child, long request, long addr,
  601. long data)
  602. {
  603. switch (request) {
  604. case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
  605. return copy_regset_to_user(child, &user_ppc_native_view,
  606. REGSET_GPR, 0, 32 * sizeof(long),
  607. (void __user *) data);
  608. case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
  609. return copy_regset_from_user(child, &user_ppc_native_view,
  610. REGSET_GPR, 0, 32 * sizeof(long),
  611. (const void __user *) data);
  612. case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
  613. return copy_regset_to_user(child, &user_ppc_native_view,
  614. REGSET_FPR, 0, 32 * sizeof(double),
  615. (void __user *) data);
  616. case PPC_PTRACE_SETFPREGS: /* Set FPRs 0 - 31. */
  617. return copy_regset_from_user(child, &user_ppc_native_view,
  618. REGSET_FPR, 0, 32 * sizeof(double),
  619. (const void __user *) data);
  620. }
  621. return -EPERM;
  622. }
  623. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  624. {
  625. int ret = -EPERM;
  626. switch (request) {
  627. /* read the word at location addr in the USER area. */
  628. case PTRACE_PEEKUSR: {
  629. unsigned long index, tmp;
  630. ret = -EIO;
  631. /* convert to index and check */
  632. #ifdef CONFIG_PPC32
  633. index = (unsigned long) addr >> 2;
  634. if ((addr & 3) || (index > PT_FPSCR)
  635. || (child->thread.regs == NULL))
  636. #else
  637. index = (unsigned long) addr >> 3;
  638. if ((addr & 7) || (index > PT_FPSCR))
  639. #endif
  640. break;
  641. CHECK_FULL_REGS(child->thread.regs);
  642. if (index < PT_FPR0) {
  643. tmp = ptrace_get_reg(child, (int) index);
  644. } else {
  645. flush_fp_to_thread(child);
  646. tmp = ((unsigned long *)child->thread.fpr)
  647. [TS_FPRWIDTH * (index - PT_FPR0)];
  648. }
  649. ret = put_user(tmp,(unsigned long __user *) data);
  650. break;
  651. }
  652. /* write the word at location addr in the USER area */
  653. case PTRACE_POKEUSR: {
  654. unsigned long index;
  655. ret = -EIO;
  656. /* convert to index and check */
  657. #ifdef CONFIG_PPC32
  658. index = (unsigned long) addr >> 2;
  659. if ((addr & 3) || (index > PT_FPSCR)
  660. || (child->thread.regs == NULL))
  661. #else
  662. index = (unsigned long) addr >> 3;
  663. if ((addr & 7) || (index > PT_FPSCR))
  664. #endif
  665. break;
  666. CHECK_FULL_REGS(child->thread.regs);
  667. if (index < PT_FPR0) {
  668. ret = ptrace_put_reg(child, index, data);
  669. } else {
  670. flush_fp_to_thread(child);
  671. ((unsigned long *)child->thread.fpr)
  672. [TS_FPRWIDTH * (index - PT_FPR0)] = data;
  673. ret = 0;
  674. }
  675. break;
  676. }
  677. case PTRACE_GET_DEBUGREG: {
  678. ret = -EINVAL;
  679. /* We only support one DABR and no IABRS at the moment */
  680. if (addr > 0)
  681. break;
  682. ret = put_user(child->thread.dabr,
  683. (unsigned long __user *)data);
  684. break;
  685. }
  686. case PTRACE_SET_DEBUGREG:
  687. ret = ptrace_set_debugreg(child, addr, data);
  688. break;
  689. #ifdef CONFIG_PPC64
  690. case PTRACE_GETREGS64:
  691. #endif
  692. case PTRACE_GETREGS: /* Get all pt_regs from the child. */
  693. return copy_regset_to_user(child, &user_ppc_native_view,
  694. REGSET_GPR,
  695. 0, sizeof(struct pt_regs),
  696. (void __user *) data);
  697. #ifdef CONFIG_PPC64
  698. case PTRACE_SETREGS64:
  699. #endif
  700. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  701. return copy_regset_from_user(child, &user_ppc_native_view,
  702. REGSET_GPR,
  703. 0, sizeof(struct pt_regs),
  704. (const void __user *) data);
  705. case PTRACE_GETFPREGS: /* Get the child FPU state (FPR0...31 + FPSCR) */
  706. return copy_regset_to_user(child, &user_ppc_native_view,
  707. REGSET_FPR,
  708. 0, sizeof(elf_fpregset_t),
  709. (void __user *) data);
  710. case PTRACE_SETFPREGS: /* Set the child FPU state (FPR0...31 + FPSCR) */
  711. return copy_regset_from_user(child, &user_ppc_native_view,
  712. REGSET_FPR,
  713. 0, sizeof(elf_fpregset_t),
  714. (const void __user *) data);
  715. #ifdef CONFIG_ALTIVEC
  716. case PTRACE_GETVRREGS:
  717. return copy_regset_to_user(child, &user_ppc_native_view,
  718. REGSET_VMX,
  719. 0, (33 * sizeof(vector128) +
  720. sizeof(u32)),
  721. (void __user *) data);
  722. case PTRACE_SETVRREGS:
  723. return copy_regset_from_user(child, &user_ppc_native_view,
  724. REGSET_VMX,
  725. 0, (33 * sizeof(vector128) +
  726. sizeof(u32)),
  727. (const void __user *) data);
  728. #endif
  729. #ifdef CONFIG_SPE
  730. case PTRACE_GETEVRREGS:
  731. /* Get the child spe register state. */
  732. return copy_regset_to_user(child, &user_ppc_native_view,
  733. REGSET_SPE, 0, 35 * sizeof(u32),
  734. (void __user *) data);
  735. case PTRACE_SETEVRREGS:
  736. /* Set the child spe register state. */
  737. return copy_regset_from_user(child, &user_ppc_native_view,
  738. REGSET_SPE, 0, 35 * sizeof(u32),
  739. (const void __user *) data);
  740. #endif
  741. /* Old reverse args ptrace callss */
  742. case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
  743. case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
  744. case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
  745. case PPC_PTRACE_SETFPREGS: /* Get FPRs 0 - 31. */
  746. ret = arch_ptrace_old(child, request, addr, data);
  747. break;
  748. default:
  749. ret = ptrace_request(child, request, addr, data);
  750. break;
  751. }
  752. return ret;
  753. }
  754. static void do_syscall_trace(void)
  755. {
  756. /* the 0x80 provides a way for the tracing parent to distinguish
  757. between a syscall stop and SIGTRAP delivery */
  758. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  759. ? 0x80 : 0));
  760. /*
  761. * this isn't the same as continuing with a signal, but it will do
  762. * for normal use. strace only continues with a signal if the
  763. * stopping signal is not SIGTRAP. -brl
  764. */
  765. if (current->exit_code) {
  766. send_sig(current->exit_code, current, 1);
  767. current->exit_code = 0;
  768. }
  769. }
  770. void do_syscall_trace_enter(struct pt_regs *regs)
  771. {
  772. secure_computing(regs->gpr[0]);
  773. if (test_thread_flag(TIF_SYSCALL_TRACE)
  774. && (current->ptrace & PT_PTRACED))
  775. do_syscall_trace();
  776. if (unlikely(current->audit_context)) {
  777. #ifdef CONFIG_PPC64
  778. if (!test_thread_flag(TIF_32BIT))
  779. audit_syscall_entry(AUDIT_ARCH_PPC64,
  780. regs->gpr[0],
  781. regs->gpr[3], regs->gpr[4],
  782. regs->gpr[5], regs->gpr[6]);
  783. else
  784. #endif
  785. audit_syscall_entry(AUDIT_ARCH_PPC,
  786. regs->gpr[0],
  787. regs->gpr[3] & 0xffffffff,
  788. regs->gpr[4] & 0xffffffff,
  789. regs->gpr[5] & 0xffffffff,
  790. regs->gpr[6] & 0xffffffff);
  791. }
  792. }
  793. void do_syscall_trace_leave(struct pt_regs *regs)
  794. {
  795. if (unlikely(current->audit_context))
  796. audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
  797. regs->result);
  798. if ((test_thread_flag(TIF_SYSCALL_TRACE)
  799. || test_thread_flag(TIF_SINGLESTEP))
  800. && (current->ptrace & PT_PTRACED))
  801. do_syscall_trace();
  802. }