kprobes.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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/spinlock.h>
  36. #include <linux/hash.h>
  37. #include <linux/init.h>
  38. #include <linux/module.h>
  39. #include <linux/moduleloader.h>
  40. #include <asm/cacheflush.h>
  41. #include <asm/errno.h>
  42. #include <asm/kdebug.h>
  43. #define KPROBE_HASH_BITS 6
  44. #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
  45. static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
  46. static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
  47. unsigned int kprobe_cpu = NR_CPUS;
  48. static DEFINE_SPINLOCK(kprobe_lock);
  49. static struct kprobe *curr_kprobe;
  50. /*
  51. * kprobe->ainsn.insn points to the copy of the instruction to be
  52. * single-stepped. x86_64, POWER4 and above have no-exec support and
  53. * stepping on the instruction on a vmalloced/kmalloced/data page
  54. * is a recipe for disaster
  55. */
  56. #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
  57. struct kprobe_insn_page {
  58. struct hlist_node hlist;
  59. kprobe_opcode_t *insns; /* Page of instruction slots */
  60. char slot_used[INSNS_PER_PAGE];
  61. int nused;
  62. };
  63. static struct hlist_head kprobe_insn_pages;
  64. /**
  65. * get_insn_slot() - Find a slot on an executable page for an instruction.
  66. * We allocate an executable page if there's no room on existing ones.
  67. */
  68. kprobe_opcode_t *get_insn_slot(void)
  69. {
  70. struct kprobe_insn_page *kip;
  71. struct hlist_node *pos;
  72. hlist_for_each(pos, &kprobe_insn_pages) {
  73. kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
  74. if (kip->nused < INSNS_PER_PAGE) {
  75. int i;
  76. for (i = 0; i < INSNS_PER_PAGE; i++) {
  77. if (!kip->slot_used[i]) {
  78. kip->slot_used[i] = 1;
  79. kip->nused++;
  80. return kip->insns + (i * MAX_INSN_SIZE);
  81. }
  82. }
  83. /* Surprise! No unused slots. Fix kip->nused. */
  84. kip->nused = INSNS_PER_PAGE;
  85. }
  86. }
  87. /* All out of space. Need to allocate a new page. Use slot 0.*/
  88. kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
  89. if (!kip) {
  90. return NULL;
  91. }
  92. /*
  93. * Use module_alloc so this page is within +/- 2GB of where the
  94. * kernel image and loaded module images reside. This is required
  95. * so x86_64 can correctly handle the %rip-relative fixups.
  96. */
  97. kip->insns = module_alloc(PAGE_SIZE);
  98. if (!kip->insns) {
  99. kfree(kip);
  100. return NULL;
  101. }
  102. INIT_HLIST_NODE(&kip->hlist);
  103. hlist_add_head(&kip->hlist, &kprobe_insn_pages);
  104. memset(kip->slot_used, 0, INSNS_PER_PAGE);
  105. kip->slot_used[0] = 1;
  106. kip->nused = 1;
  107. return kip->insns;
  108. }
  109. void free_insn_slot(kprobe_opcode_t *slot)
  110. {
  111. struct kprobe_insn_page *kip;
  112. struct hlist_node *pos;
  113. hlist_for_each(pos, &kprobe_insn_pages) {
  114. kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
  115. if (kip->insns <= slot &&
  116. slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
  117. int i = (slot - kip->insns) / MAX_INSN_SIZE;
  118. kip->slot_used[i] = 0;
  119. kip->nused--;
  120. if (kip->nused == 0) {
  121. /*
  122. * Page is no longer in use. Free it unless
  123. * it's the last one. We keep the last one
  124. * so as not to have to set it up again the
  125. * next time somebody inserts a probe.
  126. */
  127. hlist_del(&kip->hlist);
  128. if (hlist_empty(&kprobe_insn_pages)) {
  129. INIT_HLIST_NODE(&kip->hlist);
  130. hlist_add_head(&kip->hlist,
  131. &kprobe_insn_pages);
  132. } else {
  133. module_free(NULL, kip->insns);
  134. kfree(kip);
  135. }
  136. }
  137. return;
  138. }
  139. }
  140. }
  141. /* Locks kprobe: irqs must be disabled */
  142. void lock_kprobes(void)
  143. {
  144. spin_lock(&kprobe_lock);
  145. kprobe_cpu = smp_processor_id();
  146. }
  147. void unlock_kprobes(void)
  148. {
  149. kprobe_cpu = NR_CPUS;
  150. spin_unlock(&kprobe_lock);
  151. }
  152. /* You have to be holding the kprobe_lock */
  153. struct kprobe *get_kprobe(void *addr)
  154. {
  155. struct hlist_head *head;
  156. struct hlist_node *node;
  157. head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
  158. hlist_for_each(node, head) {
  159. struct kprobe *p = hlist_entry(node, struct kprobe, hlist);
  160. if (p->addr == addr)
  161. return p;
  162. }
  163. return NULL;
  164. }
  165. /*
  166. * Aggregate handlers for multiple kprobes support - these handlers
  167. * take care of invoking the individual kprobe handlers on p->list
  168. */
  169. static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  170. {
  171. struct kprobe *kp;
  172. list_for_each_entry(kp, &p->list, list) {
  173. if (kp->pre_handler) {
  174. curr_kprobe = kp;
  175. if (kp->pre_handler(kp, regs))
  176. return 1;
  177. }
  178. curr_kprobe = NULL;
  179. }
  180. return 0;
  181. }
  182. static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  183. unsigned long flags)
  184. {
  185. struct kprobe *kp;
  186. list_for_each_entry(kp, &p->list, list) {
  187. if (kp->post_handler) {
  188. curr_kprobe = kp;
  189. kp->post_handler(kp, regs, flags);
  190. curr_kprobe = NULL;
  191. }
  192. }
  193. return;
  194. }
  195. static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
  196. int trapnr)
  197. {
  198. /*
  199. * if we faulted "during" the execution of a user specified
  200. * probe handler, invoke just that probe's fault handler
  201. */
  202. if (curr_kprobe && curr_kprobe->fault_handler) {
  203. if (curr_kprobe->fault_handler(curr_kprobe, regs, trapnr))
  204. return 1;
  205. }
  206. return 0;
  207. }
  208. static int aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
  209. {
  210. struct kprobe *kp = curr_kprobe;
  211. if (curr_kprobe && kp->break_handler) {
  212. if (kp->break_handler(kp, regs)) {
  213. curr_kprobe = NULL;
  214. return 1;
  215. }
  216. }
  217. curr_kprobe = NULL;
  218. return 0;
  219. }
  220. struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp)
  221. {
  222. struct hlist_node *node;
  223. struct kretprobe_instance *ri;
  224. hlist_for_each_entry(ri, node, &rp->free_instances, uflist)
  225. return ri;
  226. return NULL;
  227. }
  228. static struct kretprobe_instance *get_used_rp_inst(struct kretprobe *rp)
  229. {
  230. struct hlist_node *node;
  231. struct kretprobe_instance *ri;
  232. hlist_for_each_entry(ri, node, &rp->used_instances, uflist)
  233. return ri;
  234. return NULL;
  235. }
  236. void add_rp_inst(struct kretprobe_instance *ri)
  237. {
  238. /*
  239. * Remove rp inst off the free list -
  240. * Add it back when probed function returns
  241. */
  242. hlist_del(&ri->uflist);
  243. /* Add rp inst onto table */
  244. INIT_HLIST_NODE(&ri->hlist);
  245. hlist_add_head(&ri->hlist,
  246. &kretprobe_inst_table[hash_ptr(ri->task, KPROBE_HASH_BITS)]);
  247. /* Also add this rp inst to the used list. */
  248. INIT_HLIST_NODE(&ri->uflist);
  249. hlist_add_head(&ri->uflist, &ri->rp->used_instances);
  250. }
  251. void recycle_rp_inst(struct kretprobe_instance *ri)
  252. {
  253. /* remove rp inst off the rprobe_inst_table */
  254. hlist_del(&ri->hlist);
  255. if (ri->rp) {
  256. /* remove rp inst off the used list */
  257. hlist_del(&ri->uflist);
  258. /* put rp inst back onto the free list */
  259. INIT_HLIST_NODE(&ri->uflist);
  260. hlist_add_head(&ri->uflist, &ri->rp->free_instances);
  261. } else
  262. /* Unregistering */
  263. kfree(ri);
  264. }
  265. struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk)
  266. {
  267. return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
  268. }
  269. /*
  270. * This function is called from exit_thread or flush_thread when task tk's
  271. * stack is being recycled so that we can recycle any function-return probe
  272. * instances associated with this task. These left over instances represent
  273. * probed functions that have been called but will never return.
  274. */
  275. void kprobe_flush_task(struct task_struct *tk)
  276. {
  277. struct kretprobe_instance *ri;
  278. struct hlist_head *head;
  279. struct hlist_node *node, *tmp;
  280. unsigned long flags = 0;
  281. spin_lock_irqsave(&kprobe_lock, flags);
  282. head = kretprobe_inst_table_head(current);
  283. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  284. if (ri->task == tk)
  285. recycle_rp_inst(ri);
  286. }
  287. spin_unlock_irqrestore(&kprobe_lock, flags);
  288. }
  289. /*
  290. * This kprobe pre_handler is registered with every kretprobe. When probe
  291. * hits it will set up the return probe.
  292. */
  293. static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
  294. {
  295. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  296. /*TODO: consider to only swap the RA after the last pre_handler fired */
  297. arch_prepare_kretprobe(rp, regs);
  298. return 0;
  299. }
  300. static inline void free_rp_inst(struct kretprobe *rp)
  301. {
  302. struct kretprobe_instance *ri;
  303. while ((ri = get_free_rp_inst(rp)) != NULL) {
  304. hlist_del(&ri->uflist);
  305. kfree(ri);
  306. }
  307. }
  308. /*
  309. * Keep all fields in the kprobe consistent
  310. */
  311. static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
  312. {
  313. memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
  314. memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
  315. }
  316. /*
  317. * Add the new probe to old_p->list. Fail if this is the
  318. * second jprobe at the address - two jprobes can't coexist
  319. */
  320. static int add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
  321. {
  322. struct kprobe *kp;
  323. if (p->break_handler) {
  324. list_for_each_entry(kp, &old_p->list, list) {
  325. if (kp->break_handler)
  326. return -EEXIST;
  327. }
  328. list_add_tail(&p->list, &old_p->list);
  329. } else
  330. list_add(&p->list, &old_p->list);
  331. return 0;
  332. }
  333. /*
  334. * Fill in the required fields of the "manager kprobe". Replace the
  335. * earlier kprobe in the hlist with the manager kprobe
  336. */
  337. static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  338. {
  339. copy_kprobe(p, ap);
  340. ap->addr = p->addr;
  341. ap->pre_handler = aggr_pre_handler;
  342. ap->post_handler = aggr_post_handler;
  343. ap->fault_handler = aggr_fault_handler;
  344. ap->break_handler = aggr_break_handler;
  345. INIT_LIST_HEAD(&ap->list);
  346. list_add(&p->list, &ap->list);
  347. INIT_HLIST_NODE(&ap->hlist);
  348. hlist_del(&p->hlist);
  349. hlist_add_head(&ap->hlist,
  350. &kprobe_table[hash_ptr(ap->addr, KPROBE_HASH_BITS)]);
  351. }
  352. /*
  353. * This is the second or subsequent kprobe at the address - handle
  354. * the intricacies
  355. * TODO: Move kcalloc outside the spinlock
  356. */
  357. static int register_aggr_kprobe(struct kprobe *old_p, struct kprobe *p)
  358. {
  359. int ret = 0;
  360. struct kprobe *ap;
  361. if (old_p->pre_handler == aggr_pre_handler) {
  362. copy_kprobe(old_p, p);
  363. ret = add_new_kprobe(old_p, p);
  364. } else {
  365. ap = kcalloc(1, sizeof(struct kprobe), GFP_ATOMIC);
  366. if (!ap)
  367. return -ENOMEM;
  368. add_aggr_kprobe(ap, old_p);
  369. copy_kprobe(ap, p);
  370. ret = add_new_kprobe(ap, p);
  371. }
  372. return ret;
  373. }
  374. /* kprobe removal house-keeping routines */
  375. static inline void cleanup_kprobe(struct kprobe *p, unsigned long flags)
  376. {
  377. arch_disarm_kprobe(p);
  378. hlist_del(&p->hlist);
  379. spin_unlock_irqrestore(&kprobe_lock, flags);
  380. arch_remove_kprobe(p);
  381. }
  382. static inline void cleanup_aggr_kprobe(struct kprobe *old_p,
  383. struct kprobe *p, unsigned long flags)
  384. {
  385. list_del(&p->list);
  386. if (list_empty(&old_p->list)) {
  387. cleanup_kprobe(old_p, flags);
  388. kfree(old_p);
  389. } else
  390. spin_unlock_irqrestore(&kprobe_lock, flags);
  391. }
  392. int register_kprobe(struct kprobe *p)
  393. {
  394. int ret = 0;
  395. unsigned long flags = 0;
  396. struct kprobe *old_p;
  397. if ((ret = arch_prepare_kprobe(p)) != 0) {
  398. goto rm_kprobe;
  399. }
  400. spin_lock_irqsave(&kprobe_lock, flags);
  401. old_p = get_kprobe(p->addr);
  402. p->nmissed = 0;
  403. if (old_p) {
  404. ret = register_aggr_kprobe(old_p, p);
  405. goto out;
  406. }
  407. arch_copy_kprobe(p);
  408. INIT_HLIST_NODE(&p->hlist);
  409. hlist_add_head(&p->hlist,
  410. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  411. arch_arm_kprobe(p);
  412. out:
  413. spin_unlock_irqrestore(&kprobe_lock, flags);
  414. rm_kprobe:
  415. if (ret == -EEXIST)
  416. arch_remove_kprobe(p);
  417. return ret;
  418. }
  419. void unregister_kprobe(struct kprobe *p)
  420. {
  421. unsigned long flags;
  422. struct kprobe *old_p;
  423. spin_lock_irqsave(&kprobe_lock, flags);
  424. old_p = get_kprobe(p->addr);
  425. if (old_p) {
  426. if (old_p->pre_handler == aggr_pre_handler)
  427. cleanup_aggr_kprobe(old_p, p, flags);
  428. else
  429. cleanup_kprobe(p, flags);
  430. } else
  431. spin_unlock_irqrestore(&kprobe_lock, flags);
  432. }
  433. static struct notifier_block kprobe_exceptions_nb = {
  434. .notifier_call = kprobe_exceptions_notify,
  435. .priority = 0x7fffffff /* we need to notified first */
  436. };
  437. int register_jprobe(struct jprobe *jp)
  438. {
  439. /* Todo: Verify probepoint is a function entry point */
  440. jp->kp.pre_handler = setjmp_pre_handler;
  441. jp->kp.break_handler = longjmp_break_handler;
  442. return register_kprobe(&jp->kp);
  443. }
  444. void unregister_jprobe(struct jprobe *jp)
  445. {
  446. unregister_kprobe(&jp->kp);
  447. }
  448. #ifdef ARCH_SUPPORTS_KRETPROBES
  449. int register_kretprobe(struct kretprobe *rp)
  450. {
  451. int ret = 0;
  452. struct kretprobe_instance *inst;
  453. int i;
  454. rp->kp.pre_handler = pre_handler_kretprobe;
  455. /* Pre-allocate memory for max kretprobe instances */
  456. if (rp->maxactive <= 0) {
  457. #ifdef CONFIG_PREEMPT
  458. rp->maxactive = max(10, 2 * NR_CPUS);
  459. #else
  460. rp->maxactive = NR_CPUS;
  461. #endif
  462. }
  463. INIT_HLIST_HEAD(&rp->used_instances);
  464. INIT_HLIST_HEAD(&rp->free_instances);
  465. for (i = 0; i < rp->maxactive; i++) {
  466. inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL);
  467. if (inst == NULL) {
  468. free_rp_inst(rp);
  469. return -ENOMEM;
  470. }
  471. INIT_HLIST_NODE(&inst->uflist);
  472. hlist_add_head(&inst->uflist, &rp->free_instances);
  473. }
  474. rp->nmissed = 0;
  475. /* Establish function entry probe point */
  476. if ((ret = register_kprobe(&rp->kp)) != 0)
  477. free_rp_inst(rp);
  478. return ret;
  479. }
  480. #else /* ARCH_SUPPORTS_KRETPROBES */
  481. int register_kretprobe(struct kretprobe *rp)
  482. {
  483. return -ENOSYS;
  484. }
  485. #endif /* ARCH_SUPPORTS_KRETPROBES */
  486. void unregister_kretprobe(struct kretprobe *rp)
  487. {
  488. unsigned long flags;
  489. struct kretprobe_instance *ri;
  490. unregister_kprobe(&rp->kp);
  491. /* No race here */
  492. spin_lock_irqsave(&kprobe_lock, flags);
  493. free_rp_inst(rp);
  494. while ((ri = get_used_rp_inst(rp)) != NULL) {
  495. ri->rp = NULL;
  496. hlist_del(&ri->uflist);
  497. }
  498. spin_unlock_irqrestore(&kprobe_lock, flags);
  499. }
  500. static int __init init_kprobes(void)
  501. {
  502. int i, err = 0;
  503. /* FIXME allocate the probe table, currently defined statically */
  504. /* initialize all list heads */
  505. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  506. INIT_HLIST_HEAD(&kprobe_table[i]);
  507. INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
  508. }
  509. err = arch_init();
  510. if (!err)
  511. err = register_die_notifier(&kprobe_exceptions_nb);
  512. return err;
  513. }
  514. __initcall(init_kprobes);
  515. EXPORT_SYMBOL_GPL(register_kprobe);
  516. EXPORT_SYMBOL_GPL(unregister_kprobe);
  517. EXPORT_SYMBOL_GPL(register_jprobe);
  518. EXPORT_SYMBOL_GPL(unregister_jprobe);
  519. EXPORT_SYMBOL_GPL(jprobe_return);
  520. EXPORT_SYMBOL_GPL(register_kretprobe);
  521. EXPORT_SYMBOL_GPL(unregister_kretprobe);