ptrace.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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/tracehook.h>
  25. #include <linux/elf.h>
  26. #include <linux/user.h>
  27. #include <linux/security.h>
  28. #include <linux/signal.h>
  29. #include <linux/seccomp.h>
  30. #include <linux/audit.h>
  31. #ifdef CONFIG_PPC32
  32. #include <linux/module.h>
  33. #endif
  34. #include <asm/uaccess.h>
  35. #include <asm/page.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/system.h>
  38. /*
  39. * does not yet catch signals sent when the child dies.
  40. * in exit.c or in signal.c.
  41. */
  42. /*
  43. * Set of msr bits that gdb can change on behalf of a process.
  44. */
  45. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  46. #define MSR_DEBUGCHANGE 0
  47. #else
  48. #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
  49. #endif
  50. /*
  51. * Max register writeable via put_reg
  52. */
  53. #ifdef CONFIG_PPC32
  54. #define PT_MAX_PUT_REG PT_MQ
  55. #else
  56. #define PT_MAX_PUT_REG PT_CCR
  57. #endif
  58. static unsigned long get_user_msr(struct task_struct *task)
  59. {
  60. return task->thread.regs->msr | task->thread.fpexc_mode;
  61. }
  62. static int set_user_msr(struct task_struct *task, unsigned long msr)
  63. {
  64. task->thread.regs->msr &= ~MSR_DEBUGCHANGE;
  65. task->thread.regs->msr |= msr & MSR_DEBUGCHANGE;
  66. return 0;
  67. }
  68. /*
  69. * We prevent mucking around with the reserved area of trap
  70. * which are used internally by the kernel.
  71. */
  72. static int set_user_trap(struct task_struct *task, unsigned long trap)
  73. {
  74. task->thread.regs->trap = trap & 0xfff0;
  75. return 0;
  76. }
  77. /*
  78. * Get contents of register REGNO in task TASK.
  79. */
  80. unsigned long ptrace_get_reg(struct task_struct *task, int regno)
  81. {
  82. if (task->thread.regs == NULL)
  83. return -EIO;
  84. if (regno == PT_MSR)
  85. return get_user_msr(task);
  86. if (regno < (sizeof(struct pt_regs) / sizeof(unsigned long)))
  87. return ((unsigned long *)task->thread.regs)[regno];
  88. return -EIO;
  89. }
  90. /*
  91. * Write contents of register REGNO in task TASK.
  92. */
  93. int ptrace_put_reg(struct task_struct *task, int regno, unsigned long data)
  94. {
  95. if (task->thread.regs == NULL)
  96. return -EIO;
  97. if (regno == PT_MSR)
  98. return set_user_msr(task, data);
  99. if (regno == PT_TRAP)
  100. return set_user_trap(task, data);
  101. if (regno <= PT_MAX_PUT_REG) {
  102. ((unsigned long *)task->thread.regs)[regno] = data;
  103. return 0;
  104. }
  105. return -EIO;
  106. }
  107. static int gpr_get(struct task_struct *target, const struct user_regset *regset,
  108. unsigned int pos, unsigned int count,
  109. void *kbuf, void __user *ubuf)
  110. {
  111. int ret;
  112. if (target->thread.regs == NULL)
  113. return -EIO;
  114. CHECK_FULL_REGS(target->thread.regs);
  115. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  116. target->thread.regs,
  117. 0, offsetof(struct pt_regs, msr));
  118. if (!ret) {
  119. unsigned long msr = get_user_msr(target);
  120. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &msr,
  121. offsetof(struct pt_regs, msr),
  122. offsetof(struct pt_regs, msr) +
  123. sizeof(msr));
  124. }
  125. BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
  126. offsetof(struct pt_regs, msr) + sizeof(long));
  127. if (!ret)
  128. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  129. &target->thread.regs->orig_gpr3,
  130. offsetof(struct pt_regs, orig_gpr3),
  131. sizeof(struct pt_regs));
  132. if (!ret)
  133. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  134. sizeof(struct pt_regs), -1);
  135. return ret;
  136. }
  137. static int gpr_set(struct task_struct *target, const struct user_regset *regset,
  138. unsigned int pos, unsigned int count,
  139. const void *kbuf, const void __user *ubuf)
  140. {
  141. unsigned long reg;
  142. int ret;
  143. if (target->thread.regs == NULL)
  144. return -EIO;
  145. CHECK_FULL_REGS(target->thread.regs);
  146. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  147. target->thread.regs,
  148. 0, PT_MSR * sizeof(reg));
  149. if (!ret && count > 0) {
  150. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &reg,
  151. PT_MSR * sizeof(reg),
  152. (PT_MSR + 1) * sizeof(reg));
  153. if (!ret)
  154. ret = set_user_msr(target, reg);
  155. }
  156. BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
  157. offsetof(struct pt_regs, msr) + sizeof(long));
  158. if (!ret)
  159. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  160. &target->thread.regs->orig_gpr3,
  161. PT_ORIG_R3 * sizeof(reg),
  162. (PT_MAX_PUT_REG + 1) * sizeof(reg));
  163. if (PT_MAX_PUT_REG + 1 < PT_TRAP && !ret)
  164. ret = user_regset_copyin_ignore(
  165. &pos, &count, &kbuf, &ubuf,
  166. (PT_MAX_PUT_REG + 1) * sizeof(reg),
  167. PT_TRAP * sizeof(reg));
  168. if (!ret && count > 0) {
  169. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &reg,
  170. PT_TRAP * sizeof(reg),
  171. (PT_TRAP + 1) * sizeof(reg));
  172. if (!ret)
  173. ret = set_user_trap(target, reg);
  174. }
  175. if (!ret)
  176. ret = user_regset_copyin_ignore(
  177. &pos, &count, &kbuf, &ubuf,
  178. (PT_TRAP + 1) * sizeof(reg), -1);
  179. return ret;
  180. }
  181. static int fpr_get(struct task_struct *target, const struct user_regset *regset,
  182. unsigned int pos, unsigned int count,
  183. void *kbuf, void __user *ubuf)
  184. {
  185. #ifdef CONFIG_VSX
  186. double buf[33];
  187. int i;
  188. #endif
  189. flush_fp_to_thread(target);
  190. #ifdef CONFIG_VSX
  191. /* copy to local buffer then write that out */
  192. for (i = 0; i < 32 ; i++)
  193. buf[i] = target->thread.TS_FPR(i);
  194. memcpy(&buf[32], &target->thread.fpscr, sizeof(double));
  195. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, buf, 0, -1);
  196. #else
  197. BUILD_BUG_ON(offsetof(struct thread_struct, fpscr) !=
  198. offsetof(struct thread_struct, TS_FPR(32)));
  199. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  200. &target->thread.fpr, 0, -1);
  201. #endif
  202. }
  203. static int fpr_set(struct task_struct *target, const struct user_regset *regset,
  204. unsigned int pos, unsigned int count,
  205. const void *kbuf, const void __user *ubuf)
  206. {
  207. #ifdef CONFIG_VSX
  208. double buf[33];
  209. int i;
  210. #endif
  211. flush_fp_to_thread(target);
  212. #ifdef CONFIG_VSX
  213. /* copy to local buffer then write that out */
  214. i = user_regset_copyin(&pos, &count, &kbuf, &ubuf, buf, 0, -1);
  215. if (i)
  216. return i;
  217. for (i = 0; i < 32 ; i++)
  218. target->thread.TS_FPR(i) = buf[i];
  219. memcpy(&target->thread.fpscr, &buf[32], sizeof(double));
  220. return 0;
  221. #else
  222. BUILD_BUG_ON(offsetof(struct thread_struct, fpscr) !=
  223. offsetof(struct thread_struct, TS_FPR(32)));
  224. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  225. &target->thread.fpr, 0, -1);
  226. #endif
  227. }
  228. #ifdef CONFIG_ALTIVEC
  229. /*
  230. * Get/set all the altivec registers vr0..vr31, vscr, vrsave, in one go.
  231. * The transfer totals 34 quadword. Quadwords 0-31 contain the
  232. * corresponding vector registers. Quadword 32 contains the vscr as the
  233. * last word (offset 12) within that quadword. Quadword 33 contains the
  234. * vrsave as the first word (offset 0) within the quadword.
  235. *
  236. * This definition of the VMX state is compatible with the current PPC32
  237. * ptrace interface. This allows signal handling and ptrace to use the
  238. * same structures. This also simplifies the implementation of a bi-arch
  239. * (combined (32- and 64-bit) gdb.
  240. */
  241. static int vr_active(struct task_struct *target,
  242. const struct user_regset *regset)
  243. {
  244. flush_altivec_to_thread(target);
  245. return target->thread.used_vr ? regset->n : 0;
  246. }
  247. static int vr_get(struct task_struct *target, const struct user_regset *regset,
  248. unsigned int pos, unsigned int count,
  249. void *kbuf, void __user *ubuf)
  250. {
  251. int ret;
  252. flush_altivec_to_thread(target);
  253. BUILD_BUG_ON(offsetof(struct thread_struct, vscr) !=
  254. offsetof(struct thread_struct, vr[32]));
  255. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  256. &target->thread.vr, 0,
  257. 33 * sizeof(vector128));
  258. if (!ret) {
  259. /*
  260. * Copy out only the low-order word of vrsave.
  261. */
  262. union {
  263. elf_vrreg_t reg;
  264. u32 word;
  265. } vrsave;
  266. memset(&vrsave, 0, sizeof(vrsave));
  267. vrsave.word = target->thread.vrsave;
  268. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &vrsave,
  269. 33 * sizeof(vector128), -1);
  270. }
  271. return ret;
  272. }
  273. static int vr_set(struct task_struct *target, const struct user_regset *regset,
  274. unsigned int pos, unsigned int count,
  275. const void *kbuf, const void __user *ubuf)
  276. {
  277. int ret;
  278. flush_altivec_to_thread(target);
  279. BUILD_BUG_ON(offsetof(struct thread_struct, vscr) !=
  280. offsetof(struct thread_struct, vr[32]));
  281. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  282. &target->thread.vr, 0, 33 * sizeof(vector128));
  283. if (!ret && count > 0) {
  284. /*
  285. * We use only the first word of vrsave.
  286. */
  287. union {
  288. elf_vrreg_t reg;
  289. u32 word;
  290. } vrsave;
  291. memset(&vrsave, 0, sizeof(vrsave));
  292. vrsave.word = target->thread.vrsave;
  293. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &vrsave,
  294. 33 * sizeof(vector128), -1);
  295. if (!ret)
  296. target->thread.vrsave = vrsave.word;
  297. }
  298. return ret;
  299. }
  300. #endif /* CONFIG_ALTIVEC */
  301. #ifdef CONFIG_VSX
  302. /*
  303. * Currently to set and and get all the vsx state, you need to call
  304. * the fp and VMX calls aswell. This only get/sets the lower 32
  305. * 128bit VSX registers.
  306. */
  307. static int vsr_active(struct task_struct *target,
  308. const struct user_regset *regset)
  309. {
  310. flush_vsx_to_thread(target);
  311. return target->thread.used_vsr ? regset->n : 0;
  312. }
  313. static int vsr_get(struct task_struct *target, const struct user_regset *regset,
  314. unsigned int pos, unsigned int count,
  315. void *kbuf, void __user *ubuf)
  316. {
  317. double buf[32];
  318. int ret, i;
  319. flush_vsx_to_thread(target);
  320. for (i = 0; i < 32 ; i++)
  321. buf[i] = target->thread.fpr[i][TS_VSRLOWOFFSET];
  322. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  323. buf, 0, 32 * sizeof(double));
  324. return ret;
  325. }
  326. static int vsr_set(struct task_struct *target, const struct user_regset *regset,
  327. unsigned int pos, unsigned int count,
  328. const void *kbuf, const void __user *ubuf)
  329. {
  330. double buf[32];
  331. int ret,i;
  332. flush_vsx_to_thread(target);
  333. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  334. buf, 0, 32 * sizeof(double));
  335. for (i = 0; i < 32 ; i++)
  336. target->thread.fpr[i][TS_VSRLOWOFFSET] = buf[i];
  337. return ret;
  338. }
  339. #endif /* CONFIG_VSX */
  340. #ifdef CONFIG_SPE
  341. /*
  342. * For get_evrregs/set_evrregs functions 'data' has the following layout:
  343. *
  344. * struct {
  345. * u32 evr[32];
  346. * u64 acc;
  347. * u32 spefscr;
  348. * }
  349. */
  350. static int evr_active(struct task_struct *target,
  351. const struct user_regset *regset)
  352. {
  353. flush_spe_to_thread(target);
  354. return target->thread.used_spe ? regset->n : 0;
  355. }
  356. static int evr_get(struct task_struct *target, const struct user_regset *regset,
  357. unsigned int pos, unsigned int count,
  358. void *kbuf, void __user *ubuf)
  359. {
  360. int ret;
  361. flush_spe_to_thread(target);
  362. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  363. &target->thread.evr,
  364. 0, sizeof(target->thread.evr));
  365. BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
  366. offsetof(struct thread_struct, spefscr));
  367. if (!ret)
  368. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  369. &target->thread.acc,
  370. sizeof(target->thread.evr), -1);
  371. return ret;
  372. }
  373. static int evr_set(struct task_struct *target, const struct user_regset *regset,
  374. unsigned int pos, unsigned int count,
  375. const void *kbuf, const void __user *ubuf)
  376. {
  377. int ret;
  378. flush_spe_to_thread(target);
  379. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  380. &target->thread.evr,
  381. 0, sizeof(target->thread.evr));
  382. BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
  383. offsetof(struct thread_struct, spefscr));
  384. if (!ret)
  385. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  386. &target->thread.acc,
  387. sizeof(target->thread.evr), -1);
  388. return ret;
  389. }
  390. #endif /* CONFIG_SPE */
  391. /*
  392. * These are our native regset flavors.
  393. */
  394. enum powerpc_regset {
  395. REGSET_GPR,
  396. REGSET_FPR,
  397. #ifdef CONFIG_ALTIVEC
  398. REGSET_VMX,
  399. #endif
  400. #ifdef CONFIG_VSX
  401. REGSET_VSX,
  402. #endif
  403. #ifdef CONFIG_SPE
  404. REGSET_SPE,
  405. #endif
  406. };
  407. static const struct user_regset native_regsets[] = {
  408. [REGSET_GPR] = {
  409. .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
  410. .size = sizeof(long), .align = sizeof(long),
  411. .get = gpr_get, .set = gpr_set
  412. },
  413. [REGSET_FPR] = {
  414. .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
  415. .size = sizeof(double), .align = sizeof(double),
  416. .get = fpr_get, .set = fpr_set
  417. },
  418. #ifdef CONFIG_ALTIVEC
  419. [REGSET_VMX] = {
  420. .core_note_type = NT_PPC_VMX, .n = 34,
  421. .size = sizeof(vector128), .align = sizeof(vector128),
  422. .active = vr_active, .get = vr_get, .set = vr_set
  423. },
  424. #endif
  425. #ifdef CONFIG_VSX
  426. [REGSET_VSX] = {
  427. .core_note_type = NT_PPC_VSX, .n = 32,
  428. .size = sizeof(double), .align = sizeof(double),
  429. .active = vsr_active, .get = vsr_get, .set = vsr_set
  430. },
  431. #endif
  432. #ifdef CONFIG_SPE
  433. [REGSET_SPE] = {
  434. .n = 35,
  435. .size = sizeof(u32), .align = sizeof(u32),
  436. .active = evr_active, .get = evr_get, .set = evr_set
  437. },
  438. #endif
  439. };
  440. static const struct user_regset_view user_ppc_native_view = {
  441. .name = UTS_MACHINE, .e_machine = ELF_ARCH, .ei_osabi = ELF_OSABI,
  442. .regsets = native_regsets, .n = ARRAY_SIZE(native_regsets)
  443. };
  444. #ifdef CONFIG_PPC64
  445. #include <linux/compat.h>
  446. static int gpr32_get(struct task_struct *target,
  447. const struct user_regset *regset,
  448. unsigned int pos, unsigned int count,
  449. void *kbuf, void __user *ubuf)
  450. {
  451. const unsigned long *regs = &target->thread.regs->gpr[0];
  452. compat_ulong_t *k = kbuf;
  453. compat_ulong_t __user *u = ubuf;
  454. compat_ulong_t reg;
  455. if (target->thread.regs == NULL)
  456. return -EIO;
  457. CHECK_FULL_REGS(target->thread.regs);
  458. pos /= sizeof(reg);
  459. count /= sizeof(reg);
  460. if (kbuf)
  461. for (; count > 0 && pos < PT_MSR; --count)
  462. *k++ = regs[pos++];
  463. else
  464. for (; count > 0 && pos < PT_MSR; --count)
  465. if (__put_user((compat_ulong_t) regs[pos++], u++))
  466. return -EFAULT;
  467. if (count > 0 && pos == PT_MSR) {
  468. reg = get_user_msr(target);
  469. if (kbuf)
  470. *k++ = reg;
  471. else if (__put_user(reg, u++))
  472. return -EFAULT;
  473. ++pos;
  474. --count;
  475. }
  476. if (kbuf)
  477. for (; count > 0 && pos < PT_REGS_COUNT; --count)
  478. *k++ = regs[pos++];
  479. else
  480. for (; count > 0 && pos < PT_REGS_COUNT; --count)
  481. if (__put_user((compat_ulong_t) regs[pos++], u++))
  482. return -EFAULT;
  483. kbuf = k;
  484. ubuf = u;
  485. pos *= sizeof(reg);
  486. count *= sizeof(reg);
  487. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  488. PT_REGS_COUNT * sizeof(reg), -1);
  489. }
  490. static int gpr32_set(struct task_struct *target,
  491. const struct user_regset *regset,
  492. unsigned int pos, unsigned int count,
  493. const void *kbuf, const void __user *ubuf)
  494. {
  495. unsigned long *regs = &target->thread.regs->gpr[0];
  496. const compat_ulong_t *k = kbuf;
  497. const compat_ulong_t __user *u = ubuf;
  498. compat_ulong_t reg;
  499. if (target->thread.regs == NULL)
  500. return -EIO;
  501. CHECK_FULL_REGS(target->thread.regs);
  502. pos /= sizeof(reg);
  503. count /= sizeof(reg);
  504. if (kbuf)
  505. for (; count > 0 && pos < PT_MSR; --count)
  506. regs[pos++] = *k++;
  507. else
  508. for (; count > 0 && pos < PT_MSR; --count) {
  509. if (__get_user(reg, u++))
  510. return -EFAULT;
  511. regs[pos++] = reg;
  512. }
  513. if (count > 0 && pos == PT_MSR) {
  514. if (kbuf)
  515. reg = *k++;
  516. else if (__get_user(reg, u++))
  517. return -EFAULT;
  518. set_user_msr(target, reg);
  519. ++pos;
  520. --count;
  521. }
  522. if (kbuf) {
  523. for (; count > 0 && pos <= PT_MAX_PUT_REG; --count)
  524. regs[pos++] = *k++;
  525. for (; count > 0 && pos < PT_TRAP; --count, ++pos)
  526. ++k;
  527. } else {
  528. for (; count > 0 && pos <= PT_MAX_PUT_REG; --count) {
  529. if (__get_user(reg, u++))
  530. return -EFAULT;
  531. regs[pos++] = reg;
  532. }
  533. for (; count > 0 && pos < PT_TRAP; --count, ++pos)
  534. if (__get_user(reg, u++))
  535. return -EFAULT;
  536. }
  537. if (count > 0 && pos == PT_TRAP) {
  538. if (kbuf)
  539. reg = *k++;
  540. else if (__get_user(reg, u++))
  541. return -EFAULT;
  542. set_user_trap(target, reg);
  543. ++pos;
  544. --count;
  545. }
  546. kbuf = k;
  547. ubuf = u;
  548. pos *= sizeof(reg);
  549. count *= sizeof(reg);
  550. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  551. (PT_TRAP + 1) * sizeof(reg), -1);
  552. }
  553. /*
  554. * These are the regset flavors matching the CONFIG_PPC32 native set.
  555. */
  556. static const struct user_regset compat_regsets[] = {
  557. [REGSET_GPR] = {
  558. .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
  559. .size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
  560. .get = gpr32_get, .set = gpr32_set
  561. },
  562. [REGSET_FPR] = {
  563. .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
  564. .size = sizeof(double), .align = sizeof(double),
  565. .get = fpr_get, .set = fpr_set
  566. },
  567. #ifdef CONFIG_ALTIVEC
  568. [REGSET_VMX] = {
  569. .core_note_type = NT_PPC_VMX, .n = 34,
  570. .size = sizeof(vector128), .align = sizeof(vector128),
  571. .active = vr_active, .get = vr_get, .set = vr_set
  572. },
  573. #endif
  574. #ifdef CONFIG_SPE
  575. [REGSET_SPE] = {
  576. .core_note_type = NT_PPC_SPE, .n = 35,
  577. .size = sizeof(u32), .align = sizeof(u32),
  578. .active = evr_active, .get = evr_get, .set = evr_set
  579. },
  580. #endif
  581. };
  582. static const struct user_regset_view user_ppc_compat_view = {
  583. .name = "ppc", .e_machine = EM_PPC, .ei_osabi = ELF_OSABI,
  584. .regsets = compat_regsets, .n = ARRAY_SIZE(compat_regsets)
  585. };
  586. #endif /* CONFIG_PPC64 */
  587. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  588. {
  589. #ifdef CONFIG_PPC64
  590. if (test_tsk_thread_flag(task, TIF_32BIT))
  591. return &user_ppc_compat_view;
  592. #endif
  593. return &user_ppc_native_view;
  594. }
  595. void user_enable_single_step(struct task_struct *task)
  596. {
  597. struct pt_regs *regs = task->thread.regs;
  598. if (regs != NULL) {
  599. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  600. task->thread.dbcr0 &= ~DBCR0_BT;
  601. task->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
  602. regs->msr |= MSR_DE;
  603. #else
  604. regs->msr &= ~MSR_BE;
  605. regs->msr |= MSR_SE;
  606. #endif
  607. }
  608. set_tsk_thread_flag(task, TIF_SINGLESTEP);
  609. }
  610. void user_enable_block_step(struct task_struct *task)
  611. {
  612. struct pt_regs *regs = task->thread.regs;
  613. if (regs != NULL) {
  614. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  615. task->thread.dbcr0 &= ~DBCR0_IC;
  616. task->thread.dbcr0 = DBCR0_IDM | DBCR0_BT;
  617. regs->msr |= MSR_DE;
  618. #else
  619. regs->msr &= ~MSR_SE;
  620. regs->msr |= MSR_BE;
  621. #endif
  622. }
  623. set_tsk_thread_flag(task, TIF_SINGLESTEP);
  624. }
  625. void user_disable_single_step(struct task_struct *task)
  626. {
  627. struct pt_regs *regs = task->thread.regs;
  628. if (regs != NULL) {
  629. #if defined(CONFIG_BOOKE)
  630. /* If DAC don't clear DBCRO_IDM or MSR_DE */
  631. if (task->thread.dabr)
  632. task->thread.dbcr0 &= ~(DBCR0_IC | DBCR0_BT);
  633. else {
  634. task->thread.dbcr0 &= ~(DBCR0_IC | DBCR0_BT | DBCR0_IDM);
  635. regs->msr &= ~MSR_DE;
  636. }
  637. #elif defined(CONFIG_40x)
  638. task->thread.dbcr0 &= ~(DBCR0_IC | DBCR0_BT | DBCR0_IDM);
  639. regs->msr &= ~MSR_DE;
  640. #else
  641. regs->msr &= ~(MSR_SE | MSR_BE);
  642. #endif
  643. }
  644. clear_tsk_thread_flag(task, TIF_SINGLESTEP);
  645. }
  646. int ptrace_set_debugreg(struct task_struct *task, unsigned long addr,
  647. unsigned long data)
  648. {
  649. /* For ppc64 we support one DABR and no IABR's at the moment (ppc64).
  650. * For embedded processors we support one DAC and no IAC's at the
  651. * moment.
  652. */
  653. if (addr > 0)
  654. return -EINVAL;
  655. /* The bottom 3 bits in dabr are flags */
  656. if ((data & ~0x7UL) >= TASK_SIZE)
  657. return -EIO;
  658. #ifndef CONFIG_BOOKE
  659. /* For processors using DABR (i.e. 970), the bottom 3 bits are flags.
  660. * It was assumed, on previous implementations, that 3 bits were
  661. * passed together with the data address, fitting the design of the
  662. * DABR register, as follows:
  663. *
  664. * bit 0: Read flag
  665. * bit 1: Write flag
  666. * bit 2: Breakpoint translation
  667. *
  668. * Thus, we use them here as so.
  669. */
  670. /* Ensure breakpoint translation bit is set */
  671. if (data && !(data & DABR_TRANSLATION))
  672. return -EIO;
  673. /* Move contents to the DABR register */
  674. task->thread.dabr = data;
  675. #endif
  676. #if defined(CONFIG_BOOKE)
  677. /* As described above, it was assumed 3 bits were passed with the data
  678. * address, but we will assume only the mode bits will be passed
  679. * as to not cause alignment restrictions for DAC-based processors.
  680. */
  681. /* DAC's hold the whole address without any mode flags */
  682. task->thread.dabr = data & ~0x3UL;
  683. if (task->thread.dabr == 0) {
  684. task->thread.dbcr0 &= ~(DBSR_DAC1R | DBSR_DAC1W | DBCR0_IDM);
  685. task->thread.regs->msr &= ~MSR_DE;
  686. return 0;
  687. }
  688. /* Read or Write bits must be set */
  689. if (!(data & 0x3UL))
  690. return -EINVAL;
  691. /* Set the Internal Debugging flag (IDM bit 1) for the DBCR0
  692. register */
  693. task->thread.dbcr0 = DBCR0_IDM;
  694. /* Check for write and read flags and set DBCR0
  695. accordingly */
  696. if (data & 0x1UL)
  697. task->thread.dbcr0 |= DBSR_DAC1R;
  698. if (data & 0x2UL)
  699. task->thread.dbcr0 |= DBSR_DAC1W;
  700. task->thread.regs->msr |= MSR_DE;
  701. #endif
  702. return 0;
  703. }
  704. /*
  705. * Called by kernel/ptrace.c when detaching..
  706. *
  707. * Make sure single step bits etc are not set.
  708. */
  709. void ptrace_disable(struct task_struct *child)
  710. {
  711. /* make sure the single step bit is not set. */
  712. user_disable_single_step(child);
  713. }
  714. /*
  715. * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
  716. * we mark them as obsolete now, they will be removed in a future version
  717. */
  718. static long arch_ptrace_old(struct task_struct *child, long request, long addr,
  719. long data)
  720. {
  721. switch (request) {
  722. case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
  723. return copy_regset_to_user(child, &user_ppc_native_view,
  724. REGSET_GPR, 0, 32 * sizeof(long),
  725. (void __user *) data);
  726. case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
  727. return copy_regset_from_user(child, &user_ppc_native_view,
  728. REGSET_GPR, 0, 32 * sizeof(long),
  729. (const void __user *) data);
  730. case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
  731. return copy_regset_to_user(child, &user_ppc_native_view,
  732. REGSET_FPR, 0, 32 * sizeof(double),
  733. (void __user *) data);
  734. case PPC_PTRACE_SETFPREGS: /* Set FPRs 0 - 31. */
  735. return copy_regset_from_user(child, &user_ppc_native_view,
  736. REGSET_FPR, 0, 32 * sizeof(double),
  737. (const void __user *) data);
  738. }
  739. return -EPERM;
  740. }
  741. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  742. {
  743. int ret = -EPERM;
  744. switch (request) {
  745. /* read the word at location addr in the USER area. */
  746. case PTRACE_PEEKUSR: {
  747. unsigned long index, tmp;
  748. ret = -EIO;
  749. /* convert to index and check */
  750. #ifdef CONFIG_PPC32
  751. index = (unsigned long) addr >> 2;
  752. if ((addr & 3) || (index > PT_FPSCR)
  753. || (child->thread.regs == NULL))
  754. #else
  755. index = (unsigned long) addr >> 3;
  756. if ((addr & 7) || (index > PT_FPSCR))
  757. #endif
  758. break;
  759. CHECK_FULL_REGS(child->thread.regs);
  760. if (index < PT_FPR0) {
  761. tmp = ptrace_get_reg(child, (int) index);
  762. } else {
  763. flush_fp_to_thread(child);
  764. tmp = ((unsigned long *)child->thread.fpr)
  765. [TS_FPRWIDTH * (index - PT_FPR0)];
  766. }
  767. ret = put_user(tmp,(unsigned long __user *) data);
  768. break;
  769. }
  770. /* write the word at location addr in the USER area */
  771. case PTRACE_POKEUSR: {
  772. unsigned long index;
  773. ret = -EIO;
  774. /* convert to index and check */
  775. #ifdef CONFIG_PPC32
  776. index = (unsigned long) addr >> 2;
  777. if ((addr & 3) || (index > PT_FPSCR)
  778. || (child->thread.regs == NULL))
  779. #else
  780. index = (unsigned long) addr >> 3;
  781. if ((addr & 7) || (index > PT_FPSCR))
  782. #endif
  783. break;
  784. CHECK_FULL_REGS(child->thread.regs);
  785. if (index < PT_FPR0) {
  786. ret = ptrace_put_reg(child, index, data);
  787. } else {
  788. flush_fp_to_thread(child);
  789. ((unsigned long *)child->thread.fpr)
  790. [TS_FPRWIDTH * (index - PT_FPR0)] = data;
  791. ret = 0;
  792. }
  793. break;
  794. }
  795. case PTRACE_GET_DEBUGREG: {
  796. ret = -EINVAL;
  797. /* We only support one DABR and no IABRS at the moment */
  798. if (addr > 0)
  799. break;
  800. ret = put_user(child->thread.dabr,
  801. (unsigned long __user *)data);
  802. break;
  803. }
  804. case PTRACE_SET_DEBUGREG:
  805. ret = ptrace_set_debugreg(child, addr, data);
  806. break;
  807. #ifdef CONFIG_PPC64
  808. case PTRACE_GETREGS64:
  809. #endif
  810. case PTRACE_GETREGS: /* Get all pt_regs from the child. */
  811. return copy_regset_to_user(child, &user_ppc_native_view,
  812. REGSET_GPR,
  813. 0, sizeof(struct pt_regs),
  814. (void __user *) data);
  815. #ifdef CONFIG_PPC64
  816. case PTRACE_SETREGS64:
  817. #endif
  818. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  819. return copy_regset_from_user(child, &user_ppc_native_view,
  820. REGSET_GPR,
  821. 0, sizeof(struct pt_regs),
  822. (const void __user *) data);
  823. case PTRACE_GETFPREGS: /* Get the child FPU state (FPR0...31 + FPSCR) */
  824. return copy_regset_to_user(child, &user_ppc_native_view,
  825. REGSET_FPR,
  826. 0, sizeof(elf_fpregset_t),
  827. (void __user *) data);
  828. case PTRACE_SETFPREGS: /* Set the child FPU state (FPR0...31 + FPSCR) */
  829. return copy_regset_from_user(child, &user_ppc_native_view,
  830. REGSET_FPR,
  831. 0, sizeof(elf_fpregset_t),
  832. (const void __user *) data);
  833. #ifdef CONFIG_ALTIVEC
  834. case PTRACE_GETVRREGS:
  835. return copy_regset_to_user(child, &user_ppc_native_view,
  836. REGSET_VMX,
  837. 0, (33 * sizeof(vector128) +
  838. sizeof(u32)),
  839. (void __user *) data);
  840. case PTRACE_SETVRREGS:
  841. return copy_regset_from_user(child, &user_ppc_native_view,
  842. REGSET_VMX,
  843. 0, (33 * sizeof(vector128) +
  844. sizeof(u32)),
  845. (const void __user *) data);
  846. #endif
  847. #ifdef CONFIG_VSX
  848. case PTRACE_GETVSRREGS:
  849. return copy_regset_to_user(child, &user_ppc_native_view,
  850. REGSET_VSX,
  851. 0, 32 * sizeof(double),
  852. (void __user *) data);
  853. case PTRACE_SETVSRREGS:
  854. return copy_regset_from_user(child, &user_ppc_native_view,
  855. REGSET_VSX,
  856. 0, 32 * sizeof(double),
  857. (const void __user *) data);
  858. #endif
  859. #ifdef CONFIG_SPE
  860. case PTRACE_GETEVRREGS:
  861. /* Get the child spe register state. */
  862. return copy_regset_to_user(child, &user_ppc_native_view,
  863. REGSET_SPE, 0, 35 * sizeof(u32),
  864. (void __user *) data);
  865. case PTRACE_SETEVRREGS:
  866. /* Set the child spe register state. */
  867. return copy_regset_from_user(child, &user_ppc_native_view,
  868. REGSET_SPE, 0, 35 * sizeof(u32),
  869. (const void __user *) data);
  870. #endif
  871. /* Old reverse args ptrace callss */
  872. case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
  873. case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
  874. case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
  875. case PPC_PTRACE_SETFPREGS: /* Get FPRs 0 - 31. */
  876. ret = arch_ptrace_old(child, request, addr, data);
  877. break;
  878. default:
  879. ret = ptrace_request(child, request, addr, data);
  880. break;
  881. }
  882. return ret;
  883. }
  884. /*
  885. * We must return the syscall number to actually look up in the table.
  886. * This can be -1L to skip running any syscall at all.
  887. */
  888. long do_syscall_trace_enter(struct pt_regs *regs)
  889. {
  890. long ret = 0;
  891. secure_computing(regs->gpr[0]);
  892. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  893. tracehook_report_syscall_entry(regs))
  894. /*
  895. * Tracing decided this syscall should not happen.
  896. * We'll return a bogus call number to get an ENOSYS
  897. * error, but leave the original number in regs->gpr[0].
  898. */
  899. ret = -1L;
  900. if (unlikely(current->audit_context)) {
  901. #ifdef CONFIG_PPC64
  902. if (!test_thread_flag(TIF_32BIT))
  903. audit_syscall_entry(AUDIT_ARCH_PPC64,
  904. regs->gpr[0],
  905. regs->gpr[3], regs->gpr[4],
  906. regs->gpr[5], regs->gpr[6]);
  907. else
  908. #endif
  909. audit_syscall_entry(AUDIT_ARCH_PPC,
  910. regs->gpr[0],
  911. regs->gpr[3] & 0xffffffff,
  912. regs->gpr[4] & 0xffffffff,
  913. regs->gpr[5] & 0xffffffff,
  914. regs->gpr[6] & 0xffffffff);
  915. }
  916. return ret ?: regs->gpr[0];
  917. }
  918. void do_syscall_trace_leave(struct pt_regs *regs)
  919. {
  920. int step;
  921. if (unlikely(current->audit_context))
  922. audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
  923. regs->result);
  924. step = test_thread_flag(TIF_SINGLESTEP);
  925. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  926. tracehook_report_syscall_exit(regs, step);
  927. }