kprobes.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #ifndef _LINUX_KPROBES_H
  2. #define _LINUX_KPROBES_H
  3. /*
  4. * Kernel Probes (KProbes)
  5. * include/linux/kprobes.h
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. * Copyright (C) IBM Corporation, 2002, 2004
  22. *
  23. * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
  24. * Probes initial implementation ( includes suggestions from
  25. * Rusty Russell).
  26. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  27. * interface to access function arguments.
  28. * 2005-May Hien Nguyen <hien@us.ibm.com> and Jim Keniston
  29. * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
  30. * <prasanna@in.ibm.com> added function-return probes.
  31. */
  32. #include <linux/compiler.h>
  33. #include <linux/linkage.h>
  34. #include <linux/list.h>
  35. #include <linux/notifier.h>
  36. #include <linux/smp.h>
  37. #include <linux/bug.h>
  38. #include <linux/percpu.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/rcupdate.h>
  41. #include <linux/mutex.h>
  42. #include <linux/ftrace.h>
  43. #include <asm/kprobes.h>
  44. #ifdef CONFIG_KPROBES
  45. /* kprobe_status settings */
  46. #define KPROBE_HIT_ACTIVE 0x00000001
  47. #define KPROBE_HIT_SS 0x00000002
  48. #define KPROBE_REENTER 0x00000004
  49. #define KPROBE_HIT_SSDONE 0x00000008
  50. #else /* CONFIG_KPROBES */
  51. #include <asm-generic/kprobes.h>
  52. typedef int kprobe_opcode_t;
  53. struct arch_specific_insn {
  54. int dummy;
  55. };
  56. #endif /* CONFIG_KPROBES */
  57. struct kprobe;
  58. struct pt_regs;
  59. struct kretprobe;
  60. struct kretprobe_instance;
  61. typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
  62. typedef int (*kprobe_break_handler_t) (struct kprobe *, struct pt_regs *);
  63. typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
  64. unsigned long flags);
  65. typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *,
  66. int trapnr);
  67. typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
  68. struct pt_regs *);
  69. struct kprobe {
  70. struct hlist_node hlist;
  71. /* list of kprobes for multi-handler support */
  72. struct list_head list;
  73. /*count the number of times this probe was temporarily disarmed */
  74. unsigned long nmissed;
  75. /* location of the probe point */
  76. kprobe_opcode_t *addr;
  77. /* Allow user to indicate symbol name of the probe point */
  78. const char *symbol_name;
  79. /* Offset into the symbol */
  80. unsigned int offset;
  81. /* Called before addr is executed. */
  82. kprobe_pre_handler_t pre_handler;
  83. /* Called after addr is executed, unless... */
  84. kprobe_post_handler_t post_handler;
  85. /*
  86. * ... called if executing addr causes a fault (eg. page fault).
  87. * Return 1 if it handled fault, otherwise kernel will see it.
  88. */
  89. kprobe_fault_handler_t fault_handler;
  90. /*
  91. * ... called if breakpoint trap occurs in probe handler.
  92. * Return 1 if it handled break, otherwise kernel will see it.
  93. */
  94. kprobe_break_handler_t break_handler;
  95. /* Saved opcode (which has been replaced with breakpoint) */
  96. kprobe_opcode_t opcode;
  97. /* copy of the original instruction */
  98. struct arch_specific_insn ainsn;
  99. /*
  100. * Indicates various status flags.
  101. * Protected by kprobe_mutex after this kprobe is registered.
  102. */
  103. u32 flags;
  104. };
  105. /* Kprobe status flags */
  106. #define KPROBE_FLAG_GONE 1 /* breakpoint has already gone */
  107. #define KPROBE_FLAG_DISABLED 2 /* probe is temporarily disabled */
  108. #define KPROBE_FLAG_OPTIMIZED 4 /*
  109. * probe is really optimized.
  110. * NOTE:
  111. * this flag is only for optimized_kprobe.
  112. */
  113. #define KPROBE_FLAG_FTRACE 8 /* probe is using ftrace */
  114. /* Has this kprobe gone ? */
  115. static inline int kprobe_gone(struct kprobe *p)
  116. {
  117. return p->flags & KPROBE_FLAG_GONE;
  118. }
  119. /* Is this kprobe disabled ? */
  120. static inline int kprobe_disabled(struct kprobe *p)
  121. {
  122. return p->flags & (KPROBE_FLAG_DISABLED | KPROBE_FLAG_GONE);
  123. }
  124. /* Is this kprobe really running optimized path ? */
  125. static inline int kprobe_optimized(struct kprobe *p)
  126. {
  127. return p->flags & KPROBE_FLAG_OPTIMIZED;
  128. }
  129. /* Is this kprobe uses ftrace ? */
  130. static inline int kprobe_ftrace(struct kprobe *p)
  131. {
  132. return p->flags & KPROBE_FLAG_FTRACE;
  133. }
  134. /*
  135. * Special probe type that uses setjmp-longjmp type tricks to resume
  136. * execution at a specified entry with a matching prototype corresponding
  137. * to the probed function - a trick to enable arguments to become
  138. * accessible seamlessly by probe handling logic.
  139. * Note:
  140. * Because of the way compilers allocate stack space for local variables
  141. * etc upfront, regardless of sub-scopes within a function, this mirroring
  142. * principle currently works only for probes placed on function entry points.
  143. */
  144. struct jprobe {
  145. struct kprobe kp;
  146. void *entry; /* probe handling code to jump to */
  147. };
  148. /* For backward compatibility with old code using JPROBE_ENTRY() */
  149. #define JPROBE_ENTRY(handler) (handler)
  150. /*
  151. * Function-return probe -
  152. * Note:
  153. * User needs to provide a handler function, and initialize maxactive.
  154. * maxactive - The maximum number of instances of the probed function that
  155. * can be active concurrently.
  156. * nmissed - tracks the number of times the probed function's return was
  157. * ignored, due to maxactive being too low.
  158. *
  159. */
  160. struct kretprobe {
  161. struct kprobe kp;
  162. kretprobe_handler_t handler;
  163. kretprobe_handler_t entry_handler;
  164. int maxactive;
  165. int nmissed;
  166. size_t data_size;
  167. struct hlist_head free_instances;
  168. raw_spinlock_t lock;
  169. };
  170. struct kretprobe_instance {
  171. struct hlist_node hlist;
  172. struct kretprobe *rp;
  173. kprobe_opcode_t *ret_addr;
  174. struct task_struct *task;
  175. char data[0];
  176. };
  177. struct kretprobe_blackpoint {
  178. const char *name;
  179. void *addr;
  180. };
  181. struct kprobe_blacklist_entry {
  182. struct list_head list;
  183. unsigned long start_addr;
  184. unsigned long end_addr;
  185. };
  186. #ifdef CONFIG_KPROBES
  187. DECLARE_PER_CPU(struct kprobe *, current_kprobe);
  188. DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  189. /*
  190. * For #ifdef avoidance:
  191. */
  192. static inline int kprobes_built_in(void)
  193. {
  194. return 1;
  195. }
  196. #ifdef CONFIG_KRETPROBES
  197. extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
  198. struct pt_regs *regs);
  199. extern int arch_trampoline_kprobe(struct kprobe *p);
  200. #else /* CONFIG_KRETPROBES */
  201. static inline void arch_prepare_kretprobe(struct kretprobe *rp,
  202. struct pt_regs *regs)
  203. {
  204. }
  205. static inline int arch_trampoline_kprobe(struct kprobe *p)
  206. {
  207. return 0;
  208. }
  209. #endif /* CONFIG_KRETPROBES */
  210. extern struct kretprobe_blackpoint kretprobe_blacklist[];
  211. static inline void kretprobe_assert(struct kretprobe_instance *ri,
  212. unsigned long orig_ret_address, unsigned long trampoline_address)
  213. {
  214. if (!orig_ret_address || (orig_ret_address == trampoline_address)) {
  215. printk("kretprobe BUG!: Processing kretprobe %p @ %p\n",
  216. ri->rp, ri->rp->kp.addr);
  217. BUG();
  218. }
  219. }
  220. #ifdef CONFIG_KPROBES_SANITY_TEST
  221. extern int init_test_probes(void);
  222. #else
  223. static inline int init_test_probes(void)
  224. {
  225. return 0;
  226. }
  227. #endif /* CONFIG_KPROBES_SANITY_TEST */
  228. extern int arch_prepare_kprobe(struct kprobe *p);
  229. extern void arch_arm_kprobe(struct kprobe *p);
  230. extern void arch_disarm_kprobe(struct kprobe *p);
  231. extern int arch_init_kprobes(void);
  232. extern void show_registers(struct pt_regs *regs);
  233. extern void kprobes_inc_nmissed_count(struct kprobe *p);
  234. extern bool arch_within_kprobe_blacklist(unsigned long addr);
  235. extern bool arch_kprobe_on_func_entry(unsigned long offset);
  236. extern bool kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
  237. extern bool within_kprobe_blacklist(unsigned long addr);
  238. struct kprobe_insn_cache {
  239. struct mutex mutex;
  240. void *(*alloc)(void); /* allocate insn page */
  241. void (*free)(void *); /* free insn page */
  242. struct list_head pages; /* list of kprobe_insn_page */
  243. size_t insn_size; /* size of instruction slot */
  244. int nr_garbage;
  245. };
  246. #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
  247. extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c);
  248. extern void __free_insn_slot(struct kprobe_insn_cache *c,
  249. kprobe_opcode_t *slot, int dirty);
  250. /* sleep-less address checking routine */
  251. extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c,
  252. unsigned long addr);
  253. #define DEFINE_INSN_CACHE_OPS(__name) \
  254. extern struct kprobe_insn_cache kprobe_##__name##_slots; \
  255. \
  256. static inline kprobe_opcode_t *get_##__name##_slot(void) \
  257. { \
  258. return __get_insn_slot(&kprobe_##__name##_slots); \
  259. } \
  260. \
  261. static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
  262. { \
  263. __free_insn_slot(&kprobe_##__name##_slots, slot, dirty); \
  264. } \
  265. \
  266. static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
  267. { \
  268. return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \
  269. }
  270. #else /* __ARCH_WANT_KPROBES_INSN_SLOT */
  271. #define DEFINE_INSN_CACHE_OPS(__name) \
  272. static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
  273. { \
  274. return 0; \
  275. }
  276. #endif
  277. DEFINE_INSN_CACHE_OPS(insn);
  278. #ifdef CONFIG_OPTPROBES
  279. /*
  280. * Internal structure for direct jump optimized probe
  281. */
  282. struct optimized_kprobe {
  283. struct kprobe kp;
  284. struct list_head list; /* list for optimizing queue */
  285. struct arch_optimized_insn optinsn;
  286. };
  287. /* Architecture dependent functions for direct jump optimization */
  288. extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn);
  289. extern int arch_check_optimized_kprobe(struct optimized_kprobe *op);
  290. extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
  291. struct kprobe *orig);
  292. extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op);
  293. extern void arch_optimize_kprobes(struct list_head *oplist);
  294. extern void arch_unoptimize_kprobes(struct list_head *oplist,
  295. struct list_head *done_list);
  296. extern void arch_unoptimize_kprobe(struct optimized_kprobe *op);
  297. extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
  298. unsigned long addr);
  299. extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
  300. DEFINE_INSN_CACHE_OPS(optinsn);
  301. #ifdef CONFIG_SYSCTL
  302. extern int sysctl_kprobes_optimization;
  303. extern int proc_kprobes_optimization_handler(struct ctl_table *table,
  304. int write, void __user *buffer,
  305. size_t *length, loff_t *ppos);
  306. #endif
  307. extern void wait_for_kprobe_optimizer(void);
  308. #else
  309. static inline void wait_for_kprobe_optimizer(void) { }
  310. #endif /* CONFIG_OPTPROBES */
  311. #ifdef CONFIG_KPROBES_ON_FTRACE
  312. extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
  313. struct ftrace_ops *ops, struct pt_regs *regs);
  314. extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
  315. #endif
  316. int arch_check_ftrace_location(struct kprobe *p);
  317. /* Get the kprobe at this addr (if any) - called with preemption disabled */
  318. struct kprobe *get_kprobe(void *addr);
  319. void kretprobe_hash_lock(struct task_struct *tsk,
  320. struct hlist_head **head, unsigned long *flags);
  321. void kretprobe_hash_unlock(struct task_struct *tsk, unsigned long *flags);
  322. struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk);
  323. /* kprobe_running() will just return the current_kprobe on this CPU */
  324. static inline struct kprobe *kprobe_running(void)
  325. {
  326. return (__this_cpu_read(current_kprobe));
  327. }
  328. static inline void reset_current_kprobe(void)
  329. {
  330. __this_cpu_write(current_kprobe, NULL);
  331. }
  332. static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
  333. {
  334. return this_cpu_ptr(&kprobe_ctlblk);
  335. }
  336. kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
  337. int register_kprobe(struct kprobe *p);
  338. void unregister_kprobe(struct kprobe *p);
  339. int register_kprobes(struct kprobe **kps, int num);
  340. void unregister_kprobes(struct kprobe **kps, int num);
  341. unsigned long arch_deref_entry_point(void *);
  342. int register_kretprobe(struct kretprobe *rp);
  343. void unregister_kretprobe(struct kretprobe *rp);
  344. int register_kretprobes(struct kretprobe **rps, int num);
  345. void unregister_kretprobes(struct kretprobe **rps, int num);
  346. void kprobe_flush_task(struct task_struct *tk);
  347. void recycle_rp_inst(struct kretprobe_instance *ri, struct hlist_head *head);
  348. int disable_kprobe(struct kprobe *kp);
  349. int enable_kprobe(struct kprobe *kp);
  350. void dump_kprobe(struct kprobe *kp);
  351. #else /* !CONFIG_KPROBES: */
  352. static inline int kprobes_built_in(void)
  353. {
  354. return 0;
  355. }
  356. static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  357. {
  358. return 0;
  359. }
  360. static inline struct kprobe *get_kprobe(void *addr)
  361. {
  362. return NULL;
  363. }
  364. static inline struct kprobe *kprobe_running(void)
  365. {
  366. return NULL;
  367. }
  368. static inline int register_kprobe(struct kprobe *p)
  369. {
  370. return -ENOSYS;
  371. }
  372. static inline int register_kprobes(struct kprobe **kps, int num)
  373. {
  374. return -ENOSYS;
  375. }
  376. static inline void unregister_kprobe(struct kprobe *p)
  377. {
  378. }
  379. static inline void unregister_kprobes(struct kprobe **kps, int num)
  380. {
  381. }
  382. static inline void jprobe_return(void)
  383. {
  384. }
  385. static inline int register_kretprobe(struct kretprobe *rp)
  386. {
  387. return -ENOSYS;
  388. }
  389. static inline int register_kretprobes(struct kretprobe **rps, int num)
  390. {
  391. return -ENOSYS;
  392. }
  393. static inline void unregister_kretprobe(struct kretprobe *rp)
  394. {
  395. }
  396. static inline void unregister_kretprobes(struct kretprobe **rps, int num)
  397. {
  398. }
  399. static inline void kprobe_flush_task(struct task_struct *tk)
  400. {
  401. }
  402. static inline int disable_kprobe(struct kprobe *kp)
  403. {
  404. return -ENOSYS;
  405. }
  406. static inline int enable_kprobe(struct kprobe *kp)
  407. {
  408. return -ENOSYS;
  409. }
  410. #endif /* CONFIG_KPROBES */
  411. static inline int register_jprobe(struct jprobe *p)
  412. {
  413. return -ENOSYS;
  414. }
  415. static inline int register_jprobes(struct jprobe **jps, int num)
  416. {
  417. return -ENOSYS;
  418. }
  419. static inline void unregister_jprobe(struct jprobe *p)
  420. {
  421. }
  422. static inline void unregister_jprobes(struct jprobe **jps, int num)
  423. {
  424. }
  425. static inline int disable_kretprobe(struct kretprobe *rp)
  426. {
  427. return disable_kprobe(&rp->kp);
  428. }
  429. static inline int enable_kretprobe(struct kretprobe *rp)
  430. {
  431. return enable_kprobe(&rp->kp);
  432. }
  433. static inline int disable_jprobe(struct jprobe *jp)
  434. {
  435. return -ENOSYS;
  436. }
  437. static inline int enable_jprobe(struct jprobe *jp)
  438. {
  439. return -ENOSYS;
  440. }
  441. #ifndef CONFIG_KPROBES
  442. static inline bool is_kprobe_insn_slot(unsigned long addr)
  443. {
  444. return false;
  445. }
  446. #endif
  447. #ifndef CONFIG_OPTPROBES
  448. static inline bool is_kprobe_optinsn_slot(unsigned long addr)
  449. {
  450. return false;
  451. }
  452. #endif
  453. #endif /* _LINUX_KPROBES_H */