ptrace.c 44 KB

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