hw_breakpoint.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #ifndef _LINUX_HW_BREAKPOINT_H
  2. #define _LINUX_HW_BREAKPOINT_H
  3. #ifdef __KERNEL__
  4. #include <linux/list.h>
  5. #include <linux/types.h>
  6. #include <linux/kallsyms.h>
  7. /**
  8. * struct hw_breakpoint - unified kernel/user-space hardware breakpoint
  9. * @triggered: callback invoked after target address access
  10. * @info: arch-specific breakpoint info (address, length, and type)
  11. *
  12. * %hw_breakpoint structures are the kernel's way of representing
  13. * hardware breakpoints. These are data breakpoints
  14. * (also known as "watchpoints", triggered on data access), and the breakpoint's
  15. * target address can be located in either kernel space or user space.
  16. *
  17. * The breakpoint's address, length, and type are highly
  18. * architecture-specific. The values are encoded in the @info field; you
  19. * specify them when registering the breakpoint. To examine the encoded
  20. * values use hw_breakpoint_get_{kaddress,uaddress,len,type}(), declared
  21. * below.
  22. *
  23. * The address is specified as a regular kernel pointer (for kernel-space
  24. * breakponts) or as an %__user pointer (for user-space breakpoints).
  25. * With register_user_hw_breakpoint(), the address must refer to a
  26. * location in user space. The breakpoint will be active only while the
  27. * requested task is running. Conversely with
  28. * register_kernel_hw_breakpoint(), the address must refer to a location
  29. * in kernel space, and the breakpoint will be active on all CPUs
  30. * regardless of the current task.
  31. *
  32. * The length is the breakpoint's extent in bytes, which is subject to
  33. * certain limitations. include/asm/hw_breakpoint.h contains macros
  34. * defining the available lengths for a specific architecture. Note that
  35. * the address's alignment must match the length. The breakpoint will
  36. * catch accesses to any byte in the range from address to address +
  37. * (length - 1).
  38. *
  39. * The breakpoint's type indicates the sort of access that will cause it
  40. * to trigger. Possible values may include:
  41. *
  42. * %HW_BREAKPOINT_RW (triggered on read or write access),
  43. * %HW_BREAKPOINT_WRITE (triggered on write access), and
  44. * %HW_BREAKPOINT_READ (triggered on read access).
  45. *
  46. * Appropriate macros are defined in include/asm/hw_breakpoint.h; not all
  47. * possibilities are available on all architectures. Execute breakpoints
  48. * must have length equal to the special value %HW_BREAKPOINT_LEN_EXECUTE.
  49. *
  50. * When a breakpoint gets hit, the @triggered callback is
  51. * invoked in_interrupt with a pointer to the %hw_breakpoint structure and the
  52. * processor registers.
  53. * Data breakpoints occur after the memory access has taken place.
  54. * Breakpoints are disabled during execution @triggered, to avoid
  55. * recursive traps and allow unhindered access to breakpointed memory.
  56. *
  57. * This sample code sets a breakpoint on pid_max and registers a callback
  58. * function for writes to that variable. Note that it is not portable
  59. * as written, because not all architectures support HW_BREAKPOINT_LEN_4.
  60. *
  61. * ----------------------------------------------------------------------
  62. *
  63. * #include <asm/hw_breakpoint.h>
  64. *
  65. * struct hw_breakpoint my_bp;
  66. *
  67. * static void my_triggered(struct hw_breakpoint *bp, struct pt_regs *regs)
  68. * {
  69. * printk(KERN_DEBUG "Inside triggered routine of breakpoint exception\n");
  70. * dump_stack();
  71. * .......<more debugging output>........
  72. * }
  73. *
  74. * static struct hw_breakpoint my_bp;
  75. *
  76. * static int init_module(void)
  77. * {
  78. * ..........<do anything>............
  79. * my_bp.info.type = HW_BREAKPOINT_WRITE;
  80. * my_bp.info.len = HW_BREAKPOINT_LEN_4;
  81. *
  82. * my_bp.installed = (void *)my_bp_installed;
  83. *
  84. * rc = register_kernel_hw_breakpoint(&my_bp);
  85. * ..........<do anything>............
  86. * }
  87. *
  88. * static void cleanup_module(void)
  89. * {
  90. * ..........<do anything>............
  91. * unregister_kernel_hw_breakpoint(&my_bp);
  92. * ..........<do anything>............
  93. * }
  94. *
  95. * ----------------------------------------------------------------------
  96. */
  97. struct hw_breakpoint {
  98. void (*triggered)(struct hw_breakpoint *, struct pt_regs *);
  99. struct arch_hw_breakpoint info;
  100. };
  101. /*
  102. * len and type values are defined in include/asm/hw_breakpoint.h.
  103. * Available values vary according to the architecture. On i386 the
  104. * possibilities are:
  105. *
  106. * HW_BREAKPOINT_LEN_1
  107. * HW_BREAKPOINT_LEN_2
  108. * HW_BREAKPOINT_LEN_4
  109. * HW_BREAKPOINT_RW
  110. * HW_BREAKPOINT_READ
  111. *
  112. * On other architectures HW_BREAKPOINT_LEN_8 may be available, and the
  113. * 1-, 2-, and 4-byte lengths may be unavailable. There also may be
  114. * HW_BREAKPOINT_WRITE. You can use #ifdef to check at compile time.
  115. */
  116. extern int register_user_hw_breakpoint(struct task_struct *tsk,
  117. struct hw_breakpoint *bp);
  118. extern int modify_user_hw_breakpoint(struct task_struct *tsk,
  119. struct hw_breakpoint *bp);
  120. extern void unregister_user_hw_breakpoint(struct task_struct *tsk,
  121. struct hw_breakpoint *bp);
  122. /*
  123. * Kernel breakpoints are not associated with any particular thread.
  124. */
  125. extern int register_kernel_hw_breakpoint(struct hw_breakpoint *bp);
  126. extern void unregister_kernel_hw_breakpoint(struct hw_breakpoint *bp);
  127. extern unsigned int hbp_kernel_pos;
  128. #endif /* __KERNEL__ */
  129. #endif /* _LINUX_HW_BREAKPOINT_H */