ptrace.c 27 KB

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