kprobes.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. * Kernel Probes (KProbes)
  3. * kernel/kprobes.c
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. *
  19. * Copyright (C) IBM Corporation, 2002, 2004
  20. *
  21. * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
  22. * Probes initial implementation (includes suggestions from
  23. * Rusty Russell).
  24. * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with
  25. * hlists and exceptions notifier as suggested by Andi Kleen.
  26. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  27. * interface to access function arguments.
  28. * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes
  29. * exceptions notifier to be first on the priority list.
  30. * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
  31. * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
  32. * <prasanna@in.ibm.com> added function-return probes.
  33. */
  34. #include <linux/kprobes.h>
  35. #include <linux/hash.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/module.h>
  39. #include <linux/moduleloader.h>
  40. #include <linux/kallsyms.h>
  41. #include <asm-generic/sections.h>
  42. #include <asm/cacheflush.h>
  43. #include <asm/errno.h>
  44. #include <asm/kdebug.h>
  45. #define KPROBE_HASH_BITS 6
  46. #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
  47. /*
  48. * Some oddball architectures like 64bit powerpc have function descriptors
  49. * so this must be overridable.
  50. */
  51. #ifndef kprobe_lookup_name
  52. #define kprobe_lookup_name(name, addr) \
  53. addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
  54. #endif
  55. static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
  56. static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
  57. static atomic_t kprobe_count;
  58. DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
  59. DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */
  60. static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
  61. static struct notifier_block kprobe_page_fault_nb = {
  62. .notifier_call = kprobe_exceptions_notify,
  63. .priority = 0x7fffffff /* we need to notified first */
  64. };
  65. #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
  66. /*
  67. * kprobe->ainsn.insn points to the copy of the instruction to be
  68. * single-stepped. x86_64, POWER4 and above have no-exec support and
  69. * stepping on the instruction on a vmalloced/kmalloced/data page
  70. * is a recipe for disaster
  71. */
  72. #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
  73. struct kprobe_insn_page {
  74. struct hlist_node hlist;
  75. kprobe_opcode_t *insns; /* Page of instruction slots */
  76. char slot_used[INSNS_PER_PAGE];
  77. int nused;
  78. };
  79. static struct hlist_head kprobe_insn_pages;
  80. /**
  81. * get_insn_slot() - Find a slot on an executable page for an instruction.
  82. * We allocate an executable page if there's no room on existing ones.
  83. */
  84. kprobe_opcode_t __kprobes *get_insn_slot(void)
  85. {
  86. struct kprobe_insn_page *kip;
  87. struct hlist_node *pos;
  88. hlist_for_each(pos, &kprobe_insn_pages) {
  89. kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
  90. if (kip->nused < INSNS_PER_PAGE) {
  91. int i;
  92. for (i = 0; i < INSNS_PER_PAGE; i++) {
  93. if (!kip->slot_used[i]) {
  94. kip->slot_used[i] = 1;
  95. kip->nused++;
  96. return kip->insns + (i * MAX_INSN_SIZE);
  97. }
  98. }
  99. /* Surprise! No unused slots. Fix kip->nused. */
  100. kip->nused = INSNS_PER_PAGE;
  101. }
  102. }
  103. /* All out of space. Need to allocate a new page. Use slot 0.*/
  104. kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
  105. if (!kip) {
  106. return NULL;
  107. }
  108. /*
  109. * Use module_alloc so this page is within +/- 2GB of where the
  110. * kernel image and loaded module images reside. This is required
  111. * so x86_64 can correctly handle the %rip-relative fixups.
  112. */
  113. kip->insns = module_alloc(PAGE_SIZE);
  114. if (!kip->insns) {
  115. kfree(kip);
  116. return NULL;
  117. }
  118. INIT_HLIST_NODE(&kip->hlist);
  119. hlist_add_head(&kip->hlist, &kprobe_insn_pages);
  120. memset(kip->slot_used, 0, INSNS_PER_PAGE);
  121. kip->slot_used[0] = 1;
  122. kip->nused = 1;
  123. return kip->insns;
  124. }
  125. void __kprobes free_insn_slot(kprobe_opcode_t *slot)
  126. {
  127. struct kprobe_insn_page *kip;
  128. struct hlist_node *pos;
  129. hlist_for_each(pos, &kprobe_insn_pages) {
  130. kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
  131. if (kip->insns <= slot &&
  132. slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
  133. int i = (slot - kip->insns) / MAX_INSN_SIZE;
  134. kip->slot_used[i] = 0;
  135. kip->nused--;
  136. if (kip->nused == 0) {
  137. /*
  138. * Page is no longer in use. Free it unless
  139. * it's the last one. We keep the last one
  140. * so as not to have to set it up again the
  141. * next time somebody inserts a probe.
  142. */
  143. hlist_del(&kip->hlist);
  144. if (hlist_empty(&kprobe_insn_pages)) {
  145. INIT_HLIST_NODE(&kip->hlist);
  146. hlist_add_head(&kip->hlist,
  147. &kprobe_insn_pages);
  148. } else {
  149. module_free(NULL, kip->insns);
  150. kfree(kip);
  151. }
  152. }
  153. return;
  154. }
  155. }
  156. }
  157. #endif
  158. /* We have preemption disabled.. so it is safe to use __ versions */
  159. static inline void set_kprobe_instance(struct kprobe *kp)
  160. {
  161. __get_cpu_var(kprobe_instance) = kp;
  162. }
  163. static inline void reset_kprobe_instance(void)
  164. {
  165. __get_cpu_var(kprobe_instance) = NULL;
  166. }
  167. /*
  168. * This routine is called either:
  169. * - under the kprobe_mutex - during kprobe_[un]register()
  170. * OR
  171. * - with preemption disabled - from arch/xxx/kernel/kprobes.c
  172. */
  173. struct kprobe __kprobes *get_kprobe(void *addr)
  174. {
  175. struct hlist_head *head;
  176. struct hlist_node *node;
  177. struct kprobe *p;
  178. head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
  179. hlist_for_each_entry_rcu(p, node, head, hlist) {
  180. if (p->addr == addr)
  181. return p;
  182. }
  183. return NULL;
  184. }
  185. /*
  186. * Aggregate handlers for multiple kprobes support - these handlers
  187. * take care of invoking the individual kprobe handlers on p->list
  188. */
  189. static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  190. {
  191. struct kprobe *kp;
  192. list_for_each_entry_rcu(kp, &p->list, list) {
  193. if (kp->pre_handler) {
  194. set_kprobe_instance(kp);
  195. if (kp->pre_handler(kp, regs))
  196. return 1;
  197. }
  198. reset_kprobe_instance();
  199. }
  200. return 0;
  201. }
  202. static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  203. unsigned long flags)
  204. {
  205. struct kprobe *kp;
  206. list_for_each_entry_rcu(kp, &p->list, list) {
  207. if (kp->post_handler) {
  208. set_kprobe_instance(kp);
  209. kp->post_handler(kp, regs, flags);
  210. reset_kprobe_instance();
  211. }
  212. }
  213. return;
  214. }
  215. static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
  216. int trapnr)
  217. {
  218. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  219. /*
  220. * if we faulted "during" the execution of a user specified
  221. * probe handler, invoke just that probe's fault handler
  222. */
  223. if (cur && cur->fault_handler) {
  224. if (cur->fault_handler(cur, regs, trapnr))
  225. return 1;
  226. }
  227. return 0;
  228. }
  229. static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
  230. {
  231. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  232. int ret = 0;
  233. if (cur && cur->break_handler) {
  234. if (cur->break_handler(cur, regs))
  235. ret = 1;
  236. }
  237. reset_kprobe_instance();
  238. return ret;
  239. }
  240. /* Walks the list and increments nmissed count for multiprobe case */
  241. void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
  242. {
  243. struct kprobe *kp;
  244. if (p->pre_handler != aggr_pre_handler) {
  245. p->nmissed++;
  246. } else {
  247. list_for_each_entry_rcu(kp, &p->list, list)
  248. kp->nmissed++;
  249. }
  250. return;
  251. }
  252. /* Called with kretprobe_lock held */
  253. struct kretprobe_instance __kprobes *get_free_rp_inst(struct kretprobe *rp)
  254. {
  255. struct hlist_node *node;
  256. struct kretprobe_instance *ri;
  257. hlist_for_each_entry(ri, node, &rp->free_instances, uflist)
  258. return ri;
  259. return NULL;
  260. }
  261. /* Called with kretprobe_lock held */
  262. static struct kretprobe_instance __kprobes *get_used_rp_inst(struct kretprobe
  263. *rp)
  264. {
  265. struct hlist_node *node;
  266. struct kretprobe_instance *ri;
  267. hlist_for_each_entry(ri, node, &rp->used_instances, uflist)
  268. return ri;
  269. return NULL;
  270. }
  271. /* Called with kretprobe_lock held */
  272. void __kprobes add_rp_inst(struct kretprobe_instance *ri)
  273. {
  274. /*
  275. * Remove rp inst off the free list -
  276. * Add it back when probed function returns
  277. */
  278. hlist_del(&ri->uflist);
  279. /* Add rp inst onto table */
  280. INIT_HLIST_NODE(&ri->hlist);
  281. hlist_add_head(&ri->hlist,
  282. &kretprobe_inst_table[hash_ptr(ri->task, KPROBE_HASH_BITS)]);
  283. /* Also add this rp inst to the used list. */
  284. INIT_HLIST_NODE(&ri->uflist);
  285. hlist_add_head(&ri->uflist, &ri->rp->used_instances);
  286. }
  287. /* Called with kretprobe_lock held */
  288. void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
  289. struct hlist_head *head)
  290. {
  291. /* remove rp inst off the rprobe_inst_table */
  292. hlist_del(&ri->hlist);
  293. if (ri->rp) {
  294. /* remove rp inst off the used list */
  295. hlist_del(&ri->uflist);
  296. /* put rp inst back onto the free list */
  297. INIT_HLIST_NODE(&ri->uflist);
  298. hlist_add_head(&ri->uflist, &ri->rp->free_instances);
  299. } else
  300. /* Unregistering */
  301. hlist_add_head(&ri->hlist, head);
  302. }
  303. struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
  304. {
  305. return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
  306. }
  307. /*
  308. * This function is called from finish_task_switch when task tk becomes dead,
  309. * so that we can recycle any function-return probe instances associated
  310. * with this task. These left over instances represent probed functions
  311. * that have been called but will never return.
  312. */
  313. void __kprobes kprobe_flush_task(struct task_struct *tk)
  314. {
  315. struct kretprobe_instance *ri;
  316. struct hlist_head *head, empty_rp;
  317. struct hlist_node *node, *tmp;
  318. unsigned long flags = 0;
  319. INIT_HLIST_HEAD(&empty_rp);
  320. spin_lock_irqsave(&kretprobe_lock, flags);
  321. head = kretprobe_inst_table_head(tk);
  322. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  323. if (ri->task == tk)
  324. recycle_rp_inst(ri, &empty_rp);
  325. }
  326. spin_unlock_irqrestore(&kretprobe_lock, flags);
  327. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  328. hlist_del(&ri->hlist);
  329. kfree(ri);
  330. }
  331. }
  332. static inline void free_rp_inst(struct kretprobe *rp)
  333. {
  334. struct kretprobe_instance *ri;
  335. while ((ri = get_free_rp_inst(rp)) != NULL) {
  336. hlist_del(&ri->uflist);
  337. kfree(ri);
  338. }
  339. }
  340. /*
  341. * Keep all fields in the kprobe consistent
  342. */
  343. static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
  344. {
  345. memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
  346. memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
  347. }
  348. /*
  349. * Add the new probe to old_p->list. Fail if this is the
  350. * second jprobe at the address - two jprobes can't coexist
  351. */
  352. static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
  353. {
  354. if (p->break_handler) {
  355. if (old_p->break_handler)
  356. return -EEXIST;
  357. list_add_tail_rcu(&p->list, &old_p->list);
  358. old_p->break_handler = aggr_break_handler;
  359. } else
  360. list_add_rcu(&p->list, &old_p->list);
  361. if (p->post_handler && !old_p->post_handler)
  362. old_p->post_handler = aggr_post_handler;
  363. return 0;
  364. }
  365. /*
  366. * Fill in the required fields of the "manager kprobe". Replace the
  367. * earlier kprobe in the hlist with the manager kprobe
  368. */
  369. static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  370. {
  371. copy_kprobe(p, ap);
  372. flush_insn_slot(ap);
  373. ap->addr = p->addr;
  374. ap->pre_handler = aggr_pre_handler;
  375. ap->fault_handler = aggr_fault_handler;
  376. if (p->post_handler)
  377. ap->post_handler = aggr_post_handler;
  378. if (p->break_handler)
  379. ap->break_handler = aggr_break_handler;
  380. INIT_LIST_HEAD(&ap->list);
  381. list_add_rcu(&p->list, &ap->list);
  382. hlist_replace_rcu(&p->hlist, &ap->hlist);
  383. }
  384. /*
  385. * This is the second or subsequent kprobe at the address - handle
  386. * the intricacies
  387. */
  388. static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
  389. struct kprobe *p)
  390. {
  391. int ret = 0;
  392. struct kprobe *ap;
  393. if (old_p->pre_handler == aggr_pre_handler) {
  394. copy_kprobe(old_p, p);
  395. ret = add_new_kprobe(old_p, p);
  396. } else {
  397. ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
  398. if (!ap)
  399. return -ENOMEM;
  400. add_aggr_kprobe(ap, old_p);
  401. copy_kprobe(ap, p);
  402. ret = add_new_kprobe(ap, p);
  403. }
  404. return ret;
  405. }
  406. static int __kprobes in_kprobes_functions(unsigned long addr)
  407. {
  408. if (addr >= (unsigned long)__kprobes_text_start
  409. && addr < (unsigned long)__kprobes_text_end)
  410. return -EINVAL;
  411. return 0;
  412. }
  413. static int __kprobes __register_kprobe(struct kprobe *p,
  414. unsigned long called_from)
  415. {
  416. int ret = 0;
  417. struct kprobe *old_p;
  418. struct module *probed_mod;
  419. /*
  420. * If we have a symbol_name argument look it up,
  421. * and add it to the address. That way the addr
  422. * field can either be global or relative to a symbol.
  423. */
  424. if (p->symbol_name) {
  425. if (p->addr)
  426. return -EINVAL;
  427. kprobe_lookup_name(p->symbol_name, p->addr);
  428. }
  429. if (!p->addr)
  430. return -EINVAL;
  431. p->addr = (kprobe_opcode_t *)(((char *)p->addr)+ p->offset);
  432. if ((!kernel_text_address((unsigned long) p->addr)) ||
  433. in_kprobes_functions((unsigned long) p->addr))
  434. return -EINVAL;
  435. p->mod_refcounted = 0;
  436. /* Check are we probing a module */
  437. if ((probed_mod = module_text_address((unsigned long) p->addr))) {
  438. struct module *calling_mod = module_text_address(called_from);
  439. /* We must allow modules to probe themself and
  440. * in this case avoid incrementing the module refcount,
  441. * so as to allow unloading of self probing modules.
  442. */
  443. if (calling_mod && (calling_mod != probed_mod)) {
  444. if (unlikely(!try_module_get(probed_mod)))
  445. return -EINVAL;
  446. p->mod_refcounted = 1;
  447. } else
  448. probed_mod = NULL;
  449. }
  450. p->nmissed = 0;
  451. mutex_lock(&kprobe_mutex);
  452. old_p = get_kprobe(p->addr);
  453. if (old_p) {
  454. ret = register_aggr_kprobe(old_p, p);
  455. if (!ret)
  456. atomic_inc(&kprobe_count);
  457. goto out;
  458. }
  459. if ((ret = arch_prepare_kprobe(p)) != 0)
  460. goto out;
  461. INIT_HLIST_NODE(&p->hlist);
  462. hlist_add_head_rcu(&p->hlist,
  463. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  464. if (atomic_add_return(1, &kprobe_count) == \
  465. (ARCH_INACTIVE_KPROBE_COUNT + 1))
  466. register_page_fault_notifier(&kprobe_page_fault_nb);
  467. arch_arm_kprobe(p);
  468. out:
  469. mutex_unlock(&kprobe_mutex);
  470. if (ret && probed_mod)
  471. module_put(probed_mod);
  472. return ret;
  473. }
  474. int __kprobes register_kprobe(struct kprobe *p)
  475. {
  476. return __register_kprobe(p,
  477. (unsigned long)__builtin_return_address(0));
  478. }
  479. void __kprobes unregister_kprobe(struct kprobe *p)
  480. {
  481. struct module *mod;
  482. struct kprobe *old_p, *list_p;
  483. int cleanup_p;
  484. mutex_lock(&kprobe_mutex);
  485. old_p = get_kprobe(p->addr);
  486. if (unlikely(!old_p)) {
  487. mutex_unlock(&kprobe_mutex);
  488. return;
  489. }
  490. if (p != old_p) {
  491. list_for_each_entry_rcu(list_p, &old_p->list, list)
  492. if (list_p == p)
  493. /* kprobe p is a valid probe */
  494. goto valid_p;
  495. mutex_unlock(&kprobe_mutex);
  496. return;
  497. }
  498. valid_p:
  499. if ((old_p == p) || ((old_p->pre_handler == aggr_pre_handler) &&
  500. (p->list.next == &old_p->list) &&
  501. (p->list.prev == &old_p->list))) {
  502. /* Only probe on the hash list */
  503. arch_disarm_kprobe(p);
  504. hlist_del_rcu(&old_p->hlist);
  505. cleanup_p = 1;
  506. } else {
  507. list_del_rcu(&p->list);
  508. cleanup_p = 0;
  509. }
  510. mutex_unlock(&kprobe_mutex);
  511. synchronize_sched();
  512. if (p->mod_refcounted &&
  513. (mod = module_text_address((unsigned long)p->addr)))
  514. module_put(mod);
  515. if (cleanup_p) {
  516. if (p != old_p) {
  517. list_del_rcu(&p->list);
  518. kfree(old_p);
  519. }
  520. arch_remove_kprobe(p);
  521. } else {
  522. mutex_lock(&kprobe_mutex);
  523. if (p->break_handler)
  524. old_p->break_handler = NULL;
  525. if (p->post_handler){
  526. list_for_each_entry_rcu(list_p, &old_p->list, list){
  527. if (list_p->post_handler){
  528. cleanup_p = 2;
  529. break;
  530. }
  531. }
  532. if (cleanup_p == 0)
  533. old_p->post_handler = NULL;
  534. }
  535. mutex_unlock(&kprobe_mutex);
  536. }
  537. /* Call unregister_page_fault_notifier()
  538. * if no probes are active
  539. */
  540. mutex_lock(&kprobe_mutex);
  541. if (atomic_add_return(-1, &kprobe_count) == \
  542. ARCH_INACTIVE_KPROBE_COUNT)
  543. unregister_page_fault_notifier(&kprobe_page_fault_nb);
  544. mutex_unlock(&kprobe_mutex);
  545. return;
  546. }
  547. static struct notifier_block kprobe_exceptions_nb = {
  548. .notifier_call = kprobe_exceptions_notify,
  549. .priority = 0x7fffffff /* we need to be notified first */
  550. };
  551. int __kprobes register_jprobe(struct jprobe *jp)
  552. {
  553. /* Todo: Verify probepoint is a function entry point */
  554. jp->kp.pre_handler = setjmp_pre_handler;
  555. jp->kp.break_handler = longjmp_break_handler;
  556. return __register_kprobe(&jp->kp,
  557. (unsigned long)__builtin_return_address(0));
  558. }
  559. void __kprobes unregister_jprobe(struct jprobe *jp)
  560. {
  561. unregister_kprobe(&jp->kp);
  562. }
  563. #ifdef ARCH_SUPPORTS_KRETPROBES
  564. /*
  565. * This kprobe pre_handler is registered with every kretprobe. When probe
  566. * hits it will set up the return probe.
  567. */
  568. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  569. struct pt_regs *regs)
  570. {
  571. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  572. unsigned long flags = 0;
  573. /*TODO: consider to only swap the RA after the last pre_handler fired */
  574. spin_lock_irqsave(&kretprobe_lock, flags);
  575. arch_prepare_kretprobe(rp, regs);
  576. spin_unlock_irqrestore(&kretprobe_lock, flags);
  577. return 0;
  578. }
  579. int __kprobes register_kretprobe(struct kretprobe *rp)
  580. {
  581. int ret = 0;
  582. struct kretprobe_instance *inst;
  583. int i;
  584. rp->kp.pre_handler = pre_handler_kretprobe;
  585. rp->kp.post_handler = NULL;
  586. rp->kp.fault_handler = NULL;
  587. rp->kp.break_handler = NULL;
  588. /* Pre-allocate memory for max kretprobe instances */
  589. if (rp->maxactive <= 0) {
  590. #ifdef CONFIG_PREEMPT
  591. rp->maxactive = max(10, 2 * NR_CPUS);
  592. #else
  593. rp->maxactive = NR_CPUS;
  594. #endif
  595. }
  596. INIT_HLIST_HEAD(&rp->used_instances);
  597. INIT_HLIST_HEAD(&rp->free_instances);
  598. for (i = 0; i < rp->maxactive; i++) {
  599. inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL);
  600. if (inst == NULL) {
  601. free_rp_inst(rp);
  602. return -ENOMEM;
  603. }
  604. INIT_HLIST_NODE(&inst->uflist);
  605. hlist_add_head(&inst->uflist, &rp->free_instances);
  606. }
  607. rp->nmissed = 0;
  608. /* Establish function entry probe point */
  609. if ((ret = __register_kprobe(&rp->kp,
  610. (unsigned long)__builtin_return_address(0))) != 0)
  611. free_rp_inst(rp);
  612. return ret;
  613. }
  614. #else /* ARCH_SUPPORTS_KRETPROBES */
  615. int __kprobes register_kretprobe(struct kretprobe *rp)
  616. {
  617. return -ENOSYS;
  618. }
  619. #endif /* ARCH_SUPPORTS_KRETPROBES */
  620. void __kprobes unregister_kretprobe(struct kretprobe *rp)
  621. {
  622. unsigned long flags;
  623. struct kretprobe_instance *ri;
  624. unregister_kprobe(&rp->kp);
  625. /* No race here */
  626. spin_lock_irqsave(&kretprobe_lock, flags);
  627. while ((ri = get_used_rp_inst(rp)) != NULL) {
  628. ri->rp = NULL;
  629. hlist_del(&ri->uflist);
  630. }
  631. spin_unlock_irqrestore(&kretprobe_lock, flags);
  632. free_rp_inst(rp);
  633. }
  634. static int __init init_kprobes(void)
  635. {
  636. int i, err = 0;
  637. /* FIXME allocate the probe table, currently defined statically */
  638. /* initialize all list heads */
  639. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  640. INIT_HLIST_HEAD(&kprobe_table[i]);
  641. INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
  642. }
  643. atomic_set(&kprobe_count, 0);
  644. err = arch_init_kprobes();
  645. if (!err)
  646. err = register_die_notifier(&kprobe_exceptions_nb);
  647. return err;
  648. }
  649. __initcall(init_kprobes);
  650. EXPORT_SYMBOL_GPL(register_kprobe);
  651. EXPORT_SYMBOL_GPL(unregister_kprobe);
  652. EXPORT_SYMBOL_GPL(register_jprobe);
  653. EXPORT_SYMBOL_GPL(unregister_jprobe);
  654. EXPORT_SYMBOL_GPL(jprobe_return);
  655. EXPORT_SYMBOL_GPL(register_kretprobe);
  656. EXPORT_SYMBOL_GPL(unregister_kretprobe);