| .. | .. |
|---|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-or-later */ |
|---|
| 1 | 2 | #ifndef _LINUX_KPROBES_H |
|---|
| 2 | 3 | #define _LINUX_KPROBES_H |
|---|
| 3 | 4 | /* |
|---|
| 4 | 5 | * Kernel Probes (KProbes) |
|---|
| 5 | 6 | * 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 | 7 | * |
|---|
| 21 | 8 | * Copyright (C) IBM Corporation, 2002, 2004 |
|---|
| 22 | 9 | * |
|---|
| .. | .. |
|---|
| 171 | 158 | #define KRETPROBE_MAX_DATA_SIZE 4096 |
|---|
| 172 | 159 | |
|---|
| 173 | 160 | struct kretprobe_instance { |
|---|
| 174 | | - struct hlist_node hlist; |
|---|
| 161 | + union { |
|---|
| 162 | + struct hlist_node hlist; |
|---|
| 163 | + struct rcu_head rcu; |
|---|
| 164 | + }; |
|---|
| 175 | 165 | struct kretprobe *rp; |
|---|
| 176 | 166 | kprobe_opcode_t *ret_addr; |
|---|
| 177 | 167 | struct task_struct *task; |
|---|
| 178 | 168 | void *fp; |
|---|
| 179 | | - char data[0]; |
|---|
| 169 | + char data[]; |
|---|
| 180 | 170 | }; |
|---|
| 181 | 171 | |
|---|
| 182 | 172 | struct kretprobe_blackpoint { |
|---|
| .. | .. |
|---|
| 202 | 192 | return 1; |
|---|
| 203 | 193 | } |
|---|
| 204 | 194 | |
|---|
| 195 | +extern void kprobe_busy_begin(void); |
|---|
| 196 | +extern void kprobe_busy_end(void); |
|---|
| 197 | + |
|---|
| 205 | 198 | #ifdef CONFIG_KRETPROBES |
|---|
| 206 | 199 | extern void arch_prepare_kretprobe(struct kretprobe_instance *ri, |
|---|
| 207 | 200 | struct pt_regs *regs); |
|---|
| 208 | 201 | extern int arch_trampoline_kprobe(struct kprobe *p); |
|---|
| 202 | + |
|---|
| 203 | +/* If the trampoline handler called from a kprobe, use this version */ |
|---|
| 204 | +unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs, |
|---|
| 205 | + void *trampoline_address, |
|---|
| 206 | + void *frame_pointer); |
|---|
| 207 | + |
|---|
| 208 | +static nokprobe_inline |
|---|
| 209 | +unsigned long kretprobe_trampoline_handler(struct pt_regs *regs, |
|---|
| 210 | + void *trampoline_address, |
|---|
| 211 | + void *frame_pointer) |
|---|
| 212 | +{ |
|---|
| 213 | + unsigned long ret; |
|---|
| 214 | + /* |
|---|
| 215 | + * Set a dummy kprobe for avoiding kretprobe recursion. |
|---|
| 216 | + * Since kretprobe never runs in kprobe handler, no kprobe must |
|---|
| 217 | + * be running at this point. |
|---|
| 218 | + */ |
|---|
| 219 | + kprobe_busy_begin(); |
|---|
| 220 | + ret = __kretprobe_trampoline_handler(regs, trampoline_address, frame_pointer); |
|---|
| 221 | + kprobe_busy_end(); |
|---|
| 222 | + |
|---|
| 223 | + return ret; |
|---|
| 224 | +} |
|---|
| 225 | + |
|---|
| 209 | 226 | #else /* CONFIG_KRETPROBES */ |
|---|
| 210 | 227 | static inline void arch_prepare_kretprobe(struct kretprobe *rp, |
|---|
| 211 | 228 | struct pt_regs *regs) |
|---|
| .. | .. |
|---|
| 218 | 235 | #endif /* CONFIG_KRETPROBES */ |
|---|
| 219 | 236 | |
|---|
| 220 | 237 | extern struct kretprobe_blackpoint kretprobe_blacklist[]; |
|---|
| 221 | | - |
|---|
| 222 | | -static inline void kretprobe_assert(struct kretprobe_instance *ri, |
|---|
| 223 | | - unsigned long orig_ret_address, unsigned long trampoline_address) |
|---|
| 224 | | -{ |
|---|
| 225 | | - if (!orig_ret_address || (orig_ret_address == trampoline_address)) { |
|---|
| 226 | | - printk("kretprobe BUG!: Processing kretprobe %p @ %p\n", |
|---|
| 227 | | - ri->rp, ri->rp->kp.addr); |
|---|
| 228 | | - BUG(); |
|---|
| 229 | | - } |
|---|
| 230 | | -} |
|---|
| 231 | 238 | |
|---|
| 232 | 239 | #ifdef CONFIG_KPROBES_SANITY_TEST |
|---|
| 233 | 240 | extern int init_test_probes(void); |
|---|
| .. | .. |
|---|
| 242 | 249 | extern void arch_arm_kprobe(struct kprobe *p); |
|---|
| 243 | 250 | extern void arch_disarm_kprobe(struct kprobe *p); |
|---|
| 244 | 251 | extern int arch_init_kprobes(void); |
|---|
| 245 | | -extern void show_registers(struct pt_regs *regs); |
|---|
| 246 | 252 | extern void kprobes_inc_nmissed_count(struct kprobe *p); |
|---|
| 247 | 253 | extern bool arch_within_kprobe_blacklist(unsigned long addr); |
|---|
| 248 | 254 | extern int arch_populate_kprobe_blacklist(void); |
|---|
| .. | .. |
|---|
| 257 | 263 | struct mutex mutex; |
|---|
| 258 | 264 | void *(*alloc)(void); /* allocate insn page */ |
|---|
| 259 | 265 | void (*free)(void *); /* free insn page */ |
|---|
| 266 | + const char *sym; /* symbol for insn pages */ |
|---|
| 260 | 267 | struct list_head pages; /* list of kprobe_insn_page */ |
|---|
| 261 | 268 | size_t insn_size; /* size of instruction slot */ |
|---|
| 262 | 269 | int nr_garbage; |
|---|
| .. | .. |
|---|
| 287 | 294 | { \ |
|---|
| 288 | 295 | return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \ |
|---|
| 289 | 296 | } |
|---|
| 297 | +#define KPROBE_INSN_PAGE_SYM "kprobe_insn_page" |
|---|
| 298 | +#define KPROBE_OPTINSN_PAGE_SYM "kprobe_optinsn_page" |
|---|
| 299 | +int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum, |
|---|
| 300 | + unsigned long *value, char *type, char *sym); |
|---|
| 290 | 301 | #else /* __ARCH_WANT_KPROBES_INSN_SLOT */ |
|---|
| 291 | 302 | #define DEFINE_INSN_CACHE_OPS(__name) \ |
|---|
| 292 | 303 | static inline bool is_kprobe_##__name##_slot(unsigned long addr) \ |
|---|
| .. | .. |
|---|
| 327 | 338 | #ifdef CONFIG_SYSCTL |
|---|
| 328 | 339 | extern int sysctl_kprobes_optimization; |
|---|
| 329 | 340 | extern int proc_kprobes_optimization_handler(struct ctl_table *table, |
|---|
| 330 | | - int write, void __user *buffer, |
|---|
| 341 | + int write, void *buffer, |
|---|
| 331 | 342 | size_t *length, loff_t *ppos); |
|---|
| 332 | 343 | #endif |
|---|
| 333 | 344 | extern void wait_for_kprobe_optimizer(void); |
|---|
| 345 | +bool optprobe_queued_unopt(struct optimized_kprobe *op); |
|---|
| 346 | +bool kprobe_disarmed(struct kprobe *p); |
|---|
| 334 | 347 | #else |
|---|
| 335 | 348 | static inline void wait_for_kprobe_optimizer(void) { } |
|---|
| 336 | 349 | #endif /* CONFIG_OPTPROBES */ |
|---|
| .. | .. |
|---|
| 344 | 357 | |
|---|
| 345 | 358 | /* Get the kprobe at this addr (if any) - called with preemption disabled */ |
|---|
| 346 | 359 | struct kprobe *get_kprobe(void *addr); |
|---|
| 347 | | -void kretprobe_hash_lock(struct task_struct *tsk, |
|---|
| 348 | | - struct hlist_head **head, unsigned long *flags); |
|---|
| 349 | | -void kretprobe_hash_unlock(struct task_struct *tsk, unsigned long *flags); |
|---|
| 350 | | -struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk); |
|---|
| 351 | 360 | |
|---|
| 352 | 361 | /* kprobe_running() will just return the current_kprobe on this CPU */ |
|---|
| 353 | 362 | static inline struct kprobe *kprobe_running(void) |
|---|
| .. | .. |
|---|
| 365 | 374 | return this_cpu_ptr(&kprobe_ctlblk); |
|---|
| 366 | 375 | } |
|---|
| 367 | 376 | |
|---|
| 368 | | -extern struct kprobe kprobe_busy; |
|---|
| 369 | | -void kprobe_busy_begin(void); |
|---|
| 370 | | -void kprobe_busy_end(void); |
|---|
| 371 | | - |
|---|
| 372 | 377 | kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset); |
|---|
| 373 | 378 | int register_kprobe(struct kprobe *p); |
|---|
| 374 | 379 | void unregister_kprobe(struct kprobe *p); |
|---|
| .. | .. |
|---|
| 382 | 387 | void unregister_kretprobes(struct kretprobe **rps, int num); |
|---|
| 383 | 388 | |
|---|
| 384 | 389 | void kprobe_flush_task(struct task_struct *tk); |
|---|
| 385 | | -void recycle_rp_inst(struct kretprobe_instance *ri, struct hlist_head *head); |
|---|
| 390 | + |
|---|
| 391 | +void kprobe_free_init_mem(void); |
|---|
| 386 | 392 | |
|---|
| 387 | 393 | int disable_kprobe(struct kprobe *kp); |
|---|
| 388 | 394 | int enable_kprobe(struct kprobe *kp); |
|---|
| 389 | 395 | |
|---|
| 390 | 396 | void dump_kprobe(struct kprobe *kp); |
|---|
| 391 | 397 | |
|---|
| 398 | +void *alloc_insn_page(void); |
|---|
| 399 | +void free_insn_page(void *page); |
|---|
| 400 | + |
|---|
| 401 | +int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type, |
|---|
| 402 | + char *sym); |
|---|
| 403 | + |
|---|
| 404 | +int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value, |
|---|
| 405 | + char *type, char *sym); |
|---|
| 392 | 406 | #else /* !CONFIG_KPROBES: */ |
|---|
| 393 | 407 | |
|---|
| 394 | 408 | static inline int kprobes_built_in(void) |
|---|
| .. | .. |
|---|
| 438 | 452 | static inline void kprobe_flush_task(struct task_struct *tk) |
|---|
| 439 | 453 | { |
|---|
| 440 | 454 | } |
|---|
| 455 | +static inline void kprobe_free_init_mem(void) |
|---|
| 456 | +{ |
|---|
| 457 | +} |
|---|
| 441 | 458 | static inline int disable_kprobe(struct kprobe *kp) |
|---|
| 442 | 459 | { |
|---|
| 443 | 460 | return -ENOSYS; |
|---|
| .. | .. |
|---|
| 445 | 462 | static inline int enable_kprobe(struct kprobe *kp) |
|---|
| 446 | 463 | { |
|---|
| 447 | 464 | return -ENOSYS; |
|---|
| 465 | +} |
|---|
| 466 | + |
|---|
| 467 | +static inline bool within_kprobe_blacklist(unsigned long addr) |
|---|
| 468 | +{ |
|---|
| 469 | + return true; |
|---|
| 470 | +} |
|---|
| 471 | +static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, |
|---|
| 472 | + char *type, char *sym) |
|---|
| 473 | +{ |
|---|
| 474 | + return -ERANGE; |
|---|
| 448 | 475 | } |
|---|
| 449 | 476 | #endif /* CONFIG_KPROBES */ |
|---|
| 450 | 477 | static inline int disable_kretprobe(struct kretprobe *rp) |
|---|
| .. | .. |
|---|
| 469 | 496 | } |
|---|
| 470 | 497 | #endif |
|---|
| 471 | 498 | |
|---|
| 499 | +/* Returns true if kprobes handled the fault */ |
|---|
| 500 | +static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs, |
|---|
| 501 | + unsigned int trap) |
|---|
| 502 | +{ |
|---|
| 503 | + if (!kprobes_built_in()) |
|---|
| 504 | + return false; |
|---|
| 505 | + if (user_mode(regs)) |
|---|
| 506 | + return false; |
|---|
| 507 | + /* |
|---|
| 508 | + * To be potentially processing a kprobe fault and to be allowed |
|---|
| 509 | + * to call kprobe_running(), we have to be non-preemptible. |
|---|
| 510 | + */ |
|---|
| 511 | + if (preemptible()) |
|---|
| 512 | + return false; |
|---|
| 513 | + if (!kprobe_running()) |
|---|
| 514 | + return false; |
|---|
| 515 | + return kprobe_fault_handler(regs, trap); |
|---|
| 516 | +} |
|---|
| 517 | + |
|---|
| 472 | 518 | #endif /* _LINUX_KPROBES_H */ |
|---|