Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 #ifndef _LINUX_KPROBES_H
0003 #define _LINUX_KPROBES_H
0004 /*
0005  *  Kernel Probes (KProbes)
0006  *
0007  * Copyright (C) IBM Corporation, 2002, 2004
0008  *
0009  * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
0010  *      Probes initial implementation ( includes suggestions from
0011  *      Rusty Russell).
0012  * 2004-July    Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
0013  *      interface to access function arguments.
0014  * 2005-May Hien Nguyen <hien@us.ibm.com> and Jim Keniston
0015  *      <jkenisto@us.ibm.com>  and Prasanna S Panchamukhi
0016  *      <prasanna@in.ibm.com> added function-return probes.
0017  */
0018 #include <linux/compiler.h>
0019 #include <linux/linkage.h>
0020 #include <linux/list.h>
0021 #include <linux/notifier.h>
0022 #include <linux/smp.h>
0023 #include <linux/bug.h>
0024 #include <linux/percpu.h>
0025 #include <linux/spinlock.h>
0026 #include <linux/rcupdate.h>
0027 #include <linux/mutex.h>
0028 #include <linux/ftrace.h>
0029 #include <linux/refcount.h>
0030 #include <linux/freelist.h>
0031 #include <linux/rethook.h>
0032 #include <asm/kprobes.h>
0033 
0034 #ifdef CONFIG_KPROBES
0035 
0036 /* kprobe_status settings */
0037 #define KPROBE_HIT_ACTIVE   0x00000001
0038 #define KPROBE_HIT_SS       0x00000002
0039 #define KPROBE_REENTER      0x00000004
0040 #define KPROBE_HIT_SSDONE   0x00000008
0041 
0042 #else /* !CONFIG_KPROBES */
0043 #include <asm-generic/kprobes.h>
0044 typedef int kprobe_opcode_t;
0045 struct arch_specific_insn {
0046     int dummy;
0047 };
0048 #endif /* CONFIG_KPROBES */
0049 
0050 struct kprobe;
0051 struct pt_regs;
0052 struct kretprobe;
0053 struct kretprobe_instance;
0054 typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
0055 typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
0056                        unsigned long flags);
0057 typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
0058                     struct pt_regs *);
0059 
0060 struct kprobe {
0061     struct hlist_node hlist;
0062 
0063     /* list of kprobes for multi-handler support */
0064     struct list_head list;
0065 
0066     /*count the number of times this probe was temporarily disarmed */
0067     unsigned long nmissed;
0068 
0069     /* location of the probe point */
0070     kprobe_opcode_t *addr;
0071 
0072     /* Allow user to indicate symbol name of the probe point */
0073     const char *symbol_name;
0074 
0075     /* Offset into the symbol */
0076     unsigned int offset;
0077 
0078     /* Called before addr is executed. */
0079     kprobe_pre_handler_t pre_handler;
0080 
0081     /* Called after addr is executed, unless... */
0082     kprobe_post_handler_t post_handler;
0083 
0084     /* Saved opcode (which has been replaced with breakpoint) */
0085     kprobe_opcode_t opcode;
0086 
0087     /* copy of the original instruction */
0088     struct arch_specific_insn ainsn;
0089 
0090     /*
0091      * Indicates various status flags.
0092      * Protected by kprobe_mutex after this kprobe is registered.
0093      */
0094     u32 flags;
0095 };
0096 
0097 /* Kprobe status flags */
0098 #define KPROBE_FLAG_GONE    1 /* breakpoint has already gone */
0099 #define KPROBE_FLAG_DISABLED    2 /* probe is temporarily disabled */
0100 #define KPROBE_FLAG_OPTIMIZED   4 /*
0101                    * probe is really optimized.
0102                    * NOTE:
0103                    * this flag is only for optimized_kprobe.
0104                    */
0105 #define KPROBE_FLAG_FTRACE  8 /* probe is using ftrace */
0106 
0107 /* Has this kprobe gone ? */
0108 static inline bool kprobe_gone(struct kprobe *p)
0109 {
0110     return p->flags & KPROBE_FLAG_GONE;
0111 }
0112 
0113 /* Is this kprobe disabled ? */
0114 static inline bool kprobe_disabled(struct kprobe *p)
0115 {
0116     return p->flags & (KPROBE_FLAG_DISABLED | KPROBE_FLAG_GONE);
0117 }
0118 
0119 /* Is this kprobe really running optimized path ? */
0120 static inline bool kprobe_optimized(struct kprobe *p)
0121 {
0122     return p->flags & KPROBE_FLAG_OPTIMIZED;
0123 }
0124 
0125 /* Is this kprobe uses ftrace ? */
0126 static inline bool kprobe_ftrace(struct kprobe *p)
0127 {
0128     return p->flags & KPROBE_FLAG_FTRACE;
0129 }
0130 
0131 /*
0132  * Function-return probe -
0133  * Note:
0134  * User needs to provide a handler function, and initialize maxactive.
0135  * maxactive - The maximum number of instances of the probed function that
0136  * can be active concurrently.
0137  * nmissed - tracks the number of times the probed function's return was
0138  * ignored, due to maxactive being too low.
0139  *
0140  */
0141 struct kretprobe_holder {
0142     struct kretprobe    *rp;
0143     refcount_t      ref;
0144 };
0145 
0146 struct kretprobe {
0147     struct kprobe kp;
0148     kretprobe_handler_t handler;
0149     kretprobe_handler_t entry_handler;
0150     int maxactive;
0151     int nmissed;
0152     size_t data_size;
0153 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
0154     struct rethook *rh;
0155 #else
0156     struct freelist_head freelist;
0157     struct kretprobe_holder *rph;
0158 #endif
0159 };
0160 
0161 #define KRETPROBE_MAX_DATA_SIZE 4096
0162 
0163 struct kretprobe_instance {
0164 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
0165     struct rethook_node node;
0166 #else
0167     union {
0168         struct freelist_node freelist;
0169         struct rcu_head rcu;
0170     };
0171     struct llist_node llist;
0172     struct kretprobe_holder *rph;
0173     kprobe_opcode_t *ret_addr;
0174     void *fp;
0175 #endif
0176     char data[];
0177 };
0178 
0179 struct kretprobe_blackpoint {
0180     const char *name;
0181     void *addr;
0182 };
0183 
0184 struct kprobe_blacklist_entry {
0185     struct list_head list;
0186     unsigned long start_addr;
0187     unsigned long end_addr;
0188 };
0189 
0190 #ifdef CONFIG_KPROBES
0191 DECLARE_PER_CPU(struct kprobe *, current_kprobe);
0192 DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
0193 
0194 extern void kprobe_busy_begin(void);
0195 extern void kprobe_busy_end(void);
0196 
0197 #ifdef CONFIG_KRETPROBES
0198 /* Check whether @p is used for implementing a trampoline. */
0199 extern int arch_trampoline_kprobe(struct kprobe *p);
0200 
0201 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
0202 static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
0203 {
0204     RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
0205         "Kretprobe is accessed from instance under preemptive context");
0206 
0207     return (struct kretprobe *)READ_ONCE(ri->node.rethook->data);
0208 }
0209 static nokprobe_inline unsigned long get_kretprobe_retaddr(struct kretprobe_instance *ri)
0210 {
0211     return ri->node.ret_addr;
0212 }
0213 #else
0214 extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
0215                    struct pt_regs *regs);
0216 void arch_kretprobe_fixup_return(struct pt_regs *regs,
0217                  kprobe_opcode_t *correct_ret_addr);
0218 
0219 void __kretprobe_trampoline(void);
0220 /*
0221  * Since some architecture uses structured function pointer,
0222  * use dereference_function_descriptor() to get real function address.
0223  */
0224 static nokprobe_inline void *kretprobe_trampoline_addr(void)
0225 {
0226     return dereference_kernel_function_descriptor(__kretprobe_trampoline);
0227 }
0228 
0229 /* If the trampoline handler called from a kprobe, use this version */
0230 unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
0231                          void *frame_pointer);
0232 
0233 static nokprobe_inline
0234 unsigned long kretprobe_trampoline_handler(struct pt_regs *regs,
0235                        void *frame_pointer)
0236 {
0237     unsigned long ret;
0238     /*
0239      * Set a dummy kprobe for avoiding kretprobe recursion.
0240      * Since kretprobe never runs in kprobe handler, no kprobe must
0241      * be running at this point.
0242      */
0243     kprobe_busy_begin();
0244     ret = __kretprobe_trampoline_handler(regs, frame_pointer);
0245     kprobe_busy_end();
0246 
0247     return ret;
0248 }
0249 
0250 static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
0251 {
0252     RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
0253         "Kretprobe is accessed from instance under preemptive context");
0254 
0255     return READ_ONCE(ri->rph->rp);
0256 }
0257 
0258 static nokprobe_inline unsigned long get_kretprobe_retaddr(struct kretprobe_instance *ri)
0259 {
0260     return (unsigned long)ri->ret_addr;
0261 }
0262 #endif /* CONFIG_KRETPROBE_ON_RETHOOK */
0263 
0264 #else /* !CONFIG_KRETPROBES */
0265 static inline void arch_prepare_kretprobe(struct kretprobe *rp,
0266                     struct pt_regs *regs)
0267 {
0268 }
0269 static inline int arch_trampoline_kprobe(struct kprobe *p)
0270 {
0271     return 0;
0272 }
0273 #endif /* CONFIG_KRETPROBES */
0274 
0275 /* Markers of '_kprobe_blacklist' section */
0276 extern unsigned long __start_kprobe_blacklist[];
0277 extern unsigned long __stop_kprobe_blacklist[];
0278 
0279 extern struct kretprobe_blackpoint kretprobe_blacklist[];
0280 
0281 #ifdef CONFIG_KPROBES_SANITY_TEST
0282 extern int init_test_probes(void);
0283 #else /* !CONFIG_KPROBES_SANITY_TEST */
0284 static inline int init_test_probes(void)
0285 {
0286     return 0;
0287 }
0288 #endif /* CONFIG_KPROBES_SANITY_TEST */
0289 
0290 extern int arch_prepare_kprobe(struct kprobe *p);
0291 extern void arch_arm_kprobe(struct kprobe *p);
0292 extern void arch_disarm_kprobe(struct kprobe *p);
0293 extern int arch_init_kprobes(void);
0294 extern void kprobes_inc_nmissed_count(struct kprobe *p);
0295 extern bool arch_within_kprobe_blacklist(unsigned long addr);
0296 extern int arch_populate_kprobe_blacklist(void);
0297 extern int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
0298 
0299 extern bool within_kprobe_blacklist(unsigned long addr);
0300 extern int kprobe_add_ksym_blacklist(unsigned long entry);
0301 extern int kprobe_add_area_blacklist(unsigned long start, unsigned long end);
0302 
0303 struct kprobe_insn_cache {
0304     struct mutex mutex;
0305     void *(*alloc)(void);   /* allocate insn page */
0306     void (*free)(void *);   /* free insn page */
0307     const char *sym;    /* symbol for insn pages */
0308     struct list_head pages; /* list of kprobe_insn_page */
0309     size_t insn_size;   /* size of instruction slot */
0310     int nr_garbage;
0311 };
0312 
0313 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
0314 extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c);
0315 extern void __free_insn_slot(struct kprobe_insn_cache *c,
0316                  kprobe_opcode_t *slot, int dirty);
0317 /* sleep-less address checking routine  */
0318 extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c,
0319                 unsigned long addr);
0320 
0321 #define DEFINE_INSN_CACHE_OPS(__name)                   \
0322 extern struct kprobe_insn_cache kprobe_##__name##_slots;        \
0323                                     \
0324 static inline kprobe_opcode_t *get_##__name##_slot(void)        \
0325 {                                   \
0326     return __get_insn_slot(&kprobe_##__name##_slots);       \
0327 }                                   \
0328                                     \
0329 static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
0330 {                                   \
0331     __free_insn_slot(&kprobe_##__name##_slots, slot, dirty);    \
0332 }                                   \
0333                                     \
0334 static inline bool is_kprobe_##__name##_slot(unsigned long addr)    \
0335 {                                   \
0336     return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \
0337 }
0338 #define KPROBE_INSN_PAGE_SYM        "kprobe_insn_page"
0339 #define KPROBE_OPTINSN_PAGE_SYM     "kprobe_optinsn_page"
0340 int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
0341                  unsigned long *value, char *type, char *sym);
0342 #else /* !__ARCH_WANT_KPROBES_INSN_SLOT */
0343 #define DEFINE_INSN_CACHE_OPS(__name)                   \
0344 static inline bool is_kprobe_##__name##_slot(unsigned long addr)    \
0345 {                                   \
0346     return 0;                           \
0347 }
0348 #endif
0349 
0350 DEFINE_INSN_CACHE_OPS(insn);
0351 
0352 #ifdef CONFIG_OPTPROBES
0353 /*
0354  * Internal structure for direct jump optimized probe
0355  */
0356 struct optimized_kprobe {
0357     struct kprobe kp;
0358     struct list_head list;  /* list for optimizing queue */
0359     struct arch_optimized_insn optinsn;
0360 };
0361 
0362 /* Architecture dependent functions for direct jump optimization */
0363 extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn);
0364 extern int arch_check_optimized_kprobe(struct optimized_kprobe *op);
0365 extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
0366                      struct kprobe *orig);
0367 extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op);
0368 extern void arch_optimize_kprobes(struct list_head *oplist);
0369 extern void arch_unoptimize_kprobes(struct list_head *oplist,
0370                     struct list_head *done_list);
0371 extern void arch_unoptimize_kprobe(struct optimized_kprobe *op);
0372 extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
0373                     kprobe_opcode_t *addr);
0374 
0375 extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
0376 
0377 DEFINE_INSN_CACHE_OPS(optinsn);
0378 
0379 extern void wait_for_kprobe_optimizer(void);
0380 #else /* !CONFIG_OPTPROBES */
0381 static inline void wait_for_kprobe_optimizer(void) { }
0382 #endif /* CONFIG_OPTPROBES */
0383 
0384 #ifdef CONFIG_KPROBES_ON_FTRACE
0385 extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
0386                   struct ftrace_ops *ops, struct ftrace_regs *fregs);
0387 extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
0388 #else
0389 static inline int arch_prepare_kprobe_ftrace(struct kprobe *p)
0390 {
0391     return -EINVAL;
0392 }
0393 #endif /* CONFIG_KPROBES_ON_FTRACE */
0394 
0395 /* Get the kprobe at this addr (if any) - called with preemption disabled */
0396 struct kprobe *get_kprobe(void *addr);
0397 
0398 /* kprobe_running() will just return the current_kprobe on this CPU */
0399 static inline struct kprobe *kprobe_running(void)
0400 {
0401     return __this_cpu_read(current_kprobe);
0402 }
0403 
0404 static inline void reset_current_kprobe(void)
0405 {
0406     __this_cpu_write(current_kprobe, NULL);
0407 }
0408 
0409 static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
0410 {
0411     return this_cpu_ptr(&kprobe_ctlblk);
0412 }
0413 
0414 kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
0415 kprobe_opcode_t *arch_adjust_kprobe_addr(unsigned long addr, unsigned long offset, bool *on_func_entry);
0416 
0417 int register_kprobe(struct kprobe *p);
0418 void unregister_kprobe(struct kprobe *p);
0419 int register_kprobes(struct kprobe **kps, int num);
0420 void unregister_kprobes(struct kprobe **kps, int num);
0421 
0422 int register_kretprobe(struct kretprobe *rp);
0423 void unregister_kretprobe(struct kretprobe *rp);
0424 int register_kretprobes(struct kretprobe **rps, int num);
0425 void unregister_kretprobes(struct kretprobe **rps, int num);
0426 
0427 #if defined(CONFIG_KRETPROBE_ON_RETHOOK) || !defined(CONFIG_KRETPROBES)
0428 #define kprobe_flush_task(tk)   do {} while (0)
0429 #else
0430 void kprobe_flush_task(struct task_struct *tk);
0431 #endif
0432 
0433 void kprobe_free_init_mem(void);
0434 
0435 int disable_kprobe(struct kprobe *kp);
0436 int enable_kprobe(struct kprobe *kp);
0437 
0438 void dump_kprobe(struct kprobe *kp);
0439 
0440 void *alloc_insn_page(void);
0441 
0442 void *alloc_optinsn_page(void);
0443 void free_optinsn_page(void *page);
0444 
0445 int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
0446                char *sym);
0447 
0448 int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
0449                 char *type, char *sym);
0450 #else /* !CONFIG_KPROBES: */
0451 
0452 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
0453 {
0454     return 0;
0455 }
0456 static inline struct kprobe *get_kprobe(void *addr)
0457 {
0458     return NULL;
0459 }
0460 static inline struct kprobe *kprobe_running(void)
0461 {
0462     return NULL;
0463 }
0464 #define kprobe_busy_begin() do {} while (0)
0465 #define kprobe_busy_end()   do {} while (0)
0466 
0467 static inline int register_kprobe(struct kprobe *p)
0468 {
0469     return -EOPNOTSUPP;
0470 }
0471 static inline int register_kprobes(struct kprobe **kps, int num)
0472 {
0473     return -EOPNOTSUPP;
0474 }
0475 static inline void unregister_kprobe(struct kprobe *p)
0476 {
0477 }
0478 static inline void unregister_kprobes(struct kprobe **kps, int num)
0479 {
0480 }
0481 static inline int register_kretprobe(struct kretprobe *rp)
0482 {
0483     return -EOPNOTSUPP;
0484 }
0485 static inline int register_kretprobes(struct kretprobe **rps, int num)
0486 {
0487     return -EOPNOTSUPP;
0488 }
0489 static inline void unregister_kretprobe(struct kretprobe *rp)
0490 {
0491 }
0492 static inline void unregister_kretprobes(struct kretprobe **rps, int num)
0493 {
0494 }
0495 static inline void kprobe_flush_task(struct task_struct *tk)
0496 {
0497 }
0498 static inline void kprobe_free_init_mem(void)
0499 {
0500 }
0501 static inline int disable_kprobe(struct kprobe *kp)
0502 {
0503     return -EOPNOTSUPP;
0504 }
0505 static inline int enable_kprobe(struct kprobe *kp)
0506 {
0507     return -EOPNOTSUPP;
0508 }
0509 
0510 static inline bool within_kprobe_blacklist(unsigned long addr)
0511 {
0512     return true;
0513 }
0514 static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
0515                      char *type, char *sym)
0516 {
0517     return -ERANGE;
0518 }
0519 #endif /* CONFIG_KPROBES */
0520 
0521 static inline int disable_kretprobe(struct kretprobe *rp)
0522 {
0523     return disable_kprobe(&rp->kp);
0524 }
0525 static inline int enable_kretprobe(struct kretprobe *rp)
0526 {
0527     return enable_kprobe(&rp->kp);
0528 }
0529 
0530 #ifndef CONFIG_KPROBES
0531 static inline bool is_kprobe_insn_slot(unsigned long addr)
0532 {
0533     return false;
0534 }
0535 #endif /* !CONFIG_KPROBES */
0536 
0537 #ifndef CONFIG_OPTPROBES
0538 static inline bool is_kprobe_optinsn_slot(unsigned long addr)
0539 {
0540     return false;
0541 }
0542 #endif /* !CONFIG_OPTPROBES */
0543 
0544 #ifdef CONFIG_KRETPROBES
0545 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
0546 static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
0547 {
0548     return is_rethook_trampoline(addr);
0549 }
0550 
0551 static nokprobe_inline
0552 unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
0553                       struct llist_node **cur)
0554 {
0555     return rethook_find_ret_addr(tsk, (unsigned long)fp, cur);
0556 }
0557 #else
0558 static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
0559 {
0560     return (void *)addr == kretprobe_trampoline_addr();
0561 }
0562 
0563 unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
0564                       struct llist_node **cur);
0565 #endif
0566 #else
0567 static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
0568 {
0569     return false;
0570 }
0571 
0572 static nokprobe_inline
0573 unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
0574                       struct llist_node **cur)
0575 {
0576     return 0;
0577 }
0578 #endif
0579 
0580 /* Returns true if kprobes handled the fault */
0581 static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
0582                           unsigned int trap)
0583 {
0584     if (!IS_ENABLED(CONFIG_KPROBES))
0585         return false;
0586     if (user_mode(regs))
0587         return false;
0588     /*
0589      * To be potentially processing a kprobe fault and to be allowed
0590      * to call kprobe_running(), we have to be non-preemptible.
0591      */
0592     if (preemptible())
0593         return false;
0594     if (!kprobe_running())
0595         return false;
0596     return kprobe_fault_handler(regs, trap);
0597 }
0598 
0599 #endif /* _LINUX_KPROBES_H */