ptrace.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  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. * The parameter save area on the stack is used to store arguments being passed
  40. * to callee function and is located at fixed offset from stack pointer.
  41. */
  42. #ifdef CONFIG_PPC32
  43. #define PARAMETER_SAVE_AREA_OFFSET 24 /* bytes */
  44. #else /* CONFIG_PPC32 */
  45. #define PARAMETER_SAVE_AREA_OFFSET 48 /* bytes */
  46. #endif
  47. struct pt_regs_offset {
  48. const char *name;
  49. int offset;
  50. };
  51. #define STR(s) #s /* convert to string */
  52. #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
  53. #define GPR_OFFSET_NAME(num) \
  54. {.name = STR(gpr##num), .offset = offsetof(struct pt_regs, gpr[num])}
  55. #define REG_OFFSET_END {.name = NULL, .offset = 0}
  56. static const struct pt_regs_offset regoffset_table[] = {
  57. GPR_OFFSET_NAME(0),
  58. GPR_OFFSET_NAME(1),
  59. GPR_OFFSET_NAME(2),
  60. GPR_OFFSET_NAME(3),
  61. GPR_OFFSET_NAME(4),
  62. GPR_OFFSET_NAME(5),
  63. GPR_OFFSET_NAME(6),
  64. GPR_OFFSET_NAME(7),
  65. GPR_OFFSET_NAME(8),
  66. GPR_OFFSET_NAME(9),
  67. GPR_OFFSET_NAME(10),
  68. GPR_OFFSET_NAME(11),
  69. GPR_OFFSET_NAME(12),
  70. GPR_OFFSET_NAME(13),
  71. GPR_OFFSET_NAME(14),
  72. GPR_OFFSET_NAME(15),
  73. GPR_OFFSET_NAME(16),
  74. GPR_OFFSET_NAME(17),
  75. GPR_OFFSET_NAME(18),
  76. GPR_OFFSET_NAME(19),
  77. GPR_OFFSET_NAME(20),
  78. GPR_OFFSET_NAME(21),
  79. GPR_OFFSET_NAME(22),
  80. GPR_OFFSET_NAME(23),
  81. GPR_OFFSET_NAME(24),
  82. GPR_OFFSET_NAME(25),
  83. GPR_OFFSET_NAME(26),
  84. GPR_OFFSET_NAME(27),
  85. GPR_OFFSET_NAME(28),
  86. GPR_OFFSET_NAME(29),
  87. GPR_OFFSET_NAME(30),
  88. GPR_OFFSET_NAME(31),
  89. REG_OFFSET_NAME(nip),
  90. REG_OFFSET_NAME(msr),
  91. REG_OFFSET_NAME(ctr),
  92. REG_OFFSET_NAME(link),
  93. REG_OFFSET_NAME(xer),
  94. REG_OFFSET_NAME(ccr),
  95. #ifdef CONFIG_PPC64
  96. REG_OFFSET_NAME(softe),
  97. #else
  98. REG_OFFSET_NAME(mq),
  99. #endif
  100. REG_OFFSET_NAME(trap),
  101. REG_OFFSET_NAME(dar),
  102. REG_OFFSET_NAME(dsisr),
  103. REG_OFFSET_END,
  104. };
  105. /**
  106. * regs_query_register_offset() - query register offset from its name
  107. * @name: the name of a register
  108. *
  109. * regs_query_register_offset() returns the offset of a register in struct
  110. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  111. */
  112. int regs_query_register_offset(const char *name)
  113. {
  114. const struct pt_regs_offset *roff;
  115. for (roff = regoffset_table; roff->name != NULL; roff++)
  116. if (!strcmp(roff->name, name))
  117. return roff->offset;
  118. return -EINVAL;
  119. }
  120. /**
  121. * regs_query_register_name() - query register name from its offset
  122. * @offset: the offset of a register in struct pt_regs.
  123. *
  124. * regs_query_register_name() returns the name of a register from its
  125. * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
  126. */
  127. const char *regs_query_register_name(unsigned int offset)
  128. {
  129. const struct pt_regs_offset *roff;
  130. for (roff = regoffset_table; roff->name != NULL; roff++)
  131. if (roff->offset == offset)
  132. return roff->name;
  133. return NULL;
  134. }
  135. /*
  136. * does not yet catch signals sent when the child dies.
  137. * in exit.c or in signal.c.
  138. */
  139. /*
  140. * Set of msr bits that gdb can change on behalf of a process.
  141. */
  142. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  143. #define MSR_DEBUGCHANGE 0
  144. #else
  145. #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
  146. #endif
  147. /*
  148. * Max register writeable via put_reg
  149. */
  150. #ifdef CONFIG_PPC32
  151. #define PT_MAX_PUT_REG PT_MQ
  152. #else
  153. #define PT_MAX_PUT_REG PT_CCR
  154. #endif
  155. static unsigned long get_user_msr(struct task_struct *task)
  156. {
  157. return task->thread.regs->msr | task->thread.fpexc_mode;
  158. }
  159. static int set_user_msr(struct task_struct *task, unsigned long msr)
  160. {
  161. task->thread.regs->msr &= ~MSR_DEBUGCHANGE;
  162. task->thread.regs->msr |= msr & MSR_DEBUGCHANGE;
  163. return 0;
  164. }
  165. /*
  166. * We prevent mucking around with the reserved area of trap
  167. * which are used internally by the kernel.
  168. */
  169. static int set_user_trap(struct task_struct *task, unsigned long trap)
  170. {
  171. task->thread.regs->trap = trap & 0xfff0;
  172. return 0;
  173. }
  174. /*
  175. * Get contents of register REGNO in task TASK.
  176. */
  177. unsigned long ptrace_get_reg(struct task_struct *task, int regno)
  178. {
  179. if (task->thread.regs == NULL)
  180. return -EIO;
  181. if (regno == PT_MSR)
  182. return get_user_msr(task);
  183. if (regno < (sizeof(struct pt_regs) / sizeof(unsigned long)))
  184. return ((unsigned long *)task->thread.regs)[regno];
  185. return -EIO;
  186. }
  187. /*
  188. * Write contents of register REGNO in task TASK.
  189. */
  190. int ptrace_put_reg(struct task_struct *task, int regno, unsigned long data)
  191. {
  192. if (task->thread.regs == NULL)
  193. return -EIO;
  194. if (regno == PT_MSR)
  195. return set_user_msr(task, data);
  196. if (regno == PT_TRAP)
  197. return set_user_trap(task, data);
  198. if (regno <= PT_MAX_PUT_REG) {
  199. ((unsigned long *)task->thread.regs)[regno] = data;
  200. return 0;
  201. }
  202. return -EIO;
  203. }
  204. static int gpr_get(struct task_struct *target, const struct user_regset *regset,
  205. unsigned int pos, unsigned int count,
  206. void *kbuf, void __user *ubuf)
  207. {
  208. int ret;
  209. if (target->thread.regs == NULL)
  210. return -EIO;
  211. CHECK_FULL_REGS(target->thread.regs);
  212. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  213. target->thread.regs,
  214. 0, offsetof(struct pt_regs, msr));
  215. if (!ret) {
  216. unsigned long msr = get_user_msr(target);
  217. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &msr,
  218. offsetof(struct pt_regs, msr),
  219. offsetof(struct pt_regs, msr) +
  220. sizeof(msr));
  221. }
  222. BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
  223. offsetof(struct pt_regs, msr) + sizeof(long));
  224. if (!ret)
  225. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  226. &target->thread.regs->orig_gpr3,
  227. offsetof(struct pt_regs, orig_gpr3),
  228. sizeof(struct pt_regs));
  229. if (!ret)
  230. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  231. sizeof(struct pt_regs), -1);
  232. return ret;
  233. }
  234. static int gpr_set(struct task_struct *target, const struct user_regset *regset,
  235. unsigned int pos, unsigned int count,
  236. const void *kbuf, const void __user *ubuf)
  237. {
  238. unsigned long reg;
  239. int ret;
  240. if (target->thread.regs == NULL)
  241. return -EIO;
  242. CHECK_FULL_REGS(target->thread.regs);
  243. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  244. target->thread.regs,
  245. 0, PT_MSR * sizeof(reg));
  246. if (!ret && count > 0) {
  247. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &reg,
  248. PT_MSR * sizeof(reg),
  249. (PT_MSR + 1) * sizeof(reg));
  250. if (!ret)
  251. ret = set_user_msr(target, reg);
  252. }
  253. BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
  254. offsetof(struct pt_regs, msr) + sizeof(long));
  255. if (!ret)
  256. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  257. &target->thread.regs->orig_gpr3,
  258. PT_ORIG_R3 * sizeof(reg),
  259. (PT_MAX_PUT_REG + 1) * sizeof(reg));
  260. if (PT_MAX_PUT_REG + 1 < PT_TRAP && !ret)
  261. ret = user_regset_copyin_ignore(
  262. &pos, &count, &kbuf, &ubuf,
  263. (PT_MAX_PUT_REG + 1) * sizeof(reg),
  264. PT_TRAP * sizeof(reg));
  265. if (!ret && count > 0) {
  266. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &reg,
  267. PT_TRAP * sizeof(reg),
  268. (PT_TRAP + 1) * sizeof(reg));
  269. if (!ret)
  270. ret = set_user_trap(target, reg);
  271. }
  272. if (!ret)
  273. ret = user_regset_copyin_ignore(
  274. &pos, &count, &kbuf, &ubuf,
  275. (PT_TRAP + 1) * sizeof(reg), -1);
  276. return ret;
  277. }
  278. static int fpr_get(struct task_struct *target, const struct user_regset *regset,
  279. unsigned int pos, unsigned int count,
  280. void *kbuf, void __user *ubuf)
  281. {
  282. #ifdef CONFIG_VSX
  283. double buf[33];
  284. int i;
  285. #endif
  286. flush_fp_to_thread(target);
  287. #ifdef CONFIG_VSX
  288. /* copy to local buffer then write that out */
  289. for (i = 0; i < 32 ; i++)
  290. buf[i] = target->thread.TS_FPR(i);
  291. memcpy(&buf[32], &target->thread.fpscr, sizeof(double));
  292. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, buf, 0, -1);
  293. #else
  294. BUILD_BUG_ON(offsetof(struct thread_struct, fpscr) !=
  295. offsetof(struct thread_struct, TS_FPR(32)));
  296. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  297. &target->thread.fpr, 0, -1);
  298. #endif
  299. }
  300. static int fpr_set(struct task_struct *target, const struct user_regset *regset,
  301. unsigned int pos, unsigned int count,
  302. const void *kbuf, const void __user *ubuf)
  303. {
  304. #ifdef CONFIG_VSX
  305. double buf[33];
  306. int i;
  307. #endif
  308. flush_fp_to_thread(target);
  309. #ifdef CONFIG_VSX
  310. /* copy to local buffer then write that out */
  311. i = user_regset_copyin(&pos, &count, &kbuf, &ubuf, buf, 0, -1);
  312. if (i)
  313. return i;
  314. for (i = 0; i < 32 ; i++)
  315. target->thread.TS_FPR(i) = buf[i];
  316. memcpy(&target->thread.fpscr, &buf[32], sizeof(double));
  317. return 0;
  318. #else
  319. BUILD_BUG_ON(offsetof(struct thread_struct, fpscr) !=
  320. offsetof(struct thread_struct, TS_FPR(32)));
  321. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  322. &target->thread.fpr, 0, -1);
  323. #endif
  324. }
  325. #ifdef CONFIG_ALTIVEC
  326. /*
  327. * Get/set all the altivec registers vr0..vr31, vscr, vrsave, in one go.
  328. * The transfer totals 34 quadword. Quadwords 0-31 contain the
  329. * corresponding vector registers. Quadword 32 contains the vscr as the
  330. * last word (offset 12) within that quadword. Quadword 33 contains the
  331. * vrsave as the first word (offset 0) within the quadword.
  332. *
  333. * This definition of the VMX state is compatible with the current PPC32
  334. * ptrace interface. This allows signal handling and ptrace to use the
  335. * same structures. This also simplifies the implementation of a bi-arch
  336. * (combined (32- and 64-bit) gdb.
  337. */
  338. static int vr_active(struct task_struct *target,
  339. const struct user_regset *regset)
  340. {
  341. flush_altivec_to_thread(target);
  342. return target->thread.used_vr ? regset->n : 0;
  343. }
  344. static int vr_get(struct task_struct *target, const struct user_regset *regset,
  345. unsigned int pos, unsigned int count,
  346. void *kbuf, void __user *ubuf)
  347. {
  348. int ret;
  349. flush_altivec_to_thread(target);
  350. BUILD_BUG_ON(offsetof(struct thread_struct, vscr) !=
  351. offsetof(struct thread_struct, vr[32]));
  352. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  353. &target->thread.vr, 0,
  354. 33 * sizeof(vector128));
  355. if (!ret) {
  356. /*
  357. * Copy out only the low-order word of vrsave.
  358. */
  359. union {
  360. elf_vrreg_t reg;
  361. u32 word;
  362. } vrsave;
  363. memset(&vrsave, 0, sizeof(vrsave));
  364. vrsave.word = target->thread.vrsave;
  365. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &vrsave,
  366. 33 * sizeof(vector128), -1);
  367. }
  368. return ret;
  369. }
  370. static int vr_set(struct task_struct *target, const struct user_regset *regset,
  371. unsigned int pos, unsigned int count,
  372. const void *kbuf, const void __user *ubuf)
  373. {
  374. int ret;
  375. flush_altivec_to_thread(target);
  376. BUILD_BUG_ON(offsetof(struct thread_struct, vscr) !=
  377. offsetof(struct thread_struct, vr[32]));
  378. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  379. &target->thread.vr, 0, 33 * sizeof(vector128));
  380. if (!ret && count > 0) {
  381. /*
  382. * We use only the first word of vrsave.
  383. */
  384. union {
  385. elf_vrreg_t reg;
  386. u32 word;
  387. } vrsave;
  388. memset(&vrsave, 0, sizeof(vrsave));
  389. vrsave.word = target->thread.vrsave;
  390. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &vrsave,
  391. 33 * sizeof(vector128), -1);
  392. if (!ret)
  393. target->thread.vrsave = vrsave.word;
  394. }
  395. return ret;
  396. }
  397. #endif /* CONFIG_ALTIVEC */
  398. #ifdef CONFIG_VSX
  399. /*
  400. * Currently to set and and get all the vsx state, you need to call
  401. * the fp and VMX calls aswell. This only get/sets the lower 32
  402. * 128bit VSX registers.
  403. */
  404. static int vsr_active(struct task_struct *target,
  405. const struct user_regset *regset)
  406. {
  407. flush_vsx_to_thread(target);
  408. return target->thread.used_vsr ? regset->n : 0;
  409. }
  410. static int vsr_get(struct task_struct *target, const struct user_regset *regset,
  411. unsigned int pos, unsigned int count,
  412. void *kbuf, void __user *ubuf)
  413. {
  414. double buf[32];
  415. int ret, i;
  416. flush_vsx_to_thread(target);
  417. for (i = 0; i < 32 ; i++)
  418. buf[i] = target->thread.fpr[i][TS_VSRLOWOFFSET];
  419. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  420. buf, 0, 32 * sizeof(double));
  421. return ret;
  422. }
  423. static int vsr_set(struct task_struct *target, const struct user_regset *regset,
  424. unsigned int pos, unsigned int count,
  425. const void *kbuf, const void __user *ubuf)
  426. {
  427. double buf[32];
  428. int ret,i;
  429. flush_vsx_to_thread(target);
  430. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  431. buf, 0, 32 * sizeof(double));
  432. for (i = 0; i < 32 ; i++)
  433. target->thread.fpr[i][TS_VSRLOWOFFSET] = buf[i];
  434. return ret;
  435. }
  436. #endif /* CONFIG_VSX */
  437. #ifdef CONFIG_SPE
  438. /*
  439. * For get_evrregs/set_evrregs functions 'data' has the following layout:
  440. *
  441. * struct {
  442. * u32 evr[32];
  443. * u64 acc;
  444. * u32 spefscr;
  445. * }
  446. */
  447. static int evr_active(struct task_struct *target,
  448. const struct user_regset *regset)
  449. {
  450. flush_spe_to_thread(target);
  451. return target->thread.used_spe ? regset->n : 0;
  452. }
  453. static int evr_get(struct task_struct *target, const struct user_regset *regset,
  454. unsigned int pos, unsigned int count,
  455. void *kbuf, void __user *ubuf)
  456. {
  457. int ret;
  458. flush_spe_to_thread(target);
  459. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  460. &target->thread.evr,
  461. 0, sizeof(target->thread.evr));
  462. BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
  463. offsetof(struct thread_struct, spefscr));
  464. if (!ret)
  465. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  466. &target->thread.acc,
  467. sizeof(target->thread.evr), -1);
  468. return ret;
  469. }
  470. static int evr_set(struct task_struct *target, const struct user_regset *regset,
  471. unsigned int pos, unsigned int count,
  472. const void *kbuf, const void __user *ubuf)
  473. {
  474. int ret;
  475. flush_spe_to_thread(target);
  476. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  477. &target->thread.evr,
  478. 0, sizeof(target->thread.evr));
  479. BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
  480. offsetof(struct thread_struct, spefscr));
  481. if (!ret)
  482. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  483. &target->thread.acc,
  484. sizeof(target->thread.evr), -1);
  485. return ret;
  486. }
  487. #endif /* CONFIG_SPE */
  488. /*
  489. * These are our native regset flavors.
  490. */
  491. enum powerpc_regset {
  492. REGSET_GPR,
  493. REGSET_FPR,
  494. #ifdef CONFIG_ALTIVEC
  495. REGSET_VMX,
  496. #endif
  497. #ifdef CONFIG_VSX
  498. REGSET_VSX,
  499. #endif
  500. #ifdef CONFIG_SPE
  501. REGSET_SPE,
  502. #endif
  503. };
  504. static const struct user_regset native_regsets[] = {
  505. [REGSET_GPR] = {
  506. .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
  507. .size = sizeof(long), .align = sizeof(long),
  508. .get = gpr_get, .set = gpr_set
  509. },
  510. [REGSET_FPR] = {
  511. .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
  512. .size = sizeof(double), .align = sizeof(double),
  513. .get = fpr_get, .set = fpr_set
  514. },
  515. #ifdef CONFIG_ALTIVEC
  516. [REGSET_VMX] = {
  517. .core_note_type = NT_PPC_VMX, .n = 34,
  518. .size = sizeof(vector128), .align = sizeof(vector128),
  519. .active = vr_active, .get = vr_get, .set = vr_set
  520. },
  521. #endif
  522. #ifdef CONFIG_VSX
  523. [REGSET_VSX] = {
  524. .core_note_type = NT_PPC_VSX, .n = 32,
  525. .size = sizeof(double), .align = sizeof(double),
  526. .active = vsr_active, .get = vsr_get, .set = vsr_set
  527. },
  528. #endif
  529. #ifdef CONFIG_SPE
  530. [REGSET_SPE] = {
  531. .n = 35,
  532. .size = sizeof(u32), .align = sizeof(u32),
  533. .active = evr_active, .get = evr_get, .set = evr_set
  534. },
  535. #endif
  536. };
  537. static const struct user_regset_view user_ppc_native_view = {
  538. .name = UTS_MACHINE, .e_machine = ELF_ARCH, .ei_osabi = ELF_OSABI,
  539. .regsets = native_regsets, .n = ARRAY_SIZE(native_regsets)
  540. };
  541. #ifdef CONFIG_PPC64
  542. #include <linux/compat.h>
  543. static int gpr32_get(struct task_struct *target,
  544. const struct user_regset *regset,
  545. unsigned int pos, unsigned int count,
  546. void *kbuf, void __user *ubuf)
  547. {
  548. const unsigned long *regs = &target->thread.regs->gpr[0];
  549. compat_ulong_t *k = kbuf;
  550. compat_ulong_t __user *u = ubuf;
  551. compat_ulong_t reg;
  552. if (target->thread.regs == NULL)
  553. return -EIO;
  554. CHECK_FULL_REGS(target->thread.regs);
  555. pos /= sizeof(reg);
  556. count /= sizeof(reg);
  557. if (kbuf)
  558. for (; count > 0 && pos < PT_MSR; --count)
  559. *k++ = regs[pos++];
  560. else
  561. for (; count > 0 && pos < PT_MSR; --count)
  562. if (__put_user((compat_ulong_t) regs[pos++], u++))
  563. return -EFAULT;
  564. if (count > 0 && pos == PT_MSR) {
  565. reg = get_user_msr(target);
  566. if (kbuf)
  567. *k++ = reg;
  568. else if (__put_user(reg, u++))
  569. return -EFAULT;
  570. ++pos;
  571. --count;
  572. }
  573. if (kbuf)
  574. for (; count > 0 && pos < PT_REGS_COUNT; --count)
  575. *k++ = regs[pos++];
  576. else
  577. for (; count > 0 && pos < PT_REGS_COUNT; --count)
  578. if (__put_user((compat_ulong_t) regs[pos++], u++))
  579. return -EFAULT;
  580. kbuf = k;
  581. ubuf = u;
  582. pos *= sizeof(reg);
  583. count *= sizeof(reg);
  584. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  585. PT_REGS_COUNT * sizeof(reg), -1);
  586. }
  587. static int gpr32_set(struct task_struct *target,
  588. const struct user_regset *regset,
  589. unsigned int pos, unsigned int count,
  590. const void *kbuf, const void __user *ubuf)
  591. {
  592. unsigned long *regs = &target->thread.regs->gpr[0];
  593. const compat_ulong_t *k = kbuf;
  594. const compat_ulong_t __user *u = ubuf;
  595. compat_ulong_t reg;
  596. if (target->thread.regs == NULL)
  597. return -EIO;
  598. CHECK_FULL_REGS(target->thread.regs);
  599. pos /= sizeof(reg);
  600. count /= sizeof(reg);
  601. if (kbuf)
  602. for (; count > 0 && pos < PT_MSR; --count)
  603. regs[pos++] = *k++;
  604. else
  605. for (; count > 0 && pos < PT_MSR; --count) {
  606. if (__get_user(reg, u++))
  607. return -EFAULT;
  608. regs[pos++] = reg;
  609. }
  610. if (count > 0 && pos == PT_MSR) {
  611. if (kbuf)
  612. reg = *k++;
  613. else if (__get_user(reg, u++))
  614. return -EFAULT;
  615. set_user_msr(target, reg);
  616. ++pos;
  617. --count;
  618. }
  619. if (kbuf) {
  620. for (; count > 0 && pos <= PT_MAX_PUT_REG; --count)
  621. regs[pos++] = *k++;
  622. for (; count > 0 && pos < PT_TRAP; --count, ++pos)
  623. ++k;
  624. } else {
  625. for (; count > 0 && pos <= PT_MAX_PUT_REG; --count) {
  626. if (__get_user(reg, u++))
  627. return -EFAULT;
  628. regs[pos++] = reg;
  629. }
  630. for (; count > 0 && pos < PT_TRAP; --count, ++pos)
  631. if (__get_user(reg, u++))
  632. return -EFAULT;
  633. }
  634. if (count > 0 && pos == PT_TRAP) {
  635. if (kbuf)
  636. reg = *k++;
  637. else if (__get_user(reg, u++))
  638. return -EFAULT;
  639. set_user_trap(target, reg);
  640. ++pos;
  641. --count;
  642. }
  643. kbuf = k;
  644. ubuf = u;
  645. pos *= sizeof(reg);
  646. count *= sizeof(reg);
  647. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  648. (PT_TRAP + 1) * sizeof(reg), -1);
  649. }
  650. /*
  651. * These are the regset flavors matching the CONFIG_PPC32 native set.
  652. */
  653. static const struct user_regset compat_regsets[] = {
  654. [REGSET_GPR] = {
  655. .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
  656. .size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
  657. .get = gpr32_get, .set = gpr32_set
  658. },
  659. [REGSET_FPR] = {
  660. .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
  661. .size = sizeof(double), .align = sizeof(double),
  662. .get = fpr_get, .set = fpr_set
  663. },
  664. #ifdef CONFIG_ALTIVEC
  665. [REGSET_VMX] = {
  666. .core_note_type = NT_PPC_VMX, .n = 34,
  667. .size = sizeof(vector128), .align = sizeof(vector128),
  668. .active = vr_active, .get = vr_get, .set = vr_set
  669. },
  670. #endif
  671. #ifdef CONFIG_SPE
  672. [REGSET_SPE] = {
  673. .core_note_type = NT_PPC_SPE, .n = 35,
  674. .size = sizeof(u32), .align = sizeof(u32),
  675. .active = evr_active, .get = evr_get, .set = evr_set
  676. },
  677. #endif
  678. };
  679. static const struct user_regset_view user_ppc_compat_view = {
  680. .name = "ppc", .e_machine = EM_PPC, .ei_osabi = ELF_OSABI,
  681. .regsets = compat_regsets, .n = ARRAY_SIZE(compat_regsets)
  682. };
  683. #endif /* CONFIG_PPC64 */
  684. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  685. {
  686. #ifdef CONFIG_PPC64
  687. if (test_tsk_thread_flag(task, TIF_32BIT))
  688. return &user_ppc_compat_view;
  689. #endif
  690. return &user_ppc_native_view;
  691. }
  692. void user_enable_single_step(struct task_struct *task)
  693. {
  694. struct pt_regs *regs = task->thread.regs;
  695. if (regs != NULL) {
  696. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  697. task->thread.dbcr0 &= ~DBCR0_BT;
  698. task->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
  699. regs->msr |= MSR_DE;
  700. #else
  701. regs->msr &= ~MSR_BE;
  702. regs->msr |= MSR_SE;
  703. #endif
  704. }
  705. set_tsk_thread_flag(task, TIF_SINGLESTEP);
  706. }
  707. void user_enable_block_step(struct task_struct *task)
  708. {
  709. struct pt_regs *regs = task->thread.regs;
  710. if (regs != NULL) {
  711. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  712. task->thread.dbcr0 &= ~DBCR0_IC;
  713. task->thread.dbcr0 = DBCR0_IDM | DBCR0_BT;
  714. regs->msr |= MSR_DE;
  715. #else
  716. regs->msr &= ~MSR_SE;
  717. regs->msr |= MSR_BE;
  718. #endif
  719. }
  720. set_tsk_thread_flag(task, TIF_SINGLESTEP);
  721. }
  722. void user_disable_single_step(struct task_struct *task)
  723. {
  724. struct pt_regs *regs = task->thread.regs;
  725. if (regs != NULL) {
  726. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  727. /*
  728. * The logic to disable single stepping should be as
  729. * simple as turning off the Instruction Complete flag.
  730. * And, after doing so, if all debug flags are off, turn
  731. * off DBCR0(IDM) and MSR(DE) .... Torez
  732. */
  733. task->thread.dbcr0 &= ~DBCR0_IC;
  734. /*
  735. * Test to see if any of the DBCR_ACTIVE_EVENTS bits are set.
  736. */
  737. if (!DBCR_ACTIVE_EVENTS(task->thread.dbcr0,
  738. task->thread.dbcr1)) {
  739. /*
  740. * All debug events were off.....
  741. */
  742. task->thread.dbcr0 &= ~DBCR0_IDM;
  743. regs->msr &= ~MSR_DE;
  744. }
  745. #else
  746. regs->msr &= ~(MSR_SE | MSR_BE);
  747. #endif
  748. }
  749. clear_tsk_thread_flag(task, TIF_SINGLESTEP);
  750. }
  751. int ptrace_set_debugreg(struct task_struct *task, unsigned long addr,
  752. unsigned long data)
  753. {
  754. /* For ppc64 we support one DABR and no IABR's at the moment (ppc64).
  755. * For embedded processors we support one DAC and no IAC's at the
  756. * moment.
  757. */
  758. if (addr > 0)
  759. return -EINVAL;
  760. /* The bottom 3 bits in dabr are flags */
  761. if ((data & ~0x7UL) >= TASK_SIZE)
  762. return -EIO;
  763. #ifndef CONFIG_PPC_ADV_DEBUG_REGS
  764. /* For processors using DABR (i.e. 970), the bottom 3 bits are flags.
  765. * It was assumed, on previous implementations, that 3 bits were
  766. * passed together with the data address, fitting the design of the
  767. * DABR register, as follows:
  768. *
  769. * bit 0: Read flag
  770. * bit 1: Write flag
  771. * bit 2: Breakpoint translation
  772. *
  773. * Thus, we use them here as so.
  774. */
  775. /* Ensure breakpoint translation bit is set */
  776. if (data && !(data & DABR_TRANSLATION))
  777. return -EIO;
  778. /* Move contents to the DABR register */
  779. task->thread.dabr = data;
  780. #else /* CONFIG_PPC_ADV_DEBUG_REGS */
  781. /* As described above, it was assumed 3 bits were passed with the data
  782. * address, but we will assume only the mode bits will be passed
  783. * as to not cause alignment restrictions for DAC-based processors.
  784. */
  785. /* DAC's hold the whole address without any mode flags */
  786. task->thread.dac1 = data & ~0x3UL;
  787. if (task->thread.dac1 == 0) {
  788. dbcr_dac(task) &= ~(DBCR_DAC1R | DBCR_DAC1W);
  789. if (!DBCR_ACTIVE_EVENTS(task->thread.dbcr0,
  790. task->thread.dbcr1)) {
  791. task->thread.regs->msr &= ~MSR_DE;
  792. task->thread.dbcr0 &= ~DBCR0_IDM;
  793. }
  794. return 0;
  795. }
  796. /* Read or Write bits must be set */
  797. if (!(data & 0x3UL))
  798. return -EINVAL;
  799. /* Set the Internal Debugging flag (IDM bit 1) for the DBCR0
  800. register */
  801. task->thread.dbcr0 |= DBCR0_IDM;
  802. /* Check for write and read flags and set DBCR0
  803. accordingly */
  804. dbcr_dac(task) &= ~(DBCR_DAC1R|DBCR_DAC1W);
  805. if (data & 0x1UL)
  806. dbcr_dac(task) |= DBCR_DAC1R;
  807. if (data & 0x2UL)
  808. dbcr_dac(task) |= DBCR_DAC1W;
  809. task->thread.regs->msr |= MSR_DE;
  810. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  811. return 0;
  812. }
  813. /*
  814. * Called by kernel/ptrace.c when detaching..
  815. *
  816. * Make sure single step bits etc are not set.
  817. */
  818. void ptrace_disable(struct task_struct *child)
  819. {
  820. /* make sure the single step bit is not set. */
  821. user_disable_single_step(child);
  822. }
  823. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  824. static long set_intruction_bp(struct task_struct *child,
  825. struct ppc_hw_breakpoint *bp_info)
  826. {
  827. int slot;
  828. int slot1_in_use = ((child->thread.dbcr0 & DBCR0_IAC1) != 0);
  829. int slot2_in_use = ((child->thread.dbcr0 & DBCR0_IAC2) != 0);
  830. int slot3_in_use = ((child->thread.dbcr0 & DBCR0_IAC3) != 0);
  831. int slot4_in_use = ((child->thread.dbcr0 & DBCR0_IAC4) != 0);
  832. if (dbcr_iac_range(child) & DBCR_IAC12MODE)
  833. slot2_in_use = 1;
  834. if (dbcr_iac_range(child) & DBCR_IAC34MODE)
  835. slot4_in_use = 1;
  836. if (bp_info->addr >= TASK_SIZE)
  837. return -EIO;
  838. if (bp_info->addr_mode != PPC_BREAKPOINT_MODE_EXACT) {
  839. /* Make sure range is valid. */
  840. if (bp_info->addr2 >= TASK_SIZE)
  841. return -EIO;
  842. /* We need a pair of IAC regsisters */
  843. if ((!slot1_in_use) && (!slot2_in_use)) {
  844. slot = 1;
  845. child->thread.iac1 = bp_info->addr;
  846. child->thread.iac2 = bp_info->addr2;
  847. child->thread.dbcr0 |= DBCR0_IAC1;
  848. if (bp_info->addr_mode ==
  849. PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
  850. dbcr_iac_range(child) |= DBCR_IAC12X;
  851. else
  852. dbcr_iac_range(child) |= DBCR_IAC12I;
  853. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  854. } else if ((!slot3_in_use) && (!slot4_in_use)) {
  855. slot = 3;
  856. child->thread.iac3 = bp_info->addr;
  857. child->thread.iac4 = bp_info->addr2;
  858. child->thread.dbcr0 |= DBCR0_IAC3;
  859. if (bp_info->addr_mode ==
  860. PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
  861. dbcr_iac_range(child) |= DBCR_IAC34X;
  862. else
  863. dbcr_iac_range(child) |= DBCR_IAC34I;
  864. #endif
  865. } else
  866. return -ENOSPC;
  867. } else {
  868. /* We only need one. If possible leave a pair free in
  869. * case a range is needed later
  870. */
  871. if (!slot1_in_use) {
  872. /*
  873. * Don't use iac1 if iac1-iac2 are free and either
  874. * iac3 or iac4 (but not both) are free
  875. */
  876. if (slot2_in_use || (slot3_in_use == slot4_in_use)) {
  877. slot = 1;
  878. child->thread.iac1 = bp_info->addr;
  879. child->thread.dbcr0 |= DBCR0_IAC1;
  880. goto out;
  881. }
  882. }
  883. if (!slot2_in_use) {
  884. slot = 2;
  885. child->thread.iac2 = bp_info->addr;
  886. child->thread.dbcr0 |= DBCR0_IAC2;
  887. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  888. } else if (!slot3_in_use) {
  889. slot = 3;
  890. child->thread.iac3 = bp_info->addr;
  891. child->thread.dbcr0 |= DBCR0_IAC3;
  892. } else if (!slot4_in_use) {
  893. slot = 4;
  894. child->thread.iac4 = bp_info->addr;
  895. child->thread.dbcr0 |= DBCR0_IAC4;
  896. #endif
  897. } else
  898. return -ENOSPC;
  899. }
  900. out:
  901. child->thread.dbcr0 |= DBCR0_IDM;
  902. child->thread.regs->msr |= MSR_DE;
  903. return slot;
  904. }
  905. static int del_instruction_bp(struct task_struct *child, int slot)
  906. {
  907. switch (slot) {
  908. case 1:
  909. if ((child->thread.dbcr0 & DBCR0_IAC1) == 0)
  910. return -ENOENT;
  911. if (dbcr_iac_range(child) & DBCR_IAC12MODE) {
  912. /* address range - clear slots 1 & 2 */
  913. child->thread.iac2 = 0;
  914. dbcr_iac_range(child) &= ~DBCR_IAC12MODE;
  915. }
  916. child->thread.iac1 = 0;
  917. child->thread.dbcr0 &= ~DBCR0_IAC1;
  918. break;
  919. case 2:
  920. if ((child->thread.dbcr0 & DBCR0_IAC2) == 0)
  921. return -ENOENT;
  922. if (dbcr_iac_range(child) & DBCR_IAC12MODE)
  923. /* used in a range */
  924. return -EINVAL;
  925. child->thread.iac2 = 0;
  926. child->thread.dbcr0 &= ~DBCR0_IAC2;
  927. break;
  928. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  929. case 3:
  930. if ((child->thread.dbcr0 & DBCR0_IAC3) == 0)
  931. return -ENOENT;
  932. if (dbcr_iac_range(child) & DBCR_IAC34MODE) {
  933. /* address range - clear slots 3 & 4 */
  934. child->thread.iac4 = 0;
  935. dbcr_iac_range(child) &= ~DBCR_IAC34MODE;
  936. }
  937. child->thread.iac3 = 0;
  938. child->thread.dbcr0 &= ~DBCR0_IAC3;
  939. break;
  940. case 4:
  941. if ((child->thread.dbcr0 & DBCR0_IAC4) == 0)
  942. return -ENOENT;
  943. if (dbcr_iac_range(child) & DBCR_IAC34MODE)
  944. /* Used in a range */
  945. return -EINVAL;
  946. child->thread.iac4 = 0;
  947. child->thread.dbcr0 &= ~DBCR0_IAC4;
  948. break;
  949. #endif
  950. default:
  951. return -EINVAL;
  952. }
  953. return 0;
  954. }
  955. static int set_dac(struct task_struct *child, struct ppc_hw_breakpoint *bp_info)
  956. {
  957. int byte_enable =
  958. (bp_info->condition_mode >> PPC_BREAKPOINT_CONDITION_BE_SHIFT)
  959. & 0xf;
  960. int condition_mode =
  961. bp_info->condition_mode & PPC_BREAKPOINT_CONDITION_MODE;
  962. int slot;
  963. if (byte_enable && (condition_mode == 0))
  964. return -EINVAL;
  965. if (bp_info->addr >= TASK_SIZE)
  966. return -EIO;
  967. if ((dbcr_dac(child) & (DBCR_DAC1R | DBCR_DAC1W)) == 0) {
  968. slot = 1;
  969. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
  970. dbcr_dac(child) |= DBCR_DAC1R;
  971. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
  972. dbcr_dac(child) |= DBCR_DAC1W;
  973. child->thread.dac1 = (unsigned long)bp_info->addr;
  974. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  975. if (byte_enable) {
  976. child->thread.dvc1 =
  977. (unsigned long)bp_info->condition_value;
  978. child->thread.dbcr2 |=
  979. ((byte_enable << DBCR2_DVC1BE_SHIFT) |
  980. (condition_mode << DBCR2_DVC1M_SHIFT));
  981. }
  982. #endif
  983. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  984. } else if (child->thread.dbcr2 & DBCR2_DAC12MODE) {
  985. /* Both dac1 and dac2 are part of a range */
  986. return -ENOSPC;
  987. #endif
  988. } else if ((dbcr_dac(child) & (DBCR_DAC2R | DBCR_DAC2W)) == 0) {
  989. slot = 2;
  990. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
  991. dbcr_dac(child) |= DBCR_DAC2R;
  992. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
  993. dbcr_dac(child) |= DBCR_DAC2W;
  994. child->thread.dac2 = (unsigned long)bp_info->addr;
  995. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  996. if (byte_enable) {
  997. child->thread.dvc2 =
  998. (unsigned long)bp_info->condition_value;
  999. child->thread.dbcr2 |=
  1000. ((byte_enable << DBCR2_DVC2BE_SHIFT) |
  1001. (condition_mode << DBCR2_DVC2M_SHIFT));
  1002. }
  1003. #endif
  1004. } else
  1005. return -ENOSPC;
  1006. child->thread.dbcr0 |= DBCR0_IDM;
  1007. child->thread.regs->msr |= MSR_DE;
  1008. return slot + 4;
  1009. }
  1010. static int del_dac(struct task_struct *child, int slot)
  1011. {
  1012. if (slot == 1) {
  1013. if ((dbcr_dac(child) & (DBCR_DAC1R | DBCR_DAC1W)) == 0)
  1014. return -ENOENT;
  1015. child->thread.dac1 = 0;
  1016. dbcr_dac(child) &= ~(DBCR_DAC1R | DBCR_DAC1W);
  1017. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  1018. if (child->thread.dbcr2 & DBCR2_DAC12MODE) {
  1019. child->thread.dac2 = 0;
  1020. child->thread.dbcr2 &= ~DBCR2_DAC12MODE;
  1021. }
  1022. child->thread.dbcr2 &= ~(DBCR2_DVC1M | DBCR2_DVC1BE);
  1023. #endif
  1024. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  1025. child->thread.dvc1 = 0;
  1026. #endif
  1027. } else if (slot == 2) {
  1028. if ((dbcr_dac(child) & (DBCR_DAC2R | DBCR_DAC2W)) == 0)
  1029. return -ENOENT;
  1030. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  1031. if (child->thread.dbcr2 & DBCR2_DAC12MODE)
  1032. /* Part of a range */
  1033. return -EINVAL;
  1034. child->thread.dbcr2 &= ~(DBCR2_DVC2M | DBCR2_DVC2BE);
  1035. #endif
  1036. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  1037. child->thread.dvc2 = 0;
  1038. #endif
  1039. child->thread.dac2 = 0;
  1040. dbcr_dac(child) &= ~(DBCR_DAC2R | DBCR_DAC2W);
  1041. } else
  1042. return -EINVAL;
  1043. return 0;
  1044. }
  1045. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  1046. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  1047. static int set_dac_range(struct task_struct *child,
  1048. struct ppc_hw_breakpoint *bp_info)
  1049. {
  1050. int mode = bp_info->addr_mode & PPC_BREAKPOINT_MODE_MASK;
  1051. /* We don't allow range watchpoints to be used with DVC */
  1052. if (bp_info->condition_mode)
  1053. return -EINVAL;
  1054. /*
  1055. * Best effort to verify the address range. The user/supervisor bits
  1056. * prevent trapping in kernel space, but let's fail on an obvious bad
  1057. * range. The simple test on the mask is not fool-proof, and any
  1058. * exclusive range will spill over into kernel space.
  1059. */
  1060. if (bp_info->addr >= TASK_SIZE)
  1061. return -EIO;
  1062. if (mode == PPC_BREAKPOINT_MODE_MASK) {
  1063. /*
  1064. * dac2 is a bitmask. Don't allow a mask that makes a
  1065. * kernel space address from a valid dac1 value
  1066. */
  1067. if (~((unsigned long)bp_info->addr2) >= TASK_SIZE)
  1068. return -EIO;
  1069. } else {
  1070. /*
  1071. * For range breakpoints, addr2 must also be a valid address
  1072. */
  1073. if (bp_info->addr2 >= TASK_SIZE)
  1074. return -EIO;
  1075. }
  1076. if (child->thread.dbcr0 &
  1077. (DBCR0_DAC1R | DBCR0_DAC1W | DBCR0_DAC2R | DBCR0_DAC2W))
  1078. return -ENOSPC;
  1079. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
  1080. child->thread.dbcr0 |= (DBCR0_DAC1R | DBCR0_IDM);
  1081. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
  1082. child->thread.dbcr0 |= (DBCR0_DAC1W | DBCR0_IDM);
  1083. child->thread.dac1 = bp_info->addr;
  1084. child->thread.dac2 = bp_info->addr2;
  1085. if (mode == PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE)
  1086. child->thread.dbcr2 |= DBCR2_DAC12M;
  1087. else if (mode == PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
  1088. child->thread.dbcr2 |= DBCR2_DAC12MX;
  1089. else /* PPC_BREAKPOINT_MODE_MASK */
  1090. child->thread.dbcr2 |= DBCR2_DAC12MM;
  1091. child->thread.regs->msr |= MSR_DE;
  1092. return 5;
  1093. }
  1094. #endif /* CONFIG_PPC_ADV_DEBUG_DAC_RANGE */
  1095. static long ppc_set_hwdebug(struct task_struct *child,
  1096. struct ppc_hw_breakpoint *bp_info)
  1097. {
  1098. if (bp_info->version != 1)
  1099. return -ENOTSUPP;
  1100. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1101. /*
  1102. * Check for invalid flags and combinations
  1103. */
  1104. if ((bp_info->trigger_type == 0) ||
  1105. (bp_info->trigger_type & ~(PPC_BREAKPOINT_TRIGGER_EXECUTE |
  1106. PPC_BREAKPOINT_TRIGGER_RW)) ||
  1107. (bp_info->addr_mode & ~PPC_BREAKPOINT_MODE_MASK) ||
  1108. (bp_info->condition_mode &
  1109. ~(PPC_BREAKPOINT_CONDITION_MODE |
  1110. PPC_BREAKPOINT_CONDITION_BE_ALL)))
  1111. return -EINVAL;
  1112. #if CONFIG_PPC_ADV_DEBUG_DVCS == 0
  1113. if (bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE)
  1114. return -EINVAL;
  1115. #endif
  1116. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_EXECUTE) {
  1117. if ((bp_info->trigger_type != PPC_BREAKPOINT_TRIGGER_EXECUTE) ||
  1118. (bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE))
  1119. return -EINVAL;
  1120. return set_intruction_bp(child, bp_info);
  1121. }
  1122. if (bp_info->addr_mode == PPC_BREAKPOINT_MODE_EXACT)
  1123. return set_dac(child, bp_info);
  1124. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  1125. return set_dac_range(child, bp_info);
  1126. #else
  1127. return -EINVAL;
  1128. #endif
  1129. #else /* !CONFIG_PPC_ADV_DEBUG_DVCS */
  1130. /*
  1131. * We only support one data breakpoint
  1132. */
  1133. if (((bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_RW) == 0) ||
  1134. ((bp_info->trigger_type & ~PPC_BREAKPOINT_TRIGGER_RW) != 0) ||
  1135. (bp_info->trigger_type != PPC_BREAKPOINT_TRIGGER_WRITE) ||
  1136. (bp_info->addr_mode != PPC_BREAKPOINT_MODE_EXACT) ||
  1137. (bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE))
  1138. return -EINVAL;
  1139. if (child->thread.dabr)
  1140. return -ENOSPC;
  1141. if ((unsigned long)bp_info->addr >= TASK_SIZE)
  1142. return -EIO;
  1143. child->thread.dabr = (unsigned long)bp_info->addr;
  1144. return 1;
  1145. #endif /* !CONFIG_PPC_ADV_DEBUG_DVCS */
  1146. }
  1147. static long ppc_del_hwdebug(struct task_struct *child, long addr, long data)
  1148. {
  1149. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1150. int rc;
  1151. if (data <= 4)
  1152. rc = del_instruction_bp(child, (int)data);
  1153. else
  1154. rc = del_dac(child, (int)data - 4);
  1155. if (!rc) {
  1156. if (!DBCR_ACTIVE_EVENTS(child->thread.dbcr0,
  1157. child->thread.dbcr1)) {
  1158. child->thread.dbcr0 &= ~DBCR0_IDM;
  1159. child->thread.regs->msr &= ~MSR_DE;
  1160. }
  1161. }
  1162. return rc;
  1163. #else
  1164. if (data != 1)
  1165. return -EINVAL;
  1166. if (child->thread.dabr == 0)
  1167. return -ENOENT;
  1168. child->thread.dabr = 0;
  1169. return 0;
  1170. #endif
  1171. }
  1172. /*
  1173. * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
  1174. * we mark them as obsolete now, they will be removed in a future version
  1175. */
  1176. static long arch_ptrace_old(struct task_struct *child, long request, long addr,
  1177. long data)
  1178. {
  1179. switch (request) {
  1180. case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
  1181. return copy_regset_to_user(child, &user_ppc_native_view,
  1182. REGSET_GPR, 0, 32 * sizeof(long),
  1183. (void __user *) data);
  1184. case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
  1185. return copy_regset_from_user(child, &user_ppc_native_view,
  1186. REGSET_GPR, 0, 32 * sizeof(long),
  1187. (const void __user *) data);
  1188. case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
  1189. return copy_regset_to_user(child, &user_ppc_native_view,
  1190. REGSET_FPR, 0, 32 * sizeof(double),
  1191. (void __user *) data);
  1192. case PPC_PTRACE_SETFPREGS: /* Set FPRs 0 - 31. */
  1193. return copy_regset_from_user(child, &user_ppc_native_view,
  1194. REGSET_FPR, 0, 32 * sizeof(double),
  1195. (const void __user *) data);
  1196. }
  1197. return -EPERM;
  1198. }
  1199. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  1200. {
  1201. int ret = -EPERM;
  1202. switch (request) {
  1203. /* read the word at location addr in the USER area. */
  1204. case PTRACE_PEEKUSR: {
  1205. unsigned long index, tmp;
  1206. ret = -EIO;
  1207. /* convert to index and check */
  1208. #ifdef CONFIG_PPC32
  1209. index = (unsigned long) addr >> 2;
  1210. if ((addr & 3) || (index > PT_FPSCR)
  1211. || (child->thread.regs == NULL))
  1212. #else
  1213. index = (unsigned long) addr >> 3;
  1214. if ((addr & 7) || (index > PT_FPSCR))
  1215. #endif
  1216. break;
  1217. CHECK_FULL_REGS(child->thread.regs);
  1218. if (index < PT_FPR0) {
  1219. tmp = ptrace_get_reg(child, (int) index);
  1220. } else {
  1221. flush_fp_to_thread(child);
  1222. tmp = ((unsigned long *)child->thread.fpr)
  1223. [TS_FPRWIDTH * (index - PT_FPR0)];
  1224. }
  1225. ret = put_user(tmp,(unsigned long __user *) data);
  1226. break;
  1227. }
  1228. /* write the word at location addr in the USER area */
  1229. case PTRACE_POKEUSR: {
  1230. unsigned long index;
  1231. ret = -EIO;
  1232. /* convert to index and check */
  1233. #ifdef CONFIG_PPC32
  1234. index = (unsigned long) addr >> 2;
  1235. if ((addr & 3) || (index > PT_FPSCR)
  1236. || (child->thread.regs == NULL))
  1237. #else
  1238. index = (unsigned long) addr >> 3;
  1239. if ((addr & 7) || (index > PT_FPSCR))
  1240. #endif
  1241. break;
  1242. CHECK_FULL_REGS(child->thread.regs);
  1243. if (index < PT_FPR0) {
  1244. ret = ptrace_put_reg(child, index, data);
  1245. } else {
  1246. flush_fp_to_thread(child);
  1247. ((unsigned long *)child->thread.fpr)
  1248. [TS_FPRWIDTH * (index - PT_FPR0)] = data;
  1249. ret = 0;
  1250. }
  1251. break;
  1252. }
  1253. case PPC_PTRACE_GETHWDBGINFO: {
  1254. struct ppc_debug_info dbginfo;
  1255. dbginfo.version = 1;
  1256. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1257. dbginfo.num_instruction_bps = CONFIG_PPC_ADV_DEBUG_IACS;
  1258. dbginfo.num_data_bps = CONFIG_PPC_ADV_DEBUG_DACS;
  1259. dbginfo.num_condition_regs = CONFIG_PPC_ADV_DEBUG_DVCS;
  1260. dbginfo.data_bp_alignment = 4;
  1261. dbginfo.sizeof_condition = 4;
  1262. dbginfo.features = PPC_DEBUG_FEATURE_INSN_BP_RANGE |
  1263. PPC_DEBUG_FEATURE_INSN_BP_MASK;
  1264. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  1265. dbginfo.features |=
  1266. PPC_DEBUG_FEATURE_DATA_BP_RANGE |
  1267. PPC_DEBUG_FEATURE_DATA_BP_MASK;
  1268. #endif
  1269. #else /* !CONFIG_PPC_ADV_DEBUG_REGS */
  1270. dbginfo.num_instruction_bps = 0;
  1271. dbginfo.num_data_bps = 1;
  1272. dbginfo.num_condition_regs = 0;
  1273. #ifdef CONFIG_PPC64
  1274. dbginfo.data_bp_alignment = 8;
  1275. #else
  1276. dbginfo.data_bp_alignment = 4;
  1277. #endif
  1278. dbginfo.sizeof_condition = 0;
  1279. dbginfo.features = 0;
  1280. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  1281. if (!access_ok(VERIFY_WRITE, data,
  1282. sizeof(struct ppc_debug_info)))
  1283. return -EFAULT;
  1284. ret = __copy_to_user((struct ppc_debug_info __user *)data,
  1285. &dbginfo, sizeof(struct ppc_debug_info)) ?
  1286. -EFAULT : 0;
  1287. break;
  1288. }
  1289. case PPC_PTRACE_SETHWDEBUG: {
  1290. struct ppc_hw_breakpoint bp_info;
  1291. if (!access_ok(VERIFY_READ, data,
  1292. sizeof(struct ppc_hw_breakpoint)))
  1293. return -EFAULT;
  1294. ret = __copy_from_user(&bp_info,
  1295. (struct ppc_hw_breakpoint __user *)data,
  1296. sizeof(struct ppc_hw_breakpoint)) ?
  1297. -EFAULT : 0;
  1298. if (!ret)
  1299. ret = ppc_set_hwdebug(child, &bp_info);
  1300. break;
  1301. }
  1302. case PPC_PTRACE_DELHWDEBUG: {
  1303. ret = ppc_del_hwdebug(child, addr, data);
  1304. break;
  1305. }
  1306. case PTRACE_GET_DEBUGREG: {
  1307. ret = -EINVAL;
  1308. /* We only support one DABR and no IABRS at the moment */
  1309. if (addr > 0)
  1310. break;
  1311. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1312. ret = put_user(child->thread.dac1,
  1313. (unsigned long __user *)data);
  1314. #else
  1315. ret = put_user(child->thread.dabr,
  1316. (unsigned long __user *)data);
  1317. #endif
  1318. break;
  1319. }
  1320. case PTRACE_SET_DEBUGREG:
  1321. ret = ptrace_set_debugreg(child, addr, data);
  1322. break;
  1323. #ifdef CONFIG_PPC64
  1324. case PTRACE_GETREGS64:
  1325. #endif
  1326. case PTRACE_GETREGS: /* Get all pt_regs from the child. */
  1327. return copy_regset_to_user(child, &user_ppc_native_view,
  1328. REGSET_GPR,
  1329. 0, sizeof(struct pt_regs),
  1330. (void __user *) data);
  1331. #ifdef CONFIG_PPC64
  1332. case PTRACE_SETREGS64:
  1333. #endif
  1334. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1335. return copy_regset_from_user(child, &user_ppc_native_view,
  1336. REGSET_GPR,
  1337. 0, sizeof(struct pt_regs),
  1338. (const void __user *) data);
  1339. case PTRACE_GETFPREGS: /* Get the child FPU state (FPR0...31 + FPSCR) */
  1340. return copy_regset_to_user(child, &user_ppc_native_view,
  1341. REGSET_FPR,
  1342. 0, sizeof(elf_fpregset_t),
  1343. (void __user *) data);
  1344. case PTRACE_SETFPREGS: /* Set the child FPU state (FPR0...31 + FPSCR) */
  1345. return copy_regset_from_user(child, &user_ppc_native_view,
  1346. REGSET_FPR,
  1347. 0, sizeof(elf_fpregset_t),
  1348. (const void __user *) data);
  1349. #ifdef CONFIG_ALTIVEC
  1350. case PTRACE_GETVRREGS:
  1351. return copy_regset_to_user(child, &user_ppc_native_view,
  1352. REGSET_VMX,
  1353. 0, (33 * sizeof(vector128) +
  1354. sizeof(u32)),
  1355. (void __user *) data);
  1356. case PTRACE_SETVRREGS:
  1357. return copy_regset_from_user(child, &user_ppc_native_view,
  1358. REGSET_VMX,
  1359. 0, (33 * sizeof(vector128) +
  1360. sizeof(u32)),
  1361. (const void __user *) data);
  1362. #endif
  1363. #ifdef CONFIG_VSX
  1364. case PTRACE_GETVSRREGS:
  1365. return copy_regset_to_user(child, &user_ppc_native_view,
  1366. REGSET_VSX,
  1367. 0, 32 * sizeof(double),
  1368. (void __user *) data);
  1369. case PTRACE_SETVSRREGS:
  1370. return copy_regset_from_user(child, &user_ppc_native_view,
  1371. REGSET_VSX,
  1372. 0, 32 * sizeof(double),
  1373. (const void __user *) data);
  1374. #endif
  1375. #ifdef CONFIG_SPE
  1376. case PTRACE_GETEVRREGS:
  1377. /* Get the child spe register state. */
  1378. return copy_regset_to_user(child, &user_ppc_native_view,
  1379. REGSET_SPE, 0, 35 * sizeof(u32),
  1380. (void __user *) data);
  1381. case PTRACE_SETEVRREGS:
  1382. /* Set the child spe register state. */
  1383. return copy_regset_from_user(child, &user_ppc_native_view,
  1384. REGSET_SPE, 0, 35 * sizeof(u32),
  1385. (const void __user *) data);
  1386. #endif
  1387. /* Old reverse args ptrace callss */
  1388. case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
  1389. case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
  1390. case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
  1391. case PPC_PTRACE_SETFPREGS: /* Get FPRs 0 - 31. */
  1392. ret = arch_ptrace_old(child, request, addr, data);
  1393. break;
  1394. default:
  1395. ret = ptrace_request(child, request, addr, data);
  1396. break;
  1397. }
  1398. return ret;
  1399. }
  1400. /*
  1401. * We must return the syscall number to actually look up in the table.
  1402. * This can be -1L to skip running any syscall at all.
  1403. */
  1404. long do_syscall_trace_enter(struct pt_regs *regs)
  1405. {
  1406. long ret = 0;
  1407. secure_computing(regs->gpr[0]);
  1408. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  1409. tracehook_report_syscall_entry(regs))
  1410. /*
  1411. * Tracing decided this syscall should not happen.
  1412. * We'll return a bogus call number to get an ENOSYS
  1413. * error, but leave the original number in regs->gpr[0].
  1414. */
  1415. ret = -1L;
  1416. if (unlikely(current->audit_context)) {
  1417. #ifdef CONFIG_PPC64
  1418. if (!test_thread_flag(TIF_32BIT))
  1419. audit_syscall_entry(AUDIT_ARCH_PPC64,
  1420. regs->gpr[0],
  1421. regs->gpr[3], regs->gpr[4],
  1422. regs->gpr[5], regs->gpr[6]);
  1423. else
  1424. #endif
  1425. audit_syscall_entry(AUDIT_ARCH_PPC,
  1426. regs->gpr[0],
  1427. regs->gpr[3] & 0xffffffff,
  1428. regs->gpr[4] & 0xffffffff,
  1429. regs->gpr[5] & 0xffffffff,
  1430. regs->gpr[6] & 0xffffffff);
  1431. }
  1432. return ret ?: regs->gpr[0];
  1433. }
  1434. void do_syscall_trace_leave(struct pt_regs *regs)
  1435. {
  1436. int step;
  1437. if (unlikely(current->audit_context))
  1438. audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
  1439. regs->result);
  1440. step = test_thread_flag(TIF_SINGLESTEP);
  1441. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  1442. tracehook_report_syscall_exit(regs, step);
  1443. }