ptrace-adv.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/regset.h>
  3. #include <linux/hw_breakpoint.h>
  4. #include "ptrace-decl.h"
  5. void user_enable_single_step(struct task_struct *task)
  6. {
  7. struct pt_regs *regs = task->thread.regs;
  8. if (regs != NULL) {
  9. task->thread.debug.dbcr0 &= ~DBCR0_BT;
  10. task->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
  11. regs_set_return_msr(regs, regs->msr | MSR_DE);
  12. }
  13. set_tsk_thread_flag(task, TIF_SINGLESTEP);
  14. }
  15. void user_enable_block_step(struct task_struct *task)
  16. {
  17. struct pt_regs *regs = task->thread.regs;
  18. if (regs != NULL) {
  19. task->thread.debug.dbcr0 &= ~DBCR0_IC;
  20. task->thread.debug.dbcr0 = DBCR0_IDM | DBCR0_BT;
  21. regs_set_return_msr(regs, regs->msr | MSR_DE);
  22. }
  23. set_tsk_thread_flag(task, TIF_SINGLESTEP);
  24. }
  25. void user_disable_single_step(struct task_struct *task)
  26. {
  27. struct pt_regs *regs = task->thread.regs;
  28. if (regs != NULL) {
  29. /*
  30. * The logic to disable single stepping should be as
  31. * simple as turning off the Instruction Complete flag.
  32. * And, after doing so, if all debug flags are off, turn
  33. * off DBCR0(IDM) and MSR(DE) .... Torez
  34. */
  35. task->thread.debug.dbcr0 &= ~(DBCR0_IC | DBCR0_BT);
  36. /*
  37. * Test to see if any of the DBCR_ACTIVE_EVENTS bits are set.
  38. */
  39. if (!DBCR_ACTIVE_EVENTS(task->thread.debug.dbcr0,
  40. task->thread.debug.dbcr1)) {
  41. /*
  42. * All debug events were off.....
  43. */
  44. task->thread.debug.dbcr0 &= ~DBCR0_IDM;
  45. regs_set_return_msr(regs, regs->msr & ~MSR_DE);
  46. }
  47. }
  48. clear_tsk_thread_flag(task, TIF_SINGLESTEP);
  49. }
  50. void ppc_gethwdinfo(struct ppc_debug_info *dbginfo)
  51. {
  52. dbginfo->version = 1;
  53. dbginfo->num_instruction_bps = CONFIG_PPC_ADV_DEBUG_IACS;
  54. dbginfo->num_data_bps = CONFIG_PPC_ADV_DEBUG_DACS;
  55. dbginfo->num_condition_regs = CONFIG_PPC_ADV_DEBUG_DVCS;
  56. dbginfo->data_bp_alignment = 4;
  57. dbginfo->sizeof_condition = 4;
  58. dbginfo->features = PPC_DEBUG_FEATURE_INSN_BP_RANGE |
  59. PPC_DEBUG_FEATURE_INSN_BP_MASK;
  60. if (IS_ENABLED(CONFIG_PPC_ADV_DEBUG_DAC_RANGE))
  61. dbginfo->features |= PPC_DEBUG_FEATURE_DATA_BP_RANGE |
  62. PPC_DEBUG_FEATURE_DATA_BP_MASK;
  63. }
  64. int ptrace_get_debugreg(struct task_struct *child, unsigned long addr,
  65. unsigned long __user *datalp)
  66. {
  67. /* We only support one DABR and no IABRS at the moment */
  68. if (addr > 0)
  69. return -EINVAL;
  70. return put_user(child->thread.debug.dac1, datalp);
  71. }
  72. int ptrace_set_debugreg(struct task_struct *task, unsigned long addr, unsigned long data)
  73. {
  74. struct pt_regs *regs = task->thread.regs;
  75. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  76. int ret;
  77. struct thread_struct *thread = &task->thread;
  78. struct perf_event *bp;
  79. struct perf_event_attr attr;
  80. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  81. /* For ppc64 we support one DABR and no IABR's at the moment (ppc64).
  82. * For embedded processors we support one DAC and no IAC's at the
  83. * moment.
  84. */
  85. if (addr > 0)
  86. return -EINVAL;
  87. /* The bottom 3 bits in dabr are flags */
  88. if ((data & ~0x7UL) >= TASK_SIZE)
  89. return -EIO;
  90. /* As described above, it was assumed 3 bits were passed with the data
  91. * address, but we will assume only the mode bits will be passed
  92. * as to not cause alignment restrictions for DAC-based processors.
  93. */
  94. /* DAC's hold the whole address without any mode flags */
  95. task->thread.debug.dac1 = data & ~0x3UL;
  96. if (task->thread.debug.dac1 == 0) {
  97. dbcr_dac(task) &= ~(DBCR_DAC1R | DBCR_DAC1W);
  98. if (!DBCR_ACTIVE_EVENTS(task->thread.debug.dbcr0,
  99. task->thread.debug.dbcr1)) {
  100. regs_set_return_msr(regs, regs->msr & ~MSR_DE);
  101. task->thread.debug.dbcr0 &= ~DBCR0_IDM;
  102. }
  103. return 0;
  104. }
  105. /* Read or Write bits must be set */
  106. if (!(data & 0x3UL))
  107. return -EINVAL;
  108. /* Set the Internal Debugging flag (IDM bit 1) for the DBCR0 register */
  109. task->thread.debug.dbcr0 |= DBCR0_IDM;
  110. /* Check for write and read flags and set DBCR0 accordingly */
  111. dbcr_dac(task) &= ~(DBCR_DAC1R | DBCR_DAC1W);
  112. if (data & 0x1UL)
  113. dbcr_dac(task) |= DBCR_DAC1R;
  114. if (data & 0x2UL)
  115. dbcr_dac(task) |= DBCR_DAC1W;
  116. regs_set_return_msr(regs, regs->msr | MSR_DE);
  117. return 0;
  118. }
  119. static long set_instruction_bp(struct task_struct *child,
  120. struct ppc_hw_breakpoint *bp_info)
  121. {
  122. int slot;
  123. int slot1_in_use = ((child->thread.debug.dbcr0 & DBCR0_IAC1) != 0);
  124. int slot2_in_use = ((child->thread.debug.dbcr0 & DBCR0_IAC2) != 0);
  125. int slot3_in_use = ((child->thread.debug.dbcr0 & DBCR0_IAC3) != 0);
  126. int slot4_in_use = ((child->thread.debug.dbcr0 & DBCR0_IAC4) != 0);
  127. if (dbcr_iac_range(child) & DBCR_IAC12MODE)
  128. slot2_in_use = 1;
  129. if (dbcr_iac_range(child) & DBCR_IAC34MODE)
  130. slot4_in_use = 1;
  131. if (bp_info->addr >= TASK_SIZE)
  132. return -EIO;
  133. if (bp_info->addr_mode != PPC_BREAKPOINT_MODE_EXACT) {
  134. /* Make sure range is valid. */
  135. if (bp_info->addr2 >= TASK_SIZE)
  136. return -EIO;
  137. /* We need a pair of IAC regsisters */
  138. if (!slot1_in_use && !slot2_in_use) {
  139. slot = 1;
  140. child->thread.debug.iac1 = bp_info->addr;
  141. child->thread.debug.iac2 = bp_info->addr2;
  142. child->thread.debug.dbcr0 |= DBCR0_IAC1;
  143. if (bp_info->addr_mode ==
  144. PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
  145. dbcr_iac_range(child) |= DBCR_IAC12X;
  146. else
  147. dbcr_iac_range(child) |= DBCR_IAC12I;
  148. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  149. } else if ((!slot3_in_use) && (!slot4_in_use)) {
  150. slot = 3;
  151. child->thread.debug.iac3 = bp_info->addr;
  152. child->thread.debug.iac4 = bp_info->addr2;
  153. child->thread.debug.dbcr0 |= DBCR0_IAC3;
  154. if (bp_info->addr_mode ==
  155. PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
  156. dbcr_iac_range(child) |= DBCR_IAC34X;
  157. else
  158. dbcr_iac_range(child) |= DBCR_IAC34I;
  159. #endif
  160. } else {
  161. return -ENOSPC;
  162. }
  163. } else {
  164. /* We only need one. If possible leave a pair free in
  165. * case a range is needed later
  166. */
  167. if (!slot1_in_use) {
  168. /*
  169. * Don't use iac1 if iac1-iac2 are free and either
  170. * iac3 or iac4 (but not both) are free
  171. */
  172. if (slot2_in_use || slot3_in_use == slot4_in_use) {
  173. slot = 1;
  174. child->thread.debug.iac1 = bp_info->addr;
  175. child->thread.debug.dbcr0 |= DBCR0_IAC1;
  176. goto out;
  177. }
  178. }
  179. if (!slot2_in_use) {
  180. slot = 2;
  181. child->thread.debug.iac2 = bp_info->addr;
  182. child->thread.debug.dbcr0 |= DBCR0_IAC2;
  183. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  184. } else if (!slot3_in_use) {
  185. slot = 3;
  186. child->thread.debug.iac3 = bp_info->addr;
  187. child->thread.debug.dbcr0 |= DBCR0_IAC3;
  188. } else if (!slot4_in_use) {
  189. slot = 4;
  190. child->thread.debug.iac4 = bp_info->addr;
  191. child->thread.debug.dbcr0 |= DBCR0_IAC4;
  192. #endif
  193. } else {
  194. return -ENOSPC;
  195. }
  196. }
  197. out:
  198. child->thread.debug.dbcr0 |= DBCR0_IDM;
  199. regs_set_return_msr(child->thread.regs, child->thread.regs->msr | MSR_DE);
  200. return slot;
  201. }
  202. static int del_instruction_bp(struct task_struct *child, int slot)
  203. {
  204. switch (slot) {
  205. case 1:
  206. if ((child->thread.debug.dbcr0 & DBCR0_IAC1) == 0)
  207. return -ENOENT;
  208. if (dbcr_iac_range(child) & DBCR_IAC12MODE) {
  209. /* address range - clear slots 1 & 2 */
  210. child->thread.debug.iac2 = 0;
  211. dbcr_iac_range(child) &= ~DBCR_IAC12MODE;
  212. }
  213. child->thread.debug.iac1 = 0;
  214. child->thread.debug.dbcr0 &= ~DBCR0_IAC1;
  215. break;
  216. case 2:
  217. if ((child->thread.debug.dbcr0 & DBCR0_IAC2) == 0)
  218. return -ENOENT;
  219. if (dbcr_iac_range(child) & DBCR_IAC12MODE)
  220. /* used in a range */
  221. return -EINVAL;
  222. child->thread.debug.iac2 = 0;
  223. child->thread.debug.dbcr0 &= ~DBCR0_IAC2;
  224. break;
  225. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  226. case 3:
  227. if ((child->thread.debug.dbcr0 & DBCR0_IAC3) == 0)
  228. return -ENOENT;
  229. if (dbcr_iac_range(child) & DBCR_IAC34MODE) {
  230. /* address range - clear slots 3 & 4 */
  231. child->thread.debug.iac4 = 0;
  232. dbcr_iac_range(child) &= ~DBCR_IAC34MODE;
  233. }
  234. child->thread.debug.iac3 = 0;
  235. child->thread.debug.dbcr0 &= ~DBCR0_IAC3;
  236. break;
  237. case 4:
  238. if ((child->thread.debug.dbcr0 & DBCR0_IAC4) == 0)
  239. return -ENOENT;
  240. if (dbcr_iac_range(child) & DBCR_IAC34MODE)
  241. /* Used in a range */
  242. return -EINVAL;
  243. child->thread.debug.iac4 = 0;
  244. child->thread.debug.dbcr0 &= ~DBCR0_IAC4;
  245. break;
  246. #endif
  247. default:
  248. return -EINVAL;
  249. }
  250. return 0;
  251. }
  252. static int set_dac(struct task_struct *child, struct ppc_hw_breakpoint *bp_info)
  253. {
  254. int byte_enable =
  255. (bp_info->condition_mode >> PPC_BREAKPOINT_CONDITION_BE_SHIFT)
  256. & 0xf;
  257. int condition_mode =
  258. bp_info->condition_mode & PPC_BREAKPOINT_CONDITION_MODE;
  259. int slot;
  260. if (byte_enable && condition_mode == 0)
  261. return -EINVAL;
  262. if (bp_info->addr >= TASK_SIZE)
  263. return -EIO;
  264. if ((dbcr_dac(child) & (DBCR_DAC1R | DBCR_DAC1W)) == 0) {
  265. slot = 1;
  266. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
  267. dbcr_dac(child) |= DBCR_DAC1R;
  268. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
  269. dbcr_dac(child) |= DBCR_DAC1W;
  270. child->thread.debug.dac1 = (unsigned long)bp_info->addr;
  271. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  272. if (byte_enable) {
  273. child->thread.debug.dvc1 =
  274. (unsigned long)bp_info->condition_value;
  275. child->thread.debug.dbcr2 |=
  276. ((byte_enable << DBCR2_DVC1BE_SHIFT) |
  277. (condition_mode << DBCR2_DVC1M_SHIFT));
  278. }
  279. #endif
  280. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  281. } else if (child->thread.debug.dbcr2 & DBCR2_DAC12MODE) {
  282. /* Both dac1 and dac2 are part of a range */
  283. return -ENOSPC;
  284. #endif
  285. } else if ((dbcr_dac(child) & (DBCR_DAC2R | DBCR_DAC2W)) == 0) {
  286. slot = 2;
  287. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
  288. dbcr_dac(child) |= DBCR_DAC2R;
  289. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
  290. dbcr_dac(child) |= DBCR_DAC2W;
  291. child->thread.debug.dac2 = (unsigned long)bp_info->addr;
  292. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  293. if (byte_enable) {
  294. child->thread.debug.dvc2 =
  295. (unsigned long)bp_info->condition_value;
  296. child->thread.debug.dbcr2 |=
  297. ((byte_enable << DBCR2_DVC2BE_SHIFT) |
  298. (condition_mode << DBCR2_DVC2M_SHIFT));
  299. }
  300. #endif
  301. } else {
  302. return -ENOSPC;
  303. }
  304. child->thread.debug.dbcr0 |= DBCR0_IDM;
  305. regs_set_return_msr(child->thread.regs, child->thread.regs->msr | MSR_DE);
  306. return slot + 4;
  307. }
  308. static int del_dac(struct task_struct *child, int slot)
  309. {
  310. if (slot == 1) {
  311. if ((dbcr_dac(child) & (DBCR_DAC1R | DBCR_DAC1W)) == 0)
  312. return -ENOENT;
  313. child->thread.debug.dac1 = 0;
  314. dbcr_dac(child) &= ~(DBCR_DAC1R | DBCR_DAC1W);
  315. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  316. if (child->thread.debug.dbcr2 & DBCR2_DAC12MODE) {
  317. child->thread.debug.dac2 = 0;
  318. child->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
  319. }
  320. child->thread.debug.dbcr2 &= ~(DBCR2_DVC1M | DBCR2_DVC1BE);
  321. #endif
  322. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  323. child->thread.debug.dvc1 = 0;
  324. #endif
  325. } else if (slot == 2) {
  326. if ((dbcr_dac(child) & (DBCR_DAC2R | DBCR_DAC2W)) == 0)
  327. return -ENOENT;
  328. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  329. if (child->thread.debug.dbcr2 & DBCR2_DAC12MODE)
  330. /* Part of a range */
  331. return -EINVAL;
  332. child->thread.debug.dbcr2 &= ~(DBCR2_DVC2M | DBCR2_DVC2BE);
  333. #endif
  334. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  335. child->thread.debug.dvc2 = 0;
  336. #endif
  337. child->thread.debug.dac2 = 0;
  338. dbcr_dac(child) &= ~(DBCR_DAC2R | DBCR_DAC2W);
  339. } else {
  340. return -EINVAL;
  341. }
  342. return 0;
  343. }
  344. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  345. static int set_dac_range(struct task_struct *child,
  346. struct ppc_hw_breakpoint *bp_info)
  347. {
  348. int mode = bp_info->addr_mode & PPC_BREAKPOINT_MODE_MASK;
  349. /* We don't allow range watchpoints to be used with DVC */
  350. if (bp_info->condition_mode)
  351. return -EINVAL;
  352. /*
  353. * Best effort to verify the address range. The user/supervisor bits
  354. * prevent trapping in kernel space, but let's fail on an obvious bad
  355. * range. The simple test on the mask is not fool-proof, and any
  356. * exclusive range will spill over into kernel space.
  357. */
  358. if (bp_info->addr >= TASK_SIZE)
  359. return -EIO;
  360. if (mode == PPC_BREAKPOINT_MODE_MASK) {
  361. /*
  362. * dac2 is a bitmask. Don't allow a mask that makes a
  363. * kernel space address from a valid dac1 value
  364. */
  365. if (~((unsigned long)bp_info->addr2) >= TASK_SIZE)
  366. return -EIO;
  367. } else {
  368. /*
  369. * For range breakpoints, addr2 must also be a valid address
  370. */
  371. if (bp_info->addr2 >= TASK_SIZE)
  372. return -EIO;
  373. }
  374. if (child->thread.debug.dbcr0 &
  375. (DBCR0_DAC1R | DBCR0_DAC1W | DBCR0_DAC2R | DBCR0_DAC2W))
  376. return -ENOSPC;
  377. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
  378. child->thread.debug.dbcr0 |= (DBCR0_DAC1R | DBCR0_IDM);
  379. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
  380. child->thread.debug.dbcr0 |= (DBCR0_DAC1W | DBCR0_IDM);
  381. child->thread.debug.dac1 = bp_info->addr;
  382. child->thread.debug.dac2 = bp_info->addr2;
  383. if (mode == PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE)
  384. child->thread.debug.dbcr2 |= DBCR2_DAC12M;
  385. else if (mode == PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
  386. child->thread.debug.dbcr2 |= DBCR2_DAC12MX;
  387. else /* PPC_BREAKPOINT_MODE_MASK */
  388. child->thread.debug.dbcr2 |= DBCR2_DAC12MM;
  389. regs_set_return_msr(child->thread.regs, child->thread.regs->msr | MSR_DE);
  390. return 5;
  391. }
  392. #endif /* CONFIG_PPC_ADV_DEBUG_DAC_RANGE */
  393. long ppc_set_hwdebug(struct task_struct *child, struct ppc_hw_breakpoint *bp_info)
  394. {
  395. if (bp_info->version != 1)
  396. return -ENOTSUPP;
  397. /*
  398. * Check for invalid flags and combinations
  399. */
  400. if (bp_info->trigger_type == 0 ||
  401. (bp_info->trigger_type & ~(PPC_BREAKPOINT_TRIGGER_EXECUTE |
  402. PPC_BREAKPOINT_TRIGGER_RW)) ||
  403. (bp_info->addr_mode & ~PPC_BREAKPOINT_MODE_MASK) ||
  404. (bp_info->condition_mode &
  405. ~(PPC_BREAKPOINT_CONDITION_MODE |
  406. PPC_BREAKPOINT_CONDITION_BE_ALL)))
  407. return -EINVAL;
  408. #if CONFIG_PPC_ADV_DEBUG_DVCS == 0
  409. if (bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE)
  410. return -EINVAL;
  411. #endif
  412. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_EXECUTE) {
  413. if (bp_info->trigger_type != PPC_BREAKPOINT_TRIGGER_EXECUTE ||
  414. bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE)
  415. return -EINVAL;
  416. return set_instruction_bp(child, bp_info);
  417. }
  418. if (bp_info->addr_mode == PPC_BREAKPOINT_MODE_EXACT)
  419. return set_dac(child, bp_info);
  420. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  421. return set_dac_range(child, bp_info);
  422. #else
  423. return -EINVAL;
  424. #endif
  425. }
  426. long ppc_del_hwdebug(struct task_struct *child, long data)
  427. {
  428. int rc;
  429. if (data <= 4)
  430. rc = del_instruction_bp(child, (int)data);
  431. else
  432. rc = del_dac(child, (int)data - 4);
  433. if (!rc) {
  434. if (!DBCR_ACTIVE_EVENTS(child->thread.debug.dbcr0,
  435. child->thread.debug.dbcr1)) {
  436. child->thread.debug.dbcr0 &= ~DBCR0_IDM;
  437. regs_set_return_msr(child->thread.regs,
  438. child->thread.regs->msr & ~MSR_DE);
  439. }
  440. }
  441. return rc;
  442. }