hw_breakpoint.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. *
  16. * Copyright (C) 2007 Alan Stern
  17. * Copyright (C) 2009 IBM Corporation
  18. */
  19. /*
  20. * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
  21. * using the CPU's debug registers.
  22. */
  23. #include <linux/irqflags.h>
  24. #include <linux/notifier.h>
  25. #include <linux/kallsyms.h>
  26. #include <linux/kprobes.h>
  27. #include <linux/percpu.h>
  28. #include <linux/kdebug.h>
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/sched.h>
  32. #include <linux/init.h>
  33. #include <linux/smp.h>
  34. #include <asm/hw_breakpoint.h>
  35. #include <asm/processor.h>
  36. #include <asm/debugreg.h>
  37. /* Unmasked kernel DR7 value */
  38. static unsigned long kdr7;
  39. /*
  40. * Masks for the bits corresponding to registers DR0 - DR3 in DR7 register.
  41. * Used to clear and verify the status of bits corresponding to DR0 - DR3
  42. */
  43. static const unsigned long dr7_masks[HBP_NUM] = {
  44. 0x000f0003, /* LEN0, R/W0, G0, L0 */
  45. 0x00f0000c, /* LEN1, R/W1, G1, L1 */
  46. 0x0f000030, /* LEN2, R/W2, G2, L2 */
  47. 0xf00000c0 /* LEN3, R/W3, G3, L3 */
  48. };
  49. /*
  50. * Encode the length, type, Exact, and Enable bits for a particular breakpoint
  51. * as stored in debug register 7.
  52. */
  53. static unsigned long encode_dr7(int drnum, unsigned int len, unsigned int type)
  54. {
  55. unsigned long bp_info;
  56. bp_info = (len | type) & 0xf;
  57. bp_info <<= (DR_CONTROL_SHIFT + drnum * DR_CONTROL_SIZE);
  58. bp_info |= (DR_GLOBAL_ENABLE << (drnum * DR_ENABLE_SIZE)) |
  59. DR_GLOBAL_SLOWDOWN;
  60. return bp_info;
  61. }
  62. void arch_update_kernel_hw_breakpoint(void *unused)
  63. {
  64. struct hw_breakpoint *bp;
  65. int i, cpu = get_cpu();
  66. unsigned long temp_kdr7 = 0;
  67. /* Don't allow debug exceptions while we update the registers */
  68. set_debugreg(0UL, 7);
  69. for (i = hbp_kernel_pos; i < HBP_NUM; i++) {
  70. per_cpu(this_hbp_kernel[i], cpu) = bp = hbp_kernel[i];
  71. if (bp) {
  72. temp_kdr7 |= encode_dr7(i, bp->info.len, bp->info.type);
  73. set_debugreg(bp->info.address, i);
  74. }
  75. }
  76. /* No need to set DR6. Update the debug registers with kernel-space
  77. * breakpoint values from kdr7 and user-space requests from the
  78. * current process
  79. */
  80. kdr7 = temp_kdr7;
  81. set_debugreg(kdr7 | current->thread.debugreg7, 7);
  82. put_cpu_no_resched();
  83. }
  84. /*
  85. * Install the thread breakpoints in their debug registers.
  86. */
  87. void arch_install_thread_hw_breakpoint(struct task_struct *tsk)
  88. {
  89. struct thread_struct *thread = &(tsk->thread);
  90. switch (hbp_kernel_pos) {
  91. case 4:
  92. set_debugreg(thread->debugreg[3], 3);
  93. case 3:
  94. set_debugreg(thread->debugreg[2], 2);
  95. case 2:
  96. set_debugreg(thread->debugreg[1], 1);
  97. case 1:
  98. set_debugreg(thread->debugreg[0], 0);
  99. default:
  100. break;
  101. }
  102. /* No need to set DR6 */
  103. set_debugreg((kdr7 | thread->debugreg7), 7);
  104. }
  105. /*
  106. * Install the debug register values for just the kernel, no thread.
  107. */
  108. void arch_uninstall_thread_hw_breakpoint()
  109. {
  110. /* Clear the user-space portion of debugreg7 by setting only kdr7 */
  111. set_debugreg(kdr7, 7);
  112. }
  113. static int get_hbp_len(u8 hbp_len)
  114. {
  115. unsigned int len_in_bytes = 0;
  116. switch (hbp_len) {
  117. case HW_BREAKPOINT_LEN_1:
  118. len_in_bytes = 1;
  119. break;
  120. case HW_BREAKPOINT_LEN_2:
  121. len_in_bytes = 2;
  122. break;
  123. case HW_BREAKPOINT_LEN_4:
  124. len_in_bytes = 4;
  125. break;
  126. #ifdef CONFIG_X86_64
  127. case HW_BREAKPOINT_LEN_8:
  128. len_in_bytes = 8;
  129. break;
  130. #endif
  131. }
  132. return len_in_bytes;
  133. }
  134. /*
  135. * Check for virtual address in user space.
  136. */
  137. int arch_check_va_in_userspace(unsigned long va, u8 hbp_len)
  138. {
  139. unsigned int len;
  140. len = get_hbp_len(hbp_len);
  141. return (va <= TASK_SIZE - len);
  142. }
  143. /*
  144. * Check for virtual address in kernel space.
  145. */
  146. int arch_check_va_in_kernelspace(unsigned long va, u8 hbp_len)
  147. {
  148. unsigned int len;
  149. len = get_hbp_len(hbp_len);
  150. return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
  151. }
  152. /*
  153. * Store a breakpoint's encoded address, length, and type.
  154. */
  155. static int arch_store_info(struct hw_breakpoint *bp, struct task_struct *tsk)
  156. {
  157. /*
  158. * User-space requests will always have the address field populated
  159. * Symbol names from user-space are rejected
  160. */
  161. if (tsk && bp->info.name)
  162. return -EINVAL;
  163. /*
  164. * For kernel-addresses, either the address or symbol name can be
  165. * specified.
  166. */
  167. if (bp->info.name)
  168. bp->info.address = (unsigned long)
  169. kallsyms_lookup_name(bp->info.name);
  170. if (bp->info.address)
  171. return 0;
  172. return -EINVAL;
  173. }
  174. /*
  175. * Validate the arch-specific HW Breakpoint register settings
  176. */
  177. int arch_validate_hwbkpt_settings(struct hw_breakpoint *bp,
  178. struct task_struct *tsk)
  179. {
  180. unsigned int align;
  181. int ret = -EINVAL;
  182. switch (bp->info.type) {
  183. /*
  184. * Ptrace-refactoring code
  185. * For now, we'll allow instruction breakpoint only for user-space
  186. * addresses
  187. */
  188. case HW_BREAKPOINT_EXECUTE:
  189. if ((!arch_check_va_in_userspace(bp->info.address,
  190. bp->info.len)) &&
  191. bp->info.len != HW_BREAKPOINT_LEN_EXECUTE)
  192. return ret;
  193. break;
  194. case HW_BREAKPOINT_WRITE:
  195. break;
  196. case HW_BREAKPOINT_RW:
  197. break;
  198. default:
  199. return ret;
  200. }
  201. switch (bp->info.len) {
  202. case HW_BREAKPOINT_LEN_1:
  203. align = 0;
  204. break;
  205. case HW_BREAKPOINT_LEN_2:
  206. align = 1;
  207. break;
  208. case HW_BREAKPOINT_LEN_4:
  209. align = 3;
  210. break;
  211. #ifdef CONFIG_X86_64
  212. case HW_BREAKPOINT_LEN_8:
  213. align = 7;
  214. break;
  215. #endif
  216. default:
  217. return ret;
  218. }
  219. if (bp->triggered)
  220. ret = arch_store_info(bp, tsk);
  221. if (ret < 0)
  222. return ret;
  223. /*
  224. * Check that the low-order bits of the address are appropriate
  225. * for the alignment implied by len.
  226. */
  227. if (bp->info.address & align)
  228. return -EINVAL;
  229. /* Check that the virtual address is in the proper range */
  230. if (tsk) {
  231. if (!arch_check_va_in_userspace(bp->info.address, bp->info.len))
  232. return -EFAULT;
  233. } else {
  234. if (!arch_check_va_in_kernelspace(bp->info.address,
  235. bp->info.len))
  236. return -EFAULT;
  237. }
  238. return 0;
  239. }
  240. void arch_update_user_hw_breakpoint(int pos, struct task_struct *tsk)
  241. {
  242. struct thread_struct *thread = &(tsk->thread);
  243. struct hw_breakpoint *bp = thread->hbp[pos];
  244. thread->debugreg7 &= ~dr7_masks[pos];
  245. if (bp) {
  246. thread->debugreg[pos] = bp->info.address;
  247. thread->debugreg7 |= encode_dr7(pos, bp->info.len,
  248. bp->info.type);
  249. } else
  250. thread->debugreg[pos] = 0;
  251. }
  252. void arch_flush_thread_hw_breakpoint(struct task_struct *tsk)
  253. {
  254. int i;
  255. struct thread_struct *thread = &(tsk->thread);
  256. thread->debugreg7 = 0;
  257. for (i = 0; i < HBP_NUM; i++)
  258. thread->debugreg[i] = 0;
  259. }
  260. /*
  261. * Handle debug exception notifications.
  262. *
  263. * Return value is either NOTIFY_STOP or NOTIFY_DONE as explained below.
  264. *
  265. * NOTIFY_DONE returned if one of the following conditions is true.
  266. * i) When the causative address is from user-space and the exception
  267. * is a valid one, i.e. not triggered as a result of lazy debug register
  268. * switching
  269. * ii) When there are more bits than trap<n> set in DR6 register (such
  270. * as BD, BS or BT) indicating that more than one debug condition is
  271. * met and requires some more action in do_debug().
  272. *
  273. * NOTIFY_STOP returned for all other cases
  274. *
  275. */
  276. int __kprobes hw_breakpoint_handler(struct die_args *args)
  277. {
  278. int i, cpu, rc = NOTIFY_STOP;
  279. struct hw_breakpoint *bp;
  280. /* The DR6 value is stored in args->err */
  281. unsigned long dr7, dr6 = args->err;
  282. /* Do an early return if no trap bits are set in DR6 */
  283. if ((dr6 & DR_TRAP_BITS) == 0)
  284. return NOTIFY_DONE;
  285. /* Lazy debug register switching */
  286. if (!test_tsk_thread_flag(current, TIF_DEBUG))
  287. arch_uninstall_thread_hw_breakpoint();
  288. get_debugreg(dr7, 7);
  289. /* Disable breakpoints during exception handling */
  290. set_debugreg(0UL, 7);
  291. /*
  292. * Assert that local interrupts are disabled
  293. * Reset the DRn bits in the virtualized register value.
  294. * The ptrace trigger routine will add in whatever is needed.
  295. */
  296. current->thread.debugreg6 &= ~DR_TRAP_BITS;
  297. cpu = get_cpu();
  298. /* Handle all the breakpoints that were triggered */
  299. for (i = 0; i < HBP_NUM; ++i) {
  300. if (likely(!(dr6 & (DR_TRAP0 << i))))
  301. continue;
  302. /*
  303. * Find the corresponding hw_breakpoint structure and
  304. * invoke its triggered callback.
  305. */
  306. if (i >= hbp_kernel_pos)
  307. bp = per_cpu(this_hbp_kernel[i], cpu);
  308. else {
  309. bp = current->thread.hbp[i];
  310. if (bp)
  311. rc = NOTIFY_DONE;
  312. }
  313. /*
  314. * bp can be NULL due to lazy debug register switching
  315. * or due to the delay between updates of hbp_kernel_pos
  316. * and this_hbp_kernel.
  317. */
  318. if (!bp)
  319. continue;
  320. (bp->triggered)(bp, args->regs);
  321. }
  322. if (dr6 & (~DR_TRAP_BITS))
  323. rc = NOTIFY_DONE;
  324. set_debugreg(dr7, 7);
  325. put_cpu_no_resched();
  326. return rc;
  327. }
  328. /*
  329. * Handle debug exception notifications.
  330. */
  331. int __kprobes hw_breakpoint_exceptions_notify(
  332. struct notifier_block *unused, unsigned long val, void *data)
  333. {
  334. if (val != DIE_DEBUG)
  335. return NOTIFY_DONE;
  336. return hw_breakpoint_handler(data);
  337. }