ptrace.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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 defined(CONFIG_BOOKE)
  629. /* If DAC then do not single step, skip */
  630. if (task->thread.dabr)
  631. return;
  632. #endif
  633. if (regs != NULL) {
  634. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  635. task->thread.dbcr0 &= ~(DBCR0_IC | DBCR0_BT | DBCR0_IDM);
  636. regs->msr &= ~MSR_DE;
  637. #else
  638. regs->msr &= ~(MSR_SE | MSR_BE);
  639. #endif
  640. }
  641. clear_tsk_thread_flag(task, TIF_SINGLESTEP);
  642. }
  643. int ptrace_set_debugreg(struct task_struct *task, unsigned long addr,
  644. unsigned long data)
  645. {
  646. /* For ppc64 we support one DABR and no IABR's at the moment (ppc64).
  647. * For embedded processors we support one DAC and no IAC's at the
  648. * moment.
  649. */
  650. if (addr > 0)
  651. return -EINVAL;
  652. /* The bottom 3 bits in dabr are flags */
  653. if ((data & ~0x7UL) >= TASK_SIZE)
  654. return -EIO;
  655. #ifndef CONFIG_BOOKE
  656. /* For processors using DABR (i.e. 970), the bottom 3 bits are flags.
  657. * It was assumed, on previous implementations, that 3 bits were
  658. * passed together with the data address, fitting the design of the
  659. * DABR register, as follows:
  660. *
  661. * bit 0: Read flag
  662. * bit 1: Write flag
  663. * bit 2: Breakpoint translation
  664. *
  665. * Thus, we use them here as so.
  666. */
  667. /* Ensure breakpoint translation bit is set */
  668. if (data && !(data & DABR_TRANSLATION))
  669. return -EIO;
  670. /* Move contents to the DABR register */
  671. task->thread.dabr = data;
  672. #endif
  673. #if defined(CONFIG_BOOKE)
  674. /* As described above, it was assumed 3 bits were passed with the data
  675. * address, but we will assume only the mode bits will be passed
  676. * as to not cause alignment restrictions for DAC-based processors.
  677. */
  678. /* DAC's hold the whole address without any mode flags */
  679. task->thread.dabr = data & ~0x3UL;
  680. if (task->thread.dabr == 0) {
  681. task->thread.dbcr0 &= ~(DBSR_DAC1R | DBSR_DAC1W | DBCR0_IDM);
  682. task->thread.regs->msr &= ~MSR_DE;
  683. return 0;
  684. }
  685. /* Read or Write bits must be set */
  686. if (!(data & 0x3UL))
  687. return -EINVAL;
  688. /* Set the Internal Debugging flag (IDM bit 1) for the DBCR0
  689. register */
  690. task->thread.dbcr0 = DBCR0_IDM;
  691. /* Check for write and read flags and set DBCR0
  692. accordingly */
  693. if (data & 0x1UL)
  694. task->thread.dbcr0 |= DBSR_DAC1R;
  695. if (data & 0x2UL)
  696. task->thread.dbcr0 |= DBSR_DAC1W;
  697. task->thread.regs->msr |= MSR_DE;
  698. #endif
  699. return 0;
  700. }
  701. /*
  702. * Called by kernel/ptrace.c when detaching..
  703. *
  704. * Make sure single step bits etc are not set.
  705. */
  706. void ptrace_disable(struct task_struct *child)
  707. {
  708. /* make sure the single step bit is not set. */
  709. user_disable_single_step(child);
  710. }
  711. /*
  712. * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
  713. * we mark them as obsolete now, they will be removed in a future version
  714. */
  715. static long arch_ptrace_old(struct task_struct *child, long request, long addr,
  716. long data)
  717. {
  718. switch (request) {
  719. case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
  720. return copy_regset_to_user(child, &user_ppc_native_view,
  721. REGSET_GPR, 0, 32 * sizeof(long),
  722. (void __user *) data);
  723. case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
  724. return copy_regset_from_user(child, &user_ppc_native_view,
  725. REGSET_GPR, 0, 32 * sizeof(long),
  726. (const void __user *) data);
  727. case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
  728. return copy_regset_to_user(child, &user_ppc_native_view,
  729. REGSET_FPR, 0, 32 * sizeof(double),
  730. (void __user *) data);
  731. case PPC_PTRACE_SETFPREGS: /* Set FPRs 0 - 31. */
  732. return copy_regset_from_user(child, &user_ppc_native_view,
  733. REGSET_FPR, 0, 32 * sizeof(double),
  734. (const void __user *) data);
  735. }
  736. return -EPERM;
  737. }
  738. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  739. {
  740. int ret = -EPERM;
  741. switch (request) {
  742. /* read the word at location addr in the USER area. */
  743. case PTRACE_PEEKUSR: {
  744. unsigned long index, tmp;
  745. ret = -EIO;
  746. /* convert to index and check */
  747. #ifdef CONFIG_PPC32
  748. index = (unsigned long) addr >> 2;
  749. if ((addr & 3) || (index > PT_FPSCR)
  750. || (child->thread.regs == NULL))
  751. #else
  752. index = (unsigned long) addr >> 3;
  753. if ((addr & 7) || (index > PT_FPSCR))
  754. #endif
  755. break;
  756. CHECK_FULL_REGS(child->thread.regs);
  757. if (index < PT_FPR0) {
  758. tmp = ptrace_get_reg(child, (int) index);
  759. } else {
  760. flush_fp_to_thread(child);
  761. tmp = ((unsigned long *)child->thread.fpr)
  762. [TS_FPRWIDTH * (index - PT_FPR0)];
  763. }
  764. ret = put_user(tmp,(unsigned long __user *) data);
  765. break;
  766. }
  767. /* write the word at location addr in the USER area */
  768. case PTRACE_POKEUSR: {
  769. unsigned long index;
  770. ret = -EIO;
  771. /* convert to index and check */
  772. #ifdef CONFIG_PPC32
  773. index = (unsigned long) addr >> 2;
  774. if ((addr & 3) || (index > PT_FPSCR)
  775. || (child->thread.regs == NULL))
  776. #else
  777. index = (unsigned long) addr >> 3;
  778. if ((addr & 7) || (index > PT_FPSCR))
  779. #endif
  780. break;
  781. CHECK_FULL_REGS(child->thread.regs);
  782. if (index < PT_FPR0) {
  783. ret = ptrace_put_reg(child, index, data);
  784. } else {
  785. flush_fp_to_thread(child);
  786. ((unsigned long *)child->thread.fpr)
  787. [TS_FPRWIDTH * (index - PT_FPR0)] = data;
  788. ret = 0;
  789. }
  790. break;
  791. }
  792. case PTRACE_GET_DEBUGREG: {
  793. ret = -EINVAL;
  794. /* We only support one DABR and no IABRS at the moment */
  795. if (addr > 0)
  796. break;
  797. ret = put_user(child->thread.dabr,
  798. (unsigned long __user *)data);
  799. break;
  800. }
  801. case PTRACE_SET_DEBUGREG:
  802. ret = ptrace_set_debugreg(child, addr, data);
  803. break;
  804. #ifdef CONFIG_PPC64
  805. case PTRACE_GETREGS64:
  806. #endif
  807. case PTRACE_GETREGS: /* Get all pt_regs from the child. */
  808. return copy_regset_to_user(child, &user_ppc_native_view,
  809. REGSET_GPR,
  810. 0, sizeof(struct pt_regs),
  811. (void __user *) data);
  812. #ifdef CONFIG_PPC64
  813. case PTRACE_SETREGS64:
  814. #endif
  815. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  816. return copy_regset_from_user(child, &user_ppc_native_view,
  817. REGSET_GPR,
  818. 0, sizeof(struct pt_regs),
  819. (const void __user *) data);
  820. case PTRACE_GETFPREGS: /* Get the child FPU state (FPR0...31 + FPSCR) */
  821. return copy_regset_to_user(child, &user_ppc_native_view,
  822. REGSET_FPR,
  823. 0, sizeof(elf_fpregset_t),
  824. (void __user *) data);
  825. case PTRACE_SETFPREGS: /* Set the child FPU state (FPR0...31 + FPSCR) */
  826. return copy_regset_from_user(child, &user_ppc_native_view,
  827. REGSET_FPR,
  828. 0, sizeof(elf_fpregset_t),
  829. (const void __user *) data);
  830. #ifdef CONFIG_ALTIVEC
  831. case PTRACE_GETVRREGS:
  832. return copy_regset_to_user(child, &user_ppc_native_view,
  833. REGSET_VMX,
  834. 0, (33 * sizeof(vector128) +
  835. sizeof(u32)),
  836. (void __user *) data);
  837. case PTRACE_SETVRREGS:
  838. return copy_regset_from_user(child, &user_ppc_native_view,
  839. REGSET_VMX,
  840. 0, (33 * sizeof(vector128) +
  841. sizeof(u32)),
  842. (const void __user *) data);
  843. #endif
  844. #ifdef CONFIG_VSX
  845. case PTRACE_GETVSRREGS:
  846. return copy_regset_to_user(child, &user_ppc_native_view,
  847. REGSET_VSX,
  848. 0, 32 * sizeof(double),
  849. (void __user *) data);
  850. case PTRACE_SETVSRREGS:
  851. return copy_regset_from_user(child, &user_ppc_native_view,
  852. REGSET_VSX,
  853. 0, 32 * sizeof(double),
  854. (const void __user *) data);
  855. #endif
  856. #ifdef CONFIG_SPE
  857. case PTRACE_GETEVRREGS:
  858. /* Get the child spe register state. */
  859. return copy_regset_to_user(child, &user_ppc_native_view,
  860. REGSET_SPE, 0, 35 * sizeof(u32),
  861. (void __user *) data);
  862. case PTRACE_SETEVRREGS:
  863. /* Set the child spe register state. */
  864. return copy_regset_from_user(child, &user_ppc_native_view,
  865. REGSET_SPE, 0, 35 * sizeof(u32),
  866. (const void __user *) data);
  867. #endif
  868. /* Old reverse args ptrace callss */
  869. case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
  870. case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
  871. case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
  872. case PPC_PTRACE_SETFPREGS: /* Get FPRs 0 - 31. */
  873. ret = arch_ptrace_old(child, request, addr, data);
  874. break;
  875. default:
  876. ret = ptrace_request(child, request, addr, data);
  877. break;
  878. }
  879. return ret;
  880. }
  881. /*
  882. * We must return the syscall number to actually look up in the table.
  883. * This can be -1L to skip running any syscall at all.
  884. */
  885. long do_syscall_trace_enter(struct pt_regs *regs)
  886. {
  887. long ret = 0;
  888. secure_computing(regs->gpr[0]);
  889. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  890. tracehook_report_syscall_entry(regs))
  891. /*
  892. * Tracing decided this syscall should not happen.
  893. * We'll return a bogus call number to get an ENOSYS
  894. * error, but leave the original number in regs->gpr[0].
  895. */
  896. ret = -1L;
  897. if (unlikely(current->audit_context)) {
  898. #ifdef CONFIG_PPC64
  899. if (!test_thread_flag(TIF_32BIT))
  900. audit_syscall_entry(AUDIT_ARCH_PPC64,
  901. regs->gpr[0],
  902. regs->gpr[3], regs->gpr[4],
  903. regs->gpr[5], regs->gpr[6]);
  904. else
  905. #endif
  906. audit_syscall_entry(AUDIT_ARCH_PPC,
  907. regs->gpr[0],
  908. regs->gpr[3] & 0xffffffff,
  909. regs->gpr[4] & 0xffffffff,
  910. regs->gpr[5] & 0xffffffff,
  911. regs->gpr[6] & 0xffffffff);
  912. }
  913. return ret ?: regs->gpr[0];
  914. }
  915. void do_syscall_trace_leave(struct pt_regs *regs)
  916. {
  917. int step;
  918. if (unlikely(current->audit_context))
  919. audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
  920. regs->result);
  921. step = test_thread_flag(TIF_SINGLESTEP);
  922. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  923. tracehook_report_syscall_exit(regs, step);
  924. }